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)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
89 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
91 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
93 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
94 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
95 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
96 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
97 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
98 struct nfs4_label
*olabel
);
99 #ifdef CONFIG_NFS_V4_1
100 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
102 static int nfs41_free_stateid(struct nfs_server
*, nfs4_stateid
*,
106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
107 static inline struct nfs4_label
*
108 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
109 struct iattr
*sattr
, struct nfs4_label
*label
)
116 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
119 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
120 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
127 nfs4_label_release_security(struct nfs4_label
*label
)
130 security_release_secctx(label
->label
, label
->len
);
132 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
135 return server
->attr_bitmask
;
137 return server
->attr_bitmask_nl
;
140 static inline struct nfs4_label
*
141 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
142 struct iattr
*sattr
, struct nfs4_label
*l
)
145 nfs4_label_release_security(struct nfs4_label
*label
)
148 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
149 { return server
->attr_bitmask
; }
152 /* Prevent leaks of NFSv4 errors into userland */
153 static int nfs4_map_errors(int err
)
158 case -NFS4ERR_RESOURCE
:
159 case -NFS4ERR_LAYOUTTRYLATER
:
160 case -NFS4ERR_RECALLCONFLICT
:
162 case -NFS4ERR_WRONGSEC
:
163 case -NFS4ERR_WRONG_CRED
:
165 case -NFS4ERR_BADOWNER
:
166 case -NFS4ERR_BADNAME
:
168 case -NFS4ERR_SHARE_DENIED
:
170 case -NFS4ERR_MINOR_VERS_MISMATCH
:
171 return -EPROTONOSUPPORT
;
172 case -NFS4ERR_FILE_OPEN
:
175 dprintk("%s could not handle NFSv4 error %d\n",
183 * This is our standard bitmap for GETATTR requests.
185 const u32 nfs4_fattr_bitmap
[3] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID
,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
206 static const u32 nfs4_pnfs_open_bitmap
[3] = {
208 | FATTR4_WORD0_CHANGE
211 | FATTR4_WORD0_FILEID
,
213 | FATTR4_WORD1_NUMLINKS
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY
,
221 FATTR4_WORD2_MDSTHRESHOLD
222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
227 static const u32 nfs4_open_noattr_bitmap
[3] = {
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID
,
233 const u32 nfs4_statfs_bitmap
[3] = {
234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL
,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
242 const u32 nfs4_pathconf_bitmap
[3] = {
244 | FATTR4_WORD0_MAXNAME
,
248 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME
,
252 FATTR4_WORD1_TIME_DELTA
253 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
255 | FATTR4_WORD2_CLONE_BLKSIZE
258 const u32 nfs4_fs_locations_bitmap
[3] = {
260 | FATTR4_WORD0_CHANGE
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS
,
266 | FATTR4_WORD1_NUMLINKS
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
274 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
277 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
278 struct nfs4_readdir_arg
*readdir
)
283 readdir
->cookie
= cookie
;
284 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
289 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
300 start
= p
= kmap_atomic(*readdir
->pages
);
303 *p
++ = xdr_one
; /* next */
304 *p
++ = xdr_zero
; /* cookie, first word */
305 *p
++ = xdr_one
; /* cookie, second word */
306 *p
++ = xdr_one
; /* entry len */
307 memcpy(p
, ".\0\0\0", 4); /* entry */
309 *p
++ = xdr_one
; /* bitmap length */
310 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
311 *p
++ = htonl(8); /* attribute buffer length */
312 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
)));
315 *p
++ = xdr_one
; /* next */
316 *p
++ = xdr_zero
; /* cookie, first word */
317 *p
++ = xdr_two
; /* cookie, second word */
318 *p
++ = xdr_two
; /* entry len */
319 memcpy(p
, "..\0\0", 4); /* entry */
321 *p
++ = xdr_one
; /* bitmap length */
322 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
323 *p
++ = htonl(8); /* attribute buffer length */
324 p
= xdr_encode_hyper(p
, NFS_FILEID(d_inode(dentry
->d_parent
)));
326 readdir
->pgbase
= (char *)p
- (char *)start
;
327 readdir
->count
-= readdir
->pgbase
;
328 kunmap_atomic(start
);
331 static long nfs4_update_delay(long *timeout
)
335 return NFS4_POLL_RETRY_MAX
;
337 *timeout
= NFS4_POLL_RETRY_MIN
;
338 if (*timeout
> NFS4_POLL_RETRY_MAX
)
339 *timeout
= NFS4_POLL_RETRY_MAX
;
345 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout
));
353 if (fatal_signal_pending(current
))
358 /* This is the error handling routine for processes that are allowed
361 static int nfs4_do_handle_exception(struct nfs_server
*server
,
362 int errorcode
, struct nfs4_exception
*exception
)
364 struct nfs_client
*clp
= server
->nfs_client
;
365 struct nfs4_state
*state
= exception
->state
;
366 struct inode
*inode
= exception
->inode
;
369 exception
->delay
= 0;
370 exception
->recovering
= 0;
371 exception
->retry
= 0;
375 case -NFS4ERR_OPENMODE
:
376 case -NFS4ERR_DELEG_REVOKED
:
377 case -NFS4ERR_ADMIN_REVOKED
:
378 case -NFS4ERR_BAD_STATEID
:
379 if (inode
&& nfs_async_inode_return_delegation(inode
,
381 goto wait_on_recovery
;
384 ret
= nfs4_schedule_stateid_recovery(server
, state
);
387 goto wait_on_recovery
;
388 case -NFS4ERR_EXPIRED
:
390 ret
= nfs4_schedule_stateid_recovery(server
, state
);
394 case -NFS4ERR_STALE_STATEID
:
395 case -NFS4ERR_STALE_CLIENTID
:
396 nfs4_schedule_lease_recovery(clp
);
397 goto wait_on_recovery
;
399 ret
= nfs4_schedule_migration_recovery(server
);
402 goto wait_on_recovery
;
403 case -NFS4ERR_LEASE_MOVED
:
404 nfs4_schedule_lease_moved_recovery(clp
);
405 goto wait_on_recovery
;
406 #if defined(CONFIG_NFS_V4_1)
407 case -NFS4ERR_BADSESSION
:
408 case -NFS4ERR_BADSLOT
:
409 case -NFS4ERR_BAD_HIGH_SLOT
:
410 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
411 case -NFS4ERR_DEADSESSION
:
412 case -NFS4ERR_SEQ_FALSE_RETRY
:
413 case -NFS4ERR_SEQ_MISORDERED
:
414 dprintk("%s ERROR: %d Reset session\n", __func__
,
416 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
417 goto wait_on_recovery
;
418 #endif /* defined(CONFIG_NFS_V4_1) */
419 case -NFS4ERR_FILE_OPEN
:
420 if (exception
->timeout
> HZ
) {
421 /* We have retried a decent amount, time to
428 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
430 case -NFS4ERR_RECALLCONFLICT
:
431 exception
->delay
= 1;
434 case -NFS4ERR_RETRY_UNCACHED_REP
:
435 case -NFS4ERR_OLD_STATEID
:
436 exception
->retry
= 1;
438 case -NFS4ERR_BADOWNER
:
439 /* The following works around a Linux server bug! */
440 case -NFS4ERR_BADNAME
:
441 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
442 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
443 exception
->retry
= 1;
444 printk(KERN_WARNING
"NFS: v4 server %s "
445 "does not accept raw "
447 "Reenabling the idmapper.\n",
448 server
->nfs_client
->cl_hostname
);
451 /* We failed to handle the error */
452 return nfs4_map_errors(ret
);
454 exception
->recovering
= 1;
458 /* This is the error handling routine for processes that are allowed
461 int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
463 struct nfs_client
*clp
= server
->nfs_client
;
466 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
467 if (exception
->delay
) {
468 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
471 if (exception
->recovering
) {
472 ret
= nfs4_wait_clnt_recover(clp
);
473 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
480 exception
->retry
= 1;
485 nfs4_async_handle_exception(struct rpc_task
*task
, struct nfs_server
*server
,
486 int errorcode
, struct nfs4_exception
*exception
)
488 struct nfs_client
*clp
= server
->nfs_client
;
491 ret
= nfs4_do_handle_exception(server
, errorcode
, exception
);
492 if (exception
->delay
) {
493 rpc_delay(task
, nfs4_update_delay(&exception
->timeout
));
496 if (exception
->recovering
) {
497 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
498 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
499 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
502 if (test_bit(NFS_MIG_FAILED
, &server
->mig_status
))
507 exception
->retry
= 1;
512 nfs4_async_handle_error(struct rpc_task
*task
, struct nfs_server
*server
,
513 struct nfs4_state
*state
, long *timeout
)
515 struct nfs4_exception exception
= {
519 if (task
->tk_status
>= 0)
522 exception
.timeout
= *timeout
;
523 task
->tk_status
= nfs4_async_handle_exception(task
, server
,
526 if (exception
.delay
&& timeout
)
527 *timeout
= exception
.timeout
;
534 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
535 * or 'false' otherwise.
537 static bool _nfs4_is_integrity_protected(struct nfs_client
*clp
)
539 rpc_authflavor_t flavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
541 if (flavor
== RPC_AUTH_GSS_KRB5I
||
542 flavor
== RPC_AUTH_GSS_KRB5P
)
548 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
550 spin_lock(&clp
->cl_lock
);
551 if (time_before(clp
->cl_last_renewal
,timestamp
))
552 clp
->cl_last_renewal
= timestamp
;
553 spin_unlock(&clp
->cl_lock
);
556 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
558 struct nfs_client
*clp
= server
->nfs_client
;
560 if (!nfs4_has_session(clp
))
561 do_renew_lease(clp
, timestamp
);
564 struct nfs4_call_sync_data
{
565 const struct nfs_server
*seq_server
;
566 struct nfs4_sequence_args
*seq_args
;
567 struct nfs4_sequence_res
*seq_res
;
570 void nfs4_init_sequence(struct nfs4_sequence_args
*args
,
571 struct nfs4_sequence_res
*res
, int cache_reply
)
573 args
->sa_slot
= NULL
;
574 args
->sa_cache_this
= cache_reply
;
575 args
->sa_privileged
= 0;
580 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
582 args
->sa_privileged
= 1;
585 int nfs40_setup_sequence(struct nfs4_slot_table
*tbl
,
586 struct nfs4_sequence_args
*args
,
587 struct nfs4_sequence_res
*res
,
588 struct rpc_task
*task
)
590 struct nfs4_slot
*slot
;
592 /* slot already allocated? */
593 if (res
->sr_slot
!= NULL
)
596 spin_lock(&tbl
->slot_tbl_lock
);
597 if (nfs4_slot_tbl_draining(tbl
) && !args
->sa_privileged
)
600 slot
= nfs4_alloc_slot(tbl
);
602 if (slot
== ERR_PTR(-ENOMEM
))
603 task
->tk_timeout
= HZ
>> 2;
606 spin_unlock(&tbl
->slot_tbl_lock
);
608 args
->sa_slot
= slot
;
612 rpc_call_start(task
);
616 if (args
->sa_privileged
)
617 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
618 NULL
, RPC_PRIORITY_PRIVILEGED
);
620 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
621 spin_unlock(&tbl
->slot_tbl_lock
);
624 EXPORT_SYMBOL_GPL(nfs40_setup_sequence
);
626 static int nfs40_sequence_done(struct rpc_task
*task
,
627 struct nfs4_sequence_res
*res
)
629 struct nfs4_slot
*slot
= res
->sr_slot
;
630 struct nfs4_slot_table
*tbl
;
636 spin_lock(&tbl
->slot_tbl_lock
);
637 if (!nfs41_wake_and_assign_slot(tbl
, slot
))
638 nfs4_free_slot(tbl
, slot
);
639 spin_unlock(&tbl
->slot_tbl_lock
);
646 #if defined(CONFIG_NFS_V4_1)
648 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
650 struct nfs4_session
*session
;
651 struct nfs4_slot_table
*tbl
;
652 struct nfs4_slot
*slot
= res
->sr_slot
;
653 bool send_new_highest_used_slotid
= false;
656 session
= tbl
->session
;
658 spin_lock(&tbl
->slot_tbl_lock
);
659 /* Be nice to the server: try to ensure that the last transmitted
660 * value for highest_user_slotid <= target_highest_slotid
662 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
663 send_new_highest_used_slotid
= true;
665 if (nfs41_wake_and_assign_slot(tbl
, slot
)) {
666 send_new_highest_used_slotid
= false;
669 nfs4_free_slot(tbl
, slot
);
671 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
672 send_new_highest_used_slotid
= false;
674 spin_unlock(&tbl
->slot_tbl_lock
);
676 if (send_new_highest_used_slotid
)
677 nfs41_notify_server(session
->clp
);
680 int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
682 struct nfs4_session
*session
;
683 struct nfs4_slot
*slot
= res
->sr_slot
;
684 struct nfs_client
*clp
;
685 bool interrupted
= false;
690 /* don't increment the sequence number if the task wasn't sent */
691 if (!RPC_WAS_SENT(task
))
694 session
= slot
->table
->session
;
696 if (slot
->interrupted
) {
697 slot
->interrupted
= 0;
701 trace_nfs4_sequence_done(session
, res
);
702 /* Check the SEQUENCE operation status */
703 switch (res
->sr_status
) {
705 /* Update the slot's sequence and clientid lease timer */
708 do_renew_lease(clp
, res
->sr_timestamp
);
709 /* Check sequence flags */
710 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
711 nfs41_update_target_slotid(slot
->table
, slot
, res
);
715 * sr_status remains 1 if an RPC level error occurred.
716 * The server may or may not have processed the sequence
718 * Mark the slot as having hosted an interrupted RPC call.
720 slot
->interrupted
= 1;
723 /* The server detected a resend of the RPC call and
724 * returned NFS4ERR_DELAY as per Section 2.10.6.2
727 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
732 case -NFS4ERR_BADSLOT
:
734 * The slot id we used was probably retired. Try again
735 * using a different slot id.
738 case -NFS4ERR_SEQ_MISORDERED
:
740 * Was the last operation on this sequence interrupted?
741 * If so, retry after bumping the sequence number.
748 * Could this slot have been previously retired?
749 * If so, then the server may be expecting seq_nr = 1!
751 if (slot
->seq_nr
!= 1) {
756 case -NFS4ERR_SEQ_FALSE_RETRY
:
760 /* Just update the slot sequence no. */
764 /* The session may be reset by one of the error handlers. */
765 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
766 nfs41_sequence_free_slot(res
);
770 if (rpc_restart_call_prepare(task
)) {
776 if (!rpc_restart_call(task
))
778 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
781 EXPORT_SYMBOL_GPL(nfs41_sequence_done
);
783 int nfs4_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
785 if (res
->sr_slot
== NULL
)
787 if (!res
->sr_slot
->table
->session
)
788 return nfs40_sequence_done(task
, res
);
789 return nfs41_sequence_done(task
, res
);
791 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
793 int nfs41_setup_sequence(struct nfs4_session
*session
,
794 struct nfs4_sequence_args
*args
,
795 struct nfs4_sequence_res
*res
,
796 struct rpc_task
*task
)
798 struct nfs4_slot
*slot
;
799 struct nfs4_slot_table
*tbl
;
801 dprintk("--> %s\n", __func__
);
802 /* slot already allocated? */
803 if (res
->sr_slot
!= NULL
)
806 tbl
= &session
->fc_slot_table
;
808 task
->tk_timeout
= 0;
810 spin_lock(&tbl
->slot_tbl_lock
);
811 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
812 !args
->sa_privileged
) {
813 /* The state manager will wait until the slot table is empty */
814 dprintk("%s session is draining\n", __func__
);
818 slot
= nfs4_alloc_slot(tbl
);
820 /* If out of memory, try again in 1/4 second */
821 if (slot
== ERR_PTR(-ENOMEM
))
822 task
->tk_timeout
= HZ
>> 2;
823 dprintk("<-- %s: no free slots\n", __func__
);
826 spin_unlock(&tbl
->slot_tbl_lock
);
828 args
->sa_slot
= slot
;
830 dprintk("<-- %s slotid=%u seqid=%u\n", __func__
,
831 slot
->slot_nr
, slot
->seq_nr
);
834 res
->sr_timestamp
= jiffies
;
835 res
->sr_status_flags
= 0;
837 * sr_status is only set in decode_sequence, and so will remain
838 * set to 1 if an rpc level failure occurs.
841 trace_nfs4_setup_sequence(session
, args
);
843 rpc_call_start(task
);
846 /* Privileged tasks are queued with top priority */
847 if (args
->sa_privileged
)
848 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
849 NULL
, RPC_PRIORITY_PRIVILEGED
);
851 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
852 spin_unlock(&tbl
->slot_tbl_lock
);
855 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
857 static int nfs4_setup_sequence(const struct nfs_server
*server
,
858 struct nfs4_sequence_args
*args
,
859 struct nfs4_sequence_res
*res
,
860 struct rpc_task
*task
)
862 struct nfs4_session
*session
= nfs4_get_session(server
);
866 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
869 dprintk("--> %s clp %p session %p sr_slot %u\n",
870 __func__
, session
->clp
, session
, res
->sr_slot
?
871 res
->sr_slot
->slot_nr
: NFS4_NO_SLOT
);
873 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
875 dprintk("<-- %s status=%d\n", __func__
, ret
);
879 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
881 struct nfs4_call_sync_data
*data
= calldata
;
882 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
884 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
886 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
889 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
891 struct nfs4_call_sync_data
*data
= calldata
;
893 nfs41_sequence_done(task
, data
->seq_res
);
896 static const struct rpc_call_ops nfs41_call_sync_ops
= {
897 .rpc_call_prepare
= nfs41_call_sync_prepare
,
898 .rpc_call_done
= nfs41_call_sync_done
,
901 #else /* !CONFIG_NFS_V4_1 */
903 static int nfs4_setup_sequence(const struct nfs_server
*server
,
904 struct nfs4_sequence_args
*args
,
905 struct nfs4_sequence_res
*res
,
906 struct rpc_task
*task
)
908 return nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
912 int nfs4_sequence_done(struct rpc_task
*task
,
913 struct nfs4_sequence_res
*res
)
915 return nfs40_sequence_done(task
, res
);
917 EXPORT_SYMBOL_GPL(nfs4_sequence_done
);
919 #endif /* !CONFIG_NFS_V4_1 */
921 static void nfs40_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
923 struct nfs4_call_sync_data
*data
= calldata
;
924 nfs4_setup_sequence(data
->seq_server
,
925 data
->seq_args
, data
->seq_res
, task
);
928 static void nfs40_call_sync_done(struct rpc_task
*task
, void *calldata
)
930 struct nfs4_call_sync_data
*data
= calldata
;
931 nfs4_sequence_done(task
, data
->seq_res
);
934 static const struct rpc_call_ops nfs40_call_sync_ops
= {
935 .rpc_call_prepare
= nfs40_call_sync_prepare
,
936 .rpc_call_done
= nfs40_call_sync_done
,
939 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
940 struct nfs_server
*server
,
941 struct rpc_message
*msg
,
942 struct nfs4_sequence_args
*args
,
943 struct nfs4_sequence_res
*res
)
946 struct rpc_task
*task
;
947 struct nfs_client
*clp
= server
->nfs_client
;
948 struct nfs4_call_sync_data data
= {
949 .seq_server
= server
,
953 struct rpc_task_setup task_setup
= {
956 .callback_ops
= clp
->cl_mvops
->call_sync_ops
,
957 .callback_data
= &data
960 task
= rpc_run_task(&task_setup
);
964 ret
= task
->tk_status
;
970 int nfs4_call_sync(struct rpc_clnt
*clnt
,
971 struct nfs_server
*server
,
972 struct rpc_message
*msg
,
973 struct nfs4_sequence_args
*args
,
974 struct nfs4_sequence_res
*res
,
977 nfs4_init_sequence(args
, res
, cache_reply
);
978 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
);
981 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
983 struct nfs_inode
*nfsi
= NFS_I(dir
);
985 spin_lock(&dir
->i_lock
);
986 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
987 if (!cinfo
->atomic
|| cinfo
->before
!= dir
->i_version
)
988 nfs_force_lookup_revalidate(dir
);
989 dir
->i_version
= cinfo
->after
;
990 nfsi
->attr_gencount
= nfs_inc_attr_generation_counter();
991 nfs_fscache_invalidate(dir
);
992 spin_unlock(&dir
->i_lock
);
995 struct nfs4_opendata
{
997 struct nfs_openargs o_arg
;
998 struct nfs_openres o_res
;
999 struct nfs_open_confirmargs c_arg
;
1000 struct nfs_open_confirmres c_res
;
1001 struct nfs4_string owner_name
;
1002 struct nfs4_string group_name
;
1003 struct nfs4_label
*a_label
;
1004 struct nfs_fattr f_attr
;
1005 struct nfs4_label
*f_label
;
1007 struct dentry
*dentry
;
1008 struct nfs4_state_owner
*owner
;
1009 struct nfs4_state
*state
;
1011 unsigned long timestamp
;
1012 unsigned int rpc_done
: 1;
1013 unsigned int file_created
: 1;
1014 unsigned int is_recover
: 1;
1019 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
1020 int err
, struct nfs4_exception
*exception
)
1024 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1026 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
1027 exception
->retry
= 1;
1032 nfs4_map_atomic_open_share(struct nfs_server
*server
,
1033 fmode_t fmode
, int openflags
)
1037 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
1039 res
= NFS4_SHARE_ACCESS_READ
;
1042 res
= NFS4_SHARE_ACCESS_WRITE
;
1044 case FMODE_READ
|FMODE_WRITE
:
1045 res
= NFS4_SHARE_ACCESS_BOTH
;
1047 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
1049 /* Want no delegation if we're using O_DIRECT */
1050 if (openflags
& O_DIRECT
)
1051 res
|= NFS4_SHARE_WANT_NO_DELEG
;
1056 static enum open_claim_type4
1057 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
1058 enum open_claim_type4 claim
)
1060 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
1065 case NFS4_OPEN_CLAIM_FH
:
1066 return NFS4_OPEN_CLAIM_NULL
;
1067 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1068 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1069 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1070 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
1074 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
1076 p
->o_res
.f_attr
= &p
->f_attr
;
1077 p
->o_res
.f_label
= p
->f_label
;
1078 p
->o_res
.seqid
= p
->o_arg
.seqid
;
1079 p
->c_res
.seqid
= p
->c_arg
.seqid
;
1080 p
->o_res
.server
= p
->o_arg
.server
;
1081 p
->o_res
.access_request
= p
->o_arg
.access
;
1082 nfs_fattr_init(&p
->f_attr
);
1083 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
1086 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
1087 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
1088 const struct iattr
*attrs
,
1089 struct nfs4_label
*label
,
1090 enum open_claim_type4 claim
,
1093 struct dentry
*parent
= dget_parent(dentry
);
1094 struct inode
*dir
= d_inode(parent
);
1095 struct nfs_server
*server
= NFS_SERVER(dir
);
1096 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
1097 struct nfs4_opendata
*p
;
1099 p
= kzalloc(sizeof(*p
), gfp_mask
);
1103 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
1104 if (IS_ERR(p
->f_label
))
1107 p
->a_label
= nfs4_label_alloc(server
, gfp_mask
);
1108 if (IS_ERR(p
->a_label
))
1111 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
1112 p
->o_arg
.seqid
= alloc_seqid(&sp
->so_seqid
, gfp_mask
);
1113 if (IS_ERR(p
->o_arg
.seqid
))
1114 goto err_free_label
;
1115 nfs_sb_active(dentry
->d_sb
);
1116 p
->dentry
= dget(dentry
);
1119 atomic_inc(&sp
->so_count
);
1120 p
->o_arg
.open_flags
= flags
;
1121 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
1122 p
->o_arg
.share_access
= nfs4_map_atomic_open_share(server
,
1124 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1125 * will return permission denied for all bits until close */
1126 if (!(flags
& O_EXCL
)) {
1127 /* ask server to check for all possible rights as results
1129 p
->o_arg
.access
= NFS4_ACCESS_READ
| NFS4_ACCESS_MODIFY
|
1130 NFS4_ACCESS_EXTEND
| NFS4_ACCESS_EXECUTE
;
1132 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
1133 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
1134 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
1135 p
->o_arg
.name
= &dentry
->d_name
;
1136 p
->o_arg
.server
= server
;
1137 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
1138 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
1139 p
->o_arg
.label
= nfs4_label_copy(p
->a_label
, label
);
1140 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
1141 switch (p
->o_arg
.claim
) {
1142 case NFS4_OPEN_CLAIM_NULL
:
1143 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1144 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
1145 p
->o_arg
.fh
= NFS_FH(dir
);
1147 case NFS4_OPEN_CLAIM_PREVIOUS
:
1148 case NFS4_OPEN_CLAIM_FH
:
1149 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1150 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1151 p
->o_arg
.fh
= NFS_FH(d_inode(dentry
));
1153 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
1156 p
->o_arg
.u
.attrs
= &p
->attrs
;
1157 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
1160 verf
[1] = current
->pid
;
1161 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
1162 sizeof(p
->o_arg
.u
.verifier
.data
));
1164 p
->c_arg
.fh
= &p
->o_res
.fh
;
1165 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
1166 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
1167 nfs4_init_opendata_res(p
);
1168 kref_init(&p
->kref
);
1172 nfs4_label_free(p
->a_label
);
1174 nfs4_label_free(p
->f_label
);
1182 static void nfs4_opendata_free(struct kref
*kref
)
1184 struct nfs4_opendata
*p
= container_of(kref
,
1185 struct nfs4_opendata
, kref
);
1186 struct super_block
*sb
= p
->dentry
->d_sb
;
1188 nfs_free_seqid(p
->o_arg
.seqid
);
1189 if (p
->state
!= NULL
)
1190 nfs4_put_open_state(p
->state
);
1191 nfs4_put_state_owner(p
->owner
);
1193 nfs4_label_free(p
->a_label
);
1194 nfs4_label_free(p
->f_label
);
1198 nfs_sb_deactive(sb
);
1199 nfs_fattr_free_names(&p
->f_attr
);
1200 kfree(p
->f_attr
.mdsthreshold
);
1204 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
1207 kref_put(&p
->kref
, nfs4_opendata_free
);
1210 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
1214 ret
= rpc_wait_for_completion_task(task
);
1218 static bool nfs4_mode_match_open_stateid(struct nfs4_state
*state
,
1221 switch(fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1222 case FMODE_READ
|FMODE_WRITE
:
1223 return state
->n_rdwr
!= 0;
1225 return state
->n_wronly
!= 0;
1227 return state
->n_rdonly
!= 0;
1233 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1237 if (open_mode
& (O_EXCL
|O_TRUNC
))
1239 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1241 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1242 && state
->n_rdonly
!= 0;
1245 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1246 && state
->n_wronly
!= 0;
1248 case FMODE_READ
|FMODE_WRITE
:
1249 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1250 && state
->n_rdwr
!= 0;
1256 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
,
1257 enum open_claim_type4 claim
)
1259 if (delegation
== NULL
)
1261 if ((delegation
->type
& fmode
) != fmode
)
1263 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1266 case NFS4_OPEN_CLAIM_NULL
:
1267 case NFS4_OPEN_CLAIM_FH
:
1269 case NFS4_OPEN_CLAIM_PREVIOUS
:
1270 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1275 nfs_mark_delegation_referenced(delegation
);
1279 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1288 case FMODE_READ
|FMODE_WRITE
:
1291 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1294 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state
*state
)
1296 struct nfs_client
*clp
= state
->owner
->so_server
->nfs_client
;
1297 bool need_recover
= false;
1299 if (test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
) && state
->n_rdonly
)
1300 need_recover
= true;
1301 if (test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
) && state
->n_wronly
)
1302 need_recover
= true;
1303 if (test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
) && state
->n_rdwr
)
1304 need_recover
= true;
1306 nfs4_state_mark_reclaim_nograce(clp
, state
);
1309 static bool nfs_need_update_open_stateid(struct nfs4_state
*state
,
1310 nfs4_stateid
*stateid
)
1312 if (test_and_set_bit(NFS_OPEN_STATE
, &state
->flags
) == 0)
1314 if (!nfs4_stateid_match_other(stateid
, &state
->open_stateid
)) {
1315 nfs_test_and_clear_all_open_stateid(state
);
1318 if (nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))
1323 static void nfs_resync_open_stateid_locked(struct nfs4_state
*state
)
1325 if (!(state
->n_wronly
|| state
->n_rdonly
|| state
->n_rdwr
))
1327 if (state
->n_wronly
)
1328 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1329 if (state
->n_rdonly
)
1330 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1332 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1333 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1336 static void nfs_clear_open_stateid_locked(struct nfs4_state
*state
,
1337 nfs4_stateid
*arg_stateid
,
1338 nfs4_stateid
*stateid
, fmode_t fmode
)
1340 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1341 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
1343 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1346 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1349 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1350 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1351 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1353 if (stateid
== NULL
)
1355 /* Handle races with OPEN */
1356 if (!nfs4_stateid_match_other(arg_stateid
, &state
->open_stateid
) ||
1357 (nfs4_stateid_match_other(stateid
, &state
->open_stateid
) &&
1358 !nfs4_stateid_is_newer(stateid
, &state
->open_stateid
))) {
1359 nfs_resync_open_stateid_locked(state
);
1362 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1363 nfs4_stateid_copy(&state
->stateid
, stateid
);
1364 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1367 static void nfs_clear_open_stateid(struct nfs4_state
*state
,
1368 nfs4_stateid
*arg_stateid
,
1369 nfs4_stateid
*stateid
, fmode_t fmode
)
1371 write_seqlock(&state
->seqlock
);
1372 nfs_clear_open_stateid_locked(state
, arg_stateid
, stateid
, fmode
);
1373 write_sequnlock(&state
->seqlock
);
1374 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1375 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1378 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1382 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1385 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1387 case FMODE_READ
|FMODE_WRITE
:
1388 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1390 if (!nfs_need_update_open_stateid(state
, stateid
))
1392 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1393 nfs4_stateid_copy(&state
->stateid
, stateid
);
1394 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1397 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
1400 * Protect the call to nfs4_state_set_mode_locked and
1401 * serialise the stateid update
1403 spin_lock(&state
->owner
->so_lock
);
1404 write_seqlock(&state
->seqlock
);
1405 if (deleg_stateid
!= NULL
) {
1406 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1407 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1409 if (open_stateid
!= NULL
)
1410 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
1411 write_sequnlock(&state
->seqlock
);
1412 update_open_stateflags(state
, fmode
);
1413 spin_unlock(&state
->owner
->so_lock
);
1416 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
1418 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1419 struct nfs_delegation
*deleg_cur
;
1422 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1425 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1426 if (deleg_cur
== NULL
)
1429 spin_lock(&deleg_cur
->lock
);
1430 if (rcu_dereference(nfsi
->delegation
) != deleg_cur
||
1431 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1432 (deleg_cur
->type
& fmode
) != fmode
)
1433 goto no_delegation_unlock
;
1435 if (delegation
== NULL
)
1436 delegation
= &deleg_cur
->stateid
;
1437 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1438 goto no_delegation_unlock
;
1440 nfs_mark_delegation_referenced(deleg_cur
);
1441 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
1443 no_delegation_unlock
:
1444 spin_unlock(&deleg_cur
->lock
);
1448 if (!ret
&& open_stateid
!= NULL
) {
1449 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
1452 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
))
1453 nfs4_schedule_state_manager(state
->owner
->so_server
->nfs_client
);
1458 static bool nfs4_update_lock_stateid(struct nfs4_lock_state
*lsp
,
1459 const nfs4_stateid
*stateid
)
1461 struct nfs4_state
*state
= lsp
->ls_state
;
1464 spin_lock(&state
->state_lock
);
1465 if (!nfs4_stateid_match_other(stateid
, &lsp
->ls_stateid
))
1467 if (!nfs4_stateid_is_newer(stateid
, &lsp
->ls_stateid
))
1469 nfs4_stateid_copy(&lsp
->ls_stateid
, stateid
);
1472 spin_unlock(&state
->state_lock
);
1476 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1478 struct nfs_delegation
*delegation
;
1481 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1482 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1487 nfs4_inode_return_delegation(inode
);
1490 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1492 struct nfs4_state
*state
= opendata
->state
;
1493 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1494 struct nfs_delegation
*delegation
;
1495 int open_mode
= opendata
->o_arg
.open_flags
;
1496 fmode_t fmode
= opendata
->o_arg
.fmode
;
1497 enum open_claim_type4 claim
= opendata
->o_arg
.claim
;
1498 nfs4_stateid stateid
;
1502 spin_lock(&state
->owner
->so_lock
);
1503 if (can_open_cached(state
, fmode
, open_mode
)) {
1504 update_open_stateflags(state
, fmode
);
1505 spin_unlock(&state
->owner
->so_lock
);
1506 goto out_return_state
;
1508 spin_unlock(&state
->owner
->so_lock
);
1510 delegation
= rcu_dereference(nfsi
->delegation
);
1511 if (!can_open_delegated(delegation
, fmode
, claim
)) {
1515 /* Save the delegation */
1516 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1518 nfs_release_seqid(opendata
->o_arg
.seqid
);
1519 if (!opendata
->is_recover
) {
1520 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1526 /* Try to update the stateid using the delegation */
1527 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1528 goto out_return_state
;
1531 return ERR_PTR(ret
);
1533 atomic_inc(&state
->count
);
1538 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1540 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1541 struct nfs_delegation
*delegation
;
1542 int delegation_flags
= 0;
1545 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1547 delegation_flags
= delegation
->flags
;
1549 switch (data
->o_arg
.claim
) {
1552 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
1553 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1554 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1555 "returning a delegation for "
1556 "OPEN(CLAIM_DELEGATE_CUR)\n",
1560 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1561 nfs_inode_set_delegation(state
->inode
,
1562 data
->owner
->so_cred
,
1565 nfs_inode_reclaim_delegation(state
->inode
,
1566 data
->owner
->so_cred
,
1571 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1572 * and update the nfs4_state.
1574 static struct nfs4_state
*
1575 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1577 struct inode
*inode
= data
->state
->inode
;
1578 struct nfs4_state
*state
= data
->state
;
1581 if (!data
->rpc_done
) {
1582 if (data
->rpc_status
) {
1583 ret
= data
->rpc_status
;
1586 /* cached opens have already been processed */
1590 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1594 if (data
->o_res
.delegation_type
!= 0)
1595 nfs4_opendata_check_deleg(data
, state
);
1597 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1599 atomic_inc(&state
->count
);
1603 return ERR_PTR(ret
);
1607 static struct nfs4_state
*
1608 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1610 struct inode
*inode
;
1611 struct nfs4_state
*state
= NULL
;
1614 if (!data
->rpc_done
) {
1615 state
= nfs4_try_open_cached(data
);
1616 trace_nfs4_cached_open(data
->state
);
1621 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1623 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1624 ret
= PTR_ERR(inode
);
1628 state
= nfs4_get_open_state(inode
, data
->owner
);
1631 if (data
->o_res
.delegation_type
!= 0)
1632 nfs4_opendata_check_deleg(data
, state
);
1633 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1637 nfs_release_seqid(data
->o_arg
.seqid
);
1642 return ERR_PTR(ret
);
1645 static struct nfs4_state
*
1646 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1648 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1649 return _nfs4_opendata_reclaim_to_nfs4_state(data
);
1650 return _nfs4_opendata_to_nfs4_state(data
);
1653 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1655 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1656 struct nfs_open_context
*ctx
;
1658 spin_lock(&state
->inode
->i_lock
);
1659 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1660 if (ctx
->state
!= state
)
1662 get_nfs_open_context(ctx
);
1663 spin_unlock(&state
->inode
->i_lock
);
1666 spin_unlock(&state
->inode
->i_lock
);
1667 return ERR_PTR(-ENOENT
);
1670 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1671 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1673 struct nfs4_opendata
*opendata
;
1675 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1676 NULL
, NULL
, claim
, GFP_NOFS
);
1677 if (opendata
== NULL
)
1678 return ERR_PTR(-ENOMEM
);
1679 opendata
->state
= state
;
1680 atomic_inc(&state
->count
);
1684 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
,
1687 struct nfs4_state
*newstate
;
1690 if (!nfs4_mode_match_open_stateid(opendata
->state
, fmode
))
1692 opendata
->o_arg
.open_flags
= 0;
1693 opendata
->o_arg
.fmode
= fmode
;
1694 opendata
->o_arg
.share_access
= nfs4_map_atomic_open_share(
1695 NFS_SB(opendata
->dentry
->d_sb
),
1697 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1698 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1699 nfs4_init_opendata_res(opendata
);
1700 ret
= _nfs4_recover_proc_open(opendata
);
1703 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1704 if (IS_ERR(newstate
))
1705 return PTR_ERR(newstate
);
1706 if (newstate
!= opendata
->state
)
1708 nfs4_close_state(newstate
, fmode
);
1712 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1716 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1717 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1718 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1719 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1720 /* memory barrier prior to reading state->n_* */
1721 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1722 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1724 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1727 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1730 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1734 * We may have performed cached opens for all three recoveries.
1735 * Check if we need to update the current stateid.
1737 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1738 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1739 write_seqlock(&state
->seqlock
);
1740 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1741 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1742 write_sequnlock(&state
->seqlock
);
1749 * reclaim state on the server after a reboot.
1751 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1753 struct nfs_delegation
*delegation
;
1754 struct nfs4_opendata
*opendata
;
1755 fmode_t delegation_type
= 0;
1758 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1759 NFS4_OPEN_CLAIM_PREVIOUS
);
1760 if (IS_ERR(opendata
))
1761 return PTR_ERR(opendata
);
1763 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1764 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1765 delegation_type
= delegation
->type
;
1767 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1768 status
= nfs4_open_recover(opendata
, state
);
1769 nfs4_opendata_put(opendata
);
1773 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1775 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1776 struct nfs4_exception exception
= { };
1779 err
= _nfs4_do_open_reclaim(ctx
, state
);
1780 trace_nfs4_open_reclaim(ctx
, 0, err
);
1781 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1783 if (err
!= -NFS4ERR_DELAY
)
1785 nfs4_handle_exception(server
, err
, &exception
);
1786 } while (exception
.retry
);
1790 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1792 struct nfs_open_context
*ctx
;
1795 ctx
= nfs4_state_find_open_context(state
);
1798 ret
= nfs4_do_open_reclaim(ctx
, state
);
1799 put_nfs_open_context(ctx
);
1803 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1807 printk(KERN_ERR
"NFS: %s: unhandled error "
1808 "%d.\n", __func__
, err
);
1814 case -NFS4ERR_BADSESSION
:
1815 case -NFS4ERR_BADSLOT
:
1816 case -NFS4ERR_BAD_HIGH_SLOT
:
1817 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1818 case -NFS4ERR_DEADSESSION
:
1819 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1820 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1822 case -NFS4ERR_STALE_CLIENTID
:
1823 case -NFS4ERR_STALE_STATEID
:
1824 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1825 case -NFS4ERR_EXPIRED
:
1826 /* Don't recall a delegation if it was lost */
1827 nfs4_schedule_lease_recovery(server
->nfs_client
);
1829 case -NFS4ERR_MOVED
:
1830 nfs4_schedule_migration_recovery(server
);
1832 case -NFS4ERR_LEASE_MOVED
:
1833 nfs4_schedule_lease_moved_recovery(server
->nfs_client
);
1835 case -NFS4ERR_DELEG_REVOKED
:
1836 case -NFS4ERR_ADMIN_REVOKED
:
1837 case -NFS4ERR_BAD_STATEID
:
1838 case -NFS4ERR_OPENMODE
:
1839 nfs_inode_find_state_and_recover(state
->inode
,
1841 nfs4_schedule_stateid_recovery(server
, state
);
1843 case -NFS4ERR_DELAY
:
1844 case -NFS4ERR_GRACE
:
1845 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1849 case -NFS4ERR_DENIED
:
1850 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1856 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
,
1857 struct nfs4_state
*state
, const nfs4_stateid
*stateid
,
1860 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1861 struct nfs4_opendata
*opendata
;
1864 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1865 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
1866 if (IS_ERR(opendata
))
1867 return PTR_ERR(opendata
);
1868 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
1869 write_seqlock(&state
->seqlock
);
1870 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1871 write_sequnlock(&state
->seqlock
);
1872 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1873 switch (type
& (FMODE_READ
|FMODE_WRITE
)) {
1874 case FMODE_READ
|FMODE_WRITE
:
1876 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
);
1879 err
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
);
1883 err
= nfs4_open_recover_helper(opendata
, FMODE_READ
);
1885 nfs4_opendata_put(opendata
);
1886 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
1889 static void nfs4_open_confirm_prepare(struct rpc_task
*task
, void *calldata
)
1891 struct nfs4_opendata
*data
= calldata
;
1893 nfs40_setup_sequence(data
->o_arg
.server
->nfs_client
->cl_slot_tbl
,
1894 &data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, task
);
1897 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1899 struct nfs4_opendata
*data
= calldata
;
1901 nfs40_sequence_done(task
, &data
->c_res
.seq_res
);
1903 data
->rpc_status
= task
->tk_status
;
1904 if (data
->rpc_status
== 0) {
1905 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
1906 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1907 renew_lease(data
->o_res
.server
, data
->timestamp
);
1912 static void nfs4_open_confirm_release(void *calldata
)
1914 struct nfs4_opendata
*data
= calldata
;
1915 struct nfs4_state
*state
= NULL
;
1917 /* If this request hasn't been cancelled, do nothing */
1918 if (data
->cancelled
== 0)
1920 /* In case of error, no cleanup! */
1921 if (!data
->rpc_done
)
1923 state
= nfs4_opendata_to_nfs4_state(data
);
1925 nfs4_close_state(state
, data
->o_arg
.fmode
);
1927 nfs4_opendata_put(data
);
1930 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1931 .rpc_call_prepare
= nfs4_open_confirm_prepare
,
1932 .rpc_call_done
= nfs4_open_confirm_done
,
1933 .rpc_release
= nfs4_open_confirm_release
,
1937 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1939 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1941 struct nfs_server
*server
= NFS_SERVER(d_inode(data
->dir
));
1942 struct rpc_task
*task
;
1943 struct rpc_message msg
= {
1944 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1945 .rpc_argp
= &data
->c_arg
,
1946 .rpc_resp
= &data
->c_res
,
1947 .rpc_cred
= data
->owner
->so_cred
,
1949 struct rpc_task_setup task_setup_data
= {
1950 .rpc_client
= server
->client
,
1951 .rpc_message
= &msg
,
1952 .callback_ops
= &nfs4_open_confirm_ops
,
1953 .callback_data
= data
,
1954 .workqueue
= nfsiod_workqueue
,
1955 .flags
= RPC_TASK_ASYNC
,
1959 nfs4_init_sequence(&data
->c_arg
.seq_args
, &data
->c_res
.seq_res
, 1);
1960 kref_get(&data
->kref
);
1962 data
->rpc_status
= 0;
1963 data
->timestamp
= jiffies
;
1964 if (data
->is_recover
)
1965 nfs4_set_sequence_privileged(&data
->c_arg
.seq_args
);
1966 task
= rpc_run_task(&task_setup_data
);
1968 return PTR_ERR(task
);
1969 status
= nfs4_wait_for_completion_rpc_task(task
);
1971 data
->cancelled
= 1;
1974 status
= data
->rpc_status
;
1979 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1981 struct nfs4_opendata
*data
= calldata
;
1982 struct nfs4_state_owner
*sp
= data
->owner
;
1983 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
1984 enum open_claim_type4 claim
= data
->o_arg
.claim
;
1986 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1989 * Check if we still need to send an OPEN call, or if we can use
1990 * a delegation instead.
1992 if (data
->state
!= NULL
) {
1993 struct nfs_delegation
*delegation
;
1995 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1998 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1999 if (can_open_delegated(delegation
, data
->o_arg
.fmode
, claim
))
2000 goto unlock_no_action
;
2003 /* Update client id. */
2004 data
->o_arg
.clientid
= clp
->cl_clientid
;
2008 case NFS4_OPEN_CLAIM_PREVIOUS
:
2009 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
2010 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
2011 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
2012 case NFS4_OPEN_CLAIM_FH
:
2013 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
2014 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
2016 data
->timestamp
= jiffies
;
2017 if (nfs4_setup_sequence(data
->o_arg
.server
,
2018 &data
->o_arg
.seq_args
,
2019 &data
->o_res
.seq_res
,
2021 nfs_release_seqid(data
->o_arg
.seqid
);
2023 /* Set the create mode (note dependency on the session type) */
2024 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
2025 if (data
->o_arg
.open_flags
& O_EXCL
) {
2026 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
2027 if (nfs4_has_persistent_session(clp
))
2028 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
2029 else if (clp
->cl_mvops
->minor_version
> 0)
2030 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
2034 trace_nfs4_cached_open(data
->state
);
2037 task
->tk_action
= NULL
;
2039 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
2042 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
2044 struct nfs4_opendata
*data
= calldata
;
2046 data
->rpc_status
= task
->tk_status
;
2048 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
2051 if (task
->tk_status
== 0) {
2052 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
2053 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
2057 data
->rpc_status
= -ELOOP
;
2060 data
->rpc_status
= -EISDIR
;
2063 data
->rpc_status
= -ENOTDIR
;
2066 renew_lease(data
->o_res
.server
, data
->timestamp
);
2067 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
2068 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
2073 static void nfs4_open_release(void *calldata
)
2075 struct nfs4_opendata
*data
= calldata
;
2076 struct nfs4_state
*state
= NULL
;
2078 /* If this request hasn't been cancelled, do nothing */
2079 if (data
->cancelled
== 0)
2081 /* In case of error, no cleanup! */
2082 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
2084 /* In case we need an open_confirm, no cleanup! */
2085 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
2087 state
= nfs4_opendata_to_nfs4_state(data
);
2089 nfs4_close_state(state
, data
->o_arg
.fmode
);
2091 nfs4_opendata_put(data
);
2094 static const struct rpc_call_ops nfs4_open_ops
= {
2095 .rpc_call_prepare
= nfs4_open_prepare
,
2096 .rpc_call_done
= nfs4_open_done
,
2097 .rpc_release
= nfs4_open_release
,
2100 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
2102 struct inode
*dir
= d_inode(data
->dir
);
2103 struct nfs_server
*server
= NFS_SERVER(dir
);
2104 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2105 struct nfs_openres
*o_res
= &data
->o_res
;
2106 struct rpc_task
*task
;
2107 struct rpc_message msg
= {
2108 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
2111 .rpc_cred
= data
->owner
->so_cred
,
2113 struct rpc_task_setup task_setup_data
= {
2114 .rpc_client
= server
->client
,
2115 .rpc_message
= &msg
,
2116 .callback_ops
= &nfs4_open_ops
,
2117 .callback_data
= data
,
2118 .workqueue
= nfsiod_workqueue
,
2119 .flags
= RPC_TASK_ASYNC
,
2123 nfs4_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
2124 kref_get(&data
->kref
);
2126 data
->rpc_status
= 0;
2127 data
->cancelled
= 0;
2128 data
->is_recover
= 0;
2130 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
2131 data
->is_recover
= 1;
2133 task
= rpc_run_task(&task_setup_data
);
2135 return PTR_ERR(task
);
2136 status
= nfs4_wait_for_completion_rpc_task(task
);
2138 data
->cancelled
= 1;
2141 status
= data
->rpc_status
;
2147 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
2149 struct inode
*dir
= d_inode(data
->dir
);
2150 struct nfs_openres
*o_res
= &data
->o_res
;
2153 status
= nfs4_run_open_task(data
, 1);
2154 if (status
!= 0 || !data
->rpc_done
)
2157 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
2159 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2160 status
= _nfs4_proc_open_confirm(data
);
2169 * Additional permission checks in order to distinguish between an
2170 * open for read, and an open for execute. This works around the
2171 * fact that NFSv4 OPEN treats read and execute permissions as being
2173 * Note that in the non-execute case, we want to turn off permission
2174 * checking if we just created a new file (POSIX open() semantics).
2176 static int nfs4_opendata_access(struct rpc_cred
*cred
,
2177 struct nfs4_opendata
*opendata
,
2178 struct nfs4_state
*state
, fmode_t fmode
,
2181 struct nfs_access_entry cache
;
2184 /* access call failed or for some reason the server doesn't
2185 * support any access modes -- defer access call until later */
2186 if (opendata
->o_res
.access_supported
== 0)
2191 * Use openflags to check for exec, because fmode won't
2192 * always have FMODE_EXEC set when file open for exec.
2194 if (openflags
& __FMODE_EXEC
) {
2195 /* ONLY check for exec rights */
2197 } else if ((fmode
& FMODE_READ
) && !opendata
->file_created
)
2201 cache
.jiffies
= jiffies
;
2202 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
2203 nfs_access_add_cache(state
->inode
, &cache
);
2205 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
2208 /* even though OPEN succeeded, access is denied. Close the file */
2209 nfs4_close_state(state
, fmode
);
2214 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2216 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
2218 struct inode
*dir
= d_inode(data
->dir
);
2219 struct nfs_server
*server
= NFS_SERVER(dir
);
2220 struct nfs_openargs
*o_arg
= &data
->o_arg
;
2221 struct nfs_openres
*o_res
= &data
->o_res
;
2224 status
= nfs4_run_open_task(data
, 0);
2225 if (!data
->rpc_done
)
2228 if (status
== -NFS4ERR_BADNAME
&&
2229 !(o_arg
->open_flags
& O_CREAT
))
2234 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
2236 if (o_arg
->open_flags
& O_CREAT
) {
2237 update_changeattr(dir
, &o_res
->cinfo
);
2238 if (o_arg
->open_flags
& O_EXCL
)
2239 data
->file_created
= 1;
2240 else if (o_res
->cinfo
.before
!= o_res
->cinfo
.after
)
2241 data
->file_created
= 1;
2243 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
2244 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
2245 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
2246 status
= _nfs4_proc_open_confirm(data
);
2250 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
2251 nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
2255 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
2257 return nfs4_client_recover_expired_lease(server
->nfs_client
);
2262 * reclaim state on the server after a network partition.
2263 * Assumes caller holds the appropriate lock
2265 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2267 struct nfs4_opendata
*opendata
;
2270 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
2271 NFS4_OPEN_CLAIM_FH
);
2272 if (IS_ERR(opendata
))
2273 return PTR_ERR(opendata
);
2274 ret
= nfs4_open_recover(opendata
, state
);
2276 d_drop(ctx
->dentry
);
2277 nfs4_opendata_put(opendata
);
2281 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
2283 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2284 struct nfs4_exception exception
= { };
2288 err
= _nfs4_open_expired(ctx
, state
);
2289 trace_nfs4_open_expired(ctx
, 0, err
);
2290 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
2295 case -NFS4ERR_GRACE
:
2296 case -NFS4ERR_DELAY
:
2297 nfs4_handle_exception(server
, err
, &exception
);
2300 } while (exception
.retry
);
2305 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2307 struct nfs_open_context
*ctx
;
2310 ctx
= nfs4_state_find_open_context(state
);
2313 ret
= nfs4_do_open_expired(ctx
, state
);
2314 put_nfs_open_context(ctx
);
2318 static void nfs_finish_clear_delegation_stateid(struct nfs4_state
*state
)
2320 nfs_remove_bad_delegation(state
->inode
);
2321 write_seqlock(&state
->seqlock
);
2322 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
2323 write_sequnlock(&state
->seqlock
);
2324 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
2327 static void nfs40_clear_delegation_stateid(struct nfs4_state
*state
)
2329 if (rcu_access_pointer(NFS_I(state
->inode
)->delegation
) != NULL
)
2330 nfs_finish_clear_delegation_stateid(state
);
2333 static int nfs40_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2335 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2336 nfs40_clear_delegation_stateid(state
);
2337 return nfs4_open_expired(sp
, state
);
2340 #if defined(CONFIG_NFS_V4_1)
2341 static void nfs41_check_delegation_stateid(struct nfs4_state
*state
)
2343 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2344 nfs4_stateid stateid
;
2345 struct nfs_delegation
*delegation
;
2346 struct rpc_cred
*cred
;
2349 /* Get the delegation credential for use by test/free_stateid */
2351 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
2352 if (delegation
== NULL
) {
2357 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
2358 cred
= get_rpccred(delegation
->cred
);
2360 status
= nfs41_test_stateid(server
, &stateid
, cred
);
2361 trace_nfs4_test_delegation_stateid(state
, NULL
, status
);
2363 if (status
!= NFS_OK
) {
2364 /* Free the stateid unless the server explicitly
2365 * informs us the stateid is unrecognized. */
2366 if (status
!= -NFS4ERR_BAD_STATEID
)
2367 nfs41_free_stateid(server
, &stateid
, cred
);
2368 nfs_finish_clear_delegation_stateid(state
);
2375 * nfs41_check_open_stateid - possibly free an open stateid
2377 * @state: NFSv4 state for an inode
2379 * Returns NFS_OK if recovery for this stateid is now finished.
2380 * Otherwise a negative NFS4ERR value is returned.
2382 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
2384 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2385 nfs4_stateid
*stateid
= &state
->open_stateid
;
2386 struct rpc_cred
*cred
= state
->owner
->so_cred
;
2389 /* If a state reset has been done, test_stateid is unneeded */
2390 if ((test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) == 0) &&
2391 (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) == 0) &&
2392 (test_bit(NFS_O_RDWR_STATE
, &state
->flags
) == 0))
2393 return -NFS4ERR_BAD_STATEID
;
2395 status
= nfs41_test_stateid(server
, stateid
, cred
);
2396 trace_nfs4_test_open_stateid(state
, NULL
, status
);
2397 if (status
!= NFS_OK
) {
2398 /* Free the stateid unless the server explicitly
2399 * informs us the stateid is unrecognized. */
2400 if (status
!= -NFS4ERR_BAD_STATEID
)
2401 nfs41_free_stateid(server
, stateid
, cred
);
2403 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2404 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2405 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2406 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2411 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2415 nfs41_check_delegation_stateid(state
);
2416 status
= nfs41_check_open_stateid(state
);
2417 if (status
!= NFS_OK
)
2418 status
= nfs4_open_expired(sp
, state
);
2424 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2425 * fields corresponding to attributes that were used to store the verifier.
2426 * Make sure we clobber those fields in the later setattr call
2428 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
,
2429 struct iattr
*sattr
, struct nfs4_label
**label
)
2431 const u32
*attrset
= opendata
->o_res
.attrset
;
2433 if ((attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2434 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2435 sattr
->ia_valid
|= ATTR_ATIME
;
2437 if ((attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2438 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2439 sattr
->ia_valid
|= ATTR_MTIME
;
2441 /* Except MODE, it seems harmless of setting twice. */
2442 if ((attrset
[1] & FATTR4_WORD1_MODE
))
2443 sattr
->ia_valid
&= ~ATTR_MODE
;
2445 if (attrset
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2449 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2452 struct nfs_open_context
*ctx
)
2454 struct nfs4_state_owner
*sp
= opendata
->owner
;
2455 struct nfs_server
*server
= sp
->so_server
;
2456 struct dentry
*dentry
;
2457 struct nfs4_state
*state
;
2461 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2463 ret
= _nfs4_proc_open(opendata
);
2467 state
= nfs4_opendata_to_nfs4_state(opendata
);
2468 ret
= PTR_ERR(state
);
2471 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2472 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2474 dentry
= opendata
->dentry
;
2475 if (d_really_is_negative(dentry
)) {
2476 struct dentry
*alias
;
2478 alias
= d_exact_alias(dentry
, state
->inode
);
2480 alias
= d_splice_alias(igrab(state
->inode
), dentry
);
2481 /* d_splice_alias() can't fail here - it's a non-directory */
2484 ctx
->dentry
= dentry
= alias
;
2486 nfs_set_verifier(dentry
,
2487 nfs_save_change_attribute(d_inode(opendata
->dir
)));
2490 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2495 if (d_inode(dentry
) == state
->inode
) {
2496 nfs_inode_attach_open_context(ctx
);
2497 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2498 nfs4_schedule_stateid_recovery(server
, state
);
2505 * Returns a referenced nfs4_state
2507 static int _nfs4_do_open(struct inode
*dir
,
2508 struct nfs_open_context
*ctx
,
2510 struct iattr
*sattr
,
2511 struct nfs4_label
*label
,
2514 struct nfs4_state_owner
*sp
;
2515 struct nfs4_state
*state
= NULL
;
2516 struct nfs_server
*server
= NFS_SERVER(dir
);
2517 struct nfs4_opendata
*opendata
;
2518 struct dentry
*dentry
= ctx
->dentry
;
2519 struct rpc_cred
*cred
= ctx
->cred
;
2520 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2521 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2522 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2523 struct nfs4_label
*olabel
= NULL
;
2526 /* Protect against reboot recovery conflicts */
2528 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2530 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2533 status
= nfs4_recover_expired_lease(server
);
2535 goto err_put_state_owner
;
2536 if (d_really_is_positive(dentry
))
2537 nfs4_return_incompatible_delegation(d_inode(dentry
), fmode
);
2539 if (d_really_is_positive(dentry
))
2540 claim
= NFS4_OPEN_CLAIM_FH
;
2541 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2542 label
, claim
, GFP_KERNEL
);
2543 if (opendata
== NULL
)
2544 goto err_put_state_owner
;
2547 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2548 if (IS_ERR(olabel
)) {
2549 status
= PTR_ERR(olabel
);
2550 goto err_opendata_put
;
2554 if (server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2555 if (!opendata
->f_attr
.mdsthreshold
) {
2556 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2557 if (!opendata
->f_attr
.mdsthreshold
)
2558 goto err_free_label
;
2560 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2562 if (d_really_is_positive(dentry
))
2563 opendata
->state
= nfs4_get_open_state(d_inode(dentry
), sp
);
2565 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2567 goto err_free_label
;
2570 if ((opendata
->o_arg
.open_flags
& (O_CREAT
|O_EXCL
)) == (O_CREAT
|O_EXCL
) &&
2571 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2572 nfs4_exclusive_attrset(opendata
, sattr
, &label
);
2574 * send create attributes which was not set by open
2575 * with an extra setattr.
2577 if (sattr
->ia_valid
& NFS4_VALID_ATTRS
) {
2578 nfs_fattr_init(opendata
->o_res
.f_attr
);
2579 status
= nfs4_do_setattr(state
->inode
, cred
,
2580 opendata
->o_res
.f_attr
, sattr
,
2581 state
, label
, olabel
);
2583 nfs_setattr_update_inode(state
->inode
, sattr
,
2584 opendata
->o_res
.f_attr
);
2585 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2589 if (opened
&& opendata
->file_created
)
2590 *opened
|= FILE_CREATED
;
2592 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
)) {
2593 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2594 opendata
->f_attr
.mdsthreshold
= NULL
;
2597 nfs4_label_free(olabel
);
2599 nfs4_opendata_put(opendata
);
2600 nfs4_put_state_owner(sp
);
2603 nfs4_label_free(olabel
);
2605 nfs4_opendata_put(opendata
);
2606 err_put_state_owner
:
2607 nfs4_put_state_owner(sp
);
2613 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2614 struct nfs_open_context
*ctx
,
2616 struct iattr
*sattr
,
2617 struct nfs4_label
*label
,
2620 struct nfs_server
*server
= NFS_SERVER(dir
);
2621 struct nfs4_exception exception
= { };
2622 struct nfs4_state
*res
;
2626 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
, opened
);
2628 trace_nfs4_open_file(ctx
, flags
, status
);
2631 /* NOTE: BAD_SEQID means the server and client disagree about the
2632 * book-keeping w.r.t. state-changing operations
2633 * (OPEN/CLOSE/LOCK/LOCKU...)
2634 * It is actually a sign of a bug on the client or on the server.
2636 * If we receive a BAD_SEQID error in the particular case of
2637 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2638 * have unhashed the old state_owner for us, and that we can
2639 * therefore safely retry using a new one. We should still warn
2640 * the user though...
2642 if (status
== -NFS4ERR_BAD_SEQID
) {
2643 pr_warn_ratelimited("NFS: v4 server %s "
2644 " returned a bad sequence-id error!\n",
2645 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2646 exception
.retry
= 1;
2650 * BAD_STATEID on OPEN means that the server cancelled our
2651 * state before it received the OPEN_CONFIRM.
2652 * Recover by retrying the request as per the discussion
2653 * on Page 181 of RFC3530.
2655 if (status
== -NFS4ERR_BAD_STATEID
) {
2656 exception
.retry
= 1;
2659 if (status
== -EAGAIN
) {
2660 /* We must have found a delegation */
2661 exception
.retry
= 1;
2664 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2666 res
= ERR_PTR(nfs4_handle_exception(server
,
2667 status
, &exception
));
2668 } while (exception
.retry
);
2672 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2673 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2674 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2675 struct nfs4_label
*olabel
)
2677 struct nfs_server
*server
= NFS_SERVER(inode
);
2678 struct nfs_setattrargs arg
= {
2679 .fh
= NFS_FH(inode
),
2682 .bitmask
= server
->attr_bitmask
,
2685 struct nfs_setattrres res
= {
2690 struct rpc_message msg
= {
2691 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2696 struct rpc_cred
*delegation_cred
= NULL
;
2697 unsigned long timestamp
= jiffies
;
2702 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2704 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2706 nfs_fattr_init(fattr
);
2708 /* Servers should only apply open mode checks for file size changes */
2709 truncate
= (sattr
->ia_valid
& ATTR_SIZE
) ? true : false;
2710 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2712 if (nfs4_copy_delegation_stateid(inode
, fmode
, &arg
.stateid
, &delegation_cred
)) {
2713 /* Use that stateid */
2714 } else if (truncate
&& state
!= NULL
) {
2715 struct nfs_lockowner lockowner
= {
2716 .l_owner
= current
->files
,
2717 .l_pid
= current
->tgid
,
2719 if (!nfs4_valid_open_stateid(state
))
2721 if (nfs4_select_rw_stateid(state
, FMODE_WRITE
, &lockowner
,
2722 &arg
.stateid
, &delegation_cred
) == -EIO
)
2725 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
2726 if (delegation_cred
)
2727 msg
.rpc_cred
= delegation_cred
;
2729 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2731 put_rpccred(delegation_cred
);
2732 if (status
== 0 && state
!= NULL
)
2733 renew_lease(server
, timestamp
);
2734 trace_nfs4_setattr(inode
, &arg
.stateid
, status
);
2738 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2739 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2740 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2741 struct nfs4_label
*olabel
)
2743 struct nfs_server
*server
= NFS_SERVER(inode
);
2744 struct nfs4_exception exception
= {
2750 err
= _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, ilabel
, olabel
);
2752 case -NFS4ERR_OPENMODE
:
2753 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2754 pr_warn_once("NFSv4: server %s is incorrectly "
2755 "applying open mode checks to "
2756 "a SETATTR that is not "
2757 "changing file size.\n",
2758 server
->nfs_client
->cl_hostname
);
2760 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2762 if (sattr
->ia_valid
& ATTR_OPEN
)
2767 err
= nfs4_handle_exception(server
, err
, &exception
);
2768 } while (exception
.retry
);
2774 nfs4_wait_on_layoutreturn(struct inode
*inode
, struct rpc_task
*task
)
2776 if (inode
== NULL
|| !nfs_have_layout(inode
))
2779 return pnfs_wait_on_layoutreturn(inode
, task
);
2782 struct nfs4_closedata
{
2783 struct inode
*inode
;
2784 struct nfs4_state
*state
;
2785 struct nfs_closeargs arg
;
2786 struct nfs_closeres res
;
2787 struct nfs_fattr fattr
;
2788 unsigned long timestamp
;
2793 static void nfs4_free_closedata(void *data
)
2795 struct nfs4_closedata
*calldata
= data
;
2796 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2797 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2800 pnfs_roc_release(calldata
->state
->inode
);
2801 nfs4_put_open_state(calldata
->state
);
2802 nfs_free_seqid(calldata
->arg
.seqid
);
2803 nfs4_put_state_owner(sp
);
2804 nfs_sb_deactive(sb
);
2808 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2810 struct nfs4_closedata
*calldata
= data
;
2811 struct nfs4_state
*state
= calldata
->state
;
2812 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2813 nfs4_stateid
*res_stateid
= NULL
;
2815 dprintk("%s: begin!\n", __func__
);
2816 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2818 trace_nfs4_close(state
, &calldata
->arg
, &calldata
->res
, task
->tk_status
);
2819 /* hmm. we are done with the inode, and in the process of freeing
2820 * the state_owner. we keep this around to process errors
2822 switch (task
->tk_status
) {
2824 res_stateid
= &calldata
->res
.stateid
;
2826 pnfs_roc_set_barrier(state
->inode
,
2827 calldata
->roc_barrier
);
2828 renew_lease(server
, calldata
->timestamp
);
2830 case -NFS4ERR_ADMIN_REVOKED
:
2831 case -NFS4ERR_STALE_STATEID
:
2832 case -NFS4ERR_OLD_STATEID
:
2833 case -NFS4ERR_BAD_STATEID
:
2834 case -NFS4ERR_EXPIRED
:
2835 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
2836 &state
->open_stateid
)) {
2837 rpc_restart_call_prepare(task
);
2840 if (calldata
->arg
.fmode
== 0)
2843 if (nfs4_async_handle_error(task
, server
, state
, NULL
) == -EAGAIN
) {
2844 rpc_restart_call_prepare(task
);
2848 nfs_clear_open_stateid(state
, &calldata
->arg
.stateid
,
2849 res_stateid
, calldata
->arg
.fmode
);
2851 nfs_release_seqid(calldata
->arg
.seqid
);
2852 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
2853 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
2856 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
2858 struct nfs4_closedata
*calldata
= data
;
2859 struct nfs4_state
*state
= calldata
->state
;
2860 struct inode
*inode
= calldata
->inode
;
2861 bool is_rdonly
, is_wronly
, is_rdwr
;
2864 dprintk("%s: begin!\n", __func__
);
2865 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
2868 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
2869 spin_lock(&state
->owner
->so_lock
);
2870 is_rdwr
= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2871 is_rdonly
= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2872 is_wronly
= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2873 nfs4_stateid_copy(&calldata
->arg
.stateid
, &state
->open_stateid
);
2874 /* Calculate the change in open mode */
2875 calldata
->arg
.fmode
= 0;
2876 if (state
->n_rdwr
== 0) {
2877 if (state
->n_rdonly
== 0)
2878 call_close
|= is_rdonly
;
2880 calldata
->arg
.fmode
|= FMODE_READ
;
2881 if (state
->n_wronly
== 0)
2882 call_close
|= is_wronly
;
2884 calldata
->arg
.fmode
|= FMODE_WRITE
;
2885 if (calldata
->arg
.fmode
!= (FMODE_READ
|FMODE_WRITE
))
2886 call_close
|= is_rdwr
;
2888 calldata
->arg
.fmode
|= FMODE_READ
|FMODE_WRITE
;
2890 if (!nfs4_valid_open_stateid(state
))
2892 spin_unlock(&state
->owner
->so_lock
);
2895 /* Note: exit _without_ calling nfs4_close_done */
2899 if (nfs4_wait_on_layoutreturn(inode
, task
)) {
2900 nfs_release_seqid(calldata
->arg
.seqid
);
2904 if (calldata
->arg
.fmode
== 0)
2905 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
2907 pnfs_roc_get_barrier(inode
, &calldata
->roc_barrier
);
2909 calldata
->arg
.share_access
=
2910 nfs4_map_atomic_open_share(NFS_SERVER(inode
),
2911 calldata
->arg
.fmode
, 0);
2913 nfs_fattr_init(calldata
->res
.fattr
);
2914 calldata
->timestamp
= jiffies
;
2915 if (nfs4_setup_sequence(NFS_SERVER(inode
),
2916 &calldata
->arg
.seq_args
,
2917 &calldata
->res
.seq_res
,
2919 nfs_release_seqid(calldata
->arg
.seqid
);
2920 dprintk("%s: done!\n", __func__
);
2923 task
->tk_action
= NULL
;
2925 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
2928 static const struct rpc_call_ops nfs4_close_ops
= {
2929 .rpc_call_prepare
= nfs4_close_prepare
,
2930 .rpc_call_done
= nfs4_close_done
,
2931 .rpc_release
= nfs4_free_closedata
,
2934 static bool nfs4_roc(struct inode
*inode
)
2936 if (!nfs_have_layout(inode
))
2938 return pnfs_roc(inode
);
2942 * It is possible for data to be read/written from a mem-mapped file
2943 * after the sys_close call (which hits the vfs layer as a flush).
2944 * This means that we can't safely call nfsv4 close on a file until
2945 * the inode is cleared. This in turn means that we are not good
2946 * NFSv4 citizens - we do not indicate to the server to update the file's
2947 * share state even when we are done with one of the three share
2948 * stateid's in the inode.
2950 * NOTE: Caller must be holding the sp->so_owner semaphore!
2952 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
2954 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2955 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
2956 struct nfs4_closedata
*calldata
;
2957 struct nfs4_state_owner
*sp
= state
->owner
;
2958 struct rpc_task
*task
;
2959 struct rpc_message msg
= {
2960 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
2961 .rpc_cred
= state
->owner
->so_cred
,
2963 struct rpc_task_setup task_setup_data
= {
2964 .rpc_client
= server
->client
,
2965 .rpc_message
= &msg
,
2966 .callback_ops
= &nfs4_close_ops
,
2967 .workqueue
= nfsiod_workqueue
,
2968 .flags
= RPC_TASK_ASYNC
,
2970 int status
= -ENOMEM
;
2972 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_CLEANUP
,
2973 &task_setup_data
.rpc_client
, &msg
);
2975 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
2976 if (calldata
== NULL
)
2978 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
2979 calldata
->inode
= state
->inode
;
2980 calldata
->state
= state
;
2981 calldata
->arg
.fh
= NFS_FH(state
->inode
);
2982 /* Serialization for the sequence id */
2983 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
2984 calldata
->arg
.seqid
= alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
2985 if (IS_ERR(calldata
->arg
.seqid
))
2986 goto out_free_calldata
;
2987 calldata
->arg
.fmode
= 0;
2988 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
2989 calldata
->res
.fattr
= &calldata
->fattr
;
2990 calldata
->res
.seqid
= calldata
->arg
.seqid
;
2991 calldata
->res
.server
= server
;
2992 calldata
->roc
= nfs4_roc(state
->inode
);
2993 nfs_sb_active(calldata
->inode
->i_sb
);
2995 msg
.rpc_argp
= &calldata
->arg
;
2996 msg
.rpc_resp
= &calldata
->res
;
2997 task_setup_data
.callback_data
= calldata
;
2998 task
= rpc_run_task(&task_setup_data
);
3000 return PTR_ERR(task
);
3003 status
= rpc_wait_for_completion_task(task
);
3009 nfs4_put_open_state(state
);
3010 nfs4_put_state_owner(sp
);
3014 static struct inode
*
3015 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
,
3016 int open_flags
, struct iattr
*attr
, int *opened
)
3018 struct nfs4_state
*state
;
3019 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
3021 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
3023 /* Protect against concurrent sillydeletes */
3024 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
, opened
);
3026 nfs4_label_release_security(label
);
3029 return ERR_CAST(state
);
3030 return state
->inode
;
3033 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
3035 if (ctx
->state
== NULL
)
3038 nfs4_close_sync(ctx
->state
, ctx
->mode
);
3040 nfs4_close_state(ctx
->state
, ctx
->mode
);
3043 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3044 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3045 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3047 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3049 u32 bitmask
[3] = {}, minorversion
= server
->nfs_client
->cl_minorversion
;
3050 struct nfs4_server_caps_arg args
= {
3054 struct nfs4_server_caps_res res
= {};
3055 struct rpc_message msg
= {
3056 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
3062 bitmask
[0] = FATTR4_WORD0_SUPPORTED_ATTRS
|
3063 FATTR4_WORD0_FH_EXPIRE_TYPE
|
3064 FATTR4_WORD0_LINK_SUPPORT
|
3065 FATTR4_WORD0_SYMLINK_SUPPORT
|
3066 FATTR4_WORD0_ACLSUPPORT
;
3068 bitmask
[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT
;
3070 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3072 /* Sanity check the server answers */
3073 switch (minorversion
) {
3075 res
.attr_bitmask
[1] &= FATTR4_WORD1_NFS40_MASK
;
3076 res
.attr_bitmask
[2] = 0;
3079 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS41_MASK
;
3082 res
.attr_bitmask
[2] &= FATTR4_WORD2_NFS42_MASK
;
3084 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
3085 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
3086 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
3087 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
3088 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
3089 NFS_CAP_CTIME
|NFS_CAP_MTIME
|
3090 NFS_CAP_SECURITY_LABEL
);
3091 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
&&
3092 res
.acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3093 server
->caps
|= NFS_CAP_ACLS
;
3094 if (res
.has_links
!= 0)
3095 server
->caps
|= NFS_CAP_HARDLINKS
;
3096 if (res
.has_symlinks
!= 0)
3097 server
->caps
|= NFS_CAP_SYMLINKS
;
3098 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
3099 server
->caps
|= NFS_CAP_FILEID
;
3100 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
3101 server
->caps
|= NFS_CAP_MODE
;
3102 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
3103 server
->caps
|= NFS_CAP_NLINK
;
3104 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
3105 server
->caps
|= NFS_CAP_OWNER
;
3106 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
3107 server
->caps
|= NFS_CAP_OWNER_GROUP
;
3108 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
3109 server
->caps
|= NFS_CAP_ATIME
;
3110 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
3111 server
->caps
|= NFS_CAP_CTIME
;
3112 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
3113 server
->caps
|= NFS_CAP_MTIME
;
3114 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3115 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
3116 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
3118 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
3119 sizeof(server
->attr_bitmask
));
3120 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
3122 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
3123 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
3124 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
3125 server
->cache_consistency_bitmask
[2] = 0;
3126 memcpy(server
->exclcreat_bitmask
, res
.exclcreat_bitmask
,
3127 sizeof(server
->exclcreat_bitmask
));
3128 server
->acl_bitmask
= res
.acl_bitmask
;
3129 server
->fh_expire_type
= res
.fh_expire_type
;
3135 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
3137 struct nfs4_exception exception
= { };
3140 err
= nfs4_handle_exception(server
,
3141 _nfs4_server_capabilities(server
, fhandle
),
3143 } while (exception
.retry
);
3147 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3148 struct nfs_fsinfo
*info
)
3151 struct nfs4_lookup_root_arg args
= {
3154 struct nfs4_lookup_res res
= {
3156 .fattr
= info
->fattr
,
3159 struct rpc_message msg
= {
3160 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
3165 bitmask
[0] = nfs4_fattr_bitmap
[0];
3166 bitmask
[1] = nfs4_fattr_bitmap
[1];
3168 * Process the label in the upcoming getfattr
3170 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
3172 nfs_fattr_init(info
->fattr
);
3173 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3176 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3177 struct nfs_fsinfo
*info
)
3179 struct nfs4_exception exception
= { };
3182 err
= _nfs4_lookup_root(server
, fhandle
, info
);
3183 trace_nfs4_lookup_root(server
, fhandle
, info
->fattr
, err
);
3186 case -NFS4ERR_WRONGSEC
:
3189 err
= nfs4_handle_exception(server
, err
, &exception
);
3191 } while (exception
.retry
);
3196 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3197 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
3199 struct rpc_auth_create_args auth_args
= {
3200 .pseudoflavor
= flavor
,
3202 struct rpc_auth
*auth
;
3205 auth
= rpcauth_create(&auth_args
, server
->client
);
3210 ret
= nfs4_lookup_root(server
, fhandle
, info
);
3216 * Retry pseudoroot lookup with various security flavors. We do this when:
3218 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3219 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3221 * Returns zero on success, or a negative NFS4ERR value, or a
3222 * negative errno value.
3224 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3225 struct nfs_fsinfo
*info
)
3227 /* Per 3530bis 15.33.5 */
3228 static const rpc_authflavor_t flav_array
[] = {
3232 RPC_AUTH_UNIX
, /* courtesy */
3235 int status
= -EPERM
;
3238 if (server
->auth_info
.flavor_len
> 0) {
3239 /* try each flavor specified by user */
3240 for (i
= 0; i
< server
->auth_info
.flavor_len
; i
++) {
3241 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3242 server
->auth_info
.flavors
[i
]);
3243 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3248 /* no flavors specified by user, try default list */
3249 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
3250 status
= nfs4_lookup_root_sec(server
, fhandle
, info
,
3252 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
3259 * -EACCESS could mean that the user doesn't have correct permissions
3260 * to access the mount. It could also mean that we tried to mount
3261 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3262 * existing mount programs don't handle -EACCES very well so it should
3263 * be mapped to -EPERM instead.
3265 if (status
== -EACCES
)
3270 static int nfs4_do_find_root_sec(struct nfs_server
*server
,
3271 struct nfs_fh
*fhandle
, struct nfs_fsinfo
*info
)
3273 int mv
= server
->nfs_client
->cl_minorversion
;
3274 return nfs_v4_minor_ops
[mv
]->find_root_sec(server
, fhandle
, info
);
3278 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3279 * @server: initialized nfs_server handle
3280 * @fhandle: we fill in the pseudo-fs root file handle
3281 * @info: we fill in an FSINFO struct
3282 * @auth_probe: probe the auth flavours
3284 * Returns zero on success, or a negative errno.
3286 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3287 struct nfs_fsinfo
*info
,
3293 status
= nfs4_lookup_root(server
, fhandle
, info
);
3295 if (auth_probe
|| status
== NFS4ERR_WRONGSEC
)
3296 status
= nfs4_do_find_root_sec(server
, fhandle
, info
);
3299 status
= nfs4_server_capabilities(server
, fhandle
);
3301 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
3303 return nfs4_map_errors(status
);
3306 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
3307 struct nfs_fsinfo
*info
)
3310 struct nfs_fattr
*fattr
= info
->fattr
;
3311 struct nfs4_label
*label
= NULL
;
3313 error
= nfs4_server_capabilities(server
, mntfh
);
3315 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
3319 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3321 return PTR_ERR(label
);
3323 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
3325 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
3326 goto err_free_label
;
3329 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
3330 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
3331 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
3334 nfs4_label_free(label
);
3340 * Get locations and (maybe) other attributes of a referral.
3341 * Note that we'll actually follow the referral later when
3342 * we detect fsid mismatch in inode revalidation
3344 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
3345 const struct qstr
*name
, struct nfs_fattr
*fattr
,
3346 struct nfs_fh
*fhandle
)
3348 int status
= -ENOMEM
;
3349 struct page
*page
= NULL
;
3350 struct nfs4_fs_locations
*locations
= NULL
;
3352 page
= alloc_page(GFP_KERNEL
);
3355 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
3356 if (locations
== NULL
)
3359 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
3364 * If the fsid didn't change, this is a migration event, not a
3365 * referral. Cause us to drop into the exception handler, which
3366 * will kick off migration recovery.
3368 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
3369 dprintk("%s: server did not return a different fsid for"
3370 " a referral at %s\n", __func__
, name
->name
);
3371 status
= -NFS4ERR_MOVED
;
3374 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3375 nfs_fixup_referral_attributes(&locations
->fattr
);
3377 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3378 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
3379 memset(fhandle
, 0, sizeof(struct nfs_fh
));
3387 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3388 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3390 struct nfs4_getattr_arg args
= {
3392 .bitmask
= server
->attr_bitmask
,
3394 struct nfs4_getattr_res res
= {
3399 struct rpc_message msg
= {
3400 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
3405 args
.bitmask
= nfs4_bitmask(server
, label
);
3407 nfs_fattr_init(fattr
);
3408 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3411 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3412 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3414 struct nfs4_exception exception
= { };
3417 err
= _nfs4_proc_getattr(server
, fhandle
, fattr
, label
);
3418 trace_nfs4_getattr(server
, fhandle
, fattr
, err
);
3419 err
= nfs4_handle_exception(server
, err
,
3421 } while (exception
.retry
);
3426 * The file is not closed if it is opened due to the a request to change
3427 * the size of the file. The open call will not be needed once the
3428 * VFS layer lookup-intents are implemented.
3430 * Close is called when the inode is destroyed.
3431 * If we haven't opened the file for O_WRONLY, we
3432 * need to in the size_change case to obtain a stateid.
3435 * Because OPEN is always done by name in nfsv4, it is
3436 * possible that we opened a different file by the same
3437 * name. We can recognize this race condition, but we
3438 * can't do anything about it besides returning an error.
3440 * This will be fixed with VFS changes (lookup-intent).
3443 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
3444 struct iattr
*sattr
)
3446 struct inode
*inode
= d_inode(dentry
);
3447 struct rpc_cred
*cred
= NULL
;
3448 struct nfs4_state
*state
= NULL
;
3449 struct nfs4_label
*label
= NULL
;
3452 if (pnfs_ld_layoutret_on_setattr(inode
) &&
3453 sattr
->ia_valid
& ATTR_SIZE
&&
3454 sattr
->ia_size
< i_size_read(inode
))
3455 pnfs_commit_and_return_layout(inode
);
3457 nfs_fattr_init(fattr
);
3459 /* Deal with open(O_TRUNC) */
3460 if (sattr
->ia_valid
& ATTR_OPEN
)
3461 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
);
3463 /* Optimization: if the end result is no change, don't RPC */
3464 if ((sattr
->ia_valid
& ~(ATTR_FILE
|ATTR_OPEN
)) == 0)
3467 /* Search for an existing open(O_WRITE) file */
3468 if (sattr
->ia_valid
& ATTR_FILE
) {
3469 struct nfs_open_context
*ctx
;
3471 ctx
= nfs_file_open_context(sattr
->ia_file
);
3478 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
3480 return PTR_ERR(label
);
3482 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
3484 nfs_setattr_update_inode(inode
, sattr
, fattr
);
3485 nfs_setsecurity(inode
, fattr
, label
);
3487 nfs4_label_free(label
);
3491 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
3492 const struct qstr
*name
, struct nfs_fh
*fhandle
,
3493 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3495 struct nfs_server
*server
= NFS_SERVER(dir
);
3497 struct nfs4_lookup_arg args
= {
3498 .bitmask
= server
->attr_bitmask
,
3499 .dir_fh
= NFS_FH(dir
),
3502 struct nfs4_lookup_res res
= {
3508 struct rpc_message msg
= {
3509 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
3514 args
.bitmask
= nfs4_bitmask(server
, label
);
3516 nfs_fattr_init(fattr
);
3518 dprintk("NFS call lookup %s\n", name
->name
);
3519 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3520 dprintk("NFS reply lookup: %d\n", status
);
3524 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3526 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3527 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3528 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3532 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3533 struct qstr
*name
, struct nfs_fh
*fhandle
,
3534 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3536 struct nfs4_exception exception
= { };
3537 struct rpc_clnt
*client
= *clnt
;
3540 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3541 trace_nfs4_lookup(dir
, name
, err
);
3543 case -NFS4ERR_BADNAME
:
3546 case -NFS4ERR_MOVED
:
3547 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3548 if (err
== -NFS4ERR_MOVED
)
3549 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3551 case -NFS4ERR_WRONGSEC
:
3553 if (client
!= *clnt
)
3555 client
= nfs4_negotiate_security(client
, dir
, name
);
3557 return PTR_ERR(client
);
3559 exception
.retry
= 1;
3562 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3564 } while (exception
.retry
);
3569 else if (client
!= *clnt
)
3570 rpc_shutdown_client(client
);
3575 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
3576 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3577 struct nfs4_label
*label
)
3580 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3582 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3583 if (client
!= NFS_CLIENT(dir
)) {
3584 rpc_shutdown_client(client
);
3585 nfs_fixup_secinfo_attributes(fattr
);
3591 nfs4_proc_lookup_mountpoint(struct inode
*dir
, struct qstr
*name
,
3592 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3594 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3597 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3599 return ERR_PTR(status
);
3600 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3603 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3605 struct nfs_server
*server
= NFS_SERVER(inode
);
3606 struct nfs4_accessargs args
= {
3607 .fh
= NFS_FH(inode
),
3608 .bitmask
= server
->cache_consistency_bitmask
,
3610 struct nfs4_accessres res
= {
3613 struct rpc_message msg
= {
3614 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3617 .rpc_cred
= entry
->cred
,
3619 int mode
= entry
->mask
;
3623 * Determine which access bits we want to ask for...
3625 if (mode
& MAY_READ
)
3626 args
.access
|= NFS4_ACCESS_READ
;
3627 if (S_ISDIR(inode
->i_mode
)) {
3628 if (mode
& MAY_WRITE
)
3629 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3630 if (mode
& MAY_EXEC
)
3631 args
.access
|= NFS4_ACCESS_LOOKUP
;
3633 if (mode
& MAY_WRITE
)
3634 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3635 if (mode
& MAY_EXEC
)
3636 args
.access
|= NFS4_ACCESS_EXECUTE
;
3639 res
.fattr
= nfs_alloc_fattr();
3640 if (res
.fattr
== NULL
)
3643 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3645 nfs_access_set_mask(entry
, res
.access
);
3646 nfs_refresh_inode(inode
, res
.fattr
);
3648 nfs_free_fattr(res
.fattr
);
3652 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3654 struct nfs4_exception exception
= { };
3657 err
= _nfs4_proc_access(inode
, entry
);
3658 trace_nfs4_access(inode
, err
);
3659 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3661 } while (exception
.retry
);
3666 * TODO: For the time being, we don't try to get any attributes
3667 * along with any of the zero-copy operations READ, READDIR,
3670 * In the case of the first three, we want to put the GETATTR
3671 * after the read-type operation -- this is because it is hard
3672 * to predict the length of a GETATTR response in v4, and thus
3673 * align the READ data correctly. This means that the GETATTR
3674 * may end up partially falling into the page cache, and we should
3675 * shift it into the 'tail' of the xdr_buf before processing.
3676 * To do this efficiently, we need to know the total length
3677 * of data received, which doesn't seem to be available outside
3680 * In the case of WRITE, we also want to put the GETATTR after
3681 * the operation -- in this case because we want to make sure
3682 * we get the post-operation mtime and size.
3684 * Both of these changes to the XDR layer would in fact be quite
3685 * minor, but I decided to leave them for a subsequent patch.
3687 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3688 unsigned int pgbase
, unsigned int pglen
)
3690 struct nfs4_readlink args
= {
3691 .fh
= NFS_FH(inode
),
3696 struct nfs4_readlink_res res
;
3697 struct rpc_message msg
= {
3698 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3703 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3706 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3707 unsigned int pgbase
, unsigned int pglen
)
3709 struct nfs4_exception exception
= { };
3712 err
= _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
);
3713 trace_nfs4_readlink(inode
, err
);
3714 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
3716 } while (exception
.retry
);
3721 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3724 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3727 struct nfs4_label l
, *ilabel
= NULL
;
3728 struct nfs_open_context
*ctx
;
3729 struct nfs4_state
*state
;
3732 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
3734 return PTR_ERR(ctx
);
3736 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3738 sattr
->ia_mode
&= ~current_umask();
3739 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
, NULL
);
3740 if (IS_ERR(state
)) {
3741 status
= PTR_ERR(state
);
3745 nfs4_label_release_security(ilabel
);
3746 put_nfs_open_context(ctx
);
3750 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3752 struct nfs_server
*server
= NFS_SERVER(dir
);
3753 struct nfs_removeargs args
= {
3757 struct nfs_removeres res
= {
3760 struct rpc_message msg
= {
3761 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3767 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3769 update_changeattr(dir
, &res
.cinfo
);
3773 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3775 struct nfs4_exception exception
= { };
3778 err
= _nfs4_proc_remove(dir
, name
);
3779 trace_nfs4_remove(dir
, name
, err
);
3780 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
3782 } while (exception
.retry
);
3786 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
3788 struct nfs_server
*server
= NFS_SERVER(dir
);
3789 struct nfs_removeargs
*args
= msg
->rpc_argp
;
3790 struct nfs_removeres
*res
= msg
->rpc_resp
;
3792 res
->server
= server
;
3793 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
3794 nfs4_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
3796 nfs_fattr_init(res
->dir_attr
);
3799 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
3801 nfs4_setup_sequence(NFS_SB(data
->dentry
->d_sb
),
3802 &data
->args
.seq_args
,
3807 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
3809 struct nfs_unlinkdata
*data
= task
->tk_calldata
;
3810 struct nfs_removeres
*res
= &data
->res
;
3812 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3814 if (nfs4_async_handle_error(task
, res
->server
, NULL
,
3815 &data
->timeout
) == -EAGAIN
)
3817 update_changeattr(dir
, &res
->cinfo
);
3821 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
3823 struct nfs_server
*server
= NFS_SERVER(dir
);
3824 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
3825 struct nfs_renameres
*res
= msg
->rpc_resp
;
3827 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
3828 res
->server
= server
;
3829 nfs4_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
3832 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
3834 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
3835 &data
->args
.seq_args
,
3840 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
3841 struct inode
*new_dir
)
3843 struct nfs_renamedata
*data
= task
->tk_calldata
;
3844 struct nfs_renameres
*res
= &data
->res
;
3846 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3848 if (nfs4_async_handle_error(task
, res
->server
, NULL
, &data
->timeout
) == -EAGAIN
)
3851 update_changeattr(old_dir
, &res
->old_cinfo
);
3852 update_changeattr(new_dir
, &res
->new_cinfo
);
3856 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3858 struct nfs_server
*server
= NFS_SERVER(inode
);
3859 struct nfs4_link_arg arg
= {
3860 .fh
= NFS_FH(inode
),
3861 .dir_fh
= NFS_FH(dir
),
3863 .bitmask
= server
->attr_bitmask
,
3865 struct nfs4_link_res res
= {
3869 struct rpc_message msg
= {
3870 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
3874 int status
= -ENOMEM
;
3876 res
.fattr
= nfs_alloc_fattr();
3877 if (res
.fattr
== NULL
)
3880 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3881 if (IS_ERR(res
.label
)) {
3882 status
= PTR_ERR(res
.label
);
3885 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
3887 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3889 update_changeattr(dir
, &res
.cinfo
);
3890 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
3892 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
3896 nfs4_label_free(res
.label
);
3899 nfs_free_fattr(res
.fattr
);
3903 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3905 struct nfs4_exception exception
= { };
3908 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3909 _nfs4_proc_link(inode
, dir
, name
),
3911 } while (exception
.retry
);
3915 struct nfs4_createdata
{
3916 struct rpc_message msg
;
3917 struct nfs4_create_arg arg
;
3918 struct nfs4_create_res res
;
3920 struct nfs_fattr fattr
;
3921 struct nfs4_label
*label
;
3924 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
3925 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
3927 struct nfs4_createdata
*data
;
3929 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3931 struct nfs_server
*server
= NFS_SERVER(dir
);
3933 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3934 if (IS_ERR(data
->label
))
3937 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
3938 data
->msg
.rpc_argp
= &data
->arg
;
3939 data
->msg
.rpc_resp
= &data
->res
;
3940 data
->arg
.dir_fh
= NFS_FH(dir
);
3941 data
->arg
.server
= server
;
3942 data
->arg
.name
= name
;
3943 data
->arg
.attrs
= sattr
;
3944 data
->arg
.ftype
= ftype
;
3945 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
3946 data
->res
.server
= server
;
3947 data
->res
.fh
= &data
->fh
;
3948 data
->res
.fattr
= &data
->fattr
;
3949 data
->res
.label
= data
->label
;
3950 nfs_fattr_init(data
->res
.fattr
);
3958 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
3960 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
3961 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
3963 update_changeattr(dir
, &data
->res
.dir_cinfo
);
3964 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
3969 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
3971 nfs4_label_free(data
->label
);
3975 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3976 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
3977 struct nfs4_label
*label
)
3979 struct nfs4_createdata
*data
;
3980 int status
= -ENAMETOOLONG
;
3982 if (len
> NFS4_MAXPATHLEN
)
3986 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
3990 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
3991 data
->arg
.u
.symlink
.pages
= &page
;
3992 data
->arg
.u
.symlink
.len
= len
;
3993 data
->arg
.label
= label
;
3995 status
= nfs4_do_create(dir
, dentry
, data
);
3997 nfs4_free_createdata(data
);
4002 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
4003 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
4005 struct nfs4_exception exception
= { };
4006 struct nfs4_label l
, *label
= NULL
;
4009 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4012 err
= _nfs4_proc_symlink(dir
, dentry
, page
, len
, sattr
, label
);
4013 trace_nfs4_symlink(dir
, &dentry
->d_name
, err
);
4014 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4016 } while (exception
.retry
);
4018 nfs4_label_release_security(label
);
4022 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4023 struct iattr
*sattr
, struct nfs4_label
*label
)
4025 struct nfs4_createdata
*data
;
4026 int status
= -ENOMEM
;
4028 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
4032 data
->arg
.label
= label
;
4033 status
= nfs4_do_create(dir
, dentry
, data
);
4035 nfs4_free_createdata(data
);
4040 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
4041 struct iattr
*sattr
)
4043 struct nfs4_exception exception
= { };
4044 struct nfs4_label l
, *label
= NULL
;
4047 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4049 sattr
->ia_mode
&= ~current_umask();
4051 err
= _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
);
4052 trace_nfs4_mkdir(dir
, &dentry
->d_name
, err
);
4053 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4055 } while (exception
.retry
);
4056 nfs4_label_release_security(label
);
4061 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4062 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4064 struct inode
*dir
= d_inode(dentry
);
4065 struct nfs4_readdir_arg args
= {
4070 .bitmask
= NFS_SERVER(d_inode(dentry
))->attr_bitmask
,
4073 struct nfs4_readdir_res res
;
4074 struct rpc_message msg
= {
4075 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
4082 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__
,
4084 (unsigned long long)cookie
);
4085 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
4086 res
.pgbase
= args
.pgbase
;
4087 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4089 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
4090 status
+= args
.pgbase
;
4093 nfs_invalidate_atime(dir
);
4095 dprintk("%s: returns %d\n", __func__
, status
);
4099 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
4100 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
4102 struct nfs4_exception exception
= { };
4105 err
= _nfs4_proc_readdir(dentry
, cred
, cookie
,
4106 pages
, count
, plus
);
4107 trace_nfs4_readdir(d_inode(dentry
), err
);
4108 err
= nfs4_handle_exception(NFS_SERVER(d_inode(dentry
)), err
,
4110 } while (exception
.retry
);
4114 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4115 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
4117 struct nfs4_createdata
*data
;
4118 int mode
= sattr
->ia_mode
;
4119 int status
= -ENOMEM
;
4121 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
4126 data
->arg
.ftype
= NF4FIFO
;
4127 else if (S_ISBLK(mode
)) {
4128 data
->arg
.ftype
= NF4BLK
;
4129 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4130 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4132 else if (S_ISCHR(mode
)) {
4133 data
->arg
.ftype
= NF4CHR
;
4134 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
4135 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
4136 } else if (!S_ISSOCK(mode
)) {
4141 data
->arg
.label
= label
;
4142 status
= nfs4_do_create(dir
, dentry
, data
);
4144 nfs4_free_createdata(data
);
4149 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
4150 struct iattr
*sattr
, dev_t rdev
)
4152 struct nfs4_exception exception
= { };
4153 struct nfs4_label l
, *label
= NULL
;
4156 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
4158 sattr
->ia_mode
&= ~current_umask();
4160 err
= _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
);
4161 trace_nfs4_mknod(dir
, &dentry
->d_name
, err
);
4162 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
4164 } while (exception
.retry
);
4166 nfs4_label_release_security(label
);
4171 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4172 struct nfs_fsstat
*fsstat
)
4174 struct nfs4_statfs_arg args
= {
4176 .bitmask
= server
->attr_bitmask
,
4178 struct nfs4_statfs_res res
= {
4181 struct rpc_message msg
= {
4182 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
4187 nfs_fattr_init(fsstat
->fattr
);
4188 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4191 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
4193 struct nfs4_exception exception
= { };
4196 err
= nfs4_handle_exception(server
,
4197 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
4199 } while (exception
.retry
);
4203 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4204 struct nfs_fsinfo
*fsinfo
)
4206 struct nfs4_fsinfo_arg args
= {
4208 .bitmask
= server
->attr_bitmask
,
4210 struct nfs4_fsinfo_res res
= {
4213 struct rpc_message msg
= {
4214 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
4219 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4222 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4224 struct nfs4_exception exception
= { };
4225 unsigned long now
= jiffies
;
4229 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4230 trace_nfs4_fsinfo(server
, fhandle
, fsinfo
->fattr
, err
);
4232 struct nfs_client
*clp
= server
->nfs_client
;
4234 spin_lock(&clp
->cl_lock
);
4235 clp
->cl_lease_time
= fsinfo
->lease_time
* HZ
;
4236 clp
->cl_last_renewal
= now
;
4237 spin_unlock(&clp
->cl_lock
);
4240 err
= nfs4_handle_exception(server
, err
, &exception
);
4241 } while (exception
.retry
);
4245 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
4249 nfs_fattr_init(fsinfo
->fattr
);
4250 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
4252 /* block layout checks this! */
4253 server
->pnfs_blksize
= fsinfo
->blksize
;
4254 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
->layouttype
);
4260 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4261 struct nfs_pathconf
*pathconf
)
4263 struct nfs4_pathconf_arg args
= {
4265 .bitmask
= server
->attr_bitmask
,
4267 struct nfs4_pathconf_res res
= {
4268 .pathconf
= pathconf
,
4270 struct rpc_message msg
= {
4271 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
4276 /* None of the pathconf attributes are mandatory to implement */
4277 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
4278 memset(pathconf
, 0, sizeof(*pathconf
));
4282 nfs_fattr_init(pathconf
->fattr
);
4283 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4286 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
4287 struct nfs_pathconf
*pathconf
)
4289 struct nfs4_exception exception
= { };
4293 err
= nfs4_handle_exception(server
,
4294 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
4296 } while (exception
.retry
);
4300 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
4301 const struct nfs_open_context
*ctx
,
4302 const struct nfs_lock_context
*l_ctx
,
4305 const struct nfs_lockowner
*lockowner
= NULL
;
4308 lockowner
= &l_ctx
->lockowner
;
4309 return nfs4_select_rw_stateid(ctx
->state
, fmode
, lockowner
, stateid
, NULL
);
4311 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
4313 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
4314 const struct nfs_open_context
*ctx
,
4315 const struct nfs_lock_context
*l_ctx
,
4318 nfs4_stateid current_stateid
;
4320 /* If the current stateid represents a lost lock, then exit */
4321 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
) == -EIO
)
4323 return nfs4_stateid_match(stateid
, ¤t_stateid
);
4326 static bool nfs4_error_stateid_expired(int err
)
4329 case -NFS4ERR_DELEG_REVOKED
:
4330 case -NFS4ERR_ADMIN_REVOKED
:
4331 case -NFS4ERR_BAD_STATEID
:
4332 case -NFS4ERR_STALE_STATEID
:
4333 case -NFS4ERR_OLD_STATEID
:
4334 case -NFS4ERR_OPENMODE
:
4335 case -NFS4ERR_EXPIRED
:
4341 void __nfs4_read_done_cb(struct nfs_pgio_header
*hdr
)
4343 nfs_invalidate_atime(hdr
->inode
);
4346 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4348 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4350 trace_nfs4_read(hdr
, task
->tk_status
);
4351 if (nfs4_async_handle_error(task
, server
,
4352 hdr
->args
.context
->state
,
4354 rpc_restart_call_prepare(task
);
4358 __nfs4_read_done_cb(hdr
);
4359 if (task
->tk_status
> 0)
4360 renew_lease(server
, hdr
->timestamp
);
4364 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
4365 struct nfs_pgio_args
*args
)
4368 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4369 nfs4_stateid_is_current(&args
->stateid
,
4374 rpc_restart_call_prepare(task
);
4378 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4381 dprintk("--> %s\n", __func__
);
4383 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4385 if (nfs4_read_stateid_changed(task
, &hdr
->args
))
4387 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4388 nfs4_read_done_cb(task
, hdr
);
4391 static void nfs4_proc_read_setup(struct nfs_pgio_header
*hdr
,
4392 struct rpc_message
*msg
)
4394 hdr
->timestamp
= jiffies
;
4395 hdr
->pgio_done_cb
= nfs4_read_done_cb
;
4396 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
4397 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 0);
4400 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task
*task
,
4401 struct nfs_pgio_header
*hdr
)
4403 if (nfs4_setup_sequence(NFS_SERVER(hdr
->inode
),
4404 &hdr
->args
.seq_args
,
4408 if (nfs4_set_rw_stateid(&hdr
->args
.stateid
, hdr
->args
.context
,
4409 hdr
->args
.lock_context
,
4410 hdr
->rw_ops
->rw_mode
) == -EIO
)
4412 if (unlikely(test_bit(NFS_CONTEXT_BAD
, &hdr
->args
.context
->flags
)))
4417 static int nfs4_write_done_cb(struct rpc_task
*task
,
4418 struct nfs_pgio_header
*hdr
)
4420 struct inode
*inode
= hdr
->inode
;
4422 trace_nfs4_write(hdr
, task
->tk_status
);
4423 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4424 hdr
->args
.context
->state
,
4426 rpc_restart_call_prepare(task
);
4429 if (task
->tk_status
>= 0) {
4430 renew_lease(NFS_SERVER(inode
), hdr
->timestamp
);
4431 nfs_writeback_update_inode(hdr
);
4436 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
4437 struct nfs_pgio_args
*args
)
4440 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
4441 nfs4_stateid_is_current(&args
->stateid
,
4446 rpc_restart_call_prepare(task
);
4450 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_pgio_header
*hdr
)
4452 if (!nfs4_sequence_done(task
, &hdr
->res
.seq_res
))
4454 if (nfs4_write_stateid_changed(task
, &hdr
->args
))
4456 return hdr
->pgio_done_cb
? hdr
->pgio_done_cb(task
, hdr
) :
4457 nfs4_write_done_cb(task
, hdr
);
4461 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header
*hdr
)
4463 /* Don't request attributes for pNFS or O_DIRECT writes */
4464 if (hdr
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
4466 /* Otherwise, request attributes if and only if we don't hold
4469 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
4472 static void nfs4_proc_write_setup(struct nfs_pgio_header
*hdr
,
4473 struct rpc_message
*msg
)
4475 struct nfs_server
*server
= NFS_SERVER(hdr
->inode
);
4477 if (!nfs4_write_need_cache_consistency_data(hdr
)) {
4478 hdr
->args
.bitmask
= NULL
;
4479 hdr
->res
.fattr
= NULL
;
4481 hdr
->args
.bitmask
= server
->cache_consistency_bitmask
;
4483 if (!hdr
->pgio_done_cb
)
4484 hdr
->pgio_done_cb
= nfs4_write_done_cb
;
4485 hdr
->res
.server
= server
;
4486 hdr
->timestamp
= jiffies
;
4488 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
4489 nfs4_init_sequence(&hdr
->args
.seq_args
, &hdr
->res
.seq_res
, 1);
4492 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4494 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4495 &data
->args
.seq_args
,
4500 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4502 struct inode
*inode
= data
->inode
;
4504 trace_nfs4_commit(data
, task
->tk_status
);
4505 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
),
4506 NULL
, NULL
) == -EAGAIN
) {
4507 rpc_restart_call_prepare(task
);
4513 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4515 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4517 return data
->commit_done_cb(task
, data
);
4520 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4522 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4524 if (data
->commit_done_cb
== NULL
)
4525 data
->commit_done_cb
= nfs4_commit_done_cb
;
4526 data
->res
.server
= server
;
4527 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4528 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4531 struct nfs4_renewdata
{
4532 struct nfs_client
*client
;
4533 unsigned long timestamp
;
4537 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4538 * standalone procedure for queueing an asynchronous RENEW.
4540 static void nfs4_renew_release(void *calldata
)
4542 struct nfs4_renewdata
*data
= calldata
;
4543 struct nfs_client
*clp
= data
->client
;
4545 if (atomic_read(&clp
->cl_count
) > 1)
4546 nfs4_schedule_state_renewal(clp
);
4547 nfs_put_client(clp
);
4551 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4553 struct nfs4_renewdata
*data
= calldata
;
4554 struct nfs_client
*clp
= data
->client
;
4555 unsigned long timestamp
= data
->timestamp
;
4557 trace_nfs4_renew_async(clp
, task
->tk_status
);
4558 switch (task
->tk_status
) {
4561 case -NFS4ERR_LEASE_MOVED
:
4562 nfs4_schedule_lease_moved_recovery(clp
);
4565 /* Unless we're shutting down, schedule state recovery! */
4566 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4568 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4569 nfs4_schedule_lease_recovery(clp
);
4572 nfs4_schedule_path_down_recovery(clp
);
4574 do_renew_lease(clp
, timestamp
);
4577 static const struct rpc_call_ops nfs4_renew_ops
= {
4578 .rpc_call_done
= nfs4_renew_done
,
4579 .rpc_release
= nfs4_renew_release
,
4582 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4584 struct rpc_message msg
= {
4585 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4589 struct nfs4_renewdata
*data
;
4591 if (renew_flags
== 0)
4593 if (!atomic_inc_not_zero(&clp
->cl_count
))
4595 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4599 data
->timestamp
= jiffies
;
4600 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4601 &nfs4_renew_ops
, data
);
4604 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4606 struct rpc_message msg
= {
4607 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4611 unsigned long now
= jiffies
;
4614 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4617 do_renew_lease(clp
, now
);
4621 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4623 return server
->caps
& NFS_CAP_ACLS
;
4626 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4627 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4630 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4632 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4633 struct page
**pages
)
4635 struct page
*newpage
, **spages
;
4641 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4642 newpage
= alloc_page(GFP_KERNEL
);
4644 if (newpage
== NULL
)
4646 memcpy(page_address(newpage
), buf
, len
);
4651 } while (buflen
!= 0);
4657 __free_page(spages
[rc
-1]);
4661 struct nfs4_cached_acl
{
4667 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4669 struct nfs_inode
*nfsi
= NFS_I(inode
);
4671 spin_lock(&inode
->i_lock
);
4672 kfree(nfsi
->nfs4_acl
);
4673 nfsi
->nfs4_acl
= acl
;
4674 spin_unlock(&inode
->i_lock
);
4677 static void nfs4_zap_acl_attr(struct inode
*inode
)
4679 nfs4_set_cached_acl(inode
, NULL
);
4682 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4684 struct nfs_inode
*nfsi
= NFS_I(inode
);
4685 struct nfs4_cached_acl
*acl
;
4688 spin_lock(&inode
->i_lock
);
4689 acl
= nfsi
->nfs4_acl
;
4692 if (buf
== NULL
) /* user is just asking for length */
4694 if (acl
->cached
== 0)
4696 ret
= -ERANGE
; /* see getxattr(2) man page */
4697 if (acl
->len
> buflen
)
4699 memcpy(buf
, acl
->data
, acl
->len
);
4703 spin_unlock(&inode
->i_lock
);
4707 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4709 struct nfs4_cached_acl
*acl
;
4710 size_t buflen
= sizeof(*acl
) + acl_len
;
4712 if (buflen
<= PAGE_SIZE
) {
4713 acl
= kmalloc(buflen
, GFP_KERNEL
);
4717 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4719 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4726 nfs4_set_cached_acl(inode
, acl
);
4730 * The getxattr API returns the required buffer length when called with a
4731 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4732 * the required buf. On a NULL buf, we send a page of data to the server
4733 * guessing that the ACL request can be serviced by a page. If so, we cache
4734 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4735 * the cache. If not so, we throw away the page, and cache the required
4736 * length. The next getxattr call will then produce another round trip to
4737 * the server, this time with the input buf of the required size.
4739 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4741 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4742 struct nfs_getaclargs args
= {
4743 .fh
= NFS_FH(inode
),
4747 struct nfs_getaclres res
= {
4750 struct rpc_message msg
= {
4751 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4755 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4756 int ret
= -ENOMEM
, i
;
4758 /* As long as we're doing a round trip to the server anyway,
4759 * let's be prepared for a page of acl data. */
4762 if (npages
> ARRAY_SIZE(pages
))
4765 for (i
= 0; i
< npages
; i
++) {
4766 pages
[i
] = alloc_page(GFP_KERNEL
);
4771 /* for decoding across pages */
4772 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
4773 if (!res
.acl_scratch
)
4776 args
.acl_len
= npages
* PAGE_SIZE
;
4778 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4779 __func__
, buf
, buflen
, npages
, args
.acl_len
);
4780 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
4781 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4785 /* Handle the case where the passed-in buffer is too short */
4786 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
4787 /* Did the user only issue a request for the acl length? */
4793 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
4795 if (res
.acl_len
> buflen
) {
4799 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
4804 for (i
= 0; i
< npages
; i
++)
4806 __free_page(pages
[i
]);
4807 if (res
.acl_scratch
)
4808 __free_page(res
.acl_scratch
);
4812 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4814 struct nfs4_exception exception
= { };
4817 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
4818 trace_nfs4_get_acl(inode
, ret
);
4821 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
4822 } while (exception
.retry
);
4826 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
4828 struct nfs_server
*server
= NFS_SERVER(inode
);
4831 if (!nfs4_server_supports_acls(server
))
4833 ret
= nfs_revalidate_inode(server
, inode
);
4836 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
4837 nfs_zap_acl_cache(inode
);
4838 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
4840 /* -ENOENT is returned if there is no ACL or if there is an ACL
4841 * but no cached acl data, just the acl length */
4843 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
4846 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4848 struct nfs_server
*server
= NFS_SERVER(inode
);
4849 struct page
*pages
[NFS4ACL_MAXPAGES
];
4850 struct nfs_setaclargs arg
= {
4851 .fh
= NFS_FH(inode
),
4855 struct nfs_setaclres res
;
4856 struct rpc_message msg
= {
4857 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
4861 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4864 if (!nfs4_server_supports_acls(server
))
4866 if (npages
> ARRAY_SIZE(pages
))
4868 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
);
4871 nfs4_inode_return_delegation(inode
);
4872 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4875 * Free each page after tx, so the only ref left is
4876 * held by the network stack
4879 put_page(pages
[i
-1]);
4882 * Acl update can result in inode attribute update.
4883 * so mark the attribute cache invalid.
4885 spin_lock(&inode
->i_lock
);
4886 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
4887 spin_unlock(&inode
->i_lock
);
4888 nfs_access_zap_cache(inode
);
4889 nfs_zap_acl_cache(inode
);
4893 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4895 struct nfs4_exception exception
= { };
4898 err
= __nfs4_proc_set_acl(inode
, buf
, buflen
);
4899 trace_nfs4_set_acl(inode
, err
);
4900 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4902 } while (exception
.retry
);
4906 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4907 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
4910 struct nfs_server
*server
= NFS_SERVER(inode
);
4911 struct nfs_fattr fattr
;
4912 struct nfs4_label label
= {0, 0, buflen
, buf
};
4914 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4915 struct nfs4_getattr_arg arg
= {
4916 .fh
= NFS_FH(inode
),
4919 struct nfs4_getattr_res res
= {
4924 struct rpc_message msg
= {
4925 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
4931 nfs_fattr_init(&fattr
);
4933 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 0);
4936 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
4938 if (buflen
< label
.len
)
4943 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
4946 struct nfs4_exception exception
= { };
4949 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4953 err
= _nfs4_get_security_label(inode
, buf
, buflen
);
4954 trace_nfs4_get_security_label(inode
, err
);
4955 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
4957 } while (exception
.retry
);
4961 static int _nfs4_do_set_security_label(struct inode
*inode
,
4962 struct nfs4_label
*ilabel
,
4963 struct nfs_fattr
*fattr
,
4964 struct nfs4_label
*olabel
)
4967 struct iattr sattr
= {0};
4968 struct nfs_server
*server
= NFS_SERVER(inode
);
4969 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4970 struct nfs_setattrargs arg
= {
4971 .fh
= NFS_FH(inode
),
4977 struct nfs_setattrres res
= {
4982 struct rpc_message msg
= {
4983 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
4989 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
4991 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4993 dprintk("%s failed: %d\n", __func__
, status
);
4998 static int nfs4_do_set_security_label(struct inode
*inode
,
4999 struct nfs4_label
*ilabel
,
5000 struct nfs_fattr
*fattr
,
5001 struct nfs4_label
*olabel
)
5003 struct nfs4_exception exception
= { };
5007 err
= _nfs4_do_set_security_label(inode
, ilabel
,
5009 trace_nfs4_set_security_label(inode
, err
);
5010 err
= nfs4_handle_exception(NFS_SERVER(inode
), err
,
5012 } while (exception
.retry
);
5017 nfs4_set_security_label(struct inode
*inode
, const void *buf
, size_t buflen
)
5019 struct nfs4_label ilabel
, *olabel
= NULL
;
5020 struct nfs_fattr fattr
;
5021 struct rpc_cred
*cred
;
5024 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
5027 nfs_fattr_init(&fattr
);
5031 ilabel
.label
= (char *)buf
;
5032 ilabel
.len
= buflen
;
5034 cred
= rpc_lookup_cred();
5036 return PTR_ERR(cred
);
5038 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
5039 if (IS_ERR(olabel
)) {
5040 status
= -PTR_ERR(olabel
);
5044 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
5046 nfs_setsecurity(inode
, &fattr
, olabel
);
5048 nfs4_label_free(olabel
);
5053 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5056 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
5057 nfs4_verifier
*bootverf
)
5061 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
5062 /* An impossible timestamp guarantees this value
5063 * will never match a generated boot time. */
5065 verf
[1] = cpu_to_be32(NSEC_PER_SEC
+ 1);
5067 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
5068 verf
[0] = cpu_to_be32(nn
->boot_time
.tv_sec
);
5069 verf
[1] = cpu_to_be32(nn
->boot_time
.tv_nsec
);
5071 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
5075 nfs4_init_nonuniform_client_string(struct nfs_client
*clp
)
5080 if (clp
->cl_owner_id
!= NULL
)
5084 len
= 14 + strlen(clp
->cl_ipaddr
) + 1 +
5085 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
)) +
5087 strlen(rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
)) +
5091 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5095 * Since this string is allocated at mount time, and held until the
5096 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5097 * about a memory-reclaim deadlock.
5099 str
= kmalloc(len
, GFP_KERNEL
);
5104 scnprintf(str
, len
, "Linux NFSv4.0 %s/%s %s",
5106 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
),
5107 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_PROTO
));
5110 clp
->cl_owner_id
= str
;
5115 nfs4_init_uniquifier_client_string(struct nfs_client
*clp
)
5120 len
= 10 + 10 + 1 + 10 + 1 +
5121 strlen(nfs4_client_id_uniquifier
) + 1 +
5122 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5124 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5128 * Since this string is allocated at mount time, and held until the
5129 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5130 * about a memory-reclaim deadlock.
5132 str
= kmalloc(len
, GFP_KERNEL
);
5136 scnprintf(str
, len
, "Linux NFSv%u.%u %s/%s",
5137 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5138 nfs4_client_id_uniquifier
,
5139 clp
->cl_rpcclient
->cl_nodename
);
5140 clp
->cl_owner_id
= str
;
5145 nfs4_init_uniform_client_string(struct nfs_client
*clp
)
5150 if (clp
->cl_owner_id
!= NULL
)
5153 if (nfs4_client_id_uniquifier
[0] != '\0')
5154 return nfs4_init_uniquifier_client_string(clp
);
5156 len
= 10 + 10 + 1 + 10 + 1 +
5157 strlen(clp
->cl_rpcclient
->cl_nodename
) + 1;
5159 if (len
> NFS4_OPAQUE_LIMIT
+ 1)
5163 * Since this string is allocated at mount time, and held until the
5164 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5165 * about a memory-reclaim deadlock.
5167 str
= kmalloc(len
, GFP_KERNEL
);
5171 scnprintf(str
, len
, "Linux NFSv%u.%u %s",
5172 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
5173 clp
->cl_rpcclient
->cl_nodename
);
5174 clp
->cl_owner_id
= str
;
5179 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5180 * services. Advertise one based on the address family of the
5184 nfs4_init_callback_netid(const struct nfs_client
*clp
, char *buf
, size_t len
)
5186 if (strchr(clp
->cl_ipaddr
, ':') != NULL
)
5187 return scnprintf(buf
, len
, "tcp6");
5189 return scnprintf(buf
, len
, "tcp");
5192 static void nfs4_setclientid_done(struct rpc_task
*task
, void *calldata
)
5194 struct nfs4_setclientid
*sc
= calldata
;
5196 if (task
->tk_status
== 0)
5197 sc
->sc_cred
= get_rpccred(task
->tk_rqstp
->rq_cred
);
5200 static const struct rpc_call_ops nfs4_setclientid_ops
= {
5201 .rpc_call_done
= nfs4_setclientid_done
,
5205 * nfs4_proc_setclientid - Negotiate client ID
5206 * @clp: state data structure
5207 * @program: RPC program for NFSv4 callback service
5208 * @port: IP port number for NFS4 callback service
5209 * @cred: RPC credential to use for this call
5210 * @res: where to place the result
5212 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5214 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
5215 unsigned short port
, struct rpc_cred
*cred
,
5216 struct nfs4_setclientid_res
*res
)
5218 nfs4_verifier sc_verifier
;
5219 struct nfs4_setclientid setclientid
= {
5220 .sc_verifier
= &sc_verifier
,
5224 struct rpc_message msg
= {
5225 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
5226 .rpc_argp
= &setclientid
,
5230 struct rpc_task
*task
;
5231 struct rpc_task_setup task_setup_data
= {
5232 .rpc_client
= clp
->cl_rpcclient
,
5233 .rpc_message
= &msg
,
5234 .callback_ops
= &nfs4_setclientid_ops
,
5235 .callback_data
= &setclientid
,
5236 .flags
= RPC_TASK_TIMEOUT
,
5240 /* nfs_client_id4 */
5241 nfs4_init_boot_verifier(clp
, &sc_verifier
);
5243 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
5244 status
= nfs4_init_uniform_client_string(clp
);
5246 status
= nfs4_init_nonuniform_client_string(clp
);
5252 setclientid
.sc_netid_len
=
5253 nfs4_init_callback_netid(clp
,
5254 setclientid
.sc_netid
,
5255 sizeof(setclientid
.sc_netid
));
5256 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
5257 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
5258 clp
->cl_ipaddr
, port
>> 8, port
& 255);
5260 dprintk("NFS call setclientid auth=%s, '%s'\n",
5261 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5263 task
= rpc_run_task(&task_setup_data
);
5265 status
= PTR_ERR(task
);
5268 status
= task
->tk_status
;
5269 if (setclientid
.sc_cred
) {
5270 clp
->cl_acceptor
= rpcauth_stringify_acceptor(setclientid
.sc_cred
);
5271 put_rpccred(setclientid
.sc_cred
);
5275 trace_nfs4_setclientid(clp
, status
);
5276 dprintk("NFS reply setclientid: %d\n", status
);
5281 * nfs4_proc_setclientid_confirm - Confirm client ID
5282 * @clp: state data structure
5283 * @res: result of a previous SETCLIENTID
5284 * @cred: RPC credential to use for this call
5286 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5288 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
5289 struct nfs4_setclientid_res
*arg
,
5290 struct rpc_cred
*cred
)
5292 struct rpc_message msg
= {
5293 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
5299 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5300 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5302 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5303 trace_nfs4_setclientid_confirm(clp
, status
);
5304 dprintk("NFS reply setclientid_confirm: %d\n", status
);
5308 struct nfs4_delegreturndata
{
5309 struct nfs4_delegreturnargs args
;
5310 struct nfs4_delegreturnres res
;
5312 nfs4_stateid stateid
;
5313 unsigned long timestamp
;
5314 struct nfs_fattr fattr
;
5316 struct inode
*inode
;
5321 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
5323 struct nfs4_delegreturndata
*data
= calldata
;
5325 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5328 trace_nfs4_delegreturn_exit(&data
->args
, &data
->res
, task
->tk_status
);
5329 switch (task
->tk_status
) {
5331 renew_lease(data
->res
.server
, data
->timestamp
);
5332 case -NFS4ERR_ADMIN_REVOKED
:
5333 case -NFS4ERR_DELEG_REVOKED
:
5334 case -NFS4ERR_BAD_STATEID
:
5335 case -NFS4ERR_OLD_STATEID
:
5336 case -NFS4ERR_STALE_STATEID
:
5337 case -NFS4ERR_EXPIRED
:
5338 task
->tk_status
= 0;
5340 pnfs_roc_set_barrier(data
->inode
, data
->roc_barrier
);
5343 if (nfs4_async_handle_error(task
, data
->res
.server
,
5344 NULL
, NULL
) == -EAGAIN
) {
5345 rpc_restart_call_prepare(task
);
5349 data
->rpc_status
= task
->tk_status
;
5352 static void nfs4_delegreturn_release(void *calldata
)
5354 struct nfs4_delegreturndata
*data
= calldata
;
5355 struct inode
*inode
= data
->inode
;
5359 pnfs_roc_release(inode
);
5360 nfs_iput_and_deactive(inode
);
5365 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
5367 struct nfs4_delegreturndata
*d_data
;
5369 d_data
= (struct nfs4_delegreturndata
*)data
;
5371 if (nfs4_wait_on_layoutreturn(d_data
->inode
, task
))
5375 pnfs_roc_get_barrier(d_data
->inode
, &d_data
->roc_barrier
);
5377 nfs4_setup_sequence(d_data
->res
.server
,
5378 &d_data
->args
.seq_args
,
5379 &d_data
->res
.seq_res
,
5383 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
5384 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
5385 .rpc_call_done
= nfs4_delegreturn_done
,
5386 .rpc_release
= nfs4_delegreturn_release
,
5389 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5391 struct nfs4_delegreturndata
*data
;
5392 struct nfs_server
*server
= NFS_SERVER(inode
);
5393 struct rpc_task
*task
;
5394 struct rpc_message msg
= {
5395 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
5398 struct rpc_task_setup task_setup_data
= {
5399 .rpc_client
= server
->client
,
5400 .rpc_message
= &msg
,
5401 .callback_ops
= &nfs4_delegreturn_ops
,
5402 .flags
= RPC_TASK_ASYNC
,
5406 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
5409 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
5411 nfs4_state_protect(server
->nfs_client
,
5412 NFS_SP4_MACH_CRED_CLEANUP
,
5413 &task_setup_data
.rpc_client
, &msg
);
5415 data
->args
.fhandle
= &data
->fh
;
5416 data
->args
.stateid
= &data
->stateid
;
5417 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
5418 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
5419 nfs4_stateid_copy(&data
->stateid
, stateid
);
5420 data
->res
.fattr
= &data
->fattr
;
5421 data
->res
.server
= server
;
5422 nfs_fattr_init(data
->res
.fattr
);
5423 data
->timestamp
= jiffies
;
5424 data
->rpc_status
= 0;
5425 data
->inode
= nfs_igrab_and_active(inode
);
5427 data
->roc
= nfs4_roc(inode
);
5429 task_setup_data
.callback_data
= data
;
5430 msg
.rpc_argp
= &data
->args
;
5431 msg
.rpc_resp
= &data
->res
;
5432 task
= rpc_run_task(&task_setup_data
);
5434 return PTR_ERR(task
);
5437 status
= nfs4_wait_for_completion_rpc_task(task
);
5440 status
= data
->rpc_status
;
5442 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
5444 nfs_refresh_inode(inode
, &data
->fattr
);
5450 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
5452 struct nfs_server
*server
= NFS_SERVER(inode
);
5453 struct nfs4_exception exception
= { };
5456 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
5457 trace_nfs4_delegreturn(inode
, stateid
, err
);
5459 case -NFS4ERR_STALE_STATEID
:
5460 case -NFS4ERR_EXPIRED
:
5464 err
= nfs4_handle_exception(server
, err
, &exception
);
5465 } while (exception
.retry
);
5469 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5470 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5473 * sleep, with exponential backoff, and retry the LOCK operation.
5475 static unsigned long
5476 nfs4_set_lock_task_retry(unsigned long timeout
)
5478 freezable_schedule_timeout_killable_unsafe(timeout
);
5480 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
5481 return NFS4_LOCK_MAXTIMEOUT
;
5485 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5487 struct inode
*inode
= state
->inode
;
5488 struct nfs_server
*server
= NFS_SERVER(inode
);
5489 struct nfs_client
*clp
= server
->nfs_client
;
5490 struct nfs_lockt_args arg
= {
5491 .fh
= NFS_FH(inode
),
5494 struct nfs_lockt_res res
= {
5497 struct rpc_message msg
= {
5498 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
5501 .rpc_cred
= state
->owner
->so_cred
,
5503 struct nfs4_lock_state
*lsp
;
5506 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
5507 status
= nfs4_set_lock_state(state
, request
);
5510 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5511 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5512 arg
.lock_owner
.s_dev
= server
->s_dev
;
5513 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
5516 request
->fl_type
= F_UNLCK
;
5518 case -NFS4ERR_DENIED
:
5521 request
->fl_ops
->fl_release_private(request
);
5522 request
->fl_ops
= NULL
;
5527 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5529 struct nfs4_exception exception
= { };
5533 err
= _nfs4_proc_getlk(state
, cmd
, request
);
5534 trace_nfs4_get_lock(request
, state
, cmd
, err
);
5535 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
), err
,
5537 } while (exception
.retry
);
5541 static int do_vfs_lock(struct inode
*inode
, struct file_lock
*fl
)
5543 return locks_lock_inode_wait(inode
, fl
);
5546 struct nfs4_unlockdata
{
5547 struct nfs_locku_args arg
;
5548 struct nfs_locku_res res
;
5549 struct nfs4_lock_state
*lsp
;
5550 struct nfs_open_context
*ctx
;
5551 struct file_lock fl
;
5552 struct nfs_server
*server
;
5553 unsigned long timestamp
;
5556 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
5557 struct nfs_open_context
*ctx
,
5558 struct nfs4_lock_state
*lsp
,
5559 struct nfs_seqid
*seqid
)
5561 struct nfs4_unlockdata
*p
;
5562 struct inode
*inode
= lsp
->ls_state
->inode
;
5564 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
5567 p
->arg
.fh
= NFS_FH(inode
);
5569 p
->arg
.seqid
= seqid
;
5570 p
->res
.seqid
= seqid
;
5572 atomic_inc(&lsp
->ls_count
);
5573 /* Ensure we don't close file until we're done freeing locks! */
5574 p
->ctx
= get_nfs_open_context(ctx
);
5575 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5576 p
->server
= NFS_SERVER(inode
);
5580 static void nfs4_locku_release_calldata(void *data
)
5582 struct nfs4_unlockdata
*calldata
= data
;
5583 nfs_free_seqid(calldata
->arg
.seqid
);
5584 nfs4_put_lock_state(calldata
->lsp
);
5585 put_nfs_open_context(calldata
->ctx
);
5589 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5591 struct nfs4_unlockdata
*calldata
= data
;
5593 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5595 switch (task
->tk_status
) {
5597 renew_lease(calldata
->server
, calldata
->timestamp
);
5598 do_vfs_lock(calldata
->lsp
->ls_state
->inode
, &calldata
->fl
);
5599 if (nfs4_update_lock_stateid(calldata
->lsp
,
5600 &calldata
->res
.stateid
))
5602 case -NFS4ERR_BAD_STATEID
:
5603 case -NFS4ERR_OLD_STATEID
:
5604 case -NFS4ERR_STALE_STATEID
:
5605 case -NFS4ERR_EXPIRED
:
5606 if (!nfs4_stateid_match(&calldata
->arg
.stateid
,
5607 &calldata
->lsp
->ls_stateid
))
5608 rpc_restart_call_prepare(task
);
5611 if (nfs4_async_handle_error(task
, calldata
->server
,
5612 NULL
, NULL
) == -EAGAIN
)
5613 rpc_restart_call_prepare(task
);
5615 nfs_release_seqid(calldata
->arg
.seqid
);
5618 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5620 struct nfs4_unlockdata
*calldata
= data
;
5622 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5624 nfs4_stateid_copy(&calldata
->arg
.stateid
, &calldata
->lsp
->ls_stateid
);
5625 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5626 /* Note: exit _without_ running nfs4_locku_done */
5629 calldata
->timestamp
= jiffies
;
5630 if (nfs4_setup_sequence(calldata
->server
,
5631 &calldata
->arg
.seq_args
,
5632 &calldata
->res
.seq_res
,
5634 nfs_release_seqid(calldata
->arg
.seqid
);
5637 task
->tk_action
= NULL
;
5639 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5642 static const struct rpc_call_ops nfs4_locku_ops
= {
5643 .rpc_call_prepare
= nfs4_locku_prepare
,
5644 .rpc_call_done
= nfs4_locku_done
,
5645 .rpc_release
= nfs4_locku_release_calldata
,
5648 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5649 struct nfs_open_context
*ctx
,
5650 struct nfs4_lock_state
*lsp
,
5651 struct nfs_seqid
*seqid
)
5653 struct nfs4_unlockdata
*data
;
5654 struct rpc_message msg
= {
5655 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5656 .rpc_cred
= ctx
->cred
,
5658 struct rpc_task_setup task_setup_data
= {
5659 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5660 .rpc_message
= &msg
,
5661 .callback_ops
= &nfs4_locku_ops
,
5662 .workqueue
= nfsiod_workqueue
,
5663 .flags
= RPC_TASK_ASYNC
,
5666 nfs4_state_protect(NFS_SERVER(lsp
->ls_state
->inode
)->nfs_client
,
5667 NFS_SP4_MACH_CRED_CLEANUP
, &task_setup_data
.rpc_client
, &msg
);
5669 /* Ensure this is an unlock - when canceling a lock, the
5670 * canceled lock is passed in, and it won't be an unlock.
5672 fl
->fl_type
= F_UNLCK
;
5674 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5676 nfs_free_seqid(seqid
);
5677 return ERR_PTR(-ENOMEM
);
5680 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5681 msg
.rpc_argp
= &data
->arg
;
5682 msg
.rpc_resp
= &data
->res
;
5683 task_setup_data
.callback_data
= data
;
5684 return rpc_run_task(&task_setup_data
);
5687 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5689 struct inode
*inode
= state
->inode
;
5690 struct nfs4_state_owner
*sp
= state
->owner
;
5691 struct nfs_inode
*nfsi
= NFS_I(inode
);
5692 struct nfs_seqid
*seqid
;
5693 struct nfs4_lock_state
*lsp
;
5694 struct rpc_task
*task
;
5695 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
5697 unsigned char fl_flags
= request
->fl_flags
;
5699 status
= nfs4_set_lock_state(state
, request
);
5700 /* Unlock _before_ we do the RPC call */
5701 request
->fl_flags
|= FL_EXISTS
;
5702 /* Exclude nfs_delegation_claim_locks() */
5703 mutex_lock(&sp
->so_delegreturn_mutex
);
5704 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5705 down_read(&nfsi
->rwsem
);
5706 if (do_vfs_lock(inode
, request
) == -ENOENT
) {
5707 up_read(&nfsi
->rwsem
);
5708 mutex_unlock(&sp
->so_delegreturn_mutex
);
5711 up_read(&nfsi
->rwsem
);
5712 mutex_unlock(&sp
->so_delegreturn_mutex
);
5715 /* Is this a delegated lock? */
5716 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5717 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5719 alloc_seqid
= NFS_SERVER(inode
)->nfs_client
->cl_mvops
->alloc_seqid
;
5720 seqid
= alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5724 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5725 status
= PTR_ERR(task
);
5728 status
= nfs4_wait_for_completion_rpc_task(task
);
5731 request
->fl_flags
= fl_flags
;
5732 trace_nfs4_unlock(request
, state
, F_SETLK
, status
);
5736 struct nfs4_lockdata
{
5737 struct nfs_lock_args arg
;
5738 struct nfs_lock_res res
;
5739 struct nfs4_lock_state
*lsp
;
5740 struct nfs_open_context
*ctx
;
5741 struct file_lock fl
;
5742 unsigned long timestamp
;
5745 struct nfs_server
*server
;
5748 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
5749 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
5752 struct nfs4_lockdata
*p
;
5753 struct inode
*inode
= lsp
->ls_state
->inode
;
5754 struct nfs_server
*server
= NFS_SERVER(inode
);
5755 struct nfs_seqid
*(*alloc_seqid
)(struct nfs_seqid_counter
*, gfp_t
);
5757 p
= kzalloc(sizeof(*p
), gfp_mask
);
5761 p
->arg
.fh
= NFS_FH(inode
);
5763 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
5764 if (IS_ERR(p
->arg
.open_seqid
))
5766 alloc_seqid
= server
->nfs_client
->cl_mvops
->alloc_seqid
;
5767 p
->arg
.lock_seqid
= alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
5768 if (IS_ERR(p
->arg
.lock_seqid
))
5769 goto out_free_seqid
;
5770 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5771 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5772 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
5773 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
5776 atomic_inc(&lsp
->ls_count
);
5777 p
->ctx
= get_nfs_open_context(ctx
);
5778 get_file(fl
->fl_file
);
5779 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5782 nfs_free_seqid(p
->arg
.open_seqid
);
5788 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
5790 struct nfs4_lockdata
*data
= calldata
;
5791 struct nfs4_state
*state
= data
->lsp
->ls_state
;
5793 dprintk("%s: begin!\n", __func__
);
5794 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
5796 /* Do we need to do an open_to_lock_owner? */
5797 if (!test_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
)) {
5798 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
5799 goto out_release_lock_seqid
;
5801 nfs4_stateid_copy(&data
->arg
.open_stateid
,
5802 &state
->open_stateid
);
5803 data
->arg
.new_lock_owner
= 1;
5804 data
->res
.open_seqid
= data
->arg
.open_seqid
;
5806 data
->arg
.new_lock_owner
= 0;
5807 nfs4_stateid_copy(&data
->arg
.lock_stateid
,
5808 &data
->lsp
->ls_stateid
);
5810 if (!nfs4_valid_open_stateid(state
)) {
5811 data
->rpc_status
= -EBADF
;
5812 task
->tk_action
= NULL
;
5813 goto out_release_open_seqid
;
5815 data
->timestamp
= jiffies
;
5816 if (nfs4_setup_sequence(data
->server
,
5817 &data
->arg
.seq_args
,
5821 out_release_open_seqid
:
5822 nfs_release_seqid(data
->arg
.open_seqid
);
5823 out_release_lock_seqid
:
5824 nfs_release_seqid(data
->arg
.lock_seqid
);
5826 nfs4_sequence_done(task
, &data
->res
.seq_res
);
5827 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
5830 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
5832 struct nfs4_lockdata
*data
= calldata
;
5833 struct nfs4_lock_state
*lsp
= data
->lsp
;
5835 dprintk("%s: begin!\n", __func__
);
5837 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5840 data
->rpc_status
= task
->tk_status
;
5841 switch (task
->tk_status
) {
5843 renew_lease(NFS_SERVER(d_inode(data
->ctx
->dentry
)),
5845 if (data
->arg
.new_lock
) {
5846 data
->fl
.fl_flags
&= ~(FL_SLEEP
| FL_ACCESS
);
5847 if (do_vfs_lock(lsp
->ls_state
->inode
, &data
->fl
) < 0) {
5848 rpc_restart_call_prepare(task
);
5852 if (data
->arg
.new_lock_owner
!= 0) {
5853 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
5854 nfs4_stateid_copy(&lsp
->ls_stateid
, &data
->res
.stateid
);
5855 set_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
5856 } else if (!nfs4_update_lock_stateid(lsp
, &data
->res
.stateid
))
5857 rpc_restart_call_prepare(task
);
5859 case -NFS4ERR_BAD_STATEID
:
5860 case -NFS4ERR_OLD_STATEID
:
5861 case -NFS4ERR_STALE_STATEID
:
5862 case -NFS4ERR_EXPIRED
:
5863 if (data
->arg
.new_lock_owner
!= 0) {
5864 if (!nfs4_stateid_match(&data
->arg
.open_stateid
,
5865 &lsp
->ls_state
->open_stateid
))
5866 rpc_restart_call_prepare(task
);
5867 } else if (!nfs4_stateid_match(&data
->arg
.lock_stateid
,
5869 rpc_restart_call_prepare(task
);
5871 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
5874 static void nfs4_lock_release(void *calldata
)
5876 struct nfs4_lockdata
*data
= calldata
;
5878 dprintk("%s: begin!\n", __func__
);
5879 nfs_free_seqid(data
->arg
.open_seqid
);
5880 if (data
->cancelled
!= 0) {
5881 struct rpc_task
*task
;
5882 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
5883 data
->arg
.lock_seqid
);
5885 rpc_put_task_async(task
);
5886 dprintk("%s: cancelling lock!\n", __func__
);
5888 nfs_free_seqid(data
->arg
.lock_seqid
);
5889 nfs4_put_lock_state(data
->lsp
);
5890 put_nfs_open_context(data
->ctx
);
5891 fput(data
->fl
.fl_file
);
5893 dprintk("%s: done!\n", __func__
);
5896 static const struct rpc_call_ops nfs4_lock_ops
= {
5897 .rpc_call_prepare
= nfs4_lock_prepare
,
5898 .rpc_call_done
= nfs4_lock_done
,
5899 .rpc_release
= nfs4_lock_release
,
5902 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
5905 case -NFS4ERR_ADMIN_REVOKED
:
5906 case -NFS4ERR_BAD_STATEID
:
5907 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5908 if (new_lock_owner
!= 0 ||
5909 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
5910 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
5912 case -NFS4ERR_STALE_STATEID
:
5913 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5914 case -NFS4ERR_EXPIRED
:
5915 nfs4_schedule_lease_recovery(server
->nfs_client
);
5919 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
5921 struct nfs4_lockdata
*data
;
5922 struct rpc_task
*task
;
5923 struct rpc_message msg
= {
5924 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
5925 .rpc_cred
= state
->owner
->so_cred
,
5927 struct rpc_task_setup task_setup_data
= {
5928 .rpc_client
= NFS_CLIENT(state
->inode
),
5929 .rpc_message
= &msg
,
5930 .callback_ops
= &nfs4_lock_ops
,
5931 .workqueue
= nfsiod_workqueue
,
5932 .flags
= RPC_TASK_ASYNC
,
5936 dprintk("%s: begin!\n", __func__
);
5937 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
5938 fl
->fl_u
.nfs4_fl
.owner
,
5939 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
5943 data
->arg
.block
= 1;
5944 nfs4_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5945 msg
.rpc_argp
= &data
->arg
;
5946 msg
.rpc_resp
= &data
->res
;
5947 task_setup_data
.callback_data
= data
;
5948 if (recovery_type
> NFS_LOCK_NEW
) {
5949 if (recovery_type
== NFS_LOCK_RECLAIM
)
5950 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
5951 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
5953 data
->arg
.new_lock
= 1;
5954 task
= rpc_run_task(&task_setup_data
);
5956 return PTR_ERR(task
);
5957 ret
= nfs4_wait_for_completion_rpc_task(task
);
5959 ret
= data
->rpc_status
;
5961 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
5962 data
->arg
.new_lock_owner
, ret
);
5964 data
->cancelled
= 1;
5966 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
5967 trace_nfs4_set_lock(fl
, state
, &data
->res
.stateid
, cmd
, ret
);
5971 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
5973 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5974 struct nfs4_exception exception
= {
5975 .inode
= state
->inode
,
5980 /* Cache the lock if possible... */
5981 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5983 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
5984 if (err
!= -NFS4ERR_DELAY
)
5986 nfs4_handle_exception(server
, err
, &exception
);
5987 } while (exception
.retry
);
5991 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5993 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5994 struct nfs4_exception exception
= {
5995 .inode
= state
->inode
,
5999 err
= nfs4_set_lock_state(state
, request
);
6002 if (!recover_lost_locks
) {
6003 set_bit(NFS_LOCK_LOST
, &request
->fl_u
.nfs4_fl
.owner
->ls_flags
);
6007 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
6009 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
6013 case -NFS4ERR_GRACE
:
6014 case -NFS4ERR_DELAY
:
6015 nfs4_handle_exception(server
, err
, &exception
);
6018 } while (exception
.retry
);
6023 #if defined(CONFIG_NFS_V4_1)
6025 * nfs41_check_expired_locks - possibly free a lock stateid
6027 * @state: NFSv4 state for an inode
6029 * Returns NFS_OK if recovery for this stateid is now finished.
6030 * Otherwise a negative NFS4ERR value is returned.
6032 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
6034 int status
, ret
= -NFS4ERR_BAD_STATEID
;
6035 struct nfs4_lock_state
*lsp
;
6036 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6038 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
6039 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
6040 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
6042 status
= nfs41_test_stateid(server
,
6045 trace_nfs4_test_lock_stateid(state
, lsp
, status
);
6046 if (status
!= NFS_OK
) {
6047 /* Free the stateid unless the server
6048 * informs us the stateid is unrecognized. */
6049 if (status
!= -NFS4ERR_BAD_STATEID
)
6050 nfs41_free_stateid(server
,
6053 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
6062 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
6064 int status
= NFS_OK
;
6066 if (test_bit(LK_STATE_IN_USE
, &state
->flags
))
6067 status
= nfs41_check_expired_locks(state
);
6068 if (status
!= NFS_OK
)
6069 status
= nfs4_lock_expired(state
, request
);
6074 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6076 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
6077 struct nfs4_state_owner
*sp
= state
->owner
;
6078 unsigned char fl_flags
= request
->fl_flags
;
6079 int status
= -ENOLCK
;
6081 if ((fl_flags
& FL_POSIX
) &&
6082 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
6084 /* Is this a delegated open? */
6085 status
= nfs4_set_lock_state(state
, request
);
6088 request
->fl_flags
|= FL_ACCESS
;
6089 status
= do_vfs_lock(state
->inode
, request
);
6092 mutex_lock(&sp
->so_delegreturn_mutex
);
6093 down_read(&nfsi
->rwsem
);
6094 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
6095 /* Yes: cache locks! */
6096 /* ...but avoid races with delegation recall... */
6097 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
6098 status
= do_vfs_lock(state
->inode
, request
);
6099 up_read(&nfsi
->rwsem
);
6100 mutex_unlock(&sp
->so_delegreturn_mutex
);
6103 up_read(&nfsi
->rwsem
);
6104 mutex_unlock(&sp
->so_delegreturn_mutex
);
6105 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
6107 request
->fl_flags
= fl_flags
;
6111 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
6113 struct nfs4_exception exception
= {
6115 .inode
= state
->inode
,
6120 err
= _nfs4_proc_setlk(state
, cmd
, request
);
6121 if (err
== -NFS4ERR_DENIED
)
6123 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
6125 } while (exception
.retry
);
6130 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
6132 struct nfs_open_context
*ctx
;
6133 struct nfs4_state
*state
;
6134 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
6137 /* verify open state */
6138 ctx
= nfs_file_open_context(filp
);
6141 if (request
->fl_start
< 0 || request
->fl_end
< 0)
6144 if (IS_GETLK(cmd
)) {
6146 return nfs4_proc_getlk(state
, F_GETLK
, request
);
6150 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
6153 if (request
->fl_type
== F_UNLCK
) {
6155 return nfs4_proc_unlck(state
, cmd
, request
);
6162 * Don't rely on the VFS having checked the file open mode,
6163 * since it won't do this for flock() locks.
6165 switch (request
->fl_type
) {
6167 if (!(filp
->f_mode
& FMODE_READ
))
6171 if (!(filp
->f_mode
& FMODE_WRITE
))
6176 status
= nfs4_proc_setlk(state
, cmd
, request
);
6177 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
6179 timeout
= nfs4_set_lock_task_retry(timeout
);
6180 status
= -ERESTARTSYS
;
6183 } while(status
< 0);
6187 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
6189 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
6192 err
= nfs4_set_lock_state(state
, fl
);
6195 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
6196 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
6199 struct nfs_release_lockowner_data
{
6200 struct nfs4_lock_state
*lsp
;
6201 struct nfs_server
*server
;
6202 struct nfs_release_lockowner_args args
;
6203 struct nfs_release_lockowner_res res
;
6204 unsigned long timestamp
;
6207 static void nfs4_release_lockowner_prepare(struct rpc_task
*task
, void *calldata
)
6209 struct nfs_release_lockowner_data
*data
= calldata
;
6210 struct nfs_server
*server
= data
->server
;
6211 nfs40_setup_sequence(server
->nfs_client
->cl_slot_tbl
,
6212 &data
->args
.seq_args
, &data
->res
.seq_res
, task
);
6213 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6214 data
->timestamp
= jiffies
;
6217 static void nfs4_release_lockowner_done(struct rpc_task
*task
, void *calldata
)
6219 struct nfs_release_lockowner_data
*data
= calldata
;
6220 struct nfs_server
*server
= data
->server
;
6222 nfs40_sequence_done(task
, &data
->res
.seq_res
);
6224 switch (task
->tk_status
) {
6226 renew_lease(server
, data
->timestamp
);
6228 case -NFS4ERR_STALE_CLIENTID
:
6229 case -NFS4ERR_EXPIRED
:
6230 nfs4_schedule_lease_recovery(server
->nfs_client
);
6232 case -NFS4ERR_LEASE_MOVED
:
6233 case -NFS4ERR_DELAY
:
6234 if (nfs4_async_handle_error(task
, server
,
6235 NULL
, NULL
) == -EAGAIN
)
6236 rpc_restart_call_prepare(task
);
6240 static void nfs4_release_lockowner_release(void *calldata
)
6242 struct nfs_release_lockowner_data
*data
= calldata
;
6243 nfs4_free_lock_state(data
->server
, data
->lsp
);
6247 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
6248 .rpc_call_prepare
= nfs4_release_lockowner_prepare
,
6249 .rpc_call_done
= nfs4_release_lockowner_done
,
6250 .rpc_release
= nfs4_release_lockowner_release
,
6254 nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
6256 struct nfs_release_lockowner_data
*data
;
6257 struct rpc_message msg
= {
6258 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
6261 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
6264 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
6268 data
->server
= server
;
6269 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
6270 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
6271 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
6273 msg
.rpc_argp
= &data
->args
;
6274 msg
.rpc_resp
= &data
->res
;
6275 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
6276 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
6279 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6281 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler
*handler
,
6282 struct dentry
*unused
, struct inode
*inode
,
6283 const char *key
, const void *buf
,
6284 size_t buflen
, int flags
)
6286 return nfs4_proc_set_acl(inode
, buf
, buflen
);
6289 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler
*handler
,
6290 struct dentry
*unused
, struct inode
*inode
,
6291 const char *key
, void *buf
, size_t buflen
)
6293 return nfs4_proc_get_acl(inode
, buf
, buflen
);
6296 static bool nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
)
6298 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry
)));
6301 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6303 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler
*handler
,
6304 struct dentry
*unused
, struct inode
*inode
,
6305 const char *key
, const void *buf
,
6306 size_t buflen
, int flags
)
6308 if (security_ismaclabel(key
))
6309 return nfs4_set_security_label(inode
, buf
, buflen
);
6314 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler
*handler
,
6315 struct dentry
*unused
, struct inode
*inode
,
6316 const char *key
, void *buf
, size_t buflen
)
6318 if (security_ismaclabel(key
))
6319 return nfs4_get_security_label(inode
, buf
, buflen
);
6324 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6328 if (nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
)) {
6329 len
= security_inode_listsecurity(inode
, list
, list_len
);
6330 if (list_len
&& len
> list_len
)
6336 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
6337 .prefix
= XATTR_SECURITY_PREFIX
,
6338 .get
= nfs4_xattr_get_nfs4_label
,
6339 .set
= nfs4_xattr_set_nfs4_label
,
6345 nfs4_listxattr_nfs4_label(struct inode
*inode
, char *list
, size_t list_len
)
6353 * nfs_fhget will use either the mounted_on_fileid or the fileid
6355 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
6357 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
6358 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
6359 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
6360 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
6363 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
6364 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
6365 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
6369 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6370 const struct qstr
*name
,
6371 struct nfs4_fs_locations
*fs_locations
,
6374 struct nfs_server
*server
= NFS_SERVER(dir
);
6376 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6378 struct nfs4_fs_locations_arg args
= {
6379 .dir_fh
= NFS_FH(dir
),
6384 struct nfs4_fs_locations_res res
= {
6385 .fs_locations
= fs_locations
,
6387 struct rpc_message msg
= {
6388 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6394 dprintk("%s: start\n", __func__
);
6396 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6397 * is not supported */
6398 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
6399 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
6401 bitmask
[0] |= FATTR4_WORD0_FILEID
;
6403 nfs_fattr_init(&fs_locations
->fattr
);
6404 fs_locations
->server
= server
;
6405 fs_locations
->nlocations
= 0;
6406 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6407 dprintk("%s: returned status = %d\n", __func__
, status
);
6411 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
6412 const struct qstr
*name
,
6413 struct nfs4_fs_locations
*fs_locations
,
6416 struct nfs4_exception exception
= { };
6419 err
= _nfs4_proc_fs_locations(client
, dir
, name
,
6420 fs_locations
, page
);
6421 trace_nfs4_get_fs_locations(dir
, name
, err
);
6422 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6424 } while (exception
.retry
);
6429 * This operation also signals the server that this client is
6430 * performing migration recovery. The server can stop returning
6431 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6432 * appended to this compound to identify the client ID which is
6433 * performing recovery.
6435 static int _nfs40_proc_get_locations(struct inode
*inode
,
6436 struct nfs4_fs_locations
*locations
,
6437 struct page
*page
, struct rpc_cred
*cred
)
6439 struct nfs_server
*server
= NFS_SERVER(inode
);
6440 struct rpc_clnt
*clnt
= server
->client
;
6442 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6444 struct nfs4_fs_locations_arg args
= {
6445 .clientid
= server
->nfs_client
->cl_clientid
,
6446 .fh
= NFS_FH(inode
),
6449 .migration
= 1, /* skip LOOKUP */
6450 .renew
= 1, /* append RENEW */
6452 struct nfs4_fs_locations_res res
= {
6453 .fs_locations
= locations
,
6457 struct rpc_message msg
= {
6458 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6463 unsigned long now
= jiffies
;
6466 nfs_fattr_init(&locations
->fattr
);
6467 locations
->server
= server
;
6468 locations
->nlocations
= 0;
6470 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6471 nfs4_set_sequence_privileged(&args
.seq_args
);
6472 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6473 &args
.seq_args
, &res
.seq_res
);
6477 renew_lease(server
, now
);
6481 #ifdef CONFIG_NFS_V4_1
6484 * This operation also signals the server that this client is
6485 * performing migration recovery. The server can stop asserting
6486 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6487 * performing this operation is identified in the SEQUENCE
6488 * operation in this compound.
6490 * When the client supports GETATTR(fs_locations_info), it can
6491 * be plumbed in here.
6493 static int _nfs41_proc_get_locations(struct inode
*inode
,
6494 struct nfs4_fs_locations
*locations
,
6495 struct page
*page
, struct rpc_cred
*cred
)
6497 struct nfs_server
*server
= NFS_SERVER(inode
);
6498 struct rpc_clnt
*clnt
= server
->client
;
6500 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
6502 struct nfs4_fs_locations_arg args
= {
6503 .fh
= NFS_FH(inode
),
6506 .migration
= 1, /* skip LOOKUP */
6508 struct nfs4_fs_locations_res res
= {
6509 .fs_locations
= locations
,
6512 struct rpc_message msg
= {
6513 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
6520 nfs_fattr_init(&locations
->fattr
);
6521 locations
->server
= server
;
6522 locations
->nlocations
= 0;
6524 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6525 nfs4_set_sequence_privileged(&args
.seq_args
);
6526 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6527 &args
.seq_args
, &res
.seq_res
);
6528 if (status
== NFS4_OK
&&
6529 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6530 status
= -NFS4ERR_LEASE_MOVED
;
6534 #endif /* CONFIG_NFS_V4_1 */
6537 * nfs4_proc_get_locations - discover locations for a migrated FSID
6538 * @inode: inode on FSID that is migrating
6539 * @locations: result of query
6541 * @cred: credential to use for this operation
6543 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6544 * operation failed, or a negative errno if a local error occurred.
6546 * On success, "locations" is filled in, but if the server has
6547 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6550 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6551 * from this client that require migration recovery.
6553 int nfs4_proc_get_locations(struct inode
*inode
,
6554 struct nfs4_fs_locations
*locations
,
6555 struct page
*page
, struct rpc_cred
*cred
)
6557 struct nfs_server
*server
= NFS_SERVER(inode
);
6558 struct nfs_client
*clp
= server
->nfs_client
;
6559 const struct nfs4_mig_recovery_ops
*ops
=
6560 clp
->cl_mvops
->mig_recovery_ops
;
6561 struct nfs4_exception exception
= { };
6564 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6565 (unsigned long long)server
->fsid
.major
,
6566 (unsigned long long)server
->fsid
.minor
,
6568 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6571 status
= ops
->get_locations(inode
, locations
, page
, cred
);
6572 if (status
!= -NFS4ERR_DELAY
)
6574 nfs4_handle_exception(server
, status
, &exception
);
6575 } while (exception
.retry
);
6580 * This operation also signals the server that this client is
6581 * performing "lease moved" recovery. The server can stop
6582 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6583 * is appended to this compound to identify the client ID which is
6584 * performing recovery.
6586 static int _nfs40_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6588 struct nfs_server
*server
= NFS_SERVER(inode
);
6589 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
6590 struct rpc_clnt
*clnt
= server
->client
;
6591 struct nfs4_fsid_present_arg args
= {
6592 .fh
= NFS_FH(inode
),
6593 .clientid
= clp
->cl_clientid
,
6594 .renew
= 1, /* append RENEW */
6596 struct nfs4_fsid_present_res res
= {
6599 struct rpc_message msg
= {
6600 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6605 unsigned long now
= jiffies
;
6608 res
.fh
= nfs_alloc_fhandle();
6612 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6613 nfs4_set_sequence_privileged(&args
.seq_args
);
6614 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6615 &args
.seq_args
, &res
.seq_res
);
6616 nfs_free_fhandle(res
.fh
);
6620 do_renew_lease(clp
, now
);
6624 #ifdef CONFIG_NFS_V4_1
6627 * This operation also signals the server that this client is
6628 * performing "lease moved" recovery. The server can stop asserting
6629 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6630 * this operation is identified in the SEQUENCE operation in this
6633 static int _nfs41_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6635 struct nfs_server
*server
= NFS_SERVER(inode
);
6636 struct rpc_clnt
*clnt
= server
->client
;
6637 struct nfs4_fsid_present_arg args
= {
6638 .fh
= NFS_FH(inode
),
6640 struct nfs4_fsid_present_res res
= {
6642 struct rpc_message msg
= {
6643 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSID_PRESENT
],
6650 res
.fh
= nfs_alloc_fhandle();
6654 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6655 nfs4_set_sequence_privileged(&args
.seq_args
);
6656 status
= nfs4_call_sync_sequence(clnt
, server
, &msg
,
6657 &args
.seq_args
, &res
.seq_res
);
6658 nfs_free_fhandle(res
.fh
);
6659 if (status
== NFS4_OK
&&
6660 res
.seq_res
.sr_status_flags
& SEQ4_STATUS_LEASE_MOVED
)
6661 status
= -NFS4ERR_LEASE_MOVED
;
6665 #endif /* CONFIG_NFS_V4_1 */
6668 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6669 * @inode: inode on FSID to check
6670 * @cred: credential to use for this operation
6672 * Server indicates whether the FSID is present, moved, or not
6673 * recognized. This operation is necessary to clear a LEASE_MOVED
6674 * condition for this client ID.
6676 * Returns NFS4_OK if the FSID is present on this server,
6677 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6678 * NFS4ERR code if some error occurred on the server, or a
6679 * negative errno if a local failure occurred.
6681 int nfs4_proc_fsid_present(struct inode
*inode
, struct rpc_cred
*cred
)
6683 struct nfs_server
*server
= NFS_SERVER(inode
);
6684 struct nfs_client
*clp
= server
->nfs_client
;
6685 const struct nfs4_mig_recovery_ops
*ops
=
6686 clp
->cl_mvops
->mig_recovery_ops
;
6687 struct nfs4_exception exception
= { };
6690 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__
,
6691 (unsigned long long)server
->fsid
.major
,
6692 (unsigned long long)server
->fsid
.minor
,
6694 nfs_display_fhandle(NFS_FH(inode
), __func__
);
6697 status
= ops
->fsid_present(inode
, cred
);
6698 if (status
!= -NFS4ERR_DELAY
)
6700 nfs4_handle_exception(server
, status
, &exception
);
6701 } while (exception
.retry
);
6706 * If 'use_integrity' is true and the state managment nfs_client
6707 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6708 * and the machine credential as per RFC3530bis and RFC5661 Security
6709 * Considerations sections. Otherwise, just use the user cred with the
6710 * filesystem's rpc_client.
6712 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
6715 struct nfs4_secinfo_arg args
= {
6716 .dir_fh
= NFS_FH(dir
),
6719 struct nfs4_secinfo_res res
= {
6722 struct rpc_message msg
= {
6723 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
6727 struct rpc_clnt
*clnt
= NFS_SERVER(dir
)->client
;
6728 struct rpc_cred
*cred
= NULL
;
6730 if (use_integrity
) {
6731 clnt
= NFS_SERVER(dir
)->nfs_client
->cl_rpcclient
;
6732 cred
= nfs4_get_clid_cred(NFS_SERVER(dir
)->nfs_client
);
6733 msg
.rpc_cred
= cred
;
6736 dprintk("NFS call secinfo %s\n", name
->name
);
6738 nfs4_state_protect(NFS_SERVER(dir
)->nfs_client
,
6739 NFS_SP4_MACH_CRED_SECINFO
, &clnt
, &msg
);
6741 status
= nfs4_call_sync(clnt
, NFS_SERVER(dir
), &msg
, &args
.seq_args
,
6743 dprintk("NFS reply secinfo: %d\n", status
);
6751 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
6752 struct nfs4_secinfo_flavors
*flavors
)
6754 struct nfs4_exception exception
= { };
6757 err
= -NFS4ERR_WRONGSEC
;
6759 /* try to use integrity protection with machine cred */
6760 if (_nfs4_is_integrity_protected(NFS_SERVER(dir
)->nfs_client
))
6761 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, true);
6764 * if unable to use integrity protection, or SECINFO with
6765 * integrity protection returns NFS4ERR_WRONGSEC (which is
6766 * disallowed by spec, but exists in deployed servers) use
6767 * the current filesystem's rpc_client and the user cred.
6769 if (err
== -NFS4ERR_WRONGSEC
)
6770 err
= _nfs4_proc_secinfo(dir
, name
, flavors
, false);
6772 trace_nfs4_secinfo(dir
, name
, err
);
6773 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
,
6775 } while (exception
.retry
);
6779 #ifdef CONFIG_NFS_V4_1
6781 * Check the exchange flags returned by the server for invalid flags, having
6782 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6785 static int nfs4_check_cl_exchange_flags(u32 flags
)
6787 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
6789 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
6790 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
6792 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
6796 return -NFS4ERR_INVAL
;
6800 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
6801 struct nfs41_server_scope
*b
)
6803 if (a
->server_scope_sz
== b
->server_scope_sz
&&
6804 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
6811 nfs4_bind_one_conn_to_session_done(struct rpc_task
*task
, void *calldata
)
6815 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops
= {
6816 .rpc_call_done
= &nfs4_bind_one_conn_to_session_done
,
6820 * nfs4_proc_bind_one_conn_to_session()
6822 * The 4.1 client currently uses the same TCP connection for the
6823 * fore and backchannel.
6826 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt
*clnt
,
6827 struct rpc_xprt
*xprt
,
6828 struct nfs_client
*clp
,
6829 struct rpc_cred
*cred
)
6832 struct nfs41_bind_conn_to_session_args args
= {
6834 .dir
= NFS4_CDFC4_FORE_OR_BOTH
,
6836 struct nfs41_bind_conn_to_session_res res
;
6837 struct rpc_message msg
= {
6839 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
6844 struct rpc_task_setup task_setup_data
= {
6847 .callback_ops
= &nfs4_bind_one_conn_to_session_ops
,
6848 .rpc_message
= &msg
,
6849 .flags
= RPC_TASK_TIMEOUT
,
6851 struct rpc_task
*task
;
6853 dprintk("--> %s\n", __func__
);
6855 nfs4_copy_sessionid(&args
.sessionid
, &clp
->cl_session
->sess_id
);
6856 if (!(clp
->cl_session
->flags
& SESSION4_BACK_CHAN
))
6857 args
.dir
= NFS4_CDFC4_FORE
;
6859 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6860 if (xprt
!= rcu_access_pointer(clnt
->cl_xprt
))
6861 args
.dir
= NFS4_CDFC4_FORE
;
6863 task
= rpc_run_task(&task_setup_data
);
6864 if (!IS_ERR(task
)) {
6865 status
= task
->tk_status
;
6868 status
= PTR_ERR(task
);
6869 trace_nfs4_bind_conn_to_session(clp
, status
);
6871 if (memcmp(res
.sessionid
.data
,
6872 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
6873 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
6877 if ((res
.dir
& args
.dir
) != res
.dir
|| res
.dir
== 0) {
6878 dprintk("NFS: %s: Unexpected direction from server\n",
6883 if (res
.use_conn_in_rdma_mode
!= args
.use_conn_in_rdma_mode
) {
6884 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6891 dprintk("<-- %s status= %d\n", __func__
, status
);
6895 struct rpc_bind_conn_calldata
{
6896 struct nfs_client
*clp
;
6897 struct rpc_cred
*cred
;
6901 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt
*clnt
,
6902 struct rpc_xprt
*xprt
,
6905 struct rpc_bind_conn_calldata
*p
= calldata
;
6907 return nfs4_proc_bind_one_conn_to_session(clnt
, xprt
, p
->clp
, p
->cred
);
6910 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6912 struct rpc_bind_conn_calldata data
= {
6916 return rpc_clnt_iterate_for_each_xprt(clp
->cl_rpcclient
,
6917 nfs4_proc_bind_conn_to_session_callback
, &data
);
6921 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6922 * and operations we'd like to see to enable certain features in the allow map
6924 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request
= {
6925 .how
= SP4_MACH_CRED
,
6926 .enforce
.u
.words
= {
6927 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6928 1 << (OP_EXCHANGE_ID
- 32) |
6929 1 << (OP_CREATE_SESSION
- 32) |
6930 1 << (OP_DESTROY_SESSION
- 32) |
6931 1 << (OP_DESTROY_CLIENTID
- 32)
6934 [0] = 1 << (OP_CLOSE
) |
6935 1 << (OP_OPEN_DOWNGRADE
) |
6937 1 << (OP_DELEGRETURN
) |
6939 [1] = 1 << (OP_SECINFO
- 32) |
6940 1 << (OP_SECINFO_NO_NAME
- 32) |
6941 1 << (OP_LAYOUTRETURN
- 32) |
6942 1 << (OP_TEST_STATEID
- 32) |
6943 1 << (OP_FREE_STATEID
- 32) |
6944 1 << (OP_WRITE
- 32)
6949 * Select the state protection mode for client `clp' given the server results
6950 * from exchange_id in `sp'.
6952 * Returns 0 on success, negative errno otherwise.
6954 static int nfs4_sp4_select_mode(struct nfs_client
*clp
,
6955 struct nfs41_state_protection
*sp
)
6957 static const u32 supported_enforce
[NFS4_OP_MAP_NUM_WORDS
] = {
6958 [1] = 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
6959 1 << (OP_EXCHANGE_ID
- 32) |
6960 1 << (OP_CREATE_SESSION
- 32) |
6961 1 << (OP_DESTROY_SESSION
- 32) |
6962 1 << (OP_DESTROY_CLIENTID
- 32)
6966 if (sp
->how
== SP4_MACH_CRED
) {
6967 /* Print state protect result */
6968 dfprintk(MOUNT
, "Server SP4_MACH_CRED support:\n");
6969 for (i
= 0; i
<= LAST_NFS4_OP
; i
++) {
6970 if (test_bit(i
, sp
->enforce
.u
.longs
))
6971 dfprintk(MOUNT
, " enforce op %d\n", i
);
6972 if (test_bit(i
, sp
->allow
.u
.longs
))
6973 dfprintk(MOUNT
, " allow op %d\n", i
);
6976 /* make sure nothing is on enforce list that isn't supported */
6977 for (i
= 0; i
< NFS4_OP_MAP_NUM_WORDS
; i
++) {
6978 if (sp
->enforce
.u
.words
[i
] & ~supported_enforce
[i
]) {
6979 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
6985 * Minimal mode - state operations are allowed to use machine
6986 * credential. Note this already happens by default, so the
6987 * client doesn't have to do anything more than the negotiation.
6989 * NOTE: we don't care if EXCHANGE_ID is in the list -
6990 * we're already using the machine cred for exchange_id
6991 * and will never use a different cred.
6993 if (test_bit(OP_BIND_CONN_TO_SESSION
, sp
->enforce
.u
.longs
) &&
6994 test_bit(OP_CREATE_SESSION
, sp
->enforce
.u
.longs
) &&
6995 test_bit(OP_DESTROY_SESSION
, sp
->enforce
.u
.longs
) &&
6996 test_bit(OP_DESTROY_CLIENTID
, sp
->enforce
.u
.longs
)) {
6997 dfprintk(MOUNT
, "sp4_mach_cred:\n");
6998 dfprintk(MOUNT
, " minimal mode enabled\n");
6999 set_bit(NFS_SP4_MACH_CRED_MINIMAL
, &clp
->cl_sp4_flags
);
7001 dfprintk(MOUNT
, "sp4_mach_cred: disabled\n");
7005 if (test_bit(OP_CLOSE
, sp
->allow
.u
.longs
) &&
7006 test_bit(OP_OPEN_DOWNGRADE
, sp
->allow
.u
.longs
) &&
7007 test_bit(OP_DELEGRETURN
, sp
->allow
.u
.longs
) &&
7008 test_bit(OP_LOCKU
, sp
->allow
.u
.longs
)) {
7009 dfprintk(MOUNT
, " cleanup mode enabled\n");
7010 set_bit(NFS_SP4_MACH_CRED_CLEANUP
, &clp
->cl_sp4_flags
);
7013 if (test_bit(OP_LAYOUTRETURN
, sp
->allow
.u
.longs
)) {
7014 dfprintk(MOUNT
, " pnfs cleanup mode enabled\n");
7015 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
7016 &clp
->cl_sp4_flags
);
7019 if (test_bit(OP_SECINFO
, sp
->allow
.u
.longs
) &&
7020 test_bit(OP_SECINFO_NO_NAME
, sp
->allow
.u
.longs
)) {
7021 dfprintk(MOUNT
, " secinfo mode enabled\n");
7022 set_bit(NFS_SP4_MACH_CRED_SECINFO
, &clp
->cl_sp4_flags
);
7025 if (test_bit(OP_TEST_STATEID
, sp
->allow
.u
.longs
) &&
7026 test_bit(OP_FREE_STATEID
, sp
->allow
.u
.longs
)) {
7027 dfprintk(MOUNT
, " stateid mode enabled\n");
7028 set_bit(NFS_SP4_MACH_CRED_STATEID
, &clp
->cl_sp4_flags
);
7031 if (test_bit(OP_WRITE
, sp
->allow
.u
.longs
)) {
7032 dfprintk(MOUNT
, " write mode enabled\n");
7033 set_bit(NFS_SP4_MACH_CRED_WRITE
, &clp
->cl_sp4_flags
);
7036 if (test_bit(OP_COMMIT
, sp
->allow
.u
.longs
)) {
7037 dfprintk(MOUNT
, " commit mode enabled\n");
7038 set_bit(NFS_SP4_MACH_CRED_COMMIT
, &clp
->cl_sp4_flags
);
7046 * _nfs4_proc_exchange_id()
7048 * Wrapper for EXCHANGE_ID operation.
7050 static int _nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
,
7053 nfs4_verifier verifier
;
7054 struct nfs41_exchange_id_args args
= {
7055 .verifier
= &verifier
,
7057 #ifdef CONFIG_NFS_V4_1_MIGRATION
7058 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7059 EXCHGID4_FLAG_BIND_PRINC_STATEID
|
7060 EXCHGID4_FLAG_SUPP_MOVED_MIGR
,
7062 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
7063 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
7066 struct nfs41_exchange_id_res res
= {
7070 struct rpc_message msg
= {
7071 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
7077 nfs4_init_boot_verifier(clp
, &verifier
);
7079 status
= nfs4_init_uniform_client_string(clp
);
7083 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7084 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
7087 res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
7089 if (unlikely(res
.server_owner
== NULL
)) {
7094 res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
7096 if (unlikely(res
.server_scope
== NULL
)) {
7098 goto out_server_owner
;
7101 res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
7102 if (unlikely(res
.impl_id
== NULL
)) {
7104 goto out_server_scope
;
7109 args
.state_protect
.how
= SP4_NONE
;
7113 args
.state_protect
= nfs4_sp4_mach_cred_request
;
7123 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7124 trace_nfs4_exchange_id(clp
, status
);
7126 status
= nfs4_check_cl_exchange_flags(res
.flags
);
7129 status
= nfs4_sp4_select_mode(clp
, &res
.state_protect
);
7132 clp
->cl_clientid
= res
.clientid
;
7133 clp
->cl_exchange_flags
= res
.flags
;
7134 /* Client ID is not confirmed */
7135 if (!(res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
)) {
7136 clear_bit(NFS4_SESSION_ESTABLISHED
,
7137 &clp
->cl_session
->session_state
);
7138 clp
->cl_seqid
= res
.seqid
;
7141 kfree(clp
->cl_serverowner
);
7142 clp
->cl_serverowner
= res
.server_owner
;
7143 res
.server_owner
= NULL
;
7145 /* use the most recent implementation id */
7146 kfree(clp
->cl_implid
);
7147 clp
->cl_implid
= res
.impl_id
;
7150 if (clp
->cl_serverscope
!= NULL
&&
7151 !nfs41_same_server_scope(clp
->cl_serverscope
,
7152 res
.server_scope
)) {
7153 dprintk("%s: server_scope mismatch detected\n",
7155 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
7156 kfree(clp
->cl_serverscope
);
7157 clp
->cl_serverscope
= NULL
;
7160 if (clp
->cl_serverscope
== NULL
) {
7161 clp
->cl_serverscope
= res
.server_scope
;
7162 res
.server_scope
= NULL
;
7169 kfree(res
.server_scope
);
7171 kfree(res
.server_owner
);
7173 if (clp
->cl_implid
!= NULL
)
7174 dprintk("NFS reply exchange_id: Server Implementation ID: "
7175 "domain: %s, name: %s, date: %llu,%u\n",
7176 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
7177 clp
->cl_implid
->date
.seconds
,
7178 clp
->cl_implid
->date
.nseconds
);
7179 dprintk("NFS reply exchange_id: %d\n", status
);
7184 * nfs4_proc_exchange_id()
7186 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7188 * Since the clientid has expired, all compounds using sessions
7189 * associated with the stale clientid will be returning
7190 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7191 * be in some phase of session reset.
7193 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7195 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7197 rpc_authflavor_t authflavor
= clp
->cl_rpcclient
->cl_auth
->au_flavor
;
7200 /* try SP4_MACH_CRED if krb5i/p */
7201 if (authflavor
== RPC_AUTH_GSS_KRB5I
||
7202 authflavor
== RPC_AUTH_GSS_KRB5P
) {
7203 status
= _nfs4_proc_exchange_id(clp
, cred
, SP4_MACH_CRED
);
7209 return _nfs4_proc_exchange_id(clp
, cred
, SP4_NONE
);
7212 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7213 struct rpc_cred
*cred
)
7215 struct rpc_message msg
= {
7216 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
7222 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7223 trace_nfs4_destroy_clientid(clp
, status
);
7225 dprintk("NFS: Got error %d from the server %s on "
7226 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
7230 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
7231 struct rpc_cred
*cred
)
7236 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
7237 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
7239 case -NFS4ERR_DELAY
:
7240 case -NFS4ERR_CLIENTID_BUSY
:
7250 int nfs4_destroy_clientid(struct nfs_client
*clp
)
7252 struct rpc_cred
*cred
;
7255 if (clp
->cl_mvops
->minor_version
< 1)
7257 if (clp
->cl_exchange_flags
== 0)
7259 if (clp
->cl_preserve_clid
)
7261 cred
= nfs4_get_clid_cred(clp
);
7262 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
7267 case -NFS4ERR_STALE_CLIENTID
:
7268 clp
->cl_exchange_flags
= 0;
7274 struct nfs4_get_lease_time_data
{
7275 struct nfs4_get_lease_time_args
*args
;
7276 struct nfs4_get_lease_time_res
*res
;
7277 struct nfs_client
*clp
;
7280 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
7283 struct nfs4_get_lease_time_data
*data
=
7284 (struct nfs4_get_lease_time_data
*)calldata
;
7286 dprintk("--> %s\n", __func__
);
7287 /* just setup sequence, do not trigger session recovery
7288 since we're invoked within one */
7289 nfs41_setup_sequence(data
->clp
->cl_session
,
7290 &data
->args
->la_seq_args
,
7291 &data
->res
->lr_seq_res
,
7293 dprintk("<-- %s\n", __func__
);
7297 * Called from nfs4_state_manager thread for session setup, so don't recover
7298 * from sequence operation or clientid errors.
7300 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
7302 struct nfs4_get_lease_time_data
*data
=
7303 (struct nfs4_get_lease_time_data
*)calldata
;
7305 dprintk("--> %s\n", __func__
);
7306 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
7308 switch (task
->tk_status
) {
7309 case -NFS4ERR_DELAY
:
7310 case -NFS4ERR_GRACE
:
7311 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
7312 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
7313 task
->tk_status
= 0;
7315 case -NFS4ERR_RETRY_UNCACHED_REP
:
7316 rpc_restart_call_prepare(task
);
7319 dprintk("<-- %s\n", __func__
);
7322 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
7323 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
7324 .rpc_call_done
= nfs4_get_lease_time_done
,
7327 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
7329 struct rpc_task
*task
;
7330 struct nfs4_get_lease_time_args args
;
7331 struct nfs4_get_lease_time_res res
= {
7332 .lr_fsinfo
= fsinfo
,
7334 struct nfs4_get_lease_time_data data
= {
7339 struct rpc_message msg
= {
7340 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
7344 struct rpc_task_setup task_setup
= {
7345 .rpc_client
= clp
->cl_rpcclient
,
7346 .rpc_message
= &msg
,
7347 .callback_ops
= &nfs4_get_lease_time_ops
,
7348 .callback_data
= &data
,
7349 .flags
= RPC_TASK_TIMEOUT
,
7353 nfs4_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
7354 nfs4_set_sequence_privileged(&args
.la_seq_args
);
7355 dprintk("--> %s\n", __func__
);
7356 task
= rpc_run_task(&task_setup
);
7359 status
= PTR_ERR(task
);
7361 status
= task
->tk_status
;
7364 dprintk("<-- %s return %d\n", __func__
, status
);
7370 * Initialize the values to be used by the client in CREATE_SESSION
7371 * If nfs4_init_session set the fore channel request and response sizes,
7374 * Set the back channel max_resp_sz_cached to zero to force the client to
7375 * always set csa_cachethis to FALSE because the current implementation
7376 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7378 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
,
7379 struct rpc_clnt
*clnt
)
7381 unsigned int max_rqst_sz
, max_resp_sz
;
7382 unsigned int max_bc_payload
= rpc_max_bc_payload(clnt
);
7384 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
7385 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
7387 /* Fore channel attributes */
7388 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
7389 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
7390 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
7391 args
->fc_attrs
.max_reqs
= max_session_slots
;
7393 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7394 "max_ops=%u max_reqs=%u\n",
7396 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
7397 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
7399 /* Back channel attributes */
7400 args
->bc_attrs
.max_rqst_sz
= max_bc_payload
;
7401 args
->bc_attrs
.max_resp_sz
= max_bc_payload
;
7402 args
->bc_attrs
.max_resp_sz_cached
= 0;
7403 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
7404 args
->bc_attrs
.max_reqs
= NFS41_BC_MAX_CALLBACKS
;
7406 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7407 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7409 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
7410 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
7411 args
->bc_attrs
.max_reqs
);
7414 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
,
7415 struct nfs41_create_session_res
*res
)
7417 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
7418 struct nfs4_channel_attrs
*rcvd
= &res
->fc_attrs
;
7420 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
7423 * Our requested max_ops is the minimum we need; we're not
7424 * prepared to break up compounds into smaller pieces than that.
7425 * So, no point even trying to continue if the server won't
7428 if (rcvd
->max_ops
< sent
->max_ops
)
7430 if (rcvd
->max_reqs
== 0)
7432 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
7433 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
7437 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
,
7438 struct nfs41_create_session_res
*res
)
7440 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
7441 struct nfs4_channel_attrs
*rcvd
= &res
->bc_attrs
;
7443 if (!(res
->flags
& SESSION4_BACK_CHAN
))
7445 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
7447 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
7449 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
7451 /* These would render the backchannel useless: */
7452 if (rcvd
->max_ops
!= sent
->max_ops
)
7454 if (rcvd
->max_reqs
!= sent
->max_reqs
)
7460 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
7461 struct nfs41_create_session_res
*res
)
7465 ret
= nfs4_verify_fore_channel_attrs(args
, res
);
7468 return nfs4_verify_back_channel_attrs(args
, res
);
7471 static void nfs4_update_session(struct nfs4_session
*session
,
7472 struct nfs41_create_session_res
*res
)
7474 nfs4_copy_sessionid(&session
->sess_id
, &res
->sessionid
);
7475 /* Mark client id and session as being confirmed */
7476 session
->clp
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
7477 set_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
);
7478 session
->flags
= res
->flags
;
7479 memcpy(&session
->fc_attrs
, &res
->fc_attrs
, sizeof(session
->fc_attrs
));
7480 if (res
->flags
& SESSION4_BACK_CHAN
)
7481 memcpy(&session
->bc_attrs
, &res
->bc_attrs
,
7482 sizeof(session
->bc_attrs
));
7485 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
7486 struct rpc_cred
*cred
)
7488 struct nfs4_session
*session
= clp
->cl_session
;
7489 struct nfs41_create_session_args args
= {
7491 .clientid
= clp
->cl_clientid
,
7492 .seqid
= clp
->cl_seqid
,
7493 .cb_program
= NFS4_CALLBACK
,
7495 struct nfs41_create_session_res res
;
7497 struct rpc_message msg
= {
7498 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
7505 nfs4_init_channel_attrs(&args
, clp
->cl_rpcclient
);
7506 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
7508 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7509 trace_nfs4_create_session(clp
, status
);
7512 /* Verify the session's negotiated channel_attrs values */
7513 status
= nfs4_verify_channel_attrs(&args
, &res
);
7514 /* Increment the clientid slot sequence id */
7515 if (clp
->cl_seqid
== res
.seqid
)
7519 nfs4_update_session(session
, &res
);
7526 * Issues a CREATE_SESSION operation to the server.
7527 * It is the responsibility of the caller to verify the session is
7528 * expired before calling this routine.
7530 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7534 struct nfs4_session
*session
= clp
->cl_session
;
7536 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
7538 status
= _nfs4_proc_create_session(clp
, cred
);
7542 /* Init or reset the session slot tables */
7543 status
= nfs4_setup_session_slot_tables(session
);
7544 dprintk("slot table setup returned %d\n", status
);
7548 ptr
= (unsigned *)&session
->sess_id
.data
[0];
7549 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
7550 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
7552 dprintk("<-- %s\n", __func__
);
7557 * Issue the over-the-wire RPC DESTROY_SESSION.
7558 * The caller must serialize access to this routine.
7560 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
7561 struct rpc_cred
*cred
)
7563 struct rpc_message msg
= {
7564 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
7565 .rpc_argp
= session
,
7570 dprintk("--> nfs4_proc_destroy_session\n");
7572 /* session is still being setup */
7573 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED
, &session
->session_state
))
7576 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
7577 trace_nfs4_destroy_session(session
->clp
, status
);
7580 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7581 "Session has been destroyed regardless...\n", status
);
7583 dprintk("<-- nfs4_proc_destroy_session\n");
7588 * Renew the cl_session lease.
7590 struct nfs4_sequence_data
{
7591 struct nfs_client
*clp
;
7592 struct nfs4_sequence_args args
;
7593 struct nfs4_sequence_res res
;
7596 static void nfs41_sequence_release(void *data
)
7598 struct nfs4_sequence_data
*calldata
= data
;
7599 struct nfs_client
*clp
= calldata
->clp
;
7601 if (atomic_read(&clp
->cl_count
) > 1)
7602 nfs4_schedule_state_renewal(clp
);
7603 nfs_put_client(clp
);
7607 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
7609 switch(task
->tk_status
) {
7610 case -NFS4ERR_DELAY
:
7611 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
7614 nfs4_schedule_lease_recovery(clp
);
7619 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
7621 struct nfs4_sequence_data
*calldata
= data
;
7622 struct nfs_client
*clp
= calldata
->clp
;
7624 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
7627 trace_nfs4_sequence(clp
, task
->tk_status
);
7628 if (task
->tk_status
< 0) {
7629 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
7630 if (atomic_read(&clp
->cl_count
) == 1)
7633 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
7634 rpc_restart_call_prepare(task
);
7638 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
7640 dprintk("<-- %s\n", __func__
);
7643 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
7645 struct nfs4_sequence_data
*calldata
= data
;
7646 struct nfs_client
*clp
= calldata
->clp
;
7647 struct nfs4_sequence_args
*args
;
7648 struct nfs4_sequence_res
*res
;
7650 args
= task
->tk_msg
.rpc_argp
;
7651 res
= task
->tk_msg
.rpc_resp
;
7653 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
7656 static const struct rpc_call_ops nfs41_sequence_ops
= {
7657 .rpc_call_done
= nfs41_sequence_call_done
,
7658 .rpc_call_prepare
= nfs41_sequence_prepare
,
7659 .rpc_release
= nfs41_sequence_release
,
7662 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
7663 struct rpc_cred
*cred
,
7666 struct nfs4_sequence_data
*calldata
;
7667 struct rpc_message msg
= {
7668 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
7671 struct rpc_task_setup task_setup_data
= {
7672 .rpc_client
= clp
->cl_rpcclient
,
7673 .rpc_message
= &msg
,
7674 .callback_ops
= &nfs41_sequence_ops
,
7675 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
7678 if (!atomic_inc_not_zero(&clp
->cl_count
))
7679 return ERR_PTR(-EIO
);
7680 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7681 if (calldata
== NULL
) {
7682 nfs_put_client(clp
);
7683 return ERR_PTR(-ENOMEM
);
7685 nfs4_init_sequence(&calldata
->args
, &calldata
->res
, 0);
7687 nfs4_set_sequence_privileged(&calldata
->args
);
7688 msg
.rpc_argp
= &calldata
->args
;
7689 msg
.rpc_resp
= &calldata
->res
;
7690 calldata
->clp
= clp
;
7691 task_setup_data
.callback_data
= calldata
;
7693 return rpc_run_task(&task_setup_data
);
7696 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
7698 struct rpc_task
*task
;
7701 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
7703 task
= _nfs41_proc_sequence(clp
, cred
, false);
7705 ret
= PTR_ERR(task
);
7707 rpc_put_task_async(task
);
7708 dprintk("<-- %s status=%d\n", __func__
, ret
);
7712 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
7714 struct rpc_task
*task
;
7717 task
= _nfs41_proc_sequence(clp
, cred
, true);
7719 ret
= PTR_ERR(task
);
7722 ret
= rpc_wait_for_completion_task(task
);
7724 ret
= task
->tk_status
;
7727 dprintk("<-- %s status=%d\n", __func__
, ret
);
7731 struct nfs4_reclaim_complete_data
{
7732 struct nfs_client
*clp
;
7733 struct nfs41_reclaim_complete_args arg
;
7734 struct nfs41_reclaim_complete_res res
;
7737 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
7739 struct nfs4_reclaim_complete_data
*calldata
= data
;
7741 nfs41_setup_sequence(calldata
->clp
->cl_session
,
7742 &calldata
->arg
.seq_args
,
7743 &calldata
->res
.seq_res
,
7747 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
7749 switch(task
->tk_status
) {
7751 case -NFS4ERR_COMPLETE_ALREADY
:
7752 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
7754 case -NFS4ERR_DELAY
:
7755 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
7757 case -NFS4ERR_RETRY_UNCACHED_REP
:
7760 nfs4_schedule_lease_recovery(clp
);
7765 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
7767 struct nfs4_reclaim_complete_data
*calldata
= data
;
7768 struct nfs_client
*clp
= calldata
->clp
;
7769 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
7771 dprintk("--> %s\n", __func__
);
7772 if (!nfs41_sequence_done(task
, res
))
7775 trace_nfs4_reclaim_complete(clp
, task
->tk_status
);
7776 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
7777 rpc_restart_call_prepare(task
);
7780 dprintk("<-- %s\n", __func__
);
7783 static void nfs4_free_reclaim_complete_data(void *data
)
7785 struct nfs4_reclaim_complete_data
*calldata
= data
;
7790 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
7791 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
7792 .rpc_call_done
= nfs4_reclaim_complete_done
,
7793 .rpc_release
= nfs4_free_reclaim_complete_data
,
7797 * Issue a global reclaim complete.
7799 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
7800 struct rpc_cred
*cred
)
7802 struct nfs4_reclaim_complete_data
*calldata
;
7803 struct rpc_task
*task
;
7804 struct rpc_message msg
= {
7805 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
7808 struct rpc_task_setup task_setup_data
= {
7809 .rpc_client
= clp
->cl_rpcclient
,
7810 .rpc_message
= &msg
,
7811 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
7812 .flags
= RPC_TASK_ASYNC
,
7814 int status
= -ENOMEM
;
7816 dprintk("--> %s\n", __func__
);
7817 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
7818 if (calldata
== NULL
)
7820 calldata
->clp
= clp
;
7821 calldata
->arg
.one_fs
= 0;
7823 nfs4_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
7824 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
7825 msg
.rpc_argp
= &calldata
->arg
;
7826 msg
.rpc_resp
= &calldata
->res
;
7827 task_setup_data
.callback_data
= calldata
;
7828 task
= rpc_run_task(&task_setup_data
);
7830 status
= PTR_ERR(task
);
7833 status
= nfs4_wait_for_completion_rpc_task(task
);
7835 status
= task
->tk_status
;
7839 dprintk("<-- %s status=%d\n", __func__
, status
);
7844 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
7846 struct nfs4_layoutget
*lgp
= calldata
;
7847 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
7848 struct nfs4_session
*session
= nfs4_get_session(server
);
7850 dprintk("--> %s\n", __func__
);
7851 nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
7852 &lgp
->res
.seq_res
, task
);
7853 dprintk("<-- %s\n", __func__
);
7856 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
7858 struct nfs4_layoutget
*lgp
= calldata
;
7860 dprintk("--> %s\n", __func__
);
7861 nfs41_sequence_done(task
, &lgp
->res
.seq_res
);
7862 dprintk("<-- %s\n", __func__
);
7866 nfs4_layoutget_handle_exception(struct rpc_task
*task
,
7867 struct nfs4_layoutget
*lgp
, struct nfs4_exception
*exception
)
7869 struct inode
*inode
= lgp
->args
.inode
;
7870 struct nfs_server
*server
= NFS_SERVER(inode
);
7871 struct pnfs_layout_hdr
*lo
;
7872 int status
= task
->tk_status
;
7874 dprintk("--> %s tk_status => %d\n", __func__
, -task
->tk_status
);
7881 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7882 * on the file. set tk_status to -ENODATA to tell upper layer to
7885 case -NFS4ERR_LAYOUTUNAVAILABLE
:
7889 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7890 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7892 case -NFS4ERR_BADLAYOUT
:
7893 status
= -EOVERFLOW
;
7896 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7897 * (or clients) writing to the same RAID stripe except when
7898 * the minlength argument is 0 (see RFC5661 section 18.43.3).
7900 * Treat it like we would RECALLCONFLICT -- we retry for a little
7901 * while, and then eventually give up.
7903 case -NFS4ERR_LAYOUTTRYLATER
:
7904 if (lgp
->args
.minlength
== 0) {
7905 status
= -EOVERFLOW
;
7909 case -NFS4ERR_RECALLCONFLICT
:
7910 nfs4_handle_exception(server
, -NFS4ERR_RECALLCONFLICT
,
7912 status
= -ERECALLCONFLICT
;
7914 case -NFS4ERR_EXPIRED
:
7915 case -NFS4ERR_BAD_STATEID
:
7916 exception
->timeout
= 0;
7917 spin_lock(&inode
->i_lock
);
7918 if (nfs4_stateid_match(&lgp
->args
.stateid
,
7919 &lgp
->args
.ctx
->state
->stateid
)) {
7920 spin_unlock(&inode
->i_lock
);
7921 /* If the open stateid was bad, then recover it. */
7922 exception
->state
= lgp
->args
.ctx
->state
;
7925 lo
= NFS_I(inode
)->layout
;
7926 if (lo
&& !test_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
) &&
7927 nfs4_stateid_match_other(&lgp
->args
.stateid
, &lo
->plh_stateid
)) {
7931 * Mark the bad layout state as invalid, then retry
7932 * with the current stateid.
7934 set_bit(NFS_LAYOUT_INVALID_STID
, &lo
->plh_flags
);
7935 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
, 0);
7936 spin_unlock(&inode
->i_lock
);
7937 pnfs_free_lseg_list(&head
);
7941 spin_unlock(&inode
->i_lock
);
7944 status
= nfs4_handle_exception(server
, status
, exception
);
7945 if (exception
->retry
)
7948 dprintk("<-- %s\n", __func__
);
7952 static size_t max_response_pages(struct nfs_server
*server
)
7954 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
7955 return nfs_page_array_len(0, max_resp_sz
);
7958 static void nfs4_free_pages(struct page
**pages
, size_t size
)
7965 for (i
= 0; i
< size
; i
++) {
7968 __free_page(pages
[i
]);
7973 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
7975 struct page
**pages
;
7978 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
7980 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
7984 for (i
= 0; i
< size
; i
++) {
7985 pages
[i
] = alloc_page(gfp_flags
);
7987 dprintk("%s: failed to allocate page\n", __func__
);
7988 nfs4_free_pages(pages
, size
);
7996 static void nfs4_layoutget_release(void *calldata
)
7998 struct nfs4_layoutget
*lgp
= calldata
;
7999 struct inode
*inode
= lgp
->args
.inode
;
8000 struct nfs_server
*server
= NFS_SERVER(inode
);
8001 size_t max_pages
= max_response_pages(server
);
8003 dprintk("--> %s\n", __func__
);
8004 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
8005 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
8006 put_nfs_open_context(lgp
->args
.ctx
);
8008 dprintk("<-- %s\n", __func__
);
8011 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
8012 .rpc_call_prepare
= nfs4_layoutget_prepare
,
8013 .rpc_call_done
= nfs4_layoutget_done
,
8014 .rpc_release
= nfs4_layoutget_release
,
8017 struct pnfs_layout_segment
*
8018 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, long *timeout
, gfp_t gfp_flags
)
8020 struct inode
*inode
= lgp
->args
.inode
;
8021 struct nfs_server
*server
= NFS_SERVER(inode
);
8022 size_t max_pages
= max_response_pages(server
);
8023 struct rpc_task
*task
;
8024 struct rpc_message msg
= {
8025 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
8026 .rpc_argp
= &lgp
->args
,
8027 .rpc_resp
= &lgp
->res
,
8028 .rpc_cred
= lgp
->cred
,
8030 struct rpc_task_setup task_setup_data
= {
8031 .rpc_client
= server
->client
,
8032 .rpc_message
= &msg
,
8033 .callback_ops
= &nfs4_layoutget_call_ops
,
8034 .callback_data
= lgp
,
8035 .flags
= RPC_TASK_ASYNC
,
8037 struct pnfs_layout_segment
*lseg
= NULL
;
8038 struct nfs4_exception exception
= {
8040 .timeout
= *timeout
,
8044 dprintk("--> %s\n", __func__
);
8046 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8047 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
8049 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
8050 if (!lgp
->args
.layout
.pages
) {
8051 nfs4_layoutget_release(lgp
);
8052 return ERR_PTR(-ENOMEM
);
8054 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
8056 lgp
->res
.layoutp
= &lgp
->args
.layout
;
8057 lgp
->res
.seq_res
.sr_slot
= NULL
;
8058 nfs4_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
8060 task
= rpc_run_task(&task_setup_data
);
8062 return ERR_CAST(task
);
8063 status
= nfs4_wait_for_completion_rpc_task(task
);
8065 status
= nfs4_layoutget_handle_exception(task
, lgp
, &exception
);
8066 *timeout
= exception
.timeout
;
8069 trace_nfs4_layoutget(lgp
->args
.ctx
,
8075 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8076 if (status
== 0 && lgp
->res
.layoutp
->len
)
8077 lseg
= pnfs_layout_process(lgp
);
8079 dprintk("<-- %s status=%d\n", __func__
, status
);
8081 return ERR_PTR(status
);
8086 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
8088 struct nfs4_layoutreturn
*lrp
= calldata
;
8090 dprintk("--> %s\n", __func__
);
8091 nfs41_setup_sequence(lrp
->clp
->cl_session
,
8092 &lrp
->args
.seq_args
,
8097 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
8099 struct nfs4_layoutreturn
*lrp
= calldata
;
8100 struct nfs_server
*server
;
8102 dprintk("--> %s\n", __func__
);
8104 if (!nfs41_sequence_done(task
, &lrp
->res
.seq_res
))
8107 server
= NFS_SERVER(lrp
->args
.inode
);
8108 switch (task
->tk_status
) {
8110 task
->tk_status
= 0;
8113 case -NFS4ERR_DELAY
:
8114 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) != -EAGAIN
)
8116 rpc_restart_call_prepare(task
);
8119 dprintk("<-- %s\n", __func__
);
8122 static void nfs4_layoutreturn_release(void *calldata
)
8124 struct nfs4_layoutreturn
*lrp
= calldata
;
8125 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
8128 dprintk("--> %s\n", __func__
);
8129 spin_lock(&lo
->plh_inode
->i_lock
);
8130 pnfs_mark_matching_lsegs_invalid(lo
, &freeme
, &lrp
->args
.range
,
8131 be32_to_cpu(lrp
->args
.stateid
.seqid
));
8132 pnfs_mark_layout_returned_if_empty(lo
);
8133 if (lrp
->res
.lrs_present
)
8134 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
8135 pnfs_clear_layoutreturn_waitbit(lo
);
8136 spin_unlock(&lo
->plh_inode
->i_lock
);
8137 pnfs_free_lseg_list(&freeme
);
8138 pnfs_put_layout_hdr(lrp
->args
.layout
);
8139 nfs_iput_and_deactive(lrp
->inode
);
8141 dprintk("<-- %s\n", __func__
);
8144 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
8145 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
8146 .rpc_call_done
= nfs4_layoutreturn_done
,
8147 .rpc_release
= nfs4_layoutreturn_release
,
8150 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
, bool sync
)
8152 struct rpc_task
*task
;
8153 struct rpc_message msg
= {
8154 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
8155 .rpc_argp
= &lrp
->args
,
8156 .rpc_resp
= &lrp
->res
,
8157 .rpc_cred
= lrp
->cred
,
8159 struct rpc_task_setup task_setup_data
= {
8160 .rpc_client
= NFS_SERVER(lrp
->args
.inode
)->client
,
8161 .rpc_message
= &msg
,
8162 .callback_ops
= &nfs4_layoutreturn_call_ops
,
8163 .callback_data
= lrp
,
8167 nfs4_state_protect(NFS_SERVER(lrp
->args
.inode
)->nfs_client
,
8168 NFS_SP4_MACH_CRED_PNFS_CLEANUP
,
8169 &task_setup_data
.rpc_client
, &msg
);
8171 dprintk("--> %s\n", __func__
);
8173 lrp
->inode
= nfs_igrab_and_active(lrp
->args
.inode
);
8175 nfs4_layoutreturn_release(lrp
);
8178 task_setup_data
.flags
|= RPC_TASK_ASYNC
;
8180 nfs4_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
8181 task
= rpc_run_task(&task_setup_data
);
8183 return PTR_ERR(task
);
8185 status
= task
->tk_status
;
8186 trace_nfs4_layoutreturn(lrp
->args
.inode
, &lrp
->args
.stateid
, status
);
8187 dprintk("<-- %s status=%d\n", __func__
, status
);
8193 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8194 struct pnfs_device
*pdev
,
8195 struct rpc_cred
*cred
)
8197 struct nfs4_getdeviceinfo_args args
= {
8199 .notify_types
= NOTIFY_DEVICEID4_CHANGE
|
8200 NOTIFY_DEVICEID4_DELETE
,
8202 struct nfs4_getdeviceinfo_res res
= {
8205 struct rpc_message msg
= {
8206 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
8213 dprintk("--> %s\n", __func__
);
8214 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
8215 if (res
.notification
& ~args
.notify_types
)
8216 dprintk("%s: unsupported notification\n", __func__
);
8217 if (res
.notification
!= args
.notify_types
)
8220 dprintk("<-- %s status=%d\n", __func__
, status
);
8225 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
8226 struct pnfs_device
*pdev
,
8227 struct rpc_cred
*cred
)
8229 struct nfs4_exception exception
= { };
8233 err
= nfs4_handle_exception(server
,
8234 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
8236 } while (exception
.retry
);
8239 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
8241 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
8243 struct nfs4_layoutcommit_data
*data
= calldata
;
8244 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8245 struct nfs4_session
*session
= nfs4_get_session(server
);
8247 nfs41_setup_sequence(session
,
8248 &data
->args
.seq_args
,
8254 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
8256 struct nfs4_layoutcommit_data
*data
= calldata
;
8257 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
8259 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
8262 switch (task
->tk_status
) { /* Just ignore these failures */
8263 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
8264 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
8265 case -NFS4ERR_BADLAYOUT
: /* no layout */
8266 case -NFS4ERR_GRACE
: /* loca_recalim always false */
8267 task
->tk_status
= 0;
8271 if (nfs4_async_handle_error(task
, server
, NULL
, NULL
) == -EAGAIN
) {
8272 rpc_restart_call_prepare(task
);
8278 static void nfs4_layoutcommit_release(void *calldata
)
8280 struct nfs4_layoutcommit_data
*data
= calldata
;
8282 pnfs_cleanup_layoutcommit(data
);
8283 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
8285 put_rpccred(data
->cred
);
8286 nfs_iput_and_deactive(data
->inode
);
8290 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
8291 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
8292 .rpc_call_done
= nfs4_layoutcommit_done
,
8293 .rpc_release
= nfs4_layoutcommit_release
,
8297 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
8299 struct rpc_message msg
= {
8300 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
8301 .rpc_argp
= &data
->args
,
8302 .rpc_resp
= &data
->res
,
8303 .rpc_cred
= data
->cred
,
8305 struct rpc_task_setup task_setup_data
= {
8306 .task
= &data
->task
,
8307 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
8308 .rpc_message
= &msg
,
8309 .callback_ops
= &nfs4_layoutcommit_ops
,
8310 .callback_data
= data
,
8312 struct rpc_task
*task
;
8315 dprintk("NFS: initiating layoutcommit call. sync %d "
8316 "lbw: %llu inode %lu\n", sync
,
8317 data
->args
.lastbytewritten
,
8318 data
->args
.inode
->i_ino
);
8321 data
->inode
= nfs_igrab_and_active(data
->args
.inode
);
8322 if (data
->inode
== NULL
) {
8323 nfs4_layoutcommit_release(data
);
8326 task_setup_data
.flags
= RPC_TASK_ASYNC
;
8328 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
8329 task
= rpc_run_task(&task_setup_data
);
8331 return PTR_ERR(task
);
8333 status
= task
->tk_status
;
8334 trace_nfs4_layoutcommit(data
->args
.inode
, &data
->args
.stateid
, status
);
8335 dprintk("%s: status %d\n", __func__
, status
);
8341 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8342 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8345 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8346 struct nfs_fsinfo
*info
,
8347 struct nfs4_secinfo_flavors
*flavors
, bool use_integrity
)
8349 struct nfs41_secinfo_no_name_args args
= {
8350 .style
= SECINFO_STYLE_CURRENT_FH
,
8352 struct nfs4_secinfo_res res
= {
8355 struct rpc_message msg
= {
8356 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
8360 struct rpc_clnt
*clnt
= server
->client
;
8361 struct rpc_cred
*cred
= NULL
;
8364 if (use_integrity
) {
8365 clnt
= server
->nfs_client
->cl_rpcclient
;
8366 cred
= nfs4_get_clid_cred(server
->nfs_client
);
8367 msg
.rpc_cred
= cred
;
8370 dprintk("--> %s\n", __func__
);
8371 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
,
8373 dprintk("<-- %s status=%d\n", __func__
, status
);
8382 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8383 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
8385 struct nfs4_exception exception
= { };
8388 /* first try using integrity protection */
8389 err
= -NFS4ERR_WRONGSEC
;
8391 /* try to use integrity protection with machine cred */
8392 if (_nfs4_is_integrity_protected(server
->nfs_client
))
8393 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8397 * if unable to use integrity protection, or SECINFO with
8398 * integrity protection returns NFS4ERR_WRONGSEC (which is
8399 * disallowed by spec, but exists in deployed servers) use
8400 * the current filesystem's rpc_client and the user cred.
8402 if (err
== -NFS4ERR_WRONGSEC
)
8403 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
,
8408 case -NFS4ERR_WRONGSEC
:
8412 err
= nfs4_handle_exception(server
, err
, &exception
);
8414 } while (exception
.retry
);
8420 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
8421 struct nfs_fsinfo
*info
)
8425 rpc_authflavor_t flavor
= RPC_AUTH_MAXFLAVOR
;
8426 struct nfs4_secinfo_flavors
*flavors
;
8427 struct nfs4_secinfo4
*secinfo
;
8430 page
= alloc_page(GFP_KERNEL
);
8436 flavors
= page_address(page
);
8437 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
8440 * Fall back on "guess and check" method if
8441 * the server doesn't support SECINFO_NO_NAME
8443 if (err
== -NFS4ERR_WRONGSEC
|| err
== -ENOTSUPP
) {
8444 err
= nfs4_find_root_sec(server
, fhandle
, info
);
8450 for (i
= 0; i
< flavors
->num_flavors
; i
++) {
8451 secinfo
= &flavors
->flavors
[i
];
8453 switch (secinfo
->flavor
) {
8457 flavor
= rpcauth_get_pseudoflavor(secinfo
->flavor
,
8458 &secinfo
->flavor_info
);
8461 flavor
= RPC_AUTH_MAXFLAVOR
;
8465 if (!nfs_auth_info_match(&server
->auth_info
, flavor
))
8466 flavor
= RPC_AUTH_MAXFLAVOR
;
8468 if (flavor
!= RPC_AUTH_MAXFLAVOR
) {
8469 err
= nfs4_lookup_root_sec(server
, fhandle
,
8476 if (flavor
== RPC_AUTH_MAXFLAVOR
)
8487 static int _nfs41_test_stateid(struct nfs_server
*server
,
8488 nfs4_stateid
*stateid
,
8489 struct rpc_cred
*cred
)
8492 struct nfs41_test_stateid_args args
= {
8495 struct nfs41_test_stateid_res res
;
8496 struct rpc_message msg
= {
8497 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
8502 struct rpc_clnt
*rpc_client
= server
->client
;
8504 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
8507 dprintk("NFS call test_stateid %p\n", stateid
);
8508 nfs4_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
8509 nfs4_set_sequence_privileged(&args
.seq_args
);
8510 status
= nfs4_call_sync_sequence(rpc_client
, server
, &msg
,
8511 &args
.seq_args
, &res
.seq_res
);
8512 if (status
!= NFS_OK
) {
8513 dprintk("NFS reply test_stateid: failed, %d\n", status
);
8516 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
8521 * nfs41_test_stateid - perform a TEST_STATEID operation
8523 * @server: server / transport on which to perform the operation
8524 * @stateid: state ID to test
8527 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8528 * Otherwise a negative NFS4ERR value is returned if the operation
8529 * failed or the state ID is not currently valid.
8531 static int nfs41_test_stateid(struct nfs_server
*server
,
8532 nfs4_stateid
*stateid
,
8533 struct rpc_cred
*cred
)
8535 struct nfs4_exception exception
= { };
8538 err
= _nfs41_test_stateid(server
, stateid
, cred
);
8539 if (err
!= -NFS4ERR_DELAY
)
8541 nfs4_handle_exception(server
, err
, &exception
);
8542 } while (exception
.retry
);
8546 struct nfs_free_stateid_data
{
8547 struct nfs_server
*server
;
8548 struct nfs41_free_stateid_args args
;
8549 struct nfs41_free_stateid_res res
;
8552 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
8554 struct nfs_free_stateid_data
*data
= calldata
;
8555 nfs41_setup_sequence(nfs4_get_session(data
->server
),
8556 &data
->args
.seq_args
,
8561 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
8563 struct nfs_free_stateid_data
*data
= calldata
;
8565 nfs41_sequence_done(task
, &data
->res
.seq_res
);
8567 switch (task
->tk_status
) {
8568 case -NFS4ERR_DELAY
:
8569 if (nfs4_async_handle_error(task
, data
->server
, NULL
, NULL
) == -EAGAIN
)
8570 rpc_restart_call_prepare(task
);
8574 static void nfs41_free_stateid_release(void *calldata
)
8579 static const struct rpc_call_ops nfs41_free_stateid_ops
= {
8580 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
8581 .rpc_call_done
= nfs41_free_stateid_done
,
8582 .rpc_release
= nfs41_free_stateid_release
,
8585 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
8586 nfs4_stateid
*stateid
,
8587 struct rpc_cred
*cred
,
8590 struct rpc_message msg
= {
8591 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
8594 struct rpc_task_setup task_setup
= {
8595 .rpc_client
= server
->client
,
8596 .rpc_message
= &msg
,
8597 .callback_ops
= &nfs41_free_stateid_ops
,
8598 .flags
= RPC_TASK_ASYNC
,
8600 struct nfs_free_stateid_data
*data
;
8602 nfs4_state_protect(server
->nfs_client
, NFS_SP4_MACH_CRED_STATEID
,
8603 &task_setup
.rpc_client
, &msg
);
8605 dprintk("NFS call free_stateid %p\n", stateid
);
8606 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
8608 return ERR_PTR(-ENOMEM
);
8609 data
->server
= server
;
8610 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
8612 task_setup
.callback_data
= data
;
8614 msg
.rpc_argp
= &data
->args
;
8615 msg
.rpc_resp
= &data
->res
;
8616 nfs4_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
8618 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
8620 return rpc_run_task(&task_setup
);
8624 * nfs41_free_stateid - perform a FREE_STATEID operation
8626 * @server: server / transport on which to perform the operation
8627 * @stateid: state ID to release
8630 * Returns NFS_OK if the server freed "stateid". Otherwise a
8631 * negative NFS4ERR value is returned.
8633 static int nfs41_free_stateid(struct nfs_server
*server
,
8634 nfs4_stateid
*stateid
,
8635 struct rpc_cred
*cred
)
8637 struct rpc_task
*task
;
8640 task
= _nfs41_free_stateid(server
, stateid
, cred
, true);
8642 return PTR_ERR(task
);
8643 ret
= rpc_wait_for_completion_task(task
);
8645 ret
= task
->tk_status
;
8651 nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
8653 struct rpc_task
*task
;
8654 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
8656 task
= _nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
8657 nfs4_free_lock_state(server
, lsp
);
8663 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
8664 const nfs4_stateid
*s2
)
8666 if (s1
->type
!= s2
->type
)
8669 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
8672 if (s1
->seqid
== s2
->seqid
)
8674 if (s1
->seqid
== 0 || s2
->seqid
== 0)
8680 #endif /* CONFIG_NFS_V4_1 */
8682 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
8683 const nfs4_stateid
*s2
)
8685 return nfs4_stateid_match(s1
, s2
);
8689 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
8690 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
8691 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
8692 .recover_open
= nfs4_open_reclaim
,
8693 .recover_lock
= nfs4_lock_reclaim
,
8694 .establish_clid
= nfs4_init_clientid
,
8695 .detect_trunking
= nfs40_discover_server_trunking
,
8698 #if defined(CONFIG_NFS_V4_1)
8699 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
8700 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
8701 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
8702 .recover_open
= nfs4_open_reclaim
,
8703 .recover_lock
= nfs4_lock_reclaim
,
8704 .establish_clid
= nfs41_init_clientid
,
8705 .reclaim_complete
= nfs41_proc_reclaim_complete
,
8706 .detect_trunking
= nfs41_discover_server_trunking
,
8708 #endif /* CONFIG_NFS_V4_1 */
8710 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
8711 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
8712 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
8713 .recover_open
= nfs40_open_expired
,
8714 .recover_lock
= nfs4_lock_expired
,
8715 .establish_clid
= nfs4_init_clientid
,
8718 #if defined(CONFIG_NFS_V4_1)
8719 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
8720 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
8721 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
8722 .recover_open
= nfs41_open_expired
,
8723 .recover_lock
= nfs41_lock_expired
,
8724 .establish_clid
= nfs41_init_clientid
,
8726 #endif /* CONFIG_NFS_V4_1 */
8728 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
8729 .sched_state_renewal
= nfs4_proc_async_renew
,
8730 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
8731 .renew_lease
= nfs4_proc_renew
,
8734 #if defined(CONFIG_NFS_V4_1)
8735 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
8736 .sched_state_renewal
= nfs41_proc_async_sequence
,
8737 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
8738 .renew_lease
= nfs4_proc_sequence
,
8742 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops
= {
8743 .get_locations
= _nfs40_proc_get_locations
,
8744 .fsid_present
= _nfs40_proc_fsid_present
,
8747 #if defined(CONFIG_NFS_V4_1)
8748 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops
= {
8749 .get_locations
= _nfs41_proc_get_locations
,
8750 .fsid_present
= _nfs41_proc_fsid_present
,
8752 #endif /* CONFIG_NFS_V4_1 */
8754 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
8756 .init_caps
= NFS_CAP_READDIRPLUS
8757 | NFS_CAP_ATOMIC_OPEN
8758 | NFS_CAP_POSIX_LOCK
,
8759 .init_client
= nfs40_init_client
,
8760 .shutdown_client
= nfs40_shutdown_client
,
8761 .match_stateid
= nfs4_match_stateid
,
8762 .find_root_sec
= nfs4_find_root_sec
,
8763 .free_lock_state
= nfs4_release_lockowner
,
8764 .alloc_seqid
= nfs_alloc_seqid
,
8765 .call_sync_ops
= &nfs40_call_sync_ops
,
8766 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
8767 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
8768 .state_renewal_ops
= &nfs40_state_renewal_ops
,
8769 .mig_recovery_ops
= &nfs40_mig_recovery_ops
,
8772 #if defined(CONFIG_NFS_V4_1)
8773 static struct nfs_seqid
*
8774 nfs_alloc_no_seqid(struct nfs_seqid_counter
*arg1
, gfp_t arg2
)
8779 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
8781 .init_caps
= NFS_CAP_READDIRPLUS
8782 | NFS_CAP_ATOMIC_OPEN
8783 | NFS_CAP_POSIX_LOCK
8784 | NFS_CAP_STATEID_NFSV41
8785 | NFS_CAP_ATOMIC_OPEN_V1
,
8786 .init_client
= nfs41_init_client
,
8787 .shutdown_client
= nfs41_shutdown_client
,
8788 .match_stateid
= nfs41_match_stateid
,
8789 .find_root_sec
= nfs41_find_root_sec
,
8790 .free_lock_state
= nfs41_free_lock_state
,
8791 .alloc_seqid
= nfs_alloc_no_seqid
,
8792 .call_sync_ops
= &nfs41_call_sync_ops
,
8793 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
8794 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
8795 .state_renewal_ops
= &nfs41_state_renewal_ops
,
8796 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
8800 #if defined(CONFIG_NFS_V4_2)
8801 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
8803 .init_caps
= NFS_CAP_READDIRPLUS
8804 | NFS_CAP_ATOMIC_OPEN
8805 | NFS_CAP_POSIX_LOCK
8806 | NFS_CAP_STATEID_NFSV41
8807 | NFS_CAP_ATOMIC_OPEN_V1
8810 | NFS_CAP_DEALLOCATE
8812 | NFS_CAP_LAYOUTSTATS
8814 .init_client
= nfs41_init_client
,
8815 .shutdown_client
= nfs41_shutdown_client
,
8816 .match_stateid
= nfs41_match_stateid
,
8817 .find_root_sec
= nfs41_find_root_sec
,
8818 .free_lock_state
= nfs41_free_lock_state
,
8819 .call_sync_ops
= &nfs41_call_sync_ops
,
8820 .alloc_seqid
= nfs_alloc_no_seqid
,
8821 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
8822 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
8823 .state_renewal_ops
= &nfs41_state_renewal_ops
,
8824 .mig_recovery_ops
= &nfs41_mig_recovery_ops
,
8828 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
8829 [0] = &nfs_v4_0_minor_ops
,
8830 #if defined(CONFIG_NFS_V4_1)
8831 [1] = &nfs_v4_1_minor_ops
,
8833 #if defined(CONFIG_NFS_V4_2)
8834 [2] = &nfs_v4_2_minor_ops
,
8838 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
8840 ssize_t error
, error2
;
8842 error
= generic_listxattr(dentry
, list
, size
);
8850 error2
= nfs4_listxattr_nfs4_label(d_inode(dentry
), list
, size
);
8853 return error
+ error2
;
8856 static const struct inode_operations nfs4_dir_inode_operations
= {
8857 .create
= nfs_create
,
8858 .lookup
= nfs_lookup
,
8859 .atomic_open
= nfs_atomic_open
,
8861 .unlink
= nfs_unlink
,
8862 .symlink
= nfs_symlink
,
8866 .rename
= nfs_rename
,
8867 .permission
= nfs_permission
,
8868 .getattr
= nfs_getattr
,
8869 .setattr
= nfs_setattr
,
8870 .getxattr
= generic_getxattr
,
8871 .setxattr
= generic_setxattr
,
8872 .listxattr
= nfs4_listxattr
,
8873 .removexattr
= generic_removexattr
,
8876 static const struct inode_operations nfs4_file_inode_operations
= {
8877 .permission
= nfs_permission
,
8878 .getattr
= nfs_getattr
,
8879 .setattr
= nfs_setattr
,
8880 .getxattr
= generic_getxattr
,
8881 .setxattr
= generic_setxattr
,
8882 .listxattr
= nfs4_listxattr
,
8883 .removexattr
= generic_removexattr
,
8886 const struct nfs_rpc_ops nfs_v4_clientops
= {
8887 .version
= 4, /* protocol version */
8888 .dentry_ops
= &nfs4_dentry_operations
,
8889 .dir_inode_ops
= &nfs4_dir_inode_operations
,
8890 .file_inode_ops
= &nfs4_file_inode_operations
,
8891 .file_ops
= &nfs4_file_operations
,
8892 .getroot
= nfs4_proc_get_root
,
8893 .submount
= nfs4_submount
,
8894 .try_mount
= nfs4_try_mount
,
8895 .getattr
= nfs4_proc_getattr
,
8896 .setattr
= nfs4_proc_setattr
,
8897 .lookup
= nfs4_proc_lookup
,
8898 .access
= nfs4_proc_access
,
8899 .readlink
= nfs4_proc_readlink
,
8900 .create
= nfs4_proc_create
,
8901 .remove
= nfs4_proc_remove
,
8902 .unlink_setup
= nfs4_proc_unlink_setup
,
8903 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
8904 .unlink_done
= nfs4_proc_unlink_done
,
8905 .rename_setup
= nfs4_proc_rename_setup
,
8906 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
8907 .rename_done
= nfs4_proc_rename_done
,
8908 .link
= nfs4_proc_link
,
8909 .symlink
= nfs4_proc_symlink
,
8910 .mkdir
= nfs4_proc_mkdir
,
8911 .rmdir
= nfs4_proc_remove
,
8912 .readdir
= nfs4_proc_readdir
,
8913 .mknod
= nfs4_proc_mknod
,
8914 .statfs
= nfs4_proc_statfs
,
8915 .fsinfo
= nfs4_proc_fsinfo
,
8916 .pathconf
= nfs4_proc_pathconf
,
8917 .set_capabilities
= nfs4_server_capabilities
,
8918 .decode_dirent
= nfs4_decode_dirent
,
8919 .pgio_rpc_prepare
= nfs4_proc_pgio_rpc_prepare
,
8920 .read_setup
= nfs4_proc_read_setup
,
8921 .read_done
= nfs4_read_done
,
8922 .write_setup
= nfs4_proc_write_setup
,
8923 .write_done
= nfs4_write_done
,
8924 .commit_setup
= nfs4_proc_commit_setup
,
8925 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
8926 .commit_done
= nfs4_commit_done
,
8927 .lock
= nfs4_proc_lock
,
8928 .clear_acl_cache
= nfs4_zap_acl_attr
,
8929 .close_context
= nfs4_close_context
,
8930 .open_context
= nfs4_atomic_open
,
8931 .have_delegation
= nfs4_have_delegation
,
8932 .return_delegation
= nfs4_inode_return_delegation
,
8933 .alloc_client
= nfs4_alloc_client
,
8934 .init_client
= nfs4_init_client
,
8935 .free_client
= nfs4_free_client
,
8936 .create_server
= nfs4_create_server
,
8937 .clone_server
= nfs_clone_server
,
8940 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
8941 .name
= XATTR_NAME_NFSV4_ACL
,
8942 .list
= nfs4_xattr_list_nfs4_acl
,
8943 .get
= nfs4_xattr_get_nfs4_acl
,
8944 .set
= nfs4_xattr_set_nfs4_acl
,
8947 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
8948 &nfs4_xattr_nfs4_acl_handler
,
8949 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8950 &nfs4_xattr_nfs4_label_handler
,