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/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
59 #include "delegation.h"
65 #include "nfs4idmap.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
76 /* file attributes which can be mapped to nfs attributes */
77 #define NFS4_VALID_ATTRS (ATTR_MODE \
88 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
89 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
90 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
91 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
92 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
93 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
94 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
95 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
96 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
97 struct nfs4_label
*olabel
);
98 #ifdef CONFIG_NFS_V4_1
99 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
100 struct rpc_cred
*cred
,
101 struct nfs4_slot
*slot
,
103 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
105 static int nfs41_free_stateid(struct nfs_server
*, const nfs4_stateid
*,
106 struct rpc_cred
*, bool);
109 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
110 static inline struct nfs4_label
*
111 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
112 struct iattr
*sattr
, struct nfs4_label
*label
)
119 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
122 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
123 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
130 nfs4_label_release_security(struct nfs4_label
*label
)
133 security_release_secctx(label
->label
, label
->len
);
135 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
138 return server
->attr_bitmask
;
140 return server
->attr_bitmask_nl
;
143 static inline struct nfs4_label
*
144 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
145 struct iattr
*sattr
, struct nfs4_label
*l
)
148 nfs4_label_release_security(struct nfs4_label
*label
)
151 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
152 { return server
->attr_bitmask
; }
155 /* Prevent leaks of NFSv4 errors into userland */
156 static int nfs4_map_errors(int err
)
161 case -NFS4ERR_RESOURCE
:
162 case -NFS4ERR_LAYOUTTRYLATER
:
163 case -NFS4ERR_RECALLCONFLICT
:
165 case -NFS4ERR_WRONGSEC
:
166 case -NFS4ERR_WRONG_CRED
:
168 case -NFS4ERR_BADOWNER
:
169 case -NFS4ERR_BADNAME
:
171 case -NFS4ERR_SHARE_DENIED
:
173 case -NFS4ERR_MINOR_VERS_MISMATCH
:
174 return -EPROTONOSUPPORT
;
175 case -NFS4ERR_FILE_OPEN
:
178 dprintk("%s could not handle NFSv4 error %d\n",
186 * This is our standard bitmap for GETATTR requests.
188 const u32 nfs4_fattr_bitmap
[3] = {
190 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_FILEID
,
195 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER_GROUP
198 | FATTR4_WORD1_RAWDEV
199 | FATTR4_WORD1_SPACE_USED
200 | FATTR4_WORD1_TIME_ACCESS
201 | FATTR4_WORD1_TIME_METADATA
202 | FATTR4_WORD1_TIME_MODIFY
203 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
204 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
205 FATTR4_WORD2_SECURITY_LABEL
209 static const u32 nfs4_pnfs_open_bitmap
[3] = {
211 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_FILEID
,
216 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER_GROUP
219 | FATTR4_WORD1_RAWDEV
220 | FATTR4_WORD1_SPACE_USED
221 | FATTR4_WORD1_TIME_ACCESS
222 | FATTR4_WORD1_TIME_METADATA
223 | FATTR4_WORD1_TIME_MODIFY
,
224 FATTR4_WORD2_MDSTHRESHOLD
225 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
226 | FATTR4_WORD2_SECURITY_LABEL
230 static const u32 nfs4_open_noattr_bitmap
[3] = {
232 | FATTR4_WORD0_FILEID
,
235 const u32 nfs4_statfs_bitmap
[3] = {
236 FATTR4_WORD0_FILES_AVAIL
237 | FATTR4_WORD0_FILES_FREE
238 | FATTR4_WORD0_FILES_TOTAL
,
239 FATTR4_WORD1_SPACE_AVAIL
240 | FATTR4_WORD1_SPACE_FREE
241 | FATTR4_WORD1_SPACE_TOTAL
244 const u32 nfs4_pathconf_bitmap
[3] = {
246 | FATTR4_WORD0_MAXNAME
,
250 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
251 | FATTR4_WORD0_MAXREAD
252 | FATTR4_WORD0_MAXWRITE
253 | FATTR4_WORD0_LEASE_TIME
,
254 FATTR4_WORD1_TIME_DELTA
255 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
256 FATTR4_WORD2_LAYOUT_BLKSIZE
257 | FATTR4_WORD2_CLONE_BLKSIZE
260 const u32 nfs4_fs_locations_bitmap
[3] = {
264 | FATTR4_WORD0_FILEID
265 | FATTR4_WORD0_FS_LOCATIONS
,
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
273 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
276 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
277 struct nfs4_readdir_arg
*readdir
)
279 unsigned int attrs
= FATTR4_WORD0_FILEID
| FATTR4_WORD0_TYPE
;
283 readdir
->cookie
= cookie
;
284 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
289 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
300 start
= p
= kmap_atomic(*readdir
->pages
);
303 *p
++ = xdr_one
; /* next */
304 *p
++ = xdr_zero
; /* cookie, first word */
305 *p
++ = xdr_one
; /* cookie, second word */
306 *p
++ = xdr_one
; /* entry len */
307 memcpy(p
, ".\0\0\0", 4); /* entry */
309 *p
++ = xdr_one
; /* bitmap length */
310 *p
++ = htonl(attrs
); /* bitmap */
311 *p
++ = htonl(12); /* attribute buffer length */
312 *p
++ = htonl(NF4DIR
);
313 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
)));
316 *p
++ = xdr_one
; /* next */
317 *p
++ = xdr_zero
; /* cookie, first word */
318 *p
++ = xdr_two
; /* cookie, second word */
319 *p
++ = xdr_two
; /* entry len */
320 memcpy(p
, "..\0\0", 4); /* entry */
322 *p
++ = xdr_one
; /* bitmap length */
323 *p
++ = htonl(attrs
); /* bitmap */
324 *p
++ = htonl(12); /* attribute buffer length */
325 *p
++ = htonl(NF4DIR
);
326 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
328 readdir
->pgbase
= (char *)p
- (char *)start
;
329 readdir
->count
-= readdir
->pgbase
;
330 kunmap_atomic(start
);
333 static void nfs4_test_and_free_stateid(struct nfs_server
*server
,
334 nfs4_stateid
*stateid
,
335 struct rpc_cred
*cred
)
337 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
339 ops
->test_and_free_expired(server
, stateid
, cred
);
342 static void __nfs4_free_revoked_stateid(struct nfs_server
*server
,
343 nfs4_stateid
*stateid
,
344 struct rpc_cred
*cred
)
346 stateid
->type
= NFS4_REVOKED_STATEID_TYPE
;
347 nfs4_test_and_free_stateid(server
, stateid
, cred
);
350 static void nfs4_free_revoked_stateid(struct nfs_server
*server
,
351 const nfs4_stateid
*stateid
,
352 struct rpc_cred
*cred
)
356 nfs4_stateid_copy(&tmp
, stateid
);
357 __nfs4_free_revoked_stateid(server
, &tmp
, cred
);
360 static long nfs4_update_delay(long *timeout
)
364 return NFS4_POLL_RETRY_MAX
;
366 *timeout
= NFS4_POLL_RETRY_MIN
;
367 if (*timeout
> NFS4_POLL_RETRY_MAX
)
368 *timeout
= NFS4_POLL_RETRY_MAX
;
374 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
380 freezable_schedule_timeout_killable_unsafe(
381 nfs4_update_delay(timeout
));
382 if (fatal_signal_pending(current
))
387 /* This is the error handling routine for processes that are allowed
390 static int nfs4_do_handle_exception(struct nfs_server
*server
,
391 int errorcode
, struct nfs4_exception
*exception
)
393 struct nfs_client
*clp
= server
->nfs_client
;
394 struct nfs4_state
*state
= exception
->state
;
395 const nfs4_stateid
*stateid
= exception
->stateid
;
396 struct inode
*inode
= exception
->inode
;
399 exception
->delay
= 0;
400 exception
->recovering
= 0;
401 exception
->retry
= 0;
403 if (stateid
== NULL
&& state
!= NULL
)
404 stateid
= &state
->stateid
;
409 case -NFS4ERR_DELEG_REVOKED
:
410 case -NFS4ERR_ADMIN_REVOKED
:
411 case -NFS4ERR_EXPIRED
:
412 case -NFS4ERR_BAD_STATEID
:
413 if (inode
!= NULL
&& stateid
!= NULL
) {
414 nfs_inode_find_state_and_recover(inode
,
416 goto wait_on_recovery
;
418 case -NFS4ERR_OPENMODE
:
422 err
= nfs_async_inode_return_delegation(inode
,
425 goto wait_on_recovery
;
426 if (stateid
!= NULL
&& stateid
->type
== NFS4_DELEGATION_STATEID_TYPE
) {
427 exception
->retry
= 1;
433 ret
= nfs4_schedule_stateid_recovery(server
, state
);
436 goto wait_on_recovery
;
437 case -NFS4ERR_STALE_STATEID
:
438 case -NFS4ERR_STALE_CLIENTID
:
439 nfs4_schedule_lease_recovery(clp
);
440 goto wait_on_recovery
;
442 ret
= nfs4_schedule_migration_recovery(server
);
445 goto wait_on_recovery
;
446 case -NFS4ERR_LEASE_MOVED
:
447 nfs4_schedule_lease_moved_recovery(clp
);
448 goto wait_on_recovery
;
449 #if defined(CONFIG_NFS_V4_1)
450 case -NFS4ERR_BADSESSION
:
451 case -NFS4ERR_BADSLOT
:
452 case -NFS4ERR_BAD_HIGH_SLOT
:
453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
454 case -NFS4ERR_DEADSESSION
:
455 case -NFS4ERR_SEQ_FALSE_RETRY
:
456 case -NFS4ERR_SEQ_MISORDERED
:
457 dprintk("%s ERROR: %d Reset session\n", __func__
,
459 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
460 goto wait_on_recovery
;
461 #endif /* defined(CONFIG_NFS_V4_1) */
462 case -NFS4ERR_FILE_OPEN
:
463 if (exception
->timeout
> HZ
) {
464 /* We have retried a decent amount, time to
471 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
473 case -NFS4ERR_LAYOUTTRYLATER
:
474 case -NFS4ERR_RECALLCONFLICT
:
475 exception
->delay
= 1;
478 case -NFS4ERR_RETRY_UNCACHED_REP
:
479 case -NFS4ERR_OLD_STATEID
:
480 exception
->retry
= 1;
482 case -NFS4ERR_BADOWNER
:
483 /* The following works around a Linux server bug! */
484 case -NFS4ERR_BADNAME
:
485 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
486 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
487 exception
->retry
= 1;
488 printk(KERN_WARNING
"NFS: v4 server %s "
489 "does not accept raw "
491 "Reenabling the idmapper.\n",
492 server
->nfs_client
->cl_hostname
);
495 /* We failed to handle the error */
496 return nfs4_map_errors(ret
);
498 exception
->recovering
= 1;
502 /* This is the error handling routine for processes that are allowed
505 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
507 struct nfs_client
*clp
= server
->nfs_client
;
510 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
511 if (exception
->delay
) {
512 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
515 if (exception
->recovering
) {
516 ret
= nfs4_wait_clnt_recover(clp
);
517 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
524 exception
->retry
= 1;
529 nfs4_async_handle_exception(struct rpc_task
*task
, struct nfs_server
*server
,
530 int errorcode
, struct nfs4_exception
*exception
)
532 struct nfs_client
*clp
= server
->nfs_client
;
535 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
536 if (exception
->delay
) {
537 rpc_delay(task
, nfs4_update_delay(&exception
->timeout
));
540 if (exception
->recovering
) {
541 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
542 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
543 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
546 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
551 exception
->retry
= 1;
553 * For NFS4ERR_MOVED, the client transport will need to
554 * be recomputed after migration recovery has completed.
556 if (errorcode
== -NFS4ERR_MOVED
)
557 rpc_task_release_transport(task
);
563 nfs4_async_handle_error(struct rpc_task
*task
, struct nfs_server
*server
,
564 struct nfs4_state
*state
, long *timeout
)
566 struct nfs4_exception exception
= {
570 if (task
->tk_status
>= 0)
573 exception
.timeout
= *timeout
;
574 task
->tk_status
= nfs4_async_handle_exception(task
, server
,
577 if (exception
.delay
&& timeout
)
578 *timeout
= exception
.timeout
;
585 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
586 * or 'false' otherwise.
588 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
590 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
591 return (flavor
== RPC_AUTH_GSS_KRB5I
) || (flavor
== RPC_AUTH_GSS_KRB5P
);
594 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
596 spin_lock(&clp
->cl_lock
);
597 if (time_before(clp
->cl_last_renewal
,timestamp
))
598 clp
->cl_last_renewal
= timestamp
;
599 spin_unlock(&clp
->cl_lock
);
602 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
604 struct nfs_client
*clp
= server
->nfs_client
;
606 if (!nfs4_has_session(clp
))
607 do_renew_lease(clp
, timestamp
);
610 struct nfs4_call_sync_data
{
611 const struct nfs_server
*seq_server
;
612 struct nfs4_sequence_args
*seq_args
;
613 struct nfs4_sequence_res
*seq_res
;
616 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
617 struct nfs4_sequence_res
*res
, int cache_reply
)
619 args
->sa_slot
= NULL
;
620 args
->sa_cache_this
= cache_reply
;
621 args
->sa_privileged
= 0;
626 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
628 args
->sa_privileged
= 1;
631 static void nfs40_sequence_free_slot(struct nfs4_sequence_res
*res
)
633 struct nfs4_slot
*slot
= res
->sr_slot
;
634 struct nfs4_slot_table
*tbl
;
637 spin_lock(&tbl
->slot_tbl_lock
);
638 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
639 nfs4_free_slot(tbl
, slot
);
640 spin_unlock(&tbl
->slot_tbl_lock
);
645 static int nfs40_sequence_done(struct rpc_task
*task
,
646 struct nfs4_sequence_res
*res
)
648 if (res
->sr_slot
!= NULL
)
649 nfs40_sequence_free_slot(res
);
653 #if defined(CONFIG_NFS_V4_1)
655 static void nfs41_release_slot(struct nfs4_slot
*slot
)
657 struct nfs4_session
*session
;
658 struct nfs4_slot_table
*tbl
;
659 bool send_new_highest_used_slotid
= false;
664 session
= tbl
->session
;
666 /* Bump the slot sequence number */
671 spin_lock(&tbl
->slot_tbl_lock
);
672 /* Be nice to the server: try to ensure that the last transmitted
673 * value for highest_user_slotid <= target_highest_slotid
675 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
676 send_new_highest_used_slotid
= true;
678 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
679 send_new_highest_used_slotid
= false;
682 nfs4_free_slot(tbl
, slot
);
684 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
685 send_new_highest_used_slotid
= false;
687 spin_unlock(&tbl
->slot_tbl_lock
);
688 if (send_new_highest_used_slotid
)
689 nfs41_notify_server(session
->clp
);
690 if (waitqueue_active(&tbl
->slot_waitq
))
691 wake_up_all(&tbl
->slot_waitq
);
694 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
696 nfs41_release_slot(res
->sr_slot
);
700 static int nfs41_sequence_process(struct rpc_task
*task
,
701 struct nfs4_sequence_res
*res
)
703 struct nfs4_session
*session
;
704 struct nfs4_slot
*slot
= res
->sr_slot
;
705 struct nfs_client
*clp
;
706 bool interrupted
= false;
711 /* don't increment the sequence number if the task wasn't sent */
712 if (!RPC_WAS_SENT(task
))
715 session
= slot
->table
->session
;
717 if (slot
->interrupted
) {
718 if (res
->sr_status
!= -NFS4ERR_DELAY
)
719 slot
->interrupted
= 0;
723 trace_nfs4_sequence_done(session
, res
);
724 /* Check the SEQUENCE operation status */
725 switch (res
->sr_status
) {
727 /* Update the slot's sequence and clientid lease timer */
730 do_renew_lease(clp
, res
->sr_timestamp
);
731 /* Check sequence flags */
732 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
,
734 nfs41_update_target_slotid(slot
->table
, slot
, res
);
738 * sr_status remains 1 if an RPC level error occurred.
739 * The server may or may not have processed the sequence
741 * Mark the slot as having hosted an interrupted RPC call.
743 slot
->interrupted
= 1;
746 /* The server detected a resend of the RPC call and
747 * returned NFS4ERR_DELAY as per Section 2.10.6.2
750 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
755 case -NFS4ERR_RETRY_UNCACHED_REP
:
756 case -NFS4ERR_SEQ_FALSE_RETRY
:
758 * The server thinks we tried to replay a request.
759 * Retry the call after bumping the sequence ID.
762 case -NFS4ERR_BADSLOT
:
764 * The slot id we used was probably retired. Try again
765 * using a different slot id.
767 if (slot
->slot_nr
< slot
->table
->target_highest_slotid
)
768 goto session_recover
;
770 case -NFS4ERR_SEQ_MISORDERED
:
772 * Was the last operation on this sequence interrupted?
773 * If so, retry after bumping the sequence number.
778 * Could this slot have been previously retired?
779 * If so, then the server may be expecting seq_nr = 1!
781 if (slot
->seq_nr
!= 1) {
785 goto session_recover
;
787 /* Just update the slot sequence no. */
791 /* The session may be reset by one of the error handlers. */
792 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
796 nfs4_schedule_session_recovery(session
, res
->sr_status
);
801 if (rpc_restart_call_prepare(task
)) {
802 nfs41_sequence_free_slot(res
);
808 if (!rpc_restart_call(task
))
810 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
814 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
816 if (!nfs41_sequence_process(task
, res
))
818 if (res
->sr_slot
!= NULL
)
819 nfs41_sequence_free_slot(res
);
823 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
825 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
827 if (res
->sr_slot
== NULL
)
829 if (res
->sr_slot
->table
->session
!= NULL
)
830 return nfs41_sequence_process(task
, res
);
831 return nfs40_sequence_done(task
, res
);
834 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
836 if (res
->sr_slot
!= NULL
) {
837 if (res
->sr_slot
->table
->session
!= NULL
)
838 nfs41_sequence_free_slot(res
);
840 nfs40_sequence_free_slot(res
);
844 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
846 if (res
->sr_slot
== NULL
)
848 if (!res
->sr_slot
->table
->session
)
849 return nfs40_sequence_done(task
, res
);
850 return nfs41_sequence_done(task
, res
);
852 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
854 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
856 struct nfs4_call_sync_data
*data
= calldata
;
858 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
860 nfs4_setup_sequence(data
->seq_server
->nfs_client
,
861 data
->seq_args
, data
->seq_res
, task
);
864 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
866 struct nfs4_call_sync_data
*data
= calldata
;
868 nfs41_sequence_done(task
, data
->seq_res
);
871 static const struct rpc_call_ops nfs41_call_sync_ops
= {
872 .rpc_call_prepare
= nfs41_call_sync_prepare
,
873 .rpc_call_done
= nfs41_call_sync_done
,
877 nfs4_sequence_process_interrupted(struct nfs_client
*client
,
878 struct nfs4_slot
*slot
, struct rpc_cred
*cred
)
880 struct rpc_task
*task
;
882 task
= _nfs41_proc_sequence(client
, cred
, slot
, true);
884 rpc_put_task_async(task
);
887 #else /* !CONFIG_NFS_V4_1 */
889 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
891 return nfs40_sequence_done(task
, res
);
894 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
896 if (res
->sr_slot
!= NULL
)
897 nfs40_sequence_free_slot(res
);
900 int nfs4_sequence_done(struct rpc_task
*task
,
901 struct nfs4_sequence_res
*res
)
903 return nfs40_sequence_done(task
, res
);
905 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
908 nfs4_sequence_process_interrupted(struct nfs_client
*client
,
909 struct nfs4_slot
*slot
, struct rpc_cred
*cred
)
912 slot
->interrupted
= 0;
915 #endif /* !CONFIG_NFS_V4_1 */
917 static void nfs41_sequence_res_init(struct nfs4_sequence_res
*res
)
919 res
->sr_timestamp
= jiffies
;
920 res
->sr_status_flags
= 0;
925 void nfs4_sequence_attach_slot(struct nfs4_sequence_args
*args
,
926 struct nfs4_sequence_res
*res
,
927 struct nfs4_slot
*slot
)
931 slot
->privileged
= args
->sa_privileged
? 1 : 0;
932 args
->sa_slot
= slot
;
937 int nfs4_setup_sequence(struct nfs_client
*client
,
938 struct nfs4_sequence_args
*args
,
939 struct nfs4_sequence_res
*res
,
940 struct rpc_task
*task
)
942 struct nfs4_session
*session
= nfs4_get_session(client
);
943 struct nfs4_slot_table
*tbl
= client
->cl_slot_tbl
;
944 struct nfs4_slot
*slot
;
946 /* slot already allocated? */
947 if (res
->sr_slot
!= NULL
)
951 tbl
= &session
->fc_slot_table
;
952 task
->tk_timeout
= 0;
956 spin_lock(&tbl
->slot_tbl_lock
);
957 /* The state manager will wait until the slot table is empty */
958 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
961 slot
= nfs4_alloc_slot(tbl
);
963 /* Try again in 1/4 second */
964 if (slot
== ERR_PTR(-ENOMEM
))
965 task
->tk_timeout
= HZ
>> 2;
968 spin_unlock(&tbl
->slot_tbl_lock
);
970 if (likely(!slot
->interrupted
))
972 nfs4_sequence_process_interrupted(client
,
973 slot
, task
->tk_msg
.rpc_cred
);
976 nfs4_sequence_attach_slot(args
, res
, slot
);
978 trace_nfs4_setup_sequence(session
, args
);
980 nfs41_sequence_res_init(res
);
981 rpc_call_start(task
);
985 if (args
->sa_privileged
)
986 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
987 NULL
, RPC_PRIORITY_PRIVILEGED
);
989 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
990 spin_unlock(&tbl
->slot_tbl_lock
);
993 EXPORT_SYMBOL_GPL(nfs4_setup_sequence
);
995 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
997 struct nfs4_call_sync_data
*data
= calldata
;
998 nfs4_setup_sequence(data
->seq_server
->nfs_client
,
999 data
->seq_args
, data
->seq_res
, task
);
1002 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
1004 struct nfs4_call_sync_data
*data
= calldata
;
1005 nfs4_sequence_done(task
, data
->seq_res
);
1008 static const struct rpc_call_ops nfs40_call_sync_ops
= {
1009 .rpc_call_prepare
= nfs40_call_sync_prepare
,
1010 .rpc_call_done
= nfs40_call_sync_done
,
1013 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
1014 struct nfs_server
*server
,
1015 struct rpc_message
*msg
,
1016 struct nfs4_sequence_args
*args
,
1017 struct nfs4_sequence_res
*res
)
1020 struct rpc_task
*task
;
1021 struct nfs_client
*clp
= server
->nfs_client
;
1022 struct nfs4_call_sync_data data
= {
1023 .seq_server
= server
,
1027 struct rpc_task_setup task_setup
= {
1030 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
1031 .callback_data
= &data
1034 task
= rpc_run_task(&task_setup
);
1036 ret
= PTR_ERR(task
);
1038 ret
= task
->tk_status
;
1044 int nfs4_call_sync(struct rpc_clnt
*clnt
,
1045 struct nfs_server
*server
,
1046 struct rpc_message
*msg
,
1047 struct nfs4_sequence_args
*args
,
1048 struct nfs4_sequence_res
*res
,
1051 nfs4_init_sequence(args
, res
, cache_reply
);
1052 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
1055 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
,
1056 unsigned long timestamp
)
1058 struct nfs_inode
*nfsi
= NFS_I(dir
);
1060 spin_lock(&dir
->i_lock
);
1061 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1062 if (cinfo
->atomic
&& cinfo
->before
== dir
->i_version
) {
1063 nfsi
->cache_validity
&= ~NFS_INO_REVAL_PAGECACHE
;
1064 nfsi
->attrtimeo_timestamp
= jiffies
;
1066 nfs_force_lookup_revalidate(dir
);
1067 if (cinfo
->before
!= dir
->i_version
)
1068 nfsi
->cache_validity
|= NFS_INO_INVALID_ACCESS
|
1069 NFS_INO_INVALID_ACL
;
1071 dir
->i_version
= cinfo
->after
;
1072 nfsi
->read_cache_jiffies
= timestamp
;
1073 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
1074 nfs_fscache_invalidate(dir
);
1075 spin_unlock(&dir
->i_lock
);
1078 struct nfs4_opendata
{
1080 struct nfs_openargs o_arg
;
1081 struct nfs_openres o_res
;
1082 struct nfs_open_confirmargs c_arg
;
1083 struct nfs_open_confirmres c_res
;
1084 struct nfs4_string owner_name
;
1085 struct nfs4_string group_name
;
1086 struct nfs4_label
*a_label
;
1087 struct nfs_fattr f_attr
;
1088 struct nfs4_label
*f_label
;
1090 struct dentry
*dentry
;
1091 struct nfs4_state_owner
*owner
;
1092 struct nfs4_state
*state
;
1094 unsigned long timestamp
;
1102 struct nfs4_open_createattrs
{
1103 struct nfs4_label
*label
;
1104 struct iattr
*sattr
;
1105 const __u32 verf
[2];
1108 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
1109 int err
, struct nfs4_exception
*exception
)
1113 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1115 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
1116 exception
->retry
= 1;
1121 nfs4_map_atomic_open_share(struct nfs_server
*server
,
1122 fmode_t fmode
, int openflags
)
1126 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
1128 res
= NFS4_SHARE_ACCESS_READ
;
1131 res
= NFS4_SHARE_ACCESS_WRITE
;
1133 case FMODE_READ
|FMODE_WRITE
:
1134 res
= NFS4_SHARE_ACCESS_BOTH
;
1136 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1138 /* Want no delegation if we're using O_DIRECT */
1139 if (openflags
& O_DIRECT
)
1140 res
|= NFS4_SHARE_WANT_NO_DELEG
;
1145 static enum open_claim_type4
1146 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
1147 enum open_claim_type4 claim
)
1149 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
1154 case NFS4_OPEN_CLAIM_FH
:
1155 return NFS4_OPEN_CLAIM_NULL
;
1156 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1157 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1158 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1159 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
1163 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
1165 p
->o_res
.f_attr
= &p
->f_attr
;
1166 p
->o_res
.f_label
= p
->f_label
;
1167 p
->o_res
.seqid
= p
->o_arg
.seqid
;
1168 p
->c_res
.seqid
= p
->c_arg
.seqid
;
1169 p
->o_res
.server
= p
->o_arg
.server
;
1170 p
->o_res
.access_request
= p
->o_arg
.access
;
1171 nfs_fattr_init(&p
->f_attr
);
1172 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
1175 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
1176 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
1177 const struct nfs4_open_createattrs
*c
,
1178 enum open_claim_type4 claim
,
1181 struct dentry
*parent
= dget_parent(dentry
);
1182 struct inode
*dir
= d_inode(parent
);
1183 struct nfs_server
*server
= NFS_SERVER(dir
);
1184 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1185 struct nfs4_label
*label
= (c
!= NULL
) ? c
->label
: NULL
;
1186 struct nfs4_opendata
*p
;
1188 p
= kzalloc(sizeof(*p
), gfp_mask
);
1192 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1193 if (IS_ERR(p
->f_label
))
1196 p
->a_label
= nfs4_label_alloc(server
, gfp_mask
);
1197 if (IS_ERR(p
->a_label
))
1200 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1201 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1202 if (IS_ERR(p
->o_arg
.seqid
))
1203 goto err_free_label
;
1204 nfs_sb_active(dentry
->d_sb
);
1205 p
->dentry
= dget(dentry
);
1208 atomic_inc(&sp
->so_count
);
1209 p
->o_arg
.open_flags
= flags
;
1210 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1211 p
->o_arg
.umask
= current_umask();
1212 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1213 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1215 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1216 * will return permission denied for all bits until close */
1217 if (!(flags
& O_EXCL
)) {
1218 /* ask server to check for all possible rights as results
1220 switch (p
->o_arg
.claim
) {
1223 case NFS4_OPEN_CLAIM_NULL
:
1224 case NFS4_OPEN_CLAIM_FH
:
1225 p
->o_arg
.access
= NFS4_ACCESS_READ
|
1226 NFS4_ACCESS_MODIFY
|
1227 NFS4_ACCESS_EXTEND
|
1228 NFS4_ACCESS_EXECUTE
;
1231 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1232 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1233 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1234 p
->o_arg
.name
= &dentry
->d_name
;
1235 p
->o_arg
.server
= server
;
1236 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1237 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1238 p
->o_arg
.label
= nfs4_label_copy(p
->a_label
, label
);
1239 switch (p
->o_arg
.claim
) {
1240 case NFS4_OPEN_CLAIM_NULL
:
1241 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1242 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1243 p
->o_arg
.fh
= NFS_FH(dir
);
1245 case NFS4_OPEN_CLAIM_PREVIOUS
:
1246 case NFS4_OPEN_CLAIM_FH
:
1247 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1248 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1249 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1251 if (c
!= NULL
&& c
->sattr
!= NULL
&& c
->sattr
->ia_valid
!= 0) {
1252 p
->o_arg
.u
.attrs
= &p
->attrs
;
1253 memcpy(&p
->attrs
, c
->sattr
, sizeof(p
->attrs
));
1255 memcpy(p
->o_arg
.u
.verifier
.data
, c
->verf
,
1256 sizeof(p
->o_arg
.u
.verifier
.data
));
1258 p
->c_arg
.fh
= &p
->o_res
.fh
;
1259 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1260 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1261 nfs4_init_opendata_res(p
);
1262 kref_init(&p
->kref
);
1266 nfs4_label_free(p
->a_label
);
1268 nfs4_label_free(p
->f_label
);
1276 static void nfs4_opendata_free(struct kref
*kref
)
1278 struct nfs4_opendata
*p
= container_of(kref
,
1279 struct nfs4_opendata
, kref
);
1280 struct super_block
*sb
= p
->dentry
->d_sb
;
1282 nfs_free_seqid(p
->o_arg
.seqid
);
1283 nfs4_sequence_free_slot(&p
->o_res
.seq_res
);
1284 if (p
->state
!= NULL
)
1285 nfs4_put_open_state(p
->state
);
1286 nfs4_put_state_owner(p
->owner
);
1288 nfs4_label_free(p
->a_label
);
1289 nfs4_label_free(p
->f_label
);
1293 nfs_sb_deactive(sb
);
1294 nfs_fattr_free_names(&p
->f_attr
);
1295 kfree(p
->f_attr
.mdsthreshold
);
1299 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1302 kref_put(&p
->kref
, nfs4_opendata_free
);
1305 static bool nfs4_mode_match_open_stateid(struct nfs4_state
*state
,
1308 switch(fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1309 case FMODE_READ
|FMODE_WRITE
:
1310 return state
->n_rdwr
!= 0;
1312 return state
->n_wronly
!= 0;
1314 return state
->n_rdonly
!= 0;
1320 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
,
1321 int open_mode
, enum open_claim_type4 claim
)
1325 if (open_mode
& (O_EXCL
|O_TRUNC
))
1328 case NFS4_OPEN_CLAIM_NULL
:
1329 case NFS4_OPEN_CLAIM_FH
:
1334 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1336 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1337 && state
->n_rdonly
!= 0;
1340 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1341 && state
->n_wronly
!= 0;
1343 case FMODE_READ
|FMODE_WRITE
:
1344 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1345 && state
->n_rdwr
!= 0;
1351 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
,
1352 enum open_claim_type4 claim
)
1354 if (delegation
== NULL
)
1356 if ((delegation
->type
& fmode
) != fmode
)
1359 case NFS4_OPEN_CLAIM_NULL
:
1360 case NFS4_OPEN_CLAIM_FH
:
1362 case NFS4_OPEN_CLAIM_PREVIOUS
:
1363 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1368 nfs_mark_delegation_referenced(delegation
);
1372 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1381 case FMODE_READ
|FMODE_WRITE
:
1384 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1387 #ifdef CONFIG_NFS_V4_1
1388 static bool nfs_open_stateid_recover_openmode(struct nfs4_state
*state
)
1390 if (state
->n_rdonly
&& !test_bit(NFS_O_RDONLY_STATE
, &state
->flags
))
1392 if (state
->n_wronly
&& !test_bit(NFS_O_WRONLY_STATE
, &state
->flags
))
1394 if (state
->n_rdwr
&& !test_bit(NFS_O_RDWR_STATE
, &state
->flags
))
1398 #endif /* CONFIG_NFS_V4_1 */
1400 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1402 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1403 bool need_recover
= false;
1405 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1406 need_recover
= true;
1407 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1408 need_recover
= true;
1409 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1410 need_recover
= true;
1412 nfs4_state_mark_reclaim_nograce(clp
, state
);
1415 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1416 const nfs4_stateid
*stateid
, nfs4_stateid
*freeme
)
1418 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1420 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1421 nfs4_stateid_copy(freeme
, &state
->open_stateid
);
1422 nfs_test_and_clear_all_open_stateid(state
);
1425 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1430 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1432 if (!(state
->n_wronly
|| state
->n_rdonly
|| state
->n_rdwr
))
1434 if (state
->n_wronly
)
1435 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1436 if (state
->n_rdonly
)
1437 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1439 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1440 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1443 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1444 nfs4_stateid
*stateid
, fmode_t fmode
)
1446 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1447 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1449 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1452 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1455 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1456 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1457 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1459 if (stateid
== NULL
)
1461 /* Handle OPEN+OPEN_DOWNGRADE races */
1462 if (nfs4_stateid_match_other(stateid
, &state
->open_stateid
) &&
1463 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
)) {
1464 nfs_resync_open_stateid_locked(state
);
1467 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1468 nfs4_stateid_copy(&state
->stateid
, stateid
);
1469 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1472 static void nfs_clear_open_stateid(struct nfs4_state
*state
,
1473 nfs4_stateid
*arg_stateid
,
1474 nfs4_stateid
*stateid
, fmode_t fmode
)
1476 write_seqlock(&state
->seqlock
);
1477 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1478 if (nfs4_state_match_open_stateid_other(state
, arg_stateid
))
1479 nfs_clear_open_stateid_locked(state
, stateid
, fmode
);
1480 write_sequnlock(&state
->seqlock
);
1481 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1482 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1485 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
,
1486 const nfs4_stateid
*stateid
, fmode_t fmode
,
1487 nfs4_stateid
*freeme
)
1491 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1494 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1496 case FMODE_READ
|FMODE_WRITE
:
1497 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1499 if (!nfs_need_update_open_stateid(state
, stateid
, freeme
))
1501 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1502 nfs4_stateid_copy(&state
->stateid
, stateid
);
1503 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1506 static void __update_open_stateid(struct nfs4_state
*state
,
1507 const nfs4_stateid
*open_stateid
,
1508 const nfs4_stateid
*deleg_stateid
,
1510 nfs4_stateid
*freeme
)
1513 * Protect the call to nfs4_state_set_mode_locked and
1514 * serialise the stateid update
1516 spin_lock(&state
->owner
->so_lock
);
1517 write_seqlock(&state
->seqlock
);
1518 if (deleg_stateid
!= NULL
) {
1519 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1520 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1522 if (open_stateid
!= NULL
)
1523 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
, freeme
);
1524 write_sequnlock(&state
->seqlock
);
1525 update_open_stateflags(state
, fmode
);
1526 spin_unlock(&state
->owner
->so_lock
);
1529 static int update_open_stateid(struct nfs4_state
*state
,
1530 const nfs4_stateid
*open_stateid
,
1531 const nfs4_stateid
*delegation
,
1534 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1535 struct nfs_client
*clp
= server
->nfs_client
;
1536 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1537 struct nfs_delegation
*deleg_cur
;
1538 nfs4_stateid freeme
= { };
1541 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1544 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1545 if (deleg_cur
== NULL
)
1548 spin_lock(&deleg_cur
->lock
);
1549 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1550 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1551 (deleg_cur
->type
& fmode
) != fmode
)
1552 goto no_delegation_unlock
;
1554 if (delegation
== NULL
)
1555 delegation
= &deleg_cur
->stateid
;
1556 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1557 goto no_delegation_unlock
;
1559 nfs_mark_delegation_referenced(deleg_cur
);
1560 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
,
1563 no_delegation_unlock
:
1564 spin_unlock(&deleg_cur
->lock
);
1568 if (!ret
&& open_stateid
!= NULL
) {
1569 __update_open_stateid(state
, open_stateid
, NULL
, fmode
, &freeme
);
1572 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1573 nfs4_schedule_state_manager(clp
);
1574 if (freeme
.type
!= 0)
1575 nfs4_test_and_free_stateid(server
, &freeme
,
1576 state
->owner
->so_cred
);
1581 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1582 const nfs4_stateid
*stateid
)
1584 struct nfs4_state
*state
= lsp
->ls_state
;
1587 spin_lock(&state
->state_lock
);
1588 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1590 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1592 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1595 spin_unlock(&state
->state_lock
);
1599 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1601 struct nfs_delegation
*delegation
;
1604 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1605 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1610 nfs4_inode_return_delegation(inode
);
1613 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1615 struct nfs4_state
*state
= opendata
->state
;
1616 struct nfs_delegation
*delegation
;
1617 int open_mode
= opendata
->o_arg
.open_flags
;
1618 fmode_t fmode
= opendata
->o_arg
.fmode
;
1619 enum open_claim_type4 claim
= opendata
->o_arg
.claim
;
1620 nfs4_stateid stateid
;
1624 spin_lock(&state
->owner
->so_lock
);
1625 if (can_open_cached(state
, fmode
, open_mode
, claim
)) {
1626 update_open_stateflags(state
, fmode
);
1627 spin_unlock(&state
->owner
->so_lock
);
1628 goto out_return_state
;
1630 spin_unlock(&state
->owner
->so_lock
);
1632 delegation
= nfs4_get_valid_delegation(state
->inode
);
1633 if (!can_open_delegated(delegation
, fmode
, claim
)) {
1637 /* Save the delegation */
1638 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1640 nfs_release_seqid(opendata
->o_arg
.seqid
);
1641 if (!opendata
->is_recover
) {
1642 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1648 /* Try to update the stateid using the delegation */
1649 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1650 goto out_return_state
;
1653 return ERR_PTR(ret
);
1655 atomic_inc(&state
->count
);
1660 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1662 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1663 struct nfs_delegation
*delegation
;
1664 int delegation_flags
= 0;
1667 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1669 delegation_flags
= delegation
->flags
;
1671 switch (data
->o_arg
.claim
) {
1674 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1675 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1676 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1677 "returning a delegation for "
1678 "OPEN(CLAIM_DELEGATE_CUR)\n",
1682 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1683 nfs_inode_set_delegation(state
->inode
,
1684 data
->owner
->so_cred
,
1687 nfs_inode_reclaim_delegation(state
->inode
,
1688 data
->owner
->so_cred
,
1693 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1694 * and update the nfs4_state.
1696 static struct nfs4_state
*
1697 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1699 struct inode
*inode
= data
->state
->inode
;
1700 struct nfs4_state
*state
= data
->state
;
1703 if (!data
->rpc_done
) {
1704 if (data
->rpc_status
)
1705 return ERR_PTR(data
->rpc_status
);
1706 /* cached opens have already been processed */
1710 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1712 return ERR_PTR(ret
);
1714 if (data
->o_res
.delegation_type
!= 0)
1715 nfs4_opendata_check_deleg(data
, state
);
1717 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1719 atomic_inc(&state
->count
);
1724 static struct inode
*
1725 nfs4_opendata_get_inode(struct nfs4_opendata
*data
)
1727 struct inode
*inode
;
1729 switch (data
->o_arg
.claim
) {
1730 case NFS4_OPEN_CLAIM_NULL
:
1731 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1732 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1733 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1734 return ERR_PTR(-EAGAIN
);
1735 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
,
1736 &data
->f_attr
, data
->f_label
);
1739 inode
= d_inode(data
->dentry
);
1741 nfs_refresh_inode(inode
, &data
->f_attr
);
1746 static struct nfs4_state
*
1747 nfs4_opendata_find_nfs4_state(struct nfs4_opendata
*data
)
1749 struct nfs4_state
*state
;
1750 struct inode
*inode
;
1752 inode
= nfs4_opendata_get_inode(data
);
1754 return ERR_CAST(inode
);
1755 if (data
->state
!= NULL
&& data
->state
->inode
== inode
) {
1756 state
= data
->state
;
1757 atomic_inc(&state
->count
);
1759 state
= nfs4_get_open_state(inode
, data
->owner
);
1762 state
= ERR_PTR(-ENOMEM
);
1766 static struct nfs4_state
*
1767 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1769 struct nfs4_state
*state
;
1771 if (!data
->rpc_done
) {
1772 state
= nfs4_try_open_cached(data
);
1773 trace_nfs4_cached_open(data
->state
);
1777 state
= nfs4_opendata_find_nfs4_state(data
);
1781 if (data
->o_res
.delegation_type
!= 0)
1782 nfs4_opendata_check_deleg(data
, state
);
1783 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1786 nfs_release_seqid(data
->o_arg
.seqid
);
1790 static struct nfs4_state
*
1791 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1793 struct nfs4_state
*ret
;
1795 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1796 ret
=_nfs4_opendata_reclaim_to_nfs4_state(data
);
1798 ret
= _nfs4_opendata_to_nfs4_state(data
);
1799 nfs4_sequence_free_slot(&data
->o_res
.seq_res
);
1803 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1805 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1806 struct nfs_open_context
*ctx
;
1808 spin_lock(&state
->inode
->i_lock
);
1809 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1810 if (ctx
->state
!= state
)
1812 get_nfs_open_context(ctx
);
1813 spin_unlock(&state
->inode
->i_lock
);
1816 spin_unlock(&state
->inode
->i_lock
);
1817 return ERR_PTR(-ENOENT
);
1820 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1821 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1823 struct nfs4_opendata
*opendata
;
1825 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1826 NULL
, claim
, GFP_NOFS
);
1827 if (opendata
== NULL
)
1828 return ERR_PTR(-ENOMEM
);
1829 opendata
->state
= state
;
1830 atomic_inc(&state
->count
);
1834 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
,
1837 struct nfs4_state
*newstate
;
1840 if (!nfs4_mode_match_open_stateid(opendata
->state
, fmode
))
1842 opendata
->o_arg
.open_flags
= 0;
1843 opendata
->o_arg
.fmode
= fmode
;
1844 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1845 NFS_SB(opendata
->dentry
->d_sb
),
1847 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1848 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1849 nfs4_init_opendata_res(opendata
);
1850 ret
= _nfs4_recover_proc_open(opendata
);
1853 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1854 if (IS_ERR(newstate
))
1855 return PTR_ERR(newstate
);
1856 if (newstate
!= opendata
->state
)
1858 nfs4_close_state(newstate
, fmode
);
1862 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1866 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1867 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1868 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1869 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1870 /* memory barrier prior to reading state->n_* */
1871 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1872 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1874 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1877 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1880 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1884 * We may have performed cached opens for all three recoveries.
1885 * Check if we need to update the current stateid.
1887 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1888 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1889 write_seqlock(&state
->seqlock
);
1890 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1891 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1892 write_sequnlock(&state
->seqlock
);
1899 * reclaim state on the server after a reboot.
1901 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1903 struct nfs_delegation
*delegation
;
1904 struct nfs4_opendata
*opendata
;
1905 fmode_t delegation_type
= 0;
1908 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1909 NFS4_OPEN_CLAIM_PREVIOUS
);
1910 if (IS_ERR(opendata
))
1911 return PTR_ERR(opendata
);
1913 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1914 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1915 delegation_type
= delegation
->type
;
1917 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1918 status
= nfs4_open_recover(opendata
, state
);
1919 nfs4_opendata_put(opendata
);
1923 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1925 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1926 struct nfs4_exception exception
= { };
1929 err
= _nfs4_do_open_reclaim(ctx
, state
);
1930 trace_nfs4_open_reclaim(ctx
, 0, err
);
1931 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1933 if (err
!= -NFS4ERR_DELAY
)
1935 nfs4_handle_exception(server
, err
, &exception
);
1936 } while (exception
.retry
);
1940 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1942 struct nfs_open_context
*ctx
;
1945 ctx
= nfs4_state_find_open_context(state
);
1948 ret
= nfs4_do_open_reclaim(ctx
, state
);
1949 put_nfs_open_context(ctx
);
1953 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, struct file_lock
*fl
, int err
)
1957 printk(KERN_ERR
"NFS: %s: unhandled error "
1958 "%d.\n", __func__
, err
);
1964 case -NFS4ERR_BADSESSION
:
1965 case -NFS4ERR_BADSLOT
:
1966 case -NFS4ERR_BAD_HIGH_SLOT
:
1967 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1968 case -NFS4ERR_DEADSESSION
:
1969 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1970 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1972 case -NFS4ERR_STALE_CLIENTID
:
1973 case -NFS4ERR_STALE_STATEID
:
1974 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1975 /* Don't recall a delegation if it was lost */
1976 nfs4_schedule_lease_recovery(server
->nfs_client
);
1978 case -NFS4ERR_MOVED
:
1979 nfs4_schedule_migration_recovery(server
);
1981 case -NFS4ERR_LEASE_MOVED
:
1982 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1984 case -NFS4ERR_DELEG_REVOKED
:
1985 case -NFS4ERR_ADMIN_REVOKED
:
1986 case -NFS4ERR_EXPIRED
:
1987 case -NFS4ERR_BAD_STATEID
:
1988 case -NFS4ERR_OPENMODE
:
1989 nfs_inode_find_state_and_recover(state
->inode
,
1991 nfs4_schedule_stateid_recovery(server
, state
);
1993 case -NFS4ERR_DELAY
:
1994 case -NFS4ERR_GRACE
:
1995 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1999 case -NFS4ERR_DENIED
:
2001 struct nfs4_lock_state
*lsp
= fl
->fl_u
.nfs4_fl
.owner
;
2003 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2010 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
,
2011 struct nfs4_state
*state
, const nfs4_stateid
*stateid
,
2014 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2015 struct nfs4_opendata
*opendata
;
2018 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2019 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
2020 if (IS_ERR(opendata
))
2021 return PTR_ERR(opendata
);
2022 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
2023 write_seqlock(&state
->seqlock
);
2024 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2025 write_sequnlock(&state
->seqlock
);
2026 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2027 switch (type
& (FMODE_READ
|FMODE_WRITE
)) {
2028 case FMODE_READ
|FMODE_WRITE
:
2030 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
2033 err
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
2037 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
2039 nfs4_opendata_put(opendata
);
2040 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, NULL
, err
);
2043 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
2045 struct nfs4_opendata
*data
= calldata
;
2047 nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
2048 &data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, task
);
2051 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
2053 struct nfs4_opendata
*data
= calldata
;
2055 nfs40_sequence_done(task
, &data
->c_res
.seq_res
);
2057 data
->rpc_status
= task
->tk_status
;
2058 if (data
->rpc_status
== 0) {
2059 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
2060 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2061 renew_lease(data
->o_res
.server
, data
->timestamp
);
2062 data
->rpc_done
= true;
2066 static void nfs4_open_confirm_release(void *calldata
)
2068 struct nfs4_opendata
*data
= calldata
;
2069 struct nfs4_state
*state
= NULL
;
2071 /* If this request hasn't been cancelled, do nothing */
2072 if (!data
->cancelled
)
2074 /* In case of error, no cleanup! */
2075 if (!data
->rpc_done
)
2077 state
= nfs4_opendata_to_nfs4_state(data
);
2079 nfs4_close_state(state
, data
->o_arg
.fmode
);
2081 nfs4_opendata_put(data
);
2084 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
2085 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
2086 .rpc_call_done
= nfs4_open_confirm_done
,
2087 .rpc_release
= nfs4_open_confirm_release
,
2091 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2093 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
2095 struct nfs_server
*server
= NFS_SERVER(d_inode(data
->dir
));
2096 struct rpc_task
*task
;
2097 struct rpc_message msg
= {
2098 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
2099 .rpc_argp
= &data
->c_arg
,
2100 .rpc_resp
= &data
->c_res
,
2101 .rpc_cred
= data
->owner
->so_cred
,
2103 struct rpc_task_setup task_setup_data
= {
2104 .rpc_client
= server
->client
,
2105 .rpc_message
= &msg
,
2106 .callback_ops
= &nfs4_open_confirm_ops
,
2107 .callback_data
= data
,
2108 .workqueue
= nfsiod_workqueue
,
2109 .flags
= RPC_TASK_ASYNC
,
2113 nfs4_init_sequence(&data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, 1);
2114 kref_get(&data
->kref
);
2115 data
->rpc_done
= false;
2116 data
->rpc_status
= 0;
2117 data
->timestamp
= jiffies
;
2118 if (data
->is_recover
)
2119 nfs4_set_sequence_privileged(&data
->c_arg
.seq_args
);
2120 task
= rpc_run_task(&task_setup_data
);
2122 return PTR_ERR(task
);
2123 status
= rpc_wait_for_completion_task(task
);
2125 data
->cancelled
= true;
2128 status
= data
->rpc_status
;
2133 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
2135 struct nfs4_opendata
*data
= calldata
;
2136 struct nfs4_state_owner
*sp
= data
->owner
;
2137 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
2138 enum open_claim_type4 claim
= data
->o_arg
.claim
;
2140 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
2143 * Check if we still need to send an OPEN call, or if we can use
2144 * a delegation instead.
2146 if (data
->state
!= NULL
) {
2147 struct nfs_delegation
*delegation
;
2149 if (can_open_cached(data
->state
, data
->o_arg
.fmode
,
2150 data
->o_arg
.open_flags
, claim
))
2153 delegation
= nfs4_get_valid_delegation(data
->state
->inode
);
2154 if (can_open_delegated(delegation
, data
->o_arg
.fmode
, claim
))
2155 goto unlock_no_action
;
2158 /* Update client id. */
2159 data
->o_arg
.clientid
= clp
->cl_clientid
;
2163 case NFS4_OPEN_CLAIM_PREVIOUS
:
2164 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
2165 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
2166 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
2167 case NFS4_OPEN_CLAIM_FH
:
2168 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
2170 data
->timestamp
= jiffies
;
2171 if (nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
2172 &data
->o_arg
.seq_args
,
2173 &data
->o_res
.seq_res
,
2175 nfs_release_seqid(data
->o_arg
.seqid
);
2177 /* Set the create mode (note dependency on the session type) */
2178 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
2179 if (data
->o_arg
.open_flags
& O_EXCL
) {
2180 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
2181 if (nfs4_has_persistent_session(clp
))
2182 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
2183 else if (clp
->cl_mvops
->minor_version
> 0)
2184 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
2188 trace_nfs4_cached_open(data
->state
);
2191 task
->tk_action
= NULL
;
2193 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
2196 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
2198 struct nfs4_opendata
*data
= calldata
;
2200 data
->rpc_status
= task
->tk_status
;
2202 if (!nfs4_sequence_process(task
, &data
->o_res
.seq_res
))
2205 if (task
->tk_status
== 0) {
2206 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
2207 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
2211 data
->rpc_status
= -ELOOP
;
2214 data
->rpc_status
= -EISDIR
;
2217 data
->rpc_status
= -ENOTDIR
;
2220 renew_lease(data
->o_res
.server
, data
->timestamp
);
2221 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
2222 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2224 data
->rpc_done
= true;
2227 static void nfs4_open_release(void *calldata
)
2229 struct nfs4_opendata
*data
= calldata
;
2230 struct nfs4_state
*state
= NULL
;
2232 /* If this request hasn't been cancelled, do nothing */
2233 if (!data
->cancelled
)
2235 /* In case of error, no cleanup! */
2236 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
2238 /* In case we need an open_confirm, no cleanup! */
2239 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2241 state
= nfs4_opendata_to_nfs4_state(data
);
2243 nfs4_close_state(state
, data
->o_arg
.fmode
);
2245 nfs4_opendata_put(data
);
2248 static const struct rpc_call_ops nfs4_open_ops
= {
2249 .rpc_call_prepare
= nfs4_open_prepare
,
2250 .rpc_call_done
= nfs4_open_done
,
2251 .rpc_release
= nfs4_open_release
,
2254 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
2256 struct inode
*dir
= d_inode(data
->dir
);
2257 struct nfs_server
*server
= NFS_SERVER(dir
);
2258 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2259 struct nfs_openres
*o_res
= &data
->o_res
;
2260 struct rpc_task
*task
;
2261 struct rpc_message msg
= {
2262 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
2265 .rpc_cred
= data
->owner
->so_cred
,
2267 struct rpc_task_setup task_setup_data
= {
2268 .rpc_client
= server
->client
,
2269 .rpc_message
= &msg
,
2270 .callback_ops
= &nfs4_open_ops
,
2271 .callback_data
= data
,
2272 .workqueue
= nfsiod_workqueue
,
2273 .flags
= RPC_TASK_ASYNC
,
2277 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
2278 kref_get(&data
->kref
);
2279 data
->rpc_done
= false;
2280 data
->rpc_status
= 0;
2281 data
->cancelled
= false;
2282 data
->is_recover
= false;
2284 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2285 data
->is_recover
= true;
2287 task
= rpc_run_task(&task_setup_data
);
2289 return PTR_ERR(task
);
2290 status
= rpc_wait_for_completion_task(task
);
2292 data
->cancelled
= true;
2295 status
= data
->rpc_status
;
2301 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
2303 struct inode
*dir
= d_inode(data
->dir
);
2304 struct nfs_openres
*o_res
= &data
->o_res
;
2307 status
= nfs4_run_open_task(data
, 1);
2308 if (status
!= 0 || !data
->rpc_done
)
2311 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
2313 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2314 status
= _nfs4_proc_open_confirm(data
);
2320 * Additional permission checks in order to distinguish between an
2321 * open for read, and an open for execute. This works around the
2322 * fact that NFSv4 OPEN treats read and execute permissions as being
2324 * Note that in the non-execute case, we want to turn off permission
2325 * checking if we just created a new file (POSIX open() semantics).
2327 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2328 struct nfs4_opendata
*opendata
,
2329 struct nfs4_state
*state
, fmode_t fmode
,
2332 struct nfs_access_entry cache
;
2335 /* access call failed or for some reason the server doesn't
2336 * support any access modes -- defer access call until later */
2337 if (opendata
->o_res
.access_supported
== 0)
2342 * Use openflags to check for exec, because fmode won't
2343 * always have FMODE_EXEC set when file open for exec.
2345 if (openflags
& __FMODE_EXEC
) {
2346 /* ONLY check for exec rights */
2347 if (S_ISDIR(state
->inode
->i_mode
))
2348 mask
= NFS4_ACCESS_LOOKUP
;
2350 mask
= NFS4_ACCESS_EXECUTE
;
2351 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2352 mask
= NFS4_ACCESS_READ
;
2355 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2356 nfs_access_add_cache(state
->inode
, &cache
);
2358 flags
= NFS4_ACCESS_READ
| NFS4_ACCESS_EXECUTE
| NFS4_ACCESS_LOOKUP
;
2359 if ((mask
& ~cache
.mask
& flags
) == 0)
2366 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2368 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2370 struct inode
*dir
= d_inode(data
->dir
);
2371 struct nfs_server
*server
= NFS_SERVER(dir
);
2372 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2373 struct nfs_openres
*o_res
= &data
->o_res
;
2376 status
= nfs4_run_open_task(data
, 0);
2377 if (!data
->rpc_done
)
2380 if (status
== -NFS4ERR_BADNAME
&&
2381 !(o_arg
->open_flags
& O_CREAT
))
2386 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2388 if (o_arg
->open_flags
& O_CREAT
) {
2389 if (o_arg
->open_flags
& O_EXCL
)
2390 data
->file_created
= true;
2391 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2392 data
->file_created
= true;
2393 if (data
->file_created
|| dir
->i_version
!= o_res
->cinfo
.after
)
2394 update_changeattr(dir
, &o_res
->cinfo
,
2395 o_res
->f_attr
->time_start
);
2397 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2398 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2399 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2400 status
= _nfs4_proc_open_confirm(data
);
2404 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
)) {
2405 nfs4_sequence_free_slot(&o_res
->seq_res
);
2406 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2413 * reclaim state on the server after a network partition.
2414 * Assumes caller holds the appropriate lock
2416 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2418 struct nfs4_opendata
*opendata
;
2421 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2422 NFS4_OPEN_CLAIM_FH
);
2423 if (IS_ERR(opendata
))
2424 return PTR_ERR(opendata
);
2425 ret
= nfs4_open_recover(opendata
, state
);
2427 d_drop(ctx
->dentry
);
2428 nfs4_opendata_put(opendata
);
2432 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2434 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2435 struct nfs4_exception exception
= { };
2439 err
= _nfs4_open_expired(ctx
, state
);
2440 trace_nfs4_open_expired(ctx
, 0, err
);
2441 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2446 case -NFS4ERR_GRACE
:
2447 case -NFS4ERR_DELAY
:
2448 nfs4_handle_exception(server
, err
, &exception
);
2451 } while (exception
.retry
);
2456 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2458 struct nfs_open_context
*ctx
;
2461 ctx
= nfs4_state_find_open_context(state
);
2464 ret
= nfs4_do_open_expired(ctx
, state
);
2465 put_nfs_open_context(ctx
);
2469 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
,
2470 const nfs4_stateid
*stateid
)
2472 nfs_remove_bad_delegation(state
->inode
, stateid
);
2473 write_seqlock(&state
->seqlock
);
2474 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2475 write_sequnlock(&state
->seqlock
);
2476 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2479 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2481 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2482 nfs_finish_clear_delegation_stateid(state
, NULL
);
2485 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2487 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2488 nfs40_clear_delegation_stateid(state
);
2489 return nfs4_open_expired(sp
, state
);
2492 static int nfs40_test_and_free_expired_stateid(struct nfs_server
*server
,
2493 nfs4_stateid
*stateid
,
2494 struct rpc_cred
*cred
)
2496 return -NFS4ERR_BAD_STATEID
;
2499 #if defined(CONFIG_NFS_V4_1)
2500 static int nfs41_test_and_free_expired_stateid(struct nfs_server
*server
,
2501 nfs4_stateid
*stateid
,
2502 struct rpc_cred
*cred
)
2506 switch (stateid
->type
) {
2509 case NFS4_INVALID_STATEID_TYPE
:
2510 case NFS4_SPECIAL_STATEID_TYPE
:
2511 return -NFS4ERR_BAD_STATEID
;
2512 case NFS4_REVOKED_STATEID_TYPE
:
2516 status
= nfs41_test_stateid(server
, stateid
, cred
);
2518 case -NFS4ERR_EXPIRED
:
2519 case -NFS4ERR_ADMIN_REVOKED
:
2520 case -NFS4ERR_DELEG_REVOKED
:
2526 /* Ack the revoked state to the server */
2527 nfs41_free_stateid(server
, stateid
, cred
, true);
2528 return -NFS4ERR_EXPIRED
;
2531 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2533 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2534 nfs4_stateid stateid
;
2535 struct nfs_delegation
*delegation
;
2536 struct rpc_cred
*cred
;
2539 /* Get the delegation credential for use by test/free_stateid */
2541 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2542 if (delegation
== NULL
) {
2547 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2548 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
2550 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2554 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED
,
2555 &delegation
->flags
)) {
2560 cred
= get_rpccred(delegation
->cred
);
2562 status
= nfs41_test_and_free_expired_stateid(server
, &stateid
, cred
);
2563 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2564 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
2565 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2571 * nfs41_check_expired_locks - possibly free a lock stateid
2573 * @state: NFSv4 state for an inode
2575 * Returns NFS_OK if recovery for this stateid is now finished.
2576 * Otherwise a negative NFS4ERR value is returned.
2578 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
2580 int status
, ret
= NFS_OK
;
2581 struct nfs4_lock_state
*lsp
, *prev
= NULL
;
2582 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2584 if (!test_bit(LK_STATE_IN_USE
, &state
->flags
))
2587 spin_lock(&state
->state_lock
);
2588 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
2589 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
2590 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
2592 atomic_inc(&lsp
->ls_count
);
2593 spin_unlock(&state
->state_lock
);
2595 nfs4_put_lock_state(prev
);
2598 status
= nfs41_test_and_free_expired_stateid(server
,
2601 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
2602 if (status
== -NFS4ERR_EXPIRED
||
2603 status
== -NFS4ERR_BAD_STATEID
) {
2604 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
2605 lsp
->ls_stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
2606 if (!recover_lost_locks
)
2607 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2608 } else if (status
!= NFS_OK
) {
2610 nfs4_put_lock_state(prev
);
2613 spin_lock(&state
->state_lock
);
2616 spin_unlock(&state
->state_lock
);
2617 nfs4_put_lock_state(prev
);
2623 * nfs41_check_open_stateid - possibly free an open stateid
2625 * @state: NFSv4 state for an inode
2627 * Returns NFS_OK if recovery for this stateid is now finished.
2628 * Otherwise a negative NFS4ERR value is returned.
2630 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2632 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2633 nfs4_stateid
*stateid
= &state
->open_stateid
;
2634 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2637 if (test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0) {
2638 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0) {
2639 if (nfs4_have_delegation(state
->inode
, state
->state
))
2641 return -NFS4ERR_OPENMODE
;
2643 return -NFS4ERR_BAD_STATEID
;
2645 status
= nfs41_test_and_free_expired_stateid(server
, stateid
, cred
);
2646 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2647 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
) {
2648 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2649 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2650 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2651 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2652 stateid
->type
= NFS4_INVALID_STATEID_TYPE
;
2655 if (nfs_open_stateid_recover_openmode(state
))
2656 return -NFS4ERR_OPENMODE
;
2660 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2664 nfs41_check_delegation_stateid(state
);
2665 status
= nfs41_check_expired_locks(state
);
2666 if (status
!= NFS_OK
)
2668 status
= nfs41_check_open_stateid(state
);
2669 if (status
!= NFS_OK
)
2670 status
= nfs4_open_expired(sp
, state
);
2676 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2677 * fields corresponding to attributes that were used to store the verifier.
2678 * Make sure we clobber those fields in the later setattr call
2680 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2681 struct iattr
*sattr
, struct nfs4_label
**label
)
2683 const u32
*attrset
= opendata
->o_res
.attrset
;
2685 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2686 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2687 sattr
->ia_valid
|= ATTR_ATIME
;
2689 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2690 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2691 sattr
->ia_valid
|= ATTR_MTIME
;
2693 /* Except MODE, it seems harmless of setting twice. */
2694 if (opendata
->o_arg
.createmode
!= NFS4_CREATE_EXCLUSIVE
&&
2695 (attrset
[1] & FATTR4_WORD1_MODE
||
2696 attrset
[2] & FATTR4_WORD2_MODE_UMASK
))
2697 sattr
->ia_valid
&= ~ATTR_MODE
;
2699 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2703 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2706 struct nfs_open_context
*ctx
)
2708 struct nfs4_state_owner
*sp
= opendata
->owner
;
2709 struct nfs_server
*server
= sp
->so_server
;
2710 struct dentry
*dentry
;
2711 struct nfs4_state
*state
;
2715 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2717 ret
= _nfs4_proc_open(opendata
);
2721 state
= _nfs4_opendata_to_nfs4_state(opendata
);
2722 ret
= PTR_ERR(state
);
2726 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2727 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2728 if (opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
)
2729 set_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
);
2731 dentry
= opendata
->dentry
;
2732 if (d_really_is_negative(dentry
)) {
2733 struct dentry
*alias
;
2735 alias
= d_exact_alias(dentry
, state
->inode
);
2737 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2738 /* d_splice_alias() can't fail here - it's a non-directory */
2741 ctx
->dentry
= dentry
= alias
;
2743 nfs_set_verifier(dentry
,
2744 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2747 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2751 if (d_inode(dentry
) == state
->inode
) {
2752 nfs_inode_attach_open_context(ctx
);
2753 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2754 nfs4_schedule_stateid_recovery(server
, state
);
2757 if (!opendata
->cancelled
)
2758 nfs4_sequence_free_slot(&opendata
->o_res
.seq_res
);
2763 * Returns a referenced nfs4_state
2765 static int _nfs4_do_open(struct inode
*dir
,
2766 struct nfs_open_context
*ctx
,
2768 const struct nfs4_open_createattrs
*c
,
2771 struct nfs4_state_owner
*sp
;
2772 struct nfs4_state
*state
= NULL
;
2773 struct nfs_server
*server
= NFS_SERVER(dir
);
2774 struct nfs4_opendata
*opendata
;
2775 struct dentry
*dentry
= ctx
->dentry
;
2776 struct rpc_cred
*cred
= ctx
->cred
;
2777 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2778 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2779 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2780 struct iattr
*sattr
= c
->sattr
;
2781 struct nfs4_label
*label
= c
->label
;
2782 struct nfs4_label
*olabel
= NULL
;
2785 /* Protect against reboot recovery conflicts */
2787 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2789 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2792 status
= nfs4_client_recover_expired_lease(server
->nfs_client
);
2794 goto err_put_state_owner
;
2795 if (d_really_is_positive(dentry
))
2796 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2798 if (d_really_is_positive(dentry
))
2799 claim
= NFS4_OPEN_CLAIM_FH
;
2800 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
,
2801 c
, claim
, GFP_KERNEL
);
2802 if (opendata
== NULL
)
2803 goto err_put_state_owner
;
2806 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2807 if (IS_ERR(olabel
)) {
2808 status
= PTR_ERR(olabel
);
2809 goto err_opendata_put
;
2813 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2814 if (!opendata
->f_attr
.mdsthreshold
) {
2815 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2816 if (!opendata
->f_attr
.mdsthreshold
)
2817 goto err_free_label
;
2819 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2821 if (d_really_is_positive(dentry
))
2822 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2824 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2826 goto err_free_label
;
2829 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2830 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2831 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2833 * send create attributes which was not set by open
2834 * with an extra setattr.
2836 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2837 nfs_fattr_init(opendata
->o_res
.f_attr
);
2838 status
= nfs4_do_setattr(state
->inode
, cred
,
2839 opendata
->o_res
.f_attr
, sattr
,
2840 ctx
, label
, olabel
);
2842 nfs_setattr_update_inode(state
->inode
, sattr
,
2843 opendata
->o_res
.f_attr
);
2844 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2848 if (opened
&& opendata
->file_created
)
2849 *opened
|= FILE_CREATED
;
2851 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2852 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2853 opendata
->f_attr
.mdsthreshold
= NULL
;
2856 nfs4_label_free(olabel
);
2858 nfs4_opendata_put(opendata
);
2859 nfs4_put_state_owner(sp
);
2862 nfs4_label_free(olabel
);
2864 nfs4_opendata_put(opendata
);
2865 err_put_state_owner
:
2866 nfs4_put_state_owner(sp
);
2872 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2873 struct nfs_open_context
*ctx
,
2875 struct iattr
*sattr
,
2876 struct nfs4_label
*label
,
2879 struct nfs_server
*server
= NFS_SERVER(dir
);
2880 struct nfs4_exception exception
= { };
2881 struct nfs4_state
*res
;
2882 struct nfs4_open_createattrs c
= {
2886 [0] = (__u32
)jiffies
,
2887 [1] = (__u32
)current
->pid
,
2893 status
= _nfs4_do_open(dir
, ctx
, flags
, &c
, opened
);
2895 trace_nfs4_open_file(ctx
, flags
, status
);
2898 /* NOTE: BAD_SEQID means the server and client disagree about the
2899 * book-keeping w.r.t. state-changing operations
2900 * (OPEN/CLOSE/LOCK/LOCKU...)
2901 * It is actually a sign of a bug on the client or on the server.
2903 * If we receive a BAD_SEQID error in the particular case of
2904 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2905 * have unhashed the old state_owner for us, and that we can
2906 * therefore safely retry using a new one. We should still warn
2907 * the user though...
2909 if (status
== -NFS4ERR_BAD_SEQID
) {
2910 pr_warn_ratelimited("NFS: v4 server %s "
2911 " returned a bad sequence-id error!\n",
2912 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2913 exception
.retry
= 1;
2917 * BAD_STATEID on OPEN means that the server cancelled our
2918 * state before it received the OPEN_CONFIRM.
2919 * Recover by retrying the request as per the discussion
2920 * on Page 181 of RFC3530.
2922 if (status
== -NFS4ERR_BAD_STATEID
) {
2923 exception
.retry
= 1;
2926 if (status
== -NFS4ERR_EXPIRED
) {
2927 nfs4_schedule_lease_recovery(server
->nfs_client
);
2928 exception
.retry
= 1;
2931 if (status
== -EAGAIN
) {
2932 /* We must have found a delegation */
2933 exception
.retry
= 1;
2936 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2938 res
= ERR_PTR(nfs4_handle_exception(server
,
2939 status
, &exception
));
2940 } while (exception
.retry
);
2944 static int _nfs4_do_setattr(struct inode
*inode
,
2945 struct nfs_setattrargs
*arg
,
2946 struct nfs_setattrres
*res
,
2947 struct rpc_cred
*cred
,
2948 struct nfs_open_context
*ctx
)
2950 struct nfs_server
*server
= NFS_SERVER(inode
);
2951 struct rpc_message msg
= {
2952 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2957 struct rpc_cred
*delegation_cred
= NULL
;
2958 unsigned long timestamp
= jiffies
;
2962 nfs_fattr_init(res
->fattr
);
2964 /* Servers should only apply open mode checks for file size changes */
2965 truncate
= (arg
->iap
->ia_valid
& ATTR_SIZE
) ? true : false;
2969 if (nfs4_copy_delegation_stateid(inode
, FMODE_WRITE
, &arg
->stateid
, &delegation_cred
)) {
2970 /* Use that stateid */
2971 } else if (ctx
!= NULL
&& ctx
->state
) {
2972 struct nfs_lock_context
*l_ctx
;
2973 if (!nfs4_valid_open_stateid(ctx
->state
))
2975 l_ctx
= nfs_get_lock_context(ctx
);
2977 return PTR_ERR(l_ctx
);
2978 status
= nfs4_select_rw_stateid(ctx
->state
, FMODE_WRITE
, l_ctx
,
2979 &arg
->stateid
, &delegation_cred
);
2980 nfs_put_lock_context(l_ctx
);
2985 nfs4_stateid_copy(&arg
->stateid
, &zero_stateid
);
2987 if (delegation_cred
)
2988 msg
.rpc_cred
= delegation_cred
;
2990 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
->seq_args
, &res
->seq_res
, 1);
2992 put_rpccred(delegation_cred
);
2993 if (status
== 0 && ctx
!= NULL
)
2994 renew_lease(server
, timestamp
);
2995 trace_nfs4_setattr(inode
, &arg
->stateid
, status
);
2999 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
3000 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
3001 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
3002 struct nfs4_label
*olabel
)
3004 struct nfs_server
*server
= NFS_SERVER(inode
);
3005 struct nfs4_state
*state
= ctx
? ctx
->state
: NULL
;
3006 struct nfs_setattrargs arg
= {
3007 .fh
= NFS_FH(inode
),
3010 .bitmask
= server
->attr_bitmask
,
3013 struct nfs_setattrres res
= {
3018 struct nfs4_exception exception
= {
3021 .stateid
= &arg
.stateid
,
3025 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
3027 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
3030 err
= _nfs4_do_setattr(inode
, &arg
, &res
, cred
, ctx
);
3032 case -NFS4ERR_OPENMODE
:
3033 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
3034 pr_warn_once("NFSv4: server %s is incorrectly "
3035 "applying open mode checks to "
3036 "a SETATTR that is not "
3037 "changing file size.\n",
3038 server
->nfs_client
->cl_hostname
);
3040 if (state
&& !(state
->state
& FMODE_WRITE
)) {
3042 if (sattr
->ia_valid
& ATTR_OPEN
)
3047 err
= nfs4_handle_exception(server
, err
, &exception
);
3048 } while (exception
.retry
);
3054 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
3056 if (inode
== NULL
|| !nfs_have_layout(inode
))
3059 return pnfs_wait_on_layoutreturn(inode
, task
);
3062 struct nfs4_closedata
{
3063 struct inode
*inode
;
3064 struct nfs4_state
*state
;
3065 struct nfs_closeargs arg
;
3066 struct nfs_closeres res
;
3068 struct nfs4_layoutreturn_args arg
;
3069 struct nfs4_layoutreturn_res res
;
3070 struct nfs4_xdr_opaque_data ld_private
;
3074 struct nfs_fattr fattr
;
3075 unsigned long timestamp
;
3078 static void nfs4_free_closedata(void *data
)
3080 struct nfs4_closedata
*calldata
= data
;
3081 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
3082 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
3084 if (calldata
->lr
.roc
)
3085 pnfs_roc_release(&calldata
->lr
.arg
, &calldata
->lr
.res
,
3086 calldata
->res
.lr_ret
);
3087 nfs4_put_open_state(calldata
->state
);
3088 nfs_free_seqid(calldata
->arg
.seqid
);
3089 nfs4_put_state_owner(sp
);
3090 nfs_sb_deactive(sb
);
3094 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
3096 struct nfs4_closedata
*calldata
= data
;
3097 struct nfs4_state
*state
= calldata
->state
;
3098 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
3099 nfs4_stateid
*res_stateid
= NULL
;
3101 dprintk("%s: begin!\n", __func__
);
3102 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
3104 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
3106 /* Handle Layoutreturn errors */
3107 if (calldata
->arg
.lr_args
&& task
->tk_status
!= 0) {
3108 switch (calldata
->res
.lr_ret
) {
3110 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3113 calldata
->arg
.lr_args
= NULL
;
3114 calldata
->res
.lr_res
= NULL
;
3116 case -NFS4ERR_ADMIN_REVOKED
:
3117 case -NFS4ERR_DELEG_REVOKED
:
3118 case -NFS4ERR_EXPIRED
:
3119 case -NFS4ERR_BAD_STATEID
:
3120 case -NFS4ERR_OLD_STATEID
:
3121 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
3122 case -NFS4ERR_WRONG_CRED
:
3123 calldata
->arg
.lr_args
= NULL
;
3124 calldata
->res
.lr_res
= NULL
;
3125 calldata
->res
.lr_ret
= 0;
3126 rpc_restart_call_prepare(task
);
3131 /* hmm. we are done with the inode, and in the process of freeing
3132 * the state_owner. we keep this around to process errors
3134 switch (task
->tk_status
) {
3136 res_stateid
= &calldata
->res
.stateid
;
3137 renew_lease(server
, calldata
->timestamp
);
3139 case -NFS4ERR_ACCESS
:
3140 if (calldata
->arg
.bitmask
!= NULL
) {
3141 calldata
->arg
.bitmask
= NULL
;
3142 calldata
->res
.fattr
= NULL
;
3143 task
->tk_status
= 0;
3144 rpc_restart_call_prepare(task
);
3149 case -NFS4ERR_ADMIN_REVOKED
:
3150 case -NFS4ERR_STALE_STATEID
:
3151 case -NFS4ERR_EXPIRED
:
3152 nfs4_free_revoked_stateid(server
,
3153 &calldata
->arg
.stateid
,
3154 task
->tk_msg
.rpc_cred
);
3155 case -NFS4ERR_OLD_STATEID
:
3156 case -NFS4ERR_BAD_STATEID
:
3157 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
3158 &state
->open_stateid
)) {
3159 rpc_restart_call_prepare(task
);
3162 if (calldata
->arg
.fmode
== 0)
3165 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
3166 rpc_restart_call_prepare(task
);
3170 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
3171 res_stateid
, calldata
->arg
.fmode
);
3173 nfs_release_seqid(calldata
->arg
.seqid
);
3174 nfs_refresh_inode(calldata
->inode
, &calldata
->fattr
);
3175 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
3178 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
3180 struct nfs4_closedata
*calldata
= data
;
3181 struct nfs4_state
*state
= calldata
->state
;
3182 struct inode
*inode
= calldata
->inode
;
3183 bool is_rdonly
, is_wronly
, is_rdwr
;
3186 dprintk("%s: begin!\n", __func__
);
3187 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3190 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
3191 spin_lock(&state
->owner
->so_lock
);
3192 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
3193 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
3194 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
3195 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
3196 /* Calculate the change in open mode */
3197 calldata
->arg
.fmode
= 0;
3198 if (state
->n_rdwr
== 0) {
3199 if (state
->n_rdonly
== 0)
3200 call_close
|= is_rdonly
;
3202 calldata
->arg
.fmode
|= FMODE_READ
;
3203 if (state
->n_wronly
== 0)
3204 call_close
|= is_wronly
;
3206 calldata
->arg
.fmode
|= FMODE_WRITE
;
3207 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
3208 call_close
|= is_rdwr
;
3210 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
3212 if (!nfs4_valid_open_stateid(state
) ||
3213 test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
3215 spin_unlock(&state
->owner
->so_lock
);
3218 /* Note: exit _without_ calling nfs4_close_done */
3222 if (!calldata
->lr
.roc
&& nfs4_wait_on_layoutreturn(inode
, task
)) {
3223 nfs_release_seqid(calldata
->arg
.seqid
);
3227 if (calldata
->arg
.fmode
== 0)
3228 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
3230 if (calldata
->arg
.fmode
== 0 || calldata
->arg
.fmode
== FMODE_READ
) {
3231 /* Close-to-open cache consistency revalidation */
3232 if (!nfs4_have_delegation(inode
, FMODE_READ
))
3233 calldata
->arg
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
3235 calldata
->arg
.bitmask
= NULL
;
3238 calldata
->arg
.share_access
=
3239 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
3240 calldata
->arg
.fmode
, 0);
3242 if (calldata
->res
.fattr
== NULL
)
3243 calldata
->arg
.bitmask
= NULL
;
3244 else if (calldata
->arg
.bitmask
== NULL
)
3245 calldata
->res
.fattr
= NULL
;
3246 calldata
->timestamp
= jiffies
;
3247 if (nfs4_setup_sequence(NFS_SERVER(inode
)->nfs_client
,
3248 &calldata
->arg
.seq_args
,
3249 &calldata
->res
.seq_res
,
3251 nfs_release_seqid(calldata
->arg
.seqid
);
3252 dprintk("%s: done!\n", __func__
);
3255 task
->tk_action
= NULL
;
3257 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
3260 static const struct rpc_call_ops nfs4_close_ops
= {
3261 .rpc_call_prepare
= nfs4_close_prepare
,
3262 .rpc_call_done
= nfs4_close_done
,
3263 .rpc_release
= nfs4_free_closedata
,
3267 * It is possible for data to be read/written from a mem-mapped file
3268 * after the sys_close call (which hits the vfs layer as a flush).
3269 * This means that we can't safely call nfsv4 close on a file until
3270 * the inode is cleared. This in turn means that we are not good
3271 * NFSv4 citizens - we do not indicate to the server to update the file's
3272 * share state even when we are done with one of the three share
3273 * stateid's in the inode.
3275 * NOTE: Caller must be holding the sp->so_owner semaphore!
3277 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
3279 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3280 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
3281 struct nfs4_closedata
*calldata
;
3282 struct nfs4_state_owner
*sp
= state
->owner
;
3283 struct rpc_task
*task
;
3284 struct rpc_message msg
= {
3285 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
3286 .rpc_cred
= state
->owner
->so_cred
,
3288 struct rpc_task_setup task_setup_data
= {
3289 .rpc_client
= server
->client
,
3290 .rpc_message
= &msg
,
3291 .callback_ops
= &nfs4_close_ops
,
3292 .workqueue
= nfsiod_workqueue
,
3293 .flags
= RPC_TASK_ASYNC
,
3295 int status
= -ENOMEM
;
3297 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
3298 &task_setup_data
.rpc_client
, &msg
);
3300 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
3301 if (calldata
== NULL
)
3303 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
3304 calldata
->inode
= state
->inode
;
3305 calldata
->state
= state
;
3306 calldata
->arg
.fh
= NFS_FH(state
->inode
);
3307 /* Serialization for the sequence id */
3308 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
3309 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
3310 if (IS_ERR(calldata
->arg
.seqid
))
3311 goto out_free_calldata
;
3312 nfs_fattr_init(&calldata
->fattr
);
3313 calldata
->arg
.fmode
= 0;
3314 calldata
->lr
.arg
.ld_private
= &calldata
->lr
.ld_private
;
3315 calldata
->res
.fattr
= &calldata
->fattr
;
3316 calldata
->res
.seqid
= calldata
->arg
.seqid
;
3317 calldata
->res
.server
= server
;
3318 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3319 calldata
->lr
.roc
= pnfs_roc(state
->inode
,
3320 &calldata
->lr
.arg
, &calldata
->lr
.res
, msg
.rpc_cred
);
3321 if (calldata
->lr
.roc
) {
3322 calldata
->arg
.lr_args
= &calldata
->lr
.arg
;
3323 calldata
->res
.lr_res
= &calldata
->lr
.res
;
3325 nfs_sb_active(calldata
->inode
->i_sb
);
3327 msg
.rpc_argp
= &calldata
->arg
;
3328 msg
.rpc_resp
= &calldata
->res
;
3329 task_setup_data
.callback_data
= calldata
;
3330 task
= rpc_run_task(&task_setup_data
);
3332 return PTR_ERR(task
);
3335 status
= rpc_wait_for_completion_task(task
);
3341 nfs4_put_open_state(state
);
3342 nfs4_put_state_owner(sp
);
3346 static struct inode
*
3347 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3348 int open_flags
, struct iattr
*attr
, int *opened
)
3350 struct nfs4_state
*state
;
3351 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3353 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3355 /* Protect against concurrent sillydeletes */
3356 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3358 nfs4_label_release_security(label
);
3361 return ERR_CAST(state
);
3362 return state
->inode
;
3365 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3367 if (ctx
->state
== NULL
)
3370 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3372 nfs4_close_state(ctx
->state
, ctx
->mode
);
3375 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3376 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3377 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3379 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3381 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3382 struct nfs4_server_caps_arg args
= {
3386 struct nfs4_server_caps_res res
= {};
3387 struct rpc_message msg
= {
3388 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3395 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3396 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3397 FATTR4_WORD0_LINK_SUPPORT
|
3398 FATTR4_WORD0_SYMLINK_SUPPORT
|
3399 FATTR4_WORD0_ACLSUPPORT
;
3401 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3403 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3405 /* Sanity check the server answers */
3406 switch (minorversion
) {
3408 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3409 res
.attr_bitmask
[2] = 0;
3412 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3415 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3417 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3418 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3419 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3420 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3421 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3422 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3423 NFS_CAP_SECURITY_LABEL
);
3424 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3425 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3426 server
->caps
|= NFS_CAP_ACLS
;
3427 if (res
.has_links
!= 0)
3428 server
->caps
|= NFS_CAP_HARDLINKS
;
3429 if (res
.has_symlinks
!= 0)
3430 server
->caps
|= NFS_CAP_SYMLINKS
;
3431 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3432 server
->caps
|= NFS_CAP_FILEID
;
3433 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3434 server
->caps
|= NFS_CAP_MODE
;
3435 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3436 server
->caps
|= NFS_CAP_NLINK
;
3437 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3438 server
->caps
|= NFS_CAP_OWNER
;
3439 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3440 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3441 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3442 server
->caps
|= NFS_CAP_ATIME
;
3443 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3444 server
->caps
|= NFS_CAP_CTIME
;
3445 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3446 server
->caps
|= NFS_CAP_MTIME
;
3447 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3448 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3449 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3451 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3452 sizeof(server
->attr_bitmask
));
3453 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3455 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3456 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3457 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3458 server
->cache_consistency_bitmask
[2] = 0;
3460 /* Avoid a regression due to buggy server */
3461 for (i
= 0; i
< ARRAY_SIZE(res
.exclcreat_bitmask
); i
++)
3462 res
.exclcreat_bitmask
[i
] &= res
.attr_bitmask
[i
];
3463 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3464 sizeof(server
->exclcreat_bitmask
));
3466 server
->acl_bitmask
= res
.acl_bitmask
;
3467 server
->fh_expire_type
= res
.fh_expire_type
;
3473 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3475 struct nfs4_exception exception
= { };
3478 err
= nfs4_handle_exception(server
,
3479 _nfs4_server_capabilities(server
, fhandle
),
3481 } while (exception
.retry
);
3485 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3486 struct nfs_fsinfo
*info
)
3489 struct nfs4_lookup_root_arg args
= {
3492 struct nfs4_lookup_res res
= {
3494 .fattr
= info
->fattr
,
3497 struct rpc_message msg
= {
3498 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3503 bitmask
[0] = nfs4_fattr_bitmap
[0];
3504 bitmask
[1] = nfs4_fattr_bitmap
[1];
3506 * Process the label in the upcoming getfattr
3508 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3510 nfs_fattr_init(info
->fattr
);
3511 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3514 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3515 struct nfs_fsinfo
*info
)
3517 struct nfs4_exception exception
= { };
3520 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3521 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3524 case -NFS4ERR_WRONGSEC
:
3527 err
= nfs4_handle_exception(server
, err
, &exception
);
3529 } while (exception
.retry
);
3534 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3535 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3537 struct rpc_auth_create_args auth_args
= {
3538 .pseudoflavor
= flavor
,
3540 struct rpc_auth
*auth
;
3542 auth
= rpcauth_create(&auth_args
, server
->client
);
3545 return nfs4_lookup_root(server
, fhandle
, info
);
3549 * Retry pseudoroot lookup with various security flavors. We do this when:
3551 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3552 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3554 * Returns zero on success, or a negative NFS4ERR value, or a
3555 * negative errno value.
3557 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3558 struct nfs_fsinfo
*info
)
3560 /* Per 3530bis 15.33.5 */
3561 static const rpc_authflavor_t flav_array
[] = {
3565 RPC_AUTH_UNIX
, /* courtesy */
3568 int status
= -EPERM
;
3571 if (server
->auth_info
.flavor_len
> 0) {
3572 /* try each flavor specified by user */
3573 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3574 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3575 server
->auth_info
.flavors
[i
]);
3576 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3581 /* no flavors specified by user, try default list */
3582 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3583 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3585 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3592 * -EACCESS could mean that the user doesn't have correct permissions
3593 * to access the mount. It could also mean that we tried to mount
3594 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3595 * existing mount programs don't handle -EACCES very well so it should
3596 * be mapped to -EPERM instead.
3598 if (status
== -EACCES
)
3604 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3605 * @server: initialized nfs_server handle
3606 * @fhandle: we fill in the pseudo-fs root file handle
3607 * @info: we fill in an FSINFO struct
3608 * @auth_probe: probe the auth flavours
3610 * Returns zero on success, or a negative errno.
3612 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3613 struct nfs_fsinfo
*info
,
3619 status
= nfs4_lookup_root(server
, fhandle
, info
);
3621 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3622 status
= server
->nfs_client
->cl_mvops
->find_root_sec(server
,
3626 status
= nfs4_server_capabilities(server
, fhandle
);
3628 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3630 return nfs4_map_errors(status
);
3633 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3634 struct nfs_fsinfo
*info
)
3637 struct nfs_fattr
*fattr
= info
->fattr
;
3638 struct nfs4_label
*label
= NULL
;
3640 error
= nfs4_server_capabilities(server
, mntfh
);
3642 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3646 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3648 return PTR_ERR(label
);
3650 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3652 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3653 goto err_free_label
;
3656 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3657 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3658 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3661 nfs4_label_free(label
);
3667 * Get locations and (maybe) other attributes of a referral.
3668 * Note that we'll actually follow the referral later when
3669 * we detect fsid mismatch in inode revalidation
3671 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3672 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3673 struct nfs_fh
*fhandle
)
3675 int status
= -ENOMEM
;
3676 struct page
*page
= NULL
;
3677 struct nfs4_fs_locations
*locations
= NULL
;
3679 page
= alloc_page(GFP_KERNEL
);
3682 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3683 if (locations
== NULL
)
3686 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3691 * If the fsid didn't change, this is a migration event, not a
3692 * referral. Cause us to drop into the exception handler, which
3693 * will kick off migration recovery.
3695 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3696 dprintk("%s: server did not return a different fsid for"
3697 " a referral at %s\n", __func__
, name
->name
);
3698 status
= -NFS4ERR_MOVED
;
3701 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3702 nfs_fixup_referral_attributes(&locations
->fattr
);
3704 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3705 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3706 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3714 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3715 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3717 struct nfs4_getattr_arg args
= {
3719 .bitmask
= server
->attr_bitmask
,
3721 struct nfs4_getattr_res res
= {
3726 struct rpc_message msg
= {
3727 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3732 args
.bitmask
= nfs4_bitmask(server
, label
);
3734 nfs_fattr_init(fattr
);
3735 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3738 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3739 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3741 struct nfs4_exception exception
= { };
3744 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3745 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3746 err
= nfs4_handle_exception(server
, err
,
3748 } while (exception
.retry
);
3753 * The file is not closed if it is opened due to the a request to change
3754 * the size of the file. The open call will not be needed once the
3755 * VFS layer lookup-intents are implemented.
3757 * Close is called when the inode is destroyed.
3758 * If we haven't opened the file for O_WRONLY, we
3759 * need to in the size_change case to obtain a stateid.
3762 * Because OPEN is always done by name in nfsv4, it is
3763 * possible that we opened a different file by the same
3764 * name. We can recognize this race condition, but we
3765 * can't do anything about it besides returning an error.
3767 * This will be fixed with VFS changes (lookup-intent).
3770 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3771 struct iattr
*sattr
)
3773 struct inode
*inode
= d_inode(dentry
);
3774 struct rpc_cred
*cred
= NULL
;
3775 struct nfs_open_context
*ctx
= NULL
;
3776 struct nfs4_label
*label
= NULL
;
3779 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3780 sattr
->ia_valid
& ATTR_SIZE
&&
3781 sattr
->ia_size
< i_size_read(inode
))
3782 pnfs_commit_and_return_layout(inode
);
3784 nfs_fattr_init(fattr
);
3786 /* Deal with open(O_TRUNC) */
3787 if (sattr
->ia_valid
& ATTR_OPEN
)
3788 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3790 /* Optimization: if the end result is no change, don't RPC */
3791 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3794 /* Search for an existing open(O_WRITE) file */
3795 if (sattr
->ia_valid
& ATTR_FILE
) {
3797 ctx
= nfs_file_open_context(sattr
->ia_file
);
3802 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3804 return PTR_ERR(label
);
3806 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, ctx
, NULL
, label
);
3808 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3809 nfs_setsecurity(inode
, fattr
, label
);
3811 nfs4_label_free(label
);
3815 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3816 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3817 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3819 struct nfs_server
*server
= NFS_SERVER(dir
);
3821 struct nfs4_lookup_arg args
= {
3822 .bitmask
= server
->attr_bitmask
,
3823 .dir_fh
= NFS_FH(dir
),
3826 struct nfs4_lookup_res res
= {
3832 struct rpc_message msg
= {
3833 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3838 args
.bitmask
= nfs4_bitmask(server
, label
);
3840 nfs_fattr_init(fattr
);
3842 dprintk("NFS call lookup %s\n", name
->name
);
3843 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3844 dprintk("NFS reply lookup: %d\n", status
);
3848 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3850 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3851 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3852 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3856 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3857 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3858 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3860 struct nfs4_exception exception
= { };
3861 struct rpc_clnt
*client
= *clnt
;
3864 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3865 trace_nfs4_lookup(dir
, name
, err
);
3867 case -NFS4ERR_BADNAME
:
3870 case -NFS4ERR_MOVED
:
3871 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3872 if (err
== -NFS4ERR_MOVED
)
3873 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3875 case -NFS4ERR_WRONGSEC
:
3877 if (client
!= *clnt
)
3879 client
= nfs4_negotiate_security(client
, dir
, name
);
3881 return PTR_ERR(client
);
3883 exception
.retry
= 1;
3886 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3888 } while (exception
.retry
);
3893 else if (client
!= *clnt
)
3894 rpc_shutdown_client(client
);
3899 static int nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
3900 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3901 struct nfs4_label
*label
)
3904 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3906 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3907 if (client
!= NFS_CLIENT(dir
)) {
3908 rpc_shutdown_client(client
);
3909 nfs_fixup_secinfo_attributes(fattr
);
3915 nfs4_proc_lookup_mountpoint(struct inode
*dir
, const struct qstr
*name
,
3916 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3918 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3921 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3923 return ERR_PTR(status
);
3924 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3927 static int _nfs4_proc_lookupp(struct inode
*inode
,
3928 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3929 struct nfs4_label
*label
)
3931 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
3932 struct nfs_server
*server
= NFS_SERVER(inode
);
3934 struct nfs4_lookupp_arg args
= {
3935 .bitmask
= server
->attr_bitmask
,
3936 .fh
= NFS_FH(inode
),
3938 struct nfs4_lookupp_res res
= {
3944 struct rpc_message msg
= {
3945 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUPP
],
3950 args
.bitmask
= nfs4_bitmask(server
, label
);
3952 nfs_fattr_init(fattr
);
3954 dprintk("NFS call lookupp ino=0x%lx\n", inode
->i_ino
);
3955 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
3957 dprintk("NFS reply lookupp: %d\n", status
);
3961 static int nfs4_proc_lookupp(struct inode
*inode
, struct nfs_fh
*fhandle
,
3962 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3964 struct nfs4_exception exception
= { };
3967 err
= _nfs4_proc_lookupp(inode
, fhandle
, fattr
, label
);
3968 trace_nfs4_lookupp(inode
, err
);
3969 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3971 } while (exception
.retry
);
3975 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3977 struct nfs_server
*server
= NFS_SERVER(inode
);
3978 struct nfs4_accessargs args
= {
3979 .fh
= NFS_FH(inode
),
3980 .bitmask
= server
->cache_consistency_bitmask
,
3982 struct nfs4_accessres res
= {
3985 struct rpc_message msg
= {
3986 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3989 .rpc_cred
= entry
->cred
,
3991 int mode
= entry
->mask
;
3995 * Determine which access bits we want to ask for...
3997 if (mode
& MAY_READ
)
3998 args
.access
|= NFS4_ACCESS_READ
;
3999 if (S_ISDIR(inode
->i_mode
)) {
4000 if (mode
& MAY_WRITE
)
4001 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
4002 if (mode
& MAY_EXEC
)
4003 args
.access
|= NFS4_ACCESS_LOOKUP
;
4005 if (mode
& MAY_WRITE
)
4006 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
4007 if (mode
& MAY_EXEC
)
4008 args
.access
|= NFS4_ACCESS_EXECUTE
;
4011 res
.fattr
= nfs_alloc_fattr();
4012 if (res
.fattr
== NULL
)
4015 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4017 nfs_access_set_mask(entry
, res
.access
);
4018 nfs_refresh_inode(inode
, res
.fattr
);
4020 nfs_free_fattr(res
.fattr
);
4024 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
4026 struct nfs4_exception exception
= { };
4029 err
= _nfs4_proc_access(inode
, entry
);
4030 trace_nfs4_access(inode
, err
);
4031 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4033 } while (exception
.retry
);
4038 * TODO: For the time being, we don't try to get any attributes
4039 * along with any of the zero-copy operations READ, READDIR,
4042 * In the case of the first three, we want to put the GETATTR
4043 * after the read-type operation -- this is because it is hard
4044 * to predict the length of a GETATTR response in v4, and thus
4045 * align the READ data correctly. This means that the GETATTR
4046 * may end up partially falling into the page cache, and we should
4047 * shift it into the 'tail' of the xdr_buf before processing.
4048 * To do this efficiently, we need to know the total length
4049 * of data received, which doesn't seem to be available outside
4052 * In the case of WRITE, we also want to put the GETATTR after
4053 * the operation -- in this case because we want to make sure
4054 * we get the post-operation mtime and size.
4056 * Both of these changes to the XDR layer would in fact be quite
4057 * minor, but I decided to leave them for a subsequent patch.
4059 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
4060 unsigned int pgbase
, unsigned int pglen
)
4062 struct nfs4_readlink args
= {
4063 .fh
= NFS_FH(inode
),
4068 struct nfs4_readlink_res res
;
4069 struct rpc_message msg
= {
4070 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
4075 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4078 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
4079 unsigned int pgbase
, unsigned int pglen
)
4081 struct nfs4_exception exception
= { };
4084 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
4085 trace_nfs4_readlink(inode
, err
);
4086 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4088 } while (exception
.retry
);
4093 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4096 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
4099 struct nfs_server
*server
= NFS_SERVER(dir
);
4100 struct nfs4_label l
, *ilabel
= NULL
;
4101 struct nfs_open_context
*ctx
;
4102 struct nfs4_state
*state
;
4105 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
, NULL
);
4107 return PTR_ERR(ctx
);
4109 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4111 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4112 sattr
->ia_mode
&= ~current_umask();
4113 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
4114 if (IS_ERR(state
)) {
4115 status
= PTR_ERR(state
);
4119 nfs4_label_release_security(ilabel
);
4120 put_nfs_open_context(ctx
);
4124 static int _nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4126 struct nfs_server
*server
= NFS_SERVER(dir
);
4127 struct nfs_removeargs args
= {
4131 struct nfs_removeres res
= {
4134 struct rpc_message msg
= {
4135 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
4139 unsigned long timestamp
= jiffies
;
4142 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
4144 update_changeattr(dir
, &res
.cinfo
, timestamp
);
4148 static int nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4150 struct nfs4_exception exception
= { };
4153 err
= _nfs4_proc_remove(dir
, name
);
4154 trace_nfs4_remove(dir
, name
, err
);
4155 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4157 } while (exception
.retry
);
4161 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
4163 struct nfs_server
*server
= NFS_SERVER(dir
);
4164 struct nfs_removeargs
*args
= msg
->rpc_argp
;
4165 struct nfs_removeres
*res
= msg
->rpc_resp
;
4167 res
->server
= server
;
4168 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
4169 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
4171 nfs_fattr_init(res
->dir_attr
);
4174 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
4176 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
)->nfs_client
,
4177 &data
->args
.seq_args
,
4182 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
4184 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
4185 struct nfs_removeres
*res
= &data
->res
;
4187 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4189 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
4190 &data
->timeout
) == -EAGAIN
)
4192 if (task
->tk_status
== 0)
4193 update_changeattr(dir
, &res
->cinfo
, res
->dir_attr
->time_start
);
4197 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
4199 struct nfs_server
*server
= NFS_SERVER(dir
);
4200 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
4201 struct nfs_renameres
*res
= msg
->rpc_resp
;
4203 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
4204 res
->server
= server
;
4205 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
4208 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
4210 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
)->nfs_client
,
4211 &data
->args
.seq_args
,
4216 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
4217 struct inode
*new_dir
)
4219 struct nfs_renamedata
*data
= task
->tk_calldata
;
4220 struct nfs_renameres
*res
= &data
->res
;
4222 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4224 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
4227 if (task
->tk_status
== 0) {
4228 update_changeattr(old_dir
, &res
->old_cinfo
, res
->old_fattr
->time_start
);
4229 if (new_dir
!= old_dir
)
4230 update_changeattr(new_dir
, &res
->new_cinfo
, res
->new_fattr
->time_start
);
4235 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4237 struct nfs_server
*server
= NFS_SERVER(inode
);
4238 struct nfs4_link_arg arg
= {
4239 .fh
= NFS_FH(inode
),
4240 .dir_fh
= NFS_FH(dir
),
4242 .bitmask
= server
->attr_bitmask
,
4244 struct nfs4_link_res res
= {
4248 struct rpc_message msg
= {
4249 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
4253 int status
= -ENOMEM
;
4255 res
.fattr
= nfs_alloc_fattr();
4256 if (res
.fattr
== NULL
)
4259 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4260 if (IS_ERR(res
.label
)) {
4261 status
= PTR_ERR(res
.label
);
4264 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
4266 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4268 update_changeattr(dir
, &res
.cinfo
, res
.fattr
->time_start
);
4269 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
4271 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
4275 nfs4_label_free(res
.label
);
4278 nfs_free_fattr(res
.fattr
);
4282 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4284 struct nfs4_exception exception
= { };
4287 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4288 _nfs4_proc_link(inode
, dir
, name
),
4290 } while (exception
.retry
);
4294 struct nfs4_createdata
{
4295 struct rpc_message msg
;
4296 struct nfs4_create_arg arg
;
4297 struct nfs4_create_res res
;
4299 struct nfs_fattr fattr
;
4300 struct nfs4_label
*label
;
4303 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
4304 const struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
4306 struct nfs4_createdata
*data
;
4308 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4310 struct nfs_server
*server
= NFS_SERVER(dir
);
4312 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4313 if (IS_ERR(data
->label
))
4316 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
4317 data
->msg
.rpc_argp
= &data
->arg
;
4318 data
->msg
.rpc_resp
= &data
->res
;
4319 data
->arg
.dir_fh
= NFS_FH(dir
);
4320 data
->arg
.server
= server
;
4321 data
->arg
.name
= name
;
4322 data
->arg
.attrs
= sattr
;
4323 data
->arg
.ftype
= ftype
;
4324 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
4325 data
->arg
.umask
= current_umask();
4326 data
->res
.server
= server
;
4327 data
->res
.fh
= &data
->fh
;
4328 data
->res
.fattr
= &data
->fattr
;
4329 data
->res
.label
= data
->label
;
4330 nfs_fattr_init(data
->res
.fattr
);
4338 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
4340 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
4341 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4343 update_changeattr(dir
, &data
->res
.dir_cinfo
,
4344 data
->res
.fattr
->time_start
);
4345 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
4350 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
4352 nfs4_label_free(data
->label
);
4356 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4357 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
4358 struct nfs4_label
*label
)
4360 struct nfs4_createdata
*data
;
4361 int status
= -ENAMETOOLONG
;
4363 if (len
> NFS4_MAXPATHLEN
)
4367 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
4371 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
4372 data
->arg
.u
.symlink
.pages
= &page
;
4373 data
->arg
.u
.symlink
.len
= len
;
4374 data
->arg
.label
= label
;
4376 status
= nfs4_do_create(dir
, dentry
, data
);
4378 nfs4_free_createdata(data
);
4383 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4384 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
4386 struct nfs4_exception exception
= { };
4387 struct nfs4_label l
, *label
= NULL
;
4390 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4393 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4394 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4395 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4397 } while (exception
.retry
);
4399 nfs4_label_release_security(label
);
4403 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4404 struct iattr
*sattr
, struct nfs4_label
*label
)
4406 struct nfs4_createdata
*data
;
4407 int status
= -ENOMEM
;
4409 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4413 data
->arg
.label
= label
;
4414 status
= nfs4_do_create(dir
, dentry
, data
);
4416 nfs4_free_createdata(data
);
4421 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4422 struct iattr
*sattr
)
4424 struct nfs_server
*server
= NFS_SERVER(dir
);
4425 struct nfs4_exception exception
= { };
4426 struct nfs4_label l
, *label
= NULL
;
4429 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4431 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4432 sattr
->ia_mode
&= ~current_umask();
4434 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4435 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4436 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4438 } while (exception
.retry
);
4439 nfs4_label_release_security(label
);
4444 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4445 u64 cookie
, struct page
**pages
, unsigned int count
, bool plus
)
4447 struct inode
*dir
= d_inode(dentry
);
4448 struct nfs4_readdir_arg args
= {
4453 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4456 struct nfs4_readdir_res res
;
4457 struct rpc_message msg
= {
4458 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4465 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4467 (unsigned long long)cookie
);
4468 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4469 res
.pgbase
= args
.pgbase
;
4470 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4472 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4473 status
+= args
.pgbase
;
4476 nfs_invalidate_atime(dir
);
4478 dprintk("%s: returns %d\n", __func__
, status
);
4482 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4483 u64 cookie
, struct page
**pages
, unsigned int count
, bool plus
)
4485 struct nfs4_exception exception
= { };
4488 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4489 pages
, count
, plus
);
4490 trace_nfs4_readdir(d_inode(dentry
), err
);
4491 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4493 } while (exception
.retry
);
4497 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4498 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4500 struct nfs4_createdata
*data
;
4501 int mode
= sattr
->ia_mode
;
4502 int status
= -ENOMEM
;
4504 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4509 data
->arg
.ftype
= NF4FIFO
;
4510 else if (S_ISBLK(mode
)) {
4511 data
->arg
.ftype
= NF4BLK
;
4512 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4513 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4515 else if (S_ISCHR(mode
)) {
4516 data
->arg
.ftype
= NF4CHR
;
4517 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4518 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4519 } else if (!S_ISSOCK(mode
)) {
4524 data
->arg
.label
= label
;
4525 status
= nfs4_do_create(dir
, dentry
, data
);
4527 nfs4_free_createdata(data
);
4532 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4533 struct iattr
*sattr
, dev_t rdev
)
4535 struct nfs_server
*server
= NFS_SERVER(dir
);
4536 struct nfs4_exception exception
= { };
4537 struct nfs4_label l
, *label
= NULL
;
4540 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4542 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4543 sattr
->ia_mode
&= ~current_umask();
4545 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4546 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4547 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4549 } while (exception
.retry
);
4551 nfs4_label_release_security(label
);
4556 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4557 struct nfs_fsstat
*fsstat
)
4559 struct nfs4_statfs_arg args
= {
4561 .bitmask
= server
->attr_bitmask
,
4563 struct nfs4_statfs_res res
= {
4566 struct rpc_message msg
= {
4567 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4572 nfs_fattr_init(fsstat
->fattr
);
4573 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4576 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4578 struct nfs4_exception exception
= { };
4581 err
= nfs4_handle_exception(server
,
4582 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4584 } while (exception
.retry
);
4588 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4589 struct nfs_fsinfo
*fsinfo
)
4591 struct nfs4_fsinfo_arg args
= {
4593 .bitmask
= server
->attr_bitmask
,
4595 struct nfs4_fsinfo_res res
= {
4598 struct rpc_message msg
= {
4599 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4604 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4607 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4609 struct nfs4_exception exception
= { };
4610 unsigned long now
= jiffies
;
4614 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4615 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4617 nfs4_set_lease_period(server
->nfs_client
,
4618 fsinfo
->lease_time
* HZ
,
4622 err
= nfs4_handle_exception(server
, err
, &exception
);
4623 } while (exception
.retry
);
4627 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4631 nfs_fattr_init(fsinfo
->fattr
);
4632 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4634 /* block layout checks this! */
4635 server
->pnfs_blksize
= fsinfo
->blksize
;
4636 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
);
4642 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4643 struct nfs_pathconf
*pathconf
)
4645 struct nfs4_pathconf_arg args
= {
4647 .bitmask
= server
->attr_bitmask
,
4649 struct nfs4_pathconf_res res
= {
4650 .pathconf
= pathconf
,
4652 struct rpc_message msg
= {
4653 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4658 /* None of the pathconf attributes are mandatory to implement */
4659 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4660 memset(pathconf
, 0, sizeof(*pathconf
));
4664 nfs_fattr_init(pathconf
->fattr
);
4665 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4668 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4669 struct nfs_pathconf
*pathconf
)
4671 struct nfs4_exception exception
= { };
4675 err
= nfs4_handle_exception(server
,
4676 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4678 } while (exception
.retry
);
4682 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4683 const struct nfs_open_context
*ctx
,
4684 const struct nfs_lock_context
*l_ctx
,
4687 return nfs4_select_rw_stateid(ctx
->state
, fmode
, l_ctx
, stateid
, NULL
);
4689 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4691 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4692 const struct nfs_open_context
*ctx
,
4693 const struct nfs_lock_context
*l_ctx
,
4696 nfs4_stateid current_stateid
;
4698 /* If the current stateid represents a lost lock, then exit */
4699 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4701 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4704 static bool nfs4_error_stateid_expired(int err
)
4707 case -NFS4ERR_DELEG_REVOKED
:
4708 case -NFS4ERR_ADMIN_REVOKED
:
4709 case -NFS4ERR_BAD_STATEID
:
4710 case -NFS4ERR_STALE_STATEID
:
4711 case -NFS4ERR_OLD_STATEID
:
4712 case -NFS4ERR_OPENMODE
:
4713 case -NFS4ERR_EXPIRED
:
4719 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4721 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4723 trace_nfs4_read(hdr
, task
->tk_status
);
4724 if (task
->tk_status
< 0) {
4725 struct nfs4_exception exception
= {
4726 .inode
= hdr
->inode
,
4727 .state
= hdr
->args
.context
->state
,
4728 .stateid
= &hdr
->args
.stateid
,
4730 task
->tk_status
= nfs4_async_handle_exception(task
,
4731 server
, task
->tk_status
, &exception
);
4732 if (exception
.retry
) {
4733 rpc_restart_call_prepare(task
);
4738 if (task
->tk_status
> 0)
4739 renew_lease(server
, hdr
->timestamp
);
4743 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4744 struct nfs_pgio_args
*args
)
4747 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4748 nfs4_stateid_is_current(&args
->stateid
,
4753 rpc_restart_call_prepare(task
);
4757 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4760 dprintk("--> %s\n", __func__
);
4762 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4764 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4766 if (task
->tk_status
> 0)
4767 nfs_invalidate_atime(hdr
->inode
);
4768 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4769 nfs4_read_done_cb(task
, hdr
);
4772 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4773 struct rpc_message
*msg
)
4775 hdr
->timestamp
= jiffies
;
4776 if (!hdr
->pgio_done_cb
)
4777 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4778 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4779 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4782 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4783 struct nfs_pgio_header
*hdr
)
4785 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
)->nfs_client
,
4786 &hdr
->args
.seq_args
,
4790 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4791 hdr
->args
.lock_context
,
4792 hdr
->rw_mode
) == -EIO
)
4794 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4799 static int nfs4_write_done_cb(struct rpc_task
*task
,
4800 struct nfs_pgio_header
*hdr
)
4802 struct inode
*inode
= hdr
->inode
;
4804 trace_nfs4_write(hdr
, task
->tk_status
);
4805 if (task
->tk_status
< 0) {
4806 struct nfs4_exception exception
= {
4807 .inode
= hdr
->inode
,
4808 .state
= hdr
->args
.context
->state
,
4809 .stateid
= &hdr
->args
.stateid
,
4811 task
->tk_status
= nfs4_async_handle_exception(task
,
4812 NFS_SERVER(inode
), task
->tk_status
,
4814 if (exception
.retry
) {
4815 rpc_restart_call_prepare(task
);
4819 if (task
->tk_status
>= 0) {
4820 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4821 nfs_writeback_update_inode(hdr
);
4826 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4827 struct nfs_pgio_args
*args
)
4830 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4831 nfs4_stateid_is_current(&args
->stateid
,
4836 rpc_restart_call_prepare(task
);
4840 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4842 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4844 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4846 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4847 nfs4_write_done_cb(task
, hdr
);
4851 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4853 /* Don't request attributes for pNFS or O_DIRECT writes */
4854 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4856 /* Otherwise, request attributes if and only if we don't hold
4859 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4862 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4863 struct rpc_message
*msg
)
4865 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4867 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4868 hdr
->args
.bitmask
= NULL
;
4869 hdr
->res
.fattr
= NULL
;
4871 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4873 if (!hdr
->pgio_done_cb
)
4874 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4875 hdr
->res
.server
= server
;
4876 hdr
->timestamp
= jiffies
;
4878 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4879 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4882 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4884 nfs4_setup_sequence(NFS_SERVER(data
->inode
)->nfs_client
,
4885 &data
->args
.seq_args
,
4890 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4892 struct inode
*inode
= data
->inode
;
4894 trace_nfs4_commit(data
, task
->tk_status
);
4895 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4896 NULL
, NULL
) == -EAGAIN
) {
4897 rpc_restart_call_prepare(task
);
4903 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4905 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4907 return data
->commit_done_cb(task
, data
);
4910 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4912 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4914 if (data
->commit_done_cb
== NULL
)
4915 data
->commit_done_cb
= nfs4_commit_done_cb
;
4916 data
->res
.server
= server
;
4917 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4918 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4921 struct nfs4_renewdata
{
4922 struct nfs_client
*client
;
4923 unsigned long timestamp
;
4927 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4928 * standalone procedure for queueing an asynchronous RENEW.
4930 static void nfs4_renew_release(void *calldata
)
4932 struct nfs4_renewdata
*data
= calldata
;
4933 struct nfs_client
*clp
= data
->client
;
4935 if (atomic_read(&clp
->cl_count
) > 1)
4936 nfs4_schedule_state_renewal(clp
);
4937 nfs_put_client(clp
);
4941 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4943 struct nfs4_renewdata
*data
= calldata
;
4944 struct nfs_client
*clp
= data
->client
;
4945 unsigned long timestamp
= data
->timestamp
;
4947 trace_nfs4_renew_async(clp
, task
->tk_status
);
4948 switch (task
->tk_status
) {
4951 case -NFS4ERR_LEASE_MOVED
:
4952 nfs4_schedule_lease_moved_recovery(clp
);
4955 /* Unless we're shutting down, schedule state recovery! */
4956 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4958 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4959 nfs4_schedule_lease_recovery(clp
);
4962 nfs4_schedule_path_down_recovery(clp
);
4964 do_renew_lease(clp
, timestamp
);
4967 static const struct rpc_call_ops nfs4_renew_ops
= {
4968 .rpc_call_done
= nfs4_renew_done
,
4969 .rpc_release
= nfs4_renew_release
,
4972 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4974 struct rpc_message msg
= {
4975 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4979 struct nfs4_renewdata
*data
;
4981 if (renew_flags
== 0)
4983 if (!atomic_inc_not_zero(&clp
->cl_count
))
4985 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4987 nfs_put_client(clp
);
4991 data
->timestamp
= jiffies
;
4992 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4993 &nfs4_renew_ops
, data
);
4996 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4998 struct rpc_message msg
= {
4999 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
5003 unsigned long now
= jiffies
;
5006 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5009 do_renew_lease(clp
, now
);
5013 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
5015 return server
->caps
& NFS_CAP_ACLS
;
5018 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5019 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5022 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5024 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
5025 struct page
**pages
)
5027 struct page
*newpage
, **spages
;
5033 len
= min_t(size_t, PAGE_SIZE
, buflen
);
5034 newpage
= alloc_page(GFP_KERNEL
);
5036 if (newpage
== NULL
)
5038 memcpy(page_address(newpage
), buf
, len
);
5043 } while (buflen
!= 0);
5049 __free_page(spages
[rc
-1]);
5053 struct nfs4_cached_acl
{
5059 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
5061 struct nfs_inode
*nfsi
= NFS_I(inode
);
5063 spin_lock(&inode
->i_lock
);
5064 kfree(nfsi
->nfs4_acl
);
5065 nfsi
->nfs4_acl
= acl
;
5066 spin_unlock(&inode
->i_lock
);
5069 static void nfs4_zap_acl_attr(struct inode
*inode
)
5071 nfs4_set_cached_acl(inode
, NULL
);
5074 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
5076 struct nfs_inode
*nfsi
= NFS_I(inode
);
5077 struct nfs4_cached_acl
*acl
;
5080 spin_lock(&inode
->i_lock
);
5081 acl
= nfsi
->nfs4_acl
;
5084 if (buf
== NULL
) /* user is just asking for length */
5086 if (acl
->cached
== 0)
5088 ret
= -ERANGE
; /* see getxattr(2) man page */
5089 if (acl
->len
> buflen
)
5091 memcpy(buf
, acl
->data
, acl
->len
);
5095 spin_unlock(&inode
->i_lock
);
5099 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
5101 struct nfs4_cached_acl
*acl
;
5102 size_t buflen
= sizeof(*acl
) + acl_len
;
5104 if (buflen
<= PAGE_SIZE
) {
5105 acl
= kmalloc(buflen
, GFP_KERNEL
);
5109 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
5111 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
5118 nfs4_set_cached_acl(inode
, acl
);
5122 * The getxattr API returns the required buffer length when called with a
5123 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5124 * the required buf. On a NULL buf, we send a page of data to the server
5125 * guessing that the ACL request can be serviced by a page. If so, we cache
5126 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5127 * the cache. If not so, we throw away the page, and cache the required
5128 * length. The next getxattr call will then produce another round trip to
5129 * the server, this time with the input buf of the required size.
5131 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5133 struct page
*pages
[NFS4ACL_MAXPAGES
+ 1] = {NULL
, };
5134 struct nfs_getaclargs args
= {
5135 .fh
= NFS_FH(inode
),
5139 struct nfs_getaclres res
= {
5142 struct rpc_message msg
= {
5143 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
5147 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
) + 1;
5148 int ret
= -ENOMEM
, i
;
5150 if (npages
> ARRAY_SIZE(pages
))
5153 for (i
= 0; i
< npages
; i
++) {
5154 pages
[i
] = alloc_page(GFP_KERNEL
);
5159 /* for decoding across pages */
5160 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
5161 if (!res
.acl_scratch
)
5164 args
.acl_len
= npages
* PAGE_SIZE
;
5166 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5167 __func__
, buf
, buflen
, npages
, args
.acl_len
);
5168 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
5169 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5173 /* Handle the case where the passed-in buffer is too short */
5174 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
5175 /* Did the user only issue a request for the acl length? */
5181 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
5183 if (res
.acl_len
> buflen
) {
5187 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
5192 for (i
= 0; i
< npages
; i
++)
5194 __free_page(pages
[i
]);
5195 if (res
.acl_scratch
)
5196 __free_page(res
.acl_scratch
);
5200 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5202 struct nfs4_exception exception
= { };
5205 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
5206 trace_nfs4_get_acl(inode
, ret
);
5209 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
5210 } while (exception
.retry
);
5214 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
5216 struct nfs_server
*server
= NFS_SERVER(inode
);
5219 if (!nfs4_server_supports_acls(server
))
5221 ret
= nfs_revalidate_inode(server
, inode
);
5224 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
5225 nfs_zap_acl_cache(inode
);
5226 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
5228 /* -ENOENT is returned if there is no ACL or if there is an ACL
5229 * but no cached acl data, just the acl length */
5231 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
5234 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5236 struct nfs_server
*server
= NFS_SERVER(inode
);
5237 struct page
*pages
[NFS4ACL_MAXPAGES
];
5238 struct nfs_setaclargs arg
= {
5239 .fh
= NFS_FH(inode
),
5243 struct nfs_setaclres res
;
5244 struct rpc_message msg
= {
5245 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
5249 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5252 if (!nfs4_server_supports_acls(server
))
5254 if (npages
> ARRAY_SIZE(pages
))
5256 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
5259 nfs4_inode_return_delegation(inode
);
5260 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5263 * Free each page after tx, so the only ref left is
5264 * held by the network stack
5267 put_page(pages
[i
-1]);
5270 * Acl update can result in inode attribute update.
5271 * so mark the attribute cache invalid.
5273 spin_lock(&inode
->i_lock
);
5274 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
5275 spin_unlock(&inode
->i_lock
);
5276 nfs_access_zap_cache(inode
);
5277 nfs_zap_acl_cache(inode
);
5281 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5283 struct nfs4_exception exception
= { };
5286 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
5287 trace_nfs4_set_acl(inode
, err
);
5288 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5290 } while (exception
.retry
);
5294 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5295 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
5298 struct nfs_server
*server
= NFS_SERVER(inode
);
5299 struct nfs_fattr fattr
;
5300 struct nfs4_label label
= {0, 0, buflen
, buf
};
5302 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5303 struct nfs4_getattr_arg arg
= {
5304 .fh
= NFS_FH(inode
),
5307 struct nfs4_getattr_res res
= {
5312 struct rpc_message msg
= {
5313 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
5319 nfs_fattr_init(&fattr
);
5321 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
5324 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
5329 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
5332 struct nfs4_exception exception
= { };
5335 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5339 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
5340 trace_nfs4_get_security_label(inode
, err
);
5341 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5343 } while (exception
.retry
);
5347 static int _nfs4_do_set_security_label(struct inode
*inode
,
5348 struct nfs4_label
*ilabel
,
5349 struct nfs_fattr
*fattr
,
5350 struct nfs4_label
*olabel
)
5353 struct iattr sattr
= {0};
5354 struct nfs_server
*server
= NFS_SERVER(inode
);
5355 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5356 struct nfs_setattrargs arg
= {
5357 .fh
= NFS_FH(inode
),
5363 struct nfs_setattrres res
= {
5368 struct rpc_message msg
= {
5369 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
5375 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
5377 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5379 dprintk("%s failed: %d\n", __func__
, status
);
5384 static int nfs4_do_set_security_label(struct inode
*inode
,
5385 struct nfs4_label
*ilabel
,
5386 struct nfs_fattr
*fattr
,
5387 struct nfs4_label
*olabel
)
5389 struct nfs4_exception exception
= { };
5393 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5395 trace_nfs4_set_security_label(inode
, err
);
5396 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5398 } while (exception
.retry
);
5403 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5405 struct nfs4_label ilabel
, *olabel
= NULL
;
5406 struct nfs_fattr fattr
;
5407 struct rpc_cred
*cred
;
5410 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5413 nfs_fattr_init(&fattr
);
5417 ilabel
.label
= (char *)buf
;
5418 ilabel
.len
= buflen
;
5420 cred
= rpc_lookup_cred();
5422 return PTR_ERR(cred
);
5424 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5425 if (IS_ERR(olabel
)) {
5426 status
= -PTR_ERR(olabel
);
5430 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5432 nfs_setsecurity(inode
, &fattr
, olabel
);
5434 nfs4_label_free(olabel
);
5439 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5442 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5443 nfs4_verifier
*bootverf
)
5447 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5448 /* An impossible timestamp guarantees this value
5449 * will never match a generated boot time. */
5450 verf
[0] = cpu_to_be32(U32_MAX
);
5451 verf
[1] = cpu_to_be32(U32_MAX
);
5453 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5454 u64 ns
= ktime_to_ns(nn
->boot_time
);
5456 verf
[0] = cpu_to_be32(ns
>> 32);
5457 verf
[1] = cpu_to_be32(ns
);
5459 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5463 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5468 if (clp
->cl_owner_id
!= NULL
)
5472 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5473 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5475 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
5479 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5483 * Since this string is allocated at mount time, and held until the
5484 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5485 * about a memory-reclaim deadlock.
5487 str
= kmalloc(len
, GFP_KERNEL
);
5492 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5494 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5495 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5498 clp
->cl_owner_id
= str
;
5503 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5508 len
= 10 + 10 + 1 + 10 + 1 +
5509 strlen(nfs4_client_id_uniquifier
) + 1 +
5510 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5512 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5516 * Since this string is allocated at mount time, and held until the
5517 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5518 * about a memory-reclaim deadlock.
5520 str
= kmalloc(len
, GFP_KERNEL
);
5524 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5525 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5526 nfs4_client_id_uniquifier
,
5527 clp
->cl_rpcclient
->cl_nodename
);
5528 clp
->cl_owner_id
= str
;
5533 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5538 if (clp
->cl_owner_id
!= NULL
)
5541 if (nfs4_client_id_uniquifier
[0] != '\0')
5542 return nfs4_init_uniquifier_client_string(clp
);
5544 len
= 10 + 10 + 1 + 10 + 1 +
5545 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5547 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5551 * Since this string is allocated at mount time, and held until the
5552 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5553 * about a memory-reclaim deadlock.
5555 str
= kmalloc(len
, GFP_KERNEL
);
5559 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5560 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5561 clp
->cl_rpcclient
->cl_nodename
);
5562 clp
->cl_owner_id
= str
;
5567 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5568 * services. Advertise one based on the address family of the
5572 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5574 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5575 return scnprintf(buf
, len
, "tcp6");
5577 return scnprintf(buf
, len
, "tcp");
5580 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5582 struct nfs4_setclientid
*sc
= calldata
;
5584 if (task
->tk_status
== 0)
5585 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5588 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5589 .rpc_call_done
= nfs4_setclientid_done
,
5593 * nfs4_proc_setclientid - Negotiate client ID
5594 * @clp: state data structure
5595 * @program: RPC program for NFSv4 callback service
5596 * @port: IP port number for NFS4 callback service
5597 * @cred: RPC credential to use for this call
5598 * @res: where to place the result
5600 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5602 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5603 unsigned short port
, struct rpc_cred
*cred
,
5604 struct nfs4_setclientid_res
*res
)
5606 nfs4_verifier sc_verifier
;
5607 struct nfs4_setclientid setclientid
= {
5608 .sc_verifier
= &sc_verifier
,
5612 struct rpc_message msg
= {
5613 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5614 .rpc_argp
= &setclientid
,
5618 struct rpc_task
*task
;
5619 struct rpc_task_setup task_setup_data
= {
5620 .rpc_client
= clp
->cl_rpcclient
,
5621 .rpc_message
= &msg
,
5622 .callback_ops
= &nfs4_setclientid_ops
,
5623 .callback_data
= &setclientid
,
5624 .flags
= RPC_TASK_TIMEOUT
,
5628 /* nfs_client_id4 */
5629 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5631 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5632 status
= nfs4_init_uniform_client_string(clp
);
5634 status
= nfs4_init_nonuniform_client_string(clp
);
5640 setclientid
.sc_netid_len
=
5641 nfs4_init_callback_netid(clp
,
5642 setclientid
.sc_netid
,
5643 sizeof(setclientid
.sc_netid
));
5644 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5645 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5646 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5648 dprintk("NFS call setclientid auth=%s, '%s'\n",
5649 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5651 task
= rpc_run_task(&task_setup_data
);
5653 status
= PTR_ERR(task
);
5656 status
= task
->tk_status
;
5657 if (setclientid
.sc_cred
) {
5658 kfree(clp
->cl_acceptor
);
5659 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5660 put_rpccred(setclientid
.sc_cred
);
5664 trace_nfs4_setclientid(clp
, status
);
5665 dprintk("NFS reply setclientid: %d\n", status
);
5670 * nfs4_proc_setclientid_confirm - Confirm client ID
5671 * @clp: state data structure
5672 * @res: result of a previous SETCLIENTID
5673 * @cred: RPC credential to use for this call
5675 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5677 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5678 struct nfs4_setclientid_res
*arg
,
5679 struct rpc_cred
*cred
)
5681 struct rpc_message msg
= {
5682 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5688 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5689 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5691 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5692 trace_nfs4_setclientid_confirm(clp
, status
);
5693 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5697 struct nfs4_delegreturndata
{
5698 struct nfs4_delegreturnargs args
;
5699 struct nfs4_delegreturnres res
;
5701 nfs4_stateid stateid
;
5702 unsigned long timestamp
;
5704 struct nfs4_layoutreturn_args arg
;
5705 struct nfs4_layoutreturn_res res
;
5706 struct nfs4_xdr_opaque_data ld_private
;
5710 struct nfs_fattr fattr
;
5712 struct inode
*inode
;
5715 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5717 struct nfs4_delegreturndata
*data
= calldata
;
5719 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5722 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5724 /* Handle Layoutreturn errors */
5725 if (data
->args
.lr_args
&& task
->tk_status
!= 0) {
5726 switch(data
->res
.lr_ret
) {
5728 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5731 data
->args
.lr_args
= NULL
;
5732 data
->res
.lr_res
= NULL
;
5734 case -NFS4ERR_ADMIN_REVOKED
:
5735 case -NFS4ERR_DELEG_REVOKED
:
5736 case -NFS4ERR_EXPIRED
:
5737 case -NFS4ERR_BAD_STATEID
:
5738 case -NFS4ERR_OLD_STATEID
:
5739 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
5740 case -NFS4ERR_WRONG_CRED
:
5741 data
->args
.lr_args
= NULL
;
5742 data
->res
.lr_res
= NULL
;
5743 data
->res
.lr_ret
= 0;
5744 rpc_restart_call_prepare(task
);
5749 switch (task
->tk_status
) {
5751 renew_lease(data
->res
.server
, data
->timestamp
);
5753 case -NFS4ERR_ADMIN_REVOKED
:
5754 case -NFS4ERR_DELEG_REVOKED
:
5755 case -NFS4ERR_EXPIRED
:
5756 nfs4_free_revoked_stateid(data
->res
.server
,
5758 task
->tk_msg
.rpc_cred
);
5759 case -NFS4ERR_BAD_STATEID
:
5760 case -NFS4ERR_OLD_STATEID
:
5761 case -NFS4ERR_STALE_STATEID
:
5762 task
->tk_status
= 0;
5764 case -NFS4ERR_ACCESS
:
5765 if (data
->args
.bitmask
) {
5766 data
->args
.bitmask
= NULL
;
5767 data
->res
.fattr
= NULL
;
5768 task
->tk_status
= 0;
5769 rpc_restart_call_prepare(task
);
5773 if (nfs4_async_handle_error(task
, data
->res
.server
,
5774 NULL
, NULL
) == -EAGAIN
) {
5775 rpc_restart_call_prepare(task
);
5779 data
->rpc_status
= task
->tk_status
;
5782 static void nfs4_delegreturn_release(void *calldata
)
5784 struct nfs4_delegreturndata
*data
= calldata
;
5785 struct inode
*inode
= data
->inode
;
5789 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
,
5791 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5792 nfs_iput_and_deactive(inode
);
5797 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5799 struct nfs4_delegreturndata
*d_data
;
5801 d_data
= (struct nfs4_delegreturndata
*)data
;
5803 if (!d_data
->lr
.roc
&& nfs4_wait_on_layoutreturn(d_data
->inode
, task
)) {
5804 nfs4_sequence_done(task
, &d_data
->res
.seq_res
);
5808 nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
5809 &d_data
->args
.seq_args
,
5810 &d_data
->res
.seq_res
,
5814 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5815 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5816 .rpc_call_done
= nfs4_delegreturn_done
,
5817 .rpc_release
= nfs4_delegreturn_release
,
5820 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5822 struct nfs4_delegreturndata
*data
;
5823 struct nfs_server
*server
= NFS_SERVER(inode
);
5824 struct rpc_task
*task
;
5825 struct rpc_message msg
= {
5826 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5829 struct rpc_task_setup task_setup_data
= {
5830 .rpc_client
= server
->client
,
5831 .rpc_message
= &msg
,
5832 .callback_ops
= &nfs4_delegreturn_ops
,
5833 .flags
= RPC_TASK_ASYNC
,
5837 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5840 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5842 nfs4_state_protect(server
->nfs_client
,
5843 NFS_SP4_MACH_CRED_CLEANUP
,
5844 &task_setup_data
.rpc_client
, &msg
);
5846 data
->args
.fhandle
= &data
->fh
;
5847 data
->args
.stateid
= &data
->stateid
;
5848 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5849 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5850 nfs4_stateid_copy(&data
->stateid
, stateid
);
5851 data
->res
.fattr
= &data
->fattr
;
5852 data
->res
.server
= server
;
5853 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5854 data
->lr
.arg
.ld_private
= &data
->lr
.ld_private
;
5855 nfs_fattr_init(data
->res
.fattr
);
5856 data
->timestamp
= jiffies
;
5857 data
->rpc_status
= 0;
5858 data
->lr
.roc
= pnfs_roc(inode
, &data
->lr
.arg
, &data
->lr
.res
, cred
);
5859 data
->inode
= nfs_igrab_and_active(inode
);
5862 data
->args
.lr_args
= &data
->lr
.arg
;
5863 data
->res
.lr_res
= &data
->lr
.res
;
5865 } else if (data
->lr
.roc
) {
5866 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
, 0);
5867 data
->lr
.roc
= false;
5870 task_setup_data
.callback_data
= data
;
5871 msg
.rpc_argp
= &data
->args
;
5872 msg
.rpc_resp
= &data
->res
;
5873 task
= rpc_run_task(&task_setup_data
);
5875 return PTR_ERR(task
);
5878 status
= rpc_wait_for_completion_task(task
);
5881 status
= data
->rpc_status
;
5887 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5889 struct nfs_server
*server
= NFS_SERVER(inode
);
5890 struct nfs4_exception exception
= { };
5893 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5894 trace_nfs4_delegreturn(inode
, stateid
, err
);
5896 case -NFS4ERR_STALE_STATEID
:
5897 case -NFS4ERR_EXPIRED
:
5901 err
= nfs4_handle_exception(server
, err
, &exception
);
5902 } while (exception
.retry
);
5906 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5908 struct inode
*inode
= state
->inode
;
5909 struct nfs_server
*server
= NFS_SERVER(inode
);
5910 struct nfs_client
*clp
= server
->nfs_client
;
5911 struct nfs_lockt_args arg
= {
5912 .fh
= NFS_FH(inode
),
5915 struct nfs_lockt_res res
= {
5918 struct rpc_message msg
= {
5919 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5922 .rpc_cred
= state
->owner
->so_cred
,
5924 struct nfs4_lock_state
*lsp
;
5927 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5928 status
= nfs4_set_lock_state(state
, request
);
5931 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5932 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5933 arg
.lock_owner
.s_dev
= server
->s_dev
;
5934 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5937 request
->fl_type
= F_UNLCK
;
5939 case -NFS4ERR_DENIED
:
5942 request
->fl_ops
->fl_release_private(request
);
5943 request
->fl_ops
= NULL
;
5948 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5950 struct nfs4_exception exception
= { };
5954 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5955 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5956 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5958 } while (exception
.retry
);
5962 struct nfs4_unlockdata
{
5963 struct nfs_locku_args arg
;
5964 struct nfs_locku_res res
;
5965 struct nfs4_lock_state
*lsp
;
5966 struct nfs_open_context
*ctx
;
5967 struct nfs_lock_context
*l_ctx
;
5968 struct file_lock fl
;
5969 struct nfs_server
*server
;
5970 unsigned long timestamp
;
5973 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5974 struct nfs_open_context
*ctx
,
5975 struct nfs4_lock_state
*lsp
,
5976 struct nfs_seqid
*seqid
)
5978 struct nfs4_unlockdata
*p
;
5979 struct inode
*inode
= lsp
->ls_state
->inode
;
5981 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5984 p
->arg
.fh
= NFS_FH(inode
);
5986 p
->arg
.seqid
= seqid
;
5987 p
->res
.seqid
= seqid
;
5989 atomic_inc(&lsp
->ls_count
);
5990 /* Ensure we don't close file until we're done freeing locks! */
5991 p
->ctx
= get_nfs_open_context(ctx
);
5992 p
->l_ctx
= nfs_get_lock_context(ctx
);
5993 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5994 p
->server
= NFS_SERVER(inode
);
5998 static void nfs4_locku_release_calldata(void *data
)
6000 struct nfs4_unlockdata
*calldata
= data
;
6001 nfs_free_seqid(calldata
->arg
.seqid
);
6002 nfs4_put_lock_state(calldata
->lsp
);
6003 nfs_put_lock_context(calldata
->l_ctx
);
6004 put_nfs_open_context(calldata
->ctx
);
6008 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
6010 struct nfs4_unlockdata
*calldata
= data
;
6012 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
6014 switch (task
->tk_status
) {
6016 renew_lease(calldata
->server
, calldata
->timestamp
);
6017 locks_lock_inode_wait(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
6018 if (nfs4_update_lock_stateid(calldata
->lsp
,
6019 &calldata
->res
.stateid
))
6021 case -NFS4ERR_ADMIN_REVOKED
:
6022 case -NFS4ERR_EXPIRED
:
6023 nfs4_free_revoked_stateid(calldata
->server
,
6024 &calldata
->arg
.stateid
,
6025 task
->tk_msg
.rpc_cred
);
6026 case -NFS4ERR_BAD_STATEID
:
6027 case -NFS4ERR_OLD_STATEID
:
6028 case -NFS4ERR_STALE_STATEID
:
6029 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
6030 &calldata
->lsp
->ls_stateid
))
6031 rpc_restart_call_prepare(task
);
6034 if (nfs4_async_handle_error(task
, calldata
->server
,
6035 NULL
, NULL
) == -EAGAIN
)
6036 rpc_restart_call_prepare(task
);
6038 nfs_release_seqid(calldata
->arg
.seqid
);
6041 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
6043 struct nfs4_unlockdata
*calldata
= data
;
6045 if (test_bit(NFS_CONTEXT_UNLOCK
, &calldata
->l_ctx
->open_context
->flags
) &&
6046 nfs_async_iocounter_wait(task
, calldata
->l_ctx
))
6049 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
6051 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
6052 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
6053 /* Note: exit _without_ running nfs4_locku_done */
6056 calldata
->timestamp
= jiffies
;
6057 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
6058 &calldata
->arg
.seq_args
,
6059 &calldata
->res
.seq_res
,
6061 nfs_release_seqid(calldata
->arg
.seqid
);
6064 task
->tk_action
= NULL
;
6066 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
6069 static const struct rpc_call_ops nfs4_locku_ops
= {
6070 .rpc_call_prepare
= nfs4_locku_prepare
,
6071 .rpc_call_done
= nfs4_locku_done
,
6072 .rpc_release
= nfs4_locku_release_calldata
,
6075 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
6076 struct nfs_open_context
*ctx
,
6077 struct nfs4_lock_state
*lsp
,
6078 struct nfs_seqid
*seqid
)
6080 struct nfs4_unlockdata
*data
;
6081 struct rpc_message msg
= {
6082 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
6083 .rpc_cred
= ctx
->cred
,
6085 struct rpc_task_setup task_setup_data
= {
6086 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
6087 .rpc_message
= &msg
,
6088 .callback_ops
= &nfs4_locku_ops
,
6089 .workqueue
= nfsiod_workqueue
,
6090 .flags
= RPC_TASK_ASYNC
,
6093 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
6094 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
6096 /* Ensure this is an unlock - when canceling a lock, the
6097 * canceled lock is passed in, and it won't be an unlock.
6099 fl
->fl_type
= F_UNLCK
;
6100 if (fl
->fl_flags
& FL_CLOSE
)
6101 set_bit(NFS_CONTEXT_UNLOCK
, &ctx
->flags
);
6103 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
6105 nfs_free_seqid(seqid
);
6106 return ERR_PTR(-ENOMEM
);
6109 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6110 msg
.rpc_argp
= &data
->arg
;
6111 msg
.rpc_resp
= &data
->res
;
6112 task_setup_data
.callback_data
= data
;
6113 return rpc_run_task(&task_setup_data
);
6116 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6118 struct inode
*inode
= state
->inode
;
6119 struct nfs4_state_owner
*sp
= state
->owner
;
6120 struct nfs_inode
*nfsi
= NFS_I(inode
);
6121 struct nfs_seqid
*seqid
;
6122 struct nfs4_lock_state
*lsp
;
6123 struct rpc_task
*task
;
6124 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6126 unsigned char fl_flags
= request
->fl_flags
;
6128 status
= nfs4_set_lock_state(state
, request
);
6129 /* Unlock _before_ we do the RPC call */
6130 request
->fl_flags
|= FL_EXISTS
;
6131 /* Exclude nfs_delegation_claim_locks() */
6132 mutex_lock(&sp
->so_delegreturn_mutex
);
6133 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6134 down_read(&nfsi
->rwsem
);
6135 if (locks_lock_inode_wait(inode
, request
) == -ENOENT
) {
6136 up_read(&nfsi
->rwsem
);
6137 mutex_unlock(&sp
->so_delegreturn_mutex
);
6140 up_read(&nfsi
->rwsem
);
6141 mutex_unlock(&sp
->so_delegreturn_mutex
);
6144 /* Is this a delegated lock? */
6145 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6146 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
6148 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
6149 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
6153 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
6154 status
= PTR_ERR(task
);
6157 status
= rpc_wait_for_completion_task(task
);
6160 request
->fl_flags
= fl_flags
;
6161 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
6165 struct nfs4_lockdata
{
6166 struct nfs_lock_args arg
;
6167 struct nfs_lock_res res
;
6168 struct nfs4_lock_state
*lsp
;
6169 struct nfs_open_context
*ctx
;
6170 struct file_lock fl
;
6171 unsigned long timestamp
;
6174 struct nfs_server
*server
;
6177 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
6178 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
6181 struct nfs4_lockdata
*p
;
6182 struct inode
*inode
= lsp
->ls_state
->inode
;
6183 struct nfs_server
*server
= NFS_SERVER(inode
);
6184 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6186 p
= kzalloc(sizeof(*p
), gfp_mask
);
6190 p
->arg
.fh
= NFS_FH(inode
);
6192 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
6193 if (IS_ERR(p
->arg
.open_seqid
))
6195 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
6196 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
6197 if (IS_ERR(p
->arg
.lock_seqid
))
6198 goto out_free_seqid
;
6199 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6200 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6201 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
6202 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
6205 atomic_inc(&lsp
->ls_count
);
6206 p
->ctx
= get_nfs_open_context(ctx
);
6207 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
6210 nfs_free_seqid(p
->arg
.open_seqid
);
6216 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
6218 struct nfs4_lockdata
*data
= calldata
;
6219 struct nfs4_state
*state
= data
->lsp
->ls_state
;
6221 dprintk("%s: begin!\n", __func__
);
6222 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
6224 /* Do we need to do an open_to_lock_owner? */
6225 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
6226 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
6227 goto out_release_lock_seqid
;
6229 nfs4_stateid_copy(&data
->arg
.open_stateid
,
6230 &state
->open_stateid
);
6231 data
->arg
.new_lock_owner
= 1;
6232 data
->res
.open_seqid
= data
->arg
.open_seqid
;
6234 data
->arg
.new_lock_owner
= 0;
6235 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
6236 &data
->lsp
->ls_stateid
);
6238 if (!nfs4_valid_open_stateid(state
)) {
6239 data
->rpc_status
= -EBADF
;
6240 task
->tk_action
= NULL
;
6241 goto out_release_open_seqid
;
6243 data
->timestamp
= jiffies
;
6244 if (nfs4_setup_sequence(data
->server
->nfs_client
,
6245 &data
->arg
.seq_args
,
6249 out_release_open_seqid
:
6250 nfs_release_seqid(data
->arg
.open_seqid
);
6251 out_release_lock_seqid
:
6252 nfs_release_seqid(data
->arg
.lock_seqid
);
6254 nfs4_sequence_done(task
, &data
->res
.seq_res
);
6255 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
6258 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
6260 struct nfs4_lockdata
*data
= calldata
;
6261 struct nfs4_lock_state
*lsp
= data
->lsp
;
6263 dprintk("%s: begin!\n", __func__
);
6265 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
6268 data
->rpc_status
= task
->tk_status
;
6269 switch (task
->tk_status
) {
6271 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
6273 if (data
->arg
.new_lock
) {
6274 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
6275 if (locks_lock_inode_wait(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
6276 rpc_restart_call_prepare(task
);
6280 if (data
->arg
.new_lock_owner
!= 0) {
6281 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
6282 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
6283 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6284 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
6285 rpc_restart_call_prepare(task
);
6287 case -NFS4ERR_BAD_STATEID
:
6288 case -NFS4ERR_OLD_STATEID
:
6289 case -NFS4ERR_STALE_STATEID
:
6290 case -NFS4ERR_EXPIRED
:
6291 if (data
->arg
.new_lock_owner
!= 0) {
6292 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
6293 &lsp
->ls_state
->open_stateid
))
6294 rpc_restart_call_prepare(task
);
6295 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
6297 rpc_restart_call_prepare(task
);
6299 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
6302 static void nfs4_lock_release(void *calldata
)
6304 struct nfs4_lockdata
*data
= calldata
;
6306 dprintk("%s: begin!\n", __func__
);
6307 nfs_free_seqid(data
->arg
.open_seqid
);
6308 if (data
->cancelled
) {
6309 struct rpc_task
*task
;
6310 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
6311 data
->arg
.lock_seqid
);
6313 rpc_put_task_async(task
);
6314 dprintk("%s: cancelling lock!\n", __func__
);
6316 nfs_free_seqid(data
->arg
.lock_seqid
);
6317 nfs4_put_lock_state(data
->lsp
);
6318 put_nfs_open_context(data
->ctx
);
6320 dprintk("%s: done!\n", __func__
);
6323 static const struct rpc_call_ops nfs4_lock_ops
= {
6324 .rpc_call_prepare
= nfs4_lock_prepare
,
6325 .rpc_call_done
= nfs4_lock_done
,
6326 .rpc_release
= nfs4_lock_release
,
6329 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
6332 case -NFS4ERR_ADMIN_REVOKED
:
6333 case -NFS4ERR_EXPIRED
:
6334 case -NFS4ERR_BAD_STATEID
:
6335 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6336 if (new_lock_owner
!= 0 ||
6337 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
6338 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
6340 case -NFS4ERR_STALE_STATEID
:
6341 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6342 nfs4_schedule_lease_recovery(server
->nfs_client
);
6346 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
6348 struct nfs4_lockdata
*data
;
6349 struct rpc_task
*task
;
6350 struct rpc_message msg
= {
6351 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
6352 .rpc_cred
= state
->owner
->so_cred
,
6354 struct rpc_task_setup task_setup_data
= {
6355 .rpc_client
= NFS_CLIENT(state
->inode
),
6356 .rpc_message
= &msg
,
6357 .callback_ops
= &nfs4_lock_ops
,
6358 .workqueue
= nfsiod_workqueue
,
6359 .flags
= RPC_TASK_ASYNC
,
6363 dprintk("%s: begin!\n", __func__
);
6364 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
6365 fl
->fl_u
.nfs4_fl
.owner
,
6366 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
6370 data
->arg
.block
= 1;
6371 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6372 msg
.rpc_argp
= &data
->arg
;
6373 msg
.rpc_resp
= &data
->res
;
6374 task_setup_data
.callback_data
= data
;
6375 if (recovery_type
> NFS_LOCK_NEW
) {
6376 if (recovery_type
== NFS_LOCK_RECLAIM
)
6377 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
6378 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
6380 data
->arg
.new_lock
= 1;
6381 task
= rpc_run_task(&task_setup_data
);
6383 return PTR_ERR(task
);
6384 ret
= rpc_wait_for_completion_task(task
);
6386 ret
= data
->rpc_status
;
6388 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
6389 data
->arg
.new_lock_owner
, ret
);
6391 data
->cancelled
= true;
6393 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
6394 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
6398 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
6400 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6401 struct nfs4_exception exception
= {
6402 .inode
= state
->inode
,
6407 /* Cache the lock if possible... */
6408 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6410 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
6411 if (err
!= -NFS4ERR_DELAY
)
6413 nfs4_handle_exception(server
, err
, &exception
);
6414 } while (exception
.retry
);
6418 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6420 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6421 struct nfs4_exception exception
= {
6422 .inode
= state
->inode
,
6426 err
= nfs4_set_lock_state(state
, request
);
6429 if (!recover_lost_locks
) {
6430 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6434 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6436 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6440 case -NFS4ERR_GRACE
:
6441 case -NFS4ERR_DELAY
:
6442 nfs4_handle_exception(server
, err
, &exception
);
6445 } while (exception
.retry
);
6450 #if defined(CONFIG_NFS_V4_1)
6451 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6453 struct nfs4_lock_state
*lsp
;
6456 status
= nfs4_set_lock_state(state
, request
);
6459 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6460 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) ||
6461 test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
6463 return nfs4_lock_expired(state
, request
);
6467 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6469 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6470 struct nfs4_state_owner
*sp
= state
->owner
;
6471 unsigned char fl_flags
= request
->fl_flags
;
6474 request
->fl_flags
|= FL_ACCESS
;
6475 status
= locks_lock_inode_wait(state
->inode
, request
);
6478 mutex_lock(&sp
->so_delegreturn_mutex
);
6479 down_read(&nfsi
->rwsem
);
6480 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6481 /* Yes: cache locks! */
6482 /* ...but avoid races with delegation recall... */
6483 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6484 status
= locks_lock_inode_wait(state
->inode
, request
);
6485 up_read(&nfsi
->rwsem
);
6486 mutex_unlock(&sp
->so_delegreturn_mutex
);
6489 up_read(&nfsi
->rwsem
);
6490 mutex_unlock(&sp
->so_delegreturn_mutex
);
6491 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6493 request
->fl_flags
= fl_flags
;
6497 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6499 struct nfs4_exception exception
= {
6501 .inode
= state
->inode
,
6506 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6507 if (err
== -NFS4ERR_DENIED
)
6509 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6511 } while (exception
.retry
);
6515 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6516 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6519 nfs4_retry_setlk_simple(struct nfs4_state
*state
, int cmd
,
6520 struct file_lock
*request
)
6522 int status
= -ERESTARTSYS
;
6523 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6525 while(!signalled()) {
6526 status
= nfs4_proc_setlk(state
, cmd
, request
);
6527 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6529 freezable_schedule_timeout_interruptible(timeout
);
6531 timeout
= min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT
, timeout
);
6532 status
= -ERESTARTSYS
;
6537 #ifdef CONFIG_NFS_V4_1
6538 struct nfs4_lock_waiter
{
6539 struct task_struct
*task
;
6540 struct inode
*inode
;
6541 struct nfs_lowner
*owner
;
6546 nfs4_wake_lock_waiter(wait_queue_entry_t
*wait
, unsigned int mode
, int flags
, void *key
)
6549 struct cb_notify_lock_args
*cbnl
= key
;
6550 struct nfs4_lock_waiter
*waiter
= wait
->private;
6551 struct nfs_lowner
*lowner
= &cbnl
->cbnl_owner
,
6552 *wowner
= waiter
->owner
;
6554 /* Only wake if the callback was for the same owner */
6555 if (lowner
->clientid
!= wowner
->clientid
||
6556 lowner
->id
!= wowner
->id
||
6557 lowner
->s_dev
!= wowner
->s_dev
)
6560 /* Make sure it's for the right inode */
6561 if (nfs_compare_fh(NFS_FH(waiter
->inode
), &cbnl
->cbnl_fh
))
6564 waiter
->notified
= true;
6566 /* override "private" so we can use default_wake_function */
6567 wait
->private = waiter
->task
;
6568 ret
= autoremove_wake_function(wait
, mode
, flags
, key
);
6569 wait
->private = waiter
;
6574 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6576 int status
= -ERESTARTSYS
;
6577 unsigned long flags
;
6578 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
6579 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6580 struct nfs_client
*clp
= server
->nfs_client
;
6581 wait_queue_head_t
*q
= &clp
->cl_lock_waitq
;
6582 struct nfs_lowner owner
= { .clientid
= clp
->cl_clientid
,
6583 .id
= lsp
->ls_seqid
.owner_id
,
6584 .s_dev
= server
->s_dev
};
6585 struct nfs4_lock_waiter waiter
= { .task
= current
,
6586 .inode
= state
->inode
,
6588 .notified
= false };
6589 wait_queue_entry_t wait
;
6591 /* Don't bother with waitqueue if we don't expect a callback */
6592 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
))
6593 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6596 wait
.private = &waiter
;
6597 wait
.func
= nfs4_wake_lock_waiter
;
6598 add_wait_queue(q
, &wait
);
6600 while(!signalled()) {
6601 status
= nfs4_proc_setlk(state
, cmd
, request
);
6602 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6605 status
= -ERESTARTSYS
;
6606 spin_lock_irqsave(&q
->lock
, flags
);
6607 if (waiter
.notified
) {
6608 spin_unlock_irqrestore(&q
->lock
, flags
);
6611 set_current_state(TASK_INTERRUPTIBLE
);
6612 spin_unlock_irqrestore(&q
->lock
, flags
);
6614 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT
);
6617 finish_wait(q
, &wait
);
6620 #else /* !CONFIG_NFS_V4_1 */
6622 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6624 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6629 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6631 struct nfs_open_context
*ctx
;
6632 struct nfs4_state
*state
;
6635 /* verify open state */
6636 ctx
= nfs_file_open_context(filp
);
6639 if (IS_GETLK(cmd
)) {
6641 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6645 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6648 if (request
->fl_type
== F_UNLCK
) {
6650 return nfs4_proc_unlck(state
, cmd
, request
);
6657 if ((request
->fl_flags
& FL_POSIX
) &&
6658 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6662 * Don't rely on the VFS having checked the file open mode,
6663 * since it won't do this for flock() locks.
6665 switch (request
->fl_type
) {
6667 if (!(filp
->f_mode
& FMODE_READ
))
6671 if (!(filp
->f_mode
& FMODE_WRITE
))
6675 status
= nfs4_set_lock_state(state
, request
);
6679 return nfs4_retry_setlk(state
, cmd
, request
);
6682 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6684 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6687 err
= nfs4_set_lock_state(state
, fl
);
6691 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6692 if (err
!= -NFS4ERR_DELAY
)
6695 } while (err
== -NFS4ERR_DELAY
);
6696 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, fl
, err
);
6699 struct nfs_release_lockowner_data
{
6700 struct nfs4_lock_state
*lsp
;
6701 struct nfs_server
*server
;
6702 struct nfs_release_lockowner_args args
;
6703 struct nfs_release_lockowner_res res
;
6704 unsigned long timestamp
;
6707 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6709 struct nfs_release_lockowner_data
*data
= calldata
;
6710 struct nfs_server
*server
= data
->server
;
6711 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
6712 &data
->res
.seq_res
, task
);
6713 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6714 data
->timestamp
= jiffies
;
6717 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6719 struct nfs_release_lockowner_data
*data
= calldata
;
6720 struct nfs_server
*server
= data
->server
;
6722 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6724 switch (task
->tk_status
) {
6726 renew_lease(server
, data
->timestamp
);
6728 case -NFS4ERR_STALE_CLIENTID
:
6729 case -NFS4ERR_EXPIRED
:
6730 nfs4_schedule_lease_recovery(server
->nfs_client
);
6732 case -NFS4ERR_LEASE_MOVED
:
6733 case -NFS4ERR_DELAY
:
6734 if (nfs4_async_handle_error(task
, server
,
6735 NULL
, NULL
) == -EAGAIN
)
6736 rpc_restart_call_prepare(task
);
6740 static void nfs4_release_lockowner_release(void *calldata
)
6742 struct nfs_release_lockowner_data
*data
= calldata
;
6743 nfs4_free_lock_state(data
->server
, data
->lsp
);
6747 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6748 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6749 .rpc_call_done
= nfs4_release_lockowner_done
,
6750 .rpc_release
= nfs4_release_lockowner_release
,
6754 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6756 struct nfs_release_lockowner_data
*data
;
6757 struct rpc_message msg
= {
6758 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6761 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6764 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6768 data
->server
= server
;
6769 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6770 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6771 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6773 msg
.rpc_argp
= &data
->args
;
6774 msg
.rpc_resp
= &data
->res
;
6775 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6776 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6779 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6781 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6782 struct dentry
*unused
, struct inode
*inode
,
6783 const char *key
, const void *buf
,
6784 size_t buflen
, int flags
)
6786 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6789 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6790 struct dentry
*unused
, struct inode
*inode
,
6791 const char *key
, void *buf
, size_t buflen
)
6793 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6796 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6798 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6801 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6803 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6804 struct dentry
*unused
, struct inode
*inode
,
6805 const char *key
, const void *buf
,
6806 size_t buflen
, int flags
)
6808 if (security_ismaclabel(key
))
6809 return nfs4_set_security_label(inode
, buf
, buflen
);
6814 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6815 struct dentry
*unused
, struct inode
*inode
,
6816 const char *key
, void *buf
, size_t buflen
)
6818 if (security_ismaclabel(key
))
6819 return nfs4_get_security_label(inode
, buf
, buflen
);
6824 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6828 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6829 len
= security_inode_listsecurity(inode
, list
, list_len
);
6830 if (list_len
&& len
> list_len
)
6836 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6837 .prefix
= XATTR_SECURITY_PREFIX
,
6838 .get
= nfs4_xattr_get_nfs4_label
,
6839 .set
= nfs4_xattr_set_nfs4_label
,
6845 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6853 * nfs_fhget will use either the mounted_on_fileid or the fileid
6855 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6857 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6858 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6859 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6860 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6863 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6864 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6865 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6869 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6870 const struct qstr
*name
,
6871 struct nfs4_fs_locations
*fs_locations
,
6874 struct nfs_server
*server
= NFS_SERVER(dir
);
6876 struct nfs4_fs_locations_arg args
= {
6877 .dir_fh
= NFS_FH(dir
),
6882 struct nfs4_fs_locations_res res
= {
6883 .fs_locations
= fs_locations
,
6885 struct rpc_message msg
= {
6886 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6892 dprintk("%s: start\n", __func__
);
6894 bitmask
[0] = nfs4_fattr_bitmap
[0] | FATTR4_WORD0_FS_LOCATIONS
;
6895 bitmask
[1] = nfs4_fattr_bitmap
[1];
6897 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6898 * is not supported */
6899 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6900 bitmask
[0] &= ~FATTR4_WORD0_FILEID
;
6902 bitmask
[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID
;
6904 nfs_fattr_init(&fs_locations
->fattr
);
6905 fs_locations
->server
= server
;
6906 fs_locations
->nlocations
= 0;
6907 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6908 dprintk("%s: returned status = %d\n", __func__
, status
);
6912 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6913 const struct qstr
*name
,
6914 struct nfs4_fs_locations
*fs_locations
,
6917 struct nfs4_exception exception
= { };
6920 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6921 fs_locations
, page
);
6922 trace_nfs4_get_fs_locations(dir
, name
, err
);
6923 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6925 } while (exception
.retry
);
6930 * This operation also signals the server that this client is
6931 * performing migration recovery. The server can stop returning
6932 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6933 * appended to this compound to identify the client ID which is
6934 * performing recovery.
6936 static int _nfs40_proc_get_locations(struct inode
*inode
,
6937 struct nfs4_fs_locations
*locations
,
6938 struct page
*page
, struct rpc_cred
*cred
)
6940 struct nfs_server
*server
= NFS_SERVER(inode
);
6941 struct rpc_clnt
*clnt
= server
->client
;
6943 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6945 struct nfs4_fs_locations_arg args
= {
6946 .clientid
= server
->nfs_client
->cl_clientid
,
6947 .fh
= NFS_FH(inode
),
6950 .migration
= 1, /* skip LOOKUP */
6951 .renew
= 1, /* append RENEW */
6953 struct nfs4_fs_locations_res res
= {
6954 .fs_locations
= locations
,
6958 struct rpc_message msg
= {
6959 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6964 unsigned long now
= jiffies
;
6967 nfs_fattr_init(&locations
->fattr
);
6968 locations
->server
= server
;
6969 locations
->nlocations
= 0;
6971 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6972 nfs4_set_sequence_privileged(&args
.seq_args
);
6973 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6974 &args
.seq_args
, &res
.seq_res
);
6978 renew_lease(server
, now
);
6982 #ifdef CONFIG_NFS_V4_1
6985 * This operation also signals the server that this client is
6986 * performing migration recovery. The server can stop asserting
6987 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6988 * performing this operation is identified in the SEQUENCE
6989 * operation in this compound.
6991 * When the client supports GETATTR(fs_locations_info), it can
6992 * be plumbed in here.
6994 static int _nfs41_proc_get_locations(struct inode
*inode
,
6995 struct nfs4_fs_locations
*locations
,
6996 struct page
*page
, struct rpc_cred
*cred
)
6998 struct nfs_server
*server
= NFS_SERVER(inode
);
6999 struct rpc_clnt
*clnt
= server
->client
;
7001 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
7003 struct nfs4_fs_locations_arg args
= {
7004 .fh
= NFS_FH(inode
),
7007 .migration
= 1, /* skip LOOKUP */
7009 struct nfs4_fs_locations_res res
= {
7010 .fs_locations
= locations
,
7013 struct rpc_message msg
= {
7014 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
7021 nfs_fattr_init(&locations
->fattr
);
7022 locations
->server
= server
;
7023 locations
->nlocations
= 0;
7025 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7026 nfs4_set_sequence_privileged(&args
.seq_args
);
7027 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7028 &args
.seq_args
, &res
.seq_res
);
7029 if (status
== NFS4_OK
&&
7030 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
7031 status
= -NFS4ERR_LEASE_MOVED
;
7035 #endif /* CONFIG_NFS_V4_1 */
7038 * nfs4_proc_get_locations - discover locations for a migrated FSID
7039 * @inode: inode on FSID that is migrating
7040 * @locations: result of query
7042 * @cred: credential to use for this operation
7044 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7045 * operation failed, or a negative errno if a local error occurred.
7047 * On success, "locations" is filled in, but if the server has
7048 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7051 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7052 * from this client that require migration recovery.
7054 int nfs4_proc_get_locations(struct inode
*inode
,
7055 struct nfs4_fs_locations
*locations
,
7056 struct page
*page
, struct rpc_cred
*cred
)
7058 struct nfs_server
*server
= NFS_SERVER(inode
);
7059 struct nfs_client
*clp
= server
->nfs_client
;
7060 const struct nfs4_mig_recovery_ops
*ops
=
7061 clp
->cl_mvops
->mig_recovery_ops
;
7062 struct nfs4_exception exception
= { };
7065 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
7066 (unsigned long long)server
->fsid
.major
,
7067 (unsigned long long)server
->fsid
.minor
,
7069 nfs_display_fhandle(NFS_FH(inode
), __func__
);
7072 status
= ops
->get_locations(inode
, locations
, page
, cred
);
7073 if (status
!= -NFS4ERR_DELAY
)
7075 nfs4_handle_exception(server
, status
, &exception
);
7076 } while (exception
.retry
);
7081 * This operation also signals the server that this client is
7082 * performing "lease moved" recovery. The server can stop
7083 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7084 * is appended to this compound to identify the client ID which is
7085 * performing recovery.
7087 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7089 struct nfs_server
*server
= NFS_SERVER(inode
);
7090 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
7091 struct rpc_clnt
*clnt
= server
->client
;
7092 struct nfs4_fsid_present_arg args
= {
7093 .fh
= NFS_FH(inode
),
7094 .clientid
= clp
->cl_clientid
,
7095 .renew
= 1, /* append RENEW */
7097 struct nfs4_fsid_present_res res
= {
7100 struct rpc_message msg
= {
7101 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7106 unsigned long now
= jiffies
;
7109 res
.fh
= nfs_alloc_fhandle();
7113 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7114 nfs4_set_sequence_privileged(&args
.seq_args
);
7115 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7116 &args
.seq_args
, &res
.seq_res
);
7117 nfs_free_fhandle(res
.fh
);
7121 do_renew_lease(clp
, now
);
7125 #ifdef CONFIG_NFS_V4_1
7128 * This operation also signals the server that this client is
7129 * performing "lease moved" recovery. The server can stop asserting
7130 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7131 * this operation is identified in the SEQUENCE operation in this
7134 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7136 struct nfs_server
*server
= NFS_SERVER(inode
);
7137 struct rpc_clnt
*clnt
= server
->client
;
7138 struct nfs4_fsid_present_arg args
= {
7139 .fh
= NFS_FH(inode
),
7141 struct nfs4_fsid_present_res res
= {
7143 struct rpc_message msg
= {
7144 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7151 res
.fh
= nfs_alloc_fhandle();
7155 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7156 nfs4_set_sequence_privileged(&args
.seq_args
);
7157 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7158 &args
.seq_args
, &res
.seq_res
);
7159 nfs_free_fhandle(res
.fh
);
7160 if (status
== NFS4_OK
&&
7161 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
7162 status
= -NFS4ERR_LEASE_MOVED
;
7166 #endif /* CONFIG_NFS_V4_1 */
7169 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7170 * @inode: inode on FSID to check
7171 * @cred: credential to use for this operation
7173 * Server indicates whether the FSID is present, moved, or not
7174 * recognized. This operation is necessary to clear a LEASE_MOVED
7175 * condition for this client ID.
7177 * Returns NFS4_OK if the FSID is present on this server,
7178 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7179 * NFS4ERR code if some error occurred on the server, or a
7180 * negative errno if a local failure occurred.
7182 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7184 struct nfs_server
*server
= NFS_SERVER(inode
);
7185 struct nfs_client
*clp
= server
->nfs_client
;
7186 const struct nfs4_mig_recovery_ops
*ops
=
7187 clp
->cl_mvops
->mig_recovery_ops
;
7188 struct nfs4_exception exception
= { };
7191 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
7192 (unsigned long long)server
->fsid
.major
,
7193 (unsigned long long)server
->fsid
.minor
,
7195 nfs_display_fhandle(NFS_FH(inode
), __func__
);
7198 status
= ops
->fsid_present(inode
, cred
);
7199 if (status
!= -NFS4ERR_DELAY
)
7201 nfs4_handle_exception(server
, status
, &exception
);
7202 } while (exception
.retry
);
7207 * If 'use_integrity' is true and the state managment nfs_client
7208 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7209 * and the machine credential as per RFC3530bis and RFC5661 Security
7210 * Considerations sections. Otherwise, just use the user cred with the
7211 * filesystem's rpc_client.
7213 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7216 struct nfs4_secinfo_arg args
= {
7217 .dir_fh
= NFS_FH(dir
),
7220 struct nfs4_secinfo_res res
= {
7223 struct rpc_message msg
= {
7224 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
7228 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
7229 struct rpc_cred
*cred
= NULL
;
7231 if (use_integrity
) {
7232 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
7233 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
7234 msg
.rpc_cred
= cred
;
7237 dprintk("NFS call secinfo %s\n", name
->name
);
7239 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
7240 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
7242 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
7244 dprintk("NFS reply secinfo: %d\n", status
);
7252 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
7253 struct nfs4_secinfo_flavors
*flavors
)
7255 struct nfs4_exception exception
= { };
7258 err
= -NFS4ERR_WRONGSEC
;
7260 /* try to use integrity protection with machine cred */
7261 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
7262 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
7265 * if unable to use integrity protection, or SECINFO with
7266 * integrity protection returns NFS4ERR_WRONGSEC (which is
7267 * disallowed by spec, but exists in deployed servers) use
7268 * the current filesystem's rpc_client and the user cred.
7270 if (err
== -NFS4ERR_WRONGSEC
)
7271 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
7273 trace_nfs4_secinfo(dir
, name
, err
);
7274 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
7276 } while (exception
.retry
);
7280 #ifdef CONFIG_NFS_V4_1
7282 * Check the exchange flags returned by the server for invalid flags, having
7283 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7286 static int nfs4_check_cl_exchange_flags(u32 flags
, u32 version
)
7288 if (version
>= 2 && (flags
& ~EXCHGID4_2_FLAG_MASK_R
))
7290 else if (version
< 2 && (flags
& ~EXCHGID4_FLAG_MASK_R
))
7292 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
7293 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
7295 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
7299 return -NFS4ERR_INVAL
;
7303 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
7304 struct nfs41_server_scope
*b
)
7306 if (a
->server_scope_sz
!= b
->server_scope_sz
)
7308 return memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0;
7312 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
7316 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
7317 .rpc_call_done
= nfs4_bind_one_conn_to_session_done
,
7321 * nfs4_proc_bind_one_conn_to_session()
7323 * The 4.1 client currently uses the same TCP connection for the
7324 * fore and backchannel.
7327 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
7328 struct rpc_xprt
*xprt
,
7329 struct nfs_client
*clp
,
7330 struct rpc_cred
*cred
)
7333 struct nfs41_bind_conn_to_session_args args
= {
7335 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
7337 struct nfs41_bind_conn_to_session_res res
;
7338 struct rpc_message msg
= {
7340 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
7345 struct rpc_task_setup task_setup_data
= {
7348 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
7349 .rpc_message
= &msg
,
7350 .flags
= RPC_TASK_TIMEOUT
,
7352 struct rpc_task
*task
;
7354 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
7355 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
7356 args
.dir
= NFS4_CDFC4_FORE
;
7358 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7359 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
7360 args
.dir
= NFS4_CDFC4_FORE
;
7362 task
= rpc_run_task(&task_setup_data
);
7363 if (!IS_ERR(task
)) {
7364 status
= task
->tk_status
;
7367 status
= PTR_ERR(task
);
7368 trace_nfs4_bind_conn_to_session(clp
, status
);
7370 if (memcmp(res
.sessionid
.data
,
7371 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
7372 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
7375 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
7376 dprintk("NFS: %s: Unexpected direction from server\n",
7380 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
7381 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7390 struct rpc_bind_conn_calldata
{
7391 struct nfs_client
*clp
;
7392 struct rpc_cred
*cred
;
7396 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
7397 struct rpc_xprt
*xprt
,
7400 struct rpc_bind_conn_calldata
*p
= calldata
;
7402 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
7405 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7407 struct rpc_bind_conn_calldata data
= {
7411 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
7412 nfs4_proc_bind_conn_to_session_callback
, &data
);
7416 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7417 * and operations we'd like to see to enable certain features in the allow map
7419 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
7420 .how
= SP4_MACH_CRED
,
7421 .enforce
.u
.words
= {
7422 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7423 1 << (OP_EXCHANGE_ID
- 32) |
7424 1 << (OP_CREATE_SESSION
- 32) |
7425 1 << (OP_DESTROY_SESSION
- 32) |
7426 1 << (OP_DESTROY_CLIENTID
- 32)
7429 [0] = 1 << (OP_CLOSE
) |
7430 1 << (OP_OPEN_DOWNGRADE
) |
7432 1 << (OP_DELEGRETURN
) |
7434 [1] = 1 << (OP_SECINFO
- 32) |
7435 1 << (OP_SECINFO_NO_NAME
- 32) |
7436 1 << (OP_LAYOUTRETURN
- 32) |
7437 1 << (OP_TEST_STATEID
- 32) |
7438 1 << (OP_FREE_STATEID
- 32) |
7439 1 << (OP_WRITE
- 32)
7444 * Select the state protection mode for client `clp' given the server results
7445 * from exchange_id in `sp'.
7447 * Returns 0 on success, negative errno otherwise.
7449 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
7450 struct nfs41_state_protection
*sp
)
7452 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
7453 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7454 1 << (OP_EXCHANGE_ID
- 32) |
7455 1 << (OP_CREATE_SESSION
- 32) |
7456 1 << (OP_DESTROY_SESSION
- 32) |
7457 1 << (OP_DESTROY_CLIENTID
- 32)
7459 unsigned long flags
= 0;
7463 if (sp
->how
== SP4_MACH_CRED
) {
7464 /* Print state protect result */
7465 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
7466 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
7467 if (test_bit(i
, sp
->enforce
.u
.longs
))
7468 dfprintk(MOUNT
, " enforce op %d\n", i
);
7469 if (test_bit(i
, sp
->allow
.u
.longs
))
7470 dfprintk(MOUNT
, " allow op %d\n", i
);
7473 /* make sure nothing is on enforce list that isn't supported */
7474 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
7475 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
7476 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7483 * Minimal mode - state operations are allowed to use machine
7484 * credential. Note this already happens by default, so the
7485 * client doesn't have to do anything more than the negotiation.
7487 * NOTE: we don't care if EXCHANGE_ID is in the list -
7488 * we're already using the machine cred for exchange_id
7489 * and will never use a different cred.
7491 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
7492 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
7493 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
7494 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
7495 dfprintk(MOUNT
, "sp4_mach_cred:\n");
7496 dfprintk(MOUNT
, " minimal mode enabled\n");
7497 __set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &flags
);
7499 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7504 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7505 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7506 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7507 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7508 dfprintk(MOUNT
, " cleanup mode enabled\n");
7509 __set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &flags
);
7512 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7513 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7514 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
, &flags
);
7517 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7518 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7519 dfprintk(MOUNT
, " secinfo mode enabled\n");
7520 __set_bit(NFS_SP4_MACH_CRED_SECINFO
, &flags
);
7523 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7524 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7525 dfprintk(MOUNT
, " stateid mode enabled\n");
7526 __set_bit(NFS_SP4_MACH_CRED_STATEID
, &flags
);
7529 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7530 dfprintk(MOUNT
, " write mode enabled\n");
7531 __set_bit(NFS_SP4_MACH_CRED_WRITE
, &flags
);
7534 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7535 dfprintk(MOUNT
, " commit mode enabled\n");
7536 __set_bit(NFS_SP4_MACH_CRED_COMMIT
, &flags
);
7540 clp
->cl_sp4_flags
= flags
;
7544 struct nfs41_exchange_id_data
{
7545 struct nfs41_exchange_id_res res
;
7546 struct nfs41_exchange_id_args args
;
7549 static void nfs4_exchange_id_release(void *data
)
7551 struct nfs41_exchange_id_data
*cdata
=
7552 (struct nfs41_exchange_id_data
*)data
;
7554 nfs_put_client(cdata
->args
.client
);
7555 kfree(cdata
->res
.impl_id
);
7556 kfree(cdata
->res
.server_scope
);
7557 kfree(cdata
->res
.server_owner
);
7561 static const struct rpc_call_ops nfs4_exchange_id_call_ops
= {
7562 .rpc_release
= nfs4_exchange_id_release
,
7566 * _nfs4_proc_exchange_id()
7568 * Wrapper for EXCHANGE_ID operation.
7570 static struct rpc_task
*
7571 nfs4_run_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7572 u32 sp4_how
, struct rpc_xprt
*xprt
)
7574 struct rpc_message msg
= {
7575 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7578 struct rpc_task_setup task_setup_data
= {
7579 .rpc_client
= clp
->cl_rpcclient
,
7580 .callback_ops
= &nfs4_exchange_id_call_ops
,
7581 .rpc_message
= &msg
,
7582 .flags
= RPC_TASK_TIMEOUT
,
7584 struct nfs41_exchange_id_data
*calldata
;
7587 if (!atomic_inc_not_zero(&clp
->cl_count
))
7588 return ERR_PTR(-EIO
);
7591 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7595 nfs4_init_boot_verifier(clp
, &calldata
->args
.verifier
);
7597 status
= nfs4_init_uniform_client_string(clp
);
7601 calldata
->res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7604 if (unlikely(calldata
->res
.server_owner
== NULL
))
7607 calldata
->res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7609 if (unlikely(calldata
->res
.server_scope
== NULL
))
7610 goto out_server_owner
;
7612 calldata
->res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7613 if (unlikely(calldata
->res
.impl_id
== NULL
))
7614 goto out_server_scope
;
7618 calldata
->args
.state_protect
.how
= SP4_NONE
;
7622 calldata
->args
.state_protect
= nfs4_sp4_mach_cred_request
;
7632 task_setup_data
.rpc_xprt
= xprt
;
7633 task_setup_data
.flags
|= RPC_TASK_SOFTCONN
;
7634 memcpy(calldata
->args
.verifier
.data
, clp
->cl_confirm
.data
,
7635 sizeof(calldata
->args
.verifier
.data
));
7637 calldata
->args
.client
= clp
;
7638 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7639 EXCHGID4_FLAG_BIND_PRINC_STATEID
;
7640 #ifdef CONFIG_NFS_V4_1_MIGRATION
7641 calldata
->args
.flags
|= EXCHGID4_FLAG_SUPP_MOVED_MIGR
;
7643 msg
.rpc_argp
= &calldata
->args
;
7644 msg
.rpc_resp
= &calldata
->res
;
7645 task_setup_data
.callback_data
= calldata
;
7647 return rpc_run_task(&task_setup_data
);
7650 kfree(calldata
->res
.impl_id
);
7652 kfree(calldata
->res
.server_scope
);
7654 kfree(calldata
->res
.server_owner
);
7658 nfs_put_client(clp
);
7659 return ERR_PTR(status
);
7663 * _nfs4_proc_exchange_id()
7665 * Wrapper for EXCHANGE_ID operation.
7667 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7670 struct rpc_task
*task
;
7671 struct nfs41_exchange_id_args
*argp
;
7672 struct nfs41_exchange_id_res
*resp
;
7675 task
= nfs4_run_exchange_id(clp
, cred
, sp4_how
, NULL
);
7677 return PTR_ERR(task
);
7679 argp
= task
->tk_msg
.rpc_argp
;
7680 resp
= task
->tk_msg
.rpc_resp
;
7681 status
= task
->tk_status
;
7685 status
= nfs4_check_cl_exchange_flags(resp
->flags
,
7686 clp
->cl_mvops
->minor_version
);
7690 status
= nfs4_sp4_select_mode(clp
, &resp
->state_protect
);
7694 clp
->cl_clientid
= resp
->clientid
;
7695 clp
->cl_exchange_flags
= resp
->flags
;
7696 clp
->cl_seqid
= resp
->seqid
;
7697 /* Client ID is not confirmed */
7698 if (!(resp
->flags
& EXCHGID4_FLAG_CONFIRMED_R
))
7699 clear_bit(NFS4_SESSION_ESTABLISHED
,
7700 &clp
->cl_session
->session_state
);
7702 if (clp
->cl_serverscope
!= NULL
&&
7703 !nfs41_same_server_scope(clp
->cl_serverscope
,
7704 resp
->server_scope
)) {
7705 dprintk("%s: server_scope mismatch detected\n",
7707 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7710 swap(clp
->cl_serverowner
, resp
->server_owner
);
7711 swap(clp
->cl_serverscope
, resp
->server_scope
);
7712 swap(clp
->cl_implid
, resp
->impl_id
);
7714 /* Save the EXCHANGE_ID verifier session trunk tests */
7715 memcpy(clp
->cl_confirm
.data
, argp
->verifier
.data
,
7716 sizeof(clp
->cl_confirm
.data
));
7718 trace_nfs4_exchange_id(clp
, status
);
7724 * nfs4_proc_exchange_id()
7726 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7728 * Since the clientid has expired, all compounds using sessions
7729 * associated with the stale clientid will be returning
7730 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7731 * be in some phase of session reset.
7733 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7735 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7737 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7740 /* try SP4_MACH_CRED if krb5i/p */
7741 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7742 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7743 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
);
7749 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
);
7753 * nfs4_test_session_trunk
7755 * This is an add_xprt_test() test function called from
7756 * rpc_clnt_setup_test_and_add_xprt.
7758 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7759 * and is dereferrenced in nfs4_exchange_id_release
7761 * Upon success, add the new transport to the rpc_clnt
7763 * @clnt: struct rpc_clnt to get new transport
7764 * @xprt: the rpc_xprt to test
7765 * @data: call data for _nfs4_proc_exchange_id.
7767 int nfs4_test_session_trunk(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
,
7770 struct nfs4_add_xprt_data
*adata
= (struct nfs4_add_xprt_data
*)data
;
7771 struct rpc_task
*task
;
7776 dprintk("--> %s try %s\n", __func__
,
7777 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
7779 sp4_how
= (adata
->clp
->cl_sp4_flags
== 0 ? SP4_NONE
: SP4_MACH_CRED
);
7781 /* Test connection for session trunking. Async exchange_id call */
7782 task
= nfs4_run_exchange_id(adata
->clp
, adata
->cred
, sp4_how
, xprt
);
7784 return PTR_ERR(task
);
7786 status
= task
->tk_status
;
7788 status
= nfs4_detect_session_trunking(adata
->clp
,
7789 task
->tk_msg
.rpc_resp
, xprt
);
7794 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk
);
7796 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7797 struct rpc_cred
*cred
)
7799 struct rpc_message msg
= {
7800 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7806 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7807 trace_nfs4_destroy_clientid(clp
, status
);
7809 dprintk("NFS: Got error %d from the server %s on "
7810 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7814 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7815 struct rpc_cred
*cred
)
7820 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7821 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7823 case -NFS4ERR_DELAY
:
7824 case -NFS4ERR_CLIENTID_BUSY
:
7834 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7836 struct rpc_cred
*cred
;
7839 if (clp
->cl_mvops
->minor_version
< 1)
7841 if (clp
->cl_exchange_flags
== 0)
7843 if (clp
->cl_preserve_clid
)
7845 cred
= nfs4_get_clid_cred(clp
);
7846 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7851 case -NFS4ERR_STALE_CLIENTID
:
7852 clp
->cl_exchange_flags
= 0;
7858 struct nfs4_get_lease_time_data
{
7859 struct nfs4_get_lease_time_args
*args
;
7860 struct nfs4_get_lease_time_res
*res
;
7861 struct nfs_client
*clp
;
7864 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7867 struct nfs4_get_lease_time_data
*data
=
7868 (struct nfs4_get_lease_time_data
*)calldata
;
7870 dprintk("--> %s\n", __func__
);
7871 /* just setup sequence, do not trigger session recovery
7872 since we're invoked within one */
7873 nfs4_setup_sequence(data
->clp
,
7874 &data
->args
->la_seq_args
,
7875 &data
->res
->lr_seq_res
,
7877 dprintk("<-- %s\n", __func__
);
7881 * Called from nfs4_state_manager thread for session setup, so don't recover
7882 * from sequence operation or clientid errors.
7884 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7886 struct nfs4_get_lease_time_data
*data
=
7887 (struct nfs4_get_lease_time_data
*)calldata
;
7889 dprintk("--> %s\n", __func__
);
7890 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7892 switch (task
->tk_status
) {
7893 case -NFS4ERR_DELAY
:
7894 case -NFS4ERR_GRACE
:
7895 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7896 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7897 task
->tk_status
= 0;
7899 case -NFS4ERR_RETRY_UNCACHED_REP
:
7900 rpc_restart_call_prepare(task
);
7903 dprintk("<-- %s\n", __func__
);
7906 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7907 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7908 .rpc_call_done
= nfs4_get_lease_time_done
,
7911 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7913 struct rpc_task
*task
;
7914 struct nfs4_get_lease_time_args args
;
7915 struct nfs4_get_lease_time_res res
= {
7916 .lr_fsinfo
= fsinfo
,
7918 struct nfs4_get_lease_time_data data
= {
7923 struct rpc_message msg
= {
7924 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7928 struct rpc_task_setup task_setup
= {
7929 .rpc_client
= clp
->cl_rpcclient
,
7930 .rpc_message
= &msg
,
7931 .callback_ops
= &nfs4_get_lease_time_ops
,
7932 .callback_data
= &data
,
7933 .flags
= RPC_TASK_TIMEOUT
,
7937 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7938 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7939 task
= rpc_run_task(&task_setup
);
7942 return PTR_ERR(task
);
7944 status
= task
->tk_status
;
7950 * Initialize the values to be used by the client in CREATE_SESSION
7951 * If nfs4_init_session set the fore channel request and response sizes,
7954 * Set the back channel max_resp_sz_cached to zero to force the client to
7955 * always set csa_cachethis to FALSE because the current implementation
7956 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7958 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7959 struct rpc_clnt
*clnt
)
7961 unsigned int max_rqst_sz
, max_resp_sz
;
7962 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7964 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7965 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7967 /* Fore channel attributes */
7968 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7969 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7970 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7971 args
->fc_attrs
.max_reqs
= max_session_slots
;
7973 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7974 "max_ops=%u max_reqs=%u\n",
7976 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7977 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7979 /* Back channel attributes */
7980 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7981 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7982 args
->bc_attrs
.max_resp_sz_cached
= 0;
7983 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7984 args
->bc_attrs
.max_reqs
= min_t(unsigned short, max_session_cb_slots
, 1);
7986 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7987 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7989 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7990 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7991 args
->bc_attrs
.max_reqs
);
7994 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7995 struct nfs41_create_session_res
*res
)
7997 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7998 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
8000 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
8003 * Our requested max_ops is the minimum we need; we're not
8004 * prepared to break up compounds into smaller pieces than that.
8005 * So, no point even trying to continue if the server won't
8008 if (rcvd
->max_ops
< sent
->max_ops
)
8010 if (rcvd
->max_reqs
== 0)
8012 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
8013 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
8017 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
8018 struct nfs41_create_session_res
*res
)
8020 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
8021 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
8023 if (!(res
->flags
& SESSION4_BACK_CHAN
))
8025 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
8027 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
8029 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
8031 if (rcvd
->max_ops
> sent
->max_ops
)
8033 if (rcvd
->max_reqs
> sent
->max_reqs
)
8039 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
8040 struct nfs41_create_session_res
*res
)
8044 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
8047 return nfs4_verify_back_channel_attrs(args
, res
);
8050 static void nfs4_update_session(struct nfs4_session
*session
,
8051 struct nfs41_create_session_res
*res
)
8053 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
8054 /* Mark client id and session as being confirmed */
8055 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
8056 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
8057 session
->flags
= res
->flags
;
8058 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
8059 if (res
->flags
& SESSION4_BACK_CHAN
)
8060 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
8061 sizeof(session
->bc_attrs
));
8064 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
8065 struct rpc_cred
*cred
)
8067 struct nfs4_session
*session
= clp
->cl_session
;
8068 struct nfs41_create_session_args args
= {
8070 .clientid
= clp
->cl_clientid
,
8071 .seqid
= clp
->cl_seqid
,
8072 .cb_program
= NFS4_CALLBACK
,
8074 struct nfs41_create_session_res res
;
8076 struct rpc_message msg
= {
8077 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
8084 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
8085 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
8087 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8088 trace_nfs4_create_session(clp
, status
);
8091 case -NFS4ERR_STALE_CLIENTID
:
8092 case -NFS4ERR_DELAY
:
8101 /* Verify the session's negotiated channel_attrs values */
8102 status
= nfs4_verify_channel_attrs(&args
, &res
);
8103 /* Increment the clientid slot sequence id */
8106 nfs4_update_session(session
, &res
);
8113 * Issues a CREATE_SESSION operation to the server.
8114 * It is the responsibility of the caller to verify the session is
8115 * expired before calling this routine.
8117 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8121 struct nfs4_session
*session
= clp
->cl_session
;
8123 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
8125 status
= _nfs4_proc_create_session(clp
, cred
);
8129 /* Init or reset the session slot tables */
8130 status
= nfs4_setup_session_slot_tables(session
);
8131 dprintk("slot table setup returned %d\n", status
);
8135 ptr
= (unsigned *)&session
->sess_id
.data
[0];
8136 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
8137 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
8139 dprintk("<-- %s\n", __func__
);
8144 * Issue the over-the-wire RPC DESTROY_SESSION.
8145 * The caller must serialize access to this routine.
8147 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
8148 struct rpc_cred
*cred
)
8150 struct rpc_message msg
= {
8151 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
8152 .rpc_argp
= session
,
8157 dprintk("--> nfs4_proc_destroy_session\n");
8159 /* session is still being setup */
8160 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
8163 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8164 trace_nfs4_destroy_session(session
->clp
, status
);
8167 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8168 "Session has been destroyed regardless...\n", status
);
8170 dprintk("<-- nfs4_proc_destroy_session\n");
8175 * Renew the cl_session lease.
8177 struct nfs4_sequence_data
{
8178 struct nfs_client
*clp
;
8179 struct nfs4_sequence_args args
;
8180 struct nfs4_sequence_res res
;
8183 static void nfs41_sequence_release(void *data
)
8185 struct nfs4_sequence_data
*calldata
= data
;
8186 struct nfs_client
*clp
= calldata
->clp
;
8188 if (atomic_read(&clp
->cl_count
) > 1)
8189 nfs4_schedule_state_renewal(clp
);
8190 nfs_put_client(clp
);
8194 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8196 switch(task
->tk_status
) {
8197 case -NFS4ERR_DELAY
:
8198 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8201 nfs4_schedule_lease_recovery(clp
);
8206 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
8208 struct nfs4_sequence_data
*calldata
= data
;
8209 struct nfs_client
*clp
= calldata
->clp
;
8211 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
8214 trace_nfs4_sequence(clp
, task
->tk_status
);
8215 if (task
->tk_status
< 0) {
8216 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
8217 if (atomic_read(&clp
->cl_count
) == 1)
8220 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
8221 rpc_restart_call_prepare(task
);
8225 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
8227 dprintk("<-- %s\n", __func__
);
8230 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
8232 struct nfs4_sequence_data
*calldata
= data
;
8233 struct nfs_client
*clp
= calldata
->clp
;
8234 struct nfs4_sequence_args
*args
;
8235 struct nfs4_sequence_res
*res
;
8237 args
= task
->tk_msg
.rpc_argp
;
8238 res
= task
->tk_msg
.rpc_resp
;
8240 nfs4_setup_sequence(clp
, args
, res
, task
);
8243 static const struct rpc_call_ops nfs41_sequence_ops
= {
8244 .rpc_call_done
= nfs41_sequence_call_done
,
8245 .rpc_call_prepare
= nfs41_sequence_prepare
,
8246 .rpc_release
= nfs41_sequence_release
,
8249 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
8250 struct rpc_cred
*cred
,
8251 struct nfs4_slot
*slot
,
8254 struct nfs4_sequence_data
*calldata
;
8255 struct rpc_message msg
= {
8256 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
8259 struct rpc_task_setup task_setup_data
= {
8260 .rpc_client
= clp
->cl_rpcclient
,
8261 .rpc_message
= &msg
,
8262 .callback_ops
= &nfs41_sequence_ops
,
8263 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
8265 struct rpc_task
*ret
;
8267 ret
= ERR_PTR(-EIO
);
8268 if (!atomic_inc_not_zero(&clp
->cl_count
))
8271 ret
= ERR_PTR(-ENOMEM
);
8272 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8273 if (calldata
== NULL
)
8275 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
8276 nfs4_sequence_attach_slot(&calldata
->args
, &calldata
->res
, slot
);
8278 nfs4_set_sequence_privileged(&calldata
->args
);
8279 msg
.rpc_argp
= &calldata
->args
;
8280 msg
.rpc_resp
= &calldata
->res
;
8281 calldata
->clp
= clp
;
8282 task_setup_data
.callback_data
= calldata
;
8284 ret
= rpc_run_task(&task_setup_data
);
8289 nfs_put_client(clp
);
8291 nfs41_release_slot(slot
);
8295 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
8297 struct rpc_task
*task
;
8300 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
8302 task
= _nfs41_proc_sequence(clp
, cred
, NULL
, false);
8304 ret
= PTR_ERR(task
);
8306 rpc_put_task_async(task
);
8307 dprintk("<-- %s status=%d\n", __func__
, ret
);
8311 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8313 struct rpc_task
*task
;
8316 task
= _nfs41_proc_sequence(clp
, cred
, NULL
, true);
8318 ret
= PTR_ERR(task
);
8321 ret
= rpc_wait_for_completion_task(task
);
8323 ret
= task
->tk_status
;
8326 dprintk("<-- %s status=%d\n", __func__
, ret
);
8330 struct nfs4_reclaim_complete_data
{
8331 struct nfs_client
*clp
;
8332 struct nfs41_reclaim_complete_args arg
;
8333 struct nfs41_reclaim_complete_res res
;
8336 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
8338 struct nfs4_reclaim_complete_data
*calldata
= data
;
8340 nfs4_setup_sequence(calldata
->clp
,
8341 &calldata
->arg
.seq_args
,
8342 &calldata
->res
.seq_res
,
8346 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8348 switch(task
->tk_status
) {
8350 case -NFS4ERR_COMPLETE_ALREADY
:
8351 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
8353 case -NFS4ERR_DELAY
:
8354 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8356 case -NFS4ERR_RETRY_UNCACHED_REP
:
8358 case -NFS4ERR_BADSESSION
:
8359 case -NFS4ERR_DEADSESSION
:
8360 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
8361 nfs4_schedule_session_recovery(clp
->cl_session
,
8365 nfs4_schedule_lease_recovery(clp
);
8370 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
8372 struct nfs4_reclaim_complete_data
*calldata
= data
;
8373 struct nfs_client
*clp
= calldata
->clp
;
8374 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
8376 dprintk("--> %s\n", __func__
);
8377 if (!nfs41_sequence_done(task
, res
))
8380 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
8381 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
8382 rpc_restart_call_prepare(task
);
8385 dprintk("<-- %s\n", __func__
);
8388 static void nfs4_free_reclaim_complete_data(void *data
)
8390 struct nfs4_reclaim_complete_data
*calldata
= data
;
8395 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
8396 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
8397 .rpc_call_done
= nfs4_reclaim_complete_done
,
8398 .rpc_release
= nfs4_free_reclaim_complete_data
,
8402 * Issue a global reclaim complete.
8404 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
8405 struct rpc_cred
*cred
)
8407 struct nfs4_reclaim_complete_data
*calldata
;
8408 struct rpc_task
*task
;
8409 struct rpc_message msg
= {
8410 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
8413 struct rpc_task_setup task_setup_data
= {
8414 .rpc_client
= clp
->cl_rpcclient
,
8415 .rpc_message
= &msg
,
8416 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
8417 .flags
= RPC_TASK_ASYNC
,
8419 int status
= -ENOMEM
;
8421 dprintk("--> %s\n", __func__
);
8422 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8423 if (calldata
== NULL
)
8425 calldata
->clp
= clp
;
8426 calldata
->arg
.one_fs
= 0;
8428 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
8429 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
8430 msg
.rpc_argp
= &calldata
->arg
;
8431 msg
.rpc_resp
= &calldata
->res
;
8432 task_setup_data
.callback_data
= calldata
;
8433 task
= rpc_run_task(&task_setup_data
);
8435 status
= PTR_ERR(task
);
8438 status
= rpc_wait_for_completion_task(task
);
8440 status
= task
->tk_status
;
8443 dprintk("<-- %s status=%d\n", __func__
, status
);
8448 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
8450 struct nfs4_layoutget
*lgp
= calldata
;
8451 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
8453 dprintk("--> %s\n", __func__
);
8454 nfs4_setup_sequence(server
->nfs_client
, &lgp
->args
.seq_args
,
8455 &lgp
->res
.seq_res
, task
);
8456 dprintk("<-- %s\n", __func__
);
8459 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
8461 struct nfs4_layoutget
*lgp
= calldata
;
8463 dprintk("--> %s\n", __func__
);
8464 nfs41_sequence_process(task
, &lgp
->res
.seq_res
);
8465 dprintk("<-- %s\n", __func__
);
8469 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
8470 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
8472 struct inode
*inode
= lgp
->args
.inode
;
8473 struct nfs_server
*server
= NFS_SERVER(inode
);
8474 struct pnfs_layout_hdr
*lo
;
8475 int nfs4err
= task
->tk_status
;
8476 int err
, status
= 0;
8479 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
8481 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8488 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8489 * on the file. set tk_status to -ENODATA to tell upper layer to
8492 case -NFS4ERR_LAYOUTUNAVAILABLE
:
8496 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8497 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8499 case -NFS4ERR_BADLAYOUT
:
8500 status
= -EOVERFLOW
;
8503 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8504 * (or clients) writing to the same RAID stripe except when
8505 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8507 * Treat it like we would RECALLCONFLICT -- we retry for a little
8508 * while, and then eventually give up.
8510 case -NFS4ERR_LAYOUTTRYLATER
:
8511 if (lgp
->args
.minlength
== 0) {
8512 status
= -EOVERFLOW
;
8517 case -NFS4ERR_RECALLCONFLICT
:
8518 status
= -ERECALLCONFLICT
;
8520 case -NFS4ERR_DELEG_REVOKED
:
8521 case -NFS4ERR_ADMIN_REVOKED
:
8522 case -NFS4ERR_EXPIRED
:
8523 case -NFS4ERR_BAD_STATEID
:
8524 exception
->timeout
= 0;
8525 spin_lock(&inode
->i_lock
);
8526 lo
= NFS_I(inode
)->layout
;
8527 /* If the open stateid was bad, then recover it. */
8528 if (!lo
|| test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) ||
8529 !nfs4_stateid_match_other(&lgp
->args
.stateid
, &lo
->plh_stateid
)) {
8530 spin_unlock(&inode
->i_lock
);
8531 exception
->state
= lgp
->args
.ctx
->state
;
8532 exception
->stateid
= &lgp
->args
.stateid
;
8537 * Mark the bad layout state as invalid, then retry
8539 pnfs_mark_layout_stateid_invalid(lo
, &head
);
8540 spin_unlock(&inode
->i_lock
);
8541 nfs_commit_inode(inode
, 0);
8542 pnfs_free_lseg_list(&head
);
8547 err
= nfs4_handle_exception(server
, nfs4err
, exception
);
8549 if (exception
->retry
)
8555 dprintk("<-- %s\n", __func__
);
8559 static size_t max_response_pages(struct nfs_server
*server
)
8561 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
8562 return nfs_page_array_len(0, max_resp_sz
);
8565 static void nfs4_free_pages(struct page
**pages
, size_t size
)
8572 for (i
= 0; i
< size
; i
++) {
8575 __free_page(pages
[i
]);
8580 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
8582 struct page
**pages
;
8585 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
8587 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
8591 for (i
= 0; i
< size
; i
++) {
8592 pages
[i
] = alloc_page(gfp_flags
);
8594 dprintk("%s: failed to allocate page\n", __func__
);
8595 nfs4_free_pages(pages
, size
);
8603 static void nfs4_layoutget_release(void *calldata
)
8605 struct nfs4_layoutget
*lgp
= calldata
;
8606 struct inode
*inode
= lgp
->args
.inode
;
8607 struct nfs_server
*server
= NFS_SERVER(inode
);
8608 size_t max_pages
= max_response_pages(server
);
8610 dprintk("--> %s\n", __func__
);
8611 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8612 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8613 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8614 put_nfs_open_context(lgp
->args
.ctx
);
8616 dprintk("<-- %s\n", __func__
);
8619 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8620 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8621 .rpc_call_done
= nfs4_layoutget_done
,
8622 .rpc_release
= nfs4_layoutget_release
,
8625 struct pnfs_layout_segment
*
8626 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8628 struct inode
*inode
= lgp
->args
.inode
;
8629 struct nfs_server
*server
= NFS_SERVER(inode
);
8630 size_t max_pages
= max_response_pages(server
);
8631 struct rpc_task
*task
;
8632 struct rpc_message msg
= {
8633 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8634 .rpc_argp
= &lgp
->args
,
8635 .rpc_resp
= &lgp
->res
,
8636 .rpc_cred
= lgp
->cred
,
8638 struct rpc_task_setup task_setup_data
= {
8639 .rpc_client
= server
->client
,
8640 .rpc_message
= &msg
,
8641 .callback_ops
= &nfs4_layoutget_call_ops
,
8642 .callback_data
= lgp
,
8643 .flags
= RPC_TASK_ASYNC
,
8645 struct pnfs_layout_segment
*lseg
= NULL
;
8646 struct nfs4_exception exception
= {
8648 .timeout
= *timeout
,
8652 dprintk("--> %s\n", __func__
);
8654 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8655 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8657 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8658 if (!lgp
->args
.layout
.pages
) {
8659 nfs4_layoutget_release(lgp
);
8660 return ERR_PTR(-ENOMEM
);
8662 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8664 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8665 lgp
->res
.seq_res
.sr_slot
= NULL
;
8666 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8668 task
= rpc_run_task(&task_setup_data
);
8670 return ERR_CAST(task
);
8671 status
= rpc_wait_for_completion_task(task
);
8675 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8676 if (task
->tk_status
< 0 || lgp
->res
.layoutp
->len
== 0) {
8677 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8678 *timeout
= exception
.timeout
;
8680 lseg
= pnfs_layout_process(lgp
);
8682 trace_nfs4_layoutget(lgp
->args
.ctx
,
8689 dprintk("<-- %s status=%d\n", __func__
, status
);
8691 return ERR_PTR(status
);
8696 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8698 struct nfs4_layoutreturn
*lrp
= calldata
;
8700 dprintk("--> %s\n", __func__
);
8701 nfs4_setup_sequence(lrp
->clp
,
8702 &lrp
->args
.seq_args
,
8707 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8709 struct nfs4_layoutreturn
*lrp
= calldata
;
8710 struct nfs_server
*server
;
8712 dprintk("--> %s\n", __func__
);
8714 if (!nfs41_sequence_process(task
, &lrp
->res
.seq_res
))
8717 server
= NFS_SERVER(lrp
->args
.inode
);
8718 switch (task
->tk_status
) {
8720 task
->tk_status
= 0;
8723 case -NFS4ERR_DELAY
:
8724 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8726 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8727 rpc_restart_call_prepare(task
);
8730 dprintk("<-- %s\n", __func__
);
8733 static void nfs4_layoutreturn_release(void *calldata
)
8735 struct nfs4_layoutreturn
*lrp
= calldata
;
8736 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8738 dprintk("--> %s\n", __func__
);
8739 pnfs_layoutreturn_free_lsegs(lo
, &lrp
->args
.stateid
, &lrp
->args
.range
,
8740 lrp
->res
.lrs_present
? &lrp
->res
.stateid
: NULL
);
8741 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8742 if (lrp
->ld_private
.ops
&& lrp
->ld_private
.ops
->free
)
8743 lrp
->ld_private
.ops
->free(&lrp
->ld_private
);
8744 pnfs_put_layout_hdr(lrp
->args
.layout
);
8745 nfs_iput_and_deactive(lrp
->inode
);
8747 dprintk("<-- %s\n", __func__
);
8750 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8751 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8752 .rpc_call_done
= nfs4_layoutreturn_done
,
8753 .rpc_release
= nfs4_layoutreturn_release
,
8756 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8758 struct rpc_task
*task
;
8759 struct rpc_message msg
= {
8760 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8761 .rpc_argp
= &lrp
->args
,
8762 .rpc_resp
= &lrp
->res
,
8763 .rpc_cred
= lrp
->cred
,
8765 struct rpc_task_setup task_setup_data
= {
8766 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8767 .rpc_message
= &msg
,
8768 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8769 .callback_data
= lrp
,
8773 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8774 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8775 &task_setup_data
.rpc_client
, &msg
);
8777 dprintk("--> %s\n", __func__
);
8779 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8781 nfs4_layoutreturn_release(lrp
);
8784 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8786 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8787 task
= rpc_run_task(&task_setup_data
);
8789 return PTR_ERR(task
);
8791 status
= task
->tk_status
;
8792 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8793 dprintk("<-- %s status=%d\n", __func__
, status
);
8799 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8800 struct pnfs_device
*pdev
,
8801 struct rpc_cred
*cred
)
8803 struct nfs4_getdeviceinfo_args args
= {
8805 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8806 NOTIFY_DEVICEID4_DELETE
,
8808 struct nfs4_getdeviceinfo_res res
= {
8811 struct rpc_message msg
= {
8812 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8819 dprintk("--> %s\n", __func__
);
8820 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8821 if (res
.notification
& ~args
.notify_types
)
8822 dprintk("%s: unsupported notification\n", __func__
);
8823 if (res
.notification
!= args
.notify_types
)
8826 dprintk("<-- %s status=%d\n", __func__
, status
);
8831 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8832 struct pnfs_device
*pdev
,
8833 struct rpc_cred
*cred
)
8835 struct nfs4_exception exception
= { };
8839 err
= nfs4_handle_exception(server
,
8840 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8842 } while (exception
.retry
);
8845 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8847 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8849 struct nfs4_layoutcommit_data
*data
= calldata
;
8850 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8852 nfs4_setup_sequence(server
->nfs_client
,
8853 &data
->args
.seq_args
,
8859 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8861 struct nfs4_layoutcommit_data
*data
= calldata
;
8862 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8864 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8867 switch (task
->tk_status
) { /* Just ignore these failures */
8868 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8869 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8870 case -NFS4ERR_BADLAYOUT
: /* no layout */
8871 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8872 task
->tk_status
= 0;
8876 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8877 rpc_restart_call_prepare(task
);
8883 static void nfs4_layoutcommit_release(void *calldata
)
8885 struct nfs4_layoutcommit_data
*data
= calldata
;
8887 pnfs_cleanup_layoutcommit(data
);
8888 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8890 put_rpccred(data
->cred
);
8891 nfs_iput_and_deactive(data
->inode
);
8895 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8896 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8897 .rpc_call_done
= nfs4_layoutcommit_done
,
8898 .rpc_release
= nfs4_layoutcommit_release
,
8902 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8904 struct rpc_message msg
= {
8905 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8906 .rpc_argp
= &data
->args
,
8907 .rpc_resp
= &data
->res
,
8908 .rpc_cred
= data
->cred
,
8910 struct rpc_task_setup task_setup_data
= {
8911 .task
= &data
->task
,
8912 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8913 .rpc_message
= &msg
,
8914 .callback_ops
= &nfs4_layoutcommit_ops
,
8915 .callback_data
= data
,
8917 struct rpc_task
*task
;
8920 dprintk("NFS: initiating layoutcommit call. sync %d "
8921 "lbw: %llu inode %lu\n", sync
,
8922 data
->args
.lastbytewritten
,
8923 data
->args
.inode
->i_ino
);
8926 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8927 if (data
->inode
== NULL
) {
8928 nfs4_layoutcommit_release(data
);
8931 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8933 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8934 task
= rpc_run_task(&task_setup_data
);
8936 return PTR_ERR(task
);
8938 status
= task
->tk_status
;
8939 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8940 dprintk("%s: status %d\n", __func__
, status
);
8946 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8947 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8950 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8951 struct nfs_fsinfo
*info
,
8952 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8954 struct nfs41_secinfo_no_name_args args
= {
8955 .style
= SECINFO_STYLE_CURRENT_FH
,
8957 struct nfs4_secinfo_res res
= {
8960 struct rpc_message msg
= {
8961 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8965 struct rpc_clnt
*clnt
= server
->client
;
8966 struct rpc_cred
*cred
= NULL
;
8969 if (use_integrity
) {
8970 clnt
= server
->nfs_client
->cl_rpcclient
;
8971 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8972 msg
.rpc_cred
= cred
;
8975 dprintk("--> %s\n", __func__
);
8976 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8978 dprintk("<-- %s status=%d\n", __func__
, status
);
8987 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8988 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8990 struct nfs4_exception exception
= { };
8993 /* first try using integrity protection */
8994 err
= -NFS4ERR_WRONGSEC
;
8996 /* try to use integrity protection with machine cred */
8997 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8998 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
9002 * if unable to use integrity protection, or SECINFO with
9003 * integrity protection returns NFS4ERR_WRONGSEC (which is
9004 * disallowed by spec, but exists in deployed servers) use
9005 * the current filesystem's rpc_client and the user cred.
9007 if (err
== -NFS4ERR_WRONGSEC
)
9008 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
9013 case -NFS4ERR_WRONGSEC
:
9017 err
= nfs4_handle_exception(server
, err
, &exception
);
9019 } while (exception
.retry
);
9025 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
9026 struct nfs_fsinfo
*info
)
9030 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
9031 struct nfs4_secinfo_flavors
*flavors
;
9032 struct nfs4_secinfo4
*secinfo
;
9035 page
= alloc_page(GFP_KERNEL
);
9041 flavors
= page_address(page
);
9042 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
9045 * Fall back on "guess and check" method if
9046 * the server doesn't support SECINFO_NO_NAME
9048 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
9049 err
= nfs4_find_root_sec(server
, fhandle
, info
);
9055 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
9056 secinfo
= &flavors
->flavors
[i
];
9058 switch (secinfo
->flavor
) {
9062 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
9063 &secinfo
->flavor_info
);
9066 flavor
= RPC_AUTH_MAXFLAVOR
;
9070 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
9071 flavor
= RPC_AUTH_MAXFLAVOR
;
9073 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
9074 err
= nfs4_lookup_root_sec(server
, fhandle
,
9081 if (flavor
== RPC_AUTH_MAXFLAVOR
)
9092 static int _nfs41_test_stateid(struct nfs_server
*server
,
9093 nfs4_stateid
*stateid
,
9094 struct rpc_cred
*cred
)
9097 struct nfs41_test_stateid_args args
= {
9100 struct nfs41_test_stateid_res res
;
9101 struct rpc_message msg
= {
9102 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
9107 struct rpc_clnt
*rpc_client
= server
->client
;
9109 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9112 dprintk("NFS call test_stateid %p\n", stateid
);
9113 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
9114 nfs4_set_sequence_privileged(&args
.seq_args
);
9115 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
9116 &args
.seq_args
, &res
.seq_res
);
9117 if (status
!= NFS_OK
) {
9118 dprintk("NFS reply test_stateid: failed, %d\n", status
);
9121 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
9125 static void nfs4_handle_delay_or_session_error(struct nfs_server
*server
,
9126 int err
, struct nfs4_exception
*exception
)
9128 exception
->retry
= 0;
9130 case -NFS4ERR_DELAY
:
9131 case -NFS4ERR_RETRY_UNCACHED_REP
:
9132 nfs4_handle_exception(server
, err
, exception
);
9134 case -NFS4ERR_BADSESSION
:
9135 case -NFS4ERR_BADSLOT
:
9136 case -NFS4ERR_BAD_HIGH_SLOT
:
9137 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
9138 case -NFS4ERR_DEADSESSION
:
9139 nfs4_do_handle_exception(server
, err
, exception
);
9144 * nfs41_test_stateid - perform a TEST_STATEID operation
9146 * @server: server / transport on which to perform the operation
9147 * @stateid: state ID to test
9150 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9151 * Otherwise a negative NFS4ERR value is returned if the operation
9152 * failed or the state ID is not currently valid.
9154 static int nfs41_test_stateid(struct nfs_server
*server
,
9155 nfs4_stateid
*stateid
,
9156 struct rpc_cred
*cred
)
9158 struct nfs4_exception exception
= { };
9161 err
= _nfs41_test_stateid(server
, stateid
, cred
);
9162 nfs4_handle_delay_or_session_error(server
, err
, &exception
);
9163 } while (exception
.retry
);
9167 struct nfs_free_stateid_data
{
9168 struct nfs_server
*server
;
9169 struct nfs41_free_stateid_args args
;
9170 struct nfs41_free_stateid_res res
;
9173 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
9175 struct nfs_free_stateid_data
*data
= calldata
;
9176 nfs4_setup_sequence(data
->server
->nfs_client
,
9177 &data
->args
.seq_args
,
9182 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
9184 struct nfs_free_stateid_data
*data
= calldata
;
9186 nfs41_sequence_done(task
, &data
->res
.seq_res
);
9188 switch (task
->tk_status
) {
9189 case -NFS4ERR_DELAY
:
9190 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
9191 rpc_restart_call_prepare(task
);
9195 static void nfs41_free_stateid_release(void *calldata
)
9200 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
9201 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
9202 .rpc_call_done
= nfs41_free_stateid_done
,
9203 .rpc_release
= nfs41_free_stateid_release
,
9206 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
9207 const nfs4_stateid
*stateid
,
9208 struct rpc_cred
*cred
,
9211 struct rpc_message msg
= {
9212 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
9215 struct rpc_task_setup task_setup
= {
9216 .rpc_client
= server
->client
,
9217 .rpc_message
= &msg
,
9218 .callback_ops
= &nfs41_free_stateid_ops
,
9219 .flags
= RPC_TASK_ASYNC
,
9221 struct nfs_free_stateid_data
*data
;
9223 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9224 &task_setup
.rpc_client
, &msg
);
9226 dprintk("NFS call free_stateid %p\n", stateid
);
9227 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
9229 return ERR_PTR(-ENOMEM
);
9230 data
->server
= server
;
9231 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
9233 task_setup
.callback_data
= data
;
9235 msg
.rpc_argp
= &data
->args
;
9236 msg
.rpc_resp
= &data
->res
;
9237 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
9239 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
9241 return rpc_run_task(&task_setup
);
9245 * nfs41_free_stateid - perform a FREE_STATEID operation
9247 * @server: server / transport on which to perform the operation
9248 * @stateid: state ID to release
9250 * @is_recovery: set to true if this call needs to be privileged
9252 * Note: this function is always asynchronous.
9254 static int nfs41_free_stateid(struct nfs_server
*server
,
9255 const nfs4_stateid
*stateid
,
9256 struct rpc_cred
*cred
,
9259 struct rpc_task
*task
;
9261 task
= _nfs41_free_stateid(server
, stateid
, cred
, is_recovery
);
9263 return PTR_ERR(task
);
9269 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
9271 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
9273 nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
9274 nfs4_free_lock_state(server
, lsp
);
9277 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
9278 const nfs4_stateid
*s2
)
9280 if (s1
->type
!= s2
->type
)
9283 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
9286 if (s1
->seqid
== s2
->seqid
)
9289 return s1
->seqid
== 0 || s2
->seqid
== 0;
9292 #endif /* CONFIG_NFS_V4_1 */
9294 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
9295 const nfs4_stateid
*s2
)
9297 return nfs4_stateid_match(s1
, s2
);
9301 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
9302 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9303 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9304 .recover_open
= nfs4_open_reclaim
,
9305 .recover_lock
= nfs4_lock_reclaim
,
9306 .establish_clid
= nfs4_init_clientid
,
9307 .detect_trunking
= nfs40_discover_server_trunking
,
9310 #if defined(CONFIG_NFS_V4_1)
9311 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
9312 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9313 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9314 .recover_open
= nfs4_open_reclaim
,
9315 .recover_lock
= nfs4_lock_reclaim
,
9316 .establish_clid
= nfs41_init_clientid
,
9317 .reclaim_complete
= nfs41_proc_reclaim_complete
,
9318 .detect_trunking
= nfs41_discover_server_trunking
,
9320 #endif /* CONFIG_NFS_V4_1 */
9322 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
9323 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9324 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9325 .recover_open
= nfs40_open_expired
,
9326 .recover_lock
= nfs4_lock_expired
,
9327 .establish_clid
= nfs4_init_clientid
,
9330 #if defined(CONFIG_NFS_V4_1)
9331 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
9332 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9333 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9334 .recover_open
= nfs41_open_expired
,
9335 .recover_lock
= nfs41_lock_expired
,
9336 .establish_clid
= nfs41_init_clientid
,
9338 #endif /* CONFIG_NFS_V4_1 */
9340 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
9341 .sched_state_renewal
= nfs4_proc_async_renew
,
9342 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
9343 .renew_lease
= nfs4_proc_renew
,
9346 #if defined(CONFIG_NFS_V4_1)
9347 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
9348 .sched_state_renewal
= nfs41_proc_async_sequence
,
9349 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
9350 .renew_lease
= nfs4_proc_sequence
,
9354 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
9355 .get_locations
= _nfs40_proc_get_locations
,
9356 .fsid_present
= _nfs40_proc_fsid_present
,
9359 #if defined(CONFIG_NFS_V4_1)
9360 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
9361 .get_locations
= _nfs41_proc_get_locations
,
9362 .fsid_present
= _nfs41_proc_fsid_present
,
9364 #endif /* CONFIG_NFS_V4_1 */
9366 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
9368 .init_caps
= NFS_CAP_READDIRPLUS
9369 | NFS_CAP_ATOMIC_OPEN
9370 | NFS_CAP_POSIX_LOCK
,
9371 .init_client
= nfs40_init_client
,
9372 .shutdown_client
= nfs40_shutdown_client
,
9373 .match_stateid
= nfs4_match_stateid
,
9374 .find_root_sec
= nfs4_find_root_sec
,
9375 .free_lock_state
= nfs4_release_lockowner
,
9376 .test_and_free_expired
= nfs40_test_and_free_expired_stateid
,
9377 .alloc_seqid
= nfs_alloc_seqid
,
9378 .call_sync_ops
= &nfs40_call_sync_ops
,
9379 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
9380 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
9381 .state_renewal_ops
= &nfs40_state_renewal_ops
,
9382 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
9385 #if defined(CONFIG_NFS_V4_1)
9386 static struct nfs_seqid
*
9387 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
9392 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
9394 .init_caps
= NFS_CAP_READDIRPLUS
9395 | NFS_CAP_ATOMIC_OPEN
9396 | NFS_CAP_POSIX_LOCK
9397 | NFS_CAP_STATEID_NFSV41
9398 | NFS_CAP_ATOMIC_OPEN_V1
,
9399 .init_client
= nfs41_init_client
,
9400 .shutdown_client
= nfs41_shutdown_client
,
9401 .match_stateid
= nfs41_match_stateid
,
9402 .find_root_sec
= nfs41_find_root_sec
,
9403 .free_lock_state
= nfs41_free_lock_state
,
9404 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9405 .alloc_seqid
= nfs_alloc_no_seqid
,
9406 .session_trunk
= nfs4_test_session_trunk
,
9407 .call_sync_ops
= &nfs41_call_sync_ops
,
9408 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9409 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9410 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9411 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9415 #if defined(CONFIG_NFS_V4_2)
9416 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
9418 .init_caps
= NFS_CAP_READDIRPLUS
9419 | NFS_CAP_ATOMIC_OPEN
9420 | NFS_CAP_POSIX_LOCK
9421 | NFS_CAP_STATEID_NFSV41
9422 | NFS_CAP_ATOMIC_OPEN_V1
9425 | NFS_CAP_DEALLOCATE
9427 | NFS_CAP_LAYOUTSTATS
9429 .init_client
= nfs41_init_client
,
9430 .shutdown_client
= nfs41_shutdown_client
,
9431 .match_stateid
= nfs41_match_stateid
,
9432 .find_root_sec
= nfs41_find_root_sec
,
9433 .free_lock_state
= nfs41_free_lock_state
,
9434 .call_sync_ops
= &nfs41_call_sync_ops
,
9435 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9436 .alloc_seqid
= nfs_alloc_no_seqid
,
9437 .session_trunk
= nfs4_test_session_trunk
,
9438 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9439 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9440 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9441 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9445 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
9446 [0] = &nfs_v4_0_minor_ops
,
9447 #if defined(CONFIG_NFS_V4_1)
9448 [1] = &nfs_v4_1_minor_ops
,
9450 #if defined(CONFIG_NFS_V4_2)
9451 [2] = &nfs_v4_2_minor_ops
,
9455 static ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
9457 ssize_t error
, error2
;
9459 error
= generic_listxattr(dentry
, list
, size
);
9467 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
9470 return error
+ error2
;
9473 static const struct inode_operations nfs4_dir_inode_operations
= {
9474 .create
= nfs_create
,
9475 .lookup
= nfs_lookup
,
9476 .atomic_open
= nfs_atomic_open
,
9478 .unlink
= nfs_unlink
,
9479 .symlink
= nfs_symlink
,
9483 .rename
= nfs_rename
,
9484 .permission
= nfs_permission
,
9485 .getattr
= nfs_getattr
,
9486 .setattr
= nfs_setattr
,
9487 .listxattr
= nfs4_listxattr
,
9490 static const struct inode_operations nfs4_file_inode_operations
= {
9491 .permission
= nfs_permission
,
9492 .getattr
= nfs_getattr
,
9493 .setattr
= nfs_setattr
,
9494 .listxattr
= nfs4_listxattr
,
9497 const struct nfs_rpc_ops nfs_v4_clientops
= {
9498 .version
= 4, /* protocol version */
9499 .dentry_ops
= &nfs4_dentry_operations
,
9500 .dir_inode_ops
= &nfs4_dir_inode_operations
,
9501 .file_inode_ops
= &nfs4_file_inode_operations
,
9502 .file_ops
= &nfs4_file_operations
,
9503 .getroot
= nfs4_proc_get_root
,
9504 .submount
= nfs4_submount
,
9505 .try_mount
= nfs4_try_mount
,
9506 .getattr
= nfs4_proc_getattr
,
9507 .setattr
= nfs4_proc_setattr
,
9508 .lookup
= nfs4_proc_lookup
,
9509 .lookupp
= nfs4_proc_lookupp
,
9510 .access
= nfs4_proc_access
,
9511 .readlink
= nfs4_proc_readlink
,
9512 .create
= nfs4_proc_create
,
9513 .remove
= nfs4_proc_remove
,
9514 .unlink_setup
= nfs4_proc_unlink_setup
,
9515 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
9516 .unlink_done
= nfs4_proc_unlink_done
,
9517 .rename_setup
= nfs4_proc_rename_setup
,
9518 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
9519 .rename_done
= nfs4_proc_rename_done
,
9520 .link
= nfs4_proc_link
,
9521 .symlink
= nfs4_proc_symlink
,
9522 .mkdir
= nfs4_proc_mkdir
,
9523 .rmdir
= nfs4_proc_remove
,
9524 .readdir
= nfs4_proc_readdir
,
9525 .mknod
= nfs4_proc_mknod
,
9526 .statfs
= nfs4_proc_statfs
,
9527 .fsinfo
= nfs4_proc_fsinfo
,
9528 .pathconf
= nfs4_proc_pathconf
,
9529 .set_capabilities
= nfs4_server_capabilities
,
9530 .decode_dirent
= nfs4_decode_dirent
,
9531 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
9532 .read_setup
= nfs4_proc_read_setup
,
9533 .read_done
= nfs4_read_done
,
9534 .write_setup
= nfs4_proc_write_setup
,
9535 .write_done
= nfs4_write_done
,
9536 .commit_setup
= nfs4_proc_commit_setup
,
9537 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
9538 .commit_done
= nfs4_commit_done
,
9539 .lock
= nfs4_proc_lock
,
9540 .clear_acl_cache
= nfs4_zap_acl_attr
,
9541 .close_context
= nfs4_close_context
,
9542 .open_context
= nfs4_atomic_open
,
9543 .have_delegation
= nfs4_have_delegation
,
9544 .return_delegation
= nfs4_inode_return_delegation
,
9545 .alloc_client
= nfs4_alloc_client
,
9546 .init_client
= nfs4_init_client
,
9547 .free_client
= nfs4_free_client
,
9548 .create_server
= nfs4_create_server
,
9549 .clone_server
= nfs_clone_server
,
9552 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
9553 .name
= XATTR_NAME_NFSV4_ACL
,
9554 .list
= nfs4_xattr_list_nfs4_acl
,
9555 .get
= nfs4_xattr_get_nfs4_acl
,
9556 .set
= nfs4_xattr_set_nfs4_acl
,
9559 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
9560 &nfs4_xattr_nfs4_acl_handler
,
9561 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9562 &nfs4_xattr_nfs4_label_handler
,