4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/utsname.h>
40 #include <linux/delay.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/smp_lock.h>
49 #include <linux/namei.h>
50 #include <linux/mount.h>
53 #include "delegation.h"
57 #define NFSDBG_FACILITY NFSDBG_PROC
59 #define NFS4_POLL_RETRY_MIN (HZ/10)
60 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
64 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
65 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
66 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
67 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
69 /* Prevent leaks of NFSv4 errors into userland */
70 static int nfs4_map_errors(int err
)
73 dprintk("%s could not handle NFSv4 error %d\n",
81 * This is our standard bitmap for GETATTR requests.
83 const u32 nfs4_fattr_bitmap
[2] = {
88 | FATTR4_WORD0_FILEID
,
90 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
100 const u32 nfs4_statfs_bitmap
[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL
,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
109 const u32 nfs4_pathconf_bitmap
[2] = {
111 | FATTR4_WORD0_MAXNAME
,
115 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME
,
122 const u32 nfs4_fs_locations_bitmap
[2] = {
124 | FATTR4_WORD0_CHANGE
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS
,
130 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
141 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
142 struct nfs4_readdir_arg
*readdir
)
146 BUG_ON(readdir
->count
< 80);
148 readdir
->cookie
= cookie
;
149 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
154 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
165 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
168 *p
++ = xdr_one
; /* next */
169 *p
++ = xdr_zero
; /* cookie, first word */
170 *p
++ = xdr_one
; /* cookie, second word */
171 *p
++ = xdr_one
; /* entry len */
172 memcpy(p
, ".\0\0\0", 4); /* entry */
174 *p
++ = xdr_one
; /* bitmap length */
175 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
176 *p
++ = htonl(8); /* attribute buffer length */
177 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
180 *p
++ = xdr_one
; /* next */
181 *p
++ = xdr_zero
; /* cookie, first word */
182 *p
++ = xdr_two
; /* cookie, second word */
183 *p
++ = xdr_two
; /* entry len */
184 memcpy(p
, "..\0\0", 4); /* entry */
186 *p
++ = xdr_one
; /* bitmap length */
187 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
188 *p
++ = htonl(8); /* attribute buffer length */
189 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
191 readdir
->pgbase
= (char *)p
- (char *)start
;
192 readdir
->count
-= readdir
->pgbase
;
193 kunmap_atomic(start
, KM_USER0
);
196 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
202 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
203 nfs_wait_bit_killable
, TASK_KILLABLE
);
207 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
214 *timeout
= NFS4_POLL_RETRY_MIN
;
215 if (*timeout
> NFS4_POLL_RETRY_MAX
)
216 *timeout
= NFS4_POLL_RETRY_MAX
;
217 schedule_timeout_killable(*timeout
);
218 if (fatal_signal_pending(current
))
224 /* This is the error handling routine for processes that are allowed
227 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
229 struct nfs_client
*clp
= server
->nfs_client
;
230 struct nfs4_state
*state
= exception
->state
;
233 exception
->retry
= 0;
237 case -NFS4ERR_ADMIN_REVOKED
:
238 case -NFS4ERR_BAD_STATEID
:
239 case -NFS4ERR_OPENMODE
:
242 nfs4_state_mark_reclaim_nograce(clp
, state
);
243 case -NFS4ERR_STALE_CLIENTID
:
244 case -NFS4ERR_STALE_STATEID
:
245 case -NFS4ERR_EXPIRED
:
246 nfs4_schedule_state_recovery(clp
);
247 ret
= nfs4_wait_clnt_recover(clp
);
249 exception
->retry
= 1;
251 case -NFS4ERR_FILE_OPEN
:
254 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
257 case -NFS4ERR_OLD_STATEID
:
258 exception
->retry
= 1;
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret
);
265 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
267 struct nfs_client
*clp
= server
->nfs_client
;
268 spin_lock(&clp
->cl_lock
);
269 if (time_before(clp
->cl_last_renewal
,timestamp
))
270 clp
->cl_last_renewal
= timestamp
;
271 spin_unlock(&clp
->cl_lock
);
274 #if defined(CONFIG_NFS_V4_1)
276 int _nfs4_call_sync_session(struct nfs_server
*server
,
277 struct rpc_message
*msg
,
278 struct nfs4_sequence_args
*args
,
279 struct nfs4_sequence_res
*res
,
282 /* in preparation for setting up the sequence op */
283 return rpc_call_sync(server
->client
, msg
, 0);
286 #endif /* CONFIG_NFS_V4_1 */
288 int _nfs4_call_sync(struct nfs_server
*server
,
289 struct rpc_message
*msg
,
290 struct nfs4_sequence_args
*args
,
291 struct nfs4_sequence_res
*res
,
294 args
->sa_session
= res
->sr_session
= NULL
;
295 return rpc_call_sync(server
->client
, msg
, 0);
298 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
299 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
300 &(res)->seq_res, (cache_reply))
302 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
304 struct nfs_inode
*nfsi
= NFS_I(dir
);
306 spin_lock(&dir
->i_lock
);
307 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
308 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
309 nfs_force_lookup_revalidate(dir
);
310 nfsi
->change_attr
= cinfo
->after
;
311 spin_unlock(&dir
->i_lock
);
314 struct nfs4_opendata
{
316 struct nfs_openargs o_arg
;
317 struct nfs_openres o_res
;
318 struct nfs_open_confirmargs c_arg
;
319 struct nfs_open_confirmres c_res
;
320 struct nfs_fattr f_attr
;
321 struct nfs_fattr dir_attr
;
324 struct nfs4_state_owner
*owner
;
325 struct nfs4_state
*state
;
327 unsigned long timestamp
;
328 unsigned int rpc_done
: 1;
334 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
336 p
->o_res
.f_attr
= &p
->f_attr
;
337 p
->o_res
.dir_attr
= &p
->dir_attr
;
338 p
->o_res
.seqid
= p
->o_arg
.seqid
;
339 p
->c_res
.seqid
= p
->c_arg
.seqid
;
340 p
->o_res
.server
= p
->o_arg
.server
;
341 nfs_fattr_init(&p
->f_attr
);
342 nfs_fattr_init(&p
->dir_attr
);
345 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
346 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
347 const struct iattr
*attrs
)
349 struct dentry
*parent
= dget_parent(path
->dentry
);
350 struct inode
*dir
= parent
->d_inode
;
351 struct nfs_server
*server
= NFS_SERVER(dir
);
352 struct nfs4_opendata
*p
;
354 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
357 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
358 if (p
->o_arg
.seqid
== NULL
)
360 p
->path
.mnt
= mntget(path
->mnt
);
361 p
->path
.dentry
= dget(path
->dentry
);
364 atomic_inc(&sp
->so_count
);
365 p
->o_arg
.fh
= NFS_FH(dir
);
366 p
->o_arg
.open_flags
= flags
;
367 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
368 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
369 p
->o_arg
.id
= sp
->so_owner_id
.id
;
370 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
371 p
->o_arg
.server
= server
;
372 p
->o_arg
.bitmask
= server
->attr_bitmask
;
373 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
374 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
375 if (flags
& O_EXCL
) {
376 u32
*s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
379 } else if (flags
& O_CREAT
) {
380 p
->o_arg
.u
.attrs
= &p
->attrs
;
381 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
383 p
->c_arg
.fh
= &p
->o_res
.fh
;
384 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
385 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
386 nfs4_init_opendata_res(p
);
396 static void nfs4_opendata_free(struct kref
*kref
)
398 struct nfs4_opendata
*p
= container_of(kref
,
399 struct nfs4_opendata
, kref
);
401 nfs_free_seqid(p
->o_arg
.seqid
);
402 if (p
->state
!= NULL
)
403 nfs4_put_open_state(p
->state
);
404 nfs4_put_state_owner(p
->owner
);
410 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
413 kref_put(&p
->kref
, nfs4_opendata_free
);
416 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
420 ret
= rpc_wait_for_completion_task(task
);
424 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
428 if (open_mode
& O_EXCL
)
430 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
432 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0;
435 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0;
437 case FMODE_READ
|FMODE_WRITE
:
438 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0;
444 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
446 if ((delegation
->type
& fmode
) != fmode
)
448 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
450 nfs_mark_delegation_referenced(delegation
);
454 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
463 case FMODE_READ
|FMODE_WRITE
:
466 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
469 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
471 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
472 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
473 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
476 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
479 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
481 case FMODE_READ
|FMODE_WRITE
:
482 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
486 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
488 write_seqlock(&state
->seqlock
);
489 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
490 write_sequnlock(&state
->seqlock
);
493 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
496 * Protect the call to nfs4_state_set_mode_locked and
497 * serialise the stateid update
499 write_seqlock(&state
->seqlock
);
500 if (deleg_stateid
!= NULL
) {
501 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
502 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
504 if (open_stateid
!= NULL
)
505 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
506 write_sequnlock(&state
->seqlock
);
507 spin_lock(&state
->owner
->so_lock
);
508 update_open_stateflags(state
, fmode
);
509 spin_unlock(&state
->owner
->so_lock
);
512 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
514 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
515 struct nfs_delegation
*deleg_cur
;
518 fmode
&= (FMODE_READ
|FMODE_WRITE
);
521 deleg_cur
= rcu_dereference(nfsi
->delegation
);
522 if (deleg_cur
== NULL
)
525 spin_lock(&deleg_cur
->lock
);
526 if (nfsi
->delegation
!= deleg_cur
||
527 (deleg_cur
->type
& fmode
) != fmode
)
528 goto no_delegation_unlock
;
530 if (delegation
== NULL
)
531 delegation
= &deleg_cur
->stateid
;
532 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
533 goto no_delegation_unlock
;
535 nfs_mark_delegation_referenced(deleg_cur
);
536 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
538 no_delegation_unlock
:
539 spin_unlock(&deleg_cur
->lock
);
543 if (!ret
&& open_stateid
!= NULL
) {
544 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
552 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
554 struct nfs_delegation
*delegation
;
557 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
558 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
563 nfs_inode_return_delegation(inode
);
566 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
568 struct nfs4_state
*state
= opendata
->state
;
569 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
570 struct nfs_delegation
*delegation
;
571 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
572 fmode_t fmode
= opendata
->o_arg
.fmode
;
573 nfs4_stateid stateid
;
577 if (can_open_cached(state
, fmode
, open_mode
)) {
578 spin_lock(&state
->owner
->so_lock
);
579 if (can_open_cached(state
, fmode
, open_mode
)) {
580 update_open_stateflags(state
, fmode
);
581 spin_unlock(&state
->owner
->so_lock
);
582 goto out_return_state
;
584 spin_unlock(&state
->owner
->so_lock
);
587 delegation
= rcu_dereference(nfsi
->delegation
);
588 if (delegation
== NULL
||
589 !can_open_delegated(delegation
, fmode
)) {
593 /* Save the delegation */
594 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
596 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
601 /* Try to update the stateid using the delegation */
602 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
603 goto out_return_state
;
608 atomic_inc(&state
->count
);
612 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
615 struct nfs4_state
*state
= NULL
;
616 struct nfs_delegation
*delegation
;
619 if (!data
->rpc_done
) {
620 state
= nfs4_try_open_cached(data
);
625 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
627 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
628 ret
= PTR_ERR(inode
);
632 state
= nfs4_get_open_state(inode
, data
->owner
);
635 if (data
->o_res
.delegation_type
!= 0) {
636 int delegation_flags
= 0;
639 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
641 delegation_flags
= delegation
->flags
;
643 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
644 nfs_inode_set_delegation(state
->inode
,
645 data
->owner
->so_cred
,
648 nfs_inode_reclaim_delegation(state
->inode
,
649 data
->owner
->so_cred
,
653 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
664 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
666 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
667 struct nfs_open_context
*ctx
;
669 spin_lock(&state
->inode
->i_lock
);
670 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
671 if (ctx
->state
!= state
)
673 get_nfs_open_context(ctx
);
674 spin_unlock(&state
->inode
->i_lock
);
677 spin_unlock(&state
->inode
->i_lock
);
678 return ERR_PTR(-ENOENT
);
681 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
683 struct nfs4_opendata
*opendata
;
685 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
);
686 if (opendata
== NULL
)
687 return ERR_PTR(-ENOMEM
);
688 opendata
->state
= state
;
689 atomic_inc(&state
->count
);
693 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
695 struct nfs4_state
*newstate
;
698 opendata
->o_arg
.open_flags
= 0;
699 opendata
->o_arg
.fmode
= fmode
;
700 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
701 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
702 nfs4_init_opendata_res(opendata
);
703 ret
= _nfs4_proc_open(opendata
);
706 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
707 if (IS_ERR(newstate
))
708 return PTR_ERR(newstate
);
709 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
714 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
716 struct nfs4_state
*newstate
;
719 /* memory barrier prior to reading state->n_* */
720 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
722 if (state
->n_rdwr
!= 0) {
723 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
726 if (newstate
!= state
)
729 if (state
->n_wronly
!= 0) {
730 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
733 if (newstate
!= state
)
736 if (state
->n_rdonly
!= 0) {
737 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
740 if (newstate
!= state
)
744 * We may have performed cached opens for all three recoveries.
745 * Check if we need to update the current stateid.
747 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
748 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
749 write_seqlock(&state
->seqlock
);
750 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
751 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
752 write_sequnlock(&state
->seqlock
);
759 * reclaim state on the server after a reboot.
761 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
763 struct nfs_delegation
*delegation
;
764 struct nfs4_opendata
*opendata
;
765 fmode_t delegation_type
= 0;
768 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
769 if (IS_ERR(opendata
))
770 return PTR_ERR(opendata
);
771 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
772 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
774 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
775 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
776 delegation_type
= delegation
->type
;
778 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
779 status
= nfs4_open_recover(opendata
, state
);
780 nfs4_opendata_put(opendata
);
784 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
786 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
787 struct nfs4_exception exception
= { };
790 err
= _nfs4_do_open_reclaim(ctx
, state
);
791 if (err
!= -NFS4ERR_DELAY
)
793 nfs4_handle_exception(server
, err
, &exception
);
794 } while (exception
.retry
);
798 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
800 struct nfs_open_context
*ctx
;
803 ctx
= nfs4_state_find_open_context(state
);
806 ret
= nfs4_do_open_reclaim(ctx
, state
);
807 put_nfs_open_context(ctx
);
811 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
813 struct nfs4_opendata
*opendata
;
816 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
817 if (IS_ERR(opendata
))
818 return PTR_ERR(opendata
);
819 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
820 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
821 sizeof(opendata
->o_arg
.u
.delegation
.data
));
822 ret
= nfs4_open_recover(opendata
, state
);
823 nfs4_opendata_put(opendata
);
827 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
829 struct nfs4_exception exception
= { };
830 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
833 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
837 case -NFS4ERR_STALE_CLIENTID
:
838 case -NFS4ERR_STALE_STATEID
:
839 case -NFS4ERR_EXPIRED
:
840 /* Don't recall a delegation if it was lost */
841 nfs4_schedule_state_recovery(server
->nfs_client
);
844 err
= nfs4_handle_exception(server
, err
, &exception
);
845 } while (exception
.retry
);
849 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
851 struct nfs4_opendata
*data
= calldata
;
853 data
->rpc_status
= task
->tk_status
;
854 if (RPC_ASSASSINATED(task
))
856 if (data
->rpc_status
== 0) {
857 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
858 sizeof(data
->o_res
.stateid
.data
));
859 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
860 renew_lease(data
->o_res
.server
, data
->timestamp
);
865 static void nfs4_open_confirm_release(void *calldata
)
867 struct nfs4_opendata
*data
= calldata
;
868 struct nfs4_state
*state
= NULL
;
870 /* If this request hasn't been cancelled, do nothing */
871 if (data
->cancelled
== 0)
873 /* In case of error, no cleanup! */
876 state
= nfs4_opendata_to_nfs4_state(data
);
878 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
880 nfs4_opendata_put(data
);
883 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
884 .rpc_call_done
= nfs4_open_confirm_done
,
885 .rpc_release
= nfs4_open_confirm_release
,
889 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
891 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
893 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
894 struct rpc_task
*task
;
895 struct rpc_message msg
= {
896 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
897 .rpc_argp
= &data
->c_arg
,
898 .rpc_resp
= &data
->c_res
,
899 .rpc_cred
= data
->owner
->so_cred
,
901 struct rpc_task_setup task_setup_data
= {
902 .rpc_client
= server
->client
,
904 .callback_ops
= &nfs4_open_confirm_ops
,
905 .callback_data
= data
,
906 .workqueue
= nfsiod_workqueue
,
907 .flags
= RPC_TASK_ASYNC
,
911 kref_get(&data
->kref
);
913 data
->rpc_status
= 0;
914 data
->timestamp
= jiffies
;
915 task
= rpc_run_task(&task_setup_data
);
917 return PTR_ERR(task
);
918 status
= nfs4_wait_for_completion_rpc_task(task
);
923 status
= data
->rpc_status
;
928 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
930 struct nfs4_opendata
*data
= calldata
;
931 struct nfs4_state_owner
*sp
= data
->owner
;
933 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
936 * Check if we still need to send an OPEN call, or if we can use
937 * a delegation instead.
939 if (data
->state
!= NULL
) {
940 struct nfs_delegation
*delegation
;
942 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
945 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
946 if (delegation
!= NULL
&&
947 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
953 /* Update sequence id. */
954 data
->o_arg
.id
= sp
->so_owner_id
.id
;
955 data
->o_arg
.clientid
= sp
->so_client
->cl_clientid
;
956 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
957 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
958 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
960 data
->timestamp
= jiffies
;
961 rpc_call_start(task
);
964 task
->tk_action
= NULL
;
968 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
970 struct nfs4_opendata
*data
= calldata
;
972 data
->rpc_status
= task
->tk_status
;
973 if (RPC_ASSASSINATED(task
))
975 if (task
->tk_status
== 0) {
976 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
980 data
->rpc_status
= -ELOOP
;
983 data
->rpc_status
= -EISDIR
;
986 data
->rpc_status
= -ENOTDIR
;
988 renew_lease(data
->o_res
.server
, data
->timestamp
);
989 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
990 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
995 static void nfs4_open_release(void *calldata
)
997 struct nfs4_opendata
*data
= calldata
;
998 struct nfs4_state
*state
= NULL
;
1000 /* If this request hasn't been cancelled, do nothing */
1001 if (data
->cancelled
== 0)
1003 /* In case of error, no cleanup! */
1004 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1006 /* In case we need an open_confirm, no cleanup! */
1007 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1009 state
= nfs4_opendata_to_nfs4_state(data
);
1011 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1013 nfs4_opendata_put(data
);
1016 static const struct rpc_call_ops nfs4_open_ops
= {
1017 .rpc_call_prepare
= nfs4_open_prepare
,
1018 .rpc_call_done
= nfs4_open_done
,
1019 .rpc_release
= nfs4_open_release
,
1023 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1025 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1027 struct inode
*dir
= data
->dir
->d_inode
;
1028 struct nfs_server
*server
= NFS_SERVER(dir
);
1029 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1030 struct nfs_openres
*o_res
= &data
->o_res
;
1031 struct rpc_task
*task
;
1032 struct rpc_message msg
= {
1033 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1036 .rpc_cred
= data
->owner
->so_cred
,
1038 struct rpc_task_setup task_setup_data
= {
1039 .rpc_client
= server
->client
,
1040 .rpc_message
= &msg
,
1041 .callback_ops
= &nfs4_open_ops
,
1042 .callback_data
= data
,
1043 .workqueue
= nfsiod_workqueue
,
1044 .flags
= RPC_TASK_ASYNC
,
1048 kref_get(&data
->kref
);
1050 data
->rpc_status
= 0;
1051 data
->cancelled
= 0;
1052 task
= rpc_run_task(&task_setup_data
);
1054 return PTR_ERR(task
);
1055 status
= nfs4_wait_for_completion_rpc_task(task
);
1057 data
->cancelled
= 1;
1060 status
= data
->rpc_status
;
1062 if (status
!= 0 || !data
->rpc_done
)
1065 if (o_res
->fh
.size
== 0)
1066 _nfs4_proc_lookup(dir
, o_arg
->name
, &o_res
->fh
, o_res
->f_attr
);
1068 if (o_arg
->open_flags
& O_CREAT
) {
1069 update_changeattr(dir
, &o_res
->cinfo
);
1070 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1072 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1073 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1074 status
= _nfs4_proc_open_confirm(data
);
1078 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1079 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1083 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1085 struct nfs_client
*clp
= server
->nfs_client
;
1089 ret
= nfs4_wait_clnt_recover(clp
);
1092 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1093 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1095 nfs4_schedule_state_recovery(clp
);
1102 * reclaim state on the server after a network partition.
1103 * Assumes caller holds the appropriate lock
1105 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1107 struct nfs4_opendata
*opendata
;
1110 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1111 if (IS_ERR(opendata
))
1112 return PTR_ERR(opendata
);
1113 ret
= nfs4_open_recover(opendata
, state
);
1115 d_drop(ctx
->path
.dentry
);
1116 nfs4_opendata_put(opendata
);
1120 static inline int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1122 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1123 struct nfs4_exception exception
= { };
1127 err
= _nfs4_open_expired(ctx
, state
);
1128 if (err
!= -NFS4ERR_DELAY
)
1130 nfs4_handle_exception(server
, err
, &exception
);
1131 } while (exception
.retry
);
1135 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1137 struct nfs_open_context
*ctx
;
1140 ctx
= nfs4_state_find_open_context(state
);
1142 return PTR_ERR(ctx
);
1143 ret
= nfs4_do_open_expired(ctx
, state
);
1144 put_nfs_open_context(ctx
);
1149 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1150 * fields corresponding to attributes that were used to store the verifier.
1151 * Make sure we clobber those fields in the later setattr call
1153 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1155 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1156 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1157 sattr
->ia_valid
|= ATTR_ATIME
;
1159 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1160 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1161 sattr
->ia_valid
|= ATTR_MTIME
;
1165 * Returns a referenced nfs4_state
1167 static int _nfs4_do_open(struct inode
*dir
, struct path
*path
, fmode_t fmode
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
, struct nfs4_state
**res
)
1169 struct nfs4_state_owner
*sp
;
1170 struct nfs4_state
*state
= NULL
;
1171 struct nfs_server
*server
= NFS_SERVER(dir
);
1172 struct nfs4_opendata
*opendata
;
1175 /* Protect against reboot recovery conflicts */
1177 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1178 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1181 status
= nfs4_recover_expired_lease(server
);
1183 goto err_put_state_owner
;
1184 if (path
->dentry
->d_inode
!= NULL
)
1185 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1187 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
);
1188 if (opendata
== NULL
)
1189 goto err_put_state_owner
;
1191 if (path
->dentry
->d_inode
!= NULL
)
1192 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1194 status
= _nfs4_proc_open(opendata
);
1196 goto err_opendata_put
;
1198 if (opendata
->o_arg
.open_flags
& O_EXCL
)
1199 nfs4_exclusive_attrset(opendata
, sattr
);
1201 state
= nfs4_opendata_to_nfs4_state(opendata
);
1202 status
= PTR_ERR(state
);
1204 goto err_opendata_put
;
1205 nfs4_opendata_put(opendata
);
1206 nfs4_put_state_owner(sp
);
1210 nfs4_opendata_put(opendata
);
1211 err_put_state_owner
:
1212 nfs4_put_state_owner(sp
);
1219 static struct nfs4_state
*nfs4_do_open(struct inode
*dir
, struct path
*path
, fmode_t fmode
, int flags
, struct iattr
*sattr
, struct rpc_cred
*cred
)
1221 struct nfs4_exception exception
= { };
1222 struct nfs4_state
*res
;
1226 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1229 /* NOTE: BAD_SEQID means the server and client disagree about the
1230 * book-keeping w.r.t. state-changing operations
1231 * (OPEN/CLOSE/LOCK/LOCKU...)
1232 * It is actually a sign of a bug on the client or on the server.
1234 * If we receive a BAD_SEQID error in the particular case of
1235 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1236 * have unhashed the old state_owner for us, and that we can
1237 * therefore safely retry using a new one. We should still warn
1238 * the user though...
1240 if (status
== -NFS4ERR_BAD_SEQID
) {
1241 printk(KERN_WARNING
"NFS: v4 server %s "
1242 " returned a bad sequence-id error!\n",
1243 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1244 exception
.retry
= 1;
1248 * BAD_STATEID on OPEN means that the server cancelled our
1249 * state before it received the OPEN_CONFIRM.
1250 * Recover by retrying the request as per the discussion
1251 * on Page 181 of RFC3530.
1253 if (status
== -NFS4ERR_BAD_STATEID
) {
1254 exception
.retry
= 1;
1257 if (status
== -EAGAIN
) {
1258 /* We must have found a delegation */
1259 exception
.retry
= 1;
1262 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1263 status
, &exception
));
1264 } while (exception
.retry
);
1268 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1269 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1270 struct nfs4_state
*state
)
1272 struct nfs_server
*server
= NFS_SERVER(inode
);
1273 struct nfs_setattrargs arg
= {
1274 .fh
= NFS_FH(inode
),
1277 .bitmask
= server
->attr_bitmask
,
1279 struct nfs_setattrres res
= {
1283 struct rpc_message msg
= {
1284 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1289 unsigned long timestamp
= jiffies
;
1292 nfs_fattr_init(fattr
);
1294 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1295 /* Use that stateid */
1296 } else if (state
!= NULL
) {
1297 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1299 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1301 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1302 if (status
== 0 && state
!= NULL
)
1303 renew_lease(server
, timestamp
);
1307 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1308 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1309 struct nfs4_state
*state
)
1311 struct nfs_server
*server
= NFS_SERVER(inode
);
1312 struct nfs4_exception exception
= { };
1315 err
= nfs4_handle_exception(server
,
1316 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1318 } while (exception
.retry
);
1322 struct nfs4_closedata
{
1324 struct inode
*inode
;
1325 struct nfs4_state
*state
;
1326 struct nfs_closeargs arg
;
1327 struct nfs_closeres res
;
1328 struct nfs_fattr fattr
;
1329 unsigned long timestamp
;
1332 static void nfs4_free_closedata(void *data
)
1334 struct nfs4_closedata
*calldata
= data
;
1335 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1337 nfs4_put_open_state(calldata
->state
);
1338 nfs_free_seqid(calldata
->arg
.seqid
);
1339 nfs4_put_state_owner(sp
);
1340 path_put(&calldata
->path
);
1344 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1346 struct nfs4_closedata
*calldata
= data
;
1347 struct nfs4_state
*state
= calldata
->state
;
1348 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1350 if (RPC_ASSASSINATED(task
))
1352 /* hmm. we are done with the inode, and in the process of freeing
1353 * the state_owner. we keep this around to process errors
1355 switch (task
->tk_status
) {
1357 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1358 renew_lease(server
, calldata
->timestamp
);
1360 case -NFS4ERR_STALE_STATEID
:
1361 case -NFS4ERR_OLD_STATEID
:
1362 case -NFS4ERR_BAD_STATEID
:
1363 case -NFS4ERR_EXPIRED
:
1364 if (calldata
->arg
.fmode
== 0)
1367 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
) {
1368 rpc_restart_call(task
);
1372 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1375 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1377 struct nfs4_closedata
*calldata
= data
;
1378 struct nfs4_state
*state
= calldata
->state
;
1379 int clear_rd
, clear_wr
, clear_rdwr
;
1381 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1384 clear_rd
= clear_wr
= clear_rdwr
= 0;
1385 spin_lock(&state
->owner
->so_lock
);
1386 /* Calculate the change in open mode */
1387 if (state
->n_rdwr
== 0) {
1388 if (state
->n_rdonly
== 0) {
1389 clear_rd
|= test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1390 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1392 if (state
->n_wronly
== 0) {
1393 clear_wr
|= test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1394 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1397 spin_unlock(&state
->owner
->so_lock
);
1398 if (!clear_rd
&& !clear_wr
&& !clear_rdwr
) {
1399 /* Note: exit _without_ calling nfs4_close_done */
1400 task
->tk_action
= NULL
;
1403 nfs_fattr_init(calldata
->res
.fattr
);
1404 if (test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0) {
1405 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1406 calldata
->arg
.fmode
= FMODE_READ
;
1407 } else if (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0) {
1408 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1409 calldata
->arg
.fmode
= FMODE_WRITE
;
1411 calldata
->timestamp
= jiffies
;
1412 rpc_call_start(task
);
1415 static const struct rpc_call_ops nfs4_close_ops
= {
1416 .rpc_call_prepare
= nfs4_close_prepare
,
1417 .rpc_call_done
= nfs4_close_done
,
1418 .rpc_release
= nfs4_free_closedata
,
1422 * It is possible for data to be read/written from a mem-mapped file
1423 * after the sys_close call (which hits the vfs layer as a flush).
1424 * This means that we can't safely call nfsv4 close on a file until
1425 * the inode is cleared. This in turn means that we are not good
1426 * NFSv4 citizens - we do not indicate to the server to update the file's
1427 * share state even when we are done with one of the three share
1428 * stateid's in the inode.
1430 * NOTE: Caller must be holding the sp->so_owner semaphore!
1432 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, int wait
)
1434 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1435 struct nfs4_closedata
*calldata
;
1436 struct nfs4_state_owner
*sp
= state
->owner
;
1437 struct rpc_task
*task
;
1438 struct rpc_message msg
= {
1439 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1440 .rpc_cred
= state
->owner
->so_cred
,
1442 struct rpc_task_setup task_setup_data
= {
1443 .rpc_client
= server
->client
,
1444 .rpc_message
= &msg
,
1445 .callback_ops
= &nfs4_close_ops
,
1446 .workqueue
= nfsiod_workqueue
,
1447 .flags
= RPC_TASK_ASYNC
,
1449 int status
= -ENOMEM
;
1451 calldata
= kmalloc(sizeof(*calldata
), GFP_KERNEL
);
1452 if (calldata
== NULL
)
1454 calldata
->inode
= state
->inode
;
1455 calldata
->state
= state
;
1456 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1457 calldata
->arg
.stateid
= &state
->open_stateid
;
1458 /* Serialization for the sequence id */
1459 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
1460 if (calldata
->arg
.seqid
== NULL
)
1461 goto out_free_calldata
;
1462 calldata
->arg
.fmode
= 0;
1463 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1464 calldata
->res
.fattr
= &calldata
->fattr
;
1465 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1466 calldata
->res
.server
= server
;
1467 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1468 calldata
->path
.mnt
= mntget(path
->mnt
);
1469 calldata
->path
.dentry
= dget(path
->dentry
);
1471 msg
.rpc_argp
= &calldata
->arg
,
1472 msg
.rpc_resp
= &calldata
->res
,
1473 task_setup_data
.callback_data
= calldata
;
1474 task
= rpc_run_task(&task_setup_data
);
1476 return PTR_ERR(task
);
1479 status
= rpc_wait_for_completion_task(task
);
1485 nfs4_put_open_state(state
);
1486 nfs4_put_state_owner(sp
);
1490 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1495 /* If the open_intent is for execute, we have an extra check to make */
1496 if (fmode
& FMODE_EXEC
) {
1497 ret
= nfs_may_open(state
->inode
,
1498 state
->owner
->so_cred
,
1499 nd
->intent
.open
.flags
);
1503 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1504 if (!IS_ERR(filp
)) {
1505 struct nfs_open_context
*ctx
;
1506 ctx
= nfs_file_open_context(filp
);
1510 ret
= PTR_ERR(filp
);
1512 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
1517 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1519 struct path path
= {
1520 .mnt
= nd
->path
.mnt
,
1523 struct dentry
*parent
;
1525 struct rpc_cred
*cred
;
1526 struct nfs4_state
*state
;
1528 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
1530 if (nd
->flags
& LOOKUP_CREATE
) {
1531 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
1532 attr
.ia_valid
= ATTR_MODE
;
1533 if (!IS_POSIXACL(dir
))
1534 attr
.ia_mode
&= ~current_umask();
1537 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
1540 cred
= rpc_lookup_cred();
1542 return (struct dentry
*)cred
;
1543 parent
= dentry
->d_parent
;
1544 /* Protect against concurrent sillydeletes */
1545 nfs_block_sillyrename(parent
);
1546 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
1548 if (IS_ERR(state
)) {
1549 if (PTR_ERR(state
) == -ENOENT
) {
1550 d_add(dentry
, NULL
);
1551 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1553 nfs_unblock_sillyrename(parent
);
1554 return (struct dentry
*)state
;
1556 res
= d_add_unique(dentry
, igrab(state
->inode
));
1559 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
1560 nfs_unblock_sillyrename(parent
);
1561 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1566 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
1568 struct path path
= {
1569 .mnt
= nd
->path
.mnt
,
1572 struct rpc_cred
*cred
;
1573 struct nfs4_state
*state
;
1574 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
1576 cred
= rpc_lookup_cred();
1578 return PTR_ERR(cred
);
1579 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
1581 if (IS_ERR(state
)) {
1582 switch (PTR_ERR(state
)) {
1588 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
1594 if (state
->inode
== dentry
->d_inode
) {
1595 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1596 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1599 nfs4_close_sync(&path
, state
, fmode
);
1605 void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
1607 if (ctx
->state
== NULL
)
1610 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
1612 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
1615 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1617 struct nfs4_server_caps_arg args
= {
1620 struct nfs4_server_caps_res res
= {};
1621 struct rpc_message msg
= {
1622 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
1628 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1630 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
1631 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
1632 server
->caps
|= NFS_CAP_ACLS
;
1633 if (res
.has_links
!= 0)
1634 server
->caps
|= NFS_CAP_HARDLINKS
;
1635 if (res
.has_symlinks
!= 0)
1636 server
->caps
|= NFS_CAP_SYMLINKS
;
1637 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
1638 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
1639 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
1640 server
->acl_bitmask
= res
.acl_bitmask
;
1646 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
1648 struct nfs4_exception exception
= { };
1651 err
= nfs4_handle_exception(server
,
1652 _nfs4_server_capabilities(server
, fhandle
),
1654 } while (exception
.retry
);
1658 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1659 struct nfs_fsinfo
*info
)
1661 struct nfs4_lookup_root_arg args
= {
1662 .bitmask
= nfs4_fattr_bitmap
,
1664 struct nfs4_lookup_res res
= {
1666 .fattr
= info
->fattr
,
1669 struct rpc_message msg
= {
1670 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
1674 nfs_fattr_init(info
->fattr
);
1675 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1678 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1679 struct nfs_fsinfo
*info
)
1681 struct nfs4_exception exception
= { };
1684 err
= nfs4_handle_exception(server
,
1685 _nfs4_lookup_root(server
, fhandle
, info
),
1687 } while (exception
.retry
);
1692 * get the file handle for the "/" directory on the server
1694 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
1695 struct nfs_fsinfo
*info
)
1699 status
= nfs4_lookup_root(server
, fhandle
, info
);
1701 status
= nfs4_server_capabilities(server
, fhandle
);
1703 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
1704 return nfs4_map_errors(status
);
1708 * Get locations and (maybe) other attributes of a referral.
1709 * Note that we'll actually follow the referral later when
1710 * we detect fsid mismatch in inode revalidation
1712 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
1714 int status
= -ENOMEM
;
1715 struct page
*page
= NULL
;
1716 struct nfs4_fs_locations
*locations
= NULL
;
1718 page
= alloc_page(GFP_KERNEL
);
1721 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
1722 if (locations
== NULL
)
1725 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
1728 /* Make sure server returned a different fsid for the referral */
1729 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
1730 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
1735 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
1736 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
1738 fattr
->mode
= S_IFDIR
;
1739 memset(fhandle
, 0, sizeof(struct nfs_fh
));
1748 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1750 struct nfs4_getattr_arg args
= {
1752 .bitmask
= server
->attr_bitmask
,
1754 struct nfs4_getattr_res res
= {
1758 struct rpc_message msg
= {
1759 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
1764 nfs_fattr_init(fattr
);
1765 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1768 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1770 struct nfs4_exception exception
= { };
1773 err
= nfs4_handle_exception(server
,
1774 _nfs4_proc_getattr(server
, fhandle
, fattr
),
1776 } while (exception
.retry
);
1781 * The file is not closed if it is opened due to the a request to change
1782 * the size of the file. The open call will not be needed once the
1783 * VFS layer lookup-intents are implemented.
1785 * Close is called when the inode is destroyed.
1786 * If we haven't opened the file for O_WRONLY, we
1787 * need to in the size_change case to obtain a stateid.
1790 * Because OPEN is always done by name in nfsv4, it is
1791 * possible that we opened a different file by the same
1792 * name. We can recognize this race condition, but we
1793 * can't do anything about it besides returning an error.
1795 * This will be fixed with VFS changes (lookup-intent).
1798 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
1799 struct iattr
*sattr
)
1801 struct inode
*inode
= dentry
->d_inode
;
1802 struct rpc_cred
*cred
= NULL
;
1803 struct nfs4_state
*state
= NULL
;
1806 nfs_fattr_init(fattr
);
1808 /* Search for an existing open(O_WRITE) file */
1809 if (sattr
->ia_valid
& ATTR_FILE
) {
1810 struct nfs_open_context
*ctx
;
1812 ctx
= nfs_file_open_context(sattr
->ia_file
);
1819 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
1821 nfs_setattr_update_inode(inode
, sattr
);
1825 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
1826 const struct qstr
*name
, struct nfs_fh
*fhandle
,
1827 struct nfs_fattr
*fattr
)
1830 struct nfs4_lookup_arg args
= {
1831 .bitmask
= server
->attr_bitmask
,
1835 struct nfs4_lookup_res res
= {
1840 struct rpc_message msg
= {
1841 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
1846 nfs_fattr_init(fattr
);
1848 dprintk("NFS call lookupfh %s\n", name
->name
);
1849 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1850 dprintk("NFS reply lookupfh: %d\n", status
);
1854 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
1855 struct qstr
*name
, struct nfs_fh
*fhandle
,
1856 struct nfs_fattr
*fattr
)
1858 struct nfs4_exception exception
= { };
1861 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
1863 if (err
== -NFS4ERR_MOVED
) {
1867 err
= nfs4_handle_exception(server
, err
, &exception
);
1868 } while (exception
.retry
);
1872 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
1873 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1877 dprintk("NFS call lookup %s\n", name
->name
);
1878 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
1879 if (status
== -NFS4ERR_MOVED
)
1880 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
1881 dprintk("NFS reply lookup: %d\n", status
);
1885 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
1887 struct nfs4_exception exception
= { };
1890 err
= nfs4_handle_exception(NFS_SERVER(dir
),
1891 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
1893 } while (exception
.retry
);
1897 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
1899 struct nfs_server
*server
= NFS_SERVER(inode
);
1900 struct nfs_fattr fattr
;
1901 struct nfs4_accessargs args
= {
1902 .fh
= NFS_FH(inode
),
1903 .bitmask
= server
->attr_bitmask
,
1905 struct nfs4_accessres res
= {
1909 struct rpc_message msg
= {
1910 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
1913 .rpc_cred
= entry
->cred
,
1915 int mode
= entry
->mask
;
1919 * Determine which access bits we want to ask for...
1921 if (mode
& MAY_READ
)
1922 args
.access
|= NFS4_ACCESS_READ
;
1923 if (S_ISDIR(inode
->i_mode
)) {
1924 if (mode
& MAY_WRITE
)
1925 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
1926 if (mode
& MAY_EXEC
)
1927 args
.access
|= NFS4_ACCESS_LOOKUP
;
1929 if (mode
& MAY_WRITE
)
1930 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
1931 if (mode
& MAY_EXEC
)
1932 args
.access
|= NFS4_ACCESS_EXECUTE
;
1934 nfs_fattr_init(&fattr
);
1935 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
1938 if (res
.access
& NFS4_ACCESS_READ
)
1939 entry
->mask
|= MAY_READ
;
1940 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
1941 entry
->mask
|= MAY_WRITE
;
1942 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
1943 entry
->mask
|= MAY_EXEC
;
1944 nfs_refresh_inode(inode
, &fattr
);
1949 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
1951 struct nfs4_exception exception
= { };
1954 err
= nfs4_handle_exception(NFS_SERVER(inode
),
1955 _nfs4_proc_access(inode
, entry
),
1957 } while (exception
.retry
);
1962 * TODO: For the time being, we don't try to get any attributes
1963 * along with any of the zero-copy operations READ, READDIR,
1966 * In the case of the first three, we want to put the GETATTR
1967 * after the read-type operation -- this is because it is hard
1968 * to predict the length of a GETATTR response in v4, and thus
1969 * align the READ data correctly. This means that the GETATTR
1970 * may end up partially falling into the page cache, and we should
1971 * shift it into the 'tail' of the xdr_buf before processing.
1972 * To do this efficiently, we need to know the total length
1973 * of data received, which doesn't seem to be available outside
1976 * In the case of WRITE, we also want to put the GETATTR after
1977 * the operation -- in this case because we want to make sure
1978 * we get the post-operation mtime and size. This means that
1979 * we can't use xdr_encode_pages() as written: we need a variant
1980 * of it which would leave room in the 'tail' iovec.
1982 * Both of these changes to the XDR layer would in fact be quite
1983 * minor, but I decided to leave them for a subsequent patch.
1985 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
1986 unsigned int pgbase
, unsigned int pglen
)
1988 struct nfs4_readlink args
= {
1989 .fh
= NFS_FH(inode
),
1994 struct nfs4_readlink_res res
;
1995 struct rpc_message msg
= {
1996 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2001 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2004 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2005 unsigned int pgbase
, unsigned int pglen
)
2007 struct nfs4_exception exception
= { };
2010 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2011 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2013 } while (exception
.retry
);
2019 * We will need to arrange for the VFS layer to provide an atomic open.
2020 * Until then, this create/open method is prone to inefficiency and race
2021 * conditions due to the lookup, create, and open VFS calls from sys_open()
2022 * placed on the wire.
2024 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2025 * The file will be opened again in the subsequent VFS open call
2026 * (nfs4_proc_file_open).
2028 * The open for read will just hang around to be used by any process that
2029 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2033 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2034 int flags
, struct nameidata
*nd
)
2036 struct path path
= {
2037 .mnt
= nd
->path
.mnt
,
2040 struct nfs4_state
*state
;
2041 struct rpc_cred
*cred
;
2042 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2045 cred
= rpc_lookup_cred();
2047 status
= PTR_ERR(cred
);
2050 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2052 if (IS_ERR(state
)) {
2053 status
= PTR_ERR(state
);
2056 d_add(dentry
, igrab(state
->inode
));
2057 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2058 if (flags
& O_EXCL
) {
2059 struct nfs_fattr fattr
;
2060 status
= nfs4_do_setattr(state
->inode
, cred
, &fattr
, sattr
, state
);
2062 nfs_setattr_update_inode(state
->inode
, sattr
);
2063 nfs_post_op_update_inode(state
->inode
, &fattr
);
2065 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2066 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2068 nfs4_close_sync(&path
, state
, fmode
);
2075 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2077 struct nfs_server
*server
= NFS_SERVER(dir
);
2078 struct nfs_removeargs args
= {
2080 .name
.len
= name
->len
,
2081 .name
.name
= name
->name
,
2082 .bitmask
= server
->attr_bitmask
,
2084 struct nfs_removeres res
= {
2087 struct rpc_message msg
= {
2088 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2094 nfs_fattr_init(&res
.dir_attr
);
2095 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2097 update_changeattr(dir
, &res
.cinfo
);
2098 nfs_post_op_update_inode(dir
, &res
.dir_attr
);
2103 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2105 struct nfs4_exception exception
= { };
2108 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2109 _nfs4_proc_remove(dir
, name
),
2111 } while (exception
.retry
);
2115 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2117 struct nfs_server
*server
= NFS_SERVER(dir
);
2118 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2119 struct nfs_removeres
*res
= msg
->rpc_resp
;
2121 args
->bitmask
= server
->cache_consistency_bitmask
;
2122 res
->server
= server
;
2123 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2126 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2128 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2130 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2132 update_changeattr(dir
, &res
->cinfo
);
2133 nfs_post_op_update_inode(dir
, &res
->dir_attr
);
2137 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2138 struct inode
*new_dir
, struct qstr
*new_name
)
2140 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2141 struct nfs4_rename_arg arg
= {
2142 .old_dir
= NFS_FH(old_dir
),
2143 .new_dir
= NFS_FH(new_dir
),
2144 .old_name
= old_name
,
2145 .new_name
= new_name
,
2146 .bitmask
= server
->attr_bitmask
,
2148 struct nfs_fattr old_fattr
, new_fattr
;
2149 struct nfs4_rename_res res
= {
2151 .old_fattr
= &old_fattr
,
2152 .new_fattr
= &new_fattr
,
2154 struct rpc_message msg
= {
2155 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2161 nfs_fattr_init(res
.old_fattr
);
2162 nfs_fattr_init(res
.new_fattr
);
2163 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2166 update_changeattr(old_dir
, &res
.old_cinfo
);
2167 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2168 update_changeattr(new_dir
, &res
.new_cinfo
);
2169 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2174 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2175 struct inode
*new_dir
, struct qstr
*new_name
)
2177 struct nfs4_exception exception
= { };
2180 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2181 _nfs4_proc_rename(old_dir
, old_name
,
2184 } while (exception
.retry
);
2188 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2190 struct nfs_server
*server
= NFS_SERVER(inode
);
2191 struct nfs4_link_arg arg
= {
2192 .fh
= NFS_FH(inode
),
2193 .dir_fh
= NFS_FH(dir
),
2195 .bitmask
= server
->attr_bitmask
,
2197 struct nfs_fattr fattr
, dir_attr
;
2198 struct nfs4_link_res res
= {
2201 .dir_attr
= &dir_attr
,
2203 struct rpc_message msg
= {
2204 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2210 nfs_fattr_init(res
.fattr
);
2211 nfs_fattr_init(res
.dir_attr
);
2212 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2214 update_changeattr(dir
, &res
.cinfo
);
2215 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2216 nfs_post_op_update_inode(inode
, res
.fattr
);
2222 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2224 struct nfs4_exception exception
= { };
2227 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2228 _nfs4_proc_link(inode
, dir
, name
),
2230 } while (exception
.retry
);
2234 struct nfs4_createdata
{
2235 struct rpc_message msg
;
2236 struct nfs4_create_arg arg
;
2237 struct nfs4_create_res res
;
2239 struct nfs_fattr fattr
;
2240 struct nfs_fattr dir_fattr
;
2243 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2244 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2246 struct nfs4_createdata
*data
;
2248 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2250 struct nfs_server
*server
= NFS_SERVER(dir
);
2252 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2253 data
->msg
.rpc_argp
= &data
->arg
;
2254 data
->msg
.rpc_resp
= &data
->res
;
2255 data
->arg
.dir_fh
= NFS_FH(dir
);
2256 data
->arg
.server
= server
;
2257 data
->arg
.name
= name
;
2258 data
->arg
.attrs
= sattr
;
2259 data
->arg
.ftype
= ftype
;
2260 data
->arg
.bitmask
= server
->attr_bitmask
;
2261 data
->res
.server
= server
;
2262 data
->res
.fh
= &data
->fh
;
2263 data
->res
.fattr
= &data
->fattr
;
2264 data
->res
.dir_fattr
= &data
->dir_fattr
;
2265 nfs_fattr_init(data
->res
.fattr
);
2266 nfs_fattr_init(data
->res
.dir_fattr
);
2271 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2273 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2274 &data
->arg
, &data
->res
, 1);
2276 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2277 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2278 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2283 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2288 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2289 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2291 struct nfs4_createdata
*data
;
2292 int status
= -ENAMETOOLONG
;
2294 if (len
> NFS4_MAXPATHLEN
)
2298 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2302 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2303 data
->arg
.u
.symlink
.pages
= &page
;
2304 data
->arg
.u
.symlink
.len
= len
;
2306 status
= nfs4_do_create(dir
, dentry
, data
);
2308 nfs4_free_createdata(data
);
2313 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2314 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2316 struct nfs4_exception exception
= { };
2319 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2320 _nfs4_proc_symlink(dir
, dentry
, page
,
2323 } while (exception
.retry
);
2327 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2328 struct iattr
*sattr
)
2330 struct nfs4_createdata
*data
;
2331 int status
= -ENOMEM
;
2333 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2337 status
= nfs4_do_create(dir
, dentry
, data
);
2339 nfs4_free_createdata(data
);
2344 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2345 struct iattr
*sattr
)
2347 struct nfs4_exception exception
= { };
2350 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2351 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2353 } while (exception
.retry
);
2357 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2358 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2360 struct inode
*dir
= dentry
->d_inode
;
2361 struct nfs4_readdir_arg args
= {
2366 .bitmask
= NFS_SERVER(dentry
->d_inode
)->cache_consistency_bitmask
,
2368 struct nfs4_readdir_res res
;
2369 struct rpc_message msg
= {
2370 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2377 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2378 dentry
->d_parent
->d_name
.name
,
2379 dentry
->d_name
.name
,
2380 (unsigned long long)cookie
);
2381 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2382 res
.pgbase
= args
.pgbase
;
2383 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2385 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2387 nfs_invalidate_atime(dir
);
2389 dprintk("%s: returns %d\n", __func__
, status
);
2393 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2394 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2396 struct nfs4_exception exception
= { };
2399 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2400 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2403 } while (exception
.retry
);
2407 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2408 struct iattr
*sattr
, dev_t rdev
)
2410 struct nfs4_createdata
*data
;
2411 int mode
= sattr
->ia_mode
;
2412 int status
= -ENOMEM
;
2414 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2415 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2417 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2422 data
->arg
.ftype
= NF4FIFO
;
2423 else if (S_ISBLK(mode
)) {
2424 data
->arg
.ftype
= NF4BLK
;
2425 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2426 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2428 else if (S_ISCHR(mode
)) {
2429 data
->arg
.ftype
= NF4CHR
;
2430 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2431 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2434 status
= nfs4_do_create(dir
, dentry
, data
);
2436 nfs4_free_createdata(data
);
2441 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2442 struct iattr
*sattr
, dev_t rdev
)
2444 struct nfs4_exception exception
= { };
2447 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2448 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2450 } while (exception
.retry
);
2454 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2455 struct nfs_fsstat
*fsstat
)
2457 struct nfs4_statfs_arg args
= {
2459 .bitmask
= server
->attr_bitmask
,
2461 struct nfs4_statfs_res res
= {
2464 struct rpc_message msg
= {
2465 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2470 nfs_fattr_init(fsstat
->fattr
);
2471 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2474 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2476 struct nfs4_exception exception
= { };
2479 err
= nfs4_handle_exception(server
,
2480 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2482 } while (exception
.retry
);
2486 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2487 struct nfs_fsinfo
*fsinfo
)
2489 struct nfs4_fsinfo_arg args
= {
2491 .bitmask
= server
->attr_bitmask
,
2493 struct nfs4_fsinfo_res res
= {
2496 struct rpc_message msg
= {
2497 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
2502 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2505 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2507 struct nfs4_exception exception
= { };
2511 err
= nfs4_handle_exception(server
,
2512 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
2514 } while (exception
.retry
);
2518 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2520 nfs_fattr_init(fsinfo
->fattr
);
2521 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
2524 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2525 struct nfs_pathconf
*pathconf
)
2527 struct nfs4_pathconf_arg args
= {
2529 .bitmask
= server
->attr_bitmask
,
2531 struct nfs4_pathconf_res res
= {
2532 .pathconf
= pathconf
,
2534 struct rpc_message msg
= {
2535 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
2540 /* None of the pathconf attributes are mandatory to implement */
2541 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
2542 memset(pathconf
, 0, sizeof(*pathconf
));
2546 nfs_fattr_init(pathconf
->fattr
);
2547 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2550 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2551 struct nfs_pathconf
*pathconf
)
2553 struct nfs4_exception exception
= { };
2557 err
= nfs4_handle_exception(server
,
2558 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
2560 } while (exception
.retry
);
2564 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
2566 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2568 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
2569 rpc_restart_call(task
);
2573 nfs_invalidate_atime(data
->inode
);
2574 if (task
->tk_status
> 0)
2575 renew_lease(server
, data
->timestamp
);
2579 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
2581 data
->timestamp
= jiffies
;
2582 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
2585 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
2587 struct inode
*inode
= data
->inode
;
2589 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
2590 rpc_restart_call(task
);
2593 if (task
->tk_status
>= 0) {
2594 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
2595 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
2600 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
2602 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2604 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
2605 data
->res
.server
= server
;
2606 data
->timestamp
= jiffies
;
2608 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
2611 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
2613 struct inode
*inode
= data
->inode
;
2615 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
2616 rpc_restart_call(task
);
2619 nfs_refresh_inode(inode
, data
->res
.fattr
);
2623 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
2625 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2627 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
2628 data
->res
.server
= server
;
2629 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
2633 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2634 * standalone procedure for queueing an asynchronous RENEW.
2636 static void nfs4_renew_done(struct rpc_task
*task
, void *data
)
2638 struct nfs_client
*clp
= (struct nfs_client
*)task
->tk_msg
.rpc_argp
;
2639 unsigned long timestamp
= (unsigned long)data
;
2641 if (task
->tk_status
< 0) {
2642 /* Unless we're shutting down, schedule state recovery! */
2643 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
2644 nfs4_schedule_state_recovery(clp
);
2647 spin_lock(&clp
->cl_lock
);
2648 if (time_before(clp
->cl_last_renewal
,timestamp
))
2649 clp
->cl_last_renewal
= timestamp
;
2650 spin_unlock(&clp
->cl_lock
);
2653 static const struct rpc_call_ops nfs4_renew_ops
= {
2654 .rpc_call_done
= nfs4_renew_done
,
2657 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
2659 struct rpc_message msg
= {
2660 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2665 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
2666 &nfs4_renew_ops
, (void *)jiffies
);
2669 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
2671 struct rpc_message msg
= {
2672 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
2676 unsigned long now
= jiffies
;
2679 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2682 spin_lock(&clp
->cl_lock
);
2683 if (time_before(clp
->cl_last_renewal
,now
))
2684 clp
->cl_last_renewal
= now
;
2685 spin_unlock(&clp
->cl_lock
);
2689 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
2691 return (server
->caps
& NFS_CAP_ACLS
)
2692 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
2693 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
2696 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2697 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2700 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2702 static void buf_to_pages(const void *buf
, size_t buflen
,
2703 struct page
**pages
, unsigned int *pgbase
)
2705 const void *p
= buf
;
2707 *pgbase
= offset_in_page(buf
);
2709 while (p
< buf
+ buflen
) {
2710 *(pages
++) = virt_to_page(p
);
2711 p
+= PAGE_CACHE_SIZE
;
2715 struct nfs4_cached_acl
{
2721 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
2723 struct nfs_inode
*nfsi
= NFS_I(inode
);
2725 spin_lock(&inode
->i_lock
);
2726 kfree(nfsi
->nfs4_acl
);
2727 nfsi
->nfs4_acl
= acl
;
2728 spin_unlock(&inode
->i_lock
);
2731 static void nfs4_zap_acl_attr(struct inode
*inode
)
2733 nfs4_set_cached_acl(inode
, NULL
);
2736 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
2738 struct nfs_inode
*nfsi
= NFS_I(inode
);
2739 struct nfs4_cached_acl
*acl
;
2742 spin_lock(&inode
->i_lock
);
2743 acl
= nfsi
->nfs4_acl
;
2746 if (buf
== NULL
) /* user is just asking for length */
2748 if (acl
->cached
== 0)
2750 ret
= -ERANGE
; /* see getxattr(2) man page */
2751 if (acl
->len
> buflen
)
2753 memcpy(buf
, acl
->data
, acl
->len
);
2757 spin_unlock(&inode
->i_lock
);
2761 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
2763 struct nfs4_cached_acl
*acl
;
2765 if (buf
&& acl_len
<= PAGE_SIZE
) {
2766 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
2770 memcpy(acl
->data
, buf
, acl_len
);
2772 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
2779 nfs4_set_cached_acl(inode
, acl
);
2782 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
2784 struct page
*pages
[NFS4ACL_MAXPAGES
];
2785 struct nfs_getaclargs args
= {
2786 .fh
= NFS_FH(inode
),
2790 struct nfs_getaclres res
= {
2794 struct rpc_message msg
= {
2795 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
2799 struct page
*localpage
= NULL
;
2802 if (buflen
< PAGE_SIZE
) {
2803 /* As long as we're doing a round trip to the server anyway,
2804 * let's be prepared for a page of acl data. */
2805 localpage
= alloc_page(GFP_KERNEL
);
2806 resp_buf
= page_address(localpage
);
2807 if (localpage
== NULL
)
2809 args
.acl_pages
[0] = localpage
;
2810 args
.acl_pgbase
= 0;
2811 args
.acl_len
= PAGE_SIZE
;
2814 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
2816 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2819 if (res
.acl_len
> args
.acl_len
)
2820 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
2822 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
2825 if (res
.acl_len
> buflen
)
2828 memcpy(buf
, resp_buf
, res
.acl_len
);
2833 __free_page(localpage
);
2837 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
2839 struct nfs4_exception exception
= { };
2842 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
2845 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
2846 } while (exception
.retry
);
2850 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
2852 struct nfs_server
*server
= NFS_SERVER(inode
);
2855 if (!nfs4_server_supports_acls(server
))
2857 ret
= nfs_revalidate_inode(server
, inode
);
2860 if (NFS_I(inode
)->cache_validity
& NFS_INO_INVALID_ACL
)
2861 nfs_zap_acl_cache(inode
);
2862 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
2865 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
2868 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
2870 struct nfs_server
*server
= NFS_SERVER(inode
);
2871 struct page
*pages
[NFS4ACL_MAXPAGES
];
2872 struct nfs_setaclargs arg
= {
2873 .fh
= NFS_FH(inode
),
2877 struct nfs_setaclres res
;
2878 struct rpc_message msg
= {
2879 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
2885 if (!nfs4_server_supports_acls(server
))
2887 nfs_inode_return_delegation(inode
);
2888 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
2889 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2890 nfs_access_zap_cache(inode
);
2891 nfs_zap_acl_cache(inode
);
2895 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
2897 struct nfs4_exception exception
= { };
2900 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2901 __nfs4_proc_set_acl(inode
, buf
, buflen
),
2903 } while (exception
.retry
);
2908 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
2910 struct nfs_client
*clp
= server
->nfs_client
;
2912 if (!clp
|| task
->tk_status
>= 0)
2914 switch(task
->tk_status
) {
2915 case -NFS4ERR_ADMIN_REVOKED
:
2916 case -NFS4ERR_BAD_STATEID
:
2917 case -NFS4ERR_OPENMODE
:
2920 nfs4_state_mark_reclaim_nograce(clp
, state
);
2921 case -NFS4ERR_STALE_CLIENTID
:
2922 case -NFS4ERR_STALE_STATEID
:
2923 case -NFS4ERR_EXPIRED
:
2924 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
2925 nfs4_schedule_state_recovery(clp
);
2926 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
2927 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
2928 task
->tk_status
= 0;
2930 case -NFS4ERR_DELAY
:
2931 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
2932 case -NFS4ERR_GRACE
:
2933 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
2934 task
->tk_status
= 0;
2936 case -NFS4ERR_OLD_STATEID
:
2937 task
->tk_status
= 0;
2940 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
2944 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
, unsigned short port
, struct rpc_cred
*cred
)
2946 nfs4_verifier sc_verifier
;
2947 struct nfs4_setclientid setclientid
= {
2948 .sc_verifier
= &sc_verifier
,
2951 struct rpc_message msg
= {
2952 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
2953 .rpc_argp
= &setclientid
,
2961 p
= (__be32
*)sc_verifier
.data
;
2962 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
2963 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
2966 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
2967 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
2969 rpc_peeraddr2str(clp
->cl_rpcclient
,
2971 rpc_peeraddr2str(clp
->cl_rpcclient
,
2973 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
2974 clp
->cl_id_uniquifier
);
2975 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
2976 sizeof(setclientid
.sc_netid
),
2977 rpc_peeraddr2str(clp
->cl_rpcclient
,
2978 RPC_DISPLAY_NETID
));
2979 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
2980 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
2981 clp
->cl_ipaddr
, port
>> 8, port
& 255);
2983 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
2984 if (status
!= -NFS4ERR_CLID_INUSE
)
2989 ssleep(clp
->cl_lease_time
+ 1);
2991 if (++clp
->cl_id_uniquifier
== 0)
2997 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
2999 struct nfs_fsinfo fsinfo
;
3000 struct rpc_message msg
= {
3001 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3003 .rpc_resp
= &fsinfo
,
3010 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3012 spin_lock(&clp
->cl_lock
);
3013 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3014 clp
->cl_last_renewal
= now
;
3015 spin_unlock(&clp
->cl_lock
);
3020 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3025 err
= _nfs4_proc_setclientid_confirm(clp
, cred
);
3029 case -NFS4ERR_RESOURCE
:
3030 /* The IBM lawyers misread another document! */
3031 case -NFS4ERR_DELAY
:
3032 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3038 struct nfs4_delegreturndata
{
3039 struct nfs4_delegreturnargs args
;
3040 struct nfs4_delegreturnres res
;
3042 nfs4_stateid stateid
;
3043 unsigned long timestamp
;
3044 struct nfs_fattr fattr
;
3048 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3050 struct nfs4_delegreturndata
*data
= calldata
;
3051 data
->rpc_status
= task
->tk_status
;
3052 if (data
->rpc_status
== 0)
3053 renew_lease(data
->res
.server
, data
->timestamp
);
3056 static void nfs4_delegreturn_release(void *calldata
)
3061 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3062 .rpc_call_done
= nfs4_delegreturn_done
,
3063 .rpc_release
= nfs4_delegreturn_release
,
3066 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3068 struct nfs4_delegreturndata
*data
;
3069 struct nfs_server
*server
= NFS_SERVER(inode
);
3070 struct rpc_task
*task
;
3071 struct rpc_message msg
= {
3072 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3075 struct rpc_task_setup task_setup_data
= {
3076 .rpc_client
= server
->client
,
3077 .rpc_message
= &msg
,
3078 .callback_ops
= &nfs4_delegreturn_ops
,
3079 .flags
= RPC_TASK_ASYNC
,
3083 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
3086 data
->args
.fhandle
= &data
->fh
;
3087 data
->args
.stateid
= &data
->stateid
;
3088 data
->args
.bitmask
= server
->attr_bitmask
;
3089 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3090 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3091 data
->res
.fattr
= &data
->fattr
;
3092 data
->res
.server
= server
;
3093 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3094 nfs_fattr_init(data
->res
.fattr
);
3095 data
->timestamp
= jiffies
;
3096 data
->rpc_status
= 0;
3098 task_setup_data
.callback_data
= data
;
3099 msg
.rpc_argp
= &data
->args
,
3100 msg
.rpc_resp
= &data
->res
,
3101 task
= rpc_run_task(&task_setup_data
);
3103 return PTR_ERR(task
);
3106 status
= nfs4_wait_for_completion_rpc_task(task
);
3109 status
= data
->rpc_status
;
3112 nfs_refresh_inode(inode
, &data
->fattr
);
3118 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3120 struct nfs_server
*server
= NFS_SERVER(inode
);
3121 struct nfs4_exception exception
= { };
3124 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3126 case -NFS4ERR_STALE_STATEID
:
3127 case -NFS4ERR_EXPIRED
:
3131 err
= nfs4_handle_exception(server
, err
, &exception
);
3132 } while (exception
.retry
);
3136 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3137 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3140 * sleep, with exponential backoff, and retry the LOCK operation.
3142 static unsigned long
3143 nfs4_set_lock_task_retry(unsigned long timeout
)
3145 schedule_timeout_killable(timeout
);
3147 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3148 return NFS4_LOCK_MAXTIMEOUT
;
3152 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3154 struct inode
*inode
= state
->inode
;
3155 struct nfs_server
*server
= NFS_SERVER(inode
);
3156 struct nfs_client
*clp
= server
->nfs_client
;
3157 struct nfs_lockt_args arg
= {
3158 .fh
= NFS_FH(inode
),
3161 struct nfs_lockt_res res
= {
3164 struct rpc_message msg
= {
3165 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3168 .rpc_cred
= state
->owner
->so_cred
,
3170 struct nfs4_lock_state
*lsp
;
3173 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3174 status
= nfs4_set_lock_state(state
, request
);
3177 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3178 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3179 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3182 request
->fl_type
= F_UNLCK
;
3184 case -NFS4ERR_DENIED
:
3187 request
->fl_ops
->fl_release_private(request
);
3192 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3194 struct nfs4_exception exception
= { };
3198 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3199 _nfs4_proc_getlk(state
, cmd
, request
),
3201 } while (exception
.retry
);
3205 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3208 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3210 res
= posix_lock_file_wait(file
, fl
);
3213 res
= flock_lock_file_wait(file
, fl
);
3221 struct nfs4_unlockdata
{
3222 struct nfs_locku_args arg
;
3223 struct nfs_locku_res res
;
3224 struct nfs4_lock_state
*lsp
;
3225 struct nfs_open_context
*ctx
;
3226 struct file_lock fl
;
3227 const struct nfs_server
*server
;
3228 unsigned long timestamp
;
3231 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3232 struct nfs_open_context
*ctx
,
3233 struct nfs4_lock_state
*lsp
,
3234 struct nfs_seqid
*seqid
)
3236 struct nfs4_unlockdata
*p
;
3237 struct inode
*inode
= lsp
->ls_state
->inode
;
3239 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
3242 p
->arg
.fh
= NFS_FH(inode
);
3244 p
->arg
.seqid
= seqid
;
3245 p
->res
.seqid
= seqid
;
3246 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3247 p
->arg
.stateid
= &lsp
->ls_stateid
;
3249 atomic_inc(&lsp
->ls_count
);
3250 /* Ensure we don't close file until we're done freeing locks! */
3251 p
->ctx
= get_nfs_open_context(ctx
);
3252 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3253 p
->server
= NFS_SERVER(inode
);
3257 static void nfs4_locku_release_calldata(void *data
)
3259 struct nfs4_unlockdata
*calldata
= data
;
3260 nfs_free_seqid(calldata
->arg
.seqid
);
3261 nfs4_put_lock_state(calldata
->lsp
);
3262 put_nfs_open_context(calldata
->ctx
);
3266 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3268 struct nfs4_unlockdata
*calldata
= data
;
3270 if (RPC_ASSASSINATED(task
))
3272 switch (task
->tk_status
) {
3274 memcpy(calldata
->lsp
->ls_stateid
.data
,
3275 calldata
->res
.stateid
.data
,
3276 sizeof(calldata
->lsp
->ls_stateid
.data
));
3277 renew_lease(calldata
->server
, calldata
->timestamp
);
3279 case -NFS4ERR_BAD_STATEID
:
3280 case -NFS4ERR_OLD_STATEID
:
3281 case -NFS4ERR_STALE_STATEID
:
3282 case -NFS4ERR_EXPIRED
:
3285 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3286 rpc_restart_call(task
);
3290 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3292 struct nfs4_unlockdata
*calldata
= data
;
3294 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3296 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3297 /* Note: exit _without_ running nfs4_locku_done */
3298 task
->tk_action
= NULL
;
3301 calldata
->timestamp
= jiffies
;
3302 rpc_call_start(task
);
3305 static const struct rpc_call_ops nfs4_locku_ops
= {
3306 .rpc_call_prepare
= nfs4_locku_prepare
,
3307 .rpc_call_done
= nfs4_locku_done
,
3308 .rpc_release
= nfs4_locku_release_calldata
,
3311 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3312 struct nfs_open_context
*ctx
,
3313 struct nfs4_lock_state
*lsp
,
3314 struct nfs_seqid
*seqid
)
3316 struct nfs4_unlockdata
*data
;
3317 struct rpc_message msg
= {
3318 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3319 .rpc_cred
= ctx
->cred
,
3321 struct rpc_task_setup task_setup_data
= {
3322 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3323 .rpc_message
= &msg
,
3324 .callback_ops
= &nfs4_locku_ops
,
3325 .workqueue
= nfsiod_workqueue
,
3326 .flags
= RPC_TASK_ASYNC
,
3329 /* Ensure this is an unlock - when canceling a lock, the
3330 * canceled lock is passed in, and it won't be an unlock.
3332 fl
->fl_type
= F_UNLCK
;
3334 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3336 nfs_free_seqid(seqid
);
3337 return ERR_PTR(-ENOMEM
);
3340 msg
.rpc_argp
= &data
->arg
,
3341 msg
.rpc_resp
= &data
->res
,
3342 task_setup_data
.callback_data
= data
;
3343 return rpc_run_task(&task_setup_data
);
3346 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3348 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3349 struct nfs_seqid
*seqid
;
3350 struct nfs4_lock_state
*lsp
;
3351 struct rpc_task
*task
;
3353 unsigned char fl_flags
= request
->fl_flags
;
3355 status
= nfs4_set_lock_state(state
, request
);
3356 /* Unlock _before_ we do the RPC call */
3357 request
->fl_flags
|= FL_EXISTS
;
3358 down_read(&nfsi
->rwsem
);
3359 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3360 up_read(&nfsi
->rwsem
);
3363 up_read(&nfsi
->rwsem
);
3366 /* Is this a delegated lock? */
3367 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3369 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3370 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3374 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3375 status
= PTR_ERR(task
);
3378 status
= nfs4_wait_for_completion_rpc_task(task
);
3381 request
->fl_flags
= fl_flags
;
3385 struct nfs4_lockdata
{
3386 struct nfs_lock_args arg
;
3387 struct nfs_lock_res res
;
3388 struct nfs4_lock_state
*lsp
;
3389 struct nfs_open_context
*ctx
;
3390 struct file_lock fl
;
3391 unsigned long timestamp
;
3396 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3397 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
)
3399 struct nfs4_lockdata
*p
;
3400 struct inode
*inode
= lsp
->ls_state
->inode
;
3401 struct nfs_server
*server
= NFS_SERVER(inode
);
3403 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3407 p
->arg
.fh
= NFS_FH(inode
);
3409 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
);
3410 if (p
->arg
.open_seqid
== NULL
)
3412 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3413 if (p
->arg
.lock_seqid
== NULL
)
3414 goto out_free_seqid
;
3415 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
3416 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
3417 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3418 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
3419 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3421 atomic_inc(&lsp
->ls_count
);
3422 p
->ctx
= get_nfs_open_context(ctx
);
3423 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3426 nfs_free_seqid(p
->arg
.open_seqid
);
3432 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
3434 struct nfs4_lockdata
*data
= calldata
;
3435 struct nfs4_state
*state
= data
->lsp
->ls_state
;
3437 dprintk("%s: begin!\n", __func__
);
3438 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
3440 /* Do we need to do an open_to_lock_owner? */
3441 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
3442 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
3444 data
->arg
.open_stateid
= &state
->stateid
;
3445 data
->arg
.new_lock_owner
= 1;
3446 data
->res
.open_seqid
= data
->arg
.open_seqid
;
3448 data
->arg
.new_lock_owner
= 0;
3449 data
->timestamp
= jiffies
;
3450 rpc_call_start(task
);
3451 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
3454 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
3456 struct nfs4_lockdata
*data
= calldata
;
3458 dprintk("%s: begin!\n", __func__
);
3460 data
->rpc_status
= task
->tk_status
;
3461 if (RPC_ASSASSINATED(task
))
3463 if (data
->arg
.new_lock_owner
!= 0) {
3464 if (data
->rpc_status
== 0)
3465 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
3469 if (data
->rpc_status
== 0) {
3470 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
3471 sizeof(data
->lsp
->ls_stateid
.data
));
3472 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
3473 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
3476 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
3479 static void nfs4_lock_release(void *calldata
)
3481 struct nfs4_lockdata
*data
= calldata
;
3483 dprintk("%s: begin!\n", __func__
);
3484 nfs_free_seqid(data
->arg
.open_seqid
);
3485 if (data
->cancelled
!= 0) {
3486 struct rpc_task
*task
;
3487 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
3488 data
->arg
.lock_seqid
);
3491 dprintk("%s: cancelling lock!\n", __func__
);
3493 nfs_free_seqid(data
->arg
.lock_seqid
);
3494 nfs4_put_lock_state(data
->lsp
);
3495 put_nfs_open_context(data
->ctx
);
3497 dprintk("%s: done!\n", __func__
);
3500 static const struct rpc_call_ops nfs4_lock_ops
= {
3501 .rpc_call_prepare
= nfs4_lock_prepare
,
3502 .rpc_call_done
= nfs4_lock_done
,
3503 .rpc_release
= nfs4_lock_release
,
3506 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int reclaim
)
3508 struct nfs4_lockdata
*data
;
3509 struct rpc_task
*task
;
3510 struct rpc_message msg
= {
3511 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
3512 .rpc_cred
= state
->owner
->so_cred
,
3514 struct rpc_task_setup task_setup_data
= {
3515 .rpc_client
= NFS_CLIENT(state
->inode
),
3516 .rpc_message
= &msg
,
3517 .callback_ops
= &nfs4_lock_ops
,
3518 .workqueue
= nfsiod_workqueue
,
3519 .flags
= RPC_TASK_ASYNC
,
3523 dprintk("%s: begin!\n", __func__
);
3524 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
3525 fl
->fl_u
.nfs4_fl
.owner
);
3529 data
->arg
.block
= 1;
3531 data
->arg
.reclaim
= 1;
3532 msg
.rpc_argp
= &data
->arg
,
3533 msg
.rpc_resp
= &data
->res
,
3534 task_setup_data
.callback_data
= data
;
3535 task
= rpc_run_task(&task_setup_data
);
3537 return PTR_ERR(task
);
3538 ret
= nfs4_wait_for_completion_rpc_task(task
);
3540 ret
= data
->rpc_status
;
3541 if (ret
== -NFS4ERR_DENIED
)
3544 data
->cancelled
= 1;
3546 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
3550 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
3552 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3553 struct nfs4_exception exception
= { };
3557 /* Cache the lock if possible... */
3558 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
3560 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 1);
3561 if (err
!= -NFS4ERR_DELAY
)
3563 nfs4_handle_exception(server
, err
, &exception
);
3564 } while (exception
.retry
);
3568 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
3570 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3571 struct nfs4_exception exception
= { };
3574 err
= nfs4_set_lock_state(state
, request
);
3578 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
3580 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 0);
3581 if (err
!= -NFS4ERR_DELAY
)
3583 nfs4_handle_exception(server
, err
, &exception
);
3584 } while (exception
.retry
);
3588 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3590 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3591 unsigned char fl_flags
= request
->fl_flags
;
3594 /* Is this a delegated open? */
3595 status
= nfs4_set_lock_state(state
, request
);
3598 request
->fl_flags
|= FL_ACCESS
;
3599 status
= do_vfs_lock(request
->fl_file
, request
);
3602 down_read(&nfsi
->rwsem
);
3603 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
3604 /* Yes: cache locks! */
3605 /* ...but avoid races with delegation recall... */
3606 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
3607 status
= do_vfs_lock(request
->fl_file
, request
);
3610 status
= _nfs4_do_setlk(state
, cmd
, request
, 0);
3613 /* Note: we always want to sleep here! */
3614 request
->fl_flags
= fl_flags
| FL_SLEEP
;
3615 if (do_vfs_lock(request
->fl_file
, request
) < 0)
3616 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
3618 up_read(&nfsi
->rwsem
);
3620 request
->fl_flags
= fl_flags
;
3624 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3626 struct nfs4_exception exception
= { };
3630 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3631 _nfs4_proc_setlk(state
, cmd
, request
),
3633 } while (exception
.retry
);
3638 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
3640 struct nfs_open_context
*ctx
;
3641 struct nfs4_state
*state
;
3642 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
3645 /* verify open state */
3646 ctx
= nfs_file_open_context(filp
);
3649 if (request
->fl_start
< 0 || request
->fl_end
< 0)
3653 return nfs4_proc_getlk(state
, F_GETLK
, request
);
3655 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
3658 if (request
->fl_type
== F_UNLCK
)
3659 return nfs4_proc_unlck(state
, cmd
, request
);
3662 status
= nfs4_proc_setlk(state
, cmd
, request
);
3663 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
3665 timeout
= nfs4_set_lock_task_retry(timeout
);
3666 status
= -ERESTARTSYS
;
3669 } while(status
< 0);
3673 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
3675 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
3676 struct nfs4_exception exception
= { };
3679 err
= nfs4_set_lock_state(state
, fl
);
3683 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, 0);
3684 if (err
!= -NFS4ERR_DELAY
)
3686 err
= nfs4_handle_exception(server
, err
, &exception
);
3687 } while (exception
.retry
);
3692 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3694 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
3695 size_t buflen
, int flags
)
3697 struct inode
*inode
= dentry
->d_inode
;
3699 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3702 return nfs4_proc_set_acl(inode
, buf
, buflen
);
3705 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3706 * and that's what we'll do for e.g. user attributes that haven't been set.
3707 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3708 * attributes in kernel-managed attribute namespaces. */
3709 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
3712 struct inode
*inode
= dentry
->d_inode
;
3714 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
3717 return nfs4_proc_get_acl(inode
, buf
, buflen
);
3720 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
3722 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
3724 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
3726 if (buf
&& buflen
< len
)
3729 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
3733 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
3735 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
3736 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
3737 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
3740 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
3741 NFS_ATTR_FATTR_NLINK
;
3742 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
3746 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
3747 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
3749 struct nfs_server
*server
= NFS_SERVER(dir
);
3751 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
3752 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
3754 struct nfs4_fs_locations_arg args
= {
3755 .dir_fh
= NFS_FH(dir
),
3760 struct nfs4_fs_locations_res res
= {
3761 .fs_locations
= fs_locations
,
3763 struct rpc_message msg
= {
3764 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
3770 dprintk("%s: start\n", __func__
);
3771 nfs_fattr_init(&fs_locations
->fattr
);
3772 fs_locations
->server
= server
;
3773 fs_locations
->nlocations
= 0;
3774 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3775 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
3776 dprintk("%s: returned status = %d\n", __func__
, status
);
3780 #ifdef CONFIG_NFS_V4_1
3781 /* Destroy the slot table */
3782 static void nfs4_destroy_slot_table(struct nfs4_session
*session
)
3784 if (session
->fc_slot_table
.slots
== NULL
)
3786 kfree(session
->fc_slot_table
.slots
);
3787 session
->fc_slot_table
.slots
= NULL
;
3791 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
3793 struct nfs4_session
*session
;
3794 struct nfs4_slot_table
*tbl
;
3796 session
= kzalloc(sizeof(struct nfs4_session
), GFP_KERNEL
);
3799 tbl
= &session
->fc_slot_table
;
3800 spin_lock_init(&tbl
->slot_tbl_lock
);
3801 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "Slot table");
3806 void nfs4_destroy_session(struct nfs4_session
*session
)
3808 nfs4_destroy_slot_table(session
);
3812 #endif /* CONFIG_NFS_V4_1 */
3814 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops
= {
3815 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
3816 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
3817 .recover_open
= nfs4_open_reclaim
,
3818 .recover_lock
= nfs4_lock_reclaim
,
3821 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops
= {
3822 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
3823 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
3824 .recover_open
= nfs4_open_expired
,
3825 .recover_lock
= nfs4_lock_expired
,
3828 static const struct inode_operations nfs4_file_inode_operations
= {
3829 .permission
= nfs_permission
,
3830 .getattr
= nfs_getattr
,
3831 .setattr
= nfs_setattr
,
3832 .getxattr
= nfs4_getxattr
,
3833 .setxattr
= nfs4_setxattr
,
3834 .listxattr
= nfs4_listxattr
,
3837 const struct nfs_rpc_ops nfs_v4_clientops
= {
3838 .version
= 4, /* protocol version */
3839 .dentry_ops
= &nfs4_dentry_operations
,
3840 .dir_inode_ops
= &nfs4_dir_inode_operations
,
3841 .file_inode_ops
= &nfs4_file_inode_operations
,
3842 .getroot
= nfs4_proc_get_root
,
3843 .getattr
= nfs4_proc_getattr
,
3844 .setattr
= nfs4_proc_setattr
,
3845 .lookupfh
= nfs4_proc_lookupfh
,
3846 .lookup
= nfs4_proc_lookup
,
3847 .access
= nfs4_proc_access
,
3848 .readlink
= nfs4_proc_readlink
,
3849 .create
= nfs4_proc_create
,
3850 .remove
= nfs4_proc_remove
,
3851 .unlink_setup
= nfs4_proc_unlink_setup
,
3852 .unlink_done
= nfs4_proc_unlink_done
,
3853 .rename
= nfs4_proc_rename
,
3854 .link
= nfs4_proc_link
,
3855 .symlink
= nfs4_proc_symlink
,
3856 .mkdir
= nfs4_proc_mkdir
,
3857 .rmdir
= nfs4_proc_remove
,
3858 .readdir
= nfs4_proc_readdir
,
3859 .mknod
= nfs4_proc_mknod
,
3860 .statfs
= nfs4_proc_statfs
,
3861 .fsinfo
= nfs4_proc_fsinfo
,
3862 .pathconf
= nfs4_proc_pathconf
,
3863 .set_capabilities
= nfs4_server_capabilities
,
3864 .decode_dirent
= nfs4_decode_dirent
,
3865 .read_setup
= nfs4_proc_read_setup
,
3866 .read_done
= nfs4_read_done
,
3867 .write_setup
= nfs4_proc_write_setup
,
3868 .write_done
= nfs4_write_done
,
3869 .commit_setup
= nfs4_proc_commit_setup
,
3870 .commit_done
= nfs4_commit_done
,
3871 .lock
= nfs4_proc_lock
,
3872 .clear_acl_cache
= nfs4_zap_acl_attr
,
3873 .close_context
= nfs4_close_context
,