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/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/sunrpc/gss_api.h>
45 #include <linux/nfs.h>
46 #include <linux/nfs4.h>
47 #include <linux/nfs_fs.h>
48 #include <linux/nfs_page.h>
49 #include <linux/nfs_mount.h>
50 #include <linux/namei.h>
51 #include <linux/mount.h>
52 #include <linux/module.h>
53 #include <linux/sunrpc/bc_xprt.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
58 #include "delegation.h"
64 #define NFSDBG_FACILITY NFSDBG_PROC
66 #define NFS4_POLL_RETRY_MIN (HZ/10)
67 #define NFS4_POLL_RETRY_MAX (15*HZ)
69 #define NFS4_MAX_LOOP_ON_RECOVER (10)
72 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
73 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
74 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
75 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
76 static int _nfs4_proc_lookup(struct rpc_clnt
*client
, struct inode
*dir
,
77 const struct qstr
*name
, struct nfs_fh
*fhandle
,
78 struct nfs_fattr
*fattr
);
79 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
80 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
81 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
82 struct nfs4_state
*state
);
84 /* Prevent leaks of NFSv4 errors into userland */
85 static int nfs4_map_errors(int err
)
90 case -NFS4ERR_RESOURCE
:
92 case -NFS4ERR_WRONGSEC
:
94 case -NFS4ERR_BADOWNER
:
95 case -NFS4ERR_BADNAME
:
97 case -NFS4ERR_SHARE_DENIED
:
100 dprintk("%s could not handle NFSv4 error %d\n",
108 * This is our standard bitmap for GETATTR requests.
110 const u32 nfs4_fattr_bitmap
[2] = {
112 | FATTR4_WORD0_CHANGE
115 | FATTR4_WORD0_FILEID
,
117 | FATTR4_WORD1_NUMLINKS
119 | FATTR4_WORD1_OWNER_GROUP
120 | FATTR4_WORD1_RAWDEV
121 | FATTR4_WORD1_SPACE_USED
122 | FATTR4_WORD1_TIME_ACCESS
123 | FATTR4_WORD1_TIME_METADATA
124 | FATTR4_WORD1_TIME_MODIFY
127 const u32 nfs4_statfs_bitmap
[2] = {
128 FATTR4_WORD0_FILES_AVAIL
129 | FATTR4_WORD0_FILES_FREE
130 | FATTR4_WORD0_FILES_TOTAL
,
131 FATTR4_WORD1_SPACE_AVAIL
132 | FATTR4_WORD1_SPACE_FREE
133 | FATTR4_WORD1_SPACE_TOTAL
136 const u32 nfs4_pathconf_bitmap
[2] = {
138 | FATTR4_WORD0_MAXNAME
,
142 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
143 | FATTR4_WORD0_MAXREAD
144 | FATTR4_WORD0_MAXWRITE
145 | FATTR4_WORD0_LEASE_TIME
,
146 FATTR4_WORD1_TIME_DELTA
147 | FATTR4_WORD1_FS_LAYOUT_TYPES
150 const u32 nfs4_fs_locations_bitmap
[2] = {
152 | FATTR4_WORD0_CHANGE
155 | FATTR4_WORD0_FILEID
156 | FATTR4_WORD0_FS_LOCATIONS
,
158 | FATTR4_WORD1_NUMLINKS
160 | FATTR4_WORD1_OWNER_GROUP
161 | FATTR4_WORD1_RAWDEV
162 | FATTR4_WORD1_SPACE_USED
163 | FATTR4_WORD1_TIME_ACCESS
164 | FATTR4_WORD1_TIME_METADATA
165 | FATTR4_WORD1_TIME_MODIFY
166 | FATTR4_WORD1_MOUNTED_ON_FILEID
169 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
170 struct nfs4_readdir_arg
*readdir
)
174 BUG_ON(readdir
->count
< 80);
176 readdir
->cookie
= cookie
;
177 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
182 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
187 * NFSv4 servers do not return entries for '.' and '..'
188 * Therefore, we fake these entries here. We let '.'
189 * have cookie 0 and '..' have cookie 1. Note that
190 * when talking to the server, we always send cookie 0
193 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
196 *p
++ = xdr_one
; /* next */
197 *p
++ = xdr_zero
; /* cookie, first word */
198 *p
++ = xdr_one
; /* cookie, second word */
199 *p
++ = xdr_one
; /* entry len */
200 memcpy(p
, ".\0\0\0", 4); /* entry */
202 *p
++ = xdr_one
; /* bitmap length */
203 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
204 *p
++ = htonl(8); /* attribute buffer length */
205 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
208 *p
++ = xdr_one
; /* next */
209 *p
++ = xdr_zero
; /* cookie, first word */
210 *p
++ = xdr_two
; /* cookie, second word */
211 *p
++ = xdr_two
; /* entry len */
212 memcpy(p
, "..\0\0", 4); /* entry */
214 *p
++ = xdr_one
; /* bitmap length */
215 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
216 *p
++ = htonl(8); /* attribute buffer length */
217 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
219 readdir
->pgbase
= (char *)p
- (char *)start
;
220 readdir
->count
-= readdir
->pgbase
;
221 kunmap_atomic(start
, KM_USER0
);
224 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
230 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
231 nfs_wait_bit_killable
, TASK_KILLABLE
);
235 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
242 *timeout
= NFS4_POLL_RETRY_MIN
;
243 if (*timeout
> NFS4_POLL_RETRY_MAX
)
244 *timeout
= NFS4_POLL_RETRY_MAX
;
245 schedule_timeout_killable(*timeout
);
246 if (fatal_signal_pending(current
))
252 /* This is the error handling routine for processes that are allowed
255 static int nfs4_handle_exception(struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
257 struct nfs_client
*clp
= server
->nfs_client
;
258 struct nfs4_state
*state
= exception
->state
;
259 struct inode
*inode
= exception
->inode
;
262 exception
->retry
= 0;
266 case -NFS4ERR_OPENMODE
:
267 if (nfs_have_delegation(inode
, FMODE_READ
)) {
268 nfs_inode_return_delegation(inode
);
269 exception
->retry
= 1;
274 nfs4_schedule_stateid_recovery(server
, state
);
275 goto wait_on_recovery
;
276 case -NFS4ERR_DELEG_REVOKED
:
277 case -NFS4ERR_ADMIN_REVOKED
:
278 case -NFS4ERR_BAD_STATEID
:
280 nfs_remove_bad_delegation(state
->inode
);
283 nfs4_schedule_stateid_recovery(server
, state
);
284 goto wait_on_recovery
;
285 case -NFS4ERR_EXPIRED
:
287 nfs4_schedule_stateid_recovery(server
, state
);
288 case -NFS4ERR_STALE_STATEID
:
289 case -NFS4ERR_STALE_CLIENTID
:
290 nfs4_schedule_lease_recovery(clp
);
291 goto wait_on_recovery
;
292 #if defined(CONFIG_NFS_V4_1)
293 case -NFS4ERR_BADSESSION
:
294 case -NFS4ERR_BADSLOT
:
295 case -NFS4ERR_BAD_HIGH_SLOT
:
296 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
297 case -NFS4ERR_DEADSESSION
:
298 case -NFS4ERR_SEQ_FALSE_RETRY
:
299 case -NFS4ERR_SEQ_MISORDERED
:
300 dprintk("%s ERROR: %d Reset session\n", __func__
,
302 nfs4_schedule_session_recovery(clp
->cl_session
);
303 exception
->retry
= 1;
305 #endif /* defined(CONFIG_NFS_V4_1) */
306 case -NFS4ERR_FILE_OPEN
:
307 if (exception
->timeout
> HZ
) {
308 /* We have retried a decent amount, time to
317 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
320 case -NFS4ERR_RETRY_UNCACHED_REP
:
321 case -NFS4ERR_OLD_STATEID
:
322 exception
->retry
= 1;
324 case -NFS4ERR_BADOWNER
:
325 /* The following works around a Linux server bug! */
326 case -NFS4ERR_BADNAME
:
327 if (server
->caps
& NFS_CAP_UIDGID_NOMAP
) {
328 server
->caps
&= ~NFS_CAP_UIDGID_NOMAP
;
329 exception
->retry
= 1;
330 printk(KERN_WARNING
"NFS: v4 server %s "
331 "does not accept raw "
333 "Reenabling the idmapper.\n",
334 server
->nfs_client
->cl_hostname
);
337 /* We failed to handle the error */
338 return nfs4_map_errors(ret
);
340 ret
= nfs4_wait_clnt_recover(clp
);
342 exception
->retry
= 1;
347 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
349 spin_lock(&clp
->cl_lock
);
350 if (time_before(clp
->cl_last_renewal
,timestamp
))
351 clp
->cl_last_renewal
= timestamp
;
352 spin_unlock(&clp
->cl_lock
);
355 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
357 do_renew_lease(server
->nfs_client
, timestamp
);
360 #if defined(CONFIG_NFS_V4_1)
363 * nfs4_free_slot - free a slot and efficiently update slot table.
365 * freeing a slot is trivially done by clearing its respective bit
367 * If the freed slotid equals highest_used_slotid we want to update it
368 * so that the server would be able to size down the slot table if needed,
369 * otherwise we know that the highest_used_slotid is still in use.
370 * When updating highest_used_slotid there may be "holes" in the bitmap
371 * so we need to scan down from highest_used_slotid to 0 looking for the now
372 * highest slotid in use.
373 * If none found, highest_used_slotid is set to -1.
375 * Must be called while holding tbl->slot_tbl_lock
378 nfs4_free_slot(struct nfs4_slot_table
*tbl
, struct nfs4_slot
*free_slot
)
380 int free_slotid
= free_slot
- tbl
->slots
;
381 int slotid
= free_slotid
;
383 BUG_ON(slotid
< 0 || slotid
>= NFS4_MAX_SLOT_TABLE
);
384 /* clear used bit in bitmap */
385 __clear_bit(slotid
, tbl
->used_slots
);
387 /* update highest_used_slotid when it is freed */
388 if (slotid
== tbl
->highest_used_slotid
) {
389 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
390 if (slotid
< tbl
->max_slots
)
391 tbl
->highest_used_slotid
= slotid
;
393 tbl
->highest_used_slotid
= -1;
395 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
396 free_slotid
, tbl
->highest_used_slotid
);
400 * Signal state manager thread if session fore channel is drained
402 static void nfs4_check_drain_fc_complete(struct nfs4_session
*ses
)
404 struct rpc_task
*task
;
406 if (!test_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
)) {
407 task
= rpc_wake_up_next(&ses
->fc_slot_table
.slot_tbl_waitq
);
409 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
413 if (ses
->fc_slot_table
.highest_used_slotid
!= -1)
416 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__
);
417 complete(&ses
->fc_slot_table
.complete
);
421 * Signal state manager thread if session back channel is drained
423 void nfs4_check_drain_bc_complete(struct nfs4_session
*ses
)
425 if (!test_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
) ||
426 ses
->bc_slot_table
.highest_used_slotid
!= -1)
428 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__
);
429 complete(&ses
->bc_slot_table
.complete
);
432 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
434 struct nfs4_slot_table
*tbl
;
436 tbl
= &res
->sr_session
->fc_slot_table
;
438 /* just wake up the next guy waiting since
439 * we may have not consumed a slot after all */
440 dprintk("%s: No slot\n", __func__
);
444 spin_lock(&tbl
->slot_tbl_lock
);
445 nfs4_free_slot(tbl
, res
->sr_slot
);
446 nfs4_check_drain_fc_complete(res
->sr_session
);
447 spin_unlock(&tbl
->slot_tbl_lock
);
451 static int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
453 unsigned long timestamp
;
454 struct nfs_client
*clp
;
457 * sr_status remains 1 if an RPC level error occurred. The server
458 * may or may not have processed the sequence operation..
459 * Proceed as if the server received and processed the sequence
462 if (res
->sr_status
== 1)
463 res
->sr_status
= NFS_OK
;
465 /* don't increment the sequence number if the task wasn't sent */
466 if (!RPC_WAS_SENT(task
))
469 /* Check the SEQUENCE operation status */
470 switch (res
->sr_status
) {
472 /* Update the slot's sequence and clientid lease timer */
473 ++res
->sr_slot
->seq_nr
;
474 timestamp
= res
->sr_renewal_time
;
475 clp
= res
->sr_session
->clp
;
476 do_renew_lease(clp
, timestamp
);
477 /* Check sequence flags */
478 if (res
->sr_status_flags
!= 0)
479 nfs4_schedule_lease_recovery(clp
);
482 /* The server detected a resend of the RPC call and
483 * returned NFS4ERR_DELAY as per Section 2.10.6.2
486 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
488 res
->sr_slot
- res
->sr_session
->fc_slot_table
.slots
,
489 res
->sr_slot
->seq_nr
);
492 /* Just update the slot sequence no. */
493 ++res
->sr_slot
->seq_nr
;
496 /* The session may be reset by one of the error handlers. */
497 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
498 nfs41_sequence_free_slot(res
);
501 if (!rpc_restart_call(task
))
503 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
507 static int nfs4_sequence_done(struct rpc_task
*task
,
508 struct nfs4_sequence_res
*res
)
510 if (res
->sr_session
== NULL
)
512 return nfs41_sequence_done(task
, res
);
516 * nfs4_find_slot - efficiently look for a free slot
518 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
519 * If found, we mark the slot as used, update the highest_used_slotid,
520 * and respectively set up the sequence operation args.
521 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
523 * Note: must be called with under the slot_tbl_lock.
526 nfs4_find_slot(struct nfs4_slot_table
*tbl
)
529 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
530 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
532 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
533 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
535 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
536 if (slotid
>= tbl
->max_slots
)
538 __set_bit(slotid
, tbl
->used_slots
);
539 if (slotid
> tbl
->highest_used_slotid
)
540 tbl
->highest_used_slotid
= slotid
;
543 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
544 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
548 int nfs41_setup_sequence(struct nfs4_session
*session
,
549 struct nfs4_sequence_args
*args
,
550 struct nfs4_sequence_res
*res
,
552 struct rpc_task
*task
)
554 struct nfs4_slot
*slot
;
555 struct nfs4_slot_table
*tbl
;
558 dprintk("--> %s\n", __func__
);
559 /* slot already allocated? */
560 if (res
->sr_slot
!= NULL
)
563 tbl
= &session
->fc_slot_table
;
565 spin_lock(&tbl
->slot_tbl_lock
);
566 if (test_bit(NFS4_SESSION_DRAINING
, &session
->session_state
) &&
567 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
569 * The state manager will wait until the slot table is empty.
570 * Schedule the reset thread
572 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
573 spin_unlock(&tbl
->slot_tbl_lock
);
574 dprintk("%s Schedule Session Reset\n", __func__
);
578 if (!rpc_queue_empty(&tbl
->slot_tbl_waitq
) &&
579 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
580 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
581 spin_unlock(&tbl
->slot_tbl_lock
);
582 dprintk("%s enforce FIFO order\n", __func__
);
586 slotid
= nfs4_find_slot(tbl
);
587 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
588 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
589 spin_unlock(&tbl
->slot_tbl_lock
);
590 dprintk("<-- %s: no free slots\n", __func__
);
593 spin_unlock(&tbl
->slot_tbl_lock
);
595 rpc_task_set_priority(task
, RPC_PRIORITY_NORMAL
);
596 slot
= tbl
->slots
+ slotid
;
597 args
->sa_session
= session
;
598 args
->sa_slotid
= slotid
;
599 args
->sa_cache_this
= cache_reply
;
601 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
603 res
->sr_session
= session
;
605 res
->sr_renewal_time
= jiffies
;
606 res
->sr_status_flags
= 0;
608 * sr_status is only set in decode_sequence, and so will remain
609 * set to 1 if an rpc level failure occurs.
614 EXPORT_SYMBOL_GPL(nfs41_setup_sequence
);
616 int nfs4_setup_sequence(const struct nfs_server
*server
,
617 struct nfs4_sequence_args
*args
,
618 struct nfs4_sequence_res
*res
,
620 struct rpc_task
*task
)
622 struct nfs4_session
*session
= nfs4_get_session(server
);
625 if (session
== NULL
) {
626 args
->sa_session
= NULL
;
627 res
->sr_session
= NULL
;
631 dprintk("--> %s clp %p session %p sr_slot %td\n",
632 __func__
, session
->clp
, session
, res
->sr_slot
?
633 res
->sr_slot
- session
->fc_slot_table
.slots
: -1);
635 ret
= nfs41_setup_sequence(session
, args
, res
, cache_reply
,
638 dprintk("<-- %s status=%d\n", __func__
, ret
);
642 struct nfs41_call_sync_data
{
643 const struct nfs_server
*seq_server
;
644 struct nfs4_sequence_args
*seq_args
;
645 struct nfs4_sequence_res
*seq_res
;
649 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
651 struct nfs41_call_sync_data
*data
= calldata
;
653 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
655 if (nfs4_setup_sequence(data
->seq_server
, data
->seq_args
,
656 data
->seq_res
, data
->cache_reply
, task
))
658 rpc_call_start(task
);
661 static void nfs41_call_priv_sync_prepare(struct rpc_task
*task
, void *calldata
)
663 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
664 nfs41_call_sync_prepare(task
, calldata
);
667 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
669 struct nfs41_call_sync_data
*data
= calldata
;
671 nfs41_sequence_done(task
, data
->seq_res
);
674 struct rpc_call_ops nfs41_call_sync_ops
= {
675 .rpc_call_prepare
= nfs41_call_sync_prepare
,
676 .rpc_call_done
= nfs41_call_sync_done
,
679 struct rpc_call_ops nfs41_call_priv_sync_ops
= {
680 .rpc_call_prepare
= nfs41_call_priv_sync_prepare
,
681 .rpc_call_done
= nfs41_call_sync_done
,
684 static int nfs4_call_sync_sequence(struct rpc_clnt
*clnt
,
685 struct nfs_server
*server
,
686 struct rpc_message
*msg
,
687 struct nfs4_sequence_args
*args
,
688 struct nfs4_sequence_res
*res
,
693 struct rpc_task
*task
;
694 struct nfs41_call_sync_data data
= {
695 .seq_server
= server
,
698 .cache_reply
= cache_reply
,
700 struct rpc_task_setup task_setup
= {
703 .callback_ops
= &nfs41_call_sync_ops
,
704 .callback_data
= &data
709 task_setup
.callback_ops
= &nfs41_call_priv_sync_ops
;
710 task
= rpc_run_task(&task_setup
);
714 ret
= task
->tk_status
;
720 int _nfs4_call_sync_session(struct rpc_clnt
*clnt
,
721 struct nfs_server
*server
,
722 struct rpc_message
*msg
,
723 struct nfs4_sequence_args
*args
,
724 struct nfs4_sequence_res
*res
,
727 return nfs4_call_sync_sequence(clnt
, server
, msg
, args
, res
, cache_reply
, 0);
731 static int nfs4_sequence_done(struct rpc_task
*task
,
732 struct nfs4_sequence_res
*res
)
736 #endif /* CONFIG_NFS_V4_1 */
738 int _nfs4_call_sync(struct rpc_clnt
*clnt
,
739 struct nfs_server
*server
,
740 struct rpc_message
*msg
,
741 struct nfs4_sequence_args
*args
,
742 struct nfs4_sequence_res
*res
,
745 args
->sa_session
= res
->sr_session
= NULL
;
746 return rpc_call_sync(clnt
, msg
, 0);
750 int nfs4_call_sync(struct rpc_clnt
*clnt
,
751 struct nfs_server
*server
,
752 struct rpc_message
*msg
,
753 struct nfs4_sequence_args
*args
,
754 struct nfs4_sequence_res
*res
,
757 return server
->nfs_client
->cl_mvops
->call_sync(clnt
, server
, msg
,
758 args
, res
, cache_reply
);
761 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
763 struct nfs_inode
*nfsi
= NFS_I(dir
);
765 spin_lock(&dir
->i_lock
);
766 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
767 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
768 nfs_force_lookup_revalidate(dir
);
769 nfsi
->change_attr
= cinfo
->after
;
770 spin_unlock(&dir
->i_lock
);
773 struct nfs4_opendata
{
775 struct nfs_openargs o_arg
;
776 struct nfs_openres o_res
;
777 struct nfs_open_confirmargs c_arg
;
778 struct nfs_open_confirmres c_res
;
779 struct nfs_fattr f_attr
;
780 struct nfs_fattr dir_attr
;
783 struct nfs4_state_owner
*owner
;
784 struct nfs4_state
*state
;
786 unsigned long timestamp
;
787 unsigned int rpc_done
: 1;
793 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
795 p
->o_res
.f_attr
= &p
->f_attr
;
796 p
->o_res
.dir_attr
= &p
->dir_attr
;
797 p
->o_res
.seqid
= p
->o_arg
.seqid
;
798 p
->c_res
.seqid
= p
->c_arg
.seqid
;
799 p
->o_res
.server
= p
->o_arg
.server
;
800 nfs_fattr_init(&p
->f_attr
);
801 nfs_fattr_init(&p
->dir_attr
);
804 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
805 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
806 const struct iattr
*attrs
,
809 struct dentry
*parent
= dget_parent(path
->dentry
);
810 struct inode
*dir
= parent
->d_inode
;
811 struct nfs_server
*server
= NFS_SERVER(dir
);
812 struct nfs4_opendata
*p
;
814 p
= kzalloc(sizeof(*p
), gfp_mask
);
817 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
818 if (p
->o_arg
.seqid
== NULL
)
824 atomic_inc(&sp
->so_count
);
825 p
->o_arg
.fh
= NFS_FH(dir
);
826 p
->o_arg
.open_flags
= flags
;
827 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
828 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
829 p
->o_arg
.id
= sp
->so_owner_id
.id
;
830 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
831 p
->o_arg
.server
= server
;
832 p
->o_arg
.bitmask
= server
->attr_bitmask
;
833 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
834 if (flags
& O_CREAT
) {
837 p
->o_arg
.u
.attrs
= &p
->attrs
;
838 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
839 s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
843 p
->c_arg
.fh
= &p
->o_res
.fh
;
844 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
845 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
846 nfs4_init_opendata_res(p
);
856 static void nfs4_opendata_free(struct kref
*kref
)
858 struct nfs4_opendata
*p
= container_of(kref
,
859 struct nfs4_opendata
, kref
);
861 nfs_free_seqid(p
->o_arg
.seqid
);
862 if (p
->state
!= NULL
)
863 nfs4_put_open_state(p
->state
);
864 nfs4_put_state_owner(p
->owner
);
870 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
873 kref_put(&p
->kref
, nfs4_opendata_free
);
876 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
880 ret
= rpc_wait_for_completion_task(task
);
884 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
888 if (open_mode
& O_EXCL
)
890 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
892 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
893 && state
->n_rdonly
!= 0;
896 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
897 && state
->n_wronly
!= 0;
899 case FMODE_READ
|FMODE_WRITE
:
900 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
901 && state
->n_rdwr
!= 0;
907 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
909 if ((delegation
->type
& fmode
) != fmode
)
911 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
913 nfs_mark_delegation_referenced(delegation
);
917 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
926 case FMODE_READ
|FMODE_WRITE
:
929 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
932 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
934 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
935 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
936 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
939 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
942 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
944 case FMODE_READ
|FMODE_WRITE
:
945 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
949 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
951 write_seqlock(&state
->seqlock
);
952 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
953 write_sequnlock(&state
->seqlock
);
956 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
959 * Protect the call to nfs4_state_set_mode_locked and
960 * serialise the stateid update
962 write_seqlock(&state
->seqlock
);
963 if (deleg_stateid
!= NULL
) {
964 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
965 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
967 if (open_stateid
!= NULL
)
968 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
969 write_sequnlock(&state
->seqlock
);
970 spin_lock(&state
->owner
->so_lock
);
971 update_open_stateflags(state
, fmode
);
972 spin_unlock(&state
->owner
->so_lock
);
975 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
977 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
978 struct nfs_delegation
*deleg_cur
;
981 fmode
&= (FMODE_READ
|FMODE_WRITE
);
984 deleg_cur
= rcu_dereference(nfsi
->delegation
);
985 if (deleg_cur
== NULL
)
988 spin_lock(&deleg_cur
->lock
);
989 if (nfsi
->delegation
!= deleg_cur
||
990 (deleg_cur
->type
& fmode
) != fmode
)
991 goto no_delegation_unlock
;
993 if (delegation
== NULL
)
994 delegation
= &deleg_cur
->stateid
;
995 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
996 goto no_delegation_unlock
;
998 nfs_mark_delegation_referenced(deleg_cur
);
999 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
1001 no_delegation_unlock
:
1002 spin_unlock(&deleg_cur
->lock
);
1006 if (!ret
&& open_stateid
!= NULL
) {
1007 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
1015 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
1017 struct nfs_delegation
*delegation
;
1020 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1021 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
1026 nfs_inode_return_delegation(inode
);
1029 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
1031 struct nfs4_state
*state
= opendata
->state
;
1032 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1033 struct nfs_delegation
*delegation
;
1034 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
1035 fmode_t fmode
= opendata
->o_arg
.fmode
;
1036 nfs4_stateid stateid
;
1040 if (can_open_cached(state
, fmode
, open_mode
)) {
1041 spin_lock(&state
->owner
->so_lock
);
1042 if (can_open_cached(state
, fmode
, open_mode
)) {
1043 update_open_stateflags(state
, fmode
);
1044 spin_unlock(&state
->owner
->so_lock
);
1045 goto out_return_state
;
1047 spin_unlock(&state
->owner
->so_lock
);
1050 delegation
= rcu_dereference(nfsi
->delegation
);
1051 if (delegation
== NULL
||
1052 !can_open_delegated(delegation
, fmode
)) {
1056 /* Save the delegation */
1057 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
1059 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
1064 /* Try to update the stateid using the delegation */
1065 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1066 goto out_return_state
;
1069 return ERR_PTR(ret
);
1071 atomic_inc(&state
->count
);
1075 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1077 struct inode
*inode
;
1078 struct nfs4_state
*state
= NULL
;
1079 struct nfs_delegation
*delegation
;
1082 if (!data
->rpc_done
) {
1083 state
= nfs4_try_open_cached(data
);
1088 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1090 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
1091 ret
= PTR_ERR(inode
);
1095 state
= nfs4_get_open_state(inode
, data
->owner
);
1098 if (data
->o_res
.delegation_type
!= 0) {
1099 int delegation_flags
= 0;
1102 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1104 delegation_flags
= delegation
->flags
;
1106 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1107 nfs_inode_set_delegation(state
->inode
,
1108 data
->owner
->so_cred
,
1111 nfs_inode_reclaim_delegation(state
->inode
,
1112 data
->owner
->so_cred
,
1116 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1124 return ERR_PTR(ret
);
1127 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1129 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1130 struct nfs_open_context
*ctx
;
1132 spin_lock(&state
->inode
->i_lock
);
1133 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1134 if (ctx
->state
!= state
)
1136 get_nfs_open_context(ctx
);
1137 spin_unlock(&state
->inode
->i_lock
);
1140 spin_unlock(&state
->inode
->i_lock
);
1141 return ERR_PTR(-ENOENT
);
1144 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1146 struct nfs4_opendata
*opendata
;
1148 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
, GFP_NOFS
);
1149 if (opendata
== NULL
)
1150 return ERR_PTR(-ENOMEM
);
1151 opendata
->state
= state
;
1152 atomic_inc(&state
->count
);
1156 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1158 struct nfs4_state
*newstate
;
1161 opendata
->o_arg
.open_flags
= 0;
1162 opendata
->o_arg
.fmode
= fmode
;
1163 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1164 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1165 nfs4_init_opendata_res(opendata
);
1166 ret
= _nfs4_recover_proc_open(opendata
);
1169 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1170 if (IS_ERR(newstate
))
1171 return PTR_ERR(newstate
);
1172 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1177 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1179 struct nfs4_state
*newstate
;
1182 /* memory barrier prior to reading state->n_* */
1183 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1185 if (state
->n_rdwr
!= 0) {
1186 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1187 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1190 if (newstate
!= state
)
1193 if (state
->n_wronly
!= 0) {
1194 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1195 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1198 if (newstate
!= state
)
1201 if (state
->n_rdonly
!= 0) {
1202 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1203 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1206 if (newstate
!= state
)
1210 * We may have performed cached opens for all three recoveries.
1211 * Check if we need to update the current stateid.
1213 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1214 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1215 write_seqlock(&state
->seqlock
);
1216 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1217 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1218 write_sequnlock(&state
->seqlock
);
1225 * reclaim state on the server after a reboot.
1227 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1229 struct nfs_delegation
*delegation
;
1230 struct nfs4_opendata
*opendata
;
1231 fmode_t delegation_type
= 0;
1234 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1235 if (IS_ERR(opendata
))
1236 return PTR_ERR(opendata
);
1237 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1238 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1240 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1241 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1242 delegation_type
= delegation
->type
;
1244 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1245 status
= nfs4_open_recover(opendata
, state
);
1246 nfs4_opendata_put(opendata
);
1250 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1252 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1253 struct nfs4_exception exception
= { };
1256 err
= _nfs4_do_open_reclaim(ctx
, state
);
1257 if (err
!= -NFS4ERR_DELAY
)
1259 nfs4_handle_exception(server
, err
, &exception
);
1260 } while (exception
.retry
);
1264 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1266 struct nfs_open_context
*ctx
;
1269 ctx
= nfs4_state_find_open_context(state
);
1271 return PTR_ERR(ctx
);
1272 ret
= nfs4_do_open_reclaim(ctx
, state
);
1273 put_nfs_open_context(ctx
);
1277 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1279 struct nfs4_opendata
*opendata
;
1282 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1283 if (IS_ERR(opendata
))
1284 return PTR_ERR(opendata
);
1285 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1286 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1287 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1288 ret
= nfs4_open_recover(opendata
, state
);
1289 nfs4_opendata_put(opendata
);
1293 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1295 struct nfs4_exception exception
= { };
1296 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1299 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1305 case -NFS4ERR_BADSESSION
:
1306 case -NFS4ERR_BADSLOT
:
1307 case -NFS4ERR_BAD_HIGH_SLOT
:
1308 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1309 case -NFS4ERR_DEADSESSION
:
1310 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
);
1312 case -NFS4ERR_STALE_CLIENTID
:
1313 case -NFS4ERR_STALE_STATEID
:
1314 case -NFS4ERR_EXPIRED
:
1315 /* Don't recall a delegation if it was lost */
1316 nfs4_schedule_lease_recovery(server
->nfs_client
);
1320 * The show must go on: exit, but mark the
1321 * stateid as needing recovery.
1323 case -NFS4ERR_DELEG_REVOKED
:
1324 case -NFS4ERR_ADMIN_REVOKED
:
1325 case -NFS4ERR_BAD_STATEID
:
1326 nfs_inode_find_state_and_recover(state
->inode
,
1328 nfs4_schedule_stateid_recovery(server
, state
);
1331 * User RPCSEC_GSS context has expired.
1332 * We cannot recover this stateid now, so
1333 * skip it and allow recovery thread to
1340 err
= nfs4_handle_exception(server
, err
, &exception
);
1341 } while (exception
.retry
);
1346 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1348 struct nfs4_opendata
*data
= calldata
;
1350 data
->rpc_status
= task
->tk_status
;
1351 if (data
->rpc_status
== 0) {
1352 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1353 sizeof(data
->o_res
.stateid
.data
));
1354 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1355 renew_lease(data
->o_res
.server
, data
->timestamp
);
1360 static void nfs4_open_confirm_release(void *calldata
)
1362 struct nfs4_opendata
*data
= calldata
;
1363 struct nfs4_state
*state
= NULL
;
1365 /* If this request hasn't been cancelled, do nothing */
1366 if (data
->cancelled
== 0)
1368 /* In case of error, no cleanup! */
1369 if (!data
->rpc_done
)
1371 state
= nfs4_opendata_to_nfs4_state(data
);
1373 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1375 nfs4_opendata_put(data
);
1378 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1379 .rpc_call_done
= nfs4_open_confirm_done
,
1380 .rpc_release
= nfs4_open_confirm_release
,
1384 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1386 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1388 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1389 struct rpc_task
*task
;
1390 struct rpc_message msg
= {
1391 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1392 .rpc_argp
= &data
->c_arg
,
1393 .rpc_resp
= &data
->c_res
,
1394 .rpc_cred
= data
->owner
->so_cred
,
1396 struct rpc_task_setup task_setup_data
= {
1397 .rpc_client
= server
->client
,
1398 .rpc_message
= &msg
,
1399 .callback_ops
= &nfs4_open_confirm_ops
,
1400 .callback_data
= data
,
1401 .workqueue
= nfsiod_workqueue
,
1402 .flags
= RPC_TASK_ASYNC
,
1406 kref_get(&data
->kref
);
1408 data
->rpc_status
= 0;
1409 data
->timestamp
= jiffies
;
1410 task
= rpc_run_task(&task_setup_data
);
1412 return PTR_ERR(task
);
1413 status
= nfs4_wait_for_completion_rpc_task(task
);
1415 data
->cancelled
= 1;
1418 status
= data
->rpc_status
;
1423 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1425 struct nfs4_opendata
*data
= calldata
;
1426 struct nfs4_state_owner
*sp
= data
->owner
;
1428 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1431 * Check if we still need to send an OPEN call, or if we can use
1432 * a delegation instead.
1434 if (data
->state
!= NULL
) {
1435 struct nfs_delegation
*delegation
;
1437 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1440 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1441 if (delegation
!= NULL
&&
1442 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1448 /* Update sequence id. */
1449 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1450 data
->o_arg
.clientid
= sp
->so_server
->nfs_client
->cl_clientid
;
1451 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1452 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1453 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1455 data
->timestamp
= jiffies
;
1456 if (nfs4_setup_sequence(data
->o_arg
.server
,
1457 &data
->o_arg
.seq_args
,
1458 &data
->o_res
.seq_res
, 1, task
))
1460 rpc_call_start(task
);
1463 task
->tk_action
= NULL
;
1467 static void nfs4_recover_open_prepare(struct rpc_task
*task
, void *calldata
)
1469 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
1470 nfs4_open_prepare(task
, calldata
);
1473 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1475 struct nfs4_opendata
*data
= calldata
;
1477 data
->rpc_status
= task
->tk_status
;
1479 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1482 if (task
->tk_status
== 0) {
1483 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1487 data
->rpc_status
= -ELOOP
;
1490 data
->rpc_status
= -EISDIR
;
1493 data
->rpc_status
= -ENOTDIR
;
1495 renew_lease(data
->o_res
.server
, data
->timestamp
);
1496 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1497 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1502 static void nfs4_open_release(void *calldata
)
1504 struct nfs4_opendata
*data
= calldata
;
1505 struct nfs4_state
*state
= NULL
;
1507 /* If this request hasn't been cancelled, do nothing */
1508 if (data
->cancelled
== 0)
1510 /* In case of error, no cleanup! */
1511 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1513 /* In case we need an open_confirm, no cleanup! */
1514 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1516 state
= nfs4_opendata_to_nfs4_state(data
);
1518 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1520 nfs4_opendata_put(data
);
1523 static const struct rpc_call_ops nfs4_open_ops
= {
1524 .rpc_call_prepare
= nfs4_open_prepare
,
1525 .rpc_call_done
= nfs4_open_done
,
1526 .rpc_release
= nfs4_open_release
,
1529 static const struct rpc_call_ops nfs4_recover_open_ops
= {
1530 .rpc_call_prepare
= nfs4_recover_open_prepare
,
1531 .rpc_call_done
= nfs4_open_done
,
1532 .rpc_release
= nfs4_open_release
,
1535 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1537 struct inode
*dir
= data
->dir
->d_inode
;
1538 struct nfs_server
*server
= NFS_SERVER(dir
);
1539 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1540 struct nfs_openres
*o_res
= &data
->o_res
;
1541 struct rpc_task
*task
;
1542 struct rpc_message msg
= {
1543 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1546 .rpc_cred
= data
->owner
->so_cred
,
1548 struct rpc_task_setup task_setup_data
= {
1549 .rpc_client
= server
->client
,
1550 .rpc_message
= &msg
,
1551 .callback_ops
= &nfs4_open_ops
,
1552 .callback_data
= data
,
1553 .workqueue
= nfsiod_workqueue
,
1554 .flags
= RPC_TASK_ASYNC
,
1558 kref_get(&data
->kref
);
1560 data
->rpc_status
= 0;
1561 data
->cancelled
= 0;
1563 task_setup_data
.callback_ops
= &nfs4_recover_open_ops
;
1564 task
= rpc_run_task(&task_setup_data
);
1566 return PTR_ERR(task
);
1567 status
= nfs4_wait_for_completion_rpc_task(task
);
1569 data
->cancelled
= 1;
1572 status
= data
->rpc_status
;
1578 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1580 struct inode
*dir
= data
->dir
->d_inode
;
1581 struct nfs_openres
*o_res
= &data
->o_res
;
1584 status
= nfs4_run_open_task(data
, 1);
1585 if (status
!= 0 || !data
->rpc_done
)
1588 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1590 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1591 status
= _nfs4_proc_open_confirm(data
);
1600 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1602 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1604 struct inode
*dir
= data
->dir
->d_inode
;
1605 struct nfs_server
*server
= NFS_SERVER(dir
);
1606 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1607 struct nfs_openres
*o_res
= &data
->o_res
;
1610 status
= nfs4_run_open_task(data
, 0);
1611 if (status
!= 0 || !data
->rpc_done
)
1614 if (o_arg
->open_flags
& O_CREAT
) {
1615 update_changeattr(dir
, &o_res
->cinfo
);
1616 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1618 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1619 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1620 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1621 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1622 status
= _nfs4_proc_open_confirm(data
);
1626 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1627 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1631 static int nfs4_client_recover_expired_lease(struct nfs_client
*clp
)
1636 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1637 ret
= nfs4_wait_clnt_recover(clp
);
1640 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1641 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1643 nfs4_schedule_state_manager(clp
);
1649 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1651 return nfs4_client_recover_expired_lease(server
->nfs_client
);
1656 * reclaim state on the server after a network partition.
1657 * Assumes caller holds the appropriate lock
1659 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1661 struct nfs4_opendata
*opendata
;
1664 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1665 if (IS_ERR(opendata
))
1666 return PTR_ERR(opendata
);
1667 ret
= nfs4_open_recover(opendata
, state
);
1669 d_drop(ctx
->path
.dentry
);
1670 nfs4_opendata_put(opendata
);
1674 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1676 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1677 struct nfs4_exception exception
= { };
1681 err
= _nfs4_open_expired(ctx
, state
);
1685 case -NFS4ERR_GRACE
:
1686 case -NFS4ERR_DELAY
:
1687 nfs4_handle_exception(server
, err
, &exception
);
1690 } while (exception
.retry
);
1695 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1697 struct nfs_open_context
*ctx
;
1700 ctx
= nfs4_state_find_open_context(state
);
1702 return PTR_ERR(ctx
);
1703 ret
= nfs4_do_open_expired(ctx
, state
);
1704 put_nfs_open_context(ctx
);
1709 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1710 * fields corresponding to attributes that were used to store the verifier.
1711 * Make sure we clobber those fields in the later setattr call
1713 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1715 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1716 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1717 sattr
->ia_valid
|= ATTR_ATIME
;
1719 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1720 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1721 sattr
->ia_valid
|= ATTR_MTIME
;
1725 * Returns a referenced nfs4_state
1727 static int _nfs4_do_open(struct inode
*dir
, struct path
*path
, fmode_t fmode
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
, struct nfs4_state
**res
)
1729 struct nfs4_state_owner
*sp
;
1730 struct nfs4_state
*state
= NULL
;
1731 struct nfs_server
*server
= NFS_SERVER(dir
);
1732 struct nfs4_opendata
*opendata
;
1735 /* Protect against reboot recovery conflicts */
1737 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1738 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1741 status
= nfs4_recover_expired_lease(server
);
1743 goto err_put_state_owner
;
1744 if (path
->dentry
->d_inode
!= NULL
)
1745 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1747 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
, GFP_KERNEL
);
1748 if (opendata
== NULL
)
1749 goto err_put_state_owner
;
1751 if (path
->dentry
->d_inode
!= NULL
)
1752 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1754 status
= _nfs4_proc_open(opendata
);
1756 goto err_opendata_put
;
1758 state
= nfs4_opendata_to_nfs4_state(opendata
);
1759 status
= PTR_ERR(state
);
1761 goto err_opendata_put
;
1762 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
1763 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1765 if (opendata
->o_arg
.open_flags
& O_EXCL
) {
1766 nfs4_exclusive_attrset(opendata
, sattr
);
1768 nfs_fattr_init(opendata
->o_res
.f_attr
);
1769 status
= nfs4_do_setattr(state
->inode
, cred
,
1770 opendata
->o_res
.f_attr
, sattr
,
1773 nfs_setattr_update_inode(state
->inode
, sattr
);
1774 nfs_post_op_update_inode(state
->inode
, opendata
->o_res
.f_attr
);
1776 nfs_revalidate_inode(server
, state
->inode
);
1777 nfs4_opendata_put(opendata
);
1778 nfs4_put_state_owner(sp
);
1782 nfs4_opendata_put(opendata
);
1783 err_put_state_owner
:
1784 nfs4_put_state_owner(sp
);
1791 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
, struct path
*path
, fmode_t fmode
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
)
1793 struct nfs4_exception exception
= { };
1794 struct nfs4_state
*res
;
1798 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1801 /* NOTE: BAD_SEQID means the server and client disagree about the
1802 * book-keeping w.r.t. state-changing operations
1803 * (OPEN/CLOSE/LOCK/LOCKU...)
1804 * It is actually a sign of a bug on the client or on the server.
1806 * If we receive a BAD_SEQID error in the particular case of
1807 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1808 * have unhashed the old state_owner for us, and that we can
1809 * therefore safely retry using a new one. We should still warn
1810 * the user though...
1812 if (status
== -NFS4ERR_BAD_SEQID
) {
1813 printk(KERN_WARNING
"NFS: v4 server %s "
1814 " returned a bad sequence-id error!\n",
1815 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1816 exception
.retry
= 1;
1820 * BAD_STATEID on OPEN means that the server cancelled our
1821 * state before it received the OPEN_CONFIRM.
1822 * Recover by retrying the request as per the discussion
1823 * on Page 181 of RFC3530.
1825 if (status
== -NFS4ERR_BAD_STATEID
) {
1826 exception
.retry
= 1;
1829 if (status
== -EAGAIN
) {
1830 /* We must have found a delegation */
1831 exception
.retry
= 1;
1834 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1835 status
, &exception
));
1836 } while (exception
.retry
);
1840 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1841 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1842 struct nfs4_state
*state
)
1844 struct nfs_server
*server
= NFS_SERVER(inode
);
1845 struct nfs_setattrargs arg
= {
1846 .fh
= NFS_FH(inode
),
1849 .bitmask
= server
->attr_bitmask
,
1851 struct nfs_setattrres res
= {
1855 struct rpc_message msg
= {
1856 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1861 unsigned long timestamp
= jiffies
;
1864 nfs_fattr_init(fattr
);
1866 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1867 /* Use that stateid */
1868 } else if (state
!= NULL
) {
1869 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
, current
->tgid
);
1871 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1873 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
1874 if (status
== 0 && state
!= NULL
)
1875 renew_lease(server
, timestamp
);
1879 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1880 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1881 struct nfs4_state
*state
)
1883 struct nfs_server
*server
= NFS_SERVER(inode
);
1884 struct nfs4_exception exception
= {
1890 err
= nfs4_handle_exception(server
,
1891 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1893 } while (exception
.retry
);
1897 struct nfs4_closedata
{
1899 struct inode
*inode
;
1900 struct nfs4_state
*state
;
1901 struct nfs_closeargs arg
;
1902 struct nfs_closeres res
;
1903 struct nfs_fattr fattr
;
1904 unsigned long timestamp
;
1909 static void nfs4_free_closedata(void *data
)
1911 struct nfs4_closedata
*calldata
= data
;
1912 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1915 pnfs_roc_release(calldata
->state
->inode
);
1916 nfs4_put_open_state(calldata
->state
);
1917 nfs_free_seqid(calldata
->arg
.seqid
);
1918 nfs4_put_state_owner(sp
);
1919 path_put(&calldata
->path
);
1923 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
1926 spin_lock(&state
->owner
->so_lock
);
1927 if (!(fmode
& FMODE_READ
))
1928 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1929 if (!(fmode
& FMODE_WRITE
))
1930 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1931 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1932 spin_unlock(&state
->owner
->so_lock
);
1935 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1937 struct nfs4_closedata
*calldata
= data
;
1938 struct nfs4_state
*state
= calldata
->state
;
1939 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1941 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
1943 /* hmm. we are done with the inode, and in the process of freeing
1944 * the state_owner. we keep this around to process errors
1946 switch (task
->tk_status
) {
1949 pnfs_roc_set_barrier(state
->inode
,
1950 calldata
->roc_barrier
);
1951 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1952 renew_lease(server
, calldata
->timestamp
);
1953 nfs4_close_clear_stateid_flags(state
,
1954 calldata
->arg
.fmode
);
1956 case -NFS4ERR_STALE_STATEID
:
1957 case -NFS4ERR_OLD_STATEID
:
1958 case -NFS4ERR_BAD_STATEID
:
1959 case -NFS4ERR_EXPIRED
:
1960 if (calldata
->arg
.fmode
== 0)
1963 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
1964 rpc_restart_call_prepare(task
);
1966 nfs_release_seqid(calldata
->arg
.seqid
);
1967 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1970 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1972 struct nfs4_closedata
*calldata
= data
;
1973 struct nfs4_state
*state
= calldata
->state
;
1976 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1979 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1980 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
1981 spin_lock(&state
->owner
->so_lock
);
1982 /* Calculate the change in open mode */
1983 if (state
->n_rdwr
== 0) {
1984 if (state
->n_rdonly
== 0) {
1985 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1986 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1987 calldata
->arg
.fmode
&= ~FMODE_READ
;
1989 if (state
->n_wronly
== 0) {
1990 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1991 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1992 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
1995 spin_unlock(&state
->owner
->so_lock
);
1998 /* Note: exit _without_ calling nfs4_close_done */
1999 task
->tk_action
= NULL
;
2003 if (calldata
->arg
.fmode
== 0) {
2004 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
2005 if (calldata
->roc
&&
2006 pnfs_roc_drain(calldata
->inode
, &calldata
->roc_barrier
)) {
2007 rpc_sleep_on(&NFS_SERVER(calldata
->inode
)->roc_rpcwaitq
,
2013 nfs_fattr_init(calldata
->res
.fattr
);
2014 calldata
->timestamp
= jiffies
;
2015 if (nfs4_setup_sequence(NFS_SERVER(calldata
->inode
),
2016 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
2019 rpc_call_start(task
);
2022 static const struct rpc_call_ops nfs4_close_ops
= {
2023 .rpc_call_prepare
= nfs4_close_prepare
,
2024 .rpc_call_done
= nfs4_close_done
,
2025 .rpc_release
= nfs4_free_closedata
,
2029 * It is possible for data to be read/written from a mem-mapped file
2030 * after the sys_close call (which hits the vfs layer as a flush).
2031 * This means that we can't safely call nfsv4 close on a file until
2032 * the inode is cleared. This in turn means that we are not good
2033 * NFSv4 citizens - we do not indicate to the server to update the file's
2034 * share state even when we are done with one of the three share
2035 * stateid's in the inode.
2037 * NOTE: Caller must be holding the sp->so_owner semaphore!
2039 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
, bool roc
)
2041 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2042 struct nfs4_closedata
*calldata
;
2043 struct nfs4_state_owner
*sp
= state
->owner
;
2044 struct rpc_task
*task
;
2045 struct rpc_message msg
= {
2046 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
2047 .rpc_cred
= state
->owner
->so_cred
,
2049 struct rpc_task_setup task_setup_data
= {
2050 .rpc_client
= server
->client
,
2051 .rpc_message
= &msg
,
2052 .callback_ops
= &nfs4_close_ops
,
2053 .workqueue
= nfsiod_workqueue
,
2054 .flags
= RPC_TASK_ASYNC
,
2056 int status
= -ENOMEM
;
2058 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
2059 if (calldata
== NULL
)
2061 calldata
->inode
= state
->inode
;
2062 calldata
->state
= state
;
2063 calldata
->arg
.fh
= NFS_FH(state
->inode
);
2064 calldata
->arg
.stateid
= &state
->open_stateid
;
2065 /* Serialization for the sequence id */
2066 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
2067 if (calldata
->arg
.seqid
== NULL
)
2068 goto out_free_calldata
;
2069 calldata
->arg
.fmode
= 0;
2070 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
2071 calldata
->res
.fattr
= &calldata
->fattr
;
2072 calldata
->res
.seqid
= calldata
->arg
.seqid
;
2073 calldata
->res
.server
= server
;
2074 calldata
->roc
= roc
;
2076 calldata
->path
= *path
;
2078 msg
.rpc_argp
= &calldata
->arg
;
2079 msg
.rpc_resp
= &calldata
->res
;
2080 task_setup_data
.callback_data
= calldata
;
2081 task
= rpc_run_task(&task_setup_data
);
2083 return PTR_ERR(task
);
2086 status
= rpc_wait_for_completion_task(task
);
2093 pnfs_roc_release(state
->inode
);
2094 nfs4_put_open_state(state
);
2095 nfs4_put_state_owner(sp
);
2099 static struct inode
*
2100 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
, int open_flags
, struct iattr
*attr
)
2102 struct nfs4_state
*state
;
2104 /* Protect against concurrent sillydeletes */
2105 state
= nfs4_do_open(dir
, &ctx
->path
, ctx
->mode
, open_flags
, attr
, ctx
->cred
);
2107 return ERR_CAST(state
);
2109 return igrab(state
->inode
);
2112 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2114 if (ctx
->state
== NULL
)
2117 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2119 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2122 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2124 struct nfs4_server_caps_arg args
= {
2127 struct nfs4_server_caps_res res
= {};
2128 struct rpc_message msg
= {
2129 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2135 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2137 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2138 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2139 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2140 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2141 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2142 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2143 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2144 server
->caps
|= NFS_CAP_ACLS
;
2145 if (res
.has_links
!= 0)
2146 server
->caps
|= NFS_CAP_HARDLINKS
;
2147 if (res
.has_symlinks
!= 0)
2148 server
->caps
|= NFS_CAP_SYMLINKS
;
2149 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2150 server
->caps
|= NFS_CAP_FILEID
;
2151 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2152 server
->caps
|= NFS_CAP_MODE
;
2153 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2154 server
->caps
|= NFS_CAP_NLINK
;
2155 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2156 server
->caps
|= NFS_CAP_OWNER
;
2157 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2158 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2159 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2160 server
->caps
|= NFS_CAP_ATIME
;
2161 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2162 server
->caps
|= NFS_CAP_CTIME
;
2163 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2164 server
->caps
|= NFS_CAP_MTIME
;
2166 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2167 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2168 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2169 server
->acl_bitmask
= res
.acl_bitmask
;
2175 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2177 struct nfs4_exception exception
= { };
2180 err
= nfs4_handle_exception(server
,
2181 _nfs4_server_capabilities(server
, fhandle
),
2183 } while (exception
.retry
);
2187 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2188 struct nfs_fsinfo
*info
)
2190 struct nfs4_lookup_root_arg args
= {
2191 .bitmask
= nfs4_fattr_bitmap
,
2193 struct nfs4_lookup_res res
= {
2195 .fattr
= info
->fattr
,
2198 struct rpc_message msg
= {
2199 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2204 nfs_fattr_init(info
->fattr
);
2205 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2208 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2209 struct nfs_fsinfo
*info
)
2211 struct nfs4_exception exception
= { };
2214 err
= _nfs4_lookup_root(server
, fhandle
, info
);
2217 case -NFS4ERR_WRONGSEC
:
2220 err
= nfs4_handle_exception(server
, err
, &exception
);
2222 } while (exception
.retry
);
2226 static int nfs4_lookup_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2227 struct nfs_fsinfo
*info
, rpc_authflavor_t flavor
)
2229 struct rpc_auth
*auth
;
2232 auth
= rpcauth_create(flavor
, server
->client
);
2237 ret
= nfs4_lookup_root(server
, fhandle
, info
);
2242 static int nfs4_find_root_sec(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2243 struct nfs_fsinfo
*info
)
2245 int i
, len
, status
= 0;
2246 rpc_authflavor_t flav_array
[NFS_MAX_SECFLAVORS
];
2248 len
= gss_mech_list_pseudoflavors(&flav_array
[0]);
2249 flav_array
[len
] = RPC_AUTH_NULL
;
2252 for (i
= 0; i
< len
; i
++) {
2253 status
= nfs4_lookup_root_sec(server
, fhandle
, info
, flav_array
[i
]);
2254 if (status
== -NFS4ERR_WRONGSEC
|| status
== -EACCES
)
2259 * -EACCESS could mean that the user doesn't have correct permissions
2260 * to access the mount. It could also mean that we tried to mount
2261 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2262 * existing mount programs don't handle -EACCES very well so it should
2263 * be mapped to -EPERM instead.
2265 if (status
== -EACCES
)
2271 * get the file handle for the "/" directory on the server
2273 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2274 struct nfs_fsinfo
*info
)
2276 int status
= nfs4_lookup_root(server
, fhandle
, info
);
2277 if ((status
== -NFS4ERR_WRONGSEC
) && !(server
->flags
& NFS_MOUNT_SECFLAVOUR
))
2279 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2280 * by nfs4_map_errors() as this function exits.
2282 status
= nfs4_find_root_sec(server
, fhandle
, info
);
2284 status
= nfs4_server_capabilities(server
, fhandle
);
2286 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2287 return nfs4_map_errors(status
);
2290 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
);
2292 * Get locations and (maybe) other attributes of a referral.
2293 * Note that we'll actually follow the referral later when
2294 * we detect fsid mismatch in inode revalidation
2296 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
,
2297 struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2299 int status
= -ENOMEM
;
2300 struct page
*page
= NULL
;
2301 struct nfs4_fs_locations
*locations
= NULL
;
2303 page
= alloc_page(GFP_KERNEL
);
2306 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2307 if (locations
== NULL
)
2310 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2313 /* Make sure server returned a different fsid for the referral */
2314 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2315 dprintk("%s: server did not return a different fsid for"
2316 " a referral at %s\n", __func__
, name
->name
);
2320 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2321 nfs_fixup_referral_attributes(&locations
->fattr
);
2323 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2324 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2325 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2333 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2335 struct nfs4_getattr_arg args
= {
2337 .bitmask
= server
->attr_bitmask
,
2339 struct nfs4_getattr_res res
= {
2343 struct rpc_message msg
= {
2344 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2349 nfs_fattr_init(fattr
);
2350 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2353 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2355 struct nfs4_exception exception
= { };
2358 err
= nfs4_handle_exception(server
,
2359 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2361 } while (exception
.retry
);
2366 * The file is not closed if it is opened due to the a request to change
2367 * the size of the file. The open call will not be needed once the
2368 * VFS layer lookup-intents are implemented.
2370 * Close is called when the inode is destroyed.
2371 * If we haven't opened the file for O_WRONLY, we
2372 * need to in the size_change case to obtain a stateid.
2375 * Because OPEN is always done by name in nfsv4, it is
2376 * possible that we opened a different file by the same
2377 * name. We can recognize this race condition, but we
2378 * can't do anything about it besides returning an error.
2380 * This will be fixed with VFS changes (lookup-intent).
2383 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2384 struct iattr
*sattr
)
2386 struct inode
*inode
= dentry
->d_inode
;
2387 struct rpc_cred
*cred
= NULL
;
2388 struct nfs4_state
*state
= NULL
;
2391 if (pnfs_ld_layoutret_on_setattr(inode
))
2392 pnfs_return_layout(inode
);
2394 nfs_fattr_init(fattr
);
2396 /* Search for an existing open(O_WRITE) file */
2397 if (sattr
->ia_valid
& ATTR_FILE
) {
2398 struct nfs_open_context
*ctx
;
2400 ctx
= nfs_file_open_context(sattr
->ia_file
);
2407 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2409 nfs_setattr_update_inode(inode
, sattr
);
2413 static int _nfs4_proc_lookupfh(struct rpc_clnt
*clnt
, struct nfs_server
*server
,
2414 const struct nfs_fh
*dirfh
, const struct qstr
*name
,
2415 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2418 struct nfs4_lookup_arg args
= {
2419 .bitmask
= server
->attr_bitmask
,
2423 struct nfs4_lookup_res res
= {
2428 struct rpc_message msg
= {
2429 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2434 nfs_fattr_init(fattr
);
2436 dprintk("NFS call lookupfh %s\n", name
->name
);
2437 status
= nfs4_call_sync(clnt
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2438 dprintk("NFS reply lookupfh: %d\n", status
);
2442 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2443 struct qstr
*name
, struct nfs_fh
*fhandle
,
2444 struct nfs_fattr
*fattr
)
2446 struct nfs4_exception exception
= { };
2449 err
= _nfs4_proc_lookupfh(server
->client
, server
, dirfh
, name
, fhandle
, fattr
);
2451 if (err
== -NFS4ERR_MOVED
) {
2455 err
= nfs4_handle_exception(server
, err
, &exception
);
2456 } while (exception
.retry
);
2460 static int _nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
,
2461 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2462 struct nfs_fattr
*fattr
)
2466 dprintk("NFS call lookup %s\n", name
->name
);
2467 status
= _nfs4_proc_lookupfh(clnt
, NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2468 if (status
== -NFS4ERR_MOVED
)
2469 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2470 dprintk("NFS reply lookup: %d\n", status
);
2474 void nfs_fixup_secinfo_attributes(struct nfs_fattr
*fattr
, struct nfs_fh
*fh
)
2476 memset(fh
, 0, sizeof(struct nfs_fh
));
2477 fattr
->fsid
.major
= 1;
2478 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
2479 NFS_ATTR_FATTR_NLINK
| NFS_ATTR_FATTR_FSID
| NFS_ATTR_FATTR_MOUNTPOINT
;
2480 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
2484 static int nfs4_proc_lookup(struct rpc_clnt
*clnt
, struct inode
*dir
, struct qstr
*name
,
2485 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2487 struct nfs4_exception exception
= { };
2490 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2491 _nfs4_proc_lookup(clnt
, dir
, name
, fhandle
, fattr
),
2494 nfs_fixup_secinfo_attributes(fattr
, fhandle
);
2495 } while (exception
.retry
);
2499 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2501 struct nfs_server
*server
= NFS_SERVER(inode
);
2502 struct nfs4_accessargs args
= {
2503 .fh
= NFS_FH(inode
),
2504 .bitmask
= server
->attr_bitmask
,
2506 struct nfs4_accessres res
= {
2509 struct rpc_message msg
= {
2510 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2513 .rpc_cred
= entry
->cred
,
2515 int mode
= entry
->mask
;
2519 * Determine which access bits we want to ask for...
2521 if (mode
& MAY_READ
)
2522 args
.access
|= NFS4_ACCESS_READ
;
2523 if (S_ISDIR(inode
->i_mode
)) {
2524 if (mode
& MAY_WRITE
)
2525 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2526 if (mode
& MAY_EXEC
)
2527 args
.access
|= NFS4_ACCESS_LOOKUP
;
2529 if (mode
& MAY_WRITE
)
2530 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2531 if (mode
& MAY_EXEC
)
2532 args
.access
|= NFS4_ACCESS_EXECUTE
;
2535 res
.fattr
= nfs_alloc_fattr();
2536 if (res
.fattr
== NULL
)
2539 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2542 if (res
.access
& NFS4_ACCESS_READ
)
2543 entry
->mask
|= MAY_READ
;
2544 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2545 entry
->mask
|= MAY_WRITE
;
2546 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2547 entry
->mask
|= MAY_EXEC
;
2548 nfs_refresh_inode(inode
, res
.fattr
);
2550 nfs_free_fattr(res
.fattr
);
2554 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2556 struct nfs4_exception exception
= { };
2559 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2560 _nfs4_proc_access(inode
, entry
),
2562 } while (exception
.retry
);
2567 * TODO: For the time being, we don't try to get any attributes
2568 * along with any of the zero-copy operations READ, READDIR,
2571 * In the case of the first three, we want to put the GETATTR
2572 * after the read-type operation -- this is because it is hard
2573 * to predict the length of a GETATTR response in v4, and thus
2574 * align the READ data correctly. This means that the GETATTR
2575 * may end up partially falling into the page cache, and we should
2576 * shift it into the 'tail' of the xdr_buf before processing.
2577 * To do this efficiently, we need to know the total length
2578 * of data received, which doesn't seem to be available outside
2581 * In the case of WRITE, we also want to put the GETATTR after
2582 * the operation -- in this case because we want to make sure
2583 * we get the post-operation mtime and size. This means that
2584 * we can't use xdr_encode_pages() as written: we need a variant
2585 * of it which would leave room in the 'tail' iovec.
2587 * Both of these changes to the XDR layer would in fact be quite
2588 * minor, but I decided to leave them for a subsequent patch.
2590 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2591 unsigned int pgbase
, unsigned int pglen
)
2593 struct nfs4_readlink args
= {
2594 .fh
= NFS_FH(inode
),
2599 struct nfs4_readlink_res res
;
2600 struct rpc_message msg
= {
2601 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2606 return nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
2609 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2610 unsigned int pgbase
, unsigned int pglen
)
2612 struct nfs4_exception exception
= { };
2615 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2616 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2618 } while (exception
.retry
);
2624 * We will need to arrange for the VFS layer to provide an atomic open.
2625 * Until then, this create/open method is prone to inefficiency and race
2626 * conditions due to the lookup, create, and open VFS calls from sys_open()
2627 * placed on the wire.
2629 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2630 * The file will be opened again in the subsequent VFS open call
2631 * (nfs4_proc_file_open).
2633 * The open for read will just hang around to be used by any process that
2634 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2638 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2639 int flags
, struct nfs_open_context
*ctx
)
2641 struct path my_path
= {
2644 struct path
*path
= &my_path
;
2645 struct nfs4_state
*state
;
2646 struct rpc_cred
*cred
= NULL
;
2655 sattr
->ia_mode
&= ~current_umask();
2656 state
= nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
);
2658 if (IS_ERR(state
)) {
2659 status
= PTR_ERR(state
);
2662 d_add(dentry
, igrab(state
->inode
));
2663 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2667 nfs4_close_sync(path
, state
, fmode
);
2672 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2674 struct nfs_server
*server
= NFS_SERVER(dir
);
2675 struct nfs_removeargs args
= {
2677 .name
.len
= name
->len
,
2678 .name
.name
= name
->name
,
2679 .bitmask
= server
->attr_bitmask
,
2681 struct nfs_removeres res
= {
2684 struct rpc_message msg
= {
2685 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2689 int status
= -ENOMEM
;
2691 res
.dir_attr
= nfs_alloc_fattr();
2692 if (res
.dir_attr
== NULL
)
2695 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 1);
2697 update_changeattr(dir
, &res
.cinfo
);
2698 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2700 nfs_free_fattr(res
.dir_attr
);
2705 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2707 struct nfs4_exception exception
= { };
2710 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2711 _nfs4_proc_remove(dir
, name
),
2713 } while (exception
.retry
);
2717 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2719 struct nfs_server
*server
= NFS_SERVER(dir
);
2720 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2721 struct nfs_removeres
*res
= msg
->rpc_resp
;
2723 args
->bitmask
= server
->cache_consistency_bitmask
;
2724 res
->server
= server
;
2725 res
->seq_res
.sr_slot
= NULL
;
2726 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2729 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2731 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2733 if (!nfs4_sequence_done(task
, &res
->seq_res
))
2735 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2737 update_changeattr(dir
, &res
->cinfo
);
2738 nfs_post_op_update_inode(dir
, res
->dir_attr
);
2742 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
2744 struct nfs_server
*server
= NFS_SERVER(dir
);
2745 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
2746 struct nfs_renameres
*res
= msg
->rpc_resp
;
2748 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
2749 arg
->bitmask
= server
->attr_bitmask
;
2750 res
->server
= server
;
2753 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
2754 struct inode
*new_dir
)
2756 struct nfs_renameres
*res
= task
->tk_msg
.rpc_resp
;
2758 if (!nfs4_sequence_done(task
, &res
->seq_res
))
2760 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2763 update_changeattr(old_dir
, &res
->old_cinfo
);
2764 nfs_post_op_update_inode(old_dir
, res
->old_fattr
);
2765 update_changeattr(new_dir
, &res
->new_cinfo
);
2766 nfs_post_op_update_inode(new_dir
, res
->new_fattr
);
2770 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2771 struct inode
*new_dir
, struct qstr
*new_name
)
2773 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2774 struct nfs_renameargs arg
= {
2775 .old_dir
= NFS_FH(old_dir
),
2776 .new_dir
= NFS_FH(new_dir
),
2777 .old_name
= old_name
,
2778 .new_name
= new_name
,
2779 .bitmask
= server
->attr_bitmask
,
2781 struct nfs_renameres res
= {
2784 struct rpc_message msg
= {
2785 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2789 int status
= -ENOMEM
;
2791 res
.old_fattr
= nfs_alloc_fattr();
2792 res
.new_fattr
= nfs_alloc_fattr();
2793 if (res
.old_fattr
== NULL
|| res
.new_fattr
== NULL
)
2796 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2798 update_changeattr(old_dir
, &res
.old_cinfo
);
2799 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2800 update_changeattr(new_dir
, &res
.new_cinfo
);
2801 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2804 nfs_free_fattr(res
.new_fattr
);
2805 nfs_free_fattr(res
.old_fattr
);
2809 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2810 struct inode
*new_dir
, struct qstr
*new_name
)
2812 struct nfs4_exception exception
= { };
2815 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2816 _nfs4_proc_rename(old_dir
, old_name
,
2819 } while (exception
.retry
);
2823 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2825 struct nfs_server
*server
= NFS_SERVER(inode
);
2826 struct nfs4_link_arg arg
= {
2827 .fh
= NFS_FH(inode
),
2828 .dir_fh
= NFS_FH(dir
),
2830 .bitmask
= server
->attr_bitmask
,
2832 struct nfs4_link_res res
= {
2835 struct rpc_message msg
= {
2836 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2840 int status
= -ENOMEM
;
2842 res
.fattr
= nfs_alloc_fattr();
2843 res
.dir_attr
= nfs_alloc_fattr();
2844 if (res
.fattr
== NULL
|| res
.dir_attr
== NULL
)
2847 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
2849 update_changeattr(dir
, &res
.cinfo
);
2850 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2851 nfs_post_op_update_inode(inode
, res
.fattr
);
2854 nfs_free_fattr(res
.dir_attr
);
2855 nfs_free_fattr(res
.fattr
);
2859 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2861 struct nfs4_exception exception
= { };
2864 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2865 _nfs4_proc_link(inode
, dir
, name
),
2867 } while (exception
.retry
);
2871 struct nfs4_createdata
{
2872 struct rpc_message msg
;
2873 struct nfs4_create_arg arg
;
2874 struct nfs4_create_res res
;
2876 struct nfs_fattr fattr
;
2877 struct nfs_fattr dir_fattr
;
2880 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2881 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2883 struct nfs4_createdata
*data
;
2885 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2887 struct nfs_server
*server
= NFS_SERVER(dir
);
2889 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2890 data
->msg
.rpc_argp
= &data
->arg
;
2891 data
->msg
.rpc_resp
= &data
->res
;
2892 data
->arg
.dir_fh
= NFS_FH(dir
);
2893 data
->arg
.server
= server
;
2894 data
->arg
.name
= name
;
2895 data
->arg
.attrs
= sattr
;
2896 data
->arg
.ftype
= ftype
;
2897 data
->arg
.bitmask
= server
->attr_bitmask
;
2898 data
->res
.server
= server
;
2899 data
->res
.fh
= &data
->fh
;
2900 data
->res
.fattr
= &data
->fattr
;
2901 data
->res
.dir_fattr
= &data
->dir_fattr
;
2902 nfs_fattr_init(data
->res
.fattr
);
2903 nfs_fattr_init(data
->res
.dir_fattr
);
2908 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2910 int status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &data
->msg
,
2911 &data
->arg
.seq_args
, &data
->res
.seq_res
, 1);
2913 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2914 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2915 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2920 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2925 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2926 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2928 struct nfs4_createdata
*data
;
2929 int status
= -ENAMETOOLONG
;
2931 if (len
> NFS4_MAXPATHLEN
)
2935 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2939 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2940 data
->arg
.u
.symlink
.pages
= &page
;
2941 data
->arg
.u
.symlink
.len
= len
;
2943 status
= nfs4_do_create(dir
, dentry
, data
);
2945 nfs4_free_createdata(data
);
2950 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2951 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2953 struct nfs4_exception exception
= { };
2956 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2957 _nfs4_proc_symlink(dir
, dentry
, page
,
2960 } while (exception
.retry
);
2964 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2965 struct iattr
*sattr
)
2967 struct nfs4_createdata
*data
;
2968 int status
= -ENOMEM
;
2970 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2974 status
= nfs4_do_create(dir
, dentry
, data
);
2976 nfs4_free_createdata(data
);
2981 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2982 struct iattr
*sattr
)
2984 struct nfs4_exception exception
= { };
2987 sattr
->ia_mode
&= ~current_umask();
2989 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2990 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2992 } while (exception
.retry
);
2996 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2997 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
2999 struct inode
*dir
= dentry
->d_inode
;
3000 struct nfs4_readdir_arg args
= {
3005 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
3008 struct nfs4_readdir_res res
;
3009 struct rpc_message msg
= {
3010 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
3017 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
3018 dentry
->d_parent
->d_name
.name
,
3019 dentry
->d_name
.name
,
3020 (unsigned long long)cookie
);
3021 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
3022 res
.pgbase
= args
.pgbase
;
3023 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3025 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
3026 status
+= args
.pgbase
;
3029 nfs_invalidate_atime(dir
);
3031 dprintk("%s: returns %d\n", __func__
, status
);
3035 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
3036 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
3038 struct nfs4_exception exception
= { };
3041 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
3042 _nfs4_proc_readdir(dentry
, cred
, cookie
,
3043 pages
, count
, plus
),
3045 } while (exception
.retry
);
3049 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3050 struct iattr
*sattr
, dev_t rdev
)
3052 struct nfs4_createdata
*data
;
3053 int mode
= sattr
->ia_mode
;
3054 int status
= -ENOMEM
;
3056 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
3057 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
3059 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
3064 data
->arg
.ftype
= NF4FIFO
;
3065 else if (S_ISBLK(mode
)) {
3066 data
->arg
.ftype
= NF4BLK
;
3067 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3068 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3070 else if (S_ISCHR(mode
)) {
3071 data
->arg
.ftype
= NF4CHR
;
3072 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
3073 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
3076 status
= nfs4_do_create(dir
, dentry
, data
);
3078 nfs4_free_createdata(data
);
3083 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
3084 struct iattr
*sattr
, dev_t rdev
)
3086 struct nfs4_exception exception
= { };
3089 sattr
->ia_mode
&= ~current_umask();
3091 err
= nfs4_handle_exception(NFS_SERVER(dir
),
3092 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
3094 } while (exception
.retry
);
3098 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3099 struct nfs_fsstat
*fsstat
)
3101 struct nfs4_statfs_arg args
= {
3103 .bitmask
= server
->attr_bitmask
,
3105 struct nfs4_statfs_res res
= {
3108 struct rpc_message msg
= {
3109 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
3114 nfs_fattr_init(fsstat
->fattr
);
3115 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3118 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
3120 struct nfs4_exception exception
= { };
3123 err
= nfs4_handle_exception(server
,
3124 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
3126 } while (exception
.retry
);
3130 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3131 struct nfs_fsinfo
*fsinfo
)
3133 struct nfs4_fsinfo_arg args
= {
3135 .bitmask
= server
->attr_bitmask
,
3137 struct nfs4_fsinfo_res res
= {
3140 struct rpc_message msg
= {
3141 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3146 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3149 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3151 struct nfs4_exception exception
= { };
3155 err
= nfs4_handle_exception(server
,
3156 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
3158 } while (exception
.retry
);
3162 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3164 nfs_fattr_init(fsinfo
->fattr
);
3165 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3168 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3169 struct nfs_pathconf
*pathconf
)
3171 struct nfs4_pathconf_arg args
= {
3173 .bitmask
= server
->attr_bitmask
,
3175 struct nfs4_pathconf_res res
= {
3176 .pathconf
= pathconf
,
3178 struct rpc_message msg
= {
3179 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3184 /* None of the pathconf attributes are mandatory to implement */
3185 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3186 memset(pathconf
, 0, sizeof(*pathconf
));
3190 nfs_fattr_init(pathconf
->fattr
);
3191 return nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3194 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3195 struct nfs_pathconf
*pathconf
)
3197 struct nfs4_exception exception
= { };
3201 err
= nfs4_handle_exception(server
,
3202 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3204 } while (exception
.retry
);
3208 void __nfs4_read_done_cb(struct nfs_read_data
*data
)
3210 nfs_invalidate_atime(data
->inode
);
3213 static int nfs4_read_done_cb(struct rpc_task
*task
, struct nfs_read_data
*data
)
3215 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3217 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3218 nfs_restart_rpc(task
, server
->nfs_client
);
3222 __nfs4_read_done_cb(data
);
3223 if (task
->tk_status
> 0)
3224 renew_lease(server
, data
->timestamp
);
3228 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3231 dprintk("--> %s\n", __func__
);
3233 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3236 return data
->read_done_cb
? data
->read_done_cb(task
, data
) :
3237 nfs4_read_done_cb(task
, data
);
3240 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3242 data
->timestamp
= jiffies
;
3243 data
->read_done_cb
= nfs4_read_done_cb
;
3244 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3247 /* Reset the the nfs_read_data to send the read to the MDS. */
3248 void nfs4_reset_read(struct rpc_task
*task
, struct nfs_read_data
*data
)
3250 dprintk("%s Reset task for i/o through\n", __func__
);
3251 put_lseg(data
->lseg
);
3253 /* offsets will differ in the dense stripe case */
3254 data
->args
.offset
= data
->mds_offset
;
3255 data
->ds_clp
= NULL
;
3256 data
->args
.fh
= NFS_FH(data
->inode
);
3257 data
->read_done_cb
= nfs4_read_done_cb
;
3258 task
->tk_ops
= data
->mds_ops
;
3259 rpc_task_reset_client(task
, NFS_CLIENT(data
->inode
));
3261 EXPORT_SYMBOL_GPL(nfs4_reset_read
);
3263 static int nfs4_write_done_cb(struct rpc_task
*task
, struct nfs_write_data
*data
)
3265 struct inode
*inode
= data
->inode
;
3267 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3268 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3271 if (task
->tk_status
>= 0) {
3272 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3273 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3278 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3280 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3282 return data
->write_done_cb
? data
->write_done_cb(task
, data
) :
3283 nfs4_write_done_cb(task
, data
);
3286 /* Reset the the nfs_write_data to send the write to the MDS. */
3287 void nfs4_reset_write(struct rpc_task
*task
, struct nfs_write_data
*data
)
3289 dprintk("%s Reset task for i/o through\n", __func__
);
3290 put_lseg(data
->lseg
);
3292 data
->ds_clp
= NULL
;
3293 data
->write_done_cb
= nfs4_write_done_cb
;
3294 data
->args
.fh
= NFS_FH(data
->inode
);
3295 data
->args
.bitmask
= data
->res
.server
->cache_consistency_bitmask
;
3296 data
->args
.offset
= data
->mds_offset
;
3297 data
->res
.fattr
= &data
->fattr
;
3298 task
->tk_ops
= data
->mds_ops
;
3299 rpc_task_reset_client(task
, NFS_CLIENT(data
->inode
));
3301 EXPORT_SYMBOL_GPL(nfs4_reset_write
);
3303 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3305 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3308 data
->args
.bitmask
= NULL
;
3309 data
->res
.fattr
= NULL
;
3311 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3312 if (!data
->write_done_cb
)
3313 data
->write_done_cb
= nfs4_write_done_cb
;
3314 data
->res
.server
= server
;
3315 data
->timestamp
= jiffies
;
3317 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3320 static int nfs4_commit_done_cb(struct rpc_task
*task
, struct nfs_write_data
*data
)
3322 struct inode
*inode
= data
->inode
;
3324 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3325 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3328 nfs_refresh_inode(inode
, data
->res
.fattr
);
3332 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3334 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3336 return data
->write_done_cb(task
, data
);
3339 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3341 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3344 data
->args
.bitmask
= NULL
;
3345 data
->res
.fattr
= NULL
;
3347 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3348 if (!data
->write_done_cb
)
3349 data
->write_done_cb
= nfs4_commit_done_cb
;
3350 data
->res
.server
= server
;
3351 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3354 struct nfs4_renewdata
{
3355 struct nfs_client
*client
;
3356 unsigned long timestamp
;
3360 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3361 * standalone procedure for queueing an asynchronous RENEW.
3363 static void nfs4_renew_release(void *calldata
)
3365 struct nfs4_renewdata
*data
= calldata
;
3366 struct nfs_client
*clp
= data
->client
;
3368 if (atomic_read(&clp
->cl_count
) > 1)
3369 nfs4_schedule_state_renewal(clp
);
3370 nfs_put_client(clp
);
3374 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
3376 struct nfs4_renewdata
*data
= calldata
;
3377 struct nfs_client
*clp
= data
->client
;
3378 unsigned long timestamp
= data
->timestamp
;
3380 if (task
->tk_status
< 0) {
3381 /* Unless we're shutting down, schedule state recovery! */
3382 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3383 nfs4_schedule_lease_recovery(clp
);
3386 do_renew_lease(clp
, timestamp
);
3389 static const struct rpc_call_ops nfs4_renew_ops
= {
3390 .rpc_call_done
= nfs4_renew_done
,
3391 .rpc_release
= nfs4_renew_release
,
3394 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3396 struct rpc_message msg
= {
3397 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3401 struct nfs4_renewdata
*data
;
3403 if (!atomic_inc_not_zero(&clp
->cl_count
))
3405 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
3409 data
->timestamp
= jiffies
;
3410 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3411 &nfs4_renew_ops
, data
);
3414 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3416 struct rpc_message msg
= {
3417 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3421 unsigned long now
= jiffies
;
3424 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3427 do_renew_lease(clp
, now
);
3431 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3433 return (server
->caps
& NFS_CAP_ACLS
)
3434 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3435 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3438 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3439 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3442 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3444 static void buf_to_pages(const void *buf
, size_t buflen
,
3445 struct page
**pages
, unsigned int *pgbase
)
3447 const void *p
= buf
;
3449 *pgbase
= offset_in_page(buf
);
3451 while (p
< buf
+ buflen
) {
3452 *(pages
++) = virt_to_page(p
);
3453 p
+= PAGE_CACHE_SIZE
;
3457 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
3458 struct page
**pages
, unsigned int *pgbase
)
3460 struct page
*newpage
, **spages
;
3466 len
= min_t(size_t, PAGE_CACHE_SIZE
, buflen
);
3467 newpage
= alloc_page(GFP_KERNEL
);
3469 if (newpage
== NULL
)
3471 memcpy(page_address(newpage
), buf
, len
);
3476 } while (buflen
!= 0);
3482 __free_page(spages
[rc
-1]);
3486 struct nfs4_cached_acl
{
3492 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3494 struct nfs_inode
*nfsi
= NFS_I(inode
);
3496 spin_lock(&inode
->i_lock
);
3497 kfree(nfsi
->nfs4_acl
);
3498 nfsi
->nfs4_acl
= acl
;
3499 spin_unlock(&inode
->i_lock
);
3502 static void nfs4_zap_acl_attr(struct inode
*inode
)
3504 nfs4_set_cached_acl(inode
, NULL
);
3507 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3509 struct nfs_inode
*nfsi
= NFS_I(inode
);
3510 struct nfs4_cached_acl
*acl
;
3513 spin_lock(&inode
->i_lock
);
3514 acl
= nfsi
->nfs4_acl
;
3517 if (buf
== NULL
) /* user is just asking for length */
3519 if (acl
->cached
== 0)
3521 ret
= -ERANGE
; /* see getxattr(2) man page */
3522 if (acl
->len
> buflen
)
3524 memcpy(buf
, acl
->data
, acl
->len
);
3528 spin_unlock(&inode
->i_lock
);
3532 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3534 struct nfs4_cached_acl
*acl
;
3536 if (buf
&& acl_len
<= PAGE_SIZE
) {
3537 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3541 memcpy(acl
->data
, buf
, acl_len
);
3543 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3550 nfs4_set_cached_acl(inode
, acl
);
3553 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3555 struct page
*pages
[NFS4ACL_MAXPAGES
];
3556 struct nfs_getaclargs args
= {
3557 .fh
= NFS_FH(inode
),
3561 struct nfs_getaclres res
= {
3565 struct rpc_message msg
= {
3566 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3570 struct page
*localpage
= NULL
;
3573 if (buflen
< PAGE_SIZE
) {
3574 /* As long as we're doing a round trip to the server anyway,
3575 * let's be prepared for a page of acl data. */
3576 localpage
= alloc_page(GFP_KERNEL
);
3577 resp_buf
= page_address(localpage
);
3578 if (localpage
== NULL
)
3580 args
.acl_pages
[0] = localpage
;
3581 args
.acl_pgbase
= 0;
3582 args
.acl_len
= PAGE_SIZE
;
3585 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3587 ret
= nfs4_call_sync(NFS_SERVER(inode
)->client
, NFS_SERVER(inode
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
3590 if (res
.acl_len
> args
.acl_len
)
3591 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3593 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3596 if (res
.acl_len
> buflen
)
3599 memcpy(buf
, resp_buf
, res
.acl_len
);
3604 __free_page(localpage
);
3608 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3610 struct nfs4_exception exception
= { };
3613 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3616 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3617 } while (exception
.retry
);
3621 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3623 struct nfs_server
*server
= NFS_SERVER(inode
);
3626 if (!nfs4_server_supports_acls(server
))
3628 ret
= nfs_revalidate_inode(server
, inode
);
3631 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
3632 nfs_zap_acl_cache(inode
);
3633 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3636 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3639 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3641 struct nfs_server
*server
= NFS_SERVER(inode
);
3642 struct page
*pages
[NFS4ACL_MAXPAGES
];
3643 struct nfs_setaclargs arg
= {
3644 .fh
= NFS_FH(inode
),
3648 struct nfs_setaclres res
;
3649 struct rpc_message msg
= {
3650 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3656 if (!nfs4_server_supports_acls(server
))
3658 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3661 nfs_inode_return_delegation(inode
);
3662 ret
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
3665 * Free each page after tx, so the only ref left is
3666 * held by the network stack
3669 put_page(pages
[i
-1]);
3672 * Acl update can result in inode attribute update.
3673 * so mark the attribute cache invalid.
3675 spin_lock(&inode
->i_lock
);
3676 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
3677 spin_unlock(&inode
->i_lock
);
3678 nfs_access_zap_cache(inode
);
3679 nfs_zap_acl_cache(inode
);
3683 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3685 struct nfs4_exception exception
= { };
3688 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3689 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3691 } while (exception
.retry
);
3696 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3698 struct nfs_client
*clp
= server
->nfs_client
;
3700 if (task
->tk_status
>= 0)
3702 switch(task
->tk_status
) {
3703 case -NFS4ERR_DELEG_REVOKED
:
3704 case -NFS4ERR_ADMIN_REVOKED
:
3705 case -NFS4ERR_BAD_STATEID
:
3707 nfs_remove_bad_delegation(state
->inode
);
3708 case -NFS4ERR_OPENMODE
:
3711 nfs4_schedule_stateid_recovery(server
, state
);
3712 goto wait_on_recovery
;
3713 case -NFS4ERR_EXPIRED
:
3715 nfs4_schedule_stateid_recovery(server
, state
);
3716 case -NFS4ERR_STALE_STATEID
:
3717 case -NFS4ERR_STALE_CLIENTID
:
3718 nfs4_schedule_lease_recovery(clp
);
3719 goto wait_on_recovery
;
3720 #if defined(CONFIG_NFS_V4_1)
3721 case -NFS4ERR_BADSESSION
:
3722 case -NFS4ERR_BADSLOT
:
3723 case -NFS4ERR_BAD_HIGH_SLOT
:
3724 case -NFS4ERR_DEADSESSION
:
3725 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3726 case -NFS4ERR_SEQ_FALSE_RETRY
:
3727 case -NFS4ERR_SEQ_MISORDERED
:
3728 dprintk("%s ERROR %d, Reset session\n", __func__
,
3730 nfs4_schedule_session_recovery(clp
->cl_session
);
3731 task
->tk_status
= 0;
3733 #endif /* CONFIG_NFS_V4_1 */
3734 case -NFS4ERR_DELAY
:
3735 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3736 case -NFS4ERR_GRACE
:
3738 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3739 task
->tk_status
= 0;
3741 case -NFS4ERR_RETRY_UNCACHED_REP
:
3742 case -NFS4ERR_OLD_STATEID
:
3743 task
->tk_status
= 0;
3746 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3749 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3750 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3751 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3752 task
->tk_status
= 0;
3756 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
3757 unsigned short port
, struct rpc_cred
*cred
,
3758 struct nfs4_setclientid_res
*res
)
3760 nfs4_verifier sc_verifier
;
3761 struct nfs4_setclientid setclientid
= {
3762 .sc_verifier
= &sc_verifier
,
3764 .sc_cb_ident
= clp
->cl_cb_ident
,
3766 struct rpc_message msg
= {
3767 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3768 .rpc_argp
= &setclientid
,
3776 p
= (__be32
*)sc_verifier
.data
;
3777 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3778 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3781 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3782 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3784 rpc_peeraddr2str(clp
->cl_rpcclient
,
3786 rpc_peeraddr2str(clp
->cl_rpcclient
,
3788 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3789 clp
->cl_id_uniquifier
);
3790 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3791 sizeof(setclientid
.sc_netid
),
3792 rpc_peeraddr2str(clp
->cl_rpcclient
,
3793 RPC_DISPLAY_NETID
));
3794 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3795 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3796 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3798 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
3799 if (status
!= -NFS4ERR_CLID_INUSE
)
3802 ++clp
->cl_id_uniquifier
;
3806 ssleep(clp
->cl_lease_time
/ HZ
+ 1);
3811 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
3812 struct nfs4_setclientid_res
*arg
,
3813 struct rpc_cred
*cred
)
3815 struct nfs_fsinfo fsinfo
;
3816 struct rpc_message msg
= {
3817 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3819 .rpc_resp
= &fsinfo
,
3826 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
3828 spin_lock(&clp
->cl_lock
);
3829 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3830 clp
->cl_last_renewal
= now
;
3831 spin_unlock(&clp
->cl_lock
);
3836 struct nfs4_delegreturndata
{
3837 struct nfs4_delegreturnargs args
;
3838 struct nfs4_delegreturnres res
;
3840 nfs4_stateid stateid
;
3841 unsigned long timestamp
;
3842 struct nfs_fattr fattr
;
3846 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3848 struct nfs4_delegreturndata
*data
= calldata
;
3850 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3853 switch (task
->tk_status
) {
3854 case -NFS4ERR_STALE_STATEID
:
3855 case -NFS4ERR_EXPIRED
:
3857 renew_lease(data
->res
.server
, data
->timestamp
);
3860 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
3862 nfs_restart_rpc(task
, data
->res
.server
->nfs_client
);
3866 data
->rpc_status
= task
->tk_status
;
3869 static void nfs4_delegreturn_release(void *calldata
)
3874 #if defined(CONFIG_NFS_V4_1)
3875 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3877 struct nfs4_delegreturndata
*d_data
;
3879 d_data
= (struct nfs4_delegreturndata
*)data
;
3881 if (nfs4_setup_sequence(d_data
->res
.server
,
3882 &d_data
->args
.seq_args
,
3883 &d_data
->res
.seq_res
, 1, task
))
3885 rpc_call_start(task
);
3887 #endif /* CONFIG_NFS_V4_1 */
3889 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3890 #if defined(CONFIG_NFS_V4_1)
3891 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3892 #endif /* CONFIG_NFS_V4_1 */
3893 .rpc_call_done
= nfs4_delegreturn_done
,
3894 .rpc_release
= nfs4_delegreturn_release
,
3897 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3899 struct nfs4_delegreturndata
*data
;
3900 struct nfs_server
*server
= NFS_SERVER(inode
);
3901 struct rpc_task
*task
;
3902 struct rpc_message msg
= {
3903 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3906 struct rpc_task_setup task_setup_data
= {
3907 .rpc_client
= server
->client
,
3908 .rpc_message
= &msg
,
3909 .callback_ops
= &nfs4_delegreturn_ops
,
3910 .flags
= RPC_TASK_ASYNC
,
3914 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
3917 data
->args
.fhandle
= &data
->fh
;
3918 data
->args
.stateid
= &data
->stateid
;
3919 data
->args
.bitmask
= server
->attr_bitmask
;
3920 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3921 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3922 data
->res
.fattr
= &data
->fattr
;
3923 data
->res
.server
= server
;
3924 nfs_fattr_init(data
->res
.fattr
);
3925 data
->timestamp
= jiffies
;
3926 data
->rpc_status
= 0;
3928 task_setup_data
.callback_data
= data
;
3929 msg
.rpc_argp
= &data
->args
;
3930 msg
.rpc_resp
= &data
->res
;
3931 task
= rpc_run_task(&task_setup_data
);
3933 return PTR_ERR(task
);
3936 status
= nfs4_wait_for_completion_rpc_task(task
);
3939 status
= data
->rpc_status
;
3942 nfs_refresh_inode(inode
, &data
->fattr
);
3948 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3950 struct nfs_server
*server
= NFS_SERVER(inode
);
3951 struct nfs4_exception exception
= { };
3954 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3956 case -NFS4ERR_STALE_STATEID
:
3957 case -NFS4ERR_EXPIRED
:
3961 err
= nfs4_handle_exception(server
, err
, &exception
);
3962 } while (exception
.retry
);
3966 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3967 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3970 * sleep, with exponential backoff, and retry the LOCK operation.
3972 static unsigned long
3973 nfs4_set_lock_task_retry(unsigned long timeout
)
3975 schedule_timeout_killable(timeout
);
3977 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3978 return NFS4_LOCK_MAXTIMEOUT
;
3982 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3984 struct inode
*inode
= state
->inode
;
3985 struct nfs_server
*server
= NFS_SERVER(inode
);
3986 struct nfs_client
*clp
= server
->nfs_client
;
3987 struct nfs_lockt_args arg
= {
3988 .fh
= NFS_FH(inode
),
3991 struct nfs_lockt_res res
= {
3994 struct rpc_message msg
= {
3995 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3998 .rpc_cred
= state
->owner
->so_cred
,
4000 struct nfs4_lock_state
*lsp
;
4003 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
4004 status
= nfs4_set_lock_state(state
, request
);
4007 lsp
= request
->fl_u
.nfs4_fl
.owner
;
4008 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
4009 arg
.lock_owner
.s_dev
= server
->s_dev
;
4010 status
= nfs4_call_sync(server
->client
, server
, &msg
, &arg
.seq_args
, &res
.seq_res
, 1);
4013 request
->fl_type
= F_UNLCK
;
4015 case -NFS4ERR_DENIED
:
4018 request
->fl_ops
->fl_release_private(request
);
4023 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4025 struct nfs4_exception exception
= { };
4029 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4030 _nfs4_proc_getlk(state
, cmd
, request
),
4032 } while (exception
.retry
);
4036 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
4039 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
4041 res
= posix_lock_file_wait(file
, fl
);
4044 res
= flock_lock_file_wait(file
, fl
);
4052 struct nfs4_unlockdata
{
4053 struct nfs_locku_args arg
;
4054 struct nfs_locku_res res
;
4055 struct nfs4_lock_state
*lsp
;
4056 struct nfs_open_context
*ctx
;
4057 struct file_lock fl
;
4058 const struct nfs_server
*server
;
4059 unsigned long timestamp
;
4062 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
4063 struct nfs_open_context
*ctx
,
4064 struct nfs4_lock_state
*lsp
,
4065 struct nfs_seqid
*seqid
)
4067 struct nfs4_unlockdata
*p
;
4068 struct inode
*inode
= lsp
->ls_state
->inode
;
4070 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
4073 p
->arg
.fh
= NFS_FH(inode
);
4075 p
->arg
.seqid
= seqid
;
4076 p
->res
.seqid
= seqid
;
4077 p
->arg
.stateid
= &lsp
->ls_stateid
;
4079 atomic_inc(&lsp
->ls_count
);
4080 /* Ensure we don't close file until we're done freeing locks! */
4081 p
->ctx
= get_nfs_open_context(ctx
);
4082 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4083 p
->server
= NFS_SERVER(inode
);
4087 static void nfs4_locku_release_calldata(void *data
)
4089 struct nfs4_unlockdata
*calldata
= data
;
4090 nfs_free_seqid(calldata
->arg
.seqid
);
4091 nfs4_put_lock_state(calldata
->lsp
);
4092 put_nfs_open_context(calldata
->ctx
);
4096 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
4098 struct nfs4_unlockdata
*calldata
= data
;
4100 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
4102 switch (task
->tk_status
) {
4104 memcpy(calldata
->lsp
->ls_stateid
.data
,
4105 calldata
->res
.stateid
.data
,
4106 sizeof(calldata
->lsp
->ls_stateid
.data
));
4107 renew_lease(calldata
->server
, calldata
->timestamp
);
4109 case -NFS4ERR_BAD_STATEID
:
4110 case -NFS4ERR_OLD_STATEID
:
4111 case -NFS4ERR_STALE_STATEID
:
4112 case -NFS4ERR_EXPIRED
:
4115 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
4116 nfs_restart_rpc(task
,
4117 calldata
->server
->nfs_client
);
4121 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
4123 struct nfs4_unlockdata
*calldata
= data
;
4125 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
4127 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
4128 /* Note: exit _without_ running nfs4_locku_done */
4129 task
->tk_action
= NULL
;
4132 calldata
->timestamp
= jiffies
;
4133 if (nfs4_setup_sequence(calldata
->server
,
4134 &calldata
->arg
.seq_args
,
4135 &calldata
->res
.seq_res
, 1, task
))
4137 rpc_call_start(task
);
4140 static const struct rpc_call_ops nfs4_locku_ops
= {
4141 .rpc_call_prepare
= nfs4_locku_prepare
,
4142 .rpc_call_done
= nfs4_locku_done
,
4143 .rpc_release
= nfs4_locku_release_calldata
,
4146 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
4147 struct nfs_open_context
*ctx
,
4148 struct nfs4_lock_state
*lsp
,
4149 struct nfs_seqid
*seqid
)
4151 struct nfs4_unlockdata
*data
;
4152 struct rpc_message msg
= {
4153 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
4154 .rpc_cred
= ctx
->cred
,
4156 struct rpc_task_setup task_setup_data
= {
4157 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
4158 .rpc_message
= &msg
,
4159 .callback_ops
= &nfs4_locku_ops
,
4160 .workqueue
= nfsiod_workqueue
,
4161 .flags
= RPC_TASK_ASYNC
,
4164 /* Ensure this is an unlock - when canceling a lock, the
4165 * canceled lock is passed in, and it won't be an unlock.
4167 fl
->fl_type
= F_UNLCK
;
4169 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
4171 nfs_free_seqid(seqid
);
4172 return ERR_PTR(-ENOMEM
);
4175 msg
.rpc_argp
= &data
->arg
;
4176 msg
.rpc_resp
= &data
->res
;
4177 task_setup_data
.callback_data
= data
;
4178 return rpc_run_task(&task_setup_data
);
4181 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4183 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4184 struct nfs_seqid
*seqid
;
4185 struct nfs4_lock_state
*lsp
;
4186 struct rpc_task
*task
;
4188 unsigned char fl_flags
= request
->fl_flags
;
4190 status
= nfs4_set_lock_state(state
, request
);
4191 /* Unlock _before_ we do the RPC call */
4192 request
->fl_flags
|= FL_EXISTS
;
4193 down_read(&nfsi
->rwsem
);
4194 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
4195 up_read(&nfsi
->rwsem
);
4198 up_read(&nfsi
->rwsem
);
4201 /* Is this a delegated lock? */
4202 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
4204 lsp
= request
->fl_u
.nfs4_fl
.owner
;
4205 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
4209 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
4210 status
= PTR_ERR(task
);
4213 status
= nfs4_wait_for_completion_rpc_task(task
);
4216 request
->fl_flags
= fl_flags
;
4220 struct nfs4_lockdata
{
4221 struct nfs_lock_args arg
;
4222 struct nfs_lock_res res
;
4223 struct nfs4_lock_state
*lsp
;
4224 struct nfs_open_context
*ctx
;
4225 struct file_lock fl
;
4226 unsigned long timestamp
;
4229 struct nfs_server
*server
;
4232 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
4233 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
4236 struct nfs4_lockdata
*p
;
4237 struct inode
*inode
= lsp
->ls_state
->inode
;
4238 struct nfs_server
*server
= NFS_SERVER(inode
);
4240 p
= kzalloc(sizeof(*p
), gfp_mask
);
4244 p
->arg
.fh
= NFS_FH(inode
);
4246 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
4247 if (p
->arg
.open_seqid
== NULL
)
4249 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
4250 if (p
->arg
.lock_seqid
== NULL
)
4251 goto out_free_seqid
;
4252 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
4253 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4254 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
4255 p
->arg
.lock_owner
.s_dev
= server
->s_dev
;
4256 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
4259 atomic_inc(&lsp
->ls_count
);
4260 p
->ctx
= get_nfs_open_context(ctx
);
4261 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4264 nfs_free_seqid(p
->arg
.open_seqid
);
4270 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
4272 struct nfs4_lockdata
*data
= calldata
;
4273 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4275 dprintk("%s: begin!\n", __func__
);
4276 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4278 /* Do we need to do an open_to_lock_owner? */
4279 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4280 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
4282 data
->arg
.open_stateid
= &state
->stateid
;
4283 data
->arg
.new_lock_owner
= 1;
4284 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4286 data
->arg
.new_lock_owner
= 0;
4287 data
->timestamp
= jiffies
;
4288 if (nfs4_setup_sequence(data
->server
,
4289 &data
->arg
.seq_args
,
4290 &data
->res
.seq_res
, 1, task
))
4292 rpc_call_start(task
);
4293 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4296 static void nfs4_recover_lock_prepare(struct rpc_task
*task
, void *calldata
)
4298 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4299 nfs4_lock_prepare(task
, calldata
);
4302 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4304 struct nfs4_lockdata
*data
= calldata
;
4306 dprintk("%s: begin!\n", __func__
);
4308 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4311 data
->rpc_status
= task
->tk_status
;
4312 if (data
->arg
.new_lock_owner
!= 0) {
4313 if (data
->rpc_status
== 0)
4314 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4318 if (data
->rpc_status
== 0) {
4319 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
4320 sizeof(data
->lsp
->ls_stateid
.data
));
4321 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
4322 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4325 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4328 static void nfs4_lock_release(void *calldata
)
4330 struct nfs4_lockdata
*data
= calldata
;
4332 dprintk("%s: begin!\n", __func__
);
4333 nfs_free_seqid(data
->arg
.open_seqid
);
4334 if (data
->cancelled
!= 0) {
4335 struct rpc_task
*task
;
4336 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4337 data
->arg
.lock_seqid
);
4339 rpc_put_task_async(task
);
4340 dprintk("%s: cancelling lock!\n", __func__
);
4342 nfs_free_seqid(data
->arg
.lock_seqid
);
4343 nfs4_put_lock_state(data
->lsp
);
4344 put_nfs_open_context(data
->ctx
);
4346 dprintk("%s: done!\n", __func__
);
4349 static const struct rpc_call_ops nfs4_lock_ops
= {
4350 .rpc_call_prepare
= nfs4_lock_prepare
,
4351 .rpc_call_done
= nfs4_lock_done
,
4352 .rpc_release
= nfs4_lock_release
,
4355 static const struct rpc_call_ops nfs4_recover_lock_ops
= {
4356 .rpc_call_prepare
= nfs4_recover_lock_prepare
,
4357 .rpc_call_done
= nfs4_lock_done
,
4358 .rpc_release
= nfs4_lock_release
,
4361 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4364 case -NFS4ERR_ADMIN_REVOKED
:
4365 case -NFS4ERR_BAD_STATEID
:
4366 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4367 if (new_lock_owner
!= 0 ||
4368 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4369 nfs4_schedule_stateid_recovery(server
, lsp
->ls_state
);
4371 case -NFS4ERR_STALE_STATEID
:
4372 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4373 case -NFS4ERR_EXPIRED
:
4374 nfs4_schedule_lease_recovery(server
->nfs_client
);
4378 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4380 struct nfs4_lockdata
*data
;
4381 struct rpc_task
*task
;
4382 struct rpc_message msg
= {
4383 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4384 .rpc_cred
= state
->owner
->so_cred
,
4386 struct rpc_task_setup task_setup_data
= {
4387 .rpc_client
= NFS_CLIENT(state
->inode
),
4388 .rpc_message
= &msg
,
4389 .callback_ops
= &nfs4_lock_ops
,
4390 .workqueue
= nfsiod_workqueue
,
4391 .flags
= RPC_TASK_ASYNC
,
4395 dprintk("%s: begin!\n", __func__
);
4396 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4397 fl
->fl_u
.nfs4_fl
.owner
,
4398 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
4402 data
->arg
.block
= 1;
4403 if (recovery_type
> NFS_LOCK_NEW
) {
4404 if (recovery_type
== NFS_LOCK_RECLAIM
)
4405 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4406 task_setup_data
.callback_ops
= &nfs4_recover_lock_ops
;
4408 msg
.rpc_argp
= &data
->arg
;
4409 msg
.rpc_resp
= &data
->res
;
4410 task_setup_data
.callback_data
= data
;
4411 task
= rpc_run_task(&task_setup_data
);
4413 return PTR_ERR(task
);
4414 ret
= nfs4_wait_for_completion_rpc_task(task
);
4416 ret
= data
->rpc_status
;
4418 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4419 data
->arg
.new_lock_owner
, ret
);
4421 data
->cancelled
= 1;
4423 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4427 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4429 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4430 struct nfs4_exception exception
= {
4431 .inode
= state
->inode
,
4436 /* Cache the lock if possible... */
4437 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4439 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4440 if (err
!= -NFS4ERR_DELAY
)
4442 nfs4_handle_exception(server
, err
, &exception
);
4443 } while (exception
.retry
);
4447 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4449 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4450 struct nfs4_exception exception
= {
4451 .inode
= state
->inode
,
4455 err
= nfs4_set_lock_state(state
, request
);
4459 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4461 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4465 case -NFS4ERR_GRACE
:
4466 case -NFS4ERR_DELAY
:
4467 nfs4_handle_exception(server
, err
, &exception
);
4470 } while (exception
.retry
);
4475 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4477 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4478 unsigned char fl_flags
= request
->fl_flags
;
4479 int status
= -ENOLCK
;
4481 if ((fl_flags
& FL_POSIX
) &&
4482 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4484 /* Is this a delegated open? */
4485 status
= nfs4_set_lock_state(state
, request
);
4488 request
->fl_flags
|= FL_ACCESS
;
4489 status
= do_vfs_lock(request
->fl_file
, request
);
4492 down_read(&nfsi
->rwsem
);
4493 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4494 /* Yes: cache locks! */
4495 /* ...but avoid races with delegation recall... */
4496 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4497 status
= do_vfs_lock(request
->fl_file
, request
);
4500 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4503 /* Note: we always want to sleep here! */
4504 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4505 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4506 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4508 up_read(&nfsi
->rwsem
);
4510 request
->fl_flags
= fl_flags
;
4514 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4516 struct nfs4_exception exception
= {
4518 .inode
= state
->inode
,
4523 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4524 if (err
== -NFS4ERR_DENIED
)
4526 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4528 } while (exception
.retry
);
4533 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4535 struct nfs_open_context
*ctx
;
4536 struct nfs4_state
*state
;
4537 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4540 /* verify open state */
4541 ctx
= nfs_file_open_context(filp
);
4544 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4547 if (IS_GETLK(cmd
)) {
4549 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4553 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4556 if (request
->fl_type
== F_UNLCK
) {
4558 return nfs4_proc_unlck(state
, cmd
, request
);
4565 * Don't rely on the VFS having checked the file open mode,
4566 * since it won't do this for flock() locks.
4568 switch (request
->fl_type
& (F_RDLCK
|F_WRLCK
|F_UNLCK
)) {
4570 if (!(filp
->f_mode
& FMODE_READ
))
4574 if (!(filp
->f_mode
& FMODE_WRITE
))
4579 status
= nfs4_proc_setlk(state
, cmd
, request
);
4580 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4582 timeout
= nfs4_set_lock_task_retry(timeout
);
4583 status
= -ERESTARTSYS
;
4586 } while(status
< 0);
4590 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4592 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4593 struct nfs4_exception exception
= { };
4596 err
= nfs4_set_lock_state(state
, fl
);
4600 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4603 printk(KERN_ERR
"%s: unhandled error %d.\n",
4608 case -NFS4ERR_EXPIRED
:
4609 nfs4_schedule_stateid_recovery(server
, state
);
4610 case -NFS4ERR_STALE_CLIENTID
:
4611 case -NFS4ERR_STALE_STATEID
:
4612 nfs4_schedule_lease_recovery(server
->nfs_client
);
4614 case -NFS4ERR_BADSESSION
:
4615 case -NFS4ERR_BADSLOT
:
4616 case -NFS4ERR_BAD_HIGH_SLOT
:
4617 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4618 case -NFS4ERR_DEADSESSION
:
4619 nfs4_schedule_session_recovery(server
->nfs_client
->cl_session
);
4623 * The show must go on: exit, but mark the
4624 * stateid as needing recovery.
4626 case -NFS4ERR_DELEG_REVOKED
:
4627 case -NFS4ERR_ADMIN_REVOKED
:
4628 case -NFS4ERR_BAD_STATEID
:
4629 case -NFS4ERR_OPENMODE
:
4630 nfs4_schedule_stateid_recovery(server
, state
);
4635 * User RPCSEC_GSS context has expired.
4636 * We cannot recover this stateid now, so
4637 * skip it and allow recovery thread to
4643 case -NFS4ERR_DENIED
:
4644 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4647 case -NFS4ERR_DELAY
:
4650 err
= nfs4_handle_exception(server
, err
, &exception
);
4651 } while (exception
.retry
);
4656 static void nfs4_release_lockowner_release(void *calldata
)
4661 const struct rpc_call_ops nfs4_release_lockowner_ops
= {
4662 .rpc_release
= nfs4_release_lockowner_release
,
4665 void nfs4_release_lockowner(const struct nfs4_lock_state
*lsp
)
4667 struct nfs_server
*server
= lsp
->ls_state
->owner
->so_server
;
4668 struct nfs_release_lockowner_args
*args
;
4669 struct rpc_message msg
= {
4670 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
4673 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
4675 args
= kmalloc(sizeof(*args
), GFP_NOFS
);
4678 args
->lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4679 args
->lock_owner
.id
= lsp
->ls_id
.id
;
4680 args
->lock_owner
.s_dev
= server
->s_dev
;
4681 msg
.rpc_argp
= args
;
4682 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, args
);
4685 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4687 static int nfs4_xattr_set_nfs4_acl(struct dentry
*dentry
, const char *key
,
4688 const void *buf
, size_t buflen
,
4689 int flags
, int type
)
4691 if (strcmp(key
, "") != 0)
4694 return nfs4_proc_set_acl(dentry
->d_inode
, buf
, buflen
);
4697 static int nfs4_xattr_get_nfs4_acl(struct dentry
*dentry
, const char *key
,
4698 void *buf
, size_t buflen
, int type
)
4700 if (strcmp(key
, "") != 0)
4703 return nfs4_proc_get_acl(dentry
->d_inode
, buf
, buflen
);
4706 static size_t nfs4_xattr_list_nfs4_acl(struct dentry
*dentry
, char *list
,
4707 size_t list_len
, const char *name
,
4708 size_t name_len
, int type
)
4710 size_t len
= sizeof(XATTR_NAME_NFSV4_ACL
);
4712 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4715 if (list
&& len
<= list_len
)
4716 memcpy(list
, XATTR_NAME_NFSV4_ACL
, len
);
4721 * nfs_fhget will use either the mounted_on_fileid or the fileid
4723 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4725 if (!(((fattr
->valid
& NFS_ATTR_FATTR_MOUNTED_ON_FILEID
) ||
4726 (fattr
->valid
& NFS_ATTR_FATTR_FILEID
)) &&
4727 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4728 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4731 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4732 NFS_ATTR_FATTR_NLINK
;
4733 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4737 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4738 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4740 struct nfs_server
*server
= NFS_SERVER(dir
);
4742 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4744 struct nfs4_fs_locations_arg args
= {
4745 .dir_fh
= NFS_FH(dir
),
4750 struct nfs4_fs_locations_res res
= {
4751 .fs_locations
= fs_locations
,
4753 struct rpc_message msg
= {
4754 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4760 dprintk("%s: start\n", __func__
);
4762 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4763 * is not supported */
4764 if (NFS_SERVER(dir
)->attr_bitmask
[1] & FATTR4_WORD1_MOUNTED_ON_FILEID
)
4765 bitmask
[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID
;
4767 bitmask
[0] |= FATTR4_WORD0_FILEID
;
4769 nfs_fattr_init(&fs_locations
->fattr
);
4770 fs_locations
->server
= server
;
4771 fs_locations
->nlocations
= 0;
4772 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4773 dprintk("%s: returned status = %d\n", __func__
, status
);
4777 static int _nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
)
4780 struct nfs4_secinfo_arg args
= {
4781 .dir_fh
= NFS_FH(dir
),
4784 struct nfs4_secinfo_res res
= {
4787 struct rpc_message msg
= {
4788 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SECINFO
],
4793 dprintk("NFS call secinfo %s\n", name
->name
);
4794 status
= nfs4_call_sync(NFS_SERVER(dir
)->client
, NFS_SERVER(dir
), &msg
, &args
.seq_args
, &res
.seq_res
, 0);
4795 dprintk("NFS reply secinfo: %d\n", status
);
4799 int nfs4_proc_secinfo(struct inode
*dir
, const struct qstr
*name
, struct nfs4_secinfo_flavors
*flavors
)
4801 struct nfs4_exception exception
= { };
4804 err
= nfs4_handle_exception(NFS_SERVER(dir
),
4805 _nfs4_proc_secinfo(dir
, name
, flavors
),
4807 } while (exception
.retry
);
4811 #ifdef CONFIG_NFS_V4_1
4813 * Check the exchange flags returned by the server for invalid flags, having
4814 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4817 static int nfs4_check_cl_exchange_flags(u32 flags
)
4819 if (flags
& ~EXCHGID4_FLAG_MASK_R
)
4821 if ((flags
& EXCHGID4_FLAG_USE_PNFS_MDS
) &&
4822 (flags
& EXCHGID4_FLAG_USE_NON_PNFS
))
4824 if (!(flags
& (EXCHGID4_FLAG_MASK_PNFS
)))
4828 return -NFS4ERR_INVAL
;
4832 * nfs4_proc_exchange_id()
4834 * Since the clientid has expired, all compounds using sessions
4835 * associated with the stale clientid will be returning
4836 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4837 * be in some phase of session reset.
4839 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4841 nfs4_verifier verifier
;
4842 struct nfs41_exchange_id_args args
= {
4844 .flags
= EXCHGID4_FLAG_SUPP_MOVED_REFER
,
4846 struct nfs41_exchange_id_res res
= {
4850 struct rpc_message msg
= {
4851 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4858 dprintk("--> %s\n", __func__
);
4859 BUG_ON(clp
== NULL
);
4861 p
= (u32
*)verifier
.data
;
4862 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4863 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4864 args
.verifier
= &verifier
;
4866 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4869 init_utsname()->nodename
,
4870 init_utsname()->domainname
,
4871 clp
->cl_rpcclient
->cl_auth
->au_flavor
);
4873 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
4875 status
= nfs4_check_cl_exchange_flags(clp
->cl_exchange_flags
);
4876 dprintk("<-- %s status= %d\n", __func__
, status
);
4880 struct nfs4_get_lease_time_data
{
4881 struct nfs4_get_lease_time_args
*args
;
4882 struct nfs4_get_lease_time_res
*res
;
4883 struct nfs_client
*clp
;
4886 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4890 struct nfs4_get_lease_time_data
*data
=
4891 (struct nfs4_get_lease_time_data
*)calldata
;
4893 dprintk("--> %s\n", __func__
);
4894 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4895 /* just setup sequence, do not trigger session recovery
4896 since we're invoked within one */
4897 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4898 &data
->args
->la_seq_args
,
4899 &data
->res
->lr_seq_res
, 0, task
);
4901 BUG_ON(ret
== -EAGAIN
);
4902 rpc_call_start(task
);
4903 dprintk("<-- %s\n", __func__
);
4907 * Called from nfs4_state_manager thread for session setup, so don't recover
4908 * from sequence operation or clientid errors.
4910 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4912 struct nfs4_get_lease_time_data
*data
=
4913 (struct nfs4_get_lease_time_data
*)calldata
;
4915 dprintk("--> %s\n", __func__
);
4916 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
4918 switch (task
->tk_status
) {
4919 case -NFS4ERR_DELAY
:
4920 case -NFS4ERR_GRACE
:
4921 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4922 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4923 task
->tk_status
= 0;
4925 case -NFS4ERR_RETRY_UNCACHED_REP
:
4926 nfs_restart_rpc(task
, data
->clp
);
4929 dprintk("<-- %s\n", __func__
);
4932 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4933 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4934 .rpc_call_done
= nfs4_get_lease_time_done
,
4937 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4939 struct rpc_task
*task
;
4940 struct nfs4_get_lease_time_args args
;
4941 struct nfs4_get_lease_time_res res
= {
4942 .lr_fsinfo
= fsinfo
,
4944 struct nfs4_get_lease_time_data data
= {
4949 struct rpc_message msg
= {
4950 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4954 struct rpc_task_setup task_setup
= {
4955 .rpc_client
= clp
->cl_rpcclient
,
4956 .rpc_message
= &msg
,
4957 .callback_ops
= &nfs4_get_lease_time_ops
,
4958 .callback_data
= &data
,
4959 .flags
= RPC_TASK_TIMEOUT
,
4963 dprintk("--> %s\n", __func__
);
4964 task
= rpc_run_task(&task_setup
);
4967 status
= PTR_ERR(task
);
4969 status
= task
->tk_status
;
4972 dprintk("<-- %s return %d\n", __func__
, status
);
4978 * Reset a slot table
4980 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, u32 max_reqs
,
4983 struct nfs4_slot
*new = NULL
;
4987 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__
,
4988 max_reqs
, tbl
->max_slots
);
4990 /* Does the newly negotiated max_reqs match the existing slot table? */
4991 if (max_reqs
!= tbl
->max_slots
) {
4993 new = kmalloc(max_reqs
* sizeof(struct nfs4_slot
),
5000 spin_lock(&tbl
->slot_tbl_lock
);
5003 tbl
->max_slots
= max_reqs
;
5005 for (i
= 0; i
< tbl
->max_slots
; ++i
)
5006 tbl
->slots
[i
].seq_nr
= ivalue
;
5007 spin_unlock(&tbl
->slot_tbl_lock
);
5008 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
5009 tbl
, tbl
->slots
, tbl
->max_slots
);
5011 dprintk("<-- %s: return %d\n", __func__
, ret
);
5016 * Reset the forechannel and backchannel slot tables
5018 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
5022 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
5023 session
->fc_attrs
.max_reqs
, 1);
5027 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
5028 session
->bc_attrs
.max_reqs
, 0);
5032 /* Destroy the slot table */
5033 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
5035 if (session
->fc_slot_table
.slots
!= NULL
) {
5036 kfree(session
->fc_slot_table
.slots
);
5037 session
->fc_slot_table
.slots
= NULL
;
5039 if (session
->bc_slot_table
.slots
!= NULL
) {
5040 kfree(session
->bc_slot_table
.slots
);
5041 session
->bc_slot_table
.slots
= NULL
;
5047 * Initialize slot table
5049 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
5050 int max_slots
, int ivalue
)
5052 struct nfs4_slot
*slot
;
5055 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
5057 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
5059 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_NOFS
);
5064 spin_lock(&tbl
->slot_tbl_lock
);
5065 tbl
->max_slots
= max_slots
;
5067 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
5068 spin_unlock(&tbl
->slot_tbl_lock
);
5069 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
5070 tbl
, tbl
->slots
, tbl
->max_slots
);
5072 dprintk("<-- %s: return %d\n", __func__
, ret
);
5077 * Initialize the forechannel and backchannel tables
5079 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
5081 struct nfs4_slot_table
*tbl
;
5084 tbl
= &session
->fc_slot_table
;
5085 if (tbl
->slots
== NULL
) {
5086 status
= nfs4_init_slot_table(tbl
,
5087 session
->fc_attrs
.max_reqs
, 1);
5092 tbl
= &session
->bc_slot_table
;
5093 if (tbl
->slots
== NULL
) {
5094 status
= nfs4_init_slot_table(tbl
,
5095 session
->bc_attrs
.max_reqs
, 0);
5097 nfs4_destroy_slot_tables(session
);
5103 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
5105 struct nfs4_session
*session
;
5106 struct nfs4_slot_table
*tbl
;
5108 session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
5112 tbl
= &session
->fc_slot_table
;
5113 tbl
->highest_used_slotid
= -1;
5114 spin_lock_init(&tbl
->slot_tbl_lock
);
5115 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
5116 init_completion(&tbl
->complete
);
5118 tbl
= &session
->bc_slot_table
;
5119 tbl
->highest_used_slotid
= -1;
5120 spin_lock_init(&tbl
->slot_tbl_lock
);
5121 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
5122 init_completion(&tbl
->complete
);
5124 session
->session_state
= 1<<NFS4_SESSION_INITING
;
5130 void nfs4_destroy_session(struct nfs4_session
*session
)
5132 nfs4_proc_destroy_session(session
);
5133 dprintk("%s Destroy backchannel for xprt %p\n",
5134 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
5135 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
5136 NFS41_BC_MIN_CALLBACKS
);
5137 nfs4_destroy_slot_tables(session
);
5142 * Initialize the values to be used by the client in CREATE_SESSION
5143 * If nfs4_init_session set the fore channel request and response sizes,
5146 * Set the back channel max_resp_sz_cached to zero to force the client to
5147 * always set csa_cachethis to FALSE because the current implementation
5148 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5150 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
5152 struct nfs4_session
*session
= args
->client
->cl_session
;
5153 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
5154 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
5157 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
5159 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
5160 /* Fore channel attributes */
5161 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
5162 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
5163 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
5164 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
5166 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5167 "max_ops=%u max_reqs=%u\n",
5169 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
5170 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
5172 /* Back channel attributes */
5173 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
5174 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
5175 args
->bc_attrs
.max_resp_sz_cached
= 0;
5176 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
5177 args
->bc_attrs
.max_reqs
= 1;
5179 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5180 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5182 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
5183 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
5184 args
->bc_attrs
.max_reqs
);
5187 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
5189 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
5190 struct nfs4_channel_attrs
*rcvd
= &session
->fc_attrs
;
5192 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
5195 * Our requested max_ops is the minimum we need; we're not
5196 * prepared to break up compounds into smaller pieces than that.
5197 * So, no point even trying to continue if the server won't
5200 if (rcvd
->max_ops
< sent
->max_ops
)
5202 if (rcvd
->max_reqs
== 0)
5207 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
5209 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
5210 struct nfs4_channel_attrs
*rcvd
= &session
->bc_attrs
;
5212 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
5214 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
5216 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
5218 /* These would render the backchannel useless: */
5219 if (rcvd
->max_ops
== 0)
5221 if (rcvd
->max_reqs
== 0)
5226 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
5227 struct nfs4_session
*session
)
5231 ret
= nfs4_verify_fore_channel_attrs(args
, session
);
5234 return nfs4_verify_back_channel_attrs(args
, session
);
5237 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
5239 struct nfs4_session
*session
= clp
->cl_session
;
5240 struct nfs41_create_session_args args
= {
5242 .cb_program
= NFS4_CALLBACK
,
5244 struct nfs41_create_session_res res
= {
5247 struct rpc_message msg
= {
5248 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
5254 nfs4_init_channel_attrs(&args
);
5255 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
5257 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5260 /* Verify the session's negotiated channel_attrs values */
5261 status
= nfs4_verify_channel_attrs(&args
, session
);
5263 /* Increment the clientid slot sequence id */
5271 * Issues a CREATE_SESSION operation to the server.
5272 * It is the responsibility of the caller to verify the session is
5273 * expired before calling this routine.
5275 int nfs4_proc_create_session(struct nfs_client
*clp
)
5279 struct nfs4_session
*session
= clp
->cl_session
;
5281 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
5283 status
= _nfs4_proc_create_session(clp
);
5287 /* Init and reset the fore channel */
5288 status
= nfs4_init_slot_tables(session
);
5289 dprintk("slot table initialization returned %d\n", status
);
5292 status
= nfs4_reset_slot_tables(session
);
5293 dprintk("slot table reset returned %d\n", status
);
5297 ptr
= (unsigned *)&session
->sess_id
.data
[0];
5298 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
5299 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
5301 dprintk("<-- %s\n", __func__
);
5306 * Issue the over-the-wire RPC DESTROY_SESSION.
5307 * The caller must serialize access to this routine.
5309 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
5312 struct rpc_message msg
;
5314 dprintk("--> nfs4_proc_destroy_session\n");
5316 /* session is still being setup */
5317 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
5320 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
5321 msg
.rpc_argp
= session
;
5322 msg
.rpc_resp
= NULL
;
5323 msg
.rpc_cred
= NULL
;
5324 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, RPC_TASK_TIMEOUT
);
5328 "Got error %d from the server on DESTROY_SESSION. "
5329 "Session has been destroyed regardless...\n", status
);
5331 dprintk("<-- nfs4_proc_destroy_session\n");
5335 int nfs4_init_session(struct nfs_server
*server
)
5337 struct nfs_client
*clp
= server
->nfs_client
;
5338 struct nfs4_session
*session
;
5339 unsigned int rsize
, wsize
;
5342 if (!nfs4_has_session(clp
))
5345 session
= clp
->cl_session
;
5346 if (!test_and_clear_bit(NFS4_SESSION_INITING
, &session
->session_state
))
5349 rsize
= server
->rsize
;
5351 rsize
= NFS_MAX_FILE_IO_SIZE
;
5352 wsize
= server
->wsize
;
5354 wsize
= NFS_MAX_FILE_IO_SIZE
;
5356 session
->fc_attrs
.max_rqst_sz
= wsize
+ nfs41_maxwrite_overhead
;
5357 session
->fc_attrs
.max_resp_sz
= rsize
+ nfs41_maxread_overhead
;
5359 ret
= nfs4_recover_expired_lease(server
);
5361 ret
= nfs4_check_client_ready(clp
);
5365 int nfs4_init_ds_session(struct nfs_client
*clp
)
5367 struct nfs4_session
*session
= clp
->cl_session
;
5370 if (!test_and_clear_bit(NFS4_SESSION_INITING
, &session
->session_state
))
5373 ret
= nfs4_client_recover_expired_lease(clp
);
5375 /* Test for the DS role */
5376 if (!is_ds_client(clp
))
5379 ret
= nfs4_check_client_ready(clp
);
5383 EXPORT_SYMBOL_GPL(nfs4_init_ds_session
);
5387 * Renew the cl_session lease.
5389 struct nfs4_sequence_data
{
5390 struct nfs_client
*clp
;
5391 struct nfs4_sequence_args args
;
5392 struct nfs4_sequence_res res
;
5395 static void nfs41_sequence_release(void *data
)
5397 struct nfs4_sequence_data
*calldata
= data
;
5398 struct nfs_client
*clp
= calldata
->clp
;
5400 if (atomic_read(&clp
->cl_count
) > 1)
5401 nfs4_schedule_state_renewal(clp
);
5402 nfs_put_client(clp
);
5406 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5408 switch(task
->tk_status
) {
5409 case -NFS4ERR_DELAY
:
5410 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5413 nfs4_schedule_lease_recovery(clp
);
5418 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5420 struct nfs4_sequence_data
*calldata
= data
;
5421 struct nfs_client
*clp
= calldata
->clp
;
5423 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
5426 if (task
->tk_status
< 0) {
5427 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5428 if (atomic_read(&clp
->cl_count
) == 1)
5431 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
5432 rpc_restart_call_prepare(task
);
5436 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5438 dprintk("<-- %s\n", __func__
);
5441 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5443 struct nfs4_sequence_data
*calldata
= data
;
5444 struct nfs_client
*clp
= calldata
->clp
;
5445 struct nfs4_sequence_args
*args
;
5446 struct nfs4_sequence_res
*res
;
5448 args
= task
->tk_msg
.rpc_argp
;
5449 res
= task
->tk_msg
.rpc_resp
;
5451 if (nfs41_setup_sequence(clp
->cl_session
, args
, res
, 0, task
))
5453 rpc_call_start(task
);
5456 static const struct rpc_call_ops nfs41_sequence_ops
= {
5457 .rpc_call_done
= nfs41_sequence_call_done
,
5458 .rpc_call_prepare
= nfs41_sequence_prepare
,
5459 .rpc_release
= nfs41_sequence_release
,
5462 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5464 struct nfs4_sequence_data
*calldata
;
5465 struct rpc_message msg
= {
5466 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5469 struct rpc_task_setup task_setup_data
= {
5470 .rpc_client
= clp
->cl_rpcclient
,
5471 .rpc_message
= &msg
,
5472 .callback_ops
= &nfs41_sequence_ops
,
5473 .flags
= RPC_TASK_ASYNC
| RPC_TASK_SOFT
,
5476 if (!atomic_inc_not_zero(&clp
->cl_count
))
5477 return ERR_PTR(-EIO
);
5478 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5479 if (calldata
== NULL
) {
5480 nfs_put_client(clp
);
5481 return ERR_PTR(-ENOMEM
);
5483 msg
.rpc_argp
= &calldata
->args
;
5484 msg
.rpc_resp
= &calldata
->res
;
5485 calldata
->clp
= clp
;
5486 task_setup_data
.callback_data
= calldata
;
5488 return rpc_run_task(&task_setup_data
);
5491 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5493 struct rpc_task
*task
;
5496 task
= _nfs41_proc_sequence(clp
, cred
);
5498 ret
= PTR_ERR(task
);
5500 rpc_put_task_async(task
);
5501 dprintk("<-- %s status=%d\n", __func__
, ret
);
5505 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5507 struct rpc_task
*task
;
5510 task
= _nfs41_proc_sequence(clp
, cred
);
5512 ret
= PTR_ERR(task
);
5515 ret
= rpc_wait_for_completion_task(task
);
5517 struct nfs4_sequence_res
*res
= task
->tk_msg
.rpc_resp
;
5519 if (task
->tk_status
== 0)
5520 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
5521 ret
= task
->tk_status
;
5525 dprintk("<-- %s status=%d\n", __func__
, ret
);
5529 struct nfs4_reclaim_complete_data
{
5530 struct nfs_client
*clp
;
5531 struct nfs41_reclaim_complete_args arg
;
5532 struct nfs41_reclaim_complete_res res
;
5535 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5537 struct nfs4_reclaim_complete_data
*calldata
= data
;
5539 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
5540 if (nfs41_setup_sequence(calldata
->clp
->cl_session
,
5541 &calldata
->arg
.seq_args
,
5542 &calldata
->res
.seq_res
, 0, task
))
5545 rpc_call_start(task
);
5548 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5550 switch(task
->tk_status
) {
5552 case -NFS4ERR_COMPLETE_ALREADY
:
5553 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
5555 case -NFS4ERR_DELAY
:
5556 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5558 case -NFS4ERR_RETRY_UNCACHED_REP
:
5561 nfs4_schedule_lease_recovery(clp
);
5566 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5568 struct nfs4_reclaim_complete_data
*calldata
= data
;
5569 struct nfs_client
*clp
= calldata
->clp
;
5570 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5572 dprintk("--> %s\n", __func__
);
5573 if (!nfs41_sequence_done(task
, res
))
5576 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
5577 rpc_restart_call_prepare(task
);
5580 dprintk("<-- %s\n", __func__
);
5583 static void nfs4_free_reclaim_complete_data(void *data
)
5585 struct nfs4_reclaim_complete_data
*calldata
= data
;
5590 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5591 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5592 .rpc_call_done
= nfs4_reclaim_complete_done
,
5593 .rpc_release
= nfs4_free_reclaim_complete_data
,
5597 * Issue a global reclaim complete.
5599 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5601 struct nfs4_reclaim_complete_data
*calldata
;
5602 struct rpc_task
*task
;
5603 struct rpc_message msg
= {
5604 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5606 struct rpc_task_setup task_setup_data
= {
5607 .rpc_client
= clp
->cl_rpcclient
,
5608 .rpc_message
= &msg
,
5609 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5610 .flags
= RPC_TASK_ASYNC
,
5612 int status
= -ENOMEM
;
5614 dprintk("--> %s\n", __func__
);
5615 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5616 if (calldata
== NULL
)
5618 calldata
->clp
= clp
;
5619 calldata
->arg
.one_fs
= 0;
5621 msg
.rpc_argp
= &calldata
->arg
;
5622 msg
.rpc_resp
= &calldata
->res
;
5623 task_setup_data
.callback_data
= calldata
;
5624 task
= rpc_run_task(&task_setup_data
);
5626 status
= PTR_ERR(task
);
5629 status
= nfs4_wait_for_completion_rpc_task(task
);
5631 status
= task
->tk_status
;
5635 dprintk("<-- %s status=%d\n", __func__
, status
);
5640 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
5642 struct nfs4_layoutget
*lgp
= calldata
;
5643 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
5645 dprintk("--> %s\n", __func__
);
5646 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5647 * right now covering the LAYOUTGET we are about to send.
5648 * However, that is not so catastrophic, and there seems
5649 * to be no way to prevent it completely.
5651 if (nfs4_setup_sequence(server
, &lgp
->args
.seq_args
,
5652 &lgp
->res
.seq_res
, 0, task
))
5654 if (pnfs_choose_layoutget_stateid(&lgp
->args
.stateid
,
5655 NFS_I(lgp
->args
.inode
)->layout
,
5656 lgp
->args
.ctx
->state
)) {
5657 rpc_exit(task
, NFS4_OK
);
5660 rpc_call_start(task
);
5663 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
5665 struct nfs4_layoutget
*lgp
= calldata
;
5666 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
5668 dprintk("--> %s\n", __func__
);
5670 if (!nfs4_sequence_done(task
, &lgp
->res
.seq_res
))
5673 switch (task
->tk_status
) {
5676 case -NFS4ERR_LAYOUTTRYLATER
:
5677 case -NFS4ERR_RECALLCONFLICT
:
5678 task
->tk_status
= -NFS4ERR_DELAY
;
5681 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
5682 rpc_restart_call_prepare(task
);
5686 dprintk("<-- %s\n", __func__
);
5689 static void nfs4_layoutget_release(void *calldata
)
5691 struct nfs4_layoutget
*lgp
= calldata
;
5693 dprintk("--> %s\n", __func__
);
5694 put_nfs_open_context(lgp
->args
.ctx
);
5696 dprintk("<-- %s\n", __func__
);
5699 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
5700 .rpc_call_prepare
= nfs4_layoutget_prepare
,
5701 .rpc_call_done
= nfs4_layoutget_done
,
5702 .rpc_release
= nfs4_layoutget_release
,
5705 int nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
)
5707 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
5708 struct rpc_task
*task
;
5709 struct rpc_message msg
= {
5710 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
5711 .rpc_argp
= &lgp
->args
,
5712 .rpc_resp
= &lgp
->res
,
5714 struct rpc_task_setup task_setup_data
= {
5715 .rpc_client
= server
->client
,
5716 .rpc_message
= &msg
,
5717 .callback_ops
= &nfs4_layoutget_call_ops
,
5718 .callback_data
= lgp
,
5719 .flags
= RPC_TASK_ASYNC
,
5723 dprintk("--> %s\n", __func__
);
5725 lgp
->res
.layoutp
= &lgp
->args
.layout
;
5726 lgp
->res
.seq_res
.sr_slot
= NULL
;
5727 task
= rpc_run_task(&task_setup_data
);
5729 return PTR_ERR(task
);
5730 status
= nfs4_wait_for_completion_rpc_task(task
);
5732 status
= task
->tk_status
;
5734 status
= pnfs_layout_process(lgp
);
5736 dprintk("<-- %s status=%d\n", __func__
, status
);
5741 nfs4_layoutreturn_prepare(struct rpc_task
*task
, void *calldata
)
5743 struct nfs4_layoutreturn
*lrp
= calldata
;
5745 dprintk("--> %s\n", __func__
);
5746 if (nfs41_setup_sequence(lrp
->clp
->cl_session
, &lrp
->args
.seq_args
,
5747 &lrp
->res
.seq_res
, 0, task
))
5749 rpc_call_start(task
);
5752 static void nfs4_layoutreturn_done(struct rpc_task
*task
, void *calldata
)
5754 struct nfs4_layoutreturn
*lrp
= calldata
;
5755 struct nfs_server
*server
;
5756 struct pnfs_layout_hdr
*lo
= NFS_I(lrp
->args
.inode
)->layout
;
5758 dprintk("--> %s\n", __func__
);
5760 if (!nfs4_sequence_done(task
, &lrp
->res
.seq_res
))
5763 server
= NFS_SERVER(lrp
->args
.inode
);
5764 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
5765 nfs_restart_rpc(task
, lrp
->clp
);
5768 spin_lock(&lo
->plh_inode
->i_lock
);
5769 if (task
->tk_status
== 0) {
5770 if (lrp
->res
.lrs_present
) {
5771 pnfs_set_layout_stateid(lo
, &lrp
->res
.stateid
, true);
5773 BUG_ON(!list_empty(&lo
->plh_segs
));
5775 lo
->plh_block_lgets
--;
5776 spin_unlock(&lo
->plh_inode
->i_lock
);
5777 dprintk("<-- %s\n", __func__
);
5780 static void nfs4_layoutreturn_release(void *calldata
)
5782 struct nfs4_layoutreturn
*lrp
= calldata
;
5784 dprintk("--> %s\n", __func__
);
5785 put_layout_hdr(NFS_I(lrp
->args
.inode
)->layout
);
5787 dprintk("<-- %s\n", __func__
);
5790 static const struct rpc_call_ops nfs4_layoutreturn_call_ops
= {
5791 .rpc_call_prepare
= nfs4_layoutreturn_prepare
,
5792 .rpc_call_done
= nfs4_layoutreturn_done
,
5793 .rpc_release
= nfs4_layoutreturn_release
,
5796 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn
*lrp
)
5798 struct rpc_task
*task
;
5799 struct rpc_message msg
= {
5800 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTRETURN
],
5801 .rpc_argp
= &lrp
->args
,
5802 .rpc_resp
= &lrp
->res
,
5804 struct rpc_task_setup task_setup_data
= {
5805 .rpc_client
= lrp
->clp
->cl_rpcclient
,
5806 .rpc_message
= &msg
,
5807 .callback_ops
= &nfs4_layoutreturn_call_ops
,
5808 .callback_data
= lrp
,
5812 dprintk("--> %s\n", __func__
);
5813 task
= rpc_run_task(&task_setup_data
);
5815 return PTR_ERR(task
);
5816 status
= task
->tk_status
;
5817 dprintk("<-- %s status=%d\n", __func__
, status
);
5823 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
, struct pnfs_device
*pdev
)
5825 struct nfs4_getdeviceinfo_args args
= {
5828 struct nfs4_getdeviceinfo_res res
= {
5831 struct rpc_message msg
= {
5832 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
5838 dprintk("--> %s\n", __func__
);
5839 status
= nfs4_call_sync(server
->client
, server
, &msg
, &args
.seq_args
, &res
.seq_res
, 0);
5840 dprintk("<-- %s status=%d\n", __func__
, status
);
5845 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
, struct pnfs_device
*pdev
)
5847 struct nfs4_exception exception
= { };
5851 err
= nfs4_handle_exception(server
,
5852 _nfs4_proc_getdeviceinfo(server
, pdev
),
5854 } while (exception
.retry
);
5857 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
5859 static void nfs4_layoutcommit_prepare(struct rpc_task
*task
, void *calldata
)
5861 struct nfs4_layoutcommit_data
*data
= calldata
;
5862 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
5864 if (nfs4_setup_sequence(server
, &data
->args
.seq_args
,
5865 &data
->res
.seq_res
, 1, task
))
5867 rpc_call_start(task
);
5871 nfs4_layoutcommit_done(struct rpc_task
*task
, void *calldata
)
5873 struct nfs4_layoutcommit_data
*data
= calldata
;
5874 struct nfs_server
*server
= NFS_SERVER(data
->args
.inode
);
5876 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
5879 switch (task
->tk_status
) { /* Just ignore these failures */
5880 case NFS4ERR_DELEG_REVOKED
: /* layout was recalled */
5881 case NFS4ERR_BADIOMODE
: /* no IOMODE_RW layout for range */
5882 case NFS4ERR_BADLAYOUT
: /* no layout */
5883 case NFS4ERR_GRACE
: /* loca_recalim always false */
5884 task
->tk_status
= 0;
5887 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
5888 nfs_restart_rpc(task
, server
->nfs_client
);
5892 if (task
->tk_status
== 0)
5893 nfs_post_op_update_inode_force_wcc(data
->args
.inode
,
5897 static void nfs4_layoutcommit_release(void *calldata
)
5899 struct nfs4_layoutcommit_data
*data
= calldata
;
5900 struct pnfs_layout_segment
*lseg
, *tmp
;
5902 /* Matched by references in pnfs_set_layoutcommit */
5903 list_for_each_entry_safe(lseg
, tmp
, &data
->lseg_list
, pls_lc_list
) {
5904 list_del_init(&lseg
->pls_lc_list
);
5905 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT
,
5909 put_rpccred(data
->cred
);
5913 static const struct rpc_call_ops nfs4_layoutcommit_ops
= {
5914 .rpc_call_prepare
= nfs4_layoutcommit_prepare
,
5915 .rpc_call_done
= nfs4_layoutcommit_done
,
5916 .rpc_release
= nfs4_layoutcommit_release
,
5920 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data
*data
, bool sync
)
5922 struct rpc_message msg
= {
5923 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTCOMMIT
],
5924 .rpc_argp
= &data
->args
,
5925 .rpc_resp
= &data
->res
,
5926 .rpc_cred
= data
->cred
,
5928 struct rpc_task_setup task_setup_data
= {
5929 .task
= &data
->task
,
5930 .rpc_client
= NFS_CLIENT(data
->args
.inode
),
5931 .rpc_message
= &msg
,
5932 .callback_ops
= &nfs4_layoutcommit_ops
,
5933 .callback_data
= data
,
5934 .flags
= RPC_TASK_ASYNC
,
5936 struct rpc_task
*task
;
5939 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5940 "lbw: %llu inode %lu\n",
5941 data
->task
.tk_pid
, sync
,
5942 data
->args
.lastbytewritten
,
5943 data
->args
.inode
->i_ino
);
5945 task
= rpc_run_task(&task_setup_data
);
5947 return PTR_ERR(task
);
5950 status
= nfs4_wait_for_completion_rpc_task(task
);
5953 status
= task
->tk_status
;
5955 dprintk("%s: status %d\n", __func__
, status
);
5959 #endif /* CONFIG_NFS_V4_1 */
5961 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5962 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5963 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5964 .recover_open
= nfs4_open_reclaim
,
5965 .recover_lock
= nfs4_lock_reclaim
,
5966 .establish_clid
= nfs4_init_clientid
,
5967 .get_clid_cred
= nfs4_get_setclientid_cred
,
5970 #if defined(CONFIG_NFS_V4_1)
5971 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5972 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5973 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5974 .recover_open
= nfs4_open_reclaim
,
5975 .recover_lock
= nfs4_lock_reclaim
,
5976 .establish_clid
= nfs41_init_clientid
,
5977 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5978 .reclaim_complete
= nfs41_proc_reclaim_complete
,
5980 #endif /* CONFIG_NFS_V4_1 */
5982 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5983 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5984 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5985 .recover_open
= nfs4_open_expired
,
5986 .recover_lock
= nfs4_lock_expired
,
5987 .establish_clid
= nfs4_init_clientid
,
5988 .get_clid_cred
= nfs4_get_setclientid_cred
,
5991 #if defined(CONFIG_NFS_V4_1)
5992 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5993 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5994 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5995 .recover_open
= nfs4_open_expired
,
5996 .recover_lock
= nfs4_lock_expired
,
5997 .establish_clid
= nfs41_init_clientid
,
5998 .get_clid_cred
= nfs4_get_exchange_id_cred
,
6000 #endif /* CONFIG_NFS_V4_1 */
6002 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
6003 .sched_state_renewal
= nfs4_proc_async_renew
,
6004 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
6005 .renew_lease
= nfs4_proc_renew
,
6008 #if defined(CONFIG_NFS_V4_1)
6009 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
6010 .sched_state_renewal
= nfs41_proc_async_sequence
,
6011 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
6012 .renew_lease
= nfs4_proc_sequence
,
6016 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
6018 .call_sync
= _nfs4_call_sync
,
6019 .validate_stateid
= nfs4_validate_delegation_stateid
,
6020 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
6021 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
6022 .state_renewal_ops
= &nfs40_state_renewal_ops
,
6025 #if defined(CONFIG_NFS_V4_1)
6026 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
6028 .call_sync
= _nfs4_call_sync_session
,
6029 .validate_stateid
= nfs41_validate_delegation_stateid
,
6030 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
6031 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
6032 .state_renewal_ops
= &nfs41_state_renewal_ops
,
6036 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
6037 [0] = &nfs_v4_0_minor_ops
,
6038 #if defined(CONFIG_NFS_V4_1)
6039 [1] = &nfs_v4_1_minor_ops
,
6043 static const struct inode_operations nfs4_file_inode_operations
= {
6044 .permission
= nfs_permission
,
6045 .getattr
= nfs_getattr
,
6046 .setattr
= nfs_setattr
,
6047 .getxattr
= generic_getxattr
,
6048 .setxattr
= generic_setxattr
,
6049 .listxattr
= generic_listxattr
,
6050 .removexattr
= generic_removexattr
,
6053 const struct nfs_rpc_ops nfs_v4_clientops
= {
6054 .version
= 4, /* protocol version */
6055 .dentry_ops
= &nfs4_dentry_operations
,
6056 .dir_inode_ops
= &nfs4_dir_inode_operations
,
6057 .file_inode_ops
= &nfs4_file_inode_operations
,
6058 .file_ops
= &nfs4_file_operations
,
6059 .getroot
= nfs4_proc_get_root
,
6060 .getattr
= nfs4_proc_getattr
,
6061 .setattr
= nfs4_proc_setattr
,
6062 .lookupfh
= nfs4_proc_lookupfh
,
6063 .lookup
= nfs4_proc_lookup
,
6064 .access
= nfs4_proc_access
,
6065 .readlink
= nfs4_proc_readlink
,
6066 .create
= nfs4_proc_create
,
6067 .remove
= nfs4_proc_remove
,
6068 .unlink_setup
= nfs4_proc_unlink_setup
,
6069 .unlink_done
= nfs4_proc_unlink_done
,
6070 .rename
= nfs4_proc_rename
,
6071 .rename_setup
= nfs4_proc_rename_setup
,
6072 .rename_done
= nfs4_proc_rename_done
,
6073 .link
= nfs4_proc_link
,
6074 .symlink
= nfs4_proc_symlink
,
6075 .mkdir
= nfs4_proc_mkdir
,
6076 .rmdir
= nfs4_proc_remove
,
6077 .readdir
= nfs4_proc_readdir
,
6078 .mknod
= nfs4_proc_mknod
,
6079 .statfs
= nfs4_proc_statfs
,
6080 .fsinfo
= nfs4_proc_fsinfo
,
6081 .pathconf
= nfs4_proc_pathconf
,
6082 .set_capabilities
= nfs4_server_capabilities
,
6083 .decode_dirent
= nfs4_decode_dirent
,
6084 .read_setup
= nfs4_proc_read_setup
,
6085 .read_done
= nfs4_read_done
,
6086 .write_setup
= nfs4_proc_write_setup
,
6087 .write_done
= nfs4_write_done
,
6088 .commit_setup
= nfs4_proc_commit_setup
,
6089 .commit_done
= nfs4_commit_done
,
6090 .lock
= nfs4_proc_lock
,
6091 .clear_acl_cache
= nfs4_zap_acl_attr
,
6092 .close_context
= nfs4_close_context
,
6093 .open_context
= nfs4_atomic_open
,
6094 .init_client
= nfs4_init_client
,
6095 .secinfo
= nfs4_proc_secinfo
,
6098 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler
= {
6099 .prefix
= XATTR_NAME_NFSV4_ACL
,
6100 .list
= nfs4_xattr_list_nfs4_acl
,
6101 .get
= nfs4_xattr_get_nfs4_acl
,
6102 .set
= nfs4_xattr_set_nfs4_acl
,
6105 const struct xattr_handler
*nfs4_xattr_handlers
[] = {
6106 &nfs4_xattr_nfs4_acl_handler
,