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/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
89 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
91 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
93 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
94 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
95 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
96 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
97 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
98 struct nfs4_label
*olabel
);
99 #ifdef CONFIG_NFS_V4_1
100 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
102 static int nfs41_free_stateid(struct nfs_server
*, const nfs4_stateid
*,
103 struct rpc_cred
*, bool);
106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
107 static inline struct nfs4_label
*
108 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
109 struct iattr
*sattr
, struct nfs4_label
*label
)
116 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
119 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
120 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
127 nfs4_label_release_security(struct nfs4_label
*label
)
130 security_release_secctx(label
->label
, label
->len
);
132 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
135 return server
->attr_bitmask
;
137 return server
->attr_bitmask_nl
;
140 static inline struct nfs4_label
*
141 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
142 struct iattr
*sattr
, struct nfs4_label
*l
)
145 nfs4_label_release_security(struct nfs4_label
*label
)
148 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
149 { return server
->attr_bitmask
; }
152 /* Prevent leaks of NFSv4 errors into userland */
153 static int nfs4_map_errors(int err
)
158 case -NFS4ERR_RESOURCE
:
159 case -NFS4ERR_LAYOUTTRYLATER
:
160 case -NFS4ERR_RECALLCONFLICT
:
162 case -NFS4ERR_WRONGSEC
:
163 case -NFS4ERR_WRONG_CRED
:
165 case -NFS4ERR_BADOWNER
:
166 case -NFS4ERR_BADNAME
:
168 case -NFS4ERR_SHARE_DENIED
:
170 case -NFS4ERR_MINOR_VERS_MISMATCH
:
171 return -EPROTONOSUPPORT
;
172 case -NFS4ERR_FILE_OPEN
:
175 dprintk("%s could not handle NFSv4 error %d\n",
183 * This is our standard bitmap for GETATTR requests.
185 const u32 nfs4_fattr_bitmap
[3] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID
,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
206 static const u32 nfs4_pnfs_open_bitmap
[3] = {
208 | FATTR4_WORD0_CHANGE
211 | FATTR4_WORD0_FILEID
,
213 | FATTR4_WORD1_NUMLINKS
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY
,
221 FATTR4_WORD2_MDSTHRESHOLD
222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
227 static const u32 nfs4_open_noattr_bitmap
[3] = {
229 | FATTR4_WORD0_FILEID
,
232 const u32 nfs4_statfs_bitmap
[3] = {
233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL
,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
241 const u32 nfs4_pathconf_bitmap
[3] = {
243 | FATTR4_WORD0_MAXNAME
,
247 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME
,
251 FATTR4_WORD1_TIME_DELTA
252 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
254 | FATTR4_WORD2_CLONE_BLKSIZE
257 const u32 nfs4_fs_locations_bitmap
[3] = {
259 | FATTR4_WORD0_CHANGE
262 | FATTR4_WORD0_FILEID
263 | FATTR4_WORD0_FS_LOCATIONS
,
265 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
273 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
276 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
277 struct nfs4_readdir_arg
*readdir
)
282 readdir
->cookie
= cookie
;
283 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
288 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
299 start
= p
= kmap_atomic(*readdir
->pages
);
302 *p
++ = xdr_one
; /* next */
303 *p
++ = xdr_zero
; /* cookie, first word */
304 *p
++ = xdr_one
; /* cookie, second word */
305 *p
++ = xdr_one
; /* entry len */
306 memcpy(p
, ".\0\0\0", 4); /* entry */
308 *p
++ = xdr_one
; /* bitmap length */
309 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
310 *p
++ = htonl(8); /* attribute buffer length */
311 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
)));
314 *p
++ = xdr_one
; /* next */
315 *p
++ = xdr_zero
; /* cookie, first word */
316 *p
++ = xdr_two
; /* cookie, second word */
317 *p
++ = xdr_two
; /* entry len */
318 memcpy(p
, "..\0\0", 4); /* entry */
320 *p
++ = xdr_one
; /* bitmap length */
321 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
322 *p
++ = htonl(8); /* attribute buffer length */
323 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
325 readdir
->pgbase
= (char *)p
- (char *)start
;
326 readdir
->count
-= readdir
->pgbase
;
327 kunmap_atomic(start
);
330 static void nfs4_test_and_free_stateid(struct nfs_server
*server
,
331 nfs4_stateid
*stateid
,
332 struct rpc_cred
*cred
)
334 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
336 ops
->test_and_free_expired(server
, stateid
, cred
);
339 static void __nfs4_free_revoked_stateid(struct nfs_server
*server
,
340 nfs4_stateid
*stateid
,
341 struct rpc_cred
*cred
)
343 stateid
->type
= NFS4_REVOKED_STATEID_TYPE
;
344 nfs4_test_and_free_stateid(server
, stateid
, cred
);
347 static void nfs4_free_revoked_stateid(struct nfs_server
*server
,
348 const nfs4_stateid
*stateid
,
349 struct rpc_cred
*cred
)
353 nfs4_stateid_copy(&tmp
, stateid
);
354 __nfs4_free_revoked_stateid(server
, &tmp
, cred
);
357 static long nfs4_update_delay(long *timeout
)
361 return NFS4_POLL_RETRY_MAX
;
363 *timeout
= NFS4_POLL_RETRY_MIN
;
364 if (*timeout
> NFS4_POLL_RETRY_MAX
)
365 *timeout
= NFS4_POLL_RETRY_MAX
;
371 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
377 freezable_schedule_timeout_killable_unsafe(
378 nfs4_update_delay(timeout
));
379 if (fatal_signal_pending(current
))
384 /* This is the error handling routine for processes that are allowed
387 static int nfs4_do_handle_exception(struct nfs_server
*server
,
388 int errorcode
, struct nfs4_exception
*exception
)
390 struct nfs_client
*clp
= server
->nfs_client
;
391 struct nfs4_state
*state
= exception
->state
;
392 const nfs4_stateid
*stateid
= exception
->stateid
;
393 struct inode
*inode
= exception
->inode
;
396 exception
->delay
= 0;
397 exception
->recovering
= 0;
398 exception
->retry
= 0;
400 if (stateid
== NULL
&& state
!= NULL
)
401 stateid
= &state
->stateid
;
406 case -NFS4ERR_DELEG_REVOKED
:
407 case -NFS4ERR_ADMIN_REVOKED
:
408 case -NFS4ERR_EXPIRED
:
409 case -NFS4ERR_BAD_STATEID
:
410 if (inode
!= NULL
&& stateid
!= NULL
) {
411 nfs_inode_find_state_and_recover(inode
,
413 goto wait_on_recovery
;
415 case -NFS4ERR_OPENMODE
:
419 err
= nfs_async_inode_return_delegation(inode
,
422 goto wait_on_recovery
;
423 if (stateid
!= NULL
&& stateid
->type
== NFS4_DELEGATION_STATEID_TYPE
) {
424 exception
->retry
= 1;
430 ret
= nfs4_schedule_stateid_recovery(server
, state
);
433 goto wait_on_recovery
;
434 case -NFS4ERR_STALE_STATEID
:
435 case -NFS4ERR_STALE_CLIENTID
:
436 nfs4_schedule_lease_recovery(clp
);
437 goto wait_on_recovery
;
439 ret
= nfs4_schedule_migration_recovery(server
);
442 goto wait_on_recovery
;
443 case -NFS4ERR_LEASE_MOVED
:
444 nfs4_schedule_lease_moved_recovery(clp
);
445 goto wait_on_recovery
;
446 #if defined(CONFIG_NFS_V4_1)
447 case -NFS4ERR_BADSESSION
:
448 case -NFS4ERR_BADSLOT
:
449 case -NFS4ERR_BAD_HIGH_SLOT
:
450 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
451 case -NFS4ERR_DEADSESSION
:
452 case -NFS4ERR_SEQ_FALSE_RETRY
:
453 case -NFS4ERR_SEQ_MISORDERED
:
454 dprintk("%s ERROR: %d Reset session\n", __func__
,
456 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
457 goto wait_on_recovery
;
458 #endif /* defined(CONFIG_NFS_V4_1) */
459 case -NFS4ERR_FILE_OPEN
:
460 if (exception
->timeout
> HZ
) {
461 /* We have retried a decent amount, time to
468 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
470 case -NFS4ERR_LAYOUTTRYLATER
:
471 case -NFS4ERR_RECALLCONFLICT
:
472 exception
->delay
= 1;
475 case -NFS4ERR_RETRY_UNCACHED_REP
:
476 case -NFS4ERR_OLD_STATEID
:
477 exception
->retry
= 1;
479 case -NFS4ERR_BADOWNER
:
480 /* The following works around a Linux server bug! */
481 case -NFS4ERR_BADNAME
:
482 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
483 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
484 exception
->retry
= 1;
485 printk(KERN_WARNING
"NFS: v4 server %s "
486 "does not accept raw "
488 "Reenabling the idmapper.\n",
489 server
->nfs_client
->cl_hostname
);
492 /* We failed to handle the error */
493 return nfs4_map_errors(ret
);
495 exception
->recovering
= 1;
499 /* This is the error handling routine for processes that are allowed
502 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
504 struct nfs_client
*clp
= server
->nfs_client
;
507 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
508 if (exception
->delay
) {
509 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
512 if (exception
->recovering
) {
513 ret
= nfs4_wait_clnt_recover(clp
);
514 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
521 exception
->retry
= 1;
526 nfs4_async_handle_exception(struct rpc_task
*task
, struct nfs_server
*server
,
527 int errorcode
, struct nfs4_exception
*exception
)
529 struct nfs_client
*clp
= server
->nfs_client
;
532 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
533 if (exception
->delay
) {
534 rpc_delay(task
, nfs4_update_delay(&exception
->timeout
));
537 if (exception
->recovering
) {
538 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
539 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
540 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
543 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
548 exception
->retry
= 1;
553 nfs4_async_handle_error(struct rpc_task
*task
, struct nfs_server
*server
,
554 struct nfs4_state
*state
, long *timeout
)
556 struct nfs4_exception exception
= {
560 if (task
->tk_status
>= 0)
563 exception
.timeout
= *timeout
;
564 task
->tk_status
= nfs4_async_handle_exception(task
, server
,
567 if (exception
.delay
&& timeout
)
568 *timeout
= exception
.timeout
;
575 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
576 * or 'false' otherwise.
578 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
580 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
582 if (flavor
== RPC_AUTH_GSS_KRB5I
||
583 flavor
== RPC_AUTH_GSS_KRB5P
)
589 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
591 spin_lock(&clp
->cl_lock
);
592 if (time_before(clp
->cl_last_renewal
,timestamp
))
593 clp
->cl_last_renewal
= timestamp
;
594 spin_unlock(&clp
->cl_lock
);
597 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
599 struct nfs_client
*clp
= server
->nfs_client
;
601 if (!nfs4_has_session(clp
))
602 do_renew_lease(clp
, timestamp
);
605 struct nfs4_call_sync_data
{
606 const struct nfs_server
*seq_server
;
607 struct nfs4_sequence_args
*seq_args
;
608 struct nfs4_sequence_res
*seq_res
;
611 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
612 struct nfs4_sequence_res
*res
, int cache_reply
)
614 args
->sa_slot
= NULL
;
615 args
->sa_cache_this
= cache_reply
;
616 args
->sa_privileged
= 0;
621 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
623 args
->sa_privileged
= 1;
626 int nfs40_setup_sequence(struct nfs4_slot_table
*tbl
,
627 struct nfs4_sequence_args
*args
,
628 struct nfs4_sequence_res
*res
,
629 struct rpc_task
*task
)
631 struct nfs4_slot
*slot
;
633 /* slot already allocated? */
634 if (res
->sr_slot
!= NULL
)
637 spin_lock(&tbl
->slot_tbl_lock
);
638 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
641 slot
= nfs4_alloc_slot(tbl
);
643 if (slot
== ERR_PTR(-ENOMEM
))
644 task
->tk_timeout
= HZ
>> 2;
647 spin_unlock(&tbl
->slot_tbl_lock
);
649 slot
->privileged
= args
->sa_privileged
? 1 : 0;
650 args
->sa_slot
= slot
;
654 rpc_call_start(task
);
658 if (args
->sa_privileged
)
659 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
660 NULL
, RPC_PRIORITY_PRIVILEGED
);
662 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
663 spin_unlock(&tbl
->slot_tbl_lock
);
666 EXPORT_SYMBOL_GPL(nfs40_setup_sequence
);
668 static void nfs40_sequence_free_slot(struct nfs4_sequence_res
*res
)
670 struct nfs4_slot
*slot
= res
->sr_slot
;
671 struct nfs4_slot_table
*tbl
;
674 spin_lock(&tbl
->slot_tbl_lock
);
675 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
676 nfs4_free_slot(tbl
, slot
);
677 spin_unlock(&tbl
->slot_tbl_lock
);
682 static int nfs40_sequence_done(struct rpc_task
*task
,
683 struct nfs4_sequence_res
*res
)
685 if (res
->sr_slot
!= NULL
)
686 nfs40_sequence_free_slot(res
);
690 #if defined(CONFIG_NFS_V4_1)
692 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
694 struct nfs4_session
*session
;
695 struct nfs4_slot_table
*tbl
;
696 struct nfs4_slot
*slot
= res
->sr_slot
;
697 bool send_new_highest_used_slotid
= false;
700 session
= tbl
->session
;
702 /* Bump the slot sequence number */
707 spin_lock(&tbl
->slot_tbl_lock
);
708 /* Be nice to the server: try to ensure that the last transmitted
709 * value for highest_user_slotid <= target_highest_slotid
711 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
712 send_new_highest_used_slotid
= true;
714 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
715 send_new_highest_used_slotid
= false;
718 nfs4_free_slot(tbl
, slot
);
720 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
721 send_new_highest_used_slotid
= false;
723 spin_unlock(&tbl
->slot_tbl_lock
);
725 if (send_new_highest_used_slotid
)
726 nfs41_notify_server(session
->clp
);
727 if (waitqueue_active(&tbl
->slot_waitq
))
728 wake_up_all(&tbl
->slot_waitq
);
731 static int nfs41_sequence_process(struct rpc_task
*task
,
732 struct nfs4_sequence_res
*res
)
734 struct nfs4_session
*session
;
735 struct nfs4_slot
*slot
= res
->sr_slot
;
736 struct nfs_client
*clp
;
737 bool interrupted
= false;
742 /* don't increment the sequence number if the task wasn't sent */
743 if (!RPC_WAS_SENT(task
))
746 session
= slot
->table
->session
;
748 if (slot
->interrupted
) {
749 slot
->interrupted
= 0;
753 trace_nfs4_sequence_done(session
, res
);
754 /* Check the SEQUENCE operation status */
755 switch (res
->sr_status
) {
757 /* If previous op on slot was interrupted and we reused
758 * the seq# and got a reply from the cache, then retry
760 if (task
->tk_status
== -EREMOTEIO
&& interrupted
) {
764 /* Update the slot's sequence and clientid lease timer */
767 do_renew_lease(clp
, res
->sr_timestamp
);
768 /* Check sequence flags */
769 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
,
771 nfs41_update_target_slotid(slot
->table
, slot
, res
);
775 * sr_status remains 1 if an RPC level error occurred.
776 * The server may or may not have processed the sequence
778 * Mark the slot as having hosted an interrupted RPC call.
780 slot
->interrupted
= 1;
783 /* The server detected a resend of the RPC call and
784 * returned NFS4ERR_DELAY as per Section 2.10.6.2
787 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
792 case -NFS4ERR_BADSLOT
:
794 * The slot id we used was probably retired. Try again
795 * using a different slot id.
798 case -NFS4ERR_SEQ_MISORDERED
:
800 * Was the last operation on this sequence interrupted?
801 * If so, retry after bumping the sequence number.
808 * Could this slot have been previously retired?
809 * If so, then the server may be expecting seq_nr = 1!
811 if (slot
->seq_nr
!= 1) {
816 case -NFS4ERR_SEQ_FALSE_RETRY
:
820 /* Just update the slot sequence no. */
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
829 if (rpc_restart_call_prepare(task
)) {
830 nfs41_sequence_free_slot(res
);
836 if (!rpc_restart_call(task
))
838 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
842 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
844 if (!nfs41_sequence_process(task
, res
))
846 if (res
->sr_slot
!= NULL
)
847 nfs41_sequence_free_slot(res
);
851 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
853 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
855 if (res
->sr_slot
== NULL
)
857 if (res
->sr_slot
->table
->session
!= NULL
)
858 return nfs41_sequence_process(task
, res
);
859 return nfs40_sequence_done(task
, res
);
862 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
864 if (res
->sr_slot
!= NULL
) {
865 if (res
->sr_slot
->table
->session
!= NULL
)
866 nfs41_sequence_free_slot(res
);
868 nfs40_sequence_free_slot(res
);
872 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
874 if (res
->sr_slot
== NULL
)
876 if (!res
->sr_slot
->table
->session
)
877 return nfs40_sequence_done(task
, res
);
878 return nfs41_sequence_done(task
, res
);
880 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
882 int nfs41_setup_sequence(struct nfs4_session
*session
,
883 struct nfs4_sequence_args
*args
,
884 struct nfs4_sequence_res
*res
,
885 struct rpc_task
*task
)
887 struct nfs4_slot
*slot
;
888 struct nfs4_slot_table
*tbl
;
890 dprintk("--> %s\n", __func__
);
891 /* slot already allocated? */
892 if (res
->sr_slot
!= NULL
)
895 tbl
= &session
->fc_slot_table
;
897 task
->tk_timeout
= 0;
899 spin_lock(&tbl
->slot_tbl_lock
);
900 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
901 !args
->sa_privileged
) {
902 /* The state manager will wait until the slot table is empty */
903 dprintk("%s session is draining\n", __func__
);
907 slot
= nfs4_alloc_slot(tbl
);
909 /* If out of memory, try again in 1/4 second */
910 if (slot
== ERR_PTR(-ENOMEM
))
911 task
->tk_timeout
= HZ
>> 2;
912 dprintk("<-- %s: no free slots\n", __func__
);
915 spin_unlock(&tbl
->slot_tbl_lock
);
917 slot
->privileged
= args
->sa_privileged
? 1 : 0;
918 args
->sa_slot
= slot
;
920 dprintk("<-- %s slotid=%u seqid=%u\n", __func__
,
921 slot
->slot_nr
, slot
->seq_nr
);
924 res
->sr_timestamp
= jiffies
;
925 res
->sr_status_flags
= 0;
927 * sr_status is only set in decode_sequence, and so will remain
928 * set to 1 if an rpc level failure occurs.
931 trace_nfs4_setup_sequence(session
, args
);
933 rpc_call_start(task
);
936 /* Privileged tasks are queued with top priority */
937 if (args
->sa_privileged
)
938 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
939 NULL
, RPC_PRIORITY_PRIVILEGED
);
941 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
942 spin_unlock(&tbl
->slot_tbl_lock
);
945 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
947 static int nfs4_setup_sequence(const struct nfs_server
*server
,
948 struct nfs4_sequence_args
*args
,
949 struct nfs4_sequence_res
*res
,
950 struct rpc_task
*task
)
952 struct nfs4_session
*session
= nfs4_get_session(server
);
956 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
959 dprintk("--> %s clp %p session %p sr_slot %u\n",
960 __func__
, session
->clp
, session
, res
->sr_slot
?
961 res
->sr_slot
->slot_nr
: NFS4_NO_SLOT
);
963 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
965 dprintk("<-- %s status=%d\n", __func__
, ret
);
969 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
971 struct nfs4_call_sync_data
*data
= calldata
;
972 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
974 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
976 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
979 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
981 struct nfs4_call_sync_data
*data
= calldata
;
983 nfs41_sequence_done(task
, data
->seq_res
);
986 static const struct rpc_call_ops nfs41_call_sync_ops
= {
987 .rpc_call_prepare
= nfs41_call_sync_prepare
,
988 .rpc_call_done
= nfs41_call_sync_done
,
991 #else /* !CONFIG_NFS_V4_1 */
993 static int nfs4_setup_sequence(const struct nfs_server
*server
,
994 struct nfs4_sequence_args
*args
,
995 struct nfs4_sequence_res
*res
,
996 struct rpc_task
*task
)
998 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
1002 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
1004 return nfs40_sequence_done(task
, res
);
1007 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
1009 if (res
->sr_slot
!= NULL
)
1010 nfs40_sequence_free_slot(res
);
1013 int nfs4_sequence_done(struct rpc_task
*task
,
1014 struct nfs4_sequence_res
*res
)
1016 return nfs40_sequence_done(task
, res
);
1018 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
1020 #endif /* !CONFIG_NFS_V4_1 */
1022 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
1024 struct nfs4_call_sync_data
*data
= calldata
;
1025 nfs4_setup_sequence(data
->seq_server
,
1026 data
->seq_args
, data
->seq_res
, task
);
1029 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
1031 struct nfs4_call_sync_data
*data
= calldata
;
1032 nfs4_sequence_done(task
, data
->seq_res
);
1035 static const struct rpc_call_ops nfs40_call_sync_ops
= {
1036 .rpc_call_prepare
= nfs40_call_sync_prepare
,
1037 .rpc_call_done
= nfs40_call_sync_done
,
1040 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
1041 struct nfs_server
*server
,
1042 struct rpc_message
*msg
,
1043 struct nfs4_sequence_args
*args
,
1044 struct nfs4_sequence_res
*res
)
1047 struct rpc_task
*task
;
1048 struct nfs_client
*clp
= server
->nfs_client
;
1049 struct nfs4_call_sync_data data
= {
1050 .seq_server
= server
,
1054 struct rpc_task_setup task_setup
= {
1057 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
1058 .callback_data
= &data
1061 task
= rpc_run_task(&task_setup
);
1063 ret
= PTR_ERR(task
);
1065 ret
= task
->tk_status
;
1071 int nfs4_call_sync(struct rpc_clnt
*clnt
,
1072 struct nfs_server
*server
,
1073 struct rpc_message
*msg
,
1074 struct nfs4_sequence_args
*args
,
1075 struct nfs4_sequence_res
*res
,
1078 nfs4_init_sequence(args
, res
, cache_reply
);
1079 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
1082 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
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 nfs_force_lookup_revalidate(dir
);
1090 dir
->i_version
= cinfo
->after
;
1091 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
1092 nfs_fscache_invalidate(dir
);
1093 spin_unlock(&dir
->i_lock
);
1096 struct nfs4_opendata
{
1098 struct nfs_openargs o_arg
;
1099 struct nfs_openres o_res
;
1100 struct nfs_open_confirmargs c_arg
;
1101 struct nfs_open_confirmres c_res
;
1102 struct nfs4_string owner_name
;
1103 struct nfs4_string group_name
;
1104 struct nfs4_label
*a_label
;
1105 struct nfs_fattr f_attr
;
1106 struct nfs4_label
*f_label
;
1108 struct dentry
*dentry
;
1109 struct nfs4_state_owner
*owner
;
1110 struct nfs4_state
*state
;
1112 unsigned long timestamp
;
1113 unsigned int rpc_done
: 1;
1114 unsigned int file_created
: 1;
1115 unsigned int is_recover
: 1;
1120 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
1121 int err
, struct nfs4_exception
*exception
)
1125 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1127 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
1128 exception
->retry
= 1;
1133 nfs4_map_atomic_open_share(struct nfs_server
*server
,
1134 fmode_t fmode
, int openflags
)
1138 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
1140 res
= NFS4_SHARE_ACCESS_READ
;
1143 res
= NFS4_SHARE_ACCESS_WRITE
;
1145 case FMODE_READ
|FMODE_WRITE
:
1146 res
= NFS4_SHARE_ACCESS_BOTH
;
1148 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1150 /* Want no delegation if we're using O_DIRECT */
1151 if (openflags
& O_DIRECT
)
1152 res
|= NFS4_SHARE_WANT_NO_DELEG
;
1157 static enum open_claim_type4
1158 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
1159 enum open_claim_type4 claim
)
1161 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
1166 case NFS4_OPEN_CLAIM_FH
:
1167 return NFS4_OPEN_CLAIM_NULL
;
1168 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1169 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1170 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1171 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
1175 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
1177 p
->o_res
.f_attr
= &p
->f_attr
;
1178 p
->o_res
.f_label
= p
->f_label
;
1179 p
->o_res
.seqid
= p
->o_arg
.seqid
;
1180 p
->c_res
.seqid
= p
->c_arg
.seqid
;
1181 p
->o_res
.server
= p
->o_arg
.server
;
1182 p
->o_res
.access_request
= p
->o_arg
.access
;
1183 nfs_fattr_init(&p
->f_attr
);
1184 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
1187 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
1188 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
1189 const struct iattr
*attrs
,
1190 struct nfs4_label
*label
,
1191 enum open_claim_type4 claim
,
1194 struct dentry
*parent
= dget_parent(dentry
);
1195 struct inode
*dir
= d_inode(parent
);
1196 struct nfs_server
*server
= NFS_SERVER(dir
);
1197 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1198 struct nfs4_opendata
*p
;
1200 p
= kzalloc(sizeof(*p
), gfp_mask
);
1204 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1205 if (IS_ERR(p
->f_label
))
1208 p
->a_label
= nfs4_label_alloc(server
, gfp_mask
);
1209 if (IS_ERR(p
->a_label
))
1212 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1213 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1214 if (IS_ERR(p
->o_arg
.seqid
))
1215 goto err_free_label
;
1216 nfs_sb_active(dentry
->d_sb
);
1217 p
->dentry
= dget(dentry
);
1220 atomic_inc(&sp
->so_count
);
1221 p
->o_arg
.open_flags
= flags
;
1222 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1223 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1224 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1226 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1227 * will return permission denied for all bits until close */
1228 if (!(flags
& O_EXCL
)) {
1229 /* ask server to check for all possible rights as results
1231 switch (p
->o_arg
.claim
) {
1234 case NFS4_OPEN_CLAIM_NULL
:
1235 case NFS4_OPEN_CLAIM_FH
:
1236 p
->o_arg
.access
= NFS4_ACCESS_READ
|
1237 NFS4_ACCESS_MODIFY
|
1238 NFS4_ACCESS_EXTEND
|
1239 NFS4_ACCESS_EXECUTE
;
1242 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1243 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1244 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1245 p
->o_arg
.name
= &dentry
->d_name
;
1246 p
->o_arg
.server
= server
;
1247 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1248 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1249 p
->o_arg
.label
= nfs4_label_copy(p
->a_label
, label
);
1250 switch (p
->o_arg
.claim
) {
1251 case NFS4_OPEN_CLAIM_NULL
:
1252 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1253 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1254 p
->o_arg
.fh
= NFS_FH(dir
);
1256 case NFS4_OPEN_CLAIM_PREVIOUS
:
1257 case NFS4_OPEN_CLAIM_FH
:
1258 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1259 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1260 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1262 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1265 p
->o_arg
.u
.attrs
= &p
->attrs
;
1266 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1269 verf
[1] = current
->pid
;
1270 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1271 sizeof(p
->o_arg
.u
.verifier
.data
));
1273 p
->c_arg
.fh
= &p
->o_res
.fh
;
1274 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1275 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1276 nfs4_init_opendata_res(p
);
1277 kref_init(&p
->kref
);
1281 nfs4_label_free(p
->a_label
);
1283 nfs4_label_free(p
->f_label
);
1291 static void nfs4_opendata_free(struct kref
*kref
)
1293 struct nfs4_opendata
*p
= container_of(kref
,
1294 struct nfs4_opendata
, kref
);
1295 struct super_block
*sb
= p
->dentry
->d_sb
;
1297 nfs_free_seqid(p
->o_arg
.seqid
);
1298 nfs4_sequence_free_slot(&p
->o_res
.seq_res
);
1299 if (p
->state
!= NULL
)
1300 nfs4_put_open_state(p
->state
);
1301 nfs4_put_state_owner(p
->owner
);
1303 nfs4_label_free(p
->a_label
);
1304 nfs4_label_free(p
->f_label
);
1308 nfs_sb_deactive(sb
);
1309 nfs_fattr_free_names(&p
->f_attr
);
1310 kfree(p
->f_attr
.mdsthreshold
);
1314 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1317 kref_put(&p
->kref
, nfs4_opendata_free
);
1320 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1324 ret
= rpc_wait_for_completion_task(task
);
1328 static bool nfs4_mode_match_open_stateid(struct nfs4_state
*state
,
1331 switch(fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1332 case FMODE_READ
|FMODE_WRITE
:
1333 return state
->n_rdwr
!= 0;
1335 return state
->n_wronly
!= 0;
1337 return state
->n_rdonly
!= 0;
1343 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1347 if (open_mode
& (O_EXCL
|O_TRUNC
))
1349 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1351 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1352 && state
->n_rdonly
!= 0;
1355 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1356 && state
->n_wronly
!= 0;
1358 case FMODE_READ
|FMODE_WRITE
:
1359 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1360 && state
->n_rdwr
!= 0;
1366 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
,
1367 enum open_claim_type4 claim
)
1369 if (delegation
== NULL
)
1371 if ((delegation
->type
& fmode
) != fmode
)
1373 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1376 case NFS4_OPEN_CLAIM_NULL
:
1377 case NFS4_OPEN_CLAIM_FH
:
1379 case NFS4_OPEN_CLAIM_PREVIOUS
:
1380 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1385 nfs_mark_delegation_referenced(delegation
);
1389 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1398 case FMODE_READ
|FMODE_WRITE
:
1401 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1404 #ifdef CONFIG_NFS_V4_1
1405 static bool nfs_open_stateid_recover_openmode(struct nfs4_state
*state
)
1407 if (state
->n_rdonly
&& !test_bit(NFS_O_RDONLY_STATE
, &state
->flags
))
1409 if (state
->n_wronly
&& !test_bit(NFS_O_WRONLY_STATE
, &state
->flags
))
1411 if (state
->n_rdwr
&& !test_bit(NFS_O_RDWR_STATE
, &state
->flags
))
1415 #endif /* CONFIG_NFS_V4_1 */
1417 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1419 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1420 bool need_recover
= false;
1422 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1423 need_recover
= true;
1424 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1425 need_recover
= true;
1426 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1427 need_recover
= true;
1429 nfs4_state_mark_reclaim_nograce(clp
, state
);
1432 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1433 const nfs4_stateid
*stateid
, nfs4_stateid
*freeme
)
1435 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1437 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1438 nfs4_stateid_copy(freeme
, &state
->open_stateid
);
1439 nfs_test_and_clear_all_open_stateid(state
);
1442 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1447 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1449 if (!(state
->n_wronly
|| state
->n_rdonly
|| state
->n_rdwr
))
1451 if (state
->n_wronly
)
1452 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1453 if (state
->n_rdonly
)
1454 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1456 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1457 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1460 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1461 nfs4_stateid
*stateid
, fmode_t fmode
)
1463 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1464 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1466 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1469 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1472 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1473 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1474 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1476 if (stateid
== NULL
)
1478 /* Handle OPEN+OPEN_DOWNGRADE races */
1479 if (nfs4_stateid_match_other(stateid
, &state
->open_stateid
) &&
1480 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
)) {
1481 nfs_resync_open_stateid_locked(state
);
1484 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1485 nfs4_stateid_copy(&state
->stateid
, stateid
);
1486 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1489 static void nfs_clear_open_stateid(struct nfs4_state
*state
,
1490 nfs4_stateid
*arg_stateid
,
1491 nfs4_stateid
*stateid
, fmode_t fmode
)
1493 write_seqlock(&state
->seqlock
);
1494 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1495 if (nfs4_state_match_open_stateid_other(state
, arg_stateid
))
1496 nfs_clear_open_stateid_locked(state
, stateid
, fmode
);
1497 write_sequnlock(&state
->seqlock
);
1498 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1499 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1502 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
,
1503 const nfs4_stateid
*stateid
, fmode_t fmode
,
1504 nfs4_stateid
*freeme
)
1508 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1511 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1513 case FMODE_READ
|FMODE_WRITE
:
1514 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1516 if (!nfs_need_update_open_stateid(state
, stateid
, freeme
))
1518 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1519 nfs4_stateid_copy(&state
->stateid
, stateid
);
1520 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1523 static void __update_open_stateid(struct nfs4_state
*state
,
1524 const nfs4_stateid
*open_stateid
,
1525 const nfs4_stateid
*deleg_stateid
,
1527 nfs4_stateid
*freeme
)
1530 * Protect the call to nfs4_state_set_mode_locked and
1531 * serialise the stateid update
1533 spin_lock(&state
->owner
->so_lock
);
1534 write_seqlock(&state
->seqlock
);
1535 if (deleg_stateid
!= NULL
) {
1536 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1537 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1539 if (open_stateid
!= NULL
)
1540 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
, freeme
);
1541 write_sequnlock(&state
->seqlock
);
1542 update_open_stateflags(state
, fmode
);
1543 spin_unlock(&state
->owner
->so_lock
);
1546 static int update_open_stateid(struct nfs4_state
*state
,
1547 const nfs4_stateid
*open_stateid
,
1548 const nfs4_stateid
*delegation
,
1551 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1552 struct nfs_client
*clp
= server
->nfs_client
;
1553 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1554 struct nfs_delegation
*deleg_cur
;
1555 nfs4_stateid freeme
= { };
1558 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1561 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1562 if (deleg_cur
== NULL
)
1565 spin_lock(&deleg_cur
->lock
);
1566 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1567 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1568 (deleg_cur
->type
& fmode
) != fmode
)
1569 goto no_delegation_unlock
;
1571 if (delegation
== NULL
)
1572 delegation
= &deleg_cur
->stateid
;
1573 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1574 goto no_delegation_unlock
;
1576 nfs_mark_delegation_referenced(deleg_cur
);
1577 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
,
1580 no_delegation_unlock
:
1581 spin_unlock(&deleg_cur
->lock
);
1585 if (!ret
&& open_stateid
!= NULL
) {
1586 __update_open_stateid(state
, open_stateid
, NULL
, fmode
, &freeme
);
1589 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1590 nfs4_schedule_state_manager(clp
);
1591 if (freeme
.type
!= 0)
1592 nfs4_test_and_free_stateid(server
, &freeme
,
1593 state
->owner
->so_cred
);
1598 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1599 const nfs4_stateid
*stateid
)
1601 struct nfs4_state
*state
= lsp
->ls_state
;
1604 spin_lock(&state
->state_lock
);
1605 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1607 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1609 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1612 spin_unlock(&state
->state_lock
);
1616 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1618 struct nfs_delegation
*delegation
;
1621 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1622 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1627 nfs4_inode_return_delegation(inode
);
1630 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1632 struct nfs4_state
*state
= opendata
->state
;
1633 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1634 struct nfs_delegation
*delegation
;
1635 int open_mode
= opendata
->o_arg
.open_flags
;
1636 fmode_t fmode
= opendata
->o_arg
.fmode
;
1637 enum open_claim_type4 claim
= opendata
->o_arg
.claim
;
1638 nfs4_stateid stateid
;
1642 spin_lock(&state
->owner
->so_lock
);
1643 if (can_open_cached(state
, fmode
, open_mode
)) {
1644 update_open_stateflags(state
, fmode
);
1645 spin_unlock(&state
->owner
->so_lock
);
1646 goto out_return_state
;
1648 spin_unlock(&state
->owner
->so_lock
);
1650 delegation
= rcu_dereference(nfsi
->delegation
);
1651 if (!can_open_delegated(delegation
, fmode
, claim
)) {
1655 /* Save the delegation */
1656 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1658 nfs_release_seqid(opendata
->o_arg
.seqid
);
1659 if (!opendata
->is_recover
) {
1660 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1666 /* Try to update the stateid using the delegation */
1667 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1668 goto out_return_state
;
1671 return ERR_PTR(ret
);
1673 atomic_inc(&state
->count
);
1678 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1680 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1681 struct nfs_delegation
*delegation
;
1682 int delegation_flags
= 0;
1685 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1687 delegation_flags
= delegation
->flags
;
1689 switch (data
->o_arg
.claim
) {
1692 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1693 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1694 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1695 "returning a delegation for "
1696 "OPEN(CLAIM_DELEGATE_CUR)\n",
1700 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1701 nfs_inode_set_delegation(state
->inode
,
1702 data
->owner
->so_cred
,
1705 nfs_inode_reclaim_delegation(state
->inode
,
1706 data
->owner
->so_cred
,
1711 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1712 * and update the nfs4_state.
1714 static struct nfs4_state
*
1715 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1717 struct inode
*inode
= data
->state
->inode
;
1718 struct nfs4_state
*state
= data
->state
;
1721 if (!data
->rpc_done
) {
1722 if (data
->rpc_status
) {
1723 ret
= data
->rpc_status
;
1726 /* cached opens have already been processed */
1730 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1734 if (data
->o_res
.delegation_type
!= 0)
1735 nfs4_opendata_check_deleg(data
, state
);
1737 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1739 atomic_inc(&state
->count
);
1743 return ERR_PTR(ret
);
1747 static struct nfs4_state
*
1748 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1750 struct inode
*inode
;
1751 struct nfs4_state
*state
= NULL
;
1754 if (!data
->rpc_done
) {
1755 state
= nfs4_try_open_cached(data
);
1756 trace_nfs4_cached_open(data
->state
);
1761 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1763 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1764 ret
= PTR_ERR(inode
);
1768 state
= nfs4_get_open_state(inode
, data
->owner
);
1771 if (data
->o_res
.delegation_type
!= 0)
1772 nfs4_opendata_check_deleg(data
, state
);
1773 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1777 nfs_release_seqid(data
->o_arg
.seqid
);
1782 return ERR_PTR(ret
);
1785 static struct nfs4_state
*
1786 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1788 struct nfs4_state
*ret
;
1790 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1791 ret
=_nfs4_opendata_reclaim_to_nfs4_state(data
);
1793 ret
= _nfs4_opendata_to_nfs4_state(data
);
1794 nfs4_sequence_free_slot(&data
->o_res
.seq_res
);
1798 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1800 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1801 struct nfs_open_context
*ctx
;
1803 spin_lock(&state
->inode
->i_lock
);
1804 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1805 if (ctx
->state
!= state
)
1807 get_nfs_open_context(ctx
);
1808 spin_unlock(&state
->inode
->i_lock
);
1811 spin_unlock(&state
->inode
->i_lock
);
1812 return ERR_PTR(-ENOENT
);
1815 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1816 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1818 struct nfs4_opendata
*opendata
;
1820 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1821 NULL
, NULL
, claim
, GFP_NOFS
);
1822 if (opendata
== NULL
)
1823 return ERR_PTR(-ENOMEM
);
1824 opendata
->state
= state
;
1825 atomic_inc(&state
->count
);
1829 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
,
1832 struct nfs4_state
*newstate
;
1835 if (!nfs4_mode_match_open_stateid(opendata
->state
, fmode
))
1837 opendata
->o_arg
.open_flags
= 0;
1838 opendata
->o_arg
.fmode
= fmode
;
1839 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1840 NFS_SB(opendata
->dentry
->d_sb
),
1842 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1843 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1844 nfs4_init_opendata_res(opendata
);
1845 ret
= _nfs4_recover_proc_open(opendata
);
1848 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1849 if (IS_ERR(newstate
))
1850 return PTR_ERR(newstate
);
1851 if (newstate
!= opendata
->state
)
1853 nfs4_close_state(newstate
, fmode
);
1857 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1861 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1862 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1863 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1864 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1865 /* memory barrier prior to reading state->n_* */
1866 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1867 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1869 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1872 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1875 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1879 * We may have performed cached opens for all three recoveries.
1880 * Check if we need to update the current stateid.
1882 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1883 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1884 write_seqlock(&state
->seqlock
);
1885 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1886 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1887 write_sequnlock(&state
->seqlock
);
1894 * reclaim state on the server after a reboot.
1896 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1898 struct nfs_delegation
*delegation
;
1899 struct nfs4_opendata
*opendata
;
1900 fmode_t delegation_type
= 0;
1903 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1904 NFS4_OPEN_CLAIM_PREVIOUS
);
1905 if (IS_ERR(opendata
))
1906 return PTR_ERR(opendata
);
1908 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1909 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1910 delegation_type
= delegation
->type
;
1912 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1913 status
= nfs4_open_recover(opendata
, state
);
1914 nfs4_opendata_put(opendata
);
1918 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1920 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1921 struct nfs4_exception exception
= { };
1924 err
= _nfs4_do_open_reclaim(ctx
, state
);
1925 trace_nfs4_open_reclaim(ctx
, 0, err
);
1926 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1928 if (err
!= -NFS4ERR_DELAY
)
1930 nfs4_handle_exception(server
, err
, &exception
);
1931 } while (exception
.retry
);
1935 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1937 struct nfs_open_context
*ctx
;
1940 ctx
= nfs4_state_find_open_context(state
);
1943 ret
= nfs4_do_open_reclaim(ctx
, state
);
1944 put_nfs_open_context(ctx
);
1948 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1952 printk(KERN_ERR
"NFS: %s: unhandled error "
1953 "%d.\n", __func__
, err
);
1959 case -NFS4ERR_BADSESSION
:
1960 case -NFS4ERR_BADSLOT
:
1961 case -NFS4ERR_BAD_HIGH_SLOT
:
1962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1963 case -NFS4ERR_DEADSESSION
:
1964 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1965 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1967 case -NFS4ERR_STALE_CLIENTID
:
1968 case -NFS4ERR_STALE_STATEID
:
1969 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1970 /* Don't recall a delegation if it was lost */
1971 nfs4_schedule_lease_recovery(server
->nfs_client
);
1973 case -NFS4ERR_MOVED
:
1974 nfs4_schedule_migration_recovery(server
);
1976 case -NFS4ERR_LEASE_MOVED
:
1977 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1979 case -NFS4ERR_DELEG_REVOKED
:
1980 case -NFS4ERR_ADMIN_REVOKED
:
1981 case -NFS4ERR_EXPIRED
:
1982 case -NFS4ERR_BAD_STATEID
:
1983 case -NFS4ERR_OPENMODE
:
1984 nfs_inode_find_state_and_recover(state
->inode
,
1986 nfs4_schedule_stateid_recovery(server
, state
);
1988 case -NFS4ERR_DELAY
:
1989 case -NFS4ERR_GRACE
:
1990 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1994 case -NFS4ERR_DENIED
:
1995 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2001 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
,
2002 struct nfs4_state
*state
, const nfs4_stateid
*stateid
,
2005 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2006 struct nfs4_opendata
*opendata
;
2009 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2010 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
2011 if (IS_ERR(opendata
))
2012 return PTR_ERR(opendata
);
2013 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
2014 write_seqlock(&state
->seqlock
);
2015 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2016 write_sequnlock(&state
->seqlock
);
2017 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2018 switch (type
& (FMODE_READ
|FMODE_WRITE
)) {
2019 case FMODE_READ
|FMODE_WRITE
:
2021 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
2024 err
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
2028 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
2030 nfs4_opendata_put(opendata
);
2031 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
2034 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
2036 struct nfs4_opendata
*data
= calldata
;
2038 nfs40_setup_sequence(data
->o_arg
.server
->nfs_client
->cl_slot_tbl
,
2039 &data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, task
);
2042 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
2044 struct nfs4_opendata
*data
= calldata
;
2046 nfs40_sequence_done(task
, &data
->c_res
.seq_res
);
2048 data
->rpc_status
= task
->tk_status
;
2049 if (data
->rpc_status
== 0) {
2050 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
2051 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2052 renew_lease(data
->o_res
.server
, data
->timestamp
);
2057 static void nfs4_open_confirm_release(void *calldata
)
2059 struct nfs4_opendata
*data
= calldata
;
2060 struct nfs4_state
*state
= NULL
;
2062 /* If this request hasn't been cancelled, do nothing */
2063 if (data
->cancelled
== 0)
2065 /* In case of error, no cleanup! */
2066 if (!data
->rpc_done
)
2068 state
= nfs4_opendata_to_nfs4_state(data
);
2070 nfs4_close_state(state
, data
->o_arg
.fmode
);
2072 nfs4_opendata_put(data
);
2075 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
2076 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
2077 .rpc_call_done
= nfs4_open_confirm_done
,
2078 .rpc_release
= nfs4_open_confirm_release
,
2082 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2084 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
2086 struct nfs_server
*server
= NFS_SERVER(d_inode(data
->dir
));
2087 struct rpc_task
*task
;
2088 struct rpc_message msg
= {
2089 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
2090 .rpc_argp
= &data
->c_arg
,
2091 .rpc_resp
= &data
->c_res
,
2092 .rpc_cred
= data
->owner
->so_cred
,
2094 struct rpc_task_setup task_setup_data
= {
2095 .rpc_client
= server
->client
,
2096 .rpc_message
= &msg
,
2097 .callback_ops
= &nfs4_open_confirm_ops
,
2098 .callback_data
= data
,
2099 .workqueue
= nfsiod_workqueue
,
2100 .flags
= RPC_TASK_ASYNC
,
2104 nfs4_init_sequence(&data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, 1);
2105 kref_get(&data
->kref
);
2107 data
->rpc_status
= 0;
2108 data
->timestamp
= jiffies
;
2109 if (data
->is_recover
)
2110 nfs4_set_sequence_privileged(&data
->c_arg
.seq_args
);
2111 task
= rpc_run_task(&task_setup_data
);
2113 return PTR_ERR(task
);
2114 status
= nfs4_wait_for_completion_rpc_task(task
);
2116 data
->cancelled
= 1;
2119 status
= data
->rpc_status
;
2124 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
2126 struct nfs4_opendata
*data
= calldata
;
2127 struct nfs4_state_owner
*sp
= data
->owner
;
2128 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
2129 enum open_claim_type4 claim
= data
->o_arg
.claim
;
2131 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
2134 * Check if we still need to send an OPEN call, or if we can use
2135 * a delegation instead.
2137 if (data
->state
!= NULL
) {
2138 struct nfs_delegation
*delegation
;
2140 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
2143 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
2144 if (can_open_delegated(delegation
, data
->o_arg
.fmode
, claim
))
2145 goto unlock_no_action
;
2148 /* Update client id. */
2149 data
->o_arg
.clientid
= clp
->cl_clientid
;
2153 case NFS4_OPEN_CLAIM_PREVIOUS
:
2154 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
2155 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
2156 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
2157 case NFS4_OPEN_CLAIM_FH
:
2158 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
2159 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
2161 data
->timestamp
= jiffies
;
2162 if (nfs4_setup_sequence(data
->o_arg
.server
,
2163 &data
->o_arg
.seq_args
,
2164 &data
->o_res
.seq_res
,
2166 nfs_release_seqid(data
->o_arg
.seqid
);
2168 /* Set the create mode (note dependency on the session type) */
2169 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
2170 if (data
->o_arg
.open_flags
& O_EXCL
) {
2171 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
2172 if (nfs4_has_persistent_session(clp
))
2173 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
2174 else if (clp
->cl_mvops
->minor_version
> 0)
2175 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
2179 trace_nfs4_cached_open(data
->state
);
2182 task
->tk_action
= NULL
;
2184 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
2187 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
2189 struct nfs4_opendata
*data
= calldata
;
2191 data
->rpc_status
= task
->tk_status
;
2193 if (!nfs4_sequence_process(task
, &data
->o_res
.seq_res
))
2196 if (task
->tk_status
== 0) {
2197 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
2198 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
2202 data
->rpc_status
= -ELOOP
;
2205 data
->rpc_status
= -EISDIR
;
2208 data
->rpc_status
= -ENOTDIR
;
2211 renew_lease(data
->o_res
.server
, data
->timestamp
);
2212 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
2213 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2218 static void nfs4_open_release(void *calldata
)
2220 struct nfs4_opendata
*data
= calldata
;
2221 struct nfs4_state
*state
= NULL
;
2223 /* If this request hasn't been cancelled, do nothing */
2224 if (data
->cancelled
== 0)
2226 /* In case of error, no cleanup! */
2227 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
2229 /* In case we need an open_confirm, no cleanup! */
2230 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2232 state
= nfs4_opendata_to_nfs4_state(data
);
2234 nfs4_close_state(state
, data
->o_arg
.fmode
);
2236 nfs4_opendata_put(data
);
2239 static const struct rpc_call_ops nfs4_open_ops
= {
2240 .rpc_call_prepare
= nfs4_open_prepare
,
2241 .rpc_call_done
= nfs4_open_done
,
2242 .rpc_release
= nfs4_open_release
,
2245 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
2247 struct inode
*dir
= d_inode(data
->dir
);
2248 struct nfs_server
*server
= NFS_SERVER(dir
);
2249 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2250 struct nfs_openres
*o_res
= &data
->o_res
;
2251 struct rpc_task
*task
;
2252 struct rpc_message msg
= {
2253 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
2256 .rpc_cred
= data
->owner
->so_cred
,
2258 struct rpc_task_setup task_setup_data
= {
2259 .rpc_client
= server
->client
,
2260 .rpc_message
= &msg
,
2261 .callback_ops
= &nfs4_open_ops
,
2262 .callback_data
= data
,
2263 .workqueue
= nfsiod_workqueue
,
2264 .flags
= RPC_TASK_ASYNC
,
2268 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
2269 kref_get(&data
->kref
);
2271 data
->rpc_status
= 0;
2272 data
->cancelled
= 0;
2273 data
->is_recover
= 0;
2275 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2276 data
->is_recover
= 1;
2278 task
= rpc_run_task(&task_setup_data
);
2280 return PTR_ERR(task
);
2281 status
= nfs4_wait_for_completion_rpc_task(task
);
2283 data
->cancelled
= 1;
2286 status
= data
->rpc_status
;
2292 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
2294 struct inode
*dir
= d_inode(data
->dir
);
2295 struct nfs_openres
*o_res
= &data
->o_res
;
2298 status
= nfs4_run_open_task(data
, 1);
2299 if (status
!= 0 || !data
->rpc_done
)
2302 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
2304 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2305 status
= _nfs4_proc_open_confirm(data
);
2314 * Additional permission checks in order to distinguish between an
2315 * open for read, and an open for execute. This works around the
2316 * fact that NFSv4 OPEN treats read and execute permissions as being
2318 * Note that in the non-execute case, we want to turn off permission
2319 * checking if we just created a new file (POSIX open() semantics).
2321 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2322 struct nfs4_opendata
*opendata
,
2323 struct nfs4_state
*state
, fmode_t fmode
,
2326 struct nfs_access_entry cache
;
2329 /* access call failed or for some reason the server doesn't
2330 * support any access modes -- defer access call until later */
2331 if (opendata
->o_res
.access_supported
== 0)
2336 * Use openflags to check for exec, because fmode won't
2337 * always have FMODE_EXEC set when file open for exec.
2339 if (openflags
& __FMODE_EXEC
) {
2340 /* ONLY check for exec rights */
2342 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2346 cache
.jiffies
= jiffies
;
2347 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2348 nfs_access_add_cache(state
->inode
, &cache
);
2350 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
2353 /* even though OPEN succeeded, access is denied. Close the file */
2354 nfs4_close_state(state
, fmode
);
2359 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2361 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2363 struct inode
*dir
= d_inode(data
->dir
);
2364 struct nfs_server
*server
= NFS_SERVER(dir
);
2365 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2366 struct nfs_openres
*o_res
= &data
->o_res
;
2369 status
= nfs4_run_open_task(data
, 0);
2370 if (!data
->rpc_done
)
2373 if (status
== -NFS4ERR_BADNAME
&&
2374 !(o_arg
->open_flags
& O_CREAT
))
2379 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2381 if (o_arg
->open_flags
& O_CREAT
) {
2382 update_changeattr(dir
, &o_res
->cinfo
);
2383 if (o_arg
->open_flags
& O_EXCL
)
2384 data
->file_created
= 1;
2385 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2386 data
->file_created
= 1;
2388 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2389 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2390 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2391 status
= _nfs4_proc_open_confirm(data
);
2395 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
2396 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2400 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
2402 return nfs4_client_recover_expired_lease(server
->nfs_client
);
2407 * reclaim state on the server after a network partition.
2408 * Assumes caller holds the appropriate lock
2410 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2412 struct nfs4_opendata
*opendata
;
2415 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2416 NFS4_OPEN_CLAIM_FH
);
2417 if (IS_ERR(opendata
))
2418 return PTR_ERR(opendata
);
2419 ret
= nfs4_open_recover(opendata
, state
);
2421 d_drop(ctx
->dentry
);
2422 nfs4_opendata_put(opendata
);
2426 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2428 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2429 struct nfs4_exception exception
= { };
2433 err
= _nfs4_open_expired(ctx
, state
);
2434 trace_nfs4_open_expired(ctx
, 0, err
);
2435 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2440 case -NFS4ERR_GRACE
:
2441 case -NFS4ERR_DELAY
:
2442 nfs4_handle_exception(server
, err
, &exception
);
2445 } while (exception
.retry
);
2450 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2452 struct nfs_open_context
*ctx
;
2455 ctx
= nfs4_state_find_open_context(state
);
2458 ret
= nfs4_do_open_expired(ctx
, state
);
2459 put_nfs_open_context(ctx
);
2463 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
,
2464 const nfs4_stateid
*stateid
)
2466 nfs_remove_bad_delegation(state
->inode
, stateid
);
2467 write_seqlock(&state
->seqlock
);
2468 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2469 write_sequnlock(&state
->seqlock
);
2470 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2473 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2475 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2476 nfs_finish_clear_delegation_stateid(state
, NULL
);
2479 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2481 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2482 nfs40_clear_delegation_stateid(state
);
2483 return nfs4_open_expired(sp
, state
);
2486 static int nfs40_test_and_free_expired_stateid(struct nfs_server
*server
,
2487 nfs4_stateid
*stateid
,
2488 struct rpc_cred
*cred
)
2490 return -NFS4ERR_BAD_STATEID
;
2493 #if defined(CONFIG_NFS_V4_1)
2494 static int nfs41_test_and_free_expired_stateid(struct nfs_server
*server
,
2495 nfs4_stateid
*stateid
,
2496 struct rpc_cred
*cred
)
2500 switch (stateid
->type
) {
2503 case NFS4_INVALID_STATEID_TYPE
:
2504 case NFS4_SPECIAL_STATEID_TYPE
:
2505 return -NFS4ERR_BAD_STATEID
;
2506 case NFS4_REVOKED_STATEID_TYPE
:
2510 status
= nfs41_test_stateid(server
, stateid
, cred
);
2512 case -NFS4ERR_EXPIRED
:
2513 case -NFS4ERR_ADMIN_REVOKED
:
2514 case -NFS4ERR_DELEG_REVOKED
:
2520 /* Ack the revoked state to the server */
2521 nfs41_free_stateid(server
, stateid
, cred
, true);
2522 return -NFS4ERR_EXPIRED
;
2525 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2527 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2528 nfs4_stateid stateid
;
2529 struct nfs_delegation
*delegation
;
2530 struct rpc_cred
*cred
;
2533 /* Get the delegation credential for use by test/free_stateid */
2535 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2536 if (delegation
== NULL
) {
2541 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2542 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
2544 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2548 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
)) {
2553 cred
= get_rpccred(delegation
->cred
);
2555 status
= nfs41_test_and_free_expired_stateid(server
, &stateid
, cred
);
2556 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2557 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
2558 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2564 * nfs41_check_expired_locks - possibly free a lock stateid
2566 * @state: NFSv4 state for an inode
2568 * Returns NFS_OK if recovery for this stateid is now finished.
2569 * Otherwise a negative NFS4ERR value is returned.
2571 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
2573 int status
, ret
= NFS_OK
;
2574 struct nfs4_lock_state
*lsp
, *prev
= NULL
;
2575 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2577 if (!test_bit(LK_STATE_IN_USE
, &state
->flags
))
2580 spin_lock(&state
->state_lock
);
2581 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
2582 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
2583 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
2585 atomic_inc(&lsp
->ls_count
);
2586 spin_unlock(&state
->state_lock
);
2588 nfs4_put_lock_state(prev
);
2591 status
= nfs41_test_and_free_expired_stateid(server
,
2594 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
2595 if (status
== -NFS4ERR_EXPIRED
||
2596 status
== -NFS4ERR_BAD_STATEID
) {
2597 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
2598 lsp
->ls_stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
2599 if (!recover_lost_locks
)
2600 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2601 } else if (status
!= NFS_OK
) {
2603 nfs4_put_lock_state(prev
);
2606 spin_lock(&state
->state_lock
);
2609 spin_unlock(&state
->state_lock
);
2610 nfs4_put_lock_state(prev
);
2616 * nfs41_check_open_stateid - possibly free an open stateid
2618 * @state: NFSv4 state for an inode
2620 * Returns NFS_OK if recovery for this stateid is now finished.
2621 * Otherwise a negative NFS4ERR value is returned.
2623 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2625 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2626 nfs4_stateid
*stateid
= &state
->open_stateid
;
2627 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2630 if (test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0) {
2631 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0) {
2632 if (nfs4_have_delegation(state
->inode
, state
->state
))
2634 return -NFS4ERR_OPENMODE
;
2636 return -NFS4ERR_BAD_STATEID
;
2638 status
= nfs41_test_and_free_expired_stateid(server
, stateid
, cred
);
2639 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2640 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
) {
2641 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2642 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2643 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2644 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2645 stateid
->type
= NFS4_INVALID_STATEID_TYPE
;
2647 if (status
!= NFS_OK
)
2649 if (nfs_open_stateid_recover_openmode(state
))
2650 return -NFS4ERR_OPENMODE
;
2654 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2658 nfs41_check_delegation_stateid(state
);
2659 status
= nfs41_check_expired_locks(state
);
2660 if (status
!= NFS_OK
)
2662 status
= nfs41_check_open_stateid(state
);
2663 if (status
!= NFS_OK
)
2664 status
= nfs4_open_expired(sp
, state
);
2670 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2671 * fields corresponding to attributes that were used to store the verifier.
2672 * Make sure we clobber those fields in the later setattr call
2674 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2675 struct iattr
*sattr
, struct nfs4_label
**label
)
2677 const u32
*attrset
= opendata
->o_res
.attrset
;
2679 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2680 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2681 sattr
->ia_valid
|= ATTR_ATIME
;
2683 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2684 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2685 sattr
->ia_valid
|= ATTR_MTIME
;
2687 /* Except MODE, it seems harmless of setting twice. */
2688 if ((attrset
[1] & FATTR4_WORD1_MODE
))
2689 sattr
->ia_valid
&= ~ATTR_MODE
;
2691 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2695 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2698 struct nfs_open_context
*ctx
)
2700 struct nfs4_state_owner
*sp
= opendata
->owner
;
2701 struct nfs_server
*server
= sp
->so_server
;
2702 struct dentry
*dentry
;
2703 struct nfs4_state
*state
;
2707 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2709 ret
= _nfs4_proc_open(opendata
);
2713 state
= nfs4_opendata_to_nfs4_state(opendata
);
2714 ret
= PTR_ERR(state
);
2717 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2718 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2719 if (opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
)
2720 set_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
);
2722 dentry
= opendata
->dentry
;
2723 if (d_really_is_negative(dentry
)) {
2724 struct dentry
*alias
;
2726 alias
= d_exact_alias(dentry
, state
->inode
);
2728 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2729 /* d_splice_alias() can't fail here - it's a non-directory */
2732 ctx
->dentry
= dentry
= alias
;
2734 nfs_set_verifier(dentry
,
2735 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2738 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2743 if (d_inode(dentry
) == state
->inode
) {
2744 nfs_inode_attach_open_context(ctx
);
2745 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2746 nfs4_schedule_stateid_recovery(server
, state
);
2753 * Returns a referenced nfs4_state
2755 static int _nfs4_do_open(struct inode
*dir
,
2756 struct nfs_open_context
*ctx
,
2758 struct iattr
*sattr
,
2759 struct nfs4_label
*label
,
2762 struct nfs4_state_owner
*sp
;
2763 struct nfs4_state
*state
= NULL
;
2764 struct nfs_server
*server
= NFS_SERVER(dir
);
2765 struct nfs4_opendata
*opendata
;
2766 struct dentry
*dentry
= ctx
->dentry
;
2767 struct rpc_cred
*cred
= ctx
->cred
;
2768 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2769 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2770 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2771 struct nfs4_label
*olabel
= NULL
;
2774 /* Protect against reboot recovery conflicts */
2776 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2778 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2781 status
= nfs4_recover_expired_lease(server
);
2783 goto err_put_state_owner
;
2784 if (d_really_is_positive(dentry
))
2785 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2787 if (d_really_is_positive(dentry
))
2788 claim
= NFS4_OPEN_CLAIM_FH
;
2789 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2790 label
, claim
, GFP_KERNEL
);
2791 if (opendata
== NULL
)
2792 goto err_put_state_owner
;
2795 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2796 if (IS_ERR(olabel
)) {
2797 status
= PTR_ERR(olabel
);
2798 goto err_opendata_put
;
2802 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2803 if (!opendata
->f_attr
.mdsthreshold
) {
2804 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2805 if (!opendata
->f_attr
.mdsthreshold
)
2806 goto err_free_label
;
2808 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2810 if (d_really_is_positive(dentry
))
2811 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2813 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2815 goto err_free_label
;
2818 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2819 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2820 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2822 * send create attributes which was not set by open
2823 * with an extra setattr.
2825 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2826 nfs_fattr_init(opendata
->o_res
.f_attr
);
2827 status
= nfs4_do_setattr(state
->inode
, cred
,
2828 opendata
->o_res
.f_attr
, sattr
,
2829 state
, label
, olabel
);
2831 nfs_setattr_update_inode(state
->inode
, sattr
,
2832 opendata
->o_res
.f_attr
);
2833 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2837 if (opened
&& opendata
->file_created
)
2838 *opened
|= FILE_CREATED
;
2840 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2841 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2842 opendata
->f_attr
.mdsthreshold
= NULL
;
2845 nfs4_label_free(olabel
);
2847 nfs4_opendata_put(opendata
);
2848 nfs4_put_state_owner(sp
);
2851 nfs4_label_free(olabel
);
2853 nfs4_opendata_put(opendata
);
2854 err_put_state_owner
:
2855 nfs4_put_state_owner(sp
);
2861 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2862 struct nfs_open_context
*ctx
,
2864 struct iattr
*sattr
,
2865 struct nfs4_label
*label
,
2868 struct nfs_server
*server
= NFS_SERVER(dir
);
2869 struct nfs4_exception exception
= { };
2870 struct nfs4_state
*res
;
2874 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2876 trace_nfs4_open_file(ctx
, flags
, status
);
2879 /* NOTE: BAD_SEQID means the server and client disagree about the
2880 * book-keeping w.r.t. state-changing operations
2881 * (OPEN/CLOSE/LOCK/LOCKU...)
2882 * It is actually a sign of a bug on the client or on the server.
2884 * If we receive a BAD_SEQID error in the particular case of
2885 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2886 * have unhashed the old state_owner for us, and that we can
2887 * therefore safely retry using a new one. We should still warn
2888 * the user though...
2890 if (status
== -NFS4ERR_BAD_SEQID
) {
2891 pr_warn_ratelimited("NFS: v4 server %s "
2892 " returned a bad sequence-id error!\n",
2893 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2894 exception
.retry
= 1;
2898 * BAD_STATEID on OPEN means that the server cancelled our
2899 * state before it received the OPEN_CONFIRM.
2900 * Recover by retrying the request as per the discussion
2901 * on Page 181 of RFC3530.
2903 if (status
== -NFS4ERR_BAD_STATEID
) {
2904 exception
.retry
= 1;
2907 if (status
== -EAGAIN
) {
2908 /* We must have found a delegation */
2909 exception
.retry
= 1;
2912 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2914 res
= ERR_PTR(nfs4_handle_exception(server
,
2915 status
, &exception
));
2916 } while (exception
.retry
);
2920 static int _nfs4_do_setattr(struct inode
*inode
,
2921 struct nfs_setattrargs
*arg
,
2922 struct nfs_setattrres
*res
,
2923 struct rpc_cred
*cred
,
2924 struct nfs4_state
*state
)
2926 struct nfs_server
*server
= NFS_SERVER(inode
);
2927 struct rpc_message msg
= {
2928 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2933 struct rpc_cred
*delegation_cred
= NULL
;
2934 unsigned long timestamp
= jiffies
;
2939 nfs_fattr_init(res
->fattr
);
2941 /* Servers should only apply open mode checks for file size changes */
2942 truncate
= (arg
->iap
->ia_valid
& ATTR_SIZE
) ? true : false;
2943 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2945 if (nfs4_copy_delegation_stateid(inode
, fmode
, &arg
->stateid
, &delegation_cred
)) {
2946 /* Use that stateid */
2947 } else if (truncate
&& state
!= NULL
) {
2948 struct nfs_lockowner lockowner
= {
2949 .l_owner
= current
->files
,
2951 if (!nfs4_valid_open_stateid(state
))
2953 if (nfs4_select_rw_stateid(state
, FMODE_WRITE
, &lockowner
,
2954 &arg
->stateid
, &delegation_cred
) == -EIO
)
2957 nfs4_stateid_copy(&arg
->stateid
, &zero_stateid
);
2958 if (delegation_cred
)
2959 msg
.rpc_cred
= delegation_cred
;
2961 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
->seq_args
, &res
->seq_res
, 1);
2963 put_rpccred(delegation_cred
);
2964 if (status
== 0 && state
!= NULL
)
2965 renew_lease(server
, timestamp
);
2966 trace_nfs4_setattr(inode
, &arg
->stateid
, status
);
2970 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2971 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2972 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2973 struct nfs4_label
*olabel
)
2975 struct nfs_server
*server
= NFS_SERVER(inode
);
2976 struct nfs_setattrargs arg
= {
2977 .fh
= NFS_FH(inode
),
2980 .bitmask
= server
->attr_bitmask
,
2983 struct nfs_setattrres res
= {
2988 struct nfs4_exception exception
= {
2991 .stateid
= &arg
.stateid
,
2995 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2997 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
3000 err
= _nfs4_do_setattr(inode
, &arg
, &res
, cred
, state
);
3002 case -NFS4ERR_OPENMODE
:
3003 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
3004 pr_warn_once("NFSv4: server %s is incorrectly "
3005 "applying open mode checks to "
3006 "a SETATTR that is not "
3007 "changing file size.\n",
3008 server
->nfs_client
->cl_hostname
);
3010 if (state
&& !(state
->state
& FMODE_WRITE
)) {
3012 if (sattr
->ia_valid
& ATTR_OPEN
)
3017 err
= nfs4_handle_exception(server
, err
, &exception
);
3018 } while (exception
.retry
);
3024 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
3026 if (inode
== NULL
|| !nfs_have_layout(inode
))
3029 return pnfs_wait_on_layoutreturn(inode
, task
);
3032 struct nfs4_closedata
{
3033 struct inode
*inode
;
3034 struct nfs4_state
*state
;
3035 struct nfs_closeargs arg
;
3036 struct nfs_closeres res
;
3038 struct nfs4_layoutreturn_args arg
;
3039 struct nfs4_layoutreturn_res res
;
3043 struct nfs_fattr fattr
;
3044 unsigned long timestamp
;
3047 static void nfs4_free_closedata(void *data
)
3049 struct nfs4_closedata
*calldata
= data
;
3050 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
3051 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
3053 if (calldata
->lr
.roc
)
3054 pnfs_roc_release(&calldata
->lr
.arg
, &calldata
->lr
.res
,
3055 calldata
->res
.lr_ret
);
3056 nfs4_put_open_state(calldata
->state
);
3057 nfs_free_seqid(calldata
->arg
.seqid
);
3058 nfs4_put_state_owner(sp
);
3059 nfs_sb_deactive(sb
);
3063 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
3065 struct nfs4_closedata
*calldata
= data
;
3066 struct nfs4_state
*state
= calldata
->state
;
3067 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
3068 nfs4_stateid
*res_stateid
= NULL
;
3070 dprintk("%s: begin!\n", __func__
);
3071 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
3073 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
3075 /* Handle Layoutreturn errors */
3076 if (calldata
->arg
.lr_args
&& task
->tk_status
!= 0) {
3077 switch (calldata
->res
.lr_ret
) {
3079 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3082 calldata
->arg
.lr_args
= NULL
;
3083 calldata
->res
.lr_res
= NULL
;
3085 case -NFS4ERR_ADMIN_REVOKED
:
3086 case -NFS4ERR_DELEG_REVOKED
:
3087 case -NFS4ERR_EXPIRED
:
3088 case -NFS4ERR_BAD_STATEID
:
3089 case -NFS4ERR_OLD_STATEID
:
3090 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
3091 case -NFS4ERR_WRONG_CRED
:
3092 calldata
->arg
.lr_args
= NULL
;
3093 calldata
->res
.lr_res
= NULL
;
3094 calldata
->res
.lr_ret
= 0;
3095 rpc_restart_call_prepare(task
);
3100 /* hmm. we are done with the inode, and in the process of freeing
3101 * the state_owner. we keep this around to process errors
3103 switch (task
->tk_status
) {
3105 res_stateid
= &calldata
->res
.stateid
;
3106 renew_lease(server
, calldata
->timestamp
);
3108 case -NFS4ERR_ADMIN_REVOKED
:
3109 case -NFS4ERR_STALE_STATEID
:
3110 case -NFS4ERR_EXPIRED
:
3111 nfs4_free_revoked_stateid(server
,
3112 &calldata
->arg
.stateid
,
3113 task
->tk_msg
.rpc_cred
);
3114 case -NFS4ERR_OLD_STATEID
:
3115 case -NFS4ERR_BAD_STATEID
:
3116 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
3117 &state
->open_stateid
)) {
3118 rpc_restart_call_prepare(task
);
3121 if (calldata
->arg
.fmode
== 0)
3124 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
3125 rpc_restart_call_prepare(task
);
3129 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
3130 res_stateid
, calldata
->arg
.fmode
);
3132 nfs_release_seqid(calldata
->arg
.seqid
);
3133 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
3134 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
3137 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
3139 struct nfs4_closedata
*calldata
= data
;
3140 struct nfs4_state
*state
= calldata
->state
;
3141 struct inode
*inode
= calldata
->inode
;
3142 bool is_rdonly
, is_wronly
, is_rdwr
;
3145 dprintk("%s: begin!\n", __func__
);
3146 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3149 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
3150 spin_lock(&state
->owner
->so_lock
);
3151 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
3152 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
3153 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
3154 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
3155 /* Calculate the change in open mode */
3156 calldata
->arg
.fmode
= 0;
3157 if (state
->n_rdwr
== 0) {
3158 if (state
->n_rdonly
== 0)
3159 call_close
|= is_rdonly
;
3161 calldata
->arg
.fmode
|= FMODE_READ
;
3162 if (state
->n_wronly
== 0)
3163 call_close
|= is_wronly
;
3165 calldata
->arg
.fmode
|= FMODE_WRITE
;
3166 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
3167 call_close
|= is_rdwr
;
3169 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
3171 if (!nfs4_valid_open_stateid(state
) ||
3172 test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
3174 spin_unlock(&state
->owner
->so_lock
);
3177 /* Note: exit _without_ calling nfs4_close_done */
3181 if (!calldata
->lr
.roc
&& nfs4_wait_on_layoutreturn(inode
, task
)) {
3182 nfs_release_seqid(calldata
->arg
.seqid
);
3186 if (calldata
->arg
.fmode
== 0) {
3187 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
3189 /* Close-to-open cache consistency revalidation */
3190 if (!nfs4_have_delegation(inode
, FMODE_READ
))
3191 calldata
->arg
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
3193 calldata
->arg
.bitmask
= NULL
;
3196 calldata
->arg
.share_access
=
3197 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
3198 calldata
->arg
.fmode
, 0);
3200 nfs_fattr_init(calldata
->res
.fattr
);
3201 calldata
->timestamp
= jiffies
;
3202 if (nfs4_setup_sequence(NFS_SERVER(inode
),
3203 &calldata
->arg
.seq_args
,
3204 &calldata
->res
.seq_res
,
3206 nfs_release_seqid(calldata
->arg
.seqid
);
3207 dprintk("%s: done!\n", __func__
);
3210 task
->tk_action
= NULL
;
3212 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
3215 static const struct rpc_call_ops nfs4_close_ops
= {
3216 .rpc_call_prepare
= nfs4_close_prepare
,
3217 .rpc_call_done
= nfs4_close_done
,
3218 .rpc_release
= nfs4_free_closedata
,
3222 * It is possible for data to be read/written from a mem-mapped file
3223 * after the sys_close call (which hits the vfs layer as a flush).
3224 * This means that we can't safely call nfsv4 close on a file until
3225 * the inode is cleared. This in turn means that we are not good
3226 * NFSv4 citizens - we do not indicate to the server to update the file's
3227 * share state even when we are done with one of the three share
3228 * stateid's in the inode.
3230 * NOTE: Caller must be holding the sp->so_owner semaphore!
3232 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
3234 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3235 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
3236 struct nfs4_closedata
*calldata
;
3237 struct nfs4_state_owner
*sp
= state
->owner
;
3238 struct rpc_task
*task
;
3239 struct rpc_message msg
= {
3240 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
3241 .rpc_cred
= state
->owner
->so_cred
,
3243 struct rpc_task_setup task_setup_data
= {
3244 .rpc_client
= server
->client
,
3245 .rpc_message
= &msg
,
3246 .callback_ops
= &nfs4_close_ops
,
3247 .workqueue
= nfsiod_workqueue
,
3248 .flags
= RPC_TASK_ASYNC
,
3250 int status
= -ENOMEM
;
3252 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
3253 &task_setup_data
.rpc_client
, &msg
);
3255 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
3256 if (calldata
== NULL
)
3258 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
3259 calldata
->inode
= state
->inode
;
3260 calldata
->state
= state
;
3261 calldata
->arg
.fh
= NFS_FH(state
->inode
);
3262 /* Serialization for the sequence id */
3263 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
3264 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
3265 if (IS_ERR(calldata
->arg
.seqid
))
3266 goto out_free_calldata
;
3267 calldata
->arg
.fmode
= 0;
3268 calldata
->res
.fattr
= &calldata
->fattr
;
3269 calldata
->res
.seqid
= calldata
->arg
.seqid
;
3270 calldata
->res
.server
= server
;
3271 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3272 calldata
->lr
.roc
= pnfs_roc(state
->inode
,
3273 &calldata
->lr
.arg
, &calldata
->lr
.res
, msg
.rpc_cred
);
3274 if (calldata
->lr
.roc
) {
3275 calldata
->arg
.lr_args
= &calldata
->lr
.arg
;
3276 calldata
->res
.lr_res
= &calldata
->lr
.res
;
3278 nfs_sb_active(calldata
->inode
->i_sb
);
3280 msg
.rpc_argp
= &calldata
->arg
;
3281 msg
.rpc_resp
= &calldata
->res
;
3282 task_setup_data
.callback_data
= calldata
;
3283 task
= rpc_run_task(&task_setup_data
);
3285 return PTR_ERR(task
);
3288 status
= rpc_wait_for_completion_task(task
);
3294 nfs4_put_open_state(state
);
3295 nfs4_put_state_owner(sp
);
3299 static struct inode
*
3300 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3301 int open_flags
, struct iattr
*attr
, int *opened
)
3303 struct nfs4_state
*state
;
3304 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3306 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3308 /* Protect against concurrent sillydeletes */
3309 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3311 nfs4_label_release_security(label
);
3314 return ERR_CAST(state
);
3315 return state
->inode
;
3318 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3320 if (ctx
->state
== NULL
)
3323 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3325 nfs4_close_state(ctx
->state
, ctx
->mode
);
3328 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3329 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3330 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3332 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3334 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3335 struct nfs4_server_caps_arg args
= {
3339 struct nfs4_server_caps_res res
= {};
3340 struct rpc_message msg
= {
3341 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3347 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3348 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3349 FATTR4_WORD0_LINK_SUPPORT
|
3350 FATTR4_WORD0_SYMLINK_SUPPORT
|
3351 FATTR4_WORD0_ACLSUPPORT
;
3353 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3355 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3357 /* Sanity check the server answers */
3358 switch (minorversion
) {
3360 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3361 res
.attr_bitmask
[2] = 0;
3364 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3367 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3369 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3370 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3371 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3372 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3373 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3374 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3375 NFS_CAP_SECURITY_LABEL
);
3376 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3377 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3378 server
->caps
|= NFS_CAP_ACLS
;
3379 if (res
.has_links
!= 0)
3380 server
->caps
|= NFS_CAP_HARDLINKS
;
3381 if (res
.has_symlinks
!= 0)
3382 server
->caps
|= NFS_CAP_SYMLINKS
;
3383 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3384 server
->caps
|= NFS_CAP_FILEID
;
3385 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3386 server
->caps
|= NFS_CAP_MODE
;
3387 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3388 server
->caps
|= NFS_CAP_NLINK
;
3389 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3390 server
->caps
|= NFS_CAP_OWNER
;
3391 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3392 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3393 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3394 server
->caps
|= NFS_CAP_ATIME
;
3395 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3396 server
->caps
|= NFS_CAP_CTIME
;
3397 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3398 server
->caps
|= NFS_CAP_MTIME
;
3399 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3400 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3401 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3403 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3404 sizeof(server
->attr_bitmask
));
3405 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3407 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3408 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3409 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3410 server
->cache_consistency_bitmask
[2] = 0;
3411 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3412 sizeof(server
->exclcreat_bitmask
));
3413 server
->acl_bitmask
= res
.acl_bitmask
;
3414 server
->fh_expire_type
= res
.fh_expire_type
;
3420 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3422 struct nfs4_exception exception
= { };
3425 err
= nfs4_handle_exception(server
,
3426 _nfs4_server_capabilities(server
, fhandle
),
3428 } while (exception
.retry
);
3432 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3433 struct nfs_fsinfo
*info
)
3436 struct nfs4_lookup_root_arg args
= {
3439 struct nfs4_lookup_res res
= {
3441 .fattr
= info
->fattr
,
3444 struct rpc_message msg
= {
3445 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3450 bitmask
[0] = nfs4_fattr_bitmap
[0];
3451 bitmask
[1] = nfs4_fattr_bitmap
[1];
3453 * Process the label in the upcoming getfattr
3455 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3457 nfs_fattr_init(info
->fattr
);
3458 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3461 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3462 struct nfs_fsinfo
*info
)
3464 struct nfs4_exception exception
= { };
3467 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3468 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3471 case -NFS4ERR_WRONGSEC
:
3474 err
= nfs4_handle_exception(server
, err
, &exception
);
3476 } while (exception
.retry
);
3481 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3482 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3484 struct rpc_auth_create_args auth_args
= {
3485 .pseudoflavor
= flavor
,
3487 struct rpc_auth
*auth
;
3490 auth
= rpcauth_create(&auth_args
, server
->client
);
3495 ret
= nfs4_lookup_root(server
, fhandle
, info
);
3501 * Retry pseudoroot lookup with various security flavors. We do this when:
3503 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3504 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3506 * Returns zero on success, or a negative NFS4ERR value, or a
3507 * negative errno value.
3509 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3510 struct nfs_fsinfo
*info
)
3512 /* Per 3530bis 15.33.5 */
3513 static const rpc_authflavor_t flav_array
[] = {
3517 RPC_AUTH_UNIX
, /* courtesy */
3520 int status
= -EPERM
;
3523 if (server
->auth_info
.flavor_len
> 0) {
3524 /* try each flavor specified by user */
3525 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3526 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3527 server
->auth_info
.flavors
[i
]);
3528 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3533 /* no flavors specified by user, try default list */
3534 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3535 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3537 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3544 * -EACCESS could mean that the user doesn't have correct permissions
3545 * to access the mount. It could also mean that we tried to mount
3546 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3547 * existing mount programs don't handle -EACCES very well so it should
3548 * be mapped to -EPERM instead.
3550 if (status
== -EACCES
)
3556 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3557 * @server: initialized nfs_server handle
3558 * @fhandle: we fill in the pseudo-fs root file handle
3559 * @info: we fill in an FSINFO struct
3560 * @auth_probe: probe the auth flavours
3562 * Returns zero on success, or a negative errno.
3564 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3565 struct nfs_fsinfo
*info
,
3571 status
= nfs4_lookup_root(server
, fhandle
, info
);
3573 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3574 status
= server
->nfs_client
->cl_mvops
->find_root_sec(server
,
3578 status
= nfs4_server_capabilities(server
, fhandle
);
3580 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3582 return nfs4_map_errors(status
);
3585 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3586 struct nfs_fsinfo
*info
)
3589 struct nfs_fattr
*fattr
= info
->fattr
;
3590 struct nfs4_label
*label
= NULL
;
3592 error
= nfs4_server_capabilities(server
, mntfh
);
3594 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3598 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3600 return PTR_ERR(label
);
3602 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3604 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3605 goto err_free_label
;
3608 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3609 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3610 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3613 nfs4_label_free(label
);
3619 * Get locations and (maybe) other attributes of a referral.
3620 * Note that we'll actually follow the referral later when
3621 * we detect fsid mismatch in inode revalidation
3623 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3624 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3625 struct nfs_fh
*fhandle
)
3627 int status
= -ENOMEM
;
3628 struct page
*page
= NULL
;
3629 struct nfs4_fs_locations
*locations
= NULL
;
3631 page
= alloc_page(GFP_KERNEL
);
3634 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3635 if (locations
== NULL
)
3638 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3643 * If the fsid didn't change, this is a migration event, not a
3644 * referral. Cause us to drop into the exception handler, which
3645 * will kick off migration recovery.
3647 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3648 dprintk("%s: server did not return a different fsid for"
3649 " a referral at %s\n", __func__
, name
->name
);
3650 status
= -NFS4ERR_MOVED
;
3653 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3654 nfs_fixup_referral_attributes(&locations
->fattr
);
3656 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3657 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3658 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3666 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3667 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3669 struct nfs4_getattr_arg args
= {
3671 .bitmask
= server
->attr_bitmask
,
3673 struct nfs4_getattr_res res
= {
3678 struct rpc_message msg
= {
3679 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3684 args
.bitmask
= nfs4_bitmask(server
, label
);
3686 nfs_fattr_init(fattr
);
3687 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3690 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3691 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3693 struct nfs4_exception exception
= { };
3696 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3697 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3698 err
= nfs4_handle_exception(server
, err
,
3700 } while (exception
.retry
);
3705 * The file is not closed if it is opened due to the a request to change
3706 * the size of the file. The open call will not be needed once the
3707 * VFS layer lookup-intents are implemented.
3709 * Close is called when the inode is destroyed.
3710 * If we haven't opened the file for O_WRONLY, we
3711 * need to in the size_change case to obtain a stateid.
3714 * Because OPEN is always done by name in nfsv4, it is
3715 * possible that we opened a different file by the same
3716 * name. We can recognize this race condition, but we
3717 * can't do anything about it besides returning an error.
3719 * This will be fixed with VFS changes (lookup-intent).
3722 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3723 struct iattr
*sattr
)
3725 struct inode
*inode
= d_inode(dentry
);
3726 struct rpc_cred
*cred
= NULL
;
3727 struct nfs4_state
*state
= NULL
;
3728 struct nfs4_label
*label
= NULL
;
3731 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3732 sattr
->ia_valid
& ATTR_SIZE
&&
3733 sattr
->ia_size
< i_size_read(inode
))
3734 pnfs_commit_and_return_layout(inode
);
3736 nfs_fattr_init(fattr
);
3738 /* Deal with open(O_TRUNC) */
3739 if (sattr
->ia_valid
& ATTR_OPEN
)
3740 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3742 /* Optimization: if the end result is no change, don't RPC */
3743 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3746 /* Search for an existing open(O_WRITE) file */
3747 if (sattr
->ia_valid
& ATTR_FILE
) {
3748 struct nfs_open_context
*ctx
;
3750 ctx
= nfs_file_open_context(sattr
->ia_file
);
3757 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3759 return PTR_ERR(label
);
3761 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
3763 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3764 nfs_setsecurity(inode
, fattr
, label
);
3766 nfs4_label_free(label
);
3770 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3771 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3772 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3774 struct nfs_server
*server
= NFS_SERVER(dir
);
3776 struct nfs4_lookup_arg args
= {
3777 .bitmask
= server
->attr_bitmask
,
3778 .dir_fh
= NFS_FH(dir
),
3781 struct nfs4_lookup_res res
= {
3787 struct rpc_message msg
= {
3788 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3793 args
.bitmask
= nfs4_bitmask(server
, label
);
3795 nfs_fattr_init(fattr
);
3797 dprintk("NFS call lookup %s\n", name
->name
);
3798 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3799 dprintk("NFS reply lookup: %d\n", status
);
3803 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3805 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3806 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3807 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3811 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3812 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3813 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3815 struct nfs4_exception exception
= { };
3816 struct rpc_clnt
*client
= *clnt
;
3819 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3820 trace_nfs4_lookup(dir
, name
, err
);
3822 case -NFS4ERR_BADNAME
:
3825 case -NFS4ERR_MOVED
:
3826 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3827 if (err
== -NFS4ERR_MOVED
)
3828 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3830 case -NFS4ERR_WRONGSEC
:
3832 if (client
!= *clnt
)
3834 client
= nfs4_negotiate_security(client
, dir
, name
);
3836 return PTR_ERR(client
);
3838 exception
.retry
= 1;
3841 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3843 } while (exception
.retry
);
3848 else if (client
!= *clnt
)
3849 rpc_shutdown_client(client
);
3854 static int nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
3855 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3856 struct nfs4_label
*label
)
3859 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3861 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3862 if (client
!= NFS_CLIENT(dir
)) {
3863 rpc_shutdown_client(client
);
3864 nfs_fixup_secinfo_attributes(fattr
);
3870 nfs4_proc_lookup_mountpoint(struct inode
*dir
, const struct qstr
*name
,
3871 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3873 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3876 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3878 return ERR_PTR(status
);
3879 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3882 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3884 struct nfs_server
*server
= NFS_SERVER(inode
);
3885 struct nfs4_accessargs args
= {
3886 .fh
= NFS_FH(inode
),
3887 .bitmask
= server
->cache_consistency_bitmask
,
3889 struct nfs4_accessres res
= {
3892 struct rpc_message msg
= {
3893 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3896 .rpc_cred
= entry
->cred
,
3898 int mode
= entry
->mask
;
3902 * Determine which access bits we want to ask for...
3904 if (mode
& MAY_READ
)
3905 args
.access
|= NFS4_ACCESS_READ
;
3906 if (S_ISDIR(inode
->i_mode
)) {
3907 if (mode
& MAY_WRITE
)
3908 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3909 if (mode
& MAY_EXEC
)
3910 args
.access
|= NFS4_ACCESS_LOOKUP
;
3912 if (mode
& MAY_WRITE
)
3913 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3914 if (mode
& MAY_EXEC
)
3915 args
.access
|= NFS4_ACCESS_EXECUTE
;
3918 res
.fattr
= nfs_alloc_fattr();
3919 if (res
.fattr
== NULL
)
3922 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3924 nfs_access_set_mask(entry
, res
.access
);
3925 nfs_refresh_inode(inode
, res
.fattr
);
3927 nfs_free_fattr(res
.fattr
);
3931 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3933 struct nfs4_exception exception
= { };
3936 err
= _nfs4_proc_access(inode
, entry
);
3937 trace_nfs4_access(inode
, err
);
3938 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3940 } while (exception
.retry
);
3945 * TODO: For the time being, we don't try to get any attributes
3946 * along with any of the zero-copy operations READ, READDIR,
3949 * In the case of the first three, we want to put the GETATTR
3950 * after the read-type operation -- this is because it is hard
3951 * to predict the length of a GETATTR response in v4, and thus
3952 * align the READ data correctly. This means that the GETATTR
3953 * may end up partially falling into the page cache, and we should
3954 * shift it into the 'tail' of the xdr_buf before processing.
3955 * To do this efficiently, we need to know the total length
3956 * of data received, which doesn't seem to be available outside
3959 * In the case of WRITE, we also want to put the GETATTR after
3960 * the operation -- in this case because we want to make sure
3961 * we get the post-operation mtime and size.
3963 * Both of these changes to the XDR layer would in fact be quite
3964 * minor, but I decided to leave them for a subsequent patch.
3966 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3967 unsigned int pgbase
, unsigned int pglen
)
3969 struct nfs4_readlink args
= {
3970 .fh
= NFS_FH(inode
),
3975 struct nfs4_readlink_res res
;
3976 struct rpc_message msg
= {
3977 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3982 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3985 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3986 unsigned int pgbase
, unsigned int pglen
)
3988 struct nfs4_exception exception
= { };
3991 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3992 trace_nfs4_readlink(inode
, err
);
3993 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3995 } while (exception
.retry
);
4000 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4003 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
4006 struct nfs4_label l
, *ilabel
= NULL
;
4007 struct nfs_open_context
*ctx
;
4008 struct nfs4_state
*state
;
4011 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
, NULL
);
4013 return PTR_ERR(ctx
);
4015 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4017 sattr
->ia_mode
&= ~current_umask();
4018 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
4019 if (IS_ERR(state
)) {
4020 status
= PTR_ERR(state
);
4024 nfs4_label_release_security(ilabel
);
4025 put_nfs_open_context(ctx
);
4029 static int _nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4031 struct nfs_server
*server
= NFS_SERVER(dir
);
4032 struct nfs_removeargs args
= {
4036 struct nfs_removeres res
= {
4039 struct rpc_message msg
= {
4040 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
4046 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
4048 update_changeattr(dir
, &res
.cinfo
);
4052 static int nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4054 struct nfs4_exception exception
= { };
4057 err
= _nfs4_proc_remove(dir
, name
);
4058 trace_nfs4_remove(dir
, name
, err
);
4059 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4061 } while (exception
.retry
);
4065 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
4067 struct nfs_server
*server
= NFS_SERVER(dir
);
4068 struct nfs_removeargs
*args
= msg
->rpc_argp
;
4069 struct nfs_removeres
*res
= msg
->rpc_resp
;
4071 res
->server
= server
;
4072 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
4073 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
4075 nfs_fattr_init(res
->dir_attr
);
4078 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
4080 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
),
4081 &data
->args
.seq_args
,
4086 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
4088 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
4089 struct nfs_removeres
*res
= &data
->res
;
4091 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4093 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
4094 &data
->timeout
) == -EAGAIN
)
4096 update_changeattr(dir
, &res
->cinfo
);
4100 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
4102 struct nfs_server
*server
= NFS_SERVER(dir
);
4103 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
4104 struct nfs_renameres
*res
= msg
->rpc_resp
;
4106 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
4107 res
->server
= server
;
4108 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
4111 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
4113 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
4114 &data
->args
.seq_args
,
4119 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
4120 struct inode
*new_dir
)
4122 struct nfs_renamedata
*data
= task
->tk_calldata
;
4123 struct nfs_renameres
*res
= &data
->res
;
4125 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4127 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
4130 update_changeattr(old_dir
, &res
->old_cinfo
);
4131 update_changeattr(new_dir
, &res
->new_cinfo
);
4135 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4137 struct nfs_server
*server
= NFS_SERVER(inode
);
4138 struct nfs4_link_arg arg
= {
4139 .fh
= NFS_FH(inode
),
4140 .dir_fh
= NFS_FH(dir
),
4142 .bitmask
= server
->attr_bitmask
,
4144 struct nfs4_link_res res
= {
4148 struct rpc_message msg
= {
4149 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
4153 int status
= -ENOMEM
;
4155 res
.fattr
= nfs_alloc_fattr();
4156 if (res
.fattr
== NULL
)
4159 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4160 if (IS_ERR(res
.label
)) {
4161 status
= PTR_ERR(res
.label
);
4164 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
4166 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4168 update_changeattr(dir
, &res
.cinfo
);
4169 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
4171 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
4175 nfs4_label_free(res
.label
);
4178 nfs_free_fattr(res
.fattr
);
4182 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4184 struct nfs4_exception exception
= { };
4187 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4188 _nfs4_proc_link(inode
, dir
, name
),
4190 } while (exception
.retry
);
4194 struct nfs4_createdata
{
4195 struct rpc_message msg
;
4196 struct nfs4_create_arg arg
;
4197 struct nfs4_create_res res
;
4199 struct nfs_fattr fattr
;
4200 struct nfs4_label
*label
;
4203 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
4204 const struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
4206 struct nfs4_createdata
*data
;
4208 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4210 struct nfs_server
*server
= NFS_SERVER(dir
);
4212 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4213 if (IS_ERR(data
->label
))
4216 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
4217 data
->msg
.rpc_argp
= &data
->arg
;
4218 data
->msg
.rpc_resp
= &data
->res
;
4219 data
->arg
.dir_fh
= NFS_FH(dir
);
4220 data
->arg
.server
= server
;
4221 data
->arg
.name
= name
;
4222 data
->arg
.attrs
= sattr
;
4223 data
->arg
.ftype
= ftype
;
4224 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
4225 data
->res
.server
= server
;
4226 data
->res
.fh
= &data
->fh
;
4227 data
->res
.fattr
= &data
->fattr
;
4228 data
->res
.label
= data
->label
;
4229 nfs_fattr_init(data
->res
.fattr
);
4237 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
4239 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
4240 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4242 update_changeattr(dir
, &data
->res
.dir_cinfo
);
4243 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
4248 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
4250 nfs4_label_free(data
->label
);
4254 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4255 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
4256 struct nfs4_label
*label
)
4258 struct nfs4_createdata
*data
;
4259 int status
= -ENAMETOOLONG
;
4261 if (len
> NFS4_MAXPATHLEN
)
4265 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
4269 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
4270 data
->arg
.u
.symlink
.pages
= &page
;
4271 data
->arg
.u
.symlink
.len
= len
;
4272 data
->arg
.label
= label
;
4274 status
= nfs4_do_create(dir
, dentry
, data
);
4276 nfs4_free_createdata(data
);
4281 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4282 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
4284 struct nfs4_exception exception
= { };
4285 struct nfs4_label l
, *label
= NULL
;
4288 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4291 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4292 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4293 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4295 } while (exception
.retry
);
4297 nfs4_label_release_security(label
);
4301 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4302 struct iattr
*sattr
, struct nfs4_label
*label
)
4304 struct nfs4_createdata
*data
;
4305 int status
= -ENOMEM
;
4307 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4311 data
->arg
.label
= label
;
4312 status
= nfs4_do_create(dir
, dentry
, data
);
4314 nfs4_free_createdata(data
);
4319 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4320 struct iattr
*sattr
)
4322 struct nfs4_exception exception
= { };
4323 struct nfs4_label l
, *label
= NULL
;
4326 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4328 sattr
->ia_mode
&= ~current_umask();
4330 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4331 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4332 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4334 } while (exception
.retry
);
4335 nfs4_label_release_security(label
);
4340 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4341 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4343 struct inode
*dir
= d_inode(dentry
);
4344 struct nfs4_readdir_arg args
= {
4349 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4352 struct nfs4_readdir_res res
;
4353 struct rpc_message msg
= {
4354 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4361 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4363 (unsigned long long)cookie
);
4364 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4365 res
.pgbase
= args
.pgbase
;
4366 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4368 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4369 status
+= args
.pgbase
;
4372 nfs_invalidate_atime(dir
);
4374 dprintk("%s: returns %d\n", __func__
, status
);
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 nfs4_exception exception
= { };
4384 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4385 pages
, count
, plus
);
4386 trace_nfs4_readdir(d_inode(dentry
), err
);
4387 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4389 } while (exception
.retry
);
4393 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4394 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4396 struct nfs4_createdata
*data
;
4397 int mode
= sattr
->ia_mode
;
4398 int status
= -ENOMEM
;
4400 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4405 data
->arg
.ftype
= NF4FIFO
;
4406 else if (S_ISBLK(mode
)) {
4407 data
->arg
.ftype
= NF4BLK
;
4408 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4409 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4411 else if (S_ISCHR(mode
)) {
4412 data
->arg
.ftype
= NF4CHR
;
4413 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4414 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4415 } else if (!S_ISSOCK(mode
)) {
4420 data
->arg
.label
= label
;
4421 status
= nfs4_do_create(dir
, dentry
, data
);
4423 nfs4_free_createdata(data
);
4428 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4429 struct iattr
*sattr
, dev_t rdev
)
4431 struct nfs4_exception exception
= { };
4432 struct nfs4_label l
, *label
= NULL
;
4435 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4437 sattr
->ia_mode
&= ~current_umask();
4439 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4440 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4441 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4443 } while (exception
.retry
);
4445 nfs4_label_release_security(label
);
4450 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4451 struct nfs_fsstat
*fsstat
)
4453 struct nfs4_statfs_arg args
= {
4455 .bitmask
= server
->attr_bitmask
,
4457 struct nfs4_statfs_res res
= {
4460 struct rpc_message msg
= {
4461 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4466 nfs_fattr_init(fsstat
->fattr
);
4467 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4470 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4472 struct nfs4_exception exception
= { };
4475 err
= nfs4_handle_exception(server
,
4476 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4478 } while (exception
.retry
);
4482 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4483 struct nfs_fsinfo
*fsinfo
)
4485 struct nfs4_fsinfo_arg args
= {
4487 .bitmask
= server
->attr_bitmask
,
4489 struct nfs4_fsinfo_res res
= {
4492 struct rpc_message msg
= {
4493 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4498 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4501 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4503 struct nfs4_exception exception
= { };
4504 unsigned long now
= jiffies
;
4508 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4509 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4511 nfs4_set_lease_period(server
->nfs_client
,
4512 fsinfo
->lease_time
* HZ
,
4516 err
= nfs4_handle_exception(server
, err
, &exception
);
4517 } while (exception
.retry
);
4521 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4525 nfs_fattr_init(fsinfo
->fattr
);
4526 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4528 /* block layout checks this! */
4529 server
->pnfs_blksize
= fsinfo
->blksize
;
4530 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
);
4536 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4537 struct nfs_pathconf
*pathconf
)
4539 struct nfs4_pathconf_arg args
= {
4541 .bitmask
= server
->attr_bitmask
,
4543 struct nfs4_pathconf_res res
= {
4544 .pathconf
= pathconf
,
4546 struct rpc_message msg
= {
4547 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4552 /* None of the pathconf attributes are mandatory to implement */
4553 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4554 memset(pathconf
, 0, sizeof(*pathconf
));
4558 nfs_fattr_init(pathconf
->fattr
);
4559 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4562 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4563 struct nfs_pathconf
*pathconf
)
4565 struct nfs4_exception exception
= { };
4569 err
= nfs4_handle_exception(server
,
4570 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4572 } while (exception
.retry
);
4576 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4577 const struct nfs_open_context
*ctx
,
4578 const struct nfs_lock_context
*l_ctx
,
4581 const struct nfs_lockowner
*lockowner
= NULL
;
4584 lockowner
= &l_ctx
->lockowner
;
4585 return nfs4_select_rw_stateid(ctx
->state
, fmode
, lockowner
, stateid
, NULL
);
4587 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4589 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4590 const struct nfs_open_context
*ctx
,
4591 const struct nfs_lock_context
*l_ctx
,
4594 nfs4_stateid current_stateid
;
4596 /* If the current stateid represents a lost lock, then exit */
4597 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4599 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4602 static bool nfs4_error_stateid_expired(int err
)
4605 case -NFS4ERR_DELEG_REVOKED
:
4606 case -NFS4ERR_ADMIN_REVOKED
:
4607 case -NFS4ERR_BAD_STATEID
:
4608 case -NFS4ERR_STALE_STATEID
:
4609 case -NFS4ERR_OLD_STATEID
:
4610 case -NFS4ERR_OPENMODE
:
4611 case -NFS4ERR_EXPIRED
:
4617 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4619 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4621 trace_nfs4_read(hdr
, task
->tk_status
);
4622 if (task
->tk_status
< 0) {
4623 struct nfs4_exception exception
= {
4624 .inode
= hdr
->inode
,
4625 .state
= hdr
->args
.context
->state
,
4626 .stateid
= &hdr
->args
.stateid
,
4628 task
->tk_status
= nfs4_async_handle_exception(task
,
4629 server
, task
->tk_status
, &exception
);
4630 if (exception
.retry
) {
4631 rpc_restart_call_prepare(task
);
4636 if (task
->tk_status
> 0)
4637 renew_lease(server
, hdr
->timestamp
);
4641 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4642 struct nfs_pgio_args
*args
)
4645 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4646 nfs4_stateid_is_current(&args
->stateid
,
4651 rpc_restart_call_prepare(task
);
4655 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4658 dprintk("--> %s\n", __func__
);
4660 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4662 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4664 if (task
->tk_status
> 0)
4665 nfs_invalidate_atime(hdr
->inode
);
4666 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4667 nfs4_read_done_cb(task
, hdr
);
4670 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4671 struct rpc_message
*msg
)
4673 hdr
->timestamp
= jiffies
;
4674 if (!hdr
->pgio_done_cb
)
4675 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4676 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4677 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4680 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4681 struct nfs_pgio_header
*hdr
)
4683 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
),
4684 &hdr
->args
.seq_args
,
4688 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4689 hdr
->args
.lock_context
,
4690 hdr
->rw_ops
->rw_mode
) == -EIO
)
4692 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4697 static int nfs4_write_done_cb(struct rpc_task
*task
,
4698 struct nfs_pgio_header
*hdr
)
4700 struct inode
*inode
= hdr
->inode
;
4702 trace_nfs4_write(hdr
, task
->tk_status
);
4703 if (task
->tk_status
< 0) {
4704 struct nfs4_exception exception
= {
4705 .inode
= hdr
->inode
,
4706 .state
= hdr
->args
.context
->state
,
4707 .stateid
= &hdr
->args
.stateid
,
4709 task
->tk_status
= nfs4_async_handle_exception(task
,
4710 NFS_SERVER(inode
), task
->tk_status
,
4712 if (exception
.retry
) {
4713 rpc_restart_call_prepare(task
);
4717 if (task
->tk_status
>= 0) {
4718 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4719 nfs_writeback_update_inode(hdr
);
4724 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4725 struct nfs_pgio_args
*args
)
4728 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4729 nfs4_stateid_is_current(&args
->stateid
,
4734 rpc_restart_call_prepare(task
);
4738 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4740 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4742 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4744 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4745 nfs4_write_done_cb(task
, hdr
);
4749 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4751 /* Don't request attributes for pNFS or O_DIRECT writes */
4752 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4754 /* Otherwise, request attributes if and only if we don't hold
4757 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4760 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4761 struct rpc_message
*msg
)
4763 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4765 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4766 hdr
->args
.bitmask
= NULL
;
4767 hdr
->res
.fattr
= NULL
;
4769 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4771 if (!hdr
->pgio_done_cb
)
4772 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4773 hdr
->res
.server
= server
;
4774 hdr
->timestamp
= jiffies
;
4776 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4777 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4780 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4782 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4783 &data
->args
.seq_args
,
4788 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4790 struct inode
*inode
= data
->inode
;
4792 trace_nfs4_commit(data
, task
->tk_status
);
4793 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4794 NULL
, NULL
) == -EAGAIN
) {
4795 rpc_restart_call_prepare(task
);
4801 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4803 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4805 return data
->commit_done_cb(task
, data
);
4808 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4810 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4812 if (data
->commit_done_cb
== NULL
)
4813 data
->commit_done_cb
= nfs4_commit_done_cb
;
4814 data
->res
.server
= server
;
4815 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4816 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4819 struct nfs4_renewdata
{
4820 struct nfs_client
*client
;
4821 unsigned long timestamp
;
4825 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4826 * standalone procedure for queueing an asynchronous RENEW.
4828 static void nfs4_renew_release(void *calldata
)
4830 struct nfs4_renewdata
*data
= calldata
;
4831 struct nfs_client
*clp
= data
->client
;
4833 if (atomic_read(&clp
->cl_count
) > 1)
4834 nfs4_schedule_state_renewal(clp
);
4835 nfs_put_client(clp
);
4839 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4841 struct nfs4_renewdata
*data
= calldata
;
4842 struct nfs_client
*clp
= data
->client
;
4843 unsigned long timestamp
= data
->timestamp
;
4845 trace_nfs4_renew_async(clp
, task
->tk_status
);
4846 switch (task
->tk_status
) {
4849 case -NFS4ERR_LEASE_MOVED
:
4850 nfs4_schedule_lease_moved_recovery(clp
);
4853 /* Unless we're shutting down, schedule state recovery! */
4854 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4856 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4857 nfs4_schedule_lease_recovery(clp
);
4860 nfs4_schedule_path_down_recovery(clp
);
4862 do_renew_lease(clp
, timestamp
);
4865 static const struct rpc_call_ops nfs4_renew_ops
= {
4866 .rpc_call_done
= nfs4_renew_done
,
4867 .rpc_release
= nfs4_renew_release
,
4870 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4872 struct rpc_message msg
= {
4873 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4877 struct nfs4_renewdata
*data
;
4879 if (renew_flags
== 0)
4881 if (!atomic_inc_not_zero(&clp
->cl_count
))
4883 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4887 data
->timestamp
= jiffies
;
4888 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4889 &nfs4_renew_ops
, data
);
4892 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4894 struct rpc_message msg
= {
4895 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4899 unsigned long now
= jiffies
;
4902 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4905 do_renew_lease(clp
, now
);
4909 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4911 return server
->caps
& NFS_CAP_ACLS
;
4914 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4915 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4918 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4920 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4921 struct page
**pages
)
4923 struct page
*newpage
, **spages
;
4929 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4930 newpage
= alloc_page(GFP_KERNEL
);
4932 if (newpage
== NULL
)
4934 memcpy(page_address(newpage
), buf
, len
);
4939 } while (buflen
!= 0);
4945 __free_page(spages
[rc
-1]);
4949 struct nfs4_cached_acl
{
4955 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4957 struct nfs_inode
*nfsi
= NFS_I(inode
);
4959 spin_lock(&inode
->i_lock
);
4960 kfree(nfsi
->nfs4_acl
);
4961 nfsi
->nfs4_acl
= acl
;
4962 spin_unlock(&inode
->i_lock
);
4965 static void nfs4_zap_acl_attr(struct inode
*inode
)
4967 nfs4_set_cached_acl(inode
, NULL
);
4970 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4972 struct nfs_inode
*nfsi
= NFS_I(inode
);
4973 struct nfs4_cached_acl
*acl
;
4976 spin_lock(&inode
->i_lock
);
4977 acl
= nfsi
->nfs4_acl
;
4980 if (buf
== NULL
) /* user is just asking for length */
4982 if (acl
->cached
== 0)
4984 ret
= -ERANGE
; /* see getxattr(2) man page */
4985 if (acl
->len
> buflen
)
4987 memcpy(buf
, acl
->data
, acl
->len
);
4991 spin_unlock(&inode
->i_lock
);
4995 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4997 struct nfs4_cached_acl
*acl
;
4998 size_t buflen
= sizeof(*acl
) + acl_len
;
5000 if (buflen
<= PAGE_SIZE
) {
5001 acl
= kmalloc(buflen
, GFP_KERNEL
);
5005 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
5007 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
5014 nfs4_set_cached_acl(inode
, acl
);
5018 * The getxattr API returns the required buffer length when called with a
5019 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5020 * the required buf. On a NULL buf, we send a page of data to the server
5021 * guessing that the ACL request can be serviced by a page. If so, we cache
5022 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5023 * the cache. If not so, we throw away the page, and cache the required
5024 * length. The next getxattr call will then produce another round trip to
5025 * the server, this time with the input buf of the required size.
5027 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5029 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
5030 struct nfs_getaclargs args
= {
5031 .fh
= NFS_FH(inode
),
5035 struct nfs_getaclres res
= {
5038 struct rpc_message msg
= {
5039 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
5043 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5044 int ret
= -ENOMEM
, i
;
5046 /* As long as we're doing a round trip to the server anyway,
5047 * let's be prepared for a page of acl data. */
5050 if (npages
> ARRAY_SIZE(pages
))
5053 for (i
= 0; i
< npages
; i
++) {
5054 pages
[i
] = alloc_page(GFP_KERNEL
);
5059 /* for decoding across pages */
5060 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
5061 if (!res
.acl_scratch
)
5064 args
.acl_len
= npages
* PAGE_SIZE
;
5066 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5067 __func__
, buf
, buflen
, npages
, args
.acl_len
);
5068 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
5069 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5073 /* Handle the case where the passed-in buffer is too short */
5074 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
5075 /* Did the user only issue a request for the acl length? */
5081 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
5083 if (res
.acl_len
> buflen
) {
5087 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
5092 for (i
= 0; i
< npages
; i
++)
5094 __free_page(pages
[i
]);
5095 if (res
.acl_scratch
)
5096 __free_page(res
.acl_scratch
);
5100 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5102 struct nfs4_exception exception
= { };
5105 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
5106 trace_nfs4_get_acl(inode
, ret
);
5109 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
5110 } while (exception
.retry
);
5114 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
5116 struct nfs_server
*server
= NFS_SERVER(inode
);
5119 if (!nfs4_server_supports_acls(server
))
5121 ret
= nfs_revalidate_inode(server
, inode
);
5124 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
5125 nfs_zap_acl_cache(inode
);
5126 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
5128 /* -ENOENT is returned if there is no ACL or if there is an ACL
5129 * but no cached acl data, just the acl length */
5131 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
5134 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5136 struct nfs_server
*server
= NFS_SERVER(inode
);
5137 struct page
*pages
[NFS4ACL_MAXPAGES
];
5138 struct nfs_setaclargs arg
= {
5139 .fh
= NFS_FH(inode
),
5143 struct nfs_setaclres res
;
5144 struct rpc_message msg
= {
5145 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
5149 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5152 if (!nfs4_server_supports_acls(server
))
5154 if (npages
> ARRAY_SIZE(pages
))
5156 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
5159 nfs4_inode_return_delegation(inode
);
5160 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5163 * Free each page after tx, so the only ref left is
5164 * held by the network stack
5167 put_page(pages
[i
-1]);
5170 * Acl update can result in inode attribute update.
5171 * so mark the attribute cache invalid.
5173 spin_lock(&inode
->i_lock
);
5174 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
5175 spin_unlock(&inode
->i_lock
);
5176 nfs_access_zap_cache(inode
);
5177 nfs_zap_acl_cache(inode
);
5181 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5183 struct nfs4_exception exception
= { };
5186 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
5187 trace_nfs4_set_acl(inode
, err
);
5188 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5190 } while (exception
.retry
);
5194 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5195 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
5198 struct nfs_server
*server
= NFS_SERVER(inode
);
5199 struct nfs_fattr fattr
;
5200 struct nfs4_label label
= {0, 0, buflen
, buf
};
5202 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5203 struct nfs4_getattr_arg arg
= {
5204 .fh
= NFS_FH(inode
),
5207 struct nfs4_getattr_res res
= {
5212 struct rpc_message msg
= {
5213 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
5219 nfs_fattr_init(&fattr
);
5221 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
5224 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
5226 if (buflen
< label
.len
)
5231 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
5234 struct nfs4_exception exception
= { };
5237 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5241 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
5242 trace_nfs4_get_security_label(inode
, err
);
5243 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5245 } while (exception
.retry
);
5249 static int _nfs4_do_set_security_label(struct inode
*inode
,
5250 struct nfs4_label
*ilabel
,
5251 struct nfs_fattr
*fattr
,
5252 struct nfs4_label
*olabel
)
5255 struct iattr sattr
= {0};
5256 struct nfs_server
*server
= NFS_SERVER(inode
);
5257 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5258 struct nfs_setattrargs arg
= {
5259 .fh
= NFS_FH(inode
),
5265 struct nfs_setattrres res
= {
5270 struct rpc_message msg
= {
5271 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
5277 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
5279 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5281 dprintk("%s failed: %d\n", __func__
, status
);
5286 static int nfs4_do_set_security_label(struct inode
*inode
,
5287 struct nfs4_label
*ilabel
,
5288 struct nfs_fattr
*fattr
,
5289 struct nfs4_label
*olabel
)
5291 struct nfs4_exception exception
= { };
5295 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5297 trace_nfs4_set_security_label(inode
, err
);
5298 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5300 } while (exception
.retry
);
5305 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5307 struct nfs4_label ilabel
, *olabel
= NULL
;
5308 struct nfs_fattr fattr
;
5309 struct rpc_cred
*cred
;
5312 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5315 nfs_fattr_init(&fattr
);
5319 ilabel
.label
= (char *)buf
;
5320 ilabel
.len
= buflen
;
5322 cred
= rpc_lookup_cred();
5324 return PTR_ERR(cred
);
5326 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5327 if (IS_ERR(olabel
)) {
5328 status
= -PTR_ERR(olabel
);
5332 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5334 nfs_setsecurity(inode
, &fattr
, olabel
);
5336 nfs4_label_free(olabel
);
5341 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5344 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5345 nfs4_verifier
*bootverf
)
5349 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5350 /* An impossible timestamp guarantees this value
5351 * will never match a generated boot time. */
5352 verf
[0] = cpu_to_be32(U32_MAX
);
5353 verf
[1] = cpu_to_be32(U32_MAX
);
5355 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5356 u64 ns
= ktime_to_ns(nn
->boot_time
);
5358 verf
[0] = cpu_to_be32(ns
>> 32);
5359 verf
[1] = cpu_to_be32(ns
);
5361 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5365 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5370 if (clp
->cl_owner_id
!= NULL
)
5374 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5375 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5377 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
5381 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5385 * Since this string is allocated at mount time, and held until the
5386 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5387 * about a memory-reclaim deadlock.
5389 str
= kmalloc(len
, GFP_KERNEL
);
5394 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5396 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5397 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5400 clp
->cl_owner_id
= str
;
5405 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5410 len
= 10 + 10 + 1 + 10 + 1 +
5411 strlen(nfs4_client_id_uniquifier
) + 1 +
5412 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5414 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5418 * Since this string is allocated at mount time, and held until the
5419 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5420 * about a memory-reclaim deadlock.
5422 str
= kmalloc(len
, GFP_KERNEL
);
5426 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5427 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5428 nfs4_client_id_uniquifier
,
5429 clp
->cl_rpcclient
->cl_nodename
);
5430 clp
->cl_owner_id
= str
;
5435 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5440 if (clp
->cl_owner_id
!= NULL
)
5443 if (nfs4_client_id_uniquifier
[0] != '\0')
5444 return nfs4_init_uniquifier_client_string(clp
);
5446 len
= 10 + 10 + 1 + 10 + 1 +
5447 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5449 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5453 * Since this string is allocated at mount time, and held until the
5454 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5455 * about a memory-reclaim deadlock.
5457 str
= kmalloc(len
, GFP_KERNEL
);
5461 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5462 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5463 clp
->cl_rpcclient
->cl_nodename
);
5464 clp
->cl_owner_id
= str
;
5469 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5470 * services. Advertise one based on the address family of the
5474 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5476 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5477 return scnprintf(buf
, len
, "tcp6");
5479 return scnprintf(buf
, len
, "tcp");
5482 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5484 struct nfs4_setclientid
*sc
= calldata
;
5486 if (task
->tk_status
== 0)
5487 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5490 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5491 .rpc_call_done
= nfs4_setclientid_done
,
5495 * nfs4_proc_setclientid - Negotiate client ID
5496 * @clp: state data structure
5497 * @program: RPC program for NFSv4 callback service
5498 * @port: IP port number for NFS4 callback service
5499 * @cred: RPC credential to use for this call
5500 * @res: where to place the result
5502 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5504 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5505 unsigned short port
, struct rpc_cred
*cred
,
5506 struct nfs4_setclientid_res
*res
)
5508 nfs4_verifier sc_verifier
;
5509 struct nfs4_setclientid setclientid
= {
5510 .sc_verifier
= &sc_verifier
,
5514 struct rpc_message msg
= {
5515 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5516 .rpc_argp
= &setclientid
,
5520 struct rpc_task
*task
;
5521 struct rpc_task_setup task_setup_data
= {
5522 .rpc_client
= clp
->cl_rpcclient
,
5523 .rpc_message
= &msg
,
5524 .callback_ops
= &nfs4_setclientid_ops
,
5525 .callback_data
= &setclientid
,
5526 .flags
= RPC_TASK_TIMEOUT
,
5530 /* nfs_client_id4 */
5531 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5533 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5534 status
= nfs4_init_uniform_client_string(clp
);
5536 status
= nfs4_init_nonuniform_client_string(clp
);
5542 setclientid
.sc_netid_len
=
5543 nfs4_init_callback_netid(clp
,
5544 setclientid
.sc_netid
,
5545 sizeof(setclientid
.sc_netid
));
5546 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5547 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5548 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5550 dprintk("NFS call setclientid auth=%s, '%s'\n",
5551 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5553 task
= rpc_run_task(&task_setup_data
);
5555 status
= PTR_ERR(task
);
5558 status
= task
->tk_status
;
5559 if (setclientid
.sc_cred
) {
5560 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5561 put_rpccred(setclientid
.sc_cred
);
5565 trace_nfs4_setclientid(clp
, status
);
5566 dprintk("NFS reply setclientid: %d\n", status
);
5571 * nfs4_proc_setclientid_confirm - Confirm client ID
5572 * @clp: state data structure
5573 * @res: result of a previous SETCLIENTID
5574 * @cred: RPC credential to use for this call
5576 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5578 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5579 struct nfs4_setclientid_res
*arg
,
5580 struct rpc_cred
*cred
)
5582 struct rpc_message msg
= {
5583 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5589 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5590 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5592 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5593 trace_nfs4_setclientid_confirm(clp
, status
);
5594 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5598 struct nfs4_delegreturndata
{
5599 struct nfs4_delegreturnargs args
;
5600 struct nfs4_delegreturnres res
;
5602 nfs4_stateid stateid
;
5603 unsigned long timestamp
;
5605 struct nfs4_layoutreturn_args arg
;
5606 struct nfs4_layoutreturn_res res
;
5610 struct nfs_fattr fattr
;
5612 struct inode
*inode
;
5615 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5617 struct nfs4_delegreturndata
*data
= calldata
;
5619 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5622 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5624 /* Handle Layoutreturn errors */
5625 if (data
->args
.lr_args
&& task
->tk_status
!= 0) {
5626 switch(data
->res
.lr_ret
) {
5628 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5631 data
->args
.lr_args
= NULL
;
5632 data
->res
.lr_res
= NULL
;
5634 case -NFS4ERR_ADMIN_REVOKED
:
5635 case -NFS4ERR_DELEG_REVOKED
:
5636 case -NFS4ERR_EXPIRED
:
5637 case -NFS4ERR_BAD_STATEID
:
5638 case -NFS4ERR_OLD_STATEID
:
5639 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
5640 case -NFS4ERR_WRONG_CRED
:
5641 data
->args
.lr_args
= NULL
;
5642 data
->res
.lr_res
= NULL
;
5643 data
->res
.lr_ret
= 0;
5644 rpc_restart_call_prepare(task
);
5649 switch (task
->tk_status
) {
5651 renew_lease(data
->res
.server
, data
->timestamp
);
5653 case -NFS4ERR_ADMIN_REVOKED
:
5654 case -NFS4ERR_DELEG_REVOKED
:
5655 case -NFS4ERR_EXPIRED
:
5656 nfs4_free_revoked_stateid(data
->res
.server
,
5658 task
->tk_msg
.rpc_cred
);
5659 case -NFS4ERR_BAD_STATEID
:
5660 case -NFS4ERR_OLD_STATEID
:
5661 case -NFS4ERR_STALE_STATEID
:
5662 task
->tk_status
= 0;
5665 if (nfs4_async_handle_error(task
, data
->res
.server
,
5666 NULL
, NULL
) == -EAGAIN
) {
5667 rpc_restart_call_prepare(task
);
5671 data
->rpc_status
= task
->tk_status
;
5674 static void nfs4_delegreturn_release(void *calldata
)
5676 struct nfs4_delegreturndata
*data
= calldata
;
5677 struct inode
*inode
= data
->inode
;
5681 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
,
5683 nfs_iput_and_deactive(inode
);
5688 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5690 struct nfs4_delegreturndata
*d_data
;
5692 d_data
= (struct nfs4_delegreturndata
*)data
;
5694 if (!d_data
->lr
.roc
&& nfs4_wait_on_layoutreturn(d_data
->inode
, task
))
5697 nfs4_setup_sequence(d_data
->res
.server
,
5698 &d_data
->args
.seq_args
,
5699 &d_data
->res
.seq_res
,
5703 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5704 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5705 .rpc_call_done
= nfs4_delegreturn_done
,
5706 .rpc_release
= nfs4_delegreturn_release
,
5709 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5711 struct nfs4_delegreturndata
*data
;
5712 struct nfs_server
*server
= NFS_SERVER(inode
);
5713 struct rpc_task
*task
;
5714 struct rpc_message msg
= {
5715 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5718 struct rpc_task_setup task_setup_data
= {
5719 .rpc_client
= server
->client
,
5720 .rpc_message
= &msg
,
5721 .callback_ops
= &nfs4_delegreturn_ops
,
5722 .flags
= RPC_TASK_ASYNC
,
5726 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5729 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5731 nfs4_state_protect(server
->nfs_client
,
5732 NFS_SP4_MACH_CRED_CLEANUP
,
5733 &task_setup_data
.rpc_client
, &msg
);
5735 data
->args
.fhandle
= &data
->fh
;
5736 data
->args
.stateid
= &data
->stateid
;
5737 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5738 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5739 nfs4_stateid_copy(&data
->stateid
, stateid
);
5740 data
->res
.fattr
= &data
->fattr
;
5741 data
->res
.server
= server
;
5742 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5743 nfs_fattr_init(data
->res
.fattr
);
5744 data
->timestamp
= jiffies
;
5745 data
->rpc_status
= 0;
5746 data
->lr
.roc
= pnfs_roc(inode
, &data
->lr
.arg
, &data
->lr
.res
, cred
);
5747 data
->inode
= nfs_igrab_and_active(inode
);
5750 data
->args
.lr_args
= &data
->lr
.arg
;
5751 data
->res
.lr_res
= &data
->lr
.res
;
5753 } else if (data
->lr
.roc
) {
5754 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
, 0);
5755 data
->lr
.roc
= false;
5758 task_setup_data
.callback_data
= data
;
5759 msg
.rpc_argp
= &data
->args
;
5760 msg
.rpc_resp
= &data
->res
;
5761 task
= rpc_run_task(&task_setup_data
);
5763 return PTR_ERR(task
);
5766 status
= nfs4_wait_for_completion_rpc_task(task
);
5769 status
= data
->rpc_status
;
5771 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5773 nfs_refresh_inode(inode
, &data
->fattr
);
5779 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5781 struct nfs_server
*server
= NFS_SERVER(inode
);
5782 struct nfs4_exception exception
= { };
5785 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5786 trace_nfs4_delegreturn(inode
, stateid
, err
);
5788 case -NFS4ERR_STALE_STATEID
:
5789 case -NFS4ERR_EXPIRED
:
5793 err
= nfs4_handle_exception(server
, err
, &exception
);
5794 } while (exception
.retry
);
5798 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5800 struct inode
*inode
= state
->inode
;
5801 struct nfs_server
*server
= NFS_SERVER(inode
);
5802 struct nfs_client
*clp
= server
->nfs_client
;
5803 struct nfs_lockt_args arg
= {
5804 .fh
= NFS_FH(inode
),
5807 struct nfs_lockt_res res
= {
5810 struct rpc_message msg
= {
5811 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5814 .rpc_cred
= state
->owner
->so_cred
,
5816 struct nfs4_lock_state
*lsp
;
5819 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5820 status
= nfs4_set_lock_state(state
, request
);
5823 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5824 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5825 arg
.lock_owner
.s_dev
= server
->s_dev
;
5826 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5829 request
->fl_type
= F_UNLCK
;
5831 case -NFS4ERR_DENIED
:
5834 request
->fl_ops
->fl_release_private(request
);
5835 request
->fl_ops
= NULL
;
5840 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5842 struct nfs4_exception exception
= { };
5846 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5847 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5848 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5850 } while (exception
.retry
);
5854 struct nfs4_unlockdata
{
5855 struct nfs_locku_args arg
;
5856 struct nfs_locku_res res
;
5857 struct nfs4_lock_state
*lsp
;
5858 struct nfs_open_context
*ctx
;
5859 struct file_lock fl
;
5860 struct nfs_server
*server
;
5861 unsigned long timestamp
;
5864 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5865 struct nfs_open_context
*ctx
,
5866 struct nfs4_lock_state
*lsp
,
5867 struct nfs_seqid
*seqid
)
5869 struct nfs4_unlockdata
*p
;
5870 struct inode
*inode
= lsp
->ls_state
->inode
;
5872 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5875 p
->arg
.fh
= NFS_FH(inode
);
5877 p
->arg
.seqid
= seqid
;
5878 p
->res
.seqid
= seqid
;
5880 atomic_inc(&lsp
->ls_count
);
5881 /* Ensure we don't close file until we're done freeing locks! */
5882 p
->ctx
= get_nfs_open_context(ctx
);
5883 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5884 p
->server
= NFS_SERVER(inode
);
5888 static void nfs4_locku_release_calldata(void *data
)
5890 struct nfs4_unlockdata
*calldata
= data
;
5891 nfs_free_seqid(calldata
->arg
.seqid
);
5892 nfs4_put_lock_state(calldata
->lsp
);
5893 put_nfs_open_context(calldata
->ctx
);
5897 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5899 struct nfs4_unlockdata
*calldata
= data
;
5901 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5903 switch (task
->tk_status
) {
5905 renew_lease(calldata
->server
, calldata
->timestamp
);
5906 locks_lock_inode_wait(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5907 if (nfs4_update_lock_stateid(calldata
->lsp
,
5908 &calldata
->res
.stateid
))
5910 case -NFS4ERR_ADMIN_REVOKED
:
5911 case -NFS4ERR_EXPIRED
:
5912 nfs4_free_revoked_stateid(calldata
->server
,
5913 &calldata
->arg
.stateid
,
5914 task
->tk_msg
.rpc_cred
);
5915 case -NFS4ERR_BAD_STATEID
:
5916 case -NFS4ERR_OLD_STATEID
:
5917 case -NFS4ERR_STALE_STATEID
:
5918 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5919 &calldata
->lsp
->ls_stateid
))
5920 rpc_restart_call_prepare(task
);
5923 if (nfs4_async_handle_error(task
, calldata
->server
,
5924 NULL
, NULL
) == -EAGAIN
)
5925 rpc_restart_call_prepare(task
);
5927 nfs_release_seqid(calldata
->arg
.seqid
);
5930 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5932 struct nfs4_unlockdata
*calldata
= data
;
5934 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5936 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5937 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5938 /* Note: exit _without_ running nfs4_locku_done */
5941 calldata
->timestamp
= jiffies
;
5942 if (nfs4_setup_sequence(calldata
->server
,
5943 &calldata
->arg
.seq_args
,
5944 &calldata
->res
.seq_res
,
5946 nfs_release_seqid(calldata
->arg
.seqid
);
5949 task
->tk_action
= NULL
;
5951 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5954 static const struct rpc_call_ops nfs4_locku_ops
= {
5955 .rpc_call_prepare
= nfs4_locku_prepare
,
5956 .rpc_call_done
= nfs4_locku_done
,
5957 .rpc_release
= nfs4_locku_release_calldata
,
5960 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5961 struct nfs_open_context
*ctx
,
5962 struct nfs4_lock_state
*lsp
,
5963 struct nfs_seqid
*seqid
)
5965 struct nfs4_unlockdata
*data
;
5966 struct rpc_message msg
= {
5967 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5968 .rpc_cred
= ctx
->cred
,
5970 struct rpc_task_setup task_setup_data
= {
5971 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5972 .rpc_message
= &msg
,
5973 .callback_ops
= &nfs4_locku_ops
,
5974 .workqueue
= nfsiod_workqueue
,
5975 .flags
= RPC_TASK_ASYNC
,
5978 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5979 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5981 /* Ensure this is an unlock - when canceling a lock, the
5982 * canceled lock is passed in, and it won't be an unlock.
5984 fl
->fl_type
= F_UNLCK
;
5986 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5988 nfs_free_seqid(seqid
);
5989 return ERR_PTR(-ENOMEM
);
5992 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5993 msg
.rpc_argp
= &data
->arg
;
5994 msg
.rpc_resp
= &data
->res
;
5995 task_setup_data
.callback_data
= data
;
5996 return rpc_run_task(&task_setup_data
);
5999 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6001 struct inode
*inode
= state
->inode
;
6002 struct nfs4_state_owner
*sp
= state
->owner
;
6003 struct nfs_inode
*nfsi
= NFS_I(inode
);
6004 struct nfs_seqid
*seqid
;
6005 struct nfs4_lock_state
*lsp
;
6006 struct rpc_task
*task
;
6007 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6009 unsigned char fl_flags
= request
->fl_flags
;
6011 status
= nfs4_set_lock_state(state
, request
);
6012 /* Unlock _before_ we do the RPC call */
6013 request
->fl_flags
|= FL_EXISTS
;
6014 /* Exclude nfs_delegation_claim_locks() */
6015 mutex_lock(&sp
->so_delegreturn_mutex
);
6016 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6017 down_read(&nfsi
->rwsem
);
6018 if (locks_lock_inode_wait(inode
, request
) == -ENOENT
) {
6019 up_read(&nfsi
->rwsem
);
6020 mutex_unlock(&sp
->so_delegreturn_mutex
);
6023 up_read(&nfsi
->rwsem
);
6024 mutex_unlock(&sp
->so_delegreturn_mutex
);
6027 /* Is this a delegated lock? */
6028 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6029 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
6031 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
6032 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
6036 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
6037 status
= PTR_ERR(task
);
6040 status
= nfs4_wait_for_completion_rpc_task(task
);
6043 request
->fl_flags
= fl_flags
;
6044 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
6048 struct nfs4_lockdata
{
6049 struct nfs_lock_args arg
;
6050 struct nfs_lock_res res
;
6051 struct nfs4_lock_state
*lsp
;
6052 struct nfs_open_context
*ctx
;
6053 struct file_lock fl
;
6054 unsigned long timestamp
;
6057 struct nfs_server
*server
;
6060 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
6061 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
6064 struct nfs4_lockdata
*p
;
6065 struct inode
*inode
= lsp
->ls_state
->inode
;
6066 struct nfs_server
*server
= NFS_SERVER(inode
);
6067 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6069 p
= kzalloc(sizeof(*p
), gfp_mask
);
6073 p
->arg
.fh
= NFS_FH(inode
);
6075 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
6076 if (IS_ERR(p
->arg
.open_seqid
))
6078 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
6079 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
6080 if (IS_ERR(p
->arg
.lock_seqid
))
6081 goto out_free_seqid
;
6082 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6083 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6084 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
6085 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
6088 atomic_inc(&lsp
->ls_count
);
6089 p
->ctx
= get_nfs_open_context(ctx
);
6090 get_file(fl
->fl_file
);
6091 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
6094 nfs_free_seqid(p
->arg
.open_seqid
);
6100 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
6102 struct nfs4_lockdata
*data
= calldata
;
6103 struct nfs4_state
*state
= data
->lsp
->ls_state
;
6105 dprintk("%s: begin!\n", __func__
);
6106 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
6108 /* Do we need to do an open_to_lock_owner? */
6109 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
6110 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
6111 goto out_release_lock_seqid
;
6113 nfs4_stateid_copy(&data
->arg
.open_stateid
,
6114 &state
->open_stateid
);
6115 data
->arg
.new_lock_owner
= 1;
6116 data
->res
.open_seqid
= data
->arg
.open_seqid
;
6118 data
->arg
.new_lock_owner
= 0;
6119 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
6120 &data
->lsp
->ls_stateid
);
6122 if (!nfs4_valid_open_stateid(state
)) {
6123 data
->rpc_status
= -EBADF
;
6124 task
->tk_action
= NULL
;
6125 goto out_release_open_seqid
;
6127 data
->timestamp
= jiffies
;
6128 if (nfs4_setup_sequence(data
->server
,
6129 &data
->arg
.seq_args
,
6133 out_release_open_seqid
:
6134 nfs_release_seqid(data
->arg
.open_seqid
);
6135 out_release_lock_seqid
:
6136 nfs_release_seqid(data
->arg
.lock_seqid
);
6138 nfs4_sequence_done(task
, &data
->res
.seq_res
);
6139 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
6142 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
6144 struct nfs4_lockdata
*data
= calldata
;
6145 struct nfs4_lock_state
*lsp
= data
->lsp
;
6147 dprintk("%s: begin!\n", __func__
);
6149 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
6152 data
->rpc_status
= task
->tk_status
;
6153 switch (task
->tk_status
) {
6155 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
6157 if (data
->arg
.new_lock
) {
6158 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
6159 if (locks_lock_inode_wait(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
6160 rpc_restart_call_prepare(task
);
6164 if (data
->arg
.new_lock_owner
!= 0) {
6165 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
6166 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
6167 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6168 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
6169 rpc_restart_call_prepare(task
);
6171 case -NFS4ERR_BAD_STATEID
:
6172 case -NFS4ERR_OLD_STATEID
:
6173 case -NFS4ERR_STALE_STATEID
:
6174 case -NFS4ERR_EXPIRED
:
6175 if (data
->arg
.new_lock_owner
!= 0) {
6176 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
6177 &lsp
->ls_state
->open_stateid
))
6178 rpc_restart_call_prepare(task
);
6179 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
6181 rpc_restart_call_prepare(task
);
6183 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
6186 static void nfs4_lock_release(void *calldata
)
6188 struct nfs4_lockdata
*data
= calldata
;
6190 dprintk("%s: begin!\n", __func__
);
6191 nfs_free_seqid(data
->arg
.open_seqid
);
6192 if (data
->cancelled
!= 0) {
6193 struct rpc_task
*task
;
6194 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
6195 data
->arg
.lock_seqid
);
6197 rpc_put_task_async(task
);
6198 dprintk("%s: cancelling lock!\n", __func__
);
6200 nfs_free_seqid(data
->arg
.lock_seqid
);
6201 nfs4_put_lock_state(data
->lsp
);
6202 put_nfs_open_context(data
->ctx
);
6203 fput(data
->fl
.fl_file
);
6205 dprintk("%s: done!\n", __func__
);
6208 static const struct rpc_call_ops nfs4_lock_ops
= {
6209 .rpc_call_prepare
= nfs4_lock_prepare
,
6210 .rpc_call_done
= nfs4_lock_done
,
6211 .rpc_release
= nfs4_lock_release
,
6214 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
6217 case -NFS4ERR_ADMIN_REVOKED
:
6218 case -NFS4ERR_EXPIRED
:
6219 case -NFS4ERR_BAD_STATEID
:
6220 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6221 if (new_lock_owner
!= 0 ||
6222 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
6223 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
6225 case -NFS4ERR_STALE_STATEID
:
6226 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6227 nfs4_schedule_lease_recovery(server
->nfs_client
);
6231 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
6233 struct nfs4_lockdata
*data
;
6234 struct rpc_task
*task
;
6235 struct rpc_message msg
= {
6236 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
6237 .rpc_cred
= state
->owner
->so_cred
,
6239 struct rpc_task_setup task_setup_data
= {
6240 .rpc_client
= NFS_CLIENT(state
->inode
),
6241 .rpc_message
= &msg
,
6242 .callback_ops
= &nfs4_lock_ops
,
6243 .workqueue
= nfsiod_workqueue
,
6244 .flags
= RPC_TASK_ASYNC
,
6248 dprintk("%s: begin!\n", __func__
);
6249 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
6250 fl
->fl_u
.nfs4_fl
.owner
,
6251 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
6255 data
->arg
.block
= 1;
6256 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6257 msg
.rpc_argp
= &data
->arg
;
6258 msg
.rpc_resp
= &data
->res
;
6259 task_setup_data
.callback_data
= data
;
6260 if (recovery_type
> NFS_LOCK_NEW
) {
6261 if (recovery_type
== NFS_LOCK_RECLAIM
)
6262 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
6263 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
6265 data
->arg
.new_lock
= 1;
6266 task
= rpc_run_task(&task_setup_data
);
6268 return PTR_ERR(task
);
6269 ret
= nfs4_wait_for_completion_rpc_task(task
);
6271 ret
= data
->rpc_status
;
6273 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
6274 data
->arg
.new_lock_owner
, ret
);
6276 data
->cancelled
= 1;
6278 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
6279 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
6283 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
6285 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6286 struct nfs4_exception exception
= {
6287 .inode
= state
->inode
,
6292 /* Cache the lock if possible... */
6293 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6295 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
6296 if (err
!= -NFS4ERR_DELAY
)
6298 nfs4_handle_exception(server
, err
, &exception
);
6299 } while (exception
.retry
);
6303 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6305 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6306 struct nfs4_exception exception
= {
6307 .inode
= state
->inode
,
6311 err
= nfs4_set_lock_state(state
, request
);
6314 if (!recover_lost_locks
) {
6315 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6319 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6321 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6325 case -NFS4ERR_GRACE
:
6326 case -NFS4ERR_DELAY
:
6327 nfs4_handle_exception(server
, err
, &exception
);
6330 } while (exception
.retry
);
6335 #if defined(CONFIG_NFS_V4_1)
6336 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6338 struct nfs4_lock_state
*lsp
;
6341 status
= nfs4_set_lock_state(state
, request
);
6344 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6345 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) ||
6346 test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
6348 status
= nfs4_lock_expired(state
, request
);
6353 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6355 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6356 struct nfs4_state_owner
*sp
= state
->owner
;
6357 unsigned char fl_flags
= request
->fl_flags
;
6360 request
->fl_flags
|= FL_ACCESS
;
6361 status
= locks_lock_inode_wait(state
->inode
, request
);
6364 mutex_lock(&sp
->so_delegreturn_mutex
);
6365 down_read(&nfsi
->rwsem
);
6366 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6367 /* Yes: cache locks! */
6368 /* ...but avoid races with delegation recall... */
6369 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6370 status
= locks_lock_inode_wait(state
->inode
, request
);
6371 up_read(&nfsi
->rwsem
);
6372 mutex_unlock(&sp
->so_delegreturn_mutex
);
6375 up_read(&nfsi
->rwsem
);
6376 mutex_unlock(&sp
->so_delegreturn_mutex
);
6377 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6379 request
->fl_flags
= fl_flags
;
6383 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6385 struct nfs4_exception exception
= {
6387 .inode
= state
->inode
,
6392 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6393 if (err
== -NFS4ERR_DENIED
)
6395 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6397 } while (exception
.retry
);
6401 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6402 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6405 nfs4_retry_setlk_simple(struct nfs4_state
*state
, int cmd
,
6406 struct file_lock
*request
)
6408 int status
= -ERESTARTSYS
;
6409 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6411 while(!signalled()) {
6412 status
= nfs4_proc_setlk(state
, cmd
, request
);
6413 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6415 freezable_schedule_timeout_interruptible(timeout
);
6417 timeout
= min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT
, timeout
);
6418 status
= -ERESTARTSYS
;
6423 #ifdef CONFIG_NFS_V4_1
6424 struct nfs4_lock_waiter
{
6425 struct task_struct
*task
;
6426 struct inode
*inode
;
6427 struct nfs_lowner
*owner
;
6432 nfs4_wake_lock_waiter(wait_queue_t
*wait
, unsigned int mode
, int flags
, void *key
)
6435 struct cb_notify_lock_args
*cbnl
= key
;
6436 struct nfs4_lock_waiter
*waiter
= wait
->private;
6437 struct nfs_lowner
*lowner
= &cbnl
->cbnl_owner
,
6438 *wowner
= waiter
->owner
;
6440 /* Only wake if the callback was for the same owner */
6441 if (lowner
->clientid
!= wowner
->clientid
||
6442 lowner
->id
!= wowner
->id
||
6443 lowner
->s_dev
!= wowner
->s_dev
)
6446 /* Make sure it's for the right inode */
6447 if (nfs_compare_fh(NFS_FH(waiter
->inode
), &cbnl
->cbnl_fh
))
6450 waiter
->notified
= true;
6452 /* override "private" so we can use default_wake_function */
6453 wait
->private = waiter
->task
;
6454 ret
= autoremove_wake_function(wait
, mode
, flags
, key
);
6455 wait
->private = waiter
;
6460 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6462 int status
= -ERESTARTSYS
;
6463 unsigned long flags
;
6464 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
6465 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6466 struct nfs_client
*clp
= server
->nfs_client
;
6467 wait_queue_head_t
*q
= &clp
->cl_lock_waitq
;
6468 struct nfs_lowner owner
= { .clientid
= clp
->cl_clientid
,
6469 .id
= lsp
->ls_seqid
.owner_id
,
6470 .s_dev
= server
->s_dev
};
6471 struct nfs4_lock_waiter waiter
= { .task
= current
,
6472 .inode
= state
->inode
,
6474 .notified
= false };
6477 /* Don't bother with waitqueue if we don't expect a callback */
6478 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
))
6479 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6482 wait
.private = &waiter
;
6483 wait
.func
= nfs4_wake_lock_waiter
;
6484 add_wait_queue(q
, &wait
);
6486 while(!signalled()) {
6487 status
= nfs4_proc_setlk(state
, cmd
, request
);
6488 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6491 status
= -ERESTARTSYS
;
6492 spin_lock_irqsave(&q
->lock
, flags
);
6493 if (waiter
.notified
) {
6494 spin_unlock_irqrestore(&q
->lock
, flags
);
6497 set_current_state(TASK_INTERRUPTIBLE
);
6498 spin_unlock_irqrestore(&q
->lock
, flags
);
6500 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT
);
6503 finish_wait(q
, &wait
);
6506 #else /* !CONFIG_NFS_V4_1 */
6508 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6510 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6515 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6517 struct nfs_open_context
*ctx
;
6518 struct nfs4_state
*state
;
6521 /* verify open state */
6522 ctx
= nfs_file_open_context(filp
);
6525 if (request
->fl_start
< 0 || request
->fl_end
< 0)
6528 if (IS_GETLK(cmd
)) {
6530 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6534 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6537 if (request
->fl_type
== F_UNLCK
) {
6539 return nfs4_proc_unlck(state
, cmd
, request
);
6546 if ((request
->fl_flags
& FL_POSIX
) &&
6547 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6551 * Don't rely on the VFS having checked the file open mode,
6552 * since it won't do this for flock() locks.
6554 switch (request
->fl_type
) {
6556 if (!(filp
->f_mode
& FMODE_READ
))
6560 if (!(filp
->f_mode
& FMODE_WRITE
))
6564 status
= nfs4_set_lock_state(state
, request
);
6568 return nfs4_retry_setlk(state
, cmd
, request
);
6571 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6573 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6576 err
= nfs4_set_lock_state(state
, fl
);
6579 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6580 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6583 struct nfs_release_lockowner_data
{
6584 struct nfs4_lock_state
*lsp
;
6585 struct nfs_server
*server
;
6586 struct nfs_release_lockowner_args args
;
6587 struct nfs_release_lockowner_res res
;
6588 unsigned long timestamp
;
6591 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6593 struct nfs_release_lockowner_data
*data
= calldata
;
6594 struct nfs_server
*server
= data
->server
;
6595 nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
6596 &data
->args
.seq_args
, &data
->res
.seq_res
, task
);
6597 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6598 data
->timestamp
= jiffies
;
6601 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6603 struct nfs_release_lockowner_data
*data
= calldata
;
6604 struct nfs_server
*server
= data
->server
;
6606 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6608 switch (task
->tk_status
) {
6610 renew_lease(server
, data
->timestamp
);
6612 case -NFS4ERR_STALE_CLIENTID
:
6613 case -NFS4ERR_EXPIRED
:
6614 nfs4_schedule_lease_recovery(server
->nfs_client
);
6616 case -NFS4ERR_LEASE_MOVED
:
6617 case -NFS4ERR_DELAY
:
6618 if (nfs4_async_handle_error(task
, server
,
6619 NULL
, NULL
) == -EAGAIN
)
6620 rpc_restart_call_prepare(task
);
6624 static void nfs4_release_lockowner_release(void *calldata
)
6626 struct nfs_release_lockowner_data
*data
= calldata
;
6627 nfs4_free_lock_state(data
->server
, data
->lsp
);
6631 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6632 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6633 .rpc_call_done
= nfs4_release_lockowner_done
,
6634 .rpc_release
= nfs4_release_lockowner_release
,
6638 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6640 struct nfs_release_lockowner_data
*data
;
6641 struct rpc_message msg
= {
6642 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6645 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6648 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6652 data
->server
= server
;
6653 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6654 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6655 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6657 msg
.rpc_argp
= &data
->args
;
6658 msg
.rpc_resp
= &data
->res
;
6659 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6660 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6663 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6665 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6666 struct dentry
*unused
, struct inode
*inode
,
6667 const char *key
, const void *buf
,
6668 size_t buflen
, int flags
)
6670 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6673 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6674 struct dentry
*unused
, struct inode
*inode
,
6675 const char *key
, void *buf
, size_t buflen
)
6677 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6680 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6682 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6685 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6687 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6688 struct dentry
*unused
, struct inode
*inode
,
6689 const char *key
, const void *buf
,
6690 size_t buflen
, int flags
)
6692 if (security_ismaclabel(key
))
6693 return nfs4_set_security_label(inode
, buf
, buflen
);
6698 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6699 struct dentry
*unused
, struct inode
*inode
,
6700 const char *key
, void *buf
, size_t buflen
)
6702 if (security_ismaclabel(key
))
6703 return nfs4_get_security_label(inode
, buf
, buflen
);
6708 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6712 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6713 len
= security_inode_listsecurity(inode
, list
, list_len
);
6714 if (list_len
&& len
> list_len
)
6720 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6721 .prefix
= XATTR_SECURITY_PREFIX
,
6722 .get
= nfs4_xattr_get_nfs4_label
,
6723 .set
= nfs4_xattr_set_nfs4_label
,
6729 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6737 * nfs_fhget will use either the mounted_on_fileid or the fileid
6739 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6741 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6742 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6743 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6744 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6747 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6748 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6749 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6753 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6754 const struct qstr
*name
,
6755 struct nfs4_fs_locations
*fs_locations
,
6758 struct nfs_server
*server
= NFS_SERVER(dir
);
6760 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6762 struct nfs4_fs_locations_arg args
= {
6763 .dir_fh
= NFS_FH(dir
),
6768 struct nfs4_fs_locations_res res
= {
6769 .fs_locations
= fs_locations
,
6771 struct rpc_message msg
= {
6772 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6778 dprintk("%s: start\n", __func__
);
6780 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6781 * is not supported */
6782 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6783 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6785 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6787 nfs_fattr_init(&fs_locations
->fattr
);
6788 fs_locations
->server
= server
;
6789 fs_locations
->nlocations
= 0;
6790 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6791 dprintk("%s: returned status = %d\n", __func__
, status
);
6795 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6796 const struct qstr
*name
,
6797 struct nfs4_fs_locations
*fs_locations
,
6800 struct nfs4_exception exception
= { };
6803 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6804 fs_locations
, page
);
6805 trace_nfs4_get_fs_locations(dir
, name
, err
);
6806 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6808 } while (exception
.retry
);
6813 * This operation also signals the server that this client is
6814 * performing migration recovery. The server can stop returning
6815 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6816 * appended to this compound to identify the client ID which is
6817 * performing recovery.
6819 static int _nfs40_proc_get_locations(struct inode
*inode
,
6820 struct nfs4_fs_locations
*locations
,
6821 struct page
*page
, struct rpc_cred
*cred
)
6823 struct nfs_server
*server
= NFS_SERVER(inode
);
6824 struct rpc_clnt
*clnt
= server
->client
;
6826 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6828 struct nfs4_fs_locations_arg args
= {
6829 .clientid
= server
->nfs_client
->cl_clientid
,
6830 .fh
= NFS_FH(inode
),
6833 .migration
= 1, /* skip LOOKUP */
6834 .renew
= 1, /* append RENEW */
6836 struct nfs4_fs_locations_res res
= {
6837 .fs_locations
= locations
,
6841 struct rpc_message msg
= {
6842 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6847 unsigned long now
= jiffies
;
6850 nfs_fattr_init(&locations
->fattr
);
6851 locations
->server
= server
;
6852 locations
->nlocations
= 0;
6854 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6855 nfs4_set_sequence_privileged(&args
.seq_args
);
6856 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6857 &args
.seq_args
, &res
.seq_res
);
6861 renew_lease(server
, now
);
6865 #ifdef CONFIG_NFS_V4_1
6868 * This operation also signals the server that this client is
6869 * performing migration recovery. The server can stop asserting
6870 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6871 * performing this operation is identified in the SEQUENCE
6872 * operation in this compound.
6874 * When the client supports GETATTR(fs_locations_info), it can
6875 * be plumbed in here.
6877 static int _nfs41_proc_get_locations(struct inode
*inode
,
6878 struct nfs4_fs_locations
*locations
,
6879 struct page
*page
, struct rpc_cred
*cred
)
6881 struct nfs_server
*server
= NFS_SERVER(inode
);
6882 struct rpc_clnt
*clnt
= server
->client
;
6884 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6886 struct nfs4_fs_locations_arg args
= {
6887 .fh
= NFS_FH(inode
),
6890 .migration
= 1, /* skip LOOKUP */
6892 struct nfs4_fs_locations_res res
= {
6893 .fs_locations
= locations
,
6896 struct rpc_message msg
= {
6897 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6904 nfs_fattr_init(&locations
->fattr
);
6905 locations
->server
= server
;
6906 locations
->nlocations
= 0;
6908 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6909 nfs4_set_sequence_privileged(&args
.seq_args
);
6910 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6911 &args
.seq_args
, &res
.seq_res
);
6912 if (status
== NFS4_OK
&&
6913 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6914 status
= -NFS4ERR_LEASE_MOVED
;
6918 #endif /* CONFIG_NFS_V4_1 */
6921 * nfs4_proc_get_locations - discover locations for a migrated FSID
6922 * @inode: inode on FSID that is migrating
6923 * @locations: result of query
6925 * @cred: credential to use for this operation
6927 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6928 * operation failed, or a negative errno if a local error occurred.
6930 * On success, "locations" is filled in, but if the server has
6931 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6934 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6935 * from this client that require migration recovery.
6937 int nfs4_proc_get_locations(struct inode
*inode
,
6938 struct nfs4_fs_locations
*locations
,
6939 struct page
*page
, struct rpc_cred
*cred
)
6941 struct nfs_server
*server
= NFS_SERVER(inode
);
6942 struct nfs_client
*clp
= server
->nfs_client
;
6943 const struct nfs4_mig_recovery_ops
*ops
=
6944 clp
->cl_mvops
->mig_recovery_ops
;
6945 struct nfs4_exception exception
= { };
6948 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6949 (unsigned long long)server
->fsid
.major
,
6950 (unsigned long long)server
->fsid
.minor
,
6952 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6955 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6956 if (status
!= -NFS4ERR_DELAY
)
6958 nfs4_handle_exception(server
, status
, &exception
);
6959 } while (exception
.retry
);
6964 * This operation also signals the server that this client is
6965 * performing "lease moved" recovery. The server can stop
6966 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6967 * is appended to this compound to identify the client ID which is
6968 * performing recovery.
6970 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6972 struct nfs_server
*server
= NFS_SERVER(inode
);
6973 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
6974 struct rpc_clnt
*clnt
= server
->client
;
6975 struct nfs4_fsid_present_arg args
= {
6976 .fh
= NFS_FH(inode
),
6977 .clientid
= clp
->cl_clientid
,
6978 .renew
= 1, /* append RENEW */
6980 struct nfs4_fsid_present_res res
= {
6983 struct rpc_message msg
= {
6984 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6989 unsigned long now
= jiffies
;
6992 res
.fh
= nfs_alloc_fhandle();
6996 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6997 nfs4_set_sequence_privileged(&args
.seq_args
);
6998 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6999 &args
.seq_args
, &res
.seq_res
);
7000 nfs_free_fhandle(res
.fh
);
7004 do_renew_lease(clp
, now
);
7008 #ifdef CONFIG_NFS_V4_1
7011 * This operation also signals the server that this client is
7012 * performing "lease moved" recovery. The server can stop asserting
7013 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7014 * this operation is identified in the SEQUENCE operation in this
7017 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7019 struct nfs_server
*server
= NFS_SERVER(inode
);
7020 struct rpc_clnt
*clnt
= server
->client
;
7021 struct nfs4_fsid_present_arg args
= {
7022 .fh
= NFS_FH(inode
),
7024 struct nfs4_fsid_present_res res
= {
7026 struct rpc_message msg
= {
7027 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7034 res
.fh
= nfs_alloc_fhandle();
7038 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7039 nfs4_set_sequence_privileged(&args
.seq_args
);
7040 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7041 &args
.seq_args
, &res
.seq_res
);
7042 nfs_free_fhandle(res
.fh
);
7043 if (status
== NFS4_OK
&&
7044 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
7045 status
= -NFS4ERR_LEASE_MOVED
;
7049 #endif /* CONFIG_NFS_V4_1 */
7052 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7053 * @inode: inode on FSID to check
7054 * @cred: credential to use for this operation
7056 * Server indicates whether the FSID is present, moved, or not
7057 * recognized. This operation is necessary to clear a LEASE_MOVED
7058 * condition for this client ID.
7060 * Returns NFS4_OK if the FSID is present on this server,
7061 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7062 * NFS4ERR code if some error occurred on the server, or a
7063 * negative errno if a local failure occurred.
7065 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7067 struct nfs_server
*server
= NFS_SERVER(inode
);
7068 struct nfs_client
*clp
= server
->nfs_client
;
7069 const struct nfs4_mig_recovery_ops
*ops
=
7070 clp
->cl_mvops
->mig_recovery_ops
;
7071 struct nfs4_exception exception
= { };
7074 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
7075 (unsigned long long)server
->fsid
.major
,
7076 (unsigned long long)server
->fsid
.minor
,
7078 nfs_display_fhandle(NFS_FH(inode
), __func__
);
7081 status
= ops
->fsid_present(inode
, cred
);
7082 if (status
!= -NFS4ERR_DELAY
)
7084 nfs4_handle_exception(server
, status
, &exception
);
7085 } while (exception
.retry
);
7090 * If 'use_integrity' is true and the state managment nfs_client
7091 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7092 * and the machine credential as per RFC3530bis and RFC5661 Security
7093 * Considerations sections. Otherwise, just use the user cred with the
7094 * filesystem's rpc_client.
7096 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7099 struct nfs4_secinfo_arg args
= {
7100 .dir_fh
= NFS_FH(dir
),
7103 struct nfs4_secinfo_res res
= {
7106 struct rpc_message msg
= {
7107 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
7111 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
7112 struct rpc_cred
*cred
= NULL
;
7114 if (use_integrity
) {
7115 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
7116 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
7117 msg
.rpc_cred
= cred
;
7120 dprintk("NFS call secinfo %s\n", name
->name
);
7122 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
7123 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
7125 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
7127 dprintk("NFS reply secinfo: %d\n", status
);
7135 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
7136 struct nfs4_secinfo_flavors
*flavors
)
7138 struct nfs4_exception exception
= { };
7141 err
= -NFS4ERR_WRONGSEC
;
7143 /* try to use integrity protection with machine cred */
7144 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
7145 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
7148 * if unable to use integrity protection, or SECINFO with
7149 * integrity protection returns NFS4ERR_WRONGSEC (which is
7150 * disallowed by spec, but exists in deployed servers) use
7151 * the current filesystem's rpc_client and the user cred.
7153 if (err
== -NFS4ERR_WRONGSEC
)
7154 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
7156 trace_nfs4_secinfo(dir
, name
, err
);
7157 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
7159 } while (exception
.retry
);
7163 #ifdef CONFIG_NFS_V4_1
7165 * Check the exchange flags returned by the server for invalid flags, having
7166 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7169 static int nfs4_check_cl_exchange_flags(u32 flags
)
7171 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
7173 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
7174 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
7176 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
7180 return -NFS4ERR_INVAL
;
7184 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
7185 struct nfs41_server_scope
*b
)
7187 if (a
->server_scope_sz
== b
->server_scope_sz
&&
7188 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
7195 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
7199 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
7200 .rpc_call_done
= &nfs4_bind_one_conn_to_session_done
,
7204 * nfs4_proc_bind_one_conn_to_session()
7206 * The 4.1 client currently uses the same TCP connection for the
7207 * fore and backchannel.
7210 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
7211 struct rpc_xprt
*xprt
,
7212 struct nfs_client
*clp
,
7213 struct rpc_cred
*cred
)
7216 struct nfs41_bind_conn_to_session_args args
= {
7218 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
7220 struct nfs41_bind_conn_to_session_res res
;
7221 struct rpc_message msg
= {
7223 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
7228 struct rpc_task_setup task_setup_data
= {
7231 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
7232 .rpc_message
= &msg
,
7233 .flags
= RPC_TASK_TIMEOUT
,
7235 struct rpc_task
*task
;
7237 dprintk("--> %s\n", __func__
);
7239 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
7240 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
7241 args
.dir
= NFS4_CDFC4_FORE
;
7243 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7244 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
7245 args
.dir
= NFS4_CDFC4_FORE
;
7247 task
= rpc_run_task(&task_setup_data
);
7248 if (!IS_ERR(task
)) {
7249 status
= task
->tk_status
;
7252 status
= PTR_ERR(task
);
7253 trace_nfs4_bind_conn_to_session(clp
, status
);
7255 if (memcmp(res
.sessionid
.data
,
7256 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
7257 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
7261 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
7262 dprintk("NFS: %s: Unexpected direction from server\n",
7267 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
7268 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7275 dprintk("<-- %s status= %d\n", __func__
, status
);
7279 struct rpc_bind_conn_calldata
{
7280 struct nfs_client
*clp
;
7281 struct rpc_cred
*cred
;
7285 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
7286 struct rpc_xprt
*xprt
,
7289 struct rpc_bind_conn_calldata
*p
= calldata
;
7291 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
7294 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7296 struct rpc_bind_conn_calldata data
= {
7300 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
7301 nfs4_proc_bind_conn_to_session_callback
, &data
);
7305 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7306 * and operations we'd like to see to enable certain features in the allow map
7308 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
7309 .how
= SP4_MACH_CRED
,
7310 .enforce
.u
.words
= {
7311 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7312 1 << (OP_EXCHANGE_ID
- 32) |
7313 1 << (OP_CREATE_SESSION
- 32) |
7314 1 << (OP_DESTROY_SESSION
- 32) |
7315 1 << (OP_DESTROY_CLIENTID
- 32)
7318 [0] = 1 << (OP_CLOSE
) |
7319 1 << (OP_OPEN_DOWNGRADE
) |
7321 1 << (OP_DELEGRETURN
) |
7323 [1] = 1 << (OP_SECINFO
- 32) |
7324 1 << (OP_SECINFO_NO_NAME
- 32) |
7325 1 << (OP_LAYOUTRETURN
- 32) |
7326 1 << (OP_TEST_STATEID
- 32) |
7327 1 << (OP_FREE_STATEID
- 32) |
7328 1 << (OP_WRITE
- 32)
7333 * Select the state protection mode for client `clp' given the server results
7334 * from exchange_id in `sp'.
7336 * Returns 0 on success, negative errno otherwise.
7338 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
7339 struct nfs41_state_protection
*sp
)
7341 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
7342 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7343 1 << (OP_EXCHANGE_ID
- 32) |
7344 1 << (OP_CREATE_SESSION
- 32) |
7345 1 << (OP_DESTROY_SESSION
- 32) |
7346 1 << (OP_DESTROY_CLIENTID
- 32)
7350 if (sp
->how
== SP4_MACH_CRED
) {
7351 /* Print state protect result */
7352 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
7353 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
7354 if (test_bit(i
, sp
->enforce
.u
.longs
))
7355 dfprintk(MOUNT
, " enforce op %d\n", i
);
7356 if (test_bit(i
, sp
->allow
.u
.longs
))
7357 dfprintk(MOUNT
, " allow op %d\n", i
);
7360 /* make sure nothing is on enforce list that isn't supported */
7361 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
7362 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
7363 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7369 * Minimal mode - state operations are allowed to use machine
7370 * credential. Note this already happens by default, so the
7371 * client doesn't have to do anything more than the negotiation.
7373 * NOTE: we don't care if EXCHANGE_ID is in the list -
7374 * we're already using the machine cred for exchange_id
7375 * and will never use a different cred.
7377 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
7378 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
7379 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
7380 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
7381 dfprintk(MOUNT
, "sp4_mach_cred:\n");
7382 dfprintk(MOUNT
, " minimal mode enabled\n");
7383 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
7385 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7389 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7390 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7391 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7392 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7393 dfprintk(MOUNT
, " cleanup mode enabled\n");
7394 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
7397 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7398 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7399 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
7400 &clp
->cl_sp4_flags
);
7403 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7404 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7405 dfprintk(MOUNT
, " secinfo mode enabled\n");
7406 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
7409 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7410 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7411 dfprintk(MOUNT
, " stateid mode enabled\n");
7412 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
7415 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7416 dfprintk(MOUNT
, " write mode enabled\n");
7417 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
7420 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7421 dfprintk(MOUNT
, " commit mode enabled\n");
7422 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
7429 struct nfs41_exchange_id_data
{
7430 struct nfs41_exchange_id_res res
;
7431 struct nfs41_exchange_id_args args
;
7432 struct rpc_xprt
*xprt
;
7436 static void nfs4_exchange_id_done(struct rpc_task
*task
, void *data
)
7438 struct nfs41_exchange_id_data
*cdata
=
7439 (struct nfs41_exchange_id_data
*)data
;
7440 struct nfs_client
*clp
= cdata
->args
.client
;
7441 int status
= task
->tk_status
;
7443 trace_nfs4_exchange_id(clp
, status
);
7446 status
= nfs4_check_cl_exchange_flags(cdata
->res
.flags
);
7448 if (cdata
->xprt
&& status
== 0) {
7449 status
= nfs4_detect_session_trunking(clp
, &cdata
->res
,
7455 status
= nfs4_sp4_select_mode(clp
, &cdata
->res
.state_protect
);
7458 clp
->cl_clientid
= cdata
->res
.clientid
;
7459 clp
->cl_exchange_flags
= cdata
->res
.flags
;
7460 /* Client ID is not confirmed */
7461 if (!(cdata
->res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
)) {
7462 clear_bit(NFS4_SESSION_ESTABLISHED
,
7463 &clp
->cl_session
->session_state
);
7464 clp
->cl_seqid
= cdata
->res
.seqid
;
7467 kfree(clp
->cl_serverowner
);
7468 clp
->cl_serverowner
= cdata
->res
.server_owner
;
7469 cdata
->res
.server_owner
= NULL
;
7471 /* use the most recent implementation id */
7472 kfree(clp
->cl_implid
);
7473 clp
->cl_implid
= cdata
->res
.impl_id
;
7474 cdata
->res
.impl_id
= NULL
;
7476 if (clp
->cl_serverscope
!= NULL
&&
7477 !nfs41_same_server_scope(clp
->cl_serverscope
,
7478 cdata
->res
.server_scope
)) {
7479 dprintk("%s: server_scope mismatch detected\n",
7481 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7482 kfree(clp
->cl_serverscope
);
7483 clp
->cl_serverscope
= NULL
;
7486 if (clp
->cl_serverscope
== NULL
) {
7487 clp
->cl_serverscope
= cdata
->res
.server_scope
;
7488 cdata
->res
.server_scope
= NULL
;
7490 /* Save the EXCHANGE_ID verifier session trunk tests */
7491 memcpy(clp
->cl_confirm
.data
, cdata
->args
.verifier
->data
,
7492 sizeof(clp
->cl_confirm
.data
));
7495 cdata
->rpc_status
= status
;
7499 static void nfs4_exchange_id_release(void *data
)
7501 struct nfs41_exchange_id_data
*cdata
=
7502 (struct nfs41_exchange_id_data
*)data
;
7504 nfs_put_client(cdata
->args
.client
);
7506 xprt_put(cdata
->xprt
);
7507 rpc_clnt_xprt_switch_put(cdata
->args
.client
->cl_rpcclient
);
7509 kfree(cdata
->res
.impl_id
);
7510 kfree(cdata
->res
.server_scope
);
7511 kfree(cdata
->res
.server_owner
);
7515 static const struct rpc_call_ops nfs4_exchange_id_call_ops
= {
7516 .rpc_call_done
= nfs4_exchange_id_done
,
7517 .rpc_release
= nfs4_exchange_id_release
,
7521 * _nfs4_proc_exchange_id()
7523 * Wrapper for EXCHANGE_ID operation.
7525 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7526 u32 sp4_how
, struct rpc_xprt
*xprt
)
7528 nfs4_verifier verifier
;
7529 struct rpc_message msg
= {
7530 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7533 struct rpc_task_setup task_setup_data
= {
7534 .rpc_client
= clp
->cl_rpcclient
,
7535 .callback_ops
= &nfs4_exchange_id_call_ops
,
7536 .rpc_message
= &msg
,
7537 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7539 struct nfs41_exchange_id_data
*calldata
;
7540 struct rpc_task
*task
;
7543 if (!atomic_inc_not_zero(&clp
->cl_count
))
7547 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7552 nfs4_init_boot_verifier(clp
, &verifier
);
7554 status
= nfs4_init_uniform_client_string(clp
);
7558 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7559 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
7562 calldata
->res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7565 if (unlikely(calldata
->res
.server_owner
== NULL
))
7568 calldata
->res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7570 if (unlikely(calldata
->res
.server_scope
== NULL
))
7571 goto out_server_owner
;
7573 calldata
->res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7574 if (unlikely(calldata
->res
.impl_id
== NULL
))
7575 goto out_server_scope
;
7579 calldata
->args
.state_protect
.how
= SP4_NONE
;
7583 calldata
->args
.state_protect
= nfs4_sp4_mach_cred_request
;
7593 calldata
->xprt
= xprt
;
7594 task_setup_data
.rpc_xprt
= xprt
;
7595 task_setup_data
.flags
=
7596 RPC_TASK_SOFT
|RPC_TASK_SOFTCONN
|RPC_TASK_ASYNC
;
7597 calldata
->args
.verifier
= &clp
->cl_confirm
;
7599 calldata
->args
.verifier
= &verifier
;
7601 calldata
->args
.client
= clp
;
7602 #ifdef CONFIG_NFS_V4_1_MIGRATION
7603 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7604 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
7605 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
7607 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7608 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
7610 msg
.rpc_argp
= &calldata
->args
;
7611 msg
.rpc_resp
= &calldata
->res
;
7612 task_setup_data
.callback_data
= calldata
;
7614 task
= rpc_run_task(&task_setup_data
);
7616 status
= PTR_ERR(task
);
7621 status
= rpc_wait_for_completion_task(task
);
7623 status
= calldata
->rpc_status
;
7624 } else /* session trunking test */
7625 status
= calldata
->rpc_status
;
7629 if (clp
->cl_implid
!= NULL
)
7630 dprintk("NFS reply exchange_id: Server Implementation ID: "
7631 "domain: %s, name: %s, date: %llu,%u\n",
7632 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
7633 clp
->cl_implid
->date
.seconds
,
7634 clp
->cl_implid
->date
.nseconds
);
7635 dprintk("NFS reply exchange_id: %d\n", status
);
7639 kfree(calldata
->res
.impl_id
);
7641 kfree(calldata
->res
.server_scope
);
7643 kfree(calldata
->res
.server_owner
);
7650 * nfs4_proc_exchange_id()
7652 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7654 * Since the clientid has expired, all compounds using sessions
7655 * associated with the stale clientid will be returning
7656 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7657 * be in some phase of session reset.
7659 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7661 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7663 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7666 /* try SP4_MACH_CRED if krb5i/p */
7667 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7668 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7669 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
, NULL
);
7675 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
, NULL
);
7679 * nfs4_test_session_trunk
7681 * This is an add_xprt_test() test function called from
7682 * rpc_clnt_setup_test_and_add_xprt.
7684 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7685 * and is dereferrenced in nfs4_exchange_id_release
7687 * Upon success, add the new transport to the rpc_clnt
7689 * @clnt: struct rpc_clnt to get new transport
7690 * @xprt: the rpc_xprt to test
7691 * @data: call data for _nfs4_proc_exchange_id.
7693 int nfs4_test_session_trunk(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
,
7696 struct nfs4_add_xprt_data
*adata
= (struct nfs4_add_xprt_data
*)data
;
7699 dprintk("--> %s try %s\n", __func__
,
7700 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
7702 sp4_how
= (adata
->clp
->cl_sp4_flags
== 0 ? SP4_NONE
: SP4_MACH_CRED
);
7704 /* Test connection for session trunking. Async exchange_id call */
7705 return _nfs4_proc_exchange_id(adata
->clp
, adata
->cred
, sp4_how
, xprt
);
7707 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk
);
7709 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7710 struct rpc_cred
*cred
)
7712 struct rpc_message msg
= {
7713 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7719 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7720 trace_nfs4_destroy_clientid(clp
, status
);
7722 dprintk("NFS: Got error %d from the server %s on "
7723 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7727 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7728 struct rpc_cred
*cred
)
7733 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7734 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7736 case -NFS4ERR_DELAY
:
7737 case -NFS4ERR_CLIENTID_BUSY
:
7747 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7749 struct rpc_cred
*cred
;
7752 if (clp
->cl_mvops
->minor_version
< 1)
7754 if (clp
->cl_exchange_flags
== 0)
7756 if (clp
->cl_preserve_clid
)
7758 cred
= nfs4_get_clid_cred(clp
);
7759 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7764 case -NFS4ERR_STALE_CLIENTID
:
7765 clp
->cl_exchange_flags
= 0;
7771 struct nfs4_get_lease_time_data
{
7772 struct nfs4_get_lease_time_args
*args
;
7773 struct nfs4_get_lease_time_res
*res
;
7774 struct nfs_client
*clp
;
7777 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7780 struct nfs4_get_lease_time_data
*data
=
7781 (struct nfs4_get_lease_time_data
*)calldata
;
7783 dprintk("--> %s\n", __func__
);
7784 /* just setup sequence, do not trigger session recovery
7785 since we're invoked within one */
7786 nfs41_setup_sequence(data
->clp
->cl_session
,
7787 &data
->args
->la_seq_args
,
7788 &data
->res
->lr_seq_res
,
7790 dprintk("<-- %s\n", __func__
);
7794 * Called from nfs4_state_manager thread for session setup, so don't recover
7795 * from sequence operation or clientid errors.
7797 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7799 struct nfs4_get_lease_time_data
*data
=
7800 (struct nfs4_get_lease_time_data
*)calldata
;
7802 dprintk("--> %s\n", __func__
);
7803 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7805 switch (task
->tk_status
) {
7806 case -NFS4ERR_DELAY
:
7807 case -NFS4ERR_GRACE
:
7808 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7809 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7810 task
->tk_status
= 0;
7812 case -NFS4ERR_RETRY_UNCACHED_REP
:
7813 rpc_restart_call_prepare(task
);
7816 dprintk("<-- %s\n", __func__
);
7819 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7820 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7821 .rpc_call_done
= nfs4_get_lease_time_done
,
7824 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7826 struct rpc_task
*task
;
7827 struct nfs4_get_lease_time_args args
;
7828 struct nfs4_get_lease_time_res res
= {
7829 .lr_fsinfo
= fsinfo
,
7831 struct nfs4_get_lease_time_data data
= {
7836 struct rpc_message msg
= {
7837 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7841 struct rpc_task_setup task_setup
= {
7842 .rpc_client
= clp
->cl_rpcclient
,
7843 .rpc_message
= &msg
,
7844 .callback_ops
= &nfs4_get_lease_time_ops
,
7845 .callback_data
= &data
,
7846 .flags
= RPC_TASK_TIMEOUT
,
7850 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7851 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7852 dprintk("--> %s\n", __func__
);
7853 task
= rpc_run_task(&task_setup
);
7856 status
= PTR_ERR(task
);
7858 status
= task
->tk_status
;
7861 dprintk("<-- %s return %d\n", __func__
, status
);
7867 * Initialize the values to be used by the client in CREATE_SESSION
7868 * If nfs4_init_session set the fore channel request and response sizes,
7871 * Set the back channel max_resp_sz_cached to zero to force the client to
7872 * always set csa_cachethis to FALSE because the current implementation
7873 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7875 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7876 struct rpc_clnt
*clnt
)
7878 unsigned int max_rqst_sz
, max_resp_sz
;
7879 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7881 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7882 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7884 /* Fore channel attributes */
7885 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7886 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7887 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7888 args
->fc_attrs
.max_reqs
= max_session_slots
;
7890 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7891 "max_ops=%u max_reqs=%u\n",
7893 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7894 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7896 /* Back channel attributes */
7897 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7898 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7899 args
->bc_attrs
.max_resp_sz_cached
= 0;
7900 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7901 args
->bc_attrs
.max_reqs
= min_t(unsigned short, max_session_cb_slots
, 1);
7903 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7904 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7906 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7907 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7908 args
->bc_attrs
.max_reqs
);
7911 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7912 struct nfs41_create_session_res
*res
)
7914 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7915 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7917 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7920 * Our requested max_ops is the minimum we need; we're not
7921 * prepared to break up compounds into smaller pieces than that.
7922 * So, no point even trying to continue if the server won't
7925 if (rcvd
->max_ops
< sent
->max_ops
)
7927 if (rcvd
->max_reqs
== 0)
7929 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7930 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7934 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7935 struct nfs41_create_session_res
*res
)
7937 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7938 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7940 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7942 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7944 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7946 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7948 if (rcvd
->max_ops
> sent
->max_ops
)
7950 if (rcvd
->max_reqs
> sent
->max_reqs
)
7956 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7957 struct nfs41_create_session_res
*res
)
7961 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
7964 return nfs4_verify_back_channel_attrs(args
, res
);
7967 static void nfs4_update_session(struct nfs4_session
*session
,
7968 struct nfs41_create_session_res
*res
)
7970 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
7971 /* Mark client id and session as being confirmed */
7972 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
7973 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
7974 session
->flags
= res
->flags
;
7975 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
7976 if (res
->flags
& SESSION4_BACK_CHAN
)
7977 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
7978 sizeof(session
->bc_attrs
));
7981 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
7982 struct rpc_cred
*cred
)
7984 struct nfs4_session
*session
= clp
->cl_session
;
7985 struct nfs41_create_session_args args
= {
7987 .clientid
= clp
->cl_clientid
,
7988 .seqid
= clp
->cl_seqid
,
7989 .cb_program
= NFS4_CALLBACK
,
7991 struct nfs41_create_session_res res
;
7993 struct rpc_message msg
= {
7994 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
8001 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
8002 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
8004 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8005 trace_nfs4_create_session(clp
, status
);
8008 case -NFS4ERR_STALE_CLIENTID
:
8009 case -NFS4ERR_DELAY
:
8018 /* Verify the session's negotiated channel_attrs values */
8019 status
= nfs4_verify_channel_attrs(&args
, &res
);
8020 /* Increment the clientid slot sequence id */
8023 nfs4_update_session(session
, &res
);
8030 * Issues a CREATE_SESSION operation to the server.
8031 * It is the responsibility of the caller to verify the session is
8032 * expired before calling this routine.
8034 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8038 struct nfs4_session
*session
= clp
->cl_session
;
8040 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
8042 status
= _nfs4_proc_create_session(clp
, cred
);
8046 /* Init or reset the session slot tables */
8047 status
= nfs4_setup_session_slot_tables(session
);
8048 dprintk("slot table setup returned %d\n", status
);
8052 ptr
= (unsigned *)&session
->sess_id
.data
[0];
8053 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
8054 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
8056 dprintk("<-- %s\n", __func__
);
8061 * Issue the over-the-wire RPC DESTROY_SESSION.
8062 * The caller must serialize access to this routine.
8064 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
8065 struct rpc_cred
*cred
)
8067 struct rpc_message msg
= {
8068 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
8069 .rpc_argp
= session
,
8074 dprintk("--> nfs4_proc_destroy_session\n");
8076 /* session is still being setup */
8077 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
8080 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8081 trace_nfs4_destroy_session(session
->clp
, status
);
8084 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8085 "Session has been destroyed regardless...\n", status
);
8087 dprintk("<-- nfs4_proc_destroy_session\n");
8092 * Renew the cl_session lease.
8094 struct nfs4_sequence_data
{
8095 struct nfs_client
*clp
;
8096 struct nfs4_sequence_args args
;
8097 struct nfs4_sequence_res res
;
8100 static void nfs41_sequence_release(void *data
)
8102 struct nfs4_sequence_data
*calldata
= data
;
8103 struct nfs_client
*clp
= calldata
->clp
;
8105 if (atomic_read(&clp
->cl_count
) > 1)
8106 nfs4_schedule_state_renewal(clp
);
8107 nfs_put_client(clp
);
8111 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8113 switch(task
->tk_status
) {
8114 case -NFS4ERR_DELAY
:
8115 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8118 nfs4_schedule_lease_recovery(clp
);
8123 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
8125 struct nfs4_sequence_data
*calldata
= data
;
8126 struct nfs_client
*clp
= calldata
->clp
;
8128 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
8131 trace_nfs4_sequence(clp
, task
->tk_status
);
8132 if (task
->tk_status
< 0) {
8133 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
8134 if (atomic_read(&clp
->cl_count
) == 1)
8137 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
8138 rpc_restart_call_prepare(task
);
8142 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
8144 dprintk("<-- %s\n", __func__
);
8147 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
8149 struct nfs4_sequence_data
*calldata
= data
;
8150 struct nfs_client
*clp
= calldata
->clp
;
8151 struct nfs4_sequence_args
*args
;
8152 struct nfs4_sequence_res
*res
;
8154 args
= task
->tk_msg
.rpc_argp
;
8155 res
= task
->tk_msg
.rpc_resp
;
8157 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
8160 static const struct rpc_call_ops nfs41_sequence_ops
= {
8161 .rpc_call_done
= nfs41_sequence_call_done
,
8162 .rpc_call_prepare
= nfs41_sequence_prepare
,
8163 .rpc_release
= nfs41_sequence_release
,
8166 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
8167 struct rpc_cred
*cred
,
8170 struct nfs4_sequence_data
*calldata
;
8171 struct rpc_message msg
= {
8172 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
8175 struct rpc_task_setup task_setup_data
= {
8176 .rpc_client
= clp
->cl_rpcclient
,
8177 .rpc_message
= &msg
,
8178 .callback_ops
= &nfs41_sequence_ops
,
8179 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
8182 if (!atomic_inc_not_zero(&clp
->cl_count
))
8183 return ERR_PTR(-EIO
);
8184 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8185 if (calldata
== NULL
) {
8186 nfs_put_client(clp
);
8187 return ERR_PTR(-ENOMEM
);
8189 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
8191 nfs4_set_sequence_privileged(&calldata
->args
);
8192 msg
.rpc_argp
= &calldata
->args
;
8193 msg
.rpc_resp
= &calldata
->res
;
8194 calldata
->clp
= clp
;
8195 task_setup_data
.callback_data
= calldata
;
8197 return rpc_run_task(&task_setup_data
);
8200 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
8202 struct rpc_task
*task
;
8205 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
8207 task
= _nfs41_proc_sequence(clp
, cred
, false);
8209 ret
= PTR_ERR(task
);
8211 rpc_put_task_async(task
);
8212 dprintk("<-- %s status=%d\n", __func__
, ret
);
8216 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8218 struct rpc_task
*task
;
8221 task
= _nfs41_proc_sequence(clp
, cred
, true);
8223 ret
= PTR_ERR(task
);
8226 ret
= rpc_wait_for_completion_task(task
);
8228 ret
= task
->tk_status
;
8231 dprintk("<-- %s status=%d\n", __func__
, ret
);
8235 struct nfs4_reclaim_complete_data
{
8236 struct nfs_client
*clp
;
8237 struct nfs41_reclaim_complete_args arg
;
8238 struct nfs41_reclaim_complete_res res
;
8241 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
8243 struct nfs4_reclaim_complete_data
*calldata
= data
;
8245 nfs41_setup_sequence(calldata
->clp
->cl_session
,
8246 &calldata
->arg
.seq_args
,
8247 &calldata
->res
.seq_res
,
8251 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8253 switch(task
->tk_status
) {
8255 case -NFS4ERR_COMPLETE_ALREADY
:
8256 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
8258 case -NFS4ERR_DELAY
:
8259 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8261 case -NFS4ERR_RETRY_UNCACHED_REP
:
8264 nfs4_schedule_lease_recovery(clp
);
8269 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
8271 struct nfs4_reclaim_complete_data
*calldata
= data
;
8272 struct nfs_client
*clp
= calldata
->clp
;
8273 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
8275 dprintk("--> %s\n", __func__
);
8276 if (!nfs41_sequence_done(task
, res
))
8279 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
8280 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
8281 rpc_restart_call_prepare(task
);
8284 dprintk("<-- %s\n", __func__
);
8287 static void nfs4_free_reclaim_complete_data(void *data
)
8289 struct nfs4_reclaim_complete_data
*calldata
= data
;
8294 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
8295 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
8296 .rpc_call_done
= nfs4_reclaim_complete_done
,
8297 .rpc_release
= nfs4_free_reclaim_complete_data
,
8301 * Issue a global reclaim complete.
8303 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
8304 struct rpc_cred
*cred
)
8306 struct nfs4_reclaim_complete_data
*calldata
;
8307 struct rpc_task
*task
;
8308 struct rpc_message msg
= {
8309 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
8312 struct rpc_task_setup task_setup_data
= {
8313 .rpc_client
= clp
->cl_rpcclient
,
8314 .rpc_message
= &msg
,
8315 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
8316 .flags
= RPC_TASK_ASYNC
,
8318 int status
= -ENOMEM
;
8320 dprintk("--> %s\n", __func__
);
8321 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8322 if (calldata
== NULL
)
8324 calldata
->clp
= clp
;
8325 calldata
->arg
.one_fs
= 0;
8327 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
8328 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
8329 msg
.rpc_argp
= &calldata
->arg
;
8330 msg
.rpc_resp
= &calldata
->res
;
8331 task_setup_data
.callback_data
= calldata
;
8332 task
= rpc_run_task(&task_setup_data
);
8334 status
= PTR_ERR(task
);
8337 status
= nfs4_wait_for_completion_rpc_task(task
);
8339 status
= task
->tk_status
;
8343 dprintk("<-- %s status=%d\n", __func__
, status
);
8348 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
8350 struct nfs4_layoutget
*lgp
= calldata
;
8351 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
8352 struct nfs4_session
*session
= nfs4_get_session(server
);
8354 dprintk("--> %s\n", __func__
);
8355 nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
8356 &lgp
->res
.seq_res
, task
);
8357 dprintk("<-- %s\n", __func__
);
8360 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
8362 struct nfs4_layoutget
*lgp
= calldata
;
8364 dprintk("--> %s\n", __func__
);
8365 nfs41_sequence_process(task
, &lgp
->res
.seq_res
);
8366 dprintk("<-- %s\n", __func__
);
8370 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
8371 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
8373 struct inode
*inode
= lgp
->args
.inode
;
8374 struct nfs_server
*server
= NFS_SERVER(inode
);
8375 struct pnfs_layout_hdr
*lo
;
8376 int nfs4err
= task
->tk_status
;
8377 int err
, status
= 0;
8380 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
8387 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8388 * on the file. set tk_status to -ENODATA to tell upper layer to
8391 case -NFS4ERR_LAYOUTUNAVAILABLE
:
8395 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8396 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8398 case -NFS4ERR_BADLAYOUT
:
8399 status
= -EOVERFLOW
;
8402 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8403 * (or clients) writing to the same RAID stripe except when
8404 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8406 * Treat it like we would RECALLCONFLICT -- we retry for a little
8407 * while, and then eventually give up.
8409 case -NFS4ERR_LAYOUTTRYLATER
:
8410 if (lgp
->args
.minlength
== 0) {
8411 status
= -EOVERFLOW
;
8416 case -NFS4ERR_RECALLCONFLICT
:
8417 status
= -ERECALLCONFLICT
;
8419 case -NFS4ERR_DELEG_REVOKED
:
8420 case -NFS4ERR_ADMIN_REVOKED
:
8421 case -NFS4ERR_EXPIRED
:
8422 case -NFS4ERR_BAD_STATEID
:
8423 exception
->timeout
= 0;
8424 spin_lock(&inode
->i_lock
);
8425 lo
= NFS_I(inode
)->layout
;
8426 /* If the open stateid was bad, then recover it. */
8427 if (!lo
|| test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) ||
8428 nfs4_stateid_match_other(&lgp
->args
.stateid
,
8429 &lgp
->args
.ctx
->state
->stateid
)) {
8430 spin_unlock(&inode
->i_lock
);
8431 exception
->state
= lgp
->args
.ctx
->state
;
8432 exception
->stateid
= &lgp
->args
.stateid
;
8437 * Mark the bad layout state as invalid, then retry
8439 pnfs_mark_layout_stateid_invalid(lo
, &head
);
8440 spin_unlock(&inode
->i_lock
);
8441 pnfs_free_lseg_list(&head
);
8446 err
= nfs4_handle_exception(server
, nfs4err
, exception
);
8448 if (exception
->retry
)
8454 dprintk("<-- %s\n", __func__
);
8458 static size_t max_response_pages(struct nfs_server
*server
)
8460 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
8461 return nfs_page_array_len(0, max_resp_sz
);
8464 static void nfs4_free_pages(struct page
**pages
, size_t size
)
8471 for (i
= 0; i
< size
; i
++) {
8474 __free_page(pages
[i
]);
8479 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
8481 struct page
**pages
;
8484 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
8486 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
8490 for (i
= 0; i
< size
; i
++) {
8491 pages
[i
] = alloc_page(gfp_flags
);
8493 dprintk("%s: failed to allocate page\n", __func__
);
8494 nfs4_free_pages(pages
, size
);
8502 static void nfs4_layoutget_release(void *calldata
)
8504 struct nfs4_layoutget
*lgp
= calldata
;
8505 struct inode
*inode
= lgp
->args
.inode
;
8506 struct nfs_server
*server
= NFS_SERVER(inode
);
8507 size_t max_pages
= max_response_pages(server
);
8509 dprintk("--> %s\n", __func__
);
8510 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8511 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8512 put_nfs_open_context(lgp
->args
.ctx
);
8514 dprintk("<-- %s\n", __func__
);
8517 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8518 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8519 .rpc_call_done
= nfs4_layoutget_done
,
8520 .rpc_release
= nfs4_layoutget_release
,
8523 struct pnfs_layout_segment
*
8524 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8526 struct inode
*inode
= lgp
->args
.inode
;
8527 struct nfs_server
*server
= NFS_SERVER(inode
);
8528 size_t max_pages
= max_response_pages(server
);
8529 struct rpc_task
*task
;
8530 struct rpc_message msg
= {
8531 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8532 .rpc_argp
= &lgp
->args
,
8533 .rpc_resp
= &lgp
->res
,
8534 .rpc_cred
= lgp
->cred
,
8536 struct rpc_task_setup task_setup_data
= {
8537 .rpc_client
= server
->client
,
8538 .rpc_message
= &msg
,
8539 .callback_ops
= &nfs4_layoutget_call_ops
,
8540 .callback_data
= lgp
,
8541 .flags
= RPC_TASK_ASYNC
,
8543 struct pnfs_layout_segment
*lseg
= NULL
;
8544 struct nfs4_exception exception
= {
8546 .timeout
= *timeout
,
8550 dprintk("--> %s\n", __func__
);
8552 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8553 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8555 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8556 if (!lgp
->args
.layout
.pages
) {
8557 nfs4_layoutget_release(lgp
);
8558 return ERR_PTR(-ENOMEM
);
8560 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8562 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8563 lgp
->res
.seq_res
.sr_slot
= NULL
;
8564 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8566 task
= rpc_run_task(&task_setup_data
);
8568 return ERR_CAST(task
);
8569 status
= nfs4_wait_for_completion_rpc_task(task
);
8571 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8572 *timeout
= exception
.timeout
;
8575 trace_nfs4_layoutget(lgp
->args
.ctx
,
8581 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8582 if (status
== 0 && lgp
->res
.layoutp
->len
)
8583 lseg
= pnfs_layout_process(lgp
);
8584 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8586 dprintk("<-- %s status=%d\n", __func__
, status
);
8588 return ERR_PTR(status
);
8593 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8595 struct nfs4_layoutreturn
*lrp
= calldata
;
8597 dprintk("--> %s\n", __func__
);
8598 nfs41_setup_sequence(lrp
->clp
->cl_session
,
8599 &lrp
->args
.seq_args
,
8604 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8606 struct nfs4_layoutreturn
*lrp
= calldata
;
8607 struct nfs_server
*server
;
8609 dprintk("--> %s\n", __func__
);
8611 if (!nfs41_sequence_process(task
, &lrp
->res
.seq_res
))
8614 server
= NFS_SERVER(lrp
->args
.inode
);
8615 switch (task
->tk_status
) {
8617 task
->tk_status
= 0;
8620 case -NFS4ERR_DELAY
:
8621 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8623 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8624 rpc_restart_call_prepare(task
);
8627 dprintk("<-- %s\n", __func__
);
8630 static void nfs4_layoutreturn_release(void *calldata
)
8632 struct nfs4_layoutreturn
*lrp
= calldata
;
8633 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8635 dprintk("--> %s\n", __func__
);
8636 pnfs_layoutreturn_free_lsegs(lo
, &lrp
->args
.stateid
, &lrp
->args
.range
,
8637 lrp
->res
.lrs_present
? &lrp
->res
.stateid
: NULL
);
8638 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8639 pnfs_put_layout_hdr(lrp
->args
.layout
);
8640 nfs_iput_and_deactive(lrp
->inode
);
8642 dprintk("<-- %s\n", __func__
);
8645 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8646 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8647 .rpc_call_done
= nfs4_layoutreturn_done
,
8648 .rpc_release
= nfs4_layoutreturn_release
,
8651 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8653 struct rpc_task
*task
;
8654 struct rpc_message msg
= {
8655 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8656 .rpc_argp
= &lrp
->args
,
8657 .rpc_resp
= &lrp
->res
,
8658 .rpc_cred
= lrp
->cred
,
8660 struct rpc_task_setup task_setup_data
= {
8661 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8662 .rpc_message
= &msg
,
8663 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8664 .callback_data
= lrp
,
8668 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8669 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8670 &task_setup_data
.rpc_client
, &msg
);
8672 dprintk("--> %s\n", __func__
);
8674 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8676 nfs4_layoutreturn_release(lrp
);
8679 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8681 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8682 task
= rpc_run_task(&task_setup_data
);
8684 return PTR_ERR(task
);
8686 status
= task
->tk_status
;
8687 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8688 dprintk("<-- %s status=%d\n", __func__
, status
);
8694 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8695 struct pnfs_device
*pdev
,
8696 struct rpc_cred
*cred
)
8698 struct nfs4_getdeviceinfo_args args
= {
8700 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8701 NOTIFY_DEVICEID4_DELETE
,
8703 struct nfs4_getdeviceinfo_res res
= {
8706 struct rpc_message msg
= {
8707 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8714 dprintk("--> %s\n", __func__
);
8715 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8716 if (res
.notification
& ~args
.notify_types
)
8717 dprintk("%s: unsupported notification\n", __func__
);
8718 if (res
.notification
!= args
.notify_types
)
8721 dprintk("<-- %s status=%d\n", __func__
, status
);
8726 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8727 struct pnfs_device
*pdev
,
8728 struct rpc_cred
*cred
)
8730 struct nfs4_exception exception
= { };
8734 err
= nfs4_handle_exception(server
,
8735 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8737 } while (exception
.retry
);
8740 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8742 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8744 struct nfs4_layoutcommit_data
*data
= calldata
;
8745 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8746 struct nfs4_session
*session
= nfs4_get_session(server
);
8748 nfs41_setup_sequence(session
,
8749 &data
->args
.seq_args
,
8755 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8757 struct nfs4_layoutcommit_data
*data
= calldata
;
8758 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8760 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8763 switch (task
->tk_status
) { /* Just ignore these failures */
8764 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8765 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8766 case -NFS4ERR_BADLAYOUT
: /* no layout */
8767 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8768 task
->tk_status
= 0;
8772 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8773 rpc_restart_call_prepare(task
);
8779 static void nfs4_layoutcommit_release(void *calldata
)
8781 struct nfs4_layoutcommit_data
*data
= calldata
;
8783 pnfs_cleanup_layoutcommit(data
);
8784 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8786 put_rpccred(data
->cred
);
8787 nfs_iput_and_deactive(data
->inode
);
8791 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8792 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8793 .rpc_call_done
= nfs4_layoutcommit_done
,
8794 .rpc_release
= nfs4_layoutcommit_release
,
8798 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8800 struct rpc_message msg
= {
8801 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8802 .rpc_argp
= &data
->args
,
8803 .rpc_resp
= &data
->res
,
8804 .rpc_cred
= data
->cred
,
8806 struct rpc_task_setup task_setup_data
= {
8807 .task
= &data
->task
,
8808 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8809 .rpc_message
= &msg
,
8810 .callback_ops
= &nfs4_layoutcommit_ops
,
8811 .callback_data
= data
,
8813 struct rpc_task
*task
;
8816 dprintk("NFS: initiating layoutcommit call. sync %d "
8817 "lbw: %llu inode %lu\n", sync
,
8818 data
->args
.lastbytewritten
,
8819 data
->args
.inode
->i_ino
);
8822 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8823 if (data
->inode
== NULL
) {
8824 nfs4_layoutcommit_release(data
);
8827 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8829 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8830 task
= rpc_run_task(&task_setup_data
);
8832 return PTR_ERR(task
);
8834 status
= task
->tk_status
;
8835 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8836 dprintk("%s: status %d\n", __func__
, status
);
8842 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8843 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8846 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8847 struct nfs_fsinfo
*info
,
8848 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8850 struct nfs41_secinfo_no_name_args args
= {
8851 .style
= SECINFO_STYLE_CURRENT_FH
,
8853 struct nfs4_secinfo_res res
= {
8856 struct rpc_message msg
= {
8857 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8861 struct rpc_clnt
*clnt
= server
->client
;
8862 struct rpc_cred
*cred
= NULL
;
8865 if (use_integrity
) {
8866 clnt
= server
->nfs_client
->cl_rpcclient
;
8867 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8868 msg
.rpc_cred
= cred
;
8871 dprintk("--> %s\n", __func__
);
8872 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8874 dprintk("<-- %s status=%d\n", __func__
, status
);
8883 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8884 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8886 struct nfs4_exception exception
= { };
8889 /* first try using integrity protection */
8890 err
= -NFS4ERR_WRONGSEC
;
8892 /* try to use integrity protection with machine cred */
8893 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8894 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8898 * if unable to use integrity protection, or SECINFO with
8899 * integrity protection returns NFS4ERR_WRONGSEC (which is
8900 * disallowed by spec, but exists in deployed servers) use
8901 * the current filesystem's rpc_client and the user cred.
8903 if (err
== -NFS4ERR_WRONGSEC
)
8904 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8909 case -NFS4ERR_WRONGSEC
:
8913 err
= nfs4_handle_exception(server
, err
, &exception
);
8915 } while (exception
.retry
);
8921 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8922 struct nfs_fsinfo
*info
)
8926 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8927 struct nfs4_secinfo_flavors
*flavors
;
8928 struct nfs4_secinfo4
*secinfo
;
8931 page
= alloc_page(GFP_KERNEL
);
8937 flavors
= page_address(page
);
8938 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8941 * Fall back on "guess and check" method if
8942 * the server doesn't support SECINFO_NO_NAME
8944 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8945 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8951 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8952 secinfo
= &flavors
->flavors
[i
];
8954 switch (secinfo
->flavor
) {
8958 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
8959 &secinfo
->flavor_info
);
8962 flavor
= RPC_AUTH_MAXFLAVOR
;
8966 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
8967 flavor
= RPC_AUTH_MAXFLAVOR
;
8969 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
8970 err
= nfs4_lookup_root_sec(server
, fhandle
,
8977 if (flavor
== RPC_AUTH_MAXFLAVOR
)
8988 static int _nfs41_test_stateid(struct nfs_server
*server
,
8989 nfs4_stateid
*stateid
,
8990 struct rpc_cred
*cred
)
8993 struct nfs41_test_stateid_args args
= {
8996 struct nfs41_test_stateid_res res
;
8997 struct rpc_message msg
= {
8998 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
9003 struct rpc_clnt
*rpc_client
= server
->client
;
9005 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9008 dprintk("NFS call test_stateid %p\n", stateid
);
9009 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
9010 nfs4_set_sequence_privileged(&args
.seq_args
);
9011 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
9012 &args
.seq_args
, &res
.seq_res
);
9013 if (status
!= NFS_OK
) {
9014 dprintk("NFS reply test_stateid: failed, %d\n", status
);
9017 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
9021 static void nfs4_handle_delay_or_session_error(struct nfs_server
*server
,
9022 int err
, struct nfs4_exception
*exception
)
9024 exception
->retry
= 0;
9026 case -NFS4ERR_DELAY
:
9027 case -NFS4ERR_RETRY_UNCACHED_REP
:
9028 nfs4_handle_exception(server
, err
, exception
);
9030 case -NFS4ERR_BADSESSION
:
9031 case -NFS4ERR_BADSLOT
:
9032 case -NFS4ERR_BAD_HIGH_SLOT
:
9033 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
9034 case -NFS4ERR_DEADSESSION
:
9035 nfs4_do_handle_exception(server
, err
, exception
);
9040 * nfs41_test_stateid - perform a TEST_STATEID operation
9042 * @server: server / transport on which to perform the operation
9043 * @stateid: state ID to test
9046 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9047 * Otherwise a negative NFS4ERR value is returned if the operation
9048 * failed or the state ID is not currently valid.
9050 static int nfs41_test_stateid(struct nfs_server
*server
,
9051 nfs4_stateid
*stateid
,
9052 struct rpc_cred
*cred
)
9054 struct nfs4_exception exception
= { };
9057 err
= _nfs41_test_stateid(server
, stateid
, cred
);
9058 nfs4_handle_delay_or_session_error(server
, err
, &exception
);
9059 } while (exception
.retry
);
9063 struct nfs_free_stateid_data
{
9064 struct nfs_server
*server
;
9065 struct nfs41_free_stateid_args args
;
9066 struct nfs41_free_stateid_res res
;
9069 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
9071 struct nfs_free_stateid_data
*data
= calldata
;
9072 nfs41_setup_sequence(nfs4_get_session(data
->server
),
9073 &data
->args
.seq_args
,
9078 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
9080 struct nfs_free_stateid_data
*data
= calldata
;
9082 nfs41_sequence_done(task
, &data
->res
.seq_res
);
9084 switch (task
->tk_status
) {
9085 case -NFS4ERR_DELAY
:
9086 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
9087 rpc_restart_call_prepare(task
);
9091 static void nfs41_free_stateid_release(void *calldata
)
9096 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
9097 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
9098 .rpc_call_done
= nfs41_free_stateid_done
,
9099 .rpc_release
= nfs41_free_stateid_release
,
9102 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
9103 const nfs4_stateid
*stateid
,
9104 struct rpc_cred
*cred
,
9107 struct rpc_message msg
= {
9108 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
9111 struct rpc_task_setup task_setup
= {
9112 .rpc_client
= server
->client
,
9113 .rpc_message
= &msg
,
9114 .callback_ops
= &nfs41_free_stateid_ops
,
9115 .flags
= RPC_TASK_ASYNC
,
9117 struct nfs_free_stateid_data
*data
;
9119 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9120 &task_setup
.rpc_client
, &msg
);
9122 dprintk("NFS call free_stateid %p\n", stateid
);
9123 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
9125 return ERR_PTR(-ENOMEM
);
9126 data
->server
= server
;
9127 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
9129 task_setup
.callback_data
= data
;
9131 msg
.rpc_argp
= &data
->args
;
9132 msg
.rpc_resp
= &data
->res
;
9133 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
9135 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
9137 return rpc_run_task(&task_setup
);
9141 * nfs41_free_stateid - perform a FREE_STATEID operation
9143 * @server: server / transport on which to perform the operation
9144 * @stateid: state ID to release
9146 * @is_recovery: set to true if this call needs to be privileged
9148 * Note: this function is always asynchronous.
9150 static int nfs41_free_stateid(struct nfs_server
*server
,
9151 const nfs4_stateid
*stateid
,
9152 struct rpc_cred
*cred
,
9155 struct rpc_task
*task
;
9157 task
= _nfs41_free_stateid(server
, stateid
, cred
, is_recovery
);
9159 return PTR_ERR(task
);
9165 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
9167 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
9169 nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
9170 nfs4_free_lock_state(server
, lsp
);
9173 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
9174 const nfs4_stateid
*s2
)
9176 if (s1
->type
!= s2
->type
)
9179 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
9182 if (s1
->seqid
== s2
->seqid
)
9184 if (s1
->seqid
== 0 || s2
->seqid
== 0)
9190 #endif /* CONFIG_NFS_V4_1 */
9192 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
9193 const nfs4_stateid
*s2
)
9195 return nfs4_stateid_match(s1
, s2
);
9199 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
9200 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9201 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9202 .recover_open
= nfs4_open_reclaim
,
9203 .recover_lock
= nfs4_lock_reclaim
,
9204 .establish_clid
= nfs4_init_clientid
,
9205 .detect_trunking
= nfs40_discover_server_trunking
,
9208 #if defined(CONFIG_NFS_V4_1)
9209 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
9210 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9211 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9212 .recover_open
= nfs4_open_reclaim
,
9213 .recover_lock
= nfs4_lock_reclaim
,
9214 .establish_clid
= nfs41_init_clientid
,
9215 .reclaim_complete
= nfs41_proc_reclaim_complete
,
9216 .detect_trunking
= nfs41_discover_server_trunking
,
9218 #endif /* CONFIG_NFS_V4_1 */
9220 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
9221 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9222 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9223 .recover_open
= nfs40_open_expired
,
9224 .recover_lock
= nfs4_lock_expired
,
9225 .establish_clid
= nfs4_init_clientid
,
9228 #if defined(CONFIG_NFS_V4_1)
9229 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
9230 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9231 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9232 .recover_open
= nfs41_open_expired
,
9233 .recover_lock
= nfs41_lock_expired
,
9234 .establish_clid
= nfs41_init_clientid
,
9236 #endif /* CONFIG_NFS_V4_1 */
9238 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
9239 .sched_state_renewal
= nfs4_proc_async_renew
,
9240 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
9241 .renew_lease
= nfs4_proc_renew
,
9244 #if defined(CONFIG_NFS_V4_1)
9245 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
9246 .sched_state_renewal
= nfs41_proc_async_sequence
,
9247 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
9248 .renew_lease
= nfs4_proc_sequence
,
9252 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
9253 .get_locations
= _nfs40_proc_get_locations
,
9254 .fsid_present
= _nfs40_proc_fsid_present
,
9257 #if defined(CONFIG_NFS_V4_1)
9258 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
9259 .get_locations
= _nfs41_proc_get_locations
,
9260 .fsid_present
= _nfs41_proc_fsid_present
,
9262 #endif /* CONFIG_NFS_V4_1 */
9264 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
9266 .init_caps
= NFS_CAP_READDIRPLUS
9267 | NFS_CAP_ATOMIC_OPEN
9268 | NFS_CAP_POSIX_LOCK
,
9269 .init_client
= nfs40_init_client
,
9270 .shutdown_client
= nfs40_shutdown_client
,
9271 .match_stateid
= nfs4_match_stateid
,
9272 .find_root_sec
= nfs4_find_root_sec
,
9273 .free_lock_state
= nfs4_release_lockowner
,
9274 .test_and_free_expired
= nfs40_test_and_free_expired_stateid
,
9275 .alloc_seqid
= nfs_alloc_seqid
,
9276 .call_sync_ops
= &nfs40_call_sync_ops
,
9277 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
9278 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
9279 .state_renewal_ops
= &nfs40_state_renewal_ops
,
9280 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
9283 #if defined(CONFIG_NFS_V4_1)
9284 static struct nfs_seqid
*
9285 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
9290 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
9292 .init_caps
= NFS_CAP_READDIRPLUS
9293 | NFS_CAP_ATOMIC_OPEN
9294 | NFS_CAP_POSIX_LOCK
9295 | NFS_CAP_STATEID_NFSV41
9296 | NFS_CAP_ATOMIC_OPEN_V1
,
9297 .init_client
= nfs41_init_client
,
9298 .shutdown_client
= nfs41_shutdown_client
,
9299 .match_stateid
= nfs41_match_stateid
,
9300 .find_root_sec
= nfs41_find_root_sec
,
9301 .free_lock_state
= nfs41_free_lock_state
,
9302 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9303 .alloc_seqid
= nfs_alloc_no_seqid
,
9304 .session_trunk
= nfs4_test_session_trunk
,
9305 .call_sync_ops
= &nfs41_call_sync_ops
,
9306 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9307 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9308 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9309 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9313 #if defined(CONFIG_NFS_V4_2)
9314 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
9316 .init_caps
= NFS_CAP_READDIRPLUS
9317 | NFS_CAP_ATOMIC_OPEN
9318 | NFS_CAP_POSIX_LOCK
9319 | NFS_CAP_STATEID_NFSV41
9320 | NFS_CAP_ATOMIC_OPEN_V1
9323 | NFS_CAP_DEALLOCATE
9325 | NFS_CAP_LAYOUTSTATS
9327 .init_client
= nfs41_init_client
,
9328 .shutdown_client
= nfs41_shutdown_client
,
9329 .match_stateid
= nfs41_match_stateid
,
9330 .find_root_sec
= nfs41_find_root_sec
,
9331 .free_lock_state
= nfs41_free_lock_state
,
9332 .call_sync_ops
= &nfs41_call_sync_ops
,
9333 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9334 .alloc_seqid
= nfs_alloc_no_seqid
,
9335 .session_trunk
= nfs4_test_session_trunk
,
9336 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9337 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9338 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9339 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9343 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
9344 [0] = &nfs_v4_0_minor_ops
,
9345 #if defined(CONFIG_NFS_V4_1)
9346 [1] = &nfs_v4_1_minor_ops
,
9348 #if defined(CONFIG_NFS_V4_2)
9349 [2] = &nfs_v4_2_minor_ops
,
9353 static ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
9355 ssize_t error
, error2
;
9357 error
= generic_listxattr(dentry
, list
, size
);
9365 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
9368 return error
+ error2
;
9371 static const struct inode_operations nfs4_dir_inode_operations
= {
9372 .create
= nfs_create
,
9373 .lookup
= nfs_lookup
,
9374 .atomic_open
= nfs_atomic_open
,
9376 .unlink
= nfs_unlink
,
9377 .symlink
= nfs_symlink
,
9381 .rename
= nfs_rename
,
9382 .permission
= nfs_permission
,
9383 .getattr
= nfs_getattr
,
9384 .setattr
= nfs_setattr
,
9385 .listxattr
= nfs4_listxattr
,
9388 static const struct inode_operations nfs4_file_inode_operations
= {
9389 .permission
= nfs_permission
,
9390 .getattr
= nfs_getattr
,
9391 .setattr
= nfs_setattr
,
9392 .listxattr
= nfs4_listxattr
,
9395 const struct nfs_rpc_ops nfs_v4_clientops
= {
9396 .version
= 4, /* protocol version */
9397 .dentry_ops
= &nfs4_dentry_operations
,
9398 .dir_inode_ops
= &nfs4_dir_inode_operations
,
9399 .file_inode_ops
= &nfs4_file_inode_operations
,
9400 .file_ops
= &nfs4_file_operations
,
9401 .getroot
= nfs4_proc_get_root
,
9402 .submount
= nfs4_submount
,
9403 .try_mount
= nfs4_try_mount
,
9404 .getattr
= nfs4_proc_getattr
,
9405 .setattr
= nfs4_proc_setattr
,
9406 .lookup
= nfs4_proc_lookup
,
9407 .access
= nfs4_proc_access
,
9408 .readlink
= nfs4_proc_readlink
,
9409 .create
= nfs4_proc_create
,
9410 .remove
= nfs4_proc_remove
,
9411 .unlink_setup
= nfs4_proc_unlink_setup
,
9412 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
9413 .unlink_done
= nfs4_proc_unlink_done
,
9414 .rename_setup
= nfs4_proc_rename_setup
,
9415 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
9416 .rename_done
= nfs4_proc_rename_done
,
9417 .link
= nfs4_proc_link
,
9418 .symlink
= nfs4_proc_symlink
,
9419 .mkdir
= nfs4_proc_mkdir
,
9420 .rmdir
= nfs4_proc_remove
,
9421 .readdir
= nfs4_proc_readdir
,
9422 .mknod
= nfs4_proc_mknod
,
9423 .statfs
= nfs4_proc_statfs
,
9424 .fsinfo
= nfs4_proc_fsinfo
,
9425 .pathconf
= nfs4_proc_pathconf
,
9426 .set_capabilities
= nfs4_server_capabilities
,
9427 .decode_dirent
= nfs4_decode_dirent
,
9428 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
9429 .read_setup
= nfs4_proc_read_setup
,
9430 .read_done
= nfs4_read_done
,
9431 .write_setup
= nfs4_proc_write_setup
,
9432 .write_done
= nfs4_write_done
,
9433 .commit_setup
= nfs4_proc_commit_setup
,
9434 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
9435 .commit_done
= nfs4_commit_done
,
9436 .lock
= nfs4_proc_lock
,
9437 .clear_acl_cache
= nfs4_zap_acl_attr
,
9438 .close_context
= nfs4_close_context
,
9439 .open_context
= nfs4_atomic_open
,
9440 .have_delegation
= nfs4_have_delegation
,
9441 .return_delegation
= nfs4_inode_return_delegation
,
9442 .alloc_client
= nfs4_alloc_client
,
9443 .init_client
= nfs4_init_client
,
9444 .free_client
= nfs4_free_client
,
9445 .create_server
= nfs4_create_server
,
9446 .clone_server
= nfs_clone_server
,
9449 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
9450 .name
= XATTR_NAME_NFSV4_ACL
,
9451 .list
= nfs4_xattr_list_nfs4_acl
,
9452 .get
= nfs4_xattr_get_nfs4_acl
,
9453 .set
= nfs4_xattr_set_nfs4_acl
,
9456 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
9457 &nfs4_xattr_nfs4_acl_handler
,
9458 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9459 &nfs4_xattr_nfs4_label_handler
,