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
*);
81 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
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
);
85 #ifdef CONFIG_NFS_V4_1
86 static int nfs41_test_stateid(struct nfs_server
*, nfs4_stateid
*);
87 static int nfs41_free_stateid(struct nfs_server
*, nfs4_stateid
*);
89 /* Prevent leaks of NFSv4 errors into userland */
90 static int nfs4_map_errors(int err
)
95 case -NFS4ERR_RESOURCE
:
97 case -NFS4ERR_WRONGSEC
:
99 case -NFS4ERR_BADOWNER
:
100 case -NFS4ERR_BADNAME
:
102 case -NFS4ERR_SHARE_DENIED
:
104 case -NFS4ERR_MINOR_VERS_MISMATCH
:
105 return -EPROTONOSUPPORT
;
106 case -NFS4ERR_ACCESS
:
109 dprintk("%s could not handle NFSv4 error %d\n",
117 * This is our standard bitmap for GETATTR requests.
119 const u32 nfs4_fattr_bitmap
[3] = {
121 | FATTR4_WORD0_CHANGE
124 | FATTR4_WORD0_FILEID
,
126 | FATTR4_WORD1_NUMLINKS
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
136 static const u32 nfs4_pnfs_open_bitmap
[3] = {
138 | FATTR4_WORD0_CHANGE
141 | FATTR4_WORD0_FILEID
,
143 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
,
151 FATTR4_WORD2_MDSTHRESHOLD
154 static const u32 nfs4_open_noattr_bitmap
[3] = {
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID
,
160 const u32 nfs4_statfs_bitmap
[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL
,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
169 const u32 nfs4_pathconf_bitmap
[2] = {
171 | FATTR4_WORD0_MAXNAME
,
175 const u32 nfs4_fsinfo_bitmap
[3] = { FATTR4_WORD0_MAXFILESIZE
176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME
,
179 FATTR4_WORD1_TIME_DELTA
180 | FATTR4_WORD1_FS_LAYOUT_TYPES
,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
184 const u32 nfs4_fs_locations_bitmap
[2] = {
186 | FATTR4_WORD0_CHANGE
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS
,
192 | FATTR4_WORD1_NUMLINKS
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
203 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
204 struct nfs4_readdir_arg
*readdir
)
209 readdir
->cookie
= cookie
;
210 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
215 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
226 start
= p
= kmap_atomic(*readdir
->pages
);
229 *p
++ = xdr_one
; /* next */
230 *p
++ = xdr_zero
; /* cookie, first word */
231 *p
++ = xdr_one
; /* cookie, second word */
232 *p
++ = xdr_one
; /* entry len */
233 memcpy(p
, ".\0\0\0", 4); /* entry */
235 *p
++ = xdr_one
; /* bitmap length */
236 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
237 *p
++ = htonl(8); /* attribute buffer length */
238 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
241 *p
++ = xdr_one
; /* next */
242 *p
++ = xdr_zero
; /* cookie, first word */
243 *p
++ = xdr_two
; /* cookie, second word */
244 *p
++ = xdr_two
; /* entry len */
245 memcpy(p
, "..\0\0", 4); /* entry */
247 *p
++ = xdr_one
; /* bitmap length */
248 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
249 *p
++ = htonl(8); /* attribute buffer length */
250 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
252 readdir
->pgbase
= (char *)p
- (char *)start
;
253 readdir
->count
-= readdir
->pgbase
;
254 kunmap_atomic(start
);
257 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
264 *timeout
= NFS4_POLL_RETRY_MIN
;
265 if (*timeout
> NFS4_POLL_RETRY_MAX
)
266 *timeout
= NFS4_POLL_RETRY_MAX
;
267 freezable_schedule_timeout_killable(*timeout
);
268 if (fatal_signal_pending(current
))
274 /* This is the error handling routine for processes that are allowed
277 static int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
279 struct nfs_client
*clp
= server
->nfs_client
;
280 struct nfs4_state
*state
= exception
->state
;
281 struct inode
*inode
= exception
->inode
;
284 exception
->retry
= 0;
288 case -NFS4ERR_OPENMODE
:
289 if (inode
&& nfs4_have_delegation(inode
, FMODE_READ
)) {
290 nfs4_inode_return_delegation(inode
);
291 exception
->retry
= 1;
296 nfs4_schedule_stateid_recovery(server
, state
);
297 goto wait_on_recovery
;
298 case -NFS4ERR_DELEG_REVOKED
:
299 case -NFS4ERR_ADMIN_REVOKED
:
300 case -NFS4ERR_BAD_STATEID
:
303 nfs_remove_bad_delegation(state
->inode
);
304 nfs4_schedule_stateid_recovery(server
, state
);
305 goto wait_on_recovery
;
306 case -NFS4ERR_EXPIRED
:
308 nfs4_schedule_stateid_recovery(server
, state
);
309 case -NFS4ERR_STALE_STATEID
:
310 case -NFS4ERR_STALE_CLIENTID
:
311 nfs4_schedule_lease_recovery(clp
);
312 goto wait_on_recovery
;
313 #if defined(CONFIG_NFS_V4_1)
314 case -NFS4ERR_BADSESSION
:
315 case -NFS4ERR_BADSLOT
:
316 case -NFS4ERR_BAD_HIGH_SLOT
:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
318 case -NFS4ERR_DEADSESSION
:
319 case -NFS4ERR_SEQ_FALSE_RETRY
:
320 case -NFS4ERR_SEQ_MISORDERED
:
321 dprintk("%s ERROR: %d Reset session\n", __func__
,
323 nfs4_schedule_session_recovery(clp
->cl_session
, errorcode
);
324 goto wait_on_recovery
;
325 #endif /* defined(CONFIG_NFS_V4_1) */
326 case -NFS4ERR_FILE_OPEN
:
327 if (exception
->timeout
> HZ
) {
328 /* We have retried a decent amount, time to
336 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
339 case -NFS4ERR_RETRY_UNCACHED_REP
:
340 case -NFS4ERR_OLD_STATEID
:
341 exception
->retry
= 1;
343 case -NFS4ERR_BADOWNER
:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME
:
346 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
347 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
348 exception
->retry
= 1;
349 printk(KERN_WARNING
"NFS: v4 server %s "
350 "does not accept raw "
352 "Reenabling the idmapper.\n",
353 server
->nfs_client
->cl_hostname
);
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret
);
359 ret
= nfs4_wait_clnt_recover(clp
);
361 exception
->retry
= 1;
366 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
368 spin_lock(&clp
->cl_lock
);
369 if (time_before(clp
->cl_last_renewal
,timestamp
))
370 clp
->cl_last_renewal
= timestamp
;
371 spin_unlock(&clp
->cl_lock
);
374 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
376 do_renew_lease(server
->nfs_client
, timestamp
);
379 #if defined(CONFIG_NFS_V4_1)
381 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
383 struct nfs4_session
*session
;
384 struct nfs4_slot_table
*tbl
;
385 bool send_new_highest_used_slotid
= false;
388 /* just wake up the next guy waiting since
389 * we may have not consumed a slot after all */
390 dprintk("%s: No slot\n", __func__
);
393 tbl
= res
->sr_slot
->table
;
394 session
= tbl
->session
;
396 spin_lock(&tbl
->slot_tbl_lock
);
397 /* Be nice to the server: try to ensure that the last transmitted
398 * value for highest_user_slotid <= target_highest_slotid
400 if (tbl
->highest_used_slotid
> tbl
->target_highest_slotid
)
401 send_new_highest_used_slotid
= true;
403 if (nfs41_wake_and_assign_slot(tbl
, res
->sr_slot
)) {
404 send_new_highest_used_slotid
= false;
407 nfs4_free_slot(tbl
, res
->sr_slot
);
409 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
)
410 send_new_highest_used_slotid
= false;
412 spin_unlock(&tbl
->slot_tbl_lock
);
414 if (send_new_highest_used_slotid
)
415 nfs41_server_notify_highest_slotid_update(session
->clp
);
418 static int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
420 struct nfs4_session
*session
;
421 struct nfs4_slot
*slot
;
422 struct nfs_client
*clp
;
423 bool interrupted
= false;
426 /* don't increment the sequence number if the task wasn't sent */
427 if (!RPC_WAS_SENT(task
))
431 session
= slot
->table
->session
;
433 if (slot
->interrupted
) {
434 slot
->interrupted
= 0;
438 /* Check the SEQUENCE operation status */
439 switch (res
->sr_status
) {
441 /* Update the slot's sequence and clientid lease timer */
444 do_renew_lease(clp
, res
->sr_timestamp
);
445 /* Check sequence flags */
446 if (res
->sr_status_flags
!= 0)
447 nfs4_schedule_lease_recovery(clp
);
448 nfs41_update_target_slotid(slot
->table
, slot
, res
);
452 * sr_status remains 1 if an RPC level error occurred.
453 * The server may or may not have processed the sequence
455 * Mark the slot as having hosted an interrupted RPC call.
457 slot
->interrupted
= 1;
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
469 case -NFS4ERR_BADSLOT
:
471 * The slot id we used was probably retired. Try again
472 * using a different slot id.
475 case -NFS4ERR_SEQ_MISORDERED
:
477 * Was the last operation on this sequence interrupted?
478 * If so, retry after bumping the sequence number.
485 * Could this slot have been previously retired?
486 * If so, then the server may be expecting seq_nr = 1!
488 if (slot
->seq_nr
!= 1) {
493 case -NFS4ERR_SEQ_FALSE_RETRY
:
497 /* Just update the slot sequence no. */
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
503 nfs41_sequence_free_slot(res
);
506 if (rpc_restart_call_prepare(task
)) {
512 if (!rpc_restart_call(task
))
514 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
518 static int nfs4_sequence_done(struct rpc_task
*task
,
519 struct nfs4_sequence_res
*res
)
521 if (res
->sr_slot
== NULL
)
523 return nfs41_sequence_done(task
, res
);
526 static void nfs41_init_sequence(struct nfs4_sequence_args
*args
,
527 struct nfs4_sequence_res
*res
, int cache_reply
)
529 args
->sa_slot
= NULL
;
530 args
->sa_cache_this
= 0;
531 args
->sa_privileged
= 0;
533 args
->sa_cache_this
= 1;
537 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
539 args
->sa_privileged
= 1;
542 int nfs41_setup_sequence(struct nfs4_session
*session
,
543 struct nfs4_sequence_args
*args
,
544 struct nfs4_sequence_res
*res
,
545 struct rpc_task
*task
)
547 struct nfs4_slot
*slot
;
548 struct nfs4_slot_table
*tbl
;
550 dprintk("--> %s\n", __func__
);
551 /* slot already allocated? */
552 if (res
->sr_slot
!= NULL
)
555 tbl
= &session
->fc_slot_table
;
557 task
->tk_timeout
= 0;
559 spin_lock(&tbl
->slot_tbl_lock
);
560 if (test_bit(NFS4_SESSION_DRAINING
, &session
->session_state
) &&
561 !args
->sa_privileged
) {
562 /* The state manager will wait until the slot table is empty */
563 dprintk("%s session is draining\n", __func__
);
567 slot
= nfs4_alloc_slot(tbl
);
569 /* If out of memory, try again in 1/4 second */
570 if (slot
== ERR_PTR(-ENOMEM
))
571 task
->tk_timeout
= HZ
>> 2;
572 dprintk("<-- %s: no free slots\n", __func__
);
575 spin_unlock(&tbl
->slot_tbl_lock
);
577 args
->sa_slot
= slot
;
579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
,
580 slot
->slot_nr
, slot
->seq_nr
);
583 res
->sr_timestamp
= jiffies
;
584 res
->sr_status_flags
= 0;
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
591 rpc_call_start(task
);
594 /* Privileged tasks are queued with top priority */
595 if (args
->sa_privileged
)
596 rpc_sleep_on_priority(&tbl
->slot_tbl_waitq
, task
,
597 NULL
, RPC_PRIORITY_PRIVILEGED
);
599 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
600 spin_unlock(&tbl
->slot_tbl_lock
);
603 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
605 int nfs4_setup_sequence(const struct nfs_server
*server
,
606 struct nfs4_sequence_args
*args
,
607 struct nfs4_sequence_res
*res
,
608 struct rpc_task
*task
)
610 struct nfs4_session
*session
= nfs4_get_session(server
);
613 if (session
== NULL
) {
614 rpc_call_start(task
);
618 dprintk("--> %s clp %p session %p sr_slot %d\n",
619 __func__
, session
->clp
, session
, res
->sr_slot
?
620 res
->sr_slot
->slot_nr
: -1);
622 ret
= nfs41_setup_sequence(session
, args
, res
, task
);
624 dprintk("<-- %s status=%d\n", __func__
, ret
);
628 struct nfs41_call_sync_data
{
629 const struct nfs_server
*seq_server
;
630 struct nfs4_sequence_args
*seq_args
;
631 struct nfs4_sequence_res
*seq_res
;
634 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
636 struct nfs41_call_sync_data
*data
= calldata
;
637 struct nfs4_session
*session
= nfs4_get_session(data
->seq_server
);
639 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
641 nfs41_setup_sequence(session
, data
->seq_args
, data
->seq_res
, task
);
644 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
646 struct nfs41_call_sync_data
*data
= calldata
;
648 nfs41_sequence_done(task
, data
->seq_res
);
651 static const struct rpc_call_ops nfs41_call_sync_ops
= {
652 .rpc_call_prepare
= nfs41_call_sync_prepare
,
653 .rpc_call_done
= nfs41_call_sync_done
,
656 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
657 struct nfs_server
*server
,
658 struct rpc_message
*msg
,
659 struct nfs4_sequence_args
*args
,
660 struct nfs4_sequence_res
*res
)
663 struct rpc_task
*task
;
664 struct nfs41_call_sync_data data
= {
665 .seq_server
= server
,
669 struct rpc_task_setup task_setup
= {
672 .callback_ops
= &nfs41_call_sync_ops
,
673 .callback_data
= &data
676 task
= rpc_run_task(&task_setup
);
680 ret
= task
->tk_status
;
688 void nfs41_init_sequence(struct nfs4_sequence_args
*args
,
689 struct nfs4_sequence_res
*res
, int cache_reply
)
693 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args
*args
)
698 static int nfs4_sequence_done(struct rpc_task
*task
,
699 struct nfs4_sequence_res
*res
)
703 #endif /* CONFIG_NFS_V4_1 */
706 int _nfs4_call_sync(struct rpc_clnt
*clnt
,
707 struct nfs_server
*server
,
708 struct rpc_message
*msg
,
709 struct nfs4_sequence_args
*args
,
710 struct nfs4_sequence_res
*res
)
712 return rpc_call_sync(clnt
, msg
, 0);
716 int nfs4_call_sync(struct rpc_clnt
*clnt
,
717 struct nfs_server
*server
,
718 struct rpc_message
*msg
,
719 struct nfs4_sequence_args
*args
,
720 struct nfs4_sequence_res
*res
,
723 nfs41_init_sequence(args
, res
, cache_reply
);
724 return server
->nfs_client
->cl_mvops
->call_sync(clnt
, server
, msg
,
728 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
730 struct nfs_inode
*nfsi
= NFS_I(dir
);
732 spin_lock(&dir
->i_lock
);
733 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_DATA
;
734 if (!cinfo
->atomic
|| cinfo
->before
!= dir
->i_version
)
735 nfs_force_lookup_revalidate(dir
);
736 dir
->i_version
= cinfo
->after
;
737 nfs_fscache_invalidate(dir
);
738 spin_unlock(&dir
->i_lock
);
741 struct nfs4_opendata
{
743 struct nfs_openargs o_arg
;
744 struct nfs_openres o_res
;
745 struct nfs_open_confirmargs c_arg
;
746 struct nfs_open_confirmres c_res
;
747 struct nfs4_string owner_name
;
748 struct nfs4_string group_name
;
749 struct nfs_fattr f_attr
;
751 struct dentry
*dentry
;
752 struct nfs4_state_owner
*owner
;
753 struct nfs4_state
*state
;
755 unsigned long timestamp
;
756 unsigned int rpc_done
: 1;
762 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
764 p
->o_res
.f_attr
= &p
->f_attr
;
765 p
->o_res
.seqid
= p
->o_arg
.seqid
;
766 p
->c_res
.seqid
= p
->c_arg
.seqid
;
767 p
->o_res
.server
= p
->o_arg
.server
;
768 p
->o_res
.access_request
= p
->o_arg
.access
;
769 nfs_fattr_init(&p
->f_attr
);
770 nfs_fattr_init_names(&p
->f_attr
, &p
->owner_name
, &p
->group_name
);
773 static struct nfs4_opendata
*nfs4_opendata_alloc(struct dentry
*dentry
,
774 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
775 const struct iattr
*attrs
,
778 struct dentry
*parent
= dget_parent(dentry
);
779 struct inode
*dir
= parent
->d_inode
;
780 struct nfs_server
*server
= NFS_SERVER(dir
);
781 struct nfs4_opendata
*p
;
783 p
= kzalloc(sizeof(*p
), gfp_mask
);
786 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
787 if (p
->o_arg
.seqid
== NULL
)
789 nfs_sb_active(dentry
->d_sb
);
790 p
->dentry
= dget(dentry
);
793 atomic_inc(&sp
->so_count
);
794 p
->o_arg
.fh
= NFS_FH(dir
);
795 p
->o_arg
.open_flags
= flags
;
796 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
797 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
798 * will return permission denied for all bits until close */
799 if (!(flags
& O_EXCL
)) {
800 /* ask server to check for all possible rights as results
802 p
->o_arg
.access
= NFS4_ACCESS_READ
| NFS4_ACCESS_MODIFY
|
803 NFS4_ACCESS_EXTEND
| NFS4_ACCESS_EXECUTE
;
805 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
806 p
->o_arg
.id
.create_time
= ktime_to_ns(sp
->so_seqid
.create_time
);
807 p
->o_arg
.id
.uniquifier
= sp
->so_seqid
.owner_id
;
808 p
->o_arg
.name
= &dentry
->d_name
;
809 p
->o_arg
.server
= server
;
810 p
->o_arg
.bitmask
= server
->attr_bitmask
;
811 p
->o_arg
.open_bitmap
= &nfs4_fattr_bitmap
[0];
812 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
813 if (attrs
!= NULL
&& attrs
->ia_valid
!= 0) {
816 p
->o_arg
.u
.attrs
= &p
->attrs
;
817 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
820 verf
[1] = current
->pid
;
821 memcpy(p
->o_arg
.u
.verifier
.data
, verf
,
822 sizeof(p
->o_arg
.u
.verifier
.data
));
824 p
->c_arg
.fh
= &p
->o_res
.fh
;
825 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
826 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
827 nfs4_init_opendata_res(p
);
837 static void nfs4_opendata_free(struct kref
*kref
)
839 struct nfs4_opendata
*p
= container_of(kref
,
840 struct nfs4_opendata
, kref
);
841 struct super_block
*sb
= p
->dentry
->d_sb
;
843 nfs_free_seqid(p
->o_arg
.seqid
);
844 if (p
->state
!= NULL
)
845 nfs4_put_open_state(p
->state
);
846 nfs4_put_state_owner(p
->owner
);
850 nfs_fattr_free_names(&p
->f_attr
);
854 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
857 kref_put(&p
->kref
, nfs4_opendata_free
);
860 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
864 ret
= rpc_wait_for_completion_task(task
);
868 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
872 if (open_mode
& (O_EXCL
|O_TRUNC
))
874 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
876 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
877 && state
->n_rdonly
!= 0;
880 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
881 && state
->n_wronly
!= 0;
883 case FMODE_READ
|FMODE_WRITE
:
884 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
885 && state
->n_rdwr
!= 0;
891 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
893 if (delegation
== NULL
)
895 if ((delegation
->type
& fmode
) != fmode
)
897 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
899 nfs_mark_delegation_referenced(delegation
);
903 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
912 case FMODE_READ
|FMODE_WRITE
:
915 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
918 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
920 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
921 nfs4_stateid_copy(&state
->stateid
, stateid
);
922 nfs4_stateid_copy(&state
->open_stateid
, stateid
);
925 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
928 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
930 case FMODE_READ
|FMODE_WRITE
:
931 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
935 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
937 write_seqlock(&state
->seqlock
);
938 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
939 write_sequnlock(&state
->seqlock
);
942 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
945 * Protect the call to nfs4_state_set_mode_locked and
946 * serialise the stateid update
948 write_seqlock(&state
->seqlock
);
949 if (deleg_stateid
!= NULL
) {
950 nfs4_stateid_copy(&state
->stateid
, deleg_stateid
);
951 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
953 if (open_stateid
!= NULL
)
954 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
955 write_sequnlock(&state
->seqlock
);
956 spin_lock(&state
->owner
->so_lock
);
957 update_open_stateflags(state
, fmode
);
958 spin_unlock(&state
->owner
->so_lock
);
961 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
963 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
964 struct nfs_delegation
*deleg_cur
;
967 fmode
&= (FMODE_READ
|FMODE_WRITE
);
970 deleg_cur
= rcu_dereference(nfsi
->delegation
);
971 if (deleg_cur
== NULL
)
974 spin_lock(&deleg_cur
->lock
);
975 if (nfsi
->delegation
!= deleg_cur
||
976 (deleg_cur
->type
& fmode
) != fmode
)
977 goto no_delegation_unlock
;
979 if (delegation
== NULL
)
980 delegation
= &deleg_cur
->stateid
;
981 else if (!nfs4_stateid_match(&deleg_cur
->stateid
, delegation
))
982 goto no_delegation_unlock
;
984 nfs_mark_delegation_referenced(deleg_cur
);
985 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
987 no_delegation_unlock
:
988 spin_unlock(&deleg_cur
->lock
);
992 if (!ret
&& open_stateid
!= NULL
) {
993 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
1001 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1003 struct nfs_delegation
*delegation
;
1006 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1007 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1012 nfs4_inode_return_delegation(inode
);
1015 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1017 struct nfs4_state
*state
= opendata
->state
;
1018 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1019 struct nfs_delegation
*delegation
;
1020 int open_mode
= opendata
->o_arg
.open_flags
& (O_EXCL
|O_TRUNC
);
1021 fmode_t fmode
= opendata
->o_arg
.fmode
;
1022 nfs4_stateid stateid
;
1026 if (can_open_cached(state
, fmode
, open_mode
)) {
1027 spin_lock(&state
->owner
->so_lock
);
1028 if (can_open_cached(state
, fmode
, open_mode
)) {
1029 update_open_stateflags(state
, fmode
);
1030 spin_unlock(&state
->owner
->so_lock
);
1031 goto out_return_state
;
1033 spin_unlock(&state
->owner
->so_lock
);
1036 delegation
= rcu_dereference(nfsi
->delegation
);
1037 if (!can_open_delegated(delegation
, fmode
)) {
1041 /* Save the delegation */
1042 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1044 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1049 /* Try to update the stateid using the delegation */
1050 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1051 goto out_return_state
;
1054 return ERR_PTR(ret
);
1056 atomic_inc(&state
->count
);
1061 nfs4_opendata_check_deleg(struct nfs4_opendata
*data
, struct nfs4_state
*state
)
1063 struct nfs_client
*clp
= NFS_SERVER(state
->inode
)->nfs_client
;
1064 struct nfs_delegation
*delegation
;
1065 int delegation_flags
= 0;
1068 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1070 delegation_flags
= delegation
->flags
;
1072 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_DELEGATE_CUR
) {
1073 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1074 "returning a delegation for "
1075 "OPEN(CLAIM_DELEGATE_CUR)\n",
1077 } else if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1078 nfs_inode_set_delegation(state
->inode
,
1079 data
->owner
->so_cred
,
1082 nfs_inode_reclaim_delegation(state
->inode
,
1083 data
->owner
->so_cred
,
1088 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1089 * and update the nfs4_state.
1091 static struct nfs4_state
*
1092 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata
*data
)
1094 struct inode
*inode
= data
->state
->inode
;
1095 struct nfs4_state
*state
= data
->state
;
1098 if (!data
->rpc_done
) {
1099 ret
= data
->rpc_status
;
1104 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR_TYPE
) ||
1105 !(data
->f_attr
.valid
& NFS_ATTR_FATTR_FILEID
) ||
1106 !(data
->f_attr
.valid
& NFS_ATTR_FATTR_CHANGE
))
1110 state
= nfs4_get_open_state(inode
, data
->owner
);
1114 ret
= nfs_refresh_inode(inode
, &data
->f_attr
);
1118 if (data
->o_res
.delegation_type
!= 0)
1119 nfs4_opendata_check_deleg(data
, state
);
1120 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1125 return ERR_PTR(ret
);
1129 static struct nfs4_state
*
1130 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1132 struct inode
*inode
;
1133 struct nfs4_state
*state
= NULL
;
1136 if (!data
->rpc_done
) {
1137 state
= nfs4_try_open_cached(data
);
1142 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1144 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
1145 ret
= PTR_ERR(inode
);
1149 state
= nfs4_get_open_state(inode
, data
->owner
);
1152 if (data
->o_res
.delegation_type
!= 0)
1153 nfs4_opendata_check_deleg(data
, state
);
1154 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1162 return ERR_PTR(ret
);
1165 static struct nfs4_state
*
1166 nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1168 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
)
1169 return _nfs4_opendata_reclaim_to_nfs4_state(data
);
1170 return _nfs4_opendata_to_nfs4_state(data
);
1173 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1175 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1176 struct nfs_open_context
*ctx
;
1178 spin_lock(&state
->inode
->i_lock
);
1179 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1180 if (ctx
->state
!= state
)
1182 get_nfs_open_context(ctx
);
1183 spin_unlock(&state
->inode
->i_lock
);
1186 spin_unlock(&state
->inode
->i_lock
);
1187 return ERR_PTR(-ENOENT
);
1190 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1192 struct nfs4_opendata
*opendata
;
1194 opendata
= nfs4_opendata_alloc(ctx
->dentry
, state
->owner
, 0, 0, NULL
, GFP_NOFS
);
1195 if (opendata
== NULL
)
1196 return ERR_PTR(-ENOMEM
);
1197 opendata
->state
= state
;
1198 atomic_inc(&state
->count
);
1202 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1204 struct nfs4_state
*newstate
;
1207 opendata
->o_arg
.open_flags
= 0;
1208 opendata
->o_arg
.fmode
= fmode
;
1209 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1210 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1211 nfs4_init_opendata_res(opendata
);
1212 ret
= _nfs4_recover_proc_open(opendata
);
1215 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1216 if (IS_ERR(newstate
))
1217 return PTR_ERR(newstate
);
1218 nfs4_close_state(newstate
, fmode
);
1223 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1225 struct nfs4_state
*newstate
;
1228 /* memory barrier prior to reading state->n_* */
1229 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1231 if (state
->n_rdwr
!= 0) {
1232 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1233 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1236 if (newstate
!= state
)
1239 if (state
->n_wronly
!= 0) {
1240 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1241 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1244 if (newstate
!= state
)
1247 if (state
->n_rdonly
!= 0) {
1248 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1249 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1252 if (newstate
!= state
)
1256 * We may have performed cached opens for all three recoveries.
1257 * Check if we need to update the current stateid.
1259 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1260 !nfs4_stateid_match(&state
->stateid
, &state
->open_stateid
)) {
1261 write_seqlock(&state
->seqlock
);
1262 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1263 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1264 write_sequnlock(&state
->seqlock
);
1271 * reclaim state on the server after a reboot.
1273 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1275 struct nfs_delegation
*delegation
;
1276 struct nfs4_opendata
*opendata
;
1277 fmode_t delegation_type
= 0;
1280 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1281 if (IS_ERR(opendata
))
1282 return PTR_ERR(opendata
);
1283 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1284 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1286 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1287 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1288 delegation_type
= delegation
->type
;
1290 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1291 status
= nfs4_open_recover(opendata
, state
);
1292 nfs4_opendata_put(opendata
);
1296 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1298 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1299 struct nfs4_exception exception
= { };
1302 err
= _nfs4_do_open_reclaim(ctx
, state
);
1303 if (err
!= -NFS4ERR_DELAY
)
1305 nfs4_handle_exception(server
, err
, &exception
);
1306 } while (exception
.retry
);
1310 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1312 struct nfs_open_context
*ctx
;
1315 ctx
= nfs4_state_find_open_context(state
);
1317 return PTR_ERR(ctx
);
1318 ret
= nfs4_do_open_reclaim(ctx
, state
);
1319 put_nfs_open_context(ctx
);
1323 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1325 struct nfs4_opendata
*opendata
;
1328 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1329 if (IS_ERR(opendata
))
1330 return PTR_ERR(opendata
);
1331 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1332 nfs4_stateid_copy(&opendata
->o_arg
.u
.delegation
, stateid
);
1333 ret
= nfs4_open_recover(opendata
, state
);
1334 nfs4_opendata_put(opendata
);
1338 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1340 struct nfs4_exception exception
= { };
1341 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1344 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1350 case -NFS4ERR_BADSESSION
:
1351 case -NFS4ERR_BADSLOT
:
1352 case -NFS4ERR_BAD_HIGH_SLOT
:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1354 case -NFS4ERR_DEADSESSION
:
1355 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
1357 case -NFS4ERR_STALE_CLIENTID
:
1358 case -NFS4ERR_STALE_STATEID
:
1359 case -NFS4ERR_EXPIRED
:
1360 /* Don't recall a delegation if it was lost */
1361 nfs4_schedule_lease_recovery(server
->nfs_client
);
1365 * The show must go on: exit, but mark the
1366 * stateid as needing recovery.
1368 case -NFS4ERR_DELEG_REVOKED
:
1369 case -NFS4ERR_ADMIN_REVOKED
:
1370 case -NFS4ERR_BAD_STATEID
:
1371 nfs_inode_find_state_and_recover(state
->inode
,
1373 nfs4_schedule_stateid_recovery(server
, state
);
1378 err
= nfs4_handle_exception(server
, err
, &exception
);
1379 } while (exception
.retry
);
1384 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1386 struct nfs4_opendata
*data
= calldata
;
1388 data
->rpc_status
= task
->tk_status
;
1389 if (data
->rpc_status
== 0) {
1390 nfs4_stateid_copy(&data
->o_res
.stateid
, &data
->c_res
.stateid
);
1391 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1392 renew_lease(data
->o_res
.server
, data
->timestamp
);
1397 static void nfs4_open_confirm_release(void *calldata
)
1399 struct nfs4_opendata
*data
= calldata
;
1400 struct nfs4_state
*state
= NULL
;
1402 /* If this request hasn't been cancelled, do nothing */
1403 if (data
->cancelled
== 0)
1405 /* In case of error, no cleanup! */
1406 if (!data
->rpc_done
)
1408 state
= nfs4_opendata_to_nfs4_state(data
);
1410 nfs4_close_state(state
, data
->o_arg
.fmode
);
1412 nfs4_opendata_put(data
);
1415 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1416 .rpc_call_done
= nfs4_open_confirm_done
,
1417 .rpc_release
= nfs4_open_confirm_release
,
1421 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1423 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1425 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1426 struct rpc_task
*task
;
1427 struct rpc_message msg
= {
1428 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1429 .rpc_argp
= &data
->c_arg
,
1430 .rpc_resp
= &data
->c_res
,
1431 .rpc_cred
= data
->owner
->so_cred
,
1433 struct rpc_task_setup task_setup_data
= {
1434 .rpc_client
= server
->client
,
1435 .rpc_message
= &msg
,
1436 .callback_ops
= &nfs4_open_confirm_ops
,
1437 .callback_data
= data
,
1438 .workqueue
= nfsiod_workqueue
,
1439 .flags
= RPC_TASK_ASYNC
,
1443 kref_get(&data
->kref
);
1445 data
->rpc_status
= 0;
1446 data
->timestamp
= jiffies
;
1447 task
= rpc_run_task(&task_setup_data
);
1449 return PTR_ERR(task
);
1450 status
= nfs4_wait_for_completion_rpc_task(task
);
1452 data
->cancelled
= 1;
1455 status
= data
->rpc_status
;
1460 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1462 struct nfs4_opendata
*data
= calldata
;
1463 struct nfs4_state_owner
*sp
= data
->owner
;
1465 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1468 * Check if we still need to send an OPEN call, or if we can use
1469 * a delegation instead.
1471 if (data
->state
!= NULL
) {
1472 struct nfs_delegation
*delegation
;
1474 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1477 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1478 if (data
->o_arg
.claim
!= NFS4_OPEN_CLAIM_DELEGATE_CUR
&&
1479 can_open_delegated(delegation
, data
->o_arg
.fmode
))
1480 goto unlock_no_action
;
1483 /* Update client id. */
1484 data
->o_arg
.clientid
= sp
->so_server
->nfs_client
->cl_clientid
;
1485 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1486 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1487 data
->o_arg
.open_bitmap
= &nfs4_open_noattr_bitmap
[0];
1488 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1490 data
->timestamp
= jiffies
;
1491 if (nfs4_setup_sequence(data
->o_arg
.server
,
1492 &data
->o_arg
.seq_args
,
1493 &data
->o_res
.seq_res
,
1495 nfs_release_seqid(data
->o_arg
.seqid
);
1500 task
->tk_action
= NULL
;
1501 nfs4_sequence_done(task
, &data
->o_res
.seq_res
);
1504 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1506 struct nfs4_opendata
*data
= calldata
;
1508 data
->rpc_status
= task
->tk_status
;
1510 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1513 if (task
->tk_status
== 0) {
1514 if (data
->o_res
.f_attr
->valid
& NFS_ATTR_FATTR_TYPE
) {
1515 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1519 data
->rpc_status
= -ELOOP
;
1522 data
->rpc_status
= -EISDIR
;
1525 data
->rpc_status
= -ENOTDIR
;
1528 renew_lease(data
->o_res
.server
, data
->timestamp
);
1529 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1530 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1535 static void nfs4_open_release(void *calldata
)
1537 struct nfs4_opendata
*data
= calldata
;
1538 struct nfs4_state
*state
= NULL
;
1540 /* If this request hasn't been cancelled, do nothing */
1541 if (data
->cancelled
== 0)
1543 /* In case of error, no cleanup! */
1544 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1546 /* In case we need an open_confirm, no cleanup! */
1547 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1549 state
= nfs4_opendata_to_nfs4_state(data
);
1551 nfs4_close_state(state
, data
->o_arg
.fmode
);
1553 nfs4_opendata_put(data
);
1556 static const struct rpc_call_ops nfs4_open_ops
= {
1557 .rpc_call_prepare
= nfs4_open_prepare
,
1558 .rpc_call_done
= nfs4_open_done
,
1559 .rpc_release
= nfs4_open_release
,
1562 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1564 struct inode
*dir
= data
->dir
->d_inode
;
1565 struct nfs_server
*server
= NFS_SERVER(dir
);
1566 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1567 struct nfs_openres
*o_res
= &data
->o_res
;
1568 struct rpc_task
*task
;
1569 struct rpc_message msg
= {
1570 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1573 .rpc_cred
= data
->owner
->so_cred
,
1575 struct rpc_task_setup task_setup_data
= {
1576 .rpc_client
= server
->client
,
1577 .rpc_message
= &msg
,
1578 .callback_ops
= &nfs4_open_ops
,
1579 .callback_data
= data
,
1580 .workqueue
= nfsiod_workqueue
,
1581 .flags
= RPC_TASK_ASYNC
,
1585 nfs41_init_sequence(&o_arg
->seq_args
, &o_res
->seq_res
, 1);
1586 kref_get(&data
->kref
);
1588 data
->rpc_status
= 0;
1589 data
->cancelled
= 0;
1591 nfs4_set_sequence_privileged(&o_arg
->seq_args
);
1592 task
= rpc_run_task(&task_setup_data
);
1594 return PTR_ERR(task
);
1595 status
= nfs4_wait_for_completion_rpc_task(task
);
1597 data
->cancelled
= 1;
1600 status
= data
->rpc_status
;
1606 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1608 struct inode
*dir
= data
->dir
->d_inode
;
1609 struct nfs_openres
*o_res
= &data
->o_res
;
1612 status
= nfs4_run_open_task(data
, 1);
1613 if (status
!= 0 || !data
->rpc_done
)
1616 nfs_fattr_map_and_free_names(NFS_SERVER(dir
), &data
->f_attr
);
1618 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1619 status
= _nfs4_proc_open_confirm(data
);
1627 static int nfs4_opendata_access(struct rpc_cred
*cred
,
1628 struct nfs4_opendata
*opendata
,
1629 struct nfs4_state
*state
, fmode_t fmode
,
1632 struct nfs_access_entry cache
;
1635 /* access call failed or for some reason the server doesn't
1636 * support any access modes -- defer access call until later */
1637 if (opendata
->o_res
.access_supported
== 0)
1641 /* don't check MAY_WRITE - a newly created file may not have
1642 * write mode bits, but POSIX allows the creating process to write.
1643 * use openflags to check for exec, because fmode won't
1644 * always have FMODE_EXEC set when file open for exec. */
1645 if (openflags
& __FMODE_EXEC
) {
1646 /* ONLY check for exec rights */
1648 } else if (fmode
& FMODE_READ
)
1652 cache
.jiffies
= jiffies
;
1653 nfs_access_set_mask(&cache
, opendata
->o_res
.access_result
);
1654 nfs_access_add_cache(state
->inode
, &cache
);
1656 if ((mask
& ~cache
.mask
& (MAY_READ
| MAY_EXEC
)) == 0)
1659 /* even though OPEN succeeded, access is denied. Close the file */
1660 nfs4_close_state(state
, fmode
);
1665 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1667 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1669 struct inode
*dir
= data
->dir
->d_inode
;
1670 struct nfs_server
*server
= NFS_SERVER(dir
);
1671 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1672 struct nfs_openres
*o_res
= &data
->o_res
;
1675 status
= nfs4_run_open_task(data
, 0);
1676 if (!data
->rpc_done
)
1679 if (status
== -NFS4ERR_BADNAME
&&
1680 !(o_arg
->open_flags
& O_CREAT
))
1685 nfs_fattr_map_and_free_names(server
, &data
->f_attr
);
1687 if (o_arg
->open_flags
& O_CREAT
)
1688 update_changeattr(dir
, &o_res
->cinfo
);
1689 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1690 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1691 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1692 status
= _nfs4_proc_open_confirm(data
);
1696 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1697 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1701 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1703 return nfs4_client_recover_expired_lease(server
->nfs_client
);
1708 * reclaim state on the server after a network partition.
1709 * Assumes caller holds the appropriate lock
1711 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1713 struct nfs4_opendata
*opendata
;
1716 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1717 if (IS_ERR(opendata
))
1718 return PTR_ERR(opendata
);
1719 ret
= nfs4_open_recover(opendata
, state
);
1721 d_drop(ctx
->dentry
);
1722 nfs4_opendata_put(opendata
);
1726 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1728 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1729 struct nfs4_exception exception
= { };
1733 err
= _nfs4_open_expired(ctx
, state
);
1737 case -NFS4ERR_GRACE
:
1738 case -NFS4ERR_DELAY
:
1739 nfs4_handle_exception(server
, err
, &exception
);
1742 } while (exception
.retry
);
1747 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1749 struct nfs_open_context
*ctx
;
1752 ctx
= nfs4_state_find_open_context(state
);
1754 return PTR_ERR(ctx
);
1755 ret
= nfs4_do_open_expired(ctx
, state
);
1756 put_nfs_open_context(ctx
);
1760 #if defined(CONFIG_NFS_V4_1)
1761 static void nfs41_clear_delegation_stateid(struct nfs4_state
*state
)
1763 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1764 nfs4_stateid
*stateid
= &state
->stateid
;
1767 /* If a state reset has been done, test_stateid is unneeded */
1768 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1771 status
= nfs41_test_stateid(server
, stateid
);
1772 if (status
!= NFS_OK
) {
1773 /* Free the stateid unless the server explicitly
1774 * informs us the stateid is unrecognized. */
1775 if (status
!= -NFS4ERR_BAD_STATEID
)
1776 nfs41_free_stateid(server
, stateid
);
1777 nfs_remove_bad_delegation(state
->inode
);
1779 write_seqlock(&state
->seqlock
);
1780 nfs4_stateid_copy(&state
->stateid
, &state
->open_stateid
);
1781 write_sequnlock(&state
->seqlock
);
1782 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1787 * nfs41_check_open_stateid - possibly free an open stateid
1789 * @state: NFSv4 state for an inode
1791 * Returns NFS_OK if recovery for this stateid is now finished.
1792 * Otherwise a negative NFS4ERR value is returned.
1794 static int nfs41_check_open_stateid(struct nfs4_state
*state
)
1796 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1797 nfs4_stateid
*stateid
= &state
->open_stateid
;
1800 /* If a state reset has been done, test_stateid is unneeded */
1801 if ((test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) == 0) &&
1802 (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) == 0) &&
1803 (test_bit(NFS_O_RDWR_STATE
, &state
->flags
) == 0))
1804 return -NFS4ERR_BAD_STATEID
;
1806 status
= nfs41_test_stateid(server
, stateid
);
1807 if (status
!= NFS_OK
) {
1808 /* Free the stateid unless the server explicitly
1809 * informs us the stateid is unrecognized. */
1810 if (status
!= -NFS4ERR_BAD_STATEID
)
1811 nfs41_free_stateid(server
, stateid
);
1813 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1814 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1815 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1820 static int nfs41_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1824 nfs41_clear_delegation_stateid(state
);
1825 status
= nfs41_check_open_stateid(state
);
1826 if (status
!= NFS_OK
)
1827 status
= nfs4_open_expired(sp
, state
);
1833 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1834 * fields corresponding to attributes that were used to store the verifier.
1835 * Make sure we clobber those fields in the later setattr call
1837 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1839 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1840 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1841 sattr
->ia_valid
|= ATTR_ATIME
;
1843 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1844 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1845 sattr
->ia_valid
|= ATTR_MTIME
;
1849 * Returns a referenced nfs4_state
1851 static int _nfs4_do_open(struct inode
*dir
,
1852 struct dentry
*dentry
,
1855 struct iattr
*sattr
,
1856 struct rpc_cred
*cred
,
1857 struct nfs4_state
**res
,
1858 struct nfs4_threshold
**ctx_th
)
1860 struct nfs4_state_owner
*sp
;
1861 struct nfs4_state
*state
= NULL
;
1862 struct nfs_server
*server
= NFS_SERVER(dir
);
1863 struct nfs4_opendata
*opendata
;
1866 /* Protect against reboot recovery conflicts */
1868 sp
= nfs4_get_state_owner(server
, cred
, GFP_KERNEL
);
1870 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1873 status
= nfs4_recover_expired_lease(server
);
1875 goto err_put_state_owner
;
1876 if (dentry
->d_inode
!= NULL
)
1877 nfs4_return_incompatible_delegation(dentry
->d_inode
, fmode
);
1879 opendata
= nfs4_opendata_alloc(dentry
, sp
, fmode
, flags
, sattr
, GFP_KERNEL
);
1880 if (opendata
== NULL
)
1881 goto err_put_state_owner
;
1883 if (ctx_th
&& server
->attr_bitmask
[2] & FATTR4_WORD2_MDSTHRESHOLD
) {
1884 opendata
->f_attr
.mdsthreshold
= pnfs_mdsthreshold_alloc();
1885 if (!opendata
->f_attr
.mdsthreshold
)
1886 goto err_opendata_put
;
1887 opendata
->o_arg
.open_bitmap
= &nfs4_pnfs_open_bitmap
[0];
1889 if (dentry
->d_inode
!= NULL
)
1890 opendata
->state
= nfs4_get_open_state(dentry
->d_inode
, sp
);
1892 status
= _nfs4_proc_open(opendata
);
1894 goto err_opendata_put
;
1896 state
= nfs4_opendata_to_nfs4_state(opendata
);
1897 status
= PTR_ERR(state
);
1899 goto err_opendata_put
;
1900 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
1901 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1903 status
= nfs4_opendata_access(cred
, opendata
, state
, fmode
, flags
);
1905 goto err_opendata_put
;
1907 if (opendata
->o_arg
.open_flags
& O_EXCL
) {
1908 nfs4_exclusive_attrset(opendata
, sattr
);
1910 nfs_fattr_init(opendata
->o_res
.f_attr
);
1911 status
= nfs4_do_setattr(state
->inode
, cred
,
1912 opendata
->o_res
.f_attr
, sattr
,
1915 nfs_setattr_update_inode(state
->inode
, sattr
);
1916 nfs_post_op_update_inode(state
->inode
, opendata
->o_res
.f_attr
);
1919 if (pnfs_use_threshold(ctx_th
, opendata
->f_attr
.mdsthreshold
, server
))
1920 *ctx_th
= opendata
->f_attr
.mdsthreshold
;
1922 kfree(opendata
->f_attr
.mdsthreshold
);
1923 opendata
->f_attr
.mdsthreshold
= NULL
;
1925 nfs4_opendata_put(opendata
);
1926 nfs4_put_state_owner(sp
);
1930 kfree(opendata
->f_attr
.mdsthreshold
);
1931 nfs4_opendata_put(opendata
);
1932 err_put_state_owner
:
1933 nfs4_put_state_owner(sp
);
1940 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
,
1941 struct dentry
*dentry
,
1944 struct iattr
*sattr
,
1945 struct rpc_cred
*cred
,
1946 struct nfs4_threshold
**ctx_th
)
1948 struct nfs4_exception exception
= { };
1949 struct nfs4_state
*res
;
1952 fmode
&= FMODE_READ
|FMODE_WRITE
|FMODE_EXEC
;
1954 status
= _nfs4_do_open(dir
, dentry
, fmode
, flags
, sattr
, cred
,
1958 /* NOTE: BAD_SEQID means the server and client disagree about the
1959 * book-keeping w.r.t. state-changing operations
1960 * (OPEN/CLOSE/LOCK/LOCKU...)
1961 * It is actually a sign of a bug on the client or on the server.
1963 * If we receive a BAD_SEQID error in the particular case of
1964 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1965 * have unhashed the old state_owner for us, and that we can
1966 * therefore safely retry using a new one. We should still warn
1967 * the user though...
1969 if (status
== -NFS4ERR_BAD_SEQID
) {
1970 pr_warn_ratelimited("NFS: v4 server %s "
1971 " returned a bad sequence-id error!\n",
1972 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1973 exception
.retry
= 1;
1977 * BAD_STATEID on OPEN means that the server cancelled our
1978 * state before it received the OPEN_CONFIRM.
1979 * Recover by retrying the request as per the discussion
1980 * on Page 181 of RFC3530.
1982 if (status
== -NFS4ERR_BAD_STATEID
) {
1983 exception
.retry
= 1;
1986 if (status
== -EAGAIN
) {
1987 /* We must have found a delegation */
1988 exception
.retry
= 1;
1991 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1992 status
, &exception
));
1993 } while (exception
.retry
);
1997 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1998 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1999 struct nfs4_state
*state
)
2001 struct nfs_server
*server
= NFS_SERVER(inode
);
2002 struct nfs_setattrargs arg
= {
2003 .fh
= NFS_FH(inode
),
2006 .bitmask
= server
->attr_bitmask
,
2008 struct nfs_setattrres res
= {
2012 struct rpc_message msg
= {
2013 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
2018 unsigned long timestamp
= jiffies
;
2021 nfs_fattr_init(fattr
);
2023 if (state
!= NULL
) {
2024 struct nfs_lockowner lockowner
= {
2025 .l_owner
= current
->files
,
2026 .l_pid
= current
->tgid
,
2028 nfs4_select_rw_stateid(&arg
.stateid
, state
, FMODE_WRITE
,
2030 } else if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
,
2032 /* Use that stateid */
2034 nfs4_stateid_copy(&arg
.stateid
, &zero_stateid
);
2036 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2037 if (status
== 0 && state
!= NULL
)
2038 renew_lease(server
, timestamp
);
2042 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
2043 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
2044 struct nfs4_state
*state
)
2046 struct nfs_server
*server
= NFS_SERVER(inode
);
2047 struct nfs4_exception exception
= {
2053 err
= _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2055 case -NFS4ERR_OPENMODE
:
2056 if (state
&& !(state
->state
& FMODE_WRITE
)) {
2058 if (sattr
->ia_valid
& ATTR_OPEN
)
2063 err
= nfs4_handle_exception(server
, err
, &exception
);
2064 } while (exception
.retry
);
2069 struct nfs4_closedata
{
2070 struct inode
*inode
;
2071 struct nfs4_state
*state
;
2072 struct nfs_closeargs arg
;
2073 struct nfs_closeres res
;
2074 struct nfs_fattr fattr
;
2075 unsigned long timestamp
;
2080 static void nfs4_free_closedata(void *data
)
2082 struct nfs4_closedata
*calldata
= data
;
2083 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
2084 struct super_block
*sb
= calldata
->state
->inode
->i_sb
;
2087 pnfs_roc_release(calldata
->state
->inode
);
2088 nfs4_put_open_state(calldata
->state
);
2089 nfs_free_seqid(calldata
->arg
.seqid
);
2090 nfs4_put_state_owner(sp
);
2091 nfs_sb_deactive_async(sb
);
2095 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
2098 spin_lock(&state
->owner
->so_lock
);
2099 if (!(fmode
& FMODE_READ
))
2100 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2101 if (!(fmode
& FMODE_WRITE
))
2102 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2103 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2104 spin_unlock(&state
->owner
->so_lock
);
2107 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
2109 struct nfs4_closedata
*calldata
= data
;
2110 struct nfs4_state
*state
= calldata
->state
;
2111 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
2113 dprintk("%s: begin!\n", __func__
);
2114 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
2116 /* hmm. we are done with the inode, and in the process of freeing
2117 * the state_owner. we keep this around to process errors
2119 switch (task
->tk_status
) {
2122 pnfs_roc_set_barrier(state
->inode
,
2123 calldata
->roc_barrier
);
2124 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
2125 renew_lease(server
, calldata
->timestamp
);
2126 nfs4_close_clear_stateid_flags(state
,
2127 calldata
->arg
.fmode
);
2129 case -NFS4ERR_STALE_STATEID
:
2130 case -NFS4ERR_OLD_STATEID
:
2131 case -NFS4ERR_BAD_STATEID
:
2132 case -NFS4ERR_EXPIRED
:
2133 if (calldata
->arg
.fmode
== 0)
2136 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
2137 rpc_restart_call_prepare(task
);
2139 nfs_release_seqid(calldata
->arg
.seqid
);
2140 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
2141 dprintk("%s: done, ret = %d!\n", __func__
, task
->tk_status
);
2144 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
2146 struct nfs4_closedata
*calldata
= data
;
2147 struct nfs4_state
*state
= calldata
->state
;
2148 struct inode
*inode
= calldata
->inode
;
2151 dprintk("%s: begin!\n", __func__
);
2152 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
2155 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
2156 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
2157 spin_lock(&state
->owner
->so_lock
);
2158 /* Calculate the change in open mode */
2159 if (state
->n_rdwr
== 0) {
2160 if (state
->n_rdonly
== 0) {
2161 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
2162 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2163 calldata
->arg
.fmode
&= ~FMODE_READ
;
2165 if (state
->n_wronly
== 0) {
2166 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
2167 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
2168 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
2171 spin_unlock(&state
->owner
->so_lock
);
2174 /* Note: exit _without_ calling nfs4_close_done */
2175 task
->tk_action
= NULL
;
2176 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
2180 if (calldata
->arg
.fmode
== 0) {
2181 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
2182 if (calldata
->roc
&&
2183 pnfs_roc_drain(inode
, &calldata
->roc_barrier
, task
))
2187 nfs_fattr_init(calldata
->res
.fattr
);
2188 calldata
->timestamp
= jiffies
;
2189 if (nfs4_setup_sequence(NFS_SERVER(inode
),
2190 &calldata
->arg
.seq_args
,
2191 &calldata
->res
.seq_res
,
2193 nfs_release_seqid(calldata
->arg
.seqid
);
2195 dprintk("%s: done!\n", __func__
);
2198 static const struct rpc_call_ops nfs4_close_ops
= {
2199 .rpc_call_prepare
= nfs4_close_prepare
,
2200 .rpc_call_done
= nfs4_close_done
,
2201 .rpc_release
= nfs4_free_closedata
,
2205 * It is possible for data to be read/written from a mem-mapped file
2206 * after the sys_close call (which hits the vfs layer as a flush).
2207 * This means that we can't safely call nfsv4 close on a file until
2208 * the inode is cleared. This in turn means that we are not good
2209 * NFSv4 citizens - we do not indicate to the server to update the file's
2210 * share state even when we are done with one of the three share
2211 * stateid's in the inode.
2213 * NOTE: Caller must be holding the sp->so_owner semaphore!
2215 int nfs4_do_close(struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
2217 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2218 struct nfs4_closedata
*calldata
;
2219 struct nfs4_state_owner
*sp
= state
->owner
;
2220 struct rpc_task
*task
;
2221 struct rpc_message msg
= {
2222 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
2223 .rpc_cred
= state
->owner
->so_cred
,
2225 struct rpc_task_setup task_setup_data
= {
2226 .rpc_client
= server
->client
,
2227 .rpc_message
= &msg
,
2228 .callback_ops
= &nfs4_close_ops
,
2229 .workqueue
= nfsiod_workqueue
,
2230 .flags
= RPC_TASK_ASYNC
,
2232 int status
= -ENOMEM
;
2234 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
2235 if (calldata
== NULL
)
2237 nfs41_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 1);
2238 calldata
->inode
= state
->inode
;
2239 calldata
->state
= state
;
2240 calldata
->arg
.fh
= NFS_FH(state
->inode
);
2241 calldata
->arg
.stateid
= &state
->open_stateid
;
2242 /* Serialization for the sequence id */
2243 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
2244 if (calldata
->arg
.seqid
== NULL
)
2245 goto out_free_calldata
;
2246 calldata
->arg
.fmode
= 0;
2247 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
2248 calldata
->res
.fattr
= &calldata
->fattr
;
2249 calldata
->res
.seqid
= calldata
->arg
.seqid
;
2250 calldata
->res
.server
= server
;
2251 calldata
->roc
= pnfs_roc(state
->inode
);
2252 nfs_sb_active(calldata
->inode
->i_sb
);
2254 msg
.rpc_argp
= &calldata
->arg
;
2255 msg
.rpc_resp
= &calldata
->res
;
2256 task_setup_data
.callback_data
= calldata
;
2257 task
= rpc_run_task(&task_setup_data
);
2259 return PTR_ERR(task
);
2262 status
= rpc_wait_for_completion_task(task
);
2268 nfs4_put_open_state(state
);
2269 nfs4_put_state_owner(sp
);
2273 static struct inode
*
2274 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
, int open_flags
, struct iattr
*attr
)
2276 struct nfs4_state
*state
;
2278 /* Protect against concurrent sillydeletes */
2279 state
= nfs4_do_open(dir
, ctx
->dentry
, ctx
->mode
, open_flags
, attr
,
2280 ctx
->cred
, &ctx
->mdsthreshold
);
2282 return ERR_CAST(state
);
2284 return igrab(state
->inode
);
2287 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2289 if (ctx
->state
== NULL
)
2292 nfs4_close_sync(ctx
->state
, ctx
->mode
);
2294 nfs4_close_state(ctx
->state
, ctx
->mode
);
2297 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2299 struct nfs4_server_caps_arg args
= {
2302 struct nfs4_server_caps_res res
= {};
2303 struct rpc_message msg
= {
2304 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2310 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2312 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2313 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2314 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2315 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2316 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2317 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2318 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2319 server
->caps
|= NFS_CAP_ACLS
;
2320 if (res
.has_links
!= 0)
2321 server
->caps
|= NFS_CAP_HARDLINKS
;
2322 if (res
.has_symlinks
!= 0)
2323 server
->caps
|= NFS_CAP_SYMLINKS
;
2324 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2325 server
->caps
|= NFS_CAP_FILEID
;
2326 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2327 server
->caps
|= NFS_CAP_MODE
;
2328 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2329 server
->caps
|= NFS_CAP_NLINK
;
2330 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2331 server
->caps
|= NFS_CAP_OWNER
;
2332 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2333 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2334 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2335 server
->caps
|= NFS_CAP_ATIME
;
2336 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2337 server
->caps
|= NFS_CAP_CTIME
;
2338 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2339 server
->caps
|= NFS_CAP_MTIME
;
2341 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2342 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2343 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2344 server
->acl_bitmask
= res
.acl_bitmask
;
2345 server
->fh_expire_type
= res
.fh_expire_type
;
2351 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2353 struct nfs4_exception exception
= { };
2356 err
= nfs4_handle_exception(server
,
2357 _nfs4_server_capabilities(server
, fhandle
),
2359 } while (exception
.retry
);
2363 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2364 struct nfs_fsinfo
*info
)
2366 struct nfs4_lookup_root_arg args
= {
2367 .bitmask
= nfs4_fattr_bitmap
,
2369 struct nfs4_lookup_res res
= {
2371 .fattr
= info
->fattr
,
2374 struct rpc_message msg
= {
2375 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2380 nfs_fattr_init(info
->fattr
);
2381 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2384 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2385 struct nfs_fsinfo
*info
)
2387 struct nfs4_exception exception
= { };
2390 err
= _nfs4_lookup_root(server
, fhandle
, info
);
2393 case -NFS4ERR_WRONGSEC
:
2396 err
= nfs4_handle_exception(server
, err
, &exception
);
2398 } while (exception
.retry
);
2403 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2404 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
2406 struct rpc_auth
*auth
;
2409 auth
= rpcauth_create(flavor
, server
->client
);
2414 ret
= nfs4_lookup_root(server
, fhandle
, info
);
2419 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2420 struct nfs_fsinfo
*info
)
2422 int i
, len
, status
= 0;
2423 rpc_authflavor_t flav_array
[NFS_MAX_SECFLAVORS
];
2425 len
= rpcauth_list_flavors(flav_array
, ARRAY_SIZE(flav_array
));
2429 for (i
= 0; i
< len
; i
++) {
2430 /* AUTH_UNIX is the default flavor if none was specified,
2431 * thus has already been tried. */
2432 if (flav_array
[i
] == RPC_AUTH_UNIX
)
2435 status
= nfs4_lookup_root_sec(server
, fhandle
, info
, flav_array
[i
]);
2436 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
2441 * -EACCESS could mean that the user doesn't have correct permissions
2442 * to access the mount. It could also mean that we tried to mount
2443 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2444 * existing mount programs don't handle -EACCES very well so it should
2445 * be mapped to -EPERM instead.
2447 if (status
== -EACCES
)
2453 * get the file handle for the "/" directory on the server
2455 int nfs4_proc_get_rootfh(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2456 struct nfs_fsinfo
*info
)
2458 int minor_version
= server
->nfs_client
->cl_minorversion
;
2459 int status
= nfs4_lookup_root(server
, fhandle
, info
);
2460 if ((status
== -NFS4ERR_WRONGSEC
) && !(server
->flags
& NFS_MOUNT_SECFLAVOUR
))
2462 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2463 * by nfs4_map_errors() as this function exits.
2465 status
= nfs_v4_minor_ops
[minor_version
]->find_root_sec(server
, fhandle
, info
);
2467 status
= nfs4_server_capabilities(server
, fhandle
);
2469 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2470 return nfs4_map_errors(status
);
2473 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*mntfh
,
2474 struct nfs_fsinfo
*info
)
2477 struct nfs_fattr
*fattr
= info
->fattr
;
2479 error
= nfs4_server_capabilities(server
, mntfh
);
2481 dprintk("nfs4_get_root: getcaps error = %d\n", -error
);
2485 error
= nfs4_proc_getattr(server
, mntfh
, fattr
);
2487 dprintk("nfs4_get_root: getattr error = %d\n", -error
);
2491 if (fattr
->valid
& NFS_ATTR_FATTR_FSID
&&
2492 !nfs_fsid_equal(&server
->fsid
, &fattr
->fsid
))
2493 memcpy(&server
->fsid
, &fattr
->fsid
, sizeof(server
->fsid
));
2499 * Get locations and (maybe) other attributes of a referral.
2500 * Note that we'll actually follow the referral later when
2501 * we detect fsid mismatch in inode revalidation
2503 static int nfs4_get_referral(struct rpc_clnt
*client
, struct inode
*dir
,
2504 const struct qstr
*name
, struct nfs_fattr
*fattr
,
2505 struct nfs_fh
*fhandle
)
2507 int status
= -ENOMEM
;
2508 struct page
*page
= NULL
;
2509 struct nfs4_fs_locations
*locations
= NULL
;
2511 page
= alloc_page(GFP_KERNEL
);
2514 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2515 if (locations
== NULL
)
2518 status
= nfs4_proc_fs_locations(client
, dir
, name
, locations
, page
);
2521 /* Make sure server returned a different fsid for the referral */
2522 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2523 dprintk("%s: server did not return a different fsid for"
2524 " a referral at %s\n", __func__
, name
->name
);
2528 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2529 nfs_fixup_referral_attributes(&locations
->fattr
);
2531 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2532 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2533 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2541 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2543 struct nfs4_getattr_arg args
= {
2545 .bitmask
= server
->attr_bitmask
,
2547 struct nfs4_getattr_res res
= {
2551 struct rpc_message msg
= {
2552 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2557 nfs_fattr_init(fattr
);
2558 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2561 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2563 struct nfs4_exception exception
= { };
2566 err
= nfs4_handle_exception(server
,
2567 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2569 } while (exception
.retry
);
2574 * The file is not closed if it is opened due to the a request to change
2575 * the size of the file. The open call will not be needed once the
2576 * VFS layer lookup-intents are implemented.
2578 * Close is called when the inode is destroyed.
2579 * If we haven't opened the file for O_WRONLY, we
2580 * need to in the size_change case to obtain a stateid.
2583 * Because OPEN is always done by name in nfsv4, it is
2584 * possible that we opened a different file by the same
2585 * name. We can recognize this race condition, but we
2586 * can't do anything about it besides returning an error.
2588 * This will be fixed with VFS changes (lookup-intent).
2591 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2592 struct iattr
*sattr
)
2594 struct inode
*inode
= dentry
->d_inode
;
2595 struct rpc_cred
*cred
= NULL
;
2596 struct nfs4_state
*state
= NULL
;
2599 if (pnfs_ld_layoutret_on_setattr(inode
))
2600 pnfs_return_layout(inode
);
2602 nfs_fattr_init(fattr
);
2604 /* Deal with open(O_TRUNC) */
2605 if (sattr
->ia_valid
& ATTR_OPEN
)
2606 sattr
->ia_valid
&= ~(ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
);
2608 /* Optimization: if the end result is no change, don't RPC */
2609 if ((sattr
->ia_valid
& ~(ATTR_FILE
)) == 0)
2612 /* Search for an existing open(O_WRITE) file */
2613 if (sattr
->ia_valid
& ATTR_FILE
) {
2614 struct nfs_open_context
*ctx
;
2616 ctx
= nfs_file_open_context(sattr
->ia_file
);
2623 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2625 nfs_setattr_update_inode(inode
, sattr
);
2629 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
2630 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2631 struct nfs_fattr
*fattr
)
2633 struct nfs_server
*server
= NFS_SERVER(dir
);
2635 struct nfs4_lookup_arg args
= {
2636 .bitmask
= server
->attr_bitmask
,
2637 .dir_fh
= NFS_FH(dir
),
2640 struct nfs4_lookup_res res
= {
2645 struct rpc_message msg
= {
2646 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2651 nfs_fattr_init(fattr
);
2653 dprintk("NFS call lookup %s\n", name
->name
);
2654 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2655 dprintk("NFS reply lookup: %d\n", status
);
2659 static void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
)
2661 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
2662 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_MOUNTPOINT
;
2663 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
2667 static int nfs4_proc_lookup_common(struct rpc_clnt
**clnt
, struct inode
*dir
,
2668 struct qstr
*name
, struct nfs_fh
*fhandle
,
2669 struct nfs_fattr
*fattr
)
2671 struct nfs4_exception exception
= { };
2672 struct rpc_clnt
*client
= *clnt
;
2675 err
= _nfs4_proc_lookup(client
, dir
, name
, fhandle
, fattr
);
2677 case -NFS4ERR_BADNAME
:
2680 case -NFS4ERR_MOVED
:
2681 err
= nfs4_get_referral(client
, dir
, name
, fattr
, fhandle
);
2683 case -NFS4ERR_WRONGSEC
:
2685 if (client
!= *clnt
)
2688 client
= nfs4_create_sec_client(client
, dir
, name
);
2690 return PTR_ERR(client
);
2692 exception
.retry
= 1;
2695 err
= nfs4_handle_exception(NFS_SERVER(dir
), err
, &exception
);
2697 } while (exception
.retry
);
2702 else if (client
!= *clnt
)
2703 rpc_shutdown_client(client
);
2708 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
2709 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2712 struct rpc_clnt
*client
= NFS_CLIENT(dir
);
2714 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
);
2715 if (client
!= NFS_CLIENT(dir
)) {
2716 rpc_shutdown_client(client
);
2717 nfs_fixup_secinfo_attributes(fattr
);
2723 nfs4_proc_lookup_mountpoint(struct inode
*dir
, struct qstr
*name
,
2724 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2727 struct rpc_clnt
*client
= rpc_clone_client(NFS_CLIENT(dir
));
2729 status
= nfs4_proc_lookup_common(&client
, dir
, name
, fhandle
, fattr
);
2731 rpc_shutdown_client(client
);
2732 return ERR_PTR(status
);
2737 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2739 struct nfs_server
*server
= NFS_SERVER(inode
);
2740 struct nfs4_accessargs args
= {
2741 .fh
= NFS_FH(inode
),
2742 .bitmask
= server
->cache_consistency_bitmask
,
2744 struct nfs4_accessres res
= {
2747 struct rpc_message msg
= {
2748 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2751 .rpc_cred
= entry
->cred
,
2753 int mode
= entry
->mask
;
2757 * Determine which access bits we want to ask for...
2759 if (mode
& MAY_READ
)
2760 args
.access
|= NFS4_ACCESS_READ
;
2761 if (S_ISDIR(inode
->i_mode
)) {
2762 if (mode
& MAY_WRITE
)
2763 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2764 if (mode
& MAY_EXEC
)
2765 args
.access
|= NFS4_ACCESS_LOOKUP
;
2767 if (mode
& MAY_WRITE
)
2768 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2769 if (mode
& MAY_EXEC
)
2770 args
.access
|= NFS4_ACCESS_EXECUTE
;
2773 res
.fattr
= nfs_alloc_fattr();
2774 if (res
.fattr
== NULL
)
2777 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2779 nfs_access_set_mask(entry
, res
.access
);
2780 nfs_refresh_inode(inode
, res
.fattr
);
2782 nfs_free_fattr(res
.fattr
);
2786 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2788 struct nfs4_exception exception
= { };
2791 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2792 _nfs4_proc_access(inode
, entry
),
2794 } while (exception
.retry
);
2799 * TODO: For the time being, we don't try to get any attributes
2800 * along with any of the zero-copy operations READ, READDIR,
2803 * In the case of the first three, we want to put the GETATTR
2804 * after the read-type operation -- this is because it is hard
2805 * to predict the length of a GETATTR response in v4, and thus
2806 * align the READ data correctly. This means that the GETATTR
2807 * may end up partially falling into the page cache, and we should
2808 * shift it into the 'tail' of the xdr_buf before processing.
2809 * To do this efficiently, we need to know the total length
2810 * of data received, which doesn't seem to be available outside
2813 * In the case of WRITE, we also want to put the GETATTR after
2814 * the operation -- in this case because we want to make sure
2815 * we get the post-operation mtime and size.
2817 * Both of these changes to the XDR layer would in fact be quite
2818 * minor, but I decided to leave them for a subsequent patch.
2820 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2821 unsigned int pgbase
, unsigned int pglen
)
2823 struct nfs4_readlink args
= {
2824 .fh
= NFS_FH(inode
),
2829 struct nfs4_readlink_res res
;
2830 struct rpc_message msg
= {
2831 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2836 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2839 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2840 unsigned int pgbase
, unsigned int pglen
)
2842 struct nfs4_exception exception
= { };
2845 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2846 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2848 } while (exception
.retry
);
2853 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2856 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2859 struct nfs_open_context
*ctx
;
2860 struct nfs4_state
*state
;
2863 ctx
= alloc_nfs_open_context(dentry
, FMODE_READ
);
2865 return PTR_ERR(ctx
);
2867 sattr
->ia_mode
&= ~current_umask();
2868 state
= nfs4_do_open(dir
, dentry
, ctx
->mode
,
2869 flags
, sattr
, ctx
->cred
,
2870 &ctx
->mdsthreshold
);
2872 if (IS_ERR(state
)) {
2873 status
= PTR_ERR(state
);
2876 d_add(dentry
, igrab(state
->inode
));
2877 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2880 put_nfs_open_context(ctx
);
2884 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2886 struct nfs_server
*server
= NFS_SERVER(dir
);
2887 struct nfs_removeargs args
= {
2891 struct nfs_removeres res
= {
2894 struct rpc_message msg
= {
2895 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2901 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
2903 update_changeattr(dir
, &res
.cinfo
);
2907 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2909 struct nfs4_exception exception
= { };
2912 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2913 _nfs4_proc_remove(dir
, name
),
2915 } while (exception
.retry
);
2919 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2921 struct nfs_server
*server
= NFS_SERVER(dir
);
2922 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2923 struct nfs_removeres
*res
= msg
->rpc_resp
;
2925 res
->server
= server
;
2926 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2927 nfs41_init_sequence(&args
->seq_args
, &res
->seq_res
, 1);
2930 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task
*task
, struct nfs_unlinkdata
*data
)
2932 nfs4_setup_sequence(NFS_SERVER(data
->dir
),
2933 &data
->args
.seq_args
,
2938 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2940 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2942 if (!nfs4_sequence_done(task
, &res
->seq_res
))
2944 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2946 update_changeattr(dir
, &res
->cinfo
);
2950 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
2952 struct nfs_server
*server
= NFS_SERVER(dir
);
2953 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
2954 struct nfs_renameres
*res
= msg
->rpc_resp
;
2956 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
2957 res
->server
= server
;
2958 nfs41_init_sequence(&arg
->seq_args
, &res
->seq_res
, 1);
2961 static void nfs4_proc_rename_rpc_prepare(struct rpc_task
*task
, struct nfs_renamedata
*data
)
2963 nfs4_setup_sequence(NFS_SERVER(data
->old_dir
),
2964 &data
->args
.seq_args
,
2969 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
2970 struct inode
*new_dir
)
2972 struct nfs_renameres
*res
= task
->tk_msg
.rpc_resp
;
2974 if (!nfs4_sequence_done(task
, &res
->seq_res
))
2976 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2979 update_changeattr(old_dir
, &res
->old_cinfo
);
2980 update_changeattr(new_dir
, &res
->new_cinfo
);
2984 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2985 struct inode
*new_dir
, struct qstr
*new_name
)
2987 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2988 struct nfs_renameargs arg
= {
2989 .old_dir
= NFS_FH(old_dir
),
2990 .new_dir
= NFS_FH(new_dir
),
2991 .old_name
= old_name
,
2992 .new_name
= new_name
,
2994 struct nfs_renameres res
= {
2997 struct rpc_message msg
= {
2998 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
3002 int status
= -ENOMEM
;
3004 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3006 update_changeattr(old_dir
, &res
.old_cinfo
);
3007 update_changeattr(new_dir
, &res
.new_cinfo
);
3012 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
3013 struct inode
*new_dir
, struct qstr
*new_name
)
3015 struct nfs4_exception exception
= { };
3018 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
3019 _nfs4_proc_rename(old_dir
, old_name
,
3022 } while (exception
.retry
);
3026 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3028 struct nfs_server
*server
= NFS_SERVER(inode
);
3029 struct nfs4_link_arg arg
= {
3030 .fh
= NFS_FH(inode
),
3031 .dir_fh
= NFS_FH(dir
),
3033 .bitmask
= server
->attr_bitmask
,
3035 struct nfs4_link_res res
= {
3038 struct rpc_message msg
= {
3039 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
3043 int status
= -ENOMEM
;
3045 res
.fattr
= nfs_alloc_fattr();
3046 if (res
.fattr
== NULL
)
3049 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3051 update_changeattr(dir
, &res
.cinfo
);
3052 nfs_post_op_update_inode(inode
, res
.fattr
);
3055 nfs_free_fattr(res
.fattr
);
3059 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
3061 struct nfs4_exception exception
= { };
3064 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3065 _nfs4_proc_link(inode
, dir
, name
),
3067 } while (exception
.retry
);
3071 struct nfs4_createdata
{
3072 struct rpc_message msg
;
3073 struct nfs4_create_arg arg
;
3074 struct nfs4_create_res res
;
3076 struct nfs_fattr fattr
;
3079 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
3080 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
3082 struct nfs4_createdata
*data
;
3084 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3086 struct nfs_server
*server
= NFS_SERVER(dir
);
3088 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
3089 data
->msg
.rpc_argp
= &data
->arg
;
3090 data
->msg
.rpc_resp
= &data
->res
;
3091 data
->arg
.dir_fh
= NFS_FH(dir
);
3092 data
->arg
.server
= server
;
3093 data
->arg
.name
= name
;
3094 data
->arg
.attrs
= sattr
;
3095 data
->arg
.ftype
= ftype
;
3096 data
->arg
.bitmask
= server
->attr_bitmask
;
3097 data
->res
.server
= server
;
3098 data
->res
.fh
= &data
->fh
;
3099 data
->res
.fattr
= &data
->fattr
;
3100 nfs_fattr_init(data
->res
.fattr
);
3105 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
3107 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
3108 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
3110 update_changeattr(dir
, &data
->res
.dir_cinfo
);
3111 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
3116 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
3121 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3122 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
3124 struct nfs4_createdata
*data
;
3125 int status
= -ENAMETOOLONG
;
3127 if (len
> NFS4_MAXPATHLEN
)
3131 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
3135 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
3136 data
->arg
.u
.symlink
.pages
= &page
;
3137 data
->arg
.u
.symlink
.len
= len
;
3139 status
= nfs4_do_create(dir
, dentry
, data
);
3141 nfs4_free_createdata(data
);
3146 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
3147 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
3149 struct nfs4_exception exception
= { };
3152 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3153 _nfs4_proc_symlink(dir
, dentry
, page
,
3156 } while (exception
.retry
);
3160 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3161 struct iattr
*sattr
)
3163 struct nfs4_createdata
*data
;
3164 int status
= -ENOMEM
;
3166 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
3170 status
= nfs4_do_create(dir
, dentry
, data
);
3172 nfs4_free_createdata(data
);
3177 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
3178 struct iattr
*sattr
)
3180 struct nfs4_exception exception
= { };
3183 sattr
->ia_mode
&= ~current_umask();
3185 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3186 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
3188 } while (exception
.retry
);
3192 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3193 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3195 struct inode
*dir
= dentry
->d_inode
;
3196 struct nfs4_readdir_arg args
= {
3201 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
3204 struct nfs4_readdir_res res
;
3205 struct rpc_message msg
= {
3206 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
3213 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
3214 dentry
->d_parent
->d_name
.name
,
3215 dentry
->d_name
.name
,
3216 (unsigned long long)cookie
);
3217 nfs4_setup_readdir(cookie
, NFS_I(dir
)->cookieverf
, dentry
, &args
);
3218 res
.pgbase
= args
.pgbase
;
3219 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3221 memcpy(NFS_I(dir
)->cookieverf
, res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
3222 status
+= args
.pgbase
;
3225 nfs_invalidate_atime(dir
);
3227 dprintk("%s: returns %d\n", __func__
, status
);
3231 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3232 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3234 struct nfs4_exception exception
= { };
3237 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
3238 _nfs4_proc_readdir(dentry
, cred
, cookie
,
3239 pages
, count
, plus
),
3241 } while (exception
.retry
);
3245 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3246 struct iattr
*sattr
, dev_t rdev
)
3248 struct nfs4_createdata
*data
;
3249 int mode
= sattr
->ia_mode
;
3250 int status
= -ENOMEM
;
3252 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
3257 data
->arg
.ftype
= NF4FIFO
;
3258 else if (S_ISBLK(mode
)) {
3259 data
->arg
.ftype
= NF4BLK
;
3260 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3261 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3263 else if (S_ISCHR(mode
)) {
3264 data
->arg
.ftype
= NF4CHR
;
3265 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3266 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3267 } else if (!S_ISSOCK(mode
)) {
3272 status
= nfs4_do_create(dir
, dentry
, data
);
3274 nfs4_free_createdata(data
);
3279 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3280 struct iattr
*sattr
, dev_t rdev
)
3282 struct nfs4_exception exception
= { };
3285 sattr
->ia_mode
&= ~current_umask();
3287 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3288 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
3290 } while (exception
.retry
);
3294 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3295 struct nfs_fsstat
*fsstat
)
3297 struct nfs4_statfs_arg args
= {
3299 .bitmask
= server
->attr_bitmask
,
3301 struct nfs4_statfs_res res
= {
3304 struct rpc_message msg
= {
3305 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
3310 nfs_fattr_init(fsstat
->fattr
);
3311 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3314 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
3316 struct nfs4_exception exception
= { };
3319 err
= nfs4_handle_exception(server
,
3320 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
3322 } while (exception
.retry
);
3326 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3327 struct nfs_fsinfo
*fsinfo
)
3329 struct nfs4_fsinfo_arg args
= {
3331 .bitmask
= server
->attr_bitmask
,
3333 struct nfs4_fsinfo_res res
= {
3336 struct rpc_message msg
= {
3337 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3342 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3345 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3347 struct nfs4_exception exception
= { };
3351 err
= nfs4_handle_exception(server
,
3352 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
3354 } while (exception
.retry
);
3358 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3362 nfs_fattr_init(fsinfo
->fattr
);
3363 error
= nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3365 /* block layout checks this! */
3366 server
->pnfs_blksize
= fsinfo
->blksize
;
3367 set_pnfs_layoutdriver(server
, fhandle
, fsinfo
->layouttype
);
3373 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3374 struct nfs_pathconf
*pathconf
)
3376 struct nfs4_pathconf_arg args
= {
3378 .bitmask
= server
->attr_bitmask
,
3380 struct nfs4_pathconf_res res
= {
3381 .pathconf
= pathconf
,
3383 struct rpc_message msg
= {
3384 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3389 /* None of the pathconf attributes are mandatory to implement */
3390 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3391 memset(pathconf
, 0, sizeof(*pathconf
));
3395 nfs_fattr_init(pathconf
->fattr
);
3396 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3399 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3400 struct nfs_pathconf
*pathconf
)
3402 struct nfs4_exception exception
= { };
3406 err
= nfs4_handle_exception(server
,
3407 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3409 } while (exception
.retry
);
3413 void __nfs4_read_done_cb(struct nfs_read_data
*data
)
3415 nfs_invalidate_atime(data
->header
->inode
);
3418 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_read_data
*data
)
3420 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
3422 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3423 rpc_restart_call_prepare(task
);
3427 __nfs4_read_done_cb(data
);
3428 if (task
->tk_status
> 0)
3429 renew_lease(server
, data
->timestamp
);
3433 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3436 dprintk("--> %s\n", __func__
);
3438 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3441 return data
->read_done_cb
? data
->read_done_cb(task
, data
) :
3442 nfs4_read_done_cb(task
, data
);
3445 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3447 data
->timestamp
= jiffies
;
3448 data
->read_done_cb
= nfs4_read_done_cb
;
3449 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3450 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 0);
3453 static void nfs4_proc_read_rpc_prepare(struct rpc_task
*task
, struct nfs_read_data
*data
)
3455 nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
3456 &data
->args
.seq_args
,
3461 static int nfs4_write_done_cb(struct rpc_task
*task
, struct nfs_write_data
*data
)
3463 struct inode
*inode
= data
->header
->inode
;
3465 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3466 rpc_restart_call_prepare(task
);
3469 if (task
->tk_status
>= 0) {
3470 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3471 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
3476 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3478 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3480 return data
->write_done_cb
? data
->write_done_cb(task
, data
) :
3481 nfs4_write_done_cb(task
, data
);
3485 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data
*data
)
3487 const struct nfs_pgio_header
*hdr
= data
->header
;
3489 /* Don't request attributes for pNFS or O_DIRECT writes */
3490 if (data
->ds_clp
!= NULL
|| hdr
->dreq
!= NULL
)
3492 /* Otherwise, request attributes if and only if we don't hold
3495 return nfs4_have_delegation(hdr
->inode
, FMODE_READ
) == 0;
3498 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3500 struct nfs_server
*server
= NFS_SERVER(data
->header
->inode
);
3502 if (!nfs4_write_need_cache_consistency_data(data
)) {
3503 data
->args
.bitmask
= NULL
;
3504 data
->res
.fattr
= NULL
;
3506 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3508 if (!data
->write_done_cb
)
3509 data
->write_done_cb
= nfs4_write_done_cb
;
3510 data
->res
.server
= server
;
3511 data
->timestamp
= jiffies
;
3513 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3514 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
3517 static void nfs4_proc_write_rpc_prepare(struct rpc_task
*task
, struct nfs_write_data
*data
)
3519 nfs4_setup_sequence(NFS_SERVER(data
->header
->inode
),
3520 &data
->args
.seq_args
,
3525 static void nfs4_proc_commit_rpc_prepare(struct rpc_task
*task
, struct nfs_commit_data
*data
)
3527 nfs4_setup_sequence(NFS_SERVER(data
->inode
),
3528 &data
->args
.seq_args
,
3533 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_commit_data
*data
)
3535 struct inode
*inode
= data
->inode
;
3537 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3538 rpc_restart_call_prepare(task
);
3544 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_commit_data
*data
)
3546 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3548 return data
->commit_done_cb(task
, data
);
3551 static void nfs4_proc_commit_setup(struct nfs_commit_data
*data
, struct rpc_message
*msg
)
3553 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3555 if (data
->commit_done_cb
== NULL
)
3556 data
->commit_done_cb
= nfs4_commit_done_cb
;
3557 data
->res
.server
= server
;
3558 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3559 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
3562 struct nfs4_renewdata
{
3563 struct nfs_client
*client
;
3564 unsigned long timestamp
;
3568 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3569 * standalone procedure for queueing an asynchronous RENEW.
3571 static void nfs4_renew_release(void *calldata
)
3573 struct nfs4_renewdata
*data
= calldata
;
3574 struct nfs_client
*clp
= data
->client
;
3576 if (atomic_read(&clp
->cl_count
) > 1)
3577 nfs4_schedule_state_renewal(clp
);
3578 nfs_put_client(clp
);
3582 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
3584 struct nfs4_renewdata
*data
= calldata
;
3585 struct nfs_client
*clp
= data
->client
;
3586 unsigned long timestamp
= data
->timestamp
;
3588 if (task
->tk_status
< 0) {
3589 /* Unless we're shutting down, schedule state recovery! */
3590 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) == 0)
3592 if (task
->tk_status
!= NFS4ERR_CB_PATH_DOWN
) {
3593 nfs4_schedule_lease_recovery(clp
);
3596 nfs4_schedule_path_down_recovery(clp
);
3598 do_renew_lease(clp
, timestamp
);
3601 static const struct rpc_call_ops nfs4_renew_ops
= {
3602 .rpc_call_done
= nfs4_renew_done
,
3603 .rpc_release
= nfs4_renew_release
,
3606 static int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
3608 struct rpc_message msg
= {
3609 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3613 struct nfs4_renewdata
*data
;
3615 if (renew_flags
== 0)
3617 if (!atomic_inc_not_zero(&clp
->cl_count
))
3619 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
3623 data
->timestamp
= jiffies
;
3624 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3625 &nfs4_renew_ops
, data
);
3628 static int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3630 struct rpc_message msg
= {
3631 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3635 unsigned long now
= jiffies
;
3638 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3641 do_renew_lease(clp
, now
);
3645 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3647 return (server
->caps
& NFS_CAP_ACLS
)
3648 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3649 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3652 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3653 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3656 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3658 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
3659 struct page
**pages
, unsigned int *pgbase
)
3661 struct page
*newpage
, **spages
;
3667 len
= min_t(size_t, PAGE_SIZE
, buflen
);
3668 newpage
= alloc_page(GFP_KERNEL
);
3670 if (newpage
== NULL
)
3672 memcpy(page_address(newpage
), buf
, len
);
3677 } while (buflen
!= 0);
3683 __free_page(spages
[rc
-1]);
3687 struct nfs4_cached_acl
{
3693 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3695 struct nfs_inode
*nfsi
= NFS_I(inode
);
3697 spin_lock(&inode
->i_lock
);
3698 kfree(nfsi
->nfs4_acl
);
3699 nfsi
->nfs4_acl
= acl
;
3700 spin_unlock(&inode
->i_lock
);
3703 static void nfs4_zap_acl_attr(struct inode
*inode
)
3705 nfs4_set_cached_acl(inode
, NULL
);
3708 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3710 struct nfs_inode
*nfsi
= NFS_I(inode
);
3711 struct nfs4_cached_acl
*acl
;
3714 spin_lock(&inode
->i_lock
);
3715 acl
= nfsi
->nfs4_acl
;
3718 if (buf
== NULL
) /* user is just asking for length */
3720 if (acl
->cached
== 0)
3722 ret
= -ERANGE
; /* see getxattr(2) man page */
3723 if (acl
->len
> buflen
)
3725 memcpy(buf
, acl
->data
, acl
->len
);
3729 spin_unlock(&inode
->i_lock
);
3733 static void nfs4_write_cached_acl(struct inode
*inode
, struct page
**pages
, size_t pgbase
, size_t acl_len
)
3735 struct nfs4_cached_acl
*acl
;
3736 size_t buflen
= sizeof(*acl
) + acl_len
;
3738 if (buflen
<= PAGE_SIZE
) {
3739 acl
= kmalloc(buflen
, GFP_KERNEL
);
3743 _copy_from_pages(acl
->data
, pages
, pgbase
, acl_len
);
3745 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3752 nfs4_set_cached_acl(inode
, acl
);
3756 * The getxattr API returns the required buffer length when called with a
3757 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3758 * the required buf. On a NULL buf, we send a page of data to the server
3759 * guessing that the ACL request can be serviced by a page. If so, we cache
3760 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3761 * the cache. If not so, we throw away the page, and cache the required
3762 * length. The next getxattr call will then produce another round trip to
3763 * the server, this time with the input buf of the required size.
3765 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3767 struct page
*pages
[NFS4ACL_MAXPAGES
] = {NULL
, };
3768 struct nfs_getaclargs args
= {
3769 .fh
= NFS_FH(inode
),
3773 struct nfs_getaclres res
= {
3776 struct rpc_message msg
= {
3777 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3781 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
3782 int ret
= -ENOMEM
, i
;
3784 /* As long as we're doing a round trip to the server anyway,
3785 * let's be prepared for a page of acl data. */
3788 if (npages
> ARRAY_SIZE(pages
))
3791 for (i
= 0; i
< npages
; i
++) {
3792 pages
[i
] = alloc_page(GFP_KERNEL
);
3797 /* for decoding across pages */
3798 res
.acl_scratch
= alloc_page(GFP_KERNEL
);
3799 if (!res
.acl_scratch
)
3802 args
.acl_len
= npages
* PAGE_SIZE
;
3803 args
.acl_pgbase
= 0;
3805 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3806 __func__
, buf
, buflen
, npages
, args
.acl_len
);
3807 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
),
3808 &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3812 /* Handle the case where the passed-in buffer is too short */
3813 if (res
.acl_flags
& NFS4_ACL_TRUNC
) {
3814 /* Did the user only issue a request for the acl length? */
3820 nfs4_write_cached_acl(inode
, pages
, res
.acl_data_offset
, res
.acl_len
);
3822 if (res
.acl_len
> buflen
) {
3826 _copy_from_pages(buf
, pages
, res
.acl_data_offset
, res
.acl_len
);
3831 for (i
= 0; i
< npages
; i
++)
3833 __free_page(pages
[i
]);
3834 if (res
.acl_scratch
)
3835 __free_page(res
.acl_scratch
);
3839 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3841 struct nfs4_exception exception
= { };
3844 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3847 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3848 } while (exception
.retry
);
3852 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3854 struct nfs_server
*server
= NFS_SERVER(inode
);
3857 if (!nfs4_server_supports_acls(server
))
3859 ret
= nfs_revalidate_inode(server
, inode
);
3862 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
3863 nfs_zap_acl_cache(inode
);
3864 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3866 /* -ENOENT is returned if there is no ACL or if there is an ACL
3867 * but no cached acl data, just the acl length */
3869 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3872 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3874 struct nfs_server
*server
= NFS_SERVER(inode
);
3875 struct page
*pages
[NFS4ACL_MAXPAGES
];
3876 struct nfs_setaclargs arg
= {
3877 .fh
= NFS_FH(inode
),
3881 struct nfs_setaclres res
;
3882 struct rpc_message msg
= {
3883 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3887 unsigned int npages
= DIV_ROUND_UP(buflen
, PAGE_SIZE
);
3890 if (!nfs4_server_supports_acls(server
))
3892 if (npages
> ARRAY_SIZE(pages
))
3894 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3897 nfs4_inode_return_delegation(inode
);
3898 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3901 * Free each page after tx, so the only ref left is
3902 * held by the network stack
3905 put_page(pages
[i
-1]);
3908 * Acl update can result in inode attribute update.
3909 * so mark the attribute cache invalid.
3911 spin_lock(&inode
->i_lock
);
3912 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
3913 spin_unlock(&inode
->i_lock
);
3914 nfs_access_zap_cache(inode
);
3915 nfs_zap_acl_cache(inode
);
3919 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3921 struct nfs4_exception exception
= { };
3924 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3925 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3927 } while (exception
.retry
);
3932 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3934 struct nfs_client
*clp
= server
->nfs_client
;
3936 if (task
->tk_status
>= 0)
3938 switch(task
->tk_status
) {
3939 case -NFS4ERR_DELEG_REVOKED
:
3940 case -NFS4ERR_ADMIN_REVOKED
:
3941 case -NFS4ERR_BAD_STATEID
:
3944 nfs_remove_bad_delegation(state
->inode
);
3945 case -NFS4ERR_OPENMODE
:
3948 nfs4_schedule_stateid_recovery(server
, state
);
3949 goto wait_on_recovery
;
3950 case -NFS4ERR_EXPIRED
:
3952 nfs4_schedule_stateid_recovery(server
, state
);
3953 case -NFS4ERR_STALE_STATEID
:
3954 case -NFS4ERR_STALE_CLIENTID
:
3955 nfs4_schedule_lease_recovery(clp
);
3956 goto wait_on_recovery
;
3957 #if defined(CONFIG_NFS_V4_1)
3958 case -NFS4ERR_BADSESSION
:
3959 case -NFS4ERR_BADSLOT
:
3960 case -NFS4ERR_BAD_HIGH_SLOT
:
3961 case -NFS4ERR_DEADSESSION
:
3962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3963 case -NFS4ERR_SEQ_FALSE_RETRY
:
3964 case -NFS4ERR_SEQ_MISORDERED
:
3965 dprintk("%s ERROR %d, Reset session\n", __func__
,
3967 nfs4_schedule_session_recovery(clp
->cl_session
, task
->tk_status
);
3968 task
->tk_status
= 0;
3970 #endif /* CONFIG_NFS_V4_1 */
3971 case -NFS4ERR_DELAY
:
3972 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3973 case -NFS4ERR_GRACE
:
3974 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3975 task
->tk_status
= 0;
3977 case -NFS4ERR_RETRY_UNCACHED_REP
:
3978 case -NFS4ERR_OLD_STATEID
:
3979 task
->tk_status
= 0;
3982 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3985 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3986 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3987 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3988 task
->tk_status
= 0;
3992 static void nfs4_init_boot_verifier(const struct nfs_client
*clp
,
3993 nfs4_verifier
*bootverf
)
3997 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
3998 /* An impossible timestamp guarantees this value
3999 * will never match a generated boot time. */
4001 verf
[1] = (__be32
)(NSEC_PER_SEC
+ 1);
4003 struct nfs_net
*nn
= net_generic(clp
->cl_net
, nfs_net_id
);
4004 verf
[0] = (__be32
)nn
->boot_time
.tv_sec
;
4005 verf
[1] = (__be32
)nn
->boot_time
.tv_nsec
;
4007 memcpy(bootverf
->data
, verf
, sizeof(bootverf
->data
));
4011 nfs4_init_nonuniform_client_string(const struct nfs_client
*clp
,
4012 char *buf
, size_t len
)
4014 unsigned int result
;
4017 result
= scnprintf(buf
, len
, "Linux NFSv4.0 %s/%s %s",
4019 rpc_peeraddr2str(clp
->cl_rpcclient
,
4021 rpc_peeraddr2str(clp
->cl_rpcclient
,
4022 RPC_DISPLAY_PROTO
));
4028 nfs4_init_uniform_client_string(const struct nfs_client
*clp
,
4029 char *buf
, size_t len
)
4031 char *nodename
= clp
->cl_rpcclient
->cl_nodename
;
4033 if (nfs4_client_id_uniquifier
[0] != '\0')
4034 nodename
= nfs4_client_id_uniquifier
;
4035 return scnprintf(buf
, len
, "Linux NFSv%u.%u %s",
4036 clp
->rpc_ops
->version
, clp
->cl_minorversion
,
4041 * nfs4_proc_setclientid - Negotiate client ID
4042 * @clp: state data structure
4043 * @program: RPC program for NFSv4 callback service
4044 * @port: IP port number for NFS4 callback service
4045 * @cred: RPC credential to use for this call
4046 * @res: where to place the result
4048 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4050 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
4051 unsigned short port
, struct rpc_cred
*cred
,
4052 struct nfs4_setclientid_res
*res
)
4054 nfs4_verifier sc_verifier
;
4055 struct nfs4_setclientid setclientid
= {
4056 .sc_verifier
= &sc_verifier
,
4058 .sc_cb_ident
= clp
->cl_cb_ident
,
4060 struct rpc_message msg
= {
4061 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
4062 .rpc_argp
= &setclientid
,
4068 /* nfs_client_id4 */
4069 nfs4_init_boot_verifier(clp
, &sc_verifier
);
4070 if (test_bit(NFS_CS_MIGRATION
, &clp
->cl_flags
))
4071 setclientid
.sc_name_len
=
4072 nfs4_init_uniform_client_string(clp
,
4073 setclientid
.sc_name
,
4074 sizeof(setclientid
.sc_name
));
4076 setclientid
.sc_name_len
=
4077 nfs4_init_nonuniform_client_string(clp
,
4078 setclientid
.sc_name
,
4079 sizeof(setclientid
.sc_name
));
4082 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
4083 sizeof(setclientid
.sc_netid
),
4084 rpc_peeraddr2str(clp
->cl_rpcclient
,
4085 RPC_DISPLAY_NETID
));
4087 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
4088 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
4089 clp
->cl_ipaddr
, port
>> 8, port
& 255);
4091 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4092 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4093 setclientid
.sc_name_len
, setclientid
.sc_name
);
4094 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4095 dprintk("NFS reply setclientid: %d\n", status
);
4100 * nfs4_proc_setclientid_confirm - Confirm client ID
4101 * @clp: state data structure
4102 * @res: result of a previous SETCLIENTID
4103 * @cred: RPC credential to use for this call
4105 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4107 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
4108 struct nfs4_setclientid_res
*arg
,
4109 struct rpc_cred
*cred
)
4111 struct nfs_fsinfo fsinfo
;
4112 struct rpc_message msg
= {
4113 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
4115 .rpc_resp
= &fsinfo
,
4121 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4122 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
4125 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4127 spin_lock(&clp
->cl_lock
);
4128 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
4129 clp
->cl_last_renewal
= now
;
4130 spin_unlock(&clp
->cl_lock
);
4132 dprintk("NFS reply setclientid_confirm: %d\n", status
);
4136 struct nfs4_delegreturndata
{
4137 struct nfs4_delegreturnargs args
;
4138 struct nfs4_delegreturnres res
;
4140 nfs4_stateid stateid
;
4141 unsigned long timestamp
;
4142 struct nfs_fattr fattr
;
4146 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
4148 struct nfs4_delegreturndata
*data
= calldata
;
4150 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4153 switch (task
->tk_status
) {
4154 case -NFS4ERR_STALE_STATEID
:
4155 case -NFS4ERR_EXPIRED
:
4157 renew_lease(data
->res
.server
, data
->timestamp
);
4160 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
4162 rpc_restart_call_prepare(task
);
4166 data
->rpc_status
= task
->tk_status
;
4169 static void nfs4_delegreturn_release(void *calldata
)
4174 #if defined(CONFIG_NFS_V4_1)
4175 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
4177 struct nfs4_delegreturndata
*d_data
;
4179 d_data
= (struct nfs4_delegreturndata
*)data
;
4181 nfs4_setup_sequence(d_data
->res
.server
,
4182 &d_data
->args
.seq_args
,
4183 &d_data
->res
.seq_res
,
4186 #endif /* CONFIG_NFS_V4_1 */
4188 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
4189 #if defined(CONFIG_NFS_V4_1)
4190 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
4191 #endif /* CONFIG_NFS_V4_1 */
4192 .rpc_call_done
= nfs4_delegreturn_done
,
4193 .rpc_release
= nfs4_delegreturn_release
,
4196 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
4198 struct nfs4_delegreturndata
*data
;
4199 struct nfs_server
*server
= NFS_SERVER(inode
);
4200 struct rpc_task
*task
;
4201 struct rpc_message msg
= {
4202 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
4205 struct rpc_task_setup task_setup_data
= {
4206 .rpc_client
= server
->client
,
4207 .rpc_message
= &msg
,
4208 .callback_ops
= &nfs4_delegreturn_ops
,
4209 .flags
= RPC_TASK_ASYNC
,
4213 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
4216 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
4217 data
->args
.fhandle
= &data
->fh
;
4218 data
->args
.stateid
= &data
->stateid
;
4219 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
4220 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
4221 nfs4_stateid_copy(&data
->stateid
, stateid
);
4222 data
->res
.fattr
= &data
->fattr
;
4223 data
->res
.server
= server
;
4224 nfs_fattr_init(data
->res
.fattr
);
4225 data
->timestamp
= jiffies
;
4226 data
->rpc_status
= 0;
4228 task_setup_data
.callback_data
= data
;
4229 msg
.rpc_argp
= &data
->args
;
4230 msg
.rpc_resp
= &data
->res
;
4231 task
= rpc_run_task(&task_setup_data
);
4233 return PTR_ERR(task
);
4236 status
= nfs4_wait_for_completion_rpc_task(task
);
4239 status
= data
->rpc_status
;
4241 nfs_post_op_update_inode_force_wcc(inode
, &data
->fattr
);
4243 nfs_refresh_inode(inode
, &data
->fattr
);
4249 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
4251 struct nfs_server
*server
= NFS_SERVER(inode
);
4252 struct nfs4_exception exception
= { };
4255 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
4257 case -NFS4ERR_STALE_STATEID
:
4258 case -NFS4ERR_EXPIRED
:
4262 err
= nfs4_handle_exception(server
, err
, &exception
);
4263 } while (exception
.retry
);
4267 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4268 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4271 * sleep, with exponential backoff, and retry the LOCK operation.
4273 static unsigned long
4274 nfs4_set_lock_task_retry(unsigned long timeout
)
4276 freezable_schedule_timeout_killable(timeout
);
4278 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
4279 return NFS4_LOCK_MAXTIMEOUT
;
4283 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4285 struct inode
*inode
= state
->inode
;
4286 struct nfs_server
*server
= NFS_SERVER(inode
);
4287 struct nfs_client
*clp
= server
->nfs_client
;
4288 struct nfs_lockt_args arg
= {
4289 .fh
= NFS_FH(inode
),
4292 struct nfs_lockt_res res
= {
4295 struct rpc_message msg
= {
4296 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
4299 .rpc_cred
= state
->owner
->so_cred
,
4301 struct nfs4_lock_state
*lsp
;
4304 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
4305 status
= nfs4_set_lock_state(state
, request
);
4308 lsp
= request
->fl_u
.nfs4_fl
.owner
;
4309 arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
4310 arg
.lock_owner
.s_dev
= server
->s_dev
;
4311 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4314 request
->fl_type
= F_UNLCK
;
4316 case -NFS4ERR_DENIED
:
4319 request
->fl_ops
->fl_release_private(request
);
4324 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4326 struct nfs4_exception exception
= { };
4330 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4331 _nfs4_proc_getlk(state
, cmd
, request
),
4333 } while (exception
.retry
);
4337 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
4340 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
4342 res
= posix_lock_file_wait(file
, fl
);
4345 res
= flock_lock_file_wait(file
, fl
);
4353 struct nfs4_unlockdata
{
4354 struct nfs_locku_args arg
;
4355 struct nfs_locku_res res
;
4356 struct nfs4_lock_state
*lsp
;
4357 struct nfs_open_context
*ctx
;
4358 struct file_lock fl
;
4359 const struct nfs_server
*server
;
4360 unsigned long timestamp
;
4363 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
4364 struct nfs_open_context
*ctx
,
4365 struct nfs4_lock_state
*lsp
,
4366 struct nfs_seqid
*seqid
)
4368 struct nfs4_unlockdata
*p
;
4369 struct inode
*inode
= lsp
->ls_state
->inode
;
4371 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
4374 p
->arg
.fh
= NFS_FH(inode
);
4376 p
->arg
.seqid
= seqid
;
4377 p
->res
.seqid
= seqid
;
4378 p
->arg
.stateid
= &lsp
->ls_stateid
;
4380 atomic_inc(&lsp
->ls_count
);
4381 /* Ensure we don't close file until we're done freeing locks! */
4382 p
->ctx
= get_nfs_open_context(ctx
);
4383 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4384 p
->server
= NFS_SERVER(inode
);
4388 static void nfs4_locku_release_calldata(void *data
)
4390 struct nfs4_unlockdata
*calldata
= data
;
4391 nfs_free_seqid(calldata
->arg
.seqid
);
4392 nfs4_put_lock_state(calldata
->lsp
);
4393 put_nfs_open_context(calldata
->ctx
);
4397 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
4399 struct nfs4_unlockdata
*calldata
= data
;
4401 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
4403 switch (task
->tk_status
) {
4405 nfs4_stateid_copy(&calldata
->lsp
->ls_stateid
,
4406 &calldata
->res
.stateid
);
4407 renew_lease(calldata
->server
, calldata
->timestamp
);
4409 case -NFS4ERR_BAD_STATEID
:
4410 case -NFS4ERR_OLD_STATEID
:
4411 case -NFS4ERR_STALE_STATEID
:
4412 case -NFS4ERR_EXPIRED
:
4415 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
4416 rpc_restart_call_prepare(task
);
4418 nfs_release_seqid(calldata
->arg
.seqid
);
4421 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
4423 struct nfs4_unlockdata
*calldata
= data
;
4425 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
4427 if (test_bit(NFS_LOCK_INITIALIZED
, &calldata
->lsp
->ls_flags
) == 0) {
4428 /* Note: exit _without_ running nfs4_locku_done */
4429 task
->tk_action
= NULL
;
4430 nfs4_sequence_done(task
, &calldata
->res
.seq_res
);
4433 calldata
->timestamp
= jiffies
;
4434 if (nfs4_setup_sequence(calldata
->server
,
4435 &calldata
->arg
.seq_args
,
4436 &calldata
->res
.seq_res
,
4438 nfs_release_seqid(calldata
->arg
.seqid
);
4441 static const struct rpc_call_ops nfs4_locku_ops
= {
4442 .rpc_call_prepare
= nfs4_locku_prepare
,
4443 .rpc_call_done
= nfs4_locku_done
,
4444 .rpc_release
= nfs4_locku_release_calldata
,
4447 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
4448 struct nfs_open_context
*ctx
,
4449 struct nfs4_lock_state
*lsp
,
4450 struct nfs_seqid
*seqid
)
4452 struct nfs4_unlockdata
*data
;
4453 struct rpc_message msg
= {
4454 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
4455 .rpc_cred
= ctx
->cred
,
4457 struct rpc_task_setup task_setup_data
= {
4458 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
4459 .rpc_message
= &msg
,
4460 .callback_ops
= &nfs4_locku_ops
,
4461 .workqueue
= nfsiod_workqueue
,
4462 .flags
= RPC_TASK_ASYNC
,
4465 /* Ensure this is an unlock - when canceling a lock, the
4466 * canceled lock is passed in, and it won't be an unlock.
4468 fl
->fl_type
= F_UNLCK
;
4470 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
4472 nfs_free_seqid(seqid
);
4473 return ERR_PTR(-ENOMEM
);
4476 nfs41_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4477 msg
.rpc_argp
= &data
->arg
;
4478 msg
.rpc_resp
= &data
->res
;
4479 task_setup_data
.callback_data
= data
;
4480 return rpc_run_task(&task_setup_data
);
4483 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4485 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4486 struct nfs_seqid
*seqid
;
4487 struct nfs4_lock_state
*lsp
;
4488 struct rpc_task
*task
;
4490 unsigned char fl_flags
= request
->fl_flags
;
4492 status
= nfs4_set_lock_state(state
, request
);
4493 /* Unlock _before_ we do the RPC call */
4494 request
->fl_flags
|= FL_EXISTS
;
4495 down_read(&nfsi
->rwsem
);
4496 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
4497 up_read(&nfsi
->rwsem
);
4500 up_read(&nfsi
->rwsem
);
4503 /* Is this a delegated lock? */
4504 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
4506 lsp
= request
->fl_u
.nfs4_fl
.owner
;
4507 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
4511 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
4512 status
= PTR_ERR(task
);
4515 status
= nfs4_wait_for_completion_rpc_task(task
);
4518 request
->fl_flags
= fl_flags
;
4522 struct nfs4_lockdata
{
4523 struct nfs_lock_args arg
;
4524 struct nfs_lock_res res
;
4525 struct nfs4_lock_state
*lsp
;
4526 struct nfs_open_context
*ctx
;
4527 struct file_lock fl
;
4528 unsigned long timestamp
;
4531 struct nfs_server
*server
;
4534 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
4535 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
4538 struct nfs4_lockdata
*p
;
4539 struct inode
*inode
= lsp
->ls_state
->inode
;
4540 struct nfs_server
*server
= NFS_SERVER(inode
);
4542 p
= kzalloc(sizeof(*p
), gfp_mask
);
4546 p
->arg
.fh
= NFS_FH(inode
);
4548 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
4549 if (p
->arg
.open_seqid
== NULL
)
4551 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
4552 if (p
->arg
.lock_seqid
== NULL
)
4553 goto out_free_seqid
;
4554 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
4555 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4556 p
->arg
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
4557 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
4558 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
4561 atomic_inc(&lsp
->ls_count
);
4562 p
->ctx
= get_nfs_open_context(ctx
);
4563 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4566 nfs_free_seqid(p
->arg
.open_seqid
);
4572 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
4574 struct nfs4_lockdata
*data
= calldata
;
4575 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4577 dprintk("%s: begin!\n", __func__
);
4578 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4580 /* Do we need to do an open_to_lock_owner? */
4581 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4582 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0) {
4583 goto out_release_lock_seqid
;
4585 data
->arg
.open_stateid
= &state
->stateid
;
4586 data
->arg
.new_lock_owner
= 1;
4587 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4589 data
->arg
.new_lock_owner
= 0;
4590 data
->timestamp
= jiffies
;
4591 if (nfs4_setup_sequence(data
->server
,
4592 &data
->arg
.seq_args
,
4596 nfs_release_seqid(data
->arg
.open_seqid
);
4597 out_release_lock_seqid
:
4598 nfs_release_seqid(data
->arg
.lock_seqid
);
4599 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4602 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4604 struct nfs4_lockdata
*data
= calldata
;
4606 dprintk("%s: begin!\n", __func__
);
4608 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4611 data
->rpc_status
= task
->tk_status
;
4612 if (data
->arg
.new_lock_owner
!= 0) {
4613 if (data
->rpc_status
== 0)
4614 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4618 if (data
->rpc_status
== 0) {
4619 nfs4_stateid_copy(&data
->lsp
->ls_stateid
, &data
->res
.stateid
);
4620 set_bit(NFS_LOCK_INITIALIZED
, &data
->lsp
->ls_flags
);
4621 renew_lease(NFS_SERVER(data
->ctx
->dentry
->d_inode
), data
->timestamp
);
4624 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4627 static void nfs4_lock_release(void *calldata
)
4629 struct nfs4_lockdata
*data
= calldata
;
4631 dprintk("%s: begin!\n", __func__
);
4632 nfs_free_seqid(data
->arg
.open_seqid
);
4633 if (data
->cancelled
!= 0) {
4634 struct rpc_task
*task
;
4635 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4636 data
->arg
.lock_seqid
);
4638 rpc_put_task_async(task
);
4639 dprintk("%s: cancelling lock!\n", __func__
);
4641 nfs_free_seqid(data
->arg
.lock_seqid
);
4642 nfs4_put_lock_state(data
->lsp
);
4643 put_nfs_open_context(data
->ctx
);
4645 dprintk("%s: done!\n", __func__
);
4648 static const struct rpc_call_ops nfs4_lock_ops
= {
4649 .rpc_call_prepare
= nfs4_lock_prepare
,
4650 .rpc_call_done
= nfs4_lock_done
,
4651 .rpc_release
= nfs4_lock_release
,
4654 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4657 case -NFS4ERR_ADMIN_REVOKED
:
4658 case -NFS4ERR_BAD_STATEID
:
4659 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4660 if (new_lock_owner
!= 0 ||
4661 test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
) != 0)
4662 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
4664 case -NFS4ERR_STALE_STATEID
:
4665 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4666 case -NFS4ERR_EXPIRED
:
4667 nfs4_schedule_lease_recovery(server
->nfs_client
);
4671 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4673 struct nfs4_lockdata
*data
;
4674 struct rpc_task
*task
;
4675 struct rpc_message msg
= {
4676 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4677 .rpc_cred
= state
->owner
->so_cred
,
4679 struct rpc_task_setup task_setup_data
= {
4680 .rpc_client
= NFS_CLIENT(state
->inode
),
4681 .rpc_message
= &msg
,
4682 .callback_ops
= &nfs4_lock_ops
,
4683 .workqueue
= nfsiod_workqueue
,
4684 .flags
= RPC_TASK_ASYNC
,
4688 dprintk("%s: begin!\n", __func__
);
4689 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4690 fl
->fl_u
.nfs4_fl
.owner
,
4691 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
4695 data
->arg
.block
= 1;
4696 nfs41_init_sequence(&data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
4697 msg
.rpc_argp
= &data
->arg
;
4698 msg
.rpc_resp
= &data
->res
;
4699 task_setup_data
.callback_data
= data
;
4700 if (recovery_type
> NFS_LOCK_NEW
) {
4701 if (recovery_type
== NFS_LOCK_RECLAIM
)
4702 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4703 nfs4_set_sequence_privileged(&data
->arg
.seq_args
);
4705 task
= rpc_run_task(&task_setup_data
);
4707 return PTR_ERR(task
);
4708 ret
= nfs4_wait_for_completion_rpc_task(task
);
4710 ret
= data
->rpc_status
;
4712 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4713 data
->arg
.new_lock_owner
, ret
);
4715 data
->cancelled
= 1;
4717 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4721 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4723 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4724 struct nfs4_exception exception
= {
4725 .inode
= state
->inode
,
4730 /* Cache the lock if possible... */
4731 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4733 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4734 if (err
!= -NFS4ERR_DELAY
)
4736 nfs4_handle_exception(server
, err
, &exception
);
4737 } while (exception
.retry
);
4741 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4743 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4744 struct nfs4_exception exception
= {
4745 .inode
= state
->inode
,
4749 err
= nfs4_set_lock_state(state
, request
);
4753 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4755 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4759 case -NFS4ERR_GRACE
:
4760 case -NFS4ERR_DELAY
:
4761 nfs4_handle_exception(server
, err
, &exception
);
4764 } while (exception
.retry
);
4769 #if defined(CONFIG_NFS_V4_1)
4771 * nfs41_check_expired_locks - possibly free a lock stateid
4773 * @state: NFSv4 state for an inode
4775 * Returns NFS_OK if recovery for this stateid is now finished.
4776 * Otherwise a negative NFS4ERR value is returned.
4778 static int nfs41_check_expired_locks(struct nfs4_state
*state
)
4780 int status
, ret
= -NFS4ERR_BAD_STATEID
;
4781 struct nfs4_lock_state
*lsp
;
4782 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4784 list_for_each_entry(lsp
, &state
->lock_states
, ls_locks
) {
4785 if (test_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
)) {
4786 status
= nfs41_test_stateid(server
, &lsp
->ls_stateid
);
4787 if (status
!= NFS_OK
) {
4788 /* Free the stateid unless the server
4789 * informs us the stateid is unrecognized. */
4790 if (status
!= -NFS4ERR_BAD_STATEID
)
4791 nfs41_free_stateid(server
,
4793 clear_bit(NFS_LOCK_INITIALIZED
, &lsp
->ls_flags
);
4802 static int nfs41_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4804 int status
= NFS_OK
;
4806 if (test_bit(LK_STATE_IN_USE
, &state
->flags
))
4807 status
= nfs41_check_expired_locks(state
);
4808 if (status
!= NFS_OK
)
4809 status
= nfs4_lock_expired(state
, request
);
4814 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4816 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4817 unsigned char fl_flags
= request
->fl_flags
;
4818 int status
= -ENOLCK
;
4820 if ((fl_flags
& FL_POSIX
) &&
4821 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4823 /* Is this a delegated open? */
4824 status
= nfs4_set_lock_state(state
, request
);
4827 request
->fl_flags
|= FL_ACCESS
;
4828 status
= do_vfs_lock(request
->fl_file
, request
);
4831 down_read(&nfsi
->rwsem
);
4832 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4833 /* Yes: cache locks! */
4834 /* ...but avoid races with delegation recall... */
4835 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4836 status
= do_vfs_lock(request
->fl_file
, request
);
4839 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4842 /* Note: we always want to sleep here! */
4843 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4844 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4845 printk(KERN_WARNING
"NFS: %s: VFS is out of sync with lock "
4846 "manager!\n", __func__
);
4848 up_read(&nfsi
->rwsem
);
4850 request
->fl_flags
= fl_flags
;
4854 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4856 struct nfs4_exception exception
= {
4858 .inode
= state
->inode
,
4863 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4864 if (err
== -NFS4ERR_DENIED
)
4866 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4868 } while (exception
.retry
);
4873 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4875 struct nfs_open_context
*ctx
;
4876 struct nfs4_state
*state
;
4877 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4880 /* verify open state */
4881 ctx
= nfs_file_open_context(filp
);
4884 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4887 if (IS_GETLK(cmd
)) {
4889 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4893 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4896 if (request
->fl_type
== F_UNLCK
) {
4898 return nfs4_proc_unlck(state
, cmd
, request
);
4905 * Don't rely on the VFS having checked the file open mode,
4906 * since it won't do this for flock() locks.
4908 switch (request
->fl_type
) {
4910 if (!(filp
->f_mode
& FMODE_READ
))
4914 if (!(filp
->f_mode
& FMODE_WRITE
))
4919 status
= nfs4_proc_setlk(state
, cmd
, request
);
4920 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4922 timeout
= nfs4_set_lock_task_retry(timeout
);
4923 status
= -ERESTARTSYS
;
4926 } while(status
< 0);
4930 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4932 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4933 struct nfs4_exception exception
= { };
4936 err
= nfs4_set_lock_state(state
, fl
);
4940 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4943 printk(KERN_ERR
"NFS: %s: unhandled error "
4944 "%d.\n", __func__
, err
);
4948 case -NFS4ERR_EXPIRED
:
4949 nfs4_schedule_stateid_recovery(server
, state
);
4950 case -NFS4ERR_STALE_CLIENTID
:
4951 case -NFS4ERR_STALE_STATEID
:
4952 nfs4_schedule_lease_recovery(server
->nfs_client
);
4954 case -NFS4ERR_BADSESSION
:
4955 case -NFS4ERR_BADSLOT
:
4956 case -NFS4ERR_BAD_HIGH_SLOT
:
4957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4958 case -NFS4ERR_DEADSESSION
:
4959 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
, err
);
4963 * The show must go on: exit, but mark the
4964 * stateid as needing recovery.
4966 case -NFS4ERR_DELEG_REVOKED
:
4967 case -NFS4ERR_ADMIN_REVOKED
:
4968 case -NFS4ERR_BAD_STATEID
:
4969 case -NFS4ERR_OPENMODE
:
4970 nfs4_schedule_stateid_recovery(server
, state
);
4974 case -NFS4ERR_DENIED
:
4975 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4978 case -NFS4ERR_DELAY
:
4981 err
= nfs4_handle_exception(server
, err
, &exception
);
4982 } while (exception
.retry
);
4987 struct nfs_release_lockowner_data
{
4988 struct nfs4_lock_state
*lsp
;
4989 struct nfs_server
*server
;
4990 struct nfs_release_lockowner_args args
;
4993 static void nfs4_release_lockowner_release(void *calldata
)
4995 struct nfs_release_lockowner_data
*data
= calldata
;
4996 nfs4_free_lock_state(data
->server
, data
->lsp
);
5000 static const struct rpc_call_ops nfs4_release_lockowner_ops
= {
5001 .rpc_release
= nfs4_release_lockowner_release
,
5004 int nfs4_release_lockowner(struct nfs4_lock_state
*lsp
)
5006 struct nfs_server
*server
= lsp
->ls_state
->owner
->so_server
;
5007 struct nfs_release_lockowner_data
*data
;
5008 struct rpc_message msg
= {
5009 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
5012 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
5014 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
5018 data
->server
= server
;
5019 data
->args
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
5020 data
->args
.lock_owner
.id
= lsp
->ls_seqid
.owner_id
;
5021 data
->args
.lock_owner
.s_dev
= server
->s_dev
;
5022 msg
.rpc_argp
= &data
->args
;
5023 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, data
);
5027 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5029 static int nfs4_xattr_set_nfs4_acl(struct dentry
*dentry
, const char *key
,
5030 const void *buf
, size_t buflen
,
5031 int flags
, int type
)
5033 if (strcmp(key
, "") != 0)
5036 return nfs4_proc_set_acl(dentry
->d_inode
, buf
, buflen
);
5039 static int nfs4_xattr_get_nfs4_acl(struct dentry
*dentry
, const char *key
,
5040 void *buf
, size_t buflen
, int type
)
5042 if (strcmp(key
, "") != 0)
5045 return nfs4_proc_get_acl(dentry
->d_inode
, buf
, buflen
);
5048 static size_t nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
, char *list
,
5049 size_t list_len
, const char *name
,
5050 size_t name_len
, int type
)
5052 size_t len
= sizeof(XATTR_NAME_NFSV4_ACL
);
5054 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
5057 if (list
&& len
<= list_len
)
5058 memcpy(list
, XATTR_NAME_NFSV4_ACL
, len
);
5063 * nfs_fhget will use either the mounted_on_fileid or the fileid
5065 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
5067 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
5068 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
5069 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
5070 (fattr
->valid
& NFS_ATTR_FATTR_V4_LOCATIONS
)))
5073 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
5074 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_V4_REFERRAL
;
5075 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
5079 static int _nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5080 const struct qstr
*name
,
5081 struct nfs4_fs_locations
*fs_locations
,
5084 struct nfs_server
*server
= NFS_SERVER(dir
);
5086 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
5088 struct nfs4_fs_locations_arg args
= {
5089 .dir_fh
= NFS_FH(dir
),
5094 struct nfs4_fs_locations_res res
= {
5095 .fs_locations
= fs_locations
,
5097 struct rpc_message msg
= {
5098 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
5104 dprintk("%s: start\n", __func__
);
5106 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5107 * is not supported */
5108 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
5109 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
5111 bitmask
[0] |= FATTR4_WORD0_FILEID
;
5113 nfs_fattr_init(&fs_locations
->fattr
);
5114 fs_locations
->server
= server
;
5115 fs_locations
->nlocations
= 0;
5116 status
= nfs4_call_sync(client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5117 dprintk("%s: returned status = %d\n", __func__
, status
);
5121 int nfs4_proc_fs_locations(struct rpc_clnt
*client
, struct inode
*dir
,
5122 const struct qstr
*name
,
5123 struct nfs4_fs_locations
*fs_locations
,
5126 struct nfs4_exception exception
= { };
5129 err
= nfs4_handle_exception(NFS_SERVER(dir
),
5130 _nfs4_proc_fs_locations(client
, dir
, name
, fs_locations
, page
),
5132 } while (exception
.retry
);
5136 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
)
5139 struct nfs4_secinfo_arg args
= {
5140 .dir_fh
= NFS_FH(dir
),
5143 struct nfs4_secinfo_res res
= {
5146 struct rpc_message msg
= {
5147 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
5152 dprintk("NFS call secinfo %s\n", name
->name
);
5153 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5154 dprintk("NFS reply secinfo: %d\n", status
);
5158 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
,
5159 struct nfs4_secinfo_flavors
*flavors
)
5161 struct nfs4_exception exception
= { };
5164 err
= nfs4_handle_exception(NFS_SERVER(dir
),
5165 _nfs4_proc_secinfo(dir
, name
, flavors
),
5167 } while (exception
.retry
);
5171 #ifdef CONFIG_NFS_V4_1
5173 * Check the exchange flags returned by the server for invalid flags, having
5174 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5177 static int nfs4_check_cl_exchange_flags(u32 flags
)
5179 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
5181 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
5182 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
5184 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
5188 return -NFS4ERR_INVAL
;
5192 nfs41_same_server_scope(struct nfs41_server_scope
*a
,
5193 struct nfs41_server_scope
*b
)
5195 if (a
->server_scope_sz
== b
->server_scope_sz
&&
5196 memcmp(a
->server_scope
, b
->server_scope
, a
->server_scope_sz
) == 0)
5203 * nfs4_proc_bind_conn_to_session()
5205 * The 4.1 client currently uses the same TCP connection for the
5206 * fore and backchannel.
5208 int nfs4_proc_bind_conn_to_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5211 struct nfs41_bind_conn_to_session_res res
;
5212 struct rpc_message msg
= {
5214 &nfs4_procedures
[NFSPROC4_CLNT_BIND_CONN_TO_SESSION
],
5220 dprintk("--> %s\n", __func__
);
5222 res
.session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
5223 if (unlikely(res
.session
== NULL
)) {
5228 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5230 if (memcmp(res
.session
->sess_id
.data
,
5231 clp
->cl_session
->sess_id
.data
, NFS4_MAX_SESSIONID_LEN
)) {
5232 dprintk("NFS: %s: Session ID mismatch\n", __func__
);
5236 if (res
.dir
!= NFS4_CDFS4_BOTH
) {
5237 dprintk("NFS: %s: Unexpected direction from server\n",
5242 if (res
.use_conn_in_rdma_mode
) {
5243 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5252 dprintk("<-- %s status= %d\n", __func__
, status
);
5257 * nfs4_proc_exchange_id()
5259 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5261 * Since the clientid has expired, all compounds using sessions
5262 * associated with the stale clientid will be returning
5263 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5264 * be in some phase of session reset.
5266 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5268 nfs4_verifier verifier
;
5269 struct nfs41_exchange_id_args args
= {
5270 .verifier
= &verifier
,
5272 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
,
5274 struct nfs41_exchange_id_res res
= {
5278 struct rpc_message msg
= {
5279 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
5285 nfs4_init_boot_verifier(clp
, &verifier
);
5286 args
.id_len
= nfs4_init_uniform_client_string(clp
, args
.id
,
5288 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5289 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
5290 args
.id_len
, args
.id
);
5292 res
.server_owner
= kzalloc(sizeof(struct nfs41_server_owner
),
5294 if (unlikely(res
.server_owner
== NULL
)) {
5299 res
.server_scope
= kzalloc(sizeof(struct nfs41_server_scope
),
5301 if (unlikely(res
.server_scope
== NULL
)) {
5303 goto out_server_owner
;
5306 res
.impl_id
= kzalloc(sizeof(struct nfs41_impl_id
), GFP_NOFS
);
5307 if (unlikely(res
.impl_id
== NULL
)) {
5309 goto out_server_scope
;
5312 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5314 status
= nfs4_check_cl_exchange_flags(res
.flags
);
5317 clp
->cl_clientid
= res
.clientid
;
5318 clp
->cl_exchange_flags
= (res
.flags
& ~EXCHGID4_FLAG_CONFIRMED_R
);
5319 if (!(res
.flags
& EXCHGID4_FLAG_CONFIRMED_R
))
5320 clp
->cl_seqid
= res
.seqid
;
5322 kfree(clp
->cl_serverowner
);
5323 clp
->cl_serverowner
= res
.server_owner
;
5324 res
.server_owner
= NULL
;
5326 /* use the most recent implementation id */
5327 kfree(clp
->cl_implid
);
5328 clp
->cl_implid
= res
.impl_id
;
5330 if (clp
->cl_serverscope
!= NULL
&&
5331 !nfs41_same_server_scope(clp
->cl_serverscope
,
5332 res
.server_scope
)) {
5333 dprintk("%s: server_scope mismatch detected\n",
5335 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
, &clp
->cl_state
);
5336 kfree(clp
->cl_serverscope
);
5337 clp
->cl_serverscope
= NULL
;
5340 if (clp
->cl_serverscope
== NULL
) {
5341 clp
->cl_serverscope
= res
.server_scope
;
5348 kfree(res
.server_owner
);
5350 kfree(res
.server_scope
);
5352 if (clp
->cl_implid
!= NULL
)
5353 dprintk("NFS reply exchange_id: Server Implementation ID: "
5354 "domain: %s, name: %s, date: %llu,%u\n",
5355 clp
->cl_implid
->domain
, clp
->cl_implid
->name
,
5356 clp
->cl_implid
->date
.seconds
,
5357 clp
->cl_implid
->date
.nseconds
);
5358 dprintk("NFS reply exchange_id: %d\n", status
);
5362 static int _nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
5363 struct rpc_cred
*cred
)
5365 struct rpc_message msg
= {
5366 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_CLIENTID
],
5372 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5374 dprintk("NFS: Got error %d from the server %s on "
5375 "DESTROY_CLIENTID.", status
, clp
->cl_hostname
);
5379 static int nfs4_proc_destroy_clientid(struct nfs_client
*clp
,
5380 struct rpc_cred
*cred
)
5385 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
5386 ret
= _nfs4_proc_destroy_clientid(clp
, cred
);
5388 case -NFS4ERR_DELAY
:
5389 case -NFS4ERR_CLIENTID_BUSY
:
5399 int nfs4_destroy_clientid(struct nfs_client
*clp
)
5401 struct rpc_cred
*cred
;
5404 if (clp
->cl_mvops
->minor_version
< 1)
5406 if (clp
->cl_exchange_flags
== 0)
5408 if (clp
->cl_preserve_clid
)
5410 cred
= nfs4_get_exchange_id_cred(clp
);
5411 ret
= nfs4_proc_destroy_clientid(clp
, cred
);
5416 case -NFS4ERR_STALE_CLIENTID
:
5417 clp
->cl_exchange_flags
= 0;
5423 struct nfs4_get_lease_time_data
{
5424 struct nfs4_get_lease_time_args
*args
;
5425 struct nfs4_get_lease_time_res
*res
;
5426 struct nfs_client
*clp
;
5429 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
5432 struct nfs4_get_lease_time_data
*data
=
5433 (struct nfs4_get_lease_time_data
*)calldata
;
5435 dprintk("--> %s\n", __func__
);
5436 /* just setup sequence, do not trigger session recovery
5437 since we're invoked within one */
5438 nfs41_setup_sequence(data
->clp
->cl_session
,
5439 &data
->args
->la_seq_args
,
5440 &data
->res
->lr_seq_res
,
5442 dprintk("<-- %s\n", __func__
);
5446 * Called from nfs4_state_manager thread for session setup, so don't recover
5447 * from sequence operation or clientid errors.
5449 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
5451 struct nfs4_get_lease_time_data
*data
=
5452 (struct nfs4_get_lease_time_data
*)calldata
;
5454 dprintk("--> %s\n", __func__
);
5455 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
5457 switch (task
->tk_status
) {
5458 case -NFS4ERR_DELAY
:
5459 case -NFS4ERR_GRACE
:
5460 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
5461 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
5462 task
->tk_status
= 0;
5464 case -NFS4ERR_RETRY_UNCACHED_REP
:
5465 rpc_restart_call_prepare(task
);
5468 dprintk("<-- %s\n", __func__
);
5471 static const struct rpc_call_ops nfs4_get_lease_time_ops
= {
5472 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
5473 .rpc_call_done
= nfs4_get_lease_time_done
,
5476 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
5478 struct rpc_task
*task
;
5479 struct nfs4_get_lease_time_args args
;
5480 struct nfs4_get_lease_time_res res
= {
5481 .lr_fsinfo
= fsinfo
,
5483 struct nfs4_get_lease_time_data data
= {
5488 struct rpc_message msg
= {
5489 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
5493 struct rpc_task_setup task_setup
= {
5494 .rpc_client
= clp
->cl_rpcclient
,
5495 .rpc_message
= &msg
,
5496 .callback_ops
= &nfs4_get_lease_time_ops
,
5497 .callback_data
= &data
,
5498 .flags
= RPC_TASK_TIMEOUT
,
5502 nfs41_init_sequence(&args
.la_seq_args
, &res
.lr_seq_res
, 0);
5503 nfs4_set_sequence_privileged(&args
.la_seq_args
);
5504 dprintk("--> %s\n", __func__
);
5505 task
= rpc_run_task(&task_setup
);
5508 status
= PTR_ERR(task
);
5510 status
= task
->tk_status
;
5513 dprintk("<-- %s return %d\n", __func__
, status
);
5519 * Initialize the values to be used by the client in CREATE_SESSION
5520 * If nfs4_init_session set the fore channel request and response sizes,
5523 * Set the back channel max_resp_sz_cached to zero to force the client to
5524 * always set csa_cachethis to FALSE because the current implementation
5525 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5527 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
5529 struct nfs4_session
*session
= args
->client
->cl_session
;
5530 unsigned int mxrqst_sz
= session
->fc_target_max_rqst_sz
,
5531 mxresp_sz
= session
->fc_target_max_resp_sz
;
5534 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
5536 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
5537 /* Fore channel attributes */
5538 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
5539 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
5540 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
5541 args
->fc_attrs
.max_reqs
= max_session_slots
;
5543 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5544 "max_ops=%u max_reqs=%u\n",
5546 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
5547 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
5549 /* Back channel attributes */
5550 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
5551 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
5552 args
->bc_attrs
.max_resp_sz_cached
= 0;
5553 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
5554 args
->bc_attrs
.max_reqs
= 1;
5556 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5557 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5559 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
5560 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
5561 args
->bc_attrs
.max_reqs
);
5564 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
5566 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
5567 struct nfs4_channel_attrs
*rcvd
= &session
->fc_attrs
;
5569 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
5572 * Our requested max_ops is the minimum we need; we're not
5573 * prepared to break up compounds into smaller pieces than that.
5574 * So, no point even trying to continue if the server won't
5577 if (rcvd
->max_ops
< sent
->max_ops
)
5579 if (rcvd
->max_reqs
== 0)
5581 if (rcvd
->max_reqs
> NFS4_MAX_SLOT_TABLE
)
5582 rcvd
->max_reqs
= NFS4_MAX_SLOT_TABLE
;
5586 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
5588 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
5589 struct nfs4_channel_attrs
*rcvd
= &session
->bc_attrs
;
5591 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
5593 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
5595 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
5597 /* These would render the backchannel useless: */
5598 if (rcvd
->max_ops
!= sent
->max_ops
)
5600 if (rcvd
->max_reqs
!= sent
->max_reqs
)
5605 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
5606 struct nfs4_session
*session
)
5610 ret
= nfs4_verify_fore_channel_attrs(args
, session
);
5613 return nfs4_verify_back_channel_attrs(args
, session
);
5616 static int _nfs4_proc_create_session(struct nfs_client
*clp
,
5617 struct rpc_cred
*cred
)
5619 struct nfs4_session
*session
= clp
->cl_session
;
5620 struct nfs41_create_session_args args
= {
5622 .cb_program
= NFS4_CALLBACK
,
5624 struct nfs41_create_session_res res
= {
5627 struct rpc_message msg
= {
5628 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
5635 nfs4_init_channel_attrs(&args
);
5636 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
5638 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5641 /* Verify the session's negotiated channel_attrs values */
5642 status
= nfs4_verify_channel_attrs(&args
, session
);
5643 /* Increment the clientid slot sequence id */
5651 * Issues a CREATE_SESSION operation to the server.
5652 * It is the responsibility of the caller to verify the session is
5653 * expired before calling this routine.
5655 int nfs4_proc_create_session(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5659 struct nfs4_session
*session
= clp
->cl_session
;
5661 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
5663 status
= _nfs4_proc_create_session(clp
, cred
);
5667 /* Init or reset the session slot tables */
5668 status
= nfs4_setup_session_slot_tables(session
);
5669 dprintk("slot table setup returned %d\n", status
);
5673 ptr
= (unsigned *)&session
->sess_id
.data
[0];
5674 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
5675 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
5677 dprintk("<-- %s\n", __func__
);
5682 * Issue the over-the-wire RPC DESTROY_SESSION.
5683 * The caller must serialize access to this routine.
5685 int nfs4_proc_destroy_session(struct nfs4_session
*session
,
5686 struct rpc_cred
*cred
)
5688 struct rpc_message msg
= {
5689 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
],
5690 .rpc_argp
= session
,
5695 dprintk("--> nfs4_proc_destroy_session\n");
5697 /* session is still being setup */
5698 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
5701 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5704 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5705 "Session has been destroyed regardless...\n", status
);
5707 dprintk("<-- nfs4_proc_destroy_session\n");
5712 * Renew the cl_session lease.
5714 struct nfs4_sequence_data
{
5715 struct nfs_client
*clp
;
5716 struct nfs4_sequence_args args
;
5717 struct nfs4_sequence_res res
;
5720 static void nfs41_sequence_release(void *data
)
5722 struct nfs4_sequence_data
*calldata
= data
;
5723 struct nfs_client
*clp
= calldata
->clp
;
5725 if (atomic_read(&clp
->cl_count
) > 1)
5726 nfs4_schedule_state_renewal(clp
);
5727 nfs_put_client(clp
);
5731 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5733 switch(task
->tk_status
) {
5734 case -NFS4ERR_DELAY
:
5735 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5738 nfs4_schedule_lease_recovery(clp
);
5743 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5745 struct nfs4_sequence_data
*calldata
= data
;
5746 struct nfs_client
*clp
= calldata
->clp
;
5748 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
5751 if (task
->tk_status
< 0) {
5752 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5753 if (atomic_read(&clp
->cl_count
) == 1)
5756 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
5757 rpc_restart_call_prepare(task
);
5761 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5763 dprintk("<-- %s\n", __func__
);
5766 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5768 struct nfs4_sequence_data
*calldata
= data
;
5769 struct nfs_client
*clp
= calldata
->clp
;
5770 struct nfs4_sequence_args
*args
;
5771 struct nfs4_sequence_res
*res
;
5773 args
= task
->tk_msg
.rpc_argp
;
5774 res
= task
->tk_msg
.rpc_resp
;
5776 nfs41_setup_sequence(clp
->cl_session
, args
, res
, task
);
5779 static const struct rpc_call_ops nfs41_sequence_ops
= {
5780 .rpc_call_done
= nfs41_sequence_call_done
,
5781 .rpc_call_prepare
= nfs41_sequence_prepare
,
5782 .rpc_release
= nfs41_sequence_release
,
5785 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
,
5786 struct rpc_cred
*cred
,
5789 struct nfs4_sequence_data
*calldata
;
5790 struct rpc_message msg
= {
5791 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5794 struct rpc_task_setup task_setup_data
= {
5795 .rpc_client
= clp
->cl_rpcclient
,
5796 .rpc_message
= &msg
,
5797 .callback_ops
= &nfs41_sequence_ops
,
5798 .flags
= RPC_TASK_ASYNC
| RPC_TASK_SOFT
,
5801 if (!atomic_inc_not_zero(&clp
->cl_count
))
5802 return ERR_PTR(-EIO
);
5803 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5804 if (calldata
== NULL
) {
5805 nfs_put_client(clp
);
5806 return ERR_PTR(-ENOMEM
);
5808 nfs41_init_sequence(&calldata
->args
, &calldata
->res
, 0);
5810 nfs4_set_sequence_privileged(&calldata
->args
);
5811 msg
.rpc_argp
= &calldata
->args
;
5812 msg
.rpc_resp
= &calldata
->res
;
5813 calldata
->clp
= clp
;
5814 task_setup_data
.callback_data
= calldata
;
5816 return rpc_run_task(&task_setup_data
);
5819 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
, unsigned renew_flags
)
5821 struct rpc_task
*task
;
5824 if ((renew_flags
& NFS4_RENEW_TIMEOUT
) == 0)
5826 task
= _nfs41_proc_sequence(clp
, cred
, false);
5828 ret
= PTR_ERR(task
);
5830 rpc_put_task_async(task
);
5831 dprintk("<-- %s status=%d\n", __func__
, ret
);
5835 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5837 struct rpc_task
*task
;
5840 task
= _nfs41_proc_sequence(clp
, cred
, true);
5842 ret
= PTR_ERR(task
);
5845 ret
= rpc_wait_for_completion_task(task
);
5847 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
5849 if (task
->tk_status
== 0)
5850 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
5851 ret
= task
->tk_status
;
5855 dprintk("<-- %s status=%d\n", __func__
, ret
);
5859 struct nfs4_reclaim_complete_data
{
5860 struct nfs_client
*clp
;
5861 struct nfs41_reclaim_complete_args arg
;
5862 struct nfs41_reclaim_complete_res res
;
5865 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5867 struct nfs4_reclaim_complete_data
*calldata
= data
;
5869 nfs41_setup_sequence(calldata
->clp
->cl_session
,
5870 &calldata
->arg
.seq_args
,
5871 &calldata
->res
.seq_res
,
5875 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5877 switch(task
->tk_status
) {
5879 case -NFS4ERR_COMPLETE_ALREADY
:
5880 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
5882 case -NFS4ERR_DELAY
:
5883 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5885 case -NFS4ERR_RETRY_UNCACHED_REP
:
5888 nfs4_schedule_lease_recovery(clp
);
5893 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5895 struct nfs4_reclaim_complete_data
*calldata
= data
;
5896 struct nfs_client
*clp
= calldata
->clp
;
5897 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5899 dprintk("--> %s\n", __func__
);
5900 if (!nfs41_sequence_done(task
, res
))
5903 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
5904 rpc_restart_call_prepare(task
);
5907 dprintk("<-- %s\n", __func__
);
5910 static void nfs4_free_reclaim_complete_data(void *data
)
5912 struct nfs4_reclaim_complete_data
*calldata
= data
;
5917 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5918 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5919 .rpc_call_done
= nfs4_reclaim_complete_done
,
5920 .rpc_release
= nfs4_free_reclaim_complete_data
,
5924 * Issue a global reclaim complete.
5926 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5928 struct nfs4_reclaim_complete_data
*calldata
;
5929 struct rpc_task
*task
;
5930 struct rpc_message msg
= {
5931 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5933 struct rpc_task_setup task_setup_data
= {
5934 .rpc_client
= clp
->cl_rpcclient
,
5935 .rpc_message
= &msg
,
5936 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5937 .flags
= RPC_TASK_ASYNC
,
5939 int status
= -ENOMEM
;
5941 dprintk("--> %s\n", __func__
);
5942 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5943 if (calldata
== NULL
)
5945 calldata
->clp
= clp
;
5946 calldata
->arg
.one_fs
= 0;
5948 nfs41_init_sequence(&calldata
->arg
.seq_args
, &calldata
->res
.seq_res
, 0);
5949 nfs4_set_sequence_privileged(&calldata
->arg
.seq_args
);
5950 msg
.rpc_argp
= &calldata
->arg
;
5951 msg
.rpc_resp
= &calldata
->res
;
5952 task_setup_data
.callback_data
= calldata
;
5953 task
= rpc_run_task(&task_setup_data
);
5955 status
= PTR_ERR(task
);
5958 status
= nfs4_wait_for_completion_rpc_task(task
);
5960 status
= task
->tk_status
;
5964 dprintk("<-- %s status=%d\n", __func__
, status
);
5969 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
5971 struct nfs4_layoutget
*lgp
= calldata
;
5972 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
5973 struct nfs4_session
*session
= nfs4_get_session(server
);
5975 dprintk("--> %s\n", __func__
);
5976 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5977 * right now covering the LAYOUTGET we are about to send.
5978 * However, that is not so catastrophic, and there seems
5979 * to be no way to prevent it completely.
5981 if (nfs41_setup_sequence(session
, &lgp
->args
.seq_args
,
5982 &lgp
->res
.seq_res
, task
))
5984 if (pnfs_choose_layoutget_stateid(&lgp
->args
.stateid
,
5985 NFS_I(lgp
->args
.inode
)->layout
,
5986 lgp
->args
.ctx
->state
)) {
5987 rpc_exit(task
, NFS4_OK
);
5991 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
5993 struct nfs4_layoutget
*lgp
= calldata
;
5994 struct inode
*inode
= lgp
->args
.inode
;
5995 struct nfs_server
*server
= NFS_SERVER(inode
);
5996 struct pnfs_layout_hdr
*lo
;
5997 struct nfs4_state
*state
= NULL
;
5999 dprintk("--> %s\n", __func__
);
6001 if (!nfs41_sequence_done(task
, &lgp
->res
.seq_res
))
6004 switch (task
->tk_status
) {
6007 case -NFS4ERR_LAYOUTTRYLATER
:
6008 case -NFS4ERR_RECALLCONFLICT
:
6009 task
->tk_status
= -NFS4ERR_DELAY
;
6011 case -NFS4ERR_EXPIRED
:
6012 case -NFS4ERR_BAD_STATEID
:
6013 spin_lock(&inode
->i_lock
);
6014 lo
= NFS_I(inode
)->layout
;
6015 if (!lo
|| list_empty(&lo
->plh_segs
)) {
6016 spin_unlock(&inode
->i_lock
);
6017 /* If the open stateid was bad, then recover it. */
6018 state
= lgp
->args
.ctx
->state
;
6022 pnfs_mark_matching_lsegs_invalid(lo
, &head
, NULL
);
6023 spin_unlock(&inode
->i_lock
);
6024 /* Mark the bad layout state as invalid, then
6025 * retry using the open stateid. */
6026 pnfs_free_lseg_list(&head
);
6029 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
6030 rpc_restart_call_prepare(task
);
6032 dprintk("<-- %s\n", __func__
);
6035 static size_t max_response_pages(struct nfs_server
*server
)
6037 u32 max_resp_sz
= server
->nfs_client
->cl_session
->fc_attrs
.max_resp_sz
;
6038 return nfs_page_array_len(0, max_resp_sz
);
6041 static void nfs4_free_pages(struct page
**pages
, size_t size
)
6048 for (i
= 0; i
< size
; i
++) {
6051 __free_page(pages
[i
]);
6056 static struct page
**nfs4_alloc_pages(size_t size
, gfp_t gfp_flags
)
6058 struct page
**pages
;
6061 pages
= kcalloc(size
, sizeof(struct page
*), gfp_flags
);
6063 dprintk("%s: can't alloc array of %zu pages\n", __func__
, size
);
6067 for (i
= 0; i
< size
; i
++) {
6068 pages
[i
] = alloc_page(gfp_flags
);
6070 dprintk("%s: failed to allocate page\n", __func__
);
6071 nfs4_free_pages(pages
, size
);
6079 static void nfs4_layoutget_release(void *calldata
)
6081 struct nfs4_layoutget
*lgp
= calldata
;
6082 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
6083 size_t max_pages
= max_response_pages(server
);
6085 dprintk("--> %s\n", __func__
);
6086 nfs4_free_pages(lgp
->args
.layout
.pages
, max_pages
);
6087 put_nfs_open_context(lgp
->args
.ctx
);
6089 dprintk("<-- %s\n", __func__
);
6092 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
6093 .rpc_call_prepare
= nfs4_layoutget_prepare
,
6094 .rpc_call_done
= nfs4_layoutget_done
,
6095 .rpc_release
= nfs4_layoutget_release
,
6098 struct pnfs_layout_segment
*
6099 nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
, gfp_t gfp_flags
)
6101 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
6102 size_t max_pages
= max_response_pages(server
);
6103 struct rpc_task
*task
;
6104 struct rpc_message msg
= {
6105 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
6106 .rpc_argp
= &lgp
->args
,
6107 .rpc_resp
= &lgp
->res
,
6109 struct rpc_task_setup task_setup_data
= {
6110 .rpc_client
= server
->client
,
6111 .rpc_message
= &msg
,
6112 .callback_ops
= &nfs4_layoutget_call_ops
,
6113 .callback_data
= lgp
,
6114 .flags
= RPC_TASK_ASYNC
,
6116 struct pnfs_layout_segment
*lseg
= NULL
;
6119 dprintk("--> %s\n", __func__
);
6121 lgp
->args
.layout
.pages
= nfs4_alloc_pages(max_pages
, gfp_flags
);
6122 if (!lgp
->args
.layout
.pages
) {
6123 nfs4_layoutget_release(lgp
);
6124 return ERR_PTR(-ENOMEM
);
6126 lgp
->args
.layout
.pglen
= max_pages
* PAGE_SIZE
;
6128 lgp
->res
.layoutp
= &lgp
->args
.layout
;
6129 lgp
->res
.seq_res
.sr_slot
= NULL
;
6130 nfs41_init_sequence(&lgp
->args
.seq_args
, &lgp
->res
.seq_res
, 0);
6131 task
= rpc_run_task(&task_setup_data
);
6133 return ERR_CAST(task
);
6134 status
= nfs4_wait_for_completion_rpc_task(task
);
6136 status
= task
->tk_status
;
6138 lseg
= pnfs_layout_process(lgp
);
6140 dprintk("<-- %s status=%d\n", __func__
, status
);
6142 return ERR_PTR(status
);
6147 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
6149 struct nfs4_layoutreturn
*lrp
= calldata
;
6151 dprintk("--> %s\n", __func__
);
6152 nfs41_setup_sequence(lrp
->clp
->cl_session
,
6153 &lrp
->args
.seq_args
,
6158 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
6160 struct nfs4_layoutreturn
*lrp
= calldata
;
6161 struct nfs_server
*server
;
6163 dprintk("--> %s\n", __func__
);
6165 if (!nfs41_sequence_done(task
, &lrp
->res
.seq_res
))
6168 server
= NFS_SERVER(lrp
->args
.inode
);
6169 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
6170 rpc_restart_call_prepare(task
);
6173 dprintk("<-- %s\n", __func__
);
6176 static void nfs4_layoutreturn_release(void *calldata
)
6178 struct nfs4_layoutreturn
*lrp
= calldata
;
6179 struct pnfs_layout_hdr
*lo
= lrp
->args
.layout
;
6181 dprintk("--> %s\n", __func__
);
6182 spin_lock(&lo
->plh_inode
->i_lock
);
6183 if (lrp
->res
.lrs_present
)
6184 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
6185 lo
->plh_block_lgets
--;
6186 spin_unlock(&lo
->plh_inode
->i_lock
);
6187 pnfs_put_layout_hdr(lrp
->args
.layout
);
6189 dprintk("<-- %s\n", __func__
);
6192 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
6193 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
6194 .rpc_call_done
= nfs4_layoutreturn_done
,
6195 .rpc_release
= nfs4_layoutreturn_release
,
6198 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
)
6200 struct rpc_task
*task
;
6201 struct rpc_message msg
= {
6202 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
6203 .rpc_argp
= &lrp
->args
,
6204 .rpc_resp
= &lrp
->res
,
6206 struct rpc_task_setup task_setup_data
= {
6207 .rpc_client
= lrp
->clp
->cl_rpcclient
,
6208 .rpc_message
= &msg
,
6209 .callback_ops
= &nfs4_layoutreturn_call_ops
,
6210 .callback_data
= lrp
,
6214 dprintk("--> %s\n", __func__
);
6215 nfs41_init_sequence(&lrp
->args
.seq_args
, &lrp
->res
.seq_res
, 1);
6216 task
= rpc_run_task(&task_setup_data
);
6218 return PTR_ERR(task
);
6219 status
= task
->tk_status
;
6220 dprintk("<-- %s status=%d\n", __func__
, status
);
6226 * Retrieve the list of Data Server devices from the MDS.
6228 static int _nfs4_getdevicelist(struct nfs_server
*server
,
6229 const struct nfs_fh
*fh
,
6230 struct pnfs_devicelist
*devlist
)
6232 struct nfs4_getdevicelist_args args
= {
6234 .layoutclass
= server
->pnfs_curr_ld
->id
,
6236 struct nfs4_getdevicelist_res res
= {
6239 struct rpc_message msg
= {
6240 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICELIST
],
6246 dprintk("--> %s\n", __func__
);
6247 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
,
6249 dprintk("<-- %s status=%d\n", __func__
, status
);
6253 int nfs4_proc_getdevicelist(struct nfs_server
*server
,
6254 const struct nfs_fh
*fh
,
6255 struct pnfs_devicelist
*devlist
)
6257 struct nfs4_exception exception
= { };
6261 err
= nfs4_handle_exception(server
,
6262 _nfs4_getdevicelist(server
, fh
, devlist
),
6264 } while (exception
.retry
);
6266 dprintk("%s: err=%d, num_devs=%u\n", __func__
,
6267 err
, devlist
->num_devs
);
6271 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist
);
6274 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
, struct pnfs_device
*pdev
)
6276 struct nfs4_getdeviceinfo_args args
= {
6279 struct nfs4_getdeviceinfo_res res
= {
6282 struct rpc_message msg
= {
6283 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
6289 dprintk("--> %s\n", __func__
);
6290 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6291 dprintk("<-- %s status=%d\n", __func__
, status
);
6296 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
, struct pnfs_device
*pdev
)
6298 struct nfs4_exception exception
= { };
6302 err
= nfs4_handle_exception(server
,
6303 _nfs4_proc_getdeviceinfo(server
, pdev
),
6305 } while (exception
.retry
);
6308 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
6310 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
6312 struct nfs4_layoutcommit_data
*data
= calldata
;
6313 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
6314 struct nfs4_session
*session
= nfs4_get_session(server
);
6316 nfs41_setup_sequence(session
,
6317 &data
->args
.seq_args
,
6323 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
6325 struct nfs4_layoutcommit_data
*data
= calldata
;
6326 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
6328 if (!nfs41_sequence_done(task
, &data
->res
.seq_res
))
6331 switch (task
->tk_status
) { /* Just ignore these failures */
6332 case -NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
6333 case -NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
6334 case -NFS4ERR_BADLAYOUT
: /* no layout */
6335 case -NFS4ERR_GRACE
: /* loca_recalim always false */
6336 task
->tk_status
= 0;
6339 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
6343 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
6344 rpc_restart_call_prepare(task
);
6350 static void nfs4_layoutcommit_release(void *calldata
)
6352 struct nfs4_layoutcommit_data
*data
= calldata
;
6353 struct pnfs_layout_segment
*lseg
, *tmp
;
6354 unsigned long *bitlock
= &NFS_I(data
->args
.inode
)->flags
;
6356 pnfs_cleanup_layoutcommit(data
);
6357 /* Matched by references in pnfs_set_layoutcommit */
6358 list_for_each_entry_safe(lseg
, tmp
, &data
->lseg_list
, pls_lc_list
) {
6359 list_del_init(&lseg
->pls_lc_list
);
6360 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT
,
6362 pnfs_put_lseg(lseg
);
6365 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING
, bitlock
);
6366 smp_mb__after_clear_bit();
6367 wake_up_bit(bitlock
, NFS_INO_LAYOUTCOMMITTING
);
6369 put_rpccred(data
->cred
);
6373 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
6374 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
6375 .rpc_call_done
= nfs4_layoutcommit_done
,
6376 .rpc_release
= nfs4_layoutcommit_release
,
6380 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
6382 struct rpc_message msg
= {
6383 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
6384 .rpc_argp
= &data
->args
,
6385 .rpc_resp
= &data
->res
,
6386 .rpc_cred
= data
->cred
,
6388 struct rpc_task_setup task_setup_data
= {
6389 .task
= &data
->task
,
6390 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
6391 .rpc_message
= &msg
,
6392 .callback_ops
= &nfs4_layoutcommit_ops
,
6393 .callback_data
= data
,
6394 .flags
= RPC_TASK_ASYNC
,
6396 struct rpc_task
*task
;
6399 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6400 "lbw: %llu inode %lu\n",
6401 data
->task
.tk_pid
, sync
,
6402 data
->args
.lastbytewritten
,
6403 data
->args
.inode
->i_ino
);
6405 nfs41_init_sequence(&data
->args
.seq_args
, &data
->res
.seq_res
, 1);
6406 task
= rpc_run_task(&task_setup_data
);
6408 return PTR_ERR(task
);
6411 status
= nfs4_wait_for_completion_rpc_task(task
);
6414 status
= task
->tk_status
;
6416 dprintk("%s: status %d\n", __func__
, status
);
6422 _nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
6423 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
6425 struct nfs41_secinfo_no_name_args args
= {
6426 .style
= SECINFO_STYLE_CURRENT_FH
,
6428 struct nfs4_secinfo_res res
= {
6431 struct rpc_message msg
= {
6432 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO_NO_NAME
],
6436 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
6440 nfs41_proc_secinfo_no_name(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
6441 struct nfs_fsinfo
*info
, struct nfs4_secinfo_flavors
*flavors
)
6443 struct nfs4_exception exception
= { };
6446 err
= _nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
6449 case -NFS4ERR_WRONGSEC
:
6450 case -NFS4ERR_NOTSUPP
:
6453 err
= nfs4_handle_exception(server
, err
, &exception
);
6455 } while (exception
.retry
);
6461 nfs41_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
6462 struct nfs_fsinfo
*info
)
6466 rpc_authflavor_t flavor
;
6467 struct nfs4_secinfo_flavors
*flavors
;
6469 page
= alloc_page(GFP_KERNEL
);
6475 flavors
= page_address(page
);
6476 err
= nfs41_proc_secinfo_no_name(server
, fhandle
, info
, flavors
);
6479 * Fall back on "guess and check" method if
6480 * the server doesn't support SECINFO_NO_NAME
6482 if (err
== -NFS4ERR_WRONGSEC
|| err
== -NFS4ERR_NOTSUPP
) {
6483 err
= nfs4_find_root_sec(server
, fhandle
, info
);
6489 flavor
= nfs_find_best_sec(flavors
);
6491 err
= nfs4_lookup_root_sec(server
, fhandle
, info
, flavor
);
6501 static int _nfs41_test_stateid(struct nfs_server
*server
, nfs4_stateid
*stateid
)
6504 struct nfs41_test_stateid_args args
= {
6507 struct nfs41_test_stateid_res res
;
6508 struct rpc_message msg
= {
6509 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_TEST_STATEID
],
6514 dprintk("NFS call test_stateid %p\n", stateid
);
6515 nfs41_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6516 nfs4_set_sequence_privileged(&args
.seq_args
);
6517 status
= nfs4_call_sync_sequence(server
->client
, server
, &msg
,
6518 &args
.seq_args
, &res
.seq_res
);
6519 if (status
!= NFS_OK
) {
6520 dprintk("NFS reply test_stateid: failed, %d\n", status
);
6523 dprintk("NFS reply test_stateid: succeeded, %d\n", -res
.status
);
6528 * nfs41_test_stateid - perform a TEST_STATEID operation
6530 * @server: server / transport on which to perform the operation
6531 * @stateid: state ID to test
6533 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6534 * Otherwise a negative NFS4ERR value is returned if the operation
6535 * failed or the state ID is not currently valid.
6537 static int nfs41_test_stateid(struct nfs_server
*server
, nfs4_stateid
*stateid
)
6539 struct nfs4_exception exception
= { };
6542 err
= _nfs41_test_stateid(server
, stateid
);
6543 if (err
!= -NFS4ERR_DELAY
)
6545 nfs4_handle_exception(server
, err
, &exception
);
6546 } while (exception
.retry
);
6550 static int _nfs4_free_stateid(struct nfs_server
*server
, nfs4_stateid
*stateid
)
6552 struct nfs41_free_stateid_args args
= {
6555 struct nfs41_free_stateid_res res
;
6556 struct rpc_message msg
= {
6557 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FREE_STATEID
],
6563 dprintk("NFS call free_stateid %p\n", stateid
);
6564 nfs41_init_sequence(&args
.seq_args
, &res
.seq_res
, 0);
6565 nfs4_set_sequence_privileged(&args
.seq_args
);
6566 status
= nfs4_call_sync_sequence(server
->client
, server
, &msg
,
6567 &args
.seq_args
, &res
.seq_res
);
6568 dprintk("NFS reply free_stateid: %d\n", status
);
6573 * nfs41_free_stateid - perform a FREE_STATEID operation
6575 * @server: server / transport on which to perform the operation
6576 * @stateid: state ID to release
6578 * Returns NFS_OK if the server freed "stateid". Otherwise a
6579 * negative NFS4ERR value is returned.
6581 static int nfs41_free_stateid(struct nfs_server
*server
, nfs4_stateid
*stateid
)
6583 struct nfs4_exception exception
= { };
6586 err
= _nfs4_free_stateid(server
, stateid
);
6587 if (err
!= -NFS4ERR_DELAY
)
6589 nfs4_handle_exception(server
, err
, &exception
);
6590 } while (exception
.retry
);
6594 static bool nfs41_match_stateid(const nfs4_stateid
*s1
,
6595 const nfs4_stateid
*s2
)
6597 if (memcmp(s1
->other
, s2
->other
, sizeof(s1
->other
)) != 0)
6600 if (s1
->seqid
== s2
->seqid
)
6602 if (s1
->seqid
== 0 || s2
->seqid
== 0)
6608 #endif /* CONFIG_NFS_V4_1 */
6610 static bool nfs4_match_stateid(const nfs4_stateid
*s1
,
6611 const nfs4_stateid
*s2
)
6613 return nfs4_stateid_match(s1
, s2
);
6617 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
6618 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
6619 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
6620 .recover_open
= nfs4_open_reclaim
,
6621 .recover_lock
= nfs4_lock_reclaim
,
6622 .establish_clid
= nfs4_init_clientid
,
6623 .get_clid_cred
= nfs4_get_setclientid_cred
,
6624 .detect_trunking
= nfs40_discover_server_trunking
,
6627 #if defined(CONFIG_NFS_V4_1)
6628 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
6629 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
6630 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
6631 .recover_open
= nfs4_open_reclaim
,
6632 .recover_lock
= nfs4_lock_reclaim
,
6633 .establish_clid
= nfs41_init_clientid
,
6634 .get_clid_cred
= nfs4_get_exchange_id_cred
,
6635 .reclaim_complete
= nfs41_proc_reclaim_complete
,
6636 .detect_trunking
= nfs41_discover_server_trunking
,
6638 #endif /* CONFIG_NFS_V4_1 */
6640 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
6641 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
6642 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
6643 .recover_open
= nfs4_open_expired
,
6644 .recover_lock
= nfs4_lock_expired
,
6645 .establish_clid
= nfs4_init_clientid
,
6646 .get_clid_cred
= nfs4_get_setclientid_cred
,
6649 #if defined(CONFIG_NFS_V4_1)
6650 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
6651 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
6652 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
6653 .recover_open
= nfs41_open_expired
,
6654 .recover_lock
= nfs41_lock_expired
,
6655 .establish_clid
= nfs41_init_clientid
,
6656 .get_clid_cred
= nfs4_get_exchange_id_cred
,
6658 #endif /* CONFIG_NFS_V4_1 */
6660 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
6661 .sched_state_renewal
= nfs4_proc_async_renew
,
6662 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
6663 .renew_lease
= nfs4_proc_renew
,
6666 #if defined(CONFIG_NFS_V4_1)
6667 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
6668 .sched_state_renewal
= nfs41_proc_async_sequence
,
6669 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
6670 .renew_lease
= nfs4_proc_sequence
,
6674 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
6676 .call_sync
= _nfs4_call_sync
,
6677 .match_stateid
= nfs4_match_stateid
,
6678 .find_root_sec
= nfs4_find_root_sec
,
6679 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
6680 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
6681 .state_renewal_ops
= &nfs40_state_renewal_ops
,
6684 #if defined(CONFIG_NFS_V4_1)
6685 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
6687 .call_sync
= nfs4_call_sync_sequence
,
6688 .match_stateid
= nfs41_match_stateid
,
6689 .find_root_sec
= nfs41_find_root_sec
,
6690 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
6691 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
6692 .state_renewal_ops
= &nfs41_state_renewal_ops
,
6696 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
6697 [0] = &nfs_v4_0_minor_ops
,
6698 #if defined(CONFIG_NFS_V4_1)
6699 [1] = &nfs_v4_1_minor_ops
,
6703 const struct inode_operations nfs4_dir_inode_operations
= {
6704 .create
= nfs_create
,
6705 .lookup
= nfs_lookup
,
6706 .atomic_open
= nfs_atomic_open
,
6708 .unlink
= nfs_unlink
,
6709 .symlink
= nfs_symlink
,
6713 .rename
= nfs_rename
,
6714 .permission
= nfs_permission
,
6715 .getattr
= nfs_getattr
,
6716 .setattr
= nfs_setattr
,
6717 .getxattr
= generic_getxattr
,
6718 .setxattr
= generic_setxattr
,
6719 .listxattr
= generic_listxattr
,
6720 .removexattr
= generic_removexattr
,
6723 static const struct inode_operations nfs4_file_inode_operations
= {
6724 .permission
= nfs_permission
,
6725 .getattr
= nfs_getattr
,
6726 .setattr
= nfs_setattr
,
6727 .getxattr
= generic_getxattr
,
6728 .setxattr
= generic_setxattr
,
6729 .listxattr
= generic_listxattr
,
6730 .removexattr
= generic_removexattr
,
6733 const struct nfs_rpc_ops nfs_v4_clientops
= {
6734 .version
= 4, /* protocol version */
6735 .dentry_ops
= &nfs4_dentry_operations
,
6736 .dir_inode_ops
= &nfs4_dir_inode_operations
,
6737 .file_inode_ops
= &nfs4_file_inode_operations
,
6738 .file_ops
= &nfs4_file_operations
,
6739 .getroot
= nfs4_proc_get_root
,
6740 .submount
= nfs4_submount
,
6741 .try_mount
= nfs4_try_mount
,
6742 .getattr
= nfs4_proc_getattr
,
6743 .setattr
= nfs4_proc_setattr
,
6744 .lookup
= nfs4_proc_lookup
,
6745 .access
= nfs4_proc_access
,
6746 .readlink
= nfs4_proc_readlink
,
6747 .create
= nfs4_proc_create
,
6748 .remove
= nfs4_proc_remove
,
6749 .unlink_setup
= nfs4_proc_unlink_setup
,
6750 .unlink_rpc_prepare
= nfs4_proc_unlink_rpc_prepare
,
6751 .unlink_done
= nfs4_proc_unlink_done
,
6752 .rename
= nfs4_proc_rename
,
6753 .rename_setup
= nfs4_proc_rename_setup
,
6754 .rename_rpc_prepare
= nfs4_proc_rename_rpc_prepare
,
6755 .rename_done
= nfs4_proc_rename_done
,
6756 .link
= nfs4_proc_link
,
6757 .symlink
= nfs4_proc_symlink
,
6758 .mkdir
= nfs4_proc_mkdir
,
6759 .rmdir
= nfs4_proc_remove
,
6760 .readdir
= nfs4_proc_readdir
,
6761 .mknod
= nfs4_proc_mknod
,
6762 .statfs
= nfs4_proc_statfs
,
6763 .fsinfo
= nfs4_proc_fsinfo
,
6764 .pathconf
= nfs4_proc_pathconf
,
6765 .set_capabilities
= nfs4_server_capabilities
,
6766 .decode_dirent
= nfs4_decode_dirent
,
6767 .read_setup
= nfs4_proc_read_setup
,
6768 .read_pageio_init
= pnfs_pageio_init_read
,
6769 .read_rpc_prepare
= nfs4_proc_read_rpc_prepare
,
6770 .read_done
= nfs4_read_done
,
6771 .write_setup
= nfs4_proc_write_setup
,
6772 .write_pageio_init
= pnfs_pageio_init_write
,
6773 .write_rpc_prepare
= nfs4_proc_write_rpc_prepare
,
6774 .write_done
= nfs4_write_done
,
6775 .commit_setup
= nfs4_proc_commit_setup
,
6776 .commit_rpc_prepare
= nfs4_proc_commit_rpc_prepare
,
6777 .commit_done
= nfs4_commit_done
,
6778 .lock
= nfs4_proc_lock
,
6779 .clear_acl_cache
= nfs4_zap_acl_attr
,
6780 .close_context
= nfs4_close_context
,
6781 .open_context
= nfs4_atomic_open
,
6782 .have_delegation
= nfs4_have_delegation
,
6783 .return_delegation
= nfs4_inode_return_delegation
,
6784 .alloc_client
= nfs4_alloc_client
,
6785 .init_client
= nfs4_init_client
,
6786 .free_client
= nfs4_free_client
,
6787 .create_server
= nfs4_create_server
,
6788 .clone_server
= nfs_clone_server
,
6791 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
6792 .prefix
= XATTR_NAME_NFSV4_ACL
,
6793 .list
= nfs4_xattr_list_nfs4_acl
,
6794 .get
= nfs4_xattr_get_nfs4_acl
,
6795 .set
= nfs4_xattr_set_nfs4_acl
,
6798 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
6799 &nfs4_xattr_nfs4_acl_handler
,