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"
55 #define NFSDBG_FACILITY NFSDBG_PROC
57 #define NFS4_POLL_RETRY_MIN (1*HZ)
58 #define NFS4_POLL_RETRY_MAX (15*HZ)
60 static int _nfs4_proc_open_confirm(struct rpc_clnt
*clnt
, const struct nfs_fh
*fh
, struct nfs4_state_owner
*sp
, nfs4_stateid
*stateid
, struct nfs_seqid
*seqid
);
61 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
62 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*);
63 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
);
64 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
);
65 extern u32
*nfs4_decode_dirent(u32
*p
, struct nfs_entry
*entry
, int plus
);
66 extern struct rpc_procinfo nfs4_procedures
[];
68 /* Prevent leaks of NFSv4 errors into userland */
69 int nfs4_map_errors(int err
)
72 dprintk("%s could not handle NFSv4 error %d\n",
80 * This is our standard bitmap for GETATTR requests.
82 const u32 nfs4_fattr_bitmap
[2] = {
87 | FATTR4_WORD0_FILEID
,
89 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_SPACE_USED
94 | FATTR4_WORD1_TIME_ACCESS
95 | FATTR4_WORD1_TIME_METADATA
96 | FATTR4_WORD1_TIME_MODIFY
99 const u32 nfs4_statfs_bitmap
[2] = {
100 FATTR4_WORD0_FILES_AVAIL
101 | FATTR4_WORD0_FILES_FREE
102 | FATTR4_WORD0_FILES_TOTAL
,
103 FATTR4_WORD1_SPACE_AVAIL
104 | FATTR4_WORD1_SPACE_FREE
105 | FATTR4_WORD1_SPACE_TOTAL
108 const u32 nfs4_pathconf_bitmap
[2] = {
110 | FATTR4_WORD0_MAXNAME
,
114 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
115 | FATTR4_WORD0_MAXREAD
116 | FATTR4_WORD0_MAXWRITE
117 | FATTR4_WORD0_LEASE_TIME
,
121 static void nfs4_setup_readdir(u64 cookie
, u32
*verifier
, struct dentry
*dentry
,
122 struct nfs4_readdir_arg
*readdir
)
126 BUG_ON(readdir
->count
< 80);
128 readdir
->cookie
= cookie
;
129 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
134 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
139 * NFSv4 servers do not return entries for '.' and '..'
140 * Therefore, we fake these entries here. We let '.'
141 * have cookie 0 and '..' have cookie 1. Note that
142 * when talking to the server, we always send cookie 0
145 start
= p
= (u32
*)kmap_atomic(*readdir
->pages
, KM_USER0
);
148 *p
++ = xdr_one
; /* next */
149 *p
++ = xdr_zero
; /* cookie, first word */
150 *p
++ = xdr_one
; /* cookie, second word */
151 *p
++ = xdr_one
; /* entry len */
152 memcpy(p
, ".\0\0\0", 4); /* entry */
154 *p
++ = xdr_one
; /* bitmap length */
155 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
156 *p
++ = htonl(8); /* attribute buffer length */
157 p
= xdr_encode_hyper(p
, dentry
->d_inode
->i_ino
);
160 *p
++ = xdr_one
; /* next */
161 *p
++ = xdr_zero
; /* cookie, first word */
162 *p
++ = xdr_two
; /* cookie, second word */
163 *p
++ = xdr_two
; /* entry len */
164 memcpy(p
, "..\0\0", 4); /* entry */
166 *p
++ = xdr_one
; /* bitmap length */
167 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
168 *p
++ = htonl(8); /* attribute buffer length */
169 p
= xdr_encode_hyper(p
, dentry
->d_parent
->d_inode
->i_ino
);
171 readdir
->pgbase
= (char *)p
- (char *)start
;
172 readdir
->count
-= readdir
->pgbase
;
173 kunmap_atomic(start
, KM_USER0
);
177 renew_lease(struct nfs_server
*server
, unsigned long timestamp
)
179 struct nfs4_client
*clp
= server
->nfs4_state
;
180 spin_lock(&clp
->cl_lock
);
181 if (time_before(clp
->cl_last_renewal
,timestamp
))
182 clp
->cl_last_renewal
= timestamp
;
183 spin_unlock(&clp
->cl_lock
);
186 static void update_changeattr(struct inode
*inode
, struct nfs4_change_info
*cinfo
)
188 struct nfs_inode
*nfsi
= NFS_I(inode
);
190 spin_lock(&inode
->i_lock
);
191 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
;
192 if (cinfo
->before
== nfsi
->change_attr
&& cinfo
->atomic
)
193 nfsi
->change_attr
= cinfo
->after
;
194 spin_unlock(&inode
->i_lock
);
197 /* Helper for asynchronous RPC calls */
198 static int nfs4_call_async(struct rpc_clnt
*clnt
, rpc_action tk_begin
,
199 rpc_action tk_exit
, void *calldata
)
201 struct rpc_task
*task
;
203 if (!(task
= rpc_new_task(clnt
, tk_exit
, RPC_TASK_ASYNC
)))
206 task
->tk_calldata
= calldata
;
207 task
->tk_action
= tk_begin
;
212 static void update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, int open_flags
)
214 struct inode
*inode
= state
->inode
;
216 open_flags
&= (FMODE_READ
|FMODE_WRITE
);
217 /* Protect against nfs4_find_state() */
218 spin_lock(&state
->owner
->so_lock
);
219 spin_lock(&inode
->i_lock
);
220 state
->state
|= open_flags
;
221 /* NB! List reordering - see the reclaim code for why. */
222 if ((open_flags
& FMODE_WRITE
) && 0 == state
->nwriters
++)
223 list_move(&state
->open_states
, &state
->owner
->so_states
);
224 if (open_flags
& FMODE_READ
)
226 memcpy(&state
->stateid
, stateid
, sizeof(state
->stateid
));
227 spin_unlock(&inode
->i_lock
);
228 spin_unlock(&state
->owner
->so_lock
);
233 * reclaim state on the server after a reboot.
235 static int _nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
237 struct inode
*inode
= state
->inode
;
238 struct nfs_server
*server
= NFS_SERVER(inode
);
239 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
240 struct nfs_openargs o_arg
= {
243 .open_flags
= state
->state
,
244 .clientid
= server
->nfs4_state
->cl_clientid
,
245 .claim
= NFS4_OPEN_CLAIM_PREVIOUS
,
246 .bitmask
= server
->attr_bitmask
,
248 struct nfs_openres o_res
= {
249 .server
= server
, /* Grrr */
251 struct rpc_message msg
= {
252 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
],
255 .rpc_cred
= sp
->so_cred
,
259 if (delegation
!= NULL
) {
260 if (!(delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
)) {
261 memcpy(&state
->stateid
, &delegation
->stateid
,
262 sizeof(state
->stateid
));
263 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
266 o_arg
.u
.delegation_type
= delegation
->type
;
268 o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
269 if (o_arg
.seqid
== NULL
)
271 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
272 /* Confirm the sequence as being established */
273 nfs_confirm_seqid(&sp
->so_seqid
, status
);
274 nfs_increment_open_seqid(status
, o_arg
.seqid
);
276 memcpy(&state
->stateid
, &o_res
.stateid
, sizeof(state
->stateid
));
277 if (o_res
.delegation_type
!= 0) {
278 nfs_inode_reclaim_delegation(inode
, sp
->so_cred
, &o_res
);
279 /* Did the server issue an immediate delegation recall? */
281 nfs_async_inode_return_delegation(inode
, &o_res
.stateid
);
284 nfs_free_seqid(o_arg
.seqid
);
285 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
286 /* Ensure we update the inode attributes */
291 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
293 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
294 struct nfs4_exception exception
= { };
297 err
= _nfs4_open_reclaim(sp
, state
);
298 if (err
!= -NFS4ERR_DELAY
)
300 nfs4_handle_exception(server
, err
, &exception
);
301 } while (exception
.retry
);
305 static int _nfs4_open_delegation_recall(struct dentry
*dentry
, struct nfs4_state
*state
)
307 struct nfs4_state_owner
*sp
= state
->owner
;
308 struct inode
*inode
= dentry
->d_inode
;
309 struct nfs_server
*server
= NFS_SERVER(inode
);
310 struct dentry
*parent
= dget_parent(dentry
);
311 struct nfs_openargs arg
= {
312 .fh
= NFS_FH(parent
->d_inode
),
313 .clientid
= server
->nfs4_state
->cl_clientid
,
314 .name
= &dentry
->d_name
,
317 .bitmask
= server
->attr_bitmask
,
318 .claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
,
320 struct nfs_openres res
= {
323 struct rpc_message msg
= {
324 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
],
327 .rpc_cred
= sp
->so_cred
,
331 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
333 if (state
->state
== 0)
335 arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
337 if (arg
.seqid
== NULL
)
339 arg
.open_flags
= state
->state
;
340 memcpy(arg
.u
.delegation
.data
, state
->stateid
.data
, sizeof(arg
.u
.delegation
.data
));
341 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
342 nfs_increment_open_seqid(status
, arg
.seqid
);
345 if(res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
346 status
= _nfs4_proc_open_confirm(server
->client
, NFS_FH(inode
),
347 sp
, &res
.stateid
, arg
.seqid
);
351 nfs_confirm_seqid(&sp
->so_seqid
, 0);
353 memcpy(state
->stateid
.data
, res
.stateid
.data
,
354 sizeof(state
->stateid
.data
));
355 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
358 nfs_free_seqid(arg
.seqid
);
364 int nfs4_open_delegation_recall(struct dentry
*dentry
, struct nfs4_state
*state
)
366 struct nfs4_exception exception
= { };
367 struct nfs_server
*server
= NFS_SERVER(dentry
->d_inode
);
370 err
= _nfs4_open_delegation_recall(dentry
, state
);
374 case -NFS4ERR_STALE_CLIENTID
:
375 case -NFS4ERR_STALE_STATEID
:
376 case -NFS4ERR_EXPIRED
:
377 /* Don't recall a delegation if it was lost */
378 nfs4_schedule_state_recovery(server
->nfs4_state
);
381 err
= nfs4_handle_exception(server
, err
, &exception
);
382 } while (exception
.retry
);
386 static int _nfs4_proc_open_confirm(struct rpc_clnt
*clnt
, const struct nfs_fh
*fh
, struct nfs4_state_owner
*sp
, nfs4_stateid
*stateid
, struct nfs_seqid
*seqid
)
388 struct nfs_open_confirmargs arg
= {
393 struct nfs_open_confirmres res
;
394 struct rpc_message msg
= {
395 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
398 .rpc_cred
= sp
->so_cred
,
402 status
= rpc_call_sync(clnt
, &msg
, RPC_TASK_NOINTR
);
403 /* Confirm the sequence as being established */
404 nfs_confirm_seqid(&sp
->so_seqid
, status
);
405 nfs_increment_open_seqid(status
, seqid
);
407 memcpy(stateid
, &res
.stateid
, sizeof(*stateid
));
411 static int _nfs4_proc_open(struct inode
*dir
, struct nfs4_state_owner
*sp
, struct nfs_openargs
*o_arg
, struct nfs_openres
*o_res
)
413 struct nfs_server
*server
= NFS_SERVER(dir
);
414 struct rpc_message msg
= {
415 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
418 .rpc_cred
= sp
->so_cred
,
422 /* Update sequence id. The caller must serialize! */
423 o_arg
->id
= sp
->so_id
;
424 o_arg
->clientid
= sp
->so_client
->cl_clientid
;
426 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
428 /* OPEN on anything except a regular file is disallowed in NFSv4 */
429 switch (o_res
->f_attr
->mode
& S_IFMT
) {
443 nfs_increment_open_seqid(status
, o_arg
->seqid
);
446 if (o_arg
->open_flags
& O_CREAT
) {
447 update_changeattr(dir
, &o_res
->cinfo
);
448 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
450 nfs_refresh_inode(dir
, o_res
->dir_attr
);
451 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
452 status
= _nfs4_proc_open_confirm(server
->client
, &o_res
->fh
,
453 sp
, &o_res
->stateid
, o_arg
->seqid
);
457 nfs_confirm_seqid(&sp
->so_seqid
, 0);
458 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
459 status
= server
->rpc_ops
->getattr(server
, &o_res
->fh
, o_res
->f_attr
);
464 static int _nfs4_do_access(struct inode
*inode
, struct rpc_cred
*cred
, int openflags
)
466 struct nfs_access_entry cache
;
470 if (openflags
& FMODE_READ
)
472 if (openflags
& FMODE_WRITE
)
474 status
= nfs_access_get_cached(inode
, cred
, &cache
);
478 /* Be clever: ask server to check for all possible rights */
479 cache
.mask
= MAY_EXEC
| MAY_WRITE
| MAY_READ
;
481 cache
.jiffies
= jiffies
;
482 status
= _nfs4_proc_access(inode
, &cache
);
485 nfs_access_add_cache(inode
, &cache
);
487 if ((cache
.mask
& mask
) == mask
)
494 * reclaim state on the server after a network partition.
495 * Assumes caller holds the appropriate lock
497 static int _nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
, struct dentry
*dentry
)
499 struct dentry
*parent
= dget_parent(dentry
);
500 struct inode
*dir
= parent
->d_inode
;
501 struct inode
*inode
= state
->inode
;
502 struct nfs_server
*server
= NFS_SERVER(dir
);
503 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
504 struct nfs_fattr f_attr
, dir_attr
;
505 struct nfs_openargs o_arg
= {
507 .open_flags
= state
->state
,
508 .name
= &dentry
->d_name
,
509 .bitmask
= server
->attr_bitmask
,
510 .claim
= NFS4_OPEN_CLAIM_NULL
,
512 struct nfs_openres o_res
= {
514 .dir_attr
= &dir_attr
,
519 if (delegation
!= NULL
&& !(delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
)) {
520 status
= _nfs4_do_access(inode
, sp
->so_cred
, state
->state
);
523 memcpy(&state
->stateid
, &delegation
->stateid
, sizeof(state
->stateid
));
524 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
527 o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
529 if (o_arg
.seqid
== NULL
)
531 nfs_fattr_init(&f_attr
);
532 nfs_fattr_init(&dir_attr
);
533 status
= _nfs4_proc_open(dir
, sp
, &o_arg
, &o_res
);
536 /* Check if files differ */
537 if ((f_attr
.mode
& S_IFMT
) != (inode
->i_mode
& S_IFMT
))
539 /* Has the file handle changed? */
540 if (nfs_compare_fh(&o_res
.fh
, NFS_FH(inode
)) != 0) {
541 /* Verify if the change attributes are the same */
542 if (f_attr
.change_attr
!= NFS_I(inode
)->change_attr
)
544 if (nfs_size_to_loff_t(f_attr
.size
) != inode
->i_size
)
546 /* Lets just pretend that this is the same file */
547 nfs_copy_fh(NFS_FH(inode
), &o_res
.fh
);
548 NFS_I(inode
)->fileid
= f_attr
.fileid
;
550 memcpy(&state
->stateid
, &o_res
.stateid
, sizeof(state
->stateid
));
551 if (o_res
.delegation_type
!= 0) {
552 if (!(delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
))
553 nfs_inode_set_delegation(inode
, sp
->so_cred
, &o_res
);
555 nfs_inode_reclaim_delegation(inode
, sp
->so_cred
, &o_res
);
558 nfs_free_seqid(o_arg
.seqid
);
559 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
565 /* Invalidate the state owner so we don't ever use it again */
566 nfs4_drop_state_owner(sp
);
568 /* Should we be trying to close that stateid? */
572 static inline int nfs4_do_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
, struct dentry
*dentry
)
574 struct nfs_server
*server
= NFS_SERVER(dentry
->d_inode
);
575 struct nfs4_exception exception
= { };
579 err
= _nfs4_open_expired(sp
, state
, dentry
);
580 if (err
== -NFS4ERR_DELAY
)
581 nfs4_handle_exception(server
, err
, &exception
);
582 } while (exception
.retry
);
586 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
588 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
589 struct nfs_open_context
*ctx
;
592 spin_lock(&state
->inode
->i_lock
);
593 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
594 if (ctx
->state
!= state
)
596 get_nfs_open_context(ctx
);
597 spin_unlock(&state
->inode
->i_lock
);
598 status
= nfs4_do_open_expired(sp
, state
, ctx
->dentry
);
599 put_nfs_open_context(ctx
);
602 spin_unlock(&state
->inode
->i_lock
);
607 * Returns an nfs4_state + an extra reference to the inode
609 static int _nfs4_open_delegated(struct inode
*inode
, int flags
, struct rpc_cred
*cred
, struct nfs4_state
**res
)
611 struct nfs_delegation
*delegation
;
612 struct nfs_server
*server
= NFS_SERVER(inode
);
613 struct nfs4_client
*clp
= server
->nfs4_state
;
614 struct nfs_inode
*nfsi
= NFS_I(inode
);
615 struct nfs4_state_owner
*sp
= NULL
;
616 struct nfs4_state
*state
= NULL
;
617 int open_flags
= flags
& (FMODE_READ
|FMODE_WRITE
);
620 /* Protect against reboot recovery - NOTE ORDER! */
621 down_read(&clp
->cl_sem
);
622 /* Protect against delegation recall */
623 down_read(&nfsi
->rwsem
);
624 delegation
= NFS_I(inode
)->delegation
;
626 if (delegation
== NULL
|| (delegation
->type
& open_flags
) != open_flags
)
629 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
630 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__
);
633 state
= nfs4_get_open_state(inode
, sp
);
638 if ((state
->state
& open_flags
) == open_flags
) {
639 spin_lock(&inode
->i_lock
);
640 if (open_flags
& FMODE_READ
)
642 if (open_flags
& FMODE_WRITE
)
644 spin_unlock(&inode
->i_lock
);
646 } else if (state
->state
!= 0)
650 err
= _nfs4_do_access(inode
, cred
, open_flags
);
654 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
655 update_open_stateid(state
, &delegation
->stateid
, open_flags
);
657 nfs4_put_state_owner(sp
);
658 up_read(&nfsi
->rwsem
);
659 up_read(&clp
->cl_sem
);
666 nfs4_put_open_state(state
);
667 nfs4_put_state_owner(sp
);
669 up_read(&nfsi
->rwsem
);
670 up_read(&clp
->cl_sem
);
672 nfs_inode_return_delegation(inode
);
676 static struct nfs4_state
*nfs4_open_delegated(struct inode
*inode
, int flags
, struct rpc_cred
*cred
)
678 struct nfs4_exception exception
= { };
679 struct nfs4_state
*res
;
683 err
= _nfs4_open_delegated(inode
, flags
, cred
, &res
);
686 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode
),
688 } while (exception
.retry
);
693 * Returns an nfs4_state + an referenced inode
695 static int _nfs4_do_open(struct inode
*dir
, struct dentry
*dentry
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
, struct nfs4_state
**res
)
697 struct nfs4_state_owner
*sp
;
698 struct nfs4_state
*state
= NULL
;
699 struct nfs_server
*server
= NFS_SERVER(dir
);
700 struct nfs4_client
*clp
= server
->nfs4_state
;
701 struct inode
*inode
= NULL
;
703 struct nfs_fattr f_attr
, dir_attr
;
704 struct nfs_openargs o_arg
= {
707 .name
= &dentry
->d_name
,
709 .bitmask
= server
->attr_bitmask
,
710 .claim
= NFS4_OPEN_CLAIM_NULL
,
712 struct nfs_openres o_res
= {
714 .dir_attr
= &dir_attr
,
718 /* Protect against reboot recovery conflicts */
719 down_read(&clp
->cl_sem
);
721 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
722 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
725 if (flags
& O_EXCL
) {
726 u32
*p
= (u32
*) o_arg
.u
.verifier
.data
;
730 o_arg
.u
.attrs
= sattr
;
731 /* Serialization for the sequence id */
733 o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
734 if (o_arg
.seqid
== NULL
)
736 nfs_fattr_init(&f_attr
);
737 nfs_fattr_init(&dir_attr
);
738 status
= _nfs4_proc_open(dir
, sp
, &o_arg
, &o_res
);
743 inode
= nfs_fhget(dir
->i_sb
, &o_res
.fh
, &f_attr
);
746 state
= nfs4_get_open_state(inode
, sp
);
749 update_open_stateid(state
, &o_res
.stateid
, flags
);
750 if (o_res
.delegation_type
!= 0)
751 nfs_inode_set_delegation(inode
, cred
, &o_res
);
752 nfs_free_seqid(o_arg
.seqid
);
753 nfs4_put_state_owner(sp
);
754 up_read(&clp
->cl_sem
);
760 nfs4_put_open_state(state
);
761 nfs_free_seqid(o_arg
.seqid
);
762 nfs4_put_state_owner(sp
);
764 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
765 up_read(&clp
->cl_sem
);
773 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
, struct dentry
*dentry
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
)
775 struct nfs4_exception exception
= { };
776 struct nfs4_state
*res
;
780 status
= _nfs4_do_open(dir
, dentry
, flags
, sattr
, cred
, &res
);
783 /* NOTE: BAD_SEQID means the server and client disagree about the
784 * book-keeping w.r.t. state-changing operations
785 * (OPEN/CLOSE/LOCK/LOCKU...)
786 * It is actually a sign of a bug on the client or on the server.
788 * If we receive a BAD_SEQID error in the particular case of
789 * doing an OPEN, we assume that nfs_increment_open_seqid() will
790 * have unhashed the old state_owner for us, and that we can
791 * therefore safely retry using a new one. We should still warn
794 if (status
== -NFS4ERR_BAD_SEQID
) {
795 printk(KERN_WARNING
"NFS: v4 server returned a bad sequence-id error!\n");
800 * BAD_STATEID on OPEN means that the server cancelled our
801 * state before it received the OPEN_CONFIRM.
802 * Recover by retrying the request as per the discussion
803 * on Page 181 of RFC3530.
805 if (status
== -NFS4ERR_BAD_STATEID
) {
809 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
810 status
, &exception
));
811 } while (exception
.retry
);
815 static int _nfs4_do_setattr(struct nfs_server
*server
, struct nfs_fattr
*fattr
,
816 struct nfs_fh
*fhandle
, struct iattr
*sattr
,
817 struct nfs4_state
*state
)
819 struct nfs_setattrargs arg
= {
823 .bitmask
= server
->attr_bitmask
,
825 struct nfs_setattrres res
= {
829 struct rpc_message msg
= {
830 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
836 nfs_fattr_init(fattr
);
839 msg
.rpc_cred
= state
->owner
->so_cred
;
840 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
842 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
844 status
= rpc_call_sync(server
->client
, &msg
, 0);
848 static int nfs4_do_setattr(struct nfs_server
*server
, struct nfs_fattr
*fattr
,
849 struct nfs_fh
*fhandle
, struct iattr
*sattr
,
850 struct nfs4_state
*state
)
852 struct nfs4_exception exception
= { };
855 err
= nfs4_handle_exception(server
,
856 _nfs4_do_setattr(server
, fattr
, fhandle
, sattr
,
859 } while (exception
.retry
);
863 struct nfs4_closedata
{
865 struct nfs4_state
*state
;
866 struct nfs_closeargs arg
;
867 struct nfs_closeres res
;
868 struct nfs_fattr fattr
;
871 static void nfs4_free_closedata(struct nfs4_closedata
*calldata
)
873 struct nfs4_state
*state
= calldata
->state
;
874 struct nfs4_state_owner
*sp
= state
->owner
;
876 nfs4_put_open_state(calldata
->state
);
877 nfs_free_seqid(calldata
->arg
.seqid
);
878 nfs4_put_state_owner(sp
);
882 static void nfs4_close_done(struct rpc_task
*task
)
884 struct nfs4_closedata
*calldata
= (struct nfs4_closedata
*)task
->tk_calldata
;
885 struct nfs4_state
*state
= calldata
->state
;
886 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
888 /* hmm. we are done with the inode, and in the process of freeing
889 * the state_owner. we keep this around to process errors
891 nfs_increment_open_seqid(task
->tk_status
, calldata
->arg
.seqid
);
892 switch (task
->tk_status
) {
894 memcpy(&state
->stateid
, &calldata
->res
.stateid
,
895 sizeof(state
->stateid
));
897 case -NFS4ERR_STALE_STATEID
:
898 case -NFS4ERR_EXPIRED
:
899 state
->state
= calldata
->arg
.open_flags
;
900 nfs4_schedule_state_recovery(server
->nfs4_state
);
903 if (nfs4_async_handle_error(task
, server
) == -EAGAIN
) {
904 rpc_restart_call(task
);
908 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
909 state
->state
= calldata
->arg
.open_flags
;
910 nfs4_free_closedata(calldata
);
913 static void nfs4_close_begin(struct rpc_task
*task
)
915 struct nfs4_closedata
*calldata
= (struct nfs4_closedata
*)task
->tk_calldata
;
916 struct nfs4_state
*state
= calldata
->state
;
917 struct rpc_message msg
= {
918 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
919 .rpc_argp
= &calldata
->arg
,
920 .rpc_resp
= &calldata
->res
,
921 .rpc_cred
= state
->owner
->so_cred
,
926 status
= nfs_wait_on_sequence(calldata
->arg
.seqid
, task
);
929 /* Don't reorder reads */
931 /* Recalculate the new open mode in case someone reopened the file
932 * while we were waiting in line to be scheduled.
934 if (state
->nreaders
!= 0)
936 if (state
->nwriters
!= 0)
938 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
940 if (mode
== state
->state
) {
941 nfs4_free_closedata(calldata
);
942 task
->tk_exit
= NULL
;
946 nfs_fattr_init(calldata
->res
.fattr
);
948 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
949 calldata
->arg
.open_flags
= mode
;
950 rpc_call_setup(task
, &msg
, 0);
954 * It is possible for data to be read/written from a mem-mapped file
955 * after the sys_close call (which hits the vfs layer as a flush).
956 * This means that we can't safely call nfsv4 close on a file until
957 * the inode is cleared. This in turn means that we are not good
958 * NFSv4 citizens - we do not indicate to the server to update the file's
959 * share state even when we are done with one of the three share
960 * stateid's in the inode.
962 * NOTE: Caller must be holding the sp->so_owner semaphore!
964 int nfs4_do_close(struct inode
*inode
, struct nfs4_state
*state
, mode_t mode
)
966 struct nfs_server
*server
= NFS_SERVER(inode
);
967 struct nfs4_closedata
*calldata
;
968 int status
= -ENOMEM
;
970 calldata
= kmalloc(sizeof(*calldata
), GFP_KERNEL
);
971 if (calldata
== NULL
)
973 calldata
->inode
= inode
;
974 calldata
->state
= state
;
975 calldata
->arg
.fh
= NFS_FH(inode
);
976 calldata
->arg
.stateid
= &state
->stateid
;
977 /* Serialization for the sequence id */
978 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
979 if (calldata
->arg
.seqid
== NULL
)
980 goto out_free_calldata
;
981 calldata
->arg
.bitmask
= server
->attr_bitmask
;
982 calldata
->res
.fattr
= &calldata
->fattr
;
983 calldata
->res
.server
= server
;
985 status
= nfs4_call_async(server
->client
, nfs4_close_begin
,
986 nfs4_close_done
, calldata
);
990 nfs_free_seqid(calldata
->arg
.seqid
);
997 static void nfs4_intent_set_file(struct nameidata
*nd
, struct dentry
*dentry
, struct nfs4_state
*state
)
1001 filp
= lookup_instantiate_filp(nd
, dentry
, NULL
);
1002 if (!IS_ERR(filp
)) {
1003 struct nfs_open_context
*ctx
;
1004 ctx
= (struct nfs_open_context
*)filp
->private_data
;
1007 nfs4_close_state(state
, nd
->intent
.open
.flags
);
1011 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1014 struct rpc_cred
*cred
;
1015 struct nfs4_state
*state
;
1018 if (nd
->flags
& LOOKUP_CREATE
) {
1019 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
1020 attr
.ia_valid
= ATTR_MODE
;
1021 if (!IS_POSIXACL(dir
))
1022 attr
.ia_mode
&= ~current
->fs
->umask
;
1025 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
1028 cred
= rpcauth_lookupcred(NFS_SERVER(dir
)->client
->cl_auth
, 0);
1030 return (struct dentry
*)cred
;
1031 state
= nfs4_do_open(dir
, dentry
, nd
->intent
.open
.flags
, &attr
, cred
);
1033 if (IS_ERR(state
)) {
1034 if (PTR_ERR(state
) == -ENOENT
)
1035 d_add(dentry
, NULL
);
1036 return (struct dentry
*)state
;
1038 res
= d_add_unique(dentry
, state
->inode
);
1041 nfs4_intent_set_file(nd
, dentry
, state
);
1046 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
1048 struct rpc_cred
*cred
;
1049 struct nfs4_state
*state
;
1050 struct inode
*inode
;
1052 cred
= rpcauth_lookupcred(NFS_SERVER(dir
)->client
->cl_auth
, 0);
1054 return PTR_ERR(cred
);
1055 state
= nfs4_open_delegated(dentry
->d_inode
, openflags
, cred
);
1057 state
= nfs4_do_open(dir
, dentry
, openflags
, NULL
, cred
);
1059 if (IS_ERR(state
)) {
1060 switch (PTR_ERR(state
)) {
1066 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
1069 if (dentry
->d_inode
== NULL
)
1074 inode
= state
->inode
;
1076 if (inode
== dentry
->d_inode
) {
1077 nfs4_intent_set_file(nd
, dentry
, state
);
1080 nfs4_close_state(state
, openflags
);
1087 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1089 struct nfs4_server_caps_res res
= {};
1090 struct rpc_message msg
= {
1091 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
1092 .rpc_argp
= fhandle
,
1097 status
= rpc_call_sync(server
->client
, &msg
, 0);
1099 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
1100 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
1101 server
->caps
|= NFS_CAP_ACLS
;
1102 if (res
.has_links
!= 0)
1103 server
->caps
|= NFS_CAP_HARDLINKS
;
1104 if (res
.has_symlinks
!= 0)
1105 server
->caps
|= NFS_CAP_SYMLINKS
;
1106 server
->acl_bitmask
= res
.acl_bitmask
;
1111 static int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1113 struct nfs4_exception exception
= { };
1116 err
= nfs4_handle_exception(server
,
1117 _nfs4_server_capabilities(server
, fhandle
),
1119 } while (exception
.retry
);
1123 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1124 struct nfs_fsinfo
*info
)
1126 struct nfs4_lookup_root_arg args
= {
1127 .bitmask
= nfs4_fattr_bitmap
,
1129 struct nfs4_lookup_res res
= {
1131 .fattr
= info
->fattr
,
1134 struct rpc_message msg
= {
1135 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
1139 nfs_fattr_init(info
->fattr
);
1140 return rpc_call_sync(server
->client
, &msg
, 0);
1143 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1144 struct nfs_fsinfo
*info
)
1146 struct nfs4_exception exception
= { };
1149 err
= nfs4_handle_exception(server
,
1150 _nfs4_lookup_root(server
, fhandle
, info
),
1152 } while (exception
.retry
);
1156 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1157 struct nfs_fsinfo
*info
)
1159 struct nfs_fattr
* fattr
= info
->fattr
;
1162 struct nfs4_lookup_arg args
= {
1165 .bitmask
= nfs4_fattr_bitmap
,
1167 struct nfs4_lookup_res res
= {
1172 struct rpc_message msg
= {
1173 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
1180 * Now we do a separate LOOKUP for each component of the mount path.
1181 * The LOOKUPs are done separately so that we can conveniently
1182 * catch an ERR_WRONGSEC if it occurs along the way...
1184 status
= nfs4_lookup_root(server
, fhandle
, info
);
1188 p
= server
->mnt_path
;
1190 struct nfs4_exception exception
= { };
1197 while (*p
&& (*p
!= '/'))
1202 nfs_fattr_init(fattr
);
1203 status
= nfs4_handle_exception(server
,
1204 rpc_call_sync(server
->client
, &msg
, 0),
1206 } while (exception
.retry
);
1209 if (status
== -ENOENT
) {
1210 printk(KERN_NOTICE
"NFS: mount path %s does not exist!\n", server
->mnt_path
);
1211 printk(KERN_NOTICE
"NFS: suggestion: try mounting '/' instead.\n");
1216 status
= nfs4_server_capabilities(server
, fhandle
);
1218 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
1223 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1225 struct nfs4_getattr_arg args
= {
1227 .bitmask
= server
->attr_bitmask
,
1229 struct nfs4_getattr_res res
= {
1233 struct rpc_message msg
= {
1234 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
1239 nfs_fattr_init(fattr
);
1240 return rpc_call_sync(server
->client
, &msg
, 0);
1243 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1245 struct nfs4_exception exception
= { };
1248 err
= nfs4_handle_exception(server
,
1249 _nfs4_proc_getattr(server
, fhandle
, fattr
),
1251 } while (exception
.retry
);
1256 * The file is not closed if it is opened due to the a request to change
1257 * the size of the file. The open call will not be needed once the
1258 * VFS layer lookup-intents are implemented.
1260 * Close is called when the inode is destroyed.
1261 * If we haven't opened the file for O_WRONLY, we
1262 * need to in the size_change case to obtain a stateid.
1265 * Because OPEN is always done by name in nfsv4, it is
1266 * possible that we opened a different file by the same
1267 * name. We can recognize this race condition, but we
1268 * can't do anything about it besides returning an error.
1270 * This will be fixed with VFS changes (lookup-intent).
1273 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
1274 struct iattr
*sattr
)
1276 struct rpc_cred
*cred
;
1277 struct inode
*inode
= dentry
->d_inode
;
1278 struct nfs4_state
*state
;
1281 nfs_fattr_init(fattr
);
1283 cred
= rpcauth_lookupcred(NFS_SERVER(inode
)->client
->cl_auth
, 0);
1285 return PTR_ERR(cred
);
1286 /* Search for an existing WRITE delegation first */
1287 state
= nfs4_open_delegated(inode
, FMODE_WRITE
, cred
);
1288 if (!IS_ERR(state
)) {
1289 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1292 /* Search for an existing open(O_WRITE) stateid */
1293 state
= nfs4_find_state(inode
, cred
, FMODE_WRITE
);
1296 status
= nfs4_do_setattr(NFS_SERVER(inode
), fattr
,
1297 NFS_FH(inode
), sattr
, state
);
1299 nfs_setattr_update_inode(inode
, sattr
);
1301 nfs4_close_state(state
, FMODE_WRITE
);
1306 static int _nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
1307 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1310 struct nfs_server
*server
= NFS_SERVER(dir
);
1311 struct nfs4_lookup_arg args
= {
1312 .bitmask
= server
->attr_bitmask
,
1313 .dir_fh
= NFS_FH(dir
),
1316 struct nfs4_lookup_res res
= {
1321 struct rpc_message msg
= {
1322 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
1327 nfs_fattr_init(fattr
);
1329 dprintk("NFS call lookup %s\n", name
->name
);
1330 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1331 dprintk("NFS reply lookup: %d\n", status
);
1335 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1337 struct nfs4_exception exception
= { };
1340 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1341 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
1343 } while (exception
.retry
);
1347 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
1349 struct nfs4_accessargs args
= {
1350 .fh
= NFS_FH(inode
),
1352 struct nfs4_accessres res
= { 0 };
1353 struct rpc_message msg
= {
1354 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
1357 .rpc_cred
= entry
->cred
,
1359 int mode
= entry
->mask
;
1363 * Determine which access bits we want to ask for...
1365 if (mode
& MAY_READ
)
1366 args
.access
|= NFS4_ACCESS_READ
;
1367 if (S_ISDIR(inode
->i_mode
)) {
1368 if (mode
& MAY_WRITE
)
1369 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
1370 if (mode
& MAY_EXEC
)
1371 args
.access
|= NFS4_ACCESS_LOOKUP
;
1373 if (mode
& MAY_WRITE
)
1374 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
1375 if (mode
& MAY_EXEC
)
1376 args
.access
|= NFS4_ACCESS_EXECUTE
;
1378 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
1381 if (res
.access
& NFS4_ACCESS_READ
)
1382 entry
->mask
|= MAY_READ
;
1383 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
1384 entry
->mask
|= MAY_WRITE
;
1385 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
1386 entry
->mask
|= MAY_EXEC
;
1391 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
1393 struct nfs4_exception exception
= { };
1396 err
= nfs4_handle_exception(NFS_SERVER(inode
),
1397 _nfs4_proc_access(inode
, entry
),
1399 } while (exception
.retry
);
1404 * TODO: For the time being, we don't try to get any attributes
1405 * along with any of the zero-copy operations READ, READDIR,
1408 * In the case of the first three, we want to put the GETATTR
1409 * after the read-type operation -- this is because it is hard
1410 * to predict the length of a GETATTR response in v4, and thus
1411 * align the READ data correctly. This means that the GETATTR
1412 * may end up partially falling into the page cache, and we should
1413 * shift it into the 'tail' of the xdr_buf before processing.
1414 * To do this efficiently, we need to know the total length
1415 * of data received, which doesn't seem to be available outside
1418 * In the case of WRITE, we also want to put the GETATTR after
1419 * the operation -- in this case because we want to make sure
1420 * we get the post-operation mtime and size. This means that
1421 * we can't use xdr_encode_pages() as written: we need a variant
1422 * of it which would leave room in the 'tail' iovec.
1424 * Both of these changes to the XDR layer would in fact be quite
1425 * minor, but I decided to leave them for a subsequent patch.
1427 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
1428 unsigned int pgbase
, unsigned int pglen
)
1430 struct nfs4_readlink args
= {
1431 .fh
= NFS_FH(inode
),
1436 struct rpc_message msg
= {
1437 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
1442 return rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
1445 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
1446 unsigned int pgbase
, unsigned int pglen
)
1448 struct nfs4_exception exception
= { };
1451 err
= nfs4_handle_exception(NFS_SERVER(inode
),
1452 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
1454 } while (exception
.retry
);
1458 static int _nfs4_proc_read(struct nfs_read_data
*rdata
)
1460 int flags
= rdata
->flags
;
1461 struct inode
*inode
= rdata
->inode
;
1462 struct nfs_fattr
*fattr
= rdata
->res
.fattr
;
1463 struct nfs_server
*server
= NFS_SERVER(inode
);
1464 struct rpc_message msg
= {
1465 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
],
1466 .rpc_argp
= &rdata
->args
,
1467 .rpc_resp
= &rdata
->res
,
1468 .rpc_cred
= rdata
->cred
,
1470 unsigned long timestamp
= jiffies
;
1473 dprintk("NFS call read %d @ %Ld\n", rdata
->args
.count
,
1474 (long long) rdata
->args
.offset
);
1476 nfs_fattr_init(fattr
);
1477 status
= rpc_call_sync(server
->client
, &msg
, flags
);
1479 renew_lease(server
, timestamp
);
1480 dprintk("NFS reply read: %d\n", status
);
1484 static int nfs4_proc_read(struct nfs_read_data
*rdata
)
1486 struct nfs4_exception exception
= { };
1489 err
= nfs4_handle_exception(NFS_SERVER(rdata
->inode
),
1490 _nfs4_proc_read(rdata
),
1492 } while (exception
.retry
);
1496 static int _nfs4_proc_write(struct nfs_write_data
*wdata
)
1498 int rpcflags
= wdata
->flags
;
1499 struct inode
*inode
= wdata
->inode
;
1500 struct nfs_fattr
*fattr
= wdata
->res
.fattr
;
1501 struct nfs_server
*server
= NFS_SERVER(inode
);
1502 struct rpc_message msg
= {
1503 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
],
1504 .rpc_argp
= &wdata
->args
,
1505 .rpc_resp
= &wdata
->res
,
1506 .rpc_cred
= wdata
->cred
,
1510 dprintk("NFS call write %d @ %Ld\n", wdata
->args
.count
,
1511 (long long) wdata
->args
.offset
);
1513 nfs_fattr_init(fattr
);
1514 status
= rpc_call_sync(server
->client
, &msg
, rpcflags
);
1515 dprintk("NFS reply write: %d\n", status
);
1519 static int nfs4_proc_write(struct nfs_write_data
*wdata
)
1521 struct nfs4_exception exception
= { };
1524 err
= nfs4_handle_exception(NFS_SERVER(wdata
->inode
),
1525 _nfs4_proc_write(wdata
),
1527 } while (exception
.retry
);
1531 static int _nfs4_proc_commit(struct nfs_write_data
*cdata
)
1533 struct inode
*inode
= cdata
->inode
;
1534 struct nfs_fattr
*fattr
= cdata
->res
.fattr
;
1535 struct nfs_server
*server
= NFS_SERVER(inode
);
1536 struct rpc_message msg
= {
1537 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
],
1538 .rpc_argp
= &cdata
->args
,
1539 .rpc_resp
= &cdata
->res
,
1540 .rpc_cred
= cdata
->cred
,
1544 dprintk("NFS call commit %d @ %Ld\n", cdata
->args
.count
,
1545 (long long) cdata
->args
.offset
);
1547 nfs_fattr_init(fattr
);
1548 status
= rpc_call_sync(server
->client
, &msg
, 0);
1549 dprintk("NFS reply commit: %d\n", status
);
1553 static int nfs4_proc_commit(struct nfs_write_data
*cdata
)
1555 struct nfs4_exception exception
= { };
1558 err
= nfs4_handle_exception(NFS_SERVER(cdata
->inode
),
1559 _nfs4_proc_commit(cdata
),
1561 } while (exception
.retry
);
1567 * We will need to arrange for the VFS layer to provide an atomic open.
1568 * Until then, this create/open method is prone to inefficiency and race
1569 * conditions due to the lookup, create, and open VFS calls from sys_open()
1570 * placed on the wire.
1572 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1573 * The file will be opened again in the subsequent VFS open call
1574 * (nfs4_proc_file_open).
1576 * The open for read will just hang around to be used by any process that
1577 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1581 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
1582 int flags
, struct nameidata
*nd
)
1584 struct nfs4_state
*state
;
1585 struct rpc_cred
*cred
;
1588 cred
= rpcauth_lookupcred(NFS_SERVER(dir
)->client
->cl_auth
, 0);
1590 status
= PTR_ERR(cred
);
1593 state
= nfs4_do_open(dir
, dentry
, flags
, sattr
, cred
);
1595 if (IS_ERR(state
)) {
1596 status
= PTR_ERR(state
);
1599 d_instantiate(dentry
, state
->inode
);
1600 if (flags
& O_EXCL
) {
1601 struct nfs_fattr fattr
;
1602 status
= nfs4_do_setattr(NFS_SERVER(dir
), &fattr
,
1603 NFS_FH(state
->inode
), sattr
, state
);
1605 nfs_setattr_update_inode(state
->inode
, sattr
);
1607 if (status
== 0 && nd
!= NULL
&& (nd
->flags
& LOOKUP_OPEN
))
1608 nfs4_intent_set_file(nd
, dentry
, state
);
1610 nfs4_close_state(state
, flags
);
1615 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
1617 struct nfs4_remove_arg args
= {
1621 struct nfs4_change_info res
;
1622 struct rpc_message msg
= {
1623 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
1629 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1631 update_changeattr(dir
, &res
);
1635 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
1637 struct nfs4_exception exception
= { };
1640 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1641 _nfs4_proc_remove(dir
, name
),
1643 } while (exception
.retry
);
1647 struct unlink_desc
{
1648 struct nfs4_remove_arg args
;
1649 struct nfs4_change_info res
;
1652 static int nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct dentry
*dir
,
1655 struct unlink_desc
*up
;
1657 up
= (struct unlink_desc
*) kmalloc(sizeof(*up
), GFP_KERNEL
);
1661 up
->args
.fh
= NFS_FH(dir
->d_inode
);
1662 up
->args
.name
= name
;
1664 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
1665 msg
->rpc_argp
= &up
->args
;
1666 msg
->rpc_resp
= &up
->res
;
1670 static int nfs4_proc_unlink_done(struct dentry
*dir
, struct rpc_task
*task
)
1672 struct rpc_message
*msg
= &task
->tk_msg
;
1673 struct unlink_desc
*up
;
1675 if (msg
->rpc_resp
!= NULL
) {
1676 up
= container_of(msg
->rpc_resp
, struct unlink_desc
, res
);
1677 update_changeattr(dir
->d_inode
, &up
->res
);
1679 msg
->rpc_resp
= NULL
;
1680 msg
->rpc_argp
= NULL
;
1685 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
1686 struct inode
*new_dir
, struct qstr
*new_name
)
1688 struct nfs4_rename_arg arg
= {
1689 .old_dir
= NFS_FH(old_dir
),
1690 .new_dir
= NFS_FH(new_dir
),
1691 .old_name
= old_name
,
1692 .new_name
= new_name
,
1694 struct nfs4_rename_res res
= { };
1695 struct rpc_message msg
= {
1696 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
1702 status
= rpc_call_sync(NFS_CLIENT(old_dir
), &msg
, 0);
1705 update_changeattr(old_dir
, &res
.old_cinfo
);
1706 update_changeattr(new_dir
, &res
.new_cinfo
);
1711 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
1712 struct inode
*new_dir
, struct qstr
*new_name
)
1714 struct nfs4_exception exception
= { };
1717 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
1718 _nfs4_proc_rename(old_dir
, old_name
,
1721 } while (exception
.retry
);
1725 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
1727 struct nfs4_link_arg arg
= {
1728 .fh
= NFS_FH(inode
),
1729 .dir_fh
= NFS_FH(dir
),
1732 struct nfs4_change_info cinfo
= { };
1733 struct rpc_message msg
= {
1734 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
1740 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
1742 update_changeattr(dir
, &cinfo
);
1747 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
1749 struct nfs4_exception exception
= { };
1752 err
= nfs4_handle_exception(NFS_SERVER(inode
),
1753 _nfs4_proc_link(inode
, dir
, name
),
1755 } while (exception
.retry
);
1759 static int _nfs4_proc_symlink(struct inode
*dir
, struct qstr
*name
,
1760 struct qstr
*path
, struct iattr
*sattr
, struct nfs_fh
*fhandle
,
1761 struct nfs_fattr
*fattr
)
1763 struct nfs_server
*server
= NFS_SERVER(dir
);
1764 struct nfs_fattr dir_fattr
;
1765 struct nfs4_create_arg arg
= {
1766 .dir_fh
= NFS_FH(dir
),
1771 .bitmask
= server
->attr_bitmask
,
1773 struct nfs4_create_res res
= {
1777 .dir_fattr
= &dir_fattr
,
1779 struct rpc_message msg
= {
1780 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
],
1786 if (path
->len
> NFS4_MAXPATHLEN
)
1787 return -ENAMETOOLONG
;
1788 arg
.u
.symlink
= path
;
1789 nfs_fattr_init(fattr
);
1790 nfs_fattr_init(&dir_fattr
);
1792 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1794 update_changeattr(dir
, &res
.dir_cinfo
);
1795 nfs_post_op_update_inode(dir
, res
.dir_fattr
);
1799 static int nfs4_proc_symlink(struct inode
*dir
, struct qstr
*name
,
1800 struct qstr
*path
, struct iattr
*sattr
, struct nfs_fh
*fhandle
,
1801 struct nfs_fattr
*fattr
)
1803 struct nfs4_exception exception
= { };
1806 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1807 _nfs4_proc_symlink(dir
, name
, path
, sattr
,
1810 } while (exception
.retry
);
1814 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
1815 struct iattr
*sattr
)
1817 struct nfs_server
*server
= NFS_SERVER(dir
);
1818 struct nfs_fh fhandle
;
1819 struct nfs_fattr fattr
, dir_fattr
;
1820 struct nfs4_create_arg arg
= {
1821 .dir_fh
= NFS_FH(dir
),
1823 .name
= &dentry
->d_name
,
1826 .bitmask
= server
->attr_bitmask
,
1828 struct nfs4_create_res res
= {
1832 .dir_fattr
= &dir_fattr
,
1834 struct rpc_message msg
= {
1835 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
],
1841 nfs_fattr_init(&fattr
);
1842 nfs_fattr_init(&dir_fattr
);
1844 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1846 update_changeattr(dir
, &res
.dir_cinfo
);
1847 nfs_post_op_update_inode(dir
, res
.dir_fattr
);
1848 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
1853 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
1854 struct iattr
*sattr
)
1856 struct nfs4_exception exception
= { };
1859 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1860 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
1862 } while (exception
.retry
);
1866 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
1867 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
1869 struct inode
*dir
= dentry
->d_inode
;
1870 struct nfs4_readdir_arg args
= {
1875 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
1877 struct nfs4_readdir_res res
;
1878 struct rpc_message msg
= {
1879 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
1886 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__
,
1887 dentry
->d_parent
->d_name
.name
,
1888 dentry
->d_name
.name
,
1889 (unsigned long long)cookie
);
1891 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
1892 res
.pgbase
= args
.pgbase
;
1893 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1895 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
1897 dprintk("%s: returns %d\n", __FUNCTION__
, status
);
1901 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
1902 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
1904 struct nfs4_exception exception
= { };
1907 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
1908 _nfs4_proc_readdir(dentry
, cred
, cookie
,
1911 } while (exception
.retry
);
1915 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
1916 struct iattr
*sattr
, dev_t rdev
)
1918 struct nfs_server
*server
= NFS_SERVER(dir
);
1920 struct nfs_fattr fattr
, dir_fattr
;
1921 struct nfs4_create_arg arg
= {
1922 .dir_fh
= NFS_FH(dir
),
1924 .name
= &dentry
->d_name
,
1926 .bitmask
= server
->attr_bitmask
,
1928 struct nfs4_create_res res
= {
1932 .dir_fattr
= &dir_fattr
,
1934 struct rpc_message msg
= {
1935 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
],
1940 int mode
= sattr
->ia_mode
;
1942 nfs_fattr_init(&fattr
);
1943 nfs_fattr_init(&dir_fattr
);
1945 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
1946 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
1948 arg
.ftype
= NF4FIFO
;
1949 else if (S_ISBLK(mode
)) {
1951 arg
.u
.device
.specdata1
= MAJOR(rdev
);
1952 arg
.u
.device
.specdata2
= MINOR(rdev
);
1954 else if (S_ISCHR(mode
)) {
1956 arg
.u
.device
.specdata1
= MAJOR(rdev
);
1957 arg
.u
.device
.specdata2
= MINOR(rdev
);
1960 arg
.ftype
= NF4SOCK
;
1962 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1964 update_changeattr(dir
, &res
.dir_cinfo
);
1965 nfs_post_op_update_inode(dir
, res
.dir_fattr
);
1966 status
= nfs_instantiate(dentry
, &fh
, &fattr
);
1971 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
1972 struct iattr
*sattr
, dev_t rdev
)
1974 struct nfs4_exception exception
= { };
1977 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1978 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
1980 } while (exception
.retry
);
1984 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1985 struct nfs_fsstat
*fsstat
)
1987 struct nfs4_statfs_arg args
= {
1989 .bitmask
= server
->attr_bitmask
,
1991 struct rpc_message msg
= {
1992 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
1997 nfs_fattr_init(fsstat
->fattr
);
1998 return rpc_call_sync(server
->client
, &msg
, 0);
2001 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2003 struct nfs4_exception exception
= { };
2006 err
= nfs4_handle_exception(server
,
2007 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2009 } while (exception
.retry
);
2013 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2014 struct nfs_fsinfo
*fsinfo
)
2016 struct nfs4_fsinfo_arg args
= {
2018 .bitmask
= server
->attr_bitmask
,
2020 struct rpc_message msg
= {
2021 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
2026 return rpc_call_sync(server
->client
, &msg
, 0);
2029 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2031 struct nfs4_exception exception
= { };
2035 err
= nfs4_handle_exception(server
,
2036 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
2038 } while (exception
.retry
);
2042 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2044 nfs_fattr_init(fsinfo
->fattr
);
2045 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
2048 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2049 struct nfs_pathconf
*pathconf
)
2051 struct nfs4_pathconf_arg args
= {
2053 .bitmask
= server
->attr_bitmask
,
2055 struct rpc_message msg
= {
2056 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
2058 .rpc_resp
= pathconf
,
2061 /* None of the pathconf attributes are mandatory to implement */
2062 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
2063 memset(pathconf
, 0, sizeof(*pathconf
));
2067 nfs_fattr_init(pathconf
->fattr
);
2068 return rpc_call_sync(server
->client
, &msg
, 0);
2071 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2072 struct nfs_pathconf
*pathconf
)
2074 struct nfs4_exception exception
= { };
2078 err
= nfs4_handle_exception(server
,
2079 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
2081 } while (exception
.retry
);
2086 nfs4_read_done(struct rpc_task
*task
)
2088 struct nfs_read_data
*data
= (struct nfs_read_data
*) task
->tk_calldata
;
2089 struct inode
*inode
= data
->inode
;
2091 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
)) == -EAGAIN
) {
2092 rpc_restart_call(task
);
2095 if (task
->tk_status
> 0)
2096 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
2097 /* Call back common NFS readpage processing */
2098 nfs_readpage_result(task
);
2102 nfs4_proc_read_setup(struct nfs_read_data
*data
)
2104 struct rpc_task
*task
= &data
->task
;
2105 struct rpc_message msg
= {
2106 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
],
2107 .rpc_argp
= &data
->args
,
2108 .rpc_resp
= &data
->res
,
2109 .rpc_cred
= data
->cred
,
2111 struct inode
*inode
= data
->inode
;
2114 data
->timestamp
= jiffies
;
2116 /* N.B. Do we need to test? Never called for swapfile inode */
2117 flags
= RPC_TASK_ASYNC
| (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
2119 /* Finalize the task. */
2120 rpc_init_task(task
, NFS_CLIENT(inode
), nfs4_read_done
, flags
);
2121 rpc_call_setup(task
, &msg
, 0);
2125 nfs4_write_done(struct rpc_task
*task
)
2127 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
2128 struct inode
*inode
= data
->inode
;
2130 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
)) == -EAGAIN
) {
2131 rpc_restart_call(task
);
2134 if (task
->tk_status
>= 0)
2135 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
2136 /* Call back common NFS writeback processing */
2137 nfs_writeback_done(task
);
2141 nfs4_proc_write_setup(struct nfs_write_data
*data
, int how
)
2143 struct rpc_task
*task
= &data
->task
;
2144 struct rpc_message msg
= {
2145 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
],
2146 .rpc_argp
= &data
->args
,
2147 .rpc_resp
= &data
->res
,
2148 .rpc_cred
= data
->cred
,
2150 struct inode
*inode
= data
->inode
;
2154 if (how
& FLUSH_STABLE
) {
2155 if (!NFS_I(inode
)->ncommit
)
2156 stable
= NFS_FILE_SYNC
;
2158 stable
= NFS_DATA_SYNC
;
2160 stable
= NFS_UNSTABLE
;
2161 data
->args
.stable
= stable
;
2163 data
->timestamp
= jiffies
;
2165 /* Set the initial flags for the task. */
2166 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
2168 /* Finalize the task. */
2169 rpc_init_task(task
, NFS_CLIENT(inode
), nfs4_write_done
, flags
);
2170 rpc_call_setup(task
, &msg
, 0);
2174 nfs4_commit_done(struct rpc_task
*task
)
2176 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
2177 struct inode
*inode
= data
->inode
;
2179 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
)) == -EAGAIN
) {
2180 rpc_restart_call(task
);
2183 /* Call back common NFS writeback processing */
2184 nfs_commit_done(task
);
2188 nfs4_proc_commit_setup(struct nfs_write_data
*data
, int how
)
2190 struct rpc_task
*task
= &data
->task
;
2191 struct rpc_message msg
= {
2192 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
],
2193 .rpc_argp
= &data
->args
,
2194 .rpc_resp
= &data
->res
,
2195 .rpc_cred
= data
->cred
,
2197 struct inode
*inode
= data
->inode
;
2200 /* Set the initial flags for the task. */
2201 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
2203 /* Finalize the task. */
2204 rpc_init_task(task
, NFS_CLIENT(inode
), nfs4_commit_done
, flags
);
2205 rpc_call_setup(task
, &msg
, 0);
2209 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2210 * standalone procedure for queueing an asynchronous RENEW.
2213 renew_done(struct rpc_task
*task
)
2215 struct nfs4_client
*clp
= (struct nfs4_client
*)task
->tk_msg
.rpc_argp
;
2216 unsigned long timestamp
= (unsigned long)task
->tk_calldata
;
2218 if (task
->tk_status
< 0) {
2219 switch (task
->tk_status
) {
2220 case -NFS4ERR_STALE_CLIENTID
:
2221 case -NFS4ERR_EXPIRED
:
2222 case -NFS4ERR_CB_PATH_DOWN
:
2223 nfs4_schedule_state_recovery(clp
);
2227 spin_lock(&clp
->cl_lock
);
2228 if (time_before(clp
->cl_last_renewal
,timestamp
))
2229 clp
->cl_last_renewal
= timestamp
;
2230 spin_unlock(&clp
->cl_lock
);
2234 nfs4_proc_async_renew(struct nfs4_client
*clp
)
2236 struct rpc_message msg
= {
2237 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2239 .rpc_cred
= clp
->cl_cred
,
2242 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
2243 renew_done
, (void *)jiffies
);
2247 nfs4_proc_renew(struct nfs4_client
*clp
)
2249 struct rpc_message msg
= {
2250 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2252 .rpc_cred
= clp
->cl_cred
,
2254 unsigned long now
= jiffies
;
2257 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2260 spin_lock(&clp
->cl_lock
);
2261 if (time_before(clp
->cl_last_renewal
,now
))
2262 clp
->cl_last_renewal
= now
;
2263 spin_unlock(&clp
->cl_lock
);
2267 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
2269 return (server
->caps
& NFS_CAP_ACLS
)
2270 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
2271 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
2274 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2275 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2278 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2280 static void buf_to_pages(const void *buf
, size_t buflen
,
2281 struct page
**pages
, unsigned int *pgbase
)
2283 const void *p
= buf
;
2285 *pgbase
= offset_in_page(buf
);
2287 while (p
< buf
+ buflen
) {
2288 *(pages
++) = virt_to_page(p
);
2289 p
+= PAGE_CACHE_SIZE
;
2293 struct nfs4_cached_acl
{
2299 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
2301 struct nfs_inode
*nfsi
= NFS_I(inode
);
2303 spin_lock(&inode
->i_lock
);
2304 kfree(nfsi
->nfs4_acl
);
2305 nfsi
->nfs4_acl
= acl
;
2306 spin_unlock(&inode
->i_lock
);
2309 static void nfs4_zap_acl_attr(struct inode
*inode
)
2311 nfs4_set_cached_acl(inode
, NULL
);
2314 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
2316 struct nfs_inode
*nfsi
= NFS_I(inode
);
2317 struct nfs4_cached_acl
*acl
;
2320 spin_lock(&inode
->i_lock
);
2321 acl
= nfsi
->nfs4_acl
;
2324 if (buf
== NULL
) /* user is just asking for length */
2326 if (acl
->cached
== 0)
2328 ret
= -ERANGE
; /* see getxattr(2) man page */
2329 if (acl
->len
> buflen
)
2331 memcpy(buf
, acl
->data
, acl
->len
);
2335 spin_unlock(&inode
->i_lock
);
2339 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
2341 struct nfs4_cached_acl
*acl
;
2343 if (buf
&& acl_len
<= PAGE_SIZE
) {
2344 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
2348 memcpy(acl
->data
, buf
, acl_len
);
2350 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
2357 nfs4_set_cached_acl(inode
, acl
);
2360 static inline ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
2362 struct page
*pages
[NFS4ACL_MAXPAGES
];
2363 struct nfs_getaclargs args
= {
2364 .fh
= NFS_FH(inode
),
2368 size_t resp_len
= buflen
;
2370 struct rpc_message msg
= {
2371 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
2373 .rpc_resp
= &resp_len
,
2375 struct page
*localpage
= NULL
;
2378 if (buflen
< PAGE_SIZE
) {
2379 /* As long as we're doing a round trip to the server anyway,
2380 * let's be prepared for a page of acl data. */
2381 localpage
= alloc_page(GFP_KERNEL
);
2382 resp_buf
= page_address(localpage
);
2383 if (localpage
== NULL
)
2385 args
.acl_pages
[0] = localpage
;
2386 args
.acl_pgbase
= 0;
2387 resp_len
= args
.acl_len
= PAGE_SIZE
;
2390 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
2392 ret
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
2395 if (resp_len
> args
.acl_len
)
2396 nfs4_write_cached_acl(inode
, NULL
, resp_len
);
2398 nfs4_write_cached_acl(inode
, resp_buf
, resp_len
);
2401 if (resp_len
> buflen
)
2404 memcpy(buf
, resp_buf
, resp_len
);
2409 __free_page(localpage
);
2413 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
2415 struct nfs_server
*server
= NFS_SERVER(inode
);
2418 if (!nfs4_server_supports_acls(server
))
2420 ret
= nfs_revalidate_inode(server
, inode
);
2423 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
2426 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
2429 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
2431 struct nfs_server
*server
= NFS_SERVER(inode
);
2432 struct page
*pages
[NFS4ACL_MAXPAGES
];
2433 struct nfs_setaclargs arg
= {
2434 .fh
= NFS_FH(inode
),
2438 struct rpc_message msg
= {
2439 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
2445 if (!nfs4_server_supports_acls(server
))
2447 nfs_inode_return_delegation(inode
);
2448 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
2449 ret
= rpc_call_sync(NFS_SERVER(inode
)->client
, &msg
, 0);
2451 nfs4_write_cached_acl(inode
, buf
, buflen
);
2456 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
)
2458 struct nfs4_client
*clp
= server
->nfs4_state
;
2460 if (!clp
|| task
->tk_status
>= 0)
2462 switch(task
->tk_status
) {
2463 case -NFS4ERR_STALE_CLIENTID
:
2464 case -NFS4ERR_STALE_STATEID
:
2465 case -NFS4ERR_EXPIRED
:
2466 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
, NULL
);
2467 nfs4_schedule_state_recovery(clp
);
2468 if (test_bit(NFS4CLNT_OK
, &clp
->cl_state
))
2469 rpc_wake_up_task(task
);
2470 task
->tk_status
= 0;
2472 case -NFS4ERR_GRACE
:
2473 case -NFS4ERR_DELAY
:
2474 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
2475 task
->tk_status
= 0;
2477 case -NFS4ERR_OLD_STATEID
:
2478 task
->tk_status
= 0;
2481 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
2485 static int nfs4_wait_clnt_recover(struct rpc_clnt
*clnt
, struct nfs4_client
*clp
)
2489 int interruptible
, res
= 0;
2493 rpc_clnt_sigmask(clnt
, &oldset
);
2494 interruptible
= TASK_UNINTERRUPTIBLE
;
2496 interruptible
= TASK_INTERRUPTIBLE
;
2497 prepare_to_wait(&clp
->cl_waitq
, &wait
, interruptible
);
2498 nfs4_schedule_state_recovery(clp
);
2499 if (clnt
->cl_intr
&& signalled())
2501 else if (!test_bit(NFS4CLNT_OK
, &clp
->cl_state
))
2503 finish_wait(&clp
->cl_waitq
, &wait
);
2504 rpc_clnt_sigunmask(clnt
, &oldset
);
2508 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
2516 *timeout
= NFS4_POLL_RETRY_MIN
;
2517 if (*timeout
> NFS4_POLL_RETRY_MAX
)
2518 *timeout
= NFS4_POLL_RETRY_MAX
;
2519 rpc_clnt_sigmask(clnt
, &oldset
);
2520 if (clnt
->cl_intr
) {
2521 schedule_timeout_interruptible(*timeout
);
2525 schedule_timeout_uninterruptible(*timeout
);
2526 rpc_clnt_sigunmask(clnt
, &oldset
);
2531 /* This is the error handling routine for processes that are allowed
2534 int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
2536 struct nfs4_client
*clp
= server
->nfs4_state
;
2537 int ret
= errorcode
;
2539 exception
->retry
= 0;
2543 case -NFS4ERR_STALE_CLIENTID
:
2544 case -NFS4ERR_STALE_STATEID
:
2545 case -NFS4ERR_EXPIRED
:
2546 ret
= nfs4_wait_clnt_recover(server
->client
, clp
);
2548 exception
->retry
= 1;
2550 case -NFS4ERR_GRACE
:
2551 case -NFS4ERR_DELAY
:
2552 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
2554 exception
->retry
= 1;
2556 case -NFS4ERR_OLD_STATEID
:
2558 exception
->retry
= 1;
2560 /* We failed to handle the error */
2561 return nfs4_map_errors(ret
);
2564 int nfs4_proc_setclientid(struct nfs4_client
*clp
, u32 program
, unsigned short port
)
2566 nfs4_verifier sc_verifier
;
2567 struct nfs4_setclientid setclientid
= {
2568 .sc_verifier
= &sc_verifier
,
2571 struct rpc_message msg
= {
2572 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
2573 .rpc_argp
= &setclientid
,
2575 .rpc_cred
= clp
->cl_cred
,
2581 p
= (u32
*)sc_verifier
.data
;
2582 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
2583 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
2586 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
2587 sizeof(setclientid
.sc_name
), "%s/%u.%u.%u.%u %s %u",
2588 clp
->cl_ipaddr
, NIPQUAD(clp
->cl_addr
.s_addr
),
2589 clp
->cl_cred
->cr_ops
->cr_name
,
2590 clp
->cl_id_uniquifier
);
2591 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
2592 sizeof(setclientid
.sc_netid
), "tcp");
2593 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
2594 sizeof(setclientid
.sc_uaddr
), "%s.%d.%d",
2595 clp
->cl_ipaddr
, port
>> 8, port
& 255);
2597 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2598 if (status
!= -NFS4ERR_CLID_INUSE
)
2603 ssleep(clp
->cl_lease_time
+ 1);
2605 if (++clp
->cl_id_uniquifier
== 0)
2612 nfs4_proc_setclientid_confirm(struct nfs4_client
*clp
)
2614 struct nfs_fsinfo fsinfo
;
2615 struct rpc_message msg
= {
2616 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
2618 .rpc_resp
= &fsinfo
,
2619 .rpc_cred
= clp
->cl_cred
,
2625 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2627 spin_lock(&clp
->cl_lock
);
2628 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
2629 clp
->cl_last_renewal
= now
;
2630 spin_unlock(&clp
->cl_lock
);
2635 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
)
2637 struct nfs4_delegreturnargs args
= {
2638 .fhandle
= NFS_FH(inode
),
2641 struct rpc_message msg
= {
2642 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
2647 return rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
2650 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
)
2652 struct nfs_server
*server
= NFS_SERVER(inode
);
2653 struct nfs4_exception exception
= { };
2656 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
);
2658 case -NFS4ERR_STALE_STATEID
:
2659 case -NFS4ERR_EXPIRED
:
2660 nfs4_schedule_state_recovery(server
->nfs4_state
);
2664 err
= nfs4_handle_exception(server
, err
, &exception
);
2665 } while (exception
.retry
);
2669 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2670 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2673 * sleep, with exponential backoff, and retry the LOCK operation.
2675 static unsigned long
2676 nfs4_set_lock_task_retry(unsigned long timeout
)
2678 schedule_timeout_interruptible(timeout
);
2680 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
2681 return NFS4_LOCK_MAXTIMEOUT
;
2686 nfs4_lck_type(int cmd
, struct file_lock
*request
)
2689 switch (request
->fl_type
) {
2691 return IS_SETLKW(cmd
) ? NFS4_READW_LT
: NFS4_READ_LT
;
2693 return IS_SETLKW(cmd
) ? NFS4_WRITEW_LT
: NFS4_WRITE_LT
;
2695 return NFS4_WRITE_LT
;
2701 static inline uint64_t
2702 nfs4_lck_length(struct file_lock
*request
)
2704 if (request
->fl_end
== OFFSET_MAX
)
2705 return ~(uint64_t)0;
2706 return request
->fl_end
- request
->fl_start
+ 1;
2709 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
2711 struct inode
*inode
= state
->inode
;
2712 struct nfs_server
*server
= NFS_SERVER(inode
);
2713 struct nfs4_client
*clp
= server
->nfs4_state
;
2714 struct nfs_lockargs arg
= {
2715 .fh
= NFS_FH(inode
),
2716 .type
= nfs4_lck_type(cmd
, request
),
2717 .offset
= request
->fl_start
,
2718 .length
= nfs4_lck_length(request
),
2720 struct nfs_lockres res
= {
2723 struct rpc_message msg
= {
2724 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
2727 .rpc_cred
= state
->owner
->so_cred
,
2729 struct nfs_lowner nlo
;
2730 struct nfs4_lock_state
*lsp
;
2733 down_read(&clp
->cl_sem
);
2734 nlo
.clientid
= clp
->cl_clientid
;
2735 status
= nfs4_set_lock_state(state
, request
);
2738 lsp
= request
->fl_u
.nfs4_fl
.owner
;
2739 nlo
.id
= lsp
->ls_id
;
2741 status
= rpc_call_sync(server
->client
, &msg
, 0);
2743 request
->fl_type
= F_UNLCK
;
2744 } else if (status
== -NFS4ERR_DENIED
) {
2745 int64_t len
, start
, end
;
2746 start
= res
.u
.denied
.offset
;
2747 len
= res
.u
.denied
.length
;
2748 end
= start
+ len
- 1;
2749 if (end
< 0 || len
== 0)
2750 request
->fl_end
= OFFSET_MAX
;
2752 request
->fl_end
= (loff_t
)end
;
2753 request
->fl_start
= (loff_t
)start
;
2754 request
->fl_type
= F_WRLCK
;
2755 if (res
.u
.denied
.type
& 1)
2756 request
->fl_type
= F_RDLCK
;
2757 request
->fl_pid
= 0;
2761 up_read(&clp
->cl_sem
);
2765 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
2767 struct nfs4_exception exception
= { };
2771 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
2772 _nfs4_proc_getlk(state
, cmd
, request
),
2774 } while (exception
.retry
);
2778 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
2781 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
2783 res
= posix_lock_file_wait(file
, fl
);
2786 res
= flock_lock_file_wait(file
, fl
);
2792 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n",
2797 struct nfs4_unlockdata
{
2798 struct nfs_lockargs arg
;
2799 struct nfs_locku_opargs luargs
;
2800 struct nfs_lockres res
;
2801 struct nfs4_lock_state
*lsp
;
2802 struct nfs_open_context
*ctx
;
2804 struct completion completion
;
2807 static void nfs4_locku_release_calldata(struct nfs4_unlockdata
*calldata
)
2809 if (atomic_dec_and_test(&calldata
->refcount
)) {
2810 nfs_free_seqid(calldata
->luargs
.seqid
);
2811 nfs4_put_lock_state(calldata
->lsp
);
2812 put_nfs_open_context(calldata
->ctx
);
2817 static void nfs4_locku_complete(struct nfs4_unlockdata
*calldata
)
2819 complete(&calldata
->completion
);
2820 nfs4_locku_release_calldata(calldata
);
2823 static void nfs4_locku_done(struct rpc_task
*task
)
2825 struct nfs4_unlockdata
*calldata
= (struct nfs4_unlockdata
*)task
->tk_calldata
;
2827 nfs_increment_lock_seqid(task
->tk_status
, calldata
->luargs
.seqid
);
2828 switch (task
->tk_status
) {
2830 memcpy(calldata
->lsp
->ls_stateid
.data
,
2831 calldata
->res
.u
.stateid
.data
,
2832 sizeof(calldata
->lsp
->ls_stateid
.data
));
2834 case -NFS4ERR_STALE_STATEID
:
2835 case -NFS4ERR_EXPIRED
:
2836 nfs4_schedule_state_recovery(calldata
->res
.server
->nfs4_state
);
2839 if (nfs4_async_handle_error(task
, calldata
->res
.server
) == -EAGAIN
) {
2840 rpc_restart_call(task
);
2844 nfs4_locku_complete(calldata
);
2847 static void nfs4_locku_begin(struct rpc_task
*task
)
2849 struct nfs4_unlockdata
*calldata
= (struct nfs4_unlockdata
*)task
->tk_calldata
;
2850 struct rpc_message msg
= {
2851 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
2852 .rpc_argp
= &calldata
->arg
,
2853 .rpc_resp
= &calldata
->res
,
2854 .rpc_cred
= calldata
->lsp
->ls_state
->owner
->so_cred
,
2858 status
= nfs_wait_on_sequence(calldata
->luargs
.seqid
, task
);
2861 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
2862 nfs4_locku_complete(calldata
);
2863 task
->tk_exit
= NULL
;
2867 rpc_call_setup(task
, &msg
, 0);
2870 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
2872 struct nfs4_unlockdata
*calldata
;
2873 struct inode
*inode
= state
->inode
;
2874 struct nfs_server
*server
= NFS_SERVER(inode
);
2875 struct nfs4_lock_state
*lsp
;
2878 status
= nfs4_set_lock_state(state
, request
);
2881 lsp
= request
->fl_u
.nfs4_fl
.owner
;
2882 /* We might have lost the locks! */
2883 if ((lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0)
2885 calldata
= kmalloc(sizeof(*calldata
), GFP_KERNEL
);
2886 if (calldata
== NULL
)
2888 calldata
->luargs
.seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
2889 if (calldata
->luargs
.seqid
== NULL
) {
2893 calldata
->luargs
.stateid
= &lsp
->ls_stateid
;
2894 calldata
->arg
.fh
= NFS_FH(inode
);
2895 calldata
->arg
.type
= nfs4_lck_type(cmd
, request
);
2896 calldata
->arg
.offset
= request
->fl_start
;
2897 calldata
->arg
.length
= nfs4_lck_length(request
);
2898 calldata
->arg
.u
.locku
= &calldata
->luargs
;
2899 calldata
->res
.server
= server
;
2900 calldata
->lsp
= lsp
;
2901 atomic_inc(&lsp
->ls_count
);
2903 /* Ensure we don't close file until we're done freeing locks! */
2904 calldata
->ctx
= get_nfs_open_context((struct nfs_open_context
*)request
->fl_file
->private_data
);
2906 atomic_set(&calldata
->refcount
, 2);
2907 init_completion(&calldata
->completion
);
2909 status
= nfs4_call_async(NFS_SERVER(inode
)->client
, nfs4_locku_begin
,
2910 nfs4_locku_done
, calldata
);
2912 wait_for_completion_interruptible(&calldata
->completion
);
2913 do_vfs_lock(request
->fl_file
, request
);
2914 nfs4_locku_release_calldata(calldata
);
2918 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
, int reclaim
)
2920 struct inode
*inode
= state
->inode
;
2921 struct nfs_server
*server
= NFS_SERVER(inode
);
2922 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
2923 struct nfs_lock_opargs largs
= {
2924 .lock_stateid
= &lsp
->ls_stateid
,
2925 .open_stateid
= &state
->stateid
,
2927 .clientid
= server
->nfs4_state
->cl_clientid
,
2932 struct nfs_lockargs arg
= {
2933 .fh
= NFS_FH(inode
),
2934 .type
= nfs4_lck_type(cmd
, request
),
2935 .offset
= request
->fl_start
,
2936 .length
= nfs4_lck_length(request
),
2941 struct nfs_lockres res
= {
2944 struct rpc_message msg
= {
2945 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
2948 .rpc_cred
= state
->owner
->so_cred
,
2950 int status
= -ENOMEM
;
2952 largs
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
2953 if (largs
.lock_seqid
== NULL
)
2955 if (!(lsp
->ls_seqid
.flags
& NFS_SEQID_CONFIRMED
)) {
2956 struct nfs4_state_owner
*owner
= state
->owner
;
2958 largs
.open_seqid
= nfs_alloc_seqid(&owner
->so_seqid
);
2959 if (largs
.open_seqid
== NULL
)
2961 largs
.new_lock_owner
= 1;
2962 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
2963 /* increment open seqid on success, and seqid mutating errors */
2964 if (largs
.new_lock_owner
!= 0) {
2965 nfs_increment_open_seqid(status
, largs
.open_seqid
);
2967 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
2969 nfs_free_seqid(largs
.open_seqid
);
2971 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
2972 /* increment lock seqid on success, and seqid mutating errors*/
2973 nfs_increment_lock_seqid(status
, largs
.lock_seqid
);
2974 /* save the returned stateid. */
2976 memcpy(lsp
->ls_stateid
.data
, res
.u
.stateid
.data
,
2977 sizeof(lsp
->ls_stateid
.data
));
2978 lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
2979 } else if (status
== -NFS4ERR_DENIED
)
2982 nfs_free_seqid(largs
.lock_seqid
);
2986 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
2988 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2989 struct nfs4_exception exception
= { };
2993 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 1);
2994 if (err
!= -NFS4ERR_DELAY
)
2996 nfs4_handle_exception(server
, err
, &exception
);
2997 } while (exception
.retry
);
3001 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
3003 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3004 struct nfs4_exception exception
= { };
3008 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 0);
3009 if (err
!= -NFS4ERR_DELAY
)
3011 nfs4_handle_exception(server
, err
, &exception
);
3012 } while (exception
.retry
);
3016 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3018 struct nfs4_client
*clp
= state
->owner
->so_client
;
3021 down_read(&clp
->cl_sem
);
3022 status
= nfs4_set_lock_state(state
, request
);
3024 status
= _nfs4_do_setlk(state
, cmd
, request
, 0);
3026 /* Note: we always want to sleep here! */
3027 request
->fl_flags
|= FL_SLEEP
;
3028 if (do_vfs_lock(request
->fl_file
, request
) < 0)
3029 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __FUNCTION__
);
3031 up_read(&clp
->cl_sem
);
3035 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3037 struct nfs4_exception exception
= { };
3041 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3042 _nfs4_proc_setlk(state
, cmd
, request
),
3044 } while (exception
.retry
);
3049 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
3051 struct nfs_open_context
*ctx
;
3052 struct nfs4_state
*state
;
3053 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
3056 /* verify open state */
3057 ctx
= (struct nfs_open_context
*)filp
->private_data
;
3060 if (request
->fl_start
< 0 || request
->fl_end
< 0)
3064 return nfs4_proc_getlk(state
, F_GETLK
, request
);
3066 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
3069 if (request
->fl_type
== F_UNLCK
)
3070 return nfs4_proc_unlck(state
, cmd
, request
);
3073 status
= nfs4_proc_setlk(state
, cmd
, request
);
3074 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
3076 timeout
= nfs4_set_lock_task_retry(timeout
);
3077 status
= -ERESTARTSYS
;
3080 } while(status
< 0);
3085 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3087 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
3088 size_t buflen
, int flags
)
3090 struct inode
*inode
= dentry
->d_inode
;
3092 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3095 if (!S_ISREG(inode
->i_mode
) &&
3096 (!S_ISDIR(inode
->i_mode
) || inode
->i_mode
& S_ISVTX
))
3099 return nfs4_proc_set_acl(inode
, buf
, buflen
);
3102 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3103 * and that's what we'll do for e.g. user attributes that haven't been set.
3104 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3105 * attributes in kernel-managed attribute namespaces. */
3106 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
3109 struct inode
*inode
= dentry
->d_inode
;
3111 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3114 return nfs4_proc_get_acl(inode
, buf
, buflen
);
3117 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
3119 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
3121 if (buf
&& buflen
< len
)
3124 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
3128 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops
= {
3129 .recover_open
= nfs4_open_reclaim
,
3130 .recover_lock
= nfs4_lock_reclaim
,
3133 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops
= {
3134 .recover_open
= nfs4_open_expired
,
3135 .recover_lock
= nfs4_lock_expired
,
3138 static struct inode_operations nfs4_file_inode_operations
= {
3139 .permission
= nfs_permission
,
3140 .getattr
= nfs_getattr
,
3141 .setattr
= nfs_setattr
,
3142 .getxattr
= nfs4_getxattr
,
3143 .setxattr
= nfs4_setxattr
,
3144 .listxattr
= nfs4_listxattr
,
3147 struct nfs_rpc_ops nfs_v4_clientops
= {
3148 .version
= 4, /* protocol version */
3149 .dentry_ops
= &nfs4_dentry_operations
,
3150 .dir_inode_ops
= &nfs4_dir_inode_operations
,
3151 .file_inode_ops
= &nfs4_file_inode_operations
,
3152 .getroot
= nfs4_proc_get_root
,
3153 .getattr
= nfs4_proc_getattr
,
3154 .setattr
= nfs4_proc_setattr
,
3155 .lookup
= nfs4_proc_lookup
,
3156 .access
= nfs4_proc_access
,
3157 .readlink
= nfs4_proc_readlink
,
3158 .read
= nfs4_proc_read
,
3159 .write
= nfs4_proc_write
,
3160 .commit
= nfs4_proc_commit
,
3161 .create
= nfs4_proc_create
,
3162 .remove
= nfs4_proc_remove
,
3163 .unlink_setup
= nfs4_proc_unlink_setup
,
3164 .unlink_done
= nfs4_proc_unlink_done
,
3165 .rename
= nfs4_proc_rename
,
3166 .link
= nfs4_proc_link
,
3167 .symlink
= nfs4_proc_symlink
,
3168 .mkdir
= nfs4_proc_mkdir
,
3169 .rmdir
= nfs4_proc_remove
,
3170 .readdir
= nfs4_proc_readdir
,
3171 .mknod
= nfs4_proc_mknod
,
3172 .statfs
= nfs4_proc_statfs
,
3173 .fsinfo
= nfs4_proc_fsinfo
,
3174 .pathconf
= nfs4_proc_pathconf
,
3175 .decode_dirent
= nfs4_decode_dirent
,
3176 .read_setup
= nfs4_proc_read_setup
,
3177 .write_setup
= nfs4_proc_write_setup
,
3178 .commit_setup
= nfs4_proc_commit_setup
,
3179 .file_open
= nfs_open
,
3180 .file_release
= nfs_release
,
3181 .lock
= nfs4_proc_lock
,
3182 .clear_acl_cache
= nfs4_zap_acl_attr
,