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/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
54 #include "delegation.h"
60 #define NFSDBG_FACILITY NFSDBG_PROC
62 #define NFS4_POLL_RETRY_MIN (HZ/10)
63 #define NFS4_POLL_RETRY_MAX (15*HZ)
65 #define NFS4_MAX_LOOP_ON_RECOVER (10)
68 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
69 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
70 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
71 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
72 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
73 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
74 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
75 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
76 struct nfs4_state
*state
);
78 /* Prevent leaks of NFSv4 errors into userland */
79 static int nfs4_map_errors(int err
)
84 case -NFS4ERR_RESOURCE
:
87 dprintk("%s could not handle NFSv4 error %d\n",
95 * This is our standard bitmap for GETATTR requests.
97 const u32 nfs4_fattr_bitmap
[2] = {
102 | FATTR4_WORD0_FILEID
,
104 | FATTR4_WORD1_NUMLINKS
106 | FATTR4_WORD1_OWNER_GROUP
107 | FATTR4_WORD1_RAWDEV
108 | FATTR4_WORD1_SPACE_USED
109 | FATTR4_WORD1_TIME_ACCESS
110 | FATTR4_WORD1_TIME_METADATA
111 | FATTR4_WORD1_TIME_MODIFY
114 const u32 nfs4_statfs_bitmap
[2] = {
115 FATTR4_WORD0_FILES_AVAIL
116 | FATTR4_WORD0_FILES_FREE
117 | FATTR4_WORD0_FILES_TOTAL
,
118 FATTR4_WORD1_SPACE_AVAIL
119 | FATTR4_WORD1_SPACE_FREE
120 | FATTR4_WORD1_SPACE_TOTAL
123 const u32 nfs4_pathconf_bitmap
[2] = {
125 | FATTR4_WORD0_MAXNAME
,
129 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
130 | FATTR4_WORD0_MAXREAD
131 | FATTR4_WORD0_MAXWRITE
132 | FATTR4_WORD0_LEASE_TIME
,
133 FATTR4_WORD1_TIME_DELTA
134 | FATTR4_WORD1_FS_LAYOUT_TYPES
137 const u32 nfs4_fs_locations_bitmap
[2] = {
139 | FATTR4_WORD0_CHANGE
142 | FATTR4_WORD0_FILEID
143 | FATTR4_WORD0_FS_LOCATIONS
,
145 | FATTR4_WORD1_NUMLINKS
147 | FATTR4_WORD1_OWNER_GROUP
148 | FATTR4_WORD1_RAWDEV
149 | FATTR4_WORD1_SPACE_USED
150 | FATTR4_WORD1_TIME_ACCESS
151 | FATTR4_WORD1_TIME_METADATA
152 | FATTR4_WORD1_TIME_MODIFY
153 | FATTR4_WORD1_MOUNTED_ON_FILEID
156 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
157 struct nfs4_readdir_arg
*readdir
)
161 BUG_ON(readdir
->count
< 80);
163 readdir
->cookie
= cookie
;
164 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
169 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
174 * NFSv4 servers do not return entries for '.' and '..'
175 * Therefore, we fake these entries here. We let '.'
176 * have cookie 0 and '..' have cookie 1. Note that
177 * when talking to the server, we always send cookie 0
180 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
183 *p
++ = xdr_one
; /* next */
184 *p
++ = xdr_zero
; /* cookie, first word */
185 *p
++ = xdr_one
; /* cookie, second word */
186 *p
++ = xdr_one
; /* entry len */
187 memcpy(p
, ".\0\0\0", 4); /* entry */
189 *p
++ = xdr_one
; /* bitmap length */
190 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
191 *p
++ = htonl(8); /* attribute buffer length */
192 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
195 *p
++ = xdr_one
; /* next */
196 *p
++ = xdr_zero
; /* cookie, first word */
197 *p
++ = xdr_two
; /* cookie, second word */
198 *p
++ = xdr_two
; /* entry len */
199 memcpy(p
, "..\0\0", 4); /* entry */
201 *p
++ = xdr_one
; /* bitmap length */
202 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
203 *p
++ = htonl(8); /* attribute buffer length */
204 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
206 readdir
->pgbase
= (char *)p
- (char *)start
;
207 readdir
->count
-= readdir
->pgbase
;
208 kunmap_atomic(start
, KM_USER0
);
211 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
217 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
218 nfs_wait_bit_killable
, TASK_KILLABLE
);
222 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
229 *timeout
= NFS4_POLL_RETRY_MIN
;
230 if (*timeout
> NFS4_POLL_RETRY_MAX
)
231 *timeout
= NFS4_POLL_RETRY_MAX
;
232 schedule_timeout_killable(*timeout
);
233 if (fatal_signal_pending(current
))
239 /* This is the error handling routine for processes that are allowed
242 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
244 struct nfs_client
*clp
= server
->nfs_client
;
245 struct nfs4_state
*state
= exception
->state
;
248 exception
->retry
= 0;
252 case -NFS4ERR_ADMIN_REVOKED
:
253 case -NFS4ERR_BAD_STATEID
:
254 case -NFS4ERR_OPENMODE
:
257 nfs4_state_mark_reclaim_nograce(clp
, state
);
258 goto do_state_recovery
;
259 case -NFS4ERR_STALE_STATEID
:
260 case -NFS4ERR_STALE_CLIENTID
:
261 case -NFS4ERR_EXPIRED
:
262 goto do_state_recovery
;
263 #if defined(CONFIG_NFS_V4_1)
264 case -NFS4ERR_BADSESSION
:
265 case -NFS4ERR_BADSLOT
:
266 case -NFS4ERR_BAD_HIGH_SLOT
:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
268 case -NFS4ERR_DEADSESSION
:
269 case -NFS4ERR_SEQ_FALSE_RETRY
:
270 case -NFS4ERR_SEQ_MISORDERED
:
271 dprintk("%s ERROR: %d Reset session\n", __func__
,
273 nfs4_schedule_state_recovery(clp
);
274 exception
->retry
= 1;
276 #endif /* defined(CONFIG_NFS_V4_1) */
277 case -NFS4ERR_FILE_OPEN
:
278 if (exception
->timeout
> HZ
) {
279 /* We have retried a decent amount, time to
288 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
291 case -NFS4ERR_OLD_STATEID
:
292 exception
->retry
= 1;
294 /* We failed to handle the error */
295 return nfs4_map_errors(ret
);
297 nfs4_schedule_state_recovery(clp
);
298 ret
= nfs4_wait_clnt_recover(clp
);
300 exception
->retry
= 1;
305 static void do_renew_lease(struct nfs_client
*clp
, unsigned long timestamp
)
307 spin_lock(&clp
->cl_lock
);
308 if (time_before(clp
->cl_last_renewal
,timestamp
))
309 clp
->cl_last_renewal
= timestamp
;
310 spin_unlock(&clp
->cl_lock
);
313 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
315 do_renew_lease(server
->nfs_client
, timestamp
);
318 #if defined(CONFIG_NFS_V4_1)
321 * nfs4_free_slot - free a slot and efficiently update slot table.
323 * freeing a slot is trivially done by clearing its respective bit
325 * If the freed slotid equals highest_used_slotid we want to update it
326 * so that the server would be able to size down the slot table if needed,
327 * otherwise we know that the highest_used_slotid is still in use.
328 * When updating highest_used_slotid there may be "holes" in the bitmap
329 * so we need to scan down from highest_used_slotid to 0 looking for the now
330 * highest slotid in use.
331 * If none found, highest_used_slotid is set to -1.
333 * Must be called while holding tbl->slot_tbl_lock
336 nfs4_free_slot(struct nfs4_slot_table
*tbl
, struct nfs4_slot
*free_slot
)
338 int free_slotid
= free_slot
- tbl
->slots
;
339 int slotid
= free_slotid
;
341 BUG_ON(slotid
< 0 || slotid
>= NFS4_MAX_SLOT_TABLE
);
342 /* clear used bit in bitmap */
343 __clear_bit(slotid
, tbl
->used_slots
);
345 /* update highest_used_slotid when it is freed */
346 if (slotid
== tbl
->highest_used_slotid
) {
347 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
348 if (slotid
< tbl
->max_slots
)
349 tbl
->highest_used_slotid
= slotid
;
351 tbl
->highest_used_slotid
= -1;
353 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
354 free_slotid
, tbl
->highest_used_slotid
);
358 * Signal state manager thread if session is drained
360 static void nfs41_check_drain_session_complete(struct nfs4_session
*ses
)
362 struct rpc_task
*task
;
364 if (!test_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
)) {
365 task
= rpc_wake_up_next(&ses
->fc_slot_table
.slot_tbl_waitq
);
367 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
371 if (ses
->fc_slot_table
.highest_used_slotid
!= -1)
374 dprintk("%s COMPLETE: Session Drained\n", __func__
);
375 complete(&ses
->complete
);
378 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
380 struct nfs4_slot_table
*tbl
;
382 tbl
= &res
->sr_session
->fc_slot_table
;
384 /* just wake up the next guy waiting since
385 * we may have not consumed a slot after all */
386 dprintk("%s: No slot\n", __func__
);
390 spin_lock(&tbl
->slot_tbl_lock
);
391 nfs4_free_slot(tbl
, res
->sr_slot
);
392 nfs41_check_drain_session_complete(res
->sr_session
);
393 spin_unlock(&tbl
->slot_tbl_lock
);
397 static int nfs41_sequence_done(struct rpc_task
*task
, struct nfs4_sequence_res
*res
)
399 unsigned long timestamp
;
400 struct nfs_client
*clp
;
403 * sr_status remains 1 if an RPC level error occurred. The server
404 * may or may not have processed the sequence operation..
405 * Proceed as if the server received and processed the sequence
408 if (res
->sr_status
== 1)
409 res
->sr_status
= NFS_OK
;
411 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
415 /* Check the SEQUENCE operation status */
416 switch (res
->sr_status
) {
418 /* Update the slot's sequence and clientid lease timer */
419 ++res
->sr_slot
->seq_nr
;
420 timestamp
= res
->sr_renewal_time
;
421 clp
= res
->sr_session
->clp
;
422 do_renew_lease(clp
, timestamp
);
423 /* Check sequence flags */
424 if (atomic_read(&clp
->cl_count
) > 1)
425 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
428 /* The server detected a resend of the RPC call and
429 * returned NFS4ERR_DELAY as per Section 2.10.6.2
432 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
434 res
->sr_slot
- res
->sr_session
->fc_slot_table
.slots
,
435 res
->sr_slot
->seq_nr
);
438 /* Just update the slot sequence no. */
439 ++res
->sr_slot
->seq_nr
;
442 /* The session may be reset by one of the error handlers. */
443 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
444 nfs41_sequence_free_slot(res
);
447 if (!rpc_restart_call(task
))
449 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
453 static int nfs4_sequence_done(struct rpc_task
*task
,
454 struct nfs4_sequence_res
*res
)
456 if (res
->sr_session
== NULL
)
458 return nfs41_sequence_done(task
, res
);
462 * nfs4_find_slot - efficiently look for a free slot
464 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
465 * If found, we mark the slot as used, update the highest_used_slotid,
466 * and respectively set up the sequence operation args.
467 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
469 * Note: must be called with under the slot_tbl_lock.
472 nfs4_find_slot(struct nfs4_slot_table
*tbl
)
475 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
476 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
478 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
479 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
481 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
482 if (slotid
>= tbl
->max_slots
)
484 __set_bit(slotid
, tbl
->used_slots
);
485 if (slotid
> tbl
->highest_used_slotid
)
486 tbl
->highest_used_slotid
= slotid
;
489 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
490 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
494 static int nfs41_setup_sequence(struct nfs4_session
*session
,
495 struct nfs4_sequence_args
*args
,
496 struct nfs4_sequence_res
*res
,
498 struct rpc_task
*task
)
500 struct nfs4_slot
*slot
;
501 struct nfs4_slot_table
*tbl
;
504 dprintk("--> %s\n", __func__
);
505 /* slot already allocated? */
506 if (res
->sr_slot
!= NULL
)
509 tbl
= &session
->fc_slot_table
;
511 spin_lock(&tbl
->slot_tbl_lock
);
512 if (test_bit(NFS4_SESSION_DRAINING
, &session
->session_state
) &&
513 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
515 * The state manager will wait until the slot table is empty.
516 * Schedule the reset thread
518 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
519 spin_unlock(&tbl
->slot_tbl_lock
);
520 dprintk("%s Schedule Session Reset\n", __func__
);
524 if (!rpc_queue_empty(&tbl
->slot_tbl_waitq
) &&
525 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
526 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
527 spin_unlock(&tbl
->slot_tbl_lock
);
528 dprintk("%s enforce FIFO order\n", __func__
);
532 slotid
= nfs4_find_slot(tbl
);
533 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
534 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
535 spin_unlock(&tbl
->slot_tbl_lock
);
536 dprintk("<-- %s: no free slots\n", __func__
);
539 spin_unlock(&tbl
->slot_tbl_lock
);
541 rpc_task_set_priority(task
, RPC_PRIORITY_NORMAL
);
542 slot
= tbl
->slots
+ slotid
;
543 args
->sa_session
= session
;
544 args
->sa_slotid
= slotid
;
545 args
->sa_cache_this
= cache_reply
;
547 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
549 res
->sr_session
= session
;
551 res
->sr_renewal_time
= jiffies
;
552 res
->sr_status_flags
= 0;
554 * sr_status is only set in decode_sequence, and so will remain
555 * set to 1 if an rpc level failure occurs.
561 int nfs4_setup_sequence(const struct nfs_server
*server
,
562 struct nfs4_sequence_args
*args
,
563 struct nfs4_sequence_res
*res
,
565 struct rpc_task
*task
)
567 struct nfs4_session
*session
= nfs4_get_session(server
);
570 if (session
== NULL
) {
571 args
->sa_session
= NULL
;
572 res
->sr_session
= NULL
;
576 dprintk("--> %s clp %p session %p sr_slot %td\n",
577 __func__
, session
->clp
, session
, res
->sr_slot
?
578 res
->sr_slot
- session
->fc_slot_table
.slots
: -1);
580 ret
= nfs41_setup_sequence(session
, args
, res
, cache_reply
,
583 dprintk("<-- %s status=%d\n", __func__
, ret
);
587 struct nfs41_call_sync_data
{
588 const struct nfs_server
*seq_server
;
589 struct nfs4_sequence_args
*seq_args
;
590 struct nfs4_sequence_res
*seq_res
;
594 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
596 struct nfs41_call_sync_data
*data
= calldata
;
598 dprintk("--> %s data->seq_server %p\n", __func__
, data
->seq_server
);
600 if (nfs4_setup_sequence(data
->seq_server
, data
->seq_args
,
601 data
->seq_res
, data
->cache_reply
, task
))
603 rpc_call_start(task
);
606 static void nfs41_call_priv_sync_prepare(struct rpc_task
*task
, void *calldata
)
608 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
609 nfs41_call_sync_prepare(task
, calldata
);
612 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
614 struct nfs41_call_sync_data
*data
= calldata
;
616 nfs41_sequence_done(task
, data
->seq_res
);
619 struct rpc_call_ops nfs41_call_sync_ops
= {
620 .rpc_call_prepare
= nfs41_call_sync_prepare
,
621 .rpc_call_done
= nfs41_call_sync_done
,
624 struct rpc_call_ops nfs41_call_priv_sync_ops
= {
625 .rpc_call_prepare
= nfs41_call_priv_sync_prepare
,
626 .rpc_call_done
= nfs41_call_sync_done
,
629 static int nfs4_call_sync_sequence(struct nfs_server
*server
,
630 struct rpc_message
*msg
,
631 struct nfs4_sequence_args
*args
,
632 struct nfs4_sequence_res
*res
,
637 struct rpc_task
*task
;
638 struct nfs41_call_sync_data data
= {
639 .seq_server
= server
,
642 .cache_reply
= cache_reply
,
644 struct rpc_task_setup task_setup
= {
645 .rpc_client
= server
->client
,
647 .callback_ops
= &nfs41_call_sync_ops
,
648 .callback_data
= &data
653 task_setup
.callback_ops
= &nfs41_call_priv_sync_ops
;
654 task
= rpc_run_task(&task_setup
);
658 ret
= task
->tk_status
;
664 int _nfs4_call_sync_session(struct nfs_server
*server
,
665 struct rpc_message
*msg
,
666 struct nfs4_sequence_args
*args
,
667 struct nfs4_sequence_res
*res
,
670 return nfs4_call_sync_sequence(server
, msg
, args
, res
, cache_reply
, 0);
674 static int nfs4_sequence_done(struct rpc_task
*task
,
675 struct nfs4_sequence_res
*res
)
679 #endif /* CONFIG_NFS_V4_1 */
681 int _nfs4_call_sync(struct nfs_server
*server
,
682 struct rpc_message
*msg
,
683 struct nfs4_sequence_args
*args
,
684 struct nfs4_sequence_res
*res
,
687 args
->sa_session
= res
->sr_session
= NULL
;
688 return rpc_call_sync(server
->client
, msg
, 0);
691 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
692 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
693 &(res)->seq_res, (cache_reply))
695 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
697 struct nfs_inode
*nfsi
= NFS_I(dir
);
699 spin_lock(&dir
->i_lock
);
700 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
701 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
702 nfs_force_lookup_revalidate(dir
);
703 nfsi
->change_attr
= cinfo
->after
;
704 spin_unlock(&dir
->i_lock
);
707 struct nfs4_opendata
{
709 struct nfs_openargs o_arg
;
710 struct nfs_openres o_res
;
711 struct nfs_open_confirmargs c_arg
;
712 struct nfs_open_confirmres c_res
;
713 struct nfs_fattr f_attr
;
714 struct nfs_fattr dir_attr
;
717 struct nfs4_state_owner
*owner
;
718 struct nfs4_state
*state
;
720 unsigned long timestamp
;
721 unsigned int rpc_done
: 1;
727 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
729 p
->o_res
.f_attr
= &p
->f_attr
;
730 p
->o_res
.dir_attr
= &p
->dir_attr
;
731 p
->o_res
.seqid
= p
->o_arg
.seqid
;
732 p
->c_res
.seqid
= p
->c_arg
.seqid
;
733 p
->o_res
.server
= p
->o_arg
.server
;
734 nfs_fattr_init(&p
->f_attr
);
735 nfs_fattr_init(&p
->dir_attr
);
738 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
739 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
740 const struct iattr
*attrs
,
743 struct dentry
*parent
= dget_parent(path
->dentry
);
744 struct inode
*dir
= parent
->d_inode
;
745 struct nfs_server
*server
= NFS_SERVER(dir
);
746 struct nfs4_opendata
*p
;
748 p
= kzalloc(sizeof(*p
), gfp_mask
);
751 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
752 if (p
->o_arg
.seqid
== NULL
)
758 atomic_inc(&sp
->so_count
);
759 p
->o_arg
.fh
= NFS_FH(dir
);
760 p
->o_arg
.open_flags
= flags
;
761 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
762 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
763 p
->o_arg
.id
= sp
->so_owner_id
.id
;
764 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
765 p
->o_arg
.server
= server
;
766 p
->o_arg
.bitmask
= server
->attr_bitmask
;
767 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
768 if (flags
& O_CREAT
) {
771 p
->o_arg
.u
.attrs
= &p
->attrs
;
772 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
773 s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
777 p
->c_arg
.fh
= &p
->o_res
.fh
;
778 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
779 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
780 nfs4_init_opendata_res(p
);
790 static void nfs4_opendata_free(struct kref
*kref
)
792 struct nfs4_opendata
*p
= container_of(kref
,
793 struct nfs4_opendata
, kref
);
795 nfs_free_seqid(p
->o_arg
.seqid
);
796 if (p
->state
!= NULL
)
797 nfs4_put_open_state(p
->state
);
798 nfs4_put_state_owner(p
->owner
);
804 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
807 kref_put(&p
->kref
, nfs4_opendata_free
);
810 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
814 ret
= rpc_wait_for_completion_task(task
);
818 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
822 if (open_mode
& O_EXCL
)
824 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
826 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
827 && state
->n_rdonly
!= 0;
830 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
831 && state
->n_wronly
!= 0;
833 case FMODE_READ
|FMODE_WRITE
:
834 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
835 && state
->n_rdwr
!= 0;
841 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
843 if ((delegation
->type
& fmode
) != fmode
)
845 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
847 nfs_mark_delegation_referenced(delegation
);
851 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
860 case FMODE_READ
|FMODE_WRITE
:
863 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
866 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
868 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
869 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
870 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
873 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
876 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
878 case FMODE_READ
|FMODE_WRITE
:
879 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
883 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
885 write_seqlock(&state
->seqlock
);
886 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
887 write_sequnlock(&state
->seqlock
);
890 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
893 * Protect the call to nfs4_state_set_mode_locked and
894 * serialise the stateid update
896 write_seqlock(&state
->seqlock
);
897 if (deleg_stateid
!= NULL
) {
898 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
899 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
901 if (open_stateid
!= NULL
)
902 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
903 write_sequnlock(&state
->seqlock
);
904 spin_lock(&state
->owner
->so_lock
);
905 update_open_stateflags(state
, fmode
);
906 spin_unlock(&state
->owner
->so_lock
);
909 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
911 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
912 struct nfs_delegation
*deleg_cur
;
915 fmode
&= (FMODE_READ
|FMODE_WRITE
);
918 deleg_cur
= rcu_dereference(nfsi
->delegation
);
919 if (deleg_cur
== NULL
)
922 spin_lock(&deleg_cur
->lock
);
923 if (nfsi
->delegation
!= deleg_cur
||
924 (deleg_cur
->type
& fmode
) != fmode
)
925 goto no_delegation_unlock
;
927 if (delegation
== NULL
)
928 delegation
= &deleg_cur
->stateid
;
929 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
930 goto no_delegation_unlock
;
932 nfs_mark_delegation_referenced(deleg_cur
);
933 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
935 no_delegation_unlock
:
936 spin_unlock(&deleg_cur
->lock
);
940 if (!ret
&& open_stateid
!= NULL
) {
941 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
949 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
951 struct nfs_delegation
*delegation
;
954 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
955 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
960 nfs_inode_return_delegation(inode
);
963 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
965 struct nfs4_state
*state
= opendata
->state
;
966 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
967 struct nfs_delegation
*delegation
;
968 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
969 fmode_t fmode
= opendata
->o_arg
.fmode
;
970 nfs4_stateid stateid
;
974 if (can_open_cached(state
, fmode
, open_mode
)) {
975 spin_lock(&state
->owner
->so_lock
);
976 if (can_open_cached(state
, fmode
, open_mode
)) {
977 update_open_stateflags(state
, fmode
);
978 spin_unlock(&state
->owner
->so_lock
);
979 goto out_return_state
;
981 spin_unlock(&state
->owner
->so_lock
);
984 delegation
= rcu_dereference(nfsi
->delegation
);
985 if (delegation
== NULL
||
986 !can_open_delegated(delegation
, fmode
)) {
990 /* Save the delegation */
991 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
993 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
998 /* Try to update the stateid using the delegation */
999 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
1000 goto out_return_state
;
1003 return ERR_PTR(ret
);
1005 atomic_inc(&state
->count
);
1009 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
1011 struct inode
*inode
;
1012 struct nfs4_state
*state
= NULL
;
1013 struct nfs_delegation
*delegation
;
1016 if (!data
->rpc_done
) {
1017 state
= nfs4_try_open_cached(data
);
1022 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1024 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
1025 ret
= PTR_ERR(inode
);
1029 state
= nfs4_get_open_state(inode
, data
->owner
);
1032 if (data
->o_res
.delegation_type
!= 0) {
1033 int delegation_flags
= 0;
1036 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1038 delegation_flags
= delegation
->flags
;
1040 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1041 nfs_inode_set_delegation(state
->inode
,
1042 data
->owner
->so_cred
,
1045 nfs_inode_reclaim_delegation(state
->inode
,
1046 data
->owner
->so_cred
,
1050 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1058 return ERR_PTR(ret
);
1061 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1063 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1064 struct nfs_open_context
*ctx
;
1066 spin_lock(&state
->inode
->i_lock
);
1067 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1068 if (ctx
->state
!= state
)
1070 get_nfs_open_context(ctx
);
1071 spin_unlock(&state
->inode
->i_lock
);
1074 spin_unlock(&state
->inode
->i_lock
);
1075 return ERR_PTR(-ENOENT
);
1078 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1080 struct nfs4_opendata
*opendata
;
1082 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
, GFP_NOFS
);
1083 if (opendata
== NULL
)
1084 return ERR_PTR(-ENOMEM
);
1085 opendata
->state
= state
;
1086 atomic_inc(&state
->count
);
1090 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1092 struct nfs4_state
*newstate
;
1095 opendata
->o_arg
.open_flags
= 0;
1096 opendata
->o_arg
.fmode
= fmode
;
1097 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1098 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1099 nfs4_init_opendata_res(opendata
);
1100 ret
= _nfs4_recover_proc_open(opendata
);
1103 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1104 if (IS_ERR(newstate
))
1105 return PTR_ERR(newstate
);
1106 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1111 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1113 struct nfs4_state
*newstate
;
1116 /* memory barrier prior to reading state->n_* */
1117 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1119 if (state
->n_rdwr
!= 0) {
1120 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1121 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1124 if (newstate
!= state
)
1127 if (state
->n_wronly
!= 0) {
1128 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1129 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1132 if (newstate
!= state
)
1135 if (state
->n_rdonly
!= 0) {
1136 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1137 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1140 if (newstate
!= state
)
1144 * We may have performed cached opens for all three recoveries.
1145 * Check if we need to update the current stateid.
1147 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1148 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1149 write_seqlock(&state
->seqlock
);
1150 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1151 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1152 write_sequnlock(&state
->seqlock
);
1159 * reclaim state on the server after a reboot.
1161 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1163 struct nfs_delegation
*delegation
;
1164 struct nfs4_opendata
*opendata
;
1165 fmode_t delegation_type
= 0;
1168 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1169 if (IS_ERR(opendata
))
1170 return PTR_ERR(opendata
);
1171 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1172 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1174 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1175 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1176 delegation_type
= delegation
->type
;
1178 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1179 status
= nfs4_open_recover(opendata
, state
);
1180 nfs4_opendata_put(opendata
);
1184 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1186 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1187 struct nfs4_exception exception
= { };
1190 err
= _nfs4_do_open_reclaim(ctx
, state
);
1191 if (err
!= -NFS4ERR_DELAY
)
1193 nfs4_handle_exception(server
, err
, &exception
);
1194 } while (exception
.retry
);
1198 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1200 struct nfs_open_context
*ctx
;
1203 ctx
= nfs4_state_find_open_context(state
);
1205 return PTR_ERR(ctx
);
1206 ret
= nfs4_do_open_reclaim(ctx
, state
);
1207 put_nfs_open_context(ctx
);
1211 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1213 struct nfs4_opendata
*opendata
;
1216 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1217 if (IS_ERR(opendata
))
1218 return PTR_ERR(opendata
);
1219 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1220 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1221 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1222 ret
= nfs4_open_recover(opendata
, state
);
1223 nfs4_opendata_put(opendata
);
1227 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1229 struct nfs4_exception exception
= { };
1230 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1233 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1239 case -NFS4ERR_BADSESSION
:
1240 case -NFS4ERR_BADSLOT
:
1241 case -NFS4ERR_BAD_HIGH_SLOT
:
1242 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1243 case -NFS4ERR_DEADSESSION
:
1244 nfs4_schedule_state_recovery(
1245 server
->nfs_client
);
1247 case -NFS4ERR_STALE_CLIENTID
:
1248 case -NFS4ERR_STALE_STATEID
:
1249 case -NFS4ERR_EXPIRED
:
1250 /* Don't recall a delegation if it was lost */
1251 nfs4_schedule_state_recovery(server
->nfs_client
);
1255 * The show must go on: exit, but mark the
1256 * stateid as needing recovery.
1258 case -NFS4ERR_ADMIN_REVOKED
:
1259 case -NFS4ERR_BAD_STATEID
:
1260 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
1263 * User RPCSEC_GSS context has expired.
1264 * We cannot recover this stateid now, so
1265 * skip it and allow recovery thread to
1272 err
= nfs4_handle_exception(server
, err
, &exception
);
1273 } while (exception
.retry
);
1278 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1280 struct nfs4_opendata
*data
= calldata
;
1282 data
->rpc_status
= task
->tk_status
;
1283 if (data
->rpc_status
== 0) {
1284 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1285 sizeof(data
->o_res
.stateid
.data
));
1286 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1287 renew_lease(data
->o_res
.server
, data
->timestamp
);
1292 static void nfs4_open_confirm_release(void *calldata
)
1294 struct nfs4_opendata
*data
= calldata
;
1295 struct nfs4_state
*state
= NULL
;
1297 /* If this request hasn't been cancelled, do nothing */
1298 if (data
->cancelled
== 0)
1300 /* In case of error, no cleanup! */
1301 if (!data
->rpc_done
)
1303 state
= nfs4_opendata_to_nfs4_state(data
);
1305 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1307 nfs4_opendata_put(data
);
1310 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1311 .rpc_call_done
= nfs4_open_confirm_done
,
1312 .rpc_release
= nfs4_open_confirm_release
,
1316 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1318 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1320 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1321 struct rpc_task
*task
;
1322 struct rpc_message msg
= {
1323 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1324 .rpc_argp
= &data
->c_arg
,
1325 .rpc_resp
= &data
->c_res
,
1326 .rpc_cred
= data
->owner
->so_cred
,
1328 struct rpc_task_setup task_setup_data
= {
1329 .rpc_client
= server
->client
,
1330 .rpc_message
= &msg
,
1331 .callback_ops
= &nfs4_open_confirm_ops
,
1332 .callback_data
= data
,
1333 .workqueue
= nfsiod_workqueue
,
1334 .flags
= RPC_TASK_ASYNC
,
1338 kref_get(&data
->kref
);
1340 data
->rpc_status
= 0;
1341 data
->timestamp
= jiffies
;
1342 task
= rpc_run_task(&task_setup_data
);
1344 return PTR_ERR(task
);
1345 status
= nfs4_wait_for_completion_rpc_task(task
);
1347 data
->cancelled
= 1;
1350 status
= data
->rpc_status
;
1355 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1357 struct nfs4_opendata
*data
= calldata
;
1358 struct nfs4_state_owner
*sp
= data
->owner
;
1360 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1363 * Check if we still need to send an OPEN call, or if we can use
1364 * a delegation instead.
1366 if (data
->state
!= NULL
) {
1367 struct nfs_delegation
*delegation
;
1369 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1372 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1373 if (delegation
!= NULL
&&
1374 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1380 /* Update sequence id. */
1381 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1382 data
->o_arg
.clientid
= sp
->so_server
->nfs_client
->cl_clientid
;
1383 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1384 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1385 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1387 data
->timestamp
= jiffies
;
1388 if (nfs4_setup_sequence(data
->o_arg
.server
,
1389 &data
->o_arg
.seq_args
,
1390 &data
->o_res
.seq_res
, 1, task
))
1392 rpc_call_start(task
);
1395 task
->tk_action
= NULL
;
1399 static void nfs4_recover_open_prepare(struct rpc_task
*task
, void *calldata
)
1401 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
1402 nfs4_open_prepare(task
, calldata
);
1405 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1407 struct nfs4_opendata
*data
= calldata
;
1409 data
->rpc_status
= task
->tk_status
;
1411 if (!nfs4_sequence_done(task
, &data
->o_res
.seq_res
))
1414 if (task
->tk_status
== 0) {
1415 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1419 data
->rpc_status
= -ELOOP
;
1422 data
->rpc_status
= -EISDIR
;
1425 data
->rpc_status
= -ENOTDIR
;
1427 renew_lease(data
->o_res
.server
, data
->timestamp
);
1428 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1429 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1434 static void nfs4_open_release(void *calldata
)
1436 struct nfs4_opendata
*data
= calldata
;
1437 struct nfs4_state
*state
= NULL
;
1439 /* If this request hasn't been cancelled, do nothing */
1440 if (data
->cancelled
== 0)
1442 /* In case of error, no cleanup! */
1443 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1445 /* In case we need an open_confirm, no cleanup! */
1446 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1448 state
= nfs4_opendata_to_nfs4_state(data
);
1450 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1452 nfs4_opendata_put(data
);
1455 static const struct rpc_call_ops nfs4_open_ops
= {
1456 .rpc_call_prepare
= nfs4_open_prepare
,
1457 .rpc_call_done
= nfs4_open_done
,
1458 .rpc_release
= nfs4_open_release
,
1461 static const struct rpc_call_ops nfs4_recover_open_ops
= {
1462 .rpc_call_prepare
= nfs4_recover_open_prepare
,
1463 .rpc_call_done
= nfs4_open_done
,
1464 .rpc_release
= nfs4_open_release
,
1467 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1469 struct inode
*dir
= data
->dir
->d_inode
;
1470 struct nfs_server
*server
= NFS_SERVER(dir
);
1471 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1472 struct nfs_openres
*o_res
= &data
->o_res
;
1473 struct rpc_task
*task
;
1474 struct rpc_message msg
= {
1475 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1478 .rpc_cred
= data
->owner
->so_cred
,
1480 struct rpc_task_setup task_setup_data
= {
1481 .rpc_client
= server
->client
,
1482 .rpc_message
= &msg
,
1483 .callback_ops
= &nfs4_open_ops
,
1484 .callback_data
= data
,
1485 .workqueue
= nfsiod_workqueue
,
1486 .flags
= RPC_TASK_ASYNC
,
1490 kref_get(&data
->kref
);
1492 data
->rpc_status
= 0;
1493 data
->cancelled
= 0;
1495 task_setup_data
.callback_ops
= &nfs4_recover_open_ops
;
1496 task
= rpc_run_task(&task_setup_data
);
1498 return PTR_ERR(task
);
1499 status
= nfs4_wait_for_completion_rpc_task(task
);
1501 data
->cancelled
= 1;
1504 status
= data
->rpc_status
;
1510 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1512 struct inode
*dir
= data
->dir
->d_inode
;
1513 struct nfs_openres
*o_res
= &data
->o_res
;
1516 status
= nfs4_run_open_task(data
, 1);
1517 if (status
!= 0 || !data
->rpc_done
)
1520 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1522 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1523 status
= _nfs4_proc_open_confirm(data
);
1532 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1534 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1536 struct inode
*dir
= data
->dir
->d_inode
;
1537 struct nfs_server
*server
= NFS_SERVER(dir
);
1538 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1539 struct nfs_openres
*o_res
= &data
->o_res
;
1542 status
= nfs4_run_open_task(data
, 0);
1543 if (status
!= 0 || !data
->rpc_done
)
1546 if (o_arg
->open_flags
& O_CREAT
) {
1547 update_changeattr(dir
, &o_res
->cinfo
);
1548 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1550 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1551 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1552 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1553 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1554 status
= _nfs4_proc_open_confirm(data
);
1558 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1559 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1563 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1565 struct nfs_client
*clp
= server
->nfs_client
;
1569 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1570 ret
= nfs4_wait_clnt_recover(clp
);
1573 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1574 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1576 nfs4_schedule_state_recovery(clp
);
1584 * reclaim state on the server after a network partition.
1585 * Assumes caller holds the appropriate lock
1587 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1589 struct nfs4_opendata
*opendata
;
1592 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1593 if (IS_ERR(opendata
))
1594 return PTR_ERR(opendata
);
1595 ret
= nfs4_open_recover(opendata
, state
);
1597 d_drop(ctx
->path
.dentry
);
1598 nfs4_opendata_put(opendata
);
1602 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1604 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1605 struct nfs4_exception exception
= { };
1609 err
= _nfs4_open_expired(ctx
, state
);
1613 case -NFS4ERR_GRACE
:
1614 case -NFS4ERR_DELAY
:
1615 nfs4_handle_exception(server
, err
, &exception
);
1618 } while (exception
.retry
);
1623 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1625 struct nfs_open_context
*ctx
;
1628 ctx
= nfs4_state_find_open_context(state
);
1630 return PTR_ERR(ctx
);
1631 ret
= nfs4_do_open_expired(ctx
, state
);
1632 put_nfs_open_context(ctx
);
1637 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1638 * fields corresponding to attributes that were used to store the verifier.
1639 * Make sure we clobber those fields in the later setattr call
1641 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1643 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1644 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1645 sattr
->ia_valid
|= ATTR_ATIME
;
1647 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1648 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1649 sattr
->ia_valid
|= ATTR_MTIME
;
1653 * Returns a referenced nfs4_state
1655 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
)
1657 struct nfs4_state_owner
*sp
;
1658 struct nfs4_state
*state
= NULL
;
1659 struct nfs_server
*server
= NFS_SERVER(dir
);
1660 struct nfs4_opendata
*opendata
;
1663 /* Protect against reboot recovery conflicts */
1665 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1666 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1669 status
= nfs4_recover_expired_lease(server
);
1671 goto err_put_state_owner
;
1672 if (path
->dentry
->d_inode
!= NULL
)
1673 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1675 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
, GFP_KERNEL
);
1676 if (opendata
== NULL
)
1677 goto err_put_state_owner
;
1679 if (path
->dentry
->d_inode
!= NULL
)
1680 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1682 status
= _nfs4_proc_open(opendata
);
1684 goto err_opendata_put
;
1686 state
= nfs4_opendata_to_nfs4_state(opendata
);
1687 status
= PTR_ERR(state
);
1689 goto err_opendata_put
;
1690 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
1691 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1693 if (opendata
->o_arg
.open_flags
& O_EXCL
) {
1694 nfs4_exclusive_attrset(opendata
, sattr
);
1696 nfs_fattr_init(opendata
->o_res
.f_attr
);
1697 status
= nfs4_do_setattr(state
->inode
, cred
,
1698 opendata
->o_res
.f_attr
, sattr
,
1701 nfs_setattr_update_inode(state
->inode
, sattr
);
1702 nfs_post_op_update_inode(state
->inode
, opendata
->o_res
.f_attr
);
1704 nfs4_opendata_put(opendata
);
1705 nfs4_put_state_owner(sp
);
1709 nfs4_opendata_put(opendata
);
1710 err_put_state_owner
:
1711 nfs4_put_state_owner(sp
);
1718 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
)
1720 struct nfs4_exception exception
= { };
1721 struct nfs4_state
*res
;
1725 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1728 /* NOTE: BAD_SEQID means the server and client disagree about the
1729 * book-keeping w.r.t. state-changing operations
1730 * (OPEN/CLOSE/LOCK/LOCKU...)
1731 * It is actually a sign of a bug on the client or on the server.
1733 * If we receive a BAD_SEQID error in the particular case of
1734 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1735 * have unhashed the old state_owner for us, and that we can
1736 * therefore safely retry using a new one. We should still warn
1737 * the user though...
1739 if (status
== -NFS4ERR_BAD_SEQID
) {
1740 printk(KERN_WARNING
"NFS: v4 server %s "
1741 " returned a bad sequence-id error!\n",
1742 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1743 exception
.retry
= 1;
1747 * BAD_STATEID on OPEN means that the server cancelled our
1748 * state before it received the OPEN_CONFIRM.
1749 * Recover by retrying the request as per the discussion
1750 * on Page 181 of RFC3530.
1752 if (status
== -NFS4ERR_BAD_STATEID
) {
1753 exception
.retry
= 1;
1756 if (status
== -EAGAIN
) {
1757 /* We must have found a delegation */
1758 exception
.retry
= 1;
1761 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1762 status
, &exception
));
1763 } while (exception
.retry
);
1767 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1768 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1769 struct nfs4_state
*state
)
1771 struct nfs_server
*server
= NFS_SERVER(inode
);
1772 struct nfs_setattrargs arg
= {
1773 .fh
= NFS_FH(inode
),
1776 .bitmask
= server
->attr_bitmask
,
1778 struct nfs_setattrres res
= {
1782 struct rpc_message msg
= {
1783 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1788 unsigned long timestamp
= jiffies
;
1791 nfs_fattr_init(fattr
);
1793 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1794 /* Use that stateid */
1795 } else if (state
!= NULL
) {
1796 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
, current
->tgid
);
1798 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1800 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1801 if (status
== 0 && state
!= NULL
)
1802 renew_lease(server
, timestamp
);
1806 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1807 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1808 struct nfs4_state
*state
)
1810 struct nfs_server
*server
= NFS_SERVER(inode
);
1811 struct nfs4_exception exception
= { };
1814 err
= nfs4_handle_exception(server
,
1815 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1817 } while (exception
.retry
);
1821 struct nfs4_closedata
{
1823 struct inode
*inode
;
1824 struct nfs4_state
*state
;
1825 struct nfs_closeargs arg
;
1826 struct nfs_closeres res
;
1827 struct nfs_fattr fattr
;
1828 unsigned long timestamp
;
1831 static void nfs4_free_closedata(void *data
)
1833 struct nfs4_closedata
*calldata
= data
;
1834 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1836 nfs4_put_open_state(calldata
->state
);
1837 nfs_free_seqid(calldata
->arg
.seqid
);
1838 nfs4_put_state_owner(sp
);
1839 path_put(&calldata
->path
);
1843 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
1846 spin_lock(&state
->owner
->so_lock
);
1847 if (!(fmode
& FMODE_READ
))
1848 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1849 if (!(fmode
& FMODE_WRITE
))
1850 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1851 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1852 spin_unlock(&state
->owner
->so_lock
);
1855 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1857 struct nfs4_closedata
*calldata
= data
;
1858 struct nfs4_state
*state
= calldata
->state
;
1859 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1861 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
1863 /* hmm. we are done with the inode, and in the process of freeing
1864 * the state_owner. we keep this around to process errors
1866 switch (task
->tk_status
) {
1868 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1869 renew_lease(server
, calldata
->timestamp
);
1870 nfs4_close_clear_stateid_flags(state
,
1871 calldata
->arg
.fmode
);
1873 case -NFS4ERR_STALE_STATEID
:
1874 case -NFS4ERR_OLD_STATEID
:
1875 case -NFS4ERR_BAD_STATEID
:
1876 case -NFS4ERR_EXPIRED
:
1877 if (calldata
->arg
.fmode
== 0)
1880 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
1881 rpc_restart_call_prepare(task
);
1883 nfs_release_seqid(calldata
->arg
.seqid
);
1884 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1887 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1889 struct nfs4_closedata
*calldata
= data
;
1890 struct nfs4_state
*state
= calldata
->state
;
1893 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1896 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1897 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
1898 spin_lock(&state
->owner
->so_lock
);
1899 /* Calculate the change in open mode */
1900 if (state
->n_rdwr
== 0) {
1901 if (state
->n_rdonly
== 0) {
1902 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1903 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1904 calldata
->arg
.fmode
&= ~FMODE_READ
;
1906 if (state
->n_wronly
== 0) {
1907 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1908 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1909 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
1912 spin_unlock(&state
->owner
->so_lock
);
1915 /* Note: exit _without_ calling nfs4_close_done */
1916 task
->tk_action
= NULL
;
1920 if (calldata
->arg
.fmode
== 0)
1921 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
1923 nfs_fattr_init(calldata
->res
.fattr
);
1924 calldata
->timestamp
= jiffies
;
1925 if (nfs4_setup_sequence(NFS_SERVER(calldata
->inode
),
1926 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1929 rpc_call_start(task
);
1932 static const struct rpc_call_ops nfs4_close_ops
= {
1933 .rpc_call_prepare
= nfs4_close_prepare
,
1934 .rpc_call_done
= nfs4_close_done
,
1935 .rpc_release
= nfs4_free_closedata
,
1939 * It is possible for data to be read/written from a mem-mapped file
1940 * after the sys_close call (which hits the vfs layer as a flush).
1941 * This means that we can't safely call nfsv4 close on a file until
1942 * the inode is cleared. This in turn means that we are not good
1943 * NFSv4 citizens - we do not indicate to the server to update the file's
1944 * share state even when we are done with one of the three share
1945 * stateid's in the inode.
1947 * NOTE: Caller must be holding the sp->so_owner semaphore!
1949 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
1951 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1952 struct nfs4_closedata
*calldata
;
1953 struct nfs4_state_owner
*sp
= state
->owner
;
1954 struct rpc_task
*task
;
1955 struct rpc_message msg
= {
1956 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1957 .rpc_cred
= state
->owner
->so_cred
,
1959 struct rpc_task_setup task_setup_data
= {
1960 .rpc_client
= server
->client
,
1961 .rpc_message
= &msg
,
1962 .callback_ops
= &nfs4_close_ops
,
1963 .workqueue
= nfsiod_workqueue
,
1964 .flags
= RPC_TASK_ASYNC
,
1966 int status
= -ENOMEM
;
1968 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
1969 if (calldata
== NULL
)
1971 calldata
->inode
= state
->inode
;
1972 calldata
->state
= state
;
1973 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1974 calldata
->arg
.stateid
= &state
->open_stateid
;
1975 /* Serialization for the sequence id */
1976 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
1977 if (calldata
->arg
.seqid
== NULL
)
1978 goto out_free_calldata
;
1979 calldata
->arg
.fmode
= 0;
1980 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1981 calldata
->res
.fattr
= &calldata
->fattr
;
1982 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1983 calldata
->res
.server
= server
;
1985 calldata
->path
= *path
;
1987 msg
.rpc_argp
= &calldata
->arg
,
1988 msg
.rpc_resp
= &calldata
->res
,
1989 task_setup_data
.callback_data
= calldata
;
1990 task
= rpc_run_task(&task_setup_data
);
1992 return PTR_ERR(task
);
1995 status
= rpc_wait_for_completion_task(task
);
2001 nfs4_put_open_state(state
);
2002 nfs4_put_state_owner(sp
);
2006 static struct inode
*
2007 nfs4_atomic_open(struct inode
*dir
, struct nfs_open_context
*ctx
, int open_flags
, struct iattr
*attr
)
2009 struct nfs4_state
*state
;
2011 /* Protect against concurrent sillydeletes */
2012 state
= nfs4_do_open(dir
, &ctx
->path
, ctx
->mode
, open_flags
, attr
, ctx
->cred
);
2014 return ERR_CAST(state
);
2016 return igrab(state
->inode
);
2019 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2021 if (ctx
->state
== NULL
)
2024 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2026 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2029 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2031 struct nfs4_server_caps_arg args
= {
2034 struct nfs4_server_caps_res res
= {};
2035 struct rpc_message msg
= {
2036 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2042 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2044 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2045 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2046 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2047 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2048 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2049 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2050 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2051 server
->caps
|= NFS_CAP_ACLS
;
2052 if (res
.has_links
!= 0)
2053 server
->caps
|= NFS_CAP_HARDLINKS
;
2054 if (res
.has_symlinks
!= 0)
2055 server
->caps
|= NFS_CAP_SYMLINKS
;
2056 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2057 server
->caps
|= NFS_CAP_FILEID
;
2058 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2059 server
->caps
|= NFS_CAP_MODE
;
2060 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2061 server
->caps
|= NFS_CAP_NLINK
;
2062 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2063 server
->caps
|= NFS_CAP_OWNER
;
2064 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2065 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2066 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2067 server
->caps
|= NFS_CAP_ATIME
;
2068 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2069 server
->caps
|= NFS_CAP_CTIME
;
2070 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2071 server
->caps
|= NFS_CAP_MTIME
;
2073 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2074 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2075 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2076 server
->acl_bitmask
= res
.acl_bitmask
;
2082 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2084 struct nfs4_exception exception
= { };
2087 err
= nfs4_handle_exception(server
,
2088 _nfs4_server_capabilities(server
, fhandle
),
2090 } while (exception
.retry
);
2094 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2095 struct nfs_fsinfo
*info
)
2097 struct nfs4_lookup_root_arg args
= {
2098 .bitmask
= nfs4_fattr_bitmap
,
2100 struct nfs4_lookup_res res
= {
2102 .fattr
= info
->fattr
,
2105 struct rpc_message msg
= {
2106 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2111 nfs_fattr_init(info
->fattr
);
2112 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2115 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2116 struct nfs_fsinfo
*info
)
2118 struct nfs4_exception exception
= { };
2121 err
= nfs4_handle_exception(server
,
2122 _nfs4_lookup_root(server
, fhandle
, info
),
2124 } while (exception
.retry
);
2129 * get the file handle for the "/" directory on the server
2131 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2132 struct nfs_fsinfo
*info
)
2136 status
= nfs4_lookup_root(server
, fhandle
, info
);
2138 status
= nfs4_server_capabilities(server
, fhandle
);
2140 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2141 return nfs4_map_errors(status
);
2145 * Get locations and (maybe) other attributes of a referral.
2146 * Note that we'll actually follow the referral later when
2147 * we detect fsid mismatch in inode revalidation
2149 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2151 int status
= -ENOMEM
;
2152 struct page
*page
= NULL
;
2153 struct nfs4_fs_locations
*locations
= NULL
;
2155 page
= alloc_page(GFP_KERNEL
);
2158 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2159 if (locations
== NULL
)
2162 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2165 /* Make sure server returned a different fsid for the referral */
2166 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2167 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2172 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2173 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2175 fattr
->mode
= S_IFDIR
;
2176 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2184 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2186 struct nfs4_getattr_arg args
= {
2188 .bitmask
= server
->attr_bitmask
,
2190 struct nfs4_getattr_res res
= {
2194 struct rpc_message msg
= {
2195 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2200 nfs_fattr_init(fattr
);
2201 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2204 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2206 struct nfs4_exception exception
= { };
2209 err
= nfs4_handle_exception(server
,
2210 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2212 } while (exception
.retry
);
2217 * The file is not closed if it is opened due to the a request to change
2218 * the size of the file. The open call will not be needed once the
2219 * VFS layer lookup-intents are implemented.
2221 * Close is called when the inode is destroyed.
2222 * If we haven't opened the file for O_WRONLY, we
2223 * need to in the size_change case to obtain a stateid.
2226 * Because OPEN is always done by name in nfsv4, it is
2227 * possible that we opened a different file by the same
2228 * name. We can recognize this race condition, but we
2229 * can't do anything about it besides returning an error.
2231 * This will be fixed with VFS changes (lookup-intent).
2234 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2235 struct iattr
*sattr
)
2237 struct inode
*inode
= dentry
->d_inode
;
2238 struct rpc_cred
*cred
= NULL
;
2239 struct nfs4_state
*state
= NULL
;
2242 nfs_fattr_init(fattr
);
2244 /* Search for an existing open(O_WRITE) file */
2245 if (sattr
->ia_valid
& ATTR_FILE
) {
2246 struct nfs_open_context
*ctx
;
2248 ctx
= nfs_file_open_context(sattr
->ia_file
);
2255 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2257 nfs_setattr_update_inode(inode
, sattr
);
2261 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2262 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2263 struct nfs_fattr
*fattr
)
2266 struct nfs4_lookup_arg args
= {
2267 .bitmask
= server
->attr_bitmask
,
2271 struct nfs4_lookup_res res
= {
2276 struct rpc_message msg
= {
2277 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2282 nfs_fattr_init(fattr
);
2284 dprintk("NFS call lookupfh %s\n", name
->name
);
2285 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2286 dprintk("NFS reply lookupfh: %d\n", status
);
2290 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2291 struct qstr
*name
, struct nfs_fh
*fhandle
,
2292 struct nfs_fattr
*fattr
)
2294 struct nfs4_exception exception
= { };
2297 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2299 if (err
== -NFS4ERR_MOVED
) {
2303 err
= nfs4_handle_exception(server
, err
, &exception
);
2304 } while (exception
.retry
);
2308 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2309 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2313 dprintk("NFS call lookup %s\n", name
->name
);
2314 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2315 if (status
== -NFS4ERR_MOVED
)
2316 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2317 dprintk("NFS reply lookup: %d\n", status
);
2321 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2323 struct nfs4_exception exception
= { };
2326 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2327 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2329 } while (exception
.retry
);
2333 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2335 struct nfs_server
*server
= NFS_SERVER(inode
);
2336 struct nfs4_accessargs args
= {
2337 .fh
= NFS_FH(inode
),
2338 .bitmask
= server
->attr_bitmask
,
2340 struct nfs4_accessres res
= {
2343 struct rpc_message msg
= {
2344 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2347 .rpc_cred
= entry
->cred
,
2349 int mode
= entry
->mask
;
2353 * Determine which access bits we want to ask for...
2355 if (mode
& MAY_READ
)
2356 args
.access
|= NFS4_ACCESS_READ
;
2357 if (S_ISDIR(inode
->i_mode
)) {
2358 if (mode
& MAY_WRITE
)
2359 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2360 if (mode
& MAY_EXEC
)
2361 args
.access
|= NFS4_ACCESS_LOOKUP
;
2363 if (mode
& MAY_WRITE
)
2364 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2365 if (mode
& MAY_EXEC
)
2366 args
.access
|= NFS4_ACCESS_EXECUTE
;
2369 res
.fattr
= nfs_alloc_fattr();
2370 if (res
.fattr
== NULL
)
2373 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2376 if (res
.access
& NFS4_ACCESS_READ
)
2377 entry
->mask
|= MAY_READ
;
2378 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2379 entry
->mask
|= MAY_WRITE
;
2380 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2381 entry
->mask
|= MAY_EXEC
;
2382 nfs_refresh_inode(inode
, res
.fattr
);
2384 nfs_free_fattr(res
.fattr
);
2388 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2390 struct nfs4_exception exception
= { };
2393 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2394 _nfs4_proc_access(inode
, entry
),
2396 } while (exception
.retry
);
2401 * TODO: For the time being, we don't try to get any attributes
2402 * along with any of the zero-copy operations READ, READDIR,
2405 * In the case of the first three, we want to put the GETATTR
2406 * after the read-type operation -- this is because it is hard
2407 * to predict the length of a GETATTR response in v4, and thus
2408 * align the READ data correctly. This means that the GETATTR
2409 * may end up partially falling into the page cache, and we should
2410 * shift it into the 'tail' of the xdr_buf before processing.
2411 * To do this efficiently, we need to know the total length
2412 * of data received, which doesn't seem to be available outside
2415 * In the case of WRITE, we also want to put the GETATTR after
2416 * the operation -- in this case because we want to make sure
2417 * we get the post-operation mtime and size. This means that
2418 * we can't use xdr_encode_pages() as written: we need a variant
2419 * of it which would leave room in the 'tail' iovec.
2421 * Both of these changes to the XDR layer would in fact be quite
2422 * minor, but I decided to leave them for a subsequent patch.
2424 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2425 unsigned int pgbase
, unsigned int pglen
)
2427 struct nfs4_readlink args
= {
2428 .fh
= NFS_FH(inode
),
2433 struct nfs4_readlink_res res
;
2434 struct rpc_message msg
= {
2435 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2440 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2443 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2444 unsigned int pgbase
, unsigned int pglen
)
2446 struct nfs4_exception exception
= { };
2449 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2450 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2452 } while (exception
.retry
);
2458 * We will need to arrange for the VFS layer to provide an atomic open.
2459 * Until then, this create/open method is prone to inefficiency and race
2460 * conditions due to the lookup, create, and open VFS calls from sys_open()
2461 * placed on the wire.
2463 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2464 * The file will be opened again in the subsequent VFS open call
2465 * (nfs4_proc_file_open).
2467 * The open for read will just hang around to be used by any process that
2468 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2472 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2473 int flags
, struct nfs_open_context
*ctx
)
2475 struct path my_path
= {
2478 struct path
*path
= &my_path
;
2479 struct nfs4_state
*state
;
2480 struct rpc_cred
*cred
= NULL
;
2489 state
= nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
);
2491 if (IS_ERR(state
)) {
2492 status
= PTR_ERR(state
);
2495 d_add(dentry
, igrab(state
->inode
));
2496 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2500 nfs4_close_sync(path
, state
, fmode
);
2505 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2507 struct nfs_server
*server
= NFS_SERVER(dir
);
2508 struct nfs_removeargs args
= {
2510 .name
.len
= name
->len
,
2511 .name
.name
= name
->name
,
2512 .bitmask
= server
->attr_bitmask
,
2514 struct nfs_removeres res
= {
2517 struct rpc_message msg
= {
2518 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2522 int status
= -ENOMEM
;
2524 res
.dir_attr
= nfs_alloc_fattr();
2525 if (res
.dir_attr
== NULL
)
2528 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2530 update_changeattr(dir
, &res
.cinfo
);
2531 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2533 nfs_free_fattr(res
.dir_attr
);
2538 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2540 struct nfs4_exception exception
= { };
2543 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2544 _nfs4_proc_remove(dir
, name
),
2546 } while (exception
.retry
);
2550 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2552 struct nfs_server
*server
= NFS_SERVER(dir
);
2553 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2554 struct nfs_removeres
*res
= msg
->rpc_resp
;
2556 args
->bitmask
= server
->cache_consistency_bitmask
;
2557 res
->server
= server
;
2558 res
->seq_res
.sr_slot
= NULL
;
2559 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2562 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2564 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2566 if (!nfs4_sequence_done(task
, &res
->seq_res
))
2568 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2570 update_changeattr(dir
, &res
->cinfo
);
2571 nfs_post_op_update_inode(dir
, res
->dir_attr
);
2575 static void nfs4_proc_rename_setup(struct rpc_message
*msg
, struct inode
*dir
)
2577 struct nfs_server
*server
= NFS_SERVER(dir
);
2578 struct nfs_renameargs
*arg
= msg
->rpc_argp
;
2579 struct nfs_renameres
*res
= msg
->rpc_resp
;
2581 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
];
2582 arg
->bitmask
= server
->attr_bitmask
;
2583 res
->server
= server
;
2586 static int nfs4_proc_rename_done(struct rpc_task
*task
, struct inode
*old_dir
,
2587 struct inode
*new_dir
)
2589 struct nfs_renameres
*res
= task
->tk_msg
.rpc_resp
;
2591 if (!nfs4_sequence_done(task
, &res
->seq_res
))
2593 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2596 update_changeattr(old_dir
, &res
->old_cinfo
);
2597 nfs_post_op_update_inode(old_dir
, res
->old_fattr
);
2598 update_changeattr(new_dir
, &res
->new_cinfo
);
2599 nfs_post_op_update_inode(new_dir
, res
->new_fattr
);
2603 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2604 struct inode
*new_dir
, struct qstr
*new_name
)
2606 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2607 struct nfs_renameargs arg
= {
2608 .old_dir
= NFS_FH(old_dir
),
2609 .new_dir
= NFS_FH(new_dir
),
2610 .old_name
= old_name
,
2611 .new_name
= new_name
,
2612 .bitmask
= server
->attr_bitmask
,
2614 struct nfs_renameres res
= {
2617 struct rpc_message msg
= {
2618 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2622 int status
= -ENOMEM
;
2624 res
.old_fattr
= nfs_alloc_fattr();
2625 res
.new_fattr
= nfs_alloc_fattr();
2626 if (res
.old_fattr
== NULL
|| res
.new_fattr
== NULL
)
2629 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2631 update_changeattr(old_dir
, &res
.old_cinfo
);
2632 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2633 update_changeattr(new_dir
, &res
.new_cinfo
);
2634 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2637 nfs_free_fattr(res
.new_fattr
);
2638 nfs_free_fattr(res
.old_fattr
);
2642 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2643 struct inode
*new_dir
, struct qstr
*new_name
)
2645 struct nfs4_exception exception
= { };
2648 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2649 _nfs4_proc_rename(old_dir
, old_name
,
2652 } while (exception
.retry
);
2656 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2658 struct nfs_server
*server
= NFS_SERVER(inode
);
2659 struct nfs4_link_arg arg
= {
2660 .fh
= NFS_FH(inode
),
2661 .dir_fh
= NFS_FH(dir
),
2663 .bitmask
= server
->attr_bitmask
,
2665 struct nfs4_link_res res
= {
2668 struct rpc_message msg
= {
2669 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2673 int status
= -ENOMEM
;
2675 res
.fattr
= nfs_alloc_fattr();
2676 res
.dir_attr
= nfs_alloc_fattr();
2677 if (res
.fattr
== NULL
|| res
.dir_attr
== NULL
)
2680 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2682 update_changeattr(dir
, &res
.cinfo
);
2683 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2684 nfs_post_op_update_inode(inode
, res
.fattr
);
2687 nfs_free_fattr(res
.dir_attr
);
2688 nfs_free_fattr(res
.fattr
);
2692 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2694 struct nfs4_exception exception
= { };
2697 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2698 _nfs4_proc_link(inode
, dir
, name
),
2700 } while (exception
.retry
);
2704 struct nfs4_createdata
{
2705 struct rpc_message msg
;
2706 struct nfs4_create_arg arg
;
2707 struct nfs4_create_res res
;
2709 struct nfs_fattr fattr
;
2710 struct nfs_fattr dir_fattr
;
2713 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2714 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2716 struct nfs4_createdata
*data
;
2718 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2720 struct nfs_server
*server
= NFS_SERVER(dir
);
2722 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2723 data
->msg
.rpc_argp
= &data
->arg
;
2724 data
->msg
.rpc_resp
= &data
->res
;
2725 data
->arg
.dir_fh
= NFS_FH(dir
);
2726 data
->arg
.server
= server
;
2727 data
->arg
.name
= name
;
2728 data
->arg
.attrs
= sattr
;
2729 data
->arg
.ftype
= ftype
;
2730 data
->arg
.bitmask
= server
->attr_bitmask
;
2731 data
->res
.server
= server
;
2732 data
->res
.fh
= &data
->fh
;
2733 data
->res
.fattr
= &data
->fattr
;
2734 data
->res
.dir_fattr
= &data
->dir_fattr
;
2735 nfs_fattr_init(data
->res
.fattr
);
2736 nfs_fattr_init(data
->res
.dir_fattr
);
2741 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2743 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2744 &data
->arg
, &data
->res
, 1);
2746 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2747 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2748 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2753 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2758 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2759 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2761 struct nfs4_createdata
*data
;
2762 int status
= -ENAMETOOLONG
;
2764 if (len
> NFS4_MAXPATHLEN
)
2768 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2772 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2773 data
->arg
.u
.symlink
.pages
= &page
;
2774 data
->arg
.u
.symlink
.len
= len
;
2776 status
= nfs4_do_create(dir
, dentry
, data
);
2778 nfs4_free_createdata(data
);
2783 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2784 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2786 struct nfs4_exception exception
= { };
2789 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2790 _nfs4_proc_symlink(dir
, dentry
, page
,
2793 } while (exception
.retry
);
2797 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2798 struct iattr
*sattr
)
2800 struct nfs4_createdata
*data
;
2801 int status
= -ENOMEM
;
2803 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2807 status
= nfs4_do_create(dir
, dentry
, data
);
2809 nfs4_free_createdata(data
);
2814 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2815 struct iattr
*sattr
)
2817 struct nfs4_exception exception
= { };
2820 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2821 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2823 } while (exception
.retry
);
2827 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2828 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
2830 struct inode
*dir
= dentry
->d_inode
;
2831 struct nfs4_readdir_arg args
= {
2836 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
2839 struct nfs4_readdir_res res
;
2840 struct rpc_message msg
= {
2841 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2848 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2849 dentry
->d_parent
->d_name
.name
,
2850 dentry
->d_name
.name
,
2851 (unsigned long long)cookie
);
2852 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2853 res
.pgbase
= args
.pgbase
;
2854 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2856 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2857 status
+= args
.pgbase
;
2860 nfs_invalidate_atime(dir
);
2862 dprintk("%s: returns %d\n", __func__
, status
);
2866 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2867 u64 cookie
, struct page
**pages
, unsigned int count
, int plus
)
2869 struct nfs4_exception exception
= { };
2872 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2873 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2874 pages
, count
, plus
),
2876 } while (exception
.retry
);
2880 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2881 struct iattr
*sattr
, dev_t rdev
)
2883 struct nfs4_createdata
*data
;
2884 int mode
= sattr
->ia_mode
;
2885 int status
= -ENOMEM
;
2887 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2888 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2890 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2895 data
->arg
.ftype
= NF4FIFO
;
2896 else if (S_ISBLK(mode
)) {
2897 data
->arg
.ftype
= NF4BLK
;
2898 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2899 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2901 else if (S_ISCHR(mode
)) {
2902 data
->arg
.ftype
= NF4CHR
;
2903 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2904 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2907 status
= nfs4_do_create(dir
, dentry
, data
);
2909 nfs4_free_createdata(data
);
2914 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2915 struct iattr
*sattr
, dev_t rdev
)
2917 struct nfs4_exception exception
= { };
2920 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2921 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2923 } while (exception
.retry
);
2927 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2928 struct nfs_fsstat
*fsstat
)
2930 struct nfs4_statfs_arg args
= {
2932 .bitmask
= server
->attr_bitmask
,
2934 struct nfs4_statfs_res res
= {
2937 struct rpc_message msg
= {
2938 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2943 nfs_fattr_init(fsstat
->fattr
);
2944 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2947 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2949 struct nfs4_exception exception
= { };
2952 err
= nfs4_handle_exception(server
,
2953 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2955 } while (exception
.retry
);
2959 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2960 struct nfs_fsinfo
*fsinfo
)
2962 struct nfs4_fsinfo_arg args
= {
2964 .bitmask
= server
->attr_bitmask
,
2966 struct nfs4_fsinfo_res res
= {
2969 struct rpc_message msg
= {
2970 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
2975 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2978 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2980 struct nfs4_exception exception
= { };
2984 err
= nfs4_handle_exception(server
,
2985 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
2987 } while (exception
.retry
);
2991 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2993 nfs_fattr_init(fsinfo
->fattr
);
2994 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
2997 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2998 struct nfs_pathconf
*pathconf
)
3000 struct nfs4_pathconf_arg args
= {
3002 .bitmask
= server
->attr_bitmask
,
3004 struct nfs4_pathconf_res res
= {
3005 .pathconf
= pathconf
,
3007 struct rpc_message msg
= {
3008 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3013 /* None of the pathconf attributes are mandatory to implement */
3014 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3015 memset(pathconf
, 0, sizeof(*pathconf
));
3019 nfs_fattr_init(pathconf
->fattr
);
3020 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3023 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3024 struct nfs_pathconf
*pathconf
)
3026 struct nfs4_exception exception
= { };
3030 err
= nfs4_handle_exception(server
,
3031 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3033 } while (exception
.retry
);
3037 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3039 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3041 dprintk("--> %s\n", __func__
);
3043 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3046 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3047 nfs_restart_rpc(task
, server
->nfs_client
);
3051 nfs_invalidate_atime(data
->inode
);
3052 if (task
->tk_status
> 0)
3053 renew_lease(server
, data
->timestamp
);
3057 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3059 data
->timestamp
= jiffies
;
3060 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3063 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3065 struct inode
*inode
= data
->inode
;
3067 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3070 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3071 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3074 if (task
->tk_status
>= 0) {
3075 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3076 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3081 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3083 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3085 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3086 data
->res
.server
= server
;
3087 data
->timestamp
= jiffies
;
3089 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3092 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3094 struct inode
*inode
= data
->inode
;
3096 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3099 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3100 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3103 nfs_refresh_inode(inode
, data
->res
.fattr
);
3107 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3109 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3111 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3112 data
->res
.server
= server
;
3113 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3116 struct nfs4_renewdata
{
3117 struct nfs_client
*client
;
3118 unsigned long timestamp
;
3122 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3123 * standalone procedure for queueing an asynchronous RENEW.
3125 static void nfs4_renew_release(void *calldata
)
3127 struct nfs4_renewdata
*data
= calldata
;
3128 struct nfs_client
*clp
= data
->client
;
3130 if (atomic_read(&clp
->cl_count
) > 1)
3131 nfs4_schedule_state_renewal(clp
);
3132 nfs_put_client(clp
);
3136 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
3138 struct nfs4_renewdata
*data
= calldata
;
3139 struct nfs_client
*clp
= data
->client
;
3140 unsigned long timestamp
= data
->timestamp
;
3142 if (task
->tk_status
< 0) {
3143 /* Unless we're shutting down, schedule state recovery! */
3144 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3145 nfs4_schedule_state_recovery(clp
);
3148 do_renew_lease(clp
, timestamp
);
3151 static const struct rpc_call_ops nfs4_renew_ops
= {
3152 .rpc_call_done
= nfs4_renew_done
,
3153 .rpc_release
= nfs4_renew_release
,
3156 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3158 struct rpc_message msg
= {
3159 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3163 struct nfs4_renewdata
*data
;
3165 if (!atomic_inc_not_zero(&clp
->cl_count
))
3167 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
3171 data
->timestamp
= jiffies
;
3172 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3173 &nfs4_renew_ops
, data
);
3176 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3178 struct rpc_message msg
= {
3179 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3183 unsigned long now
= jiffies
;
3186 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3189 do_renew_lease(clp
, now
);
3193 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3195 return (server
->caps
& NFS_CAP_ACLS
)
3196 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3197 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3200 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3201 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3204 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3206 static void buf_to_pages(const void *buf
, size_t buflen
,
3207 struct page
**pages
, unsigned int *pgbase
)
3209 const void *p
= buf
;
3211 *pgbase
= offset_in_page(buf
);
3213 while (p
< buf
+ buflen
) {
3214 *(pages
++) = virt_to_page(p
);
3215 p
+= PAGE_CACHE_SIZE
;
3219 struct nfs4_cached_acl
{
3225 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3227 struct nfs_inode
*nfsi
= NFS_I(inode
);
3229 spin_lock(&inode
->i_lock
);
3230 kfree(nfsi
->nfs4_acl
);
3231 nfsi
->nfs4_acl
= acl
;
3232 spin_unlock(&inode
->i_lock
);
3235 static void nfs4_zap_acl_attr(struct inode
*inode
)
3237 nfs4_set_cached_acl(inode
, NULL
);
3240 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3242 struct nfs_inode
*nfsi
= NFS_I(inode
);
3243 struct nfs4_cached_acl
*acl
;
3246 spin_lock(&inode
->i_lock
);
3247 acl
= nfsi
->nfs4_acl
;
3250 if (buf
== NULL
) /* user is just asking for length */
3252 if (acl
->cached
== 0)
3254 ret
= -ERANGE
; /* see getxattr(2) man page */
3255 if (acl
->len
> buflen
)
3257 memcpy(buf
, acl
->data
, acl
->len
);
3261 spin_unlock(&inode
->i_lock
);
3265 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3267 struct nfs4_cached_acl
*acl
;
3269 if (buf
&& acl_len
<= PAGE_SIZE
) {
3270 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3274 memcpy(acl
->data
, buf
, acl_len
);
3276 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3283 nfs4_set_cached_acl(inode
, acl
);
3286 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3288 struct page
*pages
[NFS4ACL_MAXPAGES
];
3289 struct nfs_getaclargs args
= {
3290 .fh
= NFS_FH(inode
),
3294 struct nfs_getaclres res
= {
3298 struct rpc_message msg
= {
3299 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3303 struct page
*localpage
= NULL
;
3306 if (buflen
< PAGE_SIZE
) {
3307 /* As long as we're doing a round trip to the server anyway,
3308 * let's be prepared for a page of acl data. */
3309 localpage
= alloc_page(GFP_KERNEL
);
3310 resp_buf
= page_address(localpage
);
3311 if (localpage
== NULL
)
3313 args
.acl_pages
[0] = localpage
;
3314 args
.acl_pgbase
= 0;
3315 args
.acl_len
= PAGE_SIZE
;
3318 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3320 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3323 if (res
.acl_len
> args
.acl_len
)
3324 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3326 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3329 if (res
.acl_len
> buflen
)
3332 memcpy(buf
, resp_buf
, res
.acl_len
);
3337 __free_page(localpage
);
3341 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3343 struct nfs4_exception exception
= { };
3346 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3349 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3350 } while (exception
.retry
);
3354 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3356 struct nfs_server
*server
= NFS_SERVER(inode
);
3359 if (!nfs4_server_supports_acls(server
))
3361 ret
= nfs_revalidate_inode(server
, inode
);
3364 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
3365 nfs_zap_acl_cache(inode
);
3366 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3369 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3372 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3374 struct nfs_server
*server
= NFS_SERVER(inode
);
3375 struct page
*pages
[NFS4ACL_MAXPAGES
];
3376 struct nfs_setaclargs arg
= {
3377 .fh
= NFS_FH(inode
),
3381 struct nfs_setaclres res
;
3382 struct rpc_message msg
= {
3383 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3389 if (!nfs4_server_supports_acls(server
))
3391 nfs_inode_return_delegation(inode
);
3392 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3393 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3395 * Acl update can result in inode attribute update.
3396 * so mark the attribute cache invalid.
3398 spin_lock(&inode
->i_lock
);
3399 NFS_I(inode
)->cache_validity
|= NFS_INO_INVALID_ATTR
;
3400 spin_unlock(&inode
->i_lock
);
3401 nfs_access_zap_cache(inode
);
3402 nfs_zap_acl_cache(inode
);
3406 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3408 struct nfs4_exception exception
= { };
3411 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3412 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3414 } while (exception
.retry
);
3419 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3421 struct nfs_client
*clp
= server
->nfs_client
;
3423 if (task
->tk_status
>= 0)
3425 switch(task
->tk_status
) {
3426 case -NFS4ERR_ADMIN_REVOKED
:
3427 case -NFS4ERR_BAD_STATEID
:
3428 case -NFS4ERR_OPENMODE
:
3431 nfs4_state_mark_reclaim_nograce(clp
, state
);
3432 goto do_state_recovery
;
3433 case -NFS4ERR_STALE_STATEID
:
3434 case -NFS4ERR_STALE_CLIENTID
:
3435 case -NFS4ERR_EXPIRED
:
3436 goto do_state_recovery
;
3437 #if defined(CONFIG_NFS_V4_1)
3438 case -NFS4ERR_BADSESSION
:
3439 case -NFS4ERR_BADSLOT
:
3440 case -NFS4ERR_BAD_HIGH_SLOT
:
3441 case -NFS4ERR_DEADSESSION
:
3442 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3443 case -NFS4ERR_SEQ_FALSE_RETRY
:
3444 case -NFS4ERR_SEQ_MISORDERED
:
3445 dprintk("%s ERROR %d, Reset session\n", __func__
,
3447 nfs4_schedule_state_recovery(clp
);
3448 task
->tk_status
= 0;
3450 #endif /* CONFIG_NFS_V4_1 */
3451 case -NFS4ERR_DELAY
:
3452 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3453 case -NFS4ERR_GRACE
:
3455 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3456 task
->tk_status
= 0;
3458 case -NFS4ERR_OLD_STATEID
:
3459 task
->tk_status
= 0;
3462 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3465 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3466 nfs4_schedule_state_recovery(clp
);
3467 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3468 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3469 task
->tk_status
= 0;
3473 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
3474 unsigned short port
, struct rpc_cred
*cred
,
3475 struct nfs4_setclientid_res
*res
)
3477 nfs4_verifier sc_verifier
;
3478 struct nfs4_setclientid setclientid
= {
3479 .sc_verifier
= &sc_verifier
,
3482 struct rpc_message msg
= {
3483 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3484 .rpc_argp
= &setclientid
,
3492 p
= (__be32
*)sc_verifier
.data
;
3493 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3494 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3497 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3498 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3500 rpc_peeraddr2str(clp
->cl_rpcclient
,
3502 rpc_peeraddr2str(clp
->cl_rpcclient
,
3504 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3505 clp
->cl_id_uniquifier
);
3506 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3507 sizeof(setclientid
.sc_netid
),
3508 rpc_peeraddr2str(clp
->cl_rpcclient
,
3509 RPC_DISPLAY_NETID
));
3510 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3511 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3512 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3514 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3515 if (status
!= -NFS4ERR_CLID_INUSE
)
3520 ssleep(clp
->cl_lease_time
+ 1);
3522 if (++clp
->cl_id_uniquifier
== 0)
3528 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
3529 struct nfs4_setclientid_res
*arg
,
3530 struct rpc_cred
*cred
)
3532 struct nfs_fsinfo fsinfo
;
3533 struct rpc_message msg
= {
3534 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3536 .rpc_resp
= &fsinfo
,
3543 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3545 spin_lock(&clp
->cl_lock
);
3546 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3547 clp
->cl_last_renewal
= now
;
3548 spin_unlock(&clp
->cl_lock
);
3553 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
3554 struct nfs4_setclientid_res
*arg
,
3555 struct rpc_cred
*cred
)
3560 err
= _nfs4_proc_setclientid_confirm(clp
, arg
, cred
);
3564 case -NFS4ERR_RESOURCE
:
3565 /* The IBM lawyers misread another document! */
3566 case -NFS4ERR_DELAY
:
3567 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3573 struct nfs4_delegreturndata
{
3574 struct nfs4_delegreturnargs args
;
3575 struct nfs4_delegreturnres res
;
3577 nfs4_stateid stateid
;
3578 unsigned long timestamp
;
3579 struct nfs_fattr fattr
;
3583 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3585 struct nfs4_delegreturndata
*data
= calldata
;
3587 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
3590 switch (task
->tk_status
) {
3591 case -NFS4ERR_STALE_STATEID
:
3592 case -NFS4ERR_EXPIRED
:
3594 renew_lease(data
->res
.server
, data
->timestamp
);
3597 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
3599 nfs_restart_rpc(task
, data
->res
.server
->nfs_client
);
3603 data
->rpc_status
= task
->tk_status
;
3606 static void nfs4_delegreturn_release(void *calldata
)
3611 #if defined(CONFIG_NFS_V4_1)
3612 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3614 struct nfs4_delegreturndata
*d_data
;
3616 d_data
= (struct nfs4_delegreturndata
*)data
;
3618 if (nfs4_setup_sequence(d_data
->res
.server
,
3619 &d_data
->args
.seq_args
,
3620 &d_data
->res
.seq_res
, 1, task
))
3622 rpc_call_start(task
);
3624 #endif /* CONFIG_NFS_V4_1 */
3626 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3627 #if defined(CONFIG_NFS_V4_1)
3628 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3629 #endif /* CONFIG_NFS_V4_1 */
3630 .rpc_call_done
= nfs4_delegreturn_done
,
3631 .rpc_release
= nfs4_delegreturn_release
,
3634 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3636 struct nfs4_delegreturndata
*data
;
3637 struct nfs_server
*server
= NFS_SERVER(inode
);
3638 struct rpc_task
*task
;
3639 struct rpc_message msg
= {
3640 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3643 struct rpc_task_setup task_setup_data
= {
3644 .rpc_client
= server
->client
,
3645 .rpc_message
= &msg
,
3646 .callback_ops
= &nfs4_delegreturn_ops
,
3647 .flags
= RPC_TASK_ASYNC
,
3651 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
3654 data
->args
.fhandle
= &data
->fh
;
3655 data
->args
.stateid
= &data
->stateid
;
3656 data
->args
.bitmask
= server
->attr_bitmask
;
3657 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3658 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3659 data
->res
.fattr
= &data
->fattr
;
3660 data
->res
.server
= server
;
3661 nfs_fattr_init(data
->res
.fattr
);
3662 data
->timestamp
= jiffies
;
3663 data
->rpc_status
= 0;
3665 task_setup_data
.callback_data
= data
;
3666 msg
.rpc_argp
= &data
->args
,
3667 msg
.rpc_resp
= &data
->res
,
3668 task
= rpc_run_task(&task_setup_data
);
3670 return PTR_ERR(task
);
3673 status
= nfs4_wait_for_completion_rpc_task(task
);
3676 status
= data
->rpc_status
;
3679 nfs_refresh_inode(inode
, &data
->fattr
);
3685 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3687 struct nfs_server
*server
= NFS_SERVER(inode
);
3688 struct nfs4_exception exception
= { };
3691 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3693 case -NFS4ERR_STALE_STATEID
:
3694 case -NFS4ERR_EXPIRED
:
3698 err
= nfs4_handle_exception(server
, err
, &exception
);
3699 } while (exception
.retry
);
3703 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3704 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3707 * sleep, with exponential backoff, and retry the LOCK operation.
3709 static unsigned long
3710 nfs4_set_lock_task_retry(unsigned long timeout
)
3712 schedule_timeout_killable(timeout
);
3714 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3715 return NFS4_LOCK_MAXTIMEOUT
;
3719 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3721 struct inode
*inode
= state
->inode
;
3722 struct nfs_server
*server
= NFS_SERVER(inode
);
3723 struct nfs_client
*clp
= server
->nfs_client
;
3724 struct nfs_lockt_args arg
= {
3725 .fh
= NFS_FH(inode
),
3728 struct nfs_lockt_res res
= {
3731 struct rpc_message msg
= {
3732 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3735 .rpc_cred
= state
->owner
->so_cred
,
3737 struct nfs4_lock_state
*lsp
;
3740 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3741 status
= nfs4_set_lock_state(state
, request
);
3744 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3745 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3746 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3749 request
->fl_type
= F_UNLCK
;
3751 case -NFS4ERR_DENIED
:
3754 request
->fl_ops
->fl_release_private(request
);
3759 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3761 struct nfs4_exception exception
= { };
3765 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3766 _nfs4_proc_getlk(state
, cmd
, request
),
3768 } while (exception
.retry
);
3772 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3775 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3777 res
= posix_lock_file_wait(file
, fl
);
3780 res
= flock_lock_file_wait(file
, fl
);
3788 struct nfs4_unlockdata
{
3789 struct nfs_locku_args arg
;
3790 struct nfs_locku_res res
;
3791 struct nfs4_lock_state
*lsp
;
3792 struct nfs_open_context
*ctx
;
3793 struct file_lock fl
;
3794 const struct nfs_server
*server
;
3795 unsigned long timestamp
;
3798 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3799 struct nfs_open_context
*ctx
,
3800 struct nfs4_lock_state
*lsp
,
3801 struct nfs_seqid
*seqid
)
3803 struct nfs4_unlockdata
*p
;
3804 struct inode
*inode
= lsp
->ls_state
->inode
;
3806 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
3809 p
->arg
.fh
= NFS_FH(inode
);
3811 p
->arg
.seqid
= seqid
;
3812 p
->res
.seqid
= seqid
;
3813 p
->arg
.stateid
= &lsp
->ls_stateid
;
3815 atomic_inc(&lsp
->ls_count
);
3816 /* Ensure we don't close file until we're done freeing locks! */
3817 p
->ctx
= get_nfs_open_context(ctx
);
3818 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3819 p
->server
= NFS_SERVER(inode
);
3823 static void nfs4_locku_release_calldata(void *data
)
3825 struct nfs4_unlockdata
*calldata
= data
;
3826 nfs_free_seqid(calldata
->arg
.seqid
);
3827 nfs4_put_lock_state(calldata
->lsp
);
3828 put_nfs_open_context(calldata
->ctx
);
3832 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3834 struct nfs4_unlockdata
*calldata
= data
;
3836 if (!nfs4_sequence_done(task
, &calldata
->res
.seq_res
))
3838 switch (task
->tk_status
) {
3840 memcpy(calldata
->lsp
->ls_stateid
.data
,
3841 calldata
->res
.stateid
.data
,
3842 sizeof(calldata
->lsp
->ls_stateid
.data
));
3843 renew_lease(calldata
->server
, calldata
->timestamp
);
3845 case -NFS4ERR_BAD_STATEID
:
3846 case -NFS4ERR_OLD_STATEID
:
3847 case -NFS4ERR_STALE_STATEID
:
3848 case -NFS4ERR_EXPIRED
:
3851 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3852 nfs_restart_rpc(task
,
3853 calldata
->server
->nfs_client
);
3857 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3859 struct nfs4_unlockdata
*calldata
= data
;
3861 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3863 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3864 /* Note: exit _without_ running nfs4_locku_done */
3865 task
->tk_action
= NULL
;
3868 calldata
->timestamp
= jiffies
;
3869 if (nfs4_setup_sequence(calldata
->server
,
3870 &calldata
->arg
.seq_args
,
3871 &calldata
->res
.seq_res
, 1, task
))
3873 rpc_call_start(task
);
3876 static const struct rpc_call_ops nfs4_locku_ops
= {
3877 .rpc_call_prepare
= nfs4_locku_prepare
,
3878 .rpc_call_done
= nfs4_locku_done
,
3879 .rpc_release
= nfs4_locku_release_calldata
,
3882 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3883 struct nfs_open_context
*ctx
,
3884 struct nfs4_lock_state
*lsp
,
3885 struct nfs_seqid
*seqid
)
3887 struct nfs4_unlockdata
*data
;
3888 struct rpc_message msg
= {
3889 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3890 .rpc_cred
= ctx
->cred
,
3892 struct rpc_task_setup task_setup_data
= {
3893 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3894 .rpc_message
= &msg
,
3895 .callback_ops
= &nfs4_locku_ops
,
3896 .workqueue
= nfsiod_workqueue
,
3897 .flags
= RPC_TASK_ASYNC
,
3900 /* Ensure this is an unlock - when canceling a lock, the
3901 * canceled lock is passed in, and it won't be an unlock.
3903 fl
->fl_type
= F_UNLCK
;
3905 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3907 nfs_free_seqid(seqid
);
3908 return ERR_PTR(-ENOMEM
);
3911 msg
.rpc_argp
= &data
->arg
,
3912 msg
.rpc_resp
= &data
->res
,
3913 task_setup_data
.callback_data
= data
;
3914 return rpc_run_task(&task_setup_data
);
3917 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3919 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3920 struct nfs_seqid
*seqid
;
3921 struct nfs4_lock_state
*lsp
;
3922 struct rpc_task
*task
;
3924 unsigned char fl_flags
= request
->fl_flags
;
3926 status
= nfs4_set_lock_state(state
, request
);
3927 /* Unlock _before_ we do the RPC call */
3928 request
->fl_flags
|= FL_EXISTS
;
3929 down_read(&nfsi
->rwsem
);
3930 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3931 up_read(&nfsi
->rwsem
);
3934 up_read(&nfsi
->rwsem
);
3937 /* Is this a delegated lock? */
3938 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3940 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3941 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
3945 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3946 status
= PTR_ERR(task
);
3949 status
= nfs4_wait_for_completion_rpc_task(task
);
3952 request
->fl_flags
= fl_flags
;
3956 struct nfs4_lockdata
{
3957 struct nfs_lock_args arg
;
3958 struct nfs_lock_res res
;
3959 struct nfs4_lock_state
*lsp
;
3960 struct nfs_open_context
*ctx
;
3961 struct file_lock fl
;
3962 unsigned long timestamp
;
3965 struct nfs_server
*server
;
3968 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3969 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
3972 struct nfs4_lockdata
*p
;
3973 struct inode
*inode
= lsp
->ls_state
->inode
;
3974 struct nfs_server
*server
= NFS_SERVER(inode
);
3976 p
= kzalloc(sizeof(*p
), gfp_mask
);
3980 p
->arg
.fh
= NFS_FH(inode
);
3982 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
3983 if (p
->arg
.open_seqid
== NULL
)
3985 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
3986 if (p
->arg
.lock_seqid
== NULL
)
3987 goto out_free_seqid
;
3988 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
3989 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
3990 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3991 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
3994 atomic_inc(&lsp
->ls_count
);
3995 p
->ctx
= get_nfs_open_context(ctx
);
3996 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3999 nfs_free_seqid(p
->arg
.open_seqid
);
4005 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
4007 struct nfs4_lockdata
*data
= calldata
;
4008 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4010 dprintk("%s: begin!\n", __func__
);
4011 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4013 /* Do we need to do an open_to_lock_owner? */
4014 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4015 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
4017 data
->arg
.open_stateid
= &state
->stateid
;
4018 data
->arg
.new_lock_owner
= 1;
4019 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4021 data
->arg
.new_lock_owner
= 0;
4022 data
->timestamp
= jiffies
;
4023 if (nfs4_setup_sequence(data
->server
,
4024 &data
->arg
.seq_args
,
4025 &data
->res
.seq_res
, 1, task
))
4027 rpc_call_start(task
);
4028 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4031 static void nfs4_recover_lock_prepare(struct rpc_task
*task
, void *calldata
)
4033 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4034 nfs4_lock_prepare(task
, calldata
);
4037 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4039 struct nfs4_lockdata
*data
= calldata
;
4041 dprintk("%s: begin!\n", __func__
);
4043 if (!nfs4_sequence_done(task
, &data
->res
.seq_res
))
4046 data
->rpc_status
= task
->tk_status
;
4047 if (data
->arg
.new_lock_owner
!= 0) {
4048 if (data
->rpc_status
== 0)
4049 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4053 if (data
->rpc_status
== 0) {
4054 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
4055 sizeof(data
->lsp
->ls_stateid
.data
));
4056 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
4057 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4060 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4063 static void nfs4_lock_release(void *calldata
)
4065 struct nfs4_lockdata
*data
= calldata
;
4067 dprintk("%s: begin!\n", __func__
);
4068 nfs_free_seqid(data
->arg
.open_seqid
);
4069 if (data
->cancelled
!= 0) {
4070 struct rpc_task
*task
;
4071 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4072 data
->arg
.lock_seqid
);
4075 dprintk("%s: cancelling lock!\n", __func__
);
4077 nfs_free_seqid(data
->arg
.lock_seqid
);
4078 nfs4_put_lock_state(data
->lsp
);
4079 put_nfs_open_context(data
->ctx
);
4081 dprintk("%s: done!\n", __func__
);
4084 static const struct rpc_call_ops nfs4_lock_ops
= {
4085 .rpc_call_prepare
= nfs4_lock_prepare
,
4086 .rpc_call_done
= nfs4_lock_done
,
4087 .rpc_release
= nfs4_lock_release
,
4090 static const struct rpc_call_ops nfs4_recover_lock_ops
= {
4091 .rpc_call_prepare
= nfs4_recover_lock_prepare
,
4092 .rpc_call_done
= nfs4_lock_done
,
4093 .rpc_release
= nfs4_lock_release
,
4096 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4098 struct nfs_client
*clp
= server
->nfs_client
;
4099 struct nfs4_state
*state
= lsp
->ls_state
;
4102 case -NFS4ERR_ADMIN_REVOKED
:
4103 case -NFS4ERR_BAD_STATEID
:
4104 case -NFS4ERR_EXPIRED
:
4105 if (new_lock_owner
!= 0 ||
4106 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4107 nfs4_state_mark_reclaim_nograce(clp
, state
);
4108 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4110 case -NFS4ERR_STALE_STATEID
:
4111 if (new_lock_owner
!= 0 ||
4112 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4113 nfs4_state_mark_reclaim_reboot(clp
, state
);
4114 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4118 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4120 struct nfs4_lockdata
*data
;
4121 struct rpc_task
*task
;
4122 struct rpc_message msg
= {
4123 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4124 .rpc_cred
= state
->owner
->so_cred
,
4126 struct rpc_task_setup task_setup_data
= {
4127 .rpc_client
= NFS_CLIENT(state
->inode
),
4128 .rpc_message
= &msg
,
4129 .callback_ops
= &nfs4_lock_ops
,
4130 .workqueue
= nfsiod_workqueue
,
4131 .flags
= RPC_TASK_ASYNC
,
4135 dprintk("%s: begin!\n", __func__
);
4136 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4137 fl
->fl_u
.nfs4_fl
.owner
,
4138 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
4142 data
->arg
.block
= 1;
4143 if (recovery_type
> NFS_LOCK_NEW
) {
4144 if (recovery_type
== NFS_LOCK_RECLAIM
)
4145 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4146 task_setup_data
.callback_ops
= &nfs4_recover_lock_ops
;
4148 msg
.rpc_argp
= &data
->arg
,
4149 msg
.rpc_resp
= &data
->res
,
4150 task_setup_data
.callback_data
= data
;
4151 task
= rpc_run_task(&task_setup_data
);
4153 return PTR_ERR(task
);
4154 ret
= nfs4_wait_for_completion_rpc_task(task
);
4156 ret
= data
->rpc_status
;
4158 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4159 data
->arg
.new_lock_owner
, ret
);
4161 data
->cancelled
= 1;
4163 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4167 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4169 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4170 struct nfs4_exception exception
= { };
4174 /* Cache the lock if possible... */
4175 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4177 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4178 if (err
!= -NFS4ERR_DELAY
)
4180 nfs4_handle_exception(server
, err
, &exception
);
4181 } while (exception
.retry
);
4185 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4187 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4188 struct nfs4_exception exception
= { };
4191 err
= nfs4_set_lock_state(state
, request
);
4195 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4197 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4201 case -NFS4ERR_GRACE
:
4202 case -NFS4ERR_DELAY
:
4203 nfs4_handle_exception(server
, err
, &exception
);
4206 } while (exception
.retry
);
4211 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4213 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4214 unsigned char fl_flags
= request
->fl_flags
;
4215 int status
= -ENOLCK
;
4217 if ((fl_flags
& FL_POSIX
) &&
4218 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4220 /* Is this a delegated open? */
4221 status
= nfs4_set_lock_state(state
, request
);
4224 request
->fl_flags
|= FL_ACCESS
;
4225 status
= do_vfs_lock(request
->fl_file
, request
);
4228 down_read(&nfsi
->rwsem
);
4229 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4230 /* Yes: cache locks! */
4231 /* ...but avoid races with delegation recall... */
4232 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4233 status
= do_vfs_lock(request
->fl_file
, request
);
4236 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4239 /* Note: we always want to sleep here! */
4240 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4241 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4242 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4244 up_read(&nfsi
->rwsem
);
4246 request
->fl_flags
= fl_flags
;
4250 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4252 struct nfs4_exception exception
= { };
4256 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4257 if (err
== -NFS4ERR_DENIED
)
4259 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4261 } while (exception
.retry
);
4266 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4268 struct nfs_open_context
*ctx
;
4269 struct nfs4_state
*state
;
4270 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4273 /* verify open state */
4274 ctx
= nfs_file_open_context(filp
);
4277 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4280 if (IS_GETLK(cmd
)) {
4282 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4286 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4289 if (request
->fl_type
== F_UNLCK
) {
4291 return nfs4_proc_unlck(state
, cmd
, request
);
4298 status
= nfs4_proc_setlk(state
, cmd
, request
);
4299 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4301 timeout
= nfs4_set_lock_task_retry(timeout
);
4302 status
= -ERESTARTSYS
;
4305 } while(status
< 0);
4309 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4311 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4312 struct nfs4_exception exception
= { };
4315 err
= nfs4_set_lock_state(state
, fl
);
4319 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4322 printk(KERN_ERR
"%s: unhandled error %d.\n",
4327 case -NFS4ERR_EXPIRED
:
4328 case -NFS4ERR_STALE_CLIENTID
:
4329 case -NFS4ERR_STALE_STATEID
:
4330 case -NFS4ERR_BADSESSION
:
4331 case -NFS4ERR_BADSLOT
:
4332 case -NFS4ERR_BAD_HIGH_SLOT
:
4333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4334 case -NFS4ERR_DEADSESSION
:
4335 nfs4_schedule_state_recovery(server
->nfs_client
);
4339 * The show must go on: exit, but mark the
4340 * stateid as needing recovery.
4342 case -NFS4ERR_ADMIN_REVOKED
:
4343 case -NFS4ERR_BAD_STATEID
:
4344 case -NFS4ERR_OPENMODE
:
4345 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
4350 * User RPCSEC_GSS context has expired.
4351 * We cannot recover this stateid now, so
4352 * skip it and allow recovery thread to
4358 case -NFS4ERR_DENIED
:
4359 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4362 case -NFS4ERR_DELAY
:
4365 err
= nfs4_handle_exception(server
, err
, &exception
);
4366 } while (exception
.retry
);
4371 static void nfs4_release_lockowner_release(void *calldata
)
4376 const struct rpc_call_ops nfs4_release_lockowner_ops
= {
4377 .rpc_release
= nfs4_release_lockowner_release
,
4380 void nfs4_release_lockowner(const struct nfs4_lock_state
*lsp
)
4382 struct nfs_server
*server
= lsp
->ls_state
->owner
->so_server
;
4383 struct nfs_release_lockowner_args
*args
;
4384 struct rpc_message msg
= {
4385 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RELEASE_LOCKOWNER
],
4388 if (server
->nfs_client
->cl_mvops
->minor_version
!= 0)
4390 args
= kmalloc(sizeof(*args
), GFP_NOFS
);
4393 args
->lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4394 args
->lock_owner
.id
= lsp
->ls_id
.id
;
4395 msg
.rpc_argp
= args
;
4396 rpc_call_async(server
->client
, &msg
, 0, &nfs4_release_lockowner_ops
, args
);
4399 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4401 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4402 size_t buflen
, int flags
)
4404 struct inode
*inode
= dentry
->d_inode
;
4406 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4409 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4412 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4413 * and that's what we'll do for e.g. user attributes that haven't been set.
4414 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4415 * attributes in kernel-managed attribute namespaces. */
4416 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4419 struct inode
*inode
= dentry
->d_inode
;
4421 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4424 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4427 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4429 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4431 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4433 if (buf
&& buflen
< len
)
4436 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4440 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4442 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4443 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4444 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4447 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4448 NFS_ATTR_FATTR_NLINK
;
4449 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4453 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4454 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4456 struct nfs_server
*server
= NFS_SERVER(dir
);
4458 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4459 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4461 struct nfs4_fs_locations_arg args
= {
4462 .dir_fh
= NFS_FH(dir
),
4467 struct nfs4_fs_locations_res res
= {
4468 .fs_locations
= fs_locations
,
4470 struct rpc_message msg
= {
4471 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4477 dprintk("%s: start\n", __func__
);
4478 nfs_fattr_init(&fs_locations
->fattr
);
4479 fs_locations
->server
= server
;
4480 fs_locations
->nlocations
= 0;
4481 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4482 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4483 dprintk("%s: returned status = %d\n", __func__
, status
);
4487 #ifdef CONFIG_NFS_V4_1
4489 * nfs4_proc_exchange_id()
4491 * Since the clientid has expired, all compounds using sessions
4492 * associated with the stale clientid will be returning
4493 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4494 * be in some phase of session reset.
4496 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4498 nfs4_verifier verifier
;
4499 struct nfs41_exchange_id_args args
= {
4501 .flags
= clp
->cl_exchange_flags
,
4503 struct nfs41_exchange_id_res res
= {
4507 struct rpc_message msg
= {
4508 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4515 dprintk("--> %s\n", __func__
);
4516 BUG_ON(clp
== NULL
);
4518 /* Remove server-only flags */
4519 args
.flags
&= ~EXCHGID4_FLAG_CONFIRMED_R
;
4521 p
= (u32
*)verifier
.data
;
4522 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4523 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4524 args
.verifier
= &verifier
;
4527 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4530 rpc_peeraddr2str(clp
->cl_rpcclient
,
4532 clp
->cl_id_uniquifier
);
4534 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4536 if (status
!= -NFS4ERR_CLID_INUSE
)
4542 if (++clp
->cl_id_uniquifier
== 0)
4546 dprintk("<-- %s status= %d\n", __func__
, status
);
4550 struct nfs4_get_lease_time_data
{
4551 struct nfs4_get_lease_time_args
*args
;
4552 struct nfs4_get_lease_time_res
*res
;
4553 struct nfs_client
*clp
;
4556 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4560 struct nfs4_get_lease_time_data
*data
=
4561 (struct nfs4_get_lease_time_data
*)calldata
;
4563 dprintk("--> %s\n", __func__
);
4564 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4565 /* just setup sequence, do not trigger session recovery
4566 since we're invoked within one */
4567 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4568 &data
->args
->la_seq_args
,
4569 &data
->res
->lr_seq_res
, 0, task
);
4571 BUG_ON(ret
== -EAGAIN
);
4572 rpc_call_start(task
);
4573 dprintk("<-- %s\n", __func__
);
4577 * Called from nfs4_state_manager thread for session setup, so don't recover
4578 * from sequence operation or clientid errors.
4580 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4582 struct nfs4_get_lease_time_data
*data
=
4583 (struct nfs4_get_lease_time_data
*)calldata
;
4585 dprintk("--> %s\n", __func__
);
4586 if (!nfs41_sequence_done(task
, &data
->res
->lr_seq_res
))
4588 switch (task
->tk_status
) {
4589 case -NFS4ERR_DELAY
:
4590 case -NFS4ERR_GRACE
:
4591 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4592 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4593 task
->tk_status
= 0;
4594 nfs_restart_rpc(task
, data
->clp
);
4597 dprintk("<-- %s\n", __func__
);
4600 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4601 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4602 .rpc_call_done
= nfs4_get_lease_time_done
,
4605 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4607 struct rpc_task
*task
;
4608 struct nfs4_get_lease_time_args args
;
4609 struct nfs4_get_lease_time_res res
= {
4610 .lr_fsinfo
= fsinfo
,
4612 struct nfs4_get_lease_time_data data
= {
4617 struct rpc_message msg
= {
4618 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4622 struct rpc_task_setup task_setup
= {
4623 .rpc_client
= clp
->cl_rpcclient
,
4624 .rpc_message
= &msg
,
4625 .callback_ops
= &nfs4_get_lease_time_ops
,
4626 .callback_data
= &data
4630 dprintk("--> %s\n", __func__
);
4631 task
= rpc_run_task(&task_setup
);
4634 status
= PTR_ERR(task
);
4636 status
= task
->tk_status
;
4639 dprintk("<-- %s return %d\n", __func__
, status
);
4645 * Reset a slot table
4647 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, u32 max_reqs
,
4650 struct nfs4_slot
*new = NULL
;
4654 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__
,
4655 max_reqs
, tbl
->max_slots
);
4657 /* Does the newly negotiated max_reqs match the existing slot table? */
4658 if (max_reqs
!= tbl
->max_slots
) {
4660 new = kmalloc(max_reqs
* sizeof(struct nfs4_slot
),
4667 spin_lock(&tbl
->slot_tbl_lock
);
4670 tbl
->max_slots
= max_reqs
;
4672 for (i
= 0; i
< tbl
->max_slots
; ++i
)
4673 tbl
->slots
[i
].seq_nr
= ivalue
;
4674 spin_unlock(&tbl
->slot_tbl_lock
);
4675 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4676 tbl
, tbl
->slots
, tbl
->max_slots
);
4678 dprintk("<-- %s: return %d\n", __func__
, ret
);
4683 * Reset the forechannel and backchannel slot tables
4685 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
4689 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
4690 session
->fc_attrs
.max_reqs
, 1);
4694 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
4695 session
->bc_attrs
.max_reqs
, 0);
4699 /* Destroy the slot table */
4700 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
4702 if (session
->fc_slot_table
.slots
!= NULL
) {
4703 kfree(session
->fc_slot_table
.slots
);
4704 session
->fc_slot_table
.slots
= NULL
;
4706 if (session
->bc_slot_table
.slots
!= NULL
) {
4707 kfree(session
->bc_slot_table
.slots
);
4708 session
->bc_slot_table
.slots
= NULL
;
4714 * Initialize slot table
4716 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
4717 int max_slots
, int ivalue
)
4719 struct nfs4_slot
*slot
;
4722 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4724 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
4726 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_NOFS
);
4731 spin_lock(&tbl
->slot_tbl_lock
);
4732 tbl
->max_slots
= max_slots
;
4734 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4735 spin_unlock(&tbl
->slot_tbl_lock
);
4736 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4737 tbl
, tbl
->slots
, tbl
->max_slots
);
4739 dprintk("<-- %s: return %d\n", __func__
, ret
);
4744 * Initialize the forechannel and backchannel tables
4746 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
4748 struct nfs4_slot_table
*tbl
;
4751 tbl
= &session
->fc_slot_table
;
4752 if (tbl
->slots
== NULL
) {
4753 status
= nfs4_init_slot_table(tbl
,
4754 session
->fc_attrs
.max_reqs
, 1);
4759 tbl
= &session
->bc_slot_table
;
4760 if (tbl
->slots
== NULL
) {
4761 status
= nfs4_init_slot_table(tbl
,
4762 session
->bc_attrs
.max_reqs
, 0);
4764 nfs4_destroy_slot_tables(session
);
4770 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4772 struct nfs4_session
*session
;
4773 struct nfs4_slot_table
*tbl
;
4775 session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
4779 init_completion(&session
->complete
);
4781 tbl
= &session
->fc_slot_table
;
4782 tbl
->highest_used_slotid
= -1;
4783 spin_lock_init(&tbl
->slot_tbl_lock
);
4784 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
4786 tbl
= &session
->bc_slot_table
;
4787 tbl
->highest_used_slotid
= -1;
4788 spin_lock_init(&tbl
->slot_tbl_lock
);
4789 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
4791 session
->session_state
= 1<<NFS4_SESSION_INITING
;
4797 void nfs4_destroy_session(struct nfs4_session
*session
)
4799 nfs4_proc_destroy_session(session
);
4800 dprintk("%s Destroy backchannel for xprt %p\n",
4801 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
4802 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
4803 NFS41_BC_MIN_CALLBACKS
);
4804 nfs4_destroy_slot_tables(session
);
4809 * Initialize the values to be used by the client in CREATE_SESSION
4810 * If nfs4_init_session set the fore channel request and response sizes,
4813 * Set the back channel max_resp_sz_cached to zero to force the client to
4814 * always set csa_cachethis to FALSE because the current implementation
4815 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4817 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4819 struct nfs4_session
*session
= args
->client
->cl_session
;
4820 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4821 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4824 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4826 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4827 /* Fore channel attributes */
4828 args
->fc_attrs
.headerpadsz
= 0;
4829 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4830 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4831 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4832 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4834 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4835 "max_ops=%u max_reqs=%u\n",
4837 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4838 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
4840 /* Back channel attributes */
4841 args
->bc_attrs
.headerpadsz
= 0;
4842 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4843 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4844 args
->bc_attrs
.max_resp_sz_cached
= 0;
4845 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4846 args
->bc_attrs
.max_reqs
= 1;
4848 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4849 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4851 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4852 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4853 args
->bc_attrs
.max_reqs
);
4856 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
4858 struct nfs4_channel_attrs
*sent
= &args
->fc_attrs
;
4859 struct nfs4_channel_attrs
*rcvd
= &session
->fc_attrs
;
4861 if (rcvd
->headerpadsz
> sent
->headerpadsz
)
4863 if (rcvd
->max_resp_sz
> sent
->max_resp_sz
)
4866 * Our requested max_ops is the minimum we need; we're not
4867 * prepared to break up compounds into smaller pieces than that.
4868 * So, no point even trying to continue if the server won't
4871 if (rcvd
->max_ops
< sent
->max_ops
)
4873 if (rcvd
->max_reqs
== 0)
4878 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args
*args
, struct nfs4_session
*session
)
4880 struct nfs4_channel_attrs
*sent
= &args
->bc_attrs
;
4881 struct nfs4_channel_attrs
*rcvd
= &session
->bc_attrs
;
4883 if (rcvd
->max_rqst_sz
> sent
->max_rqst_sz
)
4885 if (rcvd
->max_resp_sz
< sent
->max_resp_sz
)
4887 if (rcvd
->max_resp_sz_cached
> sent
->max_resp_sz_cached
)
4889 /* These would render the backchannel useless: */
4890 if (rcvd
->max_ops
== 0)
4892 if (rcvd
->max_reqs
== 0)
4897 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4898 struct nfs4_session
*session
)
4902 ret
= nfs4_verify_fore_channel_attrs(args
, session
);
4905 return nfs4_verify_back_channel_attrs(args
, session
);
4908 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4910 struct nfs4_session
*session
= clp
->cl_session
;
4911 struct nfs41_create_session_args args
= {
4913 .cb_program
= NFS4_CALLBACK
,
4915 struct nfs41_create_session_res res
= {
4918 struct rpc_message msg
= {
4919 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4925 nfs4_init_channel_attrs(&args
);
4926 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
4928 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4931 /* Verify the session's negotiated channel_attrs values */
4932 status
= nfs4_verify_channel_attrs(&args
, session
);
4934 /* Increment the clientid slot sequence id */
4942 * Issues a CREATE_SESSION operation to the server.
4943 * It is the responsibility of the caller to verify the session is
4944 * expired before calling this routine.
4946 int nfs4_proc_create_session(struct nfs_client
*clp
)
4950 struct nfs4_session
*session
= clp
->cl_session
;
4952 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4954 status
= _nfs4_proc_create_session(clp
);
4958 /* Init and reset the fore channel */
4959 status
= nfs4_init_slot_tables(session
);
4960 dprintk("slot table initialization returned %d\n", status
);
4963 status
= nfs4_reset_slot_tables(session
);
4964 dprintk("slot table reset returned %d\n", status
);
4968 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4969 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4970 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4972 dprintk("<-- %s\n", __func__
);
4977 * Issue the over-the-wire RPC DESTROY_SESSION.
4978 * The caller must serialize access to this routine.
4980 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4983 struct rpc_message msg
;
4985 dprintk("--> nfs4_proc_destroy_session\n");
4987 /* session is still being setup */
4988 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
4991 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
4992 msg
.rpc_argp
= session
;
4993 msg
.rpc_resp
= NULL
;
4994 msg
.rpc_cred
= NULL
;
4995 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4999 "Got error %d from the server on DESTROY_SESSION. "
5000 "Session has been destroyed regardless...\n", status
);
5002 dprintk("<-- nfs4_proc_destroy_session\n");
5006 int nfs4_init_session(struct nfs_server
*server
)
5008 struct nfs_client
*clp
= server
->nfs_client
;
5009 struct nfs4_session
*session
;
5010 unsigned int rsize
, wsize
;
5013 if (!nfs4_has_session(clp
))
5016 session
= clp
->cl_session
;
5017 if (!test_and_clear_bit(NFS4_SESSION_INITING
, &session
->session_state
))
5020 rsize
= server
->rsize
;
5022 rsize
= NFS_MAX_FILE_IO_SIZE
;
5023 wsize
= server
->wsize
;
5025 wsize
= NFS_MAX_FILE_IO_SIZE
;
5027 session
->fc_attrs
.max_rqst_sz
= wsize
+ nfs41_maxwrite_overhead
;
5028 session
->fc_attrs
.max_resp_sz
= rsize
+ nfs41_maxread_overhead
;
5030 ret
= nfs4_recover_expired_lease(server
);
5032 ret
= nfs4_check_client_ready(clp
);
5037 * Renew the cl_session lease.
5039 struct nfs4_sequence_data
{
5040 struct nfs_client
*clp
;
5041 struct nfs4_sequence_args args
;
5042 struct nfs4_sequence_res res
;
5045 static void nfs41_sequence_release(void *data
)
5047 struct nfs4_sequence_data
*calldata
= data
;
5048 struct nfs_client
*clp
= calldata
->clp
;
5050 if (atomic_read(&clp
->cl_count
) > 1)
5051 nfs4_schedule_state_renewal(clp
);
5052 nfs_put_client(clp
);
5056 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5058 switch(task
->tk_status
) {
5059 case -NFS4ERR_DELAY
:
5060 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5063 nfs4_schedule_state_recovery(clp
);
5068 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5070 struct nfs4_sequence_data
*calldata
= data
;
5071 struct nfs_client
*clp
= calldata
->clp
;
5073 if (!nfs41_sequence_done(task
, task
->tk_msg
.rpc_resp
))
5076 if (task
->tk_status
< 0) {
5077 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5078 if (atomic_read(&clp
->cl_count
) == 1)
5081 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
5082 rpc_restart_call_prepare(task
);
5086 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5088 dprintk("<-- %s\n", __func__
);
5091 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5093 struct nfs4_sequence_data
*calldata
= data
;
5094 struct nfs_client
*clp
= calldata
->clp
;
5095 struct nfs4_sequence_args
*args
;
5096 struct nfs4_sequence_res
*res
;
5098 args
= task
->tk_msg
.rpc_argp
;
5099 res
= task
->tk_msg
.rpc_resp
;
5101 if (nfs41_setup_sequence(clp
->cl_session
, args
, res
, 0, task
))
5103 rpc_call_start(task
);
5106 static const struct rpc_call_ops nfs41_sequence_ops
= {
5107 .rpc_call_done
= nfs41_sequence_call_done
,
5108 .rpc_call_prepare
= nfs41_sequence_prepare
,
5109 .rpc_release
= nfs41_sequence_release
,
5112 static struct rpc_task
*_nfs41_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5114 struct nfs4_sequence_data
*calldata
;
5115 struct rpc_message msg
= {
5116 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5119 struct rpc_task_setup task_setup_data
= {
5120 .rpc_client
= clp
->cl_rpcclient
,
5121 .rpc_message
= &msg
,
5122 .callback_ops
= &nfs41_sequence_ops
,
5123 .flags
= RPC_TASK_ASYNC
| RPC_TASK_SOFT
,
5126 if (!atomic_inc_not_zero(&clp
->cl_count
))
5127 return ERR_PTR(-EIO
);
5128 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5129 if (calldata
== NULL
) {
5130 nfs_put_client(clp
);
5131 return ERR_PTR(-ENOMEM
);
5133 msg
.rpc_argp
= &calldata
->args
;
5134 msg
.rpc_resp
= &calldata
->res
;
5135 calldata
->clp
= clp
;
5136 task_setup_data
.callback_data
= calldata
;
5138 return rpc_run_task(&task_setup_data
);
5141 static int nfs41_proc_async_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5143 struct rpc_task
*task
;
5146 task
= _nfs41_proc_sequence(clp
, cred
);
5148 ret
= PTR_ERR(task
);
5151 dprintk("<-- %s status=%d\n", __func__
, ret
);
5155 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5157 struct rpc_task
*task
;
5160 task
= _nfs41_proc_sequence(clp
, cred
);
5162 ret
= PTR_ERR(task
);
5165 ret
= rpc_wait_for_completion_task(task
);
5167 ret
= task
->tk_status
;
5170 dprintk("<-- %s status=%d\n", __func__
, ret
);
5174 struct nfs4_reclaim_complete_data
{
5175 struct nfs_client
*clp
;
5176 struct nfs41_reclaim_complete_args arg
;
5177 struct nfs41_reclaim_complete_res res
;
5180 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5182 struct nfs4_reclaim_complete_data
*calldata
= data
;
5184 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
5185 if (nfs41_setup_sequence(calldata
->clp
->cl_session
,
5186 &calldata
->arg
.seq_args
,
5187 &calldata
->res
.seq_res
, 0, task
))
5190 rpc_call_start(task
);
5193 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5195 switch(task
->tk_status
) {
5197 case -NFS4ERR_COMPLETE_ALREADY
:
5198 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
5200 case -NFS4ERR_DELAY
:
5201 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5204 nfs4_schedule_state_recovery(clp
);
5209 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5211 struct nfs4_reclaim_complete_data
*calldata
= data
;
5212 struct nfs_client
*clp
= calldata
->clp
;
5213 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5215 dprintk("--> %s\n", __func__
);
5216 if (!nfs41_sequence_done(task
, res
))
5219 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
5220 rpc_restart_call_prepare(task
);
5223 dprintk("<-- %s\n", __func__
);
5226 static void nfs4_free_reclaim_complete_data(void *data
)
5228 struct nfs4_reclaim_complete_data
*calldata
= data
;
5233 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5234 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5235 .rpc_call_done
= nfs4_reclaim_complete_done
,
5236 .rpc_release
= nfs4_free_reclaim_complete_data
,
5240 * Issue a global reclaim complete.
5242 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5244 struct nfs4_reclaim_complete_data
*calldata
;
5245 struct rpc_task
*task
;
5246 struct rpc_message msg
= {
5247 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5249 struct rpc_task_setup task_setup_data
= {
5250 .rpc_client
= clp
->cl_rpcclient
,
5251 .rpc_message
= &msg
,
5252 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5253 .flags
= RPC_TASK_ASYNC
,
5255 int status
= -ENOMEM
;
5257 dprintk("--> %s\n", __func__
);
5258 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5259 if (calldata
== NULL
)
5261 calldata
->clp
= clp
;
5262 calldata
->arg
.one_fs
= 0;
5264 msg
.rpc_argp
= &calldata
->arg
;
5265 msg
.rpc_resp
= &calldata
->res
;
5266 task_setup_data
.callback_data
= calldata
;
5267 task
= rpc_run_task(&task_setup_data
);
5269 status
= PTR_ERR(task
);
5275 dprintk("<-- %s status=%d\n", __func__
, status
);
5280 nfs4_layoutget_prepare(struct rpc_task
*task
, void *calldata
)
5282 struct nfs4_layoutget
*lgp
= calldata
;
5283 struct inode
*ino
= lgp
->args
.inode
;
5284 struct nfs_server
*server
= NFS_SERVER(ino
);
5286 dprintk("--> %s\n", __func__
);
5287 if (nfs4_setup_sequence(server
, &lgp
->args
.seq_args
,
5288 &lgp
->res
.seq_res
, 0, task
))
5290 rpc_call_start(task
);
5293 static void nfs4_layoutget_done(struct rpc_task
*task
, void *calldata
)
5295 struct nfs4_layoutget
*lgp
= calldata
;
5296 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
5298 dprintk("--> %s\n", __func__
);
5300 if (!nfs4_sequence_done(task
, &lgp
->res
.seq_res
))
5303 switch (task
->tk_status
) {
5306 case -NFS4ERR_LAYOUTTRYLATER
:
5307 case -NFS4ERR_RECALLCONFLICT
:
5308 task
->tk_status
= -NFS4ERR_DELAY
;
5311 if (nfs4_async_handle_error(task
, server
, NULL
) == -EAGAIN
) {
5312 rpc_restart_call_prepare(task
);
5316 lgp
->status
= task
->tk_status
;
5317 dprintk("<-- %s\n", __func__
);
5320 static void nfs4_layoutget_release(void *calldata
)
5322 struct nfs4_layoutget
*lgp
= calldata
;
5324 dprintk("--> %s\n", __func__
);
5325 put_layout_hdr(lgp
->args
.inode
);
5326 if (lgp
->res
.layout
.buf
!= NULL
)
5327 free_page((unsigned long) lgp
->res
.layout
.buf
);
5328 put_nfs_open_context(lgp
->args
.ctx
);
5330 dprintk("<-- %s\n", __func__
);
5333 static const struct rpc_call_ops nfs4_layoutget_call_ops
= {
5334 .rpc_call_prepare
= nfs4_layoutget_prepare
,
5335 .rpc_call_done
= nfs4_layoutget_done
,
5336 .rpc_release
= nfs4_layoutget_release
,
5339 int nfs4_proc_layoutget(struct nfs4_layoutget
*lgp
)
5341 struct nfs_server
*server
= NFS_SERVER(lgp
->args
.inode
);
5342 struct rpc_task
*task
;
5343 struct rpc_message msg
= {
5344 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LAYOUTGET
],
5345 .rpc_argp
= &lgp
->args
,
5346 .rpc_resp
= &lgp
->res
,
5348 struct rpc_task_setup task_setup_data
= {
5349 .rpc_client
= server
->client
,
5350 .rpc_message
= &msg
,
5351 .callback_ops
= &nfs4_layoutget_call_ops
,
5352 .callback_data
= lgp
,
5353 .flags
= RPC_TASK_ASYNC
,
5357 dprintk("--> %s\n", __func__
);
5359 lgp
->res
.layout
.buf
= (void *)__get_free_page(GFP_NOFS
);
5360 if (lgp
->res
.layout
.buf
== NULL
) {
5361 nfs4_layoutget_release(lgp
);
5365 lgp
->res
.seq_res
.sr_slot
= NULL
;
5366 task
= rpc_run_task(&task_setup_data
);
5368 return PTR_ERR(task
);
5369 status
= nfs4_wait_for_completion_rpc_task(task
);
5372 status
= lgp
->status
;
5375 status
= pnfs_layout_process(lgp
);
5378 dprintk("<-- %s status=%d\n", __func__
, status
);
5383 _nfs4_proc_getdeviceinfo(struct nfs_server
*server
, struct pnfs_device
*pdev
)
5385 struct nfs4_getdeviceinfo_args args
= {
5388 struct nfs4_getdeviceinfo_res res
= {
5391 struct rpc_message msg
= {
5392 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETDEVICEINFO
],
5398 dprintk("--> %s\n", __func__
);
5399 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
5400 dprintk("<-- %s status=%d\n", __func__
, status
);
5405 int nfs4_proc_getdeviceinfo(struct nfs_server
*server
, struct pnfs_device
*pdev
)
5407 struct nfs4_exception exception
= { };
5411 err
= nfs4_handle_exception(server
,
5412 _nfs4_proc_getdeviceinfo(server
, pdev
),
5414 } while (exception
.retry
);
5417 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo
);
5419 #endif /* CONFIG_NFS_V4_1 */
5421 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5422 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5423 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5424 .recover_open
= nfs4_open_reclaim
,
5425 .recover_lock
= nfs4_lock_reclaim
,
5426 .establish_clid
= nfs4_init_clientid
,
5427 .get_clid_cred
= nfs4_get_setclientid_cred
,
5430 #if defined(CONFIG_NFS_V4_1)
5431 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5432 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5433 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5434 .recover_open
= nfs4_open_reclaim
,
5435 .recover_lock
= nfs4_lock_reclaim
,
5436 .establish_clid
= nfs41_init_clientid
,
5437 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5438 .reclaim_complete
= nfs41_proc_reclaim_complete
,
5440 #endif /* CONFIG_NFS_V4_1 */
5442 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5443 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5444 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5445 .recover_open
= nfs4_open_expired
,
5446 .recover_lock
= nfs4_lock_expired
,
5447 .establish_clid
= nfs4_init_clientid
,
5448 .get_clid_cred
= nfs4_get_setclientid_cred
,
5451 #if defined(CONFIG_NFS_V4_1)
5452 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5453 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5454 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5455 .recover_open
= nfs4_open_expired
,
5456 .recover_lock
= nfs4_lock_expired
,
5457 .establish_clid
= nfs41_init_clientid
,
5458 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5460 #endif /* CONFIG_NFS_V4_1 */
5462 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
5463 .sched_state_renewal
= nfs4_proc_async_renew
,
5464 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
5465 .renew_lease
= nfs4_proc_renew
,
5468 #if defined(CONFIG_NFS_V4_1)
5469 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
5470 .sched_state_renewal
= nfs41_proc_async_sequence
,
5471 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
5472 .renew_lease
= nfs4_proc_sequence
,
5476 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops
= {
5478 .call_sync
= _nfs4_call_sync
,
5479 .validate_stateid
= nfs4_validate_delegation_stateid
,
5480 .reboot_recovery_ops
= &nfs40_reboot_recovery_ops
,
5481 .nograce_recovery_ops
= &nfs40_nograce_recovery_ops
,
5482 .state_renewal_ops
= &nfs40_state_renewal_ops
,
5485 #if defined(CONFIG_NFS_V4_1)
5486 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops
= {
5488 .call_sync
= _nfs4_call_sync_session
,
5489 .validate_stateid
= nfs41_validate_delegation_stateid
,
5490 .reboot_recovery_ops
= &nfs41_reboot_recovery_ops
,
5491 .nograce_recovery_ops
= &nfs41_nograce_recovery_ops
,
5492 .state_renewal_ops
= &nfs41_state_renewal_ops
,
5496 const struct nfs4_minor_version_ops
*nfs_v4_minor_ops
[] = {
5497 [0] = &nfs_v4_0_minor_ops
,
5498 #if defined(CONFIG_NFS_V4_1)
5499 [1] = &nfs_v4_1_minor_ops
,
5503 static const struct inode_operations nfs4_file_inode_operations
= {
5504 .permission
= nfs_permission
,
5505 .getattr
= nfs_getattr
,
5506 .setattr
= nfs_setattr
,
5507 .getxattr
= nfs4_getxattr
,
5508 .setxattr
= nfs4_setxattr
,
5509 .listxattr
= nfs4_listxattr
,
5512 const struct nfs_rpc_ops nfs_v4_clientops
= {
5513 .version
= 4, /* protocol version */
5514 .dentry_ops
= &nfs4_dentry_operations
,
5515 .dir_inode_ops
= &nfs4_dir_inode_operations
,
5516 .file_inode_ops
= &nfs4_file_inode_operations
,
5517 .getroot
= nfs4_proc_get_root
,
5518 .getattr
= nfs4_proc_getattr
,
5519 .setattr
= nfs4_proc_setattr
,
5520 .lookupfh
= nfs4_proc_lookupfh
,
5521 .lookup
= nfs4_proc_lookup
,
5522 .access
= nfs4_proc_access
,
5523 .readlink
= nfs4_proc_readlink
,
5524 .create
= nfs4_proc_create
,
5525 .remove
= nfs4_proc_remove
,
5526 .unlink_setup
= nfs4_proc_unlink_setup
,
5527 .unlink_done
= nfs4_proc_unlink_done
,
5528 .rename
= nfs4_proc_rename
,
5529 .rename_setup
= nfs4_proc_rename_setup
,
5530 .rename_done
= nfs4_proc_rename_done
,
5531 .link
= nfs4_proc_link
,
5532 .symlink
= nfs4_proc_symlink
,
5533 .mkdir
= nfs4_proc_mkdir
,
5534 .rmdir
= nfs4_proc_remove
,
5535 .readdir
= nfs4_proc_readdir
,
5536 .mknod
= nfs4_proc_mknod
,
5537 .statfs
= nfs4_proc_statfs
,
5538 .fsinfo
= nfs4_proc_fsinfo
,
5539 .pathconf
= nfs4_proc_pathconf
,
5540 .set_capabilities
= nfs4_server_capabilities
,
5541 .decode_dirent
= nfs4_decode_dirent
,
5542 .read_setup
= nfs4_proc_read_setup
,
5543 .read_done
= nfs4_read_done
,
5544 .write_setup
= nfs4_proc_write_setup
,
5545 .write_done
= nfs4_write_done
,
5546 .commit_setup
= nfs4_proc_commit_setup
,
5547 .commit_done
= nfs4_commit_done
,
5548 .lock
= nfs4_proc_lock
,
5549 .clear_acl_cache
= nfs4_zap_acl_attr
,
5550 .close_context
= nfs4_close_context
,
5551 .open_context
= nfs4_atomic_open
,