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>
52 #include "delegation.h"
54 #define NFSDBG_FACILITY NFSDBG_PROC
56 #define NFS4_POLL_RETRY_MIN (1*HZ)
57 #define NFS4_POLL_RETRY_MAX (15*HZ)
59 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
);
60 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
61 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*);
62 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
);
63 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
);
64 extern u32
*nfs4_decode_dirent(u32
*p
, struct nfs_entry
*entry
, int plus
);
65 extern struct rpc_procinfo nfs4_procedures
[];
67 /* Prevent leaks of NFSv4 errors into userland */
68 int nfs4_map_errors(int err
)
71 dprintk("%s could not handle NFSv4 error %d\n",
79 * This is our standard bitmap for GETATTR requests.
81 const u32 nfs4_fattr_bitmap
[2] = {
86 | FATTR4_WORD0_FILEID
,
88 | FATTR4_WORD1_NUMLINKS
90 | FATTR4_WORD1_OWNER_GROUP
92 | FATTR4_WORD1_SPACE_USED
93 | FATTR4_WORD1_TIME_ACCESS
94 | FATTR4_WORD1_TIME_METADATA
95 | FATTR4_WORD1_TIME_MODIFY
98 const u32 nfs4_statfs_bitmap
[2] = {
99 FATTR4_WORD0_FILES_AVAIL
100 | FATTR4_WORD0_FILES_FREE
101 | FATTR4_WORD0_FILES_TOTAL
,
102 FATTR4_WORD1_SPACE_AVAIL
103 | FATTR4_WORD1_SPACE_FREE
104 | FATTR4_WORD1_SPACE_TOTAL
107 const u32 nfs4_pathconf_bitmap
[2] = {
109 | FATTR4_WORD0_MAXNAME
,
113 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
114 | FATTR4_WORD0_MAXREAD
115 | FATTR4_WORD0_MAXWRITE
116 | FATTR4_WORD0_LEASE_TIME
,
120 static void nfs4_setup_readdir(u64 cookie
, u32
*verifier
, struct dentry
*dentry
,
121 struct nfs4_readdir_arg
*readdir
)
125 BUG_ON(readdir
->count
< 80);
127 readdir
->cookie
= cookie
;
128 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
133 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
138 * NFSv4 servers do not return entries for '.' and '..'
139 * Therefore, we fake these entries here. We let '.'
140 * have cookie 0 and '..' have cookie 1. Note that
141 * when talking to the server, we always send cookie 0
144 start
= p
= (u32
*)kmap_atomic(*readdir
->pages
, KM_USER0
);
147 *p
++ = xdr_one
; /* next */
148 *p
++ = xdr_zero
; /* cookie, first word */
149 *p
++ = xdr_one
; /* cookie, second word */
150 *p
++ = xdr_one
; /* entry len */
151 memcpy(p
, ".\0\0\0", 4); /* entry */
153 *p
++ = xdr_one
; /* bitmap length */
154 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
155 *p
++ = htonl(8); /* attribute buffer length */
156 p
= xdr_encode_hyper(p
, dentry
->d_inode
->i_ino
);
159 *p
++ = xdr_one
; /* next */
160 *p
++ = xdr_zero
; /* cookie, first word */
161 *p
++ = xdr_two
; /* cookie, second word */
162 *p
++ = xdr_two
; /* entry len */
163 memcpy(p
, "..\0\0", 4); /* entry */
165 *p
++ = xdr_one
; /* bitmap length */
166 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
167 *p
++ = htonl(8); /* attribute buffer length */
168 p
= xdr_encode_hyper(p
, dentry
->d_parent
->d_inode
->i_ino
);
170 readdir
->pgbase
= (char *)p
- (char *)start
;
171 readdir
->count
-= readdir
->pgbase
;
172 kunmap_atomic(start
, KM_USER0
);
176 renew_lease(struct nfs_server
*server
, unsigned long timestamp
)
178 struct nfs4_client
*clp
= server
->nfs4_state
;
179 spin_lock(&clp
->cl_lock
);
180 if (time_before(clp
->cl_last_renewal
,timestamp
))
181 clp
->cl_last_renewal
= timestamp
;
182 spin_unlock(&clp
->cl_lock
);
185 static void update_changeattr(struct inode
*inode
, struct nfs4_change_info
*cinfo
)
187 struct nfs_inode
*nfsi
= NFS_I(inode
);
189 if (cinfo
->before
== nfsi
->change_attr
&& cinfo
->atomic
)
190 nfsi
->change_attr
= cinfo
->after
;
193 /* Helper for asynchronous RPC calls */
194 static int nfs4_call_async(struct rpc_clnt
*clnt
, rpc_action tk_begin
,
195 rpc_action tk_exit
, void *calldata
)
197 struct rpc_task
*task
;
199 if (!(task
= rpc_new_task(clnt
, tk_exit
, RPC_TASK_ASYNC
)))
202 task
->tk_calldata
= calldata
;
203 task
->tk_action
= tk_begin
;
208 static void update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, int open_flags
)
210 struct inode
*inode
= state
->inode
;
212 open_flags
&= (FMODE_READ
|FMODE_WRITE
);
213 /* Protect against nfs4_find_state() */
214 spin_lock(&inode
->i_lock
);
215 state
->state
|= open_flags
;
216 /* NB! List reordering - see the reclaim code for why. */
217 if ((open_flags
& FMODE_WRITE
) && 0 == state
->nwriters
++)
218 list_move(&state
->open_states
, &state
->owner
->so_states
);
219 if (open_flags
& FMODE_READ
)
221 memcpy(&state
->stateid
, stateid
, sizeof(state
->stateid
));
222 spin_unlock(&inode
->i_lock
);
227 * reclaim state on the server after a reboot.
229 static int _nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
231 struct inode
*inode
= state
->inode
;
232 struct nfs_server
*server
= NFS_SERVER(inode
);
233 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
234 struct nfs_openargs o_arg
= {
237 .open_flags
= state
->state
,
238 .clientid
= server
->nfs4_state
->cl_clientid
,
239 .claim
= NFS4_OPEN_CLAIM_PREVIOUS
,
240 .bitmask
= server
->attr_bitmask
,
242 struct nfs_openres o_res
= {
243 .server
= server
, /* Grrr */
245 struct rpc_message msg
= {
246 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
],
249 .rpc_cred
= sp
->so_cred
,
253 if (delegation
!= NULL
) {
254 if (!(delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
)) {
255 memcpy(&state
->stateid
, &delegation
->stateid
,
256 sizeof(state
->stateid
));
257 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
260 o_arg
.u
.delegation_type
= delegation
->type
;
262 o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
263 if (o_arg
.seqid
== NULL
)
265 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
266 /* Confirm the sequence as being established */
267 nfs_confirm_seqid(&sp
->so_seqid
, status
);
268 nfs_increment_open_seqid(status
, o_arg
.seqid
);
270 memcpy(&state
->stateid
, &o_res
.stateid
, sizeof(state
->stateid
));
271 if (o_res
.delegation_type
!= 0) {
272 nfs_inode_reclaim_delegation(inode
, sp
->so_cred
, &o_res
);
273 /* Did the server issue an immediate delegation recall? */
275 nfs_async_inode_return_delegation(inode
, &o_res
.stateid
);
278 nfs_free_seqid(o_arg
.seqid
);
279 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
280 /* Ensure we update the inode attributes */
285 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
287 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
288 struct nfs4_exception exception
= { };
291 err
= _nfs4_open_reclaim(sp
, state
);
292 if (err
!= -NFS4ERR_DELAY
)
294 nfs4_handle_exception(server
, err
, &exception
);
295 } while (exception
.retry
);
299 static int _nfs4_open_delegation_recall(struct dentry
*dentry
, struct nfs4_state
*state
)
301 struct nfs4_state_owner
*sp
= state
->owner
;
302 struct inode
*inode
= dentry
->d_inode
;
303 struct nfs_server
*server
= NFS_SERVER(inode
);
304 struct dentry
*parent
= dget_parent(dentry
);
305 struct nfs_openargs arg
= {
306 .fh
= NFS_FH(parent
->d_inode
),
307 .clientid
= server
->nfs4_state
->cl_clientid
,
308 .name
= &dentry
->d_name
,
311 .bitmask
= server
->attr_bitmask
,
312 .claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
,
314 struct nfs_openres res
= {
317 struct rpc_message msg
= {
318 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
],
321 .rpc_cred
= sp
->so_cred
,
325 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
327 if (state
->state
== 0)
329 arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
331 if (arg
.seqid
== NULL
)
333 arg
.open_flags
= state
->state
;
334 memcpy(arg
.u
.delegation
.data
, state
->stateid
.data
, sizeof(arg
.u
.delegation
.data
));
335 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
336 nfs_increment_open_seqid(status
, arg
.seqid
);
339 if(res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
340 status
= _nfs4_proc_open_confirm(server
->client
, NFS_FH(inode
),
341 sp
, &res
.stateid
, arg
.seqid
);
345 nfs_confirm_seqid(&sp
->so_seqid
, 0);
347 memcpy(state
->stateid
.data
, res
.stateid
.data
,
348 sizeof(state
->stateid
.data
));
349 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
352 nfs_free_seqid(arg
.seqid
);
358 int nfs4_open_delegation_recall(struct dentry
*dentry
, struct nfs4_state
*state
)
360 struct nfs4_exception exception
= { };
361 struct nfs_server
*server
= NFS_SERVER(dentry
->d_inode
);
364 err
= _nfs4_open_delegation_recall(dentry
, state
);
368 case -NFS4ERR_STALE_CLIENTID
:
369 case -NFS4ERR_STALE_STATEID
:
370 case -NFS4ERR_EXPIRED
:
371 /* Don't recall a delegation if it was lost */
372 nfs4_schedule_state_recovery(server
->nfs4_state
);
375 err
= nfs4_handle_exception(server
, err
, &exception
);
376 } while (exception
.retry
);
380 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
)
382 struct nfs_open_confirmargs arg
= {
387 struct nfs_open_confirmres res
;
388 struct rpc_message msg
= {
389 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
392 .rpc_cred
= sp
->so_cred
,
396 status
= rpc_call_sync(clnt
, &msg
, RPC_TASK_NOINTR
);
397 /* Confirm the sequence as being established */
398 nfs_confirm_seqid(&sp
->so_seqid
, status
);
399 nfs_increment_open_seqid(status
, seqid
);
401 memcpy(stateid
, &res
.stateid
, sizeof(*stateid
));
405 static int _nfs4_proc_open(struct inode
*dir
, struct nfs4_state_owner
*sp
, struct nfs_openargs
*o_arg
, struct nfs_openres
*o_res
)
407 struct nfs_server
*server
= NFS_SERVER(dir
);
408 struct rpc_message msg
= {
409 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
412 .rpc_cred
= sp
->so_cred
,
416 /* Update sequence id. The caller must serialize! */
417 o_arg
->id
= sp
->so_id
;
418 o_arg
->clientid
= sp
->so_client
->cl_clientid
;
420 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
421 nfs_increment_open_seqid(status
, o_arg
->seqid
);
424 update_changeattr(dir
, &o_res
->cinfo
);
425 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
426 status
= _nfs4_proc_open_confirm(server
->client
, &o_res
->fh
,
427 sp
, &o_res
->stateid
, o_arg
->seqid
);
431 nfs_confirm_seqid(&sp
->so_seqid
, 0);
432 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
433 status
= server
->rpc_ops
->getattr(server
, &o_res
->fh
, o_res
->f_attr
);
438 static int _nfs4_do_access(struct inode
*inode
, struct rpc_cred
*cred
, int openflags
)
440 struct nfs_access_entry cache
;
444 if (openflags
& FMODE_READ
)
446 if (openflags
& FMODE_WRITE
)
448 status
= nfs_access_get_cached(inode
, cred
, &cache
);
452 /* Be clever: ask server to check for all possible rights */
453 cache
.mask
= MAY_EXEC
| MAY_WRITE
| MAY_READ
;
455 cache
.jiffies
= jiffies
;
456 status
= _nfs4_proc_access(inode
, &cache
);
459 nfs_access_add_cache(inode
, &cache
);
461 if ((cache
.mask
& mask
) == mask
)
468 * reclaim state on the server after a network partition.
469 * Assumes caller holds the appropriate lock
471 static int _nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
, struct dentry
*dentry
)
473 struct dentry
*parent
= dget_parent(dentry
);
474 struct inode
*dir
= parent
->d_inode
;
475 struct inode
*inode
= state
->inode
;
476 struct nfs_server
*server
= NFS_SERVER(dir
);
477 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
478 struct nfs_fattr f_attr
= {
481 struct nfs_openargs o_arg
= {
483 .open_flags
= state
->state
,
484 .name
= &dentry
->d_name
,
485 .bitmask
= server
->attr_bitmask
,
486 .claim
= NFS4_OPEN_CLAIM_NULL
,
488 struct nfs_openres o_res
= {
494 if (delegation
!= NULL
&& !(delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
)) {
495 status
= _nfs4_do_access(inode
, sp
->so_cred
, state
->state
);
498 memcpy(&state
->stateid
, &delegation
->stateid
, sizeof(state
->stateid
));
499 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
502 o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
504 if (o_arg
.seqid
== NULL
)
506 status
= _nfs4_proc_open(dir
, sp
, &o_arg
, &o_res
);
509 /* Check if files differ */
510 if ((f_attr
.mode
& S_IFMT
) != (inode
->i_mode
& S_IFMT
))
512 /* Has the file handle changed? */
513 if (nfs_compare_fh(&o_res
.fh
, NFS_FH(inode
)) != 0) {
514 /* Verify if the change attributes are the same */
515 if (f_attr
.change_attr
!= NFS_I(inode
)->change_attr
)
517 if (nfs_size_to_loff_t(f_attr
.size
) != inode
->i_size
)
519 /* Lets just pretend that this is the same file */
520 nfs_copy_fh(NFS_FH(inode
), &o_res
.fh
);
521 NFS_I(inode
)->fileid
= f_attr
.fileid
;
523 memcpy(&state
->stateid
, &o_res
.stateid
, sizeof(state
->stateid
));
524 if (o_res
.delegation_type
!= 0) {
525 if (!(delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
))
526 nfs_inode_set_delegation(inode
, sp
->so_cred
, &o_res
);
528 nfs_inode_reclaim_delegation(inode
, sp
->so_cred
, &o_res
);
531 nfs_free_seqid(o_arg
.seqid
);
532 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
538 /* Invalidate the state owner so we don't ever use it again */
539 nfs4_drop_state_owner(sp
);
541 /* Should we be trying to close that stateid? */
545 static inline int nfs4_do_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
, struct dentry
*dentry
)
547 struct nfs_server
*server
= NFS_SERVER(dentry
->d_inode
);
548 struct nfs4_exception exception
= { };
552 err
= _nfs4_open_expired(sp
, state
, dentry
);
553 if (err
== -NFS4ERR_DELAY
)
554 nfs4_handle_exception(server
, err
, &exception
);
555 } while (exception
.retry
);
559 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
561 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
562 struct nfs_open_context
*ctx
;
565 spin_lock(&state
->inode
->i_lock
);
566 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
567 if (ctx
->state
!= state
)
569 get_nfs_open_context(ctx
);
570 spin_unlock(&state
->inode
->i_lock
);
571 status
= nfs4_do_open_expired(sp
, state
, ctx
->dentry
);
572 put_nfs_open_context(ctx
);
575 spin_unlock(&state
->inode
->i_lock
);
580 * Returns an nfs4_state + an extra reference to the inode
582 static int _nfs4_open_delegated(struct inode
*inode
, int flags
, struct rpc_cred
*cred
, struct nfs4_state
**res
)
584 struct nfs_delegation
*delegation
;
585 struct nfs_server
*server
= NFS_SERVER(inode
);
586 struct nfs4_client
*clp
= server
->nfs4_state
;
587 struct nfs_inode
*nfsi
= NFS_I(inode
);
588 struct nfs4_state_owner
*sp
= NULL
;
589 struct nfs4_state
*state
= NULL
;
590 int open_flags
= flags
& (FMODE_READ
|FMODE_WRITE
);
593 /* Protect against reboot recovery - NOTE ORDER! */
594 down_read(&clp
->cl_sem
);
595 /* Protect against delegation recall */
596 down_read(&nfsi
->rwsem
);
597 delegation
= NFS_I(inode
)->delegation
;
599 if (delegation
== NULL
|| (delegation
->type
& open_flags
) != open_flags
)
602 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
603 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__
);
606 state
= nfs4_get_open_state(inode
, sp
);
611 if ((state
->state
& open_flags
) == open_flags
) {
612 spin_lock(&inode
->i_lock
);
613 if (open_flags
& FMODE_READ
)
615 if (open_flags
& FMODE_WRITE
)
617 spin_unlock(&inode
->i_lock
);
619 } else if (state
->state
!= 0)
623 err
= _nfs4_do_access(inode
, cred
, open_flags
);
627 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
628 update_open_stateid(state
, &delegation
->stateid
, open_flags
);
630 nfs4_put_state_owner(sp
);
631 up_read(&nfsi
->rwsem
);
632 up_read(&clp
->cl_sem
);
639 nfs4_put_open_state(state
);
640 nfs4_put_state_owner(sp
);
642 up_read(&nfsi
->rwsem
);
643 up_read(&clp
->cl_sem
);
647 static struct nfs4_state
*nfs4_open_delegated(struct inode
*inode
, int flags
, struct rpc_cred
*cred
)
649 struct nfs4_exception exception
= { };
650 struct nfs4_state
*res
;
654 err
= _nfs4_open_delegated(inode
, flags
, cred
, &res
);
657 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode
),
659 } while (exception
.retry
);
664 * Returns an nfs4_state + an referenced inode
666 static int _nfs4_do_open(struct inode
*dir
, struct dentry
*dentry
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
, struct nfs4_state
**res
)
668 struct nfs4_state_owner
*sp
;
669 struct nfs4_state
*state
= NULL
;
670 struct nfs_server
*server
= NFS_SERVER(dir
);
671 struct nfs4_client
*clp
= server
->nfs4_state
;
672 struct inode
*inode
= NULL
;
674 struct nfs_fattr f_attr
= {
677 struct nfs_openargs o_arg
= {
680 .name
= &dentry
->d_name
,
682 .bitmask
= server
->attr_bitmask
,
683 .claim
= NFS4_OPEN_CLAIM_NULL
,
685 struct nfs_openres o_res
= {
690 /* Protect against reboot recovery conflicts */
691 down_read(&clp
->cl_sem
);
693 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
694 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
697 if (flags
& O_EXCL
) {
698 u32
*p
= (u32
*) o_arg
.u
.verifier
.data
;
702 o_arg
.u
.attrs
= sattr
;
703 /* Serialization for the sequence id */
705 o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
706 if (o_arg
.seqid
== NULL
)
708 status
= _nfs4_proc_open(dir
, sp
, &o_arg
, &o_res
);
713 inode
= nfs_fhget(dir
->i_sb
, &o_res
.fh
, &f_attr
);
716 state
= nfs4_get_open_state(inode
, sp
);
719 update_open_stateid(state
, &o_res
.stateid
, flags
);
720 if (o_res
.delegation_type
!= 0)
721 nfs_inode_set_delegation(inode
, cred
, &o_res
);
722 nfs_free_seqid(o_arg
.seqid
);
723 nfs4_put_state_owner(sp
);
724 up_read(&clp
->cl_sem
);
730 nfs4_put_open_state(state
);
731 nfs_free_seqid(o_arg
.seqid
);
732 nfs4_put_state_owner(sp
);
734 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
735 up_read(&clp
->cl_sem
);
743 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
, struct dentry
*dentry
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
)
745 struct nfs4_exception exception
= { };
746 struct nfs4_state
*res
;
750 status
= _nfs4_do_open(dir
, dentry
, flags
, sattr
, cred
, &res
);
753 /* NOTE: BAD_SEQID means the server and client disagree about the
754 * book-keeping w.r.t. state-changing operations
755 * (OPEN/CLOSE/LOCK/LOCKU...)
756 * It is actually a sign of a bug on the client or on the server.
758 * If we receive a BAD_SEQID error in the particular case of
759 * doing an OPEN, we assume that nfs_increment_open_seqid() will
760 * have unhashed the old state_owner for us, and that we can
761 * therefore safely retry using a new one. We should still warn
764 if (status
== -NFS4ERR_BAD_SEQID
) {
765 printk(KERN_WARNING
"NFS: v4 server returned a bad sequence-id error!\n");
769 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
770 status
, &exception
));
771 } while (exception
.retry
);
775 static int _nfs4_do_setattr(struct nfs_server
*server
, struct nfs_fattr
*fattr
,
776 struct nfs_fh
*fhandle
, struct iattr
*sattr
,
777 struct nfs4_state
*state
)
779 struct nfs_setattrargs arg
= {
783 .bitmask
= server
->attr_bitmask
,
785 struct nfs_setattrres res
= {
789 struct rpc_message msg
= {
790 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
799 msg
.rpc_cred
= state
->owner
->so_cred
;
800 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
802 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
804 status
= rpc_call_sync(server
->client
, &msg
, 0);
808 static int nfs4_do_setattr(struct nfs_server
*server
, struct nfs_fattr
*fattr
,
809 struct nfs_fh
*fhandle
, struct iattr
*sattr
,
810 struct nfs4_state
*state
)
812 struct nfs4_exception exception
= { };
815 err
= nfs4_handle_exception(server
,
816 _nfs4_do_setattr(server
, fattr
, fhandle
, sattr
,
819 } while (exception
.retry
);
823 struct nfs4_closedata
{
825 struct nfs4_state
*state
;
826 struct nfs_closeargs arg
;
827 struct nfs_closeres res
;
830 static void nfs4_free_closedata(struct nfs4_closedata
*calldata
)
832 struct nfs4_state
*state
= calldata
->state
;
833 struct nfs4_state_owner
*sp
= state
->owner
;
835 nfs4_put_open_state(calldata
->state
);
836 nfs_free_seqid(calldata
->arg
.seqid
);
837 nfs4_put_state_owner(sp
);
841 static void nfs4_close_done(struct rpc_task
*task
)
843 struct nfs4_closedata
*calldata
= (struct nfs4_closedata
*)task
->tk_calldata
;
844 struct nfs4_state
*state
= calldata
->state
;
845 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
847 /* hmm. we are done with the inode, and in the process of freeing
848 * the state_owner. we keep this around to process errors
850 nfs_increment_open_seqid(task
->tk_status
, calldata
->arg
.seqid
);
851 switch (task
->tk_status
) {
853 memcpy(&state
->stateid
, &calldata
->res
.stateid
,
854 sizeof(state
->stateid
));
856 case -NFS4ERR_STALE_STATEID
:
857 case -NFS4ERR_EXPIRED
:
858 state
->state
= calldata
->arg
.open_flags
;
859 nfs4_schedule_state_recovery(server
->nfs4_state
);
862 if (nfs4_async_handle_error(task
, server
) == -EAGAIN
) {
863 rpc_restart_call(task
);
867 state
->state
= calldata
->arg
.open_flags
;
868 nfs4_free_closedata(calldata
);
871 static void nfs4_close_begin(struct rpc_task
*task
)
873 struct nfs4_closedata
*calldata
= (struct nfs4_closedata
*)task
->tk_calldata
;
874 struct nfs4_state
*state
= calldata
->state
;
875 struct rpc_message msg
= {
876 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
877 .rpc_argp
= &calldata
->arg
,
878 .rpc_resp
= &calldata
->res
,
879 .rpc_cred
= state
->owner
->so_cred
,
884 status
= nfs_wait_on_sequence(calldata
->arg
.seqid
, task
);
887 /* Don't reorder reads */
889 /* Recalculate the new open mode in case someone reopened the file
890 * while we were waiting in line to be scheduled.
892 if (state
->nreaders
!= 0)
894 if (state
->nwriters
!= 0)
896 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
898 if (mode
== state
->state
) {
899 nfs4_free_closedata(calldata
);
900 task
->tk_exit
= NULL
;
905 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
906 calldata
->arg
.open_flags
= mode
;
907 rpc_call_setup(task
, &msg
, 0);
911 * It is possible for data to be read/written from a mem-mapped file
912 * after the sys_close call (which hits the vfs layer as a flush).
913 * This means that we can't safely call nfsv4 close on a file until
914 * the inode is cleared. This in turn means that we are not good
915 * NFSv4 citizens - we do not indicate to the server to update the file's
916 * share state even when we are done with one of the three share
917 * stateid's in the inode.
919 * NOTE: Caller must be holding the sp->so_owner semaphore!
921 int nfs4_do_close(struct inode
*inode
, struct nfs4_state
*state
, mode_t mode
)
923 struct nfs4_closedata
*calldata
;
924 int status
= -ENOMEM
;
926 calldata
= kmalloc(sizeof(*calldata
), GFP_KERNEL
);
927 if (calldata
== NULL
)
929 calldata
->inode
= inode
;
930 calldata
->state
= state
;
931 calldata
->arg
.fh
= NFS_FH(inode
);
932 calldata
->arg
.stateid
= &state
->stateid
;
933 /* Serialization for the sequence id */
934 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
935 if (calldata
->arg
.seqid
== NULL
)
936 goto out_free_calldata
;
938 status
= nfs4_call_async(NFS_SERVER(inode
)->client
, nfs4_close_begin
,
939 nfs4_close_done
, calldata
);
943 nfs_free_seqid(calldata
->arg
.seqid
);
951 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
954 struct rpc_cred
*cred
;
955 struct nfs4_state
*state
;
957 if (nd
->flags
& LOOKUP_CREATE
) {
958 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
959 attr
.ia_valid
= ATTR_MODE
;
960 if (!IS_POSIXACL(dir
))
961 attr
.ia_mode
&= ~current
->fs
->umask
;
964 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
967 cred
= rpcauth_lookupcred(NFS_SERVER(dir
)->client
->cl_auth
, 0);
969 return (struct inode
*)cred
;
970 state
= nfs4_do_open(dir
, dentry
, nd
->intent
.open
.flags
, &attr
, cred
);
973 return (struct inode
*)state
;
978 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
)
980 struct rpc_cred
*cred
;
981 struct nfs4_state
*state
;
984 cred
= rpcauth_lookupcred(NFS_SERVER(dir
)->client
->cl_auth
, 0);
986 return PTR_ERR(cred
);
987 state
= nfs4_open_delegated(dentry
->d_inode
, openflags
, cred
);
989 state
= nfs4_do_open(dir
, dentry
, openflags
, NULL
, cred
);
991 if (state
== ERR_PTR(-ENOENT
) && dentry
->d_inode
== 0)
995 inode
= state
->inode
;
996 if (inode
== dentry
->d_inode
) {
1001 nfs4_close_state(state
, openflags
);
1007 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1009 struct nfs4_server_caps_res res
= {};
1010 struct rpc_message msg
= {
1011 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
1012 .rpc_argp
= fhandle
,
1017 status
= rpc_call_sync(server
->client
, &msg
, 0);
1019 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
1020 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
1021 server
->caps
|= NFS_CAP_ACLS
;
1022 if (res
.has_links
!= 0)
1023 server
->caps
|= NFS_CAP_HARDLINKS
;
1024 if (res
.has_symlinks
!= 0)
1025 server
->caps
|= NFS_CAP_SYMLINKS
;
1026 server
->acl_bitmask
= res
.acl_bitmask
;
1031 static int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1033 struct nfs4_exception exception
= { };
1036 err
= nfs4_handle_exception(server
,
1037 _nfs4_server_capabilities(server
, fhandle
),
1039 } while (exception
.retry
);
1043 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1044 struct nfs_fsinfo
*info
)
1046 struct nfs_fattr
* fattr
= info
->fattr
;
1047 struct nfs4_lookup_root_arg args
= {
1048 .bitmask
= nfs4_fattr_bitmap
,
1050 struct nfs4_lookup_res res
= {
1055 struct rpc_message msg
= {
1056 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
1061 return rpc_call_sync(server
->client
, &msg
, 0);
1064 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1065 struct nfs_fsinfo
*info
)
1067 struct nfs4_exception exception
= { };
1070 err
= nfs4_handle_exception(server
,
1071 _nfs4_lookup_root(server
, fhandle
, info
),
1073 } while (exception
.retry
);
1077 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1078 struct nfs_fsinfo
*info
)
1080 struct nfs_fattr
* fattr
= info
->fattr
;
1083 struct nfs4_lookup_arg args
= {
1086 .bitmask
= nfs4_fattr_bitmap
,
1088 struct nfs4_lookup_res res
= {
1093 struct rpc_message msg
= {
1094 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
1101 * Now we do a separate LOOKUP for each component of the mount path.
1102 * The LOOKUPs are done separately so that we can conveniently
1103 * catch an ERR_WRONGSEC if it occurs along the way...
1105 status
= nfs4_lookup_root(server
, fhandle
, info
);
1109 p
= server
->mnt_path
;
1111 struct nfs4_exception exception
= { };
1118 while (*p
&& (*p
!= '/'))
1124 status
= nfs4_handle_exception(server
,
1125 rpc_call_sync(server
->client
, &msg
, 0),
1127 } while (exception
.retry
);
1130 if (status
== -ENOENT
) {
1131 printk(KERN_NOTICE
"NFS: mount path %s does not exist!\n", server
->mnt_path
);
1132 printk(KERN_NOTICE
"NFS: suggestion: try mounting '/' instead.\n");
1137 status
= nfs4_server_capabilities(server
, fhandle
);
1139 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
1144 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1146 struct nfs4_getattr_arg args
= {
1148 .bitmask
= server
->attr_bitmask
,
1150 struct nfs4_getattr_res res
= {
1154 struct rpc_message msg
= {
1155 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
1161 return rpc_call_sync(server
->client
, &msg
, 0);
1164 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1166 struct nfs4_exception exception
= { };
1169 err
= nfs4_handle_exception(server
,
1170 _nfs4_proc_getattr(server
, fhandle
, fattr
),
1172 } while (exception
.retry
);
1177 * The file is not closed if it is opened due to the a request to change
1178 * the size of the file. The open call will not be needed once the
1179 * VFS layer lookup-intents are implemented.
1181 * Close is called when the inode is destroyed.
1182 * If we haven't opened the file for O_WRONLY, we
1183 * need to in the size_change case to obtain a stateid.
1186 * Because OPEN is always done by name in nfsv4, it is
1187 * possible that we opened a different file by the same
1188 * name. We can recognize this race condition, but we
1189 * can't do anything about it besides returning an error.
1191 * This will be fixed with VFS changes (lookup-intent).
1194 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
1195 struct iattr
*sattr
)
1197 struct rpc_cred
*cred
;
1198 struct inode
*inode
= dentry
->d_inode
;
1199 struct nfs4_state
*state
;
1204 cred
= rpcauth_lookupcred(NFS_SERVER(inode
)->client
->cl_auth
, 0);
1206 return PTR_ERR(cred
);
1207 /* Search for an existing WRITE delegation first */
1208 state
= nfs4_open_delegated(inode
, FMODE_WRITE
, cred
);
1209 if (!IS_ERR(state
)) {
1210 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1213 /* Search for an existing open(O_WRITE) stateid */
1214 state
= nfs4_find_state(inode
, cred
, FMODE_WRITE
);
1217 status
= nfs4_do_setattr(NFS_SERVER(inode
), fattr
,
1218 NFS_FH(inode
), sattr
, state
);
1220 nfs_setattr_update_inode(inode
, sattr
);
1222 nfs4_close_state(state
, FMODE_WRITE
);
1227 static int _nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
,
1228 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1231 struct nfs_server
*server
= NFS_SERVER(dir
);
1232 struct nfs4_lookup_arg args
= {
1233 .bitmask
= server
->attr_bitmask
,
1234 .dir_fh
= NFS_FH(dir
),
1237 struct nfs4_lookup_res res
= {
1242 struct rpc_message msg
= {
1243 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
1250 dprintk("NFS call lookup %s\n", name
->name
);
1251 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1252 dprintk("NFS reply lookup: %d\n", status
);
1256 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1258 struct nfs4_exception exception
= { };
1261 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1262 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
1264 } while (exception
.retry
);
1268 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
1270 struct nfs4_accessargs args
= {
1271 .fh
= NFS_FH(inode
),
1273 struct nfs4_accessres res
= { 0 };
1274 struct rpc_message msg
= {
1275 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
1278 .rpc_cred
= entry
->cred
,
1280 int mode
= entry
->mask
;
1284 * Determine which access bits we want to ask for...
1286 if (mode
& MAY_READ
)
1287 args
.access
|= NFS4_ACCESS_READ
;
1288 if (S_ISDIR(inode
->i_mode
)) {
1289 if (mode
& MAY_WRITE
)
1290 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
1291 if (mode
& MAY_EXEC
)
1292 args
.access
|= NFS4_ACCESS_LOOKUP
;
1294 if (mode
& MAY_WRITE
)
1295 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
1296 if (mode
& MAY_EXEC
)
1297 args
.access
|= NFS4_ACCESS_EXECUTE
;
1299 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
1302 if (res
.access
& NFS4_ACCESS_READ
)
1303 entry
->mask
|= MAY_READ
;
1304 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
1305 entry
->mask
|= MAY_WRITE
;
1306 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
1307 entry
->mask
|= MAY_EXEC
;
1312 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
1314 struct nfs4_exception exception
= { };
1317 err
= nfs4_handle_exception(NFS_SERVER(inode
),
1318 _nfs4_proc_access(inode
, entry
),
1320 } while (exception
.retry
);
1325 * TODO: For the time being, we don't try to get any attributes
1326 * along with any of the zero-copy operations READ, READDIR,
1329 * In the case of the first three, we want to put the GETATTR
1330 * after the read-type operation -- this is because it is hard
1331 * to predict the length of a GETATTR response in v4, and thus
1332 * align the READ data correctly. This means that the GETATTR
1333 * may end up partially falling into the page cache, and we should
1334 * shift it into the 'tail' of the xdr_buf before processing.
1335 * To do this efficiently, we need to know the total length
1336 * of data received, which doesn't seem to be available outside
1339 * In the case of WRITE, we also want to put the GETATTR after
1340 * the operation -- in this case because we want to make sure
1341 * we get the post-operation mtime and size. This means that
1342 * we can't use xdr_encode_pages() as written: we need a variant
1343 * of it which would leave room in the 'tail' iovec.
1345 * Both of these changes to the XDR layer would in fact be quite
1346 * minor, but I decided to leave them for a subsequent patch.
1348 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
1349 unsigned int pgbase
, unsigned int pglen
)
1351 struct nfs4_readlink args
= {
1352 .fh
= NFS_FH(inode
),
1357 struct rpc_message msg
= {
1358 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
1363 return rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
1366 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
1367 unsigned int pgbase
, unsigned int pglen
)
1369 struct nfs4_exception exception
= { };
1372 err
= nfs4_handle_exception(NFS_SERVER(inode
),
1373 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
1375 } while (exception
.retry
);
1379 static int _nfs4_proc_read(struct nfs_read_data
*rdata
)
1381 int flags
= rdata
->flags
;
1382 struct inode
*inode
= rdata
->inode
;
1383 struct nfs_fattr
*fattr
= rdata
->res
.fattr
;
1384 struct nfs_server
*server
= NFS_SERVER(inode
);
1385 struct rpc_message msg
= {
1386 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
],
1387 .rpc_argp
= &rdata
->args
,
1388 .rpc_resp
= &rdata
->res
,
1389 .rpc_cred
= rdata
->cred
,
1391 unsigned long timestamp
= jiffies
;
1394 dprintk("NFS call read %d @ %Ld\n", rdata
->args
.count
,
1395 (long long) rdata
->args
.offset
);
1398 status
= rpc_call_sync(server
->client
, &msg
, flags
);
1400 renew_lease(server
, timestamp
);
1401 dprintk("NFS reply read: %d\n", status
);
1405 static int nfs4_proc_read(struct nfs_read_data
*rdata
)
1407 struct nfs4_exception exception
= { };
1410 err
= nfs4_handle_exception(NFS_SERVER(rdata
->inode
),
1411 _nfs4_proc_read(rdata
),
1413 } while (exception
.retry
);
1417 static int _nfs4_proc_write(struct nfs_write_data
*wdata
)
1419 int rpcflags
= wdata
->flags
;
1420 struct inode
*inode
= wdata
->inode
;
1421 struct nfs_fattr
*fattr
= wdata
->res
.fattr
;
1422 struct nfs_server
*server
= NFS_SERVER(inode
);
1423 struct rpc_message msg
= {
1424 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
],
1425 .rpc_argp
= &wdata
->args
,
1426 .rpc_resp
= &wdata
->res
,
1427 .rpc_cred
= wdata
->cred
,
1431 dprintk("NFS call write %d @ %Ld\n", wdata
->args
.count
,
1432 (long long) wdata
->args
.offset
);
1435 status
= rpc_call_sync(server
->client
, &msg
, rpcflags
);
1436 dprintk("NFS reply write: %d\n", status
);
1440 static int nfs4_proc_write(struct nfs_write_data
*wdata
)
1442 struct nfs4_exception exception
= { };
1445 err
= nfs4_handle_exception(NFS_SERVER(wdata
->inode
),
1446 _nfs4_proc_write(wdata
),
1448 } while (exception
.retry
);
1452 static int _nfs4_proc_commit(struct nfs_write_data
*cdata
)
1454 struct inode
*inode
= cdata
->inode
;
1455 struct nfs_fattr
*fattr
= cdata
->res
.fattr
;
1456 struct nfs_server
*server
= NFS_SERVER(inode
);
1457 struct rpc_message msg
= {
1458 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
],
1459 .rpc_argp
= &cdata
->args
,
1460 .rpc_resp
= &cdata
->res
,
1461 .rpc_cred
= cdata
->cred
,
1465 dprintk("NFS call commit %d @ %Ld\n", cdata
->args
.count
,
1466 (long long) cdata
->args
.offset
);
1469 status
= rpc_call_sync(server
->client
, &msg
, 0);
1470 dprintk("NFS reply commit: %d\n", status
);
1474 static int nfs4_proc_commit(struct nfs_write_data
*cdata
)
1476 struct nfs4_exception exception
= { };
1479 err
= nfs4_handle_exception(NFS_SERVER(cdata
->inode
),
1480 _nfs4_proc_commit(cdata
),
1482 } while (exception
.retry
);
1488 * We will need to arrange for the VFS layer to provide an atomic open.
1489 * Until then, this create/open method is prone to inefficiency and race
1490 * conditions due to the lookup, create, and open VFS calls from sys_open()
1491 * placed on the wire.
1493 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1494 * The file will be opened again in the subsequent VFS open call
1495 * (nfs4_proc_file_open).
1497 * The open for read will just hang around to be used by any process that
1498 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1502 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
1505 struct nfs4_state
*state
;
1506 struct rpc_cred
*cred
;
1509 cred
= rpcauth_lookupcred(NFS_SERVER(dir
)->client
->cl_auth
, 0);
1511 status
= PTR_ERR(cred
);
1514 state
= nfs4_do_open(dir
, dentry
, flags
, sattr
, cred
);
1516 if (IS_ERR(state
)) {
1517 status
= PTR_ERR(state
);
1520 d_instantiate(dentry
, state
->inode
);
1521 if (flags
& O_EXCL
) {
1522 struct nfs_fattr fattr
;
1523 status
= nfs4_do_setattr(NFS_SERVER(dir
), &fattr
,
1524 NFS_FH(state
->inode
), sattr
, state
);
1526 nfs_setattr_update_inode(state
->inode
, sattr
);
1529 } else if (flags
!= 0)
1531 nfs4_close_state(state
, flags
);
1536 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
1538 struct nfs4_remove_arg args
= {
1542 struct nfs4_change_info res
;
1543 struct rpc_message msg
= {
1544 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
1550 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1552 update_changeattr(dir
, &res
);
1556 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
1558 struct nfs4_exception exception
= { };
1561 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1562 _nfs4_proc_remove(dir
, name
),
1564 } while (exception
.retry
);
1568 struct unlink_desc
{
1569 struct nfs4_remove_arg args
;
1570 struct nfs4_change_info res
;
1573 static int nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct dentry
*dir
,
1576 struct unlink_desc
*up
;
1578 up
= (struct unlink_desc
*) kmalloc(sizeof(*up
), GFP_KERNEL
);
1582 up
->args
.fh
= NFS_FH(dir
->d_inode
);
1583 up
->args
.name
= name
;
1585 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
1586 msg
->rpc_argp
= &up
->args
;
1587 msg
->rpc_resp
= &up
->res
;
1591 static int nfs4_proc_unlink_done(struct dentry
*dir
, struct rpc_task
*task
)
1593 struct rpc_message
*msg
= &task
->tk_msg
;
1594 struct unlink_desc
*up
;
1596 if (msg
->rpc_resp
!= NULL
) {
1597 up
= container_of(msg
->rpc_resp
, struct unlink_desc
, res
);
1598 update_changeattr(dir
->d_inode
, &up
->res
);
1600 msg
->rpc_resp
= NULL
;
1601 msg
->rpc_argp
= NULL
;
1606 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
1607 struct inode
*new_dir
, struct qstr
*new_name
)
1609 struct nfs4_rename_arg arg
= {
1610 .old_dir
= NFS_FH(old_dir
),
1611 .new_dir
= NFS_FH(new_dir
),
1612 .old_name
= old_name
,
1613 .new_name
= new_name
,
1615 struct nfs4_rename_res res
= { };
1616 struct rpc_message msg
= {
1617 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
1623 status
= rpc_call_sync(NFS_CLIENT(old_dir
), &msg
, 0);
1626 update_changeattr(old_dir
, &res
.old_cinfo
);
1627 update_changeattr(new_dir
, &res
.new_cinfo
);
1632 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
1633 struct inode
*new_dir
, struct qstr
*new_name
)
1635 struct nfs4_exception exception
= { };
1638 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
1639 _nfs4_proc_rename(old_dir
, old_name
,
1642 } while (exception
.retry
);
1646 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
1648 struct nfs4_link_arg arg
= {
1649 .fh
= NFS_FH(inode
),
1650 .dir_fh
= NFS_FH(dir
),
1653 struct nfs4_change_info cinfo
= { };
1654 struct rpc_message msg
= {
1655 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
1661 status
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
1663 update_changeattr(dir
, &cinfo
);
1668 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
1670 struct nfs4_exception exception
= { };
1673 err
= nfs4_handle_exception(NFS_SERVER(inode
),
1674 _nfs4_proc_link(inode
, dir
, name
),
1676 } while (exception
.retry
);
1680 static int _nfs4_proc_symlink(struct inode
*dir
, struct qstr
*name
,
1681 struct qstr
*path
, struct iattr
*sattr
, struct nfs_fh
*fhandle
,
1682 struct nfs_fattr
*fattr
)
1684 struct nfs_server
*server
= NFS_SERVER(dir
);
1685 struct nfs4_create_arg arg
= {
1686 .dir_fh
= NFS_FH(dir
),
1691 .bitmask
= server
->attr_bitmask
,
1693 struct nfs4_create_res res
= {
1698 struct rpc_message msg
= {
1699 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
],
1705 if (path
->len
> NFS4_MAXPATHLEN
)
1706 return -ENAMETOOLONG
;
1707 arg
.u
.symlink
= path
;
1710 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1712 update_changeattr(dir
, &res
.dir_cinfo
);
1716 static int nfs4_proc_symlink(struct inode
*dir
, struct qstr
*name
,
1717 struct qstr
*path
, struct iattr
*sattr
, struct nfs_fh
*fhandle
,
1718 struct nfs_fattr
*fattr
)
1720 struct nfs4_exception exception
= { };
1723 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1724 _nfs4_proc_symlink(dir
, name
, path
, sattr
,
1727 } while (exception
.retry
);
1731 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
1732 struct iattr
*sattr
)
1734 struct nfs_server
*server
= NFS_SERVER(dir
);
1735 struct nfs_fh fhandle
;
1736 struct nfs_fattr fattr
;
1737 struct nfs4_create_arg arg
= {
1738 .dir_fh
= NFS_FH(dir
),
1740 .name
= &dentry
->d_name
,
1743 .bitmask
= server
->attr_bitmask
,
1745 struct nfs4_create_res res
= {
1750 struct rpc_message msg
= {
1751 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
],
1759 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1761 update_changeattr(dir
, &res
.dir_cinfo
);
1762 status
= nfs_instantiate(dentry
, &fhandle
, &fattr
);
1767 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
1768 struct iattr
*sattr
)
1770 struct nfs4_exception exception
= { };
1773 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1774 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
1776 } while (exception
.retry
);
1780 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
1781 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
1783 struct inode
*dir
= dentry
->d_inode
;
1784 struct nfs4_readdir_arg args
= {
1789 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
1791 struct nfs4_readdir_res res
;
1792 struct rpc_message msg
= {
1793 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
1800 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__
,
1801 dentry
->d_parent
->d_name
.name
,
1802 dentry
->d_name
.name
,
1803 (unsigned long long)cookie
);
1805 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
1806 res
.pgbase
= args
.pgbase
;
1807 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1809 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
1811 dprintk("%s: returns %d\n", __FUNCTION__
, status
);
1815 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
1816 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
1818 struct nfs4_exception exception
= { };
1821 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
1822 _nfs4_proc_readdir(dentry
, cred
, cookie
,
1825 } while (exception
.retry
);
1829 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
1830 struct iattr
*sattr
, dev_t rdev
)
1832 struct nfs_server
*server
= NFS_SERVER(dir
);
1834 struct nfs_fattr fattr
;
1835 struct nfs4_create_arg arg
= {
1836 .dir_fh
= NFS_FH(dir
),
1838 .name
= &dentry
->d_name
,
1840 .bitmask
= server
->attr_bitmask
,
1842 struct nfs4_create_res res
= {
1847 struct rpc_message msg
= {
1848 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
],
1853 int mode
= sattr
->ia_mode
;
1857 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
1858 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
1860 arg
.ftype
= NF4FIFO
;
1861 else if (S_ISBLK(mode
)) {
1863 arg
.u
.device
.specdata1
= MAJOR(rdev
);
1864 arg
.u
.device
.specdata2
= MINOR(rdev
);
1866 else if (S_ISCHR(mode
)) {
1868 arg
.u
.device
.specdata1
= MAJOR(rdev
);
1869 arg
.u
.device
.specdata2
= MINOR(rdev
);
1872 arg
.ftype
= NF4SOCK
;
1874 status
= rpc_call_sync(NFS_CLIENT(dir
), &msg
, 0);
1876 update_changeattr(dir
, &res
.dir_cinfo
);
1877 status
= nfs_instantiate(dentry
, &fh
, &fattr
);
1882 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
1883 struct iattr
*sattr
, dev_t rdev
)
1885 struct nfs4_exception exception
= { };
1888 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1889 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
1891 } while (exception
.retry
);
1895 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1896 struct nfs_fsstat
*fsstat
)
1898 struct nfs4_statfs_arg args
= {
1900 .bitmask
= server
->attr_bitmask
,
1902 struct rpc_message msg
= {
1903 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
1908 fsstat
->fattr
->valid
= 0;
1909 return rpc_call_sync(server
->client
, &msg
, 0);
1912 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
1914 struct nfs4_exception exception
= { };
1917 err
= nfs4_handle_exception(server
,
1918 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
1920 } while (exception
.retry
);
1924 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1925 struct nfs_fsinfo
*fsinfo
)
1927 struct nfs4_fsinfo_arg args
= {
1929 .bitmask
= server
->attr_bitmask
,
1931 struct rpc_message msg
= {
1932 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
1937 return rpc_call_sync(server
->client
, &msg
, 0);
1940 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
1942 struct nfs4_exception exception
= { };
1946 err
= nfs4_handle_exception(server
,
1947 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
1949 } while (exception
.retry
);
1953 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
1955 fsinfo
->fattr
->valid
= 0;
1956 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
1959 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1960 struct nfs_pathconf
*pathconf
)
1962 struct nfs4_pathconf_arg args
= {
1964 .bitmask
= server
->attr_bitmask
,
1966 struct rpc_message msg
= {
1967 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
1969 .rpc_resp
= pathconf
,
1972 /* None of the pathconf attributes are mandatory to implement */
1973 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
1974 memset(pathconf
, 0, sizeof(*pathconf
));
1978 pathconf
->fattr
->valid
= 0;
1979 return rpc_call_sync(server
->client
, &msg
, 0);
1982 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1983 struct nfs_pathconf
*pathconf
)
1985 struct nfs4_exception exception
= { };
1989 err
= nfs4_handle_exception(server
,
1990 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
1992 } while (exception
.retry
);
1997 nfs4_read_done(struct rpc_task
*task
)
1999 struct nfs_read_data
*data
= (struct nfs_read_data
*) task
->tk_calldata
;
2000 struct inode
*inode
= data
->inode
;
2002 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
)) == -EAGAIN
) {
2003 rpc_restart_call(task
);
2006 if (task
->tk_status
> 0)
2007 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
2008 /* Call back common NFS readpage processing */
2009 nfs_readpage_result(task
);
2013 nfs4_proc_read_setup(struct nfs_read_data
*data
)
2015 struct rpc_task
*task
= &data
->task
;
2016 struct rpc_message msg
= {
2017 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
],
2018 .rpc_argp
= &data
->args
,
2019 .rpc_resp
= &data
->res
,
2020 .rpc_cred
= data
->cred
,
2022 struct inode
*inode
= data
->inode
;
2025 data
->timestamp
= jiffies
;
2027 /* N.B. Do we need to test? Never called for swapfile inode */
2028 flags
= RPC_TASK_ASYNC
| (IS_SWAPFILE(inode
)? NFS_RPC_SWAPFLAGS
: 0);
2030 /* Finalize the task. */
2031 rpc_init_task(task
, NFS_CLIENT(inode
), nfs4_read_done
, flags
);
2032 rpc_call_setup(task
, &msg
, 0);
2036 nfs4_write_done(struct rpc_task
*task
)
2038 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
2039 struct inode
*inode
= data
->inode
;
2041 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
)) == -EAGAIN
) {
2042 rpc_restart_call(task
);
2045 if (task
->tk_status
>= 0)
2046 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
2047 /* Call back common NFS writeback processing */
2048 nfs_writeback_done(task
);
2052 nfs4_proc_write_setup(struct nfs_write_data
*data
, int how
)
2054 struct rpc_task
*task
= &data
->task
;
2055 struct rpc_message msg
= {
2056 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
],
2057 .rpc_argp
= &data
->args
,
2058 .rpc_resp
= &data
->res
,
2059 .rpc_cred
= data
->cred
,
2061 struct inode
*inode
= data
->inode
;
2065 if (how
& FLUSH_STABLE
) {
2066 if (!NFS_I(inode
)->ncommit
)
2067 stable
= NFS_FILE_SYNC
;
2069 stable
= NFS_DATA_SYNC
;
2071 stable
= NFS_UNSTABLE
;
2072 data
->args
.stable
= stable
;
2074 data
->timestamp
= jiffies
;
2076 /* Set the initial flags for the task. */
2077 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
2079 /* Finalize the task. */
2080 rpc_init_task(task
, NFS_CLIENT(inode
), nfs4_write_done
, flags
);
2081 rpc_call_setup(task
, &msg
, 0);
2085 nfs4_commit_done(struct rpc_task
*task
)
2087 struct nfs_write_data
*data
= (struct nfs_write_data
*) task
->tk_calldata
;
2088 struct inode
*inode
= data
->inode
;
2090 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
)) == -EAGAIN
) {
2091 rpc_restart_call(task
);
2094 /* Call back common NFS writeback processing */
2095 nfs_commit_done(task
);
2099 nfs4_proc_commit_setup(struct nfs_write_data
*data
, int how
)
2101 struct rpc_task
*task
= &data
->task
;
2102 struct rpc_message msg
= {
2103 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
],
2104 .rpc_argp
= &data
->args
,
2105 .rpc_resp
= &data
->res
,
2106 .rpc_cred
= data
->cred
,
2108 struct inode
*inode
= data
->inode
;
2111 /* Set the initial flags for the task. */
2112 flags
= (how
& FLUSH_SYNC
) ? 0 : RPC_TASK_ASYNC
;
2114 /* Finalize the task. */
2115 rpc_init_task(task
, NFS_CLIENT(inode
), nfs4_commit_done
, flags
);
2116 rpc_call_setup(task
, &msg
, 0);
2120 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2121 * standalone procedure for queueing an asynchronous RENEW.
2124 renew_done(struct rpc_task
*task
)
2126 struct nfs4_client
*clp
= (struct nfs4_client
*)task
->tk_msg
.rpc_argp
;
2127 unsigned long timestamp
= (unsigned long)task
->tk_calldata
;
2129 if (task
->tk_status
< 0) {
2130 switch (task
->tk_status
) {
2131 case -NFS4ERR_STALE_CLIENTID
:
2132 case -NFS4ERR_EXPIRED
:
2133 case -NFS4ERR_CB_PATH_DOWN
:
2134 nfs4_schedule_state_recovery(clp
);
2138 spin_lock(&clp
->cl_lock
);
2139 if (time_before(clp
->cl_last_renewal
,timestamp
))
2140 clp
->cl_last_renewal
= timestamp
;
2141 spin_unlock(&clp
->cl_lock
);
2145 nfs4_proc_async_renew(struct nfs4_client
*clp
)
2147 struct rpc_message msg
= {
2148 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2150 .rpc_cred
= clp
->cl_cred
,
2153 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
2154 renew_done
, (void *)jiffies
);
2158 nfs4_proc_renew(struct nfs4_client
*clp
)
2160 struct rpc_message msg
= {
2161 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2163 .rpc_cred
= clp
->cl_cred
,
2165 unsigned long now
= jiffies
;
2168 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2171 spin_lock(&clp
->cl_lock
);
2172 if (time_before(clp
->cl_last_renewal
,now
))
2173 clp
->cl_last_renewal
= now
;
2174 spin_unlock(&clp
->cl_lock
);
2179 * We will need to arrange for the VFS layer to provide an atomic open.
2180 * Until then, this open method is prone to inefficiency and race conditions
2181 * due to the lookup, potential create, and open VFS calls from sys_open()
2182 * placed on the wire.
2185 nfs4_proc_file_open(struct inode
*inode
, struct file
*filp
)
2187 struct dentry
*dentry
= filp
->f_dentry
;
2188 struct nfs_open_context
*ctx
;
2189 struct nfs4_state
*state
= NULL
;
2190 struct rpc_cred
*cred
;
2191 int status
= -ENOMEM
;
2193 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2194 (int)dentry
->d_parent
->d_name
.len
,
2195 dentry
->d_parent
->d_name
.name
,
2196 (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
2199 /* Find our open stateid */
2200 cred
= rpcauth_lookupcred(NFS_SERVER(inode
)->client
->cl_auth
, 0);
2202 return PTR_ERR(cred
);
2203 ctx
= alloc_nfs_open_context(dentry
, cred
);
2205 if (unlikely(ctx
== NULL
))
2207 status
= -EIO
; /* ERACE actually */
2208 state
= nfs4_find_state(inode
, cred
, filp
->f_mode
);
2209 if (unlikely(state
== NULL
))
2212 nfs4_close_state(state
, filp
->f_mode
);
2213 ctx
->mode
= filp
->f_mode
;
2214 nfs_file_set_open_context(filp
, ctx
);
2215 put_nfs_open_context(ctx
);
2216 if (filp
->f_mode
& FMODE_WRITE
)
2217 nfs_begin_data_update(inode
);
2220 printk(KERN_WARNING
"NFS: v4 raced in function %s\n", __FUNCTION__
);
2221 put_nfs_open_context(ctx
);
2229 nfs4_proc_file_release(struct inode
*inode
, struct file
*filp
)
2231 if (filp
->f_mode
& FMODE_WRITE
)
2232 nfs_end_data_update(inode
);
2233 nfs_file_clear_open_context(filp
);
2237 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
2239 return (server
->caps
& NFS_CAP_ACLS
)
2240 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
2241 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
2244 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2245 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2248 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2250 static void buf_to_pages(const void *buf
, size_t buflen
,
2251 struct page
**pages
, unsigned int *pgbase
)
2253 const void *p
= buf
;
2255 *pgbase
= offset_in_page(buf
);
2257 while (p
< buf
+ buflen
) {
2258 *(pages
++) = virt_to_page(p
);
2259 p
+= PAGE_CACHE_SIZE
;
2263 struct nfs4_cached_acl
{
2269 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
2271 struct nfs_inode
*nfsi
= NFS_I(inode
);
2273 spin_lock(&inode
->i_lock
);
2274 kfree(nfsi
->nfs4_acl
);
2275 nfsi
->nfs4_acl
= acl
;
2276 spin_unlock(&inode
->i_lock
);
2279 static void nfs4_zap_acl_attr(struct inode
*inode
)
2281 nfs4_set_cached_acl(inode
, NULL
);
2284 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
2286 struct nfs_inode
*nfsi
= NFS_I(inode
);
2287 struct nfs4_cached_acl
*acl
;
2290 spin_lock(&inode
->i_lock
);
2291 acl
= nfsi
->nfs4_acl
;
2294 if (buf
== NULL
) /* user is just asking for length */
2296 if (acl
->cached
== 0)
2298 ret
= -ERANGE
; /* see getxattr(2) man page */
2299 if (acl
->len
> buflen
)
2301 memcpy(buf
, acl
->data
, acl
->len
);
2305 spin_unlock(&inode
->i_lock
);
2309 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
2311 struct nfs4_cached_acl
*acl
;
2313 if (buf
&& acl_len
<= PAGE_SIZE
) {
2314 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
2318 memcpy(acl
->data
, buf
, acl_len
);
2320 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
2327 nfs4_set_cached_acl(inode
, acl
);
2330 static inline ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
2332 struct page
*pages
[NFS4ACL_MAXPAGES
];
2333 struct nfs_getaclargs args
= {
2334 .fh
= NFS_FH(inode
),
2338 size_t resp_len
= buflen
;
2340 struct rpc_message msg
= {
2341 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
2343 .rpc_resp
= &resp_len
,
2345 struct page
*localpage
= NULL
;
2348 if (buflen
< PAGE_SIZE
) {
2349 /* As long as we're doing a round trip to the server anyway,
2350 * let's be prepared for a page of acl data. */
2351 localpage
= alloc_page(GFP_KERNEL
);
2352 resp_buf
= page_address(localpage
);
2353 if (localpage
== NULL
)
2355 args
.acl_pages
[0] = localpage
;
2356 args
.acl_pgbase
= 0;
2357 args
.acl_len
= PAGE_SIZE
;
2360 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
2362 ret
= rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
2365 if (resp_len
> args
.acl_len
)
2366 nfs4_write_cached_acl(inode
, NULL
, resp_len
);
2368 nfs4_write_cached_acl(inode
, resp_buf
, resp_len
);
2371 if (resp_len
> buflen
)
2374 memcpy(buf
, resp_buf
, resp_len
);
2379 __free_page(localpage
);
2383 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
2385 struct nfs_server
*server
= NFS_SERVER(inode
);
2388 if (!nfs4_server_supports_acls(server
))
2390 ret
= nfs_revalidate_inode(server
, inode
);
2393 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
2396 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
2399 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
2401 struct nfs_server
*server
= NFS_SERVER(inode
);
2402 struct page
*pages
[NFS4ACL_MAXPAGES
];
2403 struct nfs_setaclargs arg
= {
2404 .fh
= NFS_FH(inode
),
2408 struct rpc_message msg
= {
2409 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
2415 if (!nfs4_server_supports_acls(server
))
2417 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
2418 ret
= rpc_call_sync(NFS_SERVER(inode
)->client
, &msg
, 0);
2420 nfs4_write_cached_acl(inode
, buf
, buflen
);
2425 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
)
2427 struct nfs4_client
*clp
= server
->nfs4_state
;
2429 if (!clp
|| task
->tk_status
>= 0)
2431 switch(task
->tk_status
) {
2432 case -NFS4ERR_STALE_CLIENTID
:
2433 case -NFS4ERR_STALE_STATEID
:
2434 case -NFS4ERR_EXPIRED
:
2435 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
, NULL
);
2436 nfs4_schedule_state_recovery(clp
);
2437 if (test_bit(NFS4CLNT_OK
, &clp
->cl_state
))
2438 rpc_wake_up_task(task
);
2439 task
->tk_status
= 0;
2441 case -NFS4ERR_GRACE
:
2442 case -NFS4ERR_DELAY
:
2443 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
2444 task
->tk_status
= 0;
2446 case -NFS4ERR_OLD_STATEID
:
2447 task
->tk_status
= 0;
2450 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
2454 static int nfs4_wait_clnt_recover(struct rpc_clnt
*clnt
, struct nfs4_client
*clp
)
2458 int interruptible
, res
= 0;
2462 rpc_clnt_sigmask(clnt
, &oldset
);
2463 interruptible
= TASK_UNINTERRUPTIBLE
;
2465 interruptible
= TASK_INTERRUPTIBLE
;
2466 prepare_to_wait(&clp
->cl_waitq
, &wait
, interruptible
);
2467 nfs4_schedule_state_recovery(clp
);
2468 if (clnt
->cl_intr
&& signalled())
2470 else if (!test_bit(NFS4CLNT_OK
, &clp
->cl_state
))
2472 finish_wait(&clp
->cl_waitq
, &wait
);
2473 rpc_clnt_sigunmask(clnt
, &oldset
);
2477 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
2485 *timeout
= NFS4_POLL_RETRY_MIN
;
2486 if (*timeout
> NFS4_POLL_RETRY_MAX
)
2487 *timeout
= NFS4_POLL_RETRY_MAX
;
2488 rpc_clnt_sigmask(clnt
, &oldset
);
2489 if (clnt
->cl_intr
) {
2490 schedule_timeout_interruptible(*timeout
);
2494 schedule_timeout_uninterruptible(*timeout
);
2495 rpc_clnt_sigunmask(clnt
, &oldset
);
2500 /* This is the error handling routine for processes that are allowed
2503 int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
2505 struct nfs4_client
*clp
= server
->nfs4_state
;
2506 int ret
= errorcode
;
2508 exception
->retry
= 0;
2512 case -NFS4ERR_STALE_CLIENTID
:
2513 case -NFS4ERR_STALE_STATEID
:
2514 case -NFS4ERR_EXPIRED
:
2515 ret
= nfs4_wait_clnt_recover(server
->client
, clp
);
2517 exception
->retry
= 1;
2519 case -NFS4ERR_GRACE
:
2520 case -NFS4ERR_DELAY
:
2521 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
2523 exception
->retry
= 1;
2525 case -NFS4ERR_OLD_STATEID
:
2527 exception
->retry
= 1;
2529 /* We failed to handle the error */
2530 return nfs4_map_errors(ret
);
2533 int nfs4_proc_setclientid(struct nfs4_client
*clp
, u32 program
, unsigned short port
)
2535 nfs4_verifier sc_verifier
;
2536 struct nfs4_setclientid setclientid
= {
2537 .sc_verifier
= &sc_verifier
,
2540 struct rpc_message msg
= {
2541 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
2542 .rpc_argp
= &setclientid
,
2544 .rpc_cred
= clp
->cl_cred
,
2550 p
= (u32
*)sc_verifier
.data
;
2551 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
2552 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
2555 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
2556 sizeof(setclientid
.sc_name
), "%s/%u.%u.%u.%u %s %u",
2557 clp
->cl_ipaddr
, NIPQUAD(clp
->cl_addr
.s_addr
),
2558 clp
->cl_cred
->cr_ops
->cr_name
,
2559 clp
->cl_id_uniquifier
);
2560 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
2561 sizeof(setclientid
.sc_netid
), "tcp");
2562 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
2563 sizeof(setclientid
.sc_uaddr
), "%s.%d.%d",
2564 clp
->cl_ipaddr
, port
>> 8, port
& 255);
2566 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2567 if (status
!= -NFS4ERR_CLID_INUSE
)
2572 ssleep(clp
->cl_lease_time
+ 1);
2574 if (++clp
->cl_id_uniquifier
== 0)
2581 nfs4_proc_setclientid_confirm(struct nfs4_client
*clp
)
2583 struct nfs_fsinfo fsinfo
;
2584 struct rpc_message msg
= {
2585 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
2587 .rpc_resp
= &fsinfo
,
2588 .rpc_cred
= clp
->cl_cred
,
2594 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2596 spin_lock(&clp
->cl_lock
);
2597 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
2598 clp
->cl_last_renewal
= now
;
2599 spin_unlock(&clp
->cl_lock
);
2604 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
)
2606 struct nfs4_delegreturnargs args
= {
2607 .fhandle
= NFS_FH(inode
),
2610 struct rpc_message msg
= {
2611 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
2616 return rpc_call_sync(NFS_CLIENT(inode
), &msg
, 0);
2619 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
)
2621 struct nfs_server
*server
= NFS_SERVER(inode
);
2622 struct nfs4_exception exception
= { };
2625 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
);
2627 case -NFS4ERR_STALE_STATEID
:
2628 case -NFS4ERR_EXPIRED
:
2629 nfs4_schedule_state_recovery(server
->nfs4_state
);
2633 err
= nfs4_handle_exception(server
, err
, &exception
);
2634 } while (exception
.retry
);
2638 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2639 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2642 * sleep, with exponential backoff, and retry the LOCK operation.
2644 static unsigned long
2645 nfs4_set_lock_task_retry(unsigned long timeout
)
2647 schedule_timeout_interruptible(timeout
);
2649 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
2650 return NFS4_LOCK_MAXTIMEOUT
;
2655 nfs4_lck_type(int cmd
, struct file_lock
*request
)
2658 switch (request
->fl_type
) {
2660 return IS_SETLKW(cmd
) ? NFS4_READW_LT
: NFS4_READ_LT
;
2662 return IS_SETLKW(cmd
) ? NFS4_WRITEW_LT
: NFS4_WRITE_LT
;
2664 return NFS4_WRITE_LT
;
2670 static inline uint64_t
2671 nfs4_lck_length(struct file_lock
*request
)
2673 if (request
->fl_end
== OFFSET_MAX
)
2674 return ~(uint64_t)0;
2675 return request
->fl_end
- request
->fl_start
+ 1;
2678 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
2680 struct inode
*inode
= state
->inode
;
2681 struct nfs_server
*server
= NFS_SERVER(inode
);
2682 struct nfs4_client
*clp
= server
->nfs4_state
;
2683 struct nfs_lockargs arg
= {
2684 .fh
= NFS_FH(inode
),
2685 .type
= nfs4_lck_type(cmd
, request
),
2686 .offset
= request
->fl_start
,
2687 .length
= nfs4_lck_length(request
),
2689 struct nfs_lockres res
= {
2692 struct rpc_message msg
= {
2693 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
2696 .rpc_cred
= state
->owner
->so_cred
,
2698 struct nfs_lowner nlo
;
2699 struct nfs4_lock_state
*lsp
;
2702 down_read(&clp
->cl_sem
);
2703 nlo
.clientid
= clp
->cl_clientid
;
2704 status
= nfs4_set_lock_state(state
, request
);
2707 lsp
= request
->fl_u
.nfs4_fl
.owner
;
2708 nlo
.id
= lsp
->ls_id
;
2710 status
= rpc_call_sync(server
->client
, &msg
, 0);
2712 request
->fl_type
= F_UNLCK
;
2713 } else if (status
== -NFS4ERR_DENIED
) {
2714 int64_t len
, start
, end
;
2715 start
= res
.u
.denied
.offset
;
2716 len
= res
.u
.denied
.length
;
2717 end
= start
+ len
- 1;
2718 if (end
< 0 || len
== 0)
2719 request
->fl_end
= OFFSET_MAX
;
2721 request
->fl_end
= (loff_t
)end
;
2722 request
->fl_start
= (loff_t
)start
;
2723 request
->fl_type
= F_WRLCK
;
2724 if (res
.u
.denied
.type
& 1)
2725 request
->fl_type
= F_RDLCK
;
2726 request
->fl_pid
= 0;
2730 up_read(&clp
->cl_sem
);
2734 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
2736 struct nfs4_exception exception
= { };
2740 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
2741 _nfs4_proc_getlk(state
, cmd
, request
),
2743 } while (exception
.retry
);
2747 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
2750 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
2752 res
= posix_lock_file_wait(file
, fl
);
2755 res
= flock_lock_file_wait(file
, fl
);
2761 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n",
2766 struct nfs4_unlockdata
{
2767 struct nfs_lockargs arg
;
2768 struct nfs_locku_opargs luargs
;
2769 struct nfs_lockres res
;
2770 struct nfs4_lock_state
*lsp
;
2771 struct nfs_open_context
*ctx
;
2773 struct completion completion
;
2776 static void nfs4_locku_release_calldata(struct nfs4_unlockdata
*calldata
)
2778 if (atomic_dec_and_test(&calldata
->refcount
)) {
2779 nfs_free_seqid(calldata
->luargs
.seqid
);
2780 nfs4_put_lock_state(calldata
->lsp
);
2781 put_nfs_open_context(calldata
->ctx
);
2786 static void nfs4_locku_complete(struct nfs4_unlockdata
*calldata
)
2788 complete(&calldata
->completion
);
2789 nfs4_locku_release_calldata(calldata
);
2792 static void nfs4_locku_done(struct rpc_task
*task
)
2794 struct nfs4_unlockdata
*calldata
= (struct nfs4_unlockdata
*)task
->tk_calldata
;
2796 nfs_increment_lock_seqid(task
->tk_status
, calldata
->luargs
.seqid
);
2797 switch (task
->tk_status
) {
2799 memcpy(calldata
->lsp
->ls_stateid
.data
,
2800 calldata
->res
.u
.stateid
.data
,
2801 sizeof(calldata
->lsp
->ls_stateid
.data
));
2803 case -NFS4ERR_STALE_STATEID
:
2804 case -NFS4ERR_EXPIRED
:
2805 nfs4_schedule_state_recovery(calldata
->res
.server
->nfs4_state
);
2808 if (nfs4_async_handle_error(task
, calldata
->res
.server
) == -EAGAIN
) {
2809 rpc_restart_call(task
);
2813 nfs4_locku_complete(calldata
);
2816 static void nfs4_locku_begin(struct rpc_task
*task
)
2818 struct nfs4_unlockdata
*calldata
= (struct nfs4_unlockdata
*)task
->tk_calldata
;
2819 struct rpc_message msg
= {
2820 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
2821 .rpc_argp
= &calldata
->arg
,
2822 .rpc_resp
= &calldata
->res
,
2823 .rpc_cred
= calldata
->lsp
->ls_state
->owner
->so_cred
,
2827 status
= nfs_wait_on_sequence(calldata
->luargs
.seqid
, task
);
2830 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
2831 nfs4_locku_complete(calldata
);
2832 task
->tk_exit
= NULL
;
2836 rpc_call_setup(task
, &msg
, 0);
2839 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
2841 struct nfs4_unlockdata
*calldata
;
2842 struct inode
*inode
= state
->inode
;
2843 struct nfs_server
*server
= NFS_SERVER(inode
);
2844 struct nfs4_lock_state
*lsp
;
2847 status
= nfs4_set_lock_state(state
, request
);
2850 lsp
= request
->fl_u
.nfs4_fl
.owner
;
2851 /* We might have lost the locks! */
2852 if ((lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0)
2854 calldata
= kmalloc(sizeof(*calldata
), GFP_KERNEL
);
2855 if (calldata
== NULL
)
2857 calldata
->luargs
.seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
2858 if (calldata
->luargs
.seqid
== NULL
) {
2862 calldata
->luargs
.stateid
= &lsp
->ls_stateid
;
2863 calldata
->arg
.fh
= NFS_FH(inode
);
2864 calldata
->arg
.type
= nfs4_lck_type(cmd
, request
);
2865 calldata
->arg
.offset
= request
->fl_start
;
2866 calldata
->arg
.length
= nfs4_lck_length(request
);
2867 calldata
->arg
.u
.locku
= &calldata
->luargs
;
2868 calldata
->res
.server
= server
;
2869 calldata
->lsp
= lsp
;
2870 atomic_inc(&lsp
->ls_count
);
2872 /* Ensure we don't close file until we're done freeing locks! */
2873 calldata
->ctx
= get_nfs_open_context((struct nfs_open_context
*)request
->fl_file
->private_data
);
2875 atomic_set(&calldata
->refcount
, 2);
2876 init_completion(&calldata
->completion
);
2878 status
= nfs4_call_async(NFS_SERVER(inode
)->client
, nfs4_locku_begin
,
2879 nfs4_locku_done
, calldata
);
2881 wait_for_completion_interruptible(&calldata
->completion
);
2882 do_vfs_lock(request
->fl_file
, request
);
2883 nfs4_locku_release_calldata(calldata
);
2887 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
, int reclaim
)
2889 struct inode
*inode
= state
->inode
;
2890 struct nfs_server
*server
= NFS_SERVER(inode
);
2891 struct nfs4_lock_state
*lsp
= request
->fl_u
.nfs4_fl
.owner
;
2892 struct nfs_lock_opargs largs
= {
2893 .lock_stateid
= &lsp
->ls_stateid
,
2894 .open_stateid
= &state
->stateid
,
2896 .clientid
= server
->nfs4_state
->cl_clientid
,
2901 struct nfs_lockargs arg
= {
2902 .fh
= NFS_FH(inode
),
2903 .type
= nfs4_lck_type(cmd
, request
),
2904 .offset
= request
->fl_start
,
2905 .length
= nfs4_lck_length(request
),
2910 struct nfs_lockres res
= {
2913 struct rpc_message msg
= {
2914 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
2917 .rpc_cred
= state
->owner
->so_cred
,
2919 int status
= -ENOMEM
;
2921 largs
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
2922 if (largs
.lock_seqid
== NULL
)
2924 if (!(lsp
->ls_seqid
.flags
& NFS_SEQID_CONFIRMED
)) {
2925 struct nfs4_state_owner
*owner
= state
->owner
;
2927 largs
.open_seqid
= nfs_alloc_seqid(&owner
->so_seqid
);
2928 if (largs
.open_seqid
== NULL
)
2930 largs
.new_lock_owner
= 1;
2931 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
2932 /* increment open seqid on success, and seqid mutating errors */
2933 if (largs
.new_lock_owner
!= 0) {
2934 nfs_increment_open_seqid(status
, largs
.open_seqid
);
2936 nfs_confirm_seqid(&lsp
->ls_seqid
, 0);
2938 nfs_free_seqid(largs
.open_seqid
);
2940 status
= rpc_call_sync(server
->client
, &msg
, RPC_TASK_NOINTR
);
2941 /* increment lock seqid on success, and seqid mutating errors*/
2942 nfs_increment_lock_seqid(status
, largs
.lock_seqid
);
2943 /* save the returned stateid. */
2945 memcpy(lsp
->ls_stateid
.data
, res
.u
.stateid
.data
,
2946 sizeof(lsp
->ls_stateid
.data
));
2947 lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
2948 } else if (status
== -NFS4ERR_DENIED
)
2951 nfs_free_seqid(largs
.lock_seqid
);
2955 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
2957 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2958 struct nfs4_exception exception
= { };
2962 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 1);
2963 if (err
!= -NFS4ERR_DELAY
)
2965 nfs4_handle_exception(server
, err
, &exception
);
2966 } while (exception
.retry
);
2970 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
2972 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
2973 struct nfs4_exception exception
= { };
2977 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 0);
2978 if (err
!= -NFS4ERR_DELAY
)
2980 nfs4_handle_exception(server
, err
, &exception
);
2981 } while (exception
.retry
);
2985 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
2987 struct nfs4_client
*clp
= state
->owner
->so_client
;
2990 down_read(&clp
->cl_sem
);
2991 status
= nfs4_set_lock_state(state
, request
);
2993 status
= _nfs4_do_setlk(state
, cmd
, request
, 0);
2995 /* Note: we always want to sleep here! */
2996 request
->fl_flags
|= FL_SLEEP
;
2997 if (do_vfs_lock(request
->fl_file
, request
) < 0)
2998 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __FUNCTION__
);
3000 up_read(&clp
->cl_sem
);
3004 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3006 struct nfs4_exception exception
= { };
3010 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3011 _nfs4_proc_setlk(state
, cmd
, request
),
3013 } while (exception
.retry
);
3018 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
3020 struct nfs_open_context
*ctx
;
3021 struct nfs4_state
*state
;
3022 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
3025 /* verify open state */
3026 ctx
= (struct nfs_open_context
*)filp
->private_data
;
3029 if (request
->fl_start
< 0 || request
->fl_end
< 0)
3033 return nfs4_proc_getlk(state
, F_GETLK
, request
);
3035 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
3038 if (request
->fl_type
== F_UNLCK
)
3039 return nfs4_proc_unlck(state
, cmd
, request
);
3042 status
= nfs4_proc_setlk(state
, cmd
, request
);
3043 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
3045 timeout
= nfs4_set_lock_task_retry(timeout
);
3046 status
= -ERESTARTSYS
;
3049 } while(status
< 0);
3054 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3056 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
3057 size_t buflen
, int flags
)
3059 struct inode
*inode
= dentry
->d_inode
;
3061 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3064 if (!S_ISREG(inode
->i_mode
) &&
3065 (!S_ISDIR(inode
->i_mode
) || inode
->i_mode
& S_ISVTX
))
3068 return nfs4_proc_set_acl(inode
, buf
, buflen
);
3071 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3072 * and that's what we'll do for e.g. user attributes that haven't been set.
3073 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3074 * attributes in kernel-managed attribute namespaces. */
3075 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
3078 struct inode
*inode
= dentry
->d_inode
;
3080 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3083 return nfs4_proc_get_acl(inode
, buf
, buflen
);
3086 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
3088 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
3090 if (buf
&& buflen
< len
)
3093 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
3097 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops
= {
3098 .recover_open
= nfs4_open_reclaim
,
3099 .recover_lock
= nfs4_lock_reclaim
,
3102 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops
= {
3103 .recover_open
= nfs4_open_expired
,
3104 .recover_lock
= nfs4_lock_expired
,
3107 static struct inode_operations nfs4_file_inode_operations
= {
3108 .permission
= nfs_permission
,
3109 .getattr
= nfs_getattr
,
3110 .setattr
= nfs_setattr
,
3111 .getxattr
= nfs4_getxattr
,
3112 .setxattr
= nfs4_setxattr
,
3113 .listxattr
= nfs4_listxattr
,
3116 struct nfs_rpc_ops nfs_v4_clientops
= {
3117 .version
= 4, /* protocol version */
3118 .dentry_ops
= &nfs4_dentry_operations
,
3119 .dir_inode_ops
= &nfs4_dir_inode_operations
,
3120 .file_inode_ops
= &nfs4_file_inode_operations
,
3121 .getroot
= nfs4_proc_get_root
,
3122 .getattr
= nfs4_proc_getattr
,
3123 .setattr
= nfs4_proc_setattr
,
3124 .lookup
= nfs4_proc_lookup
,
3125 .access
= nfs4_proc_access
,
3126 .readlink
= nfs4_proc_readlink
,
3127 .read
= nfs4_proc_read
,
3128 .write
= nfs4_proc_write
,
3129 .commit
= nfs4_proc_commit
,
3130 .create
= nfs4_proc_create
,
3131 .remove
= nfs4_proc_remove
,
3132 .unlink_setup
= nfs4_proc_unlink_setup
,
3133 .unlink_done
= nfs4_proc_unlink_done
,
3134 .rename
= nfs4_proc_rename
,
3135 .link
= nfs4_proc_link
,
3136 .symlink
= nfs4_proc_symlink
,
3137 .mkdir
= nfs4_proc_mkdir
,
3138 .rmdir
= nfs4_proc_remove
,
3139 .readdir
= nfs4_proc_readdir
,
3140 .mknod
= nfs4_proc_mknod
,
3141 .statfs
= nfs4_proc_statfs
,
3142 .fsinfo
= nfs4_proc_fsinfo
,
3143 .pathconf
= nfs4_proc_pathconf
,
3144 .decode_dirent
= nfs4_decode_dirent
,
3145 .read_setup
= nfs4_proc_read_setup
,
3146 .write_setup
= nfs4_proc_write_setup
,
3147 .commit_setup
= nfs4_proc_commit_setup
,
3148 .file_open
= nfs4_proc_file_open
,
3149 .file_release
= nfs4_proc_file_release
,
3150 .lock
= nfs4_proc_lock
,
3151 .clear_acl_cache
= nfs4_zap_acl_attr
,