4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
59 #include "delegation.h"
65 #include "nfs4idmap.h"
66 #include "nfs4session.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
76 /* file attributes which can be mapped to nfs attributes */
77 #define NFS4_VALID_ATTRS (ATTR_MODE \
88 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
89 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
90 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
91 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
92 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
93 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
94 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
95 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
96 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
97 struct nfs4_label
*olabel
);
98 #ifdef CONFIG_NFS_V4_1
99 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
101 static int nfs41_free_stateid(struct nfs_server
*, const nfs4_stateid
*,
102 struct rpc_cred
*, bool);
105 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
106 static inline struct nfs4_label
*
107 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
108 struct iattr
*sattr
, struct nfs4_label
*label
)
115 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
118 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
119 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
126 nfs4_label_release_security(struct nfs4_label
*label
)
129 security_release_secctx(label
->label
, label
->len
);
131 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
134 return server
->attr_bitmask
;
136 return server
->attr_bitmask_nl
;
139 static inline struct nfs4_label
*
140 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
141 struct iattr
*sattr
, struct nfs4_label
*l
)
144 nfs4_label_release_security(struct nfs4_label
*label
)
147 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
148 { return server
->attr_bitmask
; }
151 /* Prevent leaks of NFSv4 errors into userland */
152 static int nfs4_map_errors(int err
)
157 case -NFS4ERR_RESOURCE
:
158 case -NFS4ERR_LAYOUTTRYLATER
:
159 case -NFS4ERR_RECALLCONFLICT
:
161 case -NFS4ERR_WRONGSEC
:
162 case -NFS4ERR_WRONG_CRED
:
164 case -NFS4ERR_BADOWNER
:
165 case -NFS4ERR_BADNAME
:
167 case -NFS4ERR_SHARE_DENIED
:
169 case -NFS4ERR_MINOR_VERS_MISMATCH
:
170 return -EPROTONOSUPPORT
;
171 case -NFS4ERR_FILE_OPEN
:
174 dprintk("%s could not handle NFSv4 error %d\n",
182 * This is our standard bitmap for GETATTR requests.
184 const u32 nfs4_fattr_bitmap
[3] = {
186 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_FILEID
,
191 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
200 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
201 FATTR4_WORD2_SECURITY_LABEL
205 static const u32 nfs4_pnfs_open_bitmap
[3] = {
207 | FATTR4_WORD0_CHANGE
210 | FATTR4_WORD0_FILEID
,
212 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER_GROUP
215 | FATTR4_WORD1_RAWDEV
216 | FATTR4_WORD1_SPACE_USED
217 | FATTR4_WORD1_TIME_ACCESS
218 | FATTR4_WORD1_TIME_METADATA
219 | FATTR4_WORD1_TIME_MODIFY
,
220 FATTR4_WORD2_MDSTHRESHOLD
221 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
222 | FATTR4_WORD2_SECURITY_LABEL
226 static const u32 nfs4_open_noattr_bitmap
[3] = {
228 | FATTR4_WORD0_FILEID
,
231 const u32 nfs4_statfs_bitmap
[3] = {
232 FATTR4_WORD0_FILES_AVAIL
233 | FATTR4_WORD0_FILES_FREE
234 | FATTR4_WORD0_FILES_TOTAL
,
235 FATTR4_WORD1_SPACE_AVAIL
236 | FATTR4_WORD1_SPACE_FREE
237 | FATTR4_WORD1_SPACE_TOTAL
240 const u32 nfs4_pathconf_bitmap
[3] = {
242 | FATTR4_WORD0_MAXNAME
,
246 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
247 | FATTR4_WORD0_MAXREAD
248 | FATTR4_WORD0_MAXWRITE
249 | FATTR4_WORD0_LEASE_TIME
,
250 FATTR4_WORD1_TIME_DELTA
251 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
252 FATTR4_WORD2_LAYOUT_BLKSIZE
253 | FATTR4_WORD2_CLONE_BLKSIZE
256 const u32 nfs4_fs_locations_bitmap
[3] = {
258 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_FILEID
262 | FATTR4_WORD0_FS_LOCATIONS
,
264 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER_GROUP
267 | FATTR4_WORD1_RAWDEV
268 | FATTR4_WORD1_SPACE_USED
269 | FATTR4_WORD1_TIME_ACCESS
270 | FATTR4_WORD1_TIME_METADATA
271 | FATTR4_WORD1_TIME_MODIFY
272 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
275 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
276 struct nfs4_readdir_arg
*readdir
)
281 readdir
->cookie
= cookie
;
282 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
287 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
292 * NFSv4 servers do not return entries for '.' and '..'
293 * Therefore, we fake these entries here. We let '.'
294 * have cookie 0 and '..' have cookie 1. Note that
295 * when talking to the server, we always send cookie 0
298 start
= p
= kmap_atomic(*readdir
->pages
);
301 *p
++ = xdr_one
; /* next */
302 *p
++ = xdr_zero
; /* cookie, first word */
303 *p
++ = xdr_one
; /* cookie, second word */
304 *p
++ = xdr_one
; /* entry len */
305 memcpy(p
, ".\0\0\0", 4); /* entry */
307 *p
++ = xdr_one
; /* bitmap length */
308 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
309 *p
++ = htonl(8); /* attribute buffer length */
310 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
)));
313 *p
++ = xdr_one
; /* next */
314 *p
++ = xdr_zero
; /* cookie, first word */
315 *p
++ = xdr_two
; /* cookie, second word */
316 *p
++ = xdr_two
; /* entry len */
317 memcpy(p
, "..\0\0", 4); /* entry */
319 *p
++ = xdr_one
; /* bitmap length */
320 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
321 *p
++ = htonl(8); /* attribute buffer length */
322 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
324 readdir
->pgbase
= (char *)p
- (char *)start
;
325 readdir
->count
-= readdir
->pgbase
;
326 kunmap_atomic(start
);
329 static void nfs4_test_and_free_stateid(struct nfs_server
*server
,
330 nfs4_stateid
*stateid
,
331 struct rpc_cred
*cred
)
333 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
335 ops
->test_and_free_expired(server
, stateid
, cred
);
338 static void __nfs4_free_revoked_stateid(struct nfs_server
*server
,
339 nfs4_stateid
*stateid
,
340 struct rpc_cred
*cred
)
342 stateid
->type
= NFS4_REVOKED_STATEID_TYPE
;
343 nfs4_test_and_free_stateid(server
, stateid
, cred
);
346 static void nfs4_free_revoked_stateid(struct nfs_server
*server
,
347 const nfs4_stateid
*stateid
,
348 struct rpc_cred
*cred
)
352 nfs4_stateid_copy(&tmp
, stateid
);
353 __nfs4_free_revoked_stateid(server
, &tmp
, cred
);
356 static long nfs4_update_delay(long *timeout
)
360 return NFS4_POLL_RETRY_MAX
;
362 *timeout
= NFS4_POLL_RETRY_MIN
;
363 if (*timeout
> NFS4_POLL_RETRY_MAX
)
364 *timeout
= NFS4_POLL_RETRY_MAX
;
370 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
376 freezable_schedule_timeout_killable_unsafe(
377 nfs4_update_delay(timeout
));
378 if (fatal_signal_pending(current
))
383 /* This is the error handling routine for processes that are allowed
386 static int nfs4_do_handle_exception(struct nfs_server
*server
,
387 int errorcode
, struct nfs4_exception
*exception
)
389 struct nfs_client
*clp
= server
->nfs_client
;
390 struct nfs4_state
*state
= exception
->state
;
391 const nfs4_stateid
*stateid
= exception
->stateid
;
392 struct inode
*inode
= exception
->inode
;
395 exception
->delay
= 0;
396 exception
->recovering
= 0;
397 exception
->retry
= 0;
399 if (stateid
== NULL
&& state
!= NULL
)
400 stateid
= &state
->stateid
;
405 case -NFS4ERR_DELEG_REVOKED
:
406 case -NFS4ERR_ADMIN_REVOKED
:
407 case -NFS4ERR_EXPIRED
:
408 case -NFS4ERR_BAD_STATEID
:
409 if (inode
!= NULL
&& stateid
!= NULL
) {
410 nfs_inode_find_state_and_recover(inode
,
412 goto wait_on_recovery
;
414 case -NFS4ERR_OPENMODE
:
418 err
= nfs_async_inode_return_delegation(inode
,
421 goto wait_on_recovery
;
422 if (stateid
!= NULL
&& stateid
->type
== NFS4_DELEGATION_STATEID_TYPE
) {
423 exception
->retry
= 1;
429 ret
= nfs4_schedule_stateid_recovery(server
, state
);
432 goto wait_on_recovery
;
433 case -NFS4ERR_STALE_STATEID
:
434 case -NFS4ERR_STALE_CLIENTID
:
435 nfs4_schedule_lease_recovery(clp
);
436 goto wait_on_recovery
;
438 ret
= nfs4_schedule_migration_recovery(server
);
441 goto wait_on_recovery
;
442 case -NFS4ERR_LEASE_MOVED
:
443 nfs4_schedule_lease_moved_recovery(clp
);
444 goto wait_on_recovery
;
445 #if defined(CONFIG_NFS_V4_1)
446 case -NFS4ERR_BADSESSION
:
447 case -NFS4ERR_BADSLOT
:
448 case -NFS4ERR_BAD_HIGH_SLOT
:
449 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
450 case -NFS4ERR_DEADSESSION
:
451 case -NFS4ERR_SEQ_FALSE_RETRY
:
452 case -NFS4ERR_SEQ_MISORDERED
:
453 dprintk("%s ERROR: %d Reset session\n", __func__
,
455 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
456 goto wait_on_recovery
;
457 #endif /* defined(CONFIG_NFS_V4_1) */
458 case -NFS4ERR_FILE_OPEN
:
459 if (exception
->timeout
> HZ
) {
460 /* We have retried a decent amount, time to
467 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
469 case -NFS4ERR_LAYOUTTRYLATER
:
470 case -NFS4ERR_RECALLCONFLICT
:
471 exception
->delay
= 1;
474 case -NFS4ERR_RETRY_UNCACHED_REP
:
475 case -NFS4ERR_OLD_STATEID
:
476 exception
->retry
= 1;
478 case -NFS4ERR_BADOWNER
:
479 /* The following works around a Linux server bug! */
480 case -NFS4ERR_BADNAME
:
481 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
482 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
483 exception
->retry
= 1;
484 printk(KERN_WARNING
"NFS: v4 server %s "
485 "does not accept raw "
487 "Reenabling the idmapper.\n",
488 server
->nfs_client
->cl_hostname
);
491 /* We failed to handle the error */
492 return nfs4_map_errors(ret
);
494 exception
->recovering
= 1;
498 /* This is the error handling routine for processes that are allowed
501 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
503 struct nfs_client
*clp
= server
->nfs_client
;
506 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
507 if (exception
->delay
) {
508 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
511 if (exception
->recovering
) {
512 ret
= nfs4_wait_clnt_recover(clp
);
513 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
520 exception
->retry
= 1;
525 nfs4_async_handle_exception(struct rpc_task
*task
, struct nfs_server
*server
,
526 int errorcode
, struct nfs4_exception
*exception
)
528 struct nfs_client
*clp
= server
->nfs_client
;
531 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
532 if (exception
->delay
) {
533 rpc_delay(task
, nfs4_update_delay(&exception
->timeout
));
536 if (exception
->recovering
) {
537 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
538 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
539 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
542 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
547 exception
->retry
= 1;
552 nfs4_async_handle_error(struct rpc_task
*task
, struct nfs_server
*server
,
553 struct nfs4_state
*state
, long *timeout
)
555 struct nfs4_exception exception
= {
559 if (task
->tk_status
>= 0)
562 exception
.timeout
= *timeout
;
563 task
->tk_status
= nfs4_async_handle_exception(task
, server
,
566 if (exception
.delay
&& timeout
)
567 *timeout
= exception
.timeout
;
574 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
575 * or 'false' otherwise.
577 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
579 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
580 return (flavor
== RPC_AUTH_GSS_KRB5I
) || (flavor
== RPC_AUTH_GSS_KRB5P
);
583 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
585 spin_lock(&clp
->cl_lock
);
586 if (time_before(clp
->cl_last_renewal
,timestamp
))
587 clp
->cl_last_renewal
= timestamp
;
588 spin_unlock(&clp
->cl_lock
);
591 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
593 struct nfs_client
*clp
= server
->nfs_client
;
595 if (!nfs4_has_session(clp
))
596 do_renew_lease(clp
, timestamp
);
599 struct nfs4_call_sync_data
{
600 const struct nfs_server
*seq_server
;
601 struct nfs4_sequence_args
*seq_args
;
602 struct nfs4_sequence_res
*seq_res
;
605 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
606 struct nfs4_sequence_res
*res
, int cache_reply
)
608 args
->sa_slot
= NULL
;
609 args
->sa_cache_this
= cache_reply
;
610 args
->sa_privileged
= 0;
615 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
617 args
->sa_privileged
= 1;
620 static void nfs40_sequence_free_slot(struct nfs4_sequence_res
*res
)
622 struct nfs4_slot
*slot
= res
->sr_slot
;
623 struct nfs4_slot_table
*tbl
;
626 spin_lock(&tbl
->slot_tbl_lock
);
627 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
628 nfs4_free_slot(tbl
, slot
);
629 spin_unlock(&tbl
->slot_tbl_lock
);
634 static int nfs40_sequence_done(struct rpc_task
*task
,
635 struct nfs4_sequence_res
*res
)
637 if (res
->sr_slot
!= NULL
)
638 nfs40_sequence_free_slot(res
);
642 #if defined(CONFIG_NFS_V4_1)
644 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
646 struct nfs4_session
*session
;
647 struct nfs4_slot_table
*tbl
;
648 struct nfs4_slot
*slot
= res
->sr_slot
;
649 bool send_new_highest_used_slotid
= false;
652 session
= tbl
->session
;
654 /* Bump the slot sequence number */
659 spin_lock(&tbl
->slot_tbl_lock
);
660 /* Be nice to the server: try to ensure that the last transmitted
661 * value for highest_user_slotid <= target_highest_slotid
663 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
664 send_new_highest_used_slotid
= true;
666 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
667 send_new_highest_used_slotid
= false;
670 nfs4_free_slot(tbl
, slot
);
672 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
673 send_new_highest_used_slotid
= false;
675 spin_unlock(&tbl
->slot_tbl_lock
);
677 if (send_new_highest_used_slotid
)
678 nfs41_notify_server(session
->clp
);
679 if (waitqueue_active(&tbl
->slot_waitq
))
680 wake_up_all(&tbl
->slot_waitq
);
683 static int nfs41_sequence_process(struct rpc_task
*task
,
684 struct nfs4_sequence_res
*res
)
686 struct nfs4_session
*session
;
687 struct nfs4_slot
*slot
= res
->sr_slot
;
688 struct nfs_client
*clp
;
689 bool interrupted
= false;
694 /* don't increment the sequence number if the task wasn't sent */
695 if (!RPC_WAS_SENT(task
))
698 session
= slot
->table
->session
;
700 if (slot
->interrupted
) {
701 slot
->interrupted
= 0;
705 trace_nfs4_sequence_done(session
, res
);
706 /* Check the SEQUENCE operation status */
707 switch (res
->sr_status
) {
709 /* If previous op on slot was interrupted and we reused
710 * the seq# and got a reply from the cache, then retry
712 if (task
->tk_status
== -EREMOTEIO
&& interrupted
) {
716 /* Update the slot's sequence and clientid lease timer */
719 do_renew_lease(clp
, res
->sr_timestamp
);
720 /* Check sequence flags */
721 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
,
723 nfs41_update_target_slotid(slot
->table
, slot
, res
);
727 * sr_status remains 1 if an RPC level error occurred.
728 * The server may or may not have processed the sequence
730 * Mark the slot as having hosted an interrupted RPC call.
732 slot
->interrupted
= 1;
735 /* The server detected a resend of the RPC call and
736 * returned NFS4ERR_DELAY as per Section 2.10.6.2
739 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
744 case -NFS4ERR_BADSLOT
:
746 * The slot id we used was probably retired. Try again
747 * using a different slot id.
750 case -NFS4ERR_SEQ_MISORDERED
:
752 * Was the last operation on this sequence interrupted?
753 * If so, retry after bumping the sequence number.
760 * Could this slot have been previously retired?
761 * If so, then the server may be expecting seq_nr = 1!
763 if (slot
->seq_nr
!= 1) {
768 case -NFS4ERR_SEQ_FALSE_RETRY
:
771 case -NFS4ERR_DEADSESSION
:
772 case -NFS4ERR_BADSESSION
:
773 nfs4_schedule_session_recovery(session
, res
->sr_status
);
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
;
1040 unsigned int rpc_done
: 1;
1041 unsigned int file_created
: 1;
1042 unsigned int is_recover
: 1;
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
);
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
== 0)
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
);
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
= 1;
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);
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
== 0)
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
);
2186 data
->rpc_status
= 0;
2187 data
->cancelled
= 0;
2188 data
->is_recover
= 0;
2190 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2191 data
->is_recover
= 1;
2193 task
= rpc_run_task(&task_setup_data
);
2195 return PTR_ERR(task
);
2196 status
= rpc_wait_for_completion_task(task
);
2198 data
->cancelled
= 1;
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 */
2254 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2258 cache
.jiffies
= jiffies
;
2259 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2260 nfs_access_add_cache(state
->inode
, &cache
);
2262 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
2265 /* even though OPEN succeeded, access is denied. Close the file */
2266 nfs4_close_state(state
, fmode
);
2271 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2273 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2275 struct inode
*dir
= d_inode(data
->dir
);
2276 struct nfs_server
*server
= NFS_SERVER(dir
);
2277 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2278 struct nfs_openres
*o_res
= &data
->o_res
;
2281 status
= nfs4_run_open_task(data
, 0);
2282 if (!data
->rpc_done
)
2285 if (status
== -NFS4ERR_BADNAME
&&
2286 !(o_arg
->open_flags
& O_CREAT
))
2291 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2293 if (o_arg
->open_flags
& O_CREAT
) {
2294 if (o_arg
->open_flags
& O_EXCL
)
2295 data
->file_created
= 1;
2296 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2297 data
->file_created
= 1;
2298 if (data
->file_created
|| dir
->i_version
!= o_res
->cinfo
.after
)
2299 update_changeattr(dir
, &o_res
->cinfo
,
2300 o_res
->f_attr
->time_start
);
2302 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2303 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2304 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2305 status
= _nfs4_proc_open_confirm(data
);
2309 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
2310 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2314 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
2316 return nfs4_client_recover_expired_lease(server
->nfs_client
);
2321 * reclaim state on the server after a network partition.
2322 * Assumes caller holds the appropriate lock
2324 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2326 struct nfs4_opendata
*opendata
;
2329 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2330 NFS4_OPEN_CLAIM_FH
);
2331 if (IS_ERR(opendata
))
2332 return PTR_ERR(opendata
);
2333 ret
= nfs4_open_recover(opendata
, state
);
2335 d_drop(ctx
->dentry
);
2336 nfs4_opendata_put(opendata
);
2340 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2342 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2343 struct nfs4_exception exception
= { };
2347 err
= _nfs4_open_expired(ctx
, state
);
2348 trace_nfs4_open_expired(ctx
, 0, err
);
2349 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2354 case -NFS4ERR_GRACE
:
2355 case -NFS4ERR_DELAY
:
2356 nfs4_handle_exception(server
, err
, &exception
);
2359 } while (exception
.retry
);
2364 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2366 struct nfs_open_context
*ctx
;
2369 ctx
= nfs4_state_find_open_context(state
);
2372 ret
= nfs4_do_open_expired(ctx
, state
);
2373 put_nfs_open_context(ctx
);
2377 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
,
2378 const nfs4_stateid
*stateid
)
2380 nfs_remove_bad_delegation(state
->inode
, stateid
);
2381 write_seqlock(&state
->seqlock
);
2382 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2383 write_sequnlock(&state
->seqlock
);
2384 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2387 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2389 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2390 nfs_finish_clear_delegation_stateid(state
, NULL
);
2393 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2395 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2396 nfs40_clear_delegation_stateid(state
);
2397 return nfs4_open_expired(sp
, state
);
2400 static int nfs40_test_and_free_expired_stateid(struct nfs_server
*server
,
2401 nfs4_stateid
*stateid
,
2402 struct rpc_cred
*cred
)
2404 return -NFS4ERR_BAD_STATEID
;
2407 #if defined(CONFIG_NFS_V4_1)
2408 static int nfs41_test_and_free_expired_stateid(struct nfs_server
*server
,
2409 nfs4_stateid
*stateid
,
2410 struct rpc_cred
*cred
)
2414 switch (stateid
->type
) {
2417 case NFS4_INVALID_STATEID_TYPE
:
2418 case NFS4_SPECIAL_STATEID_TYPE
:
2419 return -NFS4ERR_BAD_STATEID
;
2420 case NFS4_REVOKED_STATEID_TYPE
:
2424 status
= nfs41_test_stateid(server
, stateid
, cred
);
2426 case -NFS4ERR_EXPIRED
:
2427 case -NFS4ERR_ADMIN_REVOKED
:
2428 case -NFS4ERR_DELEG_REVOKED
:
2434 /* Ack the revoked state to the server */
2435 nfs41_free_stateid(server
, stateid
, cred
, true);
2436 return -NFS4ERR_EXPIRED
;
2439 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2441 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2442 nfs4_stateid stateid
;
2443 struct nfs_delegation
*delegation
;
2444 struct rpc_cred
*cred
;
2447 /* Get the delegation credential for use by test/free_stateid */
2449 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2450 if (delegation
== NULL
) {
2455 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2456 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
2458 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2462 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
)) {
2467 cred
= get_rpccred(delegation
->cred
);
2469 status
= nfs41_test_and_free_expired_stateid(server
, &stateid
, cred
);
2470 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2471 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
2472 nfs_finish_clear_delegation_stateid(state
, &stateid
);
2478 * nfs41_check_expired_locks - possibly free a lock stateid
2480 * @state: NFSv4 state for an inode
2482 * Returns NFS_OK if recovery for this stateid is now finished.
2483 * Otherwise a negative NFS4ERR value is returned.
2485 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
2487 int status
, ret
= NFS_OK
;
2488 struct nfs4_lock_state
*lsp
, *prev
= NULL
;
2489 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2491 if (!test_bit(LK_STATE_IN_USE
, &state
->flags
))
2494 spin_lock(&state
->state_lock
);
2495 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
2496 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
2497 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
2499 atomic_inc(&lsp
->ls_count
);
2500 spin_unlock(&state
->state_lock
);
2502 nfs4_put_lock_state(prev
);
2505 status
= nfs41_test_and_free_expired_stateid(server
,
2508 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
2509 if (status
== -NFS4ERR_EXPIRED
||
2510 status
== -NFS4ERR_BAD_STATEID
) {
2511 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
2512 lsp
->ls_stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
2513 if (!recover_lost_locks
)
2514 set_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
);
2515 } else if (status
!= NFS_OK
) {
2517 nfs4_put_lock_state(prev
);
2520 spin_lock(&state
->state_lock
);
2523 spin_unlock(&state
->state_lock
);
2524 nfs4_put_lock_state(prev
);
2530 * nfs41_check_open_stateid - possibly free an open stateid
2532 * @state: NFSv4 state for an inode
2534 * Returns NFS_OK if recovery for this stateid is now finished.
2535 * Otherwise a negative NFS4ERR value is returned.
2537 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2539 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2540 nfs4_stateid
*stateid
= &state
->open_stateid
;
2541 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2544 if (test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0) {
2545 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0) {
2546 if (nfs4_have_delegation(state
->inode
, state
->state
))
2548 return -NFS4ERR_OPENMODE
;
2550 return -NFS4ERR_BAD_STATEID
;
2552 status
= nfs41_test_and_free_expired_stateid(server
, stateid
, cred
);
2553 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2554 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
) {
2555 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2556 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2557 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2558 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2559 stateid
->type
= NFS4_INVALID_STATEID_TYPE
;
2561 if (status
!= NFS_OK
)
2563 if (nfs_open_stateid_recover_openmode(state
))
2564 return -NFS4ERR_OPENMODE
;
2568 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2572 nfs41_check_delegation_stateid(state
);
2573 status
= nfs41_check_expired_locks(state
);
2574 if (status
!= NFS_OK
)
2576 status
= nfs41_check_open_stateid(state
);
2577 if (status
!= NFS_OK
)
2578 status
= nfs4_open_expired(sp
, state
);
2584 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2585 * fields corresponding to attributes that were used to store the verifier.
2586 * Make sure we clobber those fields in the later setattr call
2588 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2589 struct iattr
*sattr
, struct nfs4_label
**label
)
2591 const u32
*attrset
= opendata
->o_res
.attrset
;
2593 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2594 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2595 sattr
->ia_valid
|= ATTR_ATIME
;
2597 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2598 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2599 sattr
->ia_valid
|= ATTR_MTIME
;
2601 /* Except MODE, it seems harmless of setting twice. */
2602 if (opendata
->o_arg
.createmode
!= NFS4_CREATE_EXCLUSIVE
&&
2603 attrset
[1] & FATTR4_WORD1_MODE
)
2604 sattr
->ia_valid
&= ~ATTR_MODE
;
2606 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2610 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2613 struct nfs_open_context
*ctx
)
2615 struct nfs4_state_owner
*sp
= opendata
->owner
;
2616 struct nfs_server
*server
= sp
->so_server
;
2617 struct dentry
*dentry
;
2618 struct nfs4_state
*state
;
2622 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2624 ret
= _nfs4_proc_open(opendata
);
2628 state
= nfs4_opendata_to_nfs4_state(opendata
);
2629 ret
= PTR_ERR(state
);
2632 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2633 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2634 if (opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
)
2635 set_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
);
2637 dentry
= opendata
->dentry
;
2638 if (d_really_is_negative(dentry
)) {
2639 struct dentry
*alias
;
2641 alias
= d_exact_alias(dentry
, state
->inode
);
2643 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2644 /* d_splice_alias() can't fail here - it's a non-directory */
2647 ctx
->dentry
= dentry
= alias
;
2649 nfs_set_verifier(dentry
,
2650 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2653 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2658 if (d_inode(dentry
) == state
->inode
) {
2659 nfs_inode_attach_open_context(ctx
);
2660 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2661 nfs4_schedule_stateid_recovery(server
, state
);
2668 * Returns a referenced nfs4_state
2670 static int _nfs4_do_open(struct inode
*dir
,
2671 struct nfs_open_context
*ctx
,
2673 struct iattr
*sattr
,
2674 struct nfs4_label
*label
,
2677 struct nfs4_state_owner
*sp
;
2678 struct nfs4_state
*state
= NULL
;
2679 struct nfs_server
*server
= NFS_SERVER(dir
);
2680 struct nfs4_opendata
*opendata
;
2681 struct dentry
*dentry
= ctx
->dentry
;
2682 struct rpc_cred
*cred
= ctx
->cred
;
2683 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2684 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2685 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2686 struct nfs4_label
*olabel
= NULL
;
2689 /* Protect against reboot recovery conflicts */
2691 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2693 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2696 status
= nfs4_recover_expired_lease(server
);
2698 goto err_put_state_owner
;
2699 if (d_really_is_positive(dentry
))
2700 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2702 if (d_really_is_positive(dentry
))
2703 claim
= NFS4_OPEN_CLAIM_FH
;
2704 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2705 label
, claim
, GFP_KERNEL
);
2706 if (opendata
== NULL
)
2707 goto err_put_state_owner
;
2710 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2711 if (IS_ERR(olabel
)) {
2712 status
= PTR_ERR(olabel
);
2713 goto err_opendata_put
;
2717 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2718 if (!opendata
->f_attr
.mdsthreshold
) {
2719 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2720 if (!opendata
->f_attr
.mdsthreshold
)
2721 goto err_free_label
;
2723 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2725 if (d_really_is_positive(dentry
))
2726 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2728 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2730 goto err_free_label
;
2733 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2734 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2735 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2737 * send create attributes which was not set by open
2738 * with an extra setattr.
2740 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2741 nfs_fattr_init(opendata
->o_res
.f_attr
);
2742 status
= nfs4_do_setattr(state
->inode
, cred
,
2743 opendata
->o_res
.f_attr
, sattr
,
2744 ctx
, label
, olabel
);
2746 nfs_setattr_update_inode(state
->inode
, sattr
,
2747 opendata
->o_res
.f_attr
);
2748 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2752 if (opened
&& opendata
->file_created
)
2753 *opened
|= FILE_CREATED
;
2755 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2756 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2757 opendata
->f_attr
.mdsthreshold
= NULL
;
2760 nfs4_label_free(olabel
);
2762 nfs4_opendata_put(opendata
);
2763 nfs4_put_state_owner(sp
);
2766 nfs4_label_free(olabel
);
2768 nfs4_opendata_put(opendata
);
2769 err_put_state_owner
:
2770 nfs4_put_state_owner(sp
);
2776 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2777 struct nfs_open_context
*ctx
,
2779 struct iattr
*sattr
,
2780 struct nfs4_label
*label
,
2783 struct nfs_server
*server
= NFS_SERVER(dir
);
2784 struct nfs4_exception exception
= { };
2785 struct nfs4_state
*res
;
2789 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2791 trace_nfs4_open_file(ctx
, flags
, status
);
2794 /* NOTE: BAD_SEQID means the server and client disagree about the
2795 * book-keeping w.r.t. state-changing operations
2796 * (OPEN/CLOSE/LOCK/LOCKU...)
2797 * It is actually a sign of a bug on the client or on the server.
2799 * If we receive a BAD_SEQID error in the particular case of
2800 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2801 * have unhashed the old state_owner for us, and that we can
2802 * therefore safely retry using a new one. We should still warn
2803 * the user though...
2805 if (status
== -NFS4ERR_BAD_SEQID
) {
2806 pr_warn_ratelimited("NFS: v4 server %s "
2807 " returned a bad sequence-id error!\n",
2808 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2809 exception
.retry
= 1;
2813 * BAD_STATEID on OPEN means that the server cancelled our
2814 * state before it received the OPEN_CONFIRM.
2815 * Recover by retrying the request as per the discussion
2816 * on Page 181 of RFC3530.
2818 if (status
== -NFS4ERR_BAD_STATEID
) {
2819 exception
.retry
= 1;
2822 if (status
== -EAGAIN
) {
2823 /* We must have found a delegation */
2824 exception
.retry
= 1;
2827 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2829 res
= ERR_PTR(nfs4_handle_exception(server
,
2830 status
, &exception
));
2831 } while (exception
.retry
);
2835 static int _nfs4_do_setattr(struct inode
*inode
,
2836 struct nfs_setattrargs
*arg
,
2837 struct nfs_setattrres
*res
,
2838 struct rpc_cred
*cred
,
2839 struct nfs_open_context
*ctx
)
2841 struct nfs_server
*server
= NFS_SERVER(inode
);
2842 struct rpc_message msg
= {
2843 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2848 struct rpc_cred
*delegation_cred
= NULL
;
2849 unsigned long timestamp
= jiffies
;
2854 nfs_fattr_init(res
->fattr
);
2856 /* Servers should only apply open mode checks for file size changes */
2857 truncate
= (arg
->iap
->ia_valid
& ATTR_SIZE
) ? true : false;
2858 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2860 if (nfs4_copy_delegation_stateid(inode
, fmode
, &arg
->stateid
, &delegation_cred
)) {
2861 /* Use that stateid */
2862 } else if (truncate
&& ctx
!= NULL
) {
2863 struct nfs_lock_context
*l_ctx
;
2864 if (!nfs4_valid_open_stateid(ctx
->state
))
2866 l_ctx
= nfs_get_lock_context(ctx
);
2868 return PTR_ERR(l_ctx
);
2869 status
= nfs4_select_rw_stateid(ctx
->state
, FMODE_WRITE
, l_ctx
,
2870 &arg
->stateid
, &delegation_cred
);
2871 nfs_put_lock_context(l_ctx
);
2875 nfs4_stateid_copy(&arg
->stateid
, &zero_stateid
);
2876 if (delegation_cred
)
2877 msg
.rpc_cred
= delegation_cred
;
2879 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
->seq_args
, &res
->seq_res
, 1);
2881 put_rpccred(delegation_cred
);
2882 if (status
== 0 && ctx
!= NULL
)
2883 renew_lease(server
, timestamp
);
2884 trace_nfs4_setattr(inode
, &arg
->stateid
, status
);
2888 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2889 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2890 struct nfs_open_context
*ctx
, struct nfs4_label
*ilabel
,
2891 struct nfs4_label
*olabel
)
2893 struct nfs_server
*server
= NFS_SERVER(inode
);
2894 struct nfs4_state
*state
= ctx
? ctx
->state
: NULL
;
2895 struct nfs_setattrargs arg
= {
2896 .fh
= NFS_FH(inode
),
2899 .bitmask
= server
->attr_bitmask
,
2902 struct nfs_setattrres res
= {
2907 struct nfs4_exception exception
= {
2910 .stateid
= &arg
.stateid
,
2914 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2916 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2919 err
= _nfs4_do_setattr(inode
, &arg
, &res
, cred
, ctx
);
2921 case -NFS4ERR_OPENMODE
:
2922 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2923 pr_warn_once("NFSv4: server %s is incorrectly "
2924 "applying open mode checks to "
2925 "a SETATTR that is not "
2926 "changing file size.\n",
2927 server
->nfs_client
->cl_hostname
);
2929 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2931 if (sattr
->ia_valid
& ATTR_OPEN
)
2936 err
= nfs4_handle_exception(server
, err
, &exception
);
2937 } while (exception
.retry
);
2943 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
2945 if (inode
== NULL
|| !nfs_have_layout(inode
))
2948 return pnfs_wait_on_layoutreturn(inode
, task
);
2951 struct nfs4_closedata
{
2952 struct inode
*inode
;
2953 struct nfs4_state
*state
;
2954 struct nfs_closeargs arg
;
2955 struct nfs_closeres res
;
2957 struct nfs4_layoutreturn_args arg
;
2958 struct nfs4_layoutreturn_res res
;
2959 struct nfs4_xdr_opaque_data ld_private
;
2963 struct nfs_fattr fattr
;
2964 unsigned long timestamp
;
2967 static void nfs4_free_closedata(void *data
)
2969 struct nfs4_closedata
*calldata
= data
;
2970 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2971 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2973 if (calldata
->lr
.roc
)
2974 pnfs_roc_release(&calldata
->lr
.arg
, &calldata
->lr
.res
,
2975 calldata
->res
.lr_ret
);
2976 nfs4_put_open_state(calldata
->state
);
2977 nfs_free_seqid(calldata
->arg
.seqid
);
2978 nfs4_put_state_owner(sp
);
2979 nfs_sb_deactive(sb
);
2983 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2985 struct nfs4_closedata
*calldata
= data
;
2986 struct nfs4_state
*state
= calldata
->state
;
2987 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2988 nfs4_stateid
*res_stateid
= NULL
;
2990 dprintk("%s: begin!\n", __func__
);
2991 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2993 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
2995 /* Handle Layoutreturn errors */
2996 if (calldata
->arg
.lr_args
&& task
->tk_status
!= 0) {
2997 switch (calldata
->res
.lr_ret
) {
2999 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3002 calldata
->arg
.lr_args
= NULL
;
3003 calldata
->res
.lr_res
= NULL
;
3005 case -NFS4ERR_ADMIN_REVOKED
:
3006 case -NFS4ERR_DELEG_REVOKED
:
3007 case -NFS4ERR_EXPIRED
:
3008 case -NFS4ERR_BAD_STATEID
:
3009 case -NFS4ERR_OLD_STATEID
:
3010 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
3011 case -NFS4ERR_WRONG_CRED
:
3012 calldata
->arg
.lr_args
= NULL
;
3013 calldata
->res
.lr_res
= NULL
;
3014 calldata
->res
.lr_ret
= 0;
3015 rpc_restart_call_prepare(task
);
3020 /* hmm. we are done with the inode, and in the process of freeing
3021 * the state_owner. we keep this around to process errors
3023 switch (task
->tk_status
) {
3025 res_stateid
= &calldata
->res
.stateid
;
3026 renew_lease(server
, calldata
->timestamp
);
3028 case -NFS4ERR_ACCESS
:
3029 if (calldata
->arg
.bitmask
!= NULL
) {
3030 calldata
->arg
.bitmask
= NULL
;
3031 calldata
->res
.fattr
= NULL
;
3032 task
->tk_status
= 0;
3033 rpc_restart_call_prepare(task
);
3038 case -NFS4ERR_ADMIN_REVOKED
:
3039 case -NFS4ERR_STALE_STATEID
:
3040 case -NFS4ERR_EXPIRED
:
3041 nfs4_free_revoked_stateid(server
,
3042 &calldata
->arg
.stateid
,
3043 task
->tk_msg
.rpc_cred
);
3044 case -NFS4ERR_OLD_STATEID
:
3045 case -NFS4ERR_BAD_STATEID
:
3046 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
3047 &state
->open_stateid
)) {
3048 rpc_restart_call_prepare(task
);
3051 if (calldata
->arg
.fmode
== 0)
3054 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
3055 rpc_restart_call_prepare(task
);
3059 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
3060 res_stateid
, calldata
->arg
.fmode
);
3062 nfs_release_seqid(calldata
->arg
.seqid
);
3063 nfs_refresh_inode(calldata
->inode
, &calldata
->fattr
);
3064 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
3067 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
3069 struct nfs4_closedata
*calldata
= data
;
3070 struct nfs4_state
*state
= calldata
->state
;
3071 struct inode
*inode
= calldata
->inode
;
3072 bool is_rdonly
, is_wronly
, is_rdwr
;
3075 dprintk("%s: begin!\n", __func__
);
3076 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3079 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
3080 spin_lock(&state
->owner
->so_lock
);
3081 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
3082 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
3083 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
3084 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
3085 /* Calculate the change in open mode */
3086 calldata
->arg
.fmode
= 0;
3087 if (state
->n_rdwr
== 0) {
3088 if (state
->n_rdonly
== 0)
3089 call_close
|= is_rdonly
;
3091 calldata
->arg
.fmode
|= FMODE_READ
;
3092 if (state
->n_wronly
== 0)
3093 call_close
|= is_wronly
;
3095 calldata
->arg
.fmode
|= FMODE_WRITE
;
3096 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
3097 call_close
|= is_rdwr
;
3099 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
3101 if (!nfs4_valid_open_stateid(state
) ||
3102 test_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
3104 spin_unlock(&state
->owner
->so_lock
);
3107 /* Note: exit _without_ calling nfs4_close_done */
3111 if (!calldata
->lr
.roc
&& nfs4_wait_on_layoutreturn(inode
, task
)) {
3112 nfs_release_seqid(calldata
->arg
.seqid
);
3116 if (calldata
->arg
.fmode
== 0)
3117 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
3119 if (calldata
->arg
.fmode
== 0 || calldata
->arg
.fmode
== FMODE_READ
) {
3120 /* Close-to-open cache consistency revalidation */
3121 if (!nfs4_have_delegation(inode
, FMODE_READ
))
3122 calldata
->arg
.bitmask
= NFS_SERVER(inode
)->cache_consistency_bitmask
;
3124 calldata
->arg
.bitmask
= NULL
;
3127 calldata
->arg
.share_access
=
3128 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
3129 calldata
->arg
.fmode
, 0);
3131 if (calldata
->res
.fattr
== NULL
)
3132 calldata
->arg
.bitmask
= NULL
;
3133 else if (calldata
->arg
.bitmask
== NULL
)
3134 calldata
->res
.fattr
= NULL
;
3135 calldata
->timestamp
= jiffies
;
3136 if (nfs4_setup_sequence(NFS_SERVER(inode
)->nfs_client
,
3137 &calldata
->arg
.seq_args
,
3138 &calldata
->res
.seq_res
,
3140 nfs_release_seqid(calldata
->arg
.seqid
);
3141 dprintk("%s: done!\n", __func__
);
3144 task
->tk_action
= NULL
;
3146 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
3149 static const struct rpc_call_ops nfs4_close_ops
= {
3150 .rpc_call_prepare
= nfs4_close_prepare
,
3151 .rpc_call_done
= nfs4_close_done
,
3152 .rpc_release
= nfs4_free_closedata
,
3156 * It is possible for data to be read/written from a mem-mapped file
3157 * after the sys_close call (which hits the vfs layer as a flush).
3158 * This means that we can't safely call nfsv4 close on a file until
3159 * the inode is cleared. This in turn means that we are not good
3160 * NFSv4 citizens - we do not indicate to the server to update the file's
3161 * share state even when we are done with one of the three share
3162 * stateid's in the inode.
3164 * NOTE: Caller must be holding the sp->so_owner semaphore!
3166 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
3168 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3169 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
3170 struct nfs4_closedata
*calldata
;
3171 struct nfs4_state_owner
*sp
= state
->owner
;
3172 struct rpc_task
*task
;
3173 struct rpc_message msg
= {
3174 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
3175 .rpc_cred
= state
->owner
->so_cred
,
3177 struct rpc_task_setup task_setup_data
= {
3178 .rpc_client
= server
->client
,
3179 .rpc_message
= &msg
,
3180 .callback_ops
= &nfs4_close_ops
,
3181 .workqueue
= nfsiod_workqueue
,
3182 .flags
= RPC_TASK_ASYNC
,
3184 int status
= -ENOMEM
;
3186 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
3187 &task_setup_data
.rpc_client
, &msg
);
3189 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
3190 if (calldata
== NULL
)
3192 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
3193 calldata
->inode
= state
->inode
;
3194 calldata
->state
= state
;
3195 calldata
->arg
.fh
= NFS_FH(state
->inode
);
3196 /* Serialization for the sequence id */
3197 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
3198 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
3199 if (IS_ERR(calldata
->arg
.seqid
))
3200 goto out_free_calldata
;
3201 nfs_fattr_init(&calldata
->fattr
);
3202 calldata
->arg
.fmode
= 0;
3203 calldata
->lr
.arg
.ld_private
= &calldata
->lr
.ld_private
;
3204 calldata
->res
.fattr
= &calldata
->fattr
;
3205 calldata
->res
.seqid
= calldata
->arg
.seqid
;
3206 calldata
->res
.server
= server
;
3207 calldata
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
3208 calldata
->lr
.roc
= pnfs_roc(state
->inode
,
3209 &calldata
->lr
.arg
, &calldata
->lr
.res
, msg
.rpc_cred
);
3210 if (calldata
->lr
.roc
) {
3211 calldata
->arg
.lr_args
= &calldata
->lr
.arg
;
3212 calldata
->res
.lr_res
= &calldata
->lr
.res
;
3214 nfs_sb_active(calldata
->inode
->i_sb
);
3216 msg
.rpc_argp
= &calldata
->arg
;
3217 msg
.rpc_resp
= &calldata
->res
;
3218 task_setup_data
.callback_data
= calldata
;
3219 task
= rpc_run_task(&task_setup_data
);
3221 return PTR_ERR(task
);
3224 status
= rpc_wait_for_completion_task(task
);
3230 nfs4_put_open_state(state
);
3231 nfs4_put_state_owner(sp
);
3235 static struct inode
*
3236 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3237 int open_flags
, struct iattr
*attr
, int *opened
)
3239 struct nfs4_state
*state
;
3240 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3242 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3244 /* Protect against concurrent sillydeletes */
3245 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3247 nfs4_label_release_security(label
);
3250 return ERR_CAST(state
);
3251 return state
->inode
;
3254 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3256 if (ctx
->state
== NULL
)
3259 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3261 nfs4_close_state(ctx
->state
, ctx
->mode
);
3264 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3265 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3266 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3268 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3270 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3271 struct nfs4_server_caps_arg args
= {
3275 struct nfs4_server_caps_res res
= {};
3276 struct rpc_message msg
= {
3277 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3283 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3284 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3285 FATTR4_WORD0_LINK_SUPPORT
|
3286 FATTR4_WORD0_SYMLINK_SUPPORT
|
3287 FATTR4_WORD0_ACLSUPPORT
;
3289 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3291 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3293 /* Sanity check the server answers */
3294 switch (minorversion
) {
3296 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3297 res
.attr_bitmask
[2] = 0;
3300 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3303 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3305 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3306 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3307 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3308 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3309 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3310 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3311 NFS_CAP_SECURITY_LABEL
);
3312 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3313 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3314 server
->caps
|= NFS_CAP_ACLS
;
3315 if (res
.has_links
!= 0)
3316 server
->caps
|= NFS_CAP_HARDLINKS
;
3317 if (res
.has_symlinks
!= 0)
3318 server
->caps
|= NFS_CAP_SYMLINKS
;
3319 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3320 server
->caps
|= NFS_CAP_FILEID
;
3321 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3322 server
->caps
|= NFS_CAP_MODE
;
3323 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3324 server
->caps
|= NFS_CAP_NLINK
;
3325 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3326 server
->caps
|= NFS_CAP_OWNER
;
3327 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3328 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3329 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3330 server
->caps
|= NFS_CAP_ATIME
;
3331 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3332 server
->caps
|= NFS_CAP_CTIME
;
3333 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3334 server
->caps
|= NFS_CAP_MTIME
;
3335 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3336 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3337 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3339 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3340 sizeof(server
->attr_bitmask
));
3341 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3343 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3344 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3345 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3346 server
->cache_consistency_bitmask
[2] = 0;
3347 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3348 sizeof(server
->exclcreat_bitmask
));
3349 server
->acl_bitmask
= res
.acl_bitmask
;
3350 server
->fh_expire_type
= res
.fh_expire_type
;
3356 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3358 struct nfs4_exception exception
= { };
3361 err
= nfs4_handle_exception(server
,
3362 _nfs4_server_capabilities(server
, fhandle
),
3364 } while (exception
.retry
);
3368 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3369 struct nfs_fsinfo
*info
)
3372 struct nfs4_lookup_root_arg args
= {
3375 struct nfs4_lookup_res res
= {
3377 .fattr
= info
->fattr
,
3380 struct rpc_message msg
= {
3381 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3386 bitmask
[0] = nfs4_fattr_bitmap
[0];
3387 bitmask
[1] = nfs4_fattr_bitmap
[1];
3389 * Process the label in the upcoming getfattr
3391 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3393 nfs_fattr_init(info
->fattr
);
3394 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3397 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3398 struct nfs_fsinfo
*info
)
3400 struct nfs4_exception exception
= { };
3403 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3404 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3407 case -NFS4ERR_WRONGSEC
:
3410 err
= nfs4_handle_exception(server
, err
, &exception
);
3412 } while (exception
.retry
);
3417 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3418 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3420 struct rpc_auth_create_args auth_args
= {
3421 .pseudoflavor
= flavor
,
3423 struct rpc_auth
*auth
;
3426 auth
= rpcauth_create(&auth_args
, server
->client
);
3431 ret
= nfs4_lookup_root(server
, fhandle
, info
);
3437 * Retry pseudoroot lookup with various security flavors. We do this when:
3439 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3440 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3442 * Returns zero on success, or a negative NFS4ERR value, or a
3443 * negative errno value.
3445 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3446 struct nfs_fsinfo
*info
)
3448 /* Per 3530bis 15.33.5 */
3449 static const rpc_authflavor_t flav_array
[] = {
3453 RPC_AUTH_UNIX
, /* courtesy */
3456 int status
= -EPERM
;
3459 if (server
->auth_info
.flavor_len
> 0) {
3460 /* try each flavor specified by user */
3461 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3462 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3463 server
->auth_info
.flavors
[i
]);
3464 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3469 /* no flavors specified by user, try default list */
3470 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3471 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3473 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3480 * -EACCESS could mean that the user doesn't have correct permissions
3481 * to access the mount. It could also mean that we tried to mount
3482 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3483 * existing mount programs don't handle -EACCES very well so it should
3484 * be mapped to -EPERM instead.
3486 if (status
== -EACCES
)
3492 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3493 * @server: initialized nfs_server handle
3494 * @fhandle: we fill in the pseudo-fs root file handle
3495 * @info: we fill in an FSINFO struct
3496 * @auth_probe: probe the auth flavours
3498 * Returns zero on success, or a negative errno.
3500 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3501 struct nfs_fsinfo
*info
,
3507 status
= nfs4_lookup_root(server
, fhandle
, info
);
3509 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3510 status
= server
->nfs_client
->cl_mvops
->find_root_sec(server
,
3514 status
= nfs4_server_capabilities(server
, fhandle
);
3516 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3518 return nfs4_map_errors(status
);
3521 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3522 struct nfs_fsinfo
*info
)
3525 struct nfs_fattr
*fattr
= info
->fattr
;
3526 struct nfs4_label
*label
= NULL
;
3528 error
= nfs4_server_capabilities(server
, mntfh
);
3530 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3534 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3536 return PTR_ERR(label
);
3538 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3540 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3541 goto err_free_label
;
3544 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3545 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3546 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3549 nfs4_label_free(label
);
3555 * Get locations and (maybe) other attributes of a referral.
3556 * Note that we'll actually follow the referral later when
3557 * we detect fsid mismatch in inode revalidation
3559 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3560 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3561 struct nfs_fh
*fhandle
)
3563 int status
= -ENOMEM
;
3564 struct page
*page
= NULL
;
3565 struct nfs4_fs_locations
*locations
= NULL
;
3567 page
= alloc_page(GFP_KERNEL
);
3570 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3571 if (locations
== NULL
)
3574 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3579 * If the fsid didn't change, this is a migration event, not a
3580 * referral. Cause us to drop into the exception handler, which
3581 * will kick off migration recovery.
3583 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3584 dprintk("%s: server did not return a different fsid for"
3585 " a referral at %s\n", __func__
, name
->name
);
3586 status
= -NFS4ERR_MOVED
;
3589 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3590 nfs_fixup_referral_attributes(&locations
->fattr
);
3592 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3593 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3594 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3602 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3603 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3605 struct nfs4_getattr_arg args
= {
3607 .bitmask
= server
->attr_bitmask
,
3609 struct nfs4_getattr_res res
= {
3614 struct rpc_message msg
= {
3615 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3620 args
.bitmask
= nfs4_bitmask(server
, label
);
3622 nfs_fattr_init(fattr
);
3623 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3626 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3627 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3629 struct nfs4_exception exception
= { };
3632 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3633 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3634 err
= nfs4_handle_exception(server
, err
,
3636 } while (exception
.retry
);
3641 * The file is not closed if it is opened due to the a request to change
3642 * the size of the file. The open call will not be needed once the
3643 * VFS layer lookup-intents are implemented.
3645 * Close is called when the inode is destroyed.
3646 * If we haven't opened the file for O_WRONLY, we
3647 * need to in the size_change case to obtain a stateid.
3650 * Because OPEN is always done by name in nfsv4, it is
3651 * possible that we opened a different file by the same
3652 * name. We can recognize this race condition, but we
3653 * can't do anything about it besides returning an error.
3655 * This will be fixed with VFS changes (lookup-intent).
3658 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3659 struct iattr
*sattr
)
3661 struct inode
*inode
= d_inode(dentry
);
3662 struct rpc_cred
*cred
= NULL
;
3663 struct nfs_open_context
*ctx
= NULL
;
3664 struct nfs4_label
*label
= NULL
;
3667 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3668 sattr
->ia_valid
& ATTR_SIZE
&&
3669 sattr
->ia_size
< i_size_read(inode
))
3670 pnfs_commit_and_return_layout(inode
);
3672 nfs_fattr_init(fattr
);
3674 /* Deal with open(O_TRUNC) */
3675 if (sattr
->ia_valid
& ATTR_OPEN
)
3676 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3678 /* Optimization: if the end result is no change, don't RPC */
3679 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3682 /* Search for an existing open(O_WRITE) file */
3683 if (sattr
->ia_valid
& ATTR_FILE
) {
3685 ctx
= nfs_file_open_context(sattr
->ia_file
);
3690 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3692 return PTR_ERR(label
);
3694 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, ctx
, NULL
, label
);
3696 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3697 nfs_setsecurity(inode
, fattr
, label
);
3699 nfs4_label_free(label
);
3703 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3704 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3705 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3707 struct nfs_server
*server
= NFS_SERVER(dir
);
3709 struct nfs4_lookup_arg args
= {
3710 .bitmask
= server
->attr_bitmask
,
3711 .dir_fh
= NFS_FH(dir
),
3714 struct nfs4_lookup_res res
= {
3720 struct rpc_message msg
= {
3721 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3726 args
.bitmask
= nfs4_bitmask(server
, label
);
3728 nfs_fattr_init(fattr
);
3730 dprintk("NFS call lookup %s\n", name
->name
);
3731 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3732 dprintk("NFS reply lookup: %d\n", status
);
3736 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3738 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3739 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3740 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3744 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3745 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3746 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3748 struct nfs4_exception exception
= { };
3749 struct rpc_clnt
*client
= *clnt
;
3752 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3753 trace_nfs4_lookup(dir
, name
, err
);
3755 case -NFS4ERR_BADNAME
:
3758 case -NFS4ERR_MOVED
:
3759 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3760 if (err
== -NFS4ERR_MOVED
)
3761 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3763 case -NFS4ERR_WRONGSEC
:
3765 if (client
!= *clnt
)
3767 client
= nfs4_negotiate_security(client
, dir
, name
);
3769 return PTR_ERR(client
);
3771 exception
.retry
= 1;
3774 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3776 } while (exception
.retry
);
3781 else if (client
!= *clnt
)
3782 rpc_shutdown_client(client
);
3787 static int nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
3788 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3789 struct nfs4_label
*label
)
3792 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3794 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3795 if (client
!= NFS_CLIENT(dir
)) {
3796 rpc_shutdown_client(client
);
3797 nfs_fixup_secinfo_attributes(fattr
);
3803 nfs4_proc_lookup_mountpoint(struct inode
*dir
, const struct qstr
*name
,
3804 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3806 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3809 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3811 return ERR_PTR(status
);
3812 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3815 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3817 struct nfs_server
*server
= NFS_SERVER(inode
);
3818 struct nfs4_accessargs args
= {
3819 .fh
= NFS_FH(inode
),
3820 .bitmask
= server
->cache_consistency_bitmask
,
3822 struct nfs4_accessres res
= {
3825 struct rpc_message msg
= {
3826 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3829 .rpc_cred
= entry
->cred
,
3831 int mode
= entry
->mask
;
3835 * Determine which access bits we want to ask for...
3837 if (mode
& MAY_READ
)
3838 args
.access
|= NFS4_ACCESS_READ
;
3839 if (S_ISDIR(inode
->i_mode
)) {
3840 if (mode
& MAY_WRITE
)
3841 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3842 if (mode
& MAY_EXEC
)
3843 args
.access
|= NFS4_ACCESS_LOOKUP
;
3845 if (mode
& MAY_WRITE
)
3846 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3847 if (mode
& MAY_EXEC
)
3848 args
.access
|= NFS4_ACCESS_EXECUTE
;
3851 res
.fattr
= nfs_alloc_fattr();
3852 if (res
.fattr
== NULL
)
3855 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3857 nfs_access_set_mask(entry
, res
.access
);
3858 nfs_refresh_inode(inode
, res
.fattr
);
3860 nfs_free_fattr(res
.fattr
);
3864 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3866 struct nfs4_exception exception
= { };
3869 err
= _nfs4_proc_access(inode
, entry
);
3870 trace_nfs4_access(inode
, err
);
3871 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3873 } while (exception
.retry
);
3878 * TODO: For the time being, we don't try to get any attributes
3879 * along with any of the zero-copy operations READ, READDIR,
3882 * In the case of the first three, we want to put the GETATTR
3883 * after the read-type operation -- this is because it is hard
3884 * to predict the length of a GETATTR response in v4, and thus
3885 * align the READ data correctly. This means that the GETATTR
3886 * may end up partially falling into the page cache, and we should
3887 * shift it into the 'tail' of the xdr_buf before processing.
3888 * To do this efficiently, we need to know the total length
3889 * of data received, which doesn't seem to be available outside
3892 * In the case of WRITE, we also want to put the GETATTR after
3893 * the operation -- in this case because we want to make sure
3894 * we get the post-operation mtime and size.
3896 * Both of these changes to the XDR layer would in fact be quite
3897 * minor, but I decided to leave them for a subsequent patch.
3899 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3900 unsigned int pgbase
, unsigned int pglen
)
3902 struct nfs4_readlink args
= {
3903 .fh
= NFS_FH(inode
),
3908 struct nfs4_readlink_res res
;
3909 struct rpc_message msg
= {
3910 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3915 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3918 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3919 unsigned int pgbase
, unsigned int pglen
)
3921 struct nfs4_exception exception
= { };
3924 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3925 trace_nfs4_readlink(inode
, err
);
3926 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3928 } while (exception
.retry
);
3933 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3936 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3939 struct nfs_server
*server
= NFS_SERVER(dir
);
3940 struct nfs4_label l
, *ilabel
= NULL
;
3941 struct nfs_open_context
*ctx
;
3942 struct nfs4_state
*state
;
3945 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
, NULL
);
3947 return PTR_ERR(ctx
);
3949 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3951 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
3952 sattr
->ia_mode
&= ~current_umask();
3953 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
3954 if (IS_ERR(state
)) {
3955 status
= PTR_ERR(state
);
3959 nfs4_label_release_security(ilabel
);
3960 put_nfs_open_context(ctx
);
3964 static int _nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
3966 struct nfs_server
*server
= NFS_SERVER(dir
);
3967 struct nfs_removeargs args
= {
3971 struct nfs_removeres res
= {
3974 struct rpc_message msg
= {
3975 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3979 unsigned long timestamp
= jiffies
;
3982 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3984 update_changeattr(dir
, &res
.cinfo
, timestamp
);
3988 static int nfs4_proc_remove(struct inode
*dir
, const struct qstr
*name
)
3990 struct nfs4_exception exception
= { };
3993 err
= _nfs4_proc_remove(dir
, name
);
3994 trace_nfs4_remove(dir
, name
, err
);
3995 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3997 } while (exception
.retry
);
4001 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
4003 struct nfs_server
*server
= NFS_SERVER(dir
);
4004 struct nfs_removeargs
*args
= msg
->rpc_argp
;
4005 struct nfs_removeres
*res
= msg
->rpc_resp
;
4007 res
->server
= server
;
4008 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
4009 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
4011 nfs_fattr_init(res
->dir_attr
);
4014 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
4016 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
)->nfs_client
,
4017 &data
->args
.seq_args
,
4022 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
4024 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
4025 struct nfs_removeres
*res
= &data
->res
;
4027 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4029 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
4030 &data
->timeout
) == -EAGAIN
)
4032 if (task
->tk_status
== 0)
4033 update_changeattr(dir
, &res
->cinfo
, res
->dir_attr
->time_start
);
4037 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
4039 struct nfs_server
*server
= NFS_SERVER(dir
);
4040 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
4041 struct nfs_renameres
*res
= msg
->rpc_resp
;
4043 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
4044 res
->server
= server
;
4045 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
4048 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
4050 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
)->nfs_client
,
4051 &data
->args
.seq_args
,
4056 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
4057 struct inode
*new_dir
)
4059 struct nfs_renamedata
*data
= task
->tk_calldata
;
4060 struct nfs_renameres
*res
= &data
->res
;
4062 if (!nfs4_sequence_done(task
, &res
->seq_res
))
4064 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
4067 if (task
->tk_status
== 0) {
4068 update_changeattr(old_dir
, &res
->old_cinfo
, res
->old_fattr
->time_start
);
4069 if (new_dir
!= old_dir
)
4070 update_changeattr(new_dir
, &res
->new_cinfo
, res
->new_fattr
->time_start
);
4075 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4077 struct nfs_server
*server
= NFS_SERVER(inode
);
4078 struct nfs4_link_arg arg
= {
4079 .fh
= NFS_FH(inode
),
4080 .dir_fh
= NFS_FH(dir
),
4082 .bitmask
= server
->attr_bitmask
,
4084 struct nfs4_link_res res
= {
4088 struct rpc_message msg
= {
4089 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
4093 int status
= -ENOMEM
;
4095 res
.fattr
= nfs_alloc_fattr();
4096 if (res
.fattr
== NULL
)
4099 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4100 if (IS_ERR(res
.label
)) {
4101 status
= PTR_ERR(res
.label
);
4104 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
4106 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4108 update_changeattr(dir
, &res
.cinfo
, res
.fattr
->time_start
);
4109 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
4111 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
4115 nfs4_label_free(res
.label
);
4118 nfs_free_fattr(res
.fattr
);
4122 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, const struct qstr
*name
)
4124 struct nfs4_exception exception
= { };
4127 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4128 _nfs4_proc_link(inode
, dir
, name
),
4130 } while (exception
.retry
);
4134 struct nfs4_createdata
{
4135 struct rpc_message msg
;
4136 struct nfs4_create_arg arg
;
4137 struct nfs4_create_res res
;
4139 struct nfs_fattr fattr
;
4140 struct nfs4_label
*label
;
4143 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
4144 const struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
4146 struct nfs4_createdata
*data
;
4148 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
4150 struct nfs_server
*server
= NFS_SERVER(dir
);
4152 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
4153 if (IS_ERR(data
->label
))
4156 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
4157 data
->msg
.rpc_argp
= &data
->arg
;
4158 data
->msg
.rpc_resp
= &data
->res
;
4159 data
->arg
.dir_fh
= NFS_FH(dir
);
4160 data
->arg
.server
= server
;
4161 data
->arg
.name
= name
;
4162 data
->arg
.attrs
= sattr
;
4163 data
->arg
.ftype
= ftype
;
4164 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
4165 data
->arg
.umask
= current_umask();
4166 data
->res
.server
= server
;
4167 data
->res
.fh
= &data
->fh
;
4168 data
->res
.fattr
= &data
->fattr
;
4169 data
->res
.label
= data
->label
;
4170 nfs_fattr_init(data
->res
.fattr
);
4178 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
4180 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
4181 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4183 update_changeattr(dir
, &data
->res
.dir_cinfo
,
4184 data
->res
.fattr
->time_start
);
4185 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
4190 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
4192 nfs4_label_free(data
->label
);
4196 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4197 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
4198 struct nfs4_label
*label
)
4200 struct nfs4_createdata
*data
;
4201 int status
= -ENAMETOOLONG
;
4203 if (len
> NFS4_MAXPATHLEN
)
4207 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
4211 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
4212 data
->arg
.u
.symlink
.pages
= &page
;
4213 data
->arg
.u
.symlink
.len
= len
;
4214 data
->arg
.label
= label
;
4216 status
= nfs4_do_create(dir
, dentry
, data
);
4218 nfs4_free_createdata(data
);
4223 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4224 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
4226 struct nfs4_exception exception
= { };
4227 struct nfs4_label l
, *label
= NULL
;
4230 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4233 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4234 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4235 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4237 } while (exception
.retry
);
4239 nfs4_label_release_security(label
);
4243 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4244 struct iattr
*sattr
, struct nfs4_label
*label
)
4246 struct nfs4_createdata
*data
;
4247 int status
= -ENOMEM
;
4249 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4253 data
->arg
.label
= label
;
4254 status
= nfs4_do_create(dir
, dentry
, data
);
4256 nfs4_free_createdata(data
);
4261 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4262 struct iattr
*sattr
)
4264 struct nfs_server
*server
= NFS_SERVER(dir
);
4265 struct nfs4_exception exception
= { };
4266 struct nfs4_label l
, *label
= NULL
;
4269 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4271 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4272 sattr
->ia_mode
&= ~current_umask();
4274 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4275 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4276 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4278 } while (exception
.retry
);
4279 nfs4_label_release_security(label
);
4284 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4285 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4287 struct inode
*dir
= d_inode(dentry
);
4288 struct nfs4_readdir_arg args
= {
4293 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4296 struct nfs4_readdir_res res
;
4297 struct rpc_message msg
= {
4298 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4305 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4307 (unsigned long long)cookie
);
4308 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4309 res
.pgbase
= args
.pgbase
;
4310 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4312 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4313 status
+= args
.pgbase
;
4316 nfs_invalidate_atime(dir
);
4318 dprintk("%s: returns %d\n", __func__
, status
);
4322 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4323 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4325 struct nfs4_exception exception
= { };
4328 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4329 pages
, count
, plus
);
4330 trace_nfs4_readdir(d_inode(dentry
), err
);
4331 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4333 } while (exception
.retry
);
4337 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4338 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4340 struct nfs4_createdata
*data
;
4341 int mode
= sattr
->ia_mode
;
4342 int status
= -ENOMEM
;
4344 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4349 data
->arg
.ftype
= NF4FIFO
;
4350 else if (S_ISBLK(mode
)) {
4351 data
->arg
.ftype
= NF4BLK
;
4352 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4353 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4355 else if (S_ISCHR(mode
)) {
4356 data
->arg
.ftype
= NF4CHR
;
4357 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4358 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4359 } else if (!S_ISSOCK(mode
)) {
4364 data
->arg
.label
= label
;
4365 status
= nfs4_do_create(dir
, dentry
, data
);
4367 nfs4_free_createdata(data
);
4372 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4373 struct iattr
*sattr
, dev_t rdev
)
4375 struct nfs_server
*server
= NFS_SERVER(dir
);
4376 struct nfs4_exception exception
= { };
4377 struct nfs4_label l
, *label
= NULL
;
4380 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4382 if (!(server
->attr_bitmask
[2] & FATTR4_WORD2_MODE_UMASK
))
4383 sattr
->ia_mode
&= ~current_umask();
4385 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4386 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4387 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4389 } while (exception
.retry
);
4391 nfs4_label_release_security(label
);
4396 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4397 struct nfs_fsstat
*fsstat
)
4399 struct nfs4_statfs_arg args
= {
4401 .bitmask
= server
->attr_bitmask
,
4403 struct nfs4_statfs_res res
= {
4406 struct rpc_message msg
= {
4407 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4412 nfs_fattr_init(fsstat
->fattr
);
4413 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4416 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4418 struct nfs4_exception exception
= { };
4421 err
= nfs4_handle_exception(server
,
4422 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4424 } while (exception
.retry
);
4428 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4429 struct nfs_fsinfo
*fsinfo
)
4431 struct nfs4_fsinfo_arg args
= {
4433 .bitmask
= server
->attr_bitmask
,
4435 struct nfs4_fsinfo_res res
= {
4438 struct rpc_message msg
= {
4439 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4444 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4447 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4449 struct nfs4_exception exception
= { };
4450 unsigned long now
= jiffies
;
4454 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4455 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4457 nfs4_set_lease_period(server
->nfs_client
,
4458 fsinfo
->lease_time
* HZ
,
4462 err
= nfs4_handle_exception(server
, err
, &exception
);
4463 } while (exception
.retry
);
4467 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4471 nfs_fattr_init(fsinfo
->fattr
);
4472 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4474 /* block layout checks this! */
4475 server
->pnfs_blksize
= fsinfo
->blksize
;
4476 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
);
4482 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4483 struct nfs_pathconf
*pathconf
)
4485 struct nfs4_pathconf_arg args
= {
4487 .bitmask
= server
->attr_bitmask
,
4489 struct nfs4_pathconf_res res
= {
4490 .pathconf
= pathconf
,
4492 struct rpc_message msg
= {
4493 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4498 /* None of the pathconf attributes are mandatory to implement */
4499 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4500 memset(pathconf
, 0, sizeof(*pathconf
));
4504 nfs_fattr_init(pathconf
->fattr
);
4505 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4508 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4509 struct nfs_pathconf
*pathconf
)
4511 struct nfs4_exception exception
= { };
4515 err
= nfs4_handle_exception(server
,
4516 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4518 } while (exception
.retry
);
4522 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4523 const struct nfs_open_context
*ctx
,
4524 const struct nfs_lock_context
*l_ctx
,
4527 return nfs4_select_rw_stateid(ctx
->state
, fmode
, l_ctx
, stateid
, NULL
);
4529 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4531 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4532 const struct nfs_open_context
*ctx
,
4533 const struct nfs_lock_context
*l_ctx
,
4536 nfs4_stateid current_stateid
;
4538 /* If the current stateid represents a lost lock, then exit */
4539 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4541 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4544 static bool nfs4_error_stateid_expired(int err
)
4547 case -NFS4ERR_DELEG_REVOKED
:
4548 case -NFS4ERR_ADMIN_REVOKED
:
4549 case -NFS4ERR_BAD_STATEID
:
4550 case -NFS4ERR_STALE_STATEID
:
4551 case -NFS4ERR_OLD_STATEID
:
4552 case -NFS4ERR_OPENMODE
:
4553 case -NFS4ERR_EXPIRED
:
4559 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4561 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4563 trace_nfs4_read(hdr
, task
->tk_status
);
4564 if (task
->tk_status
< 0) {
4565 struct nfs4_exception exception
= {
4566 .inode
= hdr
->inode
,
4567 .state
= hdr
->args
.context
->state
,
4568 .stateid
= &hdr
->args
.stateid
,
4570 task
->tk_status
= nfs4_async_handle_exception(task
,
4571 server
, task
->tk_status
, &exception
);
4572 if (exception
.retry
) {
4573 rpc_restart_call_prepare(task
);
4578 if (task
->tk_status
> 0)
4579 renew_lease(server
, hdr
->timestamp
);
4583 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4584 struct nfs_pgio_args
*args
)
4587 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4588 nfs4_stateid_is_current(&args
->stateid
,
4593 rpc_restart_call_prepare(task
);
4597 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4600 dprintk("--> %s\n", __func__
);
4602 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4604 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4606 if (task
->tk_status
> 0)
4607 nfs_invalidate_atime(hdr
->inode
);
4608 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4609 nfs4_read_done_cb(task
, hdr
);
4612 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4613 struct rpc_message
*msg
)
4615 hdr
->timestamp
= jiffies
;
4616 if (!hdr
->pgio_done_cb
)
4617 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4618 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4619 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4622 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4623 struct nfs_pgio_header
*hdr
)
4625 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
)->nfs_client
,
4626 &hdr
->args
.seq_args
,
4630 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4631 hdr
->args
.lock_context
,
4632 hdr
->rw_ops
->rw_mode
) == -EIO
)
4634 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4639 static int nfs4_write_done_cb(struct rpc_task
*task
,
4640 struct nfs_pgio_header
*hdr
)
4642 struct inode
*inode
= hdr
->inode
;
4644 trace_nfs4_write(hdr
, task
->tk_status
);
4645 if (task
->tk_status
< 0) {
4646 struct nfs4_exception exception
= {
4647 .inode
= hdr
->inode
,
4648 .state
= hdr
->args
.context
->state
,
4649 .stateid
= &hdr
->args
.stateid
,
4651 task
->tk_status
= nfs4_async_handle_exception(task
,
4652 NFS_SERVER(inode
), task
->tk_status
,
4654 if (exception
.retry
) {
4655 rpc_restart_call_prepare(task
);
4659 if (task
->tk_status
>= 0) {
4660 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4661 nfs_writeback_update_inode(hdr
);
4666 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4667 struct nfs_pgio_args
*args
)
4670 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4671 nfs4_stateid_is_current(&args
->stateid
,
4676 rpc_restart_call_prepare(task
);
4680 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4682 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4684 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4686 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4687 nfs4_write_done_cb(task
, hdr
);
4691 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4693 /* Don't request attributes for pNFS or O_DIRECT writes */
4694 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4696 /* Otherwise, request attributes if and only if we don't hold
4699 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4702 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4703 struct rpc_message
*msg
)
4705 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4707 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4708 hdr
->args
.bitmask
= NULL
;
4709 hdr
->res
.fattr
= NULL
;
4711 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4713 if (!hdr
->pgio_done_cb
)
4714 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4715 hdr
->res
.server
= server
;
4716 hdr
->timestamp
= jiffies
;
4718 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4719 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4722 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4724 nfs4_setup_sequence(NFS_SERVER(data
->inode
)->nfs_client
,
4725 &data
->args
.seq_args
,
4730 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4732 struct inode
*inode
= data
->inode
;
4734 trace_nfs4_commit(data
, task
->tk_status
);
4735 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4736 NULL
, NULL
) == -EAGAIN
) {
4737 rpc_restart_call_prepare(task
);
4743 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4745 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4747 return data
->commit_done_cb(task
, data
);
4750 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4752 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4754 if (data
->commit_done_cb
== NULL
)
4755 data
->commit_done_cb
= nfs4_commit_done_cb
;
4756 data
->res
.server
= server
;
4757 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4758 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4761 struct nfs4_renewdata
{
4762 struct nfs_client
*client
;
4763 unsigned long timestamp
;
4767 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4768 * standalone procedure for queueing an asynchronous RENEW.
4770 static void nfs4_renew_release(void *calldata
)
4772 struct nfs4_renewdata
*data
= calldata
;
4773 struct nfs_client
*clp
= data
->client
;
4775 if (atomic_read(&clp
->cl_count
) > 1)
4776 nfs4_schedule_state_renewal(clp
);
4777 nfs_put_client(clp
);
4781 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4783 struct nfs4_renewdata
*data
= calldata
;
4784 struct nfs_client
*clp
= data
->client
;
4785 unsigned long timestamp
= data
->timestamp
;
4787 trace_nfs4_renew_async(clp
, task
->tk_status
);
4788 switch (task
->tk_status
) {
4791 case -NFS4ERR_LEASE_MOVED
:
4792 nfs4_schedule_lease_moved_recovery(clp
);
4795 /* Unless we're shutting down, schedule state recovery! */
4796 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4798 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4799 nfs4_schedule_lease_recovery(clp
);
4802 nfs4_schedule_path_down_recovery(clp
);
4804 do_renew_lease(clp
, timestamp
);
4807 static const struct rpc_call_ops nfs4_renew_ops
= {
4808 .rpc_call_done
= nfs4_renew_done
,
4809 .rpc_release
= nfs4_renew_release
,
4812 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4814 struct rpc_message msg
= {
4815 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4819 struct nfs4_renewdata
*data
;
4821 if (renew_flags
== 0)
4823 if (!atomic_inc_not_zero(&clp
->cl_count
))
4825 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4829 data
->timestamp
= jiffies
;
4830 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4831 &nfs4_renew_ops
, data
);
4834 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4836 struct rpc_message msg
= {
4837 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4841 unsigned long now
= jiffies
;
4844 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4847 do_renew_lease(clp
, now
);
4851 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4853 return server
->caps
& NFS_CAP_ACLS
;
4856 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4857 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4860 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4862 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4863 struct page
**pages
)
4865 struct page
*newpage
, **spages
;
4871 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4872 newpage
= alloc_page(GFP_KERNEL
);
4874 if (newpage
== NULL
)
4876 memcpy(page_address(newpage
), buf
, len
);
4881 } while (buflen
!= 0);
4887 __free_page(spages
[rc
-1]);
4891 struct nfs4_cached_acl
{
4897 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4899 struct nfs_inode
*nfsi
= NFS_I(inode
);
4901 spin_lock(&inode
->i_lock
);
4902 kfree(nfsi
->nfs4_acl
);
4903 nfsi
->nfs4_acl
= acl
;
4904 spin_unlock(&inode
->i_lock
);
4907 static void nfs4_zap_acl_attr(struct inode
*inode
)
4909 nfs4_set_cached_acl(inode
, NULL
);
4912 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4914 struct nfs_inode
*nfsi
= NFS_I(inode
);
4915 struct nfs4_cached_acl
*acl
;
4918 spin_lock(&inode
->i_lock
);
4919 acl
= nfsi
->nfs4_acl
;
4922 if (buf
== NULL
) /* user is just asking for length */
4924 if (acl
->cached
== 0)
4926 ret
= -ERANGE
; /* see getxattr(2) man page */
4927 if (acl
->len
> buflen
)
4929 memcpy(buf
, acl
->data
, acl
->len
);
4933 spin_unlock(&inode
->i_lock
);
4937 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4939 struct nfs4_cached_acl
*acl
;
4940 size_t buflen
= sizeof(*acl
) + acl_len
;
4942 if (buflen
<= PAGE_SIZE
) {
4943 acl
= kmalloc(buflen
, GFP_KERNEL
);
4947 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4949 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4956 nfs4_set_cached_acl(inode
, acl
);
4960 * The getxattr API returns the required buffer length when called with a
4961 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4962 * the required buf. On a NULL buf, we send a page of data to the server
4963 * guessing that the ACL request can be serviced by a page. If so, we cache
4964 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4965 * the cache. If not so, we throw away the page, and cache the required
4966 * length. The next getxattr call will then produce another round trip to
4967 * the server, this time with the input buf of the required size.
4969 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4971 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4972 struct nfs_getaclargs args
= {
4973 .fh
= NFS_FH(inode
),
4977 struct nfs_getaclres res
= {
4980 struct rpc_message msg
= {
4981 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4985 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4986 int ret
= -ENOMEM
, i
;
4988 /* As long as we're doing a round trip to the server anyway,
4989 * let's be prepared for a page of acl data. */
4992 if (npages
> ARRAY_SIZE(pages
))
4995 for (i
= 0; i
< npages
; i
++) {
4996 pages
[i
] = alloc_page(GFP_KERNEL
);
5001 /* for decoding across pages */
5002 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
5003 if (!res
.acl_scratch
)
5006 args
.acl_len
= npages
* PAGE_SIZE
;
5008 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5009 __func__
, buf
, buflen
, npages
, args
.acl_len
);
5010 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
5011 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5015 /* Handle the case where the passed-in buffer is too short */
5016 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
5017 /* Did the user only issue a request for the acl length? */
5023 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
5025 if (res
.acl_len
> buflen
) {
5029 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
5034 for (i
= 0; i
< npages
; i
++)
5036 __free_page(pages
[i
]);
5037 if (res
.acl_scratch
)
5038 __free_page(res
.acl_scratch
);
5042 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
5044 struct nfs4_exception exception
= { };
5047 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
5048 trace_nfs4_get_acl(inode
, ret
);
5051 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
5052 } while (exception
.retry
);
5056 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
5058 struct nfs_server
*server
= NFS_SERVER(inode
);
5061 if (!nfs4_server_supports_acls(server
))
5063 ret
= nfs_revalidate_inode(server
, inode
);
5066 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
5067 nfs_zap_acl_cache(inode
);
5068 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
5070 /* -ENOENT is returned if there is no ACL or if there is an ACL
5071 * but no cached acl data, just the acl length */
5073 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
5076 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5078 struct nfs_server
*server
= NFS_SERVER(inode
);
5079 struct page
*pages
[NFS4ACL_MAXPAGES
];
5080 struct nfs_setaclargs arg
= {
5081 .fh
= NFS_FH(inode
),
5085 struct nfs_setaclres res
;
5086 struct rpc_message msg
= {
5087 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
5091 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
5094 if (!nfs4_server_supports_acls(server
))
5096 if (npages
> ARRAY_SIZE(pages
))
5098 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
5101 nfs4_inode_return_delegation(inode
);
5102 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5105 * Free each page after tx, so the only ref left is
5106 * held by the network stack
5109 put_page(pages
[i
-1]);
5112 * Acl update can result in inode attribute update.
5113 * so mark the attribute cache invalid.
5115 spin_lock(&inode
->i_lock
);
5116 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
5117 spin_unlock(&inode
->i_lock
);
5118 nfs_access_zap_cache(inode
);
5119 nfs_zap_acl_cache(inode
);
5123 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
5125 struct nfs4_exception exception
= { };
5128 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
5129 trace_nfs4_set_acl(inode
, err
);
5130 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5132 } while (exception
.retry
);
5136 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5137 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
5140 struct nfs_server
*server
= NFS_SERVER(inode
);
5141 struct nfs_fattr fattr
;
5142 struct nfs4_label label
= {0, 0, buflen
, buf
};
5144 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5145 struct nfs4_getattr_arg arg
= {
5146 .fh
= NFS_FH(inode
),
5149 struct nfs4_getattr_res res
= {
5154 struct rpc_message msg
= {
5155 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
5161 nfs_fattr_init(&fattr
);
5163 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
5166 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
5168 if (buflen
< label
.len
)
5173 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
5176 struct nfs4_exception exception
= { };
5179 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5183 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
5184 trace_nfs4_get_security_label(inode
, err
);
5185 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5187 } while (exception
.retry
);
5191 static int _nfs4_do_set_security_label(struct inode
*inode
,
5192 struct nfs4_label
*ilabel
,
5193 struct nfs_fattr
*fattr
,
5194 struct nfs4_label
*olabel
)
5197 struct iattr sattr
= {0};
5198 struct nfs_server
*server
= NFS_SERVER(inode
);
5199 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
5200 struct nfs_setattrargs arg
= {
5201 .fh
= NFS_FH(inode
),
5207 struct nfs_setattrres res
= {
5212 struct rpc_message msg
= {
5213 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
5219 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
5221 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5223 dprintk("%s failed: %d\n", __func__
, status
);
5228 static int nfs4_do_set_security_label(struct inode
*inode
,
5229 struct nfs4_label
*ilabel
,
5230 struct nfs_fattr
*fattr
,
5231 struct nfs4_label
*olabel
)
5233 struct nfs4_exception exception
= { };
5237 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5239 trace_nfs4_set_security_label(inode
, err
);
5240 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5242 } while (exception
.retry
);
5247 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5249 struct nfs4_label ilabel
, *olabel
= NULL
;
5250 struct nfs_fattr fattr
;
5251 struct rpc_cred
*cred
;
5254 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5257 nfs_fattr_init(&fattr
);
5261 ilabel
.label
= (char *)buf
;
5262 ilabel
.len
= buflen
;
5264 cred
= rpc_lookup_cred();
5266 return PTR_ERR(cred
);
5268 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5269 if (IS_ERR(olabel
)) {
5270 status
= -PTR_ERR(olabel
);
5274 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5276 nfs_setsecurity(inode
, &fattr
, olabel
);
5278 nfs4_label_free(olabel
);
5283 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5286 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5287 nfs4_verifier
*bootverf
)
5291 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5292 /* An impossible timestamp guarantees this value
5293 * will never match a generated boot time. */
5294 verf
[0] = cpu_to_be32(U32_MAX
);
5295 verf
[1] = cpu_to_be32(U32_MAX
);
5297 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5298 u64 ns
= ktime_to_ns(nn
->boot_time
);
5300 verf
[0] = cpu_to_be32(ns
>> 32);
5301 verf
[1] = cpu_to_be32(ns
);
5303 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5307 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5312 if (clp
->cl_owner_id
!= NULL
)
5316 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5317 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5319 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
5323 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5327 * Since this string is allocated at mount time, and held until the
5328 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5329 * about a memory-reclaim deadlock.
5331 str
= kmalloc(len
, GFP_KERNEL
);
5336 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5338 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5339 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5342 clp
->cl_owner_id
= str
;
5347 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5352 len
= 10 + 10 + 1 + 10 + 1 +
5353 strlen(nfs4_client_id_uniquifier
) + 1 +
5354 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5356 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5360 * Since this string is allocated at mount time, and held until the
5361 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5362 * about a memory-reclaim deadlock.
5364 str
= kmalloc(len
, GFP_KERNEL
);
5368 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5369 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5370 nfs4_client_id_uniquifier
,
5371 clp
->cl_rpcclient
->cl_nodename
);
5372 clp
->cl_owner_id
= str
;
5377 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5382 if (clp
->cl_owner_id
!= NULL
)
5385 if (nfs4_client_id_uniquifier
[0] != '\0')
5386 return nfs4_init_uniquifier_client_string(clp
);
5388 len
= 10 + 10 + 1 + 10 + 1 +
5389 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5391 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5395 * Since this string is allocated at mount time, and held until the
5396 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5397 * about a memory-reclaim deadlock.
5399 str
= kmalloc(len
, GFP_KERNEL
);
5403 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5404 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5405 clp
->cl_rpcclient
->cl_nodename
);
5406 clp
->cl_owner_id
= str
;
5411 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5412 * services. Advertise one based on the address family of the
5416 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5418 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5419 return scnprintf(buf
, len
, "tcp6");
5421 return scnprintf(buf
, len
, "tcp");
5424 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5426 struct nfs4_setclientid
*sc
= calldata
;
5428 if (task
->tk_status
== 0)
5429 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5432 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5433 .rpc_call_done
= nfs4_setclientid_done
,
5437 * nfs4_proc_setclientid - Negotiate client ID
5438 * @clp: state data structure
5439 * @program: RPC program for NFSv4 callback service
5440 * @port: IP port number for NFS4 callback service
5441 * @cred: RPC credential to use for this call
5442 * @res: where to place the result
5444 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5446 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5447 unsigned short port
, struct rpc_cred
*cred
,
5448 struct nfs4_setclientid_res
*res
)
5450 nfs4_verifier sc_verifier
;
5451 struct nfs4_setclientid setclientid
= {
5452 .sc_verifier
= &sc_verifier
,
5456 struct rpc_message msg
= {
5457 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5458 .rpc_argp
= &setclientid
,
5462 struct rpc_task
*task
;
5463 struct rpc_task_setup task_setup_data
= {
5464 .rpc_client
= clp
->cl_rpcclient
,
5465 .rpc_message
= &msg
,
5466 .callback_ops
= &nfs4_setclientid_ops
,
5467 .callback_data
= &setclientid
,
5468 .flags
= RPC_TASK_TIMEOUT
,
5472 /* nfs_client_id4 */
5473 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5475 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5476 status
= nfs4_init_uniform_client_string(clp
);
5478 status
= nfs4_init_nonuniform_client_string(clp
);
5484 setclientid
.sc_netid_len
=
5485 nfs4_init_callback_netid(clp
,
5486 setclientid
.sc_netid
,
5487 sizeof(setclientid
.sc_netid
));
5488 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5489 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5490 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5492 dprintk("NFS call setclientid auth=%s, '%s'\n",
5493 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5495 task
= rpc_run_task(&task_setup_data
);
5497 status
= PTR_ERR(task
);
5500 status
= task
->tk_status
;
5501 if (setclientid
.sc_cred
) {
5502 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5503 put_rpccred(setclientid
.sc_cred
);
5507 trace_nfs4_setclientid(clp
, status
);
5508 dprintk("NFS reply setclientid: %d\n", status
);
5513 * nfs4_proc_setclientid_confirm - Confirm client ID
5514 * @clp: state data structure
5515 * @res: result of a previous SETCLIENTID
5516 * @cred: RPC credential to use for this call
5518 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5520 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5521 struct nfs4_setclientid_res
*arg
,
5522 struct rpc_cred
*cred
)
5524 struct rpc_message msg
= {
5525 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5531 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5532 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5534 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5535 trace_nfs4_setclientid_confirm(clp
, status
);
5536 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5540 struct nfs4_delegreturndata
{
5541 struct nfs4_delegreturnargs args
;
5542 struct nfs4_delegreturnres res
;
5544 nfs4_stateid stateid
;
5545 unsigned long timestamp
;
5547 struct nfs4_layoutreturn_args arg
;
5548 struct nfs4_layoutreturn_res res
;
5549 struct nfs4_xdr_opaque_data ld_private
;
5553 struct nfs_fattr fattr
;
5555 struct inode
*inode
;
5558 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5560 struct nfs4_delegreturndata
*data
= calldata
;
5562 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5565 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5567 /* Handle Layoutreturn errors */
5568 if (data
->args
.lr_args
&& task
->tk_status
!= 0) {
5569 switch(data
->res
.lr_ret
) {
5571 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5574 data
->args
.lr_args
= NULL
;
5575 data
->res
.lr_res
= NULL
;
5577 case -NFS4ERR_ADMIN_REVOKED
:
5578 case -NFS4ERR_DELEG_REVOKED
:
5579 case -NFS4ERR_EXPIRED
:
5580 case -NFS4ERR_BAD_STATEID
:
5581 case -NFS4ERR_OLD_STATEID
:
5582 case -NFS4ERR_UNKNOWN_LAYOUTTYPE
:
5583 case -NFS4ERR_WRONG_CRED
:
5584 data
->args
.lr_args
= NULL
;
5585 data
->res
.lr_res
= NULL
;
5586 data
->res
.lr_ret
= 0;
5587 rpc_restart_call_prepare(task
);
5592 switch (task
->tk_status
) {
5594 renew_lease(data
->res
.server
, data
->timestamp
);
5596 case -NFS4ERR_ADMIN_REVOKED
:
5597 case -NFS4ERR_DELEG_REVOKED
:
5598 case -NFS4ERR_EXPIRED
:
5599 nfs4_free_revoked_stateid(data
->res
.server
,
5601 task
->tk_msg
.rpc_cred
);
5602 case -NFS4ERR_BAD_STATEID
:
5603 case -NFS4ERR_OLD_STATEID
:
5604 case -NFS4ERR_STALE_STATEID
:
5605 task
->tk_status
= 0;
5607 case -NFS4ERR_ACCESS
:
5608 if (data
->args
.bitmask
) {
5609 data
->args
.bitmask
= NULL
;
5610 data
->res
.fattr
= NULL
;
5611 task
->tk_status
= 0;
5612 rpc_restart_call_prepare(task
);
5616 if (nfs4_async_handle_error(task
, data
->res
.server
,
5617 NULL
, NULL
) == -EAGAIN
) {
5618 rpc_restart_call_prepare(task
);
5622 data
->rpc_status
= task
->tk_status
;
5625 static void nfs4_delegreturn_release(void *calldata
)
5627 struct nfs4_delegreturndata
*data
= calldata
;
5628 struct inode
*inode
= data
->inode
;
5632 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
,
5634 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5635 nfs_iput_and_deactive(inode
);
5640 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5642 struct nfs4_delegreturndata
*d_data
;
5644 d_data
= (struct nfs4_delegreturndata
*)data
;
5646 if (!d_data
->lr
.roc
&& nfs4_wait_on_layoutreturn(d_data
->inode
, task
))
5649 nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
5650 &d_data
->args
.seq_args
,
5651 &d_data
->res
.seq_res
,
5655 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5656 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5657 .rpc_call_done
= nfs4_delegreturn_done
,
5658 .rpc_release
= nfs4_delegreturn_release
,
5661 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5663 struct nfs4_delegreturndata
*data
;
5664 struct nfs_server
*server
= NFS_SERVER(inode
);
5665 struct rpc_task
*task
;
5666 struct rpc_message msg
= {
5667 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5670 struct rpc_task_setup task_setup_data
= {
5671 .rpc_client
= server
->client
,
5672 .rpc_message
= &msg
,
5673 .callback_ops
= &nfs4_delegreturn_ops
,
5674 .flags
= RPC_TASK_ASYNC
,
5678 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5681 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5683 nfs4_state_protect(server
->nfs_client
,
5684 NFS_SP4_MACH_CRED_CLEANUP
,
5685 &task_setup_data
.rpc_client
, &msg
);
5687 data
->args
.fhandle
= &data
->fh
;
5688 data
->args
.stateid
= &data
->stateid
;
5689 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5690 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5691 nfs4_stateid_copy(&data
->stateid
, stateid
);
5692 data
->res
.fattr
= &data
->fattr
;
5693 data
->res
.server
= server
;
5694 data
->res
.lr_ret
= -NFS4ERR_NOMATCHING_LAYOUT
;
5695 data
->lr
.arg
.ld_private
= &data
->lr
.ld_private
;
5696 nfs_fattr_init(data
->res
.fattr
);
5697 data
->timestamp
= jiffies
;
5698 data
->rpc_status
= 0;
5699 data
->lr
.roc
= pnfs_roc(inode
, &data
->lr
.arg
, &data
->lr
.res
, cred
);
5700 data
->inode
= nfs_igrab_and_active(inode
);
5703 data
->args
.lr_args
= &data
->lr
.arg
;
5704 data
->res
.lr_res
= &data
->lr
.res
;
5706 } else if (data
->lr
.roc
) {
5707 pnfs_roc_release(&data
->lr
.arg
, &data
->lr
.res
, 0);
5708 data
->lr
.roc
= false;
5711 task_setup_data
.callback_data
= data
;
5712 msg
.rpc_argp
= &data
->args
;
5713 msg
.rpc_resp
= &data
->res
;
5714 task
= rpc_run_task(&task_setup_data
);
5716 return PTR_ERR(task
);
5719 status
= rpc_wait_for_completion_task(task
);
5722 status
= data
->rpc_status
;
5728 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5730 struct nfs_server
*server
= NFS_SERVER(inode
);
5731 struct nfs4_exception exception
= { };
5734 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5735 trace_nfs4_delegreturn(inode
, stateid
, err
);
5737 case -NFS4ERR_STALE_STATEID
:
5738 case -NFS4ERR_EXPIRED
:
5742 err
= nfs4_handle_exception(server
, err
, &exception
);
5743 } while (exception
.retry
);
5747 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5749 struct inode
*inode
= state
->inode
;
5750 struct nfs_server
*server
= NFS_SERVER(inode
);
5751 struct nfs_client
*clp
= server
->nfs_client
;
5752 struct nfs_lockt_args arg
= {
5753 .fh
= NFS_FH(inode
),
5756 struct nfs_lockt_res res
= {
5759 struct rpc_message msg
= {
5760 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5763 .rpc_cred
= state
->owner
->so_cred
,
5765 struct nfs4_lock_state
*lsp
;
5768 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5769 status
= nfs4_set_lock_state(state
, request
);
5772 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5773 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5774 arg
.lock_owner
.s_dev
= server
->s_dev
;
5775 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5778 request
->fl_type
= F_UNLCK
;
5780 case -NFS4ERR_DENIED
:
5783 request
->fl_ops
->fl_release_private(request
);
5784 request
->fl_ops
= NULL
;
5789 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5791 struct nfs4_exception exception
= { };
5795 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5796 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5797 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5799 } while (exception
.retry
);
5803 struct nfs4_unlockdata
{
5804 struct nfs_locku_args arg
;
5805 struct nfs_locku_res res
;
5806 struct nfs4_lock_state
*lsp
;
5807 struct nfs_open_context
*ctx
;
5808 struct file_lock fl
;
5809 struct nfs_server
*server
;
5810 unsigned long timestamp
;
5813 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5814 struct nfs_open_context
*ctx
,
5815 struct nfs4_lock_state
*lsp
,
5816 struct nfs_seqid
*seqid
)
5818 struct nfs4_unlockdata
*p
;
5819 struct inode
*inode
= lsp
->ls_state
->inode
;
5821 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5824 p
->arg
.fh
= NFS_FH(inode
);
5826 p
->arg
.seqid
= seqid
;
5827 p
->res
.seqid
= seqid
;
5829 atomic_inc(&lsp
->ls_count
);
5830 /* Ensure we don't close file until we're done freeing locks! */
5831 p
->ctx
= get_nfs_open_context(ctx
);
5832 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5833 p
->server
= NFS_SERVER(inode
);
5837 static void nfs4_locku_release_calldata(void *data
)
5839 struct nfs4_unlockdata
*calldata
= data
;
5840 nfs_free_seqid(calldata
->arg
.seqid
);
5841 nfs4_put_lock_state(calldata
->lsp
);
5842 put_nfs_open_context(calldata
->ctx
);
5846 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5848 struct nfs4_unlockdata
*calldata
= data
;
5850 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5852 switch (task
->tk_status
) {
5854 renew_lease(calldata
->server
, calldata
->timestamp
);
5855 locks_lock_inode_wait(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5856 if (nfs4_update_lock_stateid(calldata
->lsp
,
5857 &calldata
->res
.stateid
))
5859 case -NFS4ERR_ADMIN_REVOKED
:
5860 case -NFS4ERR_EXPIRED
:
5861 nfs4_free_revoked_stateid(calldata
->server
,
5862 &calldata
->arg
.stateid
,
5863 task
->tk_msg
.rpc_cred
);
5864 case -NFS4ERR_BAD_STATEID
:
5865 case -NFS4ERR_OLD_STATEID
:
5866 case -NFS4ERR_STALE_STATEID
:
5867 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5868 &calldata
->lsp
->ls_stateid
))
5869 rpc_restart_call_prepare(task
);
5872 if (nfs4_async_handle_error(task
, calldata
->server
,
5873 NULL
, NULL
) == -EAGAIN
)
5874 rpc_restart_call_prepare(task
);
5876 nfs_release_seqid(calldata
->arg
.seqid
);
5879 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5881 struct nfs4_unlockdata
*calldata
= data
;
5883 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5885 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5886 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5887 /* Note: exit _without_ running nfs4_locku_done */
5890 calldata
->timestamp
= jiffies
;
5891 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
5892 &calldata
->arg
.seq_args
,
5893 &calldata
->res
.seq_res
,
5895 nfs_release_seqid(calldata
->arg
.seqid
);
5898 task
->tk_action
= NULL
;
5900 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5903 static const struct rpc_call_ops nfs4_locku_ops
= {
5904 .rpc_call_prepare
= nfs4_locku_prepare
,
5905 .rpc_call_done
= nfs4_locku_done
,
5906 .rpc_release
= nfs4_locku_release_calldata
,
5909 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5910 struct nfs_open_context
*ctx
,
5911 struct nfs4_lock_state
*lsp
,
5912 struct nfs_seqid
*seqid
)
5914 struct nfs4_unlockdata
*data
;
5915 struct rpc_message msg
= {
5916 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5917 .rpc_cred
= ctx
->cred
,
5919 struct rpc_task_setup task_setup_data
= {
5920 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5921 .rpc_message
= &msg
,
5922 .callback_ops
= &nfs4_locku_ops
,
5923 .workqueue
= nfsiod_workqueue
,
5924 .flags
= RPC_TASK_ASYNC
,
5927 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5928 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5930 /* Ensure this is an unlock - when canceling a lock, the
5931 * canceled lock is passed in, and it won't be an unlock.
5933 fl
->fl_type
= F_UNLCK
;
5935 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5937 nfs_free_seqid(seqid
);
5938 return ERR_PTR(-ENOMEM
);
5941 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5942 msg
.rpc_argp
= &data
->arg
;
5943 msg
.rpc_resp
= &data
->res
;
5944 task_setup_data
.callback_data
= data
;
5945 return rpc_run_task(&task_setup_data
);
5948 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5950 struct inode
*inode
= state
->inode
;
5951 struct nfs4_state_owner
*sp
= state
->owner
;
5952 struct nfs_inode
*nfsi
= NFS_I(inode
);
5953 struct nfs_seqid
*seqid
;
5954 struct nfs4_lock_state
*lsp
;
5955 struct rpc_task
*task
;
5956 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
5958 unsigned char fl_flags
= request
->fl_flags
;
5960 status
= nfs4_set_lock_state(state
, request
);
5961 /* Unlock _before_ we do the RPC call */
5962 request
->fl_flags
|= FL_EXISTS
;
5963 /* Exclude nfs_delegation_claim_locks() */
5964 mutex_lock(&sp
->so_delegreturn_mutex
);
5965 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5966 down_read(&nfsi
->rwsem
);
5967 if (locks_lock_inode_wait(inode
, request
) == -ENOENT
) {
5968 up_read(&nfsi
->rwsem
);
5969 mutex_unlock(&sp
->so_delegreturn_mutex
);
5972 up_read(&nfsi
->rwsem
);
5973 mutex_unlock(&sp
->so_delegreturn_mutex
);
5976 /* Is this a delegated lock? */
5977 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5978 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5980 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
5981 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5985 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5986 status
= PTR_ERR(task
);
5989 status
= rpc_wait_for_completion_task(task
);
5992 request
->fl_flags
= fl_flags
;
5993 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
5997 struct nfs4_lockdata
{
5998 struct nfs_lock_args arg
;
5999 struct nfs_lock_res res
;
6000 struct nfs4_lock_state
*lsp
;
6001 struct nfs_open_context
*ctx
;
6002 struct file_lock fl
;
6003 unsigned long timestamp
;
6006 struct nfs_server
*server
;
6009 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
6010 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
6013 struct nfs4_lockdata
*p
;
6014 struct inode
*inode
= lsp
->ls_state
->inode
;
6015 struct nfs_server
*server
= NFS_SERVER(inode
);
6016 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
6018 p
= kzalloc(sizeof(*p
), gfp_mask
);
6022 p
->arg
.fh
= NFS_FH(inode
);
6024 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
6025 if (IS_ERR(p
->arg
.open_seqid
))
6027 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
6028 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
6029 if (IS_ERR(p
->arg
.lock_seqid
))
6030 goto out_free_seqid
;
6031 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6032 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6033 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
6034 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
6037 atomic_inc(&lsp
->ls_count
);
6038 p
->ctx
= get_nfs_open_context(ctx
);
6039 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
6042 nfs_free_seqid(p
->arg
.open_seqid
);
6048 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
6050 struct nfs4_lockdata
*data
= calldata
;
6051 struct nfs4_state
*state
= data
->lsp
->ls_state
;
6053 dprintk("%s: begin!\n", __func__
);
6054 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
6056 /* Do we need to do an open_to_lock_owner? */
6057 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
6058 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
6059 goto out_release_lock_seqid
;
6061 nfs4_stateid_copy(&data
->arg
.open_stateid
,
6062 &state
->open_stateid
);
6063 data
->arg
.new_lock_owner
= 1;
6064 data
->res
.open_seqid
= data
->arg
.open_seqid
;
6066 data
->arg
.new_lock_owner
= 0;
6067 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
6068 &data
->lsp
->ls_stateid
);
6070 if (!nfs4_valid_open_stateid(state
)) {
6071 data
->rpc_status
= -EBADF
;
6072 task
->tk_action
= NULL
;
6073 goto out_release_open_seqid
;
6075 data
->timestamp
= jiffies
;
6076 if (nfs4_setup_sequence(data
->server
->nfs_client
,
6077 &data
->arg
.seq_args
,
6081 out_release_open_seqid
:
6082 nfs_release_seqid(data
->arg
.open_seqid
);
6083 out_release_lock_seqid
:
6084 nfs_release_seqid(data
->arg
.lock_seqid
);
6086 nfs4_sequence_done(task
, &data
->res
.seq_res
);
6087 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
6090 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
6092 struct nfs4_lockdata
*data
= calldata
;
6093 struct nfs4_lock_state
*lsp
= data
->lsp
;
6095 dprintk("%s: begin!\n", __func__
);
6097 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
6100 data
->rpc_status
= task
->tk_status
;
6101 switch (task
->tk_status
) {
6103 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
6105 if (data
->arg
.new_lock
) {
6106 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
6107 if (locks_lock_inode_wait(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
6108 rpc_restart_call_prepare(task
);
6112 if (data
->arg
.new_lock_owner
!= 0) {
6113 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
6114 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
6115 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6116 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
6117 rpc_restart_call_prepare(task
);
6119 case -NFS4ERR_BAD_STATEID
:
6120 case -NFS4ERR_OLD_STATEID
:
6121 case -NFS4ERR_STALE_STATEID
:
6122 case -NFS4ERR_EXPIRED
:
6123 if (data
->arg
.new_lock_owner
!= 0) {
6124 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
6125 &lsp
->ls_state
->open_stateid
))
6126 rpc_restart_call_prepare(task
);
6127 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
6129 rpc_restart_call_prepare(task
);
6131 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
6134 static void nfs4_lock_release(void *calldata
)
6136 struct nfs4_lockdata
*data
= calldata
;
6138 dprintk("%s: begin!\n", __func__
);
6139 nfs_free_seqid(data
->arg
.open_seqid
);
6140 if (data
->cancelled
!= 0) {
6141 struct rpc_task
*task
;
6142 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
6143 data
->arg
.lock_seqid
);
6145 rpc_put_task_async(task
);
6146 dprintk("%s: cancelling lock!\n", __func__
);
6148 nfs_free_seqid(data
->arg
.lock_seqid
);
6149 nfs4_put_lock_state(data
->lsp
);
6150 put_nfs_open_context(data
->ctx
);
6152 dprintk("%s: done!\n", __func__
);
6155 static const struct rpc_call_ops nfs4_lock_ops
= {
6156 .rpc_call_prepare
= nfs4_lock_prepare
,
6157 .rpc_call_done
= nfs4_lock_done
,
6158 .rpc_release
= nfs4_lock_release
,
6161 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
6164 case -NFS4ERR_ADMIN_REVOKED
:
6165 case -NFS4ERR_EXPIRED
:
6166 case -NFS4ERR_BAD_STATEID
:
6167 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6168 if (new_lock_owner
!= 0 ||
6169 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
6170 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
6172 case -NFS4ERR_STALE_STATEID
:
6173 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
6174 nfs4_schedule_lease_recovery(server
->nfs_client
);
6178 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
6180 struct nfs4_lockdata
*data
;
6181 struct rpc_task
*task
;
6182 struct rpc_message msg
= {
6183 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
6184 .rpc_cred
= state
->owner
->so_cred
,
6186 struct rpc_task_setup task_setup_data
= {
6187 .rpc_client
= NFS_CLIENT(state
->inode
),
6188 .rpc_message
= &msg
,
6189 .callback_ops
= &nfs4_lock_ops
,
6190 .workqueue
= nfsiod_workqueue
,
6191 .flags
= RPC_TASK_ASYNC
,
6195 dprintk("%s: begin!\n", __func__
);
6196 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
6197 fl
->fl_u
.nfs4_fl
.owner
,
6198 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
6202 data
->arg
.block
= 1;
6203 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
6204 msg
.rpc_argp
= &data
->arg
;
6205 msg
.rpc_resp
= &data
->res
;
6206 task_setup_data
.callback_data
= data
;
6207 if (recovery_type
> NFS_LOCK_NEW
) {
6208 if (recovery_type
== NFS_LOCK_RECLAIM
)
6209 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
6210 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
6212 data
->arg
.new_lock
= 1;
6213 task
= rpc_run_task(&task_setup_data
);
6215 return PTR_ERR(task
);
6216 ret
= rpc_wait_for_completion_task(task
);
6218 ret
= data
->rpc_status
;
6220 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
6221 data
->arg
.new_lock_owner
, ret
);
6223 data
->cancelled
= 1;
6225 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
6226 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
6230 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
6232 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6233 struct nfs4_exception exception
= {
6234 .inode
= state
->inode
,
6239 /* Cache the lock if possible... */
6240 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6242 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
6243 if (err
!= -NFS4ERR_DELAY
)
6245 nfs4_handle_exception(server
, err
, &exception
);
6246 } while (exception
.retry
);
6250 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6252 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6253 struct nfs4_exception exception
= {
6254 .inode
= state
->inode
,
6258 err
= nfs4_set_lock_state(state
, request
);
6261 if (!recover_lost_locks
) {
6262 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6266 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6268 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6272 case -NFS4ERR_GRACE
:
6273 case -NFS4ERR_DELAY
:
6274 nfs4_handle_exception(server
, err
, &exception
);
6277 } while (exception
.retry
);
6282 #if defined(CONFIG_NFS_V4_1)
6283 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6285 struct nfs4_lock_state
*lsp
;
6288 status
= nfs4_set_lock_state(state
, request
);
6291 lsp
= request
->fl_u
.nfs4_fl
.owner
;
6292 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) ||
6293 test_bit(NFS_LOCK_LOST
, &lsp
->ls_flags
))
6295 status
= nfs4_lock_expired(state
, request
);
6300 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6302 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6303 struct nfs4_state_owner
*sp
= state
->owner
;
6304 unsigned char fl_flags
= request
->fl_flags
;
6307 request
->fl_flags
|= FL_ACCESS
;
6308 status
= locks_lock_inode_wait(state
->inode
, request
);
6311 mutex_lock(&sp
->so_delegreturn_mutex
);
6312 down_read(&nfsi
->rwsem
);
6313 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6314 /* Yes: cache locks! */
6315 /* ...but avoid races with delegation recall... */
6316 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6317 status
= locks_lock_inode_wait(state
->inode
, request
);
6318 up_read(&nfsi
->rwsem
);
6319 mutex_unlock(&sp
->so_delegreturn_mutex
);
6322 up_read(&nfsi
->rwsem
);
6323 mutex_unlock(&sp
->so_delegreturn_mutex
);
6324 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6326 request
->fl_flags
= fl_flags
;
6330 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6332 struct nfs4_exception exception
= {
6334 .inode
= state
->inode
,
6339 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6340 if (err
== -NFS4ERR_DENIED
)
6342 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6344 } while (exception
.retry
);
6348 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6349 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6352 nfs4_retry_setlk_simple(struct nfs4_state
*state
, int cmd
,
6353 struct file_lock
*request
)
6355 int status
= -ERESTARTSYS
;
6356 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6358 while(!signalled()) {
6359 status
= nfs4_proc_setlk(state
, cmd
, request
);
6360 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6362 freezable_schedule_timeout_interruptible(timeout
);
6364 timeout
= min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT
, timeout
);
6365 status
= -ERESTARTSYS
;
6370 #ifdef CONFIG_NFS_V4_1
6371 struct nfs4_lock_waiter
{
6372 struct task_struct
*task
;
6373 struct inode
*inode
;
6374 struct nfs_lowner
*owner
;
6379 nfs4_wake_lock_waiter(wait_queue_t
*wait
, unsigned int mode
, int flags
, void *key
)
6382 struct cb_notify_lock_args
*cbnl
= key
;
6383 struct nfs4_lock_waiter
*waiter
= wait
->private;
6384 struct nfs_lowner
*lowner
= &cbnl
->cbnl_owner
,
6385 *wowner
= waiter
->owner
;
6387 /* Only wake if the callback was for the same owner */
6388 if (lowner
->clientid
!= wowner
->clientid
||
6389 lowner
->id
!= wowner
->id
||
6390 lowner
->s_dev
!= wowner
->s_dev
)
6393 /* Make sure it's for the right inode */
6394 if (nfs_compare_fh(NFS_FH(waiter
->inode
), &cbnl
->cbnl_fh
))
6397 waiter
->notified
= true;
6399 /* override "private" so we can use default_wake_function */
6400 wait
->private = waiter
->task
;
6401 ret
= autoremove_wake_function(wait
, mode
, flags
, key
);
6402 wait
->private = waiter
;
6407 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6409 int status
= -ERESTARTSYS
;
6410 unsigned long flags
;
6411 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
6412 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6413 struct nfs_client
*clp
= server
->nfs_client
;
6414 wait_queue_head_t
*q
= &clp
->cl_lock_waitq
;
6415 struct nfs_lowner owner
= { .clientid
= clp
->cl_clientid
,
6416 .id
= lsp
->ls_seqid
.owner_id
,
6417 .s_dev
= server
->s_dev
};
6418 struct nfs4_lock_waiter waiter
= { .task
= current
,
6419 .inode
= state
->inode
,
6421 .notified
= false };
6424 /* Don't bother with waitqueue if we don't expect a callback */
6425 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK
, &state
->flags
))
6426 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6429 wait
.private = &waiter
;
6430 wait
.func
= nfs4_wake_lock_waiter
;
6431 add_wait_queue(q
, &wait
);
6433 while(!signalled()) {
6434 status
= nfs4_proc_setlk(state
, cmd
, request
);
6435 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6438 status
= -ERESTARTSYS
;
6439 spin_lock_irqsave(&q
->lock
, flags
);
6440 if (waiter
.notified
) {
6441 spin_unlock_irqrestore(&q
->lock
, flags
);
6444 set_current_state(TASK_INTERRUPTIBLE
);
6445 spin_unlock_irqrestore(&q
->lock
, flags
);
6447 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT
);
6450 finish_wait(q
, &wait
);
6453 #else /* !CONFIG_NFS_V4_1 */
6455 nfs4_retry_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6457 return nfs4_retry_setlk_simple(state
, cmd
, request
);
6462 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6464 struct nfs_open_context
*ctx
;
6465 struct nfs4_state
*state
;
6468 /* verify open state */
6469 ctx
= nfs_file_open_context(filp
);
6472 if (request
->fl_start
< 0 || request
->fl_end
< 0)
6475 if (IS_GETLK(cmd
)) {
6477 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6481 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6484 if (request
->fl_type
== F_UNLCK
) {
6486 return nfs4_proc_unlck(state
, cmd
, request
);
6493 if ((request
->fl_flags
& FL_POSIX
) &&
6494 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6498 * Don't rely on the VFS having checked the file open mode,
6499 * since it won't do this for flock() locks.
6501 switch (request
->fl_type
) {
6503 if (!(filp
->f_mode
& FMODE_READ
))
6507 if (!(filp
->f_mode
& FMODE_WRITE
))
6511 status
= nfs4_set_lock_state(state
, request
);
6515 return nfs4_retry_setlk(state
, cmd
, request
);
6518 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6520 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6523 err
= nfs4_set_lock_state(state
, fl
);
6526 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6527 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6530 struct nfs_release_lockowner_data
{
6531 struct nfs4_lock_state
*lsp
;
6532 struct nfs_server
*server
;
6533 struct nfs_release_lockowner_args args
;
6534 struct nfs_release_lockowner_res res
;
6535 unsigned long timestamp
;
6538 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6540 struct nfs_release_lockowner_data
*data
= calldata
;
6541 struct nfs_server
*server
= data
->server
;
6542 nfs4_setup_sequence(server
->nfs_client
, &data
->args
.seq_args
,
6543 &data
->res
.seq_res
, task
);
6544 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6545 data
->timestamp
= jiffies
;
6548 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6550 struct nfs_release_lockowner_data
*data
= calldata
;
6551 struct nfs_server
*server
= data
->server
;
6553 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6555 switch (task
->tk_status
) {
6557 renew_lease(server
, data
->timestamp
);
6559 case -NFS4ERR_STALE_CLIENTID
:
6560 case -NFS4ERR_EXPIRED
:
6561 nfs4_schedule_lease_recovery(server
->nfs_client
);
6563 case -NFS4ERR_LEASE_MOVED
:
6564 case -NFS4ERR_DELAY
:
6565 if (nfs4_async_handle_error(task
, server
,
6566 NULL
, NULL
) == -EAGAIN
)
6567 rpc_restart_call_prepare(task
);
6571 static void nfs4_release_lockowner_release(void *calldata
)
6573 struct nfs_release_lockowner_data
*data
= calldata
;
6574 nfs4_free_lock_state(data
->server
, data
->lsp
);
6578 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6579 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6580 .rpc_call_done
= nfs4_release_lockowner_done
,
6581 .rpc_release
= nfs4_release_lockowner_release
,
6585 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6587 struct nfs_release_lockowner_data
*data
;
6588 struct rpc_message msg
= {
6589 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6592 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6595 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6599 data
->server
= server
;
6600 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6601 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6602 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6604 msg
.rpc_argp
= &data
->args
;
6605 msg
.rpc_resp
= &data
->res
;
6606 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6607 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6610 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6612 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6613 struct dentry
*unused
, struct inode
*inode
,
6614 const char *key
, const void *buf
,
6615 size_t buflen
, int flags
)
6617 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6620 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6621 struct dentry
*unused
, struct inode
*inode
,
6622 const char *key
, void *buf
, size_t buflen
)
6624 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6627 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6629 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6632 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6634 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6635 struct dentry
*unused
, struct inode
*inode
,
6636 const char *key
, const void *buf
,
6637 size_t buflen
, int flags
)
6639 if (security_ismaclabel(key
))
6640 return nfs4_set_security_label(inode
, buf
, buflen
);
6645 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6646 struct dentry
*unused
, struct inode
*inode
,
6647 const char *key
, void *buf
, size_t buflen
)
6649 if (security_ismaclabel(key
))
6650 return nfs4_get_security_label(inode
, buf
, buflen
);
6655 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6659 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6660 len
= security_inode_listsecurity(inode
, list
, list_len
);
6661 if (list_len
&& len
> list_len
)
6667 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6668 .prefix
= XATTR_SECURITY_PREFIX
,
6669 .get
= nfs4_xattr_get_nfs4_label
,
6670 .set
= nfs4_xattr_set_nfs4_label
,
6676 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6684 * nfs_fhget will use either the mounted_on_fileid or the fileid
6686 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6688 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6689 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6690 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6691 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6694 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6695 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6696 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6700 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6701 const struct qstr
*name
,
6702 struct nfs4_fs_locations
*fs_locations
,
6705 struct nfs_server
*server
= NFS_SERVER(dir
);
6707 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6709 struct nfs4_fs_locations_arg args
= {
6710 .dir_fh
= NFS_FH(dir
),
6715 struct nfs4_fs_locations_res res
= {
6716 .fs_locations
= fs_locations
,
6718 struct rpc_message msg
= {
6719 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6725 dprintk("%s: start\n", __func__
);
6727 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6728 * is not supported */
6729 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6730 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6732 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6734 nfs_fattr_init(&fs_locations
->fattr
);
6735 fs_locations
->server
= server
;
6736 fs_locations
->nlocations
= 0;
6737 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6738 dprintk("%s: returned status = %d\n", __func__
, status
);
6742 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6743 const struct qstr
*name
,
6744 struct nfs4_fs_locations
*fs_locations
,
6747 struct nfs4_exception exception
= { };
6750 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6751 fs_locations
, page
);
6752 trace_nfs4_get_fs_locations(dir
, name
, err
);
6753 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6755 } while (exception
.retry
);
6760 * This operation also signals the server that this client is
6761 * performing migration recovery. The server can stop returning
6762 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6763 * appended to this compound to identify the client ID which is
6764 * performing recovery.
6766 static int _nfs40_proc_get_locations(struct inode
*inode
,
6767 struct nfs4_fs_locations
*locations
,
6768 struct page
*page
, struct rpc_cred
*cred
)
6770 struct nfs_server
*server
= NFS_SERVER(inode
);
6771 struct rpc_clnt
*clnt
= server
->client
;
6773 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6775 struct nfs4_fs_locations_arg args
= {
6776 .clientid
= server
->nfs_client
->cl_clientid
,
6777 .fh
= NFS_FH(inode
),
6780 .migration
= 1, /* skip LOOKUP */
6781 .renew
= 1, /* append RENEW */
6783 struct nfs4_fs_locations_res res
= {
6784 .fs_locations
= locations
,
6788 struct rpc_message msg
= {
6789 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6794 unsigned long now
= jiffies
;
6797 nfs_fattr_init(&locations
->fattr
);
6798 locations
->server
= server
;
6799 locations
->nlocations
= 0;
6801 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6802 nfs4_set_sequence_privileged(&args
.seq_args
);
6803 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6804 &args
.seq_args
, &res
.seq_res
);
6808 renew_lease(server
, now
);
6812 #ifdef CONFIG_NFS_V4_1
6815 * This operation also signals the server that this client is
6816 * performing migration recovery. The server can stop asserting
6817 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6818 * performing this operation is identified in the SEQUENCE
6819 * operation in this compound.
6821 * When the client supports GETATTR(fs_locations_info), it can
6822 * be plumbed in here.
6824 static int _nfs41_proc_get_locations(struct inode
*inode
,
6825 struct nfs4_fs_locations
*locations
,
6826 struct page
*page
, struct rpc_cred
*cred
)
6828 struct nfs_server
*server
= NFS_SERVER(inode
);
6829 struct rpc_clnt
*clnt
= server
->client
;
6831 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6833 struct nfs4_fs_locations_arg args
= {
6834 .fh
= NFS_FH(inode
),
6837 .migration
= 1, /* skip LOOKUP */
6839 struct nfs4_fs_locations_res res
= {
6840 .fs_locations
= locations
,
6843 struct rpc_message msg
= {
6844 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6851 nfs_fattr_init(&locations
->fattr
);
6852 locations
->server
= server
;
6853 locations
->nlocations
= 0;
6855 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6856 nfs4_set_sequence_privileged(&args
.seq_args
);
6857 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6858 &args
.seq_args
, &res
.seq_res
);
6859 if (status
== NFS4_OK
&&
6860 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6861 status
= -NFS4ERR_LEASE_MOVED
;
6865 #endif /* CONFIG_NFS_V4_1 */
6868 * nfs4_proc_get_locations - discover locations for a migrated FSID
6869 * @inode: inode on FSID that is migrating
6870 * @locations: result of query
6872 * @cred: credential to use for this operation
6874 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6875 * operation failed, or a negative errno if a local error occurred.
6877 * On success, "locations" is filled in, but if the server has
6878 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6881 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6882 * from this client that require migration recovery.
6884 int nfs4_proc_get_locations(struct inode
*inode
,
6885 struct nfs4_fs_locations
*locations
,
6886 struct page
*page
, struct rpc_cred
*cred
)
6888 struct nfs_server
*server
= NFS_SERVER(inode
);
6889 struct nfs_client
*clp
= server
->nfs_client
;
6890 const struct nfs4_mig_recovery_ops
*ops
=
6891 clp
->cl_mvops
->mig_recovery_ops
;
6892 struct nfs4_exception exception
= { };
6895 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6896 (unsigned long long)server
->fsid
.major
,
6897 (unsigned long long)server
->fsid
.minor
,
6899 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6902 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6903 if (status
!= -NFS4ERR_DELAY
)
6905 nfs4_handle_exception(server
, status
, &exception
);
6906 } while (exception
.retry
);
6911 * This operation also signals the server that this client is
6912 * performing "lease moved" recovery. The server can stop
6913 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6914 * is appended to this compound to identify the client ID which is
6915 * performing recovery.
6917 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6919 struct nfs_server
*server
= NFS_SERVER(inode
);
6920 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
6921 struct rpc_clnt
*clnt
= server
->client
;
6922 struct nfs4_fsid_present_arg args
= {
6923 .fh
= NFS_FH(inode
),
6924 .clientid
= clp
->cl_clientid
,
6925 .renew
= 1, /* append RENEW */
6927 struct nfs4_fsid_present_res res
= {
6930 struct rpc_message msg
= {
6931 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6936 unsigned long now
= jiffies
;
6939 res
.fh
= nfs_alloc_fhandle();
6943 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6944 nfs4_set_sequence_privileged(&args
.seq_args
);
6945 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6946 &args
.seq_args
, &res
.seq_res
);
6947 nfs_free_fhandle(res
.fh
);
6951 do_renew_lease(clp
, now
);
6955 #ifdef CONFIG_NFS_V4_1
6958 * This operation also signals the server that this client is
6959 * performing "lease moved" recovery. The server can stop asserting
6960 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6961 * this operation is identified in the SEQUENCE operation in this
6964 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6966 struct nfs_server
*server
= NFS_SERVER(inode
);
6967 struct rpc_clnt
*clnt
= server
->client
;
6968 struct nfs4_fsid_present_arg args
= {
6969 .fh
= NFS_FH(inode
),
6971 struct nfs4_fsid_present_res res
= {
6973 struct rpc_message msg
= {
6974 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6981 res
.fh
= nfs_alloc_fhandle();
6985 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6986 nfs4_set_sequence_privileged(&args
.seq_args
);
6987 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6988 &args
.seq_args
, &res
.seq_res
);
6989 nfs_free_fhandle(res
.fh
);
6990 if (status
== NFS4_OK
&&
6991 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6992 status
= -NFS4ERR_LEASE_MOVED
;
6996 #endif /* CONFIG_NFS_V4_1 */
6999 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7000 * @inode: inode on FSID to check
7001 * @cred: credential to use for this operation
7003 * Server indicates whether the FSID is present, moved, or not
7004 * recognized. This operation is necessary to clear a LEASE_MOVED
7005 * condition for this client ID.
7007 * Returns NFS4_OK if the FSID is present on this server,
7008 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7009 * NFS4ERR code if some error occurred on the server, or a
7010 * negative errno if a local failure occurred.
7012 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
7014 struct nfs_server
*server
= NFS_SERVER(inode
);
7015 struct nfs_client
*clp
= server
->nfs_client
;
7016 const struct nfs4_mig_recovery_ops
*ops
=
7017 clp
->cl_mvops
->mig_recovery_ops
;
7018 struct nfs4_exception exception
= { };
7021 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
7022 (unsigned long long)server
->fsid
.major
,
7023 (unsigned long long)server
->fsid
.minor
,
7025 nfs_display_fhandle(NFS_FH(inode
), __func__
);
7028 status
= ops
->fsid_present(inode
, cred
);
7029 if (status
!= -NFS4ERR_DELAY
)
7031 nfs4_handle_exception(server
, status
, &exception
);
7032 } while (exception
.retry
);
7037 * If 'use_integrity' is true and the state managment nfs_client
7038 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7039 * and the machine credential as per RFC3530bis and RFC5661 Security
7040 * Considerations sections. Otherwise, just use the user cred with the
7041 * filesystem's rpc_client.
7043 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7046 struct nfs4_secinfo_arg args
= {
7047 .dir_fh
= NFS_FH(dir
),
7050 struct nfs4_secinfo_res res
= {
7053 struct rpc_message msg
= {
7054 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
7058 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
7059 struct rpc_cred
*cred
= NULL
;
7061 if (use_integrity
) {
7062 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
7063 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
7064 msg
.rpc_cred
= cred
;
7067 dprintk("NFS call secinfo %s\n", name
->name
);
7069 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
7070 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
7072 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
7074 dprintk("NFS reply secinfo: %d\n", status
);
7082 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
7083 struct nfs4_secinfo_flavors
*flavors
)
7085 struct nfs4_exception exception
= { };
7088 err
= -NFS4ERR_WRONGSEC
;
7090 /* try to use integrity protection with machine cred */
7091 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
7092 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
7095 * if unable to use integrity protection, or SECINFO with
7096 * integrity protection returns NFS4ERR_WRONGSEC (which is
7097 * disallowed by spec, but exists in deployed servers) use
7098 * the current filesystem's rpc_client and the user cred.
7100 if (err
== -NFS4ERR_WRONGSEC
)
7101 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
7103 trace_nfs4_secinfo(dir
, name
, err
);
7104 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
7106 } while (exception
.retry
);
7110 #ifdef CONFIG_NFS_V4_1
7112 * Check the exchange flags returned by the server for invalid flags, having
7113 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7116 static int nfs4_check_cl_exchange_flags(u32 flags
)
7118 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
7120 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
7121 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
7123 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
7127 return -NFS4ERR_INVAL
;
7131 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
7132 struct nfs41_server_scope
*b
)
7134 if (a
->server_scope_sz
== b
->server_scope_sz
&&
7135 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
7142 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
7146 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
7147 .rpc_call_done
= &nfs4_bind_one_conn_to_session_done
,
7151 * nfs4_proc_bind_one_conn_to_session()
7153 * The 4.1 client currently uses the same TCP connection for the
7154 * fore and backchannel.
7157 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
7158 struct rpc_xprt
*xprt
,
7159 struct nfs_client
*clp
,
7160 struct rpc_cred
*cred
)
7163 struct nfs41_bind_conn_to_session_args args
= {
7165 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
7167 struct nfs41_bind_conn_to_session_res res
;
7168 struct rpc_message msg
= {
7170 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
7175 struct rpc_task_setup task_setup_data
= {
7178 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
7179 .rpc_message
= &msg
,
7180 .flags
= RPC_TASK_TIMEOUT
,
7182 struct rpc_task
*task
;
7184 dprintk("--> %s\n", __func__
);
7186 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
7187 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
7188 args
.dir
= NFS4_CDFC4_FORE
;
7190 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7191 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
7192 args
.dir
= NFS4_CDFC4_FORE
;
7194 task
= rpc_run_task(&task_setup_data
);
7195 if (!IS_ERR(task
)) {
7196 status
= task
->tk_status
;
7199 status
= PTR_ERR(task
);
7200 trace_nfs4_bind_conn_to_session(clp
, status
);
7202 if (memcmp(res
.sessionid
.data
,
7203 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
7204 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
7208 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
7209 dprintk("NFS: %s: Unexpected direction from server\n",
7214 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
7215 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7222 dprintk("<-- %s status= %d\n", __func__
, status
);
7226 struct rpc_bind_conn_calldata
{
7227 struct nfs_client
*clp
;
7228 struct rpc_cred
*cred
;
7232 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
7233 struct rpc_xprt
*xprt
,
7236 struct rpc_bind_conn_calldata
*p
= calldata
;
7238 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
7241 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7243 struct rpc_bind_conn_calldata data
= {
7247 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
7248 nfs4_proc_bind_conn_to_session_callback
, &data
);
7252 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7253 * and operations we'd like to see to enable certain features in the allow map
7255 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
7256 .how
= SP4_MACH_CRED
,
7257 .enforce
.u
.words
= {
7258 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7259 1 << (OP_EXCHANGE_ID
- 32) |
7260 1 << (OP_CREATE_SESSION
- 32) |
7261 1 << (OP_DESTROY_SESSION
- 32) |
7262 1 << (OP_DESTROY_CLIENTID
- 32)
7265 [0] = 1 << (OP_CLOSE
) |
7266 1 << (OP_OPEN_DOWNGRADE
) |
7268 1 << (OP_DELEGRETURN
) |
7270 [1] = 1 << (OP_SECINFO
- 32) |
7271 1 << (OP_SECINFO_NO_NAME
- 32) |
7272 1 << (OP_LAYOUTRETURN
- 32) |
7273 1 << (OP_TEST_STATEID
- 32) |
7274 1 << (OP_FREE_STATEID
- 32) |
7275 1 << (OP_WRITE
- 32)
7280 * Select the state protection mode for client `clp' given the server results
7281 * from exchange_id in `sp'.
7283 * Returns 0 on success, negative errno otherwise.
7285 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
7286 struct nfs41_state_protection
*sp
)
7288 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
7289 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
7290 1 << (OP_EXCHANGE_ID
- 32) |
7291 1 << (OP_CREATE_SESSION
- 32) |
7292 1 << (OP_DESTROY_SESSION
- 32) |
7293 1 << (OP_DESTROY_CLIENTID
- 32)
7297 if (sp
->how
== SP4_MACH_CRED
) {
7298 /* Print state protect result */
7299 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
7300 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
7301 if (test_bit(i
, sp
->enforce
.u
.longs
))
7302 dfprintk(MOUNT
, " enforce op %d\n", i
);
7303 if (test_bit(i
, sp
->allow
.u
.longs
))
7304 dfprintk(MOUNT
, " allow op %d\n", i
);
7307 /* make sure nothing is on enforce list that isn't supported */
7308 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
7309 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
7310 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7316 * Minimal mode - state operations are allowed to use machine
7317 * credential. Note this already happens by default, so the
7318 * client doesn't have to do anything more than the negotiation.
7320 * NOTE: we don't care if EXCHANGE_ID is in the list -
7321 * we're already using the machine cred for exchange_id
7322 * and will never use a different cred.
7324 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
7325 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
7326 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
7327 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
7328 dfprintk(MOUNT
, "sp4_mach_cred:\n");
7329 dfprintk(MOUNT
, " minimal mode enabled\n");
7330 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
7332 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7336 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7337 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7338 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7339 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7340 dfprintk(MOUNT
, " cleanup mode enabled\n");
7341 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
7344 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7345 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7346 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
7347 &clp
->cl_sp4_flags
);
7350 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7351 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7352 dfprintk(MOUNT
, " secinfo mode enabled\n");
7353 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
7356 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7357 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7358 dfprintk(MOUNT
, " stateid mode enabled\n");
7359 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
7362 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7363 dfprintk(MOUNT
, " write mode enabled\n");
7364 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
7367 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7368 dfprintk(MOUNT
, " commit mode enabled\n");
7369 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
7376 struct nfs41_exchange_id_data
{
7377 struct nfs41_exchange_id_res res
;
7378 struct nfs41_exchange_id_args args
;
7379 struct rpc_xprt
*xprt
;
7383 static void nfs4_exchange_id_done(struct rpc_task
*task
, void *data
)
7385 struct nfs41_exchange_id_data
*cdata
=
7386 (struct nfs41_exchange_id_data
*)data
;
7387 struct nfs_client
*clp
= cdata
->args
.client
;
7388 int status
= task
->tk_status
;
7390 trace_nfs4_exchange_id(clp
, status
);
7393 status
= nfs4_check_cl_exchange_flags(cdata
->res
.flags
);
7395 if (cdata
->xprt
&& status
== 0) {
7396 status
= nfs4_detect_session_trunking(clp
, &cdata
->res
,
7402 status
= nfs4_sp4_select_mode(clp
, &cdata
->res
.state_protect
);
7405 clp
->cl_clientid
= cdata
->res
.clientid
;
7406 clp
->cl_exchange_flags
= cdata
->res
.flags
;
7407 /* Client ID is not confirmed */
7408 if (!(cdata
->res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
)) {
7409 clear_bit(NFS4_SESSION_ESTABLISHED
,
7410 &clp
->cl_session
->session_state
);
7411 clp
->cl_seqid
= cdata
->res
.seqid
;
7414 kfree(clp
->cl_serverowner
);
7415 clp
->cl_serverowner
= cdata
->res
.server_owner
;
7416 cdata
->res
.server_owner
= NULL
;
7418 /* use the most recent implementation id */
7419 kfree(clp
->cl_implid
);
7420 clp
->cl_implid
= cdata
->res
.impl_id
;
7421 cdata
->res
.impl_id
= NULL
;
7423 if (clp
->cl_serverscope
!= NULL
&&
7424 !nfs41_same_server_scope(clp
->cl_serverscope
,
7425 cdata
->res
.server_scope
)) {
7426 dprintk("%s: server_scope mismatch detected\n",
7428 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7429 kfree(clp
->cl_serverscope
);
7430 clp
->cl_serverscope
= NULL
;
7433 if (clp
->cl_serverscope
== NULL
) {
7434 clp
->cl_serverscope
= cdata
->res
.server_scope
;
7435 cdata
->res
.server_scope
= NULL
;
7437 /* Save the EXCHANGE_ID verifier session trunk tests */
7438 memcpy(clp
->cl_confirm
.data
, cdata
->args
.verifier
->data
,
7439 sizeof(clp
->cl_confirm
.data
));
7442 cdata
->rpc_status
= status
;
7446 static void nfs4_exchange_id_release(void *data
)
7448 struct nfs41_exchange_id_data
*cdata
=
7449 (struct nfs41_exchange_id_data
*)data
;
7451 nfs_put_client(cdata
->args
.client
);
7453 xprt_put(cdata
->xprt
);
7454 rpc_clnt_xprt_switch_put(cdata
->args
.client
->cl_rpcclient
);
7456 kfree(cdata
->res
.impl_id
);
7457 kfree(cdata
->res
.server_scope
);
7458 kfree(cdata
->res
.server_owner
);
7462 static const struct rpc_call_ops nfs4_exchange_id_call_ops
= {
7463 .rpc_call_done
= nfs4_exchange_id_done
,
7464 .rpc_release
= nfs4_exchange_id_release
,
7468 * _nfs4_proc_exchange_id()
7470 * Wrapper for EXCHANGE_ID operation.
7472 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7473 u32 sp4_how
, struct rpc_xprt
*xprt
)
7475 nfs4_verifier verifier
;
7476 struct rpc_message msg
= {
7477 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7480 struct rpc_task_setup task_setup_data
= {
7481 .rpc_client
= clp
->cl_rpcclient
,
7482 .callback_ops
= &nfs4_exchange_id_call_ops
,
7483 .rpc_message
= &msg
,
7484 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7486 struct nfs41_exchange_id_data
*calldata
;
7487 struct rpc_task
*task
;
7490 if (!atomic_inc_not_zero(&clp
->cl_count
))
7494 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7499 nfs4_init_boot_verifier(clp
, &verifier
);
7501 status
= nfs4_init_uniform_client_string(clp
);
7505 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7506 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
7509 calldata
->res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7512 if (unlikely(calldata
->res
.server_owner
== NULL
))
7515 calldata
->res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7517 if (unlikely(calldata
->res
.server_scope
== NULL
))
7518 goto out_server_owner
;
7520 calldata
->res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7521 if (unlikely(calldata
->res
.impl_id
== NULL
))
7522 goto out_server_scope
;
7526 calldata
->args
.state_protect
.how
= SP4_NONE
;
7530 calldata
->args
.state_protect
= nfs4_sp4_mach_cred_request
;
7540 calldata
->xprt
= xprt
;
7541 task_setup_data
.rpc_xprt
= xprt
;
7542 task_setup_data
.flags
=
7543 RPC_TASK_SOFT
|RPC_TASK_SOFTCONN
|RPC_TASK_ASYNC
;
7544 calldata
->args
.verifier
= &clp
->cl_confirm
;
7546 calldata
->args
.verifier
= &verifier
;
7548 calldata
->args
.client
= clp
;
7549 #ifdef CONFIG_NFS_V4_1_MIGRATION
7550 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7551 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
7552 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
7554 calldata
->args
.flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7555 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
7557 msg
.rpc_argp
= &calldata
->args
;
7558 msg
.rpc_resp
= &calldata
->res
;
7559 task_setup_data
.callback_data
= calldata
;
7561 task
= rpc_run_task(&task_setup_data
);
7563 status
= PTR_ERR(task
);
7568 status
= rpc_wait_for_completion_task(task
);
7570 status
= calldata
->rpc_status
;
7571 } else /* session trunking test */
7572 status
= calldata
->rpc_status
;
7576 if (clp
->cl_implid
!= NULL
)
7577 dprintk("NFS reply exchange_id: Server Implementation ID: "
7578 "domain: %s, name: %s, date: %llu,%u\n",
7579 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
7580 clp
->cl_implid
->date
.seconds
,
7581 clp
->cl_implid
->date
.nseconds
);
7582 dprintk("NFS reply exchange_id: %d\n", status
);
7586 kfree(calldata
->res
.impl_id
);
7588 kfree(calldata
->res
.server_scope
);
7590 kfree(calldata
->res
.server_owner
);
7597 * nfs4_proc_exchange_id()
7599 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7601 * Since the clientid has expired, all compounds using sessions
7602 * associated with the stale clientid will be returning
7603 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7604 * be in some phase of session reset.
7606 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7608 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7610 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7613 /* try SP4_MACH_CRED if krb5i/p */
7614 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7615 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7616 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
, NULL
);
7622 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
, NULL
);
7626 * nfs4_test_session_trunk
7628 * This is an add_xprt_test() test function called from
7629 * rpc_clnt_setup_test_and_add_xprt.
7631 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7632 * and is dereferrenced in nfs4_exchange_id_release
7634 * Upon success, add the new transport to the rpc_clnt
7636 * @clnt: struct rpc_clnt to get new transport
7637 * @xprt: the rpc_xprt to test
7638 * @data: call data for _nfs4_proc_exchange_id.
7640 int nfs4_test_session_trunk(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
,
7643 struct nfs4_add_xprt_data
*adata
= (struct nfs4_add_xprt_data
*)data
;
7646 dprintk("--> %s try %s\n", __func__
,
7647 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
7649 sp4_how
= (adata
->clp
->cl_sp4_flags
== 0 ? SP4_NONE
: SP4_MACH_CRED
);
7651 /* Test connection for session trunking. Async exchange_id call */
7652 return _nfs4_proc_exchange_id(adata
->clp
, adata
->cred
, sp4_how
, xprt
);
7654 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk
);
7656 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7657 struct rpc_cred
*cred
)
7659 struct rpc_message msg
= {
7660 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7666 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7667 trace_nfs4_destroy_clientid(clp
, status
);
7669 dprintk("NFS: Got error %d from the server %s on "
7670 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7674 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7675 struct rpc_cred
*cred
)
7680 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7681 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7683 case -NFS4ERR_DELAY
:
7684 case -NFS4ERR_CLIENTID_BUSY
:
7694 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7696 struct rpc_cred
*cred
;
7699 if (clp
->cl_mvops
->minor_version
< 1)
7701 if (clp
->cl_exchange_flags
== 0)
7703 if (clp
->cl_preserve_clid
)
7705 cred
= nfs4_get_clid_cred(clp
);
7706 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7711 case -NFS4ERR_STALE_CLIENTID
:
7712 clp
->cl_exchange_flags
= 0;
7718 struct nfs4_get_lease_time_data
{
7719 struct nfs4_get_lease_time_args
*args
;
7720 struct nfs4_get_lease_time_res
*res
;
7721 struct nfs_client
*clp
;
7724 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7727 struct nfs4_get_lease_time_data
*data
=
7728 (struct nfs4_get_lease_time_data
*)calldata
;
7730 dprintk("--> %s\n", __func__
);
7731 /* just setup sequence, do not trigger session recovery
7732 since we're invoked within one */
7733 nfs4_setup_sequence(data
->clp
,
7734 &data
->args
->la_seq_args
,
7735 &data
->res
->lr_seq_res
,
7737 dprintk("<-- %s\n", __func__
);
7741 * Called from nfs4_state_manager thread for session setup, so don't recover
7742 * from sequence operation or clientid errors.
7744 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7746 struct nfs4_get_lease_time_data
*data
=
7747 (struct nfs4_get_lease_time_data
*)calldata
;
7749 dprintk("--> %s\n", __func__
);
7750 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7752 switch (task
->tk_status
) {
7753 case -NFS4ERR_DELAY
:
7754 case -NFS4ERR_GRACE
:
7755 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7756 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7757 task
->tk_status
= 0;
7759 case -NFS4ERR_RETRY_UNCACHED_REP
:
7760 rpc_restart_call_prepare(task
);
7763 dprintk("<-- %s\n", __func__
);
7766 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7767 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7768 .rpc_call_done
= nfs4_get_lease_time_done
,
7771 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7773 struct rpc_task
*task
;
7774 struct nfs4_get_lease_time_args args
;
7775 struct nfs4_get_lease_time_res res
= {
7776 .lr_fsinfo
= fsinfo
,
7778 struct nfs4_get_lease_time_data data
= {
7783 struct rpc_message msg
= {
7784 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7788 struct rpc_task_setup task_setup
= {
7789 .rpc_client
= clp
->cl_rpcclient
,
7790 .rpc_message
= &msg
,
7791 .callback_ops
= &nfs4_get_lease_time_ops
,
7792 .callback_data
= &data
,
7793 .flags
= RPC_TASK_TIMEOUT
,
7797 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7798 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7799 dprintk("--> %s\n", __func__
);
7800 task
= rpc_run_task(&task_setup
);
7803 status
= PTR_ERR(task
);
7805 status
= task
->tk_status
;
7808 dprintk("<-- %s return %d\n", __func__
, status
);
7814 * Initialize the values to be used by the client in CREATE_SESSION
7815 * If nfs4_init_session set the fore channel request and response sizes,
7818 * Set the back channel max_resp_sz_cached to zero to force the client to
7819 * always set csa_cachethis to FALSE because the current implementation
7820 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7822 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7823 struct rpc_clnt
*clnt
)
7825 unsigned int max_rqst_sz
, max_resp_sz
;
7826 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7828 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7829 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7831 /* Fore channel attributes */
7832 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7833 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7834 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7835 args
->fc_attrs
.max_reqs
= max_session_slots
;
7837 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7838 "max_ops=%u max_reqs=%u\n",
7840 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7841 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7843 /* Back channel attributes */
7844 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7845 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7846 args
->bc_attrs
.max_resp_sz_cached
= 0;
7847 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7848 args
->bc_attrs
.max_reqs
= min_t(unsigned short, max_session_cb_slots
, 1);
7850 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7851 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7853 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7854 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7855 args
->bc_attrs
.max_reqs
);
7858 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7859 struct nfs41_create_session_res
*res
)
7861 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7862 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7864 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7867 * Our requested max_ops is the minimum we need; we're not
7868 * prepared to break up compounds into smaller pieces than that.
7869 * So, no point even trying to continue if the server won't
7872 if (rcvd
->max_ops
< sent
->max_ops
)
7874 if (rcvd
->max_reqs
== 0)
7876 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7877 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7881 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7882 struct nfs41_create_session_res
*res
)
7884 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7885 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7887 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7889 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7891 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7893 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7895 if (rcvd
->max_ops
> sent
->max_ops
)
7897 if (rcvd
->max_reqs
> sent
->max_reqs
)
7903 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7904 struct nfs41_create_session_res
*res
)
7908 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
7911 return nfs4_verify_back_channel_attrs(args
, res
);
7914 static void nfs4_update_session(struct nfs4_session
*session
,
7915 struct nfs41_create_session_res
*res
)
7917 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
7918 /* Mark client id and session as being confirmed */
7919 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
7920 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
7921 session
->flags
= res
->flags
;
7922 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
7923 if (res
->flags
& SESSION4_BACK_CHAN
)
7924 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
7925 sizeof(session
->bc_attrs
));
7928 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
7929 struct rpc_cred
*cred
)
7931 struct nfs4_session
*session
= clp
->cl_session
;
7932 struct nfs41_create_session_args args
= {
7934 .clientid
= clp
->cl_clientid
,
7935 .seqid
= clp
->cl_seqid
,
7936 .cb_program
= NFS4_CALLBACK
,
7938 struct nfs41_create_session_res res
;
7940 struct rpc_message msg
= {
7941 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
7948 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
7949 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
7951 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7952 trace_nfs4_create_session(clp
, status
);
7955 case -NFS4ERR_STALE_CLIENTID
:
7956 case -NFS4ERR_DELAY
:
7965 /* Verify the session's negotiated channel_attrs values */
7966 status
= nfs4_verify_channel_attrs(&args
, &res
);
7967 /* Increment the clientid slot sequence id */
7970 nfs4_update_session(session
, &res
);
7977 * Issues a CREATE_SESSION operation to the server.
7978 * It is the responsibility of the caller to verify the session is
7979 * expired before calling this routine.
7981 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7985 struct nfs4_session
*session
= clp
->cl_session
;
7987 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
7989 status
= _nfs4_proc_create_session(clp
, cred
);
7993 /* Init or reset the session slot tables */
7994 status
= nfs4_setup_session_slot_tables(session
);
7995 dprintk("slot table setup returned %d\n", status
);
7999 ptr
= (unsigned *)&session
->sess_id
.data
[0];
8000 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
8001 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
8003 dprintk("<-- %s\n", __func__
);
8008 * Issue the over-the-wire RPC DESTROY_SESSION.
8009 * The caller must serialize access to this routine.
8011 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
8012 struct rpc_cred
*cred
)
8014 struct rpc_message msg
= {
8015 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
8016 .rpc_argp
= session
,
8021 dprintk("--> nfs4_proc_destroy_session\n");
8023 /* session is still being setup */
8024 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
8027 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
8028 trace_nfs4_destroy_session(session
->clp
, status
);
8031 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8032 "Session has been destroyed regardless...\n", status
);
8034 dprintk("<-- nfs4_proc_destroy_session\n");
8039 * Renew the cl_session lease.
8041 struct nfs4_sequence_data
{
8042 struct nfs_client
*clp
;
8043 struct nfs4_sequence_args args
;
8044 struct nfs4_sequence_res res
;
8047 static void nfs41_sequence_release(void *data
)
8049 struct nfs4_sequence_data
*calldata
= data
;
8050 struct nfs_client
*clp
= calldata
->clp
;
8052 if (atomic_read(&clp
->cl_count
) > 1)
8053 nfs4_schedule_state_renewal(clp
);
8054 nfs_put_client(clp
);
8058 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8060 switch(task
->tk_status
) {
8061 case -NFS4ERR_DELAY
:
8062 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8065 nfs4_schedule_lease_recovery(clp
);
8070 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
8072 struct nfs4_sequence_data
*calldata
= data
;
8073 struct nfs_client
*clp
= calldata
->clp
;
8075 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
8078 trace_nfs4_sequence(clp
, task
->tk_status
);
8079 if (task
->tk_status
< 0) {
8080 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
8081 if (atomic_read(&clp
->cl_count
) == 1)
8084 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
8085 rpc_restart_call_prepare(task
);
8089 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
8091 dprintk("<-- %s\n", __func__
);
8094 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
8096 struct nfs4_sequence_data
*calldata
= data
;
8097 struct nfs_client
*clp
= calldata
->clp
;
8098 struct nfs4_sequence_args
*args
;
8099 struct nfs4_sequence_res
*res
;
8101 args
= task
->tk_msg
.rpc_argp
;
8102 res
= task
->tk_msg
.rpc_resp
;
8104 nfs4_setup_sequence(clp
, args
, res
, task
);
8107 static const struct rpc_call_ops nfs41_sequence_ops
= {
8108 .rpc_call_done
= nfs41_sequence_call_done
,
8109 .rpc_call_prepare
= nfs41_sequence_prepare
,
8110 .rpc_release
= nfs41_sequence_release
,
8113 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
8114 struct rpc_cred
*cred
,
8117 struct nfs4_sequence_data
*calldata
;
8118 struct rpc_message msg
= {
8119 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
8122 struct rpc_task_setup task_setup_data
= {
8123 .rpc_client
= clp
->cl_rpcclient
,
8124 .rpc_message
= &msg
,
8125 .callback_ops
= &nfs41_sequence_ops
,
8126 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
8129 if (!atomic_inc_not_zero(&clp
->cl_count
))
8130 return ERR_PTR(-EIO
);
8131 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8132 if (calldata
== NULL
) {
8133 nfs_put_client(clp
);
8134 return ERR_PTR(-ENOMEM
);
8136 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
8138 nfs4_set_sequence_privileged(&calldata
->args
);
8139 msg
.rpc_argp
= &calldata
->args
;
8140 msg
.rpc_resp
= &calldata
->res
;
8141 calldata
->clp
= clp
;
8142 task_setup_data
.callback_data
= calldata
;
8144 return rpc_run_task(&task_setup_data
);
8147 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
8149 struct rpc_task
*task
;
8152 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
8154 task
= _nfs41_proc_sequence(clp
, cred
, false);
8156 ret
= PTR_ERR(task
);
8158 rpc_put_task_async(task
);
8159 dprintk("<-- %s status=%d\n", __func__
, ret
);
8163 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
8165 struct rpc_task
*task
;
8168 task
= _nfs41_proc_sequence(clp
, cred
, true);
8170 ret
= PTR_ERR(task
);
8173 ret
= rpc_wait_for_completion_task(task
);
8175 ret
= task
->tk_status
;
8178 dprintk("<-- %s status=%d\n", __func__
, ret
);
8182 struct nfs4_reclaim_complete_data
{
8183 struct nfs_client
*clp
;
8184 struct nfs41_reclaim_complete_args arg
;
8185 struct nfs41_reclaim_complete_res res
;
8188 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
8190 struct nfs4_reclaim_complete_data
*calldata
= data
;
8192 nfs4_setup_sequence(calldata
->clp
,
8193 &calldata
->arg
.seq_args
,
8194 &calldata
->res
.seq_res
,
8198 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
8200 switch(task
->tk_status
) {
8202 case -NFS4ERR_COMPLETE_ALREADY
:
8203 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
8205 case -NFS4ERR_DELAY
:
8206 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
8208 case -NFS4ERR_RETRY_UNCACHED_REP
:
8211 nfs4_schedule_lease_recovery(clp
);
8216 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
8218 struct nfs4_reclaim_complete_data
*calldata
= data
;
8219 struct nfs_client
*clp
= calldata
->clp
;
8220 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
8222 dprintk("--> %s\n", __func__
);
8223 if (!nfs41_sequence_done(task
, res
))
8226 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
8227 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
8228 rpc_restart_call_prepare(task
);
8231 dprintk("<-- %s\n", __func__
);
8234 static void nfs4_free_reclaim_complete_data(void *data
)
8236 struct nfs4_reclaim_complete_data
*calldata
= data
;
8241 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
8242 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
8243 .rpc_call_done
= nfs4_reclaim_complete_done
,
8244 .rpc_release
= nfs4_free_reclaim_complete_data
,
8248 * Issue a global reclaim complete.
8250 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
8251 struct rpc_cred
*cred
)
8253 struct nfs4_reclaim_complete_data
*calldata
;
8254 struct rpc_task
*task
;
8255 struct rpc_message msg
= {
8256 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
8259 struct rpc_task_setup task_setup_data
= {
8260 .rpc_client
= clp
->cl_rpcclient
,
8261 .rpc_message
= &msg
,
8262 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
8263 .flags
= RPC_TASK_ASYNC
,
8265 int status
= -ENOMEM
;
8267 dprintk("--> %s\n", __func__
);
8268 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
8269 if (calldata
== NULL
)
8271 calldata
->clp
= clp
;
8272 calldata
->arg
.one_fs
= 0;
8274 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
8275 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
8276 msg
.rpc_argp
= &calldata
->arg
;
8277 msg
.rpc_resp
= &calldata
->res
;
8278 task_setup_data
.callback_data
= calldata
;
8279 task
= rpc_run_task(&task_setup_data
);
8281 status
= PTR_ERR(task
);
8284 status
= rpc_wait_for_completion_task(task
);
8286 status
= task
->tk_status
;
8290 dprintk("<-- %s status=%d\n", __func__
, status
);
8295 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
8297 struct nfs4_layoutget
*lgp
= calldata
;
8298 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
8300 dprintk("--> %s\n", __func__
);
8301 nfs4_setup_sequence(server
->nfs_client
, &lgp
->args
.seq_args
,
8302 &lgp
->res
.seq_res
, task
);
8303 dprintk("<-- %s\n", __func__
);
8306 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
8308 struct nfs4_layoutget
*lgp
= calldata
;
8310 dprintk("--> %s\n", __func__
);
8311 nfs41_sequence_process(task
, &lgp
->res
.seq_res
);
8312 dprintk("<-- %s\n", __func__
);
8316 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
8317 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
8319 struct inode
*inode
= lgp
->args
.inode
;
8320 struct nfs_server
*server
= NFS_SERVER(inode
);
8321 struct pnfs_layout_hdr
*lo
;
8322 int nfs4err
= task
->tk_status
;
8323 int err
, status
= 0;
8326 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
8333 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8334 * on the file. set tk_status to -ENODATA to tell upper layer to
8337 case -NFS4ERR_LAYOUTUNAVAILABLE
:
8341 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8342 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8344 case -NFS4ERR_BADLAYOUT
:
8345 status
= -EOVERFLOW
;
8348 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8349 * (or clients) writing to the same RAID stripe except when
8350 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8352 * Treat it like we would RECALLCONFLICT -- we retry for a little
8353 * while, and then eventually give up.
8355 case -NFS4ERR_LAYOUTTRYLATER
:
8356 if (lgp
->args
.minlength
== 0) {
8357 status
= -EOVERFLOW
;
8362 case -NFS4ERR_RECALLCONFLICT
:
8363 status
= -ERECALLCONFLICT
;
8365 case -NFS4ERR_DELEG_REVOKED
:
8366 case -NFS4ERR_ADMIN_REVOKED
:
8367 case -NFS4ERR_EXPIRED
:
8368 case -NFS4ERR_BAD_STATEID
:
8369 exception
->timeout
= 0;
8370 spin_lock(&inode
->i_lock
);
8371 lo
= NFS_I(inode
)->layout
;
8372 /* If the open stateid was bad, then recover it. */
8373 if (!lo
|| test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) ||
8374 nfs4_stateid_match_other(&lgp
->args
.stateid
,
8375 &lgp
->args
.ctx
->state
->stateid
)) {
8376 spin_unlock(&inode
->i_lock
);
8377 exception
->state
= lgp
->args
.ctx
->state
;
8378 exception
->stateid
= &lgp
->args
.stateid
;
8383 * Mark the bad layout state as invalid, then retry
8385 pnfs_mark_layout_stateid_invalid(lo
, &head
);
8386 spin_unlock(&inode
->i_lock
);
8387 pnfs_free_lseg_list(&head
);
8392 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8393 err
= nfs4_handle_exception(server
, nfs4err
, exception
);
8395 if (exception
->retry
)
8401 dprintk("<-- %s\n", __func__
);
8405 static size_t max_response_pages(struct nfs_server
*server
)
8407 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
8408 return nfs_page_array_len(0, max_resp_sz
);
8411 static void nfs4_free_pages(struct page
**pages
, size_t size
)
8418 for (i
= 0; i
< size
; i
++) {
8421 __free_page(pages
[i
]);
8426 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
8428 struct page
**pages
;
8431 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
8433 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
8437 for (i
= 0; i
< size
; i
++) {
8438 pages
[i
] = alloc_page(gfp_flags
);
8440 dprintk("%s: failed to allocate page\n", __func__
);
8441 nfs4_free_pages(pages
, size
);
8449 static void nfs4_layoutget_release(void *calldata
)
8451 struct nfs4_layoutget
*lgp
= calldata
;
8452 struct inode
*inode
= lgp
->args
.inode
;
8453 struct nfs_server
*server
= NFS_SERVER(inode
);
8454 size_t max_pages
= max_response_pages(server
);
8456 dprintk("--> %s\n", __func__
);
8457 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8458 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8459 put_nfs_open_context(lgp
->args
.ctx
);
8461 dprintk("<-- %s\n", __func__
);
8464 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8465 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8466 .rpc_call_done
= nfs4_layoutget_done
,
8467 .rpc_release
= nfs4_layoutget_release
,
8470 struct pnfs_layout_segment
*
8471 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8473 struct inode
*inode
= lgp
->args
.inode
;
8474 struct nfs_server
*server
= NFS_SERVER(inode
);
8475 size_t max_pages
= max_response_pages(server
);
8476 struct rpc_task
*task
;
8477 struct rpc_message msg
= {
8478 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8479 .rpc_argp
= &lgp
->args
,
8480 .rpc_resp
= &lgp
->res
,
8481 .rpc_cred
= lgp
->cred
,
8483 struct rpc_task_setup task_setup_data
= {
8484 .rpc_client
= server
->client
,
8485 .rpc_message
= &msg
,
8486 .callback_ops
= &nfs4_layoutget_call_ops
,
8487 .callback_data
= lgp
,
8488 .flags
= RPC_TASK_ASYNC
,
8490 struct pnfs_layout_segment
*lseg
= NULL
;
8491 struct nfs4_exception exception
= {
8493 .timeout
= *timeout
,
8497 dprintk("--> %s\n", __func__
);
8499 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8500 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8502 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8503 if (!lgp
->args
.layout
.pages
) {
8504 nfs4_layoutget_release(lgp
);
8505 return ERR_PTR(-ENOMEM
);
8507 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8509 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8510 lgp
->res
.seq_res
.sr_slot
= NULL
;
8511 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8513 task
= rpc_run_task(&task_setup_data
);
8515 return ERR_CAST(task
);
8516 status
= rpc_wait_for_completion_task(task
);
8518 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8519 *timeout
= exception
.timeout
;
8522 trace_nfs4_layoutget(lgp
->args
.ctx
,
8528 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8529 if (status
== 0 && lgp
->res
.layoutp
->len
)
8530 lseg
= pnfs_layout_process(lgp
);
8531 nfs4_sequence_free_slot(&lgp
->res
.seq_res
);
8533 dprintk("<-- %s status=%d\n", __func__
, status
);
8535 return ERR_PTR(status
);
8540 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8542 struct nfs4_layoutreturn
*lrp
= calldata
;
8544 dprintk("--> %s\n", __func__
);
8545 nfs4_setup_sequence(lrp
->clp
,
8546 &lrp
->args
.seq_args
,
8551 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8553 struct nfs4_layoutreturn
*lrp
= calldata
;
8554 struct nfs_server
*server
;
8556 dprintk("--> %s\n", __func__
);
8558 if (!nfs41_sequence_process(task
, &lrp
->res
.seq_res
))
8561 server
= NFS_SERVER(lrp
->args
.inode
);
8562 switch (task
->tk_status
) {
8564 task
->tk_status
= 0;
8567 case -NFS4ERR_DELAY
:
8568 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8570 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8571 rpc_restart_call_prepare(task
);
8574 dprintk("<-- %s\n", __func__
);
8577 static void nfs4_layoutreturn_release(void *calldata
)
8579 struct nfs4_layoutreturn
*lrp
= calldata
;
8580 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8582 dprintk("--> %s\n", __func__
);
8583 pnfs_layoutreturn_free_lsegs(lo
, &lrp
->args
.stateid
, &lrp
->args
.range
,
8584 lrp
->res
.lrs_present
? &lrp
->res
.stateid
: NULL
);
8585 nfs4_sequence_free_slot(&lrp
->res
.seq_res
);
8586 if (lrp
->ld_private
.ops
&& lrp
->ld_private
.ops
->free
)
8587 lrp
->ld_private
.ops
->free(&lrp
->ld_private
);
8588 pnfs_put_layout_hdr(lrp
->args
.layout
);
8589 nfs_iput_and_deactive(lrp
->inode
);
8591 dprintk("<-- %s\n", __func__
);
8594 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8595 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8596 .rpc_call_done
= nfs4_layoutreturn_done
,
8597 .rpc_release
= nfs4_layoutreturn_release
,
8600 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8602 struct rpc_task
*task
;
8603 struct rpc_message msg
= {
8604 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8605 .rpc_argp
= &lrp
->args
,
8606 .rpc_resp
= &lrp
->res
,
8607 .rpc_cred
= lrp
->cred
,
8609 struct rpc_task_setup task_setup_data
= {
8610 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8611 .rpc_message
= &msg
,
8612 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8613 .callback_data
= lrp
,
8617 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8618 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8619 &task_setup_data
.rpc_client
, &msg
);
8621 dprintk("--> %s\n", __func__
);
8623 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8625 nfs4_layoutreturn_release(lrp
);
8628 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8630 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8631 task
= rpc_run_task(&task_setup_data
);
8633 return PTR_ERR(task
);
8635 status
= task
->tk_status
;
8636 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8637 dprintk("<-- %s status=%d\n", __func__
, status
);
8643 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8644 struct pnfs_device
*pdev
,
8645 struct rpc_cred
*cred
)
8647 struct nfs4_getdeviceinfo_args args
= {
8649 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8650 NOTIFY_DEVICEID4_DELETE
,
8652 struct nfs4_getdeviceinfo_res res
= {
8655 struct rpc_message msg
= {
8656 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8663 dprintk("--> %s\n", __func__
);
8664 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8665 if (res
.notification
& ~args
.notify_types
)
8666 dprintk("%s: unsupported notification\n", __func__
);
8667 if (res
.notification
!= args
.notify_types
)
8670 dprintk("<-- %s status=%d\n", __func__
, status
);
8675 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8676 struct pnfs_device
*pdev
,
8677 struct rpc_cred
*cred
)
8679 struct nfs4_exception exception
= { };
8683 err
= nfs4_handle_exception(server
,
8684 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8686 } while (exception
.retry
);
8689 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8691 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8693 struct nfs4_layoutcommit_data
*data
= calldata
;
8694 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8696 nfs4_setup_sequence(server
->nfs_client
,
8697 &data
->args
.seq_args
,
8703 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8705 struct nfs4_layoutcommit_data
*data
= calldata
;
8706 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8708 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8711 switch (task
->tk_status
) { /* Just ignore these failures */
8712 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8713 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8714 case -NFS4ERR_BADLAYOUT
: /* no layout */
8715 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8716 task
->tk_status
= 0;
8720 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8721 rpc_restart_call_prepare(task
);
8727 static void nfs4_layoutcommit_release(void *calldata
)
8729 struct nfs4_layoutcommit_data
*data
= calldata
;
8731 pnfs_cleanup_layoutcommit(data
);
8732 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8734 put_rpccred(data
->cred
);
8735 nfs_iput_and_deactive(data
->inode
);
8739 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8740 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8741 .rpc_call_done
= nfs4_layoutcommit_done
,
8742 .rpc_release
= nfs4_layoutcommit_release
,
8746 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8748 struct rpc_message msg
= {
8749 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8750 .rpc_argp
= &data
->args
,
8751 .rpc_resp
= &data
->res
,
8752 .rpc_cred
= data
->cred
,
8754 struct rpc_task_setup task_setup_data
= {
8755 .task
= &data
->task
,
8756 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8757 .rpc_message
= &msg
,
8758 .callback_ops
= &nfs4_layoutcommit_ops
,
8759 .callback_data
= data
,
8761 struct rpc_task
*task
;
8764 dprintk("NFS: initiating layoutcommit call. sync %d "
8765 "lbw: %llu inode %lu\n", sync
,
8766 data
->args
.lastbytewritten
,
8767 data
->args
.inode
->i_ino
);
8770 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8771 if (data
->inode
== NULL
) {
8772 nfs4_layoutcommit_release(data
);
8775 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8777 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8778 task
= rpc_run_task(&task_setup_data
);
8780 return PTR_ERR(task
);
8782 status
= task
->tk_status
;
8783 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8784 dprintk("%s: status %d\n", __func__
, status
);
8790 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8791 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8794 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8795 struct nfs_fsinfo
*info
,
8796 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8798 struct nfs41_secinfo_no_name_args args
= {
8799 .style
= SECINFO_STYLE_CURRENT_FH
,
8801 struct nfs4_secinfo_res res
= {
8804 struct rpc_message msg
= {
8805 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8809 struct rpc_clnt
*clnt
= server
->client
;
8810 struct rpc_cred
*cred
= NULL
;
8813 if (use_integrity
) {
8814 clnt
= server
->nfs_client
->cl_rpcclient
;
8815 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8816 msg
.rpc_cred
= cred
;
8819 dprintk("--> %s\n", __func__
);
8820 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8822 dprintk("<-- %s status=%d\n", __func__
, status
);
8831 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8832 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8834 struct nfs4_exception exception
= { };
8837 /* first try using integrity protection */
8838 err
= -NFS4ERR_WRONGSEC
;
8840 /* try to use integrity protection with machine cred */
8841 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8842 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8846 * if unable to use integrity protection, or SECINFO with
8847 * integrity protection returns NFS4ERR_WRONGSEC (which is
8848 * disallowed by spec, but exists in deployed servers) use
8849 * the current filesystem's rpc_client and the user cred.
8851 if (err
== -NFS4ERR_WRONGSEC
)
8852 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8857 case -NFS4ERR_WRONGSEC
:
8861 err
= nfs4_handle_exception(server
, err
, &exception
);
8863 } while (exception
.retry
);
8869 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8870 struct nfs_fsinfo
*info
)
8874 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8875 struct nfs4_secinfo_flavors
*flavors
;
8876 struct nfs4_secinfo4
*secinfo
;
8879 page
= alloc_page(GFP_KERNEL
);
8885 flavors
= page_address(page
);
8886 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8889 * Fall back on "guess and check" method if
8890 * the server doesn't support SECINFO_NO_NAME
8892 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8893 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8899 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8900 secinfo
= &flavors
->flavors
[i
];
8902 switch (secinfo
->flavor
) {
8906 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
8907 &secinfo
->flavor_info
);
8910 flavor
= RPC_AUTH_MAXFLAVOR
;
8914 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
8915 flavor
= RPC_AUTH_MAXFLAVOR
;
8917 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
8918 err
= nfs4_lookup_root_sec(server
, fhandle
,
8925 if (flavor
== RPC_AUTH_MAXFLAVOR
)
8936 static int _nfs41_test_stateid(struct nfs_server
*server
,
8937 nfs4_stateid
*stateid
,
8938 struct rpc_cred
*cred
)
8941 struct nfs41_test_stateid_args args
= {
8944 struct nfs41_test_stateid_res res
;
8945 struct rpc_message msg
= {
8946 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
8951 struct rpc_clnt
*rpc_client
= server
->client
;
8953 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
8956 dprintk("NFS call test_stateid %p\n", stateid
);
8957 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
8958 nfs4_set_sequence_privileged(&args
.seq_args
);
8959 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
8960 &args
.seq_args
, &res
.seq_res
);
8961 if (status
!= NFS_OK
) {
8962 dprintk("NFS reply test_stateid: failed, %d\n", status
);
8965 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
8969 static void nfs4_handle_delay_or_session_error(struct nfs_server
*server
,
8970 int err
, struct nfs4_exception
*exception
)
8972 exception
->retry
= 0;
8974 case -NFS4ERR_DELAY
:
8975 case -NFS4ERR_RETRY_UNCACHED_REP
:
8976 nfs4_handle_exception(server
, err
, exception
);
8978 case -NFS4ERR_BADSESSION
:
8979 case -NFS4ERR_BADSLOT
:
8980 case -NFS4ERR_BAD_HIGH_SLOT
:
8981 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
8982 case -NFS4ERR_DEADSESSION
:
8983 nfs4_do_handle_exception(server
, err
, exception
);
8988 * nfs41_test_stateid - perform a TEST_STATEID operation
8990 * @server: server / transport on which to perform the operation
8991 * @stateid: state ID to test
8994 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8995 * Otherwise a negative NFS4ERR value is returned if the operation
8996 * failed or the state ID is not currently valid.
8998 static int nfs41_test_stateid(struct nfs_server
*server
,
8999 nfs4_stateid
*stateid
,
9000 struct rpc_cred
*cred
)
9002 struct nfs4_exception exception
= { };
9005 err
= _nfs41_test_stateid(server
, stateid
, cred
);
9006 nfs4_handle_delay_or_session_error(server
, err
, &exception
);
9007 } while (exception
.retry
);
9011 struct nfs_free_stateid_data
{
9012 struct nfs_server
*server
;
9013 struct nfs41_free_stateid_args args
;
9014 struct nfs41_free_stateid_res res
;
9017 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
9019 struct nfs_free_stateid_data
*data
= calldata
;
9020 nfs4_setup_sequence(data
->server
->nfs_client
,
9021 &data
->args
.seq_args
,
9026 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
9028 struct nfs_free_stateid_data
*data
= calldata
;
9030 nfs41_sequence_done(task
, &data
->res
.seq_res
);
9032 switch (task
->tk_status
) {
9033 case -NFS4ERR_DELAY
:
9034 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
9035 rpc_restart_call_prepare(task
);
9039 static void nfs41_free_stateid_release(void *calldata
)
9044 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
9045 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
9046 .rpc_call_done
= nfs41_free_stateid_done
,
9047 .rpc_release
= nfs41_free_stateid_release
,
9050 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
9051 const nfs4_stateid
*stateid
,
9052 struct rpc_cred
*cred
,
9055 struct rpc_message msg
= {
9056 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
9059 struct rpc_task_setup task_setup
= {
9060 .rpc_client
= server
->client
,
9061 .rpc_message
= &msg
,
9062 .callback_ops
= &nfs41_free_stateid_ops
,
9063 .flags
= RPC_TASK_ASYNC
,
9065 struct nfs_free_stateid_data
*data
;
9067 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
9068 &task_setup
.rpc_client
, &msg
);
9070 dprintk("NFS call free_stateid %p\n", stateid
);
9071 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
9073 return ERR_PTR(-ENOMEM
);
9074 data
->server
= server
;
9075 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
9077 task_setup
.callback_data
= data
;
9079 msg
.rpc_argp
= &data
->args
;
9080 msg
.rpc_resp
= &data
->res
;
9081 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
9083 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
9085 return rpc_run_task(&task_setup
);
9089 * nfs41_free_stateid - perform a FREE_STATEID operation
9091 * @server: server / transport on which to perform the operation
9092 * @stateid: state ID to release
9094 * @is_recovery: set to true if this call needs to be privileged
9096 * Note: this function is always asynchronous.
9098 static int nfs41_free_stateid(struct nfs_server
*server
,
9099 const nfs4_stateid
*stateid
,
9100 struct rpc_cred
*cred
,
9103 struct rpc_task
*task
;
9105 task
= _nfs41_free_stateid(server
, stateid
, cred
, is_recovery
);
9107 return PTR_ERR(task
);
9113 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
9115 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
9117 nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
9118 nfs4_free_lock_state(server
, lsp
);
9121 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
9122 const nfs4_stateid
*s2
)
9124 if (s1
->type
!= s2
->type
)
9127 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
9130 if (s1
->seqid
== s2
->seqid
)
9132 if (s1
->seqid
== 0 || s2
->seqid
== 0)
9138 #endif /* CONFIG_NFS_V4_1 */
9140 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
9141 const nfs4_stateid
*s2
)
9143 return nfs4_stateid_match(s1
, s2
);
9147 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
9148 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9149 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9150 .recover_open
= nfs4_open_reclaim
,
9151 .recover_lock
= nfs4_lock_reclaim
,
9152 .establish_clid
= nfs4_init_clientid
,
9153 .detect_trunking
= nfs40_discover_server_trunking
,
9156 #if defined(CONFIG_NFS_V4_1)
9157 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
9158 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
9159 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
9160 .recover_open
= nfs4_open_reclaim
,
9161 .recover_lock
= nfs4_lock_reclaim
,
9162 .establish_clid
= nfs41_init_clientid
,
9163 .reclaim_complete
= nfs41_proc_reclaim_complete
,
9164 .detect_trunking
= nfs41_discover_server_trunking
,
9166 #endif /* CONFIG_NFS_V4_1 */
9168 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
9169 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9170 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9171 .recover_open
= nfs40_open_expired
,
9172 .recover_lock
= nfs4_lock_expired
,
9173 .establish_clid
= nfs4_init_clientid
,
9176 #if defined(CONFIG_NFS_V4_1)
9177 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
9178 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
9179 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
9180 .recover_open
= nfs41_open_expired
,
9181 .recover_lock
= nfs41_lock_expired
,
9182 .establish_clid
= nfs41_init_clientid
,
9184 #endif /* CONFIG_NFS_V4_1 */
9186 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
9187 .sched_state_renewal
= nfs4_proc_async_renew
,
9188 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
9189 .renew_lease
= nfs4_proc_renew
,
9192 #if defined(CONFIG_NFS_V4_1)
9193 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
9194 .sched_state_renewal
= nfs41_proc_async_sequence
,
9195 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
9196 .renew_lease
= nfs4_proc_sequence
,
9200 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
9201 .get_locations
= _nfs40_proc_get_locations
,
9202 .fsid_present
= _nfs40_proc_fsid_present
,
9205 #if defined(CONFIG_NFS_V4_1)
9206 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
9207 .get_locations
= _nfs41_proc_get_locations
,
9208 .fsid_present
= _nfs41_proc_fsid_present
,
9210 #endif /* CONFIG_NFS_V4_1 */
9212 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
9214 .init_caps
= NFS_CAP_READDIRPLUS
9215 | NFS_CAP_ATOMIC_OPEN
9216 | NFS_CAP_POSIX_LOCK
,
9217 .init_client
= nfs40_init_client
,
9218 .shutdown_client
= nfs40_shutdown_client
,
9219 .match_stateid
= nfs4_match_stateid
,
9220 .find_root_sec
= nfs4_find_root_sec
,
9221 .free_lock_state
= nfs4_release_lockowner
,
9222 .test_and_free_expired
= nfs40_test_and_free_expired_stateid
,
9223 .alloc_seqid
= nfs_alloc_seqid
,
9224 .call_sync_ops
= &nfs40_call_sync_ops
,
9225 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
9226 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
9227 .state_renewal_ops
= &nfs40_state_renewal_ops
,
9228 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
9231 #if defined(CONFIG_NFS_V4_1)
9232 static struct nfs_seqid
*
9233 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
9238 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
9240 .init_caps
= NFS_CAP_READDIRPLUS
9241 | NFS_CAP_ATOMIC_OPEN
9242 | NFS_CAP_POSIX_LOCK
9243 | NFS_CAP_STATEID_NFSV41
9244 | NFS_CAP_ATOMIC_OPEN_V1
,
9245 .init_client
= nfs41_init_client
,
9246 .shutdown_client
= nfs41_shutdown_client
,
9247 .match_stateid
= nfs41_match_stateid
,
9248 .find_root_sec
= nfs41_find_root_sec
,
9249 .free_lock_state
= nfs41_free_lock_state
,
9250 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9251 .alloc_seqid
= nfs_alloc_no_seqid
,
9252 .session_trunk
= nfs4_test_session_trunk
,
9253 .call_sync_ops
= &nfs41_call_sync_ops
,
9254 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9255 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9256 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9257 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9261 #if defined(CONFIG_NFS_V4_2)
9262 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
9264 .init_caps
= NFS_CAP_READDIRPLUS
9265 | NFS_CAP_ATOMIC_OPEN
9266 | NFS_CAP_POSIX_LOCK
9267 | NFS_CAP_STATEID_NFSV41
9268 | NFS_CAP_ATOMIC_OPEN_V1
9271 | NFS_CAP_DEALLOCATE
9273 | NFS_CAP_LAYOUTSTATS
9275 .init_client
= nfs41_init_client
,
9276 .shutdown_client
= nfs41_shutdown_client
,
9277 .match_stateid
= nfs41_match_stateid
,
9278 .find_root_sec
= nfs41_find_root_sec
,
9279 .free_lock_state
= nfs41_free_lock_state
,
9280 .call_sync_ops
= &nfs41_call_sync_ops
,
9281 .test_and_free_expired
= nfs41_test_and_free_expired_stateid
,
9282 .alloc_seqid
= nfs_alloc_no_seqid
,
9283 .session_trunk
= nfs4_test_session_trunk
,
9284 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
9285 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
9286 .state_renewal_ops
= &nfs41_state_renewal_ops
,
9287 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
9291 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
9292 [0] = &nfs_v4_0_minor_ops
,
9293 #if defined(CONFIG_NFS_V4_1)
9294 [1] = &nfs_v4_1_minor_ops
,
9296 #if defined(CONFIG_NFS_V4_2)
9297 [2] = &nfs_v4_2_minor_ops
,
9301 static ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
9303 ssize_t error
, error2
;
9305 error
= generic_listxattr(dentry
, list
, size
);
9313 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
9316 return error
+ error2
;
9319 static const struct inode_operations nfs4_dir_inode_operations
= {
9320 .create
= nfs_create
,
9321 .lookup
= nfs_lookup
,
9322 .atomic_open
= nfs_atomic_open
,
9324 .unlink
= nfs_unlink
,
9325 .symlink
= nfs_symlink
,
9329 .rename
= nfs_rename
,
9330 .permission
= nfs_permission
,
9331 .getattr
= nfs_getattr
,
9332 .setattr
= nfs_setattr
,
9333 .listxattr
= nfs4_listxattr
,
9336 static const struct inode_operations nfs4_file_inode_operations
= {
9337 .permission
= nfs_permission
,
9338 .getattr
= nfs_getattr
,
9339 .setattr
= nfs_setattr
,
9340 .listxattr
= nfs4_listxattr
,
9343 const struct nfs_rpc_ops nfs_v4_clientops
= {
9344 .version
= 4, /* protocol version */
9345 .dentry_ops
= &nfs4_dentry_operations
,
9346 .dir_inode_ops
= &nfs4_dir_inode_operations
,
9347 .file_inode_ops
= &nfs4_file_inode_operations
,
9348 .file_ops
= &nfs4_file_operations
,
9349 .getroot
= nfs4_proc_get_root
,
9350 .submount
= nfs4_submount
,
9351 .try_mount
= nfs4_try_mount
,
9352 .getattr
= nfs4_proc_getattr
,
9353 .setattr
= nfs4_proc_setattr
,
9354 .lookup
= nfs4_proc_lookup
,
9355 .access
= nfs4_proc_access
,
9356 .readlink
= nfs4_proc_readlink
,
9357 .create
= nfs4_proc_create
,
9358 .remove
= nfs4_proc_remove
,
9359 .unlink_setup
= nfs4_proc_unlink_setup
,
9360 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
9361 .unlink_done
= nfs4_proc_unlink_done
,
9362 .rename_setup
= nfs4_proc_rename_setup
,
9363 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
9364 .rename_done
= nfs4_proc_rename_done
,
9365 .link
= nfs4_proc_link
,
9366 .symlink
= nfs4_proc_symlink
,
9367 .mkdir
= nfs4_proc_mkdir
,
9368 .rmdir
= nfs4_proc_remove
,
9369 .readdir
= nfs4_proc_readdir
,
9370 .mknod
= nfs4_proc_mknod
,
9371 .statfs
= nfs4_proc_statfs
,
9372 .fsinfo
= nfs4_proc_fsinfo
,
9373 .pathconf
= nfs4_proc_pathconf
,
9374 .set_capabilities
= nfs4_server_capabilities
,
9375 .decode_dirent
= nfs4_decode_dirent
,
9376 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
9377 .read_setup
= nfs4_proc_read_setup
,
9378 .read_done
= nfs4_read_done
,
9379 .write_setup
= nfs4_proc_write_setup
,
9380 .write_done
= nfs4_write_done
,
9381 .commit_setup
= nfs4_proc_commit_setup
,
9382 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
9383 .commit_done
= nfs4_commit_done
,
9384 .lock
= nfs4_proc_lock
,
9385 .clear_acl_cache
= nfs4_zap_acl_attr
,
9386 .close_context
= nfs4_close_context
,
9387 .open_context
= nfs4_atomic_open
,
9388 .have_delegation
= nfs4_have_delegation
,
9389 .return_delegation
= nfs4_inode_return_delegation
,
9390 .alloc_client
= nfs4_alloc_client
,
9391 .init_client
= nfs4_init_client
,
9392 .free_client
= nfs4_free_client
,
9393 .create_server
= nfs4_create_server
,
9394 .clone_server
= nfs_clone_server
,
9397 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
9398 .name
= XATTR_NAME_NFSV4_ACL
,
9399 .list
= nfs4_xattr_list_nfs4_acl
,
9400 .get
= nfs4_xattr_get_nfs4_acl
,
9401 .set
= nfs4_xattr_set_nfs4_acl
,
9404 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
9405 &nfs4_xattr_nfs4_acl_handler
,
9406 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9407 &nfs4_xattr_nfs4_label_handler
,