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 int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
101 static int nfs41_free_stateid(struct nfs_server
*, const nfs4_stateid
*,
102 struct rpc_cred
*, bool);
105 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
106 static inline struct nfs4_label
*
107 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
108 struct iattr
*sattr
, struct nfs4_label
*label
)
115 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
118 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
119 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
126 nfs4_label_release_security(struct nfs4_label
*label
)
129 security_release_secctx(label
->label
, label
->len
);
131 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
134 return server
->attr_bitmask
;
136 return server
->attr_bitmask_nl
;
139 static inline struct nfs4_label
*
140 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
141 struct iattr
*sattr
, struct nfs4_label
*l
)
144 nfs4_label_release_security(struct nfs4_label
*label
)
147 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
148 { return server
->attr_bitmask
; }
151 /* Prevent leaks of NFSv4 errors into userland */
152 static int nfs4_map_errors(int err
)
157 case -NFS4ERR_RESOURCE
:
158 case -NFS4ERR_LAYOUTTRYLATER
:
159 case -NFS4ERR_RECALLCONFLICT
:
161 case -NFS4ERR_WRONGSEC
:
162 case -NFS4ERR_WRONG_CRED
:
164 case -NFS4ERR_BADOWNER
:
165 case -NFS4ERR_BADNAME
:
167 case -NFS4ERR_SHARE_DENIED
:
169 case -NFS4ERR_MINOR_VERS_MISMATCH
:
170 return -EPROTONOSUPPORT
;
171 case -NFS4ERR_FILE_OPEN
:
174 dprintk("%s could not handle NFSv4 error %d\n",
182 * This is our standard bitmap for GETATTR requests.
184 const u32 nfs4_fattr_bitmap
[3] = {
186 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_FILEID
,
191 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
200 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
205 static const u32 nfs4_pnfs_open_bitmap
[3] = {
207 | FATTR4_WORD0_CHANGE
210 | FATTR4_WORD0_FILEID
,
212 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY
,
220 FATTR4_WORD2_MDSTHRESHOLD
221 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
226 static const u32 nfs4_open_noattr_bitmap
[3] = {
228 | FATTR4_WORD0_FILEID
,
231 const u32 nfs4_statfs_bitmap
[3] = {
232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL
,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
240 const u32 nfs4_pathconf_bitmap
[3] = {
242 | FATTR4_WORD0_MAXNAME
,
246 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME
,
250 FATTR4_WORD1_TIME_DELTA
251 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
253 | FATTR4_WORD2_CLONE_BLKSIZE
256 const u32 nfs4_fs_locations_bitmap
[3] = {
258 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS
,
264 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
272 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
275 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
276 struct nfs4_readdir_arg
*readdir
)
281 readdir
->cookie
= cookie
;
282 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
287 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
298 start
= p
= kmap_atomic(*readdir
->pages
);
301 *p
++ = xdr_one
; /* next */
302 *p
++ = xdr_zero
; /* cookie, first word */
303 *p
++ = xdr_one
; /* cookie, second word */
304 *p
++ = xdr_one
; /* entry len */
305 memcpy(p
, ".\0\0\0", 4); /* entry */
307 *p
++ = xdr_one
; /* bitmap length */
308 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
309 *p
++ = htonl(8); /* attribute buffer length */
310 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
)));
313 *p
++ = xdr_one
; /* next */
314 *p
++ = xdr_zero
; /* cookie, first word */
315 *p
++ = xdr_two
; /* cookie, second word */
316 *p
++ = xdr_two
; /* entry len */
317 memcpy(p
, "..\0\0", 4); /* entry */
319 *p
++ = xdr_one
; /* bitmap length */
320 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
321 *p
++ = htonl(8); /* attribute buffer length */
322 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
324 readdir
->pgbase
= (char *)p
- (char *)start
;
325 readdir
->count
-= readdir
->pgbase
;
326 kunmap_atomic(start
);
329 static void nfs4_test_and_free_stateid(struct nfs_server
*server
,
330 nfs4_stateid
*stateid
,
331 struct rpc_cred
*cred
)
333 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
335 ops
->test_and_free_expired(server
, stateid
, cred
);
338 static void __nfs4_free_revoked_stateid(struct nfs_server
*server
,
339 nfs4_stateid
*stateid
,
340 struct rpc_cred
*cred
)
342 stateid
->type
= NFS4_REVOKED_STATEID_TYPE
;
343 nfs4_test_and_free_stateid(server
, stateid
, cred
);
346 static void nfs4_free_revoked_stateid(struct nfs_server
*server
,
347 const nfs4_stateid
*stateid
,
348 struct rpc_cred
*cred
)
352 nfs4_stateid_copy(&tmp
, stateid
);
353 __nfs4_free_revoked_stateid(server
, &tmp
, cred
);
356 static long nfs4_update_delay(long *timeout
)
360 return NFS4_POLL_RETRY_MAX
;
362 *timeout
= NFS4_POLL_RETRY_MIN
;
363 if (*timeout
> NFS4_POLL_RETRY_MAX
)
364 *timeout
= NFS4_POLL_RETRY_MAX
;
370 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout
));
378 if (fatal_signal_pending(current
))
383 /* This is the error handling routine for processes that are allowed
386 static int nfs4_do_handle_exception(struct nfs_server
*server
,
387 int errorcode
, struct nfs4_exception
*exception
)
389 struct nfs_client
*clp
= server
->nfs_client
;
390 struct nfs4_state
*state
= exception
->state
;
391 const nfs4_stateid
*stateid
= exception
->stateid
;
392 struct inode
*inode
= exception
->inode
;
395 exception
->delay
= 0;
396 exception
->recovering
= 0;
397 exception
->retry
= 0;
399 if (stateid
== NULL
&& state
!= NULL
)
400 stateid
= &state
->stateid
;
405 case -NFS4ERR_DELEG_REVOKED
:
406 case -NFS4ERR_ADMIN_REVOKED
:
407 case -NFS4ERR_EXPIRED
:
408 case -NFS4ERR_BAD_STATEID
:
409 if (inode
!= NULL
&& stateid
!= NULL
) {
410 nfs_inode_find_state_and_recover(inode
,
412 goto wait_on_recovery
;
414 case -NFS4ERR_OPENMODE
:
418 err
= nfs_async_inode_return_delegation(inode
,
421 goto wait_on_recovery
;
422 if (stateid
!= NULL
&& stateid
->type
== NFS4_DELEGATION_STATEID_TYPE
) {
423 exception
->retry
= 1;
429 ret
= nfs4_schedule_stateid_recovery(server
, state
);
432 goto wait_on_recovery
;
433 case -NFS4ERR_STALE_STATEID
:
434 case -NFS4ERR_STALE_CLIENTID
:
435 nfs4_schedule_lease_recovery(clp
);
436 goto wait_on_recovery
;
438 ret
= nfs4_schedule_migration_recovery(server
);
441 goto wait_on_recovery
;
442 case -NFS4ERR_LEASE_MOVED
:
443 nfs4_schedule_lease_moved_recovery(clp
);
444 goto wait_on_recovery
;
445 #if defined(CONFIG_NFS_V4_1)
446 case -NFS4ERR_BADSESSION
:
447 case -NFS4ERR_BADSLOT
:
448 case -NFS4ERR_BAD_HIGH_SLOT
:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
450 case -NFS4ERR_DEADSESSION
:
451 case -NFS4ERR_SEQ_FALSE_RETRY
:
452 case -NFS4ERR_SEQ_MISORDERED
:
453 dprintk("%s ERROR: %d Reset session\n", __func__
,
455 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
456 goto wait_on_recovery
;
457 #endif /* defined(CONFIG_NFS_V4_1) */
458 case -NFS4ERR_FILE_OPEN
:
459 if (exception
->timeout
> HZ
) {
460 /* We have retried a decent amount, time to
467 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
469 case -NFS4ERR_LAYOUTTRYLATER
:
470 case -NFS4ERR_RECALLCONFLICT
:
471 exception
->delay
= 1;
474 case -NFS4ERR_RETRY_UNCACHED_REP
:
475 case -NFS4ERR_OLD_STATEID
:
476 exception
->retry
= 1;
478 case -NFS4ERR_BADOWNER
:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME
:
481 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
482 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
483 exception
->retry
= 1;
484 printk(KERN_WARNING
"NFS: v4 server %s "
485 "does not accept raw "
487 "Reenabling the idmapper.\n",
488 server
->nfs_client
->cl_hostname
);
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret
);
494 exception
->recovering
= 1;
498 /* This is the error handling routine for processes that are allowed
501 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
503 struct nfs_client
*clp
= server
->nfs_client
;
506 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
507 if (exception
->delay
) {
508 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
511 if (exception
->recovering
) {
512 ret
= nfs4_wait_clnt_recover(clp
);
513 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
520 exception
->retry
= 1;
525 nfs4_async_handle_exception(struct rpc_task
*task
, struct nfs_server
*server
,
526 int errorcode
, struct nfs4_exception
*exception
)
528 struct nfs_client
*clp
= server
->nfs_client
;
531 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
532 if (exception
->delay
) {
533 rpc_delay(task
, nfs4_update_delay(&exception
->timeout
));
536 if (exception
->recovering
) {
537 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
539 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
542 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
547 exception
->retry
= 1;
552 nfs4_async_handle_error(struct rpc_task
*task
, struct nfs_server
*server
,
553 struct nfs4_state
*state
, long *timeout
)
555 struct nfs4_exception exception
= {
559 if (task
->tk_status
>= 0)
562 exception
.timeout
= *timeout
;
563 task
->tk_status
= nfs4_async_handle_exception(task
, server
,
566 if (exception
.delay
&& timeout
)
567 *timeout
= exception
.timeout
;
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
577 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
579 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
581 if (flavor
== RPC_AUTH_GSS_KRB5I
||
582 flavor
== RPC_AUTH_GSS_KRB5P
)
588 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
590 spin_lock(&clp
->cl_lock
);
591 if (time_before(clp
->cl_last_renewal
,timestamp
))
592 clp
->cl_last_renewal
= timestamp
;
593 spin_unlock(&clp
->cl_lock
);
596 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
598 struct nfs_client
*clp
= server
->nfs_client
;
600 if (!nfs4_has_session(clp
))
601 do_renew_lease(clp
, timestamp
);
604 struct nfs4_call_sync_data
{
605 const struct nfs_server
*seq_server
;
606 struct nfs4_sequence_args
*seq_args
;
607 struct nfs4_sequence_res
*seq_res
;
610 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
611 struct nfs4_sequence_res
*res
, int cache_reply
)
613 args
->sa_slot
= NULL
;
614 args
->sa_cache_this
= cache_reply
;
615 args
->sa_privileged
= 0;
620 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
622 args
->sa_privileged
= 1;
625 int nfs40_setup_sequence(struct nfs4_slot_table
*tbl
,
626 struct nfs4_sequence_args
*args
,
627 struct nfs4_sequence_res
*res
,
628 struct rpc_task
*task
)
630 struct nfs4_slot
*slot
;
632 /* slot already allocated? */
633 if (res
->sr_slot
!= NULL
)
636 spin_lock(&tbl
->slot_tbl_lock
);
637 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
640 slot
= nfs4_alloc_slot(tbl
);
642 if (slot
== ERR_PTR(-ENOMEM
))
643 task
->tk_timeout
= HZ
>> 2;
646 spin_unlock(&tbl
->slot_tbl_lock
);
648 slot
->privileged
= args
->sa_privileged
? 1 : 0;
649 args
->sa_slot
= slot
;
653 rpc_call_start(task
);
657 if (args
->sa_privileged
)
658 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
659 NULL
, RPC_PRIORITY_PRIVILEGED
);
661 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
662 spin_unlock(&tbl
->slot_tbl_lock
);
665 EXPORT_SYMBOL_GPL(nfs40_setup_sequence
);
667 static void nfs40_sequence_free_slot(struct nfs4_sequence_res
*res
)
669 struct nfs4_slot
*slot
= res
->sr_slot
;
670 struct nfs4_slot_table
*tbl
;
673 spin_lock(&tbl
->slot_tbl_lock
);
674 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
675 nfs4_free_slot(tbl
, slot
);
676 spin_unlock(&tbl
->slot_tbl_lock
);
681 static int nfs40_sequence_done(struct rpc_task
*task
,
682 struct nfs4_sequence_res
*res
)
684 if (res
->sr_slot
!= NULL
)
685 nfs40_sequence_free_slot(res
);
689 #if defined(CONFIG_NFS_V4_1)
691 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
693 struct nfs4_session
*session
;
694 struct nfs4_slot_table
*tbl
;
695 struct nfs4_slot
*slot
= res
->sr_slot
;
696 bool send_new_highest_used_slotid
= false;
699 session
= tbl
->session
;
701 /* Bump the slot sequence number */
706 spin_lock(&tbl
->slot_tbl_lock
);
707 /* Be nice to the server: try to ensure that the last transmitted
708 * value for highest_user_slotid <= target_highest_slotid
710 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
711 send_new_highest_used_slotid
= true;
713 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
714 send_new_highest_used_slotid
= false;
717 nfs4_free_slot(tbl
, slot
);
719 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
720 send_new_highest_used_slotid
= false;
722 spin_unlock(&tbl
->slot_tbl_lock
);
724 if (send_new_highest_used_slotid
)
725 nfs41_notify_server(session
->clp
);
726 if (waitqueue_active(&tbl
->slot_waitq
))
727 wake_up_all(&tbl
->slot_waitq
);
730 static int nfs41_sequence_process(struct rpc_task
*task
,
731 struct nfs4_sequence_res
*res
)
733 struct nfs4_session
*session
;
734 struct nfs4_slot
*slot
= res
->sr_slot
;
735 struct nfs_client
*clp
;
736 bool interrupted
= false;
741 /* don't increment the sequence number if the task wasn't sent */
742 if (!RPC_WAS_SENT(task
))
745 session
= slot
->table
->session
;
747 if (slot
->interrupted
) {
748 slot
->interrupted
= 0;
752 trace_nfs4_sequence_done(session
, res
);
753 /* Check the SEQUENCE operation status */
754 switch (res
->sr_status
) {
756 /* If previous op on slot was interrupted and we reused
757 * the seq# and got a reply from the cache, then retry
759 if (task
->tk_status
== -EREMOTEIO
&& interrupted
) {
763 /* Update the slot's sequence and clientid lease timer */
766 do_renew_lease(clp
, res
->sr_timestamp
);
767 /* Check sequence flags */
768 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
,
770 nfs41_update_target_slotid(slot
->table
, slot
, res
);
774 * sr_status remains 1 if an RPC level error occurred.
775 * The server may or may not have processed the sequence
777 * Mark the slot as having hosted an interrupted RPC call.
779 slot
->interrupted
= 1;
782 /* The server detected a resend of the RPC call and
783 * returned NFS4ERR_DELAY as per Section 2.10.6.2
786 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
791 case -NFS4ERR_BADSLOT
:
793 * The slot id we used was probably retired. Try again
794 * using a different slot id.
797 case -NFS4ERR_SEQ_MISORDERED
:
799 * Was the last operation on this sequence interrupted?
800 * If so, retry after bumping the sequence number.
807 * Could this slot have been previously retired?
808 * If so, then the server may be expecting seq_nr = 1!
810 if (slot
->seq_nr
!= 1) {
815 case -NFS4ERR_SEQ_FALSE_RETRY
:
819 /* Just update the slot sequence no. */
823 /* The session may be reset by one of the error handlers. */
824 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
828 if (rpc_restart_call_prepare(task
)) {
829 nfs41_sequence_free_slot(res
);
835 if (!rpc_restart_call(task
))
837 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
841 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
843 if (!nfs41_sequence_process(task
, res
))
845 if (res
->sr_slot
!= NULL
)
846 nfs41_sequence_free_slot(res
);
850 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
852 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
854 if (res
->sr_slot
== NULL
)
856 if (res
->sr_slot
->table
->session
!= NULL
)
857 return nfs41_sequence_process(task
, res
);
858 return nfs40_sequence_done(task
, res
);
861 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
863 if (res
->sr_slot
!= NULL
) {
864 if (res
->sr_slot
->table
->session
!= NULL
)
865 nfs41_sequence_free_slot(res
);
867 nfs40_sequence_free_slot(res
);
871 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
873 if (res
->sr_slot
== NULL
)
875 if (!res
->sr_slot
->table
->session
)
876 return nfs40_sequence_done(task
, res
);
877 return nfs41_sequence_done(task
, res
);
879 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
881 int nfs41_setup_sequence(struct nfs4_session
*session
,
882 struct nfs4_sequence_args
*args
,
883 struct nfs4_sequence_res
*res
,
884 struct rpc_task
*task
)
886 struct nfs4_slot
*slot
;
887 struct nfs4_slot_table
*tbl
;
889 dprintk("--> %s\n", __func__
);
890 /* slot already allocated? */
891 if (res
->sr_slot
!= NULL
)
894 tbl
= &session
->fc_slot_table
;
896 task
->tk_timeout
= 0;
898 spin_lock(&tbl
->slot_tbl_lock
);
899 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
900 !args
->sa_privileged
) {
901 /* The state manager will wait until the slot table is empty */
902 dprintk("%s session is draining\n", __func__
);
906 slot
= nfs4_alloc_slot(tbl
);
908 /* If out of memory, try again in 1/4 second */
909 if (slot
== ERR_PTR(-ENOMEM
))
910 task
->tk_timeout
= HZ
>> 2;
911 dprintk("<-- %s: no free slots\n", __func__
);
914 spin_unlock(&tbl
->slot_tbl_lock
);
916 slot
->privileged
= args
->sa_privileged
? 1 : 0;
917 args
->sa_slot
= slot
;
919 dprintk("<-- %s slotid=%u seqid=%u\n", __func__
,
920 slot
->slot_nr
, slot
->seq_nr
);
923 res
->sr_timestamp
= jiffies
;
924 res
->sr_status_flags
= 0;
926 * sr_status is only set in decode_sequence, and so will remain
927 * set to 1 if an rpc level failure occurs.
930 trace_nfs4_setup_sequence(session
, args
);
932 rpc_call_start(task
);
935 /* Privileged tasks are queued with top priority */
936 if (args
->sa_privileged
)
937 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
938 NULL
, RPC_PRIORITY_PRIVILEGED
);
940 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
941 spin_unlock(&tbl
->slot_tbl_lock
);
944 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
946 static int nfs4_setup_sequence(const struct nfs_server
*server
,
947 struct nfs4_sequence_args
*args
,
948 struct nfs4_sequence_res
*res
,
949 struct rpc_task
*task
)
951 struct nfs4_session
*session
= nfs4_get_session(server
);
955 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
958 dprintk("--> %s clp %p session %p sr_slot %u\n",
959 __func__
, session
->clp
, session
, res
->sr_slot
?
960 res
->sr_slot
->slot_nr
: NFS4_NO_SLOT
);
962 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
964 dprintk("<-- %s status=%d\n", __func__
, ret
);
968 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
970 struct nfs4_call_sync_data
*data
= calldata
;
971 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
973 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
975 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
978 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
980 struct nfs4_call_sync_data
*data
= calldata
;
982 nfs41_sequence_done(task
, data
->seq_res
);
985 static const struct rpc_call_ops nfs41_call_sync_ops
= {
986 .rpc_call_prepare
= nfs41_call_sync_prepare
,
987 .rpc_call_done
= nfs41_call_sync_done
,
990 #else /* !CONFIG_NFS_V4_1 */
992 static int nfs4_setup_sequence(const struct nfs_server
*server
,
993 struct nfs4_sequence_args
*args
,
994 struct nfs4_sequence_res
*res
,
995 struct rpc_task
*task
)
997 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
1001 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
1003 return nfs40_sequence_done(task
, res
);
1006 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
1008 if (res
->sr_slot
!= NULL
)
1009 nfs40_sequence_free_slot(res
);
1012 int nfs4_sequence_done(struct rpc_task
*task
,
1013 struct nfs4_sequence_res
*res
)
1015 return nfs40_sequence_done(task
, res
);
1017 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
1019 #endif /* !CONFIG_NFS_V4_1 */
1021 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
1023 struct nfs4_call_sync_data
*data
= calldata
;
1024 nfs4_setup_sequence(data
->seq_server
,
1025 data
->seq_args
, data
->seq_res
, task
);
1028 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
1030 struct nfs4_call_sync_data
*data
= calldata
;
1031 nfs4_sequence_done(task
, data
->seq_res
);
1034 static const struct rpc_call_ops nfs40_call_sync_ops
= {
1035 .rpc_call_prepare
= nfs40_call_sync_prepare
,
1036 .rpc_call_done
= nfs40_call_sync_done
,
1039 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
1040 struct nfs_server
*server
,
1041 struct rpc_message
*msg
,
1042 struct nfs4_sequence_args
*args
,
1043 struct nfs4_sequence_res
*res
)
1046 struct rpc_task
*task
;
1047 struct nfs_client
*clp
= server
->nfs_client
;
1048 struct nfs4_call_sync_data data
= {
1049 .seq_server
= server
,
1053 struct rpc_task_setup task_setup
= {
1056 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
1057 .callback_data
= &data
1060 task
= rpc_run_task(&task_setup
);
1062 ret
= PTR_ERR(task
);
1064 ret
= task
->tk_status
;
1070 int nfs4_call_sync(struct rpc_clnt
*clnt
,
1071 struct nfs_server
*server
,
1072 struct rpc_message
*msg
,
1073 struct nfs4_sequence_args
*args
,
1074 struct nfs4_sequence_res
*res
,
1077 nfs4_init_sequence(args
, res
, cache_reply
);
1078 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
1081 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
,
1082 unsigned long timestamp
)
1084 struct nfs_inode
*nfsi
= NFS_I(dir
);
1086 spin_lock(&dir
->i_lock
);
1087 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1088 if (cinfo
->atomic
&& cinfo
->before
== dir
->i_version
) {
1089 nfsi
->cache_validity
&= ~NFS_INO_REVAL_PAGECACHE
;
1090 nfsi
->attrtimeo_timestamp
= jiffies
;
1092 nfs_force_lookup_revalidate(dir
);
1093 if (cinfo
->before
!= dir
->i_version
)
1094 nfsi
->cache_validity
|= NFS_INO_INVALID_ACCESS
|
1095 NFS_INO_INVALID_ACL
;
1097 dir
->i_version
= cinfo
->after
;
1098 nfsi
->read_cache_jiffies
= timestamp
;
1099 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
1100 nfs_fscache_invalidate(dir
);
1101 spin_unlock(&dir
->i_lock
);
1104 struct nfs4_opendata
{
1106 struct nfs_openargs o_arg
;
1107 struct nfs_openres o_res
;
1108 struct nfs_open_confirmargs c_arg
;
1109 struct nfs_open_confirmres c_res
;
1110 struct nfs4_string owner_name
;
1111 struct nfs4_string group_name
;
1112 struct nfs4_label
*a_label
;
1113 struct nfs_fattr f_attr
;
1114 struct nfs4_label
*f_label
;
1116 struct dentry
*dentry
;
1117 struct nfs4_state_owner
*owner
;
1118 struct nfs4_state
*state
;
1120 unsigned long timestamp
;
1121 unsigned int rpc_done
: 1;
1122 unsigned int file_created
: 1;
1123 unsigned int is_recover
: 1;
1128 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
1129 int err
, struct nfs4_exception
*exception
)
1133 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1135 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
1136 exception
->retry
= 1;
1141 nfs4_map_atomic_open_share(struct nfs_server
*server
,
1142 fmode_t fmode
, int openflags
)
1146 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
1148 res
= NFS4_SHARE_ACCESS_READ
;
1151 res
= NFS4_SHARE_ACCESS_WRITE
;
1153 case FMODE_READ
|FMODE_WRITE
:
1154 res
= NFS4_SHARE_ACCESS_BOTH
;
1156 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1158 /* Want no delegation if we're using O_DIRECT */
1159 if (openflags
& O_DIRECT
)
1160 res
|= NFS4_SHARE_WANT_NO_DELEG
;
1165 static enum open_claim_type4
1166 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
1167 enum open_claim_type4 claim
)
1169 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
1174 case NFS4_OPEN_CLAIM_FH
:
1175 return NFS4_OPEN_CLAIM_NULL
;
1176 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1177 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1178 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1179 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
1183 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
1185 p
->o_res
.f_attr
= &p
->f_attr
;
1186 p
->o_res
.f_label
= p
->f_label
;
1187 p
->o_res
.seqid
= p
->o_arg
.seqid
;
1188 p
->c_res
.seqid
= p
->c_arg
.seqid
;
1189 p
->o_res
.server
= p
->o_arg
.server
;
1190 p
->o_res
.access_request
= p
->o_arg
.access
;
1191 nfs_fattr_init(&p
->f_attr
);
1192 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
1195 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
1196 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
1197 const struct iattr
*attrs
,
1198 struct nfs4_label
*label
,
1199 enum open_claim_type4 claim
,
1202 struct dentry
*parent
= dget_parent(dentry
);
1203 struct inode
*dir
= d_inode(parent
);
1204 struct nfs_server
*server
= NFS_SERVER(dir
);
1205 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1206 struct nfs4_opendata
*p
;
1208 p
= kzalloc(sizeof(*p
), gfp_mask
);
1212 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1213 if (IS_ERR(p
->f_label
))
1216 p
->a_label
= nfs4_label_alloc(server
, gfp_mask
);
1217 if (IS_ERR(p
->a_label
))
1220 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1221 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1222 if (IS_ERR(p
->o_arg
.seqid
))
1223 goto err_free_label
;
1224 nfs_sb_active(dentry
->d_sb
);
1225 p
->dentry
= dget(dentry
);
1228 atomic_inc(&sp
->so_count
);
1229 p
->o_arg
.open_flags
= flags
;
1230 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1231 p
->o_arg
.umask
= current_umask();
1232 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1233 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1235 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1236 * will return permission denied for all bits until close */
1237 if (!(flags
& O_EXCL
)) {
1238 /* ask server to check for all possible rights as results
1240 switch (p
->o_arg
.claim
) {
1243 case NFS4_OPEN_CLAIM_NULL
:
1244 case NFS4_OPEN_CLAIM_FH
:
1245 p
->o_arg
.access
= NFS4_ACCESS_READ
|
1246 NFS4_ACCESS_MODIFY
|
1247 NFS4_ACCESS_EXTEND
|
1248 NFS4_ACCESS_EXECUTE
;
1251 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1252 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1253 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1254 p
->o_arg
.name
= &dentry
->d_name
;
1255 p
->o_arg
.server
= server
;
1256 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1257 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1258 p
->o_arg
.label
= nfs4_label_copy(p
->a_label
, label
);
1259 switch (p
->o_arg
.claim
) {
1260 case NFS4_OPEN_CLAIM_NULL
:
1261 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1262 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1263 p
->o_arg
.fh
= NFS_FH(dir
);
1265 case NFS4_OPEN_CLAIM_PREVIOUS
:
1266 case NFS4_OPEN_CLAIM_FH
:
1267 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1268 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1269 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1271 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1274 p
->o_arg
.u
.attrs
= &p
->attrs
;
1275 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1278 verf
[1] = current
->pid
;
1279 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1280 sizeof(p
->o_arg
.u
.verifier
.data
));
1282 p
->c_arg
.fh
= &p
->o_res
.fh
;
1283 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1284 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1285 nfs4_init_opendata_res(p
);
1286 kref_init(&p
->kref
);
1290 nfs4_label_free(p
->a_label
);
1292 nfs4_label_free(p
->f_label
);
1300 static void nfs4_opendata_free(struct kref
*kref
)
1302 struct nfs4_opendata
*p
= container_of(kref
,
1303 struct nfs4_opendata
, kref
);
1304 struct super_block
*sb
= p
->dentry
->d_sb
;
1306 nfs_free_seqid(p
->o_arg
.seqid
);
1307 nfs4_sequence_free_slot(&p
->o_res
.seq_res
);
1308 if (p
->state
!= NULL
)
1309 nfs4_put_open_state(p
->state
);
1310 nfs4_put_state_owner(p
->owner
);
1312 nfs4_label_free(p
->a_label
);
1313 nfs4_label_free(p
->f_label
);
1317 nfs_sb_deactive(sb
);
1318 nfs_fattr_free_names(&p
->f_attr
);
1319 kfree(p
->f_attr
.mdsthreshold
);
1323 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1326 kref_put(&p
->kref
, nfs4_opendata_free
);
1329 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1333 ret
= rpc_wait_for_completion_task(task
);
1337 static bool nfs4_mode_match_open_stateid(struct nfs4_state
*state
,
1340 switch(fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1341 case FMODE_READ
|FMODE_WRITE
:
1342 return state
->n_rdwr
!= 0;
1344 return state
->n_wronly
!= 0;
1346 return state
->n_rdonly
!= 0;
1352 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1356 if (open_mode
& (O_EXCL
|O_TRUNC
))
1358 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1360 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1361 && state
->n_rdonly
!= 0;
1364 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1365 && state
->n_wronly
!= 0;
1367 case FMODE_READ
|FMODE_WRITE
:
1368 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1369 && state
->n_rdwr
!= 0;
1375 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
,
1376 enum open_claim_type4 claim
)
1378 if (delegation
== NULL
)
1380 if ((delegation
->type
& fmode
) != fmode
)
1382 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1385 case NFS4_OPEN_CLAIM_NULL
:
1386 case NFS4_OPEN_CLAIM_FH
:
1388 case NFS4_OPEN_CLAIM_PREVIOUS
:
1389 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1394 nfs_mark_delegation_referenced(delegation
);
1398 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1407 case FMODE_READ
|FMODE_WRITE
:
1410 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1413 #ifdef CONFIG_NFS_V4_1
1414 static bool nfs_open_stateid_recover_openmode(struct nfs4_state
*state
)
1416 if (state
->n_rdonly
&& !test_bit(NFS_O_RDONLY_STATE
, &state
->flags
))
1418 if (state
->n_wronly
&& !test_bit(NFS_O_WRONLY_STATE
, &state
->flags
))
1420 if (state
->n_rdwr
&& !test_bit(NFS_O_RDWR_STATE
, &state
->flags
))
1424 #endif /* CONFIG_NFS_V4_1 */
1426 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1428 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1429 bool need_recover
= false;
1431 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1432 need_recover
= true;
1433 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1434 need_recover
= true;
1435 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1436 need_recover
= true;
1438 nfs4_state_mark_reclaim_nograce(clp
, state
);
1441 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1442 const nfs4_stateid
*stateid
, nfs4_stateid
*freeme
)
1444 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1446 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1447 nfs4_stateid_copy(freeme
, &state
->open_stateid
);
1448 nfs_test_and_clear_all_open_stateid(state
);
1451 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1456 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1458 if (!(state
->n_wronly
|| state
->n_rdonly
|| state
->n_rdwr
))
1460 if (state
->n_wronly
)
1461 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1462 if (state
->n_rdonly
)
1463 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1465 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1466 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1469 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1470 nfs4_stateid
*stateid
, fmode_t fmode
)
1472 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1473 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1475 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1478 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1481 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1482 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1483 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1485 if (stateid
== NULL
)
1487 /* Handle OPEN+OPEN_DOWNGRADE races */
1488 if (nfs4_stateid_match_other(stateid
, &state
->open_stateid
) &&
1489 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
)) {
1490 nfs_resync_open_stateid_locked(state
);
1493 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1494 nfs4_stateid_copy(&state
->stateid
, stateid
);
1495 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1498 static void nfs_clear_open_stateid(struct nfs4_state
*state
,
1499 nfs4_stateid
*arg_stateid
,
1500 nfs4_stateid
*stateid
, fmode_t fmode
)
1502 write_seqlock(&state
->seqlock
);
1503 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1504 if (nfs4_state_match_open_stateid_other(state
, arg_stateid
))
1505 nfs_clear_open_stateid_locked(state
, stateid
, fmode
);
1506 write_sequnlock(&state
->seqlock
);
1507 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1508 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1511 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
,
1512 const nfs4_stateid
*stateid
, fmode_t fmode
,
1513 nfs4_stateid
*freeme
)
1517 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1520 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1522 case FMODE_READ
|FMODE_WRITE
:
1523 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1525 if (!nfs_need_update_open_stateid(state
, stateid
, freeme
))
1527 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1528 nfs4_stateid_copy(&state
->stateid
, stateid
);
1529 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1532 static void __update_open_stateid(struct nfs4_state
*state
,
1533 const nfs4_stateid
*open_stateid
,
1534 const nfs4_stateid
*deleg_stateid
,
1536 nfs4_stateid
*freeme
)
1539 * Protect the call to nfs4_state_set_mode_locked and
1540 * serialise the stateid update
1542 spin_lock(&state
->owner
->so_lock
);
1543 write_seqlock(&state
->seqlock
);
1544 if (deleg_stateid
!= NULL
) {
1545 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1546 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1548 if (open_stateid
!= NULL
)
1549 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
, freeme
);
1550 write_sequnlock(&state
->seqlock
);
1551 update_open_stateflags(state
, fmode
);
1552 spin_unlock(&state
->owner
->so_lock
);
1555 static int update_open_stateid(struct nfs4_state
*state
,
1556 const nfs4_stateid
*open_stateid
,
1557 const nfs4_stateid
*delegation
,
1560 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1561 struct nfs_client
*clp
= server
->nfs_client
;
1562 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1563 struct nfs_delegation
*deleg_cur
;
1564 nfs4_stateid freeme
= { };
1567 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1570 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1571 if (deleg_cur
== NULL
)
1574 spin_lock(&deleg_cur
->lock
);
1575 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1576 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1577 (deleg_cur
->type
& fmode
) != fmode
)
1578 goto no_delegation_unlock
;
1580 if (delegation
== NULL
)
1581 delegation
= &deleg_cur
->stateid
;
1582 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1583 goto no_delegation_unlock
;
1585 nfs_mark_delegation_referenced(deleg_cur
);
1586 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
,
1589 no_delegation_unlock
:
1590 spin_unlock(&deleg_cur
->lock
);
1594 if (!ret
&& open_stateid
!= NULL
) {
1595 __update_open_stateid(state
, open_stateid
, NULL
, fmode
, &freeme
);
1598 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1599 nfs4_schedule_state_manager(clp
);
1600 if (freeme
.type
!= 0)
1601 nfs4_test_and_free_stateid(server
, &freeme
,
1602 state
->owner
->so_cred
);
1607 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1608 const nfs4_stateid
*stateid
)
1610 struct nfs4_state
*state
= lsp
->ls_state
;
1613 spin_lock(&state
->state_lock
);
1614 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1616 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1618 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1621 spin_unlock(&state
->state_lock
);
1625 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1627 struct nfs_delegation
*delegation
;
1630 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1631 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1636 nfs4_inode_return_delegation(inode
);
1639 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1641 struct nfs4_state
*state
= opendata
->state
;
1642 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1643 struct nfs_delegation
*delegation
;
1644 int open_mode
= opendata
->o_arg
.open_flags
;
1645 fmode_t fmode
= opendata
->o_arg
.fmode
;
1646 enum open_claim_type4 claim
= opendata
->o_arg
.claim
;
1647 nfs4_stateid stateid
;
1651 spin_lock(&state
->owner
->so_lock
);
1652 if (can_open_cached(state
, fmode
, open_mode
)) {
1653 update_open_stateflags(state
, fmode
);
1654 spin_unlock(&state
->owner
->so_lock
);
1655 goto out_return_state
;
1657 spin_unlock(&state
->owner
->so_lock
);
1659 delegation
= rcu_dereference(nfsi
->delegation
);
1660 if (!can_open_delegated(delegation
, fmode
, claim
)) {
1664 /* Save the delegation */
1665 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1667 nfs_release_seqid(opendata
->o_arg
.seqid
);
1668 if (!opendata
->is_recover
) {
1669 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1675 /* Try to update the stateid using the delegation */
1676 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1677 goto out_return_state
;
1680 return ERR_PTR(ret
);
1682 atomic_inc(&state
->count
);
1687 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1689 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1690 struct nfs_delegation
*delegation
;
1691 int delegation_flags
= 0;
1694 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1696 delegation_flags
= delegation
->flags
;
1698 switch (data
->o_arg
.claim
) {
1701 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1702 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1703 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1704 "returning a delegation for "
1705 "OPEN(CLAIM_DELEGATE_CUR)\n",
1709 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1710 nfs_inode_set_delegation(state
->inode
,
1711 data
->owner
->so_cred
,
1714 nfs_inode_reclaim_delegation(state
->inode
,
1715 data
->owner
->so_cred
,
1720 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1721 * and update the nfs4_state.
1723 static struct nfs4_state
*
1724 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1726 struct inode
*inode
= data
->state
->inode
;
1727 struct nfs4_state
*state
= data
->state
;
1730 if (!data
->rpc_done
) {
1731 if (data
->rpc_status
) {
1732 ret
= data
->rpc_status
;
1735 /* cached opens have already been processed */
1739 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1743 if (data
->o_res
.delegation_type
!= 0)
1744 nfs4_opendata_check_deleg(data
, state
);
1746 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1748 atomic_inc(&state
->count
);
1752 return ERR_PTR(ret
);
1756 static struct nfs4_state
*
1757 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1759 struct inode
*inode
;
1760 struct nfs4_state
*state
= NULL
;
1763 if (!data
->rpc_done
) {
1764 state
= nfs4_try_open_cached(data
);
1765 trace_nfs4_cached_open(data
->state
);
1770 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1772 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1773 ret
= PTR_ERR(inode
);
1777 state
= nfs4_get_open_state(inode
, data
->owner
);
1780 if (data
->o_res
.delegation_type
!= 0)
1781 nfs4_opendata_check_deleg(data
, state
);
1782 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1786 nfs_release_seqid(data
->o_arg
.seqid
);
1791 return ERR_PTR(ret
);
1794 static struct nfs4_state
*
1795 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1797 struct nfs4_state
*ret
;
1799 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1800 ret
=_nfs4_opendata_reclaim_to_nfs4_state(data
);
1802 ret
= _nfs4_opendata_to_nfs4_state(data
);
1803 nfs4_sequence_free_slot(&data
->o_res
.seq_res
);
1807 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1809 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1810 struct nfs_open_context
*ctx
;
1812 spin_lock(&state
->inode
->i_lock
);
1813 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1814 if (ctx
->state
!= state
)
1816 get_nfs_open_context(ctx
);
1817 spin_unlock(&state
->inode
->i_lock
);
1820 spin_unlock(&state
->inode
->i_lock
);
1821 return ERR_PTR(-ENOENT
);
1824 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1825 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1827 struct nfs4_opendata
*opendata
;
1829 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1830 NULL
, NULL
, claim
, GFP_NOFS
);
1831 if (opendata
== NULL
)
1832 return ERR_PTR(-ENOMEM
);
1833 opendata
->state
= state
;
1834 atomic_inc(&state
->count
);
1838 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
,
1841 struct nfs4_state
*newstate
;
1844 if (!nfs4_mode_match_open_stateid(opendata
->state
, fmode
))
1846 opendata
->o_arg
.open_flags
= 0;
1847 opendata
->o_arg
.fmode
= fmode
;
1848 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1849 NFS_SB(opendata
->dentry
->d_sb
),
1851 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1852 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1853 nfs4_init_opendata_res(opendata
);
1854 ret
= _nfs4_recover_proc_open(opendata
);
1857 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1858 if (IS_ERR(newstate
))
1859 return PTR_ERR(newstate
);
1860 if (newstate
!= opendata
->state
)
1862 nfs4_close_state(newstate
, fmode
);
1866 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1870 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1871 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1872 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1873 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1874 /* memory barrier prior to reading state->n_* */
1875 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1876 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1878 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1881 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1884 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1888 * We may have performed cached opens for all three recoveries.
1889 * Check if we need to update the current stateid.
1891 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1892 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1893 write_seqlock(&state
->seqlock
);
1894 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1895 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1896 write_sequnlock(&state
->seqlock
);
1903 * reclaim state on the server after a reboot.
1905 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1907 struct nfs_delegation
*delegation
;
1908 struct nfs4_opendata
*opendata
;
1909 fmode_t delegation_type
= 0;
1912 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1913 NFS4_OPEN_CLAIM_PREVIOUS
);
1914 if (IS_ERR(opendata
))
1915 return PTR_ERR(opendata
);
1917 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1918 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1919 delegation_type
= delegation
->type
;
1921 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1922 status
= nfs4_open_recover(opendata
, state
);
1923 nfs4_opendata_put(opendata
);
1927 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1929 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1930 struct nfs4_exception exception
= { };
1933 err
= _nfs4_do_open_reclaim(ctx
, state
);
1934 trace_nfs4_open_reclaim(ctx
, 0, err
);
1935 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1937 if (err
!= -NFS4ERR_DELAY
)
1939 nfs4_handle_exception(server
, err
, &exception
);
1940 } while (exception
.retry
);
1944 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1946 struct nfs_open_context
*ctx
;
1949 ctx
= nfs4_state_find_open_context(state
);
1952 ret
= nfs4_do_open_reclaim(ctx
, state
);
1953 put_nfs_open_context(ctx
);
1957 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1961 printk(KERN_ERR
"NFS: %s: unhandled error "
1962 "%d.\n", __func__
, err
);
1968 case -NFS4ERR_BADSESSION
:
1969 case -NFS4ERR_BADSLOT
:
1970 case -NFS4ERR_BAD_HIGH_SLOT
:
1971 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1972 case -NFS4ERR_DEADSESSION
:
1973 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1974 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1976 case -NFS4ERR_STALE_CLIENTID
:
1977 case -NFS4ERR_STALE_STATEID
:
1978 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1979 /* Don't recall a delegation if it was lost */
1980 nfs4_schedule_lease_recovery(server
->nfs_client
);
1982 case -NFS4ERR_MOVED
:
1983 nfs4_schedule_migration_recovery(server
);
1985 case -NFS4ERR_LEASE_MOVED
:
1986 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1988 case -NFS4ERR_DELEG_REVOKED
:
1989 case -NFS4ERR_ADMIN_REVOKED
:
1990 case -NFS4ERR_EXPIRED
:
1991 case -NFS4ERR_BAD_STATEID
:
1992 case -NFS4ERR_OPENMODE
:
1993 nfs_inode_find_state_and_recover(state
->inode
,
1995 nfs4_schedule_stateid_recovery(server
, state
);
1997 case -NFS4ERR_DELAY
:
1998 case -NFS4ERR_GRACE
:
1999 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2003 case -NFS4ERR_DENIED
:
2004 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
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
, err
);
2043 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
2045 struct nfs4_opendata
*data
= calldata
;
2047 nfs40_setup_sequence(data
->o_arg
.server
->nfs_client
->cl_slot_tbl
,
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
);
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
== 0)
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
);
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
= nfs4_wait_for_completion_rpc_task(task
);
2125 data
->cancelled
= 1;
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
, data
->o_arg
.open_flags
))
2152 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
2153 if (can_open_delegated(delegation
, data
->o_arg
.fmode
, claim
))
2154 goto unlock_no_action
;
2157 /* Update client id. */
2158 data
->o_arg
.clientid
= clp
->cl_clientid
;
2162 case NFS4_OPEN_CLAIM_PREVIOUS
:
2163 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
2164 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
2165 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
2166 case NFS4_OPEN_CLAIM_FH
:
2167 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
2168 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
2170 data
->timestamp
= jiffies
;
2171 if (nfs4_setup_sequence(data
->o_arg
.server
,
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);
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
== 0)
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
);
2280 data
->rpc_status
= 0;
2281 data
->cancelled
= 0;
2282 data
->is_recover
= 0;
2284 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2285 data
->is_recover
= 1;
2287 task
= rpc_run_task(&task_setup_data
);
2289 return PTR_ERR(task
);
2290 status
= nfs4_wait_for_completion_rpc_task(task
);
2292 data
->cancelled
= 1;
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
);
2323 * Additional permission checks in order to distinguish between an
2324 * open for read, and an open for execute. This works around the
2325 * fact that NFSv4 OPEN treats read and execute permissions as being
2327 * Note that in the non-execute case, we want to turn off permission
2328 * checking if we just created a new file (POSIX open() semantics).
2330 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2331 struct nfs4_opendata
*opendata
,
2332 struct nfs4_state
*state
, fmode_t fmode
,
2335 struct nfs_access_entry cache
;
2338 /* access call failed or for some reason the server doesn't
2339 * support any access modes -- defer access call until later */
2340 if (opendata
->o_res
.access_supported
== 0)
2345 * Use openflags to check for exec, because fmode won't
2346 * always have FMODE_EXEC set when file open for exec.
2348 if (openflags
& __FMODE_EXEC
) {
2349 /* ONLY check for exec rights */
2351 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2355 cache
.jiffies
= jiffies
;
2356 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2357 nfs_access_add_cache(state
->inode
, &cache
);
2359 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
2362 /* even though OPEN succeeded, access is denied. Close the file */
2363 nfs4_close_state(state
, fmode
);
2368 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2370 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2372 struct inode
*dir
= d_inode(data
->dir
);
2373 struct nfs_server
*server
= NFS_SERVER(dir
);
2374 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2375 struct nfs_openres
*o_res
= &data
->o_res
;
2378 status
= nfs4_run_open_task(data
, 0);
2379 if (!data
->rpc_done
)
2382 if (status
== -NFS4ERR_BADNAME
&&
2383 !(o_arg
->open_flags
& O_CREAT
))
2388 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2390 if (o_arg
->open_flags
& O_CREAT
) {
2391 if (o_arg
->open_flags
& O_EXCL
)
2392 data
->file_created
= 1;
2393 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2394 data
->file_created
= 1;
2395 if (data
->file_created
|| dir
->i_version
!= o_res
->cinfo
.after
)
2396 update_changeattr(dir
, &o_res
->cinfo
,
2397 o_res
->f_attr
->time_start
);
2399 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2400 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2401 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2402 status
= _nfs4_proc_open_confirm(data
);
2406 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
2407 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2411 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
2413 return nfs4_client_recover_expired_lease(server
->nfs_client
);
2418 * reclaim state on the server after a network partition.
2419 * Assumes caller holds the appropriate lock
2421 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2423 struct nfs4_opendata
*opendata
;
2426 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2427 NFS4_OPEN_CLAIM_FH
);
2428 if (IS_ERR(opendata
))
2429 return PTR_ERR(opendata
);
2430 ret
= nfs4_open_recover(opendata
, state
);
2432 d_drop(ctx
->dentry
);
2433 nfs4_opendata_put(opendata
);
2437 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2439 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2440 struct nfs4_exception exception
= { };
2444 err
= _nfs4_open_expired(ctx
, state
);
2445 trace_nfs4_open_expired(ctx
, 0, err
);
2446 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2451 case -NFS4ERR_GRACE
:
2452 case -NFS4ERR_DELAY
:
2453 nfs4_handle_exception(server
, err
, &exception
);
2456 } while (exception
.retry
);
2461 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2463 struct nfs_open_context
*ctx
;
2466 ctx
= nfs4_state_find_open_context(state
);
2469 ret
= nfs4_do_open_expired(ctx
, state
);
2470 put_nfs_open_context(ctx
);
2474 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
,
2475 const nfs4_stateid
*stateid
)
2477 nfs_remove_bad_delegation(state
->inode
, stateid
);
2478 write_seqlock(&state
->seqlock
);
2479 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2480 write_sequnlock(&state
->seqlock
);
2481 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2484 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2486 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2487 nfs_finish_clear_delegation_stateid(state
, NULL
);
2490 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2492 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2493 nfs40_clear_delegation_stateid(state
);
2494 return nfs4_open_expired(sp
, state
);
2497 static int nfs40_test_and_free_expired_stateid(struct nfs_server
*server
,
2498 nfs4_stateid
*stateid
,
2499 struct rpc_cred
*cred
)
2501 return -NFS4ERR_BAD_STATEID
;
2504 #if defined(CONFIG_NFS_V4_1)
2505 static int nfs41_test_and_free_expired_stateid(struct nfs_server
*server
,
2506 nfs4_stateid
*stateid
,
2507 struct rpc_cred
*cred
)
2511 switch (stateid
->type
) {
2514 case NFS4_INVALID_STATEID_TYPE
:
2515 case NFS4_SPECIAL_STATEID_TYPE
:
2516 return -NFS4ERR_BAD_STATEID
;
2517 case NFS4_REVOKED_STATEID_TYPE
:
2521 status
= nfs41_test_stateid(server
, stateid
, cred
);
2523 case -NFS4ERR_EXPIRED
:
2524 case -NFS4ERR_ADMIN_REVOKED
:
2525 case -NFS4ERR_DELEG_REVOKED
:
2531 /* Ack the revoked state to the server */
2532 nfs41_free_stateid(server
, stateid
, cred
, true);
2533 return -NFS4ERR_EXPIRED
;
2536 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2538 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2539 nfs4_stateid stateid
;
2540 struct nfs_delegation
*delegation
;
2541 struct rpc_cred
*cred
;
2544 /* Get the delegation credential for use by test/free_stateid */
2546 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2547 if (delegation
== NULL
) {
2552 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2553 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) ||
2554 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED
,
2555 &delegation
->flags
)) {
2557 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2561 cred
= get_rpccred(delegation
->cred
);
2563 status
= nfs41_test_and_free_expired_stateid(server
, &stateid
, cred
);
2564 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2565 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
2566 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2572 * nfs41_check_expired_locks - possibly free a lock stateid
2574 * @state: NFSv4 state for an inode
2576 * Returns NFS_OK if recovery for this stateid is now finished.
2577 * Otherwise a negative NFS4ERR value is returned.
2579 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
2581 int status
, ret
= NFS_OK
;
2582 struct nfs4_lock_state
*lsp
, *prev
= NULL
;
2583 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2585 if (!test_bit(LK_STATE_IN_USE
, &state
->flags
))
2588 spin_lock(&state
->state_lock
);
2589 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
2590 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
2591 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
2593 atomic_inc(&lsp
->ls_count
);
2594 spin_unlock(&state
->state_lock
);
2596 nfs4_put_lock_state(prev
);
2599 status
= nfs41_test_and_free_expired_stateid(server
,
2602 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
2603 if (status
== -NFS4ERR_EXPIRED
||
2604 status
== -NFS4ERR_BAD_STATEID
) {
2605 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
2606 lsp
->ls_stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
2607 if (!recover_lost_locks
)
2608 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2609 } else if (status
!= NFS_OK
) {
2611 nfs4_put_lock_state(prev
);
2614 spin_lock(&state
->state_lock
);
2617 spin_unlock(&state
->state_lock
);
2618 nfs4_put_lock_state(prev
);
2624 * nfs41_check_open_stateid - possibly free an open stateid
2626 * @state: NFSv4 state for an inode
2628 * Returns NFS_OK if recovery for this stateid is now finished.
2629 * Otherwise a negative NFS4ERR value is returned.
2631 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2633 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2634 nfs4_stateid
*stateid
= &state
->open_stateid
;
2635 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2638 if (test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0) {
2639 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0) {
2640 if (nfs4_have_delegation(state
->inode
, state
->state
))
2642 return -NFS4ERR_OPENMODE
;
2644 return -NFS4ERR_BAD_STATEID
;
2646 status
= nfs41_test_and_free_expired_stateid(server
, stateid
, cred
);
2647 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2648 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
) {
2649 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2650 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2651 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2652 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2653 stateid
->type
= NFS4_INVALID_STATEID_TYPE
;
2655 if (status
!= NFS_OK
)
2657 if (nfs_open_stateid_recover_openmode(state
))
2658 return -NFS4ERR_OPENMODE
;
2662 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2666 nfs41_check_delegation_stateid(state
);
2667 status
= nfs41_check_expired_locks(state
);
2668 if (status
!= NFS_OK
)
2670 status
= nfs41_check_open_stateid(state
);
2671 if (status
!= NFS_OK
)
2672 status
= nfs4_open_expired(sp
, state
);
2678 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2679 * fields corresponding to attributes that were used to store the verifier.
2680 * Make sure we clobber those fields in the later setattr call
2682 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2683 struct iattr
*sattr
, struct nfs4_label
**label
)
2685 const u32
*attrset
= opendata
->o_res
.attrset
;
2687 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2688 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2689 sattr
->ia_valid
|= ATTR_ATIME
;
2691 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2692 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2693 sattr
->ia_valid
|= ATTR_MTIME
;
2695 /* Except MODE, it seems harmless of setting twice. */
2696 if (opendata
->o_arg
.createmode
!= NFS4_CREATE_EXCLUSIVE
&&
2697 attrset
[1] & FATTR4_WORD1_MODE
)
2698 sattr
->ia_valid
&= ~ATTR_MODE
;
2700 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2704 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2707 struct nfs_open_context
*ctx
)
2709 struct nfs4_state_owner
*sp
= opendata
->owner
;
2710 struct nfs_server
*server
= sp
->so_server
;
2711 struct dentry
*dentry
;
2712 struct nfs4_state
*state
;
2716 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2718 ret
= _nfs4_proc_open(opendata
);
2722 state
= nfs4_opendata_to_nfs4_state(opendata
);
2723 ret
= PTR_ERR(state
);
2727 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2728 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2729 if (opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
)
2730 set_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
);
2732 dentry
= opendata
->dentry
;
2733 if (d_really_is_negative(dentry
)) {
2734 struct dentry
*alias
;
2736 alias
= d_exact_alias(dentry
, state
->inode
);
2738 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2739 /* d_splice_alias() can't fail here - it's a non-directory */
2742 ctx
->dentry
= dentry
= alias
;
2744 nfs_set_verifier(dentry
,
2745 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2748 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2752 if (d_inode(dentry
) == state
->inode
) {
2753 nfs_inode_attach_open_context(ctx
);
2754 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2755 nfs4_schedule_stateid_recovery(server
, state
);
2762 * Returns a referenced nfs4_state
2764 static int _nfs4_do_open(struct inode
*dir
,
2765 struct nfs_open_context
*ctx
,
2767 struct iattr
*sattr
,
2768 struct nfs4_label
*label
,
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 nfs4_label
*olabel
= NULL
;
2783 /* Protect against reboot recovery conflicts */
2785 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2787 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2790 status
= nfs4_recover_expired_lease(server
);
2792 goto err_put_state_owner
;
2793 if (d_really_is_positive(dentry
))
2794 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2796 if (d_really_is_positive(dentry
))
2797 claim
= NFS4_OPEN_CLAIM_FH
;
2798 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2799 label
, claim
, GFP_KERNEL
);
2800 if (opendata
== NULL
)
2801 goto err_put_state_owner
;
2804 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2805 if (IS_ERR(olabel
)) {
2806 status
= PTR_ERR(olabel
);
2807 goto err_opendata_put
;
2811 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2812 if (!opendata
->f_attr
.mdsthreshold
) {
2813 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2814 if (!opendata
->f_attr
.mdsthreshold
)
2815 goto err_free_label
;
2817 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2819 if (d_really_is_positive(dentry
))
2820 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2822 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2824 goto err_free_label
;
2827 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2828 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2829 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2831 * send create attributes which was not set by open
2832 * with an extra setattr.
2834 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2835 nfs_fattr_init(opendata
->o_res
.f_attr
);
2836 status
= nfs4_do_setattr(state
->inode
, cred
,
2837 opendata
->o_res
.f_attr
, sattr
,
2838 ctx
, label
, olabel
);
2840 nfs_setattr_update_inode(state
->inode
, sattr
,
2841 opendata
->o_res
.f_attr
);
2842 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2846 if (opened
&& opendata
->file_created
)
2847 *opened
|= FILE_CREATED
;
2849 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2850 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2851 opendata
->f_attr
.mdsthreshold
= NULL
;
2854 nfs4_label_free(olabel
);
2856 nfs4_opendata_put(opendata
);
2857 nfs4_put_state_owner(sp
);
2860 nfs4_label_free(olabel
);
2862 nfs4_opendata_put(opendata
);
2863 err_put_state_owner
:
2864 nfs4_put_state_owner(sp
);
2870 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2871 struct nfs_open_context
*ctx
,
2873 struct iattr
*sattr
,
2874 struct nfs4_label
*label
,
2877 struct nfs_server
*server
= NFS_SERVER(dir
);
2878 struct nfs4_exception exception
= { };
2879 struct nfs4_state
*res
;
2883 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2885 trace_nfs4_open_file(ctx
, flags
, status
);
2888 /* NOTE: BAD_SEQID means the server and client disagree about the
2889 * book-keeping w.r.t. state-changing operations
2890 * (OPEN/CLOSE/LOCK/LOCKU...)
2891 * It is actually a sign of a bug on the client or on the server.
2893 * If we receive a BAD_SEQID error in the particular case of
2894 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2895 * have unhashed the old state_owner for us, and that we can
2896 * therefore safely retry using a new one. We should still warn
2897 * the user though...
2899 if (status
== -NFS4ERR_BAD_SEQID
) {
2900 pr_warn_ratelimited("NFS: v4 server %s "
2901 " returned a bad sequence-id error!\n",
2902 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2903 exception
.retry
= 1;
2907 * BAD_STATEID on OPEN means that the server cancelled our
2908 * state before it received the OPEN_CONFIRM.
2909 * Recover by retrying the request as per the discussion
2910 * on Page 181 of RFC3530.
2912 if (status
== -NFS4ERR_BAD_STATEID
) {
2913 exception
.retry
= 1;
2916 if (status
== -EAGAIN
) {
2917 /* We must have found a delegation */
2918 exception
.retry
= 1;
2921 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2923 res
= ERR_PTR(nfs4_handle_exception(server
,
2924 status
, &exception
));
2925 } while (exception
.retry
);
2929 static int _nfs4_do_setattr(struct inode
*inode
,
2930 struct nfs_setattrargs
*arg
,
2931 struct nfs_setattrres
*res
,
2932 struct rpc_cred
*cred
,
2933 struct nfs_open_context
*ctx
)
2935 struct nfs_server
*server
= NFS_SERVER(inode
);
2936 struct rpc_message msg
= {
2937 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2942 struct rpc_cred
*delegation_cred
= NULL
;
2943 unsigned long timestamp
= jiffies
;
2948 nfs_fattr_init(res
->fattr
);
2950 /* Servers should only apply open mode checks for file size changes */
2951 truncate
= (arg
->iap
->ia_valid
& ATTR_SIZE
) ? true : false;
2952 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2954 if (nfs4_copy_delegation_stateid(inode
, fmode
, &arg
->stateid
, &delegation_cred
)) {
2955 /* Use that stateid */
2956 } else if (truncate
&& ctx
!= NULL
) {
2957 struct nfs_lock_context
*l_ctx
;
2958 if (!nfs4_valid_open_stateid(ctx
->state
))
2960 l_ctx
= nfs_get_lock_context(ctx
);
2962 return PTR_ERR(l_ctx
);
2963 status
= nfs4_select_rw_stateid(ctx
->state
, FMODE_WRITE
, l_ctx
,
2964 &arg
->stateid
, &delegation_cred
);
2965 nfs_put_lock_context(l_ctx
);
2969 nfs4_stateid_copy(&arg
->stateid
, &zero_stateid
);
2970 if (delegation_cred
)
2971 msg
.rpc_cred
= delegation_cred
;
2973 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
->seq_args
, &res
->seq_res
, 1);
2975 put_rpccred(delegation_cred
);
2976 if (status
== 0 && ctx
!= NULL
)
2977 renew_lease(server
, timestamp
);
2978 trace_nfs4_setattr(inode
, &arg
->stateid
, status
);
2982 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2983 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2984 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
2985 struct nfs4_label
*olabel
)
2987 struct nfs_server
*server
= NFS_SERVER(inode
);
2988 struct nfs4_state
*state
= ctx
? ctx
->state
: NULL
;
2989 struct nfs_setattrargs arg
= {
2990 .fh
= NFS_FH(inode
),
2993 .bitmask
= server
->attr_bitmask
,
2996 struct nfs_setattrres res
= {
3001 struct nfs4_exception exception
= {
3004 .stateid
= &arg
.stateid
,
3008 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
3010 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
3013 err
= _nfs4_do_setattr(inode
, &arg
, &res
, cred
, ctx
);
3015 case -NFS4ERR_OPENMODE
:
3016 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
3017 pr_warn_once("NFSv4: server %s is incorrectly "
3018 "applying open mode checks to "
3019 "a SETATTR that is not "
3020 "changing file size.\n",
3021 server
->nfs_client
->cl_hostname
);
3023 if (state
&& !(state
->state
& FMODE_WRITE
)) {
3025 if (sattr
->ia_valid
& ATTR_OPEN
)
3030 err
= nfs4_handle_exception(server
, err
, &exception
);
3031 } while (exception
.retry
);
3037 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
3039 if (inode
== NULL
|| !nfs_have_layout(inode
))
3042 return pnfs_wait_on_layoutreturn(inode
, task
);
3045 struct nfs4_closedata
{
3046 struct inode
*inode
;
3047 struct nfs4_state
*state
;
3048 struct nfs_closeargs arg
;
3049 struct nfs_closeres res
;
3051 struct nfs4_layoutreturn_args arg
;
3052 struct nfs4_layoutreturn_res res
;
3053 struct nfs4_xdr_opaque_data ld_private
;
3057 struct nfs_fattr fattr
;
3058 unsigned long timestamp
;
3061 static void nfs4_free_closedata(void *data
)
3063 struct nfs4_closedata
*calldata
= data
;
3064 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
3065 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
3067 if (calldata
->lr
.roc
)
3068 pnfs_roc_release(&calldata
->lr
.arg
, &calldata
->lr
.res
,
3069 calldata
->res
.lr_ret
);
3070 nfs4_put_open_state(calldata
->state
);
3071 nfs_free_seqid(calldata
->arg
.seqid
);
3072 nfs4_put_state_owner(sp
);
3073 nfs_sb_deactive(sb
);
3077 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
3079 struct nfs4_closedata
*calldata
= data
;
3080 struct nfs4_state
*state
= calldata
->state
;
3081 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
3082 nfs4_stateid
*res_stateid
= NULL
;
3084 dprintk("%s: begin!\n", __func__
);
3085 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
3087 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
3089 /* Handle Layoutreturn errors */
3090 if (calldata
->arg
.lr_args
&& task
->tk_status
!= 0) {
3091 switch (calldata
->res
.lr_ret
) {
3093 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3096 calldata
->arg
.lr_args
= NULL
;
3097 calldata
->res
.lr_res
= NULL
;
3099 case -NFS4ERR_ADMIN_REVOKED
:
3100 case -NFS4ERR_DELEG_REVOKED
:
3101 case -NFS4ERR_EXPIRED
:
3102 case -NFS4ERR_BAD_STATEID
:
3103 case -NFS4ERR_OLD_STATEID
:
3104 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
3105 case -NFS4ERR_WRONG_CRED
:
3106 calldata
->arg
.lr_args
= NULL
;
3107 calldata
->res
.lr_res
= NULL
;
3108 calldata
->res
.lr_ret
= 0;
3109 rpc_restart_call_prepare(task
);
3114 /* hmm. we are done with the inode, and in the process of freeing
3115 * the state_owner. we keep this around to process errors
3117 switch (task
->tk_status
) {
3119 res_stateid
= &calldata
->res
.stateid
;
3120 renew_lease(server
, calldata
->timestamp
);
3122 case -NFS4ERR_ACCESS
:
3123 if (calldata
->arg
.bitmask
!= NULL
) {
3124 calldata
->arg
.bitmask
= NULL
;
3125 calldata
->res
.fattr
= NULL
;
3126 task
->tk_status
= 0;
3127 rpc_restart_call_prepare(task
);
3132 case -NFS4ERR_ADMIN_REVOKED
:
3133 case -NFS4ERR_STALE_STATEID
:
3134 case -NFS4ERR_EXPIRED
:
3135 nfs4_free_revoked_stateid(server
,
3136 &calldata
->arg
.stateid
,
3137 task
->tk_msg
.rpc_cred
);
3138 case -NFS4ERR_OLD_STATEID
:
3139 case -NFS4ERR_BAD_STATEID
:
3140 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
3141 &state
->open_stateid
)) {
3142 rpc_restart_call_prepare(task
);
3145 if (calldata
->arg
.fmode
== 0)
3148 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
3149 rpc_restart_call_prepare(task
);
3153 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
3154 res_stateid
, calldata
->arg
.fmode
);
3156 nfs_release_seqid(calldata
->arg
.seqid
);
3157 nfs_refresh_inode(calldata
->inode
, &calldata
->fattr
);
3158 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
3161 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
3163 struct nfs4_closedata
*calldata
= data
;
3164 struct nfs4_state
*state
= calldata
->state
;
3165 struct inode
*inode
= calldata
->inode
;
3166 bool is_rdonly
, is_wronly
, is_rdwr
;
3169 dprintk("%s: begin!\n", __func__
);
3170 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3173 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
3174 spin_lock(&state
->owner
->so_lock
);
3175 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
3176 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
3177 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
3178 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
3179 /* Calculate the change in open mode */
3180 calldata
->arg
.fmode
= 0;
3181 if (state
->n_rdwr
== 0) {
3182 if (state
->n_rdonly
== 0)
3183 call_close
|= is_rdonly
;
3185 calldata
->arg
.fmode
|= FMODE_READ
;
3186 if (state
->n_wronly
== 0)
3187 call_close
|= is_wronly
;
3189 calldata
->arg
.fmode
|= FMODE_WRITE
;
3190 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
3191 call_close
|= is_rdwr
;
3193 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
3195 if (!nfs4_valid_open_stateid(state
) ||
3196 test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
3198 spin_unlock(&state
->owner
->so_lock
);
3201 /* Note: exit _without_ calling nfs4_close_done */
3205 if (!calldata
->lr
.roc
&& nfs4_wait_on_layoutreturn(inode
, task
)) {
3206 nfs_release_seqid(calldata
->arg
.seqid
);
3210 if (calldata
->arg
.fmode
== 0)
3211 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
3213 if (calldata
->arg
.fmode
== 0 || calldata
->arg
.fmode
== FMODE_READ
) {
3214 /* Close-to-open cache consistency revalidation */
3215 if (!nfs4_have_delegation(inode
, FMODE_READ
))
3216 calldata
->arg
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
3218 calldata
->arg
.bitmask
= NULL
;
3221 calldata
->arg
.share_access
=
3222 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
3223 calldata
->arg
.fmode
, 0);
3225 if (calldata
->res
.fattr
== NULL
)
3226 calldata
->arg
.bitmask
= NULL
;
3227 else if (calldata
->arg
.bitmask
== NULL
)
3228 calldata
->res
.fattr
= NULL
;
3229 calldata
->timestamp
= jiffies
;
3230 if (nfs4_setup_sequence(NFS_SERVER(inode
),
3231 &calldata
->arg
.seq_args
,
3232 &calldata
->res
.seq_res
,
3234 nfs_release_seqid(calldata
->arg
.seqid
);
3235 dprintk("%s: done!\n", __func__
);
3238 task
->tk_action
= NULL
;
3240 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
3243 static const struct rpc_call_ops nfs4_close_ops
= {
3244 .rpc_call_prepare
= nfs4_close_prepare
,
3245 .rpc_call_done
= nfs4_close_done
,
3246 .rpc_release
= nfs4_free_closedata
,
3250 * It is possible for data to be read/written from a mem-mapped file
3251 * after the sys_close call (which hits the vfs layer as a flush).
3252 * This means that we can't safely call nfsv4 close on a file until
3253 * the inode is cleared. This in turn means that we are not good
3254 * NFSv4 citizens - we do not indicate to the server to update the file's
3255 * share state even when we are done with one of the three share
3256 * stateid's in the inode.
3258 * NOTE: Caller must be holding the sp->so_owner semaphore!
3260 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
3262 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3263 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
3264 struct nfs4_closedata
*calldata
;
3265 struct nfs4_state_owner
*sp
= state
->owner
;
3266 struct rpc_task
*task
;
3267 struct rpc_message msg
= {
3268 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
3269 .rpc_cred
= state
->owner
->so_cred
,
3271 struct rpc_task_setup task_setup_data
= {
3272 .rpc_client
= server
->client
,
3273 .rpc_message
= &msg
,
3274 .callback_ops
= &nfs4_close_ops
,
3275 .workqueue
= nfsiod_workqueue
,
3276 .flags
= RPC_TASK_ASYNC
,
3278 int status
= -ENOMEM
;
3280 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
3281 &task_setup_data
.rpc_client
, &msg
);
3283 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
3284 if (calldata
== NULL
)
3286 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
3287 calldata
->inode
= state
->inode
;
3288 calldata
->state
= state
;
3289 calldata
->arg
.fh
= NFS_FH(state
->inode
);
3290 /* Serialization for the sequence id */
3291 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
3292 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
3293 if (IS_ERR(calldata
->arg
.seqid
))
3294 goto out_free_calldata
;
3295 nfs_fattr_init(&calldata
->fattr
);
3296 calldata
->arg
.fmode
= 0;
3297 calldata
->lr
.arg
.ld_private
= &calldata
->lr
.ld_private
;
3298 calldata
->res
.fattr
= &calldata
->fattr
;
3299 calldata
->res
.seqid
= calldata
->arg
.seqid
;
3300 calldata
->res
.server
= server
;
3301 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3302 calldata
->lr
.roc
= pnfs_roc(state
->inode
,
3303 &calldata
->lr
.arg
, &calldata
->lr
.res
, msg
.rpc_cred
);
3304 if (calldata
->lr
.roc
) {
3305 calldata
->arg
.lr_args
= &calldata
->lr
.arg
;
3306 calldata
->res
.lr_res
= &calldata
->lr
.res
;
3308 nfs_sb_active(calldata
->inode
->i_sb
);
3310 msg
.rpc_argp
= &calldata
->arg
;
3311 msg
.rpc_resp
= &calldata
->res
;
3312 task_setup_data
.callback_data
= calldata
;
3313 task
= rpc_run_task(&task_setup_data
);
3315 return PTR_ERR(task
);
3318 status
= rpc_wait_for_completion_task(task
);
3324 nfs4_put_open_state(state
);
3325 nfs4_put_state_owner(sp
);
3329 static struct inode
*
3330 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3331 int open_flags
, struct iattr
*attr
, int *opened
)
3333 struct nfs4_state
*state
;
3334 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3336 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3338 /* Protect against concurrent sillydeletes */
3339 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3341 nfs4_label_release_security(label
);
3344 return ERR_CAST(state
);
3345 return state
->inode
;
3348 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3350 if (ctx
->state
== NULL
)
3353 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3355 nfs4_close_state(ctx
->state
, ctx
->mode
);
3358 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3359 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3360 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3362 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3364 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3365 struct nfs4_server_caps_arg args
= {
3369 struct nfs4_server_caps_res res
= {};
3370 struct rpc_message msg
= {
3371 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3377 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3378 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3379 FATTR4_WORD0_LINK_SUPPORT
|
3380 FATTR4_WORD0_SYMLINK_SUPPORT
|
3381 FATTR4_WORD0_ACLSUPPORT
;
3383 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3385 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3387 /* Sanity check the server answers */
3388 switch (minorversion
) {
3390 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3391 res
.attr_bitmask
[2] = 0;
3394 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3397 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3399 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3400 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3401 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3402 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3403 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3404 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3405 NFS_CAP_SECURITY_LABEL
);
3406 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3407 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3408 server
->caps
|= NFS_CAP_ACLS
;
3409 if (res
.has_links
!= 0)
3410 server
->caps
|= NFS_CAP_HARDLINKS
;
3411 if (res
.has_symlinks
!= 0)
3412 server
->caps
|= NFS_CAP_SYMLINKS
;
3413 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3414 server
->caps
|= NFS_CAP_FILEID
;
3415 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3416 server
->caps
|= NFS_CAP_MODE
;
3417 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3418 server
->caps
|= NFS_CAP_NLINK
;
3419 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3420 server
->caps
|= NFS_CAP_OWNER
;
3421 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3422 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3423 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3424 server
->caps
|= NFS_CAP_ATIME
;
3425 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3426 server
->caps
|= NFS_CAP_CTIME
;
3427 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3428 server
->caps
|= NFS_CAP_MTIME
;
3429 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3430 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3431 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3433 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3434 sizeof(server
->attr_bitmask
));
3435 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3437 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3438 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3439 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3440 server
->cache_consistency_bitmask
[2] = 0;
3441 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3442 sizeof(server
->exclcreat_bitmask
));
3443 server
->acl_bitmask
= res
.acl_bitmask
;
3444 server
->fh_expire_type
= res
.fh_expire_type
;
3450 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3452 struct nfs4_exception exception
= { };
3455 err
= nfs4_handle_exception(server
,
3456 _nfs4_server_capabilities(server
, fhandle
),
3458 } while (exception
.retry
);
3462 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3463 struct nfs_fsinfo
*info
)
3466 struct nfs4_lookup_root_arg args
= {
3469 struct nfs4_lookup_res res
= {
3471 .fattr
= info
->fattr
,
3474 struct rpc_message msg
= {
3475 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3480 bitmask
[0] = nfs4_fattr_bitmap
[0];
3481 bitmask
[1] = nfs4_fattr_bitmap
[1];
3483 * Process the label in the upcoming getfattr
3485 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3487 nfs_fattr_init(info
->fattr
);
3488 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3491 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3492 struct nfs_fsinfo
*info
)
3494 struct nfs4_exception exception
= { };
3497 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3498 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3501 case -NFS4ERR_WRONGSEC
:
3504 err
= nfs4_handle_exception(server
, err
, &exception
);
3506 } while (exception
.retry
);
3511 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3512 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3514 struct rpc_auth_create_args auth_args
= {
3515 .pseudoflavor
= flavor
,
3517 struct rpc_auth
*auth
;
3520 auth
= rpcauth_create(&auth_args
, server
->client
);
3525 ret
= nfs4_lookup_root(server
, fhandle
, info
);
3531 * Retry pseudoroot lookup with various security flavors. We do this when:
3533 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3534 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3536 * Returns zero on success, or a negative NFS4ERR value, or a
3537 * negative errno value.
3539 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3540 struct nfs_fsinfo
*info
)
3542 /* Per 3530bis 15.33.5 */
3543 static const rpc_authflavor_t flav_array
[] = {
3547 RPC_AUTH_UNIX
, /* courtesy */
3550 int status
= -EPERM
;
3553 if (server
->auth_info
.flavor_len
> 0) {
3554 /* try each flavor specified by user */
3555 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3556 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3557 server
->auth_info
.flavors
[i
]);
3558 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3563 /* no flavors specified by user, try default list */
3564 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3565 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3567 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3574 * -EACCESS could mean that the user doesn't have correct permissions
3575 * to access the mount. It could also mean that we tried to mount
3576 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3577 * existing mount programs don't handle -EACCES very well so it should
3578 * be mapped to -EPERM instead.
3580 if (status
== -EACCES
)
3586 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3587 * @server: initialized nfs_server handle
3588 * @fhandle: we fill in the pseudo-fs root file handle
3589 * @info: we fill in an FSINFO struct
3590 * @auth_probe: probe the auth flavours
3592 * Returns zero on success, or a negative errno.
3594 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3595 struct nfs_fsinfo
*info
,
3601 status
= nfs4_lookup_root(server
, fhandle
, info
);
3603 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3604 status
= server
->nfs_client
->cl_mvops
->find_root_sec(server
,
3608 status
= nfs4_server_capabilities(server
, fhandle
);
3610 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3612 return nfs4_map_errors(status
);
3615 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3616 struct nfs_fsinfo
*info
)
3619 struct nfs_fattr
*fattr
= info
->fattr
;
3620 struct nfs4_label
*label
= NULL
;
3622 error
= nfs4_server_capabilities(server
, mntfh
);
3624 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3628 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3630 return PTR_ERR(label
);
3632 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3634 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3635 goto err_free_label
;
3638 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3639 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3640 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3643 nfs4_label_free(label
);
3649 * Get locations and (maybe) other attributes of a referral.
3650 * Note that we'll actually follow the referral later when
3651 * we detect fsid mismatch in inode revalidation
3653 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3654 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3655 struct nfs_fh
*fhandle
)
3657 int status
= -ENOMEM
;
3658 struct page
*page
= NULL
;
3659 struct nfs4_fs_locations
*locations
= NULL
;
3661 page
= alloc_page(GFP_KERNEL
);
3664 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3665 if (locations
== NULL
)
3668 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3673 * If the fsid didn't change, this is a migration event, not a
3674 * referral. Cause us to drop into the exception handler, which
3675 * will kick off migration recovery.
3677 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3678 dprintk("%s: server did not return a different fsid for"
3679 " a referral at %s\n", __func__
, name
->name
);
3680 status
= -NFS4ERR_MOVED
;
3683 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3684 nfs_fixup_referral_attributes(&locations
->fattr
);
3686 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3687 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3688 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3696 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3697 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3699 struct nfs4_getattr_arg args
= {
3701 .bitmask
= server
->attr_bitmask
,
3703 struct nfs4_getattr_res res
= {
3708 struct rpc_message msg
= {
3709 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3714 args
.bitmask
= nfs4_bitmask(server
, label
);
3716 nfs_fattr_init(fattr
);
3717 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3720 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3721 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3723 struct nfs4_exception exception
= { };
3726 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3727 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3728 err
= nfs4_handle_exception(server
, err
,
3730 } while (exception
.retry
);
3735 * The file is not closed if it is opened due to the a request to change
3736 * the size of the file. The open call will not be needed once the
3737 * VFS layer lookup-intents are implemented.
3739 * Close is called when the inode is destroyed.
3740 * If we haven't opened the file for O_WRONLY, we
3741 * need to in the size_change case to obtain a stateid.
3744 * Because OPEN is always done by name in nfsv4, it is
3745 * possible that we opened a different file by the same
3746 * name. We can recognize this race condition, but we
3747 * can't do anything about it besides returning an error.
3749 * This will be fixed with VFS changes (lookup-intent).
3752 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3753 struct iattr
*sattr
)
3755 struct inode
*inode
= d_inode(dentry
);
3756 struct rpc_cred
*cred
= NULL
;
3757 struct nfs_open_context
*ctx
= NULL
;
3758 struct nfs4_label
*label
= NULL
;
3761 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3762 sattr
->ia_valid
& ATTR_SIZE
&&
3763 sattr
->ia_size
< i_size_read(inode
))
3764 pnfs_commit_and_return_layout(inode
);
3766 nfs_fattr_init(fattr
);
3768 /* Deal with open(O_TRUNC) */
3769 if (sattr
->ia_valid
& ATTR_OPEN
)
3770 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3772 /* Optimization: if the end result is no change, don't RPC */
3773 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3776 /* Search for an existing open(O_WRITE) file */
3777 if (sattr
->ia_valid
& ATTR_FILE
) {
3779 ctx
= nfs_file_open_context(sattr
->ia_file
);
3784 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3786 return PTR_ERR(label
);
3788 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, ctx
, NULL
, label
);
3790 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3791 nfs_setsecurity(inode
, fattr
, label
);
3793 nfs4_label_free(label
);
3797 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3798 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3799 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3801 struct nfs_server
*server
= NFS_SERVER(dir
);
3803 struct nfs4_lookup_arg args
= {
3804 .bitmask
= server
->attr_bitmask
,
3805 .dir_fh
= NFS_FH(dir
),
3808 struct nfs4_lookup_res res
= {
3814 struct rpc_message msg
= {
3815 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3820 args
.bitmask
= nfs4_bitmask(server
, label
);
3822 nfs_fattr_init(fattr
);
3824 dprintk("NFS call lookup %s\n", name
->name
);
3825 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3826 dprintk("NFS reply lookup: %d\n", status
);
3830 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3832 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3833 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3834 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3838 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3839 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3840 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3842 struct nfs4_exception exception
= { };
3843 struct rpc_clnt
*client
= *clnt
;
3846 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3847 trace_nfs4_lookup(dir
, name
, err
);
3849 case -NFS4ERR_BADNAME
:
3852 case -NFS4ERR_MOVED
:
3853 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3854 if (err
== -NFS4ERR_MOVED
)
3855 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3857 case -NFS4ERR_WRONGSEC
:
3859 if (client
!= *clnt
)
3861 client
= nfs4_negotiate_security(client
, dir
, name
);
3863 return PTR_ERR(client
);
3865 exception
.retry
= 1;
3868 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3870 } while (exception
.retry
);
3875 else if (client
!= *clnt
)
3876 rpc_shutdown_client(client
);
3881 static int nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
3882 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3883 struct nfs4_label
*label
)
3886 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3888 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3889 if (client
!= NFS_CLIENT(dir
)) {
3890 rpc_shutdown_client(client
);
3891 nfs_fixup_secinfo_attributes(fattr
);
3897 nfs4_proc_lookup_mountpoint(struct inode
*dir
, const struct qstr
*name
,
3898 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3900 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3903 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3905 return ERR_PTR(status
);
3906 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3909 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3911 struct nfs_server
*server
= NFS_SERVER(inode
);
3912 struct nfs4_accessargs args
= {
3913 .fh
= NFS_FH(inode
),
3914 .bitmask
= server
->cache_consistency_bitmask
,
3916 struct nfs4_accessres res
= {
3919 struct rpc_message msg
= {
3920 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3923 .rpc_cred
= entry
->cred
,
3925 int mode
= entry
->mask
;
3929 * Determine which access bits we want to ask for...
3931 if (mode
& MAY_READ
)
3932 args
.access
|= NFS4_ACCESS_READ
;
3933 if (S_ISDIR(inode
->i_mode
)) {
3934 if (mode
& MAY_WRITE
)
3935 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3936 if (mode
& MAY_EXEC
)
3937 args
.access
|= NFS4_ACCESS_LOOKUP
;
3939 if (mode
& MAY_WRITE
)
3940 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3941 if (mode
& MAY_EXEC
)
3942 args
.access
|= NFS4_ACCESS_EXECUTE
;
3945 res
.fattr
= nfs_alloc_fattr();
3946 if (res
.fattr
== NULL
)
3949 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3951 nfs_access_set_mask(entry
, res
.access
);
3952 nfs_refresh_inode(inode
, res
.fattr
);
3954 nfs_free_fattr(res
.fattr
);
3958 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3960 struct nfs4_exception exception
= { };
3963 err
= _nfs4_proc_access(inode
, entry
);
3964 trace_nfs4_access(inode
, err
);
3965 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3967 } while (exception
.retry
);
3972 * TODO: For the time being, we don't try to get any attributes
3973 * along with any of the zero-copy operations READ, READDIR,
3976 * In the case of the first three, we want to put the GETATTR
3977 * after the read-type operation -- this is because it is hard
3978 * to predict the length of a GETATTR response in v4, and thus
3979 * align the READ data correctly. This means that the GETATTR
3980 * may end up partially falling into the page cache, and we should
3981 * shift it into the 'tail' of the xdr_buf before processing.
3982 * To do this efficiently, we need to know the total length
3983 * of data received, which doesn't seem to be available outside
3986 * In the case of WRITE, we also want to put the GETATTR after
3987 * the operation -- in this case because we want to make sure
3988 * we get the post-operation mtime and size.
3990 * Both of these changes to the XDR layer would in fact be quite
3991 * minor, but I decided to leave them for a subsequent patch.
3993 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3994 unsigned int pgbase
, unsigned int pglen
)
3996 struct nfs4_readlink args
= {
3997 .fh
= NFS_FH(inode
),
4002 struct nfs4_readlink_res res
;
4003 struct rpc_message msg
= {
4004 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
4009 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4012 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
4013 unsigned int pgbase
, unsigned int pglen
)
4015 struct nfs4_exception exception
= { };
4018 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
4019 trace_nfs4_readlink(inode
, err
);
4020 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4022 } while (exception
.retry
);
4027 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4030 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
4033 struct nfs_server
*server
= NFS_SERVER(dir
);
4034 struct nfs4_label l
, *ilabel
= NULL
;
4035 struct nfs_open_context
*ctx
;
4036 struct nfs4_state
*state
;
4039 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
, NULL
);
4041 return PTR_ERR(ctx
);
4043 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4045 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4046 sattr
->ia_mode
&= ~current_umask();
4047 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
4048 if (IS_ERR(state
)) {
4049 status
= PTR_ERR(state
);
4053 nfs4_label_release_security(ilabel
);
4054 put_nfs_open_context(ctx
);
4058 static int _nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4060 struct nfs_server
*server
= NFS_SERVER(dir
);
4061 struct nfs_removeargs args
= {
4065 struct nfs_removeres res
= {
4068 struct rpc_message msg
= {
4069 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
4073 unsigned long timestamp
= jiffies
;
4076 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
4078 update_changeattr(dir
, &res
.cinfo
, timestamp
);
4082 static int nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4084 struct nfs4_exception exception
= { };
4087 err
= _nfs4_proc_remove(dir
, name
);
4088 trace_nfs4_remove(dir
, name
, err
);
4089 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4091 } while (exception
.retry
);
4095 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
4097 struct nfs_server
*server
= NFS_SERVER(dir
);
4098 struct nfs_removeargs
*args
= msg
->rpc_argp
;
4099 struct nfs_removeres
*res
= msg
->rpc_resp
;
4101 res
->server
= server
;
4102 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
4103 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
4105 nfs_fattr_init(res
->dir_attr
);
4108 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
4110 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
),
4111 &data
->args
.seq_args
,
4116 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
4118 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
4119 struct nfs_removeres
*res
= &data
->res
;
4121 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4123 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
4124 &data
->timeout
) == -EAGAIN
)
4126 if (task
->tk_status
== 0)
4127 update_changeattr(dir
, &res
->cinfo
, res
->dir_attr
->time_start
);
4131 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
4133 struct nfs_server
*server
= NFS_SERVER(dir
);
4134 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
4135 struct nfs_renameres
*res
= msg
->rpc_resp
;
4137 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
4138 res
->server
= server
;
4139 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
4142 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
4144 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
4145 &data
->args
.seq_args
,
4150 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
4151 struct inode
*new_dir
)
4153 struct nfs_renamedata
*data
= task
->tk_calldata
;
4154 struct nfs_renameres
*res
= &data
->res
;
4156 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4158 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
4161 if (task
->tk_status
== 0) {
4162 update_changeattr(old_dir
, &res
->old_cinfo
, res
->old_fattr
->time_start
);
4163 if (new_dir
!= old_dir
)
4164 update_changeattr(new_dir
, &res
->new_cinfo
, res
->new_fattr
->time_start
);
4169 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4171 struct nfs_server
*server
= NFS_SERVER(inode
);
4172 struct nfs4_link_arg arg
= {
4173 .fh
= NFS_FH(inode
),
4174 .dir_fh
= NFS_FH(dir
),
4176 .bitmask
= server
->attr_bitmask
,
4178 struct nfs4_link_res res
= {
4182 struct rpc_message msg
= {
4183 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
4187 int status
= -ENOMEM
;
4189 res
.fattr
= nfs_alloc_fattr();
4190 if (res
.fattr
== NULL
)
4193 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4194 if (IS_ERR(res
.label
)) {
4195 status
= PTR_ERR(res
.label
);
4198 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
4200 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4202 update_changeattr(dir
, &res
.cinfo
, res
.fattr
->time_start
);
4203 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
4205 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
4209 nfs4_label_free(res
.label
);
4212 nfs_free_fattr(res
.fattr
);
4216 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4218 struct nfs4_exception exception
= { };
4221 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4222 _nfs4_proc_link(inode
, dir
, name
),
4224 } while (exception
.retry
);
4228 struct nfs4_createdata
{
4229 struct rpc_message msg
;
4230 struct nfs4_create_arg arg
;
4231 struct nfs4_create_res res
;
4233 struct nfs_fattr fattr
;
4234 struct nfs4_label
*label
;
4237 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
4238 const struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
4240 struct nfs4_createdata
*data
;
4242 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4244 struct nfs_server
*server
= NFS_SERVER(dir
);
4246 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4247 if (IS_ERR(data
->label
))
4250 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
4251 data
->msg
.rpc_argp
= &data
->arg
;
4252 data
->msg
.rpc_resp
= &data
->res
;
4253 data
->arg
.dir_fh
= NFS_FH(dir
);
4254 data
->arg
.server
= server
;
4255 data
->arg
.name
= name
;
4256 data
->arg
.attrs
= sattr
;
4257 data
->arg
.ftype
= ftype
;
4258 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
4259 data
->arg
.umask
= current_umask();
4260 data
->res
.server
= server
;
4261 data
->res
.fh
= &data
->fh
;
4262 data
->res
.fattr
= &data
->fattr
;
4263 data
->res
.label
= data
->label
;
4264 nfs_fattr_init(data
->res
.fattr
);
4272 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
4274 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
4275 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4277 update_changeattr(dir
, &data
->res
.dir_cinfo
,
4278 data
->res
.fattr
->time_start
);
4279 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
4284 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
4286 nfs4_label_free(data
->label
);
4290 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4291 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
4292 struct nfs4_label
*label
)
4294 struct nfs4_createdata
*data
;
4295 int status
= -ENAMETOOLONG
;
4297 if (len
> NFS4_MAXPATHLEN
)
4301 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
4305 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
4306 data
->arg
.u
.symlink
.pages
= &page
;
4307 data
->arg
.u
.symlink
.len
= len
;
4308 data
->arg
.label
= label
;
4310 status
= nfs4_do_create(dir
, dentry
, data
);
4312 nfs4_free_createdata(data
);
4317 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4318 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
4320 struct nfs4_exception exception
= { };
4321 struct nfs4_label l
, *label
= NULL
;
4324 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4327 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4328 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4329 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4331 } while (exception
.retry
);
4333 nfs4_label_release_security(label
);
4337 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4338 struct iattr
*sattr
, struct nfs4_label
*label
)
4340 struct nfs4_createdata
*data
;
4341 int status
= -ENOMEM
;
4343 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4347 data
->arg
.label
= label
;
4348 status
= nfs4_do_create(dir
, dentry
, data
);
4350 nfs4_free_createdata(data
);
4355 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4356 struct iattr
*sattr
)
4358 struct nfs_server
*server
= NFS_SERVER(dir
);
4359 struct nfs4_exception exception
= { };
4360 struct nfs4_label l
, *label
= NULL
;
4363 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4365 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4366 sattr
->ia_mode
&= ~current_umask();
4368 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4369 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4370 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4372 } while (exception
.retry
);
4373 nfs4_label_release_security(label
);
4378 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4379 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4381 struct inode
*dir
= d_inode(dentry
);
4382 struct nfs4_readdir_arg args
= {
4387 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4390 struct nfs4_readdir_res res
;
4391 struct rpc_message msg
= {
4392 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4399 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4401 (unsigned long long)cookie
);
4402 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4403 res
.pgbase
= args
.pgbase
;
4404 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4406 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4407 status
+= args
.pgbase
;
4410 nfs_invalidate_atime(dir
);
4412 dprintk("%s: returns %d\n", __func__
, status
);
4416 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4417 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4419 struct nfs4_exception exception
= { };
4422 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4423 pages
, count
, plus
);
4424 trace_nfs4_readdir(d_inode(dentry
), err
);
4425 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4427 } while (exception
.retry
);
4431 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4432 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4434 struct nfs4_createdata
*data
;
4435 int mode
= sattr
->ia_mode
;
4436 int status
= -ENOMEM
;
4438 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4443 data
->arg
.ftype
= NF4FIFO
;
4444 else if (S_ISBLK(mode
)) {
4445 data
->arg
.ftype
= NF4BLK
;
4446 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4447 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4449 else if (S_ISCHR(mode
)) {
4450 data
->arg
.ftype
= NF4CHR
;
4451 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4452 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4453 } else if (!S_ISSOCK(mode
)) {
4458 data
->arg
.label
= label
;
4459 status
= nfs4_do_create(dir
, dentry
, data
);
4461 nfs4_free_createdata(data
);
4466 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4467 struct iattr
*sattr
, dev_t rdev
)
4469 struct nfs_server
*server
= NFS_SERVER(dir
);
4470 struct nfs4_exception exception
= { };
4471 struct nfs4_label l
, *label
= NULL
;
4474 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4476 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4477 sattr
->ia_mode
&= ~current_umask();
4479 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4480 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4481 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4483 } while (exception
.retry
);
4485 nfs4_label_release_security(label
);
4490 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4491 struct nfs_fsstat
*fsstat
)
4493 struct nfs4_statfs_arg args
= {
4495 .bitmask
= server
->attr_bitmask
,
4497 struct nfs4_statfs_res res
= {
4500 struct rpc_message msg
= {
4501 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4506 nfs_fattr_init(fsstat
->fattr
);
4507 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4510 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4512 struct nfs4_exception exception
= { };
4515 err
= nfs4_handle_exception(server
,
4516 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4518 } while (exception
.retry
);
4522 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4523 struct nfs_fsinfo
*fsinfo
)
4525 struct nfs4_fsinfo_arg args
= {
4527 .bitmask
= server
->attr_bitmask
,
4529 struct nfs4_fsinfo_res res
= {
4532 struct rpc_message msg
= {
4533 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4538 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4541 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4543 struct nfs4_exception exception
= { };
4544 unsigned long now
= jiffies
;
4548 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4549 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4551 nfs4_set_lease_period(server
->nfs_client
,
4552 fsinfo
->lease_time
* HZ
,
4556 err
= nfs4_handle_exception(server
, err
, &exception
);
4557 } while (exception
.retry
);
4561 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4565 nfs_fattr_init(fsinfo
->fattr
);
4566 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4568 /* block layout checks this! */
4569 server
->pnfs_blksize
= fsinfo
->blksize
;
4570 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
);
4576 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4577 struct nfs_pathconf
*pathconf
)
4579 struct nfs4_pathconf_arg args
= {
4581 .bitmask
= server
->attr_bitmask
,
4583 struct nfs4_pathconf_res res
= {
4584 .pathconf
= pathconf
,
4586 struct rpc_message msg
= {
4587 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4592 /* None of the pathconf attributes are mandatory to implement */
4593 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4594 memset(pathconf
, 0, sizeof(*pathconf
));
4598 nfs_fattr_init(pathconf
->fattr
);
4599 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4602 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4603 struct nfs_pathconf
*pathconf
)
4605 struct nfs4_exception exception
= { };
4609 err
= nfs4_handle_exception(server
,
4610 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4612 } while (exception
.retry
);
4616 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4617 const struct nfs_open_context
*ctx
,
4618 const struct nfs_lock_context
*l_ctx
,
4621 return nfs4_select_rw_stateid(ctx
->state
, fmode
, l_ctx
, stateid
, NULL
);
4623 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4625 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4626 const struct nfs_open_context
*ctx
,
4627 const struct nfs_lock_context
*l_ctx
,
4630 nfs4_stateid current_stateid
;
4632 /* If the current stateid represents a lost lock, then exit */
4633 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4635 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4638 static bool nfs4_error_stateid_expired(int err
)
4641 case -NFS4ERR_DELEG_REVOKED
:
4642 case -NFS4ERR_ADMIN_REVOKED
:
4643 case -NFS4ERR_BAD_STATEID
:
4644 case -NFS4ERR_STALE_STATEID
:
4645 case -NFS4ERR_OLD_STATEID
:
4646 case -NFS4ERR_OPENMODE
:
4647 case -NFS4ERR_EXPIRED
:
4653 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4655 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4657 trace_nfs4_read(hdr
, task
->tk_status
);
4658 if (task
->tk_status
< 0) {
4659 struct nfs4_exception exception
= {
4660 .inode
= hdr
->inode
,
4661 .state
= hdr
->args
.context
->state
,
4662 .stateid
= &hdr
->args
.stateid
,
4664 task
->tk_status
= nfs4_async_handle_exception(task
,
4665 server
, task
->tk_status
, &exception
);
4666 if (exception
.retry
) {
4667 rpc_restart_call_prepare(task
);
4672 if (task
->tk_status
> 0)
4673 renew_lease(server
, hdr
->timestamp
);
4677 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4678 struct nfs_pgio_args
*args
)
4681 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4682 nfs4_stateid_is_current(&args
->stateid
,
4687 rpc_restart_call_prepare(task
);
4691 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4694 dprintk("--> %s\n", __func__
);
4696 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4698 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4700 if (task
->tk_status
> 0)
4701 nfs_invalidate_atime(hdr
->inode
);
4702 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4703 nfs4_read_done_cb(task
, hdr
);
4706 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4707 struct rpc_message
*msg
)
4709 hdr
->timestamp
= jiffies
;
4710 if (!hdr
->pgio_done_cb
)
4711 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4712 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4713 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4716 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4717 struct nfs_pgio_header
*hdr
)
4719 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
),
4720 &hdr
->args
.seq_args
,
4724 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4725 hdr
->args
.lock_context
,
4726 hdr
->rw_ops
->rw_mode
) == -EIO
)
4728 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4733 static int nfs4_write_done_cb(struct rpc_task
*task
,
4734 struct nfs_pgio_header
*hdr
)
4736 struct inode
*inode
= hdr
->inode
;
4738 trace_nfs4_write(hdr
, task
->tk_status
);
4739 if (task
->tk_status
< 0) {
4740 struct nfs4_exception exception
= {
4741 .inode
= hdr
->inode
,
4742 .state
= hdr
->args
.context
->state
,
4743 .stateid
= &hdr
->args
.stateid
,
4745 task
->tk_status
= nfs4_async_handle_exception(task
,
4746 NFS_SERVER(inode
), task
->tk_status
,
4748 if (exception
.retry
) {
4749 rpc_restart_call_prepare(task
);
4753 if (task
->tk_status
>= 0) {
4754 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4755 nfs_writeback_update_inode(hdr
);
4760 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4761 struct nfs_pgio_args
*args
)
4764 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4765 nfs4_stateid_is_current(&args
->stateid
,
4770 rpc_restart_call_prepare(task
);
4774 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4776 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4778 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4780 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4781 nfs4_write_done_cb(task
, hdr
);
4785 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4787 /* Don't request attributes for pNFS or O_DIRECT writes */
4788 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4790 /* Otherwise, request attributes if and only if we don't hold
4793 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4796 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4797 struct rpc_message
*msg
)
4799 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4801 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4802 hdr
->args
.bitmask
= NULL
;
4803 hdr
->res
.fattr
= NULL
;
4805 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4807 if (!hdr
->pgio_done_cb
)
4808 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4809 hdr
->res
.server
= server
;
4810 hdr
->timestamp
= jiffies
;
4812 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4813 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4816 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4818 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4819 &data
->args
.seq_args
,
4824 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4826 struct inode
*inode
= data
->inode
;
4828 trace_nfs4_commit(data
, task
->tk_status
);
4829 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4830 NULL
, NULL
) == -EAGAIN
) {
4831 rpc_restart_call_prepare(task
);
4837 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4839 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4841 return data
->commit_done_cb(task
, data
);
4844 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4846 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4848 if (data
->commit_done_cb
== NULL
)
4849 data
->commit_done_cb
= nfs4_commit_done_cb
;
4850 data
->res
.server
= server
;
4851 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4852 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4855 struct nfs4_renewdata
{
4856 struct nfs_client
*client
;
4857 unsigned long timestamp
;
4861 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4862 * standalone procedure for queueing an asynchronous RENEW.
4864 static void nfs4_renew_release(void *calldata
)
4866 struct nfs4_renewdata
*data
= calldata
;
4867 struct nfs_client
*clp
= data
->client
;
4869 if (atomic_read(&clp
->cl_count
) > 1)
4870 nfs4_schedule_state_renewal(clp
);
4871 nfs_put_client(clp
);
4875 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4877 struct nfs4_renewdata
*data
= calldata
;
4878 struct nfs_client
*clp
= data
->client
;
4879 unsigned long timestamp
= data
->timestamp
;
4881 trace_nfs4_renew_async(clp
, task
->tk_status
);
4882 switch (task
->tk_status
) {
4885 case -NFS4ERR_LEASE_MOVED
:
4886 nfs4_schedule_lease_moved_recovery(clp
);
4889 /* Unless we're shutting down, schedule state recovery! */
4890 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4892 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4893 nfs4_schedule_lease_recovery(clp
);
4896 nfs4_schedule_path_down_recovery(clp
);
4898 do_renew_lease(clp
, timestamp
);
4901 static const struct rpc_call_ops nfs4_renew_ops
= {
4902 .rpc_call_done
= nfs4_renew_done
,
4903 .rpc_release
= nfs4_renew_release
,
4906 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4908 struct rpc_message msg
= {
4909 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4913 struct nfs4_renewdata
*data
;
4915 if (renew_flags
== 0)
4917 if (!atomic_inc_not_zero(&clp
->cl_count
))
4919 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4923 data
->timestamp
= jiffies
;
4924 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4925 &nfs4_renew_ops
, data
);
4928 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4930 struct rpc_message msg
= {
4931 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4935 unsigned long now
= jiffies
;
4938 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4941 do_renew_lease(clp
, now
);
4945 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4947 return server
->caps
& NFS_CAP_ACLS
;
4950 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4951 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4954 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4956 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4957 struct page
**pages
)
4959 struct page
*newpage
, **spages
;
4965 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4966 newpage
= alloc_page(GFP_KERNEL
);
4968 if (newpage
== NULL
)
4970 memcpy(page_address(newpage
), buf
, len
);
4975 } while (buflen
!= 0);
4981 __free_page(spages
[rc
-1]);
4985 struct nfs4_cached_acl
{
4991 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4993 struct nfs_inode
*nfsi
= NFS_I(inode
);
4995 spin_lock(&inode
->i_lock
);
4996 kfree(nfsi
->nfs4_acl
);
4997 nfsi
->nfs4_acl
= acl
;
4998 spin_unlock(&inode
->i_lock
);
5001 static void nfs4_zap_acl_attr(struct inode
*inode
)
5003 nfs4_set_cached_acl(inode
, NULL
);
5006 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
5008 struct nfs_inode
*nfsi
= NFS_I(inode
);
5009 struct nfs4_cached_acl
*acl
;
5012 spin_lock(&inode
->i_lock
);
5013 acl
= nfsi
->nfs4_acl
;
5016 if (buf
== NULL
) /* user is just asking for length */
5018 if (acl
->cached
== 0)
5020 ret
= -ERANGE
; /* see getxattr(2) man page */
5021 if (acl
->len
> buflen
)
5023 memcpy(buf
, acl
->data
, acl
->len
);
5027 spin_unlock(&inode
->i_lock
);
5031 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
5033 struct nfs4_cached_acl
*acl
;
5034 size_t buflen
= sizeof(*acl
) + acl_len
;
5036 if (buflen
<= PAGE_SIZE
) {
5037 acl
= kmalloc(buflen
, GFP_KERNEL
);
5041 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
5043 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
5050 nfs4_set_cached_acl(inode
, acl
);
5054 * The getxattr API returns the required buffer length when called with a
5055 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5056 * the required buf. On a NULL buf, we send a page of data to the server
5057 * guessing that the ACL request can be serviced by a page. If so, we cache
5058 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5059 * the cache. If not so, we throw away the page, and cache the required
5060 * length. The next getxattr call will then produce another round trip to
5061 * the server, this time with the input buf of the required size.
5063 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5065 struct page
*pages
[NFS4ACL_MAXPAGES
+ 1] = {NULL
, };
5066 struct nfs_getaclargs args
= {
5067 .fh
= NFS_FH(inode
),
5071 struct nfs_getaclres res
= {
5074 struct rpc_message msg
= {
5075 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
5079 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
) + 1;
5080 int ret
= -ENOMEM
, i
;
5082 if (npages
> ARRAY_SIZE(pages
))
5085 for (i
= 0; i
< npages
; i
++) {
5086 pages
[i
] = alloc_page(GFP_KERNEL
);
5091 /* for decoding across pages */
5092 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
5093 if (!res
.acl_scratch
)
5096 args
.acl_len
= npages
* PAGE_SIZE
;
5098 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5099 __func__
, buf
, buflen
, npages
, args
.acl_len
);
5100 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
5101 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5105 /* Handle the case where the passed-in buffer is too short */
5106 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
5107 /* Did the user only issue a request for the acl length? */
5113 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
5115 if (res
.acl_len
> buflen
) {
5119 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
5124 for (i
= 0; i
< npages
; i
++)
5126 __free_page(pages
[i
]);
5127 if (res
.acl_scratch
)
5128 __free_page(res
.acl_scratch
);
5132 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5134 struct nfs4_exception exception
= { };
5137 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
5138 trace_nfs4_get_acl(inode
, ret
);
5141 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
5142 } while (exception
.retry
);
5146 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
5148 struct nfs_server
*server
= NFS_SERVER(inode
);
5151 if (!nfs4_server_supports_acls(server
))
5153 ret
= nfs_revalidate_inode(server
, inode
);
5156 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
5157 nfs_zap_acl_cache(inode
);
5158 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
5160 /* -ENOENT is returned if there is no ACL or if there is an ACL
5161 * but no cached acl data, just the acl length */
5163 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
5166 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5168 struct nfs_server
*server
= NFS_SERVER(inode
);
5169 struct page
*pages
[NFS4ACL_MAXPAGES
];
5170 struct nfs_setaclargs arg
= {
5171 .fh
= NFS_FH(inode
),
5175 struct nfs_setaclres res
;
5176 struct rpc_message msg
= {
5177 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
5181 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5184 if (!nfs4_server_supports_acls(server
))
5186 if (npages
> ARRAY_SIZE(pages
))
5188 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
5191 nfs4_inode_return_delegation(inode
);
5192 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5195 * Free each page after tx, so the only ref left is
5196 * held by the network stack
5199 put_page(pages
[i
-1]);
5202 * Acl update can result in inode attribute update.
5203 * so mark the attribute cache invalid.
5205 spin_lock(&inode
->i_lock
);
5206 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
5207 spin_unlock(&inode
->i_lock
);
5208 nfs_access_zap_cache(inode
);
5209 nfs_zap_acl_cache(inode
);
5213 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5215 struct nfs4_exception exception
= { };
5218 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
5219 trace_nfs4_set_acl(inode
, err
);
5220 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5222 } while (exception
.retry
);
5226 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5227 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
5230 struct nfs_server
*server
= NFS_SERVER(inode
);
5231 struct nfs_fattr fattr
;
5232 struct nfs4_label label
= {0, 0, buflen
, buf
};
5234 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5235 struct nfs4_getattr_arg arg
= {
5236 .fh
= NFS_FH(inode
),
5239 struct nfs4_getattr_res res
= {
5244 struct rpc_message msg
= {
5245 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
5251 nfs_fattr_init(&fattr
);
5253 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
5256 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
5258 if (buflen
< label
.len
)
5263 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
5266 struct nfs4_exception exception
= { };
5269 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5273 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
5274 trace_nfs4_get_security_label(inode
, err
);
5275 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5277 } while (exception
.retry
);
5281 static int _nfs4_do_set_security_label(struct inode
*inode
,
5282 struct nfs4_label
*ilabel
,
5283 struct nfs_fattr
*fattr
,
5284 struct nfs4_label
*olabel
)
5287 struct iattr sattr
= {0};
5288 struct nfs_server
*server
= NFS_SERVER(inode
);
5289 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5290 struct nfs_setattrargs arg
= {
5291 .fh
= NFS_FH(inode
),
5297 struct nfs_setattrres res
= {
5302 struct rpc_message msg
= {
5303 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
5309 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
5311 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5313 dprintk("%s failed: %d\n", __func__
, status
);
5318 static int nfs4_do_set_security_label(struct inode
*inode
,
5319 struct nfs4_label
*ilabel
,
5320 struct nfs_fattr
*fattr
,
5321 struct nfs4_label
*olabel
)
5323 struct nfs4_exception exception
= { };
5327 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5329 trace_nfs4_set_security_label(inode
, err
);
5330 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5332 } while (exception
.retry
);
5337 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5339 struct nfs4_label ilabel
, *olabel
= NULL
;
5340 struct nfs_fattr fattr
;
5341 struct rpc_cred
*cred
;
5344 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5347 nfs_fattr_init(&fattr
);
5351 ilabel
.label
= (char *)buf
;
5352 ilabel
.len
= buflen
;
5354 cred
= rpc_lookup_cred();
5356 return PTR_ERR(cred
);
5358 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5359 if (IS_ERR(olabel
)) {
5360 status
= -PTR_ERR(olabel
);
5364 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5366 nfs_setsecurity(inode
, &fattr
, olabel
);
5368 nfs4_label_free(olabel
);
5373 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5376 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5377 nfs4_verifier
*bootverf
)
5381 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5382 /* An impossible timestamp guarantees this value
5383 * will never match a generated boot time. */
5384 verf
[0] = cpu_to_be32(U32_MAX
);
5385 verf
[1] = cpu_to_be32(U32_MAX
);
5387 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5388 u64 ns
= ktime_to_ns(nn
->boot_time
);
5390 verf
[0] = cpu_to_be32(ns
>> 32);
5391 verf
[1] = cpu_to_be32(ns
);
5393 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5397 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5402 if (clp
->cl_owner_id
!= NULL
)
5406 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5407 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5409 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
5413 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5417 * Since this string is allocated at mount time, and held until the
5418 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5419 * about a memory-reclaim deadlock.
5421 str
= kmalloc(len
, GFP_KERNEL
);
5426 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5428 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5429 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5432 clp
->cl_owner_id
= str
;
5437 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5442 len
= 10 + 10 + 1 + 10 + 1 +
5443 strlen(nfs4_client_id_uniquifier
) + 1 +
5444 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5446 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5450 * Since this string is allocated at mount time, and held until the
5451 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5452 * about a memory-reclaim deadlock.
5454 str
= kmalloc(len
, GFP_KERNEL
);
5458 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5459 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5460 nfs4_client_id_uniquifier
,
5461 clp
->cl_rpcclient
->cl_nodename
);
5462 clp
->cl_owner_id
= str
;
5467 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5472 if (clp
->cl_owner_id
!= NULL
)
5475 if (nfs4_client_id_uniquifier
[0] != '\0')
5476 return nfs4_init_uniquifier_client_string(clp
);
5478 len
= 10 + 10 + 1 + 10 + 1 +
5479 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5481 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5485 * Since this string is allocated at mount time, and held until the
5486 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5487 * about a memory-reclaim deadlock.
5489 str
= kmalloc(len
, GFP_KERNEL
);
5493 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5494 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5495 clp
->cl_rpcclient
->cl_nodename
);
5496 clp
->cl_owner_id
= str
;
5501 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5502 * services. Advertise one based on the address family of the
5506 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5508 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5509 return scnprintf(buf
, len
, "tcp6");
5511 return scnprintf(buf
, len
, "tcp");
5514 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5516 struct nfs4_setclientid
*sc
= calldata
;
5518 if (task
->tk_status
== 0)
5519 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5522 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5523 .rpc_call_done
= nfs4_setclientid_done
,
5527 * nfs4_proc_setclientid - Negotiate client ID
5528 * @clp: state data structure
5529 * @program: RPC program for NFSv4 callback service
5530 * @port: IP port number for NFS4 callback service
5531 * @cred: RPC credential to use for this call
5532 * @res: where to place the result
5534 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5536 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5537 unsigned short port
, struct rpc_cred
*cred
,
5538 struct nfs4_setclientid_res
*res
)
5540 nfs4_verifier sc_verifier
;
5541 struct nfs4_setclientid setclientid
= {
5542 .sc_verifier
= &sc_verifier
,
5546 struct rpc_message msg
= {
5547 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5548 .rpc_argp
= &setclientid
,
5552 struct rpc_task
*task
;
5553 struct rpc_task_setup task_setup_data
= {
5554 .rpc_client
= clp
->cl_rpcclient
,
5555 .rpc_message
= &msg
,
5556 .callback_ops
= &nfs4_setclientid_ops
,
5557 .callback_data
= &setclientid
,
5558 .flags
= RPC_TASK_TIMEOUT
,
5562 /* nfs_client_id4 */
5563 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5565 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5566 status
= nfs4_init_uniform_client_string(clp
);
5568 status
= nfs4_init_nonuniform_client_string(clp
);
5574 setclientid
.sc_netid_len
=
5575 nfs4_init_callback_netid(clp
,
5576 setclientid
.sc_netid
,
5577 sizeof(setclientid
.sc_netid
));
5578 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5579 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5580 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5582 dprintk("NFS call setclientid auth=%s, '%s'\n",
5583 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5585 task
= rpc_run_task(&task_setup_data
);
5587 status
= PTR_ERR(task
);
5590 status
= task
->tk_status
;
5591 if (setclientid
.sc_cred
) {
5592 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5593 put_rpccred(setclientid
.sc_cred
);
5597 trace_nfs4_setclientid(clp
, status
);
5598 dprintk("NFS reply setclientid: %d\n", status
);
5603 * nfs4_proc_setclientid_confirm - Confirm client ID
5604 * @clp: state data structure
5605 * @res: result of a previous SETCLIENTID
5606 * @cred: RPC credential to use for this call
5608 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5610 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5611 struct nfs4_setclientid_res
*arg
,
5612 struct rpc_cred
*cred
)
5614 struct rpc_message msg
= {
5615 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5621 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5622 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5624 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5625 trace_nfs4_setclientid_confirm(clp
, status
);
5626 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5630 struct nfs4_delegreturndata
{
5631 struct nfs4_delegreturnargs args
;
5632 struct nfs4_delegreturnres res
;
5634 nfs4_stateid stateid
;
5635 unsigned long timestamp
;
5637 struct nfs4_layoutreturn_args arg
;
5638 struct nfs4_layoutreturn_res res
;
5639 struct nfs4_xdr_opaque_data ld_private
;
5643 struct nfs_fattr fattr
;
5645 struct inode
*inode
;
5648 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5650 struct nfs4_delegreturndata
*data
= calldata
;
5652 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5655 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5657 /* Handle Layoutreturn errors */
5658 if (data
->args
.lr_args
&& task
->tk_status
!= 0) {
5659 switch(data
->res
.lr_ret
) {
5661 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5664 data
->args
.lr_args
= NULL
;
5665 data
->res
.lr_res
= NULL
;
5667 case -NFS4ERR_ADMIN_REVOKED
:
5668 case -NFS4ERR_DELEG_REVOKED
:
5669 case -NFS4ERR_EXPIRED
:
5670 case -NFS4ERR_BAD_STATEID
:
5671 case -NFS4ERR_OLD_STATEID
:
5672 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
5673 case -NFS4ERR_WRONG_CRED
:
5674 data
->args
.lr_args
= NULL
;
5675 data
->res
.lr_res
= NULL
;
5676 data
->res
.lr_ret
= 0;
5677 rpc_restart_call_prepare(task
);
5682 switch (task
->tk_status
) {
5684 renew_lease(data
->res
.server
, data
->timestamp
);
5686 case -NFS4ERR_ADMIN_REVOKED
:
5687 case -NFS4ERR_DELEG_REVOKED
:
5688 case -NFS4ERR_EXPIRED
:
5689 nfs4_free_revoked_stateid(data
->res
.server
,
5691 task
->tk_msg
.rpc_cred
);
5692 case -NFS4ERR_BAD_STATEID
:
5693 case -NFS4ERR_OLD_STATEID
:
5694 case -NFS4ERR_STALE_STATEID
:
5695 task
->tk_status
= 0;
5697 case -NFS4ERR_ACCESS
:
5698 if (data
->args
.bitmask
) {
5699 data
->args
.bitmask
= NULL
;
5700 data
->res
.fattr
= NULL
;
5701 task
->tk_status
= 0;
5702 rpc_restart_call_prepare(task
);
5706 if (nfs4_async_handle_error(task
, data
->res
.server
,
5707 NULL
, NULL
) == -EAGAIN
) {
5708 rpc_restart_call_prepare(task
);
5712 data
->rpc_status
= task
->tk_status
;
5715 static void nfs4_delegreturn_release(void *calldata
)
5717 struct nfs4_delegreturndata
*data
= calldata
;
5718 struct inode
*inode
= data
->inode
;
5722 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
,
5724 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5725 nfs_iput_and_deactive(inode
);
5730 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5732 struct nfs4_delegreturndata
*d_data
;
5734 d_data
= (struct nfs4_delegreturndata
*)data
;
5736 if (!d_data
->lr
.roc
&& nfs4_wait_on_layoutreturn(d_data
->inode
, task
))
5739 nfs4_setup_sequence(d_data
->res
.server
,
5740 &d_data
->args
.seq_args
,
5741 &d_data
->res
.seq_res
,
5745 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5746 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5747 .rpc_call_done
= nfs4_delegreturn_done
,
5748 .rpc_release
= nfs4_delegreturn_release
,
5751 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5753 struct nfs4_delegreturndata
*data
;
5754 struct nfs_server
*server
= NFS_SERVER(inode
);
5755 struct rpc_task
*task
;
5756 struct rpc_message msg
= {
5757 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5760 struct rpc_task_setup task_setup_data
= {
5761 .rpc_client
= server
->client
,
5762 .rpc_message
= &msg
,
5763 .callback_ops
= &nfs4_delegreturn_ops
,
5764 .flags
= RPC_TASK_ASYNC
,
5768 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5771 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5773 nfs4_state_protect(server
->nfs_client
,
5774 NFS_SP4_MACH_CRED_CLEANUP
,
5775 &task_setup_data
.rpc_client
, &msg
);
5777 data
->args
.fhandle
= &data
->fh
;
5778 data
->args
.stateid
= &data
->stateid
;
5779 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5780 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5781 nfs4_stateid_copy(&data
->stateid
, stateid
);
5782 data
->res
.fattr
= &data
->fattr
;
5783 data
->res
.server
= server
;
5784 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5785 data
->lr
.arg
.ld_private
= &data
->lr
.ld_private
;
5786 nfs_fattr_init(data
->res
.fattr
);
5787 data
->timestamp
= jiffies
;
5788 data
->rpc_status
= 0;
5789 data
->lr
.roc
= pnfs_roc(inode
, &data
->lr
.arg
, &data
->lr
.res
, cred
);
5790 data
->inode
= nfs_igrab_and_active(inode
);
5793 data
->args
.lr_args
= &data
->lr
.arg
;
5794 data
->res
.lr_res
= &data
->lr
.res
;
5796 } else if (data
->lr
.roc
) {
5797 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
, 0);
5798 data
->lr
.roc
= false;
5801 task_setup_data
.callback_data
= data
;
5802 msg
.rpc_argp
= &data
->args
;
5803 msg
.rpc_resp
= &data
->res
;
5804 task
= rpc_run_task(&task_setup_data
);
5806 return PTR_ERR(task
);
5809 status
= nfs4_wait_for_completion_rpc_task(task
);
5812 status
= data
->rpc_status
;
5818 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5820 struct nfs_server
*server
= NFS_SERVER(inode
);
5821 struct nfs4_exception exception
= { };
5824 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5825 trace_nfs4_delegreturn(inode
, stateid
, err
);
5827 case -NFS4ERR_STALE_STATEID
:
5828 case -NFS4ERR_EXPIRED
:
5832 err
= nfs4_handle_exception(server
, err
, &exception
);
5833 } while (exception
.retry
);
5837 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5839 struct inode
*inode
= state
->inode
;
5840 struct nfs_server
*server
= NFS_SERVER(inode
);
5841 struct nfs_client
*clp
= server
->nfs_client
;
5842 struct nfs_lockt_args arg
= {
5843 .fh
= NFS_FH(inode
),
5846 struct nfs_lockt_res res
= {
5849 struct rpc_message msg
= {
5850 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5853 .rpc_cred
= state
->owner
->so_cred
,
5855 struct nfs4_lock_state
*lsp
;
5858 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5859 status
= nfs4_set_lock_state(state
, request
);
5862 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5863 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5864 arg
.lock_owner
.s_dev
= server
->s_dev
;
5865 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5868 request
->fl_type
= F_UNLCK
;
5870 case -NFS4ERR_DENIED
:
5873 request
->fl_ops
->fl_release_private(request
);
5874 request
->fl_ops
= NULL
;
5879 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5881 struct nfs4_exception exception
= { };
5885 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5886 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5887 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5889 } while (exception
.retry
);
5893 struct nfs4_unlockdata
{
5894 struct nfs_locku_args arg
;
5895 struct nfs_locku_res res
;
5896 struct nfs4_lock_state
*lsp
;
5897 struct nfs_open_context
*ctx
;
5898 struct file_lock fl
;
5899 struct nfs_server
*server
;
5900 unsigned long timestamp
;
5903 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5904 struct nfs_open_context
*ctx
,
5905 struct nfs4_lock_state
*lsp
,
5906 struct nfs_seqid
*seqid
)
5908 struct nfs4_unlockdata
*p
;
5909 struct inode
*inode
= lsp
->ls_state
->inode
;
5911 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5914 p
->arg
.fh
= NFS_FH(inode
);
5916 p
->arg
.seqid
= seqid
;
5917 p
->res
.seqid
= seqid
;
5919 atomic_inc(&lsp
->ls_count
);
5920 /* Ensure we don't close file until we're done freeing locks! */
5921 p
->ctx
= get_nfs_open_context(ctx
);
5922 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5923 p
->server
= NFS_SERVER(inode
);
5927 static void nfs4_locku_release_calldata(void *data
)
5929 struct nfs4_unlockdata
*calldata
= data
;
5930 nfs_free_seqid(calldata
->arg
.seqid
);
5931 nfs4_put_lock_state(calldata
->lsp
);
5932 put_nfs_open_context(calldata
->ctx
);
5936 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5938 struct nfs4_unlockdata
*calldata
= data
;
5940 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5942 switch (task
->tk_status
) {
5944 renew_lease(calldata
->server
, calldata
->timestamp
);
5945 locks_lock_inode_wait(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5946 if (nfs4_update_lock_stateid(calldata
->lsp
,
5947 &calldata
->res
.stateid
))
5949 case -NFS4ERR_ADMIN_REVOKED
:
5950 case -NFS4ERR_EXPIRED
:
5951 nfs4_free_revoked_stateid(calldata
->server
,
5952 &calldata
->arg
.stateid
,
5953 task
->tk_msg
.rpc_cred
);
5954 case -NFS4ERR_BAD_STATEID
:
5955 case -NFS4ERR_OLD_STATEID
:
5956 case -NFS4ERR_STALE_STATEID
:
5957 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5958 &calldata
->lsp
->ls_stateid
))
5959 rpc_restart_call_prepare(task
);
5962 if (nfs4_async_handle_error(task
, calldata
->server
,
5963 NULL
, NULL
) == -EAGAIN
)
5964 rpc_restart_call_prepare(task
);
5966 nfs_release_seqid(calldata
->arg
.seqid
);
5969 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5971 struct nfs4_unlockdata
*calldata
= data
;
5973 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5975 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5976 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5977 /* Note: exit _without_ running nfs4_locku_done */
5980 calldata
->timestamp
= jiffies
;
5981 if (nfs4_setup_sequence(calldata
->server
,
5982 &calldata
->arg
.seq_args
,
5983 &calldata
->res
.seq_res
,
5985 nfs_release_seqid(calldata
->arg
.seqid
);
5988 task
->tk_action
= NULL
;
5990 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5993 static const struct rpc_call_ops nfs4_locku_ops
= {
5994 .rpc_call_prepare
= nfs4_locku_prepare
,
5995 .rpc_call_done
= nfs4_locku_done
,
5996 .rpc_release
= nfs4_locku_release_calldata
,
5999 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
6000 struct nfs_open_context
*ctx
,
6001 struct nfs4_lock_state
*lsp
,
6002 struct nfs_seqid
*seqid
)
6004 struct nfs4_unlockdata
*data
;
6005 struct rpc_message msg
= {
6006 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
6007 .rpc_cred
= ctx
->cred
,
6009 struct rpc_task_setup task_setup_data
= {
6010 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
6011 .rpc_message
= &msg
,
6012 .callback_ops
= &nfs4_locku_ops
,
6013 .workqueue
= nfsiod_workqueue
,
6014 .flags
= RPC_TASK_ASYNC
,
6017 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
6018 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
6020 /* Ensure this is an unlock - when canceling a lock, the
6021 * canceled lock is passed in, and it won't be an unlock.
6023 fl
->fl_type
= F_UNLCK
;
6025 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
6027 nfs_free_seqid(seqid
);
6028 return ERR_PTR(-ENOMEM
);
6031 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6032 msg
.rpc_argp
= &data
->arg
;
6033 msg
.rpc_resp
= &data
->res
;
6034 task_setup_data
.callback_data
= data
;
6035 return rpc_run_task(&task_setup_data
);
6038 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6040 struct inode
*inode
= state
->inode
;
6041 struct nfs4_state_owner
*sp
= state
->owner
;
6042 struct nfs_inode
*nfsi
= NFS_I(inode
);
6043 struct nfs_seqid
*seqid
;
6044 struct nfs4_lock_state
*lsp
;
6045 struct rpc_task
*task
;
6046 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6048 unsigned char fl_flags
= request
->fl_flags
;
6050 status
= nfs4_set_lock_state(state
, request
);
6051 /* Unlock _before_ we do the RPC call */
6052 request
->fl_flags
|= FL_EXISTS
;
6053 /* Exclude nfs_delegation_claim_locks() */
6054 mutex_lock(&sp
->so_delegreturn_mutex
);
6055 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6056 down_read(&nfsi
->rwsem
);
6057 if (locks_lock_inode_wait(inode
, request
) == -ENOENT
) {
6058 up_read(&nfsi
->rwsem
);
6059 mutex_unlock(&sp
->so_delegreturn_mutex
);
6062 up_read(&nfsi
->rwsem
);
6063 mutex_unlock(&sp
->so_delegreturn_mutex
);
6066 /* Is this a delegated lock? */
6067 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6068 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
6070 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
6071 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
6075 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
6076 status
= PTR_ERR(task
);
6079 status
= nfs4_wait_for_completion_rpc_task(task
);
6082 request
->fl_flags
= fl_flags
;
6083 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
6087 struct nfs4_lockdata
{
6088 struct nfs_lock_args arg
;
6089 struct nfs_lock_res res
;
6090 struct nfs4_lock_state
*lsp
;
6091 struct nfs_open_context
*ctx
;
6092 struct file_lock fl
;
6093 unsigned long timestamp
;
6096 struct nfs_server
*server
;
6099 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
6100 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
6103 struct nfs4_lockdata
*p
;
6104 struct inode
*inode
= lsp
->ls_state
->inode
;
6105 struct nfs_server
*server
= NFS_SERVER(inode
);
6106 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6108 p
= kzalloc(sizeof(*p
), gfp_mask
);
6112 p
->arg
.fh
= NFS_FH(inode
);
6114 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
6115 if (IS_ERR(p
->arg
.open_seqid
))
6117 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
6118 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
6119 if (IS_ERR(p
->arg
.lock_seqid
))
6120 goto out_free_seqid
;
6121 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6122 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6123 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
6124 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
6127 atomic_inc(&lsp
->ls_count
);
6128 p
->ctx
= get_nfs_open_context(ctx
);
6129 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
6132 nfs_free_seqid(p
->arg
.open_seqid
);
6138 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
6140 struct nfs4_lockdata
*data
= calldata
;
6141 struct nfs4_state
*state
= data
->lsp
->ls_state
;
6143 dprintk("%s: begin!\n", __func__
);
6144 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
6146 /* Do we need to do an open_to_lock_owner? */
6147 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
6148 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
6149 goto out_release_lock_seqid
;
6151 nfs4_stateid_copy(&data
->arg
.open_stateid
,
6152 &state
->open_stateid
);
6153 data
->arg
.new_lock_owner
= 1;
6154 data
->res
.open_seqid
= data
->arg
.open_seqid
;
6156 data
->arg
.new_lock_owner
= 0;
6157 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
6158 &data
->lsp
->ls_stateid
);
6160 if (!nfs4_valid_open_stateid(state
)) {
6161 data
->rpc_status
= -EBADF
;
6162 task
->tk_action
= NULL
;
6163 goto out_release_open_seqid
;
6165 data
->timestamp
= jiffies
;
6166 if (nfs4_setup_sequence(data
->server
,
6167 &data
->arg
.seq_args
,
6171 out_release_open_seqid
:
6172 nfs_release_seqid(data
->arg
.open_seqid
);
6173 out_release_lock_seqid
:
6174 nfs_release_seqid(data
->arg
.lock_seqid
);
6176 nfs4_sequence_done(task
, &data
->res
.seq_res
);
6177 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
6180 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
6182 struct nfs4_lockdata
*data
= calldata
;
6183 struct nfs4_lock_state
*lsp
= data
->lsp
;
6185 dprintk("%s: begin!\n", __func__
);
6187 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
6190 data
->rpc_status
= task
->tk_status
;
6191 switch (task
->tk_status
) {
6193 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
6195 if (data
->arg
.new_lock
) {
6196 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
6197 if (locks_lock_inode_wait(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
6198 rpc_restart_call_prepare(task
);
6202 if (data
->arg
.new_lock_owner
!= 0) {
6203 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
6204 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
6205 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6206 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
6207 rpc_restart_call_prepare(task
);
6209 case -NFS4ERR_BAD_STATEID
:
6210 case -NFS4ERR_OLD_STATEID
:
6211 case -NFS4ERR_STALE_STATEID
:
6212 case -NFS4ERR_EXPIRED
:
6213 if (data
->arg
.new_lock_owner
!= 0) {
6214 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
6215 &lsp
->ls_state
->open_stateid
))
6216 rpc_restart_call_prepare(task
);
6217 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
6219 rpc_restart_call_prepare(task
);
6221 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
6224 static void nfs4_lock_release(void *calldata
)
6226 struct nfs4_lockdata
*data
= calldata
;
6228 dprintk("%s: begin!\n", __func__
);
6229 nfs_free_seqid(data
->arg
.open_seqid
);
6230 if (data
->cancelled
!= 0) {
6231 struct rpc_task
*task
;
6232 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
6233 data
->arg
.lock_seqid
);
6235 rpc_put_task_async(task
);
6236 dprintk("%s: cancelling lock!\n", __func__
);
6238 nfs_free_seqid(data
->arg
.lock_seqid
);
6239 nfs4_put_lock_state(data
->lsp
);
6240 put_nfs_open_context(data
->ctx
);
6242 dprintk("%s: done!\n", __func__
);
6245 static const struct rpc_call_ops nfs4_lock_ops
= {
6246 .rpc_call_prepare
= nfs4_lock_prepare
,
6247 .rpc_call_done
= nfs4_lock_done
,
6248 .rpc_release
= nfs4_lock_release
,
6251 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
6254 case -NFS4ERR_ADMIN_REVOKED
:
6255 case -NFS4ERR_EXPIRED
:
6256 case -NFS4ERR_BAD_STATEID
:
6257 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6258 if (new_lock_owner
!= 0 ||
6259 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
6260 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
6262 case -NFS4ERR_STALE_STATEID
:
6263 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6264 nfs4_schedule_lease_recovery(server
->nfs_client
);
6268 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
6270 struct nfs4_lockdata
*data
;
6271 struct rpc_task
*task
;
6272 struct rpc_message msg
= {
6273 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
6274 .rpc_cred
= state
->owner
->so_cred
,
6276 struct rpc_task_setup task_setup_data
= {
6277 .rpc_client
= NFS_CLIENT(state
->inode
),
6278 .rpc_message
= &msg
,
6279 .callback_ops
= &nfs4_lock_ops
,
6280 .workqueue
= nfsiod_workqueue
,
6281 .flags
= RPC_TASK_ASYNC
,
6285 dprintk("%s: begin!\n", __func__
);
6286 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
6287 fl
->fl_u
.nfs4_fl
.owner
,
6288 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
6292 data
->arg
.block
= 1;
6293 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6294 msg
.rpc_argp
= &data
->arg
;
6295 msg
.rpc_resp
= &data
->res
;
6296 task_setup_data
.callback_data
= data
;
6297 if (recovery_type
> NFS_LOCK_NEW
) {
6298 if (recovery_type
== NFS_LOCK_RECLAIM
)
6299 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
6300 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
6302 data
->arg
.new_lock
= 1;
6303 task
= rpc_run_task(&task_setup_data
);
6305 return PTR_ERR(task
);
6306 ret
= nfs4_wait_for_completion_rpc_task(task
);
6308 ret
= data
->rpc_status
;
6310 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
6311 data
->arg
.new_lock_owner
, ret
);
6313 data
->cancelled
= 1;
6315 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
6316 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
6320 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
6322 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6323 struct nfs4_exception exception
= {
6324 .inode
= state
->inode
,
6329 /* Cache the lock if possible... */
6330 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6332 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
6333 if (err
!= -NFS4ERR_DELAY
)
6335 nfs4_handle_exception(server
, err
, &exception
);
6336 } while (exception
.retry
);
6340 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6342 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6343 struct nfs4_exception exception
= {
6344 .inode
= state
->inode
,
6348 err
= nfs4_set_lock_state(state
, request
);
6351 if (!recover_lost_locks
) {
6352 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6356 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6358 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6362 case -NFS4ERR_GRACE
:
6363 case -NFS4ERR_DELAY
:
6364 nfs4_handle_exception(server
, err
, &exception
);
6367 } while (exception
.retry
);
6372 #if defined(CONFIG_NFS_V4_1)
6373 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6375 struct nfs4_lock_state
*lsp
;
6378 status
= nfs4_set_lock_state(state
, request
);
6381 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6382 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) ||
6383 test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
6385 status
= nfs4_lock_expired(state
, request
);
6390 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6392 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6393 struct nfs4_state_owner
*sp
= state
->owner
;
6394 unsigned char fl_flags
= request
->fl_flags
;
6397 request
->fl_flags
|= FL_ACCESS
;
6398 status
= locks_lock_inode_wait(state
->inode
, request
);
6401 mutex_lock(&sp
->so_delegreturn_mutex
);
6402 down_read(&nfsi
->rwsem
);
6403 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6404 /* Yes: cache locks! */
6405 /* ...but avoid races with delegation recall... */
6406 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6407 status
= locks_lock_inode_wait(state
->inode
, request
);
6408 up_read(&nfsi
->rwsem
);
6409 mutex_unlock(&sp
->so_delegreturn_mutex
);
6412 up_read(&nfsi
->rwsem
);
6413 mutex_unlock(&sp
->so_delegreturn_mutex
);
6414 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6416 request
->fl_flags
= fl_flags
;
6420 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6422 struct nfs4_exception exception
= {
6424 .inode
= state
->inode
,
6429 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6430 if (err
== -NFS4ERR_DENIED
)
6432 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6434 } while (exception
.retry
);
6438 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6439 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6442 nfs4_retry_setlk_simple(struct nfs4_state
*state
, int cmd
,
6443 struct file_lock
*request
)
6445 int status
= -ERESTARTSYS
;
6446 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6448 while(!signalled()) {
6449 status
= nfs4_proc_setlk(state
, cmd
, request
);
6450 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6452 freezable_schedule_timeout_interruptible(timeout
);
6454 timeout
= min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT
, timeout
);
6455 status
= -ERESTARTSYS
;
6460 #ifdef CONFIG_NFS_V4_1
6461 struct nfs4_lock_waiter
{
6462 struct task_struct
*task
;
6463 struct inode
*inode
;
6464 struct nfs_lowner
*owner
;
6469 nfs4_wake_lock_waiter(wait_queue_t
*wait
, unsigned int mode
, int flags
, void *key
)
6472 struct cb_notify_lock_args
*cbnl
= key
;
6473 struct nfs4_lock_waiter
*waiter
= wait
->private;
6474 struct nfs_lowner
*lowner
= &cbnl
->cbnl_owner
,
6475 *wowner
= waiter
->owner
;
6477 /* Only wake if the callback was for the same owner */
6478 if (lowner
->clientid
!= wowner
->clientid
||
6479 lowner
->id
!= wowner
->id
||
6480 lowner
->s_dev
!= wowner
->s_dev
)
6483 /* Make sure it's for the right inode */
6484 if (nfs_compare_fh(NFS_FH(waiter
->inode
), &cbnl
->cbnl_fh
))
6487 waiter
->notified
= true;
6489 /* override "private" so we can use default_wake_function */
6490 wait
->private = waiter
->task
;
6491 ret
= autoremove_wake_function(wait
, mode
, flags
, key
);
6492 wait
->private = waiter
;
6497 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6499 int status
= -ERESTARTSYS
;
6500 unsigned long flags
;
6501 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
6502 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6503 struct nfs_client
*clp
= server
->nfs_client
;
6504 wait_queue_head_t
*q
= &clp
->cl_lock_waitq
;
6505 struct nfs_lowner owner
= { .clientid
= clp
->cl_clientid
,
6506 .id
= lsp
->ls_seqid
.owner_id
,
6507 .s_dev
= server
->s_dev
};
6508 struct nfs4_lock_waiter waiter
= { .task
= current
,
6509 .inode
= state
->inode
,
6511 .notified
= false };
6514 /* Don't bother with waitqueue if we don't expect a callback */
6515 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
))
6516 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6519 wait
.private = &waiter
;
6520 wait
.func
= nfs4_wake_lock_waiter
;
6521 add_wait_queue(q
, &wait
);
6523 while(!signalled()) {
6524 status
= nfs4_proc_setlk(state
, cmd
, request
);
6525 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6528 status
= -ERESTARTSYS
;
6529 spin_lock_irqsave(&q
->lock
, flags
);
6530 if (waiter
.notified
) {
6531 spin_unlock_irqrestore(&q
->lock
, flags
);
6534 set_current_state(TASK_INTERRUPTIBLE
);
6535 spin_unlock_irqrestore(&q
->lock
, flags
);
6537 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT
);
6540 finish_wait(q
, &wait
);
6543 #else /* !CONFIG_NFS_V4_1 */
6545 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6547 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6552 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6554 struct nfs_open_context
*ctx
;
6555 struct nfs4_state
*state
;
6558 /* verify open state */
6559 ctx
= nfs_file_open_context(filp
);
6562 if (request
->fl_start
< 0 || request
->fl_end
< 0)
6565 if (IS_GETLK(cmd
)) {
6567 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6571 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6574 if (request
->fl_type
== F_UNLCK
) {
6576 return nfs4_proc_unlck(state
, cmd
, request
);
6583 if ((request
->fl_flags
& FL_POSIX
) &&
6584 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6588 * Don't rely on the VFS having checked the file open mode,
6589 * since it won't do this for flock() locks.
6591 switch (request
->fl_type
) {
6593 if (!(filp
->f_mode
& FMODE_READ
))
6597 if (!(filp
->f_mode
& FMODE_WRITE
))
6601 status
= nfs4_set_lock_state(state
, request
);
6605 return nfs4_retry_setlk(state
, cmd
, request
);
6608 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6610 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6613 err
= nfs4_set_lock_state(state
, fl
);
6616 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6617 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6620 struct nfs_release_lockowner_data
{
6621 struct nfs4_lock_state
*lsp
;
6622 struct nfs_server
*server
;
6623 struct nfs_release_lockowner_args args
;
6624 struct nfs_release_lockowner_res res
;
6625 unsigned long timestamp
;
6628 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6630 struct nfs_release_lockowner_data
*data
= calldata
;
6631 struct nfs_server
*server
= data
->server
;
6632 nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
6633 &data
->args
.seq_args
, &data
->res
.seq_res
, task
);
6634 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6635 data
->timestamp
= jiffies
;
6638 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6640 struct nfs_release_lockowner_data
*data
= calldata
;
6641 struct nfs_server
*server
= data
->server
;
6643 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6645 switch (task
->tk_status
) {
6647 renew_lease(server
, data
->timestamp
);
6649 case -NFS4ERR_STALE_CLIENTID
:
6650 case -NFS4ERR_EXPIRED
:
6651 nfs4_schedule_lease_recovery(server
->nfs_client
);
6653 case -NFS4ERR_LEASE_MOVED
:
6654 case -NFS4ERR_DELAY
:
6655 if (nfs4_async_handle_error(task
, server
,
6656 NULL
, NULL
) == -EAGAIN
)
6657 rpc_restart_call_prepare(task
);
6661 static void nfs4_release_lockowner_release(void *calldata
)
6663 struct nfs_release_lockowner_data
*data
= calldata
;
6664 nfs4_free_lock_state(data
->server
, data
->lsp
);
6668 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6669 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6670 .rpc_call_done
= nfs4_release_lockowner_done
,
6671 .rpc_release
= nfs4_release_lockowner_release
,
6675 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6677 struct nfs_release_lockowner_data
*data
;
6678 struct rpc_message msg
= {
6679 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6682 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6685 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6689 data
->server
= server
;
6690 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6691 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6692 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6694 msg
.rpc_argp
= &data
->args
;
6695 msg
.rpc_resp
= &data
->res
;
6696 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6697 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6700 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6702 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6703 struct dentry
*unused
, struct inode
*inode
,
6704 const char *key
, const void *buf
,
6705 size_t buflen
, int flags
)
6707 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6710 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6711 struct dentry
*unused
, struct inode
*inode
,
6712 const char *key
, void *buf
, size_t buflen
)
6714 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6717 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6719 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6722 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6724 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6725 struct dentry
*unused
, struct inode
*inode
,
6726 const char *key
, const void *buf
,
6727 size_t buflen
, int flags
)
6729 if (security_ismaclabel(key
))
6730 return nfs4_set_security_label(inode
, buf
, buflen
);
6735 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6736 struct dentry
*unused
, struct inode
*inode
,
6737 const char *key
, void *buf
, size_t buflen
)
6739 if (security_ismaclabel(key
))
6740 return nfs4_get_security_label(inode
, buf
, buflen
);
6745 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6749 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6750 len
= security_inode_listsecurity(inode
, list
, list_len
);
6751 if (list_len
&& len
> list_len
)
6757 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6758 .prefix
= XATTR_SECURITY_PREFIX
,
6759 .get
= nfs4_xattr_get_nfs4_label
,
6760 .set
= nfs4_xattr_set_nfs4_label
,
6766 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6774 * nfs_fhget will use either the mounted_on_fileid or the fileid
6776 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6778 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6779 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6780 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6781 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6784 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6785 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6786 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6790 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6791 const struct qstr
*name
,
6792 struct nfs4_fs_locations
*fs_locations
,
6795 struct nfs_server
*server
= NFS_SERVER(dir
);
6797 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6799 struct nfs4_fs_locations_arg args
= {
6800 .dir_fh
= NFS_FH(dir
),
6805 struct nfs4_fs_locations_res res
= {
6806 .fs_locations
= fs_locations
,
6808 struct rpc_message msg
= {
6809 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6815 dprintk("%s: start\n", __func__
);
6817 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6818 * is not supported */
6819 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6820 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6822 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6824 nfs_fattr_init(&fs_locations
->fattr
);
6825 fs_locations
->server
= server
;
6826 fs_locations
->nlocations
= 0;
6827 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6828 dprintk("%s: returned status = %d\n", __func__
, status
);
6832 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6833 const struct qstr
*name
,
6834 struct nfs4_fs_locations
*fs_locations
,
6837 struct nfs4_exception exception
= { };
6840 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6841 fs_locations
, page
);
6842 trace_nfs4_get_fs_locations(dir
, name
, err
);
6843 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6845 } while (exception
.retry
);
6850 * This operation also signals the server that this client is
6851 * performing migration recovery. The server can stop returning
6852 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6853 * appended to this compound to identify the client ID which is
6854 * performing recovery.
6856 static int _nfs40_proc_get_locations(struct inode
*inode
,
6857 struct nfs4_fs_locations
*locations
,
6858 struct page
*page
, struct rpc_cred
*cred
)
6860 struct nfs_server
*server
= NFS_SERVER(inode
);
6861 struct rpc_clnt
*clnt
= server
->client
;
6863 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6865 struct nfs4_fs_locations_arg args
= {
6866 .clientid
= server
->nfs_client
->cl_clientid
,
6867 .fh
= NFS_FH(inode
),
6870 .migration
= 1, /* skip LOOKUP */
6871 .renew
= 1, /* append RENEW */
6873 struct nfs4_fs_locations_res res
= {
6874 .fs_locations
= locations
,
6878 struct rpc_message msg
= {
6879 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6884 unsigned long now
= jiffies
;
6887 nfs_fattr_init(&locations
->fattr
);
6888 locations
->server
= server
;
6889 locations
->nlocations
= 0;
6891 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6892 nfs4_set_sequence_privileged(&args
.seq_args
);
6893 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6894 &args
.seq_args
, &res
.seq_res
);
6898 renew_lease(server
, now
);
6902 #ifdef CONFIG_NFS_V4_1
6905 * This operation also signals the server that this client is
6906 * performing migration recovery. The server can stop asserting
6907 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6908 * performing this operation is identified in the SEQUENCE
6909 * operation in this compound.
6911 * When the client supports GETATTR(fs_locations_info), it can
6912 * be plumbed in here.
6914 static int _nfs41_proc_get_locations(struct inode
*inode
,
6915 struct nfs4_fs_locations
*locations
,
6916 struct page
*page
, struct rpc_cred
*cred
)
6918 struct nfs_server
*server
= NFS_SERVER(inode
);
6919 struct rpc_clnt
*clnt
= server
->client
;
6921 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6923 struct nfs4_fs_locations_arg args
= {
6924 .fh
= NFS_FH(inode
),
6927 .migration
= 1, /* skip LOOKUP */
6929 struct nfs4_fs_locations_res res
= {
6930 .fs_locations
= locations
,
6933 struct rpc_message msg
= {
6934 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6941 nfs_fattr_init(&locations
->fattr
);
6942 locations
->server
= server
;
6943 locations
->nlocations
= 0;
6945 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6946 nfs4_set_sequence_privileged(&args
.seq_args
);
6947 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6948 &args
.seq_args
, &res
.seq_res
);
6949 if (status
== NFS4_OK
&&
6950 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6951 status
= -NFS4ERR_LEASE_MOVED
;
6955 #endif /* CONFIG_NFS_V4_1 */
6958 * nfs4_proc_get_locations - discover locations for a migrated FSID
6959 * @inode: inode on FSID that is migrating
6960 * @locations: result of query
6962 * @cred: credential to use for this operation
6964 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6965 * operation failed, or a negative errno if a local error occurred.
6967 * On success, "locations" is filled in, but if the server has
6968 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6971 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6972 * from this client that require migration recovery.
6974 int nfs4_proc_get_locations(struct inode
*inode
,
6975 struct nfs4_fs_locations
*locations
,
6976 struct page
*page
, struct rpc_cred
*cred
)
6978 struct nfs_server
*server
= NFS_SERVER(inode
);
6979 struct nfs_client
*clp
= server
->nfs_client
;
6980 const struct nfs4_mig_recovery_ops
*ops
=
6981 clp
->cl_mvops
->mig_recovery_ops
;
6982 struct nfs4_exception exception
= { };
6985 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6986 (unsigned long long)server
->fsid
.major
,
6987 (unsigned long long)server
->fsid
.minor
,
6989 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6992 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6993 if (status
!= -NFS4ERR_DELAY
)
6995 nfs4_handle_exception(server
, status
, &exception
);
6996 } while (exception
.retry
);
7001 * This operation also signals the server that this client is
7002 * performing "lease moved" recovery. The server can stop
7003 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7004 * is appended to this compound to identify the client ID which is
7005 * performing recovery.
7007 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7009 struct nfs_server
*server
= NFS_SERVER(inode
);
7010 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
7011 struct rpc_clnt
*clnt
= server
->client
;
7012 struct nfs4_fsid_present_arg args
= {
7013 .fh
= NFS_FH(inode
),
7014 .clientid
= clp
->cl_clientid
,
7015 .renew
= 1, /* append RENEW */
7017 struct nfs4_fsid_present_res res
= {
7020 struct rpc_message msg
= {
7021 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7026 unsigned long now
= jiffies
;
7029 res
.fh
= nfs_alloc_fhandle();
7033 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7034 nfs4_set_sequence_privileged(&args
.seq_args
);
7035 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7036 &args
.seq_args
, &res
.seq_res
);
7037 nfs_free_fhandle(res
.fh
);
7041 do_renew_lease(clp
, now
);
7045 #ifdef CONFIG_NFS_V4_1
7048 * This operation also signals the server that this client is
7049 * performing "lease moved" recovery. The server can stop asserting
7050 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7051 * this operation is identified in the SEQUENCE operation in this
7054 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7056 struct nfs_server
*server
= NFS_SERVER(inode
);
7057 struct rpc_clnt
*clnt
= server
->client
;
7058 struct nfs4_fsid_present_arg args
= {
7059 .fh
= NFS_FH(inode
),
7061 struct nfs4_fsid_present_res res
= {
7063 struct rpc_message msg
= {
7064 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7071 res
.fh
= nfs_alloc_fhandle();
7075 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7076 nfs4_set_sequence_privileged(&args
.seq_args
);
7077 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7078 &args
.seq_args
, &res
.seq_res
);
7079 nfs_free_fhandle(res
.fh
);
7080 if (status
== NFS4_OK
&&
7081 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
7082 status
= -NFS4ERR_LEASE_MOVED
;
7086 #endif /* CONFIG_NFS_V4_1 */
7089 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7090 * @inode: inode on FSID to check
7091 * @cred: credential to use for this operation
7093 * Server indicates whether the FSID is present, moved, or not
7094 * recognized. This operation is necessary to clear a LEASE_MOVED
7095 * condition for this client ID.
7097 * Returns NFS4_OK if the FSID is present on this server,
7098 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7099 * NFS4ERR code if some error occurred on the server, or a
7100 * negative errno if a local failure occurred.
7102 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7104 struct nfs_server
*server
= NFS_SERVER(inode
);
7105 struct nfs_client
*clp
= server
->nfs_client
;
7106 const struct nfs4_mig_recovery_ops
*ops
=
7107 clp
->cl_mvops
->mig_recovery_ops
;
7108 struct nfs4_exception exception
= { };
7111 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
7112 (unsigned long long)server
->fsid
.major
,
7113 (unsigned long long)server
->fsid
.minor
,
7115 nfs_display_fhandle(NFS_FH(inode
), __func__
);
7118 status
= ops
->fsid_present(inode
, cred
);
7119 if (status
!= -NFS4ERR_DELAY
)
7121 nfs4_handle_exception(server
, status
, &exception
);
7122 } while (exception
.retry
);
7127 * If 'use_integrity' is true and the state managment nfs_client
7128 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7129 * and the machine credential as per RFC3530bis and RFC5661 Security
7130 * Considerations sections. Otherwise, just use the user cred with the
7131 * filesystem's rpc_client.
7133 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7136 struct nfs4_secinfo_arg args
= {
7137 .dir_fh
= NFS_FH(dir
),
7140 struct nfs4_secinfo_res res
= {
7143 struct rpc_message msg
= {
7144 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
7148 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
7149 struct rpc_cred
*cred
= NULL
;
7151 if (use_integrity
) {
7152 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
7153 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
7154 msg
.rpc_cred
= cred
;
7157 dprintk("NFS call secinfo %s\n", name
->name
);
7159 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
7160 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
7162 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
7164 dprintk("NFS reply secinfo: %d\n", status
);
7172 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
7173 struct nfs4_secinfo_flavors
*flavors
)
7175 struct nfs4_exception exception
= { };
7178 err
= -NFS4ERR_WRONGSEC
;
7180 /* try to use integrity protection with machine cred */
7181 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
7182 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
7185 * if unable to use integrity protection, or SECINFO with
7186 * integrity protection returns NFS4ERR_WRONGSEC (which is
7187 * disallowed by spec, but exists in deployed servers) use
7188 * the current filesystem's rpc_client and the user cred.
7190 if (err
== -NFS4ERR_WRONGSEC
)
7191 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
7193 trace_nfs4_secinfo(dir
, name
, err
);
7194 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
7196 } while (exception
.retry
);
7200 #ifdef CONFIG_NFS_V4_1
7202 * Check the exchange flags returned by the server for invalid flags, having
7203 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7206 static int nfs4_check_cl_exchange_flags(u32 flags
)
7208 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
7210 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
7211 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
7213 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
7217 return -NFS4ERR_INVAL
;
7221 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
7222 struct nfs41_server_scope
*b
)
7224 if (a
->server_scope_sz
== b
->server_scope_sz
&&
7225 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
7232 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
7236 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
7237 .rpc_call_done
= &nfs4_bind_one_conn_to_session_done
,
7241 * nfs4_proc_bind_one_conn_to_session()
7243 * The 4.1 client currently uses the same TCP connection for the
7244 * fore and backchannel.
7247 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
7248 struct rpc_xprt
*xprt
,
7249 struct nfs_client
*clp
,
7250 struct rpc_cred
*cred
)
7253 struct nfs41_bind_conn_to_session_args args
= {
7255 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
7257 struct nfs41_bind_conn_to_session_res res
;
7258 struct rpc_message msg
= {
7260 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
7265 struct rpc_task_setup task_setup_data
= {
7268 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
7269 .rpc_message
= &msg
,
7270 .flags
= RPC_TASK_TIMEOUT
,
7272 struct rpc_task
*task
;
7274 dprintk("--> %s\n", __func__
);
7276 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
7277 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
7278 args
.dir
= NFS4_CDFC4_FORE
;
7280 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7281 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
7282 args
.dir
= NFS4_CDFC4_FORE
;
7284 task
= rpc_run_task(&task_setup_data
);
7285 if (!IS_ERR(task
)) {
7286 status
= task
->tk_status
;
7289 status
= PTR_ERR(task
);
7290 trace_nfs4_bind_conn_to_session(clp
, status
);
7292 if (memcmp(res
.sessionid
.data
,
7293 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
7294 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
7298 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
7299 dprintk("NFS: %s: Unexpected direction from server\n",
7304 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
7305 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7312 dprintk("<-- %s status= %d\n", __func__
, status
);
7316 struct rpc_bind_conn_calldata
{
7317 struct nfs_client
*clp
;
7318 struct rpc_cred
*cred
;
7322 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
7323 struct rpc_xprt
*xprt
,
7326 struct rpc_bind_conn_calldata
*p
= calldata
;
7328 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
7331 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7333 struct rpc_bind_conn_calldata data
= {
7337 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
7338 nfs4_proc_bind_conn_to_session_callback
, &data
);
7342 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7343 * and operations we'd like to see to enable certain features in the allow map
7345 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
7346 .how
= SP4_MACH_CRED
,
7347 .enforce
.u
.words
= {
7348 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7349 1 << (OP_EXCHANGE_ID
- 32) |
7350 1 << (OP_CREATE_SESSION
- 32) |
7351 1 << (OP_DESTROY_SESSION
- 32) |
7352 1 << (OP_DESTROY_CLIENTID
- 32)
7355 [0] = 1 << (OP_CLOSE
) |
7356 1 << (OP_OPEN_DOWNGRADE
) |
7358 1 << (OP_DELEGRETURN
) |
7360 [1] = 1 << (OP_SECINFO
- 32) |
7361 1 << (OP_SECINFO_NO_NAME
- 32) |
7362 1 << (OP_LAYOUTRETURN
- 32) |
7363 1 << (OP_TEST_STATEID
- 32) |
7364 1 << (OP_FREE_STATEID
- 32) |
7365 1 << (OP_WRITE
- 32)
7370 * Select the state protection mode for client `clp' given the server results
7371 * from exchange_id in `sp'.
7373 * Returns 0 on success, negative errno otherwise.
7375 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
7376 struct nfs41_state_protection
*sp
)
7378 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
7379 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7380 1 << (OP_EXCHANGE_ID
- 32) |
7381 1 << (OP_CREATE_SESSION
- 32) |
7382 1 << (OP_DESTROY_SESSION
- 32) |
7383 1 << (OP_DESTROY_CLIENTID
- 32)
7387 if (sp
->how
== SP4_MACH_CRED
) {
7388 /* Print state protect result */
7389 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
7390 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
7391 if (test_bit(i
, sp
->enforce
.u
.longs
))
7392 dfprintk(MOUNT
, " enforce op %d\n", i
);
7393 if (test_bit(i
, sp
->allow
.u
.longs
))
7394 dfprintk(MOUNT
, " allow op %d\n", i
);
7397 /* make sure nothing is on enforce list that isn't supported */
7398 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
7399 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
7400 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7406 * Minimal mode - state operations are allowed to use machine
7407 * credential. Note this already happens by default, so the
7408 * client doesn't have to do anything more than the negotiation.
7410 * NOTE: we don't care if EXCHANGE_ID is in the list -
7411 * we're already using the machine cred for exchange_id
7412 * and will never use a different cred.
7414 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
7415 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
7416 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
7417 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
7418 dfprintk(MOUNT
, "sp4_mach_cred:\n");
7419 dfprintk(MOUNT
, " minimal mode enabled\n");
7420 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
7422 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7426 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7427 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7428 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7429 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7430 dfprintk(MOUNT
, " cleanup mode enabled\n");
7431 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
7434 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7435 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7436 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
7437 &clp
->cl_sp4_flags
);
7440 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7441 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7442 dfprintk(MOUNT
, " secinfo mode enabled\n");
7443 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
7446 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7447 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7448 dfprintk(MOUNT
, " stateid mode enabled\n");
7449 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
7452 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7453 dfprintk(MOUNT
, " write mode enabled\n");
7454 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
7457 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7458 dfprintk(MOUNT
, " commit mode enabled\n");
7459 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
7466 struct nfs41_exchange_id_data
{
7467 struct nfs41_exchange_id_res res
;
7468 struct nfs41_exchange_id_args args
;
7469 struct rpc_xprt
*xprt
;
7473 static void nfs4_exchange_id_done(struct rpc_task
*task
, void *data
)
7475 struct nfs41_exchange_id_data
*cdata
=
7476 (struct nfs41_exchange_id_data
*)data
;
7477 struct nfs_client
*clp
= cdata
->args
.client
;
7478 int status
= task
->tk_status
;
7480 trace_nfs4_exchange_id(clp
, status
);
7483 status
= nfs4_check_cl_exchange_flags(cdata
->res
.flags
);
7485 if (cdata
->xprt
&& status
== 0) {
7486 status
= nfs4_detect_session_trunking(clp
, &cdata
->res
,
7492 status
= nfs4_sp4_select_mode(clp
, &cdata
->res
.state_protect
);
7495 clp
->cl_clientid
= cdata
->res
.clientid
;
7496 clp
->cl_exchange_flags
= cdata
->res
.flags
;
7497 /* Client ID is not confirmed */
7498 if (!(cdata
->res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
)) {
7499 clear_bit(NFS4_SESSION_ESTABLISHED
,
7500 &clp
->cl_session
->session_state
);
7501 clp
->cl_seqid
= cdata
->res
.seqid
;
7504 kfree(clp
->cl_serverowner
);
7505 clp
->cl_serverowner
= cdata
->res
.server_owner
;
7506 cdata
->res
.server_owner
= NULL
;
7508 /* use the most recent implementation id */
7509 kfree(clp
->cl_implid
);
7510 clp
->cl_implid
= cdata
->res
.impl_id
;
7511 cdata
->res
.impl_id
= NULL
;
7513 if (clp
->cl_serverscope
!= NULL
&&
7514 !nfs41_same_server_scope(clp
->cl_serverscope
,
7515 cdata
->res
.server_scope
)) {
7516 dprintk("%s: server_scope mismatch detected\n",
7518 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7519 kfree(clp
->cl_serverscope
);
7520 clp
->cl_serverscope
= NULL
;
7523 if (clp
->cl_serverscope
== NULL
) {
7524 clp
->cl_serverscope
= cdata
->res
.server_scope
;
7525 cdata
->res
.server_scope
= NULL
;
7527 /* Save the EXCHANGE_ID verifier session trunk tests */
7528 memcpy(clp
->cl_confirm
.data
, cdata
->args
.verifier
->data
,
7529 sizeof(clp
->cl_confirm
.data
));
7532 cdata
->rpc_status
= status
;
7536 static void nfs4_exchange_id_release(void *data
)
7538 struct nfs41_exchange_id_data
*cdata
=
7539 (struct nfs41_exchange_id_data
*)data
;
7542 xprt_put(cdata
->xprt
);
7543 rpc_clnt_xprt_switch_put(cdata
->args
.client
->cl_rpcclient
);
7545 nfs_put_client(cdata
->args
.client
);
7546 kfree(cdata
->res
.impl_id
);
7547 kfree(cdata
->res
.server_scope
);
7548 kfree(cdata
->res
.server_owner
);
7552 static const struct rpc_call_ops nfs4_exchange_id_call_ops
= {
7553 .rpc_call_done
= nfs4_exchange_id_done
,
7554 .rpc_release
= nfs4_exchange_id_release
,
7558 * _nfs4_proc_exchange_id()
7560 * Wrapper for EXCHANGE_ID operation.
7562 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7563 u32 sp4_how
, struct rpc_xprt
*xprt
)
7565 nfs4_verifier verifier
;
7566 struct rpc_message msg
= {
7567 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7570 struct rpc_task_setup task_setup_data
= {
7571 .rpc_client
= clp
->cl_rpcclient
,
7572 .callback_ops
= &nfs4_exchange_id_call_ops
,
7573 .rpc_message
= &msg
,
7574 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7576 struct nfs41_exchange_id_data
*calldata
;
7577 struct rpc_task
*task
;
7580 if (!atomic_inc_not_zero(&clp
->cl_count
))
7584 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7589 nfs4_init_boot_verifier(clp
, &verifier
);
7591 status
= nfs4_init_uniform_client_string(clp
);
7595 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7596 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
7599 calldata
->res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7602 if (unlikely(calldata
->res
.server_owner
== NULL
))
7605 calldata
->res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7607 if (unlikely(calldata
->res
.server_scope
== NULL
))
7608 goto out_server_owner
;
7610 calldata
->res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7611 if (unlikely(calldata
->res
.impl_id
== NULL
))
7612 goto out_server_scope
;
7616 calldata
->args
.state_protect
.how
= SP4_NONE
;
7620 calldata
->args
.state_protect
= nfs4_sp4_mach_cred_request
;
7630 calldata
->xprt
= xprt
;
7631 task_setup_data
.rpc_xprt
= xprt
;
7632 task_setup_data
.flags
=
7633 RPC_TASK_SOFT
|RPC_TASK_SOFTCONN
|RPC_TASK_ASYNC
;
7634 calldata
->args
.verifier
= &clp
->cl_confirm
;
7636 calldata
->args
.verifier
= &verifier
;
7638 calldata
->args
.client
= clp
;
7639 #ifdef CONFIG_NFS_V4_1_MIGRATION
7640 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7641 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
7642 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
7644 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7645 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
7647 msg
.rpc_argp
= &calldata
->args
;
7648 msg
.rpc_resp
= &calldata
->res
;
7649 task_setup_data
.callback_data
= calldata
;
7651 task
= rpc_run_task(&task_setup_data
);
7653 return PTR_ERR(task
);
7656 status
= rpc_wait_for_completion_task(task
);
7658 status
= calldata
->rpc_status
;
7659 } else /* session trunking test */
7660 status
= calldata
->rpc_status
;
7664 if (clp
->cl_implid
!= NULL
)
7665 dprintk("NFS reply exchange_id: Server Implementation ID: "
7666 "domain: %s, name: %s, date: %llu,%u\n",
7667 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
7668 clp
->cl_implid
->date
.seconds
,
7669 clp
->cl_implid
->date
.nseconds
);
7670 dprintk("NFS reply exchange_id: %d\n", status
);
7674 kfree(calldata
->res
.impl_id
);
7676 kfree(calldata
->res
.server_scope
);
7678 kfree(calldata
->res
.server_owner
);
7681 nfs_put_client(clp
);
7686 * nfs4_proc_exchange_id()
7688 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7690 * Since the clientid has expired, all compounds using sessions
7691 * associated with the stale clientid will be returning
7692 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7693 * be in some phase of session reset.
7695 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7697 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7699 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7702 /* try SP4_MACH_CRED if krb5i/p */
7703 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7704 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7705 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
, NULL
);
7711 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
, NULL
);
7715 * nfs4_test_session_trunk
7717 * This is an add_xprt_test() test function called from
7718 * rpc_clnt_setup_test_and_add_xprt.
7720 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7721 * and is dereferrenced in nfs4_exchange_id_release
7723 * Upon success, add the new transport to the rpc_clnt
7725 * @clnt: struct rpc_clnt to get new transport
7726 * @xprt: the rpc_xprt to test
7727 * @data: call data for _nfs4_proc_exchange_id.
7729 int nfs4_test_session_trunk(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
,
7732 struct nfs4_add_xprt_data
*adata
= (struct nfs4_add_xprt_data
*)data
;
7735 dprintk("--> %s try %s\n", __func__
,
7736 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
7738 sp4_how
= (adata
->clp
->cl_sp4_flags
== 0 ? SP4_NONE
: SP4_MACH_CRED
);
7740 /* Test connection for session trunking. Async exchange_id call */
7741 return _nfs4_proc_exchange_id(adata
->clp
, adata
->cred
, sp4_how
, xprt
);
7743 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk
);
7745 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7746 struct rpc_cred
*cred
)
7748 struct rpc_message msg
= {
7749 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7755 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7756 trace_nfs4_destroy_clientid(clp
, status
);
7758 dprintk("NFS: Got error %d from the server %s on "
7759 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7763 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7764 struct rpc_cred
*cred
)
7769 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7770 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7772 case -NFS4ERR_DELAY
:
7773 case -NFS4ERR_CLIENTID_BUSY
:
7783 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7785 struct rpc_cred
*cred
;
7788 if (clp
->cl_mvops
->minor_version
< 1)
7790 if (clp
->cl_exchange_flags
== 0)
7792 if (clp
->cl_preserve_clid
)
7794 cred
= nfs4_get_clid_cred(clp
);
7795 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7800 case -NFS4ERR_STALE_CLIENTID
:
7801 clp
->cl_exchange_flags
= 0;
7807 struct nfs4_get_lease_time_data
{
7808 struct nfs4_get_lease_time_args
*args
;
7809 struct nfs4_get_lease_time_res
*res
;
7810 struct nfs_client
*clp
;
7813 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7816 struct nfs4_get_lease_time_data
*data
=
7817 (struct nfs4_get_lease_time_data
*)calldata
;
7819 dprintk("--> %s\n", __func__
);
7820 /* just setup sequence, do not trigger session recovery
7821 since we're invoked within one */
7822 nfs41_setup_sequence(data
->clp
->cl_session
,
7823 &data
->args
->la_seq_args
,
7824 &data
->res
->lr_seq_res
,
7826 dprintk("<-- %s\n", __func__
);
7830 * Called from nfs4_state_manager thread for session setup, so don't recover
7831 * from sequence operation or clientid errors.
7833 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7835 struct nfs4_get_lease_time_data
*data
=
7836 (struct nfs4_get_lease_time_data
*)calldata
;
7838 dprintk("--> %s\n", __func__
);
7839 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7841 switch (task
->tk_status
) {
7842 case -NFS4ERR_DELAY
:
7843 case -NFS4ERR_GRACE
:
7844 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7845 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7846 task
->tk_status
= 0;
7848 case -NFS4ERR_RETRY_UNCACHED_REP
:
7849 rpc_restart_call_prepare(task
);
7852 dprintk("<-- %s\n", __func__
);
7855 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7856 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7857 .rpc_call_done
= nfs4_get_lease_time_done
,
7860 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7862 struct rpc_task
*task
;
7863 struct nfs4_get_lease_time_args args
;
7864 struct nfs4_get_lease_time_res res
= {
7865 .lr_fsinfo
= fsinfo
,
7867 struct nfs4_get_lease_time_data data
= {
7872 struct rpc_message msg
= {
7873 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7877 struct rpc_task_setup task_setup
= {
7878 .rpc_client
= clp
->cl_rpcclient
,
7879 .rpc_message
= &msg
,
7880 .callback_ops
= &nfs4_get_lease_time_ops
,
7881 .callback_data
= &data
,
7882 .flags
= RPC_TASK_TIMEOUT
,
7886 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7887 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7888 dprintk("--> %s\n", __func__
);
7889 task
= rpc_run_task(&task_setup
);
7892 status
= PTR_ERR(task
);
7894 status
= task
->tk_status
;
7897 dprintk("<-- %s return %d\n", __func__
, status
);
7903 * Initialize the values to be used by the client in CREATE_SESSION
7904 * If nfs4_init_session set the fore channel request and response sizes,
7907 * Set the back channel max_resp_sz_cached to zero to force the client to
7908 * always set csa_cachethis to FALSE because the current implementation
7909 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7911 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7912 struct rpc_clnt
*clnt
)
7914 unsigned int max_rqst_sz
, max_resp_sz
;
7915 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7917 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7918 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7920 /* Fore channel attributes */
7921 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7922 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7923 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7924 args
->fc_attrs
.max_reqs
= max_session_slots
;
7926 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7927 "max_ops=%u max_reqs=%u\n",
7929 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7930 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7932 /* Back channel attributes */
7933 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7934 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7935 args
->bc_attrs
.max_resp_sz_cached
= 0;
7936 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7937 args
->bc_attrs
.max_reqs
= min_t(unsigned short, max_session_cb_slots
, 1);
7939 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7940 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7942 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7943 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7944 args
->bc_attrs
.max_reqs
);
7947 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7948 struct nfs41_create_session_res
*res
)
7950 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7951 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7953 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7956 * Our requested max_ops is the minimum we need; we're not
7957 * prepared to break up compounds into smaller pieces than that.
7958 * So, no point even trying to continue if the server won't
7961 if (rcvd
->max_ops
< sent
->max_ops
)
7963 if (rcvd
->max_reqs
== 0)
7965 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7966 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7970 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7971 struct nfs41_create_session_res
*res
)
7973 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7974 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7976 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7978 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7980 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7982 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7984 if (rcvd
->max_ops
> sent
->max_ops
)
7986 if (rcvd
->max_reqs
> sent
->max_reqs
)
7992 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7993 struct nfs41_create_session_res
*res
)
7997 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
8000 return nfs4_verify_back_channel_attrs(args
, res
);
8003 static void nfs4_update_session(struct nfs4_session
*session
,
8004 struct nfs41_create_session_res
*res
)
8006 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
8007 /* Mark client id and session as being confirmed */
8008 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
8009 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
8010 session
->flags
= res
->flags
;
8011 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
8012 if (res
->flags
& SESSION4_BACK_CHAN
)
8013 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
8014 sizeof(session
->bc_attrs
));
8017 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
8018 struct rpc_cred
*cred
)
8020 struct nfs4_session
*session
= clp
->cl_session
;
8021 struct nfs41_create_session_args args
= {
8023 .clientid
= clp
->cl_clientid
,
8024 .seqid
= clp
->cl_seqid
,
8025 .cb_program
= NFS4_CALLBACK
,
8027 struct nfs41_create_session_res res
;
8029 struct rpc_message msg
= {
8030 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
8037 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
8038 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
8040 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8041 trace_nfs4_create_session(clp
, status
);
8044 case -NFS4ERR_STALE_CLIENTID
:
8045 case -NFS4ERR_DELAY
:
8054 /* Verify the session's negotiated channel_attrs values */
8055 status
= nfs4_verify_channel_attrs(&args
, &res
);
8056 /* Increment the clientid slot sequence id */
8059 nfs4_update_session(session
, &res
);
8066 * Issues a CREATE_SESSION operation to the server.
8067 * It is the responsibility of the caller to verify the session is
8068 * expired before calling this routine.
8070 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8074 struct nfs4_session
*session
= clp
->cl_session
;
8076 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
8078 status
= _nfs4_proc_create_session(clp
, cred
);
8082 /* Init or reset the session slot tables */
8083 status
= nfs4_setup_session_slot_tables(session
);
8084 dprintk("slot table setup returned %d\n", status
);
8088 ptr
= (unsigned *)&session
->sess_id
.data
[0];
8089 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
8090 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
8092 dprintk("<-- %s\n", __func__
);
8097 * Issue the over-the-wire RPC DESTROY_SESSION.
8098 * The caller must serialize access to this routine.
8100 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
8101 struct rpc_cred
*cred
)
8103 struct rpc_message msg
= {
8104 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
8105 .rpc_argp
= session
,
8110 dprintk("--> nfs4_proc_destroy_session\n");
8112 /* session is still being setup */
8113 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
8116 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8117 trace_nfs4_destroy_session(session
->clp
, status
);
8120 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8121 "Session has been destroyed regardless...\n", status
);
8123 dprintk("<-- nfs4_proc_destroy_session\n");
8128 * Renew the cl_session lease.
8130 struct nfs4_sequence_data
{
8131 struct nfs_client
*clp
;
8132 struct nfs4_sequence_args args
;
8133 struct nfs4_sequence_res res
;
8136 static void nfs41_sequence_release(void *data
)
8138 struct nfs4_sequence_data
*calldata
= data
;
8139 struct nfs_client
*clp
= calldata
->clp
;
8141 if (atomic_read(&clp
->cl_count
) > 1)
8142 nfs4_schedule_state_renewal(clp
);
8143 nfs_put_client(clp
);
8147 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8149 switch(task
->tk_status
) {
8150 case -NFS4ERR_DELAY
:
8151 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8154 nfs4_schedule_lease_recovery(clp
);
8159 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
8161 struct nfs4_sequence_data
*calldata
= data
;
8162 struct nfs_client
*clp
= calldata
->clp
;
8164 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
8167 trace_nfs4_sequence(clp
, task
->tk_status
);
8168 if (task
->tk_status
< 0) {
8169 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
8170 if (atomic_read(&clp
->cl_count
) == 1)
8173 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
8174 rpc_restart_call_prepare(task
);
8178 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
8180 dprintk("<-- %s\n", __func__
);
8183 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
8185 struct nfs4_sequence_data
*calldata
= data
;
8186 struct nfs_client
*clp
= calldata
->clp
;
8187 struct nfs4_sequence_args
*args
;
8188 struct nfs4_sequence_res
*res
;
8190 args
= task
->tk_msg
.rpc_argp
;
8191 res
= task
->tk_msg
.rpc_resp
;
8193 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
8196 static const struct rpc_call_ops nfs41_sequence_ops
= {
8197 .rpc_call_done
= nfs41_sequence_call_done
,
8198 .rpc_call_prepare
= nfs41_sequence_prepare
,
8199 .rpc_release
= nfs41_sequence_release
,
8202 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
8203 struct rpc_cred
*cred
,
8206 struct nfs4_sequence_data
*calldata
;
8207 struct rpc_message msg
= {
8208 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
8211 struct rpc_task_setup task_setup_data
= {
8212 .rpc_client
= clp
->cl_rpcclient
,
8213 .rpc_message
= &msg
,
8214 .callback_ops
= &nfs41_sequence_ops
,
8215 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
8218 if (!atomic_inc_not_zero(&clp
->cl_count
))
8219 return ERR_PTR(-EIO
);
8220 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8221 if (calldata
== NULL
) {
8222 nfs_put_client(clp
);
8223 return ERR_PTR(-ENOMEM
);
8225 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
8227 nfs4_set_sequence_privileged(&calldata
->args
);
8228 msg
.rpc_argp
= &calldata
->args
;
8229 msg
.rpc_resp
= &calldata
->res
;
8230 calldata
->clp
= clp
;
8231 task_setup_data
.callback_data
= calldata
;
8233 return rpc_run_task(&task_setup_data
);
8236 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
8238 struct rpc_task
*task
;
8241 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
8243 task
= _nfs41_proc_sequence(clp
, cred
, false);
8245 ret
= PTR_ERR(task
);
8247 rpc_put_task_async(task
);
8248 dprintk("<-- %s status=%d\n", __func__
, ret
);
8252 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8254 struct rpc_task
*task
;
8257 task
= _nfs41_proc_sequence(clp
, cred
, true);
8259 ret
= PTR_ERR(task
);
8262 ret
= rpc_wait_for_completion_task(task
);
8264 ret
= task
->tk_status
;
8267 dprintk("<-- %s status=%d\n", __func__
, ret
);
8271 struct nfs4_reclaim_complete_data
{
8272 struct nfs_client
*clp
;
8273 struct nfs41_reclaim_complete_args arg
;
8274 struct nfs41_reclaim_complete_res res
;
8277 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
8279 struct nfs4_reclaim_complete_data
*calldata
= data
;
8281 nfs41_setup_sequence(calldata
->clp
->cl_session
,
8282 &calldata
->arg
.seq_args
,
8283 &calldata
->res
.seq_res
,
8287 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8289 switch(task
->tk_status
) {
8291 case -NFS4ERR_COMPLETE_ALREADY
:
8292 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
8294 case -NFS4ERR_DELAY
:
8295 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8297 case -NFS4ERR_RETRY_UNCACHED_REP
:
8300 nfs4_schedule_lease_recovery(clp
);
8305 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
8307 struct nfs4_reclaim_complete_data
*calldata
= data
;
8308 struct nfs_client
*clp
= calldata
->clp
;
8309 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
8311 dprintk("--> %s\n", __func__
);
8312 if (!nfs41_sequence_done(task
, res
))
8315 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
8316 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
8317 rpc_restart_call_prepare(task
);
8320 dprintk("<-- %s\n", __func__
);
8323 static void nfs4_free_reclaim_complete_data(void *data
)
8325 struct nfs4_reclaim_complete_data
*calldata
= data
;
8330 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
8331 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
8332 .rpc_call_done
= nfs4_reclaim_complete_done
,
8333 .rpc_release
= nfs4_free_reclaim_complete_data
,
8337 * Issue a global reclaim complete.
8339 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
8340 struct rpc_cred
*cred
)
8342 struct nfs4_reclaim_complete_data
*calldata
;
8343 struct rpc_task
*task
;
8344 struct rpc_message msg
= {
8345 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
8348 struct rpc_task_setup task_setup_data
= {
8349 .rpc_client
= clp
->cl_rpcclient
,
8350 .rpc_message
= &msg
,
8351 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
8352 .flags
= RPC_TASK_ASYNC
,
8354 int status
= -ENOMEM
;
8356 dprintk("--> %s\n", __func__
);
8357 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8358 if (calldata
== NULL
)
8360 calldata
->clp
= clp
;
8361 calldata
->arg
.one_fs
= 0;
8363 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
8364 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
8365 msg
.rpc_argp
= &calldata
->arg
;
8366 msg
.rpc_resp
= &calldata
->res
;
8367 task_setup_data
.callback_data
= calldata
;
8368 task
= rpc_run_task(&task_setup_data
);
8370 status
= PTR_ERR(task
);
8373 status
= nfs4_wait_for_completion_rpc_task(task
);
8375 status
= task
->tk_status
;
8379 dprintk("<-- %s status=%d\n", __func__
, status
);
8384 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
8386 struct nfs4_layoutget
*lgp
= calldata
;
8387 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
8388 struct nfs4_session
*session
= nfs4_get_session(server
);
8390 dprintk("--> %s\n", __func__
);
8391 nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
8392 &lgp
->res
.seq_res
, task
);
8393 dprintk("<-- %s\n", __func__
);
8396 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
8398 struct nfs4_layoutget
*lgp
= calldata
;
8400 dprintk("--> %s\n", __func__
);
8401 nfs41_sequence_process(task
, &lgp
->res
.seq_res
);
8402 dprintk("<-- %s\n", __func__
);
8406 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
8407 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
8409 struct inode
*inode
= lgp
->args
.inode
;
8410 struct nfs_server
*server
= NFS_SERVER(inode
);
8411 struct pnfs_layout_hdr
*lo
;
8412 int nfs4err
= task
->tk_status
;
8413 int err
, status
= 0;
8416 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
8423 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8424 * on the file. set tk_status to -ENODATA to tell upper layer to
8427 case -NFS4ERR_LAYOUTUNAVAILABLE
:
8431 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8432 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8434 case -NFS4ERR_BADLAYOUT
:
8435 status
= -EOVERFLOW
;
8438 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8439 * (or clients) writing to the same RAID stripe except when
8440 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8442 * Treat it like we would RECALLCONFLICT -- we retry for a little
8443 * while, and then eventually give up.
8445 case -NFS4ERR_LAYOUTTRYLATER
:
8446 if (lgp
->args
.minlength
== 0) {
8447 status
= -EOVERFLOW
;
8452 case -NFS4ERR_RECALLCONFLICT
:
8453 status
= -ERECALLCONFLICT
;
8455 case -NFS4ERR_DELEG_REVOKED
:
8456 case -NFS4ERR_ADMIN_REVOKED
:
8457 case -NFS4ERR_EXPIRED
:
8458 case -NFS4ERR_BAD_STATEID
:
8459 exception
->timeout
= 0;
8460 spin_lock(&inode
->i_lock
);
8461 lo
= NFS_I(inode
)->layout
;
8462 /* If the open stateid was bad, then recover it. */
8463 if (!lo
|| test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) ||
8464 nfs4_stateid_match_other(&lgp
->args
.stateid
,
8465 &lgp
->args
.ctx
->state
->stateid
)) {
8466 spin_unlock(&inode
->i_lock
);
8467 exception
->state
= lgp
->args
.ctx
->state
;
8468 exception
->stateid
= &lgp
->args
.stateid
;
8473 * Mark the bad layout state as invalid, then retry
8475 pnfs_mark_layout_stateid_invalid(lo
, &head
);
8476 spin_unlock(&inode
->i_lock
);
8477 pnfs_free_lseg_list(&head
);
8482 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8483 err
= nfs4_handle_exception(server
, nfs4err
, exception
);
8485 if (exception
->retry
)
8491 dprintk("<-- %s\n", __func__
);
8495 static size_t max_response_pages(struct nfs_server
*server
)
8497 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
8498 return nfs_page_array_len(0, max_resp_sz
);
8501 static void nfs4_free_pages(struct page
**pages
, size_t size
)
8508 for (i
= 0; i
< size
; i
++) {
8511 __free_page(pages
[i
]);
8516 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
8518 struct page
**pages
;
8521 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
8523 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
8527 for (i
= 0; i
< size
; i
++) {
8528 pages
[i
] = alloc_page(gfp_flags
);
8530 dprintk("%s: failed to allocate page\n", __func__
);
8531 nfs4_free_pages(pages
, size
);
8539 static void nfs4_layoutget_release(void *calldata
)
8541 struct nfs4_layoutget
*lgp
= calldata
;
8542 struct inode
*inode
= lgp
->args
.inode
;
8543 struct nfs_server
*server
= NFS_SERVER(inode
);
8544 size_t max_pages
= max_response_pages(server
);
8546 dprintk("--> %s\n", __func__
);
8547 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8548 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8549 put_nfs_open_context(lgp
->args
.ctx
);
8551 dprintk("<-- %s\n", __func__
);
8554 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8555 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8556 .rpc_call_done
= nfs4_layoutget_done
,
8557 .rpc_release
= nfs4_layoutget_release
,
8560 struct pnfs_layout_segment
*
8561 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8563 struct inode
*inode
= lgp
->args
.inode
;
8564 struct nfs_server
*server
= NFS_SERVER(inode
);
8565 size_t max_pages
= max_response_pages(server
);
8566 struct rpc_task
*task
;
8567 struct rpc_message msg
= {
8568 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8569 .rpc_argp
= &lgp
->args
,
8570 .rpc_resp
= &lgp
->res
,
8571 .rpc_cred
= lgp
->cred
,
8573 struct rpc_task_setup task_setup_data
= {
8574 .rpc_client
= server
->client
,
8575 .rpc_message
= &msg
,
8576 .callback_ops
= &nfs4_layoutget_call_ops
,
8577 .callback_data
= lgp
,
8578 .flags
= RPC_TASK_ASYNC
,
8580 struct pnfs_layout_segment
*lseg
= NULL
;
8581 struct nfs4_exception exception
= {
8583 .timeout
= *timeout
,
8587 dprintk("--> %s\n", __func__
);
8589 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8590 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8592 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8593 if (!lgp
->args
.layout
.pages
) {
8594 nfs4_layoutget_release(lgp
);
8595 return ERR_PTR(-ENOMEM
);
8597 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8599 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8600 lgp
->res
.seq_res
.sr_slot
= NULL
;
8601 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8603 task
= rpc_run_task(&task_setup_data
);
8605 return ERR_CAST(task
);
8606 status
= nfs4_wait_for_completion_rpc_task(task
);
8608 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8609 *timeout
= exception
.timeout
;
8612 trace_nfs4_layoutget(lgp
->args
.ctx
,
8618 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8619 if (status
== 0 && lgp
->res
.layoutp
->len
)
8620 lseg
= pnfs_layout_process(lgp
);
8621 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8623 dprintk("<-- %s status=%d\n", __func__
, status
);
8625 return ERR_PTR(status
);
8630 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8632 struct nfs4_layoutreturn
*lrp
= calldata
;
8634 dprintk("--> %s\n", __func__
);
8635 nfs41_setup_sequence(lrp
->clp
->cl_session
,
8636 &lrp
->args
.seq_args
,
8641 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8643 struct nfs4_layoutreturn
*lrp
= calldata
;
8644 struct nfs_server
*server
;
8646 dprintk("--> %s\n", __func__
);
8648 if (!nfs41_sequence_process(task
, &lrp
->res
.seq_res
))
8651 server
= NFS_SERVER(lrp
->args
.inode
);
8652 switch (task
->tk_status
) {
8654 task
->tk_status
= 0;
8657 case -NFS4ERR_DELAY
:
8658 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8660 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8661 rpc_restart_call_prepare(task
);
8664 dprintk("<-- %s\n", __func__
);
8667 static void nfs4_layoutreturn_release(void *calldata
)
8669 struct nfs4_layoutreturn
*lrp
= calldata
;
8670 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8672 dprintk("--> %s\n", __func__
);
8673 pnfs_layoutreturn_free_lsegs(lo
, &lrp
->args
.stateid
, &lrp
->args
.range
,
8674 lrp
->res
.lrs_present
? &lrp
->res
.stateid
: NULL
);
8675 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8676 if (lrp
->ld_private
.ops
&& lrp
->ld_private
.ops
->free
)
8677 lrp
->ld_private
.ops
->free(&lrp
->ld_private
);
8678 pnfs_put_layout_hdr(lrp
->args
.layout
);
8679 nfs_iput_and_deactive(lrp
->inode
);
8681 dprintk("<-- %s\n", __func__
);
8684 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8685 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8686 .rpc_call_done
= nfs4_layoutreturn_done
,
8687 .rpc_release
= nfs4_layoutreturn_release
,
8690 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8692 struct rpc_task
*task
;
8693 struct rpc_message msg
= {
8694 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8695 .rpc_argp
= &lrp
->args
,
8696 .rpc_resp
= &lrp
->res
,
8697 .rpc_cred
= lrp
->cred
,
8699 struct rpc_task_setup task_setup_data
= {
8700 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8701 .rpc_message
= &msg
,
8702 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8703 .callback_data
= lrp
,
8707 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8708 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8709 &task_setup_data
.rpc_client
, &msg
);
8711 dprintk("--> %s\n", __func__
);
8713 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8715 nfs4_layoutreturn_release(lrp
);
8718 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8720 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8721 task
= rpc_run_task(&task_setup_data
);
8723 return PTR_ERR(task
);
8725 status
= task
->tk_status
;
8726 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8727 dprintk("<-- %s status=%d\n", __func__
, status
);
8733 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8734 struct pnfs_device
*pdev
,
8735 struct rpc_cred
*cred
)
8737 struct nfs4_getdeviceinfo_args args
= {
8739 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8740 NOTIFY_DEVICEID4_DELETE
,
8742 struct nfs4_getdeviceinfo_res res
= {
8745 struct rpc_message msg
= {
8746 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8753 dprintk("--> %s\n", __func__
);
8754 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8755 if (res
.notification
& ~args
.notify_types
)
8756 dprintk("%s: unsupported notification\n", __func__
);
8757 if (res
.notification
!= args
.notify_types
)
8760 dprintk("<-- %s status=%d\n", __func__
, status
);
8765 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8766 struct pnfs_device
*pdev
,
8767 struct rpc_cred
*cred
)
8769 struct nfs4_exception exception
= { };
8773 err
= nfs4_handle_exception(server
,
8774 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8776 } while (exception
.retry
);
8779 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8781 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8783 struct nfs4_layoutcommit_data
*data
= calldata
;
8784 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8785 struct nfs4_session
*session
= nfs4_get_session(server
);
8787 nfs41_setup_sequence(session
,
8788 &data
->args
.seq_args
,
8794 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8796 struct nfs4_layoutcommit_data
*data
= calldata
;
8797 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8799 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8802 switch (task
->tk_status
) { /* Just ignore these failures */
8803 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8804 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8805 case -NFS4ERR_BADLAYOUT
: /* no layout */
8806 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8807 task
->tk_status
= 0;
8811 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8812 rpc_restart_call_prepare(task
);
8818 static void nfs4_layoutcommit_release(void *calldata
)
8820 struct nfs4_layoutcommit_data
*data
= calldata
;
8822 pnfs_cleanup_layoutcommit(data
);
8823 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8825 put_rpccred(data
->cred
);
8826 nfs_iput_and_deactive(data
->inode
);
8830 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8831 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8832 .rpc_call_done
= nfs4_layoutcommit_done
,
8833 .rpc_release
= nfs4_layoutcommit_release
,
8837 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8839 struct rpc_message msg
= {
8840 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8841 .rpc_argp
= &data
->args
,
8842 .rpc_resp
= &data
->res
,
8843 .rpc_cred
= data
->cred
,
8845 struct rpc_task_setup task_setup_data
= {
8846 .task
= &data
->task
,
8847 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8848 .rpc_message
= &msg
,
8849 .callback_ops
= &nfs4_layoutcommit_ops
,
8850 .callback_data
= data
,
8852 struct rpc_task
*task
;
8855 dprintk("NFS: initiating layoutcommit call. sync %d "
8856 "lbw: %llu inode %lu\n", sync
,
8857 data
->args
.lastbytewritten
,
8858 data
->args
.inode
->i_ino
);
8861 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8862 if (data
->inode
== NULL
) {
8863 nfs4_layoutcommit_release(data
);
8866 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8868 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8869 task
= rpc_run_task(&task_setup_data
);
8871 return PTR_ERR(task
);
8873 status
= task
->tk_status
;
8874 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8875 dprintk("%s: status %d\n", __func__
, status
);
8881 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8882 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8885 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8886 struct nfs_fsinfo
*info
,
8887 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8889 struct nfs41_secinfo_no_name_args args
= {
8890 .style
= SECINFO_STYLE_CURRENT_FH
,
8892 struct nfs4_secinfo_res res
= {
8895 struct rpc_message msg
= {
8896 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8900 struct rpc_clnt
*clnt
= server
->client
;
8901 struct rpc_cred
*cred
= NULL
;
8904 if (use_integrity
) {
8905 clnt
= server
->nfs_client
->cl_rpcclient
;
8906 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8907 msg
.rpc_cred
= cred
;
8910 dprintk("--> %s\n", __func__
);
8911 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8913 dprintk("<-- %s status=%d\n", __func__
, status
);
8922 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8923 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8925 struct nfs4_exception exception
= { };
8928 /* first try using integrity protection */
8929 err
= -NFS4ERR_WRONGSEC
;
8931 /* try to use integrity protection with machine cred */
8932 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8933 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8937 * if unable to use integrity protection, or SECINFO with
8938 * integrity protection returns NFS4ERR_WRONGSEC (which is
8939 * disallowed by spec, but exists in deployed servers) use
8940 * the current filesystem's rpc_client and the user cred.
8942 if (err
== -NFS4ERR_WRONGSEC
)
8943 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8948 case -NFS4ERR_WRONGSEC
:
8952 err
= nfs4_handle_exception(server
, err
, &exception
);
8954 } while (exception
.retry
);
8960 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8961 struct nfs_fsinfo
*info
)
8965 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8966 struct nfs4_secinfo_flavors
*flavors
;
8967 struct nfs4_secinfo4
*secinfo
;
8970 page
= alloc_page(GFP_KERNEL
);
8976 flavors
= page_address(page
);
8977 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8980 * Fall back on "guess and check" method if
8981 * the server doesn't support SECINFO_NO_NAME
8983 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8984 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8990 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8991 secinfo
= &flavors
->flavors
[i
];
8993 switch (secinfo
->flavor
) {
8997 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
8998 &secinfo
->flavor_info
);
9001 flavor
= RPC_AUTH_MAXFLAVOR
;
9005 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
9006 flavor
= RPC_AUTH_MAXFLAVOR
;
9008 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
9009 err
= nfs4_lookup_root_sec(server
, fhandle
,
9016 if (flavor
== RPC_AUTH_MAXFLAVOR
)
9027 static int _nfs41_test_stateid(struct nfs_server
*server
,
9028 nfs4_stateid
*stateid
,
9029 struct rpc_cred
*cred
)
9032 struct nfs41_test_stateid_args args
= {
9035 struct nfs41_test_stateid_res res
;
9036 struct rpc_message msg
= {
9037 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
9042 struct rpc_clnt
*rpc_client
= server
->client
;
9044 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9047 dprintk("NFS call test_stateid %p\n", stateid
);
9048 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
9049 nfs4_set_sequence_privileged(&args
.seq_args
);
9050 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
9051 &args
.seq_args
, &res
.seq_res
);
9052 if (status
!= NFS_OK
) {
9053 dprintk("NFS reply test_stateid: failed, %d\n", status
);
9056 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
9060 static void nfs4_handle_delay_or_session_error(struct nfs_server
*server
,
9061 int err
, struct nfs4_exception
*exception
)
9063 exception
->retry
= 0;
9065 case -NFS4ERR_DELAY
:
9066 case -NFS4ERR_RETRY_UNCACHED_REP
:
9067 nfs4_handle_exception(server
, err
, exception
);
9069 case -NFS4ERR_BADSESSION
:
9070 case -NFS4ERR_BADSLOT
:
9071 case -NFS4ERR_BAD_HIGH_SLOT
:
9072 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
9073 case -NFS4ERR_DEADSESSION
:
9074 nfs4_do_handle_exception(server
, err
, exception
);
9079 * nfs41_test_stateid - perform a TEST_STATEID operation
9081 * @server: server / transport on which to perform the operation
9082 * @stateid: state ID to test
9085 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9086 * Otherwise a negative NFS4ERR value is returned if the operation
9087 * failed or the state ID is not currently valid.
9089 static int nfs41_test_stateid(struct nfs_server
*server
,
9090 nfs4_stateid
*stateid
,
9091 struct rpc_cred
*cred
)
9093 struct nfs4_exception exception
= { };
9096 err
= _nfs41_test_stateid(server
, stateid
, cred
);
9097 nfs4_handle_delay_or_session_error(server
, err
, &exception
);
9098 } while (exception
.retry
);
9102 struct nfs_free_stateid_data
{
9103 struct nfs_server
*server
;
9104 struct nfs41_free_stateid_args args
;
9105 struct nfs41_free_stateid_res res
;
9108 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
9110 struct nfs_free_stateid_data
*data
= calldata
;
9111 nfs41_setup_sequence(nfs4_get_session(data
->server
),
9112 &data
->args
.seq_args
,
9117 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
9119 struct nfs_free_stateid_data
*data
= calldata
;
9121 nfs41_sequence_done(task
, &data
->res
.seq_res
);
9123 switch (task
->tk_status
) {
9124 case -NFS4ERR_DELAY
:
9125 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
9126 rpc_restart_call_prepare(task
);
9130 static void nfs41_free_stateid_release(void *calldata
)
9135 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
9136 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
9137 .rpc_call_done
= nfs41_free_stateid_done
,
9138 .rpc_release
= nfs41_free_stateid_release
,
9141 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
9142 const nfs4_stateid
*stateid
,
9143 struct rpc_cred
*cred
,
9146 struct rpc_message msg
= {
9147 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
9150 struct rpc_task_setup task_setup
= {
9151 .rpc_client
= server
->client
,
9152 .rpc_message
= &msg
,
9153 .callback_ops
= &nfs41_free_stateid_ops
,
9154 .flags
= RPC_TASK_ASYNC
,
9156 struct nfs_free_stateid_data
*data
;
9158 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9159 &task_setup
.rpc_client
, &msg
);
9161 dprintk("NFS call free_stateid %p\n", stateid
);
9162 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
9164 return ERR_PTR(-ENOMEM
);
9165 data
->server
= server
;
9166 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
9168 task_setup
.callback_data
= data
;
9170 msg
.rpc_argp
= &data
->args
;
9171 msg
.rpc_resp
= &data
->res
;
9172 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
9174 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
9176 return rpc_run_task(&task_setup
);
9180 * nfs41_free_stateid - perform a FREE_STATEID operation
9182 * @server: server / transport on which to perform the operation
9183 * @stateid: state ID to release
9185 * @is_recovery: set to true if this call needs to be privileged
9187 * Note: this function is always asynchronous.
9189 static int nfs41_free_stateid(struct nfs_server
*server
,
9190 const nfs4_stateid
*stateid
,
9191 struct rpc_cred
*cred
,
9194 struct rpc_task
*task
;
9196 task
= _nfs41_free_stateid(server
, stateid
, cred
, is_recovery
);
9198 return PTR_ERR(task
);
9204 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
9206 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
9208 nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
9209 nfs4_free_lock_state(server
, lsp
);
9212 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
9213 const nfs4_stateid
*s2
)
9215 if (s1
->type
!= s2
->type
)
9218 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
9221 if (s1
->seqid
== s2
->seqid
)
9223 if (s1
->seqid
== 0 || s2
->seqid
== 0)
9229 #endif /* CONFIG_NFS_V4_1 */
9231 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
9232 const nfs4_stateid
*s2
)
9234 return nfs4_stateid_match(s1
, s2
);
9238 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
9239 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9240 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9241 .recover_open
= nfs4_open_reclaim
,
9242 .recover_lock
= nfs4_lock_reclaim
,
9243 .establish_clid
= nfs4_init_clientid
,
9244 .detect_trunking
= nfs40_discover_server_trunking
,
9247 #if defined(CONFIG_NFS_V4_1)
9248 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
9249 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9250 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9251 .recover_open
= nfs4_open_reclaim
,
9252 .recover_lock
= nfs4_lock_reclaim
,
9253 .establish_clid
= nfs41_init_clientid
,
9254 .reclaim_complete
= nfs41_proc_reclaim_complete
,
9255 .detect_trunking
= nfs41_discover_server_trunking
,
9257 #endif /* CONFIG_NFS_V4_1 */
9259 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
9260 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9261 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9262 .recover_open
= nfs40_open_expired
,
9263 .recover_lock
= nfs4_lock_expired
,
9264 .establish_clid
= nfs4_init_clientid
,
9267 #if defined(CONFIG_NFS_V4_1)
9268 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
9269 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9270 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9271 .recover_open
= nfs41_open_expired
,
9272 .recover_lock
= nfs41_lock_expired
,
9273 .establish_clid
= nfs41_init_clientid
,
9275 #endif /* CONFIG_NFS_V4_1 */
9277 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
9278 .sched_state_renewal
= nfs4_proc_async_renew
,
9279 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
9280 .renew_lease
= nfs4_proc_renew
,
9283 #if defined(CONFIG_NFS_V4_1)
9284 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
9285 .sched_state_renewal
= nfs41_proc_async_sequence
,
9286 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
9287 .renew_lease
= nfs4_proc_sequence
,
9291 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
9292 .get_locations
= _nfs40_proc_get_locations
,
9293 .fsid_present
= _nfs40_proc_fsid_present
,
9296 #if defined(CONFIG_NFS_V4_1)
9297 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
9298 .get_locations
= _nfs41_proc_get_locations
,
9299 .fsid_present
= _nfs41_proc_fsid_present
,
9301 #endif /* CONFIG_NFS_V4_1 */
9303 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
9305 .init_caps
= NFS_CAP_READDIRPLUS
9306 | NFS_CAP_ATOMIC_OPEN
9307 | NFS_CAP_POSIX_LOCK
,
9308 .init_client
= nfs40_init_client
,
9309 .shutdown_client
= nfs40_shutdown_client
,
9310 .match_stateid
= nfs4_match_stateid
,
9311 .find_root_sec
= nfs4_find_root_sec
,
9312 .free_lock_state
= nfs4_release_lockowner
,
9313 .test_and_free_expired
= nfs40_test_and_free_expired_stateid
,
9314 .alloc_seqid
= nfs_alloc_seqid
,
9315 .call_sync_ops
= &nfs40_call_sync_ops
,
9316 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
9317 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
9318 .state_renewal_ops
= &nfs40_state_renewal_ops
,
9319 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
9322 #if defined(CONFIG_NFS_V4_1)
9323 static struct nfs_seqid
*
9324 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
9329 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
9331 .init_caps
= NFS_CAP_READDIRPLUS
9332 | NFS_CAP_ATOMIC_OPEN
9333 | NFS_CAP_POSIX_LOCK
9334 | NFS_CAP_STATEID_NFSV41
9335 | NFS_CAP_ATOMIC_OPEN_V1
,
9336 .init_client
= nfs41_init_client
,
9337 .shutdown_client
= nfs41_shutdown_client
,
9338 .match_stateid
= nfs41_match_stateid
,
9339 .find_root_sec
= nfs41_find_root_sec
,
9340 .free_lock_state
= nfs41_free_lock_state
,
9341 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9342 .alloc_seqid
= nfs_alloc_no_seqid
,
9343 .session_trunk
= nfs4_test_session_trunk
,
9344 .call_sync_ops
= &nfs41_call_sync_ops
,
9345 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9346 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9347 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9348 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9352 #if defined(CONFIG_NFS_V4_2)
9353 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
9355 .init_caps
= NFS_CAP_READDIRPLUS
9356 | NFS_CAP_ATOMIC_OPEN
9357 | NFS_CAP_POSIX_LOCK
9358 | NFS_CAP_STATEID_NFSV41
9359 | NFS_CAP_ATOMIC_OPEN_V1
9362 | NFS_CAP_DEALLOCATE
9364 | NFS_CAP_LAYOUTSTATS
9366 .init_client
= nfs41_init_client
,
9367 .shutdown_client
= nfs41_shutdown_client
,
9368 .match_stateid
= nfs41_match_stateid
,
9369 .find_root_sec
= nfs41_find_root_sec
,
9370 .free_lock_state
= nfs41_free_lock_state
,
9371 .call_sync_ops
= &nfs41_call_sync_ops
,
9372 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9373 .alloc_seqid
= nfs_alloc_no_seqid
,
9374 .session_trunk
= nfs4_test_session_trunk
,
9375 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9376 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9377 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9378 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9382 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
9383 [0] = &nfs_v4_0_minor_ops
,
9384 #if defined(CONFIG_NFS_V4_1)
9385 [1] = &nfs_v4_1_minor_ops
,
9387 #if defined(CONFIG_NFS_V4_2)
9388 [2] = &nfs_v4_2_minor_ops
,
9392 static ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
9394 ssize_t error
, error2
;
9396 error
= generic_listxattr(dentry
, list
, size
);
9404 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
9407 return error
+ error2
;
9410 static const struct inode_operations nfs4_dir_inode_operations
= {
9411 .create
= nfs_create
,
9412 .lookup
= nfs_lookup
,
9413 .atomic_open
= nfs_atomic_open
,
9415 .unlink
= nfs_unlink
,
9416 .symlink
= nfs_symlink
,
9420 .rename
= nfs_rename
,
9421 .permission
= nfs_permission
,
9422 .getattr
= nfs_getattr
,
9423 .setattr
= nfs_setattr
,
9424 .listxattr
= nfs4_listxattr
,
9427 static const struct inode_operations nfs4_file_inode_operations
= {
9428 .permission
= nfs_permission
,
9429 .getattr
= nfs_getattr
,
9430 .setattr
= nfs_setattr
,
9431 .listxattr
= nfs4_listxattr
,
9434 const struct nfs_rpc_ops nfs_v4_clientops
= {
9435 .version
= 4, /* protocol version */
9436 .dentry_ops
= &nfs4_dentry_operations
,
9437 .dir_inode_ops
= &nfs4_dir_inode_operations
,
9438 .file_inode_ops
= &nfs4_file_inode_operations
,
9439 .file_ops
= &nfs4_file_operations
,
9440 .getroot
= nfs4_proc_get_root
,
9441 .submount
= nfs4_submount
,
9442 .try_mount
= nfs4_try_mount
,
9443 .getattr
= nfs4_proc_getattr
,
9444 .setattr
= nfs4_proc_setattr
,
9445 .lookup
= nfs4_proc_lookup
,
9446 .access
= nfs4_proc_access
,
9447 .readlink
= nfs4_proc_readlink
,
9448 .create
= nfs4_proc_create
,
9449 .remove
= nfs4_proc_remove
,
9450 .unlink_setup
= nfs4_proc_unlink_setup
,
9451 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
9452 .unlink_done
= nfs4_proc_unlink_done
,
9453 .rename_setup
= nfs4_proc_rename_setup
,
9454 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
9455 .rename_done
= nfs4_proc_rename_done
,
9456 .link
= nfs4_proc_link
,
9457 .symlink
= nfs4_proc_symlink
,
9458 .mkdir
= nfs4_proc_mkdir
,
9459 .rmdir
= nfs4_proc_remove
,
9460 .readdir
= nfs4_proc_readdir
,
9461 .mknod
= nfs4_proc_mknod
,
9462 .statfs
= nfs4_proc_statfs
,
9463 .fsinfo
= nfs4_proc_fsinfo
,
9464 .pathconf
= nfs4_proc_pathconf
,
9465 .set_capabilities
= nfs4_server_capabilities
,
9466 .decode_dirent
= nfs4_decode_dirent
,
9467 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
9468 .read_setup
= nfs4_proc_read_setup
,
9469 .read_done
= nfs4_read_done
,
9470 .write_setup
= nfs4_proc_write_setup
,
9471 .write_done
= nfs4_write_done
,
9472 .commit_setup
= nfs4_proc_commit_setup
,
9473 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
9474 .commit_done
= nfs4_commit_done
,
9475 .lock
= nfs4_proc_lock
,
9476 .clear_acl_cache
= nfs4_zap_acl_attr
,
9477 .close_context
= nfs4_close_context
,
9478 .open_context
= nfs4_atomic_open
,
9479 .have_delegation
= nfs4_have_delegation
,
9480 .return_delegation
= nfs4_inode_return_delegation
,
9481 .alloc_client
= nfs4_alloc_client
,
9482 .init_client
= nfs4_init_client
,
9483 .free_client
= nfs4_free_client
,
9484 .create_server
= nfs4_create_server
,
9485 .clone_server
= nfs_clone_server
,
9488 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
9489 .name
= XATTR_NAME_NFSV4_ACL
,
9490 .list
= nfs4_xattr_list_nfs4_acl
,
9491 .get
= nfs4_xattr_get_nfs4_acl
,
9492 .set
= nfs4_xattr_set_nfs4_acl
,
9495 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
9496 &nfs4_xattr_nfs4_acl_handler
,
9497 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9498 &nfs4_xattr_nfs4_label_handler
,