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 goto do_state_recovery
;
253 case -NFS4ERR_STALE_STATEID
:
256 nfs4_state_mark_reclaim_reboot(clp
, state
);
257 case -NFS4ERR_STALE_CLIENTID
:
258 case -NFS4ERR_EXPIRED
:
259 goto do_state_recovery
;
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
);
293 nfs4_schedule_state_recovery(clp
);
294 ret
= nfs4_wait_clnt_recover(clp
);
296 exception
->retry
= 1;
301 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
303 struct nfs_client
*clp
= server
->nfs_client
;
304 spin_lock(&clp
->cl_lock
);
305 if (time_before(clp
->cl_last_renewal
,timestamp
))
306 clp
->cl_last_renewal
= timestamp
;
307 spin_unlock(&clp
->cl_lock
);
310 #if defined(CONFIG_NFS_V4_1)
313 * nfs4_free_slot - free a slot and efficiently update slot table.
315 * freeing a slot is trivially done by clearing its respective bit
317 * If the freed slotid equals highest_used_slotid we want to update it
318 * so that the server would be able to size down the slot table if needed,
319 * otherwise we know that the highest_used_slotid is still in use.
320 * When updating highest_used_slotid there may be "holes" in the bitmap
321 * so we need to scan down from highest_used_slotid to 0 looking for the now
322 * highest slotid in use.
323 * If none found, highest_used_slotid is set to -1.
325 * Must be called while holding tbl->slot_tbl_lock
328 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
330 int slotid
= free_slotid
;
332 /* clear used bit in bitmap */
333 __clear_bit(slotid
, tbl
->used_slots
);
335 /* update highest_used_slotid when it is freed */
336 if (slotid
== tbl
->highest_used_slotid
) {
337 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
338 if (slotid
< tbl
->max_slots
)
339 tbl
->highest_used_slotid
= slotid
;
341 tbl
->highest_used_slotid
= -1;
343 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
344 free_slotid
, tbl
->highest_used_slotid
);
348 * Signal state manager thread if session is drained
350 static void nfs41_check_drain_session_complete(struct nfs4_session
*ses
)
352 struct rpc_task
*task
;
354 if (!test_bit(NFS4CLNT_SESSION_DRAINING
, &ses
->clp
->cl_state
)) {
355 task
= rpc_wake_up_next(&ses
->fc_slot_table
.slot_tbl_waitq
);
357 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
361 if (ses
->fc_slot_table
.highest_used_slotid
!= -1)
364 dprintk("%s COMPLETE: Session Drained\n", __func__
);
365 complete(&ses
->complete
);
368 static void nfs41_sequence_free_slot(const struct nfs_client
*clp
,
369 struct nfs4_sequence_res
*res
)
371 struct nfs4_slot_table
*tbl
;
373 tbl
= &clp
->cl_session
->fc_slot_table
;
374 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
375 /* just wake up the next guy waiting since
376 * we may have not consumed a slot after all */
377 dprintk("%s: No slot\n", __func__
);
381 spin_lock(&tbl
->slot_tbl_lock
);
382 nfs4_free_slot(tbl
, res
->sr_slotid
);
383 nfs41_check_drain_session_complete(clp
->cl_session
);
384 spin_unlock(&tbl
->slot_tbl_lock
);
385 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
388 static void nfs41_sequence_done(struct nfs_client
*clp
,
389 struct nfs4_sequence_res
*res
,
392 unsigned long timestamp
;
393 struct nfs4_slot_table
*tbl
;
394 struct nfs4_slot
*slot
;
397 * sr_status remains 1 if an RPC level error occurred. The server
398 * may or may not have processed the sequence operation..
399 * Proceed as if the server received and processed the sequence
402 if (res
->sr_status
== 1)
403 res
->sr_status
= NFS_OK
;
405 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
406 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
409 /* Check the SEQUENCE operation status */
410 if (res
->sr_status
== 0) {
411 tbl
= &clp
->cl_session
->fc_slot_table
;
412 slot
= tbl
->slots
+ res
->sr_slotid
;
413 /* Update the slot's sequence and clientid lease timer */
415 timestamp
= res
->sr_renewal_time
;
416 spin_lock(&clp
->cl_lock
);
417 if (time_before(clp
->cl_last_renewal
, timestamp
))
418 clp
->cl_last_renewal
= timestamp
;
419 spin_unlock(&clp
->cl_lock
);
420 /* Check sequence flags */
421 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
424 /* The session may be reset by one of the error handlers. */
425 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
426 nfs41_sequence_free_slot(clp
, res
);
430 * nfs4_find_slot - efficiently look for a free slot
432 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
433 * If found, we mark the slot as used, update the highest_used_slotid,
434 * and respectively set up the sequence operation args.
435 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
437 * Note: must be called with under the slot_tbl_lock.
440 nfs4_find_slot(struct nfs4_slot_table
*tbl
)
443 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
444 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
446 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
447 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
449 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
450 if (slotid
>= tbl
->max_slots
)
452 __set_bit(slotid
, tbl
->used_slots
);
453 if (slotid
> tbl
->highest_used_slotid
)
454 tbl
->highest_used_slotid
= slotid
;
457 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
458 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
462 static int nfs41_setup_sequence(struct nfs4_session
*session
,
463 struct nfs4_sequence_args
*args
,
464 struct nfs4_sequence_res
*res
,
466 struct rpc_task
*task
)
468 struct nfs4_slot
*slot
;
469 struct nfs4_slot_table
*tbl
;
472 dprintk("--> %s\n", __func__
);
473 /* slot already allocated? */
474 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
477 memset(res
, 0, sizeof(*res
));
478 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
479 tbl
= &session
->fc_slot_table
;
481 spin_lock(&tbl
->slot_tbl_lock
);
482 if (test_bit(NFS4CLNT_SESSION_DRAINING
, &session
->clp
->cl_state
) &&
483 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
485 * The state manager will wait until the slot table is empty.
486 * Schedule the reset thread
488 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
489 spin_unlock(&tbl
->slot_tbl_lock
);
490 dprintk("%s Schedule Session Reset\n", __func__
);
494 if (!rpc_queue_empty(&tbl
->slot_tbl_waitq
) &&
495 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
496 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
497 spin_unlock(&tbl
->slot_tbl_lock
);
498 dprintk("%s enforce FIFO order\n", __func__
);
502 slotid
= nfs4_find_slot(tbl
);
503 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
504 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
505 spin_unlock(&tbl
->slot_tbl_lock
);
506 dprintk("<-- %s: no free slots\n", __func__
);
509 spin_unlock(&tbl
->slot_tbl_lock
);
511 rpc_task_set_priority(task
, RPC_PRIORITY_NORMAL
);
512 slot
= tbl
->slots
+ slotid
;
513 args
->sa_session
= session
;
514 args
->sa_slotid
= slotid
;
515 args
->sa_cache_this
= cache_reply
;
517 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
519 res
->sr_session
= session
;
520 res
->sr_slotid
= slotid
;
521 res
->sr_renewal_time
= jiffies
;
523 * sr_status is only set in decode_sequence, and so will remain
524 * set to 1 if an rpc level failure occurs.
530 int nfs4_setup_sequence(struct nfs_client
*clp
,
531 struct nfs4_sequence_args
*args
,
532 struct nfs4_sequence_res
*res
,
534 struct rpc_task
*task
)
538 dprintk("--> %s clp %p session %p sr_slotid %d\n",
539 __func__
, clp
, clp
->cl_session
, res
->sr_slotid
);
541 if (!nfs4_has_session(clp
))
543 ret
= nfs41_setup_sequence(clp
->cl_session
, args
, res
, cache_reply
,
545 if (ret
&& ret
!= -EAGAIN
) {
546 /* terminate rpc task */
547 task
->tk_status
= ret
;
548 task
->tk_action
= NULL
;
551 dprintk("<-- %s status=%d\n", __func__
, ret
);
555 struct nfs41_call_sync_data
{
556 struct nfs_client
*clp
;
557 struct nfs4_sequence_args
*seq_args
;
558 struct nfs4_sequence_res
*seq_res
;
562 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
564 struct nfs41_call_sync_data
*data
= calldata
;
566 dprintk("--> %s data->clp->cl_session %p\n", __func__
,
567 data
->clp
->cl_session
);
568 if (nfs4_setup_sequence(data
->clp
, data
->seq_args
,
569 data
->seq_res
, data
->cache_reply
, task
))
571 rpc_call_start(task
);
574 static void nfs41_call_priv_sync_prepare(struct rpc_task
*task
, void *calldata
)
576 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
577 nfs41_call_sync_prepare(task
, calldata
);
580 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
582 struct nfs41_call_sync_data
*data
= calldata
;
584 nfs41_sequence_done(data
->clp
, data
->seq_res
, task
->tk_status
);
587 struct rpc_call_ops nfs41_call_sync_ops
= {
588 .rpc_call_prepare
= nfs41_call_sync_prepare
,
589 .rpc_call_done
= nfs41_call_sync_done
,
592 struct rpc_call_ops nfs41_call_priv_sync_ops
= {
593 .rpc_call_prepare
= nfs41_call_priv_sync_prepare
,
594 .rpc_call_done
= nfs41_call_sync_done
,
597 static int nfs4_call_sync_sequence(struct nfs_client
*clp
,
598 struct rpc_clnt
*clnt
,
599 struct rpc_message
*msg
,
600 struct nfs4_sequence_args
*args
,
601 struct nfs4_sequence_res
*res
,
606 struct rpc_task
*task
;
607 struct nfs41_call_sync_data data
= {
611 .cache_reply
= cache_reply
,
613 struct rpc_task_setup task_setup
= {
616 .callback_ops
= &nfs41_call_sync_ops
,
617 .callback_data
= &data
620 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
622 task_setup
.callback_ops
= &nfs41_call_priv_sync_ops
;
623 task
= rpc_run_task(&task_setup
);
627 ret
= task
->tk_status
;
633 int _nfs4_call_sync_session(struct nfs_server
*server
,
634 struct rpc_message
*msg
,
635 struct nfs4_sequence_args
*args
,
636 struct nfs4_sequence_res
*res
,
639 return nfs4_call_sync_sequence(server
->nfs_client
, server
->client
,
640 msg
, args
, res
, cache_reply
, 0);
643 #endif /* CONFIG_NFS_V4_1 */
645 int _nfs4_call_sync(struct nfs_server
*server
,
646 struct rpc_message
*msg
,
647 struct nfs4_sequence_args
*args
,
648 struct nfs4_sequence_res
*res
,
651 args
->sa_session
= res
->sr_session
= NULL
;
652 return rpc_call_sync(server
->client
, msg
, 0);
655 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
656 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
657 &(res)->seq_res, (cache_reply))
659 static void nfs4_sequence_done(const struct nfs_server
*server
,
660 struct nfs4_sequence_res
*res
, int rpc_status
)
662 #ifdef CONFIG_NFS_V4_1
663 if (nfs4_has_session(server
->nfs_client
))
664 nfs41_sequence_done(server
->nfs_client
, res
, rpc_status
);
665 #endif /* CONFIG_NFS_V4_1 */
668 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
670 struct nfs_inode
*nfsi
= NFS_I(dir
);
672 spin_lock(&dir
->i_lock
);
673 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
674 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
675 nfs_force_lookup_revalidate(dir
);
676 nfsi
->change_attr
= cinfo
->after
;
677 spin_unlock(&dir
->i_lock
);
680 struct nfs4_opendata
{
682 struct nfs_openargs o_arg
;
683 struct nfs_openres o_res
;
684 struct nfs_open_confirmargs c_arg
;
685 struct nfs_open_confirmres c_res
;
686 struct nfs_fattr f_attr
;
687 struct nfs_fattr dir_attr
;
690 struct nfs4_state_owner
*owner
;
691 struct nfs4_state
*state
;
693 unsigned long timestamp
;
694 unsigned int rpc_done
: 1;
700 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
702 p
->o_res
.f_attr
= &p
->f_attr
;
703 p
->o_res
.dir_attr
= &p
->dir_attr
;
704 p
->o_res
.seqid
= p
->o_arg
.seqid
;
705 p
->c_res
.seqid
= p
->c_arg
.seqid
;
706 p
->o_res
.server
= p
->o_arg
.server
;
707 nfs_fattr_init(&p
->f_attr
);
708 nfs_fattr_init(&p
->dir_attr
);
709 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
712 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
713 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
714 const struct iattr
*attrs
)
716 struct dentry
*parent
= dget_parent(path
->dentry
);
717 struct inode
*dir
= parent
->d_inode
;
718 struct nfs_server
*server
= NFS_SERVER(dir
);
719 struct nfs4_opendata
*p
;
721 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
724 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
725 if (p
->o_arg
.seqid
== NULL
)
727 p
->path
.mnt
= mntget(path
->mnt
);
728 p
->path
.dentry
= dget(path
->dentry
);
731 atomic_inc(&sp
->so_count
);
732 p
->o_arg
.fh
= NFS_FH(dir
);
733 p
->o_arg
.open_flags
= flags
;
734 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
735 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
736 p
->o_arg
.id
= sp
->so_owner_id
.id
;
737 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
738 p
->o_arg
.server
= server
;
739 p
->o_arg
.bitmask
= server
->attr_bitmask
;
740 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
741 if (flags
& O_EXCL
) {
742 if (nfs4_has_persistent_session(server
->nfs_client
)) {
744 p
->o_arg
.u
.attrs
= &p
->attrs
;
745 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
746 } else { /* EXCLUSIVE4_1 */
747 u32
*s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
751 } else if (flags
& O_CREAT
) {
752 p
->o_arg
.u
.attrs
= &p
->attrs
;
753 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
755 p
->c_arg
.fh
= &p
->o_res
.fh
;
756 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
757 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
758 nfs4_init_opendata_res(p
);
768 static void nfs4_opendata_free(struct kref
*kref
)
770 struct nfs4_opendata
*p
= container_of(kref
,
771 struct nfs4_opendata
, kref
);
773 nfs_free_seqid(p
->o_arg
.seqid
);
774 if (p
->state
!= NULL
)
775 nfs4_put_open_state(p
->state
);
776 nfs4_put_state_owner(p
->owner
);
782 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
785 kref_put(&p
->kref
, nfs4_opendata_free
);
788 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
792 ret
= rpc_wait_for_completion_task(task
);
796 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
800 if (open_mode
& O_EXCL
)
802 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
804 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
805 && state
->n_rdonly
!= 0;
808 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
809 && state
->n_wronly
!= 0;
811 case FMODE_READ
|FMODE_WRITE
:
812 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
813 && state
->n_rdwr
!= 0;
819 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
821 if ((delegation
->type
& fmode
) != fmode
)
823 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
825 nfs_mark_delegation_referenced(delegation
);
829 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
838 case FMODE_READ
|FMODE_WRITE
:
841 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
844 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
846 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
847 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
848 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
851 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
854 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
856 case FMODE_READ
|FMODE_WRITE
:
857 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
861 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
863 write_seqlock(&state
->seqlock
);
864 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
865 write_sequnlock(&state
->seqlock
);
868 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
871 * Protect the call to nfs4_state_set_mode_locked and
872 * serialise the stateid update
874 write_seqlock(&state
->seqlock
);
875 if (deleg_stateid
!= NULL
) {
876 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
877 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
879 if (open_stateid
!= NULL
)
880 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
881 write_sequnlock(&state
->seqlock
);
882 spin_lock(&state
->owner
->so_lock
);
883 update_open_stateflags(state
, fmode
);
884 spin_unlock(&state
->owner
->so_lock
);
887 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
889 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
890 struct nfs_delegation
*deleg_cur
;
893 fmode
&= (FMODE_READ
|FMODE_WRITE
);
896 deleg_cur
= rcu_dereference(nfsi
->delegation
);
897 if (deleg_cur
== NULL
)
900 spin_lock(&deleg_cur
->lock
);
901 if (nfsi
->delegation
!= deleg_cur
||
902 (deleg_cur
->type
& fmode
) != fmode
)
903 goto no_delegation_unlock
;
905 if (delegation
== NULL
)
906 delegation
= &deleg_cur
->stateid
;
907 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
908 goto no_delegation_unlock
;
910 nfs_mark_delegation_referenced(deleg_cur
);
911 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
913 no_delegation_unlock
:
914 spin_unlock(&deleg_cur
->lock
);
918 if (!ret
&& open_stateid
!= NULL
) {
919 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
927 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
929 struct nfs_delegation
*delegation
;
932 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
933 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
938 nfs_inode_return_delegation(inode
);
941 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
943 struct nfs4_state
*state
= opendata
->state
;
944 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
945 struct nfs_delegation
*delegation
;
946 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
947 fmode_t fmode
= opendata
->o_arg
.fmode
;
948 nfs4_stateid stateid
;
952 if (can_open_cached(state
, fmode
, open_mode
)) {
953 spin_lock(&state
->owner
->so_lock
);
954 if (can_open_cached(state
, fmode
, open_mode
)) {
955 update_open_stateflags(state
, fmode
);
956 spin_unlock(&state
->owner
->so_lock
);
957 goto out_return_state
;
959 spin_unlock(&state
->owner
->so_lock
);
962 delegation
= rcu_dereference(nfsi
->delegation
);
963 if (delegation
== NULL
||
964 !can_open_delegated(delegation
, fmode
)) {
968 /* Save the delegation */
969 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
971 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
976 /* Try to update the stateid using the delegation */
977 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
978 goto out_return_state
;
983 atomic_inc(&state
->count
);
987 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
990 struct nfs4_state
*state
= NULL
;
991 struct nfs_delegation
*delegation
;
994 if (!data
->rpc_done
) {
995 state
= nfs4_try_open_cached(data
);
1000 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1002 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
1003 ret
= PTR_ERR(inode
);
1007 state
= nfs4_get_open_state(inode
, data
->owner
);
1010 if (data
->o_res
.delegation_type
!= 0) {
1011 int delegation_flags
= 0;
1014 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1016 delegation_flags
= delegation
->flags
;
1018 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1019 nfs_inode_set_delegation(state
->inode
,
1020 data
->owner
->so_cred
,
1023 nfs_inode_reclaim_delegation(state
->inode
,
1024 data
->owner
->so_cred
,
1028 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1036 return ERR_PTR(ret
);
1039 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1041 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1042 struct nfs_open_context
*ctx
;
1044 spin_lock(&state
->inode
->i_lock
);
1045 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1046 if (ctx
->state
!= state
)
1048 get_nfs_open_context(ctx
);
1049 spin_unlock(&state
->inode
->i_lock
);
1052 spin_unlock(&state
->inode
->i_lock
);
1053 return ERR_PTR(-ENOENT
);
1056 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1058 struct nfs4_opendata
*opendata
;
1060 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
);
1061 if (opendata
== NULL
)
1062 return ERR_PTR(-ENOMEM
);
1063 opendata
->state
= state
;
1064 atomic_inc(&state
->count
);
1068 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1070 struct nfs4_state
*newstate
;
1073 opendata
->o_arg
.open_flags
= 0;
1074 opendata
->o_arg
.fmode
= fmode
;
1075 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1076 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1077 nfs4_init_opendata_res(opendata
);
1078 ret
= _nfs4_recover_proc_open(opendata
);
1081 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1082 if (IS_ERR(newstate
))
1083 return PTR_ERR(newstate
);
1084 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1089 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1091 struct nfs4_state
*newstate
;
1094 /* memory barrier prior to reading state->n_* */
1095 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1097 if (state
->n_rdwr
!= 0) {
1098 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1101 if (newstate
!= state
)
1104 if (state
->n_wronly
!= 0) {
1105 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1108 if (newstate
!= state
)
1111 if (state
->n_rdonly
!= 0) {
1112 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1115 if (newstate
!= state
)
1119 * We may have performed cached opens for all three recoveries.
1120 * Check if we need to update the current stateid.
1122 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1123 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1124 write_seqlock(&state
->seqlock
);
1125 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1126 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1127 write_sequnlock(&state
->seqlock
);
1134 * reclaim state on the server after a reboot.
1136 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1138 struct nfs_delegation
*delegation
;
1139 struct nfs4_opendata
*opendata
;
1140 fmode_t delegation_type
= 0;
1143 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1144 if (IS_ERR(opendata
))
1145 return PTR_ERR(opendata
);
1146 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1147 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1149 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1150 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1151 delegation_type
= delegation
->type
;
1153 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1154 status
= nfs4_open_recover(opendata
, state
);
1155 nfs4_opendata_put(opendata
);
1159 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1161 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1162 struct nfs4_exception exception
= { };
1165 err
= _nfs4_do_open_reclaim(ctx
, state
);
1166 if (err
!= -NFS4ERR_DELAY
)
1168 nfs4_handle_exception(server
, err
, &exception
);
1169 } while (exception
.retry
);
1173 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1175 struct nfs_open_context
*ctx
;
1178 ctx
= nfs4_state_find_open_context(state
);
1180 return PTR_ERR(ctx
);
1181 ret
= nfs4_do_open_reclaim(ctx
, state
);
1182 put_nfs_open_context(ctx
);
1186 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1188 struct nfs4_opendata
*opendata
;
1191 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1192 if (IS_ERR(opendata
))
1193 return PTR_ERR(opendata
);
1194 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1195 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1196 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1197 ret
= nfs4_open_recover(opendata
, state
);
1198 nfs4_opendata_put(opendata
);
1202 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1204 struct nfs4_exception exception
= { };
1205 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1208 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1214 case -NFS4ERR_BADSESSION
:
1215 case -NFS4ERR_BADSLOT
:
1216 case -NFS4ERR_BAD_HIGH_SLOT
:
1217 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1218 case -NFS4ERR_DEADSESSION
:
1219 nfs4_schedule_state_recovery(
1220 server
->nfs_client
);
1222 case -NFS4ERR_STALE_CLIENTID
:
1223 case -NFS4ERR_STALE_STATEID
:
1224 case -NFS4ERR_EXPIRED
:
1225 /* Don't recall a delegation if it was lost */
1226 nfs4_schedule_state_recovery(server
->nfs_client
);
1230 * The show must go on: exit, but mark the
1231 * stateid as needing recovery.
1233 case -NFS4ERR_ADMIN_REVOKED
:
1234 case -NFS4ERR_BAD_STATEID
:
1235 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
1240 err
= nfs4_handle_exception(server
, err
, &exception
);
1241 } while (exception
.retry
);
1246 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1248 struct nfs4_opendata
*data
= calldata
;
1250 data
->rpc_status
= task
->tk_status
;
1251 if (RPC_ASSASSINATED(task
))
1253 if (data
->rpc_status
== 0) {
1254 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1255 sizeof(data
->o_res
.stateid
.data
));
1256 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1257 renew_lease(data
->o_res
.server
, data
->timestamp
);
1262 static void nfs4_open_confirm_release(void *calldata
)
1264 struct nfs4_opendata
*data
= calldata
;
1265 struct nfs4_state
*state
= NULL
;
1267 /* If this request hasn't been cancelled, do nothing */
1268 if (data
->cancelled
== 0)
1270 /* In case of error, no cleanup! */
1271 if (!data
->rpc_done
)
1273 state
= nfs4_opendata_to_nfs4_state(data
);
1275 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1277 nfs4_opendata_put(data
);
1280 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1281 .rpc_call_done
= nfs4_open_confirm_done
,
1282 .rpc_release
= nfs4_open_confirm_release
,
1286 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1288 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1290 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1291 struct rpc_task
*task
;
1292 struct rpc_message msg
= {
1293 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1294 .rpc_argp
= &data
->c_arg
,
1295 .rpc_resp
= &data
->c_res
,
1296 .rpc_cred
= data
->owner
->so_cred
,
1298 struct rpc_task_setup task_setup_data
= {
1299 .rpc_client
= server
->client
,
1300 .rpc_message
= &msg
,
1301 .callback_ops
= &nfs4_open_confirm_ops
,
1302 .callback_data
= data
,
1303 .workqueue
= nfsiod_workqueue
,
1304 .flags
= RPC_TASK_ASYNC
,
1308 kref_get(&data
->kref
);
1310 data
->rpc_status
= 0;
1311 data
->timestamp
= jiffies
;
1312 task
= rpc_run_task(&task_setup_data
);
1314 return PTR_ERR(task
);
1315 status
= nfs4_wait_for_completion_rpc_task(task
);
1317 data
->cancelled
= 1;
1320 status
= data
->rpc_status
;
1325 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1327 struct nfs4_opendata
*data
= calldata
;
1328 struct nfs4_state_owner
*sp
= data
->owner
;
1330 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1333 * Check if we still need to send an OPEN call, or if we can use
1334 * a delegation instead.
1336 if (data
->state
!= NULL
) {
1337 struct nfs_delegation
*delegation
;
1339 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1342 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1343 if (delegation
!= NULL
&&
1344 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1350 /* Update sequence id. */
1351 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1352 data
->o_arg
.clientid
= sp
->so_client
->cl_clientid
;
1353 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1354 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1355 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1357 data
->timestamp
= jiffies
;
1358 if (nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
1359 &data
->o_arg
.seq_args
,
1360 &data
->o_res
.seq_res
, 1, task
))
1362 rpc_call_start(task
);
1365 task
->tk_action
= NULL
;
1369 static void nfs4_recover_open_prepare(struct rpc_task
*task
, void *calldata
)
1371 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
1372 nfs4_open_prepare(task
, calldata
);
1375 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1377 struct nfs4_opendata
*data
= calldata
;
1379 data
->rpc_status
= task
->tk_status
;
1381 nfs4_sequence_done(data
->o_arg
.server
, &data
->o_res
.seq_res
,
1384 if (RPC_ASSASSINATED(task
))
1386 if (task
->tk_status
== 0) {
1387 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1391 data
->rpc_status
= -ELOOP
;
1394 data
->rpc_status
= -EISDIR
;
1397 data
->rpc_status
= -ENOTDIR
;
1399 renew_lease(data
->o_res
.server
, data
->timestamp
);
1400 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1401 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1406 static void nfs4_open_release(void *calldata
)
1408 struct nfs4_opendata
*data
= calldata
;
1409 struct nfs4_state
*state
= NULL
;
1411 /* If this request hasn't been cancelled, do nothing */
1412 if (data
->cancelled
== 0)
1414 /* In case of error, no cleanup! */
1415 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1417 /* In case we need an open_confirm, no cleanup! */
1418 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1420 state
= nfs4_opendata_to_nfs4_state(data
);
1422 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1424 nfs4_opendata_put(data
);
1427 static const struct rpc_call_ops nfs4_open_ops
= {
1428 .rpc_call_prepare
= nfs4_open_prepare
,
1429 .rpc_call_done
= nfs4_open_done
,
1430 .rpc_release
= nfs4_open_release
,
1433 static const struct rpc_call_ops nfs4_recover_open_ops
= {
1434 .rpc_call_prepare
= nfs4_recover_open_prepare
,
1435 .rpc_call_done
= nfs4_open_done
,
1436 .rpc_release
= nfs4_open_release
,
1439 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1441 struct inode
*dir
= data
->dir
->d_inode
;
1442 struct nfs_server
*server
= NFS_SERVER(dir
);
1443 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1444 struct nfs_openres
*o_res
= &data
->o_res
;
1445 struct rpc_task
*task
;
1446 struct rpc_message msg
= {
1447 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1450 .rpc_cred
= data
->owner
->so_cred
,
1452 struct rpc_task_setup task_setup_data
= {
1453 .rpc_client
= server
->client
,
1454 .rpc_message
= &msg
,
1455 .callback_ops
= &nfs4_open_ops
,
1456 .callback_data
= data
,
1457 .workqueue
= nfsiod_workqueue
,
1458 .flags
= RPC_TASK_ASYNC
,
1462 kref_get(&data
->kref
);
1464 data
->rpc_status
= 0;
1465 data
->cancelled
= 0;
1467 task_setup_data
.callback_ops
= &nfs4_recover_open_ops
;
1468 task
= rpc_run_task(&task_setup_data
);
1470 return PTR_ERR(task
);
1471 status
= nfs4_wait_for_completion_rpc_task(task
);
1473 data
->cancelled
= 1;
1476 status
= data
->rpc_status
;
1482 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1484 struct inode
*dir
= data
->dir
->d_inode
;
1485 struct nfs_openres
*o_res
= &data
->o_res
;
1488 status
= nfs4_run_open_task(data
, 1);
1489 if (status
!= 0 || !data
->rpc_done
)
1492 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1494 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1495 status
= _nfs4_proc_open_confirm(data
);
1504 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1506 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1508 struct inode
*dir
= data
->dir
->d_inode
;
1509 struct nfs_server
*server
= NFS_SERVER(dir
);
1510 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1511 struct nfs_openres
*o_res
= &data
->o_res
;
1514 status
= nfs4_run_open_task(data
, 0);
1515 if (status
!= 0 || !data
->rpc_done
)
1518 if (o_arg
->open_flags
& O_CREAT
) {
1519 update_changeattr(dir
, &o_res
->cinfo
);
1520 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1522 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1523 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1524 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1525 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1526 status
= _nfs4_proc_open_confirm(data
);
1530 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1531 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1535 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1537 struct nfs_client
*clp
= server
->nfs_client
;
1541 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1542 ret
= nfs4_wait_clnt_recover(clp
);
1545 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1546 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1548 nfs4_schedule_state_recovery(clp
);
1556 * reclaim state on the server after a network partition.
1557 * Assumes caller holds the appropriate lock
1559 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1561 struct nfs4_opendata
*opendata
;
1564 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1565 if (IS_ERR(opendata
))
1566 return PTR_ERR(opendata
);
1567 ret
= nfs4_open_recover(opendata
, state
);
1569 d_drop(ctx
->path
.dentry
);
1570 nfs4_opendata_put(opendata
);
1574 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1576 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1577 struct nfs4_exception exception
= { };
1581 err
= _nfs4_open_expired(ctx
, state
);
1585 case -NFS4ERR_GRACE
:
1586 case -NFS4ERR_DELAY
:
1587 nfs4_handle_exception(server
, err
, &exception
);
1590 } while (exception
.retry
);
1595 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1597 struct nfs_open_context
*ctx
;
1600 ctx
= nfs4_state_find_open_context(state
);
1602 return PTR_ERR(ctx
);
1603 ret
= nfs4_do_open_expired(ctx
, state
);
1604 put_nfs_open_context(ctx
);
1609 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1610 * fields corresponding to attributes that were used to store the verifier.
1611 * Make sure we clobber those fields in the later setattr call
1613 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1615 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1616 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1617 sattr
->ia_valid
|= ATTR_ATIME
;
1619 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1620 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1621 sattr
->ia_valid
|= ATTR_MTIME
;
1625 * Returns a referenced nfs4_state
1627 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
)
1629 struct nfs4_state_owner
*sp
;
1630 struct nfs4_state
*state
= NULL
;
1631 struct nfs_server
*server
= NFS_SERVER(dir
);
1632 struct nfs4_opendata
*opendata
;
1635 /* Protect against reboot recovery conflicts */
1637 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1638 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1641 status
= nfs4_recover_expired_lease(server
);
1643 goto err_put_state_owner
;
1644 if (path
->dentry
->d_inode
!= NULL
)
1645 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1647 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
);
1648 if (opendata
== NULL
)
1649 goto err_put_state_owner
;
1651 if (path
->dentry
->d_inode
!= NULL
)
1652 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1654 status
= _nfs4_proc_open(opendata
);
1656 goto err_opendata_put
;
1658 if (opendata
->o_arg
.open_flags
& O_EXCL
)
1659 nfs4_exclusive_attrset(opendata
, sattr
);
1661 state
= nfs4_opendata_to_nfs4_state(opendata
);
1662 status
= PTR_ERR(state
);
1664 goto err_opendata_put
;
1665 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
1666 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1667 nfs4_opendata_put(opendata
);
1668 nfs4_put_state_owner(sp
);
1672 nfs4_opendata_put(opendata
);
1673 err_put_state_owner
:
1674 nfs4_put_state_owner(sp
);
1681 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
)
1683 struct nfs4_exception exception
= { };
1684 struct nfs4_state
*res
;
1688 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1691 /* NOTE: BAD_SEQID means the server and client disagree about the
1692 * book-keeping w.r.t. state-changing operations
1693 * (OPEN/CLOSE/LOCK/LOCKU...)
1694 * It is actually a sign of a bug on the client or on the server.
1696 * If we receive a BAD_SEQID error in the particular case of
1697 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1698 * have unhashed the old state_owner for us, and that we can
1699 * therefore safely retry using a new one. We should still warn
1700 * the user though...
1702 if (status
== -NFS4ERR_BAD_SEQID
) {
1703 printk(KERN_WARNING
"NFS: v4 server %s "
1704 " returned a bad sequence-id error!\n",
1705 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1706 exception
.retry
= 1;
1710 * BAD_STATEID on OPEN means that the server cancelled our
1711 * state before it received the OPEN_CONFIRM.
1712 * Recover by retrying the request as per the discussion
1713 * on Page 181 of RFC3530.
1715 if (status
== -NFS4ERR_BAD_STATEID
) {
1716 exception
.retry
= 1;
1719 if (status
== -EAGAIN
) {
1720 /* We must have found a delegation */
1721 exception
.retry
= 1;
1724 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1725 status
, &exception
));
1726 } while (exception
.retry
);
1730 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1731 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1732 struct nfs4_state
*state
)
1734 struct nfs_server
*server
= NFS_SERVER(inode
);
1735 struct nfs_setattrargs arg
= {
1736 .fh
= NFS_FH(inode
),
1739 .bitmask
= server
->attr_bitmask
,
1741 struct nfs_setattrres res
= {
1745 struct rpc_message msg
= {
1746 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1751 unsigned long timestamp
= jiffies
;
1754 nfs_fattr_init(fattr
);
1756 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1757 /* Use that stateid */
1758 } else if (state
!= NULL
) {
1759 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1761 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1763 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1764 if (status
== 0 && state
!= NULL
)
1765 renew_lease(server
, timestamp
);
1769 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1770 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1771 struct nfs4_state
*state
)
1773 struct nfs_server
*server
= NFS_SERVER(inode
);
1774 struct nfs4_exception exception
= { };
1777 err
= nfs4_handle_exception(server
,
1778 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1780 } while (exception
.retry
);
1784 struct nfs4_closedata
{
1786 struct inode
*inode
;
1787 struct nfs4_state
*state
;
1788 struct nfs_closeargs arg
;
1789 struct nfs_closeres res
;
1790 struct nfs_fattr fattr
;
1791 unsigned long timestamp
;
1794 static void nfs4_free_closedata(void *data
)
1796 struct nfs4_closedata
*calldata
= data
;
1797 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1799 nfs4_put_open_state(calldata
->state
);
1800 nfs_free_seqid(calldata
->arg
.seqid
);
1801 nfs4_put_state_owner(sp
);
1802 path_put(&calldata
->path
);
1806 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
1809 spin_lock(&state
->owner
->so_lock
);
1810 if (!(fmode
& FMODE_READ
))
1811 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1812 if (!(fmode
& FMODE_WRITE
))
1813 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1814 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1815 spin_unlock(&state
->owner
->so_lock
);
1818 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1820 struct nfs4_closedata
*calldata
= data
;
1821 struct nfs4_state
*state
= calldata
->state
;
1822 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1824 nfs4_sequence_done(server
, &calldata
->res
.seq_res
, task
->tk_status
);
1825 if (RPC_ASSASSINATED(task
))
1827 /* hmm. we are done with the inode, and in the process of freeing
1828 * the state_owner. we keep this around to process errors
1830 switch (task
->tk_status
) {
1832 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1833 renew_lease(server
, calldata
->timestamp
);
1834 nfs4_close_clear_stateid_flags(state
,
1835 calldata
->arg
.fmode
);
1837 case -NFS4ERR_STALE_STATEID
:
1838 case -NFS4ERR_OLD_STATEID
:
1839 case -NFS4ERR_BAD_STATEID
:
1840 case -NFS4ERR_EXPIRED
:
1841 if (calldata
->arg
.fmode
== 0)
1844 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
1845 rpc_restart_call_prepare(task
);
1847 nfs_release_seqid(calldata
->arg
.seqid
);
1848 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1851 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1853 struct nfs4_closedata
*calldata
= data
;
1854 struct nfs4_state
*state
= calldata
->state
;
1857 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1860 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1861 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
1862 spin_lock(&state
->owner
->so_lock
);
1863 /* Calculate the change in open mode */
1864 if (state
->n_rdwr
== 0) {
1865 if (state
->n_rdonly
== 0) {
1866 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1867 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1868 calldata
->arg
.fmode
&= ~FMODE_READ
;
1870 if (state
->n_wronly
== 0) {
1871 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1872 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1873 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
1876 spin_unlock(&state
->owner
->so_lock
);
1879 /* Note: exit _without_ calling nfs4_close_done */
1880 task
->tk_action
= NULL
;
1884 if (calldata
->arg
.fmode
== 0)
1885 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
1887 nfs_fattr_init(calldata
->res
.fattr
);
1888 calldata
->timestamp
= jiffies
;
1889 if (nfs4_setup_sequence((NFS_SERVER(calldata
->inode
))->nfs_client
,
1890 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1893 rpc_call_start(task
);
1896 static const struct rpc_call_ops nfs4_close_ops
= {
1897 .rpc_call_prepare
= nfs4_close_prepare
,
1898 .rpc_call_done
= nfs4_close_done
,
1899 .rpc_release
= nfs4_free_closedata
,
1903 * It is possible for data to be read/written from a mem-mapped file
1904 * after the sys_close call (which hits the vfs layer as a flush).
1905 * This means that we can't safely call nfsv4 close on a file until
1906 * the inode is cleared. This in turn means that we are not good
1907 * NFSv4 citizens - we do not indicate to the server to update the file's
1908 * share state even when we are done with one of the three share
1909 * stateid's in the inode.
1911 * NOTE: Caller must be holding the sp->so_owner semaphore!
1913 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, int wait
)
1915 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1916 struct nfs4_closedata
*calldata
;
1917 struct nfs4_state_owner
*sp
= state
->owner
;
1918 struct rpc_task
*task
;
1919 struct rpc_message msg
= {
1920 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1921 .rpc_cred
= state
->owner
->so_cred
,
1923 struct rpc_task_setup task_setup_data
= {
1924 .rpc_client
= server
->client
,
1925 .rpc_message
= &msg
,
1926 .callback_ops
= &nfs4_close_ops
,
1927 .workqueue
= nfsiod_workqueue
,
1928 .flags
= RPC_TASK_ASYNC
,
1930 int status
= -ENOMEM
;
1932 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
1933 if (calldata
== NULL
)
1935 calldata
->inode
= state
->inode
;
1936 calldata
->state
= state
;
1937 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1938 calldata
->arg
.stateid
= &state
->open_stateid
;
1939 /* Serialization for the sequence id */
1940 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
1941 if (calldata
->arg
.seqid
== NULL
)
1942 goto out_free_calldata
;
1943 calldata
->arg
.fmode
= 0;
1944 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1945 calldata
->res
.fattr
= &calldata
->fattr
;
1946 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1947 calldata
->res
.server
= server
;
1948 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1949 calldata
->path
.mnt
= mntget(path
->mnt
);
1950 calldata
->path
.dentry
= dget(path
->dentry
);
1952 msg
.rpc_argp
= &calldata
->arg
,
1953 msg
.rpc_resp
= &calldata
->res
,
1954 task_setup_data
.callback_data
= calldata
;
1955 task
= rpc_run_task(&task_setup_data
);
1957 return PTR_ERR(task
);
1960 status
= rpc_wait_for_completion_task(task
);
1966 nfs4_put_open_state(state
);
1967 nfs4_put_state_owner(sp
);
1971 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1976 /* If the open_intent is for execute, we have an extra check to make */
1977 if (fmode
& FMODE_EXEC
) {
1978 ret
= nfs_may_open(state
->inode
,
1979 state
->owner
->so_cred
,
1980 nd
->intent
.open
.flags
);
1984 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1985 if (!IS_ERR(filp
)) {
1986 struct nfs_open_context
*ctx
;
1987 ctx
= nfs_file_open_context(filp
);
1991 ret
= PTR_ERR(filp
);
1993 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
1998 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
2000 struct path path
= {
2001 .mnt
= nd
->path
.mnt
,
2004 struct dentry
*parent
;
2006 struct rpc_cred
*cred
;
2007 struct nfs4_state
*state
;
2009 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
2011 if (nd
->flags
& LOOKUP_CREATE
) {
2012 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
2013 attr
.ia_valid
= ATTR_MODE
;
2014 if (!IS_POSIXACL(dir
))
2015 attr
.ia_mode
&= ~current_umask();
2018 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
2021 cred
= rpc_lookup_cred();
2023 return (struct dentry
*)cred
;
2024 parent
= dentry
->d_parent
;
2025 /* Protect against concurrent sillydeletes */
2026 nfs_block_sillyrename(parent
);
2027 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
2029 if (IS_ERR(state
)) {
2030 if (PTR_ERR(state
) == -ENOENT
) {
2031 d_add(dentry
, NULL
);
2032 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2034 nfs_unblock_sillyrename(parent
);
2035 return (struct dentry
*)state
;
2037 res
= d_add_unique(dentry
, igrab(state
->inode
));
2040 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
2041 nfs_unblock_sillyrename(parent
);
2042 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2047 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
2049 struct path path
= {
2050 .mnt
= nd
->path
.mnt
,
2053 struct rpc_cred
*cred
;
2054 struct nfs4_state
*state
;
2055 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
2057 cred
= rpc_lookup_cred();
2059 return PTR_ERR(cred
);
2060 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
2062 if (IS_ERR(state
)) {
2063 switch (PTR_ERR(state
)) {
2069 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
2075 if (state
->inode
== dentry
->d_inode
) {
2076 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2077 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2080 nfs4_close_sync(&path
, state
, fmode
);
2086 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2088 if (ctx
->state
== NULL
)
2091 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2093 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2096 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2098 struct nfs4_server_caps_arg args
= {
2101 struct nfs4_server_caps_res res
= {};
2102 struct rpc_message msg
= {
2103 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2109 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2111 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2112 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2113 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2114 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2115 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2116 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2117 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2118 server
->caps
|= NFS_CAP_ACLS
;
2119 if (res
.has_links
!= 0)
2120 server
->caps
|= NFS_CAP_HARDLINKS
;
2121 if (res
.has_symlinks
!= 0)
2122 server
->caps
|= NFS_CAP_SYMLINKS
;
2123 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2124 server
->caps
|= NFS_CAP_FILEID
;
2125 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2126 server
->caps
|= NFS_CAP_MODE
;
2127 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2128 server
->caps
|= NFS_CAP_NLINK
;
2129 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2130 server
->caps
|= NFS_CAP_OWNER
;
2131 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2132 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2133 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2134 server
->caps
|= NFS_CAP_ATIME
;
2135 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2136 server
->caps
|= NFS_CAP_CTIME
;
2137 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2138 server
->caps
|= NFS_CAP_MTIME
;
2140 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2141 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2142 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2143 server
->acl_bitmask
= res
.acl_bitmask
;
2149 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2151 struct nfs4_exception exception
= { };
2154 err
= nfs4_handle_exception(server
,
2155 _nfs4_server_capabilities(server
, fhandle
),
2157 } while (exception
.retry
);
2161 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2162 struct nfs_fsinfo
*info
)
2164 struct nfs4_lookup_root_arg args
= {
2165 .bitmask
= nfs4_fattr_bitmap
,
2167 struct nfs4_lookup_res res
= {
2169 .fattr
= info
->fattr
,
2172 struct rpc_message msg
= {
2173 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2178 nfs_fattr_init(info
->fattr
);
2179 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2182 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2183 struct nfs_fsinfo
*info
)
2185 struct nfs4_exception exception
= { };
2188 err
= nfs4_handle_exception(server
,
2189 _nfs4_lookup_root(server
, fhandle
, info
),
2191 } while (exception
.retry
);
2196 * get the file handle for the "/" directory on the server
2198 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2199 struct nfs_fsinfo
*info
)
2203 status
= nfs4_lookup_root(server
, fhandle
, info
);
2205 status
= nfs4_server_capabilities(server
, fhandle
);
2207 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2208 return nfs4_map_errors(status
);
2212 * Get locations and (maybe) other attributes of a referral.
2213 * Note that we'll actually follow the referral later when
2214 * we detect fsid mismatch in inode revalidation
2216 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2218 int status
= -ENOMEM
;
2219 struct page
*page
= NULL
;
2220 struct nfs4_fs_locations
*locations
= NULL
;
2222 page
= alloc_page(GFP_KERNEL
);
2225 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2226 if (locations
== NULL
)
2229 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2232 /* Make sure server returned a different fsid for the referral */
2233 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2234 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2239 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2240 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2242 fattr
->mode
= S_IFDIR
;
2243 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2252 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2254 struct nfs4_getattr_arg args
= {
2256 .bitmask
= server
->attr_bitmask
,
2258 struct nfs4_getattr_res res
= {
2262 struct rpc_message msg
= {
2263 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2268 nfs_fattr_init(fattr
);
2269 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2272 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2274 struct nfs4_exception exception
= { };
2277 err
= nfs4_handle_exception(server
,
2278 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2280 } while (exception
.retry
);
2285 * The file is not closed if it is opened due to the a request to change
2286 * the size of the file. The open call will not be needed once the
2287 * VFS layer lookup-intents are implemented.
2289 * Close is called when the inode is destroyed.
2290 * If we haven't opened the file for O_WRONLY, we
2291 * need to in the size_change case to obtain a stateid.
2294 * Because OPEN is always done by name in nfsv4, it is
2295 * possible that we opened a different file by the same
2296 * name. We can recognize this race condition, but we
2297 * can't do anything about it besides returning an error.
2299 * This will be fixed with VFS changes (lookup-intent).
2302 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2303 struct iattr
*sattr
)
2305 struct inode
*inode
= dentry
->d_inode
;
2306 struct rpc_cred
*cred
= NULL
;
2307 struct nfs4_state
*state
= NULL
;
2310 nfs_fattr_init(fattr
);
2312 /* Search for an existing open(O_WRITE) file */
2313 if (sattr
->ia_valid
& ATTR_FILE
) {
2314 struct nfs_open_context
*ctx
;
2316 ctx
= nfs_file_open_context(sattr
->ia_file
);
2323 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2325 nfs_setattr_update_inode(inode
, sattr
);
2329 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2330 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2331 struct nfs_fattr
*fattr
)
2334 struct nfs4_lookup_arg args
= {
2335 .bitmask
= server
->attr_bitmask
,
2339 struct nfs4_lookup_res res
= {
2344 struct rpc_message msg
= {
2345 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2350 nfs_fattr_init(fattr
);
2352 dprintk("NFS call lookupfh %s\n", name
->name
);
2353 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2354 dprintk("NFS reply lookupfh: %d\n", status
);
2358 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2359 struct qstr
*name
, struct nfs_fh
*fhandle
,
2360 struct nfs_fattr
*fattr
)
2362 struct nfs4_exception exception
= { };
2365 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2367 if (err
== -NFS4ERR_MOVED
) {
2371 err
= nfs4_handle_exception(server
, err
, &exception
);
2372 } while (exception
.retry
);
2376 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2377 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2381 dprintk("NFS call lookup %s\n", name
->name
);
2382 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2383 if (status
== -NFS4ERR_MOVED
)
2384 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2385 dprintk("NFS reply lookup: %d\n", status
);
2389 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2391 struct nfs4_exception exception
= { };
2394 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2395 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2397 } while (exception
.retry
);
2401 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2403 struct nfs_server
*server
= NFS_SERVER(inode
);
2404 struct nfs_fattr fattr
;
2405 struct nfs4_accessargs args
= {
2406 .fh
= NFS_FH(inode
),
2407 .bitmask
= server
->attr_bitmask
,
2409 struct nfs4_accessres res
= {
2413 struct rpc_message msg
= {
2414 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2417 .rpc_cred
= entry
->cred
,
2419 int mode
= entry
->mask
;
2423 * Determine which access bits we want to ask for...
2425 if (mode
& MAY_READ
)
2426 args
.access
|= NFS4_ACCESS_READ
;
2427 if (S_ISDIR(inode
->i_mode
)) {
2428 if (mode
& MAY_WRITE
)
2429 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2430 if (mode
& MAY_EXEC
)
2431 args
.access
|= NFS4_ACCESS_LOOKUP
;
2433 if (mode
& MAY_WRITE
)
2434 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2435 if (mode
& MAY_EXEC
)
2436 args
.access
|= NFS4_ACCESS_EXECUTE
;
2438 nfs_fattr_init(&fattr
);
2439 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2442 if (res
.access
& NFS4_ACCESS_READ
)
2443 entry
->mask
|= MAY_READ
;
2444 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2445 entry
->mask
|= MAY_WRITE
;
2446 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2447 entry
->mask
|= MAY_EXEC
;
2448 nfs_refresh_inode(inode
, &fattr
);
2453 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2455 struct nfs4_exception exception
= { };
2458 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2459 _nfs4_proc_access(inode
, entry
),
2461 } while (exception
.retry
);
2466 * TODO: For the time being, we don't try to get any attributes
2467 * along with any of the zero-copy operations READ, READDIR,
2470 * In the case of the first three, we want to put the GETATTR
2471 * after the read-type operation -- this is because it is hard
2472 * to predict the length of a GETATTR response in v4, and thus
2473 * align the READ data correctly. This means that the GETATTR
2474 * may end up partially falling into the page cache, and we should
2475 * shift it into the 'tail' of the xdr_buf before processing.
2476 * To do this efficiently, we need to know the total length
2477 * of data received, which doesn't seem to be available outside
2480 * In the case of WRITE, we also want to put the GETATTR after
2481 * the operation -- in this case because we want to make sure
2482 * we get the post-operation mtime and size. This means that
2483 * we can't use xdr_encode_pages() as written: we need a variant
2484 * of it which would leave room in the 'tail' iovec.
2486 * Both of these changes to the XDR layer would in fact be quite
2487 * minor, but I decided to leave them for a subsequent patch.
2489 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2490 unsigned int pgbase
, unsigned int pglen
)
2492 struct nfs4_readlink args
= {
2493 .fh
= NFS_FH(inode
),
2498 struct nfs4_readlink_res res
;
2499 struct rpc_message msg
= {
2500 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2505 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2508 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2509 unsigned int pgbase
, unsigned int pglen
)
2511 struct nfs4_exception exception
= { };
2514 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2515 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2517 } while (exception
.retry
);
2523 * We will need to arrange for the VFS layer to provide an atomic open.
2524 * Until then, this create/open method is prone to inefficiency and race
2525 * conditions due to the lookup, create, and open VFS calls from sys_open()
2526 * placed on the wire.
2528 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2529 * The file will be opened again in the subsequent VFS open call
2530 * (nfs4_proc_file_open).
2532 * The open for read will just hang around to be used by any process that
2533 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2537 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2538 int flags
, struct nameidata
*nd
)
2540 struct path path
= {
2541 .mnt
= nd
->path
.mnt
,
2544 struct nfs4_state
*state
;
2545 struct rpc_cred
*cred
;
2546 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2549 cred
= rpc_lookup_cred();
2551 status
= PTR_ERR(cred
);
2554 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2556 if (IS_ERR(state
)) {
2557 status
= PTR_ERR(state
);
2560 d_add(dentry
, igrab(state
->inode
));
2561 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2562 if (flags
& O_EXCL
) {
2563 struct nfs_fattr fattr
;
2564 status
= nfs4_do_setattr(state
->inode
, cred
, &fattr
, sattr
, state
);
2566 nfs_setattr_update_inode(state
->inode
, sattr
);
2567 nfs_post_op_update_inode(state
->inode
, &fattr
);
2569 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2570 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2572 nfs4_close_sync(&path
, state
, fmode
);
2579 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2581 struct nfs_server
*server
= NFS_SERVER(dir
);
2582 struct nfs_removeargs args
= {
2584 .name
.len
= name
->len
,
2585 .name
.name
= name
->name
,
2586 .bitmask
= server
->attr_bitmask
,
2588 struct nfs_removeres res
= {
2591 struct rpc_message msg
= {
2592 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2598 nfs_fattr_init(&res
.dir_attr
);
2599 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2601 update_changeattr(dir
, &res
.cinfo
);
2602 nfs_post_op_update_inode(dir
, &res
.dir_attr
);
2607 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2609 struct nfs4_exception exception
= { };
2612 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2613 _nfs4_proc_remove(dir
, name
),
2615 } while (exception
.retry
);
2619 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2621 struct nfs_server
*server
= NFS_SERVER(dir
);
2622 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2623 struct nfs_removeres
*res
= msg
->rpc_resp
;
2625 args
->bitmask
= server
->cache_consistency_bitmask
;
2626 res
->server
= server
;
2627 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2630 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2632 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2634 nfs4_sequence_done(res
->server
, &res
->seq_res
, task
->tk_status
);
2635 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2637 update_changeattr(dir
, &res
->cinfo
);
2638 nfs_post_op_update_inode(dir
, &res
->dir_attr
);
2642 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2643 struct inode
*new_dir
, struct qstr
*new_name
)
2645 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2646 struct nfs4_rename_arg arg
= {
2647 .old_dir
= NFS_FH(old_dir
),
2648 .new_dir
= NFS_FH(new_dir
),
2649 .old_name
= old_name
,
2650 .new_name
= new_name
,
2651 .bitmask
= server
->attr_bitmask
,
2653 struct nfs_fattr old_fattr
, new_fattr
;
2654 struct nfs4_rename_res res
= {
2656 .old_fattr
= &old_fattr
,
2657 .new_fattr
= &new_fattr
,
2659 struct rpc_message msg
= {
2660 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2666 nfs_fattr_init(res
.old_fattr
);
2667 nfs_fattr_init(res
.new_fattr
);
2668 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2671 update_changeattr(old_dir
, &res
.old_cinfo
);
2672 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2673 update_changeattr(new_dir
, &res
.new_cinfo
);
2674 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2679 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2680 struct inode
*new_dir
, struct qstr
*new_name
)
2682 struct nfs4_exception exception
= { };
2685 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2686 _nfs4_proc_rename(old_dir
, old_name
,
2689 } while (exception
.retry
);
2693 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2695 struct nfs_server
*server
= NFS_SERVER(inode
);
2696 struct nfs4_link_arg arg
= {
2697 .fh
= NFS_FH(inode
),
2698 .dir_fh
= NFS_FH(dir
),
2700 .bitmask
= server
->attr_bitmask
,
2702 struct nfs_fattr fattr
, dir_attr
;
2703 struct nfs4_link_res res
= {
2706 .dir_attr
= &dir_attr
,
2708 struct rpc_message msg
= {
2709 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2715 nfs_fattr_init(res
.fattr
);
2716 nfs_fattr_init(res
.dir_attr
);
2717 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2719 update_changeattr(dir
, &res
.cinfo
);
2720 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2721 nfs_post_op_update_inode(inode
, res
.fattr
);
2727 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2729 struct nfs4_exception exception
= { };
2732 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2733 _nfs4_proc_link(inode
, dir
, name
),
2735 } while (exception
.retry
);
2739 struct nfs4_createdata
{
2740 struct rpc_message msg
;
2741 struct nfs4_create_arg arg
;
2742 struct nfs4_create_res res
;
2744 struct nfs_fattr fattr
;
2745 struct nfs_fattr dir_fattr
;
2748 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2749 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2751 struct nfs4_createdata
*data
;
2753 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2755 struct nfs_server
*server
= NFS_SERVER(dir
);
2757 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2758 data
->msg
.rpc_argp
= &data
->arg
;
2759 data
->msg
.rpc_resp
= &data
->res
;
2760 data
->arg
.dir_fh
= NFS_FH(dir
);
2761 data
->arg
.server
= server
;
2762 data
->arg
.name
= name
;
2763 data
->arg
.attrs
= sattr
;
2764 data
->arg
.ftype
= ftype
;
2765 data
->arg
.bitmask
= server
->attr_bitmask
;
2766 data
->res
.server
= server
;
2767 data
->res
.fh
= &data
->fh
;
2768 data
->res
.fattr
= &data
->fattr
;
2769 data
->res
.dir_fattr
= &data
->dir_fattr
;
2770 nfs_fattr_init(data
->res
.fattr
);
2771 nfs_fattr_init(data
->res
.dir_fattr
);
2776 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2778 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2779 &data
->arg
, &data
->res
, 1);
2781 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2782 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2783 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2788 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2793 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2794 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2796 struct nfs4_createdata
*data
;
2797 int status
= -ENAMETOOLONG
;
2799 if (len
> NFS4_MAXPATHLEN
)
2803 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2807 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2808 data
->arg
.u
.symlink
.pages
= &page
;
2809 data
->arg
.u
.symlink
.len
= len
;
2811 status
= nfs4_do_create(dir
, dentry
, data
);
2813 nfs4_free_createdata(data
);
2818 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2819 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2821 struct nfs4_exception exception
= { };
2824 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2825 _nfs4_proc_symlink(dir
, dentry
, page
,
2828 } while (exception
.retry
);
2832 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2833 struct iattr
*sattr
)
2835 struct nfs4_createdata
*data
;
2836 int status
= -ENOMEM
;
2838 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2842 status
= nfs4_do_create(dir
, dentry
, data
);
2844 nfs4_free_createdata(data
);
2849 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2850 struct iattr
*sattr
)
2852 struct nfs4_exception exception
= { };
2855 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2856 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2858 } while (exception
.retry
);
2862 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2863 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2865 struct inode
*dir
= dentry
->d_inode
;
2866 struct nfs4_readdir_arg args
= {
2871 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
2873 struct nfs4_readdir_res res
;
2874 struct rpc_message msg
= {
2875 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2882 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2883 dentry
->d_parent
->d_name
.name
,
2884 dentry
->d_name
.name
,
2885 (unsigned long long)cookie
);
2886 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2887 res
.pgbase
= args
.pgbase
;
2888 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2890 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2892 nfs_invalidate_atime(dir
);
2894 dprintk("%s: returns %d\n", __func__
, status
);
2898 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2899 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2901 struct nfs4_exception exception
= { };
2904 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2905 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2908 } while (exception
.retry
);
2912 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2913 struct iattr
*sattr
, dev_t rdev
)
2915 struct nfs4_createdata
*data
;
2916 int mode
= sattr
->ia_mode
;
2917 int status
= -ENOMEM
;
2919 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2920 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2922 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2927 data
->arg
.ftype
= NF4FIFO
;
2928 else if (S_ISBLK(mode
)) {
2929 data
->arg
.ftype
= NF4BLK
;
2930 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2931 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2933 else if (S_ISCHR(mode
)) {
2934 data
->arg
.ftype
= NF4CHR
;
2935 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2936 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2939 status
= nfs4_do_create(dir
, dentry
, data
);
2941 nfs4_free_createdata(data
);
2946 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2947 struct iattr
*sattr
, dev_t rdev
)
2949 struct nfs4_exception exception
= { };
2952 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2953 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2955 } while (exception
.retry
);
2959 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2960 struct nfs_fsstat
*fsstat
)
2962 struct nfs4_statfs_arg args
= {
2964 .bitmask
= server
->attr_bitmask
,
2966 struct nfs4_statfs_res res
= {
2969 struct rpc_message msg
= {
2970 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2975 nfs_fattr_init(fsstat
->fattr
);
2976 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2979 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2981 struct nfs4_exception exception
= { };
2984 err
= nfs4_handle_exception(server
,
2985 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2987 } while (exception
.retry
);
2991 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2992 struct nfs_fsinfo
*fsinfo
)
2994 struct nfs4_fsinfo_arg args
= {
2996 .bitmask
= server
->attr_bitmask
,
2998 struct nfs4_fsinfo_res res
= {
3001 struct rpc_message msg
= {
3002 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3007 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3010 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3012 struct nfs4_exception exception
= { };
3016 err
= nfs4_handle_exception(server
,
3017 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
3019 } while (exception
.retry
);
3023 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3025 nfs_fattr_init(fsinfo
->fattr
);
3026 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3029 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3030 struct nfs_pathconf
*pathconf
)
3032 struct nfs4_pathconf_arg args
= {
3034 .bitmask
= server
->attr_bitmask
,
3036 struct nfs4_pathconf_res res
= {
3037 .pathconf
= pathconf
,
3039 struct rpc_message msg
= {
3040 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3045 /* None of the pathconf attributes are mandatory to implement */
3046 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3047 memset(pathconf
, 0, sizeof(*pathconf
));
3051 nfs_fattr_init(pathconf
->fattr
);
3052 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3055 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3056 struct nfs_pathconf
*pathconf
)
3058 struct nfs4_exception exception
= { };
3062 err
= nfs4_handle_exception(server
,
3063 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3065 } while (exception
.retry
);
3069 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3071 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3073 dprintk("--> %s\n", __func__
);
3075 nfs4_sequence_done(server
, &data
->res
.seq_res
, task
->tk_status
);
3077 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3078 nfs_restart_rpc(task
, server
->nfs_client
);
3082 nfs_invalidate_atime(data
->inode
);
3083 if (task
->tk_status
> 0)
3084 renew_lease(server
, data
->timestamp
);
3088 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3090 data
->timestamp
= jiffies
;
3091 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3094 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3096 struct inode
*inode
= data
->inode
;
3098 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3101 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3102 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3105 if (task
->tk_status
>= 0) {
3106 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3107 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3112 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3114 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3116 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3117 data
->res
.server
= server
;
3118 data
->timestamp
= jiffies
;
3120 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3123 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3125 struct inode
*inode
= data
->inode
;
3127 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3129 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3130 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3133 nfs_refresh_inode(inode
, data
->res
.fattr
);
3137 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3139 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3141 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3142 data
->res
.server
= server
;
3143 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3147 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3148 * standalone procedure for queueing an asynchronous RENEW.
3150 static void nfs4_renew_done(struct rpc_task
*task
, void *data
)
3152 struct nfs_client
*clp
= (struct nfs_client
*)task
->tk_msg
.rpc_argp
;
3153 unsigned long timestamp
= (unsigned long)data
;
3155 if (task
->tk_status
< 0) {
3156 /* Unless we're shutting down, schedule state recovery! */
3157 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3158 nfs4_schedule_state_recovery(clp
);
3161 spin_lock(&clp
->cl_lock
);
3162 if (time_before(clp
->cl_last_renewal
,timestamp
))
3163 clp
->cl_last_renewal
= timestamp
;
3164 spin_unlock(&clp
->cl_lock
);
3167 static const struct rpc_call_ops nfs4_renew_ops
= {
3168 .rpc_call_done
= nfs4_renew_done
,
3171 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3173 struct rpc_message msg
= {
3174 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3179 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3180 &nfs4_renew_ops
, (void *)jiffies
);
3183 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3185 struct rpc_message msg
= {
3186 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3190 unsigned long now
= jiffies
;
3193 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3196 spin_lock(&clp
->cl_lock
);
3197 if (time_before(clp
->cl_last_renewal
,now
))
3198 clp
->cl_last_renewal
= now
;
3199 spin_unlock(&clp
->cl_lock
);
3203 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3205 return (server
->caps
& NFS_CAP_ACLS
)
3206 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3207 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3210 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3211 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3214 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3216 static void buf_to_pages(const void *buf
, size_t buflen
,
3217 struct page
**pages
, unsigned int *pgbase
)
3219 const void *p
= buf
;
3221 *pgbase
= offset_in_page(buf
);
3223 while (p
< buf
+ buflen
) {
3224 *(pages
++) = virt_to_page(p
);
3225 p
+= PAGE_CACHE_SIZE
;
3229 struct nfs4_cached_acl
{
3235 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3237 struct nfs_inode
*nfsi
= NFS_I(inode
);
3239 spin_lock(&inode
->i_lock
);
3240 kfree(nfsi
->nfs4_acl
);
3241 nfsi
->nfs4_acl
= acl
;
3242 spin_unlock(&inode
->i_lock
);
3245 static void nfs4_zap_acl_attr(struct inode
*inode
)
3247 nfs4_set_cached_acl(inode
, NULL
);
3250 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3252 struct nfs_inode
*nfsi
= NFS_I(inode
);
3253 struct nfs4_cached_acl
*acl
;
3256 spin_lock(&inode
->i_lock
);
3257 acl
= nfsi
->nfs4_acl
;
3260 if (buf
== NULL
) /* user is just asking for length */
3262 if (acl
->cached
== 0)
3264 ret
= -ERANGE
; /* see getxattr(2) man page */
3265 if (acl
->len
> buflen
)
3267 memcpy(buf
, acl
->data
, acl
->len
);
3271 spin_unlock(&inode
->i_lock
);
3275 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3277 struct nfs4_cached_acl
*acl
;
3279 if (buf
&& acl_len
<= PAGE_SIZE
) {
3280 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3284 memcpy(acl
->data
, buf
, acl_len
);
3286 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3293 nfs4_set_cached_acl(inode
, acl
);
3296 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3298 struct page
*pages
[NFS4ACL_MAXPAGES
];
3299 struct nfs_getaclargs args
= {
3300 .fh
= NFS_FH(inode
),
3304 struct nfs_getaclres res
= {
3308 struct rpc_message msg
= {
3309 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3313 struct page
*localpage
= NULL
;
3316 if (buflen
< PAGE_SIZE
) {
3317 /* As long as we're doing a round trip to the server anyway,
3318 * let's be prepared for a page of acl data. */
3319 localpage
= alloc_page(GFP_KERNEL
);
3320 resp_buf
= page_address(localpage
);
3321 if (localpage
== NULL
)
3323 args
.acl_pages
[0] = localpage
;
3324 args
.acl_pgbase
= 0;
3325 args
.acl_len
= PAGE_SIZE
;
3328 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3330 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3333 if (res
.acl_len
> args
.acl_len
)
3334 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3336 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3339 if (res
.acl_len
> buflen
)
3342 memcpy(buf
, resp_buf
, res
.acl_len
);
3347 __free_page(localpage
);
3351 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3353 struct nfs4_exception exception
= { };
3356 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3359 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3360 } while (exception
.retry
);
3364 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3366 struct nfs_server
*server
= NFS_SERVER(inode
);
3369 if (!nfs4_server_supports_acls(server
))
3371 ret
= nfs_revalidate_inode(server
, inode
);
3374 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3377 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3380 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3382 struct nfs_server
*server
= NFS_SERVER(inode
);
3383 struct page
*pages
[NFS4ACL_MAXPAGES
];
3384 struct nfs_setaclargs arg
= {
3385 .fh
= NFS_FH(inode
),
3389 struct nfs_setaclres res
;
3390 struct rpc_message msg
= {
3391 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3397 if (!nfs4_server_supports_acls(server
))
3399 nfs_inode_return_delegation(inode
);
3400 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3401 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3402 nfs_access_zap_cache(inode
);
3403 nfs_zap_acl_cache(inode
);
3407 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3409 struct nfs4_exception exception
= { };
3412 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3413 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3415 } while (exception
.retry
);
3420 _nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs_client
*clp
, struct nfs4_state
*state
)
3422 if (!clp
|| task
->tk_status
>= 0)
3424 switch(task
->tk_status
) {
3425 case -NFS4ERR_ADMIN_REVOKED
:
3426 case -NFS4ERR_BAD_STATEID
:
3427 case -NFS4ERR_OPENMODE
:
3430 nfs4_state_mark_reclaim_nograce(clp
, state
);
3431 goto do_state_recovery
;
3432 case -NFS4ERR_STALE_STATEID
:
3435 nfs4_state_mark_reclaim_reboot(clp
, state
);
3436 case -NFS4ERR_STALE_CLIENTID
:
3437 case -NFS4ERR_EXPIRED
:
3438 goto do_state_recovery
;
3439 #if defined(CONFIG_NFS_V4_1)
3440 case -NFS4ERR_BADSESSION
:
3441 case -NFS4ERR_BADSLOT
:
3442 case -NFS4ERR_BAD_HIGH_SLOT
:
3443 case -NFS4ERR_DEADSESSION
:
3444 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3445 case -NFS4ERR_SEQ_FALSE_RETRY
:
3446 case -NFS4ERR_SEQ_MISORDERED
:
3447 dprintk("%s ERROR %d, Reset session\n", __func__
,
3449 nfs4_schedule_state_recovery(clp
);
3450 task
->tk_status
= 0;
3452 #endif /* CONFIG_NFS_V4_1 */
3453 case -NFS4ERR_DELAY
:
3455 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3456 case -NFS4ERR_GRACE
:
3457 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3458 task
->tk_status
= 0;
3460 case -NFS4ERR_OLD_STATEID
:
3461 task
->tk_status
= 0;
3464 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3467 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3468 nfs4_schedule_state_recovery(clp
);
3469 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3470 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3471 task
->tk_status
= 0;
3476 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3478 return _nfs4_async_handle_error(task
, server
, server
->nfs_client
, state
);
3481 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
, unsigned short port
, struct rpc_cred
*cred
)
3483 nfs4_verifier sc_verifier
;
3484 struct nfs4_setclientid setclientid
= {
3485 .sc_verifier
= &sc_verifier
,
3488 struct rpc_message msg
= {
3489 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3490 .rpc_argp
= &setclientid
,
3498 p
= (__be32
*)sc_verifier
.data
;
3499 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3500 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3503 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3504 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3506 rpc_peeraddr2str(clp
->cl_rpcclient
,
3508 rpc_peeraddr2str(clp
->cl_rpcclient
,
3510 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3511 clp
->cl_id_uniquifier
);
3512 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3513 sizeof(setclientid
.sc_netid
),
3514 rpc_peeraddr2str(clp
->cl_rpcclient
,
3515 RPC_DISPLAY_NETID
));
3516 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3517 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3518 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3520 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3521 if (status
!= -NFS4ERR_CLID_INUSE
)
3526 ssleep(clp
->cl_lease_time
+ 1);
3528 if (++clp
->cl_id_uniquifier
== 0)
3534 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3536 struct nfs_fsinfo fsinfo
;
3537 struct rpc_message msg
= {
3538 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3540 .rpc_resp
= &fsinfo
,
3547 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3549 spin_lock(&clp
->cl_lock
);
3550 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3551 clp
->cl_last_renewal
= now
;
3552 spin_unlock(&clp
->cl_lock
);
3557 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3562 err
= _nfs4_proc_setclientid_confirm(clp
, cred
);
3566 case -NFS4ERR_RESOURCE
:
3567 /* The IBM lawyers misread another document! */
3568 case -NFS4ERR_DELAY
:
3569 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3575 struct nfs4_delegreturndata
{
3576 struct nfs4_delegreturnargs args
;
3577 struct nfs4_delegreturnres res
;
3579 nfs4_stateid stateid
;
3580 unsigned long timestamp
;
3581 struct nfs_fattr fattr
;
3585 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3587 struct nfs4_delegreturndata
*data
= calldata
;
3589 nfs4_sequence_done(data
->res
.server
, &data
->res
.seq_res
,
3592 switch (task
->tk_status
) {
3593 case -NFS4ERR_STALE_STATEID
:
3594 case -NFS4ERR_EXPIRED
:
3596 renew_lease(data
->res
.server
, data
->timestamp
);
3599 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
3601 nfs_restart_rpc(task
, data
->res
.server
->nfs_client
);
3605 data
->rpc_status
= task
->tk_status
;
3608 static void nfs4_delegreturn_release(void *calldata
)
3613 #if defined(CONFIG_NFS_V4_1)
3614 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3616 struct nfs4_delegreturndata
*d_data
;
3618 d_data
= (struct nfs4_delegreturndata
*)data
;
3620 if (nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
3621 &d_data
->args
.seq_args
,
3622 &d_data
->res
.seq_res
, 1, task
))
3624 rpc_call_start(task
);
3626 #endif /* CONFIG_NFS_V4_1 */
3628 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3629 #if defined(CONFIG_NFS_V4_1)
3630 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3631 #endif /* CONFIG_NFS_V4_1 */
3632 .rpc_call_done
= nfs4_delegreturn_done
,
3633 .rpc_release
= nfs4_delegreturn_release
,
3636 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3638 struct nfs4_delegreturndata
*data
;
3639 struct nfs_server
*server
= NFS_SERVER(inode
);
3640 struct rpc_task
*task
;
3641 struct rpc_message msg
= {
3642 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3645 struct rpc_task_setup task_setup_data
= {
3646 .rpc_client
= server
->client
,
3647 .rpc_message
= &msg
,
3648 .callback_ops
= &nfs4_delegreturn_ops
,
3649 .flags
= RPC_TASK_ASYNC
,
3653 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3656 data
->args
.fhandle
= &data
->fh
;
3657 data
->args
.stateid
= &data
->stateid
;
3658 data
->args
.bitmask
= server
->attr_bitmask
;
3659 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3660 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3661 data
->res
.fattr
= &data
->fattr
;
3662 data
->res
.server
= server
;
3663 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3664 nfs_fattr_init(data
->res
.fattr
);
3665 data
->timestamp
= jiffies
;
3666 data
->rpc_status
= 0;
3668 task_setup_data
.callback_data
= data
;
3669 msg
.rpc_argp
= &data
->args
,
3670 msg
.rpc_resp
= &data
->res
,
3671 task
= rpc_run_task(&task_setup_data
);
3673 return PTR_ERR(task
);
3676 status
= nfs4_wait_for_completion_rpc_task(task
);
3679 status
= data
->rpc_status
;
3682 nfs_refresh_inode(inode
, &data
->fattr
);
3688 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3690 struct nfs_server
*server
= NFS_SERVER(inode
);
3691 struct nfs4_exception exception
= { };
3694 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3696 case -NFS4ERR_STALE_STATEID
:
3697 case -NFS4ERR_EXPIRED
:
3701 err
= nfs4_handle_exception(server
, err
, &exception
);
3702 } while (exception
.retry
);
3706 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3707 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3710 * sleep, with exponential backoff, and retry the LOCK operation.
3712 static unsigned long
3713 nfs4_set_lock_task_retry(unsigned long timeout
)
3715 schedule_timeout_killable(timeout
);
3717 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3718 return NFS4_LOCK_MAXTIMEOUT
;
3722 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3724 struct inode
*inode
= state
->inode
;
3725 struct nfs_server
*server
= NFS_SERVER(inode
);
3726 struct nfs_client
*clp
= server
->nfs_client
;
3727 struct nfs_lockt_args arg
= {
3728 .fh
= NFS_FH(inode
),
3731 struct nfs_lockt_res res
= {
3734 struct rpc_message msg
= {
3735 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3738 .rpc_cred
= state
->owner
->so_cred
,
3740 struct nfs4_lock_state
*lsp
;
3743 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3744 status
= nfs4_set_lock_state(state
, request
);
3747 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3748 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3749 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3752 request
->fl_type
= F_UNLCK
;
3754 case -NFS4ERR_DENIED
:
3757 request
->fl_ops
->fl_release_private(request
);
3762 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3764 struct nfs4_exception exception
= { };
3768 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3769 _nfs4_proc_getlk(state
, cmd
, request
),
3771 } while (exception
.retry
);
3775 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3778 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3780 res
= posix_lock_file_wait(file
, fl
);
3783 res
= flock_lock_file_wait(file
, fl
);
3791 struct nfs4_unlockdata
{
3792 struct nfs_locku_args arg
;
3793 struct nfs_locku_res res
;
3794 struct nfs4_lock_state
*lsp
;
3795 struct nfs_open_context
*ctx
;
3796 struct file_lock fl
;
3797 const struct nfs_server
*server
;
3798 unsigned long timestamp
;
3801 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3802 struct nfs_open_context
*ctx
,
3803 struct nfs4_lock_state
*lsp
,
3804 struct nfs_seqid
*seqid
)
3806 struct nfs4_unlockdata
*p
;
3807 struct inode
*inode
= lsp
->ls_state
->inode
;
3809 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3812 p
->arg
.fh
= NFS_FH(inode
);
3814 p
->arg
.seqid
= seqid
;
3815 p
->res
.seqid
= seqid
;
3816 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3817 p
->arg
.stateid
= &lsp
->ls_stateid
;
3819 atomic_inc(&lsp
->ls_count
);
3820 /* Ensure we don't close file until we're done freeing locks! */
3821 p
->ctx
= get_nfs_open_context(ctx
);
3822 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3823 p
->server
= NFS_SERVER(inode
);
3827 static void nfs4_locku_release_calldata(void *data
)
3829 struct nfs4_unlockdata
*calldata
= data
;
3830 nfs_free_seqid(calldata
->arg
.seqid
);
3831 nfs4_put_lock_state(calldata
->lsp
);
3832 put_nfs_open_context(calldata
->ctx
);
3836 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3838 struct nfs4_unlockdata
*calldata
= data
;
3840 nfs4_sequence_done(calldata
->server
, &calldata
->res
.seq_res
,
3842 if (RPC_ASSASSINATED(task
))
3844 switch (task
->tk_status
) {
3846 memcpy(calldata
->lsp
->ls_stateid
.data
,
3847 calldata
->res
.stateid
.data
,
3848 sizeof(calldata
->lsp
->ls_stateid
.data
));
3849 renew_lease(calldata
->server
, calldata
->timestamp
);
3851 case -NFS4ERR_BAD_STATEID
:
3852 case -NFS4ERR_OLD_STATEID
:
3853 case -NFS4ERR_STALE_STATEID
:
3854 case -NFS4ERR_EXPIRED
:
3857 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3858 nfs_restart_rpc(task
,
3859 calldata
->server
->nfs_client
);
3863 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3865 struct nfs4_unlockdata
*calldata
= data
;
3867 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3869 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3870 /* Note: exit _without_ running nfs4_locku_done */
3871 task
->tk_action
= NULL
;
3874 calldata
->timestamp
= jiffies
;
3875 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
3876 &calldata
->arg
.seq_args
,
3877 &calldata
->res
.seq_res
, 1, task
))
3879 rpc_call_start(task
);
3882 static const struct rpc_call_ops nfs4_locku_ops
= {
3883 .rpc_call_prepare
= nfs4_locku_prepare
,
3884 .rpc_call_done
= nfs4_locku_done
,
3885 .rpc_release
= nfs4_locku_release_calldata
,
3888 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3889 struct nfs_open_context
*ctx
,
3890 struct nfs4_lock_state
*lsp
,
3891 struct nfs_seqid
*seqid
)
3893 struct nfs4_unlockdata
*data
;
3894 struct rpc_message msg
= {
3895 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3896 .rpc_cred
= ctx
->cred
,
3898 struct rpc_task_setup task_setup_data
= {
3899 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3900 .rpc_message
= &msg
,
3901 .callback_ops
= &nfs4_locku_ops
,
3902 .workqueue
= nfsiod_workqueue
,
3903 .flags
= RPC_TASK_ASYNC
,
3906 /* Ensure this is an unlock - when canceling a lock, the
3907 * canceled lock is passed in, and it won't be an unlock.
3909 fl
->fl_type
= F_UNLCK
;
3911 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3913 nfs_free_seqid(seqid
);
3914 return ERR_PTR(-ENOMEM
);
3917 msg
.rpc_argp
= &data
->arg
,
3918 msg
.rpc_resp
= &data
->res
,
3919 task_setup_data
.callback_data
= data
;
3920 return rpc_run_task(&task_setup_data
);
3923 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3925 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3926 struct nfs_seqid
*seqid
;
3927 struct nfs4_lock_state
*lsp
;
3928 struct rpc_task
*task
;
3930 unsigned char fl_flags
= request
->fl_flags
;
3932 status
= nfs4_set_lock_state(state
, request
);
3933 /* Unlock _before_ we do the RPC call */
3934 request
->fl_flags
|= FL_EXISTS
;
3935 down_read(&nfsi
->rwsem
);
3936 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3937 up_read(&nfsi
->rwsem
);
3940 up_read(&nfsi
->rwsem
);
3943 /* Is this a delegated lock? */
3944 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3946 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3947 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3951 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3952 status
= PTR_ERR(task
);
3955 status
= nfs4_wait_for_completion_rpc_task(task
);
3958 request
->fl_flags
= fl_flags
;
3962 struct nfs4_lockdata
{
3963 struct nfs_lock_args arg
;
3964 struct nfs_lock_res res
;
3965 struct nfs4_lock_state
*lsp
;
3966 struct nfs_open_context
*ctx
;
3967 struct file_lock fl
;
3968 unsigned long timestamp
;
3971 struct nfs_server
*server
;
3974 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3975 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
)
3977 struct nfs4_lockdata
*p
;
3978 struct inode
*inode
= lsp
->ls_state
->inode
;
3979 struct nfs_server
*server
= NFS_SERVER(inode
);
3981 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3985 p
->arg
.fh
= NFS_FH(inode
);
3987 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
);
3988 if (p
->arg
.open_seqid
== NULL
)
3990 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3991 if (p
->arg
.lock_seqid
== NULL
)
3992 goto out_free_seqid
;
3993 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
3994 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
3995 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3996 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
3997 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4000 atomic_inc(&lsp
->ls_count
);
4001 p
->ctx
= get_nfs_open_context(ctx
);
4002 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4005 nfs_free_seqid(p
->arg
.open_seqid
);
4011 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
4013 struct nfs4_lockdata
*data
= calldata
;
4014 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4016 dprintk("%s: begin!\n", __func__
);
4017 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4019 /* Do we need to do an open_to_lock_owner? */
4020 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4021 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
4023 data
->arg
.open_stateid
= &state
->stateid
;
4024 data
->arg
.new_lock_owner
= 1;
4025 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4027 data
->arg
.new_lock_owner
= 0;
4028 data
->timestamp
= jiffies
;
4029 if (nfs4_setup_sequence(data
->server
->nfs_client
, &data
->arg
.seq_args
,
4030 &data
->res
.seq_res
, 1, task
))
4032 rpc_call_start(task
);
4033 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4036 static void nfs4_recover_lock_prepare(struct rpc_task
*task
, void *calldata
)
4038 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4039 nfs4_lock_prepare(task
, calldata
);
4042 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4044 struct nfs4_lockdata
*data
= calldata
;
4046 dprintk("%s: begin!\n", __func__
);
4048 nfs4_sequence_done(data
->server
, &data
->res
.seq_res
,
4051 data
->rpc_status
= task
->tk_status
;
4052 if (RPC_ASSASSINATED(task
))
4054 if (data
->arg
.new_lock_owner
!= 0) {
4055 if (data
->rpc_status
== 0)
4056 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4060 if (data
->rpc_status
== 0) {
4061 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
4062 sizeof(data
->lsp
->ls_stateid
.data
));
4063 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
4064 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4067 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4070 static void nfs4_lock_release(void *calldata
)
4072 struct nfs4_lockdata
*data
= calldata
;
4074 dprintk("%s: begin!\n", __func__
);
4075 nfs_free_seqid(data
->arg
.open_seqid
);
4076 if (data
->cancelled
!= 0) {
4077 struct rpc_task
*task
;
4078 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4079 data
->arg
.lock_seqid
);
4082 dprintk("%s: cancelling lock!\n", __func__
);
4084 nfs_free_seqid(data
->arg
.lock_seqid
);
4085 nfs4_put_lock_state(data
->lsp
);
4086 put_nfs_open_context(data
->ctx
);
4088 dprintk("%s: done!\n", __func__
);
4091 static const struct rpc_call_ops nfs4_lock_ops
= {
4092 .rpc_call_prepare
= nfs4_lock_prepare
,
4093 .rpc_call_done
= nfs4_lock_done
,
4094 .rpc_release
= nfs4_lock_release
,
4097 static const struct rpc_call_ops nfs4_recover_lock_ops
= {
4098 .rpc_call_prepare
= nfs4_recover_lock_prepare
,
4099 .rpc_call_done
= nfs4_lock_done
,
4100 .rpc_release
= nfs4_lock_release
,
4103 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4105 struct nfs_client
*clp
= server
->nfs_client
;
4106 struct nfs4_state
*state
= lsp
->ls_state
;
4109 case -NFS4ERR_ADMIN_REVOKED
:
4110 case -NFS4ERR_BAD_STATEID
:
4111 case -NFS4ERR_EXPIRED
:
4112 if (new_lock_owner
!= 0 ||
4113 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4114 nfs4_state_mark_reclaim_nograce(clp
, state
);
4115 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4117 case -NFS4ERR_STALE_STATEID
:
4118 if (new_lock_owner
!= 0 ||
4119 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4120 nfs4_state_mark_reclaim_reboot(clp
, state
);
4121 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4125 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4127 struct nfs4_lockdata
*data
;
4128 struct rpc_task
*task
;
4129 struct rpc_message msg
= {
4130 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4131 .rpc_cred
= state
->owner
->so_cred
,
4133 struct rpc_task_setup task_setup_data
= {
4134 .rpc_client
= NFS_CLIENT(state
->inode
),
4135 .rpc_message
= &msg
,
4136 .callback_ops
= &nfs4_lock_ops
,
4137 .workqueue
= nfsiod_workqueue
,
4138 .flags
= RPC_TASK_ASYNC
,
4142 dprintk("%s: begin!\n", __func__
);
4143 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4144 fl
->fl_u
.nfs4_fl
.owner
);
4148 data
->arg
.block
= 1;
4149 if (recovery_type
> NFS_LOCK_NEW
) {
4150 if (recovery_type
== NFS_LOCK_RECLAIM
)
4151 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4152 task_setup_data
.callback_ops
= &nfs4_recover_lock_ops
;
4154 msg
.rpc_argp
= &data
->arg
,
4155 msg
.rpc_resp
= &data
->res
,
4156 task_setup_data
.callback_data
= data
;
4157 task
= rpc_run_task(&task_setup_data
);
4159 return PTR_ERR(task
);
4160 ret
= nfs4_wait_for_completion_rpc_task(task
);
4162 ret
= data
->rpc_status
;
4164 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4165 data
->arg
.new_lock_owner
, ret
);
4167 data
->cancelled
= 1;
4169 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4173 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4175 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4176 struct nfs4_exception exception
= { };
4180 /* Cache the lock if possible... */
4181 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4183 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4184 if (err
!= -NFS4ERR_DELAY
)
4186 nfs4_handle_exception(server
, err
, &exception
);
4187 } while (exception
.retry
);
4191 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4193 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4194 struct nfs4_exception exception
= { };
4197 err
= nfs4_set_lock_state(state
, request
);
4201 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4203 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4207 case -NFS4ERR_GRACE
:
4208 case -NFS4ERR_DELAY
:
4209 nfs4_handle_exception(server
, err
, &exception
);
4212 } while (exception
.retry
);
4217 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4219 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4220 unsigned char fl_flags
= request
->fl_flags
;
4221 int status
= -ENOLCK
;
4223 if ((fl_flags
& FL_POSIX
) &&
4224 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4226 /* Is this a delegated open? */
4227 status
= nfs4_set_lock_state(state
, request
);
4230 request
->fl_flags
|= FL_ACCESS
;
4231 status
= do_vfs_lock(request
->fl_file
, request
);
4234 down_read(&nfsi
->rwsem
);
4235 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4236 /* Yes: cache locks! */
4237 /* ...but avoid races with delegation recall... */
4238 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4239 status
= do_vfs_lock(request
->fl_file
, request
);
4242 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4245 /* Note: we always want to sleep here! */
4246 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4247 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4248 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4250 up_read(&nfsi
->rwsem
);
4252 request
->fl_flags
= fl_flags
;
4256 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4258 struct nfs4_exception exception
= { };
4262 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4263 if (err
== -NFS4ERR_DENIED
)
4265 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4267 } while (exception
.retry
);
4272 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4274 struct nfs_open_context
*ctx
;
4275 struct nfs4_state
*state
;
4276 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4279 /* verify open state */
4280 ctx
= nfs_file_open_context(filp
);
4283 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4286 if (IS_GETLK(cmd
)) {
4288 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4292 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4295 if (request
->fl_type
== F_UNLCK
) {
4297 return nfs4_proc_unlck(state
, cmd
, request
);
4304 status
= nfs4_proc_setlk(state
, cmd
, request
);
4305 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4307 timeout
= nfs4_set_lock_task_retry(timeout
);
4308 status
= -ERESTARTSYS
;
4311 } while(status
< 0);
4315 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4317 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4318 struct nfs4_exception exception
= { };
4321 err
= nfs4_set_lock_state(state
, fl
);
4325 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4328 printk(KERN_ERR
"%s: unhandled error %d.\n",
4333 case -NFS4ERR_EXPIRED
:
4334 case -NFS4ERR_STALE_CLIENTID
:
4335 case -NFS4ERR_STALE_STATEID
:
4336 case -NFS4ERR_BADSESSION
:
4337 case -NFS4ERR_BADSLOT
:
4338 case -NFS4ERR_BAD_HIGH_SLOT
:
4339 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4340 case -NFS4ERR_DEADSESSION
:
4341 nfs4_schedule_state_recovery(server
->nfs_client
);
4345 * The show must go on: exit, but mark the
4346 * stateid as needing recovery.
4348 case -NFS4ERR_ADMIN_REVOKED
:
4349 case -NFS4ERR_BAD_STATEID
:
4350 case -NFS4ERR_OPENMODE
:
4351 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
4355 case -NFS4ERR_DENIED
:
4356 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4359 case -NFS4ERR_DELAY
:
4362 err
= nfs4_handle_exception(server
, err
, &exception
);
4363 } while (exception
.retry
);
4368 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4370 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4371 size_t buflen
, int flags
)
4373 struct inode
*inode
= dentry
->d_inode
;
4375 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4378 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4381 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4382 * and that's what we'll do for e.g. user attributes that haven't been set.
4383 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4384 * attributes in kernel-managed attribute namespaces. */
4385 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4388 struct inode
*inode
= dentry
->d_inode
;
4390 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4393 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4396 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4398 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4400 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4402 if (buf
&& buflen
< len
)
4405 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4409 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4411 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4412 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4413 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4416 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4417 NFS_ATTR_FATTR_NLINK
;
4418 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4422 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4423 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4425 struct nfs_server
*server
= NFS_SERVER(dir
);
4427 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4428 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4430 struct nfs4_fs_locations_arg args
= {
4431 .dir_fh
= NFS_FH(dir
),
4436 struct nfs4_fs_locations_res res
= {
4437 .fs_locations
= fs_locations
,
4439 struct rpc_message msg
= {
4440 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4446 dprintk("%s: start\n", __func__
);
4447 nfs_fattr_init(&fs_locations
->fattr
);
4448 fs_locations
->server
= server
;
4449 fs_locations
->nlocations
= 0;
4450 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4451 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4452 dprintk("%s: returned status = %d\n", __func__
, status
);
4456 #ifdef CONFIG_NFS_V4_1
4458 * nfs4_proc_exchange_id()
4460 * Since the clientid has expired, all compounds using sessions
4461 * associated with the stale clientid will be returning
4462 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4463 * be in some phase of session reset.
4465 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4467 nfs4_verifier verifier
;
4468 struct nfs41_exchange_id_args args
= {
4470 .flags
= clp
->cl_exchange_flags
,
4472 struct nfs41_exchange_id_res res
= {
4476 struct rpc_message msg
= {
4477 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4484 dprintk("--> %s\n", __func__
);
4485 BUG_ON(clp
== NULL
);
4487 /* Remove server-only flags */
4488 args
.flags
&= ~EXCHGID4_FLAG_CONFIRMED_R
;
4490 p
= (u32
*)verifier
.data
;
4491 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4492 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4493 args
.verifier
= &verifier
;
4496 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4499 rpc_peeraddr2str(clp
->cl_rpcclient
,
4501 clp
->cl_id_uniquifier
);
4503 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4505 if (status
!= NFS4ERR_CLID_INUSE
)
4511 if (++clp
->cl_id_uniquifier
== 0)
4515 dprintk("<-- %s status= %d\n", __func__
, status
);
4519 struct nfs4_get_lease_time_data
{
4520 struct nfs4_get_lease_time_args
*args
;
4521 struct nfs4_get_lease_time_res
*res
;
4522 struct nfs_client
*clp
;
4525 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4529 struct nfs4_get_lease_time_data
*data
=
4530 (struct nfs4_get_lease_time_data
*)calldata
;
4532 dprintk("--> %s\n", __func__
);
4533 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4534 /* just setup sequence, do not trigger session recovery
4535 since we're invoked within one */
4536 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4537 &data
->args
->la_seq_args
,
4538 &data
->res
->lr_seq_res
, 0, task
);
4540 BUG_ON(ret
== -EAGAIN
);
4541 rpc_call_start(task
);
4542 dprintk("<-- %s\n", __func__
);
4546 * Called from nfs4_state_manager thread for session setup, so don't recover
4547 * from sequence operation or clientid errors.
4549 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4551 struct nfs4_get_lease_time_data
*data
=
4552 (struct nfs4_get_lease_time_data
*)calldata
;
4554 dprintk("--> %s\n", __func__
);
4555 nfs41_sequence_done(data
->clp
, &data
->res
->lr_seq_res
, task
->tk_status
);
4556 switch (task
->tk_status
) {
4557 case -NFS4ERR_DELAY
:
4558 case -NFS4ERR_GRACE
:
4559 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4560 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4561 task
->tk_status
= 0;
4562 nfs_restart_rpc(task
, data
->clp
);
4565 dprintk("<-- %s\n", __func__
);
4568 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4569 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4570 .rpc_call_done
= nfs4_get_lease_time_done
,
4573 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4575 struct rpc_task
*task
;
4576 struct nfs4_get_lease_time_args args
;
4577 struct nfs4_get_lease_time_res res
= {
4578 .lr_fsinfo
= fsinfo
,
4580 struct nfs4_get_lease_time_data data
= {
4585 struct rpc_message msg
= {
4586 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4590 struct rpc_task_setup task_setup
= {
4591 .rpc_client
= clp
->cl_rpcclient
,
4592 .rpc_message
= &msg
,
4593 .callback_ops
= &nfs4_get_lease_time_ops
,
4594 .callback_data
= &data
4598 res
.lr_seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4599 dprintk("--> %s\n", __func__
);
4600 task
= rpc_run_task(&task_setup
);
4603 status
= PTR_ERR(task
);
4605 status
= task
->tk_status
;
4608 dprintk("<-- %s return %d\n", __func__
, status
);
4614 * Reset a slot table
4616 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, int max_slots
,
4617 int old_max_slots
, int ivalue
)
4622 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__
, max_slots
, tbl
);
4625 * Until we have dynamic slot table adjustment, insist
4626 * upon the same slot table size
4628 if (max_slots
!= old_max_slots
) {
4629 dprintk("%s reset slot table does't match old\n",
4631 ret
= -EINVAL
; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4634 spin_lock(&tbl
->slot_tbl_lock
);
4635 for (i
= 0; i
< max_slots
; ++i
)
4636 tbl
->slots
[i
].seq_nr
= ivalue
;
4637 spin_unlock(&tbl
->slot_tbl_lock
);
4638 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4639 tbl
, tbl
->slots
, tbl
->max_slots
);
4641 dprintk("<-- %s: return %d\n", __func__
, ret
);
4646 * Reset the forechannel and backchannel slot tables
4648 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
4652 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
4653 session
->fc_attrs
.max_reqs
,
4654 session
->fc_slot_table
.max_slots
,
4659 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
4660 session
->bc_attrs
.max_reqs
,
4661 session
->bc_slot_table
.max_slots
,
4666 /* Destroy the slot table */
4667 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
4669 if (session
->fc_slot_table
.slots
!= NULL
) {
4670 kfree(session
->fc_slot_table
.slots
);
4671 session
->fc_slot_table
.slots
= NULL
;
4673 if (session
->bc_slot_table
.slots
!= NULL
) {
4674 kfree(session
->bc_slot_table
.slots
);
4675 session
->bc_slot_table
.slots
= NULL
;
4681 * Initialize slot table
4683 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
4684 int max_slots
, int ivalue
)
4686 struct nfs4_slot
*slot
;
4689 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4691 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
4693 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_KERNEL
);
4698 spin_lock(&tbl
->slot_tbl_lock
);
4699 tbl
->max_slots
= max_slots
;
4701 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4702 spin_unlock(&tbl
->slot_tbl_lock
);
4703 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4704 tbl
, tbl
->slots
, tbl
->max_slots
);
4706 dprintk("<-- %s: return %d\n", __func__
, ret
);
4711 * Initialize the forechannel and backchannel tables
4713 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
4715 struct nfs4_slot_table
*tbl
;
4718 tbl
= &session
->fc_slot_table
;
4719 if (tbl
->slots
== NULL
) {
4720 status
= nfs4_init_slot_table(tbl
,
4721 session
->fc_attrs
.max_reqs
, 1);
4726 tbl
= &session
->bc_slot_table
;
4727 if (tbl
->slots
== NULL
) {
4728 status
= nfs4_init_slot_table(tbl
,
4729 session
->bc_attrs
.max_reqs
, 0);
4731 nfs4_destroy_slot_tables(session
);
4737 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4739 struct nfs4_session
*session
;
4740 struct nfs4_slot_table
*tbl
;
4742 session
= kzalloc(sizeof(struct nfs4_session
), GFP_KERNEL
);
4747 * The create session reply races with the server back
4748 * channel probe. Mark the client NFS_CS_SESSION_INITING
4749 * so that the client back channel can find the
4752 clp
->cl_cons_state
= NFS_CS_SESSION_INITING
;
4753 init_completion(&session
->complete
);
4755 tbl
= &session
->fc_slot_table
;
4756 tbl
->highest_used_slotid
= -1;
4757 spin_lock_init(&tbl
->slot_tbl_lock
);
4758 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
4760 tbl
= &session
->bc_slot_table
;
4761 tbl
->highest_used_slotid
= -1;
4762 spin_lock_init(&tbl
->slot_tbl_lock
);
4763 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
4769 void nfs4_destroy_session(struct nfs4_session
*session
)
4771 nfs4_proc_destroy_session(session
);
4772 dprintk("%s Destroy backchannel for xprt %p\n",
4773 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
4774 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
4775 NFS41_BC_MIN_CALLBACKS
);
4776 nfs4_destroy_slot_tables(session
);
4781 * Initialize the values to be used by the client in CREATE_SESSION
4782 * If nfs4_init_session set the fore channel request and response sizes,
4785 * Set the back channel max_resp_sz_cached to zero to force the client to
4786 * always set csa_cachethis to FALSE because the current implementation
4787 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4789 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4791 struct nfs4_session
*session
= args
->client
->cl_session
;
4792 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4793 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4796 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4798 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4799 /* Fore channel attributes */
4800 args
->fc_attrs
.headerpadsz
= 0;
4801 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4802 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4803 args
->fc_attrs
.max_resp_sz_cached
= mxresp_sz
;
4804 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4805 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4807 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4808 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4810 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4811 args
->fc_attrs
.max_resp_sz_cached
, args
->fc_attrs
.max_ops
,
4812 args
->fc_attrs
.max_reqs
);
4814 /* Back channel attributes */
4815 args
->bc_attrs
.headerpadsz
= 0;
4816 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4817 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4818 args
->bc_attrs
.max_resp_sz_cached
= 0;
4819 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4820 args
->bc_attrs
.max_reqs
= 1;
4822 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4823 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4825 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4826 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4827 args
->bc_attrs
.max_reqs
);
4830 static int _verify_channel_attr(char *chan
, char *attr_name
, u32 sent
, u32 rcvd
)
4834 printk(KERN_WARNING
"%s: Session INVALID: %s channel %s increased. "
4835 "sent=%u rcvd=%u\n", __func__
, chan
, attr_name
, sent
, rcvd
);
4839 #define _verify_fore_channel_attr(_name_) \
4840 _verify_channel_attr("fore", #_name_, \
4841 args->fc_attrs._name_, \
4842 session->fc_attrs._name_)
4844 #define _verify_back_channel_attr(_name_) \
4845 _verify_channel_attr("back", #_name_, \
4846 args->bc_attrs._name_, \
4847 session->bc_attrs._name_)
4850 * The server is not allowed to increase the fore channel header pad size,
4851 * maximum response size, or maximum number of operations.
4853 * The back channel attributes are only negotiatied down: We send what the
4854 * (back channel) server insists upon.
4856 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4857 struct nfs4_session
*session
)
4861 ret
|= _verify_fore_channel_attr(headerpadsz
);
4862 ret
|= _verify_fore_channel_attr(max_resp_sz
);
4863 ret
|= _verify_fore_channel_attr(max_ops
);
4865 ret
|= _verify_back_channel_attr(headerpadsz
);
4866 ret
|= _verify_back_channel_attr(max_rqst_sz
);
4867 ret
|= _verify_back_channel_attr(max_resp_sz
);
4868 ret
|= _verify_back_channel_attr(max_resp_sz_cached
);
4869 ret
|= _verify_back_channel_attr(max_ops
);
4870 ret
|= _verify_back_channel_attr(max_reqs
);
4875 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4877 struct nfs4_session
*session
= clp
->cl_session
;
4878 struct nfs41_create_session_args args
= {
4880 .cb_program
= NFS4_CALLBACK
,
4882 struct nfs41_create_session_res res
= {
4885 struct rpc_message msg
= {
4886 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4892 nfs4_init_channel_attrs(&args
);
4893 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
4895 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4898 /* Verify the session's negotiated channel_attrs values */
4899 status
= nfs4_verify_channel_attrs(&args
, session
);
4901 /* Increment the clientid slot sequence id */
4909 * Issues a CREATE_SESSION operation to the server.
4910 * It is the responsibility of the caller to verify the session is
4911 * expired before calling this routine.
4913 int nfs4_proc_create_session(struct nfs_client
*clp
)
4917 struct nfs4_session
*session
= clp
->cl_session
;
4919 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4921 status
= _nfs4_proc_create_session(clp
);
4925 /* Init and reset the fore channel */
4926 status
= nfs4_init_slot_tables(session
);
4927 dprintk("slot table initialization returned %d\n", status
);
4930 status
= nfs4_reset_slot_tables(session
);
4931 dprintk("slot table reset returned %d\n", status
);
4935 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4936 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4937 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4939 dprintk("<-- %s\n", __func__
);
4944 * Issue the over-the-wire RPC DESTROY_SESSION.
4945 * The caller must serialize access to this routine.
4947 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4950 struct rpc_message msg
;
4952 dprintk("--> nfs4_proc_destroy_session\n");
4954 /* session is still being setup */
4955 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
4958 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
4959 msg
.rpc_argp
= session
;
4960 msg
.rpc_resp
= NULL
;
4961 msg
.rpc_cred
= NULL
;
4962 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4966 "Got error %d from the server on DESTROY_SESSION. "
4967 "Session has been destroyed regardless...\n", status
);
4969 dprintk("<-- nfs4_proc_destroy_session\n");
4973 int nfs4_init_session(struct nfs_server
*server
)
4975 struct nfs_client
*clp
= server
->nfs_client
;
4976 struct nfs4_session
*session
;
4977 unsigned int rsize
, wsize
;
4980 if (!nfs4_has_session(clp
))
4983 rsize
= server
->rsize
;
4985 rsize
= NFS_MAX_FILE_IO_SIZE
;
4986 wsize
= server
->wsize
;
4988 wsize
= NFS_MAX_FILE_IO_SIZE
;
4990 session
= clp
->cl_session
;
4991 session
->fc_attrs
.max_rqst_sz
= wsize
+ nfs41_maxwrite_overhead
;
4992 session
->fc_attrs
.max_resp_sz
= rsize
+ nfs41_maxread_overhead
;
4994 ret
= nfs4_recover_expired_lease(server
);
4996 ret
= nfs4_check_client_ready(clp
);
5001 * Renew the cl_session lease.
5003 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5005 struct nfs4_sequence_args args
;
5006 struct nfs4_sequence_res res
;
5008 struct rpc_message msg
= {
5009 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5015 args
.sa_cache_this
= 0;
5017 return nfs4_call_sync_sequence(clp
, clp
->cl_rpcclient
, &msg
, &args
,
5018 &res
, args
.sa_cache_this
, 1);
5021 void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5023 struct nfs_client
*clp
= (struct nfs_client
*)data
;
5025 nfs41_sequence_done(clp
, task
->tk_msg
.rpc_resp
, task
->tk_status
);
5027 if (task
->tk_status
< 0) {
5028 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5030 if (_nfs4_async_handle_error(task
, NULL
, clp
, NULL
)
5032 nfs_restart_rpc(task
, clp
);
5036 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5038 kfree(task
->tk_msg
.rpc_argp
);
5039 kfree(task
->tk_msg
.rpc_resp
);
5041 dprintk("<-- %s\n", __func__
);
5044 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5046 struct nfs_client
*clp
;
5047 struct nfs4_sequence_args
*args
;
5048 struct nfs4_sequence_res
*res
;
5050 clp
= (struct nfs_client
*)data
;
5051 args
= task
->tk_msg
.rpc_argp
;
5052 res
= task
->tk_msg
.rpc_resp
;
5054 if (nfs4_setup_sequence(clp
, args
, res
, 0, task
))
5056 rpc_call_start(task
);
5059 static const struct rpc_call_ops nfs41_sequence_ops
= {
5060 .rpc_call_done
= nfs41_sequence_call_done
,
5061 .rpc_call_prepare
= nfs41_sequence_prepare
,
5064 static int nfs41_proc_async_sequence(struct nfs_client
*clp
,
5065 struct rpc_cred
*cred
)
5067 struct nfs4_sequence_args
*args
;
5068 struct nfs4_sequence_res
*res
;
5069 struct rpc_message msg
= {
5070 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5074 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
5077 res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
5082 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5083 msg
.rpc_argp
= args
;
5086 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
5087 &nfs41_sequence_ops
, (void *)clp
);
5090 struct nfs4_reclaim_complete_data
{
5091 struct nfs_client
*clp
;
5092 struct nfs41_reclaim_complete_args arg
;
5093 struct nfs41_reclaim_complete_res res
;
5096 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5098 struct nfs4_reclaim_complete_data
*calldata
= data
;
5100 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
5101 if (nfs4_setup_sequence(calldata
->clp
, &calldata
->arg
.seq_args
,
5102 &calldata
->res
.seq_res
, 0, task
))
5105 rpc_call_start(task
);
5108 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5110 struct nfs4_reclaim_complete_data
*calldata
= data
;
5111 struct nfs_client
*clp
= calldata
->clp
;
5112 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5114 dprintk("--> %s\n", __func__
);
5115 nfs41_sequence_done(clp
, res
, task
->tk_status
);
5116 switch (task
->tk_status
) {
5118 case -NFS4ERR_COMPLETE_ALREADY
:
5120 case -NFS4ERR_BADSESSION
:
5121 case -NFS4ERR_DEADSESSION
:
5123 * Handle the session error, but do not retry the operation, as
5124 * we have no way of telling whether the clientid had to be
5125 * reset before we got our reply. If reset, a new wave of
5126 * reclaim operations will follow, containing their own reclaim
5127 * complete. We don't want our retry to get on the way of
5128 * recovery by incorrectly indicating to the server that we're
5129 * done reclaiming state since the process had to be restarted.
5131 _nfs4_async_handle_error(task
, NULL
, clp
, NULL
);
5134 if (_nfs4_async_handle_error(
5135 task
, NULL
, clp
, NULL
) == -EAGAIN
) {
5136 rpc_restart_call_prepare(task
);
5141 dprintk("<-- %s\n", __func__
);
5144 static void nfs4_free_reclaim_complete_data(void *data
)
5146 struct nfs4_reclaim_complete_data
*calldata
= data
;
5151 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5152 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5153 .rpc_call_done
= nfs4_reclaim_complete_done
,
5154 .rpc_release
= nfs4_free_reclaim_complete_data
,
5158 * Issue a global reclaim complete.
5160 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5162 struct nfs4_reclaim_complete_data
*calldata
;
5163 struct rpc_task
*task
;
5164 struct rpc_message msg
= {
5165 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5167 struct rpc_task_setup task_setup_data
= {
5168 .rpc_client
= clp
->cl_rpcclient
,
5169 .rpc_message
= &msg
,
5170 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5171 .flags
= RPC_TASK_ASYNC
,
5173 int status
= -ENOMEM
;
5175 dprintk("--> %s\n", __func__
);
5176 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
5177 if (calldata
== NULL
)
5179 calldata
->clp
= clp
;
5180 calldata
->arg
.one_fs
= 0;
5181 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5183 msg
.rpc_argp
= &calldata
->arg
;
5184 msg
.rpc_resp
= &calldata
->res
;
5185 task_setup_data
.callback_data
= calldata
;
5186 task
= rpc_run_task(&task_setup_data
);
5188 status
= PTR_ERR(task
);
5191 dprintk("<-- %s status=%d\n", __func__
, status
);
5194 #endif /* CONFIG_NFS_V4_1 */
5196 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5197 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5198 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5199 .recover_open
= nfs4_open_reclaim
,
5200 .recover_lock
= nfs4_lock_reclaim
,
5201 .establish_clid
= nfs4_init_clientid
,
5202 .get_clid_cred
= nfs4_get_setclientid_cred
,
5205 #if defined(CONFIG_NFS_V4_1)
5206 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5207 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5208 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5209 .recover_open
= nfs4_open_reclaim
,
5210 .recover_lock
= nfs4_lock_reclaim
,
5211 .establish_clid
= nfs41_init_clientid
,
5212 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5213 .reclaim_complete
= nfs41_proc_reclaim_complete
,
5215 #endif /* CONFIG_NFS_V4_1 */
5217 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5218 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5219 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5220 .recover_open
= nfs4_open_expired
,
5221 .recover_lock
= nfs4_lock_expired
,
5222 .establish_clid
= nfs4_init_clientid
,
5223 .get_clid_cred
= nfs4_get_setclientid_cred
,
5226 #if defined(CONFIG_NFS_V4_1)
5227 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5228 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5229 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5230 .recover_open
= nfs4_open_expired
,
5231 .recover_lock
= nfs4_lock_expired
,
5232 .establish_clid
= nfs41_init_clientid
,
5233 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5235 #endif /* CONFIG_NFS_V4_1 */
5237 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
5238 .sched_state_renewal
= nfs4_proc_async_renew
,
5239 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
5240 .renew_lease
= nfs4_proc_renew
,
5243 #if defined(CONFIG_NFS_V4_1)
5244 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
5245 .sched_state_renewal
= nfs41_proc_async_sequence
,
5246 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
5247 .renew_lease
= nfs4_proc_sequence
,
5252 * Per minor version reboot and network partition recovery ops
5255 struct nfs4_state_recovery_ops
*nfs4_reboot_recovery_ops
[] = {
5256 &nfs40_reboot_recovery_ops
,
5257 #if defined(CONFIG_NFS_V4_1)
5258 &nfs41_reboot_recovery_ops
,
5262 struct nfs4_state_recovery_ops
*nfs4_nograce_recovery_ops
[] = {
5263 &nfs40_nograce_recovery_ops
,
5264 #if defined(CONFIG_NFS_V4_1)
5265 &nfs41_nograce_recovery_ops
,
5269 struct nfs4_state_maintenance_ops
*nfs4_state_renewal_ops
[] = {
5270 &nfs40_state_renewal_ops
,
5271 #if defined(CONFIG_NFS_V4_1)
5272 &nfs41_state_renewal_ops
,
5276 static const struct inode_operations nfs4_file_inode_operations
= {
5277 .permission
= nfs_permission
,
5278 .getattr
= nfs_getattr
,
5279 .setattr
= nfs_setattr
,
5280 .getxattr
= nfs4_getxattr
,
5281 .setxattr
= nfs4_setxattr
,
5282 .listxattr
= nfs4_listxattr
,
5285 const struct nfs_rpc_ops nfs_v4_clientops
= {
5286 .version
= 4, /* protocol version */
5287 .dentry_ops
= &nfs4_dentry_operations
,
5288 .dir_inode_ops
= &nfs4_dir_inode_operations
,
5289 .file_inode_ops
= &nfs4_file_inode_operations
,
5290 .getroot
= nfs4_proc_get_root
,
5291 .getattr
= nfs4_proc_getattr
,
5292 .setattr
= nfs4_proc_setattr
,
5293 .lookupfh
= nfs4_proc_lookupfh
,
5294 .lookup
= nfs4_proc_lookup
,
5295 .access
= nfs4_proc_access
,
5296 .readlink
= nfs4_proc_readlink
,
5297 .create
= nfs4_proc_create
,
5298 .remove
= nfs4_proc_remove
,
5299 .unlink_setup
= nfs4_proc_unlink_setup
,
5300 .unlink_done
= nfs4_proc_unlink_done
,
5301 .rename
= nfs4_proc_rename
,
5302 .link
= nfs4_proc_link
,
5303 .symlink
= nfs4_proc_symlink
,
5304 .mkdir
= nfs4_proc_mkdir
,
5305 .rmdir
= nfs4_proc_remove
,
5306 .readdir
= nfs4_proc_readdir
,
5307 .mknod
= nfs4_proc_mknod
,
5308 .statfs
= nfs4_proc_statfs
,
5309 .fsinfo
= nfs4_proc_fsinfo
,
5310 .pathconf
= nfs4_proc_pathconf
,
5311 .set_capabilities
= nfs4_server_capabilities
,
5312 .decode_dirent
= nfs4_decode_dirent
,
5313 .read_setup
= nfs4_proc_read_setup
,
5314 .read_done
= nfs4_read_done
,
5315 .write_setup
= nfs4_proc_write_setup
,
5316 .write_done
= nfs4_write_done
,
5317 .commit_setup
= nfs4_proc_commit_setup
,
5318 .commit_done
= nfs4_commit_done
,
5319 .lock
= nfs4_proc_lock
,
5320 .clear_acl_cache
= nfs4_zap_acl_attr
,
5321 .close_context
= nfs4_close_context
,