4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
59 #include "delegation.h"
65 #include "nfs4idmap.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
76 /* file attributes which can be mapped to nfs attributes */
77 #define NFS4_VALID_ATTRS (ATTR_MODE \
88 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
89 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
90 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
91 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
92 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
93 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
94 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
95 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
96 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
97 struct nfs4_label
*olabel
);
98 #ifdef CONFIG_NFS_V4_1
99 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
101 static int nfs41_free_stateid(struct nfs_server
*, const nfs4_stateid
*,
102 struct rpc_cred
*, bool);
105 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
106 static inline struct nfs4_label
*
107 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
108 struct iattr
*sattr
, struct nfs4_label
*label
)
115 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
118 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
119 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
126 nfs4_label_release_security(struct nfs4_label
*label
)
129 security_release_secctx(label
->label
, label
->len
);
131 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
134 return server
->attr_bitmask
;
136 return server
->attr_bitmask_nl
;
139 static inline struct nfs4_label
*
140 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
141 struct iattr
*sattr
, struct nfs4_label
*l
)
144 nfs4_label_release_security(struct nfs4_label
*label
)
147 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
148 { return server
->attr_bitmask
; }
151 /* Prevent leaks of NFSv4 errors into userland */
152 static int nfs4_map_errors(int err
)
157 case -NFS4ERR_RESOURCE
:
158 case -NFS4ERR_LAYOUTTRYLATER
:
159 case -NFS4ERR_RECALLCONFLICT
:
161 case -NFS4ERR_WRONGSEC
:
162 case -NFS4ERR_WRONG_CRED
:
164 case -NFS4ERR_BADOWNER
:
165 case -NFS4ERR_BADNAME
:
167 case -NFS4ERR_SHARE_DENIED
:
169 case -NFS4ERR_MINOR_VERS_MISMATCH
:
170 return -EPROTONOSUPPORT
;
171 case -NFS4ERR_FILE_OPEN
:
174 dprintk("%s could not handle NFSv4 error %d\n",
182 * This is our standard bitmap for GETATTR requests.
184 const u32 nfs4_fattr_bitmap
[3] = {
186 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_FILEID
,
191 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
200 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
205 static const u32 nfs4_pnfs_open_bitmap
[3] = {
207 | FATTR4_WORD0_CHANGE
210 | FATTR4_WORD0_FILEID
,
212 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY
,
220 FATTR4_WORD2_MDSTHRESHOLD
221 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
226 static const u32 nfs4_open_noattr_bitmap
[3] = {
228 | FATTR4_WORD0_FILEID
,
231 const u32 nfs4_statfs_bitmap
[3] = {
232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL
,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
240 const u32 nfs4_pathconf_bitmap
[3] = {
242 | FATTR4_WORD0_MAXNAME
,
246 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME
,
250 FATTR4_WORD1_TIME_DELTA
251 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
253 | FATTR4_WORD2_CLONE_BLKSIZE
256 const u32 nfs4_fs_locations_bitmap
[3] = {
258 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS
,
264 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
272 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
275 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
276 struct nfs4_readdir_arg
*readdir
)
278 unsigned int attrs
= FATTR4_WORD0_FILEID
| FATTR4_WORD0_TYPE
;
282 readdir
->cookie
= cookie
;
283 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
288 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
299 start
= p
= kmap_atomic(*readdir
->pages
);
302 *p
++ = xdr_one
; /* next */
303 *p
++ = xdr_zero
; /* cookie, first word */
304 *p
++ = xdr_one
; /* cookie, second word */
305 *p
++ = xdr_one
; /* entry len */
306 memcpy(p
, ".\0\0\0", 4); /* entry */
308 *p
++ = xdr_one
; /* bitmap length */
309 *p
++ = htonl(attrs
); /* bitmap */
310 *p
++ = htonl(12); /* attribute buffer length */
311 *p
++ = htonl(NF4DIR
);
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(attrs
); /* bitmap */
323 *p
++ = htonl(12); /* attribute buffer length */
324 *p
++ = htonl(NF4DIR
);
325 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
327 readdir
->pgbase
= (char *)p
- (char *)start
;
328 readdir
->count
-= readdir
->pgbase
;
329 kunmap_atomic(start
);
332 static void nfs4_test_and_free_stateid(struct nfs_server
*server
,
333 nfs4_stateid
*stateid
,
334 struct rpc_cred
*cred
)
336 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
338 ops
->test_and_free_expired(server
, stateid
, cred
);
341 static void __nfs4_free_revoked_stateid(struct nfs_server
*server
,
342 nfs4_stateid
*stateid
,
343 struct rpc_cred
*cred
)
345 stateid
->type
= NFS4_REVOKED_STATEID_TYPE
;
346 nfs4_test_and_free_stateid(server
, stateid
, cred
);
349 static void nfs4_free_revoked_stateid(struct nfs_server
*server
,
350 const nfs4_stateid
*stateid
,
351 struct rpc_cred
*cred
)
355 nfs4_stateid_copy(&tmp
, stateid
);
356 __nfs4_free_revoked_stateid(server
, &tmp
, cred
);
359 static long nfs4_update_delay(long *timeout
)
363 return NFS4_POLL_RETRY_MAX
;
365 *timeout
= NFS4_POLL_RETRY_MIN
;
366 if (*timeout
> NFS4_POLL_RETRY_MAX
)
367 *timeout
= NFS4_POLL_RETRY_MAX
;
373 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
379 freezable_schedule_timeout_killable_unsafe(
380 nfs4_update_delay(timeout
));
381 if (fatal_signal_pending(current
))
386 /* This is the error handling routine for processes that are allowed
389 static int nfs4_do_handle_exception(struct nfs_server
*server
,
390 int errorcode
, struct nfs4_exception
*exception
)
392 struct nfs_client
*clp
= server
->nfs_client
;
393 struct nfs4_state
*state
= exception
->state
;
394 const nfs4_stateid
*stateid
= exception
->stateid
;
395 struct inode
*inode
= exception
->inode
;
398 exception
->delay
= 0;
399 exception
->recovering
= 0;
400 exception
->retry
= 0;
402 if (stateid
== NULL
&& state
!= NULL
)
403 stateid
= &state
->stateid
;
408 case -NFS4ERR_DELEG_REVOKED
:
409 case -NFS4ERR_ADMIN_REVOKED
:
410 case -NFS4ERR_EXPIRED
:
411 case -NFS4ERR_BAD_STATEID
:
412 if (inode
!= NULL
&& stateid
!= NULL
) {
413 nfs_inode_find_state_and_recover(inode
,
415 goto wait_on_recovery
;
417 case -NFS4ERR_OPENMODE
:
421 err
= nfs_async_inode_return_delegation(inode
,
424 goto wait_on_recovery
;
425 if (stateid
!= NULL
&& stateid
->type
== NFS4_DELEGATION_STATEID_TYPE
) {
426 exception
->retry
= 1;
432 ret
= nfs4_schedule_stateid_recovery(server
, state
);
435 goto wait_on_recovery
;
436 case -NFS4ERR_STALE_STATEID
:
437 case -NFS4ERR_STALE_CLIENTID
:
438 nfs4_schedule_lease_recovery(clp
);
439 goto wait_on_recovery
;
441 ret
= nfs4_schedule_migration_recovery(server
);
444 goto wait_on_recovery
;
445 case -NFS4ERR_LEASE_MOVED
:
446 nfs4_schedule_lease_moved_recovery(clp
);
447 goto wait_on_recovery
;
448 #if defined(CONFIG_NFS_V4_1)
449 case -NFS4ERR_BADSESSION
:
450 case -NFS4ERR_BADSLOT
:
451 case -NFS4ERR_BAD_HIGH_SLOT
:
452 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
453 case -NFS4ERR_DEADSESSION
:
454 case -NFS4ERR_SEQ_FALSE_RETRY
:
455 case -NFS4ERR_SEQ_MISORDERED
:
456 dprintk("%s ERROR: %d Reset session\n", __func__
,
458 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
459 goto wait_on_recovery
;
460 #endif /* defined(CONFIG_NFS_V4_1) */
461 case -NFS4ERR_FILE_OPEN
:
462 if (exception
->timeout
> HZ
) {
463 /* We have retried a decent amount, time to
470 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
472 case -NFS4ERR_LAYOUTTRYLATER
:
473 case -NFS4ERR_RECALLCONFLICT
:
474 exception
->delay
= 1;
477 case -NFS4ERR_RETRY_UNCACHED_REP
:
478 case -NFS4ERR_OLD_STATEID
:
479 exception
->retry
= 1;
481 case -NFS4ERR_BADOWNER
:
482 /* The following works around a Linux server bug! */
483 case -NFS4ERR_BADNAME
:
484 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
485 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
486 exception
->retry
= 1;
487 printk(KERN_WARNING
"NFS: v4 server %s "
488 "does not accept raw "
490 "Reenabling the idmapper.\n",
491 server
->nfs_client
->cl_hostname
);
494 /* We failed to handle the error */
495 return nfs4_map_errors(ret
);
497 exception
->recovering
= 1;
501 /* This is the error handling routine for processes that are allowed
504 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
506 struct nfs_client
*clp
= server
->nfs_client
;
509 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
510 if (exception
->delay
) {
511 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
514 if (exception
->recovering
) {
515 ret
= nfs4_wait_clnt_recover(clp
);
516 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
523 exception
->retry
= 1;
528 nfs4_async_handle_exception(struct rpc_task
*task
, struct nfs_server
*server
,
529 int errorcode
, struct nfs4_exception
*exception
)
531 struct nfs_client
*clp
= server
->nfs_client
;
534 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
535 if (exception
->delay
) {
536 rpc_delay(task
, nfs4_update_delay(&exception
->timeout
));
539 if (exception
->recovering
) {
540 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
541 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
542 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
545 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
550 exception
->retry
= 1;
555 nfs4_async_handle_error(struct rpc_task
*task
, struct nfs_server
*server
,
556 struct nfs4_state
*state
, long *timeout
)
558 struct nfs4_exception exception
= {
562 if (task
->tk_status
>= 0)
565 exception
.timeout
= *timeout
;
566 task
->tk_status
= nfs4_async_handle_exception(task
, server
,
569 if (exception
.delay
&& timeout
)
570 *timeout
= exception
.timeout
;
577 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
578 * or 'false' otherwise.
580 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
582 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
583 return (flavor
== RPC_AUTH_GSS_KRB5I
) || (flavor
== RPC_AUTH_GSS_KRB5P
);
586 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
588 spin_lock(&clp
->cl_lock
);
589 if (time_before(clp
->cl_last_renewal
,timestamp
))
590 clp
->cl_last_renewal
= timestamp
;
591 spin_unlock(&clp
->cl_lock
);
594 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
596 struct nfs_client
*clp
= server
->nfs_client
;
598 if (!nfs4_has_session(clp
))
599 do_renew_lease(clp
, timestamp
);
602 struct nfs4_call_sync_data
{
603 const struct nfs_server
*seq_server
;
604 struct nfs4_sequence_args
*seq_args
;
605 struct nfs4_sequence_res
*seq_res
;
608 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
609 struct nfs4_sequence_res
*res
, int cache_reply
)
611 args
->sa_slot
= NULL
;
612 args
->sa_cache_this
= cache_reply
;
613 args
->sa_privileged
= 0;
618 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
620 args
->sa_privileged
= 1;
623 static void nfs40_sequence_free_slot(struct nfs4_sequence_res
*res
)
625 struct nfs4_slot
*slot
= res
->sr_slot
;
626 struct nfs4_slot_table
*tbl
;
629 spin_lock(&tbl
->slot_tbl_lock
);
630 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
631 nfs4_free_slot(tbl
, slot
);
632 spin_unlock(&tbl
->slot_tbl_lock
);
637 static int nfs40_sequence_done(struct rpc_task
*task
,
638 struct nfs4_sequence_res
*res
)
640 if (res
->sr_slot
!= NULL
)
641 nfs40_sequence_free_slot(res
);
645 #if defined(CONFIG_NFS_V4_1)
647 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
649 struct nfs4_session
*session
;
650 struct nfs4_slot_table
*tbl
;
651 struct nfs4_slot
*slot
= res
->sr_slot
;
652 bool send_new_highest_used_slotid
= false;
655 session
= tbl
->session
;
657 /* Bump the slot sequence number */
662 spin_lock(&tbl
->slot_tbl_lock
);
663 /* Be nice to the server: try to ensure that the last transmitted
664 * value for highest_user_slotid <= target_highest_slotid
666 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
667 send_new_highest_used_slotid
= true;
669 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
670 send_new_highest_used_slotid
= false;
673 nfs4_free_slot(tbl
, slot
);
675 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
676 send_new_highest_used_slotid
= false;
678 spin_unlock(&tbl
->slot_tbl_lock
);
680 if (send_new_highest_used_slotid
)
681 nfs41_notify_server(session
->clp
);
682 if (waitqueue_active(&tbl
->slot_waitq
))
683 wake_up_all(&tbl
->slot_waitq
);
686 static int nfs41_sequence_process(struct rpc_task
*task
,
687 struct nfs4_sequence_res
*res
)
689 struct nfs4_session
*session
;
690 struct nfs4_slot
*slot
= res
->sr_slot
;
691 struct nfs_client
*clp
;
692 bool interrupted
= false;
697 /* don't increment the sequence number if the task wasn't sent */
698 if (!RPC_WAS_SENT(task
))
701 session
= slot
->table
->session
;
703 if (slot
->interrupted
) {
704 if (res
->sr_status
!= -NFS4ERR_DELAY
)
705 slot
->interrupted
= 0;
709 trace_nfs4_sequence_done(session
, res
);
710 /* Check the SEQUENCE operation status */
711 switch (res
->sr_status
) {
713 /* If previous op on slot was interrupted and we reused
714 * the seq# and got a reply from the cache, then retry
716 if (task
->tk_status
== -EREMOTEIO
&& interrupted
) {
720 /* Update the slot's sequence and clientid lease timer */
723 do_renew_lease(clp
, res
->sr_timestamp
);
724 /* Check sequence flags */
725 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
,
727 nfs41_update_target_slotid(slot
->table
, slot
, res
);
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
734 * Mark the slot as having hosted an interrupted RPC call.
736 slot
->interrupted
= 1;
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
748 case -NFS4ERR_BADSLOT
:
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
754 case -NFS4ERR_SEQ_MISORDERED
:
756 * Was the last operation on this sequence interrupted?
757 * If so, retry after bumping the sequence number.
764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
767 if (slot
->seq_nr
!= 1) {
772 case -NFS4ERR_SEQ_FALSE_RETRY
:
776 /* Just update the slot sequence no. */
780 /* The session may be reset by one of the error handlers. */
781 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
785 if (rpc_restart_call_prepare(task
)) {
786 nfs41_sequence_free_slot(res
);
792 if (!rpc_restart_call(task
))
794 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
798 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
800 if (!nfs41_sequence_process(task
, res
))
802 if (res
->sr_slot
!= NULL
)
803 nfs41_sequence_free_slot(res
);
807 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
809 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
811 if (res
->sr_slot
== NULL
)
813 if (res
->sr_slot
->table
->session
!= NULL
)
814 return nfs41_sequence_process(task
, res
);
815 return nfs40_sequence_done(task
, res
);
818 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
820 if (res
->sr_slot
!= NULL
) {
821 if (res
->sr_slot
->table
->session
!= NULL
)
822 nfs41_sequence_free_slot(res
);
824 nfs40_sequence_free_slot(res
);
828 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
830 if (res
->sr_slot
== NULL
)
832 if (!res
->sr_slot
->table
->session
)
833 return nfs40_sequence_done(task
, res
);
834 return nfs41_sequence_done(task
, res
);
836 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
838 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
840 struct nfs4_call_sync_data
*data
= calldata
;
842 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
844 nfs4_setup_sequence(data
->seq_server
->nfs_client
,
845 data
->seq_args
, data
->seq_res
, task
);
848 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
850 struct nfs4_call_sync_data
*data
= calldata
;
852 nfs41_sequence_done(task
, data
->seq_res
);
855 static const struct rpc_call_ops nfs41_call_sync_ops
= {
856 .rpc_call_prepare
= nfs41_call_sync_prepare
,
857 .rpc_call_done
= nfs41_call_sync_done
,
860 #else /* !CONFIG_NFS_V4_1 */
862 static int nfs4_sequence_process(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
864 return nfs40_sequence_done(task
, res
);
867 static void nfs4_sequence_free_slot(struct nfs4_sequence_res
*res
)
869 if (res
->sr_slot
!= NULL
)
870 nfs40_sequence_free_slot(res
);
873 int nfs4_sequence_done(struct rpc_task
*task
,
874 struct nfs4_sequence_res
*res
)
876 return nfs40_sequence_done(task
, res
);
878 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
880 #endif /* !CONFIG_NFS_V4_1 */
882 int nfs4_setup_sequence(const struct nfs_client
*client
,
883 struct nfs4_sequence_args
*args
,
884 struct nfs4_sequence_res
*res
,
885 struct rpc_task
*task
)
887 struct nfs4_session
*session
= nfs4_get_session(client
);
888 struct nfs4_slot_table
*tbl
= client
->cl_slot_tbl
;
889 struct nfs4_slot
*slot
;
891 /* slot already allocated? */
892 if (res
->sr_slot
!= NULL
)
896 tbl
= &session
->fc_slot_table
;
897 task
->tk_timeout
= 0;
900 spin_lock(&tbl
->slot_tbl_lock
);
901 /* The state manager will wait until the slot table is empty */
902 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
905 slot
= nfs4_alloc_slot(tbl
);
907 /* Try again in 1/4 second */
908 if (slot
== ERR_PTR(-ENOMEM
))
909 task
->tk_timeout
= HZ
>> 2;
912 spin_unlock(&tbl
->slot_tbl_lock
);
914 slot
->privileged
= args
->sa_privileged
? 1 : 0;
915 args
->sa_slot
= slot
;
919 res
->sr_timestamp
= jiffies
;
920 res
->sr_status_flags
= 0;
924 trace_nfs4_setup_sequence(session
, args
);
926 rpc_call_start(task
);
930 if (args
->sa_privileged
)
931 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
932 NULL
, RPC_PRIORITY_PRIVILEGED
);
934 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
935 spin_unlock(&tbl
->slot_tbl_lock
);
938 EXPORT_SYMBOL_GPL(nfs4_setup_sequence
);
940 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
942 struct nfs4_call_sync_data
*data
= calldata
;
943 nfs4_setup_sequence(data
->seq_server
->nfs_client
,
944 data
->seq_args
, data
->seq_res
, task
);
947 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
949 struct nfs4_call_sync_data
*data
= calldata
;
950 nfs4_sequence_done(task
, data
->seq_res
);
953 static const struct rpc_call_ops nfs40_call_sync_ops
= {
954 .rpc_call_prepare
= nfs40_call_sync_prepare
,
955 .rpc_call_done
= nfs40_call_sync_done
,
958 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
959 struct nfs_server
*server
,
960 struct rpc_message
*msg
,
961 struct nfs4_sequence_args
*args
,
962 struct nfs4_sequence_res
*res
)
965 struct rpc_task
*task
;
966 struct nfs_client
*clp
= server
->nfs_client
;
967 struct nfs4_call_sync_data data
= {
968 .seq_server
= server
,
972 struct rpc_task_setup task_setup
= {
975 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
976 .callback_data
= &data
979 task
= rpc_run_task(&task_setup
);
983 ret
= task
->tk_status
;
989 int nfs4_call_sync(struct rpc_clnt
*clnt
,
990 struct nfs_server
*server
,
991 struct rpc_message
*msg
,
992 struct nfs4_sequence_args
*args
,
993 struct nfs4_sequence_res
*res
,
996 nfs4_init_sequence(args
, res
, cache_reply
);
997 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
1000 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
,
1001 unsigned long timestamp
)
1003 struct nfs_inode
*nfsi
= NFS_I(dir
);
1005 spin_lock(&dir
->i_lock
);
1006 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
1007 if (cinfo
->atomic
&& cinfo
->before
== dir
->i_version
) {
1008 nfsi
->cache_validity
&= ~NFS_INO_REVAL_PAGECACHE
;
1009 nfsi
->attrtimeo_timestamp
= jiffies
;
1011 nfs_force_lookup_revalidate(dir
);
1012 if (cinfo
->before
!= dir
->i_version
)
1013 nfsi
->cache_validity
|= NFS_INO_INVALID_ACCESS
|
1014 NFS_INO_INVALID_ACL
;
1016 dir
->i_version
= cinfo
->after
;
1017 nfsi
->read_cache_jiffies
= timestamp
;
1018 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
1019 nfs_fscache_invalidate(dir
);
1020 spin_unlock(&dir
->i_lock
);
1023 struct nfs4_opendata
{
1025 struct nfs_openargs o_arg
;
1026 struct nfs_openres o_res
;
1027 struct nfs_open_confirmargs c_arg
;
1028 struct nfs_open_confirmres c_res
;
1029 struct nfs4_string owner_name
;
1030 struct nfs4_string group_name
;
1031 struct nfs4_label
*a_label
;
1032 struct nfs_fattr f_attr
;
1033 struct nfs4_label
*f_label
;
1035 struct dentry
*dentry
;
1036 struct nfs4_state_owner
*owner
;
1037 struct nfs4_state
*state
;
1039 unsigned long timestamp
;
1047 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
1048 int err
, struct nfs4_exception
*exception
)
1052 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1054 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
1055 exception
->retry
= 1;
1060 nfs4_map_atomic_open_share(struct nfs_server
*server
,
1061 fmode_t fmode
, int openflags
)
1065 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
1067 res
= NFS4_SHARE_ACCESS_READ
;
1070 res
= NFS4_SHARE_ACCESS_WRITE
;
1072 case FMODE_READ
|FMODE_WRITE
:
1073 res
= NFS4_SHARE_ACCESS_BOTH
;
1075 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1077 /* Want no delegation if we're using O_DIRECT */
1078 if (openflags
& O_DIRECT
)
1079 res
|= NFS4_SHARE_WANT_NO_DELEG
;
1084 static enum open_claim_type4
1085 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
1086 enum open_claim_type4 claim
)
1088 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
1093 case NFS4_OPEN_CLAIM_FH
:
1094 return NFS4_OPEN_CLAIM_NULL
;
1095 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1096 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1097 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1098 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
1102 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
1104 p
->o_res
.f_attr
= &p
->f_attr
;
1105 p
->o_res
.f_label
= p
->f_label
;
1106 p
->o_res
.seqid
= p
->o_arg
.seqid
;
1107 p
->c_res
.seqid
= p
->c_arg
.seqid
;
1108 p
->o_res
.server
= p
->o_arg
.server
;
1109 p
->o_res
.access_request
= p
->o_arg
.access
;
1110 nfs_fattr_init(&p
->f_attr
);
1111 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
1114 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
1115 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
1116 const struct iattr
*attrs
,
1117 struct nfs4_label
*label
,
1118 enum open_claim_type4 claim
,
1121 struct dentry
*parent
= dget_parent(dentry
);
1122 struct inode
*dir
= d_inode(parent
);
1123 struct nfs_server
*server
= NFS_SERVER(dir
);
1124 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1125 struct nfs4_opendata
*p
;
1127 p
= kzalloc(sizeof(*p
), gfp_mask
);
1131 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1132 if (IS_ERR(p
->f_label
))
1135 p
->a_label
= nfs4_label_alloc(server
, gfp_mask
);
1136 if (IS_ERR(p
->a_label
))
1139 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1140 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1141 if (IS_ERR(p
->o_arg
.seqid
))
1142 goto err_free_label
;
1143 nfs_sb_active(dentry
->d_sb
);
1144 p
->dentry
= dget(dentry
);
1147 atomic_inc(&sp
->so_count
);
1148 p
->o_arg
.open_flags
= flags
;
1149 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1150 p
->o_arg
.umask
= current_umask();
1151 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1152 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1154 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1155 * will return permission denied for all bits until close */
1156 if (!(flags
& O_EXCL
)) {
1157 /* ask server to check for all possible rights as results
1159 switch (p
->o_arg
.claim
) {
1162 case NFS4_OPEN_CLAIM_NULL
:
1163 case NFS4_OPEN_CLAIM_FH
:
1164 p
->o_arg
.access
= NFS4_ACCESS_READ
|
1165 NFS4_ACCESS_MODIFY
|
1166 NFS4_ACCESS_EXTEND
|
1167 NFS4_ACCESS_EXECUTE
;
1170 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1171 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1172 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1173 p
->o_arg
.name
= &dentry
->d_name
;
1174 p
->o_arg
.server
= server
;
1175 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1176 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1177 p
->o_arg
.label
= nfs4_label_copy(p
->a_label
, label
);
1178 switch (p
->o_arg
.claim
) {
1179 case NFS4_OPEN_CLAIM_NULL
:
1180 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1181 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1182 p
->o_arg
.fh
= NFS_FH(dir
);
1184 case NFS4_OPEN_CLAIM_PREVIOUS
:
1185 case NFS4_OPEN_CLAIM_FH
:
1186 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1187 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1188 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1190 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1193 p
->o_arg
.u
.attrs
= &p
->attrs
;
1194 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1197 verf
[1] = current
->pid
;
1198 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1199 sizeof(p
->o_arg
.u
.verifier
.data
));
1201 p
->c_arg
.fh
= &p
->o_res
.fh
;
1202 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1203 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1204 nfs4_init_opendata_res(p
);
1205 kref_init(&p
->kref
);
1209 nfs4_label_free(p
->a_label
);
1211 nfs4_label_free(p
->f_label
);
1219 static void nfs4_opendata_free(struct kref
*kref
)
1221 struct nfs4_opendata
*p
= container_of(kref
,
1222 struct nfs4_opendata
, kref
);
1223 struct super_block
*sb
= p
->dentry
->d_sb
;
1225 nfs_free_seqid(p
->o_arg
.seqid
);
1226 nfs4_sequence_free_slot(&p
->o_res
.seq_res
);
1227 if (p
->state
!= NULL
)
1228 nfs4_put_open_state(p
->state
);
1229 nfs4_put_state_owner(p
->owner
);
1231 nfs4_label_free(p
->a_label
);
1232 nfs4_label_free(p
->f_label
);
1236 nfs_sb_deactive(sb
);
1237 nfs_fattr_free_names(&p
->f_attr
);
1238 kfree(p
->f_attr
.mdsthreshold
);
1242 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1245 kref_put(&p
->kref
, nfs4_opendata_free
);
1248 static bool nfs4_mode_match_open_stateid(struct nfs4_state
*state
,
1251 switch(fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1252 case FMODE_READ
|FMODE_WRITE
:
1253 return state
->n_rdwr
!= 0;
1255 return state
->n_wronly
!= 0;
1257 return state
->n_rdonly
!= 0;
1263 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1267 if (open_mode
& (O_EXCL
|O_TRUNC
))
1269 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1271 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1272 && state
->n_rdonly
!= 0;
1275 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1276 && state
->n_wronly
!= 0;
1278 case FMODE_READ
|FMODE_WRITE
:
1279 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1280 && state
->n_rdwr
!= 0;
1286 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
,
1287 enum open_claim_type4 claim
)
1289 if (delegation
== NULL
)
1291 if ((delegation
->type
& fmode
) != fmode
)
1293 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1296 case NFS4_OPEN_CLAIM_NULL
:
1297 case NFS4_OPEN_CLAIM_FH
:
1299 case NFS4_OPEN_CLAIM_PREVIOUS
:
1300 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1305 nfs_mark_delegation_referenced(delegation
);
1309 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1318 case FMODE_READ
|FMODE_WRITE
:
1321 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1324 #ifdef CONFIG_NFS_V4_1
1325 static bool nfs_open_stateid_recover_openmode(struct nfs4_state
*state
)
1327 if (state
->n_rdonly
&& !test_bit(NFS_O_RDONLY_STATE
, &state
->flags
))
1329 if (state
->n_wronly
&& !test_bit(NFS_O_WRONLY_STATE
, &state
->flags
))
1331 if (state
->n_rdwr
&& !test_bit(NFS_O_RDWR_STATE
, &state
->flags
))
1335 #endif /* CONFIG_NFS_V4_1 */
1337 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1339 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1340 bool need_recover
= false;
1342 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1343 need_recover
= true;
1344 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1345 need_recover
= true;
1346 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1347 need_recover
= true;
1349 nfs4_state_mark_reclaim_nograce(clp
, state
);
1352 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1353 const nfs4_stateid
*stateid
, nfs4_stateid
*freeme
)
1355 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1357 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1358 nfs4_stateid_copy(freeme
, &state
->open_stateid
);
1359 nfs_test_and_clear_all_open_stateid(state
);
1362 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1367 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1369 if (!(state
->n_wronly
|| state
->n_rdonly
|| state
->n_rdwr
))
1371 if (state
->n_wronly
)
1372 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1373 if (state
->n_rdonly
)
1374 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1376 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1377 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1380 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1381 nfs4_stateid
*stateid
, fmode_t fmode
)
1383 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1384 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1386 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1389 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1392 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1393 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1394 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1396 if (stateid
== NULL
)
1398 /* Handle OPEN+OPEN_DOWNGRADE races */
1399 if (nfs4_stateid_match_other(stateid
, &state
->open_stateid
) &&
1400 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
)) {
1401 nfs_resync_open_stateid_locked(state
);
1404 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1405 nfs4_stateid_copy(&state
->stateid
, stateid
);
1406 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1409 static void nfs_clear_open_stateid(struct nfs4_state
*state
,
1410 nfs4_stateid
*arg_stateid
,
1411 nfs4_stateid
*stateid
, fmode_t fmode
)
1413 write_seqlock(&state
->seqlock
);
1414 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1415 if (nfs4_state_match_open_stateid_other(state
, arg_stateid
))
1416 nfs_clear_open_stateid_locked(state
, stateid
, fmode
);
1417 write_sequnlock(&state
->seqlock
);
1418 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1419 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1422 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
,
1423 const nfs4_stateid
*stateid
, fmode_t fmode
,
1424 nfs4_stateid
*freeme
)
1428 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1431 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1433 case FMODE_READ
|FMODE_WRITE
:
1434 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1436 if (!nfs_need_update_open_stateid(state
, stateid
, freeme
))
1438 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1439 nfs4_stateid_copy(&state
->stateid
, stateid
);
1440 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1443 static void __update_open_stateid(struct nfs4_state
*state
,
1444 const nfs4_stateid
*open_stateid
,
1445 const nfs4_stateid
*deleg_stateid
,
1447 nfs4_stateid
*freeme
)
1450 * Protect the call to nfs4_state_set_mode_locked and
1451 * serialise the stateid update
1453 spin_lock(&state
->owner
->so_lock
);
1454 write_seqlock(&state
->seqlock
);
1455 if (deleg_stateid
!= NULL
) {
1456 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1457 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1459 if (open_stateid
!= NULL
)
1460 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
, freeme
);
1461 write_sequnlock(&state
->seqlock
);
1462 update_open_stateflags(state
, fmode
);
1463 spin_unlock(&state
->owner
->so_lock
);
1466 static int update_open_stateid(struct nfs4_state
*state
,
1467 const nfs4_stateid
*open_stateid
,
1468 const nfs4_stateid
*delegation
,
1471 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1472 struct nfs_client
*clp
= server
->nfs_client
;
1473 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1474 struct nfs_delegation
*deleg_cur
;
1475 nfs4_stateid freeme
= { };
1478 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1481 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1482 if (deleg_cur
== NULL
)
1485 spin_lock(&deleg_cur
->lock
);
1486 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1487 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1488 (deleg_cur
->type
& fmode
) != fmode
)
1489 goto no_delegation_unlock
;
1491 if (delegation
== NULL
)
1492 delegation
= &deleg_cur
->stateid
;
1493 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1494 goto no_delegation_unlock
;
1496 nfs_mark_delegation_referenced(deleg_cur
);
1497 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
,
1500 no_delegation_unlock
:
1501 spin_unlock(&deleg_cur
->lock
);
1505 if (!ret
&& open_stateid
!= NULL
) {
1506 __update_open_stateid(state
, open_stateid
, NULL
, fmode
, &freeme
);
1509 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1510 nfs4_schedule_state_manager(clp
);
1511 if (freeme
.type
!= 0)
1512 nfs4_test_and_free_stateid(server
, &freeme
,
1513 state
->owner
->so_cred
);
1518 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1519 const nfs4_stateid
*stateid
)
1521 struct nfs4_state
*state
= lsp
->ls_state
;
1524 spin_lock(&state
->state_lock
);
1525 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1527 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1529 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1532 spin_unlock(&state
->state_lock
);
1536 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1538 struct nfs_delegation
*delegation
;
1541 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1542 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1547 nfs4_inode_return_delegation(inode
);
1550 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1552 struct nfs4_state
*state
= opendata
->state
;
1553 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1554 struct nfs_delegation
*delegation
;
1555 int open_mode
= opendata
->o_arg
.open_flags
;
1556 fmode_t fmode
= opendata
->o_arg
.fmode
;
1557 enum open_claim_type4 claim
= opendata
->o_arg
.claim
;
1558 nfs4_stateid stateid
;
1562 spin_lock(&state
->owner
->so_lock
);
1563 if (can_open_cached(state
, fmode
, open_mode
)) {
1564 update_open_stateflags(state
, fmode
);
1565 spin_unlock(&state
->owner
->so_lock
);
1566 goto out_return_state
;
1568 spin_unlock(&state
->owner
->so_lock
);
1570 delegation
= rcu_dereference(nfsi
->delegation
);
1571 if (!can_open_delegated(delegation
, fmode
, claim
)) {
1575 /* Save the delegation */
1576 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1578 nfs_release_seqid(opendata
->o_arg
.seqid
);
1579 if (!opendata
->is_recover
) {
1580 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1586 /* Try to update the stateid using the delegation */
1587 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1588 goto out_return_state
;
1591 return ERR_PTR(ret
);
1593 atomic_inc(&state
->count
);
1598 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1600 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1601 struct nfs_delegation
*delegation
;
1602 int delegation_flags
= 0;
1605 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1607 delegation_flags
= delegation
->flags
;
1609 switch (data
->o_arg
.claim
) {
1612 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1613 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1614 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1615 "returning a delegation for "
1616 "OPEN(CLAIM_DELEGATE_CUR)\n",
1620 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1621 nfs_inode_set_delegation(state
->inode
,
1622 data
->owner
->so_cred
,
1625 nfs_inode_reclaim_delegation(state
->inode
,
1626 data
->owner
->so_cred
,
1631 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1632 * and update the nfs4_state.
1634 static struct nfs4_state
*
1635 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1637 struct inode
*inode
= data
->state
->inode
;
1638 struct nfs4_state
*state
= data
->state
;
1641 if (!data
->rpc_done
) {
1642 if (data
->rpc_status
)
1643 return ERR_PTR(data
->rpc_status
);
1644 /* cached opens have already been processed */
1648 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1650 return ERR_PTR(ret
);
1652 if (data
->o_res
.delegation_type
!= 0)
1653 nfs4_opendata_check_deleg(data
, state
);
1655 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1657 atomic_inc(&state
->count
);
1662 static struct nfs4_state
*
1663 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1665 struct inode
*inode
;
1666 struct nfs4_state
*state
= NULL
;
1669 if (!data
->rpc_done
) {
1670 state
= nfs4_try_open_cached(data
);
1671 trace_nfs4_cached_open(data
->state
);
1676 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1678 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1679 ret
= PTR_ERR(inode
);
1683 state
= nfs4_get_open_state(inode
, data
->owner
);
1686 if (data
->o_res
.delegation_type
!= 0)
1687 nfs4_opendata_check_deleg(data
, state
);
1688 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1692 nfs_release_seqid(data
->o_arg
.seqid
);
1697 return ERR_PTR(ret
);
1700 static struct nfs4_state
*
1701 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1703 struct nfs4_state
*ret
;
1705 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1706 ret
=_nfs4_opendata_reclaim_to_nfs4_state(data
);
1708 ret
= _nfs4_opendata_to_nfs4_state(data
);
1709 nfs4_sequence_free_slot(&data
->o_res
.seq_res
);
1713 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1715 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1716 struct nfs_open_context
*ctx
;
1718 spin_lock(&state
->inode
->i_lock
);
1719 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1720 if (ctx
->state
!= state
)
1722 get_nfs_open_context(ctx
);
1723 spin_unlock(&state
->inode
->i_lock
);
1726 spin_unlock(&state
->inode
->i_lock
);
1727 return ERR_PTR(-ENOENT
);
1730 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1731 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1733 struct nfs4_opendata
*opendata
;
1735 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1736 NULL
, NULL
, claim
, GFP_NOFS
);
1737 if (opendata
== NULL
)
1738 return ERR_PTR(-ENOMEM
);
1739 opendata
->state
= state
;
1740 atomic_inc(&state
->count
);
1744 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
,
1747 struct nfs4_state
*newstate
;
1750 if (!nfs4_mode_match_open_stateid(opendata
->state
, fmode
))
1752 opendata
->o_arg
.open_flags
= 0;
1753 opendata
->o_arg
.fmode
= fmode
;
1754 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1755 NFS_SB(opendata
->dentry
->d_sb
),
1757 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1758 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1759 nfs4_init_opendata_res(opendata
);
1760 ret
= _nfs4_recover_proc_open(opendata
);
1763 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1764 if (IS_ERR(newstate
))
1765 return PTR_ERR(newstate
);
1766 if (newstate
!= opendata
->state
)
1768 nfs4_close_state(newstate
, fmode
);
1772 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1776 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1777 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1778 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1779 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1780 /* memory barrier prior to reading state->n_* */
1781 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1782 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1784 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1787 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1790 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1794 * We may have performed cached opens for all three recoveries.
1795 * Check if we need to update the current stateid.
1797 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1798 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1799 write_seqlock(&state
->seqlock
);
1800 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1801 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1802 write_sequnlock(&state
->seqlock
);
1809 * reclaim state on the server after a reboot.
1811 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1813 struct nfs_delegation
*delegation
;
1814 struct nfs4_opendata
*opendata
;
1815 fmode_t delegation_type
= 0;
1818 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1819 NFS4_OPEN_CLAIM_PREVIOUS
);
1820 if (IS_ERR(opendata
))
1821 return PTR_ERR(opendata
);
1823 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1824 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1825 delegation_type
= delegation
->type
;
1827 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1828 status
= nfs4_open_recover(opendata
, state
);
1829 nfs4_opendata_put(opendata
);
1833 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1835 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1836 struct nfs4_exception exception
= { };
1839 err
= _nfs4_do_open_reclaim(ctx
, state
);
1840 trace_nfs4_open_reclaim(ctx
, 0, err
);
1841 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1843 if (err
!= -NFS4ERR_DELAY
)
1845 nfs4_handle_exception(server
, err
, &exception
);
1846 } while (exception
.retry
);
1850 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1852 struct nfs_open_context
*ctx
;
1855 ctx
= nfs4_state_find_open_context(state
);
1858 ret
= nfs4_do_open_reclaim(ctx
, state
);
1859 put_nfs_open_context(ctx
);
1863 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1867 printk(KERN_ERR
"NFS: %s: unhandled error "
1868 "%d.\n", __func__
, err
);
1874 case -NFS4ERR_BADSESSION
:
1875 case -NFS4ERR_BADSLOT
:
1876 case -NFS4ERR_BAD_HIGH_SLOT
:
1877 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1878 case -NFS4ERR_DEADSESSION
:
1879 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1880 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1882 case -NFS4ERR_STALE_CLIENTID
:
1883 case -NFS4ERR_STALE_STATEID
:
1884 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1885 /* Don't recall a delegation if it was lost */
1886 nfs4_schedule_lease_recovery(server
->nfs_client
);
1888 case -NFS4ERR_MOVED
:
1889 nfs4_schedule_migration_recovery(server
);
1891 case -NFS4ERR_LEASE_MOVED
:
1892 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1894 case -NFS4ERR_DELEG_REVOKED
:
1895 case -NFS4ERR_ADMIN_REVOKED
:
1896 case -NFS4ERR_EXPIRED
:
1897 case -NFS4ERR_BAD_STATEID
:
1898 case -NFS4ERR_OPENMODE
:
1899 nfs_inode_find_state_and_recover(state
->inode
,
1901 nfs4_schedule_stateid_recovery(server
, state
);
1903 case -NFS4ERR_DELAY
:
1904 case -NFS4ERR_GRACE
:
1905 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1909 case -NFS4ERR_DENIED
:
1910 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1916 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
,
1917 struct nfs4_state
*state
, const nfs4_stateid
*stateid
,
1920 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1921 struct nfs4_opendata
*opendata
;
1924 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1925 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
1926 if (IS_ERR(opendata
))
1927 return PTR_ERR(opendata
);
1928 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
1929 write_seqlock(&state
->seqlock
);
1930 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1931 write_sequnlock(&state
->seqlock
);
1932 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1933 switch (type
& (FMODE_READ
|FMODE_WRITE
)) {
1934 case FMODE_READ
|FMODE_WRITE
:
1936 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1939 err
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1943 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1945 nfs4_opendata_put(opendata
);
1946 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
1949 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
1951 struct nfs4_opendata
*data
= calldata
;
1953 nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
1954 &data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, task
);
1957 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1959 struct nfs4_opendata
*data
= calldata
;
1961 nfs40_sequence_done(task
, &data
->c_res
.seq_res
);
1963 data
->rpc_status
= task
->tk_status
;
1964 if (data
->rpc_status
== 0) {
1965 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
1966 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1967 renew_lease(data
->o_res
.server
, data
->timestamp
);
1968 data
->rpc_done
= true;
1972 static void nfs4_open_confirm_release(void *calldata
)
1974 struct nfs4_opendata
*data
= calldata
;
1975 struct nfs4_state
*state
= NULL
;
1977 /* If this request hasn't been cancelled, do nothing */
1978 if (!data
->cancelled
)
1980 /* In case of error, no cleanup! */
1981 if (!data
->rpc_done
)
1983 state
= nfs4_opendata_to_nfs4_state(data
);
1985 nfs4_close_state(state
, data
->o_arg
.fmode
);
1987 nfs4_opendata_put(data
);
1990 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1991 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
1992 .rpc_call_done
= nfs4_open_confirm_done
,
1993 .rpc_release
= nfs4_open_confirm_release
,
1997 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1999 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
2001 struct nfs_server
*server
= NFS_SERVER(d_inode(data
->dir
));
2002 struct rpc_task
*task
;
2003 struct rpc_message msg
= {
2004 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
2005 .rpc_argp
= &data
->c_arg
,
2006 .rpc_resp
= &data
->c_res
,
2007 .rpc_cred
= data
->owner
->so_cred
,
2009 struct rpc_task_setup task_setup_data
= {
2010 .rpc_client
= server
->client
,
2011 .rpc_message
= &msg
,
2012 .callback_ops
= &nfs4_open_confirm_ops
,
2013 .callback_data
= data
,
2014 .workqueue
= nfsiod_workqueue
,
2015 .flags
= RPC_TASK_ASYNC
,
2019 nfs4_init_sequence(&data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, 1);
2020 kref_get(&data
->kref
);
2021 data
->rpc_done
= false;
2022 data
->rpc_status
= 0;
2023 data
->timestamp
= jiffies
;
2024 if (data
->is_recover
)
2025 nfs4_set_sequence_privileged(&data
->c_arg
.seq_args
);
2026 task
= rpc_run_task(&task_setup_data
);
2028 return PTR_ERR(task
);
2029 status
= rpc_wait_for_completion_task(task
);
2031 data
->cancelled
= true;
2034 status
= data
->rpc_status
;
2039 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
2041 struct nfs4_opendata
*data
= calldata
;
2042 struct nfs4_state_owner
*sp
= data
->owner
;
2043 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
2044 enum open_claim_type4 claim
= data
->o_arg
.claim
;
2046 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
2049 * Check if we still need to send an OPEN call, or if we can use
2050 * a delegation instead.
2052 if (data
->state
!= NULL
) {
2053 struct nfs_delegation
*delegation
;
2055 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
2058 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
2059 if (can_open_delegated(delegation
, data
->o_arg
.fmode
, claim
))
2060 goto unlock_no_action
;
2063 /* Update client id. */
2064 data
->o_arg
.clientid
= clp
->cl_clientid
;
2068 case NFS4_OPEN_CLAIM_PREVIOUS
:
2069 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
2070 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
2071 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
2072 case NFS4_OPEN_CLAIM_FH
:
2073 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
2074 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
2076 data
->timestamp
= jiffies
;
2077 if (nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
2078 &data
->o_arg
.seq_args
,
2079 &data
->o_res
.seq_res
,
2081 nfs_release_seqid(data
->o_arg
.seqid
);
2083 /* Set the create mode (note dependency on the session type) */
2084 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
2085 if (data
->o_arg
.open_flags
& O_EXCL
) {
2086 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
2087 if (nfs4_has_persistent_session(clp
))
2088 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
2089 else if (clp
->cl_mvops
->minor_version
> 0)
2090 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
2094 trace_nfs4_cached_open(data
->state
);
2097 task
->tk_action
= NULL
;
2099 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
2102 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
2104 struct nfs4_opendata
*data
= calldata
;
2106 data
->rpc_status
= task
->tk_status
;
2108 if (!nfs4_sequence_process(task
, &data
->o_res
.seq_res
))
2111 if (task
->tk_status
== 0) {
2112 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
2113 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
2117 data
->rpc_status
= -ELOOP
;
2120 data
->rpc_status
= -EISDIR
;
2123 data
->rpc_status
= -ENOTDIR
;
2126 renew_lease(data
->o_res
.server
, data
->timestamp
);
2127 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
2128 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2130 data
->rpc_done
= true;
2133 static void nfs4_open_release(void *calldata
)
2135 struct nfs4_opendata
*data
= calldata
;
2136 struct nfs4_state
*state
= NULL
;
2138 /* If this request hasn't been cancelled, do nothing */
2139 if (!data
->cancelled
)
2141 /* In case of error, no cleanup! */
2142 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
2144 /* In case we need an open_confirm, no cleanup! */
2145 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2147 state
= nfs4_opendata_to_nfs4_state(data
);
2149 nfs4_close_state(state
, data
->o_arg
.fmode
);
2151 nfs4_opendata_put(data
);
2154 static const struct rpc_call_ops nfs4_open_ops
= {
2155 .rpc_call_prepare
= nfs4_open_prepare
,
2156 .rpc_call_done
= nfs4_open_done
,
2157 .rpc_release
= nfs4_open_release
,
2160 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
2162 struct inode
*dir
= d_inode(data
->dir
);
2163 struct nfs_server
*server
= NFS_SERVER(dir
);
2164 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2165 struct nfs_openres
*o_res
= &data
->o_res
;
2166 struct rpc_task
*task
;
2167 struct rpc_message msg
= {
2168 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
2171 .rpc_cred
= data
->owner
->so_cred
,
2173 struct rpc_task_setup task_setup_data
= {
2174 .rpc_client
= server
->client
,
2175 .rpc_message
= &msg
,
2176 .callback_ops
= &nfs4_open_ops
,
2177 .callback_data
= data
,
2178 .workqueue
= nfsiod_workqueue
,
2179 .flags
= RPC_TASK_ASYNC
,
2183 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
2184 kref_get(&data
->kref
);
2185 data
->rpc_done
= false;
2186 data
->rpc_status
= 0;
2187 data
->cancelled
= false;
2188 data
->is_recover
= false;
2190 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2191 data
->is_recover
= true;
2193 task
= rpc_run_task(&task_setup_data
);
2195 return PTR_ERR(task
);
2196 status
= rpc_wait_for_completion_task(task
);
2198 data
->cancelled
= true;
2201 status
= data
->rpc_status
;
2207 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
2209 struct inode
*dir
= d_inode(data
->dir
);
2210 struct nfs_openres
*o_res
= &data
->o_res
;
2213 status
= nfs4_run_open_task(data
, 1);
2214 if (status
!= 0 || !data
->rpc_done
)
2217 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
2219 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2220 status
= _nfs4_proc_open_confirm(data
);
2226 * Additional permission checks in order to distinguish between an
2227 * open for read, and an open for execute. This works around the
2228 * fact that NFSv4 OPEN treats read and execute permissions as being
2230 * Note that in the non-execute case, we want to turn off permission
2231 * checking if we just created a new file (POSIX open() semantics).
2233 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2234 struct nfs4_opendata
*opendata
,
2235 struct nfs4_state
*state
, fmode_t fmode
,
2238 struct nfs_access_entry cache
;
2241 /* access call failed or for some reason the server doesn't
2242 * support any access modes -- defer access call until later */
2243 if (opendata
->o_res
.access_supported
== 0)
2248 * Use openflags to check for exec, because fmode won't
2249 * always have FMODE_EXEC set when file open for exec.
2251 if (openflags
& __FMODE_EXEC
) {
2252 /* ONLY check for exec rights */
2253 if (S_ISDIR(state
->inode
->i_mode
))
2254 mask
= NFS4_ACCESS_LOOKUP
;
2256 mask
= NFS4_ACCESS_EXECUTE
;
2257 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2258 mask
= NFS4_ACCESS_READ
;
2261 cache
.jiffies
= jiffies
;
2262 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2263 nfs_access_add_cache(state
->inode
, &cache
);
2265 flags
= NFS4_ACCESS_READ
| NFS4_ACCESS_EXECUTE
| NFS4_ACCESS_LOOKUP
;
2266 if ((mask
& ~cache
.mask
& flags
) == 0)
2273 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2275 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2277 struct inode
*dir
= d_inode(data
->dir
);
2278 struct nfs_server
*server
= NFS_SERVER(dir
);
2279 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2280 struct nfs_openres
*o_res
= &data
->o_res
;
2283 status
= nfs4_run_open_task(data
, 0);
2284 if (!data
->rpc_done
)
2287 if (status
== -NFS4ERR_BADNAME
&&
2288 !(o_arg
->open_flags
& O_CREAT
))
2293 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2295 if (o_arg
->open_flags
& O_CREAT
) {
2296 if (o_arg
->open_flags
& O_EXCL
)
2297 data
->file_created
= true;
2298 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2299 data
->file_created
= true;
2300 if (data
->file_created
|| dir
->i_version
!= o_res
->cinfo
.after
)
2301 update_changeattr(dir
, &o_res
->cinfo
,
2302 o_res
->f_attr
->time_start
);
2304 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2305 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2306 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2307 status
= _nfs4_proc_open_confirm(data
);
2311 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
)) {
2312 nfs4_sequence_free_slot(&o_res
->seq_res
);
2313 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2320 * reclaim state on the server after a network partition.
2321 * Assumes caller holds the appropriate lock
2323 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2325 struct nfs4_opendata
*opendata
;
2328 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2329 NFS4_OPEN_CLAIM_FH
);
2330 if (IS_ERR(opendata
))
2331 return PTR_ERR(opendata
);
2332 ret
= nfs4_open_recover(opendata
, state
);
2334 d_drop(ctx
->dentry
);
2335 nfs4_opendata_put(opendata
);
2339 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2341 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2342 struct nfs4_exception exception
= { };
2346 err
= _nfs4_open_expired(ctx
, state
);
2347 trace_nfs4_open_expired(ctx
, 0, err
);
2348 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2353 case -NFS4ERR_GRACE
:
2354 case -NFS4ERR_DELAY
:
2355 nfs4_handle_exception(server
, err
, &exception
);
2358 } while (exception
.retry
);
2363 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2365 struct nfs_open_context
*ctx
;
2368 ctx
= nfs4_state_find_open_context(state
);
2371 ret
= nfs4_do_open_expired(ctx
, state
);
2372 put_nfs_open_context(ctx
);
2376 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
,
2377 const nfs4_stateid
*stateid
)
2379 nfs_remove_bad_delegation(state
->inode
, stateid
);
2380 write_seqlock(&state
->seqlock
);
2381 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2382 write_sequnlock(&state
->seqlock
);
2383 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2386 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2388 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2389 nfs_finish_clear_delegation_stateid(state
, NULL
);
2392 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2394 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2395 nfs40_clear_delegation_stateid(state
);
2396 return nfs4_open_expired(sp
, state
);
2399 static int nfs40_test_and_free_expired_stateid(struct nfs_server
*server
,
2400 nfs4_stateid
*stateid
,
2401 struct rpc_cred
*cred
)
2403 return -NFS4ERR_BAD_STATEID
;
2406 #if defined(CONFIG_NFS_V4_1)
2407 static int nfs41_test_and_free_expired_stateid(struct nfs_server
*server
,
2408 nfs4_stateid
*stateid
,
2409 struct rpc_cred
*cred
)
2413 switch (stateid
->type
) {
2416 case NFS4_INVALID_STATEID_TYPE
:
2417 case NFS4_SPECIAL_STATEID_TYPE
:
2418 return -NFS4ERR_BAD_STATEID
;
2419 case NFS4_REVOKED_STATEID_TYPE
:
2423 status
= nfs41_test_stateid(server
, stateid
, cred
);
2425 case -NFS4ERR_EXPIRED
:
2426 case -NFS4ERR_ADMIN_REVOKED
:
2427 case -NFS4ERR_DELEG_REVOKED
:
2433 /* Ack the revoked state to the server */
2434 nfs41_free_stateid(server
, stateid
, cred
, true);
2435 return -NFS4ERR_EXPIRED
;
2438 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2440 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2441 nfs4_stateid stateid
;
2442 struct nfs_delegation
*delegation
;
2443 struct rpc_cred
*cred
;
2446 /* Get the delegation credential for use by test/free_stateid */
2448 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2449 if (delegation
== NULL
) {
2454 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2455 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) ||
2456 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED
,
2457 &delegation
->flags
)) {
2459 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2463 cred
= get_rpccred(delegation
->cred
);
2465 status
= nfs41_test_and_free_expired_stateid(server
, &stateid
, cred
);
2466 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2467 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
2468 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2474 * nfs41_check_expired_locks - possibly free a lock stateid
2476 * @state: NFSv4 state for an inode
2478 * Returns NFS_OK if recovery for this stateid is now finished.
2479 * Otherwise a negative NFS4ERR value is returned.
2481 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
2483 int status
, ret
= NFS_OK
;
2484 struct nfs4_lock_state
*lsp
, *prev
= NULL
;
2485 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2487 if (!test_bit(LK_STATE_IN_USE
, &state
->flags
))
2490 spin_lock(&state
->state_lock
);
2491 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
2492 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
2493 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
2495 atomic_inc(&lsp
->ls_count
);
2496 spin_unlock(&state
->state_lock
);
2498 nfs4_put_lock_state(prev
);
2501 status
= nfs41_test_and_free_expired_stateid(server
,
2504 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
2505 if (status
== -NFS4ERR_EXPIRED
||
2506 status
== -NFS4ERR_BAD_STATEID
) {
2507 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
2508 lsp
->ls_stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
2509 if (!recover_lost_locks
)
2510 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2511 } else if (status
!= NFS_OK
) {
2513 nfs4_put_lock_state(prev
);
2516 spin_lock(&state
->state_lock
);
2519 spin_unlock(&state
->state_lock
);
2520 nfs4_put_lock_state(prev
);
2526 * nfs41_check_open_stateid - possibly free an open stateid
2528 * @state: NFSv4 state for an inode
2530 * Returns NFS_OK if recovery for this stateid is now finished.
2531 * Otherwise a negative NFS4ERR value is returned.
2533 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2535 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2536 nfs4_stateid
*stateid
= &state
->open_stateid
;
2537 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2540 if (test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0) {
2541 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0) {
2542 if (nfs4_have_delegation(state
->inode
, state
->state
))
2544 return -NFS4ERR_OPENMODE
;
2546 return -NFS4ERR_BAD_STATEID
;
2548 status
= nfs41_test_and_free_expired_stateid(server
, stateid
, cred
);
2549 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2550 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
) {
2551 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2552 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2553 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2554 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2555 stateid
->type
= NFS4_INVALID_STATEID_TYPE
;
2557 if (status
!= NFS_OK
)
2559 if (nfs_open_stateid_recover_openmode(state
))
2560 return -NFS4ERR_OPENMODE
;
2564 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2568 nfs41_check_delegation_stateid(state
);
2569 status
= nfs41_check_expired_locks(state
);
2570 if (status
!= NFS_OK
)
2572 status
= nfs41_check_open_stateid(state
);
2573 if (status
!= NFS_OK
)
2574 status
= nfs4_open_expired(sp
, state
);
2580 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2581 * fields corresponding to attributes that were used to store the verifier.
2582 * Make sure we clobber those fields in the later setattr call
2584 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2585 struct iattr
*sattr
, struct nfs4_label
**label
)
2587 const u32
*attrset
= opendata
->o_res
.attrset
;
2589 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2590 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2591 sattr
->ia_valid
|= ATTR_ATIME
;
2593 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2594 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2595 sattr
->ia_valid
|= ATTR_MTIME
;
2597 /* Except MODE, it seems harmless of setting twice. */
2598 if (opendata
->o_arg
.createmode
!= NFS4_CREATE_EXCLUSIVE
&&
2599 (attrset
[1] & FATTR4_WORD1_MODE
||
2600 attrset
[2] & FATTR4_WORD2_MODE_UMASK
))
2601 sattr
->ia_valid
&= ~ATTR_MODE
;
2603 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2607 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2610 struct nfs_open_context
*ctx
)
2612 struct nfs4_state_owner
*sp
= opendata
->owner
;
2613 struct nfs_server
*server
= sp
->so_server
;
2614 struct dentry
*dentry
;
2615 struct nfs4_state
*state
;
2619 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2621 ret
= _nfs4_proc_open(opendata
);
2625 state
= nfs4_opendata_to_nfs4_state(opendata
);
2626 ret
= PTR_ERR(state
);
2630 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2631 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2632 if (opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
)
2633 set_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
);
2635 dentry
= opendata
->dentry
;
2636 if (d_really_is_negative(dentry
)) {
2637 struct dentry
*alias
;
2639 alias
= d_exact_alias(dentry
, state
->inode
);
2641 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2642 /* d_splice_alias() can't fail here - it's a non-directory */
2645 ctx
->dentry
= dentry
= alias
;
2647 nfs_set_verifier(dentry
,
2648 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2651 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2655 if (d_inode(dentry
) == state
->inode
) {
2656 nfs_inode_attach_open_context(ctx
);
2657 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2658 nfs4_schedule_stateid_recovery(server
, state
);
2665 * Returns a referenced nfs4_state
2667 static int _nfs4_do_open(struct inode
*dir
,
2668 struct nfs_open_context
*ctx
,
2670 struct iattr
*sattr
,
2671 struct nfs4_label
*label
,
2674 struct nfs4_state_owner
*sp
;
2675 struct nfs4_state
*state
= NULL
;
2676 struct nfs_server
*server
= NFS_SERVER(dir
);
2677 struct nfs4_opendata
*opendata
;
2678 struct dentry
*dentry
= ctx
->dentry
;
2679 struct rpc_cred
*cred
= ctx
->cred
;
2680 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2681 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2682 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2683 struct nfs4_label
*olabel
= NULL
;
2686 /* Protect against reboot recovery conflicts */
2688 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2690 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2693 status
= nfs4_client_recover_expired_lease(server
->nfs_client
);
2695 goto err_put_state_owner
;
2696 if (d_really_is_positive(dentry
))
2697 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2699 if (d_really_is_positive(dentry
))
2700 claim
= NFS4_OPEN_CLAIM_FH
;
2701 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2702 label
, claim
, GFP_KERNEL
);
2703 if (opendata
== NULL
)
2704 goto err_put_state_owner
;
2707 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2708 if (IS_ERR(olabel
)) {
2709 status
= PTR_ERR(olabel
);
2710 goto err_opendata_put
;
2714 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2715 if (!opendata
->f_attr
.mdsthreshold
) {
2716 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2717 if (!opendata
->f_attr
.mdsthreshold
)
2718 goto err_free_label
;
2720 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2722 if (d_really_is_positive(dentry
))
2723 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2725 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2727 goto err_free_label
;
2730 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2731 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2732 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2734 * send create attributes which was not set by open
2735 * with an extra setattr.
2737 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2738 nfs_fattr_init(opendata
->o_res
.f_attr
);
2739 status
= nfs4_do_setattr(state
->inode
, cred
,
2740 opendata
->o_res
.f_attr
, sattr
,
2741 ctx
, label
, olabel
);
2743 nfs_setattr_update_inode(state
->inode
, sattr
,
2744 opendata
->o_res
.f_attr
);
2745 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2749 if (opened
&& opendata
->file_created
)
2750 *opened
|= FILE_CREATED
;
2752 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2753 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2754 opendata
->f_attr
.mdsthreshold
= NULL
;
2757 nfs4_label_free(olabel
);
2759 nfs4_opendata_put(opendata
);
2760 nfs4_put_state_owner(sp
);
2763 nfs4_label_free(olabel
);
2765 nfs4_opendata_put(opendata
);
2766 err_put_state_owner
:
2767 nfs4_put_state_owner(sp
);
2773 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2774 struct nfs_open_context
*ctx
,
2776 struct iattr
*sattr
,
2777 struct nfs4_label
*label
,
2780 struct nfs_server
*server
= NFS_SERVER(dir
);
2781 struct nfs4_exception exception
= { };
2782 struct nfs4_state
*res
;
2786 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2788 trace_nfs4_open_file(ctx
, flags
, status
);
2791 /* NOTE: BAD_SEQID means the server and client disagree about the
2792 * book-keeping w.r.t. state-changing operations
2793 * (OPEN/CLOSE/LOCK/LOCKU...)
2794 * It is actually a sign of a bug on the client or on the server.
2796 * If we receive a BAD_SEQID error in the particular case of
2797 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2798 * have unhashed the old state_owner for us, and that we can
2799 * therefore safely retry using a new one. We should still warn
2800 * the user though...
2802 if (status
== -NFS4ERR_BAD_SEQID
) {
2803 pr_warn_ratelimited("NFS: v4 server %s "
2804 " returned a bad sequence-id error!\n",
2805 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2806 exception
.retry
= 1;
2810 * BAD_STATEID on OPEN means that the server cancelled our
2811 * state before it received the OPEN_CONFIRM.
2812 * Recover by retrying the request as per the discussion
2813 * on Page 181 of RFC3530.
2815 if (status
== -NFS4ERR_BAD_STATEID
) {
2816 exception
.retry
= 1;
2819 if (status
== -EAGAIN
) {
2820 /* We must have found a delegation */
2821 exception
.retry
= 1;
2824 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2826 res
= ERR_PTR(nfs4_handle_exception(server
,
2827 status
, &exception
));
2828 } while (exception
.retry
);
2832 static int _nfs4_do_setattr(struct inode
*inode
,
2833 struct nfs_setattrargs
*arg
,
2834 struct nfs_setattrres
*res
,
2835 struct rpc_cred
*cred
,
2836 struct nfs_open_context
*ctx
)
2838 struct nfs_server
*server
= NFS_SERVER(inode
);
2839 struct rpc_message msg
= {
2840 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2845 struct rpc_cred
*delegation_cred
= NULL
;
2846 unsigned long timestamp
= jiffies
;
2851 nfs_fattr_init(res
->fattr
);
2853 /* Servers should only apply open mode checks for file size changes */
2854 truncate
= (arg
->iap
->ia_valid
& ATTR_SIZE
) ? true : false;
2855 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2857 if (nfs4_copy_delegation_stateid(inode
, fmode
, &arg
->stateid
, &delegation_cred
)) {
2858 /* Use that stateid */
2859 } else if (truncate
&& ctx
!= NULL
) {
2860 struct nfs_lock_context
*l_ctx
;
2861 if (!nfs4_valid_open_stateid(ctx
->state
))
2863 l_ctx
= nfs_get_lock_context(ctx
);
2865 return PTR_ERR(l_ctx
);
2866 status
= nfs4_select_rw_stateid(ctx
->state
, FMODE_WRITE
, l_ctx
,
2867 &arg
->stateid
, &delegation_cred
);
2868 nfs_put_lock_context(l_ctx
);
2872 nfs4_stateid_copy(&arg
->stateid
, &zero_stateid
);
2873 if (delegation_cred
)
2874 msg
.rpc_cred
= delegation_cred
;
2876 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
->seq_args
, &res
->seq_res
, 1);
2878 put_rpccred(delegation_cred
);
2879 if (status
== 0 && ctx
!= NULL
)
2880 renew_lease(server
, timestamp
);
2881 trace_nfs4_setattr(inode
, &arg
->stateid
, status
);
2885 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2886 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2887 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
2888 struct nfs4_label
*olabel
)
2890 struct nfs_server
*server
= NFS_SERVER(inode
);
2891 struct nfs4_state
*state
= ctx
? ctx
->state
: NULL
;
2892 struct nfs_setattrargs arg
= {
2893 .fh
= NFS_FH(inode
),
2896 .bitmask
= server
->attr_bitmask
,
2899 struct nfs_setattrres res
= {
2904 struct nfs4_exception exception
= {
2907 .stateid
= &arg
.stateid
,
2911 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2913 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2916 err
= _nfs4_do_setattr(inode
, &arg
, &res
, cred
, ctx
);
2918 case -NFS4ERR_OPENMODE
:
2919 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2920 pr_warn_once("NFSv4: server %s is incorrectly "
2921 "applying open mode checks to "
2922 "a SETATTR that is not "
2923 "changing file size.\n",
2924 server
->nfs_client
->cl_hostname
);
2926 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2928 if (sattr
->ia_valid
& ATTR_OPEN
)
2933 err
= nfs4_handle_exception(server
, err
, &exception
);
2934 } while (exception
.retry
);
2940 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
2942 if (inode
== NULL
|| !nfs_have_layout(inode
))
2945 return pnfs_wait_on_layoutreturn(inode
, task
);
2948 struct nfs4_closedata
{
2949 struct inode
*inode
;
2950 struct nfs4_state
*state
;
2951 struct nfs_closeargs arg
;
2952 struct nfs_closeres res
;
2954 struct nfs4_layoutreturn_args arg
;
2955 struct nfs4_layoutreturn_res res
;
2956 struct nfs4_xdr_opaque_data ld_private
;
2960 struct nfs_fattr fattr
;
2961 unsigned long timestamp
;
2964 static void nfs4_free_closedata(void *data
)
2966 struct nfs4_closedata
*calldata
= data
;
2967 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2968 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2970 if (calldata
->lr
.roc
)
2971 pnfs_roc_release(&calldata
->lr
.arg
, &calldata
->lr
.res
,
2972 calldata
->res
.lr_ret
);
2973 nfs4_put_open_state(calldata
->state
);
2974 nfs_free_seqid(calldata
->arg
.seqid
);
2975 nfs4_put_state_owner(sp
);
2976 nfs_sb_deactive(sb
);
2980 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2982 struct nfs4_closedata
*calldata
= data
;
2983 struct nfs4_state
*state
= calldata
->state
;
2984 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2985 nfs4_stateid
*res_stateid
= NULL
;
2987 dprintk("%s: begin!\n", __func__
);
2988 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2990 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
2992 /* Handle Layoutreturn errors */
2993 if (calldata
->arg
.lr_args
&& task
->tk_status
!= 0) {
2994 switch (calldata
->res
.lr_ret
) {
2996 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
2999 calldata
->arg
.lr_args
= NULL
;
3000 calldata
->res
.lr_res
= NULL
;
3002 case -NFS4ERR_ADMIN_REVOKED
:
3003 case -NFS4ERR_DELEG_REVOKED
:
3004 case -NFS4ERR_EXPIRED
:
3005 case -NFS4ERR_BAD_STATEID
:
3006 case -NFS4ERR_OLD_STATEID
:
3007 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
3008 case -NFS4ERR_WRONG_CRED
:
3009 calldata
->arg
.lr_args
= NULL
;
3010 calldata
->res
.lr_res
= NULL
;
3011 calldata
->res
.lr_ret
= 0;
3012 rpc_restart_call_prepare(task
);
3017 /* hmm. we are done with the inode, and in the process of freeing
3018 * the state_owner. we keep this around to process errors
3020 switch (task
->tk_status
) {
3022 res_stateid
= &calldata
->res
.stateid
;
3023 renew_lease(server
, calldata
->timestamp
);
3025 case -NFS4ERR_ACCESS
:
3026 if (calldata
->arg
.bitmask
!= NULL
) {
3027 calldata
->arg
.bitmask
= NULL
;
3028 calldata
->res
.fattr
= NULL
;
3029 task
->tk_status
= 0;
3030 rpc_restart_call_prepare(task
);
3035 case -NFS4ERR_ADMIN_REVOKED
:
3036 case -NFS4ERR_STALE_STATEID
:
3037 case -NFS4ERR_EXPIRED
:
3038 nfs4_free_revoked_stateid(server
,
3039 &calldata
->arg
.stateid
,
3040 task
->tk_msg
.rpc_cred
);
3041 case -NFS4ERR_OLD_STATEID
:
3042 case -NFS4ERR_BAD_STATEID
:
3043 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
3044 &state
->open_stateid
)) {
3045 rpc_restart_call_prepare(task
);
3048 if (calldata
->arg
.fmode
== 0)
3051 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
3052 rpc_restart_call_prepare(task
);
3056 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
3057 res_stateid
, calldata
->arg
.fmode
);
3059 nfs_release_seqid(calldata
->arg
.seqid
);
3060 nfs_refresh_inode(calldata
->inode
, &calldata
->fattr
);
3061 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
3064 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
3066 struct nfs4_closedata
*calldata
= data
;
3067 struct nfs4_state
*state
= calldata
->state
;
3068 struct inode
*inode
= calldata
->inode
;
3069 bool is_rdonly
, is_wronly
, is_rdwr
;
3072 dprintk("%s: begin!\n", __func__
);
3073 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3076 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
3077 spin_lock(&state
->owner
->so_lock
);
3078 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
3079 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
3080 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
3081 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
3082 /* Calculate the change in open mode */
3083 calldata
->arg
.fmode
= 0;
3084 if (state
->n_rdwr
== 0) {
3085 if (state
->n_rdonly
== 0)
3086 call_close
|= is_rdonly
;
3088 calldata
->arg
.fmode
|= FMODE_READ
;
3089 if (state
->n_wronly
== 0)
3090 call_close
|= is_wronly
;
3092 calldata
->arg
.fmode
|= FMODE_WRITE
;
3093 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
3094 call_close
|= is_rdwr
;
3096 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
3098 if (!nfs4_valid_open_stateid(state
) ||
3099 test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
3101 spin_unlock(&state
->owner
->so_lock
);
3104 /* Note: exit _without_ calling nfs4_close_done */
3108 if (!calldata
->lr
.roc
&& nfs4_wait_on_layoutreturn(inode
, task
)) {
3109 nfs_release_seqid(calldata
->arg
.seqid
);
3113 if (calldata
->arg
.fmode
== 0)
3114 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
3116 if (calldata
->arg
.fmode
== 0 || calldata
->arg
.fmode
== FMODE_READ
) {
3117 /* Close-to-open cache consistency revalidation */
3118 if (!nfs4_have_delegation(inode
, FMODE_READ
))
3119 calldata
->arg
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
3121 calldata
->arg
.bitmask
= NULL
;
3124 calldata
->arg
.share_access
=
3125 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
3126 calldata
->arg
.fmode
, 0);
3128 if (calldata
->res
.fattr
== NULL
)
3129 calldata
->arg
.bitmask
= NULL
;
3130 else if (calldata
->arg
.bitmask
== NULL
)
3131 calldata
->res
.fattr
= NULL
;
3132 calldata
->timestamp
= jiffies
;
3133 if (nfs4_setup_sequence(NFS_SERVER(inode
)->nfs_client
,
3134 &calldata
->arg
.seq_args
,
3135 &calldata
->res
.seq_res
,
3137 nfs_release_seqid(calldata
->arg
.seqid
);
3138 dprintk("%s: done!\n", __func__
);
3141 task
->tk_action
= NULL
;
3143 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
3146 static const struct rpc_call_ops nfs4_close_ops
= {
3147 .rpc_call_prepare
= nfs4_close_prepare
,
3148 .rpc_call_done
= nfs4_close_done
,
3149 .rpc_release
= nfs4_free_closedata
,
3153 * It is possible for data to be read/written from a mem-mapped file
3154 * after the sys_close call (which hits the vfs layer as a flush).
3155 * This means that we can't safely call nfsv4 close on a file until
3156 * the inode is cleared. This in turn means that we are not good
3157 * NFSv4 citizens - we do not indicate to the server to update the file's
3158 * share state even when we are done with one of the three share
3159 * stateid's in the inode.
3161 * NOTE: Caller must be holding the sp->so_owner semaphore!
3163 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
3165 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3166 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
3167 struct nfs4_closedata
*calldata
;
3168 struct nfs4_state_owner
*sp
= state
->owner
;
3169 struct rpc_task
*task
;
3170 struct rpc_message msg
= {
3171 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
3172 .rpc_cred
= state
->owner
->so_cred
,
3174 struct rpc_task_setup task_setup_data
= {
3175 .rpc_client
= server
->client
,
3176 .rpc_message
= &msg
,
3177 .callback_ops
= &nfs4_close_ops
,
3178 .workqueue
= nfsiod_workqueue
,
3179 .flags
= RPC_TASK_ASYNC
,
3181 int status
= -ENOMEM
;
3183 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
3184 &task_setup_data
.rpc_client
, &msg
);
3186 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
3187 if (calldata
== NULL
)
3189 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
3190 calldata
->inode
= state
->inode
;
3191 calldata
->state
= state
;
3192 calldata
->arg
.fh
= NFS_FH(state
->inode
);
3193 /* Serialization for the sequence id */
3194 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
3195 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
3196 if (IS_ERR(calldata
->arg
.seqid
))
3197 goto out_free_calldata
;
3198 nfs_fattr_init(&calldata
->fattr
);
3199 calldata
->arg
.fmode
= 0;
3200 calldata
->lr
.arg
.ld_private
= &calldata
->lr
.ld_private
;
3201 calldata
->res
.fattr
= &calldata
->fattr
;
3202 calldata
->res
.seqid
= calldata
->arg
.seqid
;
3203 calldata
->res
.server
= server
;
3204 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3205 calldata
->lr
.roc
= pnfs_roc(state
->inode
,
3206 &calldata
->lr
.arg
, &calldata
->lr
.res
, msg
.rpc_cred
);
3207 if (calldata
->lr
.roc
) {
3208 calldata
->arg
.lr_args
= &calldata
->lr
.arg
;
3209 calldata
->res
.lr_res
= &calldata
->lr
.res
;
3211 nfs_sb_active(calldata
->inode
->i_sb
);
3213 msg
.rpc_argp
= &calldata
->arg
;
3214 msg
.rpc_resp
= &calldata
->res
;
3215 task_setup_data
.callback_data
= calldata
;
3216 task
= rpc_run_task(&task_setup_data
);
3218 return PTR_ERR(task
);
3221 status
= rpc_wait_for_completion_task(task
);
3227 nfs4_put_open_state(state
);
3228 nfs4_put_state_owner(sp
);
3232 static struct inode
*
3233 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3234 int open_flags
, struct iattr
*attr
, int *opened
)
3236 struct nfs4_state
*state
;
3237 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3239 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3241 /* Protect against concurrent sillydeletes */
3242 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3244 nfs4_label_release_security(label
);
3247 return ERR_CAST(state
);
3248 return state
->inode
;
3251 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3253 if (ctx
->state
== NULL
)
3256 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3258 nfs4_close_state(ctx
->state
, ctx
->mode
);
3261 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3262 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3263 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3265 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3267 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3268 struct nfs4_server_caps_arg args
= {
3272 struct nfs4_server_caps_res res
= {};
3273 struct rpc_message msg
= {
3274 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3281 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3282 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3283 FATTR4_WORD0_LINK_SUPPORT
|
3284 FATTR4_WORD0_SYMLINK_SUPPORT
|
3285 FATTR4_WORD0_ACLSUPPORT
;
3287 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3289 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3291 /* Sanity check the server answers */
3292 switch (minorversion
) {
3294 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3295 res
.attr_bitmask
[2] = 0;
3298 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3301 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3303 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3304 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3305 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3306 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3307 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3308 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3309 NFS_CAP_SECURITY_LABEL
);
3310 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3311 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3312 server
->caps
|= NFS_CAP_ACLS
;
3313 if (res
.has_links
!= 0)
3314 server
->caps
|= NFS_CAP_HARDLINKS
;
3315 if (res
.has_symlinks
!= 0)
3316 server
->caps
|= NFS_CAP_SYMLINKS
;
3317 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3318 server
->caps
|= NFS_CAP_FILEID
;
3319 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3320 server
->caps
|= NFS_CAP_MODE
;
3321 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3322 server
->caps
|= NFS_CAP_NLINK
;
3323 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3324 server
->caps
|= NFS_CAP_OWNER
;
3325 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3326 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3327 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3328 server
->caps
|= NFS_CAP_ATIME
;
3329 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3330 server
->caps
|= NFS_CAP_CTIME
;
3331 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3332 server
->caps
|= NFS_CAP_MTIME
;
3333 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3334 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3335 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3337 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3338 sizeof(server
->attr_bitmask
));
3339 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3341 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3342 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3343 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3344 server
->cache_consistency_bitmask
[2] = 0;
3346 /* Avoid a regression due to buggy server */
3347 for (i
= 0; i
< ARRAY_SIZE(res
.exclcreat_bitmask
); i
++)
3348 res
.exclcreat_bitmask
[i
] &= res
.attr_bitmask
[i
];
3349 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3350 sizeof(server
->exclcreat_bitmask
));
3352 server
->acl_bitmask
= res
.acl_bitmask
;
3353 server
->fh_expire_type
= res
.fh_expire_type
;
3359 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3361 struct nfs4_exception exception
= { };
3364 err
= nfs4_handle_exception(server
,
3365 _nfs4_server_capabilities(server
, fhandle
),
3367 } while (exception
.retry
);
3371 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3372 struct nfs_fsinfo
*info
)
3375 struct nfs4_lookup_root_arg args
= {
3378 struct nfs4_lookup_res res
= {
3380 .fattr
= info
->fattr
,
3383 struct rpc_message msg
= {
3384 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3389 bitmask
[0] = nfs4_fattr_bitmap
[0];
3390 bitmask
[1] = nfs4_fattr_bitmap
[1];
3392 * Process the label in the upcoming getfattr
3394 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3396 nfs_fattr_init(info
->fattr
);
3397 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3400 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3401 struct nfs_fsinfo
*info
)
3403 struct nfs4_exception exception
= { };
3406 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3407 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3410 case -NFS4ERR_WRONGSEC
:
3413 err
= nfs4_handle_exception(server
, err
, &exception
);
3415 } while (exception
.retry
);
3420 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3421 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3423 struct rpc_auth_create_args auth_args
= {
3424 .pseudoflavor
= flavor
,
3426 struct rpc_auth
*auth
;
3428 auth
= rpcauth_create(&auth_args
, server
->client
);
3431 return nfs4_lookup_root(server
, fhandle
, info
);
3435 * Retry pseudoroot lookup with various security flavors. We do this when:
3437 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3438 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3440 * Returns zero on success, or a negative NFS4ERR value, or a
3441 * negative errno value.
3443 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3444 struct nfs_fsinfo
*info
)
3446 /* Per 3530bis 15.33.5 */
3447 static const rpc_authflavor_t flav_array
[] = {
3451 RPC_AUTH_UNIX
, /* courtesy */
3454 int status
= -EPERM
;
3457 if (server
->auth_info
.flavor_len
> 0) {
3458 /* try each flavor specified by user */
3459 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3460 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3461 server
->auth_info
.flavors
[i
]);
3462 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3467 /* no flavors specified by user, try default list */
3468 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3469 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3471 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3478 * -EACCESS could mean that the user doesn't have correct permissions
3479 * to access the mount. It could also mean that we tried to mount
3480 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3481 * existing mount programs don't handle -EACCES very well so it should
3482 * be mapped to -EPERM instead.
3484 if (status
== -EACCES
)
3490 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3491 * @server: initialized nfs_server handle
3492 * @fhandle: we fill in the pseudo-fs root file handle
3493 * @info: we fill in an FSINFO struct
3494 * @auth_probe: probe the auth flavours
3496 * Returns zero on success, or a negative errno.
3498 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3499 struct nfs_fsinfo
*info
,
3505 status
= nfs4_lookup_root(server
, fhandle
, info
);
3507 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3508 status
= server
->nfs_client
->cl_mvops
->find_root_sec(server
,
3512 status
= nfs4_server_capabilities(server
, fhandle
);
3514 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3516 return nfs4_map_errors(status
);
3519 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3520 struct nfs_fsinfo
*info
)
3523 struct nfs_fattr
*fattr
= info
->fattr
;
3524 struct nfs4_label
*label
= NULL
;
3526 error
= nfs4_server_capabilities(server
, mntfh
);
3528 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3532 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3534 return PTR_ERR(label
);
3536 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3538 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3539 goto err_free_label
;
3542 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3543 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3544 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3547 nfs4_label_free(label
);
3553 * Get locations and (maybe) other attributes of a referral.
3554 * Note that we'll actually follow the referral later when
3555 * we detect fsid mismatch in inode revalidation
3557 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3558 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3559 struct nfs_fh
*fhandle
)
3561 int status
= -ENOMEM
;
3562 struct page
*page
= NULL
;
3563 struct nfs4_fs_locations
*locations
= NULL
;
3565 page
= alloc_page(GFP_KERNEL
);
3568 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3569 if (locations
== NULL
)
3572 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3577 * If the fsid didn't change, this is a migration event, not a
3578 * referral. Cause us to drop into the exception handler, which
3579 * will kick off migration recovery.
3581 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3582 dprintk("%s: server did not return a different fsid for"
3583 " a referral at %s\n", __func__
, name
->name
);
3584 status
= -NFS4ERR_MOVED
;
3587 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3588 nfs_fixup_referral_attributes(&locations
->fattr
);
3590 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3591 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3592 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3600 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3601 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3603 struct nfs4_getattr_arg args
= {
3605 .bitmask
= server
->attr_bitmask
,
3607 struct nfs4_getattr_res res
= {
3612 struct rpc_message msg
= {
3613 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3618 args
.bitmask
= nfs4_bitmask(server
, label
);
3620 nfs_fattr_init(fattr
);
3621 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3624 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3625 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3627 struct nfs4_exception exception
= { };
3630 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3631 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3632 err
= nfs4_handle_exception(server
, err
,
3634 } while (exception
.retry
);
3639 * The file is not closed if it is opened due to the a request to change
3640 * the size of the file. The open call will not be needed once the
3641 * VFS layer lookup-intents are implemented.
3643 * Close is called when the inode is destroyed.
3644 * If we haven't opened the file for O_WRONLY, we
3645 * need to in the size_change case to obtain a stateid.
3648 * Because OPEN is always done by name in nfsv4, it is
3649 * possible that we opened a different file by the same
3650 * name. We can recognize this race condition, but we
3651 * can't do anything about it besides returning an error.
3653 * This will be fixed with VFS changes (lookup-intent).
3656 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3657 struct iattr
*sattr
)
3659 struct inode
*inode
= d_inode(dentry
);
3660 struct rpc_cred
*cred
= NULL
;
3661 struct nfs_open_context
*ctx
= NULL
;
3662 struct nfs4_label
*label
= NULL
;
3665 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3666 sattr
->ia_valid
& ATTR_SIZE
&&
3667 sattr
->ia_size
< i_size_read(inode
))
3668 pnfs_commit_and_return_layout(inode
);
3670 nfs_fattr_init(fattr
);
3672 /* Deal with open(O_TRUNC) */
3673 if (sattr
->ia_valid
& ATTR_OPEN
)
3674 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3676 /* Optimization: if the end result is no change, don't RPC */
3677 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3680 /* Search for an existing open(O_WRITE) file */
3681 if (sattr
->ia_valid
& ATTR_FILE
) {
3683 ctx
= nfs_file_open_context(sattr
->ia_file
);
3688 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3690 return PTR_ERR(label
);
3692 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, ctx
, NULL
, label
);
3694 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3695 nfs_setsecurity(inode
, fattr
, label
);
3697 nfs4_label_free(label
);
3701 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3702 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3703 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3705 struct nfs_server
*server
= NFS_SERVER(dir
);
3707 struct nfs4_lookup_arg args
= {
3708 .bitmask
= server
->attr_bitmask
,
3709 .dir_fh
= NFS_FH(dir
),
3712 struct nfs4_lookup_res res
= {
3718 struct rpc_message msg
= {
3719 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3724 args
.bitmask
= nfs4_bitmask(server
, label
);
3726 nfs_fattr_init(fattr
);
3728 dprintk("NFS call lookup %s\n", name
->name
);
3729 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3730 dprintk("NFS reply lookup: %d\n", status
);
3734 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3736 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3737 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3738 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3742 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3743 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3744 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3746 struct nfs4_exception exception
= { };
3747 struct rpc_clnt
*client
= *clnt
;
3750 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3751 trace_nfs4_lookup(dir
, name
, err
);
3753 case -NFS4ERR_BADNAME
:
3756 case -NFS4ERR_MOVED
:
3757 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3758 if (err
== -NFS4ERR_MOVED
)
3759 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3761 case -NFS4ERR_WRONGSEC
:
3763 if (client
!= *clnt
)
3765 client
= nfs4_negotiate_security(client
, dir
, name
);
3767 return PTR_ERR(client
);
3769 exception
.retry
= 1;
3772 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3774 } while (exception
.retry
);
3779 else if (client
!= *clnt
)
3780 rpc_shutdown_client(client
);
3785 static int nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
3786 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3787 struct nfs4_label
*label
)
3790 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3792 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3793 if (client
!= NFS_CLIENT(dir
)) {
3794 rpc_shutdown_client(client
);
3795 nfs_fixup_secinfo_attributes(fattr
);
3801 nfs4_proc_lookup_mountpoint(struct inode
*dir
, const struct qstr
*name
,
3802 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3804 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3807 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3809 return ERR_PTR(status
);
3810 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3813 static int _nfs4_proc_lookupp(struct inode
*inode
,
3814 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3815 struct nfs4_label
*label
)
3817 struct rpc_clnt
*clnt
= NFS_CLIENT(inode
);
3818 struct nfs_server
*server
= NFS_SERVER(inode
);
3820 struct nfs4_lookupp_arg args
= {
3821 .bitmask
= server
->attr_bitmask
,
3822 .fh
= NFS_FH(inode
),
3824 struct nfs4_lookupp_res res
= {
3830 struct rpc_message msg
= {
3831 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUPP
],
3836 args
.bitmask
= nfs4_bitmask(server
, label
);
3838 nfs_fattr_init(fattr
);
3840 dprintk("NFS call lookupp ino=0x%lx\n", inode
->i_ino
);
3841 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
3843 dprintk("NFS reply lookupp: %d\n", status
);
3847 static int nfs4_proc_lookupp(struct inode
*inode
, struct nfs_fh
*fhandle
,
3848 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3850 struct nfs4_exception exception
= { };
3853 err
= _nfs4_proc_lookupp(inode
, fhandle
, fattr
, label
);
3854 trace_nfs4_lookupp(inode
, err
);
3855 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3857 } while (exception
.retry
);
3861 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3863 struct nfs_server
*server
= NFS_SERVER(inode
);
3864 struct nfs4_accessargs args
= {
3865 .fh
= NFS_FH(inode
),
3866 .bitmask
= server
->cache_consistency_bitmask
,
3868 struct nfs4_accessres res
= {
3871 struct rpc_message msg
= {
3872 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3875 .rpc_cred
= entry
->cred
,
3877 int mode
= entry
->mask
;
3881 * Determine which access bits we want to ask for...
3883 if (mode
& MAY_READ
)
3884 args
.access
|= NFS4_ACCESS_READ
;
3885 if (S_ISDIR(inode
->i_mode
)) {
3886 if (mode
& MAY_WRITE
)
3887 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3888 if (mode
& MAY_EXEC
)
3889 args
.access
|= NFS4_ACCESS_LOOKUP
;
3891 if (mode
& MAY_WRITE
)
3892 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3893 if (mode
& MAY_EXEC
)
3894 args
.access
|= NFS4_ACCESS_EXECUTE
;
3897 res
.fattr
= nfs_alloc_fattr();
3898 if (res
.fattr
== NULL
)
3901 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3903 nfs_access_set_mask(entry
, res
.access
);
3904 nfs_refresh_inode(inode
, res
.fattr
);
3906 nfs_free_fattr(res
.fattr
);
3910 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3912 struct nfs4_exception exception
= { };
3915 err
= _nfs4_proc_access(inode
, entry
);
3916 trace_nfs4_access(inode
, err
);
3917 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3919 } while (exception
.retry
);
3924 * TODO: For the time being, we don't try to get any attributes
3925 * along with any of the zero-copy operations READ, READDIR,
3928 * In the case of the first three, we want to put the GETATTR
3929 * after the read-type operation -- this is because it is hard
3930 * to predict the length of a GETATTR response in v4, and thus
3931 * align the READ data correctly. This means that the GETATTR
3932 * may end up partially falling into the page cache, and we should
3933 * shift it into the 'tail' of the xdr_buf before processing.
3934 * To do this efficiently, we need to know the total length
3935 * of data received, which doesn't seem to be available outside
3938 * In the case of WRITE, we also want to put the GETATTR after
3939 * the operation -- in this case because we want to make sure
3940 * we get the post-operation mtime and size.
3942 * Both of these changes to the XDR layer would in fact be quite
3943 * minor, but I decided to leave them for a subsequent patch.
3945 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3946 unsigned int pgbase
, unsigned int pglen
)
3948 struct nfs4_readlink args
= {
3949 .fh
= NFS_FH(inode
),
3954 struct nfs4_readlink_res res
;
3955 struct rpc_message msg
= {
3956 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3961 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3964 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3965 unsigned int pgbase
, unsigned int pglen
)
3967 struct nfs4_exception exception
= { };
3970 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3971 trace_nfs4_readlink(inode
, err
);
3972 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3974 } while (exception
.retry
);
3979 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3982 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3985 struct nfs_server
*server
= NFS_SERVER(dir
);
3986 struct nfs4_label l
, *ilabel
= NULL
;
3987 struct nfs_open_context
*ctx
;
3988 struct nfs4_state
*state
;
3991 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
, NULL
);
3993 return PTR_ERR(ctx
);
3995 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3997 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
3998 sattr
->ia_mode
&= ~current_umask();
3999 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
4000 if (IS_ERR(state
)) {
4001 status
= PTR_ERR(state
);
4005 nfs4_label_release_security(ilabel
);
4006 put_nfs_open_context(ctx
);
4010 static int _nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4012 struct nfs_server
*server
= NFS_SERVER(dir
);
4013 struct nfs_removeargs args
= {
4017 struct nfs_removeres res
= {
4020 struct rpc_message msg
= {
4021 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
4025 unsigned long timestamp
= jiffies
;
4028 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
4030 update_changeattr(dir
, &res
.cinfo
, timestamp
);
4034 static int nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
4036 struct nfs4_exception exception
= { };
4039 err
= _nfs4_proc_remove(dir
, name
);
4040 trace_nfs4_remove(dir
, name
, err
);
4041 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4043 } while (exception
.retry
);
4047 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
4049 struct nfs_server
*server
= NFS_SERVER(dir
);
4050 struct nfs_removeargs
*args
= msg
->rpc_argp
;
4051 struct nfs_removeres
*res
= msg
->rpc_resp
;
4053 res
->server
= server
;
4054 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
4055 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
4057 nfs_fattr_init(res
->dir_attr
);
4060 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
4062 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
)->nfs_client
,
4063 &data
->args
.seq_args
,
4068 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
4070 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
4071 struct nfs_removeres
*res
= &data
->res
;
4073 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4075 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
4076 &data
->timeout
) == -EAGAIN
)
4078 if (task
->tk_status
== 0)
4079 update_changeattr(dir
, &res
->cinfo
, res
->dir_attr
->time_start
);
4083 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
4085 struct nfs_server
*server
= NFS_SERVER(dir
);
4086 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
4087 struct nfs_renameres
*res
= msg
->rpc_resp
;
4089 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
4090 res
->server
= server
;
4091 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
4094 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
4096 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
)->nfs_client
,
4097 &data
->args
.seq_args
,
4102 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
4103 struct inode
*new_dir
)
4105 struct nfs_renamedata
*data
= task
->tk_calldata
;
4106 struct nfs_renameres
*res
= &data
->res
;
4108 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4110 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
4113 if (task
->tk_status
== 0) {
4114 update_changeattr(old_dir
, &res
->old_cinfo
, res
->old_fattr
->time_start
);
4115 if (new_dir
!= old_dir
)
4116 update_changeattr(new_dir
, &res
->new_cinfo
, res
->new_fattr
->time_start
);
4121 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4123 struct nfs_server
*server
= NFS_SERVER(inode
);
4124 struct nfs4_link_arg arg
= {
4125 .fh
= NFS_FH(inode
),
4126 .dir_fh
= NFS_FH(dir
),
4128 .bitmask
= server
->attr_bitmask
,
4130 struct nfs4_link_res res
= {
4134 struct rpc_message msg
= {
4135 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
4139 int status
= -ENOMEM
;
4141 res
.fattr
= nfs_alloc_fattr();
4142 if (res
.fattr
== NULL
)
4145 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4146 if (IS_ERR(res
.label
)) {
4147 status
= PTR_ERR(res
.label
);
4150 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
4152 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4154 update_changeattr(dir
, &res
.cinfo
, res
.fattr
->time_start
);
4155 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
4157 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
4161 nfs4_label_free(res
.label
);
4164 nfs_free_fattr(res
.fattr
);
4168 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4170 struct nfs4_exception exception
= { };
4173 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4174 _nfs4_proc_link(inode
, dir
, name
),
4176 } while (exception
.retry
);
4180 struct nfs4_createdata
{
4181 struct rpc_message msg
;
4182 struct nfs4_create_arg arg
;
4183 struct nfs4_create_res res
;
4185 struct nfs_fattr fattr
;
4186 struct nfs4_label
*label
;
4189 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
4190 const struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
4192 struct nfs4_createdata
*data
;
4194 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4196 struct nfs_server
*server
= NFS_SERVER(dir
);
4198 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4199 if (IS_ERR(data
->label
))
4202 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
4203 data
->msg
.rpc_argp
= &data
->arg
;
4204 data
->msg
.rpc_resp
= &data
->res
;
4205 data
->arg
.dir_fh
= NFS_FH(dir
);
4206 data
->arg
.server
= server
;
4207 data
->arg
.name
= name
;
4208 data
->arg
.attrs
= sattr
;
4209 data
->arg
.ftype
= ftype
;
4210 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
4211 data
->arg
.umask
= current_umask();
4212 data
->res
.server
= server
;
4213 data
->res
.fh
= &data
->fh
;
4214 data
->res
.fattr
= &data
->fattr
;
4215 data
->res
.label
= data
->label
;
4216 nfs_fattr_init(data
->res
.fattr
);
4224 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
4226 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
4227 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4229 update_changeattr(dir
, &data
->res
.dir_cinfo
,
4230 data
->res
.fattr
->time_start
);
4231 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
4236 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
4238 nfs4_label_free(data
->label
);
4242 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4243 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
4244 struct nfs4_label
*label
)
4246 struct nfs4_createdata
*data
;
4247 int status
= -ENAMETOOLONG
;
4249 if (len
> NFS4_MAXPATHLEN
)
4253 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
4257 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
4258 data
->arg
.u
.symlink
.pages
= &page
;
4259 data
->arg
.u
.symlink
.len
= len
;
4260 data
->arg
.label
= label
;
4262 status
= nfs4_do_create(dir
, dentry
, data
);
4264 nfs4_free_createdata(data
);
4269 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4270 struct page
*page
, unsigned int len
, 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
);
4279 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4280 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4281 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4283 } while (exception
.retry
);
4285 nfs4_label_release_security(label
);
4289 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4290 struct iattr
*sattr
, struct nfs4_label
*label
)
4292 struct nfs4_createdata
*data
;
4293 int status
= -ENOMEM
;
4295 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4299 data
->arg
.label
= label
;
4300 status
= nfs4_do_create(dir
, dentry
, data
);
4302 nfs4_free_createdata(data
);
4307 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4308 struct iattr
*sattr
)
4310 struct nfs_server
*server
= NFS_SERVER(dir
);
4311 struct nfs4_exception exception
= { };
4312 struct nfs4_label l
, *label
= NULL
;
4315 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4317 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4318 sattr
->ia_mode
&= ~current_umask();
4320 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4321 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4322 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4324 } while (exception
.retry
);
4325 nfs4_label_release_security(label
);
4330 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4331 u64 cookie
, struct page
**pages
, unsigned int count
, bool plus
)
4333 struct inode
*dir
= d_inode(dentry
);
4334 struct nfs4_readdir_arg args
= {
4339 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4342 struct nfs4_readdir_res res
;
4343 struct rpc_message msg
= {
4344 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4351 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4353 (unsigned long long)cookie
);
4354 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4355 res
.pgbase
= args
.pgbase
;
4356 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4358 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4359 status
+= args
.pgbase
;
4362 nfs_invalidate_atime(dir
);
4364 dprintk("%s: returns %d\n", __func__
, status
);
4368 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4369 u64 cookie
, struct page
**pages
, unsigned int count
, bool plus
)
4371 struct nfs4_exception exception
= { };
4374 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4375 pages
, count
, plus
);
4376 trace_nfs4_readdir(d_inode(dentry
), err
);
4377 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4379 } while (exception
.retry
);
4383 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4384 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4386 struct nfs4_createdata
*data
;
4387 int mode
= sattr
->ia_mode
;
4388 int status
= -ENOMEM
;
4390 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4395 data
->arg
.ftype
= NF4FIFO
;
4396 else if (S_ISBLK(mode
)) {
4397 data
->arg
.ftype
= NF4BLK
;
4398 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4399 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4401 else if (S_ISCHR(mode
)) {
4402 data
->arg
.ftype
= NF4CHR
;
4403 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4404 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4405 } else if (!S_ISSOCK(mode
)) {
4410 data
->arg
.label
= label
;
4411 status
= nfs4_do_create(dir
, dentry
, data
);
4413 nfs4_free_createdata(data
);
4418 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4419 struct iattr
*sattr
, dev_t rdev
)
4421 struct nfs_server
*server
= NFS_SERVER(dir
);
4422 struct nfs4_exception exception
= { };
4423 struct nfs4_label l
, *label
= NULL
;
4426 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4428 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4429 sattr
->ia_mode
&= ~current_umask();
4431 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4432 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4433 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4435 } while (exception
.retry
);
4437 nfs4_label_release_security(label
);
4442 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4443 struct nfs_fsstat
*fsstat
)
4445 struct nfs4_statfs_arg args
= {
4447 .bitmask
= server
->attr_bitmask
,
4449 struct nfs4_statfs_res res
= {
4452 struct rpc_message msg
= {
4453 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4458 nfs_fattr_init(fsstat
->fattr
);
4459 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4462 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4464 struct nfs4_exception exception
= { };
4467 err
= nfs4_handle_exception(server
,
4468 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4470 } while (exception
.retry
);
4474 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4475 struct nfs_fsinfo
*fsinfo
)
4477 struct nfs4_fsinfo_arg args
= {
4479 .bitmask
= server
->attr_bitmask
,
4481 struct nfs4_fsinfo_res res
= {
4484 struct rpc_message msg
= {
4485 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4490 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4493 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4495 struct nfs4_exception exception
= { };
4496 unsigned long now
= jiffies
;
4500 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4501 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4503 nfs4_set_lease_period(server
->nfs_client
,
4504 fsinfo
->lease_time
* HZ
,
4508 err
= nfs4_handle_exception(server
, err
, &exception
);
4509 } while (exception
.retry
);
4513 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4517 nfs_fattr_init(fsinfo
->fattr
);
4518 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4520 /* block layout checks this! */
4521 server
->pnfs_blksize
= fsinfo
->blksize
;
4522 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
);
4528 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4529 struct nfs_pathconf
*pathconf
)
4531 struct nfs4_pathconf_arg args
= {
4533 .bitmask
= server
->attr_bitmask
,
4535 struct nfs4_pathconf_res res
= {
4536 .pathconf
= pathconf
,
4538 struct rpc_message msg
= {
4539 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4544 /* None of the pathconf attributes are mandatory to implement */
4545 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4546 memset(pathconf
, 0, sizeof(*pathconf
));
4550 nfs_fattr_init(pathconf
->fattr
);
4551 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4554 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4555 struct nfs_pathconf
*pathconf
)
4557 struct nfs4_exception exception
= { };
4561 err
= nfs4_handle_exception(server
,
4562 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4564 } while (exception
.retry
);
4568 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4569 const struct nfs_open_context
*ctx
,
4570 const struct nfs_lock_context
*l_ctx
,
4573 return nfs4_select_rw_stateid(ctx
->state
, fmode
, l_ctx
, stateid
, NULL
);
4575 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4577 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4578 const struct nfs_open_context
*ctx
,
4579 const struct nfs_lock_context
*l_ctx
,
4582 nfs4_stateid current_stateid
;
4584 /* If the current stateid represents a lost lock, then exit */
4585 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4587 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4590 static bool nfs4_error_stateid_expired(int err
)
4593 case -NFS4ERR_DELEG_REVOKED
:
4594 case -NFS4ERR_ADMIN_REVOKED
:
4595 case -NFS4ERR_BAD_STATEID
:
4596 case -NFS4ERR_STALE_STATEID
:
4597 case -NFS4ERR_OLD_STATEID
:
4598 case -NFS4ERR_OPENMODE
:
4599 case -NFS4ERR_EXPIRED
:
4605 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4607 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4609 trace_nfs4_read(hdr
, task
->tk_status
);
4610 if (task
->tk_status
< 0) {
4611 struct nfs4_exception exception
= {
4612 .inode
= hdr
->inode
,
4613 .state
= hdr
->args
.context
->state
,
4614 .stateid
= &hdr
->args
.stateid
,
4616 task
->tk_status
= nfs4_async_handle_exception(task
,
4617 server
, task
->tk_status
, &exception
);
4618 if (exception
.retry
) {
4619 rpc_restart_call_prepare(task
);
4624 if (task
->tk_status
> 0)
4625 renew_lease(server
, hdr
->timestamp
);
4629 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4630 struct nfs_pgio_args
*args
)
4633 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4634 nfs4_stateid_is_current(&args
->stateid
,
4639 rpc_restart_call_prepare(task
);
4643 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4646 dprintk("--> %s\n", __func__
);
4648 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4650 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4652 if (task
->tk_status
> 0)
4653 nfs_invalidate_atime(hdr
->inode
);
4654 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4655 nfs4_read_done_cb(task
, hdr
);
4658 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4659 struct rpc_message
*msg
)
4661 hdr
->timestamp
= jiffies
;
4662 if (!hdr
->pgio_done_cb
)
4663 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4664 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4665 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4668 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4669 struct nfs_pgio_header
*hdr
)
4671 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
)->nfs_client
,
4672 &hdr
->args
.seq_args
,
4676 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4677 hdr
->args
.lock_context
,
4678 hdr
->rw_mode
) == -EIO
)
4680 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4685 static int nfs4_write_done_cb(struct rpc_task
*task
,
4686 struct nfs_pgio_header
*hdr
)
4688 struct inode
*inode
= hdr
->inode
;
4690 trace_nfs4_write(hdr
, task
->tk_status
);
4691 if (task
->tk_status
< 0) {
4692 struct nfs4_exception exception
= {
4693 .inode
= hdr
->inode
,
4694 .state
= hdr
->args
.context
->state
,
4695 .stateid
= &hdr
->args
.stateid
,
4697 task
->tk_status
= nfs4_async_handle_exception(task
,
4698 NFS_SERVER(inode
), task
->tk_status
,
4700 if (exception
.retry
) {
4701 rpc_restart_call_prepare(task
);
4705 if (task
->tk_status
>= 0) {
4706 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4707 nfs_writeback_update_inode(hdr
);
4712 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4713 struct nfs_pgio_args
*args
)
4716 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4717 nfs4_stateid_is_current(&args
->stateid
,
4722 rpc_restart_call_prepare(task
);
4726 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4728 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4730 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4732 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4733 nfs4_write_done_cb(task
, hdr
);
4737 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4739 /* Don't request attributes for pNFS or O_DIRECT writes */
4740 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4742 /* Otherwise, request attributes if and only if we don't hold
4745 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4748 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4749 struct rpc_message
*msg
)
4751 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4753 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4754 hdr
->args
.bitmask
= NULL
;
4755 hdr
->res
.fattr
= NULL
;
4757 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4759 if (!hdr
->pgio_done_cb
)
4760 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4761 hdr
->res
.server
= server
;
4762 hdr
->timestamp
= jiffies
;
4764 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4765 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4768 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4770 nfs4_setup_sequence(NFS_SERVER(data
->inode
)->nfs_client
,
4771 &data
->args
.seq_args
,
4776 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4778 struct inode
*inode
= data
->inode
;
4780 trace_nfs4_commit(data
, task
->tk_status
);
4781 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4782 NULL
, NULL
) == -EAGAIN
) {
4783 rpc_restart_call_prepare(task
);
4789 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4791 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4793 return data
->commit_done_cb(task
, data
);
4796 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4798 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4800 if (data
->commit_done_cb
== NULL
)
4801 data
->commit_done_cb
= nfs4_commit_done_cb
;
4802 data
->res
.server
= server
;
4803 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4804 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4807 struct nfs4_renewdata
{
4808 struct nfs_client
*client
;
4809 unsigned long timestamp
;
4813 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4814 * standalone procedure for queueing an asynchronous RENEW.
4816 static void nfs4_renew_release(void *calldata
)
4818 struct nfs4_renewdata
*data
= calldata
;
4819 struct nfs_client
*clp
= data
->client
;
4821 if (atomic_read(&clp
->cl_count
) > 1)
4822 nfs4_schedule_state_renewal(clp
);
4823 nfs_put_client(clp
);
4827 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4829 struct nfs4_renewdata
*data
= calldata
;
4830 struct nfs_client
*clp
= data
->client
;
4831 unsigned long timestamp
= data
->timestamp
;
4833 trace_nfs4_renew_async(clp
, task
->tk_status
);
4834 switch (task
->tk_status
) {
4837 case -NFS4ERR_LEASE_MOVED
:
4838 nfs4_schedule_lease_moved_recovery(clp
);
4841 /* Unless we're shutting down, schedule state recovery! */
4842 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4844 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4845 nfs4_schedule_lease_recovery(clp
);
4848 nfs4_schedule_path_down_recovery(clp
);
4850 do_renew_lease(clp
, timestamp
);
4853 static const struct rpc_call_ops nfs4_renew_ops
= {
4854 .rpc_call_done
= nfs4_renew_done
,
4855 .rpc_release
= nfs4_renew_release
,
4858 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4860 struct rpc_message msg
= {
4861 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4865 struct nfs4_renewdata
*data
;
4867 if (renew_flags
== 0)
4869 if (!atomic_inc_not_zero(&clp
->cl_count
))
4871 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4873 nfs_put_client(clp
);
4877 data
->timestamp
= jiffies
;
4878 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4879 &nfs4_renew_ops
, data
);
4882 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4884 struct rpc_message msg
= {
4885 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4889 unsigned long now
= jiffies
;
4892 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4895 do_renew_lease(clp
, now
);
4899 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4901 return server
->caps
& NFS_CAP_ACLS
;
4904 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4905 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4908 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4910 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4911 struct page
**pages
)
4913 struct page
*newpage
, **spages
;
4919 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4920 newpage
= alloc_page(GFP_KERNEL
);
4922 if (newpage
== NULL
)
4924 memcpy(page_address(newpage
), buf
, len
);
4929 } while (buflen
!= 0);
4935 __free_page(spages
[rc
-1]);
4939 struct nfs4_cached_acl
{
4945 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4947 struct nfs_inode
*nfsi
= NFS_I(inode
);
4949 spin_lock(&inode
->i_lock
);
4950 kfree(nfsi
->nfs4_acl
);
4951 nfsi
->nfs4_acl
= acl
;
4952 spin_unlock(&inode
->i_lock
);
4955 static void nfs4_zap_acl_attr(struct inode
*inode
)
4957 nfs4_set_cached_acl(inode
, NULL
);
4960 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4962 struct nfs_inode
*nfsi
= NFS_I(inode
);
4963 struct nfs4_cached_acl
*acl
;
4966 spin_lock(&inode
->i_lock
);
4967 acl
= nfsi
->nfs4_acl
;
4970 if (buf
== NULL
) /* user is just asking for length */
4972 if (acl
->cached
== 0)
4974 ret
= -ERANGE
; /* see getxattr(2) man page */
4975 if (acl
->len
> buflen
)
4977 memcpy(buf
, acl
->data
, acl
->len
);
4981 spin_unlock(&inode
->i_lock
);
4985 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4987 struct nfs4_cached_acl
*acl
;
4988 size_t buflen
= sizeof(*acl
) + acl_len
;
4990 if (buflen
<= PAGE_SIZE
) {
4991 acl
= kmalloc(buflen
, GFP_KERNEL
);
4995 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4997 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
5004 nfs4_set_cached_acl(inode
, acl
);
5008 * The getxattr API returns the required buffer length when called with a
5009 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5010 * the required buf. On a NULL buf, we send a page of data to the server
5011 * guessing that the ACL request can be serviced by a page. If so, we cache
5012 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5013 * the cache. If not so, we throw away the page, and cache the required
5014 * length. The next getxattr call will then produce another round trip to
5015 * the server, this time with the input buf of the required size.
5017 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5019 struct page
*pages
[NFS4ACL_MAXPAGES
+ 1] = {NULL
, };
5020 struct nfs_getaclargs args
= {
5021 .fh
= NFS_FH(inode
),
5025 struct nfs_getaclres res
= {
5028 struct rpc_message msg
= {
5029 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
5033 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
) + 1;
5034 int ret
= -ENOMEM
, i
;
5036 if (npages
> ARRAY_SIZE(pages
))
5039 for (i
= 0; i
< npages
; i
++) {
5040 pages
[i
] = alloc_page(GFP_KERNEL
);
5045 /* for decoding across pages */
5046 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
5047 if (!res
.acl_scratch
)
5050 args
.acl_len
= npages
* PAGE_SIZE
;
5052 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5053 __func__
, buf
, buflen
, npages
, args
.acl_len
);
5054 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
5055 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5059 /* Handle the case where the passed-in buffer is too short */
5060 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
5061 /* Did the user only issue a request for the acl length? */
5067 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
5069 if (res
.acl_len
> buflen
) {
5073 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
5078 for (i
= 0; i
< npages
; i
++)
5080 __free_page(pages
[i
]);
5081 if (res
.acl_scratch
)
5082 __free_page(res
.acl_scratch
);
5086 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5088 struct nfs4_exception exception
= { };
5091 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
5092 trace_nfs4_get_acl(inode
, ret
);
5095 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
5096 } while (exception
.retry
);
5100 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
5102 struct nfs_server
*server
= NFS_SERVER(inode
);
5105 if (!nfs4_server_supports_acls(server
))
5107 ret
= nfs_revalidate_inode(server
, inode
);
5110 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
5111 nfs_zap_acl_cache(inode
);
5112 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
5114 /* -ENOENT is returned if there is no ACL or if there is an ACL
5115 * but no cached acl data, just the acl length */
5117 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
5120 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5122 struct nfs_server
*server
= NFS_SERVER(inode
);
5123 struct page
*pages
[NFS4ACL_MAXPAGES
];
5124 struct nfs_setaclargs arg
= {
5125 .fh
= NFS_FH(inode
),
5129 struct nfs_setaclres res
;
5130 struct rpc_message msg
= {
5131 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
5135 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5138 if (!nfs4_server_supports_acls(server
))
5140 if (npages
> ARRAY_SIZE(pages
))
5142 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
5145 nfs4_inode_return_delegation(inode
);
5146 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5149 * Free each page after tx, so the only ref left is
5150 * held by the network stack
5153 put_page(pages
[i
-1]);
5156 * Acl update can result in inode attribute update.
5157 * so mark the attribute cache invalid.
5159 spin_lock(&inode
->i_lock
);
5160 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
5161 spin_unlock(&inode
->i_lock
);
5162 nfs_access_zap_cache(inode
);
5163 nfs_zap_acl_cache(inode
);
5167 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5169 struct nfs4_exception exception
= { };
5172 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
5173 trace_nfs4_set_acl(inode
, err
);
5174 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5176 } while (exception
.retry
);
5180 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5181 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
5184 struct nfs_server
*server
= NFS_SERVER(inode
);
5185 struct nfs_fattr fattr
;
5186 struct nfs4_label label
= {0, 0, buflen
, buf
};
5188 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5189 struct nfs4_getattr_arg arg
= {
5190 .fh
= NFS_FH(inode
),
5193 struct nfs4_getattr_res res
= {
5198 struct rpc_message msg
= {
5199 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
5205 nfs_fattr_init(&fattr
);
5207 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
5210 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
5212 if (buflen
< label
.len
)
5217 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
5220 struct nfs4_exception exception
= { };
5223 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5227 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
5228 trace_nfs4_get_security_label(inode
, err
);
5229 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5231 } while (exception
.retry
);
5235 static int _nfs4_do_set_security_label(struct inode
*inode
,
5236 struct nfs4_label
*ilabel
,
5237 struct nfs_fattr
*fattr
,
5238 struct nfs4_label
*olabel
)
5241 struct iattr sattr
= {0};
5242 struct nfs_server
*server
= NFS_SERVER(inode
);
5243 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5244 struct nfs_setattrargs arg
= {
5245 .fh
= NFS_FH(inode
),
5251 struct nfs_setattrres res
= {
5256 struct rpc_message msg
= {
5257 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
5263 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
5265 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5267 dprintk("%s failed: %d\n", __func__
, status
);
5272 static int nfs4_do_set_security_label(struct inode
*inode
,
5273 struct nfs4_label
*ilabel
,
5274 struct nfs_fattr
*fattr
,
5275 struct nfs4_label
*olabel
)
5277 struct nfs4_exception exception
= { };
5281 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5283 trace_nfs4_set_security_label(inode
, err
);
5284 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5286 } while (exception
.retry
);
5291 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5293 struct nfs4_label ilabel
, *olabel
= NULL
;
5294 struct nfs_fattr fattr
;
5295 struct rpc_cred
*cred
;
5298 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5301 nfs_fattr_init(&fattr
);
5305 ilabel
.label
= (char *)buf
;
5306 ilabel
.len
= buflen
;
5308 cred
= rpc_lookup_cred();
5310 return PTR_ERR(cred
);
5312 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5313 if (IS_ERR(olabel
)) {
5314 status
= -PTR_ERR(olabel
);
5318 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5320 nfs_setsecurity(inode
, &fattr
, olabel
);
5322 nfs4_label_free(olabel
);
5327 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5330 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5331 nfs4_verifier
*bootverf
)
5335 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5336 /* An impossible timestamp guarantees this value
5337 * will never match a generated boot time. */
5338 verf
[0] = cpu_to_be32(U32_MAX
);
5339 verf
[1] = cpu_to_be32(U32_MAX
);
5341 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5342 u64 ns
= ktime_to_ns(nn
->boot_time
);
5344 verf
[0] = cpu_to_be32(ns
>> 32);
5345 verf
[1] = cpu_to_be32(ns
);
5347 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5351 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5356 if (clp
->cl_owner_id
!= NULL
)
5360 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5361 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5363 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
5367 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5371 * Since this string is allocated at mount time, and held until the
5372 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5373 * about a memory-reclaim deadlock.
5375 str
= kmalloc(len
, GFP_KERNEL
);
5380 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5382 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5383 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5386 clp
->cl_owner_id
= str
;
5391 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5396 len
= 10 + 10 + 1 + 10 + 1 +
5397 strlen(nfs4_client_id_uniquifier
) + 1 +
5398 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5400 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5404 * Since this string is allocated at mount time, and held until the
5405 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5406 * about a memory-reclaim deadlock.
5408 str
= kmalloc(len
, GFP_KERNEL
);
5412 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5413 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5414 nfs4_client_id_uniquifier
,
5415 clp
->cl_rpcclient
->cl_nodename
);
5416 clp
->cl_owner_id
= str
;
5421 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5426 if (clp
->cl_owner_id
!= NULL
)
5429 if (nfs4_client_id_uniquifier
[0] != '\0')
5430 return nfs4_init_uniquifier_client_string(clp
);
5432 len
= 10 + 10 + 1 + 10 + 1 +
5433 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5435 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5439 * Since this string is allocated at mount time, and held until the
5440 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5441 * about a memory-reclaim deadlock.
5443 str
= kmalloc(len
, GFP_KERNEL
);
5447 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5448 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5449 clp
->cl_rpcclient
->cl_nodename
);
5450 clp
->cl_owner_id
= str
;
5455 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5456 * services. Advertise one based on the address family of the
5460 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5462 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5463 return scnprintf(buf
, len
, "tcp6");
5465 return scnprintf(buf
, len
, "tcp");
5468 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5470 struct nfs4_setclientid
*sc
= calldata
;
5472 if (task
->tk_status
== 0)
5473 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5476 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5477 .rpc_call_done
= nfs4_setclientid_done
,
5481 * nfs4_proc_setclientid - Negotiate client ID
5482 * @clp: state data structure
5483 * @program: RPC program for NFSv4 callback service
5484 * @port: IP port number for NFS4 callback service
5485 * @cred: RPC credential to use for this call
5486 * @res: where to place the result
5488 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5490 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5491 unsigned short port
, struct rpc_cred
*cred
,
5492 struct nfs4_setclientid_res
*res
)
5494 nfs4_verifier sc_verifier
;
5495 struct nfs4_setclientid setclientid
= {
5496 .sc_verifier
= &sc_verifier
,
5500 struct rpc_message msg
= {
5501 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5502 .rpc_argp
= &setclientid
,
5506 struct rpc_task
*task
;
5507 struct rpc_task_setup task_setup_data
= {
5508 .rpc_client
= clp
->cl_rpcclient
,
5509 .rpc_message
= &msg
,
5510 .callback_ops
= &nfs4_setclientid_ops
,
5511 .callback_data
= &setclientid
,
5512 .flags
= RPC_TASK_TIMEOUT
,
5516 /* nfs_client_id4 */
5517 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5519 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5520 status
= nfs4_init_uniform_client_string(clp
);
5522 status
= nfs4_init_nonuniform_client_string(clp
);
5528 setclientid
.sc_netid_len
=
5529 nfs4_init_callback_netid(clp
,
5530 setclientid
.sc_netid
,
5531 sizeof(setclientid
.sc_netid
));
5532 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5533 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5534 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5536 dprintk("NFS call setclientid auth=%s, '%s'\n",
5537 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5539 task
= rpc_run_task(&task_setup_data
);
5541 status
= PTR_ERR(task
);
5544 status
= task
->tk_status
;
5545 if (setclientid
.sc_cred
) {
5546 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5547 put_rpccred(setclientid
.sc_cred
);
5551 trace_nfs4_setclientid(clp
, status
);
5552 dprintk("NFS reply setclientid: %d\n", status
);
5557 * nfs4_proc_setclientid_confirm - Confirm client ID
5558 * @clp: state data structure
5559 * @res: result of a previous SETCLIENTID
5560 * @cred: RPC credential to use for this call
5562 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5564 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5565 struct nfs4_setclientid_res
*arg
,
5566 struct rpc_cred
*cred
)
5568 struct rpc_message msg
= {
5569 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5575 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5576 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5578 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5579 trace_nfs4_setclientid_confirm(clp
, status
);
5580 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5584 struct nfs4_delegreturndata
{
5585 struct nfs4_delegreturnargs args
;
5586 struct nfs4_delegreturnres res
;
5588 nfs4_stateid stateid
;
5589 unsigned long timestamp
;
5591 struct nfs4_layoutreturn_args arg
;
5592 struct nfs4_layoutreturn_res res
;
5593 struct nfs4_xdr_opaque_data ld_private
;
5597 struct nfs_fattr fattr
;
5599 struct inode
*inode
;
5602 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5604 struct nfs4_delegreturndata
*data
= calldata
;
5606 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5609 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5611 /* Handle Layoutreturn errors */
5612 if (data
->args
.lr_args
&& task
->tk_status
!= 0) {
5613 switch(data
->res
.lr_ret
) {
5615 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5618 data
->args
.lr_args
= NULL
;
5619 data
->res
.lr_res
= NULL
;
5621 case -NFS4ERR_ADMIN_REVOKED
:
5622 case -NFS4ERR_DELEG_REVOKED
:
5623 case -NFS4ERR_EXPIRED
:
5624 case -NFS4ERR_BAD_STATEID
:
5625 case -NFS4ERR_OLD_STATEID
:
5626 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
5627 case -NFS4ERR_WRONG_CRED
:
5628 data
->args
.lr_args
= NULL
;
5629 data
->res
.lr_res
= NULL
;
5630 data
->res
.lr_ret
= 0;
5631 rpc_restart_call_prepare(task
);
5636 switch (task
->tk_status
) {
5638 renew_lease(data
->res
.server
, data
->timestamp
);
5640 case -NFS4ERR_ADMIN_REVOKED
:
5641 case -NFS4ERR_DELEG_REVOKED
:
5642 case -NFS4ERR_EXPIRED
:
5643 nfs4_free_revoked_stateid(data
->res
.server
,
5645 task
->tk_msg
.rpc_cred
);
5646 case -NFS4ERR_BAD_STATEID
:
5647 case -NFS4ERR_OLD_STATEID
:
5648 case -NFS4ERR_STALE_STATEID
:
5649 task
->tk_status
= 0;
5651 case -NFS4ERR_ACCESS
:
5652 if (data
->args
.bitmask
) {
5653 data
->args
.bitmask
= NULL
;
5654 data
->res
.fattr
= NULL
;
5655 task
->tk_status
= 0;
5656 rpc_restart_call_prepare(task
);
5660 if (nfs4_async_handle_error(task
, data
->res
.server
,
5661 NULL
, NULL
) == -EAGAIN
) {
5662 rpc_restart_call_prepare(task
);
5666 data
->rpc_status
= task
->tk_status
;
5669 static void nfs4_delegreturn_release(void *calldata
)
5671 struct nfs4_delegreturndata
*data
= calldata
;
5672 struct inode
*inode
= data
->inode
;
5676 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
,
5678 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5679 nfs_iput_and_deactive(inode
);
5684 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5686 struct nfs4_delegreturndata
*d_data
;
5688 d_data
= (struct nfs4_delegreturndata
*)data
;
5690 if (!d_data
->lr
.roc
&& nfs4_wait_on_layoutreturn(d_data
->inode
, task
))
5693 nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
5694 &d_data
->args
.seq_args
,
5695 &d_data
->res
.seq_res
,
5699 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5700 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5701 .rpc_call_done
= nfs4_delegreturn_done
,
5702 .rpc_release
= nfs4_delegreturn_release
,
5705 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5707 struct nfs4_delegreturndata
*data
;
5708 struct nfs_server
*server
= NFS_SERVER(inode
);
5709 struct rpc_task
*task
;
5710 struct rpc_message msg
= {
5711 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5714 struct rpc_task_setup task_setup_data
= {
5715 .rpc_client
= server
->client
,
5716 .rpc_message
= &msg
,
5717 .callback_ops
= &nfs4_delegreturn_ops
,
5718 .flags
= RPC_TASK_ASYNC
,
5722 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5725 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5727 nfs4_state_protect(server
->nfs_client
,
5728 NFS_SP4_MACH_CRED_CLEANUP
,
5729 &task_setup_data
.rpc_client
, &msg
);
5731 data
->args
.fhandle
= &data
->fh
;
5732 data
->args
.stateid
= &data
->stateid
;
5733 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5734 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5735 nfs4_stateid_copy(&data
->stateid
, stateid
);
5736 data
->res
.fattr
= &data
->fattr
;
5737 data
->res
.server
= server
;
5738 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5739 data
->lr
.arg
.ld_private
= &data
->lr
.ld_private
;
5740 nfs_fattr_init(data
->res
.fattr
);
5741 data
->timestamp
= jiffies
;
5742 data
->rpc_status
= 0;
5743 data
->lr
.roc
= pnfs_roc(inode
, &data
->lr
.arg
, &data
->lr
.res
, cred
);
5744 data
->inode
= nfs_igrab_and_active(inode
);
5747 data
->args
.lr_args
= &data
->lr
.arg
;
5748 data
->res
.lr_res
= &data
->lr
.res
;
5750 } else if (data
->lr
.roc
) {
5751 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
, 0);
5752 data
->lr
.roc
= false;
5755 task_setup_data
.callback_data
= data
;
5756 msg
.rpc_argp
= &data
->args
;
5757 msg
.rpc_resp
= &data
->res
;
5758 task
= rpc_run_task(&task_setup_data
);
5760 return PTR_ERR(task
);
5763 status
= rpc_wait_for_completion_task(task
);
5766 status
= data
->rpc_status
;
5772 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5774 struct nfs_server
*server
= NFS_SERVER(inode
);
5775 struct nfs4_exception exception
= { };
5778 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5779 trace_nfs4_delegreturn(inode
, stateid
, err
);
5781 case -NFS4ERR_STALE_STATEID
:
5782 case -NFS4ERR_EXPIRED
:
5786 err
= nfs4_handle_exception(server
, err
, &exception
);
5787 } while (exception
.retry
);
5791 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5793 struct inode
*inode
= state
->inode
;
5794 struct nfs_server
*server
= NFS_SERVER(inode
);
5795 struct nfs_client
*clp
= server
->nfs_client
;
5796 struct nfs_lockt_args arg
= {
5797 .fh
= NFS_FH(inode
),
5800 struct nfs_lockt_res res
= {
5803 struct rpc_message msg
= {
5804 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5807 .rpc_cred
= state
->owner
->so_cred
,
5809 struct nfs4_lock_state
*lsp
;
5812 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5813 status
= nfs4_set_lock_state(state
, request
);
5816 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5817 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5818 arg
.lock_owner
.s_dev
= server
->s_dev
;
5819 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5822 request
->fl_type
= F_UNLCK
;
5824 case -NFS4ERR_DENIED
:
5827 request
->fl_ops
->fl_release_private(request
);
5828 request
->fl_ops
= NULL
;
5833 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5835 struct nfs4_exception exception
= { };
5839 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5840 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5841 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5843 } while (exception
.retry
);
5847 struct nfs4_unlockdata
{
5848 struct nfs_locku_args arg
;
5849 struct nfs_locku_res res
;
5850 struct nfs4_lock_state
*lsp
;
5851 struct nfs_open_context
*ctx
;
5852 struct nfs_lock_context
*l_ctx
;
5853 struct file_lock fl
;
5854 struct nfs_server
*server
;
5855 unsigned long timestamp
;
5858 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5859 struct nfs_open_context
*ctx
,
5860 struct nfs4_lock_state
*lsp
,
5861 struct nfs_seqid
*seqid
)
5863 struct nfs4_unlockdata
*p
;
5864 struct inode
*inode
= lsp
->ls_state
->inode
;
5866 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5869 p
->arg
.fh
= NFS_FH(inode
);
5871 p
->arg
.seqid
= seqid
;
5872 p
->res
.seqid
= seqid
;
5874 atomic_inc(&lsp
->ls_count
);
5875 /* Ensure we don't close file until we're done freeing locks! */
5876 p
->ctx
= get_nfs_open_context(ctx
);
5877 p
->l_ctx
= nfs_get_lock_context(ctx
);
5878 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5879 p
->server
= NFS_SERVER(inode
);
5883 static void nfs4_locku_release_calldata(void *data
)
5885 struct nfs4_unlockdata
*calldata
= data
;
5886 nfs_free_seqid(calldata
->arg
.seqid
);
5887 nfs4_put_lock_state(calldata
->lsp
);
5888 nfs_put_lock_context(calldata
->l_ctx
);
5889 put_nfs_open_context(calldata
->ctx
);
5893 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5895 struct nfs4_unlockdata
*calldata
= data
;
5897 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5899 switch (task
->tk_status
) {
5901 renew_lease(calldata
->server
, calldata
->timestamp
);
5902 locks_lock_inode_wait(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5903 if (nfs4_update_lock_stateid(calldata
->lsp
,
5904 &calldata
->res
.stateid
))
5906 case -NFS4ERR_ADMIN_REVOKED
:
5907 case -NFS4ERR_EXPIRED
:
5908 nfs4_free_revoked_stateid(calldata
->server
,
5909 &calldata
->arg
.stateid
,
5910 task
->tk_msg
.rpc_cred
);
5911 case -NFS4ERR_BAD_STATEID
:
5912 case -NFS4ERR_OLD_STATEID
:
5913 case -NFS4ERR_STALE_STATEID
:
5914 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5915 &calldata
->lsp
->ls_stateid
))
5916 rpc_restart_call_prepare(task
);
5919 if (nfs4_async_handle_error(task
, calldata
->server
,
5920 NULL
, NULL
) == -EAGAIN
)
5921 rpc_restart_call_prepare(task
);
5923 nfs_release_seqid(calldata
->arg
.seqid
);
5926 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5928 struct nfs4_unlockdata
*calldata
= data
;
5930 if (test_bit(NFS_CONTEXT_UNLOCK
, &calldata
->l_ctx
->open_context
->flags
) &&
5931 nfs_async_iocounter_wait(task
, calldata
->l_ctx
))
5934 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5936 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5937 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5938 /* Note: exit _without_ running nfs4_locku_done */
5941 calldata
->timestamp
= jiffies
;
5942 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
5943 &calldata
->arg
.seq_args
,
5944 &calldata
->res
.seq_res
,
5946 nfs_release_seqid(calldata
->arg
.seqid
);
5949 task
->tk_action
= NULL
;
5951 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5954 static const struct rpc_call_ops nfs4_locku_ops
= {
5955 .rpc_call_prepare
= nfs4_locku_prepare
,
5956 .rpc_call_done
= nfs4_locku_done
,
5957 .rpc_release
= nfs4_locku_release_calldata
,
5960 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5961 struct nfs_open_context
*ctx
,
5962 struct nfs4_lock_state
*lsp
,
5963 struct nfs_seqid
*seqid
)
5965 struct nfs4_unlockdata
*data
;
5966 struct rpc_message msg
= {
5967 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5968 .rpc_cred
= ctx
->cred
,
5970 struct rpc_task_setup task_setup_data
= {
5971 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5972 .rpc_message
= &msg
,
5973 .callback_ops
= &nfs4_locku_ops
,
5974 .workqueue
= nfsiod_workqueue
,
5975 .flags
= RPC_TASK_ASYNC
,
5978 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5979 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5981 /* Ensure this is an unlock - when canceling a lock, the
5982 * canceled lock is passed in, and it won't be an unlock.
5984 fl
->fl_type
= F_UNLCK
;
5985 if (fl
->fl_flags
& FL_CLOSE
)
5986 set_bit(NFS_CONTEXT_UNLOCK
, &ctx
->flags
);
5988 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5990 nfs_free_seqid(seqid
);
5991 return ERR_PTR(-ENOMEM
);
5994 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5995 msg
.rpc_argp
= &data
->arg
;
5996 msg
.rpc_resp
= &data
->res
;
5997 task_setup_data
.callback_data
= data
;
5998 return rpc_run_task(&task_setup_data
);
6001 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6003 struct inode
*inode
= state
->inode
;
6004 struct nfs4_state_owner
*sp
= state
->owner
;
6005 struct nfs_inode
*nfsi
= NFS_I(inode
);
6006 struct nfs_seqid
*seqid
;
6007 struct nfs4_lock_state
*lsp
;
6008 struct rpc_task
*task
;
6009 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6011 unsigned char fl_flags
= request
->fl_flags
;
6013 status
= nfs4_set_lock_state(state
, request
);
6014 /* Unlock _before_ we do the RPC call */
6015 request
->fl_flags
|= FL_EXISTS
;
6016 /* Exclude nfs_delegation_claim_locks() */
6017 mutex_lock(&sp
->so_delegreturn_mutex
);
6018 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6019 down_read(&nfsi
->rwsem
);
6020 if (locks_lock_inode_wait(inode
, request
) == -ENOENT
) {
6021 up_read(&nfsi
->rwsem
);
6022 mutex_unlock(&sp
->so_delegreturn_mutex
);
6025 up_read(&nfsi
->rwsem
);
6026 mutex_unlock(&sp
->so_delegreturn_mutex
);
6029 /* Is this a delegated lock? */
6030 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6031 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
6033 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
6034 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
6038 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
6039 status
= PTR_ERR(task
);
6042 status
= rpc_wait_for_completion_task(task
);
6045 request
->fl_flags
= fl_flags
;
6046 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
6050 struct nfs4_lockdata
{
6051 struct nfs_lock_args arg
;
6052 struct nfs_lock_res res
;
6053 struct nfs4_lock_state
*lsp
;
6054 struct nfs_open_context
*ctx
;
6055 struct file_lock fl
;
6056 unsigned long timestamp
;
6059 struct nfs_server
*server
;
6062 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
6063 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
6066 struct nfs4_lockdata
*p
;
6067 struct inode
*inode
= lsp
->ls_state
->inode
;
6068 struct nfs_server
*server
= NFS_SERVER(inode
);
6069 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6071 p
= kzalloc(sizeof(*p
), gfp_mask
);
6075 p
->arg
.fh
= NFS_FH(inode
);
6077 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
6078 if (IS_ERR(p
->arg
.open_seqid
))
6080 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
6081 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
6082 if (IS_ERR(p
->arg
.lock_seqid
))
6083 goto out_free_seqid
;
6084 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6085 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6086 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
6087 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
6090 atomic_inc(&lsp
->ls_count
);
6091 p
->ctx
= get_nfs_open_context(ctx
);
6092 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
6095 nfs_free_seqid(p
->arg
.open_seqid
);
6101 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
6103 struct nfs4_lockdata
*data
= calldata
;
6104 struct nfs4_state
*state
= data
->lsp
->ls_state
;
6106 dprintk("%s: begin!\n", __func__
);
6107 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
6109 /* Do we need to do an open_to_lock_owner? */
6110 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
6111 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
6112 goto out_release_lock_seqid
;
6114 nfs4_stateid_copy(&data
->arg
.open_stateid
,
6115 &state
->open_stateid
);
6116 data
->arg
.new_lock_owner
= 1;
6117 data
->res
.open_seqid
= data
->arg
.open_seqid
;
6119 data
->arg
.new_lock_owner
= 0;
6120 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
6121 &data
->lsp
->ls_stateid
);
6123 if (!nfs4_valid_open_stateid(state
)) {
6124 data
->rpc_status
= -EBADF
;
6125 task
->tk_action
= NULL
;
6126 goto out_release_open_seqid
;
6128 data
->timestamp
= jiffies
;
6129 if (nfs4_setup_sequence(data
->server
->nfs_client
,
6130 &data
->arg
.seq_args
,
6134 out_release_open_seqid
:
6135 nfs_release_seqid(data
->arg
.open_seqid
);
6136 out_release_lock_seqid
:
6137 nfs_release_seqid(data
->arg
.lock_seqid
);
6139 nfs4_sequence_done(task
, &data
->res
.seq_res
);
6140 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
6143 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
6145 struct nfs4_lockdata
*data
= calldata
;
6146 struct nfs4_lock_state
*lsp
= data
->lsp
;
6148 dprintk("%s: begin!\n", __func__
);
6150 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
6153 data
->rpc_status
= task
->tk_status
;
6154 switch (task
->tk_status
) {
6156 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
6158 if (data
->arg
.new_lock
) {
6159 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
6160 if (locks_lock_inode_wait(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
6161 rpc_restart_call_prepare(task
);
6165 if (data
->arg
.new_lock_owner
!= 0) {
6166 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
6167 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
6168 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6169 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
6170 rpc_restart_call_prepare(task
);
6172 case -NFS4ERR_BAD_STATEID
:
6173 case -NFS4ERR_OLD_STATEID
:
6174 case -NFS4ERR_STALE_STATEID
:
6175 case -NFS4ERR_EXPIRED
:
6176 if (data
->arg
.new_lock_owner
!= 0) {
6177 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
6178 &lsp
->ls_state
->open_stateid
))
6179 rpc_restart_call_prepare(task
);
6180 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
6182 rpc_restart_call_prepare(task
);
6184 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
6187 static void nfs4_lock_release(void *calldata
)
6189 struct nfs4_lockdata
*data
= calldata
;
6191 dprintk("%s: begin!\n", __func__
);
6192 nfs_free_seqid(data
->arg
.open_seqid
);
6193 if (data
->cancelled
) {
6194 struct rpc_task
*task
;
6195 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
6196 data
->arg
.lock_seqid
);
6198 rpc_put_task_async(task
);
6199 dprintk("%s: cancelling lock!\n", __func__
);
6201 nfs_free_seqid(data
->arg
.lock_seqid
);
6202 nfs4_put_lock_state(data
->lsp
);
6203 put_nfs_open_context(data
->ctx
);
6205 dprintk("%s: done!\n", __func__
);
6208 static const struct rpc_call_ops nfs4_lock_ops
= {
6209 .rpc_call_prepare
= nfs4_lock_prepare
,
6210 .rpc_call_done
= nfs4_lock_done
,
6211 .rpc_release
= nfs4_lock_release
,
6214 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
6217 case -NFS4ERR_ADMIN_REVOKED
:
6218 case -NFS4ERR_EXPIRED
:
6219 case -NFS4ERR_BAD_STATEID
:
6220 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6221 if (new_lock_owner
!= 0 ||
6222 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
6223 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
6225 case -NFS4ERR_STALE_STATEID
:
6226 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6227 nfs4_schedule_lease_recovery(server
->nfs_client
);
6231 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
6233 struct nfs4_lockdata
*data
;
6234 struct rpc_task
*task
;
6235 struct rpc_message msg
= {
6236 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
6237 .rpc_cred
= state
->owner
->so_cred
,
6239 struct rpc_task_setup task_setup_data
= {
6240 .rpc_client
= NFS_CLIENT(state
->inode
),
6241 .rpc_message
= &msg
,
6242 .callback_ops
= &nfs4_lock_ops
,
6243 .workqueue
= nfsiod_workqueue
,
6244 .flags
= RPC_TASK_ASYNC
,
6248 dprintk("%s: begin!\n", __func__
);
6249 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
6250 fl
->fl_u
.nfs4_fl
.owner
,
6251 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
6255 data
->arg
.block
= 1;
6256 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6257 msg
.rpc_argp
= &data
->arg
;
6258 msg
.rpc_resp
= &data
->res
;
6259 task_setup_data
.callback_data
= data
;
6260 if (recovery_type
> NFS_LOCK_NEW
) {
6261 if (recovery_type
== NFS_LOCK_RECLAIM
)
6262 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
6263 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
6265 data
->arg
.new_lock
= 1;
6266 task
= rpc_run_task(&task_setup_data
);
6268 return PTR_ERR(task
);
6269 ret
= rpc_wait_for_completion_task(task
);
6271 ret
= data
->rpc_status
;
6273 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
6274 data
->arg
.new_lock_owner
, ret
);
6276 data
->cancelled
= true;
6278 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
6279 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
6283 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
6285 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6286 struct nfs4_exception exception
= {
6287 .inode
= state
->inode
,
6292 /* Cache the lock if possible... */
6293 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6295 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
6296 if (err
!= -NFS4ERR_DELAY
)
6298 nfs4_handle_exception(server
, err
, &exception
);
6299 } while (exception
.retry
);
6303 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6305 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6306 struct nfs4_exception exception
= {
6307 .inode
= state
->inode
,
6311 err
= nfs4_set_lock_state(state
, request
);
6314 if (!recover_lost_locks
) {
6315 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6319 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6321 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6325 case -NFS4ERR_GRACE
:
6326 case -NFS4ERR_DELAY
:
6327 nfs4_handle_exception(server
, err
, &exception
);
6330 } while (exception
.retry
);
6335 #if defined(CONFIG_NFS_V4_1)
6336 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6338 struct nfs4_lock_state
*lsp
;
6341 status
= nfs4_set_lock_state(state
, request
);
6344 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6345 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) ||
6346 test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
6348 return nfs4_lock_expired(state
, request
);
6352 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6354 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6355 struct nfs4_state_owner
*sp
= state
->owner
;
6356 unsigned char fl_flags
= request
->fl_flags
;
6359 request
->fl_flags
|= FL_ACCESS
;
6360 status
= locks_lock_inode_wait(state
->inode
, request
);
6363 mutex_lock(&sp
->so_delegreturn_mutex
);
6364 down_read(&nfsi
->rwsem
);
6365 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6366 /* Yes: cache locks! */
6367 /* ...but avoid races with delegation recall... */
6368 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6369 status
= locks_lock_inode_wait(state
->inode
, request
);
6370 up_read(&nfsi
->rwsem
);
6371 mutex_unlock(&sp
->so_delegreturn_mutex
);
6374 up_read(&nfsi
->rwsem
);
6375 mutex_unlock(&sp
->so_delegreturn_mutex
);
6376 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6378 request
->fl_flags
= fl_flags
;
6382 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6384 struct nfs4_exception exception
= {
6386 .inode
= state
->inode
,
6391 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6392 if (err
== -NFS4ERR_DENIED
)
6394 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6396 } while (exception
.retry
);
6400 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6401 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6404 nfs4_retry_setlk_simple(struct nfs4_state
*state
, int cmd
,
6405 struct file_lock
*request
)
6407 int status
= -ERESTARTSYS
;
6408 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6410 while(!signalled()) {
6411 status
= nfs4_proc_setlk(state
, cmd
, request
);
6412 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6414 freezable_schedule_timeout_interruptible(timeout
);
6416 timeout
= min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT
, timeout
);
6417 status
= -ERESTARTSYS
;
6422 #ifdef CONFIG_NFS_V4_1
6423 struct nfs4_lock_waiter
{
6424 struct task_struct
*task
;
6425 struct inode
*inode
;
6426 struct nfs_lowner
*owner
;
6431 nfs4_wake_lock_waiter(wait_queue_entry_t
*wait
, unsigned int mode
, int flags
, void *key
)
6434 struct cb_notify_lock_args
*cbnl
= key
;
6435 struct nfs4_lock_waiter
*waiter
= wait
->private;
6436 struct nfs_lowner
*lowner
= &cbnl
->cbnl_owner
,
6437 *wowner
= waiter
->owner
;
6439 /* Only wake if the callback was for the same owner */
6440 if (lowner
->clientid
!= wowner
->clientid
||
6441 lowner
->id
!= wowner
->id
||
6442 lowner
->s_dev
!= wowner
->s_dev
)
6445 /* Make sure it's for the right inode */
6446 if (nfs_compare_fh(NFS_FH(waiter
->inode
), &cbnl
->cbnl_fh
))
6449 waiter
->notified
= true;
6451 /* override "private" so we can use default_wake_function */
6452 wait
->private = waiter
->task
;
6453 ret
= autoremove_wake_function(wait
, mode
, flags
, key
);
6454 wait
->private = waiter
;
6459 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6461 int status
= -ERESTARTSYS
;
6462 unsigned long flags
;
6463 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
6464 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6465 struct nfs_client
*clp
= server
->nfs_client
;
6466 wait_queue_head_t
*q
= &clp
->cl_lock_waitq
;
6467 struct nfs_lowner owner
= { .clientid
= clp
->cl_clientid
,
6468 .id
= lsp
->ls_seqid
.owner_id
,
6469 .s_dev
= server
->s_dev
};
6470 struct nfs4_lock_waiter waiter
= { .task
= current
,
6471 .inode
= state
->inode
,
6473 .notified
= false };
6474 wait_queue_entry_t wait
;
6476 /* Don't bother with waitqueue if we don't expect a callback */
6477 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
))
6478 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6481 wait
.private = &waiter
;
6482 wait
.func
= nfs4_wake_lock_waiter
;
6483 add_wait_queue(q
, &wait
);
6485 while(!signalled()) {
6486 status
= nfs4_proc_setlk(state
, cmd
, request
);
6487 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6490 status
= -ERESTARTSYS
;
6491 spin_lock_irqsave(&q
->lock
, flags
);
6492 if (waiter
.notified
) {
6493 spin_unlock_irqrestore(&q
->lock
, flags
);
6496 set_current_state(TASK_INTERRUPTIBLE
);
6497 spin_unlock_irqrestore(&q
->lock
, flags
);
6499 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT
);
6502 finish_wait(q
, &wait
);
6505 #else /* !CONFIG_NFS_V4_1 */
6507 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6509 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6514 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6516 struct nfs_open_context
*ctx
;
6517 struct nfs4_state
*state
;
6520 /* verify open state */
6521 ctx
= nfs_file_open_context(filp
);
6524 if (IS_GETLK(cmd
)) {
6526 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6530 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6533 if (request
->fl_type
== F_UNLCK
) {
6535 return nfs4_proc_unlck(state
, cmd
, request
);
6542 if ((request
->fl_flags
& FL_POSIX
) &&
6543 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6546 status
= nfs4_set_lock_state(state
, request
);
6550 return nfs4_retry_setlk(state
, cmd
, request
);
6553 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6555 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6558 err
= nfs4_set_lock_state(state
, fl
);
6561 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6562 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6565 struct nfs_release_lockowner_data
{
6566 struct nfs4_lock_state
*lsp
;
6567 struct nfs_server
*server
;
6568 struct nfs_release_lockowner_args args
;
6569 struct nfs_release_lockowner_res res
;
6570 unsigned long timestamp
;
6573 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6575 struct nfs_release_lockowner_data
*data
= calldata
;
6576 struct nfs_server
*server
= data
->server
;
6577 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
6578 &data
->res
.seq_res
, task
);
6579 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6580 data
->timestamp
= jiffies
;
6583 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6585 struct nfs_release_lockowner_data
*data
= calldata
;
6586 struct nfs_server
*server
= data
->server
;
6588 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6590 switch (task
->tk_status
) {
6592 renew_lease(server
, data
->timestamp
);
6594 case -NFS4ERR_STALE_CLIENTID
:
6595 case -NFS4ERR_EXPIRED
:
6596 nfs4_schedule_lease_recovery(server
->nfs_client
);
6598 case -NFS4ERR_LEASE_MOVED
:
6599 case -NFS4ERR_DELAY
:
6600 if (nfs4_async_handle_error(task
, server
,
6601 NULL
, NULL
) == -EAGAIN
)
6602 rpc_restart_call_prepare(task
);
6606 static void nfs4_release_lockowner_release(void *calldata
)
6608 struct nfs_release_lockowner_data
*data
= calldata
;
6609 nfs4_free_lock_state(data
->server
, data
->lsp
);
6613 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6614 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6615 .rpc_call_done
= nfs4_release_lockowner_done
,
6616 .rpc_release
= nfs4_release_lockowner_release
,
6620 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6622 struct nfs_release_lockowner_data
*data
;
6623 struct rpc_message msg
= {
6624 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6627 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6630 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6634 data
->server
= server
;
6635 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6636 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6637 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6639 msg
.rpc_argp
= &data
->args
;
6640 msg
.rpc_resp
= &data
->res
;
6641 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6642 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6645 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6647 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6648 struct dentry
*unused
, struct inode
*inode
,
6649 const char *key
, const void *buf
,
6650 size_t buflen
, int flags
)
6652 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6655 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6656 struct dentry
*unused
, struct inode
*inode
,
6657 const char *key
, void *buf
, size_t buflen
)
6659 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6662 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6664 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6667 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6669 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6670 struct dentry
*unused
, struct inode
*inode
,
6671 const char *key
, const void *buf
,
6672 size_t buflen
, int flags
)
6674 if (security_ismaclabel(key
))
6675 return nfs4_set_security_label(inode
, buf
, buflen
);
6680 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6681 struct dentry
*unused
, struct inode
*inode
,
6682 const char *key
, void *buf
, size_t buflen
)
6684 if (security_ismaclabel(key
))
6685 return nfs4_get_security_label(inode
, buf
, buflen
);
6690 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6694 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6695 len
= security_inode_listsecurity(inode
, list
, list_len
);
6696 if (list_len
&& len
> list_len
)
6702 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6703 .prefix
= XATTR_SECURITY_PREFIX
,
6704 .get
= nfs4_xattr_get_nfs4_label
,
6705 .set
= nfs4_xattr_set_nfs4_label
,
6711 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6719 * nfs_fhget will use either the mounted_on_fileid or the fileid
6721 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6723 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6724 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6725 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6726 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6729 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6730 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6731 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6735 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6736 const struct qstr
*name
,
6737 struct nfs4_fs_locations
*fs_locations
,
6740 struct nfs_server
*server
= NFS_SERVER(dir
);
6742 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6744 struct nfs4_fs_locations_arg args
= {
6745 .dir_fh
= NFS_FH(dir
),
6750 struct nfs4_fs_locations_res res
= {
6751 .fs_locations
= fs_locations
,
6753 struct rpc_message msg
= {
6754 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6760 dprintk("%s: start\n", __func__
);
6762 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6763 * is not supported */
6764 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6765 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6767 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6769 nfs_fattr_init(&fs_locations
->fattr
);
6770 fs_locations
->server
= server
;
6771 fs_locations
->nlocations
= 0;
6772 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6773 dprintk("%s: returned status = %d\n", __func__
, status
);
6777 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6778 const struct qstr
*name
,
6779 struct nfs4_fs_locations
*fs_locations
,
6782 struct nfs4_exception exception
= { };
6785 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6786 fs_locations
, page
);
6787 trace_nfs4_get_fs_locations(dir
, name
, err
);
6788 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6790 } while (exception
.retry
);
6795 * This operation also signals the server that this client is
6796 * performing migration recovery. The server can stop returning
6797 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6798 * appended to this compound to identify the client ID which is
6799 * performing recovery.
6801 static int _nfs40_proc_get_locations(struct inode
*inode
,
6802 struct nfs4_fs_locations
*locations
,
6803 struct page
*page
, struct rpc_cred
*cred
)
6805 struct nfs_server
*server
= NFS_SERVER(inode
);
6806 struct rpc_clnt
*clnt
= server
->client
;
6808 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6810 struct nfs4_fs_locations_arg args
= {
6811 .clientid
= server
->nfs_client
->cl_clientid
,
6812 .fh
= NFS_FH(inode
),
6815 .migration
= 1, /* skip LOOKUP */
6816 .renew
= 1, /* append RENEW */
6818 struct nfs4_fs_locations_res res
= {
6819 .fs_locations
= locations
,
6823 struct rpc_message msg
= {
6824 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6829 unsigned long now
= jiffies
;
6832 nfs_fattr_init(&locations
->fattr
);
6833 locations
->server
= server
;
6834 locations
->nlocations
= 0;
6836 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6837 nfs4_set_sequence_privileged(&args
.seq_args
);
6838 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6839 &args
.seq_args
, &res
.seq_res
);
6843 renew_lease(server
, now
);
6847 #ifdef CONFIG_NFS_V4_1
6850 * This operation also signals the server that this client is
6851 * performing migration recovery. The server can stop asserting
6852 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6853 * performing this operation is identified in the SEQUENCE
6854 * operation in this compound.
6856 * When the client supports GETATTR(fs_locations_info), it can
6857 * be plumbed in here.
6859 static int _nfs41_proc_get_locations(struct inode
*inode
,
6860 struct nfs4_fs_locations
*locations
,
6861 struct page
*page
, struct rpc_cred
*cred
)
6863 struct nfs_server
*server
= NFS_SERVER(inode
);
6864 struct rpc_clnt
*clnt
= server
->client
;
6866 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6868 struct nfs4_fs_locations_arg args
= {
6869 .fh
= NFS_FH(inode
),
6872 .migration
= 1, /* skip LOOKUP */
6874 struct nfs4_fs_locations_res res
= {
6875 .fs_locations
= locations
,
6878 struct rpc_message msg
= {
6879 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6886 nfs_fattr_init(&locations
->fattr
);
6887 locations
->server
= server
;
6888 locations
->nlocations
= 0;
6890 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6891 nfs4_set_sequence_privileged(&args
.seq_args
);
6892 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6893 &args
.seq_args
, &res
.seq_res
);
6894 if (status
== NFS4_OK
&&
6895 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6896 status
= -NFS4ERR_LEASE_MOVED
;
6900 #endif /* CONFIG_NFS_V4_1 */
6903 * nfs4_proc_get_locations - discover locations for a migrated FSID
6904 * @inode: inode on FSID that is migrating
6905 * @locations: result of query
6907 * @cred: credential to use for this operation
6909 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6910 * operation failed, or a negative errno if a local error occurred.
6912 * On success, "locations" is filled in, but if the server has
6913 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6916 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6917 * from this client that require migration recovery.
6919 int nfs4_proc_get_locations(struct inode
*inode
,
6920 struct nfs4_fs_locations
*locations
,
6921 struct page
*page
, struct rpc_cred
*cred
)
6923 struct nfs_server
*server
= NFS_SERVER(inode
);
6924 struct nfs_client
*clp
= server
->nfs_client
;
6925 const struct nfs4_mig_recovery_ops
*ops
=
6926 clp
->cl_mvops
->mig_recovery_ops
;
6927 struct nfs4_exception exception
= { };
6930 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6931 (unsigned long long)server
->fsid
.major
,
6932 (unsigned long long)server
->fsid
.minor
,
6934 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6937 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6938 if (status
!= -NFS4ERR_DELAY
)
6940 nfs4_handle_exception(server
, status
, &exception
);
6941 } while (exception
.retry
);
6946 * This operation also signals the server that this client is
6947 * performing "lease moved" recovery. The server can stop
6948 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6949 * is appended to this compound to identify the client ID which is
6950 * performing recovery.
6952 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6954 struct nfs_server
*server
= NFS_SERVER(inode
);
6955 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
6956 struct rpc_clnt
*clnt
= server
->client
;
6957 struct nfs4_fsid_present_arg args
= {
6958 .fh
= NFS_FH(inode
),
6959 .clientid
= clp
->cl_clientid
,
6960 .renew
= 1, /* append RENEW */
6962 struct nfs4_fsid_present_res res
= {
6965 struct rpc_message msg
= {
6966 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6971 unsigned long now
= jiffies
;
6974 res
.fh
= nfs_alloc_fhandle();
6978 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6979 nfs4_set_sequence_privileged(&args
.seq_args
);
6980 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6981 &args
.seq_args
, &res
.seq_res
);
6982 nfs_free_fhandle(res
.fh
);
6986 do_renew_lease(clp
, now
);
6990 #ifdef CONFIG_NFS_V4_1
6993 * This operation also signals the server that this client is
6994 * performing "lease moved" recovery. The server can stop asserting
6995 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6996 * this operation is identified in the SEQUENCE operation in this
6999 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7001 struct nfs_server
*server
= NFS_SERVER(inode
);
7002 struct rpc_clnt
*clnt
= server
->client
;
7003 struct nfs4_fsid_present_arg args
= {
7004 .fh
= NFS_FH(inode
),
7006 struct nfs4_fsid_present_res res
= {
7008 struct rpc_message msg
= {
7009 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
7016 res
.fh
= nfs_alloc_fhandle();
7020 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7021 nfs4_set_sequence_privileged(&args
.seq_args
);
7022 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
7023 &args
.seq_args
, &res
.seq_res
);
7024 nfs_free_fhandle(res
.fh
);
7025 if (status
== NFS4_OK
&&
7026 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
7027 status
= -NFS4ERR_LEASE_MOVED
;
7031 #endif /* CONFIG_NFS_V4_1 */
7034 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7035 * @inode: inode on FSID to check
7036 * @cred: credential to use for this operation
7038 * Server indicates whether the FSID is present, moved, or not
7039 * recognized. This operation is necessary to clear a LEASE_MOVED
7040 * condition for this client ID.
7042 * Returns NFS4_OK if the FSID is present on this server,
7043 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7044 * NFS4ERR code if some error occurred on the server, or a
7045 * negative errno if a local failure occurred.
7047 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7049 struct nfs_server
*server
= NFS_SERVER(inode
);
7050 struct nfs_client
*clp
= server
->nfs_client
;
7051 const struct nfs4_mig_recovery_ops
*ops
=
7052 clp
->cl_mvops
->mig_recovery_ops
;
7053 struct nfs4_exception exception
= { };
7056 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
7057 (unsigned long long)server
->fsid
.major
,
7058 (unsigned long long)server
->fsid
.minor
,
7060 nfs_display_fhandle(NFS_FH(inode
), __func__
);
7063 status
= ops
->fsid_present(inode
, cred
);
7064 if (status
!= -NFS4ERR_DELAY
)
7066 nfs4_handle_exception(server
, status
, &exception
);
7067 } while (exception
.retry
);
7072 * If 'use_integrity' is true and the state managment nfs_client
7073 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7074 * and the machine credential as per RFC3530bis and RFC5661 Security
7075 * Considerations sections. Otherwise, just use the user cred with the
7076 * filesystem's rpc_client.
7078 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7081 struct nfs4_secinfo_arg args
= {
7082 .dir_fh
= NFS_FH(dir
),
7085 struct nfs4_secinfo_res res
= {
7088 struct rpc_message msg
= {
7089 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
7093 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
7094 struct rpc_cred
*cred
= NULL
;
7096 if (use_integrity
) {
7097 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
7098 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
7099 msg
.rpc_cred
= cred
;
7102 dprintk("NFS call secinfo %s\n", name
->name
);
7104 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
7105 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
7107 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
7109 dprintk("NFS reply secinfo: %d\n", status
);
7117 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
7118 struct nfs4_secinfo_flavors
*flavors
)
7120 struct nfs4_exception exception
= { };
7123 err
= -NFS4ERR_WRONGSEC
;
7125 /* try to use integrity protection with machine cred */
7126 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
7127 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
7130 * if unable to use integrity protection, or SECINFO with
7131 * integrity protection returns NFS4ERR_WRONGSEC (which is
7132 * disallowed by spec, but exists in deployed servers) use
7133 * the current filesystem's rpc_client and the user cred.
7135 if (err
== -NFS4ERR_WRONGSEC
)
7136 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
7138 trace_nfs4_secinfo(dir
, name
, err
);
7139 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
7141 } while (exception
.retry
);
7145 #ifdef CONFIG_NFS_V4_1
7147 * Check the exchange flags returned by the server for invalid flags, having
7148 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7151 static int nfs4_check_cl_exchange_flags(u32 flags
)
7153 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
7155 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
7156 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
7158 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
7162 return -NFS4ERR_INVAL
;
7166 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
7167 struct nfs41_server_scope
*b
)
7169 if (a
->server_scope_sz
!= b
->server_scope_sz
)
7171 return memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0;
7175 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
7179 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
7180 .rpc_call_done
= &nfs4_bind_one_conn_to_session_done
,
7184 * nfs4_proc_bind_one_conn_to_session()
7186 * The 4.1 client currently uses the same TCP connection for the
7187 * fore and backchannel.
7190 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
7191 struct rpc_xprt
*xprt
,
7192 struct nfs_client
*clp
,
7193 struct rpc_cred
*cred
)
7196 struct nfs41_bind_conn_to_session_args args
= {
7198 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
7200 struct nfs41_bind_conn_to_session_res res
;
7201 struct rpc_message msg
= {
7203 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
7208 struct rpc_task_setup task_setup_data
= {
7211 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
7212 .rpc_message
= &msg
,
7213 .flags
= RPC_TASK_TIMEOUT
,
7215 struct rpc_task
*task
;
7217 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
7218 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
7219 args
.dir
= NFS4_CDFC4_FORE
;
7221 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7222 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
7223 args
.dir
= NFS4_CDFC4_FORE
;
7225 task
= rpc_run_task(&task_setup_data
);
7226 if (!IS_ERR(task
)) {
7227 status
= task
->tk_status
;
7230 status
= PTR_ERR(task
);
7231 trace_nfs4_bind_conn_to_session(clp
, status
);
7233 if (memcmp(res
.sessionid
.data
,
7234 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
7235 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
7238 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
7239 dprintk("NFS: %s: Unexpected direction from server\n",
7243 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
7244 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7253 struct rpc_bind_conn_calldata
{
7254 struct nfs_client
*clp
;
7255 struct rpc_cred
*cred
;
7259 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
7260 struct rpc_xprt
*xprt
,
7263 struct rpc_bind_conn_calldata
*p
= calldata
;
7265 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
7268 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7270 struct rpc_bind_conn_calldata data
= {
7274 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
7275 nfs4_proc_bind_conn_to_session_callback
, &data
);
7279 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7280 * and operations we'd like to see to enable certain features in the allow map
7282 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
7283 .how
= SP4_MACH_CRED
,
7284 .enforce
.u
.words
= {
7285 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7286 1 << (OP_EXCHANGE_ID
- 32) |
7287 1 << (OP_CREATE_SESSION
- 32) |
7288 1 << (OP_DESTROY_SESSION
- 32) |
7289 1 << (OP_DESTROY_CLIENTID
- 32)
7292 [0] = 1 << (OP_CLOSE
) |
7293 1 << (OP_OPEN_DOWNGRADE
) |
7295 1 << (OP_DELEGRETURN
) |
7297 [1] = 1 << (OP_SECINFO
- 32) |
7298 1 << (OP_SECINFO_NO_NAME
- 32) |
7299 1 << (OP_LAYOUTRETURN
- 32) |
7300 1 << (OP_TEST_STATEID
- 32) |
7301 1 << (OP_FREE_STATEID
- 32) |
7302 1 << (OP_WRITE
- 32)
7307 * Select the state protection mode for client `clp' given the server results
7308 * from exchange_id in `sp'.
7310 * Returns 0 on success, negative errno otherwise.
7312 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
7313 struct nfs41_state_protection
*sp
)
7315 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
7316 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7317 1 << (OP_EXCHANGE_ID
- 32) |
7318 1 << (OP_CREATE_SESSION
- 32) |
7319 1 << (OP_DESTROY_SESSION
- 32) |
7320 1 << (OP_DESTROY_CLIENTID
- 32)
7324 if (sp
->how
== SP4_MACH_CRED
) {
7325 /* Print state protect result */
7326 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
7327 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
7328 if (test_bit(i
, sp
->enforce
.u
.longs
))
7329 dfprintk(MOUNT
, " enforce op %d\n", i
);
7330 if (test_bit(i
, sp
->allow
.u
.longs
))
7331 dfprintk(MOUNT
, " allow op %d\n", i
);
7334 /* make sure nothing is on enforce list that isn't supported */
7335 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
7336 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
7337 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7343 * Minimal mode - state operations are allowed to use machine
7344 * credential. Note this already happens by default, so the
7345 * client doesn't have to do anything more than the negotiation.
7347 * NOTE: we don't care if EXCHANGE_ID is in the list -
7348 * we're already using the machine cred for exchange_id
7349 * and will never use a different cred.
7351 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
7352 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
7353 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
7354 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
7355 dfprintk(MOUNT
, "sp4_mach_cred:\n");
7356 dfprintk(MOUNT
, " minimal mode enabled\n");
7357 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
7359 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7363 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7364 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7365 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7366 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7367 dfprintk(MOUNT
, " cleanup mode enabled\n");
7368 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
7371 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7372 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7373 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
7374 &clp
->cl_sp4_flags
);
7377 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7378 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7379 dfprintk(MOUNT
, " secinfo mode enabled\n");
7380 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
7383 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7384 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7385 dfprintk(MOUNT
, " stateid mode enabled\n");
7386 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
7389 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7390 dfprintk(MOUNT
, " write mode enabled\n");
7391 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
7394 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7395 dfprintk(MOUNT
, " commit mode enabled\n");
7396 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
7403 struct nfs41_exchange_id_data
{
7404 struct nfs41_exchange_id_res res
;
7405 struct nfs41_exchange_id_args args
;
7406 struct rpc_xprt
*xprt
;
7410 static void nfs4_exchange_id_done(struct rpc_task
*task
, void *data
)
7412 struct nfs41_exchange_id_data
*cdata
=
7413 (struct nfs41_exchange_id_data
*)data
;
7414 struct nfs_client
*clp
= cdata
->args
.client
;
7415 int status
= task
->tk_status
;
7417 trace_nfs4_exchange_id(clp
, status
);
7420 status
= nfs4_check_cl_exchange_flags(cdata
->res
.flags
);
7422 if (cdata
->xprt
&& status
== 0) {
7423 status
= nfs4_detect_session_trunking(clp
, &cdata
->res
,
7429 status
= nfs4_sp4_select_mode(clp
, &cdata
->res
.state_protect
);
7432 clp
->cl_clientid
= cdata
->res
.clientid
;
7433 clp
->cl_exchange_flags
= cdata
->res
.flags
;
7434 clp
->cl_seqid
= cdata
->res
.seqid
;
7435 /* Client ID is not confirmed */
7436 if (!(cdata
->res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
))
7437 clear_bit(NFS4_SESSION_ESTABLISHED
,
7438 &clp
->cl_session
->session_state
);
7440 kfree(clp
->cl_serverowner
);
7441 clp
->cl_serverowner
= cdata
->res
.server_owner
;
7442 cdata
->res
.server_owner
= NULL
;
7444 /* use the most recent implementation id */
7445 kfree(clp
->cl_implid
);
7446 clp
->cl_implid
= cdata
->res
.impl_id
;
7447 cdata
->res
.impl_id
= NULL
;
7449 if (clp
->cl_serverscope
!= NULL
&&
7450 !nfs41_same_server_scope(clp
->cl_serverscope
,
7451 cdata
->res
.server_scope
)) {
7452 dprintk("%s: server_scope mismatch detected\n",
7454 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7455 kfree(clp
->cl_serverscope
);
7456 clp
->cl_serverscope
= NULL
;
7459 if (clp
->cl_serverscope
== NULL
) {
7460 clp
->cl_serverscope
= cdata
->res
.server_scope
;
7461 cdata
->res
.server_scope
= NULL
;
7463 /* Save the EXCHANGE_ID verifier session trunk tests */
7464 memcpy(clp
->cl_confirm
.data
, cdata
->args
.verifier
->data
,
7465 sizeof(clp
->cl_confirm
.data
));
7468 cdata
->rpc_status
= status
;
7472 static void nfs4_exchange_id_release(void *data
)
7474 struct nfs41_exchange_id_data
*cdata
=
7475 (struct nfs41_exchange_id_data
*)data
;
7478 xprt_put(cdata
->xprt
);
7479 rpc_clnt_xprt_switch_put(cdata
->args
.client
->cl_rpcclient
);
7481 nfs_put_client(cdata
->args
.client
);
7482 kfree(cdata
->res
.impl_id
);
7483 kfree(cdata
->res
.server_scope
);
7484 kfree(cdata
->res
.server_owner
);
7488 static const struct rpc_call_ops nfs4_exchange_id_call_ops
= {
7489 .rpc_call_done
= nfs4_exchange_id_done
,
7490 .rpc_release
= nfs4_exchange_id_release
,
7494 * _nfs4_proc_exchange_id()
7496 * Wrapper for EXCHANGE_ID operation.
7498 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7499 u32 sp4_how
, struct rpc_xprt
*xprt
)
7501 nfs4_verifier verifier
;
7502 struct rpc_message msg
= {
7503 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7506 struct rpc_task_setup task_setup_data
= {
7507 .rpc_client
= clp
->cl_rpcclient
,
7508 .callback_ops
= &nfs4_exchange_id_call_ops
,
7509 .rpc_message
= &msg
,
7510 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7512 struct nfs41_exchange_id_data
*calldata
;
7513 struct rpc_task
*task
;
7516 if (!atomic_inc_not_zero(&clp
->cl_count
))
7519 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7521 nfs_put_client(clp
);
7526 nfs4_init_boot_verifier(clp
, &verifier
);
7528 status
= nfs4_init_uniform_client_string(clp
);
7532 calldata
->res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7535 if (unlikely(calldata
->res
.server_owner
== NULL
))
7538 calldata
->res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7540 if (unlikely(calldata
->res
.server_scope
== NULL
))
7541 goto out_server_owner
;
7543 calldata
->res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7544 if (unlikely(calldata
->res
.impl_id
== NULL
))
7545 goto out_server_scope
;
7549 calldata
->args
.state_protect
.how
= SP4_NONE
;
7553 calldata
->args
.state_protect
= nfs4_sp4_mach_cred_request
;
7563 calldata
->xprt
= xprt
;
7564 task_setup_data
.rpc_xprt
= xprt
;
7565 task_setup_data
.flags
=
7566 RPC_TASK_SOFT
|RPC_TASK_SOFTCONN
|RPC_TASK_ASYNC
;
7567 calldata
->args
.verifier
= &clp
->cl_confirm
;
7569 calldata
->args
.verifier
= &verifier
;
7571 calldata
->args
.client
= clp
;
7572 #ifdef CONFIG_NFS_V4_1_MIGRATION
7573 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7574 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
7575 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
7577 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7578 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
7580 msg
.rpc_argp
= &calldata
->args
;
7581 msg
.rpc_resp
= &calldata
->res
;
7582 task_setup_data
.callback_data
= calldata
;
7584 task
= rpc_run_task(&task_setup_data
);
7586 return PTR_ERR(task
);
7589 status
= rpc_wait_for_completion_task(task
);
7591 status
= calldata
->rpc_status
;
7592 } else /* session trunking test */
7593 status
= calldata
->rpc_status
;
7600 kfree(calldata
->res
.impl_id
);
7602 kfree(calldata
->res
.server_scope
);
7604 kfree(calldata
->res
.server_owner
);
7607 nfs_put_client(clp
);
7612 * nfs4_proc_exchange_id()
7614 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7616 * Since the clientid has expired, all compounds using sessions
7617 * associated with the stale clientid will be returning
7618 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7619 * be in some phase of session reset.
7621 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7623 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7625 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7628 /* try SP4_MACH_CRED if krb5i/p */
7629 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7630 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7631 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
, NULL
);
7637 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
, NULL
);
7641 * nfs4_test_session_trunk
7643 * This is an add_xprt_test() test function called from
7644 * rpc_clnt_setup_test_and_add_xprt.
7646 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7647 * and is dereferrenced in nfs4_exchange_id_release
7649 * Upon success, add the new transport to the rpc_clnt
7651 * @clnt: struct rpc_clnt to get new transport
7652 * @xprt: the rpc_xprt to test
7653 * @data: call data for _nfs4_proc_exchange_id.
7655 int nfs4_test_session_trunk(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
,
7658 struct nfs4_add_xprt_data
*adata
= (struct nfs4_add_xprt_data
*)data
;
7661 dprintk("--> %s try %s\n", __func__
,
7662 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
7664 sp4_how
= (adata
->clp
->cl_sp4_flags
== 0 ? SP4_NONE
: SP4_MACH_CRED
);
7666 /* Test connection for session trunking. Async exchange_id call */
7667 return _nfs4_proc_exchange_id(adata
->clp
, adata
->cred
, sp4_how
, xprt
);
7669 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk
);
7671 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7672 struct rpc_cred
*cred
)
7674 struct rpc_message msg
= {
7675 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7681 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7682 trace_nfs4_destroy_clientid(clp
, status
);
7684 dprintk("NFS: Got error %d from the server %s on "
7685 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7689 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7690 struct rpc_cred
*cred
)
7695 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7696 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7698 case -NFS4ERR_DELAY
:
7699 case -NFS4ERR_CLIENTID_BUSY
:
7709 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7711 struct rpc_cred
*cred
;
7714 if (clp
->cl_mvops
->minor_version
< 1)
7716 if (clp
->cl_exchange_flags
== 0)
7718 if (clp
->cl_preserve_clid
)
7720 cred
= nfs4_get_clid_cred(clp
);
7721 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7726 case -NFS4ERR_STALE_CLIENTID
:
7727 clp
->cl_exchange_flags
= 0;
7733 struct nfs4_get_lease_time_data
{
7734 struct nfs4_get_lease_time_args
*args
;
7735 struct nfs4_get_lease_time_res
*res
;
7736 struct nfs_client
*clp
;
7739 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7742 struct nfs4_get_lease_time_data
*data
=
7743 (struct nfs4_get_lease_time_data
*)calldata
;
7745 dprintk("--> %s\n", __func__
);
7746 /* just setup sequence, do not trigger session recovery
7747 since we're invoked within one */
7748 nfs4_setup_sequence(data
->clp
,
7749 &data
->args
->la_seq_args
,
7750 &data
->res
->lr_seq_res
,
7752 dprintk("<-- %s\n", __func__
);
7756 * Called from nfs4_state_manager thread for session setup, so don't recover
7757 * from sequence operation or clientid errors.
7759 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7761 struct nfs4_get_lease_time_data
*data
=
7762 (struct nfs4_get_lease_time_data
*)calldata
;
7764 dprintk("--> %s\n", __func__
);
7765 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7767 switch (task
->tk_status
) {
7768 case -NFS4ERR_DELAY
:
7769 case -NFS4ERR_GRACE
:
7770 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7771 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7772 task
->tk_status
= 0;
7774 case -NFS4ERR_RETRY_UNCACHED_REP
:
7775 rpc_restart_call_prepare(task
);
7778 dprintk("<-- %s\n", __func__
);
7781 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7782 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7783 .rpc_call_done
= nfs4_get_lease_time_done
,
7786 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7788 struct rpc_task
*task
;
7789 struct nfs4_get_lease_time_args args
;
7790 struct nfs4_get_lease_time_res res
= {
7791 .lr_fsinfo
= fsinfo
,
7793 struct nfs4_get_lease_time_data data
= {
7798 struct rpc_message msg
= {
7799 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7803 struct rpc_task_setup task_setup
= {
7804 .rpc_client
= clp
->cl_rpcclient
,
7805 .rpc_message
= &msg
,
7806 .callback_ops
= &nfs4_get_lease_time_ops
,
7807 .callback_data
= &data
,
7808 .flags
= RPC_TASK_TIMEOUT
,
7812 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7813 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7814 task
= rpc_run_task(&task_setup
);
7817 return PTR_ERR(task
);
7819 status
= task
->tk_status
;
7825 * Initialize the values to be used by the client in CREATE_SESSION
7826 * If nfs4_init_session set the fore channel request and response sizes,
7829 * Set the back channel max_resp_sz_cached to zero to force the client to
7830 * always set csa_cachethis to FALSE because the current implementation
7831 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7833 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7834 struct rpc_clnt
*clnt
)
7836 unsigned int max_rqst_sz
, max_resp_sz
;
7837 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7839 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7840 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7842 /* Fore channel attributes */
7843 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7844 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7845 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7846 args
->fc_attrs
.max_reqs
= max_session_slots
;
7848 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7849 "max_ops=%u max_reqs=%u\n",
7851 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7852 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7854 /* Back channel attributes */
7855 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7856 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7857 args
->bc_attrs
.max_resp_sz_cached
= 0;
7858 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7859 args
->bc_attrs
.max_reqs
= min_t(unsigned short, max_session_cb_slots
, 1);
7861 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7862 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7864 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7865 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7866 args
->bc_attrs
.max_reqs
);
7869 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7870 struct nfs41_create_session_res
*res
)
7872 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7873 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7875 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7878 * Our requested max_ops is the minimum we need; we're not
7879 * prepared to break up compounds into smaller pieces than that.
7880 * So, no point even trying to continue if the server won't
7883 if (rcvd
->max_ops
< sent
->max_ops
)
7885 if (rcvd
->max_reqs
== 0)
7887 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7888 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7892 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7893 struct nfs41_create_session_res
*res
)
7895 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7896 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7898 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7900 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7902 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7904 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7906 if (rcvd
->max_ops
> sent
->max_ops
)
7908 if (rcvd
->max_reqs
> sent
->max_reqs
)
7914 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7915 struct nfs41_create_session_res
*res
)
7919 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
7922 return nfs4_verify_back_channel_attrs(args
, res
);
7925 static void nfs4_update_session(struct nfs4_session
*session
,
7926 struct nfs41_create_session_res
*res
)
7928 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
7929 /* Mark client id and session as being confirmed */
7930 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
7931 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
7932 session
->flags
= res
->flags
;
7933 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
7934 if (res
->flags
& SESSION4_BACK_CHAN
)
7935 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
7936 sizeof(session
->bc_attrs
));
7939 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
7940 struct rpc_cred
*cred
)
7942 struct nfs4_session
*session
= clp
->cl_session
;
7943 struct nfs41_create_session_args args
= {
7945 .clientid
= clp
->cl_clientid
,
7946 .seqid
= clp
->cl_seqid
,
7947 .cb_program
= NFS4_CALLBACK
,
7949 struct nfs41_create_session_res res
;
7951 struct rpc_message msg
= {
7952 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
7959 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
7960 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
7962 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7963 trace_nfs4_create_session(clp
, status
);
7966 case -NFS4ERR_STALE_CLIENTID
:
7967 case -NFS4ERR_DELAY
:
7976 /* Verify the session's negotiated channel_attrs values */
7977 status
= nfs4_verify_channel_attrs(&args
, &res
);
7978 /* Increment the clientid slot sequence id */
7981 nfs4_update_session(session
, &res
);
7988 * Issues a CREATE_SESSION operation to the server.
7989 * It is the responsibility of the caller to verify the session is
7990 * expired before calling this routine.
7992 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7996 struct nfs4_session
*session
= clp
->cl_session
;
7998 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
8000 status
= _nfs4_proc_create_session(clp
, cred
);
8004 /* Init or reset the session slot tables */
8005 status
= nfs4_setup_session_slot_tables(session
);
8006 dprintk("slot table setup returned %d\n", status
);
8010 ptr
= (unsigned *)&session
->sess_id
.data
[0];
8011 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
8012 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
8014 dprintk("<-- %s\n", __func__
);
8019 * Issue the over-the-wire RPC DESTROY_SESSION.
8020 * The caller must serialize access to this routine.
8022 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
8023 struct rpc_cred
*cred
)
8025 struct rpc_message msg
= {
8026 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
8027 .rpc_argp
= session
,
8032 dprintk("--> nfs4_proc_destroy_session\n");
8034 /* session is still being setup */
8035 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
8038 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8039 trace_nfs4_destroy_session(session
->clp
, status
);
8042 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8043 "Session has been destroyed regardless...\n", status
);
8045 dprintk("<-- nfs4_proc_destroy_session\n");
8050 * Renew the cl_session lease.
8052 struct nfs4_sequence_data
{
8053 struct nfs_client
*clp
;
8054 struct nfs4_sequence_args args
;
8055 struct nfs4_sequence_res res
;
8058 static void nfs41_sequence_release(void *data
)
8060 struct nfs4_sequence_data
*calldata
= data
;
8061 struct nfs_client
*clp
= calldata
->clp
;
8063 if (atomic_read(&clp
->cl_count
) > 1)
8064 nfs4_schedule_state_renewal(clp
);
8065 nfs_put_client(clp
);
8069 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8071 switch(task
->tk_status
) {
8072 case -NFS4ERR_DELAY
:
8073 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8076 nfs4_schedule_lease_recovery(clp
);
8081 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
8083 struct nfs4_sequence_data
*calldata
= data
;
8084 struct nfs_client
*clp
= calldata
->clp
;
8086 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
8089 trace_nfs4_sequence(clp
, task
->tk_status
);
8090 if (task
->tk_status
< 0) {
8091 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
8092 if (atomic_read(&clp
->cl_count
) == 1)
8095 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
8096 rpc_restart_call_prepare(task
);
8100 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
8102 dprintk("<-- %s\n", __func__
);
8105 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
8107 struct nfs4_sequence_data
*calldata
= data
;
8108 struct nfs_client
*clp
= calldata
->clp
;
8109 struct nfs4_sequence_args
*args
;
8110 struct nfs4_sequence_res
*res
;
8112 args
= task
->tk_msg
.rpc_argp
;
8113 res
= task
->tk_msg
.rpc_resp
;
8115 nfs4_setup_sequence(clp
, args
, res
, task
);
8118 static const struct rpc_call_ops nfs41_sequence_ops
= {
8119 .rpc_call_done
= nfs41_sequence_call_done
,
8120 .rpc_call_prepare
= nfs41_sequence_prepare
,
8121 .rpc_release
= nfs41_sequence_release
,
8124 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
8125 struct rpc_cred
*cred
,
8128 struct nfs4_sequence_data
*calldata
;
8129 struct rpc_message msg
= {
8130 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
8133 struct rpc_task_setup task_setup_data
= {
8134 .rpc_client
= clp
->cl_rpcclient
,
8135 .rpc_message
= &msg
,
8136 .callback_ops
= &nfs41_sequence_ops
,
8137 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
8140 if (!atomic_inc_not_zero(&clp
->cl_count
))
8141 return ERR_PTR(-EIO
);
8142 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8143 if (calldata
== NULL
) {
8144 nfs_put_client(clp
);
8145 return ERR_PTR(-ENOMEM
);
8147 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
8149 nfs4_set_sequence_privileged(&calldata
->args
);
8150 msg
.rpc_argp
= &calldata
->args
;
8151 msg
.rpc_resp
= &calldata
->res
;
8152 calldata
->clp
= clp
;
8153 task_setup_data
.callback_data
= calldata
;
8155 return rpc_run_task(&task_setup_data
);
8158 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
8160 struct rpc_task
*task
;
8163 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
8165 task
= _nfs41_proc_sequence(clp
, cred
, false);
8167 ret
= PTR_ERR(task
);
8169 rpc_put_task_async(task
);
8170 dprintk("<-- %s status=%d\n", __func__
, ret
);
8174 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8176 struct rpc_task
*task
;
8179 task
= _nfs41_proc_sequence(clp
, cred
, true);
8181 ret
= PTR_ERR(task
);
8184 ret
= rpc_wait_for_completion_task(task
);
8186 ret
= task
->tk_status
;
8189 dprintk("<-- %s status=%d\n", __func__
, ret
);
8193 struct nfs4_reclaim_complete_data
{
8194 struct nfs_client
*clp
;
8195 struct nfs41_reclaim_complete_args arg
;
8196 struct nfs41_reclaim_complete_res res
;
8199 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
8201 struct nfs4_reclaim_complete_data
*calldata
= data
;
8203 nfs4_setup_sequence(calldata
->clp
,
8204 &calldata
->arg
.seq_args
,
8205 &calldata
->res
.seq_res
,
8209 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8211 switch(task
->tk_status
) {
8213 case -NFS4ERR_COMPLETE_ALREADY
:
8214 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
8216 case -NFS4ERR_DELAY
:
8217 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8219 case -NFS4ERR_RETRY_UNCACHED_REP
:
8221 case -NFS4ERR_BADSESSION
:
8222 case -NFS4ERR_DEADSESSION
:
8223 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
8224 nfs4_schedule_session_recovery(clp
->cl_session
,
8228 nfs4_schedule_lease_recovery(clp
);
8233 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
8235 struct nfs4_reclaim_complete_data
*calldata
= data
;
8236 struct nfs_client
*clp
= calldata
->clp
;
8237 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
8239 dprintk("--> %s\n", __func__
);
8240 if (!nfs41_sequence_done(task
, res
))
8243 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
8244 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
8245 rpc_restart_call_prepare(task
);
8248 dprintk("<-- %s\n", __func__
);
8251 static void nfs4_free_reclaim_complete_data(void *data
)
8253 struct nfs4_reclaim_complete_data
*calldata
= data
;
8258 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
8259 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
8260 .rpc_call_done
= nfs4_reclaim_complete_done
,
8261 .rpc_release
= nfs4_free_reclaim_complete_data
,
8265 * Issue a global reclaim complete.
8267 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
8268 struct rpc_cred
*cred
)
8270 struct nfs4_reclaim_complete_data
*calldata
;
8271 struct rpc_task
*task
;
8272 struct rpc_message msg
= {
8273 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
8276 struct rpc_task_setup task_setup_data
= {
8277 .rpc_client
= clp
->cl_rpcclient
,
8278 .rpc_message
= &msg
,
8279 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
8280 .flags
= RPC_TASK_ASYNC
,
8282 int status
= -ENOMEM
;
8284 dprintk("--> %s\n", __func__
);
8285 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8286 if (calldata
== NULL
)
8288 calldata
->clp
= clp
;
8289 calldata
->arg
.one_fs
= 0;
8291 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
8292 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
8293 msg
.rpc_argp
= &calldata
->arg
;
8294 msg
.rpc_resp
= &calldata
->res
;
8295 task_setup_data
.callback_data
= calldata
;
8296 task
= rpc_run_task(&task_setup_data
);
8298 status
= PTR_ERR(task
);
8301 status
= rpc_wait_for_completion_task(task
);
8303 status
= task
->tk_status
;
8306 dprintk("<-- %s status=%d\n", __func__
, status
);
8311 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
8313 struct nfs4_layoutget
*lgp
= calldata
;
8314 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
8316 dprintk("--> %s\n", __func__
);
8317 nfs4_setup_sequence(server
->nfs_client
, &lgp
->args
.seq_args
,
8318 &lgp
->res
.seq_res
, task
);
8319 dprintk("<-- %s\n", __func__
);
8322 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
8324 struct nfs4_layoutget
*lgp
= calldata
;
8326 dprintk("--> %s\n", __func__
);
8327 nfs41_sequence_process(task
, &lgp
->res
.seq_res
);
8328 dprintk("<-- %s\n", __func__
);
8332 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
8333 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
8335 struct inode
*inode
= lgp
->args
.inode
;
8336 struct nfs_server
*server
= NFS_SERVER(inode
);
8337 struct pnfs_layout_hdr
*lo
;
8338 int nfs4err
= task
->tk_status
;
8339 int err
, status
= 0;
8342 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
8349 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8350 * on the file. set tk_status to -ENODATA to tell upper layer to
8353 case -NFS4ERR_LAYOUTUNAVAILABLE
:
8357 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8358 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8360 case -NFS4ERR_BADLAYOUT
:
8361 status
= -EOVERFLOW
;
8364 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8365 * (or clients) writing to the same RAID stripe except when
8366 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8368 * Treat it like we would RECALLCONFLICT -- we retry for a little
8369 * while, and then eventually give up.
8371 case -NFS4ERR_LAYOUTTRYLATER
:
8372 if (lgp
->args
.minlength
== 0) {
8373 status
= -EOVERFLOW
;
8378 case -NFS4ERR_RECALLCONFLICT
:
8379 status
= -ERECALLCONFLICT
;
8381 case -NFS4ERR_DELEG_REVOKED
:
8382 case -NFS4ERR_ADMIN_REVOKED
:
8383 case -NFS4ERR_EXPIRED
:
8384 case -NFS4ERR_BAD_STATEID
:
8385 exception
->timeout
= 0;
8386 spin_lock(&inode
->i_lock
);
8387 lo
= NFS_I(inode
)->layout
;
8388 /* If the open stateid was bad, then recover it. */
8389 if (!lo
|| test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) ||
8390 nfs4_stateid_match_other(&lgp
->args
.stateid
,
8391 &lgp
->args
.ctx
->state
->stateid
)) {
8392 spin_unlock(&inode
->i_lock
);
8393 exception
->state
= lgp
->args
.ctx
->state
;
8394 exception
->stateid
= &lgp
->args
.stateid
;
8399 * Mark the bad layout state as invalid, then retry
8401 pnfs_mark_layout_stateid_invalid(lo
, &head
);
8402 spin_unlock(&inode
->i_lock
);
8403 nfs_commit_inode(inode
, 0);
8404 pnfs_free_lseg_list(&head
);
8409 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8410 err
= nfs4_handle_exception(server
, nfs4err
, exception
);
8412 if (exception
->retry
)
8418 dprintk("<-- %s\n", __func__
);
8422 static size_t max_response_pages(struct nfs_server
*server
)
8424 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
8425 return nfs_page_array_len(0, max_resp_sz
);
8428 static void nfs4_free_pages(struct page
**pages
, size_t size
)
8435 for (i
= 0; i
< size
; i
++) {
8438 __free_page(pages
[i
]);
8443 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
8445 struct page
**pages
;
8448 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
8450 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
8454 for (i
= 0; i
< size
; i
++) {
8455 pages
[i
] = alloc_page(gfp_flags
);
8457 dprintk("%s: failed to allocate page\n", __func__
);
8458 nfs4_free_pages(pages
, size
);
8466 static void nfs4_layoutget_release(void *calldata
)
8468 struct nfs4_layoutget
*lgp
= calldata
;
8469 struct inode
*inode
= lgp
->args
.inode
;
8470 struct nfs_server
*server
= NFS_SERVER(inode
);
8471 size_t max_pages
= max_response_pages(server
);
8473 dprintk("--> %s\n", __func__
);
8474 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8475 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8476 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8477 put_nfs_open_context(lgp
->args
.ctx
);
8479 dprintk("<-- %s\n", __func__
);
8482 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8483 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8484 .rpc_call_done
= nfs4_layoutget_done
,
8485 .rpc_release
= nfs4_layoutget_release
,
8488 struct pnfs_layout_segment
*
8489 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8491 struct inode
*inode
= lgp
->args
.inode
;
8492 struct nfs_server
*server
= NFS_SERVER(inode
);
8493 size_t max_pages
= max_response_pages(server
);
8494 struct rpc_task
*task
;
8495 struct rpc_message msg
= {
8496 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8497 .rpc_argp
= &lgp
->args
,
8498 .rpc_resp
= &lgp
->res
,
8499 .rpc_cred
= lgp
->cred
,
8501 struct rpc_task_setup task_setup_data
= {
8502 .rpc_client
= server
->client
,
8503 .rpc_message
= &msg
,
8504 .callback_ops
= &nfs4_layoutget_call_ops
,
8505 .callback_data
= lgp
,
8506 .flags
= RPC_TASK_ASYNC
,
8508 struct pnfs_layout_segment
*lseg
= NULL
;
8509 struct nfs4_exception exception
= {
8511 .timeout
= *timeout
,
8515 dprintk("--> %s\n", __func__
);
8517 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8518 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8520 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8521 if (!lgp
->args
.layout
.pages
) {
8522 nfs4_layoutget_release(lgp
);
8523 return ERR_PTR(-ENOMEM
);
8525 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8527 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8528 lgp
->res
.seq_res
.sr_slot
= NULL
;
8529 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8531 task
= rpc_run_task(&task_setup_data
);
8533 return ERR_CAST(task
);
8534 status
= rpc_wait_for_completion_task(task
);
8536 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8537 *timeout
= exception
.timeout
;
8540 trace_nfs4_layoutget(lgp
->args
.ctx
,
8546 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8547 if (status
== 0 && lgp
->res
.layoutp
->len
)
8548 lseg
= pnfs_layout_process(lgp
);
8550 dprintk("<-- %s status=%d\n", __func__
, status
);
8552 return ERR_PTR(status
);
8557 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8559 struct nfs4_layoutreturn
*lrp
= calldata
;
8561 dprintk("--> %s\n", __func__
);
8562 nfs4_setup_sequence(lrp
->clp
,
8563 &lrp
->args
.seq_args
,
8568 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8570 struct nfs4_layoutreturn
*lrp
= calldata
;
8571 struct nfs_server
*server
;
8573 dprintk("--> %s\n", __func__
);
8575 if (!nfs41_sequence_process(task
, &lrp
->res
.seq_res
))
8578 server
= NFS_SERVER(lrp
->args
.inode
);
8579 switch (task
->tk_status
) {
8581 task
->tk_status
= 0;
8584 case -NFS4ERR_DELAY
:
8585 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8587 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8588 rpc_restart_call_prepare(task
);
8591 dprintk("<-- %s\n", __func__
);
8594 static void nfs4_layoutreturn_release(void *calldata
)
8596 struct nfs4_layoutreturn
*lrp
= calldata
;
8597 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8599 dprintk("--> %s\n", __func__
);
8600 pnfs_layoutreturn_free_lsegs(lo
, &lrp
->args
.stateid
, &lrp
->args
.range
,
8601 lrp
->res
.lrs_present
? &lrp
->res
.stateid
: NULL
);
8602 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8603 if (lrp
->ld_private
.ops
&& lrp
->ld_private
.ops
->free
)
8604 lrp
->ld_private
.ops
->free(&lrp
->ld_private
);
8605 pnfs_put_layout_hdr(lrp
->args
.layout
);
8606 nfs_iput_and_deactive(lrp
->inode
);
8608 dprintk("<-- %s\n", __func__
);
8611 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8612 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8613 .rpc_call_done
= nfs4_layoutreturn_done
,
8614 .rpc_release
= nfs4_layoutreturn_release
,
8617 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8619 struct rpc_task
*task
;
8620 struct rpc_message msg
= {
8621 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8622 .rpc_argp
= &lrp
->args
,
8623 .rpc_resp
= &lrp
->res
,
8624 .rpc_cred
= lrp
->cred
,
8626 struct rpc_task_setup task_setup_data
= {
8627 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8628 .rpc_message
= &msg
,
8629 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8630 .callback_data
= lrp
,
8634 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8635 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8636 &task_setup_data
.rpc_client
, &msg
);
8638 dprintk("--> %s\n", __func__
);
8640 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8642 nfs4_layoutreturn_release(lrp
);
8645 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8647 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8648 task
= rpc_run_task(&task_setup_data
);
8650 return PTR_ERR(task
);
8652 status
= task
->tk_status
;
8653 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8654 dprintk("<-- %s status=%d\n", __func__
, status
);
8660 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8661 struct pnfs_device
*pdev
,
8662 struct rpc_cred
*cred
)
8664 struct nfs4_getdeviceinfo_args args
= {
8666 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8667 NOTIFY_DEVICEID4_DELETE
,
8669 struct nfs4_getdeviceinfo_res res
= {
8672 struct rpc_message msg
= {
8673 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8680 dprintk("--> %s\n", __func__
);
8681 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8682 if (res
.notification
& ~args
.notify_types
)
8683 dprintk("%s: unsupported notification\n", __func__
);
8684 if (res
.notification
!= args
.notify_types
)
8687 dprintk("<-- %s status=%d\n", __func__
, status
);
8692 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8693 struct pnfs_device
*pdev
,
8694 struct rpc_cred
*cred
)
8696 struct nfs4_exception exception
= { };
8700 err
= nfs4_handle_exception(server
,
8701 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8703 } while (exception
.retry
);
8706 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8708 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8710 struct nfs4_layoutcommit_data
*data
= calldata
;
8711 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8713 nfs4_setup_sequence(server
->nfs_client
,
8714 &data
->args
.seq_args
,
8720 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8722 struct nfs4_layoutcommit_data
*data
= calldata
;
8723 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8725 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8728 switch (task
->tk_status
) { /* Just ignore these failures */
8729 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8730 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8731 case -NFS4ERR_BADLAYOUT
: /* no layout */
8732 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8733 task
->tk_status
= 0;
8737 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8738 rpc_restart_call_prepare(task
);
8744 static void nfs4_layoutcommit_release(void *calldata
)
8746 struct nfs4_layoutcommit_data
*data
= calldata
;
8748 pnfs_cleanup_layoutcommit(data
);
8749 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8751 put_rpccred(data
->cred
);
8752 nfs_iput_and_deactive(data
->inode
);
8756 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8757 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8758 .rpc_call_done
= nfs4_layoutcommit_done
,
8759 .rpc_release
= nfs4_layoutcommit_release
,
8763 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8765 struct rpc_message msg
= {
8766 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8767 .rpc_argp
= &data
->args
,
8768 .rpc_resp
= &data
->res
,
8769 .rpc_cred
= data
->cred
,
8771 struct rpc_task_setup task_setup_data
= {
8772 .task
= &data
->task
,
8773 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8774 .rpc_message
= &msg
,
8775 .callback_ops
= &nfs4_layoutcommit_ops
,
8776 .callback_data
= data
,
8778 struct rpc_task
*task
;
8781 dprintk("NFS: initiating layoutcommit call. sync %d "
8782 "lbw: %llu inode %lu\n", sync
,
8783 data
->args
.lastbytewritten
,
8784 data
->args
.inode
->i_ino
);
8787 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8788 if (data
->inode
== NULL
) {
8789 nfs4_layoutcommit_release(data
);
8792 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8794 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8795 task
= rpc_run_task(&task_setup_data
);
8797 return PTR_ERR(task
);
8799 status
= task
->tk_status
;
8800 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8801 dprintk("%s: status %d\n", __func__
, status
);
8807 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8808 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8811 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8812 struct nfs_fsinfo
*info
,
8813 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8815 struct nfs41_secinfo_no_name_args args
= {
8816 .style
= SECINFO_STYLE_CURRENT_FH
,
8818 struct nfs4_secinfo_res res
= {
8821 struct rpc_message msg
= {
8822 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8826 struct rpc_clnt
*clnt
= server
->client
;
8827 struct rpc_cred
*cred
= NULL
;
8830 if (use_integrity
) {
8831 clnt
= server
->nfs_client
->cl_rpcclient
;
8832 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8833 msg
.rpc_cred
= cred
;
8836 dprintk("--> %s\n", __func__
);
8837 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8839 dprintk("<-- %s status=%d\n", __func__
, status
);
8848 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8849 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8851 struct nfs4_exception exception
= { };
8854 /* first try using integrity protection */
8855 err
= -NFS4ERR_WRONGSEC
;
8857 /* try to use integrity protection with machine cred */
8858 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8859 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8863 * if unable to use integrity protection, or SECINFO with
8864 * integrity protection returns NFS4ERR_WRONGSEC (which is
8865 * disallowed by spec, but exists in deployed servers) use
8866 * the current filesystem's rpc_client and the user cred.
8868 if (err
== -NFS4ERR_WRONGSEC
)
8869 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8874 case -NFS4ERR_WRONGSEC
:
8878 err
= nfs4_handle_exception(server
, err
, &exception
);
8880 } while (exception
.retry
);
8886 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8887 struct nfs_fsinfo
*info
)
8891 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8892 struct nfs4_secinfo_flavors
*flavors
;
8893 struct nfs4_secinfo4
*secinfo
;
8896 page
= alloc_page(GFP_KERNEL
);
8902 flavors
= page_address(page
);
8903 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8906 * Fall back on "guess and check" method if
8907 * the server doesn't support SECINFO_NO_NAME
8909 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8910 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8916 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8917 secinfo
= &flavors
->flavors
[i
];
8919 switch (secinfo
->flavor
) {
8923 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
8924 &secinfo
->flavor_info
);
8927 flavor
= RPC_AUTH_MAXFLAVOR
;
8931 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
8932 flavor
= RPC_AUTH_MAXFLAVOR
;
8934 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
8935 err
= nfs4_lookup_root_sec(server
, fhandle
,
8942 if (flavor
== RPC_AUTH_MAXFLAVOR
)
8953 static int _nfs41_test_stateid(struct nfs_server
*server
,
8954 nfs4_stateid
*stateid
,
8955 struct rpc_cred
*cred
)
8958 struct nfs41_test_stateid_args args
= {
8961 struct nfs41_test_stateid_res res
;
8962 struct rpc_message msg
= {
8963 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
8968 struct rpc_clnt
*rpc_client
= server
->client
;
8970 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
8973 dprintk("NFS call test_stateid %p\n", stateid
);
8974 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
8975 nfs4_set_sequence_privileged(&args
.seq_args
);
8976 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
8977 &args
.seq_args
, &res
.seq_res
);
8978 if (status
!= NFS_OK
) {
8979 dprintk("NFS reply test_stateid: failed, %d\n", status
);
8982 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
8986 static void nfs4_handle_delay_or_session_error(struct nfs_server
*server
,
8987 int err
, struct nfs4_exception
*exception
)
8989 exception
->retry
= 0;
8991 case -NFS4ERR_DELAY
:
8992 case -NFS4ERR_RETRY_UNCACHED_REP
:
8993 nfs4_handle_exception(server
, err
, exception
);
8995 case -NFS4ERR_BADSESSION
:
8996 case -NFS4ERR_BADSLOT
:
8997 case -NFS4ERR_BAD_HIGH_SLOT
:
8998 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
8999 case -NFS4ERR_DEADSESSION
:
9000 nfs4_do_handle_exception(server
, err
, exception
);
9005 * nfs41_test_stateid - perform a TEST_STATEID operation
9007 * @server: server / transport on which to perform the operation
9008 * @stateid: state ID to test
9011 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9012 * Otherwise a negative NFS4ERR value is returned if the operation
9013 * failed or the state ID is not currently valid.
9015 static int nfs41_test_stateid(struct nfs_server
*server
,
9016 nfs4_stateid
*stateid
,
9017 struct rpc_cred
*cred
)
9019 struct nfs4_exception exception
= { };
9022 err
= _nfs41_test_stateid(server
, stateid
, cred
);
9023 nfs4_handle_delay_or_session_error(server
, err
, &exception
);
9024 } while (exception
.retry
);
9028 struct nfs_free_stateid_data
{
9029 struct nfs_server
*server
;
9030 struct nfs41_free_stateid_args args
;
9031 struct nfs41_free_stateid_res res
;
9034 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
9036 struct nfs_free_stateid_data
*data
= calldata
;
9037 nfs4_setup_sequence(data
->server
->nfs_client
,
9038 &data
->args
.seq_args
,
9043 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
9045 struct nfs_free_stateid_data
*data
= calldata
;
9047 nfs41_sequence_done(task
, &data
->res
.seq_res
);
9049 switch (task
->tk_status
) {
9050 case -NFS4ERR_DELAY
:
9051 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
9052 rpc_restart_call_prepare(task
);
9056 static void nfs41_free_stateid_release(void *calldata
)
9061 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
9062 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
9063 .rpc_call_done
= nfs41_free_stateid_done
,
9064 .rpc_release
= nfs41_free_stateid_release
,
9067 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
9068 const nfs4_stateid
*stateid
,
9069 struct rpc_cred
*cred
,
9072 struct rpc_message msg
= {
9073 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
9076 struct rpc_task_setup task_setup
= {
9077 .rpc_client
= server
->client
,
9078 .rpc_message
= &msg
,
9079 .callback_ops
= &nfs41_free_stateid_ops
,
9080 .flags
= RPC_TASK_ASYNC
,
9082 struct nfs_free_stateid_data
*data
;
9084 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9085 &task_setup
.rpc_client
, &msg
);
9087 dprintk("NFS call free_stateid %p\n", stateid
);
9088 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
9090 return ERR_PTR(-ENOMEM
);
9091 data
->server
= server
;
9092 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
9094 task_setup
.callback_data
= data
;
9096 msg
.rpc_argp
= &data
->args
;
9097 msg
.rpc_resp
= &data
->res
;
9098 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
9100 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
9102 return rpc_run_task(&task_setup
);
9106 * nfs41_free_stateid - perform a FREE_STATEID operation
9108 * @server: server / transport on which to perform the operation
9109 * @stateid: state ID to release
9111 * @is_recovery: set to true if this call needs to be privileged
9113 * Note: this function is always asynchronous.
9115 static int nfs41_free_stateid(struct nfs_server
*server
,
9116 const nfs4_stateid
*stateid
,
9117 struct rpc_cred
*cred
,
9120 struct rpc_task
*task
;
9122 task
= _nfs41_free_stateid(server
, stateid
, cred
, is_recovery
);
9124 return PTR_ERR(task
);
9130 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
9132 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
9134 nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
9135 nfs4_free_lock_state(server
, lsp
);
9138 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
9139 const nfs4_stateid
*s2
)
9141 if (s1
->type
!= s2
->type
)
9144 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
9147 if (s1
->seqid
== s2
->seqid
)
9150 return s1
->seqid
== 0 || s2
->seqid
== 0;
9153 #endif /* CONFIG_NFS_V4_1 */
9155 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
9156 const nfs4_stateid
*s2
)
9158 return nfs4_stateid_match(s1
, s2
);
9162 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
9163 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9164 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9165 .recover_open
= nfs4_open_reclaim
,
9166 .recover_lock
= nfs4_lock_reclaim
,
9167 .establish_clid
= nfs4_init_clientid
,
9168 .detect_trunking
= nfs40_discover_server_trunking
,
9171 #if defined(CONFIG_NFS_V4_1)
9172 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
9173 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9174 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9175 .recover_open
= nfs4_open_reclaim
,
9176 .recover_lock
= nfs4_lock_reclaim
,
9177 .establish_clid
= nfs41_init_clientid
,
9178 .reclaim_complete
= nfs41_proc_reclaim_complete
,
9179 .detect_trunking
= nfs41_discover_server_trunking
,
9181 #endif /* CONFIG_NFS_V4_1 */
9183 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
9184 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9185 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9186 .recover_open
= nfs40_open_expired
,
9187 .recover_lock
= nfs4_lock_expired
,
9188 .establish_clid
= nfs4_init_clientid
,
9191 #if defined(CONFIG_NFS_V4_1)
9192 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
9193 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9194 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9195 .recover_open
= nfs41_open_expired
,
9196 .recover_lock
= nfs41_lock_expired
,
9197 .establish_clid
= nfs41_init_clientid
,
9199 #endif /* CONFIG_NFS_V4_1 */
9201 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
9202 .sched_state_renewal
= nfs4_proc_async_renew
,
9203 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
9204 .renew_lease
= nfs4_proc_renew
,
9207 #if defined(CONFIG_NFS_V4_1)
9208 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
9209 .sched_state_renewal
= nfs41_proc_async_sequence
,
9210 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
9211 .renew_lease
= nfs4_proc_sequence
,
9215 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
9216 .get_locations
= _nfs40_proc_get_locations
,
9217 .fsid_present
= _nfs40_proc_fsid_present
,
9220 #if defined(CONFIG_NFS_V4_1)
9221 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
9222 .get_locations
= _nfs41_proc_get_locations
,
9223 .fsid_present
= _nfs41_proc_fsid_present
,
9225 #endif /* CONFIG_NFS_V4_1 */
9227 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
9229 .init_caps
= NFS_CAP_READDIRPLUS
9230 | NFS_CAP_ATOMIC_OPEN
9231 | NFS_CAP_POSIX_LOCK
,
9232 .init_client
= nfs40_init_client
,
9233 .shutdown_client
= nfs40_shutdown_client
,
9234 .match_stateid
= nfs4_match_stateid
,
9235 .find_root_sec
= nfs4_find_root_sec
,
9236 .free_lock_state
= nfs4_release_lockowner
,
9237 .test_and_free_expired
= nfs40_test_and_free_expired_stateid
,
9238 .alloc_seqid
= nfs_alloc_seqid
,
9239 .call_sync_ops
= &nfs40_call_sync_ops
,
9240 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
9241 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
9242 .state_renewal_ops
= &nfs40_state_renewal_ops
,
9243 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
9246 #if defined(CONFIG_NFS_V4_1)
9247 static struct nfs_seqid
*
9248 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
9253 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
9255 .init_caps
= NFS_CAP_READDIRPLUS
9256 | NFS_CAP_ATOMIC_OPEN
9257 | NFS_CAP_POSIX_LOCK
9258 | NFS_CAP_STATEID_NFSV41
9259 | NFS_CAP_ATOMIC_OPEN_V1
,
9260 .init_client
= nfs41_init_client
,
9261 .shutdown_client
= nfs41_shutdown_client
,
9262 .match_stateid
= nfs41_match_stateid
,
9263 .find_root_sec
= nfs41_find_root_sec
,
9264 .free_lock_state
= nfs41_free_lock_state
,
9265 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9266 .alloc_seqid
= nfs_alloc_no_seqid
,
9267 .session_trunk
= nfs4_test_session_trunk
,
9268 .call_sync_ops
= &nfs41_call_sync_ops
,
9269 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9270 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9271 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9272 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9276 #if defined(CONFIG_NFS_V4_2)
9277 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
9279 .init_caps
= NFS_CAP_READDIRPLUS
9280 | NFS_CAP_ATOMIC_OPEN
9281 | NFS_CAP_POSIX_LOCK
9282 | NFS_CAP_STATEID_NFSV41
9283 | NFS_CAP_ATOMIC_OPEN_V1
9286 | NFS_CAP_DEALLOCATE
9288 | NFS_CAP_LAYOUTSTATS
9290 .init_client
= nfs41_init_client
,
9291 .shutdown_client
= nfs41_shutdown_client
,
9292 .match_stateid
= nfs41_match_stateid
,
9293 .find_root_sec
= nfs41_find_root_sec
,
9294 .free_lock_state
= nfs41_free_lock_state
,
9295 .call_sync_ops
= &nfs41_call_sync_ops
,
9296 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9297 .alloc_seqid
= nfs_alloc_no_seqid
,
9298 .session_trunk
= nfs4_test_session_trunk
,
9299 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9300 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9301 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9302 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9306 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
9307 [0] = &nfs_v4_0_minor_ops
,
9308 #if defined(CONFIG_NFS_V4_1)
9309 [1] = &nfs_v4_1_minor_ops
,
9311 #if defined(CONFIG_NFS_V4_2)
9312 [2] = &nfs_v4_2_minor_ops
,
9316 static ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
9318 ssize_t error
, error2
;
9320 error
= generic_listxattr(dentry
, list
, size
);
9328 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
9331 return error
+ error2
;
9334 static const struct inode_operations nfs4_dir_inode_operations
= {
9335 .create
= nfs_create
,
9336 .lookup
= nfs_lookup
,
9337 .atomic_open
= nfs_atomic_open
,
9339 .unlink
= nfs_unlink
,
9340 .symlink
= nfs_symlink
,
9344 .rename
= nfs_rename
,
9345 .permission
= nfs_permission
,
9346 .getattr
= nfs_getattr
,
9347 .setattr
= nfs_setattr
,
9348 .listxattr
= nfs4_listxattr
,
9351 static const struct inode_operations nfs4_file_inode_operations
= {
9352 .permission
= nfs_permission
,
9353 .getattr
= nfs_getattr
,
9354 .setattr
= nfs_setattr
,
9355 .listxattr
= nfs4_listxattr
,
9358 const struct nfs_rpc_ops nfs_v4_clientops
= {
9359 .version
= 4, /* protocol version */
9360 .dentry_ops
= &nfs4_dentry_operations
,
9361 .dir_inode_ops
= &nfs4_dir_inode_operations
,
9362 .file_inode_ops
= &nfs4_file_inode_operations
,
9363 .file_ops
= &nfs4_file_operations
,
9364 .getroot
= nfs4_proc_get_root
,
9365 .submount
= nfs4_submount
,
9366 .try_mount
= nfs4_try_mount
,
9367 .getattr
= nfs4_proc_getattr
,
9368 .setattr
= nfs4_proc_setattr
,
9369 .lookup
= nfs4_proc_lookup
,
9370 .lookupp
= nfs4_proc_lookupp
,
9371 .access
= nfs4_proc_access
,
9372 .readlink
= nfs4_proc_readlink
,
9373 .create
= nfs4_proc_create
,
9374 .remove
= nfs4_proc_remove
,
9375 .unlink_setup
= nfs4_proc_unlink_setup
,
9376 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
9377 .unlink_done
= nfs4_proc_unlink_done
,
9378 .rename_setup
= nfs4_proc_rename_setup
,
9379 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
9380 .rename_done
= nfs4_proc_rename_done
,
9381 .link
= nfs4_proc_link
,
9382 .symlink
= nfs4_proc_symlink
,
9383 .mkdir
= nfs4_proc_mkdir
,
9384 .rmdir
= nfs4_proc_remove
,
9385 .readdir
= nfs4_proc_readdir
,
9386 .mknod
= nfs4_proc_mknod
,
9387 .statfs
= nfs4_proc_statfs
,
9388 .fsinfo
= nfs4_proc_fsinfo
,
9389 .pathconf
= nfs4_proc_pathconf
,
9390 .set_capabilities
= nfs4_server_capabilities
,
9391 .decode_dirent
= nfs4_decode_dirent
,
9392 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
9393 .read_setup
= nfs4_proc_read_setup
,
9394 .read_done
= nfs4_read_done
,
9395 .write_setup
= nfs4_proc_write_setup
,
9396 .write_done
= nfs4_write_done
,
9397 .commit_setup
= nfs4_proc_commit_setup
,
9398 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
9399 .commit_done
= nfs4_commit_done
,
9400 .lock
= nfs4_proc_lock
,
9401 .clear_acl_cache
= nfs4_zap_acl_attr
,
9402 .close_context
= nfs4_close_context
,
9403 .open_context
= nfs4_atomic_open
,
9404 .have_delegation
= nfs4_have_delegation
,
9405 .return_delegation
= nfs4_inode_return_delegation
,
9406 .alloc_client
= nfs4_alloc_client
,
9407 .init_client
= nfs4_init_client
,
9408 .free_client
= nfs4_free_client
,
9409 .create_server
= nfs4_create_server
,
9410 .clone_server
= nfs_clone_server
,
9413 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
9414 .name
= XATTR_NAME_NFSV4_ACL
,
9415 .list
= nfs4_xattr_list_nfs4_acl
,
9416 .get
= nfs4_xattr_get_nfs4_acl
,
9417 .set
= nfs4_xattr_set_nfs4_acl
,
9420 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
9421 &nfs4_xattr_nfs4_acl_handler
,
9422 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9423 &nfs4_xattr_nfs4_label_handler
,