4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
54 #include "delegation.h"
59 #define NFSDBG_FACILITY NFSDBG_PROC
61 #define NFS4_POLL_RETRY_MIN (HZ/10)
62 #define NFS4_POLL_RETRY_MAX (15*HZ)
64 #define NFS4_MAX_LOOP_ON_RECOVER (10)
67 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
68 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
);
69 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
70 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
71 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
72 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
73 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
74 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
75 struct nfs4_state
*state
);
77 /* Prevent leaks of NFSv4 errors into userland */
78 static int nfs4_map_errors(int err
)
83 case -NFS4ERR_RESOURCE
:
86 dprintk("%s could not handle NFSv4 error %d\n",
94 * This is our standard bitmap for GETATTR requests.
96 const u32 nfs4_fattr_bitmap
[2] = {
101 | FATTR4_WORD0_FILEID
,
103 | FATTR4_WORD1_NUMLINKS
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
113 const u32 nfs4_statfs_bitmap
[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL
,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
122 const u32 nfs4_pathconf_bitmap
[2] = {
124 | FATTR4_WORD0_MAXNAME
,
128 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME
,
135 const u32 nfs4_fs_locations_bitmap
[2] = {
137 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS
,
143 | FATTR4_WORD1_NUMLINKS
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
154 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
155 struct nfs4_readdir_arg
*readdir
)
159 BUG_ON(readdir
->count
< 80);
161 readdir
->cookie
= cookie
;
162 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
167 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
178 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
181 *p
++ = xdr_one
; /* next */
182 *p
++ = xdr_zero
; /* cookie, first word */
183 *p
++ = xdr_one
; /* cookie, second word */
184 *p
++ = xdr_one
; /* entry len */
185 memcpy(p
, ".\0\0\0", 4); /* entry */
187 *p
++ = xdr_one
; /* bitmap length */
188 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
189 *p
++ = htonl(8); /* attribute buffer length */
190 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
193 *p
++ = xdr_one
; /* next */
194 *p
++ = xdr_zero
; /* cookie, first word */
195 *p
++ = xdr_two
; /* cookie, second word */
196 *p
++ = xdr_two
; /* entry len */
197 memcpy(p
, "..\0\0", 4); /* entry */
199 *p
++ = xdr_one
; /* bitmap length */
200 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
201 *p
++ = htonl(8); /* attribute buffer length */
202 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
204 readdir
->pgbase
= (char *)p
- (char *)start
;
205 readdir
->count
-= readdir
->pgbase
;
206 kunmap_atomic(start
, KM_USER0
);
209 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
215 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
216 nfs_wait_bit_killable
, TASK_KILLABLE
);
220 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
227 *timeout
= NFS4_POLL_RETRY_MIN
;
228 if (*timeout
> NFS4_POLL_RETRY_MAX
)
229 *timeout
= NFS4_POLL_RETRY_MAX
;
230 schedule_timeout_killable(*timeout
);
231 if (fatal_signal_pending(current
))
237 /* This is the error handling routine for processes that are allowed
240 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
242 struct nfs_client
*clp
= server
->nfs_client
;
243 struct nfs4_state
*state
= exception
->state
;
246 exception
->retry
= 0;
250 case -NFS4ERR_ADMIN_REVOKED
:
251 case -NFS4ERR_BAD_STATEID
:
252 case -NFS4ERR_OPENMODE
:
255 nfs4_state_mark_reclaim_nograce(clp
, state
);
256 goto do_state_recovery
;
257 case -NFS4ERR_STALE_STATEID
:
260 nfs4_state_mark_reclaim_reboot(clp
, state
);
261 case -NFS4ERR_STALE_CLIENTID
:
262 case -NFS4ERR_EXPIRED
:
263 goto do_state_recovery
;
264 #if defined(CONFIG_NFS_V4_1)
265 case -NFS4ERR_BADSESSION
:
266 case -NFS4ERR_BADSLOT
:
267 case -NFS4ERR_BAD_HIGH_SLOT
:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
269 case -NFS4ERR_DEADSESSION
:
270 case -NFS4ERR_SEQ_FALSE_RETRY
:
271 case -NFS4ERR_SEQ_MISORDERED
:
272 dprintk("%s ERROR: %d Reset session\n", __func__
,
274 nfs4_schedule_state_recovery(clp
);
275 exception
->retry
= 1;
277 #endif /* defined(CONFIG_NFS_V4_1) */
278 case -NFS4ERR_FILE_OPEN
:
279 if (exception
->timeout
> HZ
) {
280 /* We have retried a decent amount, time to
289 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
292 case -NFS4ERR_OLD_STATEID
:
293 exception
->retry
= 1;
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret
);
298 nfs4_schedule_state_recovery(clp
);
299 ret
= nfs4_wait_clnt_recover(clp
);
301 exception
->retry
= 1;
306 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
308 struct nfs_client
*clp
= server
->nfs_client
;
309 spin_lock(&clp
->cl_lock
);
310 if (time_before(clp
->cl_last_renewal
,timestamp
))
311 clp
->cl_last_renewal
= timestamp
;
312 spin_unlock(&clp
->cl_lock
);
315 #if defined(CONFIG_NFS_V4_1)
318 * nfs4_free_slot - free a slot and efficiently update slot table.
320 * freeing a slot is trivially done by clearing its respective bit
322 * If the freed slotid equals highest_used_slotid we want to update it
323 * so that the server would be able to size down the slot table if needed,
324 * otherwise we know that the highest_used_slotid is still in use.
325 * When updating highest_used_slotid there may be "holes" in the bitmap
326 * so we need to scan down from highest_used_slotid to 0 looking for the now
327 * highest slotid in use.
328 * If none found, highest_used_slotid is set to -1.
330 * Must be called while holding tbl->slot_tbl_lock
333 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
335 int slotid
= free_slotid
;
337 /* clear used bit in bitmap */
338 __clear_bit(slotid
, tbl
->used_slots
);
340 /* update highest_used_slotid when it is freed */
341 if (slotid
== tbl
->highest_used_slotid
) {
342 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
343 if (slotid
< tbl
->max_slots
)
344 tbl
->highest_used_slotid
= slotid
;
346 tbl
->highest_used_slotid
= -1;
348 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
349 free_slotid
, tbl
->highest_used_slotid
);
353 * Signal state manager thread if session is drained
355 static void nfs41_check_drain_session_complete(struct nfs4_session
*ses
)
357 struct rpc_task
*task
;
359 if (!test_bit(NFS4CLNT_SESSION_DRAINING
, &ses
->clp
->cl_state
)) {
360 task
= rpc_wake_up_next(&ses
->fc_slot_table
.slot_tbl_waitq
);
362 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
366 if (ses
->fc_slot_table
.highest_used_slotid
!= -1)
369 dprintk("%s COMPLETE: Session Drained\n", __func__
);
370 complete(&ses
->complete
);
373 static void nfs41_sequence_free_slot(struct nfs4_sequence_res
*res
)
375 struct nfs4_slot_table
*tbl
;
377 tbl
= &res
->sr_session
->fc_slot_table
;
378 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
379 /* just wake up the next guy waiting since
380 * we may have not consumed a slot after all */
381 dprintk("%s: No slot\n", __func__
);
385 spin_lock(&tbl
->slot_tbl_lock
);
386 nfs4_free_slot(tbl
, res
->sr_slotid
);
387 nfs41_check_drain_session_complete(res
->sr_session
);
388 spin_unlock(&tbl
->slot_tbl_lock
);
389 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
392 static void nfs41_sequence_done(struct nfs4_sequence_res
*res
)
394 unsigned long timestamp
;
395 struct nfs4_slot_table
*tbl
;
396 struct nfs4_slot
*slot
;
399 * sr_status remains 1 if an RPC level error occurred. The server
400 * may or may not have processed the sequence operation..
401 * Proceed as if the server received and processed the sequence
404 if (res
->sr_status
== 1)
405 res
->sr_status
= NFS_OK
;
407 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
408 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
411 /* Check the SEQUENCE operation status */
412 if (res
->sr_status
== 0) {
413 struct nfs_client
*clp
= res
->sr_session
->clp
;
414 tbl
= &res
->sr_session
->fc_slot_table
;
415 slot
= tbl
->slots
+ res
->sr_slotid
;
416 /* Update the slot's sequence and clientid lease timer */
418 timestamp
= res
->sr_renewal_time
;
419 spin_lock(&clp
->cl_lock
);
420 if (time_before(clp
->cl_last_renewal
, timestamp
))
421 clp
->cl_last_renewal
= timestamp
;
422 spin_unlock(&clp
->cl_lock
);
423 /* Check sequence flags */
424 if (atomic_read(&clp
->cl_count
) > 1)
425 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
428 /* The session may be reset by one of the error handlers. */
429 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
430 nfs41_sequence_free_slot(res
);
434 * nfs4_find_slot - efficiently look for a free slot
436 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
437 * If found, we mark the slot as used, update the highest_used_slotid,
438 * and respectively set up the sequence operation args.
439 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
441 * Note: must be called with under the slot_tbl_lock.
444 nfs4_find_slot(struct nfs4_slot_table
*tbl
)
447 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
448 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
450 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
451 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
453 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
454 if (slotid
>= tbl
->max_slots
)
456 __set_bit(slotid
, tbl
->used_slots
);
457 if (slotid
> tbl
->highest_used_slotid
)
458 tbl
->highest_used_slotid
= slotid
;
461 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
462 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
466 static int nfs41_setup_sequence(struct nfs4_session
*session
,
467 struct nfs4_sequence_args
*args
,
468 struct nfs4_sequence_res
*res
,
470 struct rpc_task
*task
)
472 struct nfs4_slot
*slot
;
473 struct nfs4_slot_table
*tbl
;
476 dprintk("--> %s\n", __func__
);
477 /* slot already allocated? */
478 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
481 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
482 tbl
= &session
->fc_slot_table
;
484 spin_lock(&tbl
->slot_tbl_lock
);
485 if (test_bit(NFS4CLNT_SESSION_DRAINING
, &session
->clp
->cl_state
) &&
486 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
488 * The state manager will wait until the slot table is empty.
489 * Schedule the reset thread
491 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
492 spin_unlock(&tbl
->slot_tbl_lock
);
493 dprintk("%s Schedule Session Reset\n", __func__
);
497 if (!rpc_queue_empty(&tbl
->slot_tbl_waitq
) &&
498 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
499 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
500 spin_unlock(&tbl
->slot_tbl_lock
);
501 dprintk("%s enforce FIFO order\n", __func__
);
505 slotid
= nfs4_find_slot(tbl
);
506 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
507 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
508 spin_unlock(&tbl
->slot_tbl_lock
);
509 dprintk("<-- %s: no free slots\n", __func__
);
512 spin_unlock(&tbl
->slot_tbl_lock
);
514 rpc_task_set_priority(task
, RPC_PRIORITY_NORMAL
);
515 slot
= tbl
->slots
+ slotid
;
516 args
->sa_session
= session
;
517 args
->sa_slotid
= slotid
;
518 args
->sa_cache_this
= cache_reply
;
520 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
522 res
->sr_session
= session
;
523 res
->sr_slotid
= slotid
;
524 res
->sr_renewal_time
= jiffies
;
525 res
->sr_status_flags
= 0;
527 * sr_status is only set in decode_sequence, and so will remain
528 * set to 1 if an rpc level failure occurs.
534 int nfs4_setup_sequence(struct nfs_client
*clp
,
535 struct nfs4_sequence_args
*args
,
536 struct nfs4_sequence_res
*res
,
538 struct rpc_task
*task
)
542 dprintk("--> %s clp %p session %p sr_slotid %d\n",
543 __func__
, clp
, clp
->cl_session
, res
->sr_slotid
);
545 if (!nfs4_has_session(clp
))
547 ret
= nfs41_setup_sequence(clp
->cl_session
, args
, res
, cache_reply
,
550 dprintk("<-- %s status=%d\n", __func__
, ret
);
554 struct nfs41_call_sync_data
{
555 struct nfs_client
*clp
;
556 struct nfs4_sequence_args
*seq_args
;
557 struct nfs4_sequence_res
*seq_res
;
561 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
563 struct nfs41_call_sync_data
*data
= calldata
;
565 dprintk("--> %s data->clp->cl_session %p\n", __func__
,
566 data
->clp
->cl_session
);
567 if (nfs4_setup_sequence(data
->clp
, data
->seq_args
,
568 data
->seq_res
, data
->cache_reply
, task
))
570 rpc_call_start(task
);
573 static void nfs41_call_priv_sync_prepare(struct rpc_task
*task
, void *calldata
)
575 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
576 nfs41_call_sync_prepare(task
, calldata
);
579 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
581 struct nfs41_call_sync_data
*data
= calldata
;
583 nfs41_sequence_done(data
->seq_res
);
586 struct rpc_call_ops nfs41_call_sync_ops
= {
587 .rpc_call_prepare
= nfs41_call_sync_prepare
,
588 .rpc_call_done
= nfs41_call_sync_done
,
591 struct rpc_call_ops nfs41_call_priv_sync_ops
= {
592 .rpc_call_prepare
= nfs41_call_priv_sync_prepare
,
593 .rpc_call_done
= nfs41_call_sync_done
,
596 static int nfs4_call_sync_sequence(struct nfs_client
*clp
,
597 struct rpc_clnt
*clnt
,
598 struct rpc_message
*msg
,
599 struct nfs4_sequence_args
*args
,
600 struct nfs4_sequence_res
*res
,
605 struct rpc_task
*task
;
606 struct nfs41_call_sync_data data
= {
610 .cache_reply
= cache_reply
,
612 struct rpc_task_setup task_setup
= {
615 .callback_ops
= &nfs41_call_sync_ops
,
616 .callback_data
= &data
619 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
621 task_setup
.callback_ops
= &nfs41_call_priv_sync_ops
;
622 task
= rpc_run_task(&task_setup
);
626 ret
= task
->tk_status
;
632 int _nfs4_call_sync_session(struct nfs_server
*server
,
633 struct rpc_message
*msg
,
634 struct nfs4_sequence_args
*args
,
635 struct nfs4_sequence_res
*res
,
638 return nfs4_call_sync_sequence(server
->nfs_client
, server
->client
,
639 msg
, args
, res
, cache_reply
, 0);
642 #endif /* CONFIG_NFS_V4_1 */
644 int _nfs4_call_sync(struct nfs_server
*server
,
645 struct rpc_message
*msg
,
646 struct nfs4_sequence_args
*args
,
647 struct nfs4_sequence_res
*res
,
650 args
->sa_session
= res
->sr_session
= NULL
;
651 return rpc_call_sync(server
->client
, msg
, 0);
654 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
655 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
656 &(res)->seq_res, (cache_reply))
658 static void nfs4_sequence_done(const struct nfs_server
*server
,
659 struct nfs4_sequence_res
*res
, int rpc_status
)
661 #ifdef CONFIG_NFS_V4_1
662 if (nfs4_has_session(server
->nfs_client
))
663 nfs41_sequence_done(res
);
664 #endif /* CONFIG_NFS_V4_1 */
667 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
669 struct nfs_inode
*nfsi
= NFS_I(dir
);
671 spin_lock(&dir
->i_lock
);
672 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
673 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
674 nfs_force_lookup_revalidate(dir
);
675 nfsi
->change_attr
= cinfo
->after
;
676 spin_unlock(&dir
->i_lock
);
679 struct nfs4_opendata
{
681 struct nfs_openargs o_arg
;
682 struct nfs_openres o_res
;
683 struct nfs_open_confirmargs c_arg
;
684 struct nfs_open_confirmres c_res
;
685 struct nfs_fattr f_attr
;
686 struct nfs_fattr dir_attr
;
689 struct nfs4_state_owner
*owner
;
690 struct nfs4_state
*state
;
692 unsigned long timestamp
;
693 unsigned int rpc_done
: 1;
699 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
701 p
->o_res
.f_attr
= &p
->f_attr
;
702 p
->o_res
.dir_attr
= &p
->dir_attr
;
703 p
->o_res
.seqid
= p
->o_arg
.seqid
;
704 p
->c_res
.seqid
= p
->c_arg
.seqid
;
705 p
->o_res
.server
= p
->o_arg
.server
;
706 nfs_fattr_init(&p
->f_attr
);
707 nfs_fattr_init(&p
->dir_attr
);
708 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
711 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
712 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
713 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_mask
);
724 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
, gfp_mask
);
725 if (p
->o_arg
.seqid
== NULL
)
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
, GFP_NOFS
);
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
&& err
!= -EKEYEXPIRED
)
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
:
1588 nfs4_handle_exception(server
, err
, &exception
);
1591 } while (exception
.retry
);
1596 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1598 struct nfs_open_context
*ctx
;
1601 ctx
= nfs4_state_find_open_context(state
);
1603 return PTR_ERR(ctx
);
1604 ret
= nfs4_do_open_expired(ctx
, state
);
1605 put_nfs_open_context(ctx
);
1610 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1611 * fields corresponding to attributes that were used to store the verifier.
1612 * Make sure we clobber those fields in the later setattr call
1614 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1616 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1617 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1618 sattr
->ia_valid
|= ATTR_ATIME
;
1620 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1621 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1622 sattr
->ia_valid
|= ATTR_MTIME
;
1626 * Returns a referenced nfs4_state
1628 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
)
1630 struct nfs4_state_owner
*sp
;
1631 struct nfs4_state
*state
= NULL
;
1632 struct nfs_server
*server
= NFS_SERVER(dir
);
1633 struct nfs4_opendata
*opendata
;
1636 /* Protect against reboot recovery conflicts */
1638 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1639 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1642 status
= nfs4_recover_expired_lease(server
);
1644 goto err_put_state_owner
;
1645 if (path
->dentry
->d_inode
!= NULL
)
1646 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1648 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
, GFP_KERNEL
);
1649 if (opendata
== NULL
)
1650 goto err_put_state_owner
;
1652 if (path
->dentry
->d_inode
!= NULL
)
1653 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1655 status
= _nfs4_proc_open(opendata
);
1657 goto err_opendata_put
;
1659 state
= nfs4_opendata_to_nfs4_state(opendata
);
1660 status
= PTR_ERR(state
);
1662 goto err_opendata_put
;
1663 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
1664 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1666 if (opendata
->o_arg
.open_flags
& O_EXCL
) {
1667 nfs4_exclusive_attrset(opendata
, sattr
);
1669 nfs_fattr_init(opendata
->o_res
.f_attr
);
1670 status
= nfs4_do_setattr(state
->inode
, cred
,
1671 opendata
->o_res
.f_attr
, sattr
,
1674 nfs_setattr_update_inode(state
->inode
, sattr
);
1675 nfs_post_op_update_inode(state
->inode
, opendata
->o_res
.f_attr
);
1677 nfs4_opendata_put(opendata
);
1678 nfs4_put_state_owner(sp
);
1682 nfs4_opendata_put(opendata
);
1683 err_put_state_owner
:
1684 nfs4_put_state_owner(sp
);
1691 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
)
1693 struct nfs4_exception exception
= { };
1694 struct nfs4_state
*res
;
1698 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1701 /* NOTE: BAD_SEQID means the server and client disagree about the
1702 * book-keeping w.r.t. state-changing operations
1703 * (OPEN/CLOSE/LOCK/LOCKU...)
1704 * It is actually a sign of a bug on the client or on the server.
1706 * If we receive a BAD_SEQID error in the particular case of
1707 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1708 * have unhashed the old state_owner for us, and that we can
1709 * therefore safely retry using a new one. We should still warn
1710 * the user though...
1712 if (status
== -NFS4ERR_BAD_SEQID
) {
1713 printk(KERN_WARNING
"NFS: v4 server %s "
1714 " returned a bad sequence-id error!\n",
1715 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1716 exception
.retry
= 1;
1720 * BAD_STATEID on OPEN means that the server cancelled our
1721 * state before it received the OPEN_CONFIRM.
1722 * Recover by retrying the request as per the discussion
1723 * on Page 181 of RFC3530.
1725 if (status
== -NFS4ERR_BAD_STATEID
) {
1726 exception
.retry
= 1;
1729 if (status
== -EAGAIN
) {
1730 /* We must have found a delegation */
1731 exception
.retry
= 1;
1734 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1735 status
, &exception
));
1736 } while (exception
.retry
);
1740 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1741 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1742 struct nfs4_state
*state
)
1744 struct nfs_server
*server
= NFS_SERVER(inode
);
1745 struct nfs_setattrargs arg
= {
1746 .fh
= NFS_FH(inode
),
1749 .bitmask
= server
->attr_bitmask
,
1751 struct nfs_setattrres res
= {
1755 struct rpc_message msg
= {
1756 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1761 unsigned long timestamp
= jiffies
;
1764 nfs_fattr_init(fattr
);
1766 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1767 /* Use that stateid */
1768 } else if (state
!= NULL
) {
1769 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1771 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1773 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1774 if (status
== 0 && state
!= NULL
)
1775 renew_lease(server
, timestamp
);
1779 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1780 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1781 struct nfs4_state
*state
)
1783 struct nfs_server
*server
= NFS_SERVER(inode
);
1784 struct nfs4_exception exception
= { };
1787 err
= nfs4_handle_exception(server
,
1788 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1790 } while (exception
.retry
);
1794 struct nfs4_closedata
{
1796 struct inode
*inode
;
1797 struct nfs4_state
*state
;
1798 struct nfs_closeargs arg
;
1799 struct nfs_closeres res
;
1800 struct nfs_fattr fattr
;
1801 unsigned long timestamp
;
1804 static void nfs4_free_closedata(void *data
)
1806 struct nfs4_closedata
*calldata
= data
;
1807 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1809 nfs4_put_open_state(calldata
->state
);
1810 nfs_free_seqid(calldata
->arg
.seqid
);
1811 nfs4_put_state_owner(sp
);
1812 path_put(&calldata
->path
);
1816 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
1819 spin_lock(&state
->owner
->so_lock
);
1820 if (!(fmode
& FMODE_READ
))
1821 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1822 if (!(fmode
& FMODE_WRITE
))
1823 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1824 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1825 spin_unlock(&state
->owner
->so_lock
);
1828 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1830 struct nfs4_closedata
*calldata
= data
;
1831 struct nfs4_state
*state
= calldata
->state
;
1832 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1834 nfs4_sequence_done(server
, &calldata
->res
.seq_res
, task
->tk_status
);
1835 if (RPC_ASSASSINATED(task
))
1837 /* hmm. we are done with the inode, and in the process of freeing
1838 * the state_owner. we keep this around to process errors
1840 switch (task
->tk_status
) {
1842 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1843 renew_lease(server
, calldata
->timestamp
);
1844 nfs4_close_clear_stateid_flags(state
,
1845 calldata
->arg
.fmode
);
1847 case -NFS4ERR_STALE_STATEID
:
1848 case -NFS4ERR_OLD_STATEID
:
1849 case -NFS4ERR_BAD_STATEID
:
1850 case -NFS4ERR_EXPIRED
:
1851 if (calldata
->arg
.fmode
== 0)
1854 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
1855 rpc_restart_call_prepare(task
);
1857 nfs_release_seqid(calldata
->arg
.seqid
);
1858 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1861 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1863 struct nfs4_closedata
*calldata
= data
;
1864 struct nfs4_state
*state
= calldata
->state
;
1867 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1870 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1871 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
1872 spin_lock(&state
->owner
->so_lock
);
1873 /* Calculate the change in open mode */
1874 if (state
->n_rdwr
== 0) {
1875 if (state
->n_rdonly
== 0) {
1876 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1877 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1878 calldata
->arg
.fmode
&= ~FMODE_READ
;
1880 if (state
->n_wronly
== 0) {
1881 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1882 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1883 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
1886 spin_unlock(&state
->owner
->so_lock
);
1889 /* Note: exit _without_ calling nfs4_close_done */
1890 task
->tk_action
= NULL
;
1894 if (calldata
->arg
.fmode
== 0)
1895 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
1897 nfs_fattr_init(calldata
->res
.fattr
);
1898 calldata
->timestamp
= jiffies
;
1899 if (nfs4_setup_sequence((NFS_SERVER(calldata
->inode
))->nfs_client
,
1900 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1903 rpc_call_start(task
);
1906 static const struct rpc_call_ops nfs4_close_ops
= {
1907 .rpc_call_prepare
= nfs4_close_prepare
,
1908 .rpc_call_done
= nfs4_close_done
,
1909 .rpc_release
= nfs4_free_closedata
,
1913 * It is possible for data to be read/written from a mem-mapped file
1914 * after the sys_close call (which hits the vfs layer as a flush).
1915 * This means that we can't safely call nfsv4 close on a file until
1916 * the inode is cleared. This in turn means that we are not good
1917 * NFSv4 citizens - we do not indicate to the server to update the file's
1918 * share state even when we are done with one of the three share
1919 * stateid's in the inode.
1921 * NOTE: Caller must be holding the sp->so_owner semaphore!
1923 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, gfp_t gfp_mask
, int wait
)
1925 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1926 struct nfs4_closedata
*calldata
;
1927 struct nfs4_state_owner
*sp
= state
->owner
;
1928 struct rpc_task
*task
;
1929 struct rpc_message msg
= {
1930 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1931 .rpc_cred
= state
->owner
->so_cred
,
1933 struct rpc_task_setup task_setup_data
= {
1934 .rpc_client
= server
->client
,
1935 .rpc_message
= &msg
,
1936 .callback_ops
= &nfs4_close_ops
,
1937 .workqueue
= nfsiod_workqueue
,
1938 .flags
= RPC_TASK_ASYNC
,
1940 int status
= -ENOMEM
;
1942 calldata
= kzalloc(sizeof(*calldata
), gfp_mask
);
1943 if (calldata
== NULL
)
1945 calldata
->inode
= state
->inode
;
1946 calldata
->state
= state
;
1947 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1948 calldata
->arg
.stateid
= &state
->open_stateid
;
1949 /* Serialization for the sequence id */
1950 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
, gfp_mask
);
1951 if (calldata
->arg
.seqid
== NULL
)
1952 goto out_free_calldata
;
1953 calldata
->arg
.fmode
= 0;
1954 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1955 calldata
->res
.fattr
= &calldata
->fattr
;
1956 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1957 calldata
->res
.server
= server
;
1958 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1960 calldata
->path
= *path
;
1962 msg
.rpc_argp
= &calldata
->arg
,
1963 msg
.rpc_resp
= &calldata
->res
,
1964 task_setup_data
.callback_data
= calldata
;
1965 task
= rpc_run_task(&task_setup_data
);
1967 return PTR_ERR(task
);
1970 status
= rpc_wait_for_completion_task(task
);
1976 nfs4_put_open_state(state
);
1977 nfs4_put_state_owner(sp
);
1981 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1986 /* If the open_intent is for execute, we have an extra check to make */
1987 if (fmode
& FMODE_EXEC
) {
1988 ret
= nfs_may_open(state
->inode
,
1989 state
->owner
->so_cred
,
1990 nd
->intent
.open
.flags
);
1994 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1995 if (!IS_ERR(filp
)) {
1996 struct nfs_open_context
*ctx
;
1997 ctx
= nfs_file_open_context(filp
);
2001 ret
= PTR_ERR(filp
);
2003 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
2008 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
2010 struct path path
= {
2011 .mnt
= nd
->path
.mnt
,
2014 struct dentry
*parent
;
2016 struct rpc_cred
*cred
;
2017 struct nfs4_state
*state
;
2019 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
2021 if (nd
->flags
& LOOKUP_CREATE
) {
2022 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
2023 attr
.ia_valid
= ATTR_MODE
;
2024 if (!IS_POSIXACL(dir
))
2025 attr
.ia_mode
&= ~current_umask();
2028 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
2031 cred
= rpc_lookup_cred();
2033 return (struct dentry
*)cred
;
2034 parent
= dentry
->d_parent
;
2035 /* Protect against concurrent sillydeletes */
2036 nfs_block_sillyrename(parent
);
2037 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
2039 if (IS_ERR(state
)) {
2040 if (PTR_ERR(state
) == -ENOENT
) {
2041 d_add(dentry
, NULL
);
2042 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2044 nfs_unblock_sillyrename(parent
);
2045 return (struct dentry
*)state
;
2047 res
= d_add_unique(dentry
, igrab(state
->inode
));
2050 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
2051 nfs_unblock_sillyrename(parent
);
2052 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2057 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
2059 struct path path
= {
2060 .mnt
= nd
->path
.mnt
,
2063 struct rpc_cred
*cred
;
2064 struct nfs4_state
*state
;
2065 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
2067 cred
= rpc_lookup_cred();
2069 return PTR_ERR(cred
);
2070 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
2072 if (IS_ERR(state
)) {
2073 switch (PTR_ERR(state
)) {
2079 return PTR_ERR(state
);
2084 if (state
->inode
== dentry
->d_inode
) {
2085 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2086 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2089 nfs4_close_sync(&path
, state
, fmode
);
2095 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2097 if (ctx
->state
== NULL
)
2100 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2102 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2105 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2107 struct nfs4_server_caps_arg args
= {
2110 struct nfs4_server_caps_res res
= {};
2111 struct rpc_message msg
= {
2112 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2118 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2120 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2121 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2122 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2123 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2124 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2125 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2126 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2127 server
->caps
|= NFS_CAP_ACLS
;
2128 if (res
.has_links
!= 0)
2129 server
->caps
|= NFS_CAP_HARDLINKS
;
2130 if (res
.has_symlinks
!= 0)
2131 server
->caps
|= NFS_CAP_SYMLINKS
;
2132 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2133 server
->caps
|= NFS_CAP_FILEID
;
2134 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2135 server
->caps
|= NFS_CAP_MODE
;
2136 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2137 server
->caps
|= NFS_CAP_NLINK
;
2138 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2139 server
->caps
|= NFS_CAP_OWNER
;
2140 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2141 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2142 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2143 server
->caps
|= NFS_CAP_ATIME
;
2144 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2145 server
->caps
|= NFS_CAP_CTIME
;
2146 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2147 server
->caps
|= NFS_CAP_MTIME
;
2149 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2150 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2151 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2152 server
->acl_bitmask
= res
.acl_bitmask
;
2158 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2160 struct nfs4_exception exception
= { };
2163 err
= nfs4_handle_exception(server
,
2164 _nfs4_server_capabilities(server
, fhandle
),
2166 } while (exception
.retry
);
2170 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2171 struct nfs_fsinfo
*info
)
2173 struct nfs4_lookup_root_arg args
= {
2174 .bitmask
= nfs4_fattr_bitmap
,
2176 struct nfs4_lookup_res res
= {
2178 .fattr
= info
->fattr
,
2181 struct rpc_message msg
= {
2182 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2187 nfs_fattr_init(info
->fattr
);
2188 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2191 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2192 struct nfs_fsinfo
*info
)
2194 struct nfs4_exception exception
= { };
2197 err
= nfs4_handle_exception(server
,
2198 _nfs4_lookup_root(server
, fhandle
, info
),
2200 } while (exception
.retry
);
2205 * get the file handle for the "/" directory on the server
2207 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2208 struct nfs_fsinfo
*info
)
2212 status
= nfs4_lookup_root(server
, fhandle
, info
);
2214 status
= nfs4_server_capabilities(server
, fhandle
);
2216 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2217 return nfs4_map_errors(status
);
2221 * Get locations and (maybe) other attributes of a referral.
2222 * Note that we'll actually follow the referral later when
2223 * we detect fsid mismatch in inode revalidation
2225 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2227 int status
= -ENOMEM
;
2228 struct page
*page
= NULL
;
2229 struct nfs4_fs_locations
*locations
= NULL
;
2231 page
= alloc_page(GFP_KERNEL
);
2234 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2235 if (locations
== NULL
)
2238 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2241 /* Make sure server returned a different fsid for the referral */
2242 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2243 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2248 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2249 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2251 fattr
->mode
= S_IFDIR
;
2252 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2261 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2263 struct nfs4_getattr_arg args
= {
2265 .bitmask
= server
->attr_bitmask
,
2267 struct nfs4_getattr_res res
= {
2271 struct rpc_message msg
= {
2272 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2277 nfs_fattr_init(fattr
);
2278 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2281 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2283 struct nfs4_exception exception
= { };
2286 err
= nfs4_handle_exception(server
,
2287 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2289 } while (exception
.retry
);
2294 * The file is not closed if it is opened due to the a request to change
2295 * the size of the file. The open call will not be needed once the
2296 * VFS layer lookup-intents are implemented.
2298 * Close is called when the inode is destroyed.
2299 * If we haven't opened the file for O_WRONLY, we
2300 * need to in the size_change case to obtain a stateid.
2303 * Because OPEN is always done by name in nfsv4, it is
2304 * possible that we opened a different file by the same
2305 * name. We can recognize this race condition, but we
2306 * can't do anything about it besides returning an error.
2308 * This will be fixed with VFS changes (lookup-intent).
2311 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2312 struct iattr
*sattr
)
2314 struct inode
*inode
= dentry
->d_inode
;
2315 struct rpc_cred
*cred
= NULL
;
2316 struct nfs4_state
*state
= NULL
;
2319 nfs_fattr_init(fattr
);
2321 /* Search for an existing open(O_WRITE) file */
2322 if (sattr
->ia_valid
& ATTR_FILE
) {
2323 struct nfs_open_context
*ctx
;
2325 ctx
= nfs_file_open_context(sattr
->ia_file
);
2332 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2334 nfs_setattr_update_inode(inode
, sattr
);
2338 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2339 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2340 struct nfs_fattr
*fattr
)
2343 struct nfs4_lookup_arg args
= {
2344 .bitmask
= server
->attr_bitmask
,
2348 struct nfs4_lookup_res res
= {
2353 struct rpc_message msg
= {
2354 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2359 nfs_fattr_init(fattr
);
2361 dprintk("NFS call lookupfh %s\n", name
->name
);
2362 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2363 dprintk("NFS reply lookupfh: %d\n", status
);
2367 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2368 struct qstr
*name
, struct nfs_fh
*fhandle
,
2369 struct nfs_fattr
*fattr
)
2371 struct nfs4_exception exception
= { };
2374 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2376 if (err
== -NFS4ERR_MOVED
) {
2380 err
= nfs4_handle_exception(server
, err
, &exception
);
2381 } while (exception
.retry
);
2385 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2386 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2390 dprintk("NFS call lookup %s\n", name
->name
);
2391 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2392 if (status
== -NFS4ERR_MOVED
)
2393 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2394 dprintk("NFS reply lookup: %d\n", status
);
2398 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2400 struct nfs4_exception exception
= { };
2403 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2404 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2406 } while (exception
.retry
);
2410 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2412 struct nfs_server
*server
= NFS_SERVER(inode
);
2413 struct nfs4_accessargs args
= {
2414 .fh
= NFS_FH(inode
),
2415 .bitmask
= server
->attr_bitmask
,
2417 struct nfs4_accessres res
= {
2420 struct rpc_message msg
= {
2421 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2424 .rpc_cred
= entry
->cred
,
2426 int mode
= entry
->mask
;
2430 * Determine which access bits we want to ask for...
2432 if (mode
& MAY_READ
)
2433 args
.access
|= NFS4_ACCESS_READ
;
2434 if (S_ISDIR(inode
->i_mode
)) {
2435 if (mode
& MAY_WRITE
)
2436 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2437 if (mode
& MAY_EXEC
)
2438 args
.access
|= NFS4_ACCESS_LOOKUP
;
2440 if (mode
& MAY_WRITE
)
2441 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2442 if (mode
& MAY_EXEC
)
2443 args
.access
|= NFS4_ACCESS_EXECUTE
;
2446 res
.fattr
= nfs_alloc_fattr();
2447 if (res
.fattr
== NULL
)
2450 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2453 if (res
.access
& NFS4_ACCESS_READ
)
2454 entry
->mask
|= MAY_READ
;
2455 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2456 entry
->mask
|= MAY_WRITE
;
2457 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2458 entry
->mask
|= MAY_EXEC
;
2459 nfs_refresh_inode(inode
, res
.fattr
);
2461 nfs_free_fattr(res
.fattr
);
2465 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2467 struct nfs4_exception exception
= { };
2470 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2471 _nfs4_proc_access(inode
, entry
),
2473 } while (exception
.retry
);
2478 * TODO: For the time being, we don't try to get any attributes
2479 * along with any of the zero-copy operations READ, READDIR,
2482 * In the case of the first three, we want to put the GETATTR
2483 * after the read-type operation -- this is because it is hard
2484 * to predict the length of a GETATTR response in v4, and thus
2485 * align the READ data correctly. This means that the GETATTR
2486 * may end up partially falling into the page cache, and we should
2487 * shift it into the 'tail' of the xdr_buf before processing.
2488 * To do this efficiently, we need to know the total length
2489 * of data received, which doesn't seem to be available outside
2492 * In the case of WRITE, we also want to put the GETATTR after
2493 * the operation -- in this case because we want to make sure
2494 * we get the post-operation mtime and size. This means that
2495 * we can't use xdr_encode_pages() as written: we need a variant
2496 * of it which would leave room in the 'tail' iovec.
2498 * Both of these changes to the XDR layer would in fact be quite
2499 * minor, but I decided to leave them for a subsequent patch.
2501 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2502 unsigned int pgbase
, unsigned int pglen
)
2504 struct nfs4_readlink args
= {
2505 .fh
= NFS_FH(inode
),
2510 struct nfs4_readlink_res res
;
2511 struct rpc_message msg
= {
2512 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2517 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2520 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2521 unsigned int pgbase
, unsigned int pglen
)
2523 struct nfs4_exception exception
= { };
2526 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2527 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2529 } while (exception
.retry
);
2535 * We will need to arrange for the VFS layer to provide an atomic open.
2536 * Until then, this create/open method is prone to inefficiency and race
2537 * conditions due to the lookup, create, and open VFS calls from sys_open()
2538 * placed on the wire.
2540 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2541 * The file will be opened again in the subsequent VFS open call
2542 * (nfs4_proc_file_open).
2544 * The open for read will just hang around to be used by any process that
2545 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2549 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2550 int flags
, struct nameidata
*nd
)
2552 struct path path
= {
2553 .mnt
= nd
->path
.mnt
,
2556 struct nfs4_state
*state
;
2557 struct rpc_cred
*cred
;
2558 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2561 cred
= rpc_lookup_cred();
2563 status
= PTR_ERR(cred
);
2566 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2568 if (IS_ERR(state
)) {
2569 status
= PTR_ERR(state
);
2572 d_add(dentry
, igrab(state
->inode
));
2573 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2574 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2575 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2577 nfs4_close_sync(&path
, state
, fmode
);
2584 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2586 struct nfs_server
*server
= NFS_SERVER(dir
);
2587 struct nfs_removeargs args
= {
2589 .name
.len
= name
->len
,
2590 .name
.name
= name
->name
,
2591 .bitmask
= server
->attr_bitmask
,
2593 struct nfs_removeres res
= {
2596 struct rpc_message msg
= {
2597 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2601 int status
= -ENOMEM
;
2603 res
.dir_attr
= nfs_alloc_fattr();
2604 if (res
.dir_attr
== NULL
)
2607 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2609 update_changeattr(dir
, &res
.cinfo
);
2610 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2612 nfs_free_fattr(res
.dir_attr
);
2617 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2619 struct nfs4_exception exception
= { };
2622 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2623 _nfs4_proc_remove(dir
, name
),
2625 } while (exception
.retry
);
2629 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2631 struct nfs_server
*server
= NFS_SERVER(dir
);
2632 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2633 struct nfs_removeres
*res
= msg
->rpc_resp
;
2635 args
->bitmask
= server
->cache_consistency_bitmask
;
2636 res
->server
= server
;
2637 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2640 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2642 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2644 nfs4_sequence_done(res
->server
, &res
->seq_res
, task
->tk_status
);
2645 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2647 update_changeattr(dir
, &res
->cinfo
);
2648 nfs_post_op_update_inode(dir
, res
->dir_attr
);
2652 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2653 struct inode
*new_dir
, struct qstr
*new_name
)
2655 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2656 struct nfs4_rename_arg arg
= {
2657 .old_dir
= NFS_FH(old_dir
),
2658 .new_dir
= NFS_FH(new_dir
),
2659 .old_name
= old_name
,
2660 .new_name
= new_name
,
2661 .bitmask
= server
->attr_bitmask
,
2663 struct nfs4_rename_res res
= {
2666 struct rpc_message msg
= {
2667 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2671 int status
= -ENOMEM
;
2673 res
.old_fattr
= nfs_alloc_fattr();
2674 res
.new_fattr
= nfs_alloc_fattr();
2675 if (res
.old_fattr
== NULL
|| res
.new_fattr
== NULL
)
2678 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2680 update_changeattr(old_dir
, &res
.old_cinfo
);
2681 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2682 update_changeattr(new_dir
, &res
.new_cinfo
);
2683 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2686 nfs_free_fattr(res
.new_fattr
);
2687 nfs_free_fattr(res
.old_fattr
);
2691 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2692 struct inode
*new_dir
, struct qstr
*new_name
)
2694 struct nfs4_exception exception
= { };
2697 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2698 _nfs4_proc_rename(old_dir
, old_name
,
2701 } while (exception
.retry
);
2705 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2707 struct nfs_server
*server
= NFS_SERVER(inode
);
2708 struct nfs4_link_arg arg
= {
2709 .fh
= NFS_FH(inode
),
2710 .dir_fh
= NFS_FH(dir
),
2712 .bitmask
= server
->attr_bitmask
,
2714 struct nfs4_link_res res
= {
2717 struct rpc_message msg
= {
2718 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2722 int status
= -ENOMEM
;
2724 res
.fattr
= nfs_alloc_fattr();
2725 res
.dir_attr
= nfs_alloc_fattr();
2726 if (res
.fattr
== NULL
|| res
.dir_attr
== NULL
)
2729 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2731 update_changeattr(dir
, &res
.cinfo
);
2732 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2733 nfs_post_op_update_inode(inode
, res
.fattr
);
2736 nfs_free_fattr(res
.dir_attr
);
2737 nfs_free_fattr(res
.fattr
);
2741 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2743 struct nfs4_exception exception
= { };
2746 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2747 _nfs4_proc_link(inode
, dir
, name
),
2749 } while (exception
.retry
);
2753 struct nfs4_createdata
{
2754 struct rpc_message msg
;
2755 struct nfs4_create_arg arg
;
2756 struct nfs4_create_res res
;
2758 struct nfs_fattr fattr
;
2759 struct nfs_fattr dir_fattr
;
2762 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2763 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2765 struct nfs4_createdata
*data
;
2767 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2769 struct nfs_server
*server
= NFS_SERVER(dir
);
2771 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2772 data
->msg
.rpc_argp
= &data
->arg
;
2773 data
->msg
.rpc_resp
= &data
->res
;
2774 data
->arg
.dir_fh
= NFS_FH(dir
);
2775 data
->arg
.server
= server
;
2776 data
->arg
.name
= name
;
2777 data
->arg
.attrs
= sattr
;
2778 data
->arg
.ftype
= ftype
;
2779 data
->arg
.bitmask
= server
->attr_bitmask
;
2780 data
->res
.server
= server
;
2781 data
->res
.fh
= &data
->fh
;
2782 data
->res
.fattr
= &data
->fattr
;
2783 data
->res
.dir_fattr
= &data
->dir_fattr
;
2784 nfs_fattr_init(data
->res
.fattr
);
2785 nfs_fattr_init(data
->res
.dir_fattr
);
2790 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2792 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2793 &data
->arg
, &data
->res
, 1);
2795 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2796 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2797 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2802 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2807 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2808 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2810 struct nfs4_createdata
*data
;
2811 int status
= -ENAMETOOLONG
;
2813 if (len
> NFS4_MAXPATHLEN
)
2817 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2821 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2822 data
->arg
.u
.symlink
.pages
= &page
;
2823 data
->arg
.u
.symlink
.len
= len
;
2825 status
= nfs4_do_create(dir
, dentry
, data
);
2827 nfs4_free_createdata(data
);
2832 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2833 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2835 struct nfs4_exception exception
= { };
2838 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2839 _nfs4_proc_symlink(dir
, dentry
, page
,
2842 } while (exception
.retry
);
2846 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2847 struct iattr
*sattr
)
2849 struct nfs4_createdata
*data
;
2850 int status
= -ENOMEM
;
2852 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2856 status
= nfs4_do_create(dir
, dentry
, data
);
2858 nfs4_free_createdata(data
);
2863 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2864 struct iattr
*sattr
)
2866 struct nfs4_exception exception
= { };
2869 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2870 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2872 } while (exception
.retry
);
2876 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2877 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2879 struct inode
*dir
= dentry
->d_inode
;
2880 struct nfs4_readdir_arg args
= {
2885 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
2887 struct nfs4_readdir_res res
;
2888 struct rpc_message msg
= {
2889 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2896 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2897 dentry
->d_parent
->d_name
.name
,
2898 dentry
->d_name
.name
,
2899 (unsigned long long)cookie
);
2900 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2901 res
.pgbase
= args
.pgbase
;
2902 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2904 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2906 nfs_invalidate_atime(dir
);
2908 dprintk("%s: returns %d\n", __func__
, status
);
2912 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2913 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2915 struct nfs4_exception exception
= { };
2918 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2919 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2922 } while (exception
.retry
);
2926 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2927 struct iattr
*sattr
, dev_t rdev
)
2929 struct nfs4_createdata
*data
;
2930 int mode
= sattr
->ia_mode
;
2931 int status
= -ENOMEM
;
2933 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2934 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2936 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2941 data
->arg
.ftype
= NF4FIFO
;
2942 else if (S_ISBLK(mode
)) {
2943 data
->arg
.ftype
= NF4BLK
;
2944 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2945 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2947 else if (S_ISCHR(mode
)) {
2948 data
->arg
.ftype
= NF4CHR
;
2949 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2950 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2953 status
= nfs4_do_create(dir
, dentry
, data
);
2955 nfs4_free_createdata(data
);
2960 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2961 struct iattr
*sattr
, dev_t rdev
)
2963 struct nfs4_exception exception
= { };
2966 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2967 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2969 } while (exception
.retry
);
2973 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2974 struct nfs_fsstat
*fsstat
)
2976 struct nfs4_statfs_arg args
= {
2978 .bitmask
= server
->attr_bitmask
,
2980 struct nfs4_statfs_res res
= {
2983 struct rpc_message msg
= {
2984 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2989 nfs_fattr_init(fsstat
->fattr
);
2990 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2993 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2995 struct nfs4_exception exception
= { };
2998 err
= nfs4_handle_exception(server
,
2999 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
3001 } while (exception
.retry
);
3005 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3006 struct nfs_fsinfo
*fsinfo
)
3008 struct nfs4_fsinfo_arg args
= {
3010 .bitmask
= server
->attr_bitmask
,
3012 struct nfs4_fsinfo_res res
= {
3015 struct rpc_message msg
= {
3016 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3021 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3024 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3026 struct nfs4_exception exception
= { };
3030 err
= nfs4_handle_exception(server
,
3031 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
3033 } while (exception
.retry
);
3037 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3039 nfs_fattr_init(fsinfo
->fattr
);
3040 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3043 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3044 struct nfs_pathconf
*pathconf
)
3046 struct nfs4_pathconf_arg args
= {
3048 .bitmask
= server
->attr_bitmask
,
3050 struct nfs4_pathconf_res res
= {
3051 .pathconf
= pathconf
,
3053 struct rpc_message msg
= {
3054 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3059 /* None of the pathconf attributes are mandatory to implement */
3060 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3061 memset(pathconf
, 0, sizeof(*pathconf
));
3065 nfs_fattr_init(pathconf
->fattr
);
3066 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3069 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3070 struct nfs_pathconf
*pathconf
)
3072 struct nfs4_exception exception
= { };
3076 err
= nfs4_handle_exception(server
,
3077 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3079 } while (exception
.retry
);
3083 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3085 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3087 dprintk("--> %s\n", __func__
);
3089 nfs4_sequence_done(server
, &data
->res
.seq_res
, task
->tk_status
);
3091 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3092 nfs_restart_rpc(task
, server
->nfs_client
);
3096 nfs_invalidate_atime(data
->inode
);
3097 if (task
->tk_status
> 0)
3098 renew_lease(server
, data
->timestamp
);
3102 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3104 data
->timestamp
= jiffies
;
3105 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3108 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3110 struct inode
*inode
= data
->inode
;
3112 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3115 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3116 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3119 if (task
->tk_status
>= 0) {
3120 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3121 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3126 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3128 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3130 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3131 data
->res
.server
= server
;
3132 data
->timestamp
= jiffies
;
3134 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3137 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3139 struct inode
*inode
= data
->inode
;
3141 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3143 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3144 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3147 nfs_refresh_inode(inode
, data
->res
.fattr
);
3151 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3153 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3155 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3156 data
->res
.server
= server
;
3157 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3160 struct nfs4_renewdata
{
3161 struct nfs_client
*client
;
3162 unsigned long timestamp
;
3166 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3167 * standalone procedure for queueing an asynchronous RENEW.
3169 static void nfs4_renew_release(void *calldata
)
3171 struct nfs4_renewdata
*data
= calldata
;
3172 struct nfs_client
*clp
= data
->client
;
3174 if (atomic_read(&clp
->cl_count
) > 1)
3175 nfs4_schedule_state_renewal(clp
);
3176 nfs_put_client(clp
);
3180 static void nfs4_renew_done(struct rpc_task
*task
, void *calldata
)
3182 struct nfs4_renewdata
*data
= calldata
;
3183 struct nfs_client
*clp
= data
->client
;
3184 unsigned long timestamp
= data
->timestamp
;
3186 if (task
->tk_status
< 0) {
3187 /* Unless we're shutting down, schedule state recovery! */
3188 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3189 nfs4_schedule_state_recovery(clp
);
3192 spin_lock(&clp
->cl_lock
);
3193 if (time_before(clp
->cl_last_renewal
,timestamp
))
3194 clp
->cl_last_renewal
= timestamp
;
3195 spin_unlock(&clp
->cl_lock
);
3198 static const struct rpc_call_ops nfs4_renew_ops
= {
3199 .rpc_call_done
= nfs4_renew_done
,
3200 .rpc_release
= nfs4_renew_release
,
3203 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3205 struct rpc_message msg
= {
3206 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3210 struct nfs4_renewdata
*data
;
3212 if (!atomic_inc_not_zero(&clp
->cl_count
))
3214 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
3218 data
->timestamp
= jiffies
;
3219 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3220 &nfs4_renew_ops
, data
);
3223 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3225 struct rpc_message msg
= {
3226 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3230 unsigned long now
= jiffies
;
3233 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3236 spin_lock(&clp
->cl_lock
);
3237 if (time_before(clp
->cl_last_renewal
,now
))
3238 clp
->cl_last_renewal
= now
;
3239 spin_unlock(&clp
->cl_lock
);
3243 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3245 return (server
->caps
& NFS_CAP_ACLS
)
3246 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3247 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3250 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3251 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3254 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3256 static void buf_to_pages(const void *buf
, size_t buflen
,
3257 struct page
**pages
, unsigned int *pgbase
)
3259 const void *p
= buf
;
3261 *pgbase
= offset_in_page(buf
);
3263 while (p
< buf
+ buflen
) {
3264 *(pages
++) = virt_to_page(p
);
3265 p
+= PAGE_CACHE_SIZE
;
3269 struct nfs4_cached_acl
{
3275 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3277 struct nfs_inode
*nfsi
= NFS_I(inode
);
3279 spin_lock(&inode
->i_lock
);
3280 kfree(nfsi
->nfs4_acl
);
3281 nfsi
->nfs4_acl
= acl
;
3282 spin_unlock(&inode
->i_lock
);
3285 static void nfs4_zap_acl_attr(struct inode
*inode
)
3287 nfs4_set_cached_acl(inode
, NULL
);
3290 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3292 struct nfs_inode
*nfsi
= NFS_I(inode
);
3293 struct nfs4_cached_acl
*acl
;
3296 spin_lock(&inode
->i_lock
);
3297 acl
= nfsi
->nfs4_acl
;
3300 if (buf
== NULL
) /* user is just asking for length */
3302 if (acl
->cached
== 0)
3304 ret
= -ERANGE
; /* see getxattr(2) man page */
3305 if (acl
->len
> buflen
)
3307 memcpy(buf
, acl
->data
, acl
->len
);
3311 spin_unlock(&inode
->i_lock
);
3315 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3317 struct nfs4_cached_acl
*acl
;
3319 if (buf
&& acl_len
<= PAGE_SIZE
) {
3320 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3324 memcpy(acl
->data
, buf
, acl_len
);
3326 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3333 nfs4_set_cached_acl(inode
, acl
);
3336 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3338 struct page
*pages
[NFS4ACL_MAXPAGES
];
3339 struct nfs_getaclargs args
= {
3340 .fh
= NFS_FH(inode
),
3344 struct nfs_getaclres res
= {
3348 struct rpc_message msg
= {
3349 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3353 struct page
*localpage
= NULL
;
3356 if (buflen
< PAGE_SIZE
) {
3357 /* As long as we're doing a round trip to the server anyway,
3358 * let's be prepared for a page of acl data. */
3359 localpage
= alloc_page(GFP_KERNEL
);
3360 resp_buf
= page_address(localpage
);
3361 if (localpage
== NULL
)
3363 args
.acl_pages
[0] = localpage
;
3364 args
.acl_pgbase
= 0;
3365 args
.acl_len
= PAGE_SIZE
;
3368 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3370 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3373 if (res
.acl_len
> args
.acl_len
)
3374 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3376 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3379 if (res
.acl_len
> buflen
)
3382 memcpy(buf
, resp_buf
, res
.acl_len
);
3387 __free_page(localpage
);
3391 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3393 struct nfs4_exception exception
= { };
3396 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3399 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3400 } while (exception
.retry
);
3404 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3406 struct nfs_server
*server
= NFS_SERVER(inode
);
3409 if (!nfs4_server_supports_acls(server
))
3411 ret
= nfs_revalidate_inode(server
, inode
);
3414 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3417 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3420 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3422 struct nfs_server
*server
= NFS_SERVER(inode
);
3423 struct page
*pages
[NFS4ACL_MAXPAGES
];
3424 struct nfs_setaclargs arg
= {
3425 .fh
= NFS_FH(inode
),
3429 struct nfs_setaclres res
;
3430 struct rpc_message msg
= {
3431 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3437 if (!nfs4_server_supports_acls(server
))
3439 nfs_inode_return_delegation(inode
);
3440 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3441 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3442 nfs_access_zap_cache(inode
);
3443 nfs_zap_acl_cache(inode
);
3447 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3449 struct nfs4_exception exception
= { };
3452 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3453 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3455 } while (exception
.retry
);
3460 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3462 struct nfs_client
*clp
= server
->nfs_client
;
3464 if (task
->tk_status
>= 0)
3466 switch(task
->tk_status
) {
3467 case -NFS4ERR_ADMIN_REVOKED
:
3468 case -NFS4ERR_BAD_STATEID
:
3469 case -NFS4ERR_OPENMODE
:
3472 nfs4_state_mark_reclaim_nograce(clp
, state
);
3473 goto do_state_recovery
;
3474 case -NFS4ERR_STALE_STATEID
:
3477 nfs4_state_mark_reclaim_reboot(clp
, state
);
3478 case -NFS4ERR_STALE_CLIENTID
:
3479 case -NFS4ERR_EXPIRED
:
3480 goto do_state_recovery
;
3481 #if defined(CONFIG_NFS_V4_1)
3482 case -NFS4ERR_BADSESSION
:
3483 case -NFS4ERR_BADSLOT
:
3484 case -NFS4ERR_BAD_HIGH_SLOT
:
3485 case -NFS4ERR_DEADSESSION
:
3486 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3487 case -NFS4ERR_SEQ_FALSE_RETRY
:
3488 case -NFS4ERR_SEQ_MISORDERED
:
3489 dprintk("%s ERROR %d, Reset session\n", __func__
,
3491 nfs4_schedule_state_recovery(clp
);
3492 task
->tk_status
= 0;
3494 #endif /* CONFIG_NFS_V4_1 */
3495 case -NFS4ERR_DELAY
:
3496 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3497 case -NFS4ERR_GRACE
:
3499 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3500 task
->tk_status
= 0;
3502 case -NFS4ERR_OLD_STATEID
:
3503 task
->tk_status
= 0;
3506 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3509 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3510 nfs4_schedule_state_recovery(clp
);
3511 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3512 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3513 task
->tk_status
= 0;
3517 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
,
3518 unsigned short port
, struct rpc_cred
*cred
,
3519 struct nfs4_setclientid_res
*res
)
3521 nfs4_verifier sc_verifier
;
3522 struct nfs4_setclientid setclientid
= {
3523 .sc_verifier
= &sc_verifier
,
3526 struct rpc_message msg
= {
3527 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3528 .rpc_argp
= &setclientid
,
3536 p
= (__be32
*)sc_verifier
.data
;
3537 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3538 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3541 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3542 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3544 rpc_peeraddr2str(clp
->cl_rpcclient
,
3546 rpc_peeraddr2str(clp
->cl_rpcclient
,
3548 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3549 clp
->cl_id_uniquifier
);
3550 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3551 sizeof(setclientid
.sc_netid
),
3552 rpc_peeraddr2str(clp
->cl_rpcclient
,
3553 RPC_DISPLAY_NETID
));
3554 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3555 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3556 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3558 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3559 if (status
!= -NFS4ERR_CLID_INUSE
)
3564 ssleep(clp
->cl_lease_time
+ 1);
3566 if (++clp
->cl_id_uniquifier
== 0)
3572 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
3573 struct nfs4_setclientid_res
*arg
,
3574 struct rpc_cred
*cred
)
3576 struct nfs_fsinfo fsinfo
;
3577 struct rpc_message msg
= {
3578 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3580 .rpc_resp
= &fsinfo
,
3587 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3589 spin_lock(&clp
->cl_lock
);
3590 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3591 clp
->cl_last_renewal
= now
;
3592 spin_unlock(&clp
->cl_lock
);
3597 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
,
3598 struct nfs4_setclientid_res
*arg
,
3599 struct rpc_cred
*cred
)
3604 err
= _nfs4_proc_setclientid_confirm(clp
, arg
, cred
);
3608 case -NFS4ERR_RESOURCE
:
3609 /* The IBM lawyers misread another document! */
3610 case -NFS4ERR_DELAY
:
3612 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3618 struct nfs4_delegreturndata
{
3619 struct nfs4_delegreturnargs args
;
3620 struct nfs4_delegreturnres res
;
3622 nfs4_stateid stateid
;
3623 unsigned long timestamp
;
3624 struct nfs_fattr fattr
;
3628 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3630 struct nfs4_delegreturndata
*data
= calldata
;
3632 nfs4_sequence_done(data
->res
.server
, &data
->res
.seq_res
,
3635 switch (task
->tk_status
) {
3636 case -NFS4ERR_STALE_STATEID
:
3637 case -NFS4ERR_EXPIRED
:
3639 renew_lease(data
->res
.server
, data
->timestamp
);
3642 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
3644 nfs_restart_rpc(task
, data
->res
.server
->nfs_client
);
3648 data
->rpc_status
= task
->tk_status
;
3651 static void nfs4_delegreturn_release(void *calldata
)
3656 #if defined(CONFIG_NFS_V4_1)
3657 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3659 struct nfs4_delegreturndata
*d_data
;
3661 d_data
= (struct nfs4_delegreturndata
*)data
;
3663 if (nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
3664 &d_data
->args
.seq_args
,
3665 &d_data
->res
.seq_res
, 1, task
))
3667 rpc_call_start(task
);
3669 #endif /* CONFIG_NFS_V4_1 */
3671 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3672 #if defined(CONFIG_NFS_V4_1)
3673 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3674 #endif /* CONFIG_NFS_V4_1 */
3675 .rpc_call_done
= nfs4_delegreturn_done
,
3676 .rpc_release
= nfs4_delegreturn_release
,
3679 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3681 struct nfs4_delegreturndata
*data
;
3682 struct nfs_server
*server
= NFS_SERVER(inode
);
3683 struct rpc_task
*task
;
3684 struct rpc_message msg
= {
3685 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3688 struct rpc_task_setup task_setup_data
= {
3689 .rpc_client
= server
->client
,
3690 .rpc_message
= &msg
,
3691 .callback_ops
= &nfs4_delegreturn_ops
,
3692 .flags
= RPC_TASK_ASYNC
,
3696 data
= kzalloc(sizeof(*data
), GFP_NOFS
);
3699 data
->args
.fhandle
= &data
->fh
;
3700 data
->args
.stateid
= &data
->stateid
;
3701 data
->args
.bitmask
= server
->attr_bitmask
;
3702 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3703 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3704 data
->res
.fattr
= &data
->fattr
;
3705 data
->res
.server
= server
;
3706 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3707 nfs_fattr_init(data
->res
.fattr
);
3708 data
->timestamp
= jiffies
;
3709 data
->rpc_status
= 0;
3711 task_setup_data
.callback_data
= data
;
3712 msg
.rpc_argp
= &data
->args
,
3713 msg
.rpc_resp
= &data
->res
,
3714 task
= rpc_run_task(&task_setup_data
);
3716 return PTR_ERR(task
);
3719 status
= nfs4_wait_for_completion_rpc_task(task
);
3722 status
= data
->rpc_status
;
3725 nfs_refresh_inode(inode
, &data
->fattr
);
3731 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3733 struct nfs_server
*server
= NFS_SERVER(inode
);
3734 struct nfs4_exception exception
= { };
3737 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3739 case -NFS4ERR_STALE_STATEID
:
3740 case -NFS4ERR_EXPIRED
:
3744 err
= nfs4_handle_exception(server
, err
, &exception
);
3745 } while (exception
.retry
);
3749 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3750 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3753 * sleep, with exponential backoff, and retry the LOCK operation.
3755 static unsigned long
3756 nfs4_set_lock_task_retry(unsigned long timeout
)
3758 schedule_timeout_killable(timeout
);
3760 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3761 return NFS4_LOCK_MAXTIMEOUT
;
3765 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3767 struct inode
*inode
= state
->inode
;
3768 struct nfs_server
*server
= NFS_SERVER(inode
);
3769 struct nfs_client
*clp
= server
->nfs_client
;
3770 struct nfs_lockt_args arg
= {
3771 .fh
= NFS_FH(inode
),
3774 struct nfs_lockt_res res
= {
3777 struct rpc_message msg
= {
3778 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3781 .rpc_cred
= state
->owner
->so_cred
,
3783 struct nfs4_lock_state
*lsp
;
3786 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3787 status
= nfs4_set_lock_state(state
, request
);
3790 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3791 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3792 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3795 request
->fl_type
= F_UNLCK
;
3797 case -NFS4ERR_DENIED
:
3800 request
->fl_ops
->fl_release_private(request
);
3805 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3807 struct nfs4_exception exception
= { };
3811 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3812 _nfs4_proc_getlk(state
, cmd
, request
),
3814 } while (exception
.retry
);
3818 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3821 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3823 res
= posix_lock_file_wait(file
, fl
);
3826 res
= flock_lock_file_wait(file
, fl
);
3834 struct nfs4_unlockdata
{
3835 struct nfs_locku_args arg
;
3836 struct nfs_locku_res res
;
3837 struct nfs4_lock_state
*lsp
;
3838 struct nfs_open_context
*ctx
;
3839 struct file_lock fl
;
3840 const struct nfs_server
*server
;
3841 unsigned long timestamp
;
3844 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3845 struct nfs_open_context
*ctx
,
3846 struct nfs4_lock_state
*lsp
,
3847 struct nfs_seqid
*seqid
)
3849 struct nfs4_unlockdata
*p
;
3850 struct inode
*inode
= lsp
->ls_state
->inode
;
3852 p
= kzalloc(sizeof(*p
), GFP_NOFS
);
3855 p
->arg
.fh
= NFS_FH(inode
);
3857 p
->arg
.seqid
= seqid
;
3858 p
->res
.seqid
= seqid
;
3859 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3860 p
->arg
.stateid
= &lsp
->ls_stateid
;
3862 atomic_inc(&lsp
->ls_count
);
3863 /* Ensure we don't close file until we're done freeing locks! */
3864 p
->ctx
= get_nfs_open_context(ctx
);
3865 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3866 p
->server
= NFS_SERVER(inode
);
3870 static void nfs4_locku_release_calldata(void *data
)
3872 struct nfs4_unlockdata
*calldata
= data
;
3873 nfs_free_seqid(calldata
->arg
.seqid
);
3874 nfs4_put_lock_state(calldata
->lsp
);
3875 put_nfs_open_context(calldata
->ctx
);
3879 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3881 struct nfs4_unlockdata
*calldata
= data
;
3883 nfs4_sequence_done(calldata
->server
, &calldata
->res
.seq_res
,
3885 if (RPC_ASSASSINATED(task
))
3887 switch (task
->tk_status
) {
3889 memcpy(calldata
->lsp
->ls_stateid
.data
,
3890 calldata
->res
.stateid
.data
,
3891 sizeof(calldata
->lsp
->ls_stateid
.data
));
3892 renew_lease(calldata
->server
, calldata
->timestamp
);
3894 case -NFS4ERR_BAD_STATEID
:
3895 case -NFS4ERR_OLD_STATEID
:
3896 case -NFS4ERR_STALE_STATEID
:
3897 case -NFS4ERR_EXPIRED
:
3900 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3901 nfs_restart_rpc(task
,
3902 calldata
->server
->nfs_client
);
3906 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3908 struct nfs4_unlockdata
*calldata
= data
;
3910 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3912 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3913 /* Note: exit _without_ running nfs4_locku_done */
3914 task
->tk_action
= NULL
;
3917 calldata
->timestamp
= jiffies
;
3918 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
3919 &calldata
->arg
.seq_args
,
3920 &calldata
->res
.seq_res
, 1, task
))
3922 rpc_call_start(task
);
3925 static const struct rpc_call_ops nfs4_locku_ops
= {
3926 .rpc_call_prepare
= nfs4_locku_prepare
,
3927 .rpc_call_done
= nfs4_locku_done
,
3928 .rpc_release
= nfs4_locku_release_calldata
,
3931 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3932 struct nfs_open_context
*ctx
,
3933 struct nfs4_lock_state
*lsp
,
3934 struct nfs_seqid
*seqid
)
3936 struct nfs4_unlockdata
*data
;
3937 struct rpc_message msg
= {
3938 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3939 .rpc_cred
= ctx
->cred
,
3941 struct rpc_task_setup task_setup_data
= {
3942 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3943 .rpc_message
= &msg
,
3944 .callback_ops
= &nfs4_locku_ops
,
3945 .workqueue
= nfsiod_workqueue
,
3946 .flags
= RPC_TASK_ASYNC
,
3949 /* Ensure this is an unlock - when canceling a lock, the
3950 * canceled lock is passed in, and it won't be an unlock.
3952 fl
->fl_type
= F_UNLCK
;
3954 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3956 nfs_free_seqid(seqid
);
3957 return ERR_PTR(-ENOMEM
);
3960 msg
.rpc_argp
= &data
->arg
,
3961 msg
.rpc_resp
= &data
->res
,
3962 task_setup_data
.callback_data
= data
;
3963 return rpc_run_task(&task_setup_data
);
3966 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3968 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3969 struct nfs_seqid
*seqid
;
3970 struct nfs4_lock_state
*lsp
;
3971 struct rpc_task
*task
;
3973 unsigned char fl_flags
= request
->fl_flags
;
3975 status
= nfs4_set_lock_state(state
, request
);
3976 /* Unlock _before_ we do the RPC call */
3977 request
->fl_flags
|= FL_EXISTS
;
3978 down_read(&nfsi
->rwsem
);
3979 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3980 up_read(&nfsi
->rwsem
);
3983 up_read(&nfsi
->rwsem
);
3986 /* Is this a delegated lock? */
3987 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3989 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3990 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, GFP_KERNEL
);
3994 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3995 status
= PTR_ERR(task
);
3998 status
= nfs4_wait_for_completion_rpc_task(task
);
4001 request
->fl_flags
= fl_flags
;
4005 struct nfs4_lockdata
{
4006 struct nfs_lock_args arg
;
4007 struct nfs_lock_res res
;
4008 struct nfs4_lock_state
*lsp
;
4009 struct nfs_open_context
*ctx
;
4010 struct file_lock fl
;
4011 unsigned long timestamp
;
4014 struct nfs_server
*server
;
4017 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
4018 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
,
4021 struct nfs4_lockdata
*p
;
4022 struct inode
*inode
= lsp
->ls_state
->inode
;
4023 struct nfs_server
*server
= NFS_SERVER(inode
);
4025 p
= kzalloc(sizeof(*p
), gfp_mask
);
4029 p
->arg
.fh
= NFS_FH(inode
);
4031 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
, gfp_mask
);
4032 if (p
->arg
.open_seqid
== NULL
)
4034 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
, gfp_mask
);
4035 if (p
->arg
.lock_seqid
== NULL
)
4036 goto out_free_seqid
;
4037 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
4038 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4039 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
4040 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
4041 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4044 atomic_inc(&lsp
->ls_count
);
4045 p
->ctx
= get_nfs_open_context(ctx
);
4046 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4049 nfs_free_seqid(p
->arg
.open_seqid
);
4055 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
4057 struct nfs4_lockdata
*data
= calldata
;
4058 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4060 dprintk("%s: begin!\n", __func__
);
4061 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4063 /* Do we need to do an open_to_lock_owner? */
4064 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4065 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
4067 data
->arg
.open_stateid
= &state
->stateid
;
4068 data
->arg
.new_lock_owner
= 1;
4069 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4071 data
->arg
.new_lock_owner
= 0;
4072 data
->timestamp
= jiffies
;
4073 if (nfs4_setup_sequence(data
->server
->nfs_client
, &data
->arg
.seq_args
,
4074 &data
->res
.seq_res
, 1, task
))
4076 rpc_call_start(task
);
4077 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4080 static void nfs4_recover_lock_prepare(struct rpc_task
*task
, void *calldata
)
4082 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4083 nfs4_lock_prepare(task
, calldata
);
4086 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4088 struct nfs4_lockdata
*data
= calldata
;
4090 dprintk("%s: begin!\n", __func__
);
4092 nfs4_sequence_done(data
->server
, &data
->res
.seq_res
,
4095 data
->rpc_status
= task
->tk_status
;
4096 if (RPC_ASSASSINATED(task
))
4098 if (data
->arg
.new_lock_owner
!= 0) {
4099 if (data
->rpc_status
== 0)
4100 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4104 if (data
->rpc_status
== 0) {
4105 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
4106 sizeof(data
->lsp
->ls_stateid
.data
));
4107 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
4108 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4111 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4114 static void nfs4_lock_release(void *calldata
)
4116 struct nfs4_lockdata
*data
= calldata
;
4118 dprintk("%s: begin!\n", __func__
);
4119 nfs_free_seqid(data
->arg
.open_seqid
);
4120 if (data
->cancelled
!= 0) {
4121 struct rpc_task
*task
;
4122 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4123 data
->arg
.lock_seqid
);
4126 dprintk("%s: cancelling lock!\n", __func__
);
4128 nfs_free_seqid(data
->arg
.lock_seqid
);
4129 nfs4_put_lock_state(data
->lsp
);
4130 put_nfs_open_context(data
->ctx
);
4132 dprintk("%s: done!\n", __func__
);
4135 static const struct rpc_call_ops nfs4_lock_ops
= {
4136 .rpc_call_prepare
= nfs4_lock_prepare
,
4137 .rpc_call_done
= nfs4_lock_done
,
4138 .rpc_release
= nfs4_lock_release
,
4141 static const struct rpc_call_ops nfs4_recover_lock_ops
= {
4142 .rpc_call_prepare
= nfs4_recover_lock_prepare
,
4143 .rpc_call_done
= nfs4_lock_done
,
4144 .rpc_release
= nfs4_lock_release
,
4147 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4149 struct nfs_client
*clp
= server
->nfs_client
;
4150 struct nfs4_state
*state
= lsp
->ls_state
;
4153 case -NFS4ERR_ADMIN_REVOKED
:
4154 case -NFS4ERR_BAD_STATEID
:
4155 case -NFS4ERR_EXPIRED
:
4156 if (new_lock_owner
!= 0 ||
4157 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4158 nfs4_state_mark_reclaim_nograce(clp
, state
);
4159 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4161 case -NFS4ERR_STALE_STATEID
:
4162 if (new_lock_owner
!= 0 ||
4163 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4164 nfs4_state_mark_reclaim_reboot(clp
, state
);
4165 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4169 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4171 struct nfs4_lockdata
*data
;
4172 struct rpc_task
*task
;
4173 struct rpc_message msg
= {
4174 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4175 .rpc_cred
= state
->owner
->so_cred
,
4177 struct rpc_task_setup task_setup_data
= {
4178 .rpc_client
= NFS_CLIENT(state
->inode
),
4179 .rpc_message
= &msg
,
4180 .callback_ops
= &nfs4_lock_ops
,
4181 .workqueue
= nfsiod_workqueue
,
4182 .flags
= RPC_TASK_ASYNC
,
4186 dprintk("%s: begin!\n", __func__
);
4187 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4188 fl
->fl_u
.nfs4_fl
.owner
,
4189 recovery_type
== NFS_LOCK_NEW
? GFP_KERNEL
: GFP_NOFS
);
4193 data
->arg
.block
= 1;
4194 if (recovery_type
> NFS_LOCK_NEW
) {
4195 if (recovery_type
== NFS_LOCK_RECLAIM
)
4196 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4197 task_setup_data
.callback_ops
= &nfs4_recover_lock_ops
;
4199 msg
.rpc_argp
= &data
->arg
,
4200 msg
.rpc_resp
= &data
->res
,
4201 task_setup_data
.callback_data
= data
;
4202 task
= rpc_run_task(&task_setup_data
);
4204 return PTR_ERR(task
);
4205 ret
= nfs4_wait_for_completion_rpc_task(task
);
4207 ret
= data
->rpc_status
;
4209 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4210 data
->arg
.new_lock_owner
, ret
);
4212 data
->cancelled
= 1;
4214 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4218 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4220 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4221 struct nfs4_exception exception
= { };
4225 /* Cache the lock if possible... */
4226 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4228 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4229 if (err
!= -NFS4ERR_DELAY
&& err
!= -EKEYEXPIRED
)
4231 nfs4_handle_exception(server
, err
, &exception
);
4232 } while (exception
.retry
);
4236 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4238 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4239 struct nfs4_exception exception
= { };
4242 err
= nfs4_set_lock_state(state
, request
);
4246 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4248 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4252 case -NFS4ERR_GRACE
:
4253 case -NFS4ERR_DELAY
:
4255 nfs4_handle_exception(server
, err
, &exception
);
4258 } while (exception
.retry
);
4263 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4265 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4266 unsigned char fl_flags
= request
->fl_flags
;
4267 int status
= -ENOLCK
;
4269 if ((fl_flags
& FL_POSIX
) &&
4270 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4272 /* Is this a delegated open? */
4273 status
= nfs4_set_lock_state(state
, request
);
4276 request
->fl_flags
|= FL_ACCESS
;
4277 status
= do_vfs_lock(request
->fl_file
, request
);
4280 down_read(&nfsi
->rwsem
);
4281 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4282 /* Yes: cache locks! */
4283 /* ...but avoid races with delegation recall... */
4284 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4285 status
= do_vfs_lock(request
->fl_file
, request
);
4288 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4291 /* Note: we always want to sleep here! */
4292 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4293 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4294 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4296 up_read(&nfsi
->rwsem
);
4298 request
->fl_flags
= fl_flags
;
4302 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4304 struct nfs4_exception exception
= { };
4308 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4309 if (err
== -NFS4ERR_DENIED
)
4311 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4313 } while (exception
.retry
);
4318 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4320 struct nfs_open_context
*ctx
;
4321 struct nfs4_state
*state
;
4322 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4325 /* verify open state */
4326 ctx
= nfs_file_open_context(filp
);
4329 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4332 if (IS_GETLK(cmd
)) {
4334 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4338 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4341 if (request
->fl_type
== F_UNLCK
) {
4343 return nfs4_proc_unlck(state
, cmd
, request
);
4350 status
= nfs4_proc_setlk(state
, cmd
, request
);
4351 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4353 timeout
= nfs4_set_lock_task_retry(timeout
);
4354 status
= -ERESTARTSYS
;
4357 } while(status
< 0);
4361 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4363 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4364 struct nfs4_exception exception
= { };
4367 err
= nfs4_set_lock_state(state
, fl
);
4371 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4374 printk(KERN_ERR
"%s: unhandled error %d.\n",
4379 case -NFS4ERR_EXPIRED
:
4380 case -NFS4ERR_STALE_CLIENTID
:
4381 case -NFS4ERR_STALE_STATEID
:
4382 case -NFS4ERR_BADSESSION
:
4383 case -NFS4ERR_BADSLOT
:
4384 case -NFS4ERR_BAD_HIGH_SLOT
:
4385 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4386 case -NFS4ERR_DEADSESSION
:
4387 nfs4_schedule_state_recovery(server
->nfs_client
);
4391 * The show must go on: exit, but mark the
4392 * stateid as needing recovery.
4394 case -NFS4ERR_ADMIN_REVOKED
:
4395 case -NFS4ERR_BAD_STATEID
:
4396 case -NFS4ERR_OPENMODE
:
4397 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
4401 case -NFS4ERR_DENIED
:
4402 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4405 case -NFS4ERR_DELAY
:
4409 err
= nfs4_handle_exception(server
, err
, &exception
);
4410 } while (exception
.retry
);
4415 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4417 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4418 size_t buflen
, int flags
)
4420 struct inode
*inode
= dentry
->d_inode
;
4422 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4425 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4428 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4429 * and that's what we'll do for e.g. user attributes that haven't been set.
4430 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4431 * attributes in kernel-managed attribute namespaces. */
4432 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4435 struct inode
*inode
= dentry
->d_inode
;
4437 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4440 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4443 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4445 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4447 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4449 if (buf
&& buflen
< len
)
4452 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4456 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4458 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4459 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4460 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4463 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4464 NFS_ATTR_FATTR_NLINK
;
4465 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4469 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4470 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4472 struct nfs_server
*server
= NFS_SERVER(dir
);
4474 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4475 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4477 struct nfs4_fs_locations_arg args
= {
4478 .dir_fh
= NFS_FH(dir
),
4483 struct nfs4_fs_locations_res res
= {
4484 .fs_locations
= fs_locations
,
4486 struct rpc_message msg
= {
4487 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4493 dprintk("%s: start\n", __func__
);
4494 nfs_fattr_init(&fs_locations
->fattr
);
4495 fs_locations
->server
= server
;
4496 fs_locations
->nlocations
= 0;
4497 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4498 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4499 dprintk("%s: returned status = %d\n", __func__
, status
);
4503 #ifdef CONFIG_NFS_V4_1
4505 * nfs4_proc_exchange_id()
4507 * Since the clientid has expired, all compounds using sessions
4508 * associated with the stale clientid will be returning
4509 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4510 * be in some phase of session reset.
4512 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4514 nfs4_verifier verifier
;
4515 struct nfs41_exchange_id_args args
= {
4517 .flags
= clp
->cl_exchange_flags
,
4519 struct nfs41_exchange_id_res res
= {
4523 struct rpc_message msg
= {
4524 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4531 dprintk("--> %s\n", __func__
);
4532 BUG_ON(clp
== NULL
);
4534 /* Remove server-only flags */
4535 args
.flags
&= ~EXCHGID4_FLAG_CONFIRMED_R
;
4537 p
= (u32
*)verifier
.data
;
4538 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4539 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4540 args
.verifier
= &verifier
;
4543 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4546 rpc_peeraddr2str(clp
->cl_rpcclient
,
4548 clp
->cl_id_uniquifier
);
4550 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4552 if (status
!= -NFS4ERR_CLID_INUSE
)
4558 if (++clp
->cl_id_uniquifier
== 0)
4562 dprintk("<-- %s status= %d\n", __func__
, status
);
4566 struct nfs4_get_lease_time_data
{
4567 struct nfs4_get_lease_time_args
*args
;
4568 struct nfs4_get_lease_time_res
*res
;
4569 struct nfs_client
*clp
;
4572 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4576 struct nfs4_get_lease_time_data
*data
=
4577 (struct nfs4_get_lease_time_data
*)calldata
;
4579 dprintk("--> %s\n", __func__
);
4580 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4581 /* just setup sequence, do not trigger session recovery
4582 since we're invoked within one */
4583 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4584 &data
->args
->la_seq_args
,
4585 &data
->res
->lr_seq_res
, 0, task
);
4587 BUG_ON(ret
== -EAGAIN
);
4588 rpc_call_start(task
);
4589 dprintk("<-- %s\n", __func__
);
4593 * Called from nfs4_state_manager thread for session setup, so don't recover
4594 * from sequence operation or clientid errors.
4596 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4598 struct nfs4_get_lease_time_data
*data
=
4599 (struct nfs4_get_lease_time_data
*)calldata
;
4601 dprintk("--> %s\n", __func__
);
4602 nfs41_sequence_done(&data
->res
->lr_seq_res
);
4603 switch (task
->tk_status
) {
4604 case -NFS4ERR_DELAY
:
4605 case -NFS4ERR_GRACE
:
4607 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4608 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4609 task
->tk_status
= 0;
4610 nfs_restart_rpc(task
, data
->clp
);
4613 dprintk("<-- %s\n", __func__
);
4616 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4617 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4618 .rpc_call_done
= nfs4_get_lease_time_done
,
4621 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4623 struct rpc_task
*task
;
4624 struct nfs4_get_lease_time_args args
;
4625 struct nfs4_get_lease_time_res res
= {
4626 .lr_fsinfo
= fsinfo
,
4628 struct nfs4_get_lease_time_data data
= {
4633 struct rpc_message msg
= {
4634 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4638 struct rpc_task_setup task_setup
= {
4639 .rpc_client
= clp
->cl_rpcclient
,
4640 .rpc_message
= &msg
,
4641 .callback_ops
= &nfs4_get_lease_time_ops
,
4642 .callback_data
= &data
4646 res
.lr_seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4647 dprintk("--> %s\n", __func__
);
4648 task
= rpc_run_task(&task_setup
);
4651 status
= PTR_ERR(task
);
4653 status
= task
->tk_status
;
4656 dprintk("<-- %s return %d\n", __func__
, status
);
4662 * Reset a slot table
4664 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, u32 max_reqs
,
4667 struct nfs4_slot
*new = NULL
;
4671 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__
,
4672 max_reqs
, tbl
->max_slots
);
4674 /* Does the newly negotiated max_reqs match the existing slot table? */
4675 if (max_reqs
!= tbl
->max_slots
) {
4677 new = kmalloc(max_reqs
* sizeof(struct nfs4_slot
),
4684 spin_lock(&tbl
->slot_tbl_lock
);
4687 tbl
->max_slots
= max_reqs
;
4689 for (i
= 0; i
< tbl
->max_slots
; ++i
)
4690 tbl
->slots
[i
].seq_nr
= ivalue
;
4691 spin_unlock(&tbl
->slot_tbl_lock
);
4692 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4693 tbl
, tbl
->slots
, tbl
->max_slots
);
4695 dprintk("<-- %s: return %d\n", __func__
, ret
);
4700 * Reset the forechannel and backchannel slot tables
4702 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
4706 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
4707 session
->fc_attrs
.max_reqs
, 1);
4711 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
4712 session
->bc_attrs
.max_reqs
, 0);
4716 /* Destroy the slot table */
4717 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
4719 if (session
->fc_slot_table
.slots
!= NULL
) {
4720 kfree(session
->fc_slot_table
.slots
);
4721 session
->fc_slot_table
.slots
= NULL
;
4723 if (session
->bc_slot_table
.slots
!= NULL
) {
4724 kfree(session
->bc_slot_table
.slots
);
4725 session
->bc_slot_table
.slots
= NULL
;
4731 * Initialize slot table
4733 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
4734 int max_slots
, int ivalue
)
4736 struct nfs4_slot
*slot
;
4739 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4741 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
4743 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_NOFS
);
4748 spin_lock(&tbl
->slot_tbl_lock
);
4749 tbl
->max_slots
= max_slots
;
4751 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4752 spin_unlock(&tbl
->slot_tbl_lock
);
4753 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4754 tbl
, tbl
->slots
, tbl
->max_slots
);
4756 dprintk("<-- %s: return %d\n", __func__
, ret
);
4761 * Initialize the forechannel and backchannel tables
4763 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
4765 struct nfs4_slot_table
*tbl
;
4768 tbl
= &session
->fc_slot_table
;
4769 if (tbl
->slots
== NULL
) {
4770 status
= nfs4_init_slot_table(tbl
,
4771 session
->fc_attrs
.max_reqs
, 1);
4776 tbl
= &session
->bc_slot_table
;
4777 if (tbl
->slots
== NULL
) {
4778 status
= nfs4_init_slot_table(tbl
,
4779 session
->bc_attrs
.max_reqs
, 0);
4781 nfs4_destroy_slot_tables(session
);
4787 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4789 struct nfs4_session
*session
;
4790 struct nfs4_slot_table
*tbl
;
4792 session
= kzalloc(sizeof(struct nfs4_session
), GFP_NOFS
);
4797 * The create session reply races with the server back
4798 * channel probe. Mark the client NFS_CS_SESSION_INITING
4799 * so that the client back channel can find the
4802 clp
->cl_cons_state
= NFS_CS_SESSION_INITING
;
4803 init_completion(&session
->complete
);
4805 tbl
= &session
->fc_slot_table
;
4806 tbl
->highest_used_slotid
= -1;
4807 spin_lock_init(&tbl
->slot_tbl_lock
);
4808 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
4810 tbl
= &session
->bc_slot_table
;
4811 tbl
->highest_used_slotid
= -1;
4812 spin_lock_init(&tbl
->slot_tbl_lock
);
4813 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
4819 void nfs4_destroy_session(struct nfs4_session
*session
)
4821 nfs4_proc_destroy_session(session
);
4822 dprintk("%s Destroy backchannel for xprt %p\n",
4823 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
4824 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
4825 NFS41_BC_MIN_CALLBACKS
);
4826 nfs4_destroy_slot_tables(session
);
4831 * Initialize the values to be used by the client in CREATE_SESSION
4832 * If nfs4_init_session set the fore channel request and response sizes,
4835 * Set the back channel max_resp_sz_cached to zero to force the client to
4836 * always set csa_cachethis to FALSE because the current implementation
4837 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4839 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4841 struct nfs4_session
*session
= args
->client
->cl_session
;
4842 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4843 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4846 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4848 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4849 /* Fore channel attributes */
4850 args
->fc_attrs
.headerpadsz
= 0;
4851 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4852 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4853 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4854 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4856 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4857 "max_ops=%u max_reqs=%u\n",
4859 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4860 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
4862 /* Back channel attributes */
4863 args
->bc_attrs
.headerpadsz
= 0;
4864 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4865 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4866 args
->bc_attrs
.max_resp_sz_cached
= 0;
4867 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4868 args
->bc_attrs
.max_reqs
= 1;
4870 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4871 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4873 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4874 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4875 args
->bc_attrs
.max_reqs
);
4878 static int _verify_channel_attr(char *chan
, char *attr_name
, u32 sent
, u32 rcvd
)
4882 printk(KERN_WARNING
"%s: Session INVALID: %s channel %s increased. "
4883 "sent=%u rcvd=%u\n", __func__
, chan
, attr_name
, sent
, rcvd
);
4887 #define _verify_fore_channel_attr(_name_) \
4888 _verify_channel_attr("fore", #_name_, \
4889 args->fc_attrs._name_, \
4890 session->fc_attrs._name_)
4892 #define _verify_back_channel_attr(_name_) \
4893 _verify_channel_attr("back", #_name_, \
4894 args->bc_attrs._name_, \
4895 session->bc_attrs._name_)
4898 * The server is not allowed to increase the fore channel header pad size,
4899 * maximum response size, or maximum number of operations.
4901 * The back channel attributes are only negotiatied down: We send what the
4902 * (back channel) server insists upon.
4904 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4905 struct nfs4_session
*session
)
4909 ret
|= _verify_fore_channel_attr(headerpadsz
);
4910 ret
|= _verify_fore_channel_attr(max_resp_sz
);
4911 ret
|= _verify_fore_channel_attr(max_ops
);
4913 ret
|= _verify_back_channel_attr(headerpadsz
);
4914 ret
|= _verify_back_channel_attr(max_rqst_sz
);
4915 ret
|= _verify_back_channel_attr(max_resp_sz
);
4916 ret
|= _verify_back_channel_attr(max_resp_sz_cached
);
4917 ret
|= _verify_back_channel_attr(max_ops
);
4918 ret
|= _verify_back_channel_attr(max_reqs
);
4923 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4925 struct nfs4_session
*session
= clp
->cl_session
;
4926 struct nfs41_create_session_args args
= {
4928 .cb_program
= NFS4_CALLBACK
,
4930 struct nfs41_create_session_res res
= {
4933 struct rpc_message msg
= {
4934 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4940 nfs4_init_channel_attrs(&args
);
4941 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
4943 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4946 /* Verify the session's negotiated channel_attrs values */
4947 status
= nfs4_verify_channel_attrs(&args
, session
);
4949 /* Increment the clientid slot sequence id */
4957 * Issues a CREATE_SESSION operation to the server.
4958 * It is the responsibility of the caller to verify the session is
4959 * expired before calling this routine.
4961 int nfs4_proc_create_session(struct nfs_client
*clp
)
4965 struct nfs4_session
*session
= clp
->cl_session
;
4967 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4969 status
= _nfs4_proc_create_session(clp
);
4973 /* Init and reset the fore channel */
4974 status
= nfs4_init_slot_tables(session
);
4975 dprintk("slot table initialization returned %d\n", status
);
4978 status
= nfs4_reset_slot_tables(session
);
4979 dprintk("slot table reset returned %d\n", status
);
4983 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4984 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4985 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4987 dprintk("<-- %s\n", __func__
);
4992 * Issue the over-the-wire RPC DESTROY_SESSION.
4993 * The caller must serialize access to this routine.
4995 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4998 struct rpc_message msg
;
5000 dprintk("--> nfs4_proc_destroy_session\n");
5002 /* session is still being setup */
5003 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
5006 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
5007 msg
.rpc_argp
= session
;
5008 msg
.rpc_resp
= NULL
;
5009 msg
.rpc_cred
= NULL
;
5010 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
5014 "Got error %d from the server on DESTROY_SESSION. "
5015 "Session has been destroyed regardless...\n", status
);
5017 dprintk("<-- nfs4_proc_destroy_session\n");
5021 int nfs4_init_session(struct nfs_server
*server
)
5023 struct nfs_client
*clp
= server
->nfs_client
;
5024 struct nfs4_session
*session
;
5025 unsigned int rsize
, wsize
;
5028 if (!nfs4_has_session(clp
))
5031 rsize
= server
->rsize
;
5033 rsize
= NFS_MAX_FILE_IO_SIZE
;
5034 wsize
= server
->wsize
;
5036 wsize
= NFS_MAX_FILE_IO_SIZE
;
5038 session
= clp
->cl_session
;
5039 session
->fc_attrs
.max_rqst_sz
= wsize
+ nfs41_maxwrite_overhead
;
5040 session
->fc_attrs
.max_resp_sz
= rsize
+ nfs41_maxread_overhead
;
5042 ret
= nfs4_recover_expired_lease(server
);
5044 ret
= nfs4_check_client_ready(clp
);
5049 * Renew the cl_session lease.
5051 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5053 struct nfs4_sequence_args args
;
5054 struct nfs4_sequence_res res
;
5056 struct rpc_message msg
= {
5057 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5063 args
.sa_cache_this
= 0;
5065 return nfs4_call_sync_sequence(clp
, clp
->cl_rpcclient
, &msg
, &args
,
5066 &res
, args
.sa_cache_this
, 1);
5069 struct nfs4_sequence_data
{
5070 struct nfs_client
*clp
;
5071 struct nfs4_sequence_args args
;
5072 struct nfs4_sequence_res res
;
5075 static void nfs41_sequence_release(void *data
)
5077 struct nfs4_sequence_data
*calldata
= data
;
5078 struct nfs_client
*clp
= calldata
->clp
;
5080 if (atomic_read(&clp
->cl_count
) > 1)
5081 nfs4_schedule_state_renewal(clp
);
5082 nfs_put_client(clp
);
5086 static int nfs41_sequence_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5088 switch(task
->tk_status
) {
5089 case -NFS4ERR_DELAY
:
5091 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5094 nfs4_schedule_state_recovery(clp
);
5099 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5101 struct nfs4_sequence_data
*calldata
= data
;
5102 struct nfs_client
*clp
= calldata
->clp
;
5104 nfs41_sequence_done(task
->tk_msg
.rpc_resp
);
5106 if (task
->tk_status
< 0) {
5107 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5108 if (atomic_read(&clp
->cl_count
) == 1)
5111 if (nfs41_sequence_handle_errors(task
, clp
) == -EAGAIN
) {
5112 rpc_restart_call_prepare(task
);
5116 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5118 dprintk("<-- %s\n", __func__
);
5121 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5123 struct nfs4_sequence_data
*calldata
= data
;
5124 struct nfs_client
*clp
= calldata
->clp
;
5125 struct nfs4_sequence_args
*args
;
5126 struct nfs4_sequence_res
*res
;
5128 args
= task
->tk_msg
.rpc_argp
;
5129 res
= task
->tk_msg
.rpc_resp
;
5131 if (nfs4_setup_sequence(clp
, args
, res
, 0, task
))
5133 rpc_call_start(task
);
5136 static const struct rpc_call_ops nfs41_sequence_ops
= {
5137 .rpc_call_done
= nfs41_sequence_call_done
,
5138 .rpc_call_prepare
= nfs41_sequence_prepare
,
5139 .rpc_release
= nfs41_sequence_release
,
5142 static int nfs41_proc_async_sequence(struct nfs_client
*clp
,
5143 struct rpc_cred
*cred
)
5145 struct nfs4_sequence_data
*calldata
;
5146 struct rpc_message msg
= {
5147 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5151 if (!atomic_inc_not_zero(&clp
->cl_count
))
5153 calldata
= kmalloc(sizeof(*calldata
), GFP_NOFS
);
5154 if (calldata
== NULL
) {
5155 nfs_put_client(clp
);
5158 calldata
->res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5159 msg
.rpc_argp
= &calldata
->args
;
5160 msg
.rpc_resp
= &calldata
->res
;
5161 calldata
->clp
= clp
;
5163 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
5164 &nfs41_sequence_ops
, calldata
);
5167 struct nfs4_reclaim_complete_data
{
5168 struct nfs_client
*clp
;
5169 struct nfs41_reclaim_complete_args arg
;
5170 struct nfs41_reclaim_complete_res res
;
5173 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5175 struct nfs4_reclaim_complete_data
*calldata
= data
;
5177 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
5178 if (nfs4_setup_sequence(calldata
->clp
, &calldata
->arg
.seq_args
,
5179 &calldata
->res
.seq_res
, 0, task
))
5182 rpc_call_start(task
);
5185 static int nfs41_reclaim_complete_handle_errors(struct rpc_task
*task
, struct nfs_client
*clp
)
5187 switch(task
->tk_status
) {
5189 case -NFS4ERR_COMPLETE_ALREADY
:
5190 case -NFS4ERR_WRONG_CRED
: /* What to do here? */
5192 case -NFS4ERR_DELAY
:
5194 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
5197 nfs4_schedule_state_recovery(clp
);
5202 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5204 struct nfs4_reclaim_complete_data
*calldata
= data
;
5205 struct nfs_client
*clp
= calldata
->clp
;
5206 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5208 dprintk("--> %s\n", __func__
);
5209 nfs41_sequence_done(res
);
5211 if (nfs41_reclaim_complete_handle_errors(task
, clp
) == -EAGAIN
) {
5212 rpc_restart_call_prepare(task
);
5215 dprintk("<-- %s\n", __func__
);
5218 static void nfs4_free_reclaim_complete_data(void *data
)
5220 struct nfs4_reclaim_complete_data
*calldata
= data
;
5225 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5226 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5227 .rpc_call_done
= nfs4_reclaim_complete_done
,
5228 .rpc_release
= nfs4_free_reclaim_complete_data
,
5232 * Issue a global reclaim complete.
5234 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5236 struct nfs4_reclaim_complete_data
*calldata
;
5237 struct rpc_task
*task
;
5238 struct rpc_message msg
= {
5239 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5241 struct rpc_task_setup task_setup_data
= {
5242 .rpc_client
= clp
->cl_rpcclient
,
5243 .rpc_message
= &msg
,
5244 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5245 .flags
= RPC_TASK_ASYNC
,
5247 int status
= -ENOMEM
;
5249 dprintk("--> %s\n", __func__
);
5250 calldata
= kzalloc(sizeof(*calldata
), GFP_NOFS
);
5251 if (calldata
== NULL
)
5253 calldata
->clp
= clp
;
5254 calldata
->arg
.one_fs
= 0;
5255 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5257 msg
.rpc_argp
= &calldata
->arg
;
5258 msg
.rpc_resp
= &calldata
->res
;
5259 task_setup_data
.callback_data
= calldata
;
5260 task
= rpc_run_task(&task_setup_data
);
5262 status
= PTR_ERR(task
);
5268 dprintk("<-- %s status=%d\n", __func__
, status
);
5271 #endif /* CONFIG_NFS_V4_1 */
5273 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5274 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5275 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5276 .recover_open
= nfs4_open_reclaim
,
5277 .recover_lock
= nfs4_lock_reclaim
,
5278 .establish_clid
= nfs4_init_clientid
,
5279 .get_clid_cred
= nfs4_get_setclientid_cred
,
5282 #if defined(CONFIG_NFS_V4_1)
5283 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5284 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5285 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5286 .recover_open
= nfs4_open_reclaim
,
5287 .recover_lock
= nfs4_lock_reclaim
,
5288 .establish_clid
= nfs41_init_clientid
,
5289 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5290 .reclaim_complete
= nfs41_proc_reclaim_complete
,
5292 #endif /* CONFIG_NFS_V4_1 */
5294 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5295 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5296 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5297 .recover_open
= nfs4_open_expired
,
5298 .recover_lock
= nfs4_lock_expired
,
5299 .establish_clid
= nfs4_init_clientid
,
5300 .get_clid_cred
= nfs4_get_setclientid_cred
,
5303 #if defined(CONFIG_NFS_V4_1)
5304 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5305 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5306 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5307 .recover_open
= nfs4_open_expired
,
5308 .recover_lock
= nfs4_lock_expired
,
5309 .establish_clid
= nfs41_init_clientid
,
5310 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5312 #endif /* CONFIG_NFS_V4_1 */
5314 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
5315 .sched_state_renewal
= nfs4_proc_async_renew
,
5316 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
5317 .renew_lease
= nfs4_proc_renew
,
5320 #if defined(CONFIG_NFS_V4_1)
5321 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
5322 .sched_state_renewal
= nfs41_proc_async_sequence
,
5323 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
5324 .renew_lease
= nfs4_proc_sequence
,
5329 * Per minor version reboot and network partition recovery ops
5332 struct nfs4_state_recovery_ops
*nfs4_reboot_recovery_ops
[] = {
5333 &nfs40_reboot_recovery_ops
,
5334 #if defined(CONFIG_NFS_V4_1)
5335 &nfs41_reboot_recovery_ops
,
5339 struct nfs4_state_recovery_ops
*nfs4_nograce_recovery_ops
[] = {
5340 &nfs40_nograce_recovery_ops
,
5341 #if defined(CONFIG_NFS_V4_1)
5342 &nfs41_nograce_recovery_ops
,
5346 struct nfs4_state_maintenance_ops
*nfs4_state_renewal_ops
[] = {
5347 &nfs40_state_renewal_ops
,
5348 #if defined(CONFIG_NFS_V4_1)
5349 &nfs41_state_renewal_ops
,
5353 static const struct inode_operations nfs4_file_inode_operations
= {
5354 .permission
= nfs_permission
,
5355 .getattr
= nfs_getattr
,
5356 .setattr
= nfs_setattr
,
5357 .getxattr
= nfs4_getxattr
,
5358 .setxattr
= nfs4_setxattr
,
5359 .listxattr
= nfs4_listxattr
,
5362 const struct nfs_rpc_ops nfs_v4_clientops
= {
5363 .version
= 4, /* protocol version */
5364 .dentry_ops
= &nfs4_dentry_operations
,
5365 .dir_inode_ops
= &nfs4_dir_inode_operations
,
5366 .file_inode_ops
= &nfs4_file_inode_operations
,
5367 .getroot
= nfs4_proc_get_root
,
5368 .getattr
= nfs4_proc_getattr
,
5369 .setattr
= nfs4_proc_setattr
,
5370 .lookupfh
= nfs4_proc_lookupfh
,
5371 .lookup
= nfs4_proc_lookup
,
5372 .access
= nfs4_proc_access
,
5373 .readlink
= nfs4_proc_readlink
,
5374 .create
= nfs4_proc_create
,
5375 .remove
= nfs4_proc_remove
,
5376 .unlink_setup
= nfs4_proc_unlink_setup
,
5377 .unlink_done
= nfs4_proc_unlink_done
,
5378 .rename
= nfs4_proc_rename
,
5379 .link
= nfs4_proc_link
,
5380 .symlink
= nfs4_proc_symlink
,
5381 .mkdir
= nfs4_proc_mkdir
,
5382 .rmdir
= nfs4_proc_remove
,
5383 .readdir
= nfs4_proc_readdir
,
5384 .mknod
= nfs4_proc_mknod
,
5385 .statfs
= nfs4_proc_statfs
,
5386 .fsinfo
= nfs4_proc_fsinfo
,
5387 .pathconf
= nfs4_proc_pathconf
,
5388 .set_capabilities
= nfs4_server_capabilities
,
5389 .decode_dirent
= nfs4_decode_dirent
,
5390 .read_setup
= nfs4_proc_read_setup
,
5391 .read_done
= nfs4_read_done
,
5392 .write_setup
= nfs4_proc_write_setup
,
5393 .write_done
= nfs4_write_done
,
5394 .commit_setup
= nfs4_proc_commit_setup
,
5395 .commit_done
= nfs4_commit_done
,
5396 .lock
= nfs4_proc_lock
,
5397 .clear_acl_cache
= nfs4_zap_acl_attr
,
5398 .close_context
= nfs4_close_context
,