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/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/sunrpc/clnt.h>
43 #include <linux/nfs.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/nfs_page.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/module.h>
50 #include <linux/sunrpc/bc_xprt.h>
53 #include "delegation.h"
58 #define NFSDBG_FACILITY NFSDBG_PROC
60 #define NFS4_POLL_RETRY_MIN (HZ/10)
61 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 #define NFS4_MAX_LOOP_ON_RECOVER (10)
66 static int _nfs4_proc_open(struct nfs4_opendata
*data
);
67 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
68 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
69 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
70 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
72 /* Prevent leaks of NFSv4 errors into userland */
73 static int nfs4_map_errors(int err
)
78 case -NFS4ERR_RESOURCE
:
81 dprintk("%s could not handle NFSv4 error %d\n",
89 * This is our standard bitmap for GETATTR requests.
91 const u32 nfs4_fattr_bitmap
[2] = {
96 | FATTR4_WORD0_FILEID
,
98 | FATTR4_WORD1_NUMLINKS
100 | FATTR4_WORD1_OWNER_GROUP
101 | FATTR4_WORD1_RAWDEV
102 | FATTR4_WORD1_SPACE_USED
103 | FATTR4_WORD1_TIME_ACCESS
104 | FATTR4_WORD1_TIME_METADATA
105 | FATTR4_WORD1_TIME_MODIFY
108 const u32 nfs4_statfs_bitmap
[2] = {
109 FATTR4_WORD0_FILES_AVAIL
110 | FATTR4_WORD0_FILES_FREE
111 | FATTR4_WORD0_FILES_TOTAL
,
112 FATTR4_WORD1_SPACE_AVAIL
113 | FATTR4_WORD1_SPACE_FREE
114 | FATTR4_WORD1_SPACE_TOTAL
117 const u32 nfs4_pathconf_bitmap
[2] = {
119 | FATTR4_WORD0_MAXNAME
,
123 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
124 | FATTR4_WORD0_MAXREAD
125 | FATTR4_WORD0_MAXWRITE
126 | FATTR4_WORD0_LEASE_TIME
,
130 const u32 nfs4_fs_locations_bitmap
[2] = {
132 | FATTR4_WORD0_CHANGE
135 | FATTR4_WORD0_FILEID
136 | FATTR4_WORD0_FS_LOCATIONS
,
138 | FATTR4_WORD1_NUMLINKS
140 | FATTR4_WORD1_OWNER_GROUP
141 | FATTR4_WORD1_RAWDEV
142 | FATTR4_WORD1_SPACE_USED
143 | FATTR4_WORD1_TIME_ACCESS
144 | FATTR4_WORD1_TIME_METADATA
145 | FATTR4_WORD1_TIME_MODIFY
146 | FATTR4_WORD1_MOUNTED_ON_FILEID
149 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
150 struct nfs4_readdir_arg
*readdir
)
154 BUG_ON(readdir
->count
< 80);
156 readdir
->cookie
= cookie
;
157 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
162 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
167 * NFSv4 servers do not return entries for '.' and '..'
168 * Therefore, we fake these entries here. We let '.'
169 * have cookie 0 and '..' have cookie 1. Note that
170 * when talking to the server, we always send cookie 0
173 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
176 *p
++ = xdr_one
; /* next */
177 *p
++ = xdr_zero
; /* cookie, first word */
178 *p
++ = xdr_one
; /* cookie, second word */
179 *p
++ = xdr_one
; /* entry len */
180 memcpy(p
, ".\0\0\0", 4); /* entry */
182 *p
++ = xdr_one
; /* bitmap length */
183 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
184 *p
++ = htonl(8); /* attribute buffer length */
185 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
188 *p
++ = xdr_one
; /* next */
189 *p
++ = xdr_zero
; /* cookie, first word */
190 *p
++ = xdr_two
; /* cookie, second word */
191 *p
++ = xdr_two
; /* entry len */
192 memcpy(p
, "..\0\0", 4); /* entry */
194 *p
++ = xdr_one
; /* bitmap length */
195 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
196 *p
++ = htonl(8); /* attribute buffer length */
197 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
199 readdir
->pgbase
= (char *)p
- (char *)start
;
200 readdir
->count
-= readdir
->pgbase
;
201 kunmap_atomic(start
, KM_USER0
);
204 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
210 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
211 nfs_wait_bit_killable
, TASK_KILLABLE
);
215 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
222 *timeout
= NFS4_POLL_RETRY_MIN
;
223 if (*timeout
> NFS4_POLL_RETRY_MAX
)
224 *timeout
= NFS4_POLL_RETRY_MAX
;
225 schedule_timeout_killable(*timeout
);
226 if (fatal_signal_pending(current
))
232 /* This is the error handling routine for processes that are allowed
235 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
237 struct nfs_client
*clp
= server
->nfs_client
;
238 struct nfs4_state
*state
= exception
->state
;
241 exception
->retry
= 0;
245 case -NFS4ERR_ADMIN_REVOKED
:
246 case -NFS4ERR_BAD_STATEID
:
247 case -NFS4ERR_OPENMODE
:
250 nfs4_state_mark_reclaim_nograce(clp
, state
);
251 case -NFS4ERR_STALE_CLIENTID
:
252 case -NFS4ERR_STALE_STATEID
:
253 case -NFS4ERR_EXPIRED
:
254 nfs4_schedule_state_recovery(clp
);
255 ret
= nfs4_wait_clnt_recover(clp
);
257 exception
->retry
= 1;
258 #if !defined(CONFIG_NFS_V4_1)
260 #else /* !defined(CONFIG_NFS_V4_1) */
261 if (!nfs4_has_session(server
->nfs_client
))
264 case -NFS4ERR_BADSESSION
:
265 case -NFS4ERR_BADSLOT
:
266 case -NFS4ERR_BAD_HIGH_SLOT
:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
268 case -NFS4ERR_DEADSESSION
:
269 case -NFS4ERR_SEQ_FALSE_RETRY
:
270 case -NFS4ERR_SEQ_MISORDERED
:
271 dprintk("%s ERROR: %d Reset session\n", __func__
,
273 set_bit(NFS4CLNT_SESSION_SETUP
, &clp
->cl_state
);
274 exception
->retry
= 1;
276 #endif /* !defined(CONFIG_NFS_V4_1) */
277 case -NFS4ERR_FILE_OPEN
:
280 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
283 case -NFS4ERR_OLD_STATEID
:
284 exception
->retry
= 1;
286 /* We failed to handle the error */
287 return nfs4_map_errors(ret
);
291 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
293 struct nfs_client
*clp
= server
->nfs_client
;
294 spin_lock(&clp
->cl_lock
);
295 if (time_before(clp
->cl_last_renewal
,timestamp
))
296 clp
->cl_last_renewal
= timestamp
;
297 spin_unlock(&clp
->cl_lock
);
300 #if defined(CONFIG_NFS_V4_1)
303 * nfs4_free_slot - free a slot and efficiently update slot table.
305 * freeing a slot is trivially done by clearing its respective bit
307 * If the freed slotid equals highest_used_slotid we want to update it
308 * so that the server would be able to size down the slot table if needed,
309 * otherwise we know that the highest_used_slotid is still in use.
310 * When updating highest_used_slotid there may be "holes" in the bitmap
311 * so we need to scan down from highest_used_slotid to 0 looking for the now
312 * highest slotid in use.
313 * If none found, highest_used_slotid is set to -1.
316 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
318 int slotid
= free_slotid
;
320 spin_lock(&tbl
->slot_tbl_lock
);
321 /* clear used bit in bitmap */
322 __clear_bit(slotid
, tbl
->used_slots
);
324 /* update highest_used_slotid when it is freed */
325 if (slotid
== tbl
->highest_used_slotid
) {
326 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
327 if (slotid
>= 0 && slotid
< tbl
->max_slots
)
328 tbl
->highest_used_slotid
= slotid
;
330 tbl
->highest_used_slotid
= -1;
332 rpc_wake_up_next(&tbl
->slot_tbl_waitq
);
333 spin_unlock(&tbl
->slot_tbl_lock
);
334 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
335 free_slotid
, tbl
->highest_used_slotid
);
338 void nfs41_sequence_free_slot(const struct nfs_client
*clp
,
339 struct nfs4_sequence_res
*res
)
341 struct nfs4_slot_table
*tbl
;
343 if (!nfs4_has_session(clp
)) {
344 dprintk("%s: No session\n", __func__
);
347 tbl
= &clp
->cl_session
->fc_slot_table
;
348 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
349 dprintk("%s: No slot\n", __func__
);
350 /* just wake up the next guy waiting since
351 * we may have not consumed a slot after all */
352 rpc_wake_up_next(&tbl
->slot_tbl_waitq
);
355 nfs4_free_slot(tbl
, res
->sr_slotid
);
356 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
359 static void nfs41_sequence_done(struct nfs_client
*clp
,
360 struct nfs4_sequence_res
*res
,
363 unsigned long timestamp
;
364 struct nfs4_slot_table
*tbl
;
365 struct nfs4_slot
*slot
;
368 * sr_status remains 1 if an RPC level error occurred. The server
369 * may or may not have processed the sequence operation..
370 * Proceed as if the server received and processed the sequence
373 if (res
->sr_status
== 1)
374 res
->sr_status
= NFS_OK
;
376 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
377 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
380 tbl
= &clp
->cl_session
->fc_slot_table
;
381 slot
= tbl
->slots
+ res
->sr_slotid
;
383 if (res
->sr_status
== 0) {
384 /* Update the slot's sequence and clientid lease timer */
386 timestamp
= res
->sr_renewal_time
;
387 spin_lock(&clp
->cl_lock
);
388 if (time_before(clp
->cl_last_renewal
, timestamp
))
389 clp
->cl_last_renewal
= timestamp
;
390 spin_unlock(&clp
->cl_lock
);
394 /* The session may be reset by one of the error handlers. */
395 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
396 nfs41_sequence_free_slot(clp
, res
);
400 * nfs4_find_slot - efficiently look for a free slot
402 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
403 * If found, we mark the slot as used, update the highest_used_slotid,
404 * and respectively set up the sequence operation args.
405 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
407 * Note: must be called with under the slot_tbl_lock.
410 nfs4_find_slot(struct nfs4_slot_table
*tbl
, struct rpc_task
*task
)
413 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
414 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
416 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
417 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
419 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
420 if (slotid
>= tbl
->max_slots
)
422 __set_bit(slotid
, tbl
->used_slots
);
423 if (slotid
> tbl
->highest_used_slotid
)
424 tbl
->highest_used_slotid
= slotid
;
427 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
428 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
432 static int nfs4_recover_session(struct nfs4_session
*session
)
434 struct nfs_client
*clp
= session
->clp
;
438 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
439 ret
= nfs4_wait_clnt_recover(clp
);
442 if (!test_bit(NFS4CLNT_SESSION_SETUP
, &clp
->cl_state
))
444 nfs4_schedule_state_manager(clp
);
450 static int nfs41_setup_sequence(struct nfs4_session
*session
,
451 struct nfs4_sequence_args
*args
,
452 struct nfs4_sequence_res
*res
,
454 struct rpc_task
*task
)
456 struct nfs4_slot
*slot
;
457 struct nfs4_slot_table
*tbl
;
461 dprintk("--> %s\n", __func__
);
462 /* slot already allocated? */
463 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
466 memset(res
, 0, sizeof(*res
));
467 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
468 tbl
= &session
->fc_slot_table
;
470 spin_lock(&tbl
->slot_tbl_lock
);
471 if (test_bit(NFS4CLNT_SESSION_SETUP
, &session
->clp
->cl_state
)) {
472 if (tbl
->highest_used_slotid
!= -1) {
473 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
474 spin_unlock(&tbl
->slot_tbl_lock
);
475 dprintk("<-- %s: Session reset: draining\n", __func__
);
479 /* The slot table is empty; start the reset thread */
480 dprintk("%s Session Reset\n", __func__
);
481 spin_unlock(&tbl
->slot_tbl_lock
);
482 status
= nfs4_recover_session(session
);
485 spin_lock(&tbl
->slot_tbl_lock
);
488 slotid
= nfs4_find_slot(tbl
, task
);
489 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
490 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
491 spin_unlock(&tbl
->slot_tbl_lock
);
492 dprintk("<-- %s: no free slots\n", __func__
);
495 spin_unlock(&tbl
->slot_tbl_lock
);
497 slot
= tbl
->slots
+ slotid
;
498 args
->sa_session
= session
;
499 args
->sa_slotid
= slotid
;
500 args
->sa_cache_this
= cache_reply
;
502 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
504 res
->sr_session
= session
;
505 res
->sr_slotid
= slotid
;
506 res
->sr_renewal_time
= jiffies
;
508 * sr_status is only set in decode_sequence, and so will remain
509 * set to 1 if an rpc level failure occurs.
515 int nfs4_setup_sequence(struct nfs_client
*clp
,
516 struct nfs4_sequence_args
*args
,
517 struct nfs4_sequence_res
*res
,
519 struct rpc_task
*task
)
523 dprintk("--> %s clp %p session %p sr_slotid %d\n",
524 __func__
, clp
, clp
->cl_session
, res
->sr_slotid
);
526 if (!nfs4_has_session(clp
))
528 ret
= nfs41_setup_sequence(clp
->cl_session
, args
, res
, cache_reply
,
530 if (ret
!= -EAGAIN
) {
531 /* terminate rpc task */
532 task
->tk_status
= ret
;
533 task
->tk_action
= NULL
;
536 dprintk("<-- %s status=%d\n", __func__
, ret
);
540 struct nfs41_call_sync_data
{
541 struct nfs_client
*clp
;
542 struct nfs4_sequence_args
*seq_args
;
543 struct nfs4_sequence_res
*seq_res
;
547 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
549 struct nfs41_call_sync_data
*data
= calldata
;
551 dprintk("--> %s data->clp->cl_session %p\n", __func__
,
552 data
->clp
->cl_session
);
553 if (nfs4_setup_sequence(data
->clp
, data
->seq_args
,
554 data
->seq_res
, data
->cache_reply
, task
))
556 rpc_call_start(task
);
559 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
561 struct nfs41_call_sync_data
*data
= calldata
;
563 nfs41_sequence_done(data
->clp
, data
->seq_res
, task
->tk_status
);
564 nfs41_sequence_free_slot(data
->clp
, data
->seq_res
);
567 struct rpc_call_ops nfs41_call_sync_ops
= {
568 .rpc_call_prepare
= nfs41_call_sync_prepare
,
569 .rpc_call_done
= nfs41_call_sync_done
,
572 static int nfs4_call_sync_sequence(struct nfs_client
*clp
,
573 struct rpc_clnt
*clnt
,
574 struct rpc_message
*msg
,
575 struct nfs4_sequence_args
*args
,
576 struct nfs4_sequence_res
*res
,
580 struct rpc_task
*task
;
581 struct nfs41_call_sync_data data
= {
585 .cache_reply
= cache_reply
,
587 struct rpc_task_setup task_setup
= {
590 .callback_ops
= &nfs41_call_sync_ops
,
591 .callback_data
= &data
594 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
595 task
= rpc_run_task(&task_setup
);
599 ret
= task
->tk_status
;
605 int _nfs4_call_sync_session(struct nfs_server
*server
,
606 struct rpc_message
*msg
,
607 struct nfs4_sequence_args
*args
,
608 struct nfs4_sequence_res
*res
,
611 return nfs4_call_sync_sequence(server
->nfs_client
, server
->client
,
612 msg
, args
, res
, cache_reply
);
615 #endif /* CONFIG_NFS_V4_1 */
617 int _nfs4_call_sync(struct nfs_server
*server
,
618 struct rpc_message
*msg
,
619 struct nfs4_sequence_args
*args
,
620 struct nfs4_sequence_res
*res
,
623 args
->sa_session
= res
->sr_session
= NULL
;
624 return rpc_call_sync(server
->client
, msg
, 0);
627 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
628 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
629 &(res)->seq_res, (cache_reply))
631 static void nfs4_sequence_done(const struct nfs_server
*server
,
632 struct nfs4_sequence_res
*res
, int rpc_status
)
634 #ifdef CONFIG_NFS_V4_1
635 if (nfs4_has_session(server
->nfs_client
))
636 nfs41_sequence_done(server
->nfs_client
, res
, rpc_status
);
637 #endif /* CONFIG_NFS_V4_1 */
640 /* no restart, therefore free slot here */
641 static void nfs4_sequence_done_free_slot(const struct nfs_server
*server
,
642 struct nfs4_sequence_res
*res
,
645 nfs4_sequence_done(server
, res
, rpc_status
);
646 nfs4_sequence_free_slot(server
->nfs_client
, res
);
649 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
651 struct nfs_inode
*nfsi
= NFS_I(dir
);
653 spin_lock(&dir
->i_lock
);
654 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
655 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
656 nfs_force_lookup_revalidate(dir
);
657 nfsi
->change_attr
= cinfo
->after
;
658 spin_unlock(&dir
->i_lock
);
661 struct nfs4_opendata
{
663 struct nfs_openargs o_arg
;
664 struct nfs_openres o_res
;
665 struct nfs_open_confirmargs c_arg
;
666 struct nfs_open_confirmres c_res
;
667 struct nfs_fattr f_attr
;
668 struct nfs_fattr dir_attr
;
671 struct nfs4_state_owner
*owner
;
672 struct nfs4_state
*state
;
674 unsigned long timestamp
;
675 unsigned int rpc_done
: 1;
681 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
683 p
->o_res
.f_attr
= &p
->f_attr
;
684 p
->o_res
.dir_attr
= &p
->dir_attr
;
685 p
->o_res
.seqid
= p
->o_arg
.seqid
;
686 p
->c_res
.seqid
= p
->c_arg
.seqid
;
687 p
->o_res
.server
= p
->o_arg
.server
;
688 nfs_fattr_init(&p
->f_attr
);
689 nfs_fattr_init(&p
->dir_attr
);
690 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
693 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
694 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
695 const struct iattr
*attrs
)
697 struct dentry
*parent
= dget_parent(path
->dentry
);
698 struct inode
*dir
= parent
->d_inode
;
699 struct nfs_server
*server
= NFS_SERVER(dir
);
700 struct nfs4_opendata
*p
;
702 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
705 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
706 if (p
->o_arg
.seqid
== NULL
)
708 p
->path
.mnt
= mntget(path
->mnt
);
709 p
->path
.dentry
= dget(path
->dentry
);
712 atomic_inc(&sp
->so_count
);
713 p
->o_arg
.fh
= NFS_FH(dir
);
714 p
->o_arg
.open_flags
= flags
;
715 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
716 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
717 p
->o_arg
.id
= sp
->so_owner_id
.id
;
718 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
719 p
->o_arg
.server
= server
;
720 p
->o_arg
.bitmask
= server
->attr_bitmask
;
721 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
722 if (flags
& O_EXCL
) {
723 u32
*s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
726 } else if (flags
& O_CREAT
) {
727 p
->o_arg
.u
.attrs
= &p
->attrs
;
728 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
730 p
->c_arg
.fh
= &p
->o_res
.fh
;
731 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
732 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
733 nfs4_init_opendata_res(p
);
743 static void nfs4_opendata_free(struct kref
*kref
)
745 struct nfs4_opendata
*p
= container_of(kref
,
746 struct nfs4_opendata
, kref
);
748 nfs_free_seqid(p
->o_arg
.seqid
);
749 if (p
->state
!= NULL
)
750 nfs4_put_open_state(p
->state
);
751 nfs4_put_state_owner(p
->owner
);
757 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
760 kref_put(&p
->kref
, nfs4_opendata_free
);
763 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
767 ret
= rpc_wait_for_completion_task(task
);
771 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
775 if (open_mode
& O_EXCL
)
777 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
779 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0;
782 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0;
784 case FMODE_READ
|FMODE_WRITE
:
785 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0;
791 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
793 if ((delegation
->type
& fmode
) != fmode
)
795 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
797 nfs_mark_delegation_referenced(delegation
);
801 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
810 case FMODE_READ
|FMODE_WRITE
:
813 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
816 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
818 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
819 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
820 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
823 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
826 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
828 case FMODE_READ
|FMODE_WRITE
:
829 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
833 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
835 write_seqlock(&state
->seqlock
);
836 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
837 write_sequnlock(&state
->seqlock
);
840 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
843 * Protect the call to nfs4_state_set_mode_locked and
844 * serialise the stateid update
846 write_seqlock(&state
->seqlock
);
847 if (deleg_stateid
!= NULL
) {
848 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
849 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
851 if (open_stateid
!= NULL
)
852 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
853 write_sequnlock(&state
->seqlock
);
854 spin_lock(&state
->owner
->so_lock
);
855 update_open_stateflags(state
, fmode
);
856 spin_unlock(&state
->owner
->so_lock
);
859 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
861 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
862 struct nfs_delegation
*deleg_cur
;
865 fmode
&= (FMODE_READ
|FMODE_WRITE
);
868 deleg_cur
= rcu_dereference(nfsi
->delegation
);
869 if (deleg_cur
== NULL
)
872 spin_lock(&deleg_cur
->lock
);
873 if (nfsi
->delegation
!= deleg_cur
||
874 (deleg_cur
->type
& fmode
) != fmode
)
875 goto no_delegation_unlock
;
877 if (delegation
== NULL
)
878 delegation
= &deleg_cur
->stateid
;
879 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
880 goto no_delegation_unlock
;
882 nfs_mark_delegation_referenced(deleg_cur
);
883 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
885 no_delegation_unlock
:
886 spin_unlock(&deleg_cur
->lock
);
890 if (!ret
&& open_stateid
!= NULL
) {
891 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
899 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
901 struct nfs_delegation
*delegation
;
904 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
905 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
910 nfs_inode_return_delegation(inode
);
913 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
915 struct nfs4_state
*state
= opendata
->state
;
916 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
917 struct nfs_delegation
*delegation
;
918 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
919 fmode_t fmode
= opendata
->o_arg
.fmode
;
920 nfs4_stateid stateid
;
924 if (can_open_cached(state
, fmode
, open_mode
)) {
925 spin_lock(&state
->owner
->so_lock
);
926 if (can_open_cached(state
, fmode
, open_mode
)) {
927 update_open_stateflags(state
, fmode
);
928 spin_unlock(&state
->owner
->so_lock
);
929 goto out_return_state
;
931 spin_unlock(&state
->owner
->so_lock
);
934 delegation
= rcu_dereference(nfsi
->delegation
);
935 if (delegation
== NULL
||
936 !can_open_delegated(delegation
, fmode
)) {
940 /* Save the delegation */
941 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
943 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
948 /* Try to update the stateid using the delegation */
949 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
950 goto out_return_state
;
955 atomic_inc(&state
->count
);
959 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
962 struct nfs4_state
*state
= NULL
;
963 struct nfs_delegation
*delegation
;
966 if (!data
->rpc_done
) {
967 state
= nfs4_try_open_cached(data
);
972 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
974 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
975 ret
= PTR_ERR(inode
);
979 state
= nfs4_get_open_state(inode
, data
->owner
);
982 if (data
->o_res
.delegation_type
!= 0) {
983 int delegation_flags
= 0;
986 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
988 delegation_flags
= delegation
->flags
;
990 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
991 nfs_inode_set_delegation(state
->inode
,
992 data
->owner
->so_cred
,
995 nfs_inode_reclaim_delegation(state
->inode
,
996 data
->owner
->so_cred
,
1000 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1008 return ERR_PTR(ret
);
1011 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1013 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1014 struct nfs_open_context
*ctx
;
1016 spin_lock(&state
->inode
->i_lock
);
1017 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1018 if (ctx
->state
!= state
)
1020 get_nfs_open_context(ctx
);
1021 spin_unlock(&state
->inode
->i_lock
);
1024 spin_unlock(&state
->inode
->i_lock
);
1025 return ERR_PTR(-ENOENT
);
1028 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1030 struct nfs4_opendata
*opendata
;
1032 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
);
1033 if (opendata
== NULL
)
1034 return ERR_PTR(-ENOMEM
);
1035 opendata
->state
= state
;
1036 atomic_inc(&state
->count
);
1040 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1042 struct nfs4_state
*newstate
;
1045 opendata
->o_arg
.open_flags
= 0;
1046 opendata
->o_arg
.fmode
= fmode
;
1047 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1048 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1049 nfs4_init_opendata_res(opendata
);
1050 ret
= _nfs4_proc_open(opendata
);
1053 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1054 if (IS_ERR(newstate
))
1055 return PTR_ERR(newstate
);
1056 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1061 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1063 struct nfs4_state
*newstate
;
1066 /* memory barrier prior to reading state->n_* */
1067 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1069 if (state
->n_rdwr
!= 0) {
1070 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1071 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1074 if (newstate
!= state
)
1077 if (state
->n_wronly
!= 0) {
1078 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1079 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1082 if (newstate
!= state
)
1085 if (state
->n_rdonly
!= 0) {
1086 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1087 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1090 if (newstate
!= state
)
1094 * We may have performed cached opens for all three recoveries.
1095 * Check if we need to update the current stateid.
1097 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1098 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1099 write_seqlock(&state
->seqlock
);
1100 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1101 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1102 write_sequnlock(&state
->seqlock
);
1109 * reclaim state on the server after a reboot.
1111 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1113 struct nfs_delegation
*delegation
;
1114 struct nfs4_opendata
*opendata
;
1115 fmode_t delegation_type
= 0;
1118 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1119 if (IS_ERR(opendata
))
1120 return PTR_ERR(opendata
);
1121 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1122 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1124 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1125 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1126 delegation_type
= delegation
->type
;
1128 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1129 status
= nfs4_open_recover(opendata
, state
);
1130 nfs4_opendata_put(opendata
);
1134 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1136 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1137 struct nfs4_exception exception
= { };
1140 err
= _nfs4_do_open_reclaim(ctx
, state
);
1141 if (err
!= -NFS4ERR_DELAY
)
1143 nfs4_handle_exception(server
, err
, &exception
);
1144 } while (exception
.retry
);
1148 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1150 struct nfs_open_context
*ctx
;
1153 ctx
= nfs4_state_find_open_context(state
);
1155 return PTR_ERR(ctx
);
1156 ret
= nfs4_do_open_reclaim(ctx
, state
);
1157 put_nfs_open_context(ctx
);
1161 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1163 struct nfs4_opendata
*opendata
;
1166 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1167 if (IS_ERR(opendata
))
1168 return PTR_ERR(opendata
);
1169 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1170 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1171 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1172 ret
= nfs4_open_recover(opendata
, state
);
1173 nfs4_opendata_put(opendata
);
1177 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1179 struct nfs4_exception exception
= { };
1180 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1183 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1189 case -NFS4ERR_STALE_CLIENTID
:
1190 case -NFS4ERR_STALE_STATEID
:
1191 case -NFS4ERR_EXPIRED
:
1192 /* Don't recall a delegation if it was lost */
1193 nfs4_schedule_state_recovery(server
->nfs_client
);
1197 * The show must go on: exit, but mark the
1198 * stateid as needing recovery.
1200 case -NFS4ERR_ADMIN_REVOKED
:
1201 case -NFS4ERR_BAD_STATEID
:
1202 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
1207 err
= nfs4_handle_exception(server
, err
, &exception
);
1208 } while (exception
.retry
);
1213 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1215 struct nfs4_opendata
*data
= calldata
;
1217 data
->rpc_status
= task
->tk_status
;
1218 if (RPC_ASSASSINATED(task
))
1220 if (data
->rpc_status
== 0) {
1221 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1222 sizeof(data
->o_res
.stateid
.data
));
1223 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1224 renew_lease(data
->o_res
.server
, data
->timestamp
);
1229 static void nfs4_open_confirm_release(void *calldata
)
1231 struct nfs4_opendata
*data
= calldata
;
1232 struct nfs4_state
*state
= NULL
;
1234 /* If this request hasn't been cancelled, do nothing */
1235 if (data
->cancelled
== 0)
1237 /* In case of error, no cleanup! */
1238 if (!data
->rpc_done
)
1240 state
= nfs4_opendata_to_nfs4_state(data
);
1242 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1244 nfs4_opendata_put(data
);
1247 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1248 .rpc_call_done
= nfs4_open_confirm_done
,
1249 .rpc_release
= nfs4_open_confirm_release
,
1253 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1255 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1257 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1258 struct rpc_task
*task
;
1259 struct rpc_message msg
= {
1260 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1261 .rpc_argp
= &data
->c_arg
,
1262 .rpc_resp
= &data
->c_res
,
1263 .rpc_cred
= data
->owner
->so_cred
,
1265 struct rpc_task_setup task_setup_data
= {
1266 .rpc_client
= server
->client
,
1267 .rpc_message
= &msg
,
1268 .callback_ops
= &nfs4_open_confirm_ops
,
1269 .callback_data
= data
,
1270 .workqueue
= nfsiod_workqueue
,
1271 .flags
= RPC_TASK_ASYNC
,
1275 kref_get(&data
->kref
);
1277 data
->rpc_status
= 0;
1278 data
->timestamp
= jiffies
;
1279 task
= rpc_run_task(&task_setup_data
);
1281 return PTR_ERR(task
);
1282 status
= nfs4_wait_for_completion_rpc_task(task
);
1284 data
->cancelled
= 1;
1287 status
= data
->rpc_status
;
1292 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1294 struct nfs4_opendata
*data
= calldata
;
1295 struct nfs4_state_owner
*sp
= data
->owner
;
1297 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1300 * Check if we still need to send an OPEN call, or if we can use
1301 * a delegation instead.
1303 if (data
->state
!= NULL
) {
1304 struct nfs_delegation
*delegation
;
1306 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1309 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1310 if (delegation
!= NULL
&&
1311 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1317 /* Update sequence id. */
1318 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1319 data
->o_arg
.clientid
= sp
->so_client
->cl_clientid
;
1320 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1321 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1322 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1324 data
->timestamp
= jiffies
;
1325 if (nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
1326 &data
->o_arg
.seq_args
,
1327 &data
->o_res
.seq_res
, 1, task
))
1329 rpc_call_start(task
);
1332 task
->tk_action
= NULL
;
1336 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1338 struct nfs4_opendata
*data
= calldata
;
1340 data
->rpc_status
= task
->tk_status
;
1342 nfs4_sequence_done_free_slot(data
->o_arg
.server
, &data
->o_res
.seq_res
,
1345 if (RPC_ASSASSINATED(task
))
1347 if (task
->tk_status
== 0) {
1348 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1352 data
->rpc_status
= -ELOOP
;
1355 data
->rpc_status
= -EISDIR
;
1358 data
->rpc_status
= -ENOTDIR
;
1360 renew_lease(data
->o_res
.server
, data
->timestamp
);
1361 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1362 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1367 static void nfs4_open_release(void *calldata
)
1369 struct nfs4_opendata
*data
= calldata
;
1370 struct nfs4_state
*state
= NULL
;
1372 /* If this request hasn't been cancelled, do nothing */
1373 if (data
->cancelled
== 0)
1375 /* In case of error, no cleanup! */
1376 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1378 /* In case we need an open_confirm, no cleanup! */
1379 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1381 state
= nfs4_opendata_to_nfs4_state(data
);
1383 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1385 nfs4_opendata_put(data
);
1388 static const struct rpc_call_ops nfs4_open_ops
= {
1389 .rpc_call_prepare
= nfs4_open_prepare
,
1390 .rpc_call_done
= nfs4_open_done
,
1391 .rpc_release
= nfs4_open_release
,
1395 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1397 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1399 struct inode
*dir
= data
->dir
->d_inode
;
1400 struct nfs_server
*server
= NFS_SERVER(dir
);
1401 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1402 struct nfs_openres
*o_res
= &data
->o_res
;
1403 struct rpc_task
*task
;
1404 struct rpc_message msg
= {
1405 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1408 .rpc_cred
= data
->owner
->so_cred
,
1410 struct rpc_task_setup task_setup_data
= {
1411 .rpc_client
= server
->client
,
1412 .rpc_message
= &msg
,
1413 .callback_ops
= &nfs4_open_ops
,
1414 .callback_data
= data
,
1415 .workqueue
= nfsiod_workqueue
,
1416 .flags
= RPC_TASK_ASYNC
,
1420 kref_get(&data
->kref
);
1422 data
->rpc_status
= 0;
1423 data
->cancelled
= 0;
1424 task
= rpc_run_task(&task_setup_data
);
1426 return PTR_ERR(task
);
1427 status
= nfs4_wait_for_completion_rpc_task(task
);
1429 data
->cancelled
= 1;
1432 status
= data
->rpc_status
;
1434 if (status
!= 0 || !data
->rpc_done
)
1437 if (o_res
->fh
.size
== 0)
1438 _nfs4_proc_lookup(dir
, o_arg
->name
, &o_res
->fh
, o_res
->f_attr
);
1440 if (o_arg
->open_flags
& O_CREAT
) {
1441 update_changeattr(dir
, &o_res
->cinfo
);
1442 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1444 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1445 if ((o_res
->rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) == 0)
1446 server
->caps
&= ~NFS_CAP_POSIX_LOCK
;
1447 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1448 status
= _nfs4_proc_open_confirm(data
);
1452 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1453 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1457 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1459 struct nfs_client
*clp
= server
->nfs_client
;
1463 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1464 ret
= nfs4_wait_clnt_recover(clp
);
1467 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1468 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1470 nfs4_schedule_state_recovery(clp
);
1478 * reclaim state on the server after a network partition.
1479 * Assumes caller holds the appropriate lock
1481 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1483 struct nfs4_opendata
*opendata
;
1486 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1487 if (IS_ERR(opendata
))
1488 return PTR_ERR(opendata
);
1489 ret
= nfs4_open_recover(opendata
, state
);
1491 d_drop(ctx
->path
.dentry
);
1492 nfs4_opendata_put(opendata
);
1496 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1498 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1499 struct nfs4_exception exception
= { };
1503 err
= _nfs4_open_expired(ctx
, state
);
1507 case -NFS4ERR_GRACE
:
1508 case -NFS4ERR_DELAY
:
1509 nfs4_handle_exception(server
, err
, &exception
);
1512 } while (exception
.retry
);
1517 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1519 struct nfs_open_context
*ctx
;
1522 ctx
= nfs4_state_find_open_context(state
);
1524 return PTR_ERR(ctx
);
1525 ret
= nfs4_do_open_expired(ctx
, state
);
1526 put_nfs_open_context(ctx
);
1531 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1532 * fields corresponding to attributes that were used to store the verifier.
1533 * Make sure we clobber those fields in the later setattr call
1535 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1537 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1538 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1539 sattr
->ia_valid
|= ATTR_ATIME
;
1541 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1542 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1543 sattr
->ia_valid
|= ATTR_MTIME
;
1547 * Returns a referenced nfs4_state
1549 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
)
1551 struct nfs4_state_owner
*sp
;
1552 struct nfs4_state
*state
= NULL
;
1553 struct nfs_server
*server
= NFS_SERVER(dir
);
1554 struct nfs4_opendata
*opendata
;
1557 /* Protect against reboot recovery conflicts */
1559 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1560 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1563 status
= nfs4_recover_expired_lease(server
);
1565 goto err_put_state_owner
;
1566 if (path
->dentry
->d_inode
!= NULL
)
1567 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1569 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
);
1570 if (opendata
== NULL
)
1571 goto err_put_state_owner
;
1573 if (path
->dentry
->d_inode
!= NULL
)
1574 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1576 status
= _nfs4_proc_open(opendata
);
1578 goto err_opendata_put
;
1580 if (opendata
->o_arg
.open_flags
& O_EXCL
)
1581 nfs4_exclusive_attrset(opendata
, sattr
);
1583 state
= nfs4_opendata_to_nfs4_state(opendata
);
1584 status
= PTR_ERR(state
);
1586 goto err_opendata_put
;
1587 if (server
->caps
& NFS_CAP_POSIX_LOCK
)
1588 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1589 nfs_revalidate_inode(server
, state
->inode
);
1590 nfs4_opendata_put(opendata
);
1591 nfs4_put_state_owner(sp
);
1595 nfs4_opendata_put(opendata
);
1596 err_put_state_owner
:
1597 nfs4_put_state_owner(sp
);
1604 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
)
1606 struct nfs4_exception exception
= { };
1607 struct nfs4_state
*res
;
1611 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1614 /* NOTE: BAD_SEQID means the server and client disagree about the
1615 * book-keeping w.r.t. state-changing operations
1616 * (OPEN/CLOSE/LOCK/LOCKU...)
1617 * It is actually a sign of a bug on the client or on the server.
1619 * If we receive a BAD_SEQID error in the particular case of
1620 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1621 * have unhashed the old state_owner for us, and that we can
1622 * therefore safely retry using a new one. We should still warn
1623 * the user though...
1625 if (status
== -NFS4ERR_BAD_SEQID
) {
1626 printk(KERN_WARNING
"NFS: v4 server %s "
1627 " returned a bad sequence-id error!\n",
1628 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1629 exception
.retry
= 1;
1633 * BAD_STATEID on OPEN means that the server cancelled our
1634 * state before it received the OPEN_CONFIRM.
1635 * Recover by retrying the request as per the discussion
1636 * on Page 181 of RFC3530.
1638 if (status
== -NFS4ERR_BAD_STATEID
) {
1639 exception
.retry
= 1;
1642 if (status
== -EAGAIN
) {
1643 /* We must have found a delegation */
1644 exception
.retry
= 1;
1647 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1648 status
, &exception
));
1649 } while (exception
.retry
);
1653 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1654 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1655 struct nfs4_state
*state
)
1657 struct nfs_server
*server
= NFS_SERVER(inode
);
1658 struct nfs_setattrargs arg
= {
1659 .fh
= NFS_FH(inode
),
1662 .bitmask
= server
->attr_bitmask
,
1664 struct nfs_setattrres res
= {
1668 struct rpc_message msg
= {
1669 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1674 unsigned long timestamp
= jiffies
;
1677 nfs_fattr_init(fattr
);
1679 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1680 /* Use that stateid */
1681 } else if (state
!= NULL
) {
1682 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1684 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1686 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1687 if (status
== 0 && state
!= NULL
)
1688 renew_lease(server
, timestamp
);
1692 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1693 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1694 struct nfs4_state
*state
)
1696 struct nfs_server
*server
= NFS_SERVER(inode
);
1697 struct nfs4_exception exception
= { };
1700 err
= nfs4_handle_exception(server
,
1701 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1703 } while (exception
.retry
);
1707 struct nfs4_closedata
{
1709 struct inode
*inode
;
1710 struct nfs4_state
*state
;
1711 struct nfs_closeargs arg
;
1712 struct nfs_closeres res
;
1713 struct nfs_fattr fattr
;
1714 unsigned long timestamp
;
1717 static void nfs4_free_closedata(void *data
)
1719 struct nfs4_closedata
*calldata
= data
;
1720 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1722 nfs4_put_open_state(calldata
->state
);
1723 nfs_free_seqid(calldata
->arg
.seqid
);
1724 nfs4_put_state_owner(sp
);
1725 path_put(&calldata
->path
);
1729 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1731 struct nfs4_closedata
*calldata
= data
;
1732 struct nfs4_state
*state
= calldata
->state
;
1733 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1735 nfs4_sequence_done(server
, &calldata
->res
.seq_res
, task
->tk_status
);
1736 if (RPC_ASSASSINATED(task
))
1738 /* hmm. we are done with the inode, and in the process of freeing
1739 * the state_owner. we keep this around to process errors
1741 switch (task
->tk_status
) {
1743 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1744 renew_lease(server
, calldata
->timestamp
);
1746 case -NFS4ERR_STALE_STATEID
:
1747 case -NFS4ERR_OLD_STATEID
:
1748 case -NFS4ERR_BAD_STATEID
:
1749 case -NFS4ERR_EXPIRED
:
1750 if (calldata
->arg
.fmode
== 0)
1753 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
) {
1754 nfs4_restart_rpc(task
, server
->nfs_client
);
1758 nfs4_sequence_free_slot(server
->nfs_client
, &calldata
->res
.seq_res
);
1759 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1762 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1764 struct nfs4_closedata
*calldata
= data
;
1765 struct nfs4_state
*state
= calldata
->state
;
1766 int clear_rd
, clear_wr
, clear_rdwr
;
1768 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1771 clear_rd
= clear_wr
= clear_rdwr
= 0;
1772 spin_lock(&state
->owner
->so_lock
);
1773 /* Calculate the change in open mode */
1774 if (state
->n_rdwr
== 0) {
1775 if (state
->n_rdonly
== 0) {
1776 clear_rd
|= test_and_clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1777 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1779 if (state
->n_wronly
== 0) {
1780 clear_wr
|= test_and_clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1781 clear_rdwr
|= test_and_clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1784 spin_unlock(&state
->owner
->so_lock
);
1785 if (!clear_rd
&& !clear_wr
&& !clear_rdwr
) {
1786 /* Note: exit _without_ calling nfs4_close_done */
1787 task
->tk_action
= NULL
;
1790 nfs_fattr_init(calldata
->res
.fattr
);
1791 if (test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0) {
1792 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1793 calldata
->arg
.fmode
= FMODE_READ
;
1794 } else if (test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0) {
1795 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1796 calldata
->arg
.fmode
= FMODE_WRITE
;
1798 calldata
->timestamp
= jiffies
;
1799 if (nfs4_setup_sequence((NFS_SERVER(calldata
->inode
))->nfs_client
,
1800 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1803 rpc_call_start(task
);
1806 static const struct rpc_call_ops nfs4_close_ops
= {
1807 .rpc_call_prepare
= nfs4_close_prepare
,
1808 .rpc_call_done
= nfs4_close_done
,
1809 .rpc_release
= nfs4_free_closedata
,
1813 * It is possible for data to be read/written from a mem-mapped file
1814 * after the sys_close call (which hits the vfs layer as a flush).
1815 * This means that we can't safely call nfsv4 close on a file until
1816 * the inode is cleared. This in turn means that we are not good
1817 * NFSv4 citizens - we do not indicate to the server to update the file's
1818 * share state even when we are done with one of the three share
1819 * stateid's in the inode.
1821 * NOTE: Caller must be holding the sp->so_owner semaphore!
1823 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, int wait
)
1825 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1826 struct nfs4_closedata
*calldata
;
1827 struct nfs4_state_owner
*sp
= state
->owner
;
1828 struct rpc_task
*task
;
1829 struct rpc_message msg
= {
1830 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1831 .rpc_cred
= state
->owner
->so_cred
,
1833 struct rpc_task_setup task_setup_data
= {
1834 .rpc_client
= server
->client
,
1835 .rpc_message
= &msg
,
1836 .callback_ops
= &nfs4_close_ops
,
1837 .workqueue
= nfsiod_workqueue
,
1838 .flags
= RPC_TASK_ASYNC
,
1840 int status
= -ENOMEM
;
1842 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
1843 if (calldata
== NULL
)
1845 calldata
->inode
= state
->inode
;
1846 calldata
->state
= state
;
1847 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1848 calldata
->arg
.stateid
= &state
->open_stateid
;
1849 if (nfs4_has_session(server
->nfs_client
))
1850 memset(calldata
->arg
.stateid
->data
, 0, 4); /* clear seqid */
1851 /* Serialization for the sequence id */
1852 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
1853 if (calldata
->arg
.seqid
== NULL
)
1854 goto out_free_calldata
;
1855 calldata
->arg
.fmode
= 0;
1856 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1857 calldata
->res
.fattr
= &calldata
->fattr
;
1858 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1859 calldata
->res
.server
= server
;
1860 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1861 calldata
->path
.mnt
= mntget(path
->mnt
);
1862 calldata
->path
.dentry
= dget(path
->dentry
);
1864 msg
.rpc_argp
= &calldata
->arg
,
1865 msg
.rpc_resp
= &calldata
->res
,
1866 task_setup_data
.callback_data
= calldata
;
1867 task
= rpc_run_task(&task_setup_data
);
1869 return PTR_ERR(task
);
1872 status
= rpc_wait_for_completion_task(task
);
1878 nfs4_put_open_state(state
);
1879 nfs4_put_state_owner(sp
);
1883 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1888 /* If the open_intent is for execute, we have an extra check to make */
1889 if (fmode
& FMODE_EXEC
) {
1890 ret
= nfs_may_open(state
->inode
,
1891 state
->owner
->so_cred
,
1892 nd
->intent
.open
.flags
);
1896 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1897 if (!IS_ERR(filp
)) {
1898 struct nfs_open_context
*ctx
;
1899 ctx
= nfs_file_open_context(filp
);
1903 ret
= PTR_ERR(filp
);
1905 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
1910 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
1912 struct path path
= {
1913 .mnt
= nd
->path
.mnt
,
1916 struct dentry
*parent
;
1918 struct rpc_cred
*cred
;
1919 struct nfs4_state
*state
;
1921 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
1923 if (nd
->flags
& LOOKUP_CREATE
) {
1924 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
1925 attr
.ia_valid
= ATTR_MODE
;
1926 if (!IS_POSIXACL(dir
))
1927 attr
.ia_mode
&= ~current_umask();
1930 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
1933 cred
= rpc_lookup_cred();
1935 return (struct dentry
*)cred
;
1936 parent
= dentry
->d_parent
;
1937 /* Protect against concurrent sillydeletes */
1938 nfs_block_sillyrename(parent
);
1939 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
1941 if (IS_ERR(state
)) {
1942 if (PTR_ERR(state
) == -ENOENT
) {
1943 d_add(dentry
, NULL
);
1944 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1946 nfs_unblock_sillyrename(parent
);
1947 return (struct dentry
*)state
;
1949 res
= d_add_unique(dentry
, igrab(state
->inode
));
1952 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
1953 nfs_unblock_sillyrename(parent
);
1954 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1959 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
1961 struct path path
= {
1962 .mnt
= nd
->path
.mnt
,
1965 struct rpc_cred
*cred
;
1966 struct nfs4_state
*state
;
1967 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
1969 cred
= rpc_lookup_cred();
1971 return PTR_ERR(cred
);
1972 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
1974 if (IS_ERR(state
)) {
1975 switch (PTR_ERR(state
)) {
1981 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
1987 if (state
->inode
== dentry
->d_inode
) {
1988 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
1989 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
1992 nfs4_close_sync(&path
, state
, fmode
);
1998 void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2000 if (ctx
->state
== NULL
)
2003 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2005 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2008 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2010 struct nfs4_server_caps_arg args
= {
2013 struct nfs4_server_caps_res res
= {};
2014 struct rpc_message msg
= {
2015 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2021 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2023 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2024 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2025 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2026 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2027 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2028 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2029 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2030 server
->caps
|= NFS_CAP_ACLS
;
2031 if (res
.has_links
!= 0)
2032 server
->caps
|= NFS_CAP_HARDLINKS
;
2033 if (res
.has_symlinks
!= 0)
2034 server
->caps
|= NFS_CAP_SYMLINKS
;
2035 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2036 server
->caps
|= NFS_CAP_FILEID
;
2037 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2038 server
->caps
|= NFS_CAP_MODE
;
2039 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2040 server
->caps
|= NFS_CAP_NLINK
;
2041 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2042 server
->caps
|= NFS_CAP_OWNER
;
2043 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2044 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2045 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2046 server
->caps
|= NFS_CAP_ATIME
;
2047 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2048 server
->caps
|= NFS_CAP_CTIME
;
2049 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2050 server
->caps
|= NFS_CAP_MTIME
;
2052 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2053 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2054 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2055 server
->acl_bitmask
= res
.acl_bitmask
;
2061 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2063 struct nfs4_exception exception
= { };
2066 err
= nfs4_handle_exception(server
,
2067 _nfs4_server_capabilities(server
, fhandle
),
2069 } while (exception
.retry
);
2073 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2074 struct nfs_fsinfo
*info
)
2076 struct nfs4_lookup_root_arg args
= {
2077 .bitmask
= nfs4_fattr_bitmap
,
2079 struct nfs4_lookup_res res
= {
2081 .fattr
= info
->fattr
,
2084 struct rpc_message msg
= {
2085 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2090 nfs_fattr_init(info
->fattr
);
2091 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2094 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2095 struct nfs_fsinfo
*info
)
2097 struct nfs4_exception exception
= { };
2100 err
= nfs4_handle_exception(server
,
2101 _nfs4_lookup_root(server
, fhandle
, info
),
2103 } while (exception
.retry
);
2108 * get the file handle for the "/" directory on the server
2110 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2111 struct nfs_fsinfo
*info
)
2115 status
= nfs4_lookup_root(server
, fhandle
, info
);
2117 status
= nfs4_server_capabilities(server
, fhandle
);
2119 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2120 return nfs4_map_errors(status
);
2124 * Get locations and (maybe) other attributes of a referral.
2125 * Note that we'll actually follow the referral later when
2126 * we detect fsid mismatch in inode revalidation
2128 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2130 int status
= -ENOMEM
;
2131 struct page
*page
= NULL
;
2132 struct nfs4_fs_locations
*locations
= NULL
;
2134 page
= alloc_page(GFP_KERNEL
);
2137 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2138 if (locations
== NULL
)
2141 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2144 /* Make sure server returned a different fsid for the referral */
2145 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2146 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2151 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2152 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2154 fattr
->mode
= S_IFDIR
;
2155 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2164 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2166 struct nfs4_getattr_arg args
= {
2168 .bitmask
= server
->attr_bitmask
,
2170 struct nfs4_getattr_res res
= {
2174 struct rpc_message msg
= {
2175 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2180 nfs_fattr_init(fattr
);
2181 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2184 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2186 struct nfs4_exception exception
= { };
2189 err
= nfs4_handle_exception(server
,
2190 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2192 } while (exception
.retry
);
2197 * The file is not closed if it is opened due to the a request to change
2198 * the size of the file. The open call will not be needed once the
2199 * VFS layer lookup-intents are implemented.
2201 * Close is called when the inode is destroyed.
2202 * If we haven't opened the file for O_WRONLY, we
2203 * need to in the size_change case to obtain a stateid.
2206 * Because OPEN is always done by name in nfsv4, it is
2207 * possible that we opened a different file by the same
2208 * name. We can recognize this race condition, but we
2209 * can't do anything about it besides returning an error.
2211 * This will be fixed with VFS changes (lookup-intent).
2214 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2215 struct iattr
*sattr
)
2217 struct inode
*inode
= dentry
->d_inode
;
2218 struct rpc_cred
*cred
= NULL
;
2219 struct nfs4_state
*state
= NULL
;
2222 nfs_fattr_init(fattr
);
2224 /* Search for an existing open(O_WRITE) file */
2225 if (sattr
->ia_valid
& ATTR_FILE
) {
2226 struct nfs_open_context
*ctx
;
2228 ctx
= nfs_file_open_context(sattr
->ia_file
);
2235 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2237 nfs_setattr_update_inode(inode
, sattr
);
2241 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2242 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2243 struct nfs_fattr
*fattr
)
2246 struct nfs4_lookup_arg args
= {
2247 .bitmask
= server
->attr_bitmask
,
2251 struct nfs4_lookup_res res
= {
2256 struct rpc_message msg
= {
2257 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2262 nfs_fattr_init(fattr
);
2264 dprintk("NFS call lookupfh %s\n", name
->name
);
2265 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2266 dprintk("NFS reply lookupfh: %d\n", status
);
2270 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2271 struct qstr
*name
, struct nfs_fh
*fhandle
,
2272 struct nfs_fattr
*fattr
)
2274 struct nfs4_exception exception
= { };
2277 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2279 if (err
== -NFS4ERR_MOVED
) {
2283 err
= nfs4_handle_exception(server
, err
, &exception
);
2284 } while (exception
.retry
);
2288 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2289 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2293 dprintk("NFS call lookup %s\n", name
->name
);
2294 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2295 if (status
== -NFS4ERR_MOVED
)
2296 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2297 dprintk("NFS reply lookup: %d\n", status
);
2301 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2303 struct nfs4_exception exception
= { };
2306 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2307 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2309 } while (exception
.retry
);
2313 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2315 struct nfs_server
*server
= NFS_SERVER(inode
);
2316 struct nfs_fattr fattr
;
2317 struct nfs4_accessargs args
= {
2318 .fh
= NFS_FH(inode
),
2319 .bitmask
= server
->attr_bitmask
,
2321 struct nfs4_accessres res
= {
2325 struct rpc_message msg
= {
2326 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2329 .rpc_cred
= entry
->cred
,
2331 int mode
= entry
->mask
;
2335 * Determine which access bits we want to ask for...
2337 if (mode
& MAY_READ
)
2338 args
.access
|= NFS4_ACCESS_READ
;
2339 if (S_ISDIR(inode
->i_mode
)) {
2340 if (mode
& MAY_WRITE
)
2341 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2342 if (mode
& MAY_EXEC
)
2343 args
.access
|= NFS4_ACCESS_LOOKUP
;
2345 if (mode
& MAY_WRITE
)
2346 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2347 if (mode
& MAY_EXEC
)
2348 args
.access
|= NFS4_ACCESS_EXECUTE
;
2350 nfs_fattr_init(&fattr
);
2351 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2354 if (res
.access
& NFS4_ACCESS_READ
)
2355 entry
->mask
|= MAY_READ
;
2356 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2357 entry
->mask
|= MAY_WRITE
;
2358 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2359 entry
->mask
|= MAY_EXEC
;
2360 nfs_refresh_inode(inode
, &fattr
);
2365 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2367 struct nfs4_exception exception
= { };
2370 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2371 _nfs4_proc_access(inode
, entry
),
2373 } while (exception
.retry
);
2378 * TODO: For the time being, we don't try to get any attributes
2379 * along with any of the zero-copy operations READ, READDIR,
2382 * In the case of the first three, we want to put the GETATTR
2383 * after the read-type operation -- this is because it is hard
2384 * to predict the length of a GETATTR response in v4, and thus
2385 * align the READ data correctly. This means that the GETATTR
2386 * may end up partially falling into the page cache, and we should
2387 * shift it into the 'tail' of the xdr_buf before processing.
2388 * To do this efficiently, we need to know the total length
2389 * of data received, which doesn't seem to be available outside
2392 * In the case of WRITE, we also want to put the GETATTR after
2393 * the operation -- in this case because we want to make sure
2394 * we get the post-operation mtime and size. This means that
2395 * we can't use xdr_encode_pages() as written: we need a variant
2396 * of it which would leave room in the 'tail' iovec.
2398 * Both of these changes to the XDR layer would in fact be quite
2399 * minor, but I decided to leave them for a subsequent patch.
2401 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2402 unsigned int pgbase
, unsigned int pglen
)
2404 struct nfs4_readlink args
= {
2405 .fh
= NFS_FH(inode
),
2410 struct nfs4_readlink_res res
;
2411 struct rpc_message msg
= {
2412 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2417 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2420 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2421 unsigned int pgbase
, unsigned int pglen
)
2423 struct nfs4_exception exception
= { };
2426 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2427 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2429 } while (exception
.retry
);
2435 * We will need to arrange for the VFS layer to provide an atomic open.
2436 * Until then, this create/open method is prone to inefficiency and race
2437 * conditions due to the lookup, create, and open VFS calls from sys_open()
2438 * placed on the wire.
2440 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2441 * The file will be opened again in the subsequent VFS open call
2442 * (nfs4_proc_file_open).
2444 * The open for read will just hang around to be used by any process that
2445 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2449 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2450 int flags
, struct nameidata
*nd
)
2452 struct path path
= {
2453 .mnt
= nd
->path
.mnt
,
2456 struct nfs4_state
*state
;
2457 struct rpc_cred
*cred
;
2458 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2461 cred
= rpc_lookup_cred();
2463 status
= PTR_ERR(cred
);
2466 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2468 if (IS_ERR(state
)) {
2469 status
= PTR_ERR(state
);
2472 d_add(dentry
, igrab(state
->inode
));
2473 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2474 if (flags
& O_EXCL
) {
2475 struct nfs_fattr fattr
;
2476 status
= nfs4_do_setattr(state
->inode
, cred
, &fattr
, sattr
, state
);
2478 nfs_setattr_update_inode(state
->inode
, sattr
);
2479 nfs_post_op_update_inode(state
->inode
, &fattr
);
2481 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2482 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2484 nfs4_close_sync(&path
, state
, fmode
);
2491 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2493 struct nfs_server
*server
= NFS_SERVER(dir
);
2494 struct nfs_removeargs args
= {
2496 .name
.len
= name
->len
,
2497 .name
.name
= name
->name
,
2498 .bitmask
= server
->attr_bitmask
,
2500 struct nfs_removeres res
= {
2503 struct rpc_message msg
= {
2504 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2510 nfs_fattr_init(&res
.dir_attr
);
2511 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2513 update_changeattr(dir
, &res
.cinfo
);
2514 nfs_post_op_update_inode(dir
, &res
.dir_attr
);
2519 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2521 struct nfs4_exception exception
= { };
2524 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2525 _nfs4_proc_remove(dir
, name
),
2527 } while (exception
.retry
);
2531 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2533 struct nfs_server
*server
= NFS_SERVER(dir
);
2534 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2535 struct nfs_removeres
*res
= msg
->rpc_resp
;
2537 args
->bitmask
= server
->cache_consistency_bitmask
;
2538 res
->server
= server
;
2539 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2542 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2544 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2546 nfs4_sequence_done(res
->server
, &res
->seq_res
, task
->tk_status
);
2547 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2549 nfs4_sequence_free_slot(res
->server
->nfs_client
, &res
->seq_res
);
2550 update_changeattr(dir
, &res
->cinfo
);
2551 nfs_post_op_update_inode(dir
, &res
->dir_attr
);
2555 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2556 struct inode
*new_dir
, struct qstr
*new_name
)
2558 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2559 struct nfs4_rename_arg arg
= {
2560 .old_dir
= NFS_FH(old_dir
),
2561 .new_dir
= NFS_FH(new_dir
),
2562 .old_name
= old_name
,
2563 .new_name
= new_name
,
2564 .bitmask
= server
->attr_bitmask
,
2566 struct nfs_fattr old_fattr
, new_fattr
;
2567 struct nfs4_rename_res res
= {
2569 .old_fattr
= &old_fattr
,
2570 .new_fattr
= &new_fattr
,
2572 struct rpc_message msg
= {
2573 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2579 nfs_fattr_init(res
.old_fattr
);
2580 nfs_fattr_init(res
.new_fattr
);
2581 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2584 update_changeattr(old_dir
, &res
.old_cinfo
);
2585 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2586 update_changeattr(new_dir
, &res
.new_cinfo
);
2587 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2592 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2593 struct inode
*new_dir
, struct qstr
*new_name
)
2595 struct nfs4_exception exception
= { };
2598 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2599 _nfs4_proc_rename(old_dir
, old_name
,
2602 } while (exception
.retry
);
2606 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2608 struct nfs_server
*server
= NFS_SERVER(inode
);
2609 struct nfs4_link_arg arg
= {
2610 .fh
= NFS_FH(inode
),
2611 .dir_fh
= NFS_FH(dir
),
2613 .bitmask
= server
->attr_bitmask
,
2615 struct nfs_fattr fattr
, dir_attr
;
2616 struct nfs4_link_res res
= {
2619 .dir_attr
= &dir_attr
,
2621 struct rpc_message msg
= {
2622 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2628 nfs_fattr_init(res
.fattr
);
2629 nfs_fattr_init(res
.dir_attr
);
2630 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2632 update_changeattr(dir
, &res
.cinfo
);
2633 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2634 nfs_post_op_update_inode(inode
, res
.fattr
);
2640 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2642 struct nfs4_exception exception
= { };
2645 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2646 _nfs4_proc_link(inode
, dir
, name
),
2648 } while (exception
.retry
);
2652 struct nfs4_createdata
{
2653 struct rpc_message msg
;
2654 struct nfs4_create_arg arg
;
2655 struct nfs4_create_res res
;
2657 struct nfs_fattr fattr
;
2658 struct nfs_fattr dir_fattr
;
2661 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2662 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2664 struct nfs4_createdata
*data
;
2666 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2668 struct nfs_server
*server
= NFS_SERVER(dir
);
2670 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2671 data
->msg
.rpc_argp
= &data
->arg
;
2672 data
->msg
.rpc_resp
= &data
->res
;
2673 data
->arg
.dir_fh
= NFS_FH(dir
);
2674 data
->arg
.server
= server
;
2675 data
->arg
.name
= name
;
2676 data
->arg
.attrs
= sattr
;
2677 data
->arg
.ftype
= ftype
;
2678 data
->arg
.bitmask
= server
->attr_bitmask
;
2679 data
->res
.server
= server
;
2680 data
->res
.fh
= &data
->fh
;
2681 data
->res
.fattr
= &data
->fattr
;
2682 data
->res
.dir_fattr
= &data
->dir_fattr
;
2683 nfs_fattr_init(data
->res
.fattr
);
2684 nfs_fattr_init(data
->res
.dir_fattr
);
2689 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2691 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2692 &data
->arg
, &data
->res
, 1);
2694 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2695 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2696 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2701 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2706 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2707 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2709 struct nfs4_createdata
*data
;
2710 int status
= -ENAMETOOLONG
;
2712 if (len
> NFS4_MAXPATHLEN
)
2716 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2720 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2721 data
->arg
.u
.symlink
.pages
= &page
;
2722 data
->arg
.u
.symlink
.len
= len
;
2724 status
= nfs4_do_create(dir
, dentry
, data
);
2726 nfs4_free_createdata(data
);
2731 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2732 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2734 struct nfs4_exception exception
= { };
2737 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2738 _nfs4_proc_symlink(dir
, dentry
, page
,
2741 } while (exception
.retry
);
2745 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2746 struct iattr
*sattr
)
2748 struct nfs4_createdata
*data
;
2749 int status
= -ENOMEM
;
2751 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2755 status
= nfs4_do_create(dir
, dentry
, data
);
2757 nfs4_free_createdata(data
);
2762 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2763 struct iattr
*sattr
)
2765 struct nfs4_exception exception
= { };
2768 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2769 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2771 } while (exception
.retry
);
2775 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2776 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2778 struct inode
*dir
= dentry
->d_inode
;
2779 struct nfs4_readdir_arg args
= {
2784 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
2786 struct nfs4_readdir_res res
;
2787 struct rpc_message msg
= {
2788 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2795 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2796 dentry
->d_parent
->d_name
.name
,
2797 dentry
->d_name
.name
,
2798 (unsigned long long)cookie
);
2799 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2800 res
.pgbase
= args
.pgbase
;
2801 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2803 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2805 nfs_invalidate_atime(dir
);
2807 dprintk("%s: returns %d\n", __func__
, status
);
2811 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2812 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2814 struct nfs4_exception exception
= { };
2817 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2818 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2821 } while (exception
.retry
);
2825 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2826 struct iattr
*sattr
, dev_t rdev
)
2828 struct nfs4_createdata
*data
;
2829 int mode
= sattr
->ia_mode
;
2830 int status
= -ENOMEM
;
2832 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2833 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2835 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2840 data
->arg
.ftype
= NF4FIFO
;
2841 else if (S_ISBLK(mode
)) {
2842 data
->arg
.ftype
= NF4BLK
;
2843 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2844 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2846 else if (S_ISCHR(mode
)) {
2847 data
->arg
.ftype
= NF4CHR
;
2848 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2849 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2852 status
= nfs4_do_create(dir
, dentry
, data
);
2854 nfs4_free_createdata(data
);
2859 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2860 struct iattr
*sattr
, dev_t rdev
)
2862 struct nfs4_exception exception
= { };
2865 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2866 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2868 } while (exception
.retry
);
2872 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2873 struct nfs_fsstat
*fsstat
)
2875 struct nfs4_statfs_arg args
= {
2877 .bitmask
= server
->attr_bitmask
,
2879 struct nfs4_statfs_res res
= {
2882 struct rpc_message msg
= {
2883 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2888 nfs_fattr_init(fsstat
->fattr
);
2889 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2892 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2894 struct nfs4_exception exception
= { };
2897 err
= nfs4_handle_exception(server
,
2898 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2900 } while (exception
.retry
);
2904 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2905 struct nfs_fsinfo
*fsinfo
)
2907 struct nfs4_fsinfo_arg args
= {
2909 .bitmask
= server
->attr_bitmask
,
2911 struct nfs4_fsinfo_res res
= {
2914 struct rpc_message msg
= {
2915 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
2920 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2923 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2925 struct nfs4_exception exception
= { };
2929 err
= nfs4_handle_exception(server
,
2930 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
2932 } while (exception
.retry
);
2936 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
2938 nfs_fattr_init(fsinfo
->fattr
);
2939 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
2942 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2943 struct nfs_pathconf
*pathconf
)
2945 struct nfs4_pathconf_arg args
= {
2947 .bitmask
= server
->attr_bitmask
,
2949 struct nfs4_pathconf_res res
= {
2950 .pathconf
= pathconf
,
2952 struct rpc_message msg
= {
2953 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
2958 /* None of the pathconf attributes are mandatory to implement */
2959 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
2960 memset(pathconf
, 0, sizeof(*pathconf
));
2964 nfs_fattr_init(pathconf
->fattr
);
2965 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2968 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2969 struct nfs_pathconf
*pathconf
)
2971 struct nfs4_exception exception
= { };
2975 err
= nfs4_handle_exception(server
,
2976 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
2978 } while (exception
.retry
);
2982 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
2984 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
2986 dprintk("--> %s\n", __func__
);
2988 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2989 nfs4_sequence_done(server
, &data
->res
.seq_res
, task
->tk_status
);
2991 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
2992 nfs4_restart_rpc(task
, server
->nfs_client
);
2996 nfs_invalidate_atime(data
->inode
);
2997 if (task
->tk_status
> 0)
2998 renew_lease(server
, data
->timestamp
);
3002 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3004 data
->timestamp
= jiffies
;
3005 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3008 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3010 struct inode
*inode
= data
->inode
;
3012 /* slot is freed in nfs_writeback_done */
3013 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3016 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3017 nfs4_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3020 if (task
->tk_status
>= 0) {
3021 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3022 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3027 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3029 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3031 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3032 data
->res
.server
= server
;
3033 data
->timestamp
= jiffies
;
3035 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3038 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3040 struct inode
*inode
= data
->inode
;
3042 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3044 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3045 nfs4_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3048 nfs4_sequence_free_slot(NFS_SERVER(inode
)->nfs_client
,
3049 &data
->res
.seq_res
);
3050 nfs_refresh_inode(inode
, data
->res
.fattr
);
3054 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3056 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3058 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3059 data
->res
.server
= server
;
3060 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3064 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3065 * standalone procedure for queueing an asynchronous RENEW.
3067 static void nfs4_renew_done(struct rpc_task
*task
, void *data
)
3069 struct nfs_client
*clp
= (struct nfs_client
*)task
->tk_msg
.rpc_argp
;
3070 unsigned long timestamp
= (unsigned long)data
;
3072 if (task
->tk_status
< 0) {
3073 /* Unless we're shutting down, schedule state recovery! */
3074 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3075 nfs4_schedule_state_recovery(clp
);
3078 spin_lock(&clp
->cl_lock
);
3079 if (time_before(clp
->cl_last_renewal
,timestamp
))
3080 clp
->cl_last_renewal
= timestamp
;
3081 spin_unlock(&clp
->cl_lock
);
3084 static const struct rpc_call_ops nfs4_renew_ops
= {
3085 .rpc_call_done
= nfs4_renew_done
,
3088 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3090 struct rpc_message msg
= {
3091 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3096 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3097 &nfs4_renew_ops
, (void *)jiffies
);
3100 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3102 struct rpc_message msg
= {
3103 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3107 unsigned long now
= jiffies
;
3110 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3113 spin_lock(&clp
->cl_lock
);
3114 if (time_before(clp
->cl_last_renewal
,now
))
3115 clp
->cl_last_renewal
= now
;
3116 spin_unlock(&clp
->cl_lock
);
3120 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3122 return (server
->caps
& NFS_CAP_ACLS
)
3123 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3124 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3127 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3128 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3131 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3133 static void buf_to_pages(const void *buf
, size_t buflen
,
3134 struct page
**pages
, unsigned int *pgbase
)
3136 const void *p
= buf
;
3138 *pgbase
= offset_in_page(buf
);
3140 while (p
< buf
+ buflen
) {
3141 *(pages
++) = virt_to_page(p
);
3142 p
+= PAGE_CACHE_SIZE
;
3146 static int buf_to_pages_noslab(const void *buf
, size_t buflen
,
3147 struct page
**pages
, unsigned int *pgbase
)
3149 struct page
*newpage
, **spages
;
3155 len
= min_t(size_t, PAGE_CACHE_SIZE
, buflen
);
3156 newpage
= alloc_page(GFP_KERNEL
);
3158 if (newpage
== NULL
)
3160 memcpy(page_address(newpage
), buf
, len
);
3165 } while (buflen
!= 0);
3171 __free_page(spages
[rc
-1]);
3175 struct nfs4_cached_acl
{
3181 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3183 struct nfs_inode
*nfsi
= NFS_I(inode
);
3185 spin_lock(&inode
->i_lock
);
3186 kfree(nfsi
->nfs4_acl
);
3187 nfsi
->nfs4_acl
= acl
;
3188 spin_unlock(&inode
->i_lock
);
3191 static void nfs4_zap_acl_attr(struct inode
*inode
)
3193 nfs4_set_cached_acl(inode
, NULL
);
3196 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3198 struct nfs_inode
*nfsi
= NFS_I(inode
);
3199 struct nfs4_cached_acl
*acl
;
3202 spin_lock(&inode
->i_lock
);
3203 acl
= nfsi
->nfs4_acl
;
3206 if (buf
== NULL
) /* user is just asking for length */
3208 if (acl
->cached
== 0)
3210 ret
= -ERANGE
; /* see getxattr(2) man page */
3211 if (acl
->len
> buflen
)
3213 memcpy(buf
, acl
->data
, acl
->len
);
3217 spin_unlock(&inode
->i_lock
);
3221 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3223 struct nfs4_cached_acl
*acl
;
3225 if (buf
&& acl_len
<= PAGE_SIZE
) {
3226 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3230 memcpy(acl
->data
, buf
, acl_len
);
3232 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3239 nfs4_set_cached_acl(inode
, acl
);
3242 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3244 struct page
*pages
[NFS4ACL_MAXPAGES
];
3245 struct nfs_getaclargs args
= {
3246 .fh
= NFS_FH(inode
),
3250 struct nfs_getaclres res
= {
3254 struct rpc_message msg
= {
3255 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3259 struct page
*localpage
= NULL
;
3262 if (buflen
< PAGE_SIZE
) {
3263 /* As long as we're doing a round trip to the server anyway,
3264 * let's be prepared for a page of acl data. */
3265 localpage
= alloc_page(GFP_KERNEL
);
3266 resp_buf
= page_address(localpage
);
3267 if (localpage
== NULL
)
3269 args
.acl_pages
[0] = localpage
;
3270 args
.acl_pgbase
= 0;
3271 args
.acl_len
= PAGE_SIZE
;
3274 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3276 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3279 if (res
.acl_len
> args
.acl_len
)
3280 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3282 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3285 if (res
.acl_len
> buflen
)
3288 memcpy(buf
, resp_buf
, res
.acl_len
);
3293 __free_page(localpage
);
3297 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3299 struct nfs4_exception exception
= { };
3302 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3305 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3306 } while (exception
.retry
);
3310 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3312 struct nfs_server
*server
= NFS_SERVER(inode
);
3315 if (!nfs4_server_supports_acls(server
))
3317 ret
= nfs_revalidate_inode(server
, inode
);
3320 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3323 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3326 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3328 struct nfs_server
*server
= NFS_SERVER(inode
);
3329 struct page
*pages
[NFS4ACL_MAXPAGES
];
3330 struct nfs_setaclargs arg
= {
3331 .fh
= NFS_FH(inode
),
3335 struct nfs_setaclres res
;
3336 struct rpc_message msg
= {
3337 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3343 if (!nfs4_server_supports_acls(server
))
3345 i
= buf_to_pages_noslab(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3348 nfs_inode_return_delegation(inode
);
3349 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3352 * Free each page after tx, so the only ref left is
3353 * held by the network stack
3356 put_page(pages
[i
-1]);
3358 nfs_access_zap_cache(inode
);
3359 nfs_zap_acl_cache(inode
);
3363 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3365 struct nfs4_exception exception
= { };
3368 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3369 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3371 } while (exception
.retry
);
3376 _nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs_client
*clp
, struct nfs4_state
*state
)
3378 if (!clp
|| task
->tk_status
>= 0)
3380 switch(task
->tk_status
) {
3381 case -NFS4ERR_ADMIN_REVOKED
:
3382 case -NFS4ERR_BAD_STATEID
:
3383 case -NFS4ERR_OPENMODE
:
3386 nfs4_state_mark_reclaim_nograce(clp
, state
);
3387 case -NFS4ERR_STALE_CLIENTID
:
3388 case -NFS4ERR_STALE_STATEID
:
3389 case -NFS4ERR_EXPIRED
:
3390 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3391 nfs4_schedule_state_recovery(clp
);
3392 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3393 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3394 task
->tk_status
= 0;
3396 #if defined(CONFIG_NFS_V4_1)
3397 case -NFS4ERR_BADSESSION
:
3398 case -NFS4ERR_BADSLOT
:
3399 case -NFS4ERR_BAD_HIGH_SLOT
:
3400 case -NFS4ERR_DEADSESSION
:
3401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3402 case -NFS4ERR_SEQ_FALSE_RETRY
:
3403 case -NFS4ERR_SEQ_MISORDERED
:
3404 dprintk("%s ERROR %d, Reset session\n", __func__
,
3406 set_bit(NFS4CLNT_SESSION_SETUP
, &clp
->cl_state
);
3407 task
->tk_status
= 0;
3409 #endif /* CONFIG_NFS_V4_1 */
3410 case -NFS4ERR_DELAY
:
3412 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3413 case -NFS4ERR_GRACE
:
3414 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3415 task
->tk_status
= 0;
3417 case -NFS4ERR_OLD_STATEID
:
3418 task
->tk_status
= 0;
3421 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3426 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3428 return _nfs4_async_handle_error(task
, server
, server
->nfs_client
, state
);
3431 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
, unsigned short port
, struct rpc_cred
*cred
)
3433 nfs4_verifier sc_verifier
;
3434 struct nfs4_setclientid setclientid
= {
3435 .sc_verifier
= &sc_verifier
,
3438 struct rpc_message msg
= {
3439 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3440 .rpc_argp
= &setclientid
,
3448 p
= (__be32
*)sc_verifier
.data
;
3449 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3450 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3453 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3454 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3456 rpc_peeraddr2str(clp
->cl_rpcclient
,
3458 rpc_peeraddr2str(clp
->cl_rpcclient
,
3460 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3461 clp
->cl_id_uniquifier
);
3462 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3463 sizeof(setclientid
.sc_netid
),
3464 rpc_peeraddr2str(clp
->cl_rpcclient
,
3465 RPC_DISPLAY_NETID
));
3466 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3467 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3468 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3470 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3471 if (status
!= -NFS4ERR_CLID_INUSE
)
3476 ssleep(clp
->cl_lease_time
+ 1);
3478 if (++clp
->cl_id_uniquifier
== 0)
3484 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3486 struct nfs_fsinfo fsinfo
;
3487 struct rpc_message msg
= {
3488 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3490 .rpc_resp
= &fsinfo
,
3497 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3499 spin_lock(&clp
->cl_lock
);
3500 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3501 clp
->cl_last_renewal
= now
;
3502 spin_unlock(&clp
->cl_lock
);
3507 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3512 err
= _nfs4_proc_setclientid_confirm(clp
, cred
);
3516 case -NFS4ERR_RESOURCE
:
3517 /* The IBM lawyers misread another document! */
3518 case -NFS4ERR_DELAY
:
3519 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3525 struct nfs4_delegreturndata
{
3526 struct nfs4_delegreturnargs args
;
3527 struct nfs4_delegreturnres res
;
3529 nfs4_stateid stateid
;
3530 unsigned long timestamp
;
3531 struct nfs_fattr fattr
;
3535 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3537 struct nfs4_delegreturndata
*data
= calldata
;
3539 nfs4_sequence_done_free_slot(data
->res
.server
, &data
->res
.seq_res
,
3542 data
->rpc_status
= task
->tk_status
;
3543 if (data
->rpc_status
== 0)
3544 renew_lease(data
->res
.server
, data
->timestamp
);
3547 static void nfs4_delegreturn_release(void *calldata
)
3552 #if defined(CONFIG_NFS_V4_1)
3553 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3555 struct nfs4_delegreturndata
*d_data
;
3557 d_data
= (struct nfs4_delegreturndata
*)data
;
3559 if (nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
3560 &d_data
->args
.seq_args
,
3561 &d_data
->res
.seq_res
, 1, task
))
3563 rpc_call_start(task
);
3565 #endif /* CONFIG_NFS_V4_1 */
3567 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3568 #if defined(CONFIG_NFS_V4_1)
3569 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3570 #endif /* CONFIG_NFS_V4_1 */
3571 .rpc_call_done
= nfs4_delegreturn_done
,
3572 .rpc_release
= nfs4_delegreturn_release
,
3575 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3577 struct nfs4_delegreturndata
*data
;
3578 struct nfs_server
*server
= NFS_SERVER(inode
);
3579 struct rpc_task
*task
;
3580 struct rpc_message msg
= {
3581 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3584 struct rpc_task_setup task_setup_data
= {
3585 .rpc_client
= server
->client
,
3586 .rpc_message
= &msg
,
3587 .callback_ops
= &nfs4_delegreturn_ops
,
3588 .flags
= RPC_TASK_ASYNC
,
3592 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3595 data
->args
.fhandle
= &data
->fh
;
3596 data
->args
.stateid
= &data
->stateid
;
3597 data
->args
.bitmask
= server
->attr_bitmask
;
3598 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3599 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3600 data
->res
.fattr
= &data
->fattr
;
3601 data
->res
.server
= server
;
3602 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3603 nfs_fattr_init(data
->res
.fattr
);
3604 data
->timestamp
= jiffies
;
3605 data
->rpc_status
= 0;
3607 task_setup_data
.callback_data
= data
;
3608 msg
.rpc_argp
= &data
->args
,
3609 msg
.rpc_resp
= &data
->res
,
3610 task
= rpc_run_task(&task_setup_data
);
3612 return PTR_ERR(task
);
3615 status
= nfs4_wait_for_completion_rpc_task(task
);
3618 status
= data
->rpc_status
;
3621 nfs_refresh_inode(inode
, &data
->fattr
);
3627 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3629 struct nfs_server
*server
= NFS_SERVER(inode
);
3630 struct nfs4_exception exception
= { };
3633 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3635 case -NFS4ERR_STALE_STATEID
:
3636 case -NFS4ERR_EXPIRED
:
3640 err
= nfs4_handle_exception(server
, err
, &exception
);
3641 } while (exception
.retry
);
3645 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3646 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3649 * sleep, with exponential backoff, and retry the LOCK operation.
3651 static unsigned long
3652 nfs4_set_lock_task_retry(unsigned long timeout
)
3654 schedule_timeout_killable(timeout
);
3656 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3657 return NFS4_LOCK_MAXTIMEOUT
;
3661 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3663 struct inode
*inode
= state
->inode
;
3664 struct nfs_server
*server
= NFS_SERVER(inode
);
3665 struct nfs_client
*clp
= server
->nfs_client
;
3666 struct nfs_lockt_args arg
= {
3667 .fh
= NFS_FH(inode
),
3670 struct nfs_lockt_res res
= {
3673 struct rpc_message msg
= {
3674 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3677 .rpc_cred
= state
->owner
->so_cred
,
3679 struct nfs4_lock_state
*lsp
;
3682 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3683 status
= nfs4_set_lock_state(state
, request
);
3686 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3687 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3688 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3691 request
->fl_type
= F_UNLCK
;
3693 case -NFS4ERR_DENIED
:
3696 request
->fl_ops
->fl_release_private(request
);
3701 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3703 struct nfs4_exception exception
= { };
3707 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3708 _nfs4_proc_getlk(state
, cmd
, request
),
3710 } while (exception
.retry
);
3714 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3717 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3719 res
= posix_lock_file_wait(file
, fl
);
3722 res
= flock_lock_file_wait(file
, fl
);
3730 struct nfs4_unlockdata
{
3731 struct nfs_locku_args arg
;
3732 struct nfs_locku_res res
;
3733 struct nfs4_lock_state
*lsp
;
3734 struct nfs_open_context
*ctx
;
3735 struct file_lock fl
;
3736 const struct nfs_server
*server
;
3737 unsigned long timestamp
;
3740 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3741 struct nfs_open_context
*ctx
,
3742 struct nfs4_lock_state
*lsp
,
3743 struct nfs_seqid
*seqid
)
3745 struct nfs4_unlockdata
*p
;
3746 struct inode
*inode
= lsp
->ls_state
->inode
;
3748 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3751 p
->arg
.fh
= NFS_FH(inode
);
3753 p
->arg
.seqid
= seqid
;
3754 p
->res
.seqid
= seqid
;
3755 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3756 p
->arg
.stateid
= &lsp
->ls_stateid
;
3758 atomic_inc(&lsp
->ls_count
);
3759 /* Ensure we don't close file until we're done freeing locks! */
3760 p
->ctx
= get_nfs_open_context(ctx
);
3761 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3762 p
->server
= NFS_SERVER(inode
);
3766 static void nfs4_locku_release_calldata(void *data
)
3768 struct nfs4_unlockdata
*calldata
= data
;
3769 nfs_free_seqid(calldata
->arg
.seqid
);
3770 nfs4_put_lock_state(calldata
->lsp
);
3771 put_nfs_open_context(calldata
->ctx
);
3775 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3777 struct nfs4_unlockdata
*calldata
= data
;
3779 nfs4_sequence_done(calldata
->server
, &calldata
->res
.seq_res
,
3781 if (RPC_ASSASSINATED(task
))
3783 switch (task
->tk_status
) {
3785 memcpy(calldata
->lsp
->ls_stateid
.data
,
3786 calldata
->res
.stateid
.data
,
3787 sizeof(calldata
->lsp
->ls_stateid
.data
));
3788 renew_lease(calldata
->server
, calldata
->timestamp
);
3790 case -NFS4ERR_BAD_STATEID
:
3791 case -NFS4ERR_OLD_STATEID
:
3792 case -NFS4ERR_STALE_STATEID
:
3793 case -NFS4ERR_EXPIRED
:
3796 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3797 nfs4_restart_rpc(task
,
3798 calldata
->server
->nfs_client
);
3800 nfs4_sequence_free_slot(calldata
->server
->nfs_client
,
3801 &calldata
->res
.seq_res
);
3804 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3806 struct nfs4_unlockdata
*calldata
= data
;
3808 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3810 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3811 /* Note: exit _without_ running nfs4_locku_done */
3812 task
->tk_action
= NULL
;
3815 calldata
->timestamp
= jiffies
;
3816 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
3817 &calldata
->arg
.seq_args
,
3818 &calldata
->res
.seq_res
, 1, task
))
3820 rpc_call_start(task
);
3823 static const struct rpc_call_ops nfs4_locku_ops
= {
3824 .rpc_call_prepare
= nfs4_locku_prepare
,
3825 .rpc_call_done
= nfs4_locku_done
,
3826 .rpc_release
= nfs4_locku_release_calldata
,
3829 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3830 struct nfs_open_context
*ctx
,
3831 struct nfs4_lock_state
*lsp
,
3832 struct nfs_seqid
*seqid
)
3834 struct nfs4_unlockdata
*data
;
3835 struct rpc_message msg
= {
3836 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3837 .rpc_cred
= ctx
->cred
,
3839 struct rpc_task_setup task_setup_data
= {
3840 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3841 .rpc_message
= &msg
,
3842 .callback_ops
= &nfs4_locku_ops
,
3843 .workqueue
= nfsiod_workqueue
,
3844 .flags
= RPC_TASK_ASYNC
,
3847 /* Ensure this is an unlock - when canceling a lock, the
3848 * canceled lock is passed in, and it won't be an unlock.
3850 fl
->fl_type
= F_UNLCK
;
3852 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3854 nfs_free_seqid(seqid
);
3855 return ERR_PTR(-ENOMEM
);
3858 msg
.rpc_argp
= &data
->arg
,
3859 msg
.rpc_resp
= &data
->res
,
3860 task_setup_data
.callback_data
= data
;
3861 return rpc_run_task(&task_setup_data
);
3864 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3866 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3867 struct nfs_seqid
*seqid
;
3868 struct nfs4_lock_state
*lsp
;
3869 struct rpc_task
*task
;
3871 unsigned char fl_flags
= request
->fl_flags
;
3873 status
= nfs4_set_lock_state(state
, request
);
3874 /* Unlock _before_ we do the RPC call */
3875 request
->fl_flags
|= FL_EXISTS
;
3876 down_read(&nfsi
->rwsem
);
3877 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3878 up_read(&nfsi
->rwsem
);
3881 up_read(&nfsi
->rwsem
);
3884 /* Is this a delegated lock? */
3885 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3887 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3888 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3892 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3893 status
= PTR_ERR(task
);
3896 status
= nfs4_wait_for_completion_rpc_task(task
);
3899 request
->fl_flags
= fl_flags
;
3903 struct nfs4_lockdata
{
3904 struct nfs_lock_args arg
;
3905 struct nfs_lock_res res
;
3906 struct nfs4_lock_state
*lsp
;
3907 struct nfs_open_context
*ctx
;
3908 struct file_lock fl
;
3909 unsigned long timestamp
;
3912 struct nfs_server
*server
;
3915 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3916 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
)
3918 struct nfs4_lockdata
*p
;
3919 struct inode
*inode
= lsp
->ls_state
->inode
;
3920 struct nfs_server
*server
= NFS_SERVER(inode
);
3922 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3926 p
->arg
.fh
= NFS_FH(inode
);
3928 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
);
3929 if (p
->arg
.open_seqid
== NULL
)
3931 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3932 if (p
->arg
.lock_seqid
== NULL
)
3933 goto out_free_seqid
;
3934 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
3935 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
3936 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3937 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
3938 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3941 atomic_inc(&lsp
->ls_count
);
3942 p
->ctx
= get_nfs_open_context(ctx
);
3943 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3946 nfs_free_seqid(p
->arg
.open_seqid
);
3952 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
3954 struct nfs4_lockdata
*data
= calldata
;
3955 struct nfs4_state
*state
= data
->lsp
->ls_state
;
3957 dprintk("%s: begin!\n", __func__
);
3958 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
3960 /* Do we need to do an open_to_lock_owner? */
3961 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
3962 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
3964 data
->arg
.open_stateid
= &state
->stateid
;
3965 data
->arg
.new_lock_owner
= 1;
3966 data
->res
.open_seqid
= data
->arg
.open_seqid
;
3968 data
->arg
.new_lock_owner
= 0;
3969 data
->timestamp
= jiffies
;
3970 if (nfs4_setup_sequence(data
->server
->nfs_client
, &data
->arg
.seq_args
,
3971 &data
->res
.seq_res
, 1, task
))
3973 rpc_call_start(task
);
3974 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
3977 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
3979 struct nfs4_lockdata
*data
= calldata
;
3981 dprintk("%s: begin!\n", __func__
);
3983 nfs4_sequence_done_free_slot(data
->server
, &data
->res
.seq_res
,
3986 data
->rpc_status
= task
->tk_status
;
3987 if (RPC_ASSASSINATED(task
))
3989 if (data
->arg
.new_lock_owner
!= 0) {
3990 if (data
->rpc_status
== 0)
3991 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
3995 if (data
->rpc_status
== 0) {
3996 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
3997 sizeof(data
->lsp
->ls_stateid
.data
));
3998 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
3999 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4002 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4005 static void nfs4_lock_release(void *calldata
)
4007 struct nfs4_lockdata
*data
= calldata
;
4009 dprintk("%s: begin!\n", __func__
);
4010 nfs_free_seqid(data
->arg
.open_seqid
);
4011 if (data
->cancelled
!= 0) {
4012 struct rpc_task
*task
;
4013 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4014 data
->arg
.lock_seqid
);
4017 dprintk("%s: cancelling lock!\n", __func__
);
4019 nfs_free_seqid(data
->arg
.lock_seqid
);
4020 nfs4_put_lock_state(data
->lsp
);
4021 put_nfs_open_context(data
->ctx
);
4023 dprintk("%s: done!\n", __func__
);
4026 static const struct rpc_call_ops nfs4_lock_ops
= {
4027 .rpc_call_prepare
= nfs4_lock_prepare
,
4028 .rpc_call_done
= nfs4_lock_done
,
4029 .rpc_release
= nfs4_lock_release
,
4032 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4034 struct nfs_client
*clp
= server
->nfs_client
;
4035 struct nfs4_state
*state
= lsp
->ls_state
;
4038 case -NFS4ERR_ADMIN_REVOKED
:
4039 case -NFS4ERR_BAD_STATEID
:
4040 case -NFS4ERR_EXPIRED
:
4041 if (new_lock_owner
!= 0 ||
4042 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4043 nfs4_state_mark_reclaim_nograce(clp
, state
);
4044 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4048 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int reclaim
)
4050 struct nfs4_lockdata
*data
;
4051 struct rpc_task
*task
;
4052 struct rpc_message msg
= {
4053 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4054 .rpc_cred
= state
->owner
->so_cred
,
4056 struct rpc_task_setup task_setup_data
= {
4057 .rpc_client
= NFS_CLIENT(state
->inode
),
4058 .rpc_message
= &msg
,
4059 .callback_ops
= &nfs4_lock_ops
,
4060 .workqueue
= nfsiod_workqueue
,
4061 .flags
= RPC_TASK_ASYNC
,
4065 dprintk("%s: begin!\n", __func__
);
4066 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4067 fl
->fl_u
.nfs4_fl
.owner
);
4071 data
->arg
.block
= 1;
4073 data
->arg
.reclaim
= 1;
4074 msg
.rpc_argp
= &data
->arg
,
4075 msg
.rpc_resp
= &data
->res
,
4076 task_setup_data
.callback_data
= data
;
4077 task
= rpc_run_task(&task_setup_data
);
4079 return PTR_ERR(task
);
4080 ret
= nfs4_wait_for_completion_rpc_task(task
);
4082 ret
= data
->rpc_status
;
4084 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4085 data
->arg
.new_lock_owner
, ret
);
4087 data
->cancelled
= 1;
4089 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4093 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4095 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4096 struct nfs4_exception exception
= { };
4100 /* Cache the lock if possible... */
4101 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4103 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 1);
4104 if (err
!= -NFS4ERR_DELAY
)
4106 nfs4_handle_exception(server
, err
, &exception
);
4107 } while (exception
.retry
);
4111 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4113 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4114 struct nfs4_exception exception
= { };
4117 err
= nfs4_set_lock_state(state
, request
);
4121 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4123 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, 0);
4127 case -NFS4ERR_GRACE
:
4128 case -NFS4ERR_DELAY
:
4129 nfs4_handle_exception(server
, err
, &exception
);
4132 } while (exception
.retry
);
4137 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4139 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4140 unsigned char fl_flags
= request
->fl_flags
;
4141 int status
= -ENOLCK
;
4143 if ((fl_flags
& FL_POSIX
) &&
4144 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4146 /* Is this a delegated open? */
4147 status
= nfs4_set_lock_state(state
, request
);
4150 request
->fl_flags
|= FL_ACCESS
;
4151 status
= do_vfs_lock(request
->fl_file
, request
);
4154 down_read(&nfsi
->rwsem
);
4155 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4156 /* Yes: cache locks! */
4157 /* ...but avoid races with delegation recall... */
4158 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4159 status
= do_vfs_lock(request
->fl_file
, request
);
4162 status
= _nfs4_do_setlk(state
, cmd
, request
, 0);
4165 /* Note: we always want to sleep here! */
4166 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4167 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4168 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4170 up_read(&nfsi
->rwsem
);
4172 request
->fl_flags
= fl_flags
;
4176 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4178 struct nfs4_exception exception
= { };
4182 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4183 if (err
== -NFS4ERR_DENIED
)
4185 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4187 } while (exception
.retry
);
4192 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4194 struct nfs_open_context
*ctx
;
4195 struct nfs4_state
*state
;
4196 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4199 /* verify open state */
4200 ctx
= nfs_file_open_context(filp
);
4203 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4206 if (IS_GETLK(cmd
)) {
4208 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4212 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4215 if (request
->fl_type
== F_UNLCK
) {
4217 return nfs4_proc_unlck(state
, cmd
, request
);
4224 status
= nfs4_proc_setlk(state
, cmd
, request
);
4225 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4227 timeout
= nfs4_set_lock_task_retry(timeout
);
4228 status
= -ERESTARTSYS
;
4231 } while(status
< 0);
4235 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4237 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4238 struct nfs4_exception exception
= { };
4241 err
= nfs4_set_lock_state(state
, fl
);
4245 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, 0);
4248 printk(KERN_ERR
"%s: unhandled error %d.\n",
4253 case -NFS4ERR_EXPIRED
:
4254 case -NFS4ERR_STALE_CLIENTID
:
4255 case -NFS4ERR_STALE_STATEID
:
4256 nfs4_schedule_state_recovery(server
->nfs_client
);
4260 * The show must go on: exit, but mark the
4261 * stateid as needing recovery.
4263 case -NFS4ERR_ADMIN_REVOKED
:
4264 case -NFS4ERR_BAD_STATEID
:
4265 case -NFS4ERR_OPENMODE
:
4266 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
4270 case -NFS4ERR_DENIED
:
4271 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4274 case -NFS4ERR_DELAY
:
4277 err
= nfs4_handle_exception(server
, err
, &exception
);
4278 } while (exception
.retry
);
4283 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4285 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4286 size_t buflen
, int flags
)
4288 struct inode
*inode
= dentry
->d_inode
;
4290 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4293 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4296 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4297 * and that's what we'll do for e.g. user attributes that haven't been set.
4298 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4299 * attributes in kernel-managed attribute namespaces. */
4300 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4303 struct inode
*inode
= dentry
->d_inode
;
4305 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4308 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4311 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4313 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4315 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4317 if (buf
&& buflen
< len
)
4320 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4324 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4326 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4327 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4328 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4331 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4332 NFS_ATTR_FATTR_NLINK
;
4333 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4337 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4338 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4340 struct nfs_server
*server
= NFS_SERVER(dir
);
4342 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4343 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4345 struct nfs4_fs_locations_arg args
= {
4346 .dir_fh
= NFS_FH(dir
),
4351 struct nfs4_fs_locations_res res
= {
4352 .fs_locations
= fs_locations
,
4354 struct rpc_message msg
= {
4355 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4361 dprintk("%s: start\n", __func__
);
4362 nfs_fattr_init(&fs_locations
->fattr
);
4363 fs_locations
->server
= server
;
4364 fs_locations
->nlocations
= 0;
4365 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4366 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4367 dprintk("%s: returned status = %d\n", __func__
, status
);
4371 #ifdef CONFIG_NFS_V4_1
4373 * nfs4_proc_exchange_id()
4375 * Since the clientid has expired, all compounds using sessions
4376 * associated with the stale clientid will be returning
4377 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4378 * be in some phase of session reset.
4380 static int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4382 nfs4_verifier verifier
;
4383 struct nfs41_exchange_id_args args
= {
4385 .flags
= clp
->cl_exchange_flags
,
4387 struct nfs41_exchange_id_res res
= {
4391 struct rpc_message msg
= {
4392 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4399 dprintk("--> %s\n", __func__
);
4400 BUG_ON(clp
== NULL
);
4402 p
= (u32
*)verifier
.data
;
4403 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4404 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4405 args
.verifier
= &verifier
;
4408 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4411 rpc_peeraddr2str(clp
->cl_rpcclient
,
4413 clp
->cl_id_uniquifier
);
4415 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4417 if (status
!= NFS4ERR_CLID_INUSE
)
4423 if (++clp
->cl_id_uniquifier
== 0)
4427 dprintk("<-- %s status= %d\n", __func__
, status
);
4431 struct nfs4_get_lease_time_data
{
4432 struct nfs4_get_lease_time_args
*args
;
4433 struct nfs4_get_lease_time_res
*res
;
4434 struct nfs_client
*clp
;
4437 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4441 struct nfs4_get_lease_time_data
*data
=
4442 (struct nfs4_get_lease_time_data
*)calldata
;
4444 dprintk("--> %s\n", __func__
);
4445 /* just setup sequence, do not trigger session recovery
4446 since we're invoked within one */
4447 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4448 &data
->args
->la_seq_args
,
4449 &data
->res
->lr_seq_res
, 0, task
);
4451 BUG_ON(ret
== -EAGAIN
);
4452 rpc_call_start(task
);
4453 dprintk("<-- %s\n", __func__
);
4457 * Called from nfs4_state_manager thread for session setup, so don't recover
4458 * from sequence operation or clientid errors.
4460 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4462 struct nfs4_get_lease_time_data
*data
=
4463 (struct nfs4_get_lease_time_data
*)calldata
;
4465 dprintk("--> %s\n", __func__
);
4466 nfs41_sequence_done(data
->clp
, &data
->res
->lr_seq_res
, task
->tk_status
);
4467 switch (task
->tk_status
) {
4468 case -NFS4ERR_DELAY
:
4469 case -NFS4ERR_GRACE
:
4470 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4471 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4472 task
->tk_status
= 0;
4473 nfs4_restart_rpc(task
, data
->clp
);
4476 nfs41_sequence_free_slot(data
->clp
, &data
->res
->lr_seq_res
);
4477 dprintk("<-- %s\n", __func__
);
4480 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4481 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4482 .rpc_call_done
= nfs4_get_lease_time_done
,
4485 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4487 struct rpc_task
*task
;
4488 struct nfs4_get_lease_time_args args
;
4489 struct nfs4_get_lease_time_res res
= {
4490 .lr_fsinfo
= fsinfo
,
4492 struct nfs4_get_lease_time_data data
= {
4497 struct rpc_message msg
= {
4498 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4502 struct rpc_task_setup task_setup
= {
4503 .rpc_client
= clp
->cl_rpcclient
,
4504 .rpc_message
= &msg
,
4505 .callback_ops
= &nfs4_get_lease_time_ops
,
4506 .callback_data
= &data
4510 res
.lr_seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4511 dprintk("--> %s\n", __func__
);
4512 task
= rpc_run_task(&task_setup
);
4515 status
= PTR_ERR(task
);
4517 status
= task
->tk_status
;
4520 dprintk("<-- %s return %d\n", __func__
, status
);
4526 * Reset a slot table
4528 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, int max_slots
,
4529 int old_max_slots
, int ivalue
)
4534 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__
, max_slots
, tbl
);
4537 * Until we have dynamic slot table adjustment, insist
4538 * upon the same slot table size
4540 if (max_slots
!= old_max_slots
) {
4541 dprintk("%s reset slot table does't match old\n",
4543 ret
= -EINVAL
; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4546 spin_lock(&tbl
->slot_tbl_lock
);
4547 for (i
= 0; i
< max_slots
; ++i
)
4548 tbl
->slots
[i
].seq_nr
= ivalue
;
4549 tbl
->highest_used_slotid
= -1;
4550 spin_unlock(&tbl
->slot_tbl_lock
);
4551 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4552 tbl
, tbl
->slots
, tbl
->max_slots
);
4554 dprintk("<-- %s: return %d\n", __func__
, ret
);
4559 * Reset the forechannel and backchannel slot tables
4561 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
4565 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
4566 session
->fc_attrs
.max_reqs
,
4567 session
->fc_slot_table
.max_slots
,
4572 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
4573 session
->bc_attrs
.max_reqs
,
4574 session
->bc_slot_table
.max_slots
,
4579 /* Destroy the slot table */
4580 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
4582 if (session
->fc_slot_table
.slots
!= NULL
) {
4583 kfree(session
->fc_slot_table
.slots
);
4584 session
->fc_slot_table
.slots
= NULL
;
4586 if (session
->bc_slot_table
.slots
!= NULL
) {
4587 kfree(session
->bc_slot_table
.slots
);
4588 session
->bc_slot_table
.slots
= NULL
;
4594 * Initialize slot table
4596 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
4597 int max_slots
, int ivalue
)
4600 struct nfs4_slot
*slot
;
4603 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4605 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
4607 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_KERNEL
);
4610 for (i
= 0; i
< max_slots
; ++i
)
4611 slot
[i
].seq_nr
= ivalue
;
4614 spin_lock(&tbl
->slot_tbl_lock
);
4615 if (tbl
->slots
!= NULL
) {
4616 spin_unlock(&tbl
->slot_tbl_lock
);
4617 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4618 __func__
, tbl
, tbl
->slots
);
4622 tbl
->max_slots
= max_slots
;
4624 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4625 spin_unlock(&tbl
->slot_tbl_lock
);
4626 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4627 tbl
, tbl
->slots
, tbl
->max_slots
);
4629 dprintk("<-- %s: return %d\n", __func__
, ret
);
4638 * Initialize the forechannel and backchannel tables
4640 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
4644 status
= nfs4_init_slot_table(&session
->fc_slot_table
,
4645 session
->fc_attrs
.max_reqs
, 1);
4649 status
= nfs4_init_slot_table(&session
->bc_slot_table
,
4650 session
->bc_attrs
.max_reqs
, 0);
4652 nfs4_destroy_slot_tables(session
);
4657 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4659 struct nfs4_session
*session
;
4660 struct nfs4_slot_table
*tbl
;
4662 session
= kzalloc(sizeof(struct nfs4_session
), GFP_KERNEL
);
4666 set_bit(NFS4CLNT_SESSION_SETUP
, &clp
->cl_state
);
4668 * The create session reply races with the server back
4669 * channel probe. Mark the client NFS_CS_SESSION_INITING
4670 * so that the client back channel can find the
4673 clp
->cl_cons_state
= NFS_CS_SESSION_INITING
;
4675 tbl
= &session
->fc_slot_table
;
4676 spin_lock_init(&tbl
->slot_tbl_lock
);
4677 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
4679 tbl
= &session
->bc_slot_table
;
4680 spin_lock_init(&tbl
->slot_tbl_lock
);
4681 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
4687 void nfs4_destroy_session(struct nfs4_session
*session
)
4689 nfs4_proc_destroy_session(session
);
4690 dprintk("%s Destroy backchannel for xprt %p\n",
4691 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
4692 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
4693 NFS41_BC_MIN_CALLBACKS
);
4694 nfs4_destroy_slot_tables(session
);
4699 * Initialize the values to be used by the client in CREATE_SESSION
4700 * If nfs4_init_session set the fore channel request and response sizes,
4703 * Set the back channel max_resp_sz_cached to zero to force the client to
4704 * always set csa_cachethis to FALSE because the current implementation
4705 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4707 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4709 struct nfs4_session
*session
= args
->client
->cl_session
;
4710 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4711 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4714 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4716 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4717 /* Fore channel attributes */
4718 args
->fc_attrs
.headerpadsz
= 0;
4719 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4720 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4721 args
->fc_attrs
.max_resp_sz_cached
= mxresp_sz
;
4722 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4723 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4725 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4726 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4728 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4729 args
->fc_attrs
.max_resp_sz_cached
, args
->fc_attrs
.max_ops
,
4730 args
->fc_attrs
.max_reqs
);
4732 /* Back channel attributes */
4733 args
->bc_attrs
.headerpadsz
= 0;
4734 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4735 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4736 args
->bc_attrs
.max_resp_sz_cached
= 0;
4737 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4738 args
->bc_attrs
.max_reqs
= 1;
4740 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4741 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4743 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4744 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4745 args
->bc_attrs
.max_reqs
);
4748 static int _verify_channel_attr(char *chan
, char *attr_name
, u32 sent
, u32 rcvd
)
4752 printk(KERN_WARNING
"%s: Session INVALID: %s channel %s increased. "
4753 "sent=%u rcvd=%u\n", __func__
, chan
, attr_name
, sent
, rcvd
);
4757 #define _verify_fore_channel_attr(_name_) \
4758 _verify_channel_attr("fore", #_name_, \
4759 args->fc_attrs._name_, \
4760 session->fc_attrs._name_)
4762 #define _verify_back_channel_attr(_name_) \
4763 _verify_channel_attr("back", #_name_, \
4764 args->bc_attrs._name_, \
4765 session->bc_attrs._name_)
4768 * The server is not allowed to increase the fore channel header pad size,
4769 * maximum response size, or maximum number of operations.
4771 * The back channel attributes are only negotiatied down: We send what the
4772 * (back channel) server insists upon.
4774 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4775 struct nfs4_session
*session
)
4779 ret
|= _verify_fore_channel_attr(headerpadsz
);
4780 ret
|= _verify_fore_channel_attr(max_resp_sz
);
4781 ret
|= _verify_fore_channel_attr(max_ops
);
4783 ret
|= _verify_back_channel_attr(headerpadsz
);
4784 ret
|= _verify_back_channel_attr(max_rqst_sz
);
4785 ret
|= _verify_back_channel_attr(max_resp_sz
);
4786 ret
|= _verify_back_channel_attr(max_resp_sz_cached
);
4787 ret
|= _verify_back_channel_attr(max_ops
);
4788 ret
|= _verify_back_channel_attr(max_reqs
);
4793 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4795 struct nfs4_session
*session
= clp
->cl_session
;
4796 struct nfs41_create_session_args args
= {
4798 .cb_program
= NFS4_CALLBACK
,
4800 struct nfs41_create_session_res res
= {
4803 struct rpc_message msg
= {
4804 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4810 nfs4_init_channel_attrs(&args
);
4811 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
4813 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4816 /* Verify the session's negotiated channel_attrs values */
4817 status
= nfs4_verify_channel_attrs(&args
, session
);
4819 /* Increment the clientid slot sequence id */
4827 * Issues a CREATE_SESSION operation to the server.
4828 * It is the responsibility of the caller to verify the session is
4829 * expired before calling this routine.
4831 int nfs4_proc_create_session(struct nfs_client
*clp
, int reset
)
4835 struct nfs_fsinfo fsinfo
;
4836 struct nfs4_session
*session
= clp
->cl_session
;
4838 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4840 status
= _nfs4_proc_create_session(clp
);
4844 /* Init or reset the fore channel */
4846 status
= nfs4_reset_slot_tables(session
);
4848 status
= nfs4_init_slot_tables(session
);
4849 dprintk("fore channel slot table initialization returned %d\n", status
);
4853 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4854 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4855 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4858 /* Lease time is aleady set */
4861 /* Get the lease time */
4862 status
= nfs4_proc_get_lease_time(clp
, &fsinfo
);
4864 /* Update lease time and schedule renewal */
4865 spin_lock(&clp
->cl_lock
);
4866 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
4867 clp
->cl_last_renewal
= jiffies
;
4868 clear_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
4869 spin_unlock(&clp
->cl_lock
);
4871 nfs4_schedule_state_renewal(clp
);
4874 dprintk("<-- %s\n", __func__
);
4879 * Issue the over-the-wire RPC DESTROY_SESSION.
4880 * The caller must serialize access to this routine.
4882 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4885 struct rpc_message msg
;
4887 dprintk("--> nfs4_proc_destroy_session\n");
4889 /* session is still being setup */
4890 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
4893 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
4894 msg
.rpc_argp
= session
;
4895 msg
.rpc_resp
= NULL
;
4896 msg
.rpc_cred
= NULL
;
4897 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4901 "Got error %d from the server on DESTROY_SESSION. "
4902 "Session has been destroyed regardless...\n", status
);
4904 dprintk("<-- nfs4_proc_destroy_session\n");
4908 int nfs4_init_session(struct nfs_server
*server
)
4910 struct nfs_client
*clp
= server
->nfs_client
;
4913 if (!nfs4_has_session(clp
))
4916 clp
->cl_session
->fc_attrs
.max_rqst_sz
= server
->wsize
;
4917 clp
->cl_session
->fc_attrs
.max_resp_sz
= server
->rsize
;
4918 ret
= nfs4_recover_expired_lease(server
);
4920 ret
= nfs4_check_client_ready(clp
);
4925 * Renew the cl_session lease.
4927 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4929 struct nfs4_sequence_args args
;
4930 struct nfs4_sequence_res res
;
4932 struct rpc_message msg
= {
4933 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
4939 args
.sa_cache_this
= 0;
4941 return nfs4_call_sync_sequence(clp
, clp
->cl_rpcclient
, &msg
, &args
,
4945 void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
4947 struct nfs_client
*clp
= (struct nfs_client
*)data
;
4949 nfs41_sequence_done(clp
, task
->tk_msg
.rpc_resp
, task
->tk_status
);
4951 if (task
->tk_status
< 0) {
4952 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
4954 if (_nfs4_async_handle_error(task
, NULL
, clp
, NULL
)
4956 nfs4_restart_rpc(task
, clp
);
4960 nfs41_sequence_free_slot(clp
, task
->tk_msg
.rpc_resp
);
4961 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
4963 kfree(task
->tk_msg
.rpc_argp
);
4964 kfree(task
->tk_msg
.rpc_resp
);
4966 dprintk("<-- %s\n", __func__
);
4969 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
4971 struct nfs_client
*clp
;
4972 struct nfs4_sequence_args
*args
;
4973 struct nfs4_sequence_res
*res
;
4975 clp
= (struct nfs_client
*)data
;
4976 args
= task
->tk_msg
.rpc_argp
;
4977 res
= task
->tk_msg
.rpc_resp
;
4979 if (nfs4_setup_sequence(clp
, args
, res
, 0, task
))
4981 rpc_call_start(task
);
4984 static const struct rpc_call_ops nfs41_sequence_ops
= {
4985 .rpc_call_done
= nfs41_sequence_call_done
,
4986 .rpc_call_prepare
= nfs41_sequence_prepare
,
4989 static int nfs41_proc_async_sequence(struct nfs_client
*clp
,
4990 struct rpc_cred
*cred
)
4992 struct nfs4_sequence_args
*args
;
4993 struct nfs4_sequence_res
*res
;
4994 struct rpc_message msg
= {
4995 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
4999 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
5002 res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
5007 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5008 msg
.rpc_argp
= args
;
5011 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
5012 &nfs41_sequence_ops
, (void *)clp
);
5015 #endif /* CONFIG_NFS_V4_1 */
5017 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5018 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5019 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5020 .recover_open
= nfs4_open_reclaim
,
5021 .recover_lock
= nfs4_lock_reclaim
,
5022 .establish_clid
= nfs4_init_clientid
,
5023 .get_clid_cred
= nfs4_get_setclientid_cred
,
5026 #if defined(CONFIG_NFS_V4_1)
5027 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5028 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5029 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5030 .recover_open
= nfs4_open_reclaim
,
5031 .recover_lock
= nfs4_lock_reclaim
,
5032 .establish_clid
= nfs4_proc_exchange_id
,
5033 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5035 #endif /* CONFIG_NFS_V4_1 */
5037 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5038 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5039 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5040 .recover_open
= nfs4_open_expired
,
5041 .recover_lock
= nfs4_lock_expired
,
5042 .establish_clid
= nfs4_init_clientid
,
5043 .get_clid_cred
= nfs4_get_setclientid_cred
,
5046 #if defined(CONFIG_NFS_V4_1)
5047 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5048 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5049 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5050 .recover_open
= nfs4_open_expired
,
5051 .recover_lock
= nfs4_lock_expired
,
5052 .establish_clid
= nfs4_proc_exchange_id
,
5053 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5055 #endif /* CONFIG_NFS_V4_1 */
5057 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
5058 .sched_state_renewal
= nfs4_proc_async_renew
,
5059 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
5060 .renew_lease
= nfs4_proc_renew
,
5063 #if defined(CONFIG_NFS_V4_1)
5064 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
5065 .sched_state_renewal
= nfs41_proc_async_sequence
,
5066 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
5067 .renew_lease
= nfs4_proc_sequence
,
5072 * Per minor version reboot and network partition recovery ops
5075 struct nfs4_state_recovery_ops
*nfs4_reboot_recovery_ops
[] = {
5076 &nfs40_reboot_recovery_ops
,
5077 #if defined(CONFIG_NFS_V4_1)
5078 &nfs41_reboot_recovery_ops
,
5082 struct nfs4_state_recovery_ops
*nfs4_nograce_recovery_ops
[] = {
5083 &nfs40_nograce_recovery_ops
,
5084 #if defined(CONFIG_NFS_V4_1)
5085 &nfs41_nograce_recovery_ops
,
5089 struct nfs4_state_maintenance_ops
*nfs4_state_renewal_ops
[] = {
5090 &nfs40_state_renewal_ops
,
5091 #if defined(CONFIG_NFS_V4_1)
5092 &nfs41_state_renewal_ops
,
5096 static const struct inode_operations nfs4_file_inode_operations
= {
5097 .permission
= nfs_permission
,
5098 .getattr
= nfs_getattr
,
5099 .setattr
= nfs_setattr
,
5100 .getxattr
= nfs4_getxattr
,
5101 .setxattr
= nfs4_setxattr
,
5102 .listxattr
= nfs4_listxattr
,
5105 const struct nfs_rpc_ops nfs_v4_clientops
= {
5106 .version
= 4, /* protocol version */
5107 .dentry_ops
= &nfs4_dentry_operations
,
5108 .dir_inode_ops
= &nfs4_dir_inode_operations
,
5109 .file_inode_ops
= &nfs4_file_inode_operations
,
5110 .getroot
= nfs4_proc_get_root
,
5111 .getattr
= nfs4_proc_getattr
,
5112 .setattr
= nfs4_proc_setattr
,
5113 .lookupfh
= nfs4_proc_lookupfh
,
5114 .lookup
= nfs4_proc_lookup
,
5115 .access
= nfs4_proc_access
,
5116 .readlink
= nfs4_proc_readlink
,
5117 .create
= nfs4_proc_create
,
5118 .remove
= nfs4_proc_remove
,
5119 .unlink_setup
= nfs4_proc_unlink_setup
,
5120 .unlink_done
= nfs4_proc_unlink_done
,
5121 .rename
= nfs4_proc_rename
,
5122 .link
= nfs4_proc_link
,
5123 .symlink
= nfs4_proc_symlink
,
5124 .mkdir
= nfs4_proc_mkdir
,
5125 .rmdir
= nfs4_proc_remove
,
5126 .readdir
= nfs4_proc_readdir
,
5127 .mknod
= nfs4_proc_mknod
,
5128 .statfs
= nfs4_proc_statfs
,
5129 .fsinfo
= nfs4_proc_fsinfo
,
5130 .pathconf
= nfs4_proc_pathconf
,
5131 .set_capabilities
= nfs4_server_capabilities
,
5132 .decode_dirent
= nfs4_decode_dirent
,
5133 .read_setup
= nfs4_proc_read_setup
,
5134 .read_done
= nfs4_read_done
,
5135 .write_setup
= nfs4_proc_write_setup
,
5136 .write_done
= nfs4_write_done
,
5137 .commit_setup
= nfs4_proc_commit_setup
,
5138 .commit_done
= nfs4_commit_done
,
5139 .lock
= nfs4_proc_lock
,
5140 .clear_acl_cache
= nfs4_zap_acl_attr
,
5141 .close_context
= nfs4_close_context
,