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>
53 #include "delegation.h"
57 #define NFSDBG_FACILITY NFSDBG_PROC
59 #define NFS4_POLL_RETRY_MIN (HZ/10)
60 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
64 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
65 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
66 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
67 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
69 /* Prevent leaks of NFSv4 errors into userland */
70 static int nfs4_map_errors(int err
)
73 dprintk("%s could not handle NFSv4 error %d\n",
81 * This is our standard bitmap for GETATTR requests.
83 const u32 nfs4_fattr_bitmap
[2] = {
88 | FATTR4_WORD0_FILEID
,
90 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
100 const u32 nfs4_statfs_bitmap
[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL
,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
109 const u32 nfs4_pathconf_bitmap
[2] = {
111 | FATTR4_WORD0_MAXNAME
,
115 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME
,
122 const u32 nfs4_fs_locations_bitmap
[2] = {
124 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS
,
130 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
141 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
142 struct nfs4_readdir_arg
*readdir
)
146 BUG_ON(readdir
->count
< 80);
148 readdir
->cookie
= cookie
;
149 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
154 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
165 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
168 *p
++ = xdr_one
; /* next */
169 *p
++ = xdr_zero
; /* cookie, first word */
170 *p
++ = xdr_one
; /* cookie, second word */
171 *p
++ = xdr_one
; /* entry len */
172 memcpy(p
, ".\0\0\0", 4); /* entry */
174 *p
++ = xdr_one
; /* bitmap length */
175 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
176 *p
++ = htonl(8); /* attribute buffer length */
177 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
180 *p
++ = xdr_one
; /* next */
181 *p
++ = xdr_zero
; /* cookie, first word */
182 *p
++ = xdr_two
; /* cookie, second word */
183 *p
++ = xdr_two
; /* entry len */
184 memcpy(p
, "..\0\0", 4); /* entry */
186 *p
++ = xdr_one
; /* bitmap length */
187 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
188 *p
++ = htonl(8); /* attribute buffer length */
189 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
191 readdir
->pgbase
= (char *)p
- (char *)start
;
192 readdir
->count
-= readdir
->pgbase
;
193 kunmap_atomic(start
, KM_USER0
);
196 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
202 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
203 nfs_wait_bit_killable
, TASK_KILLABLE
);
207 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
214 *timeout
= NFS4_POLL_RETRY_MIN
;
215 if (*timeout
> NFS4_POLL_RETRY_MAX
)
216 *timeout
= NFS4_POLL_RETRY_MAX
;
217 schedule_timeout_killable(*timeout
);
218 if (fatal_signal_pending(current
))
224 /* This is the error handling routine for processes that are allowed
227 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
229 struct nfs_client
*clp
= server
->nfs_client
;
230 struct nfs4_state
*state
= exception
->state
;
233 exception
->retry
= 0;
237 case -NFS4ERR_ADMIN_REVOKED
:
238 case -NFS4ERR_BAD_STATEID
:
239 case -NFS4ERR_OPENMODE
:
242 nfs4_state_mark_reclaim_nograce(clp
, state
);
243 case -NFS4ERR_STALE_CLIENTID
:
244 case -NFS4ERR_STALE_STATEID
:
245 case -NFS4ERR_EXPIRED
:
246 nfs4_schedule_state_recovery(clp
);
247 ret
= nfs4_wait_clnt_recover(clp
);
249 exception
->retry
= 1;
251 case -NFS4ERR_FILE_OPEN
:
254 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
257 case -NFS4ERR_OLD_STATEID
:
258 exception
->retry
= 1;
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret
);
265 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
267 struct nfs_client
*clp
= server
->nfs_client
;
268 spin_lock(&clp
->cl_lock
);
269 if (time_before(clp
->cl_last_renewal
,timestamp
))
270 clp
->cl_last_renewal
= timestamp
;
271 spin_unlock(&clp
->cl_lock
);
274 #if defined(CONFIG_NFS_V4_1)
277 * nfs4_free_slot - free a slot and efficiently update slot table.
279 * freeing a slot is trivially done by clearing its respective bit
281 * If the freed slotid equals highest_used_slotid we want to update it
282 * so that the server would be able to size down the slot table if needed,
283 * otherwise we know that the highest_used_slotid is still in use.
284 * When updating highest_used_slotid there may be "holes" in the bitmap
285 * so we need to scan down from highest_used_slotid to 0 looking for the now
286 * highest slotid in use.
287 * If none found, highest_used_slotid is set to -1.
290 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
292 int slotid
= free_slotid
;
294 spin_lock(&tbl
->slot_tbl_lock
);
295 /* clear used bit in bitmap */
296 __clear_bit(slotid
, tbl
->used_slots
);
298 /* update highest_used_slotid when it is freed */
299 if (slotid
== tbl
->highest_used_slotid
) {
300 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
301 if (slotid
>= 0 && slotid
< tbl
->max_slots
)
302 tbl
->highest_used_slotid
= slotid
;
304 tbl
->highest_used_slotid
= -1;
306 rpc_wake_up_next(&tbl
->slot_tbl_waitq
);
307 spin_unlock(&tbl
->slot_tbl_lock
);
308 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
309 free_slotid
, tbl
->highest_used_slotid
);
312 void nfs41_sequence_free_slot(const struct nfs_client
*clp
,
313 struct nfs4_sequence_res
*res
)
315 struct nfs4_slot_table
*tbl
;
317 if (!nfs4_has_session(clp
)) {
318 dprintk("%s: No session\n", __func__
);
321 tbl
= &clp
->cl_session
->fc_slot_table
;
322 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
323 dprintk("%s: No slot\n", __func__
);
324 /* just wake up the next guy waiting since
325 * we may have not consumed a slot after all */
326 rpc_wake_up_next(&tbl
->slot_tbl_waitq
);
329 nfs4_free_slot(tbl
, res
->sr_slotid
);
330 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
333 static void nfs41_sequence_done(struct nfs_client
*clp
,
334 struct nfs4_sequence_res
*res
,
337 unsigned long timestamp
;
338 struct nfs4_slot_table
*tbl
;
339 struct nfs4_slot
*slot
;
342 * sr_status remains 1 if an RPC level error occurred. The server
343 * may or may not have processed the sequence operation..
344 * Proceed as if the server received and processed the sequence
347 if (res
->sr_status
== 1)
348 res
->sr_status
= NFS_OK
;
350 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
351 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
354 tbl
= &clp
->cl_session
->fc_slot_table
;
355 slot
= tbl
->slots
+ res
->sr_slotid
;
357 if (res
->sr_status
== 0) {
358 /* Update the slot's sequence and clientid lease timer */
360 timestamp
= res
->sr_renewal_time
;
361 spin_lock(&clp
->cl_lock
);
362 if (time_before(clp
->cl_last_renewal
, timestamp
))
363 clp
->cl_last_renewal
= timestamp
;
364 spin_unlock(&clp
->cl_lock
);
368 /* The session may be reset by one of the error handlers. */
369 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
370 nfs41_sequence_free_slot(clp
, res
);
374 * nfs4_find_slot - efficiently look for a free slot
376 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
377 * If found, we mark the slot as used, update the highest_used_slotid,
378 * and respectively set up the sequence operation args.
379 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
381 * Note: must be called with under the slot_tbl_lock.
384 nfs4_find_slot(struct nfs4_slot_table
*tbl
, struct rpc_task
*task
)
387 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
388 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
390 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
391 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
393 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
394 if (slotid
>= tbl
->max_slots
)
396 __set_bit(slotid
, tbl
->used_slots
);
397 if (slotid
> tbl
->highest_used_slotid
)
398 tbl
->highest_used_slotid
= slotid
;
401 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
402 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
406 static int nfs41_setup_sequence(struct nfs4_session
*session
,
407 struct nfs4_sequence_args
*args
,
408 struct nfs4_sequence_res
*res
,
410 struct rpc_task
*task
)
412 struct nfs4_slot
*slot
;
413 struct nfs4_slot_table
*tbl
;
416 dprintk("--> %s\n", __func__
);
417 /* slot already allocated? */
418 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
421 memset(res
, 0, sizeof(*res
));
422 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
423 tbl
= &session
->fc_slot_table
;
425 spin_lock(&tbl
->slot_tbl_lock
);
426 slotid
= nfs4_find_slot(tbl
, task
);
427 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
428 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
429 spin_unlock(&tbl
->slot_tbl_lock
);
430 dprintk("<-- %s: no free slots\n", __func__
);
433 spin_unlock(&tbl
->slot_tbl_lock
);
435 slot
= tbl
->slots
+ slotid
;
436 args
->sa_slotid
= slotid
;
437 args
->sa_cache_this
= cache_reply
;
439 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
441 res
->sr_slotid
= slotid
;
442 res
->sr_renewal_time
= jiffies
;
444 * sr_status is only set in decode_sequence, and so will remain
445 * set to 1 if an rpc level failure occurs.
451 int nfs4_setup_sequence(struct nfs_client
*clp
,
452 struct nfs4_sequence_args
*args
,
453 struct nfs4_sequence_res
*res
,
455 struct rpc_task
*task
)
459 dprintk("--> %s clp %p session %p sr_slotid %d\n",
460 __func__
, clp
, clp
->cl_session
, res
->sr_slotid
);
462 if (!nfs4_has_session(clp
))
464 ret
= nfs41_setup_sequence(clp
->cl_session
, args
, res
, cache_reply
,
466 if (ret
!= -EAGAIN
) {
467 /* terminate rpc task */
468 task
->tk_status
= ret
;
469 task
->tk_action
= NULL
;
472 dprintk("<-- %s status=%d\n", __func__
, ret
);
476 struct nfs41_call_sync_data
{
477 struct nfs_client
*clp
;
478 struct nfs4_sequence_args
*seq_args
;
479 struct nfs4_sequence_res
*seq_res
;
483 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
485 struct nfs41_call_sync_data
*data
= calldata
;
487 dprintk("--> %s data->clp->cl_session %p\n", __func__
,
488 data
->clp
->cl_session
);
489 if (nfs4_setup_sequence(data
->clp
, data
->seq_args
,
490 data
->seq_res
, data
->cache_reply
, task
))
492 rpc_call_start(task
);
495 struct rpc_call_ops nfs41_call_sync_ops
= {
496 .rpc_call_prepare
= nfs41_call_sync_prepare
,
499 static int nfs4_call_sync_sequence(struct nfs_client
*clp
,
500 struct rpc_clnt
*clnt
,
501 struct rpc_message
*msg
,
502 struct nfs4_sequence_args
*args
,
503 struct nfs4_sequence_res
*res
,
507 struct rpc_task
*task
;
508 struct nfs41_call_sync_data data
= {
512 .cache_reply
= cache_reply
,
514 struct rpc_task_setup task_setup
= {
517 .callback_ops
= &nfs41_call_sync_ops
,
518 .callback_data
= &data
521 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
522 task
= rpc_run_task(&task_setup
);
526 ret
= task
->tk_status
;
532 int _nfs4_call_sync_session(struct nfs_server
*server
,
533 struct rpc_message
*msg
,
534 struct nfs4_sequence_args
*args
,
535 struct nfs4_sequence_res
*res
,
538 return nfs4_call_sync_sequence(server
->nfs_client
, server
->client
,
539 msg
, args
, res
, cache_reply
);
542 #endif /* CONFIG_NFS_V4_1 */
544 int _nfs4_call_sync(struct nfs_server
*server
,
545 struct rpc_message
*msg
,
546 struct nfs4_sequence_args
*args
,
547 struct nfs4_sequence_res
*res
,
550 args
->sa_session
= res
->sr_session
= NULL
;
551 return rpc_call_sync(server
->client
, msg
, 0);
554 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
555 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
556 &(res)->seq_res, (cache_reply))
558 static void nfs4_sequence_done(const struct nfs_server
*server
,
559 struct nfs4_sequence_res
*res
, int rpc_status
)
561 #ifdef CONFIG_NFS_V4_1
562 if (nfs4_has_session(server
->nfs_client
))
563 nfs41_sequence_done(server
->nfs_client
, res
, rpc_status
);
564 #endif /* CONFIG_NFS_V4_1 */
567 /* no restart, therefore free slot here */
568 static void nfs4_sequence_done_free_slot(const struct nfs_server
*server
,
569 struct nfs4_sequence_res
*res
,
572 nfs4_sequence_done(server
, res
, rpc_status
);
573 nfs4_sequence_free_slot(server
->nfs_client
, res
);
576 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
578 struct nfs_inode
*nfsi
= NFS_I(dir
);
580 spin_lock(&dir
->i_lock
);
581 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
582 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
583 nfs_force_lookup_revalidate(dir
);
584 nfsi
->change_attr
= cinfo
->after
;
585 spin_unlock(&dir
->i_lock
);
588 struct nfs4_opendata
{
590 struct nfs_openargs o_arg
;
591 struct nfs_openres o_res
;
592 struct nfs_open_confirmargs c_arg
;
593 struct nfs_open_confirmres c_res
;
594 struct nfs_fattr f_attr
;
595 struct nfs_fattr dir_attr
;
598 struct nfs4_state_owner
*owner
;
599 struct nfs4_state
*state
;
601 unsigned long timestamp
;
602 unsigned int rpc_done
: 1;
608 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
610 p
->o_res
.f_attr
= &p
->f_attr
;
611 p
->o_res
.dir_attr
= &p
->dir_attr
;
612 p
->o_res
.seqid
= p
->o_arg
.seqid
;
613 p
->c_res
.seqid
= p
->c_arg
.seqid
;
614 p
->o_res
.server
= p
->o_arg
.server
;
615 nfs_fattr_init(&p
->f_attr
);
616 nfs_fattr_init(&p
->dir_attr
);
619 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
620 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
621 const struct iattr
*attrs
)
623 struct dentry
*parent
= dget_parent(path
->dentry
);
624 struct inode
*dir
= parent
->d_inode
;
625 struct nfs_server
*server
= NFS_SERVER(dir
);
626 struct nfs4_opendata
*p
;
628 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
631 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
632 if (p
->o_arg
.seqid
== NULL
)
634 p
->path
.mnt
= mntget(path
->mnt
);
635 p
->path
.dentry
= dget(path
->dentry
);
638 atomic_inc(&sp
->so_count
);
639 p
->o_arg
.fh
= NFS_FH(dir
);
640 p
->o_arg
.open_flags
= flags
;
641 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
642 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
643 p
->o_arg
.id
= sp
->so_owner_id
.id
;
644 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
645 p
->o_arg
.server
= server
;
646 p
->o_arg
.bitmask
= server
->attr_bitmask
;
647 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
648 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
649 if (flags
& O_EXCL
) {
650 u32
*s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
653 } else if (flags
& O_CREAT
) {
654 p
->o_arg
.u
.attrs
= &p
->attrs
;
655 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
657 p
->c_arg
.fh
= &p
->o_res
.fh
;
658 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
659 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
660 nfs4_init_opendata_res(p
);
670 static void nfs4_opendata_free(struct kref
*kref
)
672 struct nfs4_opendata
*p
= container_of(kref
,
673 struct nfs4_opendata
, kref
);
675 nfs_free_seqid(p
->o_arg
.seqid
);
676 if (p
->state
!= NULL
)
677 nfs4_put_open_state(p
->state
);
678 nfs4_put_state_owner(p
->owner
);
684 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
687 kref_put(&p
->kref
, nfs4_opendata_free
);
690 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
694 ret
= rpc_wait_for_completion_task(task
);
698 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
702 if (open_mode
& O_EXCL
)
704 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
706 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0;
709 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0;
711 case FMODE_READ
|FMODE_WRITE
:
712 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0;
718 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
720 if ((delegation
->type
& fmode
) != fmode
)
722 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
724 nfs_mark_delegation_referenced(delegation
);
728 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
737 case FMODE_READ
|FMODE_WRITE
:
740 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
743 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
745 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
746 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
747 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
750 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
753 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
755 case FMODE_READ
|FMODE_WRITE
:
756 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
760 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
762 write_seqlock(&state
->seqlock
);
763 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
764 write_sequnlock(&state
->seqlock
);
767 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
770 * Protect the call to nfs4_state_set_mode_locked and
771 * serialise the stateid update
773 write_seqlock(&state
->seqlock
);
774 if (deleg_stateid
!= NULL
) {
775 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
776 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
778 if (open_stateid
!= NULL
)
779 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
780 write_sequnlock(&state
->seqlock
);
781 spin_lock(&state
->owner
->so_lock
);
782 update_open_stateflags(state
, fmode
);
783 spin_unlock(&state
->owner
->so_lock
);
786 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
788 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
789 struct nfs_delegation
*deleg_cur
;
792 fmode
&= (FMODE_READ
|FMODE_WRITE
);
795 deleg_cur
= rcu_dereference(nfsi
->delegation
);
796 if (deleg_cur
== NULL
)
799 spin_lock(&deleg_cur
->lock
);
800 if (nfsi
->delegation
!= deleg_cur
||
801 (deleg_cur
->type
& fmode
) != fmode
)
802 goto no_delegation_unlock
;
804 if (delegation
== NULL
)
805 delegation
= &deleg_cur
->stateid
;
806 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
807 goto no_delegation_unlock
;
809 nfs_mark_delegation_referenced(deleg_cur
);
810 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
812 no_delegation_unlock
:
813 spin_unlock(&deleg_cur
->lock
);
817 if (!ret
&& open_stateid
!= NULL
) {
818 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
826 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
828 struct nfs_delegation
*delegation
;
831 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
832 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
837 nfs_inode_return_delegation(inode
);
840 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
842 struct nfs4_state
*state
= opendata
->state
;
843 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
844 struct nfs_delegation
*delegation
;
845 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
846 fmode_t fmode
= opendata
->o_arg
.fmode
;
847 nfs4_stateid stateid
;
851 if (can_open_cached(state
, fmode
, open_mode
)) {
852 spin_lock(&state
->owner
->so_lock
);
853 if (can_open_cached(state
, fmode
, open_mode
)) {
854 update_open_stateflags(state
, fmode
);
855 spin_unlock(&state
->owner
->so_lock
);
856 goto out_return_state
;
858 spin_unlock(&state
->owner
->so_lock
);
861 delegation
= rcu_dereference(nfsi
->delegation
);
862 if (delegation
== NULL
||
863 !can_open_delegated(delegation
, fmode
)) {
867 /* Save the delegation */
868 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
870 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
875 /* Try to update the stateid using the delegation */
876 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
877 goto out_return_state
;
882 atomic_inc(&state
->count
);
886 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
889 struct nfs4_state
*state
= NULL
;
890 struct nfs_delegation
*delegation
;
893 if (!data
->rpc_done
) {
894 state
= nfs4_try_open_cached(data
);
899 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
901 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
902 ret
= PTR_ERR(inode
);
906 state
= nfs4_get_open_state(inode
, data
->owner
);
909 if (data
->o_res
.delegation_type
!= 0) {
910 int delegation_flags
= 0;
913 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
915 delegation_flags
= delegation
->flags
;
917 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
918 nfs_inode_set_delegation(state
->inode
,
919 data
->owner
->so_cred
,
922 nfs_inode_reclaim_delegation(state
->inode
,
923 data
->owner
->so_cred
,
927 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
938 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
940 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
941 struct nfs_open_context
*ctx
;
943 spin_lock(&state
->inode
->i_lock
);
944 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
945 if (ctx
->state
!= state
)
947 get_nfs_open_context(ctx
);
948 spin_unlock(&state
->inode
->i_lock
);
951 spin_unlock(&state
->inode
->i_lock
);
952 return ERR_PTR(-ENOENT
);
955 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
957 struct nfs4_opendata
*opendata
;
959 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
);
960 if (opendata
== NULL
)
961 return ERR_PTR(-ENOMEM
);
962 opendata
->state
= state
;
963 atomic_inc(&state
->count
);
967 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
969 struct nfs4_state
*newstate
;
972 opendata
->o_arg
.open_flags
= 0;
973 opendata
->o_arg
.fmode
= fmode
;
974 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
975 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
976 nfs4_init_opendata_res(opendata
);
977 ret
= _nfs4_proc_open(opendata
);
980 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
981 if (IS_ERR(newstate
))
982 return PTR_ERR(newstate
);
983 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
988 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
990 struct nfs4_state
*newstate
;
993 /* memory barrier prior to reading state->n_* */
994 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
996 if (state
->n_rdwr
!= 0) {
997 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1000 if (newstate
!= state
)
1003 if (state
->n_wronly
!= 0) {
1004 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1007 if (newstate
!= state
)
1010 if (state
->n_rdonly
!= 0) {
1011 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1014 if (newstate
!= state
)
1018 * We may have performed cached opens for all three recoveries.
1019 * Check if we need to update the current stateid.
1021 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1022 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1023 write_seqlock(&state
->seqlock
);
1024 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1025 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1026 write_sequnlock(&state
->seqlock
);
1033 * reclaim state on the server after a reboot.
1035 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1037 struct nfs_delegation
*delegation
;
1038 struct nfs4_opendata
*opendata
;
1039 fmode_t delegation_type
= 0;
1042 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1043 if (IS_ERR(opendata
))
1044 return PTR_ERR(opendata
);
1045 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1046 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1048 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1049 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1050 delegation_type
= delegation
->type
;
1052 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1053 status
= nfs4_open_recover(opendata
, state
);
1054 nfs4_opendata_put(opendata
);
1058 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1060 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1061 struct nfs4_exception exception
= { };
1064 err
= _nfs4_do_open_reclaim(ctx
, state
);
1065 if (err
!= -NFS4ERR_DELAY
)
1067 nfs4_handle_exception(server
, err
, &exception
);
1068 } while (exception
.retry
);
1072 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1074 struct nfs_open_context
*ctx
;
1077 ctx
= nfs4_state_find_open_context(state
);
1079 return PTR_ERR(ctx
);
1080 ret
= nfs4_do_open_reclaim(ctx
, state
);
1081 put_nfs_open_context(ctx
);
1085 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1087 struct nfs4_opendata
*opendata
;
1090 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1091 if (IS_ERR(opendata
))
1092 return PTR_ERR(opendata
);
1093 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1094 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1095 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1096 ret
= nfs4_open_recover(opendata
, state
);
1097 nfs4_opendata_put(opendata
);
1101 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1103 struct nfs4_exception exception
= { };
1104 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1107 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1111 case -NFS4ERR_STALE_CLIENTID
:
1112 case -NFS4ERR_STALE_STATEID
:
1113 case -NFS4ERR_EXPIRED
:
1114 /* Don't recall a delegation if it was lost */
1115 nfs4_schedule_state_recovery(server
->nfs_client
);
1118 err
= nfs4_handle_exception(server
, err
, &exception
);
1119 } while (exception
.retry
);
1123 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1125 struct nfs4_opendata
*data
= calldata
;
1127 data
->rpc_status
= task
->tk_status
;
1128 if (RPC_ASSASSINATED(task
))
1130 if (data
->rpc_status
== 0) {
1131 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1132 sizeof(data
->o_res
.stateid
.data
));
1133 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1134 renew_lease(data
->o_res
.server
, data
->timestamp
);
1139 static void nfs4_open_confirm_release(void *calldata
)
1141 struct nfs4_opendata
*data
= calldata
;
1142 struct nfs4_state
*state
= NULL
;
1144 /* If this request hasn't been cancelled, do nothing */
1145 if (data
->cancelled
== 0)
1147 /* In case of error, no cleanup! */
1148 if (!data
->rpc_done
)
1150 state
= nfs4_opendata_to_nfs4_state(data
);
1152 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1154 nfs4_opendata_put(data
);
1157 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1158 .rpc_call_done
= nfs4_open_confirm_done
,
1159 .rpc_release
= nfs4_open_confirm_release
,
1163 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1165 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1167 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1168 struct rpc_task
*task
;
1169 struct rpc_message msg
= {
1170 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1171 .rpc_argp
= &data
->c_arg
,
1172 .rpc_resp
= &data
->c_res
,
1173 .rpc_cred
= data
->owner
->so_cred
,
1175 struct rpc_task_setup task_setup_data
= {
1176 .rpc_client
= server
->client
,
1177 .rpc_message
= &msg
,
1178 .callback_ops
= &nfs4_open_confirm_ops
,
1179 .callback_data
= data
,
1180 .workqueue
= nfsiod_workqueue
,
1181 .flags
= RPC_TASK_ASYNC
,
1185 kref_get(&data
->kref
);
1187 data
->rpc_status
= 0;
1188 data
->timestamp
= jiffies
;
1189 task
= rpc_run_task(&task_setup_data
);
1191 return PTR_ERR(task
);
1192 status
= nfs4_wait_for_completion_rpc_task(task
);
1194 data
->cancelled
= 1;
1197 status
= data
->rpc_status
;
1202 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1204 struct nfs4_opendata
*data
= calldata
;
1205 struct nfs4_state_owner
*sp
= data
->owner
;
1207 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1210 * Check if we still need to send an OPEN call, or if we can use
1211 * a delegation instead.
1213 if (data
->state
!= NULL
) {
1214 struct nfs_delegation
*delegation
;
1216 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1219 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1220 if (delegation
!= NULL
&&
1221 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1227 /* Update sequence id. */
1228 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1229 data
->o_arg
.clientid
= sp
->so_client
->cl_clientid
;
1230 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1231 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1232 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1234 data
->timestamp
= jiffies
;
1235 rpc_call_start(task
);
1238 task
->tk_action
= NULL
;
1242 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1244 struct nfs4_opendata
*data
= calldata
;
1246 data
->rpc_status
= task
->tk_status
;
1247 if (RPC_ASSASSINATED(task
))
1249 if (task
->tk_status
== 0) {
1250 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1254 data
->rpc_status
= -ELOOP
;
1257 data
->rpc_status
= -EISDIR
;
1260 data
->rpc_status
= -ENOTDIR
;
1262 renew_lease(data
->o_res
.server
, data
->timestamp
);
1263 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1264 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1269 static void nfs4_open_release(void *calldata
)
1271 struct nfs4_opendata
*data
= calldata
;
1272 struct nfs4_state
*state
= NULL
;
1274 /* If this request hasn't been cancelled, do nothing */
1275 if (data
->cancelled
== 0)
1277 /* In case of error, no cleanup! */
1278 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1280 /* In case we need an open_confirm, no cleanup! */
1281 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1283 state
= nfs4_opendata_to_nfs4_state(data
);
1285 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1287 nfs4_opendata_put(data
);
1290 static const struct rpc_call_ops nfs4_open_ops
= {
1291 .rpc_call_prepare
= nfs4_open_prepare
,
1292 .rpc_call_done
= nfs4_open_done
,
1293 .rpc_release
= nfs4_open_release
,
1297 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1299 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1301 struct inode
*dir
= data
->dir
->d_inode
;
1302 struct nfs_server
*server
= NFS_SERVER(dir
);
1303 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1304 struct nfs_openres
*o_res
= &data
->o_res
;
1305 struct rpc_task
*task
;
1306 struct rpc_message msg
= {
1307 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1310 .rpc_cred
= data
->owner
->so_cred
,
1312 struct rpc_task_setup task_setup_data
= {
1313 .rpc_client
= server
->client
,
1314 .rpc_message
= &msg
,
1315 .callback_ops
= &nfs4_open_ops
,
1316 .callback_data
= data
,
1317 .workqueue
= nfsiod_workqueue
,
1318 .flags
= RPC_TASK_ASYNC
,
1322 kref_get(&data
->kref
);
1324 data
->rpc_status
= 0;
1325 data
->cancelled
= 0;
1326 task
= rpc_run_task(&task_setup_data
);
1328 return PTR_ERR(task
);
1329 status
= nfs4_wait_for_completion_rpc_task(task
);
1331 data
->cancelled
= 1;
1334 status
= data
->rpc_status
;
1336 if (status
!= 0 || !data
->rpc_done
)
1339 if (o_res
->fh
.size
== 0)
1340 _nfs4_proc_lookup(dir
, o_arg
->name
, &o_res
->fh
, o_res
->f_attr
);
1342 if (o_arg
->open_flags
& O_CREAT
) {
1343 update_changeattr(dir
, &o_res
->cinfo
);
1344 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1346 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1347 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1348 status
= _nfs4_proc_open_confirm(data
);
1352 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1353 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1357 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1359 struct nfs_client
*clp
= server
->nfs_client
;
1363 ret
= nfs4_wait_clnt_recover(clp
);
1366 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1367 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1369 nfs4_schedule_state_recovery(clp
);
1376 * reclaim state on the server after a network partition.
1377 * Assumes caller holds the appropriate lock
1379 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1381 struct nfs4_opendata
*opendata
;
1384 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1385 if (IS_ERR(opendata
))
1386 return PTR_ERR(opendata
);
1387 ret
= nfs4_open_recover(opendata
, state
);
1389 d_drop(ctx
->path
.dentry
);
1390 nfs4_opendata_put(opendata
);
1394 static inline int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1396 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1397 struct nfs4_exception exception
= { };
1401 err
= _nfs4_open_expired(ctx
, state
);
1402 if (err
!= -NFS4ERR_DELAY
)
1404 nfs4_handle_exception(server
, err
, &exception
);
1405 } while (exception
.retry
);
1409 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1411 struct nfs_open_context
*ctx
;
1414 ctx
= nfs4_state_find_open_context(state
);
1416 return PTR_ERR(ctx
);
1417 ret
= nfs4_do_open_expired(ctx
, state
);
1418 put_nfs_open_context(ctx
);
1423 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1424 * fields corresponding to attributes that were used to store the verifier.
1425 * Make sure we clobber those fields in the later setattr call
1427 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1429 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1430 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1431 sattr
->ia_valid
|= ATTR_ATIME
;
1433 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1434 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1435 sattr
->ia_valid
|= ATTR_MTIME
;
1439 * Returns a referenced nfs4_state
1441 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
)
1443 struct nfs4_state_owner
*sp
;
1444 struct nfs4_state
*state
= NULL
;
1445 struct nfs_server
*server
= NFS_SERVER(dir
);
1446 struct nfs4_opendata
*opendata
;
1449 /* Protect against reboot recovery conflicts */
1451 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1452 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1455 status
= nfs4_recover_expired_lease(server
);
1457 goto err_put_state_owner
;
1458 if (path
->dentry
->d_inode
!= NULL
)
1459 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1461 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
);
1462 if (opendata
== NULL
)
1463 goto err_put_state_owner
;
1465 if (path
->dentry
->d_inode
!= NULL
)
1466 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1468 status
= _nfs4_proc_open(opendata
);
1470 goto err_opendata_put
;
1472 if (opendata
->o_arg
.open_flags
& O_EXCL
)
1473 nfs4_exclusive_attrset(opendata
, sattr
);
1475 state
= nfs4_opendata_to_nfs4_state(opendata
);
1476 status
= PTR_ERR(state
);
1478 goto err_opendata_put
;
1479 nfs4_opendata_put(opendata
);
1480 nfs4_put_state_owner(sp
);
1484 nfs4_opendata_put(opendata
);
1485 err_put_state_owner
:
1486 nfs4_put_state_owner(sp
);
1493 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
)
1495 struct nfs4_exception exception
= { };
1496 struct nfs4_state
*res
;
1500 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1503 /* NOTE: BAD_SEQID means the server and client disagree about the
1504 * book-keeping w.r.t. state-changing operations
1505 * (OPEN/CLOSE/LOCK/LOCKU...)
1506 * It is actually a sign of a bug on the client or on the server.
1508 * If we receive a BAD_SEQID error in the particular case of
1509 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1510 * have unhashed the old state_owner for us, and that we can
1511 * therefore safely retry using a new one. We should still warn
1512 * the user though...
1514 if (status
== -NFS4ERR_BAD_SEQID
) {
1515 printk(KERN_WARNING
"NFS: v4 server %s "
1516 " returned a bad sequence-id error!\n",
1517 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1518 exception
.retry
= 1;
1522 * BAD_STATEID on OPEN means that the server cancelled our
1523 * state before it received the OPEN_CONFIRM.
1524 * Recover by retrying the request as per the discussion
1525 * on Page 181 of RFC3530.
1527 if (status
== -NFS4ERR_BAD_STATEID
) {
1528 exception
.retry
= 1;
1531 if (status
== -EAGAIN
) {
1532 /* We must have found a delegation */
1533 exception
.retry
= 1;
1536 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1537 status
, &exception
));
1538 } while (exception
.retry
);
1542 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1543 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1544 struct nfs4_state
*state
)
1546 struct nfs_server
*server
= NFS_SERVER(inode
);
1547 struct nfs_setattrargs arg
= {
1548 .fh
= NFS_FH(inode
),
1551 .bitmask
= server
->attr_bitmask
,
1553 struct nfs_setattrres res
= {
1557 struct rpc_message msg
= {
1558 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1563 unsigned long timestamp
= jiffies
;
1566 nfs_fattr_init(fattr
);
1568 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1569 /* Use that stateid */
1570 } else if (state
!= NULL
) {
1571 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1573 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1575 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1576 if (status
== 0 && state
!= NULL
)
1577 renew_lease(server
, timestamp
);
1581 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1582 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1583 struct nfs4_state
*state
)
1585 struct nfs_server
*server
= NFS_SERVER(inode
);
1586 struct nfs4_exception exception
= { };
1589 err
= nfs4_handle_exception(server
,
1590 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1592 } while (exception
.retry
);
1596 struct nfs4_closedata
{
1598 struct inode
*inode
;
1599 struct nfs4_state
*state
;
1600 struct nfs_closeargs arg
;
1601 struct nfs_closeres res
;
1602 struct nfs_fattr fattr
;
1603 unsigned long timestamp
;
1606 static void nfs4_free_closedata(void *data
)
1608 struct nfs4_closedata
*calldata
= data
;
1609 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1611 nfs4_put_open_state(calldata
->state
);
1612 nfs_free_seqid(calldata
->arg
.seqid
);
1613 nfs4_put_state_owner(sp
);
1614 path_put(&calldata
->path
);
1618 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1620 struct nfs4_closedata
*calldata
= data
;
1621 struct nfs4_state
*state
= calldata
->state
;
1622 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1624 if (RPC_ASSASSINATED(task
))
1626 /* hmm. we are done with the inode, and in the process of freeing
1627 * the state_owner. we keep this around to process errors
1629 switch (task
->tk_status
) {
1631 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1632 renew_lease(server
, calldata
->timestamp
);
1634 case -NFS4ERR_STALE_STATEID
:
1635 case -NFS4ERR_OLD_STATEID
:
1636 case -NFS4ERR_BAD_STATEID
:
1637 case -NFS4ERR_EXPIRED
:
1638 if (calldata
->arg
.fmode
== 0)
1641 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
) {
1642 rpc_restart_call(task
);
1646 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1649 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1651 struct nfs4_closedata
*calldata
= data
;
1652 struct nfs4_state
*state
= calldata
->state
;
1653 int clear_rd
, clear_wr
, clear_rdwr
;
1655 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1658 clear_rd
= clear_wr
= clear_rdwr
= 0;
1659 spin_lock(&state
->owner
->so_lock
);
1660 /* Calculate the change in open mode */
1661 if (state
->n_rdwr
== 0) {
1662 if (state
->n_rdonly
== 0) {
1663 clear_rd
|= test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1664 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1666 if (state
->n_wronly
== 0) {
1667 clear_wr
|= test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1668 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1671 spin_unlock(&state
->owner
->so_lock
);
1672 if (!clear_rd
&& !clear_wr
&& !clear_rdwr
) {
1673 /* Note: exit _without_ calling nfs4_close_done */
1674 task
->tk_action
= NULL
;
1677 nfs_fattr_init(calldata
->res
.fattr
);
1678 if (test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0) {
1679 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1680 calldata
->arg
.fmode
= FMODE_READ
;
1681 } else if (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0) {
1682 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1683 calldata
->arg
.fmode
= FMODE_WRITE
;
1685 calldata
->timestamp
= jiffies
;
1686 rpc_call_start(task
);
1689 static const struct rpc_call_ops nfs4_close_ops
= {
1690 .rpc_call_prepare
= nfs4_close_prepare
,
1691 .rpc_call_done
= nfs4_close_done
,
1692 .rpc_release
= nfs4_free_closedata
,
1696 * It is possible for data to be read/written from a mem-mapped file
1697 * after the sys_close call (which hits the vfs layer as a flush).
1698 * This means that we can't safely call nfsv4 close on a file until
1699 * the inode is cleared. This in turn means that we are not good
1700 * NFSv4 citizens - we do not indicate to the server to update the file's
1701 * share state even when we are done with one of the three share
1702 * stateid's in the inode.
1704 * NOTE: Caller must be holding the sp->so_owner semaphore!
1706 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, int wait
)
1708 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1709 struct nfs4_closedata
*calldata
;
1710 struct nfs4_state_owner
*sp
= state
->owner
;
1711 struct rpc_task
*task
;
1712 struct rpc_message msg
= {
1713 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1714 .rpc_cred
= state
->owner
->so_cred
,
1716 struct rpc_task_setup task_setup_data
= {
1717 .rpc_client
= server
->client
,
1718 .rpc_message
= &msg
,
1719 .callback_ops
= &nfs4_close_ops
,
1720 .workqueue
= nfsiod_workqueue
,
1721 .flags
= RPC_TASK_ASYNC
,
1723 int status
= -ENOMEM
;
1725 calldata
= kmalloc(sizeof(*calldata
), GFP_KERNEL
);
1726 if (calldata
== NULL
)
1728 calldata
->inode
= state
->inode
;
1729 calldata
->state
= state
;
1730 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1731 calldata
->arg
.stateid
= &state
->open_stateid
;
1732 /* Serialization for the sequence id */
1733 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
1734 if (calldata
->arg
.seqid
== NULL
)
1735 goto out_free_calldata
;
1736 calldata
->arg
.fmode
= 0;
1737 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1738 calldata
->res
.fattr
= &calldata
->fattr
;
1739 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1740 calldata
->res
.server
= server
;
1741 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1742 calldata
->path
.mnt
= mntget(path
->mnt
);
1743 calldata
->path
.dentry
= dget(path
->dentry
);
1745 msg
.rpc_argp
= &calldata
->arg
,
1746 msg
.rpc_resp
= &calldata
->res
,
1747 task_setup_data
.callback_data
= calldata
;
1748 task
= rpc_run_task(&task_setup_data
);
1750 return PTR_ERR(task
);
1753 status
= rpc_wait_for_completion_task(task
);
1759 nfs4_put_open_state(state
);
1760 nfs4_put_state_owner(sp
);
1764 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1769 /* If the open_intent is for execute, we have an extra check to make */
1770 if (fmode
& FMODE_EXEC
) {
1771 ret
= nfs_may_open(state
->inode
,
1772 state
->owner
->so_cred
,
1773 nd
->intent
.open
.flags
);
1777 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1778 if (!IS_ERR(filp
)) {
1779 struct nfs_open_context
*ctx
;
1780 ctx
= nfs_file_open_context(filp
);
1784 ret
= PTR_ERR(filp
);
1786 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
1791 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1793 struct path path
= {
1794 .mnt
= nd
->path
.mnt
,
1797 struct dentry
*parent
;
1799 struct rpc_cred
*cred
;
1800 struct nfs4_state
*state
;
1802 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
1804 if (nd
->flags
& LOOKUP_CREATE
) {
1805 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
1806 attr
.ia_valid
= ATTR_MODE
;
1807 if (!IS_POSIXACL(dir
))
1808 attr
.ia_mode
&= ~current_umask();
1811 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
1814 cred
= rpc_lookup_cred();
1816 return (struct dentry
*)cred
;
1817 parent
= dentry
->d_parent
;
1818 /* Protect against concurrent sillydeletes */
1819 nfs_block_sillyrename(parent
);
1820 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
1822 if (IS_ERR(state
)) {
1823 if (PTR_ERR(state
) == -ENOENT
) {
1824 d_add(dentry
, NULL
);
1825 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1827 nfs_unblock_sillyrename(parent
);
1828 return (struct dentry
*)state
;
1830 res
= d_add_unique(dentry
, igrab(state
->inode
));
1833 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
1834 nfs_unblock_sillyrename(parent
);
1835 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1840 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
1842 struct path path
= {
1843 .mnt
= nd
->path
.mnt
,
1846 struct rpc_cred
*cred
;
1847 struct nfs4_state
*state
;
1848 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
1850 cred
= rpc_lookup_cred();
1852 return PTR_ERR(cred
);
1853 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
1855 if (IS_ERR(state
)) {
1856 switch (PTR_ERR(state
)) {
1862 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
1868 if (state
->inode
== dentry
->d_inode
) {
1869 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1870 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1873 nfs4_close_sync(&path
, state
, fmode
);
1879 void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
1881 if (ctx
->state
== NULL
)
1884 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
1886 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
1889 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1891 struct nfs4_server_caps_arg args
= {
1894 struct nfs4_server_caps_res res
= {};
1895 struct rpc_message msg
= {
1896 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
1902 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1904 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
1905 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
1906 server
->caps
|= NFS_CAP_ACLS
;
1907 if (res
.has_links
!= 0)
1908 server
->caps
|= NFS_CAP_HARDLINKS
;
1909 if (res
.has_symlinks
!= 0)
1910 server
->caps
|= NFS_CAP_SYMLINKS
;
1911 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
1912 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
1913 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
1914 server
->acl_bitmask
= res
.acl_bitmask
;
1920 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1922 struct nfs4_exception exception
= { };
1925 err
= nfs4_handle_exception(server
,
1926 _nfs4_server_capabilities(server
, fhandle
),
1928 } while (exception
.retry
);
1932 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1933 struct nfs_fsinfo
*info
)
1935 struct nfs4_lookup_root_arg args
= {
1936 .bitmask
= nfs4_fattr_bitmap
,
1938 struct nfs4_lookup_res res
= {
1940 .fattr
= info
->fattr
,
1943 struct rpc_message msg
= {
1944 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
1948 nfs_fattr_init(info
->fattr
);
1949 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1952 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1953 struct nfs_fsinfo
*info
)
1955 struct nfs4_exception exception
= { };
1958 err
= nfs4_handle_exception(server
,
1959 _nfs4_lookup_root(server
, fhandle
, info
),
1961 } while (exception
.retry
);
1966 * get the file handle for the "/" directory on the server
1968 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1969 struct nfs_fsinfo
*info
)
1973 status
= nfs4_lookup_root(server
, fhandle
, info
);
1975 status
= nfs4_server_capabilities(server
, fhandle
);
1977 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
1978 return nfs4_map_errors(status
);
1982 * Get locations and (maybe) other attributes of a referral.
1983 * Note that we'll actually follow the referral later when
1984 * we detect fsid mismatch in inode revalidation
1986 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
1988 int status
= -ENOMEM
;
1989 struct page
*page
= NULL
;
1990 struct nfs4_fs_locations
*locations
= NULL
;
1992 page
= alloc_page(GFP_KERNEL
);
1995 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
1996 if (locations
== NULL
)
1999 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2002 /* Make sure server returned a different fsid for the referral */
2003 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2004 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2009 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2010 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2012 fattr
->mode
= S_IFDIR
;
2013 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2022 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2024 struct nfs4_getattr_arg args
= {
2026 .bitmask
= server
->attr_bitmask
,
2028 struct nfs4_getattr_res res
= {
2032 struct rpc_message msg
= {
2033 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2038 nfs_fattr_init(fattr
);
2039 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2042 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2044 struct nfs4_exception exception
= { };
2047 err
= nfs4_handle_exception(server
,
2048 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2050 } while (exception
.retry
);
2055 * The file is not closed if it is opened due to the a request to change
2056 * the size of the file. The open call will not be needed once the
2057 * VFS layer lookup-intents are implemented.
2059 * Close is called when the inode is destroyed.
2060 * If we haven't opened the file for O_WRONLY, we
2061 * need to in the size_change case to obtain a stateid.
2064 * Because OPEN is always done by name in nfsv4, it is
2065 * possible that we opened a different file by the same
2066 * name. We can recognize this race condition, but we
2067 * can't do anything about it besides returning an error.
2069 * This will be fixed with VFS changes (lookup-intent).
2072 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2073 struct iattr
*sattr
)
2075 struct inode
*inode
= dentry
->d_inode
;
2076 struct rpc_cred
*cred
= NULL
;
2077 struct nfs4_state
*state
= NULL
;
2080 nfs_fattr_init(fattr
);
2082 /* Search for an existing open(O_WRITE) file */
2083 if (sattr
->ia_valid
& ATTR_FILE
) {
2084 struct nfs_open_context
*ctx
;
2086 ctx
= nfs_file_open_context(sattr
->ia_file
);
2093 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2095 nfs_setattr_update_inode(inode
, sattr
);
2099 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2100 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2101 struct nfs_fattr
*fattr
)
2104 struct nfs4_lookup_arg args
= {
2105 .bitmask
= server
->attr_bitmask
,
2109 struct nfs4_lookup_res res
= {
2114 struct rpc_message msg
= {
2115 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2120 nfs_fattr_init(fattr
);
2122 dprintk("NFS call lookupfh %s\n", name
->name
);
2123 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2124 dprintk("NFS reply lookupfh: %d\n", status
);
2128 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2129 struct qstr
*name
, struct nfs_fh
*fhandle
,
2130 struct nfs_fattr
*fattr
)
2132 struct nfs4_exception exception
= { };
2135 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2137 if (err
== -NFS4ERR_MOVED
) {
2141 err
= nfs4_handle_exception(server
, err
, &exception
);
2142 } while (exception
.retry
);
2146 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2147 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2151 dprintk("NFS call lookup %s\n", name
->name
);
2152 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2153 if (status
== -NFS4ERR_MOVED
)
2154 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2155 dprintk("NFS reply lookup: %d\n", status
);
2159 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2161 struct nfs4_exception exception
= { };
2164 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2165 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2167 } while (exception
.retry
);
2171 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2173 struct nfs_server
*server
= NFS_SERVER(inode
);
2174 struct nfs_fattr fattr
;
2175 struct nfs4_accessargs args
= {
2176 .fh
= NFS_FH(inode
),
2177 .bitmask
= server
->attr_bitmask
,
2179 struct nfs4_accessres res
= {
2183 struct rpc_message msg
= {
2184 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2187 .rpc_cred
= entry
->cred
,
2189 int mode
= entry
->mask
;
2193 * Determine which access bits we want to ask for...
2195 if (mode
& MAY_READ
)
2196 args
.access
|= NFS4_ACCESS_READ
;
2197 if (S_ISDIR(inode
->i_mode
)) {
2198 if (mode
& MAY_WRITE
)
2199 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2200 if (mode
& MAY_EXEC
)
2201 args
.access
|= NFS4_ACCESS_LOOKUP
;
2203 if (mode
& MAY_WRITE
)
2204 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2205 if (mode
& MAY_EXEC
)
2206 args
.access
|= NFS4_ACCESS_EXECUTE
;
2208 nfs_fattr_init(&fattr
);
2209 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2212 if (res
.access
& NFS4_ACCESS_READ
)
2213 entry
->mask
|= MAY_READ
;
2214 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2215 entry
->mask
|= MAY_WRITE
;
2216 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2217 entry
->mask
|= MAY_EXEC
;
2218 nfs_refresh_inode(inode
, &fattr
);
2223 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2225 struct nfs4_exception exception
= { };
2228 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2229 _nfs4_proc_access(inode
, entry
),
2231 } while (exception
.retry
);
2236 * TODO: For the time being, we don't try to get any attributes
2237 * along with any of the zero-copy operations READ, READDIR,
2240 * In the case of the first three, we want to put the GETATTR
2241 * after the read-type operation -- this is because it is hard
2242 * to predict the length of a GETATTR response in v4, and thus
2243 * align the READ data correctly. This means that the GETATTR
2244 * may end up partially falling into the page cache, and we should
2245 * shift it into the 'tail' of the xdr_buf before processing.
2246 * To do this efficiently, we need to know the total length
2247 * of data received, which doesn't seem to be available outside
2250 * In the case of WRITE, we also want to put the GETATTR after
2251 * the operation -- in this case because we want to make sure
2252 * we get the post-operation mtime and size. This means that
2253 * we can't use xdr_encode_pages() as written: we need a variant
2254 * of it which would leave room in the 'tail' iovec.
2256 * Both of these changes to the XDR layer would in fact be quite
2257 * minor, but I decided to leave them for a subsequent patch.
2259 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2260 unsigned int pgbase
, unsigned int pglen
)
2262 struct nfs4_readlink args
= {
2263 .fh
= NFS_FH(inode
),
2268 struct nfs4_readlink_res res
;
2269 struct rpc_message msg
= {
2270 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2275 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2278 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2279 unsigned int pgbase
, unsigned int pglen
)
2281 struct nfs4_exception exception
= { };
2284 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2285 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2287 } while (exception
.retry
);
2293 * We will need to arrange for the VFS layer to provide an atomic open.
2294 * Until then, this create/open method is prone to inefficiency and race
2295 * conditions due to the lookup, create, and open VFS calls from sys_open()
2296 * placed on the wire.
2298 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2299 * The file will be opened again in the subsequent VFS open call
2300 * (nfs4_proc_file_open).
2302 * The open for read will just hang around to be used by any process that
2303 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2307 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2308 int flags
, struct nameidata
*nd
)
2310 struct path path
= {
2311 .mnt
= nd
->path
.mnt
,
2314 struct nfs4_state
*state
;
2315 struct rpc_cred
*cred
;
2316 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2319 cred
= rpc_lookup_cred();
2321 status
= PTR_ERR(cred
);
2324 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2326 if (IS_ERR(state
)) {
2327 status
= PTR_ERR(state
);
2330 d_add(dentry
, igrab(state
->inode
));
2331 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2332 if (flags
& O_EXCL
) {
2333 struct nfs_fattr fattr
;
2334 status
= nfs4_do_setattr(state
->inode
, cred
, &fattr
, sattr
, state
);
2336 nfs_setattr_update_inode(state
->inode
, sattr
);
2337 nfs_post_op_update_inode(state
->inode
, &fattr
);
2339 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2340 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2342 nfs4_close_sync(&path
, state
, fmode
);
2349 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2351 struct nfs_server
*server
= NFS_SERVER(dir
);
2352 struct nfs_removeargs args
= {
2354 .name
.len
= name
->len
,
2355 .name
.name
= name
->name
,
2356 .bitmask
= server
->attr_bitmask
,
2358 struct nfs_removeres res
= {
2361 struct rpc_message msg
= {
2362 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2368 nfs_fattr_init(&res
.dir_attr
);
2369 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2371 update_changeattr(dir
, &res
.cinfo
);
2372 nfs_post_op_update_inode(dir
, &res
.dir_attr
);
2377 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2379 struct nfs4_exception exception
= { };
2382 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2383 _nfs4_proc_remove(dir
, name
),
2385 } while (exception
.retry
);
2389 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2391 struct nfs_server
*server
= NFS_SERVER(dir
);
2392 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2393 struct nfs_removeres
*res
= msg
->rpc_resp
;
2395 args
->bitmask
= server
->cache_consistency_bitmask
;
2396 res
->server
= server
;
2397 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2400 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2402 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2404 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2406 update_changeattr(dir
, &res
->cinfo
);
2407 nfs_post_op_update_inode(dir
, &res
->dir_attr
);
2411 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2412 struct inode
*new_dir
, struct qstr
*new_name
)
2414 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2415 struct nfs4_rename_arg arg
= {
2416 .old_dir
= NFS_FH(old_dir
),
2417 .new_dir
= NFS_FH(new_dir
),
2418 .old_name
= old_name
,
2419 .new_name
= new_name
,
2420 .bitmask
= server
->attr_bitmask
,
2422 struct nfs_fattr old_fattr
, new_fattr
;
2423 struct nfs4_rename_res res
= {
2425 .old_fattr
= &old_fattr
,
2426 .new_fattr
= &new_fattr
,
2428 struct rpc_message msg
= {
2429 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2435 nfs_fattr_init(res
.old_fattr
);
2436 nfs_fattr_init(res
.new_fattr
);
2437 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2440 update_changeattr(old_dir
, &res
.old_cinfo
);
2441 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2442 update_changeattr(new_dir
, &res
.new_cinfo
);
2443 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2448 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2449 struct inode
*new_dir
, struct qstr
*new_name
)
2451 struct nfs4_exception exception
= { };
2454 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2455 _nfs4_proc_rename(old_dir
, old_name
,
2458 } while (exception
.retry
);
2462 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2464 struct nfs_server
*server
= NFS_SERVER(inode
);
2465 struct nfs4_link_arg arg
= {
2466 .fh
= NFS_FH(inode
),
2467 .dir_fh
= NFS_FH(dir
),
2469 .bitmask
= server
->attr_bitmask
,
2471 struct nfs_fattr fattr
, dir_attr
;
2472 struct nfs4_link_res res
= {
2475 .dir_attr
= &dir_attr
,
2477 struct rpc_message msg
= {
2478 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2484 nfs_fattr_init(res
.fattr
);
2485 nfs_fattr_init(res
.dir_attr
);
2486 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2488 update_changeattr(dir
, &res
.cinfo
);
2489 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2490 nfs_post_op_update_inode(inode
, res
.fattr
);
2496 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2498 struct nfs4_exception exception
= { };
2501 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2502 _nfs4_proc_link(inode
, dir
, name
),
2504 } while (exception
.retry
);
2508 struct nfs4_createdata
{
2509 struct rpc_message msg
;
2510 struct nfs4_create_arg arg
;
2511 struct nfs4_create_res res
;
2513 struct nfs_fattr fattr
;
2514 struct nfs_fattr dir_fattr
;
2517 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2518 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2520 struct nfs4_createdata
*data
;
2522 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2524 struct nfs_server
*server
= NFS_SERVER(dir
);
2526 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2527 data
->msg
.rpc_argp
= &data
->arg
;
2528 data
->msg
.rpc_resp
= &data
->res
;
2529 data
->arg
.dir_fh
= NFS_FH(dir
);
2530 data
->arg
.server
= server
;
2531 data
->arg
.name
= name
;
2532 data
->arg
.attrs
= sattr
;
2533 data
->arg
.ftype
= ftype
;
2534 data
->arg
.bitmask
= server
->attr_bitmask
;
2535 data
->res
.server
= server
;
2536 data
->res
.fh
= &data
->fh
;
2537 data
->res
.fattr
= &data
->fattr
;
2538 data
->res
.dir_fattr
= &data
->dir_fattr
;
2539 nfs_fattr_init(data
->res
.fattr
);
2540 nfs_fattr_init(data
->res
.dir_fattr
);
2545 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2547 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2548 &data
->arg
, &data
->res
, 1);
2550 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2551 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2552 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2557 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2562 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2563 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2565 struct nfs4_createdata
*data
;
2566 int status
= -ENAMETOOLONG
;
2568 if (len
> NFS4_MAXPATHLEN
)
2572 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2576 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2577 data
->arg
.u
.symlink
.pages
= &page
;
2578 data
->arg
.u
.symlink
.len
= len
;
2580 status
= nfs4_do_create(dir
, dentry
, data
);
2582 nfs4_free_createdata(data
);
2587 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2588 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2590 struct nfs4_exception exception
= { };
2593 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2594 _nfs4_proc_symlink(dir
, dentry
, page
,
2597 } while (exception
.retry
);
2601 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2602 struct iattr
*sattr
)
2604 struct nfs4_createdata
*data
;
2605 int status
= -ENOMEM
;
2607 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2611 status
= nfs4_do_create(dir
, dentry
, data
);
2613 nfs4_free_createdata(data
);
2618 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2619 struct iattr
*sattr
)
2621 struct nfs4_exception exception
= { };
2624 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2625 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2627 } while (exception
.retry
);
2631 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2632 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2634 struct inode
*dir
= dentry
->d_inode
;
2635 struct nfs4_readdir_arg args
= {
2640 .bitmask
= NFS_SERVER(dentry
->d_inode
)->cache_consistency_bitmask
,
2642 struct nfs4_readdir_res res
;
2643 struct rpc_message msg
= {
2644 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2651 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2652 dentry
->d_parent
->d_name
.name
,
2653 dentry
->d_name
.name
,
2654 (unsigned long long)cookie
);
2655 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2656 res
.pgbase
= args
.pgbase
;
2657 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2659 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2661 nfs_invalidate_atime(dir
);
2663 dprintk("%s: returns %d\n", __func__
, status
);
2667 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2668 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2670 struct nfs4_exception exception
= { };
2673 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2674 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2677 } while (exception
.retry
);
2681 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2682 struct iattr
*sattr
, dev_t rdev
)
2684 struct nfs4_createdata
*data
;
2685 int mode
= sattr
->ia_mode
;
2686 int status
= -ENOMEM
;
2688 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2689 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2691 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2696 data
->arg
.ftype
= NF4FIFO
;
2697 else if (S_ISBLK(mode
)) {
2698 data
->arg
.ftype
= NF4BLK
;
2699 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2700 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2702 else if (S_ISCHR(mode
)) {
2703 data
->arg
.ftype
= NF4CHR
;
2704 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2705 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2708 status
= nfs4_do_create(dir
, dentry
, data
);
2710 nfs4_free_createdata(data
);
2715 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2716 struct iattr
*sattr
, dev_t rdev
)
2718 struct nfs4_exception exception
= { };
2721 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2722 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2724 } while (exception
.retry
);
2728 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2729 struct nfs_fsstat
*fsstat
)
2731 struct nfs4_statfs_arg args
= {
2733 .bitmask
= server
->attr_bitmask
,
2735 struct nfs4_statfs_res res
= {
2738 struct rpc_message msg
= {
2739 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2744 nfs_fattr_init(fsstat
->fattr
);
2745 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2748 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2750 struct nfs4_exception exception
= { };
2753 err
= nfs4_handle_exception(server
,
2754 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2756 } while (exception
.retry
);
2760 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2761 struct nfs_fsinfo
*fsinfo
)
2763 struct nfs4_fsinfo_arg args
= {
2765 .bitmask
= server
->attr_bitmask
,
2767 struct nfs4_fsinfo_res res
= {
2770 struct rpc_message msg
= {
2771 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
2776 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2779 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2781 struct nfs4_exception exception
= { };
2785 err
= nfs4_handle_exception(server
,
2786 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
2788 } while (exception
.retry
);
2792 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2794 nfs_fattr_init(fsinfo
->fattr
);
2795 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
2798 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2799 struct nfs_pathconf
*pathconf
)
2801 struct nfs4_pathconf_arg args
= {
2803 .bitmask
= server
->attr_bitmask
,
2805 struct nfs4_pathconf_res res
= {
2806 .pathconf
= pathconf
,
2808 struct rpc_message msg
= {
2809 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
2814 /* None of the pathconf attributes are mandatory to implement */
2815 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
2816 memset(pathconf
, 0, sizeof(*pathconf
));
2820 nfs_fattr_init(pathconf
->fattr
);
2821 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2824 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2825 struct nfs_pathconf
*pathconf
)
2827 struct nfs4_exception exception
= { };
2831 err
= nfs4_handle_exception(server
,
2832 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
2834 } while (exception
.retry
);
2838 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
2840 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2842 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
2843 rpc_restart_call(task
);
2847 nfs_invalidate_atime(data
->inode
);
2848 if (task
->tk_status
> 0)
2849 renew_lease(server
, data
->timestamp
);
2853 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
2855 data
->timestamp
= jiffies
;
2856 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
2859 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
2861 struct inode
*inode
= data
->inode
;
2863 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
2864 rpc_restart_call(task
);
2867 if (task
->tk_status
>= 0) {
2868 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
2869 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
2874 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
2876 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2878 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
2879 data
->res
.server
= server
;
2880 data
->timestamp
= jiffies
;
2882 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
2885 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
2887 struct inode
*inode
= data
->inode
;
2889 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
2890 rpc_restart_call(task
);
2893 nfs_refresh_inode(inode
, data
->res
.fattr
);
2897 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
2899 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2901 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
2902 data
->res
.server
= server
;
2903 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
2907 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2908 * standalone procedure for queueing an asynchronous RENEW.
2910 static void nfs4_renew_done(struct rpc_task
*task
, void *data
)
2912 struct nfs_client
*clp
= (struct nfs_client
*)task
->tk_msg
.rpc_argp
;
2913 unsigned long timestamp
= (unsigned long)data
;
2915 if (task
->tk_status
< 0) {
2916 /* Unless we're shutting down, schedule state recovery! */
2917 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
2918 nfs4_schedule_state_recovery(clp
);
2921 spin_lock(&clp
->cl_lock
);
2922 if (time_before(clp
->cl_last_renewal
,timestamp
))
2923 clp
->cl_last_renewal
= timestamp
;
2924 spin_unlock(&clp
->cl_lock
);
2927 static const struct rpc_call_ops nfs4_renew_ops
= {
2928 .rpc_call_done
= nfs4_renew_done
,
2931 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
2933 struct rpc_message msg
= {
2934 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2939 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
2940 &nfs4_renew_ops
, (void *)jiffies
);
2943 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
2945 struct rpc_message msg
= {
2946 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2950 unsigned long now
= jiffies
;
2953 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2956 spin_lock(&clp
->cl_lock
);
2957 if (time_before(clp
->cl_last_renewal
,now
))
2958 clp
->cl_last_renewal
= now
;
2959 spin_unlock(&clp
->cl_lock
);
2963 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
2965 return (server
->caps
& NFS_CAP_ACLS
)
2966 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
2967 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
2970 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2971 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2974 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2976 static void buf_to_pages(const void *buf
, size_t buflen
,
2977 struct page
**pages
, unsigned int *pgbase
)
2979 const void *p
= buf
;
2981 *pgbase
= offset_in_page(buf
);
2983 while (p
< buf
+ buflen
) {
2984 *(pages
++) = virt_to_page(p
);
2985 p
+= PAGE_CACHE_SIZE
;
2989 struct nfs4_cached_acl
{
2995 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
2997 struct nfs_inode
*nfsi
= NFS_I(inode
);
2999 spin_lock(&inode
->i_lock
);
3000 kfree(nfsi
->nfs4_acl
);
3001 nfsi
->nfs4_acl
= acl
;
3002 spin_unlock(&inode
->i_lock
);
3005 static void nfs4_zap_acl_attr(struct inode
*inode
)
3007 nfs4_set_cached_acl(inode
, NULL
);
3010 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3012 struct nfs_inode
*nfsi
= NFS_I(inode
);
3013 struct nfs4_cached_acl
*acl
;
3016 spin_lock(&inode
->i_lock
);
3017 acl
= nfsi
->nfs4_acl
;
3020 if (buf
== NULL
) /* user is just asking for length */
3022 if (acl
->cached
== 0)
3024 ret
= -ERANGE
; /* see getxattr(2) man page */
3025 if (acl
->len
> buflen
)
3027 memcpy(buf
, acl
->data
, acl
->len
);
3031 spin_unlock(&inode
->i_lock
);
3035 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3037 struct nfs4_cached_acl
*acl
;
3039 if (buf
&& acl_len
<= PAGE_SIZE
) {
3040 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3044 memcpy(acl
->data
, buf
, acl_len
);
3046 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3053 nfs4_set_cached_acl(inode
, acl
);
3056 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3058 struct page
*pages
[NFS4ACL_MAXPAGES
];
3059 struct nfs_getaclargs args
= {
3060 .fh
= NFS_FH(inode
),
3064 struct nfs_getaclres res
= {
3068 struct rpc_message msg
= {
3069 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3073 struct page
*localpage
= NULL
;
3076 if (buflen
< PAGE_SIZE
) {
3077 /* As long as we're doing a round trip to the server anyway,
3078 * let's be prepared for a page of acl data. */
3079 localpage
= alloc_page(GFP_KERNEL
);
3080 resp_buf
= page_address(localpage
);
3081 if (localpage
== NULL
)
3083 args
.acl_pages
[0] = localpage
;
3084 args
.acl_pgbase
= 0;
3085 args
.acl_len
= PAGE_SIZE
;
3088 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3090 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3093 if (res
.acl_len
> args
.acl_len
)
3094 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3096 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3099 if (res
.acl_len
> buflen
)
3102 memcpy(buf
, resp_buf
, res
.acl_len
);
3107 __free_page(localpage
);
3111 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3113 struct nfs4_exception exception
= { };
3116 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3119 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3120 } while (exception
.retry
);
3124 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3126 struct nfs_server
*server
= NFS_SERVER(inode
);
3129 if (!nfs4_server_supports_acls(server
))
3131 ret
= nfs_revalidate_inode(server
, inode
);
3134 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
3135 nfs_zap_acl_cache(inode
);
3136 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3139 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3142 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3144 struct nfs_server
*server
= NFS_SERVER(inode
);
3145 struct page
*pages
[NFS4ACL_MAXPAGES
];
3146 struct nfs_setaclargs arg
= {
3147 .fh
= NFS_FH(inode
),
3151 struct nfs_setaclres res
;
3152 struct rpc_message msg
= {
3153 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3159 if (!nfs4_server_supports_acls(server
))
3161 nfs_inode_return_delegation(inode
);
3162 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3163 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3164 nfs_access_zap_cache(inode
);
3165 nfs_zap_acl_cache(inode
);
3169 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3171 struct nfs4_exception exception
= { };
3174 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3175 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3177 } while (exception
.retry
);
3182 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3184 struct nfs_client
*clp
= server
->nfs_client
;
3186 if (!clp
|| task
->tk_status
>= 0)
3188 switch(task
->tk_status
) {
3189 case -NFS4ERR_ADMIN_REVOKED
:
3190 case -NFS4ERR_BAD_STATEID
:
3191 case -NFS4ERR_OPENMODE
:
3194 nfs4_state_mark_reclaim_nograce(clp
, state
);
3195 case -NFS4ERR_STALE_CLIENTID
:
3196 case -NFS4ERR_STALE_STATEID
:
3197 case -NFS4ERR_EXPIRED
:
3198 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3199 nfs4_schedule_state_recovery(clp
);
3200 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3201 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3202 task
->tk_status
= 0;
3204 case -NFS4ERR_DELAY
:
3205 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3206 case -NFS4ERR_GRACE
:
3207 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3208 task
->tk_status
= 0;
3210 case -NFS4ERR_OLD_STATEID
:
3211 task
->tk_status
= 0;
3214 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3218 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
, unsigned short port
, struct rpc_cred
*cred
)
3220 nfs4_verifier sc_verifier
;
3221 struct nfs4_setclientid setclientid
= {
3222 .sc_verifier
= &sc_verifier
,
3225 struct rpc_message msg
= {
3226 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3227 .rpc_argp
= &setclientid
,
3235 p
= (__be32
*)sc_verifier
.data
;
3236 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3237 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3240 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3241 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3243 rpc_peeraddr2str(clp
->cl_rpcclient
,
3245 rpc_peeraddr2str(clp
->cl_rpcclient
,
3247 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3248 clp
->cl_id_uniquifier
);
3249 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3250 sizeof(setclientid
.sc_netid
),
3251 rpc_peeraddr2str(clp
->cl_rpcclient
,
3252 RPC_DISPLAY_NETID
));
3253 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3254 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3255 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3257 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3258 if (status
!= -NFS4ERR_CLID_INUSE
)
3263 ssleep(clp
->cl_lease_time
+ 1);
3265 if (++clp
->cl_id_uniquifier
== 0)
3271 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3273 struct nfs_fsinfo fsinfo
;
3274 struct rpc_message msg
= {
3275 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3277 .rpc_resp
= &fsinfo
,
3284 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3286 spin_lock(&clp
->cl_lock
);
3287 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3288 clp
->cl_last_renewal
= now
;
3289 spin_unlock(&clp
->cl_lock
);
3294 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3299 err
= _nfs4_proc_setclientid_confirm(clp
, cred
);
3303 case -NFS4ERR_RESOURCE
:
3304 /* The IBM lawyers misread another document! */
3305 case -NFS4ERR_DELAY
:
3306 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3312 struct nfs4_delegreturndata
{
3313 struct nfs4_delegreturnargs args
;
3314 struct nfs4_delegreturnres res
;
3316 nfs4_stateid stateid
;
3317 unsigned long timestamp
;
3318 struct nfs_fattr fattr
;
3322 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3324 struct nfs4_delegreturndata
*data
= calldata
;
3325 data
->rpc_status
= task
->tk_status
;
3326 if (data
->rpc_status
== 0)
3327 renew_lease(data
->res
.server
, data
->timestamp
);
3330 static void nfs4_delegreturn_release(void *calldata
)
3335 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3336 .rpc_call_done
= nfs4_delegreturn_done
,
3337 .rpc_release
= nfs4_delegreturn_release
,
3340 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3342 struct nfs4_delegreturndata
*data
;
3343 struct nfs_server
*server
= NFS_SERVER(inode
);
3344 struct rpc_task
*task
;
3345 struct rpc_message msg
= {
3346 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3349 struct rpc_task_setup task_setup_data
= {
3350 .rpc_client
= server
->client
,
3351 .rpc_message
= &msg
,
3352 .callback_ops
= &nfs4_delegreturn_ops
,
3353 .flags
= RPC_TASK_ASYNC
,
3357 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
3360 data
->args
.fhandle
= &data
->fh
;
3361 data
->args
.stateid
= &data
->stateid
;
3362 data
->args
.bitmask
= server
->attr_bitmask
;
3363 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3364 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3365 data
->res
.fattr
= &data
->fattr
;
3366 data
->res
.server
= server
;
3367 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3368 nfs_fattr_init(data
->res
.fattr
);
3369 data
->timestamp
= jiffies
;
3370 data
->rpc_status
= 0;
3372 task_setup_data
.callback_data
= data
;
3373 msg
.rpc_argp
= &data
->args
,
3374 msg
.rpc_resp
= &data
->res
,
3375 task
= rpc_run_task(&task_setup_data
);
3377 return PTR_ERR(task
);
3380 status
= nfs4_wait_for_completion_rpc_task(task
);
3383 status
= data
->rpc_status
;
3386 nfs_refresh_inode(inode
, &data
->fattr
);
3392 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3394 struct nfs_server
*server
= NFS_SERVER(inode
);
3395 struct nfs4_exception exception
= { };
3398 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3400 case -NFS4ERR_STALE_STATEID
:
3401 case -NFS4ERR_EXPIRED
:
3405 err
= nfs4_handle_exception(server
, err
, &exception
);
3406 } while (exception
.retry
);
3410 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3411 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3414 * sleep, with exponential backoff, and retry the LOCK operation.
3416 static unsigned long
3417 nfs4_set_lock_task_retry(unsigned long timeout
)
3419 schedule_timeout_killable(timeout
);
3421 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3422 return NFS4_LOCK_MAXTIMEOUT
;
3426 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3428 struct inode
*inode
= state
->inode
;
3429 struct nfs_server
*server
= NFS_SERVER(inode
);
3430 struct nfs_client
*clp
= server
->nfs_client
;
3431 struct nfs_lockt_args arg
= {
3432 .fh
= NFS_FH(inode
),
3435 struct nfs_lockt_res res
= {
3438 struct rpc_message msg
= {
3439 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3442 .rpc_cred
= state
->owner
->so_cred
,
3444 struct nfs4_lock_state
*lsp
;
3447 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3448 status
= nfs4_set_lock_state(state
, request
);
3451 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3452 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3453 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3456 request
->fl_type
= F_UNLCK
;
3458 case -NFS4ERR_DENIED
:
3461 request
->fl_ops
->fl_release_private(request
);
3466 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3468 struct nfs4_exception exception
= { };
3472 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3473 _nfs4_proc_getlk(state
, cmd
, request
),
3475 } while (exception
.retry
);
3479 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3482 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3484 res
= posix_lock_file_wait(file
, fl
);
3487 res
= flock_lock_file_wait(file
, fl
);
3495 struct nfs4_unlockdata
{
3496 struct nfs_locku_args arg
;
3497 struct nfs_locku_res res
;
3498 struct nfs4_lock_state
*lsp
;
3499 struct nfs_open_context
*ctx
;
3500 struct file_lock fl
;
3501 const struct nfs_server
*server
;
3502 unsigned long timestamp
;
3505 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3506 struct nfs_open_context
*ctx
,
3507 struct nfs4_lock_state
*lsp
,
3508 struct nfs_seqid
*seqid
)
3510 struct nfs4_unlockdata
*p
;
3511 struct inode
*inode
= lsp
->ls_state
->inode
;
3513 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
3516 p
->arg
.fh
= NFS_FH(inode
);
3518 p
->arg
.seqid
= seqid
;
3519 p
->res
.seqid
= seqid
;
3520 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3521 p
->arg
.stateid
= &lsp
->ls_stateid
;
3523 atomic_inc(&lsp
->ls_count
);
3524 /* Ensure we don't close file until we're done freeing locks! */
3525 p
->ctx
= get_nfs_open_context(ctx
);
3526 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3527 p
->server
= NFS_SERVER(inode
);
3531 static void nfs4_locku_release_calldata(void *data
)
3533 struct nfs4_unlockdata
*calldata
= data
;
3534 nfs_free_seqid(calldata
->arg
.seqid
);
3535 nfs4_put_lock_state(calldata
->lsp
);
3536 put_nfs_open_context(calldata
->ctx
);
3540 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3542 struct nfs4_unlockdata
*calldata
= data
;
3544 if (RPC_ASSASSINATED(task
))
3546 switch (task
->tk_status
) {
3548 memcpy(calldata
->lsp
->ls_stateid
.data
,
3549 calldata
->res
.stateid
.data
,
3550 sizeof(calldata
->lsp
->ls_stateid
.data
));
3551 renew_lease(calldata
->server
, calldata
->timestamp
);
3553 case -NFS4ERR_BAD_STATEID
:
3554 case -NFS4ERR_OLD_STATEID
:
3555 case -NFS4ERR_STALE_STATEID
:
3556 case -NFS4ERR_EXPIRED
:
3559 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3560 rpc_restart_call(task
);
3564 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3566 struct nfs4_unlockdata
*calldata
= data
;
3568 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3570 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3571 /* Note: exit _without_ running nfs4_locku_done */
3572 task
->tk_action
= NULL
;
3575 calldata
->timestamp
= jiffies
;
3576 rpc_call_start(task
);
3579 static const struct rpc_call_ops nfs4_locku_ops
= {
3580 .rpc_call_prepare
= nfs4_locku_prepare
,
3581 .rpc_call_done
= nfs4_locku_done
,
3582 .rpc_release
= nfs4_locku_release_calldata
,
3585 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3586 struct nfs_open_context
*ctx
,
3587 struct nfs4_lock_state
*lsp
,
3588 struct nfs_seqid
*seqid
)
3590 struct nfs4_unlockdata
*data
;
3591 struct rpc_message msg
= {
3592 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3593 .rpc_cred
= ctx
->cred
,
3595 struct rpc_task_setup task_setup_data
= {
3596 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3597 .rpc_message
= &msg
,
3598 .callback_ops
= &nfs4_locku_ops
,
3599 .workqueue
= nfsiod_workqueue
,
3600 .flags
= RPC_TASK_ASYNC
,
3603 /* Ensure this is an unlock - when canceling a lock, the
3604 * canceled lock is passed in, and it won't be an unlock.
3606 fl
->fl_type
= F_UNLCK
;
3608 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3610 nfs_free_seqid(seqid
);
3611 return ERR_PTR(-ENOMEM
);
3614 msg
.rpc_argp
= &data
->arg
,
3615 msg
.rpc_resp
= &data
->res
,
3616 task_setup_data
.callback_data
= data
;
3617 return rpc_run_task(&task_setup_data
);
3620 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3622 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3623 struct nfs_seqid
*seqid
;
3624 struct nfs4_lock_state
*lsp
;
3625 struct rpc_task
*task
;
3627 unsigned char fl_flags
= request
->fl_flags
;
3629 status
= nfs4_set_lock_state(state
, request
);
3630 /* Unlock _before_ we do the RPC call */
3631 request
->fl_flags
|= FL_EXISTS
;
3632 down_read(&nfsi
->rwsem
);
3633 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3634 up_read(&nfsi
->rwsem
);
3637 up_read(&nfsi
->rwsem
);
3640 /* Is this a delegated lock? */
3641 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3643 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3644 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3648 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3649 status
= PTR_ERR(task
);
3652 status
= nfs4_wait_for_completion_rpc_task(task
);
3655 request
->fl_flags
= fl_flags
;
3659 struct nfs4_lockdata
{
3660 struct nfs_lock_args arg
;
3661 struct nfs_lock_res res
;
3662 struct nfs4_lock_state
*lsp
;
3663 struct nfs_open_context
*ctx
;
3664 struct file_lock fl
;
3665 unsigned long timestamp
;
3670 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3671 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
)
3673 struct nfs4_lockdata
*p
;
3674 struct inode
*inode
= lsp
->ls_state
->inode
;
3675 struct nfs_server
*server
= NFS_SERVER(inode
);
3677 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3681 p
->arg
.fh
= NFS_FH(inode
);
3683 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
);
3684 if (p
->arg
.open_seqid
== NULL
)
3686 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3687 if (p
->arg
.lock_seqid
== NULL
)
3688 goto out_free_seqid
;
3689 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
3690 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
3691 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3692 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
3693 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3695 atomic_inc(&lsp
->ls_count
);
3696 p
->ctx
= get_nfs_open_context(ctx
);
3697 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3700 nfs_free_seqid(p
->arg
.open_seqid
);
3706 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
3708 struct nfs4_lockdata
*data
= calldata
;
3709 struct nfs4_state
*state
= data
->lsp
->ls_state
;
3711 dprintk("%s: begin!\n", __func__
);
3712 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
3714 /* Do we need to do an open_to_lock_owner? */
3715 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
3716 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
3718 data
->arg
.open_stateid
= &state
->stateid
;
3719 data
->arg
.new_lock_owner
= 1;
3720 data
->res
.open_seqid
= data
->arg
.open_seqid
;
3722 data
->arg
.new_lock_owner
= 0;
3723 data
->timestamp
= jiffies
;
3724 rpc_call_start(task
);
3725 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
3728 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
3730 struct nfs4_lockdata
*data
= calldata
;
3732 dprintk("%s: begin!\n", __func__
);
3734 data
->rpc_status
= task
->tk_status
;
3735 if (RPC_ASSASSINATED(task
))
3737 if (data
->arg
.new_lock_owner
!= 0) {
3738 if (data
->rpc_status
== 0)
3739 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
3743 if (data
->rpc_status
== 0) {
3744 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
3745 sizeof(data
->lsp
->ls_stateid
.data
));
3746 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
3747 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
3750 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
3753 static void nfs4_lock_release(void *calldata
)
3755 struct nfs4_lockdata
*data
= calldata
;
3757 dprintk("%s: begin!\n", __func__
);
3758 nfs_free_seqid(data
->arg
.open_seqid
);
3759 if (data
->cancelled
!= 0) {
3760 struct rpc_task
*task
;
3761 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
3762 data
->arg
.lock_seqid
);
3765 dprintk("%s: cancelling lock!\n", __func__
);
3767 nfs_free_seqid(data
->arg
.lock_seqid
);
3768 nfs4_put_lock_state(data
->lsp
);
3769 put_nfs_open_context(data
->ctx
);
3771 dprintk("%s: done!\n", __func__
);
3774 static const struct rpc_call_ops nfs4_lock_ops
= {
3775 .rpc_call_prepare
= nfs4_lock_prepare
,
3776 .rpc_call_done
= nfs4_lock_done
,
3777 .rpc_release
= nfs4_lock_release
,
3780 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int reclaim
)
3782 struct nfs4_lockdata
*data
;
3783 struct rpc_task
*task
;
3784 struct rpc_message msg
= {
3785 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
3786 .rpc_cred
= state
->owner
->so_cred
,
3788 struct rpc_task_setup task_setup_data
= {
3789 .rpc_client
= NFS_CLIENT(state
->inode
),
3790 .rpc_message
= &msg
,
3791 .callback_ops
= &nfs4_lock_ops
,
3792 .workqueue
= nfsiod_workqueue
,
3793 .flags
= RPC_TASK_ASYNC
,
3797 dprintk("%s: begin!\n", __func__
);
3798 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
3799 fl
->fl_u
.nfs4_fl
.owner
);
3803 data
->arg
.block
= 1;
3805 data
->arg
.reclaim
= 1;
3806 msg
.rpc_argp
= &data
->arg
,
3807 msg
.rpc_resp
= &data
->res
,
3808 task_setup_data
.callback_data
= data
;
3809 task
= rpc_run_task(&task_setup_data
);
3811 return PTR_ERR(task
);
3812 ret
= nfs4_wait_for_completion_rpc_task(task
);
3814 ret
= data
->rpc_status
;
3815 if (ret
== -NFS4ERR_DENIED
)
3818 data
->cancelled
= 1;
3820 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
3824 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
3826 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3827 struct nfs4_exception exception
= { };
3831 /* Cache the lock if possible... */
3832 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
3834 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 1);
3835 if (err
!= -NFS4ERR_DELAY
)
3837 nfs4_handle_exception(server
, err
, &exception
);
3838 } while (exception
.retry
);
3842 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
3844 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3845 struct nfs4_exception exception
= { };
3848 err
= nfs4_set_lock_state(state
, request
);
3852 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
3854 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 0);
3855 if (err
!= -NFS4ERR_DELAY
)
3857 nfs4_handle_exception(server
, err
, &exception
);
3858 } while (exception
.retry
);
3862 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3864 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3865 unsigned char fl_flags
= request
->fl_flags
;
3868 /* Is this a delegated open? */
3869 status
= nfs4_set_lock_state(state
, request
);
3872 request
->fl_flags
|= FL_ACCESS
;
3873 status
= do_vfs_lock(request
->fl_file
, request
);
3876 down_read(&nfsi
->rwsem
);
3877 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
3878 /* Yes: cache locks! */
3879 /* ...but avoid races with delegation recall... */
3880 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
3881 status
= do_vfs_lock(request
->fl_file
, request
);
3884 status
= _nfs4_do_setlk(state
, cmd
, request
, 0);
3887 /* Note: we always want to sleep here! */
3888 request
->fl_flags
= fl_flags
| FL_SLEEP
;
3889 if (do_vfs_lock(request
->fl_file
, request
) < 0)
3890 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
3892 up_read(&nfsi
->rwsem
);
3894 request
->fl_flags
= fl_flags
;
3898 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3900 struct nfs4_exception exception
= { };
3904 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3905 _nfs4_proc_setlk(state
, cmd
, request
),
3907 } while (exception
.retry
);
3912 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
3914 struct nfs_open_context
*ctx
;
3915 struct nfs4_state
*state
;
3916 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
3919 /* verify open state */
3920 ctx
= nfs_file_open_context(filp
);
3923 if (request
->fl_start
< 0 || request
->fl_end
< 0)
3927 return nfs4_proc_getlk(state
, F_GETLK
, request
);
3929 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
3932 if (request
->fl_type
== F_UNLCK
)
3933 return nfs4_proc_unlck(state
, cmd
, request
);
3936 status
= nfs4_proc_setlk(state
, cmd
, request
);
3937 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
3939 timeout
= nfs4_set_lock_task_retry(timeout
);
3940 status
= -ERESTARTSYS
;
3943 } while(status
< 0);
3947 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
3949 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3950 struct nfs4_exception exception
= { };
3953 err
= nfs4_set_lock_state(state
, fl
);
3957 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, 0);
3958 if (err
!= -NFS4ERR_DELAY
)
3960 err
= nfs4_handle_exception(server
, err
, &exception
);
3961 } while (exception
.retry
);
3966 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3968 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
3969 size_t buflen
, int flags
)
3971 struct inode
*inode
= dentry
->d_inode
;
3973 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3976 return nfs4_proc_set_acl(inode
, buf
, buflen
);
3979 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3980 * and that's what we'll do for e.g. user attributes that haven't been set.
3981 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3982 * attributes in kernel-managed attribute namespaces. */
3983 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
3986 struct inode
*inode
= dentry
->d_inode
;
3988 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3991 return nfs4_proc_get_acl(inode
, buf
, buflen
);
3994 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
3996 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
3998 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4000 if (buf
&& buflen
< len
)
4003 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4007 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4009 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4010 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4011 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4014 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4015 NFS_ATTR_FATTR_NLINK
;
4016 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4020 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4021 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4023 struct nfs_server
*server
= NFS_SERVER(dir
);
4025 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4026 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4028 struct nfs4_fs_locations_arg args
= {
4029 .dir_fh
= NFS_FH(dir
),
4034 struct nfs4_fs_locations_res res
= {
4035 .fs_locations
= fs_locations
,
4037 struct rpc_message msg
= {
4038 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4044 dprintk("%s: start\n", __func__
);
4045 nfs_fattr_init(&fs_locations
->fattr
);
4046 fs_locations
->server
= server
;
4047 fs_locations
->nlocations
= 0;
4048 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4049 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4050 dprintk("%s: returned status = %d\n", __func__
, status
);
4054 #ifdef CONFIG_NFS_V4_1
4055 /* Destroy the slot table */
4056 static void nfs4_destroy_slot_table(struct nfs4_session
*session
)
4058 if (session
->fc_slot_table
.slots
== NULL
)
4060 kfree(session
->fc_slot_table
.slots
);
4061 session
->fc_slot_table
.slots
= NULL
;
4065 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4067 struct nfs4_session
*session
;
4068 struct nfs4_slot_table
*tbl
;
4070 session
= kzalloc(sizeof(struct nfs4_session
), GFP_KERNEL
);
4073 tbl
= &session
->fc_slot_table
;
4074 spin_lock_init(&tbl
->slot_tbl_lock
);
4075 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "Slot table");
4080 void nfs4_destroy_session(struct nfs4_session
*session
)
4082 nfs4_destroy_slot_table(session
);
4086 #endif /* CONFIG_NFS_V4_1 */
4088 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops
= {
4089 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
4090 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
4091 .recover_open
= nfs4_open_reclaim
,
4092 .recover_lock
= nfs4_lock_reclaim
,
4095 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops
= {
4096 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
4097 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
4098 .recover_open
= nfs4_open_expired
,
4099 .recover_lock
= nfs4_lock_expired
,
4102 static const struct inode_operations nfs4_file_inode_operations
= {
4103 .permission
= nfs_permission
,
4104 .getattr
= nfs_getattr
,
4105 .setattr
= nfs_setattr
,
4106 .getxattr
= nfs4_getxattr
,
4107 .setxattr
= nfs4_setxattr
,
4108 .listxattr
= nfs4_listxattr
,
4111 const struct nfs_rpc_ops nfs_v4_clientops
= {
4112 .version
= 4, /* protocol version */
4113 .dentry_ops
= &nfs4_dentry_operations
,
4114 .dir_inode_ops
= &nfs4_dir_inode_operations
,
4115 .file_inode_ops
= &nfs4_file_inode_operations
,
4116 .getroot
= nfs4_proc_get_root
,
4117 .getattr
= nfs4_proc_getattr
,
4118 .setattr
= nfs4_proc_setattr
,
4119 .lookupfh
= nfs4_proc_lookupfh
,
4120 .lookup
= nfs4_proc_lookup
,
4121 .access
= nfs4_proc_access
,
4122 .readlink
= nfs4_proc_readlink
,
4123 .create
= nfs4_proc_create
,
4124 .remove
= nfs4_proc_remove
,
4125 .unlink_setup
= nfs4_proc_unlink_setup
,
4126 .unlink_done
= nfs4_proc_unlink_done
,
4127 .rename
= nfs4_proc_rename
,
4128 .link
= nfs4_proc_link
,
4129 .symlink
= nfs4_proc_symlink
,
4130 .mkdir
= nfs4_proc_mkdir
,
4131 .rmdir
= nfs4_proc_remove
,
4132 .readdir
= nfs4_proc_readdir
,
4133 .mknod
= nfs4_proc_mknod
,
4134 .statfs
= nfs4_proc_statfs
,
4135 .fsinfo
= nfs4_proc_fsinfo
,
4136 .pathconf
= nfs4_proc_pathconf
,
4137 .set_capabilities
= nfs4_server_capabilities
,
4138 .decode_dirent
= nfs4_decode_dirent
,
4139 .read_setup
= nfs4_proc_read_setup
,
4140 .read_done
= nfs4_read_done
,
4141 .write_setup
= nfs4_proc_write_setup
,
4142 .write_done
= nfs4_write_done
,
4143 .commit_setup
= nfs4_proc_commit_setup
,
4144 .commit_done
= nfs4_commit_done
,
4145 .lock
= nfs4_proc_lock
,
4146 .clear_acl_cache
= nfs4_zap_acl_attr
,
4147 .close_context
= nfs4_close_context
,