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/sunrpc/clnt.h>
43 #include <linux/nfs.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/nfs_page.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/module.h>
50 #include <linux/sunrpc/bc_xprt.h>
53 #include "delegation.h"
58 #define NFSDBG_FACILITY NFSDBG_PROC
60 #define NFS4_POLL_RETRY_MIN (HZ/10)
61 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 #define NFS4_MAX_LOOP_ON_RECOVER (10)
66 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
67 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
68 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
69 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
70 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
71 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
73 /* Prevent leaks of NFSv4 errors into userland */
74 static int nfs4_map_errors(int err
)
79 case -NFS4ERR_RESOURCE
:
82 dprintk("%s could not handle NFSv4 error %d\n",
90 * This is our standard bitmap for GETATTR requests.
92 const u32 nfs4_fattr_bitmap
[2] = {
97 | FATTR4_WORD0_FILEID
,
99 | FATTR4_WORD1_NUMLINKS
101 | FATTR4_WORD1_OWNER_GROUP
102 | FATTR4_WORD1_RAWDEV
103 | FATTR4_WORD1_SPACE_USED
104 | FATTR4_WORD1_TIME_ACCESS
105 | FATTR4_WORD1_TIME_METADATA
106 | FATTR4_WORD1_TIME_MODIFY
109 const u32 nfs4_statfs_bitmap
[2] = {
110 FATTR4_WORD0_FILES_AVAIL
111 | FATTR4_WORD0_FILES_FREE
112 | FATTR4_WORD0_FILES_TOTAL
,
113 FATTR4_WORD1_SPACE_AVAIL
114 | FATTR4_WORD1_SPACE_FREE
115 | FATTR4_WORD1_SPACE_TOTAL
118 const u32 nfs4_pathconf_bitmap
[2] = {
120 | FATTR4_WORD0_MAXNAME
,
124 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
125 | FATTR4_WORD0_MAXREAD
126 | FATTR4_WORD0_MAXWRITE
127 | FATTR4_WORD0_LEASE_TIME
,
131 const u32 nfs4_fs_locations_bitmap
[2] = {
133 | FATTR4_WORD0_CHANGE
136 | FATTR4_WORD0_FILEID
137 | FATTR4_WORD0_FS_LOCATIONS
,
139 | FATTR4_WORD1_NUMLINKS
141 | FATTR4_WORD1_OWNER_GROUP
142 | FATTR4_WORD1_RAWDEV
143 | FATTR4_WORD1_SPACE_USED
144 | FATTR4_WORD1_TIME_ACCESS
145 | FATTR4_WORD1_TIME_METADATA
146 | FATTR4_WORD1_TIME_MODIFY
147 | FATTR4_WORD1_MOUNTED_ON_FILEID
150 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
151 struct nfs4_readdir_arg
*readdir
)
155 BUG_ON(readdir
->count
< 80);
157 readdir
->cookie
= cookie
;
158 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
163 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
168 * NFSv4 servers do not return entries for '.' and '..'
169 * Therefore, we fake these entries here. We let '.'
170 * have cookie 0 and '..' have cookie 1. Note that
171 * when talking to the server, we always send cookie 0
174 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
177 *p
++ = xdr_one
; /* next */
178 *p
++ = xdr_zero
; /* cookie, first word */
179 *p
++ = xdr_one
; /* cookie, second word */
180 *p
++ = xdr_one
; /* entry len */
181 memcpy(p
, ".\0\0\0", 4); /* entry */
183 *p
++ = xdr_one
; /* bitmap length */
184 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
185 *p
++ = htonl(8); /* attribute buffer length */
186 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
189 *p
++ = xdr_one
; /* next */
190 *p
++ = xdr_zero
; /* cookie, first word */
191 *p
++ = xdr_two
; /* cookie, second word */
192 *p
++ = xdr_two
; /* entry len */
193 memcpy(p
, "..\0\0", 4); /* entry */
195 *p
++ = xdr_one
; /* bitmap length */
196 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
197 *p
++ = htonl(8); /* attribute buffer length */
198 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
200 readdir
->pgbase
= (char *)p
- (char *)start
;
201 readdir
->count
-= readdir
->pgbase
;
202 kunmap_atomic(start
, KM_USER0
);
205 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
211 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
212 nfs_wait_bit_killable
, TASK_KILLABLE
);
216 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
223 *timeout
= NFS4_POLL_RETRY_MIN
;
224 if (*timeout
> NFS4_POLL_RETRY_MAX
)
225 *timeout
= NFS4_POLL_RETRY_MAX
;
226 schedule_timeout_killable(*timeout
);
227 if (fatal_signal_pending(current
))
233 /* This is the error handling routine for processes that are allowed
236 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
238 struct nfs_client
*clp
= server
->nfs_client
;
239 struct nfs4_state
*state
= exception
->state
;
242 exception
->retry
= 0;
246 case -NFS4ERR_ADMIN_REVOKED
:
247 case -NFS4ERR_BAD_STATEID
:
248 case -NFS4ERR_OPENMODE
:
251 nfs4_state_mark_reclaim_nograce(clp
, state
);
252 case -NFS4ERR_STALE_CLIENTID
:
253 case -NFS4ERR_STALE_STATEID
:
254 case -NFS4ERR_EXPIRED
:
255 nfs4_schedule_state_recovery(clp
);
256 ret
= nfs4_wait_clnt_recover(clp
);
258 exception
->retry
= 1;
260 #if defined(CONFIG_NFS_V4_1)
261 case -NFS4ERR_BADSESSION
:
262 case -NFS4ERR_BADSLOT
:
263 case -NFS4ERR_BAD_HIGH_SLOT
:
264 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
265 case -NFS4ERR_DEADSESSION
:
266 case -NFS4ERR_SEQ_FALSE_RETRY
:
267 case -NFS4ERR_SEQ_MISORDERED
:
268 dprintk("%s ERROR: %d Reset session\n", __func__
,
270 nfs4_schedule_state_recovery(clp
);
271 exception
->retry
= 1;
273 #endif /* defined(CONFIG_NFS_V4_1) */
274 case -NFS4ERR_FILE_OPEN
:
275 if (exception
->timeout
> HZ
) {
276 /* We have retried a decent amount, time to
284 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
287 case -NFS4ERR_OLD_STATEID
:
288 exception
->retry
= 1;
290 /* We failed to handle the error */
291 return nfs4_map_errors(ret
);
295 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
297 struct nfs_client
*clp
= server
->nfs_client
;
298 spin_lock(&clp
->cl_lock
);
299 if (time_before(clp
->cl_last_renewal
,timestamp
))
300 clp
->cl_last_renewal
= timestamp
;
301 spin_unlock(&clp
->cl_lock
);
304 #if defined(CONFIG_NFS_V4_1)
307 * nfs4_free_slot - free a slot and efficiently update slot table.
309 * freeing a slot is trivially done by clearing its respective bit
311 * If the freed slotid equals highest_used_slotid we want to update it
312 * so that the server would be able to size down the slot table if needed,
313 * otherwise we know that the highest_used_slotid is still in use.
314 * When updating highest_used_slotid there may be "holes" in the bitmap
315 * so we need to scan down from highest_used_slotid to 0 looking for the now
316 * highest slotid in use.
317 * If none found, highest_used_slotid is set to -1.
319 * Must be called while holding tbl->slot_tbl_lock
322 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
324 int slotid
= free_slotid
;
326 /* clear used bit in bitmap */
327 __clear_bit(slotid
, tbl
->used_slots
);
329 /* update highest_used_slotid when it is freed */
330 if (slotid
== tbl
->highest_used_slotid
) {
331 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
332 if (slotid
< tbl
->max_slots
)
333 tbl
->highest_used_slotid
= slotid
;
335 tbl
->highest_used_slotid
= -1;
337 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
338 free_slotid
, tbl
->highest_used_slotid
);
342 * Signal state manager thread if session is drained
344 static void nfs41_check_drain_session_complete(struct nfs4_session
*ses
)
346 struct rpc_task
*task
;
348 if (!test_bit(NFS4CLNT_SESSION_DRAINING
, &ses
->clp
->cl_state
)) {
349 task
= rpc_wake_up_next(&ses
->fc_slot_table
.slot_tbl_waitq
);
351 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
355 if (ses
->fc_slot_table
.highest_used_slotid
!= -1)
358 dprintk("%s COMPLETE: Session Drained\n", __func__
);
359 complete(&ses
->complete
);
362 static void nfs41_sequence_free_slot(const struct nfs_client
*clp
,
363 struct nfs4_sequence_res
*res
)
365 struct nfs4_slot_table
*tbl
;
367 tbl
= &clp
->cl_session
->fc_slot_table
;
368 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
369 /* just wake up the next guy waiting since
370 * we may have not consumed a slot after all */
371 dprintk("%s: No slot\n", __func__
);
375 spin_lock(&tbl
->slot_tbl_lock
);
376 nfs4_free_slot(tbl
, res
->sr_slotid
);
377 nfs41_check_drain_session_complete(clp
->cl_session
);
378 spin_unlock(&tbl
->slot_tbl_lock
);
379 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
382 static void nfs41_sequence_done(struct nfs_client
*clp
,
383 struct nfs4_sequence_res
*res
,
386 unsigned long timestamp
;
387 struct nfs4_slot_table
*tbl
;
388 struct nfs4_slot
*slot
;
391 * sr_status remains 1 if an RPC level error occurred. The server
392 * may or may not have processed the sequence operation..
393 * Proceed as if the server received and processed the sequence
396 if (res
->sr_status
== 1)
397 res
->sr_status
= NFS_OK
;
399 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
400 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
403 /* Check the SEQUENCE operation status */
404 if (res
->sr_status
== 0) {
405 tbl
= &clp
->cl_session
->fc_slot_table
;
406 slot
= tbl
->slots
+ res
->sr_slotid
;
407 /* Update the slot's sequence and clientid lease timer */
409 timestamp
= res
->sr_renewal_time
;
410 spin_lock(&clp
->cl_lock
);
411 if (time_before(clp
->cl_last_renewal
, timestamp
))
412 clp
->cl_last_renewal
= timestamp
;
413 spin_unlock(&clp
->cl_lock
);
414 /* Check sequence flags */
415 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
418 /* The session may be reset by one of the error handlers. */
419 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
420 nfs41_sequence_free_slot(clp
, res
);
424 * nfs4_find_slot - efficiently look for a free slot
426 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
427 * If found, we mark the slot as used, update the highest_used_slotid,
428 * and respectively set up the sequence operation args.
429 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
431 * Note: must be called with under the slot_tbl_lock.
434 nfs4_find_slot(struct nfs4_slot_table
*tbl
)
437 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
438 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
440 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
441 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
443 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
444 if (slotid
>= tbl
->max_slots
)
446 __set_bit(slotid
, tbl
->used_slots
);
447 if (slotid
> tbl
->highest_used_slotid
)
448 tbl
->highest_used_slotid
= slotid
;
451 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
452 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
456 static int nfs41_setup_sequence(struct nfs4_session
*session
,
457 struct nfs4_sequence_args
*args
,
458 struct nfs4_sequence_res
*res
,
460 struct rpc_task
*task
)
462 struct nfs4_slot
*slot
;
463 struct nfs4_slot_table
*tbl
;
466 dprintk("--> %s\n", __func__
);
467 /* slot already allocated? */
468 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
471 memset(res
, 0, sizeof(*res
));
472 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
473 tbl
= &session
->fc_slot_table
;
475 spin_lock(&tbl
->slot_tbl_lock
);
476 if (test_bit(NFS4CLNT_SESSION_DRAINING
, &session
->clp
->cl_state
) &&
477 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
479 * The state manager will wait until the slot table is empty.
480 * Schedule the reset thread
482 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
483 spin_unlock(&tbl
->slot_tbl_lock
);
484 dprintk("%s Schedule Session Reset\n", __func__
);
488 if (!rpc_queue_empty(&tbl
->slot_tbl_waitq
) &&
489 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
490 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
491 spin_unlock(&tbl
->slot_tbl_lock
);
492 dprintk("%s enforce FIFO order\n", __func__
);
496 slotid
= nfs4_find_slot(tbl
);
497 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
498 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
499 spin_unlock(&tbl
->slot_tbl_lock
);
500 dprintk("<-- %s: no free slots\n", __func__
);
503 spin_unlock(&tbl
->slot_tbl_lock
);
505 rpc_task_set_priority(task
, RPC_PRIORITY_NORMAL
);
506 slot
= tbl
->slots
+ slotid
;
507 args
->sa_session
= session
;
508 args
->sa_slotid
= slotid
;
509 args
->sa_cache_this
= cache_reply
;
511 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
513 res
->sr_session
= session
;
514 res
->sr_slotid
= slotid
;
515 res
->sr_renewal_time
= jiffies
;
517 * sr_status is only set in decode_sequence, and so will remain
518 * set to 1 if an rpc level failure occurs.
524 int nfs4_setup_sequence(struct nfs_client
*clp
,
525 struct nfs4_sequence_args
*args
,
526 struct nfs4_sequence_res
*res
,
528 struct rpc_task
*task
)
532 dprintk("--> %s clp %p session %p sr_slotid %d\n",
533 __func__
, clp
, clp
->cl_session
, res
->sr_slotid
);
535 if (!nfs4_has_session(clp
))
537 ret
= nfs41_setup_sequence(clp
->cl_session
, args
, res
, cache_reply
,
539 if (ret
&& ret
!= -EAGAIN
) {
540 /* terminate rpc task */
541 task
->tk_status
= ret
;
542 task
->tk_action
= NULL
;
545 dprintk("<-- %s status=%d\n", __func__
, ret
);
549 struct nfs41_call_sync_data
{
550 struct nfs_client
*clp
;
551 struct nfs4_sequence_args
*seq_args
;
552 struct nfs4_sequence_res
*seq_res
;
556 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
558 struct nfs41_call_sync_data
*data
= calldata
;
560 dprintk("--> %s data->clp->cl_session %p\n", __func__
,
561 data
->clp
->cl_session
);
562 if (nfs4_setup_sequence(data
->clp
, data
->seq_args
,
563 data
->seq_res
, data
->cache_reply
, task
))
565 rpc_call_start(task
);
568 static void nfs41_call_priv_sync_prepare(struct rpc_task
*task
, void *calldata
)
570 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
571 nfs41_call_sync_prepare(task
, calldata
);
574 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
576 struct nfs41_call_sync_data
*data
= calldata
;
578 nfs41_sequence_done(data
->clp
, data
->seq_res
, task
->tk_status
);
581 struct rpc_call_ops nfs41_call_sync_ops
= {
582 .rpc_call_prepare
= nfs41_call_sync_prepare
,
583 .rpc_call_done
= nfs41_call_sync_done
,
586 struct rpc_call_ops nfs41_call_priv_sync_ops
= {
587 .rpc_call_prepare
= nfs41_call_priv_sync_prepare
,
588 .rpc_call_done
= nfs41_call_sync_done
,
591 static int nfs4_call_sync_sequence(struct nfs_client
*clp
,
592 struct rpc_clnt
*clnt
,
593 struct rpc_message
*msg
,
594 struct nfs4_sequence_args
*args
,
595 struct nfs4_sequence_res
*res
,
600 struct rpc_task
*task
;
601 struct nfs41_call_sync_data data
= {
605 .cache_reply
= cache_reply
,
607 struct rpc_task_setup task_setup
= {
610 .callback_ops
= &nfs41_call_sync_ops
,
611 .callback_data
= &data
614 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
616 task_setup
.callback_ops
= &nfs41_call_priv_sync_ops
;
617 task
= rpc_run_task(&task_setup
);
621 ret
= task
->tk_status
;
627 int _nfs4_call_sync_session(struct nfs_server
*server
,
628 struct rpc_message
*msg
,
629 struct nfs4_sequence_args
*args
,
630 struct nfs4_sequence_res
*res
,
633 return nfs4_call_sync_sequence(server
->nfs_client
, server
->client
,
634 msg
, args
, res
, cache_reply
, 0);
637 #endif /* CONFIG_NFS_V4_1 */
639 int _nfs4_call_sync(struct nfs_server
*server
,
640 struct rpc_message
*msg
,
641 struct nfs4_sequence_args
*args
,
642 struct nfs4_sequence_res
*res
,
645 args
->sa_session
= res
->sr_session
= NULL
;
646 return rpc_call_sync(server
->client
, msg
, 0);
649 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
650 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
651 &(res)->seq_res, (cache_reply))
653 static void nfs4_sequence_done(const struct nfs_server
*server
,
654 struct nfs4_sequence_res
*res
, int rpc_status
)
656 #ifdef CONFIG_NFS_V4_1
657 if (nfs4_has_session(server
->nfs_client
))
658 nfs41_sequence_done(server
->nfs_client
, res
, rpc_status
);
659 #endif /* CONFIG_NFS_V4_1 */
662 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
664 struct nfs_inode
*nfsi
= NFS_I(dir
);
666 spin_lock(&dir
->i_lock
);
667 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
668 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
669 nfs_force_lookup_revalidate(dir
);
670 nfsi
->change_attr
= cinfo
->after
;
671 spin_unlock(&dir
->i_lock
);
674 struct nfs4_opendata
{
676 struct nfs_openargs o_arg
;
677 struct nfs_openres o_res
;
678 struct nfs_open_confirmargs c_arg
;
679 struct nfs_open_confirmres c_res
;
680 struct nfs_fattr f_attr
;
681 struct nfs_fattr dir_attr
;
684 struct nfs4_state_owner
*owner
;
685 struct nfs4_state
*state
;
687 unsigned long timestamp
;
688 unsigned int rpc_done
: 1;
694 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
696 p
->o_res
.f_attr
= &p
->f_attr
;
697 p
->o_res
.dir_attr
= &p
->dir_attr
;
698 p
->o_res
.seqid
= p
->o_arg
.seqid
;
699 p
->c_res
.seqid
= p
->c_arg
.seqid
;
700 p
->o_res
.server
= p
->o_arg
.server
;
701 nfs_fattr_init(&p
->f_attr
);
702 nfs_fattr_init(&p
->dir_attr
);
703 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
706 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
707 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
708 const struct iattr
*attrs
)
710 struct dentry
*parent
= dget_parent(path
->dentry
);
711 struct inode
*dir
= parent
->d_inode
;
712 struct nfs_server
*server
= NFS_SERVER(dir
);
713 struct nfs4_opendata
*p
;
715 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
718 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
719 if (p
->o_arg
.seqid
== NULL
)
721 p
->path
.mnt
= mntget(path
->mnt
);
722 p
->path
.dentry
= dget(path
->dentry
);
725 atomic_inc(&sp
->so_count
);
726 p
->o_arg
.fh
= NFS_FH(dir
);
727 p
->o_arg
.open_flags
= flags
;
728 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
729 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
730 p
->o_arg
.id
= sp
->so_owner_id
.id
;
731 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
732 p
->o_arg
.server
= server
;
733 p
->o_arg
.bitmask
= server
->attr_bitmask
;
734 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
735 if (flags
& O_EXCL
) {
736 if (nfs4_has_persistent_session(server
->nfs_client
)) {
738 p
->o_arg
.u
.attrs
= &p
->attrs
;
739 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
740 } else { /* EXCLUSIVE4_1 */
741 u32
*s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
745 } else if (flags
& O_CREAT
) {
746 p
->o_arg
.u
.attrs
= &p
->attrs
;
747 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
749 p
->c_arg
.fh
= &p
->o_res
.fh
;
750 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
751 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
752 nfs4_init_opendata_res(p
);
762 static void nfs4_opendata_free(struct kref
*kref
)
764 struct nfs4_opendata
*p
= container_of(kref
,
765 struct nfs4_opendata
, kref
);
767 nfs_free_seqid(p
->o_arg
.seqid
);
768 if (p
->state
!= NULL
)
769 nfs4_put_open_state(p
->state
);
770 nfs4_put_state_owner(p
->owner
);
776 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
779 kref_put(&p
->kref
, nfs4_opendata_free
);
782 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
786 ret
= rpc_wait_for_completion_task(task
);
790 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
794 if (open_mode
& O_EXCL
)
796 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
798 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
799 && state
->n_rdonly
!= 0;
802 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
803 && state
->n_wronly
!= 0;
805 case FMODE_READ
|FMODE_WRITE
:
806 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
807 && state
->n_rdwr
!= 0;
813 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
815 if ((delegation
->type
& fmode
) != fmode
)
817 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
819 nfs_mark_delegation_referenced(delegation
);
823 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
832 case FMODE_READ
|FMODE_WRITE
:
835 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
838 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
840 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
841 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
842 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
845 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
848 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
850 case FMODE_READ
|FMODE_WRITE
:
851 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
855 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
857 write_seqlock(&state
->seqlock
);
858 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
859 write_sequnlock(&state
->seqlock
);
862 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
865 * Protect the call to nfs4_state_set_mode_locked and
866 * serialise the stateid update
868 write_seqlock(&state
->seqlock
);
869 if (deleg_stateid
!= NULL
) {
870 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
871 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
873 if (open_stateid
!= NULL
)
874 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
875 write_sequnlock(&state
->seqlock
);
876 spin_lock(&state
->owner
->so_lock
);
877 update_open_stateflags(state
, fmode
);
878 spin_unlock(&state
->owner
->so_lock
);
881 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
883 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
884 struct nfs_delegation
*deleg_cur
;
887 fmode
&= (FMODE_READ
|FMODE_WRITE
);
890 deleg_cur
= rcu_dereference(nfsi
->delegation
);
891 if (deleg_cur
== NULL
)
894 spin_lock(&deleg_cur
->lock
);
895 if (nfsi
->delegation
!= deleg_cur
||
896 (deleg_cur
->type
& fmode
) != fmode
)
897 goto no_delegation_unlock
;
899 if (delegation
== NULL
)
900 delegation
= &deleg_cur
->stateid
;
901 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
902 goto no_delegation_unlock
;
904 nfs_mark_delegation_referenced(deleg_cur
);
905 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
907 no_delegation_unlock
:
908 spin_unlock(&deleg_cur
->lock
);
912 if (!ret
&& open_stateid
!= NULL
) {
913 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
921 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
923 struct nfs_delegation
*delegation
;
926 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
927 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
932 nfs_inode_return_delegation(inode
);
935 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
937 struct nfs4_state
*state
= opendata
->state
;
938 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
939 struct nfs_delegation
*delegation
;
940 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
941 fmode_t fmode
= opendata
->o_arg
.fmode
;
942 nfs4_stateid stateid
;
946 if (can_open_cached(state
, fmode
, open_mode
)) {
947 spin_lock(&state
->owner
->so_lock
);
948 if (can_open_cached(state
, fmode
, open_mode
)) {
949 update_open_stateflags(state
, fmode
);
950 spin_unlock(&state
->owner
->so_lock
);
951 goto out_return_state
;
953 spin_unlock(&state
->owner
->so_lock
);
956 delegation
= rcu_dereference(nfsi
->delegation
);
957 if (delegation
== NULL
||
958 !can_open_delegated(delegation
, fmode
)) {
962 /* Save the delegation */
963 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
965 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
970 /* Try to update the stateid using the delegation */
971 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
972 goto out_return_state
;
977 atomic_inc(&state
->count
);
981 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
984 struct nfs4_state
*state
= NULL
;
985 struct nfs_delegation
*delegation
;
988 if (!data
->rpc_done
) {
989 state
= nfs4_try_open_cached(data
);
994 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
996 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
997 ret
= PTR_ERR(inode
);
1001 state
= nfs4_get_open_state(inode
, data
->owner
);
1004 if (data
->o_res
.delegation_type
!= 0) {
1005 int delegation_flags
= 0;
1008 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1010 delegation_flags
= delegation
->flags
;
1012 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1013 nfs_inode_set_delegation(state
->inode
,
1014 data
->owner
->so_cred
,
1017 nfs_inode_reclaim_delegation(state
->inode
,
1018 data
->owner
->so_cred
,
1022 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1030 return ERR_PTR(ret
);
1033 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1035 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1036 struct nfs_open_context
*ctx
;
1038 spin_lock(&state
->inode
->i_lock
);
1039 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1040 if (ctx
->state
!= state
)
1042 get_nfs_open_context(ctx
);
1043 spin_unlock(&state
->inode
->i_lock
);
1046 spin_unlock(&state
->inode
->i_lock
);
1047 return ERR_PTR(-ENOENT
);
1050 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1052 struct nfs4_opendata
*opendata
;
1054 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
);
1055 if (opendata
== NULL
)
1056 return ERR_PTR(-ENOMEM
);
1057 opendata
->state
= state
;
1058 atomic_inc(&state
->count
);
1062 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1064 struct nfs4_state
*newstate
;
1067 opendata
->o_arg
.open_flags
= 0;
1068 opendata
->o_arg
.fmode
= fmode
;
1069 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1070 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1071 nfs4_init_opendata_res(opendata
);
1072 ret
= _nfs4_recover_proc_open(opendata
);
1075 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1076 if (IS_ERR(newstate
))
1077 return PTR_ERR(newstate
);
1078 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1083 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1085 struct nfs4_state
*newstate
;
1088 /* memory barrier prior to reading state->n_* */
1089 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1091 if (state
->n_rdwr
!= 0) {
1092 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1095 if (newstate
!= state
)
1098 if (state
->n_wronly
!= 0) {
1099 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1102 if (newstate
!= state
)
1105 if (state
->n_rdonly
!= 0) {
1106 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1109 if (newstate
!= state
)
1113 * We may have performed cached opens for all three recoveries.
1114 * Check if we need to update the current stateid.
1116 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1117 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1118 write_seqlock(&state
->seqlock
);
1119 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1120 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1121 write_sequnlock(&state
->seqlock
);
1128 * reclaim state on the server after a reboot.
1130 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1132 struct nfs_delegation
*delegation
;
1133 struct nfs4_opendata
*opendata
;
1134 fmode_t delegation_type
= 0;
1137 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1138 if (IS_ERR(opendata
))
1139 return PTR_ERR(opendata
);
1140 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1141 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1143 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1144 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1145 delegation_type
= delegation
->type
;
1147 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1148 status
= nfs4_open_recover(opendata
, state
);
1149 nfs4_opendata_put(opendata
);
1153 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1155 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1156 struct nfs4_exception exception
= { };
1159 err
= _nfs4_do_open_reclaim(ctx
, state
);
1160 if (err
!= -NFS4ERR_DELAY
)
1162 nfs4_handle_exception(server
, err
, &exception
);
1163 } while (exception
.retry
);
1167 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1169 struct nfs_open_context
*ctx
;
1172 ctx
= nfs4_state_find_open_context(state
);
1174 return PTR_ERR(ctx
);
1175 ret
= nfs4_do_open_reclaim(ctx
, state
);
1176 put_nfs_open_context(ctx
);
1180 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1182 struct nfs4_opendata
*opendata
;
1185 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1186 if (IS_ERR(opendata
))
1187 return PTR_ERR(opendata
);
1188 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1189 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1190 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1191 ret
= nfs4_open_recover(opendata
, state
);
1192 nfs4_opendata_put(opendata
);
1196 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1198 struct nfs4_exception exception
= { };
1199 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1202 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1208 case -NFS4ERR_BADSESSION
:
1209 case -NFS4ERR_BADSLOT
:
1210 case -NFS4ERR_BAD_HIGH_SLOT
:
1211 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1212 case -NFS4ERR_DEADSESSION
:
1213 nfs4_schedule_state_recovery(
1214 server
->nfs_client
);
1216 case -NFS4ERR_STALE_CLIENTID
:
1217 case -NFS4ERR_STALE_STATEID
:
1218 case -NFS4ERR_EXPIRED
:
1219 /* Don't recall a delegation if it was lost */
1220 nfs4_schedule_state_recovery(server
->nfs_client
);
1224 * The show must go on: exit, but mark the
1225 * stateid as needing recovery.
1227 case -NFS4ERR_ADMIN_REVOKED
:
1228 case -NFS4ERR_BAD_STATEID
:
1229 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
1234 err
= nfs4_handle_exception(server
, err
, &exception
);
1235 } while (exception
.retry
);
1240 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1242 struct nfs4_opendata
*data
= calldata
;
1244 data
->rpc_status
= task
->tk_status
;
1245 if (RPC_ASSASSINATED(task
))
1247 if (data
->rpc_status
== 0) {
1248 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1249 sizeof(data
->o_res
.stateid
.data
));
1250 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1251 renew_lease(data
->o_res
.server
, data
->timestamp
);
1256 static void nfs4_open_confirm_release(void *calldata
)
1258 struct nfs4_opendata
*data
= calldata
;
1259 struct nfs4_state
*state
= NULL
;
1261 /* If this request hasn't been cancelled, do nothing */
1262 if (data
->cancelled
== 0)
1264 /* In case of error, no cleanup! */
1265 if (!data
->rpc_done
)
1267 state
= nfs4_opendata_to_nfs4_state(data
);
1269 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1271 nfs4_opendata_put(data
);
1274 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1275 .rpc_call_done
= nfs4_open_confirm_done
,
1276 .rpc_release
= nfs4_open_confirm_release
,
1280 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1282 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1284 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1285 struct rpc_task
*task
;
1286 struct rpc_message msg
= {
1287 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1288 .rpc_argp
= &data
->c_arg
,
1289 .rpc_resp
= &data
->c_res
,
1290 .rpc_cred
= data
->owner
->so_cred
,
1292 struct rpc_task_setup task_setup_data
= {
1293 .rpc_client
= server
->client
,
1294 .rpc_message
= &msg
,
1295 .callback_ops
= &nfs4_open_confirm_ops
,
1296 .callback_data
= data
,
1297 .workqueue
= nfsiod_workqueue
,
1298 .flags
= RPC_TASK_ASYNC
,
1302 kref_get(&data
->kref
);
1304 data
->rpc_status
= 0;
1305 data
->timestamp
= jiffies
;
1306 task
= rpc_run_task(&task_setup_data
);
1308 return PTR_ERR(task
);
1309 status
= nfs4_wait_for_completion_rpc_task(task
);
1311 data
->cancelled
= 1;
1314 status
= data
->rpc_status
;
1319 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1321 struct nfs4_opendata
*data
= calldata
;
1322 struct nfs4_state_owner
*sp
= data
->owner
;
1324 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1327 * Check if we still need to send an OPEN call, or if we can use
1328 * a delegation instead.
1330 if (data
->state
!= NULL
) {
1331 struct nfs_delegation
*delegation
;
1333 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1336 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1337 if (delegation
!= NULL
&&
1338 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1344 /* Update sequence id. */
1345 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1346 data
->o_arg
.clientid
= sp
->so_client
->cl_clientid
;
1347 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1348 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1349 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1351 data
->timestamp
= jiffies
;
1352 if (nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
1353 &data
->o_arg
.seq_args
,
1354 &data
->o_res
.seq_res
, 1, task
))
1356 rpc_call_start(task
);
1359 task
->tk_action
= NULL
;
1363 static void nfs4_recover_open_prepare(struct rpc_task
*task
, void *calldata
)
1365 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
1366 nfs4_open_prepare(task
, calldata
);
1369 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1371 struct nfs4_opendata
*data
= calldata
;
1373 data
->rpc_status
= task
->tk_status
;
1375 nfs4_sequence_done(data
->o_arg
.server
, &data
->o_res
.seq_res
,
1378 if (RPC_ASSASSINATED(task
))
1380 if (task
->tk_status
== 0) {
1381 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1385 data
->rpc_status
= -ELOOP
;
1388 data
->rpc_status
= -EISDIR
;
1391 data
->rpc_status
= -ENOTDIR
;
1393 renew_lease(data
->o_res
.server
, data
->timestamp
);
1394 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1395 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1400 static void nfs4_open_release(void *calldata
)
1402 struct nfs4_opendata
*data
= calldata
;
1403 struct nfs4_state
*state
= NULL
;
1405 /* If this request hasn't been cancelled, do nothing */
1406 if (data
->cancelled
== 0)
1408 /* In case of error, no cleanup! */
1409 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1411 /* In case we need an open_confirm, no cleanup! */
1412 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1414 state
= nfs4_opendata_to_nfs4_state(data
);
1416 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1418 nfs4_opendata_put(data
);
1421 static const struct rpc_call_ops nfs4_open_ops
= {
1422 .rpc_call_prepare
= nfs4_open_prepare
,
1423 .rpc_call_done
= nfs4_open_done
,
1424 .rpc_release
= nfs4_open_release
,
1427 static const struct rpc_call_ops nfs4_recover_open_ops
= {
1428 .rpc_call_prepare
= nfs4_recover_open_prepare
,
1429 .rpc_call_done
= nfs4_open_done
,
1430 .rpc_release
= nfs4_open_release
,
1433 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1435 struct inode
*dir
= data
->dir
->d_inode
;
1436 struct nfs_server
*server
= NFS_SERVER(dir
);
1437 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1438 struct nfs_openres
*o_res
= &data
->o_res
;
1439 struct rpc_task
*task
;
1440 struct rpc_message msg
= {
1441 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1444 .rpc_cred
= data
->owner
->so_cred
,
1446 struct rpc_task_setup task_setup_data
= {
1447 .rpc_client
= server
->client
,
1448 .rpc_message
= &msg
,
1449 .callback_ops
= &nfs4_open_ops
,
1450 .callback_data
= data
,
1451 .workqueue
= nfsiod_workqueue
,
1452 .flags
= RPC_TASK_ASYNC
,
1456 kref_get(&data
->kref
);
1458 data
->rpc_status
= 0;
1459 data
->cancelled
= 0;
1461 task_setup_data
.callback_ops
= &nfs4_recover_open_ops
;
1462 task
= rpc_run_task(&task_setup_data
);
1464 return PTR_ERR(task
);
1465 status
= nfs4_wait_for_completion_rpc_task(task
);
1467 data
->cancelled
= 1;
1470 status
= data
->rpc_status
;
1476 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1478 struct inode
*dir
= data
->dir
->d_inode
;
1479 struct nfs_openres
*o_res
= &data
->o_res
;
1482 status
= nfs4_run_open_task(data
, 1);
1483 if (status
!= 0 || !data
->rpc_done
)
1486 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1488 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1489 status
= _nfs4_proc_open_confirm(data
);
1498 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1500 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1502 struct inode
*dir
= data
->dir
->d_inode
;
1503 struct nfs_server
*server
= NFS_SERVER(dir
);
1504 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1505 struct nfs_openres
*o_res
= &data
->o_res
;
1508 status
= nfs4_run_open_task(data
, 0);
1509 if (status
!= 0 || !data
->rpc_done
)
1512 if (o_arg
->open_flags
& O_CREAT
) {
1513 update_changeattr(dir
, &o_res
->cinfo
);
1514 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1516 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1517 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1518 status
= _nfs4_proc_open_confirm(data
);
1522 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1523 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1527 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1529 struct nfs_client
*clp
= server
->nfs_client
;
1533 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1534 ret
= nfs4_wait_clnt_recover(clp
);
1537 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1538 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1540 nfs4_schedule_state_recovery(clp
);
1548 * reclaim state on the server after a network partition.
1549 * Assumes caller holds the appropriate lock
1551 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1553 struct nfs4_opendata
*opendata
;
1556 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1557 if (IS_ERR(opendata
))
1558 return PTR_ERR(opendata
);
1559 ret
= nfs4_open_recover(opendata
, state
);
1561 d_drop(ctx
->path
.dentry
);
1562 nfs4_opendata_put(opendata
);
1566 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1568 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1569 struct nfs4_exception exception
= { };
1573 err
= _nfs4_open_expired(ctx
, state
);
1577 case -NFS4ERR_GRACE
:
1578 case -NFS4ERR_DELAY
:
1579 nfs4_handle_exception(server
, err
, &exception
);
1582 } while (exception
.retry
);
1587 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1589 struct nfs_open_context
*ctx
;
1592 ctx
= nfs4_state_find_open_context(state
);
1594 return PTR_ERR(ctx
);
1595 ret
= nfs4_do_open_expired(ctx
, state
);
1596 put_nfs_open_context(ctx
);
1601 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1602 * fields corresponding to attributes that were used to store the verifier.
1603 * Make sure we clobber those fields in the later setattr call
1605 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1607 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1608 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1609 sattr
->ia_valid
|= ATTR_ATIME
;
1611 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1612 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1613 sattr
->ia_valid
|= ATTR_MTIME
;
1617 * Returns a referenced nfs4_state
1619 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
)
1621 struct nfs4_state_owner
*sp
;
1622 struct nfs4_state
*state
= NULL
;
1623 struct nfs_server
*server
= NFS_SERVER(dir
);
1624 struct nfs4_opendata
*opendata
;
1627 /* Protect against reboot recovery conflicts */
1629 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1630 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1633 status
= nfs4_recover_expired_lease(server
);
1635 goto err_put_state_owner
;
1636 if (path
->dentry
->d_inode
!= NULL
)
1637 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1639 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
);
1640 if (opendata
== NULL
)
1641 goto err_put_state_owner
;
1643 if (path
->dentry
->d_inode
!= NULL
)
1644 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1646 status
= _nfs4_proc_open(opendata
);
1648 goto err_opendata_put
;
1650 if (opendata
->o_arg
.open_flags
& O_EXCL
)
1651 nfs4_exclusive_attrset(opendata
, sattr
);
1653 state
= nfs4_opendata_to_nfs4_state(opendata
);
1654 status
= PTR_ERR(state
);
1656 goto err_opendata_put
;
1657 if ((opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) != 0)
1658 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1659 nfs4_opendata_put(opendata
);
1660 nfs4_put_state_owner(sp
);
1664 nfs4_opendata_put(opendata
);
1665 err_put_state_owner
:
1666 nfs4_put_state_owner(sp
);
1673 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
)
1675 struct nfs4_exception exception
= { };
1676 struct nfs4_state
*res
;
1680 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1683 /* NOTE: BAD_SEQID means the server and client disagree about the
1684 * book-keeping w.r.t. state-changing operations
1685 * (OPEN/CLOSE/LOCK/LOCKU...)
1686 * It is actually a sign of a bug on the client or on the server.
1688 * If we receive a BAD_SEQID error in the particular case of
1689 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1690 * have unhashed the old state_owner for us, and that we can
1691 * therefore safely retry using a new one. We should still warn
1692 * the user though...
1694 if (status
== -NFS4ERR_BAD_SEQID
) {
1695 printk(KERN_WARNING
"NFS: v4 server %s "
1696 " returned a bad sequence-id error!\n",
1697 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1698 exception
.retry
= 1;
1702 * BAD_STATEID on OPEN means that the server cancelled our
1703 * state before it received the OPEN_CONFIRM.
1704 * Recover by retrying the request as per the discussion
1705 * on Page 181 of RFC3530.
1707 if (status
== -NFS4ERR_BAD_STATEID
) {
1708 exception
.retry
= 1;
1711 if (status
== -EAGAIN
) {
1712 /* We must have found a delegation */
1713 exception
.retry
= 1;
1716 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1717 status
, &exception
));
1718 } while (exception
.retry
);
1722 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1723 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1724 struct nfs4_state
*state
)
1726 struct nfs_server
*server
= NFS_SERVER(inode
);
1727 struct nfs_setattrargs arg
= {
1728 .fh
= NFS_FH(inode
),
1731 .bitmask
= server
->attr_bitmask
,
1733 struct nfs_setattrres res
= {
1737 struct rpc_message msg
= {
1738 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1743 unsigned long timestamp
= jiffies
;
1746 nfs_fattr_init(fattr
);
1748 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1749 /* Use that stateid */
1750 } else if (state
!= NULL
) {
1751 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1753 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1755 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1756 if (status
== 0 && state
!= NULL
)
1757 renew_lease(server
, timestamp
);
1761 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1762 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1763 struct nfs4_state
*state
)
1765 struct nfs_server
*server
= NFS_SERVER(inode
);
1766 struct nfs4_exception exception
= { };
1769 err
= nfs4_handle_exception(server
,
1770 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1772 } while (exception
.retry
);
1776 struct nfs4_closedata
{
1778 struct inode
*inode
;
1779 struct nfs4_state
*state
;
1780 struct nfs_closeargs arg
;
1781 struct nfs_closeres res
;
1782 struct nfs_fattr fattr
;
1783 unsigned long timestamp
;
1786 static void nfs4_free_closedata(void *data
)
1788 struct nfs4_closedata
*calldata
= data
;
1789 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1791 nfs4_put_open_state(calldata
->state
);
1792 nfs_free_seqid(calldata
->arg
.seqid
);
1793 nfs4_put_state_owner(sp
);
1794 path_put(&calldata
->path
);
1798 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
1801 spin_lock(&state
->owner
->so_lock
);
1802 if (!(fmode
& FMODE_READ
))
1803 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1804 if (!(fmode
& FMODE_WRITE
))
1805 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1806 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1807 spin_unlock(&state
->owner
->so_lock
);
1810 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1812 struct nfs4_closedata
*calldata
= data
;
1813 struct nfs4_state
*state
= calldata
->state
;
1814 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1816 nfs4_sequence_done(server
, &calldata
->res
.seq_res
, task
->tk_status
);
1817 if (RPC_ASSASSINATED(task
))
1819 /* hmm. we are done with the inode, and in the process of freeing
1820 * the state_owner. we keep this around to process errors
1822 switch (task
->tk_status
) {
1824 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1825 renew_lease(server
, calldata
->timestamp
);
1826 nfs4_close_clear_stateid_flags(state
,
1827 calldata
->arg
.fmode
);
1829 case -NFS4ERR_STALE_STATEID
:
1830 case -NFS4ERR_OLD_STATEID
:
1831 case -NFS4ERR_BAD_STATEID
:
1832 case -NFS4ERR_EXPIRED
:
1833 if (calldata
->arg
.fmode
== 0)
1836 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
1837 rpc_restart_call_prepare(task
);
1839 nfs_release_seqid(calldata
->arg
.seqid
);
1840 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1843 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1845 struct nfs4_closedata
*calldata
= data
;
1846 struct nfs4_state
*state
= calldata
->state
;
1849 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1852 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1853 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
1854 spin_lock(&state
->owner
->so_lock
);
1855 /* Calculate the change in open mode */
1856 if (state
->n_rdwr
== 0) {
1857 if (state
->n_rdonly
== 0) {
1858 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1859 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1860 calldata
->arg
.fmode
&= ~FMODE_READ
;
1862 if (state
->n_wronly
== 0) {
1863 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1864 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1865 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
1868 spin_unlock(&state
->owner
->so_lock
);
1871 /* Note: exit _without_ calling nfs4_close_done */
1872 task
->tk_action
= NULL
;
1876 if (calldata
->arg
.fmode
== 0)
1877 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
1879 nfs_fattr_init(calldata
->res
.fattr
);
1880 calldata
->timestamp
= jiffies
;
1881 if (nfs4_setup_sequence((NFS_SERVER(calldata
->inode
))->nfs_client
,
1882 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1885 rpc_call_start(task
);
1888 static const struct rpc_call_ops nfs4_close_ops
= {
1889 .rpc_call_prepare
= nfs4_close_prepare
,
1890 .rpc_call_done
= nfs4_close_done
,
1891 .rpc_release
= nfs4_free_closedata
,
1895 * It is possible for data to be read/written from a mem-mapped file
1896 * after the sys_close call (which hits the vfs layer as a flush).
1897 * This means that we can't safely call nfsv4 close on a file until
1898 * the inode is cleared. This in turn means that we are not good
1899 * NFSv4 citizens - we do not indicate to the server to update the file's
1900 * share state even when we are done with one of the three share
1901 * stateid's in the inode.
1903 * NOTE: Caller must be holding the sp->so_owner semaphore!
1905 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, int wait
)
1907 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1908 struct nfs4_closedata
*calldata
;
1909 struct nfs4_state_owner
*sp
= state
->owner
;
1910 struct rpc_task
*task
;
1911 struct rpc_message msg
= {
1912 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1913 .rpc_cred
= state
->owner
->so_cred
,
1915 struct rpc_task_setup task_setup_data
= {
1916 .rpc_client
= server
->client
,
1917 .rpc_message
= &msg
,
1918 .callback_ops
= &nfs4_close_ops
,
1919 .workqueue
= nfsiod_workqueue
,
1920 .flags
= RPC_TASK_ASYNC
,
1922 int status
= -ENOMEM
;
1924 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
1925 if (calldata
== NULL
)
1927 calldata
->inode
= state
->inode
;
1928 calldata
->state
= state
;
1929 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1930 calldata
->arg
.stateid
= &state
->open_stateid
;
1931 /* Serialization for the sequence id */
1932 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
1933 if (calldata
->arg
.seqid
== NULL
)
1934 goto out_free_calldata
;
1935 calldata
->arg
.fmode
= 0;
1936 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1937 calldata
->res
.fattr
= &calldata
->fattr
;
1938 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1939 calldata
->res
.server
= server
;
1940 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1941 calldata
->path
.mnt
= mntget(path
->mnt
);
1942 calldata
->path
.dentry
= dget(path
->dentry
);
1944 msg
.rpc_argp
= &calldata
->arg
,
1945 msg
.rpc_resp
= &calldata
->res
,
1946 task_setup_data
.callback_data
= calldata
;
1947 task
= rpc_run_task(&task_setup_data
);
1949 return PTR_ERR(task
);
1952 status
= rpc_wait_for_completion_task(task
);
1958 nfs4_put_open_state(state
);
1959 nfs4_put_state_owner(sp
);
1963 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1968 /* If the open_intent is for execute, we have an extra check to make */
1969 if (fmode
& FMODE_EXEC
) {
1970 ret
= nfs_may_open(state
->inode
,
1971 state
->owner
->so_cred
,
1972 nd
->intent
.open
.flags
);
1976 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1977 if (!IS_ERR(filp
)) {
1978 struct nfs_open_context
*ctx
;
1979 ctx
= nfs_file_open_context(filp
);
1983 ret
= PTR_ERR(filp
);
1985 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
1990 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1992 struct path path
= {
1993 .mnt
= nd
->path
.mnt
,
1996 struct dentry
*parent
;
1998 struct rpc_cred
*cred
;
1999 struct nfs4_state
*state
;
2001 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
2003 if (nd
->flags
& LOOKUP_CREATE
) {
2004 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
2005 attr
.ia_valid
= ATTR_MODE
;
2006 if (!IS_POSIXACL(dir
))
2007 attr
.ia_mode
&= ~current_umask();
2010 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
2013 cred
= rpc_lookup_cred();
2015 return (struct dentry
*)cred
;
2016 parent
= dentry
->d_parent
;
2017 /* Protect against concurrent sillydeletes */
2018 nfs_block_sillyrename(parent
);
2019 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
2021 if (IS_ERR(state
)) {
2022 if (PTR_ERR(state
) == -ENOENT
) {
2023 d_add(dentry
, NULL
);
2024 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2026 nfs_unblock_sillyrename(parent
);
2027 return (struct dentry
*)state
;
2029 res
= d_add_unique(dentry
, igrab(state
->inode
));
2032 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
2033 nfs_unblock_sillyrename(parent
);
2034 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2039 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
2041 struct path path
= {
2042 .mnt
= nd
->path
.mnt
,
2045 struct rpc_cred
*cred
;
2046 struct nfs4_state
*state
;
2047 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
2049 cred
= rpc_lookup_cred();
2051 return PTR_ERR(cred
);
2052 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
2054 if (IS_ERR(state
)) {
2055 switch (PTR_ERR(state
)) {
2061 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
2067 if (state
->inode
== dentry
->d_inode
) {
2068 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2069 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2072 nfs4_close_sync(&path
, state
, fmode
);
2078 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2080 if (ctx
->state
== NULL
)
2083 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2085 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2088 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2090 struct nfs4_server_caps_arg args
= {
2093 struct nfs4_server_caps_res res
= {};
2094 struct rpc_message msg
= {
2095 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2101 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2103 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2104 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2105 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2106 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2107 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2108 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2109 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2110 server
->caps
|= NFS_CAP_ACLS
;
2111 if (res
.has_links
!= 0)
2112 server
->caps
|= NFS_CAP_HARDLINKS
;
2113 if (res
.has_symlinks
!= 0)
2114 server
->caps
|= NFS_CAP_SYMLINKS
;
2115 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2116 server
->caps
|= NFS_CAP_FILEID
;
2117 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2118 server
->caps
|= NFS_CAP_MODE
;
2119 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2120 server
->caps
|= NFS_CAP_NLINK
;
2121 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2122 server
->caps
|= NFS_CAP_OWNER
;
2123 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2124 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2125 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2126 server
->caps
|= NFS_CAP_ATIME
;
2127 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2128 server
->caps
|= NFS_CAP_CTIME
;
2129 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2130 server
->caps
|= NFS_CAP_MTIME
;
2132 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2133 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2134 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2135 server
->acl_bitmask
= res
.acl_bitmask
;
2141 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2143 struct nfs4_exception exception
= { };
2146 err
= nfs4_handle_exception(server
,
2147 _nfs4_server_capabilities(server
, fhandle
),
2149 } while (exception
.retry
);
2153 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2154 struct nfs_fsinfo
*info
)
2156 struct nfs4_lookup_root_arg args
= {
2157 .bitmask
= nfs4_fattr_bitmap
,
2159 struct nfs4_lookup_res res
= {
2161 .fattr
= info
->fattr
,
2164 struct rpc_message msg
= {
2165 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2170 nfs_fattr_init(info
->fattr
);
2171 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2174 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2175 struct nfs_fsinfo
*info
)
2177 struct nfs4_exception exception
= { };
2180 err
= nfs4_handle_exception(server
,
2181 _nfs4_lookup_root(server
, fhandle
, info
),
2183 } while (exception
.retry
);
2188 * get the file handle for the "/" directory on the server
2190 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2191 struct nfs_fsinfo
*info
)
2195 status
= nfs4_lookup_root(server
, fhandle
, info
);
2197 status
= nfs4_server_capabilities(server
, fhandle
);
2199 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2200 return nfs4_map_errors(status
);
2204 * Get locations and (maybe) other attributes of a referral.
2205 * Note that we'll actually follow the referral later when
2206 * we detect fsid mismatch in inode revalidation
2208 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2210 int status
= -ENOMEM
;
2211 struct page
*page
= NULL
;
2212 struct nfs4_fs_locations
*locations
= NULL
;
2214 page
= alloc_page(GFP_KERNEL
);
2217 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2218 if (locations
== NULL
)
2221 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2224 /* Make sure server returned a different fsid for the referral */
2225 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2226 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2231 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2232 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2234 fattr
->mode
= S_IFDIR
;
2235 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2244 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2246 struct nfs4_getattr_arg args
= {
2248 .bitmask
= server
->attr_bitmask
,
2250 struct nfs4_getattr_res res
= {
2254 struct rpc_message msg
= {
2255 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2260 nfs_fattr_init(fattr
);
2261 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2264 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2266 struct nfs4_exception exception
= { };
2269 err
= nfs4_handle_exception(server
,
2270 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2272 } while (exception
.retry
);
2277 * The file is not closed if it is opened due to the a request to change
2278 * the size of the file. The open call will not be needed once the
2279 * VFS layer lookup-intents are implemented.
2281 * Close is called when the inode is destroyed.
2282 * If we haven't opened the file for O_WRONLY, we
2283 * need to in the size_change case to obtain a stateid.
2286 * Because OPEN is always done by name in nfsv4, it is
2287 * possible that we opened a different file by the same
2288 * name. We can recognize this race condition, but we
2289 * can't do anything about it besides returning an error.
2291 * This will be fixed with VFS changes (lookup-intent).
2294 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2295 struct iattr
*sattr
)
2297 struct inode
*inode
= dentry
->d_inode
;
2298 struct rpc_cred
*cred
= NULL
;
2299 struct nfs4_state
*state
= NULL
;
2302 nfs_fattr_init(fattr
);
2304 /* Search for an existing open(O_WRITE) file */
2305 if (sattr
->ia_valid
& ATTR_FILE
) {
2306 struct nfs_open_context
*ctx
;
2308 ctx
= nfs_file_open_context(sattr
->ia_file
);
2315 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2317 nfs_setattr_update_inode(inode
, sattr
);
2321 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2322 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2323 struct nfs_fattr
*fattr
)
2326 struct nfs4_lookup_arg args
= {
2327 .bitmask
= server
->attr_bitmask
,
2331 struct nfs4_lookup_res res
= {
2336 struct rpc_message msg
= {
2337 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2342 nfs_fattr_init(fattr
);
2344 dprintk("NFS call lookupfh %s\n", name
->name
);
2345 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2346 dprintk("NFS reply lookupfh: %d\n", status
);
2350 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2351 struct qstr
*name
, struct nfs_fh
*fhandle
,
2352 struct nfs_fattr
*fattr
)
2354 struct nfs4_exception exception
= { };
2357 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2359 if (err
== -NFS4ERR_MOVED
) {
2363 err
= nfs4_handle_exception(server
, err
, &exception
);
2364 } while (exception
.retry
);
2368 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2369 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2373 dprintk("NFS call lookup %s\n", name
->name
);
2374 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2375 if (status
== -NFS4ERR_MOVED
)
2376 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2377 dprintk("NFS reply lookup: %d\n", status
);
2381 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2383 struct nfs4_exception exception
= { };
2386 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2387 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2389 } while (exception
.retry
);
2393 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2395 struct nfs_server
*server
= NFS_SERVER(inode
);
2396 struct nfs_fattr fattr
;
2397 struct nfs4_accessargs args
= {
2398 .fh
= NFS_FH(inode
),
2399 .bitmask
= server
->attr_bitmask
,
2401 struct nfs4_accessres res
= {
2405 struct rpc_message msg
= {
2406 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2409 .rpc_cred
= entry
->cred
,
2411 int mode
= entry
->mask
;
2415 * Determine which access bits we want to ask for...
2417 if (mode
& MAY_READ
)
2418 args
.access
|= NFS4_ACCESS_READ
;
2419 if (S_ISDIR(inode
->i_mode
)) {
2420 if (mode
& MAY_WRITE
)
2421 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2422 if (mode
& MAY_EXEC
)
2423 args
.access
|= NFS4_ACCESS_LOOKUP
;
2425 if (mode
& MAY_WRITE
)
2426 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2427 if (mode
& MAY_EXEC
)
2428 args
.access
|= NFS4_ACCESS_EXECUTE
;
2430 nfs_fattr_init(&fattr
);
2431 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2434 if (res
.access
& NFS4_ACCESS_READ
)
2435 entry
->mask
|= MAY_READ
;
2436 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2437 entry
->mask
|= MAY_WRITE
;
2438 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2439 entry
->mask
|= MAY_EXEC
;
2440 nfs_refresh_inode(inode
, &fattr
);
2445 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2447 struct nfs4_exception exception
= { };
2450 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2451 _nfs4_proc_access(inode
, entry
),
2453 } while (exception
.retry
);
2458 * TODO: For the time being, we don't try to get any attributes
2459 * along with any of the zero-copy operations READ, READDIR,
2462 * In the case of the first three, we want to put the GETATTR
2463 * after the read-type operation -- this is because it is hard
2464 * to predict the length of a GETATTR response in v4, and thus
2465 * align the READ data correctly. This means that the GETATTR
2466 * may end up partially falling into the page cache, and we should
2467 * shift it into the 'tail' of the xdr_buf before processing.
2468 * To do this efficiently, we need to know the total length
2469 * of data received, which doesn't seem to be available outside
2472 * In the case of WRITE, we also want to put the GETATTR after
2473 * the operation -- in this case because we want to make sure
2474 * we get the post-operation mtime and size. This means that
2475 * we can't use xdr_encode_pages() as written: we need a variant
2476 * of it which would leave room in the 'tail' iovec.
2478 * Both of these changes to the XDR layer would in fact be quite
2479 * minor, but I decided to leave them for a subsequent patch.
2481 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2482 unsigned int pgbase
, unsigned int pglen
)
2484 struct nfs4_readlink args
= {
2485 .fh
= NFS_FH(inode
),
2490 struct nfs4_readlink_res res
;
2491 struct rpc_message msg
= {
2492 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2497 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2500 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2501 unsigned int pgbase
, unsigned int pglen
)
2503 struct nfs4_exception exception
= { };
2506 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2507 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2509 } while (exception
.retry
);
2515 * We will need to arrange for the VFS layer to provide an atomic open.
2516 * Until then, this create/open method is prone to inefficiency and race
2517 * conditions due to the lookup, create, and open VFS calls from sys_open()
2518 * placed on the wire.
2520 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2521 * The file will be opened again in the subsequent VFS open call
2522 * (nfs4_proc_file_open).
2524 * The open for read will just hang around to be used by any process that
2525 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2529 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2530 int flags
, struct nameidata
*nd
)
2532 struct path path
= {
2533 .mnt
= nd
->path
.mnt
,
2536 struct nfs4_state
*state
;
2537 struct rpc_cred
*cred
;
2538 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2541 cred
= rpc_lookup_cred();
2543 status
= PTR_ERR(cred
);
2546 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2548 if (IS_ERR(state
)) {
2549 status
= PTR_ERR(state
);
2552 d_add(dentry
, igrab(state
->inode
));
2553 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2554 if (flags
& O_EXCL
) {
2555 struct nfs_fattr fattr
;
2556 status
= nfs4_do_setattr(state
->inode
, cred
, &fattr
, sattr
, state
);
2558 nfs_setattr_update_inode(state
->inode
, sattr
);
2559 nfs_post_op_update_inode(state
->inode
, &fattr
);
2561 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2562 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2564 nfs4_close_sync(&path
, state
, fmode
);
2571 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2573 struct nfs_server
*server
= NFS_SERVER(dir
);
2574 struct nfs_removeargs args
= {
2576 .name
.len
= name
->len
,
2577 .name
.name
= name
->name
,
2578 .bitmask
= server
->attr_bitmask
,
2580 struct nfs_removeres res
= {
2583 struct rpc_message msg
= {
2584 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2590 nfs_fattr_init(&res
.dir_attr
);
2591 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2593 update_changeattr(dir
, &res
.cinfo
);
2594 nfs_post_op_update_inode(dir
, &res
.dir_attr
);
2599 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2601 struct nfs4_exception exception
= { };
2604 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2605 _nfs4_proc_remove(dir
, name
),
2607 } while (exception
.retry
);
2611 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2613 struct nfs_server
*server
= NFS_SERVER(dir
);
2614 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2615 struct nfs_removeres
*res
= msg
->rpc_resp
;
2617 args
->bitmask
= server
->cache_consistency_bitmask
;
2618 res
->server
= server
;
2619 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2622 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2624 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2626 nfs4_sequence_done(res
->server
, &res
->seq_res
, task
->tk_status
);
2627 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2629 update_changeattr(dir
, &res
->cinfo
);
2630 nfs_post_op_update_inode(dir
, &res
->dir_attr
);
2634 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2635 struct inode
*new_dir
, struct qstr
*new_name
)
2637 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2638 struct nfs4_rename_arg arg
= {
2639 .old_dir
= NFS_FH(old_dir
),
2640 .new_dir
= NFS_FH(new_dir
),
2641 .old_name
= old_name
,
2642 .new_name
= new_name
,
2643 .bitmask
= server
->attr_bitmask
,
2645 struct nfs_fattr old_fattr
, new_fattr
;
2646 struct nfs4_rename_res res
= {
2648 .old_fattr
= &old_fattr
,
2649 .new_fattr
= &new_fattr
,
2651 struct rpc_message msg
= {
2652 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2658 nfs_fattr_init(res
.old_fattr
);
2659 nfs_fattr_init(res
.new_fattr
);
2660 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2663 update_changeattr(old_dir
, &res
.old_cinfo
);
2664 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2665 update_changeattr(new_dir
, &res
.new_cinfo
);
2666 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2671 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2672 struct inode
*new_dir
, struct qstr
*new_name
)
2674 struct nfs4_exception exception
= { };
2677 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2678 _nfs4_proc_rename(old_dir
, old_name
,
2681 } while (exception
.retry
);
2685 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2687 struct nfs_server
*server
= NFS_SERVER(inode
);
2688 struct nfs4_link_arg arg
= {
2689 .fh
= NFS_FH(inode
),
2690 .dir_fh
= NFS_FH(dir
),
2692 .bitmask
= server
->attr_bitmask
,
2694 struct nfs_fattr fattr
, dir_attr
;
2695 struct nfs4_link_res res
= {
2698 .dir_attr
= &dir_attr
,
2700 struct rpc_message msg
= {
2701 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2707 nfs_fattr_init(res
.fattr
);
2708 nfs_fattr_init(res
.dir_attr
);
2709 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2711 update_changeattr(dir
, &res
.cinfo
);
2712 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2713 nfs_post_op_update_inode(inode
, res
.fattr
);
2719 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2721 struct nfs4_exception exception
= { };
2724 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2725 _nfs4_proc_link(inode
, dir
, name
),
2727 } while (exception
.retry
);
2731 struct nfs4_createdata
{
2732 struct rpc_message msg
;
2733 struct nfs4_create_arg arg
;
2734 struct nfs4_create_res res
;
2736 struct nfs_fattr fattr
;
2737 struct nfs_fattr dir_fattr
;
2740 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2741 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2743 struct nfs4_createdata
*data
;
2745 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2747 struct nfs_server
*server
= NFS_SERVER(dir
);
2749 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2750 data
->msg
.rpc_argp
= &data
->arg
;
2751 data
->msg
.rpc_resp
= &data
->res
;
2752 data
->arg
.dir_fh
= NFS_FH(dir
);
2753 data
->arg
.server
= server
;
2754 data
->arg
.name
= name
;
2755 data
->arg
.attrs
= sattr
;
2756 data
->arg
.ftype
= ftype
;
2757 data
->arg
.bitmask
= server
->attr_bitmask
;
2758 data
->res
.server
= server
;
2759 data
->res
.fh
= &data
->fh
;
2760 data
->res
.fattr
= &data
->fattr
;
2761 data
->res
.dir_fattr
= &data
->dir_fattr
;
2762 nfs_fattr_init(data
->res
.fattr
);
2763 nfs_fattr_init(data
->res
.dir_fattr
);
2768 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2770 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2771 &data
->arg
, &data
->res
, 1);
2773 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2774 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2775 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2780 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2785 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2786 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2788 struct nfs4_createdata
*data
;
2789 int status
= -ENAMETOOLONG
;
2791 if (len
> NFS4_MAXPATHLEN
)
2795 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2799 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2800 data
->arg
.u
.symlink
.pages
= &page
;
2801 data
->arg
.u
.symlink
.len
= len
;
2803 status
= nfs4_do_create(dir
, dentry
, data
);
2805 nfs4_free_createdata(data
);
2810 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2811 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2813 struct nfs4_exception exception
= { };
2816 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2817 _nfs4_proc_symlink(dir
, dentry
, page
,
2820 } while (exception
.retry
);
2824 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2825 struct iattr
*sattr
)
2827 struct nfs4_createdata
*data
;
2828 int status
= -ENOMEM
;
2830 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2834 status
= nfs4_do_create(dir
, dentry
, data
);
2836 nfs4_free_createdata(data
);
2841 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2842 struct iattr
*sattr
)
2844 struct nfs4_exception exception
= { };
2847 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2848 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2850 } while (exception
.retry
);
2854 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2855 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2857 struct inode
*dir
= dentry
->d_inode
;
2858 struct nfs4_readdir_arg args
= {
2863 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
2865 struct nfs4_readdir_res res
;
2866 struct rpc_message msg
= {
2867 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2874 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2875 dentry
->d_parent
->d_name
.name
,
2876 dentry
->d_name
.name
,
2877 (unsigned long long)cookie
);
2878 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2879 res
.pgbase
= args
.pgbase
;
2880 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2882 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2884 nfs_invalidate_atime(dir
);
2886 dprintk("%s: returns %d\n", __func__
, status
);
2890 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2891 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2893 struct nfs4_exception exception
= { };
2896 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2897 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2900 } while (exception
.retry
);
2904 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2905 struct iattr
*sattr
, dev_t rdev
)
2907 struct nfs4_createdata
*data
;
2908 int mode
= sattr
->ia_mode
;
2909 int status
= -ENOMEM
;
2911 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2912 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2914 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2919 data
->arg
.ftype
= NF4FIFO
;
2920 else if (S_ISBLK(mode
)) {
2921 data
->arg
.ftype
= NF4BLK
;
2922 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2923 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2925 else if (S_ISCHR(mode
)) {
2926 data
->arg
.ftype
= NF4CHR
;
2927 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2928 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2931 status
= nfs4_do_create(dir
, dentry
, data
);
2933 nfs4_free_createdata(data
);
2938 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2939 struct iattr
*sattr
, dev_t rdev
)
2941 struct nfs4_exception exception
= { };
2944 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2945 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2947 } while (exception
.retry
);
2951 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2952 struct nfs_fsstat
*fsstat
)
2954 struct nfs4_statfs_arg args
= {
2956 .bitmask
= server
->attr_bitmask
,
2958 struct nfs4_statfs_res res
= {
2961 struct rpc_message msg
= {
2962 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2967 nfs_fattr_init(fsstat
->fattr
);
2968 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2971 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2973 struct nfs4_exception exception
= { };
2976 err
= nfs4_handle_exception(server
,
2977 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2979 } while (exception
.retry
);
2983 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2984 struct nfs_fsinfo
*fsinfo
)
2986 struct nfs4_fsinfo_arg args
= {
2988 .bitmask
= server
->attr_bitmask
,
2990 struct nfs4_fsinfo_res res
= {
2993 struct rpc_message msg
= {
2994 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
2999 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3002 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3004 struct nfs4_exception exception
= { };
3008 err
= nfs4_handle_exception(server
,
3009 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
3011 } while (exception
.retry
);
3015 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3017 nfs_fattr_init(fsinfo
->fattr
);
3018 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3021 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3022 struct nfs_pathconf
*pathconf
)
3024 struct nfs4_pathconf_arg args
= {
3026 .bitmask
= server
->attr_bitmask
,
3028 struct nfs4_pathconf_res res
= {
3029 .pathconf
= pathconf
,
3031 struct rpc_message msg
= {
3032 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3037 /* None of the pathconf attributes are mandatory to implement */
3038 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3039 memset(pathconf
, 0, sizeof(*pathconf
));
3043 nfs_fattr_init(pathconf
->fattr
);
3044 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3047 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3048 struct nfs_pathconf
*pathconf
)
3050 struct nfs4_exception exception
= { };
3054 err
= nfs4_handle_exception(server
,
3055 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3057 } while (exception
.retry
);
3061 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3063 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3065 dprintk("--> %s\n", __func__
);
3067 nfs4_sequence_done(server
, &data
->res
.seq_res
, task
->tk_status
);
3069 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3070 nfs_restart_rpc(task
, server
->nfs_client
);
3074 nfs_invalidate_atime(data
->inode
);
3075 if (task
->tk_status
> 0)
3076 renew_lease(server
, data
->timestamp
);
3080 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3082 data
->timestamp
= jiffies
;
3083 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3086 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3088 struct inode
*inode
= data
->inode
;
3090 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3093 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3094 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3097 if (task
->tk_status
>= 0) {
3098 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3099 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3104 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3106 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3108 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3109 data
->res
.server
= server
;
3110 data
->timestamp
= jiffies
;
3112 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3115 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3117 struct inode
*inode
= data
->inode
;
3119 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3121 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3122 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3125 nfs_refresh_inode(inode
, data
->res
.fattr
);
3129 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3131 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3133 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3134 data
->res
.server
= server
;
3135 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3139 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3140 * standalone procedure for queueing an asynchronous RENEW.
3142 static void nfs4_renew_done(struct rpc_task
*task
, void *data
)
3144 struct nfs_client
*clp
= (struct nfs_client
*)task
->tk_msg
.rpc_argp
;
3145 unsigned long timestamp
= (unsigned long)data
;
3147 if (task
->tk_status
< 0) {
3148 /* Unless we're shutting down, schedule state recovery! */
3149 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3150 nfs4_schedule_state_recovery(clp
);
3153 spin_lock(&clp
->cl_lock
);
3154 if (time_before(clp
->cl_last_renewal
,timestamp
))
3155 clp
->cl_last_renewal
= timestamp
;
3156 spin_unlock(&clp
->cl_lock
);
3159 static const struct rpc_call_ops nfs4_renew_ops
= {
3160 .rpc_call_done
= nfs4_renew_done
,
3163 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3165 struct rpc_message msg
= {
3166 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3171 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3172 &nfs4_renew_ops
, (void *)jiffies
);
3175 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3177 struct rpc_message msg
= {
3178 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3182 unsigned long now
= jiffies
;
3185 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3188 spin_lock(&clp
->cl_lock
);
3189 if (time_before(clp
->cl_last_renewal
,now
))
3190 clp
->cl_last_renewal
= now
;
3191 spin_unlock(&clp
->cl_lock
);
3195 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3197 return (server
->caps
& NFS_CAP_ACLS
)
3198 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3199 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3202 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3203 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3206 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3208 static void buf_to_pages(const void *buf
, size_t buflen
,
3209 struct page
**pages
, unsigned int *pgbase
)
3211 const void *p
= buf
;
3213 *pgbase
= offset_in_page(buf
);
3215 while (p
< buf
+ buflen
) {
3216 *(pages
++) = virt_to_page(p
);
3217 p
+= PAGE_CACHE_SIZE
;
3221 struct nfs4_cached_acl
{
3227 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3229 struct nfs_inode
*nfsi
= NFS_I(inode
);
3231 spin_lock(&inode
->i_lock
);
3232 kfree(nfsi
->nfs4_acl
);
3233 nfsi
->nfs4_acl
= acl
;
3234 spin_unlock(&inode
->i_lock
);
3237 static void nfs4_zap_acl_attr(struct inode
*inode
)
3239 nfs4_set_cached_acl(inode
, NULL
);
3242 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3244 struct nfs_inode
*nfsi
= NFS_I(inode
);
3245 struct nfs4_cached_acl
*acl
;
3248 spin_lock(&inode
->i_lock
);
3249 acl
= nfsi
->nfs4_acl
;
3252 if (buf
== NULL
) /* user is just asking for length */
3254 if (acl
->cached
== 0)
3256 ret
= -ERANGE
; /* see getxattr(2) man page */
3257 if (acl
->len
> buflen
)
3259 memcpy(buf
, acl
->data
, acl
->len
);
3263 spin_unlock(&inode
->i_lock
);
3267 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3269 struct nfs4_cached_acl
*acl
;
3271 if (buf
&& acl_len
<= PAGE_SIZE
) {
3272 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3276 memcpy(acl
->data
, buf
, acl_len
);
3278 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3285 nfs4_set_cached_acl(inode
, acl
);
3288 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3290 struct page
*pages
[NFS4ACL_MAXPAGES
];
3291 struct nfs_getaclargs args
= {
3292 .fh
= NFS_FH(inode
),
3296 struct nfs_getaclres res
= {
3300 struct rpc_message msg
= {
3301 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3305 struct page
*localpage
= NULL
;
3308 if (buflen
< PAGE_SIZE
) {
3309 /* As long as we're doing a round trip to the server anyway,
3310 * let's be prepared for a page of acl data. */
3311 localpage
= alloc_page(GFP_KERNEL
);
3312 resp_buf
= page_address(localpage
);
3313 if (localpage
== NULL
)
3315 args
.acl_pages
[0] = localpage
;
3316 args
.acl_pgbase
= 0;
3317 args
.acl_len
= PAGE_SIZE
;
3320 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3322 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3325 if (res
.acl_len
> args
.acl_len
)
3326 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3328 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3331 if (res
.acl_len
> buflen
)
3334 memcpy(buf
, resp_buf
, res
.acl_len
);
3339 __free_page(localpage
);
3343 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3345 struct nfs4_exception exception
= { };
3348 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3351 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3352 } while (exception
.retry
);
3356 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3358 struct nfs_server
*server
= NFS_SERVER(inode
);
3361 if (!nfs4_server_supports_acls(server
))
3363 ret
= nfs_revalidate_inode(server
, 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);
3394 nfs_access_zap_cache(inode
);
3395 nfs_zap_acl_cache(inode
);
3399 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3401 struct nfs4_exception exception
= { };
3404 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3405 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3407 } while (exception
.retry
);
3412 _nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs_client
*clp
, struct nfs4_state
*state
)
3414 if (!clp
|| task
->tk_status
>= 0)
3416 switch(task
->tk_status
) {
3417 case -NFS4ERR_ADMIN_REVOKED
:
3418 case -NFS4ERR_BAD_STATEID
:
3419 case -NFS4ERR_OPENMODE
:
3422 nfs4_state_mark_reclaim_nograce(clp
, state
);
3423 case -NFS4ERR_STALE_CLIENTID
:
3424 case -NFS4ERR_STALE_STATEID
:
3425 case -NFS4ERR_EXPIRED
:
3426 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3427 nfs4_schedule_state_recovery(clp
);
3428 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3429 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3430 task
->tk_status
= 0;
3432 #if defined(CONFIG_NFS_V4_1)
3433 case -NFS4ERR_BADSESSION
:
3434 case -NFS4ERR_BADSLOT
:
3435 case -NFS4ERR_BAD_HIGH_SLOT
:
3436 case -NFS4ERR_DEADSESSION
:
3437 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3438 case -NFS4ERR_SEQ_FALSE_RETRY
:
3439 case -NFS4ERR_SEQ_MISORDERED
:
3440 dprintk("%s ERROR %d, Reset session\n", __func__
,
3442 nfs4_schedule_state_recovery(clp
);
3443 task
->tk_status
= 0;
3445 #endif /* CONFIG_NFS_V4_1 */
3446 case -NFS4ERR_DELAY
:
3448 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3449 case -NFS4ERR_GRACE
:
3450 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3451 task
->tk_status
= 0;
3453 case -NFS4ERR_OLD_STATEID
:
3454 task
->tk_status
= 0;
3457 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3462 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3464 return _nfs4_async_handle_error(task
, server
, server
->nfs_client
, state
);
3467 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
, unsigned short port
, struct rpc_cred
*cred
)
3469 nfs4_verifier sc_verifier
;
3470 struct nfs4_setclientid setclientid
= {
3471 .sc_verifier
= &sc_verifier
,
3474 struct rpc_message msg
= {
3475 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3476 .rpc_argp
= &setclientid
,
3484 p
= (__be32
*)sc_verifier
.data
;
3485 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3486 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3489 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3490 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3492 rpc_peeraddr2str(clp
->cl_rpcclient
,
3494 rpc_peeraddr2str(clp
->cl_rpcclient
,
3496 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3497 clp
->cl_id_uniquifier
);
3498 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3499 sizeof(setclientid
.sc_netid
),
3500 rpc_peeraddr2str(clp
->cl_rpcclient
,
3501 RPC_DISPLAY_NETID
));
3502 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3503 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3504 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3506 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3507 if (status
!= -NFS4ERR_CLID_INUSE
)
3512 ssleep(clp
->cl_lease_time
+ 1);
3514 if (++clp
->cl_id_uniquifier
== 0)
3520 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3522 struct nfs_fsinfo fsinfo
;
3523 struct rpc_message msg
= {
3524 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3526 .rpc_resp
= &fsinfo
,
3533 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3535 spin_lock(&clp
->cl_lock
);
3536 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3537 clp
->cl_last_renewal
= now
;
3538 spin_unlock(&clp
->cl_lock
);
3543 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3548 err
= _nfs4_proc_setclientid_confirm(clp
, cred
);
3552 case -NFS4ERR_RESOURCE
:
3553 /* The IBM lawyers misread another document! */
3554 case -NFS4ERR_DELAY
:
3555 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3561 struct nfs4_delegreturndata
{
3562 struct nfs4_delegreturnargs args
;
3563 struct nfs4_delegreturnres res
;
3565 nfs4_stateid stateid
;
3566 unsigned long timestamp
;
3567 struct nfs_fattr fattr
;
3571 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3573 struct nfs4_delegreturndata
*data
= calldata
;
3575 nfs4_sequence_done(data
->res
.server
, &data
->res
.seq_res
,
3578 switch (task
->tk_status
) {
3579 case -NFS4ERR_STALE_STATEID
:
3580 case -NFS4ERR_EXPIRED
:
3582 renew_lease(data
->res
.server
, data
->timestamp
);
3585 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
3587 nfs_restart_rpc(task
, data
->res
.server
->nfs_client
);
3591 data
->rpc_status
= task
->tk_status
;
3594 static void nfs4_delegreturn_release(void *calldata
)
3599 #if defined(CONFIG_NFS_V4_1)
3600 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3602 struct nfs4_delegreturndata
*d_data
;
3604 d_data
= (struct nfs4_delegreturndata
*)data
;
3606 if (nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
3607 &d_data
->args
.seq_args
,
3608 &d_data
->res
.seq_res
, 1, task
))
3610 rpc_call_start(task
);
3612 #endif /* CONFIG_NFS_V4_1 */
3614 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3615 #if defined(CONFIG_NFS_V4_1)
3616 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3617 #endif /* CONFIG_NFS_V4_1 */
3618 .rpc_call_done
= nfs4_delegreturn_done
,
3619 .rpc_release
= nfs4_delegreturn_release
,
3622 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3624 struct nfs4_delegreturndata
*data
;
3625 struct nfs_server
*server
= NFS_SERVER(inode
);
3626 struct rpc_task
*task
;
3627 struct rpc_message msg
= {
3628 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3631 struct rpc_task_setup task_setup_data
= {
3632 .rpc_client
= server
->client
,
3633 .rpc_message
= &msg
,
3634 .callback_ops
= &nfs4_delegreturn_ops
,
3635 .flags
= RPC_TASK_ASYNC
,
3639 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3642 data
->args
.fhandle
= &data
->fh
;
3643 data
->args
.stateid
= &data
->stateid
;
3644 data
->args
.bitmask
= server
->attr_bitmask
;
3645 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3646 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3647 data
->res
.fattr
= &data
->fattr
;
3648 data
->res
.server
= server
;
3649 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3650 nfs_fattr_init(data
->res
.fattr
);
3651 data
->timestamp
= jiffies
;
3652 data
->rpc_status
= 0;
3654 task_setup_data
.callback_data
= data
;
3655 msg
.rpc_argp
= &data
->args
,
3656 msg
.rpc_resp
= &data
->res
,
3657 task
= rpc_run_task(&task_setup_data
);
3659 return PTR_ERR(task
);
3662 status
= nfs4_wait_for_completion_rpc_task(task
);
3665 status
= data
->rpc_status
;
3668 nfs_refresh_inode(inode
, &data
->fattr
);
3674 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3676 struct nfs_server
*server
= NFS_SERVER(inode
);
3677 struct nfs4_exception exception
= { };
3680 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3682 case -NFS4ERR_STALE_STATEID
:
3683 case -NFS4ERR_EXPIRED
:
3687 err
= nfs4_handle_exception(server
, err
, &exception
);
3688 } while (exception
.retry
);
3692 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3693 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3696 * sleep, with exponential backoff, and retry the LOCK operation.
3698 static unsigned long
3699 nfs4_set_lock_task_retry(unsigned long timeout
)
3701 schedule_timeout_killable(timeout
);
3703 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3704 return NFS4_LOCK_MAXTIMEOUT
;
3708 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3710 struct inode
*inode
= state
->inode
;
3711 struct nfs_server
*server
= NFS_SERVER(inode
);
3712 struct nfs_client
*clp
= server
->nfs_client
;
3713 struct nfs_lockt_args arg
= {
3714 .fh
= NFS_FH(inode
),
3717 struct nfs_lockt_res res
= {
3720 struct rpc_message msg
= {
3721 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3724 .rpc_cred
= state
->owner
->so_cred
,
3726 struct nfs4_lock_state
*lsp
;
3729 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3730 status
= nfs4_set_lock_state(state
, request
);
3733 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3734 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3735 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3738 request
->fl_type
= F_UNLCK
;
3740 case -NFS4ERR_DENIED
:
3743 request
->fl_ops
->fl_release_private(request
);
3748 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3750 struct nfs4_exception exception
= { };
3754 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3755 _nfs4_proc_getlk(state
, cmd
, request
),
3757 } while (exception
.retry
);
3761 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3764 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3766 res
= posix_lock_file_wait(file
, fl
);
3769 res
= flock_lock_file_wait(file
, fl
);
3777 struct nfs4_unlockdata
{
3778 struct nfs_locku_args arg
;
3779 struct nfs_locku_res res
;
3780 struct nfs4_lock_state
*lsp
;
3781 struct nfs_open_context
*ctx
;
3782 struct file_lock fl
;
3783 const struct nfs_server
*server
;
3784 unsigned long timestamp
;
3787 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3788 struct nfs_open_context
*ctx
,
3789 struct nfs4_lock_state
*lsp
,
3790 struct nfs_seqid
*seqid
)
3792 struct nfs4_unlockdata
*p
;
3793 struct inode
*inode
= lsp
->ls_state
->inode
;
3795 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3798 p
->arg
.fh
= NFS_FH(inode
);
3800 p
->arg
.seqid
= seqid
;
3801 p
->res
.seqid
= seqid
;
3802 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3803 p
->arg
.stateid
= &lsp
->ls_stateid
;
3805 atomic_inc(&lsp
->ls_count
);
3806 /* Ensure we don't close file until we're done freeing locks! */
3807 p
->ctx
= get_nfs_open_context(ctx
);
3808 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3809 p
->server
= NFS_SERVER(inode
);
3813 static void nfs4_locku_release_calldata(void *data
)
3815 struct nfs4_unlockdata
*calldata
= data
;
3816 nfs_free_seqid(calldata
->arg
.seqid
);
3817 nfs4_put_lock_state(calldata
->lsp
);
3818 put_nfs_open_context(calldata
->ctx
);
3822 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3824 struct nfs4_unlockdata
*calldata
= data
;
3826 nfs4_sequence_done(calldata
->server
, &calldata
->res
.seq_res
,
3828 if (RPC_ASSASSINATED(task
))
3830 switch (task
->tk_status
) {
3832 memcpy(calldata
->lsp
->ls_stateid
.data
,
3833 calldata
->res
.stateid
.data
,
3834 sizeof(calldata
->lsp
->ls_stateid
.data
));
3835 renew_lease(calldata
->server
, calldata
->timestamp
);
3837 case -NFS4ERR_BAD_STATEID
:
3838 case -NFS4ERR_OLD_STATEID
:
3839 case -NFS4ERR_STALE_STATEID
:
3840 case -NFS4ERR_EXPIRED
:
3843 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3844 nfs_restart_rpc(task
,
3845 calldata
->server
->nfs_client
);
3849 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3851 struct nfs4_unlockdata
*calldata
= data
;
3853 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3855 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3856 /* Note: exit _without_ running nfs4_locku_done */
3857 task
->tk_action
= NULL
;
3860 calldata
->timestamp
= jiffies
;
3861 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
3862 &calldata
->arg
.seq_args
,
3863 &calldata
->res
.seq_res
, 1, task
))
3865 rpc_call_start(task
);
3868 static const struct rpc_call_ops nfs4_locku_ops
= {
3869 .rpc_call_prepare
= nfs4_locku_prepare
,
3870 .rpc_call_done
= nfs4_locku_done
,
3871 .rpc_release
= nfs4_locku_release_calldata
,
3874 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3875 struct nfs_open_context
*ctx
,
3876 struct nfs4_lock_state
*lsp
,
3877 struct nfs_seqid
*seqid
)
3879 struct nfs4_unlockdata
*data
;
3880 struct rpc_message msg
= {
3881 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3882 .rpc_cred
= ctx
->cred
,
3884 struct rpc_task_setup task_setup_data
= {
3885 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3886 .rpc_message
= &msg
,
3887 .callback_ops
= &nfs4_locku_ops
,
3888 .workqueue
= nfsiod_workqueue
,
3889 .flags
= RPC_TASK_ASYNC
,
3892 /* Ensure this is an unlock - when canceling a lock, the
3893 * canceled lock is passed in, and it won't be an unlock.
3895 fl
->fl_type
= F_UNLCK
;
3897 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3899 nfs_free_seqid(seqid
);
3900 return ERR_PTR(-ENOMEM
);
3903 msg
.rpc_argp
= &data
->arg
,
3904 msg
.rpc_resp
= &data
->res
,
3905 task_setup_data
.callback_data
= data
;
3906 return rpc_run_task(&task_setup_data
);
3909 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3911 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3912 struct nfs_seqid
*seqid
;
3913 struct nfs4_lock_state
*lsp
;
3914 struct rpc_task
*task
;
3916 unsigned char fl_flags
= request
->fl_flags
;
3918 status
= nfs4_set_lock_state(state
, request
);
3919 /* Unlock _before_ we do the RPC call */
3920 request
->fl_flags
|= FL_EXISTS
;
3921 down_read(&nfsi
->rwsem
);
3922 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3923 up_read(&nfsi
->rwsem
);
3926 up_read(&nfsi
->rwsem
);
3929 /* Is this a delegated lock? */
3930 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3932 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3933 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3937 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3938 status
= PTR_ERR(task
);
3941 status
= nfs4_wait_for_completion_rpc_task(task
);
3944 request
->fl_flags
= fl_flags
;
3948 struct nfs4_lockdata
{
3949 struct nfs_lock_args arg
;
3950 struct nfs_lock_res res
;
3951 struct nfs4_lock_state
*lsp
;
3952 struct nfs_open_context
*ctx
;
3953 struct file_lock fl
;
3954 unsigned long timestamp
;
3957 struct nfs_server
*server
;
3960 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3961 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
)
3963 struct nfs4_lockdata
*p
;
3964 struct inode
*inode
= lsp
->ls_state
->inode
;
3965 struct nfs_server
*server
= NFS_SERVER(inode
);
3967 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3971 p
->arg
.fh
= NFS_FH(inode
);
3973 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
);
3974 if (p
->arg
.open_seqid
== NULL
)
3976 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3977 if (p
->arg
.lock_seqid
== NULL
)
3978 goto out_free_seqid
;
3979 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
3980 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
3981 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3982 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
3983 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3986 atomic_inc(&lsp
->ls_count
);
3987 p
->ctx
= get_nfs_open_context(ctx
);
3988 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3991 nfs_free_seqid(p
->arg
.open_seqid
);
3997 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
3999 struct nfs4_lockdata
*data
= calldata
;
4000 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4002 dprintk("%s: begin!\n", __func__
);
4003 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4005 /* Do we need to do an open_to_lock_owner? */
4006 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4007 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
4009 data
->arg
.open_stateid
= &state
->stateid
;
4010 data
->arg
.new_lock_owner
= 1;
4011 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4013 data
->arg
.new_lock_owner
= 0;
4014 data
->timestamp
= jiffies
;
4015 if (nfs4_setup_sequence(data
->server
->nfs_client
, &data
->arg
.seq_args
,
4016 &data
->res
.seq_res
, 1, task
))
4018 rpc_call_start(task
);
4019 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4022 static void nfs4_recover_lock_prepare(struct rpc_task
*task
, void *calldata
)
4024 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4025 nfs4_lock_prepare(task
, calldata
);
4028 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4030 struct nfs4_lockdata
*data
= calldata
;
4032 dprintk("%s: begin!\n", __func__
);
4034 nfs4_sequence_done(data
->server
, &data
->res
.seq_res
,
4037 data
->rpc_status
= task
->tk_status
;
4038 if (RPC_ASSASSINATED(task
))
4040 if (data
->arg
.new_lock_owner
!= 0) {
4041 if (data
->rpc_status
== 0)
4042 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4046 if (data
->rpc_status
== 0) {
4047 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
4048 sizeof(data
->lsp
->ls_stateid
.data
));
4049 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
4050 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4053 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4056 static void nfs4_lock_release(void *calldata
)
4058 struct nfs4_lockdata
*data
= calldata
;
4060 dprintk("%s: begin!\n", __func__
);
4061 nfs_free_seqid(data
->arg
.open_seqid
);
4062 if (data
->cancelled
!= 0) {
4063 struct rpc_task
*task
;
4064 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4065 data
->arg
.lock_seqid
);
4068 dprintk("%s: cancelling lock!\n", __func__
);
4070 nfs_free_seqid(data
->arg
.lock_seqid
);
4071 nfs4_put_lock_state(data
->lsp
);
4072 put_nfs_open_context(data
->ctx
);
4074 dprintk("%s: done!\n", __func__
);
4077 static const struct rpc_call_ops nfs4_lock_ops
= {
4078 .rpc_call_prepare
= nfs4_lock_prepare
,
4079 .rpc_call_done
= nfs4_lock_done
,
4080 .rpc_release
= nfs4_lock_release
,
4083 static const struct rpc_call_ops nfs4_recover_lock_ops
= {
4084 .rpc_call_prepare
= nfs4_recover_lock_prepare
,
4085 .rpc_call_done
= nfs4_lock_done
,
4086 .rpc_release
= nfs4_lock_release
,
4089 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4091 struct nfs_client
*clp
= server
->nfs_client
;
4092 struct nfs4_state
*state
= lsp
->ls_state
;
4095 case -NFS4ERR_ADMIN_REVOKED
:
4096 case -NFS4ERR_BAD_STATEID
:
4097 case -NFS4ERR_EXPIRED
:
4098 if (new_lock_owner
!= 0 ||
4099 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4100 nfs4_state_mark_reclaim_nograce(clp
, state
);
4101 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4105 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4107 struct nfs4_lockdata
*data
;
4108 struct rpc_task
*task
;
4109 struct rpc_message msg
= {
4110 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4111 .rpc_cred
= state
->owner
->so_cred
,
4113 struct rpc_task_setup task_setup_data
= {
4114 .rpc_client
= NFS_CLIENT(state
->inode
),
4115 .rpc_message
= &msg
,
4116 .callback_ops
= &nfs4_lock_ops
,
4117 .workqueue
= nfsiod_workqueue
,
4118 .flags
= RPC_TASK_ASYNC
,
4122 dprintk("%s: begin!\n", __func__
);
4123 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4124 fl
->fl_u
.nfs4_fl
.owner
);
4128 data
->arg
.block
= 1;
4129 if (recovery_type
> NFS_LOCK_NEW
) {
4130 if (recovery_type
== NFS_LOCK_RECLAIM
)
4131 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4132 task_setup_data
.callback_ops
= &nfs4_recover_lock_ops
;
4134 msg
.rpc_argp
= &data
->arg
,
4135 msg
.rpc_resp
= &data
->res
,
4136 task_setup_data
.callback_data
= data
;
4137 task
= rpc_run_task(&task_setup_data
);
4139 return PTR_ERR(task
);
4140 ret
= nfs4_wait_for_completion_rpc_task(task
);
4142 ret
= data
->rpc_status
;
4144 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4145 data
->arg
.new_lock_owner
, ret
);
4147 data
->cancelled
= 1;
4149 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4153 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4155 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4156 struct nfs4_exception exception
= { };
4160 /* Cache the lock if possible... */
4161 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4163 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4164 if (err
!= -NFS4ERR_DELAY
)
4166 nfs4_handle_exception(server
, err
, &exception
);
4167 } while (exception
.retry
);
4171 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4173 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4174 struct nfs4_exception exception
= { };
4177 err
= nfs4_set_lock_state(state
, request
);
4181 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4183 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4187 case -NFS4ERR_GRACE
:
4188 case -NFS4ERR_DELAY
:
4189 nfs4_handle_exception(server
, err
, &exception
);
4192 } while (exception
.retry
);
4197 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4199 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4200 unsigned char fl_flags
= request
->fl_flags
;
4201 int status
= -ENOLCK
;
4203 if ((fl_flags
& FL_POSIX
) &&
4204 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4206 /* Is this a delegated open? */
4207 status
= nfs4_set_lock_state(state
, request
);
4210 request
->fl_flags
|= FL_ACCESS
;
4211 status
= do_vfs_lock(request
->fl_file
, request
);
4214 down_read(&nfsi
->rwsem
);
4215 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4216 /* Yes: cache locks! */
4217 /* ...but avoid races with delegation recall... */
4218 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4219 status
= do_vfs_lock(request
->fl_file
, request
);
4222 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4225 /* Note: we always want to sleep here! */
4226 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4227 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4228 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4230 up_read(&nfsi
->rwsem
);
4232 request
->fl_flags
= fl_flags
;
4236 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4238 struct nfs4_exception exception
= { };
4242 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4243 if (err
== -NFS4ERR_DENIED
)
4245 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4247 } while (exception
.retry
);
4252 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4254 struct nfs_open_context
*ctx
;
4255 struct nfs4_state
*state
;
4256 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4259 /* verify open state */
4260 ctx
= nfs_file_open_context(filp
);
4263 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4266 if (IS_GETLK(cmd
)) {
4268 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4272 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4275 if (request
->fl_type
== F_UNLCK
) {
4277 return nfs4_proc_unlck(state
, cmd
, request
);
4284 status
= nfs4_proc_setlk(state
, cmd
, request
);
4285 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4287 timeout
= nfs4_set_lock_task_retry(timeout
);
4288 status
= -ERESTARTSYS
;
4291 } while(status
< 0);
4295 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4297 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4298 struct nfs4_exception exception
= { };
4301 err
= nfs4_set_lock_state(state
, fl
);
4305 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4308 printk(KERN_ERR
"%s: unhandled error %d.\n",
4313 case -NFS4ERR_EXPIRED
:
4314 case -NFS4ERR_STALE_CLIENTID
:
4315 case -NFS4ERR_STALE_STATEID
:
4316 case -NFS4ERR_BADSESSION
:
4317 case -NFS4ERR_BADSLOT
:
4318 case -NFS4ERR_BAD_HIGH_SLOT
:
4319 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4320 case -NFS4ERR_DEADSESSION
:
4321 nfs4_schedule_state_recovery(server
->nfs_client
);
4325 * The show must go on: exit, but mark the
4326 * stateid as needing recovery.
4328 case -NFS4ERR_ADMIN_REVOKED
:
4329 case -NFS4ERR_BAD_STATEID
:
4330 case -NFS4ERR_OPENMODE
:
4331 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
4335 case -NFS4ERR_DENIED
:
4336 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4339 case -NFS4ERR_DELAY
:
4342 err
= nfs4_handle_exception(server
, err
, &exception
);
4343 } while (exception
.retry
);
4348 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4350 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4351 size_t buflen
, int flags
)
4353 struct inode
*inode
= dentry
->d_inode
;
4355 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4358 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4361 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4362 * and that's what we'll do for e.g. user attributes that haven't been set.
4363 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4364 * attributes in kernel-managed attribute namespaces. */
4365 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4368 struct inode
*inode
= dentry
->d_inode
;
4370 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4373 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4376 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4378 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4380 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4382 if (buf
&& buflen
< len
)
4385 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4389 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4391 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4392 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4393 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4396 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4397 NFS_ATTR_FATTR_NLINK
;
4398 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4402 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4403 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4405 struct nfs_server
*server
= NFS_SERVER(dir
);
4407 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4408 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4410 struct nfs4_fs_locations_arg args
= {
4411 .dir_fh
= NFS_FH(dir
),
4416 struct nfs4_fs_locations_res res
= {
4417 .fs_locations
= fs_locations
,
4419 struct rpc_message msg
= {
4420 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4426 dprintk("%s: start\n", __func__
);
4427 nfs_fattr_init(&fs_locations
->fattr
);
4428 fs_locations
->server
= server
;
4429 fs_locations
->nlocations
= 0;
4430 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4431 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4432 dprintk("%s: returned status = %d\n", __func__
, status
);
4436 #ifdef CONFIG_NFS_V4_1
4438 * nfs4_proc_exchange_id()
4440 * Since the clientid has expired, all compounds using sessions
4441 * associated with the stale clientid will be returning
4442 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4443 * be in some phase of session reset.
4445 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4447 nfs4_verifier verifier
;
4448 struct nfs41_exchange_id_args args
= {
4450 .flags
= clp
->cl_exchange_flags
,
4452 struct nfs41_exchange_id_res res
= {
4456 struct rpc_message msg
= {
4457 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4464 dprintk("--> %s\n", __func__
);
4465 BUG_ON(clp
== NULL
);
4467 /* Remove server-only flags */
4468 args
.flags
&= ~EXCHGID4_FLAG_CONFIRMED_R
;
4470 p
= (u32
*)verifier
.data
;
4471 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4472 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4473 args
.verifier
= &verifier
;
4476 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4479 rpc_peeraddr2str(clp
->cl_rpcclient
,
4481 clp
->cl_id_uniquifier
);
4483 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4485 if (status
!= NFS4ERR_CLID_INUSE
)
4491 if (++clp
->cl_id_uniquifier
== 0)
4495 dprintk("<-- %s status= %d\n", __func__
, status
);
4499 struct nfs4_get_lease_time_data
{
4500 struct nfs4_get_lease_time_args
*args
;
4501 struct nfs4_get_lease_time_res
*res
;
4502 struct nfs_client
*clp
;
4505 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4509 struct nfs4_get_lease_time_data
*data
=
4510 (struct nfs4_get_lease_time_data
*)calldata
;
4512 dprintk("--> %s\n", __func__
);
4513 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4514 /* just setup sequence, do not trigger session recovery
4515 since we're invoked within one */
4516 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4517 &data
->args
->la_seq_args
,
4518 &data
->res
->lr_seq_res
, 0, task
);
4520 BUG_ON(ret
== -EAGAIN
);
4521 rpc_call_start(task
);
4522 dprintk("<-- %s\n", __func__
);
4526 * Called from nfs4_state_manager thread for session setup, so don't recover
4527 * from sequence operation or clientid errors.
4529 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4531 struct nfs4_get_lease_time_data
*data
=
4532 (struct nfs4_get_lease_time_data
*)calldata
;
4534 dprintk("--> %s\n", __func__
);
4535 nfs41_sequence_done(data
->clp
, &data
->res
->lr_seq_res
, task
->tk_status
);
4536 switch (task
->tk_status
) {
4537 case -NFS4ERR_DELAY
:
4538 case -NFS4ERR_GRACE
:
4539 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4540 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4541 task
->tk_status
= 0;
4542 nfs_restart_rpc(task
, data
->clp
);
4545 dprintk("<-- %s\n", __func__
);
4548 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4549 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4550 .rpc_call_done
= nfs4_get_lease_time_done
,
4553 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4555 struct rpc_task
*task
;
4556 struct nfs4_get_lease_time_args args
;
4557 struct nfs4_get_lease_time_res res
= {
4558 .lr_fsinfo
= fsinfo
,
4560 struct nfs4_get_lease_time_data data
= {
4565 struct rpc_message msg
= {
4566 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4570 struct rpc_task_setup task_setup
= {
4571 .rpc_client
= clp
->cl_rpcclient
,
4572 .rpc_message
= &msg
,
4573 .callback_ops
= &nfs4_get_lease_time_ops
,
4574 .callback_data
= &data
4578 res
.lr_seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4579 dprintk("--> %s\n", __func__
);
4580 task
= rpc_run_task(&task_setup
);
4583 status
= PTR_ERR(task
);
4585 status
= task
->tk_status
;
4588 dprintk("<-- %s return %d\n", __func__
, status
);
4594 * Reset a slot table
4596 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, int max_slots
,
4597 int old_max_slots
, int ivalue
)
4602 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__
, max_slots
, tbl
);
4605 * Until we have dynamic slot table adjustment, insist
4606 * upon the same slot table size
4608 if (max_slots
!= old_max_slots
) {
4609 dprintk("%s reset slot table does't match old\n",
4611 ret
= -EINVAL
; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4614 spin_lock(&tbl
->slot_tbl_lock
);
4615 for (i
= 0; i
< max_slots
; ++i
)
4616 tbl
->slots
[i
].seq_nr
= ivalue
;
4617 spin_unlock(&tbl
->slot_tbl_lock
);
4618 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4619 tbl
, tbl
->slots
, tbl
->max_slots
);
4621 dprintk("<-- %s: return %d\n", __func__
, ret
);
4626 * Reset the forechannel and backchannel slot tables
4628 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
4632 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
4633 session
->fc_attrs
.max_reqs
,
4634 session
->fc_slot_table
.max_slots
,
4639 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
4640 session
->bc_attrs
.max_reqs
,
4641 session
->bc_slot_table
.max_slots
,
4646 /* Destroy the slot table */
4647 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
4649 if (session
->fc_slot_table
.slots
!= NULL
) {
4650 kfree(session
->fc_slot_table
.slots
);
4651 session
->fc_slot_table
.slots
= NULL
;
4653 if (session
->bc_slot_table
.slots
!= NULL
) {
4654 kfree(session
->bc_slot_table
.slots
);
4655 session
->bc_slot_table
.slots
= NULL
;
4661 * Initialize slot table
4663 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
4664 int max_slots
, int ivalue
)
4666 struct nfs4_slot
*slot
;
4669 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4671 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
4673 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_KERNEL
);
4678 spin_lock(&tbl
->slot_tbl_lock
);
4679 tbl
->max_slots
= max_slots
;
4681 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4682 spin_unlock(&tbl
->slot_tbl_lock
);
4683 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4684 tbl
, tbl
->slots
, tbl
->max_slots
);
4686 dprintk("<-- %s: return %d\n", __func__
, ret
);
4691 * Initialize the forechannel and backchannel tables
4693 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
4695 struct nfs4_slot_table
*tbl
;
4698 tbl
= &session
->fc_slot_table
;
4699 if (tbl
->slots
== NULL
) {
4700 status
= nfs4_init_slot_table(tbl
,
4701 session
->fc_attrs
.max_reqs
, 1);
4706 tbl
= &session
->bc_slot_table
;
4707 if (tbl
->slots
== NULL
) {
4708 status
= nfs4_init_slot_table(tbl
,
4709 session
->bc_attrs
.max_reqs
, 0);
4711 nfs4_destroy_slot_tables(session
);
4717 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4719 struct nfs4_session
*session
;
4720 struct nfs4_slot_table
*tbl
;
4722 session
= kzalloc(sizeof(struct nfs4_session
), GFP_KERNEL
);
4727 * The create session reply races with the server back
4728 * channel probe. Mark the client NFS_CS_SESSION_INITING
4729 * so that the client back channel can find the
4732 clp
->cl_cons_state
= NFS_CS_SESSION_INITING
;
4733 init_completion(&session
->complete
);
4735 tbl
= &session
->fc_slot_table
;
4736 tbl
->highest_used_slotid
= -1;
4737 spin_lock_init(&tbl
->slot_tbl_lock
);
4738 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
4740 tbl
= &session
->bc_slot_table
;
4741 tbl
->highest_used_slotid
= -1;
4742 spin_lock_init(&tbl
->slot_tbl_lock
);
4743 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
4749 void nfs4_destroy_session(struct nfs4_session
*session
)
4751 nfs4_proc_destroy_session(session
);
4752 dprintk("%s Destroy backchannel for xprt %p\n",
4753 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
4754 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
4755 NFS41_BC_MIN_CALLBACKS
);
4756 nfs4_destroy_slot_tables(session
);
4761 * Initialize the values to be used by the client in CREATE_SESSION
4762 * If nfs4_init_session set the fore channel request and response sizes,
4765 * Set the back channel max_resp_sz_cached to zero to force the client to
4766 * always set csa_cachethis to FALSE because the current implementation
4767 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4769 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4771 struct nfs4_session
*session
= args
->client
->cl_session
;
4772 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4773 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4776 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4778 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4779 /* Fore channel attributes */
4780 args
->fc_attrs
.headerpadsz
= 0;
4781 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4782 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4783 args
->fc_attrs
.max_resp_sz_cached
= mxresp_sz
;
4784 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4785 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4787 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4788 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4790 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4791 args
->fc_attrs
.max_resp_sz_cached
, args
->fc_attrs
.max_ops
,
4792 args
->fc_attrs
.max_reqs
);
4794 /* Back channel attributes */
4795 args
->bc_attrs
.headerpadsz
= 0;
4796 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4797 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4798 args
->bc_attrs
.max_resp_sz_cached
= 0;
4799 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4800 args
->bc_attrs
.max_reqs
= 1;
4802 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4803 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4805 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4806 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4807 args
->bc_attrs
.max_reqs
);
4810 static int _verify_channel_attr(char *chan
, char *attr_name
, u32 sent
, u32 rcvd
)
4814 printk(KERN_WARNING
"%s: Session INVALID: %s channel %s increased. "
4815 "sent=%u rcvd=%u\n", __func__
, chan
, attr_name
, sent
, rcvd
);
4819 #define _verify_fore_channel_attr(_name_) \
4820 _verify_channel_attr("fore", #_name_, \
4821 args->fc_attrs._name_, \
4822 session->fc_attrs._name_)
4824 #define _verify_back_channel_attr(_name_) \
4825 _verify_channel_attr("back", #_name_, \
4826 args->bc_attrs._name_, \
4827 session->bc_attrs._name_)
4830 * The server is not allowed to increase the fore channel header pad size,
4831 * maximum response size, or maximum number of operations.
4833 * The back channel attributes are only negotiatied down: We send what the
4834 * (back channel) server insists upon.
4836 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4837 struct nfs4_session
*session
)
4841 ret
|= _verify_fore_channel_attr(headerpadsz
);
4842 ret
|= _verify_fore_channel_attr(max_resp_sz
);
4843 ret
|= _verify_fore_channel_attr(max_ops
);
4845 ret
|= _verify_back_channel_attr(headerpadsz
);
4846 ret
|= _verify_back_channel_attr(max_rqst_sz
);
4847 ret
|= _verify_back_channel_attr(max_resp_sz
);
4848 ret
|= _verify_back_channel_attr(max_resp_sz_cached
);
4849 ret
|= _verify_back_channel_attr(max_ops
);
4850 ret
|= _verify_back_channel_attr(max_reqs
);
4855 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4857 struct nfs4_session
*session
= clp
->cl_session
;
4858 struct nfs41_create_session_args args
= {
4860 .cb_program
= NFS4_CALLBACK
,
4862 struct nfs41_create_session_res res
= {
4865 struct rpc_message msg
= {
4866 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4872 nfs4_init_channel_attrs(&args
);
4873 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
4875 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4878 /* Verify the session's negotiated channel_attrs values */
4879 status
= nfs4_verify_channel_attrs(&args
, session
);
4881 /* Increment the clientid slot sequence id */
4889 * Issues a CREATE_SESSION operation to the server.
4890 * It is the responsibility of the caller to verify the session is
4891 * expired before calling this routine.
4893 int nfs4_proc_create_session(struct nfs_client
*clp
)
4897 struct nfs4_session
*session
= clp
->cl_session
;
4899 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4901 status
= _nfs4_proc_create_session(clp
);
4905 /* Init and reset the fore channel */
4906 status
= nfs4_init_slot_tables(session
);
4907 dprintk("slot table initialization returned %d\n", status
);
4910 status
= nfs4_reset_slot_tables(session
);
4911 dprintk("slot table reset returned %d\n", status
);
4915 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4916 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4917 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4919 dprintk("<-- %s\n", __func__
);
4924 * Issue the over-the-wire RPC DESTROY_SESSION.
4925 * The caller must serialize access to this routine.
4927 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4930 struct rpc_message msg
;
4932 dprintk("--> nfs4_proc_destroy_session\n");
4934 /* session is still being setup */
4935 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
4938 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
4939 msg
.rpc_argp
= session
;
4940 msg
.rpc_resp
= NULL
;
4941 msg
.rpc_cred
= NULL
;
4942 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4946 "Got error %d from the server on DESTROY_SESSION. "
4947 "Session has been destroyed regardless...\n", status
);
4949 dprintk("<-- nfs4_proc_destroy_session\n");
4953 int nfs4_init_session(struct nfs_server
*server
)
4955 struct nfs_client
*clp
= server
->nfs_client
;
4956 struct nfs4_session
*session
;
4957 unsigned int rsize
, wsize
;
4960 if (!nfs4_has_session(clp
))
4963 rsize
= server
->rsize
;
4965 rsize
= NFS_MAX_FILE_IO_SIZE
;
4966 wsize
= server
->wsize
;
4968 wsize
= NFS_MAX_FILE_IO_SIZE
;
4970 session
= clp
->cl_session
;
4971 session
->fc_attrs
.max_rqst_sz
= wsize
+ nfs41_maxwrite_overhead
;
4972 session
->fc_attrs
.max_resp_sz
= rsize
+ nfs41_maxread_overhead
;
4974 ret
= nfs4_recover_expired_lease(server
);
4976 ret
= nfs4_check_client_ready(clp
);
4981 * Renew the cl_session lease.
4983 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4985 struct nfs4_sequence_args args
;
4986 struct nfs4_sequence_res res
;
4988 struct rpc_message msg
= {
4989 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
4995 args
.sa_cache_this
= 0;
4997 return nfs4_call_sync_sequence(clp
, clp
->cl_rpcclient
, &msg
, &args
,
4998 &res
, args
.sa_cache_this
, 1);
5001 void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5003 struct nfs_client
*clp
= (struct nfs_client
*)data
;
5005 nfs41_sequence_done(clp
, task
->tk_msg
.rpc_resp
, task
->tk_status
);
5007 if (task
->tk_status
< 0) {
5008 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5010 if (_nfs4_async_handle_error(task
, NULL
, clp
, NULL
)
5012 nfs_restart_rpc(task
, clp
);
5016 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5018 kfree(task
->tk_msg
.rpc_argp
);
5019 kfree(task
->tk_msg
.rpc_resp
);
5021 dprintk("<-- %s\n", __func__
);
5024 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5026 struct nfs_client
*clp
;
5027 struct nfs4_sequence_args
*args
;
5028 struct nfs4_sequence_res
*res
;
5030 clp
= (struct nfs_client
*)data
;
5031 args
= task
->tk_msg
.rpc_argp
;
5032 res
= task
->tk_msg
.rpc_resp
;
5034 if (nfs4_setup_sequence(clp
, args
, res
, 0, task
))
5036 rpc_call_start(task
);
5039 static const struct rpc_call_ops nfs41_sequence_ops
= {
5040 .rpc_call_done
= nfs41_sequence_call_done
,
5041 .rpc_call_prepare
= nfs41_sequence_prepare
,
5044 static int nfs41_proc_async_sequence(struct nfs_client
*clp
,
5045 struct rpc_cred
*cred
)
5047 struct nfs4_sequence_args
*args
;
5048 struct nfs4_sequence_res
*res
;
5049 struct rpc_message msg
= {
5050 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5054 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
5057 res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
5062 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5063 msg
.rpc_argp
= args
;
5066 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
5067 &nfs41_sequence_ops
, (void *)clp
);
5070 struct nfs4_reclaim_complete_data
{
5071 struct nfs_client
*clp
;
5072 struct nfs41_reclaim_complete_args arg
;
5073 struct nfs41_reclaim_complete_res res
;
5076 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5078 struct nfs4_reclaim_complete_data
*calldata
= data
;
5080 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
5081 if (nfs4_setup_sequence(calldata
->clp
, &calldata
->arg
.seq_args
,
5082 &calldata
->res
.seq_res
, 0, task
))
5085 rpc_call_start(task
);
5088 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5090 struct nfs4_reclaim_complete_data
*calldata
= data
;
5091 struct nfs_client
*clp
= calldata
->clp
;
5092 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5094 dprintk("--> %s\n", __func__
);
5095 nfs41_sequence_done(clp
, res
, task
->tk_status
);
5096 switch (task
->tk_status
) {
5098 case -NFS4ERR_COMPLETE_ALREADY
:
5100 case -NFS4ERR_BADSESSION
:
5101 case -NFS4ERR_DEADSESSION
:
5103 * Handle the session error, but do not retry the operation, as
5104 * we have no way of telling whether the clientid had to be
5105 * reset before we got our reply. If reset, a new wave of
5106 * reclaim operations will follow, containing their own reclaim
5107 * complete. We don't want our retry to get on the way of
5108 * recovery by incorrectly indicating to the server that we're
5109 * done reclaiming state since the process had to be restarted.
5111 _nfs4_async_handle_error(task
, NULL
, clp
, NULL
);
5114 if (_nfs4_async_handle_error(
5115 task
, NULL
, clp
, NULL
) == -EAGAIN
) {
5116 rpc_restart_call_prepare(task
);
5121 dprintk("<-- %s\n", __func__
);
5124 static void nfs4_free_reclaim_complete_data(void *data
)
5126 struct nfs4_reclaim_complete_data
*calldata
= data
;
5131 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5132 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5133 .rpc_call_done
= nfs4_reclaim_complete_done
,
5134 .rpc_release
= nfs4_free_reclaim_complete_data
,
5138 * Issue a global reclaim complete.
5140 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5142 struct nfs4_reclaim_complete_data
*calldata
;
5143 struct rpc_task
*task
;
5144 struct rpc_message msg
= {
5145 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5147 struct rpc_task_setup task_setup_data
= {
5148 .rpc_client
= clp
->cl_rpcclient
,
5149 .rpc_message
= &msg
,
5150 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5151 .flags
= RPC_TASK_ASYNC
,
5153 int status
= -ENOMEM
;
5155 dprintk("--> %s\n", __func__
);
5156 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
5157 if (calldata
== NULL
)
5159 calldata
->clp
= clp
;
5160 calldata
->arg
.one_fs
= 0;
5161 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5163 msg
.rpc_argp
= &calldata
->arg
;
5164 msg
.rpc_resp
= &calldata
->res
;
5165 task_setup_data
.callback_data
= calldata
;
5166 task
= rpc_run_task(&task_setup_data
);
5168 status
= PTR_ERR(task
);
5171 dprintk("<-- %s status=%d\n", __func__
, status
);
5174 #endif /* CONFIG_NFS_V4_1 */
5176 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5177 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5178 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5179 .recover_open
= nfs4_open_reclaim
,
5180 .recover_lock
= nfs4_lock_reclaim
,
5181 .establish_clid
= nfs4_init_clientid
,
5182 .get_clid_cred
= nfs4_get_setclientid_cred
,
5185 #if defined(CONFIG_NFS_V4_1)
5186 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5187 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5188 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5189 .recover_open
= nfs4_open_reclaim
,
5190 .recover_lock
= nfs4_lock_reclaim
,
5191 .establish_clid
= nfs41_init_clientid
,
5192 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5193 .reclaim_complete
= nfs41_proc_reclaim_complete
,
5195 #endif /* CONFIG_NFS_V4_1 */
5197 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5198 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5199 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5200 .recover_open
= nfs4_open_expired
,
5201 .recover_lock
= nfs4_lock_expired
,
5202 .establish_clid
= nfs4_init_clientid
,
5203 .get_clid_cred
= nfs4_get_setclientid_cred
,
5206 #if defined(CONFIG_NFS_V4_1)
5207 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5208 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5209 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5210 .recover_open
= nfs4_open_expired
,
5211 .recover_lock
= nfs4_lock_expired
,
5212 .establish_clid
= nfs41_init_clientid
,
5213 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5215 #endif /* CONFIG_NFS_V4_1 */
5217 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
5218 .sched_state_renewal
= nfs4_proc_async_renew
,
5219 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
5220 .renew_lease
= nfs4_proc_renew
,
5223 #if defined(CONFIG_NFS_V4_1)
5224 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
5225 .sched_state_renewal
= nfs41_proc_async_sequence
,
5226 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
5227 .renew_lease
= nfs4_proc_sequence
,
5232 * Per minor version reboot and network partition recovery ops
5235 struct nfs4_state_recovery_ops
*nfs4_reboot_recovery_ops
[] = {
5236 &nfs40_reboot_recovery_ops
,
5237 #if defined(CONFIG_NFS_V4_1)
5238 &nfs41_reboot_recovery_ops
,
5242 struct nfs4_state_recovery_ops
*nfs4_nograce_recovery_ops
[] = {
5243 &nfs40_nograce_recovery_ops
,
5244 #if defined(CONFIG_NFS_V4_1)
5245 &nfs41_nograce_recovery_ops
,
5249 struct nfs4_state_maintenance_ops
*nfs4_state_renewal_ops
[] = {
5250 &nfs40_state_renewal_ops
,
5251 #if defined(CONFIG_NFS_V4_1)
5252 &nfs41_state_renewal_ops
,
5256 static const struct inode_operations nfs4_file_inode_operations
= {
5257 .permission
= nfs_permission
,
5258 .getattr
= nfs_getattr
,
5259 .setattr
= nfs_setattr
,
5260 .getxattr
= nfs4_getxattr
,
5261 .setxattr
= nfs4_setxattr
,
5262 .listxattr
= nfs4_listxattr
,
5265 const struct nfs_rpc_ops nfs_v4_clientops
= {
5266 .version
= 4, /* protocol version */
5267 .dentry_ops
= &nfs4_dentry_operations
,
5268 .dir_inode_ops
= &nfs4_dir_inode_operations
,
5269 .file_inode_ops
= &nfs4_file_inode_operations
,
5270 .getroot
= nfs4_proc_get_root
,
5271 .getattr
= nfs4_proc_getattr
,
5272 .setattr
= nfs4_proc_setattr
,
5273 .lookupfh
= nfs4_proc_lookupfh
,
5274 .lookup
= nfs4_proc_lookup
,
5275 .access
= nfs4_proc_access
,
5276 .readlink
= nfs4_proc_readlink
,
5277 .create
= nfs4_proc_create
,
5278 .remove
= nfs4_proc_remove
,
5279 .unlink_setup
= nfs4_proc_unlink_setup
,
5280 .unlink_done
= nfs4_proc_unlink_done
,
5281 .rename
= nfs4_proc_rename
,
5282 .link
= nfs4_proc_link
,
5283 .symlink
= nfs4_proc_symlink
,
5284 .mkdir
= nfs4_proc_mkdir
,
5285 .rmdir
= nfs4_proc_remove
,
5286 .readdir
= nfs4_proc_readdir
,
5287 .mknod
= nfs4_proc_mknod
,
5288 .statfs
= nfs4_proc_statfs
,
5289 .fsinfo
= nfs4_proc_fsinfo
,
5290 .pathconf
= nfs4_proc_pathconf
,
5291 .set_capabilities
= nfs4_server_capabilities
,
5292 .decode_dirent
= nfs4_decode_dirent
,
5293 .read_setup
= nfs4_proc_read_setup
,
5294 .read_done
= nfs4_read_done
,
5295 .write_setup
= nfs4_proc_write_setup
,
5296 .write_done
= nfs4_write_done
,
5297 .commit_setup
= nfs4_proc_commit_setup
,
5298 .commit_done
= nfs4_commit_done
,
5299 .lock
= nfs4_proc_lock
,
5300 .clear_acl_cache
= nfs4_zap_acl_attr
,
5301 .close_context
= nfs4_close_context
,