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/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.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)
77 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
78 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
79 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
80 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
81 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
82 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
83 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
84 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
85 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
86 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
87 struct nfs4_label
*olabel
);
88 #ifdef CONFIG_NFS_V4_1
89 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
91 static int nfs41_free_stateid(struct nfs_server
*, nfs4_stateid
*,
95 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
96 static inline struct nfs4_label
*
97 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
98 struct iattr
*sattr
, struct nfs4_label
*label
)
105 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
108 if (NFS_SERVER(dir
)->nfs_client
->cl_minorversion
< 2)
111 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
112 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
119 nfs4_label_release_security(struct nfs4_label
*label
)
122 security_release_secctx(label
->label
, label
->len
);
124 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
127 return server
->attr_bitmask
;
129 return server
->attr_bitmask_nl
;
132 static inline struct nfs4_label
*
133 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
134 struct iattr
*sattr
, struct nfs4_label
*l
)
137 nfs4_label_release_security(struct nfs4_label
*label
)
140 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
141 { return server
->attr_bitmask
; }
144 /* Prevent leaks of NFSv4 errors into userland */
145 static int nfs4_map_errors(int err
)
150 case -NFS4ERR_RESOURCE
:
151 case -NFS4ERR_LAYOUTTRYLATER
:
152 case -NFS4ERR_RECALLCONFLICT
:
154 case -NFS4ERR_WRONGSEC
:
155 case -NFS4ERR_WRONG_CRED
:
157 case -NFS4ERR_BADOWNER
:
158 case -NFS4ERR_BADNAME
:
160 case -NFS4ERR_SHARE_DENIED
:
162 case -NFS4ERR_MINOR_VERS_MISMATCH
:
163 return -EPROTONOSUPPORT
;
164 case -NFS4ERR_ACCESS
:
166 case -NFS4ERR_FILE_OPEN
:
169 dprintk("%s could not handle NFSv4 error %d\n",
177 * This is our standard bitmap for GETATTR requests.
179 const u32 nfs4_fattr_bitmap
[3] = {
181 | FATTR4_WORD0_CHANGE
184 | FATTR4_WORD0_FILEID
,
186 | FATTR4_WORD1_NUMLINKS
188 | FATTR4_WORD1_OWNER_GROUP
189 | FATTR4_WORD1_RAWDEV
190 | FATTR4_WORD1_SPACE_USED
191 | FATTR4_WORD1_TIME_ACCESS
192 | FATTR4_WORD1_TIME_METADATA
193 | FATTR4_WORD1_TIME_MODIFY
,
194 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
195 FATTR4_WORD2_SECURITY_LABEL
199 static const u32 nfs4_pnfs_open_bitmap
[3] = {
201 | FATTR4_WORD0_CHANGE
204 | FATTR4_WORD0_FILEID
,
206 | FATTR4_WORD1_NUMLINKS
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
213 | FATTR4_WORD1_TIME_MODIFY
,
214 FATTR4_WORD2_MDSTHRESHOLD
217 static const u32 nfs4_open_noattr_bitmap
[3] = {
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_FILEID
,
223 const u32 nfs4_statfs_bitmap
[3] = {
224 FATTR4_WORD0_FILES_AVAIL
225 | FATTR4_WORD0_FILES_FREE
226 | FATTR4_WORD0_FILES_TOTAL
,
227 FATTR4_WORD1_SPACE_AVAIL
228 | FATTR4_WORD1_SPACE_FREE
229 | FATTR4_WORD1_SPACE_TOTAL
232 const u32 nfs4_pathconf_bitmap
[3] = {
234 | FATTR4_WORD0_MAXNAME
,
238 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
239 | FATTR4_WORD0_MAXREAD
240 | FATTR4_WORD0_MAXWRITE
241 | FATTR4_WORD0_LEASE_TIME
,
242 FATTR4_WORD1_TIME_DELTA
243 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
244 FATTR4_WORD2_LAYOUT_BLKSIZE
247 const u32 nfs4_fs_locations_bitmap
[3] = {
249 | FATTR4_WORD0_CHANGE
252 | FATTR4_WORD0_FILEID
253 | FATTR4_WORD0_FS_LOCATIONS
,
255 | FATTR4_WORD1_NUMLINKS
257 | FATTR4_WORD1_OWNER_GROUP
258 | FATTR4_WORD1_RAWDEV
259 | FATTR4_WORD1_SPACE_USED
260 | FATTR4_WORD1_TIME_ACCESS
261 | FATTR4_WORD1_TIME_METADATA
262 | FATTR4_WORD1_TIME_MODIFY
263 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
266 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
267 struct nfs4_readdir_arg
*readdir
)
272 readdir
->cookie
= cookie
;
273 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
278 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
283 * NFSv4 servers do not return entries for '.' and '..'
284 * Therefore, we fake these entries here. We let '.'
285 * have cookie 0 and '..' have cookie 1. Note that
286 * when talking to the server, we always send cookie 0
289 start
= p
= kmap_atomic(*readdir
->pages
);
292 *p
++ = xdr_one
; /* next */
293 *p
++ = xdr_zero
; /* cookie, first word */
294 *p
++ = xdr_one
; /* cookie, second word */
295 *p
++ = xdr_one
; /* entry len */
296 memcpy(p
, ".\0\0\0", 4); /* entry */
298 *p
++ = xdr_one
; /* bitmap length */
299 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
300 *p
++ = htonl(8); /* attribute buffer length */
301 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
304 *p
++ = xdr_one
; /* next */
305 *p
++ = xdr_zero
; /* cookie, first word */
306 *p
++ = xdr_two
; /* cookie, second word */
307 *p
++ = xdr_two
; /* entry len */
308 memcpy(p
, "..\0\0", 4); /* entry */
310 *p
++ = xdr_one
; /* bitmap length */
311 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
312 *p
++ = htonl(8); /* attribute buffer length */
313 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
315 readdir
->pgbase
= (char *)p
- (char *)start
;
316 readdir
->count
-= readdir
->pgbase
;
317 kunmap_atomic(start
);
320 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
327 *timeout
= NFS4_POLL_RETRY_MIN
;
328 if (*timeout
> NFS4_POLL_RETRY_MAX
)
329 *timeout
= NFS4_POLL_RETRY_MAX
;
330 freezable_schedule_timeout_killable_unsafe(*timeout
);
331 if (fatal_signal_pending(current
))
337 /* This is the error handling routine for processes that are allowed
340 static int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
342 struct nfs_client
*clp
= server
->nfs_client
;
343 struct nfs4_state
*state
= exception
->state
;
344 struct inode
*inode
= exception
->inode
;
347 exception
->retry
= 0;
351 case -NFS4ERR_OPENMODE
:
352 if (inode
&& nfs4_have_delegation(inode
, FMODE_READ
)) {
353 nfs4_inode_return_delegation(inode
);
354 exception
->retry
= 1;
359 ret
= nfs4_schedule_stateid_recovery(server
, state
);
362 goto wait_on_recovery
;
363 case -NFS4ERR_DELEG_REVOKED
:
364 case -NFS4ERR_ADMIN_REVOKED
:
365 case -NFS4ERR_BAD_STATEID
:
366 if (inode
!= NULL
&& nfs4_have_delegation(inode
, FMODE_READ
)) {
367 nfs_remove_bad_delegation(inode
);
368 exception
->retry
= 1;
373 ret
= nfs4_schedule_stateid_recovery(server
, state
);
376 goto wait_on_recovery
;
377 case -NFS4ERR_EXPIRED
:
379 ret
= nfs4_schedule_stateid_recovery(server
, state
);
383 case -NFS4ERR_STALE_STATEID
:
384 case -NFS4ERR_STALE_CLIENTID
:
385 nfs4_schedule_lease_recovery(clp
);
386 goto wait_on_recovery
;
387 #if defined(CONFIG_NFS_V4_1)
388 case -NFS4ERR_BADSESSION
:
389 case -NFS4ERR_BADSLOT
:
390 case -NFS4ERR_BAD_HIGH_SLOT
:
391 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
392 case -NFS4ERR_DEADSESSION
:
393 case -NFS4ERR_SEQ_FALSE_RETRY
:
394 case -NFS4ERR_SEQ_MISORDERED
:
395 dprintk("%s ERROR: %d Reset session\n", __func__
,
397 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
398 goto wait_on_recovery
;
399 #endif /* defined(CONFIG_NFS_V4_1) */
400 case -NFS4ERR_FILE_OPEN
:
401 if (exception
->timeout
> HZ
) {
402 /* We have retried a decent amount, time to
410 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
413 case -NFS4ERR_RETRY_UNCACHED_REP
:
414 case -NFS4ERR_OLD_STATEID
:
415 exception
->retry
= 1;
417 case -NFS4ERR_BADOWNER
:
418 /* The following works around a Linux server bug! */
419 case -NFS4ERR_BADNAME
:
420 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
421 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
422 exception
->retry
= 1;
423 printk(KERN_WARNING
"NFS: v4 server %s "
424 "does not accept raw "
426 "Reenabling the idmapper.\n",
427 server
->nfs_client
->cl_hostname
);
430 /* We failed to handle the error */
431 return nfs4_map_errors(ret
);
433 ret
= nfs4_wait_clnt_recover(clp
);
435 exception
->retry
= 1;
440 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
441 * or 'false' otherwise.
443 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
445 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
447 if (flavor
== RPC_AUTH_GSS_KRB5I
||
448 flavor
== RPC_AUTH_GSS_KRB5P
)
454 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
456 spin_lock(&clp
->cl_lock
);
457 if (time_before(clp
->cl_last_renewal
,timestamp
))
458 clp
->cl_last_renewal
= timestamp
;
459 spin_unlock(&clp
->cl_lock
);
462 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
464 do_renew_lease(server
->nfs_client
, timestamp
);
467 struct nfs4_call_sync_data
{
468 const struct nfs_server
*seq_server
;
469 struct nfs4_sequence_args
*seq_args
;
470 struct nfs4_sequence_res
*seq_res
;
473 static void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
474 struct nfs4_sequence_res
*res
, int cache_reply
)
476 args
->sa_slot
= NULL
;
477 args
->sa_cache_this
= cache_reply
;
478 args
->sa_privileged
= 0;
483 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
485 args
->sa_privileged
= 1;
488 static int nfs40_setup_sequence(const struct nfs_server
*server
,
489 struct nfs4_sequence_args
*args
,
490 struct nfs4_sequence_res
*res
,
491 struct rpc_task
*task
)
493 struct nfs4_slot_table
*tbl
= server
->nfs_client
->cl_slot_tbl
;
494 struct nfs4_slot
*slot
;
496 /* slot already allocated? */
497 if (res
->sr_slot
!= NULL
)
500 spin_lock(&tbl
->slot_tbl_lock
);
501 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
504 slot
= nfs4_alloc_slot(tbl
);
506 if (slot
== ERR_PTR(-ENOMEM
))
507 task
->tk_timeout
= HZ
>> 2;
510 spin_unlock(&tbl
->slot_tbl_lock
);
512 args
->sa_slot
= slot
;
516 rpc_call_start(task
);
520 if (args
->sa_privileged
)
521 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
522 NULL
, RPC_PRIORITY_PRIVILEGED
);
524 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
525 spin_unlock(&tbl
->slot_tbl_lock
);
529 static int nfs40_sequence_done(struct rpc_task
*task
,
530 struct nfs4_sequence_res
*res
)
532 struct nfs4_slot
*slot
= res
->sr_slot
;
533 struct nfs4_slot_table
*tbl
;
535 if (!RPC_WAS_SENT(task
))
539 spin_lock(&tbl
->slot_tbl_lock
);
540 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
541 nfs4_free_slot(tbl
, slot
);
542 spin_unlock(&tbl
->slot_tbl_lock
);
549 #if defined(CONFIG_NFS_V4_1)
551 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
553 struct nfs4_session
*session
;
554 struct nfs4_slot_table
*tbl
;
555 bool send_new_highest_used_slotid
= false;
558 /* just wake up the next guy waiting since
559 * we may have not consumed a slot after all */
560 dprintk("%s: No slot\n", __func__
);
563 tbl
= res
->sr_slot
->table
;
564 session
= tbl
->session
;
566 spin_lock(&tbl
->slot_tbl_lock
);
567 /* Be nice to the server: try to ensure that the last transmitted
568 * value for highest_user_slotid <= target_highest_slotid
570 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
571 send_new_highest_used_slotid
= true;
573 if (nfs41_wake_and_assign_slot(tbl
, res
->sr_slot
)) {
574 send_new_highest_used_slotid
= false;
577 nfs4_free_slot(tbl
, res
->sr_slot
);
579 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
580 send_new_highest_used_slotid
= false;
582 spin_unlock(&tbl
->slot_tbl_lock
);
584 if (send_new_highest_used_slotid
)
585 nfs41_server_notify_highest_slotid_update(session
->clp
);
588 static int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
590 struct nfs4_session
*session
;
591 struct nfs4_slot
*slot
;
592 struct nfs_client
*clp
;
593 bool interrupted
= false;
596 /* don't increment the sequence number if the task wasn't sent */
597 if (!RPC_WAS_SENT(task
))
601 session
= slot
->table
->session
;
603 if (slot
->interrupted
) {
604 slot
->interrupted
= 0;
608 trace_nfs4_sequence_done(session
, res
);
609 /* Check the SEQUENCE operation status */
610 switch (res
->sr_status
) {
612 /* Update the slot's sequence and clientid lease timer */
615 do_renew_lease(clp
, res
->sr_timestamp
);
616 /* Check sequence flags */
617 if (res
->sr_status_flags
!= 0)
618 nfs4_schedule_lease_recovery(clp
);
619 nfs41_update_target_slotid(slot
->table
, slot
, res
);
623 * sr_status remains 1 if an RPC level error occurred.
624 * The server may or may not have processed the sequence
626 * Mark the slot as having hosted an interrupted RPC call.
628 slot
->interrupted
= 1;
631 /* The server detected a resend of the RPC call and
632 * returned NFS4ERR_DELAY as per Section 2.10.6.2
635 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
640 case -NFS4ERR_BADSLOT
:
642 * The slot id we used was probably retired. Try again
643 * using a different slot id.
646 case -NFS4ERR_SEQ_MISORDERED
:
648 * Was the last operation on this sequence interrupted?
649 * If so, retry after bumping the sequence number.
656 * Could this slot have been previously retired?
657 * If so, then the server may be expecting seq_nr = 1!
659 if (slot
->seq_nr
!= 1) {
664 case -NFS4ERR_SEQ_FALSE_RETRY
:
668 /* Just update the slot sequence no. */
672 /* The session may be reset by one of the error handlers. */
673 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
674 nfs41_sequence_free_slot(res
);
677 if (rpc_restart_call_prepare(task
)) {
683 if (!rpc_restart_call(task
))
685 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
689 static int nfs4_sequence_done(struct rpc_task
*task
,
690 struct nfs4_sequence_res
*res
)
692 if (res
->sr_slot
== NULL
)
694 if (!res
->sr_slot
->table
->session
)
695 return nfs40_sequence_done(task
, res
);
696 return nfs41_sequence_done(task
, res
);
699 int nfs41_setup_sequence(struct nfs4_session
*session
,
700 struct nfs4_sequence_args
*args
,
701 struct nfs4_sequence_res
*res
,
702 struct rpc_task
*task
)
704 struct nfs4_slot
*slot
;
705 struct nfs4_slot_table
*tbl
;
707 dprintk("--> %s\n", __func__
);
708 /* slot already allocated? */
709 if (res
->sr_slot
!= NULL
)
712 tbl
= &session
->fc_slot_table
;
714 task
->tk_timeout
= 0;
716 spin_lock(&tbl
->slot_tbl_lock
);
717 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
718 !args
->sa_privileged
) {
719 /* The state manager will wait until the slot table is empty */
720 dprintk("%s session is draining\n", __func__
);
724 slot
= nfs4_alloc_slot(tbl
);
726 /* If out of memory, try again in 1/4 second */
727 if (slot
== ERR_PTR(-ENOMEM
))
728 task
->tk_timeout
= HZ
>> 2;
729 dprintk("<-- %s: no free slots\n", __func__
);
732 spin_unlock(&tbl
->slot_tbl_lock
);
734 args
->sa_slot
= slot
;
736 dprintk("<-- %s slotid=%u seqid=%u\n", __func__
,
737 slot
->slot_nr
, slot
->seq_nr
);
740 res
->sr_timestamp
= jiffies
;
741 res
->sr_status_flags
= 0;
743 * sr_status is only set in decode_sequence, and so will remain
744 * set to 1 if an rpc level failure occurs.
747 trace_nfs4_setup_sequence(session
, args
);
749 rpc_call_start(task
);
752 /* Privileged tasks are queued with top priority */
753 if (args
->sa_privileged
)
754 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
755 NULL
, RPC_PRIORITY_PRIVILEGED
);
757 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
758 spin_unlock(&tbl
->slot_tbl_lock
);
761 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
763 static int nfs4_setup_sequence(const struct nfs_server
*server
,
764 struct nfs4_sequence_args
*args
,
765 struct nfs4_sequence_res
*res
,
766 struct rpc_task
*task
)
768 struct nfs4_session
*session
= nfs4_get_session(server
);
772 return nfs40_setup_sequence(server
, args
, res
, task
);
774 dprintk("--> %s clp %p session %p sr_slot %u\n",
775 __func__
, session
->clp
, session
, res
->sr_slot
?
776 res
->sr_slot
->slot_nr
: NFS4_NO_SLOT
);
778 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
780 dprintk("<-- %s status=%d\n", __func__
, ret
);
784 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
786 struct nfs4_call_sync_data
*data
= calldata
;
787 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
789 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
791 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
794 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
796 struct nfs4_call_sync_data
*data
= calldata
;
798 nfs41_sequence_done(task
, data
->seq_res
);
801 static const struct rpc_call_ops nfs41_call_sync_ops
= {
802 .rpc_call_prepare
= nfs41_call_sync_prepare
,
803 .rpc_call_done
= nfs41_call_sync_done
,
806 #else /* !CONFIG_NFS_V4_1 */
808 static int nfs4_setup_sequence(const struct nfs_server
*server
,
809 struct nfs4_sequence_args
*args
,
810 struct nfs4_sequence_res
*res
,
811 struct rpc_task
*task
)
813 return nfs40_setup_sequence(server
, args
, res
, task
);
816 static int nfs4_sequence_done(struct rpc_task
*task
,
817 struct nfs4_sequence_res
*res
)
819 return nfs40_sequence_done(task
, res
);
822 #endif /* !CONFIG_NFS_V4_1 */
824 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
826 struct nfs4_call_sync_data
*data
= calldata
;
827 nfs4_setup_sequence(data
->seq_server
,
828 data
->seq_args
, data
->seq_res
, task
);
831 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
833 struct nfs4_call_sync_data
*data
= calldata
;
834 nfs4_sequence_done(task
, data
->seq_res
);
837 static const struct rpc_call_ops nfs40_call_sync_ops
= {
838 .rpc_call_prepare
= nfs40_call_sync_prepare
,
839 .rpc_call_done
= nfs40_call_sync_done
,
842 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
843 struct nfs_server
*server
,
844 struct rpc_message
*msg
,
845 struct nfs4_sequence_args
*args
,
846 struct nfs4_sequence_res
*res
)
849 struct rpc_task
*task
;
850 struct nfs_client
*clp
= server
->nfs_client
;
851 struct nfs4_call_sync_data data
= {
852 .seq_server
= server
,
856 struct rpc_task_setup task_setup
= {
859 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
860 .callback_data
= &data
863 task
= rpc_run_task(&task_setup
);
867 ret
= task
->tk_status
;
874 int nfs4_call_sync(struct rpc_clnt
*clnt
,
875 struct nfs_server
*server
,
876 struct rpc_message
*msg
,
877 struct nfs4_sequence_args
*args
,
878 struct nfs4_sequence_res
*res
,
881 nfs4_init_sequence(args
, res
, cache_reply
);
882 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
885 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
887 struct nfs_inode
*nfsi
= NFS_I(dir
);
889 spin_lock(&dir
->i_lock
);
890 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
891 if (!cinfo
->atomic
|| cinfo
->before
!= dir
->i_version
)
892 nfs_force_lookup_revalidate(dir
);
893 dir
->i_version
= cinfo
->after
;
894 nfs_fscache_invalidate(dir
);
895 spin_unlock(&dir
->i_lock
);
898 struct nfs4_opendata
{
900 struct nfs_openargs o_arg
;
901 struct nfs_openres o_res
;
902 struct nfs_open_confirmargs c_arg
;
903 struct nfs_open_confirmres c_res
;
904 struct nfs4_string owner_name
;
905 struct nfs4_string group_name
;
906 struct nfs_fattr f_attr
;
907 struct nfs4_label
*f_label
;
909 struct dentry
*dentry
;
910 struct nfs4_state_owner
*owner
;
911 struct nfs4_state
*state
;
913 unsigned long timestamp
;
914 unsigned int rpc_done
: 1;
915 unsigned int file_created
: 1;
916 unsigned int is_recover
: 1;
921 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
922 int err
, struct nfs4_exception
*exception
)
926 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
928 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
929 exception
->retry
= 1;
933 static enum open_claim_type4
934 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
935 enum open_claim_type4 claim
)
937 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
942 case NFS4_OPEN_CLAIM_FH
:
943 return NFS4_OPEN_CLAIM_NULL
;
944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
945 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
946 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
947 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
951 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
953 p
->o_res
.f_attr
= &p
->f_attr
;
954 p
->o_res
.f_label
= p
->f_label
;
955 p
->o_res
.seqid
= p
->o_arg
.seqid
;
956 p
->c_res
.seqid
= p
->c_arg
.seqid
;
957 p
->o_res
.server
= p
->o_arg
.server
;
958 p
->o_res
.access_request
= p
->o_arg
.access
;
959 nfs_fattr_init(&p
->f_attr
);
960 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
963 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
964 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
965 const struct iattr
*attrs
,
966 struct nfs4_label
*label
,
967 enum open_claim_type4 claim
,
970 struct dentry
*parent
= dget_parent(dentry
);
971 struct inode
*dir
= parent
->d_inode
;
972 struct nfs_server
*server
= NFS_SERVER(dir
);
973 struct nfs4_opendata
*p
;
975 p
= kzalloc(sizeof(*p
), gfp_mask
);
979 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
980 if (IS_ERR(p
->f_label
))
983 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
984 if (p
->o_arg
.seqid
== NULL
)
986 nfs_sb_active(dentry
->d_sb
);
987 p
->dentry
= dget(dentry
);
990 atomic_inc(&sp
->so_count
);
991 p
->o_arg
.open_flags
= flags
;
992 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
993 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
994 * will return permission denied for all bits until close */
995 if (!(flags
& O_EXCL
)) {
996 /* ask server to check for all possible rights as results
998 p
->o_arg
.access
= NFS4_ACCESS_READ
| NFS4_ACCESS_MODIFY
|
999 NFS4_ACCESS_EXTEND
| NFS4_ACCESS_EXECUTE
;
1001 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1002 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1003 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1004 p
->o_arg
.name
= &dentry
->d_name
;
1005 p
->o_arg
.server
= server
;
1006 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1007 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1008 p
->o_arg
.label
= label
;
1009 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1010 switch (p
->o_arg
.claim
) {
1011 case NFS4_OPEN_CLAIM_NULL
:
1012 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1013 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1014 p
->o_arg
.fh
= NFS_FH(dir
);
1016 case NFS4_OPEN_CLAIM_PREVIOUS
:
1017 case NFS4_OPEN_CLAIM_FH
:
1018 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1019 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1020 p
->o_arg
.fh
= NFS_FH(dentry
->d_inode
);
1022 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1025 p
->o_arg
.u
.attrs
= &p
->attrs
;
1026 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1029 verf
[1] = current
->pid
;
1030 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1031 sizeof(p
->o_arg
.u
.verifier
.data
));
1033 p
->c_arg
.fh
= &p
->o_res
.fh
;
1034 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1035 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1036 nfs4_init_opendata_res(p
);
1037 kref_init(&p
->kref
);
1041 nfs4_label_free(p
->f_label
);
1049 static void nfs4_opendata_free(struct kref
*kref
)
1051 struct nfs4_opendata
*p
= container_of(kref
,
1052 struct nfs4_opendata
, kref
);
1053 struct super_block
*sb
= p
->dentry
->d_sb
;
1055 nfs_free_seqid(p
->o_arg
.seqid
);
1056 if (p
->state
!= NULL
)
1057 nfs4_put_open_state(p
->state
);
1058 nfs4_put_state_owner(p
->owner
);
1060 nfs4_label_free(p
->f_label
);
1064 nfs_sb_deactive(sb
);
1065 nfs_fattr_free_names(&p
->f_attr
);
1069 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1072 kref_put(&p
->kref
, nfs4_opendata_free
);
1075 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1079 ret
= rpc_wait_for_completion_task(task
);
1083 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1087 if (open_mode
& (O_EXCL
|O_TRUNC
))
1089 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1091 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1092 && state
->n_rdonly
!= 0;
1095 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1096 && state
->n_wronly
!= 0;
1098 case FMODE_READ
|FMODE_WRITE
:
1099 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1100 && state
->n_rdwr
!= 0;
1106 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
1108 if (delegation
== NULL
)
1110 if ((delegation
->type
& fmode
) != fmode
)
1112 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1114 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1116 nfs_mark_delegation_referenced(delegation
);
1120 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1129 case FMODE_READ
|FMODE_WRITE
:
1132 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1135 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1137 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1138 nfs4_stateid_copy(&state
->stateid
, stateid
);
1139 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1140 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1143 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1146 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1148 case FMODE_READ
|FMODE_WRITE
:
1149 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1153 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1155 write_seqlock(&state
->seqlock
);
1156 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
1157 write_sequnlock(&state
->seqlock
);
1160 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
1163 * Protect the call to nfs4_state_set_mode_locked and
1164 * serialise the stateid update
1166 write_seqlock(&state
->seqlock
);
1167 if (deleg_stateid
!= NULL
) {
1168 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1169 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1171 if (open_stateid
!= NULL
)
1172 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
1173 write_sequnlock(&state
->seqlock
);
1174 spin_lock(&state
->owner
->so_lock
);
1175 update_open_stateflags(state
, fmode
);
1176 spin_unlock(&state
->owner
->so_lock
);
1179 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
1181 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1182 struct nfs_delegation
*deleg_cur
;
1185 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1188 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1189 if (deleg_cur
== NULL
)
1192 spin_lock(&deleg_cur
->lock
);
1193 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1194 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1195 (deleg_cur
->type
& fmode
) != fmode
)
1196 goto no_delegation_unlock
;
1198 if (delegation
== NULL
)
1199 delegation
= &deleg_cur
->stateid
;
1200 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1201 goto no_delegation_unlock
;
1203 nfs_mark_delegation_referenced(deleg_cur
);
1204 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
1206 no_delegation_unlock
:
1207 spin_unlock(&deleg_cur
->lock
);
1211 if (!ret
&& open_stateid
!= NULL
) {
1212 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
1220 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1222 struct nfs_delegation
*delegation
;
1225 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1226 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1231 nfs4_inode_return_delegation(inode
);
1234 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1236 struct nfs4_state
*state
= opendata
->state
;
1237 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1238 struct nfs_delegation
*delegation
;
1239 int open_mode
= opendata
->o_arg
.open_flags
;
1240 fmode_t fmode
= opendata
->o_arg
.fmode
;
1241 nfs4_stateid stateid
;
1245 if (can_open_cached(state
, fmode
, open_mode
)) {
1246 spin_lock(&state
->owner
->so_lock
);
1247 if (can_open_cached(state
, fmode
, open_mode
)) {
1248 update_open_stateflags(state
, fmode
);
1249 spin_unlock(&state
->owner
->so_lock
);
1250 goto out_return_state
;
1252 spin_unlock(&state
->owner
->so_lock
);
1255 delegation
= rcu_dereference(nfsi
->delegation
);
1256 if (!can_open_delegated(delegation
, fmode
)) {
1260 /* Save the delegation */
1261 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1263 nfs_release_seqid(opendata
->o_arg
.seqid
);
1264 if (!opendata
->is_recover
) {
1265 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1271 /* Try to update the stateid using the delegation */
1272 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1273 goto out_return_state
;
1276 return ERR_PTR(ret
);
1278 atomic_inc(&state
->count
);
1283 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1285 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1286 struct nfs_delegation
*delegation
;
1287 int delegation_flags
= 0;
1290 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1292 delegation_flags
= delegation
->flags
;
1294 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_DELEGATE_CUR
) {
1295 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1296 "returning a delegation for "
1297 "OPEN(CLAIM_DELEGATE_CUR)\n",
1299 } else if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1300 nfs_inode_set_delegation(state
->inode
,
1301 data
->owner
->so_cred
,
1304 nfs_inode_reclaim_delegation(state
->inode
,
1305 data
->owner
->so_cred
,
1310 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1311 * and update the nfs4_state.
1313 static struct nfs4_state
*
1314 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1316 struct inode
*inode
= data
->state
->inode
;
1317 struct nfs4_state
*state
= data
->state
;
1320 if (!data
->rpc_done
) {
1321 if (data
->rpc_status
) {
1322 ret
= data
->rpc_status
;
1325 /* cached opens have already been processed */
1329 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1333 if (data
->o_res
.delegation_type
!= 0)
1334 nfs4_opendata_check_deleg(data
, state
);
1336 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1338 atomic_inc(&state
->count
);
1342 return ERR_PTR(ret
);
1346 static struct nfs4_state
*
1347 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1349 struct inode
*inode
;
1350 struct nfs4_state
*state
= NULL
;
1353 if (!data
->rpc_done
) {
1354 state
= nfs4_try_open_cached(data
);
1359 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1361 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1362 ret
= PTR_ERR(inode
);
1366 state
= nfs4_get_open_state(inode
, data
->owner
);
1369 if (data
->o_res
.delegation_type
!= 0)
1370 nfs4_opendata_check_deleg(data
, state
);
1371 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1375 nfs_release_seqid(data
->o_arg
.seqid
);
1380 return ERR_PTR(ret
);
1383 static struct nfs4_state
*
1384 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1386 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1387 return _nfs4_opendata_reclaim_to_nfs4_state(data
);
1388 return _nfs4_opendata_to_nfs4_state(data
);
1391 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1393 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1394 struct nfs_open_context
*ctx
;
1396 spin_lock(&state
->inode
->i_lock
);
1397 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1398 if (ctx
->state
!= state
)
1400 get_nfs_open_context(ctx
);
1401 spin_unlock(&state
->inode
->i_lock
);
1404 spin_unlock(&state
->inode
->i_lock
);
1405 return ERR_PTR(-ENOENT
);
1408 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1409 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1411 struct nfs4_opendata
*opendata
;
1413 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1414 NULL
, NULL
, claim
, GFP_NOFS
);
1415 if (opendata
== NULL
)
1416 return ERR_PTR(-ENOMEM
);
1417 opendata
->state
= state
;
1418 atomic_inc(&state
->count
);
1422 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1424 struct nfs4_state
*newstate
;
1427 opendata
->o_arg
.open_flags
= 0;
1428 opendata
->o_arg
.fmode
= fmode
;
1429 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1430 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1431 nfs4_init_opendata_res(opendata
);
1432 ret
= _nfs4_recover_proc_open(opendata
);
1435 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1436 if (IS_ERR(newstate
))
1437 return PTR_ERR(newstate
);
1438 nfs4_close_state(newstate
, fmode
);
1443 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1445 struct nfs4_state
*newstate
;
1448 /* memory barrier prior to reading state->n_* */
1449 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1450 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1452 if (state
->n_rdwr
!= 0) {
1453 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1454 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1457 if (newstate
!= state
)
1460 if (state
->n_wronly
!= 0) {
1461 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1462 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1465 if (newstate
!= state
)
1468 if (state
->n_rdonly
!= 0) {
1469 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1470 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1473 if (newstate
!= state
)
1477 * We may have performed cached opens for all three recoveries.
1478 * Check if we need to update the current stateid.
1480 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1481 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1482 write_seqlock(&state
->seqlock
);
1483 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1484 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1485 write_sequnlock(&state
->seqlock
);
1492 * reclaim state on the server after a reboot.
1494 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1496 struct nfs_delegation
*delegation
;
1497 struct nfs4_opendata
*opendata
;
1498 fmode_t delegation_type
= 0;
1501 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1502 NFS4_OPEN_CLAIM_PREVIOUS
);
1503 if (IS_ERR(opendata
))
1504 return PTR_ERR(opendata
);
1506 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1507 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1508 delegation_type
= delegation
->type
;
1510 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1511 status
= nfs4_open_recover(opendata
, state
);
1512 nfs4_opendata_put(opendata
);
1516 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1518 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1519 struct nfs4_exception exception
= { };
1522 err
= _nfs4_do_open_reclaim(ctx
, state
);
1523 trace_nfs4_open_reclaim(ctx
, 0, err
);
1524 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1526 if (err
!= -NFS4ERR_DELAY
)
1528 nfs4_handle_exception(server
, err
, &exception
);
1529 } while (exception
.retry
);
1533 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1535 struct nfs_open_context
*ctx
;
1538 ctx
= nfs4_state_find_open_context(state
);
1541 ret
= nfs4_do_open_reclaim(ctx
, state
);
1542 put_nfs_open_context(ctx
);
1546 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1550 printk(KERN_ERR
"NFS: %s: unhandled error "
1551 "%d.\n", __func__
, err
);
1556 case -NFS4ERR_BADSESSION
:
1557 case -NFS4ERR_BADSLOT
:
1558 case -NFS4ERR_BAD_HIGH_SLOT
:
1559 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1560 case -NFS4ERR_DEADSESSION
:
1561 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1562 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1564 case -NFS4ERR_STALE_CLIENTID
:
1565 case -NFS4ERR_STALE_STATEID
:
1566 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1567 case -NFS4ERR_EXPIRED
:
1568 /* Don't recall a delegation if it was lost */
1569 nfs4_schedule_lease_recovery(server
->nfs_client
);
1571 case -NFS4ERR_DELEG_REVOKED
:
1572 case -NFS4ERR_ADMIN_REVOKED
:
1573 case -NFS4ERR_BAD_STATEID
:
1574 case -NFS4ERR_OPENMODE
:
1575 nfs_inode_find_state_and_recover(state
->inode
,
1577 nfs4_schedule_stateid_recovery(server
, state
);
1579 case -NFS4ERR_DELAY
:
1580 case -NFS4ERR_GRACE
:
1581 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1585 case -NFS4ERR_DENIED
:
1586 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1592 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1594 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1595 struct nfs4_opendata
*opendata
;
1598 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1599 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
1600 if (IS_ERR(opendata
))
1601 return PTR_ERR(opendata
);
1602 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
1603 err
= nfs4_open_recover(opendata
, state
);
1604 nfs4_opendata_put(opendata
);
1605 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
1608 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
1610 struct nfs4_opendata
*data
= calldata
;
1612 nfs40_setup_sequence(data
->o_arg
.server
, &data
->o_arg
.seq_args
,
1613 &data
->o_res
.seq_res
, task
);
1616 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1618 struct nfs4_opendata
*data
= calldata
;
1620 nfs40_sequence_done(task
, &data
->o_res
.seq_res
);
1622 data
->rpc_status
= task
->tk_status
;
1623 if (data
->rpc_status
== 0) {
1624 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
1625 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1626 renew_lease(data
->o_res
.server
, data
->timestamp
);
1631 static void nfs4_open_confirm_release(void *calldata
)
1633 struct nfs4_opendata
*data
= calldata
;
1634 struct nfs4_state
*state
= NULL
;
1636 /* If this request hasn't been cancelled, do nothing */
1637 if (data
->cancelled
== 0)
1639 /* In case of error, no cleanup! */
1640 if (!data
->rpc_done
)
1642 state
= nfs4_opendata_to_nfs4_state(data
);
1644 nfs4_close_state(state
, data
->o_arg
.fmode
);
1646 nfs4_opendata_put(data
);
1649 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1650 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
1651 .rpc_call_done
= nfs4_open_confirm_done
,
1652 .rpc_release
= nfs4_open_confirm_release
,
1656 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1658 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1660 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1661 struct rpc_task
*task
;
1662 struct rpc_message msg
= {
1663 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1664 .rpc_argp
= &data
->c_arg
,
1665 .rpc_resp
= &data
->c_res
,
1666 .rpc_cred
= data
->owner
->so_cred
,
1668 struct rpc_task_setup task_setup_data
= {
1669 .rpc_client
= server
->client
,
1670 .rpc_message
= &msg
,
1671 .callback_ops
= &nfs4_open_confirm_ops
,
1672 .callback_data
= data
,
1673 .workqueue
= nfsiod_workqueue
,
1674 .flags
= RPC_TASK_ASYNC
,
1678 nfs4_init_sequence(&data
->o_arg
.seq_args
, &data
->o_res
.seq_res
, 1);
1679 kref_get(&data
->kref
);
1681 data
->rpc_status
= 0;
1682 data
->timestamp
= jiffies
;
1683 task
= rpc_run_task(&task_setup_data
);
1685 return PTR_ERR(task
);
1686 status
= nfs4_wait_for_completion_rpc_task(task
);
1688 data
->cancelled
= 1;
1691 status
= data
->rpc_status
;
1696 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1698 struct nfs4_opendata
*data
= calldata
;
1699 struct nfs4_state_owner
*sp
= data
->owner
;
1700 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
1702 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1705 * Check if we still need to send an OPEN call, or if we can use
1706 * a delegation instead.
1708 if (data
->state
!= NULL
) {
1709 struct nfs_delegation
*delegation
;
1711 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1714 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1715 if (data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
&&
1716 data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEG_CUR_FH
&&
1717 can_open_delegated(delegation
, data
->o_arg
.fmode
))
1718 goto unlock_no_action
;
1721 /* Update client id. */
1722 data
->o_arg
.clientid
= clp
->cl_clientid
;
1723 switch (data
->o_arg
.claim
) {
1724 case NFS4_OPEN_CLAIM_PREVIOUS
:
1725 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1726 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1727 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
1728 case NFS4_OPEN_CLAIM_FH
:
1729 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1730 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1732 data
->timestamp
= jiffies
;
1733 if (nfs4_setup_sequence(data
->o_arg
.server
,
1734 &data
->o_arg
.seq_args
,
1735 &data
->o_res
.seq_res
,
1737 nfs_release_seqid(data
->o_arg
.seqid
);
1739 /* Set the create mode (note dependency on the session type) */
1740 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
1741 if (data
->o_arg
.open_flags
& O_EXCL
) {
1742 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
1743 if (nfs4_has_persistent_session(clp
))
1744 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
1745 else if (clp
->cl_mvops
->minor_version
> 0)
1746 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
1752 task
->tk_action
= NULL
;
1754 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
1757 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1759 struct nfs4_opendata
*data
= calldata
;
1761 data
->rpc_status
= task
->tk_status
;
1763 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1766 if (task
->tk_status
== 0) {
1767 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
1768 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1772 data
->rpc_status
= -ELOOP
;
1775 data
->rpc_status
= -EISDIR
;
1778 data
->rpc_status
= -ENOTDIR
;
1781 renew_lease(data
->o_res
.server
, data
->timestamp
);
1782 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1783 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1788 static void nfs4_open_release(void *calldata
)
1790 struct nfs4_opendata
*data
= calldata
;
1791 struct nfs4_state
*state
= NULL
;
1793 /* If this request hasn't been cancelled, do nothing */
1794 if (data
->cancelled
== 0)
1796 /* In case of error, no cleanup! */
1797 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1799 /* In case we need an open_confirm, no cleanup! */
1800 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1802 state
= nfs4_opendata_to_nfs4_state(data
);
1804 nfs4_close_state(state
, data
->o_arg
.fmode
);
1806 nfs4_opendata_put(data
);
1809 static const struct rpc_call_ops nfs4_open_ops
= {
1810 .rpc_call_prepare
= nfs4_open_prepare
,
1811 .rpc_call_done
= nfs4_open_done
,
1812 .rpc_release
= nfs4_open_release
,
1815 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1817 struct inode
*dir
= data
->dir
->d_inode
;
1818 struct nfs_server
*server
= NFS_SERVER(dir
);
1819 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1820 struct nfs_openres
*o_res
= &data
->o_res
;
1821 struct rpc_task
*task
;
1822 struct rpc_message msg
= {
1823 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1826 .rpc_cred
= data
->owner
->so_cred
,
1828 struct rpc_task_setup task_setup_data
= {
1829 .rpc_client
= server
->client
,
1830 .rpc_message
= &msg
,
1831 .callback_ops
= &nfs4_open_ops
,
1832 .callback_data
= data
,
1833 .workqueue
= nfsiod_workqueue
,
1834 .flags
= RPC_TASK_ASYNC
,
1838 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
1839 kref_get(&data
->kref
);
1841 data
->rpc_status
= 0;
1842 data
->cancelled
= 0;
1843 data
->is_recover
= 0;
1845 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
1846 data
->is_recover
= 1;
1848 task
= rpc_run_task(&task_setup_data
);
1850 return PTR_ERR(task
);
1851 status
= nfs4_wait_for_completion_rpc_task(task
);
1853 data
->cancelled
= 1;
1856 status
= data
->rpc_status
;
1862 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1864 struct inode
*dir
= data
->dir
->d_inode
;
1865 struct nfs_openres
*o_res
= &data
->o_res
;
1868 status
= nfs4_run_open_task(data
, 1);
1869 if (status
!= 0 || !data
->rpc_done
)
1872 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
1874 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1875 status
= _nfs4_proc_open_confirm(data
);
1883 static int nfs4_opendata_access(struct rpc_cred
*cred
,
1884 struct nfs4_opendata
*opendata
,
1885 struct nfs4_state
*state
, fmode_t fmode
,
1888 struct nfs_access_entry cache
;
1891 /* access call failed or for some reason the server doesn't
1892 * support any access modes -- defer access call until later */
1893 if (opendata
->o_res
.access_supported
== 0)
1897 /* don't check MAY_WRITE - a newly created file may not have
1898 * write mode bits, but POSIX allows the creating process to write.
1899 * use openflags to check for exec, because fmode won't
1900 * always have FMODE_EXEC set when file open for exec. */
1901 if (openflags
& __FMODE_EXEC
) {
1902 /* ONLY check for exec rights */
1904 } else if (fmode
& FMODE_READ
)
1908 cache
.jiffies
= jiffies
;
1909 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
1910 nfs_access_add_cache(state
->inode
, &cache
);
1912 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
1915 /* even though OPEN succeeded, access is denied. Close the file */
1916 nfs4_close_state(state
, fmode
);
1921 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1923 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1925 struct inode
*dir
= data
->dir
->d_inode
;
1926 struct nfs_server
*server
= NFS_SERVER(dir
);
1927 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1928 struct nfs_openres
*o_res
= &data
->o_res
;
1931 status
= nfs4_run_open_task(data
, 0);
1932 if (!data
->rpc_done
)
1935 if (status
== -NFS4ERR_BADNAME
&&
1936 !(o_arg
->open_flags
& O_CREAT
))
1941 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
1943 if (o_arg
->open_flags
& O_CREAT
) {
1944 update_changeattr(dir
, &o_res
->cinfo
);
1945 if (o_arg
->open_flags
& O_EXCL
)
1946 data
->file_created
= 1;
1947 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
1948 data
->file_created
= 1;
1950 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1951 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1952 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1953 status
= _nfs4_proc_open_confirm(data
);
1957 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1958 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
1962 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1964 return nfs4_client_recover_expired_lease(server
->nfs_client
);
1969 * reclaim state on the server after a network partition.
1970 * Assumes caller holds the appropriate lock
1972 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1974 struct nfs4_opendata
*opendata
;
1977 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1978 NFS4_OPEN_CLAIM_FH
);
1979 if (IS_ERR(opendata
))
1980 return PTR_ERR(opendata
);
1981 ret
= nfs4_open_recover(opendata
, state
);
1983 d_drop(ctx
->dentry
);
1984 nfs4_opendata_put(opendata
);
1988 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1990 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1991 struct nfs4_exception exception
= { };
1995 err
= _nfs4_open_expired(ctx
, state
);
1996 trace_nfs4_open_expired(ctx
, 0, err
);
1997 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2002 case -NFS4ERR_GRACE
:
2003 case -NFS4ERR_DELAY
:
2004 nfs4_handle_exception(server
, err
, &exception
);
2007 } while (exception
.retry
);
2012 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2014 struct nfs_open_context
*ctx
;
2017 ctx
= nfs4_state_find_open_context(state
);
2020 ret
= nfs4_do_open_expired(ctx
, state
);
2021 put_nfs_open_context(ctx
);
2025 #if defined(CONFIG_NFS_V4_1)
2026 static void nfs41_clear_delegation_stateid(struct nfs4_state
*state
)
2028 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2029 nfs4_stateid
*stateid
= &state
->stateid
;
2030 struct nfs_delegation
*delegation
;
2031 struct rpc_cred
*cred
= NULL
;
2032 int status
= -NFS4ERR_BAD_STATEID
;
2034 /* If a state reset has been done, test_stateid is unneeded */
2035 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
2038 /* Get the delegation credential for use by test/free_stateid */
2040 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2041 if (delegation
!= NULL
&&
2042 nfs4_stateid_match(&delegation
->stateid
, stateid
)) {
2043 cred
= get_rpccred(delegation
->cred
);
2045 status
= nfs41_test_stateid(server
, stateid
, cred
);
2046 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2050 if (status
!= NFS_OK
) {
2051 /* Free the stateid unless the server explicitly
2052 * informs us the stateid is unrecognized. */
2053 if (status
!= -NFS4ERR_BAD_STATEID
)
2054 nfs41_free_stateid(server
, stateid
, cred
);
2055 nfs_remove_bad_delegation(state
->inode
);
2057 write_seqlock(&state
->seqlock
);
2058 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2059 write_sequnlock(&state
->seqlock
);
2060 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2068 * nfs41_check_open_stateid - possibly free an open stateid
2070 * @state: NFSv4 state for an inode
2072 * Returns NFS_OK if recovery for this stateid is now finished.
2073 * Otherwise a negative NFS4ERR value is returned.
2075 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2077 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2078 nfs4_stateid
*stateid
= &state
->open_stateid
;
2079 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2082 /* If a state reset has been done, test_stateid is unneeded */
2083 if ((test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) == 0) &&
2084 (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) == 0) &&
2085 (test_bit(NFS_O_RDWR_STATE
, &state
->flags
) == 0))
2086 return -NFS4ERR_BAD_STATEID
;
2088 status
= nfs41_test_stateid(server
, stateid
, cred
);
2089 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2090 if (status
!= NFS_OK
) {
2091 /* Free the stateid unless the server explicitly
2092 * informs us the stateid is unrecognized. */
2093 if (status
!= -NFS4ERR_BAD_STATEID
)
2094 nfs41_free_stateid(server
, stateid
, cred
);
2096 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2097 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2098 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2099 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2104 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2108 nfs41_clear_delegation_stateid(state
);
2109 status
= nfs41_check_open_stateid(state
);
2110 if (status
!= NFS_OK
)
2111 status
= nfs4_open_expired(sp
, state
);
2117 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2118 * fields corresponding to attributes that were used to store the verifier.
2119 * Make sure we clobber those fields in the later setattr call
2121 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
2123 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2124 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2125 sattr
->ia_valid
|= ATTR_ATIME
;
2127 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2128 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2129 sattr
->ia_valid
|= ATTR_MTIME
;
2132 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2135 struct nfs_open_context
*ctx
)
2137 struct nfs4_state_owner
*sp
= opendata
->owner
;
2138 struct nfs_server
*server
= sp
->so_server
;
2139 struct dentry
*dentry
;
2140 struct nfs4_state
*state
;
2144 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2146 ret
= _nfs4_proc_open(opendata
);
2150 state
= nfs4_opendata_to_nfs4_state(opendata
);
2151 ret
= PTR_ERR(state
);
2154 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2155 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2157 dentry
= opendata
->dentry
;
2158 if (dentry
->d_inode
== NULL
) {
2159 /* FIXME: Is this d_drop() ever needed? */
2161 dentry
= d_add_unique(dentry
, igrab(state
->inode
));
2162 if (dentry
== NULL
) {
2163 dentry
= opendata
->dentry
;
2164 } else if (dentry
!= ctx
->dentry
) {
2166 ctx
->dentry
= dget(dentry
);
2168 nfs_set_verifier(dentry
,
2169 nfs_save_change_attribute(opendata
->dir
->d_inode
));
2172 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2177 if (dentry
->d_inode
== state
->inode
) {
2178 nfs_inode_attach_open_context(ctx
);
2179 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2180 nfs4_schedule_stateid_recovery(server
, state
);
2187 * Returns a referenced nfs4_state
2189 static int _nfs4_do_open(struct inode
*dir
,
2190 struct nfs_open_context
*ctx
,
2192 struct iattr
*sattr
,
2193 struct nfs4_label
*label
,
2196 struct nfs4_state_owner
*sp
;
2197 struct nfs4_state
*state
= NULL
;
2198 struct nfs_server
*server
= NFS_SERVER(dir
);
2199 struct nfs4_opendata
*opendata
;
2200 struct dentry
*dentry
= ctx
->dentry
;
2201 struct rpc_cred
*cred
= ctx
->cred
;
2202 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2203 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2204 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2205 struct nfs4_label
*olabel
= NULL
;
2208 /* Protect against reboot recovery conflicts */
2210 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2212 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2215 status
= nfs4_recover_expired_lease(server
);
2217 goto err_put_state_owner
;
2218 if (dentry
->d_inode
!= NULL
)
2219 nfs4_return_incompatible_delegation(dentry
->d_inode
, fmode
);
2221 if (dentry
->d_inode
)
2222 claim
= NFS4_OPEN_CLAIM_FH
;
2223 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2224 label
, claim
, GFP_KERNEL
);
2225 if (opendata
== NULL
)
2226 goto err_put_state_owner
;
2229 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2230 if (IS_ERR(olabel
)) {
2231 status
= PTR_ERR(olabel
);
2232 goto err_opendata_put
;
2236 if (ctx_th
&& server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2237 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2238 if (!opendata
->f_attr
.mdsthreshold
)
2239 goto err_free_label
;
2240 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2242 if (dentry
->d_inode
!= NULL
)
2243 opendata
->state
= nfs4_get_open_state(dentry
->d_inode
, sp
);
2245 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2247 goto err_free_label
;
2250 if ((opendata
->o_arg
.open_flags
& O_EXCL
) &&
2251 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2252 nfs4_exclusive_attrset(opendata
, sattr
);
2254 nfs_fattr_init(opendata
->o_res
.f_attr
);
2255 status
= nfs4_do_setattr(state
->inode
, cred
,
2256 opendata
->o_res
.f_attr
, sattr
,
2257 state
, label
, olabel
);
2259 nfs_setattr_update_inode(state
->inode
, sattr
);
2260 nfs_post_op_update_inode(state
->inode
, opendata
->o_res
.f_attr
);
2261 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2264 if (opendata
->file_created
)
2265 *opened
|= FILE_CREATED
;
2267 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
))
2268 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2270 kfree(opendata
->f_attr
.mdsthreshold
);
2271 opendata
->f_attr
.mdsthreshold
= NULL
;
2273 nfs4_label_free(olabel
);
2275 nfs4_opendata_put(opendata
);
2276 nfs4_put_state_owner(sp
);
2279 nfs4_label_free(olabel
);
2281 kfree(opendata
->f_attr
.mdsthreshold
);
2282 nfs4_opendata_put(opendata
);
2283 err_put_state_owner
:
2284 nfs4_put_state_owner(sp
);
2290 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2291 struct nfs_open_context
*ctx
,
2293 struct iattr
*sattr
,
2294 struct nfs4_label
*label
,
2297 struct nfs_server
*server
= NFS_SERVER(dir
);
2298 struct nfs4_exception exception
= { };
2299 struct nfs4_state
*res
;
2303 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2305 trace_nfs4_open_file(ctx
, flags
, status
);
2308 /* NOTE: BAD_SEQID means the server and client disagree about the
2309 * book-keeping w.r.t. state-changing operations
2310 * (OPEN/CLOSE/LOCK/LOCKU...)
2311 * It is actually a sign of a bug on the client or on the server.
2313 * If we receive a BAD_SEQID error in the particular case of
2314 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2315 * have unhashed the old state_owner for us, and that we can
2316 * therefore safely retry using a new one. We should still warn
2317 * the user though...
2319 if (status
== -NFS4ERR_BAD_SEQID
) {
2320 pr_warn_ratelimited("NFS: v4 server %s "
2321 " returned a bad sequence-id error!\n",
2322 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2323 exception
.retry
= 1;
2327 * BAD_STATEID on OPEN means that the server cancelled our
2328 * state before it received the OPEN_CONFIRM.
2329 * Recover by retrying the request as per the discussion
2330 * on Page 181 of RFC3530.
2332 if (status
== -NFS4ERR_BAD_STATEID
) {
2333 exception
.retry
= 1;
2336 if (status
== -EAGAIN
) {
2337 /* We must have found a delegation */
2338 exception
.retry
= 1;
2341 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2343 res
= ERR_PTR(nfs4_handle_exception(server
,
2344 status
, &exception
));
2345 } while (exception
.retry
);
2349 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2350 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2351 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2352 struct nfs4_label
*olabel
)
2354 struct nfs_server
*server
= NFS_SERVER(inode
);
2355 struct nfs_setattrargs arg
= {
2356 .fh
= NFS_FH(inode
),
2359 .bitmask
= server
->attr_bitmask
,
2362 struct nfs_setattrres res
= {
2367 struct rpc_message msg
= {
2368 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2373 unsigned long timestamp
= jiffies
;
2378 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2380 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2382 nfs_fattr_init(fattr
);
2384 /* Servers should only apply open mode checks for file size changes */
2385 truncate
= (sattr
->ia_valid
& ATTR_SIZE
) ? true : false;
2386 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2388 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
, fmode
)) {
2389 /* Use that stateid */
2390 } else if (truncate
&& state
!= NULL
&& nfs4_valid_open_stateid(state
)) {
2391 struct nfs_lockowner lockowner
= {
2392 .l_owner
= current
->files
,
2393 .l_pid
= current
->tgid
,
2395 nfs4_select_rw_stateid(&arg
.stateid
, state
, FMODE_WRITE
,
2398 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
2400 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2401 if (status
== 0 && state
!= NULL
)
2402 renew_lease(server
, timestamp
);
2406 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2407 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2408 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2409 struct nfs4_label
*olabel
)
2411 struct nfs_server
*server
= NFS_SERVER(inode
);
2412 struct nfs4_exception exception
= {
2418 err
= _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, ilabel
, olabel
);
2419 trace_nfs4_setattr(inode
, err
);
2421 case -NFS4ERR_OPENMODE
:
2422 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2423 pr_warn_once("NFSv4: server %s is incorrectly "
2424 "applying open mode checks to "
2425 "a SETATTR that is not "
2426 "changing file size.\n",
2427 server
->nfs_client
->cl_hostname
);
2429 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2431 if (sattr
->ia_valid
& ATTR_OPEN
)
2436 err
= nfs4_handle_exception(server
, err
, &exception
);
2437 } while (exception
.retry
);
2442 struct nfs4_closedata
{
2443 struct inode
*inode
;
2444 struct nfs4_state
*state
;
2445 struct nfs_closeargs arg
;
2446 struct nfs_closeres res
;
2447 struct nfs_fattr fattr
;
2448 unsigned long timestamp
;
2453 static void nfs4_free_closedata(void *data
)
2455 struct nfs4_closedata
*calldata
= data
;
2456 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2457 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2460 pnfs_roc_release(calldata
->state
->inode
);
2461 nfs4_put_open_state(calldata
->state
);
2462 nfs_free_seqid(calldata
->arg
.seqid
);
2463 nfs4_put_state_owner(sp
);
2464 nfs_sb_deactive(sb
);
2468 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
2471 spin_lock(&state
->owner
->so_lock
);
2472 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2473 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
2475 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2478 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2481 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2482 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2483 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2485 spin_unlock(&state
->owner
->so_lock
);
2488 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2490 struct nfs4_closedata
*calldata
= data
;
2491 struct nfs4_state
*state
= calldata
->state
;
2492 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2494 dprintk("%s: begin!\n", __func__
);
2495 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2497 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
2498 /* hmm. we are done with the inode, and in the process of freeing
2499 * the state_owner. we keep this around to process errors
2501 switch (task
->tk_status
) {
2504 pnfs_roc_set_barrier(state
->inode
,
2505 calldata
->roc_barrier
);
2506 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
2507 renew_lease(server
, calldata
->timestamp
);
2508 nfs4_close_clear_stateid_flags(state
,
2509 calldata
->arg
.fmode
);
2511 case -NFS4ERR_STALE_STATEID
:
2512 case -NFS4ERR_OLD_STATEID
:
2513 case -NFS4ERR_BAD_STATEID
:
2514 case -NFS4ERR_EXPIRED
:
2515 if (calldata
->arg
.fmode
== 0)
2518 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
2519 rpc_restart_call_prepare(task
);
2521 nfs_release_seqid(calldata
->arg
.seqid
);
2522 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
2523 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
2526 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
2528 struct nfs4_closedata
*calldata
= data
;
2529 struct nfs4_state
*state
= calldata
->state
;
2530 struct inode
*inode
= calldata
->inode
;
2533 dprintk("%s: begin!\n", __func__
);
2534 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
2537 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
2538 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
2539 spin_lock(&state
->owner
->so_lock
);
2540 /* Calculate the change in open mode */
2541 if (state
->n_rdwr
== 0) {
2542 if (state
->n_rdonly
== 0) {
2543 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2544 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2545 calldata
->arg
.fmode
&= ~FMODE_READ
;
2547 if (state
->n_wronly
== 0) {
2548 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2549 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2550 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
2553 if (!nfs4_valid_open_stateid(state
))
2555 spin_unlock(&state
->owner
->so_lock
);
2558 /* Note: exit _without_ calling nfs4_close_done */
2562 if (calldata
->arg
.fmode
== 0) {
2563 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
2564 if (calldata
->roc
&&
2565 pnfs_roc_drain(inode
, &calldata
->roc_barrier
, task
)) {
2566 nfs_release_seqid(calldata
->arg
.seqid
);
2571 nfs_fattr_init(calldata
->res
.fattr
);
2572 calldata
->timestamp
= jiffies
;
2573 if (nfs4_setup_sequence(NFS_SERVER(inode
),
2574 &calldata
->arg
.seq_args
,
2575 &calldata
->res
.seq_res
,
2577 nfs_release_seqid(calldata
->arg
.seqid
);
2578 dprintk("%s: done!\n", __func__
);
2581 task
->tk_action
= NULL
;
2583 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
2586 static const struct rpc_call_ops nfs4_close_ops
= {
2587 .rpc_call_prepare
= nfs4_close_prepare
,
2588 .rpc_call_done
= nfs4_close_done
,
2589 .rpc_release
= nfs4_free_closedata
,
2593 * It is possible for data to be read/written from a mem-mapped file
2594 * after the sys_close call (which hits the vfs layer as a flush).
2595 * This means that we can't safely call nfsv4 close on a file until
2596 * the inode is cleared. This in turn means that we are not good
2597 * NFSv4 citizens - we do not indicate to the server to update the file's
2598 * share state even when we are done with one of the three share
2599 * stateid's in the inode.
2601 * NOTE: Caller must be holding the sp->so_owner semaphore!
2603 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
2605 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2606 struct nfs4_closedata
*calldata
;
2607 struct nfs4_state_owner
*sp
= state
->owner
;
2608 struct rpc_task
*task
;
2609 struct rpc_message msg
= {
2610 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
2611 .rpc_cred
= state
->owner
->so_cred
,
2613 struct rpc_task_setup task_setup_data
= {
2614 .rpc_client
= server
->client
,
2615 .rpc_message
= &msg
,
2616 .callback_ops
= &nfs4_close_ops
,
2617 .workqueue
= nfsiod_workqueue
,
2618 .flags
= RPC_TASK_ASYNC
,
2620 int status
= -ENOMEM
;
2622 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
2623 &task_setup_data
.rpc_client
, &msg
);
2625 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
2626 if (calldata
== NULL
)
2628 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
2629 calldata
->inode
= state
->inode
;
2630 calldata
->state
= state
;
2631 calldata
->arg
.fh
= NFS_FH(state
->inode
);
2632 calldata
->arg
.stateid
= &state
->open_stateid
;
2633 /* Serialization for the sequence id */
2634 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
2635 if (calldata
->arg
.seqid
== NULL
)
2636 goto out_free_calldata
;
2637 calldata
->arg
.fmode
= 0;
2638 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
2639 calldata
->res
.fattr
= &calldata
->fattr
;
2640 calldata
->res
.seqid
= calldata
->arg
.seqid
;
2641 calldata
->res
.server
= server
;
2642 calldata
->roc
= pnfs_roc(state
->inode
);
2643 nfs_sb_active(calldata
->inode
->i_sb
);
2645 msg
.rpc_argp
= &calldata
->arg
;
2646 msg
.rpc_resp
= &calldata
->res
;
2647 task_setup_data
.callback_data
= calldata
;
2648 task
= rpc_run_task(&task_setup_data
);
2650 return PTR_ERR(task
);
2653 status
= rpc_wait_for_completion_task(task
);
2659 nfs4_put_open_state(state
);
2660 nfs4_put_state_owner(sp
);
2664 static struct inode
*
2665 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
2666 int open_flags
, struct iattr
*attr
, int *opened
)
2668 struct nfs4_state
*state
;
2669 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
2671 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
2673 /* Protect against concurrent sillydeletes */
2674 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
2676 nfs4_label_release_security(label
);
2679 return ERR_CAST(state
);
2680 return state
->inode
;
2683 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2685 if (ctx
->state
== NULL
)
2688 nfs4_close_sync(ctx
->state
, ctx
->mode
);
2690 nfs4_close_state(ctx
->state
, ctx
->mode
);
2693 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2695 struct nfs4_server_caps_arg args
= {
2698 struct nfs4_server_caps_res res
= {};
2699 struct rpc_message msg
= {
2700 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2706 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2708 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2709 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2710 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2711 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2712 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2713 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2714 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2715 server
->caps
|= NFS_CAP_ACLS
;
2716 if (res
.has_links
!= 0)
2717 server
->caps
|= NFS_CAP_HARDLINKS
;
2718 if (res
.has_symlinks
!= 0)
2719 server
->caps
|= NFS_CAP_SYMLINKS
;
2720 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2721 server
->caps
|= NFS_CAP_FILEID
;
2722 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2723 server
->caps
|= NFS_CAP_MODE
;
2724 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2725 server
->caps
|= NFS_CAP_NLINK
;
2726 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2727 server
->caps
|= NFS_CAP_OWNER
;
2728 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2729 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2730 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2731 server
->caps
|= NFS_CAP_ATIME
;
2732 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2733 server
->caps
|= NFS_CAP_CTIME
;
2734 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2735 server
->caps
|= NFS_CAP_MTIME
;
2736 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2737 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2738 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
2740 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
2741 sizeof(server
->attr_bitmask
));
2743 if (server
->caps
& NFS_CAP_SECURITY_LABEL
) {
2744 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
2745 res
.attr_bitmask
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
2747 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2748 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2749 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2750 server
->acl_bitmask
= res
.acl_bitmask
;
2751 server
->fh_expire_type
= res
.fh_expire_type
;
2757 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2759 struct nfs4_exception exception
= { };
2762 err
= nfs4_handle_exception(server
,
2763 _nfs4_server_capabilities(server
, fhandle
),
2765 } while (exception
.retry
);
2769 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2770 struct nfs_fsinfo
*info
)
2773 struct nfs4_lookup_root_arg args
= {
2776 struct nfs4_lookup_res res
= {
2778 .fattr
= info
->fattr
,
2781 struct rpc_message msg
= {
2782 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2787 bitmask
[0] = nfs4_fattr_bitmap
[0];
2788 bitmask
[1] = nfs4_fattr_bitmap
[1];
2790 * Process the label in the upcoming getfattr
2792 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
2794 nfs_fattr_init(info
->fattr
);
2795 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2798 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2799 struct nfs_fsinfo
*info
)
2801 struct nfs4_exception exception
= { };
2804 err
= _nfs4_lookup_root(server
, fhandle
, info
);
2805 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
2808 case -NFS4ERR_WRONGSEC
:
2811 err
= nfs4_handle_exception(server
, err
, &exception
);
2813 } while (exception
.retry
);
2818 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2819 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
2821 struct rpc_auth_create_args auth_args
= {
2822 .pseudoflavor
= flavor
,
2824 struct rpc_auth
*auth
;
2827 auth
= rpcauth_create(&auth_args
, server
->client
);
2832 ret
= nfs4_lookup_root(server
, fhandle
, info
);
2838 * Retry pseudoroot lookup with various security flavors. We do this when:
2840 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2841 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2843 * Returns zero on success, or a negative NFS4ERR value, or a
2844 * negative errno value.
2846 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2847 struct nfs_fsinfo
*info
)
2849 /* Per 3530bis 15.33.5 */
2850 static const rpc_authflavor_t flav_array
[] = {
2854 RPC_AUTH_UNIX
, /* courtesy */
2857 int status
= -EPERM
;
2860 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
2861 status
= nfs4_lookup_root_sec(server
, fhandle
, info
, flav_array
[i
]);
2862 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
2868 * -EACCESS could mean that the user doesn't have correct permissions
2869 * to access the mount. It could also mean that we tried to mount
2870 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2871 * existing mount programs don't handle -EACCES very well so it should
2872 * be mapped to -EPERM instead.
2874 if (status
== -EACCES
)
2879 static int nfs4_do_find_root_sec(struct nfs_server
*server
,
2880 struct nfs_fh
*fhandle
, struct nfs_fsinfo
*info
)
2882 int mv
= server
->nfs_client
->cl_minorversion
;
2883 return nfs_v4_minor_ops
[mv
]->find_root_sec(server
, fhandle
, info
);
2887 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2888 * @server: initialized nfs_server handle
2889 * @fhandle: we fill in the pseudo-fs root file handle
2890 * @info: we fill in an FSINFO struct
2891 * @auth_probe: probe the auth flavours
2893 * Returns zero on success, or a negative errno.
2895 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2896 struct nfs_fsinfo
*info
,
2901 switch (auth_probe
) {
2903 status
= nfs4_lookup_root(server
, fhandle
, info
);
2904 if (status
!= -NFS4ERR_WRONGSEC
)
2906 /* Did user force a 'sec=' mount option? */
2907 if (server
->flags
& NFS_MOUNT_SECFLAVOUR
)
2910 status
= nfs4_do_find_root_sec(server
, fhandle
, info
);
2914 status
= nfs4_server_capabilities(server
, fhandle
);
2916 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2918 return nfs4_map_errors(status
);
2921 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
2922 struct nfs_fsinfo
*info
)
2925 struct nfs_fattr
*fattr
= info
->fattr
;
2926 struct nfs4_label
*label
= NULL
;
2928 error
= nfs4_server_capabilities(server
, mntfh
);
2930 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
2934 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
2936 return PTR_ERR(label
);
2938 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
2940 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
2941 goto err_free_label
;
2944 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
2945 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
2946 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
2949 nfs4_label_free(label
);
2955 * Get locations and (maybe) other attributes of a referral.
2956 * Note that we'll actually follow the referral later when
2957 * we detect fsid mismatch in inode revalidation
2959 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
2960 const struct qstr
*name
, struct nfs_fattr
*fattr
,
2961 struct nfs_fh
*fhandle
)
2963 int status
= -ENOMEM
;
2964 struct page
*page
= NULL
;
2965 struct nfs4_fs_locations
*locations
= NULL
;
2967 page
= alloc_page(GFP_KERNEL
);
2970 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2971 if (locations
== NULL
)
2974 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
2977 /* Make sure server returned a different fsid for the referral */
2978 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2979 dprintk("%s: server did not return a different fsid for"
2980 " a referral at %s\n", __func__
, name
->name
);
2984 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2985 nfs_fixup_referral_attributes(&locations
->fattr
);
2987 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2988 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2989 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2997 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2998 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3000 struct nfs4_getattr_arg args
= {
3002 .bitmask
= server
->attr_bitmask
,
3004 struct nfs4_getattr_res res
= {
3009 struct rpc_message msg
= {
3010 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3015 args
.bitmask
= nfs4_bitmask(server
, label
);
3017 nfs_fattr_init(fattr
);
3018 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3021 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3022 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3024 struct nfs4_exception exception
= { };
3027 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3028 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3029 err
= nfs4_handle_exception(server
, err
,
3031 } while (exception
.retry
);
3036 * The file is not closed if it is opened due to the a request to change
3037 * the size of the file. The open call will not be needed once the
3038 * VFS layer lookup-intents are implemented.
3040 * Close is called when the inode is destroyed.
3041 * If we haven't opened the file for O_WRONLY, we
3042 * need to in the size_change case to obtain a stateid.
3045 * Because OPEN is always done by name in nfsv4, it is
3046 * possible that we opened a different file by the same
3047 * name. We can recognize this race condition, but we
3048 * can't do anything about it besides returning an error.
3050 * This will be fixed with VFS changes (lookup-intent).
3053 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3054 struct iattr
*sattr
)
3056 struct inode
*inode
= dentry
->d_inode
;
3057 struct rpc_cred
*cred
= NULL
;
3058 struct nfs4_state
*state
= NULL
;
3059 struct nfs4_label
*label
= NULL
;
3062 if (pnfs_ld_layoutret_on_setattr(inode
))
3063 pnfs_commit_and_return_layout(inode
);
3065 nfs_fattr_init(fattr
);
3067 /* Deal with open(O_TRUNC) */
3068 if (sattr
->ia_valid
& ATTR_OPEN
)
3069 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3071 /* Optimization: if the end result is no change, don't RPC */
3072 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3075 /* Search for an existing open(O_WRITE) file */
3076 if (sattr
->ia_valid
& ATTR_FILE
) {
3077 struct nfs_open_context
*ctx
;
3079 ctx
= nfs_file_open_context(sattr
->ia_file
);
3086 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3088 return PTR_ERR(label
);
3090 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
3092 nfs_setattr_update_inode(inode
, sattr
);
3093 nfs_setsecurity(inode
, fattr
, label
);
3095 nfs4_label_free(label
);
3099 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3100 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3101 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3103 struct nfs_server
*server
= NFS_SERVER(dir
);
3105 struct nfs4_lookup_arg args
= {
3106 .bitmask
= server
->attr_bitmask
,
3107 .dir_fh
= NFS_FH(dir
),
3110 struct nfs4_lookup_res res
= {
3116 struct rpc_message msg
= {
3117 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3122 args
.bitmask
= nfs4_bitmask(server
, label
);
3124 nfs_fattr_init(fattr
);
3126 dprintk("NFS call lookup %s\n", name
->name
);
3127 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3128 dprintk("NFS reply lookup: %d\n", status
);
3132 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3134 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3135 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3136 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3140 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3141 struct qstr
*name
, struct nfs_fh
*fhandle
,
3142 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3144 struct nfs4_exception exception
= { };
3145 struct rpc_clnt
*client
= *clnt
;
3148 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3149 trace_nfs4_lookup(dir
, name
, err
);
3151 case -NFS4ERR_BADNAME
:
3154 case -NFS4ERR_MOVED
:
3155 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3157 case -NFS4ERR_WRONGSEC
:
3159 if (client
!= *clnt
)
3161 /* No security negotiation if the user specified 'sec=' */
3162 if (NFS_SERVER(dir
)->flags
& NFS_MOUNT_SECFLAVOUR
)
3164 client
= nfs4_create_sec_client(client
, dir
, name
);
3166 return PTR_ERR(client
);
3168 exception
.retry
= 1;
3171 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3173 } while (exception
.retry
);
3178 else if (client
!= *clnt
)
3179 rpc_shutdown_client(client
);
3184 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
3185 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3186 struct nfs4_label
*label
)
3189 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3191 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3192 if (client
!= NFS_CLIENT(dir
)) {
3193 rpc_shutdown_client(client
);
3194 nfs_fixup_secinfo_attributes(fattr
);
3200 nfs4_proc_lookup_mountpoint(struct inode
*dir
, struct qstr
*name
,
3201 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3203 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3206 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3208 return ERR_PTR(status
);
3209 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3212 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3214 struct nfs_server
*server
= NFS_SERVER(inode
);
3215 struct nfs4_accessargs args
= {
3216 .fh
= NFS_FH(inode
),
3217 .bitmask
= server
->cache_consistency_bitmask
,
3219 struct nfs4_accessres res
= {
3222 struct rpc_message msg
= {
3223 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3226 .rpc_cred
= entry
->cred
,
3228 int mode
= entry
->mask
;
3232 * Determine which access bits we want to ask for...
3234 if (mode
& MAY_READ
)
3235 args
.access
|= NFS4_ACCESS_READ
;
3236 if (S_ISDIR(inode
->i_mode
)) {
3237 if (mode
& MAY_WRITE
)
3238 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3239 if (mode
& MAY_EXEC
)
3240 args
.access
|= NFS4_ACCESS_LOOKUP
;
3242 if (mode
& MAY_WRITE
)
3243 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3244 if (mode
& MAY_EXEC
)
3245 args
.access
|= NFS4_ACCESS_EXECUTE
;
3248 res
.fattr
= nfs_alloc_fattr();
3249 if (res
.fattr
== NULL
)
3252 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3254 nfs_access_set_mask(entry
, res
.access
);
3255 nfs_refresh_inode(inode
, res
.fattr
);
3257 nfs_free_fattr(res
.fattr
);
3261 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3263 struct nfs4_exception exception
= { };
3266 err
= _nfs4_proc_access(inode
, entry
);
3267 trace_nfs4_access(inode
, err
);
3268 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3270 } while (exception
.retry
);
3275 * TODO: For the time being, we don't try to get any attributes
3276 * along with any of the zero-copy operations READ, READDIR,
3279 * In the case of the first three, we want to put the GETATTR
3280 * after the read-type operation -- this is because it is hard
3281 * to predict the length of a GETATTR response in v4, and thus
3282 * align the READ data correctly. This means that the GETATTR
3283 * may end up partially falling into the page cache, and we should
3284 * shift it into the 'tail' of the xdr_buf before processing.
3285 * To do this efficiently, we need to know the total length
3286 * of data received, which doesn't seem to be available outside
3289 * In the case of WRITE, we also want to put the GETATTR after
3290 * the operation -- in this case because we want to make sure
3291 * we get the post-operation mtime and size.
3293 * Both of these changes to the XDR layer would in fact be quite
3294 * minor, but I decided to leave them for a subsequent patch.
3296 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3297 unsigned int pgbase
, unsigned int pglen
)
3299 struct nfs4_readlink args
= {
3300 .fh
= NFS_FH(inode
),
3305 struct nfs4_readlink_res res
;
3306 struct rpc_message msg
= {
3307 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3312 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3315 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3316 unsigned int pgbase
, unsigned int pglen
)
3318 struct nfs4_exception exception
= { };
3321 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3322 trace_nfs4_readlink(inode
, err
);
3323 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3325 } while (exception
.retry
);
3330 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3333 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3336 struct nfs4_label l
, *ilabel
= NULL
;
3337 struct nfs_open_context
*ctx
;
3338 struct nfs4_state
*state
;
3342 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
3344 return PTR_ERR(ctx
);
3346 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3348 sattr
->ia_mode
&= ~current_umask();
3349 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, &opened
);
3350 if (IS_ERR(state
)) {
3351 status
= PTR_ERR(state
);
3355 nfs4_label_release_security(ilabel
);
3356 put_nfs_open_context(ctx
);
3360 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3362 struct nfs_server
*server
= NFS_SERVER(dir
);
3363 struct nfs_removeargs args
= {
3367 struct nfs_removeres res
= {
3370 struct rpc_message msg
= {
3371 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3377 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3379 update_changeattr(dir
, &res
.cinfo
);
3383 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3385 struct nfs4_exception exception
= { };
3388 err
= _nfs4_proc_remove(dir
, name
);
3389 trace_nfs4_remove(dir
, name
, err
);
3390 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3392 } while (exception
.retry
);
3396 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
3398 struct nfs_server
*server
= NFS_SERVER(dir
);
3399 struct nfs_removeargs
*args
= msg
->rpc_argp
;
3400 struct nfs_removeres
*res
= msg
->rpc_resp
;
3402 res
->server
= server
;
3403 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
3404 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
3406 nfs_fattr_init(res
->dir_attr
);
3409 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
3411 nfs4_setup_sequence(NFS_SERVER(data
->dir
),
3412 &data
->args
.seq_args
,
3417 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
3419 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
3420 struct nfs_removeres
*res
= &data
->res
;
3422 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3424 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3426 update_changeattr(dir
, &res
->cinfo
);
3430 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
3432 struct nfs_server
*server
= NFS_SERVER(dir
);
3433 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
3434 struct nfs_renameres
*res
= msg
->rpc_resp
;
3436 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
3437 res
->server
= server
;
3438 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
3441 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
3443 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
3444 &data
->args
.seq_args
,
3449 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
3450 struct inode
*new_dir
)
3452 struct nfs_renamedata
*data
= task
->tk_calldata
;
3453 struct nfs_renameres
*res
= &data
->res
;
3455 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3457 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3460 update_changeattr(old_dir
, &res
->old_cinfo
);
3461 update_changeattr(new_dir
, &res
->new_cinfo
);
3465 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3466 struct inode
*new_dir
, struct qstr
*new_name
)
3468 struct nfs_server
*server
= NFS_SERVER(old_dir
);
3469 struct nfs_renameargs arg
= {
3470 .old_dir
= NFS_FH(old_dir
),
3471 .new_dir
= NFS_FH(new_dir
),
3472 .old_name
= old_name
,
3473 .new_name
= new_name
,
3475 struct nfs_renameres res
= {
3478 struct rpc_message msg
= {
3479 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
3483 int status
= -ENOMEM
;
3485 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3487 update_changeattr(old_dir
, &res
.old_cinfo
);
3488 update_changeattr(new_dir
, &res
.new_cinfo
);
3493 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3494 struct inode
*new_dir
, struct qstr
*new_name
)
3496 struct nfs4_exception exception
= { };
3499 err
= _nfs4_proc_rename(old_dir
, old_name
,
3501 trace_nfs4_rename(old_dir
, old_name
, new_dir
, new_name
, err
);
3502 err
= nfs4_handle_exception(NFS_SERVER(old_dir
), err
,
3504 } while (exception
.retry
);
3508 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3510 struct nfs_server
*server
= NFS_SERVER(inode
);
3511 struct nfs4_link_arg arg
= {
3512 .fh
= NFS_FH(inode
),
3513 .dir_fh
= NFS_FH(dir
),
3515 .bitmask
= server
->attr_bitmask
,
3517 struct nfs4_link_res res
= {
3521 struct rpc_message msg
= {
3522 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
3526 int status
= -ENOMEM
;
3528 res
.fattr
= nfs_alloc_fattr();
3529 if (res
.fattr
== NULL
)
3532 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3533 if (IS_ERR(res
.label
)) {
3534 status
= PTR_ERR(res
.label
);
3537 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
3539 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3541 update_changeattr(dir
, &res
.cinfo
);
3542 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
3544 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
3548 nfs4_label_free(res
.label
);
3551 nfs_free_fattr(res
.fattr
);
3555 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3557 struct nfs4_exception exception
= { };
3560 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3561 _nfs4_proc_link(inode
, dir
, name
),
3563 } while (exception
.retry
);
3567 struct nfs4_createdata
{
3568 struct rpc_message msg
;
3569 struct nfs4_create_arg arg
;
3570 struct nfs4_create_res res
;
3572 struct nfs_fattr fattr
;
3573 struct nfs4_label
*label
;
3576 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
3577 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
3579 struct nfs4_createdata
*data
;
3581 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3583 struct nfs_server
*server
= NFS_SERVER(dir
);
3585 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3586 if (IS_ERR(data
->label
))
3589 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
3590 data
->msg
.rpc_argp
= &data
->arg
;
3591 data
->msg
.rpc_resp
= &data
->res
;
3592 data
->arg
.dir_fh
= NFS_FH(dir
);
3593 data
->arg
.server
= server
;
3594 data
->arg
.name
= name
;
3595 data
->arg
.attrs
= sattr
;
3596 data
->arg
.ftype
= ftype
;
3597 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
3598 data
->res
.server
= server
;
3599 data
->res
.fh
= &data
->fh
;
3600 data
->res
.fattr
= &data
->fattr
;
3601 data
->res
.label
= data
->label
;
3602 nfs_fattr_init(data
->res
.fattr
);
3610 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
3612 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
3613 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
3615 update_changeattr(dir
, &data
->res
.dir_cinfo
);
3616 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
3621 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
3623 nfs4_label_free(data
->label
);
3627 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3628 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
3629 struct nfs4_label
*label
)
3631 struct nfs4_createdata
*data
;
3632 int status
= -ENAMETOOLONG
;
3634 if (len
> NFS4_MAXPATHLEN
)
3638 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
3642 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
3643 data
->arg
.u
.symlink
.pages
= &page
;
3644 data
->arg
.u
.symlink
.len
= len
;
3645 data
->arg
.label
= label
;
3647 status
= nfs4_do_create(dir
, dentry
, data
);
3649 nfs4_free_createdata(data
);
3654 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3655 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
3657 struct nfs4_exception exception
= { };
3658 struct nfs4_label l
, *label
= NULL
;
3661 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3664 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
3665 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
3666 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3668 } while (exception
.retry
);
3670 nfs4_label_release_security(label
);
3674 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3675 struct iattr
*sattr
, struct nfs4_label
*label
)
3677 struct nfs4_createdata
*data
;
3678 int status
= -ENOMEM
;
3680 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
3684 data
->arg
.label
= label
;
3685 status
= nfs4_do_create(dir
, dentry
, data
);
3687 nfs4_free_createdata(data
);
3692 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3693 struct iattr
*sattr
)
3695 struct nfs4_exception exception
= { };
3696 struct nfs4_label l
, *label
= NULL
;
3699 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3701 sattr
->ia_mode
&= ~current_umask();
3703 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
3704 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
3705 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3707 } while (exception
.retry
);
3708 nfs4_label_release_security(label
);
3713 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3714 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3716 struct inode
*dir
= dentry
->d_inode
;
3717 struct nfs4_readdir_arg args
= {
3722 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
3725 struct nfs4_readdir_res res
;
3726 struct rpc_message msg
= {
3727 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
3734 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
3735 dentry
->d_parent
->d_name
.name
,
3736 dentry
->d_name
.name
,
3737 (unsigned long long)cookie
);
3738 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
3739 res
.pgbase
= args
.pgbase
;
3740 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3742 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
3743 status
+= args
.pgbase
;
3746 nfs_invalidate_atime(dir
);
3748 dprintk("%s: returns %d\n", __func__
, status
);
3752 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3753 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3755 struct nfs4_exception exception
= { };
3758 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
3759 pages
, count
, plus
);
3760 trace_nfs4_readdir(dentry
->d_inode
, err
);
3761 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
), err
,
3763 } while (exception
.retry
);
3767 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3768 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
3770 struct nfs4_createdata
*data
;
3771 int mode
= sattr
->ia_mode
;
3772 int status
= -ENOMEM
;
3774 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
3779 data
->arg
.ftype
= NF4FIFO
;
3780 else if (S_ISBLK(mode
)) {
3781 data
->arg
.ftype
= NF4BLK
;
3782 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3783 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3785 else if (S_ISCHR(mode
)) {
3786 data
->arg
.ftype
= NF4CHR
;
3787 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3788 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3789 } else if (!S_ISSOCK(mode
)) {
3794 data
->arg
.label
= label
;
3795 status
= nfs4_do_create(dir
, dentry
, data
);
3797 nfs4_free_createdata(data
);
3802 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3803 struct iattr
*sattr
, dev_t rdev
)
3805 struct nfs4_exception exception
= { };
3806 struct nfs4_label l
, *label
= NULL
;
3809 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3811 sattr
->ia_mode
&= ~current_umask();
3813 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
3814 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
3815 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3817 } while (exception
.retry
);
3819 nfs4_label_release_security(label
);
3824 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3825 struct nfs_fsstat
*fsstat
)
3827 struct nfs4_statfs_arg args
= {
3829 .bitmask
= server
->attr_bitmask
,
3831 struct nfs4_statfs_res res
= {
3834 struct rpc_message msg
= {
3835 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
3840 nfs_fattr_init(fsstat
->fattr
);
3841 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3844 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
3846 struct nfs4_exception exception
= { };
3849 err
= nfs4_handle_exception(server
,
3850 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
3852 } while (exception
.retry
);
3856 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3857 struct nfs_fsinfo
*fsinfo
)
3859 struct nfs4_fsinfo_arg args
= {
3861 .bitmask
= server
->attr_bitmask
,
3863 struct nfs4_fsinfo_res res
= {
3866 struct rpc_message msg
= {
3867 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3872 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3875 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3877 struct nfs4_exception exception
= { };
3878 unsigned long now
= jiffies
;
3882 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3883 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
3885 struct nfs_client
*clp
= server
->nfs_client
;
3887 spin_lock(&clp
->cl_lock
);
3888 clp
->cl_lease_time
= fsinfo
->lease_time
* HZ
;
3889 clp
->cl_last_renewal
= now
;
3890 spin_unlock(&clp
->cl_lock
);
3893 err
= nfs4_handle_exception(server
, err
, &exception
);
3894 } while (exception
.retry
);
3898 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3902 nfs_fattr_init(fsinfo
->fattr
);
3903 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3905 /* block layout checks this! */
3906 server
->pnfs_blksize
= fsinfo
->blksize
;
3907 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
->layouttype
);
3913 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3914 struct nfs_pathconf
*pathconf
)
3916 struct nfs4_pathconf_arg args
= {
3918 .bitmask
= server
->attr_bitmask
,
3920 struct nfs4_pathconf_res res
= {
3921 .pathconf
= pathconf
,
3923 struct rpc_message msg
= {
3924 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3929 /* None of the pathconf attributes are mandatory to implement */
3930 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3931 memset(pathconf
, 0, sizeof(*pathconf
));
3935 nfs_fattr_init(pathconf
->fattr
);
3936 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3939 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3940 struct nfs_pathconf
*pathconf
)
3942 struct nfs4_exception exception
= { };
3946 err
= nfs4_handle_exception(server
,
3947 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3949 } while (exception
.retry
);
3953 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
3954 const struct nfs_open_context
*ctx
,
3955 const struct nfs_lock_context
*l_ctx
,
3958 const struct nfs_lockowner
*lockowner
= NULL
;
3961 lockowner
= &l_ctx
->lockowner
;
3962 return nfs4_select_rw_stateid(stateid
, ctx
->state
, fmode
, lockowner
);
3964 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
3966 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
3967 const struct nfs_open_context
*ctx
,
3968 const struct nfs_lock_context
*l_ctx
,
3971 nfs4_stateid current_stateid
;
3973 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
))
3975 return nfs4_stateid_match(stateid
, ¤t_stateid
);
3978 static bool nfs4_error_stateid_expired(int err
)
3981 case -NFS4ERR_DELEG_REVOKED
:
3982 case -NFS4ERR_ADMIN_REVOKED
:
3983 case -NFS4ERR_BAD_STATEID
:
3984 case -NFS4ERR_STALE_STATEID
:
3985 case -NFS4ERR_OLD_STATEID
:
3986 case -NFS4ERR_OPENMODE
:
3987 case -NFS4ERR_EXPIRED
:
3993 void __nfs4_read_done_cb(struct nfs_read_data
*data
)
3995 nfs_invalidate_atime(data
->header
->inode
);
3998 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_read_data
*data
)
4000 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
4002 trace_nfs4_read(data
, task
->tk_status
);
4003 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
4004 rpc_restart_call_prepare(task
);
4008 __nfs4_read_done_cb(data
);
4009 if (task
->tk_status
> 0)
4010 renew_lease(server
, data
->timestamp
);
4014 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4015 struct nfs_readargs
*args
)
4018 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4019 nfs4_stateid_is_current(&args
->stateid
,
4024 rpc_restart_call_prepare(task
);
4028 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
4031 dprintk("--> %s\n", __func__
);
4033 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4035 if (nfs4_read_stateid_changed(task
, &data
->args
))
4037 return data
->read_done_cb
? data
->read_done_cb(task
, data
) :
4038 nfs4_read_done_cb(task
, data
);
4041 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
4043 data
->timestamp
= jiffies
;
4044 data
->read_done_cb
= nfs4_read_done_cb
;
4045 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4046 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
4049 static int nfs4_proc_read_rpc_prepare(struct rpc_task
*task
, struct nfs_read_data
*data
)
4051 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
4052 &data
->args
.seq_args
,
4056 if (nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
4057 data
->args
.lock_context
, FMODE_READ
) == -EIO
)
4059 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &data
->args
.context
->flags
)))
4064 static int nfs4_write_done_cb(struct rpc_task
*task
, struct nfs_write_data
*data
)
4066 struct inode
*inode
= data
->header
->inode
;
4068 trace_nfs4_write(data
, task
->tk_status
);
4069 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
4070 rpc_restart_call_prepare(task
);
4073 if (task
->tk_status
>= 0) {
4074 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
4075 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
4080 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4081 struct nfs_writeargs
*args
)
4084 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4085 nfs4_stateid_is_current(&args
->stateid
,
4090 rpc_restart_call_prepare(task
);
4094 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
4096 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4098 if (nfs4_write_stateid_changed(task
, &data
->args
))
4100 return data
->write_done_cb
? data
->write_done_cb(task
, data
) :
4101 nfs4_write_done_cb(task
, data
);
4105 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data
*data
)
4107 const struct nfs_pgio_header
*hdr
= data
->header
;
4109 /* Don't request attributes for pNFS or O_DIRECT writes */
4110 if (data
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4112 /* Otherwise, request attributes if and only if we don't hold
4115 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4118 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
4120 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
4122 if (!nfs4_write_need_cache_consistency_data(data
)) {
4123 data
->args
.bitmask
= NULL
;
4124 data
->res
.fattr
= NULL
;
4126 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
4128 if (!data
->write_done_cb
)
4129 data
->write_done_cb
= nfs4_write_done_cb
;
4130 data
->res
.server
= server
;
4131 data
->timestamp
= jiffies
;
4133 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4134 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4137 static int nfs4_proc_write_rpc_prepare(struct rpc_task
*task
, struct nfs_write_data
*data
)
4139 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
4140 &data
->args
.seq_args
,
4144 if (nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
4145 data
->args
.lock_context
, FMODE_WRITE
) == -EIO
)
4147 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &data
->args
.context
->flags
)))
4152 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4154 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4155 &data
->args
.seq_args
,
4160 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4162 struct inode
*inode
= data
->inode
;
4164 trace_nfs4_commit(data
, task
->tk_status
);
4165 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
4166 rpc_restart_call_prepare(task
);
4172 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4174 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4176 return data
->commit_done_cb(task
, data
);
4179 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4181 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4183 if (data
->commit_done_cb
== NULL
)
4184 data
->commit_done_cb
= nfs4_commit_done_cb
;
4185 data
->res
.server
= server
;
4186 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4187 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4190 struct nfs4_renewdata
{
4191 struct nfs_client
*client
;
4192 unsigned long timestamp
;
4196 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4197 * standalone procedure for queueing an asynchronous RENEW.
4199 static void nfs4_renew_release(void *calldata
)
4201 struct nfs4_renewdata
*data
= calldata
;
4202 struct nfs_client
*clp
= data
->client
;
4204 if (atomic_read(&clp
->cl_count
) > 1)
4205 nfs4_schedule_state_renewal(clp
);
4206 nfs_put_client(clp
);
4210 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4212 struct nfs4_renewdata
*data
= calldata
;
4213 struct nfs_client
*clp
= data
->client
;
4214 unsigned long timestamp
= data
->timestamp
;
4216 trace_nfs4_renew_async(clp
, task
->tk_status
);
4217 if (task
->tk_status
< 0) {
4218 /* Unless we're shutting down, schedule state recovery! */
4219 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4221 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4222 nfs4_schedule_lease_recovery(clp
);
4225 nfs4_schedule_path_down_recovery(clp
);
4227 do_renew_lease(clp
, timestamp
);
4230 static const struct rpc_call_ops nfs4_renew_ops
= {
4231 .rpc_call_done
= nfs4_renew_done
,
4232 .rpc_release
= nfs4_renew_release
,
4235 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4237 struct rpc_message msg
= {
4238 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4242 struct nfs4_renewdata
*data
;
4244 if (renew_flags
== 0)
4246 if (!atomic_inc_not_zero(&clp
->cl_count
))
4248 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4252 data
->timestamp
= jiffies
;
4253 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4254 &nfs4_renew_ops
, data
);
4257 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4259 struct rpc_message msg
= {
4260 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4264 unsigned long now
= jiffies
;
4267 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4270 do_renew_lease(clp
, now
);
4274 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4276 return (server
->caps
& NFS_CAP_ACLS
)
4277 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
4278 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
4281 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4282 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4285 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4287 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4288 struct page
**pages
, unsigned int *pgbase
)
4290 struct page
*newpage
, **spages
;
4296 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4297 newpage
= alloc_page(GFP_KERNEL
);
4299 if (newpage
== NULL
)
4301 memcpy(page_address(newpage
), buf
, len
);
4306 } while (buflen
!= 0);
4312 __free_page(spages
[rc
-1]);
4316 struct nfs4_cached_acl
{
4322 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4324 struct nfs_inode
*nfsi
= NFS_I(inode
);
4326 spin_lock(&inode
->i_lock
);
4327 kfree(nfsi
->nfs4_acl
);
4328 nfsi
->nfs4_acl
= acl
;
4329 spin_unlock(&inode
->i_lock
);
4332 static void nfs4_zap_acl_attr(struct inode
*inode
)
4334 nfs4_set_cached_acl(inode
, NULL
);
4337 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4339 struct nfs_inode
*nfsi
= NFS_I(inode
);
4340 struct nfs4_cached_acl
*acl
;
4343 spin_lock(&inode
->i_lock
);
4344 acl
= nfsi
->nfs4_acl
;
4347 if (buf
== NULL
) /* user is just asking for length */
4349 if (acl
->cached
== 0)
4351 ret
= -ERANGE
; /* see getxattr(2) man page */
4352 if (acl
->len
> buflen
)
4354 memcpy(buf
, acl
->data
, acl
->len
);
4358 spin_unlock(&inode
->i_lock
);
4362 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4364 struct nfs4_cached_acl
*acl
;
4365 size_t buflen
= sizeof(*acl
) + acl_len
;
4367 if (buflen
<= PAGE_SIZE
) {
4368 acl
= kmalloc(buflen
, GFP_KERNEL
);
4372 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4374 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4381 nfs4_set_cached_acl(inode
, acl
);
4385 * The getxattr API returns the required buffer length when called with a
4386 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4387 * the required buf. On a NULL buf, we send a page of data to the server
4388 * guessing that the ACL request can be serviced by a page. If so, we cache
4389 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4390 * the cache. If not so, we throw away the page, and cache the required
4391 * length. The next getxattr call will then produce another round trip to
4392 * the server, this time with the input buf of the required size.
4394 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4396 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4397 struct nfs_getaclargs args
= {
4398 .fh
= NFS_FH(inode
),
4402 struct nfs_getaclres res
= {
4405 struct rpc_message msg
= {
4406 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4410 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4411 int ret
= -ENOMEM
, i
;
4413 /* As long as we're doing a round trip to the server anyway,
4414 * let's be prepared for a page of acl data. */
4417 if (npages
> ARRAY_SIZE(pages
))
4420 for (i
= 0; i
< npages
; i
++) {
4421 pages
[i
] = alloc_page(GFP_KERNEL
);
4426 /* for decoding across pages */
4427 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
4428 if (!res
.acl_scratch
)
4431 args
.acl_len
= npages
* PAGE_SIZE
;
4432 args
.acl_pgbase
= 0;
4434 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4435 __func__
, buf
, buflen
, npages
, args
.acl_len
);
4436 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
4437 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4441 /* Handle the case where the passed-in buffer is too short */
4442 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
4443 /* Did the user only issue a request for the acl length? */
4449 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
4451 if (res
.acl_len
> buflen
) {
4455 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
4460 for (i
= 0; i
< npages
; i
++)
4462 __free_page(pages
[i
]);
4463 if (res
.acl_scratch
)
4464 __free_page(res
.acl_scratch
);
4468 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4470 struct nfs4_exception exception
= { };
4473 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
4474 trace_nfs4_get_acl(inode
, ret
);
4477 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
4478 } while (exception
.retry
);
4482 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
4484 struct nfs_server
*server
= NFS_SERVER(inode
);
4487 if (!nfs4_server_supports_acls(server
))
4489 ret
= nfs_revalidate_inode(server
, inode
);
4492 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
4493 nfs_zap_acl_cache(inode
);
4494 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
4496 /* -ENOENT is returned if there is no ACL or if there is an ACL
4497 * but no cached acl data, just the acl length */
4499 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
4502 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4504 struct nfs_server
*server
= NFS_SERVER(inode
);
4505 struct page
*pages
[NFS4ACL_MAXPAGES
];
4506 struct nfs_setaclargs arg
= {
4507 .fh
= NFS_FH(inode
),
4511 struct nfs_setaclres res
;
4512 struct rpc_message msg
= {
4513 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
4517 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4520 if (!nfs4_server_supports_acls(server
))
4522 if (npages
> ARRAY_SIZE(pages
))
4524 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
4527 nfs4_inode_return_delegation(inode
);
4528 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4531 * Free each page after tx, so the only ref left is
4532 * held by the network stack
4535 put_page(pages
[i
-1]);
4538 * Acl update can result in inode attribute update.
4539 * so mark the attribute cache invalid.
4541 spin_lock(&inode
->i_lock
);
4542 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
4543 spin_unlock(&inode
->i_lock
);
4544 nfs_access_zap_cache(inode
);
4545 nfs_zap_acl_cache(inode
);
4549 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4551 struct nfs4_exception exception
= { };
4554 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
4555 trace_nfs4_set_acl(inode
, err
);
4556 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4558 } while (exception
.retry
);
4562 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4563 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
4566 struct nfs_server
*server
= NFS_SERVER(inode
);
4567 struct nfs_fattr fattr
;
4568 struct nfs4_label label
= {0, 0, buflen
, buf
};
4570 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4571 struct nfs4_getattr_arg args
= {
4572 .fh
= NFS_FH(inode
),
4575 struct nfs4_getattr_res res
= {
4580 struct rpc_message msg
= {
4581 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
4587 nfs_fattr_init(&fattr
);
4589 ret
= rpc_call_sync(server
->client
, &msg
, 0);
4592 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
4594 if (buflen
< label
.len
)
4599 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
4602 struct nfs4_exception exception
= { };
4605 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4609 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
4610 trace_nfs4_get_security_label(inode
, err
);
4611 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4613 } while (exception
.retry
);
4617 static int _nfs4_do_set_security_label(struct inode
*inode
,
4618 struct nfs4_label
*ilabel
,
4619 struct nfs_fattr
*fattr
,
4620 struct nfs4_label
*olabel
)
4623 struct iattr sattr
= {0};
4624 struct nfs_server
*server
= NFS_SERVER(inode
);
4625 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4626 struct nfs_setattrargs args
= {
4627 .fh
= NFS_FH(inode
),
4633 struct nfs_setattrres res
= {
4638 struct rpc_message msg
= {
4639 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
4645 nfs4_stateid_copy(&args
.stateid
, &zero_stateid
);
4647 status
= rpc_call_sync(server
->client
, &msg
, 0);
4649 dprintk("%s failed: %d\n", __func__
, status
);
4654 static int nfs4_do_set_security_label(struct inode
*inode
,
4655 struct nfs4_label
*ilabel
,
4656 struct nfs_fattr
*fattr
,
4657 struct nfs4_label
*olabel
)
4659 struct nfs4_exception exception
= { };
4663 err
= _nfs4_do_set_security_label(inode
, ilabel
,
4665 trace_nfs4_set_security_label(inode
, err
);
4666 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4668 } while (exception
.retry
);
4673 nfs4_set_security_label(struct dentry
*dentry
, const void *buf
, size_t buflen
)
4675 struct nfs4_label ilabel
, *olabel
= NULL
;
4676 struct nfs_fattr fattr
;
4677 struct rpc_cred
*cred
;
4678 struct inode
*inode
= dentry
->d_inode
;
4681 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4684 nfs_fattr_init(&fattr
);
4688 ilabel
.label
= (char *)buf
;
4689 ilabel
.len
= buflen
;
4691 cred
= rpc_lookup_cred();
4693 return PTR_ERR(cred
);
4695 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
4696 if (IS_ERR(olabel
)) {
4697 status
= -PTR_ERR(olabel
);
4701 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
4703 nfs_setsecurity(inode
, &fattr
, olabel
);
4705 nfs4_label_free(olabel
);
4710 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4714 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
4716 struct nfs_client
*clp
= server
->nfs_client
;
4718 if (task
->tk_status
>= 0)
4720 switch(task
->tk_status
) {
4721 case -NFS4ERR_DELEG_REVOKED
:
4722 case -NFS4ERR_ADMIN_REVOKED
:
4723 case -NFS4ERR_BAD_STATEID
:
4726 nfs_remove_bad_delegation(state
->inode
);
4727 case -NFS4ERR_OPENMODE
:
4730 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4731 goto stateid_invalid
;
4732 goto wait_on_recovery
;
4733 case -NFS4ERR_EXPIRED
:
4734 if (state
!= NULL
) {
4735 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4736 goto stateid_invalid
;
4738 case -NFS4ERR_STALE_STATEID
:
4739 case -NFS4ERR_STALE_CLIENTID
:
4740 nfs4_schedule_lease_recovery(clp
);
4741 goto wait_on_recovery
;
4742 #if defined(CONFIG_NFS_V4_1)
4743 case -NFS4ERR_BADSESSION
:
4744 case -NFS4ERR_BADSLOT
:
4745 case -NFS4ERR_BAD_HIGH_SLOT
:
4746 case -NFS4ERR_DEADSESSION
:
4747 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4748 case -NFS4ERR_SEQ_FALSE_RETRY
:
4749 case -NFS4ERR_SEQ_MISORDERED
:
4750 dprintk("%s ERROR %d, Reset session\n", __func__
,
4752 nfs4_schedule_session_recovery(clp
->cl_session
, task
->tk_status
);
4753 task
->tk_status
= 0;
4755 #endif /* CONFIG_NFS_V4_1 */
4756 case -NFS4ERR_DELAY
:
4757 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
4758 case -NFS4ERR_GRACE
:
4759 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
4760 task
->tk_status
= 0;
4762 case -NFS4ERR_RETRY_UNCACHED_REP
:
4763 case -NFS4ERR_OLD_STATEID
:
4764 task
->tk_status
= 0;
4767 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
4770 task
->tk_status
= -EIO
;
4773 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
4774 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
4775 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
4776 task
->tk_status
= 0;
4780 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
4781 nfs4_verifier
*bootverf
)
4785 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
4786 /* An impossible timestamp guarantees this value
4787 * will never match a generated boot time. */
4789 verf
[1] = cpu_to_be32(NSEC_PER_SEC
+ 1);
4791 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
4792 verf
[0] = cpu_to_be32(nn
->boot_time
.tv_sec
);
4793 verf
[1] = cpu_to_be32(nn
->boot_time
.tv_nsec
);
4795 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
4799 nfs4_init_nonuniform_client_string(const struct nfs_client
*clp
,
4800 char *buf
, size_t len
)
4802 unsigned int result
;
4805 result
= scnprintf(buf
, len
, "Linux NFSv4.0 %s/%s %s",
4807 rpc_peeraddr2str(clp
->cl_rpcclient
,
4809 rpc_peeraddr2str(clp
->cl_rpcclient
,
4810 RPC_DISPLAY_PROTO
));
4816 nfs4_init_uniform_client_string(const struct nfs_client
*clp
,
4817 char *buf
, size_t len
)
4819 const char *nodename
= clp
->cl_rpcclient
->cl_nodename
;
4821 if (nfs4_client_id_uniquifier
[0] != '\0')
4822 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s/%s",
4823 clp
->rpc_ops
->version
,
4824 clp
->cl_minorversion
,
4825 nfs4_client_id_uniquifier
,
4827 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s",
4828 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
4833 * nfs4_proc_setclientid - Negotiate client ID
4834 * @clp: state data structure
4835 * @program: RPC program for NFSv4 callback service
4836 * @port: IP port number for NFS4 callback service
4837 * @cred: RPC credential to use for this call
4838 * @res: where to place the result
4840 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4842 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
4843 unsigned short port
, struct rpc_cred
*cred
,
4844 struct nfs4_setclientid_res
*res
)
4846 nfs4_verifier sc_verifier
;
4847 struct nfs4_setclientid setclientid
= {
4848 .sc_verifier
= &sc_verifier
,
4850 .sc_cb_ident
= clp
->cl_cb_ident
,
4852 struct rpc_message msg
= {
4853 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
4854 .rpc_argp
= &setclientid
,
4860 /* nfs_client_id4 */
4861 nfs4_init_boot_verifier(clp
, &sc_verifier
);
4862 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
4863 setclientid
.sc_name_len
=
4864 nfs4_init_uniform_client_string(clp
,
4865 setclientid
.sc_name
,
4866 sizeof(setclientid
.sc_name
));
4868 setclientid
.sc_name_len
=
4869 nfs4_init_nonuniform_client_string(clp
,
4870 setclientid
.sc_name
,
4871 sizeof(setclientid
.sc_name
));
4874 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
4875 sizeof(setclientid
.sc_netid
), "%s",
4876 rpc_peeraddr2str(clp
->cl_rpcclient
,
4877 RPC_DISPLAY_NETID
));
4879 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
4880 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
4881 clp
->cl_ipaddr
, port
>> 8, port
& 255);
4883 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4884 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4885 setclientid
.sc_name_len
, setclientid
.sc_name
);
4886 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4887 trace_nfs4_setclientid(clp
, status
);
4888 dprintk("NFS reply setclientid: %d\n", status
);
4893 * nfs4_proc_setclientid_confirm - Confirm client ID
4894 * @clp: state data structure
4895 * @res: result of a previous SETCLIENTID
4896 * @cred: RPC credential to use for this call
4898 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4900 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
4901 struct nfs4_setclientid_res
*arg
,
4902 struct rpc_cred
*cred
)
4904 struct rpc_message msg
= {
4905 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
4911 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4912 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4914 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4915 trace_nfs4_setclientid_confirm(clp
, status
);
4916 dprintk("NFS reply setclientid_confirm: %d\n", status
);
4920 struct nfs4_delegreturndata
{
4921 struct nfs4_delegreturnargs args
;
4922 struct nfs4_delegreturnres res
;
4924 nfs4_stateid stateid
;
4925 unsigned long timestamp
;
4926 struct nfs_fattr fattr
;
4930 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
4932 struct nfs4_delegreturndata
*data
= calldata
;
4934 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4937 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
4938 switch (task
->tk_status
) {
4939 case -NFS4ERR_STALE_STATEID
:
4940 case -NFS4ERR_EXPIRED
:
4942 renew_lease(data
->res
.server
, data
->timestamp
);
4945 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
4947 rpc_restart_call_prepare(task
);
4951 data
->rpc_status
= task
->tk_status
;
4954 static void nfs4_delegreturn_release(void *calldata
)
4959 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
4961 struct nfs4_delegreturndata
*d_data
;
4963 d_data
= (struct nfs4_delegreturndata
*)data
;
4965 nfs4_setup_sequence(d_data
->res
.server
,
4966 &d_data
->args
.seq_args
,
4967 &d_data
->res
.seq_res
,
4971 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
4972 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
4973 .rpc_call_done
= nfs4_delegreturn_done
,
4974 .rpc_release
= nfs4_delegreturn_release
,
4977 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
4979 struct nfs4_delegreturndata
*data
;
4980 struct nfs_server
*server
= NFS_SERVER(inode
);
4981 struct rpc_task
*task
;
4982 struct rpc_message msg
= {
4983 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
4986 struct rpc_task_setup task_setup_data
= {
4987 .rpc_client
= server
->client
,
4988 .rpc_message
= &msg
,
4989 .callback_ops
= &nfs4_delegreturn_ops
,
4990 .flags
= RPC_TASK_ASYNC
,
4994 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
4997 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4998 data
->args
.fhandle
= &data
->fh
;
4999 data
->args
.stateid
= &data
->stateid
;
5000 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5001 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5002 nfs4_stateid_copy(&data
->stateid
, stateid
);
5003 data
->res
.fattr
= &data
->fattr
;
5004 data
->res
.server
= server
;
5005 nfs_fattr_init(data
->res
.fattr
);
5006 data
->timestamp
= jiffies
;
5007 data
->rpc_status
= 0;
5009 task_setup_data
.callback_data
= data
;
5010 msg
.rpc_argp
= &data
->args
;
5011 msg
.rpc_resp
= &data
->res
;
5012 task
= rpc_run_task(&task_setup_data
);
5014 return PTR_ERR(task
);
5017 status
= nfs4_wait_for_completion_rpc_task(task
);
5020 status
= data
->rpc_status
;
5022 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5024 nfs_refresh_inode(inode
, &data
->fattr
);
5030 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5032 struct nfs_server
*server
= NFS_SERVER(inode
);
5033 struct nfs4_exception exception
= { };
5036 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5037 trace_nfs4_delegreturn(inode
, err
);
5039 case -NFS4ERR_STALE_STATEID
:
5040 case -NFS4ERR_EXPIRED
:
5044 err
= nfs4_handle_exception(server
, err
, &exception
);
5045 } while (exception
.retry
);
5049 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5050 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5053 * sleep, with exponential backoff, and retry the LOCK operation.
5055 static unsigned long
5056 nfs4_set_lock_task_retry(unsigned long timeout
)
5058 freezable_schedule_timeout_killable_unsafe(timeout
);
5060 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
5061 return NFS4_LOCK_MAXTIMEOUT
;
5065 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5067 struct inode
*inode
= state
->inode
;
5068 struct nfs_server
*server
= NFS_SERVER(inode
);
5069 struct nfs_client
*clp
= server
->nfs_client
;
5070 struct nfs_lockt_args arg
= {
5071 .fh
= NFS_FH(inode
),
5074 struct nfs_lockt_res res
= {
5077 struct rpc_message msg
= {
5078 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5081 .rpc_cred
= state
->owner
->so_cred
,
5083 struct nfs4_lock_state
*lsp
;
5086 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5087 status
= nfs4_set_lock_state(state
, request
);
5090 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5091 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5092 arg
.lock_owner
.s_dev
= server
->s_dev
;
5093 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5096 request
->fl_type
= F_UNLCK
;
5098 case -NFS4ERR_DENIED
:
5101 request
->fl_ops
->fl_release_private(request
);
5102 request
->fl_ops
= NULL
;
5107 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5109 struct nfs4_exception exception
= { };
5113 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5114 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5115 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5117 } while (exception
.retry
);
5121 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
5124 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
5126 res
= posix_lock_file_wait(file
, fl
);
5129 res
= flock_lock_file_wait(file
, fl
);
5137 struct nfs4_unlockdata
{
5138 struct nfs_locku_args arg
;
5139 struct nfs_locku_res res
;
5140 struct nfs4_lock_state
*lsp
;
5141 struct nfs_open_context
*ctx
;
5142 struct file_lock fl
;
5143 const struct nfs_server
*server
;
5144 unsigned long timestamp
;
5147 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5148 struct nfs_open_context
*ctx
,
5149 struct nfs4_lock_state
*lsp
,
5150 struct nfs_seqid
*seqid
)
5152 struct nfs4_unlockdata
*p
;
5153 struct inode
*inode
= lsp
->ls_state
->inode
;
5155 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5158 p
->arg
.fh
= NFS_FH(inode
);
5160 p
->arg
.seqid
= seqid
;
5161 p
->res
.seqid
= seqid
;
5162 p
->arg
.stateid
= &lsp
->ls_stateid
;
5164 atomic_inc(&lsp
->ls_count
);
5165 /* Ensure we don't close file until we're done freeing locks! */
5166 p
->ctx
= get_nfs_open_context(ctx
);
5167 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5168 p
->server
= NFS_SERVER(inode
);
5172 static void nfs4_locku_release_calldata(void *data
)
5174 struct nfs4_unlockdata
*calldata
= data
;
5175 nfs_free_seqid(calldata
->arg
.seqid
);
5176 nfs4_put_lock_state(calldata
->lsp
);
5177 put_nfs_open_context(calldata
->ctx
);
5181 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5183 struct nfs4_unlockdata
*calldata
= data
;
5185 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5187 switch (task
->tk_status
) {
5189 nfs4_stateid_copy(&calldata
->lsp
->ls_stateid
,
5190 &calldata
->res
.stateid
);
5191 renew_lease(calldata
->server
, calldata
->timestamp
);
5193 case -NFS4ERR_BAD_STATEID
:
5194 case -NFS4ERR_OLD_STATEID
:
5195 case -NFS4ERR_STALE_STATEID
:
5196 case -NFS4ERR_EXPIRED
:
5199 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
5200 rpc_restart_call_prepare(task
);
5202 nfs_release_seqid(calldata
->arg
.seqid
);
5205 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5207 struct nfs4_unlockdata
*calldata
= data
;
5209 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5211 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5212 /* Note: exit _without_ running nfs4_locku_done */
5215 calldata
->timestamp
= jiffies
;
5216 if (nfs4_setup_sequence(calldata
->server
,
5217 &calldata
->arg
.seq_args
,
5218 &calldata
->res
.seq_res
,
5220 nfs_release_seqid(calldata
->arg
.seqid
);
5223 task
->tk_action
= NULL
;
5225 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5228 static const struct rpc_call_ops nfs4_locku_ops
= {
5229 .rpc_call_prepare
= nfs4_locku_prepare
,
5230 .rpc_call_done
= nfs4_locku_done
,
5231 .rpc_release
= nfs4_locku_release_calldata
,
5234 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5235 struct nfs_open_context
*ctx
,
5236 struct nfs4_lock_state
*lsp
,
5237 struct nfs_seqid
*seqid
)
5239 struct nfs4_unlockdata
*data
;
5240 struct rpc_message msg
= {
5241 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5242 .rpc_cred
= ctx
->cred
,
5244 struct rpc_task_setup task_setup_data
= {
5245 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5246 .rpc_message
= &msg
,
5247 .callback_ops
= &nfs4_locku_ops
,
5248 .workqueue
= nfsiod_workqueue
,
5249 .flags
= RPC_TASK_ASYNC
,
5252 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5253 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5255 /* Ensure this is an unlock - when canceling a lock, the
5256 * canceled lock is passed in, and it won't be an unlock.
5258 fl
->fl_type
= F_UNLCK
;
5260 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5262 nfs_free_seqid(seqid
);
5263 return ERR_PTR(-ENOMEM
);
5266 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5267 msg
.rpc_argp
= &data
->arg
;
5268 msg
.rpc_resp
= &data
->res
;
5269 task_setup_data
.callback_data
= data
;
5270 return rpc_run_task(&task_setup_data
);
5273 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5275 struct inode
*inode
= state
->inode
;
5276 struct nfs4_state_owner
*sp
= state
->owner
;
5277 struct nfs_inode
*nfsi
= NFS_I(inode
);
5278 struct nfs_seqid
*seqid
;
5279 struct nfs4_lock_state
*lsp
;
5280 struct rpc_task
*task
;
5282 unsigned char fl_flags
= request
->fl_flags
;
5284 status
= nfs4_set_lock_state(state
, request
);
5285 /* Unlock _before_ we do the RPC call */
5286 request
->fl_flags
|= FL_EXISTS
;
5287 /* Exclude nfs_delegation_claim_locks() */
5288 mutex_lock(&sp
->so_delegreturn_mutex
);
5289 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5290 down_read(&nfsi
->rwsem
);
5291 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
5292 up_read(&nfsi
->rwsem
);
5293 mutex_unlock(&sp
->so_delegreturn_mutex
);
5296 up_read(&nfsi
->rwsem
);
5297 mutex_unlock(&sp
->so_delegreturn_mutex
);
5300 /* Is this a delegated lock? */
5301 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5302 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5304 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5308 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5309 status
= PTR_ERR(task
);
5312 status
= nfs4_wait_for_completion_rpc_task(task
);
5315 request
->fl_flags
= fl_flags
;
5316 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
5320 struct nfs4_lockdata
{
5321 struct nfs_lock_args arg
;
5322 struct nfs_lock_res res
;
5323 struct nfs4_lock_state
*lsp
;
5324 struct nfs_open_context
*ctx
;
5325 struct file_lock fl
;
5326 unsigned long timestamp
;
5329 struct nfs_server
*server
;
5332 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
5333 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
5336 struct nfs4_lockdata
*p
;
5337 struct inode
*inode
= lsp
->ls_state
->inode
;
5338 struct nfs_server
*server
= NFS_SERVER(inode
);
5340 p
= kzalloc(sizeof(*p
), gfp_mask
);
5344 p
->arg
.fh
= NFS_FH(inode
);
5346 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
5347 if (p
->arg
.open_seqid
== NULL
)
5349 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
5350 if (p
->arg
.lock_seqid
== NULL
)
5351 goto out_free_seqid
;
5352 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
5353 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5354 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5355 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
5356 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
5359 atomic_inc(&lsp
->ls_count
);
5360 p
->ctx
= get_nfs_open_context(ctx
);
5361 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5364 nfs_free_seqid(p
->arg
.open_seqid
);
5370 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
5372 struct nfs4_lockdata
*data
= calldata
;
5373 struct nfs4_state
*state
= data
->lsp
->ls_state
;
5375 dprintk("%s: begin!\n", __func__
);
5376 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
5378 /* Do we need to do an open_to_lock_owner? */
5379 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
5380 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
5381 goto out_release_lock_seqid
;
5383 data
->arg
.open_stateid
= &state
->open_stateid
;
5384 data
->arg
.new_lock_owner
= 1;
5385 data
->res
.open_seqid
= data
->arg
.open_seqid
;
5387 data
->arg
.new_lock_owner
= 0;
5388 if (!nfs4_valid_open_stateid(state
)) {
5389 data
->rpc_status
= -EBADF
;
5390 task
->tk_action
= NULL
;
5391 goto out_release_open_seqid
;
5393 data
->timestamp
= jiffies
;
5394 if (nfs4_setup_sequence(data
->server
,
5395 &data
->arg
.seq_args
,
5399 out_release_open_seqid
:
5400 nfs_release_seqid(data
->arg
.open_seqid
);
5401 out_release_lock_seqid
:
5402 nfs_release_seqid(data
->arg
.lock_seqid
);
5404 nfs4_sequence_done(task
, &data
->res
.seq_res
);
5405 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
5408 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
5410 struct nfs4_lockdata
*data
= calldata
;
5412 dprintk("%s: begin!\n", __func__
);
5414 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5417 data
->rpc_status
= task
->tk_status
;
5418 if (data
->arg
.new_lock_owner
!= 0) {
5419 if (data
->rpc_status
== 0)
5420 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
5424 if (data
->rpc_status
== 0) {
5425 nfs4_stateid_copy(&data
->lsp
->ls_stateid
, &data
->res
.stateid
);
5426 set_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
);
5427 renew_lease(NFS_SERVER(data
->ctx
->dentry
->d_inode
), data
->timestamp
);
5430 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
5433 static void nfs4_lock_release(void *calldata
)
5435 struct nfs4_lockdata
*data
= calldata
;
5437 dprintk("%s: begin!\n", __func__
);
5438 nfs_free_seqid(data
->arg
.open_seqid
);
5439 if (data
->cancelled
!= 0) {
5440 struct rpc_task
*task
;
5441 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
5442 data
->arg
.lock_seqid
);
5444 rpc_put_task_async(task
);
5445 dprintk("%s: cancelling lock!\n", __func__
);
5447 nfs_free_seqid(data
->arg
.lock_seqid
);
5448 nfs4_put_lock_state(data
->lsp
);
5449 put_nfs_open_context(data
->ctx
);
5451 dprintk("%s: done!\n", __func__
);
5454 static const struct rpc_call_ops nfs4_lock_ops
= {
5455 .rpc_call_prepare
= nfs4_lock_prepare
,
5456 .rpc_call_done
= nfs4_lock_done
,
5457 .rpc_release
= nfs4_lock_release
,
5460 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
5463 case -NFS4ERR_ADMIN_REVOKED
:
5464 case -NFS4ERR_BAD_STATEID
:
5465 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5466 if (new_lock_owner
!= 0 ||
5467 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
5468 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
5470 case -NFS4ERR_STALE_STATEID
:
5471 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5472 case -NFS4ERR_EXPIRED
:
5473 nfs4_schedule_lease_recovery(server
->nfs_client
);
5477 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
5479 struct nfs4_lockdata
*data
;
5480 struct rpc_task
*task
;
5481 struct rpc_message msg
= {
5482 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
5483 .rpc_cred
= state
->owner
->so_cred
,
5485 struct rpc_task_setup task_setup_data
= {
5486 .rpc_client
= NFS_CLIENT(state
->inode
),
5487 .rpc_message
= &msg
,
5488 .callback_ops
= &nfs4_lock_ops
,
5489 .workqueue
= nfsiod_workqueue
,
5490 .flags
= RPC_TASK_ASYNC
,
5494 dprintk("%s: begin!\n", __func__
);
5495 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
5496 fl
->fl_u
.nfs4_fl
.owner
,
5497 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
5501 data
->arg
.block
= 1;
5502 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5503 msg
.rpc_argp
= &data
->arg
;
5504 msg
.rpc_resp
= &data
->res
;
5505 task_setup_data
.callback_data
= data
;
5506 if (recovery_type
> NFS_LOCK_NEW
) {
5507 if (recovery_type
== NFS_LOCK_RECLAIM
)
5508 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
5509 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
5511 task
= rpc_run_task(&task_setup_data
);
5513 return PTR_ERR(task
);
5514 ret
= nfs4_wait_for_completion_rpc_task(task
);
5516 ret
= data
->rpc_status
;
5518 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
5519 data
->arg
.new_lock_owner
, ret
);
5521 data
->cancelled
= 1;
5523 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
5527 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
5529 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5530 struct nfs4_exception exception
= {
5531 .inode
= state
->inode
,
5536 /* Cache the lock if possible... */
5537 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5539 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
5540 trace_nfs4_lock_reclaim(request
, state
, F_SETLK
, err
);
5541 if (err
!= -NFS4ERR_DELAY
)
5543 nfs4_handle_exception(server
, err
, &exception
);
5544 } while (exception
.retry
);
5548 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5550 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5551 struct nfs4_exception exception
= {
5552 .inode
= state
->inode
,
5556 err
= nfs4_set_lock_state(state
, request
);
5559 if (!recover_lost_locks
) {
5560 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
5564 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5566 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
5567 trace_nfs4_lock_expired(request
, state
, F_SETLK
, err
);
5571 case -NFS4ERR_GRACE
:
5572 case -NFS4ERR_DELAY
:
5573 nfs4_handle_exception(server
, err
, &exception
);
5576 } while (exception
.retry
);
5581 #if defined(CONFIG_NFS_V4_1)
5583 * nfs41_check_expired_locks - possibly free a lock stateid
5585 * @state: NFSv4 state for an inode
5587 * Returns NFS_OK if recovery for this stateid is now finished.
5588 * Otherwise a negative NFS4ERR value is returned.
5590 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
5592 int status
, ret
= -NFS4ERR_BAD_STATEID
;
5593 struct nfs4_lock_state
*lsp
;
5594 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5596 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
5597 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
5598 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
5600 status
= nfs41_test_stateid(server
,
5603 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
5604 if (status
!= NFS_OK
) {
5605 /* Free the stateid unless the server
5606 * informs us the stateid is unrecognized. */
5607 if (status
!= -NFS4ERR_BAD_STATEID
)
5608 nfs41_free_stateid(server
,
5611 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
5620 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5622 int status
= NFS_OK
;
5624 if (test_bit(LK_STATE_IN_USE
, &state
->flags
))
5625 status
= nfs41_check_expired_locks(state
);
5626 if (status
!= NFS_OK
)
5627 status
= nfs4_lock_expired(state
, request
);
5632 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5634 struct nfs4_state_owner
*sp
= state
->owner
;
5635 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
5636 unsigned char fl_flags
= request
->fl_flags
;
5638 int status
= -ENOLCK
;
5640 if ((fl_flags
& FL_POSIX
) &&
5641 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
5643 /* Is this a delegated open? */
5644 status
= nfs4_set_lock_state(state
, request
);
5647 request
->fl_flags
|= FL_ACCESS
;
5648 status
= do_vfs_lock(request
->fl_file
, request
);
5651 down_read(&nfsi
->rwsem
);
5652 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
5653 /* Yes: cache locks! */
5654 /* ...but avoid races with delegation recall... */
5655 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
5656 status
= do_vfs_lock(request
->fl_file
, request
);
5659 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
5660 up_read(&nfsi
->rwsem
);
5661 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
5664 down_read(&nfsi
->rwsem
);
5665 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
)) {
5666 status
= -NFS4ERR_DELAY
;
5669 /* Note: we always want to sleep here! */
5670 request
->fl_flags
= fl_flags
| FL_SLEEP
;
5671 if (do_vfs_lock(request
->fl_file
, request
) < 0)
5672 printk(KERN_WARNING
"NFS: %s: VFS is out of sync with lock "
5673 "manager!\n", __func__
);
5675 up_read(&nfsi
->rwsem
);
5677 request
->fl_flags
= fl_flags
;
5681 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5683 struct nfs4_exception exception
= {
5685 .inode
= state
->inode
,
5690 err
= _nfs4_proc_setlk(state
, cmd
, request
);
5691 trace_nfs4_set_lock(request
, state
, cmd
, err
);
5692 if (err
== -NFS4ERR_DENIED
)
5694 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
5696 } while (exception
.retry
);
5701 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
5703 struct nfs_open_context
*ctx
;
5704 struct nfs4_state
*state
;
5705 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
5708 /* verify open state */
5709 ctx
= nfs_file_open_context(filp
);
5712 if (request
->fl_start
< 0 || request
->fl_end
< 0)
5715 if (IS_GETLK(cmd
)) {
5717 return nfs4_proc_getlk(state
, F_GETLK
, request
);
5721 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
5724 if (request
->fl_type
== F_UNLCK
) {
5726 return nfs4_proc_unlck(state
, cmd
, request
);
5733 * Don't rely on the VFS having checked the file open mode,
5734 * since it won't do this for flock() locks.
5736 switch (request
->fl_type
) {
5738 if (!(filp
->f_mode
& FMODE_READ
))
5742 if (!(filp
->f_mode
& FMODE_WRITE
))
5747 status
= nfs4_proc_setlk(state
, cmd
, request
);
5748 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
5750 timeout
= nfs4_set_lock_task_retry(timeout
);
5751 status
= -ERESTARTSYS
;
5754 } while(status
< 0);
5758 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
5760 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5763 err
= nfs4_set_lock_state(state
, fl
);
5766 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
5767 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
5770 struct nfs_release_lockowner_data
{
5771 struct nfs4_lock_state
*lsp
;
5772 struct nfs_server
*server
;
5773 struct nfs_release_lockowner_args args
;
5774 struct nfs4_sequence_args seq_args
;
5775 struct nfs4_sequence_res seq_res
;
5778 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
5780 struct nfs_release_lockowner_data
*data
= calldata
;
5781 nfs40_setup_sequence(data
->server
,
5782 &data
->seq_args
, &data
->seq_res
, task
);
5785 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
5787 struct nfs_release_lockowner_data
*data
= calldata
;
5788 nfs40_sequence_done(task
, &data
->seq_res
);
5791 static void nfs4_release_lockowner_release(void *calldata
)
5793 struct nfs_release_lockowner_data
*data
= calldata
;
5794 nfs4_free_lock_state(data
->server
, data
->lsp
);
5798 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
5799 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
5800 .rpc_call_done
= nfs4_release_lockowner_done
,
5801 .rpc_release
= nfs4_release_lockowner_release
,
5804 static int nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
5806 struct nfs_release_lockowner_data
*data
;
5807 struct rpc_message msg
= {
5808 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
5811 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
5814 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
5817 nfs4_init_sequence(&data
->seq_args
, &data
->seq_res
, 0);
5819 data
->server
= server
;
5820 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5821 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5822 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
5824 msg
.rpc_argp
= &data
->args
;
5825 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
5829 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5831 static int nfs4_xattr_set_nfs4_acl(struct dentry
*dentry
, const char *key
,
5832 const void *buf
, size_t buflen
,
5833 int flags
, int type
)
5835 if (strcmp(key
, "") != 0)
5838 return nfs4_proc_set_acl(dentry
->d_inode
, buf
, buflen
);
5841 static int nfs4_xattr_get_nfs4_acl(struct dentry
*dentry
, const char *key
,
5842 void *buf
, size_t buflen
, int type
)
5844 if (strcmp(key
, "") != 0)
5847 return nfs4_proc_get_acl(dentry
->d_inode
, buf
, buflen
);
5850 static size_t nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
, char *list
,
5851 size_t list_len
, const char *name
,
5852 size_t name_len
, int type
)
5854 size_t len
= sizeof(XATTR_NAME_NFSV4_ACL
);
5856 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
5859 if (list
&& len
<= list_len
)
5860 memcpy(list
, XATTR_NAME_NFSV4_ACL
, len
);
5864 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5865 static inline int nfs4_server_supports_labels(struct nfs_server
*server
)
5867 return server
->caps
& NFS_CAP_SECURITY_LABEL
;
5870 static int nfs4_xattr_set_nfs4_label(struct dentry
*dentry
, const char *key
,
5871 const void *buf
, size_t buflen
,
5872 int flags
, int type
)
5874 if (security_ismaclabel(key
))
5875 return nfs4_set_security_label(dentry
, buf
, buflen
);
5880 static int nfs4_xattr_get_nfs4_label(struct dentry
*dentry
, const char *key
,
5881 void *buf
, size_t buflen
, int type
)
5883 if (security_ismaclabel(key
))
5884 return nfs4_get_security_label(dentry
->d_inode
, buf
, buflen
);
5888 static size_t nfs4_xattr_list_nfs4_label(struct dentry
*dentry
, char *list
,
5889 size_t list_len
, const char *name
,
5890 size_t name_len
, int type
)
5894 if (nfs_server_capable(dentry
->d_inode
, NFS_CAP_SECURITY_LABEL
)) {
5895 len
= security_inode_listsecurity(dentry
->d_inode
, NULL
, 0);
5896 if (list
&& len
<= list_len
)
5897 security_inode_listsecurity(dentry
->d_inode
, list
, len
);
5902 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
5903 .prefix
= XATTR_SECURITY_PREFIX
,
5904 .list
= nfs4_xattr_list_nfs4_label
,
5905 .get
= nfs4_xattr_get_nfs4_label
,
5906 .set
= nfs4_xattr_set_nfs4_label
,
5912 * nfs_fhget will use either the mounted_on_fileid or the fileid
5914 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
5916 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
5917 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
5918 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
5919 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
5922 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
5923 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
5924 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
5928 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5929 const struct qstr
*name
,
5930 struct nfs4_fs_locations
*fs_locations
,
5933 struct nfs_server
*server
= NFS_SERVER(dir
);
5935 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
5937 struct nfs4_fs_locations_arg args
= {
5938 .dir_fh
= NFS_FH(dir
),
5943 struct nfs4_fs_locations_res res
= {
5944 .fs_locations
= fs_locations
,
5946 struct rpc_message msg
= {
5947 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
5953 dprintk("%s: start\n", __func__
);
5955 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5956 * is not supported */
5957 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
5958 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
5960 bitmask
[0] |= FATTR4_WORD0_FILEID
;
5962 nfs_fattr_init(&fs_locations
->fattr
);
5963 fs_locations
->server
= server
;
5964 fs_locations
->nlocations
= 0;
5965 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5966 dprintk("%s: returned status = %d\n", __func__
, status
);
5970 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5971 const struct qstr
*name
,
5972 struct nfs4_fs_locations
*fs_locations
,
5975 struct nfs4_exception exception
= { };
5978 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
5979 fs_locations
, page
);
5980 trace_nfs4_get_fs_locations(dir
, name
, err
);
5981 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
5983 } while (exception
.retry
);
5988 * If 'use_integrity' is true and the state managment nfs_client
5989 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5990 * and the machine credential as per RFC3530bis and RFC5661 Security
5991 * Considerations sections. Otherwise, just use the user cred with the
5992 * filesystem's rpc_client.
5994 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
5997 struct nfs4_secinfo_arg args
= {
5998 .dir_fh
= NFS_FH(dir
),
6001 struct nfs4_secinfo_res res
= {
6004 struct rpc_message msg
= {
6005 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
6009 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
6010 struct rpc_cred
*cred
= NULL
;
6012 if (use_integrity
) {
6013 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
6014 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
6015 msg
.rpc_cred
= cred
;
6018 dprintk("NFS call secinfo %s\n", name
->name
);
6020 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
6021 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
6023 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
6025 dprintk("NFS reply secinfo: %d\n", status
);
6033 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
6034 struct nfs4_secinfo_flavors
*flavors
)
6036 struct nfs4_exception exception
= { };
6039 err
= -NFS4ERR_WRONGSEC
;
6041 /* try to use integrity protection with machine cred */
6042 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
6043 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
6046 * if unable to use integrity protection, or SECINFO with
6047 * integrity protection returns NFS4ERR_WRONGSEC (which is
6048 * disallowed by spec, but exists in deployed servers) use
6049 * the current filesystem's rpc_client and the user cred.
6051 if (err
== -NFS4ERR_WRONGSEC
)
6052 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
6054 trace_nfs4_secinfo(dir
, name
, err
);
6055 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6057 } while (exception
.retry
);
6061 #ifdef CONFIG_NFS_V4_1
6063 * Check the exchange flags returned by the server for invalid flags, having
6064 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6067 static int nfs4_check_cl_exchange_flags(u32 flags
)
6069 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
6071 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
6072 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
6074 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
6078 return -NFS4ERR_INVAL
;
6082 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
6083 struct nfs41_server_scope
*b
)
6085 if (a
->server_scope_sz
== b
->server_scope_sz
&&
6086 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
6093 * nfs4_proc_bind_conn_to_session()
6095 * The 4.1 client currently uses the same TCP connection for the
6096 * fore and backchannel.
6098 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6101 struct nfs41_bind_conn_to_session_res res
;
6102 struct rpc_message msg
= {
6104 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
6110 dprintk("--> %s\n", __func__
);
6112 res
.session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
6113 if (unlikely(res
.session
== NULL
)) {
6118 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6119 trace_nfs4_bind_conn_to_session(clp
, status
);
6121 if (memcmp(res
.session
->sess_id
.data
,
6122 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
6123 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
6127 if (res
.dir
!= NFS4_CDFS4_BOTH
) {
6128 dprintk("NFS: %s: Unexpected direction from server\n",
6133 if (res
.use_conn_in_rdma_mode
) {
6134 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6143 dprintk("<-- %s status= %d\n", __func__
, status
);
6148 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6149 * and operations we'd like to see to enable certain features in the allow map
6151 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
6152 .how
= SP4_MACH_CRED
,
6153 .enforce
.u
.words
= {
6154 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6155 1 << (OP_EXCHANGE_ID
- 32) |
6156 1 << (OP_CREATE_SESSION
- 32) |
6157 1 << (OP_DESTROY_SESSION
- 32) |
6158 1 << (OP_DESTROY_CLIENTID
- 32)
6161 [0] = 1 << (OP_CLOSE
) |
6164 [1] = 1 << (OP_SECINFO
- 32) |
6165 1 << (OP_SECINFO_NO_NAME
- 32) |
6166 1 << (OP_TEST_STATEID
- 32) |
6167 1 << (OP_FREE_STATEID
- 32) |
6168 1 << (OP_WRITE
- 32)
6173 * Select the state protection mode for client `clp' given the server results
6174 * from exchange_id in `sp'.
6176 * Returns 0 on success, negative errno otherwise.
6178 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
6179 struct nfs41_state_protection
*sp
)
6181 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
6182 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6183 1 << (OP_EXCHANGE_ID
- 32) |
6184 1 << (OP_CREATE_SESSION
- 32) |
6185 1 << (OP_DESTROY_SESSION
- 32) |
6186 1 << (OP_DESTROY_CLIENTID
- 32)
6190 if (sp
->how
== SP4_MACH_CRED
) {
6191 /* Print state protect result */
6192 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
6193 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
6194 if (test_bit(i
, sp
->enforce
.u
.longs
))
6195 dfprintk(MOUNT
, " enforce op %d\n", i
);
6196 if (test_bit(i
, sp
->allow
.u
.longs
))
6197 dfprintk(MOUNT
, " allow op %d\n", i
);
6200 /* make sure nothing is on enforce list that isn't supported */
6201 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
6202 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
6203 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6209 * Minimal mode - state operations are allowed to use machine
6210 * credential. Note this already happens by default, so the
6211 * client doesn't have to do anything more than the negotiation.
6213 * NOTE: we don't care if EXCHANGE_ID is in the list -
6214 * we're already using the machine cred for exchange_id
6215 * and will never use a different cred.
6217 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
6218 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
6219 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
6220 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
6221 dfprintk(MOUNT
, "sp4_mach_cred:\n");
6222 dfprintk(MOUNT
, " minimal mode enabled\n");
6223 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
6225 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6229 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
6230 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
6231 dfprintk(MOUNT
, " cleanup mode enabled\n");
6232 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
6235 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
6236 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
6237 dfprintk(MOUNT
, " secinfo mode enabled\n");
6238 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
6241 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
6242 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
6243 dfprintk(MOUNT
, " stateid mode enabled\n");
6244 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
6247 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
6248 dfprintk(MOUNT
, " write mode enabled\n");
6249 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
6252 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
6253 dfprintk(MOUNT
, " commit mode enabled\n");
6254 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
6262 * _nfs4_proc_exchange_id()
6264 * Wrapper for EXCHANGE_ID operation.
6266 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
6269 nfs4_verifier verifier
;
6270 struct nfs41_exchange_id_args args
= {
6271 .verifier
= &verifier
,
6273 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
6274 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
6276 struct nfs41_exchange_id_res res
= {
6280 struct rpc_message msg
= {
6281 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
6287 nfs4_init_boot_verifier(clp
, &verifier
);
6288 args
.id_len
= nfs4_init_uniform_client_string(clp
, args
.id
,
6290 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6291 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
6292 args
.id_len
, args
.id
);
6294 res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
6296 if (unlikely(res
.server_owner
== NULL
)) {
6301 res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
6303 if (unlikely(res
.server_scope
== NULL
)) {
6305 goto out_server_owner
;
6308 res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
6309 if (unlikely(res
.impl_id
== NULL
)) {
6311 goto out_server_scope
;
6316 args
.state_protect
.how
= SP4_NONE
;
6320 args
.state_protect
= nfs4_sp4_mach_cred_request
;
6327 goto out_server_scope
;
6330 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6331 trace_nfs4_exchange_id(clp
, status
);
6333 status
= nfs4_check_cl_exchange_flags(res
.flags
);
6336 status
= nfs4_sp4_select_mode(clp
, &res
.state_protect
);
6339 clp
->cl_clientid
= res
.clientid
;
6340 clp
->cl_exchange_flags
= (res
.flags
& ~EXCHGID4_FLAG_CONFIRMED_R
);
6341 if (!(res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
))
6342 clp
->cl_seqid
= res
.seqid
;
6344 kfree(clp
->cl_serverowner
);
6345 clp
->cl_serverowner
= res
.server_owner
;
6346 res
.server_owner
= NULL
;
6348 /* use the most recent implementation id */
6349 kfree(clp
->cl_implid
);
6350 clp
->cl_implid
= res
.impl_id
;
6352 if (clp
->cl_serverscope
!= NULL
&&
6353 !nfs41_same_server_scope(clp
->cl_serverscope
,
6354 res
.server_scope
)) {
6355 dprintk("%s: server_scope mismatch detected\n",
6357 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
6358 kfree(clp
->cl_serverscope
);
6359 clp
->cl_serverscope
= NULL
;
6362 if (clp
->cl_serverscope
== NULL
) {
6363 clp
->cl_serverscope
= res
.server_scope
;
6370 kfree(res
.server_owner
);
6372 kfree(res
.server_scope
);
6374 if (clp
->cl_implid
!= NULL
)
6375 dprintk("NFS reply exchange_id: Server Implementation ID: "
6376 "domain: %s, name: %s, date: %llu,%u\n",
6377 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
6378 clp
->cl_implid
->date
.seconds
,
6379 clp
->cl_implid
->date
.nseconds
);
6380 dprintk("NFS reply exchange_id: %d\n", status
);
6385 * nfs4_proc_exchange_id()
6387 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6389 * Since the clientid has expired, all compounds using sessions
6390 * associated with the stale clientid will be returning
6391 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6392 * be in some phase of session reset.
6394 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6396 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6398 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
6401 /* try SP4_MACH_CRED if krb5i/p */
6402 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
6403 authflavor
== RPC_AUTH_GSS_KRB5P
) {
6404 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
);
6410 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
);
6413 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6414 struct rpc_cred
*cred
)
6416 struct rpc_message msg
= {
6417 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
6423 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6424 trace_nfs4_destroy_clientid(clp
, status
);
6426 dprintk("NFS: Got error %d from the server %s on "
6427 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
6431 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6432 struct rpc_cred
*cred
)
6437 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
6438 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
6440 case -NFS4ERR_DELAY
:
6441 case -NFS4ERR_CLIENTID_BUSY
:
6451 int nfs4_destroy_clientid(struct nfs_client
*clp
)
6453 struct rpc_cred
*cred
;
6456 if (clp
->cl_mvops
->minor_version
< 1)
6458 if (clp
->cl_exchange_flags
== 0)
6460 if (clp
->cl_preserve_clid
)
6462 cred
= nfs4_get_clid_cred(clp
);
6463 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
6468 case -NFS4ERR_STALE_CLIENTID
:
6469 clp
->cl_exchange_flags
= 0;
6475 struct nfs4_get_lease_time_data
{
6476 struct nfs4_get_lease_time_args
*args
;
6477 struct nfs4_get_lease_time_res
*res
;
6478 struct nfs_client
*clp
;
6481 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
6484 struct nfs4_get_lease_time_data
*data
=
6485 (struct nfs4_get_lease_time_data
*)calldata
;
6487 dprintk("--> %s\n", __func__
);
6488 /* just setup sequence, do not trigger session recovery
6489 since we're invoked within one */
6490 nfs41_setup_sequence(data
->clp
->cl_session
,
6491 &data
->args
->la_seq_args
,
6492 &data
->res
->lr_seq_res
,
6494 dprintk("<-- %s\n", __func__
);
6498 * Called from nfs4_state_manager thread for session setup, so don't recover
6499 * from sequence operation or clientid errors.
6501 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
6503 struct nfs4_get_lease_time_data
*data
=
6504 (struct nfs4_get_lease_time_data
*)calldata
;
6506 dprintk("--> %s\n", __func__
);
6507 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
6509 switch (task
->tk_status
) {
6510 case -NFS4ERR_DELAY
:
6511 case -NFS4ERR_GRACE
:
6512 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
6513 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
6514 task
->tk_status
= 0;
6516 case -NFS4ERR_RETRY_UNCACHED_REP
:
6517 rpc_restart_call_prepare(task
);
6520 dprintk("<-- %s\n", __func__
);
6523 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
6524 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
6525 .rpc_call_done
= nfs4_get_lease_time_done
,
6528 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
6530 struct rpc_task
*task
;
6531 struct nfs4_get_lease_time_args args
;
6532 struct nfs4_get_lease_time_res res
= {
6533 .lr_fsinfo
= fsinfo
,
6535 struct nfs4_get_lease_time_data data
= {
6540 struct rpc_message msg
= {
6541 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
6545 struct rpc_task_setup task_setup
= {
6546 .rpc_client
= clp
->cl_rpcclient
,
6547 .rpc_message
= &msg
,
6548 .callback_ops
= &nfs4_get_lease_time_ops
,
6549 .callback_data
= &data
,
6550 .flags
= RPC_TASK_TIMEOUT
,
6554 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
6555 nfs4_set_sequence_privileged(&args
.la_seq_args
);
6556 dprintk("--> %s\n", __func__
);
6557 task
= rpc_run_task(&task_setup
);
6560 status
= PTR_ERR(task
);
6562 status
= task
->tk_status
;
6565 dprintk("<-- %s return %d\n", __func__
, status
);
6571 * Initialize the values to be used by the client in CREATE_SESSION
6572 * If nfs4_init_session set the fore channel request and response sizes,
6575 * Set the back channel max_resp_sz_cached to zero to force the client to
6576 * always set csa_cachethis to FALSE because the current implementation
6577 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6579 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
6581 unsigned int max_rqst_sz
, max_resp_sz
;
6583 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
6584 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
6586 /* Fore channel attributes */
6587 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
6588 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
6589 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
6590 args
->fc_attrs
.max_reqs
= max_session_slots
;
6592 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6593 "max_ops=%u max_reqs=%u\n",
6595 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
6596 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
6598 /* Back channel attributes */
6599 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
6600 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
6601 args
->bc_attrs
.max_resp_sz_cached
= 0;
6602 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
6603 args
->bc_attrs
.max_reqs
= 1;
6605 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6606 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6608 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
6609 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
6610 args
->bc_attrs
.max_reqs
);
6613 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6615 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
6616 struct nfs4_channel_attrs
*rcvd
= &session
->fc_attrs
;
6618 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
6621 * Our requested max_ops is the minimum we need; we're not
6622 * prepared to break up compounds into smaller pieces than that.
6623 * So, no point even trying to continue if the server won't
6626 if (rcvd
->max_ops
< sent
->max_ops
)
6628 if (rcvd
->max_reqs
== 0)
6630 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
6631 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
6635 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6637 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
6638 struct nfs4_channel_attrs
*rcvd
= &session
->bc_attrs
;
6640 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
6642 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
6644 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
6646 /* These would render the backchannel useless: */
6647 if (rcvd
->max_ops
!= sent
->max_ops
)
6649 if (rcvd
->max_reqs
!= sent
->max_reqs
)
6654 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
6655 struct nfs4_session
*session
)
6659 ret
= nfs4_verify_fore_channel_attrs(args
, session
);
6662 return nfs4_verify_back_channel_attrs(args
, session
);
6665 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
6666 struct rpc_cred
*cred
)
6668 struct nfs4_session
*session
= clp
->cl_session
;
6669 struct nfs41_create_session_args args
= {
6671 .cb_program
= NFS4_CALLBACK
,
6673 struct nfs41_create_session_res res
= {
6676 struct rpc_message msg
= {
6677 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
6684 nfs4_init_channel_attrs(&args
);
6685 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
6687 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6688 trace_nfs4_create_session(clp
, status
);
6691 /* Verify the session's negotiated channel_attrs values */
6692 status
= nfs4_verify_channel_attrs(&args
, session
);
6693 /* Increment the clientid slot sequence id */
6701 * Issues a CREATE_SESSION operation to the server.
6702 * It is the responsibility of the caller to verify the session is
6703 * expired before calling this routine.
6705 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6709 struct nfs4_session
*session
= clp
->cl_session
;
6711 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
6713 status
= _nfs4_proc_create_session(clp
, cred
);
6717 /* Init or reset the session slot tables */
6718 status
= nfs4_setup_session_slot_tables(session
);
6719 dprintk("slot table setup returned %d\n", status
);
6723 ptr
= (unsigned *)&session
->sess_id
.data
[0];
6724 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
6725 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
6727 dprintk("<-- %s\n", __func__
);
6732 * Issue the over-the-wire RPC DESTROY_SESSION.
6733 * The caller must serialize access to this routine.
6735 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
6736 struct rpc_cred
*cred
)
6738 struct rpc_message msg
= {
6739 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
6740 .rpc_argp
= session
,
6745 dprintk("--> nfs4_proc_destroy_session\n");
6747 /* session is still being setup */
6748 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
6751 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6752 trace_nfs4_destroy_session(session
->clp
, status
);
6755 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6756 "Session has been destroyed regardless...\n", status
);
6758 dprintk("<-- nfs4_proc_destroy_session\n");
6763 * Renew the cl_session lease.
6765 struct nfs4_sequence_data
{
6766 struct nfs_client
*clp
;
6767 struct nfs4_sequence_args args
;
6768 struct nfs4_sequence_res res
;
6771 static void nfs41_sequence_release(void *data
)
6773 struct nfs4_sequence_data
*calldata
= data
;
6774 struct nfs_client
*clp
= calldata
->clp
;
6776 if (atomic_read(&clp
->cl_count
) > 1)
6777 nfs4_schedule_state_renewal(clp
);
6778 nfs_put_client(clp
);
6782 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6784 switch(task
->tk_status
) {
6785 case -NFS4ERR_DELAY
:
6786 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6789 nfs4_schedule_lease_recovery(clp
);
6794 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
6796 struct nfs4_sequence_data
*calldata
= data
;
6797 struct nfs_client
*clp
= calldata
->clp
;
6799 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
6802 trace_nfs4_sequence(clp
, task
->tk_status
);
6803 if (task
->tk_status
< 0) {
6804 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
6805 if (atomic_read(&clp
->cl_count
) == 1)
6808 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
6809 rpc_restart_call_prepare(task
);
6813 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
6815 dprintk("<-- %s\n", __func__
);
6818 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
6820 struct nfs4_sequence_data
*calldata
= data
;
6821 struct nfs_client
*clp
= calldata
->clp
;
6822 struct nfs4_sequence_args
*args
;
6823 struct nfs4_sequence_res
*res
;
6825 args
= task
->tk_msg
.rpc_argp
;
6826 res
= task
->tk_msg
.rpc_resp
;
6828 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
6831 static const struct rpc_call_ops nfs41_sequence_ops
= {
6832 .rpc_call_done
= nfs41_sequence_call_done
,
6833 .rpc_call_prepare
= nfs41_sequence_prepare
,
6834 .rpc_release
= nfs41_sequence_release
,
6837 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
6838 struct rpc_cred
*cred
,
6841 struct nfs4_sequence_data
*calldata
;
6842 struct rpc_message msg
= {
6843 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
6846 struct rpc_task_setup task_setup_data
= {
6847 .rpc_client
= clp
->cl_rpcclient
,
6848 .rpc_message
= &msg
,
6849 .callback_ops
= &nfs41_sequence_ops
,
6850 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
6853 if (!atomic_inc_not_zero(&clp
->cl_count
))
6854 return ERR_PTR(-EIO
);
6855 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6856 if (calldata
== NULL
) {
6857 nfs_put_client(clp
);
6858 return ERR_PTR(-ENOMEM
);
6860 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
6862 nfs4_set_sequence_privileged(&calldata
->args
);
6863 msg
.rpc_argp
= &calldata
->args
;
6864 msg
.rpc_resp
= &calldata
->res
;
6865 calldata
->clp
= clp
;
6866 task_setup_data
.callback_data
= calldata
;
6868 return rpc_run_task(&task_setup_data
);
6871 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
6873 struct rpc_task
*task
;
6876 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
6878 task
= _nfs41_proc_sequence(clp
, cred
, false);
6880 ret
= PTR_ERR(task
);
6882 rpc_put_task_async(task
);
6883 dprintk("<-- %s status=%d\n", __func__
, ret
);
6887 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6889 struct rpc_task
*task
;
6892 task
= _nfs41_proc_sequence(clp
, cred
, true);
6894 ret
= PTR_ERR(task
);
6897 ret
= rpc_wait_for_completion_task(task
);
6899 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
6901 if (task
->tk_status
== 0)
6902 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
6903 ret
= task
->tk_status
;
6907 dprintk("<-- %s status=%d\n", __func__
, ret
);
6911 struct nfs4_reclaim_complete_data
{
6912 struct nfs_client
*clp
;
6913 struct nfs41_reclaim_complete_args arg
;
6914 struct nfs41_reclaim_complete_res res
;
6917 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
6919 struct nfs4_reclaim_complete_data
*calldata
= data
;
6921 nfs41_setup_sequence(calldata
->clp
->cl_session
,
6922 &calldata
->arg
.seq_args
,
6923 &calldata
->res
.seq_res
,
6927 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6929 switch(task
->tk_status
) {
6931 case -NFS4ERR_COMPLETE_ALREADY
:
6932 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
6934 case -NFS4ERR_DELAY
:
6935 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6937 case -NFS4ERR_RETRY_UNCACHED_REP
:
6940 nfs4_schedule_lease_recovery(clp
);
6945 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
6947 struct nfs4_reclaim_complete_data
*calldata
= data
;
6948 struct nfs_client
*clp
= calldata
->clp
;
6949 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
6951 dprintk("--> %s\n", __func__
);
6952 if (!nfs41_sequence_done(task
, res
))
6955 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
6956 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
6957 rpc_restart_call_prepare(task
);
6960 dprintk("<-- %s\n", __func__
);
6963 static void nfs4_free_reclaim_complete_data(void *data
)
6965 struct nfs4_reclaim_complete_data
*calldata
= data
;
6970 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
6971 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
6972 .rpc_call_done
= nfs4_reclaim_complete_done
,
6973 .rpc_release
= nfs4_free_reclaim_complete_data
,
6977 * Issue a global reclaim complete.
6979 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
6980 struct rpc_cred
*cred
)
6982 struct nfs4_reclaim_complete_data
*calldata
;
6983 struct rpc_task
*task
;
6984 struct rpc_message msg
= {
6985 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
6988 struct rpc_task_setup task_setup_data
= {
6989 .rpc_client
= clp
->cl_rpcclient
,
6990 .rpc_message
= &msg
,
6991 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
6992 .flags
= RPC_TASK_ASYNC
,
6994 int status
= -ENOMEM
;
6996 dprintk("--> %s\n", __func__
);
6997 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6998 if (calldata
== NULL
)
7000 calldata
->clp
= clp
;
7001 calldata
->arg
.one_fs
= 0;
7003 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
7004 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
7005 msg
.rpc_argp
= &calldata
->arg
;
7006 msg
.rpc_resp
= &calldata
->res
;
7007 task_setup_data
.callback_data
= calldata
;
7008 task
= rpc_run_task(&task_setup_data
);
7010 status
= PTR_ERR(task
);
7013 status
= nfs4_wait_for_completion_rpc_task(task
);
7015 status
= task
->tk_status
;
7019 dprintk("<-- %s status=%d\n", __func__
, status
);
7024 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
7026 struct nfs4_layoutget
*lgp
= calldata
;
7027 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
7028 struct nfs4_session
*session
= nfs4_get_session(server
);
7030 dprintk("--> %s\n", __func__
);
7031 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7032 * right now covering the LAYOUTGET we are about to send.
7033 * However, that is not so catastrophic, and there seems
7034 * to be no way to prevent it completely.
7036 if (nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
7037 &lgp
->res
.seq_res
, task
))
7039 if (pnfs_choose_layoutget_stateid(&lgp
->args
.stateid
,
7040 NFS_I(lgp
->args
.inode
)->layout
,
7041 lgp
->args
.ctx
->state
)) {
7042 rpc_exit(task
, NFS4_OK
);
7046 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
7048 struct nfs4_layoutget
*lgp
= calldata
;
7049 struct inode
*inode
= lgp
->args
.inode
;
7050 struct nfs_server
*server
= NFS_SERVER(inode
);
7051 struct pnfs_layout_hdr
*lo
;
7052 struct nfs4_state
*state
= NULL
;
7053 unsigned long timeo
, giveup
;
7055 dprintk("--> %s\n", __func__
);
7057 if (!nfs41_sequence_done(task
, &lgp
->res
.seq_res
))
7060 switch (task
->tk_status
) {
7063 case -NFS4ERR_LAYOUTTRYLATER
:
7064 case -NFS4ERR_RECALLCONFLICT
:
7065 timeo
= rpc_get_timeout(task
->tk_client
);
7066 giveup
= lgp
->args
.timestamp
+ timeo
;
7067 if (time_after(giveup
, jiffies
))
7068 task
->tk_status
= -NFS4ERR_DELAY
;
7070 case -NFS4ERR_EXPIRED
:
7071 case -NFS4ERR_BAD_STATEID
:
7072 spin_lock(&inode
->i_lock
);
7073 lo
= NFS_I(inode
)->layout
;
7074 if (!lo
|| list_empty(&lo
->plh_segs
)) {
7075 spin_unlock(&inode
->i_lock
);
7076 /* If the open stateid was bad, then recover it. */
7077 state
= lgp
->args
.ctx
->state
;
7081 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
);
7082 spin_unlock(&inode
->i_lock
);
7083 /* Mark the bad layout state as invalid, then
7084 * retry using the open stateid. */
7085 pnfs_free_lseg_list(&head
);
7088 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
7089 rpc_restart_call_prepare(task
);
7091 dprintk("<-- %s\n", __func__
);
7094 static size_t max_response_pages(struct nfs_server
*server
)
7096 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
7097 return nfs_page_array_len(0, max_resp_sz
);
7100 static void nfs4_free_pages(struct page
**pages
, size_t size
)
7107 for (i
= 0; i
< size
; i
++) {
7110 __free_page(pages
[i
]);
7115 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
7117 struct page
**pages
;
7120 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
7122 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
7126 for (i
= 0; i
< size
; i
++) {
7127 pages
[i
] = alloc_page(gfp_flags
);
7129 dprintk("%s: failed to allocate page\n", __func__
);
7130 nfs4_free_pages(pages
, size
);
7138 static void nfs4_layoutget_release(void *calldata
)
7140 struct nfs4_layoutget
*lgp
= calldata
;
7141 struct inode
*inode
= lgp
->args
.inode
;
7142 struct nfs_server
*server
= NFS_SERVER(inode
);
7143 size_t max_pages
= max_response_pages(server
);
7145 dprintk("--> %s\n", __func__
);
7146 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
7147 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
7148 put_nfs_open_context(lgp
->args
.ctx
);
7150 dprintk("<-- %s\n", __func__
);
7153 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
7154 .rpc_call_prepare
= nfs4_layoutget_prepare
,
7155 .rpc_call_done
= nfs4_layoutget_done
,
7156 .rpc_release
= nfs4_layoutget_release
,
7159 struct pnfs_layout_segment
*
7160 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, gfp_t gfp_flags
)
7162 struct inode
*inode
= lgp
->args
.inode
;
7163 struct nfs_server
*server
= NFS_SERVER(inode
);
7164 size_t max_pages
= max_response_pages(server
);
7165 struct rpc_task
*task
;
7166 struct rpc_message msg
= {
7167 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
7168 .rpc_argp
= &lgp
->args
,
7169 .rpc_resp
= &lgp
->res
,
7170 .rpc_cred
= lgp
->cred
,
7172 struct rpc_task_setup task_setup_data
= {
7173 .rpc_client
= server
->client
,
7174 .rpc_message
= &msg
,
7175 .callback_ops
= &nfs4_layoutget_call_ops
,
7176 .callback_data
= lgp
,
7177 .flags
= RPC_TASK_ASYNC
,
7179 struct pnfs_layout_segment
*lseg
= NULL
;
7182 dprintk("--> %s\n", __func__
);
7184 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
7185 if (!lgp
->args
.layout
.pages
) {
7186 nfs4_layoutget_release(lgp
);
7187 return ERR_PTR(-ENOMEM
);
7189 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
7190 lgp
->args
.timestamp
= jiffies
;
7192 lgp
->res
.layoutp
= &lgp
->args
.layout
;
7193 lgp
->res
.seq_res
.sr_slot
= NULL
;
7194 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
7196 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7197 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
7199 task
= rpc_run_task(&task_setup_data
);
7201 return ERR_CAST(task
);
7202 status
= nfs4_wait_for_completion_rpc_task(task
);
7204 status
= task
->tk_status
;
7205 trace_nfs4_layoutget(lgp
->args
.ctx
,
7209 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7210 if (status
== 0 && lgp
->res
.layoutp
->len
)
7211 lseg
= pnfs_layout_process(lgp
);
7213 dprintk("<-- %s status=%d\n", __func__
, status
);
7215 return ERR_PTR(status
);
7220 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
7222 struct nfs4_layoutreturn
*lrp
= calldata
;
7224 dprintk("--> %s\n", __func__
);
7225 nfs41_setup_sequence(lrp
->clp
->cl_session
,
7226 &lrp
->args
.seq_args
,
7231 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
7233 struct nfs4_layoutreturn
*lrp
= calldata
;
7234 struct nfs_server
*server
;
7236 dprintk("--> %s\n", __func__
);
7238 if (!nfs41_sequence_done(task
, &lrp
->res
.seq_res
))
7241 server
= NFS_SERVER(lrp
->args
.inode
);
7242 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
7243 rpc_restart_call_prepare(task
);
7246 dprintk("<-- %s\n", __func__
);
7249 static void nfs4_layoutreturn_release(void *calldata
)
7251 struct nfs4_layoutreturn
*lrp
= calldata
;
7252 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
7254 dprintk("--> %s\n", __func__
);
7255 spin_lock(&lo
->plh_inode
->i_lock
);
7256 if (lrp
->res
.lrs_present
)
7257 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
7258 lo
->plh_block_lgets
--;
7259 spin_unlock(&lo
->plh_inode
->i_lock
);
7260 pnfs_put_layout_hdr(lrp
->args
.layout
);
7262 dprintk("<-- %s\n", __func__
);
7265 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
7266 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
7267 .rpc_call_done
= nfs4_layoutreturn_done
,
7268 .rpc_release
= nfs4_layoutreturn_release
,
7271 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
)
7273 struct rpc_task
*task
;
7274 struct rpc_message msg
= {
7275 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
7276 .rpc_argp
= &lrp
->args
,
7277 .rpc_resp
= &lrp
->res
,
7278 .rpc_cred
= lrp
->cred
,
7280 struct rpc_task_setup task_setup_data
= {
7281 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
7282 .rpc_message
= &msg
,
7283 .callback_ops
= &nfs4_layoutreturn_call_ops
,
7284 .callback_data
= lrp
,
7288 dprintk("--> %s\n", __func__
);
7289 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
7290 task
= rpc_run_task(&task_setup_data
);
7292 return PTR_ERR(task
);
7293 status
= task
->tk_status
;
7294 trace_nfs4_layoutreturn(lrp
->args
.inode
, status
);
7295 dprintk("<-- %s status=%d\n", __func__
, status
);
7301 * Retrieve the list of Data Server devices from the MDS.
7303 static int _nfs4_getdevicelist(struct nfs_server
*server
,
7304 const struct nfs_fh
*fh
,
7305 struct pnfs_devicelist
*devlist
)
7307 struct nfs4_getdevicelist_args args
= {
7309 .layoutclass
= server
->pnfs_curr_ld
->id
,
7311 struct nfs4_getdevicelist_res res
= {
7314 struct rpc_message msg
= {
7315 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICELIST
],
7321 dprintk("--> %s\n", __func__
);
7322 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
,
7324 dprintk("<-- %s status=%d\n", __func__
, status
);
7328 int nfs4_proc_getdevicelist(struct nfs_server
*server
,
7329 const struct nfs_fh
*fh
,
7330 struct pnfs_devicelist
*devlist
)
7332 struct nfs4_exception exception
= { };
7336 err
= nfs4_handle_exception(server
,
7337 _nfs4_getdevicelist(server
, fh
, devlist
),
7339 } while (exception
.retry
);
7341 dprintk("%s: err=%d, num_devs=%u\n", __func__
,
7342 err
, devlist
->num_devs
);
7346 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist
);
7349 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
7350 struct pnfs_device
*pdev
,
7351 struct rpc_cred
*cred
)
7353 struct nfs4_getdeviceinfo_args args
= {
7356 struct nfs4_getdeviceinfo_res res
= {
7359 struct rpc_message msg
= {
7360 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
7367 dprintk("--> %s\n", __func__
);
7368 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
7369 dprintk("<-- %s status=%d\n", __func__
, status
);
7374 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
7375 struct pnfs_device
*pdev
,
7376 struct rpc_cred
*cred
)
7378 struct nfs4_exception exception
= { };
7382 err
= nfs4_handle_exception(server
,
7383 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
7385 } while (exception
.retry
);
7388 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
7390 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
7392 struct nfs4_layoutcommit_data
*data
= calldata
;
7393 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
7394 struct nfs4_session
*session
= nfs4_get_session(server
);
7396 nfs41_setup_sequence(session
,
7397 &data
->args
.seq_args
,
7403 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
7405 struct nfs4_layoutcommit_data
*data
= calldata
;
7406 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
7408 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
7411 switch (task
->tk_status
) { /* Just ignore these failures */
7412 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
7413 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
7414 case -NFS4ERR_BADLAYOUT
: /* no layout */
7415 case -NFS4ERR_GRACE
: /* loca_recalim always false */
7416 task
->tk_status
= 0;
7419 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
7423 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
7424 rpc_restart_call_prepare(task
);
7430 static void nfs4_layoutcommit_release(void *calldata
)
7432 struct nfs4_layoutcommit_data
*data
= calldata
;
7434 pnfs_cleanup_layoutcommit(data
);
7435 put_rpccred(data
->cred
);
7439 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
7440 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
7441 .rpc_call_done
= nfs4_layoutcommit_done
,
7442 .rpc_release
= nfs4_layoutcommit_release
,
7446 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
7448 struct rpc_message msg
= {
7449 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
7450 .rpc_argp
= &data
->args
,
7451 .rpc_resp
= &data
->res
,
7452 .rpc_cred
= data
->cred
,
7454 struct rpc_task_setup task_setup_data
= {
7455 .task
= &data
->task
,
7456 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
7457 .rpc_message
= &msg
,
7458 .callback_ops
= &nfs4_layoutcommit_ops
,
7459 .callback_data
= data
,
7460 .flags
= RPC_TASK_ASYNC
,
7462 struct rpc_task
*task
;
7465 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7466 "lbw: %llu inode %lu\n",
7467 data
->task
.tk_pid
, sync
,
7468 data
->args
.lastbytewritten
,
7469 data
->args
.inode
->i_ino
);
7471 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
7472 task
= rpc_run_task(&task_setup_data
);
7474 return PTR_ERR(task
);
7477 status
= nfs4_wait_for_completion_rpc_task(task
);
7480 status
= task
->tk_status
;
7481 trace_nfs4_layoutcommit(data
->args
.inode
, status
);
7483 dprintk("%s: status %d\n", __func__
, status
);
7489 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7490 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7493 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7494 struct nfs_fsinfo
*info
,
7495 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7497 struct nfs41_secinfo_no_name_args args
= {
7498 .style
= SECINFO_STYLE_CURRENT_FH
,
7500 struct nfs4_secinfo_res res
= {
7503 struct rpc_message msg
= {
7504 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
7508 struct rpc_clnt
*clnt
= server
->client
;
7509 struct rpc_cred
*cred
= NULL
;
7512 if (use_integrity
) {
7513 clnt
= server
->nfs_client
->cl_rpcclient
;
7514 cred
= nfs4_get_clid_cred(server
->nfs_client
);
7515 msg
.rpc_cred
= cred
;
7518 dprintk("--> %s\n", __func__
);
7519 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
7521 dprintk("<-- %s status=%d\n", __func__
, status
);
7530 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7531 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
7533 struct nfs4_exception exception
= { };
7536 /* first try using integrity protection */
7537 err
= -NFS4ERR_WRONGSEC
;
7539 /* try to use integrity protection with machine cred */
7540 if (_nfs4_is_integrity_protected(server
->nfs_client
))
7541 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
7545 * if unable to use integrity protection, or SECINFO with
7546 * integrity protection returns NFS4ERR_WRONGSEC (which is
7547 * disallowed by spec, but exists in deployed servers) use
7548 * the current filesystem's rpc_client and the user cred.
7550 if (err
== -NFS4ERR_WRONGSEC
)
7551 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
7556 case -NFS4ERR_WRONGSEC
:
7557 case -NFS4ERR_NOTSUPP
:
7560 err
= nfs4_handle_exception(server
, err
, &exception
);
7562 } while (exception
.retry
);
7568 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7569 struct nfs_fsinfo
*info
)
7573 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
7574 struct nfs4_secinfo_flavors
*flavors
;
7575 struct nfs4_secinfo4
*secinfo
;
7578 page
= alloc_page(GFP_KERNEL
);
7584 flavors
= page_address(page
);
7585 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
7588 * Fall back on "guess and check" method if
7589 * the server doesn't support SECINFO_NO_NAME
7591 if (err
== -NFS4ERR_WRONGSEC
|| err
== -NFS4ERR_NOTSUPP
) {
7592 err
= nfs4_find_root_sec(server
, fhandle
, info
);
7598 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
7599 secinfo
= &flavors
->flavors
[i
];
7601 switch (secinfo
->flavor
) {
7605 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
7606 &secinfo
->flavor_info
);
7609 flavor
= RPC_AUTH_MAXFLAVOR
;
7613 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
7614 err
= nfs4_lookup_root_sec(server
, fhandle
,
7621 if (flavor
== RPC_AUTH_MAXFLAVOR
)
7632 static int _nfs41_test_stateid(struct nfs_server
*server
,
7633 nfs4_stateid
*stateid
,
7634 struct rpc_cred
*cred
)
7637 struct nfs41_test_stateid_args args
= {
7640 struct nfs41_test_stateid_res res
;
7641 struct rpc_message msg
= {
7642 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
7647 struct rpc_clnt
*rpc_client
= server
->client
;
7649 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
7652 dprintk("NFS call test_stateid %p\n", stateid
);
7653 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7654 nfs4_set_sequence_privileged(&args
.seq_args
);
7655 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
7656 &args
.seq_args
, &res
.seq_res
);
7657 if (status
!= NFS_OK
) {
7658 dprintk("NFS reply test_stateid: failed, %d\n", status
);
7661 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
7666 * nfs41_test_stateid - perform a TEST_STATEID operation
7668 * @server: server / transport on which to perform the operation
7669 * @stateid: state ID to test
7672 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7673 * Otherwise a negative NFS4ERR value is returned if the operation
7674 * failed or the state ID is not currently valid.
7676 static int nfs41_test_stateid(struct nfs_server
*server
,
7677 nfs4_stateid
*stateid
,
7678 struct rpc_cred
*cred
)
7680 struct nfs4_exception exception
= { };
7683 err
= _nfs41_test_stateid(server
, stateid
, cred
);
7684 if (err
!= -NFS4ERR_DELAY
)
7686 nfs4_handle_exception(server
, err
, &exception
);
7687 } while (exception
.retry
);
7691 struct nfs_free_stateid_data
{
7692 struct nfs_server
*server
;
7693 struct nfs41_free_stateid_args args
;
7694 struct nfs41_free_stateid_res res
;
7697 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
7699 struct nfs_free_stateid_data
*data
= calldata
;
7700 nfs41_setup_sequence(nfs4_get_session(data
->server
),
7701 &data
->args
.seq_args
,
7706 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
7708 struct nfs_free_stateid_data
*data
= calldata
;
7710 nfs41_sequence_done(task
, &data
->res
.seq_res
);
7712 switch (task
->tk_status
) {
7713 case -NFS4ERR_DELAY
:
7714 if (nfs4_async_handle_error(task
, data
->server
, NULL
) == -EAGAIN
)
7715 rpc_restart_call_prepare(task
);
7719 static void nfs41_free_stateid_release(void *calldata
)
7724 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
7725 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
7726 .rpc_call_done
= nfs41_free_stateid_done
,
7727 .rpc_release
= nfs41_free_stateid_release
,
7730 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
7731 nfs4_stateid
*stateid
,
7732 struct rpc_cred
*cred
,
7735 struct rpc_message msg
= {
7736 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
7739 struct rpc_task_setup task_setup
= {
7740 .rpc_client
= server
->client
,
7741 .rpc_message
= &msg
,
7742 .callback_ops
= &nfs41_free_stateid_ops
,
7743 .flags
= RPC_TASK_ASYNC
,
7745 struct nfs_free_stateid_data
*data
;
7747 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
7748 &task_setup
.rpc_client
, &msg
);
7750 dprintk("NFS call free_stateid %p\n", stateid
);
7751 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
7753 return ERR_PTR(-ENOMEM
);
7754 data
->server
= server
;
7755 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
7757 task_setup
.callback_data
= data
;
7759 msg
.rpc_argp
= &data
->args
;
7760 msg
.rpc_resp
= &data
->res
;
7761 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
7763 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
7765 return rpc_run_task(&task_setup
);
7769 * nfs41_free_stateid - perform a FREE_STATEID operation
7771 * @server: server / transport on which to perform the operation
7772 * @stateid: state ID to release
7775 * Returns NFS_OK if the server freed "stateid". Otherwise a
7776 * negative NFS4ERR value is returned.
7778 static int nfs41_free_stateid(struct nfs_server
*server
,
7779 nfs4_stateid
*stateid
,
7780 struct rpc_cred
*cred
)
7782 struct rpc_task
*task
;
7785 task
= _nfs41_free_stateid(server
, stateid
, cred
, true);
7787 return PTR_ERR(task
);
7788 ret
= rpc_wait_for_completion_task(task
);
7790 ret
= task
->tk_status
;
7795 static int nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
7797 struct rpc_task
*task
;
7798 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
7800 task
= _nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
7801 nfs4_free_lock_state(server
, lsp
);
7803 return PTR_ERR(task
);
7808 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
7809 const nfs4_stateid
*s2
)
7811 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
7814 if (s1
->seqid
== s2
->seqid
)
7816 if (s1
->seqid
== 0 || s2
->seqid
== 0)
7822 #endif /* CONFIG_NFS_V4_1 */
7824 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
7825 const nfs4_stateid
*s2
)
7827 return nfs4_stateid_match(s1
, s2
);
7831 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
7832 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7833 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7834 .recover_open
= nfs4_open_reclaim
,
7835 .recover_lock
= nfs4_lock_reclaim
,
7836 .establish_clid
= nfs4_init_clientid
,
7837 .detect_trunking
= nfs40_discover_server_trunking
,
7840 #if defined(CONFIG_NFS_V4_1)
7841 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
7842 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7843 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7844 .recover_open
= nfs4_open_reclaim
,
7845 .recover_lock
= nfs4_lock_reclaim
,
7846 .establish_clid
= nfs41_init_clientid
,
7847 .reclaim_complete
= nfs41_proc_reclaim_complete
,
7848 .detect_trunking
= nfs41_discover_server_trunking
,
7850 #endif /* CONFIG_NFS_V4_1 */
7852 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
7853 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7854 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7855 .recover_open
= nfs4_open_expired
,
7856 .recover_lock
= nfs4_lock_expired
,
7857 .establish_clid
= nfs4_init_clientid
,
7860 #if defined(CONFIG_NFS_V4_1)
7861 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
7862 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7863 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7864 .recover_open
= nfs41_open_expired
,
7865 .recover_lock
= nfs41_lock_expired
,
7866 .establish_clid
= nfs41_init_clientid
,
7868 #endif /* CONFIG_NFS_V4_1 */
7870 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
7871 .sched_state_renewal
= nfs4_proc_async_renew
,
7872 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
7873 .renew_lease
= nfs4_proc_renew
,
7876 #if defined(CONFIG_NFS_V4_1)
7877 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
7878 .sched_state_renewal
= nfs41_proc_async_sequence
,
7879 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
7880 .renew_lease
= nfs4_proc_sequence
,
7884 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
7886 .init_caps
= NFS_CAP_READDIRPLUS
7887 | NFS_CAP_ATOMIC_OPEN
7888 | NFS_CAP_CHANGE_ATTR
7889 | NFS_CAP_POSIX_LOCK
,
7890 .init_client
= nfs40_init_client
,
7891 .shutdown_client
= nfs40_shutdown_client
,
7892 .match_stateid
= nfs4_match_stateid
,
7893 .find_root_sec
= nfs4_find_root_sec
,
7894 .free_lock_state
= nfs4_release_lockowner
,
7895 .call_sync_ops
= &nfs40_call_sync_ops
,
7896 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
7897 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
7898 .state_renewal_ops
= &nfs40_state_renewal_ops
,
7901 #if defined(CONFIG_NFS_V4_1)
7902 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
7904 .init_caps
= NFS_CAP_READDIRPLUS
7905 | NFS_CAP_ATOMIC_OPEN
7906 | NFS_CAP_CHANGE_ATTR
7907 | NFS_CAP_POSIX_LOCK
7908 | NFS_CAP_STATEID_NFSV41
7909 | NFS_CAP_ATOMIC_OPEN_V1
,
7910 .init_client
= nfs41_init_client
,
7911 .shutdown_client
= nfs41_shutdown_client
,
7912 .match_stateid
= nfs41_match_stateid
,
7913 .find_root_sec
= nfs41_find_root_sec
,
7914 .free_lock_state
= nfs41_free_lock_state
,
7915 .call_sync_ops
= &nfs41_call_sync_ops
,
7916 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7917 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7918 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7922 #if defined(CONFIG_NFS_V4_2)
7923 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
7925 .init_caps
= NFS_CAP_READDIRPLUS
7926 | NFS_CAP_ATOMIC_OPEN
7927 | NFS_CAP_CHANGE_ATTR
7928 | NFS_CAP_POSIX_LOCK
7929 | NFS_CAP_STATEID_NFSV41
7930 | NFS_CAP_ATOMIC_OPEN_V1
,
7931 .init_client
= nfs41_init_client
,
7932 .shutdown_client
= nfs41_shutdown_client
,
7933 .match_stateid
= nfs41_match_stateid
,
7934 .find_root_sec
= nfs41_find_root_sec
,
7935 .free_lock_state
= nfs41_free_lock_state
,
7936 .call_sync_ops
= &nfs41_call_sync_ops
,
7937 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7938 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7939 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7943 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
7944 [0] = &nfs_v4_0_minor_ops
,
7945 #if defined(CONFIG_NFS_V4_1)
7946 [1] = &nfs_v4_1_minor_ops
,
7948 #if defined(CONFIG_NFS_V4_2)
7949 [2] = &nfs_v4_2_minor_ops
,
7953 static const struct inode_operations nfs4_dir_inode_operations
= {
7954 .create
= nfs_create
,
7955 .lookup
= nfs_lookup
,
7956 .atomic_open
= nfs_atomic_open
,
7958 .unlink
= nfs_unlink
,
7959 .symlink
= nfs_symlink
,
7963 .rename
= nfs_rename
,
7964 .permission
= nfs_permission
,
7965 .getattr
= nfs_getattr
,
7966 .setattr
= nfs_setattr
,
7967 .getxattr
= generic_getxattr
,
7968 .setxattr
= generic_setxattr
,
7969 .listxattr
= generic_listxattr
,
7970 .removexattr
= generic_removexattr
,
7973 static const struct inode_operations nfs4_file_inode_operations
= {
7974 .permission
= nfs_permission
,
7975 .getattr
= nfs_getattr
,
7976 .setattr
= nfs_setattr
,
7977 .getxattr
= generic_getxattr
,
7978 .setxattr
= generic_setxattr
,
7979 .listxattr
= generic_listxattr
,
7980 .removexattr
= generic_removexattr
,
7983 const struct nfs_rpc_ops nfs_v4_clientops
= {
7984 .version
= 4, /* protocol version */
7985 .dentry_ops
= &nfs4_dentry_operations
,
7986 .dir_inode_ops
= &nfs4_dir_inode_operations
,
7987 .file_inode_ops
= &nfs4_file_inode_operations
,
7988 .file_ops
= &nfs4_file_operations
,
7989 .getroot
= nfs4_proc_get_root
,
7990 .submount
= nfs4_submount
,
7991 .try_mount
= nfs4_try_mount
,
7992 .getattr
= nfs4_proc_getattr
,
7993 .setattr
= nfs4_proc_setattr
,
7994 .lookup
= nfs4_proc_lookup
,
7995 .access
= nfs4_proc_access
,
7996 .readlink
= nfs4_proc_readlink
,
7997 .create
= nfs4_proc_create
,
7998 .remove
= nfs4_proc_remove
,
7999 .unlink_setup
= nfs4_proc_unlink_setup
,
8000 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
8001 .unlink_done
= nfs4_proc_unlink_done
,
8002 .rename
= nfs4_proc_rename
,
8003 .rename_setup
= nfs4_proc_rename_setup
,
8004 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
8005 .rename_done
= nfs4_proc_rename_done
,
8006 .link
= nfs4_proc_link
,
8007 .symlink
= nfs4_proc_symlink
,
8008 .mkdir
= nfs4_proc_mkdir
,
8009 .rmdir
= nfs4_proc_remove
,
8010 .readdir
= nfs4_proc_readdir
,
8011 .mknod
= nfs4_proc_mknod
,
8012 .statfs
= nfs4_proc_statfs
,
8013 .fsinfo
= nfs4_proc_fsinfo
,
8014 .pathconf
= nfs4_proc_pathconf
,
8015 .set_capabilities
= nfs4_server_capabilities
,
8016 .decode_dirent
= nfs4_decode_dirent
,
8017 .read_setup
= nfs4_proc_read_setup
,
8018 .read_pageio_init
= pnfs_pageio_init_read
,
8019 .read_rpc_prepare
= nfs4_proc_read_rpc_prepare
,
8020 .read_done
= nfs4_read_done
,
8021 .write_setup
= nfs4_proc_write_setup
,
8022 .write_pageio_init
= pnfs_pageio_init_write
,
8023 .write_rpc_prepare
= nfs4_proc_write_rpc_prepare
,
8024 .write_done
= nfs4_write_done
,
8025 .commit_setup
= nfs4_proc_commit_setup
,
8026 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
8027 .commit_done
= nfs4_commit_done
,
8028 .lock
= nfs4_proc_lock
,
8029 .clear_acl_cache
= nfs4_zap_acl_attr
,
8030 .close_context
= nfs4_close_context
,
8031 .open_context
= nfs4_atomic_open
,
8032 .have_delegation
= nfs4_have_delegation
,
8033 .return_delegation
= nfs4_inode_return_delegation
,
8034 .alloc_client
= nfs4_alloc_client
,
8035 .init_client
= nfs4_init_client
,
8036 .free_client
= nfs4_free_client
,
8037 .create_server
= nfs4_create_server
,
8038 .clone_server
= nfs_clone_server
,
8041 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
8042 .prefix
= XATTR_NAME_NFSV4_ACL
,
8043 .list
= nfs4_xattr_list_nfs4_acl
,
8044 .get
= nfs4_xattr_get_nfs4_acl
,
8045 .set
= nfs4_xattr_set_nfs4_acl
,
8048 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
8049 &nfs4_xattr_nfs4_acl_handler
,
8050 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8051 &nfs4_xattr_nfs4_label_handler
,