4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
54 #include "delegation.h"
59 #define NFSDBG_FACILITY NFSDBG_PROC
61 #define NFS4_POLL_RETRY_MIN (HZ/10)
62 #define NFS4_POLL_RETRY_MAX (15*HZ)
64 #define NFS4_MAX_LOOP_ON_RECOVER (10)
67 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
68 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
69 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
70 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
71 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
72 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
73 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
74 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
75 struct nfs4_state
*state
);
77 /* Prevent leaks of NFSv4 errors into userland */
78 static int nfs4_map_errors(int err
)
83 case -NFS4ERR_RESOURCE
:
86 dprintk("%s could not handle NFSv4 error %d\n",
94 * This is our standard bitmap for GETATTR requests.
96 const u32 nfs4_fattr_bitmap
[2] = {
101 | FATTR4_WORD0_FILEID
,
103 | FATTR4_WORD1_NUMLINKS
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
113 const u32 nfs4_statfs_bitmap
[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL
,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
122 const u32 nfs4_pathconf_bitmap
[2] = {
124 | FATTR4_WORD0_MAXNAME
,
128 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME
,
135 const u32 nfs4_fs_locations_bitmap
[2] = {
137 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS
,
143 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
154 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
155 struct nfs4_readdir_arg
*readdir
)
159 BUG_ON(readdir
->count
< 80);
161 readdir
->cookie
= cookie
;
162 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
167 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
178 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
181 *p
++ = xdr_one
; /* next */
182 *p
++ = xdr_zero
; /* cookie, first word */
183 *p
++ = xdr_one
; /* cookie, second word */
184 *p
++ = xdr_one
; /* entry len */
185 memcpy(p
, ".\0\0\0", 4); /* entry */
187 *p
++ = xdr_one
; /* bitmap length */
188 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
189 *p
++ = htonl(8); /* attribute buffer length */
190 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
193 *p
++ = xdr_one
; /* next */
194 *p
++ = xdr_zero
; /* cookie, first word */
195 *p
++ = xdr_two
; /* cookie, second word */
196 *p
++ = xdr_two
; /* entry len */
197 memcpy(p
, "..\0\0", 4); /* entry */
199 *p
++ = xdr_one
; /* bitmap length */
200 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
201 *p
++ = htonl(8); /* attribute buffer length */
202 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
204 readdir
->pgbase
= (char *)p
- (char *)start
;
205 readdir
->count
-= readdir
->pgbase
;
206 kunmap_atomic(start
, KM_USER0
);
209 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
215 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
216 nfs_wait_bit_killable
, TASK_KILLABLE
);
220 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
227 *timeout
= NFS4_POLL_RETRY_MIN
;
228 if (*timeout
> NFS4_POLL_RETRY_MAX
)
229 *timeout
= NFS4_POLL_RETRY_MAX
;
230 schedule_timeout_killable(*timeout
);
231 if (fatal_signal_pending(current
))
237 /* This is the error handling routine for processes that are allowed
240 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
242 struct nfs_client
*clp
= server
->nfs_client
;
243 struct nfs4_state
*state
= exception
->state
;
246 exception
->retry
= 0;
250 case -NFS4ERR_ADMIN_REVOKED
:
251 case -NFS4ERR_BAD_STATEID
:
252 case -NFS4ERR_OPENMODE
:
255 nfs4_state_mark_reclaim_nograce(clp
, state
);
256 goto do_state_recovery
;
257 case -NFS4ERR_STALE_STATEID
:
260 nfs4_state_mark_reclaim_reboot(clp
, state
);
261 case -NFS4ERR_STALE_CLIENTID
:
262 case -NFS4ERR_EXPIRED
:
263 goto do_state_recovery
;
264 #if defined(CONFIG_NFS_V4_1)
265 case -NFS4ERR_BADSESSION
:
266 case -NFS4ERR_BADSLOT
:
267 case -NFS4ERR_BAD_HIGH_SLOT
:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
269 case -NFS4ERR_DEADSESSION
:
270 case -NFS4ERR_SEQ_FALSE_RETRY
:
271 case -NFS4ERR_SEQ_MISORDERED
:
272 dprintk("%s ERROR: %d Reset session\n", __func__
,
274 nfs4_schedule_state_recovery(clp
);
275 exception
->retry
= 1;
277 #endif /* defined(CONFIG_NFS_V4_1) */
278 case -NFS4ERR_FILE_OPEN
:
279 if (exception
->timeout
> HZ
) {
280 /* We have retried a decent amount, time to
289 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
292 case -NFS4ERR_OLD_STATEID
:
293 exception
->retry
= 1;
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret
);
298 nfs4_schedule_state_recovery(clp
);
299 ret
= nfs4_wait_clnt_recover(clp
);
301 exception
->retry
= 1;
306 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
308 spin_lock(&clp
->cl_lock
);
309 if (time_before(clp
->cl_last_renewal
,timestamp
))
310 clp
->cl_last_renewal
= timestamp
;
311 spin_unlock(&clp
->cl_lock
);
314 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
316 do_renew_lease(server
->nfs_client
, timestamp
);
319 #if defined(CONFIG_NFS_V4_1)
322 * nfs4_free_slot - free a slot and efficiently update slot table.
324 * freeing a slot is trivially done by clearing its respective bit
326 * If the freed slotid equals highest_used_slotid we want to update it
327 * so that the server would be able to size down the slot table if needed,
328 * otherwise we know that the highest_used_slotid is still in use.
329 * When updating highest_used_slotid there may be "holes" in the bitmap
330 * so we need to scan down from highest_used_slotid to 0 looking for the now
331 * highest slotid in use.
332 * If none found, highest_used_slotid is set to -1.
334 * Must be called while holding tbl->slot_tbl_lock
337 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
339 int slotid
= free_slotid
;
341 /* clear used bit in bitmap */
342 __clear_bit(slotid
, tbl
->used_slots
);
344 /* update highest_used_slotid when it is freed */
345 if (slotid
== tbl
->highest_used_slotid
) {
346 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
347 if (slotid
< tbl
->max_slots
)
348 tbl
->highest_used_slotid
= slotid
;
350 tbl
->highest_used_slotid
= -1;
352 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
353 free_slotid
, tbl
->highest_used_slotid
);
357 * Signal state manager thread if session is drained
359 static void nfs41_check_drain_session_complete(struct nfs4_session
*ses
)
361 struct rpc_task
*task
;
363 if (!test_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
)) {
364 task
= rpc_wake_up_next(&ses
->fc_slot_table
.slot_tbl_waitq
);
366 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
370 if (ses
->fc_slot_table
.highest_used_slotid
!= -1)
373 dprintk("%s COMPLETE: Session Drained\n", __func__
);
374 complete(&ses
->complete
);
377 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
379 struct nfs4_slot_table
*tbl
;
381 tbl
= &res
->sr_session
->fc_slot_table
;
382 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
383 /* just wake up the next guy waiting since
384 * we may have not consumed a slot after all */
385 dprintk("%s: No slot\n", __func__
);
389 spin_lock(&tbl
->slot_tbl_lock
);
390 nfs4_free_slot(tbl
, res
->sr_slotid
);
391 nfs41_check_drain_session_complete(res
->sr_session
);
392 spin_unlock(&tbl
->slot_tbl_lock
);
393 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
396 static int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
398 unsigned long timestamp
;
399 struct nfs4_slot_table
*tbl
;
400 struct nfs4_slot
*slot
;
401 struct nfs_client
*clp
;
404 * sr_status remains 1 if an RPC level error occurred. The server
405 * may or may not have processed the sequence operation..
406 * Proceed as if the server received and processed the sequence
409 if (res
->sr_status
== 1)
410 res
->sr_status
= NFS_OK
;
412 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
413 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
416 tbl
= &res
->sr_session
->fc_slot_table
;
417 slot
= tbl
->slots
+ res
->sr_slotid
;
419 /* Check the SEQUENCE operation status */
420 switch (res
->sr_status
) {
422 /* Update the slot's sequence and clientid lease timer */
424 timestamp
= res
->sr_renewal_time
;
425 clp
= res
->sr_session
->clp
;
426 do_renew_lease(clp
, timestamp
);
427 /* Check sequence flags */
428 if (atomic_read(&clp
->cl_count
) > 1)
429 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
432 /* The server detected a resend of the RPC call and
433 * returned NFS4ERR_DELAY as per Section 2.10.6.2
436 dprintk("%s: slot=%d seq=%d: Operation in progress\n",
437 __func__
, res
->sr_slotid
, slot
->seq_nr
);
440 /* Just update the slot sequence no. */
444 /* The session may be reset by one of the error handlers. */
445 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
446 nfs41_sequence_free_slot(res
);
449 if (!rpc_restart_call(task
))
451 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
455 static int nfs4_sequence_done(struct rpc_task
*task
,
456 struct nfs4_sequence_res
*res
)
458 if (res
->sr_session
== NULL
)
460 return nfs41_sequence_done(task
, res
);
464 * nfs4_find_slot - efficiently look for a free slot
466 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
467 * If found, we mark the slot as used, update the highest_used_slotid,
468 * and respectively set up the sequence operation args.
469 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
471 * Note: must be called with under the slot_tbl_lock.
474 nfs4_find_slot(struct nfs4_slot_table
*tbl
)
477 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
478 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
480 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
481 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
483 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
484 if (slotid
>= tbl
->max_slots
)
486 __set_bit(slotid
, tbl
->used_slots
);
487 if (slotid
> tbl
->highest_used_slotid
)
488 tbl
->highest_used_slotid
= slotid
;
491 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
492 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
496 static int nfs41_setup_sequence(struct nfs4_session
*session
,
497 struct nfs4_sequence_args
*args
,
498 struct nfs4_sequence_res
*res
,
500 struct rpc_task
*task
)
502 struct nfs4_slot
*slot
;
503 struct nfs4_slot_table
*tbl
;
506 dprintk("--> %s\n", __func__
);
507 /* slot already allocated? */
508 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
511 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
512 tbl
= &session
->fc_slot_table
;
514 spin_lock(&tbl
->slot_tbl_lock
);
515 if (test_bit(NFS4_SESSION_DRAINING
, &session
->session_state
) &&
516 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
518 * The state manager will wait until the slot table is empty.
519 * Schedule the reset thread
521 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
522 spin_unlock(&tbl
->slot_tbl_lock
);
523 dprintk("%s Schedule Session Reset\n", __func__
);
527 if (!rpc_queue_empty(&tbl
->slot_tbl_waitq
) &&
528 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
529 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
530 spin_unlock(&tbl
->slot_tbl_lock
);
531 dprintk("%s enforce FIFO order\n", __func__
);
535 slotid
= nfs4_find_slot(tbl
);
536 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
537 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
538 spin_unlock(&tbl
->slot_tbl_lock
);
539 dprintk("<-- %s: no free slots\n", __func__
);
542 spin_unlock(&tbl
->slot_tbl_lock
);
544 rpc_task_set_priority(task
, RPC_PRIORITY_NORMAL
);
545 slot
= tbl
->slots
+ slotid
;
546 args
->sa_session
= session
;
547 args
->sa_slotid
= slotid
;
548 args
->sa_cache_this
= cache_reply
;
550 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
552 res
->sr_session
= session
;
553 res
->sr_slotid
= slotid
;
554 res
->sr_renewal_time
= jiffies
;
555 res
->sr_status_flags
= 0;
557 * sr_status is only set in decode_sequence, and so will remain
558 * set to 1 if an rpc level failure occurs.
564 int nfs4_setup_sequence(const struct nfs_server
*server
,
565 struct nfs4_sequence_args
*args
,
566 struct nfs4_sequence_res
*res
,
568 struct rpc_task
*task
)
570 struct nfs4_session
*session
= nfs4_get_session(server
);
573 if (session
== NULL
) {
574 args
->sa_session
= NULL
;
575 res
->sr_session
= NULL
;
579 dprintk("--> %s clp %p session %p sr_slotid %d\n",
580 __func__
, session
->clp
, session
, res
->sr_slotid
);
582 ret
= nfs41_setup_sequence(session
, args
, res
, cache_reply
,
585 dprintk("<-- %s status=%d\n", __func__
, ret
);
589 struct nfs41_call_sync_data
{
590 const struct nfs_server
*seq_server
;
591 struct nfs4_sequence_args
*seq_args
;
592 struct nfs4_sequence_res
*seq_res
;
596 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
598 struct nfs41_call_sync_data
*data
= calldata
;
600 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
602 if (nfs4_setup_sequence(data
->seq_server
, data
->seq_args
,
603 data
->seq_res
, data
->cache_reply
, task
))
605 rpc_call_start(task
);
608 static void nfs41_call_priv_sync_prepare(struct rpc_task
*task
, void *calldata
)
610 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
611 nfs41_call_sync_prepare(task
, calldata
);
614 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
616 struct nfs41_call_sync_data
*data
= calldata
;
618 nfs41_sequence_done(task
, data
->seq_res
);
621 struct rpc_call_ops nfs41_call_sync_ops
= {
622 .rpc_call_prepare
= nfs41_call_sync_prepare
,
623 .rpc_call_done
= nfs41_call_sync_done
,
626 struct rpc_call_ops nfs41_call_priv_sync_ops
= {
627 .rpc_call_prepare
= nfs41_call_priv_sync_prepare
,
628 .rpc_call_done
= nfs41_call_sync_done
,
631 static int nfs4_call_sync_sequence(struct nfs_server
*server
,
632 struct rpc_message
*msg
,
633 struct nfs4_sequence_args
*args
,
634 struct nfs4_sequence_res
*res
,
639 struct rpc_task
*task
;
640 struct nfs41_call_sync_data data
= {
641 .seq_server
= server
,
644 .cache_reply
= cache_reply
,
646 struct rpc_task_setup task_setup
= {
647 .rpc_client
= server
->client
,
649 .callback_ops
= &nfs41_call_sync_ops
,
650 .callback_data
= &data
653 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
655 task_setup
.callback_ops
= &nfs41_call_priv_sync_ops
;
656 task
= rpc_run_task(&task_setup
);
660 ret
= task
->tk_status
;
666 int _nfs4_call_sync_session(struct nfs_server
*server
,
667 struct rpc_message
*msg
,
668 struct nfs4_sequence_args
*args
,
669 struct nfs4_sequence_res
*res
,
672 return nfs4_call_sync_sequence(server
, msg
, args
, res
, cache_reply
, 0);
676 static int nfs4_sequence_done(struct rpc_task
*task
,
677 struct nfs4_sequence_res
*res
)
681 #endif /* CONFIG_NFS_V4_1 */
683 int _nfs4_call_sync(struct nfs_server
*server
,
684 struct rpc_message
*msg
,
685 struct nfs4_sequence_args
*args
,
686 struct nfs4_sequence_res
*res
,
689 args
->sa_session
= res
->sr_session
= NULL
;
690 return rpc_call_sync(server
->client
, msg
, 0);
693 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
694 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
695 &(res)->seq_res, (cache_reply))
697 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
699 struct nfs_inode
*nfsi
= NFS_I(dir
);
701 spin_lock(&dir
->i_lock
);
702 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
703 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
704 nfs_force_lookup_revalidate(dir
);
705 nfsi
->change_attr
= cinfo
->after
;
706 spin_unlock(&dir
->i_lock
);
709 struct nfs4_opendata
{
711 struct nfs_openargs o_arg
;
712 struct nfs_openres o_res
;
713 struct nfs_open_confirmargs c_arg
;
714 struct nfs_open_confirmres c_res
;
715 struct nfs_fattr f_attr
;
716 struct nfs_fattr dir_attr
;
719 struct nfs4_state_owner
*owner
;
720 struct nfs4_state
*state
;
722 unsigned long timestamp
;
723 unsigned int rpc_done
: 1;
729 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
731 p
->o_res
.f_attr
= &p
->f_attr
;
732 p
->o_res
.dir_attr
= &p
->dir_attr
;
733 p
->o_res
.seqid
= p
->o_arg
.seqid
;
734 p
->c_res
.seqid
= p
->c_arg
.seqid
;
735 p
->o_res
.server
= p
->o_arg
.server
;
736 nfs_fattr_init(&p
->f_attr
);
737 nfs_fattr_init(&p
->dir_attr
);
738 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
741 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
742 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
743 const struct iattr
*attrs
,
746 struct dentry
*parent
= dget_parent(path
->dentry
);
747 struct inode
*dir
= parent
->d_inode
;
748 struct nfs_server
*server
= NFS_SERVER(dir
);
749 struct nfs4_opendata
*p
;
751 p
= kzalloc(sizeof(*p
), gfp_mask
);
754 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
755 if (p
->o_arg
.seqid
== NULL
)
761 atomic_inc(&sp
->so_count
);
762 p
->o_arg
.fh
= NFS_FH(dir
);
763 p
->o_arg
.open_flags
= flags
;
764 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
765 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
766 p
->o_arg
.id
= sp
->so_owner_id
.id
;
767 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
768 p
->o_arg
.server
= server
;
769 p
->o_arg
.bitmask
= server
->attr_bitmask
;
770 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
771 if (flags
& O_CREAT
) {
774 p
->o_arg
.u
.attrs
= &p
->attrs
;
775 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
776 s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
780 p
->c_arg
.fh
= &p
->o_res
.fh
;
781 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
782 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
783 nfs4_init_opendata_res(p
);
793 static void nfs4_opendata_free(struct kref
*kref
)
795 struct nfs4_opendata
*p
= container_of(kref
,
796 struct nfs4_opendata
, kref
);
798 nfs_free_seqid(p
->o_arg
.seqid
);
799 if (p
->state
!= NULL
)
800 nfs4_put_open_state(p
->state
);
801 nfs4_put_state_owner(p
->owner
);
807 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
810 kref_put(&p
->kref
, nfs4_opendata_free
);
813 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
817 ret
= rpc_wait_for_completion_task(task
);
821 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
825 if (open_mode
& O_EXCL
)
827 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
829 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
830 && state
->n_rdonly
!= 0;
833 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
834 && state
->n_wronly
!= 0;
836 case FMODE_READ
|FMODE_WRITE
:
837 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
838 && state
->n_rdwr
!= 0;
844 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
846 if ((delegation
->type
& fmode
) != fmode
)
848 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
850 nfs_mark_delegation_referenced(delegation
);
854 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
863 case FMODE_READ
|FMODE_WRITE
:
866 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
869 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
871 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
872 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
873 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
876 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
879 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
881 case FMODE_READ
|FMODE_WRITE
:
882 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
886 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
888 write_seqlock(&state
->seqlock
);
889 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
890 write_sequnlock(&state
->seqlock
);
893 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
896 * Protect the call to nfs4_state_set_mode_locked and
897 * serialise the stateid update
899 write_seqlock(&state
->seqlock
);
900 if (deleg_stateid
!= NULL
) {
901 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
902 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
904 if (open_stateid
!= NULL
)
905 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
906 write_sequnlock(&state
->seqlock
);
907 spin_lock(&state
->owner
->so_lock
);
908 update_open_stateflags(state
, fmode
);
909 spin_unlock(&state
->owner
->so_lock
);
912 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
914 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
915 struct nfs_delegation
*deleg_cur
;
918 fmode
&= (FMODE_READ
|FMODE_WRITE
);
921 deleg_cur
= rcu_dereference(nfsi
->delegation
);
922 if (deleg_cur
== NULL
)
925 spin_lock(&deleg_cur
->lock
);
926 if (nfsi
->delegation
!= deleg_cur
||
927 (deleg_cur
->type
& fmode
) != fmode
)
928 goto no_delegation_unlock
;
930 if (delegation
== NULL
)
931 delegation
= &deleg_cur
->stateid
;
932 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
933 goto no_delegation_unlock
;
935 nfs_mark_delegation_referenced(deleg_cur
);
936 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
938 no_delegation_unlock
:
939 spin_unlock(&deleg_cur
->lock
);
943 if (!ret
&& open_stateid
!= NULL
) {
944 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
952 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
954 struct nfs_delegation
*delegation
;
957 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
958 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
963 nfs_inode_return_delegation(inode
);
966 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
968 struct nfs4_state
*state
= opendata
->state
;
969 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
970 struct nfs_delegation
*delegation
;
971 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
972 fmode_t fmode
= opendata
->o_arg
.fmode
;
973 nfs4_stateid stateid
;
977 if (can_open_cached(state
, fmode
, open_mode
)) {
978 spin_lock(&state
->owner
->so_lock
);
979 if (can_open_cached(state
, fmode
, open_mode
)) {
980 update_open_stateflags(state
, fmode
);
981 spin_unlock(&state
->owner
->so_lock
);
982 goto out_return_state
;
984 spin_unlock(&state
->owner
->so_lock
);
987 delegation
= rcu_dereference(nfsi
->delegation
);
988 if (delegation
== NULL
||
989 !can_open_delegated(delegation
, fmode
)) {
993 /* Save the delegation */
994 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
996 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1001 /* Try to update the stateid using the delegation */
1002 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1003 goto out_return_state
;
1006 return ERR_PTR(ret
);
1008 atomic_inc(&state
->count
);
1012 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1014 struct inode
*inode
;
1015 struct nfs4_state
*state
= NULL
;
1016 struct nfs_delegation
*delegation
;
1019 if (!data
->rpc_done
) {
1020 state
= nfs4_try_open_cached(data
);
1025 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1027 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
1028 ret
= PTR_ERR(inode
);
1032 state
= nfs4_get_open_state(inode
, data
->owner
);
1035 if (data
->o_res
.delegation_type
!= 0) {
1036 int delegation_flags
= 0;
1039 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1041 delegation_flags
= delegation
->flags
;
1043 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1044 nfs_inode_set_delegation(state
->inode
,
1045 data
->owner
->so_cred
,
1048 nfs_inode_reclaim_delegation(state
->inode
,
1049 data
->owner
->so_cred
,
1053 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1061 return ERR_PTR(ret
);
1064 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1066 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1067 struct nfs_open_context
*ctx
;
1069 spin_lock(&state
->inode
->i_lock
);
1070 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1071 if (ctx
->state
!= state
)
1073 get_nfs_open_context(ctx
);
1074 spin_unlock(&state
->inode
->i_lock
);
1077 spin_unlock(&state
->inode
->i_lock
);
1078 return ERR_PTR(-ENOENT
);
1081 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1083 struct nfs4_opendata
*opendata
;
1085 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
, GFP_NOFS
);
1086 if (opendata
== NULL
)
1087 return ERR_PTR(-ENOMEM
);
1088 opendata
->state
= state
;
1089 atomic_inc(&state
->count
);
1093 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1095 struct nfs4_state
*newstate
;
1098 opendata
->o_arg
.open_flags
= 0;
1099 opendata
->o_arg
.fmode
= fmode
;
1100 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1101 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1102 nfs4_init_opendata_res(opendata
);
1103 ret
= _nfs4_recover_proc_open(opendata
);
1106 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1107 if (IS_ERR(newstate
))
1108 return PTR_ERR(newstate
);
1109 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1114 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1116 struct nfs4_state
*newstate
;
1119 /* memory barrier prior to reading state->n_* */
1120 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1122 if (state
->n_rdwr
!= 0) {
1123 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1126 if (newstate
!= state
)
1129 if (state
->n_wronly
!= 0) {
1130 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1133 if (newstate
!= state
)
1136 if (state
->n_rdonly
!= 0) {
1137 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1140 if (newstate
!= state
)
1144 * We may have performed cached opens for all three recoveries.
1145 * Check if we need to update the current stateid.
1147 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1148 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1149 write_seqlock(&state
->seqlock
);
1150 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1151 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1152 write_sequnlock(&state
->seqlock
);
1159 * reclaim state on the server after a reboot.
1161 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1163 struct nfs_delegation
*delegation
;
1164 struct nfs4_opendata
*opendata
;
1165 fmode_t delegation_type
= 0;
1168 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1169 if (IS_ERR(opendata
))
1170 return PTR_ERR(opendata
);
1171 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1172 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1174 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1175 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1176 delegation_type
= delegation
->type
;
1178 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1179 status
= nfs4_open_recover(opendata
, state
);
1180 nfs4_opendata_put(opendata
);
1184 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1186 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1187 struct nfs4_exception exception
= { };
1190 err
= _nfs4_do_open_reclaim(ctx
, state
);
1191 if (err
!= -NFS4ERR_DELAY
&& err
!= -EKEYEXPIRED
)
1193 nfs4_handle_exception(server
, err
, &exception
);
1194 } while (exception
.retry
);
1198 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1200 struct nfs_open_context
*ctx
;
1203 ctx
= nfs4_state_find_open_context(state
);
1205 return PTR_ERR(ctx
);
1206 ret
= nfs4_do_open_reclaim(ctx
, state
);
1207 put_nfs_open_context(ctx
);
1211 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1213 struct nfs4_opendata
*opendata
;
1216 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1217 if (IS_ERR(opendata
))
1218 return PTR_ERR(opendata
);
1219 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1220 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1221 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1222 ret
= nfs4_open_recover(opendata
, state
);
1223 nfs4_opendata_put(opendata
);
1227 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1229 struct nfs4_exception exception
= { };
1230 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1233 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1239 case -NFS4ERR_BADSESSION
:
1240 case -NFS4ERR_BADSLOT
:
1241 case -NFS4ERR_BAD_HIGH_SLOT
:
1242 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1243 case -NFS4ERR_DEADSESSION
:
1244 nfs4_schedule_state_recovery(
1245 server
->nfs_client
);
1247 case -NFS4ERR_STALE_CLIENTID
:
1248 case -NFS4ERR_STALE_STATEID
:
1249 case -NFS4ERR_EXPIRED
:
1250 /* Don't recall a delegation if it was lost */
1251 nfs4_schedule_state_recovery(server
->nfs_client
);
1255 * The show must go on: exit, but mark the
1256 * stateid as needing recovery.
1258 case -NFS4ERR_ADMIN_REVOKED
:
1259 case -NFS4ERR_BAD_STATEID
:
1260 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
1265 err
= nfs4_handle_exception(server
, err
, &exception
);
1266 } while (exception
.retry
);
1271 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1273 struct nfs4_opendata
*data
= calldata
;
1275 data
->rpc_status
= task
->tk_status
;
1276 if (data
->rpc_status
== 0) {
1277 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1278 sizeof(data
->o_res
.stateid
.data
));
1279 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1280 renew_lease(data
->o_res
.server
, data
->timestamp
);
1285 static void nfs4_open_confirm_release(void *calldata
)
1287 struct nfs4_opendata
*data
= calldata
;
1288 struct nfs4_state
*state
= NULL
;
1290 /* If this request hasn't been cancelled, do nothing */
1291 if (data
->cancelled
== 0)
1293 /* In case of error, no cleanup! */
1294 if (!data
->rpc_done
)
1296 state
= nfs4_opendata_to_nfs4_state(data
);
1298 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1300 nfs4_opendata_put(data
);
1303 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1304 .rpc_call_done
= nfs4_open_confirm_done
,
1305 .rpc_release
= nfs4_open_confirm_release
,
1309 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1311 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1313 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1314 struct rpc_task
*task
;
1315 struct rpc_message msg
= {
1316 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1317 .rpc_argp
= &data
->c_arg
,
1318 .rpc_resp
= &data
->c_res
,
1319 .rpc_cred
= data
->owner
->so_cred
,
1321 struct rpc_task_setup task_setup_data
= {
1322 .rpc_client
= server
->client
,
1323 .rpc_message
= &msg
,
1324 .callback_ops
= &nfs4_open_confirm_ops
,
1325 .callback_data
= data
,
1326 .workqueue
= nfsiod_workqueue
,
1327 .flags
= RPC_TASK_ASYNC
,
1331 kref_get(&data
->kref
);
1333 data
->rpc_status
= 0;
1334 data
->timestamp
= jiffies
;
1335 task
= rpc_run_task(&task_setup_data
);
1337 return PTR_ERR(task
);
1338 status
= nfs4_wait_for_completion_rpc_task(task
);
1340 data
->cancelled
= 1;
1343 status
= data
->rpc_status
;
1348 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1350 struct nfs4_opendata
*data
= calldata
;
1351 struct nfs4_state_owner
*sp
= data
->owner
;
1353 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1356 * Check if we still need to send an OPEN call, or if we can use
1357 * a delegation instead.
1359 if (data
->state
!= NULL
) {
1360 struct nfs_delegation
*delegation
;
1362 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1365 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1366 if (delegation
!= NULL
&&
1367 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1373 /* Update sequence id. */
1374 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1375 data
->o_arg
.clientid
= sp
->so_server
->nfs_client
->cl_clientid
;
1376 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1377 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1378 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1380 data
->timestamp
= jiffies
;
1381 if (nfs4_setup_sequence(data
->o_arg
.server
,
1382 &data
->o_arg
.seq_args
,
1383 &data
->o_res
.seq_res
, 1, task
))
1385 rpc_call_start(task
);
1388 task
->tk_action
= NULL
;
1392 static void nfs4_recover_open_prepare(struct rpc_task
*task
, void *calldata
)
1394 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
1395 nfs4_open_prepare(task
, calldata
);
1398 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1400 struct nfs4_opendata
*data
= calldata
;
1402 data
->rpc_status
= task
->tk_status
;
1404 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1407 if (task
->tk_status
== 0) {
1408 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1412 data
->rpc_status
= -ELOOP
;
1415 data
->rpc_status
= -EISDIR
;
1418 data
->rpc_status
= -ENOTDIR
;
1420 renew_lease(data
->o_res
.server
, data
->timestamp
);
1421 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1422 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1427 static void nfs4_open_release(void *calldata
)
1429 struct nfs4_opendata
*data
= calldata
;
1430 struct nfs4_state
*state
= NULL
;
1432 /* If this request hasn't been cancelled, do nothing */
1433 if (data
->cancelled
== 0)
1435 /* In case of error, no cleanup! */
1436 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1438 /* In case we need an open_confirm, no cleanup! */
1439 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1441 state
= nfs4_opendata_to_nfs4_state(data
);
1443 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1445 nfs4_opendata_put(data
);
1448 static const struct rpc_call_ops nfs4_open_ops
= {
1449 .rpc_call_prepare
= nfs4_open_prepare
,
1450 .rpc_call_done
= nfs4_open_done
,
1451 .rpc_release
= nfs4_open_release
,
1454 static const struct rpc_call_ops nfs4_recover_open_ops
= {
1455 .rpc_call_prepare
= nfs4_recover_open_prepare
,
1456 .rpc_call_done
= nfs4_open_done
,
1457 .rpc_release
= nfs4_open_release
,
1460 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1462 struct inode
*dir
= data
->dir
->d_inode
;
1463 struct nfs_server
*server
= NFS_SERVER(dir
);
1464 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1465 struct nfs_openres
*o_res
= &data
->o_res
;
1466 struct rpc_task
*task
;
1467 struct rpc_message msg
= {
1468 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1471 .rpc_cred
= data
->owner
->so_cred
,
1473 struct rpc_task_setup task_setup_data
= {
1474 .rpc_client
= server
->client
,
1475 .rpc_message
= &msg
,
1476 .callback_ops
= &nfs4_open_ops
,
1477 .callback_data
= data
,
1478 .workqueue
= nfsiod_workqueue
,
1479 .flags
= RPC_TASK_ASYNC
,
1483 kref_get(&data
->kref
);
1485 data
->rpc_status
= 0;
1486 data
->cancelled
= 0;
1488 task_setup_data
.callback_ops
= &nfs4_recover_open_ops
;
1489 task
= rpc_run_task(&task_setup_data
);
1491 return PTR_ERR(task
);
1492 status
= nfs4_wait_for_completion_rpc_task(task
);
1494 data
->cancelled
= 1;
1497 status
= data
->rpc_status
;
1503 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1505 struct inode
*dir
= data
->dir
->d_inode
;
1506 struct nfs_openres
*o_res
= &data
->o_res
;
1509 status
= nfs4_run_open_task(data
, 1);
1510 if (status
!= 0 || !data
->rpc_done
)
1513 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1515 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1516 status
= _nfs4_proc_open_confirm(data
);
1525 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1527 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1529 struct inode
*dir
= data
->dir
->d_inode
;
1530 struct nfs_server
*server
= NFS_SERVER(dir
);
1531 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1532 struct nfs_openres
*o_res
= &data
->o_res
;
1535 status
= nfs4_run_open_task(data
, 0);
1536 if (status
!= 0 || !data
->rpc_done
)
1539 if (o_arg
->open_flags
& O_CREAT
) {
1540 update_changeattr(dir
, &o_res
->cinfo
);
1541 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1543 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1544 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1545 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1546 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1547 status
= _nfs4_proc_open_confirm(data
);
1551 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1552 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1556 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1558 struct nfs_client
*clp
= server
->nfs_client
;
1562 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1563 ret
= nfs4_wait_clnt_recover(clp
);
1566 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1567 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1569 nfs4_schedule_state_recovery(clp
);
1577 * reclaim state on the server after a network partition.
1578 * Assumes caller holds the appropriate lock
1580 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1582 struct nfs4_opendata
*opendata
;
1585 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1586 if (IS_ERR(opendata
))
1587 return PTR_ERR(opendata
);
1588 ret
= nfs4_open_recover(opendata
, state
);
1590 d_drop(ctx
->path
.dentry
);
1591 nfs4_opendata_put(opendata
);
1595 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1597 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1598 struct nfs4_exception exception
= { };
1602 err
= _nfs4_open_expired(ctx
, state
);
1606 case -NFS4ERR_GRACE
:
1607 case -NFS4ERR_DELAY
:
1609 nfs4_handle_exception(server
, err
, &exception
);
1612 } while (exception
.retry
);
1617 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1619 struct nfs_open_context
*ctx
;
1622 ctx
= nfs4_state_find_open_context(state
);
1624 return PTR_ERR(ctx
);
1625 ret
= nfs4_do_open_expired(ctx
, state
);
1626 put_nfs_open_context(ctx
);
1631 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1632 * fields corresponding to attributes that were used to store the verifier.
1633 * Make sure we clobber those fields in the later setattr call
1635 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1637 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1638 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1639 sattr
->ia_valid
|= ATTR_ATIME
;
1641 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1642 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1643 sattr
->ia_valid
|= ATTR_MTIME
;
1647 * Returns a referenced nfs4_state
1649 static int _nfs4_do_open(struct inode
*dir
, struct path
*path
, fmode_t fmode
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
, struct nfs4_state
**res
)
1651 struct nfs4_state_owner
*sp
;
1652 struct nfs4_state
*state
= NULL
;
1653 struct nfs_server
*server
= NFS_SERVER(dir
);
1654 struct nfs4_opendata
*opendata
;
1657 /* Protect against reboot recovery conflicts */
1659 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1660 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1663 status
= nfs4_recover_expired_lease(server
);
1665 goto err_put_state_owner
;
1666 if (path
->dentry
->d_inode
!= NULL
)
1667 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1669 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
, GFP_KERNEL
);
1670 if (opendata
== NULL
)
1671 goto err_put_state_owner
;
1673 if (path
->dentry
->d_inode
!= NULL
)
1674 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1676 status
= _nfs4_proc_open(opendata
);
1678 goto err_opendata_put
;
1680 state
= nfs4_opendata_to_nfs4_state(opendata
);
1681 status
= PTR_ERR(state
);
1683 goto err_opendata_put
;
1684 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
1685 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1687 if (opendata
->o_arg
.open_flags
& O_EXCL
) {
1688 nfs4_exclusive_attrset(opendata
, sattr
);
1690 nfs_fattr_init(opendata
->o_res
.f_attr
);
1691 status
= nfs4_do_setattr(state
->inode
, cred
,
1692 opendata
->o_res
.f_attr
, sattr
,
1695 nfs_setattr_update_inode(state
->inode
, sattr
);
1696 nfs_post_op_update_inode(state
->inode
, opendata
->o_res
.f_attr
);
1698 nfs4_opendata_put(opendata
);
1699 nfs4_put_state_owner(sp
);
1703 nfs4_opendata_put(opendata
);
1704 err_put_state_owner
:
1705 nfs4_put_state_owner(sp
);
1712 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
, struct path
*path
, fmode_t fmode
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
)
1714 struct nfs4_exception exception
= { };
1715 struct nfs4_state
*res
;
1719 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1722 /* NOTE: BAD_SEQID means the server and client disagree about the
1723 * book-keeping w.r.t. state-changing operations
1724 * (OPEN/CLOSE/LOCK/LOCKU...)
1725 * It is actually a sign of a bug on the client or on the server.
1727 * If we receive a BAD_SEQID error in the particular case of
1728 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1729 * have unhashed the old state_owner for us, and that we can
1730 * therefore safely retry using a new one. We should still warn
1731 * the user though...
1733 if (status
== -NFS4ERR_BAD_SEQID
) {
1734 printk(KERN_WARNING
"NFS: v4 server %s "
1735 " returned a bad sequence-id error!\n",
1736 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1737 exception
.retry
= 1;
1741 * BAD_STATEID on OPEN means that the server cancelled our
1742 * state before it received the OPEN_CONFIRM.
1743 * Recover by retrying the request as per the discussion
1744 * on Page 181 of RFC3530.
1746 if (status
== -NFS4ERR_BAD_STATEID
) {
1747 exception
.retry
= 1;
1750 if (status
== -EAGAIN
) {
1751 /* We must have found a delegation */
1752 exception
.retry
= 1;
1755 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1756 status
, &exception
));
1757 } while (exception
.retry
);
1761 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1762 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1763 struct nfs4_state
*state
)
1765 struct nfs_server
*server
= NFS_SERVER(inode
);
1766 struct nfs_setattrargs arg
= {
1767 .fh
= NFS_FH(inode
),
1770 .bitmask
= server
->attr_bitmask
,
1772 struct nfs_setattrres res
= {
1776 struct rpc_message msg
= {
1777 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1782 unsigned long timestamp
= jiffies
;
1785 nfs_fattr_init(fattr
);
1787 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1788 /* Use that stateid */
1789 } else if (state
!= NULL
) {
1790 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
, current
->tgid
);
1792 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1794 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1795 if (status
== 0 && state
!= NULL
)
1796 renew_lease(server
, timestamp
);
1800 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1801 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1802 struct nfs4_state
*state
)
1804 struct nfs_server
*server
= NFS_SERVER(inode
);
1805 struct nfs4_exception exception
= { };
1808 err
= nfs4_handle_exception(server
,
1809 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1811 } while (exception
.retry
);
1815 struct nfs4_closedata
{
1817 struct inode
*inode
;
1818 struct nfs4_state
*state
;
1819 struct nfs_closeargs arg
;
1820 struct nfs_closeres res
;
1821 struct nfs_fattr fattr
;
1822 unsigned long timestamp
;
1825 static void nfs4_free_closedata(void *data
)
1827 struct nfs4_closedata
*calldata
= data
;
1828 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1830 nfs4_put_open_state(calldata
->state
);
1831 nfs_free_seqid(calldata
->arg
.seqid
);
1832 nfs4_put_state_owner(sp
);
1833 path_put(&calldata
->path
);
1837 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
1840 spin_lock(&state
->owner
->so_lock
);
1841 if (!(fmode
& FMODE_READ
))
1842 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1843 if (!(fmode
& FMODE_WRITE
))
1844 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1845 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1846 spin_unlock(&state
->owner
->so_lock
);
1849 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1851 struct nfs4_closedata
*calldata
= data
;
1852 struct nfs4_state
*state
= calldata
->state
;
1853 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1855 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
1857 /* hmm. we are done with the inode, and in the process of freeing
1858 * the state_owner. we keep this around to process errors
1860 switch (task
->tk_status
) {
1862 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1863 renew_lease(server
, calldata
->timestamp
);
1864 nfs4_close_clear_stateid_flags(state
,
1865 calldata
->arg
.fmode
);
1867 case -NFS4ERR_STALE_STATEID
:
1868 case -NFS4ERR_OLD_STATEID
:
1869 case -NFS4ERR_BAD_STATEID
:
1870 case -NFS4ERR_EXPIRED
:
1871 if (calldata
->arg
.fmode
== 0)
1874 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
1875 rpc_restart_call_prepare(task
);
1877 nfs_release_seqid(calldata
->arg
.seqid
);
1878 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1881 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1883 struct nfs4_closedata
*calldata
= data
;
1884 struct nfs4_state
*state
= calldata
->state
;
1887 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1890 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1891 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
1892 spin_lock(&state
->owner
->so_lock
);
1893 /* Calculate the change in open mode */
1894 if (state
->n_rdwr
== 0) {
1895 if (state
->n_rdonly
== 0) {
1896 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1897 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1898 calldata
->arg
.fmode
&= ~FMODE_READ
;
1900 if (state
->n_wronly
== 0) {
1901 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1902 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1903 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
1906 spin_unlock(&state
->owner
->so_lock
);
1909 /* Note: exit _without_ calling nfs4_close_done */
1910 task
->tk_action
= NULL
;
1914 if (calldata
->arg
.fmode
== 0)
1915 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
1917 nfs_fattr_init(calldata
->res
.fattr
);
1918 calldata
->timestamp
= jiffies
;
1919 if (nfs4_setup_sequence(NFS_SERVER(calldata
->inode
),
1920 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1923 rpc_call_start(task
);
1926 static const struct rpc_call_ops nfs4_close_ops
= {
1927 .rpc_call_prepare
= nfs4_close_prepare
,
1928 .rpc_call_done
= nfs4_close_done
,
1929 .rpc_release
= nfs4_free_closedata
,
1933 * It is possible for data to be read/written from a mem-mapped file
1934 * after the sys_close call (which hits the vfs layer as a flush).
1935 * This means that we can't safely call nfsv4 close on a file until
1936 * the inode is cleared. This in turn means that we are not good
1937 * NFSv4 citizens - we do not indicate to the server to update the file's
1938 * share state even when we are done with one of the three share
1939 * stateid's in the inode.
1941 * NOTE: Caller must be holding the sp->so_owner semaphore!
1943 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
1945 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1946 struct nfs4_closedata
*calldata
;
1947 struct nfs4_state_owner
*sp
= state
->owner
;
1948 struct rpc_task
*task
;
1949 struct rpc_message msg
= {
1950 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1951 .rpc_cred
= state
->owner
->so_cred
,
1953 struct rpc_task_setup task_setup_data
= {
1954 .rpc_client
= server
->client
,
1955 .rpc_message
= &msg
,
1956 .callback_ops
= &nfs4_close_ops
,
1957 .workqueue
= nfsiod_workqueue
,
1958 .flags
= RPC_TASK_ASYNC
,
1960 int status
= -ENOMEM
;
1962 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
1963 if (calldata
== NULL
)
1965 calldata
->inode
= state
->inode
;
1966 calldata
->state
= state
;
1967 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1968 calldata
->arg
.stateid
= &state
->open_stateid
;
1969 /* Serialization for the sequence id */
1970 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
1971 if (calldata
->arg
.seqid
== NULL
)
1972 goto out_free_calldata
;
1973 calldata
->arg
.fmode
= 0;
1974 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1975 calldata
->res
.fattr
= &calldata
->fattr
;
1976 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1977 calldata
->res
.server
= server
;
1978 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1980 calldata
->path
= *path
;
1982 msg
.rpc_argp
= &calldata
->arg
,
1983 msg
.rpc_resp
= &calldata
->res
,
1984 task_setup_data
.callback_data
= calldata
;
1985 task
= rpc_run_task(&task_setup_data
);
1987 return PTR_ERR(task
);
1990 status
= rpc_wait_for_completion_task(task
);
1996 nfs4_put_open_state(state
);
1997 nfs4_put_state_owner(sp
);
2001 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
2006 /* If the open_intent is for execute, we have an extra check to make */
2007 if (fmode
& FMODE_EXEC
) {
2008 ret
= nfs_may_open(state
->inode
,
2009 state
->owner
->so_cred
,
2010 nd
->intent
.open
.flags
);
2014 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
2015 if (!IS_ERR(filp
)) {
2016 struct nfs_open_context
*ctx
;
2017 ctx
= nfs_file_open_context(filp
);
2021 ret
= PTR_ERR(filp
);
2023 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
2028 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
, int open_flags
, struct iattr
*attr
)
2030 struct nfs4_state
*state
;
2032 /* Protect against concurrent sillydeletes */
2033 state
= nfs4_do_open(dir
, &ctx
->path
, ctx
->mode
, open_flags
, attr
, ctx
->cred
);
2035 return ERR_CAST(state
);
2037 return igrab(state
->inode
);
2041 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
2043 struct path path
= {
2044 .mnt
= nd
->path
.mnt
,
2047 struct rpc_cred
*cred
;
2048 struct nfs4_state
*state
;
2049 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
2051 cred
= rpc_lookup_cred();
2053 return PTR_ERR(cred
);
2054 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
2056 if (IS_ERR(state
)) {
2057 switch (PTR_ERR(state
)) {
2063 return PTR_ERR(state
);
2068 if (state
->inode
== dentry
->d_inode
) {
2069 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2070 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2073 nfs4_close_sync(&path
, state
, fmode
);
2079 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2081 if (ctx
->state
== NULL
)
2084 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2086 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2089 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2091 struct nfs4_server_caps_arg args
= {
2094 struct nfs4_server_caps_res res
= {};
2095 struct rpc_message msg
= {
2096 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2102 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2104 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2105 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2106 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2107 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2108 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2109 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2110 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2111 server
->caps
|= NFS_CAP_ACLS
;
2112 if (res
.has_links
!= 0)
2113 server
->caps
|= NFS_CAP_HARDLINKS
;
2114 if (res
.has_symlinks
!= 0)
2115 server
->caps
|= NFS_CAP_SYMLINKS
;
2116 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2117 server
->caps
|= NFS_CAP_FILEID
;
2118 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2119 server
->caps
|= NFS_CAP_MODE
;
2120 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2121 server
->caps
|= NFS_CAP_NLINK
;
2122 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2123 server
->caps
|= NFS_CAP_OWNER
;
2124 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2125 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2126 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2127 server
->caps
|= NFS_CAP_ATIME
;
2128 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2129 server
->caps
|= NFS_CAP_CTIME
;
2130 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2131 server
->caps
|= NFS_CAP_MTIME
;
2133 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2134 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2135 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2136 server
->acl_bitmask
= res
.acl_bitmask
;
2142 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2144 struct nfs4_exception exception
= { };
2147 err
= nfs4_handle_exception(server
,
2148 _nfs4_server_capabilities(server
, fhandle
),
2150 } while (exception
.retry
);
2154 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2155 struct nfs_fsinfo
*info
)
2157 struct nfs4_lookup_root_arg args
= {
2158 .bitmask
= nfs4_fattr_bitmap
,
2160 struct nfs4_lookup_res res
= {
2162 .fattr
= info
->fattr
,
2165 struct rpc_message msg
= {
2166 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2171 nfs_fattr_init(info
->fattr
);
2172 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2175 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2176 struct nfs_fsinfo
*info
)
2178 struct nfs4_exception exception
= { };
2181 err
= nfs4_handle_exception(server
,
2182 _nfs4_lookup_root(server
, fhandle
, info
),
2184 } while (exception
.retry
);
2189 * get the file handle for the "/" directory on the server
2191 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2192 struct nfs_fsinfo
*info
)
2196 status
= nfs4_lookup_root(server
, fhandle
, info
);
2198 status
= nfs4_server_capabilities(server
, fhandle
);
2200 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2201 return nfs4_map_errors(status
);
2205 * Get locations and (maybe) other attributes of a referral.
2206 * Note that we'll actually follow the referral later when
2207 * we detect fsid mismatch in inode revalidation
2209 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2211 int status
= -ENOMEM
;
2212 struct page
*page
= NULL
;
2213 struct nfs4_fs_locations
*locations
= NULL
;
2215 page
= alloc_page(GFP_KERNEL
);
2218 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2219 if (locations
== NULL
)
2222 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2225 /* Make sure server returned a different fsid for the referral */
2226 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2227 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2232 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2233 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2235 fattr
->mode
= S_IFDIR
;
2236 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2244 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2246 struct nfs4_getattr_arg args
= {
2248 .bitmask
= server
->attr_bitmask
,
2250 struct nfs4_getattr_res res
= {
2254 struct rpc_message msg
= {
2255 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2260 nfs_fattr_init(fattr
);
2261 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2264 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2266 struct nfs4_exception exception
= { };
2269 err
= nfs4_handle_exception(server
,
2270 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2272 } while (exception
.retry
);
2277 * The file is not closed if it is opened due to the a request to change
2278 * the size of the file. The open call will not be needed once the
2279 * VFS layer lookup-intents are implemented.
2281 * Close is called when the inode is destroyed.
2282 * If we haven't opened the file for O_WRONLY, we
2283 * need to in the size_change case to obtain a stateid.
2286 * Because OPEN is always done by name in nfsv4, it is
2287 * possible that we opened a different file by the same
2288 * name. We can recognize this race condition, but we
2289 * can't do anything about it besides returning an error.
2291 * This will be fixed with VFS changes (lookup-intent).
2294 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2295 struct iattr
*sattr
)
2297 struct inode
*inode
= dentry
->d_inode
;
2298 struct rpc_cred
*cred
= NULL
;
2299 struct nfs4_state
*state
= NULL
;
2302 nfs_fattr_init(fattr
);
2304 /* Search for an existing open(O_WRITE) file */
2305 if (sattr
->ia_valid
& ATTR_FILE
) {
2306 struct nfs_open_context
*ctx
;
2308 ctx
= nfs_file_open_context(sattr
->ia_file
);
2315 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2317 nfs_setattr_update_inode(inode
, sattr
);
2321 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2322 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2323 struct nfs_fattr
*fattr
)
2326 struct nfs4_lookup_arg args
= {
2327 .bitmask
= server
->attr_bitmask
,
2331 struct nfs4_lookup_res res
= {
2336 struct rpc_message msg
= {
2337 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2342 nfs_fattr_init(fattr
);
2344 dprintk("NFS call lookupfh %s\n", name
->name
);
2345 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2346 dprintk("NFS reply lookupfh: %d\n", status
);
2350 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2351 struct qstr
*name
, struct nfs_fh
*fhandle
,
2352 struct nfs_fattr
*fattr
)
2354 struct nfs4_exception exception
= { };
2357 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2359 if (err
== -NFS4ERR_MOVED
) {
2363 err
= nfs4_handle_exception(server
, err
, &exception
);
2364 } while (exception
.retry
);
2368 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2369 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2373 dprintk("NFS call lookup %s\n", name
->name
);
2374 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2375 if (status
== -NFS4ERR_MOVED
)
2376 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2377 dprintk("NFS reply lookup: %d\n", status
);
2381 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2383 struct nfs4_exception exception
= { };
2386 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2387 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2389 } while (exception
.retry
);
2393 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2395 struct nfs_server
*server
= NFS_SERVER(inode
);
2396 struct nfs4_accessargs args
= {
2397 .fh
= NFS_FH(inode
),
2398 .bitmask
= server
->attr_bitmask
,
2400 struct nfs4_accessres res
= {
2403 struct rpc_message msg
= {
2404 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2407 .rpc_cred
= entry
->cred
,
2409 int mode
= entry
->mask
;
2413 * Determine which access bits we want to ask for...
2415 if (mode
& MAY_READ
)
2416 args
.access
|= NFS4_ACCESS_READ
;
2417 if (S_ISDIR(inode
->i_mode
)) {
2418 if (mode
& MAY_WRITE
)
2419 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2420 if (mode
& MAY_EXEC
)
2421 args
.access
|= NFS4_ACCESS_LOOKUP
;
2423 if (mode
& MAY_WRITE
)
2424 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2425 if (mode
& MAY_EXEC
)
2426 args
.access
|= NFS4_ACCESS_EXECUTE
;
2429 res
.fattr
= nfs_alloc_fattr();
2430 if (res
.fattr
== NULL
)
2433 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2436 if (res
.access
& NFS4_ACCESS_READ
)
2437 entry
->mask
|= MAY_READ
;
2438 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2439 entry
->mask
|= MAY_WRITE
;
2440 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2441 entry
->mask
|= MAY_EXEC
;
2442 nfs_refresh_inode(inode
, res
.fattr
);
2444 nfs_free_fattr(res
.fattr
);
2448 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2450 struct nfs4_exception exception
= { };
2453 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2454 _nfs4_proc_access(inode
, entry
),
2456 } while (exception
.retry
);
2461 * TODO: For the time being, we don't try to get any attributes
2462 * along with any of the zero-copy operations READ, READDIR,
2465 * In the case of the first three, we want to put the GETATTR
2466 * after the read-type operation -- this is because it is hard
2467 * to predict the length of a GETATTR response in v4, and thus
2468 * align the READ data correctly. This means that the GETATTR
2469 * may end up partially falling into the page cache, and we should
2470 * shift it into the 'tail' of the xdr_buf before processing.
2471 * To do this efficiently, we need to know the total length
2472 * of data received, which doesn't seem to be available outside
2475 * In the case of WRITE, we also want to put the GETATTR after
2476 * the operation -- in this case because we want to make sure
2477 * we get the post-operation mtime and size. This means that
2478 * we can't use xdr_encode_pages() as written: we need a variant
2479 * of it which would leave room in the 'tail' iovec.
2481 * Both of these changes to the XDR layer would in fact be quite
2482 * minor, but I decided to leave them for a subsequent patch.
2484 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2485 unsigned int pgbase
, unsigned int pglen
)
2487 struct nfs4_readlink args
= {
2488 .fh
= NFS_FH(inode
),
2493 struct nfs4_readlink_res res
;
2494 struct rpc_message msg
= {
2495 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2500 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2503 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2504 unsigned int pgbase
, unsigned int pglen
)
2506 struct nfs4_exception exception
= { };
2509 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2510 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2512 } while (exception
.retry
);
2518 * We will need to arrange for the VFS layer to provide an atomic open.
2519 * Until then, this create/open method is prone to inefficiency and race
2520 * conditions due to the lookup, create, and open VFS calls from sys_open()
2521 * placed on the wire.
2523 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2524 * The file will be opened again in the subsequent VFS open call
2525 * (nfs4_proc_file_open).
2527 * The open for read will just hang around to be used by any process that
2528 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2532 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2533 int flags
, struct nameidata
*nd
)
2535 struct path path
= {
2536 .mnt
= nd
->path
.mnt
,
2539 struct nfs4_state
*state
;
2540 struct rpc_cred
*cred
;
2541 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2544 cred
= rpc_lookup_cred();
2546 status
= PTR_ERR(cred
);
2549 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2551 if (IS_ERR(state
)) {
2552 status
= PTR_ERR(state
);
2555 d_add(dentry
, igrab(state
->inode
));
2556 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2557 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2558 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2560 nfs4_close_sync(&path
, state
, fmode
);
2567 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2569 struct nfs_server
*server
= NFS_SERVER(dir
);
2570 struct nfs_removeargs args
= {
2572 .name
.len
= name
->len
,
2573 .name
.name
= name
->name
,
2574 .bitmask
= server
->attr_bitmask
,
2576 struct nfs_removeres res
= {
2579 struct rpc_message msg
= {
2580 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2584 int status
= -ENOMEM
;
2586 res
.dir_attr
= nfs_alloc_fattr();
2587 if (res
.dir_attr
== NULL
)
2590 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2592 update_changeattr(dir
, &res
.cinfo
);
2593 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2595 nfs_free_fattr(res
.dir_attr
);
2600 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2602 struct nfs4_exception exception
= { };
2605 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2606 _nfs4_proc_remove(dir
, name
),
2608 } while (exception
.retry
);
2612 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2614 struct nfs_server
*server
= NFS_SERVER(dir
);
2615 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2616 struct nfs_removeres
*res
= msg
->rpc_resp
;
2618 args
->bitmask
= server
->cache_consistency_bitmask
;
2619 res
->server
= server
;
2620 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2623 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2625 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2627 if (!nfs4_sequence_done(task
, &res
->seq_res
))
2629 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2631 update_changeattr(dir
, &res
->cinfo
);
2632 nfs_post_op_update_inode(dir
, res
->dir_attr
);
2636 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2637 struct inode
*new_dir
, struct qstr
*new_name
)
2639 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2640 struct nfs4_rename_arg arg
= {
2641 .old_dir
= NFS_FH(old_dir
),
2642 .new_dir
= NFS_FH(new_dir
),
2643 .old_name
= old_name
,
2644 .new_name
= new_name
,
2645 .bitmask
= server
->attr_bitmask
,
2647 struct nfs4_rename_res res
= {
2650 struct rpc_message msg
= {
2651 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2655 int status
= -ENOMEM
;
2657 res
.old_fattr
= nfs_alloc_fattr();
2658 res
.new_fattr
= nfs_alloc_fattr();
2659 if (res
.old_fattr
== NULL
|| res
.new_fattr
== NULL
)
2662 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2664 update_changeattr(old_dir
, &res
.old_cinfo
);
2665 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2666 update_changeattr(new_dir
, &res
.new_cinfo
);
2667 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2670 nfs_free_fattr(res
.new_fattr
);
2671 nfs_free_fattr(res
.old_fattr
);
2675 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2676 struct inode
*new_dir
, struct qstr
*new_name
)
2678 struct nfs4_exception exception
= { };
2681 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2682 _nfs4_proc_rename(old_dir
, old_name
,
2685 } while (exception
.retry
);
2689 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2691 struct nfs_server
*server
= NFS_SERVER(inode
);
2692 struct nfs4_link_arg arg
= {
2693 .fh
= NFS_FH(inode
),
2694 .dir_fh
= NFS_FH(dir
),
2696 .bitmask
= server
->attr_bitmask
,
2698 struct nfs4_link_res res
= {
2701 struct rpc_message msg
= {
2702 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2706 int status
= -ENOMEM
;
2708 res
.fattr
= nfs_alloc_fattr();
2709 res
.dir_attr
= nfs_alloc_fattr();
2710 if (res
.fattr
== NULL
|| res
.dir_attr
== NULL
)
2713 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2715 update_changeattr(dir
, &res
.cinfo
);
2716 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2717 nfs_post_op_update_inode(inode
, res
.fattr
);
2720 nfs_free_fattr(res
.dir_attr
);
2721 nfs_free_fattr(res
.fattr
);
2725 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2727 struct nfs4_exception exception
= { };
2730 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2731 _nfs4_proc_link(inode
, dir
, name
),
2733 } while (exception
.retry
);
2737 struct nfs4_createdata
{
2738 struct rpc_message msg
;
2739 struct nfs4_create_arg arg
;
2740 struct nfs4_create_res res
;
2742 struct nfs_fattr fattr
;
2743 struct nfs_fattr dir_fattr
;
2746 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2747 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2749 struct nfs4_createdata
*data
;
2751 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2753 struct nfs_server
*server
= NFS_SERVER(dir
);
2755 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2756 data
->msg
.rpc_argp
= &data
->arg
;
2757 data
->msg
.rpc_resp
= &data
->res
;
2758 data
->arg
.dir_fh
= NFS_FH(dir
);
2759 data
->arg
.server
= server
;
2760 data
->arg
.name
= name
;
2761 data
->arg
.attrs
= sattr
;
2762 data
->arg
.ftype
= ftype
;
2763 data
->arg
.bitmask
= server
->attr_bitmask
;
2764 data
->res
.server
= server
;
2765 data
->res
.fh
= &data
->fh
;
2766 data
->res
.fattr
= &data
->fattr
;
2767 data
->res
.dir_fattr
= &data
->dir_fattr
;
2768 nfs_fattr_init(data
->res
.fattr
);
2769 nfs_fattr_init(data
->res
.dir_fattr
);
2774 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2776 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2777 &data
->arg
, &data
->res
, 1);
2779 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2780 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2781 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2786 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2791 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2792 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2794 struct nfs4_createdata
*data
;
2795 int status
= -ENAMETOOLONG
;
2797 if (len
> NFS4_MAXPATHLEN
)
2801 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2805 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2806 data
->arg
.u
.symlink
.pages
= &page
;
2807 data
->arg
.u
.symlink
.len
= len
;
2809 status
= nfs4_do_create(dir
, dentry
, data
);
2811 nfs4_free_createdata(data
);
2816 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2817 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2819 struct nfs4_exception exception
= { };
2822 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2823 _nfs4_proc_symlink(dir
, dentry
, page
,
2826 } while (exception
.retry
);
2830 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2831 struct iattr
*sattr
)
2833 struct nfs4_createdata
*data
;
2834 int status
= -ENOMEM
;
2836 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2840 status
= nfs4_do_create(dir
, dentry
, data
);
2842 nfs4_free_createdata(data
);
2847 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2848 struct iattr
*sattr
)
2850 struct nfs4_exception exception
= { };
2853 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2854 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2856 } while (exception
.retry
);
2860 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2861 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2863 struct inode
*dir
= dentry
->d_inode
;
2864 struct nfs4_readdir_arg args
= {
2869 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
2871 struct nfs4_readdir_res res
;
2872 struct rpc_message msg
= {
2873 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2880 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2881 dentry
->d_parent
->d_name
.name
,
2882 dentry
->d_name
.name
,
2883 (unsigned long long)cookie
);
2884 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2885 res
.pgbase
= args
.pgbase
;
2886 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2888 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2890 nfs_invalidate_atime(dir
);
2892 dprintk("%s: returns %d\n", __func__
, status
);
2896 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2897 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2899 struct nfs4_exception exception
= { };
2902 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2903 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2906 } while (exception
.retry
);
2910 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2911 struct iattr
*sattr
, dev_t rdev
)
2913 struct nfs4_createdata
*data
;
2914 int mode
= sattr
->ia_mode
;
2915 int status
= -ENOMEM
;
2917 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2918 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2920 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2925 data
->arg
.ftype
= NF4FIFO
;
2926 else if (S_ISBLK(mode
)) {
2927 data
->arg
.ftype
= NF4BLK
;
2928 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2929 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2931 else if (S_ISCHR(mode
)) {
2932 data
->arg
.ftype
= NF4CHR
;
2933 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2934 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2937 status
= nfs4_do_create(dir
, dentry
, data
);
2939 nfs4_free_createdata(data
);
2944 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2945 struct iattr
*sattr
, dev_t rdev
)
2947 struct nfs4_exception exception
= { };
2950 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2951 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2953 } while (exception
.retry
);
2957 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2958 struct nfs_fsstat
*fsstat
)
2960 struct nfs4_statfs_arg args
= {
2962 .bitmask
= server
->attr_bitmask
,
2964 struct nfs4_statfs_res res
= {
2967 struct rpc_message msg
= {
2968 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2973 nfs_fattr_init(fsstat
->fattr
);
2974 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2977 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2979 struct nfs4_exception exception
= { };
2982 err
= nfs4_handle_exception(server
,
2983 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2985 } while (exception
.retry
);
2989 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2990 struct nfs_fsinfo
*fsinfo
)
2992 struct nfs4_fsinfo_arg args
= {
2994 .bitmask
= server
->attr_bitmask
,
2996 struct nfs4_fsinfo_res res
= {
2999 struct rpc_message msg
= {
3000 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3005 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3008 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3010 struct nfs4_exception exception
= { };
3014 err
= nfs4_handle_exception(server
,
3015 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
3017 } while (exception
.retry
);
3021 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3023 nfs_fattr_init(fsinfo
->fattr
);
3024 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3027 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3028 struct nfs_pathconf
*pathconf
)
3030 struct nfs4_pathconf_arg args
= {
3032 .bitmask
= server
->attr_bitmask
,
3034 struct nfs4_pathconf_res res
= {
3035 .pathconf
= pathconf
,
3037 struct rpc_message msg
= {
3038 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3043 /* None of the pathconf attributes are mandatory to implement */
3044 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3045 memset(pathconf
, 0, sizeof(*pathconf
));
3049 nfs_fattr_init(pathconf
->fattr
);
3050 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3053 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3054 struct nfs_pathconf
*pathconf
)
3056 struct nfs4_exception exception
= { };
3060 err
= nfs4_handle_exception(server
,
3061 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3063 } while (exception
.retry
);
3067 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3069 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3071 dprintk("--> %s\n", __func__
);
3073 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3076 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3077 nfs_restart_rpc(task
, server
->nfs_client
);
3081 nfs_invalidate_atime(data
->inode
);
3082 if (task
->tk_status
> 0)
3083 renew_lease(server
, data
->timestamp
);
3087 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3089 data
->timestamp
= jiffies
;
3090 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3093 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3095 struct inode
*inode
= data
->inode
;
3097 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3100 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3101 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3104 if (task
->tk_status
>= 0) {
3105 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3106 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3111 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3113 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3115 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3116 data
->res
.server
= server
;
3117 data
->timestamp
= jiffies
;
3119 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3122 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3124 struct inode
*inode
= data
->inode
;
3126 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3129 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3130 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3133 nfs_refresh_inode(inode
, data
->res
.fattr
);
3137 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3139 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3141 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3142 data
->res
.server
= server
;
3143 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3146 struct nfs4_renewdata
{
3147 struct nfs_client
*client
;
3148 unsigned long timestamp
;
3152 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3153 * standalone procedure for queueing an asynchronous RENEW.
3155 static void nfs4_renew_release(void *calldata
)
3157 struct nfs4_renewdata
*data
= calldata
;
3158 struct nfs_client
*clp
= data
->client
;
3160 if (atomic_read(&clp
->cl_count
) > 1)
3161 nfs4_schedule_state_renewal(clp
);
3162 nfs_put_client(clp
);
3166 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
3168 struct nfs4_renewdata
*data
= calldata
;
3169 struct nfs_client
*clp
= data
->client
;
3170 unsigned long timestamp
= data
->timestamp
;
3172 if (task
->tk_status
< 0) {
3173 /* Unless we're shutting down, schedule state recovery! */
3174 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3175 nfs4_schedule_state_recovery(clp
);
3178 do_renew_lease(clp
, timestamp
);
3181 static const struct rpc_call_ops nfs4_renew_ops
= {
3182 .rpc_call_done
= nfs4_renew_done
,
3183 .rpc_release
= nfs4_renew_release
,
3186 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3188 struct rpc_message msg
= {
3189 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3193 struct nfs4_renewdata
*data
;
3195 if (!atomic_inc_not_zero(&clp
->cl_count
))
3197 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
3201 data
->timestamp
= jiffies
;
3202 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3203 &nfs4_renew_ops
, data
);
3206 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3208 struct rpc_message msg
= {
3209 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3213 unsigned long now
= jiffies
;
3216 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3219 do_renew_lease(clp
, now
);
3223 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3225 return (server
->caps
& NFS_CAP_ACLS
)
3226 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3227 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3230 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3231 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3234 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3236 static void buf_to_pages(const void *buf
, size_t buflen
,
3237 struct page
**pages
, unsigned int *pgbase
)
3239 const void *p
= buf
;
3241 *pgbase
= offset_in_page(buf
);
3243 while (p
< buf
+ buflen
) {
3244 *(pages
++) = virt_to_page(p
);
3245 p
+= PAGE_CACHE_SIZE
;
3249 struct nfs4_cached_acl
{
3255 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3257 struct nfs_inode
*nfsi
= NFS_I(inode
);
3259 spin_lock(&inode
->i_lock
);
3260 kfree(nfsi
->nfs4_acl
);
3261 nfsi
->nfs4_acl
= acl
;
3262 spin_unlock(&inode
->i_lock
);
3265 static void nfs4_zap_acl_attr(struct inode
*inode
)
3267 nfs4_set_cached_acl(inode
, NULL
);
3270 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3272 struct nfs_inode
*nfsi
= NFS_I(inode
);
3273 struct nfs4_cached_acl
*acl
;
3276 spin_lock(&inode
->i_lock
);
3277 acl
= nfsi
->nfs4_acl
;
3280 if (buf
== NULL
) /* user is just asking for length */
3282 if (acl
->cached
== 0)
3284 ret
= -ERANGE
; /* see getxattr(2) man page */
3285 if (acl
->len
> buflen
)
3287 memcpy(buf
, acl
->data
, acl
->len
);
3291 spin_unlock(&inode
->i_lock
);
3295 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3297 struct nfs4_cached_acl
*acl
;
3299 if (buf
&& acl_len
<= PAGE_SIZE
) {
3300 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3304 memcpy(acl
->data
, buf
, acl_len
);
3306 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3313 nfs4_set_cached_acl(inode
, acl
);
3316 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3318 struct page
*pages
[NFS4ACL_MAXPAGES
];
3319 struct nfs_getaclargs args
= {
3320 .fh
= NFS_FH(inode
),
3324 struct nfs_getaclres res
= {
3328 struct rpc_message msg
= {
3329 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3333 struct page
*localpage
= NULL
;
3336 if (buflen
< PAGE_SIZE
) {
3337 /* As long as we're doing a round trip to the server anyway,
3338 * let's be prepared for a page of acl data. */
3339 localpage
= alloc_page(GFP_KERNEL
);
3340 resp_buf
= page_address(localpage
);
3341 if (localpage
== NULL
)
3343 args
.acl_pages
[0] = localpage
;
3344 args
.acl_pgbase
= 0;
3345 args
.acl_len
= PAGE_SIZE
;
3348 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3350 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3353 if (res
.acl_len
> args
.acl_len
)
3354 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3356 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3359 if (res
.acl_len
> buflen
)
3362 memcpy(buf
, resp_buf
, res
.acl_len
);
3367 __free_page(localpage
);
3371 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3373 struct nfs4_exception exception
= { };
3376 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3379 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3380 } while (exception
.retry
);
3384 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3386 struct nfs_server
*server
= NFS_SERVER(inode
);
3389 if (!nfs4_server_supports_acls(server
))
3391 ret
= nfs_revalidate_inode(server
, inode
);
3394 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3397 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3400 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3402 struct nfs_server
*server
= NFS_SERVER(inode
);
3403 struct page
*pages
[NFS4ACL_MAXPAGES
];
3404 struct nfs_setaclargs arg
= {
3405 .fh
= NFS_FH(inode
),
3409 struct nfs_setaclres res
;
3410 struct rpc_message msg
= {
3411 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3417 if (!nfs4_server_supports_acls(server
))
3419 nfs_inode_return_delegation(inode
);
3420 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3421 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3422 nfs_access_zap_cache(inode
);
3423 nfs_zap_acl_cache(inode
);
3427 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3429 struct nfs4_exception exception
= { };
3432 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3433 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3435 } while (exception
.retry
);
3440 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3442 struct nfs_client
*clp
= server
->nfs_client
;
3444 if (task
->tk_status
>= 0)
3446 switch(task
->tk_status
) {
3447 case -NFS4ERR_ADMIN_REVOKED
:
3448 case -NFS4ERR_BAD_STATEID
:
3449 case -NFS4ERR_OPENMODE
:
3452 nfs4_state_mark_reclaim_nograce(clp
, state
);
3453 goto do_state_recovery
;
3454 case -NFS4ERR_STALE_STATEID
:
3457 nfs4_state_mark_reclaim_reboot(clp
, state
);
3458 case -NFS4ERR_STALE_CLIENTID
:
3459 case -NFS4ERR_EXPIRED
:
3460 goto do_state_recovery
;
3461 #if defined(CONFIG_NFS_V4_1)
3462 case -NFS4ERR_BADSESSION
:
3463 case -NFS4ERR_BADSLOT
:
3464 case -NFS4ERR_BAD_HIGH_SLOT
:
3465 case -NFS4ERR_DEADSESSION
:
3466 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3467 case -NFS4ERR_SEQ_FALSE_RETRY
:
3468 case -NFS4ERR_SEQ_MISORDERED
:
3469 dprintk("%s ERROR %d, Reset session\n", __func__
,
3471 nfs4_schedule_state_recovery(clp
);
3472 task
->tk_status
= 0;
3474 #endif /* CONFIG_NFS_V4_1 */
3475 case -NFS4ERR_DELAY
:
3476 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3477 case -NFS4ERR_GRACE
:
3479 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3480 task
->tk_status
= 0;
3482 case -NFS4ERR_OLD_STATEID
:
3483 task
->tk_status
= 0;
3486 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3489 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3490 nfs4_schedule_state_recovery(clp
);
3491 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3492 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3493 task
->tk_status
= 0;
3497 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
3498 unsigned short port
, struct rpc_cred
*cred
,
3499 struct nfs4_setclientid_res
*res
)
3501 nfs4_verifier sc_verifier
;
3502 struct nfs4_setclientid setclientid
= {
3503 .sc_verifier
= &sc_verifier
,
3506 struct rpc_message msg
= {
3507 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3508 .rpc_argp
= &setclientid
,
3516 p
= (__be32
*)sc_verifier
.data
;
3517 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3518 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3521 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3522 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3524 rpc_peeraddr2str(clp
->cl_rpcclient
,
3526 rpc_peeraddr2str(clp
->cl_rpcclient
,
3528 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3529 clp
->cl_id_uniquifier
);
3530 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3531 sizeof(setclientid
.sc_netid
),
3532 rpc_peeraddr2str(clp
->cl_rpcclient
,
3533 RPC_DISPLAY_NETID
));
3534 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3535 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3536 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3538 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3539 if (status
!= -NFS4ERR_CLID_INUSE
)
3544 ssleep(clp
->cl_lease_time
+ 1);
3546 if (++clp
->cl_id_uniquifier
== 0)
3552 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
3553 struct nfs4_setclientid_res
*arg
,
3554 struct rpc_cred
*cred
)
3556 struct nfs_fsinfo fsinfo
;
3557 struct rpc_message msg
= {
3558 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3560 .rpc_resp
= &fsinfo
,
3567 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3569 spin_lock(&clp
->cl_lock
);
3570 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3571 clp
->cl_last_renewal
= now
;
3572 spin_unlock(&clp
->cl_lock
);
3577 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
3578 struct nfs4_setclientid_res
*arg
,
3579 struct rpc_cred
*cred
)
3584 err
= _nfs4_proc_setclientid_confirm(clp
, arg
, cred
);
3588 case -NFS4ERR_RESOURCE
:
3589 /* The IBM lawyers misread another document! */
3590 case -NFS4ERR_DELAY
:
3592 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3598 struct nfs4_delegreturndata
{
3599 struct nfs4_delegreturnargs args
;
3600 struct nfs4_delegreturnres res
;
3602 nfs4_stateid stateid
;
3603 unsigned long timestamp
;
3604 struct nfs_fattr fattr
;
3608 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3610 struct nfs4_delegreturndata
*data
= calldata
;
3612 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3615 switch (task
->tk_status
) {
3616 case -NFS4ERR_STALE_STATEID
:
3617 case -NFS4ERR_EXPIRED
:
3619 renew_lease(data
->res
.server
, data
->timestamp
);
3622 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
3624 nfs_restart_rpc(task
, data
->res
.server
->nfs_client
);
3628 data
->rpc_status
= task
->tk_status
;
3631 static void nfs4_delegreturn_release(void *calldata
)
3636 #if defined(CONFIG_NFS_V4_1)
3637 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3639 struct nfs4_delegreturndata
*d_data
;
3641 d_data
= (struct nfs4_delegreturndata
*)data
;
3643 if (nfs4_setup_sequence(d_data
->res
.server
,
3644 &d_data
->args
.seq_args
,
3645 &d_data
->res
.seq_res
, 1, task
))
3647 rpc_call_start(task
);
3649 #endif /* CONFIG_NFS_V4_1 */
3651 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3652 #if defined(CONFIG_NFS_V4_1)
3653 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3654 #endif /* CONFIG_NFS_V4_1 */
3655 .rpc_call_done
= nfs4_delegreturn_done
,
3656 .rpc_release
= nfs4_delegreturn_release
,
3659 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3661 struct nfs4_delegreturndata
*data
;
3662 struct nfs_server
*server
= NFS_SERVER(inode
);
3663 struct rpc_task
*task
;
3664 struct rpc_message msg
= {
3665 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3668 struct rpc_task_setup task_setup_data
= {
3669 .rpc_client
= server
->client
,
3670 .rpc_message
= &msg
,
3671 .callback_ops
= &nfs4_delegreturn_ops
,
3672 .flags
= RPC_TASK_ASYNC
,
3676 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
3679 data
->args
.fhandle
= &data
->fh
;
3680 data
->args
.stateid
= &data
->stateid
;
3681 data
->args
.bitmask
= server
->attr_bitmask
;
3682 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3683 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3684 data
->res
.fattr
= &data
->fattr
;
3685 data
->res
.server
= server
;
3686 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3687 nfs_fattr_init(data
->res
.fattr
);
3688 data
->timestamp
= jiffies
;
3689 data
->rpc_status
= 0;
3691 task_setup_data
.callback_data
= data
;
3692 msg
.rpc_argp
= &data
->args
,
3693 msg
.rpc_resp
= &data
->res
,
3694 task
= rpc_run_task(&task_setup_data
);
3696 return PTR_ERR(task
);
3699 status
= nfs4_wait_for_completion_rpc_task(task
);
3702 status
= data
->rpc_status
;
3705 nfs_refresh_inode(inode
, &data
->fattr
);
3711 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3713 struct nfs_server
*server
= NFS_SERVER(inode
);
3714 struct nfs4_exception exception
= { };
3717 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3719 case -NFS4ERR_STALE_STATEID
:
3720 case -NFS4ERR_EXPIRED
:
3724 err
= nfs4_handle_exception(server
, err
, &exception
);
3725 } while (exception
.retry
);
3729 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3730 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3733 * sleep, with exponential backoff, and retry the LOCK operation.
3735 static unsigned long
3736 nfs4_set_lock_task_retry(unsigned long timeout
)
3738 schedule_timeout_killable(timeout
);
3740 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3741 return NFS4_LOCK_MAXTIMEOUT
;
3745 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3747 struct inode
*inode
= state
->inode
;
3748 struct nfs_server
*server
= NFS_SERVER(inode
);
3749 struct nfs_client
*clp
= server
->nfs_client
;
3750 struct nfs_lockt_args arg
= {
3751 .fh
= NFS_FH(inode
),
3754 struct nfs_lockt_res res
= {
3757 struct rpc_message msg
= {
3758 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3761 .rpc_cred
= state
->owner
->so_cred
,
3763 struct nfs4_lock_state
*lsp
;
3766 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3767 status
= nfs4_set_lock_state(state
, request
);
3770 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3771 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3772 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3775 request
->fl_type
= F_UNLCK
;
3777 case -NFS4ERR_DENIED
:
3780 request
->fl_ops
->fl_release_private(request
);
3785 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3787 struct nfs4_exception exception
= { };
3791 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3792 _nfs4_proc_getlk(state
, cmd
, request
),
3794 } while (exception
.retry
);
3798 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3801 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3803 res
= posix_lock_file_wait(file
, fl
);
3806 res
= flock_lock_file_wait(file
, fl
);
3814 struct nfs4_unlockdata
{
3815 struct nfs_locku_args arg
;
3816 struct nfs_locku_res res
;
3817 struct nfs4_lock_state
*lsp
;
3818 struct nfs_open_context
*ctx
;
3819 struct file_lock fl
;
3820 const struct nfs_server
*server
;
3821 unsigned long timestamp
;
3824 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3825 struct nfs_open_context
*ctx
,
3826 struct nfs4_lock_state
*lsp
,
3827 struct nfs_seqid
*seqid
)
3829 struct nfs4_unlockdata
*p
;
3830 struct inode
*inode
= lsp
->ls_state
->inode
;
3832 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
3835 p
->arg
.fh
= NFS_FH(inode
);
3837 p
->arg
.seqid
= seqid
;
3838 p
->res
.seqid
= seqid
;
3839 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3840 p
->arg
.stateid
= &lsp
->ls_stateid
;
3842 atomic_inc(&lsp
->ls_count
);
3843 /* Ensure we don't close file until we're done freeing locks! */
3844 p
->ctx
= get_nfs_open_context(ctx
);
3845 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3846 p
->server
= NFS_SERVER(inode
);
3850 static void nfs4_locku_release_calldata(void *data
)
3852 struct nfs4_unlockdata
*calldata
= data
;
3853 nfs_free_seqid(calldata
->arg
.seqid
);
3854 nfs4_put_lock_state(calldata
->lsp
);
3855 put_nfs_open_context(calldata
->ctx
);
3859 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3861 struct nfs4_unlockdata
*calldata
= data
;
3863 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
3865 switch (task
->tk_status
) {
3867 memcpy(calldata
->lsp
->ls_stateid
.data
,
3868 calldata
->res
.stateid
.data
,
3869 sizeof(calldata
->lsp
->ls_stateid
.data
));
3870 renew_lease(calldata
->server
, calldata
->timestamp
);
3872 case -NFS4ERR_BAD_STATEID
:
3873 case -NFS4ERR_OLD_STATEID
:
3874 case -NFS4ERR_STALE_STATEID
:
3875 case -NFS4ERR_EXPIRED
:
3878 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3879 nfs_restart_rpc(task
,
3880 calldata
->server
->nfs_client
);
3884 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3886 struct nfs4_unlockdata
*calldata
= data
;
3888 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3890 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3891 /* Note: exit _without_ running nfs4_locku_done */
3892 task
->tk_action
= NULL
;
3895 calldata
->timestamp
= jiffies
;
3896 if (nfs4_setup_sequence(calldata
->server
,
3897 &calldata
->arg
.seq_args
,
3898 &calldata
->res
.seq_res
, 1, task
))
3900 rpc_call_start(task
);
3903 static const struct rpc_call_ops nfs4_locku_ops
= {
3904 .rpc_call_prepare
= nfs4_locku_prepare
,
3905 .rpc_call_done
= nfs4_locku_done
,
3906 .rpc_release
= nfs4_locku_release_calldata
,
3909 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3910 struct nfs_open_context
*ctx
,
3911 struct nfs4_lock_state
*lsp
,
3912 struct nfs_seqid
*seqid
)
3914 struct nfs4_unlockdata
*data
;
3915 struct rpc_message msg
= {
3916 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3917 .rpc_cred
= ctx
->cred
,
3919 struct rpc_task_setup task_setup_data
= {
3920 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3921 .rpc_message
= &msg
,
3922 .callback_ops
= &nfs4_locku_ops
,
3923 .workqueue
= nfsiod_workqueue
,
3924 .flags
= RPC_TASK_ASYNC
,
3927 /* Ensure this is an unlock - when canceling a lock, the
3928 * canceled lock is passed in, and it won't be an unlock.
3930 fl
->fl_type
= F_UNLCK
;
3932 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3934 nfs_free_seqid(seqid
);
3935 return ERR_PTR(-ENOMEM
);
3938 msg
.rpc_argp
= &data
->arg
,
3939 msg
.rpc_resp
= &data
->res
,
3940 task_setup_data
.callback_data
= data
;
3941 return rpc_run_task(&task_setup_data
);
3944 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3946 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3947 struct nfs_seqid
*seqid
;
3948 struct nfs4_lock_state
*lsp
;
3949 struct rpc_task
*task
;
3951 unsigned char fl_flags
= request
->fl_flags
;
3953 status
= nfs4_set_lock_state(state
, request
);
3954 /* Unlock _before_ we do the RPC call */
3955 request
->fl_flags
|= FL_EXISTS
;
3956 down_read(&nfsi
->rwsem
);
3957 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3958 up_read(&nfsi
->rwsem
);
3961 up_read(&nfsi
->rwsem
);
3964 /* Is this a delegated lock? */
3965 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3967 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3968 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
3972 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3973 status
= PTR_ERR(task
);
3976 status
= nfs4_wait_for_completion_rpc_task(task
);
3979 request
->fl_flags
= fl_flags
;
3983 struct nfs4_lockdata
{
3984 struct nfs_lock_args arg
;
3985 struct nfs_lock_res res
;
3986 struct nfs4_lock_state
*lsp
;
3987 struct nfs_open_context
*ctx
;
3988 struct file_lock fl
;
3989 unsigned long timestamp
;
3992 struct nfs_server
*server
;
3995 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3996 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
3999 struct nfs4_lockdata
*p
;
4000 struct inode
*inode
= lsp
->ls_state
->inode
;
4001 struct nfs_server
*server
= NFS_SERVER(inode
);
4003 p
= kzalloc(sizeof(*p
), gfp_mask
);
4007 p
->arg
.fh
= NFS_FH(inode
);
4009 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
4010 if (p
->arg
.open_seqid
== NULL
)
4012 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
4013 if (p
->arg
.lock_seqid
== NULL
)
4014 goto out_free_seqid
;
4015 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
4016 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4017 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
4018 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
4019 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4022 atomic_inc(&lsp
->ls_count
);
4023 p
->ctx
= get_nfs_open_context(ctx
);
4024 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4027 nfs_free_seqid(p
->arg
.open_seqid
);
4033 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
4035 struct nfs4_lockdata
*data
= calldata
;
4036 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4038 dprintk("%s: begin!\n", __func__
);
4039 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4041 /* Do we need to do an open_to_lock_owner? */
4042 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4043 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
4045 data
->arg
.open_stateid
= &state
->stateid
;
4046 data
->arg
.new_lock_owner
= 1;
4047 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4049 data
->arg
.new_lock_owner
= 0;
4050 data
->timestamp
= jiffies
;
4051 if (nfs4_setup_sequence(data
->server
,
4052 &data
->arg
.seq_args
,
4053 &data
->res
.seq_res
, 1, task
))
4055 rpc_call_start(task
);
4056 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4059 static void nfs4_recover_lock_prepare(struct rpc_task
*task
, void *calldata
)
4061 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4062 nfs4_lock_prepare(task
, calldata
);
4065 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4067 struct nfs4_lockdata
*data
= calldata
;
4069 dprintk("%s: begin!\n", __func__
);
4071 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4074 data
->rpc_status
= task
->tk_status
;
4075 if (data
->arg
.new_lock_owner
!= 0) {
4076 if (data
->rpc_status
== 0)
4077 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4081 if (data
->rpc_status
== 0) {
4082 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
4083 sizeof(data
->lsp
->ls_stateid
.data
));
4084 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
4085 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4088 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4091 static void nfs4_lock_release(void *calldata
)
4093 struct nfs4_lockdata
*data
= calldata
;
4095 dprintk("%s: begin!\n", __func__
);
4096 nfs_free_seqid(data
->arg
.open_seqid
);
4097 if (data
->cancelled
!= 0) {
4098 struct rpc_task
*task
;
4099 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4100 data
->arg
.lock_seqid
);
4103 dprintk("%s: cancelling lock!\n", __func__
);
4105 nfs_free_seqid(data
->arg
.lock_seqid
);
4106 nfs4_put_lock_state(data
->lsp
);
4107 put_nfs_open_context(data
->ctx
);
4109 dprintk("%s: done!\n", __func__
);
4112 static const struct rpc_call_ops nfs4_lock_ops
= {
4113 .rpc_call_prepare
= nfs4_lock_prepare
,
4114 .rpc_call_done
= nfs4_lock_done
,
4115 .rpc_release
= nfs4_lock_release
,
4118 static const struct rpc_call_ops nfs4_recover_lock_ops
= {
4119 .rpc_call_prepare
= nfs4_recover_lock_prepare
,
4120 .rpc_call_done
= nfs4_lock_done
,
4121 .rpc_release
= nfs4_lock_release
,
4124 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4126 struct nfs_client
*clp
= server
->nfs_client
;
4127 struct nfs4_state
*state
= lsp
->ls_state
;
4130 case -NFS4ERR_ADMIN_REVOKED
:
4131 case -NFS4ERR_BAD_STATEID
:
4132 case -NFS4ERR_EXPIRED
:
4133 if (new_lock_owner
!= 0 ||
4134 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4135 nfs4_state_mark_reclaim_nograce(clp
, state
);
4136 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4138 case -NFS4ERR_STALE_STATEID
:
4139 if (new_lock_owner
!= 0 ||
4140 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4141 nfs4_state_mark_reclaim_reboot(clp
, state
);
4142 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4146 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4148 struct nfs4_lockdata
*data
;
4149 struct rpc_task
*task
;
4150 struct rpc_message msg
= {
4151 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4152 .rpc_cred
= state
->owner
->so_cred
,
4154 struct rpc_task_setup task_setup_data
= {
4155 .rpc_client
= NFS_CLIENT(state
->inode
),
4156 .rpc_message
= &msg
,
4157 .callback_ops
= &nfs4_lock_ops
,
4158 .workqueue
= nfsiod_workqueue
,
4159 .flags
= RPC_TASK_ASYNC
,
4163 dprintk("%s: begin!\n", __func__
);
4164 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4165 fl
->fl_u
.nfs4_fl
.owner
,
4166 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
4170 data
->arg
.block
= 1;
4171 if (recovery_type
> NFS_LOCK_NEW
) {
4172 if (recovery_type
== NFS_LOCK_RECLAIM
)
4173 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4174 task_setup_data
.callback_ops
= &nfs4_recover_lock_ops
;
4176 msg
.rpc_argp
= &data
->arg
,
4177 msg
.rpc_resp
= &data
->res
,
4178 task_setup_data
.callback_data
= data
;
4179 task
= rpc_run_task(&task_setup_data
);
4181 return PTR_ERR(task
);
4182 ret
= nfs4_wait_for_completion_rpc_task(task
);
4184 ret
= data
->rpc_status
;
4186 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4187 data
->arg
.new_lock_owner
, ret
);
4189 data
->cancelled
= 1;
4191 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4195 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4197 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4198 struct nfs4_exception exception
= { };
4202 /* Cache the lock if possible... */
4203 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4205 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4206 if (err
!= -NFS4ERR_DELAY
&& err
!= -EKEYEXPIRED
)
4208 nfs4_handle_exception(server
, err
, &exception
);
4209 } while (exception
.retry
);
4213 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4215 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4216 struct nfs4_exception exception
= { };
4219 err
= nfs4_set_lock_state(state
, request
);
4223 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4225 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4229 case -NFS4ERR_GRACE
:
4230 case -NFS4ERR_DELAY
:
4232 nfs4_handle_exception(server
, err
, &exception
);
4235 } while (exception
.retry
);
4240 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4242 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4243 unsigned char fl_flags
= request
->fl_flags
;
4244 int status
= -ENOLCK
;
4246 if ((fl_flags
& FL_POSIX
) &&
4247 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4249 /* Is this a delegated open? */
4250 status
= nfs4_set_lock_state(state
, request
);
4253 request
->fl_flags
|= FL_ACCESS
;
4254 status
= do_vfs_lock(request
->fl_file
, request
);
4257 down_read(&nfsi
->rwsem
);
4258 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4259 /* Yes: cache locks! */
4260 /* ...but avoid races with delegation recall... */
4261 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4262 status
= do_vfs_lock(request
->fl_file
, request
);
4265 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4268 /* Note: we always want to sleep here! */
4269 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4270 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4271 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4273 up_read(&nfsi
->rwsem
);
4275 request
->fl_flags
= fl_flags
;
4279 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4281 struct nfs4_exception exception
= { };
4285 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4286 if (err
== -NFS4ERR_DENIED
)
4288 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4290 } while (exception
.retry
);
4295 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4297 struct nfs_open_context
*ctx
;
4298 struct nfs4_state
*state
;
4299 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4302 /* verify open state */
4303 ctx
= nfs_file_open_context(filp
);
4306 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4309 if (IS_GETLK(cmd
)) {
4311 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4315 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4318 if (request
->fl_type
== F_UNLCK
) {
4320 return nfs4_proc_unlck(state
, cmd
, request
);
4327 status
= nfs4_proc_setlk(state
, cmd
, request
);
4328 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4330 timeout
= nfs4_set_lock_task_retry(timeout
);
4331 status
= -ERESTARTSYS
;
4334 } while(status
< 0);
4338 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4340 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4341 struct nfs4_exception exception
= { };
4344 err
= nfs4_set_lock_state(state
, fl
);
4348 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4351 printk(KERN_ERR
"%s: unhandled error %d.\n",
4356 case -NFS4ERR_EXPIRED
:
4357 case -NFS4ERR_STALE_CLIENTID
:
4358 case -NFS4ERR_STALE_STATEID
:
4359 case -NFS4ERR_BADSESSION
:
4360 case -NFS4ERR_BADSLOT
:
4361 case -NFS4ERR_BAD_HIGH_SLOT
:
4362 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4363 case -NFS4ERR_DEADSESSION
:
4364 nfs4_schedule_state_recovery(server
->nfs_client
);
4368 * The show must go on: exit, but mark the
4369 * stateid as needing recovery.
4371 case -NFS4ERR_ADMIN_REVOKED
:
4372 case -NFS4ERR_BAD_STATEID
:
4373 case -NFS4ERR_OPENMODE
:
4374 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
4378 case -NFS4ERR_DENIED
:
4379 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4382 case -NFS4ERR_DELAY
:
4386 err
= nfs4_handle_exception(server
, err
, &exception
);
4387 } while (exception
.retry
);
4392 static void nfs4_release_lockowner_release(void *calldata
)
4397 const struct rpc_call_ops nfs4_release_lockowner_ops
= {
4398 .rpc_release
= nfs4_release_lockowner_release
,
4401 void nfs4_release_lockowner(const struct nfs4_lock_state
*lsp
)
4403 struct nfs_server
*server
= lsp
->ls_state
->owner
->so_server
;
4404 struct nfs_release_lockowner_args
*args
;
4405 struct rpc_message msg
= {
4406 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
4409 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
4411 args
= kmalloc(sizeof(*args
), GFP_NOFS
);
4414 args
->lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4415 args
->lock_owner
.id
= lsp
->ls_id
.id
;
4416 msg
.rpc_argp
= args
;
4417 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, args
);
4420 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4422 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4423 size_t buflen
, int flags
)
4425 struct inode
*inode
= dentry
->d_inode
;
4427 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4430 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4433 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4434 * and that's what we'll do for e.g. user attributes that haven't been set.
4435 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4436 * attributes in kernel-managed attribute namespaces. */
4437 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4440 struct inode
*inode
= dentry
->d_inode
;
4442 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4445 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4448 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4450 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4452 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4454 if (buf
&& buflen
< len
)
4457 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4461 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4463 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4464 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4465 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4468 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4469 NFS_ATTR_FATTR_NLINK
;
4470 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4474 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4475 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4477 struct nfs_server
*server
= NFS_SERVER(dir
);
4479 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4480 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4482 struct nfs4_fs_locations_arg args
= {
4483 .dir_fh
= NFS_FH(dir
),
4488 struct nfs4_fs_locations_res res
= {
4489 .fs_locations
= fs_locations
,
4491 struct rpc_message msg
= {
4492 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4498 dprintk("%s: start\n", __func__
);
4499 nfs_fattr_init(&fs_locations
->fattr
);
4500 fs_locations
->server
= server
;
4501 fs_locations
->nlocations
= 0;
4502 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4503 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4504 dprintk("%s: returned status = %d\n", __func__
, status
);
4508 #ifdef CONFIG_NFS_V4_1
4510 * nfs4_proc_exchange_id()
4512 * Since the clientid has expired, all compounds using sessions
4513 * associated with the stale clientid will be returning
4514 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4515 * be in some phase of session reset.
4517 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4519 nfs4_verifier verifier
;
4520 struct nfs41_exchange_id_args args
= {
4522 .flags
= clp
->cl_exchange_flags
,
4524 struct nfs41_exchange_id_res res
= {
4528 struct rpc_message msg
= {
4529 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4536 dprintk("--> %s\n", __func__
);
4537 BUG_ON(clp
== NULL
);
4539 /* Remove server-only flags */
4540 args
.flags
&= ~EXCHGID4_FLAG_CONFIRMED_R
;
4542 p
= (u32
*)verifier
.data
;
4543 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4544 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4545 args
.verifier
= &verifier
;
4548 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4551 rpc_peeraddr2str(clp
->cl_rpcclient
,
4553 clp
->cl_id_uniquifier
);
4555 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4557 if (status
!= -NFS4ERR_CLID_INUSE
)
4563 if (++clp
->cl_id_uniquifier
== 0)
4567 dprintk("<-- %s status= %d\n", __func__
, status
);
4571 struct nfs4_get_lease_time_data
{
4572 struct nfs4_get_lease_time_args
*args
;
4573 struct nfs4_get_lease_time_res
*res
;
4574 struct nfs_client
*clp
;
4577 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4581 struct nfs4_get_lease_time_data
*data
=
4582 (struct nfs4_get_lease_time_data
*)calldata
;
4584 dprintk("--> %s\n", __func__
);
4585 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4586 /* just setup sequence, do not trigger session recovery
4587 since we're invoked within one */
4588 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4589 &data
->args
->la_seq_args
,
4590 &data
->res
->lr_seq_res
, 0, task
);
4592 BUG_ON(ret
== -EAGAIN
);
4593 rpc_call_start(task
);
4594 dprintk("<-- %s\n", __func__
);
4598 * Called from nfs4_state_manager thread for session setup, so don't recover
4599 * from sequence operation or clientid errors.
4601 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4603 struct nfs4_get_lease_time_data
*data
=
4604 (struct nfs4_get_lease_time_data
*)calldata
;
4606 dprintk("--> %s\n", __func__
);
4607 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
4609 switch (task
->tk_status
) {
4610 case -NFS4ERR_DELAY
:
4611 case -NFS4ERR_GRACE
:
4613 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4614 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4615 task
->tk_status
= 0;
4616 nfs_restart_rpc(task
, data
->clp
);
4619 dprintk("<-- %s\n", __func__
);
4622 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4623 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4624 .rpc_call_done
= nfs4_get_lease_time_done
,
4627 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4629 struct rpc_task
*task
;
4630 struct nfs4_get_lease_time_args args
;
4631 struct nfs4_get_lease_time_res res
= {
4632 .lr_fsinfo
= fsinfo
,
4634 struct nfs4_get_lease_time_data data
= {
4639 struct rpc_message msg
= {
4640 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4644 struct rpc_task_setup task_setup
= {
4645 .rpc_client
= clp
->cl_rpcclient
,
4646 .rpc_message
= &msg
,
4647 .callback_ops
= &nfs4_get_lease_time_ops
,
4648 .callback_data
= &data
4652 res
.lr_seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4653 dprintk("--> %s\n", __func__
);
4654 task
= rpc_run_task(&task_setup
);
4657 status
= PTR_ERR(task
);
4659 status
= task
->tk_status
;
4662 dprintk("<-- %s return %d\n", __func__
, status
);
4668 * Reset a slot table
4670 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, u32 max_reqs
,
4673 struct nfs4_slot
*new = NULL
;
4677 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__
,
4678 max_reqs
, tbl
->max_slots
);
4680 /* Does the newly negotiated max_reqs match the existing slot table? */
4681 if (max_reqs
!= tbl
->max_slots
) {
4683 new = kmalloc(max_reqs
* sizeof(struct nfs4_slot
),
4690 spin_lock(&tbl
->slot_tbl_lock
);
4693 tbl
->max_slots
= max_reqs
;
4695 for (i
= 0; i
< tbl
->max_slots
; ++i
)
4696 tbl
->slots
[i
].seq_nr
= ivalue
;
4697 spin_unlock(&tbl
->slot_tbl_lock
);
4698 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4699 tbl
, tbl
->slots
, tbl
->max_slots
);
4701 dprintk("<-- %s: return %d\n", __func__
, ret
);
4706 * Reset the forechannel and backchannel slot tables
4708 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
4712 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
4713 session
->fc_attrs
.max_reqs
, 1);
4717 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
4718 session
->bc_attrs
.max_reqs
, 0);
4722 /* Destroy the slot table */
4723 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
4725 if (session
->fc_slot_table
.slots
!= NULL
) {
4726 kfree(session
->fc_slot_table
.slots
);
4727 session
->fc_slot_table
.slots
= NULL
;
4729 if (session
->bc_slot_table
.slots
!= NULL
) {
4730 kfree(session
->bc_slot_table
.slots
);
4731 session
->bc_slot_table
.slots
= NULL
;
4737 * Initialize slot table
4739 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
4740 int max_slots
, int ivalue
)
4742 struct nfs4_slot
*slot
;
4745 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4747 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
4749 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_NOFS
);
4754 spin_lock(&tbl
->slot_tbl_lock
);
4755 tbl
->max_slots
= max_slots
;
4757 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4758 spin_unlock(&tbl
->slot_tbl_lock
);
4759 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4760 tbl
, tbl
->slots
, tbl
->max_slots
);
4762 dprintk("<-- %s: return %d\n", __func__
, ret
);
4767 * Initialize the forechannel and backchannel tables
4769 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
4771 struct nfs4_slot_table
*tbl
;
4774 tbl
= &session
->fc_slot_table
;
4775 if (tbl
->slots
== NULL
) {
4776 status
= nfs4_init_slot_table(tbl
,
4777 session
->fc_attrs
.max_reqs
, 1);
4782 tbl
= &session
->bc_slot_table
;
4783 if (tbl
->slots
== NULL
) {
4784 status
= nfs4_init_slot_table(tbl
,
4785 session
->bc_attrs
.max_reqs
, 0);
4787 nfs4_destroy_slot_tables(session
);
4793 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4795 struct nfs4_session
*session
;
4796 struct nfs4_slot_table
*tbl
;
4798 session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
4802 init_completion(&session
->complete
);
4804 tbl
= &session
->fc_slot_table
;
4805 tbl
->highest_used_slotid
= -1;
4806 spin_lock_init(&tbl
->slot_tbl_lock
);
4807 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
4809 tbl
= &session
->bc_slot_table
;
4810 tbl
->highest_used_slotid
= -1;
4811 spin_lock_init(&tbl
->slot_tbl_lock
);
4812 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
4814 session
->session_state
= 1<<NFS4_SESSION_INITING
;
4820 void nfs4_destroy_session(struct nfs4_session
*session
)
4822 nfs4_proc_destroy_session(session
);
4823 dprintk("%s Destroy backchannel for xprt %p\n",
4824 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
4825 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
4826 NFS41_BC_MIN_CALLBACKS
);
4827 nfs4_destroy_slot_tables(session
);
4832 * Initialize the values to be used by the client in CREATE_SESSION
4833 * If nfs4_init_session set the fore channel request and response sizes,
4836 * Set the back channel max_resp_sz_cached to zero to force the client to
4837 * always set csa_cachethis to FALSE because the current implementation
4838 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4840 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4842 struct nfs4_session
*session
= args
->client
->cl_session
;
4843 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4844 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4847 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4849 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4850 /* Fore channel attributes */
4851 args
->fc_attrs
.headerpadsz
= 0;
4852 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4853 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4854 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4855 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4857 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4858 "max_ops=%u max_reqs=%u\n",
4860 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4861 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
4863 /* Back channel attributes */
4864 args
->bc_attrs
.headerpadsz
= 0;
4865 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4866 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4867 args
->bc_attrs
.max_resp_sz_cached
= 0;
4868 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4869 args
->bc_attrs
.max_reqs
= 1;
4871 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4872 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4874 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4875 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4876 args
->bc_attrs
.max_reqs
);
4879 static int _verify_channel_attr(char *chan
, char *attr_name
, u32 sent
, u32 rcvd
)
4883 printk(KERN_WARNING
"%s: Session INVALID: %s channel %s increased. "
4884 "sent=%u rcvd=%u\n", __func__
, chan
, attr_name
, sent
, rcvd
);
4888 #define _verify_fore_channel_attr(_name_) \
4889 _verify_channel_attr("fore", #_name_, \
4890 args->fc_attrs._name_, \
4891 session->fc_attrs._name_)
4893 #define _verify_back_channel_attr(_name_) \
4894 _verify_channel_attr("back", #_name_, \
4895 args->bc_attrs._name_, \
4896 session->bc_attrs._name_)
4899 * The server is not allowed to increase the fore channel header pad size,
4900 * maximum response size, or maximum number of operations.
4902 * The back channel attributes are only negotiatied down: We send what the
4903 * (back channel) server insists upon.
4905 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4906 struct nfs4_session
*session
)
4910 ret
|= _verify_fore_channel_attr(headerpadsz
);
4911 ret
|= _verify_fore_channel_attr(max_resp_sz
);
4912 ret
|= _verify_fore_channel_attr(max_ops
);
4914 ret
|= _verify_back_channel_attr(headerpadsz
);
4915 ret
|= _verify_back_channel_attr(max_rqst_sz
);
4916 ret
|= _verify_back_channel_attr(max_resp_sz
);
4917 ret
|= _verify_back_channel_attr(max_resp_sz_cached
);
4918 ret
|= _verify_back_channel_attr(max_ops
);
4919 ret
|= _verify_back_channel_attr(max_reqs
);
4924 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4926 struct nfs4_session
*session
= clp
->cl_session
;
4927 struct nfs41_create_session_args args
= {
4929 .cb_program
= NFS4_CALLBACK
,
4931 struct nfs41_create_session_res res
= {
4934 struct rpc_message msg
= {
4935 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4941 nfs4_init_channel_attrs(&args
);
4942 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
4944 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4947 /* Verify the session's negotiated channel_attrs values */
4948 status
= nfs4_verify_channel_attrs(&args
, session
);
4950 /* Increment the clientid slot sequence id */
4958 * Issues a CREATE_SESSION operation to the server.
4959 * It is the responsibility of the caller to verify the session is
4960 * expired before calling this routine.
4962 int nfs4_proc_create_session(struct nfs_client
*clp
)
4966 struct nfs4_session
*session
= clp
->cl_session
;
4968 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4970 status
= _nfs4_proc_create_session(clp
);
4974 /* Init and reset the fore channel */
4975 status
= nfs4_init_slot_tables(session
);
4976 dprintk("slot table initialization returned %d\n", status
);
4979 status
= nfs4_reset_slot_tables(session
);
4980 dprintk("slot table reset returned %d\n", status
);
4984 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4985 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4986 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4988 dprintk("<-- %s\n", __func__
);
4993 * Issue the over-the-wire RPC DESTROY_SESSION.
4994 * The caller must serialize access to this routine.
4996 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4999 struct rpc_message msg
;
5001 dprintk("--> nfs4_proc_destroy_session\n");
5003 /* session is still being setup */
5004 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
5007 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
5008 msg
.rpc_argp
= session
;
5009 msg
.rpc_resp
= NULL
;
5010 msg
.rpc_cred
= NULL
;
5011 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
5015 "Got error %d from the server on DESTROY_SESSION. "
5016 "Session has been destroyed regardless...\n", status
);
5018 dprintk("<-- nfs4_proc_destroy_session\n");
5022 int nfs4_init_session(struct nfs_server
*server
)
5024 struct nfs_client
*clp
= server
->nfs_client
;
5025 struct nfs4_session
*session
;
5026 unsigned int rsize
, wsize
;
5029 if (!nfs4_has_session(clp
))
5032 session
= clp
->cl_session
;
5033 if (!test_and_clear_bit(NFS4_SESSION_INITING
, &session
->session_state
))
5036 rsize
= server
->rsize
;
5038 rsize
= NFS_MAX_FILE_IO_SIZE
;
5039 wsize
= server
->wsize
;
5041 wsize
= NFS_MAX_FILE_IO_SIZE
;
5043 session
->fc_attrs
.max_rqst_sz
= wsize
+ nfs41_maxwrite_overhead
;
5044 session
->fc_attrs
.max_resp_sz
= rsize
+ nfs41_maxread_overhead
;
5046 ret
= nfs4_recover_expired_lease(server
);
5048 ret
= nfs4_check_client_ready(clp
);
5053 * Renew the cl_session lease.
5055 struct nfs4_sequence_data
{
5056 struct nfs_client
*clp
;
5057 struct nfs4_sequence_args args
;
5058 struct nfs4_sequence_res res
;
5061 static void nfs41_sequence_release(void *data
)
5063 struct nfs4_sequence_data
*calldata
= data
;
5064 struct nfs_client
*clp
= calldata
->clp
;
5066 if (atomic_read(&clp
->cl_count
) > 1)
5067 nfs4_schedule_state_renewal(clp
);
5068 nfs_put_client(clp
);
5072 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5074 switch(task
->tk_status
) {
5075 case -NFS4ERR_DELAY
:
5077 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5080 nfs4_schedule_state_recovery(clp
);
5085 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5087 struct nfs4_sequence_data
*calldata
= data
;
5088 struct nfs_client
*clp
= calldata
->clp
;
5090 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
5093 if (task
->tk_status
< 0) {
5094 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5095 if (atomic_read(&clp
->cl_count
) == 1)
5098 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
5099 rpc_restart_call_prepare(task
);
5103 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5105 dprintk("<-- %s\n", __func__
);
5108 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5110 struct nfs4_sequence_data
*calldata
= data
;
5111 struct nfs_client
*clp
= calldata
->clp
;
5112 struct nfs4_sequence_args
*args
;
5113 struct nfs4_sequence_res
*res
;
5115 args
= task
->tk_msg
.rpc_argp
;
5116 res
= task
->tk_msg
.rpc_resp
;
5118 if (nfs41_setup_sequence(clp
->cl_session
, args
, res
, 0, task
))
5120 rpc_call_start(task
);
5123 static const struct rpc_call_ops nfs41_sequence_ops
= {
5124 .rpc_call_done
= nfs41_sequence_call_done
,
5125 .rpc_call_prepare
= nfs41_sequence_prepare
,
5126 .rpc_release
= nfs41_sequence_release
,
5129 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5131 struct nfs4_sequence_data
*calldata
;
5132 struct rpc_message msg
= {
5133 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5136 struct rpc_task_setup task_setup_data
= {
5137 .rpc_client
= clp
->cl_rpcclient
,
5138 .rpc_message
= &msg
,
5139 .callback_ops
= &nfs41_sequence_ops
,
5140 .flags
= RPC_TASK_ASYNC
| RPC_TASK_SOFT
,
5143 if (!atomic_inc_not_zero(&clp
->cl_count
))
5144 return ERR_PTR(-EIO
);
5145 calldata
= kmalloc(sizeof(*calldata
), GFP_NOFS
);
5146 if (calldata
== NULL
) {
5147 nfs_put_client(clp
);
5148 return ERR_PTR(-ENOMEM
);
5150 calldata
->res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5151 msg
.rpc_argp
= &calldata
->args
;
5152 msg
.rpc_resp
= &calldata
->res
;
5153 calldata
->clp
= clp
;
5154 task_setup_data
.callback_data
= calldata
;
5156 return rpc_run_task(&task_setup_data
);
5159 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5161 struct rpc_task
*task
;
5164 task
= _nfs41_proc_sequence(clp
, cred
);
5166 ret
= PTR_ERR(task
);
5169 dprintk("<-- %s status=%d\n", __func__
, ret
);
5173 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5175 struct rpc_task
*task
;
5178 task
= _nfs41_proc_sequence(clp
, cred
);
5180 ret
= PTR_ERR(task
);
5183 ret
= rpc_wait_for_completion_task(task
);
5185 ret
= task
->tk_status
;
5188 dprintk("<-- %s status=%d\n", __func__
, ret
);
5192 struct nfs4_reclaim_complete_data
{
5193 struct nfs_client
*clp
;
5194 struct nfs41_reclaim_complete_args arg
;
5195 struct nfs41_reclaim_complete_res res
;
5198 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5200 struct nfs4_reclaim_complete_data
*calldata
= data
;
5202 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
5203 if (nfs41_setup_sequence(calldata
->clp
->cl_session
,
5204 &calldata
->arg
.seq_args
,
5205 &calldata
->res
.seq_res
, 0, task
))
5208 rpc_call_start(task
);
5211 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5213 switch(task
->tk_status
) {
5215 case -NFS4ERR_COMPLETE_ALREADY
:
5216 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
5218 case -NFS4ERR_DELAY
:
5220 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5223 nfs4_schedule_state_recovery(clp
);
5228 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5230 struct nfs4_reclaim_complete_data
*calldata
= data
;
5231 struct nfs_client
*clp
= calldata
->clp
;
5232 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5234 dprintk("--> %s\n", __func__
);
5235 if (!nfs41_sequence_done(task
, res
))
5238 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
5239 rpc_restart_call_prepare(task
);
5242 dprintk("<-- %s\n", __func__
);
5245 static void nfs4_free_reclaim_complete_data(void *data
)
5247 struct nfs4_reclaim_complete_data
*calldata
= data
;
5252 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5253 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5254 .rpc_call_done
= nfs4_reclaim_complete_done
,
5255 .rpc_release
= nfs4_free_reclaim_complete_data
,
5259 * Issue a global reclaim complete.
5261 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5263 struct nfs4_reclaim_complete_data
*calldata
;
5264 struct rpc_task
*task
;
5265 struct rpc_message msg
= {
5266 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5268 struct rpc_task_setup task_setup_data
= {
5269 .rpc_client
= clp
->cl_rpcclient
,
5270 .rpc_message
= &msg
,
5271 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5272 .flags
= RPC_TASK_ASYNC
,
5274 int status
= -ENOMEM
;
5276 dprintk("--> %s\n", __func__
);
5277 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5278 if (calldata
== NULL
)
5280 calldata
->clp
= clp
;
5281 calldata
->arg
.one_fs
= 0;
5282 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5284 msg
.rpc_argp
= &calldata
->arg
;
5285 msg
.rpc_resp
= &calldata
->res
;
5286 task_setup_data
.callback_data
= calldata
;
5287 task
= rpc_run_task(&task_setup_data
);
5289 status
= PTR_ERR(task
);
5295 dprintk("<-- %s status=%d\n", __func__
, status
);
5298 #endif /* CONFIG_NFS_V4_1 */
5300 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5301 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5302 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5303 .recover_open
= nfs4_open_reclaim
,
5304 .recover_lock
= nfs4_lock_reclaim
,
5305 .establish_clid
= nfs4_init_clientid
,
5306 .get_clid_cred
= nfs4_get_setclientid_cred
,
5309 #if defined(CONFIG_NFS_V4_1)
5310 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5311 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5312 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5313 .recover_open
= nfs4_open_reclaim
,
5314 .recover_lock
= nfs4_lock_reclaim
,
5315 .establish_clid
= nfs41_init_clientid
,
5316 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5317 .reclaim_complete
= nfs41_proc_reclaim_complete
,
5319 #endif /* CONFIG_NFS_V4_1 */
5321 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5322 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5323 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5324 .recover_open
= nfs4_open_expired
,
5325 .recover_lock
= nfs4_lock_expired
,
5326 .establish_clid
= nfs4_init_clientid
,
5327 .get_clid_cred
= nfs4_get_setclientid_cred
,
5330 #if defined(CONFIG_NFS_V4_1)
5331 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5332 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5333 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5334 .recover_open
= nfs4_open_expired
,
5335 .recover_lock
= nfs4_lock_expired
,
5336 .establish_clid
= nfs41_init_clientid
,
5337 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5339 #endif /* CONFIG_NFS_V4_1 */
5341 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
5342 .sched_state_renewal
= nfs4_proc_async_renew
,
5343 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
5344 .renew_lease
= nfs4_proc_renew
,
5347 #if defined(CONFIG_NFS_V4_1)
5348 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
5349 .sched_state_renewal
= nfs41_proc_async_sequence
,
5350 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
5351 .renew_lease
= nfs4_proc_sequence
,
5355 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
5357 .call_sync
= _nfs4_call_sync
,
5358 .validate_stateid
= nfs4_validate_delegation_stateid
,
5359 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
5360 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
5361 .state_renewal_ops
= &nfs40_state_renewal_ops
,
5364 #if defined(CONFIG_NFS_V4_1)
5365 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
5367 .call_sync
= _nfs4_call_sync_session
,
5368 .validate_stateid
= nfs41_validate_delegation_stateid
,
5369 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
5370 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
5371 .state_renewal_ops
= &nfs41_state_renewal_ops
,
5375 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
5376 [0] = &nfs_v4_0_minor_ops
,
5377 #if defined(CONFIG_NFS_V4_1)
5378 [1] = &nfs_v4_1_minor_ops
,
5382 static const struct inode_operations nfs4_file_inode_operations
= {
5383 .permission
= nfs_permission
,
5384 .getattr
= nfs_getattr
,
5385 .setattr
= nfs_setattr
,
5386 .getxattr
= nfs4_getxattr
,
5387 .setxattr
= nfs4_setxattr
,
5388 .listxattr
= nfs4_listxattr
,
5391 const struct nfs_rpc_ops nfs_v4_clientops
= {
5392 .version
= 4, /* protocol version */
5393 .dentry_ops
= &nfs4_dentry_operations
,
5394 .dir_inode_ops
= &nfs4_dir_inode_operations
,
5395 .file_inode_ops
= &nfs4_file_inode_operations
,
5396 .getroot
= nfs4_proc_get_root
,
5397 .getattr
= nfs4_proc_getattr
,
5398 .setattr
= nfs4_proc_setattr
,
5399 .lookupfh
= nfs4_proc_lookupfh
,
5400 .lookup
= nfs4_proc_lookup
,
5401 .access
= nfs4_proc_access
,
5402 .readlink
= nfs4_proc_readlink
,
5403 .create
= nfs4_proc_create
,
5404 .remove
= nfs4_proc_remove
,
5405 .unlink_setup
= nfs4_proc_unlink_setup
,
5406 .unlink_done
= nfs4_proc_unlink_done
,
5407 .rename
= nfs4_proc_rename
,
5408 .link
= nfs4_proc_link
,
5409 .symlink
= nfs4_proc_symlink
,
5410 .mkdir
= nfs4_proc_mkdir
,
5411 .rmdir
= nfs4_proc_remove
,
5412 .readdir
= nfs4_proc_readdir
,
5413 .mknod
= nfs4_proc_mknod
,
5414 .statfs
= nfs4_proc_statfs
,
5415 .fsinfo
= nfs4_proc_fsinfo
,
5416 .pathconf
= nfs4_proc_pathconf
,
5417 .set_capabilities
= nfs4_server_capabilities
,
5418 .decode_dirent
= nfs4_decode_dirent
,
5419 .read_setup
= nfs4_proc_read_setup
,
5420 .read_done
= nfs4_read_done
,
5421 .write_setup
= nfs4_proc_write_setup
,
5422 .write_done
= nfs4_write_done
,
5423 .commit_setup
= nfs4_proc_commit_setup
,
5424 .commit_done
= nfs4_commit_done
,
5425 .lock
= nfs4_proc_lock
,
5426 .clear_acl_cache
= nfs4_zap_acl_attr
,
5427 .close_context
= nfs4_close_context
,