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 is_recover
: 1;
920 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
921 int err
, struct nfs4_exception
*exception
)
925 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
927 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
928 exception
->retry
= 1;
932 static enum open_claim_type4
933 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
934 enum open_claim_type4 claim
)
936 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
941 case NFS4_OPEN_CLAIM_FH
:
942 return NFS4_OPEN_CLAIM_NULL
;
943 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
944 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
945 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
946 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
950 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
952 p
->o_res
.f_attr
= &p
->f_attr
;
953 p
->o_res
.f_label
= p
->f_label
;
954 p
->o_res
.seqid
= p
->o_arg
.seqid
;
955 p
->c_res
.seqid
= p
->c_arg
.seqid
;
956 p
->o_res
.server
= p
->o_arg
.server
;
957 p
->o_res
.access_request
= p
->o_arg
.access
;
958 nfs_fattr_init(&p
->f_attr
);
959 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
962 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
963 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
964 const struct iattr
*attrs
,
965 struct nfs4_label
*label
,
966 enum open_claim_type4 claim
,
969 struct dentry
*parent
= dget_parent(dentry
);
970 struct inode
*dir
= parent
->d_inode
;
971 struct nfs_server
*server
= NFS_SERVER(dir
);
972 struct nfs4_opendata
*p
;
974 p
= kzalloc(sizeof(*p
), gfp_mask
);
978 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
979 if (IS_ERR(p
->f_label
))
982 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
983 if (p
->o_arg
.seqid
== NULL
)
985 nfs_sb_active(dentry
->d_sb
);
986 p
->dentry
= dget(dentry
);
989 atomic_inc(&sp
->so_count
);
990 p
->o_arg
.open_flags
= flags
;
991 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
992 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
993 * will return permission denied for all bits until close */
994 if (!(flags
& O_EXCL
)) {
995 /* ask server to check for all possible rights as results
997 p
->o_arg
.access
= NFS4_ACCESS_READ
| NFS4_ACCESS_MODIFY
|
998 NFS4_ACCESS_EXTEND
| NFS4_ACCESS_EXECUTE
;
1000 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1001 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1002 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1003 p
->o_arg
.name
= &dentry
->d_name
;
1004 p
->o_arg
.server
= server
;
1005 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1006 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1007 p
->o_arg
.label
= label
;
1008 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1009 switch (p
->o_arg
.claim
) {
1010 case NFS4_OPEN_CLAIM_NULL
:
1011 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1012 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1013 p
->o_arg
.fh
= NFS_FH(dir
);
1015 case NFS4_OPEN_CLAIM_PREVIOUS
:
1016 case NFS4_OPEN_CLAIM_FH
:
1017 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1018 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1019 p
->o_arg
.fh
= NFS_FH(dentry
->d_inode
);
1021 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1024 p
->o_arg
.u
.attrs
= &p
->attrs
;
1025 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1028 verf
[1] = current
->pid
;
1029 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1030 sizeof(p
->o_arg
.u
.verifier
.data
));
1032 p
->c_arg
.fh
= &p
->o_res
.fh
;
1033 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1034 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1035 nfs4_init_opendata_res(p
);
1036 kref_init(&p
->kref
);
1040 nfs4_label_free(p
->f_label
);
1048 static void nfs4_opendata_free(struct kref
*kref
)
1050 struct nfs4_opendata
*p
= container_of(kref
,
1051 struct nfs4_opendata
, kref
);
1052 struct super_block
*sb
= p
->dentry
->d_sb
;
1054 nfs_free_seqid(p
->o_arg
.seqid
);
1055 if (p
->state
!= NULL
)
1056 nfs4_put_open_state(p
->state
);
1057 nfs4_put_state_owner(p
->owner
);
1059 nfs4_label_free(p
->f_label
);
1063 nfs_sb_deactive(sb
);
1064 nfs_fattr_free_names(&p
->f_attr
);
1068 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1071 kref_put(&p
->kref
, nfs4_opendata_free
);
1074 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1078 ret
= rpc_wait_for_completion_task(task
);
1082 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1086 if (open_mode
& (O_EXCL
|O_TRUNC
))
1088 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1090 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1091 && state
->n_rdonly
!= 0;
1094 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1095 && state
->n_wronly
!= 0;
1097 case FMODE_READ
|FMODE_WRITE
:
1098 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1099 && state
->n_rdwr
!= 0;
1105 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
1107 if (delegation
== NULL
)
1109 if ((delegation
->type
& fmode
) != fmode
)
1111 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1113 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1115 nfs_mark_delegation_referenced(delegation
);
1119 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1128 case FMODE_READ
|FMODE_WRITE
:
1131 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1134 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1136 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1137 nfs4_stateid_copy(&state
->stateid
, stateid
);
1138 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1139 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1142 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1145 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1147 case FMODE_READ
|FMODE_WRITE
:
1148 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1152 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1154 write_seqlock(&state
->seqlock
);
1155 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
1156 write_sequnlock(&state
->seqlock
);
1159 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
1162 * Protect the call to nfs4_state_set_mode_locked and
1163 * serialise the stateid update
1165 write_seqlock(&state
->seqlock
);
1166 if (deleg_stateid
!= NULL
) {
1167 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1168 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1170 if (open_stateid
!= NULL
)
1171 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
1172 write_sequnlock(&state
->seqlock
);
1173 spin_lock(&state
->owner
->so_lock
);
1174 update_open_stateflags(state
, fmode
);
1175 spin_unlock(&state
->owner
->so_lock
);
1178 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
1180 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1181 struct nfs_delegation
*deleg_cur
;
1184 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1187 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1188 if (deleg_cur
== NULL
)
1191 spin_lock(&deleg_cur
->lock
);
1192 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1193 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1194 (deleg_cur
->type
& fmode
) != fmode
)
1195 goto no_delegation_unlock
;
1197 if (delegation
== NULL
)
1198 delegation
= &deleg_cur
->stateid
;
1199 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1200 goto no_delegation_unlock
;
1202 nfs_mark_delegation_referenced(deleg_cur
);
1203 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
1205 no_delegation_unlock
:
1206 spin_unlock(&deleg_cur
->lock
);
1210 if (!ret
&& open_stateid
!= NULL
) {
1211 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
1219 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1221 struct nfs_delegation
*delegation
;
1224 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1225 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1230 nfs4_inode_return_delegation(inode
);
1233 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1235 struct nfs4_state
*state
= opendata
->state
;
1236 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1237 struct nfs_delegation
*delegation
;
1238 int open_mode
= opendata
->o_arg
.open_flags
;
1239 fmode_t fmode
= opendata
->o_arg
.fmode
;
1240 nfs4_stateid stateid
;
1244 if (can_open_cached(state
, fmode
, open_mode
)) {
1245 spin_lock(&state
->owner
->so_lock
);
1246 if (can_open_cached(state
, fmode
, open_mode
)) {
1247 update_open_stateflags(state
, fmode
);
1248 spin_unlock(&state
->owner
->so_lock
);
1249 goto out_return_state
;
1251 spin_unlock(&state
->owner
->so_lock
);
1254 delegation
= rcu_dereference(nfsi
->delegation
);
1255 if (!can_open_delegated(delegation
, fmode
)) {
1259 /* Save the delegation */
1260 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1262 nfs_release_seqid(opendata
->o_arg
.seqid
);
1263 if (!opendata
->is_recover
) {
1264 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1270 /* Try to update the stateid using the delegation */
1271 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1272 goto out_return_state
;
1275 return ERR_PTR(ret
);
1277 atomic_inc(&state
->count
);
1282 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1284 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1285 struct nfs_delegation
*delegation
;
1286 int delegation_flags
= 0;
1289 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1291 delegation_flags
= delegation
->flags
;
1293 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_DELEGATE_CUR
) {
1294 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1295 "returning a delegation for "
1296 "OPEN(CLAIM_DELEGATE_CUR)\n",
1298 } else if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1299 nfs_inode_set_delegation(state
->inode
,
1300 data
->owner
->so_cred
,
1303 nfs_inode_reclaim_delegation(state
->inode
,
1304 data
->owner
->so_cred
,
1309 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1310 * and update the nfs4_state.
1312 static struct nfs4_state
*
1313 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1315 struct inode
*inode
= data
->state
->inode
;
1316 struct nfs4_state
*state
= data
->state
;
1319 if (!data
->rpc_done
) {
1320 ret
= data
->rpc_status
;
1325 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR_TYPE
) ||
1326 !(data
->f_attr
.valid
& NFS_ATTR_FATTR_FILEID
) ||
1327 !(data
->f_attr
.valid
& NFS_ATTR_FATTR_CHANGE
))
1331 state
= nfs4_get_open_state(inode
, data
->owner
);
1335 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1339 nfs_setsecurity(inode
, &data
->f_attr
, data
->f_label
);
1341 if (data
->o_res
.delegation_type
!= 0)
1342 nfs4_opendata_check_deleg(data
, state
);
1343 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1348 return ERR_PTR(ret
);
1352 static struct nfs4_state
*
1353 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1355 struct inode
*inode
;
1356 struct nfs4_state
*state
= NULL
;
1359 if (!data
->rpc_done
) {
1360 state
= nfs4_try_open_cached(data
);
1365 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1367 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1368 ret
= PTR_ERR(inode
);
1372 state
= nfs4_get_open_state(inode
, data
->owner
);
1375 if (data
->o_res
.delegation_type
!= 0)
1376 nfs4_opendata_check_deleg(data
, state
);
1377 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1381 nfs_release_seqid(data
->o_arg
.seqid
);
1386 return ERR_PTR(ret
);
1389 static struct nfs4_state
*
1390 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1392 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1393 return _nfs4_opendata_reclaim_to_nfs4_state(data
);
1394 return _nfs4_opendata_to_nfs4_state(data
);
1397 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1399 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1400 struct nfs_open_context
*ctx
;
1402 spin_lock(&state
->inode
->i_lock
);
1403 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1404 if (ctx
->state
!= state
)
1406 get_nfs_open_context(ctx
);
1407 spin_unlock(&state
->inode
->i_lock
);
1410 spin_unlock(&state
->inode
->i_lock
);
1411 return ERR_PTR(-ENOENT
);
1414 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1415 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1417 struct nfs4_opendata
*opendata
;
1419 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1420 NULL
, NULL
, claim
, GFP_NOFS
);
1421 if (opendata
== NULL
)
1422 return ERR_PTR(-ENOMEM
);
1423 opendata
->state
= state
;
1424 atomic_inc(&state
->count
);
1428 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1430 struct nfs4_state
*newstate
;
1433 opendata
->o_arg
.open_flags
= 0;
1434 opendata
->o_arg
.fmode
= fmode
;
1435 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1436 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1437 nfs4_init_opendata_res(opendata
);
1438 ret
= _nfs4_recover_proc_open(opendata
);
1441 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1442 if (IS_ERR(newstate
))
1443 return PTR_ERR(newstate
);
1444 nfs4_close_state(newstate
, fmode
);
1449 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1451 struct nfs4_state
*newstate
;
1454 /* memory barrier prior to reading state->n_* */
1455 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1456 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1458 if (state
->n_rdwr
!= 0) {
1459 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1460 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1463 if (newstate
!= state
)
1466 if (state
->n_wronly
!= 0) {
1467 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1468 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1471 if (newstate
!= state
)
1474 if (state
->n_rdonly
!= 0) {
1475 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1476 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1479 if (newstate
!= state
)
1483 * We may have performed cached opens for all three recoveries.
1484 * Check if we need to update the current stateid.
1486 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1487 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1488 write_seqlock(&state
->seqlock
);
1489 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1490 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1491 write_sequnlock(&state
->seqlock
);
1498 * reclaim state on the server after a reboot.
1500 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1502 struct nfs_delegation
*delegation
;
1503 struct nfs4_opendata
*opendata
;
1504 fmode_t delegation_type
= 0;
1507 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1508 NFS4_OPEN_CLAIM_PREVIOUS
);
1509 if (IS_ERR(opendata
))
1510 return PTR_ERR(opendata
);
1512 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1513 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1514 delegation_type
= delegation
->type
;
1516 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1517 status
= nfs4_open_recover(opendata
, state
);
1518 nfs4_opendata_put(opendata
);
1522 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1524 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1525 struct nfs4_exception exception
= { };
1528 err
= _nfs4_do_open_reclaim(ctx
, state
);
1529 trace_nfs4_open_reclaim(ctx
, 0, err
);
1530 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1532 if (err
!= -NFS4ERR_DELAY
)
1534 nfs4_handle_exception(server
, err
, &exception
);
1535 } while (exception
.retry
);
1539 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1541 struct nfs_open_context
*ctx
;
1544 ctx
= nfs4_state_find_open_context(state
);
1547 ret
= nfs4_do_open_reclaim(ctx
, state
);
1548 put_nfs_open_context(ctx
);
1552 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1556 printk(KERN_ERR
"NFS: %s: unhandled error "
1557 "%d.\n", __func__
, err
);
1562 case -NFS4ERR_BADSESSION
:
1563 case -NFS4ERR_BADSLOT
:
1564 case -NFS4ERR_BAD_HIGH_SLOT
:
1565 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1566 case -NFS4ERR_DEADSESSION
:
1567 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1568 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1570 case -NFS4ERR_STALE_CLIENTID
:
1571 case -NFS4ERR_STALE_STATEID
:
1572 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1573 case -NFS4ERR_EXPIRED
:
1574 /* Don't recall a delegation if it was lost */
1575 nfs4_schedule_lease_recovery(server
->nfs_client
);
1577 case -NFS4ERR_DELEG_REVOKED
:
1578 case -NFS4ERR_ADMIN_REVOKED
:
1579 case -NFS4ERR_BAD_STATEID
:
1580 case -NFS4ERR_OPENMODE
:
1581 nfs_inode_find_state_and_recover(state
->inode
,
1583 nfs4_schedule_stateid_recovery(server
, state
);
1585 case -NFS4ERR_DELAY
:
1586 case -NFS4ERR_GRACE
:
1587 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1591 case -NFS4ERR_DENIED
:
1592 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1598 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1600 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1601 struct nfs4_opendata
*opendata
;
1604 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1605 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
1606 if (IS_ERR(opendata
))
1607 return PTR_ERR(opendata
);
1608 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
1609 err
= nfs4_open_recover(opendata
, state
);
1610 nfs4_opendata_put(opendata
);
1611 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
1614 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
1616 struct nfs4_opendata
*data
= calldata
;
1618 nfs40_setup_sequence(data
->o_arg
.server
, &data
->o_arg
.seq_args
,
1619 &data
->o_res
.seq_res
, task
);
1622 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1624 struct nfs4_opendata
*data
= calldata
;
1626 nfs40_sequence_done(task
, &data
->o_res
.seq_res
);
1628 data
->rpc_status
= task
->tk_status
;
1629 if (data
->rpc_status
== 0) {
1630 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
1631 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1632 renew_lease(data
->o_res
.server
, data
->timestamp
);
1637 static void nfs4_open_confirm_release(void *calldata
)
1639 struct nfs4_opendata
*data
= calldata
;
1640 struct nfs4_state
*state
= NULL
;
1642 /* If this request hasn't been cancelled, do nothing */
1643 if (data
->cancelled
== 0)
1645 /* In case of error, no cleanup! */
1646 if (!data
->rpc_done
)
1648 state
= nfs4_opendata_to_nfs4_state(data
);
1650 nfs4_close_state(state
, data
->o_arg
.fmode
);
1652 nfs4_opendata_put(data
);
1655 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1656 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
1657 .rpc_call_done
= nfs4_open_confirm_done
,
1658 .rpc_release
= nfs4_open_confirm_release
,
1662 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1664 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1666 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1667 struct rpc_task
*task
;
1668 struct rpc_message msg
= {
1669 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1670 .rpc_argp
= &data
->c_arg
,
1671 .rpc_resp
= &data
->c_res
,
1672 .rpc_cred
= data
->owner
->so_cred
,
1674 struct rpc_task_setup task_setup_data
= {
1675 .rpc_client
= server
->client
,
1676 .rpc_message
= &msg
,
1677 .callback_ops
= &nfs4_open_confirm_ops
,
1678 .callback_data
= data
,
1679 .workqueue
= nfsiod_workqueue
,
1680 .flags
= RPC_TASK_ASYNC
,
1684 nfs4_init_sequence(&data
->o_arg
.seq_args
, &data
->o_res
.seq_res
, 1);
1685 kref_get(&data
->kref
);
1687 data
->rpc_status
= 0;
1688 data
->timestamp
= jiffies
;
1689 task
= rpc_run_task(&task_setup_data
);
1691 return PTR_ERR(task
);
1692 status
= nfs4_wait_for_completion_rpc_task(task
);
1694 data
->cancelled
= 1;
1697 status
= data
->rpc_status
;
1702 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1704 struct nfs4_opendata
*data
= calldata
;
1705 struct nfs4_state_owner
*sp
= data
->owner
;
1706 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
1708 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1711 * Check if we still need to send an OPEN call, or if we can use
1712 * a delegation instead.
1714 if (data
->state
!= NULL
) {
1715 struct nfs_delegation
*delegation
;
1717 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1720 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1721 if (data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
&&
1722 data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEG_CUR_FH
&&
1723 can_open_delegated(delegation
, data
->o_arg
.fmode
))
1724 goto unlock_no_action
;
1727 /* Update client id. */
1728 data
->o_arg
.clientid
= clp
->cl_clientid
;
1729 switch (data
->o_arg
.claim
) {
1730 case NFS4_OPEN_CLAIM_PREVIOUS
:
1731 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1732 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1733 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
1734 case NFS4_OPEN_CLAIM_FH
:
1735 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1736 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1738 data
->timestamp
= jiffies
;
1739 if (nfs4_setup_sequence(data
->o_arg
.server
,
1740 &data
->o_arg
.seq_args
,
1741 &data
->o_res
.seq_res
,
1743 nfs_release_seqid(data
->o_arg
.seqid
);
1745 /* Set the create mode (note dependency on the session type) */
1746 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
1747 if (data
->o_arg
.open_flags
& O_EXCL
) {
1748 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
1749 if (nfs4_has_persistent_session(clp
))
1750 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
1751 else if (clp
->cl_mvops
->minor_version
> 0)
1752 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
1758 task
->tk_action
= NULL
;
1760 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
1763 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1765 struct nfs4_opendata
*data
= calldata
;
1767 data
->rpc_status
= task
->tk_status
;
1769 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1772 if (task
->tk_status
== 0) {
1773 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
1774 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1778 data
->rpc_status
= -ELOOP
;
1781 data
->rpc_status
= -EISDIR
;
1784 data
->rpc_status
= -ENOTDIR
;
1787 renew_lease(data
->o_res
.server
, data
->timestamp
);
1788 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1789 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1794 static void nfs4_open_release(void *calldata
)
1796 struct nfs4_opendata
*data
= calldata
;
1797 struct nfs4_state
*state
= NULL
;
1799 /* If this request hasn't been cancelled, do nothing */
1800 if (data
->cancelled
== 0)
1802 /* In case of error, no cleanup! */
1803 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1805 /* In case we need an open_confirm, no cleanup! */
1806 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1808 state
= nfs4_opendata_to_nfs4_state(data
);
1810 nfs4_close_state(state
, data
->o_arg
.fmode
);
1812 nfs4_opendata_put(data
);
1815 static const struct rpc_call_ops nfs4_open_ops
= {
1816 .rpc_call_prepare
= nfs4_open_prepare
,
1817 .rpc_call_done
= nfs4_open_done
,
1818 .rpc_release
= nfs4_open_release
,
1821 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1823 struct inode
*dir
= data
->dir
->d_inode
;
1824 struct nfs_server
*server
= NFS_SERVER(dir
);
1825 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1826 struct nfs_openres
*o_res
= &data
->o_res
;
1827 struct rpc_task
*task
;
1828 struct rpc_message msg
= {
1829 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1832 .rpc_cred
= data
->owner
->so_cred
,
1834 struct rpc_task_setup task_setup_data
= {
1835 .rpc_client
= server
->client
,
1836 .rpc_message
= &msg
,
1837 .callback_ops
= &nfs4_open_ops
,
1838 .callback_data
= data
,
1839 .workqueue
= nfsiod_workqueue
,
1840 .flags
= RPC_TASK_ASYNC
,
1844 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
1845 kref_get(&data
->kref
);
1847 data
->rpc_status
= 0;
1848 data
->cancelled
= 0;
1849 data
->is_recover
= 0;
1851 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
1852 data
->is_recover
= 1;
1854 task
= rpc_run_task(&task_setup_data
);
1856 return PTR_ERR(task
);
1857 status
= nfs4_wait_for_completion_rpc_task(task
);
1859 data
->cancelled
= 1;
1862 status
= data
->rpc_status
;
1868 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1870 struct inode
*dir
= data
->dir
->d_inode
;
1871 struct nfs_openres
*o_res
= &data
->o_res
;
1874 status
= nfs4_run_open_task(data
, 1);
1875 if (status
!= 0 || !data
->rpc_done
)
1878 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
1880 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1881 status
= _nfs4_proc_open_confirm(data
);
1889 static int nfs4_opendata_access(struct rpc_cred
*cred
,
1890 struct nfs4_opendata
*opendata
,
1891 struct nfs4_state
*state
, fmode_t fmode
,
1894 struct nfs_access_entry cache
;
1897 /* access call failed or for some reason the server doesn't
1898 * support any access modes -- defer access call until later */
1899 if (opendata
->o_res
.access_supported
== 0)
1903 /* don't check MAY_WRITE - a newly created file may not have
1904 * write mode bits, but POSIX allows the creating process to write.
1905 * use openflags to check for exec, because fmode won't
1906 * always have FMODE_EXEC set when file open for exec. */
1907 if (openflags
& __FMODE_EXEC
) {
1908 /* ONLY check for exec rights */
1910 } else if (fmode
& FMODE_READ
)
1914 cache
.jiffies
= jiffies
;
1915 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
1916 nfs_access_add_cache(state
->inode
, &cache
);
1918 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
1921 /* even though OPEN succeeded, access is denied. Close the file */
1922 nfs4_close_state(state
, fmode
);
1927 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1929 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1931 struct inode
*dir
= data
->dir
->d_inode
;
1932 struct nfs_server
*server
= NFS_SERVER(dir
);
1933 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1934 struct nfs_openres
*o_res
= &data
->o_res
;
1937 status
= nfs4_run_open_task(data
, 0);
1938 if (!data
->rpc_done
)
1941 if (status
== -NFS4ERR_BADNAME
&&
1942 !(o_arg
->open_flags
& O_CREAT
))
1947 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
1949 if (o_arg
->open_flags
& O_CREAT
)
1950 update_changeattr(dir
, &o_res
->cinfo
);
1951 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1952 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1953 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1954 status
= _nfs4_proc_open_confirm(data
);
1958 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1959 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
1963 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1965 return nfs4_client_recover_expired_lease(server
->nfs_client
);
1970 * reclaim state on the server after a network partition.
1971 * Assumes caller holds the appropriate lock
1973 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1975 struct nfs4_opendata
*opendata
;
1978 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1979 NFS4_OPEN_CLAIM_FH
);
1980 if (IS_ERR(opendata
))
1981 return PTR_ERR(opendata
);
1982 ret
= nfs4_open_recover(opendata
, state
);
1984 d_drop(ctx
->dentry
);
1985 nfs4_opendata_put(opendata
);
1989 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1991 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1992 struct nfs4_exception exception
= { };
1996 err
= _nfs4_open_expired(ctx
, state
);
1997 trace_nfs4_open_expired(ctx
, 0, err
);
1998 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2003 case -NFS4ERR_GRACE
:
2004 case -NFS4ERR_DELAY
:
2005 nfs4_handle_exception(server
, err
, &exception
);
2008 } while (exception
.retry
);
2013 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2015 struct nfs_open_context
*ctx
;
2018 ctx
= nfs4_state_find_open_context(state
);
2021 ret
= nfs4_do_open_expired(ctx
, state
);
2022 put_nfs_open_context(ctx
);
2026 #if defined(CONFIG_NFS_V4_1)
2027 static void nfs41_clear_delegation_stateid(struct nfs4_state
*state
)
2029 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2030 nfs4_stateid
*stateid
= &state
->stateid
;
2031 struct nfs_delegation
*delegation
;
2032 struct rpc_cred
*cred
= NULL
;
2033 int status
= -NFS4ERR_BAD_STATEID
;
2035 /* If a state reset has been done, test_stateid is unneeded */
2036 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
2039 /* Get the delegation credential for use by test/free_stateid */
2041 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2042 if (delegation
!= NULL
&&
2043 nfs4_stateid_match(&delegation
->stateid
, stateid
)) {
2044 cred
= get_rpccred(delegation
->cred
);
2046 status
= nfs41_test_stateid(server
, stateid
, cred
);
2047 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2051 if (status
!= NFS_OK
) {
2052 /* Free the stateid unless the server explicitly
2053 * informs us the stateid is unrecognized. */
2054 if (status
!= -NFS4ERR_BAD_STATEID
)
2055 nfs41_free_stateid(server
, stateid
, cred
);
2056 nfs_remove_bad_delegation(state
->inode
);
2058 write_seqlock(&state
->seqlock
);
2059 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2060 write_sequnlock(&state
->seqlock
);
2061 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2069 * nfs41_check_open_stateid - possibly free an open stateid
2071 * @state: NFSv4 state for an inode
2073 * Returns NFS_OK if recovery for this stateid is now finished.
2074 * Otherwise a negative NFS4ERR value is returned.
2076 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2078 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2079 nfs4_stateid
*stateid
= &state
->open_stateid
;
2080 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2083 /* If a state reset has been done, test_stateid is unneeded */
2084 if ((test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) == 0) &&
2085 (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) == 0) &&
2086 (test_bit(NFS_O_RDWR_STATE
, &state
->flags
) == 0))
2087 return -NFS4ERR_BAD_STATEID
;
2089 status
= nfs41_test_stateid(server
, stateid
, cred
);
2090 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2091 if (status
!= NFS_OK
) {
2092 /* Free the stateid unless the server explicitly
2093 * informs us the stateid is unrecognized. */
2094 if (status
!= -NFS4ERR_BAD_STATEID
)
2095 nfs41_free_stateid(server
, stateid
, cred
);
2097 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2098 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2099 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2100 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2105 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2109 nfs41_clear_delegation_stateid(state
);
2110 status
= nfs41_check_open_stateid(state
);
2111 if (status
!= NFS_OK
)
2112 status
= nfs4_open_expired(sp
, state
);
2118 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2119 * fields corresponding to attributes that were used to store the verifier.
2120 * Make sure we clobber those fields in the later setattr call
2122 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
2124 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2125 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2126 sattr
->ia_valid
|= ATTR_ATIME
;
2128 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2129 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2130 sattr
->ia_valid
|= ATTR_MTIME
;
2133 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2136 struct nfs_open_context
*ctx
)
2138 struct nfs4_state_owner
*sp
= opendata
->owner
;
2139 struct nfs_server
*server
= sp
->so_server
;
2140 struct dentry
*dentry
;
2141 struct nfs4_state
*state
;
2145 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2147 ret
= _nfs4_proc_open(opendata
);
2151 state
= nfs4_opendata_to_nfs4_state(opendata
);
2152 ret
= PTR_ERR(state
);
2155 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2156 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2158 dentry
= opendata
->dentry
;
2159 if (dentry
->d_inode
== NULL
) {
2160 /* FIXME: Is this d_drop() ever needed? */
2162 dentry
= d_add_unique(dentry
, igrab(state
->inode
));
2163 if (dentry
== NULL
) {
2164 dentry
= opendata
->dentry
;
2165 } else if (dentry
!= ctx
->dentry
) {
2167 ctx
->dentry
= dget(dentry
);
2169 nfs_set_verifier(dentry
,
2170 nfs_save_change_attribute(opendata
->dir
->d_inode
));
2173 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2178 if (dentry
->d_inode
== state
->inode
) {
2179 nfs_inode_attach_open_context(ctx
);
2180 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2181 nfs4_schedule_stateid_recovery(server
, state
);
2188 * Returns a referenced nfs4_state
2190 static int _nfs4_do_open(struct inode
*dir
,
2191 struct nfs_open_context
*ctx
,
2193 struct iattr
*sattr
,
2194 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
);
2265 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
))
2266 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2268 kfree(opendata
->f_attr
.mdsthreshold
);
2269 opendata
->f_attr
.mdsthreshold
= NULL
;
2271 nfs4_label_free(olabel
);
2273 nfs4_opendata_put(opendata
);
2274 nfs4_put_state_owner(sp
);
2277 nfs4_label_free(olabel
);
2279 kfree(opendata
->f_attr
.mdsthreshold
);
2280 nfs4_opendata_put(opendata
);
2281 err_put_state_owner
:
2282 nfs4_put_state_owner(sp
);
2288 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2289 struct nfs_open_context
*ctx
,
2291 struct iattr
*sattr
,
2292 struct nfs4_label
*label
)
2294 struct nfs_server
*server
= NFS_SERVER(dir
);
2295 struct nfs4_exception exception
= { };
2296 struct nfs4_state
*res
;
2300 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
);
2302 trace_nfs4_open_file(ctx
, flags
, status
);
2305 /* NOTE: BAD_SEQID means the server and client disagree about the
2306 * book-keeping w.r.t. state-changing operations
2307 * (OPEN/CLOSE/LOCK/LOCKU...)
2308 * It is actually a sign of a bug on the client or on the server.
2310 * If we receive a BAD_SEQID error in the particular case of
2311 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2312 * have unhashed the old state_owner for us, and that we can
2313 * therefore safely retry using a new one. We should still warn
2314 * the user though...
2316 if (status
== -NFS4ERR_BAD_SEQID
) {
2317 pr_warn_ratelimited("NFS: v4 server %s "
2318 " returned a bad sequence-id error!\n",
2319 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2320 exception
.retry
= 1;
2324 * BAD_STATEID on OPEN means that the server cancelled our
2325 * state before it received the OPEN_CONFIRM.
2326 * Recover by retrying the request as per the discussion
2327 * on Page 181 of RFC3530.
2329 if (status
== -NFS4ERR_BAD_STATEID
) {
2330 exception
.retry
= 1;
2333 if (status
== -EAGAIN
) {
2334 /* We must have found a delegation */
2335 exception
.retry
= 1;
2338 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2340 res
= ERR_PTR(nfs4_handle_exception(server
,
2341 status
, &exception
));
2342 } while (exception
.retry
);
2346 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2347 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2348 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2349 struct nfs4_label
*olabel
)
2351 struct nfs_server
*server
= NFS_SERVER(inode
);
2352 struct nfs_setattrargs arg
= {
2353 .fh
= NFS_FH(inode
),
2356 .bitmask
= server
->attr_bitmask
,
2359 struct nfs_setattrres res
= {
2364 struct rpc_message msg
= {
2365 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2370 unsigned long timestamp
= jiffies
;
2375 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2377 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2379 nfs_fattr_init(fattr
);
2381 /* Servers should only apply open mode checks for file size changes */
2382 truncate
= (sattr
->ia_valid
& ATTR_SIZE
) ? true : false;
2383 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2385 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
, fmode
)) {
2386 /* Use that stateid */
2387 } else if (truncate
&& state
!= NULL
&& nfs4_valid_open_stateid(state
)) {
2388 struct nfs_lockowner lockowner
= {
2389 .l_owner
= current
->files
,
2390 .l_pid
= current
->tgid
,
2392 nfs4_select_rw_stateid(&arg
.stateid
, state
, FMODE_WRITE
,
2395 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
2397 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2398 if (status
== 0 && state
!= NULL
)
2399 renew_lease(server
, timestamp
);
2403 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2404 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2405 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2406 struct nfs4_label
*olabel
)
2408 struct nfs_server
*server
= NFS_SERVER(inode
);
2409 struct nfs4_exception exception
= {
2415 err
= _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, ilabel
, olabel
);
2416 trace_nfs4_setattr(inode
, err
);
2418 case -NFS4ERR_OPENMODE
:
2419 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2420 pr_warn_once("NFSv4: server %s is incorrectly "
2421 "applying open mode checks to "
2422 "a SETATTR that is not "
2423 "changing file size.\n",
2424 server
->nfs_client
->cl_hostname
);
2426 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2428 if (sattr
->ia_valid
& ATTR_OPEN
)
2433 err
= nfs4_handle_exception(server
, err
, &exception
);
2434 } while (exception
.retry
);
2439 struct nfs4_closedata
{
2440 struct inode
*inode
;
2441 struct nfs4_state
*state
;
2442 struct nfs_closeargs arg
;
2443 struct nfs_closeres res
;
2444 struct nfs_fattr fattr
;
2445 unsigned long timestamp
;
2450 static void nfs4_free_closedata(void *data
)
2452 struct nfs4_closedata
*calldata
= data
;
2453 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2454 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2457 pnfs_roc_release(calldata
->state
->inode
);
2458 nfs4_put_open_state(calldata
->state
);
2459 nfs_free_seqid(calldata
->arg
.seqid
);
2460 nfs4_put_state_owner(sp
);
2461 nfs_sb_deactive(sb
);
2465 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
2468 spin_lock(&state
->owner
->so_lock
);
2469 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2470 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
2472 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2475 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2478 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2479 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2480 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2482 spin_unlock(&state
->owner
->so_lock
);
2485 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2487 struct nfs4_closedata
*calldata
= data
;
2488 struct nfs4_state
*state
= calldata
->state
;
2489 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2491 dprintk("%s: begin!\n", __func__
);
2492 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2494 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
2495 /* hmm. we are done with the inode, and in the process of freeing
2496 * the state_owner. we keep this around to process errors
2498 switch (task
->tk_status
) {
2501 pnfs_roc_set_barrier(state
->inode
,
2502 calldata
->roc_barrier
);
2503 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
2504 renew_lease(server
, calldata
->timestamp
);
2505 nfs4_close_clear_stateid_flags(state
,
2506 calldata
->arg
.fmode
);
2508 case -NFS4ERR_STALE_STATEID
:
2509 case -NFS4ERR_OLD_STATEID
:
2510 case -NFS4ERR_BAD_STATEID
:
2511 case -NFS4ERR_EXPIRED
:
2512 if (calldata
->arg
.fmode
== 0)
2515 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
2516 rpc_restart_call_prepare(task
);
2518 nfs_release_seqid(calldata
->arg
.seqid
);
2519 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
2520 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
2523 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
2525 struct nfs4_closedata
*calldata
= data
;
2526 struct nfs4_state
*state
= calldata
->state
;
2527 struct inode
*inode
= calldata
->inode
;
2530 dprintk("%s: begin!\n", __func__
);
2531 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
2534 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
2535 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
2536 spin_lock(&state
->owner
->so_lock
);
2537 /* Calculate the change in open mode */
2538 if (state
->n_rdwr
== 0) {
2539 if (state
->n_rdonly
== 0) {
2540 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2541 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2542 calldata
->arg
.fmode
&= ~FMODE_READ
;
2544 if (state
->n_wronly
== 0) {
2545 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2546 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2547 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
2550 if (!nfs4_valid_open_stateid(state
))
2552 spin_unlock(&state
->owner
->so_lock
);
2555 /* Note: exit _without_ calling nfs4_close_done */
2559 if (calldata
->arg
.fmode
== 0) {
2560 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
2561 if (calldata
->roc
&&
2562 pnfs_roc_drain(inode
, &calldata
->roc_barrier
, task
)) {
2563 nfs_release_seqid(calldata
->arg
.seqid
);
2568 nfs_fattr_init(calldata
->res
.fattr
);
2569 calldata
->timestamp
= jiffies
;
2570 if (nfs4_setup_sequence(NFS_SERVER(inode
),
2571 &calldata
->arg
.seq_args
,
2572 &calldata
->res
.seq_res
,
2574 nfs_release_seqid(calldata
->arg
.seqid
);
2575 dprintk("%s: done!\n", __func__
);
2578 task
->tk_action
= NULL
;
2580 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
2583 static const struct rpc_call_ops nfs4_close_ops
= {
2584 .rpc_call_prepare
= nfs4_close_prepare
,
2585 .rpc_call_done
= nfs4_close_done
,
2586 .rpc_release
= nfs4_free_closedata
,
2590 * It is possible for data to be read/written from a mem-mapped file
2591 * after the sys_close call (which hits the vfs layer as a flush).
2592 * This means that we can't safely call nfsv4 close on a file until
2593 * the inode is cleared. This in turn means that we are not good
2594 * NFSv4 citizens - we do not indicate to the server to update the file's
2595 * share state even when we are done with one of the three share
2596 * stateid's in the inode.
2598 * NOTE: Caller must be holding the sp->so_owner semaphore!
2600 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
2602 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2603 struct nfs4_closedata
*calldata
;
2604 struct nfs4_state_owner
*sp
= state
->owner
;
2605 struct rpc_task
*task
;
2606 struct rpc_message msg
= {
2607 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
2608 .rpc_cred
= state
->owner
->so_cred
,
2610 struct rpc_task_setup task_setup_data
= {
2611 .rpc_client
= server
->client
,
2612 .rpc_message
= &msg
,
2613 .callback_ops
= &nfs4_close_ops
,
2614 .workqueue
= nfsiod_workqueue
,
2615 .flags
= RPC_TASK_ASYNC
,
2617 int status
= -ENOMEM
;
2619 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
2620 &task_setup_data
.rpc_client
, &msg
);
2622 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
2623 if (calldata
== NULL
)
2625 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
2626 calldata
->inode
= state
->inode
;
2627 calldata
->state
= state
;
2628 calldata
->arg
.fh
= NFS_FH(state
->inode
);
2629 calldata
->arg
.stateid
= &state
->open_stateid
;
2630 /* Serialization for the sequence id */
2631 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
2632 if (calldata
->arg
.seqid
== NULL
)
2633 goto out_free_calldata
;
2634 calldata
->arg
.fmode
= 0;
2635 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
2636 calldata
->res
.fattr
= &calldata
->fattr
;
2637 calldata
->res
.seqid
= calldata
->arg
.seqid
;
2638 calldata
->res
.server
= server
;
2639 calldata
->roc
= pnfs_roc(state
->inode
);
2640 nfs_sb_active(calldata
->inode
->i_sb
);
2642 msg
.rpc_argp
= &calldata
->arg
;
2643 msg
.rpc_resp
= &calldata
->res
;
2644 task_setup_data
.callback_data
= calldata
;
2645 task
= rpc_run_task(&task_setup_data
);
2647 return PTR_ERR(task
);
2650 status
= rpc_wait_for_completion_task(task
);
2656 nfs4_put_open_state(state
);
2657 nfs4_put_state_owner(sp
);
2661 static struct inode
*
2662 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
, int open_flags
, struct iattr
*attr
)
2664 struct nfs4_state
*state
;
2665 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
2667 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
2669 /* Protect against concurrent sillydeletes */
2670 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
);
2672 nfs4_label_release_security(label
);
2675 return ERR_CAST(state
);
2676 return state
->inode
;
2679 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2681 if (ctx
->state
== NULL
)
2684 nfs4_close_sync(ctx
->state
, ctx
->mode
);
2686 nfs4_close_state(ctx
->state
, ctx
->mode
);
2689 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2691 struct nfs4_server_caps_arg args
= {
2694 struct nfs4_server_caps_res res
= {};
2695 struct rpc_message msg
= {
2696 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2702 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2704 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2705 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2706 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2707 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2708 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2709 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2710 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2711 server
->caps
|= NFS_CAP_ACLS
;
2712 if (res
.has_links
!= 0)
2713 server
->caps
|= NFS_CAP_HARDLINKS
;
2714 if (res
.has_symlinks
!= 0)
2715 server
->caps
|= NFS_CAP_SYMLINKS
;
2716 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2717 server
->caps
|= NFS_CAP_FILEID
;
2718 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2719 server
->caps
|= NFS_CAP_MODE
;
2720 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2721 server
->caps
|= NFS_CAP_NLINK
;
2722 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2723 server
->caps
|= NFS_CAP_OWNER
;
2724 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2725 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2726 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2727 server
->caps
|= NFS_CAP_ATIME
;
2728 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2729 server
->caps
|= NFS_CAP_CTIME
;
2730 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2731 server
->caps
|= NFS_CAP_MTIME
;
2732 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2733 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2734 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
2736 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
2737 sizeof(server
->attr_bitmask
));
2739 if (server
->caps
& NFS_CAP_SECURITY_LABEL
) {
2740 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
2741 res
.attr_bitmask
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
2743 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2744 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2745 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2746 server
->acl_bitmask
= res
.acl_bitmask
;
2747 server
->fh_expire_type
= res
.fh_expire_type
;
2753 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2755 struct nfs4_exception exception
= { };
2758 err
= nfs4_handle_exception(server
,
2759 _nfs4_server_capabilities(server
, fhandle
),
2761 } while (exception
.retry
);
2765 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2766 struct nfs_fsinfo
*info
)
2769 struct nfs4_lookup_root_arg args
= {
2772 struct nfs4_lookup_res res
= {
2774 .fattr
= info
->fattr
,
2777 struct rpc_message msg
= {
2778 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2783 bitmask
[0] = nfs4_fattr_bitmap
[0];
2784 bitmask
[1] = nfs4_fattr_bitmap
[1];
2786 * Process the label in the upcoming getfattr
2788 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
2790 nfs_fattr_init(info
->fattr
);
2791 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2794 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2795 struct nfs_fsinfo
*info
)
2797 struct nfs4_exception exception
= { };
2800 err
= _nfs4_lookup_root(server
, fhandle
, info
);
2801 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
2804 case -NFS4ERR_WRONGSEC
:
2807 err
= nfs4_handle_exception(server
, err
, &exception
);
2809 } while (exception
.retry
);
2814 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2815 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
2817 struct rpc_auth_create_args auth_args
= {
2818 .pseudoflavor
= flavor
,
2820 struct rpc_auth
*auth
;
2823 auth
= rpcauth_create(&auth_args
, server
->client
);
2828 ret
= nfs4_lookup_root(server
, fhandle
, info
);
2834 * Retry pseudoroot lookup with various security flavors. We do this when:
2836 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2837 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2839 * Returns zero on success, or a negative NFS4ERR value, or a
2840 * negative errno value.
2842 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2843 struct nfs_fsinfo
*info
)
2845 /* Per 3530bis 15.33.5 */
2846 static const rpc_authflavor_t flav_array
[] = {
2850 RPC_AUTH_UNIX
, /* courtesy */
2853 int status
= -EPERM
;
2856 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
2857 status
= nfs4_lookup_root_sec(server
, fhandle
, info
, flav_array
[i
]);
2858 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
2864 * -EACCESS could mean that the user doesn't have correct permissions
2865 * to access the mount. It could also mean that we tried to mount
2866 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2867 * existing mount programs don't handle -EACCES very well so it should
2868 * be mapped to -EPERM instead.
2870 if (status
== -EACCES
)
2875 static int nfs4_do_find_root_sec(struct nfs_server
*server
,
2876 struct nfs_fh
*fhandle
, struct nfs_fsinfo
*info
)
2878 int mv
= server
->nfs_client
->cl_minorversion
;
2879 return nfs_v4_minor_ops
[mv
]->find_root_sec(server
, fhandle
, info
);
2883 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2884 * @server: initialized nfs_server handle
2885 * @fhandle: we fill in the pseudo-fs root file handle
2886 * @info: we fill in an FSINFO struct
2887 * @auth_probe: probe the auth flavours
2889 * Returns zero on success, or a negative errno.
2891 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2892 struct nfs_fsinfo
*info
,
2897 switch (auth_probe
) {
2899 status
= nfs4_lookup_root(server
, fhandle
, info
);
2900 if (status
!= -NFS4ERR_WRONGSEC
)
2902 /* Did user force a 'sec=' mount option? */
2903 if (server
->flags
& NFS_MOUNT_SECFLAVOUR
)
2906 status
= nfs4_do_find_root_sec(server
, fhandle
, info
);
2910 status
= nfs4_server_capabilities(server
, fhandle
);
2912 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2914 return nfs4_map_errors(status
);
2917 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
2918 struct nfs_fsinfo
*info
)
2921 struct nfs_fattr
*fattr
= info
->fattr
;
2922 struct nfs4_label
*label
= NULL
;
2924 error
= nfs4_server_capabilities(server
, mntfh
);
2926 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
2930 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
2932 return PTR_ERR(label
);
2934 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
2936 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
2937 goto err_free_label
;
2940 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
2941 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
2942 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
2945 nfs4_label_free(label
);
2951 * Get locations and (maybe) other attributes of a referral.
2952 * Note that we'll actually follow the referral later when
2953 * we detect fsid mismatch in inode revalidation
2955 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
2956 const struct qstr
*name
, struct nfs_fattr
*fattr
,
2957 struct nfs_fh
*fhandle
)
2959 int status
= -ENOMEM
;
2960 struct page
*page
= NULL
;
2961 struct nfs4_fs_locations
*locations
= NULL
;
2963 page
= alloc_page(GFP_KERNEL
);
2966 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2967 if (locations
== NULL
)
2970 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
2973 /* Make sure server returned a different fsid for the referral */
2974 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2975 dprintk("%s: server did not return a different fsid for"
2976 " a referral at %s\n", __func__
, name
->name
);
2980 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2981 nfs_fixup_referral_attributes(&locations
->fattr
);
2983 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2984 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2985 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2993 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2994 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
2996 struct nfs4_getattr_arg args
= {
2998 .bitmask
= server
->attr_bitmask
,
3000 struct nfs4_getattr_res res
= {
3005 struct rpc_message msg
= {
3006 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3011 args
.bitmask
= nfs4_bitmask(server
, label
);
3013 nfs_fattr_init(fattr
);
3014 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3017 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3018 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3020 struct nfs4_exception exception
= { };
3023 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3024 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3025 err
= nfs4_handle_exception(server
, err
,
3027 } while (exception
.retry
);
3032 * The file is not closed if it is opened due to the a request to change
3033 * the size of the file. The open call will not be needed once the
3034 * VFS layer lookup-intents are implemented.
3036 * Close is called when the inode is destroyed.
3037 * If we haven't opened the file for O_WRONLY, we
3038 * need to in the size_change case to obtain a stateid.
3041 * Because OPEN is always done by name in nfsv4, it is
3042 * possible that we opened a different file by the same
3043 * name. We can recognize this race condition, but we
3044 * can't do anything about it besides returning an error.
3046 * This will be fixed with VFS changes (lookup-intent).
3049 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3050 struct iattr
*sattr
)
3052 struct inode
*inode
= dentry
->d_inode
;
3053 struct rpc_cred
*cred
= NULL
;
3054 struct nfs4_state
*state
= NULL
;
3055 struct nfs4_label
*label
= NULL
;
3058 if (pnfs_ld_layoutret_on_setattr(inode
))
3059 pnfs_commit_and_return_layout(inode
);
3061 nfs_fattr_init(fattr
);
3063 /* Deal with open(O_TRUNC) */
3064 if (sattr
->ia_valid
& ATTR_OPEN
)
3065 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3067 /* Optimization: if the end result is no change, don't RPC */
3068 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3071 /* Search for an existing open(O_WRITE) file */
3072 if (sattr
->ia_valid
& ATTR_FILE
) {
3073 struct nfs_open_context
*ctx
;
3075 ctx
= nfs_file_open_context(sattr
->ia_file
);
3082 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3084 return PTR_ERR(label
);
3086 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
3088 nfs_setattr_update_inode(inode
, sattr
);
3089 nfs_setsecurity(inode
, fattr
, label
);
3091 nfs4_label_free(label
);
3095 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3096 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3097 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3099 struct nfs_server
*server
= NFS_SERVER(dir
);
3101 struct nfs4_lookup_arg args
= {
3102 .bitmask
= server
->attr_bitmask
,
3103 .dir_fh
= NFS_FH(dir
),
3106 struct nfs4_lookup_res res
= {
3112 struct rpc_message msg
= {
3113 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3118 args
.bitmask
= nfs4_bitmask(server
, label
);
3120 nfs_fattr_init(fattr
);
3122 dprintk("NFS call lookup %s\n", name
->name
);
3123 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3124 dprintk("NFS reply lookup: %d\n", status
);
3128 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3130 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3131 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3132 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3136 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3137 struct qstr
*name
, struct nfs_fh
*fhandle
,
3138 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3140 struct nfs4_exception exception
= { };
3141 struct rpc_clnt
*client
= *clnt
;
3144 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3145 trace_nfs4_lookup(dir
, name
, err
);
3147 case -NFS4ERR_BADNAME
:
3150 case -NFS4ERR_MOVED
:
3151 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3153 case -NFS4ERR_WRONGSEC
:
3155 if (client
!= *clnt
)
3157 /* No security negotiation if the user specified 'sec=' */
3158 if (NFS_SERVER(dir
)->flags
& NFS_MOUNT_SECFLAVOUR
)
3160 client
= nfs4_create_sec_client(client
, dir
, name
);
3162 return PTR_ERR(client
);
3164 exception
.retry
= 1;
3167 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3169 } while (exception
.retry
);
3174 else if (client
!= *clnt
)
3175 rpc_shutdown_client(client
);
3180 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
3181 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3182 struct nfs4_label
*label
)
3185 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3187 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3188 if (client
!= NFS_CLIENT(dir
)) {
3189 rpc_shutdown_client(client
);
3190 nfs_fixup_secinfo_attributes(fattr
);
3196 nfs4_proc_lookup_mountpoint(struct inode
*dir
, struct qstr
*name
,
3197 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3199 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3202 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3204 return ERR_PTR(status
);
3205 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3208 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3210 struct nfs_server
*server
= NFS_SERVER(inode
);
3211 struct nfs4_accessargs args
= {
3212 .fh
= NFS_FH(inode
),
3213 .bitmask
= server
->cache_consistency_bitmask
,
3215 struct nfs4_accessres res
= {
3218 struct rpc_message msg
= {
3219 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3222 .rpc_cred
= entry
->cred
,
3224 int mode
= entry
->mask
;
3228 * Determine which access bits we want to ask for...
3230 if (mode
& MAY_READ
)
3231 args
.access
|= NFS4_ACCESS_READ
;
3232 if (S_ISDIR(inode
->i_mode
)) {
3233 if (mode
& MAY_WRITE
)
3234 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3235 if (mode
& MAY_EXEC
)
3236 args
.access
|= NFS4_ACCESS_LOOKUP
;
3238 if (mode
& MAY_WRITE
)
3239 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3240 if (mode
& MAY_EXEC
)
3241 args
.access
|= NFS4_ACCESS_EXECUTE
;
3244 res
.fattr
= nfs_alloc_fattr();
3245 if (res
.fattr
== NULL
)
3248 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3250 nfs_access_set_mask(entry
, res
.access
);
3251 nfs_refresh_inode(inode
, res
.fattr
);
3253 nfs_free_fattr(res
.fattr
);
3257 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3259 struct nfs4_exception exception
= { };
3262 err
= _nfs4_proc_access(inode
, entry
);
3263 trace_nfs4_access(inode
, err
);
3264 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3266 } while (exception
.retry
);
3271 * TODO: For the time being, we don't try to get any attributes
3272 * along with any of the zero-copy operations READ, READDIR,
3275 * In the case of the first three, we want to put the GETATTR
3276 * after the read-type operation -- this is because it is hard
3277 * to predict the length of a GETATTR response in v4, and thus
3278 * align the READ data correctly. This means that the GETATTR
3279 * may end up partially falling into the page cache, and we should
3280 * shift it into the 'tail' of the xdr_buf before processing.
3281 * To do this efficiently, we need to know the total length
3282 * of data received, which doesn't seem to be available outside
3285 * In the case of WRITE, we also want to put the GETATTR after
3286 * the operation -- in this case because we want to make sure
3287 * we get the post-operation mtime and size.
3289 * Both of these changes to the XDR layer would in fact be quite
3290 * minor, but I decided to leave them for a subsequent patch.
3292 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3293 unsigned int pgbase
, unsigned int pglen
)
3295 struct nfs4_readlink args
= {
3296 .fh
= NFS_FH(inode
),
3301 struct nfs4_readlink_res res
;
3302 struct rpc_message msg
= {
3303 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3308 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3311 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3312 unsigned int pgbase
, unsigned int pglen
)
3314 struct nfs4_exception exception
= { };
3317 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3318 trace_nfs4_readlink(inode
, err
);
3319 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3321 } while (exception
.retry
);
3326 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3329 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3332 struct nfs4_label l
, *ilabel
= NULL
;
3333 struct nfs_open_context
*ctx
;
3334 struct nfs4_state
*state
;
3337 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
3339 return PTR_ERR(ctx
);
3341 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3343 sattr
->ia_mode
&= ~current_umask();
3344 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
);
3345 if (IS_ERR(state
)) {
3346 status
= PTR_ERR(state
);
3350 nfs4_label_release_security(ilabel
);
3351 put_nfs_open_context(ctx
);
3355 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3357 struct nfs_server
*server
= NFS_SERVER(dir
);
3358 struct nfs_removeargs args
= {
3362 struct nfs_removeres res
= {
3365 struct rpc_message msg
= {
3366 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3372 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3374 update_changeattr(dir
, &res
.cinfo
);
3378 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3380 struct nfs4_exception exception
= { };
3383 err
= _nfs4_proc_remove(dir
, name
);
3384 trace_nfs4_remove(dir
, name
, err
);
3385 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3387 } while (exception
.retry
);
3391 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
3393 struct nfs_server
*server
= NFS_SERVER(dir
);
3394 struct nfs_removeargs
*args
= msg
->rpc_argp
;
3395 struct nfs_removeres
*res
= msg
->rpc_resp
;
3397 res
->server
= server
;
3398 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
3399 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
3401 nfs_fattr_init(res
->dir_attr
);
3404 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
3406 nfs4_setup_sequence(NFS_SERVER(data
->dir
),
3407 &data
->args
.seq_args
,
3412 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
3414 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
3415 struct nfs_removeres
*res
= &data
->res
;
3417 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3419 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3421 update_changeattr(dir
, &res
->cinfo
);
3425 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
3427 struct nfs_server
*server
= NFS_SERVER(dir
);
3428 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
3429 struct nfs_renameres
*res
= msg
->rpc_resp
;
3431 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
3432 res
->server
= server
;
3433 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
3436 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
3438 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
3439 &data
->args
.seq_args
,
3444 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
3445 struct inode
*new_dir
)
3447 struct nfs_renamedata
*data
= task
->tk_calldata
;
3448 struct nfs_renameres
*res
= &data
->res
;
3450 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3452 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3455 update_changeattr(old_dir
, &res
->old_cinfo
);
3456 update_changeattr(new_dir
, &res
->new_cinfo
);
3460 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3461 struct inode
*new_dir
, struct qstr
*new_name
)
3463 struct nfs_server
*server
= NFS_SERVER(old_dir
);
3464 struct nfs_renameargs arg
= {
3465 .old_dir
= NFS_FH(old_dir
),
3466 .new_dir
= NFS_FH(new_dir
),
3467 .old_name
= old_name
,
3468 .new_name
= new_name
,
3470 struct nfs_renameres res
= {
3473 struct rpc_message msg
= {
3474 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
3478 int status
= -ENOMEM
;
3480 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3482 update_changeattr(old_dir
, &res
.old_cinfo
);
3483 update_changeattr(new_dir
, &res
.new_cinfo
);
3488 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3489 struct inode
*new_dir
, struct qstr
*new_name
)
3491 struct nfs4_exception exception
= { };
3494 err
= _nfs4_proc_rename(old_dir
, old_name
,
3496 trace_nfs4_rename(old_dir
, old_name
, new_dir
, new_name
, err
);
3497 err
= nfs4_handle_exception(NFS_SERVER(old_dir
), err
,
3499 } while (exception
.retry
);
3503 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3505 struct nfs_server
*server
= NFS_SERVER(inode
);
3506 struct nfs4_link_arg arg
= {
3507 .fh
= NFS_FH(inode
),
3508 .dir_fh
= NFS_FH(dir
),
3510 .bitmask
= server
->attr_bitmask
,
3512 struct nfs4_link_res res
= {
3516 struct rpc_message msg
= {
3517 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
3521 int status
= -ENOMEM
;
3523 res
.fattr
= nfs_alloc_fattr();
3524 if (res
.fattr
== NULL
)
3527 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3528 if (IS_ERR(res
.label
)) {
3529 status
= PTR_ERR(res
.label
);
3532 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
3534 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3536 update_changeattr(dir
, &res
.cinfo
);
3537 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
3539 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
3543 nfs4_label_free(res
.label
);
3546 nfs_free_fattr(res
.fattr
);
3550 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3552 struct nfs4_exception exception
= { };
3555 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3556 _nfs4_proc_link(inode
, dir
, name
),
3558 } while (exception
.retry
);
3562 struct nfs4_createdata
{
3563 struct rpc_message msg
;
3564 struct nfs4_create_arg arg
;
3565 struct nfs4_create_res res
;
3567 struct nfs_fattr fattr
;
3568 struct nfs4_label
*label
;
3571 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
3572 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
3574 struct nfs4_createdata
*data
;
3576 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3578 struct nfs_server
*server
= NFS_SERVER(dir
);
3580 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3581 if (IS_ERR(data
->label
))
3584 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
3585 data
->msg
.rpc_argp
= &data
->arg
;
3586 data
->msg
.rpc_resp
= &data
->res
;
3587 data
->arg
.dir_fh
= NFS_FH(dir
);
3588 data
->arg
.server
= server
;
3589 data
->arg
.name
= name
;
3590 data
->arg
.attrs
= sattr
;
3591 data
->arg
.ftype
= ftype
;
3592 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
3593 data
->res
.server
= server
;
3594 data
->res
.fh
= &data
->fh
;
3595 data
->res
.fattr
= &data
->fattr
;
3596 data
->res
.label
= data
->label
;
3597 nfs_fattr_init(data
->res
.fattr
);
3605 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
3607 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
3608 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
3610 update_changeattr(dir
, &data
->res
.dir_cinfo
);
3611 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
3616 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
3618 nfs4_label_free(data
->label
);
3622 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3623 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
3624 struct nfs4_label
*label
)
3626 struct nfs4_createdata
*data
;
3627 int status
= -ENAMETOOLONG
;
3629 if (len
> NFS4_MAXPATHLEN
)
3633 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
3637 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
3638 data
->arg
.u
.symlink
.pages
= &page
;
3639 data
->arg
.u
.symlink
.len
= len
;
3640 data
->arg
.label
= label
;
3642 status
= nfs4_do_create(dir
, dentry
, data
);
3644 nfs4_free_createdata(data
);
3649 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3650 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
3652 struct nfs4_exception exception
= { };
3653 struct nfs4_label l
, *label
= NULL
;
3656 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3659 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
3660 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
3661 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3663 } while (exception
.retry
);
3665 nfs4_label_release_security(label
);
3669 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3670 struct iattr
*sattr
, struct nfs4_label
*label
)
3672 struct nfs4_createdata
*data
;
3673 int status
= -ENOMEM
;
3675 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
3679 data
->arg
.label
= label
;
3680 status
= nfs4_do_create(dir
, dentry
, data
);
3682 nfs4_free_createdata(data
);
3687 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3688 struct iattr
*sattr
)
3690 struct nfs4_exception exception
= { };
3691 struct nfs4_label l
, *label
= NULL
;
3694 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3696 sattr
->ia_mode
&= ~current_umask();
3698 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
3699 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
3700 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3702 } while (exception
.retry
);
3703 nfs4_label_release_security(label
);
3708 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3709 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3711 struct inode
*dir
= dentry
->d_inode
;
3712 struct nfs4_readdir_arg args
= {
3717 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
3720 struct nfs4_readdir_res res
;
3721 struct rpc_message msg
= {
3722 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
3729 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
3730 dentry
->d_parent
->d_name
.name
,
3731 dentry
->d_name
.name
,
3732 (unsigned long long)cookie
);
3733 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
3734 res
.pgbase
= args
.pgbase
;
3735 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3737 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
3738 status
+= args
.pgbase
;
3741 nfs_invalidate_atime(dir
);
3743 dprintk("%s: returns %d\n", __func__
, status
);
3747 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3748 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3750 struct nfs4_exception exception
= { };
3753 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
3754 pages
, count
, plus
);
3755 trace_nfs4_readdir(dentry
->d_inode
, err
);
3756 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
), err
,
3758 } while (exception
.retry
);
3762 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3763 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
3765 struct nfs4_createdata
*data
;
3766 int mode
= sattr
->ia_mode
;
3767 int status
= -ENOMEM
;
3769 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
3774 data
->arg
.ftype
= NF4FIFO
;
3775 else if (S_ISBLK(mode
)) {
3776 data
->arg
.ftype
= NF4BLK
;
3777 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3778 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3780 else if (S_ISCHR(mode
)) {
3781 data
->arg
.ftype
= NF4CHR
;
3782 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3783 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3784 } else if (!S_ISSOCK(mode
)) {
3789 data
->arg
.label
= label
;
3790 status
= nfs4_do_create(dir
, dentry
, data
);
3792 nfs4_free_createdata(data
);
3797 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3798 struct iattr
*sattr
, dev_t rdev
)
3800 struct nfs4_exception exception
= { };
3801 struct nfs4_label l
, *label
= NULL
;
3804 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3806 sattr
->ia_mode
&= ~current_umask();
3808 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
3809 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
3810 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3812 } while (exception
.retry
);
3814 nfs4_label_release_security(label
);
3819 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3820 struct nfs_fsstat
*fsstat
)
3822 struct nfs4_statfs_arg args
= {
3824 .bitmask
= server
->attr_bitmask
,
3826 struct nfs4_statfs_res res
= {
3829 struct rpc_message msg
= {
3830 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
3835 nfs_fattr_init(fsstat
->fattr
);
3836 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3839 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
3841 struct nfs4_exception exception
= { };
3844 err
= nfs4_handle_exception(server
,
3845 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
3847 } while (exception
.retry
);
3851 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3852 struct nfs_fsinfo
*fsinfo
)
3854 struct nfs4_fsinfo_arg args
= {
3856 .bitmask
= server
->attr_bitmask
,
3858 struct nfs4_fsinfo_res res
= {
3861 struct rpc_message msg
= {
3862 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3867 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3870 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3872 struct nfs4_exception exception
= { };
3873 unsigned long now
= jiffies
;
3877 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3878 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
3880 struct nfs_client
*clp
= server
->nfs_client
;
3882 spin_lock(&clp
->cl_lock
);
3883 clp
->cl_lease_time
= fsinfo
->lease_time
* HZ
;
3884 clp
->cl_last_renewal
= now
;
3885 spin_unlock(&clp
->cl_lock
);
3888 err
= nfs4_handle_exception(server
, err
, &exception
);
3889 } while (exception
.retry
);
3893 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3897 nfs_fattr_init(fsinfo
->fattr
);
3898 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3900 /* block layout checks this! */
3901 server
->pnfs_blksize
= fsinfo
->blksize
;
3902 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
->layouttype
);
3908 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3909 struct nfs_pathconf
*pathconf
)
3911 struct nfs4_pathconf_arg args
= {
3913 .bitmask
= server
->attr_bitmask
,
3915 struct nfs4_pathconf_res res
= {
3916 .pathconf
= pathconf
,
3918 struct rpc_message msg
= {
3919 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3924 /* None of the pathconf attributes are mandatory to implement */
3925 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3926 memset(pathconf
, 0, sizeof(*pathconf
));
3930 nfs_fattr_init(pathconf
->fattr
);
3931 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3934 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3935 struct nfs_pathconf
*pathconf
)
3937 struct nfs4_exception exception
= { };
3941 err
= nfs4_handle_exception(server
,
3942 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3944 } while (exception
.retry
);
3948 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
3949 const struct nfs_open_context
*ctx
,
3950 const struct nfs_lock_context
*l_ctx
,
3953 const struct nfs_lockowner
*lockowner
= NULL
;
3956 lockowner
= &l_ctx
->lockowner
;
3957 return nfs4_select_rw_stateid(stateid
, ctx
->state
, fmode
, lockowner
);
3959 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
3961 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
3962 const struct nfs_open_context
*ctx
,
3963 const struct nfs_lock_context
*l_ctx
,
3966 nfs4_stateid current_stateid
;
3968 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
))
3970 return nfs4_stateid_match(stateid
, ¤t_stateid
);
3973 static bool nfs4_error_stateid_expired(int err
)
3976 case -NFS4ERR_DELEG_REVOKED
:
3977 case -NFS4ERR_ADMIN_REVOKED
:
3978 case -NFS4ERR_BAD_STATEID
:
3979 case -NFS4ERR_STALE_STATEID
:
3980 case -NFS4ERR_OLD_STATEID
:
3981 case -NFS4ERR_OPENMODE
:
3982 case -NFS4ERR_EXPIRED
:
3988 void __nfs4_read_done_cb(struct nfs_read_data
*data
)
3990 nfs_invalidate_atime(data
->header
->inode
);
3993 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_read_data
*data
)
3995 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
3997 trace_nfs4_read(data
, task
->tk_status
);
3998 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3999 rpc_restart_call_prepare(task
);
4003 __nfs4_read_done_cb(data
);
4004 if (task
->tk_status
> 0)
4005 renew_lease(server
, data
->timestamp
);
4009 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4010 struct nfs_readargs
*args
)
4013 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4014 nfs4_stateid_is_current(&args
->stateid
,
4019 rpc_restart_call_prepare(task
);
4023 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
4026 dprintk("--> %s\n", __func__
);
4028 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4030 if (nfs4_read_stateid_changed(task
, &data
->args
))
4032 return data
->read_done_cb
? data
->read_done_cb(task
, data
) :
4033 nfs4_read_done_cb(task
, data
);
4036 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
4038 data
->timestamp
= jiffies
;
4039 data
->read_done_cb
= nfs4_read_done_cb
;
4040 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4041 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
4044 static int nfs4_proc_read_rpc_prepare(struct rpc_task
*task
, struct nfs_read_data
*data
)
4046 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
4047 &data
->args
.seq_args
,
4051 if (nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
4052 data
->args
.lock_context
, FMODE_READ
) == -EIO
)
4054 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &data
->args
.context
->flags
)))
4059 static int nfs4_write_done_cb(struct rpc_task
*task
, struct nfs_write_data
*data
)
4061 struct inode
*inode
= data
->header
->inode
;
4063 trace_nfs4_write(data
, task
->tk_status
);
4064 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
4065 rpc_restart_call_prepare(task
);
4068 if (task
->tk_status
>= 0) {
4069 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
4070 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
4075 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4076 struct nfs_writeargs
*args
)
4079 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4080 nfs4_stateid_is_current(&args
->stateid
,
4085 rpc_restart_call_prepare(task
);
4089 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
4091 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4093 if (nfs4_write_stateid_changed(task
, &data
->args
))
4095 return data
->write_done_cb
? data
->write_done_cb(task
, data
) :
4096 nfs4_write_done_cb(task
, data
);
4100 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data
*data
)
4102 const struct nfs_pgio_header
*hdr
= data
->header
;
4104 /* Don't request attributes for pNFS or O_DIRECT writes */
4105 if (data
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4107 /* Otherwise, request attributes if and only if we don't hold
4110 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4113 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
4115 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
4117 if (!nfs4_write_need_cache_consistency_data(data
)) {
4118 data
->args
.bitmask
= NULL
;
4119 data
->res
.fattr
= NULL
;
4121 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
4123 if (!data
->write_done_cb
)
4124 data
->write_done_cb
= nfs4_write_done_cb
;
4125 data
->res
.server
= server
;
4126 data
->timestamp
= jiffies
;
4128 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4129 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4132 static int nfs4_proc_write_rpc_prepare(struct rpc_task
*task
, struct nfs_write_data
*data
)
4134 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
4135 &data
->args
.seq_args
,
4139 if (nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
4140 data
->args
.lock_context
, FMODE_WRITE
) == -EIO
)
4142 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &data
->args
.context
->flags
)))
4147 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4149 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4150 &data
->args
.seq_args
,
4155 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4157 struct inode
*inode
= data
->inode
;
4159 trace_nfs4_commit(data
, task
->tk_status
);
4160 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
4161 rpc_restart_call_prepare(task
);
4167 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4169 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4171 return data
->commit_done_cb(task
, data
);
4174 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4176 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4178 if (data
->commit_done_cb
== NULL
)
4179 data
->commit_done_cb
= nfs4_commit_done_cb
;
4180 data
->res
.server
= server
;
4181 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4182 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4185 struct nfs4_renewdata
{
4186 struct nfs_client
*client
;
4187 unsigned long timestamp
;
4191 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4192 * standalone procedure for queueing an asynchronous RENEW.
4194 static void nfs4_renew_release(void *calldata
)
4196 struct nfs4_renewdata
*data
= calldata
;
4197 struct nfs_client
*clp
= data
->client
;
4199 if (atomic_read(&clp
->cl_count
) > 1)
4200 nfs4_schedule_state_renewal(clp
);
4201 nfs_put_client(clp
);
4205 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4207 struct nfs4_renewdata
*data
= calldata
;
4208 struct nfs_client
*clp
= data
->client
;
4209 unsigned long timestamp
= data
->timestamp
;
4211 trace_nfs4_renew_async(clp
, task
->tk_status
);
4212 if (task
->tk_status
< 0) {
4213 /* Unless we're shutting down, schedule state recovery! */
4214 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4216 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4217 nfs4_schedule_lease_recovery(clp
);
4220 nfs4_schedule_path_down_recovery(clp
);
4222 do_renew_lease(clp
, timestamp
);
4225 static const struct rpc_call_ops nfs4_renew_ops
= {
4226 .rpc_call_done
= nfs4_renew_done
,
4227 .rpc_release
= nfs4_renew_release
,
4230 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4232 struct rpc_message msg
= {
4233 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4237 struct nfs4_renewdata
*data
;
4239 if (renew_flags
== 0)
4241 if (!atomic_inc_not_zero(&clp
->cl_count
))
4243 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4247 data
->timestamp
= jiffies
;
4248 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4249 &nfs4_renew_ops
, data
);
4252 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4254 struct rpc_message msg
= {
4255 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4259 unsigned long now
= jiffies
;
4262 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4265 do_renew_lease(clp
, now
);
4269 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4271 return (server
->caps
& NFS_CAP_ACLS
)
4272 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
4273 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
4276 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4277 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4280 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4282 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4283 struct page
**pages
, unsigned int *pgbase
)
4285 struct page
*newpage
, **spages
;
4291 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4292 newpage
= alloc_page(GFP_KERNEL
);
4294 if (newpage
== NULL
)
4296 memcpy(page_address(newpage
), buf
, len
);
4301 } while (buflen
!= 0);
4307 __free_page(spages
[rc
-1]);
4311 struct nfs4_cached_acl
{
4317 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4319 struct nfs_inode
*nfsi
= NFS_I(inode
);
4321 spin_lock(&inode
->i_lock
);
4322 kfree(nfsi
->nfs4_acl
);
4323 nfsi
->nfs4_acl
= acl
;
4324 spin_unlock(&inode
->i_lock
);
4327 static void nfs4_zap_acl_attr(struct inode
*inode
)
4329 nfs4_set_cached_acl(inode
, NULL
);
4332 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4334 struct nfs_inode
*nfsi
= NFS_I(inode
);
4335 struct nfs4_cached_acl
*acl
;
4338 spin_lock(&inode
->i_lock
);
4339 acl
= nfsi
->nfs4_acl
;
4342 if (buf
== NULL
) /* user is just asking for length */
4344 if (acl
->cached
== 0)
4346 ret
= -ERANGE
; /* see getxattr(2) man page */
4347 if (acl
->len
> buflen
)
4349 memcpy(buf
, acl
->data
, acl
->len
);
4353 spin_unlock(&inode
->i_lock
);
4357 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4359 struct nfs4_cached_acl
*acl
;
4360 size_t buflen
= sizeof(*acl
) + acl_len
;
4362 if (buflen
<= PAGE_SIZE
) {
4363 acl
= kmalloc(buflen
, GFP_KERNEL
);
4367 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4369 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4376 nfs4_set_cached_acl(inode
, acl
);
4380 * The getxattr API returns the required buffer length when called with a
4381 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4382 * the required buf. On a NULL buf, we send a page of data to the server
4383 * guessing that the ACL request can be serviced by a page. If so, we cache
4384 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4385 * the cache. If not so, we throw away the page, and cache the required
4386 * length. The next getxattr call will then produce another round trip to
4387 * the server, this time with the input buf of the required size.
4389 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4391 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4392 struct nfs_getaclargs args
= {
4393 .fh
= NFS_FH(inode
),
4397 struct nfs_getaclres res
= {
4400 struct rpc_message msg
= {
4401 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4405 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4406 int ret
= -ENOMEM
, i
;
4408 /* As long as we're doing a round trip to the server anyway,
4409 * let's be prepared for a page of acl data. */
4412 if (npages
> ARRAY_SIZE(pages
))
4415 for (i
= 0; i
< npages
; i
++) {
4416 pages
[i
] = alloc_page(GFP_KERNEL
);
4421 /* for decoding across pages */
4422 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
4423 if (!res
.acl_scratch
)
4426 args
.acl_len
= npages
* PAGE_SIZE
;
4427 args
.acl_pgbase
= 0;
4429 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4430 __func__
, buf
, buflen
, npages
, args
.acl_len
);
4431 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
4432 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4436 /* Handle the case where the passed-in buffer is too short */
4437 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
4438 /* Did the user only issue a request for the acl length? */
4444 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
4446 if (res
.acl_len
> buflen
) {
4450 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
4455 for (i
= 0; i
< npages
; i
++)
4457 __free_page(pages
[i
]);
4458 if (res
.acl_scratch
)
4459 __free_page(res
.acl_scratch
);
4463 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4465 struct nfs4_exception exception
= { };
4468 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
4469 trace_nfs4_get_acl(inode
, ret
);
4472 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
4473 } while (exception
.retry
);
4477 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
4479 struct nfs_server
*server
= NFS_SERVER(inode
);
4482 if (!nfs4_server_supports_acls(server
))
4484 ret
= nfs_revalidate_inode(server
, inode
);
4487 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
4488 nfs_zap_acl_cache(inode
);
4489 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
4491 /* -ENOENT is returned if there is no ACL or if there is an ACL
4492 * but no cached acl data, just the acl length */
4494 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
4497 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4499 struct nfs_server
*server
= NFS_SERVER(inode
);
4500 struct page
*pages
[NFS4ACL_MAXPAGES
];
4501 struct nfs_setaclargs arg
= {
4502 .fh
= NFS_FH(inode
),
4506 struct nfs_setaclres res
;
4507 struct rpc_message msg
= {
4508 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
4512 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4515 if (!nfs4_server_supports_acls(server
))
4517 if (npages
> ARRAY_SIZE(pages
))
4519 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
4522 nfs4_inode_return_delegation(inode
);
4523 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4526 * Free each page after tx, so the only ref left is
4527 * held by the network stack
4530 put_page(pages
[i
-1]);
4533 * Acl update can result in inode attribute update.
4534 * so mark the attribute cache invalid.
4536 spin_lock(&inode
->i_lock
);
4537 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
4538 spin_unlock(&inode
->i_lock
);
4539 nfs_access_zap_cache(inode
);
4540 nfs_zap_acl_cache(inode
);
4544 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4546 struct nfs4_exception exception
= { };
4549 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
4550 trace_nfs4_set_acl(inode
, err
);
4551 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4553 } while (exception
.retry
);
4557 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4558 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
4561 struct nfs_server
*server
= NFS_SERVER(inode
);
4562 struct nfs_fattr fattr
;
4563 struct nfs4_label label
= {0, 0, buflen
, buf
};
4565 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4566 struct nfs4_getattr_arg args
= {
4567 .fh
= NFS_FH(inode
),
4570 struct nfs4_getattr_res res
= {
4575 struct rpc_message msg
= {
4576 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
4582 nfs_fattr_init(&fattr
);
4584 ret
= rpc_call_sync(server
->client
, &msg
, 0);
4587 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
4589 if (buflen
< label
.len
)
4594 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
4597 struct nfs4_exception exception
= { };
4600 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4604 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
4605 trace_nfs4_get_security_label(inode
, err
);
4606 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4608 } while (exception
.retry
);
4612 static int _nfs4_do_set_security_label(struct inode
*inode
,
4613 struct nfs4_label
*ilabel
,
4614 struct nfs_fattr
*fattr
,
4615 struct nfs4_label
*olabel
)
4618 struct iattr sattr
= {0};
4619 struct nfs_server
*server
= NFS_SERVER(inode
);
4620 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4621 struct nfs_setattrargs args
= {
4622 .fh
= NFS_FH(inode
),
4628 struct nfs_setattrres res
= {
4633 struct rpc_message msg
= {
4634 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
4640 nfs4_stateid_copy(&args
.stateid
, &zero_stateid
);
4642 status
= rpc_call_sync(server
->client
, &msg
, 0);
4644 dprintk("%s failed: %d\n", __func__
, status
);
4649 static int nfs4_do_set_security_label(struct inode
*inode
,
4650 struct nfs4_label
*ilabel
,
4651 struct nfs_fattr
*fattr
,
4652 struct nfs4_label
*olabel
)
4654 struct nfs4_exception exception
= { };
4658 err
= _nfs4_do_set_security_label(inode
, ilabel
,
4660 trace_nfs4_set_security_label(inode
, err
);
4661 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4663 } while (exception
.retry
);
4668 nfs4_set_security_label(struct dentry
*dentry
, const void *buf
, size_t buflen
)
4670 struct nfs4_label ilabel
, *olabel
= NULL
;
4671 struct nfs_fattr fattr
;
4672 struct rpc_cred
*cred
;
4673 struct inode
*inode
= dentry
->d_inode
;
4676 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4679 nfs_fattr_init(&fattr
);
4683 ilabel
.label
= (char *)buf
;
4684 ilabel
.len
= buflen
;
4686 cred
= rpc_lookup_cred();
4688 return PTR_ERR(cred
);
4690 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
4691 if (IS_ERR(olabel
)) {
4692 status
= -PTR_ERR(olabel
);
4696 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
4698 nfs_setsecurity(inode
, &fattr
, olabel
);
4700 nfs4_label_free(olabel
);
4705 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4709 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
4711 struct nfs_client
*clp
= server
->nfs_client
;
4713 if (task
->tk_status
>= 0)
4715 switch(task
->tk_status
) {
4716 case -NFS4ERR_DELEG_REVOKED
:
4717 case -NFS4ERR_ADMIN_REVOKED
:
4718 case -NFS4ERR_BAD_STATEID
:
4721 nfs_remove_bad_delegation(state
->inode
);
4722 case -NFS4ERR_OPENMODE
:
4725 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4726 goto stateid_invalid
;
4727 goto wait_on_recovery
;
4728 case -NFS4ERR_EXPIRED
:
4729 if (state
!= NULL
) {
4730 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4731 goto stateid_invalid
;
4733 case -NFS4ERR_STALE_STATEID
:
4734 case -NFS4ERR_STALE_CLIENTID
:
4735 nfs4_schedule_lease_recovery(clp
);
4736 goto wait_on_recovery
;
4737 #if defined(CONFIG_NFS_V4_1)
4738 case -NFS4ERR_BADSESSION
:
4739 case -NFS4ERR_BADSLOT
:
4740 case -NFS4ERR_BAD_HIGH_SLOT
:
4741 case -NFS4ERR_DEADSESSION
:
4742 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4743 case -NFS4ERR_SEQ_FALSE_RETRY
:
4744 case -NFS4ERR_SEQ_MISORDERED
:
4745 dprintk("%s ERROR %d, Reset session\n", __func__
,
4747 nfs4_schedule_session_recovery(clp
->cl_session
, task
->tk_status
);
4748 task
->tk_status
= 0;
4750 #endif /* CONFIG_NFS_V4_1 */
4751 case -NFS4ERR_DELAY
:
4752 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
4753 case -NFS4ERR_GRACE
:
4754 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
4755 task
->tk_status
= 0;
4757 case -NFS4ERR_RETRY_UNCACHED_REP
:
4758 case -NFS4ERR_OLD_STATEID
:
4759 task
->tk_status
= 0;
4762 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
4765 task
->tk_status
= -EIO
;
4768 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
4769 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
4770 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
4771 task
->tk_status
= 0;
4775 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
4776 nfs4_verifier
*bootverf
)
4780 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
4781 /* An impossible timestamp guarantees this value
4782 * will never match a generated boot time. */
4784 verf
[1] = cpu_to_be32(NSEC_PER_SEC
+ 1);
4786 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
4787 verf
[0] = cpu_to_be32(nn
->boot_time
.tv_sec
);
4788 verf
[1] = cpu_to_be32(nn
->boot_time
.tv_nsec
);
4790 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
4794 nfs4_init_nonuniform_client_string(const struct nfs_client
*clp
,
4795 char *buf
, size_t len
)
4797 unsigned int result
;
4800 result
= scnprintf(buf
, len
, "Linux NFSv4.0 %s/%s %s",
4802 rpc_peeraddr2str(clp
->cl_rpcclient
,
4804 rpc_peeraddr2str(clp
->cl_rpcclient
,
4805 RPC_DISPLAY_PROTO
));
4811 nfs4_init_uniform_client_string(const struct nfs_client
*clp
,
4812 char *buf
, size_t len
)
4814 const char *nodename
= clp
->cl_rpcclient
->cl_nodename
;
4816 if (nfs4_client_id_uniquifier
[0] != '\0')
4817 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s/%s",
4818 clp
->rpc_ops
->version
,
4819 clp
->cl_minorversion
,
4820 nfs4_client_id_uniquifier
,
4822 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s",
4823 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
4828 * nfs4_proc_setclientid - Negotiate client ID
4829 * @clp: state data structure
4830 * @program: RPC program for NFSv4 callback service
4831 * @port: IP port number for NFS4 callback service
4832 * @cred: RPC credential to use for this call
4833 * @res: where to place the result
4835 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4837 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
4838 unsigned short port
, struct rpc_cred
*cred
,
4839 struct nfs4_setclientid_res
*res
)
4841 nfs4_verifier sc_verifier
;
4842 struct nfs4_setclientid setclientid
= {
4843 .sc_verifier
= &sc_verifier
,
4845 .sc_cb_ident
= clp
->cl_cb_ident
,
4847 struct rpc_message msg
= {
4848 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
4849 .rpc_argp
= &setclientid
,
4855 /* nfs_client_id4 */
4856 nfs4_init_boot_verifier(clp
, &sc_verifier
);
4857 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
4858 setclientid
.sc_name_len
=
4859 nfs4_init_uniform_client_string(clp
,
4860 setclientid
.sc_name
,
4861 sizeof(setclientid
.sc_name
));
4863 setclientid
.sc_name_len
=
4864 nfs4_init_nonuniform_client_string(clp
,
4865 setclientid
.sc_name
,
4866 sizeof(setclientid
.sc_name
));
4869 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
4870 sizeof(setclientid
.sc_netid
), "%s",
4871 rpc_peeraddr2str(clp
->cl_rpcclient
,
4872 RPC_DISPLAY_NETID
));
4874 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
4875 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
4876 clp
->cl_ipaddr
, port
>> 8, port
& 255);
4878 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4879 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4880 setclientid
.sc_name_len
, setclientid
.sc_name
);
4881 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4882 trace_nfs4_setclientid(clp
, status
);
4883 dprintk("NFS reply setclientid: %d\n", status
);
4888 * nfs4_proc_setclientid_confirm - Confirm client ID
4889 * @clp: state data structure
4890 * @res: result of a previous SETCLIENTID
4891 * @cred: RPC credential to use for this call
4893 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4895 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
4896 struct nfs4_setclientid_res
*arg
,
4897 struct rpc_cred
*cred
)
4899 struct rpc_message msg
= {
4900 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
4906 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4907 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4909 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4910 trace_nfs4_setclientid_confirm(clp
, status
);
4911 dprintk("NFS reply setclientid_confirm: %d\n", status
);
4915 struct nfs4_delegreturndata
{
4916 struct nfs4_delegreturnargs args
;
4917 struct nfs4_delegreturnres res
;
4919 nfs4_stateid stateid
;
4920 unsigned long timestamp
;
4921 struct nfs_fattr fattr
;
4925 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
4927 struct nfs4_delegreturndata
*data
= calldata
;
4929 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4932 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
4933 switch (task
->tk_status
) {
4934 case -NFS4ERR_STALE_STATEID
:
4935 case -NFS4ERR_EXPIRED
:
4937 renew_lease(data
->res
.server
, data
->timestamp
);
4940 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
4942 rpc_restart_call_prepare(task
);
4946 data
->rpc_status
= task
->tk_status
;
4949 static void nfs4_delegreturn_release(void *calldata
)
4954 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
4956 struct nfs4_delegreturndata
*d_data
;
4958 d_data
= (struct nfs4_delegreturndata
*)data
;
4960 nfs4_setup_sequence(d_data
->res
.server
,
4961 &d_data
->args
.seq_args
,
4962 &d_data
->res
.seq_res
,
4966 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
4967 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
4968 .rpc_call_done
= nfs4_delegreturn_done
,
4969 .rpc_release
= nfs4_delegreturn_release
,
4972 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
4974 struct nfs4_delegreturndata
*data
;
4975 struct nfs_server
*server
= NFS_SERVER(inode
);
4976 struct rpc_task
*task
;
4977 struct rpc_message msg
= {
4978 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
4981 struct rpc_task_setup task_setup_data
= {
4982 .rpc_client
= server
->client
,
4983 .rpc_message
= &msg
,
4984 .callback_ops
= &nfs4_delegreturn_ops
,
4985 .flags
= RPC_TASK_ASYNC
,
4989 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
4992 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4993 data
->args
.fhandle
= &data
->fh
;
4994 data
->args
.stateid
= &data
->stateid
;
4995 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
4996 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
4997 nfs4_stateid_copy(&data
->stateid
, stateid
);
4998 data
->res
.fattr
= &data
->fattr
;
4999 data
->res
.server
= server
;
5000 nfs_fattr_init(data
->res
.fattr
);
5001 data
->timestamp
= jiffies
;
5002 data
->rpc_status
= 0;
5004 task_setup_data
.callback_data
= data
;
5005 msg
.rpc_argp
= &data
->args
;
5006 msg
.rpc_resp
= &data
->res
;
5007 task
= rpc_run_task(&task_setup_data
);
5009 return PTR_ERR(task
);
5012 status
= nfs4_wait_for_completion_rpc_task(task
);
5015 status
= data
->rpc_status
;
5017 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5019 nfs_refresh_inode(inode
, &data
->fattr
);
5025 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5027 struct nfs_server
*server
= NFS_SERVER(inode
);
5028 struct nfs4_exception exception
= { };
5031 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5032 trace_nfs4_delegreturn(inode
, err
);
5034 case -NFS4ERR_STALE_STATEID
:
5035 case -NFS4ERR_EXPIRED
:
5039 err
= nfs4_handle_exception(server
, err
, &exception
);
5040 } while (exception
.retry
);
5044 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5045 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5048 * sleep, with exponential backoff, and retry the LOCK operation.
5050 static unsigned long
5051 nfs4_set_lock_task_retry(unsigned long timeout
)
5053 freezable_schedule_timeout_killable_unsafe(timeout
);
5055 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
5056 return NFS4_LOCK_MAXTIMEOUT
;
5060 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5062 struct inode
*inode
= state
->inode
;
5063 struct nfs_server
*server
= NFS_SERVER(inode
);
5064 struct nfs_client
*clp
= server
->nfs_client
;
5065 struct nfs_lockt_args arg
= {
5066 .fh
= NFS_FH(inode
),
5069 struct nfs_lockt_res res
= {
5072 struct rpc_message msg
= {
5073 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5076 .rpc_cred
= state
->owner
->so_cred
,
5078 struct nfs4_lock_state
*lsp
;
5081 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5082 status
= nfs4_set_lock_state(state
, request
);
5085 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5086 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5087 arg
.lock_owner
.s_dev
= server
->s_dev
;
5088 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5091 request
->fl_type
= F_UNLCK
;
5093 case -NFS4ERR_DENIED
:
5096 request
->fl_ops
->fl_release_private(request
);
5101 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5103 struct nfs4_exception exception
= { };
5107 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5108 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5109 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5111 } while (exception
.retry
);
5115 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
5118 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
5120 res
= posix_lock_file_wait(file
, fl
);
5123 res
= flock_lock_file_wait(file
, fl
);
5131 struct nfs4_unlockdata
{
5132 struct nfs_locku_args arg
;
5133 struct nfs_locku_res res
;
5134 struct nfs4_lock_state
*lsp
;
5135 struct nfs_open_context
*ctx
;
5136 struct file_lock fl
;
5137 const struct nfs_server
*server
;
5138 unsigned long timestamp
;
5141 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5142 struct nfs_open_context
*ctx
,
5143 struct nfs4_lock_state
*lsp
,
5144 struct nfs_seqid
*seqid
)
5146 struct nfs4_unlockdata
*p
;
5147 struct inode
*inode
= lsp
->ls_state
->inode
;
5149 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5152 p
->arg
.fh
= NFS_FH(inode
);
5154 p
->arg
.seqid
= seqid
;
5155 p
->res
.seqid
= seqid
;
5156 p
->arg
.stateid
= &lsp
->ls_stateid
;
5158 atomic_inc(&lsp
->ls_count
);
5159 /* Ensure we don't close file until we're done freeing locks! */
5160 p
->ctx
= get_nfs_open_context(ctx
);
5161 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5162 p
->server
= NFS_SERVER(inode
);
5166 static void nfs4_locku_release_calldata(void *data
)
5168 struct nfs4_unlockdata
*calldata
= data
;
5169 nfs_free_seqid(calldata
->arg
.seqid
);
5170 nfs4_put_lock_state(calldata
->lsp
);
5171 put_nfs_open_context(calldata
->ctx
);
5175 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5177 struct nfs4_unlockdata
*calldata
= data
;
5179 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5181 switch (task
->tk_status
) {
5183 nfs4_stateid_copy(&calldata
->lsp
->ls_stateid
,
5184 &calldata
->res
.stateid
);
5185 renew_lease(calldata
->server
, calldata
->timestamp
);
5187 case -NFS4ERR_BAD_STATEID
:
5188 case -NFS4ERR_OLD_STATEID
:
5189 case -NFS4ERR_STALE_STATEID
:
5190 case -NFS4ERR_EXPIRED
:
5193 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
5194 rpc_restart_call_prepare(task
);
5196 nfs_release_seqid(calldata
->arg
.seqid
);
5199 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5201 struct nfs4_unlockdata
*calldata
= data
;
5203 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5205 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5206 /* Note: exit _without_ running nfs4_locku_done */
5209 calldata
->timestamp
= jiffies
;
5210 if (nfs4_setup_sequence(calldata
->server
,
5211 &calldata
->arg
.seq_args
,
5212 &calldata
->res
.seq_res
,
5214 nfs_release_seqid(calldata
->arg
.seqid
);
5217 task
->tk_action
= NULL
;
5219 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5222 static const struct rpc_call_ops nfs4_locku_ops
= {
5223 .rpc_call_prepare
= nfs4_locku_prepare
,
5224 .rpc_call_done
= nfs4_locku_done
,
5225 .rpc_release
= nfs4_locku_release_calldata
,
5228 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5229 struct nfs_open_context
*ctx
,
5230 struct nfs4_lock_state
*lsp
,
5231 struct nfs_seqid
*seqid
)
5233 struct nfs4_unlockdata
*data
;
5234 struct rpc_message msg
= {
5235 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5236 .rpc_cred
= ctx
->cred
,
5238 struct rpc_task_setup task_setup_data
= {
5239 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5240 .rpc_message
= &msg
,
5241 .callback_ops
= &nfs4_locku_ops
,
5242 .workqueue
= nfsiod_workqueue
,
5243 .flags
= RPC_TASK_ASYNC
,
5246 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5247 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5249 /* Ensure this is an unlock - when canceling a lock, the
5250 * canceled lock is passed in, and it won't be an unlock.
5252 fl
->fl_type
= F_UNLCK
;
5254 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5256 nfs_free_seqid(seqid
);
5257 return ERR_PTR(-ENOMEM
);
5260 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5261 msg
.rpc_argp
= &data
->arg
;
5262 msg
.rpc_resp
= &data
->res
;
5263 task_setup_data
.callback_data
= data
;
5264 return rpc_run_task(&task_setup_data
);
5267 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5269 struct inode
*inode
= state
->inode
;
5270 struct nfs4_state_owner
*sp
= state
->owner
;
5271 struct nfs_inode
*nfsi
= NFS_I(inode
);
5272 struct nfs_seqid
*seqid
;
5273 struct nfs4_lock_state
*lsp
;
5274 struct rpc_task
*task
;
5276 unsigned char fl_flags
= request
->fl_flags
;
5278 status
= nfs4_set_lock_state(state
, request
);
5279 /* Unlock _before_ we do the RPC call */
5280 request
->fl_flags
|= FL_EXISTS
;
5281 /* Exclude nfs_delegation_claim_locks() */
5282 mutex_lock(&sp
->so_delegreturn_mutex
);
5283 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5284 down_read(&nfsi
->rwsem
);
5285 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
5286 up_read(&nfsi
->rwsem
);
5287 mutex_unlock(&sp
->so_delegreturn_mutex
);
5290 up_read(&nfsi
->rwsem
);
5291 mutex_unlock(&sp
->so_delegreturn_mutex
);
5294 /* Is this a delegated lock? */
5295 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5296 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5298 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5302 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5303 status
= PTR_ERR(task
);
5306 status
= nfs4_wait_for_completion_rpc_task(task
);
5309 request
->fl_flags
= fl_flags
;
5310 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
5314 struct nfs4_lockdata
{
5315 struct nfs_lock_args arg
;
5316 struct nfs_lock_res res
;
5317 struct nfs4_lock_state
*lsp
;
5318 struct nfs_open_context
*ctx
;
5319 struct file_lock fl
;
5320 unsigned long timestamp
;
5323 struct nfs_server
*server
;
5326 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
5327 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
5330 struct nfs4_lockdata
*p
;
5331 struct inode
*inode
= lsp
->ls_state
->inode
;
5332 struct nfs_server
*server
= NFS_SERVER(inode
);
5334 p
= kzalloc(sizeof(*p
), gfp_mask
);
5338 p
->arg
.fh
= NFS_FH(inode
);
5340 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
5341 if (p
->arg
.open_seqid
== NULL
)
5343 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
5344 if (p
->arg
.lock_seqid
== NULL
)
5345 goto out_free_seqid
;
5346 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
5347 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5348 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5349 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
5350 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
5353 atomic_inc(&lsp
->ls_count
);
5354 p
->ctx
= get_nfs_open_context(ctx
);
5355 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5358 nfs_free_seqid(p
->arg
.open_seqid
);
5364 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
5366 struct nfs4_lockdata
*data
= calldata
;
5367 struct nfs4_state
*state
= data
->lsp
->ls_state
;
5369 dprintk("%s: begin!\n", __func__
);
5370 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
5372 /* Do we need to do an open_to_lock_owner? */
5373 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
5374 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
5375 goto out_release_lock_seqid
;
5377 data
->arg
.open_stateid
= &state
->open_stateid
;
5378 data
->arg
.new_lock_owner
= 1;
5379 data
->res
.open_seqid
= data
->arg
.open_seqid
;
5381 data
->arg
.new_lock_owner
= 0;
5382 if (!nfs4_valid_open_stateid(state
)) {
5383 data
->rpc_status
= -EBADF
;
5384 task
->tk_action
= NULL
;
5385 goto out_release_open_seqid
;
5387 data
->timestamp
= jiffies
;
5388 if (nfs4_setup_sequence(data
->server
,
5389 &data
->arg
.seq_args
,
5393 out_release_open_seqid
:
5394 nfs_release_seqid(data
->arg
.open_seqid
);
5395 out_release_lock_seqid
:
5396 nfs_release_seqid(data
->arg
.lock_seqid
);
5398 nfs4_sequence_done(task
, &data
->res
.seq_res
);
5399 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
5402 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
5404 struct nfs4_lockdata
*data
= calldata
;
5406 dprintk("%s: begin!\n", __func__
);
5408 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5411 data
->rpc_status
= task
->tk_status
;
5412 if (data
->arg
.new_lock_owner
!= 0) {
5413 if (data
->rpc_status
== 0)
5414 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
5418 if (data
->rpc_status
== 0) {
5419 nfs4_stateid_copy(&data
->lsp
->ls_stateid
, &data
->res
.stateid
);
5420 set_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
);
5421 renew_lease(NFS_SERVER(data
->ctx
->dentry
->d_inode
), data
->timestamp
);
5424 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
5427 static void nfs4_lock_release(void *calldata
)
5429 struct nfs4_lockdata
*data
= calldata
;
5431 dprintk("%s: begin!\n", __func__
);
5432 nfs_free_seqid(data
->arg
.open_seqid
);
5433 if (data
->cancelled
!= 0) {
5434 struct rpc_task
*task
;
5435 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
5436 data
->arg
.lock_seqid
);
5438 rpc_put_task_async(task
);
5439 dprintk("%s: cancelling lock!\n", __func__
);
5441 nfs_free_seqid(data
->arg
.lock_seqid
);
5442 nfs4_put_lock_state(data
->lsp
);
5443 put_nfs_open_context(data
->ctx
);
5445 dprintk("%s: done!\n", __func__
);
5448 static const struct rpc_call_ops nfs4_lock_ops
= {
5449 .rpc_call_prepare
= nfs4_lock_prepare
,
5450 .rpc_call_done
= nfs4_lock_done
,
5451 .rpc_release
= nfs4_lock_release
,
5454 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
5457 case -NFS4ERR_ADMIN_REVOKED
:
5458 case -NFS4ERR_BAD_STATEID
:
5459 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5460 if (new_lock_owner
!= 0 ||
5461 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
5462 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
5464 case -NFS4ERR_STALE_STATEID
:
5465 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5466 case -NFS4ERR_EXPIRED
:
5467 nfs4_schedule_lease_recovery(server
->nfs_client
);
5471 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
5473 struct nfs4_lockdata
*data
;
5474 struct rpc_task
*task
;
5475 struct rpc_message msg
= {
5476 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
5477 .rpc_cred
= state
->owner
->so_cred
,
5479 struct rpc_task_setup task_setup_data
= {
5480 .rpc_client
= NFS_CLIENT(state
->inode
),
5481 .rpc_message
= &msg
,
5482 .callback_ops
= &nfs4_lock_ops
,
5483 .workqueue
= nfsiod_workqueue
,
5484 .flags
= RPC_TASK_ASYNC
,
5488 dprintk("%s: begin!\n", __func__
);
5489 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
5490 fl
->fl_u
.nfs4_fl
.owner
,
5491 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
5495 data
->arg
.block
= 1;
5496 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5497 msg
.rpc_argp
= &data
->arg
;
5498 msg
.rpc_resp
= &data
->res
;
5499 task_setup_data
.callback_data
= data
;
5500 if (recovery_type
> NFS_LOCK_NEW
) {
5501 if (recovery_type
== NFS_LOCK_RECLAIM
)
5502 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
5503 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
5505 task
= rpc_run_task(&task_setup_data
);
5507 return PTR_ERR(task
);
5508 ret
= nfs4_wait_for_completion_rpc_task(task
);
5510 ret
= data
->rpc_status
;
5512 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
5513 data
->arg
.new_lock_owner
, ret
);
5515 data
->cancelled
= 1;
5517 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
5521 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
5523 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5524 struct nfs4_exception exception
= {
5525 .inode
= state
->inode
,
5530 /* Cache the lock if possible... */
5531 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5533 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
5534 trace_nfs4_lock_reclaim(request
, state
, F_SETLK
, err
);
5535 if (err
!= -NFS4ERR_DELAY
)
5537 nfs4_handle_exception(server
, err
, &exception
);
5538 } while (exception
.retry
);
5542 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5544 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5545 struct nfs4_exception exception
= {
5546 .inode
= state
->inode
,
5550 err
= nfs4_set_lock_state(state
, request
);
5553 if (!recover_lost_locks
) {
5554 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
5558 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5560 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
5561 trace_nfs4_lock_expired(request
, state
, F_SETLK
, err
);
5565 case -NFS4ERR_GRACE
:
5566 case -NFS4ERR_DELAY
:
5567 nfs4_handle_exception(server
, err
, &exception
);
5570 } while (exception
.retry
);
5575 #if defined(CONFIG_NFS_V4_1)
5577 * nfs41_check_expired_locks - possibly free a lock stateid
5579 * @state: NFSv4 state for an inode
5581 * Returns NFS_OK if recovery for this stateid is now finished.
5582 * Otherwise a negative NFS4ERR value is returned.
5584 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
5586 int status
, ret
= -NFS4ERR_BAD_STATEID
;
5587 struct nfs4_lock_state
*lsp
;
5588 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5590 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
5591 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
5592 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
5594 status
= nfs41_test_stateid(server
,
5597 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
5598 if (status
!= NFS_OK
) {
5599 /* Free the stateid unless the server
5600 * informs us the stateid is unrecognized. */
5601 if (status
!= -NFS4ERR_BAD_STATEID
)
5602 nfs41_free_stateid(server
,
5605 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
5614 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5616 int status
= NFS_OK
;
5618 if (test_bit(LK_STATE_IN_USE
, &state
->flags
))
5619 status
= nfs41_check_expired_locks(state
);
5620 if (status
!= NFS_OK
)
5621 status
= nfs4_lock_expired(state
, request
);
5626 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5628 struct nfs4_state_owner
*sp
= state
->owner
;
5629 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
5630 unsigned char fl_flags
= request
->fl_flags
;
5632 int status
= -ENOLCK
;
5634 if ((fl_flags
& FL_POSIX
) &&
5635 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
5637 /* Is this a delegated open? */
5638 status
= nfs4_set_lock_state(state
, request
);
5641 request
->fl_flags
|= FL_ACCESS
;
5642 status
= do_vfs_lock(request
->fl_file
, request
);
5645 down_read(&nfsi
->rwsem
);
5646 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
5647 /* Yes: cache locks! */
5648 /* ...but avoid races with delegation recall... */
5649 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
5650 status
= do_vfs_lock(request
->fl_file
, request
);
5653 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
5654 up_read(&nfsi
->rwsem
);
5655 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
5658 down_read(&nfsi
->rwsem
);
5659 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
)) {
5660 status
= -NFS4ERR_DELAY
;
5663 /* Note: we always want to sleep here! */
5664 request
->fl_flags
= fl_flags
| FL_SLEEP
;
5665 if (do_vfs_lock(request
->fl_file
, request
) < 0)
5666 printk(KERN_WARNING
"NFS: %s: VFS is out of sync with lock "
5667 "manager!\n", __func__
);
5669 up_read(&nfsi
->rwsem
);
5671 request
->fl_flags
= fl_flags
;
5675 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5677 struct nfs4_exception exception
= {
5679 .inode
= state
->inode
,
5684 err
= _nfs4_proc_setlk(state
, cmd
, request
);
5685 trace_nfs4_set_lock(request
, state
, cmd
, err
);
5686 if (err
== -NFS4ERR_DENIED
)
5688 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
5690 } while (exception
.retry
);
5695 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
5697 struct nfs_open_context
*ctx
;
5698 struct nfs4_state
*state
;
5699 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
5702 /* verify open state */
5703 ctx
= nfs_file_open_context(filp
);
5706 if (request
->fl_start
< 0 || request
->fl_end
< 0)
5709 if (IS_GETLK(cmd
)) {
5711 return nfs4_proc_getlk(state
, F_GETLK
, request
);
5715 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
5718 if (request
->fl_type
== F_UNLCK
) {
5720 return nfs4_proc_unlck(state
, cmd
, request
);
5727 * Don't rely on the VFS having checked the file open mode,
5728 * since it won't do this for flock() locks.
5730 switch (request
->fl_type
) {
5732 if (!(filp
->f_mode
& FMODE_READ
))
5736 if (!(filp
->f_mode
& FMODE_WRITE
))
5741 status
= nfs4_proc_setlk(state
, cmd
, request
);
5742 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
5744 timeout
= nfs4_set_lock_task_retry(timeout
);
5745 status
= -ERESTARTSYS
;
5748 } while(status
< 0);
5752 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
5754 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5757 err
= nfs4_set_lock_state(state
, fl
);
5760 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
5761 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
5764 struct nfs_release_lockowner_data
{
5765 struct nfs4_lock_state
*lsp
;
5766 struct nfs_server
*server
;
5767 struct nfs_release_lockowner_args args
;
5768 struct nfs4_sequence_args seq_args
;
5769 struct nfs4_sequence_res seq_res
;
5772 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
5774 struct nfs_release_lockowner_data
*data
= calldata
;
5775 nfs40_setup_sequence(data
->server
,
5776 &data
->seq_args
, &data
->seq_res
, task
);
5779 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
5781 struct nfs_release_lockowner_data
*data
= calldata
;
5782 nfs40_sequence_done(task
, &data
->seq_res
);
5785 static void nfs4_release_lockowner_release(void *calldata
)
5787 struct nfs_release_lockowner_data
*data
= calldata
;
5788 nfs4_free_lock_state(data
->server
, data
->lsp
);
5792 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
5793 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
5794 .rpc_call_done
= nfs4_release_lockowner_done
,
5795 .rpc_release
= nfs4_release_lockowner_release
,
5798 static int nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
5800 struct nfs_release_lockowner_data
*data
;
5801 struct rpc_message msg
= {
5802 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
5805 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
5808 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
5811 nfs4_init_sequence(&data
->seq_args
, &data
->seq_res
, 0);
5813 data
->server
= server
;
5814 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5815 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5816 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
5818 msg
.rpc_argp
= &data
->args
;
5819 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
5823 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5825 static int nfs4_xattr_set_nfs4_acl(struct dentry
*dentry
, const char *key
,
5826 const void *buf
, size_t buflen
,
5827 int flags
, int type
)
5829 if (strcmp(key
, "") != 0)
5832 return nfs4_proc_set_acl(dentry
->d_inode
, buf
, buflen
);
5835 static int nfs4_xattr_get_nfs4_acl(struct dentry
*dentry
, const char *key
,
5836 void *buf
, size_t buflen
, int type
)
5838 if (strcmp(key
, "") != 0)
5841 return nfs4_proc_get_acl(dentry
->d_inode
, buf
, buflen
);
5844 static size_t nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
, char *list
,
5845 size_t list_len
, const char *name
,
5846 size_t name_len
, int type
)
5848 size_t len
= sizeof(XATTR_NAME_NFSV4_ACL
);
5850 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
5853 if (list
&& len
<= list_len
)
5854 memcpy(list
, XATTR_NAME_NFSV4_ACL
, len
);
5858 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5859 static inline int nfs4_server_supports_labels(struct nfs_server
*server
)
5861 return server
->caps
& NFS_CAP_SECURITY_LABEL
;
5864 static int nfs4_xattr_set_nfs4_label(struct dentry
*dentry
, const char *key
,
5865 const void *buf
, size_t buflen
,
5866 int flags
, int type
)
5868 if (security_ismaclabel(key
))
5869 return nfs4_set_security_label(dentry
, buf
, buflen
);
5874 static int nfs4_xattr_get_nfs4_label(struct dentry
*dentry
, const char *key
,
5875 void *buf
, size_t buflen
, int type
)
5877 if (security_ismaclabel(key
))
5878 return nfs4_get_security_label(dentry
->d_inode
, buf
, buflen
);
5882 static size_t nfs4_xattr_list_nfs4_label(struct dentry
*dentry
, char *list
,
5883 size_t list_len
, const char *name
,
5884 size_t name_len
, int type
)
5888 if (nfs_server_capable(dentry
->d_inode
, NFS_CAP_SECURITY_LABEL
)) {
5889 len
= security_inode_listsecurity(dentry
->d_inode
, NULL
, 0);
5890 if (list
&& len
<= list_len
)
5891 security_inode_listsecurity(dentry
->d_inode
, list
, len
);
5896 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
5897 .prefix
= XATTR_SECURITY_PREFIX
,
5898 .list
= nfs4_xattr_list_nfs4_label
,
5899 .get
= nfs4_xattr_get_nfs4_label
,
5900 .set
= nfs4_xattr_set_nfs4_label
,
5906 * nfs_fhget will use either the mounted_on_fileid or the fileid
5908 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
5910 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
5911 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
5912 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
5913 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
5916 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
5917 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
5918 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
5922 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5923 const struct qstr
*name
,
5924 struct nfs4_fs_locations
*fs_locations
,
5927 struct nfs_server
*server
= NFS_SERVER(dir
);
5929 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
5931 struct nfs4_fs_locations_arg args
= {
5932 .dir_fh
= NFS_FH(dir
),
5937 struct nfs4_fs_locations_res res
= {
5938 .fs_locations
= fs_locations
,
5940 struct rpc_message msg
= {
5941 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
5947 dprintk("%s: start\n", __func__
);
5949 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5950 * is not supported */
5951 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
5952 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
5954 bitmask
[0] |= FATTR4_WORD0_FILEID
;
5956 nfs_fattr_init(&fs_locations
->fattr
);
5957 fs_locations
->server
= server
;
5958 fs_locations
->nlocations
= 0;
5959 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5960 dprintk("%s: returned status = %d\n", __func__
, status
);
5964 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5965 const struct qstr
*name
,
5966 struct nfs4_fs_locations
*fs_locations
,
5969 struct nfs4_exception exception
= { };
5972 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
5973 fs_locations
, page
);
5974 trace_nfs4_get_fs_locations(dir
, name
, err
);
5975 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
5977 } while (exception
.retry
);
5982 * If 'use_integrity' is true and the state managment nfs_client
5983 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5984 * and the machine credential as per RFC3530bis and RFC5661 Security
5985 * Considerations sections. Otherwise, just use the user cred with the
5986 * filesystem's rpc_client.
5988 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
5991 struct nfs4_secinfo_arg args
= {
5992 .dir_fh
= NFS_FH(dir
),
5995 struct nfs4_secinfo_res res
= {
5998 struct rpc_message msg
= {
5999 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
6003 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
6005 if (use_integrity
) {
6006 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
6007 msg
.rpc_cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
6010 dprintk("NFS call secinfo %s\n", name
->name
);
6012 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
6013 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
6015 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
6017 dprintk("NFS reply secinfo: %d\n", status
);
6020 put_rpccred(msg
.rpc_cred
);
6025 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
6026 struct nfs4_secinfo_flavors
*flavors
)
6028 struct nfs4_exception exception
= { };
6031 err
= -NFS4ERR_WRONGSEC
;
6033 /* try to use integrity protection with machine cred */
6034 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
6035 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
6038 * if unable to use integrity protection, or SECINFO with
6039 * integrity protection returns NFS4ERR_WRONGSEC (which is
6040 * disallowed by spec, but exists in deployed servers) use
6041 * the current filesystem's rpc_client and the user cred.
6043 if (err
== -NFS4ERR_WRONGSEC
)
6044 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
6046 trace_nfs4_secinfo(dir
, name
, err
);
6047 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6049 } while (exception
.retry
);
6053 #ifdef CONFIG_NFS_V4_1
6055 * Check the exchange flags returned by the server for invalid flags, having
6056 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6059 static int nfs4_check_cl_exchange_flags(u32 flags
)
6061 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
6063 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
6064 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
6066 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
6070 return -NFS4ERR_INVAL
;
6074 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
6075 struct nfs41_server_scope
*b
)
6077 if (a
->server_scope_sz
== b
->server_scope_sz
&&
6078 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
6085 * nfs4_proc_bind_conn_to_session()
6087 * The 4.1 client currently uses the same TCP connection for the
6088 * fore and backchannel.
6090 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6093 struct nfs41_bind_conn_to_session_res res
;
6094 struct rpc_message msg
= {
6096 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
6102 dprintk("--> %s\n", __func__
);
6104 res
.session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
6105 if (unlikely(res
.session
== NULL
)) {
6110 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6111 trace_nfs4_bind_conn_to_session(clp
, status
);
6113 if (memcmp(res
.session
->sess_id
.data
,
6114 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
6115 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
6119 if (res
.dir
!= NFS4_CDFS4_BOTH
) {
6120 dprintk("NFS: %s: Unexpected direction from server\n",
6125 if (res
.use_conn_in_rdma_mode
) {
6126 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6135 dprintk("<-- %s status= %d\n", __func__
, status
);
6140 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6141 * and operations we'd like to see to enable certain features in the allow map
6143 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
6144 .how
= SP4_MACH_CRED
,
6145 .enforce
.u
.words
= {
6146 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6147 1 << (OP_EXCHANGE_ID
- 32) |
6148 1 << (OP_CREATE_SESSION
- 32) |
6149 1 << (OP_DESTROY_SESSION
- 32) |
6150 1 << (OP_DESTROY_CLIENTID
- 32)
6153 [0] = 1 << (OP_CLOSE
) |
6155 [1] = 1 << (OP_SECINFO
- 32) |
6156 1 << (OP_SECINFO_NO_NAME
- 32) |
6157 1 << (OP_TEST_STATEID
- 32) |
6158 1 << (OP_FREE_STATEID
- 32)
6163 * Select the state protection mode for client `clp' given the server results
6164 * from exchange_id in `sp'.
6166 * Returns 0 on success, negative errno otherwise.
6168 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
6169 struct nfs41_state_protection
*sp
)
6171 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
6172 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6173 1 << (OP_EXCHANGE_ID
- 32) |
6174 1 << (OP_CREATE_SESSION
- 32) |
6175 1 << (OP_DESTROY_SESSION
- 32) |
6176 1 << (OP_DESTROY_CLIENTID
- 32)
6180 if (sp
->how
== SP4_MACH_CRED
) {
6181 /* Print state protect result */
6182 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
6183 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
6184 if (test_bit(i
, sp
->enforce
.u
.longs
))
6185 dfprintk(MOUNT
, " enforce op %d\n", i
);
6186 if (test_bit(i
, sp
->allow
.u
.longs
))
6187 dfprintk(MOUNT
, " allow op %d\n", i
);
6190 /* make sure nothing is on enforce list that isn't supported */
6191 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
6192 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
6193 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6199 * Minimal mode - state operations are allowed to use machine
6200 * credential. Note this already happens by default, so the
6201 * client doesn't have to do anything more than the negotiation.
6203 * NOTE: we don't care if EXCHANGE_ID is in the list -
6204 * we're already using the machine cred for exchange_id
6205 * and will never use a different cred.
6207 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
6208 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
6209 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
6210 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
6211 dfprintk(MOUNT
, "sp4_mach_cred:\n");
6212 dfprintk(MOUNT
, " minimal mode enabled\n");
6213 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
6215 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6219 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
6220 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
6221 dfprintk(MOUNT
, " cleanup mode enabled\n");
6222 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
6225 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
6226 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
6227 dfprintk(MOUNT
, " secinfo mode enabled\n");
6228 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
6231 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
6232 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
6233 dfprintk(MOUNT
, " stateid mode enabled\n");
6234 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
6237 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
6238 dfprintk(MOUNT
, " write mode enabled\n");
6239 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
6242 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
6243 dfprintk(MOUNT
, " commit mode enabled\n");
6244 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
6252 * _nfs4_proc_exchange_id()
6254 * Wrapper for EXCHANGE_ID operation.
6256 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
6259 nfs4_verifier verifier
;
6260 struct nfs41_exchange_id_args args
= {
6261 .verifier
= &verifier
,
6263 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
6264 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
6266 struct nfs41_exchange_id_res res
= {
6270 struct rpc_message msg
= {
6271 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
6277 nfs4_init_boot_verifier(clp
, &verifier
);
6278 args
.id_len
= nfs4_init_uniform_client_string(clp
, args
.id
,
6280 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6281 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
6282 args
.id_len
, args
.id
);
6284 res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
6286 if (unlikely(res
.server_owner
== NULL
)) {
6291 res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
6293 if (unlikely(res
.server_scope
== NULL
)) {
6295 goto out_server_owner
;
6298 res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
6299 if (unlikely(res
.impl_id
== NULL
)) {
6301 goto out_server_scope
;
6306 args
.state_protect
.how
= SP4_NONE
;
6310 args
.state_protect
= nfs4_sp4_mach_cred_request
;
6317 goto out_server_scope
;
6320 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6321 trace_nfs4_exchange_id(clp
, status
);
6323 status
= nfs4_check_cl_exchange_flags(res
.flags
);
6326 status
= nfs4_sp4_select_mode(clp
, &res
.state_protect
);
6329 clp
->cl_clientid
= res
.clientid
;
6330 clp
->cl_exchange_flags
= (res
.flags
& ~EXCHGID4_FLAG_CONFIRMED_R
);
6331 if (!(res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
))
6332 clp
->cl_seqid
= res
.seqid
;
6334 kfree(clp
->cl_serverowner
);
6335 clp
->cl_serverowner
= res
.server_owner
;
6336 res
.server_owner
= NULL
;
6338 /* use the most recent implementation id */
6339 kfree(clp
->cl_implid
);
6340 clp
->cl_implid
= res
.impl_id
;
6342 if (clp
->cl_serverscope
!= NULL
&&
6343 !nfs41_same_server_scope(clp
->cl_serverscope
,
6344 res
.server_scope
)) {
6345 dprintk("%s: server_scope mismatch detected\n",
6347 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
6348 kfree(clp
->cl_serverscope
);
6349 clp
->cl_serverscope
= NULL
;
6352 if (clp
->cl_serverscope
== NULL
) {
6353 clp
->cl_serverscope
= res
.server_scope
;
6360 kfree(res
.server_owner
);
6362 kfree(res
.server_scope
);
6364 if (clp
->cl_implid
!= NULL
)
6365 dprintk("NFS reply exchange_id: Server Implementation ID: "
6366 "domain: %s, name: %s, date: %llu,%u\n",
6367 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
6368 clp
->cl_implid
->date
.seconds
,
6369 clp
->cl_implid
->date
.nseconds
);
6370 dprintk("NFS reply exchange_id: %d\n", status
);
6375 * nfs4_proc_exchange_id()
6377 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6379 * Since the clientid has expired, all compounds using sessions
6380 * associated with the stale clientid will be returning
6381 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6382 * be in some phase of session reset.
6384 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6386 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6388 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
6391 /* try SP4_MACH_CRED if krb5i/p */
6392 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
6393 authflavor
== RPC_AUTH_GSS_KRB5P
) {
6394 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
);
6400 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
);
6403 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6404 struct rpc_cred
*cred
)
6406 struct rpc_message msg
= {
6407 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
6413 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6414 trace_nfs4_destroy_clientid(clp
, status
);
6416 dprintk("NFS: Got error %d from the server %s on "
6417 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
6421 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6422 struct rpc_cred
*cred
)
6427 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
6428 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
6430 case -NFS4ERR_DELAY
:
6431 case -NFS4ERR_CLIENTID_BUSY
:
6441 int nfs4_destroy_clientid(struct nfs_client
*clp
)
6443 struct rpc_cred
*cred
;
6446 if (clp
->cl_mvops
->minor_version
< 1)
6448 if (clp
->cl_exchange_flags
== 0)
6450 if (clp
->cl_preserve_clid
)
6452 cred
= nfs4_get_clid_cred(clp
);
6453 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
6458 case -NFS4ERR_STALE_CLIENTID
:
6459 clp
->cl_exchange_flags
= 0;
6465 struct nfs4_get_lease_time_data
{
6466 struct nfs4_get_lease_time_args
*args
;
6467 struct nfs4_get_lease_time_res
*res
;
6468 struct nfs_client
*clp
;
6471 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
6474 struct nfs4_get_lease_time_data
*data
=
6475 (struct nfs4_get_lease_time_data
*)calldata
;
6477 dprintk("--> %s\n", __func__
);
6478 /* just setup sequence, do not trigger session recovery
6479 since we're invoked within one */
6480 nfs41_setup_sequence(data
->clp
->cl_session
,
6481 &data
->args
->la_seq_args
,
6482 &data
->res
->lr_seq_res
,
6484 dprintk("<-- %s\n", __func__
);
6488 * Called from nfs4_state_manager thread for session setup, so don't recover
6489 * from sequence operation or clientid errors.
6491 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
6493 struct nfs4_get_lease_time_data
*data
=
6494 (struct nfs4_get_lease_time_data
*)calldata
;
6496 dprintk("--> %s\n", __func__
);
6497 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
6499 switch (task
->tk_status
) {
6500 case -NFS4ERR_DELAY
:
6501 case -NFS4ERR_GRACE
:
6502 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
6503 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
6504 task
->tk_status
= 0;
6506 case -NFS4ERR_RETRY_UNCACHED_REP
:
6507 rpc_restart_call_prepare(task
);
6510 dprintk("<-- %s\n", __func__
);
6513 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
6514 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
6515 .rpc_call_done
= nfs4_get_lease_time_done
,
6518 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
6520 struct rpc_task
*task
;
6521 struct nfs4_get_lease_time_args args
;
6522 struct nfs4_get_lease_time_res res
= {
6523 .lr_fsinfo
= fsinfo
,
6525 struct nfs4_get_lease_time_data data
= {
6530 struct rpc_message msg
= {
6531 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
6535 struct rpc_task_setup task_setup
= {
6536 .rpc_client
= clp
->cl_rpcclient
,
6537 .rpc_message
= &msg
,
6538 .callback_ops
= &nfs4_get_lease_time_ops
,
6539 .callback_data
= &data
,
6540 .flags
= RPC_TASK_TIMEOUT
,
6544 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
6545 nfs4_set_sequence_privileged(&args
.la_seq_args
);
6546 dprintk("--> %s\n", __func__
);
6547 task
= rpc_run_task(&task_setup
);
6550 status
= PTR_ERR(task
);
6552 status
= task
->tk_status
;
6555 dprintk("<-- %s return %d\n", __func__
, status
);
6561 * Initialize the values to be used by the client in CREATE_SESSION
6562 * If nfs4_init_session set the fore channel request and response sizes,
6565 * Set the back channel max_resp_sz_cached to zero to force the client to
6566 * always set csa_cachethis to FALSE because the current implementation
6567 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6569 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
6571 unsigned int max_rqst_sz
, max_resp_sz
;
6573 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
6574 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
6576 /* Fore channel attributes */
6577 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
6578 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
6579 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
6580 args
->fc_attrs
.max_reqs
= max_session_slots
;
6582 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6583 "max_ops=%u max_reqs=%u\n",
6585 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
6586 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
6588 /* Back channel attributes */
6589 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
6590 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
6591 args
->bc_attrs
.max_resp_sz_cached
= 0;
6592 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
6593 args
->bc_attrs
.max_reqs
= 1;
6595 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6596 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6598 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
6599 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
6600 args
->bc_attrs
.max_reqs
);
6603 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6605 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
6606 struct nfs4_channel_attrs
*rcvd
= &session
->fc_attrs
;
6608 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
6611 * Our requested max_ops is the minimum we need; we're not
6612 * prepared to break up compounds into smaller pieces than that.
6613 * So, no point even trying to continue if the server won't
6616 if (rcvd
->max_ops
< sent
->max_ops
)
6618 if (rcvd
->max_reqs
== 0)
6620 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
6621 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
6625 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6627 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
6628 struct nfs4_channel_attrs
*rcvd
= &session
->bc_attrs
;
6630 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
6632 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
6634 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
6636 /* These would render the backchannel useless: */
6637 if (rcvd
->max_ops
!= sent
->max_ops
)
6639 if (rcvd
->max_reqs
!= sent
->max_reqs
)
6644 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
6645 struct nfs4_session
*session
)
6649 ret
= nfs4_verify_fore_channel_attrs(args
, session
);
6652 return nfs4_verify_back_channel_attrs(args
, session
);
6655 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
6656 struct rpc_cred
*cred
)
6658 struct nfs4_session
*session
= clp
->cl_session
;
6659 struct nfs41_create_session_args args
= {
6661 .cb_program
= NFS4_CALLBACK
,
6663 struct nfs41_create_session_res res
= {
6666 struct rpc_message msg
= {
6667 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
6674 nfs4_init_channel_attrs(&args
);
6675 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
6677 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6678 trace_nfs4_create_session(clp
, status
);
6681 /* Verify the session's negotiated channel_attrs values */
6682 status
= nfs4_verify_channel_attrs(&args
, session
);
6683 /* Increment the clientid slot sequence id */
6691 * Issues a CREATE_SESSION operation to the server.
6692 * It is the responsibility of the caller to verify the session is
6693 * expired before calling this routine.
6695 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6699 struct nfs4_session
*session
= clp
->cl_session
;
6701 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
6703 status
= _nfs4_proc_create_session(clp
, cred
);
6707 /* Init or reset the session slot tables */
6708 status
= nfs4_setup_session_slot_tables(session
);
6709 dprintk("slot table setup returned %d\n", status
);
6713 ptr
= (unsigned *)&session
->sess_id
.data
[0];
6714 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
6715 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
6717 dprintk("<-- %s\n", __func__
);
6722 * Issue the over-the-wire RPC DESTROY_SESSION.
6723 * The caller must serialize access to this routine.
6725 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
6726 struct rpc_cred
*cred
)
6728 struct rpc_message msg
= {
6729 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
6730 .rpc_argp
= session
,
6735 dprintk("--> nfs4_proc_destroy_session\n");
6737 /* session is still being setup */
6738 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
6741 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6742 trace_nfs4_destroy_session(session
->clp
, status
);
6745 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6746 "Session has been destroyed regardless...\n", status
);
6748 dprintk("<-- nfs4_proc_destroy_session\n");
6753 * Renew the cl_session lease.
6755 struct nfs4_sequence_data
{
6756 struct nfs_client
*clp
;
6757 struct nfs4_sequence_args args
;
6758 struct nfs4_sequence_res res
;
6761 static void nfs41_sequence_release(void *data
)
6763 struct nfs4_sequence_data
*calldata
= data
;
6764 struct nfs_client
*clp
= calldata
->clp
;
6766 if (atomic_read(&clp
->cl_count
) > 1)
6767 nfs4_schedule_state_renewal(clp
);
6768 nfs_put_client(clp
);
6772 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6774 switch(task
->tk_status
) {
6775 case -NFS4ERR_DELAY
:
6776 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6779 nfs4_schedule_lease_recovery(clp
);
6784 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
6786 struct nfs4_sequence_data
*calldata
= data
;
6787 struct nfs_client
*clp
= calldata
->clp
;
6789 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
6792 trace_nfs4_sequence(clp
, task
->tk_status
);
6793 if (task
->tk_status
< 0) {
6794 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
6795 if (atomic_read(&clp
->cl_count
) == 1)
6798 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
6799 rpc_restart_call_prepare(task
);
6803 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
6805 dprintk("<-- %s\n", __func__
);
6808 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
6810 struct nfs4_sequence_data
*calldata
= data
;
6811 struct nfs_client
*clp
= calldata
->clp
;
6812 struct nfs4_sequence_args
*args
;
6813 struct nfs4_sequence_res
*res
;
6815 args
= task
->tk_msg
.rpc_argp
;
6816 res
= task
->tk_msg
.rpc_resp
;
6818 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
6821 static const struct rpc_call_ops nfs41_sequence_ops
= {
6822 .rpc_call_done
= nfs41_sequence_call_done
,
6823 .rpc_call_prepare
= nfs41_sequence_prepare
,
6824 .rpc_release
= nfs41_sequence_release
,
6827 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
6828 struct rpc_cred
*cred
,
6831 struct nfs4_sequence_data
*calldata
;
6832 struct rpc_message msg
= {
6833 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
6836 struct rpc_task_setup task_setup_data
= {
6837 .rpc_client
= clp
->cl_rpcclient
,
6838 .rpc_message
= &msg
,
6839 .callback_ops
= &nfs41_sequence_ops
,
6840 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
6843 if (!atomic_inc_not_zero(&clp
->cl_count
))
6844 return ERR_PTR(-EIO
);
6845 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6846 if (calldata
== NULL
) {
6847 nfs_put_client(clp
);
6848 return ERR_PTR(-ENOMEM
);
6850 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
6852 nfs4_set_sequence_privileged(&calldata
->args
);
6853 msg
.rpc_argp
= &calldata
->args
;
6854 msg
.rpc_resp
= &calldata
->res
;
6855 calldata
->clp
= clp
;
6856 task_setup_data
.callback_data
= calldata
;
6858 return rpc_run_task(&task_setup_data
);
6861 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
6863 struct rpc_task
*task
;
6866 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
6868 task
= _nfs41_proc_sequence(clp
, cred
, false);
6870 ret
= PTR_ERR(task
);
6872 rpc_put_task_async(task
);
6873 dprintk("<-- %s status=%d\n", __func__
, ret
);
6877 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6879 struct rpc_task
*task
;
6882 task
= _nfs41_proc_sequence(clp
, cred
, true);
6884 ret
= PTR_ERR(task
);
6887 ret
= rpc_wait_for_completion_task(task
);
6889 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
6891 if (task
->tk_status
== 0)
6892 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
6893 ret
= task
->tk_status
;
6897 dprintk("<-- %s status=%d\n", __func__
, ret
);
6901 struct nfs4_reclaim_complete_data
{
6902 struct nfs_client
*clp
;
6903 struct nfs41_reclaim_complete_args arg
;
6904 struct nfs41_reclaim_complete_res res
;
6907 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
6909 struct nfs4_reclaim_complete_data
*calldata
= data
;
6911 nfs41_setup_sequence(calldata
->clp
->cl_session
,
6912 &calldata
->arg
.seq_args
,
6913 &calldata
->res
.seq_res
,
6917 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6919 switch(task
->tk_status
) {
6921 case -NFS4ERR_COMPLETE_ALREADY
:
6922 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
6924 case -NFS4ERR_DELAY
:
6925 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6927 case -NFS4ERR_RETRY_UNCACHED_REP
:
6930 nfs4_schedule_lease_recovery(clp
);
6935 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
6937 struct nfs4_reclaim_complete_data
*calldata
= data
;
6938 struct nfs_client
*clp
= calldata
->clp
;
6939 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
6941 dprintk("--> %s\n", __func__
);
6942 if (!nfs41_sequence_done(task
, res
))
6945 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
6946 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
6947 rpc_restart_call_prepare(task
);
6950 dprintk("<-- %s\n", __func__
);
6953 static void nfs4_free_reclaim_complete_data(void *data
)
6955 struct nfs4_reclaim_complete_data
*calldata
= data
;
6960 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
6961 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
6962 .rpc_call_done
= nfs4_reclaim_complete_done
,
6963 .rpc_release
= nfs4_free_reclaim_complete_data
,
6967 * Issue a global reclaim complete.
6969 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
6970 struct rpc_cred
*cred
)
6972 struct nfs4_reclaim_complete_data
*calldata
;
6973 struct rpc_task
*task
;
6974 struct rpc_message msg
= {
6975 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
6978 struct rpc_task_setup task_setup_data
= {
6979 .rpc_client
= clp
->cl_rpcclient
,
6980 .rpc_message
= &msg
,
6981 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
6982 .flags
= RPC_TASK_ASYNC
,
6984 int status
= -ENOMEM
;
6986 dprintk("--> %s\n", __func__
);
6987 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6988 if (calldata
== NULL
)
6990 calldata
->clp
= clp
;
6991 calldata
->arg
.one_fs
= 0;
6993 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
6994 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
6995 msg
.rpc_argp
= &calldata
->arg
;
6996 msg
.rpc_resp
= &calldata
->res
;
6997 task_setup_data
.callback_data
= calldata
;
6998 task
= rpc_run_task(&task_setup_data
);
7000 status
= PTR_ERR(task
);
7003 status
= nfs4_wait_for_completion_rpc_task(task
);
7005 status
= task
->tk_status
;
7009 dprintk("<-- %s status=%d\n", __func__
, status
);
7014 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
7016 struct nfs4_layoutget
*lgp
= calldata
;
7017 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
7018 struct nfs4_session
*session
= nfs4_get_session(server
);
7020 dprintk("--> %s\n", __func__
);
7021 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7022 * right now covering the LAYOUTGET we are about to send.
7023 * However, that is not so catastrophic, and there seems
7024 * to be no way to prevent it completely.
7026 if (nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
7027 &lgp
->res
.seq_res
, task
))
7029 if (pnfs_choose_layoutget_stateid(&lgp
->args
.stateid
,
7030 NFS_I(lgp
->args
.inode
)->layout
,
7031 lgp
->args
.ctx
->state
)) {
7032 rpc_exit(task
, NFS4_OK
);
7036 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
7038 struct nfs4_layoutget
*lgp
= calldata
;
7039 struct inode
*inode
= lgp
->args
.inode
;
7040 struct nfs_server
*server
= NFS_SERVER(inode
);
7041 struct pnfs_layout_hdr
*lo
;
7042 struct nfs4_state
*state
= NULL
;
7043 unsigned long timeo
, giveup
;
7045 dprintk("--> %s\n", __func__
);
7047 if (!nfs41_sequence_done(task
, &lgp
->res
.seq_res
))
7050 switch (task
->tk_status
) {
7053 case -NFS4ERR_LAYOUTTRYLATER
:
7054 case -NFS4ERR_RECALLCONFLICT
:
7055 timeo
= rpc_get_timeout(task
->tk_client
);
7056 giveup
= lgp
->args
.timestamp
+ timeo
;
7057 if (time_after(giveup
, jiffies
))
7058 task
->tk_status
= -NFS4ERR_DELAY
;
7060 case -NFS4ERR_EXPIRED
:
7061 case -NFS4ERR_BAD_STATEID
:
7062 spin_lock(&inode
->i_lock
);
7063 lo
= NFS_I(inode
)->layout
;
7064 if (!lo
|| list_empty(&lo
->plh_segs
)) {
7065 spin_unlock(&inode
->i_lock
);
7066 /* If the open stateid was bad, then recover it. */
7067 state
= lgp
->args
.ctx
->state
;
7071 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
);
7072 spin_unlock(&inode
->i_lock
);
7073 /* Mark the bad layout state as invalid, then
7074 * retry using the open stateid. */
7075 pnfs_free_lseg_list(&head
);
7078 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
7079 rpc_restart_call_prepare(task
);
7081 dprintk("<-- %s\n", __func__
);
7084 static size_t max_response_pages(struct nfs_server
*server
)
7086 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
7087 return nfs_page_array_len(0, max_resp_sz
);
7090 static void nfs4_free_pages(struct page
**pages
, size_t size
)
7097 for (i
= 0; i
< size
; i
++) {
7100 __free_page(pages
[i
]);
7105 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
7107 struct page
**pages
;
7110 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
7112 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
7116 for (i
= 0; i
< size
; i
++) {
7117 pages
[i
] = alloc_page(gfp_flags
);
7119 dprintk("%s: failed to allocate page\n", __func__
);
7120 nfs4_free_pages(pages
, size
);
7128 static void nfs4_layoutget_release(void *calldata
)
7130 struct nfs4_layoutget
*lgp
= calldata
;
7131 struct inode
*inode
= lgp
->args
.inode
;
7132 struct nfs_server
*server
= NFS_SERVER(inode
);
7133 size_t max_pages
= max_response_pages(server
);
7135 dprintk("--> %s\n", __func__
);
7136 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
7137 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
7138 put_nfs_open_context(lgp
->args
.ctx
);
7140 dprintk("<-- %s\n", __func__
);
7143 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
7144 .rpc_call_prepare
= nfs4_layoutget_prepare
,
7145 .rpc_call_done
= nfs4_layoutget_done
,
7146 .rpc_release
= nfs4_layoutget_release
,
7149 struct pnfs_layout_segment
*
7150 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, gfp_t gfp_flags
)
7152 struct inode
*inode
= lgp
->args
.inode
;
7153 struct nfs_server
*server
= NFS_SERVER(inode
);
7154 size_t max_pages
= max_response_pages(server
);
7155 struct rpc_task
*task
;
7156 struct rpc_message msg
= {
7157 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
7158 .rpc_argp
= &lgp
->args
,
7159 .rpc_resp
= &lgp
->res
,
7160 .rpc_cred
= lgp
->cred
,
7162 struct rpc_task_setup task_setup_data
= {
7163 .rpc_client
= server
->client
,
7164 .rpc_message
= &msg
,
7165 .callback_ops
= &nfs4_layoutget_call_ops
,
7166 .callback_data
= lgp
,
7167 .flags
= RPC_TASK_ASYNC
,
7169 struct pnfs_layout_segment
*lseg
= NULL
;
7172 dprintk("--> %s\n", __func__
);
7174 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
7175 if (!lgp
->args
.layout
.pages
) {
7176 nfs4_layoutget_release(lgp
);
7177 return ERR_PTR(-ENOMEM
);
7179 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
7180 lgp
->args
.timestamp
= jiffies
;
7182 lgp
->res
.layoutp
= &lgp
->args
.layout
;
7183 lgp
->res
.seq_res
.sr_slot
= NULL
;
7184 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
7186 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7187 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
7189 task
= rpc_run_task(&task_setup_data
);
7191 return ERR_CAST(task
);
7192 status
= nfs4_wait_for_completion_rpc_task(task
);
7194 status
= task
->tk_status
;
7195 trace_nfs4_layoutget(lgp
->args
.ctx
,
7199 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7200 if (status
== 0 && lgp
->res
.layoutp
->len
)
7201 lseg
= pnfs_layout_process(lgp
);
7203 dprintk("<-- %s status=%d\n", __func__
, status
);
7205 return ERR_PTR(status
);
7210 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
7212 struct nfs4_layoutreturn
*lrp
= calldata
;
7214 dprintk("--> %s\n", __func__
);
7215 nfs41_setup_sequence(lrp
->clp
->cl_session
,
7216 &lrp
->args
.seq_args
,
7221 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
7223 struct nfs4_layoutreturn
*lrp
= calldata
;
7224 struct nfs_server
*server
;
7226 dprintk("--> %s\n", __func__
);
7228 if (!nfs41_sequence_done(task
, &lrp
->res
.seq_res
))
7231 server
= NFS_SERVER(lrp
->args
.inode
);
7232 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
7233 rpc_restart_call_prepare(task
);
7236 dprintk("<-- %s\n", __func__
);
7239 static void nfs4_layoutreturn_release(void *calldata
)
7241 struct nfs4_layoutreturn
*lrp
= calldata
;
7242 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
7244 dprintk("--> %s\n", __func__
);
7245 spin_lock(&lo
->plh_inode
->i_lock
);
7246 if (lrp
->res
.lrs_present
)
7247 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
7248 lo
->plh_block_lgets
--;
7249 spin_unlock(&lo
->plh_inode
->i_lock
);
7250 pnfs_put_layout_hdr(lrp
->args
.layout
);
7252 dprintk("<-- %s\n", __func__
);
7255 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
7256 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
7257 .rpc_call_done
= nfs4_layoutreturn_done
,
7258 .rpc_release
= nfs4_layoutreturn_release
,
7261 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
)
7263 struct rpc_task
*task
;
7264 struct rpc_message msg
= {
7265 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
7266 .rpc_argp
= &lrp
->args
,
7267 .rpc_resp
= &lrp
->res
,
7268 .rpc_cred
= lrp
->cred
,
7270 struct rpc_task_setup task_setup_data
= {
7271 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
7272 .rpc_message
= &msg
,
7273 .callback_ops
= &nfs4_layoutreturn_call_ops
,
7274 .callback_data
= lrp
,
7278 dprintk("--> %s\n", __func__
);
7279 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
7280 task
= rpc_run_task(&task_setup_data
);
7282 return PTR_ERR(task
);
7283 status
= task
->tk_status
;
7284 trace_nfs4_layoutreturn(lrp
->args
.inode
, status
);
7285 dprintk("<-- %s status=%d\n", __func__
, status
);
7291 * Retrieve the list of Data Server devices from the MDS.
7293 static int _nfs4_getdevicelist(struct nfs_server
*server
,
7294 const struct nfs_fh
*fh
,
7295 struct pnfs_devicelist
*devlist
)
7297 struct nfs4_getdevicelist_args args
= {
7299 .layoutclass
= server
->pnfs_curr_ld
->id
,
7301 struct nfs4_getdevicelist_res res
= {
7304 struct rpc_message msg
= {
7305 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICELIST
],
7311 dprintk("--> %s\n", __func__
);
7312 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
,
7314 dprintk("<-- %s status=%d\n", __func__
, status
);
7318 int nfs4_proc_getdevicelist(struct nfs_server
*server
,
7319 const struct nfs_fh
*fh
,
7320 struct pnfs_devicelist
*devlist
)
7322 struct nfs4_exception exception
= { };
7326 err
= nfs4_handle_exception(server
,
7327 _nfs4_getdevicelist(server
, fh
, devlist
),
7329 } while (exception
.retry
);
7331 dprintk("%s: err=%d, num_devs=%u\n", __func__
,
7332 err
, devlist
->num_devs
);
7336 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist
);
7339 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
7340 struct pnfs_device
*pdev
,
7341 struct rpc_cred
*cred
)
7343 struct nfs4_getdeviceinfo_args args
= {
7346 struct nfs4_getdeviceinfo_res res
= {
7349 struct rpc_message msg
= {
7350 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
7357 dprintk("--> %s\n", __func__
);
7358 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
7359 dprintk("<-- %s status=%d\n", __func__
, status
);
7364 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
7365 struct pnfs_device
*pdev
,
7366 struct rpc_cred
*cred
)
7368 struct nfs4_exception exception
= { };
7372 err
= nfs4_handle_exception(server
,
7373 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
7375 } while (exception
.retry
);
7378 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
7380 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
7382 struct nfs4_layoutcommit_data
*data
= calldata
;
7383 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
7384 struct nfs4_session
*session
= nfs4_get_session(server
);
7386 nfs41_setup_sequence(session
,
7387 &data
->args
.seq_args
,
7393 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
7395 struct nfs4_layoutcommit_data
*data
= calldata
;
7396 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
7398 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
7401 switch (task
->tk_status
) { /* Just ignore these failures */
7402 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
7403 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
7404 case -NFS4ERR_BADLAYOUT
: /* no layout */
7405 case -NFS4ERR_GRACE
: /* loca_recalim always false */
7406 task
->tk_status
= 0;
7409 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
7413 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
7414 rpc_restart_call_prepare(task
);
7420 static void nfs4_layoutcommit_release(void *calldata
)
7422 struct nfs4_layoutcommit_data
*data
= calldata
;
7424 pnfs_cleanup_layoutcommit(data
);
7425 put_rpccred(data
->cred
);
7429 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
7430 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
7431 .rpc_call_done
= nfs4_layoutcommit_done
,
7432 .rpc_release
= nfs4_layoutcommit_release
,
7436 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
7438 struct rpc_message msg
= {
7439 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
7440 .rpc_argp
= &data
->args
,
7441 .rpc_resp
= &data
->res
,
7442 .rpc_cred
= data
->cred
,
7444 struct rpc_task_setup task_setup_data
= {
7445 .task
= &data
->task
,
7446 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
7447 .rpc_message
= &msg
,
7448 .callback_ops
= &nfs4_layoutcommit_ops
,
7449 .callback_data
= data
,
7450 .flags
= RPC_TASK_ASYNC
,
7452 struct rpc_task
*task
;
7455 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7456 "lbw: %llu inode %lu\n",
7457 data
->task
.tk_pid
, sync
,
7458 data
->args
.lastbytewritten
,
7459 data
->args
.inode
->i_ino
);
7461 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
7462 task
= rpc_run_task(&task_setup_data
);
7464 return PTR_ERR(task
);
7467 status
= nfs4_wait_for_completion_rpc_task(task
);
7470 status
= task
->tk_status
;
7471 trace_nfs4_layoutcommit(data
->args
.inode
, status
);
7473 dprintk("%s: status %d\n", __func__
, status
);
7479 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7480 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7483 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7484 struct nfs_fsinfo
*info
,
7485 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
7487 struct nfs41_secinfo_no_name_args args
= {
7488 .style
= SECINFO_STYLE_CURRENT_FH
,
7490 struct nfs4_secinfo_res res
= {
7493 struct rpc_message msg
= {
7494 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
7498 struct rpc_clnt
*clnt
= server
->client
;
7501 if (use_integrity
) {
7502 clnt
= server
->nfs_client
->cl_rpcclient
;
7503 msg
.rpc_cred
= nfs4_get_clid_cred(server
->nfs_client
);
7506 dprintk("--> %s\n", __func__
);
7507 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
7509 dprintk("<-- %s status=%d\n", __func__
, status
);
7512 put_rpccred(msg
.rpc_cred
);
7518 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7519 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
7521 struct nfs4_exception exception
= { };
7524 /* first try using integrity protection */
7525 err
= -NFS4ERR_WRONGSEC
;
7527 /* try to use integrity protection with machine cred */
7528 if (_nfs4_is_integrity_protected(server
->nfs_client
))
7529 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
7533 * if unable to use integrity protection, or SECINFO with
7534 * integrity protection returns NFS4ERR_WRONGSEC (which is
7535 * disallowed by spec, but exists in deployed servers) use
7536 * the current filesystem's rpc_client and the user cred.
7538 if (err
== -NFS4ERR_WRONGSEC
)
7539 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
7544 case -NFS4ERR_WRONGSEC
:
7545 case -NFS4ERR_NOTSUPP
:
7548 err
= nfs4_handle_exception(server
, err
, &exception
);
7550 } while (exception
.retry
);
7556 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7557 struct nfs_fsinfo
*info
)
7561 rpc_authflavor_t flavor
;
7562 struct nfs4_secinfo_flavors
*flavors
;
7564 page
= alloc_page(GFP_KERNEL
);
7570 flavors
= page_address(page
);
7571 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
7574 * Fall back on "guess and check" method if
7575 * the server doesn't support SECINFO_NO_NAME
7577 if (err
== -NFS4ERR_WRONGSEC
|| err
== -NFS4ERR_NOTSUPP
) {
7578 err
= nfs4_find_root_sec(server
, fhandle
, info
);
7584 flavor
= nfs_find_best_sec(flavors
);
7586 err
= nfs4_lookup_root_sec(server
, fhandle
, info
, flavor
);
7596 static int _nfs41_test_stateid(struct nfs_server
*server
,
7597 nfs4_stateid
*stateid
,
7598 struct rpc_cred
*cred
)
7601 struct nfs41_test_stateid_args args
= {
7604 struct nfs41_test_stateid_res res
;
7605 struct rpc_message msg
= {
7606 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
7611 struct rpc_clnt
*rpc_client
= server
->client
;
7613 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
7616 dprintk("NFS call test_stateid %p\n", stateid
);
7617 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7618 nfs4_set_sequence_privileged(&args
.seq_args
);
7619 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
7620 &args
.seq_args
, &res
.seq_res
);
7621 if (status
!= NFS_OK
) {
7622 dprintk("NFS reply test_stateid: failed, %d\n", status
);
7625 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
7630 * nfs41_test_stateid - perform a TEST_STATEID operation
7632 * @server: server / transport on which to perform the operation
7633 * @stateid: state ID to test
7636 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7637 * Otherwise a negative NFS4ERR value is returned if the operation
7638 * failed or the state ID is not currently valid.
7640 static int nfs41_test_stateid(struct nfs_server
*server
,
7641 nfs4_stateid
*stateid
,
7642 struct rpc_cred
*cred
)
7644 struct nfs4_exception exception
= { };
7647 err
= _nfs41_test_stateid(server
, stateid
, cred
);
7648 if (err
!= -NFS4ERR_DELAY
)
7650 nfs4_handle_exception(server
, err
, &exception
);
7651 } while (exception
.retry
);
7655 struct nfs_free_stateid_data
{
7656 struct nfs_server
*server
;
7657 struct nfs41_free_stateid_args args
;
7658 struct nfs41_free_stateid_res res
;
7661 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
7663 struct nfs_free_stateid_data
*data
= calldata
;
7664 nfs41_setup_sequence(nfs4_get_session(data
->server
),
7665 &data
->args
.seq_args
,
7670 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
7672 struct nfs_free_stateid_data
*data
= calldata
;
7674 nfs41_sequence_done(task
, &data
->res
.seq_res
);
7676 switch (task
->tk_status
) {
7677 case -NFS4ERR_DELAY
:
7678 if (nfs4_async_handle_error(task
, data
->server
, NULL
) == -EAGAIN
)
7679 rpc_restart_call_prepare(task
);
7683 static void nfs41_free_stateid_release(void *calldata
)
7688 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
7689 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
7690 .rpc_call_done
= nfs41_free_stateid_done
,
7691 .rpc_release
= nfs41_free_stateid_release
,
7694 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
7695 nfs4_stateid
*stateid
,
7696 struct rpc_cred
*cred
,
7699 struct rpc_message msg
= {
7700 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
7703 struct rpc_task_setup task_setup
= {
7704 .rpc_client
= server
->client
,
7705 .rpc_message
= &msg
,
7706 .callback_ops
= &nfs41_free_stateid_ops
,
7707 .flags
= RPC_TASK_ASYNC
,
7709 struct nfs_free_stateid_data
*data
;
7711 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
7712 &task_setup
.rpc_client
, &msg
);
7714 dprintk("NFS call free_stateid %p\n", stateid
);
7715 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
7717 return ERR_PTR(-ENOMEM
);
7718 data
->server
= server
;
7719 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
7721 task_setup
.callback_data
= data
;
7723 msg
.rpc_argp
= &data
->args
;
7724 msg
.rpc_resp
= &data
->res
;
7725 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
7727 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
7729 return rpc_run_task(&task_setup
);
7733 * nfs41_free_stateid - perform a FREE_STATEID operation
7735 * @server: server / transport on which to perform the operation
7736 * @stateid: state ID to release
7739 * Returns NFS_OK if the server freed "stateid". Otherwise a
7740 * negative NFS4ERR value is returned.
7742 static int nfs41_free_stateid(struct nfs_server
*server
,
7743 nfs4_stateid
*stateid
,
7744 struct rpc_cred
*cred
)
7746 struct rpc_task
*task
;
7749 task
= _nfs41_free_stateid(server
, stateid
, cred
, true);
7751 return PTR_ERR(task
);
7752 ret
= rpc_wait_for_completion_task(task
);
7754 ret
= task
->tk_status
;
7759 static int nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
7761 struct rpc_task
*task
;
7762 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
7764 task
= _nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
7765 nfs4_free_lock_state(server
, lsp
);
7767 return PTR_ERR(task
);
7772 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
7773 const nfs4_stateid
*s2
)
7775 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
7778 if (s1
->seqid
== s2
->seqid
)
7780 if (s1
->seqid
== 0 || s2
->seqid
== 0)
7786 #endif /* CONFIG_NFS_V4_1 */
7788 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
7789 const nfs4_stateid
*s2
)
7791 return nfs4_stateid_match(s1
, s2
);
7795 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
7796 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7797 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7798 .recover_open
= nfs4_open_reclaim
,
7799 .recover_lock
= nfs4_lock_reclaim
,
7800 .establish_clid
= nfs4_init_clientid
,
7801 .detect_trunking
= nfs40_discover_server_trunking
,
7804 #if defined(CONFIG_NFS_V4_1)
7805 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
7806 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7807 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7808 .recover_open
= nfs4_open_reclaim
,
7809 .recover_lock
= nfs4_lock_reclaim
,
7810 .establish_clid
= nfs41_init_clientid
,
7811 .reclaim_complete
= nfs41_proc_reclaim_complete
,
7812 .detect_trunking
= nfs41_discover_server_trunking
,
7814 #endif /* CONFIG_NFS_V4_1 */
7816 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
7817 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7818 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7819 .recover_open
= nfs4_open_expired
,
7820 .recover_lock
= nfs4_lock_expired
,
7821 .establish_clid
= nfs4_init_clientid
,
7824 #if defined(CONFIG_NFS_V4_1)
7825 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
7826 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7827 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7828 .recover_open
= nfs41_open_expired
,
7829 .recover_lock
= nfs41_lock_expired
,
7830 .establish_clid
= nfs41_init_clientid
,
7832 #endif /* CONFIG_NFS_V4_1 */
7834 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
7835 .sched_state_renewal
= nfs4_proc_async_renew
,
7836 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
7837 .renew_lease
= nfs4_proc_renew
,
7840 #if defined(CONFIG_NFS_V4_1)
7841 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
7842 .sched_state_renewal
= nfs41_proc_async_sequence
,
7843 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
7844 .renew_lease
= nfs4_proc_sequence
,
7848 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
7850 .init_caps
= NFS_CAP_READDIRPLUS
7851 | NFS_CAP_ATOMIC_OPEN
7852 | NFS_CAP_CHANGE_ATTR
7853 | NFS_CAP_POSIX_LOCK
,
7854 .init_client
= nfs40_init_client
,
7855 .shutdown_client
= nfs40_shutdown_client
,
7856 .match_stateid
= nfs4_match_stateid
,
7857 .find_root_sec
= nfs4_find_root_sec
,
7858 .free_lock_state
= nfs4_release_lockowner
,
7859 .call_sync_ops
= &nfs40_call_sync_ops
,
7860 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
7861 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
7862 .state_renewal_ops
= &nfs40_state_renewal_ops
,
7865 #if defined(CONFIG_NFS_V4_1)
7866 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
7868 .init_caps
= NFS_CAP_READDIRPLUS
7869 | NFS_CAP_ATOMIC_OPEN
7870 | NFS_CAP_CHANGE_ATTR
7871 | NFS_CAP_POSIX_LOCK
7872 | NFS_CAP_STATEID_NFSV41
7873 | NFS_CAP_ATOMIC_OPEN_V1
,
7874 .init_client
= nfs41_init_client
,
7875 .shutdown_client
= nfs41_shutdown_client
,
7876 .match_stateid
= nfs41_match_stateid
,
7877 .find_root_sec
= nfs41_find_root_sec
,
7878 .free_lock_state
= nfs41_free_lock_state
,
7879 .call_sync_ops
= &nfs41_call_sync_ops
,
7880 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7881 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7882 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7886 #if defined(CONFIG_NFS_V4_2)
7887 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
7889 .init_caps
= NFS_CAP_READDIRPLUS
7890 | NFS_CAP_ATOMIC_OPEN
7891 | NFS_CAP_CHANGE_ATTR
7892 | NFS_CAP_POSIX_LOCK
7893 | NFS_CAP_STATEID_NFSV41
7894 | NFS_CAP_ATOMIC_OPEN_V1
,
7895 .init_client
= nfs41_init_client
,
7896 .shutdown_client
= nfs41_shutdown_client
,
7897 .match_stateid
= nfs41_match_stateid
,
7898 .find_root_sec
= nfs41_find_root_sec
,
7899 .free_lock_state
= nfs41_free_lock_state
,
7900 .call_sync_ops
= &nfs41_call_sync_ops
,
7901 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7902 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7903 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7907 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
7908 [0] = &nfs_v4_0_minor_ops
,
7909 #if defined(CONFIG_NFS_V4_1)
7910 [1] = &nfs_v4_1_minor_ops
,
7912 #if defined(CONFIG_NFS_V4_2)
7913 [2] = &nfs_v4_2_minor_ops
,
7917 static const struct inode_operations nfs4_dir_inode_operations
= {
7918 .create
= nfs_create
,
7919 .lookup
= nfs_lookup
,
7920 .atomic_open
= nfs_atomic_open
,
7922 .unlink
= nfs_unlink
,
7923 .symlink
= nfs_symlink
,
7927 .rename
= nfs_rename
,
7928 .permission
= nfs_permission
,
7929 .getattr
= nfs_getattr
,
7930 .setattr
= nfs_setattr
,
7931 .getxattr
= generic_getxattr
,
7932 .setxattr
= generic_setxattr
,
7933 .listxattr
= generic_listxattr
,
7934 .removexattr
= generic_removexattr
,
7937 static const struct inode_operations nfs4_file_inode_operations
= {
7938 .permission
= nfs_permission
,
7939 .getattr
= nfs_getattr
,
7940 .setattr
= nfs_setattr
,
7941 .getxattr
= generic_getxattr
,
7942 .setxattr
= generic_setxattr
,
7943 .listxattr
= generic_listxattr
,
7944 .removexattr
= generic_removexattr
,
7947 const struct nfs_rpc_ops nfs_v4_clientops
= {
7948 .version
= 4, /* protocol version */
7949 .dentry_ops
= &nfs4_dentry_operations
,
7950 .dir_inode_ops
= &nfs4_dir_inode_operations
,
7951 .file_inode_ops
= &nfs4_file_inode_operations
,
7952 .file_ops
= &nfs4_file_operations
,
7953 .getroot
= nfs4_proc_get_root
,
7954 .submount
= nfs4_submount
,
7955 .try_mount
= nfs4_try_mount
,
7956 .getattr
= nfs4_proc_getattr
,
7957 .setattr
= nfs4_proc_setattr
,
7958 .lookup
= nfs4_proc_lookup
,
7959 .access
= nfs4_proc_access
,
7960 .readlink
= nfs4_proc_readlink
,
7961 .create
= nfs4_proc_create
,
7962 .remove
= nfs4_proc_remove
,
7963 .unlink_setup
= nfs4_proc_unlink_setup
,
7964 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
7965 .unlink_done
= nfs4_proc_unlink_done
,
7966 .rename
= nfs4_proc_rename
,
7967 .rename_setup
= nfs4_proc_rename_setup
,
7968 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
7969 .rename_done
= nfs4_proc_rename_done
,
7970 .link
= nfs4_proc_link
,
7971 .symlink
= nfs4_proc_symlink
,
7972 .mkdir
= nfs4_proc_mkdir
,
7973 .rmdir
= nfs4_proc_remove
,
7974 .readdir
= nfs4_proc_readdir
,
7975 .mknod
= nfs4_proc_mknod
,
7976 .statfs
= nfs4_proc_statfs
,
7977 .fsinfo
= nfs4_proc_fsinfo
,
7978 .pathconf
= nfs4_proc_pathconf
,
7979 .set_capabilities
= nfs4_server_capabilities
,
7980 .decode_dirent
= nfs4_decode_dirent
,
7981 .read_setup
= nfs4_proc_read_setup
,
7982 .read_pageio_init
= pnfs_pageio_init_read
,
7983 .read_rpc_prepare
= nfs4_proc_read_rpc_prepare
,
7984 .read_done
= nfs4_read_done
,
7985 .write_setup
= nfs4_proc_write_setup
,
7986 .write_pageio_init
= pnfs_pageio_init_write
,
7987 .write_rpc_prepare
= nfs4_proc_write_rpc_prepare
,
7988 .write_done
= nfs4_write_done
,
7989 .commit_setup
= nfs4_proc_commit_setup
,
7990 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
7991 .commit_done
= nfs4_commit_done
,
7992 .lock
= nfs4_proc_lock
,
7993 .clear_acl_cache
= nfs4_zap_acl_attr
,
7994 .close_context
= nfs4_close_context
,
7995 .open_context
= nfs4_atomic_open
,
7996 .have_delegation
= nfs4_have_delegation
,
7997 .return_delegation
= nfs4_inode_return_delegation
,
7998 .alloc_client
= nfs4_alloc_client
,
7999 .init_client
= nfs4_init_client
,
8000 .free_client
= nfs4_free_client
,
8001 .create_server
= nfs4_create_server
,
8002 .clone_server
= nfs_clone_server
,
8005 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
8006 .prefix
= XATTR_NAME_NFSV4_ACL
,
8007 .list
= nfs4_xattr_list_nfs4_acl
,
8008 .get
= nfs4_xattr_get_nfs4_acl
,
8009 .set
= nfs4_xattr_set_nfs4_acl
,
8012 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
8013 &nfs4_xattr_nfs4_acl_handler
,
8014 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8015 &nfs4_xattr_nfs4_label_handler
,