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/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4idmap.h"
67 #include "nfs4session.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
78 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
79 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
80 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
81 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*, long *);
82 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
83 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
84 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
85 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
86 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
87 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
88 struct nfs4_label
*olabel
);
89 #ifdef CONFIG_NFS_V4_1
90 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
92 static int nfs41_free_stateid(struct nfs_server
*, nfs4_stateid
*,
96 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
97 static inline struct nfs4_label
*
98 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
99 struct iattr
*sattr
, struct nfs4_label
*label
)
106 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
109 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
110 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
117 nfs4_label_release_security(struct nfs4_label
*label
)
120 security_release_secctx(label
->label
, label
->len
);
122 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
125 return server
->attr_bitmask
;
127 return server
->attr_bitmask_nl
;
130 static inline struct nfs4_label
*
131 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
132 struct iattr
*sattr
, struct nfs4_label
*l
)
135 nfs4_label_release_security(struct nfs4_label
*label
)
138 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
139 { return server
->attr_bitmask
; }
142 /* Prevent leaks of NFSv4 errors into userland */
143 static int nfs4_map_errors(int err
)
148 case -NFS4ERR_RESOURCE
:
149 case -NFS4ERR_LAYOUTTRYLATER
:
150 case -NFS4ERR_RECALLCONFLICT
:
152 case -NFS4ERR_WRONGSEC
:
153 case -NFS4ERR_WRONG_CRED
:
155 case -NFS4ERR_BADOWNER
:
156 case -NFS4ERR_BADNAME
:
158 case -NFS4ERR_SHARE_DENIED
:
160 case -NFS4ERR_MINOR_VERS_MISMATCH
:
161 return -EPROTONOSUPPORT
;
162 case -NFS4ERR_FILE_OPEN
:
165 dprintk("%s could not handle NFSv4 error %d\n",
173 * This is our standard bitmap for GETATTR requests.
175 const u32 nfs4_fattr_bitmap
[3] = {
177 | FATTR4_WORD0_CHANGE
180 | FATTR4_WORD0_FILEID
,
182 | FATTR4_WORD1_NUMLINKS
184 | FATTR4_WORD1_OWNER_GROUP
185 | FATTR4_WORD1_RAWDEV
186 | FATTR4_WORD1_SPACE_USED
187 | FATTR4_WORD1_TIME_ACCESS
188 | FATTR4_WORD1_TIME_METADATA
189 | FATTR4_WORD1_TIME_MODIFY
190 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap
[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID
,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY
,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap
[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID
,
220 const u32 nfs4_statfs_bitmap
[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL
,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap
[3] = {
231 | FATTR4_WORD0_MAXNAME
,
235 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME
,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap
[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS
,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
263 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
264 struct nfs4_readdir_arg
*readdir
)
269 readdir
->cookie
= cookie
;
270 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
275 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start
= p
= kmap_atomic(*readdir
->pages
);
289 *p
++ = xdr_one
; /* next */
290 *p
++ = xdr_zero
; /* cookie, first word */
291 *p
++ = xdr_one
; /* cookie, second word */
292 *p
++ = xdr_one
; /* entry len */
293 memcpy(p
, ".\0\0\0", 4); /* entry */
295 *p
++ = xdr_one
; /* bitmap length */
296 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
297 *p
++ = htonl(8); /* attribute buffer length */
298 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
)));
301 *p
++ = xdr_one
; /* next */
302 *p
++ = xdr_zero
; /* cookie, first word */
303 *p
++ = xdr_two
; /* cookie, second word */
304 *p
++ = xdr_two
; /* entry len */
305 memcpy(p
, "..\0\0", 4); /* entry */
307 *p
++ = xdr_one
; /* bitmap length */
308 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
309 *p
++ = htonl(8); /* attribute buffer length */
310 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
312 readdir
->pgbase
= (char *)p
- (char *)start
;
313 readdir
->count
-= readdir
->pgbase
;
314 kunmap_atomic(start
);
317 static long nfs4_update_delay(long *timeout
)
321 return NFS4_POLL_RETRY_MAX
;
323 *timeout
= NFS4_POLL_RETRY_MIN
;
324 if (*timeout
> NFS4_POLL_RETRY_MAX
)
325 *timeout
= NFS4_POLL_RETRY_MAX
;
331 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
337 freezable_schedule_timeout_killable_unsafe(
338 nfs4_update_delay(timeout
));
339 if (fatal_signal_pending(current
))
344 /* This is the error handling routine for processes that are allowed
347 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
349 struct nfs_client
*clp
= server
->nfs_client
;
350 struct nfs4_state
*state
= exception
->state
;
351 struct inode
*inode
= exception
->inode
;
354 exception
->retry
= 0;
358 case -NFS4ERR_OPENMODE
:
359 case -NFS4ERR_DELEG_REVOKED
:
360 case -NFS4ERR_ADMIN_REVOKED
:
361 case -NFS4ERR_BAD_STATEID
:
362 if (inode
&& nfs4_have_delegation(inode
, FMODE_READ
)) {
363 nfs4_inode_return_delegation(inode
);
364 exception
->retry
= 1;
369 ret
= nfs4_schedule_stateid_recovery(server
, state
);
372 goto wait_on_recovery
;
373 case -NFS4ERR_EXPIRED
:
375 ret
= nfs4_schedule_stateid_recovery(server
, state
);
379 case -NFS4ERR_STALE_STATEID
:
380 case -NFS4ERR_STALE_CLIENTID
:
381 nfs4_schedule_lease_recovery(clp
);
382 goto wait_on_recovery
;
384 ret
= nfs4_schedule_migration_recovery(server
);
387 goto wait_on_recovery
;
388 case -NFS4ERR_LEASE_MOVED
:
389 nfs4_schedule_lease_moved_recovery(clp
);
390 goto wait_on_recovery
;
391 #if defined(CONFIG_NFS_V4_1)
392 case -NFS4ERR_BADSESSION
:
393 case -NFS4ERR_BADSLOT
:
394 case -NFS4ERR_BAD_HIGH_SLOT
:
395 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
396 case -NFS4ERR_DEADSESSION
:
397 case -NFS4ERR_SEQ_FALSE_RETRY
:
398 case -NFS4ERR_SEQ_MISORDERED
:
399 dprintk("%s ERROR: %d Reset session\n", __func__
,
401 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
402 goto wait_on_recovery
;
403 #endif /* defined(CONFIG_NFS_V4_1) */
404 case -NFS4ERR_FILE_OPEN
:
405 if (exception
->timeout
> HZ
) {
406 /* We have retried a decent amount, time to
414 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
417 case -NFS4ERR_RETRY_UNCACHED_REP
:
418 case -NFS4ERR_OLD_STATEID
:
419 exception
->retry
= 1;
421 case -NFS4ERR_BADOWNER
:
422 /* The following works around a Linux server bug! */
423 case -NFS4ERR_BADNAME
:
424 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
425 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
426 exception
->retry
= 1;
427 printk(KERN_WARNING
"NFS: v4 server %s "
428 "does not accept raw "
430 "Reenabling the idmapper.\n",
431 server
->nfs_client
->cl_hostname
);
434 /* We failed to handle the error */
435 return nfs4_map_errors(ret
);
437 ret
= nfs4_wait_clnt_recover(clp
);
438 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
441 exception
->retry
= 1;
446 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
447 * or 'false' otherwise.
449 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
451 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
453 if (flavor
== RPC_AUTH_GSS_KRB5I
||
454 flavor
== RPC_AUTH_GSS_KRB5P
)
460 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
462 spin_lock(&clp
->cl_lock
);
463 if (time_before(clp
->cl_last_renewal
,timestamp
))
464 clp
->cl_last_renewal
= timestamp
;
465 spin_unlock(&clp
->cl_lock
);
468 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
470 do_renew_lease(server
->nfs_client
, timestamp
);
473 struct nfs4_call_sync_data
{
474 const struct nfs_server
*seq_server
;
475 struct nfs4_sequence_args
*seq_args
;
476 struct nfs4_sequence_res
*seq_res
;
479 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
480 struct nfs4_sequence_res
*res
, int cache_reply
)
482 args
->sa_slot
= NULL
;
483 args
->sa_cache_this
= cache_reply
;
484 args
->sa_privileged
= 0;
489 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
491 args
->sa_privileged
= 1;
494 int nfs40_setup_sequence(struct nfs4_slot_table
*tbl
,
495 struct nfs4_sequence_args
*args
,
496 struct nfs4_sequence_res
*res
,
497 struct rpc_task
*task
)
499 struct nfs4_slot
*slot
;
501 /* slot already allocated? */
502 if (res
->sr_slot
!= NULL
)
505 spin_lock(&tbl
->slot_tbl_lock
);
506 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
509 slot
= nfs4_alloc_slot(tbl
);
511 if (slot
== ERR_PTR(-ENOMEM
))
512 task
->tk_timeout
= HZ
>> 2;
515 spin_unlock(&tbl
->slot_tbl_lock
);
517 args
->sa_slot
= slot
;
521 rpc_call_start(task
);
525 if (args
->sa_privileged
)
526 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
527 NULL
, RPC_PRIORITY_PRIVILEGED
);
529 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
530 spin_unlock(&tbl
->slot_tbl_lock
);
533 EXPORT_SYMBOL_GPL(nfs40_setup_sequence
);
535 static int nfs40_sequence_done(struct rpc_task
*task
,
536 struct nfs4_sequence_res
*res
)
538 struct nfs4_slot
*slot
= res
->sr_slot
;
539 struct nfs4_slot_table
*tbl
;
545 spin_lock(&tbl
->slot_tbl_lock
);
546 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
547 nfs4_free_slot(tbl
, slot
);
548 spin_unlock(&tbl
->slot_tbl_lock
);
555 #if defined(CONFIG_NFS_V4_1)
557 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
559 struct nfs4_session
*session
;
560 struct nfs4_slot_table
*tbl
;
561 struct nfs4_slot
*slot
= res
->sr_slot
;
562 bool send_new_highest_used_slotid
= false;
565 session
= tbl
->session
;
567 spin_lock(&tbl
->slot_tbl_lock
);
568 /* Be nice to the server: try to ensure that the last transmitted
569 * value for highest_user_slotid <= target_highest_slotid
571 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
572 send_new_highest_used_slotid
= true;
574 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
575 send_new_highest_used_slotid
= false;
578 nfs4_free_slot(tbl
, slot
);
580 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
581 send_new_highest_used_slotid
= false;
583 spin_unlock(&tbl
->slot_tbl_lock
);
585 if (send_new_highest_used_slotid
)
586 nfs41_server_notify_highest_slotid_update(session
->clp
);
589 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
591 struct nfs4_session
*session
;
592 struct nfs4_slot
*slot
= res
->sr_slot
;
593 struct nfs_client
*clp
;
594 bool interrupted
= false;
599 /* don't increment the sequence number if the task wasn't sent */
600 if (!RPC_WAS_SENT(task
))
603 session
= slot
->table
->session
;
605 if (slot
->interrupted
) {
606 slot
->interrupted
= 0;
610 trace_nfs4_sequence_done(session
, res
);
611 /* Check the SEQUENCE operation status */
612 switch (res
->sr_status
) {
614 /* Update the slot's sequence and clientid lease timer */
617 do_renew_lease(clp
, res
->sr_timestamp
);
618 /* Check sequence flags */
619 if (res
->sr_status_flags
!= 0)
620 nfs4_schedule_lease_recovery(clp
);
621 nfs41_update_target_slotid(slot
->table
, slot
, res
);
625 * sr_status remains 1 if an RPC level error occurred.
626 * The server may or may not have processed the sequence
628 * Mark the slot as having hosted an interrupted RPC call.
630 slot
->interrupted
= 1;
633 /* The server detected a resend of the RPC call and
634 * returned NFS4ERR_DELAY as per Section 2.10.6.2
637 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
642 case -NFS4ERR_BADSLOT
:
644 * The slot id we used was probably retired. Try again
645 * using a different slot id.
648 case -NFS4ERR_SEQ_MISORDERED
:
650 * Was the last operation on this sequence interrupted?
651 * If so, retry after bumping the sequence number.
658 * Could this slot have been previously retired?
659 * If so, then the server may be expecting seq_nr = 1!
661 if (slot
->seq_nr
!= 1) {
666 case -NFS4ERR_SEQ_FALSE_RETRY
:
670 /* Just update the slot sequence no. */
674 /* The session may be reset by one of the error handlers. */
675 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
676 nfs41_sequence_free_slot(res
);
680 if (rpc_restart_call_prepare(task
)) {
686 if (!rpc_restart_call(task
))
688 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
691 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
693 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
695 if (res
->sr_slot
== NULL
)
697 if (!res
->sr_slot
->table
->session
)
698 return nfs40_sequence_done(task
, res
);
699 return nfs41_sequence_done(task
, res
);
701 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
703 int nfs41_setup_sequence(struct nfs4_session
*session
,
704 struct nfs4_sequence_args
*args
,
705 struct nfs4_sequence_res
*res
,
706 struct rpc_task
*task
)
708 struct nfs4_slot
*slot
;
709 struct nfs4_slot_table
*tbl
;
711 dprintk("--> %s\n", __func__
);
712 /* slot already allocated? */
713 if (res
->sr_slot
!= NULL
)
716 tbl
= &session
->fc_slot_table
;
718 task
->tk_timeout
= 0;
720 spin_lock(&tbl
->slot_tbl_lock
);
721 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
722 !args
->sa_privileged
) {
723 /* The state manager will wait until the slot table is empty */
724 dprintk("%s session is draining\n", __func__
);
728 slot
= nfs4_alloc_slot(tbl
);
730 /* If out of memory, try again in 1/4 second */
731 if (slot
== ERR_PTR(-ENOMEM
))
732 task
->tk_timeout
= HZ
>> 2;
733 dprintk("<-- %s: no free slots\n", __func__
);
736 spin_unlock(&tbl
->slot_tbl_lock
);
738 args
->sa_slot
= slot
;
740 dprintk("<-- %s slotid=%u seqid=%u\n", __func__
,
741 slot
->slot_nr
, slot
->seq_nr
);
744 res
->sr_timestamp
= jiffies
;
745 res
->sr_status_flags
= 0;
747 * sr_status is only set in decode_sequence, and so will remain
748 * set to 1 if an rpc level failure occurs.
751 trace_nfs4_setup_sequence(session
, args
);
753 rpc_call_start(task
);
756 /* Privileged tasks are queued with top priority */
757 if (args
->sa_privileged
)
758 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
759 NULL
, RPC_PRIORITY_PRIVILEGED
);
761 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
762 spin_unlock(&tbl
->slot_tbl_lock
);
765 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
767 static int nfs4_setup_sequence(const struct nfs_server
*server
,
768 struct nfs4_sequence_args
*args
,
769 struct nfs4_sequence_res
*res
,
770 struct rpc_task
*task
)
772 struct nfs4_session
*session
= nfs4_get_session(server
);
776 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
779 dprintk("--> %s clp %p session %p sr_slot %u\n",
780 __func__
, session
->clp
, session
, res
->sr_slot
?
781 res
->sr_slot
->slot_nr
: NFS4_NO_SLOT
);
783 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
785 dprintk("<-- %s status=%d\n", __func__
, ret
);
789 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
791 struct nfs4_call_sync_data
*data
= calldata
;
792 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
794 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
796 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
799 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
801 struct nfs4_call_sync_data
*data
= calldata
;
803 nfs41_sequence_done(task
, data
->seq_res
);
806 static const struct rpc_call_ops nfs41_call_sync_ops
= {
807 .rpc_call_prepare
= nfs41_call_sync_prepare
,
808 .rpc_call_done
= nfs41_call_sync_done
,
811 #else /* !CONFIG_NFS_V4_1 */
813 static int nfs4_setup_sequence(const struct nfs_server
*server
,
814 struct nfs4_sequence_args
*args
,
815 struct nfs4_sequence_res
*res
,
816 struct rpc_task
*task
)
818 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
822 int nfs4_sequence_done(struct rpc_task
*task
,
823 struct nfs4_sequence_res
*res
)
825 return nfs40_sequence_done(task
, res
);
827 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
829 #endif /* !CONFIG_NFS_V4_1 */
831 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
833 struct nfs4_call_sync_data
*data
= calldata
;
834 nfs4_setup_sequence(data
->seq_server
,
835 data
->seq_args
, data
->seq_res
, task
);
838 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
840 struct nfs4_call_sync_data
*data
= calldata
;
841 nfs4_sequence_done(task
, data
->seq_res
);
844 static const struct rpc_call_ops nfs40_call_sync_ops
= {
845 .rpc_call_prepare
= nfs40_call_sync_prepare
,
846 .rpc_call_done
= nfs40_call_sync_done
,
849 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
850 struct nfs_server
*server
,
851 struct rpc_message
*msg
,
852 struct nfs4_sequence_args
*args
,
853 struct nfs4_sequence_res
*res
)
856 struct rpc_task
*task
;
857 struct nfs_client
*clp
= server
->nfs_client
;
858 struct nfs4_call_sync_data data
= {
859 .seq_server
= server
,
863 struct rpc_task_setup task_setup
= {
866 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
867 .callback_data
= &data
870 task
= rpc_run_task(&task_setup
);
874 ret
= task
->tk_status
;
880 int nfs4_call_sync(struct rpc_clnt
*clnt
,
881 struct nfs_server
*server
,
882 struct rpc_message
*msg
,
883 struct nfs4_sequence_args
*args
,
884 struct nfs4_sequence_res
*res
,
887 nfs4_init_sequence(args
, res
, cache_reply
);
888 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
891 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
893 struct nfs_inode
*nfsi
= NFS_I(dir
);
895 spin_lock(&dir
->i_lock
);
896 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
897 if (!cinfo
->atomic
|| cinfo
->before
!= dir
->i_version
)
898 nfs_force_lookup_revalidate(dir
);
899 dir
->i_version
= cinfo
->after
;
900 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
901 nfs_fscache_invalidate(dir
);
902 spin_unlock(&dir
->i_lock
);
905 struct nfs4_opendata
{
907 struct nfs_openargs o_arg
;
908 struct nfs_openres o_res
;
909 struct nfs_open_confirmargs c_arg
;
910 struct nfs_open_confirmres c_res
;
911 struct nfs4_string owner_name
;
912 struct nfs4_string group_name
;
913 struct nfs_fattr f_attr
;
914 struct nfs4_label
*f_label
;
916 struct dentry
*dentry
;
917 struct nfs4_state_owner
*owner
;
918 struct nfs4_state
*state
;
920 unsigned long timestamp
;
921 unsigned int rpc_done
: 1;
922 unsigned int file_created
: 1;
923 unsigned int is_recover
: 1;
928 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
929 int err
, struct nfs4_exception
*exception
)
933 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
935 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
936 exception
->retry
= 1;
941 nfs4_map_atomic_open_share(struct nfs_server
*server
,
942 fmode_t fmode
, int openflags
)
946 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
948 res
= NFS4_SHARE_ACCESS_READ
;
951 res
= NFS4_SHARE_ACCESS_WRITE
;
953 case FMODE_READ
|FMODE_WRITE
:
954 res
= NFS4_SHARE_ACCESS_BOTH
;
956 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
958 /* Want no delegation if we're using O_DIRECT */
959 if (openflags
& O_DIRECT
)
960 res
|= NFS4_SHARE_WANT_NO_DELEG
;
965 static enum open_claim_type4
966 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
967 enum open_claim_type4 claim
)
969 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
974 case NFS4_OPEN_CLAIM_FH
:
975 return NFS4_OPEN_CLAIM_NULL
;
976 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
977 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
978 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
979 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
983 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
985 p
->o_res
.f_attr
= &p
->f_attr
;
986 p
->o_res
.f_label
= p
->f_label
;
987 p
->o_res
.seqid
= p
->o_arg
.seqid
;
988 p
->c_res
.seqid
= p
->c_arg
.seqid
;
989 p
->o_res
.server
= p
->o_arg
.server
;
990 p
->o_res
.access_request
= p
->o_arg
.access
;
991 nfs_fattr_init(&p
->f_attr
);
992 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
995 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
996 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
997 const struct iattr
*attrs
,
998 struct nfs4_label
*label
,
999 enum open_claim_type4 claim
,
1002 struct dentry
*parent
= dget_parent(dentry
);
1003 struct inode
*dir
= d_inode(parent
);
1004 struct nfs_server
*server
= NFS_SERVER(dir
);
1005 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1006 struct nfs4_opendata
*p
;
1008 p
= kzalloc(sizeof(*p
), gfp_mask
);
1012 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1013 if (IS_ERR(p
->f_label
))
1016 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1017 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1018 if (IS_ERR(p
->o_arg
.seqid
))
1019 goto err_free_label
;
1020 nfs_sb_active(dentry
->d_sb
);
1021 p
->dentry
= dget(dentry
);
1024 atomic_inc(&sp
->so_count
);
1025 p
->o_arg
.open_flags
= flags
;
1026 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1027 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1029 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1030 * will return permission denied for all bits until close */
1031 if (!(flags
& O_EXCL
)) {
1032 /* ask server to check for all possible rights as results
1034 p
->o_arg
.access
= NFS4_ACCESS_READ
| NFS4_ACCESS_MODIFY
|
1035 NFS4_ACCESS_EXTEND
| NFS4_ACCESS_EXECUTE
;
1037 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1038 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1039 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1040 p
->o_arg
.name
= &dentry
->d_name
;
1041 p
->o_arg
.server
= server
;
1042 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1043 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1044 p
->o_arg
.label
= label
;
1045 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1046 switch (p
->o_arg
.claim
) {
1047 case NFS4_OPEN_CLAIM_NULL
:
1048 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1049 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1050 p
->o_arg
.fh
= NFS_FH(dir
);
1052 case NFS4_OPEN_CLAIM_PREVIOUS
:
1053 case NFS4_OPEN_CLAIM_FH
:
1054 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1055 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1056 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1058 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1061 p
->o_arg
.u
.attrs
= &p
->attrs
;
1062 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1065 verf
[1] = current
->pid
;
1066 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1067 sizeof(p
->o_arg
.u
.verifier
.data
));
1069 p
->c_arg
.fh
= &p
->o_res
.fh
;
1070 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1071 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1072 nfs4_init_opendata_res(p
);
1073 kref_init(&p
->kref
);
1077 nfs4_label_free(p
->f_label
);
1085 static void nfs4_opendata_free(struct kref
*kref
)
1087 struct nfs4_opendata
*p
= container_of(kref
,
1088 struct nfs4_opendata
, kref
);
1089 struct super_block
*sb
= p
->dentry
->d_sb
;
1091 nfs_free_seqid(p
->o_arg
.seqid
);
1092 if (p
->state
!= NULL
)
1093 nfs4_put_open_state(p
->state
);
1094 nfs4_put_state_owner(p
->owner
);
1096 nfs4_label_free(p
->f_label
);
1100 nfs_sb_deactive(sb
);
1101 nfs_fattr_free_names(&p
->f_attr
);
1102 kfree(p
->f_attr
.mdsthreshold
);
1106 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1109 kref_put(&p
->kref
, nfs4_opendata_free
);
1112 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1116 ret
= rpc_wait_for_completion_task(task
);
1120 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1124 if (open_mode
& (O_EXCL
|O_TRUNC
))
1126 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1128 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1129 && state
->n_rdonly
!= 0;
1132 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1133 && state
->n_wronly
!= 0;
1135 case FMODE_READ
|FMODE_WRITE
:
1136 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1137 && state
->n_rdwr
!= 0;
1143 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
1145 if (delegation
== NULL
)
1147 if ((delegation
->type
& fmode
) != fmode
)
1149 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1151 nfs_mark_delegation_referenced(delegation
);
1155 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1164 case FMODE_READ
|FMODE_WRITE
:
1167 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1170 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1172 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1173 bool need_recover
= false;
1175 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1176 need_recover
= true;
1177 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1178 need_recover
= true;
1179 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1180 need_recover
= true;
1182 nfs4_state_mark_reclaim_nograce(clp
, state
);
1185 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1186 nfs4_stateid
*stateid
)
1188 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1190 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1191 nfs_test_and_clear_all_open_stateid(state
);
1194 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1199 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1201 if (state
->n_wronly
)
1202 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1203 if (state
->n_rdonly
)
1204 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1206 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1209 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1210 nfs4_stateid
*stateid
, fmode_t fmode
)
1212 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1213 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1215 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1218 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1221 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1222 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1223 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1225 if (stateid
== NULL
)
1227 /* Handle races with OPEN */
1228 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
) ||
1229 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
)) {
1230 nfs_resync_open_stateid_locked(state
);
1233 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1234 nfs4_stateid_copy(&state
->stateid
, stateid
);
1235 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1238 static void nfs_clear_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1240 write_seqlock(&state
->seqlock
);
1241 nfs_clear_open_stateid_locked(state
, stateid
, fmode
);
1242 write_sequnlock(&state
->seqlock
);
1243 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1244 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1247 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1251 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1254 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1256 case FMODE_READ
|FMODE_WRITE
:
1257 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1259 if (!nfs_need_update_open_stateid(state
, stateid
))
1261 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1262 nfs4_stateid_copy(&state
->stateid
, stateid
);
1263 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1266 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
1269 * Protect the call to nfs4_state_set_mode_locked and
1270 * serialise the stateid update
1272 write_seqlock(&state
->seqlock
);
1273 if (deleg_stateid
!= NULL
) {
1274 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1275 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1277 if (open_stateid
!= NULL
)
1278 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
1279 write_sequnlock(&state
->seqlock
);
1280 spin_lock(&state
->owner
->so_lock
);
1281 update_open_stateflags(state
, fmode
);
1282 spin_unlock(&state
->owner
->so_lock
);
1285 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
1287 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1288 struct nfs_delegation
*deleg_cur
;
1291 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1294 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1295 if (deleg_cur
== NULL
)
1298 spin_lock(&deleg_cur
->lock
);
1299 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1300 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1301 (deleg_cur
->type
& fmode
) != fmode
)
1302 goto no_delegation_unlock
;
1304 if (delegation
== NULL
)
1305 delegation
= &deleg_cur
->stateid
;
1306 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1307 goto no_delegation_unlock
;
1309 nfs_mark_delegation_referenced(deleg_cur
);
1310 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
1312 no_delegation_unlock
:
1313 spin_unlock(&deleg_cur
->lock
);
1317 if (!ret
&& open_stateid
!= NULL
) {
1318 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
1321 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1322 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1327 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1328 const nfs4_stateid
*stateid
)
1330 struct nfs4_state
*state
= lsp
->ls_state
;
1333 spin_lock(&state
->state_lock
);
1334 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1336 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1338 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1341 spin_unlock(&state
->state_lock
);
1345 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1347 struct nfs_delegation
*delegation
;
1350 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1351 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1356 nfs4_inode_return_delegation(inode
);
1359 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1361 struct nfs4_state
*state
= opendata
->state
;
1362 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1363 struct nfs_delegation
*delegation
;
1364 int open_mode
= opendata
->o_arg
.open_flags
;
1365 fmode_t fmode
= opendata
->o_arg
.fmode
;
1366 nfs4_stateid stateid
;
1370 spin_lock(&state
->owner
->so_lock
);
1371 if (can_open_cached(state
, fmode
, open_mode
)) {
1372 update_open_stateflags(state
, fmode
);
1373 spin_unlock(&state
->owner
->so_lock
);
1374 goto out_return_state
;
1376 spin_unlock(&state
->owner
->so_lock
);
1378 delegation
= rcu_dereference(nfsi
->delegation
);
1379 if (!can_open_delegated(delegation
, fmode
)) {
1383 /* Save the delegation */
1384 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1386 nfs_release_seqid(opendata
->o_arg
.seqid
);
1387 if (!opendata
->is_recover
) {
1388 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1394 /* Try to update the stateid using the delegation */
1395 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1396 goto out_return_state
;
1399 return ERR_PTR(ret
);
1401 atomic_inc(&state
->count
);
1406 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1408 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1409 struct nfs_delegation
*delegation
;
1410 int delegation_flags
= 0;
1413 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1415 delegation_flags
= delegation
->flags
;
1417 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_DELEGATE_CUR
) {
1418 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1419 "returning a delegation for "
1420 "OPEN(CLAIM_DELEGATE_CUR)\n",
1422 } else if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1423 nfs_inode_set_delegation(state
->inode
,
1424 data
->owner
->so_cred
,
1427 nfs_inode_reclaim_delegation(state
->inode
,
1428 data
->owner
->so_cred
,
1433 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1434 * and update the nfs4_state.
1436 static struct nfs4_state
*
1437 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1439 struct inode
*inode
= data
->state
->inode
;
1440 struct nfs4_state
*state
= data
->state
;
1443 if (!data
->rpc_done
) {
1444 if (data
->rpc_status
) {
1445 ret
= data
->rpc_status
;
1448 /* cached opens have already been processed */
1452 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1456 if (data
->o_res
.delegation_type
!= 0)
1457 nfs4_opendata_check_deleg(data
, state
);
1459 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1461 atomic_inc(&state
->count
);
1465 return ERR_PTR(ret
);
1469 static struct nfs4_state
*
1470 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1472 struct inode
*inode
;
1473 struct nfs4_state
*state
= NULL
;
1476 if (!data
->rpc_done
) {
1477 state
= nfs4_try_open_cached(data
);
1482 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1484 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1485 ret
= PTR_ERR(inode
);
1489 state
= nfs4_get_open_state(inode
, data
->owner
);
1492 if (data
->o_res
.delegation_type
!= 0)
1493 nfs4_opendata_check_deleg(data
, state
);
1494 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1498 nfs_release_seqid(data
->o_arg
.seqid
);
1503 return ERR_PTR(ret
);
1506 static struct nfs4_state
*
1507 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1509 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1510 return _nfs4_opendata_reclaim_to_nfs4_state(data
);
1511 return _nfs4_opendata_to_nfs4_state(data
);
1514 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1516 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1517 struct nfs_open_context
*ctx
;
1519 spin_lock(&state
->inode
->i_lock
);
1520 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1521 if (ctx
->state
!= state
)
1523 get_nfs_open_context(ctx
);
1524 spin_unlock(&state
->inode
->i_lock
);
1527 spin_unlock(&state
->inode
->i_lock
);
1528 return ERR_PTR(-ENOENT
);
1531 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1532 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1534 struct nfs4_opendata
*opendata
;
1536 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1537 NULL
, NULL
, claim
, GFP_NOFS
);
1538 if (opendata
== NULL
)
1539 return ERR_PTR(-ENOMEM
);
1540 opendata
->state
= state
;
1541 atomic_inc(&state
->count
);
1545 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1547 struct nfs4_state
*newstate
;
1550 if ((opendata
->o_arg
.claim
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
1551 opendata
->o_arg
.claim
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
) &&
1552 (opendata
->o_arg
.u
.delegation_type
& fmode
) != fmode
)
1553 /* This mode can't have been delegated, so we must have
1554 * a valid open_stateid to cover it - not need to reclaim.
1557 opendata
->o_arg
.open_flags
= 0;
1558 opendata
->o_arg
.fmode
= fmode
;
1559 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1560 NFS_SB(opendata
->dentry
->d_sb
),
1562 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1563 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1564 nfs4_init_opendata_res(opendata
);
1565 ret
= _nfs4_recover_proc_open(opendata
);
1568 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1569 if (IS_ERR(newstate
))
1570 return PTR_ERR(newstate
);
1571 nfs4_close_state(newstate
, fmode
);
1576 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1578 struct nfs4_state
*newstate
;
1581 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1582 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1583 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1584 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1585 /* memory barrier prior to reading state->n_* */
1586 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1587 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1589 if (state
->n_rdwr
!= 0) {
1590 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1593 if (newstate
!= state
)
1596 if (state
->n_wronly
!= 0) {
1597 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1600 if (newstate
!= state
)
1603 if (state
->n_rdonly
!= 0) {
1604 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1607 if (newstate
!= state
)
1611 * We may have performed cached opens for all three recoveries.
1612 * Check if we need to update the current stateid.
1614 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1615 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1616 write_seqlock(&state
->seqlock
);
1617 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1618 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1619 write_sequnlock(&state
->seqlock
);
1626 * reclaim state on the server after a reboot.
1628 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1630 struct nfs_delegation
*delegation
;
1631 struct nfs4_opendata
*opendata
;
1632 fmode_t delegation_type
= 0;
1635 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1636 NFS4_OPEN_CLAIM_PREVIOUS
);
1637 if (IS_ERR(opendata
))
1638 return PTR_ERR(opendata
);
1640 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1641 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1642 delegation_type
= delegation
->type
;
1644 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1645 status
= nfs4_open_recover(opendata
, state
);
1646 nfs4_opendata_put(opendata
);
1650 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1652 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1653 struct nfs4_exception exception
= { };
1656 err
= _nfs4_do_open_reclaim(ctx
, state
);
1657 trace_nfs4_open_reclaim(ctx
, 0, err
);
1658 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1660 if (err
!= -NFS4ERR_DELAY
)
1662 nfs4_handle_exception(server
, err
, &exception
);
1663 } while (exception
.retry
);
1667 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1669 struct nfs_open_context
*ctx
;
1672 ctx
= nfs4_state_find_open_context(state
);
1675 ret
= nfs4_do_open_reclaim(ctx
, state
);
1676 put_nfs_open_context(ctx
);
1680 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1684 printk(KERN_ERR
"NFS: %s: unhandled error "
1685 "%d.\n", __func__
, err
);
1691 case -NFS4ERR_BADSESSION
:
1692 case -NFS4ERR_BADSLOT
:
1693 case -NFS4ERR_BAD_HIGH_SLOT
:
1694 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1695 case -NFS4ERR_DEADSESSION
:
1696 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1697 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1699 case -NFS4ERR_STALE_CLIENTID
:
1700 case -NFS4ERR_STALE_STATEID
:
1701 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1702 case -NFS4ERR_EXPIRED
:
1703 /* Don't recall a delegation if it was lost */
1704 nfs4_schedule_lease_recovery(server
->nfs_client
);
1706 case -NFS4ERR_MOVED
:
1707 nfs4_schedule_migration_recovery(server
);
1709 case -NFS4ERR_LEASE_MOVED
:
1710 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1712 case -NFS4ERR_DELEG_REVOKED
:
1713 case -NFS4ERR_ADMIN_REVOKED
:
1714 case -NFS4ERR_BAD_STATEID
:
1715 case -NFS4ERR_OPENMODE
:
1716 nfs_inode_find_state_and_recover(state
->inode
,
1718 nfs4_schedule_stateid_recovery(server
, state
);
1720 case -NFS4ERR_DELAY
:
1721 case -NFS4ERR_GRACE
:
1722 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1726 case -NFS4ERR_DENIED
:
1727 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1733 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1735 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1736 struct nfs4_opendata
*opendata
;
1739 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1740 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
1741 if (IS_ERR(opendata
))
1742 return PTR_ERR(opendata
);
1743 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
1744 err
= nfs4_open_recover(opendata
, state
);
1745 nfs4_opendata_put(opendata
);
1746 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
1749 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
1751 struct nfs4_opendata
*data
= calldata
;
1753 nfs40_setup_sequence(data
->o_arg
.server
->nfs_client
->cl_slot_tbl
,
1754 &data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, task
);
1757 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1759 struct nfs4_opendata
*data
= calldata
;
1761 nfs40_sequence_done(task
, &data
->c_res
.seq_res
);
1763 data
->rpc_status
= task
->tk_status
;
1764 if (data
->rpc_status
== 0) {
1765 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
1766 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1767 renew_lease(data
->o_res
.server
, data
->timestamp
);
1772 static void nfs4_open_confirm_release(void *calldata
)
1774 struct nfs4_opendata
*data
= calldata
;
1775 struct nfs4_state
*state
= NULL
;
1777 /* If this request hasn't been cancelled, do nothing */
1778 if (data
->cancelled
== 0)
1780 /* In case of error, no cleanup! */
1781 if (!data
->rpc_done
)
1783 state
= nfs4_opendata_to_nfs4_state(data
);
1785 nfs4_close_state(state
, data
->o_arg
.fmode
);
1787 nfs4_opendata_put(data
);
1790 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1791 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
1792 .rpc_call_done
= nfs4_open_confirm_done
,
1793 .rpc_release
= nfs4_open_confirm_release
,
1797 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1799 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1801 struct nfs_server
*server
= NFS_SERVER(d_inode(data
->dir
));
1802 struct rpc_task
*task
;
1803 struct rpc_message msg
= {
1804 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1805 .rpc_argp
= &data
->c_arg
,
1806 .rpc_resp
= &data
->c_res
,
1807 .rpc_cred
= data
->owner
->so_cred
,
1809 struct rpc_task_setup task_setup_data
= {
1810 .rpc_client
= server
->client
,
1811 .rpc_message
= &msg
,
1812 .callback_ops
= &nfs4_open_confirm_ops
,
1813 .callback_data
= data
,
1814 .workqueue
= nfsiod_workqueue
,
1815 .flags
= RPC_TASK_ASYNC
,
1819 nfs4_init_sequence(&data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, 1);
1820 kref_get(&data
->kref
);
1822 data
->rpc_status
= 0;
1823 data
->timestamp
= jiffies
;
1824 task
= rpc_run_task(&task_setup_data
);
1826 return PTR_ERR(task
);
1827 status
= nfs4_wait_for_completion_rpc_task(task
);
1829 data
->cancelled
= 1;
1832 status
= data
->rpc_status
;
1837 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1839 struct nfs4_opendata
*data
= calldata
;
1840 struct nfs4_state_owner
*sp
= data
->owner
;
1841 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
1843 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1846 * Check if we still need to send an OPEN call, or if we can use
1847 * a delegation instead.
1849 if (data
->state
!= NULL
) {
1850 struct nfs_delegation
*delegation
;
1852 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1855 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1856 if (data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
&&
1857 data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEG_CUR_FH
&&
1858 can_open_delegated(delegation
, data
->o_arg
.fmode
))
1859 goto unlock_no_action
;
1862 /* Update client id. */
1863 data
->o_arg
.clientid
= clp
->cl_clientid
;
1864 switch (data
->o_arg
.claim
) {
1865 case NFS4_OPEN_CLAIM_PREVIOUS
:
1866 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1867 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1868 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
1869 case NFS4_OPEN_CLAIM_FH
:
1870 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1871 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1873 data
->timestamp
= jiffies
;
1874 if (nfs4_setup_sequence(data
->o_arg
.server
,
1875 &data
->o_arg
.seq_args
,
1876 &data
->o_res
.seq_res
,
1878 nfs_release_seqid(data
->o_arg
.seqid
);
1880 /* Set the create mode (note dependency on the session type) */
1881 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
1882 if (data
->o_arg
.open_flags
& O_EXCL
) {
1883 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
1884 if (nfs4_has_persistent_session(clp
))
1885 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
1886 else if (clp
->cl_mvops
->minor_version
> 0)
1887 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
1893 task
->tk_action
= NULL
;
1895 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
1898 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1900 struct nfs4_opendata
*data
= calldata
;
1902 data
->rpc_status
= task
->tk_status
;
1904 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1907 if (task
->tk_status
== 0) {
1908 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
1909 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1913 data
->rpc_status
= -ELOOP
;
1916 data
->rpc_status
= -EISDIR
;
1919 data
->rpc_status
= -ENOTDIR
;
1922 renew_lease(data
->o_res
.server
, data
->timestamp
);
1923 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1924 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1929 static void nfs4_open_release(void *calldata
)
1931 struct nfs4_opendata
*data
= calldata
;
1932 struct nfs4_state
*state
= NULL
;
1934 /* If this request hasn't been cancelled, do nothing */
1935 if (data
->cancelled
== 0)
1937 /* In case of error, no cleanup! */
1938 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1940 /* In case we need an open_confirm, no cleanup! */
1941 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1943 state
= nfs4_opendata_to_nfs4_state(data
);
1945 nfs4_close_state(state
, data
->o_arg
.fmode
);
1947 nfs4_opendata_put(data
);
1950 static const struct rpc_call_ops nfs4_open_ops
= {
1951 .rpc_call_prepare
= nfs4_open_prepare
,
1952 .rpc_call_done
= nfs4_open_done
,
1953 .rpc_release
= nfs4_open_release
,
1956 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1958 struct inode
*dir
= d_inode(data
->dir
);
1959 struct nfs_server
*server
= NFS_SERVER(dir
);
1960 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1961 struct nfs_openres
*o_res
= &data
->o_res
;
1962 struct rpc_task
*task
;
1963 struct rpc_message msg
= {
1964 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1967 .rpc_cred
= data
->owner
->so_cred
,
1969 struct rpc_task_setup task_setup_data
= {
1970 .rpc_client
= server
->client
,
1971 .rpc_message
= &msg
,
1972 .callback_ops
= &nfs4_open_ops
,
1973 .callback_data
= data
,
1974 .workqueue
= nfsiod_workqueue
,
1975 .flags
= RPC_TASK_ASYNC
,
1979 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
1980 kref_get(&data
->kref
);
1982 data
->rpc_status
= 0;
1983 data
->cancelled
= 0;
1984 data
->is_recover
= 0;
1986 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
1987 data
->is_recover
= 1;
1989 task
= rpc_run_task(&task_setup_data
);
1991 return PTR_ERR(task
);
1992 status
= nfs4_wait_for_completion_rpc_task(task
);
1994 data
->cancelled
= 1;
1997 status
= data
->rpc_status
;
2003 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
2005 struct inode
*dir
= d_inode(data
->dir
);
2006 struct nfs_openres
*o_res
= &data
->o_res
;
2009 status
= nfs4_run_open_task(data
, 1);
2010 if (status
!= 0 || !data
->rpc_done
)
2013 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
2015 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2016 status
= _nfs4_proc_open_confirm(data
);
2025 * Additional permission checks in order to distinguish between an
2026 * open for read, and an open for execute. This works around the
2027 * fact that NFSv4 OPEN treats read and execute permissions as being
2029 * Note that in the non-execute case, we want to turn off permission
2030 * checking if we just created a new file (POSIX open() semantics).
2032 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2033 struct nfs4_opendata
*opendata
,
2034 struct nfs4_state
*state
, fmode_t fmode
,
2037 struct nfs_access_entry cache
;
2040 /* access call failed or for some reason the server doesn't
2041 * support any access modes -- defer access call until later */
2042 if (opendata
->o_res
.access_supported
== 0)
2047 * Use openflags to check for exec, because fmode won't
2048 * always have FMODE_EXEC set when file open for exec.
2050 if (openflags
& __FMODE_EXEC
) {
2051 /* ONLY check for exec rights */
2053 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2057 cache
.jiffies
= jiffies
;
2058 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2059 nfs_access_add_cache(state
->inode
, &cache
);
2061 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
2064 /* even though OPEN succeeded, access is denied. Close the file */
2065 nfs4_close_state(state
, fmode
);
2070 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2072 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2074 struct inode
*dir
= d_inode(data
->dir
);
2075 struct nfs_server
*server
= NFS_SERVER(dir
);
2076 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2077 struct nfs_openres
*o_res
= &data
->o_res
;
2080 status
= nfs4_run_open_task(data
, 0);
2081 if (!data
->rpc_done
)
2084 if (status
== -NFS4ERR_BADNAME
&&
2085 !(o_arg
->open_flags
& O_CREAT
))
2090 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2092 if (o_arg
->open_flags
& O_CREAT
) {
2093 update_changeattr(dir
, &o_res
->cinfo
);
2094 if (o_arg
->open_flags
& O_EXCL
)
2095 data
->file_created
= 1;
2096 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2097 data
->file_created
= 1;
2099 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2100 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2101 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2102 status
= _nfs4_proc_open_confirm(data
);
2106 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
2107 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2111 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
2113 return nfs4_client_recover_expired_lease(server
->nfs_client
);
2118 * reclaim state on the server after a network partition.
2119 * Assumes caller holds the appropriate lock
2121 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2123 struct nfs4_opendata
*opendata
;
2126 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2127 NFS4_OPEN_CLAIM_FH
);
2128 if (IS_ERR(opendata
))
2129 return PTR_ERR(opendata
);
2130 ret
= nfs4_open_recover(opendata
, state
);
2132 d_drop(ctx
->dentry
);
2133 nfs4_opendata_put(opendata
);
2137 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2139 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2140 struct nfs4_exception exception
= { };
2144 err
= _nfs4_open_expired(ctx
, state
);
2145 trace_nfs4_open_expired(ctx
, 0, err
);
2146 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2151 case -NFS4ERR_GRACE
:
2152 case -NFS4ERR_DELAY
:
2153 nfs4_handle_exception(server
, err
, &exception
);
2156 } while (exception
.retry
);
2161 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2163 struct nfs_open_context
*ctx
;
2166 ctx
= nfs4_state_find_open_context(state
);
2169 ret
= nfs4_do_open_expired(ctx
, state
);
2170 put_nfs_open_context(ctx
);
2174 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
)
2176 nfs_remove_bad_delegation(state
->inode
);
2177 write_seqlock(&state
->seqlock
);
2178 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2179 write_sequnlock(&state
->seqlock
);
2180 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2183 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2185 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2186 nfs_finish_clear_delegation_stateid(state
);
2189 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2191 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2192 nfs40_clear_delegation_stateid(state
);
2193 return nfs4_open_expired(sp
, state
);
2196 #if defined(CONFIG_NFS_V4_1)
2197 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2199 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2200 nfs4_stateid stateid
;
2201 struct nfs_delegation
*delegation
;
2202 struct rpc_cred
*cred
;
2205 /* Get the delegation credential for use by test/free_stateid */
2207 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2208 if (delegation
== NULL
) {
2213 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2214 cred
= get_rpccred(delegation
->cred
);
2216 status
= nfs41_test_stateid(server
, &stateid
, cred
);
2217 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2219 if (status
!= NFS_OK
) {
2220 /* Free the stateid unless the server explicitly
2221 * informs us the stateid is unrecognized. */
2222 if (status
!= -NFS4ERR_BAD_STATEID
)
2223 nfs41_free_stateid(server
, &stateid
, cred
);
2224 nfs_finish_clear_delegation_stateid(state
);
2231 * nfs41_check_open_stateid - possibly free an open stateid
2233 * @state: NFSv4 state for an inode
2235 * Returns NFS_OK if recovery for this stateid is now finished.
2236 * Otherwise a negative NFS4ERR value is returned.
2238 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2240 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2241 nfs4_stateid
*stateid
= &state
->open_stateid
;
2242 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2245 /* If a state reset has been done, test_stateid is unneeded */
2246 if ((test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) == 0) &&
2247 (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) == 0) &&
2248 (test_bit(NFS_O_RDWR_STATE
, &state
->flags
) == 0))
2249 return -NFS4ERR_BAD_STATEID
;
2251 status
= nfs41_test_stateid(server
, stateid
, cred
);
2252 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2253 if (status
!= NFS_OK
) {
2254 /* Free the stateid unless the server explicitly
2255 * informs us the stateid is unrecognized. */
2256 if (status
!= -NFS4ERR_BAD_STATEID
)
2257 nfs41_free_stateid(server
, stateid
, cred
);
2259 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2260 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2261 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2262 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2267 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2271 nfs41_check_delegation_stateid(state
);
2272 status
= nfs41_check_open_stateid(state
);
2273 if (status
!= NFS_OK
)
2274 status
= nfs4_open_expired(sp
, state
);
2280 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2281 * fields corresponding to attributes that were used to store the verifier.
2282 * Make sure we clobber those fields in the later setattr call
2284 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
2286 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2287 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2288 sattr
->ia_valid
|= ATTR_ATIME
;
2290 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2291 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2292 sattr
->ia_valid
|= ATTR_MTIME
;
2295 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2298 struct nfs_open_context
*ctx
)
2300 struct nfs4_state_owner
*sp
= opendata
->owner
;
2301 struct nfs_server
*server
= sp
->so_server
;
2302 struct dentry
*dentry
;
2303 struct nfs4_state
*state
;
2307 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2309 ret
= _nfs4_proc_open(opendata
);
2313 state
= nfs4_opendata_to_nfs4_state(opendata
);
2314 ret
= PTR_ERR(state
);
2317 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2318 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2320 dentry
= opendata
->dentry
;
2321 if (d_really_is_negative(dentry
)) {
2322 /* FIXME: Is this d_drop() ever needed? */
2324 dentry
= d_add_unique(dentry
, igrab(state
->inode
));
2325 if (dentry
== NULL
) {
2326 dentry
= opendata
->dentry
;
2327 } else if (dentry
!= ctx
->dentry
) {
2329 ctx
->dentry
= dget(dentry
);
2331 nfs_set_verifier(dentry
,
2332 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2335 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2340 if (d_inode(dentry
) == state
->inode
) {
2341 nfs_inode_attach_open_context(ctx
);
2342 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2343 nfs4_schedule_stateid_recovery(server
, state
);
2350 * Returns a referenced nfs4_state
2352 static int _nfs4_do_open(struct inode
*dir
,
2353 struct nfs_open_context
*ctx
,
2355 struct iattr
*sattr
,
2356 struct nfs4_label
*label
,
2359 struct nfs4_state_owner
*sp
;
2360 struct nfs4_state
*state
= NULL
;
2361 struct nfs_server
*server
= NFS_SERVER(dir
);
2362 struct nfs4_opendata
*opendata
;
2363 struct dentry
*dentry
= ctx
->dentry
;
2364 struct rpc_cred
*cred
= ctx
->cred
;
2365 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2366 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2367 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2368 struct nfs4_label
*olabel
= NULL
;
2371 /* Protect against reboot recovery conflicts */
2373 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2375 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2378 status
= nfs4_recover_expired_lease(server
);
2380 goto err_put_state_owner
;
2381 if (d_really_is_positive(dentry
))
2382 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2384 if (d_really_is_positive(dentry
))
2385 claim
= NFS4_OPEN_CLAIM_FH
;
2386 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2387 label
, claim
, GFP_KERNEL
);
2388 if (opendata
== NULL
)
2389 goto err_put_state_owner
;
2392 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2393 if (IS_ERR(olabel
)) {
2394 status
= PTR_ERR(olabel
);
2395 goto err_opendata_put
;
2399 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2400 if (!opendata
->f_attr
.mdsthreshold
) {
2401 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2402 if (!opendata
->f_attr
.mdsthreshold
)
2403 goto err_free_label
;
2405 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2407 if (d_really_is_positive(dentry
))
2408 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2410 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2412 goto err_free_label
;
2415 if ((opendata
->o_arg
.open_flags
& O_EXCL
) &&
2416 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2417 nfs4_exclusive_attrset(opendata
, sattr
);
2419 nfs_fattr_init(opendata
->o_res
.f_attr
);
2420 status
= nfs4_do_setattr(state
->inode
, cred
,
2421 opendata
->o_res
.f_attr
, sattr
,
2422 state
, label
, olabel
);
2424 nfs_setattr_update_inode(state
->inode
, sattr
,
2425 opendata
->o_res
.f_attr
);
2426 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2429 if (opendata
->file_created
)
2430 *opened
|= FILE_CREATED
;
2432 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2433 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2434 opendata
->f_attr
.mdsthreshold
= NULL
;
2437 nfs4_label_free(olabel
);
2439 nfs4_opendata_put(opendata
);
2440 nfs4_put_state_owner(sp
);
2443 nfs4_label_free(olabel
);
2445 nfs4_opendata_put(opendata
);
2446 err_put_state_owner
:
2447 nfs4_put_state_owner(sp
);
2453 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2454 struct nfs_open_context
*ctx
,
2456 struct iattr
*sattr
,
2457 struct nfs4_label
*label
,
2460 struct nfs_server
*server
= NFS_SERVER(dir
);
2461 struct nfs4_exception exception
= { };
2462 struct nfs4_state
*res
;
2466 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2468 trace_nfs4_open_file(ctx
, flags
, status
);
2471 /* NOTE: BAD_SEQID means the server and client disagree about the
2472 * book-keeping w.r.t. state-changing operations
2473 * (OPEN/CLOSE/LOCK/LOCKU...)
2474 * It is actually a sign of a bug on the client or on the server.
2476 * If we receive a BAD_SEQID error in the particular case of
2477 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2478 * have unhashed the old state_owner for us, and that we can
2479 * therefore safely retry using a new one. We should still warn
2480 * the user though...
2482 if (status
== -NFS4ERR_BAD_SEQID
) {
2483 pr_warn_ratelimited("NFS: v4 server %s "
2484 " returned a bad sequence-id error!\n",
2485 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2486 exception
.retry
= 1;
2490 * BAD_STATEID on OPEN means that the server cancelled our
2491 * state before it received the OPEN_CONFIRM.
2492 * Recover by retrying the request as per the discussion
2493 * on Page 181 of RFC3530.
2495 if (status
== -NFS4ERR_BAD_STATEID
) {
2496 exception
.retry
= 1;
2499 if (status
== -EAGAIN
) {
2500 /* We must have found a delegation */
2501 exception
.retry
= 1;
2504 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2506 res
= ERR_PTR(nfs4_handle_exception(server
,
2507 status
, &exception
));
2508 } while (exception
.retry
);
2512 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2513 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2514 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2515 struct nfs4_label
*olabel
)
2517 struct nfs_server
*server
= NFS_SERVER(inode
);
2518 struct nfs_setattrargs arg
= {
2519 .fh
= NFS_FH(inode
),
2522 .bitmask
= server
->attr_bitmask
,
2525 struct nfs_setattrres res
= {
2530 struct rpc_message msg
= {
2531 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2536 unsigned long timestamp
= jiffies
;
2541 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2543 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2545 nfs_fattr_init(fattr
);
2547 /* Servers should only apply open mode checks for file size changes */
2548 truncate
= (sattr
->ia_valid
& ATTR_SIZE
) ? true : false;
2549 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2551 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
, fmode
)) {
2552 /* Use that stateid */
2553 } else if (truncate
&& state
!= NULL
) {
2554 struct nfs_lockowner lockowner
= {
2555 .l_owner
= current
->files
,
2556 .l_pid
= current
->tgid
,
2558 if (!nfs4_valid_open_stateid(state
))
2560 if (nfs4_select_rw_stateid(&arg
.stateid
, state
, FMODE_WRITE
,
2561 &lockowner
) == -EIO
)
2564 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
2566 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2567 if (status
== 0 && state
!= NULL
)
2568 renew_lease(server
, timestamp
);
2572 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2573 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2574 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2575 struct nfs4_label
*olabel
)
2577 struct nfs_server
*server
= NFS_SERVER(inode
);
2578 struct nfs4_exception exception
= {
2584 err
= _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, ilabel
, olabel
);
2585 trace_nfs4_setattr(inode
, err
);
2587 case -NFS4ERR_OPENMODE
:
2588 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2589 pr_warn_once("NFSv4: server %s is incorrectly "
2590 "applying open mode checks to "
2591 "a SETATTR that is not "
2592 "changing file size.\n",
2593 server
->nfs_client
->cl_hostname
);
2595 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2597 if (sattr
->ia_valid
& ATTR_OPEN
)
2602 err
= nfs4_handle_exception(server
, err
, &exception
);
2603 } while (exception
.retry
);
2608 struct nfs4_closedata
{
2609 struct inode
*inode
;
2610 struct nfs4_state
*state
;
2611 struct nfs_closeargs arg
;
2612 struct nfs_closeres res
;
2613 struct nfs_fattr fattr
;
2614 unsigned long timestamp
;
2619 static void nfs4_free_closedata(void *data
)
2621 struct nfs4_closedata
*calldata
= data
;
2622 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2623 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2626 pnfs_roc_release(calldata
->state
->inode
);
2627 nfs4_put_open_state(calldata
->state
);
2628 nfs_free_seqid(calldata
->arg
.seqid
);
2629 nfs4_put_state_owner(sp
);
2630 nfs_sb_deactive(sb
);
2634 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2636 struct nfs4_closedata
*calldata
= data
;
2637 struct nfs4_state
*state
= calldata
->state
;
2638 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2639 nfs4_stateid
*res_stateid
= NULL
;
2641 dprintk("%s: begin!\n", __func__
);
2642 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2644 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
2645 /* hmm. we are done with the inode, and in the process of freeing
2646 * the state_owner. we keep this around to process errors
2648 switch (task
->tk_status
) {
2650 res_stateid
= &calldata
->res
.stateid
;
2651 if (calldata
->arg
.fmode
== 0 && calldata
->roc
)
2652 pnfs_roc_set_barrier(state
->inode
,
2653 calldata
->roc_barrier
);
2654 renew_lease(server
, calldata
->timestamp
);
2656 case -NFS4ERR_ADMIN_REVOKED
:
2657 case -NFS4ERR_STALE_STATEID
:
2658 case -NFS4ERR_OLD_STATEID
:
2659 case -NFS4ERR_BAD_STATEID
:
2660 case -NFS4ERR_EXPIRED
:
2661 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
2662 &state
->open_stateid
)) {
2663 rpc_restart_call_prepare(task
);
2666 if (calldata
->arg
.fmode
== 0)
2669 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
2670 rpc_restart_call_prepare(task
);
2674 nfs_clear_open_stateid(state
, res_stateid
, calldata
->arg
.fmode
);
2676 nfs_release_seqid(calldata
->arg
.seqid
);
2677 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
2678 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
2681 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
2683 struct nfs4_closedata
*calldata
= data
;
2684 struct nfs4_state
*state
= calldata
->state
;
2685 struct inode
*inode
= calldata
->inode
;
2686 bool is_rdonly
, is_wronly
, is_rdwr
;
2689 dprintk("%s: begin!\n", __func__
);
2690 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
2693 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
2694 spin_lock(&state
->owner
->so_lock
);
2695 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2696 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2697 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2698 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
2699 /* Calculate the change in open mode */
2700 calldata
->arg
.fmode
= 0;
2701 if (state
->n_rdwr
== 0) {
2702 if (state
->n_rdonly
== 0)
2703 call_close
|= is_rdonly
;
2705 calldata
->arg
.fmode
|= FMODE_READ
;
2706 if (state
->n_wronly
== 0)
2707 call_close
|= is_wronly
;
2709 calldata
->arg
.fmode
|= FMODE_WRITE
;
2711 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
2713 if (calldata
->arg
.fmode
== 0)
2714 call_close
|= is_rdwr
;
2716 if (!nfs4_valid_open_stateid(state
))
2718 spin_unlock(&state
->owner
->so_lock
);
2721 /* Note: exit _without_ calling nfs4_close_done */
2725 if (calldata
->arg
.fmode
== 0) {
2726 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
2727 if (calldata
->roc
&&
2728 pnfs_roc_drain(inode
, &calldata
->roc_barrier
, task
)) {
2729 nfs_release_seqid(calldata
->arg
.seqid
);
2733 calldata
->arg
.share_access
=
2734 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
2735 calldata
->arg
.fmode
, 0);
2737 nfs_fattr_init(calldata
->res
.fattr
);
2738 calldata
->timestamp
= jiffies
;
2739 if (nfs4_setup_sequence(NFS_SERVER(inode
),
2740 &calldata
->arg
.seq_args
,
2741 &calldata
->res
.seq_res
,
2743 nfs_release_seqid(calldata
->arg
.seqid
);
2744 dprintk("%s: done!\n", __func__
);
2747 task
->tk_action
= NULL
;
2749 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
2752 static const struct rpc_call_ops nfs4_close_ops
= {
2753 .rpc_call_prepare
= nfs4_close_prepare
,
2754 .rpc_call_done
= nfs4_close_done
,
2755 .rpc_release
= nfs4_free_closedata
,
2758 static bool nfs4_roc(struct inode
*inode
)
2760 if (!nfs_have_layout(inode
))
2762 return pnfs_roc(inode
);
2766 * It is possible for data to be read/written from a mem-mapped file
2767 * after the sys_close call (which hits the vfs layer as a flush).
2768 * This means that we can't safely call nfsv4 close on a file until
2769 * the inode is cleared. This in turn means that we are not good
2770 * NFSv4 citizens - we do not indicate to the server to update the file's
2771 * share state even when we are done with one of the three share
2772 * stateid's in the inode.
2774 * NOTE: Caller must be holding the sp->so_owner semaphore!
2776 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
2778 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2779 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
2780 struct nfs4_closedata
*calldata
;
2781 struct nfs4_state_owner
*sp
= state
->owner
;
2782 struct rpc_task
*task
;
2783 struct rpc_message msg
= {
2784 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
2785 .rpc_cred
= state
->owner
->so_cred
,
2787 struct rpc_task_setup task_setup_data
= {
2788 .rpc_client
= server
->client
,
2789 .rpc_message
= &msg
,
2790 .callback_ops
= &nfs4_close_ops
,
2791 .workqueue
= nfsiod_workqueue
,
2792 .flags
= RPC_TASK_ASYNC
,
2794 int status
= -ENOMEM
;
2796 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
2797 &task_setup_data
.rpc_client
, &msg
);
2799 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
2800 if (calldata
== NULL
)
2802 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
2803 calldata
->inode
= state
->inode
;
2804 calldata
->state
= state
;
2805 calldata
->arg
.fh
= NFS_FH(state
->inode
);
2806 /* Serialization for the sequence id */
2807 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
2808 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
2809 if (IS_ERR(calldata
->arg
.seqid
))
2810 goto out_free_calldata
;
2811 calldata
->arg
.fmode
= 0;
2812 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
2813 calldata
->res
.fattr
= &calldata
->fattr
;
2814 calldata
->res
.seqid
= calldata
->arg
.seqid
;
2815 calldata
->res
.server
= server
;
2816 calldata
->roc
= nfs4_roc(state
->inode
);
2817 nfs_sb_active(calldata
->inode
->i_sb
);
2819 msg
.rpc_argp
= &calldata
->arg
;
2820 msg
.rpc_resp
= &calldata
->res
;
2821 task_setup_data
.callback_data
= calldata
;
2822 task
= rpc_run_task(&task_setup_data
);
2824 return PTR_ERR(task
);
2827 status
= rpc_wait_for_completion_task(task
);
2833 nfs4_put_open_state(state
);
2834 nfs4_put_state_owner(sp
);
2838 static struct inode
*
2839 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
2840 int open_flags
, struct iattr
*attr
, int *opened
)
2842 struct nfs4_state
*state
;
2843 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
2845 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
2847 /* Protect against concurrent sillydeletes */
2848 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
2850 nfs4_label_release_security(label
);
2853 return ERR_CAST(state
);
2854 return state
->inode
;
2857 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2859 if (ctx
->state
== NULL
)
2862 nfs4_close_sync(ctx
->state
, ctx
->mode
);
2864 nfs4_close_state(ctx
->state
, ctx
->mode
);
2867 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2868 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2869 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2871 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2873 struct nfs4_server_caps_arg args
= {
2876 struct nfs4_server_caps_res res
= {};
2877 struct rpc_message msg
= {
2878 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2884 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2886 /* Sanity check the server answers */
2887 switch (server
->nfs_client
->cl_minorversion
) {
2889 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
2890 res
.attr_bitmask
[2] = 0;
2893 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
2896 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
2898 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2899 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2900 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2901 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2902 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2903 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
2904 NFS_CAP_SECURITY_LABEL
);
2905 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
2906 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
2907 server
->caps
|= NFS_CAP_ACLS
;
2908 if (res
.has_links
!= 0)
2909 server
->caps
|= NFS_CAP_HARDLINKS
;
2910 if (res
.has_symlinks
!= 0)
2911 server
->caps
|= NFS_CAP_SYMLINKS
;
2912 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2913 server
->caps
|= NFS_CAP_FILEID
;
2914 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2915 server
->caps
|= NFS_CAP_MODE
;
2916 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2917 server
->caps
|= NFS_CAP_NLINK
;
2918 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2919 server
->caps
|= NFS_CAP_OWNER
;
2920 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2921 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2922 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2923 server
->caps
|= NFS_CAP_ATIME
;
2924 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2925 server
->caps
|= NFS_CAP_CTIME
;
2926 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2927 server
->caps
|= NFS_CAP_MTIME
;
2928 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2929 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2930 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
2932 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
2933 sizeof(server
->attr_bitmask
));
2934 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
2936 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2937 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2938 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2939 server
->cache_consistency_bitmask
[2] = 0;
2940 server
->acl_bitmask
= res
.acl_bitmask
;
2941 server
->fh_expire_type
= res
.fh_expire_type
;
2947 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2949 struct nfs4_exception exception
= { };
2952 err
= nfs4_handle_exception(server
,
2953 _nfs4_server_capabilities(server
, fhandle
),
2955 } while (exception
.retry
);
2959 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2960 struct nfs_fsinfo
*info
)
2963 struct nfs4_lookup_root_arg args
= {
2966 struct nfs4_lookup_res res
= {
2968 .fattr
= info
->fattr
,
2971 struct rpc_message msg
= {
2972 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2977 bitmask
[0] = nfs4_fattr_bitmap
[0];
2978 bitmask
[1] = nfs4_fattr_bitmap
[1];
2980 * Process the label in the upcoming getfattr
2982 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
2984 nfs_fattr_init(info
->fattr
);
2985 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2988 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2989 struct nfs_fsinfo
*info
)
2991 struct nfs4_exception exception
= { };
2994 err
= _nfs4_lookup_root(server
, fhandle
, info
);
2995 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
2998 case -NFS4ERR_WRONGSEC
:
3001 err
= nfs4_handle_exception(server
, err
, &exception
);
3003 } while (exception
.retry
);
3008 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3009 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3011 struct rpc_auth_create_args auth_args
= {
3012 .pseudoflavor
= flavor
,
3014 struct rpc_auth
*auth
;
3017 auth
= rpcauth_create(&auth_args
, server
->client
);
3022 ret
= nfs4_lookup_root(server
, fhandle
, info
);
3028 * Retry pseudoroot lookup with various security flavors. We do this when:
3030 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3031 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3033 * Returns zero on success, or a negative NFS4ERR value, or a
3034 * negative errno value.
3036 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3037 struct nfs_fsinfo
*info
)
3039 /* Per 3530bis 15.33.5 */
3040 static const rpc_authflavor_t flav_array
[] = {
3044 RPC_AUTH_UNIX
, /* courtesy */
3047 int status
= -EPERM
;
3050 if (server
->auth_info
.flavor_len
> 0) {
3051 /* try each flavor specified by user */
3052 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3053 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3054 server
->auth_info
.flavors
[i
]);
3055 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3060 /* no flavors specified by user, try default list */
3061 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3062 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3064 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3071 * -EACCESS could mean that the user doesn't have correct permissions
3072 * to access the mount. It could also mean that we tried to mount
3073 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3074 * existing mount programs don't handle -EACCES very well so it should
3075 * be mapped to -EPERM instead.
3077 if (status
== -EACCES
)
3082 static int nfs4_do_find_root_sec(struct nfs_server
*server
,
3083 struct nfs_fh
*fhandle
, struct nfs_fsinfo
*info
)
3085 int mv
= server
->nfs_client
->cl_minorversion
;
3086 return nfs_v4_minor_ops
[mv
]->find_root_sec(server
, fhandle
, info
);
3090 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3091 * @server: initialized nfs_server handle
3092 * @fhandle: we fill in the pseudo-fs root file handle
3093 * @info: we fill in an FSINFO struct
3094 * @auth_probe: probe the auth flavours
3096 * Returns zero on success, or a negative errno.
3098 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3099 struct nfs_fsinfo
*info
,
3105 status
= nfs4_lookup_root(server
, fhandle
, info
);
3107 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3108 status
= nfs4_do_find_root_sec(server
, fhandle
, info
);
3111 status
= nfs4_server_capabilities(server
, fhandle
);
3113 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3115 return nfs4_map_errors(status
);
3118 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3119 struct nfs_fsinfo
*info
)
3122 struct nfs_fattr
*fattr
= info
->fattr
;
3123 struct nfs4_label
*label
= NULL
;
3125 error
= nfs4_server_capabilities(server
, mntfh
);
3127 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3131 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3133 return PTR_ERR(label
);
3135 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3137 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3138 goto err_free_label
;
3141 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3142 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3143 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3146 nfs4_label_free(label
);
3152 * Get locations and (maybe) other attributes of a referral.
3153 * Note that we'll actually follow the referral later when
3154 * we detect fsid mismatch in inode revalidation
3156 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3157 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3158 struct nfs_fh
*fhandle
)
3160 int status
= -ENOMEM
;
3161 struct page
*page
= NULL
;
3162 struct nfs4_fs_locations
*locations
= NULL
;
3164 page
= alloc_page(GFP_KERNEL
);
3167 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3168 if (locations
== NULL
)
3171 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3176 * If the fsid didn't change, this is a migration event, not a
3177 * referral. Cause us to drop into the exception handler, which
3178 * will kick off migration recovery.
3180 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3181 dprintk("%s: server did not return a different fsid for"
3182 " a referral at %s\n", __func__
, name
->name
);
3183 status
= -NFS4ERR_MOVED
;
3186 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3187 nfs_fixup_referral_attributes(&locations
->fattr
);
3189 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3190 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3191 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3199 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3200 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3202 struct nfs4_getattr_arg args
= {
3204 .bitmask
= server
->attr_bitmask
,
3206 struct nfs4_getattr_res res
= {
3211 struct rpc_message msg
= {
3212 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3217 args
.bitmask
= nfs4_bitmask(server
, label
);
3219 nfs_fattr_init(fattr
);
3220 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3223 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3224 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3226 struct nfs4_exception exception
= { };
3229 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3230 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3231 err
= nfs4_handle_exception(server
, err
,
3233 } while (exception
.retry
);
3238 * The file is not closed if it is opened due to the a request to change
3239 * the size of the file. The open call will not be needed once the
3240 * VFS layer lookup-intents are implemented.
3242 * Close is called when the inode is destroyed.
3243 * If we haven't opened the file for O_WRONLY, we
3244 * need to in the size_change case to obtain a stateid.
3247 * Because OPEN is always done by name in nfsv4, it is
3248 * possible that we opened a different file by the same
3249 * name. We can recognize this race condition, but we
3250 * can't do anything about it besides returning an error.
3252 * This will be fixed with VFS changes (lookup-intent).
3255 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3256 struct iattr
*sattr
)
3258 struct inode
*inode
= d_inode(dentry
);
3259 struct rpc_cred
*cred
= NULL
;
3260 struct nfs4_state
*state
= NULL
;
3261 struct nfs4_label
*label
= NULL
;
3264 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3265 sattr
->ia_valid
& ATTR_SIZE
&&
3266 sattr
->ia_size
< i_size_read(inode
))
3267 pnfs_commit_and_return_layout(inode
);
3269 nfs_fattr_init(fattr
);
3271 /* Deal with open(O_TRUNC) */
3272 if (sattr
->ia_valid
& ATTR_OPEN
)
3273 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3275 /* Optimization: if the end result is no change, don't RPC */
3276 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3279 /* Search for an existing open(O_WRITE) file */
3280 if (sattr
->ia_valid
& ATTR_FILE
) {
3281 struct nfs_open_context
*ctx
;
3283 ctx
= nfs_file_open_context(sattr
->ia_file
);
3290 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3292 return PTR_ERR(label
);
3294 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
3296 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3297 nfs_setsecurity(inode
, fattr
, label
);
3299 nfs4_label_free(label
);
3303 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3304 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3305 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3307 struct nfs_server
*server
= NFS_SERVER(dir
);
3309 struct nfs4_lookup_arg args
= {
3310 .bitmask
= server
->attr_bitmask
,
3311 .dir_fh
= NFS_FH(dir
),
3314 struct nfs4_lookup_res res
= {
3320 struct rpc_message msg
= {
3321 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3326 args
.bitmask
= nfs4_bitmask(server
, label
);
3328 nfs_fattr_init(fattr
);
3330 dprintk("NFS call lookup %s\n", name
->name
);
3331 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3332 dprintk("NFS reply lookup: %d\n", status
);
3336 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3338 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3339 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3340 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3344 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3345 struct qstr
*name
, struct nfs_fh
*fhandle
,
3346 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3348 struct nfs4_exception exception
= { };
3349 struct rpc_clnt
*client
= *clnt
;
3352 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3353 trace_nfs4_lookup(dir
, name
, err
);
3355 case -NFS4ERR_BADNAME
:
3358 case -NFS4ERR_MOVED
:
3359 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3360 if (err
== -NFS4ERR_MOVED
)
3361 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3363 case -NFS4ERR_WRONGSEC
:
3365 if (client
!= *clnt
)
3367 client
= nfs4_negotiate_security(client
, dir
, name
);
3369 return PTR_ERR(client
);
3371 exception
.retry
= 1;
3374 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3376 } while (exception
.retry
);
3381 else if (client
!= *clnt
)
3382 rpc_shutdown_client(client
);
3387 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
3388 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3389 struct nfs4_label
*label
)
3392 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3394 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3395 if (client
!= NFS_CLIENT(dir
)) {
3396 rpc_shutdown_client(client
);
3397 nfs_fixup_secinfo_attributes(fattr
);
3403 nfs4_proc_lookup_mountpoint(struct inode
*dir
, struct qstr
*name
,
3404 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3406 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3409 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3411 return ERR_PTR(status
);
3412 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3415 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3417 struct nfs_server
*server
= NFS_SERVER(inode
);
3418 struct nfs4_accessargs args
= {
3419 .fh
= NFS_FH(inode
),
3420 .bitmask
= server
->cache_consistency_bitmask
,
3422 struct nfs4_accessres res
= {
3425 struct rpc_message msg
= {
3426 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3429 .rpc_cred
= entry
->cred
,
3431 int mode
= entry
->mask
;
3435 * Determine which access bits we want to ask for...
3437 if (mode
& MAY_READ
)
3438 args
.access
|= NFS4_ACCESS_READ
;
3439 if (S_ISDIR(inode
->i_mode
)) {
3440 if (mode
& MAY_WRITE
)
3441 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3442 if (mode
& MAY_EXEC
)
3443 args
.access
|= NFS4_ACCESS_LOOKUP
;
3445 if (mode
& MAY_WRITE
)
3446 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3447 if (mode
& MAY_EXEC
)
3448 args
.access
|= NFS4_ACCESS_EXECUTE
;
3451 res
.fattr
= nfs_alloc_fattr();
3452 if (res
.fattr
== NULL
)
3455 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3457 nfs_access_set_mask(entry
, res
.access
);
3458 nfs_refresh_inode(inode
, res
.fattr
);
3460 nfs_free_fattr(res
.fattr
);
3464 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3466 struct nfs4_exception exception
= { };
3469 err
= _nfs4_proc_access(inode
, entry
);
3470 trace_nfs4_access(inode
, err
);
3471 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3473 } while (exception
.retry
);
3478 * TODO: For the time being, we don't try to get any attributes
3479 * along with any of the zero-copy operations READ, READDIR,
3482 * In the case of the first three, we want to put the GETATTR
3483 * after the read-type operation -- this is because it is hard
3484 * to predict the length of a GETATTR response in v4, and thus
3485 * align the READ data correctly. This means that the GETATTR
3486 * may end up partially falling into the page cache, and we should
3487 * shift it into the 'tail' of the xdr_buf before processing.
3488 * To do this efficiently, we need to know the total length
3489 * of data received, which doesn't seem to be available outside
3492 * In the case of WRITE, we also want to put the GETATTR after
3493 * the operation -- in this case because we want to make sure
3494 * we get the post-operation mtime and size.
3496 * Both of these changes to the XDR layer would in fact be quite
3497 * minor, but I decided to leave them for a subsequent patch.
3499 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3500 unsigned int pgbase
, unsigned int pglen
)
3502 struct nfs4_readlink args
= {
3503 .fh
= NFS_FH(inode
),
3508 struct nfs4_readlink_res res
;
3509 struct rpc_message msg
= {
3510 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3515 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3518 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3519 unsigned int pgbase
, unsigned int pglen
)
3521 struct nfs4_exception exception
= { };
3524 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3525 trace_nfs4_readlink(inode
, err
);
3526 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3528 } while (exception
.retry
);
3533 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3536 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3539 struct nfs4_label l
, *ilabel
= NULL
;
3540 struct nfs_open_context
*ctx
;
3541 struct nfs4_state
*state
;
3545 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
3547 return PTR_ERR(ctx
);
3549 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3551 sattr
->ia_mode
&= ~current_umask();
3552 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, &opened
);
3553 if (IS_ERR(state
)) {
3554 status
= PTR_ERR(state
);
3558 nfs4_label_release_security(ilabel
);
3559 put_nfs_open_context(ctx
);
3563 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3565 struct nfs_server
*server
= NFS_SERVER(dir
);
3566 struct nfs_removeargs args
= {
3570 struct nfs_removeres res
= {
3573 struct rpc_message msg
= {
3574 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3580 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3582 update_changeattr(dir
, &res
.cinfo
);
3586 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3588 struct nfs4_exception exception
= { };
3591 err
= _nfs4_proc_remove(dir
, name
);
3592 trace_nfs4_remove(dir
, name
, err
);
3593 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3595 } while (exception
.retry
);
3599 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
3601 struct nfs_server
*server
= NFS_SERVER(dir
);
3602 struct nfs_removeargs
*args
= msg
->rpc_argp
;
3603 struct nfs_removeres
*res
= msg
->rpc_resp
;
3605 res
->server
= server
;
3606 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
3607 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
3609 nfs_fattr_init(res
->dir_attr
);
3612 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
3614 nfs4_setup_sequence(NFS_SERVER(data
->dir
),
3615 &data
->args
.seq_args
,
3620 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
3622 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
3623 struct nfs_removeres
*res
= &data
->res
;
3625 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3627 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
3628 &data
->timeout
) == -EAGAIN
)
3630 update_changeattr(dir
, &res
->cinfo
);
3634 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
3636 struct nfs_server
*server
= NFS_SERVER(dir
);
3637 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
3638 struct nfs_renameres
*res
= msg
->rpc_resp
;
3640 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
3641 res
->server
= server
;
3642 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
3645 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
3647 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
3648 &data
->args
.seq_args
,
3653 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
3654 struct inode
*new_dir
)
3656 struct nfs_renamedata
*data
= task
->tk_calldata
;
3657 struct nfs_renameres
*res
= &data
->res
;
3659 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3661 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
3664 update_changeattr(old_dir
, &res
->old_cinfo
);
3665 update_changeattr(new_dir
, &res
->new_cinfo
);
3669 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3671 struct nfs_server
*server
= NFS_SERVER(inode
);
3672 struct nfs4_link_arg arg
= {
3673 .fh
= NFS_FH(inode
),
3674 .dir_fh
= NFS_FH(dir
),
3676 .bitmask
= server
->attr_bitmask
,
3678 struct nfs4_link_res res
= {
3682 struct rpc_message msg
= {
3683 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
3687 int status
= -ENOMEM
;
3689 res
.fattr
= nfs_alloc_fattr();
3690 if (res
.fattr
== NULL
)
3693 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3694 if (IS_ERR(res
.label
)) {
3695 status
= PTR_ERR(res
.label
);
3698 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
3700 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3702 update_changeattr(dir
, &res
.cinfo
);
3703 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
3705 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
3709 nfs4_label_free(res
.label
);
3712 nfs_free_fattr(res
.fattr
);
3716 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3718 struct nfs4_exception exception
= { };
3721 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3722 _nfs4_proc_link(inode
, dir
, name
),
3724 } while (exception
.retry
);
3728 struct nfs4_createdata
{
3729 struct rpc_message msg
;
3730 struct nfs4_create_arg arg
;
3731 struct nfs4_create_res res
;
3733 struct nfs_fattr fattr
;
3734 struct nfs4_label
*label
;
3737 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
3738 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
3740 struct nfs4_createdata
*data
;
3742 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3744 struct nfs_server
*server
= NFS_SERVER(dir
);
3746 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3747 if (IS_ERR(data
->label
))
3750 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
3751 data
->msg
.rpc_argp
= &data
->arg
;
3752 data
->msg
.rpc_resp
= &data
->res
;
3753 data
->arg
.dir_fh
= NFS_FH(dir
);
3754 data
->arg
.server
= server
;
3755 data
->arg
.name
= name
;
3756 data
->arg
.attrs
= sattr
;
3757 data
->arg
.ftype
= ftype
;
3758 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
3759 data
->res
.server
= server
;
3760 data
->res
.fh
= &data
->fh
;
3761 data
->res
.fattr
= &data
->fattr
;
3762 data
->res
.label
= data
->label
;
3763 nfs_fattr_init(data
->res
.fattr
);
3771 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
3773 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
3774 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
3776 update_changeattr(dir
, &data
->res
.dir_cinfo
);
3777 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
3782 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
3784 nfs4_label_free(data
->label
);
3788 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3789 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
3790 struct nfs4_label
*label
)
3792 struct nfs4_createdata
*data
;
3793 int status
= -ENAMETOOLONG
;
3795 if (len
> NFS4_MAXPATHLEN
)
3799 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
3803 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
3804 data
->arg
.u
.symlink
.pages
= &page
;
3805 data
->arg
.u
.symlink
.len
= len
;
3806 data
->arg
.label
= label
;
3808 status
= nfs4_do_create(dir
, dentry
, data
);
3810 nfs4_free_createdata(data
);
3815 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3816 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
3818 struct nfs4_exception exception
= { };
3819 struct nfs4_label l
, *label
= NULL
;
3822 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3825 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
3826 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
3827 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3829 } while (exception
.retry
);
3831 nfs4_label_release_security(label
);
3835 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3836 struct iattr
*sattr
, struct nfs4_label
*label
)
3838 struct nfs4_createdata
*data
;
3839 int status
= -ENOMEM
;
3841 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
3845 data
->arg
.label
= label
;
3846 status
= nfs4_do_create(dir
, dentry
, data
);
3848 nfs4_free_createdata(data
);
3853 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3854 struct iattr
*sattr
)
3856 struct nfs4_exception exception
= { };
3857 struct nfs4_label l
, *label
= NULL
;
3860 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3862 sattr
->ia_mode
&= ~current_umask();
3864 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
3865 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
3866 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3868 } while (exception
.retry
);
3869 nfs4_label_release_security(label
);
3874 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3875 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3877 struct inode
*dir
= d_inode(dentry
);
3878 struct nfs4_readdir_arg args
= {
3883 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
3886 struct nfs4_readdir_res res
;
3887 struct rpc_message msg
= {
3888 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
3895 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
3897 (unsigned long long)cookie
);
3898 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
3899 res
.pgbase
= args
.pgbase
;
3900 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3902 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
3903 status
+= args
.pgbase
;
3906 nfs_invalidate_atime(dir
);
3908 dprintk("%s: returns %d\n", __func__
, status
);
3912 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3913 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3915 struct nfs4_exception exception
= { };
3918 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
3919 pages
, count
, plus
);
3920 trace_nfs4_readdir(d_inode(dentry
), err
);
3921 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
3923 } while (exception
.retry
);
3927 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3928 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
3930 struct nfs4_createdata
*data
;
3931 int mode
= sattr
->ia_mode
;
3932 int status
= -ENOMEM
;
3934 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
3939 data
->arg
.ftype
= NF4FIFO
;
3940 else if (S_ISBLK(mode
)) {
3941 data
->arg
.ftype
= NF4BLK
;
3942 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3943 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3945 else if (S_ISCHR(mode
)) {
3946 data
->arg
.ftype
= NF4CHR
;
3947 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3948 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3949 } else if (!S_ISSOCK(mode
)) {
3954 data
->arg
.label
= label
;
3955 status
= nfs4_do_create(dir
, dentry
, data
);
3957 nfs4_free_createdata(data
);
3962 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3963 struct iattr
*sattr
, dev_t rdev
)
3965 struct nfs4_exception exception
= { };
3966 struct nfs4_label l
, *label
= NULL
;
3969 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3971 sattr
->ia_mode
&= ~current_umask();
3973 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
3974 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
3975 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3977 } while (exception
.retry
);
3979 nfs4_label_release_security(label
);
3984 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3985 struct nfs_fsstat
*fsstat
)
3987 struct nfs4_statfs_arg args
= {
3989 .bitmask
= server
->attr_bitmask
,
3991 struct nfs4_statfs_res res
= {
3994 struct rpc_message msg
= {
3995 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4000 nfs_fattr_init(fsstat
->fattr
);
4001 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4004 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4006 struct nfs4_exception exception
= { };
4009 err
= nfs4_handle_exception(server
,
4010 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4012 } while (exception
.retry
);
4016 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4017 struct nfs_fsinfo
*fsinfo
)
4019 struct nfs4_fsinfo_arg args
= {
4021 .bitmask
= server
->attr_bitmask
,
4023 struct nfs4_fsinfo_res res
= {
4026 struct rpc_message msg
= {
4027 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4032 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4035 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4037 struct nfs4_exception exception
= { };
4038 unsigned long now
= jiffies
;
4042 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4043 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4045 struct nfs_client
*clp
= server
->nfs_client
;
4047 spin_lock(&clp
->cl_lock
);
4048 clp
->cl_lease_time
= fsinfo
->lease_time
* HZ
;
4049 clp
->cl_last_renewal
= now
;
4050 spin_unlock(&clp
->cl_lock
);
4053 err
= nfs4_handle_exception(server
, err
, &exception
);
4054 } while (exception
.retry
);
4058 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4062 nfs_fattr_init(fsinfo
->fattr
);
4063 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4065 /* block layout checks this! */
4066 server
->pnfs_blksize
= fsinfo
->blksize
;
4067 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
->layouttype
);
4073 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4074 struct nfs_pathconf
*pathconf
)
4076 struct nfs4_pathconf_arg args
= {
4078 .bitmask
= server
->attr_bitmask
,
4080 struct nfs4_pathconf_res res
= {
4081 .pathconf
= pathconf
,
4083 struct rpc_message msg
= {
4084 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4089 /* None of the pathconf attributes are mandatory to implement */
4090 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4091 memset(pathconf
, 0, sizeof(*pathconf
));
4095 nfs_fattr_init(pathconf
->fattr
);
4096 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4099 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4100 struct nfs_pathconf
*pathconf
)
4102 struct nfs4_exception exception
= { };
4106 err
= nfs4_handle_exception(server
,
4107 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4109 } while (exception
.retry
);
4113 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4114 const struct nfs_open_context
*ctx
,
4115 const struct nfs_lock_context
*l_ctx
,
4118 const struct nfs_lockowner
*lockowner
= NULL
;
4121 lockowner
= &l_ctx
->lockowner
;
4122 return nfs4_select_rw_stateid(stateid
, ctx
->state
, fmode
, lockowner
);
4124 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4126 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4127 const struct nfs_open_context
*ctx
,
4128 const struct nfs_lock_context
*l_ctx
,
4131 nfs4_stateid current_stateid
;
4133 /* If the current stateid represents a lost lock, then exit */
4134 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4136 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4139 static bool nfs4_error_stateid_expired(int err
)
4142 case -NFS4ERR_DELEG_REVOKED
:
4143 case -NFS4ERR_ADMIN_REVOKED
:
4144 case -NFS4ERR_BAD_STATEID
:
4145 case -NFS4ERR_STALE_STATEID
:
4146 case -NFS4ERR_OLD_STATEID
:
4147 case -NFS4ERR_OPENMODE
:
4148 case -NFS4ERR_EXPIRED
:
4154 void __nfs4_read_done_cb(struct nfs_pgio_header
*hdr
)
4156 nfs_invalidate_atime(hdr
->inode
);
4159 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4161 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4163 trace_nfs4_read(hdr
, task
->tk_status
);
4164 if (nfs4_async_handle_error(task
, server
,
4165 hdr
->args
.context
->state
,
4167 rpc_restart_call_prepare(task
);
4171 __nfs4_read_done_cb(hdr
);
4172 if (task
->tk_status
> 0)
4173 renew_lease(server
, hdr
->timestamp
);
4177 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4178 struct nfs_pgio_args
*args
)
4181 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4182 nfs4_stateid_is_current(&args
->stateid
,
4187 rpc_restart_call_prepare(task
);
4191 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4194 dprintk("--> %s\n", __func__
);
4196 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4198 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4200 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4201 nfs4_read_done_cb(task
, hdr
);
4204 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4205 struct rpc_message
*msg
)
4207 hdr
->timestamp
= jiffies
;
4208 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4209 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4210 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4213 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4214 struct nfs_pgio_header
*hdr
)
4216 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
),
4217 &hdr
->args
.seq_args
,
4221 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4222 hdr
->args
.lock_context
,
4223 hdr
->rw_ops
->rw_mode
) == -EIO
)
4225 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4230 static int nfs4_write_done_cb(struct rpc_task
*task
,
4231 struct nfs_pgio_header
*hdr
)
4233 struct inode
*inode
= hdr
->inode
;
4235 trace_nfs4_write(hdr
, task
->tk_status
);
4236 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4237 hdr
->args
.context
->state
,
4239 rpc_restart_call_prepare(task
);
4242 if (task
->tk_status
>= 0) {
4243 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4244 nfs_writeback_update_inode(hdr
);
4249 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4250 struct nfs_pgio_args
*args
)
4253 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4254 nfs4_stateid_is_current(&args
->stateid
,
4259 rpc_restart_call_prepare(task
);
4263 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4265 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4267 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4269 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4270 nfs4_write_done_cb(task
, hdr
);
4274 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4276 /* Don't request attributes for pNFS or O_DIRECT writes */
4277 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4279 /* Otherwise, request attributes if and only if we don't hold
4282 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4285 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4286 struct rpc_message
*msg
)
4288 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4290 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4291 hdr
->args
.bitmask
= NULL
;
4292 hdr
->res
.fattr
= NULL
;
4294 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4296 if (!hdr
->pgio_done_cb
)
4297 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4298 hdr
->res
.server
= server
;
4299 hdr
->timestamp
= jiffies
;
4301 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4302 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4305 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4307 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4308 &data
->args
.seq_args
,
4313 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4315 struct inode
*inode
= data
->inode
;
4317 trace_nfs4_commit(data
, task
->tk_status
);
4318 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4319 NULL
, NULL
) == -EAGAIN
) {
4320 rpc_restart_call_prepare(task
);
4326 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4328 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4330 return data
->commit_done_cb(task
, data
);
4333 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4335 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4337 if (data
->commit_done_cb
== NULL
)
4338 data
->commit_done_cb
= nfs4_commit_done_cb
;
4339 data
->res
.server
= server
;
4340 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4341 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4344 struct nfs4_renewdata
{
4345 struct nfs_client
*client
;
4346 unsigned long timestamp
;
4350 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4351 * standalone procedure for queueing an asynchronous RENEW.
4353 static void nfs4_renew_release(void *calldata
)
4355 struct nfs4_renewdata
*data
= calldata
;
4356 struct nfs_client
*clp
= data
->client
;
4358 if (atomic_read(&clp
->cl_count
) > 1)
4359 nfs4_schedule_state_renewal(clp
);
4360 nfs_put_client(clp
);
4364 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4366 struct nfs4_renewdata
*data
= calldata
;
4367 struct nfs_client
*clp
= data
->client
;
4368 unsigned long timestamp
= data
->timestamp
;
4370 trace_nfs4_renew_async(clp
, task
->tk_status
);
4371 switch (task
->tk_status
) {
4374 case -NFS4ERR_LEASE_MOVED
:
4375 nfs4_schedule_lease_moved_recovery(clp
);
4378 /* Unless we're shutting down, schedule state recovery! */
4379 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4381 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4382 nfs4_schedule_lease_recovery(clp
);
4385 nfs4_schedule_path_down_recovery(clp
);
4387 do_renew_lease(clp
, timestamp
);
4390 static const struct rpc_call_ops nfs4_renew_ops
= {
4391 .rpc_call_done
= nfs4_renew_done
,
4392 .rpc_release
= nfs4_renew_release
,
4395 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4397 struct rpc_message msg
= {
4398 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4402 struct nfs4_renewdata
*data
;
4404 if (renew_flags
== 0)
4406 if (!atomic_inc_not_zero(&clp
->cl_count
))
4408 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4412 data
->timestamp
= jiffies
;
4413 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4414 &nfs4_renew_ops
, data
);
4417 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4419 struct rpc_message msg
= {
4420 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4424 unsigned long now
= jiffies
;
4427 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4430 do_renew_lease(clp
, now
);
4434 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4436 return server
->caps
& NFS_CAP_ACLS
;
4439 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4440 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4443 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4445 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4446 struct page
**pages
, unsigned int *pgbase
)
4448 struct page
*newpage
, **spages
;
4454 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4455 newpage
= alloc_page(GFP_KERNEL
);
4457 if (newpage
== NULL
)
4459 memcpy(page_address(newpage
), buf
, len
);
4464 } while (buflen
!= 0);
4470 __free_page(spages
[rc
-1]);
4474 struct nfs4_cached_acl
{
4480 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4482 struct nfs_inode
*nfsi
= NFS_I(inode
);
4484 spin_lock(&inode
->i_lock
);
4485 kfree(nfsi
->nfs4_acl
);
4486 nfsi
->nfs4_acl
= acl
;
4487 spin_unlock(&inode
->i_lock
);
4490 static void nfs4_zap_acl_attr(struct inode
*inode
)
4492 nfs4_set_cached_acl(inode
, NULL
);
4495 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4497 struct nfs_inode
*nfsi
= NFS_I(inode
);
4498 struct nfs4_cached_acl
*acl
;
4501 spin_lock(&inode
->i_lock
);
4502 acl
= nfsi
->nfs4_acl
;
4505 if (buf
== NULL
) /* user is just asking for length */
4507 if (acl
->cached
== 0)
4509 ret
= -ERANGE
; /* see getxattr(2) man page */
4510 if (acl
->len
> buflen
)
4512 memcpy(buf
, acl
->data
, acl
->len
);
4516 spin_unlock(&inode
->i_lock
);
4520 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4522 struct nfs4_cached_acl
*acl
;
4523 size_t buflen
= sizeof(*acl
) + acl_len
;
4525 if (buflen
<= PAGE_SIZE
) {
4526 acl
= kmalloc(buflen
, GFP_KERNEL
);
4530 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4532 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4539 nfs4_set_cached_acl(inode
, acl
);
4543 * The getxattr API returns the required buffer length when called with a
4544 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4545 * the required buf. On a NULL buf, we send a page of data to the server
4546 * guessing that the ACL request can be serviced by a page. If so, we cache
4547 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4548 * the cache. If not so, we throw away the page, and cache the required
4549 * length. The next getxattr call will then produce another round trip to
4550 * the server, this time with the input buf of the required size.
4552 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4554 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4555 struct nfs_getaclargs args
= {
4556 .fh
= NFS_FH(inode
),
4560 struct nfs_getaclres res
= {
4563 struct rpc_message msg
= {
4564 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4568 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4569 int ret
= -ENOMEM
, i
;
4571 /* As long as we're doing a round trip to the server anyway,
4572 * let's be prepared for a page of acl data. */
4575 if (npages
> ARRAY_SIZE(pages
))
4578 for (i
= 0; i
< npages
; i
++) {
4579 pages
[i
] = alloc_page(GFP_KERNEL
);
4584 /* for decoding across pages */
4585 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
4586 if (!res
.acl_scratch
)
4589 args
.acl_len
= npages
* PAGE_SIZE
;
4590 args
.acl_pgbase
= 0;
4592 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4593 __func__
, buf
, buflen
, npages
, args
.acl_len
);
4594 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
4595 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4599 /* Handle the case where the passed-in buffer is too short */
4600 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
4601 /* Did the user only issue a request for the acl length? */
4607 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
4609 if (res
.acl_len
> buflen
) {
4613 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
4618 for (i
= 0; i
< npages
; i
++)
4620 __free_page(pages
[i
]);
4621 if (res
.acl_scratch
)
4622 __free_page(res
.acl_scratch
);
4626 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4628 struct nfs4_exception exception
= { };
4631 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
4632 trace_nfs4_get_acl(inode
, ret
);
4635 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
4636 } while (exception
.retry
);
4640 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
4642 struct nfs_server
*server
= NFS_SERVER(inode
);
4645 if (!nfs4_server_supports_acls(server
))
4647 ret
= nfs_revalidate_inode(server
, inode
);
4650 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
4651 nfs_zap_acl_cache(inode
);
4652 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
4654 /* -ENOENT is returned if there is no ACL or if there is an ACL
4655 * but no cached acl data, just the acl length */
4657 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
4660 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4662 struct nfs_server
*server
= NFS_SERVER(inode
);
4663 struct page
*pages
[NFS4ACL_MAXPAGES
];
4664 struct nfs_setaclargs arg
= {
4665 .fh
= NFS_FH(inode
),
4669 struct nfs_setaclres res
;
4670 struct rpc_message msg
= {
4671 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
4675 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4678 if (!nfs4_server_supports_acls(server
))
4680 if (npages
> ARRAY_SIZE(pages
))
4682 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
4685 nfs4_inode_return_delegation(inode
);
4686 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4689 * Free each page after tx, so the only ref left is
4690 * held by the network stack
4693 put_page(pages
[i
-1]);
4696 * Acl update can result in inode attribute update.
4697 * so mark the attribute cache invalid.
4699 spin_lock(&inode
->i_lock
);
4700 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
4701 spin_unlock(&inode
->i_lock
);
4702 nfs_access_zap_cache(inode
);
4703 nfs_zap_acl_cache(inode
);
4707 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4709 struct nfs4_exception exception
= { };
4712 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
4713 trace_nfs4_set_acl(inode
, err
);
4714 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4716 } while (exception
.retry
);
4720 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4721 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
4724 struct nfs_server
*server
= NFS_SERVER(inode
);
4725 struct nfs_fattr fattr
;
4726 struct nfs4_label label
= {0, 0, buflen
, buf
};
4728 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4729 struct nfs4_getattr_arg arg
= {
4730 .fh
= NFS_FH(inode
),
4733 struct nfs4_getattr_res res
= {
4738 struct rpc_message msg
= {
4739 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
4745 nfs_fattr_init(&fattr
);
4747 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
4750 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
4752 if (buflen
< label
.len
)
4757 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
4760 struct nfs4_exception exception
= { };
4763 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4767 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
4768 trace_nfs4_get_security_label(inode
, err
);
4769 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4771 } while (exception
.retry
);
4775 static int _nfs4_do_set_security_label(struct inode
*inode
,
4776 struct nfs4_label
*ilabel
,
4777 struct nfs_fattr
*fattr
,
4778 struct nfs4_label
*olabel
)
4781 struct iattr sattr
= {0};
4782 struct nfs_server
*server
= NFS_SERVER(inode
);
4783 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4784 struct nfs_setattrargs arg
= {
4785 .fh
= NFS_FH(inode
),
4791 struct nfs_setattrres res
= {
4796 struct rpc_message msg
= {
4797 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
4803 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
4805 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4807 dprintk("%s failed: %d\n", __func__
, status
);
4812 static int nfs4_do_set_security_label(struct inode
*inode
,
4813 struct nfs4_label
*ilabel
,
4814 struct nfs_fattr
*fattr
,
4815 struct nfs4_label
*olabel
)
4817 struct nfs4_exception exception
= { };
4821 err
= _nfs4_do_set_security_label(inode
, ilabel
,
4823 trace_nfs4_set_security_label(inode
, err
);
4824 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4826 } while (exception
.retry
);
4831 nfs4_set_security_label(struct dentry
*dentry
, const void *buf
, size_t buflen
)
4833 struct nfs4_label ilabel
, *olabel
= NULL
;
4834 struct nfs_fattr fattr
;
4835 struct rpc_cred
*cred
;
4836 struct inode
*inode
= d_inode(dentry
);
4839 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4842 nfs_fattr_init(&fattr
);
4846 ilabel
.label
= (char *)buf
;
4847 ilabel
.len
= buflen
;
4849 cred
= rpc_lookup_cred();
4851 return PTR_ERR(cred
);
4853 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
4854 if (IS_ERR(olabel
)) {
4855 status
= -PTR_ERR(olabel
);
4859 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
4861 nfs_setsecurity(inode
, &fattr
, olabel
);
4863 nfs4_label_free(olabel
);
4868 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4872 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
,
4873 struct nfs4_state
*state
, long *timeout
)
4875 struct nfs_client
*clp
= server
->nfs_client
;
4877 if (task
->tk_status
>= 0)
4879 switch(task
->tk_status
) {
4880 case -NFS4ERR_DELEG_REVOKED
:
4881 case -NFS4ERR_ADMIN_REVOKED
:
4882 case -NFS4ERR_BAD_STATEID
:
4883 case -NFS4ERR_OPENMODE
:
4886 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4887 goto recovery_failed
;
4888 goto wait_on_recovery
;
4889 case -NFS4ERR_EXPIRED
:
4890 if (state
!= NULL
) {
4891 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4892 goto recovery_failed
;
4894 case -NFS4ERR_STALE_STATEID
:
4895 case -NFS4ERR_STALE_CLIENTID
:
4896 nfs4_schedule_lease_recovery(clp
);
4897 goto wait_on_recovery
;
4898 case -NFS4ERR_MOVED
:
4899 if (nfs4_schedule_migration_recovery(server
) < 0)
4900 goto recovery_failed
;
4901 goto wait_on_recovery
;
4902 case -NFS4ERR_LEASE_MOVED
:
4903 nfs4_schedule_lease_moved_recovery(clp
);
4904 goto wait_on_recovery
;
4905 #if defined(CONFIG_NFS_V4_1)
4906 case -NFS4ERR_BADSESSION
:
4907 case -NFS4ERR_BADSLOT
:
4908 case -NFS4ERR_BAD_HIGH_SLOT
:
4909 case -NFS4ERR_DEADSESSION
:
4910 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4911 case -NFS4ERR_SEQ_FALSE_RETRY
:
4912 case -NFS4ERR_SEQ_MISORDERED
:
4913 dprintk("%s ERROR %d, Reset session\n", __func__
,
4915 nfs4_schedule_session_recovery(clp
->cl_session
, task
->tk_status
);
4916 goto wait_on_recovery
;
4917 #endif /* CONFIG_NFS_V4_1 */
4918 case -NFS4ERR_DELAY
:
4919 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
4920 rpc_delay(task
, nfs4_update_delay(timeout
));
4922 case -NFS4ERR_GRACE
:
4923 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
4924 case -NFS4ERR_RETRY_UNCACHED_REP
:
4925 case -NFS4ERR_OLD_STATEID
:
4928 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
4931 task
->tk_status
= -EIO
;
4934 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
4935 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
4936 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
4937 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
4938 goto recovery_failed
;
4940 task
->tk_status
= 0;
4944 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
4945 nfs4_verifier
*bootverf
)
4949 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
4950 /* An impossible timestamp guarantees this value
4951 * will never match a generated boot time. */
4953 verf
[1] = cpu_to_be32(NSEC_PER_SEC
+ 1);
4955 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
4956 verf
[0] = cpu_to_be32(nn
->boot_time
.tv_sec
);
4957 verf
[1] = cpu_to_be32(nn
->boot_time
.tv_nsec
);
4959 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
4963 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
4968 bool retried
= false;
4970 if (clp
->cl_owner_id
!= NULL
)
4974 len
= 10 + strlen(clp
->cl_ipaddr
) + 1 +
4975 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
4977 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
4981 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
4985 * Since this string is allocated at mount time, and held until the
4986 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
4987 * about a memory-reclaim deadlock.
4989 str
= kmalloc(len
, GFP_KERNEL
);
4994 result
= scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
4996 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
4997 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5000 /* Did something change? */
5001 if (result
>= len
) {
5008 clp
->cl_owner_id
= str
;
5013 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5019 len
= 10 + 10 + 1 + 10 + 1 +
5020 strlen(nfs4_client_id_uniquifier
) + 1 +
5021 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5023 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5027 * Since this string is allocated at mount time, and held until the
5028 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5029 * about a memory-reclaim deadlock.
5031 str
= kmalloc(len
, GFP_KERNEL
);
5035 result
= scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5036 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5037 nfs4_client_id_uniquifier
,
5038 clp
->cl_rpcclient
->cl_nodename
);
5039 if (result
>= len
) {
5043 clp
->cl_owner_id
= str
;
5048 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5054 if (clp
->cl_owner_id
!= NULL
)
5057 if (nfs4_client_id_uniquifier
[0] != '\0')
5058 return nfs4_init_uniquifier_client_string(clp
);
5060 len
= 10 + 10 + 1 + 10 + 1 +
5061 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5063 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5067 * Since this string is allocated at mount time, and held until the
5068 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5069 * about a memory-reclaim deadlock.
5071 str
= kmalloc(len
, GFP_KERNEL
);
5075 result
= scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5076 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5077 clp
->cl_rpcclient
->cl_nodename
);
5078 if (result
>= len
) {
5082 clp
->cl_owner_id
= str
;
5087 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5088 * services. Advertise one based on the address family of the
5092 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5094 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5095 return scnprintf(buf
, len
, "tcp6");
5097 return scnprintf(buf
, len
, "tcp");
5100 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5102 struct nfs4_setclientid
*sc
= calldata
;
5104 if (task
->tk_status
== 0)
5105 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5108 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5109 .rpc_call_done
= nfs4_setclientid_done
,
5113 * nfs4_proc_setclientid - Negotiate client ID
5114 * @clp: state data structure
5115 * @program: RPC program for NFSv4 callback service
5116 * @port: IP port number for NFS4 callback service
5117 * @cred: RPC credential to use for this call
5118 * @res: where to place the result
5120 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5122 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5123 unsigned short port
, struct rpc_cred
*cred
,
5124 struct nfs4_setclientid_res
*res
)
5126 nfs4_verifier sc_verifier
;
5127 struct nfs4_setclientid setclientid
= {
5128 .sc_verifier
= &sc_verifier
,
5132 struct rpc_message msg
= {
5133 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5134 .rpc_argp
= &setclientid
,
5138 struct rpc_task
*task
;
5139 struct rpc_task_setup task_setup_data
= {
5140 .rpc_client
= clp
->cl_rpcclient
,
5141 .rpc_message
= &msg
,
5142 .callback_ops
= &nfs4_setclientid_ops
,
5143 .callback_data
= &setclientid
,
5144 .flags
= RPC_TASK_TIMEOUT
,
5148 /* nfs_client_id4 */
5149 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5151 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5152 status
= nfs4_init_uniform_client_string(clp
);
5154 status
= nfs4_init_nonuniform_client_string(clp
);
5160 setclientid
.sc_netid_len
=
5161 nfs4_init_callback_netid(clp
,
5162 setclientid
.sc_netid
,
5163 sizeof(setclientid
.sc_netid
));
5164 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5165 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5166 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5168 dprintk("NFS call setclientid auth=%s, '%s'\n",
5169 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5171 task
= rpc_run_task(&task_setup_data
);
5173 status
= PTR_ERR(task
);
5176 status
= task
->tk_status
;
5177 if (setclientid
.sc_cred
) {
5178 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5179 put_rpccred(setclientid
.sc_cred
);
5183 trace_nfs4_setclientid(clp
, status
);
5184 dprintk("NFS reply setclientid: %d\n", status
);
5189 * nfs4_proc_setclientid_confirm - Confirm client ID
5190 * @clp: state data structure
5191 * @res: result of a previous SETCLIENTID
5192 * @cred: RPC credential to use for this call
5194 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5196 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5197 struct nfs4_setclientid_res
*arg
,
5198 struct rpc_cred
*cred
)
5200 struct rpc_message msg
= {
5201 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5207 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5208 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5210 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5211 trace_nfs4_setclientid_confirm(clp
, status
);
5212 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5216 struct nfs4_delegreturndata
{
5217 struct nfs4_delegreturnargs args
;
5218 struct nfs4_delegreturnres res
;
5220 nfs4_stateid stateid
;
5221 unsigned long timestamp
;
5222 struct nfs_fattr fattr
;
5224 struct inode
*inode
;
5229 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5231 struct nfs4_delegreturndata
*data
= calldata
;
5233 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5236 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5237 switch (task
->tk_status
) {
5239 renew_lease(data
->res
.server
, data
->timestamp
);
5240 case -NFS4ERR_ADMIN_REVOKED
:
5241 case -NFS4ERR_DELEG_REVOKED
:
5242 case -NFS4ERR_BAD_STATEID
:
5243 case -NFS4ERR_OLD_STATEID
:
5244 case -NFS4ERR_STALE_STATEID
:
5245 case -NFS4ERR_EXPIRED
:
5246 task
->tk_status
= 0;
5248 pnfs_roc_set_barrier(data
->inode
, data
->roc_barrier
);
5251 if (nfs4_async_handle_error(task
, data
->res
.server
,
5252 NULL
, NULL
) == -EAGAIN
) {
5253 rpc_restart_call_prepare(task
);
5257 data
->rpc_status
= task
->tk_status
;
5260 static void nfs4_delegreturn_release(void *calldata
)
5262 struct nfs4_delegreturndata
*data
= calldata
;
5263 struct inode
*inode
= data
->inode
;
5267 pnfs_roc_release(inode
);
5268 nfs_iput_and_deactive(inode
);
5273 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5275 struct nfs4_delegreturndata
*d_data
;
5277 d_data
= (struct nfs4_delegreturndata
*)data
;
5280 pnfs_roc_drain(d_data
->inode
, &d_data
->roc_barrier
, task
))
5283 nfs4_setup_sequence(d_data
->res
.server
,
5284 &d_data
->args
.seq_args
,
5285 &d_data
->res
.seq_res
,
5289 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5290 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5291 .rpc_call_done
= nfs4_delegreturn_done
,
5292 .rpc_release
= nfs4_delegreturn_release
,
5295 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5297 struct nfs4_delegreturndata
*data
;
5298 struct nfs_server
*server
= NFS_SERVER(inode
);
5299 struct rpc_task
*task
;
5300 struct rpc_message msg
= {
5301 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5304 struct rpc_task_setup task_setup_data
= {
5305 .rpc_client
= server
->client
,
5306 .rpc_message
= &msg
,
5307 .callback_ops
= &nfs4_delegreturn_ops
,
5308 .flags
= RPC_TASK_ASYNC
,
5312 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5315 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5316 data
->args
.fhandle
= &data
->fh
;
5317 data
->args
.stateid
= &data
->stateid
;
5318 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5319 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5320 nfs4_stateid_copy(&data
->stateid
, stateid
);
5321 data
->res
.fattr
= &data
->fattr
;
5322 data
->res
.server
= server
;
5323 nfs_fattr_init(data
->res
.fattr
);
5324 data
->timestamp
= jiffies
;
5325 data
->rpc_status
= 0;
5326 data
->inode
= nfs_igrab_and_active(inode
);
5328 data
->roc
= nfs4_roc(inode
);
5330 task_setup_data
.callback_data
= data
;
5331 msg
.rpc_argp
= &data
->args
;
5332 msg
.rpc_resp
= &data
->res
;
5333 task
= rpc_run_task(&task_setup_data
);
5335 return PTR_ERR(task
);
5338 status
= nfs4_wait_for_completion_rpc_task(task
);
5341 status
= data
->rpc_status
;
5343 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5345 nfs_refresh_inode(inode
, &data
->fattr
);
5351 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5353 struct nfs_server
*server
= NFS_SERVER(inode
);
5354 struct nfs4_exception exception
= { };
5357 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5358 trace_nfs4_delegreturn(inode
, err
);
5360 case -NFS4ERR_STALE_STATEID
:
5361 case -NFS4ERR_EXPIRED
:
5365 err
= nfs4_handle_exception(server
, err
, &exception
);
5366 } while (exception
.retry
);
5370 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5371 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5374 * sleep, with exponential backoff, and retry the LOCK operation.
5376 static unsigned long
5377 nfs4_set_lock_task_retry(unsigned long timeout
)
5379 freezable_schedule_timeout_killable_unsafe(timeout
);
5381 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
5382 return NFS4_LOCK_MAXTIMEOUT
;
5386 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5388 struct inode
*inode
= state
->inode
;
5389 struct nfs_server
*server
= NFS_SERVER(inode
);
5390 struct nfs_client
*clp
= server
->nfs_client
;
5391 struct nfs_lockt_args arg
= {
5392 .fh
= NFS_FH(inode
),
5395 struct nfs_lockt_res res
= {
5398 struct rpc_message msg
= {
5399 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5402 .rpc_cred
= state
->owner
->so_cred
,
5404 struct nfs4_lock_state
*lsp
;
5407 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5408 status
= nfs4_set_lock_state(state
, request
);
5411 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5412 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5413 arg
.lock_owner
.s_dev
= server
->s_dev
;
5414 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5417 request
->fl_type
= F_UNLCK
;
5419 case -NFS4ERR_DENIED
:
5422 request
->fl_ops
->fl_release_private(request
);
5423 request
->fl_ops
= NULL
;
5428 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5430 struct nfs4_exception exception
= { };
5434 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5435 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5436 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5438 } while (exception
.retry
);
5442 static int do_vfs_lock(struct inode
*inode
, struct file_lock
*fl
)
5445 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
5447 res
= posix_lock_inode_wait(inode
, fl
);
5450 res
= flock_lock_inode_wait(inode
, fl
);
5458 struct nfs4_unlockdata
{
5459 struct nfs_locku_args arg
;
5460 struct nfs_locku_res res
;
5461 struct nfs4_lock_state
*lsp
;
5462 struct nfs_open_context
*ctx
;
5463 struct file_lock fl
;
5464 const struct nfs_server
*server
;
5465 unsigned long timestamp
;
5468 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5469 struct nfs_open_context
*ctx
,
5470 struct nfs4_lock_state
*lsp
,
5471 struct nfs_seqid
*seqid
)
5473 struct nfs4_unlockdata
*p
;
5474 struct inode
*inode
= lsp
->ls_state
->inode
;
5476 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5479 p
->arg
.fh
= NFS_FH(inode
);
5481 p
->arg
.seqid
= seqid
;
5482 p
->res
.seqid
= seqid
;
5484 atomic_inc(&lsp
->ls_count
);
5485 /* Ensure we don't close file until we're done freeing locks! */
5486 p
->ctx
= get_nfs_open_context(ctx
);
5487 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5488 p
->server
= NFS_SERVER(inode
);
5492 static void nfs4_locku_release_calldata(void *data
)
5494 struct nfs4_unlockdata
*calldata
= data
;
5495 nfs_free_seqid(calldata
->arg
.seqid
);
5496 nfs4_put_lock_state(calldata
->lsp
);
5497 put_nfs_open_context(calldata
->ctx
);
5501 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5503 struct nfs4_unlockdata
*calldata
= data
;
5505 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5507 switch (task
->tk_status
) {
5509 renew_lease(calldata
->server
, calldata
->timestamp
);
5510 do_vfs_lock(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5511 if (nfs4_update_lock_stateid(calldata
->lsp
,
5512 &calldata
->res
.stateid
))
5514 case -NFS4ERR_BAD_STATEID
:
5515 case -NFS4ERR_OLD_STATEID
:
5516 case -NFS4ERR_STALE_STATEID
:
5517 case -NFS4ERR_EXPIRED
:
5518 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5519 &calldata
->lsp
->ls_stateid
))
5520 rpc_restart_call_prepare(task
);
5523 if (nfs4_async_handle_error(task
, calldata
->server
,
5524 NULL
, NULL
) == -EAGAIN
)
5525 rpc_restart_call_prepare(task
);
5527 nfs_release_seqid(calldata
->arg
.seqid
);
5530 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5532 struct nfs4_unlockdata
*calldata
= data
;
5534 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5536 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5537 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5538 /* Note: exit _without_ running nfs4_locku_done */
5541 calldata
->timestamp
= jiffies
;
5542 if (nfs4_setup_sequence(calldata
->server
,
5543 &calldata
->arg
.seq_args
,
5544 &calldata
->res
.seq_res
,
5546 nfs_release_seqid(calldata
->arg
.seqid
);
5549 task
->tk_action
= NULL
;
5551 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5554 static const struct rpc_call_ops nfs4_locku_ops
= {
5555 .rpc_call_prepare
= nfs4_locku_prepare
,
5556 .rpc_call_done
= nfs4_locku_done
,
5557 .rpc_release
= nfs4_locku_release_calldata
,
5560 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5561 struct nfs_open_context
*ctx
,
5562 struct nfs4_lock_state
*lsp
,
5563 struct nfs_seqid
*seqid
)
5565 struct nfs4_unlockdata
*data
;
5566 struct rpc_message msg
= {
5567 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5568 .rpc_cred
= ctx
->cred
,
5570 struct rpc_task_setup task_setup_data
= {
5571 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5572 .rpc_message
= &msg
,
5573 .callback_ops
= &nfs4_locku_ops
,
5574 .workqueue
= nfsiod_workqueue
,
5575 .flags
= RPC_TASK_ASYNC
,
5578 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5579 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5581 /* Ensure this is an unlock - when canceling a lock, the
5582 * canceled lock is passed in, and it won't be an unlock.
5584 fl
->fl_type
= F_UNLCK
;
5586 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5588 nfs_free_seqid(seqid
);
5589 return ERR_PTR(-ENOMEM
);
5592 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5593 msg
.rpc_argp
= &data
->arg
;
5594 msg
.rpc_resp
= &data
->res
;
5595 task_setup_data
.callback_data
= data
;
5596 return rpc_run_task(&task_setup_data
);
5599 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5601 struct inode
*inode
= state
->inode
;
5602 struct nfs4_state_owner
*sp
= state
->owner
;
5603 struct nfs_inode
*nfsi
= NFS_I(inode
);
5604 struct nfs_seqid
*seqid
;
5605 struct nfs4_lock_state
*lsp
;
5606 struct rpc_task
*task
;
5607 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
5609 unsigned char fl_flags
= request
->fl_flags
;
5611 status
= nfs4_set_lock_state(state
, request
);
5612 /* Unlock _before_ we do the RPC call */
5613 request
->fl_flags
|= FL_EXISTS
;
5614 /* Exclude nfs_delegation_claim_locks() */
5615 mutex_lock(&sp
->so_delegreturn_mutex
);
5616 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5617 down_read(&nfsi
->rwsem
);
5618 if (do_vfs_lock(inode
, request
) == -ENOENT
) {
5619 up_read(&nfsi
->rwsem
);
5620 mutex_unlock(&sp
->so_delegreturn_mutex
);
5623 up_read(&nfsi
->rwsem
);
5624 mutex_unlock(&sp
->so_delegreturn_mutex
);
5627 /* Is this a delegated lock? */
5628 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5629 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5631 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
5632 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5636 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5637 status
= PTR_ERR(task
);
5640 status
= nfs4_wait_for_completion_rpc_task(task
);
5643 request
->fl_flags
= fl_flags
;
5644 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
5648 struct nfs4_lockdata
{
5649 struct nfs_lock_args arg
;
5650 struct nfs_lock_res res
;
5651 struct nfs4_lock_state
*lsp
;
5652 struct nfs_open_context
*ctx
;
5653 struct file_lock fl
;
5654 unsigned long timestamp
;
5657 struct nfs_server
*server
;
5660 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
5661 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
5664 struct nfs4_lockdata
*p
;
5665 struct inode
*inode
= lsp
->ls_state
->inode
;
5666 struct nfs_server
*server
= NFS_SERVER(inode
);
5667 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
5669 p
= kzalloc(sizeof(*p
), gfp_mask
);
5673 p
->arg
.fh
= NFS_FH(inode
);
5675 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
5676 if (IS_ERR(p
->arg
.open_seqid
))
5678 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
5679 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
5680 if (IS_ERR(p
->arg
.lock_seqid
))
5681 goto out_free_seqid
;
5682 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5683 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5684 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
5685 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
5688 atomic_inc(&lsp
->ls_count
);
5689 p
->ctx
= get_nfs_open_context(ctx
);
5690 get_file(fl
->fl_file
);
5691 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5694 nfs_free_seqid(p
->arg
.open_seqid
);
5700 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
5702 struct nfs4_lockdata
*data
= calldata
;
5703 struct nfs4_state
*state
= data
->lsp
->ls_state
;
5705 dprintk("%s: begin!\n", __func__
);
5706 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
5708 /* Do we need to do an open_to_lock_owner? */
5709 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
5710 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
5711 goto out_release_lock_seqid
;
5713 nfs4_stateid_copy(&data
->arg
.open_stateid
,
5714 &state
->open_stateid
);
5715 data
->arg
.new_lock_owner
= 1;
5716 data
->res
.open_seqid
= data
->arg
.open_seqid
;
5718 data
->arg
.new_lock_owner
= 0;
5719 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
5720 &data
->lsp
->ls_stateid
);
5722 if (!nfs4_valid_open_stateid(state
)) {
5723 data
->rpc_status
= -EBADF
;
5724 task
->tk_action
= NULL
;
5725 goto out_release_open_seqid
;
5727 data
->timestamp
= jiffies
;
5728 if (nfs4_setup_sequence(data
->server
,
5729 &data
->arg
.seq_args
,
5733 out_release_open_seqid
:
5734 nfs_release_seqid(data
->arg
.open_seqid
);
5735 out_release_lock_seqid
:
5736 nfs_release_seqid(data
->arg
.lock_seqid
);
5738 nfs4_sequence_done(task
, &data
->res
.seq_res
);
5739 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
5742 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
5744 struct nfs4_lockdata
*data
= calldata
;
5745 struct nfs4_lock_state
*lsp
= data
->lsp
;
5747 dprintk("%s: begin!\n", __func__
);
5749 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5752 data
->rpc_status
= task
->tk_status
;
5753 switch (task
->tk_status
) {
5755 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
5757 if (data
->arg
.new_lock
) {
5758 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
5759 if (do_vfs_lock(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
5760 rpc_restart_call_prepare(task
);
5764 if (data
->arg
.new_lock_owner
!= 0) {
5765 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
5766 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
5767 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
5768 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
5769 rpc_restart_call_prepare(task
);
5771 case -NFS4ERR_BAD_STATEID
:
5772 case -NFS4ERR_OLD_STATEID
:
5773 case -NFS4ERR_STALE_STATEID
:
5774 case -NFS4ERR_EXPIRED
:
5775 if (data
->arg
.new_lock_owner
!= 0) {
5776 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
5777 &lsp
->ls_state
->open_stateid
))
5778 rpc_restart_call_prepare(task
);
5779 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
5781 rpc_restart_call_prepare(task
);
5783 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
5786 static void nfs4_lock_release(void *calldata
)
5788 struct nfs4_lockdata
*data
= calldata
;
5790 dprintk("%s: begin!\n", __func__
);
5791 nfs_free_seqid(data
->arg
.open_seqid
);
5792 if (data
->cancelled
!= 0) {
5793 struct rpc_task
*task
;
5794 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
5795 data
->arg
.lock_seqid
);
5797 rpc_put_task_async(task
);
5798 dprintk("%s: cancelling lock!\n", __func__
);
5800 nfs_free_seqid(data
->arg
.lock_seqid
);
5801 nfs4_put_lock_state(data
->lsp
);
5802 put_nfs_open_context(data
->ctx
);
5803 fput(data
->fl
.fl_file
);
5805 dprintk("%s: done!\n", __func__
);
5808 static const struct rpc_call_ops nfs4_lock_ops
= {
5809 .rpc_call_prepare
= nfs4_lock_prepare
,
5810 .rpc_call_done
= nfs4_lock_done
,
5811 .rpc_release
= nfs4_lock_release
,
5814 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
5817 case -NFS4ERR_ADMIN_REVOKED
:
5818 case -NFS4ERR_BAD_STATEID
:
5819 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5820 if (new_lock_owner
!= 0 ||
5821 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
5822 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
5824 case -NFS4ERR_STALE_STATEID
:
5825 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5826 case -NFS4ERR_EXPIRED
:
5827 nfs4_schedule_lease_recovery(server
->nfs_client
);
5831 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
5833 struct nfs4_lockdata
*data
;
5834 struct rpc_task
*task
;
5835 struct rpc_message msg
= {
5836 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
5837 .rpc_cred
= state
->owner
->so_cred
,
5839 struct rpc_task_setup task_setup_data
= {
5840 .rpc_client
= NFS_CLIENT(state
->inode
),
5841 .rpc_message
= &msg
,
5842 .callback_ops
= &nfs4_lock_ops
,
5843 .workqueue
= nfsiod_workqueue
,
5844 .flags
= RPC_TASK_ASYNC
,
5848 dprintk("%s: begin!\n", __func__
);
5849 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
5850 fl
->fl_u
.nfs4_fl
.owner
,
5851 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
5855 data
->arg
.block
= 1;
5856 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5857 msg
.rpc_argp
= &data
->arg
;
5858 msg
.rpc_resp
= &data
->res
;
5859 task_setup_data
.callback_data
= data
;
5860 if (recovery_type
> NFS_LOCK_NEW
) {
5861 if (recovery_type
== NFS_LOCK_RECLAIM
)
5862 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
5863 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
5865 data
->arg
.new_lock
= 1;
5866 task
= rpc_run_task(&task_setup_data
);
5868 return PTR_ERR(task
);
5869 ret
= nfs4_wait_for_completion_rpc_task(task
);
5871 ret
= data
->rpc_status
;
5873 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
5874 data
->arg
.new_lock_owner
, ret
);
5876 data
->cancelled
= 1;
5878 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
5882 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
5884 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5885 struct nfs4_exception exception
= {
5886 .inode
= state
->inode
,
5891 /* Cache the lock if possible... */
5892 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5894 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
5895 trace_nfs4_lock_reclaim(request
, state
, F_SETLK
, err
);
5896 if (err
!= -NFS4ERR_DELAY
)
5898 nfs4_handle_exception(server
, err
, &exception
);
5899 } while (exception
.retry
);
5903 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5905 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5906 struct nfs4_exception exception
= {
5907 .inode
= state
->inode
,
5911 err
= nfs4_set_lock_state(state
, request
);
5914 if (!recover_lost_locks
) {
5915 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
5919 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5921 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
5922 trace_nfs4_lock_expired(request
, state
, F_SETLK
, err
);
5926 case -NFS4ERR_GRACE
:
5927 case -NFS4ERR_DELAY
:
5928 nfs4_handle_exception(server
, err
, &exception
);
5931 } while (exception
.retry
);
5936 #if defined(CONFIG_NFS_V4_1)
5938 * nfs41_check_expired_locks - possibly free a lock stateid
5940 * @state: NFSv4 state for an inode
5942 * Returns NFS_OK if recovery for this stateid is now finished.
5943 * Otherwise a negative NFS4ERR value is returned.
5945 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
5947 int status
, ret
= -NFS4ERR_BAD_STATEID
;
5948 struct nfs4_lock_state
*lsp
;
5949 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5951 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
5952 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
5953 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
5955 status
= nfs41_test_stateid(server
,
5958 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
5959 if (status
!= NFS_OK
) {
5960 /* Free the stateid unless the server
5961 * informs us the stateid is unrecognized. */
5962 if (status
!= -NFS4ERR_BAD_STATEID
)
5963 nfs41_free_stateid(server
,
5966 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
5975 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5977 int status
= NFS_OK
;
5979 if (test_bit(LK_STATE_IN_USE
, &state
->flags
))
5980 status
= nfs41_check_expired_locks(state
);
5981 if (status
!= NFS_OK
)
5982 status
= nfs4_lock_expired(state
, request
);
5987 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5989 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
5990 unsigned char fl_flags
= request
->fl_flags
;
5991 int status
= -ENOLCK
;
5993 if ((fl_flags
& FL_POSIX
) &&
5994 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
5996 /* Is this a delegated open? */
5997 status
= nfs4_set_lock_state(state
, request
);
6000 request
->fl_flags
|= FL_ACCESS
;
6001 status
= do_vfs_lock(state
->inode
, request
);
6004 down_read(&nfsi
->rwsem
);
6005 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6006 /* Yes: cache locks! */
6007 /* ...but avoid races with delegation recall... */
6008 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6009 status
= do_vfs_lock(state
->inode
, request
);
6010 up_read(&nfsi
->rwsem
);
6013 up_read(&nfsi
->rwsem
);
6014 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6016 request
->fl_flags
= fl_flags
;
6020 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6022 struct nfs4_exception exception
= {
6024 .inode
= state
->inode
,
6029 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6030 trace_nfs4_set_lock(request
, state
, cmd
, err
);
6031 if (err
== -NFS4ERR_DENIED
)
6033 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6035 } while (exception
.retry
);
6040 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6042 struct nfs_open_context
*ctx
;
6043 struct nfs4_state
*state
;
6044 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6047 /* verify open state */
6048 ctx
= nfs_file_open_context(filp
);
6051 if (request
->fl_start
< 0 || request
->fl_end
< 0)
6054 if (IS_GETLK(cmd
)) {
6056 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6060 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6063 if (request
->fl_type
== F_UNLCK
) {
6065 return nfs4_proc_unlck(state
, cmd
, request
);
6072 * Don't rely on the VFS having checked the file open mode,
6073 * since it won't do this for flock() locks.
6075 switch (request
->fl_type
) {
6077 if (!(filp
->f_mode
& FMODE_READ
))
6081 if (!(filp
->f_mode
& FMODE_WRITE
))
6086 status
= nfs4_proc_setlk(state
, cmd
, request
);
6087 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6089 timeout
= nfs4_set_lock_task_retry(timeout
);
6090 status
= -ERESTARTSYS
;
6093 } while(status
< 0);
6097 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6099 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6102 err
= nfs4_set_lock_state(state
, fl
);
6105 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6106 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6109 struct nfs_release_lockowner_data
{
6110 struct nfs4_lock_state
*lsp
;
6111 struct nfs_server
*server
;
6112 struct nfs_release_lockowner_args args
;
6113 struct nfs_release_lockowner_res res
;
6114 unsigned long timestamp
;
6117 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6119 struct nfs_release_lockowner_data
*data
= calldata
;
6120 struct nfs_server
*server
= data
->server
;
6121 nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
6122 &data
->args
.seq_args
, &data
->res
.seq_res
, task
);
6123 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6124 data
->timestamp
= jiffies
;
6127 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6129 struct nfs_release_lockowner_data
*data
= calldata
;
6130 struct nfs_server
*server
= data
->server
;
6132 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6134 switch (task
->tk_status
) {
6136 renew_lease(server
, data
->timestamp
);
6138 case -NFS4ERR_STALE_CLIENTID
:
6139 case -NFS4ERR_EXPIRED
:
6140 nfs4_schedule_lease_recovery(server
->nfs_client
);
6142 case -NFS4ERR_LEASE_MOVED
:
6143 case -NFS4ERR_DELAY
:
6144 if (nfs4_async_handle_error(task
, server
,
6145 NULL
, NULL
) == -EAGAIN
)
6146 rpc_restart_call_prepare(task
);
6150 static void nfs4_release_lockowner_release(void *calldata
)
6152 struct nfs_release_lockowner_data
*data
= calldata
;
6153 nfs4_free_lock_state(data
->server
, data
->lsp
);
6157 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6158 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6159 .rpc_call_done
= nfs4_release_lockowner_done
,
6160 .rpc_release
= nfs4_release_lockowner_release
,
6164 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6166 struct nfs_release_lockowner_data
*data
;
6167 struct rpc_message msg
= {
6168 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6171 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6174 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6178 data
->server
= server
;
6179 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6180 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6181 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6183 msg
.rpc_argp
= &data
->args
;
6184 msg
.rpc_resp
= &data
->res
;
6185 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6186 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6189 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6191 static int nfs4_xattr_set_nfs4_acl(struct dentry
*dentry
, const char *key
,
6192 const void *buf
, size_t buflen
,
6193 int flags
, int type
)
6195 if (strcmp(key
, "") != 0)
6198 return nfs4_proc_set_acl(d_inode(dentry
), buf
, buflen
);
6201 static int nfs4_xattr_get_nfs4_acl(struct dentry
*dentry
, const char *key
,
6202 void *buf
, size_t buflen
, int type
)
6204 if (strcmp(key
, "") != 0)
6207 return nfs4_proc_get_acl(d_inode(dentry
), buf
, buflen
);
6210 static size_t nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
, char *list
,
6211 size_t list_len
, const char *name
,
6212 size_t name_len
, int type
)
6214 size_t len
= sizeof(XATTR_NAME_NFSV4_ACL
);
6216 if (!nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
))))
6219 if (list
&& len
<= list_len
)
6220 memcpy(list
, XATTR_NAME_NFSV4_ACL
, len
);
6224 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6225 static inline int nfs4_server_supports_labels(struct nfs_server
*server
)
6227 return server
->caps
& NFS_CAP_SECURITY_LABEL
;
6230 static int nfs4_xattr_set_nfs4_label(struct dentry
*dentry
, const char *key
,
6231 const void *buf
, size_t buflen
,
6232 int flags
, int type
)
6234 if (security_ismaclabel(key
))
6235 return nfs4_set_security_label(dentry
, buf
, buflen
);
6240 static int nfs4_xattr_get_nfs4_label(struct dentry
*dentry
, const char *key
,
6241 void *buf
, size_t buflen
, int type
)
6243 if (security_ismaclabel(key
))
6244 return nfs4_get_security_label(d_inode(dentry
), buf
, buflen
);
6248 static size_t nfs4_xattr_list_nfs4_label(struct dentry
*dentry
, char *list
,
6249 size_t list_len
, const char *name
,
6250 size_t name_len
, int type
)
6254 if (nfs_server_capable(d_inode(dentry
), NFS_CAP_SECURITY_LABEL
)) {
6255 len
= security_inode_listsecurity(d_inode(dentry
), NULL
, 0);
6256 if (list
&& len
<= list_len
)
6257 security_inode_listsecurity(d_inode(dentry
), list
, len
);
6262 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6263 .prefix
= XATTR_SECURITY_PREFIX
,
6264 .list
= nfs4_xattr_list_nfs4_label
,
6265 .get
= nfs4_xattr_get_nfs4_label
,
6266 .set
= nfs4_xattr_set_nfs4_label
,
6272 * nfs_fhget will use either the mounted_on_fileid or the fileid
6274 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6276 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6277 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6278 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6279 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6282 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6283 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6284 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6288 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6289 const struct qstr
*name
,
6290 struct nfs4_fs_locations
*fs_locations
,
6293 struct nfs_server
*server
= NFS_SERVER(dir
);
6295 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6297 struct nfs4_fs_locations_arg args
= {
6298 .dir_fh
= NFS_FH(dir
),
6303 struct nfs4_fs_locations_res res
= {
6304 .fs_locations
= fs_locations
,
6306 struct rpc_message msg
= {
6307 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6313 dprintk("%s: start\n", __func__
);
6315 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6316 * is not supported */
6317 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6318 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6320 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6322 nfs_fattr_init(&fs_locations
->fattr
);
6323 fs_locations
->server
= server
;
6324 fs_locations
->nlocations
= 0;
6325 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6326 dprintk("%s: returned status = %d\n", __func__
, status
);
6330 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6331 const struct qstr
*name
,
6332 struct nfs4_fs_locations
*fs_locations
,
6335 struct nfs4_exception exception
= { };
6338 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6339 fs_locations
, page
);
6340 trace_nfs4_get_fs_locations(dir
, name
, err
);
6341 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6343 } while (exception
.retry
);
6348 * This operation also signals the server that this client is
6349 * performing migration recovery. The server can stop returning
6350 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6351 * appended to this compound to identify the client ID which is
6352 * performing recovery.
6354 static int _nfs40_proc_get_locations(struct inode
*inode
,
6355 struct nfs4_fs_locations
*locations
,
6356 struct page
*page
, struct rpc_cred
*cred
)
6358 struct nfs_server
*server
= NFS_SERVER(inode
);
6359 struct rpc_clnt
*clnt
= server
->client
;
6361 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6363 struct nfs4_fs_locations_arg args
= {
6364 .clientid
= server
->nfs_client
->cl_clientid
,
6365 .fh
= NFS_FH(inode
),
6368 .migration
= 1, /* skip LOOKUP */
6369 .renew
= 1, /* append RENEW */
6371 struct nfs4_fs_locations_res res
= {
6372 .fs_locations
= locations
,
6376 struct rpc_message msg
= {
6377 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6382 unsigned long now
= jiffies
;
6385 nfs_fattr_init(&locations
->fattr
);
6386 locations
->server
= server
;
6387 locations
->nlocations
= 0;
6389 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6390 nfs4_set_sequence_privileged(&args
.seq_args
);
6391 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6392 &args
.seq_args
, &res
.seq_res
);
6396 renew_lease(server
, now
);
6400 #ifdef CONFIG_NFS_V4_1
6403 * This operation also signals the server that this client is
6404 * performing migration recovery. The server can stop asserting
6405 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6406 * performing this operation is identified in the SEQUENCE
6407 * operation in this compound.
6409 * When the client supports GETATTR(fs_locations_info), it can
6410 * be plumbed in here.
6412 static int _nfs41_proc_get_locations(struct inode
*inode
,
6413 struct nfs4_fs_locations
*locations
,
6414 struct page
*page
, struct rpc_cred
*cred
)
6416 struct nfs_server
*server
= NFS_SERVER(inode
);
6417 struct rpc_clnt
*clnt
= server
->client
;
6419 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6421 struct nfs4_fs_locations_arg args
= {
6422 .fh
= NFS_FH(inode
),
6425 .migration
= 1, /* skip LOOKUP */
6427 struct nfs4_fs_locations_res res
= {
6428 .fs_locations
= locations
,
6431 struct rpc_message msg
= {
6432 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6439 nfs_fattr_init(&locations
->fattr
);
6440 locations
->server
= server
;
6441 locations
->nlocations
= 0;
6443 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6444 nfs4_set_sequence_privileged(&args
.seq_args
);
6445 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6446 &args
.seq_args
, &res
.seq_res
);
6447 if (status
== NFS4_OK
&&
6448 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6449 status
= -NFS4ERR_LEASE_MOVED
;
6453 #endif /* CONFIG_NFS_V4_1 */
6456 * nfs4_proc_get_locations - discover locations for a migrated FSID
6457 * @inode: inode on FSID that is migrating
6458 * @locations: result of query
6460 * @cred: credential to use for this operation
6462 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6463 * operation failed, or a negative errno if a local error occurred.
6465 * On success, "locations" is filled in, but if the server has
6466 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6469 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6470 * from this client that require migration recovery.
6472 int nfs4_proc_get_locations(struct inode
*inode
,
6473 struct nfs4_fs_locations
*locations
,
6474 struct page
*page
, struct rpc_cred
*cred
)
6476 struct nfs_server
*server
= NFS_SERVER(inode
);
6477 struct nfs_client
*clp
= server
->nfs_client
;
6478 const struct nfs4_mig_recovery_ops
*ops
=
6479 clp
->cl_mvops
->mig_recovery_ops
;
6480 struct nfs4_exception exception
= { };
6483 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6484 (unsigned long long)server
->fsid
.major
,
6485 (unsigned long long)server
->fsid
.minor
,
6487 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6490 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6491 if (status
!= -NFS4ERR_DELAY
)
6493 nfs4_handle_exception(server
, status
, &exception
);
6494 } while (exception
.retry
);
6499 * This operation also signals the server that this client is
6500 * performing "lease moved" recovery. The server can stop
6501 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6502 * is appended to this compound to identify the client ID which is
6503 * performing recovery.
6505 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6507 struct nfs_server
*server
= NFS_SERVER(inode
);
6508 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
6509 struct rpc_clnt
*clnt
= server
->client
;
6510 struct nfs4_fsid_present_arg args
= {
6511 .fh
= NFS_FH(inode
),
6512 .clientid
= clp
->cl_clientid
,
6513 .renew
= 1, /* append RENEW */
6515 struct nfs4_fsid_present_res res
= {
6518 struct rpc_message msg
= {
6519 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6524 unsigned long now
= jiffies
;
6527 res
.fh
= nfs_alloc_fhandle();
6531 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6532 nfs4_set_sequence_privileged(&args
.seq_args
);
6533 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6534 &args
.seq_args
, &res
.seq_res
);
6535 nfs_free_fhandle(res
.fh
);
6539 do_renew_lease(clp
, now
);
6543 #ifdef CONFIG_NFS_V4_1
6546 * This operation also signals the server that this client is
6547 * performing "lease moved" recovery. The server can stop asserting
6548 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6549 * this operation is identified in the SEQUENCE operation in this
6552 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6554 struct nfs_server
*server
= NFS_SERVER(inode
);
6555 struct rpc_clnt
*clnt
= server
->client
;
6556 struct nfs4_fsid_present_arg args
= {
6557 .fh
= NFS_FH(inode
),
6559 struct nfs4_fsid_present_res res
= {
6561 struct rpc_message msg
= {
6562 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6569 res
.fh
= nfs_alloc_fhandle();
6573 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6574 nfs4_set_sequence_privileged(&args
.seq_args
);
6575 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6576 &args
.seq_args
, &res
.seq_res
);
6577 nfs_free_fhandle(res
.fh
);
6578 if (status
== NFS4_OK
&&
6579 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6580 status
= -NFS4ERR_LEASE_MOVED
;
6584 #endif /* CONFIG_NFS_V4_1 */
6587 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6588 * @inode: inode on FSID to check
6589 * @cred: credential to use for this operation
6591 * Server indicates whether the FSID is present, moved, or not
6592 * recognized. This operation is necessary to clear a LEASE_MOVED
6593 * condition for this client ID.
6595 * Returns NFS4_OK if the FSID is present on this server,
6596 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6597 * NFS4ERR code if some error occurred on the server, or a
6598 * negative errno if a local failure occurred.
6600 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6602 struct nfs_server
*server
= NFS_SERVER(inode
);
6603 struct nfs_client
*clp
= server
->nfs_client
;
6604 const struct nfs4_mig_recovery_ops
*ops
=
6605 clp
->cl_mvops
->mig_recovery_ops
;
6606 struct nfs4_exception exception
= { };
6609 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6610 (unsigned long long)server
->fsid
.major
,
6611 (unsigned long long)server
->fsid
.minor
,
6613 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6616 status
= ops
->fsid_present(inode
, cred
);
6617 if (status
!= -NFS4ERR_DELAY
)
6619 nfs4_handle_exception(server
, status
, &exception
);
6620 } while (exception
.retry
);
6625 * If 'use_integrity' is true and the state managment nfs_client
6626 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6627 * and the machine credential as per RFC3530bis and RFC5661 Security
6628 * Considerations sections. Otherwise, just use the user cred with the
6629 * filesystem's rpc_client.
6631 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
6634 struct nfs4_secinfo_arg args
= {
6635 .dir_fh
= NFS_FH(dir
),
6638 struct nfs4_secinfo_res res
= {
6641 struct rpc_message msg
= {
6642 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
6646 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
6647 struct rpc_cred
*cred
= NULL
;
6649 if (use_integrity
) {
6650 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
6651 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
6652 msg
.rpc_cred
= cred
;
6655 dprintk("NFS call secinfo %s\n", name
->name
);
6657 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
6658 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
6660 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
6662 dprintk("NFS reply secinfo: %d\n", status
);
6670 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
6671 struct nfs4_secinfo_flavors
*flavors
)
6673 struct nfs4_exception exception
= { };
6676 err
= -NFS4ERR_WRONGSEC
;
6678 /* try to use integrity protection with machine cred */
6679 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
6680 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
6683 * if unable to use integrity protection, or SECINFO with
6684 * integrity protection returns NFS4ERR_WRONGSEC (which is
6685 * disallowed by spec, but exists in deployed servers) use
6686 * the current filesystem's rpc_client and the user cred.
6688 if (err
== -NFS4ERR_WRONGSEC
)
6689 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
6691 trace_nfs4_secinfo(dir
, name
, err
);
6692 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6694 } while (exception
.retry
);
6698 #ifdef CONFIG_NFS_V4_1
6700 * Check the exchange flags returned by the server for invalid flags, having
6701 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6704 static int nfs4_check_cl_exchange_flags(u32 flags
)
6706 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
6708 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
6709 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
6711 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
6715 return -NFS4ERR_INVAL
;
6719 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
6720 struct nfs41_server_scope
*b
)
6722 if (a
->server_scope_sz
== b
->server_scope_sz
&&
6723 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
6730 * nfs4_proc_bind_conn_to_session()
6732 * The 4.1 client currently uses the same TCP connection for the
6733 * fore and backchannel.
6735 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6738 struct nfs41_bind_conn_to_session_args args
= {
6740 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
6742 struct nfs41_bind_conn_to_session_res res
;
6743 struct rpc_message msg
= {
6745 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
6751 dprintk("--> %s\n", __func__
);
6753 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
6754 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
6755 args
.dir
= NFS4_CDFC4_FORE
;
6757 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6758 trace_nfs4_bind_conn_to_session(clp
, status
);
6760 if (memcmp(res
.sessionid
.data
,
6761 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
6762 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
6766 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
6767 dprintk("NFS: %s: Unexpected direction from server\n",
6772 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
6773 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6780 dprintk("<-- %s status= %d\n", __func__
, status
);
6785 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6786 * and operations we'd like to see to enable certain features in the allow map
6788 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
6789 .how
= SP4_MACH_CRED
,
6790 .enforce
.u
.words
= {
6791 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6792 1 << (OP_EXCHANGE_ID
- 32) |
6793 1 << (OP_CREATE_SESSION
- 32) |
6794 1 << (OP_DESTROY_SESSION
- 32) |
6795 1 << (OP_DESTROY_CLIENTID
- 32)
6798 [0] = 1 << (OP_CLOSE
) |
6801 [1] = 1 << (OP_SECINFO
- 32) |
6802 1 << (OP_SECINFO_NO_NAME
- 32) |
6803 1 << (OP_TEST_STATEID
- 32) |
6804 1 << (OP_FREE_STATEID
- 32) |
6805 1 << (OP_WRITE
- 32)
6810 * Select the state protection mode for client `clp' given the server results
6811 * from exchange_id in `sp'.
6813 * Returns 0 on success, negative errno otherwise.
6815 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
6816 struct nfs41_state_protection
*sp
)
6818 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
6819 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6820 1 << (OP_EXCHANGE_ID
- 32) |
6821 1 << (OP_CREATE_SESSION
- 32) |
6822 1 << (OP_DESTROY_SESSION
- 32) |
6823 1 << (OP_DESTROY_CLIENTID
- 32)
6827 if (sp
->how
== SP4_MACH_CRED
) {
6828 /* Print state protect result */
6829 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
6830 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
6831 if (test_bit(i
, sp
->enforce
.u
.longs
))
6832 dfprintk(MOUNT
, " enforce op %d\n", i
);
6833 if (test_bit(i
, sp
->allow
.u
.longs
))
6834 dfprintk(MOUNT
, " allow op %d\n", i
);
6837 /* make sure nothing is on enforce list that isn't supported */
6838 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
6839 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
6840 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6846 * Minimal mode - state operations are allowed to use machine
6847 * credential. Note this already happens by default, so the
6848 * client doesn't have to do anything more than the negotiation.
6850 * NOTE: we don't care if EXCHANGE_ID is in the list -
6851 * we're already using the machine cred for exchange_id
6852 * and will never use a different cred.
6854 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
6855 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
6856 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
6857 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
6858 dfprintk(MOUNT
, "sp4_mach_cred:\n");
6859 dfprintk(MOUNT
, " minimal mode enabled\n");
6860 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
6862 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6866 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
6867 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
6868 dfprintk(MOUNT
, " cleanup mode enabled\n");
6869 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
6872 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
6873 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
6874 dfprintk(MOUNT
, " secinfo mode enabled\n");
6875 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
6878 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
6879 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
6880 dfprintk(MOUNT
, " stateid mode enabled\n");
6881 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
6884 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
6885 dfprintk(MOUNT
, " write mode enabled\n");
6886 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
6889 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
6890 dfprintk(MOUNT
, " commit mode enabled\n");
6891 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
6899 * _nfs4_proc_exchange_id()
6901 * Wrapper for EXCHANGE_ID operation.
6903 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
6906 nfs4_verifier verifier
;
6907 struct nfs41_exchange_id_args args
= {
6908 .verifier
= &verifier
,
6910 #ifdef CONFIG_NFS_V4_1_MIGRATION
6911 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
6912 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
6913 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
6915 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
6916 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
6919 struct nfs41_exchange_id_res res
= {
6923 struct rpc_message msg
= {
6924 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
6930 nfs4_init_boot_verifier(clp
, &verifier
);
6932 status
= nfs4_init_uniform_client_string(clp
);
6936 dprintk("NFS call exchange_id auth=%s, '%s'\n",
6937 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
6940 res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
6942 if (unlikely(res
.server_owner
== NULL
)) {
6947 res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
6949 if (unlikely(res
.server_scope
== NULL
)) {
6951 goto out_server_owner
;
6954 res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
6955 if (unlikely(res
.impl_id
== NULL
)) {
6957 goto out_server_scope
;
6962 args
.state_protect
.how
= SP4_NONE
;
6966 args
.state_protect
= nfs4_sp4_mach_cred_request
;
6976 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6977 trace_nfs4_exchange_id(clp
, status
);
6979 status
= nfs4_check_cl_exchange_flags(res
.flags
);
6982 status
= nfs4_sp4_select_mode(clp
, &res
.state_protect
);
6985 clp
->cl_clientid
= res
.clientid
;
6986 clp
->cl_exchange_flags
= res
.flags
;
6987 /* Client ID is not confirmed */
6988 if (!(res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
)) {
6989 clear_bit(NFS4_SESSION_ESTABLISHED
,
6990 &clp
->cl_session
->session_state
);
6991 clp
->cl_seqid
= res
.seqid
;
6994 kfree(clp
->cl_serverowner
);
6995 clp
->cl_serverowner
= res
.server_owner
;
6996 res
.server_owner
= NULL
;
6998 /* use the most recent implementation id */
6999 kfree(clp
->cl_implid
);
7000 clp
->cl_implid
= res
.impl_id
;
7003 if (clp
->cl_serverscope
!= NULL
&&
7004 !nfs41_same_server_scope(clp
->cl_serverscope
,
7005 res
.server_scope
)) {
7006 dprintk("%s: server_scope mismatch detected\n",
7008 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7009 kfree(clp
->cl_serverscope
);
7010 clp
->cl_serverscope
= NULL
;
7013 if (clp
->cl_serverscope
== NULL
) {
7014 clp
->cl_serverscope
= res
.server_scope
;
7015 res
.server_scope
= NULL
;
7022 kfree(res
.server_scope
);
7024 kfree(res
.server_owner
);
7026 if (clp
->cl_implid
!= NULL
)
7027 dprintk("NFS reply exchange_id: Server Implementation ID: "
7028 "domain: %s, name: %s, date: %llu,%u\n",
7029 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
7030 clp
->cl_implid
->date
.seconds
,
7031 clp
->cl_implid
->date
.nseconds
);
7032 dprintk("NFS reply exchange_id: %d\n", status
);
7037 * nfs4_proc_exchange_id()
7039 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7041 * Since the clientid has expired, all compounds using sessions
7042 * associated with the stale clientid will be returning
7043 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7044 * be in some phase of session reset.
7046 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7048 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7050 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7053 /* try SP4_MACH_CRED if krb5i/p */
7054 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7055 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7056 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
);
7062 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
);
7065 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7066 struct rpc_cred
*cred
)
7068 struct rpc_message msg
= {
7069 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7075 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7076 trace_nfs4_destroy_clientid(clp
, status
);
7078 dprintk("NFS: Got error %d from the server %s on "
7079 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7083 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7084 struct rpc_cred
*cred
)
7089 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7090 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7092 case -NFS4ERR_DELAY
:
7093 case -NFS4ERR_CLIENTID_BUSY
:
7103 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7105 struct rpc_cred
*cred
;
7108 if (clp
->cl_mvops
->minor_version
< 1)
7110 if (clp
->cl_exchange_flags
== 0)
7112 if (clp
->cl_preserve_clid
)
7114 cred
= nfs4_get_clid_cred(clp
);
7115 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7120 case -NFS4ERR_STALE_CLIENTID
:
7121 clp
->cl_exchange_flags
= 0;
7127 struct nfs4_get_lease_time_data
{
7128 struct nfs4_get_lease_time_args
*args
;
7129 struct nfs4_get_lease_time_res
*res
;
7130 struct nfs_client
*clp
;
7133 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7136 struct nfs4_get_lease_time_data
*data
=
7137 (struct nfs4_get_lease_time_data
*)calldata
;
7139 dprintk("--> %s\n", __func__
);
7140 /* just setup sequence, do not trigger session recovery
7141 since we're invoked within one */
7142 nfs41_setup_sequence(data
->clp
->cl_session
,
7143 &data
->args
->la_seq_args
,
7144 &data
->res
->lr_seq_res
,
7146 dprintk("<-- %s\n", __func__
);
7150 * Called from nfs4_state_manager thread for session setup, so don't recover
7151 * from sequence operation or clientid errors.
7153 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7155 struct nfs4_get_lease_time_data
*data
=
7156 (struct nfs4_get_lease_time_data
*)calldata
;
7158 dprintk("--> %s\n", __func__
);
7159 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7161 switch (task
->tk_status
) {
7162 case -NFS4ERR_DELAY
:
7163 case -NFS4ERR_GRACE
:
7164 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7165 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7166 task
->tk_status
= 0;
7168 case -NFS4ERR_RETRY_UNCACHED_REP
:
7169 rpc_restart_call_prepare(task
);
7172 dprintk("<-- %s\n", __func__
);
7175 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7176 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7177 .rpc_call_done
= nfs4_get_lease_time_done
,
7180 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7182 struct rpc_task
*task
;
7183 struct nfs4_get_lease_time_args args
;
7184 struct nfs4_get_lease_time_res res
= {
7185 .lr_fsinfo
= fsinfo
,
7187 struct nfs4_get_lease_time_data data
= {
7192 struct rpc_message msg
= {
7193 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7197 struct rpc_task_setup task_setup
= {
7198 .rpc_client
= clp
->cl_rpcclient
,
7199 .rpc_message
= &msg
,
7200 .callback_ops
= &nfs4_get_lease_time_ops
,
7201 .callback_data
= &data
,
7202 .flags
= RPC_TASK_TIMEOUT
,
7206 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7207 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7208 dprintk("--> %s\n", __func__
);
7209 task
= rpc_run_task(&task_setup
);
7212 status
= PTR_ERR(task
);
7214 status
= task
->tk_status
;
7217 dprintk("<-- %s return %d\n", __func__
, status
);
7223 * Initialize the values to be used by the client in CREATE_SESSION
7224 * If nfs4_init_session set the fore channel request and response sizes,
7227 * Set the back channel max_resp_sz_cached to zero to force the client to
7228 * always set csa_cachethis to FALSE because the current implementation
7229 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7231 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
7233 unsigned int max_rqst_sz
, max_resp_sz
;
7235 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7236 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7238 /* Fore channel attributes */
7239 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7240 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7241 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7242 args
->fc_attrs
.max_reqs
= max_session_slots
;
7244 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7245 "max_ops=%u max_reqs=%u\n",
7247 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7248 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7250 /* Back channel attributes */
7251 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
7252 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
7253 args
->bc_attrs
.max_resp_sz_cached
= 0;
7254 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7255 args
->bc_attrs
.max_reqs
= 1;
7257 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7258 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7260 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7261 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7262 args
->bc_attrs
.max_reqs
);
7265 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7266 struct nfs41_create_session_res
*res
)
7268 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7269 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7271 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7274 * Our requested max_ops is the minimum we need; we're not
7275 * prepared to break up compounds into smaller pieces than that.
7276 * So, no point even trying to continue if the server won't
7279 if (rcvd
->max_ops
< sent
->max_ops
)
7281 if (rcvd
->max_reqs
== 0)
7283 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7284 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7288 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7289 struct nfs41_create_session_res
*res
)
7291 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7292 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7294 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7296 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7298 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7300 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7302 /* These would render the backchannel useless: */
7303 if (rcvd
->max_ops
!= sent
->max_ops
)
7305 if (rcvd
->max_reqs
!= sent
->max_reqs
)
7311 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7312 struct nfs41_create_session_res
*res
)
7316 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
7319 return nfs4_verify_back_channel_attrs(args
, res
);
7322 static void nfs4_update_session(struct nfs4_session
*session
,
7323 struct nfs41_create_session_res
*res
)
7325 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
7326 /* Mark client id and session as being confirmed */
7327 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
7328 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
7329 session
->flags
= res
->flags
;
7330 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
7331 if (res
->flags
& SESSION4_BACK_CHAN
)
7332 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
7333 sizeof(session
->bc_attrs
));
7336 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
7337 struct rpc_cred
*cred
)
7339 struct nfs4_session
*session
= clp
->cl_session
;
7340 struct nfs41_create_session_args args
= {
7342 .clientid
= clp
->cl_clientid
,
7343 .seqid
= clp
->cl_seqid
,
7344 .cb_program
= NFS4_CALLBACK
,
7346 struct nfs41_create_session_res res
;
7348 struct rpc_message msg
= {
7349 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
7356 nfs4_init_channel_attrs(&args
);
7357 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
7359 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7360 trace_nfs4_create_session(clp
, status
);
7363 /* Verify the session's negotiated channel_attrs values */
7364 status
= nfs4_verify_channel_attrs(&args
, &res
);
7365 /* Increment the clientid slot sequence id */
7366 if (clp
->cl_seqid
== res
.seqid
)
7370 nfs4_update_session(session
, &res
);
7377 * Issues a CREATE_SESSION operation to the server.
7378 * It is the responsibility of the caller to verify the session is
7379 * expired before calling this routine.
7381 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7385 struct nfs4_session
*session
= clp
->cl_session
;
7387 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
7389 status
= _nfs4_proc_create_session(clp
, cred
);
7393 /* Init or reset the session slot tables */
7394 status
= nfs4_setup_session_slot_tables(session
);
7395 dprintk("slot table setup returned %d\n", status
);
7399 ptr
= (unsigned *)&session
->sess_id
.data
[0];
7400 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
7401 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
7403 dprintk("<-- %s\n", __func__
);
7408 * Issue the over-the-wire RPC DESTROY_SESSION.
7409 * The caller must serialize access to this routine.
7411 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
7412 struct rpc_cred
*cred
)
7414 struct rpc_message msg
= {
7415 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
7416 .rpc_argp
= session
,
7421 dprintk("--> nfs4_proc_destroy_session\n");
7423 /* session is still being setup */
7424 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
7427 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7428 trace_nfs4_destroy_session(session
->clp
, status
);
7431 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7432 "Session has been destroyed regardless...\n", status
);
7434 dprintk("<-- nfs4_proc_destroy_session\n");
7439 * Renew the cl_session lease.
7441 struct nfs4_sequence_data
{
7442 struct nfs_client
*clp
;
7443 struct nfs4_sequence_args args
;
7444 struct nfs4_sequence_res res
;
7447 static void nfs41_sequence_release(void *data
)
7449 struct nfs4_sequence_data
*calldata
= data
;
7450 struct nfs_client
*clp
= calldata
->clp
;
7452 if (atomic_read(&clp
->cl_count
) > 1)
7453 nfs4_schedule_state_renewal(clp
);
7454 nfs_put_client(clp
);
7458 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
7460 switch(task
->tk_status
) {
7461 case -NFS4ERR_DELAY
:
7462 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
7465 nfs4_schedule_lease_recovery(clp
);
7470 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
7472 struct nfs4_sequence_data
*calldata
= data
;
7473 struct nfs_client
*clp
= calldata
->clp
;
7475 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
7478 trace_nfs4_sequence(clp
, task
->tk_status
);
7479 if (task
->tk_status
< 0) {
7480 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
7481 if (atomic_read(&clp
->cl_count
) == 1)
7484 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
7485 rpc_restart_call_prepare(task
);
7489 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
7491 dprintk("<-- %s\n", __func__
);
7494 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
7496 struct nfs4_sequence_data
*calldata
= data
;
7497 struct nfs_client
*clp
= calldata
->clp
;
7498 struct nfs4_sequence_args
*args
;
7499 struct nfs4_sequence_res
*res
;
7501 args
= task
->tk_msg
.rpc_argp
;
7502 res
= task
->tk_msg
.rpc_resp
;
7504 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
7507 static const struct rpc_call_ops nfs41_sequence_ops
= {
7508 .rpc_call_done
= nfs41_sequence_call_done
,
7509 .rpc_call_prepare
= nfs41_sequence_prepare
,
7510 .rpc_release
= nfs41_sequence_release
,
7513 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
7514 struct rpc_cred
*cred
,
7517 struct nfs4_sequence_data
*calldata
;
7518 struct rpc_message msg
= {
7519 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
7522 struct rpc_task_setup task_setup_data
= {
7523 .rpc_client
= clp
->cl_rpcclient
,
7524 .rpc_message
= &msg
,
7525 .callback_ops
= &nfs41_sequence_ops
,
7526 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7529 if (!atomic_inc_not_zero(&clp
->cl_count
))
7530 return ERR_PTR(-EIO
);
7531 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7532 if (calldata
== NULL
) {
7533 nfs_put_client(clp
);
7534 return ERR_PTR(-ENOMEM
);
7536 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
7538 nfs4_set_sequence_privileged(&calldata
->args
);
7539 msg
.rpc_argp
= &calldata
->args
;
7540 msg
.rpc_resp
= &calldata
->res
;
7541 calldata
->clp
= clp
;
7542 task_setup_data
.callback_data
= calldata
;
7544 return rpc_run_task(&task_setup_data
);
7547 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
7549 struct rpc_task
*task
;
7552 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
7554 task
= _nfs41_proc_sequence(clp
, cred
, false);
7556 ret
= PTR_ERR(task
);
7558 rpc_put_task_async(task
);
7559 dprintk("<-- %s status=%d\n", __func__
, ret
);
7563 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7565 struct rpc_task
*task
;
7568 task
= _nfs41_proc_sequence(clp
, cred
, true);
7570 ret
= PTR_ERR(task
);
7573 ret
= rpc_wait_for_completion_task(task
);
7575 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
7577 if (task
->tk_status
== 0)
7578 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
7579 ret
= task
->tk_status
;
7583 dprintk("<-- %s status=%d\n", __func__
, ret
);
7587 struct nfs4_reclaim_complete_data
{
7588 struct nfs_client
*clp
;
7589 struct nfs41_reclaim_complete_args arg
;
7590 struct nfs41_reclaim_complete_res res
;
7593 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
7595 struct nfs4_reclaim_complete_data
*calldata
= data
;
7597 nfs41_setup_sequence(calldata
->clp
->cl_session
,
7598 &calldata
->arg
.seq_args
,
7599 &calldata
->res
.seq_res
,
7603 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
7605 switch(task
->tk_status
) {
7607 case -NFS4ERR_COMPLETE_ALREADY
:
7608 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
7610 case -NFS4ERR_DELAY
:
7611 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
7613 case -NFS4ERR_RETRY_UNCACHED_REP
:
7616 nfs4_schedule_lease_recovery(clp
);
7621 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
7623 struct nfs4_reclaim_complete_data
*calldata
= data
;
7624 struct nfs_client
*clp
= calldata
->clp
;
7625 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
7627 dprintk("--> %s\n", __func__
);
7628 if (!nfs41_sequence_done(task
, res
))
7631 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
7632 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
7633 rpc_restart_call_prepare(task
);
7636 dprintk("<-- %s\n", __func__
);
7639 static void nfs4_free_reclaim_complete_data(void *data
)
7641 struct nfs4_reclaim_complete_data
*calldata
= data
;
7646 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
7647 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
7648 .rpc_call_done
= nfs4_reclaim_complete_done
,
7649 .rpc_release
= nfs4_free_reclaim_complete_data
,
7653 * Issue a global reclaim complete.
7655 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
7656 struct rpc_cred
*cred
)
7658 struct nfs4_reclaim_complete_data
*calldata
;
7659 struct rpc_task
*task
;
7660 struct rpc_message msg
= {
7661 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
7664 struct rpc_task_setup task_setup_data
= {
7665 .rpc_client
= clp
->cl_rpcclient
,
7666 .rpc_message
= &msg
,
7667 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
7668 .flags
= RPC_TASK_ASYNC
,
7670 int status
= -ENOMEM
;
7672 dprintk("--> %s\n", __func__
);
7673 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7674 if (calldata
== NULL
)
7676 calldata
->clp
= clp
;
7677 calldata
->arg
.one_fs
= 0;
7679 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
7680 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
7681 msg
.rpc_argp
= &calldata
->arg
;
7682 msg
.rpc_resp
= &calldata
->res
;
7683 task_setup_data
.callback_data
= calldata
;
7684 task
= rpc_run_task(&task_setup_data
);
7686 status
= PTR_ERR(task
);
7689 status
= nfs4_wait_for_completion_rpc_task(task
);
7691 status
= task
->tk_status
;
7695 dprintk("<-- %s status=%d\n", __func__
, status
);
7700 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
7702 struct nfs4_layoutget
*lgp
= calldata
;
7703 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
7704 struct nfs4_session
*session
= nfs4_get_session(server
);
7706 dprintk("--> %s\n", __func__
);
7707 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7708 * right now covering the LAYOUTGET we are about to send.
7709 * However, that is not so catastrophic, and there seems
7710 * to be no way to prevent it completely.
7712 if (nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
7713 &lgp
->res
.seq_res
, task
))
7715 if (pnfs_choose_layoutget_stateid(&lgp
->args
.stateid
,
7716 NFS_I(lgp
->args
.inode
)->layout
,
7718 lgp
->args
.ctx
->state
)) {
7719 rpc_exit(task
, NFS4_OK
);
7723 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
7725 struct nfs4_layoutget
*lgp
= calldata
;
7726 struct inode
*inode
= lgp
->args
.inode
;
7727 struct nfs_server
*server
= NFS_SERVER(inode
);
7728 struct pnfs_layout_hdr
*lo
;
7729 struct nfs4_state
*state
= NULL
;
7730 unsigned long timeo
, now
, giveup
;
7732 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
7734 if (!nfs41_sequence_done(task
, &lgp
->res
.seq_res
))
7737 switch (task
->tk_status
) {
7741 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7742 * (or clients) writing to the same RAID stripe
7744 case -NFS4ERR_LAYOUTTRYLATER
:
7746 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7747 * existing layout before getting a new one).
7749 case -NFS4ERR_RECALLCONFLICT
:
7750 timeo
= rpc_get_timeout(task
->tk_client
);
7751 giveup
= lgp
->args
.timestamp
+ timeo
;
7753 if (time_after(giveup
, now
)) {
7754 unsigned long delay
;
7757 * - Not less then NFS4_POLL_RETRY_MIN.
7758 * - One last time a jiffie before we give up
7759 * - exponential backoff (time_now minus start_attempt)
7761 delay
= max_t(unsigned long, NFS4_POLL_RETRY_MIN
,
7762 min((giveup
- now
- 1),
7763 now
- lgp
->args
.timestamp
));
7765 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7767 rpc_delay(task
, delay
);
7768 task
->tk_status
= 0;
7769 rpc_restart_call_prepare(task
);
7770 goto out
; /* Do not call nfs4_async_handle_error() */
7773 case -NFS4ERR_EXPIRED
:
7774 case -NFS4ERR_BAD_STATEID
:
7775 spin_lock(&inode
->i_lock
);
7776 lo
= NFS_I(inode
)->layout
;
7777 if (!lo
|| list_empty(&lo
->plh_segs
)) {
7778 spin_unlock(&inode
->i_lock
);
7779 /* If the open stateid was bad, then recover it. */
7780 state
= lgp
->args
.ctx
->state
;
7785 * Mark the bad layout state as invalid, then retry
7786 * with the current stateid.
7788 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
);
7789 spin_unlock(&inode
->i_lock
);
7790 pnfs_free_lseg_list(&head
);
7792 task
->tk_status
= 0;
7793 rpc_restart_call_prepare(task
);
7796 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
)
7797 rpc_restart_call_prepare(task
);
7799 dprintk("<-- %s\n", __func__
);
7802 static size_t max_response_pages(struct nfs_server
*server
)
7804 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
7805 return nfs_page_array_len(0, max_resp_sz
);
7808 static void nfs4_free_pages(struct page
**pages
, size_t size
)
7815 for (i
= 0; i
< size
; i
++) {
7818 __free_page(pages
[i
]);
7823 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
7825 struct page
**pages
;
7828 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
7830 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
7834 for (i
= 0; i
< size
; i
++) {
7835 pages
[i
] = alloc_page(gfp_flags
);
7837 dprintk("%s: failed to allocate page\n", __func__
);
7838 nfs4_free_pages(pages
, size
);
7846 static void nfs4_layoutget_release(void *calldata
)
7848 struct nfs4_layoutget
*lgp
= calldata
;
7849 struct inode
*inode
= lgp
->args
.inode
;
7850 struct nfs_server
*server
= NFS_SERVER(inode
);
7851 size_t max_pages
= max_response_pages(server
);
7853 dprintk("--> %s\n", __func__
);
7854 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
7855 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
7856 put_nfs_open_context(lgp
->args
.ctx
);
7858 dprintk("<-- %s\n", __func__
);
7861 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
7862 .rpc_call_prepare
= nfs4_layoutget_prepare
,
7863 .rpc_call_done
= nfs4_layoutget_done
,
7864 .rpc_release
= nfs4_layoutget_release
,
7867 struct pnfs_layout_segment
*
7868 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, gfp_t gfp_flags
)
7870 struct inode
*inode
= lgp
->args
.inode
;
7871 struct nfs_server
*server
= NFS_SERVER(inode
);
7872 size_t max_pages
= max_response_pages(server
);
7873 struct rpc_task
*task
;
7874 struct rpc_message msg
= {
7875 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
7876 .rpc_argp
= &lgp
->args
,
7877 .rpc_resp
= &lgp
->res
,
7878 .rpc_cred
= lgp
->cred
,
7880 struct rpc_task_setup task_setup_data
= {
7881 .rpc_client
= server
->client
,
7882 .rpc_message
= &msg
,
7883 .callback_ops
= &nfs4_layoutget_call_ops
,
7884 .callback_data
= lgp
,
7885 .flags
= RPC_TASK_ASYNC
,
7887 struct pnfs_layout_segment
*lseg
= NULL
;
7890 dprintk("--> %s\n", __func__
);
7892 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7893 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
7895 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
7896 if (!lgp
->args
.layout
.pages
) {
7897 nfs4_layoutget_release(lgp
);
7898 return ERR_PTR(-ENOMEM
);
7900 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
7901 lgp
->args
.timestamp
= jiffies
;
7903 lgp
->res
.layoutp
= &lgp
->args
.layout
;
7904 lgp
->res
.seq_res
.sr_slot
= NULL
;
7905 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
7907 task
= rpc_run_task(&task_setup_data
);
7909 return ERR_CAST(task
);
7910 status
= nfs4_wait_for_completion_rpc_task(task
);
7912 status
= task
->tk_status
;
7913 trace_nfs4_layoutget(lgp
->args
.ctx
,
7917 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7918 if (status
== 0 && lgp
->res
.layoutp
->len
)
7919 lseg
= pnfs_layout_process(lgp
);
7921 dprintk("<-- %s status=%d\n", __func__
, status
);
7923 return ERR_PTR(status
);
7928 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
7930 struct nfs4_layoutreturn
*lrp
= calldata
;
7932 dprintk("--> %s\n", __func__
);
7933 nfs41_setup_sequence(lrp
->clp
->cl_session
,
7934 &lrp
->args
.seq_args
,
7939 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
7941 struct nfs4_layoutreturn
*lrp
= calldata
;
7942 struct nfs_server
*server
;
7944 dprintk("--> %s\n", __func__
);
7946 if (!nfs41_sequence_done(task
, &lrp
->res
.seq_res
))
7949 server
= NFS_SERVER(lrp
->args
.inode
);
7950 switch (task
->tk_status
) {
7952 task
->tk_status
= 0;
7955 case -NFS4ERR_DELAY
:
7956 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
7958 rpc_restart_call_prepare(task
);
7961 dprintk("<-- %s\n", __func__
);
7964 static void nfs4_layoutreturn_release(void *calldata
)
7966 struct nfs4_layoutreturn
*lrp
= calldata
;
7967 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
7969 dprintk("--> %s\n", __func__
);
7970 spin_lock(&lo
->plh_inode
->i_lock
);
7971 if (lrp
->res
.lrs_present
)
7972 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
7973 pnfs_clear_layoutreturn_waitbit(lo
);
7974 clear_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE
, &lo
->plh_flags
);
7975 rpc_wake_up(&NFS_SERVER(lo
->plh_inode
)->roc_rpcwaitq
);
7976 lo
->plh_block_lgets
--;
7977 spin_unlock(&lo
->plh_inode
->i_lock
);
7978 pnfs_put_layout_hdr(lrp
->args
.layout
);
7979 nfs_iput_and_deactive(lrp
->inode
);
7981 dprintk("<-- %s\n", __func__
);
7984 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
7985 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
7986 .rpc_call_done
= nfs4_layoutreturn_done
,
7987 .rpc_release
= nfs4_layoutreturn_release
,
7990 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
7992 struct rpc_task
*task
;
7993 struct rpc_message msg
= {
7994 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
7995 .rpc_argp
= &lrp
->args
,
7996 .rpc_resp
= &lrp
->res
,
7997 .rpc_cred
= lrp
->cred
,
7999 struct rpc_task_setup task_setup_data
= {
8000 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8001 .rpc_message
= &msg
,
8002 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8003 .callback_data
= lrp
,
8007 dprintk("--> %s\n", __func__
);
8009 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8011 nfs4_layoutreturn_release(lrp
);
8014 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8016 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8017 task
= rpc_run_task(&task_setup_data
);
8019 return PTR_ERR(task
);
8021 status
= task
->tk_status
;
8022 trace_nfs4_layoutreturn(lrp
->args
.inode
, status
);
8023 dprintk("<-- %s status=%d\n", __func__
, status
);
8029 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8030 struct pnfs_device
*pdev
,
8031 struct rpc_cred
*cred
)
8033 struct nfs4_getdeviceinfo_args args
= {
8035 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8036 NOTIFY_DEVICEID4_DELETE
,
8038 struct nfs4_getdeviceinfo_res res
= {
8041 struct rpc_message msg
= {
8042 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8049 dprintk("--> %s\n", __func__
);
8050 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8051 if (res
.notification
& ~args
.notify_types
)
8052 dprintk("%s: unsupported notification\n", __func__
);
8053 if (res
.notification
!= args
.notify_types
)
8056 dprintk("<-- %s status=%d\n", __func__
, status
);
8061 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8062 struct pnfs_device
*pdev
,
8063 struct rpc_cred
*cred
)
8065 struct nfs4_exception exception
= { };
8069 err
= nfs4_handle_exception(server
,
8070 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8072 } while (exception
.retry
);
8075 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8077 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8079 struct nfs4_layoutcommit_data
*data
= calldata
;
8080 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8081 struct nfs4_session
*session
= nfs4_get_session(server
);
8083 nfs41_setup_sequence(session
,
8084 &data
->args
.seq_args
,
8090 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8092 struct nfs4_layoutcommit_data
*data
= calldata
;
8093 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8095 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8098 switch (task
->tk_status
) { /* Just ignore these failures */
8099 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8100 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8101 case -NFS4ERR_BADLAYOUT
: /* no layout */
8102 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8103 task
->tk_status
= 0;
8107 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8108 rpc_restart_call_prepare(task
);
8114 static void nfs4_layoutcommit_release(void *calldata
)
8116 struct nfs4_layoutcommit_data
*data
= calldata
;
8118 pnfs_cleanup_layoutcommit(data
);
8119 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8121 put_rpccred(data
->cred
);
8122 nfs_iput_and_deactive(data
->inode
);
8126 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8127 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8128 .rpc_call_done
= nfs4_layoutcommit_done
,
8129 .rpc_release
= nfs4_layoutcommit_release
,
8133 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8135 struct rpc_message msg
= {
8136 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8137 .rpc_argp
= &data
->args
,
8138 .rpc_resp
= &data
->res
,
8139 .rpc_cred
= data
->cred
,
8141 struct rpc_task_setup task_setup_data
= {
8142 .task
= &data
->task
,
8143 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8144 .rpc_message
= &msg
,
8145 .callback_ops
= &nfs4_layoutcommit_ops
,
8146 .callback_data
= data
,
8148 struct rpc_task
*task
;
8151 dprintk("NFS: initiating layoutcommit call. sync %d "
8152 "lbw: %llu inode %lu\n", sync
,
8153 data
->args
.lastbytewritten
,
8154 data
->args
.inode
->i_ino
);
8157 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8158 if (data
->inode
== NULL
) {
8159 nfs4_layoutcommit_release(data
);
8162 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8164 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8165 task
= rpc_run_task(&task_setup_data
);
8167 return PTR_ERR(task
);
8169 status
= task
->tk_status
;
8170 trace_nfs4_layoutcommit(data
->args
.inode
, status
);
8171 dprintk("%s: status %d\n", __func__
, status
);
8177 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8178 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8181 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8182 struct nfs_fsinfo
*info
,
8183 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8185 struct nfs41_secinfo_no_name_args args
= {
8186 .style
= SECINFO_STYLE_CURRENT_FH
,
8188 struct nfs4_secinfo_res res
= {
8191 struct rpc_message msg
= {
8192 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8196 struct rpc_clnt
*clnt
= server
->client
;
8197 struct rpc_cred
*cred
= NULL
;
8200 if (use_integrity
) {
8201 clnt
= server
->nfs_client
->cl_rpcclient
;
8202 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8203 msg
.rpc_cred
= cred
;
8206 dprintk("--> %s\n", __func__
);
8207 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8209 dprintk("<-- %s status=%d\n", __func__
, status
);
8218 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8219 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8221 struct nfs4_exception exception
= { };
8224 /* first try using integrity protection */
8225 err
= -NFS4ERR_WRONGSEC
;
8227 /* try to use integrity protection with machine cred */
8228 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8229 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8233 * if unable to use integrity protection, or SECINFO with
8234 * integrity protection returns NFS4ERR_WRONGSEC (which is
8235 * disallowed by spec, but exists in deployed servers) use
8236 * the current filesystem's rpc_client and the user cred.
8238 if (err
== -NFS4ERR_WRONGSEC
)
8239 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8244 case -NFS4ERR_WRONGSEC
:
8248 err
= nfs4_handle_exception(server
, err
, &exception
);
8250 } while (exception
.retry
);
8256 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8257 struct nfs_fsinfo
*info
)
8261 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8262 struct nfs4_secinfo_flavors
*flavors
;
8263 struct nfs4_secinfo4
*secinfo
;
8266 page
= alloc_page(GFP_KERNEL
);
8272 flavors
= page_address(page
);
8273 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8276 * Fall back on "guess and check" method if
8277 * the server doesn't support SECINFO_NO_NAME
8279 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8280 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8286 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8287 secinfo
= &flavors
->flavors
[i
];
8289 switch (secinfo
->flavor
) {
8293 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
8294 &secinfo
->flavor_info
);
8297 flavor
= RPC_AUTH_MAXFLAVOR
;
8301 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
8302 flavor
= RPC_AUTH_MAXFLAVOR
;
8304 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
8305 err
= nfs4_lookup_root_sec(server
, fhandle
,
8312 if (flavor
== RPC_AUTH_MAXFLAVOR
)
8323 static int _nfs41_test_stateid(struct nfs_server
*server
,
8324 nfs4_stateid
*stateid
,
8325 struct rpc_cred
*cred
)
8328 struct nfs41_test_stateid_args args
= {
8331 struct nfs41_test_stateid_res res
;
8332 struct rpc_message msg
= {
8333 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
8338 struct rpc_clnt
*rpc_client
= server
->client
;
8340 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
8343 dprintk("NFS call test_stateid %p\n", stateid
);
8344 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
8345 nfs4_set_sequence_privileged(&args
.seq_args
);
8346 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
8347 &args
.seq_args
, &res
.seq_res
);
8348 if (status
!= NFS_OK
) {
8349 dprintk("NFS reply test_stateid: failed, %d\n", status
);
8352 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
8357 * nfs41_test_stateid - perform a TEST_STATEID operation
8359 * @server: server / transport on which to perform the operation
8360 * @stateid: state ID to test
8363 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8364 * Otherwise a negative NFS4ERR value is returned if the operation
8365 * failed or the state ID is not currently valid.
8367 static int nfs41_test_stateid(struct nfs_server
*server
,
8368 nfs4_stateid
*stateid
,
8369 struct rpc_cred
*cred
)
8371 struct nfs4_exception exception
= { };
8374 err
= _nfs41_test_stateid(server
, stateid
, cred
);
8375 if (err
!= -NFS4ERR_DELAY
)
8377 nfs4_handle_exception(server
, err
, &exception
);
8378 } while (exception
.retry
);
8382 struct nfs_free_stateid_data
{
8383 struct nfs_server
*server
;
8384 struct nfs41_free_stateid_args args
;
8385 struct nfs41_free_stateid_res res
;
8388 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
8390 struct nfs_free_stateid_data
*data
= calldata
;
8391 nfs41_setup_sequence(nfs4_get_session(data
->server
),
8392 &data
->args
.seq_args
,
8397 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
8399 struct nfs_free_stateid_data
*data
= calldata
;
8401 nfs41_sequence_done(task
, &data
->res
.seq_res
);
8403 switch (task
->tk_status
) {
8404 case -NFS4ERR_DELAY
:
8405 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
8406 rpc_restart_call_prepare(task
);
8410 static void nfs41_free_stateid_release(void *calldata
)
8415 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
8416 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
8417 .rpc_call_done
= nfs41_free_stateid_done
,
8418 .rpc_release
= nfs41_free_stateid_release
,
8421 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
8422 nfs4_stateid
*stateid
,
8423 struct rpc_cred
*cred
,
8426 struct rpc_message msg
= {
8427 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
8430 struct rpc_task_setup task_setup
= {
8431 .rpc_client
= server
->client
,
8432 .rpc_message
= &msg
,
8433 .callback_ops
= &nfs41_free_stateid_ops
,
8434 .flags
= RPC_TASK_ASYNC
,
8436 struct nfs_free_stateid_data
*data
;
8438 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
8439 &task_setup
.rpc_client
, &msg
);
8441 dprintk("NFS call free_stateid %p\n", stateid
);
8442 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
8444 return ERR_PTR(-ENOMEM
);
8445 data
->server
= server
;
8446 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
8448 task_setup
.callback_data
= data
;
8450 msg
.rpc_argp
= &data
->args
;
8451 msg
.rpc_resp
= &data
->res
;
8452 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
8454 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
8456 return rpc_run_task(&task_setup
);
8460 * nfs41_free_stateid - perform a FREE_STATEID operation
8462 * @server: server / transport on which to perform the operation
8463 * @stateid: state ID to release
8466 * Returns NFS_OK if the server freed "stateid". Otherwise a
8467 * negative NFS4ERR value is returned.
8469 static int nfs41_free_stateid(struct nfs_server
*server
,
8470 nfs4_stateid
*stateid
,
8471 struct rpc_cred
*cred
)
8473 struct rpc_task
*task
;
8476 task
= _nfs41_free_stateid(server
, stateid
, cred
, true);
8478 return PTR_ERR(task
);
8479 ret
= rpc_wait_for_completion_task(task
);
8481 ret
= task
->tk_status
;
8487 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
8489 struct rpc_task
*task
;
8490 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
8492 task
= _nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
8493 nfs4_free_lock_state(server
, lsp
);
8499 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
8500 const nfs4_stateid
*s2
)
8502 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
8505 if (s1
->seqid
== s2
->seqid
)
8507 if (s1
->seqid
== 0 || s2
->seqid
== 0)
8513 #endif /* CONFIG_NFS_V4_1 */
8515 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
8516 const nfs4_stateid
*s2
)
8518 return nfs4_stateid_match(s1
, s2
);
8522 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
8523 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
8524 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
8525 .recover_open
= nfs4_open_reclaim
,
8526 .recover_lock
= nfs4_lock_reclaim
,
8527 .establish_clid
= nfs4_init_clientid
,
8528 .detect_trunking
= nfs40_discover_server_trunking
,
8531 #if defined(CONFIG_NFS_V4_1)
8532 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
8533 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
8534 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
8535 .recover_open
= nfs4_open_reclaim
,
8536 .recover_lock
= nfs4_lock_reclaim
,
8537 .establish_clid
= nfs41_init_clientid
,
8538 .reclaim_complete
= nfs41_proc_reclaim_complete
,
8539 .detect_trunking
= nfs41_discover_server_trunking
,
8541 #endif /* CONFIG_NFS_V4_1 */
8543 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
8544 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
8545 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
8546 .recover_open
= nfs40_open_expired
,
8547 .recover_lock
= nfs4_lock_expired
,
8548 .establish_clid
= nfs4_init_clientid
,
8551 #if defined(CONFIG_NFS_V4_1)
8552 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
8553 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
8554 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
8555 .recover_open
= nfs41_open_expired
,
8556 .recover_lock
= nfs41_lock_expired
,
8557 .establish_clid
= nfs41_init_clientid
,
8559 #endif /* CONFIG_NFS_V4_1 */
8561 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
8562 .sched_state_renewal
= nfs4_proc_async_renew
,
8563 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
8564 .renew_lease
= nfs4_proc_renew
,
8567 #if defined(CONFIG_NFS_V4_1)
8568 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
8569 .sched_state_renewal
= nfs41_proc_async_sequence
,
8570 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
8571 .renew_lease
= nfs4_proc_sequence
,
8575 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
8576 .get_locations
= _nfs40_proc_get_locations
,
8577 .fsid_present
= _nfs40_proc_fsid_present
,
8580 #if defined(CONFIG_NFS_V4_1)
8581 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
8582 .get_locations
= _nfs41_proc_get_locations
,
8583 .fsid_present
= _nfs41_proc_fsid_present
,
8585 #endif /* CONFIG_NFS_V4_1 */
8587 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
8589 .init_caps
= NFS_CAP_READDIRPLUS
8590 | NFS_CAP_ATOMIC_OPEN
8591 | NFS_CAP_CHANGE_ATTR
8592 | NFS_CAP_POSIX_LOCK
,
8593 .init_client
= nfs40_init_client
,
8594 .shutdown_client
= nfs40_shutdown_client
,
8595 .match_stateid
= nfs4_match_stateid
,
8596 .find_root_sec
= nfs4_find_root_sec
,
8597 .free_lock_state
= nfs4_release_lockowner
,
8598 .alloc_seqid
= nfs_alloc_seqid
,
8599 .call_sync_ops
= &nfs40_call_sync_ops
,
8600 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
8601 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
8602 .state_renewal_ops
= &nfs40_state_renewal_ops
,
8603 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
8606 #if defined(CONFIG_NFS_V4_1)
8607 static struct nfs_seqid
*
8608 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
8613 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
8615 .init_caps
= NFS_CAP_READDIRPLUS
8616 | NFS_CAP_ATOMIC_OPEN
8617 | NFS_CAP_CHANGE_ATTR
8618 | NFS_CAP_POSIX_LOCK
8619 | NFS_CAP_STATEID_NFSV41
8620 | NFS_CAP_ATOMIC_OPEN_V1
,
8621 .init_client
= nfs41_init_client
,
8622 .shutdown_client
= nfs41_shutdown_client
,
8623 .match_stateid
= nfs41_match_stateid
,
8624 .find_root_sec
= nfs41_find_root_sec
,
8625 .free_lock_state
= nfs41_free_lock_state
,
8626 .alloc_seqid
= nfs_alloc_no_seqid
,
8627 .call_sync_ops
= &nfs41_call_sync_ops
,
8628 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
8629 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
8630 .state_renewal_ops
= &nfs41_state_renewal_ops
,
8631 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
8635 #if defined(CONFIG_NFS_V4_2)
8636 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
8638 .init_caps
= NFS_CAP_READDIRPLUS
8639 | NFS_CAP_ATOMIC_OPEN
8640 | NFS_CAP_CHANGE_ATTR
8641 | NFS_CAP_POSIX_LOCK
8642 | NFS_CAP_STATEID_NFSV41
8643 | NFS_CAP_ATOMIC_OPEN_V1
8645 | NFS_CAP_DEALLOCATE
8647 | NFS_CAP_LAYOUTSTATS
,
8648 .init_client
= nfs41_init_client
,
8649 .shutdown_client
= nfs41_shutdown_client
,
8650 .match_stateid
= nfs41_match_stateid
,
8651 .find_root_sec
= nfs41_find_root_sec
,
8652 .free_lock_state
= nfs41_free_lock_state
,
8653 .call_sync_ops
= &nfs41_call_sync_ops
,
8654 .alloc_seqid
= nfs_alloc_no_seqid
,
8655 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
8656 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
8657 .state_renewal_ops
= &nfs41_state_renewal_ops
,
8661 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
8662 [0] = &nfs_v4_0_minor_ops
,
8663 #if defined(CONFIG_NFS_V4_1)
8664 [1] = &nfs_v4_1_minor_ops
,
8666 #if defined(CONFIG_NFS_V4_2)
8667 [2] = &nfs_v4_2_minor_ops
,
8671 static const struct inode_operations nfs4_dir_inode_operations
= {
8672 .create
= nfs_create
,
8673 .lookup
= nfs_lookup
,
8674 .atomic_open
= nfs_atomic_open
,
8676 .unlink
= nfs_unlink
,
8677 .symlink
= nfs_symlink
,
8681 .rename
= nfs_rename
,
8682 .permission
= nfs_permission
,
8683 .getattr
= nfs_getattr
,
8684 .setattr
= nfs_setattr
,
8685 .getxattr
= generic_getxattr
,
8686 .setxattr
= generic_setxattr
,
8687 .listxattr
= generic_listxattr
,
8688 .removexattr
= generic_removexattr
,
8691 static const struct inode_operations nfs4_file_inode_operations
= {
8692 .permission
= nfs_permission
,
8693 .getattr
= nfs_getattr
,
8694 .setattr
= nfs_setattr
,
8695 .getxattr
= generic_getxattr
,
8696 .setxattr
= generic_setxattr
,
8697 .listxattr
= generic_listxattr
,
8698 .removexattr
= generic_removexattr
,
8701 const struct nfs_rpc_ops nfs_v4_clientops
= {
8702 .version
= 4, /* protocol version */
8703 .dentry_ops
= &nfs4_dentry_operations
,
8704 .dir_inode_ops
= &nfs4_dir_inode_operations
,
8705 .file_inode_ops
= &nfs4_file_inode_operations
,
8706 .file_ops
= &nfs4_file_operations
,
8707 .getroot
= nfs4_proc_get_root
,
8708 .submount
= nfs4_submount
,
8709 .try_mount
= nfs4_try_mount
,
8710 .getattr
= nfs4_proc_getattr
,
8711 .setattr
= nfs4_proc_setattr
,
8712 .lookup
= nfs4_proc_lookup
,
8713 .access
= nfs4_proc_access
,
8714 .readlink
= nfs4_proc_readlink
,
8715 .create
= nfs4_proc_create
,
8716 .remove
= nfs4_proc_remove
,
8717 .unlink_setup
= nfs4_proc_unlink_setup
,
8718 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
8719 .unlink_done
= nfs4_proc_unlink_done
,
8720 .rename_setup
= nfs4_proc_rename_setup
,
8721 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
8722 .rename_done
= nfs4_proc_rename_done
,
8723 .link
= nfs4_proc_link
,
8724 .symlink
= nfs4_proc_symlink
,
8725 .mkdir
= nfs4_proc_mkdir
,
8726 .rmdir
= nfs4_proc_remove
,
8727 .readdir
= nfs4_proc_readdir
,
8728 .mknod
= nfs4_proc_mknod
,
8729 .statfs
= nfs4_proc_statfs
,
8730 .fsinfo
= nfs4_proc_fsinfo
,
8731 .pathconf
= nfs4_proc_pathconf
,
8732 .set_capabilities
= nfs4_server_capabilities
,
8733 .decode_dirent
= nfs4_decode_dirent
,
8734 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
8735 .read_setup
= nfs4_proc_read_setup
,
8736 .read_done
= nfs4_read_done
,
8737 .write_setup
= nfs4_proc_write_setup
,
8738 .write_done
= nfs4_write_done
,
8739 .commit_setup
= nfs4_proc_commit_setup
,
8740 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
8741 .commit_done
= nfs4_commit_done
,
8742 .lock
= nfs4_proc_lock
,
8743 .clear_acl_cache
= nfs4_zap_acl_attr
,
8744 .close_context
= nfs4_close_context
,
8745 .open_context
= nfs4_atomic_open
,
8746 .have_delegation
= nfs4_have_delegation
,
8747 .return_delegation
= nfs4_inode_return_delegation
,
8748 .alloc_client
= nfs4_alloc_client
,
8749 .init_client
= nfs4_init_client
,
8750 .free_client
= nfs4_free_client
,
8751 .create_server
= nfs4_create_server
,
8752 .clone_server
= nfs_clone_server
,
8755 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
8756 .prefix
= XATTR_NAME_NFSV4_ACL
,
8757 .list
= nfs4_xattr_list_nfs4_acl
,
8758 .get
= nfs4_xattr_get_nfs4_acl
,
8759 .set
= nfs4_xattr_set_nfs4_acl
,
8762 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
8763 &nfs4_xattr_nfs4_acl_handler
,
8764 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8765 &nfs4_xattr_nfs4_label_handler
,