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/utsname.h>
40 #include <linux/delay.h>
41 #include <linux/errno.h>
42 #include <linux/string.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/smp_lock.h>
49 #include <linux/namei.h>
50 #include <linux/mount.h>
51 #include <linux/module.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)
65 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
66 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
67 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
68 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
69 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
71 /* Prevent leaks of NFSv4 errors into userland */
72 static int nfs4_map_errors(int err
)
75 dprintk("%s could not handle NFSv4 error %d\n",
83 * This is our standard bitmap for GETATTR requests.
85 const u32 nfs4_fattr_bitmap
[2] = {
90 | FATTR4_WORD0_FILEID
,
92 | FATTR4_WORD1_NUMLINKS
94 | FATTR4_WORD1_OWNER_GROUP
96 | FATTR4_WORD1_SPACE_USED
97 | FATTR4_WORD1_TIME_ACCESS
98 | FATTR4_WORD1_TIME_METADATA
99 | FATTR4_WORD1_TIME_MODIFY
102 const u32 nfs4_statfs_bitmap
[2] = {
103 FATTR4_WORD0_FILES_AVAIL
104 | FATTR4_WORD0_FILES_FREE
105 | FATTR4_WORD0_FILES_TOTAL
,
106 FATTR4_WORD1_SPACE_AVAIL
107 | FATTR4_WORD1_SPACE_FREE
108 | FATTR4_WORD1_SPACE_TOTAL
111 const u32 nfs4_pathconf_bitmap
[2] = {
113 | FATTR4_WORD0_MAXNAME
,
117 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
118 | FATTR4_WORD0_MAXREAD
119 | FATTR4_WORD0_MAXWRITE
120 | FATTR4_WORD0_LEASE_TIME
,
124 const u32 nfs4_fs_locations_bitmap
[2] = {
126 | FATTR4_WORD0_CHANGE
129 | FATTR4_WORD0_FILEID
130 | FATTR4_WORD0_FS_LOCATIONS
,
132 | FATTR4_WORD1_NUMLINKS
134 | FATTR4_WORD1_OWNER_GROUP
135 | FATTR4_WORD1_RAWDEV
136 | FATTR4_WORD1_SPACE_USED
137 | FATTR4_WORD1_TIME_ACCESS
138 | FATTR4_WORD1_TIME_METADATA
139 | FATTR4_WORD1_TIME_MODIFY
140 | FATTR4_WORD1_MOUNTED_ON_FILEID
143 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
144 struct nfs4_readdir_arg
*readdir
)
148 BUG_ON(readdir
->count
< 80);
150 readdir
->cookie
= cookie
;
151 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
156 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
161 * NFSv4 servers do not return entries for '.' and '..'
162 * Therefore, we fake these entries here. We let '.'
163 * have cookie 0 and '..' have cookie 1. Note that
164 * when talking to the server, we always send cookie 0
167 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
170 *p
++ = xdr_one
; /* next */
171 *p
++ = xdr_zero
; /* cookie, first word */
172 *p
++ = xdr_one
; /* cookie, second word */
173 *p
++ = xdr_one
; /* entry len */
174 memcpy(p
, ".\0\0\0", 4); /* entry */
176 *p
++ = xdr_one
; /* bitmap length */
177 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
178 *p
++ = htonl(8); /* attribute buffer length */
179 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
182 *p
++ = xdr_one
; /* next */
183 *p
++ = xdr_zero
; /* cookie, first word */
184 *p
++ = xdr_two
; /* cookie, second word */
185 *p
++ = xdr_two
; /* entry len */
186 memcpy(p
, "..\0\0", 4); /* entry */
188 *p
++ = xdr_one
; /* bitmap length */
189 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
190 *p
++ = htonl(8); /* attribute buffer length */
191 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
193 readdir
->pgbase
= (char *)p
- (char *)start
;
194 readdir
->count
-= readdir
->pgbase
;
195 kunmap_atomic(start
, KM_USER0
);
198 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
204 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
205 nfs_wait_bit_killable
, TASK_KILLABLE
);
209 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
216 *timeout
= NFS4_POLL_RETRY_MIN
;
217 if (*timeout
> NFS4_POLL_RETRY_MAX
)
218 *timeout
= NFS4_POLL_RETRY_MAX
;
219 schedule_timeout_killable(*timeout
);
220 if (fatal_signal_pending(current
))
226 /* This is the error handling routine for processes that are allowed
229 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
231 struct nfs_client
*clp
= server
->nfs_client
;
232 struct nfs4_state
*state
= exception
->state
;
235 exception
->retry
= 0;
239 case -NFS4ERR_ADMIN_REVOKED
:
240 case -NFS4ERR_BAD_STATEID
:
241 case -NFS4ERR_OPENMODE
:
244 nfs4_state_mark_reclaim_nograce(clp
, state
);
245 case -NFS4ERR_STALE_CLIENTID
:
246 case -NFS4ERR_STALE_STATEID
:
247 case -NFS4ERR_EXPIRED
:
248 nfs4_schedule_state_recovery(clp
);
249 ret
= nfs4_wait_clnt_recover(clp
);
251 exception
->retry
= 1;
253 case -NFS4ERR_FILE_OPEN
:
256 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
259 case -NFS4ERR_OLD_STATEID
:
260 exception
->retry
= 1;
262 /* We failed to handle the error */
263 return nfs4_map_errors(ret
);
267 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
269 struct nfs_client
*clp
= server
->nfs_client
;
270 spin_lock(&clp
->cl_lock
);
271 if (time_before(clp
->cl_last_renewal
,timestamp
))
272 clp
->cl_last_renewal
= timestamp
;
273 spin_unlock(&clp
->cl_lock
);
276 #if defined(CONFIG_NFS_V4_1)
279 * nfs4_free_slot - free a slot and efficiently update slot table.
281 * freeing a slot is trivially done by clearing its respective bit
283 * If the freed slotid equals highest_used_slotid we want to update it
284 * so that the server would be able to size down the slot table if needed,
285 * otherwise we know that the highest_used_slotid is still in use.
286 * When updating highest_used_slotid there may be "holes" in the bitmap
287 * so we need to scan down from highest_used_slotid to 0 looking for the now
288 * highest slotid in use.
289 * If none found, highest_used_slotid is set to -1.
292 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
294 int slotid
= free_slotid
;
296 spin_lock(&tbl
->slot_tbl_lock
);
297 /* clear used bit in bitmap */
298 __clear_bit(slotid
, tbl
->used_slots
);
300 /* update highest_used_slotid when it is freed */
301 if (slotid
== tbl
->highest_used_slotid
) {
302 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
303 if (slotid
>= 0 && slotid
< tbl
->max_slots
)
304 tbl
->highest_used_slotid
= slotid
;
306 tbl
->highest_used_slotid
= -1;
308 rpc_wake_up_next(&tbl
->slot_tbl_waitq
);
309 spin_unlock(&tbl
->slot_tbl_lock
);
310 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
311 free_slotid
, tbl
->highest_used_slotid
);
314 void nfs41_sequence_free_slot(const struct nfs_client
*clp
,
315 struct nfs4_sequence_res
*res
)
317 struct nfs4_slot_table
*tbl
;
319 if (!nfs4_has_session(clp
)) {
320 dprintk("%s: No session\n", __func__
);
323 tbl
= &clp
->cl_session
->fc_slot_table
;
324 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
325 dprintk("%s: No slot\n", __func__
);
326 /* just wake up the next guy waiting since
327 * we may have not consumed a slot after all */
328 rpc_wake_up_next(&tbl
->slot_tbl_waitq
);
331 nfs4_free_slot(tbl
, res
->sr_slotid
);
332 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
335 static void nfs41_sequence_done(struct nfs_client
*clp
,
336 struct nfs4_sequence_res
*res
,
339 unsigned long timestamp
;
340 struct nfs4_slot_table
*tbl
;
341 struct nfs4_slot
*slot
;
344 * sr_status remains 1 if an RPC level error occurred. The server
345 * may or may not have processed the sequence operation..
346 * Proceed as if the server received and processed the sequence
349 if (res
->sr_status
== 1)
350 res
->sr_status
= NFS_OK
;
352 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
353 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
356 tbl
= &clp
->cl_session
->fc_slot_table
;
357 slot
= tbl
->slots
+ res
->sr_slotid
;
359 if (res
->sr_status
== 0) {
360 /* Update the slot's sequence and clientid lease timer */
362 timestamp
= res
->sr_renewal_time
;
363 spin_lock(&clp
->cl_lock
);
364 if (time_before(clp
->cl_last_renewal
, timestamp
))
365 clp
->cl_last_renewal
= timestamp
;
366 spin_unlock(&clp
->cl_lock
);
370 /* The session may be reset by one of the error handlers. */
371 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
372 nfs41_sequence_free_slot(clp
, res
);
376 * nfs4_find_slot - efficiently look for a free slot
378 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
379 * If found, we mark the slot as used, update the highest_used_slotid,
380 * and respectively set up the sequence operation args.
381 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
383 * Note: must be called with under the slot_tbl_lock.
386 nfs4_find_slot(struct nfs4_slot_table
*tbl
, struct rpc_task
*task
)
389 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
390 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
392 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
393 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
395 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
396 if (slotid
>= tbl
->max_slots
)
398 __set_bit(slotid
, tbl
->used_slots
);
399 if (slotid
> tbl
->highest_used_slotid
)
400 tbl
->highest_used_slotid
= slotid
;
403 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
404 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
408 static int nfs41_setup_sequence(struct nfs4_session
*session
,
409 struct nfs4_sequence_args
*args
,
410 struct nfs4_sequence_res
*res
,
412 struct rpc_task
*task
)
414 struct nfs4_slot
*slot
;
415 struct nfs4_slot_table
*tbl
;
418 dprintk("--> %s\n", __func__
);
419 /* slot already allocated? */
420 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
423 memset(res
, 0, sizeof(*res
));
424 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
425 tbl
= &session
->fc_slot_table
;
427 spin_lock(&tbl
->slot_tbl_lock
);
428 slotid
= nfs4_find_slot(tbl
, task
);
429 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
430 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
431 spin_unlock(&tbl
->slot_tbl_lock
);
432 dprintk("<-- %s: no free slots\n", __func__
);
435 spin_unlock(&tbl
->slot_tbl_lock
);
437 slot
= tbl
->slots
+ slotid
;
438 args
->sa_session
= session
;
439 args
->sa_slotid
= slotid
;
440 args
->sa_cache_this
= cache_reply
;
442 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
444 res
->sr_session
= session
;
445 res
->sr_slotid
= slotid
;
446 res
->sr_renewal_time
= jiffies
;
448 * sr_status is only set in decode_sequence, and so will remain
449 * set to 1 if an rpc level failure occurs.
455 int nfs4_setup_sequence(struct nfs_client
*clp
,
456 struct nfs4_sequence_args
*args
,
457 struct nfs4_sequence_res
*res
,
459 struct rpc_task
*task
)
463 dprintk("--> %s clp %p session %p sr_slotid %d\n",
464 __func__
, clp
, clp
->cl_session
, res
->sr_slotid
);
466 if (!nfs4_has_session(clp
))
468 ret
= nfs41_setup_sequence(clp
->cl_session
, args
, res
, cache_reply
,
470 if (ret
!= -EAGAIN
) {
471 /* terminate rpc task */
472 task
->tk_status
= ret
;
473 task
->tk_action
= NULL
;
476 dprintk("<-- %s status=%d\n", __func__
, ret
);
480 struct nfs41_call_sync_data
{
481 struct nfs_client
*clp
;
482 struct nfs4_sequence_args
*seq_args
;
483 struct nfs4_sequence_res
*seq_res
;
487 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
489 struct nfs41_call_sync_data
*data
= calldata
;
491 dprintk("--> %s data->clp->cl_session %p\n", __func__
,
492 data
->clp
->cl_session
);
493 if (nfs4_setup_sequence(data
->clp
, data
->seq_args
,
494 data
->seq_res
, data
->cache_reply
, task
))
496 rpc_call_start(task
);
499 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
501 struct nfs41_call_sync_data
*data
= calldata
;
503 nfs41_sequence_done(data
->clp
, data
->seq_res
, task
->tk_status
);
504 nfs41_sequence_free_slot(data
->clp
, data
->seq_res
);
507 struct rpc_call_ops nfs41_call_sync_ops
= {
508 .rpc_call_prepare
= nfs41_call_sync_prepare
,
509 .rpc_call_done
= nfs41_call_sync_done
,
512 static int nfs4_call_sync_sequence(struct nfs_client
*clp
,
513 struct rpc_clnt
*clnt
,
514 struct rpc_message
*msg
,
515 struct nfs4_sequence_args
*args
,
516 struct nfs4_sequence_res
*res
,
520 struct rpc_task
*task
;
521 struct nfs41_call_sync_data data
= {
525 .cache_reply
= cache_reply
,
527 struct rpc_task_setup task_setup
= {
530 .callback_ops
= &nfs41_call_sync_ops
,
531 .callback_data
= &data
534 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
535 task
= rpc_run_task(&task_setup
);
539 ret
= task
->tk_status
;
545 int _nfs4_call_sync_session(struct nfs_server
*server
,
546 struct rpc_message
*msg
,
547 struct nfs4_sequence_args
*args
,
548 struct nfs4_sequence_res
*res
,
551 return nfs4_call_sync_sequence(server
->nfs_client
, server
->client
,
552 msg
, args
, res
, cache_reply
);
555 #endif /* CONFIG_NFS_V4_1 */
557 int _nfs4_call_sync(struct nfs_server
*server
,
558 struct rpc_message
*msg
,
559 struct nfs4_sequence_args
*args
,
560 struct nfs4_sequence_res
*res
,
563 args
->sa_session
= res
->sr_session
= NULL
;
564 return rpc_call_sync(server
->client
, msg
, 0);
567 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
568 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
569 &(res)->seq_res, (cache_reply))
571 static void nfs4_sequence_done(const struct nfs_server
*server
,
572 struct nfs4_sequence_res
*res
, int rpc_status
)
574 #ifdef CONFIG_NFS_V4_1
575 if (nfs4_has_session(server
->nfs_client
))
576 nfs41_sequence_done(server
->nfs_client
, res
, rpc_status
);
577 #endif /* CONFIG_NFS_V4_1 */
580 /* no restart, therefore free slot here */
581 static void nfs4_sequence_done_free_slot(const struct nfs_server
*server
,
582 struct nfs4_sequence_res
*res
,
585 nfs4_sequence_done(server
, res
, rpc_status
);
586 nfs4_sequence_free_slot(server
->nfs_client
, res
);
589 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
591 struct nfs_inode
*nfsi
= NFS_I(dir
);
593 spin_lock(&dir
->i_lock
);
594 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
595 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
596 nfs_force_lookup_revalidate(dir
);
597 nfsi
->change_attr
= cinfo
->after
;
598 spin_unlock(&dir
->i_lock
);
601 struct nfs4_opendata
{
603 struct nfs_openargs o_arg
;
604 struct nfs_openres o_res
;
605 struct nfs_open_confirmargs c_arg
;
606 struct nfs_open_confirmres c_res
;
607 struct nfs_fattr f_attr
;
608 struct nfs_fattr dir_attr
;
611 struct nfs4_state_owner
*owner
;
612 struct nfs4_state
*state
;
614 unsigned long timestamp
;
615 unsigned int rpc_done
: 1;
621 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
623 p
->o_res
.f_attr
= &p
->f_attr
;
624 p
->o_res
.dir_attr
= &p
->dir_attr
;
625 p
->o_res
.seqid
= p
->o_arg
.seqid
;
626 p
->c_res
.seqid
= p
->c_arg
.seqid
;
627 p
->o_res
.server
= p
->o_arg
.server
;
628 nfs_fattr_init(&p
->f_attr
);
629 nfs_fattr_init(&p
->dir_attr
);
632 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
633 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
634 const struct iattr
*attrs
)
636 struct dentry
*parent
= dget_parent(path
->dentry
);
637 struct inode
*dir
= parent
->d_inode
;
638 struct nfs_server
*server
= NFS_SERVER(dir
);
639 struct nfs4_opendata
*p
;
641 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
644 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
645 if (p
->o_arg
.seqid
== NULL
)
647 p
->path
.mnt
= mntget(path
->mnt
);
648 p
->path
.dentry
= dget(path
->dentry
);
651 atomic_inc(&sp
->so_count
);
652 p
->o_arg
.fh
= NFS_FH(dir
);
653 p
->o_arg
.open_flags
= flags
;
654 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
655 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
656 p
->o_arg
.id
= sp
->so_owner_id
.id
;
657 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
658 p
->o_arg
.server
= server
;
659 p
->o_arg
.bitmask
= server
->attr_bitmask
;
660 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
661 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
662 if (flags
& O_EXCL
) {
663 u32
*s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
666 } else if (flags
& O_CREAT
) {
667 p
->o_arg
.u
.attrs
= &p
->attrs
;
668 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
670 p
->c_arg
.fh
= &p
->o_res
.fh
;
671 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
672 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
673 nfs4_init_opendata_res(p
);
683 static void nfs4_opendata_free(struct kref
*kref
)
685 struct nfs4_opendata
*p
= container_of(kref
,
686 struct nfs4_opendata
, kref
);
688 nfs_free_seqid(p
->o_arg
.seqid
);
689 if (p
->state
!= NULL
)
690 nfs4_put_open_state(p
->state
);
691 nfs4_put_state_owner(p
->owner
);
697 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
700 kref_put(&p
->kref
, nfs4_opendata_free
);
703 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
707 ret
= rpc_wait_for_completion_task(task
);
711 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
715 if (open_mode
& O_EXCL
)
717 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
719 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0;
722 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0;
724 case FMODE_READ
|FMODE_WRITE
:
725 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0;
731 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
733 if ((delegation
->type
& fmode
) != fmode
)
735 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
737 nfs_mark_delegation_referenced(delegation
);
741 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
750 case FMODE_READ
|FMODE_WRITE
:
753 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
756 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
758 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
759 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
760 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
763 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
766 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
768 case FMODE_READ
|FMODE_WRITE
:
769 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
773 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
775 write_seqlock(&state
->seqlock
);
776 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
777 write_sequnlock(&state
->seqlock
);
780 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
783 * Protect the call to nfs4_state_set_mode_locked and
784 * serialise the stateid update
786 write_seqlock(&state
->seqlock
);
787 if (deleg_stateid
!= NULL
) {
788 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
789 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
791 if (open_stateid
!= NULL
)
792 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
793 write_sequnlock(&state
->seqlock
);
794 spin_lock(&state
->owner
->so_lock
);
795 update_open_stateflags(state
, fmode
);
796 spin_unlock(&state
->owner
->so_lock
);
799 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
801 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
802 struct nfs_delegation
*deleg_cur
;
805 fmode
&= (FMODE_READ
|FMODE_WRITE
);
808 deleg_cur
= rcu_dereference(nfsi
->delegation
);
809 if (deleg_cur
== NULL
)
812 spin_lock(&deleg_cur
->lock
);
813 if (nfsi
->delegation
!= deleg_cur
||
814 (deleg_cur
->type
& fmode
) != fmode
)
815 goto no_delegation_unlock
;
817 if (delegation
== NULL
)
818 delegation
= &deleg_cur
->stateid
;
819 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
820 goto no_delegation_unlock
;
822 nfs_mark_delegation_referenced(deleg_cur
);
823 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
825 no_delegation_unlock
:
826 spin_unlock(&deleg_cur
->lock
);
830 if (!ret
&& open_stateid
!= NULL
) {
831 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
839 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
841 struct nfs_delegation
*delegation
;
844 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
845 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
850 nfs_inode_return_delegation(inode
);
853 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
855 struct nfs4_state
*state
= opendata
->state
;
856 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
857 struct nfs_delegation
*delegation
;
858 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
859 fmode_t fmode
= opendata
->o_arg
.fmode
;
860 nfs4_stateid stateid
;
864 if (can_open_cached(state
, fmode
, open_mode
)) {
865 spin_lock(&state
->owner
->so_lock
);
866 if (can_open_cached(state
, fmode
, open_mode
)) {
867 update_open_stateflags(state
, fmode
);
868 spin_unlock(&state
->owner
->so_lock
);
869 goto out_return_state
;
871 spin_unlock(&state
->owner
->so_lock
);
874 delegation
= rcu_dereference(nfsi
->delegation
);
875 if (delegation
== NULL
||
876 !can_open_delegated(delegation
, fmode
)) {
880 /* Save the delegation */
881 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
883 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
888 /* Try to update the stateid using the delegation */
889 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
890 goto out_return_state
;
895 atomic_inc(&state
->count
);
899 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
902 struct nfs4_state
*state
= NULL
;
903 struct nfs_delegation
*delegation
;
906 if (!data
->rpc_done
) {
907 state
= nfs4_try_open_cached(data
);
912 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
914 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
915 ret
= PTR_ERR(inode
);
919 state
= nfs4_get_open_state(inode
, data
->owner
);
922 if (data
->o_res
.delegation_type
!= 0) {
923 int delegation_flags
= 0;
926 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
928 delegation_flags
= delegation
->flags
;
930 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
931 nfs_inode_set_delegation(state
->inode
,
932 data
->owner
->so_cred
,
935 nfs_inode_reclaim_delegation(state
->inode
,
936 data
->owner
->so_cred
,
940 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
951 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
953 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
954 struct nfs_open_context
*ctx
;
956 spin_lock(&state
->inode
->i_lock
);
957 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
958 if (ctx
->state
!= state
)
960 get_nfs_open_context(ctx
);
961 spin_unlock(&state
->inode
->i_lock
);
964 spin_unlock(&state
->inode
->i_lock
);
965 return ERR_PTR(-ENOENT
);
968 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
970 struct nfs4_opendata
*opendata
;
972 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
);
973 if (opendata
== NULL
)
974 return ERR_PTR(-ENOMEM
);
975 opendata
->state
= state
;
976 atomic_inc(&state
->count
);
980 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
982 struct nfs4_state
*newstate
;
985 opendata
->o_arg
.open_flags
= 0;
986 opendata
->o_arg
.fmode
= fmode
;
987 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
988 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
989 nfs4_init_opendata_res(opendata
);
990 ret
= _nfs4_proc_open(opendata
);
993 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
994 if (IS_ERR(newstate
))
995 return PTR_ERR(newstate
);
996 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1001 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1003 struct nfs4_state
*newstate
;
1006 /* memory barrier prior to reading state->n_* */
1007 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1009 if (state
->n_rdwr
!= 0) {
1010 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1013 if (newstate
!= state
)
1016 if (state
->n_wronly
!= 0) {
1017 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1020 if (newstate
!= state
)
1023 if (state
->n_rdonly
!= 0) {
1024 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1027 if (newstate
!= state
)
1031 * We may have performed cached opens for all three recoveries.
1032 * Check if we need to update the current stateid.
1034 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1035 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1036 write_seqlock(&state
->seqlock
);
1037 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1038 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1039 write_sequnlock(&state
->seqlock
);
1046 * reclaim state on the server after a reboot.
1048 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1050 struct nfs_delegation
*delegation
;
1051 struct nfs4_opendata
*opendata
;
1052 fmode_t delegation_type
= 0;
1055 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1056 if (IS_ERR(opendata
))
1057 return PTR_ERR(opendata
);
1058 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1059 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1061 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1062 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1063 delegation_type
= delegation
->type
;
1065 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1066 status
= nfs4_open_recover(opendata
, state
);
1067 nfs4_opendata_put(opendata
);
1071 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1073 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1074 struct nfs4_exception exception
= { };
1077 err
= _nfs4_do_open_reclaim(ctx
, state
);
1078 if (err
!= -NFS4ERR_DELAY
)
1080 nfs4_handle_exception(server
, err
, &exception
);
1081 } while (exception
.retry
);
1085 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1087 struct nfs_open_context
*ctx
;
1090 ctx
= nfs4_state_find_open_context(state
);
1092 return PTR_ERR(ctx
);
1093 ret
= nfs4_do_open_reclaim(ctx
, state
);
1094 put_nfs_open_context(ctx
);
1098 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1100 struct nfs4_opendata
*opendata
;
1103 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1104 if (IS_ERR(opendata
))
1105 return PTR_ERR(opendata
);
1106 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1107 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1108 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1109 ret
= nfs4_open_recover(opendata
, state
);
1110 nfs4_opendata_put(opendata
);
1114 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1116 struct nfs4_exception exception
= { };
1117 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1120 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1124 case -NFS4ERR_STALE_CLIENTID
:
1125 case -NFS4ERR_STALE_STATEID
:
1126 case -NFS4ERR_EXPIRED
:
1127 /* Don't recall a delegation if it was lost */
1128 nfs4_schedule_state_recovery(server
->nfs_client
);
1131 err
= nfs4_handle_exception(server
, err
, &exception
);
1132 } while (exception
.retry
);
1136 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1138 struct nfs4_opendata
*data
= calldata
;
1140 data
->rpc_status
= task
->tk_status
;
1141 if (RPC_ASSASSINATED(task
))
1143 if (data
->rpc_status
== 0) {
1144 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1145 sizeof(data
->o_res
.stateid
.data
));
1146 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1147 renew_lease(data
->o_res
.server
, data
->timestamp
);
1152 static void nfs4_open_confirm_release(void *calldata
)
1154 struct nfs4_opendata
*data
= calldata
;
1155 struct nfs4_state
*state
= NULL
;
1157 /* If this request hasn't been cancelled, do nothing */
1158 if (data
->cancelled
== 0)
1160 /* In case of error, no cleanup! */
1161 if (!data
->rpc_done
)
1163 state
= nfs4_opendata_to_nfs4_state(data
);
1165 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1167 nfs4_opendata_put(data
);
1170 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1171 .rpc_call_done
= nfs4_open_confirm_done
,
1172 .rpc_release
= nfs4_open_confirm_release
,
1176 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1178 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1180 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1181 struct rpc_task
*task
;
1182 struct rpc_message msg
= {
1183 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1184 .rpc_argp
= &data
->c_arg
,
1185 .rpc_resp
= &data
->c_res
,
1186 .rpc_cred
= data
->owner
->so_cred
,
1188 struct rpc_task_setup task_setup_data
= {
1189 .rpc_client
= server
->client
,
1190 .rpc_message
= &msg
,
1191 .callback_ops
= &nfs4_open_confirm_ops
,
1192 .callback_data
= data
,
1193 .workqueue
= nfsiod_workqueue
,
1194 .flags
= RPC_TASK_ASYNC
,
1198 kref_get(&data
->kref
);
1200 data
->rpc_status
= 0;
1201 data
->timestamp
= jiffies
;
1202 task
= rpc_run_task(&task_setup_data
);
1204 return PTR_ERR(task
);
1205 status
= nfs4_wait_for_completion_rpc_task(task
);
1207 data
->cancelled
= 1;
1210 status
= data
->rpc_status
;
1215 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1217 struct nfs4_opendata
*data
= calldata
;
1218 struct nfs4_state_owner
*sp
= data
->owner
;
1220 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1223 * Check if we still need to send an OPEN call, or if we can use
1224 * a delegation instead.
1226 if (data
->state
!= NULL
) {
1227 struct nfs_delegation
*delegation
;
1229 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1232 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1233 if (delegation
!= NULL
&&
1234 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1240 /* Update sequence id. */
1241 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1242 data
->o_arg
.clientid
= sp
->so_client
->cl_clientid
;
1243 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1244 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1245 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1247 data
->timestamp
= jiffies
;
1248 if (nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
1249 &data
->o_arg
.seq_args
,
1250 &data
->o_res
.seq_res
, 1, task
))
1252 rpc_call_start(task
);
1255 task
->tk_action
= NULL
;
1259 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1261 struct nfs4_opendata
*data
= calldata
;
1263 data
->rpc_status
= task
->tk_status
;
1265 nfs4_sequence_done_free_slot(data
->o_arg
.server
, &data
->o_res
.seq_res
,
1268 if (RPC_ASSASSINATED(task
))
1270 if (task
->tk_status
== 0) {
1271 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1275 data
->rpc_status
= -ELOOP
;
1278 data
->rpc_status
= -EISDIR
;
1281 data
->rpc_status
= -ENOTDIR
;
1283 renew_lease(data
->o_res
.server
, data
->timestamp
);
1284 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1285 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1290 static void nfs4_open_release(void *calldata
)
1292 struct nfs4_opendata
*data
= calldata
;
1293 struct nfs4_state
*state
= NULL
;
1295 /* If this request hasn't been cancelled, do nothing */
1296 if (data
->cancelled
== 0)
1298 /* In case of error, no cleanup! */
1299 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1301 /* In case we need an open_confirm, no cleanup! */
1302 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1304 state
= nfs4_opendata_to_nfs4_state(data
);
1306 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1308 nfs4_opendata_put(data
);
1311 static const struct rpc_call_ops nfs4_open_ops
= {
1312 .rpc_call_prepare
= nfs4_open_prepare
,
1313 .rpc_call_done
= nfs4_open_done
,
1314 .rpc_release
= nfs4_open_release
,
1318 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1320 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1322 struct inode
*dir
= data
->dir
->d_inode
;
1323 struct nfs_server
*server
= NFS_SERVER(dir
);
1324 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1325 struct nfs_openres
*o_res
= &data
->o_res
;
1326 struct rpc_task
*task
;
1327 struct rpc_message msg
= {
1328 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1331 .rpc_cred
= data
->owner
->so_cred
,
1333 struct rpc_task_setup task_setup_data
= {
1334 .rpc_client
= server
->client
,
1335 .rpc_message
= &msg
,
1336 .callback_ops
= &nfs4_open_ops
,
1337 .callback_data
= data
,
1338 .workqueue
= nfsiod_workqueue
,
1339 .flags
= RPC_TASK_ASYNC
,
1343 kref_get(&data
->kref
);
1345 data
->rpc_status
= 0;
1346 data
->cancelled
= 0;
1347 task
= rpc_run_task(&task_setup_data
);
1349 return PTR_ERR(task
);
1350 status
= nfs4_wait_for_completion_rpc_task(task
);
1352 data
->cancelled
= 1;
1355 status
= data
->rpc_status
;
1357 if (status
!= 0 || !data
->rpc_done
)
1360 if (o_res
->fh
.size
== 0)
1361 _nfs4_proc_lookup(dir
, o_arg
->name
, &o_res
->fh
, o_res
->f_attr
);
1363 if (o_arg
->open_flags
& O_CREAT
) {
1364 update_changeattr(dir
, &o_res
->cinfo
);
1365 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1367 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1368 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1369 status
= _nfs4_proc_open_confirm(data
);
1373 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1374 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1378 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1380 struct nfs_client
*clp
= server
->nfs_client
;
1384 ret
= nfs4_wait_clnt_recover(clp
);
1387 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1388 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1390 nfs4_schedule_state_recovery(clp
);
1397 * reclaim state on the server after a network partition.
1398 * Assumes caller holds the appropriate lock
1400 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1402 struct nfs4_opendata
*opendata
;
1405 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1406 if (IS_ERR(opendata
))
1407 return PTR_ERR(opendata
);
1408 ret
= nfs4_open_recover(opendata
, state
);
1410 d_drop(ctx
->path
.dentry
);
1411 nfs4_opendata_put(opendata
);
1415 static inline int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1417 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1418 struct nfs4_exception exception
= { };
1422 err
= _nfs4_open_expired(ctx
, state
);
1423 if (err
!= -NFS4ERR_DELAY
)
1425 nfs4_handle_exception(server
, err
, &exception
);
1426 } while (exception
.retry
);
1430 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1432 struct nfs_open_context
*ctx
;
1435 ctx
= nfs4_state_find_open_context(state
);
1437 return PTR_ERR(ctx
);
1438 ret
= nfs4_do_open_expired(ctx
, state
);
1439 put_nfs_open_context(ctx
);
1444 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1445 * fields corresponding to attributes that were used to store the verifier.
1446 * Make sure we clobber those fields in the later setattr call
1448 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1450 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1451 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1452 sattr
->ia_valid
|= ATTR_ATIME
;
1454 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1455 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1456 sattr
->ia_valid
|= ATTR_MTIME
;
1460 * Returns a referenced nfs4_state
1462 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
)
1464 struct nfs4_state_owner
*sp
;
1465 struct nfs4_state
*state
= NULL
;
1466 struct nfs_server
*server
= NFS_SERVER(dir
);
1467 struct nfs4_opendata
*opendata
;
1470 /* Protect against reboot recovery conflicts */
1472 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1473 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1476 status
= nfs4_recover_expired_lease(server
);
1478 goto err_put_state_owner
;
1479 if (path
->dentry
->d_inode
!= NULL
)
1480 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1482 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
);
1483 if (opendata
== NULL
)
1484 goto err_put_state_owner
;
1486 if (path
->dentry
->d_inode
!= NULL
)
1487 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1489 status
= _nfs4_proc_open(opendata
);
1491 goto err_opendata_put
;
1493 if (opendata
->o_arg
.open_flags
& O_EXCL
)
1494 nfs4_exclusive_attrset(opendata
, sattr
);
1496 state
= nfs4_opendata_to_nfs4_state(opendata
);
1497 status
= PTR_ERR(state
);
1499 goto err_opendata_put
;
1500 nfs4_opendata_put(opendata
);
1501 nfs4_put_state_owner(sp
);
1505 nfs4_opendata_put(opendata
);
1506 err_put_state_owner
:
1507 nfs4_put_state_owner(sp
);
1514 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
)
1516 struct nfs4_exception exception
= { };
1517 struct nfs4_state
*res
;
1521 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1524 /* NOTE: BAD_SEQID means the server and client disagree about the
1525 * book-keeping w.r.t. state-changing operations
1526 * (OPEN/CLOSE/LOCK/LOCKU...)
1527 * It is actually a sign of a bug on the client or on the server.
1529 * If we receive a BAD_SEQID error in the particular case of
1530 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1531 * have unhashed the old state_owner for us, and that we can
1532 * therefore safely retry using a new one. We should still warn
1533 * the user though...
1535 if (status
== -NFS4ERR_BAD_SEQID
) {
1536 printk(KERN_WARNING
"NFS: v4 server %s "
1537 " returned a bad sequence-id error!\n",
1538 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1539 exception
.retry
= 1;
1543 * BAD_STATEID on OPEN means that the server cancelled our
1544 * state before it received the OPEN_CONFIRM.
1545 * Recover by retrying the request as per the discussion
1546 * on Page 181 of RFC3530.
1548 if (status
== -NFS4ERR_BAD_STATEID
) {
1549 exception
.retry
= 1;
1552 if (status
== -EAGAIN
) {
1553 /* We must have found a delegation */
1554 exception
.retry
= 1;
1557 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1558 status
, &exception
));
1559 } while (exception
.retry
);
1563 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1564 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1565 struct nfs4_state
*state
)
1567 struct nfs_server
*server
= NFS_SERVER(inode
);
1568 struct nfs_setattrargs arg
= {
1569 .fh
= NFS_FH(inode
),
1572 .bitmask
= server
->attr_bitmask
,
1574 struct nfs_setattrres res
= {
1578 struct rpc_message msg
= {
1579 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1584 unsigned long timestamp
= jiffies
;
1587 nfs_fattr_init(fattr
);
1589 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1590 /* Use that stateid */
1591 } else if (state
!= NULL
) {
1592 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1594 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1596 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1597 if (status
== 0 && state
!= NULL
)
1598 renew_lease(server
, timestamp
);
1602 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1603 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1604 struct nfs4_state
*state
)
1606 struct nfs_server
*server
= NFS_SERVER(inode
);
1607 struct nfs4_exception exception
= { };
1610 err
= nfs4_handle_exception(server
,
1611 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1613 } while (exception
.retry
);
1617 struct nfs4_closedata
{
1619 struct inode
*inode
;
1620 struct nfs4_state
*state
;
1621 struct nfs_closeargs arg
;
1622 struct nfs_closeres res
;
1623 struct nfs_fattr fattr
;
1624 unsigned long timestamp
;
1627 static void nfs4_free_closedata(void *data
)
1629 struct nfs4_closedata
*calldata
= data
;
1630 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1632 nfs4_put_open_state(calldata
->state
);
1633 nfs_free_seqid(calldata
->arg
.seqid
);
1634 nfs4_put_state_owner(sp
);
1635 path_put(&calldata
->path
);
1639 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1641 struct nfs4_closedata
*calldata
= data
;
1642 struct nfs4_state
*state
= calldata
->state
;
1643 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1645 nfs4_sequence_done(server
, &calldata
->res
.seq_res
, task
->tk_status
);
1646 if (RPC_ASSASSINATED(task
))
1648 /* hmm. we are done with the inode, and in the process of freeing
1649 * the state_owner. we keep this around to process errors
1651 switch (task
->tk_status
) {
1653 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1654 renew_lease(server
, calldata
->timestamp
);
1656 case -NFS4ERR_STALE_STATEID
:
1657 case -NFS4ERR_OLD_STATEID
:
1658 case -NFS4ERR_BAD_STATEID
:
1659 case -NFS4ERR_EXPIRED
:
1660 if (calldata
->arg
.fmode
== 0)
1663 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
) {
1664 nfs4_restart_rpc(task
, server
->nfs_client
);
1668 nfs4_sequence_free_slot(server
->nfs_client
, &calldata
->res
.seq_res
);
1669 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1672 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1674 struct nfs4_closedata
*calldata
= data
;
1675 struct nfs4_state
*state
= calldata
->state
;
1676 int clear_rd
, clear_wr
, clear_rdwr
;
1678 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1681 clear_rd
= clear_wr
= clear_rdwr
= 0;
1682 spin_lock(&state
->owner
->so_lock
);
1683 /* Calculate the change in open mode */
1684 if (state
->n_rdwr
== 0) {
1685 if (state
->n_rdonly
== 0) {
1686 clear_rd
|= test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1687 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1689 if (state
->n_wronly
== 0) {
1690 clear_wr
|= test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1691 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1694 spin_unlock(&state
->owner
->so_lock
);
1695 if (!clear_rd
&& !clear_wr
&& !clear_rdwr
) {
1696 /* Note: exit _without_ calling nfs4_close_done */
1697 task
->tk_action
= NULL
;
1700 nfs_fattr_init(calldata
->res
.fattr
);
1701 if (test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0) {
1702 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1703 calldata
->arg
.fmode
= FMODE_READ
;
1704 } else if (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0) {
1705 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1706 calldata
->arg
.fmode
= FMODE_WRITE
;
1708 calldata
->timestamp
= jiffies
;
1709 if (nfs4_setup_sequence((NFS_SERVER(calldata
->inode
))->nfs_client
,
1710 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1713 rpc_call_start(task
);
1716 static const struct rpc_call_ops nfs4_close_ops
= {
1717 .rpc_call_prepare
= nfs4_close_prepare
,
1718 .rpc_call_done
= nfs4_close_done
,
1719 .rpc_release
= nfs4_free_closedata
,
1723 * It is possible for data to be read/written from a mem-mapped file
1724 * after the sys_close call (which hits the vfs layer as a flush).
1725 * This means that we can't safely call nfsv4 close on a file until
1726 * the inode is cleared. This in turn means that we are not good
1727 * NFSv4 citizens - we do not indicate to the server to update the file's
1728 * share state even when we are done with one of the three share
1729 * stateid's in the inode.
1731 * NOTE: Caller must be holding the sp->so_owner semaphore!
1733 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, int wait
)
1735 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1736 struct nfs4_closedata
*calldata
;
1737 struct nfs4_state_owner
*sp
= state
->owner
;
1738 struct rpc_task
*task
;
1739 struct rpc_message msg
= {
1740 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1741 .rpc_cred
= state
->owner
->so_cred
,
1743 struct rpc_task_setup task_setup_data
= {
1744 .rpc_client
= server
->client
,
1745 .rpc_message
= &msg
,
1746 .callback_ops
= &nfs4_close_ops
,
1747 .workqueue
= nfsiod_workqueue
,
1748 .flags
= RPC_TASK_ASYNC
,
1750 int status
= -ENOMEM
;
1752 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
1753 if (calldata
== NULL
)
1755 calldata
->inode
= state
->inode
;
1756 calldata
->state
= state
;
1757 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1758 calldata
->arg
.stateid
= &state
->open_stateid
;
1759 /* Serialization for the sequence id */
1760 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
1761 if (calldata
->arg
.seqid
== NULL
)
1762 goto out_free_calldata
;
1763 calldata
->arg
.fmode
= 0;
1764 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1765 calldata
->res
.fattr
= &calldata
->fattr
;
1766 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1767 calldata
->res
.server
= server
;
1768 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1769 calldata
->path
.mnt
= mntget(path
->mnt
);
1770 calldata
->path
.dentry
= dget(path
->dentry
);
1772 msg
.rpc_argp
= &calldata
->arg
,
1773 msg
.rpc_resp
= &calldata
->res
,
1774 task_setup_data
.callback_data
= calldata
;
1775 task
= rpc_run_task(&task_setup_data
);
1777 return PTR_ERR(task
);
1780 status
= rpc_wait_for_completion_task(task
);
1786 nfs4_put_open_state(state
);
1787 nfs4_put_state_owner(sp
);
1791 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1796 /* If the open_intent is for execute, we have an extra check to make */
1797 if (fmode
& FMODE_EXEC
) {
1798 ret
= nfs_may_open(state
->inode
,
1799 state
->owner
->so_cred
,
1800 nd
->intent
.open
.flags
);
1804 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1805 if (!IS_ERR(filp
)) {
1806 struct nfs_open_context
*ctx
;
1807 ctx
= nfs_file_open_context(filp
);
1811 ret
= PTR_ERR(filp
);
1813 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
1818 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1820 struct path path
= {
1821 .mnt
= nd
->path
.mnt
,
1824 struct dentry
*parent
;
1826 struct rpc_cred
*cred
;
1827 struct nfs4_state
*state
;
1829 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
1831 if (nd
->flags
& LOOKUP_CREATE
) {
1832 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
1833 attr
.ia_valid
= ATTR_MODE
;
1834 if (!IS_POSIXACL(dir
))
1835 attr
.ia_mode
&= ~current_umask();
1838 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
1841 cred
= rpc_lookup_cred();
1843 return (struct dentry
*)cred
;
1844 parent
= dentry
->d_parent
;
1845 /* Protect against concurrent sillydeletes */
1846 nfs_block_sillyrename(parent
);
1847 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
1849 if (IS_ERR(state
)) {
1850 if (PTR_ERR(state
) == -ENOENT
) {
1851 d_add(dentry
, NULL
);
1852 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1854 nfs_unblock_sillyrename(parent
);
1855 return (struct dentry
*)state
;
1857 res
= d_add_unique(dentry
, igrab(state
->inode
));
1860 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
1861 nfs_unblock_sillyrename(parent
);
1862 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1867 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
1869 struct path path
= {
1870 .mnt
= nd
->path
.mnt
,
1873 struct rpc_cred
*cred
;
1874 struct nfs4_state
*state
;
1875 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
1877 cred
= rpc_lookup_cred();
1879 return PTR_ERR(cred
);
1880 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
1882 if (IS_ERR(state
)) {
1883 switch (PTR_ERR(state
)) {
1889 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
1895 if (state
->inode
== dentry
->d_inode
) {
1896 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1897 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1900 nfs4_close_sync(&path
, state
, fmode
);
1906 void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
1908 if (ctx
->state
== NULL
)
1911 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
1913 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
1916 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1918 struct nfs4_server_caps_arg args
= {
1921 struct nfs4_server_caps_res res
= {};
1922 struct rpc_message msg
= {
1923 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
1929 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1931 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
1932 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
1933 server
->caps
|= NFS_CAP_ACLS
;
1934 if (res
.has_links
!= 0)
1935 server
->caps
|= NFS_CAP_HARDLINKS
;
1936 if (res
.has_symlinks
!= 0)
1937 server
->caps
|= NFS_CAP_SYMLINKS
;
1938 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
1939 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
1940 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
1941 server
->acl_bitmask
= res
.acl_bitmask
;
1947 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1949 struct nfs4_exception exception
= { };
1952 err
= nfs4_handle_exception(server
,
1953 _nfs4_server_capabilities(server
, fhandle
),
1955 } while (exception
.retry
);
1959 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1960 struct nfs_fsinfo
*info
)
1962 struct nfs4_lookup_root_arg args
= {
1963 .bitmask
= nfs4_fattr_bitmap
,
1965 struct nfs4_lookup_res res
= {
1967 .fattr
= info
->fattr
,
1970 struct rpc_message msg
= {
1971 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
1975 nfs_fattr_init(info
->fattr
);
1976 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1979 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1980 struct nfs_fsinfo
*info
)
1982 struct nfs4_exception exception
= { };
1985 err
= nfs4_handle_exception(server
,
1986 _nfs4_lookup_root(server
, fhandle
, info
),
1988 } while (exception
.retry
);
1993 * get the file handle for the "/" directory on the server
1995 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1996 struct nfs_fsinfo
*info
)
2000 status
= nfs4_lookup_root(server
, fhandle
, info
);
2002 status
= nfs4_server_capabilities(server
, fhandle
);
2004 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2005 return nfs4_map_errors(status
);
2009 * Get locations and (maybe) other attributes of a referral.
2010 * Note that we'll actually follow the referral later when
2011 * we detect fsid mismatch in inode revalidation
2013 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2015 int status
= -ENOMEM
;
2016 struct page
*page
= NULL
;
2017 struct nfs4_fs_locations
*locations
= NULL
;
2019 page
= alloc_page(GFP_KERNEL
);
2022 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2023 if (locations
== NULL
)
2026 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2029 /* Make sure server returned a different fsid for the referral */
2030 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2031 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2036 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2037 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2039 fattr
->mode
= S_IFDIR
;
2040 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2049 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2051 struct nfs4_getattr_arg args
= {
2053 .bitmask
= server
->attr_bitmask
,
2055 struct nfs4_getattr_res res
= {
2059 struct rpc_message msg
= {
2060 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2065 nfs_fattr_init(fattr
);
2066 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2069 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2071 struct nfs4_exception exception
= { };
2074 err
= nfs4_handle_exception(server
,
2075 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2077 } while (exception
.retry
);
2082 * The file is not closed if it is opened due to the a request to change
2083 * the size of the file. The open call will not be needed once the
2084 * VFS layer lookup-intents are implemented.
2086 * Close is called when the inode is destroyed.
2087 * If we haven't opened the file for O_WRONLY, we
2088 * need to in the size_change case to obtain a stateid.
2091 * Because OPEN is always done by name in nfsv4, it is
2092 * possible that we opened a different file by the same
2093 * name. We can recognize this race condition, but we
2094 * can't do anything about it besides returning an error.
2096 * This will be fixed with VFS changes (lookup-intent).
2099 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2100 struct iattr
*sattr
)
2102 struct inode
*inode
= dentry
->d_inode
;
2103 struct rpc_cred
*cred
= NULL
;
2104 struct nfs4_state
*state
= NULL
;
2107 nfs_fattr_init(fattr
);
2109 /* Search for an existing open(O_WRITE) file */
2110 if (sattr
->ia_valid
& ATTR_FILE
) {
2111 struct nfs_open_context
*ctx
;
2113 ctx
= nfs_file_open_context(sattr
->ia_file
);
2120 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2122 nfs_setattr_update_inode(inode
, sattr
);
2126 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2127 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2128 struct nfs_fattr
*fattr
)
2131 struct nfs4_lookup_arg args
= {
2132 .bitmask
= server
->attr_bitmask
,
2136 struct nfs4_lookup_res res
= {
2141 struct rpc_message msg
= {
2142 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2147 nfs_fattr_init(fattr
);
2149 dprintk("NFS call lookupfh %s\n", name
->name
);
2150 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2151 dprintk("NFS reply lookupfh: %d\n", status
);
2155 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2156 struct qstr
*name
, struct nfs_fh
*fhandle
,
2157 struct nfs_fattr
*fattr
)
2159 struct nfs4_exception exception
= { };
2162 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2164 if (err
== -NFS4ERR_MOVED
) {
2168 err
= nfs4_handle_exception(server
, err
, &exception
);
2169 } while (exception
.retry
);
2173 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2174 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2178 dprintk("NFS call lookup %s\n", name
->name
);
2179 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2180 if (status
== -NFS4ERR_MOVED
)
2181 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2182 dprintk("NFS reply lookup: %d\n", status
);
2186 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2188 struct nfs4_exception exception
= { };
2191 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2192 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2194 } while (exception
.retry
);
2198 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2200 struct nfs_server
*server
= NFS_SERVER(inode
);
2201 struct nfs_fattr fattr
;
2202 struct nfs4_accessargs args
= {
2203 .fh
= NFS_FH(inode
),
2204 .bitmask
= server
->attr_bitmask
,
2206 struct nfs4_accessres res
= {
2210 struct rpc_message msg
= {
2211 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2214 .rpc_cred
= entry
->cred
,
2216 int mode
= entry
->mask
;
2220 * Determine which access bits we want to ask for...
2222 if (mode
& MAY_READ
)
2223 args
.access
|= NFS4_ACCESS_READ
;
2224 if (S_ISDIR(inode
->i_mode
)) {
2225 if (mode
& MAY_WRITE
)
2226 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2227 if (mode
& MAY_EXEC
)
2228 args
.access
|= NFS4_ACCESS_LOOKUP
;
2230 if (mode
& MAY_WRITE
)
2231 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2232 if (mode
& MAY_EXEC
)
2233 args
.access
|= NFS4_ACCESS_EXECUTE
;
2235 nfs_fattr_init(&fattr
);
2236 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2239 if (res
.access
& NFS4_ACCESS_READ
)
2240 entry
->mask
|= MAY_READ
;
2241 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2242 entry
->mask
|= MAY_WRITE
;
2243 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2244 entry
->mask
|= MAY_EXEC
;
2245 nfs_refresh_inode(inode
, &fattr
);
2250 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2252 struct nfs4_exception exception
= { };
2255 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2256 _nfs4_proc_access(inode
, entry
),
2258 } while (exception
.retry
);
2263 * TODO: For the time being, we don't try to get any attributes
2264 * along with any of the zero-copy operations READ, READDIR,
2267 * In the case of the first three, we want to put the GETATTR
2268 * after the read-type operation -- this is because it is hard
2269 * to predict the length of a GETATTR response in v4, and thus
2270 * align the READ data correctly. This means that the GETATTR
2271 * may end up partially falling into the page cache, and we should
2272 * shift it into the 'tail' of the xdr_buf before processing.
2273 * To do this efficiently, we need to know the total length
2274 * of data received, which doesn't seem to be available outside
2277 * In the case of WRITE, we also want to put the GETATTR after
2278 * the operation -- in this case because we want to make sure
2279 * we get the post-operation mtime and size. This means that
2280 * we can't use xdr_encode_pages() as written: we need a variant
2281 * of it which would leave room in the 'tail' iovec.
2283 * Both of these changes to the XDR layer would in fact be quite
2284 * minor, but I decided to leave them for a subsequent patch.
2286 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2287 unsigned int pgbase
, unsigned int pglen
)
2289 struct nfs4_readlink args
= {
2290 .fh
= NFS_FH(inode
),
2295 struct nfs4_readlink_res res
;
2296 struct rpc_message msg
= {
2297 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2302 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2305 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2306 unsigned int pgbase
, unsigned int pglen
)
2308 struct nfs4_exception exception
= { };
2311 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2312 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2314 } while (exception
.retry
);
2320 * We will need to arrange for the VFS layer to provide an atomic open.
2321 * Until then, this create/open method is prone to inefficiency and race
2322 * conditions due to the lookup, create, and open VFS calls from sys_open()
2323 * placed on the wire.
2325 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2326 * The file will be opened again in the subsequent VFS open call
2327 * (nfs4_proc_file_open).
2329 * The open for read will just hang around to be used by any process that
2330 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2334 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2335 int flags
, struct nameidata
*nd
)
2337 struct path path
= {
2338 .mnt
= nd
->path
.mnt
,
2341 struct nfs4_state
*state
;
2342 struct rpc_cred
*cred
;
2343 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2346 cred
= rpc_lookup_cred();
2348 status
= PTR_ERR(cred
);
2351 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2353 if (IS_ERR(state
)) {
2354 status
= PTR_ERR(state
);
2357 d_add(dentry
, igrab(state
->inode
));
2358 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2359 if (flags
& O_EXCL
) {
2360 struct nfs_fattr fattr
;
2361 status
= nfs4_do_setattr(state
->inode
, cred
, &fattr
, sattr
, state
);
2363 nfs_setattr_update_inode(state
->inode
, sattr
);
2364 nfs_post_op_update_inode(state
->inode
, &fattr
);
2366 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2367 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2369 nfs4_close_sync(&path
, state
, fmode
);
2376 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2378 struct nfs_server
*server
= NFS_SERVER(dir
);
2379 struct nfs_removeargs args
= {
2381 .name
.len
= name
->len
,
2382 .name
.name
= name
->name
,
2383 .bitmask
= server
->attr_bitmask
,
2385 struct nfs_removeres res
= {
2388 struct rpc_message msg
= {
2389 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2395 nfs_fattr_init(&res
.dir_attr
);
2396 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2398 update_changeattr(dir
, &res
.cinfo
);
2399 nfs_post_op_update_inode(dir
, &res
.dir_attr
);
2404 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2406 struct nfs4_exception exception
= { };
2409 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2410 _nfs4_proc_remove(dir
, name
),
2412 } while (exception
.retry
);
2416 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2418 struct nfs_server
*server
= NFS_SERVER(dir
);
2419 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2420 struct nfs_removeres
*res
= msg
->rpc_resp
;
2422 args
->bitmask
= server
->cache_consistency_bitmask
;
2423 res
->server
= server
;
2424 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2427 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2429 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2431 nfs4_sequence_done(res
->server
, &res
->seq_res
, task
->tk_status
);
2432 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2434 nfs4_sequence_free_slot(res
->server
->nfs_client
, &res
->seq_res
);
2435 update_changeattr(dir
, &res
->cinfo
);
2436 nfs_post_op_update_inode(dir
, &res
->dir_attr
);
2440 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2441 struct inode
*new_dir
, struct qstr
*new_name
)
2443 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2444 struct nfs4_rename_arg arg
= {
2445 .old_dir
= NFS_FH(old_dir
),
2446 .new_dir
= NFS_FH(new_dir
),
2447 .old_name
= old_name
,
2448 .new_name
= new_name
,
2449 .bitmask
= server
->attr_bitmask
,
2451 struct nfs_fattr old_fattr
, new_fattr
;
2452 struct nfs4_rename_res res
= {
2454 .old_fattr
= &old_fattr
,
2455 .new_fattr
= &new_fattr
,
2457 struct rpc_message msg
= {
2458 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2464 nfs_fattr_init(res
.old_fattr
);
2465 nfs_fattr_init(res
.new_fattr
);
2466 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2469 update_changeattr(old_dir
, &res
.old_cinfo
);
2470 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2471 update_changeattr(new_dir
, &res
.new_cinfo
);
2472 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2477 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2478 struct inode
*new_dir
, struct qstr
*new_name
)
2480 struct nfs4_exception exception
= { };
2483 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2484 _nfs4_proc_rename(old_dir
, old_name
,
2487 } while (exception
.retry
);
2491 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2493 struct nfs_server
*server
= NFS_SERVER(inode
);
2494 struct nfs4_link_arg arg
= {
2495 .fh
= NFS_FH(inode
),
2496 .dir_fh
= NFS_FH(dir
),
2498 .bitmask
= server
->attr_bitmask
,
2500 struct nfs_fattr fattr
, dir_attr
;
2501 struct nfs4_link_res res
= {
2504 .dir_attr
= &dir_attr
,
2506 struct rpc_message msg
= {
2507 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2513 nfs_fattr_init(res
.fattr
);
2514 nfs_fattr_init(res
.dir_attr
);
2515 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2517 update_changeattr(dir
, &res
.cinfo
);
2518 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2519 nfs_post_op_update_inode(inode
, res
.fattr
);
2525 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2527 struct nfs4_exception exception
= { };
2530 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2531 _nfs4_proc_link(inode
, dir
, name
),
2533 } while (exception
.retry
);
2537 struct nfs4_createdata
{
2538 struct rpc_message msg
;
2539 struct nfs4_create_arg arg
;
2540 struct nfs4_create_res res
;
2542 struct nfs_fattr fattr
;
2543 struct nfs_fattr dir_fattr
;
2546 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2547 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2549 struct nfs4_createdata
*data
;
2551 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2553 struct nfs_server
*server
= NFS_SERVER(dir
);
2555 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2556 data
->msg
.rpc_argp
= &data
->arg
;
2557 data
->msg
.rpc_resp
= &data
->res
;
2558 data
->arg
.dir_fh
= NFS_FH(dir
);
2559 data
->arg
.server
= server
;
2560 data
->arg
.name
= name
;
2561 data
->arg
.attrs
= sattr
;
2562 data
->arg
.ftype
= ftype
;
2563 data
->arg
.bitmask
= server
->attr_bitmask
;
2564 data
->res
.server
= server
;
2565 data
->res
.fh
= &data
->fh
;
2566 data
->res
.fattr
= &data
->fattr
;
2567 data
->res
.dir_fattr
= &data
->dir_fattr
;
2568 nfs_fattr_init(data
->res
.fattr
);
2569 nfs_fattr_init(data
->res
.dir_fattr
);
2574 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2576 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2577 &data
->arg
, &data
->res
, 1);
2579 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2580 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2581 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2586 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2591 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2592 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2594 struct nfs4_createdata
*data
;
2595 int status
= -ENAMETOOLONG
;
2597 if (len
> NFS4_MAXPATHLEN
)
2601 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2605 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2606 data
->arg
.u
.symlink
.pages
= &page
;
2607 data
->arg
.u
.symlink
.len
= len
;
2609 status
= nfs4_do_create(dir
, dentry
, data
);
2611 nfs4_free_createdata(data
);
2616 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2617 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2619 struct nfs4_exception exception
= { };
2622 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2623 _nfs4_proc_symlink(dir
, dentry
, page
,
2626 } while (exception
.retry
);
2630 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2631 struct iattr
*sattr
)
2633 struct nfs4_createdata
*data
;
2634 int status
= -ENOMEM
;
2636 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2640 status
= nfs4_do_create(dir
, dentry
, data
);
2642 nfs4_free_createdata(data
);
2647 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2648 struct iattr
*sattr
)
2650 struct nfs4_exception exception
= { };
2653 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2654 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2656 } while (exception
.retry
);
2660 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2661 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2663 struct inode
*dir
= dentry
->d_inode
;
2664 struct nfs4_readdir_arg args
= {
2669 .bitmask
= NFS_SERVER(dentry
->d_inode
)->cache_consistency_bitmask
,
2671 struct nfs4_readdir_res res
;
2672 struct rpc_message msg
= {
2673 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2680 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2681 dentry
->d_parent
->d_name
.name
,
2682 dentry
->d_name
.name
,
2683 (unsigned long long)cookie
);
2684 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2685 res
.pgbase
= args
.pgbase
;
2686 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2688 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2690 nfs_invalidate_atime(dir
);
2692 dprintk("%s: returns %d\n", __func__
, status
);
2696 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2697 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2699 struct nfs4_exception exception
= { };
2702 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2703 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2706 } while (exception
.retry
);
2710 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2711 struct iattr
*sattr
, dev_t rdev
)
2713 struct nfs4_createdata
*data
;
2714 int mode
= sattr
->ia_mode
;
2715 int status
= -ENOMEM
;
2717 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2718 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2720 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2725 data
->arg
.ftype
= NF4FIFO
;
2726 else if (S_ISBLK(mode
)) {
2727 data
->arg
.ftype
= NF4BLK
;
2728 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2729 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2731 else if (S_ISCHR(mode
)) {
2732 data
->arg
.ftype
= NF4CHR
;
2733 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2734 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2737 status
= nfs4_do_create(dir
, dentry
, data
);
2739 nfs4_free_createdata(data
);
2744 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2745 struct iattr
*sattr
, dev_t rdev
)
2747 struct nfs4_exception exception
= { };
2750 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2751 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2753 } while (exception
.retry
);
2757 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2758 struct nfs_fsstat
*fsstat
)
2760 struct nfs4_statfs_arg args
= {
2762 .bitmask
= server
->attr_bitmask
,
2764 struct nfs4_statfs_res res
= {
2767 struct rpc_message msg
= {
2768 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2773 nfs_fattr_init(fsstat
->fattr
);
2774 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2777 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2779 struct nfs4_exception exception
= { };
2782 err
= nfs4_handle_exception(server
,
2783 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2785 } while (exception
.retry
);
2789 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2790 struct nfs_fsinfo
*fsinfo
)
2792 struct nfs4_fsinfo_arg args
= {
2794 .bitmask
= server
->attr_bitmask
,
2796 struct nfs4_fsinfo_res res
= {
2799 struct rpc_message msg
= {
2800 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
2805 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2808 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2810 struct nfs4_exception exception
= { };
2814 err
= nfs4_handle_exception(server
,
2815 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
2817 } while (exception
.retry
);
2821 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2823 nfs_fattr_init(fsinfo
->fattr
);
2824 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
2827 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2828 struct nfs_pathconf
*pathconf
)
2830 struct nfs4_pathconf_arg args
= {
2832 .bitmask
= server
->attr_bitmask
,
2834 struct nfs4_pathconf_res res
= {
2835 .pathconf
= pathconf
,
2837 struct rpc_message msg
= {
2838 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
2843 /* None of the pathconf attributes are mandatory to implement */
2844 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
2845 memset(pathconf
, 0, sizeof(*pathconf
));
2849 nfs_fattr_init(pathconf
->fattr
);
2850 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2853 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2854 struct nfs_pathconf
*pathconf
)
2856 struct nfs4_exception exception
= { };
2860 err
= nfs4_handle_exception(server
,
2861 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
2863 } while (exception
.retry
);
2867 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
2869 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2871 dprintk("--> %s\n", __func__
);
2873 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2874 nfs4_sequence_done(server
, &data
->res
.seq_res
, task
->tk_status
);
2876 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
2877 nfs4_restart_rpc(task
, server
->nfs_client
);
2881 nfs_invalidate_atime(data
->inode
);
2882 if (task
->tk_status
> 0)
2883 renew_lease(server
, data
->timestamp
);
2887 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
2889 data
->timestamp
= jiffies
;
2890 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
2893 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
2895 struct inode
*inode
= data
->inode
;
2897 /* slot is freed in nfs_writeback_done */
2898 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
2901 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
2902 nfs4_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
2905 if (task
->tk_status
>= 0) {
2906 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
2907 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
2912 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
2914 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2916 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
2917 data
->res
.server
= server
;
2918 data
->timestamp
= jiffies
;
2920 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
2923 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
2925 struct inode
*inode
= data
->inode
;
2927 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
2929 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
2930 nfs4_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
2933 nfs4_sequence_free_slot(NFS_SERVER(inode
)->nfs_client
,
2934 &data
->res
.seq_res
);
2935 nfs_refresh_inode(inode
, data
->res
.fattr
);
2939 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
2941 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2943 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
2944 data
->res
.server
= server
;
2945 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
2949 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2950 * standalone procedure for queueing an asynchronous RENEW.
2952 static void nfs4_renew_done(struct rpc_task
*task
, void *data
)
2954 struct nfs_client
*clp
= (struct nfs_client
*)task
->tk_msg
.rpc_argp
;
2955 unsigned long timestamp
= (unsigned long)data
;
2957 if (task
->tk_status
< 0) {
2958 /* Unless we're shutting down, schedule state recovery! */
2959 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
2960 nfs4_schedule_state_recovery(clp
);
2963 spin_lock(&clp
->cl_lock
);
2964 if (time_before(clp
->cl_last_renewal
,timestamp
))
2965 clp
->cl_last_renewal
= timestamp
;
2966 spin_unlock(&clp
->cl_lock
);
2969 static const struct rpc_call_ops nfs4_renew_ops
= {
2970 .rpc_call_done
= nfs4_renew_done
,
2973 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
2975 struct rpc_message msg
= {
2976 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2981 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
2982 &nfs4_renew_ops
, (void *)jiffies
);
2985 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
2987 struct rpc_message msg
= {
2988 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2992 unsigned long now
= jiffies
;
2995 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2998 spin_lock(&clp
->cl_lock
);
2999 if (time_before(clp
->cl_last_renewal
,now
))
3000 clp
->cl_last_renewal
= now
;
3001 spin_unlock(&clp
->cl_lock
);
3005 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3007 return (server
->caps
& NFS_CAP_ACLS
)
3008 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3009 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3012 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3013 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3016 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3018 static void buf_to_pages(const void *buf
, size_t buflen
,
3019 struct page
**pages
, unsigned int *pgbase
)
3021 const void *p
= buf
;
3023 *pgbase
= offset_in_page(buf
);
3025 while (p
< buf
+ buflen
) {
3026 *(pages
++) = virt_to_page(p
);
3027 p
+= PAGE_CACHE_SIZE
;
3031 struct nfs4_cached_acl
{
3037 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3039 struct nfs_inode
*nfsi
= NFS_I(inode
);
3041 spin_lock(&inode
->i_lock
);
3042 kfree(nfsi
->nfs4_acl
);
3043 nfsi
->nfs4_acl
= acl
;
3044 spin_unlock(&inode
->i_lock
);
3047 static void nfs4_zap_acl_attr(struct inode
*inode
)
3049 nfs4_set_cached_acl(inode
, NULL
);
3052 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3054 struct nfs_inode
*nfsi
= NFS_I(inode
);
3055 struct nfs4_cached_acl
*acl
;
3058 spin_lock(&inode
->i_lock
);
3059 acl
= nfsi
->nfs4_acl
;
3062 if (buf
== NULL
) /* user is just asking for length */
3064 if (acl
->cached
== 0)
3066 ret
= -ERANGE
; /* see getxattr(2) man page */
3067 if (acl
->len
> buflen
)
3069 memcpy(buf
, acl
->data
, acl
->len
);
3073 spin_unlock(&inode
->i_lock
);
3077 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3079 struct nfs4_cached_acl
*acl
;
3081 if (buf
&& acl_len
<= PAGE_SIZE
) {
3082 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3086 memcpy(acl
->data
, buf
, acl_len
);
3088 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3095 nfs4_set_cached_acl(inode
, acl
);
3098 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3100 struct page
*pages
[NFS4ACL_MAXPAGES
];
3101 struct nfs_getaclargs args
= {
3102 .fh
= NFS_FH(inode
),
3106 struct nfs_getaclres res
= {
3110 struct rpc_message msg
= {
3111 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3115 struct page
*localpage
= NULL
;
3118 if (buflen
< PAGE_SIZE
) {
3119 /* As long as we're doing a round trip to the server anyway,
3120 * let's be prepared for a page of acl data. */
3121 localpage
= alloc_page(GFP_KERNEL
);
3122 resp_buf
= page_address(localpage
);
3123 if (localpage
== NULL
)
3125 args
.acl_pages
[0] = localpage
;
3126 args
.acl_pgbase
= 0;
3127 args
.acl_len
= PAGE_SIZE
;
3130 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3132 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3135 if (res
.acl_len
> args
.acl_len
)
3136 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3138 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3141 if (res
.acl_len
> buflen
)
3144 memcpy(buf
, resp_buf
, res
.acl_len
);
3149 __free_page(localpage
);
3153 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3155 struct nfs4_exception exception
= { };
3158 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3161 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3162 } while (exception
.retry
);
3166 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3168 struct nfs_server
*server
= NFS_SERVER(inode
);
3171 if (!nfs4_server_supports_acls(server
))
3173 ret
= nfs_revalidate_inode(server
, inode
);
3176 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
3177 nfs_zap_acl_cache(inode
);
3178 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3181 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3184 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3186 struct nfs_server
*server
= NFS_SERVER(inode
);
3187 struct page
*pages
[NFS4ACL_MAXPAGES
];
3188 struct nfs_setaclargs arg
= {
3189 .fh
= NFS_FH(inode
),
3193 struct nfs_setaclres res
;
3194 struct rpc_message msg
= {
3195 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3201 if (!nfs4_server_supports_acls(server
))
3203 nfs_inode_return_delegation(inode
);
3204 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3205 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3206 nfs_access_zap_cache(inode
);
3207 nfs_zap_acl_cache(inode
);
3211 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3213 struct nfs4_exception exception
= { };
3216 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3217 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3219 } while (exception
.retry
);
3224 _nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs_client
*clp
, struct nfs4_state
*state
)
3226 if (!clp
|| task
->tk_status
>= 0)
3228 switch(task
->tk_status
) {
3229 case -NFS4ERR_ADMIN_REVOKED
:
3230 case -NFS4ERR_BAD_STATEID
:
3231 case -NFS4ERR_OPENMODE
:
3234 nfs4_state_mark_reclaim_nograce(clp
, state
);
3235 case -NFS4ERR_STALE_CLIENTID
:
3236 case -NFS4ERR_STALE_STATEID
:
3237 case -NFS4ERR_EXPIRED
:
3238 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3239 nfs4_schedule_state_recovery(clp
);
3240 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3241 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3242 task
->tk_status
= 0;
3244 case -NFS4ERR_DELAY
:
3246 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3247 case -NFS4ERR_GRACE
:
3248 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3249 task
->tk_status
= 0;
3251 case -NFS4ERR_OLD_STATEID
:
3252 task
->tk_status
= 0;
3255 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3260 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3262 return _nfs4_async_handle_error(task
, server
, server
->nfs_client
, state
);
3265 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
, unsigned short port
, struct rpc_cred
*cred
)
3267 nfs4_verifier sc_verifier
;
3268 struct nfs4_setclientid setclientid
= {
3269 .sc_verifier
= &sc_verifier
,
3272 struct rpc_message msg
= {
3273 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3274 .rpc_argp
= &setclientid
,
3282 p
= (__be32
*)sc_verifier
.data
;
3283 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3284 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3287 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3288 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3290 rpc_peeraddr2str(clp
->cl_rpcclient
,
3292 rpc_peeraddr2str(clp
->cl_rpcclient
,
3294 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3295 clp
->cl_id_uniquifier
);
3296 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3297 sizeof(setclientid
.sc_netid
),
3298 rpc_peeraddr2str(clp
->cl_rpcclient
,
3299 RPC_DISPLAY_NETID
));
3300 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3301 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3302 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3304 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3305 if (status
!= -NFS4ERR_CLID_INUSE
)
3310 ssleep(clp
->cl_lease_time
+ 1);
3312 if (++clp
->cl_id_uniquifier
== 0)
3318 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3320 struct nfs_fsinfo fsinfo
;
3321 struct rpc_message msg
= {
3322 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3324 .rpc_resp
= &fsinfo
,
3331 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3333 spin_lock(&clp
->cl_lock
);
3334 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3335 clp
->cl_last_renewal
= now
;
3336 spin_unlock(&clp
->cl_lock
);
3341 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3346 err
= _nfs4_proc_setclientid_confirm(clp
, cred
);
3350 case -NFS4ERR_RESOURCE
:
3351 /* The IBM lawyers misread another document! */
3352 case -NFS4ERR_DELAY
:
3353 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3359 struct nfs4_delegreturndata
{
3360 struct nfs4_delegreturnargs args
;
3361 struct nfs4_delegreturnres res
;
3363 nfs4_stateid stateid
;
3364 unsigned long timestamp
;
3365 struct nfs_fattr fattr
;
3369 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3371 struct nfs4_delegreturndata
*data
= calldata
;
3373 nfs4_sequence_done_free_slot(data
->res
.server
, &data
->res
.seq_res
,
3376 data
->rpc_status
= task
->tk_status
;
3377 if (data
->rpc_status
== 0)
3378 renew_lease(data
->res
.server
, data
->timestamp
);
3381 static void nfs4_delegreturn_release(void *calldata
)
3386 #if defined(CONFIG_NFS_V4_1)
3387 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3389 struct nfs4_delegreturndata
*d_data
;
3391 d_data
= (struct nfs4_delegreturndata
*)data
;
3393 if (nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
3394 &d_data
->args
.seq_args
,
3395 &d_data
->res
.seq_res
, 1, task
))
3397 rpc_call_start(task
);
3399 #endif /* CONFIG_NFS_V4_1 */
3401 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3402 #if defined(CONFIG_NFS_V4_1)
3403 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3404 #endif /* CONFIG_NFS_V4_1 */
3405 .rpc_call_done
= nfs4_delegreturn_done
,
3406 .rpc_release
= nfs4_delegreturn_release
,
3409 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3411 struct nfs4_delegreturndata
*data
;
3412 struct nfs_server
*server
= NFS_SERVER(inode
);
3413 struct rpc_task
*task
;
3414 struct rpc_message msg
= {
3415 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3418 struct rpc_task_setup task_setup_data
= {
3419 .rpc_client
= server
->client
,
3420 .rpc_message
= &msg
,
3421 .callback_ops
= &nfs4_delegreturn_ops
,
3422 .flags
= RPC_TASK_ASYNC
,
3426 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3429 data
->args
.fhandle
= &data
->fh
;
3430 data
->args
.stateid
= &data
->stateid
;
3431 data
->args
.bitmask
= server
->attr_bitmask
;
3432 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3433 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3434 data
->res
.fattr
= &data
->fattr
;
3435 data
->res
.server
= server
;
3436 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3437 nfs_fattr_init(data
->res
.fattr
);
3438 data
->timestamp
= jiffies
;
3439 data
->rpc_status
= 0;
3441 task_setup_data
.callback_data
= data
;
3442 msg
.rpc_argp
= &data
->args
,
3443 msg
.rpc_resp
= &data
->res
,
3444 task
= rpc_run_task(&task_setup_data
);
3446 return PTR_ERR(task
);
3449 status
= nfs4_wait_for_completion_rpc_task(task
);
3452 status
= data
->rpc_status
;
3455 nfs_refresh_inode(inode
, &data
->fattr
);
3461 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3463 struct nfs_server
*server
= NFS_SERVER(inode
);
3464 struct nfs4_exception exception
= { };
3467 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3469 case -NFS4ERR_STALE_STATEID
:
3470 case -NFS4ERR_EXPIRED
:
3474 err
= nfs4_handle_exception(server
, err
, &exception
);
3475 } while (exception
.retry
);
3479 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3480 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3483 * sleep, with exponential backoff, and retry the LOCK operation.
3485 static unsigned long
3486 nfs4_set_lock_task_retry(unsigned long timeout
)
3488 schedule_timeout_killable(timeout
);
3490 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3491 return NFS4_LOCK_MAXTIMEOUT
;
3495 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3497 struct inode
*inode
= state
->inode
;
3498 struct nfs_server
*server
= NFS_SERVER(inode
);
3499 struct nfs_client
*clp
= server
->nfs_client
;
3500 struct nfs_lockt_args arg
= {
3501 .fh
= NFS_FH(inode
),
3504 struct nfs_lockt_res res
= {
3507 struct rpc_message msg
= {
3508 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3511 .rpc_cred
= state
->owner
->so_cred
,
3513 struct nfs4_lock_state
*lsp
;
3516 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3517 status
= nfs4_set_lock_state(state
, request
);
3520 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3521 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3522 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3525 request
->fl_type
= F_UNLCK
;
3527 case -NFS4ERR_DENIED
:
3530 request
->fl_ops
->fl_release_private(request
);
3535 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3537 struct nfs4_exception exception
= { };
3541 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3542 _nfs4_proc_getlk(state
, cmd
, request
),
3544 } while (exception
.retry
);
3548 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3551 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3553 res
= posix_lock_file_wait(file
, fl
);
3556 res
= flock_lock_file_wait(file
, fl
);
3564 struct nfs4_unlockdata
{
3565 struct nfs_locku_args arg
;
3566 struct nfs_locku_res res
;
3567 struct nfs4_lock_state
*lsp
;
3568 struct nfs_open_context
*ctx
;
3569 struct file_lock fl
;
3570 const struct nfs_server
*server
;
3571 unsigned long timestamp
;
3574 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3575 struct nfs_open_context
*ctx
,
3576 struct nfs4_lock_state
*lsp
,
3577 struct nfs_seqid
*seqid
)
3579 struct nfs4_unlockdata
*p
;
3580 struct inode
*inode
= lsp
->ls_state
->inode
;
3582 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3585 p
->arg
.fh
= NFS_FH(inode
);
3587 p
->arg
.seqid
= seqid
;
3588 p
->res
.seqid
= seqid
;
3589 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3590 p
->arg
.stateid
= &lsp
->ls_stateid
;
3592 atomic_inc(&lsp
->ls_count
);
3593 /* Ensure we don't close file until we're done freeing locks! */
3594 p
->ctx
= get_nfs_open_context(ctx
);
3595 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3596 p
->server
= NFS_SERVER(inode
);
3600 static void nfs4_locku_release_calldata(void *data
)
3602 struct nfs4_unlockdata
*calldata
= data
;
3603 nfs_free_seqid(calldata
->arg
.seqid
);
3604 nfs4_put_lock_state(calldata
->lsp
);
3605 put_nfs_open_context(calldata
->ctx
);
3609 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3611 struct nfs4_unlockdata
*calldata
= data
;
3613 nfs4_sequence_done(calldata
->server
, &calldata
->res
.seq_res
,
3615 if (RPC_ASSASSINATED(task
))
3617 switch (task
->tk_status
) {
3619 memcpy(calldata
->lsp
->ls_stateid
.data
,
3620 calldata
->res
.stateid
.data
,
3621 sizeof(calldata
->lsp
->ls_stateid
.data
));
3622 renew_lease(calldata
->server
, calldata
->timestamp
);
3624 case -NFS4ERR_BAD_STATEID
:
3625 case -NFS4ERR_OLD_STATEID
:
3626 case -NFS4ERR_STALE_STATEID
:
3627 case -NFS4ERR_EXPIRED
:
3630 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3631 nfs4_restart_rpc(task
,
3632 calldata
->server
->nfs_client
);
3634 nfs4_sequence_free_slot(calldata
->server
->nfs_client
,
3635 &calldata
->res
.seq_res
);
3638 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3640 struct nfs4_unlockdata
*calldata
= data
;
3642 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3644 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3645 /* Note: exit _without_ running nfs4_locku_done */
3646 task
->tk_action
= NULL
;
3649 calldata
->timestamp
= jiffies
;
3650 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
3651 &calldata
->arg
.seq_args
,
3652 &calldata
->res
.seq_res
, 1, task
))
3654 rpc_call_start(task
);
3657 static const struct rpc_call_ops nfs4_locku_ops
= {
3658 .rpc_call_prepare
= nfs4_locku_prepare
,
3659 .rpc_call_done
= nfs4_locku_done
,
3660 .rpc_release
= nfs4_locku_release_calldata
,
3663 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3664 struct nfs_open_context
*ctx
,
3665 struct nfs4_lock_state
*lsp
,
3666 struct nfs_seqid
*seqid
)
3668 struct nfs4_unlockdata
*data
;
3669 struct rpc_message msg
= {
3670 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3671 .rpc_cred
= ctx
->cred
,
3673 struct rpc_task_setup task_setup_data
= {
3674 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3675 .rpc_message
= &msg
,
3676 .callback_ops
= &nfs4_locku_ops
,
3677 .workqueue
= nfsiod_workqueue
,
3678 .flags
= RPC_TASK_ASYNC
,
3681 /* Ensure this is an unlock - when canceling a lock, the
3682 * canceled lock is passed in, and it won't be an unlock.
3684 fl
->fl_type
= F_UNLCK
;
3686 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3688 nfs_free_seqid(seqid
);
3689 return ERR_PTR(-ENOMEM
);
3692 msg
.rpc_argp
= &data
->arg
,
3693 msg
.rpc_resp
= &data
->res
,
3694 task_setup_data
.callback_data
= data
;
3695 return rpc_run_task(&task_setup_data
);
3698 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3700 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3701 struct nfs_seqid
*seqid
;
3702 struct nfs4_lock_state
*lsp
;
3703 struct rpc_task
*task
;
3705 unsigned char fl_flags
= request
->fl_flags
;
3707 status
= nfs4_set_lock_state(state
, request
);
3708 /* Unlock _before_ we do the RPC call */
3709 request
->fl_flags
|= FL_EXISTS
;
3710 down_read(&nfsi
->rwsem
);
3711 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3712 up_read(&nfsi
->rwsem
);
3715 up_read(&nfsi
->rwsem
);
3718 /* Is this a delegated lock? */
3719 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3721 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3722 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3726 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3727 status
= PTR_ERR(task
);
3730 status
= nfs4_wait_for_completion_rpc_task(task
);
3733 request
->fl_flags
= fl_flags
;
3737 struct nfs4_lockdata
{
3738 struct nfs_lock_args arg
;
3739 struct nfs_lock_res res
;
3740 struct nfs4_lock_state
*lsp
;
3741 struct nfs_open_context
*ctx
;
3742 struct file_lock fl
;
3743 unsigned long timestamp
;
3746 struct nfs_server
*server
;
3749 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3750 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
)
3752 struct nfs4_lockdata
*p
;
3753 struct inode
*inode
= lsp
->ls_state
->inode
;
3754 struct nfs_server
*server
= NFS_SERVER(inode
);
3756 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3760 p
->arg
.fh
= NFS_FH(inode
);
3762 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
);
3763 if (p
->arg
.open_seqid
== NULL
)
3765 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3766 if (p
->arg
.lock_seqid
== NULL
)
3767 goto out_free_seqid
;
3768 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
3769 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
3770 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3771 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
3772 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3775 atomic_inc(&lsp
->ls_count
);
3776 p
->ctx
= get_nfs_open_context(ctx
);
3777 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3780 nfs_free_seqid(p
->arg
.open_seqid
);
3786 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
3788 struct nfs4_lockdata
*data
= calldata
;
3789 struct nfs4_state
*state
= data
->lsp
->ls_state
;
3791 dprintk("%s: begin!\n", __func__
);
3792 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
3794 /* Do we need to do an open_to_lock_owner? */
3795 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
3796 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
3798 data
->arg
.open_stateid
= &state
->stateid
;
3799 data
->arg
.new_lock_owner
= 1;
3800 data
->res
.open_seqid
= data
->arg
.open_seqid
;
3802 data
->arg
.new_lock_owner
= 0;
3803 data
->timestamp
= jiffies
;
3804 if (nfs4_setup_sequence(data
->server
->nfs_client
, &data
->arg
.seq_args
,
3805 &data
->res
.seq_res
, 1, task
))
3807 rpc_call_start(task
);
3808 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
3811 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
3813 struct nfs4_lockdata
*data
= calldata
;
3815 dprintk("%s: begin!\n", __func__
);
3817 nfs4_sequence_done_free_slot(data
->server
, &data
->res
.seq_res
,
3820 data
->rpc_status
= task
->tk_status
;
3821 if (RPC_ASSASSINATED(task
))
3823 if (data
->arg
.new_lock_owner
!= 0) {
3824 if (data
->rpc_status
== 0)
3825 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
3829 if (data
->rpc_status
== 0) {
3830 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
3831 sizeof(data
->lsp
->ls_stateid
.data
));
3832 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
3833 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
3836 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
3839 static void nfs4_lock_release(void *calldata
)
3841 struct nfs4_lockdata
*data
= calldata
;
3843 dprintk("%s: begin!\n", __func__
);
3844 nfs_free_seqid(data
->arg
.open_seqid
);
3845 if (data
->cancelled
!= 0) {
3846 struct rpc_task
*task
;
3847 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
3848 data
->arg
.lock_seqid
);
3851 dprintk("%s: cancelling lock!\n", __func__
);
3853 nfs_free_seqid(data
->arg
.lock_seqid
);
3854 nfs4_put_lock_state(data
->lsp
);
3855 put_nfs_open_context(data
->ctx
);
3857 dprintk("%s: done!\n", __func__
);
3860 static const struct rpc_call_ops nfs4_lock_ops
= {
3861 .rpc_call_prepare
= nfs4_lock_prepare
,
3862 .rpc_call_done
= nfs4_lock_done
,
3863 .rpc_release
= nfs4_lock_release
,
3866 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int reclaim
)
3868 struct nfs4_lockdata
*data
;
3869 struct rpc_task
*task
;
3870 struct rpc_message msg
= {
3871 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
3872 .rpc_cred
= state
->owner
->so_cred
,
3874 struct rpc_task_setup task_setup_data
= {
3875 .rpc_client
= NFS_CLIENT(state
->inode
),
3876 .rpc_message
= &msg
,
3877 .callback_ops
= &nfs4_lock_ops
,
3878 .workqueue
= nfsiod_workqueue
,
3879 .flags
= RPC_TASK_ASYNC
,
3883 dprintk("%s: begin!\n", __func__
);
3884 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
3885 fl
->fl_u
.nfs4_fl
.owner
);
3889 data
->arg
.block
= 1;
3891 data
->arg
.reclaim
= 1;
3892 msg
.rpc_argp
= &data
->arg
,
3893 msg
.rpc_resp
= &data
->res
,
3894 task_setup_data
.callback_data
= data
;
3895 task
= rpc_run_task(&task_setup_data
);
3897 return PTR_ERR(task
);
3898 ret
= nfs4_wait_for_completion_rpc_task(task
);
3900 ret
= data
->rpc_status
;
3901 if (ret
== -NFS4ERR_DENIED
)
3904 data
->cancelled
= 1;
3906 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
3910 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
3912 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3913 struct nfs4_exception exception
= { };
3917 /* Cache the lock if possible... */
3918 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
3920 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 1);
3921 if (err
!= -NFS4ERR_DELAY
)
3923 nfs4_handle_exception(server
, err
, &exception
);
3924 } while (exception
.retry
);
3928 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
3930 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3931 struct nfs4_exception exception
= { };
3934 err
= nfs4_set_lock_state(state
, request
);
3938 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
3940 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 0);
3941 if (err
!= -NFS4ERR_DELAY
)
3943 nfs4_handle_exception(server
, err
, &exception
);
3944 } while (exception
.retry
);
3948 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3950 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3951 unsigned char fl_flags
= request
->fl_flags
;
3954 /* Is this a delegated open? */
3955 status
= nfs4_set_lock_state(state
, request
);
3958 request
->fl_flags
|= FL_ACCESS
;
3959 status
= do_vfs_lock(request
->fl_file
, request
);
3962 down_read(&nfsi
->rwsem
);
3963 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
3964 /* Yes: cache locks! */
3965 /* ...but avoid races with delegation recall... */
3966 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
3967 status
= do_vfs_lock(request
->fl_file
, request
);
3970 status
= _nfs4_do_setlk(state
, cmd
, request
, 0);
3973 /* Note: we always want to sleep here! */
3974 request
->fl_flags
= fl_flags
| FL_SLEEP
;
3975 if (do_vfs_lock(request
->fl_file
, request
) < 0)
3976 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
3978 up_read(&nfsi
->rwsem
);
3980 request
->fl_flags
= fl_flags
;
3984 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3986 struct nfs4_exception exception
= { };
3990 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3991 _nfs4_proc_setlk(state
, cmd
, request
),
3993 } while (exception
.retry
);
3998 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4000 struct nfs_open_context
*ctx
;
4001 struct nfs4_state
*state
;
4002 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4005 /* verify open state */
4006 ctx
= nfs_file_open_context(filp
);
4009 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4013 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4015 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4018 if (request
->fl_type
== F_UNLCK
)
4019 return nfs4_proc_unlck(state
, cmd
, request
);
4022 status
= nfs4_proc_setlk(state
, cmd
, request
);
4023 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4025 timeout
= nfs4_set_lock_task_retry(timeout
);
4026 status
= -ERESTARTSYS
;
4029 } while(status
< 0);
4033 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4035 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4036 struct nfs4_exception exception
= { };
4039 err
= nfs4_set_lock_state(state
, fl
);
4043 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, 0);
4044 if (err
!= -NFS4ERR_DELAY
)
4046 err
= nfs4_handle_exception(server
, err
, &exception
);
4047 } while (exception
.retry
);
4052 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4054 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4055 size_t buflen
, int flags
)
4057 struct inode
*inode
= dentry
->d_inode
;
4059 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4062 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4065 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4066 * and that's what we'll do for e.g. user attributes that haven't been set.
4067 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4068 * attributes in kernel-managed attribute namespaces. */
4069 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4072 struct inode
*inode
= dentry
->d_inode
;
4074 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4077 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4080 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4082 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4084 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4086 if (buf
&& buflen
< len
)
4089 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4093 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4095 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4096 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4097 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4100 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4101 NFS_ATTR_FATTR_NLINK
;
4102 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4106 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4107 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4109 struct nfs_server
*server
= NFS_SERVER(dir
);
4111 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4112 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4114 struct nfs4_fs_locations_arg args
= {
4115 .dir_fh
= NFS_FH(dir
),
4120 struct nfs4_fs_locations_res res
= {
4121 .fs_locations
= fs_locations
,
4123 struct rpc_message msg
= {
4124 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4130 dprintk("%s: start\n", __func__
);
4131 nfs_fattr_init(&fs_locations
->fattr
);
4132 fs_locations
->server
= server
;
4133 fs_locations
->nlocations
= 0;
4134 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4135 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4136 dprintk("%s: returned status = %d\n", __func__
, status
);
4140 #ifdef CONFIG_NFS_V4_1
4142 * nfs4_proc_exchange_id()
4144 * Since the clientid has expired, all compounds using sessions
4145 * associated with the stale clientid will be returning
4146 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4147 * be in some phase of session reset.
4149 static int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4151 nfs4_verifier verifier
;
4152 struct nfs41_exchange_id_args args
= {
4154 .flags
= clp
->cl_exchange_flags
,
4156 struct nfs41_exchange_id_res res
= {
4160 struct rpc_message msg
= {
4161 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4168 dprintk("--> %s\n", __func__
);
4169 BUG_ON(clp
== NULL
);
4170 p
= (u32
*)verifier
.data
;
4171 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4172 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4173 args
.verifier
= &verifier
;
4176 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4179 rpc_peeraddr2str(clp
->cl_rpcclient
,
4181 clp
->cl_id_uniquifier
);
4183 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4185 if (status
!= NFS4ERR_CLID_INUSE
)
4191 if (++clp
->cl_id_uniquifier
== 0)
4195 dprintk("<-- %s status= %d\n", __func__
, status
);
4199 struct nfs4_get_lease_time_data
{
4200 struct nfs4_get_lease_time_args
*args
;
4201 struct nfs4_get_lease_time_res
*res
;
4202 struct nfs_client
*clp
;
4205 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4209 struct nfs4_get_lease_time_data
*data
=
4210 (struct nfs4_get_lease_time_data
*)calldata
;
4212 dprintk("--> %s\n", __func__
);
4213 /* just setup sequence, do not trigger session recovery
4214 since we're invoked within one */
4215 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4216 &data
->args
->la_seq_args
,
4217 &data
->res
->lr_seq_res
, 0, task
);
4219 BUG_ON(ret
== -EAGAIN
);
4220 rpc_call_start(task
);
4221 dprintk("<-- %s\n", __func__
);
4225 * Called from nfs4_state_manager thread for session setup, so don't recover
4226 * from sequence operation or clientid errors.
4228 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4230 struct nfs4_get_lease_time_data
*data
=
4231 (struct nfs4_get_lease_time_data
*)calldata
;
4233 dprintk("--> %s\n", __func__
);
4234 nfs41_sequence_done(data
->clp
, &data
->res
->lr_seq_res
, task
->tk_status
);
4235 switch (task
->tk_status
) {
4236 case -NFS4ERR_DELAY
:
4237 case -NFS4ERR_GRACE
:
4238 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4239 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4240 task
->tk_status
= 0;
4241 nfs4_restart_rpc(task
, data
->clp
);
4244 nfs41_sequence_free_slot(data
->clp
, &data
->res
->lr_seq_res
);
4245 dprintk("<-- %s\n", __func__
);
4248 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4249 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4250 .rpc_call_done
= nfs4_get_lease_time_done
,
4253 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4255 struct rpc_task
*task
;
4256 struct nfs4_get_lease_time_args args
;
4257 struct nfs4_get_lease_time_res res
= {
4258 .lr_fsinfo
= fsinfo
,
4260 struct nfs4_get_lease_time_data data
= {
4265 struct rpc_message msg
= {
4266 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4270 struct rpc_task_setup task_setup
= {
4271 .rpc_client
= clp
->cl_rpcclient
,
4272 .rpc_message
= &msg
,
4273 .callback_ops
= &nfs4_get_lease_time_ops
,
4274 .callback_data
= &data
4278 res
.lr_seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4279 dprintk("--> %s\n", __func__
);
4280 task
= rpc_run_task(&task_setup
);
4283 status
= PTR_ERR(task
);
4285 status
= task
->tk_status
;
4288 dprintk("<-- %s return %d\n", __func__
, status
);
4293 /* Reset a slot table */
4294 static int nfs4_reset_slot_table(struct nfs4_session
*session
)
4296 struct nfs4_slot_table
*tbl
= &session
->fc_slot_table
;
4297 int i
, max_slots
= session
->fc_attrs
.max_reqs
;
4298 int old_max_slots
= session
->fc_slot_table
.max_slots
;
4301 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__
,
4302 session
->fc_attrs
.max_reqs
, tbl
);
4304 /* Until we have dynamic slot table adjustment, insist
4305 * upon the same slot table size */
4306 if (max_slots
!= old_max_slots
) {
4307 dprintk("%s reset slot table does't match old\n",
4309 ret
= -EINVAL
; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4312 spin_lock(&tbl
->slot_tbl_lock
);
4313 for (i
= 0; i
< max_slots
; ++i
)
4314 tbl
->slots
[i
].seq_nr
= 1;
4315 tbl
->highest_used_slotid
= -1;
4316 spin_unlock(&tbl
->slot_tbl_lock
);
4317 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4318 tbl
, tbl
->slots
, tbl
->max_slots
);
4320 dprintk("<-- %s: return %d\n", __func__
, ret
);
4325 * Initialize slot table
4327 static int nfs4_init_slot_table(struct nfs4_session
*session
)
4329 struct nfs4_slot_table
*tbl
= &session
->fc_slot_table
;
4330 int i
, max_slots
= session
->fc_attrs
.max_reqs
;
4331 struct nfs4_slot
*slot
;
4334 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4336 dprintk("--> %s: max_reqs=%u\n", __func__
,
4337 session
->fc_attrs
.max_reqs
);
4339 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_KERNEL
);
4342 for (i
= 0; i
< max_slots
; ++i
)
4346 spin_lock(&tbl
->slot_tbl_lock
);
4347 if (tbl
->slots
!= NULL
) {
4348 spin_unlock(&tbl
->slot_tbl_lock
);
4349 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4350 __func__
, tbl
, tbl
->slots
);
4354 tbl
->max_slots
= max_slots
;
4356 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4357 spin_unlock(&tbl
->slot_tbl_lock
);
4358 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4359 tbl
, tbl
->slots
, tbl
->max_slots
);
4361 dprintk("<-- %s: return %d\n", __func__
, ret
);
4368 /* Destroy the slot table */
4369 static void nfs4_destroy_slot_table(struct nfs4_session
*session
)
4371 if (session
->fc_slot_table
.slots
== NULL
)
4373 kfree(session
->fc_slot_table
.slots
);
4374 session
->fc_slot_table
.slots
= NULL
;
4378 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4380 struct nfs4_session
*session
;
4381 struct nfs4_slot_table
*tbl
;
4383 session
= kzalloc(sizeof(struct nfs4_session
), GFP_KERNEL
);
4387 set_bit(NFS4CLNT_SESSION_SETUP
, &clp
->cl_state
);
4389 * The create session reply races with the server back
4390 * channel probe. Mark the client NFS_CS_SESSION_INITING
4391 * so that the client back channel can find the
4394 clp
->cl_cons_state
= NFS_CS_SESSION_INITING
;
4396 tbl
= &session
->fc_slot_table
;
4397 spin_lock_init(&tbl
->slot_tbl_lock
);
4398 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "Slot table");
4403 void nfs4_destroy_session(struct nfs4_session
*session
)
4405 nfs4_destroy_slot_table(session
);
4410 * Initialize the values to be used by the client in CREATE_SESSION
4411 * If nfs4_init_session set the fore channel request and response sizes,
4414 * Set the back channel max_resp_sz_cached to zero to force the client to
4415 * always set csa_cachethis to FALSE because the current implementation
4416 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4418 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4420 struct nfs4_session
*session
= args
->client
->cl_session
;
4421 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4422 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4425 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4427 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4428 /* Fore channel attributes */
4429 args
->fc_attrs
.headerpadsz
= 0;
4430 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4431 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4432 args
->fc_attrs
.max_resp_sz_cached
= mxresp_sz
;
4433 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4434 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4436 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4437 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4439 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4440 args
->fc_attrs
.max_resp_sz_cached
, args
->fc_attrs
.max_ops
,
4441 args
->fc_attrs
.max_reqs
);
4443 /* Back channel attributes */
4444 args
->bc_attrs
.headerpadsz
= 0;
4445 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4446 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4447 args
->bc_attrs
.max_resp_sz_cached
= 0;
4448 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4449 args
->bc_attrs
.max_reqs
= 1;
4451 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4452 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4454 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4455 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4456 args
->bc_attrs
.max_reqs
);
4459 static int _verify_channel_attr(char *chan
, char *attr_name
, u32 sent
, u32 rcvd
)
4463 printk(KERN_WARNING
"%s: Session INVALID: %s channel %s increased. "
4464 "sent=%u rcvd=%u\n", __func__
, chan
, attr_name
, sent
, rcvd
);
4468 #define _verify_fore_channel_attr(_name_) \
4469 _verify_channel_attr("fore", #_name_, \
4470 args->fc_attrs._name_, \
4471 session->fc_attrs._name_)
4473 #define _verify_back_channel_attr(_name_) \
4474 _verify_channel_attr("back", #_name_, \
4475 args->bc_attrs._name_, \
4476 session->bc_attrs._name_)
4479 * The server is not allowed to increase the fore channel header pad size,
4480 * maximum response size, or maximum number of operations.
4482 * The back channel attributes are only negotiatied down: We send what the
4483 * (back channel) server insists upon.
4485 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4486 struct nfs4_session
*session
)
4490 ret
|= _verify_fore_channel_attr(headerpadsz
);
4491 ret
|= _verify_fore_channel_attr(max_resp_sz
);
4492 ret
|= _verify_fore_channel_attr(max_ops
);
4494 ret
|= _verify_back_channel_attr(headerpadsz
);
4495 ret
|= _verify_back_channel_attr(max_rqst_sz
);
4496 ret
|= _verify_back_channel_attr(max_resp_sz
);
4497 ret
|= _verify_back_channel_attr(max_resp_sz_cached
);
4498 ret
|= _verify_back_channel_attr(max_ops
);
4499 ret
|= _verify_back_channel_attr(max_reqs
);
4504 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4506 struct nfs4_session
*session
= clp
->cl_session
;
4507 struct nfs41_create_session_args args
= {
4509 .cb_program
= NFS4_CALLBACK
,
4511 struct nfs41_create_session_res res
= {
4514 struct rpc_message msg
= {
4515 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4521 nfs4_init_channel_attrs(&args
);
4522 args
.flags
= (SESSION4_PERSIST
);
4524 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4527 /* Verify the session's negotiated channel_attrs values */
4528 status
= nfs4_verify_channel_attrs(&args
, session
);
4530 /* Increment the clientid slot sequence id */
4538 * Issues a CREATE_SESSION operation to the server.
4539 * It is the responsibility of the caller to verify the session is
4540 * expired before calling this routine.
4542 int nfs4_proc_create_session(struct nfs_client
*clp
, int reset
)
4546 struct nfs_fsinfo fsinfo
;
4547 struct nfs4_session
*session
= clp
->cl_session
;
4549 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4551 status
= _nfs4_proc_create_session(clp
);
4555 /* Init or reset the fore channel */
4557 status
= nfs4_reset_slot_table(session
);
4559 status
= nfs4_init_slot_table(session
);
4560 dprintk("fore channel slot table initialization returned %d\n", status
);
4564 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4565 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4566 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4569 /* Lease time is aleady set */
4572 /* Get the lease time */
4573 status
= nfs4_proc_get_lease_time(clp
, &fsinfo
);
4575 /* Update lease time and schedule renewal */
4576 spin_lock(&clp
->cl_lock
);
4577 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
4578 clp
->cl_last_renewal
= jiffies
;
4579 clear_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
4580 spin_unlock(&clp
->cl_lock
);
4582 nfs4_schedule_state_renewal(clp
);
4585 dprintk("<-- %s\n", __func__
);
4590 * Issue the over-the-wire RPC DESTROY_SESSION.
4591 * The caller must serialize access to this routine.
4593 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4596 struct rpc_message msg
;
4598 dprintk("--> nfs4_proc_destroy_session\n");
4600 /* session is still being setup */
4601 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
4604 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
4605 msg
.rpc_argp
= session
;
4606 msg
.rpc_resp
= NULL
;
4607 msg
.rpc_cred
= NULL
;
4608 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4612 "Got error %d from the server on DESTROY_SESSION. "
4613 "Session has been destroyed regardless...\n", status
);
4615 dprintk("<-- nfs4_proc_destroy_session\n");
4620 * Renew the cl_session lease.
4622 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4624 struct nfs4_sequence_args args
;
4625 struct nfs4_sequence_res res
;
4627 struct rpc_message msg
= {
4628 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
4634 args
.sa_cache_this
= 0;
4636 return nfs4_call_sync_sequence(clp
, clp
->cl_rpcclient
, &msg
, &args
,
4640 void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
4642 struct nfs_client
*clp
= (struct nfs_client
*)data
;
4644 nfs41_sequence_done(clp
, task
->tk_msg
.rpc_resp
, task
->tk_status
);
4646 if (task
->tk_status
< 0) {
4647 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
4649 if (_nfs4_async_handle_error(task
, NULL
, clp
, NULL
)
4651 nfs4_restart_rpc(task
, clp
);
4655 nfs41_sequence_free_slot(clp
, task
->tk_msg
.rpc_resp
);
4656 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
4658 put_rpccred(task
->tk_msg
.rpc_cred
);
4659 kfree(task
->tk_msg
.rpc_argp
);
4660 kfree(task
->tk_msg
.rpc_resp
);
4662 dprintk("<-- %s\n", __func__
);
4665 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
4667 struct nfs_client
*clp
;
4668 struct nfs4_sequence_args
*args
;
4669 struct nfs4_sequence_res
*res
;
4671 clp
= (struct nfs_client
*)data
;
4672 args
= task
->tk_msg
.rpc_argp
;
4673 res
= task
->tk_msg
.rpc_resp
;
4675 if (nfs4_setup_sequence(clp
, args
, res
, 0, task
))
4677 rpc_call_start(task
);
4680 static const struct rpc_call_ops nfs41_sequence_ops
= {
4681 .rpc_call_done
= nfs41_sequence_call_done
,
4682 .rpc_call_prepare
= nfs41_sequence_prepare
,
4685 static int nfs41_proc_async_sequence(struct nfs_client
*clp
,
4686 struct rpc_cred
*cred
)
4688 struct nfs4_sequence_args
*args
;
4689 struct nfs4_sequence_res
*res
;
4690 struct rpc_message msg
= {
4691 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
4695 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
4698 res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
4703 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4704 msg
.rpc_argp
= args
;
4707 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
4708 &nfs41_sequence_ops
, (void *)clp
);
4711 #endif /* CONFIG_NFS_V4_1 */
4713 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops
= {
4714 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
4715 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
4716 .recover_open
= nfs4_open_reclaim
,
4717 .recover_lock
= nfs4_lock_reclaim
,
4720 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops
= {
4721 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
4722 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
4723 .recover_open
= nfs4_open_expired
,
4724 .recover_lock
= nfs4_lock_expired
,
4727 static const struct inode_operations nfs4_file_inode_operations
= {
4728 .permission
= nfs_permission
,
4729 .getattr
= nfs_getattr
,
4730 .setattr
= nfs_setattr
,
4731 .getxattr
= nfs4_getxattr
,
4732 .setxattr
= nfs4_setxattr
,
4733 .listxattr
= nfs4_listxattr
,
4736 const struct nfs_rpc_ops nfs_v4_clientops
= {
4737 .version
= 4, /* protocol version */
4738 .dentry_ops
= &nfs4_dentry_operations
,
4739 .dir_inode_ops
= &nfs4_dir_inode_operations
,
4740 .file_inode_ops
= &nfs4_file_inode_operations
,
4741 .getroot
= nfs4_proc_get_root
,
4742 .getattr
= nfs4_proc_getattr
,
4743 .setattr
= nfs4_proc_setattr
,
4744 .lookupfh
= nfs4_proc_lookupfh
,
4745 .lookup
= nfs4_proc_lookup
,
4746 .access
= nfs4_proc_access
,
4747 .readlink
= nfs4_proc_readlink
,
4748 .create
= nfs4_proc_create
,
4749 .remove
= nfs4_proc_remove
,
4750 .unlink_setup
= nfs4_proc_unlink_setup
,
4751 .unlink_done
= nfs4_proc_unlink_done
,
4752 .rename
= nfs4_proc_rename
,
4753 .link
= nfs4_proc_link
,
4754 .symlink
= nfs4_proc_symlink
,
4755 .mkdir
= nfs4_proc_mkdir
,
4756 .rmdir
= nfs4_proc_remove
,
4757 .readdir
= nfs4_proc_readdir
,
4758 .mknod
= nfs4_proc_mknod
,
4759 .statfs
= nfs4_proc_statfs
,
4760 .fsinfo
= nfs4_proc_fsinfo
,
4761 .pathconf
= nfs4_proc_pathconf
,
4762 .set_capabilities
= nfs4_server_capabilities
,
4763 .decode_dirent
= nfs4_decode_dirent
,
4764 .read_setup
= nfs4_proc_read_setup
,
4765 .read_done
= nfs4_read_done
,
4766 .write_setup
= nfs4_proc_write_setup
,
4767 .write_done
= nfs4_write_done
,
4768 .commit_setup
= nfs4_proc_commit_setup
,
4769 .commit_done
= nfs4_commit_done
,
4770 .lock
= nfs4_proc_lock
,
4771 .clear_acl_cache
= nfs4_zap_acl_attr
,
4772 .close_context
= nfs4_close_context
,