4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
89 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
91 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
93 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
94 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
95 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
96 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
97 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
98 struct nfs4_label
*olabel
);
99 #ifdef CONFIG_NFS_V4_1
100 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
102 static int nfs41_free_stateid(struct nfs_server
*, const nfs4_stateid
*,
103 struct rpc_cred
*, bool);
106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
107 static inline struct nfs4_label
*
108 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
109 struct iattr
*sattr
, struct nfs4_label
*label
)
116 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
119 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
120 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
127 nfs4_label_release_security(struct nfs4_label
*label
)
130 security_release_secctx(label
->label
, label
->len
);
132 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
135 return server
->attr_bitmask
;
137 return server
->attr_bitmask_nl
;
140 static inline struct nfs4_label
*
141 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
142 struct iattr
*sattr
, struct nfs4_label
*l
)
145 nfs4_label_release_security(struct nfs4_label
*label
)
148 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
149 { return server
->attr_bitmask
; }
152 /* Prevent leaks of NFSv4 errors into userland */
153 static int nfs4_map_errors(int err
)
158 case -NFS4ERR_RESOURCE
:
159 case -NFS4ERR_LAYOUTTRYLATER
:
160 case -NFS4ERR_RECALLCONFLICT
:
162 case -NFS4ERR_WRONGSEC
:
163 case -NFS4ERR_WRONG_CRED
:
165 case -NFS4ERR_BADOWNER
:
166 case -NFS4ERR_BADNAME
:
168 case -NFS4ERR_SHARE_DENIED
:
170 case -NFS4ERR_MINOR_VERS_MISMATCH
:
171 return -EPROTONOSUPPORT
;
172 case -NFS4ERR_FILE_OPEN
:
175 dprintk("%s could not handle NFSv4 error %d\n",
183 * This is our standard bitmap for GETATTR requests.
185 const u32 nfs4_fattr_bitmap
[3] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID
,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
206 static const u32 nfs4_pnfs_open_bitmap
[3] = {
208 | FATTR4_WORD0_CHANGE
211 | FATTR4_WORD0_FILEID
,
213 | FATTR4_WORD1_NUMLINKS
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY
,
221 FATTR4_WORD2_MDSTHRESHOLD
222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
227 static const u32 nfs4_open_noattr_bitmap
[3] = {
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID
,
233 const u32 nfs4_statfs_bitmap
[3] = {
234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL
,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
242 const u32 nfs4_pathconf_bitmap
[3] = {
244 | FATTR4_WORD0_MAXNAME
,
248 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME
,
252 FATTR4_WORD1_TIME_DELTA
253 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
255 | FATTR4_WORD2_CLONE_BLKSIZE
258 const u32 nfs4_fs_locations_bitmap
[3] = {
260 | FATTR4_WORD0_CHANGE
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS
,
266 | FATTR4_WORD1_NUMLINKS
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
274 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
277 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
278 struct nfs4_readdir_arg
*readdir
)
283 readdir
->cookie
= cookie
;
284 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
289 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
300 start
= p
= kmap_atomic(*readdir
->pages
);
303 *p
++ = xdr_one
; /* next */
304 *p
++ = xdr_zero
; /* cookie, first word */
305 *p
++ = xdr_one
; /* cookie, second word */
306 *p
++ = xdr_one
; /* entry len */
307 memcpy(p
, ".\0\0\0", 4); /* entry */
309 *p
++ = xdr_one
; /* bitmap length */
310 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
311 *p
++ = htonl(8); /* attribute buffer length */
312 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
)));
315 *p
++ = xdr_one
; /* next */
316 *p
++ = xdr_zero
; /* cookie, first word */
317 *p
++ = xdr_two
; /* cookie, second word */
318 *p
++ = xdr_two
; /* entry len */
319 memcpy(p
, "..\0\0", 4); /* entry */
321 *p
++ = xdr_one
; /* bitmap length */
322 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
323 *p
++ = htonl(8); /* attribute buffer length */
324 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
326 readdir
->pgbase
= (char *)p
- (char *)start
;
327 readdir
->count
-= readdir
->pgbase
;
328 kunmap_atomic(start
);
331 static void nfs4_test_and_free_stateid(struct nfs_server
*server
,
332 nfs4_stateid
*stateid
,
333 struct rpc_cred
*cred
)
335 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
337 ops
->test_and_free_expired(server
, stateid
, cred
);
340 static void __nfs4_free_revoked_stateid(struct nfs_server
*server
,
341 nfs4_stateid
*stateid
,
342 struct rpc_cred
*cred
)
344 stateid
->type
= NFS4_REVOKED_STATEID_TYPE
;
345 nfs4_test_and_free_stateid(server
, stateid
, cred
);
348 static void nfs4_free_revoked_stateid(struct nfs_server
*server
,
349 const nfs4_stateid
*stateid
,
350 struct rpc_cred
*cred
)
354 nfs4_stateid_copy(&tmp
, stateid
);
355 __nfs4_free_revoked_stateid(server
, &tmp
, cred
);
358 static long nfs4_update_delay(long *timeout
)
362 return NFS4_POLL_RETRY_MAX
;
364 *timeout
= NFS4_POLL_RETRY_MIN
;
365 if (*timeout
> NFS4_POLL_RETRY_MAX
)
366 *timeout
= NFS4_POLL_RETRY_MAX
;
372 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout
));
380 if (fatal_signal_pending(current
))
385 /* This is the error handling routine for processes that are allowed
388 static int nfs4_do_handle_exception(struct nfs_server
*server
,
389 int errorcode
, struct nfs4_exception
*exception
)
391 struct nfs_client
*clp
= server
->nfs_client
;
392 struct nfs4_state
*state
= exception
->state
;
393 const nfs4_stateid
*stateid
= exception
->stateid
;
394 struct inode
*inode
= exception
->inode
;
397 exception
->delay
= 0;
398 exception
->recovering
= 0;
399 exception
->retry
= 0;
401 if (stateid
== NULL
&& state
!= NULL
)
402 stateid
= &state
->stateid
;
407 case -NFS4ERR_DELEG_REVOKED
:
408 case -NFS4ERR_ADMIN_REVOKED
:
409 case -NFS4ERR_EXPIRED
:
410 case -NFS4ERR_BAD_STATEID
:
411 if (inode
!= NULL
&& stateid
!= NULL
) {
412 nfs_inode_find_state_and_recover(inode
,
414 goto wait_on_recovery
;
416 case -NFS4ERR_OPENMODE
:
420 err
= nfs_async_inode_return_delegation(inode
,
423 goto wait_on_recovery
;
424 if (stateid
!= NULL
&& stateid
->type
== NFS4_DELEGATION_STATEID_TYPE
) {
425 exception
->retry
= 1;
431 ret
= nfs4_schedule_stateid_recovery(server
, state
);
434 goto wait_on_recovery
;
435 case -NFS4ERR_STALE_STATEID
:
436 case -NFS4ERR_STALE_CLIENTID
:
437 nfs4_schedule_lease_recovery(clp
);
438 goto wait_on_recovery
;
440 ret
= nfs4_schedule_migration_recovery(server
);
443 goto wait_on_recovery
;
444 case -NFS4ERR_LEASE_MOVED
:
445 nfs4_schedule_lease_moved_recovery(clp
);
446 goto wait_on_recovery
;
447 #if defined(CONFIG_NFS_V4_1)
448 case -NFS4ERR_BADSESSION
:
449 case -NFS4ERR_BADSLOT
:
450 case -NFS4ERR_BAD_HIGH_SLOT
:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
452 case -NFS4ERR_DEADSESSION
:
453 case -NFS4ERR_SEQ_FALSE_RETRY
:
454 case -NFS4ERR_SEQ_MISORDERED
:
455 dprintk("%s ERROR: %d Reset session\n", __func__
,
457 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
458 goto wait_on_recovery
;
459 #endif /* defined(CONFIG_NFS_V4_1) */
460 case -NFS4ERR_FILE_OPEN
:
461 if (exception
->timeout
> HZ
) {
462 /* We have retried a decent amount, time to
469 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
471 case -NFS4ERR_LAYOUTTRYLATER
:
472 case -NFS4ERR_RECALLCONFLICT
:
473 exception
->delay
= 1;
476 case -NFS4ERR_RETRY_UNCACHED_REP
:
477 case -NFS4ERR_OLD_STATEID
:
478 exception
->retry
= 1;
480 case -NFS4ERR_BADOWNER
:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME
:
483 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
484 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
485 exception
->retry
= 1;
486 printk(KERN_WARNING
"NFS: v4 server %s "
487 "does not accept raw "
489 "Reenabling the idmapper.\n",
490 server
->nfs_client
->cl_hostname
);
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret
);
496 exception
->recovering
= 1;
500 /* This is the error handling routine for processes that are allowed
503 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
505 struct nfs_client
*clp
= server
->nfs_client
;
508 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
509 if (exception
->delay
) {
510 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
513 if (exception
->recovering
) {
514 ret
= nfs4_wait_clnt_recover(clp
);
515 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
522 exception
->retry
= 1;
527 nfs4_async_handle_exception(struct rpc_task
*task
, struct nfs_server
*server
,
528 int errorcode
, struct nfs4_exception
*exception
)
530 struct nfs_client
*clp
= server
->nfs_client
;
533 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
534 if (exception
->delay
) {
535 rpc_delay(task
, nfs4_update_delay(&exception
->timeout
));
538 if (exception
->recovering
) {
539 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
541 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
544 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
549 exception
->retry
= 1;
554 nfs4_async_handle_error(struct rpc_task
*task
, struct nfs_server
*server
,
555 struct nfs4_state
*state
, long *timeout
)
557 struct nfs4_exception exception
= {
561 if (task
->tk_status
>= 0)
564 exception
.timeout
= *timeout
;
565 task
->tk_status
= nfs4_async_handle_exception(task
, server
,
568 if (exception
.delay
&& timeout
)
569 *timeout
= exception
.timeout
;
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
579 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
581 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
583 if (flavor
== RPC_AUTH_GSS_KRB5I
||
584 flavor
== RPC_AUTH_GSS_KRB5P
)
590 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
592 spin_lock(&clp
->cl_lock
);
593 if (time_before(clp
->cl_last_renewal
,timestamp
))
594 clp
->cl_last_renewal
= timestamp
;
595 spin_unlock(&clp
->cl_lock
);
598 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
600 struct nfs_client
*clp
= server
->nfs_client
;
602 if (!nfs4_has_session(clp
))
603 do_renew_lease(clp
, timestamp
);
606 struct nfs4_call_sync_data
{
607 const struct nfs_server
*seq_server
;
608 struct nfs4_sequence_args
*seq_args
;
609 struct nfs4_sequence_res
*seq_res
;
612 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
613 struct nfs4_sequence_res
*res
, int cache_reply
)
615 args
->sa_slot
= NULL
;
616 args
->sa_cache_this
= cache_reply
;
617 args
->sa_privileged
= 0;
622 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
624 args
->sa_privileged
= 1;
627 int nfs40_setup_sequence(struct nfs4_slot_table
*tbl
,
628 struct nfs4_sequence_args
*args
,
629 struct nfs4_sequence_res
*res
,
630 struct rpc_task
*task
)
632 struct nfs4_slot
*slot
;
634 /* slot already allocated? */
635 if (res
->sr_slot
!= NULL
)
638 spin_lock(&tbl
->slot_tbl_lock
);
639 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
642 slot
= nfs4_alloc_slot(tbl
);
644 if (slot
== ERR_PTR(-ENOMEM
))
645 task
->tk_timeout
= HZ
>> 2;
648 spin_unlock(&tbl
->slot_tbl_lock
);
650 slot
->privileged
= args
->sa_privileged
? 1 : 0;
651 args
->sa_slot
= slot
;
655 rpc_call_start(task
);
659 if (args
->sa_privileged
)
660 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
661 NULL
, RPC_PRIORITY_PRIVILEGED
);
663 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
664 spin_unlock(&tbl
->slot_tbl_lock
);
667 EXPORT_SYMBOL_GPL(nfs40_setup_sequence
);
669 static void nfs40_sequence_free_slot(struct nfs4_sequence_res
*res
)
671 struct nfs4_slot
*slot
= res
->sr_slot
;
672 struct nfs4_slot_table
*tbl
;
675 spin_lock(&tbl
->slot_tbl_lock
);
676 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
677 nfs4_free_slot(tbl
, slot
);
678 spin_unlock(&tbl
->slot_tbl_lock
);
683 static int nfs40_sequence_done(struct rpc_task
*task
,
684 struct nfs4_sequence_res
*res
)
686 if (res
->sr_slot
!= NULL
)
687 nfs40_sequence_free_slot(res
);
691 #if defined(CONFIG_NFS_V4_1)
693 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
695 struct nfs4_session
*session
;
696 struct nfs4_slot_table
*tbl
;
697 struct nfs4_slot
*slot
= res
->sr_slot
;
698 bool send_new_highest_used_slotid
= false;
701 session
= tbl
->session
;
703 /* Bump the slot sequence number */
708 spin_lock(&tbl
->slot_tbl_lock
);
709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
712 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
713 send_new_highest_used_slotid
= true;
715 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
716 send_new_highest_used_slotid
= false;
719 nfs4_free_slot(tbl
, slot
);
721 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
722 send_new_highest_used_slotid
= false;
724 spin_unlock(&tbl
->slot_tbl_lock
);
726 if (send_new_highest_used_slotid
)
727 nfs41_notify_server(session
->clp
);
728 if (waitqueue_active(&tbl
->slot_waitq
))
729 wake_up_all(&tbl
->slot_waitq
);
732 static int nfs41_sequence_process(struct rpc_task
*task
,
733 struct nfs4_sequence_res
*res
)
735 struct nfs4_session
*session
;
736 struct nfs4_slot
*slot
= res
->sr_slot
;
737 struct nfs_client
*clp
;
738 bool interrupted
= false;
743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task
))
747 session
= slot
->table
->session
;
749 if (slot
->interrupted
) {
750 slot
->interrupted
= 0;
754 trace_nfs4_sequence_done(session
, res
);
755 /* Check the SEQUENCE operation status */
756 switch (res
->sr_status
) {
758 /* If previous op on slot was interrupted and we reused
759 * the seq# and got a reply from the cache, then retry
761 if (task
->tk_status
== -EREMOTEIO
&& interrupted
) {
765 /* Update the slot's sequence and clientid lease timer */
768 do_renew_lease(clp
, res
->sr_timestamp
);
769 /* Check sequence flags */
770 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
,
772 nfs41_update_target_slotid(slot
->table
, slot
, res
);
776 * sr_status remains 1 if an RPC level error occurred.
777 * The server may or may not have processed the sequence
779 * Mark the slot as having hosted an interrupted RPC call.
781 slot
->interrupted
= 1;
784 /* The server detected a resend of the RPC call and
785 * returned NFS4ERR_DELAY as per Section 2.10.6.2
788 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
793 case -NFS4ERR_BADSLOT
:
795 * The slot id we used was probably retired. Try again
796 * using a different slot id.
799 case -NFS4ERR_SEQ_MISORDERED
:
801 * Was the last operation on this sequence interrupted?
802 * If so, retry after bumping the sequence number.
809 * Could this slot have been previously retired?
810 * If so, then the server may be expecting seq_nr = 1!
812 if (slot
->seq_nr
!= 1) {
817 case -NFS4ERR_SEQ_FALSE_RETRY
:
821 /* Just update the slot sequence no. */
825 /* The session may be reset by one of the error handlers. */
826 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
830 if (rpc_restart_call_prepare(task
)) {
831 nfs41_sequence_free_slot(res
);
837 if (!rpc_restart_call(task
))
839 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
843 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
845 if (!nfs41_sequence_process(task
, res
))
847 if (res
->sr_slot
!= NULL
)
848 nfs41_sequence_free_slot(res
);
852 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
854 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
856 if (res
->sr_slot
== NULL
)
858 if (res
->sr_slot
->table
->session
!= NULL
)
859 return nfs41_sequence_process(task
, res
);
860 return nfs40_sequence_done(task
, res
);
863 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
865 if (res
->sr_slot
!= NULL
) {
866 if (res
->sr_slot
->table
->session
!= NULL
)
867 nfs41_sequence_free_slot(res
);
869 nfs40_sequence_free_slot(res
);
873 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
875 if (res
->sr_slot
== NULL
)
877 if (!res
->sr_slot
->table
->session
)
878 return nfs40_sequence_done(task
, res
);
879 return nfs41_sequence_done(task
, res
);
881 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
883 int nfs41_setup_sequence(struct nfs4_session
*session
,
884 struct nfs4_sequence_args
*args
,
885 struct nfs4_sequence_res
*res
,
886 struct rpc_task
*task
)
888 struct nfs4_slot
*slot
;
889 struct nfs4_slot_table
*tbl
;
891 dprintk("--> %s\n", __func__
);
892 /* slot already allocated? */
893 if (res
->sr_slot
!= NULL
)
896 tbl
= &session
->fc_slot_table
;
898 task
->tk_timeout
= 0;
900 spin_lock(&tbl
->slot_tbl_lock
);
901 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
902 !args
->sa_privileged
) {
903 /* The state manager will wait until the slot table is empty */
904 dprintk("%s session is draining\n", __func__
);
908 slot
= nfs4_alloc_slot(tbl
);
910 /* If out of memory, try again in 1/4 second */
911 if (slot
== ERR_PTR(-ENOMEM
))
912 task
->tk_timeout
= HZ
>> 2;
913 dprintk("<-- %s: no free slots\n", __func__
);
916 spin_unlock(&tbl
->slot_tbl_lock
);
918 slot
->privileged
= args
->sa_privileged
? 1 : 0;
919 args
->sa_slot
= slot
;
921 dprintk("<-- %s slotid=%u seqid=%u\n", __func__
,
922 slot
->slot_nr
, slot
->seq_nr
);
925 res
->sr_timestamp
= jiffies
;
926 res
->sr_status_flags
= 0;
928 * sr_status is only set in decode_sequence, and so will remain
929 * set to 1 if an rpc level failure occurs.
932 trace_nfs4_setup_sequence(session
, args
);
934 rpc_call_start(task
);
937 /* Privileged tasks are queued with top priority */
938 if (args
->sa_privileged
)
939 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
940 NULL
, RPC_PRIORITY_PRIVILEGED
);
942 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
943 spin_unlock(&tbl
->slot_tbl_lock
);
946 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
948 static int nfs4_setup_sequence(const struct nfs_server
*server
,
949 struct nfs4_sequence_args
*args
,
950 struct nfs4_sequence_res
*res
,
951 struct rpc_task
*task
)
953 struct nfs4_session
*session
= nfs4_get_session(server
);
957 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
960 dprintk("--> %s clp %p session %p sr_slot %u\n",
961 __func__
, session
->clp
, session
, res
->sr_slot
?
962 res
->sr_slot
->slot_nr
: NFS4_NO_SLOT
);
964 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
966 dprintk("<-- %s status=%d\n", __func__
, ret
);
970 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
972 struct nfs4_call_sync_data
*data
= calldata
;
973 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
975 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
977 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
980 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
982 struct nfs4_call_sync_data
*data
= calldata
;
984 nfs41_sequence_done(task
, data
->seq_res
);
987 static const struct rpc_call_ops nfs41_call_sync_ops
= {
988 .rpc_call_prepare
= nfs41_call_sync_prepare
,
989 .rpc_call_done
= nfs41_call_sync_done
,
992 #else /* !CONFIG_NFS_V4_1 */
994 static int nfs4_setup_sequence(const struct nfs_server
*server
,
995 struct nfs4_sequence_args
*args
,
996 struct nfs4_sequence_res
*res
,
997 struct rpc_task
*task
)
999 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
1003 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
1005 return nfs40_sequence_done(task
, res
);
1008 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
1010 if (res
->sr_slot
!= NULL
)
1011 nfs40_sequence_free_slot(res
);
1014 int nfs4_sequence_done(struct rpc_task
*task
,
1015 struct nfs4_sequence_res
*res
)
1017 return nfs40_sequence_done(task
, res
);
1019 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
1021 #endif /* !CONFIG_NFS_V4_1 */
1023 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
1025 struct nfs4_call_sync_data
*data
= calldata
;
1026 nfs4_setup_sequence(data
->seq_server
,
1027 data
->seq_args
, data
->seq_res
, task
);
1030 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
1032 struct nfs4_call_sync_data
*data
= calldata
;
1033 nfs4_sequence_done(task
, data
->seq_res
);
1036 static const struct rpc_call_ops nfs40_call_sync_ops
= {
1037 .rpc_call_prepare
= nfs40_call_sync_prepare
,
1038 .rpc_call_done
= nfs40_call_sync_done
,
1041 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
1042 struct nfs_server
*server
,
1043 struct rpc_message
*msg
,
1044 struct nfs4_sequence_args
*args
,
1045 struct nfs4_sequence_res
*res
)
1048 struct rpc_task
*task
;
1049 struct nfs_client
*clp
= server
->nfs_client
;
1050 struct nfs4_call_sync_data data
= {
1051 .seq_server
= server
,
1055 struct rpc_task_setup task_setup
= {
1058 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
1059 .callback_data
= &data
1062 task
= rpc_run_task(&task_setup
);
1064 ret
= PTR_ERR(task
);
1066 ret
= task
->tk_status
;
1072 int nfs4_call_sync(struct rpc_clnt
*clnt
,
1073 struct nfs_server
*server
,
1074 struct rpc_message
*msg
,
1075 struct nfs4_sequence_args
*args
,
1076 struct nfs4_sequence_res
*res
,
1079 nfs4_init_sequence(args
, res
, cache_reply
);
1080 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
1083 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
1085 struct nfs_inode
*nfsi
= NFS_I(dir
);
1087 spin_lock(&dir
->i_lock
);
1088 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1089 if (!cinfo
->atomic
|| cinfo
->before
!= dir
->i_version
)
1090 nfs_force_lookup_revalidate(dir
);
1091 dir
->i_version
= cinfo
->after
;
1092 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
1093 nfs_fscache_invalidate(dir
);
1094 spin_unlock(&dir
->i_lock
);
1097 struct nfs4_opendata
{
1099 struct nfs_openargs o_arg
;
1100 struct nfs_openres o_res
;
1101 struct nfs_open_confirmargs c_arg
;
1102 struct nfs_open_confirmres c_res
;
1103 struct nfs4_string owner_name
;
1104 struct nfs4_string group_name
;
1105 struct nfs4_label
*a_label
;
1106 struct nfs_fattr f_attr
;
1107 struct nfs4_label
*f_label
;
1109 struct dentry
*dentry
;
1110 struct nfs4_state_owner
*owner
;
1111 struct nfs4_state
*state
;
1113 unsigned long timestamp
;
1114 unsigned int rpc_done
: 1;
1115 unsigned int file_created
: 1;
1116 unsigned int is_recover
: 1;
1121 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
1122 int err
, struct nfs4_exception
*exception
)
1126 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1128 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
1129 exception
->retry
= 1;
1134 nfs4_map_atomic_open_share(struct nfs_server
*server
,
1135 fmode_t fmode
, int openflags
)
1139 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
1141 res
= NFS4_SHARE_ACCESS_READ
;
1144 res
= NFS4_SHARE_ACCESS_WRITE
;
1146 case FMODE_READ
|FMODE_WRITE
:
1147 res
= NFS4_SHARE_ACCESS_BOTH
;
1149 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1151 /* Want no delegation if we're using O_DIRECT */
1152 if (openflags
& O_DIRECT
)
1153 res
|= NFS4_SHARE_WANT_NO_DELEG
;
1158 static enum open_claim_type4
1159 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
1160 enum open_claim_type4 claim
)
1162 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
1167 case NFS4_OPEN_CLAIM_FH
:
1168 return NFS4_OPEN_CLAIM_NULL
;
1169 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1170 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1171 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1172 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
1176 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
1178 p
->o_res
.f_attr
= &p
->f_attr
;
1179 p
->o_res
.f_label
= p
->f_label
;
1180 p
->o_res
.seqid
= p
->o_arg
.seqid
;
1181 p
->c_res
.seqid
= p
->c_arg
.seqid
;
1182 p
->o_res
.server
= p
->o_arg
.server
;
1183 p
->o_res
.access_request
= p
->o_arg
.access
;
1184 nfs_fattr_init(&p
->f_attr
);
1185 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
1188 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
1189 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
1190 const struct iattr
*attrs
,
1191 struct nfs4_label
*label
,
1192 enum open_claim_type4 claim
,
1195 struct dentry
*parent
= dget_parent(dentry
);
1196 struct inode
*dir
= d_inode(parent
);
1197 struct nfs_server
*server
= NFS_SERVER(dir
);
1198 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1199 struct nfs4_opendata
*p
;
1201 p
= kzalloc(sizeof(*p
), gfp_mask
);
1205 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1206 if (IS_ERR(p
->f_label
))
1209 p
->a_label
= nfs4_label_alloc(server
, gfp_mask
);
1210 if (IS_ERR(p
->a_label
))
1213 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1214 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1215 if (IS_ERR(p
->o_arg
.seqid
))
1216 goto err_free_label
;
1217 nfs_sb_active(dentry
->d_sb
);
1218 p
->dentry
= dget(dentry
);
1221 atomic_inc(&sp
->so_count
);
1222 p
->o_arg
.open_flags
= flags
;
1223 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1224 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1226 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1227 * will return permission denied for all bits until close */
1228 if (!(flags
& O_EXCL
)) {
1229 /* ask server to check for all possible rights as results
1231 p
->o_arg
.access
= NFS4_ACCESS_READ
| NFS4_ACCESS_MODIFY
|
1232 NFS4_ACCESS_EXTEND
| NFS4_ACCESS_EXECUTE
;
1234 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1235 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1236 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1237 p
->o_arg
.name
= &dentry
->d_name
;
1238 p
->o_arg
.server
= server
;
1239 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1240 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1241 p
->o_arg
.label
= nfs4_label_copy(p
->a_label
, label
);
1242 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1243 switch (p
->o_arg
.claim
) {
1244 case NFS4_OPEN_CLAIM_NULL
:
1245 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1246 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1247 p
->o_arg
.fh
= NFS_FH(dir
);
1249 case NFS4_OPEN_CLAIM_PREVIOUS
:
1250 case NFS4_OPEN_CLAIM_FH
:
1251 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1252 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1253 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1255 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1258 p
->o_arg
.u
.attrs
= &p
->attrs
;
1259 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1262 verf
[1] = current
->pid
;
1263 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1264 sizeof(p
->o_arg
.u
.verifier
.data
));
1266 p
->c_arg
.fh
= &p
->o_res
.fh
;
1267 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1268 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1269 nfs4_init_opendata_res(p
);
1270 kref_init(&p
->kref
);
1274 nfs4_label_free(p
->a_label
);
1276 nfs4_label_free(p
->f_label
);
1284 static void nfs4_opendata_free(struct kref
*kref
)
1286 struct nfs4_opendata
*p
= container_of(kref
,
1287 struct nfs4_opendata
, kref
);
1288 struct super_block
*sb
= p
->dentry
->d_sb
;
1290 nfs_free_seqid(p
->o_arg
.seqid
);
1291 nfs4_sequence_free_slot(&p
->o_res
.seq_res
);
1292 if (p
->state
!= NULL
)
1293 nfs4_put_open_state(p
->state
);
1294 nfs4_put_state_owner(p
->owner
);
1296 nfs4_label_free(p
->a_label
);
1297 nfs4_label_free(p
->f_label
);
1301 nfs_sb_deactive(sb
);
1302 nfs_fattr_free_names(&p
->f_attr
);
1303 kfree(p
->f_attr
.mdsthreshold
);
1307 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1310 kref_put(&p
->kref
, nfs4_opendata_free
);
1313 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1317 ret
= rpc_wait_for_completion_task(task
);
1321 static bool nfs4_mode_match_open_stateid(struct nfs4_state
*state
,
1324 switch(fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1325 case FMODE_READ
|FMODE_WRITE
:
1326 return state
->n_rdwr
!= 0;
1328 return state
->n_wronly
!= 0;
1330 return state
->n_rdonly
!= 0;
1336 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1340 if (open_mode
& (O_EXCL
|O_TRUNC
))
1342 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1344 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1345 && state
->n_rdonly
!= 0;
1348 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1349 && state
->n_wronly
!= 0;
1351 case FMODE_READ
|FMODE_WRITE
:
1352 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1353 && state
->n_rdwr
!= 0;
1359 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
,
1360 enum open_claim_type4 claim
)
1362 if (delegation
== NULL
)
1364 if ((delegation
->type
& fmode
) != fmode
)
1366 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1369 case NFS4_OPEN_CLAIM_NULL
:
1370 case NFS4_OPEN_CLAIM_FH
:
1372 case NFS4_OPEN_CLAIM_PREVIOUS
:
1373 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1378 nfs_mark_delegation_referenced(delegation
);
1382 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1391 case FMODE_READ
|FMODE_WRITE
:
1394 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1397 #ifdef CONFIG_NFS_V4_1
1398 static bool nfs_open_stateid_recover_openmode(struct nfs4_state
*state
)
1400 if (state
->n_rdonly
&& !test_bit(NFS_O_RDONLY_STATE
, &state
->flags
))
1402 if (state
->n_wronly
&& !test_bit(NFS_O_WRONLY_STATE
, &state
->flags
))
1404 if (state
->n_rdwr
&& !test_bit(NFS_O_RDWR_STATE
, &state
->flags
))
1408 #endif /* CONFIG_NFS_V4_1 */
1410 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1412 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1413 bool need_recover
= false;
1415 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1416 need_recover
= true;
1417 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1418 need_recover
= true;
1419 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1420 need_recover
= true;
1422 nfs4_state_mark_reclaim_nograce(clp
, state
);
1425 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1426 const nfs4_stateid
*stateid
, nfs4_stateid
*freeme
)
1428 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1430 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1431 nfs4_stateid_copy(freeme
, &state
->open_stateid
);
1432 nfs_test_and_clear_all_open_stateid(state
);
1435 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1440 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1442 if (!(state
->n_wronly
|| state
->n_rdonly
|| state
->n_rdwr
))
1444 if (state
->n_wronly
)
1445 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1446 if (state
->n_rdonly
)
1447 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1449 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1450 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1453 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1454 nfs4_stateid
*arg_stateid
,
1455 nfs4_stateid
*stateid
, fmode_t fmode
)
1457 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1458 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1460 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1463 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1466 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1467 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1468 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1470 if (stateid
== NULL
)
1472 /* Handle races with OPEN */
1473 if (!nfs4_stateid_match_other(arg_stateid
, &state
->open_stateid
) ||
1474 (nfs4_stateid_match_other(stateid
, &state
->open_stateid
) &&
1475 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))) {
1476 nfs_resync_open_stateid_locked(state
);
1479 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1480 nfs4_stateid_copy(&state
->stateid
, stateid
);
1481 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1484 static void nfs_clear_open_stateid(struct nfs4_state
*state
,
1485 nfs4_stateid
*arg_stateid
,
1486 nfs4_stateid
*stateid
, fmode_t fmode
)
1488 write_seqlock(&state
->seqlock
);
1489 nfs_clear_open_stateid_locked(state
, arg_stateid
, stateid
, fmode
);
1490 write_sequnlock(&state
->seqlock
);
1491 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1492 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1495 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
,
1496 const nfs4_stateid
*stateid
, fmode_t fmode
,
1497 nfs4_stateid
*freeme
)
1501 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1504 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1506 case FMODE_READ
|FMODE_WRITE
:
1507 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1509 if (!nfs_need_update_open_stateid(state
, stateid
, freeme
))
1511 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1512 nfs4_stateid_copy(&state
->stateid
, stateid
);
1513 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1516 static void __update_open_stateid(struct nfs4_state
*state
,
1517 const nfs4_stateid
*open_stateid
,
1518 const nfs4_stateid
*deleg_stateid
,
1520 nfs4_stateid
*freeme
)
1523 * Protect the call to nfs4_state_set_mode_locked and
1524 * serialise the stateid update
1526 spin_lock(&state
->owner
->so_lock
);
1527 write_seqlock(&state
->seqlock
);
1528 if (deleg_stateid
!= NULL
) {
1529 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1530 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1532 if (open_stateid
!= NULL
)
1533 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
, freeme
);
1534 write_sequnlock(&state
->seqlock
);
1535 update_open_stateflags(state
, fmode
);
1536 spin_unlock(&state
->owner
->so_lock
);
1539 static int update_open_stateid(struct nfs4_state
*state
,
1540 const nfs4_stateid
*open_stateid
,
1541 const nfs4_stateid
*delegation
,
1544 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1545 struct nfs_client
*clp
= server
->nfs_client
;
1546 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1547 struct nfs_delegation
*deleg_cur
;
1548 nfs4_stateid freeme
= { };
1551 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1554 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1555 if (deleg_cur
== NULL
)
1558 spin_lock(&deleg_cur
->lock
);
1559 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1560 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1561 (deleg_cur
->type
& fmode
) != fmode
)
1562 goto no_delegation_unlock
;
1564 if (delegation
== NULL
)
1565 delegation
= &deleg_cur
->stateid
;
1566 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1567 goto no_delegation_unlock
;
1569 nfs_mark_delegation_referenced(deleg_cur
);
1570 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
,
1573 no_delegation_unlock
:
1574 spin_unlock(&deleg_cur
->lock
);
1578 if (!ret
&& open_stateid
!= NULL
) {
1579 __update_open_stateid(state
, open_stateid
, NULL
, fmode
, &freeme
);
1582 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1583 nfs4_schedule_state_manager(clp
);
1584 if (freeme
.type
!= 0)
1585 nfs4_test_and_free_stateid(server
, &freeme
,
1586 state
->owner
->so_cred
);
1591 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1592 const nfs4_stateid
*stateid
)
1594 struct nfs4_state
*state
= lsp
->ls_state
;
1597 spin_lock(&state
->state_lock
);
1598 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1600 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1602 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1605 spin_unlock(&state
->state_lock
);
1609 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1611 struct nfs_delegation
*delegation
;
1614 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1615 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1620 nfs4_inode_return_delegation(inode
);
1623 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1625 struct nfs4_state
*state
= opendata
->state
;
1626 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1627 struct nfs_delegation
*delegation
;
1628 int open_mode
= opendata
->o_arg
.open_flags
;
1629 fmode_t fmode
= opendata
->o_arg
.fmode
;
1630 enum open_claim_type4 claim
= opendata
->o_arg
.claim
;
1631 nfs4_stateid stateid
;
1635 spin_lock(&state
->owner
->so_lock
);
1636 if (can_open_cached(state
, fmode
, open_mode
)) {
1637 update_open_stateflags(state
, fmode
);
1638 spin_unlock(&state
->owner
->so_lock
);
1639 goto out_return_state
;
1641 spin_unlock(&state
->owner
->so_lock
);
1643 delegation
= rcu_dereference(nfsi
->delegation
);
1644 if (!can_open_delegated(delegation
, fmode
, claim
)) {
1648 /* Save the delegation */
1649 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1651 nfs_release_seqid(opendata
->o_arg
.seqid
);
1652 if (!opendata
->is_recover
) {
1653 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1659 /* Try to update the stateid using the delegation */
1660 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1661 goto out_return_state
;
1664 return ERR_PTR(ret
);
1666 atomic_inc(&state
->count
);
1671 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1673 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1674 struct nfs_delegation
*delegation
;
1675 int delegation_flags
= 0;
1678 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1680 delegation_flags
= delegation
->flags
;
1682 switch (data
->o_arg
.claim
) {
1685 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1686 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1687 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1688 "returning a delegation for "
1689 "OPEN(CLAIM_DELEGATE_CUR)\n",
1693 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1694 nfs_inode_set_delegation(state
->inode
,
1695 data
->owner
->so_cred
,
1698 nfs_inode_reclaim_delegation(state
->inode
,
1699 data
->owner
->so_cred
,
1704 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1705 * and update the nfs4_state.
1707 static struct nfs4_state
*
1708 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1710 struct inode
*inode
= data
->state
->inode
;
1711 struct nfs4_state
*state
= data
->state
;
1714 if (!data
->rpc_done
) {
1715 if (data
->rpc_status
) {
1716 ret
= data
->rpc_status
;
1719 /* cached opens have already been processed */
1723 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1727 if (data
->o_res
.delegation_type
!= 0)
1728 nfs4_opendata_check_deleg(data
, state
);
1730 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1732 atomic_inc(&state
->count
);
1736 return ERR_PTR(ret
);
1740 static struct nfs4_state
*
1741 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1743 struct inode
*inode
;
1744 struct nfs4_state
*state
= NULL
;
1747 if (!data
->rpc_done
) {
1748 state
= nfs4_try_open_cached(data
);
1749 trace_nfs4_cached_open(data
->state
);
1754 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1756 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1757 ret
= PTR_ERR(inode
);
1761 state
= nfs4_get_open_state(inode
, data
->owner
);
1764 if (data
->o_res
.delegation_type
!= 0)
1765 nfs4_opendata_check_deleg(data
, state
);
1766 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1770 nfs_release_seqid(data
->o_arg
.seqid
);
1775 return ERR_PTR(ret
);
1778 static struct nfs4_state
*
1779 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1781 struct nfs4_state
*ret
;
1783 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1784 ret
=_nfs4_opendata_reclaim_to_nfs4_state(data
);
1786 ret
= _nfs4_opendata_to_nfs4_state(data
);
1787 nfs4_sequence_free_slot(&data
->o_res
.seq_res
);
1791 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1793 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1794 struct nfs_open_context
*ctx
;
1796 spin_lock(&state
->inode
->i_lock
);
1797 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1798 if (ctx
->state
!= state
)
1800 get_nfs_open_context(ctx
);
1801 spin_unlock(&state
->inode
->i_lock
);
1804 spin_unlock(&state
->inode
->i_lock
);
1805 return ERR_PTR(-ENOENT
);
1808 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1809 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1811 struct nfs4_opendata
*opendata
;
1813 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1814 NULL
, NULL
, claim
, GFP_NOFS
);
1815 if (opendata
== NULL
)
1816 return ERR_PTR(-ENOMEM
);
1817 opendata
->state
= state
;
1818 atomic_inc(&state
->count
);
1822 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
,
1825 struct nfs4_state
*newstate
;
1828 if (!nfs4_mode_match_open_stateid(opendata
->state
, fmode
))
1830 opendata
->o_arg
.open_flags
= 0;
1831 opendata
->o_arg
.fmode
= fmode
;
1832 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1833 NFS_SB(opendata
->dentry
->d_sb
),
1835 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1836 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1837 nfs4_init_opendata_res(opendata
);
1838 ret
= _nfs4_recover_proc_open(opendata
);
1841 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1842 if (IS_ERR(newstate
))
1843 return PTR_ERR(newstate
);
1844 if (newstate
!= opendata
->state
)
1846 nfs4_close_state(newstate
, fmode
);
1850 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1854 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1855 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1856 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1857 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1858 /* memory barrier prior to reading state->n_* */
1859 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1860 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1862 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1865 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1868 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1872 * We may have performed cached opens for all three recoveries.
1873 * Check if we need to update the current stateid.
1875 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1876 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1877 write_seqlock(&state
->seqlock
);
1878 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1879 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1880 write_sequnlock(&state
->seqlock
);
1887 * reclaim state on the server after a reboot.
1889 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1891 struct nfs_delegation
*delegation
;
1892 struct nfs4_opendata
*opendata
;
1893 fmode_t delegation_type
= 0;
1896 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1897 NFS4_OPEN_CLAIM_PREVIOUS
);
1898 if (IS_ERR(opendata
))
1899 return PTR_ERR(opendata
);
1901 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1902 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1903 delegation_type
= delegation
->type
;
1905 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1906 status
= nfs4_open_recover(opendata
, state
);
1907 nfs4_opendata_put(opendata
);
1911 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1913 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1914 struct nfs4_exception exception
= { };
1917 err
= _nfs4_do_open_reclaim(ctx
, state
);
1918 trace_nfs4_open_reclaim(ctx
, 0, err
);
1919 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1921 if (err
!= -NFS4ERR_DELAY
)
1923 nfs4_handle_exception(server
, err
, &exception
);
1924 } while (exception
.retry
);
1928 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1930 struct nfs_open_context
*ctx
;
1933 ctx
= nfs4_state_find_open_context(state
);
1936 ret
= nfs4_do_open_reclaim(ctx
, state
);
1937 put_nfs_open_context(ctx
);
1941 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1945 printk(KERN_ERR
"NFS: %s: unhandled error "
1946 "%d.\n", __func__
, err
);
1952 case -NFS4ERR_BADSESSION
:
1953 case -NFS4ERR_BADSLOT
:
1954 case -NFS4ERR_BAD_HIGH_SLOT
:
1955 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1956 case -NFS4ERR_DEADSESSION
:
1957 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1958 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1960 case -NFS4ERR_STALE_CLIENTID
:
1961 case -NFS4ERR_STALE_STATEID
:
1962 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1963 /* Don't recall a delegation if it was lost */
1964 nfs4_schedule_lease_recovery(server
->nfs_client
);
1966 case -NFS4ERR_MOVED
:
1967 nfs4_schedule_migration_recovery(server
);
1969 case -NFS4ERR_LEASE_MOVED
:
1970 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1972 case -NFS4ERR_DELEG_REVOKED
:
1973 case -NFS4ERR_ADMIN_REVOKED
:
1974 case -NFS4ERR_EXPIRED
:
1975 case -NFS4ERR_BAD_STATEID
:
1976 case -NFS4ERR_OPENMODE
:
1977 nfs_inode_find_state_and_recover(state
->inode
,
1979 nfs4_schedule_stateid_recovery(server
, state
);
1981 case -NFS4ERR_DELAY
:
1982 case -NFS4ERR_GRACE
:
1983 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1987 case -NFS4ERR_DENIED
:
1988 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1994 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
,
1995 struct nfs4_state
*state
, const nfs4_stateid
*stateid
,
1998 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1999 struct nfs4_opendata
*opendata
;
2002 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2003 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
2004 if (IS_ERR(opendata
))
2005 return PTR_ERR(opendata
);
2006 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
2007 write_seqlock(&state
->seqlock
);
2008 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2009 write_sequnlock(&state
->seqlock
);
2010 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2011 switch (type
& (FMODE_READ
|FMODE_WRITE
)) {
2012 case FMODE_READ
|FMODE_WRITE
:
2014 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
2017 err
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
2021 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
2023 nfs4_opendata_put(opendata
);
2024 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
2027 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
2029 struct nfs4_opendata
*data
= calldata
;
2031 nfs40_setup_sequence(data
->o_arg
.server
->nfs_client
->cl_slot_tbl
,
2032 &data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, task
);
2035 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
2037 struct nfs4_opendata
*data
= calldata
;
2039 nfs40_sequence_done(task
, &data
->c_res
.seq_res
);
2041 data
->rpc_status
= task
->tk_status
;
2042 if (data
->rpc_status
== 0) {
2043 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
2044 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2045 renew_lease(data
->o_res
.server
, data
->timestamp
);
2050 static void nfs4_open_confirm_release(void *calldata
)
2052 struct nfs4_opendata
*data
= calldata
;
2053 struct nfs4_state
*state
= NULL
;
2055 /* If this request hasn't been cancelled, do nothing */
2056 if (data
->cancelled
== 0)
2058 /* In case of error, no cleanup! */
2059 if (!data
->rpc_done
)
2061 state
= nfs4_opendata_to_nfs4_state(data
);
2063 nfs4_close_state(state
, data
->o_arg
.fmode
);
2065 nfs4_opendata_put(data
);
2068 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
2069 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
2070 .rpc_call_done
= nfs4_open_confirm_done
,
2071 .rpc_release
= nfs4_open_confirm_release
,
2075 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2077 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
2079 struct nfs_server
*server
= NFS_SERVER(d_inode(data
->dir
));
2080 struct rpc_task
*task
;
2081 struct rpc_message msg
= {
2082 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
2083 .rpc_argp
= &data
->c_arg
,
2084 .rpc_resp
= &data
->c_res
,
2085 .rpc_cred
= data
->owner
->so_cred
,
2087 struct rpc_task_setup task_setup_data
= {
2088 .rpc_client
= server
->client
,
2089 .rpc_message
= &msg
,
2090 .callback_ops
= &nfs4_open_confirm_ops
,
2091 .callback_data
= data
,
2092 .workqueue
= nfsiod_workqueue
,
2093 .flags
= RPC_TASK_ASYNC
,
2097 nfs4_init_sequence(&data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, 1);
2098 kref_get(&data
->kref
);
2100 data
->rpc_status
= 0;
2101 data
->timestamp
= jiffies
;
2102 if (data
->is_recover
)
2103 nfs4_set_sequence_privileged(&data
->c_arg
.seq_args
);
2104 task
= rpc_run_task(&task_setup_data
);
2106 return PTR_ERR(task
);
2107 status
= nfs4_wait_for_completion_rpc_task(task
);
2109 data
->cancelled
= 1;
2112 status
= data
->rpc_status
;
2117 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
2119 struct nfs4_opendata
*data
= calldata
;
2120 struct nfs4_state_owner
*sp
= data
->owner
;
2121 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
2122 enum open_claim_type4 claim
= data
->o_arg
.claim
;
2124 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
2127 * Check if we still need to send an OPEN call, or if we can use
2128 * a delegation instead.
2130 if (data
->state
!= NULL
) {
2131 struct nfs_delegation
*delegation
;
2133 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
2136 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
2137 if (can_open_delegated(delegation
, data
->o_arg
.fmode
, claim
))
2138 goto unlock_no_action
;
2141 /* Update client id. */
2142 data
->o_arg
.clientid
= clp
->cl_clientid
;
2146 case NFS4_OPEN_CLAIM_PREVIOUS
:
2147 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
2148 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
2149 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
2150 case NFS4_OPEN_CLAIM_FH
:
2151 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
2152 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
2154 data
->timestamp
= jiffies
;
2155 if (nfs4_setup_sequence(data
->o_arg
.server
,
2156 &data
->o_arg
.seq_args
,
2157 &data
->o_res
.seq_res
,
2159 nfs_release_seqid(data
->o_arg
.seqid
);
2161 /* Set the create mode (note dependency on the session type) */
2162 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
2163 if (data
->o_arg
.open_flags
& O_EXCL
) {
2164 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
2165 if (nfs4_has_persistent_session(clp
))
2166 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
2167 else if (clp
->cl_mvops
->minor_version
> 0)
2168 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
2172 trace_nfs4_cached_open(data
->state
);
2175 task
->tk_action
= NULL
;
2177 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
2180 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
2182 struct nfs4_opendata
*data
= calldata
;
2184 data
->rpc_status
= task
->tk_status
;
2186 if (!nfs4_sequence_process(task
, &data
->o_res
.seq_res
))
2189 if (task
->tk_status
== 0) {
2190 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
2191 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
2195 data
->rpc_status
= -ELOOP
;
2198 data
->rpc_status
= -EISDIR
;
2201 data
->rpc_status
= -ENOTDIR
;
2204 renew_lease(data
->o_res
.server
, data
->timestamp
);
2205 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
2206 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2211 static void nfs4_open_release(void *calldata
)
2213 struct nfs4_opendata
*data
= calldata
;
2214 struct nfs4_state
*state
= NULL
;
2216 /* If this request hasn't been cancelled, do nothing */
2217 if (data
->cancelled
== 0)
2219 /* In case of error, no cleanup! */
2220 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
2222 /* In case we need an open_confirm, no cleanup! */
2223 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2225 state
= nfs4_opendata_to_nfs4_state(data
);
2227 nfs4_close_state(state
, data
->o_arg
.fmode
);
2229 nfs4_opendata_put(data
);
2232 static const struct rpc_call_ops nfs4_open_ops
= {
2233 .rpc_call_prepare
= nfs4_open_prepare
,
2234 .rpc_call_done
= nfs4_open_done
,
2235 .rpc_release
= nfs4_open_release
,
2238 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
2240 struct inode
*dir
= d_inode(data
->dir
);
2241 struct nfs_server
*server
= NFS_SERVER(dir
);
2242 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2243 struct nfs_openres
*o_res
= &data
->o_res
;
2244 struct rpc_task
*task
;
2245 struct rpc_message msg
= {
2246 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
2249 .rpc_cred
= data
->owner
->so_cred
,
2251 struct rpc_task_setup task_setup_data
= {
2252 .rpc_client
= server
->client
,
2253 .rpc_message
= &msg
,
2254 .callback_ops
= &nfs4_open_ops
,
2255 .callback_data
= data
,
2256 .workqueue
= nfsiod_workqueue
,
2257 .flags
= RPC_TASK_ASYNC
,
2261 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
2262 kref_get(&data
->kref
);
2264 data
->rpc_status
= 0;
2265 data
->cancelled
= 0;
2266 data
->is_recover
= 0;
2268 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2269 data
->is_recover
= 1;
2271 task
= rpc_run_task(&task_setup_data
);
2273 return PTR_ERR(task
);
2274 status
= nfs4_wait_for_completion_rpc_task(task
);
2276 data
->cancelled
= 1;
2279 status
= data
->rpc_status
;
2285 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
2287 struct inode
*dir
= d_inode(data
->dir
);
2288 struct nfs_openres
*o_res
= &data
->o_res
;
2291 status
= nfs4_run_open_task(data
, 1);
2292 if (status
!= 0 || !data
->rpc_done
)
2295 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
2297 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2298 status
= _nfs4_proc_open_confirm(data
);
2307 * Additional permission checks in order to distinguish between an
2308 * open for read, and an open for execute. This works around the
2309 * fact that NFSv4 OPEN treats read and execute permissions as being
2311 * Note that in the non-execute case, we want to turn off permission
2312 * checking if we just created a new file (POSIX open() semantics).
2314 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2315 struct nfs4_opendata
*opendata
,
2316 struct nfs4_state
*state
, fmode_t fmode
,
2319 struct nfs_access_entry cache
;
2322 /* access call failed or for some reason the server doesn't
2323 * support any access modes -- defer access call until later */
2324 if (opendata
->o_res
.access_supported
== 0)
2329 * Use openflags to check for exec, because fmode won't
2330 * always have FMODE_EXEC set when file open for exec.
2332 if (openflags
& __FMODE_EXEC
) {
2333 /* ONLY check for exec rights */
2335 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2339 cache
.jiffies
= jiffies
;
2340 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2341 nfs_access_add_cache(state
->inode
, &cache
);
2343 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
2346 /* even though OPEN succeeded, access is denied. Close the file */
2347 nfs4_close_state(state
, fmode
);
2352 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2354 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2356 struct inode
*dir
= d_inode(data
->dir
);
2357 struct nfs_server
*server
= NFS_SERVER(dir
);
2358 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2359 struct nfs_openres
*o_res
= &data
->o_res
;
2362 status
= nfs4_run_open_task(data
, 0);
2363 if (!data
->rpc_done
)
2366 if (status
== -NFS4ERR_BADNAME
&&
2367 !(o_arg
->open_flags
& O_CREAT
))
2372 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2374 if (o_arg
->open_flags
& O_CREAT
) {
2375 update_changeattr(dir
, &o_res
->cinfo
);
2376 if (o_arg
->open_flags
& O_EXCL
)
2377 data
->file_created
= 1;
2378 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2379 data
->file_created
= 1;
2381 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2382 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2383 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2384 status
= _nfs4_proc_open_confirm(data
);
2388 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
2389 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2393 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
2395 return nfs4_client_recover_expired_lease(server
->nfs_client
);
2400 * reclaim state on the server after a network partition.
2401 * Assumes caller holds the appropriate lock
2403 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2405 struct nfs4_opendata
*opendata
;
2408 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2409 NFS4_OPEN_CLAIM_FH
);
2410 if (IS_ERR(opendata
))
2411 return PTR_ERR(opendata
);
2412 ret
= nfs4_open_recover(opendata
, state
);
2414 d_drop(ctx
->dentry
);
2415 nfs4_opendata_put(opendata
);
2419 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2421 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2422 struct nfs4_exception exception
= { };
2426 err
= _nfs4_open_expired(ctx
, state
);
2427 trace_nfs4_open_expired(ctx
, 0, err
);
2428 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2433 case -NFS4ERR_GRACE
:
2434 case -NFS4ERR_DELAY
:
2435 nfs4_handle_exception(server
, err
, &exception
);
2438 } while (exception
.retry
);
2443 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2445 struct nfs_open_context
*ctx
;
2448 ctx
= nfs4_state_find_open_context(state
);
2451 ret
= nfs4_do_open_expired(ctx
, state
);
2452 put_nfs_open_context(ctx
);
2456 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
,
2457 const nfs4_stateid
*stateid
)
2459 nfs_remove_bad_delegation(state
->inode
, stateid
);
2460 write_seqlock(&state
->seqlock
);
2461 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2462 write_sequnlock(&state
->seqlock
);
2463 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2466 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2468 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2469 nfs_finish_clear_delegation_stateid(state
, NULL
);
2472 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2474 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2475 nfs40_clear_delegation_stateid(state
);
2476 return nfs4_open_expired(sp
, state
);
2479 static int nfs40_test_and_free_expired_stateid(struct nfs_server
*server
,
2480 nfs4_stateid
*stateid
,
2481 struct rpc_cred
*cred
)
2483 return -NFS4ERR_BAD_STATEID
;
2486 #if defined(CONFIG_NFS_V4_1)
2487 static int nfs41_test_and_free_expired_stateid(struct nfs_server
*server
,
2488 nfs4_stateid
*stateid
,
2489 struct rpc_cred
*cred
)
2493 switch (stateid
->type
) {
2496 case NFS4_INVALID_STATEID_TYPE
:
2497 case NFS4_SPECIAL_STATEID_TYPE
:
2498 return -NFS4ERR_BAD_STATEID
;
2499 case NFS4_REVOKED_STATEID_TYPE
:
2503 status
= nfs41_test_stateid(server
, stateid
, cred
);
2505 case -NFS4ERR_EXPIRED
:
2506 case -NFS4ERR_ADMIN_REVOKED
:
2507 case -NFS4ERR_DELEG_REVOKED
:
2513 /* Ack the revoked state to the server */
2514 nfs41_free_stateid(server
, stateid
, cred
, true);
2515 return -NFS4ERR_EXPIRED
;
2518 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2520 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2521 nfs4_stateid stateid
;
2522 struct nfs_delegation
*delegation
;
2523 struct rpc_cred
*cred
;
2526 /* Get the delegation credential for use by test/free_stateid */
2528 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2529 if (delegation
== NULL
) {
2534 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2535 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
2537 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2541 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
)) {
2546 cred
= get_rpccred(delegation
->cred
);
2548 status
= nfs41_test_and_free_expired_stateid(server
, &stateid
, cred
);
2549 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2550 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
2551 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2557 * nfs41_check_expired_locks - possibly free a lock stateid
2559 * @state: NFSv4 state for an inode
2561 * Returns NFS_OK if recovery for this stateid is now finished.
2562 * Otherwise a negative NFS4ERR value is returned.
2564 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
2566 int status
, ret
= NFS_OK
;
2567 struct nfs4_lock_state
*lsp
;
2568 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2570 if (!test_bit(LK_STATE_IN_USE
, &state
->flags
))
2572 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
2573 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
2574 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
2576 status
= nfs41_test_and_free_expired_stateid(server
,
2579 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
2580 if (status
== -NFS4ERR_EXPIRED
||
2581 status
== -NFS4ERR_BAD_STATEID
) {
2582 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
2583 lsp
->ls_stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
2584 if (!recover_lost_locks
)
2585 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2586 } else if (status
!= NFS_OK
) {
2597 * nfs41_check_open_stateid - possibly free an open stateid
2599 * @state: NFSv4 state for an inode
2601 * Returns NFS_OK if recovery for this stateid is now finished.
2602 * Otherwise a negative NFS4ERR value is returned.
2604 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2606 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2607 nfs4_stateid
*stateid
= &state
->open_stateid
;
2608 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2611 if (test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0) {
2612 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0) {
2613 if (nfs4_have_delegation(state
->inode
, state
->state
))
2615 return -NFS4ERR_OPENMODE
;
2617 return -NFS4ERR_BAD_STATEID
;
2619 status
= nfs41_test_and_free_expired_stateid(server
, stateid
, cred
);
2620 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2621 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
) {
2622 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2623 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2624 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2625 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2626 stateid
->type
= NFS4_INVALID_STATEID_TYPE
;
2628 if (status
!= NFS_OK
)
2630 if (nfs_open_stateid_recover_openmode(state
))
2631 return -NFS4ERR_OPENMODE
;
2635 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2639 nfs41_check_delegation_stateid(state
);
2640 status
= nfs41_check_expired_locks(state
);
2641 if (status
!= NFS_OK
)
2643 status
= nfs41_check_open_stateid(state
);
2644 if (status
!= NFS_OK
)
2645 status
= nfs4_open_expired(sp
, state
);
2651 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2652 * fields corresponding to attributes that were used to store the verifier.
2653 * Make sure we clobber those fields in the later setattr call
2655 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2656 struct iattr
*sattr
, struct nfs4_label
**label
)
2658 const u32
*attrset
= opendata
->o_res
.attrset
;
2660 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2661 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2662 sattr
->ia_valid
|= ATTR_ATIME
;
2664 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2665 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2666 sattr
->ia_valid
|= ATTR_MTIME
;
2668 /* Except MODE, it seems harmless of setting twice. */
2669 if ((attrset
[1] & FATTR4_WORD1_MODE
))
2670 sattr
->ia_valid
&= ~ATTR_MODE
;
2672 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2676 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2679 struct nfs_open_context
*ctx
)
2681 struct nfs4_state_owner
*sp
= opendata
->owner
;
2682 struct nfs_server
*server
= sp
->so_server
;
2683 struct dentry
*dentry
;
2684 struct nfs4_state
*state
;
2688 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2690 ret
= _nfs4_proc_open(opendata
);
2694 state
= nfs4_opendata_to_nfs4_state(opendata
);
2695 ret
= PTR_ERR(state
);
2698 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2699 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2700 if (opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
)
2701 set_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
);
2703 dentry
= opendata
->dentry
;
2704 if (d_really_is_negative(dentry
)) {
2705 struct dentry
*alias
;
2707 alias
= d_exact_alias(dentry
, state
->inode
);
2709 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2710 /* d_splice_alias() can't fail here - it's a non-directory */
2713 ctx
->dentry
= dentry
= alias
;
2715 nfs_set_verifier(dentry
,
2716 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2719 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2724 if (d_inode(dentry
) == state
->inode
) {
2725 nfs_inode_attach_open_context(ctx
);
2726 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2727 nfs4_schedule_stateid_recovery(server
, state
);
2734 * Returns a referenced nfs4_state
2736 static int _nfs4_do_open(struct inode
*dir
,
2737 struct nfs_open_context
*ctx
,
2739 struct iattr
*sattr
,
2740 struct nfs4_label
*label
,
2743 struct nfs4_state_owner
*sp
;
2744 struct nfs4_state
*state
= NULL
;
2745 struct nfs_server
*server
= NFS_SERVER(dir
);
2746 struct nfs4_opendata
*opendata
;
2747 struct dentry
*dentry
= ctx
->dentry
;
2748 struct rpc_cred
*cred
= ctx
->cred
;
2749 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2750 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2751 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2752 struct nfs4_label
*olabel
= NULL
;
2755 /* Protect against reboot recovery conflicts */
2757 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2759 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2762 status
= nfs4_recover_expired_lease(server
);
2764 goto err_put_state_owner
;
2765 if (d_really_is_positive(dentry
))
2766 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2768 if (d_really_is_positive(dentry
))
2769 claim
= NFS4_OPEN_CLAIM_FH
;
2770 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2771 label
, claim
, GFP_KERNEL
);
2772 if (opendata
== NULL
)
2773 goto err_put_state_owner
;
2776 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2777 if (IS_ERR(olabel
)) {
2778 status
= PTR_ERR(olabel
);
2779 goto err_opendata_put
;
2783 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2784 if (!opendata
->f_attr
.mdsthreshold
) {
2785 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2786 if (!opendata
->f_attr
.mdsthreshold
)
2787 goto err_free_label
;
2789 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2791 if (d_really_is_positive(dentry
))
2792 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2794 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2796 goto err_free_label
;
2799 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2800 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2801 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2803 * send create attributes which was not set by open
2804 * with an extra setattr.
2806 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2807 nfs_fattr_init(opendata
->o_res
.f_attr
);
2808 status
= nfs4_do_setattr(state
->inode
, cred
,
2809 opendata
->o_res
.f_attr
, sattr
,
2810 state
, label
, olabel
);
2812 nfs_setattr_update_inode(state
->inode
, sattr
,
2813 opendata
->o_res
.f_attr
);
2814 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2818 if (opened
&& opendata
->file_created
)
2819 *opened
|= FILE_CREATED
;
2821 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2822 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2823 opendata
->f_attr
.mdsthreshold
= NULL
;
2826 nfs4_label_free(olabel
);
2828 nfs4_opendata_put(opendata
);
2829 nfs4_put_state_owner(sp
);
2832 nfs4_label_free(olabel
);
2834 nfs4_opendata_put(opendata
);
2835 err_put_state_owner
:
2836 nfs4_put_state_owner(sp
);
2842 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2843 struct nfs_open_context
*ctx
,
2845 struct iattr
*sattr
,
2846 struct nfs4_label
*label
,
2849 struct nfs_server
*server
= NFS_SERVER(dir
);
2850 struct nfs4_exception exception
= { };
2851 struct nfs4_state
*res
;
2855 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2857 trace_nfs4_open_file(ctx
, flags
, status
);
2860 /* NOTE: BAD_SEQID means the server and client disagree about the
2861 * book-keeping w.r.t. state-changing operations
2862 * (OPEN/CLOSE/LOCK/LOCKU...)
2863 * It is actually a sign of a bug on the client or on the server.
2865 * If we receive a BAD_SEQID error in the particular case of
2866 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2867 * have unhashed the old state_owner for us, and that we can
2868 * therefore safely retry using a new one. We should still warn
2869 * the user though...
2871 if (status
== -NFS4ERR_BAD_SEQID
) {
2872 pr_warn_ratelimited("NFS: v4 server %s "
2873 " returned a bad sequence-id error!\n",
2874 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2875 exception
.retry
= 1;
2879 * BAD_STATEID on OPEN means that the server cancelled our
2880 * state before it received the OPEN_CONFIRM.
2881 * Recover by retrying the request as per the discussion
2882 * on Page 181 of RFC3530.
2884 if (status
== -NFS4ERR_BAD_STATEID
) {
2885 exception
.retry
= 1;
2888 if (status
== -EAGAIN
) {
2889 /* We must have found a delegation */
2890 exception
.retry
= 1;
2893 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2895 res
= ERR_PTR(nfs4_handle_exception(server
,
2896 status
, &exception
));
2897 } while (exception
.retry
);
2901 static int _nfs4_do_setattr(struct inode
*inode
,
2902 struct nfs_setattrargs
*arg
,
2903 struct nfs_setattrres
*res
,
2904 struct rpc_cred
*cred
,
2905 struct nfs4_state
*state
)
2907 struct nfs_server
*server
= NFS_SERVER(inode
);
2908 struct rpc_message msg
= {
2909 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2914 struct rpc_cred
*delegation_cred
= NULL
;
2915 unsigned long timestamp
= jiffies
;
2920 nfs_fattr_init(res
->fattr
);
2922 /* Servers should only apply open mode checks for file size changes */
2923 truncate
= (arg
->iap
->ia_valid
& ATTR_SIZE
) ? true : false;
2924 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2926 if (nfs4_copy_delegation_stateid(inode
, fmode
, &arg
->stateid
, &delegation_cred
)) {
2927 /* Use that stateid */
2928 } else if (truncate
&& state
!= NULL
) {
2929 struct nfs_lockowner lockowner
= {
2930 .l_owner
= current
->files
,
2931 .l_pid
= current
->tgid
,
2933 if (!nfs4_valid_open_stateid(state
))
2935 if (nfs4_select_rw_stateid(state
, FMODE_WRITE
, &lockowner
,
2936 &arg
->stateid
, &delegation_cred
) == -EIO
)
2939 nfs4_stateid_copy(&arg
->stateid
, &zero_stateid
);
2940 if (delegation_cred
)
2941 msg
.rpc_cred
= delegation_cred
;
2943 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
->seq_args
, &res
->seq_res
, 1);
2945 put_rpccred(delegation_cred
);
2946 if (status
== 0 && state
!= NULL
)
2947 renew_lease(server
, timestamp
);
2948 trace_nfs4_setattr(inode
, &arg
->stateid
, status
);
2952 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2953 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2954 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2955 struct nfs4_label
*olabel
)
2957 struct nfs_server
*server
= NFS_SERVER(inode
);
2958 struct nfs_setattrargs arg
= {
2959 .fh
= NFS_FH(inode
),
2962 .bitmask
= server
->attr_bitmask
,
2965 struct nfs_setattrres res
= {
2970 struct nfs4_exception exception
= {
2973 .stateid
= &arg
.stateid
,
2977 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2979 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2982 err
= _nfs4_do_setattr(inode
, &arg
, &res
, cred
, state
);
2984 case -NFS4ERR_OPENMODE
:
2985 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2986 pr_warn_once("NFSv4: server %s is incorrectly "
2987 "applying open mode checks to "
2988 "a SETATTR that is not "
2989 "changing file size.\n",
2990 server
->nfs_client
->cl_hostname
);
2992 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2994 if (sattr
->ia_valid
& ATTR_OPEN
)
2999 err
= nfs4_handle_exception(server
, err
, &exception
);
3000 } while (exception
.retry
);
3006 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
3008 if (inode
== NULL
|| !nfs_have_layout(inode
))
3011 return pnfs_wait_on_layoutreturn(inode
, task
);
3014 struct nfs4_closedata
{
3015 struct inode
*inode
;
3016 struct nfs4_state
*state
;
3017 struct nfs_closeargs arg
;
3018 struct nfs_closeres res
;
3019 struct nfs_fattr fattr
;
3020 unsigned long timestamp
;
3025 static void nfs4_free_closedata(void *data
)
3027 struct nfs4_closedata
*calldata
= data
;
3028 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
3029 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
3032 pnfs_roc_release(calldata
->state
->inode
);
3033 nfs4_put_open_state(calldata
->state
);
3034 nfs_free_seqid(calldata
->arg
.seqid
);
3035 nfs4_put_state_owner(sp
);
3036 nfs_sb_deactive(sb
);
3040 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
3042 struct nfs4_closedata
*calldata
= data
;
3043 struct nfs4_state
*state
= calldata
->state
;
3044 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
3045 nfs4_stateid
*res_stateid
= NULL
;
3047 dprintk("%s: begin!\n", __func__
);
3048 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
3050 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
3051 /* hmm. we are done with the inode, and in the process of freeing
3052 * the state_owner. we keep this around to process errors
3054 switch (task
->tk_status
) {
3056 res_stateid
= &calldata
->res
.stateid
;
3058 pnfs_roc_set_barrier(state
->inode
,
3059 calldata
->roc_barrier
);
3060 renew_lease(server
, calldata
->timestamp
);
3062 case -NFS4ERR_ADMIN_REVOKED
:
3063 case -NFS4ERR_STALE_STATEID
:
3064 case -NFS4ERR_EXPIRED
:
3065 nfs4_free_revoked_stateid(server
,
3066 &calldata
->arg
.stateid
,
3067 task
->tk_msg
.rpc_cred
);
3068 case -NFS4ERR_OLD_STATEID
:
3069 case -NFS4ERR_BAD_STATEID
:
3070 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
3071 &state
->open_stateid
)) {
3072 rpc_restart_call_prepare(task
);
3075 if (calldata
->arg
.fmode
== 0)
3078 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
3079 rpc_restart_call_prepare(task
);
3083 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
3084 res_stateid
, calldata
->arg
.fmode
);
3086 nfs_release_seqid(calldata
->arg
.seqid
);
3087 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
3088 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
3091 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
3093 struct nfs4_closedata
*calldata
= data
;
3094 struct nfs4_state
*state
= calldata
->state
;
3095 struct inode
*inode
= calldata
->inode
;
3096 bool is_rdonly
, is_wronly
, is_rdwr
;
3099 dprintk("%s: begin!\n", __func__
);
3100 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3103 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
3104 spin_lock(&state
->owner
->so_lock
);
3105 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
3106 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
3107 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
3108 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
3109 /* Calculate the change in open mode */
3110 calldata
->arg
.fmode
= 0;
3111 if (state
->n_rdwr
== 0) {
3112 if (state
->n_rdonly
== 0)
3113 call_close
|= is_rdonly
;
3115 calldata
->arg
.fmode
|= FMODE_READ
;
3116 if (state
->n_wronly
== 0)
3117 call_close
|= is_wronly
;
3119 calldata
->arg
.fmode
|= FMODE_WRITE
;
3120 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
3121 call_close
|= is_rdwr
;
3123 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
3125 if (!nfs4_valid_open_stateid(state
))
3127 spin_unlock(&state
->owner
->so_lock
);
3130 /* Note: exit _without_ calling nfs4_close_done */
3134 if (nfs4_wait_on_layoutreturn(inode
, task
)) {
3135 nfs_release_seqid(calldata
->arg
.seqid
);
3139 if (calldata
->arg
.fmode
== 0)
3140 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
3142 pnfs_roc_get_barrier(inode
, &calldata
->roc_barrier
);
3144 calldata
->arg
.share_access
=
3145 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
3146 calldata
->arg
.fmode
, 0);
3148 nfs_fattr_init(calldata
->res
.fattr
);
3149 calldata
->timestamp
= jiffies
;
3150 if (nfs4_setup_sequence(NFS_SERVER(inode
),
3151 &calldata
->arg
.seq_args
,
3152 &calldata
->res
.seq_res
,
3154 nfs_release_seqid(calldata
->arg
.seqid
);
3155 dprintk("%s: done!\n", __func__
);
3158 task
->tk_action
= NULL
;
3160 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
3163 static const struct rpc_call_ops nfs4_close_ops
= {
3164 .rpc_call_prepare
= nfs4_close_prepare
,
3165 .rpc_call_done
= nfs4_close_done
,
3166 .rpc_release
= nfs4_free_closedata
,
3169 static bool nfs4_roc(struct inode
*inode
)
3171 if (!nfs_have_layout(inode
))
3173 return pnfs_roc(inode
);
3177 * It is possible for data to be read/written from a mem-mapped file
3178 * after the sys_close call (which hits the vfs layer as a flush).
3179 * This means that we can't safely call nfsv4 close on a file until
3180 * the inode is cleared. This in turn means that we are not good
3181 * NFSv4 citizens - we do not indicate to the server to update the file's
3182 * share state even when we are done with one of the three share
3183 * stateid's in the inode.
3185 * NOTE: Caller must be holding the sp->so_owner semaphore!
3187 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
3189 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3190 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
3191 struct nfs4_closedata
*calldata
;
3192 struct nfs4_state_owner
*sp
= state
->owner
;
3193 struct rpc_task
*task
;
3194 struct rpc_message msg
= {
3195 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
3196 .rpc_cred
= state
->owner
->so_cred
,
3198 struct rpc_task_setup task_setup_data
= {
3199 .rpc_client
= server
->client
,
3200 .rpc_message
= &msg
,
3201 .callback_ops
= &nfs4_close_ops
,
3202 .workqueue
= nfsiod_workqueue
,
3203 .flags
= RPC_TASK_ASYNC
,
3205 int status
= -ENOMEM
;
3207 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
3208 &task_setup_data
.rpc_client
, &msg
);
3210 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
3211 if (calldata
== NULL
)
3213 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
3214 calldata
->inode
= state
->inode
;
3215 calldata
->state
= state
;
3216 calldata
->arg
.fh
= NFS_FH(state
->inode
);
3217 /* Serialization for the sequence id */
3218 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
3219 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
3220 if (IS_ERR(calldata
->arg
.seqid
))
3221 goto out_free_calldata
;
3222 calldata
->arg
.fmode
= 0;
3223 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
3224 calldata
->res
.fattr
= &calldata
->fattr
;
3225 calldata
->res
.seqid
= calldata
->arg
.seqid
;
3226 calldata
->res
.server
= server
;
3227 calldata
->roc
= nfs4_roc(state
->inode
);
3228 nfs_sb_active(calldata
->inode
->i_sb
);
3230 msg
.rpc_argp
= &calldata
->arg
;
3231 msg
.rpc_resp
= &calldata
->res
;
3232 task_setup_data
.callback_data
= calldata
;
3233 task
= rpc_run_task(&task_setup_data
);
3235 return PTR_ERR(task
);
3238 status
= rpc_wait_for_completion_task(task
);
3244 nfs4_put_open_state(state
);
3245 nfs4_put_state_owner(sp
);
3249 static struct inode
*
3250 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3251 int open_flags
, struct iattr
*attr
, int *opened
)
3253 struct nfs4_state
*state
;
3254 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3256 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3258 /* Protect against concurrent sillydeletes */
3259 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3261 nfs4_label_release_security(label
);
3264 return ERR_CAST(state
);
3265 return state
->inode
;
3268 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3270 if (ctx
->state
== NULL
)
3273 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3275 nfs4_close_state(ctx
->state
, ctx
->mode
);
3278 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3279 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3280 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3282 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3284 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3285 struct nfs4_server_caps_arg args
= {
3289 struct nfs4_server_caps_res res
= {};
3290 struct rpc_message msg
= {
3291 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3297 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3298 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3299 FATTR4_WORD0_LINK_SUPPORT
|
3300 FATTR4_WORD0_SYMLINK_SUPPORT
|
3301 FATTR4_WORD0_ACLSUPPORT
;
3303 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3305 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3307 /* Sanity check the server answers */
3308 switch (minorversion
) {
3310 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3311 res
.attr_bitmask
[2] = 0;
3314 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3317 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3319 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3320 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3321 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3322 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3323 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3324 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3325 NFS_CAP_SECURITY_LABEL
);
3326 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3327 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3328 server
->caps
|= NFS_CAP_ACLS
;
3329 if (res
.has_links
!= 0)
3330 server
->caps
|= NFS_CAP_HARDLINKS
;
3331 if (res
.has_symlinks
!= 0)
3332 server
->caps
|= NFS_CAP_SYMLINKS
;
3333 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3334 server
->caps
|= NFS_CAP_FILEID
;
3335 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3336 server
->caps
|= NFS_CAP_MODE
;
3337 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3338 server
->caps
|= NFS_CAP_NLINK
;
3339 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3340 server
->caps
|= NFS_CAP_OWNER
;
3341 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3342 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3343 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3344 server
->caps
|= NFS_CAP_ATIME
;
3345 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3346 server
->caps
|= NFS_CAP_CTIME
;
3347 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3348 server
->caps
|= NFS_CAP_MTIME
;
3349 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3350 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3351 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3353 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3354 sizeof(server
->attr_bitmask
));
3355 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3357 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3358 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3359 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3360 server
->cache_consistency_bitmask
[2] = 0;
3361 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3362 sizeof(server
->exclcreat_bitmask
));
3363 server
->acl_bitmask
= res
.acl_bitmask
;
3364 server
->fh_expire_type
= res
.fh_expire_type
;
3370 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3372 struct nfs4_exception exception
= { };
3375 err
= nfs4_handle_exception(server
,
3376 _nfs4_server_capabilities(server
, fhandle
),
3378 } while (exception
.retry
);
3382 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3383 struct nfs_fsinfo
*info
)
3386 struct nfs4_lookup_root_arg args
= {
3389 struct nfs4_lookup_res res
= {
3391 .fattr
= info
->fattr
,
3394 struct rpc_message msg
= {
3395 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3400 bitmask
[0] = nfs4_fattr_bitmap
[0];
3401 bitmask
[1] = nfs4_fattr_bitmap
[1];
3403 * Process the label in the upcoming getfattr
3405 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3407 nfs_fattr_init(info
->fattr
);
3408 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3411 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3412 struct nfs_fsinfo
*info
)
3414 struct nfs4_exception exception
= { };
3417 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3418 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3421 case -NFS4ERR_WRONGSEC
:
3424 err
= nfs4_handle_exception(server
, err
, &exception
);
3426 } while (exception
.retry
);
3431 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3432 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3434 struct rpc_auth_create_args auth_args
= {
3435 .pseudoflavor
= flavor
,
3437 struct rpc_auth
*auth
;
3440 auth
= rpcauth_create(&auth_args
, server
->client
);
3445 ret
= nfs4_lookup_root(server
, fhandle
, info
);
3451 * Retry pseudoroot lookup with various security flavors. We do this when:
3453 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3454 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3456 * Returns zero on success, or a negative NFS4ERR value, or a
3457 * negative errno value.
3459 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3460 struct nfs_fsinfo
*info
)
3462 /* Per 3530bis 15.33.5 */
3463 static const rpc_authflavor_t flav_array
[] = {
3467 RPC_AUTH_UNIX
, /* courtesy */
3470 int status
= -EPERM
;
3473 if (server
->auth_info
.flavor_len
> 0) {
3474 /* try each flavor specified by user */
3475 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3476 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3477 server
->auth_info
.flavors
[i
]);
3478 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3483 /* no flavors specified by user, try default list */
3484 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3485 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3487 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3494 * -EACCESS could mean that the user doesn't have correct permissions
3495 * to access the mount. It could also mean that we tried to mount
3496 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3497 * existing mount programs don't handle -EACCES very well so it should
3498 * be mapped to -EPERM instead.
3500 if (status
== -EACCES
)
3506 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3507 * @server: initialized nfs_server handle
3508 * @fhandle: we fill in the pseudo-fs root file handle
3509 * @info: we fill in an FSINFO struct
3510 * @auth_probe: probe the auth flavours
3512 * Returns zero on success, or a negative errno.
3514 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3515 struct nfs_fsinfo
*info
,
3521 status
= nfs4_lookup_root(server
, fhandle
, info
);
3523 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3524 status
= server
->nfs_client
->cl_mvops
->find_root_sec(server
,
3528 status
= nfs4_server_capabilities(server
, fhandle
);
3530 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3532 return nfs4_map_errors(status
);
3535 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3536 struct nfs_fsinfo
*info
)
3539 struct nfs_fattr
*fattr
= info
->fattr
;
3540 struct nfs4_label
*label
= NULL
;
3542 error
= nfs4_server_capabilities(server
, mntfh
);
3544 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3548 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3550 return PTR_ERR(label
);
3552 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3554 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3555 goto err_free_label
;
3558 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3559 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3560 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3563 nfs4_label_free(label
);
3569 * Get locations and (maybe) other attributes of a referral.
3570 * Note that we'll actually follow the referral later when
3571 * we detect fsid mismatch in inode revalidation
3573 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3574 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3575 struct nfs_fh
*fhandle
)
3577 int status
= -ENOMEM
;
3578 struct page
*page
= NULL
;
3579 struct nfs4_fs_locations
*locations
= NULL
;
3581 page
= alloc_page(GFP_KERNEL
);
3584 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3585 if (locations
== NULL
)
3588 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3593 * If the fsid didn't change, this is a migration event, not a
3594 * referral. Cause us to drop into the exception handler, which
3595 * will kick off migration recovery.
3597 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3598 dprintk("%s: server did not return a different fsid for"
3599 " a referral at %s\n", __func__
, name
->name
);
3600 status
= -NFS4ERR_MOVED
;
3603 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3604 nfs_fixup_referral_attributes(&locations
->fattr
);
3606 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3607 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3608 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3616 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3617 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3619 struct nfs4_getattr_arg args
= {
3621 .bitmask
= server
->attr_bitmask
,
3623 struct nfs4_getattr_res res
= {
3628 struct rpc_message msg
= {
3629 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3634 args
.bitmask
= nfs4_bitmask(server
, label
);
3636 nfs_fattr_init(fattr
);
3637 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3640 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3641 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3643 struct nfs4_exception exception
= { };
3646 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3647 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3648 err
= nfs4_handle_exception(server
, err
,
3650 } while (exception
.retry
);
3655 * The file is not closed if it is opened due to the a request to change
3656 * the size of the file. The open call will not be needed once the
3657 * VFS layer lookup-intents are implemented.
3659 * Close is called when the inode is destroyed.
3660 * If we haven't opened the file for O_WRONLY, we
3661 * need to in the size_change case to obtain a stateid.
3664 * Because OPEN is always done by name in nfsv4, it is
3665 * possible that we opened a different file by the same
3666 * name. We can recognize this race condition, but we
3667 * can't do anything about it besides returning an error.
3669 * This will be fixed with VFS changes (lookup-intent).
3672 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3673 struct iattr
*sattr
)
3675 struct inode
*inode
= d_inode(dentry
);
3676 struct rpc_cred
*cred
= NULL
;
3677 struct nfs4_state
*state
= NULL
;
3678 struct nfs4_label
*label
= NULL
;
3681 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3682 sattr
->ia_valid
& ATTR_SIZE
&&
3683 sattr
->ia_size
< i_size_read(inode
))
3684 pnfs_commit_and_return_layout(inode
);
3686 nfs_fattr_init(fattr
);
3688 /* Deal with open(O_TRUNC) */
3689 if (sattr
->ia_valid
& ATTR_OPEN
)
3690 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3692 /* Optimization: if the end result is no change, don't RPC */
3693 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3696 /* Search for an existing open(O_WRITE) file */
3697 if (sattr
->ia_valid
& ATTR_FILE
) {
3698 struct nfs_open_context
*ctx
;
3700 ctx
= nfs_file_open_context(sattr
->ia_file
);
3707 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3709 return PTR_ERR(label
);
3711 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
3713 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3714 nfs_setsecurity(inode
, fattr
, label
);
3716 nfs4_label_free(label
);
3720 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3721 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3722 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3724 struct nfs_server
*server
= NFS_SERVER(dir
);
3726 struct nfs4_lookup_arg args
= {
3727 .bitmask
= server
->attr_bitmask
,
3728 .dir_fh
= NFS_FH(dir
),
3731 struct nfs4_lookup_res res
= {
3737 struct rpc_message msg
= {
3738 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3743 args
.bitmask
= nfs4_bitmask(server
, label
);
3745 nfs_fattr_init(fattr
);
3747 dprintk("NFS call lookup %s\n", name
->name
);
3748 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3749 dprintk("NFS reply lookup: %d\n", status
);
3753 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3755 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3756 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3757 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3761 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3762 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3763 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3765 struct nfs4_exception exception
= { };
3766 struct rpc_clnt
*client
= *clnt
;
3769 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3770 trace_nfs4_lookup(dir
, name
, err
);
3772 case -NFS4ERR_BADNAME
:
3775 case -NFS4ERR_MOVED
:
3776 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3777 if (err
== -NFS4ERR_MOVED
)
3778 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3780 case -NFS4ERR_WRONGSEC
:
3782 if (client
!= *clnt
)
3784 client
= nfs4_negotiate_security(client
, dir
, name
);
3786 return PTR_ERR(client
);
3788 exception
.retry
= 1;
3791 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3793 } while (exception
.retry
);
3798 else if (client
!= *clnt
)
3799 rpc_shutdown_client(client
);
3804 static int nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
3805 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3806 struct nfs4_label
*label
)
3809 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3811 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3812 if (client
!= NFS_CLIENT(dir
)) {
3813 rpc_shutdown_client(client
);
3814 nfs_fixup_secinfo_attributes(fattr
);
3820 nfs4_proc_lookup_mountpoint(struct inode
*dir
, const struct qstr
*name
,
3821 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3823 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3826 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3828 return ERR_PTR(status
);
3829 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3832 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3834 struct nfs_server
*server
= NFS_SERVER(inode
);
3835 struct nfs4_accessargs args
= {
3836 .fh
= NFS_FH(inode
),
3837 .bitmask
= server
->cache_consistency_bitmask
,
3839 struct nfs4_accessres res
= {
3842 struct rpc_message msg
= {
3843 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3846 .rpc_cred
= entry
->cred
,
3848 int mode
= entry
->mask
;
3852 * Determine which access bits we want to ask for...
3854 if (mode
& MAY_READ
)
3855 args
.access
|= NFS4_ACCESS_READ
;
3856 if (S_ISDIR(inode
->i_mode
)) {
3857 if (mode
& MAY_WRITE
)
3858 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3859 if (mode
& MAY_EXEC
)
3860 args
.access
|= NFS4_ACCESS_LOOKUP
;
3862 if (mode
& MAY_WRITE
)
3863 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3864 if (mode
& MAY_EXEC
)
3865 args
.access
|= NFS4_ACCESS_EXECUTE
;
3868 res
.fattr
= nfs_alloc_fattr();
3869 if (res
.fattr
== NULL
)
3872 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3874 nfs_access_set_mask(entry
, res
.access
);
3875 nfs_refresh_inode(inode
, res
.fattr
);
3877 nfs_free_fattr(res
.fattr
);
3881 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3883 struct nfs4_exception exception
= { };
3886 err
= _nfs4_proc_access(inode
, entry
);
3887 trace_nfs4_access(inode
, err
);
3888 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3890 } while (exception
.retry
);
3895 * TODO: For the time being, we don't try to get any attributes
3896 * along with any of the zero-copy operations READ, READDIR,
3899 * In the case of the first three, we want to put the GETATTR
3900 * after the read-type operation -- this is because it is hard
3901 * to predict the length of a GETATTR response in v4, and thus
3902 * align the READ data correctly. This means that the GETATTR
3903 * may end up partially falling into the page cache, and we should
3904 * shift it into the 'tail' of the xdr_buf before processing.
3905 * To do this efficiently, we need to know the total length
3906 * of data received, which doesn't seem to be available outside
3909 * In the case of WRITE, we also want to put the GETATTR after
3910 * the operation -- in this case because we want to make sure
3911 * we get the post-operation mtime and size.
3913 * Both of these changes to the XDR layer would in fact be quite
3914 * minor, but I decided to leave them for a subsequent patch.
3916 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3917 unsigned int pgbase
, unsigned int pglen
)
3919 struct nfs4_readlink args
= {
3920 .fh
= NFS_FH(inode
),
3925 struct nfs4_readlink_res res
;
3926 struct rpc_message msg
= {
3927 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3932 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3935 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3936 unsigned int pgbase
, unsigned int pglen
)
3938 struct nfs4_exception exception
= { };
3941 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3942 trace_nfs4_readlink(inode
, err
);
3943 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3945 } while (exception
.retry
);
3950 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3953 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3956 struct nfs4_label l
, *ilabel
= NULL
;
3957 struct nfs_open_context
*ctx
;
3958 struct nfs4_state
*state
;
3961 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
3963 return PTR_ERR(ctx
);
3965 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3967 sattr
->ia_mode
&= ~current_umask();
3968 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
3969 if (IS_ERR(state
)) {
3970 status
= PTR_ERR(state
);
3974 nfs4_label_release_security(ilabel
);
3975 put_nfs_open_context(ctx
);
3979 static int _nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
3981 struct nfs_server
*server
= NFS_SERVER(dir
);
3982 struct nfs_removeargs args
= {
3986 struct nfs_removeres res
= {
3989 struct rpc_message msg
= {
3990 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3996 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3998 update_changeattr(dir
, &res
.cinfo
);
4002 static int nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4004 struct nfs4_exception exception
= { };
4007 err
= _nfs4_proc_remove(dir
, name
);
4008 trace_nfs4_remove(dir
, name
, err
);
4009 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4011 } while (exception
.retry
);
4015 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
4017 struct nfs_server
*server
= NFS_SERVER(dir
);
4018 struct nfs_removeargs
*args
= msg
->rpc_argp
;
4019 struct nfs_removeres
*res
= msg
->rpc_resp
;
4021 res
->server
= server
;
4022 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
4023 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
4025 nfs_fattr_init(res
->dir_attr
);
4028 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
4030 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
),
4031 &data
->args
.seq_args
,
4036 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
4038 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
4039 struct nfs_removeres
*res
= &data
->res
;
4041 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4043 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
4044 &data
->timeout
) == -EAGAIN
)
4046 update_changeattr(dir
, &res
->cinfo
);
4050 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
4052 struct nfs_server
*server
= NFS_SERVER(dir
);
4053 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
4054 struct nfs_renameres
*res
= msg
->rpc_resp
;
4056 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
4057 res
->server
= server
;
4058 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
4061 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
4063 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
4064 &data
->args
.seq_args
,
4069 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
4070 struct inode
*new_dir
)
4072 struct nfs_renamedata
*data
= task
->tk_calldata
;
4073 struct nfs_renameres
*res
= &data
->res
;
4075 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4077 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
4080 update_changeattr(old_dir
, &res
->old_cinfo
);
4081 update_changeattr(new_dir
, &res
->new_cinfo
);
4085 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4087 struct nfs_server
*server
= NFS_SERVER(inode
);
4088 struct nfs4_link_arg arg
= {
4089 .fh
= NFS_FH(inode
),
4090 .dir_fh
= NFS_FH(dir
),
4092 .bitmask
= server
->attr_bitmask
,
4094 struct nfs4_link_res res
= {
4098 struct rpc_message msg
= {
4099 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
4103 int status
= -ENOMEM
;
4105 res
.fattr
= nfs_alloc_fattr();
4106 if (res
.fattr
== NULL
)
4109 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4110 if (IS_ERR(res
.label
)) {
4111 status
= PTR_ERR(res
.label
);
4114 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
4116 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4118 update_changeattr(dir
, &res
.cinfo
);
4119 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
4121 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
4125 nfs4_label_free(res
.label
);
4128 nfs_free_fattr(res
.fattr
);
4132 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4134 struct nfs4_exception exception
= { };
4137 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4138 _nfs4_proc_link(inode
, dir
, name
),
4140 } while (exception
.retry
);
4144 struct nfs4_createdata
{
4145 struct rpc_message msg
;
4146 struct nfs4_create_arg arg
;
4147 struct nfs4_create_res res
;
4149 struct nfs_fattr fattr
;
4150 struct nfs4_label
*label
;
4153 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
4154 const struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
4156 struct nfs4_createdata
*data
;
4158 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4160 struct nfs_server
*server
= NFS_SERVER(dir
);
4162 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4163 if (IS_ERR(data
->label
))
4166 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
4167 data
->msg
.rpc_argp
= &data
->arg
;
4168 data
->msg
.rpc_resp
= &data
->res
;
4169 data
->arg
.dir_fh
= NFS_FH(dir
);
4170 data
->arg
.server
= server
;
4171 data
->arg
.name
= name
;
4172 data
->arg
.attrs
= sattr
;
4173 data
->arg
.ftype
= ftype
;
4174 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
4175 data
->res
.server
= server
;
4176 data
->res
.fh
= &data
->fh
;
4177 data
->res
.fattr
= &data
->fattr
;
4178 data
->res
.label
= data
->label
;
4179 nfs_fattr_init(data
->res
.fattr
);
4187 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
4189 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
4190 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4192 update_changeattr(dir
, &data
->res
.dir_cinfo
);
4193 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
4198 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
4200 nfs4_label_free(data
->label
);
4204 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4205 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
4206 struct nfs4_label
*label
)
4208 struct nfs4_createdata
*data
;
4209 int status
= -ENAMETOOLONG
;
4211 if (len
> NFS4_MAXPATHLEN
)
4215 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
4219 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
4220 data
->arg
.u
.symlink
.pages
= &page
;
4221 data
->arg
.u
.symlink
.len
= len
;
4222 data
->arg
.label
= label
;
4224 status
= nfs4_do_create(dir
, dentry
, data
);
4226 nfs4_free_createdata(data
);
4231 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4232 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
4234 struct nfs4_exception exception
= { };
4235 struct nfs4_label l
, *label
= NULL
;
4238 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4241 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4242 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4243 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4245 } while (exception
.retry
);
4247 nfs4_label_release_security(label
);
4251 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4252 struct iattr
*sattr
, struct nfs4_label
*label
)
4254 struct nfs4_createdata
*data
;
4255 int status
= -ENOMEM
;
4257 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4261 data
->arg
.label
= label
;
4262 status
= nfs4_do_create(dir
, dentry
, data
);
4264 nfs4_free_createdata(data
);
4269 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4270 struct iattr
*sattr
)
4272 struct nfs4_exception exception
= { };
4273 struct nfs4_label l
, *label
= NULL
;
4276 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4278 sattr
->ia_mode
&= ~current_umask();
4280 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4281 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4282 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4284 } while (exception
.retry
);
4285 nfs4_label_release_security(label
);
4290 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4291 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4293 struct inode
*dir
= d_inode(dentry
);
4294 struct nfs4_readdir_arg args
= {
4299 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4302 struct nfs4_readdir_res res
;
4303 struct rpc_message msg
= {
4304 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4311 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4313 (unsigned long long)cookie
);
4314 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4315 res
.pgbase
= args
.pgbase
;
4316 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4318 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4319 status
+= args
.pgbase
;
4322 nfs_invalidate_atime(dir
);
4324 dprintk("%s: returns %d\n", __func__
, status
);
4328 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4329 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4331 struct nfs4_exception exception
= { };
4334 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4335 pages
, count
, plus
);
4336 trace_nfs4_readdir(d_inode(dentry
), err
);
4337 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4339 } while (exception
.retry
);
4343 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4344 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4346 struct nfs4_createdata
*data
;
4347 int mode
= sattr
->ia_mode
;
4348 int status
= -ENOMEM
;
4350 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4355 data
->arg
.ftype
= NF4FIFO
;
4356 else if (S_ISBLK(mode
)) {
4357 data
->arg
.ftype
= NF4BLK
;
4358 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4359 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4361 else if (S_ISCHR(mode
)) {
4362 data
->arg
.ftype
= NF4CHR
;
4363 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4364 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4365 } else if (!S_ISSOCK(mode
)) {
4370 data
->arg
.label
= label
;
4371 status
= nfs4_do_create(dir
, dentry
, data
);
4373 nfs4_free_createdata(data
);
4378 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4379 struct iattr
*sattr
, dev_t rdev
)
4381 struct nfs4_exception exception
= { };
4382 struct nfs4_label l
, *label
= NULL
;
4385 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4387 sattr
->ia_mode
&= ~current_umask();
4389 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4390 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4391 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4393 } while (exception
.retry
);
4395 nfs4_label_release_security(label
);
4400 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4401 struct nfs_fsstat
*fsstat
)
4403 struct nfs4_statfs_arg args
= {
4405 .bitmask
= server
->attr_bitmask
,
4407 struct nfs4_statfs_res res
= {
4410 struct rpc_message msg
= {
4411 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4416 nfs_fattr_init(fsstat
->fattr
);
4417 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4420 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4422 struct nfs4_exception exception
= { };
4425 err
= nfs4_handle_exception(server
,
4426 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4428 } while (exception
.retry
);
4432 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4433 struct nfs_fsinfo
*fsinfo
)
4435 struct nfs4_fsinfo_arg args
= {
4437 .bitmask
= server
->attr_bitmask
,
4439 struct nfs4_fsinfo_res res
= {
4442 struct rpc_message msg
= {
4443 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4448 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4451 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4453 struct nfs4_exception exception
= { };
4454 unsigned long now
= jiffies
;
4458 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4459 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4461 nfs4_set_lease_period(server
->nfs_client
,
4462 fsinfo
->lease_time
* HZ
,
4466 err
= nfs4_handle_exception(server
, err
, &exception
);
4467 } while (exception
.retry
);
4471 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4475 nfs_fattr_init(fsinfo
->fattr
);
4476 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4478 /* block layout checks this! */
4479 server
->pnfs_blksize
= fsinfo
->blksize
;
4480 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
);
4486 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4487 struct nfs_pathconf
*pathconf
)
4489 struct nfs4_pathconf_arg args
= {
4491 .bitmask
= server
->attr_bitmask
,
4493 struct nfs4_pathconf_res res
= {
4494 .pathconf
= pathconf
,
4496 struct rpc_message msg
= {
4497 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4502 /* None of the pathconf attributes are mandatory to implement */
4503 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4504 memset(pathconf
, 0, sizeof(*pathconf
));
4508 nfs_fattr_init(pathconf
->fattr
);
4509 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4512 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4513 struct nfs_pathconf
*pathconf
)
4515 struct nfs4_exception exception
= { };
4519 err
= nfs4_handle_exception(server
,
4520 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4522 } while (exception
.retry
);
4526 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4527 const struct nfs_open_context
*ctx
,
4528 const struct nfs_lock_context
*l_ctx
,
4531 const struct nfs_lockowner
*lockowner
= NULL
;
4534 lockowner
= &l_ctx
->lockowner
;
4535 return nfs4_select_rw_stateid(ctx
->state
, fmode
, lockowner
, stateid
, NULL
);
4537 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4539 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4540 const struct nfs_open_context
*ctx
,
4541 const struct nfs_lock_context
*l_ctx
,
4544 nfs4_stateid current_stateid
;
4546 /* If the current stateid represents a lost lock, then exit */
4547 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4549 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4552 static bool nfs4_error_stateid_expired(int err
)
4555 case -NFS4ERR_DELEG_REVOKED
:
4556 case -NFS4ERR_ADMIN_REVOKED
:
4557 case -NFS4ERR_BAD_STATEID
:
4558 case -NFS4ERR_STALE_STATEID
:
4559 case -NFS4ERR_OLD_STATEID
:
4560 case -NFS4ERR_OPENMODE
:
4561 case -NFS4ERR_EXPIRED
:
4567 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4569 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4571 trace_nfs4_read(hdr
, task
->tk_status
);
4572 if (task
->tk_status
< 0) {
4573 struct nfs4_exception exception
= {
4574 .inode
= hdr
->inode
,
4575 .state
= hdr
->args
.context
->state
,
4576 .stateid
= &hdr
->args
.stateid
,
4578 task
->tk_status
= nfs4_async_handle_exception(task
,
4579 server
, task
->tk_status
, &exception
);
4580 if (exception
.retry
) {
4581 rpc_restart_call_prepare(task
);
4586 if (task
->tk_status
> 0)
4587 renew_lease(server
, hdr
->timestamp
);
4591 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4592 struct nfs_pgio_args
*args
)
4595 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4596 nfs4_stateid_is_current(&args
->stateid
,
4601 rpc_restart_call_prepare(task
);
4605 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4608 dprintk("--> %s\n", __func__
);
4610 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4612 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4614 if (task
->tk_status
> 0)
4615 nfs_invalidate_atime(hdr
->inode
);
4616 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4617 nfs4_read_done_cb(task
, hdr
);
4620 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4621 struct rpc_message
*msg
)
4623 hdr
->timestamp
= jiffies
;
4624 if (!hdr
->pgio_done_cb
)
4625 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4626 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4627 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4630 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4631 struct nfs_pgio_header
*hdr
)
4633 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
),
4634 &hdr
->args
.seq_args
,
4638 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4639 hdr
->args
.lock_context
,
4640 hdr
->rw_ops
->rw_mode
) == -EIO
)
4642 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4647 static int nfs4_write_done_cb(struct rpc_task
*task
,
4648 struct nfs_pgio_header
*hdr
)
4650 struct inode
*inode
= hdr
->inode
;
4652 trace_nfs4_write(hdr
, task
->tk_status
);
4653 if (task
->tk_status
< 0) {
4654 struct nfs4_exception exception
= {
4655 .inode
= hdr
->inode
,
4656 .state
= hdr
->args
.context
->state
,
4657 .stateid
= &hdr
->args
.stateid
,
4659 task
->tk_status
= nfs4_async_handle_exception(task
,
4660 NFS_SERVER(inode
), task
->tk_status
,
4662 if (exception
.retry
) {
4663 rpc_restart_call_prepare(task
);
4667 if (task
->tk_status
>= 0) {
4668 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4669 nfs_writeback_update_inode(hdr
);
4674 static bool nfs4_write_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_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4690 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4692 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4694 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4695 nfs4_write_done_cb(task
, hdr
);
4699 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4701 /* Don't request attributes for pNFS or O_DIRECT writes */
4702 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4704 /* Otherwise, request attributes if and only if we don't hold
4707 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4710 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4711 struct rpc_message
*msg
)
4713 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4715 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4716 hdr
->args
.bitmask
= NULL
;
4717 hdr
->res
.fattr
= NULL
;
4719 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4721 if (!hdr
->pgio_done_cb
)
4722 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4723 hdr
->res
.server
= server
;
4724 hdr
->timestamp
= jiffies
;
4726 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4727 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4730 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4732 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4733 &data
->args
.seq_args
,
4738 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4740 struct inode
*inode
= data
->inode
;
4742 trace_nfs4_commit(data
, task
->tk_status
);
4743 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4744 NULL
, NULL
) == -EAGAIN
) {
4745 rpc_restart_call_prepare(task
);
4751 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4753 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4755 return data
->commit_done_cb(task
, data
);
4758 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4760 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4762 if (data
->commit_done_cb
== NULL
)
4763 data
->commit_done_cb
= nfs4_commit_done_cb
;
4764 data
->res
.server
= server
;
4765 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4766 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4769 struct nfs4_renewdata
{
4770 struct nfs_client
*client
;
4771 unsigned long timestamp
;
4775 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4776 * standalone procedure for queueing an asynchronous RENEW.
4778 static void nfs4_renew_release(void *calldata
)
4780 struct nfs4_renewdata
*data
= calldata
;
4781 struct nfs_client
*clp
= data
->client
;
4783 if (atomic_read(&clp
->cl_count
) > 1)
4784 nfs4_schedule_state_renewal(clp
);
4785 nfs_put_client(clp
);
4789 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4791 struct nfs4_renewdata
*data
= calldata
;
4792 struct nfs_client
*clp
= data
->client
;
4793 unsigned long timestamp
= data
->timestamp
;
4795 trace_nfs4_renew_async(clp
, task
->tk_status
);
4796 switch (task
->tk_status
) {
4799 case -NFS4ERR_LEASE_MOVED
:
4800 nfs4_schedule_lease_moved_recovery(clp
);
4803 /* Unless we're shutting down, schedule state recovery! */
4804 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4806 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4807 nfs4_schedule_lease_recovery(clp
);
4810 nfs4_schedule_path_down_recovery(clp
);
4812 do_renew_lease(clp
, timestamp
);
4815 static const struct rpc_call_ops nfs4_renew_ops
= {
4816 .rpc_call_done
= nfs4_renew_done
,
4817 .rpc_release
= nfs4_renew_release
,
4820 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4822 struct rpc_message msg
= {
4823 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4827 struct nfs4_renewdata
*data
;
4829 if (renew_flags
== 0)
4831 if (!atomic_inc_not_zero(&clp
->cl_count
))
4833 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4837 data
->timestamp
= jiffies
;
4838 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4839 &nfs4_renew_ops
, data
);
4842 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4844 struct rpc_message msg
= {
4845 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4849 unsigned long now
= jiffies
;
4852 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4855 do_renew_lease(clp
, now
);
4859 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4861 return server
->caps
& NFS_CAP_ACLS
;
4864 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4865 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4868 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4870 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4871 struct page
**pages
)
4873 struct page
*newpage
, **spages
;
4879 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4880 newpage
= alloc_page(GFP_KERNEL
);
4882 if (newpage
== NULL
)
4884 memcpy(page_address(newpage
), buf
, len
);
4889 } while (buflen
!= 0);
4895 __free_page(spages
[rc
-1]);
4899 struct nfs4_cached_acl
{
4905 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4907 struct nfs_inode
*nfsi
= NFS_I(inode
);
4909 spin_lock(&inode
->i_lock
);
4910 kfree(nfsi
->nfs4_acl
);
4911 nfsi
->nfs4_acl
= acl
;
4912 spin_unlock(&inode
->i_lock
);
4915 static void nfs4_zap_acl_attr(struct inode
*inode
)
4917 nfs4_set_cached_acl(inode
, NULL
);
4920 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4922 struct nfs_inode
*nfsi
= NFS_I(inode
);
4923 struct nfs4_cached_acl
*acl
;
4926 spin_lock(&inode
->i_lock
);
4927 acl
= nfsi
->nfs4_acl
;
4930 if (buf
== NULL
) /* user is just asking for length */
4932 if (acl
->cached
== 0)
4934 ret
= -ERANGE
; /* see getxattr(2) man page */
4935 if (acl
->len
> buflen
)
4937 memcpy(buf
, acl
->data
, acl
->len
);
4941 spin_unlock(&inode
->i_lock
);
4945 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4947 struct nfs4_cached_acl
*acl
;
4948 size_t buflen
= sizeof(*acl
) + acl_len
;
4950 if (buflen
<= PAGE_SIZE
) {
4951 acl
= kmalloc(buflen
, GFP_KERNEL
);
4955 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4957 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4964 nfs4_set_cached_acl(inode
, acl
);
4968 * The getxattr API returns the required buffer length when called with a
4969 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4970 * the required buf. On a NULL buf, we send a page of data to the server
4971 * guessing that the ACL request can be serviced by a page. If so, we cache
4972 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4973 * the cache. If not so, we throw away the page, and cache the required
4974 * length. The next getxattr call will then produce another round trip to
4975 * the server, this time with the input buf of the required size.
4977 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4979 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4980 struct nfs_getaclargs args
= {
4981 .fh
= NFS_FH(inode
),
4985 struct nfs_getaclres res
= {
4988 struct rpc_message msg
= {
4989 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4993 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4994 int ret
= -ENOMEM
, i
;
4996 /* As long as we're doing a round trip to the server anyway,
4997 * let's be prepared for a page of acl data. */
5000 if (npages
> ARRAY_SIZE(pages
))
5003 for (i
= 0; i
< npages
; i
++) {
5004 pages
[i
] = alloc_page(GFP_KERNEL
);
5009 /* for decoding across pages */
5010 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
5011 if (!res
.acl_scratch
)
5014 args
.acl_len
= npages
* PAGE_SIZE
;
5016 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5017 __func__
, buf
, buflen
, npages
, args
.acl_len
);
5018 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
5019 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5023 /* Handle the case where the passed-in buffer is too short */
5024 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
5025 /* Did the user only issue a request for the acl length? */
5031 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
5033 if (res
.acl_len
> buflen
) {
5037 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
5042 for (i
= 0; i
< npages
; i
++)
5044 __free_page(pages
[i
]);
5045 if (res
.acl_scratch
)
5046 __free_page(res
.acl_scratch
);
5050 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5052 struct nfs4_exception exception
= { };
5055 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
5056 trace_nfs4_get_acl(inode
, ret
);
5059 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
5060 } while (exception
.retry
);
5064 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
5066 struct nfs_server
*server
= NFS_SERVER(inode
);
5069 if (!nfs4_server_supports_acls(server
))
5071 ret
= nfs_revalidate_inode(server
, inode
);
5074 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
5075 nfs_zap_acl_cache(inode
);
5076 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
5078 /* -ENOENT is returned if there is no ACL or if there is an ACL
5079 * but no cached acl data, just the acl length */
5081 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
5084 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5086 struct nfs_server
*server
= NFS_SERVER(inode
);
5087 struct page
*pages
[NFS4ACL_MAXPAGES
];
5088 struct nfs_setaclargs arg
= {
5089 .fh
= NFS_FH(inode
),
5093 struct nfs_setaclres res
;
5094 struct rpc_message msg
= {
5095 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
5099 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5102 if (!nfs4_server_supports_acls(server
))
5104 if (npages
> ARRAY_SIZE(pages
))
5106 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
5109 nfs4_inode_return_delegation(inode
);
5110 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5113 * Free each page after tx, so the only ref left is
5114 * held by the network stack
5117 put_page(pages
[i
-1]);
5120 * Acl update can result in inode attribute update.
5121 * so mark the attribute cache invalid.
5123 spin_lock(&inode
->i_lock
);
5124 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
5125 spin_unlock(&inode
->i_lock
);
5126 nfs_access_zap_cache(inode
);
5127 nfs_zap_acl_cache(inode
);
5131 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5133 struct nfs4_exception exception
= { };
5136 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
5137 trace_nfs4_set_acl(inode
, err
);
5138 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5140 } while (exception
.retry
);
5144 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5145 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
5148 struct nfs_server
*server
= NFS_SERVER(inode
);
5149 struct nfs_fattr fattr
;
5150 struct nfs4_label label
= {0, 0, buflen
, buf
};
5152 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5153 struct nfs4_getattr_arg arg
= {
5154 .fh
= NFS_FH(inode
),
5157 struct nfs4_getattr_res res
= {
5162 struct rpc_message msg
= {
5163 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
5169 nfs_fattr_init(&fattr
);
5171 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
5174 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
5176 if (buflen
< label
.len
)
5181 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
5184 struct nfs4_exception exception
= { };
5187 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5191 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
5192 trace_nfs4_get_security_label(inode
, err
);
5193 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5195 } while (exception
.retry
);
5199 static int _nfs4_do_set_security_label(struct inode
*inode
,
5200 struct nfs4_label
*ilabel
,
5201 struct nfs_fattr
*fattr
,
5202 struct nfs4_label
*olabel
)
5205 struct iattr sattr
= {0};
5206 struct nfs_server
*server
= NFS_SERVER(inode
);
5207 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5208 struct nfs_setattrargs arg
= {
5209 .fh
= NFS_FH(inode
),
5215 struct nfs_setattrres res
= {
5220 struct rpc_message msg
= {
5221 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
5227 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
5229 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5231 dprintk("%s failed: %d\n", __func__
, status
);
5236 static int nfs4_do_set_security_label(struct inode
*inode
,
5237 struct nfs4_label
*ilabel
,
5238 struct nfs_fattr
*fattr
,
5239 struct nfs4_label
*olabel
)
5241 struct nfs4_exception exception
= { };
5245 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5247 trace_nfs4_set_security_label(inode
, err
);
5248 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5250 } while (exception
.retry
);
5255 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5257 struct nfs4_label ilabel
, *olabel
= NULL
;
5258 struct nfs_fattr fattr
;
5259 struct rpc_cred
*cred
;
5262 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5265 nfs_fattr_init(&fattr
);
5269 ilabel
.label
= (char *)buf
;
5270 ilabel
.len
= buflen
;
5272 cred
= rpc_lookup_cred();
5274 return PTR_ERR(cred
);
5276 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5277 if (IS_ERR(olabel
)) {
5278 status
= -PTR_ERR(olabel
);
5282 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5284 nfs_setsecurity(inode
, &fattr
, olabel
);
5286 nfs4_label_free(olabel
);
5291 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5294 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5295 nfs4_verifier
*bootverf
)
5299 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5300 /* An impossible timestamp guarantees this value
5301 * will never match a generated boot time. */
5302 verf
[0] = cpu_to_be32(U32_MAX
);
5303 verf
[1] = cpu_to_be32(U32_MAX
);
5305 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5306 u64 ns
= ktime_to_ns(nn
->boot_time
);
5308 verf
[0] = cpu_to_be32(ns
>> 32);
5309 verf
[1] = cpu_to_be32(ns
);
5311 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5315 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5320 if (clp
->cl_owner_id
!= NULL
)
5324 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5325 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5327 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
5331 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5335 * Since this string is allocated at mount time, and held until the
5336 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5337 * about a memory-reclaim deadlock.
5339 str
= kmalloc(len
, GFP_KERNEL
);
5344 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5346 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5347 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5350 clp
->cl_owner_id
= str
;
5355 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5360 len
= 10 + 10 + 1 + 10 + 1 +
5361 strlen(nfs4_client_id_uniquifier
) + 1 +
5362 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5364 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5368 * Since this string is allocated at mount time, and held until the
5369 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5370 * about a memory-reclaim deadlock.
5372 str
= kmalloc(len
, GFP_KERNEL
);
5376 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5377 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5378 nfs4_client_id_uniquifier
,
5379 clp
->cl_rpcclient
->cl_nodename
);
5380 clp
->cl_owner_id
= str
;
5385 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5390 if (clp
->cl_owner_id
!= NULL
)
5393 if (nfs4_client_id_uniquifier
[0] != '\0')
5394 return nfs4_init_uniquifier_client_string(clp
);
5396 len
= 10 + 10 + 1 + 10 + 1 +
5397 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5399 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5403 * Since this string is allocated at mount time, and held until the
5404 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5405 * about a memory-reclaim deadlock.
5407 str
= kmalloc(len
, GFP_KERNEL
);
5411 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5412 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5413 clp
->cl_rpcclient
->cl_nodename
);
5414 clp
->cl_owner_id
= str
;
5419 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5420 * services. Advertise one based on the address family of the
5424 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5426 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5427 return scnprintf(buf
, len
, "tcp6");
5429 return scnprintf(buf
, len
, "tcp");
5432 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5434 struct nfs4_setclientid
*sc
= calldata
;
5436 if (task
->tk_status
== 0)
5437 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5440 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5441 .rpc_call_done
= nfs4_setclientid_done
,
5445 * nfs4_proc_setclientid - Negotiate client ID
5446 * @clp: state data structure
5447 * @program: RPC program for NFSv4 callback service
5448 * @port: IP port number for NFS4 callback service
5449 * @cred: RPC credential to use for this call
5450 * @res: where to place the result
5452 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5454 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5455 unsigned short port
, struct rpc_cred
*cred
,
5456 struct nfs4_setclientid_res
*res
)
5458 nfs4_verifier sc_verifier
;
5459 struct nfs4_setclientid setclientid
= {
5460 .sc_verifier
= &sc_verifier
,
5464 struct rpc_message msg
= {
5465 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5466 .rpc_argp
= &setclientid
,
5470 struct rpc_task
*task
;
5471 struct rpc_task_setup task_setup_data
= {
5472 .rpc_client
= clp
->cl_rpcclient
,
5473 .rpc_message
= &msg
,
5474 .callback_ops
= &nfs4_setclientid_ops
,
5475 .callback_data
= &setclientid
,
5476 .flags
= RPC_TASK_TIMEOUT
,
5480 /* nfs_client_id4 */
5481 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5483 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5484 status
= nfs4_init_uniform_client_string(clp
);
5486 status
= nfs4_init_nonuniform_client_string(clp
);
5492 setclientid
.sc_netid_len
=
5493 nfs4_init_callback_netid(clp
,
5494 setclientid
.sc_netid
,
5495 sizeof(setclientid
.sc_netid
));
5496 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5497 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5498 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5500 dprintk("NFS call setclientid auth=%s, '%s'\n",
5501 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5503 task
= rpc_run_task(&task_setup_data
);
5505 status
= PTR_ERR(task
);
5508 status
= task
->tk_status
;
5509 if (setclientid
.sc_cred
) {
5510 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5511 put_rpccred(setclientid
.sc_cred
);
5515 trace_nfs4_setclientid(clp
, status
);
5516 dprintk("NFS reply setclientid: %d\n", status
);
5521 * nfs4_proc_setclientid_confirm - Confirm client ID
5522 * @clp: state data structure
5523 * @res: result of a previous SETCLIENTID
5524 * @cred: RPC credential to use for this call
5526 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5528 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5529 struct nfs4_setclientid_res
*arg
,
5530 struct rpc_cred
*cred
)
5532 struct rpc_message msg
= {
5533 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5539 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5540 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5542 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5543 trace_nfs4_setclientid_confirm(clp
, status
);
5544 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5548 struct nfs4_delegreturndata
{
5549 struct nfs4_delegreturnargs args
;
5550 struct nfs4_delegreturnres res
;
5552 nfs4_stateid stateid
;
5553 unsigned long timestamp
;
5554 struct nfs_fattr fattr
;
5556 struct inode
*inode
;
5561 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5563 struct nfs4_delegreturndata
*data
= calldata
;
5565 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5568 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5569 switch (task
->tk_status
) {
5571 renew_lease(data
->res
.server
, data
->timestamp
);
5572 case -NFS4ERR_ADMIN_REVOKED
:
5573 case -NFS4ERR_DELEG_REVOKED
:
5574 case -NFS4ERR_EXPIRED
:
5575 nfs4_free_revoked_stateid(data
->res
.server
,
5577 task
->tk_msg
.rpc_cred
);
5578 case -NFS4ERR_BAD_STATEID
:
5579 case -NFS4ERR_OLD_STATEID
:
5580 case -NFS4ERR_STALE_STATEID
:
5581 task
->tk_status
= 0;
5583 pnfs_roc_set_barrier(data
->inode
, data
->roc_barrier
);
5586 if (nfs4_async_handle_error(task
, data
->res
.server
,
5587 NULL
, NULL
) == -EAGAIN
) {
5588 rpc_restart_call_prepare(task
);
5592 data
->rpc_status
= task
->tk_status
;
5595 static void nfs4_delegreturn_release(void *calldata
)
5597 struct nfs4_delegreturndata
*data
= calldata
;
5598 struct inode
*inode
= data
->inode
;
5602 pnfs_roc_release(inode
);
5603 nfs_iput_and_deactive(inode
);
5608 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5610 struct nfs4_delegreturndata
*d_data
;
5612 d_data
= (struct nfs4_delegreturndata
*)data
;
5614 if (nfs4_wait_on_layoutreturn(d_data
->inode
, task
))
5618 pnfs_roc_get_barrier(d_data
->inode
, &d_data
->roc_barrier
);
5620 nfs4_setup_sequence(d_data
->res
.server
,
5621 &d_data
->args
.seq_args
,
5622 &d_data
->res
.seq_res
,
5626 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5627 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5628 .rpc_call_done
= nfs4_delegreturn_done
,
5629 .rpc_release
= nfs4_delegreturn_release
,
5632 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5634 struct nfs4_delegreturndata
*data
;
5635 struct nfs_server
*server
= NFS_SERVER(inode
);
5636 struct rpc_task
*task
;
5637 struct rpc_message msg
= {
5638 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5641 struct rpc_task_setup task_setup_data
= {
5642 .rpc_client
= server
->client
,
5643 .rpc_message
= &msg
,
5644 .callback_ops
= &nfs4_delegreturn_ops
,
5645 .flags
= RPC_TASK_ASYNC
,
5649 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5652 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5654 nfs4_state_protect(server
->nfs_client
,
5655 NFS_SP4_MACH_CRED_CLEANUP
,
5656 &task_setup_data
.rpc_client
, &msg
);
5658 data
->args
.fhandle
= &data
->fh
;
5659 data
->args
.stateid
= &data
->stateid
;
5660 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5661 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5662 nfs4_stateid_copy(&data
->stateid
, stateid
);
5663 data
->res
.fattr
= &data
->fattr
;
5664 data
->res
.server
= server
;
5665 nfs_fattr_init(data
->res
.fattr
);
5666 data
->timestamp
= jiffies
;
5667 data
->rpc_status
= 0;
5668 data
->inode
= nfs_igrab_and_active(inode
);
5670 data
->roc
= nfs4_roc(inode
);
5672 task_setup_data
.callback_data
= data
;
5673 msg
.rpc_argp
= &data
->args
;
5674 msg
.rpc_resp
= &data
->res
;
5675 task
= rpc_run_task(&task_setup_data
);
5677 return PTR_ERR(task
);
5680 status
= nfs4_wait_for_completion_rpc_task(task
);
5683 status
= data
->rpc_status
;
5685 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5687 nfs_refresh_inode(inode
, &data
->fattr
);
5693 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5695 struct nfs_server
*server
= NFS_SERVER(inode
);
5696 struct nfs4_exception exception
= { };
5699 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5700 trace_nfs4_delegreturn(inode
, stateid
, err
);
5702 case -NFS4ERR_STALE_STATEID
:
5703 case -NFS4ERR_EXPIRED
:
5707 err
= nfs4_handle_exception(server
, err
, &exception
);
5708 } while (exception
.retry
);
5712 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5714 struct inode
*inode
= state
->inode
;
5715 struct nfs_server
*server
= NFS_SERVER(inode
);
5716 struct nfs_client
*clp
= server
->nfs_client
;
5717 struct nfs_lockt_args arg
= {
5718 .fh
= NFS_FH(inode
),
5721 struct nfs_lockt_res res
= {
5724 struct rpc_message msg
= {
5725 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5728 .rpc_cred
= state
->owner
->so_cred
,
5730 struct nfs4_lock_state
*lsp
;
5733 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5734 status
= nfs4_set_lock_state(state
, request
);
5737 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5738 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5739 arg
.lock_owner
.s_dev
= server
->s_dev
;
5740 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5743 request
->fl_type
= F_UNLCK
;
5745 case -NFS4ERR_DENIED
:
5748 request
->fl_ops
->fl_release_private(request
);
5749 request
->fl_ops
= NULL
;
5754 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5756 struct nfs4_exception exception
= { };
5760 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5761 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5762 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5764 } while (exception
.retry
);
5768 struct nfs4_unlockdata
{
5769 struct nfs_locku_args arg
;
5770 struct nfs_locku_res res
;
5771 struct nfs4_lock_state
*lsp
;
5772 struct nfs_open_context
*ctx
;
5773 struct file_lock fl
;
5774 struct nfs_server
*server
;
5775 unsigned long timestamp
;
5778 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5779 struct nfs_open_context
*ctx
,
5780 struct nfs4_lock_state
*lsp
,
5781 struct nfs_seqid
*seqid
)
5783 struct nfs4_unlockdata
*p
;
5784 struct inode
*inode
= lsp
->ls_state
->inode
;
5786 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5789 p
->arg
.fh
= NFS_FH(inode
);
5791 p
->arg
.seqid
= seqid
;
5792 p
->res
.seqid
= seqid
;
5794 atomic_inc(&lsp
->ls_count
);
5795 /* Ensure we don't close file until we're done freeing locks! */
5796 p
->ctx
= get_nfs_open_context(ctx
);
5797 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5798 p
->server
= NFS_SERVER(inode
);
5802 static void nfs4_locku_release_calldata(void *data
)
5804 struct nfs4_unlockdata
*calldata
= data
;
5805 nfs_free_seqid(calldata
->arg
.seqid
);
5806 nfs4_put_lock_state(calldata
->lsp
);
5807 put_nfs_open_context(calldata
->ctx
);
5811 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5813 struct nfs4_unlockdata
*calldata
= data
;
5815 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5817 switch (task
->tk_status
) {
5819 renew_lease(calldata
->server
, calldata
->timestamp
);
5820 locks_lock_inode_wait(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5821 if (nfs4_update_lock_stateid(calldata
->lsp
,
5822 &calldata
->res
.stateid
))
5824 case -NFS4ERR_ADMIN_REVOKED
:
5825 case -NFS4ERR_EXPIRED
:
5826 nfs4_free_revoked_stateid(calldata
->server
,
5827 &calldata
->arg
.stateid
,
5828 task
->tk_msg
.rpc_cred
);
5829 case -NFS4ERR_BAD_STATEID
:
5830 case -NFS4ERR_OLD_STATEID
:
5831 case -NFS4ERR_STALE_STATEID
:
5832 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5833 &calldata
->lsp
->ls_stateid
))
5834 rpc_restart_call_prepare(task
);
5837 if (nfs4_async_handle_error(task
, calldata
->server
,
5838 NULL
, NULL
) == -EAGAIN
)
5839 rpc_restart_call_prepare(task
);
5841 nfs_release_seqid(calldata
->arg
.seqid
);
5844 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5846 struct nfs4_unlockdata
*calldata
= data
;
5848 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5850 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5851 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5852 /* Note: exit _without_ running nfs4_locku_done */
5855 calldata
->timestamp
= jiffies
;
5856 if (nfs4_setup_sequence(calldata
->server
,
5857 &calldata
->arg
.seq_args
,
5858 &calldata
->res
.seq_res
,
5860 nfs_release_seqid(calldata
->arg
.seqid
);
5863 task
->tk_action
= NULL
;
5865 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5868 static const struct rpc_call_ops nfs4_locku_ops
= {
5869 .rpc_call_prepare
= nfs4_locku_prepare
,
5870 .rpc_call_done
= nfs4_locku_done
,
5871 .rpc_release
= nfs4_locku_release_calldata
,
5874 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5875 struct nfs_open_context
*ctx
,
5876 struct nfs4_lock_state
*lsp
,
5877 struct nfs_seqid
*seqid
)
5879 struct nfs4_unlockdata
*data
;
5880 struct rpc_message msg
= {
5881 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5882 .rpc_cred
= ctx
->cred
,
5884 struct rpc_task_setup task_setup_data
= {
5885 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5886 .rpc_message
= &msg
,
5887 .callback_ops
= &nfs4_locku_ops
,
5888 .workqueue
= nfsiod_workqueue
,
5889 .flags
= RPC_TASK_ASYNC
,
5892 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5893 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5895 /* Ensure this is an unlock - when canceling a lock, the
5896 * canceled lock is passed in, and it won't be an unlock.
5898 fl
->fl_type
= F_UNLCK
;
5900 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5902 nfs_free_seqid(seqid
);
5903 return ERR_PTR(-ENOMEM
);
5906 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5907 msg
.rpc_argp
= &data
->arg
;
5908 msg
.rpc_resp
= &data
->res
;
5909 task_setup_data
.callback_data
= data
;
5910 return rpc_run_task(&task_setup_data
);
5913 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5915 struct inode
*inode
= state
->inode
;
5916 struct nfs4_state_owner
*sp
= state
->owner
;
5917 struct nfs_inode
*nfsi
= NFS_I(inode
);
5918 struct nfs_seqid
*seqid
;
5919 struct nfs4_lock_state
*lsp
;
5920 struct rpc_task
*task
;
5921 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
5923 unsigned char fl_flags
= request
->fl_flags
;
5925 status
= nfs4_set_lock_state(state
, request
);
5926 /* Unlock _before_ we do the RPC call */
5927 request
->fl_flags
|= FL_EXISTS
;
5928 /* Exclude nfs_delegation_claim_locks() */
5929 mutex_lock(&sp
->so_delegreturn_mutex
);
5930 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5931 down_read(&nfsi
->rwsem
);
5932 if (locks_lock_inode_wait(inode
, request
) == -ENOENT
) {
5933 up_read(&nfsi
->rwsem
);
5934 mutex_unlock(&sp
->so_delegreturn_mutex
);
5937 up_read(&nfsi
->rwsem
);
5938 mutex_unlock(&sp
->so_delegreturn_mutex
);
5941 /* Is this a delegated lock? */
5942 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5943 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5945 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
5946 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5950 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5951 status
= PTR_ERR(task
);
5954 status
= nfs4_wait_for_completion_rpc_task(task
);
5957 request
->fl_flags
= fl_flags
;
5958 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
5962 struct nfs4_lockdata
{
5963 struct nfs_lock_args arg
;
5964 struct nfs_lock_res res
;
5965 struct nfs4_lock_state
*lsp
;
5966 struct nfs_open_context
*ctx
;
5967 struct file_lock fl
;
5968 unsigned long timestamp
;
5971 struct nfs_server
*server
;
5974 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
5975 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
5978 struct nfs4_lockdata
*p
;
5979 struct inode
*inode
= lsp
->ls_state
->inode
;
5980 struct nfs_server
*server
= NFS_SERVER(inode
);
5981 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
5983 p
= kzalloc(sizeof(*p
), gfp_mask
);
5987 p
->arg
.fh
= NFS_FH(inode
);
5989 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
5990 if (IS_ERR(p
->arg
.open_seqid
))
5992 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
5993 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
5994 if (IS_ERR(p
->arg
.lock_seqid
))
5995 goto out_free_seqid
;
5996 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5997 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5998 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
5999 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
6002 atomic_inc(&lsp
->ls_count
);
6003 p
->ctx
= get_nfs_open_context(ctx
);
6004 get_file(fl
->fl_file
);
6005 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
6008 nfs_free_seqid(p
->arg
.open_seqid
);
6014 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
6016 struct nfs4_lockdata
*data
= calldata
;
6017 struct nfs4_state
*state
= data
->lsp
->ls_state
;
6019 dprintk("%s: begin!\n", __func__
);
6020 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
6022 /* Do we need to do an open_to_lock_owner? */
6023 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
6024 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
6025 goto out_release_lock_seqid
;
6027 nfs4_stateid_copy(&data
->arg
.open_stateid
,
6028 &state
->open_stateid
);
6029 data
->arg
.new_lock_owner
= 1;
6030 data
->res
.open_seqid
= data
->arg
.open_seqid
;
6032 data
->arg
.new_lock_owner
= 0;
6033 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
6034 &data
->lsp
->ls_stateid
);
6036 if (!nfs4_valid_open_stateid(state
)) {
6037 data
->rpc_status
= -EBADF
;
6038 task
->tk_action
= NULL
;
6039 goto out_release_open_seqid
;
6041 data
->timestamp
= jiffies
;
6042 if (nfs4_setup_sequence(data
->server
,
6043 &data
->arg
.seq_args
,
6047 out_release_open_seqid
:
6048 nfs_release_seqid(data
->arg
.open_seqid
);
6049 out_release_lock_seqid
:
6050 nfs_release_seqid(data
->arg
.lock_seqid
);
6052 nfs4_sequence_done(task
, &data
->res
.seq_res
);
6053 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
6056 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
6058 struct nfs4_lockdata
*data
= calldata
;
6059 struct nfs4_lock_state
*lsp
= data
->lsp
;
6061 dprintk("%s: begin!\n", __func__
);
6063 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
6066 data
->rpc_status
= task
->tk_status
;
6067 switch (task
->tk_status
) {
6069 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
6071 if (data
->arg
.new_lock
) {
6072 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
6073 if (locks_lock_inode_wait(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
6074 rpc_restart_call_prepare(task
);
6078 if (data
->arg
.new_lock_owner
!= 0) {
6079 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
6080 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
6081 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6082 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
6083 rpc_restart_call_prepare(task
);
6085 case -NFS4ERR_BAD_STATEID
:
6086 case -NFS4ERR_OLD_STATEID
:
6087 case -NFS4ERR_STALE_STATEID
:
6088 case -NFS4ERR_EXPIRED
:
6089 if (data
->arg
.new_lock_owner
!= 0) {
6090 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
6091 &lsp
->ls_state
->open_stateid
))
6092 rpc_restart_call_prepare(task
);
6093 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
6095 rpc_restart_call_prepare(task
);
6097 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
6100 static void nfs4_lock_release(void *calldata
)
6102 struct nfs4_lockdata
*data
= calldata
;
6104 dprintk("%s: begin!\n", __func__
);
6105 nfs_free_seqid(data
->arg
.open_seqid
);
6106 if (data
->cancelled
!= 0) {
6107 struct rpc_task
*task
;
6108 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
6109 data
->arg
.lock_seqid
);
6111 rpc_put_task_async(task
);
6112 dprintk("%s: cancelling lock!\n", __func__
);
6114 nfs_free_seqid(data
->arg
.lock_seqid
);
6115 nfs4_put_lock_state(data
->lsp
);
6116 put_nfs_open_context(data
->ctx
);
6117 fput(data
->fl
.fl_file
);
6119 dprintk("%s: done!\n", __func__
);
6122 static const struct rpc_call_ops nfs4_lock_ops
= {
6123 .rpc_call_prepare
= nfs4_lock_prepare
,
6124 .rpc_call_done
= nfs4_lock_done
,
6125 .rpc_release
= nfs4_lock_release
,
6128 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
6131 case -NFS4ERR_ADMIN_REVOKED
:
6132 case -NFS4ERR_EXPIRED
:
6133 case -NFS4ERR_BAD_STATEID
:
6134 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6135 if (new_lock_owner
!= 0 ||
6136 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
6137 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
6139 case -NFS4ERR_STALE_STATEID
:
6140 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6141 nfs4_schedule_lease_recovery(server
->nfs_client
);
6145 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
6147 struct nfs4_lockdata
*data
;
6148 struct rpc_task
*task
;
6149 struct rpc_message msg
= {
6150 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
6151 .rpc_cred
= state
->owner
->so_cred
,
6153 struct rpc_task_setup task_setup_data
= {
6154 .rpc_client
= NFS_CLIENT(state
->inode
),
6155 .rpc_message
= &msg
,
6156 .callback_ops
= &nfs4_lock_ops
,
6157 .workqueue
= nfsiod_workqueue
,
6158 .flags
= RPC_TASK_ASYNC
,
6162 dprintk("%s: begin!\n", __func__
);
6163 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
6164 fl
->fl_u
.nfs4_fl
.owner
,
6165 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
6169 data
->arg
.block
= 1;
6170 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6171 msg
.rpc_argp
= &data
->arg
;
6172 msg
.rpc_resp
= &data
->res
;
6173 task_setup_data
.callback_data
= data
;
6174 if (recovery_type
> NFS_LOCK_NEW
) {
6175 if (recovery_type
== NFS_LOCK_RECLAIM
)
6176 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
6177 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
6179 data
->arg
.new_lock
= 1;
6180 task
= rpc_run_task(&task_setup_data
);
6182 return PTR_ERR(task
);
6183 ret
= nfs4_wait_for_completion_rpc_task(task
);
6185 ret
= data
->rpc_status
;
6187 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
6188 data
->arg
.new_lock_owner
, ret
);
6190 data
->cancelled
= 1;
6192 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
6193 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
6197 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
6199 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6200 struct nfs4_exception exception
= {
6201 .inode
= state
->inode
,
6206 /* Cache the lock if possible... */
6207 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6209 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
6210 if (err
!= -NFS4ERR_DELAY
)
6212 nfs4_handle_exception(server
, err
, &exception
);
6213 } while (exception
.retry
);
6217 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6219 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6220 struct nfs4_exception exception
= {
6221 .inode
= state
->inode
,
6225 err
= nfs4_set_lock_state(state
, request
);
6228 if (!recover_lost_locks
) {
6229 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6233 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6235 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6239 case -NFS4ERR_GRACE
:
6240 case -NFS4ERR_DELAY
:
6241 nfs4_handle_exception(server
, err
, &exception
);
6244 } while (exception
.retry
);
6249 #if defined(CONFIG_NFS_V4_1)
6250 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6252 struct nfs4_lock_state
*lsp
;
6255 status
= nfs4_set_lock_state(state
, request
);
6258 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6259 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) ||
6260 test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
6262 status
= nfs4_lock_expired(state
, request
);
6267 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6269 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6270 struct nfs4_state_owner
*sp
= state
->owner
;
6271 unsigned char fl_flags
= request
->fl_flags
;
6274 request
->fl_flags
|= FL_ACCESS
;
6275 status
= locks_lock_inode_wait(state
->inode
, request
);
6278 mutex_lock(&sp
->so_delegreturn_mutex
);
6279 down_read(&nfsi
->rwsem
);
6280 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6281 /* Yes: cache locks! */
6282 /* ...but avoid races with delegation recall... */
6283 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6284 status
= locks_lock_inode_wait(state
->inode
, request
);
6285 up_read(&nfsi
->rwsem
);
6286 mutex_unlock(&sp
->so_delegreturn_mutex
);
6289 up_read(&nfsi
->rwsem
);
6290 mutex_unlock(&sp
->so_delegreturn_mutex
);
6291 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6293 request
->fl_flags
= fl_flags
;
6297 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6299 struct nfs4_exception exception
= {
6301 .inode
= state
->inode
,
6306 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6307 if (err
== -NFS4ERR_DENIED
)
6309 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6311 } while (exception
.retry
);
6315 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6316 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6319 nfs4_retry_setlk_simple(struct nfs4_state
*state
, int cmd
,
6320 struct file_lock
*request
)
6322 int status
= -ERESTARTSYS
;
6323 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6325 while(!signalled()) {
6326 status
= nfs4_proc_setlk(state
, cmd
, request
);
6327 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6329 freezable_schedule_timeout_interruptible(timeout
);
6331 timeout
= min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT
, timeout
);
6332 status
= -ERESTARTSYS
;
6337 #ifdef CONFIG_NFS_V4_1
6338 struct nfs4_lock_waiter
{
6339 struct task_struct
*task
;
6340 struct inode
*inode
;
6341 struct nfs_lowner
*owner
;
6346 nfs4_wake_lock_waiter(wait_queue_t
*wait
, unsigned int mode
, int flags
, void *key
)
6349 struct cb_notify_lock_args
*cbnl
= key
;
6350 struct nfs4_lock_waiter
*waiter
= wait
->private;
6351 struct nfs_lowner
*lowner
= &cbnl
->cbnl_owner
,
6352 *wowner
= waiter
->owner
;
6354 /* Only wake if the callback was for the same owner */
6355 if (lowner
->clientid
!= wowner
->clientid
||
6356 lowner
->id
!= wowner
->id
||
6357 lowner
->s_dev
!= wowner
->s_dev
)
6360 /* Make sure it's for the right inode */
6361 if (nfs_compare_fh(NFS_FH(waiter
->inode
), &cbnl
->cbnl_fh
))
6364 waiter
->notified
= true;
6366 /* override "private" so we can use default_wake_function */
6367 wait
->private = waiter
->task
;
6368 ret
= autoremove_wake_function(wait
, mode
, flags
, key
);
6369 wait
->private = waiter
;
6374 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6376 int status
= -ERESTARTSYS
;
6377 unsigned long flags
;
6378 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
6379 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6380 struct nfs_client
*clp
= server
->nfs_client
;
6381 wait_queue_head_t
*q
= &clp
->cl_lock_waitq
;
6382 struct nfs_lowner owner
= { .clientid
= clp
->cl_clientid
,
6383 .id
= lsp
->ls_seqid
.owner_id
,
6384 .s_dev
= server
->s_dev
};
6385 struct nfs4_lock_waiter waiter
= { .task
= current
,
6386 .inode
= state
->inode
,
6388 .notified
= false };
6391 /* Don't bother with waitqueue if we don't expect a callback */
6392 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
))
6393 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6396 wait
.private = &waiter
;
6397 wait
.func
= nfs4_wake_lock_waiter
;
6398 add_wait_queue(q
, &wait
);
6400 while(!signalled()) {
6401 status
= nfs4_proc_setlk(state
, cmd
, request
);
6402 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6405 status
= -ERESTARTSYS
;
6406 spin_lock_irqsave(&q
->lock
, flags
);
6407 if (waiter
.notified
) {
6408 spin_unlock_irqrestore(&q
->lock
, flags
);
6411 set_current_state(TASK_INTERRUPTIBLE
);
6412 spin_unlock_irqrestore(&q
->lock
, flags
);
6414 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT
);
6417 finish_wait(q
, &wait
);
6420 #else /* !CONFIG_NFS_V4_1 */
6422 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6424 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6429 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6431 struct nfs_open_context
*ctx
;
6432 struct nfs4_state
*state
;
6435 /* verify open state */
6436 ctx
= nfs_file_open_context(filp
);
6439 if (request
->fl_start
< 0 || request
->fl_end
< 0)
6442 if (IS_GETLK(cmd
)) {
6444 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6448 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6451 if (request
->fl_type
== F_UNLCK
) {
6453 return nfs4_proc_unlck(state
, cmd
, request
);
6460 if ((request
->fl_flags
& FL_POSIX
) &&
6461 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6465 * Don't rely on the VFS having checked the file open mode,
6466 * since it won't do this for flock() locks.
6468 switch (request
->fl_type
) {
6470 if (!(filp
->f_mode
& FMODE_READ
))
6474 if (!(filp
->f_mode
& FMODE_WRITE
))
6478 status
= nfs4_set_lock_state(state
, request
);
6482 return nfs4_retry_setlk(state
, cmd
, request
);
6485 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6487 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6490 err
= nfs4_set_lock_state(state
, fl
);
6493 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6494 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6497 struct nfs_release_lockowner_data
{
6498 struct nfs4_lock_state
*lsp
;
6499 struct nfs_server
*server
;
6500 struct nfs_release_lockowner_args args
;
6501 struct nfs_release_lockowner_res res
;
6502 unsigned long timestamp
;
6505 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6507 struct nfs_release_lockowner_data
*data
= calldata
;
6508 struct nfs_server
*server
= data
->server
;
6509 nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
6510 &data
->args
.seq_args
, &data
->res
.seq_res
, task
);
6511 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6512 data
->timestamp
= jiffies
;
6515 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6517 struct nfs_release_lockowner_data
*data
= calldata
;
6518 struct nfs_server
*server
= data
->server
;
6520 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6522 switch (task
->tk_status
) {
6524 renew_lease(server
, data
->timestamp
);
6526 case -NFS4ERR_STALE_CLIENTID
:
6527 case -NFS4ERR_EXPIRED
:
6528 nfs4_schedule_lease_recovery(server
->nfs_client
);
6530 case -NFS4ERR_LEASE_MOVED
:
6531 case -NFS4ERR_DELAY
:
6532 if (nfs4_async_handle_error(task
, server
,
6533 NULL
, NULL
) == -EAGAIN
)
6534 rpc_restart_call_prepare(task
);
6538 static void nfs4_release_lockowner_release(void *calldata
)
6540 struct nfs_release_lockowner_data
*data
= calldata
;
6541 nfs4_free_lock_state(data
->server
, data
->lsp
);
6545 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6546 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6547 .rpc_call_done
= nfs4_release_lockowner_done
,
6548 .rpc_release
= nfs4_release_lockowner_release
,
6552 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6554 struct nfs_release_lockowner_data
*data
;
6555 struct rpc_message msg
= {
6556 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6559 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6562 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6566 data
->server
= server
;
6567 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6568 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6569 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6571 msg
.rpc_argp
= &data
->args
;
6572 msg
.rpc_resp
= &data
->res
;
6573 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6574 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6577 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6579 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6580 struct dentry
*unused
, struct inode
*inode
,
6581 const char *key
, const void *buf
,
6582 size_t buflen
, int flags
)
6584 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6587 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6588 struct dentry
*unused
, struct inode
*inode
,
6589 const char *key
, void *buf
, size_t buflen
)
6591 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6594 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6596 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6599 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6601 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6602 struct dentry
*unused
, struct inode
*inode
,
6603 const char *key
, const void *buf
,
6604 size_t buflen
, int flags
)
6606 if (security_ismaclabel(key
))
6607 return nfs4_set_security_label(inode
, buf
, buflen
);
6612 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6613 struct dentry
*unused
, struct inode
*inode
,
6614 const char *key
, void *buf
, size_t buflen
)
6616 if (security_ismaclabel(key
))
6617 return nfs4_get_security_label(inode
, buf
, buflen
);
6622 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6626 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6627 len
= security_inode_listsecurity(inode
, list
, list_len
);
6628 if (list_len
&& len
> list_len
)
6634 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6635 .prefix
= XATTR_SECURITY_PREFIX
,
6636 .get
= nfs4_xattr_get_nfs4_label
,
6637 .set
= nfs4_xattr_set_nfs4_label
,
6643 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6651 * nfs_fhget will use either the mounted_on_fileid or the fileid
6653 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6655 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6656 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6657 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6658 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6661 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6662 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6663 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6667 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6668 const struct qstr
*name
,
6669 struct nfs4_fs_locations
*fs_locations
,
6672 struct nfs_server
*server
= NFS_SERVER(dir
);
6674 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6676 struct nfs4_fs_locations_arg args
= {
6677 .dir_fh
= NFS_FH(dir
),
6682 struct nfs4_fs_locations_res res
= {
6683 .fs_locations
= fs_locations
,
6685 struct rpc_message msg
= {
6686 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6692 dprintk("%s: start\n", __func__
);
6694 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6695 * is not supported */
6696 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6697 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6699 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6701 nfs_fattr_init(&fs_locations
->fattr
);
6702 fs_locations
->server
= server
;
6703 fs_locations
->nlocations
= 0;
6704 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6705 dprintk("%s: returned status = %d\n", __func__
, status
);
6709 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6710 const struct qstr
*name
,
6711 struct nfs4_fs_locations
*fs_locations
,
6714 struct nfs4_exception exception
= { };
6717 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6718 fs_locations
, page
);
6719 trace_nfs4_get_fs_locations(dir
, name
, err
);
6720 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6722 } while (exception
.retry
);
6727 * This operation also signals the server that this client is
6728 * performing migration recovery. The server can stop returning
6729 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6730 * appended to this compound to identify the client ID which is
6731 * performing recovery.
6733 static int _nfs40_proc_get_locations(struct inode
*inode
,
6734 struct nfs4_fs_locations
*locations
,
6735 struct page
*page
, struct rpc_cred
*cred
)
6737 struct nfs_server
*server
= NFS_SERVER(inode
);
6738 struct rpc_clnt
*clnt
= server
->client
;
6740 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6742 struct nfs4_fs_locations_arg args
= {
6743 .clientid
= server
->nfs_client
->cl_clientid
,
6744 .fh
= NFS_FH(inode
),
6747 .migration
= 1, /* skip LOOKUP */
6748 .renew
= 1, /* append RENEW */
6750 struct nfs4_fs_locations_res res
= {
6751 .fs_locations
= locations
,
6755 struct rpc_message msg
= {
6756 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6761 unsigned long now
= jiffies
;
6764 nfs_fattr_init(&locations
->fattr
);
6765 locations
->server
= server
;
6766 locations
->nlocations
= 0;
6768 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6769 nfs4_set_sequence_privileged(&args
.seq_args
);
6770 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6771 &args
.seq_args
, &res
.seq_res
);
6775 renew_lease(server
, now
);
6779 #ifdef CONFIG_NFS_V4_1
6782 * This operation also signals the server that this client is
6783 * performing migration recovery. The server can stop asserting
6784 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6785 * performing this operation is identified in the SEQUENCE
6786 * operation in this compound.
6788 * When the client supports GETATTR(fs_locations_info), it can
6789 * be plumbed in here.
6791 static int _nfs41_proc_get_locations(struct inode
*inode
,
6792 struct nfs4_fs_locations
*locations
,
6793 struct page
*page
, struct rpc_cred
*cred
)
6795 struct nfs_server
*server
= NFS_SERVER(inode
);
6796 struct rpc_clnt
*clnt
= server
->client
;
6798 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6800 struct nfs4_fs_locations_arg args
= {
6801 .fh
= NFS_FH(inode
),
6804 .migration
= 1, /* skip LOOKUP */
6806 struct nfs4_fs_locations_res res
= {
6807 .fs_locations
= locations
,
6810 struct rpc_message msg
= {
6811 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6818 nfs_fattr_init(&locations
->fattr
);
6819 locations
->server
= server
;
6820 locations
->nlocations
= 0;
6822 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6823 nfs4_set_sequence_privileged(&args
.seq_args
);
6824 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6825 &args
.seq_args
, &res
.seq_res
);
6826 if (status
== NFS4_OK
&&
6827 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6828 status
= -NFS4ERR_LEASE_MOVED
;
6832 #endif /* CONFIG_NFS_V4_1 */
6835 * nfs4_proc_get_locations - discover locations for a migrated FSID
6836 * @inode: inode on FSID that is migrating
6837 * @locations: result of query
6839 * @cred: credential to use for this operation
6841 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6842 * operation failed, or a negative errno if a local error occurred.
6844 * On success, "locations" is filled in, but if the server has
6845 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6848 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6849 * from this client that require migration recovery.
6851 int nfs4_proc_get_locations(struct inode
*inode
,
6852 struct nfs4_fs_locations
*locations
,
6853 struct page
*page
, struct rpc_cred
*cred
)
6855 struct nfs_server
*server
= NFS_SERVER(inode
);
6856 struct nfs_client
*clp
= server
->nfs_client
;
6857 const struct nfs4_mig_recovery_ops
*ops
=
6858 clp
->cl_mvops
->mig_recovery_ops
;
6859 struct nfs4_exception exception
= { };
6862 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6863 (unsigned long long)server
->fsid
.major
,
6864 (unsigned long long)server
->fsid
.minor
,
6866 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6869 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6870 if (status
!= -NFS4ERR_DELAY
)
6872 nfs4_handle_exception(server
, status
, &exception
);
6873 } while (exception
.retry
);
6878 * This operation also signals the server that this client is
6879 * performing "lease moved" recovery. The server can stop
6880 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6881 * is appended to this compound to identify the client ID which is
6882 * performing recovery.
6884 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6886 struct nfs_server
*server
= NFS_SERVER(inode
);
6887 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
6888 struct rpc_clnt
*clnt
= server
->client
;
6889 struct nfs4_fsid_present_arg args
= {
6890 .fh
= NFS_FH(inode
),
6891 .clientid
= clp
->cl_clientid
,
6892 .renew
= 1, /* append RENEW */
6894 struct nfs4_fsid_present_res res
= {
6897 struct rpc_message msg
= {
6898 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6903 unsigned long now
= jiffies
;
6906 res
.fh
= nfs_alloc_fhandle();
6910 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6911 nfs4_set_sequence_privileged(&args
.seq_args
);
6912 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6913 &args
.seq_args
, &res
.seq_res
);
6914 nfs_free_fhandle(res
.fh
);
6918 do_renew_lease(clp
, now
);
6922 #ifdef CONFIG_NFS_V4_1
6925 * This operation also signals the server that this client is
6926 * performing "lease moved" recovery. The server can stop asserting
6927 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6928 * this operation is identified in the SEQUENCE operation in this
6931 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6933 struct nfs_server
*server
= NFS_SERVER(inode
);
6934 struct rpc_clnt
*clnt
= server
->client
;
6935 struct nfs4_fsid_present_arg args
= {
6936 .fh
= NFS_FH(inode
),
6938 struct nfs4_fsid_present_res res
= {
6940 struct rpc_message msg
= {
6941 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6948 res
.fh
= nfs_alloc_fhandle();
6952 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6953 nfs4_set_sequence_privileged(&args
.seq_args
);
6954 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6955 &args
.seq_args
, &res
.seq_res
);
6956 nfs_free_fhandle(res
.fh
);
6957 if (status
== NFS4_OK
&&
6958 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6959 status
= -NFS4ERR_LEASE_MOVED
;
6963 #endif /* CONFIG_NFS_V4_1 */
6966 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6967 * @inode: inode on FSID to check
6968 * @cred: credential to use for this operation
6970 * Server indicates whether the FSID is present, moved, or not
6971 * recognized. This operation is necessary to clear a LEASE_MOVED
6972 * condition for this client ID.
6974 * Returns NFS4_OK if the FSID is present on this server,
6975 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6976 * NFS4ERR code if some error occurred on the server, or a
6977 * negative errno if a local failure occurred.
6979 int nfs4_proc_fsid_present(struct inode
*inode
, 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
->fsid_present(inode
, cred
);
6996 if (status
!= -NFS4ERR_DELAY
)
6998 nfs4_handle_exception(server
, status
, &exception
);
6999 } while (exception
.retry
);
7004 * If 'use_integrity' is true and the state managment nfs_client
7005 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7006 * and the machine credential as per RFC3530bis and RFC5661 Security
7007 * Considerations sections. Otherwise, just use the user cred with the
7008 * filesystem's rpc_client.
7010 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7013 struct nfs4_secinfo_arg args
= {
7014 .dir_fh
= NFS_FH(dir
),
7017 struct nfs4_secinfo_res res
= {
7020 struct rpc_message msg
= {
7021 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
7025 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
7026 struct rpc_cred
*cred
= NULL
;
7028 if (use_integrity
) {
7029 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
7030 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
7031 msg
.rpc_cred
= cred
;
7034 dprintk("NFS call secinfo %s\n", name
->name
);
7036 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
7037 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
7039 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
7041 dprintk("NFS reply secinfo: %d\n", status
);
7049 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
7050 struct nfs4_secinfo_flavors
*flavors
)
7052 struct nfs4_exception exception
= { };
7055 err
= -NFS4ERR_WRONGSEC
;
7057 /* try to use integrity protection with machine cred */
7058 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
7059 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
7062 * if unable to use integrity protection, or SECINFO with
7063 * integrity protection returns NFS4ERR_WRONGSEC (which is
7064 * disallowed by spec, but exists in deployed servers) use
7065 * the current filesystem's rpc_client and the user cred.
7067 if (err
== -NFS4ERR_WRONGSEC
)
7068 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
7070 trace_nfs4_secinfo(dir
, name
, err
);
7071 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
7073 } while (exception
.retry
);
7077 #ifdef CONFIG_NFS_V4_1
7079 * Check the exchange flags returned by the server for invalid flags, having
7080 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7083 static int nfs4_check_cl_exchange_flags(u32 flags
)
7085 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
7087 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
7088 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
7090 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
7094 return -NFS4ERR_INVAL
;
7098 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
7099 struct nfs41_server_scope
*b
)
7101 if (a
->server_scope_sz
== b
->server_scope_sz
&&
7102 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
7109 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
7113 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
7114 .rpc_call_done
= &nfs4_bind_one_conn_to_session_done
,
7118 * nfs4_proc_bind_one_conn_to_session()
7120 * The 4.1 client currently uses the same TCP connection for the
7121 * fore and backchannel.
7124 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
7125 struct rpc_xprt
*xprt
,
7126 struct nfs_client
*clp
,
7127 struct rpc_cred
*cred
)
7130 struct nfs41_bind_conn_to_session_args args
= {
7132 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
7134 struct nfs41_bind_conn_to_session_res res
;
7135 struct rpc_message msg
= {
7137 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
7142 struct rpc_task_setup task_setup_data
= {
7145 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
7146 .rpc_message
= &msg
,
7147 .flags
= RPC_TASK_TIMEOUT
,
7149 struct rpc_task
*task
;
7151 dprintk("--> %s\n", __func__
);
7153 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
7154 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
7155 args
.dir
= NFS4_CDFC4_FORE
;
7157 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7158 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
7159 args
.dir
= NFS4_CDFC4_FORE
;
7161 task
= rpc_run_task(&task_setup_data
);
7162 if (!IS_ERR(task
)) {
7163 status
= task
->tk_status
;
7166 status
= PTR_ERR(task
);
7167 trace_nfs4_bind_conn_to_session(clp
, status
);
7169 if (memcmp(res
.sessionid
.data
,
7170 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
7171 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
7175 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
7176 dprintk("NFS: %s: Unexpected direction from server\n",
7181 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
7182 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7189 dprintk("<-- %s status= %d\n", __func__
, status
);
7193 struct rpc_bind_conn_calldata
{
7194 struct nfs_client
*clp
;
7195 struct rpc_cred
*cred
;
7199 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
7200 struct rpc_xprt
*xprt
,
7203 struct rpc_bind_conn_calldata
*p
= calldata
;
7205 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
7208 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7210 struct rpc_bind_conn_calldata data
= {
7214 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
7215 nfs4_proc_bind_conn_to_session_callback
, &data
);
7219 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7220 * and operations we'd like to see to enable certain features in the allow map
7222 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
7223 .how
= SP4_MACH_CRED
,
7224 .enforce
.u
.words
= {
7225 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7226 1 << (OP_EXCHANGE_ID
- 32) |
7227 1 << (OP_CREATE_SESSION
- 32) |
7228 1 << (OP_DESTROY_SESSION
- 32) |
7229 1 << (OP_DESTROY_CLIENTID
- 32)
7232 [0] = 1 << (OP_CLOSE
) |
7233 1 << (OP_OPEN_DOWNGRADE
) |
7235 1 << (OP_DELEGRETURN
) |
7237 [1] = 1 << (OP_SECINFO
- 32) |
7238 1 << (OP_SECINFO_NO_NAME
- 32) |
7239 1 << (OP_LAYOUTRETURN
- 32) |
7240 1 << (OP_TEST_STATEID
- 32) |
7241 1 << (OP_FREE_STATEID
- 32) |
7242 1 << (OP_WRITE
- 32)
7247 * Select the state protection mode for client `clp' given the server results
7248 * from exchange_id in `sp'.
7250 * Returns 0 on success, negative errno otherwise.
7252 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
7253 struct nfs41_state_protection
*sp
)
7255 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
7256 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7257 1 << (OP_EXCHANGE_ID
- 32) |
7258 1 << (OP_CREATE_SESSION
- 32) |
7259 1 << (OP_DESTROY_SESSION
- 32) |
7260 1 << (OP_DESTROY_CLIENTID
- 32)
7264 if (sp
->how
== SP4_MACH_CRED
) {
7265 /* Print state protect result */
7266 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
7267 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
7268 if (test_bit(i
, sp
->enforce
.u
.longs
))
7269 dfprintk(MOUNT
, " enforce op %d\n", i
);
7270 if (test_bit(i
, sp
->allow
.u
.longs
))
7271 dfprintk(MOUNT
, " allow op %d\n", i
);
7274 /* make sure nothing is on enforce list that isn't supported */
7275 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
7276 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
7277 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7283 * Minimal mode - state operations are allowed to use machine
7284 * credential. Note this already happens by default, so the
7285 * client doesn't have to do anything more than the negotiation.
7287 * NOTE: we don't care if EXCHANGE_ID is in the list -
7288 * we're already using the machine cred for exchange_id
7289 * and will never use a different cred.
7291 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
7292 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
7293 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
7294 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
7295 dfprintk(MOUNT
, "sp4_mach_cred:\n");
7296 dfprintk(MOUNT
, " minimal mode enabled\n");
7297 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
7299 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7303 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7304 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7305 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7306 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7307 dfprintk(MOUNT
, " cleanup mode enabled\n");
7308 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
7311 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7312 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7313 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
7314 &clp
->cl_sp4_flags
);
7317 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7318 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7319 dfprintk(MOUNT
, " secinfo mode enabled\n");
7320 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
7323 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7324 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7325 dfprintk(MOUNT
, " stateid mode enabled\n");
7326 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
7329 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7330 dfprintk(MOUNT
, " write mode enabled\n");
7331 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
7334 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7335 dfprintk(MOUNT
, " commit mode enabled\n");
7336 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
7343 struct nfs41_exchange_id_data
{
7344 struct nfs41_exchange_id_res res
;
7345 struct nfs41_exchange_id_args args
;
7346 struct rpc_xprt
*xprt
;
7350 static void nfs4_exchange_id_done(struct rpc_task
*task
, void *data
)
7352 struct nfs41_exchange_id_data
*cdata
=
7353 (struct nfs41_exchange_id_data
*)data
;
7354 struct nfs_client
*clp
= cdata
->args
.client
;
7355 int status
= task
->tk_status
;
7357 trace_nfs4_exchange_id(clp
, status
);
7360 status
= nfs4_check_cl_exchange_flags(cdata
->res
.flags
);
7362 if (cdata
->xprt
&& status
== 0) {
7363 status
= nfs4_detect_session_trunking(clp
, &cdata
->res
,
7369 status
= nfs4_sp4_select_mode(clp
, &cdata
->res
.state_protect
);
7372 clp
->cl_clientid
= cdata
->res
.clientid
;
7373 clp
->cl_exchange_flags
= cdata
->res
.flags
;
7374 /* Client ID is not confirmed */
7375 if (!(cdata
->res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
)) {
7376 clear_bit(NFS4_SESSION_ESTABLISHED
,
7377 &clp
->cl_session
->session_state
);
7378 clp
->cl_seqid
= cdata
->res
.seqid
;
7381 kfree(clp
->cl_serverowner
);
7382 clp
->cl_serverowner
= cdata
->res
.server_owner
;
7383 cdata
->res
.server_owner
= NULL
;
7385 /* use the most recent implementation id */
7386 kfree(clp
->cl_implid
);
7387 clp
->cl_implid
= cdata
->res
.impl_id
;
7388 cdata
->res
.impl_id
= NULL
;
7390 if (clp
->cl_serverscope
!= NULL
&&
7391 !nfs41_same_server_scope(clp
->cl_serverscope
,
7392 cdata
->res
.server_scope
)) {
7393 dprintk("%s: server_scope mismatch detected\n",
7395 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7396 kfree(clp
->cl_serverscope
);
7397 clp
->cl_serverscope
= NULL
;
7400 if (clp
->cl_serverscope
== NULL
) {
7401 clp
->cl_serverscope
= cdata
->res
.server_scope
;
7402 cdata
->res
.server_scope
= NULL
;
7404 /* Save the EXCHANGE_ID verifier session trunk tests */
7405 memcpy(clp
->cl_confirm
.data
, cdata
->args
.verifier
->data
,
7406 sizeof(clp
->cl_confirm
.data
));
7409 cdata
->rpc_status
= status
;
7413 static void nfs4_exchange_id_release(void *data
)
7415 struct nfs41_exchange_id_data
*cdata
=
7416 (struct nfs41_exchange_id_data
*)data
;
7418 nfs_put_client(cdata
->args
.client
);
7420 xprt_put(cdata
->xprt
);
7421 rpc_clnt_xprt_switch_put(cdata
->args
.client
->cl_rpcclient
);
7423 kfree(cdata
->res
.impl_id
);
7424 kfree(cdata
->res
.server_scope
);
7425 kfree(cdata
->res
.server_owner
);
7429 static const struct rpc_call_ops nfs4_exchange_id_call_ops
= {
7430 .rpc_call_done
= nfs4_exchange_id_done
,
7431 .rpc_release
= nfs4_exchange_id_release
,
7435 * _nfs4_proc_exchange_id()
7437 * Wrapper for EXCHANGE_ID operation.
7439 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7440 u32 sp4_how
, struct rpc_xprt
*xprt
)
7442 nfs4_verifier verifier
;
7443 struct rpc_message msg
= {
7444 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7447 struct rpc_task_setup task_setup_data
= {
7448 .rpc_client
= clp
->cl_rpcclient
,
7449 .callback_ops
= &nfs4_exchange_id_call_ops
,
7450 .rpc_message
= &msg
,
7451 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7453 struct nfs41_exchange_id_data
*calldata
;
7454 struct rpc_task
*task
;
7457 if (!atomic_inc_not_zero(&clp
->cl_count
))
7461 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7466 nfs4_init_boot_verifier(clp
, &verifier
);
7468 status
= nfs4_init_uniform_client_string(clp
);
7472 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7473 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
7476 calldata
->res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7479 if (unlikely(calldata
->res
.server_owner
== NULL
))
7482 calldata
->res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7484 if (unlikely(calldata
->res
.server_scope
== NULL
))
7485 goto out_server_owner
;
7487 calldata
->res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7488 if (unlikely(calldata
->res
.impl_id
== NULL
))
7489 goto out_server_scope
;
7493 calldata
->args
.state_protect
.how
= SP4_NONE
;
7497 calldata
->args
.state_protect
= nfs4_sp4_mach_cred_request
;
7507 calldata
->xprt
= xprt
;
7508 task_setup_data
.rpc_xprt
= xprt
;
7509 task_setup_data
.flags
=
7510 RPC_TASK_SOFT
|RPC_TASK_SOFTCONN
|RPC_TASK_ASYNC
;
7511 calldata
->args
.verifier
= &clp
->cl_confirm
;
7513 calldata
->args
.verifier
= &verifier
;
7515 calldata
->args
.client
= clp
;
7516 #ifdef CONFIG_NFS_V4_1_MIGRATION
7517 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7518 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
7519 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
7521 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7522 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
7524 msg
.rpc_argp
= &calldata
->args
;
7525 msg
.rpc_resp
= &calldata
->res
;
7526 task_setup_data
.callback_data
= calldata
;
7528 task
= rpc_run_task(&task_setup_data
);
7530 status
= PTR_ERR(task
);
7535 status
= rpc_wait_for_completion_task(task
);
7537 status
= calldata
->rpc_status
;
7538 } else /* session trunking test */
7539 status
= calldata
->rpc_status
;
7543 if (clp
->cl_implid
!= NULL
)
7544 dprintk("NFS reply exchange_id: Server Implementation ID: "
7545 "domain: %s, name: %s, date: %llu,%u\n",
7546 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
7547 clp
->cl_implid
->date
.seconds
,
7548 clp
->cl_implid
->date
.nseconds
);
7549 dprintk("NFS reply exchange_id: %d\n", status
);
7553 kfree(calldata
->res
.impl_id
);
7555 kfree(calldata
->res
.server_scope
);
7557 kfree(calldata
->res
.server_owner
);
7564 * nfs4_proc_exchange_id()
7566 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7568 * Since the clientid has expired, all compounds using sessions
7569 * associated with the stale clientid will be returning
7570 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7571 * be in some phase of session reset.
7573 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7575 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7577 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7580 /* try SP4_MACH_CRED if krb5i/p */
7581 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7582 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7583 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
, NULL
);
7589 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
, NULL
);
7593 * nfs4_test_session_trunk
7595 * This is an add_xprt_test() test function called from
7596 * rpc_clnt_setup_test_and_add_xprt.
7598 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7599 * and is dereferrenced in nfs4_exchange_id_release
7601 * Upon success, add the new transport to the rpc_clnt
7603 * @clnt: struct rpc_clnt to get new transport
7604 * @xprt: the rpc_xprt to test
7605 * @data: call data for _nfs4_proc_exchange_id.
7607 int nfs4_test_session_trunk(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
,
7610 struct nfs4_add_xprt_data
*adata
= (struct nfs4_add_xprt_data
*)data
;
7613 dprintk("--> %s try %s\n", __func__
,
7614 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
7616 sp4_how
= (adata
->clp
->cl_sp4_flags
== 0 ? SP4_NONE
: SP4_MACH_CRED
);
7618 /* Test connection for session trunking. Async exchange_id call */
7619 return _nfs4_proc_exchange_id(adata
->clp
, adata
->cred
, sp4_how
, xprt
);
7621 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk
);
7623 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7624 struct rpc_cred
*cred
)
7626 struct rpc_message msg
= {
7627 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7633 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7634 trace_nfs4_destroy_clientid(clp
, status
);
7636 dprintk("NFS: Got error %d from the server %s on "
7637 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7641 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7642 struct rpc_cred
*cred
)
7647 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7648 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7650 case -NFS4ERR_DELAY
:
7651 case -NFS4ERR_CLIENTID_BUSY
:
7661 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7663 struct rpc_cred
*cred
;
7666 if (clp
->cl_mvops
->minor_version
< 1)
7668 if (clp
->cl_exchange_flags
== 0)
7670 if (clp
->cl_preserve_clid
)
7672 cred
= nfs4_get_clid_cred(clp
);
7673 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7678 case -NFS4ERR_STALE_CLIENTID
:
7679 clp
->cl_exchange_flags
= 0;
7685 struct nfs4_get_lease_time_data
{
7686 struct nfs4_get_lease_time_args
*args
;
7687 struct nfs4_get_lease_time_res
*res
;
7688 struct nfs_client
*clp
;
7691 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7694 struct nfs4_get_lease_time_data
*data
=
7695 (struct nfs4_get_lease_time_data
*)calldata
;
7697 dprintk("--> %s\n", __func__
);
7698 /* just setup sequence, do not trigger session recovery
7699 since we're invoked within one */
7700 nfs41_setup_sequence(data
->clp
->cl_session
,
7701 &data
->args
->la_seq_args
,
7702 &data
->res
->lr_seq_res
,
7704 dprintk("<-- %s\n", __func__
);
7708 * Called from nfs4_state_manager thread for session setup, so don't recover
7709 * from sequence operation or clientid errors.
7711 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7713 struct nfs4_get_lease_time_data
*data
=
7714 (struct nfs4_get_lease_time_data
*)calldata
;
7716 dprintk("--> %s\n", __func__
);
7717 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7719 switch (task
->tk_status
) {
7720 case -NFS4ERR_DELAY
:
7721 case -NFS4ERR_GRACE
:
7722 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7723 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7724 task
->tk_status
= 0;
7726 case -NFS4ERR_RETRY_UNCACHED_REP
:
7727 rpc_restart_call_prepare(task
);
7730 dprintk("<-- %s\n", __func__
);
7733 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7734 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7735 .rpc_call_done
= nfs4_get_lease_time_done
,
7738 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7740 struct rpc_task
*task
;
7741 struct nfs4_get_lease_time_args args
;
7742 struct nfs4_get_lease_time_res res
= {
7743 .lr_fsinfo
= fsinfo
,
7745 struct nfs4_get_lease_time_data data
= {
7750 struct rpc_message msg
= {
7751 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7755 struct rpc_task_setup task_setup
= {
7756 .rpc_client
= clp
->cl_rpcclient
,
7757 .rpc_message
= &msg
,
7758 .callback_ops
= &nfs4_get_lease_time_ops
,
7759 .callback_data
= &data
,
7760 .flags
= RPC_TASK_TIMEOUT
,
7764 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7765 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7766 dprintk("--> %s\n", __func__
);
7767 task
= rpc_run_task(&task_setup
);
7770 status
= PTR_ERR(task
);
7772 status
= task
->tk_status
;
7775 dprintk("<-- %s return %d\n", __func__
, status
);
7781 * Initialize the values to be used by the client in CREATE_SESSION
7782 * If nfs4_init_session set the fore channel request and response sizes,
7785 * Set the back channel max_resp_sz_cached to zero to force the client to
7786 * always set csa_cachethis to FALSE because the current implementation
7787 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7789 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7790 struct rpc_clnt
*clnt
)
7792 unsigned int max_rqst_sz
, max_resp_sz
;
7793 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7795 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7796 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7798 /* Fore channel attributes */
7799 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7800 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7801 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7802 args
->fc_attrs
.max_reqs
= max_session_slots
;
7804 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7805 "max_ops=%u max_reqs=%u\n",
7807 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7808 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7810 /* Back channel attributes */
7811 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7812 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7813 args
->bc_attrs
.max_resp_sz_cached
= 0;
7814 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7815 args
->bc_attrs
.max_reqs
= min_t(unsigned short, max_session_cb_slots
, 1);
7817 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7818 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7820 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7821 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7822 args
->bc_attrs
.max_reqs
);
7825 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7826 struct nfs41_create_session_res
*res
)
7828 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7829 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7831 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7834 * Our requested max_ops is the minimum we need; we're not
7835 * prepared to break up compounds into smaller pieces than that.
7836 * So, no point even trying to continue if the server won't
7839 if (rcvd
->max_ops
< sent
->max_ops
)
7841 if (rcvd
->max_reqs
== 0)
7843 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7844 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7848 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7849 struct nfs41_create_session_res
*res
)
7851 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7852 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7854 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7856 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7858 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7860 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7862 if (rcvd
->max_ops
> sent
->max_ops
)
7864 if (rcvd
->max_reqs
> sent
->max_reqs
)
7870 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7871 struct nfs41_create_session_res
*res
)
7875 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
7878 return nfs4_verify_back_channel_attrs(args
, res
);
7881 static void nfs4_update_session(struct nfs4_session
*session
,
7882 struct nfs41_create_session_res
*res
)
7884 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
7885 /* Mark client id and session as being confirmed */
7886 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
7887 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
7888 session
->flags
= res
->flags
;
7889 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
7890 if (res
->flags
& SESSION4_BACK_CHAN
)
7891 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
7892 sizeof(session
->bc_attrs
));
7895 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
7896 struct rpc_cred
*cred
)
7898 struct nfs4_session
*session
= clp
->cl_session
;
7899 struct nfs41_create_session_args args
= {
7901 .clientid
= clp
->cl_clientid
,
7902 .seqid
= clp
->cl_seqid
,
7903 .cb_program
= NFS4_CALLBACK
,
7905 struct nfs41_create_session_res res
;
7907 struct rpc_message msg
= {
7908 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
7915 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
7916 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
7918 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7919 trace_nfs4_create_session(clp
, status
);
7922 case -NFS4ERR_STALE_CLIENTID
:
7923 case -NFS4ERR_DELAY
:
7932 /* Verify the session's negotiated channel_attrs values */
7933 status
= nfs4_verify_channel_attrs(&args
, &res
);
7934 /* Increment the clientid slot sequence id */
7937 nfs4_update_session(session
, &res
);
7944 * Issues a CREATE_SESSION operation to the server.
7945 * It is the responsibility of the caller to verify the session is
7946 * expired before calling this routine.
7948 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7952 struct nfs4_session
*session
= clp
->cl_session
;
7954 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
7956 status
= _nfs4_proc_create_session(clp
, cred
);
7960 /* Init or reset the session slot tables */
7961 status
= nfs4_setup_session_slot_tables(session
);
7962 dprintk("slot table setup returned %d\n", status
);
7966 ptr
= (unsigned *)&session
->sess_id
.data
[0];
7967 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
7968 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
7970 dprintk("<-- %s\n", __func__
);
7975 * Issue the over-the-wire RPC DESTROY_SESSION.
7976 * The caller must serialize access to this routine.
7978 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
7979 struct rpc_cred
*cred
)
7981 struct rpc_message msg
= {
7982 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
7983 .rpc_argp
= session
,
7988 dprintk("--> nfs4_proc_destroy_session\n");
7990 /* session is still being setup */
7991 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
7994 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7995 trace_nfs4_destroy_session(session
->clp
, status
);
7998 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7999 "Session has been destroyed regardless...\n", status
);
8001 dprintk("<-- nfs4_proc_destroy_session\n");
8006 * Renew the cl_session lease.
8008 struct nfs4_sequence_data
{
8009 struct nfs_client
*clp
;
8010 struct nfs4_sequence_args args
;
8011 struct nfs4_sequence_res res
;
8014 static void nfs41_sequence_release(void *data
)
8016 struct nfs4_sequence_data
*calldata
= data
;
8017 struct nfs_client
*clp
= calldata
->clp
;
8019 if (atomic_read(&clp
->cl_count
) > 1)
8020 nfs4_schedule_state_renewal(clp
);
8021 nfs_put_client(clp
);
8025 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8027 switch(task
->tk_status
) {
8028 case -NFS4ERR_DELAY
:
8029 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8032 nfs4_schedule_lease_recovery(clp
);
8037 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
8039 struct nfs4_sequence_data
*calldata
= data
;
8040 struct nfs_client
*clp
= calldata
->clp
;
8042 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
8045 trace_nfs4_sequence(clp
, task
->tk_status
);
8046 if (task
->tk_status
< 0) {
8047 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
8048 if (atomic_read(&clp
->cl_count
) == 1)
8051 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
8052 rpc_restart_call_prepare(task
);
8056 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
8058 dprintk("<-- %s\n", __func__
);
8061 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
8063 struct nfs4_sequence_data
*calldata
= data
;
8064 struct nfs_client
*clp
= calldata
->clp
;
8065 struct nfs4_sequence_args
*args
;
8066 struct nfs4_sequence_res
*res
;
8068 args
= task
->tk_msg
.rpc_argp
;
8069 res
= task
->tk_msg
.rpc_resp
;
8071 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
8074 static const struct rpc_call_ops nfs41_sequence_ops
= {
8075 .rpc_call_done
= nfs41_sequence_call_done
,
8076 .rpc_call_prepare
= nfs41_sequence_prepare
,
8077 .rpc_release
= nfs41_sequence_release
,
8080 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
8081 struct rpc_cred
*cred
,
8084 struct nfs4_sequence_data
*calldata
;
8085 struct rpc_message msg
= {
8086 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
8089 struct rpc_task_setup task_setup_data
= {
8090 .rpc_client
= clp
->cl_rpcclient
,
8091 .rpc_message
= &msg
,
8092 .callback_ops
= &nfs41_sequence_ops
,
8093 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
8096 if (!atomic_inc_not_zero(&clp
->cl_count
))
8097 return ERR_PTR(-EIO
);
8098 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8099 if (calldata
== NULL
) {
8100 nfs_put_client(clp
);
8101 return ERR_PTR(-ENOMEM
);
8103 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
8105 nfs4_set_sequence_privileged(&calldata
->args
);
8106 msg
.rpc_argp
= &calldata
->args
;
8107 msg
.rpc_resp
= &calldata
->res
;
8108 calldata
->clp
= clp
;
8109 task_setup_data
.callback_data
= calldata
;
8111 return rpc_run_task(&task_setup_data
);
8114 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
8116 struct rpc_task
*task
;
8119 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
8121 task
= _nfs41_proc_sequence(clp
, cred
, false);
8123 ret
= PTR_ERR(task
);
8125 rpc_put_task_async(task
);
8126 dprintk("<-- %s status=%d\n", __func__
, ret
);
8130 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8132 struct rpc_task
*task
;
8135 task
= _nfs41_proc_sequence(clp
, cred
, true);
8137 ret
= PTR_ERR(task
);
8140 ret
= rpc_wait_for_completion_task(task
);
8142 ret
= task
->tk_status
;
8145 dprintk("<-- %s status=%d\n", __func__
, ret
);
8149 struct nfs4_reclaim_complete_data
{
8150 struct nfs_client
*clp
;
8151 struct nfs41_reclaim_complete_args arg
;
8152 struct nfs41_reclaim_complete_res res
;
8155 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
8157 struct nfs4_reclaim_complete_data
*calldata
= data
;
8159 nfs41_setup_sequence(calldata
->clp
->cl_session
,
8160 &calldata
->arg
.seq_args
,
8161 &calldata
->res
.seq_res
,
8165 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8167 switch(task
->tk_status
) {
8169 case -NFS4ERR_COMPLETE_ALREADY
:
8170 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
8172 case -NFS4ERR_DELAY
:
8173 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8175 case -NFS4ERR_RETRY_UNCACHED_REP
:
8178 nfs4_schedule_lease_recovery(clp
);
8183 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
8185 struct nfs4_reclaim_complete_data
*calldata
= data
;
8186 struct nfs_client
*clp
= calldata
->clp
;
8187 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
8189 dprintk("--> %s\n", __func__
);
8190 if (!nfs41_sequence_done(task
, res
))
8193 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
8194 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
8195 rpc_restart_call_prepare(task
);
8198 dprintk("<-- %s\n", __func__
);
8201 static void nfs4_free_reclaim_complete_data(void *data
)
8203 struct nfs4_reclaim_complete_data
*calldata
= data
;
8208 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
8209 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
8210 .rpc_call_done
= nfs4_reclaim_complete_done
,
8211 .rpc_release
= nfs4_free_reclaim_complete_data
,
8215 * Issue a global reclaim complete.
8217 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
8218 struct rpc_cred
*cred
)
8220 struct nfs4_reclaim_complete_data
*calldata
;
8221 struct rpc_task
*task
;
8222 struct rpc_message msg
= {
8223 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
8226 struct rpc_task_setup task_setup_data
= {
8227 .rpc_client
= clp
->cl_rpcclient
,
8228 .rpc_message
= &msg
,
8229 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
8230 .flags
= RPC_TASK_ASYNC
,
8232 int status
= -ENOMEM
;
8234 dprintk("--> %s\n", __func__
);
8235 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8236 if (calldata
== NULL
)
8238 calldata
->clp
= clp
;
8239 calldata
->arg
.one_fs
= 0;
8241 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
8242 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
8243 msg
.rpc_argp
= &calldata
->arg
;
8244 msg
.rpc_resp
= &calldata
->res
;
8245 task_setup_data
.callback_data
= calldata
;
8246 task
= rpc_run_task(&task_setup_data
);
8248 status
= PTR_ERR(task
);
8251 status
= nfs4_wait_for_completion_rpc_task(task
);
8253 status
= task
->tk_status
;
8257 dprintk("<-- %s status=%d\n", __func__
, status
);
8262 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
8264 struct nfs4_layoutget
*lgp
= calldata
;
8265 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
8266 struct nfs4_session
*session
= nfs4_get_session(server
);
8268 dprintk("--> %s\n", __func__
);
8269 nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
8270 &lgp
->res
.seq_res
, task
);
8271 dprintk("<-- %s\n", __func__
);
8274 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
8276 struct nfs4_layoutget
*lgp
= calldata
;
8278 dprintk("--> %s\n", __func__
);
8279 nfs41_sequence_process(task
, &lgp
->res
.seq_res
);
8280 dprintk("<-- %s\n", __func__
);
8284 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
8285 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
8287 struct inode
*inode
= lgp
->args
.inode
;
8288 struct nfs_server
*server
= NFS_SERVER(inode
);
8289 struct pnfs_layout_hdr
*lo
;
8290 int nfs4err
= task
->tk_status
;
8291 int err
, status
= 0;
8294 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
8301 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8302 * on the file. set tk_status to -ENODATA to tell upper layer to
8305 case -NFS4ERR_LAYOUTUNAVAILABLE
:
8309 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8310 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8312 case -NFS4ERR_BADLAYOUT
:
8313 status
= -EOVERFLOW
;
8316 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8317 * (or clients) writing to the same RAID stripe except when
8318 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8320 * Treat it like we would RECALLCONFLICT -- we retry for a little
8321 * while, and then eventually give up.
8323 case -NFS4ERR_LAYOUTTRYLATER
:
8324 if (lgp
->args
.minlength
== 0) {
8325 status
= -EOVERFLOW
;
8330 case -NFS4ERR_RECALLCONFLICT
:
8331 status
= -ERECALLCONFLICT
;
8333 case -NFS4ERR_DELEG_REVOKED
:
8334 case -NFS4ERR_ADMIN_REVOKED
:
8335 case -NFS4ERR_EXPIRED
:
8336 case -NFS4ERR_BAD_STATEID
:
8337 exception
->timeout
= 0;
8338 spin_lock(&inode
->i_lock
);
8339 lo
= NFS_I(inode
)->layout
;
8340 /* If the open stateid was bad, then recover it. */
8341 if (!lo
|| test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) ||
8342 nfs4_stateid_match_other(&lgp
->args
.stateid
,
8343 &lgp
->args
.ctx
->state
->stateid
)) {
8344 spin_unlock(&inode
->i_lock
);
8345 exception
->state
= lgp
->args
.ctx
->state
;
8346 exception
->stateid
= &lgp
->args
.stateid
;
8351 * Mark the bad layout state as invalid, then retry
8353 pnfs_mark_layout_stateid_invalid(lo
, &head
);
8354 spin_unlock(&inode
->i_lock
);
8355 pnfs_free_lseg_list(&head
);
8360 err
= nfs4_handle_exception(server
, nfs4err
, exception
);
8362 if (exception
->retry
)
8368 dprintk("<-- %s\n", __func__
);
8372 static size_t max_response_pages(struct nfs_server
*server
)
8374 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
8375 return nfs_page_array_len(0, max_resp_sz
);
8378 static void nfs4_free_pages(struct page
**pages
, size_t size
)
8385 for (i
= 0; i
< size
; i
++) {
8388 __free_page(pages
[i
]);
8393 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
8395 struct page
**pages
;
8398 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
8400 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
8404 for (i
= 0; i
< size
; i
++) {
8405 pages
[i
] = alloc_page(gfp_flags
);
8407 dprintk("%s: failed to allocate page\n", __func__
);
8408 nfs4_free_pages(pages
, size
);
8416 static void nfs4_layoutget_release(void *calldata
)
8418 struct nfs4_layoutget
*lgp
= calldata
;
8419 struct inode
*inode
= lgp
->args
.inode
;
8420 struct nfs_server
*server
= NFS_SERVER(inode
);
8421 size_t max_pages
= max_response_pages(server
);
8423 dprintk("--> %s\n", __func__
);
8424 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8425 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8426 put_nfs_open_context(lgp
->args
.ctx
);
8428 dprintk("<-- %s\n", __func__
);
8431 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8432 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8433 .rpc_call_done
= nfs4_layoutget_done
,
8434 .rpc_release
= nfs4_layoutget_release
,
8437 struct pnfs_layout_segment
*
8438 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8440 struct inode
*inode
= lgp
->args
.inode
;
8441 struct nfs_server
*server
= NFS_SERVER(inode
);
8442 size_t max_pages
= max_response_pages(server
);
8443 struct rpc_task
*task
;
8444 struct rpc_message msg
= {
8445 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8446 .rpc_argp
= &lgp
->args
,
8447 .rpc_resp
= &lgp
->res
,
8448 .rpc_cred
= lgp
->cred
,
8450 struct rpc_task_setup task_setup_data
= {
8451 .rpc_client
= server
->client
,
8452 .rpc_message
= &msg
,
8453 .callback_ops
= &nfs4_layoutget_call_ops
,
8454 .callback_data
= lgp
,
8455 .flags
= RPC_TASK_ASYNC
,
8457 struct pnfs_layout_segment
*lseg
= NULL
;
8458 struct nfs4_exception exception
= {
8460 .timeout
= *timeout
,
8464 dprintk("--> %s\n", __func__
);
8466 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8467 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8469 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8470 if (!lgp
->args
.layout
.pages
) {
8471 nfs4_layoutget_release(lgp
);
8472 return ERR_PTR(-ENOMEM
);
8474 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8476 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8477 lgp
->res
.seq_res
.sr_slot
= NULL
;
8478 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8480 task
= rpc_run_task(&task_setup_data
);
8482 return ERR_CAST(task
);
8483 status
= nfs4_wait_for_completion_rpc_task(task
);
8485 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8486 *timeout
= exception
.timeout
;
8489 trace_nfs4_layoutget(lgp
->args
.ctx
,
8495 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8496 if (status
== 0 && lgp
->res
.layoutp
->len
)
8497 lseg
= pnfs_layout_process(lgp
);
8498 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8500 dprintk("<-- %s status=%d\n", __func__
, status
);
8502 return ERR_PTR(status
);
8507 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8509 struct nfs4_layoutreturn
*lrp
= calldata
;
8511 dprintk("--> %s\n", __func__
);
8512 nfs41_setup_sequence(lrp
->clp
->cl_session
,
8513 &lrp
->args
.seq_args
,
8518 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8520 struct nfs4_layoutreturn
*lrp
= calldata
;
8521 struct nfs_server
*server
;
8523 dprintk("--> %s\n", __func__
);
8525 if (!nfs41_sequence_process(task
, &lrp
->res
.seq_res
))
8528 server
= NFS_SERVER(lrp
->args
.inode
);
8529 switch (task
->tk_status
) {
8531 task
->tk_status
= 0;
8534 case -NFS4ERR_DELAY
:
8535 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8537 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8538 rpc_restart_call_prepare(task
);
8541 dprintk("<-- %s\n", __func__
);
8544 static void nfs4_layoutreturn_release(void *calldata
)
8546 struct nfs4_layoutreturn
*lrp
= calldata
;
8547 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8550 dprintk("--> %s\n", __func__
);
8551 spin_lock(&lo
->plh_inode
->i_lock
);
8552 if (lrp
->res
.lrs_present
) {
8553 pnfs_mark_matching_lsegs_invalid(lo
, &freeme
,
8555 be32_to_cpu(lrp
->args
.stateid
.seqid
));
8556 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
8558 pnfs_mark_layout_stateid_invalid(lo
, &freeme
);
8559 pnfs_clear_layoutreturn_waitbit(lo
);
8560 spin_unlock(&lo
->plh_inode
->i_lock
);
8561 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8562 pnfs_free_lseg_list(&freeme
);
8563 pnfs_put_layout_hdr(lrp
->args
.layout
);
8564 nfs_iput_and_deactive(lrp
->inode
);
8566 dprintk("<-- %s\n", __func__
);
8569 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8570 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8571 .rpc_call_done
= nfs4_layoutreturn_done
,
8572 .rpc_release
= nfs4_layoutreturn_release
,
8575 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8577 struct rpc_task
*task
;
8578 struct rpc_message msg
= {
8579 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8580 .rpc_argp
= &lrp
->args
,
8581 .rpc_resp
= &lrp
->res
,
8582 .rpc_cred
= lrp
->cred
,
8584 struct rpc_task_setup task_setup_data
= {
8585 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8586 .rpc_message
= &msg
,
8587 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8588 .callback_data
= lrp
,
8592 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8593 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8594 &task_setup_data
.rpc_client
, &msg
);
8596 dprintk("--> %s\n", __func__
);
8598 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8600 nfs4_layoutreturn_release(lrp
);
8603 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8605 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8606 task
= rpc_run_task(&task_setup_data
);
8608 return PTR_ERR(task
);
8610 status
= task
->tk_status
;
8611 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8612 dprintk("<-- %s status=%d\n", __func__
, status
);
8618 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8619 struct pnfs_device
*pdev
,
8620 struct rpc_cred
*cred
)
8622 struct nfs4_getdeviceinfo_args args
= {
8624 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8625 NOTIFY_DEVICEID4_DELETE
,
8627 struct nfs4_getdeviceinfo_res res
= {
8630 struct rpc_message msg
= {
8631 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8638 dprintk("--> %s\n", __func__
);
8639 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8640 if (res
.notification
& ~args
.notify_types
)
8641 dprintk("%s: unsupported notification\n", __func__
);
8642 if (res
.notification
!= args
.notify_types
)
8645 dprintk("<-- %s status=%d\n", __func__
, status
);
8650 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8651 struct pnfs_device
*pdev
,
8652 struct rpc_cred
*cred
)
8654 struct nfs4_exception exception
= { };
8658 err
= nfs4_handle_exception(server
,
8659 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8661 } while (exception
.retry
);
8664 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8666 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8668 struct nfs4_layoutcommit_data
*data
= calldata
;
8669 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8670 struct nfs4_session
*session
= nfs4_get_session(server
);
8672 nfs41_setup_sequence(session
,
8673 &data
->args
.seq_args
,
8679 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8681 struct nfs4_layoutcommit_data
*data
= calldata
;
8682 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8684 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8687 switch (task
->tk_status
) { /* Just ignore these failures */
8688 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8689 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8690 case -NFS4ERR_BADLAYOUT
: /* no layout */
8691 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8692 task
->tk_status
= 0;
8696 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8697 rpc_restart_call_prepare(task
);
8703 static void nfs4_layoutcommit_release(void *calldata
)
8705 struct nfs4_layoutcommit_data
*data
= calldata
;
8707 pnfs_cleanup_layoutcommit(data
);
8708 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8710 put_rpccred(data
->cred
);
8711 nfs_iput_and_deactive(data
->inode
);
8715 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8716 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8717 .rpc_call_done
= nfs4_layoutcommit_done
,
8718 .rpc_release
= nfs4_layoutcommit_release
,
8722 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8724 struct rpc_message msg
= {
8725 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8726 .rpc_argp
= &data
->args
,
8727 .rpc_resp
= &data
->res
,
8728 .rpc_cred
= data
->cred
,
8730 struct rpc_task_setup task_setup_data
= {
8731 .task
= &data
->task
,
8732 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8733 .rpc_message
= &msg
,
8734 .callback_ops
= &nfs4_layoutcommit_ops
,
8735 .callback_data
= data
,
8737 struct rpc_task
*task
;
8740 dprintk("NFS: initiating layoutcommit call. sync %d "
8741 "lbw: %llu inode %lu\n", sync
,
8742 data
->args
.lastbytewritten
,
8743 data
->args
.inode
->i_ino
);
8746 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8747 if (data
->inode
== NULL
) {
8748 nfs4_layoutcommit_release(data
);
8751 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8753 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8754 task
= rpc_run_task(&task_setup_data
);
8756 return PTR_ERR(task
);
8758 status
= task
->tk_status
;
8759 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8760 dprintk("%s: status %d\n", __func__
, status
);
8766 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8767 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8770 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8771 struct nfs_fsinfo
*info
,
8772 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8774 struct nfs41_secinfo_no_name_args args
= {
8775 .style
= SECINFO_STYLE_CURRENT_FH
,
8777 struct nfs4_secinfo_res res
= {
8780 struct rpc_message msg
= {
8781 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8785 struct rpc_clnt
*clnt
= server
->client
;
8786 struct rpc_cred
*cred
= NULL
;
8789 if (use_integrity
) {
8790 clnt
= server
->nfs_client
->cl_rpcclient
;
8791 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8792 msg
.rpc_cred
= cred
;
8795 dprintk("--> %s\n", __func__
);
8796 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8798 dprintk("<-- %s status=%d\n", __func__
, status
);
8807 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8808 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8810 struct nfs4_exception exception
= { };
8813 /* first try using integrity protection */
8814 err
= -NFS4ERR_WRONGSEC
;
8816 /* try to use integrity protection with machine cred */
8817 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8818 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8822 * if unable to use integrity protection, or SECINFO with
8823 * integrity protection returns NFS4ERR_WRONGSEC (which is
8824 * disallowed by spec, but exists in deployed servers) use
8825 * the current filesystem's rpc_client and the user cred.
8827 if (err
== -NFS4ERR_WRONGSEC
)
8828 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8833 case -NFS4ERR_WRONGSEC
:
8837 err
= nfs4_handle_exception(server
, err
, &exception
);
8839 } while (exception
.retry
);
8845 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8846 struct nfs_fsinfo
*info
)
8850 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8851 struct nfs4_secinfo_flavors
*flavors
;
8852 struct nfs4_secinfo4
*secinfo
;
8855 page
= alloc_page(GFP_KERNEL
);
8861 flavors
= page_address(page
);
8862 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8865 * Fall back on "guess and check" method if
8866 * the server doesn't support SECINFO_NO_NAME
8868 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8869 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8875 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8876 secinfo
= &flavors
->flavors
[i
];
8878 switch (secinfo
->flavor
) {
8882 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
8883 &secinfo
->flavor_info
);
8886 flavor
= RPC_AUTH_MAXFLAVOR
;
8890 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
8891 flavor
= RPC_AUTH_MAXFLAVOR
;
8893 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
8894 err
= nfs4_lookup_root_sec(server
, fhandle
,
8901 if (flavor
== RPC_AUTH_MAXFLAVOR
)
8912 static int _nfs41_test_stateid(struct nfs_server
*server
,
8913 nfs4_stateid
*stateid
,
8914 struct rpc_cred
*cred
)
8917 struct nfs41_test_stateid_args args
= {
8920 struct nfs41_test_stateid_res res
;
8921 struct rpc_message msg
= {
8922 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
8927 struct rpc_clnt
*rpc_client
= server
->client
;
8929 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
8932 dprintk("NFS call test_stateid %p\n", stateid
);
8933 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
8934 nfs4_set_sequence_privileged(&args
.seq_args
);
8935 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
8936 &args
.seq_args
, &res
.seq_res
);
8937 if (status
!= NFS_OK
) {
8938 dprintk("NFS reply test_stateid: failed, %d\n", status
);
8941 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
8945 static void nfs4_handle_delay_or_session_error(struct nfs_server
*server
,
8946 int err
, struct nfs4_exception
*exception
)
8948 exception
->retry
= 0;
8950 case -NFS4ERR_DELAY
:
8951 case -NFS4ERR_RETRY_UNCACHED_REP
:
8952 nfs4_handle_exception(server
, err
, exception
);
8954 case -NFS4ERR_BADSESSION
:
8955 case -NFS4ERR_BADSLOT
:
8956 case -NFS4ERR_BAD_HIGH_SLOT
:
8957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
8958 case -NFS4ERR_DEADSESSION
:
8959 nfs4_do_handle_exception(server
, err
, exception
);
8964 * nfs41_test_stateid - perform a TEST_STATEID operation
8966 * @server: server / transport on which to perform the operation
8967 * @stateid: state ID to test
8970 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8971 * Otherwise a negative NFS4ERR value is returned if the operation
8972 * failed or the state ID is not currently valid.
8974 static int nfs41_test_stateid(struct nfs_server
*server
,
8975 nfs4_stateid
*stateid
,
8976 struct rpc_cred
*cred
)
8978 struct nfs4_exception exception
= { };
8981 err
= _nfs41_test_stateid(server
, stateid
, cred
);
8982 nfs4_handle_delay_or_session_error(server
, err
, &exception
);
8983 } while (exception
.retry
);
8987 struct nfs_free_stateid_data
{
8988 struct nfs_server
*server
;
8989 struct nfs41_free_stateid_args args
;
8990 struct nfs41_free_stateid_res res
;
8993 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
8995 struct nfs_free_stateid_data
*data
= calldata
;
8996 nfs41_setup_sequence(nfs4_get_session(data
->server
),
8997 &data
->args
.seq_args
,
9002 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
9004 struct nfs_free_stateid_data
*data
= calldata
;
9006 nfs41_sequence_done(task
, &data
->res
.seq_res
);
9008 switch (task
->tk_status
) {
9009 case -NFS4ERR_DELAY
:
9010 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
9011 rpc_restart_call_prepare(task
);
9015 static void nfs41_free_stateid_release(void *calldata
)
9020 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
9021 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
9022 .rpc_call_done
= nfs41_free_stateid_done
,
9023 .rpc_release
= nfs41_free_stateid_release
,
9026 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
9027 const nfs4_stateid
*stateid
,
9028 struct rpc_cred
*cred
,
9031 struct rpc_message msg
= {
9032 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
9035 struct rpc_task_setup task_setup
= {
9036 .rpc_client
= server
->client
,
9037 .rpc_message
= &msg
,
9038 .callback_ops
= &nfs41_free_stateid_ops
,
9039 .flags
= RPC_TASK_ASYNC
,
9041 struct nfs_free_stateid_data
*data
;
9043 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9044 &task_setup
.rpc_client
, &msg
);
9046 dprintk("NFS call free_stateid %p\n", stateid
);
9047 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
9049 return ERR_PTR(-ENOMEM
);
9050 data
->server
= server
;
9051 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
9053 task_setup
.callback_data
= data
;
9055 msg
.rpc_argp
= &data
->args
;
9056 msg
.rpc_resp
= &data
->res
;
9057 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
9059 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
9061 return rpc_run_task(&task_setup
);
9065 * nfs41_free_stateid - perform a FREE_STATEID operation
9067 * @server: server / transport on which to perform the operation
9068 * @stateid: state ID to release
9070 * @is_recovery: set to true if this call needs to be privileged
9072 * Note: this function is always asynchronous.
9074 static int nfs41_free_stateid(struct nfs_server
*server
,
9075 const nfs4_stateid
*stateid
,
9076 struct rpc_cred
*cred
,
9079 struct rpc_task
*task
;
9081 task
= _nfs41_free_stateid(server
, stateid
, cred
, is_recovery
);
9083 return PTR_ERR(task
);
9089 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
9091 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
9093 nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
9094 nfs4_free_lock_state(server
, lsp
);
9097 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
9098 const nfs4_stateid
*s2
)
9100 if (s1
->type
!= s2
->type
)
9103 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
9106 if (s1
->seqid
== s2
->seqid
)
9108 if (s1
->seqid
== 0 || s2
->seqid
== 0)
9114 #endif /* CONFIG_NFS_V4_1 */
9116 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
9117 const nfs4_stateid
*s2
)
9119 return nfs4_stateid_match(s1
, s2
);
9123 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
9124 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9125 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9126 .recover_open
= nfs4_open_reclaim
,
9127 .recover_lock
= nfs4_lock_reclaim
,
9128 .establish_clid
= nfs4_init_clientid
,
9129 .detect_trunking
= nfs40_discover_server_trunking
,
9132 #if defined(CONFIG_NFS_V4_1)
9133 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
9134 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9135 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9136 .recover_open
= nfs4_open_reclaim
,
9137 .recover_lock
= nfs4_lock_reclaim
,
9138 .establish_clid
= nfs41_init_clientid
,
9139 .reclaim_complete
= nfs41_proc_reclaim_complete
,
9140 .detect_trunking
= nfs41_discover_server_trunking
,
9142 #endif /* CONFIG_NFS_V4_1 */
9144 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
9145 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9146 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9147 .recover_open
= nfs40_open_expired
,
9148 .recover_lock
= nfs4_lock_expired
,
9149 .establish_clid
= nfs4_init_clientid
,
9152 #if defined(CONFIG_NFS_V4_1)
9153 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
9154 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9155 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9156 .recover_open
= nfs41_open_expired
,
9157 .recover_lock
= nfs41_lock_expired
,
9158 .establish_clid
= nfs41_init_clientid
,
9160 #endif /* CONFIG_NFS_V4_1 */
9162 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
9163 .sched_state_renewal
= nfs4_proc_async_renew
,
9164 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
9165 .renew_lease
= nfs4_proc_renew
,
9168 #if defined(CONFIG_NFS_V4_1)
9169 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
9170 .sched_state_renewal
= nfs41_proc_async_sequence
,
9171 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
9172 .renew_lease
= nfs4_proc_sequence
,
9176 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
9177 .get_locations
= _nfs40_proc_get_locations
,
9178 .fsid_present
= _nfs40_proc_fsid_present
,
9181 #if defined(CONFIG_NFS_V4_1)
9182 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
9183 .get_locations
= _nfs41_proc_get_locations
,
9184 .fsid_present
= _nfs41_proc_fsid_present
,
9186 #endif /* CONFIG_NFS_V4_1 */
9188 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
9190 .init_caps
= NFS_CAP_READDIRPLUS
9191 | NFS_CAP_ATOMIC_OPEN
9192 | NFS_CAP_POSIX_LOCK
,
9193 .init_client
= nfs40_init_client
,
9194 .shutdown_client
= nfs40_shutdown_client
,
9195 .match_stateid
= nfs4_match_stateid
,
9196 .find_root_sec
= nfs4_find_root_sec
,
9197 .free_lock_state
= nfs4_release_lockowner
,
9198 .test_and_free_expired
= nfs40_test_and_free_expired_stateid
,
9199 .alloc_seqid
= nfs_alloc_seqid
,
9200 .call_sync_ops
= &nfs40_call_sync_ops
,
9201 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
9202 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
9203 .state_renewal_ops
= &nfs40_state_renewal_ops
,
9204 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
9207 #if defined(CONFIG_NFS_V4_1)
9208 static struct nfs_seqid
*
9209 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
9214 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
9216 .init_caps
= NFS_CAP_READDIRPLUS
9217 | NFS_CAP_ATOMIC_OPEN
9218 | NFS_CAP_POSIX_LOCK
9219 | NFS_CAP_STATEID_NFSV41
9220 | NFS_CAP_ATOMIC_OPEN_V1
,
9221 .init_client
= nfs41_init_client
,
9222 .shutdown_client
= nfs41_shutdown_client
,
9223 .match_stateid
= nfs41_match_stateid
,
9224 .find_root_sec
= nfs41_find_root_sec
,
9225 .free_lock_state
= nfs41_free_lock_state
,
9226 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9227 .alloc_seqid
= nfs_alloc_no_seqid
,
9228 .session_trunk
= nfs4_test_session_trunk
,
9229 .call_sync_ops
= &nfs41_call_sync_ops
,
9230 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9231 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9232 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9233 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9237 #if defined(CONFIG_NFS_V4_2)
9238 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
9240 .init_caps
= NFS_CAP_READDIRPLUS
9241 | NFS_CAP_ATOMIC_OPEN
9242 | NFS_CAP_POSIX_LOCK
9243 | NFS_CAP_STATEID_NFSV41
9244 | NFS_CAP_ATOMIC_OPEN_V1
9247 | NFS_CAP_DEALLOCATE
9249 | NFS_CAP_LAYOUTSTATS
9251 .init_client
= nfs41_init_client
,
9252 .shutdown_client
= nfs41_shutdown_client
,
9253 .match_stateid
= nfs41_match_stateid
,
9254 .find_root_sec
= nfs41_find_root_sec
,
9255 .free_lock_state
= nfs41_free_lock_state
,
9256 .call_sync_ops
= &nfs41_call_sync_ops
,
9257 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9258 .alloc_seqid
= nfs_alloc_no_seqid
,
9259 .session_trunk
= nfs4_test_session_trunk
,
9260 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9261 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9262 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9263 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9267 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
9268 [0] = &nfs_v4_0_minor_ops
,
9269 #if defined(CONFIG_NFS_V4_1)
9270 [1] = &nfs_v4_1_minor_ops
,
9272 #if defined(CONFIG_NFS_V4_2)
9273 [2] = &nfs_v4_2_minor_ops
,
9277 static ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
9279 ssize_t error
, error2
;
9281 error
= generic_listxattr(dentry
, list
, size
);
9289 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
9292 return error
+ error2
;
9295 static const struct inode_operations nfs4_dir_inode_operations
= {
9296 .create
= nfs_create
,
9297 .lookup
= nfs_lookup
,
9298 .atomic_open
= nfs_atomic_open
,
9300 .unlink
= nfs_unlink
,
9301 .symlink
= nfs_symlink
,
9305 .rename
= nfs_rename
,
9306 .permission
= nfs_permission
,
9307 .getattr
= nfs_getattr
,
9308 .setattr
= nfs_setattr
,
9309 .getxattr
= generic_getxattr
,
9310 .setxattr
= generic_setxattr
,
9311 .listxattr
= nfs4_listxattr
,
9312 .removexattr
= generic_removexattr
,
9315 static const struct inode_operations nfs4_file_inode_operations
= {
9316 .permission
= nfs_permission
,
9317 .getattr
= nfs_getattr
,
9318 .setattr
= nfs_setattr
,
9319 .getxattr
= generic_getxattr
,
9320 .setxattr
= generic_setxattr
,
9321 .listxattr
= nfs4_listxattr
,
9322 .removexattr
= generic_removexattr
,
9325 const struct nfs_rpc_ops nfs_v4_clientops
= {
9326 .version
= 4, /* protocol version */
9327 .dentry_ops
= &nfs4_dentry_operations
,
9328 .dir_inode_ops
= &nfs4_dir_inode_operations
,
9329 .file_inode_ops
= &nfs4_file_inode_operations
,
9330 .file_ops
= &nfs4_file_operations
,
9331 .getroot
= nfs4_proc_get_root
,
9332 .submount
= nfs4_submount
,
9333 .try_mount
= nfs4_try_mount
,
9334 .getattr
= nfs4_proc_getattr
,
9335 .setattr
= nfs4_proc_setattr
,
9336 .lookup
= nfs4_proc_lookup
,
9337 .access
= nfs4_proc_access
,
9338 .readlink
= nfs4_proc_readlink
,
9339 .create
= nfs4_proc_create
,
9340 .remove
= nfs4_proc_remove
,
9341 .unlink_setup
= nfs4_proc_unlink_setup
,
9342 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
9343 .unlink_done
= nfs4_proc_unlink_done
,
9344 .rename_setup
= nfs4_proc_rename_setup
,
9345 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
9346 .rename_done
= nfs4_proc_rename_done
,
9347 .link
= nfs4_proc_link
,
9348 .symlink
= nfs4_proc_symlink
,
9349 .mkdir
= nfs4_proc_mkdir
,
9350 .rmdir
= nfs4_proc_remove
,
9351 .readdir
= nfs4_proc_readdir
,
9352 .mknod
= nfs4_proc_mknod
,
9353 .statfs
= nfs4_proc_statfs
,
9354 .fsinfo
= nfs4_proc_fsinfo
,
9355 .pathconf
= nfs4_proc_pathconf
,
9356 .set_capabilities
= nfs4_server_capabilities
,
9357 .decode_dirent
= nfs4_decode_dirent
,
9358 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
9359 .read_setup
= nfs4_proc_read_setup
,
9360 .read_done
= nfs4_read_done
,
9361 .write_setup
= nfs4_proc_write_setup
,
9362 .write_done
= nfs4_write_done
,
9363 .commit_setup
= nfs4_proc_commit_setup
,
9364 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
9365 .commit_done
= nfs4_commit_done
,
9366 .lock
= nfs4_proc_lock
,
9367 .clear_acl_cache
= nfs4_zap_acl_attr
,
9368 .close_context
= nfs4_close_context
,
9369 .open_context
= nfs4_atomic_open
,
9370 .have_delegation
= nfs4_have_delegation
,
9371 .return_delegation
= nfs4_inode_return_delegation
,
9372 .alloc_client
= nfs4_alloc_client
,
9373 .init_client
= nfs4_init_client
,
9374 .free_client
= nfs4_free_client
,
9375 .create_server
= nfs4_create_server
,
9376 .clone_server
= nfs_clone_server
,
9379 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
9380 .name
= XATTR_NAME_NFSV4_ACL
,
9381 .list
= nfs4_xattr_list_nfs4_acl
,
9382 .get
= nfs4_xattr_get_nfs4_acl
,
9383 .set
= nfs4_xattr_set_nfs4_acl
,
9386 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
9387 &nfs4_xattr_nfs4_acl_handler
,
9388 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9389 &nfs4_xattr_nfs4_label_handler
,