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
2888 * Returns zero on success, or a negative errno.
2890 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2891 struct nfs_fsinfo
*info
)
2895 status
= nfs4_lookup_root(server
, fhandle
, info
);
2896 if ((status
== -NFS4ERR_WRONGSEC
) &&
2897 !(server
->flags
& NFS_MOUNT_SECFLAVOUR
))
2898 status
= nfs4_do_find_root_sec(server
, fhandle
, info
);
2901 status
= nfs4_server_capabilities(server
, fhandle
);
2903 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2905 return nfs4_map_errors(status
);
2908 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
2909 struct nfs_fsinfo
*info
)
2912 struct nfs_fattr
*fattr
= info
->fattr
;
2913 struct nfs4_label
*label
= NULL
;
2915 error
= nfs4_server_capabilities(server
, mntfh
);
2917 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
2921 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
2923 return PTR_ERR(label
);
2925 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
2927 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
2928 goto err_free_label
;
2931 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
2932 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
2933 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
2936 nfs4_label_free(label
);
2942 * Get locations and (maybe) other attributes of a referral.
2943 * Note that we'll actually follow the referral later when
2944 * we detect fsid mismatch in inode revalidation
2946 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
2947 const struct qstr
*name
, struct nfs_fattr
*fattr
,
2948 struct nfs_fh
*fhandle
)
2950 int status
= -ENOMEM
;
2951 struct page
*page
= NULL
;
2952 struct nfs4_fs_locations
*locations
= NULL
;
2954 page
= alloc_page(GFP_KERNEL
);
2957 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2958 if (locations
== NULL
)
2961 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
2964 /* Make sure server returned a different fsid for the referral */
2965 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2966 dprintk("%s: server did not return a different fsid for"
2967 " a referral at %s\n", __func__
, name
->name
);
2971 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2972 nfs_fixup_referral_attributes(&locations
->fattr
);
2974 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2975 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2976 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2984 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2985 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
2987 struct nfs4_getattr_arg args
= {
2989 .bitmask
= server
->attr_bitmask
,
2991 struct nfs4_getattr_res res
= {
2996 struct rpc_message msg
= {
2997 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3002 args
.bitmask
= nfs4_bitmask(server
, label
);
3004 nfs_fattr_init(fattr
);
3005 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3008 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3009 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3011 struct nfs4_exception exception
= { };
3014 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3015 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3016 err
= nfs4_handle_exception(server
, err
,
3018 } while (exception
.retry
);
3023 * The file is not closed if it is opened due to the a request to change
3024 * the size of the file. The open call will not be needed once the
3025 * VFS layer lookup-intents are implemented.
3027 * Close is called when the inode is destroyed.
3028 * If we haven't opened the file for O_WRONLY, we
3029 * need to in the size_change case to obtain a stateid.
3032 * Because OPEN is always done by name in nfsv4, it is
3033 * possible that we opened a different file by the same
3034 * name. We can recognize this race condition, but we
3035 * can't do anything about it besides returning an error.
3037 * This will be fixed with VFS changes (lookup-intent).
3040 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3041 struct iattr
*sattr
)
3043 struct inode
*inode
= dentry
->d_inode
;
3044 struct rpc_cred
*cred
= NULL
;
3045 struct nfs4_state
*state
= NULL
;
3046 struct nfs4_label
*label
= NULL
;
3049 if (pnfs_ld_layoutret_on_setattr(inode
))
3050 pnfs_commit_and_return_layout(inode
);
3052 nfs_fattr_init(fattr
);
3054 /* Deal with open(O_TRUNC) */
3055 if (sattr
->ia_valid
& ATTR_OPEN
)
3056 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3058 /* Optimization: if the end result is no change, don't RPC */
3059 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3062 /* Search for an existing open(O_WRITE) file */
3063 if (sattr
->ia_valid
& ATTR_FILE
) {
3064 struct nfs_open_context
*ctx
;
3066 ctx
= nfs_file_open_context(sattr
->ia_file
);
3073 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3075 return PTR_ERR(label
);
3077 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
3079 nfs_setattr_update_inode(inode
, sattr
);
3080 nfs_setsecurity(inode
, fattr
, label
);
3082 nfs4_label_free(label
);
3086 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3087 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3088 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3090 struct nfs_server
*server
= NFS_SERVER(dir
);
3092 struct nfs4_lookup_arg args
= {
3093 .bitmask
= server
->attr_bitmask
,
3094 .dir_fh
= NFS_FH(dir
),
3097 struct nfs4_lookup_res res
= {
3103 struct rpc_message msg
= {
3104 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3109 args
.bitmask
= nfs4_bitmask(server
, label
);
3111 nfs_fattr_init(fattr
);
3113 dprintk("NFS call lookup %s\n", name
->name
);
3114 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3115 dprintk("NFS reply lookup: %d\n", status
);
3119 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3121 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3122 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3123 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3127 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3128 struct qstr
*name
, struct nfs_fh
*fhandle
,
3129 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3131 struct nfs4_exception exception
= { };
3132 struct rpc_clnt
*client
= *clnt
;
3135 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3136 trace_nfs4_lookup(dir
, name
, err
);
3138 case -NFS4ERR_BADNAME
:
3141 case -NFS4ERR_MOVED
:
3142 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3144 case -NFS4ERR_WRONGSEC
:
3146 if (client
!= *clnt
)
3149 client
= nfs4_create_sec_client(client
, dir
, name
);
3151 return PTR_ERR(client
);
3153 exception
.retry
= 1;
3156 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3158 } while (exception
.retry
);
3163 else if (client
!= *clnt
)
3164 rpc_shutdown_client(client
);
3169 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
3170 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3171 struct nfs4_label
*label
)
3174 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3176 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3177 if (client
!= NFS_CLIENT(dir
)) {
3178 rpc_shutdown_client(client
);
3179 nfs_fixup_secinfo_attributes(fattr
);
3185 nfs4_proc_lookup_mountpoint(struct inode
*dir
, struct qstr
*name
,
3186 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3189 struct rpc_clnt
*client
= rpc_clone_client(NFS_CLIENT(dir
));
3191 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3193 rpc_shutdown_client(client
);
3194 return ERR_PTR(status
);
3199 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3201 struct nfs_server
*server
= NFS_SERVER(inode
);
3202 struct nfs4_accessargs args
= {
3203 .fh
= NFS_FH(inode
),
3204 .bitmask
= server
->cache_consistency_bitmask
,
3206 struct nfs4_accessres res
= {
3209 struct rpc_message msg
= {
3210 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3213 .rpc_cred
= entry
->cred
,
3215 int mode
= entry
->mask
;
3219 * Determine which access bits we want to ask for...
3221 if (mode
& MAY_READ
)
3222 args
.access
|= NFS4_ACCESS_READ
;
3223 if (S_ISDIR(inode
->i_mode
)) {
3224 if (mode
& MAY_WRITE
)
3225 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3226 if (mode
& MAY_EXEC
)
3227 args
.access
|= NFS4_ACCESS_LOOKUP
;
3229 if (mode
& MAY_WRITE
)
3230 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3231 if (mode
& MAY_EXEC
)
3232 args
.access
|= NFS4_ACCESS_EXECUTE
;
3235 res
.fattr
= nfs_alloc_fattr();
3236 if (res
.fattr
== NULL
)
3239 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3241 nfs_access_set_mask(entry
, res
.access
);
3242 nfs_refresh_inode(inode
, res
.fattr
);
3244 nfs_free_fattr(res
.fattr
);
3248 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3250 struct nfs4_exception exception
= { };
3253 err
= _nfs4_proc_access(inode
, entry
);
3254 trace_nfs4_access(inode
, err
);
3255 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3257 } while (exception
.retry
);
3262 * TODO: For the time being, we don't try to get any attributes
3263 * along with any of the zero-copy operations READ, READDIR,
3266 * In the case of the first three, we want to put the GETATTR
3267 * after the read-type operation -- this is because it is hard
3268 * to predict the length of a GETATTR response in v4, and thus
3269 * align the READ data correctly. This means that the GETATTR
3270 * may end up partially falling into the page cache, and we should
3271 * shift it into the 'tail' of the xdr_buf before processing.
3272 * To do this efficiently, we need to know the total length
3273 * of data received, which doesn't seem to be available outside
3276 * In the case of WRITE, we also want to put the GETATTR after
3277 * the operation -- in this case because we want to make sure
3278 * we get the post-operation mtime and size.
3280 * Both of these changes to the XDR layer would in fact be quite
3281 * minor, but I decided to leave them for a subsequent patch.
3283 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3284 unsigned int pgbase
, unsigned int pglen
)
3286 struct nfs4_readlink args
= {
3287 .fh
= NFS_FH(inode
),
3292 struct nfs4_readlink_res res
;
3293 struct rpc_message msg
= {
3294 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3299 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3302 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3303 unsigned int pgbase
, unsigned int pglen
)
3305 struct nfs4_exception exception
= { };
3308 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3309 trace_nfs4_readlink(inode
, err
);
3310 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3312 } while (exception
.retry
);
3317 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3320 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3323 struct nfs4_label l
, *ilabel
= NULL
;
3324 struct nfs_open_context
*ctx
;
3325 struct nfs4_state
*state
;
3328 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
3330 return PTR_ERR(ctx
);
3332 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3334 sattr
->ia_mode
&= ~current_umask();
3335 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
);
3336 if (IS_ERR(state
)) {
3337 status
= PTR_ERR(state
);
3341 nfs4_label_release_security(ilabel
);
3342 put_nfs_open_context(ctx
);
3346 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3348 struct nfs_server
*server
= NFS_SERVER(dir
);
3349 struct nfs_removeargs args
= {
3353 struct nfs_removeres res
= {
3356 struct rpc_message msg
= {
3357 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3363 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3365 update_changeattr(dir
, &res
.cinfo
);
3369 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3371 struct nfs4_exception exception
= { };
3374 err
= _nfs4_proc_remove(dir
, name
);
3375 trace_nfs4_remove(dir
, name
, err
);
3376 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3378 } while (exception
.retry
);
3382 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
3384 struct nfs_server
*server
= NFS_SERVER(dir
);
3385 struct nfs_removeargs
*args
= msg
->rpc_argp
;
3386 struct nfs_removeres
*res
= msg
->rpc_resp
;
3388 res
->server
= server
;
3389 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
3390 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
3392 nfs_fattr_init(res
->dir_attr
);
3395 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
3397 nfs4_setup_sequence(NFS_SERVER(data
->dir
),
3398 &data
->args
.seq_args
,
3403 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
3405 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
3406 struct nfs_removeres
*res
= &data
->res
;
3408 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3410 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3412 update_changeattr(dir
, &res
->cinfo
);
3416 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
3418 struct nfs_server
*server
= NFS_SERVER(dir
);
3419 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
3420 struct nfs_renameres
*res
= msg
->rpc_resp
;
3422 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
3423 res
->server
= server
;
3424 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
3427 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
3429 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
3430 &data
->args
.seq_args
,
3435 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
3436 struct inode
*new_dir
)
3438 struct nfs_renamedata
*data
= task
->tk_calldata
;
3439 struct nfs_renameres
*res
= &data
->res
;
3441 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3443 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3446 update_changeattr(old_dir
, &res
->old_cinfo
);
3447 update_changeattr(new_dir
, &res
->new_cinfo
);
3451 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3452 struct inode
*new_dir
, struct qstr
*new_name
)
3454 struct nfs_server
*server
= NFS_SERVER(old_dir
);
3455 struct nfs_renameargs arg
= {
3456 .old_dir
= NFS_FH(old_dir
),
3457 .new_dir
= NFS_FH(new_dir
),
3458 .old_name
= old_name
,
3459 .new_name
= new_name
,
3461 struct nfs_renameres res
= {
3464 struct rpc_message msg
= {
3465 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
3469 int status
= -ENOMEM
;
3471 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3473 update_changeattr(old_dir
, &res
.old_cinfo
);
3474 update_changeattr(new_dir
, &res
.new_cinfo
);
3479 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3480 struct inode
*new_dir
, struct qstr
*new_name
)
3482 struct nfs4_exception exception
= { };
3485 err
= _nfs4_proc_rename(old_dir
, old_name
,
3487 trace_nfs4_rename(old_dir
, old_name
, new_dir
, new_name
, err
);
3488 err
= nfs4_handle_exception(NFS_SERVER(old_dir
), err
,
3490 } while (exception
.retry
);
3494 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3496 struct nfs_server
*server
= NFS_SERVER(inode
);
3497 struct nfs4_link_arg arg
= {
3498 .fh
= NFS_FH(inode
),
3499 .dir_fh
= NFS_FH(dir
),
3501 .bitmask
= server
->attr_bitmask
,
3503 struct nfs4_link_res res
= {
3507 struct rpc_message msg
= {
3508 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
3512 int status
= -ENOMEM
;
3514 res
.fattr
= nfs_alloc_fattr();
3515 if (res
.fattr
== NULL
)
3518 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3519 if (IS_ERR(res
.label
)) {
3520 status
= PTR_ERR(res
.label
);
3523 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
3525 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3527 update_changeattr(dir
, &res
.cinfo
);
3528 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
3530 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
3534 nfs4_label_free(res
.label
);
3537 nfs_free_fattr(res
.fattr
);
3541 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3543 struct nfs4_exception exception
= { };
3546 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3547 _nfs4_proc_link(inode
, dir
, name
),
3549 } while (exception
.retry
);
3553 struct nfs4_createdata
{
3554 struct rpc_message msg
;
3555 struct nfs4_create_arg arg
;
3556 struct nfs4_create_res res
;
3558 struct nfs_fattr fattr
;
3559 struct nfs4_label
*label
;
3562 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
3563 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
3565 struct nfs4_createdata
*data
;
3567 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3569 struct nfs_server
*server
= NFS_SERVER(dir
);
3571 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3572 if (IS_ERR(data
->label
))
3575 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
3576 data
->msg
.rpc_argp
= &data
->arg
;
3577 data
->msg
.rpc_resp
= &data
->res
;
3578 data
->arg
.dir_fh
= NFS_FH(dir
);
3579 data
->arg
.server
= server
;
3580 data
->arg
.name
= name
;
3581 data
->arg
.attrs
= sattr
;
3582 data
->arg
.ftype
= ftype
;
3583 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
3584 data
->res
.server
= server
;
3585 data
->res
.fh
= &data
->fh
;
3586 data
->res
.fattr
= &data
->fattr
;
3587 data
->res
.label
= data
->label
;
3588 nfs_fattr_init(data
->res
.fattr
);
3596 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
3598 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
3599 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
3601 update_changeattr(dir
, &data
->res
.dir_cinfo
);
3602 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
3607 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
3609 nfs4_label_free(data
->label
);
3613 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3614 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
3615 struct nfs4_label
*label
)
3617 struct nfs4_createdata
*data
;
3618 int status
= -ENAMETOOLONG
;
3620 if (len
> NFS4_MAXPATHLEN
)
3624 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
3628 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
3629 data
->arg
.u
.symlink
.pages
= &page
;
3630 data
->arg
.u
.symlink
.len
= len
;
3631 data
->arg
.label
= label
;
3633 status
= nfs4_do_create(dir
, dentry
, data
);
3635 nfs4_free_createdata(data
);
3640 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3641 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
3643 struct nfs4_exception exception
= { };
3644 struct nfs4_label l
, *label
= NULL
;
3647 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3650 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
3651 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
3652 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3654 } while (exception
.retry
);
3656 nfs4_label_release_security(label
);
3660 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3661 struct iattr
*sattr
, struct nfs4_label
*label
)
3663 struct nfs4_createdata
*data
;
3664 int status
= -ENOMEM
;
3666 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
3670 data
->arg
.label
= label
;
3671 status
= nfs4_do_create(dir
, dentry
, data
);
3673 nfs4_free_createdata(data
);
3678 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3679 struct iattr
*sattr
)
3681 struct nfs4_exception exception
= { };
3682 struct nfs4_label l
, *label
= NULL
;
3685 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3687 sattr
->ia_mode
&= ~current_umask();
3689 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
3690 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
3691 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3693 } while (exception
.retry
);
3694 nfs4_label_release_security(label
);
3699 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3700 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3702 struct inode
*dir
= dentry
->d_inode
;
3703 struct nfs4_readdir_arg args
= {
3708 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
3711 struct nfs4_readdir_res res
;
3712 struct rpc_message msg
= {
3713 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
3720 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
3721 dentry
->d_parent
->d_name
.name
,
3722 dentry
->d_name
.name
,
3723 (unsigned long long)cookie
);
3724 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
3725 res
.pgbase
= args
.pgbase
;
3726 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3728 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
3729 status
+= args
.pgbase
;
3732 nfs_invalidate_atime(dir
);
3734 dprintk("%s: returns %d\n", __func__
, status
);
3738 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3739 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3741 struct nfs4_exception exception
= { };
3744 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
3745 pages
, count
, plus
);
3746 trace_nfs4_readdir(dentry
->d_inode
, err
);
3747 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
), err
,
3749 } while (exception
.retry
);
3753 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3754 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
3756 struct nfs4_createdata
*data
;
3757 int mode
= sattr
->ia_mode
;
3758 int status
= -ENOMEM
;
3760 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
3765 data
->arg
.ftype
= NF4FIFO
;
3766 else if (S_ISBLK(mode
)) {
3767 data
->arg
.ftype
= NF4BLK
;
3768 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3769 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3771 else if (S_ISCHR(mode
)) {
3772 data
->arg
.ftype
= NF4CHR
;
3773 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3774 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3775 } else if (!S_ISSOCK(mode
)) {
3780 data
->arg
.label
= label
;
3781 status
= nfs4_do_create(dir
, dentry
, data
);
3783 nfs4_free_createdata(data
);
3788 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3789 struct iattr
*sattr
, dev_t rdev
)
3791 struct nfs4_exception exception
= { };
3792 struct nfs4_label l
, *label
= NULL
;
3795 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3797 sattr
->ia_mode
&= ~current_umask();
3799 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
3800 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
3801 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3803 } while (exception
.retry
);
3805 nfs4_label_release_security(label
);
3810 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3811 struct nfs_fsstat
*fsstat
)
3813 struct nfs4_statfs_arg args
= {
3815 .bitmask
= server
->attr_bitmask
,
3817 struct nfs4_statfs_res res
= {
3820 struct rpc_message msg
= {
3821 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
3826 nfs_fattr_init(fsstat
->fattr
);
3827 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3830 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
3832 struct nfs4_exception exception
= { };
3835 err
= nfs4_handle_exception(server
,
3836 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
3838 } while (exception
.retry
);
3842 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3843 struct nfs_fsinfo
*fsinfo
)
3845 struct nfs4_fsinfo_arg args
= {
3847 .bitmask
= server
->attr_bitmask
,
3849 struct nfs4_fsinfo_res res
= {
3852 struct rpc_message msg
= {
3853 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3858 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3861 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3863 struct nfs4_exception exception
= { };
3864 unsigned long now
= jiffies
;
3868 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3869 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
3871 struct nfs_client
*clp
= server
->nfs_client
;
3873 spin_lock(&clp
->cl_lock
);
3874 clp
->cl_lease_time
= fsinfo
->lease_time
* HZ
;
3875 clp
->cl_last_renewal
= now
;
3876 spin_unlock(&clp
->cl_lock
);
3879 err
= nfs4_handle_exception(server
, err
, &exception
);
3880 } while (exception
.retry
);
3884 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3888 nfs_fattr_init(fsinfo
->fattr
);
3889 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3891 /* block layout checks this! */
3892 server
->pnfs_blksize
= fsinfo
->blksize
;
3893 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
->layouttype
);
3899 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3900 struct nfs_pathconf
*pathconf
)
3902 struct nfs4_pathconf_arg args
= {
3904 .bitmask
= server
->attr_bitmask
,
3906 struct nfs4_pathconf_res res
= {
3907 .pathconf
= pathconf
,
3909 struct rpc_message msg
= {
3910 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3915 /* None of the pathconf attributes are mandatory to implement */
3916 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3917 memset(pathconf
, 0, sizeof(*pathconf
));
3921 nfs_fattr_init(pathconf
->fattr
);
3922 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3925 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3926 struct nfs_pathconf
*pathconf
)
3928 struct nfs4_exception exception
= { };
3932 err
= nfs4_handle_exception(server
,
3933 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3935 } while (exception
.retry
);
3939 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
3940 const struct nfs_open_context
*ctx
,
3941 const struct nfs_lock_context
*l_ctx
,
3944 const struct nfs_lockowner
*lockowner
= NULL
;
3947 lockowner
= &l_ctx
->lockowner
;
3948 return nfs4_select_rw_stateid(stateid
, ctx
->state
, fmode
, lockowner
);
3950 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
3952 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
3953 const struct nfs_open_context
*ctx
,
3954 const struct nfs_lock_context
*l_ctx
,
3957 nfs4_stateid current_stateid
;
3959 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
))
3961 return nfs4_stateid_match(stateid
, ¤t_stateid
);
3964 static bool nfs4_error_stateid_expired(int err
)
3967 case -NFS4ERR_DELEG_REVOKED
:
3968 case -NFS4ERR_ADMIN_REVOKED
:
3969 case -NFS4ERR_BAD_STATEID
:
3970 case -NFS4ERR_STALE_STATEID
:
3971 case -NFS4ERR_OLD_STATEID
:
3972 case -NFS4ERR_OPENMODE
:
3973 case -NFS4ERR_EXPIRED
:
3979 void __nfs4_read_done_cb(struct nfs_read_data
*data
)
3981 nfs_invalidate_atime(data
->header
->inode
);
3984 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_read_data
*data
)
3986 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
3988 trace_nfs4_read(data
, task
->tk_status
);
3989 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3990 rpc_restart_call_prepare(task
);
3994 __nfs4_read_done_cb(data
);
3995 if (task
->tk_status
> 0)
3996 renew_lease(server
, data
->timestamp
);
4000 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4001 struct nfs_readargs
*args
)
4004 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4005 nfs4_stateid_is_current(&args
->stateid
,
4010 rpc_restart_call_prepare(task
);
4014 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
4017 dprintk("--> %s\n", __func__
);
4019 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4021 if (nfs4_read_stateid_changed(task
, &data
->args
))
4023 return data
->read_done_cb
? data
->read_done_cb(task
, data
) :
4024 nfs4_read_done_cb(task
, data
);
4027 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
4029 data
->timestamp
= jiffies
;
4030 data
->read_done_cb
= nfs4_read_done_cb
;
4031 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4032 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
4035 static int nfs4_proc_read_rpc_prepare(struct rpc_task
*task
, struct nfs_read_data
*data
)
4037 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
4038 &data
->args
.seq_args
,
4042 if (nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
4043 data
->args
.lock_context
, FMODE_READ
) == -EIO
)
4045 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &data
->args
.context
->flags
)))
4050 static int nfs4_write_done_cb(struct rpc_task
*task
, struct nfs_write_data
*data
)
4052 struct inode
*inode
= data
->header
->inode
;
4054 trace_nfs4_write(data
, task
->tk_status
);
4055 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
4056 rpc_restart_call_prepare(task
);
4059 if (task
->tk_status
>= 0) {
4060 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
4061 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
4066 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4067 struct nfs_writeargs
*args
)
4070 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4071 nfs4_stateid_is_current(&args
->stateid
,
4076 rpc_restart_call_prepare(task
);
4080 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
4082 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4084 if (nfs4_write_stateid_changed(task
, &data
->args
))
4086 return data
->write_done_cb
? data
->write_done_cb(task
, data
) :
4087 nfs4_write_done_cb(task
, data
);
4091 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data
*data
)
4093 const struct nfs_pgio_header
*hdr
= data
->header
;
4095 /* Don't request attributes for pNFS or O_DIRECT writes */
4096 if (data
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4098 /* Otherwise, request attributes if and only if we don't hold
4101 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4104 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
4106 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
4108 if (!nfs4_write_need_cache_consistency_data(data
)) {
4109 data
->args
.bitmask
= NULL
;
4110 data
->res
.fattr
= NULL
;
4112 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
4114 if (!data
->write_done_cb
)
4115 data
->write_done_cb
= nfs4_write_done_cb
;
4116 data
->res
.server
= server
;
4117 data
->timestamp
= jiffies
;
4119 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4120 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4123 static int nfs4_proc_write_rpc_prepare(struct rpc_task
*task
, struct nfs_write_data
*data
)
4125 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
4126 &data
->args
.seq_args
,
4130 if (nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
4131 data
->args
.lock_context
, FMODE_WRITE
) == -EIO
)
4133 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &data
->args
.context
->flags
)))
4138 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4140 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4141 &data
->args
.seq_args
,
4146 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4148 struct inode
*inode
= data
->inode
;
4150 trace_nfs4_commit(data
, task
->tk_status
);
4151 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
4152 rpc_restart_call_prepare(task
);
4158 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4160 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4162 return data
->commit_done_cb(task
, data
);
4165 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4167 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4169 if (data
->commit_done_cb
== NULL
)
4170 data
->commit_done_cb
= nfs4_commit_done_cb
;
4171 data
->res
.server
= server
;
4172 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4173 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4176 struct nfs4_renewdata
{
4177 struct nfs_client
*client
;
4178 unsigned long timestamp
;
4182 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4183 * standalone procedure for queueing an asynchronous RENEW.
4185 static void nfs4_renew_release(void *calldata
)
4187 struct nfs4_renewdata
*data
= calldata
;
4188 struct nfs_client
*clp
= data
->client
;
4190 if (atomic_read(&clp
->cl_count
) > 1)
4191 nfs4_schedule_state_renewal(clp
);
4192 nfs_put_client(clp
);
4196 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4198 struct nfs4_renewdata
*data
= calldata
;
4199 struct nfs_client
*clp
= data
->client
;
4200 unsigned long timestamp
= data
->timestamp
;
4202 trace_nfs4_renew_async(clp
, task
->tk_status
);
4203 if (task
->tk_status
< 0) {
4204 /* Unless we're shutting down, schedule state recovery! */
4205 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4207 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4208 nfs4_schedule_lease_recovery(clp
);
4211 nfs4_schedule_path_down_recovery(clp
);
4213 do_renew_lease(clp
, timestamp
);
4216 static const struct rpc_call_ops nfs4_renew_ops
= {
4217 .rpc_call_done
= nfs4_renew_done
,
4218 .rpc_release
= nfs4_renew_release
,
4221 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4223 struct rpc_message msg
= {
4224 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4228 struct nfs4_renewdata
*data
;
4230 if (renew_flags
== 0)
4232 if (!atomic_inc_not_zero(&clp
->cl_count
))
4234 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4238 data
->timestamp
= jiffies
;
4239 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4240 &nfs4_renew_ops
, data
);
4243 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4245 struct rpc_message msg
= {
4246 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4250 unsigned long now
= jiffies
;
4253 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4256 do_renew_lease(clp
, now
);
4260 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4262 return (server
->caps
& NFS_CAP_ACLS
)
4263 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
4264 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
4267 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4268 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4271 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4273 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4274 struct page
**pages
, unsigned int *pgbase
)
4276 struct page
*newpage
, **spages
;
4282 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4283 newpage
= alloc_page(GFP_KERNEL
);
4285 if (newpage
== NULL
)
4287 memcpy(page_address(newpage
), buf
, len
);
4292 } while (buflen
!= 0);
4298 __free_page(spages
[rc
-1]);
4302 struct nfs4_cached_acl
{
4308 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4310 struct nfs_inode
*nfsi
= NFS_I(inode
);
4312 spin_lock(&inode
->i_lock
);
4313 kfree(nfsi
->nfs4_acl
);
4314 nfsi
->nfs4_acl
= acl
;
4315 spin_unlock(&inode
->i_lock
);
4318 static void nfs4_zap_acl_attr(struct inode
*inode
)
4320 nfs4_set_cached_acl(inode
, NULL
);
4323 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4325 struct nfs_inode
*nfsi
= NFS_I(inode
);
4326 struct nfs4_cached_acl
*acl
;
4329 spin_lock(&inode
->i_lock
);
4330 acl
= nfsi
->nfs4_acl
;
4333 if (buf
== NULL
) /* user is just asking for length */
4335 if (acl
->cached
== 0)
4337 ret
= -ERANGE
; /* see getxattr(2) man page */
4338 if (acl
->len
> buflen
)
4340 memcpy(buf
, acl
->data
, acl
->len
);
4344 spin_unlock(&inode
->i_lock
);
4348 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4350 struct nfs4_cached_acl
*acl
;
4351 size_t buflen
= sizeof(*acl
) + acl_len
;
4353 if (buflen
<= PAGE_SIZE
) {
4354 acl
= kmalloc(buflen
, GFP_KERNEL
);
4358 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4360 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4367 nfs4_set_cached_acl(inode
, acl
);
4371 * The getxattr API returns the required buffer length when called with a
4372 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4373 * the required buf. On a NULL buf, we send a page of data to the server
4374 * guessing that the ACL request can be serviced by a page. If so, we cache
4375 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4376 * the cache. If not so, we throw away the page, and cache the required
4377 * length. The next getxattr call will then produce another round trip to
4378 * the server, this time with the input buf of the required size.
4380 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4382 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4383 struct nfs_getaclargs args
= {
4384 .fh
= NFS_FH(inode
),
4388 struct nfs_getaclres res
= {
4391 struct rpc_message msg
= {
4392 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4396 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4397 int ret
= -ENOMEM
, i
;
4399 /* As long as we're doing a round trip to the server anyway,
4400 * let's be prepared for a page of acl data. */
4403 if (npages
> ARRAY_SIZE(pages
))
4406 for (i
= 0; i
< npages
; i
++) {
4407 pages
[i
] = alloc_page(GFP_KERNEL
);
4412 /* for decoding across pages */
4413 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
4414 if (!res
.acl_scratch
)
4417 args
.acl_len
= npages
* PAGE_SIZE
;
4418 args
.acl_pgbase
= 0;
4420 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4421 __func__
, buf
, buflen
, npages
, args
.acl_len
);
4422 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
4423 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4427 /* Handle the case where the passed-in buffer is too short */
4428 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
4429 /* Did the user only issue a request for the acl length? */
4435 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
4437 if (res
.acl_len
> buflen
) {
4441 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
4446 for (i
= 0; i
< npages
; i
++)
4448 __free_page(pages
[i
]);
4449 if (res
.acl_scratch
)
4450 __free_page(res
.acl_scratch
);
4454 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4456 struct nfs4_exception exception
= { };
4459 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
4460 trace_nfs4_get_acl(inode
, ret
);
4463 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
4464 } while (exception
.retry
);
4468 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
4470 struct nfs_server
*server
= NFS_SERVER(inode
);
4473 if (!nfs4_server_supports_acls(server
))
4475 ret
= nfs_revalidate_inode(server
, inode
);
4478 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
4479 nfs_zap_acl_cache(inode
);
4480 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
4482 /* -ENOENT is returned if there is no ACL or if there is an ACL
4483 * but no cached acl data, just the acl length */
4485 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
4488 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4490 struct nfs_server
*server
= NFS_SERVER(inode
);
4491 struct page
*pages
[NFS4ACL_MAXPAGES
];
4492 struct nfs_setaclargs arg
= {
4493 .fh
= NFS_FH(inode
),
4497 struct nfs_setaclres res
;
4498 struct rpc_message msg
= {
4499 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
4503 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4506 if (!nfs4_server_supports_acls(server
))
4508 if (npages
> ARRAY_SIZE(pages
))
4510 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
4513 nfs4_inode_return_delegation(inode
);
4514 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4517 * Free each page after tx, so the only ref left is
4518 * held by the network stack
4521 put_page(pages
[i
-1]);
4524 * Acl update can result in inode attribute update.
4525 * so mark the attribute cache invalid.
4527 spin_lock(&inode
->i_lock
);
4528 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
4529 spin_unlock(&inode
->i_lock
);
4530 nfs_access_zap_cache(inode
);
4531 nfs_zap_acl_cache(inode
);
4535 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4537 struct nfs4_exception exception
= { };
4540 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
4541 trace_nfs4_set_acl(inode
, err
);
4542 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4544 } while (exception
.retry
);
4548 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4549 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
4552 struct nfs_server
*server
= NFS_SERVER(inode
);
4553 struct nfs_fattr fattr
;
4554 struct nfs4_label label
= {0, 0, buflen
, buf
};
4556 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4557 struct nfs4_getattr_arg args
= {
4558 .fh
= NFS_FH(inode
),
4561 struct nfs4_getattr_res res
= {
4566 struct rpc_message msg
= {
4567 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
4573 nfs_fattr_init(&fattr
);
4575 ret
= rpc_call_sync(server
->client
, &msg
, 0);
4578 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
4580 if (buflen
< label
.len
)
4585 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
4588 struct nfs4_exception exception
= { };
4591 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4595 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
4596 trace_nfs4_get_security_label(inode
, err
);
4597 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4599 } while (exception
.retry
);
4603 static int _nfs4_do_set_security_label(struct inode
*inode
,
4604 struct nfs4_label
*ilabel
,
4605 struct nfs_fattr
*fattr
,
4606 struct nfs4_label
*olabel
)
4609 struct iattr sattr
= {0};
4610 struct nfs_server
*server
= NFS_SERVER(inode
);
4611 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4612 struct nfs_setattrargs args
= {
4613 .fh
= NFS_FH(inode
),
4619 struct nfs_setattrres res
= {
4624 struct rpc_message msg
= {
4625 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
4631 nfs4_stateid_copy(&args
.stateid
, &zero_stateid
);
4633 status
= rpc_call_sync(server
->client
, &msg
, 0);
4635 dprintk("%s failed: %d\n", __func__
, status
);
4640 static int nfs4_do_set_security_label(struct inode
*inode
,
4641 struct nfs4_label
*ilabel
,
4642 struct nfs_fattr
*fattr
,
4643 struct nfs4_label
*olabel
)
4645 struct nfs4_exception exception
= { };
4649 err
= _nfs4_do_set_security_label(inode
, ilabel
,
4651 trace_nfs4_set_security_label(inode
, err
);
4652 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4654 } while (exception
.retry
);
4659 nfs4_set_security_label(struct dentry
*dentry
, const void *buf
, size_t buflen
)
4661 struct nfs4_label ilabel
, *olabel
= NULL
;
4662 struct nfs_fattr fattr
;
4663 struct rpc_cred
*cred
;
4664 struct inode
*inode
= dentry
->d_inode
;
4667 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4670 nfs_fattr_init(&fattr
);
4674 ilabel
.label
= (char *)buf
;
4675 ilabel
.len
= buflen
;
4677 cred
= rpc_lookup_cred();
4679 return PTR_ERR(cred
);
4681 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
4682 if (IS_ERR(olabel
)) {
4683 status
= -PTR_ERR(olabel
);
4687 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
4689 nfs_setsecurity(inode
, &fattr
, olabel
);
4691 nfs4_label_free(olabel
);
4696 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4700 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
4702 struct nfs_client
*clp
= server
->nfs_client
;
4704 if (task
->tk_status
>= 0)
4706 switch(task
->tk_status
) {
4707 case -NFS4ERR_DELEG_REVOKED
:
4708 case -NFS4ERR_ADMIN_REVOKED
:
4709 case -NFS4ERR_BAD_STATEID
:
4712 nfs_remove_bad_delegation(state
->inode
);
4713 case -NFS4ERR_OPENMODE
:
4716 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4717 goto stateid_invalid
;
4718 goto wait_on_recovery
;
4719 case -NFS4ERR_EXPIRED
:
4720 if (state
!= NULL
) {
4721 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4722 goto stateid_invalid
;
4724 case -NFS4ERR_STALE_STATEID
:
4725 case -NFS4ERR_STALE_CLIENTID
:
4726 nfs4_schedule_lease_recovery(clp
);
4727 goto wait_on_recovery
;
4728 #if defined(CONFIG_NFS_V4_1)
4729 case -NFS4ERR_BADSESSION
:
4730 case -NFS4ERR_BADSLOT
:
4731 case -NFS4ERR_BAD_HIGH_SLOT
:
4732 case -NFS4ERR_DEADSESSION
:
4733 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4734 case -NFS4ERR_SEQ_FALSE_RETRY
:
4735 case -NFS4ERR_SEQ_MISORDERED
:
4736 dprintk("%s ERROR %d, Reset session\n", __func__
,
4738 nfs4_schedule_session_recovery(clp
->cl_session
, task
->tk_status
);
4739 task
->tk_status
= 0;
4741 #endif /* CONFIG_NFS_V4_1 */
4742 case -NFS4ERR_DELAY
:
4743 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
4744 case -NFS4ERR_GRACE
:
4745 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
4746 task
->tk_status
= 0;
4748 case -NFS4ERR_RETRY_UNCACHED_REP
:
4749 case -NFS4ERR_OLD_STATEID
:
4750 task
->tk_status
= 0;
4753 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
4756 task
->tk_status
= -EIO
;
4759 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
4760 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
4761 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
4762 task
->tk_status
= 0;
4766 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
4767 nfs4_verifier
*bootverf
)
4771 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
4772 /* An impossible timestamp guarantees this value
4773 * will never match a generated boot time. */
4775 verf
[1] = cpu_to_be32(NSEC_PER_SEC
+ 1);
4777 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
4778 verf
[0] = cpu_to_be32(nn
->boot_time
.tv_sec
);
4779 verf
[1] = cpu_to_be32(nn
->boot_time
.tv_nsec
);
4781 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
4785 nfs4_init_nonuniform_client_string(const struct nfs_client
*clp
,
4786 char *buf
, size_t len
)
4788 unsigned int result
;
4791 result
= scnprintf(buf
, len
, "Linux NFSv4.0 %s/%s %s",
4793 rpc_peeraddr2str(clp
->cl_rpcclient
,
4795 rpc_peeraddr2str(clp
->cl_rpcclient
,
4796 RPC_DISPLAY_PROTO
));
4802 nfs4_init_uniform_client_string(const struct nfs_client
*clp
,
4803 char *buf
, size_t len
)
4805 const char *nodename
= clp
->cl_rpcclient
->cl_nodename
;
4807 if (nfs4_client_id_uniquifier
[0] != '\0')
4808 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s/%s",
4809 clp
->rpc_ops
->version
,
4810 clp
->cl_minorversion
,
4811 nfs4_client_id_uniquifier
,
4813 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s",
4814 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
4819 * nfs4_proc_setclientid - Negotiate client ID
4820 * @clp: state data structure
4821 * @program: RPC program for NFSv4 callback service
4822 * @port: IP port number for NFS4 callback service
4823 * @cred: RPC credential to use for this call
4824 * @res: where to place the result
4826 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4828 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
4829 unsigned short port
, struct rpc_cred
*cred
,
4830 struct nfs4_setclientid_res
*res
)
4832 nfs4_verifier sc_verifier
;
4833 struct nfs4_setclientid setclientid
= {
4834 .sc_verifier
= &sc_verifier
,
4836 .sc_cb_ident
= clp
->cl_cb_ident
,
4838 struct rpc_message msg
= {
4839 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
4840 .rpc_argp
= &setclientid
,
4846 /* nfs_client_id4 */
4847 nfs4_init_boot_verifier(clp
, &sc_verifier
);
4848 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
4849 setclientid
.sc_name_len
=
4850 nfs4_init_uniform_client_string(clp
,
4851 setclientid
.sc_name
,
4852 sizeof(setclientid
.sc_name
));
4854 setclientid
.sc_name_len
=
4855 nfs4_init_nonuniform_client_string(clp
,
4856 setclientid
.sc_name
,
4857 sizeof(setclientid
.sc_name
));
4860 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
4861 sizeof(setclientid
.sc_netid
), "%s",
4862 rpc_peeraddr2str(clp
->cl_rpcclient
,
4863 RPC_DISPLAY_NETID
));
4865 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
4866 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
4867 clp
->cl_ipaddr
, port
>> 8, port
& 255);
4869 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4870 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4871 setclientid
.sc_name_len
, setclientid
.sc_name
);
4872 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4873 trace_nfs4_setclientid(clp
, status
);
4874 dprintk("NFS reply setclientid: %d\n", status
);
4879 * nfs4_proc_setclientid_confirm - Confirm client ID
4880 * @clp: state data structure
4881 * @res: result of a previous SETCLIENTID
4882 * @cred: RPC credential to use for this call
4884 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4886 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
4887 struct nfs4_setclientid_res
*arg
,
4888 struct rpc_cred
*cred
)
4890 struct rpc_message msg
= {
4891 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
4897 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4898 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4900 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4901 trace_nfs4_setclientid_confirm(clp
, status
);
4902 dprintk("NFS reply setclientid_confirm: %d\n", status
);
4906 struct nfs4_delegreturndata
{
4907 struct nfs4_delegreturnargs args
;
4908 struct nfs4_delegreturnres res
;
4910 nfs4_stateid stateid
;
4911 unsigned long timestamp
;
4912 struct nfs_fattr fattr
;
4916 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
4918 struct nfs4_delegreturndata
*data
= calldata
;
4920 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4923 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
4924 switch (task
->tk_status
) {
4925 case -NFS4ERR_STALE_STATEID
:
4926 case -NFS4ERR_EXPIRED
:
4928 renew_lease(data
->res
.server
, data
->timestamp
);
4931 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
4933 rpc_restart_call_prepare(task
);
4937 data
->rpc_status
= task
->tk_status
;
4940 static void nfs4_delegreturn_release(void *calldata
)
4945 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
4947 struct nfs4_delegreturndata
*d_data
;
4949 d_data
= (struct nfs4_delegreturndata
*)data
;
4951 nfs4_setup_sequence(d_data
->res
.server
,
4952 &d_data
->args
.seq_args
,
4953 &d_data
->res
.seq_res
,
4957 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
4958 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
4959 .rpc_call_done
= nfs4_delegreturn_done
,
4960 .rpc_release
= nfs4_delegreturn_release
,
4963 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
4965 struct nfs4_delegreturndata
*data
;
4966 struct nfs_server
*server
= NFS_SERVER(inode
);
4967 struct rpc_task
*task
;
4968 struct rpc_message msg
= {
4969 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
4972 struct rpc_task_setup task_setup_data
= {
4973 .rpc_client
= server
->client
,
4974 .rpc_message
= &msg
,
4975 .callback_ops
= &nfs4_delegreturn_ops
,
4976 .flags
= RPC_TASK_ASYNC
,
4980 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
4983 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4984 data
->args
.fhandle
= &data
->fh
;
4985 data
->args
.stateid
= &data
->stateid
;
4986 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
4987 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
4988 nfs4_stateid_copy(&data
->stateid
, stateid
);
4989 data
->res
.fattr
= &data
->fattr
;
4990 data
->res
.server
= server
;
4991 nfs_fattr_init(data
->res
.fattr
);
4992 data
->timestamp
= jiffies
;
4993 data
->rpc_status
= 0;
4995 task_setup_data
.callback_data
= data
;
4996 msg
.rpc_argp
= &data
->args
;
4997 msg
.rpc_resp
= &data
->res
;
4998 task
= rpc_run_task(&task_setup_data
);
5000 return PTR_ERR(task
);
5003 status
= nfs4_wait_for_completion_rpc_task(task
);
5006 status
= data
->rpc_status
;
5008 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5010 nfs_refresh_inode(inode
, &data
->fattr
);
5016 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5018 struct nfs_server
*server
= NFS_SERVER(inode
);
5019 struct nfs4_exception exception
= { };
5022 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5023 trace_nfs4_delegreturn(inode
, err
);
5025 case -NFS4ERR_STALE_STATEID
:
5026 case -NFS4ERR_EXPIRED
:
5030 err
= nfs4_handle_exception(server
, err
, &exception
);
5031 } while (exception
.retry
);
5035 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5036 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5039 * sleep, with exponential backoff, and retry the LOCK operation.
5041 static unsigned long
5042 nfs4_set_lock_task_retry(unsigned long timeout
)
5044 freezable_schedule_timeout_killable_unsafe(timeout
);
5046 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
5047 return NFS4_LOCK_MAXTIMEOUT
;
5051 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5053 struct inode
*inode
= state
->inode
;
5054 struct nfs_server
*server
= NFS_SERVER(inode
);
5055 struct nfs_client
*clp
= server
->nfs_client
;
5056 struct nfs_lockt_args arg
= {
5057 .fh
= NFS_FH(inode
),
5060 struct nfs_lockt_res res
= {
5063 struct rpc_message msg
= {
5064 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5067 .rpc_cred
= state
->owner
->so_cred
,
5069 struct nfs4_lock_state
*lsp
;
5072 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5073 status
= nfs4_set_lock_state(state
, request
);
5076 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5077 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5078 arg
.lock_owner
.s_dev
= server
->s_dev
;
5079 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5082 request
->fl_type
= F_UNLCK
;
5084 case -NFS4ERR_DENIED
:
5087 request
->fl_ops
->fl_release_private(request
);
5092 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5094 struct nfs4_exception exception
= { };
5098 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5099 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5100 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5102 } while (exception
.retry
);
5106 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
5109 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
5111 res
= posix_lock_file_wait(file
, fl
);
5114 res
= flock_lock_file_wait(file
, fl
);
5122 struct nfs4_unlockdata
{
5123 struct nfs_locku_args arg
;
5124 struct nfs_locku_res res
;
5125 struct nfs4_lock_state
*lsp
;
5126 struct nfs_open_context
*ctx
;
5127 struct file_lock fl
;
5128 const struct nfs_server
*server
;
5129 unsigned long timestamp
;
5132 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5133 struct nfs_open_context
*ctx
,
5134 struct nfs4_lock_state
*lsp
,
5135 struct nfs_seqid
*seqid
)
5137 struct nfs4_unlockdata
*p
;
5138 struct inode
*inode
= lsp
->ls_state
->inode
;
5140 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5143 p
->arg
.fh
= NFS_FH(inode
);
5145 p
->arg
.seqid
= seqid
;
5146 p
->res
.seqid
= seqid
;
5147 p
->arg
.stateid
= &lsp
->ls_stateid
;
5149 atomic_inc(&lsp
->ls_count
);
5150 /* Ensure we don't close file until we're done freeing locks! */
5151 p
->ctx
= get_nfs_open_context(ctx
);
5152 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5153 p
->server
= NFS_SERVER(inode
);
5157 static void nfs4_locku_release_calldata(void *data
)
5159 struct nfs4_unlockdata
*calldata
= data
;
5160 nfs_free_seqid(calldata
->arg
.seqid
);
5161 nfs4_put_lock_state(calldata
->lsp
);
5162 put_nfs_open_context(calldata
->ctx
);
5166 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5168 struct nfs4_unlockdata
*calldata
= data
;
5170 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5172 switch (task
->tk_status
) {
5174 nfs4_stateid_copy(&calldata
->lsp
->ls_stateid
,
5175 &calldata
->res
.stateid
);
5176 renew_lease(calldata
->server
, calldata
->timestamp
);
5178 case -NFS4ERR_BAD_STATEID
:
5179 case -NFS4ERR_OLD_STATEID
:
5180 case -NFS4ERR_STALE_STATEID
:
5181 case -NFS4ERR_EXPIRED
:
5184 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
5185 rpc_restart_call_prepare(task
);
5187 nfs_release_seqid(calldata
->arg
.seqid
);
5190 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5192 struct nfs4_unlockdata
*calldata
= data
;
5194 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5196 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5197 /* Note: exit _without_ running nfs4_locku_done */
5200 calldata
->timestamp
= jiffies
;
5201 if (nfs4_setup_sequence(calldata
->server
,
5202 &calldata
->arg
.seq_args
,
5203 &calldata
->res
.seq_res
,
5205 nfs_release_seqid(calldata
->arg
.seqid
);
5208 task
->tk_action
= NULL
;
5210 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5213 static const struct rpc_call_ops nfs4_locku_ops
= {
5214 .rpc_call_prepare
= nfs4_locku_prepare
,
5215 .rpc_call_done
= nfs4_locku_done
,
5216 .rpc_release
= nfs4_locku_release_calldata
,
5219 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5220 struct nfs_open_context
*ctx
,
5221 struct nfs4_lock_state
*lsp
,
5222 struct nfs_seqid
*seqid
)
5224 struct nfs4_unlockdata
*data
;
5225 struct rpc_message msg
= {
5226 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5227 .rpc_cred
= ctx
->cred
,
5229 struct rpc_task_setup task_setup_data
= {
5230 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5231 .rpc_message
= &msg
,
5232 .callback_ops
= &nfs4_locku_ops
,
5233 .workqueue
= nfsiod_workqueue
,
5234 .flags
= RPC_TASK_ASYNC
,
5237 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5238 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5240 /* Ensure this is an unlock - when canceling a lock, the
5241 * canceled lock is passed in, and it won't be an unlock.
5243 fl
->fl_type
= F_UNLCK
;
5245 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5247 nfs_free_seqid(seqid
);
5248 return ERR_PTR(-ENOMEM
);
5251 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5252 msg
.rpc_argp
= &data
->arg
;
5253 msg
.rpc_resp
= &data
->res
;
5254 task_setup_data
.callback_data
= data
;
5255 return rpc_run_task(&task_setup_data
);
5258 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5260 struct inode
*inode
= state
->inode
;
5261 struct nfs4_state_owner
*sp
= state
->owner
;
5262 struct nfs_inode
*nfsi
= NFS_I(inode
);
5263 struct nfs_seqid
*seqid
;
5264 struct nfs4_lock_state
*lsp
;
5265 struct rpc_task
*task
;
5267 unsigned char fl_flags
= request
->fl_flags
;
5269 status
= nfs4_set_lock_state(state
, request
);
5270 /* Unlock _before_ we do the RPC call */
5271 request
->fl_flags
|= FL_EXISTS
;
5272 /* Exclude nfs_delegation_claim_locks() */
5273 mutex_lock(&sp
->so_delegreturn_mutex
);
5274 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5275 down_read(&nfsi
->rwsem
);
5276 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
5277 up_read(&nfsi
->rwsem
);
5278 mutex_unlock(&sp
->so_delegreturn_mutex
);
5281 up_read(&nfsi
->rwsem
);
5282 mutex_unlock(&sp
->so_delegreturn_mutex
);
5285 /* Is this a delegated lock? */
5286 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5287 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5289 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5293 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5294 status
= PTR_ERR(task
);
5297 status
= nfs4_wait_for_completion_rpc_task(task
);
5300 request
->fl_flags
= fl_flags
;
5301 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
5305 struct nfs4_lockdata
{
5306 struct nfs_lock_args arg
;
5307 struct nfs_lock_res res
;
5308 struct nfs4_lock_state
*lsp
;
5309 struct nfs_open_context
*ctx
;
5310 struct file_lock fl
;
5311 unsigned long timestamp
;
5314 struct nfs_server
*server
;
5317 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
5318 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
5321 struct nfs4_lockdata
*p
;
5322 struct inode
*inode
= lsp
->ls_state
->inode
;
5323 struct nfs_server
*server
= NFS_SERVER(inode
);
5325 p
= kzalloc(sizeof(*p
), gfp_mask
);
5329 p
->arg
.fh
= NFS_FH(inode
);
5331 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
5332 if (p
->arg
.open_seqid
== NULL
)
5334 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
5335 if (p
->arg
.lock_seqid
== NULL
)
5336 goto out_free_seqid
;
5337 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
5338 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5339 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5340 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
5341 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
5344 atomic_inc(&lsp
->ls_count
);
5345 p
->ctx
= get_nfs_open_context(ctx
);
5346 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5349 nfs_free_seqid(p
->arg
.open_seqid
);
5355 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
5357 struct nfs4_lockdata
*data
= calldata
;
5358 struct nfs4_state
*state
= data
->lsp
->ls_state
;
5360 dprintk("%s: begin!\n", __func__
);
5361 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
5363 /* Do we need to do an open_to_lock_owner? */
5364 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
5365 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
5366 goto out_release_lock_seqid
;
5368 data
->arg
.open_stateid
= &state
->open_stateid
;
5369 data
->arg
.new_lock_owner
= 1;
5370 data
->res
.open_seqid
= data
->arg
.open_seqid
;
5372 data
->arg
.new_lock_owner
= 0;
5373 if (!nfs4_valid_open_stateid(state
)) {
5374 data
->rpc_status
= -EBADF
;
5375 task
->tk_action
= NULL
;
5376 goto out_release_open_seqid
;
5378 data
->timestamp
= jiffies
;
5379 if (nfs4_setup_sequence(data
->server
,
5380 &data
->arg
.seq_args
,
5384 out_release_open_seqid
:
5385 nfs_release_seqid(data
->arg
.open_seqid
);
5386 out_release_lock_seqid
:
5387 nfs_release_seqid(data
->arg
.lock_seqid
);
5389 nfs4_sequence_done(task
, &data
->res
.seq_res
);
5390 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
5393 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
5395 struct nfs4_lockdata
*data
= calldata
;
5397 dprintk("%s: begin!\n", __func__
);
5399 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5402 data
->rpc_status
= task
->tk_status
;
5403 if (data
->arg
.new_lock_owner
!= 0) {
5404 if (data
->rpc_status
== 0)
5405 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
5409 if (data
->rpc_status
== 0) {
5410 nfs4_stateid_copy(&data
->lsp
->ls_stateid
, &data
->res
.stateid
);
5411 set_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
);
5412 renew_lease(NFS_SERVER(data
->ctx
->dentry
->d_inode
), data
->timestamp
);
5415 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
5418 static void nfs4_lock_release(void *calldata
)
5420 struct nfs4_lockdata
*data
= calldata
;
5422 dprintk("%s: begin!\n", __func__
);
5423 nfs_free_seqid(data
->arg
.open_seqid
);
5424 if (data
->cancelled
!= 0) {
5425 struct rpc_task
*task
;
5426 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
5427 data
->arg
.lock_seqid
);
5429 rpc_put_task_async(task
);
5430 dprintk("%s: cancelling lock!\n", __func__
);
5432 nfs_free_seqid(data
->arg
.lock_seqid
);
5433 nfs4_put_lock_state(data
->lsp
);
5434 put_nfs_open_context(data
->ctx
);
5436 dprintk("%s: done!\n", __func__
);
5439 static const struct rpc_call_ops nfs4_lock_ops
= {
5440 .rpc_call_prepare
= nfs4_lock_prepare
,
5441 .rpc_call_done
= nfs4_lock_done
,
5442 .rpc_release
= nfs4_lock_release
,
5445 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
5448 case -NFS4ERR_ADMIN_REVOKED
:
5449 case -NFS4ERR_BAD_STATEID
:
5450 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5451 if (new_lock_owner
!= 0 ||
5452 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
5453 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
5455 case -NFS4ERR_STALE_STATEID
:
5456 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5457 case -NFS4ERR_EXPIRED
:
5458 nfs4_schedule_lease_recovery(server
->nfs_client
);
5462 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
5464 struct nfs4_lockdata
*data
;
5465 struct rpc_task
*task
;
5466 struct rpc_message msg
= {
5467 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
5468 .rpc_cred
= state
->owner
->so_cred
,
5470 struct rpc_task_setup task_setup_data
= {
5471 .rpc_client
= NFS_CLIENT(state
->inode
),
5472 .rpc_message
= &msg
,
5473 .callback_ops
= &nfs4_lock_ops
,
5474 .workqueue
= nfsiod_workqueue
,
5475 .flags
= RPC_TASK_ASYNC
,
5479 dprintk("%s: begin!\n", __func__
);
5480 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
5481 fl
->fl_u
.nfs4_fl
.owner
,
5482 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
5486 data
->arg
.block
= 1;
5487 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5488 msg
.rpc_argp
= &data
->arg
;
5489 msg
.rpc_resp
= &data
->res
;
5490 task_setup_data
.callback_data
= data
;
5491 if (recovery_type
> NFS_LOCK_NEW
) {
5492 if (recovery_type
== NFS_LOCK_RECLAIM
)
5493 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
5494 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
5496 task
= rpc_run_task(&task_setup_data
);
5498 return PTR_ERR(task
);
5499 ret
= nfs4_wait_for_completion_rpc_task(task
);
5501 ret
= data
->rpc_status
;
5503 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
5504 data
->arg
.new_lock_owner
, ret
);
5506 data
->cancelled
= 1;
5508 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
5512 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
5514 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5515 struct nfs4_exception exception
= {
5516 .inode
= state
->inode
,
5521 /* Cache the lock if possible... */
5522 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5524 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
5525 trace_nfs4_lock_reclaim(request
, state
, F_SETLK
, err
);
5526 if (err
!= -NFS4ERR_DELAY
)
5528 nfs4_handle_exception(server
, err
, &exception
);
5529 } while (exception
.retry
);
5533 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5535 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5536 struct nfs4_exception exception
= {
5537 .inode
= state
->inode
,
5541 err
= nfs4_set_lock_state(state
, request
);
5544 if (!recover_lost_locks
) {
5545 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
5549 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5551 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
5552 trace_nfs4_lock_expired(request
, state
, F_SETLK
, err
);
5556 case -NFS4ERR_GRACE
:
5557 case -NFS4ERR_DELAY
:
5558 nfs4_handle_exception(server
, err
, &exception
);
5561 } while (exception
.retry
);
5566 #if defined(CONFIG_NFS_V4_1)
5568 * nfs41_check_expired_locks - possibly free a lock stateid
5570 * @state: NFSv4 state for an inode
5572 * Returns NFS_OK if recovery for this stateid is now finished.
5573 * Otherwise a negative NFS4ERR value is returned.
5575 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
5577 int status
, ret
= -NFS4ERR_BAD_STATEID
;
5578 struct nfs4_lock_state
*lsp
;
5579 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5581 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
5582 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
5583 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
5585 status
= nfs41_test_stateid(server
,
5588 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
5589 if (status
!= NFS_OK
) {
5590 /* Free the stateid unless the server
5591 * informs us the stateid is unrecognized. */
5592 if (status
!= -NFS4ERR_BAD_STATEID
)
5593 nfs41_free_stateid(server
,
5596 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
5605 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5607 int status
= NFS_OK
;
5609 if (test_bit(LK_STATE_IN_USE
, &state
->flags
))
5610 status
= nfs41_check_expired_locks(state
);
5611 if (status
!= NFS_OK
)
5612 status
= nfs4_lock_expired(state
, request
);
5617 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5619 struct nfs4_state_owner
*sp
= state
->owner
;
5620 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
5621 unsigned char fl_flags
= request
->fl_flags
;
5623 int status
= -ENOLCK
;
5625 if ((fl_flags
& FL_POSIX
) &&
5626 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
5628 /* Is this a delegated open? */
5629 status
= nfs4_set_lock_state(state
, request
);
5632 request
->fl_flags
|= FL_ACCESS
;
5633 status
= do_vfs_lock(request
->fl_file
, request
);
5636 down_read(&nfsi
->rwsem
);
5637 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
5638 /* Yes: cache locks! */
5639 /* ...but avoid races with delegation recall... */
5640 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
5641 status
= do_vfs_lock(request
->fl_file
, request
);
5644 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
5645 up_read(&nfsi
->rwsem
);
5646 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
5649 down_read(&nfsi
->rwsem
);
5650 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
)) {
5651 status
= -NFS4ERR_DELAY
;
5654 /* Note: we always want to sleep here! */
5655 request
->fl_flags
= fl_flags
| FL_SLEEP
;
5656 if (do_vfs_lock(request
->fl_file
, request
) < 0)
5657 printk(KERN_WARNING
"NFS: %s: VFS is out of sync with lock "
5658 "manager!\n", __func__
);
5660 up_read(&nfsi
->rwsem
);
5662 request
->fl_flags
= fl_flags
;
5666 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5668 struct nfs4_exception exception
= {
5670 .inode
= state
->inode
,
5675 err
= _nfs4_proc_setlk(state
, cmd
, request
);
5676 trace_nfs4_set_lock(request
, state
, cmd
, err
);
5677 if (err
== -NFS4ERR_DENIED
)
5679 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
5681 } while (exception
.retry
);
5686 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
5688 struct nfs_open_context
*ctx
;
5689 struct nfs4_state
*state
;
5690 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
5693 /* verify open state */
5694 ctx
= nfs_file_open_context(filp
);
5697 if (request
->fl_start
< 0 || request
->fl_end
< 0)
5700 if (IS_GETLK(cmd
)) {
5702 return nfs4_proc_getlk(state
, F_GETLK
, request
);
5706 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
5709 if (request
->fl_type
== F_UNLCK
) {
5711 return nfs4_proc_unlck(state
, cmd
, request
);
5718 * Don't rely on the VFS having checked the file open mode,
5719 * since it won't do this for flock() locks.
5721 switch (request
->fl_type
) {
5723 if (!(filp
->f_mode
& FMODE_READ
))
5727 if (!(filp
->f_mode
& FMODE_WRITE
))
5732 status
= nfs4_proc_setlk(state
, cmd
, request
);
5733 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
5735 timeout
= nfs4_set_lock_task_retry(timeout
);
5736 status
= -ERESTARTSYS
;
5739 } while(status
< 0);
5743 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
5745 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5748 err
= nfs4_set_lock_state(state
, fl
);
5751 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
5752 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
5755 struct nfs_release_lockowner_data
{
5756 struct nfs4_lock_state
*lsp
;
5757 struct nfs_server
*server
;
5758 struct nfs_release_lockowner_args args
;
5759 struct nfs4_sequence_args seq_args
;
5760 struct nfs4_sequence_res seq_res
;
5763 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
5765 struct nfs_release_lockowner_data
*data
= calldata
;
5766 nfs40_setup_sequence(data
->server
,
5767 &data
->seq_args
, &data
->seq_res
, task
);
5770 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
5772 struct nfs_release_lockowner_data
*data
= calldata
;
5773 nfs40_sequence_done(task
, &data
->seq_res
);
5776 static void nfs4_release_lockowner_release(void *calldata
)
5778 struct nfs_release_lockowner_data
*data
= calldata
;
5779 nfs4_free_lock_state(data
->server
, data
->lsp
);
5783 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
5784 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
5785 .rpc_call_done
= nfs4_release_lockowner_done
,
5786 .rpc_release
= nfs4_release_lockowner_release
,
5789 static int nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
5791 struct nfs_release_lockowner_data
*data
;
5792 struct rpc_message msg
= {
5793 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
5796 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
5799 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
5802 nfs4_init_sequence(&data
->seq_args
, &data
->seq_res
, 0);
5804 data
->server
= server
;
5805 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5806 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5807 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
5809 msg
.rpc_argp
= &data
->args
;
5810 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
5814 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5816 static int nfs4_xattr_set_nfs4_acl(struct dentry
*dentry
, const char *key
,
5817 const void *buf
, size_t buflen
,
5818 int flags
, int type
)
5820 if (strcmp(key
, "") != 0)
5823 return nfs4_proc_set_acl(dentry
->d_inode
, buf
, buflen
);
5826 static int nfs4_xattr_get_nfs4_acl(struct dentry
*dentry
, const char *key
,
5827 void *buf
, size_t buflen
, int type
)
5829 if (strcmp(key
, "") != 0)
5832 return nfs4_proc_get_acl(dentry
->d_inode
, buf
, buflen
);
5835 static size_t nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
, char *list
,
5836 size_t list_len
, const char *name
,
5837 size_t name_len
, int type
)
5839 size_t len
= sizeof(XATTR_NAME_NFSV4_ACL
);
5841 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
5844 if (list
&& len
<= list_len
)
5845 memcpy(list
, XATTR_NAME_NFSV4_ACL
, len
);
5849 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5850 static inline int nfs4_server_supports_labels(struct nfs_server
*server
)
5852 return server
->caps
& NFS_CAP_SECURITY_LABEL
;
5855 static int nfs4_xattr_set_nfs4_label(struct dentry
*dentry
, const char *key
,
5856 const void *buf
, size_t buflen
,
5857 int flags
, int type
)
5859 if (security_ismaclabel(key
))
5860 return nfs4_set_security_label(dentry
, buf
, buflen
);
5865 static int nfs4_xattr_get_nfs4_label(struct dentry
*dentry
, const char *key
,
5866 void *buf
, size_t buflen
, int type
)
5868 if (security_ismaclabel(key
))
5869 return nfs4_get_security_label(dentry
->d_inode
, buf
, buflen
);
5873 static size_t nfs4_xattr_list_nfs4_label(struct dentry
*dentry
, char *list
,
5874 size_t list_len
, const char *name
,
5875 size_t name_len
, int type
)
5879 if (nfs_server_capable(dentry
->d_inode
, NFS_CAP_SECURITY_LABEL
)) {
5880 len
= security_inode_listsecurity(dentry
->d_inode
, NULL
, 0);
5881 if (list
&& len
<= list_len
)
5882 security_inode_listsecurity(dentry
->d_inode
, list
, len
);
5887 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
5888 .prefix
= XATTR_SECURITY_PREFIX
,
5889 .list
= nfs4_xattr_list_nfs4_label
,
5890 .get
= nfs4_xattr_get_nfs4_label
,
5891 .set
= nfs4_xattr_set_nfs4_label
,
5897 * nfs_fhget will use either the mounted_on_fileid or the fileid
5899 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
5901 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
5902 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
5903 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
5904 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
5907 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
5908 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
5909 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
5913 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5914 const struct qstr
*name
,
5915 struct nfs4_fs_locations
*fs_locations
,
5918 struct nfs_server
*server
= NFS_SERVER(dir
);
5920 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
5922 struct nfs4_fs_locations_arg args
= {
5923 .dir_fh
= NFS_FH(dir
),
5928 struct nfs4_fs_locations_res res
= {
5929 .fs_locations
= fs_locations
,
5931 struct rpc_message msg
= {
5932 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
5938 dprintk("%s: start\n", __func__
);
5940 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5941 * is not supported */
5942 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
5943 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
5945 bitmask
[0] |= FATTR4_WORD0_FILEID
;
5947 nfs_fattr_init(&fs_locations
->fattr
);
5948 fs_locations
->server
= server
;
5949 fs_locations
->nlocations
= 0;
5950 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5951 dprintk("%s: returned status = %d\n", __func__
, status
);
5955 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5956 const struct qstr
*name
,
5957 struct nfs4_fs_locations
*fs_locations
,
5960 struct nfs4_exception exception
= { };
5963 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
5964 fs_locations
, page
);
5965 trace_nfs4_get_fs_locations(dir
, name
, err
);
5966 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
5968 } while (exception
.retry
);
5973 * If 'use_integrity' is true and the state managment nfs_client
5974 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5975 * and the machine credential as per RFC3530bis and RFC5661 Security
5976 * Considerations sections. Otherwise, just use the user cred with the
5977 * filesystem's rpc_client.
5979 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
5982 struct nfs4_secinfo_arg args
= {
5983 .dir_fh
= NFS_FH(dir
),
5986 struct nfs4_secinfo_res res
= {
5989 struct rpc_message msg
= {
5990 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
5994 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
5996 if (use_integrity
) {
5997 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
5998 msg
.rpc_cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
6001 dprintk("NFS call secinfo %s\n", name
->name
);
6003 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
6004 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
6006 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
6008 dprintk("NFS reply secinfo: %d\n", status
);
6011 put_rpccred(msg
.rpc_cred
);
6016 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
6017 struct nfs4_secinfo_flavors
*flavors
)
6019 struct nfs4_exception exception
= { };
6022 err
= -NFS4ERR_WRONGSEC
;
6024 /* try to use integrity protection with machine cred */
6025 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
6026 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
6029 * if unable to use integrity protection, or SECINFO with
6030 * integrity protection returns NFS4ERR_WRONGSEC (which is
6031 * disallowed by spec, but exists in deployed servers) use
6032 * the current filesystem's rpc_client and the user cred.
6034 if (err
== -NFS4ERR_WRONGSEC
)
6035 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
6037 trace_nfs4_secinfo(dir
, name
, err
);
6038 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6040 } while (exception
.retry
);
6044 #ifdef CONFIG_NFS_V4_1
6046 * Check the exchange flags returned by the server for invalid flags, having
6047 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6050 static int nfs4_check_cl_exchange_flags(u32 flags
)
6052 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
6054 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
6055 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
6057 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
6061 return -NFS4ERR_INVAL
;
6065 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
6066 struct nfs41_server_scope
*b
)
6068 if (a
->server_scope_sz
== b
->server_scope_sz
&&
6069 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
6076 * nfs4_proc_bind_conn_to_session()
6078 * The 4.1 client currently uses the same TCP connection for the
6079 * fore and backchannel.
6081 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6084 struct nfs41_bind_conn_to_session_res res
;
6085 struct rpc_message msg
= {
6087 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
6093 dprintk("--> %s\n", __func__
);
6095 res
.session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
6096 if (unlikely(res
.session
== NULL
)) {
6101 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6102 trace_nfs4_bind_conn_to_session(clp
, status
);
6104 if (memcmp(res
.session
->sess_id
.data
,
6105 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
6106 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
6110 if (res
.dir
!= NFS4_CDFS4_BOTH
) {
6111 dprintk("NFS: %s: Unexpected direction from server\n",
6116 if (res
.use_conn_in_rdma_mode
) {
6117 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6126 dprintk("<-- %s status= %d\n", __func__
, status
);
6131 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6132 * and operations we'd like to see to enable certain features in the allow map
6134 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
6135 .how
= SP4_MACH_CRED
,
6136 .enforce
.u
.words
= {
6137 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6138 1 << (OP_EXCHANGE_ID
- 32) |
6139 1 << (OP_CREATE_SESSION
- 32) |
6140 1 << (OP_DESTROY_SESSION
- 32) |
6141 1 << (OP_DESTROY_CLIENTID
- 32)
6144 [0] = 1 << (OP_CLOSE
) |
6146 [1] = 1 << (OP_SECINFO
- 32) |
6147 1 << (OP_SECINFO_NO_NAME
- 32) |
6148 1 << (OP_TEST_STATEID
- 32) |
6149 1 << (OP_FREE_STATEID
- 32)
6154 * Select the state protection mode for client `clp' given the server results
6155 * from exchange_id in `sp'.
6157 * Returns 0 on success, negative errno otherwise.
6159 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
6160 struct nfs41_state_protection
*sp
)
6162 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
6163 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6164 1 << (OP_EXCHANGE_ID
- 32) |
6165 1 << (OP_CREATE_SESSION
- 32) |
6166 1 << (OP_DESTROY_SESSION
- 32) |
6167 1 << (OP_DESTROY_CLIENTID
- 32)
6171 if (sp
->how
== SP4_MACH_CRED
) {
6172 /* Print state protect result */
6173 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
6174 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
6175 if (test_bit(i
, sp
->enforce
.u
.longs
))
6176 dfprintk(MOUNT
, " enforce op %d\n", i
);
6177 if (test_bit(i
, sp
->allow
.u
.longs
))
6178 dfprintk(MOUNT
, " allow op %d\n", i
);
6181 /* make sure nothing is on enforce list that isn't supported */
6182 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
6183 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
6184 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6190 * Minimal mode - state operations are allowed to use machine
6191 * credential. Note this already happens by default, so the
6192 * client doesn't have to do anything more than the negotiation.
6194 * NOTE: we don't care if EXCHANGE_ID is in the list -
6195 * we're already using the machine cred for exchange_id
6196 * and will never use a different cred.
6198 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
6199 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
6200 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
6201 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
6202 dfprintk(MOUNT
, "sp4_mach_cred:\n");
6203 dfprintk(MOUNT
, " minimal mode enabled\n");
6204 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
6206 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6210 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
6211 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
6212 dfprintk(MOUNT
, " cleanup mode enabled\n");
6213 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
6216 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
6217 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
6218 dfprintk(MOUNT
, " secinfo mode enabled\n");
6219 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
6222 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
6223 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
6224 dfprintk(MOUNT
, " stateid mode enabled\n");
6225 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
6228 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
6229 dfprintk(MOUNT
, " write mode enabled\n");
6230 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
6233 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
6234 dfprintk(MOUNT
, " commit mode enabled\n");
6235 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
6243 * _nfs4_proc_exchange_id()
6245 * Wrapper for EXCHANGE_ID operation.
6247 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
6250 nfs4_verifier verifier
;
6251 struct nfs41_exchange_id_args args
= {
6252 .verifier
= &verifier
,
6254 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
6255 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
6257 struct nfs41_exchange_id_res res
= {
6261 struct rpc_message msg
= {
6262 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
6268 nfs4_init_boot_verifier(clp
, &verifier
);
6269 args
.id_len
= nfs4_init_uniform_client_string(clp
, args
.id
,
6271 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6272 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
6273 args
.id_len
, args
.id
);
6275 res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
6277 if (unlikely(res
.server_owner
== NULL
)) {
6282 res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
6284 if (unlikely(res
.server_scope
== NULL
)) {
6286 goto out_server_owner
;
6289 res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
6290 if (unlikely(res
.impl_id
== NULL
)) {
6292 goto out_server_scope
;
6297 args
.state_protect
.how
= SP4_NONE
;
6301 args
.state_protect
= nfs4_sp4_mach_cred_request
;
6308 goto out_server_scope
;
6311 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6312 trace_nfs4_exchange_id(clp
, status
);
6314 status
= nfs4_check_cl_exchange_flags(res
.flags
);
6317 status
= nfs4_sp4_select_mode(clp
, &res
.state_protect
);
6320 clp
->cl_clientid
= res
.clientid
;
6321 clp
->cl_exchange_flags
= (res
.flags
& ~EXCHGID4_FLAG_CONFIRMED_R
);
6322 if (!(res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
))
6323 clp
->cl_seqid
= res
.seqid
;
6325 kfree(clp
->cl_serverowner
);
6326 clp
->cl_serverowner
= res
.server_owner
;
6327 res
.server_owner
= NULL
;
6329 /* use the most recent implementation id */
6330 kfree(clp
->cl_implid
);
6331 clp
->cl_implid
= res
.impl_id
;
6333 if (clp
->cl_serverscope
!= NULL
&&
6334 !nfs41_same_server_scope(clp
->cl_serverscope
,
6335 res
.server_scope
)) {
6336 dprintk("%s: server_scope mismatch detected\n",
6338 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
6339 kfree(clp
->cl_serverscope
);
6340 clp
->cl_serverscope
= NULL
;
6343 if (clp
->cl_serverscope
== NULL
) {
6344 clp
->cl_serverscope
= res
.server_scope
;
6351 kfree(res
.server_owner
);
6353 kfree(res
.server_scope
);
6355 if (clp
->cl_implid
!= NULL
)
6356 dprintk("NFS reply exchange_id: Server Implementation ID: "
6357 "domain: %s, name: %s, date: %llu,%u\n",
6358 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
6359 clp
->cl_implid
->date
.seconds
,
6360 clp
->cl_implid
->date
.nseconds
);
6361 dprintk("NFS reply exchange_id: %d\n", status
);
6366 * nfs4_proc_exchange_id()
6368 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6370 * Since the clientid has expired, all compounds using sessions
6371 * associated with the stale clientid will be returning
6372 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6373 * be in some phase of session reset.
6375 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6377 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6379 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
6382 /* try SP4_MACH_CRED if krb5i/p */
6383 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
6384 authflavor
== RPC_AUTH_GSS_KRB5P
) {
6385 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
);
6391 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
);
6394 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6395 struct rpc_cred
*cred
)
6397 struct rpc_message msg
= {
6398 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
6404 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6405 trace_nfs4_destroy_clientid(clp
, status
);
6407 dprintk("NFS: Got error %d from the server %s on "
6408 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
6412 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6413 struct rpc_cred
*cred
)
6418 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
6419 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
6421 case -NFS4ERR_DELAY
:
6422 case -NFS4ERR_CLIENTID_BUSY
:
6432 int nfs4_destroy_clientid(struct nfs_client
*clp
)
6434 struct rpc_cred
*cred
;
6437 if (clp
->cl_mvops
->minor_version
< 1)
6439 if (clp
->cl_exchange_flags
== 0)
6441 if (clp
->cl_preserve_clid
)
6443 cred
= nfs4_get_clid_cred(clp
);
6444 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
6449 case -NFS4ERR_STALE_CLIENTID
:
6450 clp
->cl_exchange_flags
= 0;
6456 struct nfs4_get_lease_time_data
{
6457 struct nfs4_get_lease_time_args
*args
;
6458 struct nfs4_get_lease_time_res
*res
;
6459 struct nfs_client
*clp
;
6462 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
6465 struct nfs4_get_lease_time_data
*data
=
6466 (struct nfs4_get_lease_time_data
*)calldata
;
6468 dprintk("--> %s\n", __func__
);
6469 /* just setup sequence, do not trigger session recovery
6470 since we're invoked within one */
6471 nfs41_setup_sequence(data
->clp
->cl_session
,
6472 &data
->args
->la_seq_args
,
6473 &data
->res
->lr_seq_res
,
6475 dprintk("<-- %s\n", __func__
);
6479 * Called from nfs4_state_manager thread for session setup, so don't recover
6480 * from sequence operation or clientid errors.
6482 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
6484 struct nfs4_get_lease_time_data
*data
=
6485 (struct nfs4_get_lease_time_data
*)calldata
;
6487 dprintk("--> %s\n", __func__
);
6488 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
6490 switch (task
->tk_status
) {
6491 case -NFS4ERR_DELAY
:
6492 case -NFS4ERR_GRACE
:
6493 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
6494 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
6495 task
->tk_status
= 0;
6497 case -NFS4ERR_RETRY_UNCACHED_REP
:
6498 rpc_restart_call_prepare(task
);
6501 dprintk("<-- %s\n", __func__
);
6504 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
6505 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
6506 .rpc_call_done
= nfs4_get_lease_time_done
,
6509 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
6511 struct rpc_task
*task
;
6512 struct nfs4_get_lease_time_args args
;
6513 struct nfs4_get_lease_time_res res
= {
6514 .lr_fsinfo
= fsinfo
,
6516 struct nfs4_get_lease_time_data data
= {
6521 struct rpc_message msg
= {
6522 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
6526 struct rpc_task_setup task_setup
= {
6527 .rpc_client
= clp
->cl_rpcclient
,
6528 .rpc_message
= &msg
,
6529 .callback_ops
= &nfs4_get_lease_time_ops
,
6530 .callback_data
= &data
,
6531 .flags
= RPC_TASK_TIMEOUT
,
6535 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
6536 nfs4_set_sequence_privileged(&args
.la_seq_args
);
6537 dprintk("--> %s\n", __func__
);
6538 task
= rpc_run_task(&task_setup
);
6541 status
= PTR_ERR(task
);
6543 status
= task
->tk_status
;
6546 dprintk("<-- %s return %d\n", __func__
, status
);
6552 * Initialize the values to be used by the client in CREATE_SESSION
6553 * If nfs4_init_session set the fore channel request and response sizes,
6556 * Set the back channel max_resp_sz_cached to zero to force the client to
6557 * always set csa_cachethis to FALSE because the current implementation
6558 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6560 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
6562 unsigned int max_rqst_sz
, max_resp_sz
;
6564 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
6565 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
6567 /* Fore channel attributes */
6568 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
6569 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
6570 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
6571 args
->fc_attrs
.max_reqs
= max_session_slots
;
6573 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6574 "max_ops=%u max_reqs=%u\n",
6576 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
6577 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
6579 /* Back channel attributes */
6580 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
6581 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
6582 args
->bc_attrs
.max_resp_sz_cached
= 0;
6583 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
6584 args
->bc_attrs
.max_reqs
= 1;
6586 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6587 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6589 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
6590 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
6591 args
->bc_attrs
.max_reqs
);
6594 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6596 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
6597 struct nfs4_channel_attrs
*rcvd
= &session
->fc_attrs
;
6599 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
6602 * Our requested max_ops is the minimum we need; we're not
6603 * prepared to break up compounds into smaller pieces than that.
6604 * So, no point even trying to continue if the server won't
6607 if (rcvd
->max_ops
< sent
->max_ops
)
6609 if (rcvd
->max_reqs
== 0)
6611 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
6612 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
6616 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6618 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
6619 struct nfs4_channel_attrs
*rcvd
= &session
->bc_attrs
;
6621 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
6623 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
6625 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
6627 /* These would render the backchannel useless: */
6628 if (rcvd
->max_ops
!= sent
->max_ops
)
6630 if (rcvd
->max_reqs
!= sent
->max_reqs
)
6635 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
6636 struct nfs4_session
*session
)
6640 ret
= nfs4_verify_fore_channel_attrs(args
, session
);
6643 return nfs4_verify_back_channel_attrs(args
, session
);
6646 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
6647 struct rpc_cred
*cred
)
6649 struct nfs4_session
*session
= clp
->cl_session
;
6650 struct nfs41_create_session_args args
= {
6652 .cb_program
= NFS4_CALLBACK
,
6654 struct nfs41_create_session_res res
= {
6657 struct rpc_message msg
= {
6658 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
6665 nfs4_init_channel_attrs(&args
);
6666 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
6668 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6669 trace_nfs4_create_session(clp
, status
);
6672 /* Verify the session's negotiated channel_attrs values */
6673 status
= nfs4_verify_channel_attrs(&args
, session
);
6674 /* Increment the clientid slot sequence id */
6682 * Issues a CREATE_SESSION operation to the server.
6683 * It is the responsibility of the caller to verify the session is
6684 * expired before calling this routine.
6686 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6690 struct nfs4_session
*session
= clp
->cl_session
;
6692 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
6694 status
= _nfs4_proc_create_session(clp
, cred
);
6698 /* Init or reset the session slot tables */
6699 status
= nfs4_setup_session_slot_tables(session
);
6700 dprintk("slot table setup returned %d\n", status
);
6704 ptr
= (unsigned *)&session
->sess_id
.data
[0];
6705 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
6706 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
6708 dprintk("<-- %s\n", __func__
);
6713 * Issue the over-the-wire RPC DESTROY_SESSION.
6714 * The caller must serialize access to this routine.
6716 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
6717 struct rpc_cred
*cred
)
6719 struct rpc_message msg
= {
6720 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
6721 .rpc_argp
= session
,
6726 dprintk("--> nfs4_proc_destroy_session\n");
6728 /* session is still being setup */
6729 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
6732 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6733 trace_nfs4_destroy_session(session
->clp
, status
);
6736 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6737 "Session has been destroyed regardless...\n", status
);
6739 dprintk("<-- nfs4_proc_destroy_session\n");
6744 * Renew the cl_session lease.
6746 struct nfs4_sequence_data
{
6747 struct nfs_client
*clp
;
6748 struct nfs4_sequence_args args
;
6749 struct nfs4_sequence_res res
;
6752 static void nfs41_sequence_release(void *data
)
6754 struct nfs4_sequence_data
*calldata
= data
;
6755 struct nfs_client
*clp
= calldata
->clp
;
6757 if (atomic_read(&clp
->cl_count
) > 1)
6758 nfs4_schedule_state_renewal(clp
);
6759 nfs_put_client(clp
);
6763 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6765 switch(task
->tk_status
) {
6766 case -NFS4ERR_DELAY
:
6767 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6770 nfs4_schedule_lease_recovery(clp
);
6775 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
6777 struct nfs4_sequence_data
*calldata
= data
;
6778 struct nfs_client
*clp
= calldata
->clp
;
6780 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
6783 trace_nfs4_sequence(clp
, task
->tk_status
);
6784 if (task
->tk_status
< 0) {
6785 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
6786 if (atomic_read(&clp
->cl_count
) == 1)
6789 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
6790 rpc_restart_call_prepare(task
);
6794 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
6796 dprintk("<-- %s\n", __func__
);
6799 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
6801 struct nfs4_sequence_data
*calldata
= data
;
6802 struct nfs_client
*clp
= calldata
->clp
;
6803 struct nfs4_sequence_args
*args
;
6804 struct nfs4_sequence_res
*res
;
6806 args
= task
->tk_msg
.rpc_argp
;
6807 res
= task
->tk_msg
.rpc_resp
;
6809 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
6812 static const struct rpc_call_ops nfs41_sequence_ops
= {
6813 .rpc_call_done
= nfs41_sequence_call_done
,
6814 .rpc_call_prepare
= nfs41_sequence_prepare
,
6815 .rpc_release
= nfs41_sequence_release
,
6818 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
6819 struct rpc_cred
*cred
,
6822 struct nfs4_sequence_data
*calldata
;
6823 struct rpc_message msg
= {
6824 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
6827 struct rpc_task_setup task_setup_data
= {
6828 .rpc_client
= clp
->cl_rpcclient
,
6829 .rpc_message
= &msg
,
6830 .callback_ops
= &nfs41_sequence_ops
,
6831 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
6834 if (!atomic_inc_not_zero(&clp
->cl_count
))
6835 return ERR_PTR(-EIO
);
6836 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6837 if (calldata
== NULL
) {
6838 nfs_put_client(clp
);
6839 return ERR_PTR(-ENOMEM
);
6841 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
6843 nfs4_set_sequence_privileged(&calldata
->args
);
6844 msg
.rpc_argp
= &calldata
->args
;
6845 msg
.rpc_resp
= &calldata
->res
;
6846 calldata
->clp
= clp
;
6847 task_setup_data
.callback_data
= calldata
;
6849 return rpc_run_task(&task_setup_data
);
6852 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
6854 struct rpc_task
*task
;
6857 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
6859 task
= _nfs41_proc_sequence(clp
, cred
, false);
6861 ret
= PTR_ERR(task
);
6863 rpc_put_task_async(task
);
6864 dprintk("<-- %s status=%d\n", __func__
, ret
);
6868 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6870 struct rpc_task
*task
;
6873 task
= _nfs41_proc_sequence(clp
, cred
, true);
6875 ret
= PTR_ERR(task
);
6878 ret
= rpc_wait_for_completion_task(task
);
6880 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
6882 if (task
->tk_status
== 0)
6883 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
6884 ret
= task
->tk_status
;
6888 dprintk("<-- %s status=%d\n", __func__
, ret
);
6892 struct nfs4_reclaim_complete_data
{
6893 struct nfs_client
*clp
;
6894 struct nfs41_reclaim_complete_args arg
;
6895 struct nfs41_reclaim_complete_res res
;
6898 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
6900 struct nfs4_reclaim_complete_data
*calldata
= data
;
6902 nfs41_setup_sequence(calldata
->clp
->cl_session
,
6903 &calldata
->arg
.seq_args
,
6904 &calldata
->res
.seq_res
,
6908 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6910 switch(task
->tk_status
) {
6912 case -NFS4ERR_COMPLETE_ALREADY
:
6913 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
6915 case -NFS4ERR_DELAY
:
6916 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6918 case -NFS4ERR_RETRY_UNCACHED_REP
:
6921 nfs4_schedule_lease_recovery(clp
);
6926 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
6928 struct nfs4_reclaim_complete_data
*calldata
= data
;
6929 struct nfs_client
*clp
= calldata
->clp
;
6930 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
6932 dprintk("--> %s\n", __func__
);
6933 if (!nfs41_sequence_done(task
, res
))
6936 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
6937 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
6938 rpc_restart_call_prepare(task
);
6941 dprintk("<-- %s\n", __func__
);
6944 static void nfs4_free_reclaim_complete_data(void *data
)
6946 struct nfs4_reclaim_complete_data
*calldata
= data
;
6951 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
6952 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
6953 .rpc_call_done
= nfs4_reclaim_complete_done
,
6954 .rpc_release
= nfs4_free_reclaim_complete_data
,
6958 * Issue a global reclaim complete.
6960 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
6961 struct rpc_cred
*cred
)
6963 struct nfs4_reclaim_complete_data
*calldata
;
6964 struct rpc_task
*task
;
6965 struct rpc_message msg
= {
6966 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
6969 struct rpc_task_setup task_setup_data
= {
6970 .rpc_client
= clp
->cl_rpcclient
,
6971 .rpc_message
= &msg
,
6972 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
6973 .flags
= RPC_TASK_ASYNC
,
6975 int status
= -ENOMEM
;
6977 dprintk("--> %s\n", __func__
);
6978 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6979 if (calldata
== NULL
)
6981 calldata
->clp
= clp
;
6982 calldata
->arg
.one_fs
= 0;
6984 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
6985 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
6986 msg
.rpc_argp
= &calldata
->arg
;
6987 msg
.rpc_resp
= &calldata
->res
;
6988 task_setup_data
.callback_data
= calldata
;
6989 task
= rpc_run_task(&task_setup_data
);
6991 status
= PTR_ERR(task
);
6994 status
= nfs4_wait_for_completion_rpc_task(task
);
6996 status
= task
->tk_status
;
7000 dprintk("<-- %s status=%d\n", __func__
, status
);
7005 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
7007 struct nfs4_layoutget
*lgp
= calldata
;
7008 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
7009 struct nfs4_session
*session
= nfs4_get_session(server
);
7011 dprintk("--> %s\n", __func__
);
7012 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7013 * right now covering the LAYOUTGET we are about to send.
7014 * However, that is not so catastrophic, and there seems
7015 * to be no way to prevent it completely.
7017 if (nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
7018 &lgp
->res
.seq_res
, task
))
7020 if (pnfs_choose_layoutget_stateid(&lgp
->args
.stateid
,
7021 NFS_I(lgp
->args
.inode
)->layout
,
7022 lgp
->args
.ctx
->state
)) {
7023 rpc_exit(task
, NFS4_OK
);
7027 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
7029 struct nfs4_layoutget
*lgp
= calldata
;
7030 struct inode
*inode
= lgp
->args
.inode
;
7031 struct nfs_server
*server
= NFS_SERVER(inode
);
7032 struct pnfs_layout_hdr
*lo
;
7033 struct nfs4_state
*state
= NULL
;
7034 unsigned long timeo
, giveup
;
7036 dprintk("--> %s\n", __func__
);
7038 if (!nfs41_sequence_done(task
, &lgp
->res
.seq_res
))
7041 switch (task
->tk_status
) {
7044 case -NFS4ERR_LAYOUTTRYLATER
:
7045 case -NFS4ERR_RECALLCONFLICT
:
7046 timeo
= rpc_get_timeout(task
->tk_client
);
7047 giveup
= lgp
->args
.timestamp
+ timeo
;
7048 if (time_after(giveup
, jiffies
))
7049 task
->tk_status
= -NFS4ERR_DELAY
;
7051 case -NFS4ERR_EXPIRED
:
7052 case -NFS4ERR_BAD_STATEID
:
7053 spin_lock(&inode
->i_lock
);
7054 lo
= NFS_I(inode
)->layout
;
7055 if (!lo
|| list_empty(&lo
->plh_segs
)) {
7056 spin_unlock(&inode
->i_lock
);
7057 /* If the open stateid was bad, then recover it. */
7058 state
= lgp
->args
.ctx
->state
;
7062 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
);
7063 spin_unlock(&inode
->i_lock
);
7064 /* Mark the bad layout state as invalid, then
7065 * retry using the open stateid. */
7066 pnfs_free_lseg_list(&head
);
7069 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
7070 rpc_restart_call_prepare(task
);
7072 dprintk("<-- %s\n", __func__
);
7075 static size_t max_response_pages(struct nfs_server
*server
)
7077 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
7078 return nfs_page_array_len(0, max_resp_sz
);
7081 static void nfs4_free_pages(struct page
**pages
, size_t size
)
7088 for (i
= 0; i
< size
; i
++) {
7091 __free_page(pages
[i
]);
7096 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
7098 struct page
**pages
;
7101 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
7103 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
7107 for (i
= 0; i
< size
; i
++) {
7108 pages
[i
] = alloc_page(gfp_flags
);
7110 dprintk("%s: failed to allocate page\n", __func__
);
7111 nfs4_free_pages(pages
, size
);
7119 static void nfs4_layoutget_release(void *calldata
)
7121 struct nfs4_layoutget
*lgp
= calldata
;
7122 struct inode
*inode
= lgp
->args
.inode
;
7123 struct nfs_server
*server
= NFS_SERVER(inode
);
7124 size_t max_pages
= max_response_pages(server
);
7126 dprintk("--> %s\n", __func__
);
7127 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
7128 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
7129 put_nfs_open_context(lgp
->args
.ctx
);
7131 dprintk("<-- %s\n", __func__
);
7134 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
7135 .rpc_call_prepare
= nfs4_layoutget_prepare
,
7136 .rpc_call_done
= nfs4_layoutget_done
,
7137 .rpc_release
= nfs4_layoutget_release
,
7140 struct pnfs_layout_segment
*
7141 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, gfp_t gfp_flags
)
7143 struct inode
*inode
= lgp
->args
.inode
;
7144 struct nfs_server
*server
= NFS_SERVER(inode
);
7145 size_t max_pages
= max_response_pages(server
);
7146 struct rpc_task
*task
;
7147 struct rpc_message msg
= {
7148 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
7149 .rpc_argp
= &lgp
->args
,
7150 .rpc_resp
= &lgp
->res
,
7151 .rpc_cred
= lgp
->cred
,
7153 struct rpc_task_setup task_setup_data
= {
7154 .rpc_client
= server
->client
,
7155 .rpc_message
= &msg
,
7156 .callback_ops
= &nfs4_layoutget_call_ops
,
7157 .callback_data
= lgp
,
7158 .flags
= RPC_TASK_ASYNC
,
7160 struct pnfs_layout_segment
*lseg
= NULL
;
7163 dprintk("--> %s\n", __func__
);
7165 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
7166 if (!lgp
->args
.layout
.pages
) {
7167 nfs4_layoutget_release(lgp
);
7168 return ERR_PTR(-ENOMEM
);
7170 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
7171 lgp
->args
.timestamp
= jiffies
;
7173 lgp
->res
.layoutp
= &lgp
->args
.layout
;
7174 lgp
->res
.seq_res
.sr_slot
= NULL
;
7175 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
7177 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7178 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
7180 task
= rpc_run_task(&task_setup_data
);
7182 return ERR_CAST(task
);
7183 status
= nfs4_wait_for_completion_rpc_task(task
);
7185 status
= task
->tk_status
;
7186 trace_nfs4_layoutget(lgp
->args
.ctx
,
7190 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7191 if (status
== 0 && lgp
->res
.layoutp
->len
)
7192 lseg
= pnfs_layout_process(lgp
);
7194 dprintk("<-- %s status=%d\n", __func__
, status
);
7196 return ERR_PTR(status
);
7201 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
7203 struct nfs4_layoutreturn
*lrp
= calldata
;
7205 dprintk("--> %s\n", __func__
);
7206 nfs41_setup_sequence(lrp
->clp
->cl_session
,
7207 &lrp
->args
.seq_args
,
7212 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
7214 struct nfs4_layoutreturn
*lrp
= calldata
;
7215 struct nfs_server
*server
;
7217 dprintk("--> %s\n", __func__
);
7219 if (!nfs41_sequence_done(task
, &lrp
->res
.seq_res
))
7222 server
= NFS_SERVER(lrp
->args
.inode
);
7223 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
7224 rpc_restart_call_prepare(task
);
7227 dprintk("<-- %s\n", __func__
);
7230 static void nfs4_layoutreturn_release(void *calldata
)
7232 struct nfs4_layoutreturn
*lrp
= calldata
;
7233 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
7235 dprintk("--> %s\n", __func__
);
7236 spin_lock(&lo
->plh_inode
->i_lock
);
7237 if (lrp
->res
.lrs_present
)
7238 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
7239 lo
->plh_block_lgets
--;
7240 spin_unlock(&lo
->plh_inode
->i_lock
);
7241 pnfs_put_layout_hdr(lrp
->args
.layout
);
7243 dprintk("<-- %s\n", __func__
);
7246 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
7247 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
7248 .rpc_call_done
= nfs4_layoutreturn_done
,
7249 .rpc_release
= nfs4_layoutreturn_release
,
7252 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
)
7254 struct rpc_task
*task
;
7255 struct rpc_message msg
= {
7256 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
7257 .rpc_argp
= &lrp
->args
,
7258 .rpc_resp
= &lrp
->res
,
7259 .rpc_cred
= lrp
->cred
,
7261 struct rpc_task_setup task_setup_data
= {
7262 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
7263 .rpc_message
= &msg
,
7264 .callback_ops
= &nfs4_layoutreturn_call_ops
,
7265 .callback_data
= lrp
,
7269 dprintk("--> %s\n", __func__
);
7270 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
7271 task
= rpc_run_task(&task_setup_data
);
7273 return PTR_ERR(task
);
7274 status
= task
->tk_status
;
7275 trace_nfs4_layoutreturn(lrp
->args
.inode
, status
);
7276 dprintk("<-- %s status=%d\n", __func__
, status
);
7282 * Retrieve the list of Data Server devices from the MDS.
7284 static int _nfs4_getdevicelist(struct nfs_server
*server
,
7285 const struct nfs_fh
*fh
,
7286 struct pnfs_devicelist
*devlist
)
7288 struct nfs4_getdevicelist_args args
= {
7290 .layoutclass
= server
->pnfs_curr_ld
->id
,
7292 struct nfs4_getdevicelist_res res
= {
7295 struct rpc_message msg
= {
7296 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICELIST
],
7302 dprintk("--> %s\n", __func__
);
7303 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
,
7305 dprintk("<-- %s status=%d\n", __func__
, status
);
7309 int nfs4_proc_getdevicelist(struct nfs_server
*server
,
7310 const struct nfs_fh
*fh
,
7311 struct pnfs_devicelist
*devlist
)
7313 struct nfs4_exception exception
= { };
7317 err
= nfs4_handle_exception(server
,
7318 _nfs4_getdevicelist(server
, fh
, devlist
),
7320 } while (exception
.retry
);
7322 dprintk("%s: err=%d, num_devs=%u\n", __func__
,
7323 err
, devlist
->num_devs
);
7327 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist
);
7330 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
7331 struct pnfs_device
*pdev
,
7332 struct rpc_cred
*cred
)
7334 struct nfs4_getdeviceinfo_args args
= {
7337 struct nfs4_getdeviceinfo_res res
= {
7340 struct rpc_message msg
= {
7341 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
7348 dprintk("--> %s\n", __func__
);
7349 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
7350 dprintk("<-- %s status=%d\n", __func__
, status
);
7355 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
7356 struct pnfs_device
*pdev
,
7357 struct rpc_cred
*cred
)
7359 struct nfs4_exception exception
= { };
7363 err
= nfs4_handle_exception(server
,
7364 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
7366 } while (exception
.retry
);
7369 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
7371 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
7373 struct nfs4_layoutcommit_data
*data
= calldata
;
7374 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
7375 struct nfs4_session
*session
= nfs4_get_session(server
);
7377 nfs41_setup_sequence(session
,
7378 &data
->args
.seq_args
,
7384 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
7386 struct nfs4_layoutcommit_data
*data
= calldata
;
7387 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
7389 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
7392 switch (task
->tk_status
) { /* Just ignore these failures */
7393 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
7394 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
7395 case -NFS4ERR_BADLAYOUT
: /* no layout */
7396 case -NFS4ERR_GRACE
: /* loca_recalim always false */
7397 task
->tk_status
= 0;
7400 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
7404 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
7405 rpc_restart_call_prepare(task
);
7411 static void nfs4_layoutcommit_release(void *calldata
)
7413 struct nfs4_layoutcommit_data
*data
= calldata
;
7415 pnfs_cleanup_layoutcommit(data
);
7416 put_rpccred(data
->cred
);
7420 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
7421 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
7422 .rpc_call_done
= nfs4_layoutcommit_done
,
7423 .rpc_release
= nfs4_layoutcommit_release
,
7427 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
7429 struct rpc_message msg
= {
7430 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
7431 .rpc_argp
= &data
->args
,
7432 .rpc_resp
= &data
->res
,
7433 .rpc_cred
= data
->cred
,
7435 struct rpc_task_setup task_setup_data
= {
7436 .task
= &data
->task
,
7437 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
7438 .rpc_message
= &msg
,
7439 .callback_ops
= &nfs4_layoutcommit_ops
,
7440 .callback_data
= data
,
7441 .flags
= RPC_TASK_ASYNC
,
7443 struct rpc_task
*task
;
7446 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7447 "lbw: %llu inode %lu\n",
7448 data
->task
.tk_pid
, sync
,
7449 data
->args
.lastbytewritten
,
7450 data
->args
.inode
->i_ino
);
7452 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
7453 task
= rpc_run_task(&task_setup_data
);
7455 return PTR_ERR(task
);
7458 status
= nfs4_wait_for_completion_rpc_task(task
);
7461 status
= task
->tk_status
;
7462 trace_nfs4_layoutcommit(data
->args
.inode
, status
);
7464 dprintk("%s: status %d\n", __func__
, status
);
7470 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7471 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7474 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7475 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
7477 struct nfs41_secinfo_no_name_args args
= {
7478 .style
= SECINFO_STYLE_CURRENT_FH
,
7480 struct nfs4_secinfo_res res
= {
7483 struct rpc_message msg
= {
7484 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
7488 return nfs4_call_sync(server
->nfs_client
->cl_rpcclient
, server
, &msg
,
7489 &args
.seq_args
, &res
.seq_res
, 0);
7493 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7494 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
7496 struct nfs4_exception exception
= { };
7499 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
7502 case -NFS4ERR_WRONGSEC
:
7503 case -NFS4ERR_NOTSUPP
:
7506 err
= nfs4_handle_exception(server
, err
, &exception
);
7508 } while (exception
.retry
);
7514 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7515 struct nfs_fsinfo
*info
)
7519 rpc_authflavor_t flavor
;
7520 struct nfs4_secinfo_flavors
*flavors
;
7522 page
= alloc_page(GFP_KERNEL
);
7528 flavors
= page_address(page
);
7529 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
7532 * Fall back on "guess and check" method if
7533 * the server doesn't support SECINFO_NO_NAME
7535 if (err
== -NFS4ERR_WRONGSEC
|| err
== -NFS4ERR_NOTSUPP
) {
7536 err
= nfs4_find_root_sec(server
, fhandle
, info
);
7542 flavor
= nfs_find_best_sec(flavors
);
7544 err
= nfs4_lookup_root_sec(server
, fhandle
, info
, flavor
);
7554 static int _nfs41_test_stateid(struct nfs_server
*server
,
7555 nfs4_stateid
*stateid
,
7556 struct rpc_cred
*cred
)
7559 struct nfs41_test_stateid_args args
= {
7562 struct nfs41_test_stateid_res res
;
7563 struct rpc_message msg
= {
7564 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
7569 struct rpc_clnt
*rpc_client
= server
->client
;
7571 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
7574 dprintk("NFS call test_stateid %p\n", stateid
);
7575 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7576 nfs4_set_sequence_privileged(&args
.seq_args
);
7577 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
7578 &args
.seq_args
, &res
.seq_res
);
7579 if (status
!= NFS_OK
) {
7580 dprintk("NFS reply test_stateid: failed, %d\n", status
);
7583 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
7588 * nfs41_test_stateid - perform a TEST_STATEID operation
7590 * @server: server / transport on which to perform the operation
7591 * @stateid: state ID to test
7594 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7595 * Otherwise a negative NFS4ERR value is returned if the operation
7596 * failed or the state ID is not currently valid.
7598 static int nfs41_test_stateid(struct nfs_server
*server
,
7599 nfs4_stateid
*stateid
,
7600 struct rpc_cred
*cred
)
7602 struct nfs4_exception exception
= { };
7605 err
= _nfs41_test_stateid(server
, stateid
, cred
);
7606 if (err
!= -NFS4ERR_DELAY
)
7608 nfs4_handle_exception(server
, err
, &exception
);
7609 } while (exception
.retry
);
7613 struct nfs_free_stateid_data
{
7614 struct nfs_server
*server
;
7615 struct nfs41_free_stateid_args args
;
7616 struct nfs41_free_stateid_res res
;
7619 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
7621 struct nfs_free_stateid_data
*data
= calldata
;
7622 nfs41_setup_sequence(nfs4_get_session(data
->server
),
7623 &data
->args
.seq_args
,
7628 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
7630 struct nfs_free_stateid_data
*data
= calldata
;
7632 nfs41_sequence_done(task
, &data
->res
.seq_res
);
7634 switch (task
->tk_status
) {
7635 case -NFS4ERR_DELAY
:
7636 if (nfs4_async_handle_error(task
, data
->server
, NULL
) == -EAGAIN
)
7637 rpc_restart_call_prepare(task
);
7641 static void nfs41_free_stateid_release(void *calldata
)
7646 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
7647 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
7648 .rpc_call_done
= nfs41_free_stateid_done
,
7649 .rpc_release
= nfs41_free_stateid_release
,
7652 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
7653 nfs4_stateid
*stateid
,
7654 struct rpc_cred
*cred
,
7657 struct rpc_message msg
= {
7658 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
7661 struct rpc_task_setup task_setup
= {
7662 .rpc_client
= server
->client
,
7663 .rpc_message
= &msg
,
7664 .callback_ops
= &nfs41_free_stateid_ops
,
7665 .flags
= RPC_TASK_ASYNC
,
7667 struct nfs_free_stateid_data
*data
;
7669 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
7670 &task_setup
.rpc_client
, &msg
);
7672 dprintk("NFS call free_stateid %p\n", stateid
);
7673 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
7675 return ERR_PTR(-ENOMEM
);
7676 data
->server
= server
;
7677 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
7679 task_setup
.callback_data
= data
;
7681 msg
.rpc_argp
= &data
->args
;
7682 msg
.rpc_resp
= &data
->res
;
7683 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
7685 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
7687 return rpc_run_task(&task_setup
);
7691 * nfs41_free_stateid - perform a FREE_STATEID operation
7693 * @server: server / transport on which to perform the operation
7694 * @stateid: state ID to release
7697 * Returns NFS_OK if the server freed "stateid". Otherwise a
7698 * negative NFS4ERR value is returned.
7700 static int nfs41_free_stateid(struct nfs_server
*server
,
7701 nfs4_stateid
*stateid
,
7702 struct rpc_cred
*cred
)
7704 struct rpc_task
*task
;
7707 task
= _nfs41_free_stateid(server
, stateid
, cred
, true);
7709 return PTR_ERR(task
);
7710 ret
= rpc_wait_for_completion_task(task
);
7712 ret
= task
->tk_status
;
7717 static int nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
7719 struct rpc_task
*task
;
7720 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
7722 task
= _nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
7723 nfs4_free_lock_state(server
, lsp
);
7725 return PTR_ERR(task
);
7730 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
7731 const nfs4_stateid
*s2
)
7733 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
7736 if (s1
->seqid
== s2
->seqid
)
7738 if (s1
->seqid
== 0 || s2
->seqid
== 0)
7744 #endif /* CONFIG_NFS_V4_1 */
7746 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
7747 const nfs4_stateid
*s2
)
7749 return nfs4_stateid_match(s1
, s2
);
7753 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
7754 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7755 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7756 .recover_open
= nfs4_open_reclaim
,
7757 .recover_lock
= nfs4_lock_reclaim
,
7758 .establish_clid
= nfs4_init_clientid
,
7759 .detect_trunking
= nfs40_discover_server_trunking
,
7762 #if defined(CONFIG_NFS_V4_1)
7763 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
7764 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7765 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7766 .recover_open
= nfs4_open_reclaim
,
7767 .recover_lock
= nfs4_lock_reclaim
,
7768 .establish_clid
= nfs41_init_clientid
,
7769 .reclaim_complete
= nfs41_proc_reclaim_complete
,
7770 .detect_trunking
= nfs41_discover_server_trunking
,
7772 #endif /* CONFIG_NFS_V4_1 */
7774 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
7775 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7776 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7777 .recover_open
= nfs4_open_expired
,
7778 .recover_lock
= nfs4_lock_expired
,
7779 .establish_clid
= nfs4_init_clientid
,
7782 #if defined(CONFIG_NFS_V4_1)
7783 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
7784 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7785 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7786 .recover_open
= nfs41_open_expired
,
7787 .recover_lock
= nfs41_lock_expired
,
7788 .establish_clid
= nfs41_init_clientid
,
7790 #endif /* CONFIG_NFS_V4_1 */
7792 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
7793 .sched_state_renewal
= nfs4_proc_async_renew
,
7794 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
7795 .renew_lease
= nfs4_proc_renew
,
7798 #if defined(CONFIG_NFS_V4_1)
7799 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
7800 .sched_state_renewal
= nfs41_proc_async_sequence
,
7801 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
7802 .renew_lease
= nfs4_proc_sequence
,
7806 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
7808 .init_caps
= NFS_CAP_READDIRPLUS
7809 | NFS_CAP_ATOMIC_OPEN
7810 | NFS_CAP_CHANGE_ATTR
7811 | NFS_CAP_POSIX_LOCK
,
7812 .init_client
= nfs40_init_client
,
7813 .shutdown_client
= nfs40_shutdown_client
,
7814 .match_stateid
= nfs4_match_stateid
,
7815 .find_root_sec
= nfs4_find_root_sec
,
7816 .free_lock_state
= nfs4_release_lockowner
,
7817 .call_sync_ops
= &nfs40_call_sync_ops
,
7818 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
7819 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
7820 .state_renewal_ops
= &nfs40_state_renewal_ops
,
7823 #if defined(CONFIG_NFS_V4_1)
7824 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
7826 .init_caps
= NFS_CAP_READDIRPLUS
7827 | NFS_CAP_ATOMIC_OPEN
7828 | NFS_CAP_CHANGE_ATTR
7829 | NFS_CAP_POSIX_LOCK
7830 | NFS_CAP_STATEID_NFSV41
7831 | NFS_CAP_ATOMIC_OPEN_V1
,
7832 .init_client
= nfs41_init_client
,
7833 .shutdown_client
= nfs41_shutdown_client
,
7834 .match_stateid
= nfs41_match_stateid
,
7835 .find_root_sec
= nfs41_find_root_sec
,
7836 .free_lock_state
= nfs41_free_lock_state
,
7837 .call_sync_ops
= &nfs41_call_sync_ops
,
7838 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7839 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7840 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7844 #if defined(CONFIG_NFS_V4_2)
7845 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
7847 .init_caps
= NFS_CAP_READDIRPLUS
7848 | NFS_CAP_ATOMIC_OPEN
7849 | NFS_CAP_CHANGE_ATTR
7850 | NFS_CAP_POSIX_LOCK
7851 | NFS_CAP_STATEID_NFSV41
7852 | NFS_CAP_ATOMIC_OPEN_V1
,
7853 .init_client
= nfs41_init_client
,
7854 .shutdown_client
= nfs41_shutdown_client
,
7855 .match_stateid
= nfs41_match_stateid
,
7856 .find_root_sec
= nfs41_find_root_sec
,
7857 .free_lock_state
= nfs41_free_lock_state
,
7858 .call_sync_ops
= &nfs41_call_sync_ops
,
7859 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7860 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7861 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7865 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
7866 [0] = &nfs_v4_0_minor_ops
,
7867 #if defined(CONFIG_NFS_V4_1)
7868 [1] = &nfs_v4_1_minor_ops
,
7870 #if defined(CONFIG_NFS_V4_2)
7871 [2] = &nfs_v4_2_minor_ops
,
7875 static const struct inode_operations nfs4_dir_inode_operations
= {
7876 .create
= nfs_create
,
7877 .lookup
= nfs_lookup
,
7878 .atomic_open
= nfs_atomic_open
,
7880 .unlink
= nfs_unlink
,
7881 .symlink
= nfs_symlink
,
7885 .rename
= nfs_rename
,
7886 .permission
= nfs_permission
,
7887 .getattr
= nfs_getattr
,
7888 .setattr
= nfs_setattr
,
7889 .getxattr
= generic_getxattr
,
7890 .setxattr
= generic_setxattr
,
7891 .listxattr
= generic_listxattr
,
7892 .removexattr
= generic_removexattr
,
7895 static const struct inode_operations nfs4_file_inode_operations
= {
7896 .permission
= nfs_permission
,
7897 .getattr
= nfs_getattr
,
7898 .setattr
= nfs_setattr
,
7899 .getxattr
= generic_getxattr
,
7900 .setxattr
= generic_setxattr
,
7901 .listxattr
= generic_listxattr
,
7902 .removexattr
= generic_removexattr
,
7905 const struct nfs_rpc_ops nfs_v4_clientops
= {
7906 .version
= 4, /* protocol version */
7907 .dentry_ops
= &nfs4_dentry_operations
,
7908 .dir_inode_ops
= &nfs4_dir_inode_operations
,
7909 .file_inode_ops
= &nfs4_file_inode_operations
,
7910 .file_ops
= &nfs4_file_operations
,
7911 .getroot
= nfs4_proc_get_root
,
7912 .submount
= nfs4_submount
,
7913 .try_mount
= nfs4_try_mount
,
7914 .getattr
= nfs4_proc_getattr
,
7915 .setattr
= nfs4_proc_setattr
,
7916 .lookup
= nfs4_proc_lookup
,
7917 .access
= nfs4_proc_access
,
7918 .readlink
= nfs4_proc_readlink
,
7919 .create
= nfs4_proc_create
,
7920 .remove
= nfs4_proc_remove
,
7921 .unlink_setup
= nfs4_proc_unlink_setup
,
7922 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
7923 .unlink_done
= nfs4_proc_unlink_done
,
7924 .rename
= nfs4_proc_rename
,
7925 .rename_setup
= nfs4_proc_rename_setup
,
7926 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
7927 .rename_done
= nfs4_proc_rename_done
,
7928 .link
= nfs4_proc_link
,
7929 .symlink
= nfs4_proc_symlink
,
7930 .mkdir
= nfs4_proc_mkdir
,
7931 .rmdir
= nfs4_proc_remove
,
7932 .readdir
= nfs4_proc_readdir
,
7933 .mknod
= nfs4_proc_mknod
,
7934 .statfs
= nfs4_proc_statfs
,
7935 .fsinfo
= nfs4_proc_fsinfo
,
7936 .pathconf
= nfs4_proc_pathconf
,
7937 .set_capabilities
= nfs4_server_capabilities
,
7938 .decode_dirent
= nfs4_decode_dirent
,
7939 .read_setup
= nfs4_proc_read_setup
,
7940 .read_pageio_init
= pnfs_pageio_init_read
,
7941 .read_rpc_prepare
= nfs4_proc_read_rpc_prepare
,
7942 .read_done
= nfs4_read_done
,
7943 .write_setup
= nfs4_proc_write_setup
,
7944 .write_pageio_init
= pnfs_pageio_init_write
,
7945 .write_rpc_prepare
= nfs4_proc_write_rpc_prepare
,
7946 .write_done
= nfs4_write_done
,
7947 .commit_setup
= nfs4_proc_commit_setup
,
7948 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
7949 .commit_done
= nfs4_commit_done
,
7950 .lock
= nfs4_proc_lock
,
7951 .clear_acl_cache
= nfs4_zap_acl_attr
,
7952 .close_context
= nfs4_close_context
,
7953 .open_context
= nfs4_atomic_open
,
7954 .have_delegation
= nfs4_have_delegation
,
7955 .return_delegation
= nfs4_inode_return_delegation
,
7956 .alloc_client
= nfs4_alloc_client
,
7957 .init_client
= nfs4_init_client
,
7958 .free_client
= nfs4_free_client
,
7959 .create_server
= nfs4_create_server
,
7960 .clone_server
= nfs_clone_server
,
7963 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
7964 .prefix
= XATTR_NAME_NFSV4_ACL
,
7965 .list
= nfs4_xattr_list_nfs4_acl
,
7966 .get
= nfs4_xattr_get_nfs4_acl
,
7967 .set
= nfs4_xattr_set_nfs4_acl
,
7970 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
7971 &nfs4_xattr_nfs4_acl_handler
,
7972 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7973 &nfs4_xattr_nfs4_label_handler
,