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 nfs_open_context
*ctx
, 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
:
819 case -NFS4ERR_DEADSESSION
:
820 case -NFS4ERR_BADSESSION
:
821 nfs4_schedule_session_recovery(session
, res
->sr_status
);
824 /* Just update the slot sequence no. */
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
833 if (rpc_restart_call_prepare(task
)) {
834 nfs41_sequence_free_slot(res
);
840 if (!rpc_restart_call(task
))
842 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
846 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
848 if (!nfs41_sequence_process(task
, res
))
850 if (res
->sr_slot
!= NULL
)
851 nfs41_sequence_free_slot(res
);
855 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
857 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
859 if (res
->sr_slot
== NULL
)
861 if (res
->sr_slot
->table
->session
!= NULL
)
862 return nfs41_sequence_process(task
, res
);
863 return nfs40_sequence_done(task
, res
);
866 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
868 if (res
->sr_slot
!= NULL
) {
869 if (res
->sr_slot
->table
->session
!= NULL
)
870 nfs41_sequence_free_slot(res
);
872 nfs40_sequence_free_slot(res
);
876 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
878 if (res
->sr_slot
== NULL
)
880 if (!res
->sr_slot
->table
->session
)
881 return nfs40_sequence_done(task
, res
);
882 return nfs41_sequence_done(task
, res
);
884 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
886 int nfs41_setup_sequence(struct nfs4_session
*session
,
887 struct nfs4_sequence_args
*args
,
888 struct nfs4_sequence_res
*res
,
889 struct rpc_task
*task
)
891 struct nfs4_slot
*slot
;
892 struct nfs4_slot_table
*tbl
;
894 dprintk("--> %s\n", __func__
);
895 /* slot already allocated? */
896 if (res
->sr_slot
!= NULL
)
899 tbl
= &session
->fc_slot_table
;
901 task
->tk_timeout
= 0;
903 spin_lock(&tbl
->slot_tbl_lock
);
904 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
905 !args
->sa_privileged
) {
906 /* The state manager will wait until the slot table is empty */
907 dprintk("%s session is draining\n", __func__
);
911 slot
= nfs4_alloc_slot(tbl
);
913 /* If out of memory, try again in 1/4 second */
914 if (slot
== ERR_PTR(-ENOMEM
))
915 task
->tk_timeout
= HZ
>> 2;
916 dprintk("<-- %s: no free slots\n", __func__
);
919 spin_unlock(&tbl
->slot_tbl_lock
);
921 slot
->privileged
= args
->sa_privileged
? 1 : 0;
922 args
->sa_slot
= slot
;
924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__
,
925 slot
->slot_nr
, slot
->seq_nr
);
928 res
->sr_timestamp
= jiffies
;
929 res
->sr_status_flags
= 0;
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
935 trace_nfs4_setup_sequence(session
, args
);
937 rpc_call_start(task
);
940 /* Privileged tasks are queued with top priority */
941 if (args
->sa_privileged
)
942 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
943 NULL
, RPC_PRIORITY_PRIVILEGED
);
945 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
946 spin_unlock(&tbl
->slot_tbl_lock
);
949 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
951 static int nfs4_setup_sequence(const struct nfs_server
*server
,
952 struct nfs4_sequence_args
*args
,
953 struct nfs4_sequence_res
*res
,
954 struct rpc_task
*task
)
956 struct nfs4_session
*session
= nfs4_get_session(server
);
960 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
963 dprintk("--> %s clp %p session %p sr_slot %u\n",
964 __func__
, session
->clp
, session
, res
->sr_slot
?
965 res
->sr_slot
->slot_nr
: NFS4_NO_SLOT
);
967 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
969 dprintk("<-- %s status=%d\n", __func__
, ret
);
973 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
975 struct nfs4_call_sync_data
*data
= calldata
;
976 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
978 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
980 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
983 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
985 struct nfs4_call_sync_data
*data
= calldata
;
987 nfs41_sequence_done(task
, data
->seq_res
);
990 static const struct rpc_call_ops nfs41_call_sync_ops
= {
991 .rpc_call_prepare
= nfs41_call_sync_prepare
,
992 .rpc_call_done
= nfs41_call_sync_done
,
995 #else /* !CONFIG_NFS_V4_1 */
997 static int nfs4_setup_sequence(const struct nfs_server
*server
,
998 struct nfs4_sequence_args
*args
,
999 struct nfs4_sequence_res
*res
,
1000 struct rpc_task
*task
)
1002 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
1006 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
1008 return nfs40_sequence_done(task
, res
);
1011 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
1013 if (res
->sr_slot
!= NULL
)
1014 nfs40_sequence_free_slot(res
);
1017 int nfs4_sequence_done(struct rpc_task
*task
,
1018 struct nfs4_sequence_res
*res
)
1020 return nfs40_sequence_done(task
, res
);
1022 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
1024 #endif /* !CONFIG_NFS_V4_1 */
1026 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
1028 struct nfs4_call_sync_data
*data
= calldata
;
1029 nfs4_setup_sequence(data
->seq_server
,
1030 data
->seq_args
, data
->seq_res
, task
);
1033 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
1035 struct nfs4_call_sync_data
*data
= calldata
;
1036 nfs4_sequence_done(task
, data
->seq_res
);
1039 static const struct rpc_call_ops nfs40_call_sync_ops
= {
1040 .rpc_call_prepare
= nfs40_call_sync_prepare
,
1041 .rpc_call_done
= nfs40_call_sync_done
,
1044 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
1045 struct nfs_server
*server
,
1046 struct rpc_message
*msg
,
1047 struct nfs4_sequence_args
*args
,
1048 struct nfs4_sequence_res
*res
)
1051 struct rpc_task
*task
;
1052 struct nfs_client
*clp
= server
->nfs_client
;
1053 struct nfs4_call_sync_data data
= {
1054 .seq_server
= server
,
1058 struct rpc_task_setup task_setup
= {
1061 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
1062 .callback_data
= &data
1065 task
= rpc_run_task(&task_setup
);
1067 ret
= PTR_ERR(task
);
1069 ret
= task
->tk_status
;
1075 int nfs4_call_sync(struct rpc_clnt
*clnt
,
1076 struct nfs_server
*server
,
1077 struct rpc_message
*msg
,
1078 struct nfs4_sequence_args
*args
,
1079 struct nfs4_sequence_res
*res
,
1082 nfs4_init_sequence(args
, res
, cache_reply
);
1083 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
1086 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
1088 struct nfs_inode
*nfsi
= NFS_I(dir
);
1090 spin_lock(&dir
->i_lock
);
1091 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1092 if (cinfo
->atomic
&& cinfo
->before
== dir
->i_version
) {
1093 nfsi
->cache_validity
&= ~NFS_INO_REVAL_PAGECACHE
;
1094 nfsi
->attrtimeo_timestamp
= jiffies
;
1096 nfs_force_lookup_revalidate(dir
);
1097 if (cinfo
->before
!= dir
->i_version
)
1098 nfsi
->cache_validity
|= NFS_INO_INVALID_ACCESS
|
1099 NFS_INO_INVALID_ACL
;
1101 dir
->i_version
= cinfo
->after
;
1102 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
1103 nfs_fscache_invalidate(dir
);
1104 spin_unlock(&dir
->i_lock
);
1107 struct nfs4_opendata
{
1109 struct nfs_openargs o_arg
;
1110 struct nfs_openres o_res
;
1111 struct nfs_open_confirmargs c_arg
;
1112 struct nfs_open_confirmres c_res
;
1113 struct nfs4_string owner_name
;
1114 struct nfs4_string group_name
;
1115 struct nfs4_label
*a_label
;
1116 struct nfs_fattr f_attr
;
1117 struct nfs4_label
*f_label
;
1119 struct dentry
*dentry
;
1120 struct nfs4_state_owner
*owner
;
1121 struct nfs4_state
*state
;
1123 unsigned long timestamp
;
1124 unsigned int rpc_done
: 1;
1125 unsigned int file_created
: 1;
1126 unsigned int is_recover
: 1;
1131 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
1132 int err
, struct nfs4_exception
*exception
)
1136 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1138 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
1139 exception
->retry
= 1;
1144 nfs4_map_atomic_open_share(struct nfs_server
*server
,
1145 fmode_t fmode
, int openflags
)
1149 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
1151 res
= NFS4_SHARE_ACCESS_READ
;
1154 res
= NFS4_SHARE_ACCESS_WRITE
;
1156 case FMODE_READ
|FMODE_WRITE
:
1157 res
= NFS4_SHARE_ACCESS_BOTH
;
1159 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1161 /* Want no delegation if we're using O_DIRECT */
1162 if (openflags
& O_DIRECT
)
1163 res
|= NFS4_SHARE_WANT_NO_DELEG
;
1168 static enum open_claim_type4
1169 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
1170 enum open_claim_type4 claim
)
1172 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
1177 case NFS4_OPEN_CLAIM_FH
:
1178 return NFS4_OPEN_CLAIM_NULL
;
1179 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1180 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1181 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1182 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
1186 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
1188 p
->o_res
.f_attr
= &p
->f_attr
;
1189 p
->o_res
.f_label
= p
->f_label
;
1190 p
->o_res
.seqid
= p
->o_arg
.seqid
;
1191 p
->c_res
.seqid
= p
->c_arg
.seqid
;
1192 p
->o_res
.server
= p
->o_arg
.server
;
1193 p
->o_res
.access_request
= p
->o_arg
.access
;
1194 nfs_fattr_init(&p
->f_attr
);
1195 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
1198 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
1199 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
1200 const struct iattr
*attrs
,
1201 struct nfs4_label
*label
,
1202 enum open_claim_type4 claim
,
1205 struct dentry
*parent
= dget_parent(dentry
);
1206 struct inode
*dir
= d_inode(parent
);
1207 struct nfs_server
*server
= NFS_SERVER(dir
);
1208 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1209 struct nfs4_opendata
*p
;
1211 p
= kzalloc(sizeof(*p
), gfp_mask
);
1215 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1216 if (IS_ERR(p
->f_label
))
1219 p
->a_label
= nfs4_label_alloc(server
, gfp_mask
);
1220 if (IS_ERR(p
->a_label
))
1223 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1224 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1225 if (IS_ERR(p
->o_arg
.seqid
))
1226 goto err_free_label
;
1227 nfs_sb_active(dentry
->d_sb
);
1228 p
->dentry
= dget(dentry
);
1231 atomic_inc(&sp
->so_count
);
1232 p
->o_arg
.open_flags
= flags
;
1233 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1234 p
->o_arg
.umask
= current_umask();
1235 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1236 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1238 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1239 * will return permission denied for all bits until close */
1240 if (!(flags
& O_EXCL
)) {
1241 /* ask server to check for all possible rights as results
1243 switch (p
->o_arg
.claim
) {
1246 case NFS4_OPEN_CLAIM_NULL
:
1247 case NFS4_OPEN_CLAIM_FH
:
1248 p
->o_arg
.access
= NFS4_ACCESS_READ
|
1249 NFS4_ACCESS_MODIFY
|
1250 NFS4_ACCESS_EXTEND
|
1251 NFS4_ACCESS_EXECUTE
;
1254 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1255 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1256 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1257 p
->o_arg
.name
= &dentry
->d_name
;
1258 p
->o_arg
.server
= server
;
1259 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1260 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1261 p
->o_arg
.label
= nfs4_label_copy(p
->a_label
, label
);
1262 switch (p
->o_arg
.claim
) {
1263 case NFS4_OPEN_CLAIM_NULL
:
1264 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1265 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1266 p
->o_arg
.fh
= NFS_FH(dir
);
1268 case NFS4_OPEN_CLAIM_PREVIOUS
:
1269 case NFS4_OPEN_CLAIM_FH
:
1270 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1271 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1272 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1274 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1277 p
->o_arg
.u
.attrs
= &p
->attrs
;
1278 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1281 verf
[1] = current
->pid
;
1282 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1283 sizeof(p
->o_arg
.u
.verifier
.data
));
1285 p
->c_arg
.fh
= &p
->o_res
.fh
;
1286 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1287 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1288 nfs4_init_opendata_res(p
);
1289 kref_init(&p
->kref
);
1293 nfs4_label_free(p
->a_label
);
1295 nfs4_label_free(p
->f_label
);
1303 static void nfs4_opendata_free(struct kref
*kref
)
1305 struct nfs4_opendata
*p
= container_of(kref
,
1306 struct nfs4_opendata
, kref
);
1307 struct super_block
*sb
= p
->dentry
->d_sb
;
1309 nfs_free_seqid(p
->o_arg
.seqid
);
1310 nfs4_sequence_free_slot(&p
->o_res
.seq_res
);
1311 if (p
->state
!= NULL
)
1312 nfs4_put_open_state(p
->state
);
1313 nfs4_put_state_owner(p
->owner
);
1315 nfs4_label_free(p
->a_label
);
1316 nfs4_label_free(p
->f_label
);
1320 nfs_sb_deactive(sb
);
1321 nfs_fattr_free_names(&p
->f_attr
);
1322 kfree(p
->f_attr
.mdsthreshold
);
1326 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1329 kref_put(&p
->kref
, nfs4_opendata_free
);
1332 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1336 ret
= rpc_wait_for_completion_task(task
);
1340 static bool nfs4_mode_match_open_stateid(struct nfs4_state
*state
,
1343 switch(fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1344 case FMODE_READ
|FMODE_WRITE
:
1345 return state
->n_rdwr
!= 0;
1347 return state
->n_wronly
!= 0;
1349 return state
->n_rdonly
!= 0;
1355 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1359 if (open_mode
& (O_EXCL
|O_TRUNC
))
1361 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1363 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1364 && state
->n_rdonly
!= 0;
1367 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1368 && state
->n_wronly
!= 0;
1370 case FMODE_READ
|FMODE_WRITE
:
1371 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1372 && state
->n_rdwr
!= 0;
1378 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
,
1379 enum open_claim_type4 claim
)
1381 if (delegation
== NULL
)
1383 if ((delegation
->type
& fmode
) != fmode
)
1385 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1388 case NFS4_OPEN_CLAIM_NULL
:
1389 case NFS4_OPEN_CLAIM_FH
:
1391 case NFS4_OPEN_CLAIM_PREVIOUS
:
1392 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1397 nfs_mark_delegation_referenced(delegation
);
1401 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1410 case FMODE_READ
|FMODE_WRITE
:
1413 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1416 #ifdef CONFIG_NFS_V4_1
1417 static bool nfs_open_stateid_recover_openmode(struct nfs4_state
*state
)
1419 if (state
->n_rdonly
&& !test_bit(NFS_O_RDONLY_STATE
, &state
->flags
))
1421 if (state
->n_wronly
&& !test_bit(NFS_O_WRONLY_STATE
, &state
->flags
))
1423 if (state
->n_rdwr
&& !test_bit(NFS_O_RDWR_STATE
, &state
->flags
))
1427 #endif /* CONFIG_NFS_V4_1 */
1429 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1431 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1432 bool need_recover
= false;
1434 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1435 need_recover
= true;
1436 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1437 need_recover
= true;
1438 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1439 need_recover
= true;
1441 nfs4_state_mark_reclaim_nograce(clp
, state
);
1444 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1445 const nfs4_stateid
*stateid
, nfs4_stateid
*freeme
)
1447 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1449 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1450 nfs4_stateid_copy(freeme
, &state
->open_stateid
);
1451 nfs_test_and_clear_all_open_stateid(state
);
1454 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1459 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1461 if (!(state
->n_wronly
|| state
->n_rdonly
|| state
->n_rdwr
))
1463 if (state
->n_wronly
)
1464 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1465 if (state
->n_rdonly
)
1466 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1468 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1469 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1472 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1473 nfs4_stateid
*stateid
, fmode_t fmode
)
1475 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1476 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1478 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1481 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1484 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1485 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1486 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1488 if (stateid
== NULL
)
1490 /* Handle OPEN+OPEN_DOWNGRADE races */
1491 if (nfs4_stateid_match_other(stateid
, &state
->open_stateid
) &&
1492 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
)) {
1493 nfs_resync_open_stateid_locked(state
);
1496 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1497 nfs4_stateid_copy(&state
->stateid
, stateid
);
1498 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1501 static void nfs_clear_open_stateid(struct nfs4_state
*state
,
1502 nfs4_stateid
*arg_stateid
,
1503 nfs4_stateid
*stateid
, fmode_t fmode
)
1505 write_seqlock(&state
->seqlock
);
1506 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1507 if (nfs4_state_match_open_stateid_other(state
, arg_stateid
))
1508 nfs_clear_open_stateid_locked(state
, stateid
, fmode
);
1509 write_sequnlock(&state
->seqlock
);
1510 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1511 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1514 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
,
1515 const nfs4_stateid
*stateid
, fmode_t fmode
,
1516 nfs4_stateid
*freeme
)
1520 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1523 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1525 case FMODE_READ
|FMODE_WRITE
:
1526 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1528 if (!nfs_need_update_open_stateid(state
, stateid
, freeme
))
1530 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1531 nfs4_stateid_copy(&state
->stateid
, stateid
);
1532 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1535 static void __update_open_stateid(struct nfs4_state
*state
,
1536 const nfs4_stateid
*open_stateid
,
1537 const nfs4_stateid
*deleg_stateid
,
1539 nfs4_stateid
*freeme
)
1542 * Protect the call to nfs4_state_set_mode_locked and
1543 * serialise the stateid update
1545 spin_lock(&state
->owner
->so_lock
);
1546 write_seqlock(&state
->seqlock
);
1547 if (deleg_stateid
!= NULL
) {
1548 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1549 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1551 if (open_stateid
!= NULL
)
1552 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
, freeme
);
1553 write_sequnlock(&state
->seqlock
);
1554 update_open_stateflags(state
, fmode
);
1555 spin_unlock(&state
->owner
->so_lock
);
1558 static int update_open_stateid(struct nfs4_state
*state
,
1559 const nfs4_stateid
*open_stateid
,
1560 const nfs4_stateid
*delegation
,
1563 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1564 struct nfs_client
*clp
= server
->nfs_client
;
1565 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1566 struct nfs_delegation
*deleg_cur
;
1567 nfs4_stateid freeme
= { };
1570 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1573 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1574 if (deleg_cur
== NULL
)
1577 spin_lock(&deleg_cur
->lock
);
1578 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1579 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1580 (deleg_cur
->type
& fmode
) != fmode
)
1581 goto no_delegation_unlock
;
1583 if (delegation
== NULL
)
1584 delegation
= &deleg_cur
->stateid
;
1585 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1586 goto no_delegation_unlock
;
1588 nfs_mark_delegation_referenced(deleg_cur
);
1589 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
,
1592 no_delegation_unlock
:
1593 spin_unlock(&deleg_cur
->lock
);
1597 if (!ret
&& open_stateid
!= NULL
) {
1598 __update_open_stateid(state
, open_stateid
, NULL
, fmode
, &freeme
);
1601 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1602 nfs4_schedule_state_manager(clp
);
1603 if (freeme
.type
!= 0)
1604 nfs4_test_and_free_stateid(server
, &freeme
,
1605 state
->owner
->so_cred
);
1610 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1611 const nfs4_stateid
*stateid
)
1613 struct nfs4_state
*state
= lsp
->ls_state
;
1616 spin_lock(&state
->state_lock
);
1617 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1619 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1621 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1624 spin_unlock(&state
->state_lock
);
1628 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1630 struct nfs_delegation
*delegation
;
1633 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1634 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1639 nfs4_inode_return_delegation(inode
);
1642 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1644 struct nfs4_state
*state
= opendata
->state
;
1645 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1646 struct nfs_delegation
*delegation
;
1647 int open_mode
= opendata
->o_arg
.open_flags
;
1648 fmode_t fmode
= opendata
->o_arg
.fmode
;
1649 enum open_claim_type4 claim
= opendata
->o_arg
.claim
;
1650 nfs4_stateid stateid
;
1654 spin_lock(&state
->owner
->so_lock
);
1655 if (can_open_cached(state
, fmode
, open_mode
)) {
1656 update_open_stateflags(state
, fmode
);
1657 spin_unlock(&state
->owner
->so_lock
);
1658 goto out_return_state
;
1660 spin_unlock(&state
->owner
->so_lock
);
1662 delegation
= rcu_dereference(nfsi
->delegation
);
1663 if (!can_open_delegated(delegation
, fmode
, claim
)) {
1667 /* Save the delegation */
1668 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1670 nfs_release_seqid(opendata
->o_arg
.seqid
);
1671 if (!opendata
->is_recover
) {
1672 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1678 /* Try to update the stateid using the delegation */
1679 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1680 goto out_return_state
;
1683 return ERR_PTR(ret
);
1685 atomic_inc(&state
->count
);
1690 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1692 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1693 struct nfs_delegation
*delegation
;
1694 int delegation_flags
= 0;
1697 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1699 delegation_flags
= delegation
->flags
;
1701 switch (data
->o_arg
.claim
) {
1704 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1705 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1706 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1707 "returning a delegation for "
1708 "OPEN(CLAIM_DELEGATE_CUR)\n",
1712 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1713 nfs_inode_set_delegation(state
->inode
,
1714 data
->owner
->so_cred
,
1717 nfs_inode_reclaim_delegation(state
->inode
,
1718 data
->owner
->so_cred
,
1723 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1724 * and update the nfs4_state.
1726 static struct nfs4_state
*
1727 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1729 struct inode
*inode
= data
->state
->inode
;
1730 struct nfs4_state
*state
= data
->state
;
1733 if (!data
->rpc_done
) {
1734 if (data
->rpc_status
) {
1735 ret
= data
->rpc_status
;
1738 /* cached opens have already been processed */
1742 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1746 if (data
->o_res
.delegation_type
!= 0)
1747 nfs4_opendata_check_deleg(data
, state
);
1749 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1751 atomic_inc(&state
->count
);
1755 return ERR_PTR(ret
);
1759 static struct nfs4_state
*
1760 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1762 struct inode
*inode
;
1763 struct nfs4_state
*state
= NULL
;
1766 if (!data
->rpc_done
) {
1767 state
= nfs4_try_open_cached(data
);
1768 trace_nfs4_cached_open(data
->state
);
1773 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1775 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1776 ret
= PTR_ERR(inode
);
1780 state
= nfs4_get_open_state(inode
, data
->owner
);
1783 if (data
->o_res
.delegation_type
!= 0)
1784 nfs4_opendata_check_deleg(data
, state
);
1785 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1789 nfs_release_seqid(data
->o_arg
.seqid
);
1794 return ERR_PTR(ret
);
1797 static struct nfs4_state
*
1798 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1800 struct nfs4_state
*ret
;
1802 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1803 ret
=_nfs4_opendata_reclaim_to_nfs4_state(data
);
1805 ret
= _nfs4_opendata_to_nfs4_state(data
);
1806 nfs4_sequence_free_slot(&data
->o_res
.seq_res
);
1810 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1812 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1813 struct nfs_open_context
*ctx
;
1815 spin_lock(&state
->inode
->i_lock
);
1816 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1817 if (ctx
->state
!= state
)
1819 get_nfs_open_context(ctx
);
1820 spin_unlock(&state
->inode
->i_lock
);
1823 spin_unlock(&state
->inode
->i_lock
);
1824 return ERR_PTR(-ENOENT
);
1827 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1828 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1830 struct nfs4_opendata
*opendata
;
1832 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1833 NULL
, NULL
, claim
, GFP_NOFS
);
1834 if (opendata
== NULL
)
1835 return ERR_PTR(-ENOMEM
);
1836 opendata
->state
= state
;
1837 atomic_inc(&state
->count
);
1841 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
,
1844 struct nfs4_state
*newstate
;
1847 if (!nfs4_mode_match_open_stateid(opendata
->state
, fmode
))
1849 opendata
->o_arg
.open_flags
= 0;
1850 opendata
->o_arg
.fmode
= fmode
;
1851 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1852 NFS_SB(opendata
->dentry
->d_sb
),
1854 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1855 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1856 nfs4_init_opendata_res(opendata
);
1857 ret
= _nfs4_recover_proc_open(opendata
);
1860 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1861 if (IS_ERR(newstate
))
1862 return PTR_ERR(newstate
);
1863 if (newstate
!= opendata
->state
)
1865 nfs4_close_state(newstate
, fmode
);
1869 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1873 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1874 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1875 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1876 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1877 /* memory barrier prior to reading state->n_* */
1878 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1879 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1881 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1884 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1887 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1891 * We may have performed cached opens for all three recoveries.
1892 * Check if we need to update the current stateid.
1894 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1895 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1896 write_seqlock(&state
->seqlock
);
1897 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1898 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1899 write_sequnlock(&state
->seqlock
);
1906 * reclaim state on the server after a reboot.
1908 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1910 struct nfs_delegation
*delegation
;
1911 struct nfs4_opendata
*opendata
;
1912 fmode_t delegation_type
= 0;
1915 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1916 NFS4_OPEN_CLAIM_PREVIOUS
);
1917 if (IS_ERR(opendata
))
1918 return PTR_ERR(opendata
);
1920 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1921 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1922 delegation_type
= delegation
->type
;
1924 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1925 status
= nfs4_open_recover(opendata
, state
);
1926 nfs4_opendata_put(opendata
);
1930 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1932 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1933 struct nfs4_exception exception
= { };
1936 err
= _nfs4_do_open_reclaim(ctx
, state
);
1937 trace_nfs4_open_reclaim(ctx
, 0, err
);
1938 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1940 if (err
!= -NFS4ERR_DELAY
)
1942 nfs4_handle_exception(server
, err
, &exception
);
1943 } while (exception
.retry
);
1947 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1949 struct nfs_open_context
*ctx
;
1952 ctx
= nfs4_state_find_open_context(state
);
1955 ret
= nfs4_do_open_reclaim(ctx
, state
);
1956 put_nfs_open_context(ctx
);
1960 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1964 printk(KERN_ERR
"NFS: %s: unhandled error "
1965 "%d.\n", __func__
, err
);
1971 case -NFS4ERR_BADSESSION
:
1972 case -NFS4ERR_BADSLOT
:
1973 case -NFS4ERR_BAD_HIGH_SLOT
:
1974 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1975 case -NFS4ERR_DEADSESSION
:
1976 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1977 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1979 case -NFS4ERR_STALE_CLIENTID
:
1980 case -NFS4ERR_STALE_STATEID
:
1981 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1982 /* Don't recall a delegation if it was lost */
1983 nfs4_schedule_lease_recovery(server
->nfs_client
);
1985 case -NFS4ERR_MOVED
:
1986 nfs4_schedule_migration_recovery(server
);
1988 case -NFS4ERR_LEASE_MOVED
:
1989 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1991 case -NFS4ERR_DELEG_REVOKED
:
1992 case -NFS4ERR_ADMIN_REVOKED
:
1993 case -NFS4ERR_EXPIRED
:
1994 case -NFS4ERR_BAD_STATEID
:
1995 case -NFS4ERR_OPENMODE
:
1996 nfs_inode_find_state_and_recover(state
->inode
,
1998 nfs4_schedule_stateid_recovery(server
, state
);
2000 case -NFS4ERR_DELAY
:
2001 case -NFS4ERR_GRACE
:
2002 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2006 case -NFS4ERR_DENIED
:
2007 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2013 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
,
2014 struct nfs4_state
*state
, const nfs4_stateid
*stateid
,
2017 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2018 struct nfs4_opendata
*opendata
;
2021 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2022 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
2023 if (IS_ERR(opendata
))
2024 return PTR_ERR(opendata
);
2025 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
2026 write_seqlock(&state
->seqlock
);
2027 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2028 write_sequnlock(&state
->seqlock
);
2029 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2030 switch (type
& (FMODE_READ
|FMODE_WRITE
)) {
2031 case FMODE_READ
|FMODE_WRITE
:
2033 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
2036 err
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
2040 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
2042 nfs4_opendata_put(opendata
);
2043 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
2046 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
2048 struct nfs4_opendata
*data
= calldata
;
2050 nfs40_setup_sequence(data
->o_arg
.server
->nfs_client
->cl_slot_tbl
,
2051 &data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, task
);
2054 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
2056 struct nfs4_opendata
*data
= calldata
;
2058 nfs40_sequence_done(task
, &data
->c_res
.seq_res
);
2060 data
->rpc_status
= task
->tk_status
;
2061 if (data
->rpc_status
== 0) {
2062 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
2063 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2064 renew_lease(data
->o_res
.server
, data
->timestamp
);
2069 static void nfs4_open_confirm_release(void *calldata
)
2071 struct nfs4_opendata
*data
= calldata
;
2072 struct nfs4_state
*state
= NULL
;
2074 /* If this request hasn't been cancelled, do nothing */
2075 if (data
->cancelled
== 0)
2077 /* In case of error, no cleanup! */
2078 if (!data
->rpc_done
)
2080 state
= nfs4_opendata_to_nfs4_state(data
);
2082 nfs4_close_state(state
, data
->o_arg
.fmode
);
2084 nfs4_opendata_put(data
);
2087 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
2088 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
2089 .rpc_call_done
= nfs4_open_confirm_done
,
2090 .rpc_release
= nfs4_open_confirm_release
,
2094 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2096 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
2098 struct nfs_server
*server
= NFS_SERVER(d_inode(data
->dir
));
2099 struct rpc_task
*task
;
2100 struct rpc_message msg
= {
2101 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
2102 .rpc_argp
= &data
->c_arg
,
2103 .rpc_resp
= &data
->c_res
,
2104 .rpc_cred
= data
->owner
->so_cred
,
2106 struct rpc_task_setup task_setup_data
= {
2107 .rpc_client
= server
->client
,
2108 .rpc_message
= &msg
,
2109 .callback_ops
= &nfs4_open_confirm_ops
,
2110 .callback_data
= data
,
2111 .workqueue
= nfsiod_workqueue
,
2112 .flags
= RPC_TASK_ASYNC
,
2116 nfs4_init_sequence(&data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, 1);
2117 kref_get(&data
->kref
);
2119 data
->rpc_status
= 0;
2120 data
->timestamp
= jiffies
;
2121 if (data
->is_recover
)
2122 nfs4_set_sequence_privileged(&data
->c_arg
.seq_args
);
2123 task
= rpc_run_task(&task_setup_data
);
2125 return PTR_ERR(task
);
2126 status
= nfs4_wait_for_completion_rpc_task(task
);
2128 data
->cancelled
= 1;
2131 status
= data
->rpc_status
;
2136 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
2138 struct nfs4_opendata
*data
= calldata
;
2139 struct nfs4_state_owner
*sp
= data
->owner
;
2140 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
2141 enum open_claim_type4 claim
= data
->o_arg
.claim
;
2143 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
2146 * Check if we still need to send an OPEN call, or if we can use
2147 * a delegation instead.
2149 if (data
->state
!= NULL
) {
2150 struct nfs_delegation
*delegation
;
2152 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
2155 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
2156 if (can_open_delegated(delegation
, data
->o_arg
.fmode
, claim
))
2157 goto unlock_no_action
;
2160 /* Update client id. */
2161 data
->o_arg
.clientid
= clp
->cl_clientid
;
2165 case NFS4_OPEN_CLAIM_PREVIOUS
:
2166 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
2167 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
2168 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
2169 case NFS4_OPEN_CLAIM_FH
:
2170 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
2171 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
2173 data
->timestamp
= jiffies
;
2174 if (nfs4_setup_sequence(data
->o_arg
.server
,
2175 &data
->o_arg
.seq_args
,
2176 &data
->o_res
.seq_res
,
2178 nfs_release_seqid(data
->o_arg
.seqid
);
2180 /* Set the create mode (note dependency on the session type) */
2181 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
2182 if (data
->o_arg
.open_flags
& O_EXCL
) {
2183 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
2184 if (nfs4_has_persistent_session(clp
))
2185 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
2186 else if (clp
->cl_mvops
->minor_version
> 0)
2187 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
2191 trace_nfs4_cached_open(data
->state
);
2194 task
->tk_action
= NULL
;
2196 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
2199 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
2201 struct nfs4_opendata
*data
= calldata
;
2203 data
->rpc_status
= task
->tk_status
;
2205 if (!nfs4_sequence_process(task
, &data
->o_res
.seq_res
))
2208 if (task
->tk_status
== 0) {
2209 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
2210 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
2214 data
->rpc_status
= -ELOOP
;
2217 data
->rpc_status
= -EISDIR
;
2220 data
->rpc_status
= -ENOTDIR
;
2223 renew_lease(data
->o_res
.server
, data
->timestamp
);
2224 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
2225 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2230 static void nfs4_open_release(void *calldata
)
2232 struct nfs4_opendata
*data
= calldata
;
2233 struct nfs4_state
*state
= NULL
;
2235 /* If this request hasn't been cancelled, do nothing */
2236 if (data
->cancelled
== 0)
2238 /* In case of error, no cleanup! */
2239 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
2241 /* In case we need an open_confirm, no cleanup! */
2242 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2244 state
= nfs4_opendata_to_nfs4_state(data
);
2246 nfs4_close_state(state
, data
->o_arg
.fmode
);
2248 nfs4_opendata_put(data
);
2251 static const struct rpc_call_ops nfs4_open_ops
= {
2252 .rpc_call_prepare
= nfs4_open_prepare
,
2253 .rpc_call_done
= nfs4_open_done
,
2254 .rpc_release
= nfs4_open_release
,
2257 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
2259 struct inode
*dir
= d_inode(data
->dir
);
2260 struct nfs_server
*server
= NFS_SERVER(dir
);
2261 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2262 struct nfs_openres
*o_res
= &data
->o_res
;
2263 struct rpc_task
*task
;
2264 struct rpc_message msg
= {
2265 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
2268 .rpc_cred
= data
->owner
->so_cred
,
2270 struct rpc_task_setup task_setup_data
= {
2271 .rpc_client
= server
->client
,
2272 .rpc_message
= &msg
,
2273 .callback_ops
= &nfs4_open_ops
,
2274 .callback_data
= data
,
2275 .workqueue
= nfsiod_workqueue
,
2276 .flags
= RPC_TASK_ASYNC
,
2280 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
2281 kref_get(&data
->kref
);
2283 data
->rpc_status
= 0;
2284 data
->cancelled
= 0;
2285 data
->is_recover
= 0;
2287 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2288 data
->is_recover
= 1;
2290 task
= rpc_run_task(&task_setup_data
);
2292 return PTR_ERR(task
);
2293 status
= nfs4_wait_for_completion_rpc_task(task
);
2295 data
->cancelled
= 1;
2298 status
= data
->rpc_status
;
2304 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
2306 struct inode
*dir
= d_inode(data
->dir
);
2307 struct nfs_openres
*o_res
= &data
->o_res
;
2310 status
= nfs4_run_open_task(data
, 1);
2311 if (status
!= 0 || !data
->rpc_done
)
2314 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
2316 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2317 status
= _nfs4_proc_open_confirm(data
);
2326 * Additional permission checks in order to distinguish between an
2327 * open for read, and an open for execute. This works around the
2328 * fact that NFSv4 OPEN treats read and execute permissions as being
2330 * Note that in the non-execute case, we want to turn off permission
2331 * checking if we just created a new file (POSIX open() semantics).
2333 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2334 struct nfs4_opendata
*opendata
,
2335 struct nfs4_state
*state
, fmode_t fmode
,
2338 struct nfs_access_entry cache
;
2341 /* access call failed or for some reason the server doesn't
2342 * support any access modes -- defer access call until later */
2343 if (opendata
->o_res
.access_supported
== 0)
2348 * Use openflags to check for exec, because fmode won't
2349 * always have FMODE_EXEC set when file open for exec.
2351 if (openflags
& __FMODE_EXEC
) {
2352 /* ONLY check for exec rights */
2354 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2358 cache
.jiffies
= jiffies
;
2359 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2360 nfs_access_add_cache(state
->inode
, &cache
);
2362 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
2365 /* even though OPEN succeeded, access is denied. Close the file */
2366 nfs4_close_state(state
, fmode
);
2371 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2373 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2375 struct inode
*dir
= d_inode(data
->dir
);
2376 struct nfs_server
*server
= NFS_SERVER(dir
);
2377 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2378 struct nfs_openres
*o_res
= &data
->o_res
;
2381 status
= nfs4_run_open_task(data
, 0);
2382 if (!data
->rpc_done
)
2385 if (status
== -NFS4ERR_BADNAME
&&
2386 !(o_arg
->open_flags
& O_CREAT
))
2391 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2393 if (o_arg
->open_flags
& O_CREAT
) {
2394 update_changeattr(dir
, &o_res
->cinfo
);
2395 if (o_arg
->open_flags
& O_EXCL
)
2396 data
->file_created
= 1;
2397 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2398 data
->file_created
= 1;
2400 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2401 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2402 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2403 status
= _nfs4_proc_open_confirm(data
);
2407 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
2408 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2412 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
2414 return nfs4_client_recover_expired_lease(server
->nfs_client
);
2419 * reclaim state on the server after a network partition.
2420 * Assumes caller holds the appropriate lock
2422 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2424 struct nfs4_opendata
*opendata
;
2427 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2428 NFS4_OPEN_CLAIM_FH
);
2429 if (IS_ERR(opendata
))
2430 return PTR_ERR(opendata
);
2431 ret
= nfs4_open_recover(opendata
, state
);
2433 d_drop(ctx
->dentry
);
2434 nfs4_opendata_put(opendata
);
2438 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2440 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2441 struct nfs4_exception exception
= { };
2445 err
= _nfs4_open_expired(ctx
, state
);
2446 trace_nfs4_open_expired(ctx
, 0, err
);
2447 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2452 case -NFS4ERR_GRACE
:
2453 case -NFS4ERR_DELAY
:
2454 nfs4_handle_exception(server
, err
, &exception
);
2457 } while (exception
.retry
);
2462 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2464 struct nfs_open_context
*ctx
;
2467 ctx
= nfs4_state_find_open_context(state
);
2470 ret
= nfs4_do_open_expired(ctx
, state
);
2471 put_nfs_open_context(ctx
);
2475 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
,
2476 const nfs4_stateid
*stateid
)
2478 nfs_remove_bad_delegation(state
->inode
, stateid
);
2479 write_seqlock(&state
->seqlock
);
2480 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2481 write_sequnlock(&state
->seqlock
);
2482 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2485 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2487 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2488 nfs_finish_clear_delegation_stateid(state
, NULL
);
2491 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2493 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2494 nfs40_clear_delegation_stateid(state
);
2495 return nfs4_open_expired(sp
, state
);
2498 static int nfs40_test_and_free_expired_stateid(struct nfs_server
*server
,
2499 nfs4_stateid
*stateid
,
2500 struct rpc_cred
*cred
)
2502 return -NFS4ERR_BAD_STATEID
;
2505 #if defined(CONFIG_NFS_V4_1)
2506 static int nfs41_test_and_free_expired_stateid(struct nfs_server
*server
,
2507 nfs4_stateid
*stateid
,
2508 struct rpc_cred
*cred
)
2512 switch (stateid
->type
) {
2515 case NFS4_INVALID_STATEID_TYPE
:
2516 case NFS4_SPECIAL_STATEID_TYPE
:
2517 return -NFS4ERR_BAD_STATEID
;
2518 case NFS4_REVOKED_STATEID_TYPE
:
2522 status
= nfs41_test_stateid(server
, stateid
, cred
);
2524 case -NFS4ERR_EXPIRED
:
2525 case -NFS4ERR_ADMIN_REVOKED
:
2526 case -NFS4ERR_DELEG_REVOKED
:
2532 /* Ack the revoked state to the server */
2533 nfs41_free_stateid(server
, stateid
, cred
, true);
2534 return -NFS4ERR_EXPIRED
;
2537 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2539 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2540 nfs4_stateid stateid
;
2541 struct nfs_delegation
*delegation
;
2542 struct rpc_cred
*cred
;
2545 /* Get the delegation credential for use by test/free_stateid */
2547 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2548 if (delegation
== NULL
) {
2553 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2554 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
2556 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2560 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
)) {
2565 cred
= get_rpccred(delegation
->cred
);
2567 status
= nfs41_test_and_free_expired_stateid(server
, &stateid
, cred
);
2568 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2569 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
2570 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2576 * nfs41_check_expired_locks - possibly free a lock stateid
2578 * @state: NFSv4 state for an inode
2580 * Returns NFS_OK if recovery for this stateid is now finished.
2581 * Otherwise a negative NFS4ERR value is returned.
2583 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
2585 int status
, ret
= NFS_OK
;
2586 struct nfs4_lock_state
*lsp
, *prev
= NULL
;
2587 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2589 if (!test_bit(LK_STATE_IN_USE
, &state
->flags
))
2592 spin_lock(&state
->state_lock
);
2593 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
2594 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
2595 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
2597 atomic_inc(&lsp
->ls_count
);
2598 spin_unlock(&state
->state_lock
);
2600 nfs4_put_lock_state(prev
);
2603 status
= nfs41_test_and_free_expired_stateid(server
,
2606 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
2607 if (status
== -NFS4ERR_EXPIRED
||
2608 status
== -NFS4ERR_BAD_STATEID
) {
2609 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
2610 lsp
->ls_stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
2611 if (!recover_lost_locks
)
2612 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2613 } else if (status
!= NFS_OK
) {
2615 nfs4_put_lock_state(prev
);
2618 spin_lock(&state
->state_lock
);
2621 spin_unlock(&state
->state_lock
);
2622 nfs4_put_lock_state(prev
);
2628 * nfs41_check_open_stateid - possibly free an open stateid
2630 * @state: NFSv4 state for an inode
2632 * Returns NFS_OK if recovery for this stateid is now finished.
2633 * Otherwise a negative NFS4ERR value is returned.
2635 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2637 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2638 nfs4_stateid
*stateid
= &state
->open_stateid
;
2639 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2642 if (test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0) {
2643 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0) {
2644 if (nfs4_have_delegation(state
->inode
, state
->state
))
2646 return -NFS4ERR_OPENMODE
;
2648 return -NFS4ERR_BAD_STATEID
;
2650 status
= nfs41_test_and_free_expired_stateid(server
, stateid
, cred
);
2651 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2652 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
) {
2653 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2654 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2655 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2656 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2657 stateid
->type
= NFS4_INVALID_STATEID_TYPE
;
2659 if (status
!= NFS_OK
)
2661 if (nfs_open_stateid_recover_openmode(state
))
2662 return -NFS4ERR_OPENMODE
;
2666 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2670 nfs41_check_delegation_stateid(state
);
2671 status
= nfs41_check_expired_locks(state
);
2672 if (status
!= NFS_OK
)
2674 status
= nfs41_check_open_stateid(state
);
2675 if (status
!= NFS_OK
)
2676 status
= nfs4_open_expired(sp
, state
);
2682 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2683 * fields corresponding to attributes that were used to store the verifier.
2684 * Make sure we clobber those fields in the later setattr call
2686 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2687 struct iattr
*sattr
, struct nfs4_label
**label
)
2689 const u32
*attrset
= opendata
->o_res
.attrset
;
2691 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2692 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2693 sattr
->ia_valid
|= ATTR_ATIME
;
2695 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2696 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2697 sattr
->ia_valid
|= ATTR_MTIME
;
2699 /* Except MODE, it seems harmless of setting twice. */
2700 if ((attrset
[1] & FATTR4_WORD1_MODE
))
2701 sattr
->ia_valid
&= ~ATTR_MODE
;
2703 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2707 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2710 struct nfs_open_context
*ctx
)
2712 struct nfs4_state_owner
*sp
= opendata
->owner
;
2713 struct nfs_server
*server
= sp
->so_server
;
2714 struct dentry
*dentry
;
2715 struct nfs4_state
*state
;
2719 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2721 ret
= _nfs4_proc_open(opendata
);
2725 state
= nfs4_opendata_to_nfs4_state(opendata
);
2726 ret
= PTR_ERR(state
);
2729 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2730 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2731 if (opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
)
2732 set_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
);
2734 dentry
= opendata
->dentry
;
2735 if (d_really_is_negative(dentry
)) {
2736 struct dentry
*alias
;
2738 alias
= d_exact_alias(dentry
, state
->inode
);
2740 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2741 /* d_splice_alias() can't fail here - it's a non-directory */
2744 ctx
->dentry
= dentry
= alias
;
2746 nfs_set_verifier(dentry
,
2747 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2750 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2755 if (d_inode(dentry
) == state
->inode
) {
2756 nfs_inode_attach_open_context(ctx
);
2757 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2758 nfs4_schedule_stateid_recovery(server
, state
);
2765 * Returns a referenced nfs4_state
2767 static int _nfs4_do_open(struct inode
*dir
,
2768 struct nfs_open_context
*ctx
,
2770 struct iattr
*sattr
,
2771 struct nfs4_label
*label
,
2774 struct nfs4_state_owner
*sp
;
2775 struct nfs4_state
*state
= NULL
;
2776 struct nfs_server
*server
= NFS_SERVER(dir
);
2777 struct nfs4_opendata
*opendata
;
2778 struct dentry
*dentry
= ctx
->dentry
;
2779 struct rpc_cred
*cred
= ctx
->cred
;
2780 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2781 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2782 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2783 struct nfs4_label
*olabel
= NULL
;
2786 /* Protect against reboot recovery conflicts */
2788 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2790 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2793 status
= nfs4_recover_expired_lease(server
);
2795 goto err_put_state_owner
;
2796 if (d_really_is_positive(dentry
))
2797 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2799 if (d_really_is_positive(dentry
))
2800 claim
= NFS4_OPEN_CLAIM_FH
;
2801 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2802 label
, claim
, GFP_KERNEL
);
2803 if (opendata
== NULL
)
2804 goto err_put_state_owner
;
2807 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2808 if (IS_ERR(olabel
)) {
2809 status
= PTR_ERR(olabel
);
2810 goto err_opendata_put
;
2814 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2815 if (!opendata
->f_attr
.mdsthreshold
) {
2816 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2817 if (!opendata
->f_attr
.mdsthreshold
)
2818 goto err_free_label
;
2820 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2822 if (d_really_is_positive(dentry
))
2823 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2825 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2827 goto err_free_label
;
2830 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2831 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2832 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2834 * send create attributes which was not set by open
2835 * with an extra setattr.
2837 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2838 nfs_fattr_init(opendata
->o_res
.f_attr
);
2839 status
= nfs4_do_setattr(state
->inode
, cred
,
2840 opendata
->o_res
.f_attr
, sattr
,
2841 ctx
, label
, olabel
);
2843 nfs_setattr_update_inode(state
->inode
, sattr
,
2844 opendata
->o_res
.f_attr
);
2845 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2849 if (opened
&& opendata
->file_created
)
2850 *opened
|= FILE_CREATED
;
2852 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2853 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2854 opendata
->f_attr
.mdsthreshold
= NULL
;
2857 nfs4_label_free(olabel
);
2859 nfs4_opendata_put(opendata
);
2860 nfs4_put_state_owner(sp
);
2863 nfs4_label_free(olabel
);
2865 nfs4_opendata_put(opendata
);
2866 err_put_state_owner
:
2867 nfs4_put_state_owner(sp
);
2873 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2874 struct nfs_open_context
*ctx
,
2876 struct iattr
*sattr
,
2877 struct nfs4_label
*label
,
2880 struct nfs_server
*server
= NFS_SERVER(dir
);
2881 struct nfs4_exception exception
= { };
2882 struct nfs4_state
*res
;
2886 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2888 trace_nfs4_open_file(ctx
, flags
, status
);
2891 /* NOTE: BAD_SEQID means the server and client disagree about the
2892 * book-keeping w.r.t. state-changing operations
2893 * (OPEN/CLOSE/LOCK/LOCKU...)
2894 * It is actually a sign of a bug on the client or on the server.
2896 * If we receive a BAD_SEQID error in the particular case of
2897 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2898 * have unhashed the old state_owner for us, and that we can
2899 * therefore safely retry using a new one. We should still warn
2900 * the user though...
2902 if (status
== -NFS4ERR_BAD_SEQID
) {
2903 pr_warn_ratelimited("NFS: v4 server %s "
2904 " returned a bad sequence-id error!\n",
2905 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2906 exception
.retry
= 1;
2910 * BAD_STATEID on OPEN means that the server cancelled our
2911 * state before it received the OPEN_CONFIRM.
2912 * Recover by retrying the request as per the discussion
2913 * on Page 181 of RFC3530.
2915 if (status
== -NFS4ERR_BAD_STATEID
) {
2916 exception
.retry
= 1;
2919 if (status
== -EAGAIN
) {
2920 /* We must have found a delegation */
2921 exception
.retry
= 1;
2924 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2926 res
= ERR_PTR(nfs4_handle_exception(server
,
2927 status
, &exception
));
2928 } while (exception
.retry
);
2932 static int _nfs4_do_setattr(struct inode
*inode
,
2933 struct nfs_setattrargs
*arg
,
2934 struct nfs_setattrres
*res
,
2935 struct rpc_cred
*cred
,
2936 struct nfs_open_context
*ctx
)
2938 struct nfs_server
*server
= NFS_SERVER(inode
);
2939 struct rpc_message msg
= {
2940 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2945 struct rpc_cred
*delegation_cred
= NULL
;
2946 unsigned long timestamp
= jiffies
;
2951 nfs_fattr_init(res
->fattr
);
2953 /* Servers should only apply open mode checks for file size changes */
2954 truncate
= (arg
->iap
->ia_valid
& ATTR_SIZE
) ? true : false;
2955 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2957 if (nfs4_copy_delegation_stateid(inode
, fmode
, &arg
->stateid
, &delegation_cred
)) {
2958 /* Use that stateid */
2959 } else if (truncate
&& ctx
!= NULL
) {
2960 struct nfs_lock_context
*l_ctx
;
2961 if (!nfs4_valid_open_stateid(ctx
->state
))
2963 l_ctx
= nfs_get_lock_context(ctx
);
2965 return PTR_ERR(l_ctx
);
2966 status
= nfs4_select_rw_stateid(ctx
->state
, FMODE_WRITE
, l_ctx
,
2967 &arg
->stateid
, &delegation_cred
);
2968 nfs_put_lock_context(l_ctx
);
2972 nfs4_stateid_copy(&arg
->stateid
, &zero_stateid
);
2973 if (delegation_cred
)
2974 msg
.rpc_cred
= delegation_cred
;
2976 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
->seq_args
, &res
->seq_res
, 1);
2978 put_rpccred(delegation_cred
);
2979 if (status
== 0 && ctx
!= NULL
)
2980 renew_lease(server
, timestamp
);
2981 trace_nfs4_setattr(inode
, &arg
->stateid
, status
);
2985 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2986 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2987 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
2988 struct nfs4_label
*olabel
)
2990 struct nfs_server
*server
= NFS_SERVER(inode
);
2991 struct nfs4_state
*state
= ctx
? ctx
->state
: NULL
;
2992 struct nfs_setattrargs arg
= {
2993 .fh
= NFS_FH(inode
),
2996 .bitmask
= server
->attr_bitmask
,
2999 struct nfs_setattrres res
= {
3004 struct nfs4_exception exception
= {
3007 .stateid
= &arg
.stateid
,
3011 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
3013 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
3016 err
= _nfs4_do_setattr(inode
, &arg
, &res
, cred
, ctx
);
3018 case -NFS4ERR_OPENMODE
:
3019 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
3020 pr_warn_once("NFSv4: server %s is incorrectly "
3021 "applying open mode checks to "
3022 "a SETATTR that is not "
3023 "changing file size.\n",
3024 server
->nfs_client
->cl_hostname
);
3026 if (state
&& !(state
->state
& FMODE_WRITE
)) {
3028 if (sattr
->ia_valid
& ATTR_OPEN
)
3033 err
= nfs4_handle_exception(server
, err
, &exception
);
3034 } while (exception
.retry
);
3040 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
3042 if (inode
== NULL
|| !nfs_have_layout(inode
))
3045 return pnfs_wait_on_layoutreturn(inode
, task
);
3048 struct nfs4_closedata
{
3049 struct inode
*inode
;
3050 struct nfs4_state
*state
;
3051 struct nfs_closeargs arg
;
3052 struct nfs_closeres res
;
3054 struct nfs4_layoutreturn_args arg
;
3055 struct nfs4_layoutreturn_res res
;
3056 struct nfs4_xdr_opaque_data ld_private
;
3060 struct nfs_fattr fattr
;
3061 unsigned long timestamp
;
3064 static void nfs4_free_closedata(void *data
)
3066 struct nfs4_closedata
*calldata
= data
;
3067 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
3068 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
3070 if (calldata
->lr
.roc
)
3071 pnfs_roc_release(&calldata
->lr
.arg
, &calldata
->lr
.res
,
3072 calldata
->res
.lr_ret
);
3073 nfs4_put_open_state(calldata
->state
);
3074 nfs_free_seqid(calldata
->arg
.seqid
);
3075 nfs4_put_state_owner(sp
);
3076 nfs_sb_deactive(sb
);
3080 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
3082 struct nfs4_closedata
*calldata
= data
;
3083 struct nfs4_state
*state
= calldata
->state
;
3084 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
3085 nfs4_stateid
*res_stateid
= NULL
;
3087 dprintk("%s: begin!\n", __func__
);
3088 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
3090 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
3092 /* Handle Layoutreturn errors */
3093 if (calldata
->arg
.lr_args
&& task
->tk_status
!= 0) {
3094 switch (calldata
->res
.lr_ret
) {
3096 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3099 calldata
->arg
.lr_args
= NULL
;
3100 calldata
->res
.lr_res
= NULL
;
3102 case -NFS4ERR_ADMIN_REVOKED
:
3103 case -NFS4ERR_DELEG_REVOKED
:
3104 case -NFS4ERR_EXPIRED
:
3105 case -NFS4ERR_BAD_STATEID
:
3106 case -NFS4ERR_OLD_STATEID
:
3107 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
3108 case -NFS4ERR_WRONG_CRED
:
3109 calldata
->arg
.lr_args
= NULL
;
3110 calldata
->res
.lr_res
= NULL
;
3111 calldata
->res
.lr_ret
= 0;
3112 rpc_restart_call_prepare(task
);
3117 /* hmm. we are done with the inode, and in the process of freeing
3118 * the state_owner. we keep this around to process errors
3120 switch (task
->tk_status
) {
3122 res_stateid
= &calldata
->res
.stateid
;
3123 renew_lease(server
, calldata
->timestamp
);
3125 case -NFS4ERR_ACCESS
:
3126 if (calldata
->arg
.bitmask
!= NULL
) {
3127 calldata
->arg
.bitmask
= NULL
;
3128 calldata
->res
.fattr
= NULL
;
3129 task
->tk_status
= 0;
3130 rpc_restart_call_prepare(task
);
3135 case -NFS4ERR_ADMIN_REVOKED
:
3136 case -NFS4ERR_STALE_STATEID
:
3137 case -NFS4ERR_EXPIRED
:
3138 nfs4_free_revoked_stateid(server
,
3139 &calldata
->arg
.stateid
,
3140 task
->tk_msg
.rpc_cred
);
3141 case -NFS4ERR_OLD_STATEID
:
3142 case -NFS4ERR_BAD_STATEID
:
3143 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
3144 &state
->open_stateid
)) {
3145 rpc_restart_call_prepare(task
);
3148 if (calldata
->arg
.fmode
== 0)
3151 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
3152 rpc_restart_call_prepare(task
);
3156 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
3157 res_stateid
, calldata
->arg
.fmode
);
3159 nfs_release_seqid(calldata
->arg
.seqid
);
3160 nfs_refresh_inode(calldata
->inode
, &calldata
->fattr
);
3161 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
3164 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
3166 struct nfs4_closedata
*calldata
= data
;
3167 struct nfs4_state
*state
= calldata
->state
;
3168 struct inode
*inode
= calldata
->inode
;
3169 bool is_rdonly
, is_wronly
, is_rdwr
;
3172 dprintk("%s: begin!\n", __func__
);
3173 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3176 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
3177 spin_lock(&state
->owner
->so_lock
);
3178 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
3179 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
3180 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
3181 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
3182 /* Calculate the change in open mode */
3183 calldata
->arg
.fmode
= 0;
3184 if (state
->n_rdwr
== 0) {
3185 if (state
->n_rdonly
== 0)
3186 call_close
|= is_rdonly
;
3188 calldata
->arg
.fmode
|= FMODE_READ
;
3189 if (state
->n_wronly
== 0)
3190 call_close
|= is_wronly
;
3192 calldata
->arg
.fmode
|= FMODE_WRITE
;
3193 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
3194 call_close
|= is_rdwr
;
3196 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
3198 if (!nfs4_valid_open_stateid(state
) ||
3199 test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
3201 spin_unlock(&state
->owner
->so_lock
);
3204 /* Note: exit _without_ calling nfs4_close_done */
3208 if (!calldata
->lr
.roc
&& nfs4_wait_on_layoutreturn(inode
, task
)) {
3209 nfs_release_seqid(calldata
->arg
.seqid
);
3213 if (calldata
->arg
.fmode
== 0)
3214 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
3216 if (calldata
->arg
.fmode
== 0 || calldata
->arg
.fmode
== FMODE_READ
) {
3217 /* Close-to-open cache consistency revalidation */
3218 if (!nfs4_have_delegation(inode
, FMODE_READ
))
3219 calldata
->arg
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
3221 calldata
->arg
.bitmask
= NULL
;
3224 calldata
->arg
.share_access
=
3225 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
3226 calldata
->arg
.fmode
, 0);
3228 if (calldata
->res
.fattr
== NULL
)
3229 calldata
->arg
.bitmask
= NULL
;
3230 else if (calldata
->arg
.bitmask
== NULL
)
3231 calldata
->res
.fattr
= NULL
;
3232 calldata
->timestamp
= jiffies
;
3233 if (nfs4_setup_sequence(NFS_SERVER(inode
),
3234 &calldata
->arg
.seq_args
,
3235 &calldata
->res
.seq_res
,
3237 nfs_release_seqid(calldata
->arg
.seqid
);
3238 dprintk("%s: done!\n", __func__
);
3241 task
->tk_action
= NULL
;
3243 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
3246 static const struct rpc_call_ops nfs4_close_ops
= {
3247 .rpc_call_prepare
= nfs4_close_prepare
,
3248 .rpc_call_done
= nfs4_close_done
,
3249 .rpc_release
= nfs4_free_closedata
,
3253 * It is possible for data to be read/written from a mem-mapped file
3254 * after the sys_close call (which hits the vfs layer as a flush).
3255 * This means that we can't safely call nfsv4 close on a file until
3256 * the inode is cleared. This in turn means that we are not good
3257 * NFSv4 citizens - we do not indicate to the server to update the file's
3258 * share state even when we are done with one of the three share
3259 * stateid's in the inode.
3261 * NOTE: Caller must be holding the sp->so_owner semaphore!
3263 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
3265 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3266 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
3267 struct nfs4_closedata
*calldata
;
3268 struct nfs4_state_owner
*sp
= state
->owner
;
3269 struct rpc_task
*task
;
3270 struct rpc_message msg
= {
3271 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
3272 .rpc_cred
= state
->owner
->so_cred
,
3274 struct rpc_task_setup task_setup_data
= {
3275 .rpc_client
= server
->client
,
3276 .rpc_message
= &msg
,
3277 .callback_ops
= &nfs4_close_ops
,
3278 .workqueue
= nfsiod_workqueue
,
3279 .flags
= RPC_TASK_ASYNC
,
3281 int status
= -ENOMEM
;
3283 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
3284 &task_setup_data
.rpc_client
, &msg
);
3286 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
3287 if (calldata
== NULL
)
3289 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
3290 calldata
->inode
= state
->inode
;
3291 calldata
->state
= state
;
3292 calldata
->arg
.fh
= NFS_FH(state
->inode
);
3293 /* Serialization for the sequence id */
3294 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
3295 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
3296 if (IS_ERR(calldata
->arg
.seqid
))
3297 goto out_free_calldata
;
3298 nfs_fattr_init(&calldata
->fattr
);
3299 calldata
->arg
.fmode
= 0;
3300 calldata
->lr
.arg
.ld_private
= &calldata
->lr
.ld_private
;
3301 calldata
->res
.fattr
= &calldata
->fattr
;
3302 calldata
->res
.seqid
= calldata
->arg
.seqid
;
3303 calldata
->res
.server
= server
;
3304 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3305 calldata
->lr
.roc
= pnfs_roc(state
->inode
,
3306 &calldata
->lr
.arg
, &calldata
->lr
.res
, msg
.rpc_cred
);
3307 if (calldata
->lr
.roc
) {
3308 calldata
->arg
.lr_args
= &calldata
->lr
.arg
;
3309 calldata
->res
.lr_res
= &calldata
->lr
.res
;
3311 nfs_sb_active(calldata
->inode
->i_sb
);
3313 msg
.rpc_argp
= &calldata
->arg
;
3314 msg
.rpc_resp
= &calldata
->res
;
3315 task_setup_data
.callback_data
= calldata
;
3316 task
= rpc_run_task(&task_setup_data
);
3318 return PTR_ERR(task
);
3321 status
= rpc_wait_for_completion_task(task
);
3327 nfs4_put_open_state(state
);
3328 nfs4_put_state_owner(sp
);
3332 static struct inode
*
3333 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3334 int open_flags
, struct iattr
*attr
, int *opened
)
3336 struct nfs4_state
*state
;
3337 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3339 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3341 /* Protect against concurrent sillydeletes */
3342 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3344 nfs4_label_release_security(label
);
3347 return ERR_CAST(state
);
3348 return state
->inode
;
3351 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3353 if (ctx
->state
== NULL
)
3356 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3358 nfs4_close_state(ctx
->state
, ctx
->mode
);
3361 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3362 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3363 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3365 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3367 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3368 struct nfs4_server_caps_arg args
= {
3372 struct nfs4_server_caps_res res
= {};
3373 struct rpc_message msg
= {
3374 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3380 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3381 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3382 FATTR4_WORD0_LINK_SUPPORT
|
3383 FATTR4_WORD0_SYMLINK_SUPPORT
|
3384 FATTR4_WORD0_ACLSUPPORT
;
3386 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3388 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3390 /* Sanity check the server answers */
3391 switch (minorversion
) {
3393 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3394 res
.attr_bitmask
[2] = 0;
3397 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3400 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3402 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3403 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3404 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3405 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3406 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3407 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3408 NFS_CAP_SECURITY_LABEL
);
3409 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3410 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3411 server
->caps
|= NFS_CAP_ACLS
;
3412 if (res
.has_links
!= 0)
3413 server
->caps
|= NFS_CAP_HARDLINKS
;
3414 if (res
.has_symlinks
!= 0)
3415 server
->caps
|= NFS_CAP_SYMLINKS
;
3416 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3417 server
->caps
|= NFS_CAP_FILEID
;
3418 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3419 server
->caps
|= NFS_CAP_MODE
;
3420 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3421 server
->caps
|= NFS_CAP_NLINK
;
3422 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3423 server
->caps
|= NFS_CAP_OWNER
;
3424 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3425 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3426 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3427 server
->caps
|= NFS_CAP_ATIME
;
3428 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3429 server
->caps
|= NFS_CAP_CTIME
;
3430 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3431 server
->caps
|= NFS_CAP_MTIME
;
3432 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3433 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3434 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3436 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3437 sizeof(server
->attr_bitmask
));
3438 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3440 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3441 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3442 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3443 server
->cache_consistency_bitmask
[2] = 0;
3444 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3445 sizeof(server
->exclcreat_bitmask
));
3446 server
->acl_bitmask
= res
.acl_bitmask
;
3447 server
->fh_expire_type
= res
.fh_expire_type
;
3453 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3455 struct nfs4_exception exception
= { };
3458 err
= nfs4_handle_exception(server
,
3459 _nfs4_server_capabilities(server
, fhandle
),
3461 } while (exception
.retry
);
3465 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3466 struct nfs_fsinfo
*info
)
3469 struct nfs4_lookup_root_arg args
= {
3472 struct nfs4_lookup_res res
= {
3474 .fattr
= info
->fattr
,
3477 struct rpc_message msg
= {
3478 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3483 bitmask
[0] = nfs4_fattr_bitmap
[0];
3484 bitmask
[1] = nfs4_fattr_bitmap
[1];
3486 * Process the label in the upcoming getfattr
3488 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3490 nfs_fattr_init(info
->fattr
);
3491 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3494 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3495 struct nfs_fsinfo
*info
)
3497 struct nfs4_exception exception
= { };
3500 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3501 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3504 case -NFS4ERR_WRONGSEC
:
3507 err
= nfs4_handle_exception(server
, err
, &exception
);
3509 } while (exception
.retry
);
3514 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3515 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3517 struct rpc_auth_create_args auth_args
= {
3518 .pseudoflavor
= flavor
,
3520 struct rpc_auth
*auth
;
3523 auth
= rpcauth_create(&auth_args
, server
->client
);
3528 ret
= nfs4_lookup_root(server
, fhandle
, info
);
3534 * Retry pseudoroot lookup with various security flavors. We do this when:
3536 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3537 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3539 * Returns zero on success, or a negative NFS4ERR value, or a
3540 * negative errno value.
3542 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3543 struct nfs_fsinfo
*info
)
3545 /* Per 3530bis 15.33.5 */
3546 static const rpc_authflavor_t flav_array
[] = {
3550 RPC_AUTH_UNIX
, /* courtesy */
3553 int status
= -EPERM
;
3556 if (server
->auth_info
.flavor_len
> 0) {
3557 /* try each flavor specified by user */
3558 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3559 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3560 server
->auth_info
.flavors
[i
]);
3561 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3566 /* no flavors specified by user, try default list */
3567 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3568 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3570 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3577 * -EACCESS could mean that the user doesn't have correct permissions
3578 * to access the mount. It could also mean that we tried to mount
3579 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3580 * existing mount programs don't handle -EACCES very well so it should
3581 * be mapped to -EPERM instead.
3583 if (status
== -EACCES
)
3589 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3590 * @server: initialized nfs_server handle
3591 * @fhandle: we fill in the pseudo-fs root file handle
3592 * @info: we fill in an FSINFO struct
3593 * @auth_probe: probe the auth flavours
3595 * Returns zero on success, or a negative errno.
3597 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3598 struct nfs_fsinfo
*info
,
3604 status
= nfs4_lookup_root(server
, fhandle
, info
);
3606 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3607 status
= server
->nfs_client
->cl_mvops
->find_root_sec(server
,
3611 status
= nfs4_server_capabilities(server
, fhandle
);
3613 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3615 return nfs4_map_errors(status
);
3618 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3619 struct nfs_fsinfo
*info
)
3622 struct nfs_fattr
*fattr
= info
->fattr
;
3623 struct nfs4_label
*label
= NULL
;
3625 error
= nfs4_server_capabilities(server
, mntfh
);
3627 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3631 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3633 return PTR_ERR(label
);
3635 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3637 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3638 goto err_free_label
;
3641 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3642 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3643 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3646 nfs4_label_free(label
);
3652 * Get locations and (maybe) other attributes of a referral.
3653 * Note that we'll actually follow the referral later when
3654 * we detect fsid mismatch in inode revalidation
3656 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3657 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3658 struct nfs_fh
*fhandle
)
3660 int status
= -ENOMEM
;
3661 struct page
*page
= NULL
;
3662 struct nfs4_fs_locations
*locations
= NULL
;
3664 page
= alloc_page(GFP_KERNEL
);
3667 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3668 if (locations
== NULL
)
3671 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3676 * If the fsid didn't change, this is a migration event, not a
3677 * referral. Cause us to drop into the exception handler, which
3678 * will kick off migration recovery.
3680 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3681 dprintk("%s: server did not return a different fsid for"
3682 " a referral at %s\n", __func__
, name
->name
);
3683 status
= -NFS4ERR_MOVED
;
3686 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3687 nfs_fixup_referral_attributes(&locations
->fattr
);
3689 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3690 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3691 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3699 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3700 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3702 struct nfs4_getattr_arg args
= {
3704 .bitmask
= server
->attr_bitmask
,
3706 struct nfs4_getattr_res res
= {
3711 struct rpc_message msg
= {
3712 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3717 args
.bitmask
= nfs4_bitmask(server
, label
);
3719 nfs_fattr_init(fattr
);
3720 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3723 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3724 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3726 struct nfs4_exception exception
= { };
3729 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3730 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3731 err
= nfs4_handle_exception(server
, err
,
3733 } while (exception
.retry
);
3738 * The file is not closed if it is opened due to the a request to change
3739 * the size of the file. The open call will not be needed once the
3740 * VFS layer lookup-intents are implemented.
3742 * Close is called when the inode is destroyed.
3743 * If we haven't opened the file for O_WRONLY, we
3744 * need to in the size_change case to obtain a stateid.
3747 * Because OPEN is always done by name in nfsv4, it is
3748 * possible that we opened a different file by the same
3749 * name. We can recognize this race condition, but we
3750 * can't do anything about it besides returning an error.
3752 * This will be fixed with VFS changes (lookup-intent).
3755 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3756 struct iattr
*sattr
)
3758 struct inode
*inode
= d_inode(dentry
);
3759 struct rpc_cred
*cred
= NULL
;
3760 struct nfs_open_context
*ctx
= NULL
;
3761 struct nfs4_label
*label
= NULL
;
3764 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3765 sattr
->ia_valid
& ATTR_SIZE
&&
3766 sattr
->ia_size
< i_size_read(inode
))
3767 pnfs_commit_and_return_layout(inode
);
3769 nfs_fattr_init(fattr
);
3771 /* Deal with open(O_TRUNC) */
3772 if (sattr
->ia_valid
& ATTR_OPEN
)
3773 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3775 /* Optimization: if the end result is no change, don't RPC */
3776 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3779 /* Search for an existing open(O_WRITE) file */
3780 if (sattr
->ia_valid
& ATTR_FILE
) {
3782 ctx
= nfs_file_open_context(sattr
->ia_file
);
3787 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3789 return PTR_ERR(label
);
3791 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, ctx
, NULL
, label
);
3793 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3794 nfs_setsecurity(inode
, fattr
, label
);
3796 nfs4_label_free(label
);
3800 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3801 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3802 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3804 struct nfs_server
*server
= NFS_SERVER(dir
);
3806 struct nfs4_lookup_arg args
= {
3807 .bitmask
= server
->attr_bitmask
,
3808 .dir_fh
= NFS_FH(dir
),
3811 struct nfs4_lookup_res res
= {
3817 struct rpc_message msg
= {
3818 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3823 args
.bitmask
= nfs4_bitmask(server
, label
);
3825 nfs_fattr_init(fattr
);
3827 dprintk("NFS call lookup %s\n", name
->name
);
3828 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3829 dprintk("NFS reply lookup: %d\n", status
);
3833 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3835 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3836 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3837 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3841 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3842 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3843 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3845 struct nfs4_exception exception
= { };
3846 struct rpc_clnt
*client
= *clnt
;
3849 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3850 trace_nfs4_lookup(dir
, name
, err
);
3852 case -NFS4ERR_BADNAME
:
3855 case -NFS4ERR_MOVED
:
3856 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3857 if (err
== -NFS4ERR_MOVED
)
3858 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3860 case -NFS4ERR_WRONGSEC
:
3862 if (client
!= *clnt
)
3864 client
= nfs4_negotiate_security(client
, dir
, name
);
3866 return PTR_ERR(client
);
3868 exception
.retry
= 1;
3871 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3873 } while (exception
.retry
);
3878 else if (client
!= *clnt
)
3879 rpc_shutdown_client(client
);
3884 static int nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
3885 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3886 struct nfs4_label
*label
)
3889 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3891 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3892 if (client
!= NFS_CLIENT(dir
)) {
3893 rpc_shutdown_client(client
);
3894 nfs_fixup_secinfo_attributes(fattr
);
3900 nfs4_proc_lookup_mountpoint(struct inode
*dir
, const struct qstr
*name
,
3901 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3903 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3906 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3908 return ERR_PTR(status
);
3909 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3912 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3914 struct nfs_server
*server
= NFS_SERVER(inode
);
3915 struct nfs4_accessargs args
= {
3916 .fh
= NFS_FH(inode
),
3917 .bitmask
= server
->cache_consistency_bitmask
,
3919 struct nfs4_accessres res
= {
3922 struct rpc_message msg
= {
3923 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3926 .rpc_cred
= entry
->cred
,
3928 int mode
= entry
->mask
;
3932 * Determine which access bits we want to ask for...
3934 if (mode
& MAY_READ
)
3935 args
.access
|= NFS4_ACCESS_READ
;
3936 if (S_ISDIR(inode
->i_mode
)) {
3937 if (mode
& MAY_WRITE
)
3938 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3939 if (mode
& MAY_EXEC
)
3940 args
.access
|= NFS4_ACCESS_LOOKUP
;
3942 if (mode
& MAY_WRITE
)
3943 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3944 if (mode
& MAY_EXEC
)
3945 args
.access
|= NFS4_ACCESS_EXECUTE
;
3948 res
.fattr
= nfs_alloc_fattr();
3949 if (res
.fattr
== NULL
)
3952 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3954 nfs_access_set_mask(entry
, res
.access
);
3955 nfs_refresh_inode(inode
, res
.fattr
);
3957 nfs_free_fattr(res
.fattr
);
3961 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3963 struct nfs4_exception exception
= { };
3966 err
= _nfs4_proc_access(inode
, entry
);
3967 trace_nfs4_access(inode
, err
);
3968 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3970 } while (exception
.retry
);
3975 * TODO: For the time being, we don't try to get any attributes
3976 * along with any of the zero-copy operations READ, READDIR,
3979 * In the case of the first three, we want to put the GETATTR
3980 * after the read-type operation -- this is because it is hard
3981 * to predict the length of a GETATTR response in v4, and thus
3982 * align the READ data correctly. This means that the GETATTR
3983 * may end up partially falling into the page cache, and we should
3984 * shift it into the 'tail' of the xdr_buf before processing.
3985 * To do this efficiently, we need to know the total length
3986 * of data received, which doesn't seem to be available outside
3989 * In the case of WRITE, we also want to put the GETATTR after
3990 * the operation -- in this case because we want to make sure
3991 * we get the post-operation mtime and size.
3993 * Both of these changes to the XDR layer would in fact be quite
3994 * minor, but I decided to leave them for a subsequent patch.
3996 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3997 unsigned int pgbase
, unsigned int pglen
)
3999 struct nfs4_readlink args
= {
4000 .fh
= NFS_FH(inode
),
4005 struct nfs4_readlink_res res
;
4006 struct rpc_message msg
= {
4007 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
4012 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4015 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
4016 unsigned int pgbase
, unsigned int pglen
)
4018 struct nfs4_exception exception
= { };
4021 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
4022 trace_nfs4_readlink(inode
, err
);
4023 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4025 } while (exception
.retry
);
4030 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4033 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
4036 struct nfs_server
*server
= NFS_SERVER(dir
);
4037 struct nfs4_label l
, *ilabel
= NULL
;
4038 struct nfs_open_context
*ctx
;
4039 struct nfs4_state
*state
;
4042 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
, NULL
);
4044 return PTR_ERR(ctx
);
4046 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4048 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4049 sattr
->ia_mode
&= ~current_umask();
4050 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
4051 if (IS_ERR(state
)) {
4052 status
= PTR_ERR(state
);
4056 nfs4_label_release_security(ilabel
);
4057 put_nfs_open_context(ctx
);
4061 static int _nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4063 struct nfs_server
*server
= NFS_SERVER(dir
);
4064 struct nfs_removeargs args
= {
4068 struct nfs_removeres res
= {
4071 struct rpc_message msg
= {
4072 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
4078 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
4080 update_changeattr(dir
, &res
.cinfo
);
4084 static int nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4086 struct nfs4_exception exception
= { };
4089 err
= _nfs4_proc_remove(dir
, name
);
4090 trace_nfs4_remove(dir
, name
, err
);
4091 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4093 } while (exception
.retry
);
4097 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
4099 struct nfs_server
*server
= NFS_SERVER(dir
);
4100 struct nfs_removeargs
*args
= msg
->rpc_argp
;
4101 struct nfs_removeres
*res
= msg
->rpc_resp
;
4103 res
->server
= server
;
4104 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
4105 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
4107 nfs_fattr_init(res
->dir_attr
);
4110 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
4112 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
),
4113 &data
->args
.seq_args
,
4118 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
4120 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
4121 struct nfs_removeres
*res
= &data
->res
;
4123 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4125 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
4126 &data
->timeout
) == -EAGAIN
)
4128 update_changeattr(dir
, &res
->cinfo
);
4132 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
4134 struct nfs_server
*server
= NFS_SERVER(dir
);
4135 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
4136 struct nfs_renameres
*res
= msg
->rpc_resp
;
4138 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
4139 res
->server
= server
;
4140 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
4143 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
4145 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
4146 &data
->args
.seq_args
,
4151 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
4152 struct inode
*new_dir
)
4154 struct nfs_renamedata
*data
= task
->tk_calldata
;
4155 struct nfs_renameres
*res
= &data
->res
;
4157 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4159 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
4162 update_changeattr(old_dir
, &res
->old_cinfo
);
4163 update_changeattr(new_dir
, &res
->new_cinfo
);
4167 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4169 struct nfs_server
*server
= NFS_SERVER(inode
);
4170 struct nfs4_link_arg arg
= {
4171 .fh
= NFS_FH(inode
),
4172 .dir_fh
= NFS_FH(dir
),
4174 .bitmask
= server
->attr_bitmask
,
4176 struct nfs4_link_res res
= {
4180 struct rpc_message msg
= {
4181 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
4185 int status
= -ENOMEM
;
4187 res
.fattr
= nfs_alloc_fattr();
4188 if (res
.fattr
== NULL
)
4191 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4192 if (IS_ERR(res
.label
)) {
4193 status
= PTR_ERR(res
.label
);
4196 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
4198 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4200 update_changeattr(dir
, &res
.cinfo
);
4201 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
4203 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
4207 nfs4_label_free(res
.label
);
4210 nfs_free_fattr(res
.fattr
);
4214 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4216 struct nfs4_exception exception
= { };
4219 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4220 _nfs4_proc_link(inode
, dir
, name
),
4222 } while (exception
.retry
);
4226 struct nfs4_createdata
{
4227 struct rpc_message msg
;
4228 struct nfs4_create_arg arg
;
4229 struct nfs4_create_res res
;
4231 struct nfs_fattr fattr
;
4232 struct nfs4_label
*label
;
4235 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
4236 const struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
4238 struct nfs4_createdata
*data
;
4240 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4242 struct nfs_server
*server
= NFS_SERVER(dir
);
4244 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4245 if (IS_ERR(data
->label
))
4248 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
4249 data
->msg
.rpc_argp
= &data
->arg
;
4250 data
->msg
.rpc_resp
= &data
->res
;
4251 data
->arg
.dir_fh
= NFS_FH(dir
);
4252 data
->arg
.server
= server
;
4253 data
->arg
.name
= name
;
4254 data
->arg
.attrs
= sattr
;
4255 data
->arg
.ftype
= ftype
;
4256 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
4257 data
->arg
.umask
= current_umask();
4258 data
->res
.server
= server
;
4259 data
->res
.fh
= &data
->fh
;
4260 data
->res
.fattr
= &data
->fattr
;
4261 data
->res
.label
= data
->label
;
4262 nfs_fattr_init(data
->res
.fattr
);
4270 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
4272 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
4273 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4275 update_changeattr(dir
, &data
->res
.dir_cinfo
);
4276 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
4281 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
4283 nfs4_label_free(data
->label
);
4287 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4288 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
4289 struct nfs4_label
*label
)
4291 struct nfs4_createdata
*data
;
4292 int status
= -ENAMETOOLONG
;
4294 if (len
> NFS4_MAXPATHLEN
)
4298 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
4302 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
4303 data
->arg
.u
.symlink
.pages
= &page
;
4304 data
->arg
.u
.symlink
.len
= len
;
4305 data
->arg
.label
= label
;
4307 status
= nfs4_do_create(dir
, dentry
, data
);
4309 nfs4_free_createdata(data
);
4314 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4315 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
4317 struct nfs4_exception exception
= { };
4318 struct nfs4_label l
, *label
= NULL
;
4321 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4324 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4325 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4326 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4328 } while (exception
.retry
);
4330 nfs4_label_release_security(label
);
4334 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4335 struct iattr
*sattr
, struct nfs4_label
*label
)
4337 struct nfs4_createdata
*data
;
4338 int status
= -ENOMEM
;
4340 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4344 data
->arg
.label
= label
;
4345 status
= nfs4_do_create(dir
, dentry
, data
);
4347 nfs4_free_createdata(data
);
4352 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4353 struct iattr
*sattr
)
4355 struct nfs_server
*server
= NFS_SERVER(dir
);
4356 struct nfs4_exception exception
= { };
4357 struct nfs4_label l
, *label
= NULL
;
4360 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4362 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4363 sattr
->ia_mode
&= ~current_umask();
4365 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4366 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4367 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4369 } while (exception
.retry
);
4370 nfs4_label_release_security(label
);
4375 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4376 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4378 struct inode
*dir
= d_inode(dentry
);
4379 struct nfs4_readdir_arg args
= {
4384 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4387 struct nfs4_readdir_res res
;
4388 struct rpc_message msg
= {
4389 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4396 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4398 (unsigned long long)cookie
);
4399 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4400 res
.pgbase
= args
.pgbase
;
4401 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4403 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4404 status
+= args
.pgbase
;
4407 nfs_invalidate_atime(dir
);
4409 dprintk("%s: returns %d\n", __func__
, status
);
4413 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4414 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4416 struct nfs4_exception exception
= { };
4419 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4420 pages
, count
, plus
);
4421 trace_nfs4_readdir(d_inode(dentry
), err
);
4422 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4424 } while (exception
.retry
);
4428 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4429 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4431 struct nfs4_createdata
*data
;
4432 int mode
= sattr
->ia_mode
;
4433 int status
= -ENOMEM
;
4435 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4440 data
->arg
.ftype
= NF4FIFO
;
4441 else if (S_ISBLK(mode
)) {
4442 data
->arg
.ftype
= NF4BLK
;
4443 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4444 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4446 else if (S_ISCHR(mode
)) {
4447 data
->arg
.ftype
= NF4CHR
;
4448 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4449 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4450 } else if (!S_ISSOCK(mode
)) {
4455 data
->arg
.label
= label
;
4456 status
= nfs4_do_create(dir
, dentry
, data
);
4458 nfs4_free_createdata(data
);
4463 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4464 struct iattr
*sattr
, dev_t rdev
)
4466 struct nfs_server
*server
= NFS_SERVER(dir
);
4467 struct nfs4_exception exception
= { };
4468 struct nfs4_label l
, *label
= NULL
;
4471 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4473 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4474 sattr
->ia_mode
&= ~current_umask();
4476 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4477 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4478 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4480 } while (exception
.retry
);
4482 nfs4_label_release_security(label
);
4487 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4488 struct nfs_fsstat
*fsstat
)
4490 struct nfs4_statfs_arg args
= {
4492 .bitmask
= server
->attr_bitmask
,
4494 struct nfs4_statfs_res res
= {
4497 struct rpc_message msg
= {
4498 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4503 nfs_fattr_init(fsstat
->fattr
);
4504 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4507 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4509 struct nfs4_exception exception
= { };
4512 err
= nfs4_handle_exception(server
,
4513 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4515 } while (exception
.retry
);
4519 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4520 struct nfs_fsinfo
*fsinfo
)
4522 struct nfs4_fsinfo_arg args
= {
4524 .bitmask
= server
->attr_bitmask
,
4526 struct nfs4_fsinfo_res res
= {
4529 struct rpc_message msg
= {
4530 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4535 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4538 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4540 struct nfs4_exception exception
= { };
4541 unsigned long now
= jiffies
;
4545 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4546 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4548 nfs4_set_lease_period(server
->nfs_client
,
4549 fsinfo
->lease_time
* HZ
,
4553 err
= nfs4_handle_exception(server
, err
, &exception
);
4554 } while (exception
.retry
);
4558 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4562 nfs_fattr_init(fsinfo
->fattr
);
4563 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4565 /* block layout checks this! */
4566 server
->pnfs_blksize
= fsinfo
->blksize
;
4567 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
);
4573 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4574 struct nfs_pathconf
*pathconf
)
4576 struct nfs4_pathconf_arg args
= {
4578 .bitmask
= server
->attr_bitmask
,
4580 struct nfs4_pathconf_res res
= {
4581 .pathconf
= pathconf
,
4583 struct rpc_message msg
= {
4584 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4589 /* None of the pathconf attributes are mandatory to implement */
4590 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4591 memset(pathconf
, 0, sizeof(*pathconf
));
4595 nfs_fattr_init(pathconf
->fattr
);
4596 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4599 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4600 struct nfs_pathconf
*pathconf
)
4602 struct nfs4_exception exception
= { };
4606 err
= nfs4_handle_exception(server
,
4607 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4609 } while (exception
.retry
);
4613 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4614 const struct nfs_open_context
*ctx
,
4615 const struct nfs_lock_context
*l_ctx
,
4618 return nfs4_select_rw_stateid(ctx
->state
, fmode
, l_ctx
, stateid
, NULL
);
4620 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4622 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4623 const struct nfs_open_context
*ctx
,
4624 const struct nfs_lock_context
*l_ctx
,
4627 nfs4_stateid current_stateid
;
4629 /* If the current stateid represents a lost lock, then exit */
4630 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4632 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4635 static bool nfs4_error_stateid_expired(int err
)
4638 case -NFS4ERR_DELEG_REVOKED
:
4639 case -NFS4ERR_ADMIN_REVOKED
:
4640 case -NFS4ERR_BAD_STATEID
:
4641 case -NFS4ERR_STALE_STATEID
:
4642 case -NFS4ERR_OLD_STATEID
:
4643 case -NFS4ERR_OPENMODE
:
4644 case -NFS4ERR_EXPIRED
:
4650 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4652 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4654 trace_nfs4_read(hdr
, task
->tk_status
);
4655 if (task
->tk_status
< 0) {
4656 struct nfs4_exception exception
= {
4657 .inode
= hdr
->inode
,
4658 .state
= hdr
->args
.context
->state
,
4659 .stateid
= &hdr
->args
.stateid
,
4661 task
->tk_status
= nfs4_async_handle_exception(task
,
4662 server
, task
->tk_status
, &exception
);
4663 if (exception
.retry
) {
4664 rpc_restart_call_prepare(task
);
4669 if (task
->tk_status
> 0)
4670 renew_lease(server
, hdr
->timestamp
);
4674 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4675 struct nfs_pgio_args
*args
)
4678 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4679 nfs4_stateid_is_current(&args
->stateid
,
4684 rpc_restart_call_prepare(task
);
4688 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4691 dprintk("--> %s\n", __func__
);
4693 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4695 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4697 if (task
->tk_status
> 0)
4698 nfs_invalidate_atime(hdr
->inode
);
4699 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4700 nfs4_read_done_cb(task
, hdr
);
4703 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4704 struct rpc_message
*msg
)
4706 hdr
->timestamp
= jiffies
;
4707 if (!hdr
->pgio_done_cb
)
4708 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4709 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4710 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4713 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4714 struct nfs_pgio_header
*hdr
)
4716 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
),
4717 &hdr
->args
.seq_args
,
4721 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4722 hdr
->args
.lock_context
,
4723 hdr
->rw_ops
->rw_mode
) == -EIO
)
4725 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4730 static int nfs4_write_done_cb(struct rpc_task
*task
,
4731 struct nfs_pgio_header
*hdr
)
4733 struct inode
*inode
= hdr
->inode
;
4735 trace_nfs4_write(hdr
, task
->tk_status
);
4736 if (task
->tk_status
< 0) {
4737 struct nfs4_exception exception
= {
4738 .inode
= hdr
->inode
,
4739 .state
= hdr
->args
.context
->state
,
4740 .stateid
= &hdr
->args
.stateid
,
4742 task
->tk_status
= nfs4_async_handle_exception(task
,
4743 NFS_SERVER(inode
), task
->tk_status
,
4745 if (exception
.retry
) {
4746 rpc_restart_call_prepare(task
);
4750 if (task
->tk_status
>= 0) {
4751 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4752 nfs_writeback_update_inode(hdr
);
4757 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4758 struct nfs_pgio_args
*args
)
4761 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4762 nfs4_stateid_is_current(&args
->stateid
,
4767 rpc_restart_call_prepare(task
);
4771 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4773 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4775 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4777 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4778 nfs4_write_done_cb(task
, hdr
);
4782 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4784 /* Don't request attributes for pNFS or O_DIRECT writes */
4785 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4787 /* Otherwise, request attributes if and only if we don't hold
4790 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4793 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4794 struct rpc_message
*msg
)
4796 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4798 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4799 hdr
->args
.bitmask
= NULL
;
4800 hdr
->res
.fattr
= NULL
;
4802 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4804 if (!hdr
->pgio_done_cb
)
4805 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4806 hdr
->res
.server
= server
;
4807 hdr
->timestamp
= jiffies
;
4809 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4810 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4813 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4815 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4816 &data
->args
.seq_args
,
4821 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4823 struct inode
*inode
= data
->inode
;
4825 trace_nfs4_commit(data
, task
->tk_status
);
4826 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4827 NULL
, NULL
) == -EAGAIN
) {
4828 rpc_restart_call_prepare(task
);
4834 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4836 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4838 return data
->commit_done_cb(task
, data
);
4841 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4843 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4845 if (data
->commit_done_cb
== NULL
)
4846 data
->commit_done_cb
= nfs4_commit_done_cb
;
4847 data
->res
.server
= server
;
4848 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4849 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4852 struct nfs4_renewdata
{
4853 struct nfs_client
*client
;
4854 unsigned long timestamp
;
4858 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4859 * standalone procedure for queueing an asynchronous RENEW.
4861 static void nfs4_renew_release(void *calldata
)
4863 struct nfs4_renewdata
*data
= calldata
;
4864 struct nfs_client
*clp
= data
->client
;
4866 if (atomic_read(&clp
->cl_count
) > 1)
4867 nfs4_schedule_state_renewal(clp
);
4868 nfs_put_client(clp
);
4872 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4874 struct nfs4_renewdata
*data
= calldata
;
4875 struct nfs_client
*clp
= data
->client
;
4876 unsigned long timestamp
= data
->timestamp
;
4878 trace_nfs4_renew_async(clp
, task
->tk_status
);
4879 switch (task
->tk_status
) {
4882 case -NFS4ERR_LEASE_MOVED
:
4883 nfs4_schedule_lease_moved_recovery(clp
);
4886 /* Unless we're shutting down, schedule state recovery! */
4887 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4889 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4890 nfs4_schedule_lease_recovery(clp
);
4893 nfs4_schedule_path_down_recovery(clp
);
4895 do_renew_lease(clp
, timestamp
);
4898 static const struct rpc_call_ops nfs4_renew_ops
= {
4899 .rpc_call_done
= nfs4_renew_done
,
4900 .rpc_release
= nfs4_renew_release
,
4903 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4905 struct rpc_message msg
= {
4906 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4910 struct nfs4_renewdata
*data
;
4912 if (renew_flags
== 0)
4914 if (!atomic_inc_not_zero(&clp
->cl_count
))
4916 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4920 data
->timestamp
= jiffies
;
4921 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4922 &nfs4_renew_ops
, data
);
4925 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4927 struct rpc_message msg
= {
4928 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4932 unsigned long now
= jiffies
;
4935 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4938 do_renew_lease(clp
, now
);
4942 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4944 return server
->caps
& NFS_CAP_ACLS
;
4947 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4948 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4951 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4953 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4954 struct page
**pages
)
4956 struct page
*newpage
, **spages
;
4962 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4963 newpage
= alloc_page(GFP_KERNEL
);
4965 if (newpage
== NULL
)
4967 memcpy(page_address(newpage
), buf
, len
);
4972 } while (buflen
!= 0);
4978 __free_page(spages
[rc
-1]);
4982 struct nfs4_cached_acl
{
4988 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4990 struct nfs_inode
*nfsi
= NFS_I(inode
);
4992 spin_lock(&inode
->i_lock
);
4993 kfree(nfsi
->nfs4_acl
);
4994 nfsi
->nfs4_acl
= acl
;
4995 spin_unlock(&inode
->i_lock
);
4998 static void nfs4_zap_acl_attr(struct inode
*inode
)
5000 nfs4_set_cached_acl(inode
, NULL
);
5003 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
5005 struct nfs_inode
*nfsi
= NFS_I(inode
);
5006 struct nfs4_cached_acl
*acl
;
5009 spin_lock(&inode
->i_lock
);
5010 acl
= nfsi
->nfs4_acl
;
5013 if (buf
== NULL
) /* user is just asking for length */
5015 if (acl
->cached
== 0)
5017 ret
= -ERANGE
; /* see getxattr(2) man page */
5018 if (acl
->len
> buflen
)
5020 memcpy(buf
, acl
->data
, acl
->len
);
5024 spin_unlock(&inode
->i_lock
);
5028 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
5030 struct nfs4_cached_acl
*acl
;
5031 size_t buflen
= sizeof(*acl
) + acl_len
;
5033 if (buflen
<= PAGE_SIZE
) {
5034 acl
= kmalloc(buflen
, GFP_KERNEL
);
5038 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
5040 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
5047 nfs4_set_cached_acl(inode
, acl
);
5051 * The getxattr API returns the required buffer length when called with a
5052 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5053 * the required buf. On a NULL buf, we send a page of data to the server
5054 * guessing that the ACL request can be serviced by a page. If so, we cache
5055 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5056 * the cache. If not so, we throw away the page, and cache the required
5057 * length. The next getxattr call will then produce another round trip to
5058 * the server, this time with the input buf of the required size.
5060 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5062 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
5063 struct nfs_getaclargs args
= {
5064 .fh
= NFS_FH(inode
),
5068 struct nfs_getaclres res
= {
5071 struct rpc_message msg
= {
5072 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
5076 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5077 int ret
= -ENOMEM
, i
;
5079 /* As long as we're doing a round trip to the server anyway,
5080 * let's be prepared for a page of acl data. */
5083 if (npages
> ARRAY_SIZE(pages
))
5086 for (i
= 0; i
< npages
; i
++) {
5087 pages
[i
] = alloc_page(GFP_KERNEL
);
5092 /* for decoding across pages */
5093 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
5094 if (!res
.acl_scratch
)
5097 args
.acl_len
= npages
* PAGE_SIZE
;
5099 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5100 __func__
, buf
, buflen
, npages
, args
.acl_len
);
5101 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
5102 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5106 /* Handle the case where the passed-in buffer is too short */
5107 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
5108 /* Did the user only issue a request for the acl length? */
5114 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
5116 if (res
.acl_len
> buflen
) {
5120 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
5125 for (i
= 0; i
< npages
; i
++)
5127 __free_page(pages
[i
]);
5128 if (res
.acl_scratch
)
5129 __free_page(res
.acl_scratch
);
5133 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5135 struct nfs4_exception exception
= { };
5138 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
5139 trace_nfs4_get_acl(inode
, ret
);
5142 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
5143 } while (exception
.retry
);
5147 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
5149 struct nfs_server
*server
= NFS_SERVER(inode
);
5152 if (!nfs4_server_supports_acls(server
))
5154 ret
= nfs_revalidate_inode(server
, inode
);
5157 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
5158 nfs_zap_acl_cache(inode
);
5159 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
5161 /* -ENOENT is returned if there is no ACL or if there is an ACL
5162 * but no cached acl data, just the acl length */
5164 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
5167 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5169 struct nfs_server
*server
= NFS_SERVER(inode
);
5170 struct page
*pages
[NFS4ACL_MAXPAGES
];
5171 struct nfs_setaclargs arg
= {
5172 .fh
= NFS_FH(inode
),
5176 struct nfs_setaclres res
;
5177 struct rpc_message msg
= {
5178 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
5182 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5185 if (!nfs4_server_supports_acls(server
))
5187 if (npages
> ARRAY_SIZE(pages
))
5189 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
5192 nfs4_inode_return_delegation(inode
);
5193 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5196 * Free each page after tx, so the only ref left is
5197 * held by the network stack
5200 put_page(pages
[i
-1]);
5203 * Acl update can result in inode attribute update.
5204 * so mark the attribute cache invalid.
5206 spin_lock(&inode
->i_lock
);
5207 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
5208 spin_unlock(&inode
->i_lock
);
5209 nfs_access_zap_cache(inode
);
5210 nfs_zap_acl_cache(inode
);
5214 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5216 struct nfs4_exception exception
= { };
5219 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
5220 trace_nfs4_set_acl(inode
, err
);
5221 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5223 } while (exception
.retry
);
5227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5228 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
5231 struct nfs_server
*server
= NFS_SERVER(inode
);
5232 struct nfs_fattr fattr
;
5233 struct nfs4_label label
= {0, 0, buflen
, buf
};
5235 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5236 struct nfs4_getattr_arg arg
= {
5237 .fh
= NFS_FH(inode
),
5240 struct nfs4_getattr_res res
= {
5245 struct rpc_message msg
= {
5246 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
5252 nfs_fattr_init(&fattr
);
5254 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
5257 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
5259 if (buflen
< label
.len
)
5264 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
5267 struct nfs4_exception exception
= { };
5270 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5274 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
5275 trace_nfs4_get_security_label(inode
, err
);
5276 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5278 } while (exception
.retry
);
5282 static int _nfs4_do_set_security_label(struct inode
*inode
,
5283 struct nfs4_label
*ilabel
,
5284 struct nfs_fattr
*fattr
,
5285 struct nfs4_label
*olabel
)
5288 struct iattr sattr
= {0};
5289 struct nfs_server
*server
= NFS_SERVER(inode
);
5290 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5291 struct nfs_setattrargs arg
= {
5292 .fh
= NFS_FH(inode
),
5298 struct nfs_setattrres res
= {
5303 struct rpc_message msg
= {
5304 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
5310 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
5312 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5314 dprintk("%s failed: %d\n", __func__
, status
);
5319 static int nfs4_do_set_security_label(struct inode
*inode
,
5320 struct nfs4_label
*ilabel
,
5321 struct nfs_fattr
*fattr
,
5322 struct nfs4_label
*olabel
)
5324 struct nfs4_exception exception
= { };
5328 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5330 trace_nfs4_set_security_label(inode
, err
);
5331 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5333 } while (exception
.retry
);
5338 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5340 struct nfs4_label ilabel
, *olabel
= NULL
;
5341 struct nfs_fattr fattr
;
5342 struct rpc_cred
*cred
;
5345 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5348 nfs_fattr_init(&fattr
);
5352 ilabel
.label
= (char *)buf
;
5353 ilabel
.len
= buflen
;
5355 cred
= rpc_lookup_cred();
5357 return PTR_ERR(cred
);
5359 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5360 if (IS_ERR(olabel
)) {
5361 status
= -PTR_ERR(olabel
);
5365 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5367 nfs_setsecurity(inode
, &fattr
, olabel
);
5369 nfs4_label_free(olabel
);
5374 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5377 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5378 nfs4_verifier
*bootverf
)
5382 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5383 /* An impossible timestamp guarantees this value
5384 * will never match a generated boot time. */
5385 verf
[0] = cpu_to_be32(U32_MAX
);
5386 verf
[1] = cpu_to_be32(U32_MAX
);
5388 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5389 u64 ns
= ktime_to_ns(nn
->boot_time
);
5391 verf
[0] = cpu_to_be32(ns
>> 32);
5392 verf
[1] = cpu_to_be32(ns
);
5394 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5398 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5403 if (clp
->cl_owner_id
!= NULL
)
5407 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5408 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5410 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
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
);
5427 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5429 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5430 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5433 clp
->cl_owner_id
= str
;
5438 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5443 len
= 10 + 10 + 1 + 10 + 1 +
5444 strlen(nfs4_client_id_uniquifier
) + 1 +
5445 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5447 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5451 * Since this string is allocated at mount time, and held until the
5452 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5453 * about a memory-reclaim deadlock.
5455 str
= kmalloc(len
, GFP_KERNEL
);
5459 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5460 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5461 nfs4_client_id_uniquifier
,
5462 clp
->cl_rpcclient
->cl_nodename
);
5463 clp
->cl_owner_id
= str
;
5468 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5473 if (clp
->cl_owner_id
!= NULL
)
5476 if (nfs4_client_id_uniquifier
[0] != '\0')
5477 return nfs4_init_uniquifier_client_string(clp
);
5479 len
= 10 + 10 + 1 + 10 + 1 +
5480 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5482 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5486 * Since this string is allocated at mount time, and held until the
5487 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5488 * about a memory-reclaim deadlock.
5490 str
= kmalloc(len
, GFP_KERNEL
);
5494 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5495 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5496 clp
->cl_rpcclient
->cl_nodename
);
5497 clp
->cl_owner_id
= str
;
5502 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5503 * services. Advertise one based on the address family of the
5507 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5509 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5510 return scnprintf(buf
, len
, "tcp6");
5512 return scnprintf(buf
, len
, "tcp");
5515 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5517 struct nfs4_setclientid
*sc
= calldata
;
5519 if (task
->tk_status
== 0)
5520 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5523 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5524 .rpc_call_done
= nfs4_setclientid_done
,
5528 * nfs4_proc_setclientid - Negotiate client ID
5529 * @clp: state data structure
5530 * @program: RPC program for NFSv4 callback service
5531 * @port: IP port number for NFS4 callback service
5532 * @cred: RPC credential to use for this call
5533 * @res: where to place the result
5535 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5537 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5538 unsigned short port
, struct rpc_cred
*cred
,
5539 struct nfs4_setclientid_res
*res
)
5541 nfs4_verifier sc_verifier
;
5542 struct nfs4_setclientid setclientid
= {
5543 .sc_verifier
= &sc_verifier
,
5547 struct rpc_message msg
= {
5548 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5549 .rpc_argp
= &setclientid
,
5553 struct rpc_task
*task
;
5554 struct rpc_task_setup task_setup_data
= {
5555 .rpc_client
= clp
->cl_rpcclient
,
5556 .rpc_message
= &msg
,
5557 .callback_ops
= &nfs4_setclientid_ops
,
5558 .callback_data
= &setclientid
,
5559 .flags
= RPC_TASK_TIMEOUT
,
5563 /* nfs_client_id4 */
5564 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5566 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5567 status
= nfs4_init_uniform_client_string(clp
);
5569 status
= nfs4_init_nonuniform_client_string(clp
);
5575 setclientid
.sc_netid_len
=
5576 nfs4_init_callback_netid(clp
,
5577 setclientid
.sc_netid
,
5578 sizeof(setclientid
.sc_netid
));
5579 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5580 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5581 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5583 dprintk("NFS call setclientid auth=%s, '%s'\n",
5584 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5586 task
= rpc_run_task(&task_setup_data
);
5588 status
= PTR_ERR(task
);
5591 status
= task
->tk_status
;
5592 if (setclientid
.sc_cred
) {
5593 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5594 put_rpccred(setclientid
.sc_cred
);
5598 trace_nfs4_setclientid(clp
, status
);
5599 dprintk("NFS reply setclientid: %d\n", status
);
5604 * nfs4_proc_setclientid_confirm - Confirm client ID
5605 * @clp: state data structure
5606 * @res: result of a previous SETCLIENTID
5607 * @cred: RPC credential to use for this call
5609 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5611 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5612 struct nfs4_setclientid_res
*arg
,
5613 struct rpc_cred
*cred
)
5615 struct rpc_message msg
= {
5616 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5622 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5623 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5625 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5626 trace_nfs4_setclientid_confirm(clp
, status
);
5627 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5631 struct nfs4_delegreturndata
{
5632 struct nfs4_delegreturnargs args
;
5633 struct nfs4_delegreturnres res
;
5635 nfs4_stateid stateid
;
5636 unsigned long timestamp
;
5638 struct nfs4_layoutreturn_args arg
;
5639 struct nfs4_layoutreturn_res res
;
5640 struct nfs4_xdr_opaque_data ld_private
;
5644 struct nfs_fattr fattr
;
5646 struct inode
*inode
;
5649 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5651 struct nfs4_delegreturndata
*data
= calldata
;
5653 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5656 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5658 /* Handle Layoutreturn errors */
5659 if (data
->args
.lr_args
&& task
->tk_status
!= 0) {
5660 switch(data
->res
.lr_ret
) {
5662 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5665 data
->args
.lr_args
= NULL
;
5666 data
->res
.lr_res
= NULL
;
5668 case -NFS4ERR_ADMIN_REVOKED
:
5669 case -NFS4ERR_DELEG_REVOKED
:
5670 case -NFS4ERR_EXPIRED
:
5671 case -NFS4ERR_BAD_STATEID
:
5672 case -NFS4ERR_OLD_STATEID
:
5673 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
5674 case -NFS4ERR_WRONG_CRED
:
5675 data
->args
.lr_args
= NULL
;
5676 data
->res
.lr_res
= NULL
;
5677 data
->res
.lr_ret
= 0;
5678 rpc_restart_call_prepare(task
);
5683 switch (task
->tk_status
) {
5685 renew_lease(data
->res
.server
, data
->timestamp
);
5687 case -NFS4ERR_ADMIN_REVOKED
:
5688 case -NFS4ERR_DELEG_REVOKED
:
5689 case -NFS4ERR_EXPIRED
:
5690 nfs4_free_revoked_stateid(data
->res
.server
,
5692 task
->tk_msg
.rpc_cred
);
5693 case -NFS4ERR_BAD_STATEID
:
5694 case -NFS4ERR_OLD_STATEID
:
5695 case -NFS4ERR_STALE_STATEID
:
5696 task
->tk_status
= 0;
5698 case -NFS4ERR_ACCESS
:
5699 if (data
->args
.bitmask
) {
5700 data
->args
.bitmask
= NULL
;
5701 data
->res
.fattr
= NULL
;
5702 task
->tk_status
= 0;
5703 rpc_restart_call_prepare(task
);
5707 if (nfs4_async_handle_error(task
, data
->res
.server
,
5708 NULL
, NULL
) == -EAGAIN
) {
5709 rpc_restart_call_prepare(task
);
5713 data
->rpc_status
= task
->tk_status
;
5716 static void nfs4_delegreturn_release(void *calldata
)
5718 struct nfs4_delegreturndata
*data
= calldata
;
5719 struct inode
*inode
= data
->inode
;
5723 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
,
5725 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5726 nfs_iput_and_deactive(inode
);
5731 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5733 struct nfs4_delegreturndata
*d_data
;
5735 d_data
= (struct nfs4_delegreturndata
*)data
;
5737 if (!d_data
->lr
.roc
&& nfs4_wait_on_layoutreturn(d_data
->inode
, task
))
5740 nfs4_setup_sequence(d_data
->res
.server
,
5741 &d_data
->args
.seq_args
,
5742 &d_data
->res
.seq_res
,
5746 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5747 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5748 .rpc_call_done
= nfs4_delegreturn_done
,
5749 .rpc_release
= nfs4_delegreturn_release
,
5752 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5754 struct nfs4_delegreturndata
*data
;
5755 struct nfs_server
*server
= NFS_SERVER(inode
);
5756 struct rpc_task
*task
;
5757 struct rpc_message msg
= {
5758 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5761 struct rpc_task_setup task_setup_data
= {
5762 .rpc_client
= server
->client
,
5763 .rpc_message
= &msg
,
5764 .callback_ops
= &nfs4_delegreturn_ops
,
5765 .flags
= RPC_TASK_ASYNC
,
5769 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5772 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5774 nfs4_state_protect(server
->nfs_client
,
5775 NFS_SP4_MACH_CRED_CLEANUP
,
5776 &task_setup_data
.rpc_client
, &msg
);
5778 data
->args
.fhandle
= &data
->fh
;
5779 data
->args
.stateid
= &data
->stateid
;
5780 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5781 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5782 nfs4_stateid_copy(&data
->stateid
, stateid
);
5783 data
->res
.fattr
= &data
->fattr
;
5784 data
->res
.server
= server
;
5785 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5786 data
->lr
.arg
.ld_private
= &data
->lr
.ld_private
;
5787 nfs_fattr_init(data
->res
.fattr
);
5788 data
->timestamp
= jiffies
;
5789 data
->rpc_status
= 0;
5790 data
->lr
.roc
= pnfs_roc(inode
, &data
->lr
.arg
, &data
->lr
.res
, cred
);
5791 data
->inode
= nfs_igrab_and_active(inode
);
5794 data
->args
.lr_args
= &data
->lr
.arg
;
5795 data
->res
.lr_res
= &data
->lr
.res
;
5797 } else if (data
->lr
.roc
) {
5798 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
, 0);
5799 data
->lr
.roc
= false;
5802 task_setup_data
.callback_data
= data
;
5803 msg
.rpc_argp
= &data
->args
;
5804 msg
.rpc_resp
= &data
->res
;
5805 task
= rpc_run_task(&task_setup_data
);
5807 return PTR_ERR(task
);
5810 status
= nfs4_wait_for_completion_rpc_task(task
);
5813 status
= data
->rpc_status
;
5819 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5821 struct nfs_server
*server
= NFS_SERVER(inode
);
5822 struct nfs4_exception exception
= { };
5825 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5826 trace_nfs4_delegreturn(inode
, stateid
, err
);
5828 case -NFS4ERR_STALE_STATEID
:
5829 case -NFS4ERR_EXPIRED
:
5833 err
= nfs4_handle_exception(server
, err
, &exception
);
5834 } while (exception
.retry
);
5838 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5840 struct inode
*inode
= state
->inode
;
5841 struct nfs_server
*server
= NFS_SERVER(inode
);
5842 struct nfs_client
*clp
= server
->nfs_client
;
5843 struct nfs_lockt_args arg
= {
5844 .fh
= NFS_FH(inode
),
5847 struct nfs_lockt_res res
= {
5850 struct rpc_message msg
= {
5851 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5854 .rpc_cred
= state
->owner
->so_cred
,
5856 struct nfs4_lock_state
*lsp
;
5859 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5860 status
= nfs4_set_lock_state(state
, request
);
5863 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5864 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5865 arg
.lock_owner
.s_dev
= server
->s_dev
;
5866 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5869 request
->fl_type
= F_UNLCK
;
5871 case -NFS4ERR_DENIED
:
5874 request
->fl_ops
->fl_release_private(request
);
5875 request
->fl_ops
= NULL
;
5880 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5882 struct nfs4_exception exception
= { };
5886 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5887 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5888 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5890 } while (exception
.retry
);
5894 struct nfs4_unlockdata
{
5895 struct nfs_locku_args arg
;
5896 struct nfs_locku_res res
;
5897 struct nfs4_lock_state
*lsp
;
5898 struct nfs_open_context
*ctx
;
5899 struct file_lock fl
;
5900 struct nfs_server
*server
;
5901 unsigned long timestamp
;
5904 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5905 struct nfs_open_context
*ctx
,
5906 struct nfs4_lock_state
*lsp
,
5907 struct nfs_seqid
*seqid
)
5909 struct nfs4_unlockdata
*p
;
5910 struct inode
*inode
= lsp
->ls_state
->inode
;
5912 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5915 p
->arg
.fh
= NFS_FH(inode
);
5917 p
->arg
.seqid
= seqid
;
5918 p
->res
.seqid
= seqid
;
5920 atomic_inc(&lsp
->ls_count
);
5921 /* Ensure we don't close file until we're done freeing locks! */
5922 p
->ctx
= get_nfs_open_context(ctx
);
5923 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5924 p
->server
= NFS_SERVER(inode
);
5928 static void nfs4_locku_release_calldata(void *data
)
5930 struct nfs4_unlockdata
*calldata
= data
;
5931 nfs_free_seqid(calldata
->arg
.seqid
);
5932 nfs4_put_lock_state(calldata
->lsp
);
5933 put_nfs_open_context(calldata
->ctx
);
5937 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5939 struct nfs4_unlockdata
*calldata
= data
;
5941 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5943 switch (task
->tk_status
) {
5945 renew_lease(calldata
->server
, calldata
->timestamp
);
5946 locks_lock_inode_wait(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5947 if (nfs4_update_lock_stateid(calldata
->lsp
,
5948 &calldata
->res
.stateid
))
5950 case -NFS4ERR_ADMIN_REVOKED
:
5951 case -NFS4ERR_EXPIRED
:
5952 nfs4_free_revoked_stateid(calldata
->server
,
5953 &calldata
->arg
.stateid
,
5954 task
->tk_msg
.rpc_cred
);
5955 case -NFS4ERR_BAD_STATEID
:
5956 case -NFS4ERR_OLD_STATEID
:
5957 case -NFS4ERR_STALE_STATEID
:
5958 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5959 &calldata
->lsp
->ls_stateid
))
5960 rpc_restart_call_prepare(task
);
5963 if (nfs4_async_handle_error(task
, calldata
->server
,
5964 NULL
, NULL
) == -EAGAIN
)
5965 rpc_restart_call_prepare(task
);
5967 nfs_release_seqid(calldata
->arg
.seqid
);
5970 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5972 struct nfs4_unlockdata
*calldata
= data
;
5974 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5976 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5977 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5978 /* Note: exit _without_ running nfs4_locku_done */
5981 calldata
->timestamp
= jiffies
;
5982 if (nfs4_setup_sequence(calldata
->server
,
5983 &calldata
->arg
.seq_args
,
5984 &calldata
->res
.seq_res
,
5986 nfs_release_seqid(calldata
->arg
.seqid
);
5989 task
->tk_action
= NULL
;
5991 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5994 static const struct rpc_call_ops nfs4_locku_ops
= {
5995 .rpc_call_prepare
= nfs4_locku_prepare
,
5996 .rpc_call_done
= nfs4_locku_done
,
5997 .rpc_release
= nfs4_locku_release_calldata
,
6000 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
6001 struct nfs_open_context
*ctx
,
6002 struct nfs4_lock_state
*lsp
,
6003 struct nfs_seqid
*seqid
)
6005 struct nfs4_unlockdata
*data
;
6006 struct rpc_message msg
= {
6007 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
6008 .rpc_cred
= ctx
->cred
,
6010 struct rpc_task_setup task_setup_data
= {
6011 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
6012 .rpc_message
= &msg
,
6013 .callback_ops
= &nfs4_locku_ops
,
6014 .workqueue
= nfsiod_workqueue
,
6015 .flags
= RPC_TASK_ASYNC
,
6018 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
6019 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
6021 /* Ensure this is an unlock - when canceling a lock, the
6022 * canceled lock is passed in, and it won't be an unlock.
6024 fl
->fl_type
= F_UNLCK
;
6026 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
6028 nfs_free_seqid(seqid
);
6029 return ERR_PTR(-ENOMEM
);
6032 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6033 msg
.rpc_argp
= &data
->arg
;
6034 msg
.rpc_resp
= &data
->res
;
6035 task_setup_data
.callback_data
= data
;
6036 return rpc_run_task(&task_setup_data
);
6039 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6041 struct inode
*inode
= state
->inode
;
6042 struct nfs4_state_owner
*sp
= state
->owner
;
6043 struct nfs_inode
*nfsi
= NFS_I(inode
);
6044 struct nfs_seqid
*seqid
;
6045 struct nfs4_lock_state
*lsp
;
6046 struct rpc_task
*task
;
6047 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6049 unsigned char fl_flags
= request
->fl_flags
;
6051 status
= nfs4_set_lock_state(state
, request
);
6052 /* Unlock _before_ we do the RPC call */
6053 request
->fl_flags
|= FL_EXISTS
;
6054 /* Exclude nfs_delegation_claim_locks() */
6055 mutex_lock(&sp
->so_delegreturn_mutex
);
6056 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6057 down_read(&nfsi
->rwsem
);
6058 if (locks_lock_inode_wait(inode
, request
) == -ENOENT
) {
6059 up_read(&nfsi
->rwsem
);
6060 mutex_unlock(&sp
->so_delegreturn_mutex
);
6063 up_read(&nfsi
->rwsem
);
6064 mutex_unlock(&sp
->so_delegreturn_mutex
);
6067 /* Is this a delegated lock? */
6068 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6069 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
6071 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
6072 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
6076 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
6077 status
= PTR_ERR(task
);
6080 status
= nfs4_wait_for_completion_rpc_task(task
);
6083 request
->fl_flags
= fl_flags
;
6084 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
6088 struct nfs4_lockdata
{
6089 struct nfs_lock_args arg
;
6090 struct nfs_lock_res res
;
6091 struct nfs4_lock_state
*lsp
;
6092 struct nfs_open_context
*ctx
;
6093 struct file_lock fl
;
6094 unsigned long timestamp
;
6097 struct nfs_server
*server
;
6100 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
6101 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
6104 struct nfs4_lockdata
*p
;
6105 struct inode
*inode
= lsp
->ls_state
->inode
;
6106 struct nfs_server
*server
= NFS_SERVER(inode
);
6107 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6109 p
= kzalloc(sizeof(*p
), gfp_mask
);
6113 p
->arg
.fh
= NFS_FH(inode
);
6115 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
6116 if (IS_ERR(p
->arg
.open_seqid
))
6118 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
6119 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
6120 if (IS_ERR(p
->arg
.lock_seqid
))
6121 goto out_free_seqid
;
6122 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6123 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6124 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
6125 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
6128 atomic_inc(&lsp
->ls_count
);
6129 p
->ctx
= get_nfs_open_context(ctx
);
6130 get_file(fl
->fl_file
);
6131 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
6134 nfs_free_seqid(p
->arg
.open_seqid
);
6140 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
6142 struct nfs4_lockdata
*data
= calldata
;
6143 struct nfs4_state
*state
= data
->lsp
->ls_state
;
6145 dprintk("%s: begin!\n", __func__
);
6146 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
6148 /* Do we need to do an open_to_lock_owner? */
6149 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
6150 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
6151 goto out_release_lock_seqid
;
6153 nfs4_stateid_copy(&data
->arg
.open_stateid
,
6154 &state
->open_stateid
);
6155 data
->arg
.new_lock_owner
= 1;
6156 data
->res
.open_seqid
= data
->arg
.open_seqid
;
6158 data
->arg
.new_lock_owner
= 0;
6159 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
6160 &data
->lsp
->ls_stateid
);
6162 if (!nfs4_valid_open_stateid(state
)) {
6163 data
->rpc_status
= -EBADF
;
6164 task
->tk_action
= NULL
;
6165 goto out_release_open_seqid
;
6167 data
->timestamp
= jiffies
;
6168 if (nfs4_setup_sequence(data
->server
,
6169 &data
->arg
.seq_args
,
6173 out_release_open_seqid
:
6174 nfs_release_seqid(data
->arg
.open_seqid
);
6175 out_release_lock_seqid
:
6176 nfs_release_seqid(data
->arg
.lock_seqid
);
6178 nfs4_sequence_done(task
, &data
->res
.seq_res
);
6179 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
6182 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
6184 struct nfs4_lockdata
*data
= calldata
;
6185 struct nfs4_lock_state
*lsp
= data
->lsp
;
6187 dprintk("%s: begin!\n", __func__
);
6189 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
6192 data
->rpc_status
= task
->tk_status
;
6193 switch (task
->tk_status
) {
6195 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
6197 if (data
->arg
.new_lock
) {
6198 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
6199 if (locks_lock_inode_wait(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
6200 rpc_restart_call_prepare(task
);
6204 if (data
->arg
.new_lock_owner
!= 0) {
6205 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
6206 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
6207 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6208 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
6209 rpc_restart_call_prepare(task
);
6211 case -NFS4ERR_BAD_STATEID
:
6212 case -NFS4ERR_OLD_STATEID
:
6213 case -NFS4ERR_STALE_STATEID
:
6214 case -NFS4ERR_EXPIRED
:
6215 if (data
->arg
.new_lock_owner
!= 0) {
6216 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
6217 &lsp
->ls_state
->open_stateid
))
6218 rpc_restart_call_prepare(task
);
6219 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
6221 rpc_restart_call_prepare(task
);
6223 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
6226 static void nfs4_lock_release(void *calldata
)
6228 struct nfs4_lockdata
*data
= calldata
;
6230 dprintk("%s: begin!\n", __func__
);
6231 nfs_free_seqid(data
->arg
.open_seqid
);
6232 if (data
->cancelled
!= 0) {
6233 struct rpc_task
*task
;
6234 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
6235 data
->arg
.lock_seqid
);
6237 rpc_put_task_async(task
);
6238 dprintk("%s: cancelling lock!\n", __func__
);
6240 nfs_free_seqid(data
->arg
.lock_seqid
);
6241 nfs4_put_lock_state(data
->lsp
);
6242 put_nfs_open_context(data
->ctx
);
6243 fput(data
->fl
.fl_file
);
6245 dprintk("%s: done!\n", __func__
);
6248 static const struct rpc_call_ops nfs4_lock_ops
= {
6249 .rpc_call_prepare
= nfs4_lock_prepare
,
6250 .rpc_call_done
= nfs4_lock_done
,
6251 .rpc_release
= nfs4_lock_release
,
6254 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
6257 case -NFS4ERR_ADMIN_REVOKED
:
6258 case -NFS4ERR_EXPIRED
:
6259 case -NFS4ERR_BAD_STATEID
:
6260 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6261 if (new_lock_owner
!= 0 ||
6262 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
6263 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
6265 case -NFS4ERR_STALE_STATEID
:
6266 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6267 nfs4_schedule_lease_recovery(server
->nfs_client
);
6271 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
6273 struct nfs4_lockdata
*data
;
6274 struct rpc_task
*task
;
6275 struct rpc_message msg
= {
6276 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
6277 .rpc_cred
= state
->owner
->so_cred
,
6279 struct rpc_task_setup task_setup_data
= {
6280 .rpc_client
= NFS_CLIENT(state
->inode
),
6281 .rpc_message
= &msg
,
6282 .callback_ops
= &nfs4_lock_ops
,
6283 .workqueue
= nfsiod_workqueue
,
6284 .flags
= RPC_TASK_ASYNC
,
6288 dprintk("%s: begin!\n", __func__
);
6289 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
6290 fl
->fl_u
.nfs4_fl
.owner
,
6291 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
6295 data
->arg
.block
= 1;
6296 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6297 msg
.rpc_argp
= &data
->arg
;
6298 msg
.rpc_resp
= &data
->res
;
6299 task_setup_data
.callback_data
= data
;
6300 if (recovery_type
> NFS_LOCK_NEW
) {
6301 if (recovery_type
== NFS_LOCK_RECLAIM
)
6302 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
6303 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
6305 data
->arg
.new_lock
= 1;
6306 task
= rpc_run_task(&task_setup_data
);
6308 return PTR_ERR(task
);
6309 ret
= nfs4_wait_for_completion_rpc_task(task
);
6311 ret
= data
->rpc_status
;
6313 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
6314 data
->arg
.new_lock_owner
, ret
);
6316 data
->cancelled
= 1;
6318 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
6319 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
6323 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
6325 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6326 struct nfs4_exception exception
= {
6327 .inode
= state
->inode
,
6332 /* Cache the lock if possible... */
6333 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6335 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
6336 if (err
!= -NFS4ERR_DELAY
)
6338 nfs4_handle_exception(server
, err
, &exception
);
6339 } while (exception
.retry
);
6343 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6345 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6346 struct nfs4_exception exception
= {
6347 .inode
= state
->inode
,
6351 err
= nfs4_set_lock_state(state
, request
);
6354 if (!recover_lost_locks
) {
6355 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6359 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6361 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6365 case -NFS4ERR_GRACE
:
6366 case -NFS4ERR_DELAY
:
6367 nfs4_handle_exception(server
, err
, &exception
);
6370 } while (exception
.retry
);
6375 #if defined(CONFIG_NFS_V4_1)
6376 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6378 struct nfs4_lock_state
*lsp
;
6381 status
= nfs4_set_lock_state(state
, request
);
6384 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6385 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) ||
6386 test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
6388 status
= nfs4_lock_expired(state
, request
);
6393 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6395 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6396 struct nfs4_state_owner
*sp
= state
->owner
;
6397 unsigned char fl_flags
= request
->fl_flags
;
6400 request
->fl_flags
|= FL_ACCESS
;
6401 status
= locks_lock_inode_wait(state
->inode
, request
);
6404 mutex_lock(&sp
->so_delegreturn_mutex
);
6405 down_read(&nfsi
->rwsem
);
6406 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6407 /* Yes: cache locks! */
6408 /* ...but avoid races with delegation recall... */
6409 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6410 status
= locks_lock_inode_wait(state
->inode
, request
);
6411 up_read(&nfsi
->rwsem
);
6412 mutex_unlock(&sp
->so_delegreturn_mutex
);
6415 up_read(&nfsi
->rwsem
);
6416 mutex_unlock(&sp
->so_delegreturn_mutex
);
6417 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6419 request
->fl_flags
= fl_flags
;
6423 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6425 struct nfs4_exception exception
= {
6427 .inode
= state
->inode
,
6432 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6433 if (err
== -NFS4ERR_DENIED
)
6435 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6437 } while (exception
.retry
);
6441 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6442 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6445 nfs4_retry_setlk_simple(struct nfs4_state
*state
, int cmd
,
6446 struct file_lock
*request
)
6448 int status
= -ERESTARTSYS
;
6449 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6451 while(!signalled()) {
6452 status
= nfs4_proc_setlk(state
, cmd
, request
);
6453 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6455 freezable_schedule_timeout_interruptible(timeout
);
6457 timeout
= min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT
, timeout
);
6458 status
= -ERESTARTSYS
;
6463 #ifdef CONFIG_NFS_V4_1
6464 struct nfs4_lock_waiter
{
6465 struct task_struct
*task
;
6466 struct inode
*inode
;
6467 struct nfs_lowner
*owner
;
6472 nfs4_wake_lock_waiter(wait_queue_t
*wait
, unsigned int mode
, int flags
, void *key
)
6475 struct cb_notify_lock_args
*cbnl
= key
;
6476 struct nfs4_lock_waiter
*waiter
= wait
->private;
6477 struct nfs_lowner
*lowner
= &cbnl
->cbnl_owner
,
6478 *wowner
= waiter
->owner
;
6480 /* Only wake if the callback was for the same owner */
6481 if (lowner
->clientid
!= wowner
->clientid
||
6482 lowner
->id
!= wowner
->id
||
6483 lowner
->s_dev
!= wowner
->s_dev
)
6486 /* Make sure it's for the right inode */
6487 if (nfs_compare_fh(NFS_FH(waiter
->inode
), &cbnl
->cbnl_fh
))
6490 waiter
->notified
= true;
6492 /* override "private" so we can use default_wake_function */
6493 wait
->private = waiter
->task
;
6494 ret
= autoremove_wake_function(wait
, mode
, flags
, key
);
6495 wait
->private = waiter
;
6500 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6502 int status
= -ERESTARTSYS
;
6503 unsigned long flags
;
6504 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
6505 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6506 struct nfs_client
*clp
= server
->nfs_client
;
6507 wait_queue_head_t
*q
= &clp
->cl_lock_waitq
;
6508 struct nfs_lowner owner
= { .clientid
= clp
->cl_clientid
,
6509 .id
= lsp
->ls_seqid
.owner_id
,
6510 .s_dev
= server
->s_dev
};
6511 struct nfs4_lock_waiter waiter
= { .task
= current
,
6512 .inode
= state
->inode
,
6514 .notified
= false };
6517 /* Don't bother with waitqueue if we don't expect a callback */
6518 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
))
6519 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6522 wait
.private = &waiter
;
6523 wait
.func
= nfs4_wake_lock_waiter
;
6524 add_wait_queue(q
, &wait
);
6526 while(!signalled()) {
6527 status
= nfs4_proc_setlk(state
, cmd
, request
);
6528 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6531 status
= -ERESTARTSYS
;
6532 spin_lock_irqsave(&q
->lock
, flags
);
6533 if (waiter
.notified
) {
6534 spin_unlock_irqrestore(&q
->lock
, flags
);
6537 set_current_state(TASK_INTERRUPTIBLE
);
6538 spin_unlock_irqrestore(&q
->lock
, flags
);
6540 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT
);
6543 finish_wait(q
, &wait
);
6546 #else /* !CONFIG_NFS_V4_1 */
6548 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6550 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6555 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6557 struct nfs_open_context
*ctx
;
6558 struct nfs4_state
*state
;
6561 /* verify open state */
6562 ctx
= nfs_file_open_context(filp
);
6565 if (request
->fl_start
< 0 || request
->fl_end
< 0)
6568 if (IS_GETLK(cmd
)) {
6570 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6574 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6577 if (request
->fl_type
== F_UNLCK
) {
6579 return nfs4_proc_unlck(state
, cmd
, request
);
6586 if ((request
->fl_flags
& FL_POSIX
) &&
6587 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6591 * Don't rely on the VFS having checked the file open mode,
6592 * since it won't do this for flock() locks.
6594 switch (request
->fl_type
) {
6596 if (!(filp
->f_mode
& FMODE_READ
))
6600 if (!(filp
->f_mode
& FMODE_WRITE
))
6604 status
= nfs4_set_lock_state(state
, request
);
6608 return nfs4_retry_setlk(state
, cmd
, request
);
6611 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6613 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6616 err
= nfs4_set_lock_state(state
, fl
);
6619 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6620 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6623 struct nfs_release_lockowner_data
{
6624 struct nfs4_lock_state
*lsp
;
6625 struct nfs_server
*server
;
6626 struct nfs_release_lockowner_args args
;
6627 struct nfs_release_lockowner_res res
;
6628 unsigned long timestamp
;
6631 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6633 struct nfs_release_lockowner_data
*data
= calldata
;
6634 struct nfs_server
*server
= data
->server
;
6635 nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
6636 &data
->args
.seq_args
, &data
->res
.seq_res
, task
);
6637 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6638 data
->timestamp
= jiffies
;
6641 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6643 struct nfs_release_lockowner_data
*data
= calldata
;
6644 struct nfs_server
*server
= data
->server
;
6646 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6648 switch (task
->tk_status
) {
6650 renew_lease(server
, data
->timestamp
);
6652 case -NFS4ERR_STALE_CLIENTID
:
6653 case -NFS4ERR_EXPIRED
:
6654 nfs4_schedule_lease_recovery(server
->nfs_client
);
6656 case -NFS4ERR_LEASE_MOVED
:
6657 case -NFS4ERR_DELAY
:
6658 if (nfs4_async_handle_error(task
, server
,
6659 NULL
, NULL
) == -EAGAIN
)
6660 rpc_restart_call_prepare(task
);
6664 static void nfs4_release_lockowner_release(void *calldata
)
6666 struct nfs_release_lockowner_data
*data
= calldata
;
6667 nfs4_free_lock_state(data
->server
, data
->lsp
);
6671 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6672 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6673 .rpc_call_done
= nfs4_release_lockowner_done
,
6674 .rpc_release
= nfs4_release_lockowner_release
,
6678 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6680 struct nfs_release_lockowner_data
*data
;
6681 struct rpc_message msg
= {
6682 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6685 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6688 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6692 data
->server
= server
;
6693 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6694 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6695 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6697 msg
.rpc_argp
= &data
->args
;
6698 msg
.rpc_resp
= &data
->res
;
6699 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6700 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6703 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6705 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6706 struct dentry
*unused
, struct inode
*inode
,
6707 const char *key
, const void *buf
,
6708 size_t buflen
, int flags
)
6710 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6713 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6714 struct dentry
*unused
, struct inode
*inode
,
6715 const char *key
, void *buf
, size_t buflen
)
6717 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6720 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6722 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6725 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6727 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6728 struct dentry
*unused
, struct inode
*inode
,
6729 const char *key
, const void *buf
,
6730 size_t buflen
, int flags
)
6732 if (security_ismaclabel(key
))
6733 return nfs4_set_security_label(inode
, buf
, buflen
);
6738 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6739 struct dentry
*unused
, struct inode
*inode
,
6740 const char *key
, void *buf
, size_t buflen
)
6742 if (security_ismaclabel(key
))
6743 return nfs4_get_security_label(inode
, buf
, buflen
);
6748 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6752 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6753 len
= security_inode_listsecurity(inode
, list
, list_len
);
6754 if (list_len
&& len
> list_len
)
6760 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6761 .prefix
= XATTR_SECURITY_PREFIX
,
6762 .get
= nfs4_xattr_get_nfs4_label
,
6763 .set
= nfs4_xattr_set_nfs4_label
,
6769 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6777 * nfs_fhget will use either the mounted_on_fileid or the fileid
6779 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6781 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6782 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6783 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6784 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6787 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6788 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6789 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6793 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6794 const struct qstr
*name
,
6795 struct nfs4_fs_locations
*fs_locations
,
6798 struct nfs_server
*server
= NFS_SERVER(dir
);
6800 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6802 struct nfs4_fs_locations_arg args
= {
6803 .dir_fh
= NFS_FH(dir
),
6808 struct nfs4_fs_locations_res res
= {
6809 .fs_locations
= fs_locations
,
6811 struct rpc_message msg
= {
6812 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6818 dprintk("%s: start\n", __func__
);
6820 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6821 * is not supported */
6822 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6823 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6825 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6827 nfs_fattr_init(&fs_locations
->fattr
);
6828 fs_locations
->server
= server
;
6829 fs_locations
->nlocations
= 0;
6830 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6831 dprintk("%s: returned status = %d\n", __func__
, status
);
6835 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6836 const struct qstr
*name
,
6837 struct nfs4_fs_locations
*fs_locations
,
6840 struct nfs4_exception exception
= { };
6843 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6844 fs_locations
, page
);
6845 trace_nfs4_get_fs_locations(dir
, name
, err
);
6846 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6848 } while (exception
.retry
);
6853 * This operation also signals the server that this client is
6854 * performing migration recovery. The server can stop returning
6855 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6856 * appended to this compound to identify the client ID which is
6857 * performing recovery.
6859 static int _nfs40_proc_get_locations(struct inode
*inode
,
6860 struct nfs4_fs_locations
*locations
,
6861 struct page
*page
, struct rpc_cred
*cred
)
6863 struct nfs_server
*server
= NFS_SERVER(inode
);
6864 struct rpc_clnt
*clnt
= server
->client
;
6866 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6868 struct nfs4_fs_locations_arg args
= {
6869 .clientid
= server
->nfs_client
->cl_clientid
,
6870 .fh
= NFS_FH(inode
),
6873 .migration
= 1, /* skip LOOKUP */
6874 .renew
= 1, /* append RENEW */
6876 struct nfs4_fs_locations_res res
= {
6877 .fs_locations
= locations
,
6881 struct rpc_message msg
= {
6882 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6887 unsigned long now
= jiffies
;
6890 nfs_fattr_init(&locations
->fattr
);
6891 locations
->server
= server
;
6892 locations
->nlocations
= 0;
6894 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6895 nfs4_set_sequence_privileged(&args
.seq_args
);
6896 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6897 &args
.seq_args
, &res
.seq_res
);
6901 renew_lease(server
, now
);
6905 #ifdef CONFIG_NFS_V4_1
6908 * This operation also signals the server that this client is
6909 * performing migration recovery. The server can stop asserting
6910 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6911 * performing this operation is identified in the SEQUENCE
6912 * operation in this compound.
6914 * When the client supports GETATTR(fs_locations_info), it can
6915 * be plumbed in here.
6917 static int _nfs41_proc_get_locations(struct inode
*inode
,
6918 struct nfs4_fs_locations
*locations
,
6919 struct page
*page
, struct rpc_cred
*cred
)
6921 struct nfs_server
*server
= NFS_SERVER(inode
);
6922 struct rpc_clnt
*clnt
= server
->client
;
6924 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6926 struct nfs4_fs_locations_arg args
= {
6927 .fh
= NFS_FH(inode
),
6930 .migration
= 1, /* skip LOOKUP */
6932 struct nfs4_fs_locations_res res
= {
6933 .fs_locations
= locations
,
6936 struct rpc_message msg
= {
6937 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6944 nfs_fattr_init(&locations
->fattr
);
6945 locations
->server
= server
;
6946 locations
->nlocations
= 0;
6948 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6949 nfs4_set_sequence_privileged(&args
.seq_args
);
6950 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6951 &args
.seq_args
, &res
.seq_res
);
6952 if (status
== NFS4_OK
&&
6953 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6954 status
= -NFS4ERR_LEASE_MOVED
;
6958 #endif /* CONFIG_NFS_V4_1 */
6961 * nfs4_proc_get_locations - discover locations for a migrated FSID
6962 * @inode: inode on FSID that is migrating
6963 * @locations: result of query
6965 * @cred: credential to use for this operation
6967 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6968 * operation failed, or a negative errno if a local error occurred.
6970 * On success, "locations" is filled in, but if the server has
6971 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6974 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6975 * from this client that require migration recovery.
6977 int nfs4_proc_get_locations(struct inode
*inode
,
6978 struct nfs4_fs_locations
*locations
,
6979 struct page
*page
, struct rpc_cred
*cred
)
6981 struct nfs_server
*server
= NFS_SERVER(inode
);
6982 struct nfs_client
*clp
= server
->nfs_client
;
6983 const struct nfs4_mig_recovery_ops
*ops
=
6984 clp
->cl_mvops
->mig_recovery_ops
;
6985 struct nfs4_exception exception
= { };
6988 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6989 (unsigned long long)server
->fsid
.major
,
6990 (unsigned long long)server
->fsid
.minor
,
6992 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6995 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6996 if (status
!= -NFS4ERR_DELAY
)
6998 nfs4_handle_exception(server
, status
, &exception
);
6999 } while (exception
.retry
);
7004 * This operation also signals the server that this client is
7005 * performing "lease moved" recovery. The server can stop
7006 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7007 * is appended to this compound to identify the client ID which is
7008 * performing recovery.
7010 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7012 struct nfs_server
*server
= NFS_SERVER(inode
);
7013 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
7014 struct rpc_clnt
*clnt
= server
->client
;
7015 struct nfs4_fsid_present_arg args
= {
7016 .fh
= NFS_FH(inode
),
7017 .clientid
= clp
->cl_clientid
,
7018 .renew
= 1, /* append RENEW */
7020 struct nfs4_fsid_present_res res
= {
7023 struct rpc_message msg
= {
7024 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7029 unsigned long now
= jiffies
;
7032 res
.fh
= nfs_alloc_fhandle();
7036 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7037 nfs4_set_sequence_privileged(&args
.seq_args
);
7038 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7039 &args
.seq_args
, &res
.seq_res
);
7040 nfs_free_fhandle(res
.fh
);
7044 do_renew_lease(clp
, now
);
7048 #ifdef CONFIG_NFS_V4_1
7051 * This operation also signals the server that this client is
7052 * performing "lease moved" recovery. The server can stop asserting
7053 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7054 * this operation is identified in the SEQUENCE operation in this
7057 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7059 struct nfs_server
*server
= NFS_SERVER(inode
);
7060 struct rpc_clnt
*clnt
= server
->client
;
7061 struct nfs4_fsid_present_arg args
= {
7062 .fh
= NFS_FH(inode
),
7064 struct nfs4_fsid_present_res res
= {
7066 struct rpc_message msg
= {
7067 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7074 res
.fh
= nfs_alloc_fhandle();
7078 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7079 nfs4_set_sequence_privileged(&args
.seq_args
);
7080 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7081 &args
.seq_args
, &res
.seq_res
);
7082 nfs_free_fhandle(res
.fh
);
7083 if (status
== NFS4_OK
&&
7084 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
7085 status
= -NFS4ERR_LEASE_MOVED
;
7089 #endif /* CONFIG_NFS_V4_1 */
7092 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7093 * @inode: inode on FSID to check
7094 * @cred: credential to use for this operation
7096 * Server indicates whether the FSID is present, moved, or not
7097 * recognized. This operation is necessary to clear a LEASE_MOVED
7098 * condition for this client ID.
7100 * Returns NFS4_OK if the FSID is present on this server,
7101 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7102 * NFS4ERR code if some error occurred on the server, or a
7103 * negative errno if a local failure occurred.
7105 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7107 struct nfs_server
*server
= NFS_SERVER(inode
);
7108 struct nfs_client
*clp
= server
->nfs_client
;
7109 const struct nfs4_mig_recovery_ops
*ops
=
7110 clp
->cl_mvops
->mig_recovery_ops
;
7111 struct nfs4_exception exception
= { };
7114 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
7115 (unsigned long long)server
->fsid
.major
,
7116 (unsigned long long)server
->fsid
.minor
,
7118 nfs_display_fhandle(NFS_FH(inode
), __func__
);
7121 status
= ops
->fsid_present(inode
, cred
);
7122 if (status
!= -NFS4ERR_DELAY
)
7124 nfs4_handle_exception(server
, status
, &exception
);
7125 } while (exception
.retry
);
7130 * If 'use_integrity' is true and the state managment nfs_client
7131 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7132 * and the machine credential as per RFC3530bis and RFC5661 Security
7133 * Considerations sections. Otherwise, just use the user cred with the
7134 * filesystem's rpc_client.
7136 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7139 struct nfs4_secinfo_arg args
= {
7140 .dir_fh
= NFS_FH(dir
),
7143 struct nfs4_secinfo_res res
= {
7146 struct rpc_message msg
= {
7147 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
7151 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
7152 struct rpc_cred
*cred
= NULL
;
7154 if (use_integrity
) {
7155 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
7156 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
7157 msg
.rpc_cred
= cred
;
7160 dprintk("NFS call secinfo %s\n", name
->name
);
7162 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
7163 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
7165 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
7167 dprintk("NFS reply secinfo: %d\n", status
);
7175 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
7176 struct nfs4_secinfo_flavors
*flavors
)
7178 struct nfs4_exception exception
= { };
7181 err
= -NFS4ERR_WRONGSEC
;
7183 /* try to use integrity protection with machine cred */
7184 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
7185 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
7188 * if unable to use integrity protection, or SECINFO with
7189 * integrity protection returns NFS4ERR_WRONGSEC (which is
7190 * disallowed by spec, but exists in deployed servers) use
7191 * the current filesystem's rpc_client and the user cred.
7193 if (err
== -NFS4ERR_WRONGSEC
)
7194 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
7196 trace_nfs4_secinfo(dir
, name
, err
);
7197 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
7199 } while (exception
.retry
);
7203 #ifdef CONFIG_NFS_V4_1
7205 * Check the exchange flags returned by the server for invalid flags, having
7206 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7209 static int nfs4_check_cl_exchange_flags(u32 flags
)
7211 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
7213 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
7214 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
7216 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
7220 return -NFS4ERR_INVAL
;
7224 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
7225 struct nfs41_server_scope
*b
)
7227 if (a
->server_scope_sz
== b
->server_scope_sz
&&
7228 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
7235 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
7239 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
7240 .rpc_call_done
= &nfs4_bind_one_conn_to_session_done
,
7244 * nfs4_proc_bind_one_conn_to_session()
7246 * The 4.1 client currently uses the same TCP connection for the
7247 * fore and backchannel.
7250 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
7251 struct rpc_xprt
*xprt
,
7252 struct nfs_client
*clp
,
7253 struct rpc_cred
*cred
)
7256 struct nfs41_bind_conn_to_session_args args
= {
7258 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
7260 struct nfs41_bind_conn_to_session_res res
;
7261 struct rpc_message msg
= {
7263 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
7268 struct rpc_task_setup task_setup_data
= {
7271 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
7272 .rpc_message
= &msg
,
7273 .flags
= RPC_TASK_TIMEOUT
,
7275 struct rpc_task
*task
;
7277 dprintk("--> %s\n", __func__
);
7279 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
7280 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
7281 args
.dir
= NFS4_CDFC4_FORE
;
7283 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7284 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
7285 args
.dir
= NFS4_CDFC4_FORE
;
7287 task
= rpc_run_task(&task_setup_data
);
7288 if (!IS_ERR(task
)) {
7289 status
= task
->tk_status
;
7292 status
= PTR_ERR(task
);
7293 trace_nfs4_bind_conn_to_session(clp
, status
);
7295 if (memcmp(res
.sessionid
.data
,
7296 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
7297 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
7301 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
7302 dprintk("NFS: %s: Unexpected direction from server\n",
7307 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
7308 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7315 dprintk("<-- %s status= %d\n", __func__
, status
);
7319 struct rpc_bind_conn_calldata
{
7320 struct nfs_client
*clp
;
7321 struct rpc_cred
*cred
;
7325 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
7326 struct rpc_xprt
*xprt
,
7329 struct rpc_bind_conn_calldata
*p
= calldata
;
7331 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
7334 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7336 struct rpc_bind_conn_calldata data
= {
7340 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
7341 nfs4_proc_bind_conn_to_session_callback
, &data
);
7345 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7346 * and operations we'd like to see to enable certain features in the allow map
7348 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
7349 .how
= SP4_MACH_CRED
,
7350 .enforce
.u
.words
= {
7351 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7352 1 << (OP_EXCHANGE_ID
- 32) |
7353 1 << (OP_CREATE_SESSION
- 32) |
7354 1 << (OP_DESTROY_SESSION
- 32) |
7355 1 << (OP_DESTROY_CLIENTID
- 32)
7358 [0] = 1 << (OP_CLOSE
) |
7359 1 << (OP_OPEN_DOWNGRADE
) |
7361 1 << (OP_DELEGRETURN
) |
7363 [1] = 1 << (OP_SECINFO
- 32) |
7364 1 << (OP_SECINFO_NO_NAME
- 32) |
7365 1 << (OP_LAYOUTRETURN
- 32) |
7366 1 << (OP_TEST_STATEID
- 32) |
7367 1 << (OP_FREE_STATEID
- 32) |
7368 1 << (OP_WRITE
- 32)
7373 * Select the state protection mode for client `clp' given the server results
7374 * from exchange_id in `sp'.
7376 * Returns 0 on success, negative errno otherwise.
7378 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
7379 struct nfs41_state_protection
*sp
)
7381 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
7382 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7383 1 << (OP_EXCHANGE_ID
- 32) |
7384 1 << (OP_CREATE_SESSION
- 32) |
7385 1 << (OP_DESTROY_SESSION
- 32) |
7386 1 << (OP_DESTROY_CLIENTID
- 32)
7390 if (sp
->how
== SP4_MACH_CRED
) {
7391 /* Print state protect result */
7392 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
7393 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
7394 if (test_bit(i
, sp
->enforce
.u
.longs
))
7395 dfprintk(MOUNT
, " enforce op %d\n", i
);
7396 if (test_bit(i
, sp
->allow
.u
.longs
))
7397 dfprintk(MOUNT
, " allow op %d\n", i
);
7400 /* make sure nothing is on enforce list that isn't supported */
7401 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
7402 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
7403 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7409 * Minimal mode - state operations are allowed to use machine
7410 * credential. Note this already happens by default, so the
7411 * client doesn't have to do anything more than the negotiation.
7413 * NOTE: we don't care if EXCHANGE_ID is in the list -
7414 * we're already using the machine cred for exchange_id
7415 * and will never use a different cred.
7417 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
7418 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
7419 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
7420 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
7421 dfprintk(MOUNT
, "sp4_mach_cred:\n");
7422 dfprintk(MOUNT
, " minimal mode enabled\n");
7423 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
7425 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7429 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7430 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7431 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7432 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7433 dfprintk(MOUNT
, " cleanup mode enabled\n");
7434 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
7437 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7438 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7439 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
7440 &clp
->cl_sp4_flags
);
7443 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7444 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7445 dfprintk(MOUNT
, " secinfo mode enabled\n");
7446 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
7449 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7450 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7451 dfprintk(MOUNT
, " stateid mode enabled\n");
7452 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
7455 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7456 dfprintk(MOUNT
, " write mode enabled\n");
7457 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
7460 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7461 dfprintk(MOUNT
, " commit mode enabled\n");
7462 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
7469 struct nfs41_exchange_id_data
{
7470 struct nfs41_exchange_id_res res
;
7471 struct nfs41_exchange_id_args args
;
7472 struct rpc_xprt
*xprt
;
7476 static void nfs4_exchange_id_done(struct rpc_task
*task
, void *data
)
7478 struct nfs41_exchange_id_data
*cdata
=
7479 (struct nfs41_exchange_id_data
*)data
;
7480 struct nfs_client
*clp
= cdata
->args
.client
;
7481 int status
= task
->tk_status
;
7483 trace_nfs4_exchange_id(clp
, status
);
7486 status
= nfs4_check_cl_exchange_flags(cdata
->res
.flags
);
7488 if (cdata
->xprt
&& status
== 0) {
7489 status
= nfs4_detect_session_trunking(clp
, &cdata
->res
,
7495 status
= nfs4_sp4_select_mode(clp
, &cdata
->res
.state_protect
);
7498 clp
->cl_clientid
= cdata
->res
.clientid
;
7499 clp
->cl_exchange_flags
= cdata
->res
.flags
;
7500 /* Client ID is not confirmed */
7501 if (!(cdata
->res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
)) {
7502 clear_bit(NFS4_SESSION_ESTABLISHED
,
7503 &clp
->cl_session
->session_state
);
7504 clp
->cl_seqid
= cdata
->res
.seqid
;
7507 kfree(clp
->cl_serverowner
);
7508 clp
->cl_serverowner
= cdata
->res
.server_owner
;
7509 cdata
->res
.server_owner
= NULL
;
7511 /* use the most recent implementation id */
7512 kfree(clp
->cl_implid
);
7513 clp
->cl_implid
= cdata
->res
.impl_id
;
7514 cdata
->res
.impl_id
= NULL
;
7516 if (clp
->cl_serverscope
!= NULL
&&
7517 !nfs41_same_server_scope(clp
->cl_serverscope
,
7518 cdata
->res
.server_scope
)) {
7519 dprintk("%s: server_scope mismatch detected\n",
7521 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7522 kfree(clp
->cl_serverscope
);
7523 clp
->cl_serverscope
= NULL
;
7526 if (clp
->cl_serverscope
== NULL
) {
7527 clp
->cl_serverscope
= cdata
->res
.server_scope
;
7528 cdata
->res
.server_scope
= NULL
;
7530 /* Save the EXCHANGE_ID verifier session trunk tests */
7531 memcpy(clp
->cl_confirm
.data
, cdata
->args
.verifier
->data
,
7532 sizeof(clp
->cl_confirm
.data
));
7535 cdata
->rpc_status
= status
;
7539 static void nfs4_exchange_id_release(void *data
)
7541 struct nfs41_exchange_id_data
*cdata
=
7542 (struct nfs41_exchange_id_data
*)data
;
7544 nfs_put_client(cdata
->args
.client
);
7546 xprt_put(cdata
->xprt
);
7547 rpc_clnt_xprt_switch_put(cdata
->args
.client
->cl_rpcclient
);
7549 kfree(cdata
->res
.impl_id
);
7550 kfree(cdata
->res
.server_scope
);
7551 kfree(cdata
->res
.server_owner
);
7555 static const struct rpc_call_ops nfs4_exchange_id_call_ops
= {
7556 .rpc_call_done
= nfs4_exchange_id_done
,
7557 .rpc_release
= nfs4_exchange_id_release
,
7561 * _nfs4_proc_exchange_id()
7563 * Wrapper for EXCHANGE_ID operation.
7565 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7566 u32 sp4_how
, struct rpc_xprt
*xprt
)
7568 nfs4_verifier verifier
;
7569 struct rpc_message msg
= {
7570 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7573 struct rpc_task_setup task_setup_data
= {
7574 .rpc_client
= clp
->cl_rpcclient
,
7575 .callback_ops
= &nfs4_exchange_id_call_ops
,
7576 .rpc_message
= &msg
,
7577 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7579 struct nfs41_exchange_id_data
*calldata
;
7580 struct rpc_task
*task
;
7583 if (!atomic_inc_not_zero(&clp
->cl_count
))
7587 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7592 nfs4_init_boot_verifier(clp
, &verifier
);
7594 status
= nfs4_init_uniform_client_string(clp
);
7598 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7599 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
7602 calldata
->res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7605 if (unlikely(calldata
->res
.server_owner
== NULL
))
7608 calldata
->res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7610 if (unlikely(calldata
->res
.server_scope
== NULL
))
7611 goto out_server_owner
;
7613 calldata
->res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7614 if (unlikely(calldata
->res
.impl_id
== NULL
))
7615 goto out_server_scope
;
7619 calldata
->args
.state_protect
.how
= SP4_NONE
;
7623 calldata
->args
.state_protect
= nfs4_sp4_mach_cred_request
;
7633 calldata
->xprt
= xprt
;
7634 task_setup_data
.rpc_xprt
= xprt
;
7635 task_setup_data
.flags
=
7636 RPC_TASK_SOFT
|RPC_TASK_SOFTCONN
|RPC_TASK_ASYNC
;
7637 calldata
->args
.verifier
= &clp
->cl_confirm
;
7639 calldata
->args
.verifier
= &verifier
;
7641 calldata
->args
.client
= clp
;
7642 #ifdef CONFIG_NFS_V4_1_MIGRATION
7643 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7644 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
7645 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
7647 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7648 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
7650 msg
.rpc_argp
= &calldata
->args
;
7651 msg
.rpc_resp
= &calldata
->res
;
7652 task_setup_data
.callback_data
= calldata
;
7654 task
= rpc_run_task(&task_setup_data
);
7656 status
= PTR_ERR(task
);
7661 status
= rpc_wait_for_completion_task(task
);
7663 status
= calldata
->rpc_status
;
7664 } else /* session trunking test */
7665 status
= calldata
->rpc_status
;
7669 if (clp
->cl_implid
!= NULL
)
7670 dprintk("NFS reply exchange_id: Server Implementation ID: "
7671 "domain: %s, name: %s, date: %llu,%u\n",
7672 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
7673 clp
->cl_implid
->date
.seconds
,
7674 clp
->cl_implid
->date
.nseconds
);
7675 dprintk("NFS reply exchange_id: %d\n", status
);
7679 kfree(calldata
->res
.impl_id
);
7681 kfree(calldata
->res
.server_scope
);
7683 kfree(calldata
->res
.server_owner
);
7690 * nfs4_proc_exchange_id()
7692 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7694 * Since the clientid has expired, all compounds using sessions
7695 * associated with the stale clientid will be returning
7696 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7697 * be in some phase of session reset.
7699 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7701 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7703 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7706 /* try SP4_MACH_CRED if krb5i/p */
7707 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7708 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7709 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
, NULL
);
7715 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
, NULL
);
7719 * nfs4_test_session_trunk
7721 * This is an add_xprt_test() test function called from
7722 * rpc_clnt_setup_test_and_add_xprt.
7724 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7725 * and is dereferrenced in nfs4_exchange_id_release
7727 * Upon success, add the new transport to the rpc_clnt
7729 * @clnt: struct rpc_clnt to get new transport
7730 * @xprt: the rpc_xprt to test
7731 * @data: call data for _nfs4_proc_exchange_id.
7733 int nfs4_test_session_trunk(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
,
7736 struct nfs4_add_xprt_data
*adata
= (struct nfs4_add_xprt_data
*)data
;
7739 dprintk("--> %s try %s\n", __func__
,
7740 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
7742 sp4_how
= (adata
->clp
->cl_sp4_flags
== 0 ? SP4_NONE
: SP4_MACH_CRED
);
7744 /* Test connection for session trunking. Async exchange_id call */
7745 return _nfs4_proc_exchange_id(adata
->clp
, adata
->cred
, sp4_how
, xprt
);
7747 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk
);
7749 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7750 struct rpc_cred
*cred
)
7752 struct rpc_message msg
= {
7753 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7759 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7760 trace_nfs4_destroy_clientid(clp
, status
);
7762 dprintk("NFS: Got error %d from the server %s on "
7763 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7767 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7768 struct rpc_cred
*cred
)
7773 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7774 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7776 case -NFS4ERR_DELAY
:
7777 case -NFS4ERR_CLIENTID_BUSY
:
7787 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7789 struct rpc_cred
*cred
;
7792 if (clp
->cl_mvops
->minor_version
< 1)
7794 if (clp
->cl_exchange_flags
== 0)
7796 if (clp
->cl_preserve_clid
)
7798 cred
= nfs4_get_clid_cred(clp
);
7799 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7804 case -NFS4ERR_STALE_CLIENTID
:
7805 clp
->cl_exchange_flags
= 0;
7811 struct nfs4_get_lease_time_data
{
7812 struct nfs4_get_lease_time_args
*args
;
7813 struct nfs4_get_lease_time_res
*res
;
7814 struct nfs_client
*clp
;
7817 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7820 struct nfs4_get_lease_time_data
*data
=
7821 (struct nfs4_get_lease_time_data
*)calldata
;
7823 dprintk("--> %s\n", __func__
);
7824 /* just setup sequence, do not trigger session recovery
7825 since we're invoked within one */
7826 nfs41_setup_sequence(data
->clp
->cl_session
,
7827 &data
->args
->la_seq_args
,
7828 &data
->res
->lr_seq_res
,
7830 dprintk("<-- %s\n", __func__
);
7834 * Called from nfs4_state_manager thread for session setup, so don't recover
7835 * from sequence operation or clientid errors.
7837 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7839 struct nfs4_get_lease_time_data
*data
=
7840 (struct nfs4_get_lease_time_data
*)calldata
;
7842 dprintk("--> %s\n", __func__
);
7843 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7845 switch (task
->tk_status
) {
7846 case -NFS4ERR_DELAY
:
7847 case -NFS4ERR_GRACE
:
7848 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7849 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7850 task
->tk_status
= 0;
7852 case -NFS4ERR_RETRY_UNCACHED_REP
:
7853 rpc_restart_call_prepare(task
);
7856 dprintk("<-- %s\n", __func__
);
7859 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7860 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7861 .rpc_call_done
= nfs4_get_lease_time_done
,
7864 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7866 struct rpc_task
*task
;
7867 struct nfs4_get_lease_time_args args
;
7868 struct nfs4_get_lease_time_res res
= {
7869 .lr_fsinfo
= fsinfo
,
7871 struct nfs4_get_lease_time_data data
= {
7876 struct rpc_message msg
= {
7877 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7881 struct rpc_task_setup task_setup
= {
7882 .rpc_client
= clp
->cl_rpcclient
,
7883 .rpc_message
= &msg
,
7884 .callback_ops
= &nfs4_get_lease_time_ops
,
7885 .callback_data
= &data
,
7886 .flags
= RPC_TASK_TIMEOUT
,
7890 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7891 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7892 dprintk("--> %s\n", __func__
);
7893 task
= rpc_run_task(&task_setup
);
7896 status
= PTR_ERR(task
);
7898 status
= task
->tk_status
;
7901 dprintk("<-- %s return %d\n", __func__
, status
);
7907 * Initialize the values to be used by the client in CREATE_SESSION
7908 * If nfs4_init_session set the fore channel request and response sizes,
7911 * Set the back channel max_resp_sz_cached to zero to force the client to
7912 * always set csa_cachethis to FALSE because the current implementation
7913 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7915 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7916 struct rpc_clnt
*clnt
)
7918 unsigned int max_rqst_sz
, max_resp_sz
;
7919 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7921 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7922 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7924 /* Fore channel attributes */
7925 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7926 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7927 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7928 args
->fc_attrs
.max_reqs
= max_session_slots
;
7930 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7931 "max_ops=%u max_reqs=%u\n",
7933 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7934 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7936 /* Back channel attributes */
7937 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7938 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7939 args
->bc_attrs
.max_resp_sz_cached
= 0;
7940 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7941 args
->bc_attrs
.max_reqs
= min_t(unsigned short, max_session_cb_slots
, 1);
7943 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7944 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7946 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7947 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7948 args
->bc_attrs
.max_reqs
);
7951 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7952 struct nfs41_create_session_res
*res
)
7954 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7955 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7957 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7960 * Our requested max_ops is the minimum we need; we're not
7961 * prepared to break up compounds into smaller pieces than that.
7962 * So, no point even trying to continue if the server won't
7965 if (rcvd
->max_ops
< sent
->max_ops
)
7967 if (rcvd
->max_reqs
== 0)
7969 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7970 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7974 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7975 struct nfs41_create_session_res
*res
)
7977 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7978 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7980 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7982 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7984 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7986 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7988 if (rcvd
->max_ops
> sent
->max_ops
)
7990 if (rcvd
->max_reqs
> sent
->max_reqs
)
7996 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7997 struct nfs41_create_session_res
*res
)
8001 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
8004 return nfs4_verify_back_channel_attrs(args
, res
);
8007 static void nfs4_update_session(struct nfs4_session
*session
,
8008 struct nfs41_create_session_res
*res
)
8010 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
8011 /* Mark client id and session as being confirmed */
8012 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
8013 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
8014 session
->flags
= res
->flags
;
8015 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
8016 if (res
->flags
& SESSION4_BACK_CHAN
)
8017 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
8018 sizeof(session
->bc_attrs
));
8021 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
8022 struct rpc_cred
*cred
)
8024 struct nfs4_session
*session
= clp
->cl_session
;
8025 struct nfs41_create_session_args args
= {
8027 .clientid
= clp
->cl_clientid
,
8028 .seqid
= clp
->cl_seqid
,
8029 .cb_program
= NFS4_CALLBACK
,
8031 struct nfs41_create_session_res res
;
8033 struct rpc_message msg
= {
8034 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
8041 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
8042 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
8044 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8045 trace_nfs4_create_session(clp
, status
);
8048 case -NFS4ERR_STALE_CLIENTID
:
8049 case -NFS4ERR_DELAY
:
8058 /* Verify the session's negotiated channel_attrs values */
8059 status
= nfs4_verify_channel_attrs(&args
, &res
);
8060 /* Increment the clientid slot sequence id */
8063 nfs4_update_session(session
, &res
);
8070 * Issues a CREATE_SESSION operation to the server.
8071 * It is the responsibility of the caller to verify the session is
8072 * expired before calling this routine.
8074 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8078 struct nfs4_session
*session
= clp
->cl_session
;
8080 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
8082 status
= _nfs4_proc_create_session(clp
, cred
);
8086 /* Init or reset the session slot tables */
8087 status
= nfs4_setup_session_slot_tables(session
);
8088 dprintk("slot table setup returned %d\n", status
);
8092 ptr
= (unsigned *)&session
->sess_id
.data
[0];
8093 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
8094 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
8096 dprintk("<-- %s\n", __func__
);
8101 * Issue the over-the-wire RPC DESTROY_SESSION.
8102 * The caller must serialize access to this routine.
8104 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
8105 struct rpc_cred
*cred
)
8107 struct rpc_message msg
= {
8108 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
8109 .rpc_argp
= session
,
8114 dprintk("--> nfs4_proc_destroy_session\n");
8116 /* session is still being setup */
8117 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
8120 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8121 trace_nfs4_destroy_session(session
->clp
, status
);
8124 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8125 "Session has been destroyed regardless...\n", status
);
8127 dprintk("<-- nfs4_proc_destroy_session\n");
8132 * Renew the cl_session lease.
8134 struct nfs4_sequence_data
{
8135 struct nfs_client
*clp
;
8136 struct nfs4_sequence_args args
;
8137 struct nfs4_sequence_res res
;
8140 static void nfs41_sequence_release(void *data
)
8142 struct nfs4_sequence_data
*calldata
= data
;
8143 struct nfs_client
*clp
= calldata
->clp
;
8145 if (atomic_read(&clp
->cl_count
) > 1)
8146 nfs4_schedule_state_renewal(clp
);
8147 nfs_put_client(clp
);
8151 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8153 switch(task
->tk_status
) {
8154 case -NFS4ERR_DELAY
:
8155 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8158 nfs4_schedule_lease_recovery(clp
);
8163 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
8165 struct nfs4_sequence_data
*calldata
= data
;
8166 struct nfs_client
*clp
= calldata
->clp
;
8168 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
8171 trace_nfs4_sequence(clp
, task
->tk_status
);
8172 if (task
->tk_status
< 0) {
8173 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
8174 if (atomic_read(&clp
->cl_count
) == 1)
8177 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
8178 rpc_restart_call_prepare(task
);
8182 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
8184 dprintk("<-- %s\n", __func__
);
8187 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
8189 struct nfs4_sequence_data
*calldata
= data
;
8190 struct nfs_client
*clp
= calldata
->clp
;
8191 struct nfs4_sequence_args
*args
;
8192 struct nfs4_sequence_res
*res
;
8194 args
= task
->tk_msg
.rpc_argp
;
8195 res
= task
->tk_msg
.rpc_resp
;
8197 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
8200 static const struct rpc_call_ops nfs41_sequence_ops
= {
8201 .rpc_call_done
= nfs41_sequence_call_done
,
8202 .rpc_call_prepare
= nfs41_sequence_prepare
,
8203 .rpc_release
= nfs41_sequence_release
,
8206 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
8207 struct rpc_cred
*cred
,
8210 struct nfs4_sequence_data
*calldata
;
8211 struct rpc_message msg
= {
8212 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
8215 struct rpc_task_setup task_setup_data
= {
8216 .rpc_client
= clp
->cl_rpcclient
,
8217 .rpc_message
= &msg
,
8218 .callback_ops
= &nfs41_sequence_ops
,
8219 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
8222 if (!atomic_inc_not_zero(&clp
->cl_count
))
8223 return ERR_PTR(-EIO
);
8224 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8225 if (calldata
== NULL
) {
8226 nfs_put_client(clp
);
8227 return ERR_PTR(-ENOMEM
);
8229 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
8231 nfs4_set_sequence_privileged(&calldata
->args
);
8232 msg
.rpc_argp
= &calldata
->args
;
8233 msg
.rpc_resp
= &calldata
->res
;
8234 calldata
->clp
= clp
;
8235 task_setup_data
.callback_data
= calldata
;
8237 return rpc_run_task(&task_setup_data
);
8240 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
8242 struct rpc_task
*task
;
8245 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
8247 task
= _nfs41_proc_sequence(clp
, cred
, false);
8249 ret
= PTR_ERR(task
);
8251 rpc_put_task_async(task
);
8252 dprintk("<-- %s status=%d\n", __func__
, ret
);
8256 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8258 struct rpc_task
*task
;
8261 task
= _nfs41_proc_sequence(clp
, cred
, true);
8263 ret
= PTR_ERR(task
);
8266 ret
= rpc_wait_for_completion_task(task
);
8268 ret
= task
->tk_status
;
8271 dprintk("<-- %s status=%d\n", __func__
, ret
);
8275 struct nfs4_reclaim_complete_data
{
8276 struct nfs_client
*clp
;
8277 struct nfs41_reclaim_complete_args arg
;
8278 struct nfs41_reclaim_complete_res res
;
8281 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
8283 struct nfs4_reclaim_complete_data
*calldata
= data
;
8285 nfs41_setup_sequence(calldata
->clp
->cl_session
,
8286 &calldata
->arg
.seq_args
,
8287 &calldata
->res
.seq_res
,
8291 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8293 switch(task
->tk_status
) {
8295 case -NFS4ERR_COMPLETE_ALREADY
:
8296 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
8298 case -NFS4ERR_DELAY
:
8299 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8301 case -NFS4ERR_RETRY_UNCACHED_REP
:
8304 nfs4_schedule_lease_recovery(clp
);
8309 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
8311 struct nfs4_reclaim_complete_data
*calldata
= data
;
8312 struct nfs_client
*clp
= calldata
->clp
;
8313 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
8315 dprintk("--> %s\n", __func__
);
8316 if (!nfs41_sequence_done(task
, res
))
8319 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
8320 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
8321 rpc_restart_call_prepare(task
);
8324 dprintk("<-- %s\n", __func__
);
8327 static void nfs4_free_reclaim_complete_data(void *data
)
8329 struct nfs4_reclaim_complete_data
*calldata
= data
;
8334 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
8335 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
8336 .rpc_call_done
= nfs4_reclaim_complete_done
,
8337 .rpc_release
= nfs4_free_reclaim_complete_data
,
8341 * Issue a global reclaim complete.
8343 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
8344 struct rpc_cred
*cred
)
8346 struct nfs4_reclaim_complete_data
*calldata
;
8347 struct rpc_task
*task
;
8348 struct rpc_message msg
= {
8349 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
8352 struct rpc_task_setup task_setup_data
= {
8353 .rpc_client
= clp
->cl_rpcclient
,
8354 .rpc_message
= &msg
,
8355 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
8356 .flags
= RPC_TASK_ASYNC
,
8358 int status
= -ENOMEM
;
8360 dprintk("--> %s\n", __func__
);
8361 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8362 if (calldata
== NULL
)
8364 calldata
->clp
= clp
;
8365 calldata
->arg
.one_fs
= 0;
8367 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
8368 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
8369 msg
.rpc_argp
= &calldata
->arg
;
8370 msg
.rpc_resp
= &calldata
->res
;
8371 task_setup_data
.callback_data
= calldata
;
8372 task
= rpc_run_task(&task_setup_data
);
8374 status
= PTR_ERR(task
);
8377 status
= nfs4_wait_for_completion_rpc_task(task
);
8379 status
= task
->tk_status
;
8383 dprintk("<-- %s status=%d\n", __func__
, status
);
8388 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
8390 struct nfs4_layoutget
*lgp
= calldata
;
8391 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
8392 struct nfs4_session
*session
= nfs4_get_session(server
);
8394 dprintk("--> %s\n", __func__
);
8395 nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
8396 &lgp
->res
.seq_res
, task
);
8397 dprintk("<-- %s\n", __func__
);
8400 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
8402 struct nfs4_layoutget
*lgp
= calldata
;
8404 dprintk("--> %s\n", __func__
);
8405 nfs41_sequence_process(task
, &lgp
->res
.seq_res
);
8406 dprintk("<-- %s\n", __func__
);
8410 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
8411 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
8413 struct inode
*inode
= lgp
->args
.inode
;
8414 struct nfs_server
*server
= NFS_SERVER(inode
);
8415 struct pnfs_layout_hdr
*lo
;
8416 int nfs4err
= task
->tk_status
;
8417 int err
, status
= 0;
8420 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
8427 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8428 * on the file. set tk_status to -ENODATA to tell upper layer to
8431 case -NFS4ERR_LAYOUTUNAVAILABLE
:
8435 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8436 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8438 case -NFS4ERR_BADLAYOUT
:
8439 status
= -EOVERFLOW
;
8442 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8443 * (or clients) writing to the same RAID stripe except when
8444 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8446 * Treat it like we would RECALLCONFLICT -- we retry for a little
8447 * while, and then eventually give up.
8449 case -NFS4ERR_LAYOUTTRYLATER
:
8450 if (lgp
->args
.minlength
== 0) {
8451 status
= -EOVERFLOW
;
8456 case -NFS4ERR_RECALLCONFLICT
:
8457 status
= -ERECALLCONFLICT
;
8459 case -NFS4ERR_DELEG_REVOKED
:
8460 case -NFS4ERR_ADMIN_REVOKED
:
8461 case -NFS4ERR_EXPIRED
:
8462 case -NFS4ERR_BAD_STATEID
:
8463 exception
->timeout
= 0;
8464 spin_lock(&inode
->i_lock
);
8465 lo
= NFS_I(inode
)->layout
;
8466 /* If the open stateid was bad, then recover it. */
8467 if (!lo
|| test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) ||
8468 nfs4_stateid_match_other(&lgp
->args
.stateid
,
8469 &lgp
->args
.ctx
->state
->stateid
)) {
8470 spin_unlock(&inode
->i_lock
);
8471 exception
->state
= lgp
->args
.ctx
->state
;
8472 exception
->stateid
= &lgp
->args
.stateid
;
8477 * Mark the bad layout state as invalid, then retry
8479 pnfs_mark_layout_stateid_invalid(lo
, &head
);
8480 spin_unlock(&inode
->i_lock
);
8481 pnfs_free_lseg_list(&head
);
8486 err
= nfs4_handle_exception(server
, nfs4err
, exception
);
8488 if (exception
->retry
)
8494 dprintk("<-- %s\n", __func__
);
8498 static size_t max_response_pages(struct nfs_server
*server
)
8500 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
8501 return nfs_page_array_len(0, max_resp_sz
);
8504 static void nfs4_free_pages(struct page
**pages
, size_t size
)
8511 for (i
= 0; i
< size
; i
++) {
8514 __free_page(pages
[i
]);
8519 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
8521 struct page
**pages
;
8524 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
8526 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
8530 for (i
= 0; i
< size
; i
++) {
8531 pages
[i
] = alloc_page(gfp_flags
);
8533 dprintk("%s: failed to allocate page\n", __func__
);
8534 nfs4_free_pages(pages
, size
);
8542 static void nfs4_layoutget_release(void *calldata
)
8544 struct nfs4_layoutget
*lgp
= calldata
;
8545 struct inode
*inode
= lgp
->args
.inode
;
8546 struct nfs_server
*server
= NFS_SERVER(inode
);
8547 size_t max_pages
= max_response_pages(server
);
8549 dprintk("--> %s\n", __func__
);
8550 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8551 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8552 put_nfs_open_context(lgp
->args
.ctx
);
8554 dprintk("<-- %s\n", __func__
);
8557 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8558 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8559 .rpc_call_done
= nfs4_layoutget_done
,
8560 .rpc_release
= nfs4_layoutget_release
,
8563 struct pnfs_layout_segment
*
8564 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8566 struct inode
*inode
= lgp
->args
.inode
;
8567 struct nfs_server
*server
= NFS_SERVER(inode
);
8568 size_t max_pages
= max_response_pages(server
);
8569 struct rpc_task
*task
;
8570 struct rpc_message msg
= {
8571 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8572 .rpc_argp
= &lgp
->args
,
8573 .rpc_resp
= &lgp
->res
,
8574 .rpc_cred
= lgp
->cred
,
8576 struct rpc_task_setup task_setup_data
= {
8577 .rpc_client
= server
->client
,
8578 .rpc_message
= &msg
,
8579 .callback_ops
= &nfs4_layoutget_call_ops
,
8580 .callback_data
= lgp
,
8581 .flags
= RPC_TASK_ASYNC
,
8583 struct pnfs_layout_segment
*lseg
= NULL
;
8584 struct nfs4_exception exception
= {
8586 .timeout
= *timeout
,
8590 dprintk("--> %s\n", __func__
);
8592 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8593 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8595 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8596 if (!lgp
->args
.layout
.pages
) {
8597 nfs4_layoutget_release(lgp
);
8598 return ERR_PTR(-ENOMEM
);
8600 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8602 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8603 lgp
->res
.seq_res
.sr_slot
= NULL
;
8604 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8606 task
= rpc_run_task(&task_setup_data
);
8608 return ERR_CAST(task
);
8609 status
= nfs4_wait_for_completion_rpc_task(task
);
8611 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8612 *timeout
= exception
.timeout
;
8615 trace_nfs4_layoutget(lgp
->args
.ctx
,
8621 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8622 if (status
== 0 && lgp
->res
.layoutp
->len
)
8623 lseg
= pnfs_layout_process(lgp
);
8624 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8626 dprintk("<-- %s status=%d\n", __func__
, status
);
8628 return ERR_PTR(status
);
8633 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8635 struct nfs4_layoutreturn
*lrp
= calldata
;
8637 dprintk("--> %s\n", __func__
);
8638 nfs41_setup_sequence(lrp
->clp
->cl_session
,
8639 &lrp
->args
.seq_args
,
8644 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8646 struct nfs4_layoutreturn
*lrp
= calldata
;
8647 struct nfs_server
*server
;
8649 dprintk("--> %s\n", __func__
);
8651 if (!nfs41_sequence_process(task
, &lrp
->res
.seq_res
))
8654 server
= NFS_SERVER(lrp
->args
.inode
);
8655 switch (task
->tk_status
) {
8657 task
->tk_status
= 0;
8660 case -NFS4ERR_DELAY
:
8661 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8663 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8664 rpc_restart_call_prepare(task
);
8667 dprintk("<-- %s\n", __func__
);
8670 static void nfs4_layoutreturn_release(void *calldata
)
8672 struct nfs4_layoutreturn
*lrp
= calldata
;
8673 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8675 dprintk("--> %s\n", __func__
);
8676 pnfs_layoutreturn_free_lsegs(lo
, &lrp
->args
.stateid
, &lrp
->args
.range
,
8677 lrp
->res
.lrs_present
? &lrp
->res
.stateid
: NULL
);
8678 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8679 if (lrp
->ld_private
.ops
&& lrp
->ld_private
.ops
->free
)
8680 lrp
->ld_private
.ops
->free(&lrp
->ld_private
);
8681 pnfs_put_layout_hdr(lrp
->args
.layout
);
8682 nfs_iput_and_deactive(lrp
->inode
);
8684 dprintk("<-- %s\n", __func__
);
8687 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8688 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8689 .rpc_call_done
= nfs4_layoutreturn_done
,
8690 .rpc_release
= nfs4_layoutreturn_release
,
8693 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8695 struct rpc_task
*task
;
8696 struct rpc_message msg
= {
8697 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8698 .rpc_argp
= &lrp
->args
,
8699 .rpc_resp
= &lrp
->res
,
8700 .rpc_cred
= lrp
->cred
,
8702 struct rpc_task_setup task_setup_data
= {
8703 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8704 .rpc_message
= &msg
,
8705 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8706 .callback_data
= lrp
,
8710 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8711 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8712 &task_setup_data
.rpc_client
, &msg
);
8714 dprintk("--> %s\n", __func__
);
8716 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8718 nfs4_layoutreturn_release(lrp
);
8721 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8723 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8724 task
= rpc_run_task(&task_setup_data
);
8726 return PTR_ERR(task
);
8728 status
= task
->tk_status
;
8729 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8730 dprintk("<-- %s status=%d\n", __func__
, status
);
8736 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8737 struct pnfs_device
*pdev
,
8738 struct rpc_cred
*cred
)
8740 struct nfs4_getdeviceinfo_args args
= {
8742 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8743 NOTIFY_DEVICEID4_DELETE
,
8745 struct nfs4_getdeviceinfo_res res
= {
8748 struct rpc_message msg
= {
8749 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8756 dprintk("--> %s\n", __func__
);
8757 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8758 if (res
.notification
& ~args
.notify_types
)
8759 dprintk("%s: unsupported notification\n", __func__
);
8760 if (res
.notification
!= args
.notify_types
)
8763 dprintk("<-- %s status=%d\n", __func__
, status
);
8768 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8769 struct pnfs_device
*pdev
,
8770 struct rpc_cred
*cred
)
8772 struct nfs4_exception exception
= { };
8776 err
= nfs4_handle_exception(server
,
8777 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8779 } while (exception
.retry
);
8782 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8784 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8786 struct nfs4_layoutcommit_data
*data
= calldata
;
8787 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8788 struct nfs4_session
*session
= nfs4_get_session(server
);
8790 nfs41_setup_sequence(session
,
8791 &data
->args
.seq_args
,
8797 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8799 struct nfs4_layoutcommit_data
*data
= calldata
;
8800 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8802 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8805 switch (task
->tk_status
) { /* Just ignore these failures */
8806 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8807 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8808 case -NFS4ERR_BADLAYOUT
: /* no layout */
8809 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8810 task
->tk_status
= 0;
8814 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8815 rpc_restart_call_prepare(task
);
8821 static void nfs4_layoutcommit_release(void *calldata
)
8823 struct nfs4_layoutcommit_data
*data
= calldata
;
8825 pnfs_cleanup_layoutcommit(data
);
8826 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8828 put_rpccred(data
->cred
);
8829 nfs_iput_and_deactive(data
->inode
);
8833 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8834 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8835 .rpc_call_done
= nfs4_layoutcommit_done
,
8836 .rpc_release
= nfs4_layoutcommit_release
,
8840 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8842 struct rpc_message msg
= {
8843 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8844 .rpc_argp
= &data
->args
,
8845 .rpc_resp
= &data
->res
,
8846 .rpc_cred
= data
->cred
,
8848 struct rpc_task_setup task_setup_data
= {
8849 .task
= &data
->task
,
8850 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8851 .rpc_message
= &msg
,
8852 .callback_ops
= &nfs4_layoutcommit_ops
,
8853 .callback_data
= data
,
8855 struct rpc_task
*task
;
8858 dprintk("NFS: initiating layoutcommit call. sync %d "
8859 "lbw: %llu inode %lu\n", sync
,
8860 data
->args
.lastbytewritten
,
8861 data
->args
.inode
->i_ino
);
8864 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8865 if (data
->inode
== NULL
) {
8866 nfs4_layoutcommit_release(data
);
8869 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8871 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8872 task
= rpc_run_task(&task_setup_data
);
8874 return PTR_ERR(task
);
8876 status
= task
->tk_status
;
8877 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8878 dprintk("%s: status %d\n", __func__
, status
);
8884 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8885 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8888 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8889 struct nfs_fsinfo
*info
,
8890 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8892 struct nfs41_secinfo_no_name_args args
= {
8893 .style
= SECINFO_STYLE_CURRENT_FH
,
8895 struct nfs4_secinfo_res res
= {
8898 struct rpc_message msg
= {
8899 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8903 struct rpc_clnt
*clnt
= server
->client
;
8904 struct rpc_cred
*cred
= NULL
;
8907 if (use_integrity
) {
8908 clnt
= server
->nfs_client
->cl_rpcclient
;
8909 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8910 msg
.rpc_cred
= cred
;
8913 dprintk("--> %s\n", __func__
);
8914 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8916 dprintk("<-- %s status=%d\n", __func__
, status
);
8925 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8926 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8928 struct nfs4_exception exception
= { };
8931 /* first try using integrity protection */
8932 err
= -NFS4ERR_WRONGSEC
;
8934 /* try to use integrity protection with machine cred */
8935 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8936 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8940 * if unable to use integrity protection, or SECINFO with
8941 * integrity protection returns NFS4ERR_WRONGSEC (which is
8942 * disallowed by spec, but exists in deployed servers) use
8943 * the current filesystem's rpc_client and the user cred.
8945 if (err
== -NFS4ERR_WRONGSEC
)
8946 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8951 case -NFS4ERR_WRONGSEC
:
8955 err
= nfs4_handle_exception(server
, err
, &exception
);
8957 } while (exception
.retry
);
8963 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8964 struct nfs_fsinfo
*info
)
8968 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8969 struct nfs4_secinfo_flavors
*flavors
;
8970 struct nfs4_secinfo4
*secinfo
;
8973 page
= alloc_page(GFP_KERNEL
);
8979 flavors
= page_address(page
);
8980 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8983 * Fall back on "guess and check" method if
8984 * the server doesn't support SECINFO_NO_NAME
8986 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8987 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8993 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8994 secinfo
= &flavors
->flavors
[i
];
8996 switch (secinfo
->flavor
) {
9000 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
9001 &secinfo
->flavor_info
);
9004 flavor
= RPC_AUTH_MAXFLAVOR
;
9008 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
9009 flavor
= RPC_AUTH_MAXFLAVOR
;
9011 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
9012 err
= nfs4_lookup_root_sec(server
, fhandle
,
9019 if (flavor
== RPC_AUTH_MAXFLAVOR
)
9030 static int _nfs41_test_stateid(struct nfs_server
*server
,
9031 nfs4_stateid
*stateid
,
9032 struct rpc_cred
*cred
)
9035 struct nfs41_test_stateid_args args
= {
9038 struct nfs41_test_stateid_res res
;
9039 struct rpc_message msg
= {
9040 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
9045 struct rpc_clnt
*rpc_client
= server
->client
;
9047 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9050 dprintk("NFS call test_stateid %p\n", stateid
);
9051 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
9052 nfs4_set_sequence_privileged(&args
.seq_args
);
9053 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
9054 &args
.seq_args
, &res
.seq_res
);
9055 if (status
!= NFS_OK
) {
9056 dprintk("NFS reply test_stateid: failed, %d\n", status
);
9059 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
9063 static void nfs4_handle_delay_or_session_error(struct nfs_server
*server
,
9064 int err
, struct nfs4_exception
*exception
)
9066 exception
->retry
= 0;
9068 case -NFS4ERR_DELAY
:
9069 case -NFS4ERR_RETRY_UNCACHED_REP
:
9070 nfs4_handle_exception(server
, err
, exception
);
9072 case -NFS4ERR_BADSESSION
:
9073 case -NFS4ERR_BADSLOT
:
9074 case -NFS4ERR_BAD_HIGH_SLOT
:
9075 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
9076 case -NFS4ERR_DEADSESSION
:
9077 nfs4_do_handle_exception(server
, err
, exception
);
9082 * nfs41_test_stateid - perform a TEST_STATEID operation
9084 * @server: server / transport on which to perform the operation
9085 * @stateid: state ID to test
9088 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9089 * Otherwise a negative NFS4ERR value is returned if the operation
9090 * failed or the state ID is not currently valid.
9092 static int nfs41_test_stateid(struct nfs_server
*server
,
9093 nfs4_stateid
*stateid
,
9094 struct rpc_cred
*cred
)
9096 struct nfs4_exception exception
= { };
9099 err
= _nfs41_test_stateid(server
, stateid
, cred
);
9100 nfs4_handle_delay_or_session_error(server
, err
, &exception
);
9101 } while (exception
.retry
);
9105 struct nfs_free_stateid_data
{
9106 struct nfs_server
*server
;
9107 struct nfs41_free_stateid_args args
;
9108 struct nfs41_free_stateid_res res
;
9111 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
9113 struct nfs_free_stateid_data
*data
= calldata
;
9114 nfs41_setup_sequence(nfs4_get_session(data
->server
),
9115 &data
->args
.seq_args
,
9120 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
9122 struct nfs_free_stateid_data
*data
= calldata
;
9124 nfs41_sequence_done(task
, &data
->res
.seq_res
);
9126 switch (task
->tk_status
) {
9127 case -NFS4ERR_DELAY
:
9128 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
9129 rpc_restart_call_prepare(task
);
9133 static void nfs41_free_stateid_release(void *calldata
)
9138 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
9139 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
9140 .rpc_call_done
= nfs41_free_stateid_done
,
9141 .rpc_release
= nfs41_free_stateid_release
,
9144 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
9145 const nfs4_stateid
*stateid
,
9146 struct rpc_cred
*cred
,
9149 struct rpc_message msg
= {
9150 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
9153 struct rpc_task_setup task_setup
= {
9154 .rpc_client
= server
->client
,
9155 .rpc_message
= &msg
,
9156 .callback_ops
= &nfs41_free_stateid_ops
,
9157 .flags
= RPC_TASK_ASYNC
,
9159 struct nfs_free_stateid_data
*data
;
9161 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9162 &task_setup
.rpc_client
, &msg
);
9164 dprintk("NFS call free_stateid %p\n", stateid
);
9165 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
9167 return ERR_PTR(-ENOMEM
);
9168 data
->server
= server
;
9169 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
9171 task_setup
.callback_data
= data
;
9173 msg
.rpc_argp
= &data
->args
;
9174 msg
.rpc_resp
= &data
->res
;
9175 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
9177 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
9179 return rpc_run_task(&task_setup
);
9183 * nfs41_free_stateid - perform a FREE_STATEID operation
9185 * @server: server / transport on which to perform the operation
9186 * @stateid: state ID to release
9188 * @is_recovery: set to true if this call needs to be privileged
9190 * Note: this function is always asynchronous.
9192 static int nfs41_free_stateid(struct nfs_server
*server
,
9193 const nfs4_stateid
*stateid
,
9194 struct rpc_cred
*cred
,
9197 struct rpc_task
*task
;
9199 task
= _nfs41_free_stateid(server
, stateid
, cred
, is_recovery
);
9201 return PTR_ERR(task
);
9207 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
9209 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
9211 nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
9212 nfs4_free_lock_state(server
, lsp
);
9215 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
9216 const nfs4_stateid
*s2
)
9218 if (s1
->type
!= s2
->type
)
9221 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
9224 if (s1
->seqid
== s2
->seqid
)
9226 if (s1
->seqid
== 0 || s2
->seqid
== 0)
9232 #endif /* CONFIG_NFS_V4_1 */
9234 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
9235 const nfs4_stateid
*s2
)
9237 return nfs4_stateid_match(s1
, s2
);
9241 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
9242 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9243 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9244 .recover_open
= nfs4_open_reclaim
,
9245 .recover_lock
= nfs4_lock_reclaim
,
9246 .establish_clid
= nfs4_init_clientid
,
9247 .detect_trunking
= nfs40_discover_server_trunking
,
9250 #if defined(CONFIG_NFS_V4_1)
9251 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
9252 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9253 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9254 .recover_open
= nfs4_open_reclaim
,
9255 .recover_lock
= nfs4_lock_reclaim
,
9256 .establish_clid
= nfs41_init_clientid
,
9257 .reclaim_complete
= nfs41_proc_reclaim_complete
,
9258 .detect_trunking
= nfs41_discover_server_trunking
,
9260 #endif /* CONFIG_NFS_V4_1 */
9262 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
9263 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9264 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9265 .recover_open
= nfs40_open_expired
,
9266 .recover_lock
= nfs4_lock_expired
,
9267 .establish_clid
= nfs4_init_clientid
,
9270 #if defined(CONFIG_NFS_V4_1)
9271 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
9272 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9273 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9274 .recover_open
= nfs41_open_expired
,
9275 .recover_lock
= nfs41_lock_expired
,
9276 .establish_clid
= nfs41_init_clientid
,
9278 #endif /* CONFIG_NFS_V4_1 */
9280 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
9281 .sched_state_renewal
= nfs4_proc_async_renew
,
9282 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
9283 .renew_lease
= nfs4_proc_renew
,
9286 #if defined(CONFIG_NFS_V4_1)
9287 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
9288 .sched_state_renewal
= nfs41_proc_async_sequence
,
9289 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
9290 .renew_lease
= nfs4_proc_sequence
,
9294 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
9295 .get_locations
= _nfs40_proc_get_locations
,
9296 .fsid_present
= _nfs40_proc_fsid_present
,
9299 #if defined(CONFIG_NFS_V4_1)
9300 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
9301 .get_locations
= _nfs41_proc_get_locations
,
9302 .fsid_present
= _nfs41_proc_fsid_present
,
9304 #endif /* CONFIG_NFS_V4_1 */
9306 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
9308 .init_caps
= NFS_CAP_READDIRPLUS
9309 | NFS_CAP_ATOMIC_OPEN
9310 | NFS_CAP_POSIX_LOCK
,
9311 .init_client
= nfs40_init_client
,
9312 .shutdown_client
= nfs40_shutdown_client
,
9313 .match_stateid
= nfs4_match_stateid
,
9314 .find_root_sec
= nfs4_find_root_sec
,
9315 .free_lock_state
= nfs4_release_lockowner
,
9316 .test_and_free_expired
= nfs40_test_and_free_expired_stateid
,
9317 .alloc_seqid
= nfs_alloc_seqid
,
9318 .call_sync_ops
= &nfs40_call_sync_ops
,
9319 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
9320 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
9321 .state_renewal_ops
= &nfs40_state_renewal_ops
,
9322 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
9325 #if defined(CONFIG_NFS_V4_1)
9326 static struct nfs_seqid
*
9327 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
9332 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
9334 .init_caps
= NFS_CAP_READDIRPLUS
9335 | NFS_CAP_ATOMIC_OPEN
9336 | NFS_CAP_POSIX_LOCK
9337 | NFS_CAP_STATEID_NFSV41
9338 | NFS_CAP_ATOMIC_OPEN_V1
,
9339 .init_client
= nfs41_init_client
,
9340 .shutdown_client
= nfs41_shutdown_client
,
9341 .match_stateid
= nfs41_match_stateid
,
9342 .find_root_sec
= nfs41_find_root_sec
,
9343 .free_lock_state
= nfs41_free_lock_state
,
9344 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9345 .alloc_seqid
= nfs_alloc_no_seqid
,
9346 .session_trunk
= nfs4_test_session_trunk
,
9347 .call_sync_ops
= &nfs41_call_sync_ops
,
9348 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9349 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9350 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9351 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9355 #if defined(CONFIG_NFS_V4_2)
9356 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
9358 .init_caps
= NFS_CAP_READDIRPLUS
9359 | NFS_CAP_ATOMIC_OPEN
9360 | NFS_CAP_POSIX_LOCK
9361 | NFS_CAP_STATEID_NFSV41
9362 | NFS_CAP_ATOMIC_OPEN_V1
9365 | NFS_CAP_DEALLOCATE
9367 | NFS_CAP_LAYOUTSTATS
9369 .init_client
= nfs41_init_client
,
9370 .shutdown_client
= nfs41_shutdown_client
,
9371 .match_stateid
= nfs41_match_stateid
,
9372 .find_root_sec
= nfs41_find_root_sec
,
9373 .free_lock_state
= nfs41_free_lock_state
,
9374 .call_sync_ops
= &nfs41_call_sync_ops
,
9375 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9376 .alloc_seqid
= nfs_alloc_no_seqid
,
9377 .session_trunk
= nfs4_test_session_trunk
,
9378 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9379 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9380 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9381 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9385 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
9386 [0] = &nfs_v4_0_minor_ops
,
9387 #if defined(CONFIG_NFS_V4_1)
9388 [1] = &nfs_v4_1_minor_ops
,
9390 #if defined(CONFIG_NFS_V4_2)
9391 [2] = &nfs_v4_2_minor_ops
,
9395 static ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
9397 ssize_t error
, error2
;
9399 error
= generic_listxattr(dentry
, list
, size
);
9407 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
9410 return error
+ error2
;
9413 static const struct inode_operations nfs4_dir_inode_operations
= {
9414 .create
= nfs_create
,
9415 .lookup
= nfs_lookup
,
9416 .atomic_open
= nfs_atomic_open
,
9418 .unlink
= nfs_unlink
,
9419 .symlink
= nfs_symlink
,
9423 .rename
= nfs_rename
,
9424 .permission
= nfs_permission
,
9425 .getattr
= nfs_getattr
,
9426 .setattr
= nfs_setattr
,
9427 .listxattr
= nfs4_listxattr
,
9430 static const struct inode_operations nfs4_file_inode_operations
= {
9431 .permission
= nfs_permission
,
9432 .getattr
= nfs_getattr
,
9433 .setattr
= nfs_setattr
,
9434 .listxattr
= nfs4_listxattr
,
9437 const struct nfs_rpc_ops nfs_v4_clientops
= {
9438 .version
= 4, /* protocol version */
9439 .dentry_ops
= &nfs4_dentry_operations
,
9440 .dir_inode_ops
= &nfs4_dir_inode_operations
,
9441 .file_inode_ops
= &nfs4_file_inode_operations
,
9442 .file_ops
= &nfs4_file_operations
,
9443 .getroot
= nfs4_proc_get_root
,
9444 .submount
= nfs4_submount
,
9445 .try_mount
= nfs4_try_mount
,
9446 .getattr
= nfs4_proc_getattr
,
9447 .setattr
= nfs4_proc_setattr
,
9448 .lookup
= nfs4_proc_lookup
,
9449 .access
= nfs4_proc_access
,
9450 .readlink
= nfs4_proc_readlink
,
9451 .create
= nfs4_proc_create
,
9452 .remove
= nfs4_proc_remove
,
9453 .unlink_setup
= nfs4_proc_unlink_setup
,
9454 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
9455 .unlink_done
= nfs4_proc_unlink_done
,
9456 .rename_setup
= nfs4_proc_rename_setup
,
9457 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
9458 .rename_done
= nfs4_proc_rename_done
,
9459 .link
= nfs4_proc_link
,
9460 .symlink
= nfs4_proc_symlink
,
9461 .mkdir
= nfs4_proc_mkdir
,
9462 .rmdir
= nfs4_proc_remove
,
9463 .readdir
= nfs4_proc_readdir
,
9464 .mknod
= nfs4_proc_mknod
,
9465 .statfs
= nfs4_proc_statfs
,
9466 .fsinfo
= nfs4_proc_fsinfo
,
9467 .pathconf
= nfs4_proc_pathconf
,
9468 .set_capabilities
= nfs4_server_capabilities
,
9469 .decode_dirent
= nfs4_decode_dirent
,
9470 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
9471 .read_setup
= nfs4_proc_read_setup
,
9472 .read_done
= nfs4_read_done
,
9473 .write_setup
= nfs4_proc_write_setup
,
9474 .write_done
= nfs4_write_done
,
9475 .commit_setup
= nfs4_proc_commit_setup
,
9476 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
9477 .commit_done
= nfs4_commit_done
,
9478 .lock
= nfs4_proc_lock
,
9479 .clear_acl_cache
= nfs4_zap_acl_attr
,
9480 .close_context
= nfs4_close_context
,
9481 .open_context
= nfs4_atomic_open
,
9482 .have_delegation
= nfs4_have_delegation
,
9483 .return_delegation
= nfs4_inode_return_delegation
,
9484 .alloc_client
= nfs4_alloc_client
,
9485 .init_client
= nfs4_init_client
,
9486 .free_client
= nfs4_free_client
,
9487 .create_server
= nfs4_create_server
,
9488 .clone_server
= nfs_clone_server
,
9491 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
9492 .name
= XATTR_NAME_NFSV4_ACL
,
9493 .list
= nfs4_xattr_list_nfs4_acl
,
9494 .get
= nfs4_xattr_get_nfs4_acl
,
9495 .set
= nfs4_xattr_set_nfs4_acl
,
9498 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
9499 &nfs4_xattr_nfs4_acl_handler
,
9500 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9501 &nfs4_xattr_nfs4_label_handler
,