4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
60 #include "delegation.h"
66 #include "nfs4session.h"
69 #define NFSDBG_FACILITY NFSDBG_PROC
71 #define NFS4_POLL_RETRY_MIN (HZ/10)
72 #define NFS4_POLL_RETRY_MAX (15*HZ)
75 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
76 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
77 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
78 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
79 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
80 static int nfs4_proc_getattr(struct nfs_server
*, struct nfs_fh
*, struct nfs_fattr
*, struct nfs4_label
*label
);
81 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
, struct nfs4_label
*label
);
82 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
83 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
84 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
85 struct nfs4_label
*olabel
);
86 #ifdef CONFIG_NFS_V4_1
87 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*,
89 static int nfs41_free_stateid(struct nfs_server
*, nfs4_stateid
*,
93 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
94 static inline struct nfs4_label
*
95 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
96 struct iattr
*sattr
, struct nfs4_label
*label
)
103 if (nfs_server_capable(dir
, NFS_CAP_SECURITY_LABEL
) == 0)
106 if (NFS_SERVER(dir
)->nfs_client
->cl_minorversion
< 2)
109 err
= security_dentry_init_security(dentry
, sattr
->ia_mode
,
110 &dentry
->d_name
, (void **)&label
->label
, &label
->len
);
117 nfs4_label_release_security(struct nfs4_label
*label
)
120 security_release_secctx(label
->label
, label
->len
);
122 static inline u32
*nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
125 return server
->attr_bitmask
;
127 return server
->attr_bitmask_nl
;
130 static inline struct nfs4_label
*
131 nfs4_label_init_security(struct inode
*dir
, struct dentry
*dentry
,
132 struct iattr
*sattr
, struct nfs4_label
*l
)
135 nfs4_label_release_security(struct nfs4_label
*label
)
138 nfs4_bitmask(struct nfs_server
*server
, struct nfs4_label
*label
)
139 { return server
->attr_bitmask
; }
142 /* Prevent leaks of NFSv4 errors into userland */
143 static int nfs4_map_errors(int err
)
148 case -NFS4ERR_RESOURCE
:
149 case -NFS4ERR_LAYOUTTRYLATER
:
150 case -NFS4ERR_RECALLCONFLICT
:
152 case -NFS4ERR_WRONGSEC
:
154 case -NFS4ERR_BADOWNER
:
155 case -NFS4ERR_BADNAME
:
157 case -NFS4ERR_SHARE_DENIED
:
159 case -NFS4ERR_MINOR_VERS_MISMATCH
:
160 return -EPROTONOSUPPORT
;
161 case -NFS4ERR_ACCESS
:
163 case -NFS4ERR_FILE_OPEN
:
166 dprintk("%s could not handle NFSv4 error %d\n",
174 * This is our standard bitmap for GETATTR requests.
176 const u32 nfs4_fattr_bitmap
[3] = {
178 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_FILEID
,
183 | FATTR4_WORD1_NUMLINKS
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
190 | FATTR4_WORD1_TIME_MODIFY
,
191 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
196 static const u32 nfs4_pnfs_open_bitmap
[3] = {
198 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_FILEID
,
203 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY
,
211 FATTR4_WORD2_MDSTHRESHOLD
214 static const u32 nfs4_open_noattr_bitmap
[3] = {
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID
,
220 const u32 nfs4_statfs_bitmap
[3] = {
221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL
,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
229 const u32 nfs4_pathconf_bitmap
[3] = {
231 | FATTR4_WORD0_MAXNAME
,
235 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME
,
239 FATTR4_WORD1_TIME_DELTA
240 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
244 const u32 nfs4_fs_locations_bitmap
[3] = {
246 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS
,
252 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
260 | FATTR4_WORD1_MOUNTED_ON_FILEID
,
263 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
264 struct nfs4_readdir_arg
*readdir
)
269 readdir
->cookie
= cookie
;
270 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
275 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
286 start
= p
= kmap_atomic(*readdir
->pages
);
289 *p
++ = xdr_one
; /* next */
290 *p
++ = xdr_zero
; /* cookie, first word */
291 *p
++ = xdr_one
; /* cookie, second word */
292 *p
++ = xdr_one
; /* entry len */
293 memcpy(p
, ".\0\0\0", 4); /* entry */
295 *p
++ = xdr_one
; /* bitmap length */
296 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
297 *p
++ = htonl(8); /* attribute buffer length */
298 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
301 *p
++ = xdr_one
; /* next */
302 *p
++ = xdr_zero
; /* cookie, first word */
303 *p
++ = xdr_two
; /* cookie, second word */
304 *p
++ = xdr_two
; /* entry len */
305 memcpy(p
, "..\0\0", 4); /* entry */
307 *p
++ = xdr_one
; /* bitmap length */
308 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
309 *p
++ = htonl(8); /* attribute buffer length */
310 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
312 readdir
->pgbase
= (char *)p
- (char *)start
;
313 readdir
->count
-= readdir
->pgbase
;
314 kunmap_atomic(start
);
317 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
324 *timeout
= NFS4_POLL_RETRY_MIN
;
325 if (*timeout
> NFS4_POLL_RETRY_MAX
)
326 *timeout
= NFS4_POLL_RETRY_MAX
;
327 freezable_schedule_timeout_killable_unsafe(*timeout
);
328 if (fatal_signal_pending(current
))
334 /* This is the error handling routine for processes that are allowed
337 static int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
339 struct nfs_client
*clp
= server
->nfs_client
;
340 struct nfs4_state
*state
= exception
->state
;
341 struct inode
*inode
= exception
->inode
;
344 exception
->retry
= 0;
348 case -NFS4ERR_OPENMODE
:
349 if (inode
&& nfs4_have_delegation(inode
, FMODE_READ
)) {
350 nfs4_inode_return_delegation(inode
);
351 exception
->retry
= 1;
356 ret
= nfs4_schedule_stateid_recovery(server
, state
);
359 goto wait_on_recovery
;
360 case -NFS4ERR_DELEG_REVOKED
:
361 case -NFS4ERR_ADMIN_REVOKED
:
362 case -NFS4ERR_BAD_STATEID
:
363 if (inode
!= NULL
&& nfs4_have_delegation(inode
, FMODE_READ
)) {
364 nfs_remove_bad_delegation(inode
);
365 exception
->retry
= 1;
370 ret
= nfs4_schedule_stateid_recovery(server
, state
);
373 goto wait_on_recovery
;
374 case -NFS4ERR_EXPIRED
:
376 ret
= nfs4_schedule_stateid_recovery(server
, state
);
380 case -NFS4ERR_STALE_STATEID
:
381 case -NFS4ERR_STALE_CLIENTID
:
382 nfs4_schedule_lease_recovery(clp
);
383 goto wait_on_recovery
;
384 #if defined(CONFIG_NFS_V4_1)
385 case -NFS4ERR_BADSESSION
:
386 case -NFS4ERR_BADSLOT
:
387 case -NFS4ERR_BAD_HIGH_SLOT
:
388 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
389 case -NFS4ERR_DEADSESSION
:
390 case -NFS4ERR_SEQ_FALSE_RETRY
:
391 case -NFS4ERR_SEQ_MISORDERED
:
392 dprintk("%s ERROR: %d Reset session\n", __func__
,
394 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
395 goto wait_on_recovery
;
396 #endif /* defined(CONFIG_NFS_V4_1) */
397 case -NFS4ERR_FILE_OPEN
:
398 if (exception
->timeout
> HZ
) {
399 /* We have retried a decent amount, time to
407 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
410 case -NFS4ERR_RETRY_UNCACHED_REP
:
411 case -NFS4ERR_OLD_STATEID
:
412 exception
->retry
= 1;
414 case -NFS4ERR_BADOWNER
:
415 /* The following works around a Linux server bug! */
416 case -NFS4ERR_BADNAME
:
417 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
418 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
419 exception
->retry
= 1;
420 printk(KERN_WARNING
"NFS: v4 server %s "
421 "does not accept raw "
423 "Reenabling the idmapper.\n",
424 server
->nfs_client
->cl_hostname
);
427 /* We failed to handle the error */
428 return nfs4_map_errors(ret
);
430 ret
= nfs4_wait_clnt_recover(clp
);
432 exception
->retry
= 1;
437 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
439 spin_lock(&clp
->cl_lock
);
440 if (time_before(clp
->cl_last_renewal
,timestamp
))
441 clp
->cl_last_renewal
= timestamp
;
442 spin_unlock(&clp
->cl_lock
);
445 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
447 do_renew_lease(server
->nfs_client
, timestamp
);
450 #if defined(CONFIG_NFS_V4_1)
452 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
454 struct nfs4_session
*session
;
455 struct nfs4_slot_table
*tbl
;
456 bool send_new_highest_used_slotid
= false;
459 /* just wake up the next guy waiting since
460 * we may have not consumed a slot after all */
461 dprintk("%s: No slot\n", __func__
);
464 tbl
= res
->sr_slot
->table
;
465 session
= tbl
->session
;
467 spin_lock(&tbl
->slot_tbl_lock
);
468 /* Be nice to the server: try to ensure that the last transmitted
469 * value for highest_user_slotid <= target_highest_slotid
471 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
472 send_new_highest_used_slotid
= true;
474 if (nfs41_wake_and_assign_slot(tbl
, res
->sr_slot
)) {
475 send_new_highest_used_slotid
= false;
478 nfs4_free_slot(tbl
, res
->sr_slot
);
480 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
481 send_new_highest_used_slotid
= false;
483 spin_unlock(&tbl
->slot_tbl_lock
);
485 if (send_new_highest_used_slotid
)
486 nfs41_server_notify_highest_slotid_update(session
->clp
);
489 static int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
491 struct nfs4_session
*session
;
492 struct nfs4_slot
*slot
;
493 struct nfs_client
*clp
;
494 bool interrupted
= false;
497 /* don't increment the sequence number if the task wasn't sent */
498 if (!RPC_WAS_SENT(task
))
502 session
= slot
->table
->session
;
504 if (slot
->interrupted
) {
505 slot
->interrupted
= 0;
509 /* Check the SEQUENCE operation status */
510 switch (res
->sr_status
) {
512 /* Update the slot's sequence and clientid lease timer */
515 do_renew_lease(clp
, res
->sr_timestamp
);
516 /* Check sequence flags */
517 if (res
->sr_status_flags
!= 0)
518 nfs4_schedule_lease_recovery(clp
);
519 nfs41_update_target_slotid(slot
->table
, slot
, res
);
523 * sr_status remains 1 if an RPC level error occurred.
524 * The server may or may not have processed the sequence
526 * Mark the slot as having hosted an interrupted RPC call.
528 slot
->interrupted
= 1;
531 /* The server detected a resend of the RPC call and
532 * returned NFS4ERR_DELAY as per Section 2.10.6.2
535 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
540 case -NFS4ERR_BADSLOT
:
542 * The slot id we used was probably retired. Try again
543 * using a different slot id.
546 case -NFS4ERR_SEQ_MISORDERED
:
548 * Was the last operation on this sequence interrupted?
549 * If so, retry after bumping the sequence number.
556 * Could this slot have been previously retired?
557 * If so, then the server may be expecting seq_nr = 1!
559 if (slot
->seq_nr
!= 1) {
564 case -NFS4ERR_SEQ_FALSE_RETRY
:
568 /* Just update the slot sequence no. */
572 /* The session may be reset by one of the error handlers. */
573 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
574 nfs41_sequence_free_slot(res
);
577 if (rpc_restart_call_prepare(task
)) {
583 if (!rpc_restart_call(task
))
585 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
589 static int nfs4_sequence_done(struct rpc_task
*task
,
590 struct nfs4_sequence_res
*res
)
592 if (res
->sr_slot
== NULL
)
594 return nfs41_sequence_done(task
, res
);
597 static void nfs41_init_sequence(struct nfs4_sequence_args
*args
,
598 struct nfs4_sequence_res
*res
, int cache_reply
)
600 args
->sa_slot
= NULL
;
601 args
->sa_cache_this
= 0;
602 args
->sa_privileged
= 0;
604 args
->sa_cache_this
= 1;
608 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
610 args
->sa_privileged
= 1;
613 int nfs41_setup_sequence(struct nfs4_session
*session
,
614 struct nfs4_sequence_args
*args
,
615 struct nfs4_sequence_res
*res
,
616 struct rpc_task
*task
)
618 struct nfs4_slot
*slot
;
619 struct nfs4_slot_table
*tbl
;
621 dprintk("--> %s\n", __func__
);
622 /* slot already allocated? */
623 if (res
->sr_slot
!= NULL
)
626 tbl
= &session
->fc_slot_table
;
628 task
->tk_timeout
= 0;
630 spin_lock(&tbl
->slot_tbl_lock
);
631 if (test_bit(NFS4_SLOT_TBL_DRAINING
, &tbl
->slot_tbl_state
) &&
632 !args
->sa_privileged
) {
633 /* The state manager will wait until the slot table is empty */
634 dprintk("%s session is draining\n", __func__
);
638 slot
= nfs4_alloc_slot(tbl
);
640 /* If out of memory, try again in 1/4 second */
641 if (slot
== ERR_PTR(-ENOMEM
))
642 task
->tk_timeout
= HZ
>> 2;
643 dprintk("<-- %s: no free slots\n", __func__
);
646 spin_unlock(&tbl
->slot_tbl_lock
);
648 args
->sa_slot
= slot
;
650 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
,
651 slot
->slot_nr
, slot
->seq_nr
);
654 res
->sr_timestamp
= jiffies
;
655 res
->sr_status_flags
= 0;
657 * sr_status is only set in decode_sequence, and so will remain
658 * set to 1 if an rpc level failure occurs.
662 rpc_call_start(task
);
665 /* Privileged tasks are queued with top priority */
666 if (args
->sa_privileged
)
667 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
668 NULL
, RPC_PRIORITY_PRIVILEGED
);
670 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
671 spin_unlock(&tbl
->slot_tbl_lock
);
674 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
676 int nfs4_setup_sequence(const struct nfs_server
*server
,
677 struct nfs4_sequence_args
*args
,
678 struct nfs4_sequence_res
*res
,
679 struct rpc_task
*task
)
681 struct nfs4_session
*session
= nfs4_get_session(server
);
684 if (session
== NULL
) {
685 rpc_call_start(task
);
689 dprintk("--> %s clp %p session %p sr_slot %d\n",
690 __func__
, session
->clp
, session
, res
->sr_slot
?
691 res
->sr_slot
->slot_nr
: -1);
693 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
695 dprintk("<-- %s status=%d\n", __func__
, ret
);
699 struct nfs41_call_sync_data
{
700 const struct nfs_server
*seq_server
;
701 struct nfs4_sequence_args
*seq_args
;
702 struct nfs4_sequence_res
*seq_res
;
705 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
707 struct nfs41_call_sync_data
*data
= calldata
;
708 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
710 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
712 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
715 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
717 struct nfs41_call_sync_data
*data
= calldata
;
719 nfs41_sequence_done(task
, data
->seq_res
);
722 static const struct rpc_call_ops nfs41_call_sync_ops
= {
723 .rpc_call_prepare
= nfs41_call_sync_prepare
,
724 .rpc_call_done
= nfs41_call_sync_done
,
727 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
728 struct nfs_server
*server
,
729 struct rpc_message
*msg
,
730 struct nfs4_sequence_args
*args
,
731 struct nfs4_sequence_res
*res
)
734 struct rpc_task
*task
;
735 struct nfs41_call_sync_data data
= {
736 .seq_server
= server
,
740 struct rpc_task_setup task_setup
= {
743 .callback_ops
= &nfs41_call_sync_ops
,
744 .callback_data
= &data
747 task
= rpc_run_task(&task_setup
);
751 ret
= task
->tk_status
;
759 void nfs41_init_sequence(struct nfs4_sequence_args
*args
,
760 struct nfs4_sequence_res
*res
, int cache_reply
)
764 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
769 static int nfs4_sequence_done(struct rpc_task
*task
,
770 struct nfs4_sequence_res
*res
)
774 #endif /* CONFIG_NFS_V4_1 */
777 int _nfs4_call_sync(struct rpc_clnt
*clnt
,
778 struct nfs_server
*server
,
779 struct rpc_message
*msg
,
780 struct nfs4_sequence_args
*args
,
781 struct nfs4_sequence_res
*res
)
783 return rpc_call_sync(clnt
, msg
, 0);
787 int nfs4_call_sync(struct rpc_clnt
*clnt
,
788 struct nfs_server
*server
,
789 struct rpc_message
*msg
,
790 struct nfs4_sequence_args
*args
,
791 struct nfs4_sequence_res
*res
,
794 nfs41_init_sequence(args
, res
, cache_reply
);
795 return server
->nfs_client
->cl_mvops
->call_sync(clnt
, server
, msg
,
799 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
801 struct nfs_inode
*nfsi
= NFS_I(dir
);
803 spin_lock(&dir
->i_lock
);
804 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
805 if (!cinfo
->atomic
|| cinfo
->before
!= dir
->i_version
)
806 nfs_force_lookup_revalidate(dir
);
807 dir
->i_version
= cinfo
->after
;
808 nfs_fscache_invalidate(dir
);
809 spin_unlock(&dir
->i_lock
);
812 struct nfs4_opendata
{
814 struct nfs_openargs o_arg
;
815 struct nfs_openres o_res
;
816 struct nfs_open_confirmargs c_arg
;
817 struct nfs_open_confirmres c_res
;
818 struct nfs4_string owner_name
;
819 struct nfs4_string group_name
;
820 struct nfs_fattr f_attr
;
821 struct nfs4_label
*f_label
;
823 struct dentry
*dentry
;
824 struct nfs4_state_owner
*owner
;
825 struct nfs4_state
*state
;
827 unsigned long timestamp
;
828 unsigned int rpc_done
: 1;
829 unsigned int is_recover
: 1;
834 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server
*server
,
835 int err
, struct nfs4_exception
*exception
)
839 if (!(server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
))
841 server
->caps
&= ~NFS_CAP_ATOMIC_OPEN_V1
;
842 exception
->retry
= 1;
846 static enum open_claim_type4
847 nfs4_map_atomic_open_claim(struct nfs_server
*server
,
848 enum open_claim_type4 claim
)
850 if (server
->caps
& NFS_CAP_ATOMIC_OPEN_V1
)
855 case NFS4_OPEN_CLAIM_FH
:
856 return NFS4_OPEN_CLAIM_NULL
;
857 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
858 return NFS4_OPEN_CLAIM_DELEGATE_CUR
;
859 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
860 return NFS4_OPEN_CLAIM_DELEGATE_PREV
;
864 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
866 p
->o_res
.f_attr
= &p
->f_attr
;
867 p
->o_res
.f_label
= p
->f_label
;
868 p
->o_res
.seqid
= p
->o_arg
.seqid
;
869 p
->c_res
.seqid
= p
->c_arg
.seqid
;
870 p
->o_res
.server
= p
->o_arg
.server
;
871 p
->o_res
.access_request
= p
->o_arg
.access
;
872 nfs_fattr_init(&p
->f_attr
);
873 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
876 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
877 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
878 const struct iattr
*attrs
,
879 struct nfs4_label
*label
,
880 enum open_claim_type4 claim
,
883 struct dentry
*parent
= dget_parent(dentry
);
884 struct inode
*dir
= parent
->d_inode
;
885 struct nfs_server
*server
= NFS_SERVER(dir
);
886 struct nfs4_opendata
*p
;
888 p
= kzalloc(sizeof(*p
), gfp_mask
);
892 p
->f_label
= nfs4_label_alloc(server
, gfp_mask
);
893 if (IS_ERR(p
->f_label
))
896 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
897 if (p
->o_arg
.seqid
== NULL
)
899 nfs_sb_active(dentry
->d_sb
);
900 p
->dentry
= dget(dentry
);
903 atomic_inc(&sp
->so_count
);
904 p
->o_arg
.open_flags
= flags
;
905 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
906 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
907 * will return permission denied for all bits until close */
908 if (!(flags
& O_EXCL
)) {
909 /* ask server to check for all possible rights as results
911 p
->o_arg
.access
= NFS4_ACCESS_READ
| NFS4_ACCESS_MODIFY
|
912 NFS4_ACCESS_EXTEND
| NFS4_ACCESS_EXECUTE
;
914 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
915 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
916 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
917 p
->o_arg
.name
= &dentry
->d_name
;
918 p
->o_arg
.server
= server
;
919 p
->o_arg
.bitmask
= nfs4_bitmask(server
, label
);
920 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
921 p
->o_arg
.label
= label
;
922 p
->o_arg
.claim
= nfs4_map_atomic_open_claim(server
, claim
);
923 switch (p
->o_arg
.claim
) {
924 case NFS4_OPEN_CLAIM_NULL
:
925 case NFS4_OPEN_CLAIM_DELEGATE_CUR
:
926 case NFS4_OPEN_CLAIM_DELEGATE_PREV
:
927 p
->o_arg
.fh
= NFS_FH(dir
);
929 case NFS4_OPEN_CLAIM_PREVIOUS
:
930 case NFS4_OPEN_CLAIM_FH
:
931 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
932 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
933 p
->o_arg
.fh
= NFS_FH(dentry
->d_inode
);
935 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
938 p
->o_arg
.u
.attrs
= &p
->attrs
;
939 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
942 verf
[1] = current
->pid
;
943 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
944 sizeof(p
->o_arg
.u
.verifier
.data
));
946 p
->c_arg
.fh
= &p
->o_res
.fh
;
947 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
948 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
949 nfs4_init_opendata_res(p
);
954 nfs4_label_free(p
->f_label
);
962 static void nfs4_opendata_free(struct kref
*kref
)
964 struct nfs4_opendata
*p
= container_of(kref
,
965 struct nfs4_opendata
, kref
);
966 struct super_block
*sb
= p
->dentry
->d_sb
;
968 nfs_free_seqid(p
->o_arg
.seqid
);
969 if (p
->state
!= NULL
)
970 nfs4_put_open_state(p
->state
);
971 nfs4_put_state_owner(p
->owner
);
973 nfs4_label_free(p
->f_label
);
978 nfs_fattr_free_names(&p
->f_attr
);
982 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
985 kref_put(&p
->kref
, nfs4_opendata_free
);
988 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
992 ret
= rpc_wait_for_completion_task(task
);
996 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
1000 if (open_mode
& (O_EXCL
|O_TRUNC
))
1002 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1004 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
1005 && state
->n_rdonly
!= 0;
1008 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
1009 && state
->n_wronly
!= 0;
1011 case FMODE_READ
|FMODE_WRITE
:
1012 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
1013 && state
->n_rdwr
!= 0;
1019 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
1021 if (delegation
== NULL
)
1023 if ((delegation
->type
& fmode
) != fmode
)
1025 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
1027 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
1029 nfs_mark_delegation_referenced(delegation
);
1033 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
1042 case FMODE_READ
|FMODE_WRITE
:
1045 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
1048 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1050 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1051 nfs4_stateid_copy(&state
->stateid
, stateid
);
1052 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
1053 set_bit(NFS_OPEN_STATE
, &state
->flags
);
1056 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1059 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1061 case FMODE_READ
|FMODE_WRITE
:
1062 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1066 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
1068 write_seqlock(&state
->seqlock
);
1069 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
1070 write_sequnlock(&state
->seqlock
);
1073 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
1076 * Protect the call to nfs4_state_set_mode_locked and
1077 * serialise the stateid update
1079 write_seqlock(&state
->seqlock
);
1080 if (deleg_stateid
!= NULL
) {
1081 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
1082 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1084 if (open_stateid
!= NULL
)
1085 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
1086 write_sequnlock(&state
->seqlock
);
1087 spin_lock(&state
->owner
->so_lock
);
1088 update_open_stateflags(state
, fmode
);
1089 spin_unlock(&state
->owner
->so_lock
);
1092 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
1094 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1095 struct nfs_delegation
*deleg_cur
;
1098 fmode
&= (FMODE_READ
|FMODE_WRITE
);
1101 deleg_cur
= rcu_dereference(nfsi
->delegation
);
1102 if (deleg_cur
== NULL
)
1105 spin_lock(&deleg_cur
->lock
);
1106 if (nfsi
->delegation
!= deleg_cur
||
1107 test_bit(NFS_DELEGATION_RETURNING
, &deleg_cur
->flags
) ||
1108 (deleg_cur
->type
& fmode
) != fmode
)
1109 goto no_delegation_unlock
;
1111 if (delegation
== NULL
)
1112 delegation
= &deleg_cur
->stateid
;
1113 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
1114 goto no_delegation_unlock
;
1116 nfs_mark_delegation_referenced(deleg_cur
);
1117 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
1119 no_delegation_unlock
:
1120 spin_unlock(&deleg_cur
->lock
);
1124 if (!ret
&& open_stateid
!= NULL
) {
1125 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
1133 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1135 struct nfs_delegation
*delegation
;
1138 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1139 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1144 nfs4_inode_return_delegation(inode
);
1147 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1149 struct nfs4_state
*state
= opendata
->state
;
1150 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1151 struct nfs_delegation
*delegation
;
1152 int open_mode
= opendata
->o_arg
.open_flags
;
1153 fmode_t fmode
= opendata
->o_arg
.fmode
;
1154 nfs4_stateid stateid
;
1158 if (can_open_cached(state
, fmode
, open_mode
)) {
1159 spin_lock(&state
->owner
->so_lock
);
1160 if (can_open_cached(state
, fmode
, open_mode
)) {
1161 update_open_stateflags(state
, fmode
);
1162 spin_unlock(&state
->owner
->so_lock
);
1163 goto out_return_state
;
1165 spin_unlock(&state
->owner
->so_lock
);
1168 delegation
= rcu_dereference(nfsi
->delegation
);
1169 if (!can_open_delegated(delegation
, fmode
)) {
1173 /* Save the delegation */
1174 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1176 nfs_release_seqid(opendata
->o_arg
.seqid
);
1177 if (!opendata
->is_recover
) {
1178 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1184 /* Try to update the stateid using the delegation */
1185 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1186 goto out_return_state
;
1189 return ERR_PTR(ret
);
1191 atomic_inc(&state
->count
);
1196 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1198 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1199 struct nfs_delegation
*delegation
;
1200 int delegation_flags
= 0;
1203 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1205 delegation_flags
= delegation
->flags
;
1207 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_DELEGATE_CUR
) {
1208 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1209 "returning a delegation for "
1210 "OPEN(CLAIM_DELEGATE_CUR)\n",
1212 } else if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1213 nfs_inode_set_delegation(state
->inode
,
1214 data
->owner
->so_cred
,
1217 nfs_inode_reclaim_delegation(state
->inode
,
1218 data
->owner
->so_cred
,
1223 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1224 * and update the nfs4_state.
1226 static struct nfs4_state
*
1227 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1229 struct inode
*inode
= data
->state
->inode
;
1230 struct nfs4_state
*state
= data
->state
;
1233 if (!data
->rpc_done
) {
1234 ret
= data
->rpc_status
;
1239 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR_TYPE
) ||
1240 !(data
->f_attr
.valid
& NFS_ATTR_FATTR_FILEID
) ||
1241 !(data
->f_attr
.valid
& NFS_ATTR_FATTR_CHANGE
))
1245 state
= nfs4_get_open_state(inode
, data
->owner
);
1249 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1253 nfs_setsecurity(inode
, &data
->f_attr
, data
->f_label
);
1255 if (data
->o_res
.delegation_type
!= 0)
1256 nfs4_opendata_check_deleg(data
, state
);
1257 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1262 return ERR_PTR(ret
);
1266 static struct nfs4_state
*
1267 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1269 struct inode
*inode
;
1270 struct nfs4_state
*state
= NULL
;
1273 if (!data
->rpc_done
) {
1274 state
= nfs4_try_open_cached(data
);
1279 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1281 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
, data
->f_label
);
1282 ret
= PTR_ERR(inode
);
1286 state
= nfs4_get_open_state(inode
, data
->owner
);
1289 if (data
->o_res
.delegation_type
!= 0)
1290 nfs4_opendata_check_deleg(data
, state
);
1291 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1295 nfs_release_seqid(data
->o_arg
.seqid
);
1300 return ERR_PTR(ret
);
1303 static struct nfs4_state
*
1304 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1306 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1307 return _nfs4_opendata_reclaim_to_nfs4_state(data
);
1308 return _nfs4_opendata_to_nfs4_state(data
);
1311 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1313 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1314 struct nfs_open_context
*ctx
;
1316 spin_lock(&state
->inode
->i_lock
);
1317 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1318 if (ctx
->state
!= state
)
1320 get_nfs_open_context(ctx
);
1321 spin_unlock(&state
->inode
->i_lock
);
1324 spin_unlock(&state
->inode
->i_lock
);
1325 return ERR_PTR(-ENOENT
);
1328 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
,
1329 struct nfs4_state
*state
, enum open_claim_type4 claim
)
1331 struct nfs4_opendata
*opendata
;
1333 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0,
1334 NULL
, NULL
, claim
, GFP_NOFS
);
1335 if (opendata
== NULL
)
1336 return ERR_PTR(-ENOMEM
);
1337 opendata
->state
= state
;
1338 atomic_inc(&state
->count
);
1342 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1344 struct nfs4_state
*newstate
;
1347 opendata
->o_arg
.open_flags
= 0;
1348 opendata
->o_arg
.fmode
= fmode
;
1349 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1350 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1351 nfs4_init_opendata_res(opendata
);
1352 ret
= _nfs4_recover_proc_open(opendata
);
1355 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1356 if (IS_ERR(newstate
))
1357 return PTR_ERR(newstate
);
1358 nfs4_close_state(newstate
, fmode
);
1363 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1365 struct nfs4_state
*newstate
;
1368 /* memory barrier prior to reading state->n_* */
1369 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1370 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
1372 if (state
->n_rdwr
!= 0) {
1373 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1374 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1377 if (newstate
!= state
)
1380 if (state
->n_wronly
!= 0) {
1381 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1382 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1385 if (newstate
!= state
)
1388 if (state
->n_rdonly
!= 0) {
1389 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1390 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1393 if (newstate
!= state
)
1397 * We may have performed cached opens for all three recoveries.
1398 * Check if we need to update the current stateid.
1400 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1401 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1402 write_seqlock(&state
->seqlock
);
1403 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1404 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1405 write_sequnlock(&state
->seqlock
);
1412 * reclaim state on the server after a reboot.
1414 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1416 struct nfs_delegation
*delegation
;
1417 struct nfs4_opendata
*opendata
;
1418 fmode_t delegation_type
= 0;
1421 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1422 NFS4_OPEN_CLAIM_PREVIOUS
);
1423 if (IS_ERR(opendata
))
1424 return PTR_ERR(opendata
);
1426 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1427 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1428 delegation_type
= delegation
->type
;
1430 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1431 status
= nfs4_open_recover(opendata
, state
);
1432 nfs4_opendata_put(opendata
);
1436 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1438 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1439 struct nfs4_exception exception
= { };
1442 err
= _nfs4_do_open_reclaim(ctx
, state
);
1443 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1445 if (err
!= -NFS4ERR_DELAY
)
1447 nfs4_handle_exception(server
, err
, &exception
);
1448 } while (exception
.retry
);
1452 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1454 struct nfs_open_context
*ctx
;
1457 ctx
= nfs4_state_find_open_context(state
);
1460 ret
= nfs4_do_open_reclaim(ctx
, state
);
1461 put_nfs_open_context(ctx
);
1465 static int nfs4_handle_delegation_recall_error(struct nfs_server
*server
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
, int err
)
1469 printk(KERN_ERR
"NFS: %s: unhandled error "
1470 "%d.\n", __func__
, err
);
1475 case -NFS4ERR_BADSESSION
:
1476 case -NFS4ERR_BADSLOT
:
1477 case -NFS4ERR_BAD_HIGH_SLOT
:
1478 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1479 case -NFS4ERR_DEADSESSION
:
1480 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1481 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1483 case -NFS4ERR_STALE_CLIENTID
:
1484 case -NFS4ERR_STALE_STATEID
:
1485 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1486 case -NFS4ERR_EXPIRED
:
1487 /* Don't recall a delegation if it was lost */
1488 nfs4_schedule_lease_recovery(server
->nfs_client
);
1490 case -NFS4ERR_DELEG_REVOKED
:
1491 case -NFS4ERR_ADMIN_REVOKED
:
1492 case -NFS4ERR_BAD_STATEID
:
1493 case -NFS4ERR_OPENMODE
:
1494 nfs_inode_find_state_and_recover(state
->inode
,
1496 nfs4_schedule_stateid_recovery(server
, state
);
1498 case -NFS4ERR_DELAY
:
1499 case -NFS4ERR_GRACE
:
1500 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1504 case -NFS4ERR_DENIED
:
1505 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1511 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1513 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1514 struct nfs4_opendata
*opendata
;
1517 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1518 NFS4_OPEN_CLAIM_DELEG_CUR_FH
);
1519 if (IS_ERR(opendata
))
1520 return PTR_ERR(opendata
);
1521 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
1522 err
= nfs4_open_recover(opendata
, state
);
1523 nfs4_opendata_put(opendata
);
1524 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
1527 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1529 struct nfs4_opendata
*data
= calldata
;
1531 data
->rpc_status
= task
->tk_status
;
1532 if (data
->rpc_status
== 0) {
1533 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
1534 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1535 renew_lease(data
->o_res
.server
, data
->timestamp
);
1540 static void nfs4_open_confirm_release(void *calldata
)
1542 struct nfs4_opendata
*data
= calldata
;
1543 struct nfs4_state
*state
= NULL
;
1545 /* If this request hasn't been cancelled, do nothing */
1546 if (data
->cancelled
== 0)
1548 /* In case of error, no cleanup! */
1549 if (!data
->rpc_done
)
1551 state
= nfs4_opendata_to_nfs4_state(data
);
1553 nfs4_close_state(state
, data
->o_arg
.fmode
);
1555 nfs4_opendata_put(data
);
1558 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1559 .rpc_call_done
= nfs4_open_confirm_done
,
1560 .rpc_release
= nfs4_open_confirm_release
,
1564 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1566 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1568 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1569 struct rpc_task
*task
;
1570 struct rpc_message msg
= {
1571 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1572 .rpc_argp
= &data
->c_arg
,
1573 .rpc_resp
= &data
->c_res
,
1574 .rpc_cred
= data
->owner
->so_cred
,
1576 struct rpc_task_setup task_setup_data
= {
1577 .rpc_client
= server
->client
,
1578 .rpc_message
= &msg
,
1579 .callback_ops
= &nfs4_open_confirm_ops
,
1580 .callback_data
= data
,
1581 .workqueue
= nfsiod_workqueue
,
1582 .flags
= RPC_TASK_ASYNC
,
1586 kref_get(&data
->kref
);
1588 data
->rpc_status
= 0;
1589 data
->timestamp
= jiffies
;
1590 task
= rpc_run_task(&task_setup_data
);
1592 return PTR_ERR(task
);
1593 status
= nfs4_wait_for_completion_rpc_task(task
);
1595 data
->cancelled
= 1;
1598 status
= data
->rpc_status
;
1603 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1605 struct nfs4_opendata
*data
= calldata
;
1606 struct nfs4_state_owner
*sp
= data
->owner
;
1607 struct nfs_client
*clp
= sp
->so_server
->nfs_client
;
1609 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1612 * Check if we still need to send an OPEN call, or if we can use
1613 * a delegation instead.
1615 if (data
->state
!= NULL
) {
1616 struct nfs_delegation
*delegation
;
1618 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1621 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1622 if (data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
&&
1623 data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEG_CUR_FH
&&
1624 can_open_delegated(delegation
, data
->o_arg
.fmode
))
1625 goto unlock_no_action
;
1628 /* Update client id. */
1629 data
->o_arg
.clientid
= clp
->cl_clientid
;
1630 switch (data
->o_arg
.claim
) {
1631 case NFS4_OPEN_CLAIM_PREVIOUS
:
1632 case NFS4_OPEN_CLAIM_DELEG_CUR_FH
:
1633 case NFS4_OPEN_CLAIM_DELEG_PREV_FH
:
1634 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
1635 case NFS4_OPEN_CLAIM_FH
:
1636 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1637 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1639 data
->timestamp
= jiffies
;
1640 if (nfs4_setup_sequence(data
->o_arg
.server
,
1641 &data
->o_arg
.seq_args
,
1642 &data
->o_res
.seq_res
,
1644 nfs_release_seqid(data
->o_arg
.seqid
);
1646 /* Set the create mode (note dependency on the session type) */
1647 data
->o_arg
.createmode
= NFS4_CREATE_UNCHECKED
;
1648 if (data
->o_arg
.open_flags
& O_EXCL
) {
1649 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE
;
1650 if (nfs4_has_persistent_session(clp
))
1651 data
->o_arg
.createmode
= NFS4_CREATE_GUARDED
;
1652 else if (clp
->cl_mvops
->minor_version
> 0)
1653 data
->o_arg
.createmode
= NFS4_CREATE_EXCLUSIVE4_1
;
1659 task
->tk_action
= NULL
;
1661 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
1664 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1666 struct nfs4_opendata
*data
= calldata
;
1668 data
->rpc_status
= task
->tk_status
;
1670 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1673 if (task
->tk_status
== 0) {
1674 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
1675 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1679 data
->rpc_status
= -ELOOP
;
1682 data
->rpc_status
= -EISDIR
;
1685 data
->rpc_status
= -ENOTDIR
;
1688 renew_lease(data
->o_res
.server
, data
->timestamp
);
1689 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1690 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1695 static void nfs4_open_release(void *calldata
)
1697 struct nfs4_opendata
*data
= calldata
;
1698 struct nfs4_state
*state
= NULL
;
1700 /* If this request hasn't been cancelled, do nothing */
1701 if (data
->cancelled
== 0)
1703 /* In case of error, no cleanup! */
1704 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1706 /* In case we need an open_confirm, no cleanup! */
1707 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1709 state
= nfs4_opendata_to_nfs4_state(data
);
1711 nfs4_close_state(state
, data
->o_arg
.fmode
);
1713 nfs4_opendata_put(data
);
1716 static const struct rpc_call_ops nfs4_open_ops
= {
1717 .rpc_call_prepare
= nfs4_open_prepare
,
1718 .rpc_call_done
= nfs4_open_done
,
1719 .rpc_release
= nfs4_open_release
,
1722 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1724 struct inode
*dir
= data
->dir
->d_inode
;
1725 struct nfs_server
*server
= NFS_SERVER(dir
);
1726 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1727 struct nfs_openres
*o_res
= &data
->o_res
;
1728 struct rpc_task
*task
;
1729 struct rpc_message msg
= {
1730 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1733 .rpc_cred
= data
->owner
->so_cred
,
1735 struct rpc_task_setup task_setup_data
= {
1736 .rpc_client
= server
->client
,
1737 .rpc_message
= &msg
,
1738 .callback_ops
= &nfs4_open_ops
,
1739 .callback_data
= data
,
1740 .workqueue
= nfsiod_workqueue
,
1741 .flags
= RPC_TASK_ASYNC
,
1745 nfs41_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
1746 kref_get(&data
->kref
);
1748 data
->rpc_status
= 0;
1749 data
->cancelled
= 0;
1750 data
->is_recover
= 0;
1752 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
1753 data
->is_recover
= 1;
1755 task
= rpc_run_task(&task_setup_data
);
1757 return PTR_ERR(task
);
1758 status
= nfs4_wait_for_completion_rpc_task(task
);
1760 data
->cancelled
= 1;
1763 status
= data
->rpc_status
;
1769 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1771 struct inode
*dir
= data
->dir
->d_inode
;
1772 struct nfs_openres
*o_res
= &data
->o_res
;
1775 status
= nfs4_run_open_task(data
, 1);
1776 if (status
!= 0 || !data
->rpc_done
)
1779 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
1781 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1782 status
= _nfs4_proc_open_confirm(data
);
1790 static int nfs4_opendata_access(struct rpc_cred
*cred
,
1791 struct nfs4_opendata
*opendata
,
1792 struct nfs4_state
*state
, fmode_t fmode
,
1795 struct nfs_access_entry cache
;
1798 /* access call failed or for some reason the server doesn't
1799 * support any access modes -- defer access call until later */
1800 if (opendata
->o_res
.access_supported
== 0)
1804 /* don't check MAY_WRITE - a newly created file may not have
1805 * write mode bits, but POSIX allows the creating process to write.
1806 * use openflags to check for exec, because fmode won't
1807 * always have FMODE_EXEC set when file open for exec. */
1808 if (openflags
& __FMODE_EXEC
) {
1809 /* ONLY check for exec rights */
1811 } else if (fmode
& FMODE_READ
)
1815 cache
.jiffies
= jiffies
;
1816 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
1817 nfs_access_add_cache(state
->inode
, &cache
);
1819 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
1822 /* even though OPEN succeeded, access is denied. Close the file */
1823 nfs4_close_state(state
, fmode
);
1828 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1830 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1832 struct inode
*dir
= data
->dir
->d_inode
;
1833 struct nfs_server
*server
= NFS_SERVER(dir
);
1834 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1835 struct nfs_openres
*o_res
= &data
->o_res
;
1838 status
= nfs4_run_open_task(data
, 0);
1839 if (!data
->rpc_done
)
1842 if (status
== -NFS4ERR_BADNAME
&&
1843 !(o_arg
->open_flags
& O_CREAT
))
1848 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
1850 if (o_arg
->open_flags
& O_CREAT
)
1851 update_changeattr(dir
, &o_res
->cinfo
);
1852 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1853 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1854 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1855 status
= _nfs4_proc_open_confirm(data
);
1859 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1860 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
, o_res
->f_label
);
1864 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1866 return nfs4_client_recover_expired_lease(server
->nfs_client
);
1871 * reclaim state on the server after a network partition.
1872 * Assumes caller holds the appropriate lock
1874 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1876 struct nfs4_opendata
*opendata
;
1879 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
,
1880 NFS4_OPEN_CLAIM_FH
);
1881 if (IS_ERR(opendata
))
1882 return PTR_ERR(opendata
);
1883 ret
= nfs4_open_recover(opendata
, state
);
1885 d_drop(ctx
->dentry
);
1886 nfs4_opendata_put(opendata
);
1890 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1892 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1893 struct nfs4_exception exception
= { };
1897 err
= _nfs4_open_expired(ctx
, state
);
1898 if (nfs4_clear_cap_atomic_open_v1(server
, err
, &exception
))
1903 case -NFS4ERR_GRACE
:
1904 case -NFS4ERR_DELAY
:
1905 nfs4_handle_exception(server
, err
, &exception
);
1908 } while (exception
.retry
);
1913 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1915 struct nfs_open_context
*ctx
;
1918 ctx
= nfs4_state_find_open_context(state
);
1921 ret
= nfs4_do_open_expired(ctx
, state
);
1922 put_nfs_open_context(ctx
);
1926 #if defined(CONFIG_NFS_V4_1)
1927 static void nfs41_clear_delegation_stateid(struct nfs4_state
*state
)
1929 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1930 nfs4_stateid
*stateid
= &state
->stateid
;
1931 struct nfs_delegation
*delegation
;
1932 struct rpc_cred
*cred
= NULL
;
1933 int status
= -NFS4ERR_BAD_STATEID
;
1935 /* If a state reset has been done, test_stateid is unneeded */
1936 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1939 /* Get the delegation credential for use by test/free_stateid */
1941 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1942 if (delegation
!= NULL
&&
1943 nfs4_stateid_match(&delegation
->stateid
, stateid
)) {
1944 cred
= get_rpccred(delegation
->cred
);
1946 status
= nfs41_test_stateid(server
, stateid
, cred
);
1950 if (status
!= NFS_OK
) {
1951 /* Free the stateid unless the server explicitly
1952 * informs us the stateid is unrecognized. */
1953 if (status
!= -NFS4ERR_BAD_STATEID
)
1954 nfs41_free_stateid(server
, stateid
, cred
);
1955 nfs_remove_bad_delegation(state
->inode
);
1957 write_seqlock(&state
->seqlock
);
1958 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1959 write_sequnlock(&state
->seqlock
);
1960 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1968 * nfs41_check_open_stateid - possibly free an open stateid
1970 * @state: NFSv4 state for an inode
1972 * Returns NFS_OK if recovery for this stateid is now finished.
1973 * Otherwise a negative NFS4ERR value is returned.
1975 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
1977 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1978 nfs4_stateid
*stateid
= &state
->open_stateid
;
1979 struct rpc_cred
*cred
= state
->owner
->so_cred
;
1982 /* If a state reset has been done, test_stateid is unneeded */
1983 if ((test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) == 0) &&
1984 (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) == 0) &&
1985 (test_bit(NFS_O_RDWR_STATE
, &state
->flags
) == 0))
1986 return -NFS4ERR_BAD_STATEID
;
1988 status
= nfs41_test_stateid(server
, stateid
, cred
);
1989 if (status
!= NFS_OK
) {
1990 /* Free the stateid unless the server explicitly
1991 * informs us the stateid is unrecognized. */
1992 if (status
!= -NFS4ERR_BAD_STATEID
)
1993 nfs41_free_stateid(server
, stateid
, cred
);
1995 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1996 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1997 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1998 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2003 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
2007 nfs41_clear_delegation_stateid(state
);
2008 status
= nfs41_check_open_stateid(state
);
2009 if (status
!= NFS_OK
)
2010 status
= nfs4_open_expired(sp
, state
);
2016 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2017 * fields corresponding to attributes that were used to store the verifier.
2018 * Make sure we clobber those fields in the later setattr call
2020 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
2022 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
2023 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
2024 sattr
->ia_valid
|= ATTR_ATIME
;
2026 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
2027 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
2028 sattr
->ia_valid
|= ATTR_MTIME
;
2031 static int _nfs4_open_and_get_state(struct nfs4_opendata
*opendata
,
2034 struct nfs_open_context
*ctx
)
2036 struct nfs4_state_owner
*sp
= opendata
->owner
;
2037 struct nfs_server
*server
= sp
->so_server
;
2038 struct dentry
*dentry
;
2039 struct nfs4_state
*state
;
2043 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
2045 ret
= _nfs4_proc_open(opendata
);
2049 state
= nfs4_opendata_to_nfs4_state(opendata
);
2050 ret
= PTR_ERR(state
);
2053 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
2054 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
2056 dentry
= opendata
->dentry
;
2057 if (dentry
->d_inode
== NULL
) {
2058 /* FIXME: Is this d_drop() ever needed? */
2060 dentry
= d_add_unique(dentry
, igrab(state
->inode
));
2061 if (dentry
== NULL
) {
2062 dentry
= opendata
->dentry
;
2063 } else if (dentry
!= ctx
->dentry
) {
2065 ctx
->dentry
= dget(dentry
);
2067 nfs_set_verifier(dentry
,
2068 nfs_save_change_attribute(opendata
->dir
->d_inode
));
2071 ret
= nfs4_opendata_access(sp
->so_cred
, opendata
, state
, fmode
, flags
);
2076 if (dentry
->d_inode
== state
->inode
) {
2077 nfs_inode_attach_open_context(ctx
);
2078 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
2079 nfs4_schedule_stateid_recovery(server
, state
);
2086 * Returns a referenced nfs4_state
2088 static int _nfs4_do_open(struct inode
*dir
,
2089 struct nfs_open_context
*ctx
,
2091 struct iattr
*sattr
,
2092 struct nfs4_label
*label
)
2094 struct nfs4_state_owner
*sp
;
2095 struct nfs4_state
*state
= NULL
;
2096 struct nfs_server
*server
= NFS_SERVER(dir
);
2097 struct nfs4_opendata
*opendata
;
2098 struct dentry
*dentry
= ctx
->dentry
;
2099 struct rpc_cred
*cred
= ctx
->cred
;
2100 struct nfs4_threshold
**ctx_th
= &ctx
->mdsthreshold
;
2101 fmode_t fmode
= ctx
->mode
& (FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
);
2102 enum open_claim_type4 claim
= NFS4_OPEN_CLAIM_NULL
;
2103 struct nfs4_label
*olabel
= NULL
;
2106 /* Protect against reboot recovery conflicts */
2108 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
2110 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2113 status
= nfs4_recover_expired_lease(server
);
2115 goto err_put_state_owner
;
2116 if (dentry
->d_inode
!= NULL
)
2117 nfs4_return_incompatible_delegation(dentry
->d_inode
, fmode
);
2119 if (dentry
->d_inode
)
2120 claim
= NFS4_OPEN_CLAIM_FH
;
2121 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
,
2122 label
, claim
, GFP_KERNEL
);
2123 if (opendata
== NULL
)
2124 goto err_put_state_owner
;
2127 olabel
= nfs4_label_alloc(server
, GFP_KERNEL
);
2128 if (IS_ERR(olabel
)) {
2129 status
= PTR_ERR(olabel
);
2130 goto err_opendata_put
;
2134 if (ctx_th
&& server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
2135 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
2136 if (!opendata
->f_attr
.mdsthreshold
)
2137 goto err_free_label
;
2138 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
2140 if (dentry
->d_inode
!= NULL
)
2141 opendata
->state
= nfs4_get_open_state(dentry
->d_inode
, sp
);
2143 status
= _nfs4_open_and_get_state(opendata
, fmode
, flags
, ctx
);
2145 goto err_free_label
;
2148 if ((opendata
->o_arg
.open_flags
& O_EXCL
) &&
2149 (opendata
->o_arg
.createmode
!= NFS4_CREATE_GUARDED
)) {
2150 nfs4_exclusive_attrset(opendata
, sattr
);
2152 nfs_fattr_init(opendata
->o_res
.f_attr
);
2153 status
= nfs4_do_setattr(state
->inode
, cred
,
2154 opendata
->o_res
.f_attr
, sattr
,
2155 state
, label
, olabel
);
2157 nfs_setattr_update_inode(state
->inode
, sattr
);
2158 nfs_post_op_update_inode(state
->inode
, opendata
->o_res
.f_attr
);
2159 nfs_setsecurity(state
->inode
, opendata
->o_res
.f_attr
, olabel
);
2163 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
))
2164 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
2166 kfree(opendata
->f_attr
.mdsthreshold
);
2167 opendata
->f_attr
.mdsthreshold
= NULL
;
2169 nfs4_label_free(olabel
);
2171 nfs4_opendata_put(opendata
);
2172 nfs4_put_state_owner(sp
);
2175 nfs4_label_free(olabel
);
2177 kfree(opendata
->f_attr
.mdsthreshold
);
2178 nfs4_opendata_put(opendata
);
2179 err_put_state_owner
:
2180 nfs4_put_state_owner(sp
);
2186 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
2187 struct nfs_open_context
*ctx
,
2189 struct iattr
*sattr
,
2190 struct nfs4_label
*label
)
2192 struct nfs_server
*server
= NFS_SERVER(dir
);
2193 struct nfs4_exception exception
= { };
2194 struct nfs4_state
*res
;
2198 status
= _nfs4_do_open(dir
, ctx
, flags
, sattr
, label
);
2202 /* NOTE: BAD_SEQID means the server and client disagree about the
2203 * book-keeping w.r.t. state-changing operations
2204 * (OPEN/CLOSE/LOCK/LOCKU...)
2205 * It is actually a sign of a bug on the client or on the server.
2207 * If we receive a BAD_SEQID error in the particular case of
2208 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2209 * have unhashed the old state_owner for us, and that we can
2210 * therefore safely retry using a new one. We should still warn
2211 * the user though...
2213 if (status
== -NFS4ERR_BAD_SEQID
) {
2214 pr_warn_ratelimited("NFS: v4 server %s "
2215 " returned a bad sequence-id error!\n",
2216 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
2217 exception
.retry
= 1;
2221 * BAD_STATEID on OPEN means that the server cancelled our
2222 * state before it received the OPEN_CONFIRM.
2223 * Recover by retrying the request as per the discussion
2224 * on Page 181 of RFC3530.
2226 if (status
== -NFS4ERR_BAD_STATEID
) {
2227 exception
.retry
= 1;
2230 if (status
== -EAGAIN
) {
2231 /* We must have found a delegation */
2232 exception
.retry
= 1;
2235 if (nfs4_clear_cap_atomic_open_v1(server
, status
, &exception
))
2237 res
= ERR_PTR(nfs4_handle_exception(server
,
2238 status
, &exception
));
2239 } while (exception
.retry
);
2243 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2244 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2245 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2246 struct nfs4_label
*olabel
)
2248 struct nfs_server
*server
= NFS_SERVER(inode
);
2249 struct nfs_setattrargs arg
= {
2250 .fh
= NFS_FH(inode
),
2253 .bitmask
= server
->attr_bitmask
,
2256 struct nfs_setattrres res
= {
2261 struct rpc_message msg
= {
2262 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2267 unsigned long timestamp
= jiffies
;
2272 arg
.bitmask
= nfs4_bitmask(server
, ilabel
);
2274 arg
.bitmask
= nfs4_bitmask(server
, olabel
);
2276 nfs_fattr_init(fattr
);
2278 /* Servers should only apply open mode checks for file size changes */
2279 truncate
= (sattr
->ia_valid
& ATTR_SIZE
) ? true : false;
2280 fmode
= truncate
? FMODE_WRITE
: FMODE_READ
;
2282 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
, fmode
)) {
2283 /* Use that stateid */
2284 } else if (truncate
&& state
!= NULL
&& nfs4_valid_open_stateid(state
)) {
2285 struct nfs_lockowner lockowner
= {
2286 .l_owner
= current
->files
,
2287 .l_pid
= current
->tgid
,
2289 nfs4_select_rw_stateid(&arg
.stateid
, state
, FMODE_WRITE
,
2292 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
2294 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2295 if (status
== 0 && state
!= NULL
)
2296 renew_lease(server
, timestamp
);
2300 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2301 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2302 struct nfs4_state
*state
, struct nfs4_label
*ilabel
,
2303 struct nfs4_label
*olabel
)
2305 struct nfs_server
*server
= NFS_SERVER(inode
);
2306 struct nfs4_exception exception
= {
2312 err
= _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, ilabel
, olabel
);
2314 case -NFS4ERR_OPENMODE
:
2315 if (!(sattr
->ia_valid
& ATTR_SIZE
)) {
2316 pr_warn_once("NFSv4: server %s is incorrectly "
2317 "applying open mode checks to "
2318 "a SETATTR that is not "
2319 "changing file size.\n",
2320 server
->nfs_client
->cl_hostname
);
2322 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2324 if (sattr
->ia_valid
& ATTR_OPEN
)
2329 err
= nfs4_handle_exception(server
, err
, &exception
);
2330 } while (exception
.retry
);
2335 struct nfs4_closedata
{
2336 struct inode
*inode
;
2337 struct nfs4_state
*state
;
2338 struct nfs_closeargs arg
;
2339 struct nfs_closeres res
;
2340 struct nfs_fattr fattr
;
2341 unsigned long timestamp
;
2346 static void nfs4_free_closedata(void *data
)
2348 struct nfs4_closedata
*calldata
= data
;
2349 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2350 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2353 pnfs_roc_release(calldata
->state
->inode
);
2354 nfs4_put_open_state(calldata
->state
);
2355 nfs_free_seqid(calldata
->arg
.seqid
);
2356 nfs4_put_state_owner(sp
);
2357 nfs_sb_deactive(sb
);
2361 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
2364 spin_lock(&state
->owner
->so_lock
);
2365 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2366 switch (fmode
& (FMODE_READ
|FMODE_WRITE
)) {
2368 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2371 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2374 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2375 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2376 clear_bit(NFS_OPEN_STATE
, &state
->flags
);
2378 spin_unlock(&state
->owner
->so_lock
);
2381 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2383 struct nfs4_closedata
*calldata
= data
;
2384 struct nfs4_state
*state
= calldata
->state
;
2385 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2387 dprintk("%s: begin!\n", __func__
);
2388 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2390 /* hmm. we are done with the inode, and in the process of freeing
2391 * the state_owner. we keep this around to process errors
2393 switch (task
->tk_status
) {
2396 pnfs_roc_set_barrier(state
->inode
,
2397 calldata
->roc_barrier
);
2398 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
2399 renew_lease(server
, calldata
->timestamp
);
2400 nfs4_close_clear_stateid_flags(state
,
2401 calldata
->arg
.fmode
);
2403 case -NFS4ERR_STALE_STATEID
:
2404 case -NFS4ERR_OLD_STATEID
:
2405 case -NFS4ERR_BAD_STATEID
:
2406 case -NFS4ERR_EXPIRED
:
2407 if (calldata
->arg
.fmode
== 0)
2410 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
2411 rpc_restart_call_prepare(task
);
2413 nfs_release_seqid(calldata
->arg
.seqid
);
2414 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
2415 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
2418 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
2420 struct nfs4_closedata
*calldata
= data
;
2421 struct nfs4_state
*state
= calldata
->state
;
2422 struct inode
*inode
= calldata
->inode
;
2425 dprintk("%s: begin!\n", __func__
);
2426 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
2429 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
2430 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
2431 spin_lock(&state
->owner
->so_lock
);
2432 /* Calculate the change in open mode */
2433 if (state
->n_rdwr
== 0) {
2434 if (state
->n_rdonly
== 0) {
2435 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2436 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2437 calldata
->arg
.fmode
&= ~FMODE_READ
;
2439 if (state
->n_wronly
== 0) {
2440 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2441 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2442 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
2445 if (!nfs4_valid_open_stateid(state
))
2447 spin_unlock(&state
->owner
->so_lock
);
2450 /* Note: exit _without_ calling nfs4_close_done */
2454 if (calldata
->arg
.fmode
== 0) {
2455 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
2456 if (calldata
->roc
&&
2457 pnfs_roc_drain(inode
, &calldata
->roc_barrier
, task
)) {
2458 nfs_release_seqid(calldata
->arg
.seqid
);
2463 nfs_fattr_init(calldata
->res
.fattr
);
2464 calldata
->timestamp
= jiffies
;
2465 if (nfs4_setup_sequence(NFS_SERVER(inode
),
2466 &calldata
->arg
.seq_args
,
2467 &calldata
->res
.seq_res
,
2469 nfs_release_seqid(calldata
->arg
.seqid
);
2470 dprintk("%s: done!\n", __func__
);
2473 task
->tk_action
= NULL
;
2475 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
2478 static const struct rpc_call_ops nfs4_close_ops
= {
2479 .rpc_call_prepare
= nfs4_close_prepare
,
2480 .rpc_call_done
= nfs4_close_done
,
2481 .rpc_release
= nfs4_free_closedata
,
2485 * It is possible for data to be read/written from a mem-mapped file
2486 * after the sys_close call (which hits the vfs layer as a flush).
2487 * This means that we can't safely call nfsv4 close on a file until
2488 * the inode is cleared. This in turn means that we are not good
2489 * NFSv4 citizens - we do not indicate to the server to update the file's
2490 * share state even when we are done with one of the three share
2491 * stateid's in the inode.
2493 * NOTE: Caller must be holding the sp->so_owner semaphore!
2495 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
2497 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2498 struct nfs4_closedata
*calldata
;
2499 struct nfs4_state_owner
*sp
= state
->owner
;
2500 struct rpc_task
*task
;
2501 struct rpc_message msg
= {
2502 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
2503 .rpc_cred
= state
->owner
->so_cred
,
2505 struct rpc_task_setup task_setup_data
= {
2506 .rpc_client
= server
->client
,
2507 .rpc_message
= &msg
,
2508 .callback_ops
= &nfs4_close_ops
,
2509 .workqueue
= nfsiod_workqueue
,
2510 .flags
= RPC_TASK_ASYNC
,
2512 int status
= -ENOMEM
;
2514 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
2515 if (calldata
== NULL
)
2517 nfs41_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
2518 calldata
->inode
= state
->inode
;
2519 calldata
->state
= state
;
2520 calldata
->arg
.fh
= NFS_FH(state
->inode
);
2521 calldata
->arg
.stateid
= &state
->open_stateid
;
2522 /* Serialization for the sequence id */
2523 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
2524 if (calldata
->arg
.seqid
== NULL
)
2525 goto out_free_calldata
;
2526 calldata
->arg
.fmode
= 0;
2527 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
2528 calldata
->res
.fattr
= &calldata
->fattr
;
2529 calldata
->res
.seqid
= calldata
->arg
.seqid
;
2530 calldata
->res
.server
= server
;
2531 calldata
->roc
= pnfs_roc(state
->inode
);
2532 nfs_sb_active(calldata
->inode
->i_sb
);
2534 msg
.rpc_argp
= &calldata
->arg
;
2535 msg
.rpc_resp
= &calldata
->res
;
2536 task_setup_data
.callback_data
= calldata
;
2537 task
= rpc_run_task(&task_setup_data
);
2539 return PTR_ERR(task
);
2542 status
= rpc_wait_for_completion_task(task
);
2548 nfs4_put_open_state(state
);
2549 nfs4_put_state_owner(sp
);
2553 static struct inode
*
2554 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
, int open_flags
, struct iattr
*attr
)
2556 struct nfs4_state
*state
;
2557 struct nfs4_label l
= {0, 0, 0, NULL
}, *label
= NULL
;
2559 label
= nfs4_label_init_security(dir
, ctx
->dentry
, attr
, &l
);
2561 /* Protect against concurrent sillydeletes */
2562 state
= nfs4_do_open(dir
, ctx
, open_flags
, attr
, label
);
2564 nfs4_label_release_security(label
);
2567 return ERR_CAST(state
);
2568 return state
->inode
;
2571 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2573 if (ctx
->state
== NULL
)
2576 nfs4_close_sync(ctx
->state
, ctx
->mode
);
2578 nfs4_close_state(ctx
->state
, ctx
->mode
);
2581 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2583 struct nfs4_server_caps_arg args
= {
2586 struct nfs4_server_caps_res res
= {};
2587 struct rpc_message msg
= {
2588 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2594 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2596 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2597 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2598 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2599 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2600 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2601 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2602 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2603 server
->caps
|= NFS_CAP_ACLS
;
2604 if (res
.has_links
!= 0)
2605 server
->caps
|= NFS_CAP_HARDLINKS
;
2606 if (res
.has_symlinks
!= 0)
2607 server
->caps
|= NFS_CAP_SYMLINKS
;
2608 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2609 server
->caps
|= NFS_CAP_FILEID
;
2610 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2611 server
->caps
|= NFS_CAP_MODE
;
2612 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2613 server
->caps
|= NFS_CAP_NLINK
;
2614 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2615 server
->caps
|= NFS_CAP_OWNER
;
2616 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2617 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2618 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2619 server
->caps
|= NFS_CAP_ATIME
;
2620 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2621 server
->caps
|= NFS_CAP_CTIME
;
2622 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2623 server
->caps
|= NFS_CAP_MTIME
;
2624 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2625 if (res
.attr_bitmask
[2] & FATTR4_WORD2_SECURITY_LABEL
)
2626 server
->caps
|= NFS_CAP_SECURITY_LABEL
;
2628 memcpy(server
->attr_bitmask_nl
, res
.attr_bitmask
,
2629 sizeof(server
->attr_bitmask
));
2631 if (server
->caps
& NFS_CAP_SECURITY_LABEL
) {
2632 server
->attr_bitmask_nl
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
2633 res
.attr_bitmask
[2] &= ~FATTR4_WORD2_SECURITY_LABEL
;
2635 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2636 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2637 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2638 server
->acl_bitmask
= res
.acl_bitmask
;
2639 server
->fh_expire_type
= res
.fh_expire_type
;
2645 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2647 struct nfs4_exception exception
= { };
2650 err
= nfs4_handle_exception(server
,
2651 _nfs4_server_capabilities(server
, fhandle
),
2653 } while (exception
.retry
);
2657 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2658 struct nfs_fsinfo
*info
)
2661 struct nfs4_lookup_root_arg args
= {
2664 struct nfs4_lookup_res res
= {
2666 .fattr
= info
->fattr
,
2669 struct rpc_message msg
= {
2670 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2675 bitmask
[0] = nfs4_fattr_bitmap
[0];
2676 bitmask
[1] = nfs4_fattr_bitmap
[1];
2678 * Process the label in the upcoming getfattr
2680 bitmask
[2] = nfs4_fattr_bitmap
[2] & ~FATTR4_WORD2_SECURITY_LABEL
;
2682 nfs_fattr_init(info
->fattr
);
2683 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2686 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2687 struct nfs_fsinfo
*info
)
2689 struct nfs4_exception exception
= { };
2692 err
= _nfs4_lookup_root(server
, fhandle
, info
);
2695 case -NFS4ERR_WRONGSEC
:
2698 err
= nfs4_handle_exception(server
, err
, &exception
);
2700 } while (exception
.retry
);
2705 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2706 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
2708 struct rpc_auth
*auth
;
2711 auth
= rpcauth_create(flavor
, server
->client
);
2716 ret
= nfs4_lookup_root(server
, fhandle
, info
);
2722 * Retry pseudoroot lookup with various security flavors. We do this when:
2724 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2725 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2727 * Returns zero on success, or a negative NFS4ERR value, or a
2728 * negative errno value.
2730 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2731 struct nfs_fsinfo
*info
)
2733 /* Per 3530bis 15.33.5 */
2734 static const rpc_authflavor_t flav_array
[] = {
2738 RPC_AUTH_UNIX
, /* courtesy */
2741 int status
= -EPERM
;
2744 for (i
= 0; i
< ARRAY_SIZE(flav_array
); i
++) {
2745 status
= nfs4_lookup_root_sec(server
, fhandle
, info
, flav_array
[i
]);
2746 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
2752 * -EACCESS could mean that the user doesn't have correct permissions
2753 * to access the mount. It could also mean that we tried to mount
2754 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2755 * existing mount programs don't handle -EACCES very well so it should
2756 * be mapped to -EPERM instead.
2758 if (status
== -EACCES
)
2763 static int nfs4_do_find_root_sec(struct nfs_server
*server
,
2764 struct nfs_fh
*fhandle
, struct nfs_fsinfo
*info
)
2766 int mv
= server
->nfs_client
->cl_minorversion
;
2767 return nfs_v4_minor_ops
[mv
]->find_root_sec(server
, fhandle
, info
);
2771 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2772 * @server: initialized nfs_server handle
2773 * @fhandle: we fill in the pseudo-fs root file handle
2774 * @info: we fill in an FSINFO struct
2776 * Returns zero on success, or a negative errno.
2778 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2779 struct nfs_fsinfo
*info
)
2783 status
= nfs4_lookup_root(server
, fhandle
, info
);
2784 if ((status
== -NFS4ERR_WRONGSEC
) &&
2785 !(server
->flags
& NFS_MOUNT_SECFLAVOUR
))
2786 status
= nfs4_do_find_root_sec(server
, fhandle
, info
);
2789 status
= nfs4_server_capabilities(server
, fhandle
);
2791 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2793 return nfs4_map_errors(status
);
2796 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
2797 struct nfs_fsinfo
*info
)
2800 struct nfs_fattr
*fattr
= info
->fattr
;
2801 struct nfs4_label
*label
= NULL
;
2803 error
= nfs4_server_capabilities(server
, mntfh
);
2805 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
2809 label
= nfs4_label_alloc(server
, GFP_KERNEL
);
2811 return PTR_ERR(label
);
2813 error
= nfs4_proc_getattr(server
, mntfh
, fattr
, label
);
2815 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
2816 goto err_free_label
;
2819 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
2820 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
2821 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
2824 nfs4_label_free(label
);
2830 * Get locations and (maybe) other attributes of a referral.
2831 * Note that we'll actually follow the referral later when
2832 * we detect fsid mismatch in inode revalidation
2834 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
2835 const struct qstr
*name
, struct nfs_fattr
*fattr
,
2836 struct nfs_fh
*fhandle
)
2838 int status
= -ENOMEM
;
2839 struct page
*page
= NULL
;
2840 struct nfs4_fs_locations
*locations
= NULL
;
2842 page
= alloc_page(GFP_KERNEL
);
2845 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2846 if (locations
== NULL
)
2849 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
2852 /* Make sure server returned a different fsid for the referral */
2853 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2854 dprintk("%s: server did not return a different fsid for"
2855 " a referral at %s\n", __func__
, name
->name
);
2859 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2860 nfs_fixup_referral_attributes(&locations
->fattr
);
2862 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2863 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2864 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2872 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2873 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
2875 struct nfs4_getattr_arg args
= {
2877 .bitmask
= server
->attr_bitmask
,
2879 struct nfs4_getattr_res res
= {
2884 struct rpc_message msg
= {
2885 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2890 args
.bitmask
= nfs4_bitmask(server
, label
);
2892 nfs_fattr_init(fattr
);
2893 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2896 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2897 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
2899 struct nfs4_exception exception
= { };
2902 err
= nfs4_handle_exception(server
,
2903 _nfs4_proc_getattr(server
, fhandle
, fattr
, label
),
2905 } while (exception
.retry
);
2910 * The file is not closed if it is opened due to the a request to change
2911 * the size of the file. The open call will not be needed once the
2912 * VFS layer lookup-intents are implemented.
2914 * Close is called when the inode is destroyed.
2915 * If we haven't opened the file for O_WRONLY, we
2916 * need to in the size_change case to obtain a stateid.
2919 * Because OPEN is always done by name in nfsv4, it is
2920 * possible that we opened a different file by the same
2921 * name. We can recognize this race condition, but we
2922 * can't do anything about it besides returning an error.
2924 * This will be fixed with VFS changes (lookup-intent).
2927 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2928 struct iattr
*sattr
)
2930 struct inode
*inode
= dentry
->d_inode
;
2931 struct rpc_cred
*cred
= NULL
;
2932 struct nfs4_state
*state
= NULL
;
2933 struct nfs4_label
*label
= NULL
;
2936 if (pnfs_ld_layoutret_on_setattr(inode
))
2937 pnfs_commit_and_return_layout(inode
);
2939 nfs_fattr_init(fattr
);
2941 /* Deal with open(O_TRUNC) */
2942 if (sattr
->ia_valid
& ATTR_OPEN
)
2943 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
);
2945 /* Optimization: if the end result is no change, don't RPC */
2946 if ((sattr
->ia_valid
& ~(ATTR_FILE
)) == 0)
2949 /* Search for an existing open(O_WRITE) file */
2950 if (sattr
->ia_valid
& ATTR_FILE
) {
2951 struct nfs_open_context
*ctx
;
2953 ctx
= nfs_file_open_context(sattr
->ia_file
);
2960 label
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
2962 return PTR_ERR(label
);
2964 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
, NULL
, label
);
2966 nfs_setattr_update_inode(inode
, sattr
);
2967 nfs_setsecurity(inode
, fattr
, label
);
2969 nfs4_label_free(label
);
2973 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
2974 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2975 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
2977 struct nfs_server
*server
= NFS_SERVER(dir
);
2979 struct nfs4_lookup_arg args
= {
2980 .bitmask
= server
->attr_bitmask
,
2981 .dir_fh
= NFS_FH(dir
),
2984 struct nfs4_lookup_res res
= {
2990 struct rpc_message msg
= {
2991 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2996 args
.bitmask
= nfs4_bitmask(server
, label
);
2998 nfs_fattr_init(fattr
);
3000 dprintk("NFS call lookup %s\n", name
->name
);
3001 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3002 dprintk("NFS reply lookup: %d\n", status
);
3006 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
3008 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3009 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
3010 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3014 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
3015 struct qstr
*name
, struct nfs_fh
*fhandle
,
3016 struct nfs_fattr
*fattr
, struct nfs4_label
*label
)
3018 struct nfs4_exception exception
= { };
3019 struct rpc_clnt
*client
= *clnt
;
3022 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
, label
);
3024 case -NFS4ERR_BADNAME
:
3027 case -NFS4ERR_MOVED
:
3028 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
3030 case -NFS4ERR_WRONGSEC
:
3032 if (client
!= *clnt
)
3035 client
= nfs4_create_sec_client(client
, dir
, name
);
3037 return PTR_ERR(client
);
3039 exception
.retry
= 1;
3042 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
3044 } while (exception
.retry
);
3049 else if (client
!= *clnt
)
3050 rpc_shutdown_client(client
);
3055 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
3056 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
,
3057 struct nfs4_label
*label
)
3060 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3062 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, label
);
3063 if (client
!= NFS_CLIENT(dir
)) {
3064 rpc_shutdown_client(client
);
3065 nfs_fixup_secinfo_attributes(fattr
);
3071 nfs4_proc_lookup_mountpoint(struct inode
*dir
, struct qstr
*name
,
3072 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
3074 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
3077 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
, NULL
);
3079 return ERR_PTR(status
);
3080 return (client
== NFS_CLIENT(dir
)) ? rpc_clone_client(client
) : client
;
3083 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3085 struct nfs_server
*server
= NFS_SERVER(inode
);
3086 struct nfs4_accessargs args
= {
3087 .fh
= NFS_FH(inode
),
3088 .bitmask
= server
->cache_consistency_bitmask
,
3090 struct nfs4_accessres res
= {
3093 struct rpc_message msg
= {
3094 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
3097 .rpc_cred
= entry
->cred
,
3099 int mode
= entry
->mask
;
3103 * Determine which access bits we want to ask for...
3105 if (mode
& MAY_READ
)
3106 args
.access
|= NFS4_ACCESS_READ
;
3107 if (S_ISDIR(inode
->i_mode
)) {
3108 if (mode
& MAY_WRITE
)
3109 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
3110 if (mode
& MAY_EXEC
)
3111 args
.access
|= NFS4_ACCESS_LOOKUP
;
3113 if (mode
& MAY_WRITE
)
3114 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
3115 if (mode
& MAY_EXEC
)
3116 args
.access
|= NFS4_ACCESS_EXECUTE
;
3119 res
.fattr
= nfs_alloc_fattr();
3120 if (res
.fattr
== NULL
)
3123 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3125 nfs_access_set_mask(entry
, res
.access
);
3126 nfs_refresh_inode(inode
, res
.fattr
);
3128 nfs_free_fattr(res
.fattr
);
3132 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
3134 struct nfs4_exception exception
= { };
3137 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3138 _nfs4_proc_access(inode
, entry
),
3140 } while (exception
.retry
);
3145 * TODO: For the time being, we don't try to get any attributes
3146 * along with any of the zero-copy operations READ, READDIR,
3149 * In the case of the first three, we want to put the GETATTR
3150 * after the read-type operation -- this is because it is hard
3151 * to predict the length of a GETATTR response in v4, and thus
3152 * align the READ data correctly. This means that the GETATTR
3153 * may end up partially falling into the page cache, and we should
3154 * shift it into the 'tail' of the xdr_buf before processing.
3155 * To do this efficiently, we need to know the total length
3156 * of data received, which doesn't seem to be available outside
3159 * In the case of WRITE, we also want to put the GETATTR after
3160 * the operation -- in this case because we want to make sure
3161 * we get the post-operation mtime and size.
3163 * Both of these changes to the XDR layer would in fact be quite
3164 * minor, but I decided to leave them for a subsequent patch.
3166 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3167 unsigned int pgbase
, unsigned int pglen
)
3169 struct nfs4_readlink args
= {
3170 .fh
= NFS_FH(inode
),
3175 struct nfs4_readlink_res res
;
3176 struct rpc_message msg
= {
3177 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
3182 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3185 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
3186 unsigned int pgbase
, unsigned int pglen
)
3188 struct nfs4_exception exception
= { };
3191 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3192 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
3194 } while (exception
.retry
);
3199 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3202 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
3205 struct nfs4_label l
, *ilabel
= NULL
;
3206 struct nfs_open_context
*ctx
;
3207 struct nfs4_state
*state
;
3210 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
3212 return PTR_ERR(ctx
);
3214 ilabel
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3216 sattr
->ia_mode
&= ~current_umask();
3217 state
= nfs4_do_open(dir
, ctx
, flags
, sattr
, ilabel
);
3218 if (IS_ERR(state
)) {
3219 status
= PTR_ERR(state
);
3223 nfs4_label_release_security(ilabel
);
3224 put_nfs_open_context(ctx
);
3228 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3230 struct nfs_server
*server
= NFS_SERVER(dir
);
3231 struct nfs_removeargs args
= {
3235 struct nfs_removeres res
= {
3238 struct rpc_message msg
= {
3239 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
3245 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
3247 update_changeattr(dir
, &res
.cinfo
);
3251 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
3253 struct nfs4_exception exception
= { };
3256 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3257 _nfs4_proc_remove(dir
, name
),
3259 } while (exception
.retry
);
3263 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
3265 struct nfs_server
*server
= NFS_SERVER(dir
);
3266 struct nfs_removeargs
*args
= msg
->rpc_argp
;
3267 struct nfs_removeres
*res
= msg
->rpc_resp
;
3269 res
->server
= server
;
3270 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
3271 nfs41_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
3273 nfs_fattr_init(res
->dir_attr
);
3276 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
3278 nfs4_setup_sequence(NFS_SERVER(data
->dir
),
3279 &data
->args
.seq_args
,
3284 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
3286 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
3288 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3290 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3292 update_changeattr(dir
, &res
->cinfo
);
3296 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
3298 struct nfs_server
*server
= NFS_SERVER(dir
);
3299 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
3300 struct nfs_renameres
*res
= msg
->rpc_resp
;
3302 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
3303 res
->server
= server
;
3304 nfs41_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
3307 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
3309 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
3310 &data
->args
.seq_args
,
3315 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
3316 struct inode
*new_dir
)
3318 struct nfs_renameres
*res
= task
->tk_msg
.rpc_resp
;
3320 if (!nfs4_sequence_done(task
, &res
->seq_res
))
3322 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
3325 update_changeattr(old_dir
, &res
->old_cinfo
);
3326 update_changeattr(new_dir
, &res
->new_cinfo
);
3330 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3331 struct inode
*new_dir
, struct qstr
*new_name
)
3333 struct nfs_server
*server
= NFS_SERVER(old_dir
);
3334 struct nfs_renameargs arg
= {
3335 .old_dir
= NFS_FH(old_dir
),
3336 .new_dir
= NFS_FH(new_dir
),
3337 .old_name
= old_name
,
3338 .new_name
= new_name
,
3340 struct nfs_renameres res
= {
3343 struct rpc_message msg
= {
3344 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
3348 int status
= -ENOMEM
;
3350 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3352 update_changeattr(old_dir
, &res
.old_cinfo
);
3353 update_changeattr(new_dir
, &res
.new_cinfo
);
3358 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3359 struct inode
*new_dir
, struct qstr
*new_name
)
3361 struct nfs4_exception exception
= { };
3364 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
3365 _nfs4_proc_rename(old_dir
, old_name
,
3368 } while (exception
.retry
);
3372 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3374 struct nfs_server
*server
= NFS_SERVER(inode
);
3375 struct nfs4_link_arg arg
= {
3376 .fh
= NFS_FH(inode
),
3377 .dir_fh
= NFS_FH(dir
),
3379 .bitmask
= server
->attr_bitmask
,
3381 struct nfs4_link_res res
= {
3385 struct rpc_message msg
= {
3386 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
3390 int status
= -ENOMEM
;
3392 res
.fattr
= nfs_alloc_fattr();
3393 if (res
.fattr
== NULL
)
3396 res
.label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3397 if (IS_ERR(res
.label
)) {
3398 status
= PTR_ERR(res
.label
);
3401 arg
.bitmask
= nfs4_bitmask(server
, res
.label
);
3403 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3405 update_changeattr(dir
, &res
.cinfo
);
3406 status
= nfs_post_op_update_inode(inode
, res
.fattr
);
3408 nfs_setsecurity(inode
, res
.fattr
, res
.label
);
3412 nfs4_label_free(res
.label
);
3415 nfs_free_fattr(res
.fattr
);
3419 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3421 struct nfs4_exception exception
= { };
3424 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3425 _nfs4_proc_link(inode
, dir
, name
),
3427 } while (exception
.retry
);
3431 struct nfs4_createdata
{
3432 struct rpc_message msg
;
3433 struct nfs4_create_arg arg
;
3434 struct nfs4_create_res res
;
3436 struct nfs_fattr fattr
;
3437 struct nfs4_label
*label
;
3440 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
3441 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
3443 struct nfs4_createdata
*data
;
3445 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3447 struct nfs_server
*server
= NFS_SERVER(dir
);
3449 data
->label
= nfs4_label_alloc(server
, GFP_KERNEL
);
3450 if (IS_ERR(data
->label
))
3453 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
3454 data
->msg
.rpc_argp
= &data
->arg
;
3455 data
->msg
.rpc_resp
= &data
->res
;
3456 data
->arg
.dir_fh
= NFS_FH(dir
);
3457 data
->arg
.server
= server
;
3458 data
->arg
.name
= name
;
3459 data
->arg
.attrs
= sattr
;
3460 data
->arg
.ftype
= ftype
;
3461 data
->arg
.bitmask
= nfs4_bitmask(server
, data
->label
);
3462 data
->res
.server
= server
;
3463 data
->res
.fh
= &data
->fh
;
3464 data
->res
.fattr
= &data
->fattr
;
3465 data
->res
.label
= data
->label
;
3466 nfs_fattr_init(data
->res
.fattr
);
3474 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
3476 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
3477 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
3479 update_changeattr(dir
, &data
->res
.dir_cinfo
);
3480 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
, data
->res
.label
);
3485 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
3487 nfs4_label_free(data
->label
);
3491 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3492 struct page
*page
, unsigned int len
, struct iattr
*sattr
,
3493 struct nfs4_label
*label
)
3495 struct nfs4_createdata
*data
;
3496 int status
= -ENAMETOOLONG
;
3498 if (len
> NFS4_MAXPATHLEN
)
3502 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
3506 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
3507 data
->arg
.u
.symlink
.pages
= &page
;
3508 data
->arg
.u
.symlink
.len
= len
;
3509 data
->arg
.label
= label
;
3511 status
= nfs4_do_create(dir
, dentry
, data
);
3513 nfs4_free_createdata(data
);
3518 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3519 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
3521 struct nfs4_exception exception
= { };
3522 struct nfs4_label l
, *label
= NULL
;
3525 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3528 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3529 _nfs4_proc_symlink(dir
, dentry
, page
,
3532 } while (exception
.retry
);
3534 nfs4_label_release_security(label
);
3538 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3539 struct iattr
*sattr
, struct nfs4_label
*label
)
3541 struct nfs4_createdata
*data
;
3542 int status
= -ENOMEM
;
3544 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
3548 data
->arg
.label
= label
;
3549 status
= nfs4_do_create(dir
, dentry
, data
);
3551 nfs4_free_createdata(data
);
3556 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3557 struct iattr
*sattr
)
3559 struct nfs4_exception exception
= { };
3560 struct nfs4_label l
, *label
= NULL
;
3563 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3565 sattr
->ia_mode
&= ~current_umask();
3567 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3568 _nfs4_proc_mkdir(dir
, dentry
, sattr
, label
),
3570 } while (exception
.retry
);
3571 nfs4_label_release_security(label
);
3576 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3577 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3579 struct inode
*dir
= dentry
->d_inode
;
3580 struct nfs4_readdir_arg args
= {
3585 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
3588 struct nfs4_readdir_res res
;
3589 struct rpc_message msg
= {
3590 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
3597 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
3598 dentry
->d_parent
->d_name
.name
,
3599 dentry
->d_name
.name
,
3600 (unsigned long long)cookie
);
3601 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
3602 res
.pgbase
= args
.pgbase
;
3603 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3605 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
3606 status
+= args
.pgbase
;
3609 nfs_invalidate_atime(dir
);
3611 dprintk("%s: returns %d\n", __func__
, status
);
3615 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3616 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3618 struct nfs4_exception exception
= { };
3621 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
3622 _nfs4_proc_readdir(dentry
, cred
, cookie
,
3623 pages
, count
, plus
),
3625 } while (exception
.retry
);
3629 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3630 struct iattr
*sattr
, struct nfs4_label
*label
, dev_t rdev
)
3632 struct nfs4_createdata
*data
;
3633 int mode
= sattr
->ia_mode
;
3634 int status
= -ENOMEM
;
3636 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
3641 data
->arg
.ftype
= NF4FIFO
;
3642 else if (S_ISBLK(mode
)) {
3643 data
->arg
.ftype
= NF4BLK
;
3644 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3645 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3647 else if (S_ISCHR(mode
)) {
3648 data
->arg
.ftype
= NF4CHR
;
3649 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3650 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3651 } else if (!S_ISSOCK(mode
)) {
3656 data
->arg
.label
= label
;
3657 status
= nfs4_do_create(dir
, dentry
, data
);
3659 nfs4_free_createdata(data
);
3664 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3665 struct iattr
*sattr
, dev_t rdev
)
3667 struct nfs4_exception exception
= { };
3668 struct nfs4_label l
, *label
= NULL
;
3671 label
= nfs4_label_init_security(dir
, dentry
, sattr
, &l
);
3673 sattr
->ia_mode
&= ~current_umask();
3675 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3676 _nfs4_proc_mknod(dir
, dentry
, sattr
, label
, rdev
),
3678 } while (exception
.retry
);
3680 nfs4_label_release_security(label
);
3685 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3686 struct nfs_fsstat
*fsstat
)
3688 struct nfs4_statfs_arg args
= {
3690 .bitmask
= server
->attr_bitmask
,
3692 struct nfs4_statfs_res res
= {
3695 struct rpc_message msg
= {
3696 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
3701 nfs_fattr_init(fsstat
->fattr
);
3702 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3705 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
3707 struct nfs4_exception exception
= { };
3710 err
= nfs4_handle_exception(server
,
3711 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
3713 } while (exception
.retry
);
3717 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3718 struct nfs_fsinfo
*fsinfo
)
3720 struct nfs4_fsinfo_arg args
= {
3722 .bitmask
= server
->attr_bitmask
,
3724 struct nfs4_fsinfo_res res
= {
3727 struct rpc_message msg
= {
3728 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3733 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3736 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3738 struct nfs4_exception exception
= { };
3739 unsigned long now
= jiffies
;
3743 err
= _nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3745 struct nfs_client
*clp
= server
->nfs_client
;
3747 spin_lock(&clp
->cl_lock
);
3748 clp
->cl_lease_time
= fsinfo
->lease_time
* HZ
;
3749 clp
->cl_last_renewal
= now
;
3750 spin_unlock(&clp
->cl_lock
);
3753 err
= nfs4_handle_exception(server
, err
, &exception
);
3754 } while (exception
.retry
);
3758 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3762 nfs_fattr_init(fsinfo
->fattr
);
3763 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3765 /* block layout checks this! */
3766 server
->pnfs_blksize
= fsinfo
->blksize
;
3767 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
->layouttype
);
3773 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3774 struct nfs_pathconf
*pathconf
)
3776 struct nfs4_pathconf_arg args
= {
3778 .bitmask
= server
->attr_bitmask
,
3780 struct nfs4_pathconf_res res
= {
3781 .pathconf
= pathconf
,
3783 struct rpc_message msg
= {
3784 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3789 /* None of the pathconf attributes are mandatory to implement */
3790 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3791 memset(pathconf
, 0, sizeof(*pathconf
));
3795 nfs_fattr_init(pathconf
->fattr
);
3796 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3799 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3800 struct nfs_pathconf
*pathconf
)
3802 struct nfs4_exception exception
= { };
3806 err
= nfs4_handle_exception(server
,
3807 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3809 } while (exception
.retry
);
3813 int nfs4_set_rw_stateid(nfs4_stateid
*stateid
,
3814 const struct nfs_open_context
*ctx
,
3815 const struct nfs_lock_context
*l_ctx
,
3818 const struct nfs_lockowner
*lockowner
= NULL
;
3821 lockowner
= &l_ctx
->lockowner
;
3822 return nfs4_select_rw_stateid(stateid
, ctx
->state
, fmode
, lockowner
);
3824 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid
);
3826 static bool nfs4_stateid_is_current(nfs4_stateid
*stateid
,
3827 const struct nfs_open_context
*ctx
,
3828 const struct nfs_lock_context
*l_ctx
,
3831 nfs4_stateid current_stateid
;
3833 if (nfs4_set_rw_stateid(¤t_stateid
, ctx
, l_ctx
, fmode
))
3835 return nfs4_stateid_match(stateid
, ¤t_stateid
);
3838 static bool nfs4_error_stateid_expired(int err
)
3841 case -NFS4ERR_DELEG_REVOKED
:
3842 case -NFS4ERR_ADMIN_REVOKED
:
3843 case -NFS4ERR_BAD_STATEID
:
3844 case -NFS4ERR_STALE_STATEID
:
3845 case -NFS4ERR_OLD_STATEID
:
3846 case -NFS4ERR_OPENMODE
:
3847 case -NFS4ERR_EXPIRED
:
3853 void __nfs4_read_done_cb(struct nfs_read_data
*data
)
3855 nfs_invalidate_atime(data
->header
->inode
);
3858 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_read_data
*data
)
3860 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
3862 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3863 rpc_restart_call_prepare(task
);
3867 __nfs4_read_done_cb(data
);
3868 if (task
->tk_status
> 0)
3869 renew_lease(server
, data
->timestamp
);
3873 static bool nfs4_read_stateid_changed(struct rpc_task
*task
,
3874 struct nfs_readargs
*args
)
3877 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
3878 nfs4_stateid_is_current(&args
->stateid
,
3883 rpc_restart_call_prepare(task
);
3887 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3890 dprintk("--> %s\n", __func__
);
3892 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3894 if (nfs4_read_stateid_changed(task
, &data
->args
))
3896 return data
->read_done_cb
? data
->read_done_cb(task
, data
) :
3897 nfs4_read_done_cb(task
, data
);
3900 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3902 data
->timestamp
= jiffies
;
3903 data
->read_done_cb
= nfs4_read_done_cb
;
3904 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3905 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
3908 static void nfs4_proc_read_rpc_prepare(struct rpc_task
*task
, struct nfs_read_data
*data
)
3910 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
3911 &data
->args
.seq_args
,
3915 nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
3916 data
->args
.lock_context
, FMODE_READ
);
3919 static int nfs4_write_done_cb(struct rpc_task
*task
, struct nfs_write_data
*data
)
3921 struct inode
*inode
= data
->header
->inode
;
3923 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3924 rpc_restart_call_prepare(task
);
3927 if (task
->tk_status
>= 0) {
3928 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3929 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
3934 static bool nfs4_write_stateid_changed(struct rpc_task
*task
,
3935 struct nfs_writeargs
*args
)
3938 if (!nfs4_error_stateid_expired(task
->tk_status
) ||
3939 nfs4_stateid_is_current(&args
->stateid
,
3944 rpc_restart_call_prepare(task
);
3948 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3950 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3952 if (nfs4_write_stateid_changed(task
, &data
->args
))
3954 return data
->write_done_cb
? data
->write_done_cb(task
, data
) :
3955 nfs4_write_done_cb(task
, data
);
3959 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data
*data
)
3961 const struct nfs_pgio_header
*hdr
= data
->header
;
3963 /* Don't request attributes for pNFS or O_DIRECT writes */
3964 if (data
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
3966 /* Otherwise, request attributes if and only if we don't hold
3969 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
3972 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3974 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
3976 if (!nfs4_write_need_cache_consistency_data(data
)) {
3977 data
->args
.bitmask
= NULL
;
3978 data
->res
.fattr
= NULL
;
3980 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3982 if (!data
->write_done_cb
)
3983 data
->write_done_cb
= nfs4_write_done_cb
;
3984 data
->res
.server
= server
;
3985 data
->timestamp
= jiffies
;
3987 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3988 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
3991 static void nfs4_proc_write_rpc_prepare(struct rpc_task
*task
, struct nfs_write_data
*data
)
3993 if (nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
3994 &data
->args
.seq_args
,
3998 nfs4_set_rw_stateid(&data
->args
.stateid
, data
->args
.context
,
3999 data
->args
.lock_context
, FMODE_WRITE
);
4002 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4004 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
4005 &data
->args
.seq_args
,
4010 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4012 struct inode
*inode
= data
->inode
;
4014 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
4015 rpc_restart_call_prepare(task
);
4021 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
4023 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4025 return data
->commit_done_cb(task
, data
);
4028 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
4030 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
4032 if (data
->commit_done_cb
== NULL
)
4033 data
->commit_done_cb
= nfs4_commit_done_cb
;
4034 data
->res
.server
= server
;
4035 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
4036 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4039 struct nfs4_renewdata
{
4040 struct nfs_client
*client
;
4041 unsigned long timestamp
;
4045 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4046 * standalone procedure for queueing an asynchronous RENEW.
4048 static void nfs4_renew_release(void *calldata
)
4050 struct nfs4_renewdata
*data
= calldata
;
4051 struct nfs_client
*clp
= data
->client
;
4053 if (atomic_read(&clp
->cl_count
) > 1)
4054 nfs4_schedule_state_renewal(clp
);
4055 nfs_put_client(clp
);
4059 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
4061 struct nfs4_renewdata
*data
= calldata
;
4062 struct nfs_client
*clp
= data
->client
;
4063 unsigned long timestamp
= data
->timestamp
;
4065 if (task
->tk_status
< 0) {
4066 /* Unless we're shutting down, schedule state recovery! */
4067 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
4069 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
4070 nfs4_schedule_lease_recovery(clp
);
4073 nfs4_schedule_path_down_recovery(clp
);
4075 do_renew_lease(clp
, timestamp
);
4078 static const struct rpc_call_ops nfs4_renew_ops
= {
4079 .rpc_call_done
= nfs4_renew_done
,
4080 .rpc_release
= nfs4_renew_release
,
4083 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
4085 struct rpc_message msg
= {
4086 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4090 struct nfs4_renewdata
*data
;
4092 if (renew_flags
== 0)
4094 if (!atomic_inc_not_zero(&clp
->cl_count
))
4096 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
4100 data
->timestamp
= jiffies
;
4101 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
,
4102 &nfs4_renew_ops
, data
);
4105 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4107 struct rpc_message msg
= {
4108 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
4112 unsigned long now
= jiffies
;
4115 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4118 do_renew_lease(clp
, now
);
4122 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
4124 return (server
->caps
& NFS_CAP_ACLS
)
4125 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
4126 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
4129 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4130 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4133 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4135 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
4136 struct page
**pages
, unsigned int *pgbase
)
4138 struct page
*newpage
, **spages
;
4144 len
= min_t(size_t, PAGE_SIZE
, buflen
);
4145 newpage
= alloc_page(GFP_KERNEL
);
4147 if (newpage
== NULL
)
4149 memcpy(page_address(newpage
), buf
, len
);
4154 } while (buflen
!= 0);
4160 __free_page(spages
[rc
-1]);
4164 struct nfs4_cached_acl
{
4170 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
4172 struct nfs_inode
*nfsi
= NFS_I(inode
);
4174 spin_lock(&inode
->i_lock
);
4175 kfree(nfsi
->nfs4_acl
);
4176 nfsi
->nfs4_acl
= acl
;
4177 spin_unlock(&inode
->i_lock
);
4180 static void nfs4_zap_acl_attr(struct inode
*inode
)
4182 nfs4_set_cached_acl(inode
, NULL
);
4185 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
4187 struct nfs_inode
*nfsi
= NFS_I(inode
);
4188 struct nfs4_cached_acl
*acl
;
4191 spin_lock(&inode
->i_lock
);
4192 acl
= nfsi
->nfs4_acl
;
4195 if (buf
== NULL
) /* user is just asking for length */
4197 if (acl
->cached
== 0)
4199 ret
= -ERANGE
; /* see getxattr(2) man page */
4200 if (acl
->len
> buflen
)
4202 memcpy(buf
, acl
->data
, acl
->len
);
4206 spin_unlock(&inode
->i_lock
);
4210 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
4212 struct nfs4_cached_acl
*acl
;
4213 size_t buflen
= sizeof(*acl
) + acl_len
;
4215 if (buflen
<= PAGE_SIZE
) {
4216 acl
= kmalloc(buflen
, GFP_KERNEL
);
4220 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
4222 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
4229 nfs4_set_cached_acl(inode
, acl
);
4233 * The getxattr API returns the required buffer length when called with a
4234 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4235 * the required buf. On a NULL buf, we send a page of data to the server
4236 * guessing that the ACL request can be serviced by a page. If so, we cache
4237 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4238 * the cache. If not so, we throw away the page, and cache the required
4239 * length. The next getxattr call will then produce another round trip to
4240 * the server, this time with the input buf of the required size.
4242 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4244 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
4245 struct nfs_getaclargs args
= {
4246 .fh
= NFS_FH(inode
),
4250 struct nfs_getaclres res
= {
4253 struct rpc_message msg
= {
4254 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
4258 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4259 int ret
= -ENOMEM
, i
;
4261 /* As long as we're doing a round trip to the server anyway,
4262 * let's be prepared for a page of acl data. */
4265 if (npages
> ARRAY_SIZE(pages
))
4268 for (i
= 0; i
< npages
; i
++) {
4269 pages
[i
] = alloc_page(GFP_KERNEL
);
4274 /* for decoding across pages */
4275 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
4276 if (!res
.acl_scratch
)
4279 args
.acl_len
= npages
* PAGE_SIZE
;
4280 args
.acl_pgbase
= 0;
4282 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4283 __func__
, buf
, buflen
, npages
, args
.acl_len
);
4284 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
4285 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4289 /* Handle the case where the passed-in buffer is too short */
4290 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
4291 /* Did the user only issue a request for the acl length? */
4297 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
4299 if (res
.acl_len
> buflen
) {
4303 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
4308 for (i
= 0; i
< npages
; i
++)
4310 __free_page(pages
[i
]);
4311 if (res
.acl_scratch
)
4312 __free_page(res
.acl_scratch
);
4316 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
4318 struct nfs4_exception exception
= { };
4321 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
4324 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
4325 } while (exception
.retry
);
4329 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
4331 struct nfs_server
*server
= NFS_SERVER(inode
);
4334 if (!nfs4_server_supports_acls(server
))
4336 ret
= nfs_revalidate_inode(server
, inode
);
4339 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
4340 nfs_zap_acl_cache(inode
);
4341 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
4343 /* -ENOENT is returned if there is no ACL or if there is an ACL
4344 * but no cached acl data, just the acl length */
4346 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
4349 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4351 struct nfs_server
*server
= NFS_SERVER(inode
);
4352 struct page
*pages
[NFS4ACL_MAXPAGES
];
4353 struct nfs_setaclargs arg
= {
4354 .fh
= NFS_FH(inode
),
4358 struct nfs_setaclres res
;
4359 struct rpc_message msg
= {
4360 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
4364 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
4367 if (!nfs4_server_supports_acls(server
))
4369 if (npages
> ARRAY_SIZE(pages
))
4371 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
4374 nfs4_inode_return_delegation(inode
);
4375 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4378 * Free each page after tx, so the only ref left is
4379 * held by the network stack
4382 put_page(pages
[i
-1]);
4385 * Acl update can result in inode attribute update.
4386 * so mark the attribute cache invalid.
4388 spin_lock(&inode
->i_lock
);
4389 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
4390 spin_unlock(&inode
->i_lock
);
4391 nfs_access_zap_cache(inode
);
4392 nfs_zap_acl_cache(inode
);
4396 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
4398 struct nfs4_exception exception
= { };
4401 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4402 __nfs4_proc_set_acl(inode
, buf
, buflen
),
4404 } while (exception
.retry
);
4408 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4409 static int _nfs4_get_security_label(struct inode
*inode
, void *buf
,
4412 struct nfs_server
*server
= NFS_SERVER(inode
);
4413 struct nfs_fattr fattr
;
4414 struct nfs4_label label
= {0, 0, buflen
, buf
};
4416 u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4417 struct nfs4_getattr_arg args
= {
4418 .fh
= NFS_FH(inode
),
4421 struct nfs4_getattr_res res
= {
4426 struct rpc_message msg
= {
4427 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
4433 nfs_fattr_init(&fattr
);
4435 ret
= rpc_call_sync(server
->client
, &msg
, 0);
4438 if (!(fattr
.valid
& NFS_ATTR_FATTR_V4_SECURITY_LABEL
))
4440 if (buflen
< label
.len
)
4445 static int nfs4_get_security_label(struct inode
*inode
, void *buf
,
4448 struct nfs4_exception exception
= { };
4451 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4455 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4456 _nfs4_get_security_label(inode
, buf
, buflen
),
4458 } while (exception
.retry
);
4462 static int _nfs4_do_set_security_label(struct inode
*inode
,
4463 struct nfs4_label
*ilabel
,
4464 struct nfs_fattr
*fattr
,
4465 struct nfs4_label
*olabel
)
4468 struct iattr sattr
= {0};
4469 struct nfs_server
*server
= NFS_SERVER(inode
);
4470 const u32 bitmask
[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL
};
4471 struct nfs_setattrargs args
= {
4472 .fh
= NFS_FH(inode
),
4478 struct nfs_setattrres res
= {
4483 struct rpc_message msg
= {
4484 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
4490 nfs4_stateid_copy(&args
.stateid
, &zero_stateid
);
4492 status
= rpc_call_sync(server
->client
, &msg
, 0);
4494 dprintk("%s failed: %d\n", __func__
, status
);
4499 static int nfs4_do_set_security_label(struct inode
*inode
,
4500 struct nfs4_label
*ilabel
,
4501 struct nfs_fattr
*fattr
,
4502 struct nfs4_label
*olabel
)
4504 struct nfs4_exception exception
= { };
4508 err
= nfs4_handle_exception(NFS_SERVER(inode
),
4509 _nfs4_do_set_security_label(inode
, ilabel
,
4512 } while (exception
.retry
);
4517 nfs4_set_security_label(struct dentry
*dentry
, const void *buf
, size_t buflen
)
4519 struct nfs4_label ilabel
, *olabel
= NULL
;
4520 struct nfs_fattr fattr
;
4521 struct rpc_cred
*cred
;
4522 struct inode
*inode
= dentry
->d_inode
;
4525 if (!nfs_server_capable(inode
, NFS_CAP_SECURITY_LABEL
))
4528 nfs_fattr_init(&fattr
);
4532 ilabel
.label
= (char *)buf
;
4533 ilabel
.len
= buflen
;
4535 cred
= rpc_lookup_cred();
4537 return PTR_ERR(cred
);
4539 olabel
= nfs4_label_alloc(NFS_SERVER(inode
), GFP_KERNEL
);
4540 if (IS_ERR(olabel
)) {
4541 status
= -PTR_ERR(olabel
);
4545 status
= nfs4_do_set_security_label(inode
, &ilabel
, &fattr
, olabel
);
4547 nfs_setsecurity(inode
, &fattr
, olabel
);
4549 nfs4_label_free(olabel
);
4554 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4558 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
4560 struct nfs_client
*clp
= server
->nfs_client
;
4562 if (task
->tk_status
>= 0)
4564 switch(task
->tk_status
) {
4565 case -NFS4ERR_DELEG_REVOKED
:
4566 case -NFS4ERR_ADMIN_REVOKED
:
4567 case -NFS4ERR_BAD_STATEID
:
4570 nfs_remove_bad_delegation(state
->inode
);
4571 case -NFS4ERR_OPENMODE
:
4574 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4575 goto stateid_invalid
;
4576 goto wait_on_recovery
;
4577 case -NFS4ERR_EXPIRED
:
4578 if (state
!= NULL
) {
4579 if (nfs4_schedule_stateid_recovery(server
, state
) < 0)
4580 goto stateid_invalid
;
4582 case -NFS4ERR_STALE_STATEID
:
4583 case -NFS4ERR_STALE_CLIENTID
:
4584 nfs4_schedule_lease_recovery(clp
);
4585 goto wait_on_recovery
;
4586 #if defined(CONFIG_NFS_V4_1)
4587 case -NFS4ERR_BADSESSION
:
4588 case -NFS4ERR_BADSLOT
:
4589 case -NFS4ERR_BAD_HIGH_SLOT
:
4590 case -NFS4ERR_DEADSESSION
:
4591 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4592 case -NFS4ERR_SEQ_FALSE_RETRY
:
4593 case -NFS4ERR_SEQ_MISORDERED
:
4594 dprintk("%s ERROR %d, Reset session\n", __func__
,
4596 nfs4_schedule_session_recovery(clp
->cl_session
, task
->tk_status
);
4597 task
->tk_status
= 0;
4599 #endif /* CONFIG_NFS_V4_1 */
4600 case -NFS4ERR_DELAY
:
4601 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
4602 case -NFS4ERR_GRACE
:
4603 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
4604 task
->tk_status
= 0;
4606 case -NFS4ERR_RETRY_UNCACHED_REP
:
4607 case -NFS4ERR_OLD_STATEID
:
4608 task
->tk_status
= 0;
4611 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
4614 task
->tk_status
= -EIO
;
4617 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
4618 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
4619 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
4620 task
->tk_status
= 0;
4624 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
4625 nfs4_verifier
*bootverf
)
4629 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
4630 /* An impossible timestamp guarantees this value
4631 * will never match a generated boot time. */
4633 verf
[1] = (__be32
)(NSEC_PER_SEC
+ 1);
4635 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
4636 verf
[0] = (__be32
)nn
->boot_time
.tv_sec
;
4637 verf
[1] = (__be32
)nn
->boot_time
.tv_nsec
;
4639 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
4643 nfs4_init_nonuniform_client_string(const struct nfs_client
*clp
,
4644 char *buf
, size_t len
)
4646 unsigned int result
;
4649 result
= scnprintf(buf
, len
, "Linux NFSv4.0 %s/%s %s",
4651 rpc_peeraddr2str(clp
->cl_rpcclient
,
4653 rpc_peeraddr2str(clp
->cl_rpcclient
,
4654 RPC_DISPLAY_PROTO
));
4660 nfs4_init_uniform_client_string(const struct nfs_client
*clp
,
4661 char *buf
, size_t len
)
4663 char *nodename
= clp
->cl_rpcclient
->cl_nodename
;
4665 if (nfs4_client_id_uniquifier
[0] != '\0')
4666 nodename
= nfs4_client_id_uniquifier
;
4667 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s",
4668 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
4673 * nfs4_proc_setclientid - Negotiate client ID
4674 * @clp: state data structure
4675 * @program: RPC program for NFSv4 callback service
4676 * @port: IP port number for NFS4 callback service
4677 * @cred: RPC credential to use for this call
4678 * @res: where to place the result
4680 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4682 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
4683 unsigned short port
, struct rpc_cred
*cred
,
4684 struct nfs4_setclientid_res
*res
)
4686 nfs4_verifier sc_verifier
;
4687 struct nfs4_setclientid setclientid
= {
4688 .sc_verifier
= &sc_verifier
,
4690 .sc_cb_ident
= clp
->cl_cb_ident
,
4692 struct rpc_message msg
= {
4693 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
4694 .rpc_argp
= &setclientid
,
4700 /* nfs_client_id4 */
4701 nfs4_init_boot_verifier(clp
, &sc_verifier
);
4702 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
4703 setclientid
.sc_name_len
=
4704 nfs4_init_uniform_client_string(clp
,
4705 setclientid
.sc_name
,
4706 sizeof(setclientid
.sc_name
));
4708 setclientid
.sc_name_len
=
4709 nfs4_init_nonuniform_client_string(clp
,
4710 setclientid
.sc_name
,
4711 sizeof(setclientid
.sc_name
));
4714 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
4715 sizeof(setclientid
.sc_netid
), "%s",
4716 rpc_peeraddr2str(clp
->cl_rpcclient
,
4717 RPC_DISPLAY_NETID
));
4719 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
4720 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
4721 clp
->cl_ipaddr
, port
>> 8, port
& 255);
4723 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4724 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4725 setclientid
.sc_name_len
, setclientid
.sc_name
);
4726 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4727 dprintk("NFS reply setclientid: %d\n", status
);
4732 * nfs4_proc_setclientid_confirm - Confirm client ID
4733 * @clp: state data structure
4734 * @res: result of a previous SETCLIENTID
4735 * @cred: RPC credential to use for this call
4737 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4739 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
4740 struct nfs4_setclientid_res
*arg
,
4741 struct rpc_cred
*cred
)
4743 struct rpc_message msg
= {
4744 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
4750 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4751 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4753 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4754 dprintk("NFS reply setclientid_confirm: %d\n", status
);
4758 struct nfs4_delegreturndata
{
4759 struct nfs4_delegreturnargs args
;
4760 struct nfs4_delegreturnres res
;
4762 nfs4_stateid stateid
;
4763 unsigned long timestamp
;
4764 struct nfs_fattr fattr
;
4768 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
4770 struct nfs4_delegreturndata
*data
= calldata
;
4772 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4775 switch (task
->tk_status
) {
4776 case -NFS4ERR_STALE_STATEID
:
4777 case -NFS4ERR_EXPIRED
:
4779 renew_lease(data
->res
.server
, data
->timestamp
);
4782 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
4784 rpc_restart_call_prepare(task
);
4788 data
->rpc_status
= task
->tk_status
;
4791 static void nfs4_delegreturn_release(void *calldata
)
4796 #if defined(CONFIG_NFS_V4_1)
4797 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
4799 struct nfs4_delegreturndata
*d_data
;
4801 d_data
= (struct nfs4_delegreturndata
*)data
;
4803 nfs4_setup_sequence(d_data
->res
.server
,
4804 &d_data
->args
.seq_args
,
4805 &d_data
->res
.seq_res
,
4808 #endif /* CONFIG_NFS_V4_1 */
4810 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
4811 #if defined(CONFIG_NFS_V4_1)
4812 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
4813 #endif /* CONFIG_NFS_V4_1 */
4814 .rpc_call_done
= nfs4_delegreturn_done
,
4815 .rpc_release
= nfs4_delegreturn_release
,
4818 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
4820 struct nfs4_delegreturndata
*data
;
4821 struct nfs_server
*server
= NFS_SERVER(inode
);
4822 struct rpc_task
*task
;
4823 struct rpc_message msg
= {
4824 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
4827 struct rpc_task_setup task_setup_data
= {
4828 .rpc_client
= server
->client
,
4829 .rpc_message
= &msg
,
4830 .callback_ops
= &nfs4_delegreturn_ops
,
4831 .flags
= RPC_TASK_ASYNC
,
4835 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
4838 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4839 data
->args
.fhandle
= &data
->fh
;
4840 data
->args
.stateid
= &data
->stateid
;
4841 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
4842 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
4843 nfs4_stateid_copy(&data
->stateid
, stateid
);
4844 data
->res
.fattr
= &data
->fattr
;
4845 data
->res
.server
= server
;
4846 nfs_fattr_init(data
->res
.fattr
);
4847 data
->timestamp
= jiffies
;
4848 data
->rpc_status
= 0;
4850 task_setup_data
.callback_data
= data
;
4851 msg
.rpc_argp
= &data
->args
;
4852 msg
.rpc_resp
= &data
->res
;
4853 task
= rpc_run_task(&task_setup_data
);
4855 return PTR_ERR(task
);
4858 status
= nfs4_wait_for_completion_rpc_task(task
);
4861 status
= data
->rpc_status
;
4863 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
4865 nfs_refresh_inode(inode
, &data
->fattr
);
4871 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
4873 struct nfs_server
*server
= NFS_SERVER(inode
);
4874 struct nfs4_exception exception
= { };
4877 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
4879 case -NFS4ERR_STALE_STATEID
:
4880 case -NFS4ERR_EXPIRED
:
4884 err
= nfs4_handle_exception(server
, err
, &exception
);
4885 } while (exception
.retry
);
4889 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4890 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4893 * sleep, with exponential backoff, and retry the LOCK operation.
4895 static unsigned long
4896 nfs4_set_lock_task_retry(unsigned long timeout
)
4898 freezable_schedule_timeout_killable_unsafe(timeout
);
4900 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
4901 return NFS4_LOCK_MAXTIMEOUT
;
4905 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4907 struct inode
*inode
= state
->inode
;
4908 struct nfs_server
*server
= NFS_SERVER(inode
);
4909 struct nfs_client
*clp
= server
->nfs_client
;
4910 struct nfs_lockt_args arg
= {
4911 .fh
= NFS_FH(inode
),
4914 struct nfs_lockt_res res
= {
4917 struct rpc_message msg
= {
4918 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
4921 .rpc_cred
= state
->owner
->so_cred
,
4923 struct nfs4_lock_state
*lsp
;
4926 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
4927 status
= nfs4_set_lock_state(state
, request
);
4930 lsp
= request
->fl_u
.nfs4_fl
.owner
;
4931 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
4932 arg
.lock_owner
.s_dev
= server
->s_dev
;
4933 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4936 request
->fl_type
= F_UNLCK
;
4938 case -NFS4ERR_DENIED
:
4941 request
->fl_ops
->fl_release_private(request
);
4946 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4948 struct nfs4_exception exception
= { };
4952 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4953 _nfs4_proc_getlk(state
, cmd
, request
),
4955 } while (exception
.retry
);
4959 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
4962 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
4964 res
= posix_lock_file_wait(file
, fl
);
4967 res
= flock_lock_file_wait(file
, fl
);
4975 struct nfs4_unlockdata
{
4976 struct nfs_locku_args arg
;
4977 struct nfs_locku_res res
;
4978 struct nfs4_lock_state
*lsp
;
4979 struct nfs_open_context
*ctx
;
4980 struct file_lock fl
;
4981 const struct nfs_server
*server
;
4982 unsigned long timestamp
;
4985 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
4986 struct nfs_open_context
*ctx
,
4987 struct nfs4_lock_state
*lsp
,
4988 struct nfs_seqid
*seqid
)
4990 struct nfs4_unlockdata
*p
;
4991 struct inode
*inode
= lsp
->ls_state
->inode
;
4993 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
4996 p
->arg
.fh
= NFS_FH(inode
);
4998 p
->arg
.seqid
= seqid
;
4999 p
->res
.seqid
= seqid
;
5000 p
->arg
.stateid
= &lsp
->ls_stateid
;
5002 atomic_inc(&lsp
->ls_count
);
5003 /* Ensure we don't close file until we're done freeing locks! */
5004 p
->ctx
= get_nfs_open_context(ctx
);
5005 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5006 p
->server
= NFS_SERVER(inode
);
5010 static void nfs4_locku_release_calldata(void *data
)
5012 struct nfs4_unlockdata
*calldata
= data
;
5013 nfs_free_seqid(calldata
->arg
.seqid
);
5014 nfs4_put_lock_state(calldata
->lsp
);
5015 put_nfs_open_context(calldata
->ctx
);
5019 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
5021 struct nfs4_unlockdata
*calldata
= data
;
5023 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
5025 switch (task
->tk_status
) {
5027 nfs4_stateid_copy(&calldata
->lsp
->ls_stateid
,
5028 &calldata
->res
.stateid
);
5029 renew_lease(calldata
->server
, calldata
->timestamp
);
5031 case -NFS4ERR_BAD_STATEID
:
5032 case -NFS4ERR_OLD_STATEID
:
5033 case -NFS4ERR_STALE_STATEID
:
5034 case -NFS4ERR_EXPIRED
:
5037 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
5038 rpc_restart_call_prepare(task
);
5040 nfs_release_seqid(calldata
->arg
.seqid
);
5043 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
5045 struct nfs4_unlockdata
*calldata
= data
;
5047 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
5049 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
5050 /* Note: exit _without_ running nfs4_locku_done */
5053 calldata
->timestamp
= jiffies
;
5054 if (nfs4_setup_sequence(calldata
->server
,
5055 &calldata
->arg
.seq_args
,
5056 &calldata
->res
.seq_res
,
5058 nfs_release_seqid(calldata
->arg
.seqid
);
5061 task
->tk_action
= NULL
;
5063 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
5066 static const struct rpc_call_ops nfs4_locku_ops
= {
5067 .rpc_call_prepare
= nfs4_locku_prepare
,
5068 .rpc_call_done
= nfs4_locku_done
,
5069 .rpc_release
= nfs4_locku_release_calldata
,
5072 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
5073 struct nfs_open_context
*ctx
,
5074 struct nfs4_lock_state
*lsp
,
5075 struct nfs_seqid
*seqid
)
5077 struct nfs4_unlockdata
*data
;
5078 struct rpc_message msg
= {
5079 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
5080 .rpc_cred
= ctx
->cred
,
5082 struct rpc_task_setup task_setup_data
= {
5083 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
5084 .rpc_message
= &msg
,
5085 .callback_ops
= &nfs4_locku_ops
,
5086 .workqueue
= nfsiod_workqueue
,
5087 .flags
= RPC_TASK_ASYNC
,
5090 /* Ensure this is an unlock - when canceling a lock, the
5091 * canceled lock is passed in, and it won't be an unlock.
5093 fl
->fl_type
= F_UNLCK
;
5095 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
5097 nfs_free_seqid(seqid
);
5098 return ERR_PTR(-ENOMEM
);
5101 nfs41_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5102 msg
.rpc_argp
= &data
->arg
;
5103 msg
.rpc_resp
= &data
->res
;
5104 task_setup_data
.callback_data
= data
;
5105 return rpc_run_task(&task_setup_data
);
5108 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5110 struct inode
*inode
= state
->inode
;
5111 struct nfs4_state_owner
*sp
= state
->owner
;
5112 struct nfs_inode
*nfsi
= NFS_I(inode
);
5113 struct nfs_seqid
*seqid
;
5114 struct nfs4_lock_state
*lsp
;
5115 struct rpc_task
*task
;
5117 unsigned char fl_flags
= request
->fl_flags
;
5119 status
= nfs4_set_lock_state(state
, request
);
5120 /* Unlock _before_ we do the RPC call */
5121 request
->fl_flags
|= FL_EXISTS
;
5122 /* Exclude nfs_delegation_claim_locks() */
5123 mutex_lock(&sp
->so_delegreturn_mutex
);
5124 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5125 down_read(&nfsi
->rwsem
);
5126 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
5127 up_read(&nfsi
->rwsem
);
5128 mutex_unlock(&sp
->so_delegreturn_mutex
);
5131 up_read(&nfsi
->rwsem
);
5132 mutex_unlock(&sp
->so_delegreturn_mutex
);
5135 /* Is this a delegated lock? */
5136 lsp
= request
->fl_u
.nfs4_fl
.owner
;
5137 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) == 0)
5139 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
5143 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
5144 status
= PTR_ERR(task
);
5147 status
= nfs4_wait_for_completion_rpc_task(task
);
5150 request
->fl_flags
= fl_flags
;
5154 struct nfs4_lockdata
{
5155 struct nfs_lock_args arg
;
5156 struct nfs_lock_res res
;
5157 struct nfs4_lock_state
*lsp
;
5158 struct nfs_open_context
*ctx
;
5159 struct file_lock fl
;
5160 unsigned long timestamp
;
5163 struct nfs_server
*server
;
5166 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
5167 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
5170 struct nfs4_lockdata
*p
;
5171 struct inode
*inode
= lsp
->ls_state
->inode
;
5172 struct nfs_server
*server
= NFS_SERVER(inode
);
5174 p
= kzalloc(sizeof(*p
), gfp_mask
);
5178 p
->arg
.fh
= NFS_FH(inode
);
5180 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
5181 if (p
->arg
.open_seqid
== NULL
)
5183 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
5184 if (p
->arg
.lock_seqid
== NULL
)
5185 goto out_free_seqid
;
5186 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
5187 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5188 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5189 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
5190 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
5193 atomic_inc(&lsp
->ls_count
);
5194 p
->ctx
= get_nfs_open_context(ctx
);
5195 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
5198 nfs_free_seqid(p
->arg
.open_seqid
);
5204 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
5206 struct nfs4_lockdata
*data
= calldata
;
5207 struct nfs4_state
*state
= data
->lsp
->ls_state
;
5209 dprintk("%s: begin!\n", __func__
);
5210 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
5212 /* Do we need to do an open_to_lock_owner? */
5213 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
5214 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
5215 goto out_release_lock_seqid
;
5217 data
->arg
.open_stateid
= &state
->open_stateid
;
5218 data
->arg
.new_lock_owner
= 1;
5219 data
->res
.open_seqid
= data
->arg
.open_seqid
;
5221 data
->arg
.new_lock_owner
= 0;
5222 if (!nfs4_valid_open_stateid(state
)) {
5223 data
->rpc_status
= -EBADF
;
5224 task
->tk_action
= NULL
;
5225 goto out_release_open_seqid
;
5227 data
->timestamp
= jiffies
;
5228 if (nfs4_setup_sequence(data
->server
,
5229 &data
->arg
.seq_args
,
5233 out_release_open_seqid
:
5234 nfs_release_seqid(data
->arg
.open_seqid
);
5235 out_release_lock_seqid
:
5236 nfs_release_seqid(data
->arg
.lock_seqid
);
5238 nfs4_sequence_done(task
, &data
->res
.seq_res
);
5239 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
5242 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
5244 struct nfs4_lockdata
*data
= calldata
;
5246 dprintk("%s: begin!\n", __func__
);
5248 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5251 data
->rpc_status
= task
->tk_status
;
5252 if (data
->arg
.new_lock_owner
!= 0) {
5253 if (data
->rpc_status
== 0)
5254 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
5258 if (data
->rpc_status
== 0) {
5259 nfs4_stateid_copy(&data
->lsp
->ls_stateid
, &data
->res
.stateid
);
5260 set_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
);
5261 renew_lease(NFS_SERVER(data
->ctx
->dentry
->d_inode
), data
->timestamp
);
5264 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
5267 static void nfs4_lock_release(void *calldata
)
5269 struct nfs4_lockdata
*data
= calldata
;
5271 dprintk("%s: begin!\n", __func__
);
5272 nfs_free_seqid(data
->arg
.open_seqid
);
5273 if (data
->cancelled
!= 0) {
5274 struct rpc_task
*task
;
5275 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
5276 data
->arg
.lock_seqid
);
5278 rpc_put_task_async(task
);
5279 dprintk("%s: cancelling lock!\n", __func__
);
5281 nfs_free_seqid(data
->arg
.lock_seqid
);
5282 nfs4_put_lock_state(data
->lsp
);
5283 put_nfs_open_context(data
->ctx
);
5285 dprintk("%s: done!\n", __func__
);
5288 static const struct rpc_call_ops nfs4_lock_ops
= {
5289 .rpc_call_prepare
= nfs4_lock_prepare
,
5290 .rpc_call_done
= nfs4_lock_done
,
5291 .rpc_release
= nfs4_lock_release
,
5294 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
5297 case -NFS4ERR_ADMIN_REVOKED
:
5298 case -NFS4ERR_BAD_STATEID
:
5299 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5300 if (new_lock_owner
!= 0 ||
5301 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
5302 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
5304 case -NFS4ERR_STALE_STATEID
:
5305 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
5306 case -NFS4ERR_EXPIRED
:
5307 nfs4_schedule_lease_recovery(server
->nfs_client
);
5311 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
5313 struct nfs4_lockdata
*data
;
5314 struct rpc_task
*task
;
5315 struct rpc_message msg
= {
5316 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
5317 .rpc_cred
= state
->owner
->so_cred
,
5319 struct rpc_task_setup task_setup_data
= {
5320 .rpc_client
= NFS_CLIENT(state
->inode
),
5321 .rpc_message
= &msg
,
5322 .callback_ops
= &nfs4_lock_ops
,
5323 .workqueue
= nfsiod_workqueue
,
5324 .flags
= RPC_TASK_ASYNC
,
5328 dprintk("%s: begin!\n", __func__
);
5329 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
5330 fl
->fl_u
.nfs4_fl
.owner
,
5331 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
5335 data
->arg
.block
= 1;
5336 nfs41_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
5337 msg
.rpc_argp
= &data
->arg
;
5338 msg
.rpc_resp
= &data
->res
;
5339 task_setup_data
.callback_data
= data
;
5340 if (recovery_type
> NFS_LOCK_NEW
) {
5341 if (recovery_type
== NFS_LOCK_RECLAIM
)
5342 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
5343 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
5345 task
= rpc_run_task(&task_setup_data
);
5347 return PTR_ERR(task
);
5348 ret
= nfs4_wait_for_completion_rpc_task(task
);
5350 ret
= data
->rpc_status
;
5352 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
5353 data
->arg
.new_lock_owner
, ret
);
5355 data
->cancelled
= 1;
5357 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
5361 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
5363 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5364 struct nfs4_exception exception
= {
5365 .inode
= state
->inode
,
5370 /* Cache the lock if possible... */
5371 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5373 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
5374 if (err
!= -NFS4ERR_DELAY
)
5376 nfs4_handle_exception(server
, err
, &exception
);
5377 } while (exception
.retry
);
5381 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5383 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5384 struct nfs4_exception exception
= {
5385 .inode
= state
->inode
,
5389 err
= nfs4_set_lock_state(state
, request
);
5393 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
5395 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
5399 case -NFS4ERR_GRACE
:
5400 case -NFS4ERR_DELAY
:
5401 nfs4_handle_exception(server
, err
, &exception
);
5404 } while (exception
.retry
);
5409 #if defined(CONFIG_NFS_V4_1)
5411 * nfs41_check_expired_locks - possibly free a lock stateid
5413 * @state: NFSv4 state for an inode
5415 * Returns NFS_OK if recovery for this stateid is now finished.
5416 * Otherwise a negative NFS4ERR value is returned.
5418 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
5420 int status
, ret
= -NFS4ERR_BAD_STATEID
;
5421 struct nfs4_lock_state
*lsp
;
5422 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5424 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
5425 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
5426 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
5428 status
= nfs41_test_stateid(server
,
5431 if (status
!= NFS_OK
) {
5432 /* Free the stateid unless the server
5433 * informs us the stateid is unrecognized. */
5434 if (status
!= -NFS4ERR_BAD_STATEID
)
5435 nfs41_free_stateid(server
,
5438 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
5447 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
5449 int status
= NFS_OK
;
5451 if (test_bit(LK_STATE_IN_USE
, &state
->flags
))
5452 status
= nfs41_check_expired_locks(state
);
5453 if (status
!= NFS_OK
)
5454 status
= nfs4_lock_expired(state
, request
);
5459 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5461 struct nfs4_state_owner
*sp
= state
->owner
;
5462 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
5463 unsigned char fl_flags
= request
->fl_flags
;
5465 int status
= -ENOLCK
;
5467 if ((fl_flags
& FL_POSIX
) &&
5468 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
5470 /* Is this a delegated open? */
5471 status
= nfs4_set_lock_state(state
, request
);
5474 request
->fl_flags
|= FL_ACCESS
;
5475 status
= do_vfs_lock(request
->fl_file
, request
);
5478 down_read(&nfsi
->rwsem
);
5479 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
5480 /* Yes: cache locks! */
5481 /* ...but avoid races with delegation recall... */
5482 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
5483 status
= do_vfs_lock(request
->fl_file
, request
);
5486 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
5487 up_read(&nfsi
->rwsem
);
5488 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
5491 down_read(&nfsi
->rwsem
);
5492 if (read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
)) {
5493 status
= -NFS4ERR_DELAY
;
5496 /* Note: we always want to sleep here! */
5497 request
->fl_flags
= fl_flags
| FL_SLEEP
;
5498 if (do_vfs_lock(request
->fl_file
, request
) < 0)
5499 printk(KERN_WARNING
"NFS: %s: VFS is out of sync with lock "
5500 "manager!\n", __func__
);
5502 up_read(&nfsi
->rwsem
);
5504 request
->fl_flags
= fl_flags
;
5508 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
5510 struct nfs4_exception exception
= {
5512 .inode
= state
->inode
,
5517 err
= _nfs4_proc_setlk(state
, cmd
, request
);
5518 if (err
== -NFS4ERR_DENIED
)
5520 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
5522 } while (exception
.retry
);
5527 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
5529 struct nfs_open_context
*ctx
;
5530 struct nfs4_state
*state
;
5531 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
5534 /* verify open state */
5535 ctx
= nfs_file_open_context(filp
);
5538 if (request
->fl_start
< 0 || request
->fl_end
< 0)
5541 if (IS_GETLK(cmd
)) {
5543 return nfs4_proc_getlk(state
, F_GETLK
, request
);
5547 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
5550 if (request
->fl_type
== F_UNLCK
) {
5552 return nfs4_proc_unlck(state
, cmd
, request
);
5559 * Don't rely on the VFS having checked the file open mode,
5560 * since it won't do this for flock() locks.
5562 switch (request
->fl_type
) {
5564 if (!(filp
->f_mode
& FMODE_READ
))
5568 if (!(filp
->f_mode
& FMODE_WRITE
))
5573 status
= nfs4_proc_setlk(state
, cmd
, request
);
5574 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
5576 timeout
= nfs4_set_lock_task_retry(timeout
);
5577 status
= -ERESTARTSYS
;
5580 } while(status
< 0);
5584 int nfs4_lock_delegation_recall(struct file_lock
*fl
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
5586 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
5589 err
= nfs4_set_lock_state(state
, fl
);
5592 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
5593 return nfs4_handle_delegation_recall_error(server
, state
, stateid
, err
);
5596 struct nfs_release_lockowner_data
{
5597 struct nfs4_lock_state
*lsp
;
5598 struct nfs_server
*server
;
5599 struct nfs_release_lockowner_args args
;
5602 static void nfs4_release_lockowner_release(void *calldata
)
5604 struct nfs_release_lockowner_data
*data
= calldata
;
5605 nfs4_free_lock_state(data
->server
, data
->lsp
);
5609 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
5610 .rpc_release
= nfs4_release_lockowner_release
,
5613 static int nfs4_release_lockowner(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
5615 struct nfs_release_lockowner_data
*data
;
5616 struct rpc_message msg
= {
5617 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
5620 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
5622 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
5626 data
->server
= server
;
5627 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5628 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5629 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
5630 msg
.rpc_argp
= &data
->args
;
5631 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
5635 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5637 static int nfs4_xattr_set_nfs4_acl(struct dentry
*dentry
, const char *key
,
5638 const void *buf
, size_t buflen
,
5639 int flags
, int type
)
5641 if (strcmp(key
, "") != 0)
5644 return nfs4_proc_set_acl(dentry
->d_inode
, buf
, buflen
);
5647 static int nfs4_xattr_get_nfs4_acl(struct dentry
*dentry
, const char *key
,
5648 void *buf
, size_t buflen
, int type
)
5650 if (strcmp(key
, "") != 0)
5653 return nfs4_proc_get_acl(dentry
->d_inode
, buf
, buflen
);
5656 static size_t nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
, char *list
,
5657 size_t list_len
, const char *name
,
5658 size_t name_len
, int type
)
5660 size_t len
= sizeof(XATTR_NAME_NFSV4_ACL
);
5662 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
5665 if (list
&& len
<= list_len
)
5666 memcpy(list
, XATTR_NAME_NFSV4_ACL
, len
);
5670 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5671 static inline int nfs4_server_supports_labels(struct nfs_server
*server
)
5673 return server
->caps
& NFS_CAP_SECURITY_LABEL
;
5676 static int nfs4_xattr_set_nfs4_label(struct dentry
*dentry
, const char *key
,
5677 const void *buf
, size_t buflen
,
5678 int flags
, int type
)
5680 if (security_ismaclabel(key
))
5681 return nfs4_set_security_label(dentry
, buf
, buflen
);
5686 static int nfs4_xattr_get_nfs4_label(struct dentry
*dentry
, const char *key
,
5687 void *buf
, size_t buflen
, int type
)
5689 if (security_ismaclabel(key
))
5690 return nfs4_get_security_label(dentry
->d_inode
, buf
, buflen
);
5694 static size_t nfs4_xattr_list_nfs4_label(struct dentry
*dentry
, char *list
,
5695 size_t list_len
, const char *name
,
5696 size_t name_len
, int type
)
5700 if (nfs_server_capable(dentry
->d_inode
, NFS_CAP_SECURITY_LABEL
)) {
5701 len
= security_inode_listsecurity(dentry
->d_inode
, NULL
, 0);
5702 if (list
&& len
<= list_len
)
5703 security_inode_listsecurity(dentry
->d_inode
, list
, len
);
5708 static const struct xattr_handler nfs4_xattr_nfs4_label_handler
= {
5709 .prefix
= XATTR_SECURITY_PREFIX
,
5710 .list
= nfs4_xattr_list_nfs4_label
,
5711 .get
= nfs4_xattr_get_nfs4_label
,
5712 .set
= nfs4_xattr_set_nfs4_label
,
5718 * nfs_fhget will use either the mounted_on_fileid or the fileid
5720 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
5722 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
5723 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
5724 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
5725 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
5728 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
5729 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
5730 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
5734 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5735 const struct qstr
*name
,
5736 struct nfs4_fs_locations
*fs_locations
,
5739 struct nfs_server
*server
= NFS_SERVER(dir
);
5741 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
5743 struct nfs4_fs_locations_arg args
= {
5744 .dir_fh
= NFS_FH(dir
),
5749 struct nfs4_fs_locations_res res
= {
5750 .fs_locations
= fs_locations
,
5752 struct rpc_message msg
= {
5753 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
5759 dprintk("%s: start\n", __func__
);
5761 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5762 * is not supported */
5763 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
5764 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
5766 bitmask
[0] |= FATTR4_WORD0_FILEID
;
5768 nfs_fattr_init(&fs_locations
->fattr
);
5769 fs_locations
->server
= server
;
5770 fs_locations
->nlocations
= 0;
5771 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5772 dprintk("%s: returned status = %d\n", __func__
, status
);
5776 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5777 const struct qstr
*name
,
5778 struct nfs4_fs_locations
*fs_locations
,
5781 struct nfs4_exception exception
= { };
5784 err
= nfs4_handle_exception(NFS_SERVER(dir
),
5785 _nfs4_proc_fs_locations(client
, dir
, name
, fs_locations
, page
),
5787 } while (exception
.retry
);
5791 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
)
5794 struct nfs4_secinfo_arg args
= {
5795 .dir_fh
= NFS_FH(dir
),
5798 struct nfs4_secinfo_res res
= {
5801 struct rpc_message msg
= {
5802 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
5807 dprintk("NFS call secinfo %s\n", name
->name
);
5808 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5809 dprintk("NFS reply secinfo: %d\n", status
);
5813 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
5814 struct nfs4_secinfo_flavors
*flavors
)
5816 struct nfs4_exception exception
= { };
5819 err
= nfs4_handle_exception(NFS_SERVER(dir
),
5820 _nfs4_proc_secinfo(dir
, name
, flavors
),
5822 } while (exception
.retry
);
5826 #ifdef CONFIG_NFS_V4_1
5828 * Check the exchange flags returned by the server for invalid flags, having
5829 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5832 static int nfs4_check_cl_exchange_flags(u32 flags
)
5834 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
5836 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
5837 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
5839 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
5843 return -NFS4ERR_INVAL
;
5847 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
5848 struct nfs41_server_scope
*b
)
5850 if (a
->server_scope_sz
== b
->server_scope_sz
&&
5851 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
5858 * nfs4_proc_bind_conn_to_session()
5860 * The 4.1 client currently uses the same TCP connection for the
5861 * fore and backchannel.
5863 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5866 struct nfs41_bind_conn_to_session_res res
;
5867 struct rpc_message msg
= {
5869 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
5875 dprintk("--> %s\n", __func__
);
5877 res
.session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
5878 if (unlikely(res
.session
== NULL
)) {
5883 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5885 if (memcmp(res
.session
->sess_id
.data
,
5886 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
5887 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
5891 if (res
.dir
!= NFS4_CDFS4_BOTH
) {
5892 dprintk("NFS: %s: Unexpected direction from server\n",
5897 if (res
.use_conn_in_rdma_mode
) {
5898 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5907 dprintk("<-- %s status= %d\n", __func__
, status
);
5912 * nfs4_proc_exchange_id()
5914 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5916 * Since the clientid has expired, all compounds using sessions
5917 * associated with the stale clientid will be returning
5918 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5919 * be in some phase of session reset.
5921 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5923 nfs4_verifier verifier
;
5924 struct nfs41_exchange_id_args args
= {
5925 .verifier
= &verifier
,
5927 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
|
5928 EXCHGID4_FLAG_BIND_PRINC_STATEID
,
5930 struct nfs41_exchange_id_res res
= {
5934 struct rpc_message msg
= {
5935 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
5941 nfs4_init_boot_verifier(clp
, &verifier
);
5942 args
.id_len
= nfs4_init_uniform_client_string(clp
, args
.id
,
5944 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5945 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5946 args
.id_len
, args
.id
);
5948 res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
5950 if (unlikely(res
.server_owner
== NULL
)) {
5955 res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
5957 if (unlikely(res
.server_scope
== NULL
)) {
5959 goto out_server_owner
;
5962 res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
5963 if (unlikely(res
.impl_id
== NULL
)) {
5965 goto out_server_scope
;
5968 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5970 status
= nfs4_check_cl_exchange_flags(res
.flags
);
5973 clp
->cl_clientid
= res
.clientid
;
5974 clp
->cl_exchange_flags
= (res
.flags
& ~EXCHGID4_FLAG_CONFIRMED_R
);
5975 if (!(res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
))
5976 clp
->cl_seqid
= res
.seqid
;
5978 kfree(clp
->cl_serverowner
);
5979 clp
->cl_serverowner
= res
.server_owner
;
5980 res
.server_owner
= NULL
;
5982 /* use the most recent implementation id */
5983 kfree(clp
->cl_implid
);
5984 clp
->cl_implid
= res
.impl_id
;
5986 if (clp
->cl_serverscope
!= NULL
&&
5987 !nfs41_same_server_scope(clp
->cl_serverscope
,
5988 res
.server_scope
)) {
5989 dprintk("%s: server_scope mismatch detected\n",
5991 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
5992 kfree(clp
->cl_serverscope
);
5993 clp
->cl_serverscope
= NULL
;
5996 if (clp
->cl_serverscope
== NULL
) {
5997 clp
->cl_serverscope
= res
.server_scope
;
6004 kfree(res
.server_owner
);
6006 kfree(res
.server_scope
);
6008 if (clp
->cl_implid
!= NULL
)
6009 dprintk("NFS reply exchange_id: Server Implementation ID: "
6010 "domain: %s, name: %s, date: %llu,%u\n",
6011 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
6012 clp
->cl_implid
->date
.seconds
,
6013 clp
->cl_implid
->date
.nseconds
);
6014 dprintk("NFS reply exchange_id: %d\n", status
);
6018 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6019 struct rpc_cred
*cred
)
6021 struct rpc_message msg
= {
6022 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
6028 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6030 dprintk("NFS: Got error %d from the server %s on "
6031 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
6035 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
6036 struct rpc_cred
*cred
)
6041 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
6042 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
6044 case -NFS4ERR_DELAY
:
6045 case -NFS4ERR_CLIENTID_BUSY
:
6055 int nfs4_destroy_clientid(struct nfs_client
*clp
)
6057 struct rpc_cred
*cred
;
6060 if (clp
->cl_mvops
->minor_version
< 1)
6062 if (clp
->cl_exchange_flags
== 0)
6064 if (clp
->cl_preserve_clid
)
6066 cred
= nfs4_get_exchange_id_cred(clp
);
6067 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
6072 case -NFS4ERR_STALE_CLIENTID
:
6073 clp
->cl_exchange_flags
= 0;
6079 struct nfs4_get_lease_time_data
{
6080 struct nfs4_get_lease_time_args
*args
;
6081 struct nfs4_get_lease_time_res
*res
;
6082 struct nfs_client
*clp
;
6085 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
6088 struct nfs4_get_lease_time_data
*data
=
6089 (struct nfs4_get_lease_time_data
*)calldata
;
6091 dprintk("--> %s\n", __func__
);
6092 /* just setup sequence, do not trigger session recovery
6093 since we're invoked within one */
6094 nfs41_setup_sequence(data
->clp
->cl_session
,
6095 &data
->args
->la_seq_args
,
6096 &data
->res
->lr_seq_res
,
6098 dprintk("<-- %s\n", __func__
);
6102 * Called from nfs4_state_manager thread for session setup, so don't recover
6103 * from sequence operation or clientid errors.
6105 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
6107 struct nfs4_get_lease_time_data
*data
=
6108 (struct nfs4_get_lease_time_data
*)calldata
;
6110 dprintk("--> %s\n", __func__
);
6111 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
6113 switch (task
->tk_status
) {
6114 case -NFS4ERR_DELAY
:
6115 case -NFS4ERR_GRACE
:
6116 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
6117 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
6118 task
->tk_status
= 0;
6120 case -NFS4ERR_RETRY_UNCACHED_REP
:
6121 rpc_restart_call_prepare(task
);
6124 dprintk("<-- %s\n", __func__
);
6127 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
6128 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
6129 .rpc_call_done
= nfs4_get_lease_time_done
,
6132 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
6134 struct rpc_task
*task
;
6135 struct nfs4_get_lease_time_args args
;
6136 struct nfs4_get_lease_time_res res
= {
6137 .lr_fsinfo
= fsinfo
,
6139 struct nfs4_get_lease_time_data data
= {
6144 struct rpc_message msg
= {
6145 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
6149 struct rpc_task_setup task_setup
= {
6150 .rpc_client
= clp
->cl_rpcclient
,
6151 .rpc_message
= &msg
,
6152 .callback_ops
= &nfs4_get_lease_time_ops
,
6153 .callback_data
= &data
,
6154 .flags
= RPC_TASK_TIMEOUT
,
6158 nfs41_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
6159 nfs4_set_sequence_privileged(&args
.la_seq_args
);
6160 dprintk("--> %s\n", __func__
);
6161 task
= rpc_run_task(&task_setup
);
6164 status
= PTR_ERR(task
);
6166 status
= task
->tk_status
;
6169 dprintk("<-- %s return %d\n", __func__
, status
);
6175 * Initialize the values to be used by the client in CREATE_SESSION
6176 * If nfs4_init_session set the fore channel request and response sizes,
6179 * Set the back channel max_resp_sz_cached to zero to force the client to
6180 * always set csa_cachethis to FALSE because the current implementation
6181 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6183 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
6185 unsigned int max_rqst_sz
, max_resp_sz
;
6187 max_rqst_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxwrite_overhead
;
6188 max_resp_sz
= NFS_MAX_FILE_IO_SIZE
+ nfs41_maxread_overhead
;
6190 /* Fore channel attributes */
6191 args
->fc_attrs
.max_rqst_sz
= max_rqst_sz
;
6192 args
->fc_attrs
.max_resp_sz
= max_resp_sz
;
6193 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
6194 args
->fc_attrs
.max_reqs
= max_session_slots
;
6196 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6197 "max_ops=%u max_reqs=%u\n",
6199 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
6200 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
6202 /* Back channel attributes */
6203 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
6204 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
6205 args
->bc_attrs
.max_resp_sz_cached
= 0;
6206 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
6207 args
->bc_attrs
.max_reqs
= 1;
6209 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6210 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6212 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
6213 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
6214 args
->bc_attrs
.max_reqs
);
6217 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6219 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
6220 struct nfs4_channel_attrs
*rcvd
= &session
->fc_attrs
;
6222 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
6225 * Our requested max_ops is the minimum we need; we're not
6226 * prepared to break up compounds into smaller pieces than that.
6227 * So, no point even trying to continue if the server won't
6230 if (rcvd
->max_ops
< sent
->max_ops
)
6232 if (rcvd
->max_reqs
== 0)
6234 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
6235 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
6239 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
6241 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
6242 struct nfs4_channel_attrs
*rcvd
= &session
->bc_attrs
;
6244 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
6246 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
6248 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
6250 /* These would render the backchannel useless: */
6251 if (rcvd
->max_ops
!= sent
->max_ops
)
6253 if (rcvd
->max_reqs
!= sent
->max_reqs
)
6258 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
6259 struct nfs4_session
*session
)
6263 ret
= nfs4_verify_fore_channel_attrs(args
, session
);
6266 return nfs4_verify_back_channel_attrs(args
, session
);
6269 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
6270 struct rpc_cred
*cred
)
6272 struct nfs4_session
*session
= clp
->cl_session
;
6273 struct nfs41_create_session_args args
= {
6275 .cb_program
= NFS4_CALLBACK
,
6277 struct nfs41_create_session_res res
= {
6280 struct rpc_message msg
= {
6281 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
6288 nfs4_init_channel_attrs(&args
);
6289 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
6291 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6294 /* Verify the session's negotiated channel_attrs values */
6295 status
= nfs4_verify_channel_attrs(&args
, session
);
6296 /* Increment the clientid slot sequence id */
6304 * Issues a CREATE_SESSION operation to the server.
6305 * It is the responsibility of the caller to verify the session is
6306 * expired before calling this routine.
6308 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6312 struct nfs4_session
*session
= clp
->cl_session
;
6314 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
6316 status
= _nfs4_proc_create_session(clp
, cred
);
6320 /* Init or reset the session slot tables */
6321 status
= nfs4_setup_session_slot_tables(session
);
6322 dprintk("slot table setup returned %d\n", status
);
6326 ptr
= (unsigned *)&session
->sess_id
.data
[0];
6327 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
6328 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
6330 dprintk("<-- %s\n", __func__
);
6335 * Issue the over-the-wire RPC DESTROY_SESSION.
6336 * The caller must serialize access to this routine.
6338 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
6339 struct rpc_cred
*cred
)
6341 struct rpc_message msg
= {
6342 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
6343 .rpc_argp
= session
,
6348 dprintk("--> nfs4_proc_destroy_session\n");
6350 /* session is still being setup */
6351 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
6354 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
6357 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6358 "Session has been destroyed regardless...\n", status
);
6360 dprintk("<-- nfs4_proc_destroy_session\n");
6365 * Renew the cl_session lease.
6367 struct nfs4_sequence_data
{
6368 struct nfs_client
*clp
;
6369 struct nfs4_sequence_args args
;
6370 struct nfs4_sequence_res res
;
6373 static void nfs41_sequence_release(void *data
)
6375 struct nfs4_sequence_data
*calldata
= data
;
6376 struct nfs_client
*clp
= calldata
->clp
;
6378 if (atomic_read(&clp
->cl_count
) > 1)
6379 nfs4_schedule_state_renewal(clp
);
6380 nfs_put_client(clp
);
6384 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6386 switch(task
->tk_status
) {
6387 case -NFS4ERR_DELAY
:
6388 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6391 nfs4_schedule_lease_recovery(clp
);
6396 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
6398 struct nfs4_sequence_data
*calldata
= data
;
6399 struct nfs_client
*clp
= calldata
->clp
;
6401 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
6404 if (task
->tk_status
< 0) {
6405 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
6406 if (atomic_read(&clp
->cl_count
) == 1)
6409 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
6410 rpc_restart_call_prepare(task
);
6414 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
6416 dprintk("<-- %s\n", __func__
);
6419 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
6421 struct nfs4_sequence_data
*calldata
= data
;
6422 struct nfs_client
*clp
= calldata
->clp
;
6423 struct nfs4_sequence_args
*args
;
6424 struct nfs4_sequence_res
*res
;
6426 args
= task
->tk_msg
.rpc_argp
;
6427 res
= task
->tk_msg
.rpc_resp
;
6429 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
6432 static const struct rpc_call_ops nfs41_sequence_ops
= {
6433 .rpc_call_done
= nfs41_sequence_call_done
,
6434 .rpc_call_prepare
= nfs41_sequence_prepare
,
6435 .rpc_release
= nfs41_sequence_release
,
6438 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
6439 struct rpc_cred
*cred
,
6442 struct nfs4_sequence_data
*calldata
;
6443 struct rpc_message msg
= {
6444 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
6447 struct rpc_task_setup task_setup_data
= {
6448 .rpc_client
= clp
->cl_rpcclient
,
6449 .rpc_message
= &msg
,
6450 .callback_ops
= &nfs41_sequence_ops
,
6451 .flags
= RPC_TASK_ASYNC
| RPC_TASK_TIMEOUT
,
6454 if (!atomic_inc_not_zero(&clp
->cl_count
))
6455 return ERR_PTR(-EIO
);
6456 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6457 if (calldata
== NULL
) {
6458 nfs_put_client(clp
);
6459 return ERR_PTR(-ENOMEM
);
6461 nfs41_init_sequence(&calldata
->args
, &calldata
->res
, 0);
6463 nfs4_set_sequence_privileged(&calldata
->args
);
6464 msg
.rpc_argp
= &calldata
->args
;
6465 msg
.rpc_resp
= &calldata
->res
;
6466 calldata
->clp
= clp
;
6467 task_setup_data
.callback_data
= calldata
;
6469 return rpc_run_task(&task_setup_data
);
6472 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
6474 struct rpc_task
*task
;
6477 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
6479 task
= _nfs41_proc_sequence(clp
, cred
, false);
6481 ret
= PTR_ERR(task
);
6483 rpc_put_task_async(task
);
6484 dprintk("<-- %s status=%d\n", __func__
, ret
);
6488 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
6490 struct rpc_task
*task
;
6493 task
= _nfs41_proc_sequence(clp
, cred
, true);
6495 ret
= PTR_ERR(task
);
6498 ret
= rpc_wait_for_completion_task(task
);
6500 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
6502 if (task
->tk_status
== 0)
6503 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
6504 ret
= task
->tk_status
;
6508 dprintk("<-- %s status=%d\n", __func__
, ret
);
6512 struct nfs4_reclaim_complete_data
{
6513 struct nfs_client
*clp
;
6514 struct nfs41_reclaim_complete_args arg
;
6515 struct nfs41_reclaim_complete_res res
;
6518 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
6520 struct nfs4_reclaim_complete_data
*calldata
= data
;
6522 nfs41_setup_sequence(calldata
->clp
->cl_session
,
6523 &calldata
->arg
.seq_args
,
6524 &calldata
->res
.seq_res
,
6528 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
6530 switch(task
->tk_status
) {
6532 case -NFS4ERR_COMPLETE_ALREADY
:
6533 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
6535 case -NFS4ERR_DELAY
:
6536 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
6538 case -NFS4ERR_RETRY_UNCACHED_REP
:
6541 nfs4_schedule_lease_recovery(clp
);
6546 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
6548 struct nfs4_reclaim_complete_data
*calldata
= data
;
6549 struct nfs_client
*clp
= calldata
->clp
;
6550 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
6552 dprintk("--> %s\n", __func__
);
6553 if (!nfs41_sequence_done(task
, res
))
6556 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
6557 rpc_restart_call_prepare(task
);
6560 dprintk("<-- %s\n", __func__
);
6563 static void nfs4_free_reclaim_complete_data(void *data
)
6565 struct nfs4_reclaim_complete_data
*calldata
= data
;
6570 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
6571 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
6572 .rpc_call_done
= nfs4_reclaim_complete_done
,
6573 .rpc_release
= nfs4_free_reclaim_complete_data
,
6577 * Issue a global reclaim complete.
6579 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
,
6580 struct rpc_cred
*cred
)
6582 struct nfs4_reclaim_complete_data
*calldata
;
6583 struct rpc_task
*task
;
6584 struct rpc_message msg
= {
6585 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
6588 struct rpc_task_setup task_setup_data
= {
6589 .rpc_client
= clp
->cl_rpcclient
,
6590 .rpc_message
= &msg
,
6591 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
6592 .flags
= RPC_TASK_ASYNC
,
6594 int status
= -ENOMEM
;
6596 dprintk("--> %s\n", __func__
);
6597 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
6598 if (calldata
== NULL
)
6600 calldata
->clp
= clp
;
6601 calldata
->arg
.one_fs
= 0;
6603 nfs41_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
6604 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
6605 msg
.rpc_argp
= &calldata
->arg
;
6606 msg
.rpc_resp
= &calldata
->res
;
6607 task_setup_data
.callback_data
= calldata
;
6608 task
= rpc_run_task(&task_setup_data
);
6610 status
= PTR_ERR(task
);
6613 status
= nfs4_wait_for_completion_rpc_task(task
);
6615 status
= task
->tk_status
;
6619 dprintk("<-- %s status=%d\n", __func__
, status
);
6624 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
6626 struct nfs4_layoutget
*lgp
= calldata
;
6627 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
6628 struct nfs4_session
*session
= nfs4_get_session(server
);
6630 dprintk("--> %s\n", __func__
);
6631 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6632 * right now covering the LAYOUTGET we are about to send.
6633 * However, that is not so catastrophic, and there seems
6634 * to be no way to prevent it completely.
6636 if (nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
6637 &lgp
->res
.seq_res
, task
))
6639 if (pnfs_choose_layoutget_stateid(&lgp
->args
.stateid
,
6640 NFS_I(lgp
->args
.inode
)->layout
,
6641 lgp
->args
.ctx
->state
)) {
6642 rpc_exit(task
, NFS4_OK
);
6646 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
6648 struct nfs4_layoutget
*lgp
= calldata
;
6649 struct inode
*inode
= lgp
->args
.inode
;
6650 struct nfs_server
*server
= NFS_SERVER(inode
);
6651 struct pnfs_layout_hdr
*lo
;
6652 struct nfs4_state
*state
= NULL
;
6653 unsigned long timeo
, giveup
;
6655 dprintk("--> %s\n", __func__
);
6657 if (!nfs41_sequence_done(task
, &lgp
->res
.seq_res
))
6660 switch (task
->tk_status
) {
6663 case -NFS4ERR_LAYOUTTRYLATER
:
6664 case -NFS4ERR_RECALLCONFLICT
:
6665 timeo
= rpc_get_timeout(task
->tk_client
);
6666 giveup
= lgp
->args
.timestamp
+ timeo
;
6667 if (time_after(giveup
, jiffies
))
6668 task
->tk_status
= -NFS4ERR_DELAY
;
6670 case -NFS4ERR_EXPIRED
:
6671 case -NFS4ERR_BAD_STATEID
:
6672 spin_lock(&inode
->i_lock
);
6673 lo
= NFS_I(inode
)->layout
;
6674 if (!lo
|| list_empty(&lo
->plh_segs
)) {
6675 spin_unlock(&inode
->i_lock
);
6676 /* If the open stateid was bad, then recover it. */
6677 state
= lgp
->args
.ctx
->state
;
6681 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
);
6682 spin_unlock(&inode
->i_lock
);
6683 /* Mark the bad layout state as invalid, then
6684 * retry using the open stateid. */
6685 pnfs_free_lseg_list(&head
);
6688 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
6689 rpc_restart_call_prepare(task
);
6691 dprintk("<-- %s\n", __func__
);
6694 static size_t max_response_pages(struct nfs_server
*server
)
6696 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
6697 return nfs_page_array_len(0, max_resp_sz
);
6700 static void nfs4_free_pages(struct page
**pages
, size_t size
)
6707 for (i
= 0; i
< size
; i
++) {
6710 __free_page(pages
[i
]);
6715 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
6717 struct page
**pages
;
6720 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
6722 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
6726 for (i
= 0; i
< size
; i
++) {
6727 pages
[i
] = alloc_page(gfp_flags
);
6729 dprintk("%s: failed to allocate page\n", __func__
);
6730 nfs4_free_pages(pages
, size
);
6738 static void nfs4_layoutget_release(void *calldata
)
6740 struct nfs4_layoutget
*lgp
= calldata
;
6741 struct inode
*inode
= lgp
->args
.inode
;
6742 struct nfs_server
*server
= NFS_SERVER(inode
);
6743 size_t max_pages
= max_response_pages(server
);
6745 dprintk("--> %s\n", __func__
);
6746 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
6747 pnfs_put_layout_hdr(NFS_I(inode
)->layout
);
6748 put_nfs_open_context(lgp
->args
.ctx
);
6750 dprintk("<-- %s\n", __func__
);
6753 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
6754 .rpc_call_prepare
= nfs4_layoutget_prepare
,
6755 .rpc_call_done
= nfs4_layoutget_done
,
6756 .rpc_release
= nfs4_layoutget_release
,
6759 struct pnfs_layout_segment
*
6760 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, gfp_t gfp_flags
)
6762 struct inode
*inode
= lgp
->args
.inode
;
6763 struct nfs_server
*server
= NFS_SERVER(inode
);
6764 size_t max_pages
= max_response_pages(server
);
6765 struct rpc_task
*task
;
6766 struct rpc_message msg
= {
6767 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
6768 .rpc_argp
= &lgp
->args
,
6769 .rpc_resp
= &lgp
->res
,
6770 .rpc_cred
= lgp
->cred
,
6772 struct rpc_task_setup task_setup_data
= {
6773 .rpc_client
= server
->client
,
6774 .rpc_message
= &msg
,
6775 .callback_ops
= &nfs4_layoutget_call_ops
,
6776 .callback_data
= lgp
,
6777 .flags
= RPC_TASK_ASYNC
,
6779 struct pnfs_layout_segment
*lseg
= NULL
;
6782 dprintk("--> %s\n", __func__
);
6784 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
6785 if (!lgp
->args
.layout
.pages
) {
6786 nfs4_layoutget_release(lgp
);
6787 return ERR_PTR(-ENOMEM
);
6789 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
6790 lgp
->args
.timestamp
= jiffies
;
6792 lgp
->res
.layoutp
= &lgp
->args
.layout
;
6793 lgp
->res
.seq_res
.sr_slot
= NULL
;
6794 nfs41_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
6796 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6797 pnfs_get_layout_hdr(NFS_I(inode
)->layout
);
6799 task
= rpc_run_task(&task_setup_data
);
6801 return ERR_CAST(task
);
6802 status
= nfs4_wait_for_completion_rpc_task(task
);
6804 status
= task
->tk_status
;
6805 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6806 if (status
== 0 && lgp
->res
.layoutp
->len
)
6807 lseg
= pnfs_layout_process(lgp
);
6809 dprintk("<-- %s status=%d\n", __func__
, status
);
6811 return ERR_PTR(status
);
6816 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
6818 struct nfs4_layoutreturn
*lrp
= calldata
;
6820 dprintk("--> %s\n", __func__
);
6821 nfs41_setup_sequence(lrp
->clp
->cl_session
,
6822 &lrp
->args
.seq_args
,
6827 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
6829 struct nfs4_layoutreturn
*lrp
= calldata
;
6830 struct nfs_server
*server
;
6832 dprintk("--> %s\n", __func__
);
6834 if (!nfs41_sequence_done(task
, &lrp
->res
.seq_res
))
6837 server
= NFS_SERVER(lrp
->args
.inode
);
6838 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
6839 rpc_restart_call_prepare(task
);
6842 dprintk("<-- %s\n", __func__
);
6845 static void nfs4_layoutreturn_release(void *calldata
)
6847 struct nfs4_layoutreturn
*lrp
= calldata
;
6848 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
6850 dprintk("--> %s\n", __func__
);
6851 spin_lock(&lo
->plh_inode
->i_lock
);
6852 if (lrp
->res
.lrs_present
)
6853 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
6854 lo
->plh_block_lgets
--;
6855 spin_unlock(&lo
->plh_inode
->i_lock
);
6856 pnfs_put_layout_hdr(lrp
->args
.layout
);
6858 dprintk("<-- %s\n", __func__
);
6861 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
6862 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
6863 .rpc_call_done
= nfs4_layoutreturn_done
,
6864 .rpc_release
= nfs4_layoutreturn_release
,
6867 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
)
6869 struct rpc_task
*task
;
6870 struct rpc_message msg
= {
6871 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
6872 .rpc_argp
= &lrp
->args
,
6873 .rpc_resp
= &lrp
->res
,
6874 .rpc_cred
= lrp
->cred
,
6876 struct rpc_task_setup task_setup_data
= {
6877 .rpc_client
= lrp
->clp
->cl_rpcclient
,
6878 .rpc_message
= &msg
,
6879 .callback_ops
= &nfs4_layoutreturn_call_ops
,
6880 .callback_data
= lrp
,
6884 dprintk("--> %s\n", __func__
);
6885 nfs41_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
6886 task
= rpc_run_task(&task_setup_data
);
6888 return PTR_ERR(task
);
6889 status
= task
->tk_status
;
6890 dprintk("<-- %s status=%d\n", __func__
, status
);
6896 * Retrieve the list of Data Server devices from the MDS.
6898 static int _nfs4_getdevicelist(struct nfs_server
*server
,
6899 const struct nfs_fh
*fh
,
6900 struct pnfs_devicelist
*devlist
)
6902 struct nfs4_getdevicelist_args args
= {
6904 .layoutclass
= server
->pnfs_curr_ld
->id
,
6906 struct nfs4_getdevicelist_res res
= {
6909 struct rpc_message msg
= {
6910 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICELIST
],
6916 dprintk("--> %s\n", __func__
);
6917 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
,
6919 dprintk("<-- %s status=%d\n", __func__
, status
);
6923 int nfs4_proc_getdevicelist(struct nfs_server
*server
,
6924 const struct nfs_fh
*fh
,
6925 struct pnfs_devicelist
*devlist
)
6927 struct nfs4_exception exception
= { };
6931 err
= nfs4_handle_exception(server
,
6932 _nfs4_getdevicelist(server
, fh
, devlist
),
6934 } while (exception
.retry
);
6936 dprintk("%s: err=%d, num_devs=%u\n", __func__
,
6937 err
, devlist
->num_devs
);
6941 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist
);
6944 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
6945 struct pnfs_device
*pdev
,
6946 struct rpc_cred
*cred
)
6948 struct nfs4_getdeviceinfo_args args
= {
6951 struct nfs4_getdeviceinfo_res res
= {
6954 struct rpc_message msg
= {
6955 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
6962 dprintk("--> %s\n", __func__
);
6963 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6964 dprintk("<-- %s status=%d\n", __func__
, status
);
6969 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
,
6970 struct pnfs_device
*pdev
,
6971 struct rpc_cred
*cred
)
6973 struct nfs4_exception exception
= { };
6977 err
= nfs4_handle_exception(server
,
6978 _nfs4_proc_getdeviceinfo(server
, pdev
, cred
),
6980 } while (exception
.retry
);
6983 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
6985 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
6987 struct nfs4_layoutcommit_data
*data
= calldata
;
6988 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
6989 struct nfs4_session
*session
= nfs4_get_session(server
);
6991 nfs41_setup_sequence(session
,
6992 &data
->args
.seq_args
,
6998 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
7000 struct nfs4_layoutcommit_data
*data
= calldata
;
7001 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
7003 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
7006 switch (task
->tk_status
) { /* Just ignore these failures */
7007 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
7008 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
7009 case -NFS4ERR_BADLAYOUT
: /* no layout */
7010 case -NFS4ERR_GRACE
: /* loca_recalim always false */
7011 task
->tk_status
= 0;
7014 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
7018 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
7019 rpc_restart_call_prepare(task
);
7025 static void nfs4_layoutcommit_release(void *calldata
)
7027 struct nfs4_layoutcommit_data
*data
= calldata
;
7029 pnfs_cleanup_layoutcommit(data
);
7030 put_rpccred(data
->cred
);
7034 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
7035 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
7036 .rpc_call_done
= nfs4_layoutcommit_done
,
7037 .rpc_release
= nfs4_layoutcommit_release
,
7041 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
7043 struct rpc_message msg
= {
7044 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
7045 .rpc_argp
= &data
->args
,
7046 .rpc_resp
= &data
->res
,
7047 .rpc_cred
= data
->cred
,
7049 struct rpc_task_setup task_setup_data
= {
7050 .task
= &data
->task
,
7051 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
7052 .rpc_message
= &msg
,
7053 .callback_ops
= &nfs4_layoutcommit_ops
,
7054 .callback_data
= data
,
7055 .flags
= RPC_TASK_ASYNC
,
7057 struct rpc_task
*task
;
7060 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7061 "lbw: %llu inode %lu\n",
7062 data
->task
.tk_pid
, sync
,
7063 data
->args
.lastbytewritten
,
7064 data
->args
.inode
->i_ino
);
7066 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
7067 task
= rpc_run_task(&task_setup_data
);
7069 return PTR_ERR(task
);
7072 status
= nfs4_wait_for_completion_rpc_task(task
);
7075 status
= task
->tk_status
;
7077 dprintk("%s: status %d\n", __func__
, status
);
7083 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7084 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
7086 struct nfs41_secinfo_no_name_args args
= {
7087 .style
= SECINFO_STYLE_CURRENT_FH
,
7089 struct nfs4_secinfo_res res
= {
7092 struct rpc_message msg
= {
7093 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
7097 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
7101 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7102 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
7104 struct nfs4_exception exception
= { };
7107 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
7110 case -NFS4ERR_WRONGSEC
:
7111 case -NFS4ERR_NOTSUPP
:
7114 err
= nfs4_handle_exception(server
, err
, &exception
);
7116 } while (exception
.retry
);
7122 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
7123 struct nfs_fsinfo
*info
)
7127 rpc_authflavor_t flavor
;
7128 struct nfs4_secinfo_flavors
*flavors
;
7130 page
= alloc_page(GFP_KERNEL
);
7136 flavors
= page_address(page
);
7137 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
7140 * Fall back on "guess and check" method if
7141 * the server doesn't support SECINFO_NO_NAME
7143 if (err
== -NFS4ERR_WRONGSEC
|| err
== -NFS4ERR_NOTSUPP
) {
7144 err
= nfs4_find_root_sec(server
, fhandle
, info
);
7150 flavor
= nfs_find_best_sec(flavors
);
7152 err
= nfs4_lookup_root_sec(server
, fhandle
, info
, flavor
);
7162 static int _nfs41_test_stateid(struct nfs_server
*server
,
7163 nfs4_stateid
*stateid
,
7164 struct rpc_cred
*cred
)
7167 struct nfs41_test_stateid_args args
= {
7170 struct nfs41_test_stateid_res res
;
7171 struct rpc_message msg
= {
7172 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
7178 dprintk("NFS call test_stateid %p\n", stateid
);
7179 nfs41_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
7180 nfs4_set_sequence_privileged(&args
.seq_args
);
7181 status
= nfs4_call_sync_sequence(server
->client
, server
, &msg
,
7182 &args
.seq_args
, &res
.seq_res
);
7183 if (status
!= NFS_OK
) {
7184 dprintk("NFS reply test_stateid: failed, %d\n", status
);
7187 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
7192 * nfs41_test_stateid - perform a TEST_STATEID operation
7194 * @server: server / transport on which to perform the operation
7195 * @stateid: state ID to test
7198 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7199 * Otherwise a negative NFS4ERR value is returned if the operation
7200 * failed or the state ID is not currently valid.
7202 static int nfs41_test_stateid(struct nfs_server
*server
,
7203 nfs4_stateid
*stateid
,
7204 struct rpc_cred
*cred
)
7206 struct nfs4_exception exception
= { };
7209 err
= _nfs41_test_stateid(server
, stateid
, cred
);
7210 if (err
!= -NFS4ERR_DELAY
)
7212 nfs4_handle_exception(server
, err
, &exception
);
7213 } while (exception
.retry
);
7217 struct nfs_free_stateid_data
{
7218 struct nfs_server
*server
;
7219 struct nfs41_free_stateid_args args
;
7220 struct nfs41_free_stateid_res res
;
7223 static void nfs41_free_stateid_prepare(struct rpc_task
*task
, void *calldata
)
7225 struct nfs_free_stateid_data
*data
= calldata
;
7226 nfs41_setup_sequence(nfs4_get_session(data
->server
),
7227 &data
->args
.seq_args
,
7232 static void nfs41_free_stateid_done(struct rpc_task
*task
, void *calldata
)
7234 struct nfs_free_stateid_data
*data
= calldata
;
7236 nfs41_sequence_done(task
, &data
->res
.seq_res
);
7238 switch (task
->tk_status
) {
7239 case -NFS4ERR_DELAY
:
7240 if (nfs4_async_handle_error(task
, data
->server
, NULL
) == -EAGAIN
)
7241 rpc_restart_call_prepare(task
);
7245 static void nfs41_free_stateid_release(void *calldata
)
7250 const struct rpc_call_ops nfs41_free_stateid_ops
= {
7251 .rpc_call_prepare
= nfs41_free_stateid_prepare
,
7252 .rpc_call_done
= nfs41_free_stateid_done
,
7253 .rpc_release
= nfs41_free_stateid_release
,
7256 static struct rpc_task
*_nfs41_free_stateid(struct nfs_server
*server
,
7257 nfs4_stateid
*stateid
,
7258 struct rpc_cred
*cred
,
7261 struct rpc_message msg
= {
7262 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
7265 struct rpc_task_setup task_setup
= {
7266 .rpc_client
= server
->client
,
7267 .rpc_message
= &msg
,
7268 .callback_ops
= &nfs41_free_stateid_ops
,
7269 .flags
= RPC_TASK_ASYNC
,
7271 struct nfs_free_stateid_data
*data
;
7273 dprintk("NFS call free_stateid %p\n", stateid
);
7274 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
7276 return ERR_PTR(-ENOMEM
);
7277 data
->server
= server
;
7278 nfs4_stateid_copy(&data
->args
.stateid
, stateid
);
7280 task_setup
.callback_data
= data
;
7282 msg
.rpc_argp
= &data
->args
;
7283 msg
.rpc_resp
= &data
->res
;
7284 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
7286 nfs4_set_sequence_privileged(&data
->args
.seq_args
);
7288 return rpc_run_task(&task_setup
);
7292 * nfs41_free_stateid - perform a FREE_STATEID operation
7294 * @server: server / transport on which to perform the operation
7295 * @stateid: state ID to release
7298 * Returns NFS_OK if the server freed "stateid". Otherwise a
7299 * negative NFS4ERR value is returned.
7301 static int nfs41_free_stateid(struct nfs_server
*server
,
7302 nfs4_stateid
*stateid
,
7303 struct rpc_cred
*cred
)
7305 struct rpc_task
*task
;
7308 task
= _nfs41_free_stateid(server
, stateid
, cred
, true);
7310 return PTR_ERR(task
);
7311 ret
= rpc_wait_for_completion_task(task
);
7313 ret
= task
->tk_status
;
7318 static int nfs41_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
7320 struct rpc_task
*task
;
7321 struct rpc_cred
*cred
= lsp
->ls_state
->owner
->so_cred
;
7323 task
= _nfs41_free_stateid(server
, &lsp
->ls_stateid
, cred
, false);
7324 nfs4_free_lock_state(server
, lsp
);
7326 return PTR_ERR(task
);
7331 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
7332 const nfs4_stateid
*s2
)
7334 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
7337 if (s1
->seqid
== s2
->seqid
)
7339 if (s1
->seqid
== 0 || s2
->seqid
== 0)
7345 #endif /* CONFIG_NFS_V4_1 */
7347 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
7348 const nfs4_stateid
*s2
)
7350 return nfs4_stateid_match(s1
, s2
);
7354 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
7355 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7356 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7357 .recover_open
= nfs4_open_reclaim
,
7358 .recover_lock
= nfs4_lock_reclaim
,
7359 .establish_clid
= nfs4_init_clientid
,
7360 .get_clid_cred
= nfs4_get_setclientid_cred
,
7361 .detect_trunking
= nfs40_discover_server_trunking
,
7364 #if defined(CONFIG_NFS_V4_1)
7365 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
7366 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
7367 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
7368 .recover_open
= nfs4_open_reclaim
,
7369 .recover_lock
= nfs4_lock_reclaim
,
7370 .establish_clid
= nfs41_init_clientid
,
7371 .get_clid_cred
= nfs4_get_exchange_id_cred
,
7372 .reclaim_complete
= nfs41_proc_reclaim_complete
,
7373 .detect_trunking
= nfs41_discover_server_trunking
,
7375 #endif /* CONFIG_NFS_V4_1 */
7377 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
7378 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7379 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7380 .recover_open
= nfs4_open_expired
,
7381 .recover_lock
= nfs4_lock_expired
,
7382 .establish_clid
= nfs4_init_clientid
,
7383 .get_clid_cred
= nfs4_get_setclientid_cred
,
7386 #if defined(CONFIG_NFS_V4_1)
7387 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
7388 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
7389 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
7390 .recover_open
= nfs41_open_expired
,
7391 .recover_lock
= nfs41_lock_expired
,
7392 .establish_clid
= nfs41_init_clientid
,
7393 .get_clid_cred
= nfs4_get_exchange_id_cred
,
7395 #endif /* CONFIG_NFS_V4_1 */
7397 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
7398 .sched_state_renewal
= nfs4_proc_async_renew
,
7399 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
7400 .renew_lease
= nfs4_proc_renew
,
7403 #if defined(CONFIG_NFS_V4_1)
7404 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
7405 .sched_state_renewal
= nfs41_proc_async_sequence
,
7406 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
7407 .renew_lease
= nfs4_proc_sequence
,
7411 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
7413 .init_caps
= NFS_CAP_READDIRPLUS
7414 | NFS_CAP_ATOMIC_OPEN
7415 | NFS_CAP_CHANGE_ATTR
7416 | NFS_CAP_POSIX_LOCK
,
7417 .call_sync
= _nfs4_call_sync
,
7418 .match_stateid
= nfs4_match_stateid
,
7419 .find_root_sec
= nfs4_find_root_sec
,
7420 .free_lock_state
= nfs4_release_lockowner
,
7421 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
7422 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
7423 .state_renewal_ops
= &nfs40_state_renewal_ops
,
7426 #if defined(CONFIG_NFS_V4_1)
7427 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
7429 .init_caps
= NFS_CAP_READDIRPLUS
7430 | NFS_CAP_ATOMIC_OPEN
7431 | NFS_CAP_CHANGE_ATTR
7432 | NFS_CAP_POSIX_LOCK
7433 | NFS_CAP_STATEID_NFSV41
7434 | NFS_CAP_ATOMIC_OPEN_V1
,
7435 .call_sync
= nfs4_call_sync_sequence
,
7436 .match_stateid
= nfs41_match_stateid
,
7437 .find_root_sec
= nfs41_find_root_sec
,
7438 .free_lock_state
= nfs41_free_lock_state
,
7439 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7440 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7441 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7445 #if defined(CONFIG_NFS_V4_2)
7446 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops
= {
7448 .init_caps
= NFS_CAP_READDIRPLUS
7449 | NFS_CAP_ATOMIC_OPEN
7450 | NFS_CAP_CHANGE_ATTR
7451 | NFS_CAP_POSIX_LOCK
7452 | NFS_CAP_STATEID_NFSV41
7453 | NFS_CAP_ATOMIC_OPEN_V1
,
7454 .call_sync
= nfs4_call_sync_sequence
,
7455 .match_stateid
= nfs41_match_stateid
,
7456 .find_root_sec
= nfs41_find_root_sec
,
7457 .free_lock_state
= nfs41_free_lock_state
,
7458 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
7459 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
7460 .state_renewal_ops
= &nfs41_state_renewal_ops
,
7464 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
7465 [0] = &nfs_v4_0_minor_ops
,
7466 #if defined(CONFIG_NFS_V4_1)
7467 [1] = &nfs_v4_1_minor_ops
,
7469 #if defined(CONFIG_NFS_V4_2)
7470 [2] = &nfs_v4_2_minor_ops
,
7474 const struct inode_operations nfs4_dir_inode_operations
= {
7475 .create
= nfs_create
,
7476 .lookup
= nfs_lookup
,
7477 .atomic_open
= nfs_atomic_open
,
7479 .unlink
= nfs_unlink
,
7480 .symlink
= nfs_symlink
,
7484 .rename
= nfs_rename
,
7485 .permission
= nfs_permission
,
7486 .getattr
= nfs_getattr
,
7487 .setattr
= nfs_setattr
,
7488 .getxattr
= generic_getxattr
,
7489 .setxattr
= generic_setxattr
,
7490 .listxattr
= generic_listxattr
,
7491 .removexattr
= generic_removexattr
,
7494 static const struct inode_operations nfs4_file_inode_operations
= {
7495 .permission
= nfs_permission
,
7496 .getattr
= nfs_getattr
,
7497 .setattr
= nfs_setattr
,
7498 .getxattr
= generic_getxattr
,
7499 .setxattr
= generic_setxattr
,
7500 .listxattr
= generic_listxattr
,
7501 .removexattr
= generic_removexattr
,
7504 const struct nfs_rpc_ops nfs_v4_clientops
= {
7505 .version
= 4, /* protocol version */
7506 .dentry_ops
= &nfs4_dentry_operations
,
7507 .dir_inode_ops
= &nfs4_dir_inode_operations
,
7508 .file_inode_ops
= &nfs4_file_inode_operations
,
7509 .file_ops
= &nfs4_file_operations
,
7510 .getroot
= nfs4_proc_get_root
,
7511 .submount
= nfs4_submount
,
7512 .try_mount
= nfs4_try_mount
,
7513 .getattr
= nfs4_proc_getattr
,
7514 .setattr
= nfs4_proc_setattr
,
7515 .lookup
= nfs4_proc_lookup
,
7516 .access
= nfs4_proc_access
,
7517 .readlink
= nfs4_proc_readlink
,
7518 .create
= nfs4_proc_create
,
7519 .remove
= nfs4_proc_remove
,
7520 .unlink_setup
= nfs4_proc_unlink_setup
,
7521 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
7522 .unlink_done
= nfs4_proc_unlink_done
,
7523 .rename
= nfs4_proc_rename
,
7524 .rename_setup
= nfs4_proc_rename_setup
,
7525 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
7526 .rename_done
= nfs4_proc_rename_done
,
7527 .link
= nfs4_proc_link
,
7528 .symlink
= nfs4_proc_symlink
,
7529 .mkdir
= nfs4_proc_mkdir
,
7530 .rmdir
= nfs4_proc_remove
,
7531 .readdir
= nfs4_proc_readdir
,
7532 .mknod
= nfs4_proc_mknod
,
7533 .statfs
= nfs4_proc_statfs
,
7534 .fsinfo
= nfs4_proc_fsinfo
,
7535 .pathconf
= nfs4_proc_pathconf
,
7536 .set_capabilities
= nfs4_server_capabilities
,
7537 .decode_dirent
= nfs4_decode_dirent
,
7538 .read_setup
= nfs4_proc_read_setup
,
7539 .read_pageio_init
= pnfs_pageio_init_read
,
7540 .read_rpc_prepare
= nfs4_proc_read_rpc_prepare
,
7541 .read_done
= nfs4_read_done
,
7542 .write_setup
= nfs4_proc_write_setup
,
7543 .write_pageio_init
= pnfs_pageio_init_write
,
7544 .write_rpc_prepare
= nfs4_proc_write_rpc_prepare
,
7545 .write_done
= nfs4_write_done
,
7546 .commit_setup
= nfs4_proc_commit_setup
,
7547 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
7548 .commit_done
= nfs4_commit_done
,
7549 .lock
= nfs4_proc_lock
,
7550 .clear_acl_cache
= nfs4_zap_acl_attr
,
7551 .close_context
= nfs4_close_context
,
7552 .open_context
= nfs4_atomic_open
,
7553 .have_delegation
= nfs4_have_delegation
,
7554 .return_delegation
= nfs4_inode_return_delegation
,
7555 .alloc_client
= nfs4_alloc_client
,
7556 .init_client
= nfs4_init_client
,
7557 .free_client
= nfs4_free_client
,
7558 .create_server
= nfs4_create_server
,
7559 .clone_server
= nfs_clone_server
,
7562 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
7563 .prefix
= XATTR_NAME_NFSV4_ACL
,
7564 .list
= nfs4_xattr_list_nfs4_acl
,
7565 .get
= nfs4_xattr_get_nfs4_acl
,
7566 .set
= nfs4_xattr_set_nfs4_acl
,
7569 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
7570 &nfs4_xattr_nfs4_acl_handler
,
7571 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7572 &nfs4_xattr_nfs4_label_handler
,