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_recover_proc_open(struct nfs4_opendata
*data
);
68 static int nfs4_do_fsinfo(struct nfs_server
*, struct nfs_fh
*, struct nfs_fsinfo
*);
69 static int nfs4_async_handle_error(struct rpc_task
*, const struct nfs_server
*, struct nfs4_state
*);
70 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
71 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
);
73 /* Prevent leaks of NFSv4 errors into userland */
74 static int nfs4_map_errors(int err
)
79 case -NFS4ERR_RESOURCE
:
82 dprintk("%s could not handle NFSv4 error %d\n",
90 * This is our standard bitmap for GETATTR requests.
92 const u32 nfs4_fattr_bitmap
[2] = {
97 | FATTR4_WORD0_FILEID
,
99 | FATTR4_WORD1_NUMLINKS
101 | FATTR4_WORD1_OWNER_GROUP
102 | FATTR4_WORD1_RAWDEV
103 | FATTR4_WORD1_SPACE_USED
104 | FATTR4_WORD1_TIME_ACCESS
105 | FATTR4_WORD1_TIME_METADATA
106 | FATTR4_WORD1_TIME_MODIFY
109 const u32 nfs4_statfs_bitmap
[2] = {
110 FATTR4_WORD0_FILES_AVAIL
111 | FATTR4_WORD0_FILES_FREE
112 | FATTR4_WORD0_FILES_TOTAL
,
113 FATTR4_WORD1_SPACE_AVAIL
114 | FATTR4_WORD1_SPACE_FREE
115 | FATTR4_WORD1_SPACE_TOTAL
118 const u32 nfs4_pathconf_bitmap
[2] = {
120 | FATTR4_WORD0_MAXNAME
,
124 const u32 nfs4_fsinfo_bitmap
[2] = { FATTR4_WORD0_MAXFILESIZE
125 | FATTR4_WORD0_MAXREAD
126 | FATTR4_WORD0_MAXWRITE
127 | FATTR4_WORD0_LEASE_TIME
,
131 const u32 nfs4_fs_locations_bitmap
[2] = {
133 | FATTR4_WORD0_CHANGE
136 | FATTR4_WORD0_FILEID
137 | FATTR4_WORD0_FS_LOCATIONS
,
139 | FATTR4_WORD1_NUMLINKS
141 | FATTR4_WORD1_OWNER_GROUP
142 | FATTR4_WORD1_RAWDEV
143 | FATTR4_WORD1_SPACE_USED
144 | FATTR4_WORD1_TIME_ACCESS
145 | FATTR4_WORD1_TIME_METADATA
146 | FATTR4_WORD1_TIME_MODIFY
147 | FATTR4_WORD1_MOUNTED_ON_FILEID
150 static void nfs4_setup_readdir(u64 cookie
, __be32
*verifier
, struct dentry
*dentry
,
151 struct nfs4_readdir_arg
*readdir
)
155 BUG_ON(readdir
->count
< 80);
157 readdir
->cookie
= cookie
;
158 memcpy(&readdir
->verifier
, verifier
, sizeof(readdir
->verifier
));
163 memset(&readdir
->verifier
, 0, sizeof(readdir
->verifier
));
168 * NFSv4 servers do not return entries for '.' and '..'
169 * Therefore, we fake these entries here. We let '.'
170 * have cookie 0 and '..' have cookie 1. Note that
171 * when talking to the server, we always send cookie 0
174 start
= p
= kmap_atomic(*readdir
->pages
, KM_USER0
);
177 *p
++ = xdr_one
; /* next */
178 *p
++ = xdr_zero
; /* cookie, first word */
179 *p
++ = xdr_one
; /* cookie, second word */
180 *p
++ = xdr_one
; /* entry len */
181 memcpy(p
, ".\0\0\0", 4); /* entry */
183 *p
++ = xdr_one
; /* bitmap length */
184 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
185 *p
++ = htonl(8); /* attribute buffer length */
186 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_inode
));
189 *p
++ = xdr_one
; /* next */
190 *p
++ = xdr_zero
; /* cookie, first word */
191 *p
++ = xdr_two
; /* cookie, second word */
192 *p
++ = xdr_two
; /* entry len */
193 memcpy(p
, "..\0\0", 4); /* entry */
195 *p
++ = xdr_one
; /* bitmap length */
196 *p
++ = htonl(FATTR4_WORD0_FILEID
); /* bitmap */
197 *p
++ = htonl(8); /* attribute buffer length */
198 p
= xdr_encode_hyper(p
, NFS_FILEID(dentry
->d_parent
->d_inode
));
200 readdir
->pgbase
= (char *)p
- (char *)start
;
201 readdir
->count
-= readdir
->pgbase
;
202 kunmap_atomic(start
, KM_USER0
);
205 static int nfs4_wait_clnt_recover(struct nfs_client
*clp
)
211 res
= wait_on_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
,
212 nfs_wait_bit_killable
, TASK_KILLABLE
);
216 static int nfs4_delay(struct rpc_clnt
*clnt
, long *timeout
)
223 *timeout
= NFS4_POLL_RETRY_MIN
;
224 if (*timeout
> NFS4_POLL_RETRY_MAX
)
225 *timeout
= NFS4_POLL_RETRY_MAX
;
226 schedule_timeout_killable(*timeout
);
227 if (fatal_signal_pending(current
))
233 /* This is the error handling routine for processes that are allowed
236 static int nfs4_handle_exception(const struct nfs_server
*server
, int errorcode
, struct nfs4_exception
*exception
)
238 struct nfs_client
*clp
= server
->nfs_client
;
239 struct nfs4_state
*state
= exception
->state
;
242 exception
->retry
= 0;
246 case -NFS4ERR_ADMIN_REVOKED
:
247 case -NFS4ERR_BAD_STATEID
:
248 case -NFS4ERR_OPENMODE
:
251 nfs4_state_mark_reclaim_nograce(clp
, state
);
252 goto do_state_recovery
;
253 case -NFS4ERR_STALE_STATEID
:
256 nfs4_state_mark_reclaim_reboot(clp
, state
);
257 case -NFS4ERR_STALE_CLIENTID
:
258 case -NFS4ERR_EXPIRED
:
259 goto do_state_recovery
;
260 #if defined(CONFIG_NFS_V4_1)
261 case -NFS4ERR_BADSESSION
:
262 case -NFS4ERR_BADSLOT
:
263 case -NFS4ERR_BAD_HIGH_SLOT
:
264 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
265 case -NFS4ERR_DEADSESSION
:
266 case -NFS4ERR_SEQ_FALSE_RETRY
:
267 case -NFS4ERR_SEQ_MISORDERED
:
268 dprintk("%s ERROR: %d Reset session\n", __func__
,
270 nfs4_schedule_state_recovery(clp
);
271 exception
->retry
= 1;
273 #endif /* defined(CONFIG_NFS_V4_1) */
274 case -NFS4ERR_FILE_OPEN
:
275 if (exception
->timeout
> HZ
) {
276 /* We have retried a decent amount, time to
285 ret
= nfs4_delay(server
->client
, &exception
->timeout
);
288 case -NFS4ERR_OLD_STATEID
:
289 exception
->retry
= 1;
291 /* We failed to handle the error */
292 return nfs4_map_errors(ret
);
294 nfs4_schedule_state_recovery(clp
);
295 ret
= nfs4_wait_clnt_recover(clp
);
297 exception
->retry
= 1;
302 static void renew_lease(const struct nfs_server
*server
, unsigned long timestamp
)
304 struct nfs_client
*clp
= server
->nfs_client
;
305 spin_lock(&clp
->cl_lock
);
306 if (time_before(clp
->cl_last_renewal
,timestamp
))
307 clp
->cl_last_renewal
= timestamp
;
308 spin_unlock(&clp
->cl_lock
);
311 #if defined(CONFIG_NFS_V4_1)
314 * nfs4_free_slot - free a slot and efficiently update slot table.
316 * freeing a slot is trivially done by clearing its respective bit
318 * If the freed slotid equals highest_used_slotid we want to update it
319 * so that the server would be able to size down the slot table if needed,
320 * otherwise we know that the highest_used_slotid is still in use.
321 * When updating highest_used_slotid there may be "holes" in the bitmap
322 * so we need to scan down from highest_used_slotid to 0 looking for the now
323 * highest slotid in use.
324 * If none found, highest_used_slotid is set to -1.
326 * Must be called while holding tbl->slot_tbl_lock
329 nfs4_free_slot(struct nfs4_slot_table
*tbl
, u8 free_slotid
)
331 int slotid
= free_slotid
;
333 /* clear used bit in bitmap */
334 __clear_bit(slotid
, tbl
->used_slots
);
336 /* update highest_used_slotid when it is freed */
337 if (slotid
== tbl
->highest_used_slotid
) {
338 slotid
= find_last_bit(tbl
->used_slots
, tbl
->max_slots
);
339 if (slotid
< tbl
->max_slots
)
340 tbl
->highest_used_slotid
= slotid
;
342 tbl
->highest_used_slotid
= -1;
344 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__
,
345 free_slotid
, tbl
->highest_used_slotid
);
349 * Signal state manager thread if session is drained
351 static void nfs41_check_drain_session_complete(struct nfs4_session
*ses
)
353 struct rpc_task
*task
;
355 if (!test_bit(NFS4CLNT_SESSION_DRAINING
, &ses
->clp
->cl_state
)) {
356 task
= rpc_wake_up_next(&ses
->fc_slot_table
.slot_tbl_waitq
);
358 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
362 if (ses
->fc_slot_table
.highest_used_slotid
!= -1)
365 dprintk("%s COMPLETE: Session Drained\n", __func__
);
366 complete(&ses
->complete
);
369 static void nfs41_sequence_free_slot(const struct nfs_client
*clp
,
370 struct nfs4_sequence_res
*res
)
372 struct nfs4_slot_table
*tbl
;
374 tbl
= &clp
->cl_session
->fc_slot_table
;
375 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
) {
376 /* just wake up the next guy waiting since
377 * we may have not consumed a slot after all */
378 dprintk("%s: No slot\n", __func__
);
382 spin_lock(&tbl
->slot_tbl_lock
);
383 nfs4_free_slot(tbl
, res
->sr_slotid
);
384 nfs41_check_drain_session_complete(clp
->cl_session
);
385 spin_unlock(&tbl
->slot_tbl_lock
);
386 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
389 static void nfs41_sequence_done(struct nfs_client
*clp
,
390 struct nfs4_sequence_res
*res
,
393 unsigned long timestamp
;
394 struct nfs4_slot_table
*tbl
;
395 struct nfs4_slot
*slot
;
398 * sr_status remains 1 if an RPC level error occurred. The server
399 * may or may not have processed the sequence operation..
400 * Proceed as if the server received and processed the sequence
403 if (res
->sr_status
== 1)
404 res
->sr_status
= NFS_OK
;
406 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
407 if (res
->sr_slotid
== NFS4_MAX_SLOT_TABLE
)
410 /* Check the SEQUENCE operation status */
411 if (res
->sr_status
== 0) {
412 tbl
= &clp
->cl_session
->fc_slot_table
;
413 slot
= tbl
->slots
+ res
->sr_slotid
;
414 /* Update the slot's sequence and clientid lease timer */
416 timestamp
= res
->sr_renewal_time
;
417 spin_lock(&clp
->cl_lock
);
418 if (time_before(clp
->cl_last_renewal
, timestamp
))
419 clp
->cl_last_renewal
= timestamp
;
420 spin_unlock(&clp
->cl_lock
);
421 /* Check sequence flags */
422 if (atomic_read(&clp
->cl_count
) > 1)
423 nfs41_handle_sequence_flag_errors(clp
, res
->sr_status_flags
);
426 /* The session may be reset by one of the error handlers. */
427 dprintk("%s: Error %d free the slot \n", __func__
, res
->sr_status
);
428 nfs41_sequence_free_slot(clp
, res
);
432 * nfs4_find_slot - efficiently look for a free slot
434 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
435 * If found, we mark the slot as used, update the highest_used_slotid,
436 * and respectively set up the sequence operation args.
437 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
439 * Note: must be called with under the slot_tbl_lock.
442 nfs4_find_slot(struct nfs4_slot_table
*tbl
)
445 u8 ret_id
= NFS4_MAX_SLOT_TABLE
;
446 BUILD_BUG_ON((u8
)NFS4_MAX_SLOT_TABLE
!= (int)NFS4_MAX_SLOT_TABLE
);
448 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
449 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
,
451 slotid
= find_first_zero_bit(tbl
->used_slots
, tbl
->max_slots
);
452 if (slotid
>= tbl
->max_slots
)
454 __set_bit(slotid
, tbl
->used_slots
);
455 if (slotid
> tbl
->highest_used_slotid
)
456 tbl
->highest_used_slotid
= slotid
;
459 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
460 __func__
, tbl
->used_slots
[0], tbl
->highest_used_slotid
, ret_id
);
464 static int nfs41_setup_sequence(struct nfs4_session
*session
,
465 struct nfs4_sequence_args
*args
,
466 struct nfs4_sequence_res
*res
,
468 struct rpc_task
*task
)
470 struct nfs4_slot
*slot
;
471 struct nfs4_slot_table
*tbl
;
474 dprintk("--> %s\n", __func__
);
475 /* slot already allocated? */
476 if (res
->sr_slotid
!= NFS4_MAX_SLOT_TABLE
)
479 memset(res
, 0, sizeof(*res
));
480 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
481 tbl
= &session
->fc_slot_table
;
483 spin_lock(&tbl
->slot_tbl_lock
);
484 if (test_bit(NFS4CLNT_SESSION_DRAINING
, &session
->clp
->cl_state
) &&
485 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
487 * The state manager will wait until the slot table is empty.
488 * Schedule the reset thread
490 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
491 spin_unlock(&tbl
->slot_tbl_lock
);
492 dprintk("%s Schedule Session Reset\n", __func__
);
496 if (!rpc_queue_empty(&tbl
->slot_tbl_waitq
) &&
497 !rpc_task_has_priority(task
, RPC_PRIORITY_PRIVILEGED
)) {
498 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
499 spin_unlock(&tbl
->slot_tbl_lock
);
500 dprintk("%s enforce FIFO order\n", __func__
);
504 slotid
= nfs4_find_slot(tbl
);
505 if (slotid
== NFS4_MAX_SLOT_TABLE
) {
506 rpc_sleep_on(&tbl
->slot_tbl_waitq
, task
, NULL
);
507 spin_unlock(&tbl
->slot_tbl_lock
);
508 dprintk("<-- %s: no free slots\n", __func__
);
511 spin_unlock(&tbl
->slot_tbl_lock
);
513 rpc_task_set_priority(task
, RPC_PRIORITY_NORMAL
);
514 slot
= tbl
->slots
+ slotid
;
515 args
->sa_session
= session
;
516 args
->sa_slotid
= slotid
;
517 args
->sa_cache_this
= cache_reply
;
519 dprintk("<-- %s slotid=%d seqid=%d\n", __func__
, slotid
, slot
->seq_nr
);
521 res
->sr_session
= session
;
522 res
->sr_slotid
= slotid
;
523 res
->sr_renewal_time
= jiffies
;
525 * sr_status is only set in decode_sequence, and so will remain
526 * set to 1 if an rpc level failure occurs.
532 int nfs4_setup_sequence(struct nfs_client
*clp
,
533 struct nfs4_sequence_args
*args
,
534 struct nfs4_sequence_res
*res
,
536 struct rpc_task
*task
)
540 dprintk("--> %s clp %p session %p sr_slotid %d\n",
541 __func__
, clp
, clp
->cl_session
, res
->sr_slotid
);
543 if (!nfs4_has_session(clp
))
545 ret
= nfs41_setup_sequence(clp
->cl_session
, args
, res
, cache_reply
,
547 if (ret
&& ret
!= -EAGAIN
) {
548 /* terminate rpc task */
549 task
->tk_status
= ret
;
550 task
->tk_action
= NULL
;
553 dprintk("<-- %s status=%d\n", __func__
, ret
);
557 struct nfs41_call_sync_data
{
558 struct nfs_client
*clp
;
559 struct nfs4_sequence_args
*seq_args
;
560 struct nfs4_sequence_res
*seq_res
;
564 static void nfs41_call_sync_prepare(struct rpc_task
*task
, void *calldata
)
566 struct nfs41_call_sync_data
*data
= calldata
;
568 dprintk("--> %s data->clp->cl_session %p\n", __func__
,
569 data
->clp
->cl_session
);
570 if (nfs4_setup_sequence(data
->clp
, data
->seq_args
,
571 data
->seq_res
, data
->cache_reply
, task
))
573 rpc_call_start(task
);
576 static void nfs41_call_priv_sync_prepare(struct rpc_task
*task
, void *calldata
)
578 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
579 nfs41_call_sync_prepare(task
, calldata
);
582 static void nfs41_call_sync_done(struct rpc_task
*task
, void *calldata
)
584 struct nfs41_call_sync_data
*data
= calldata
;
586 nfs41_sequence_done(data
->clp
, data
->seq_res
, task
->tk_status
);
589 struct rpc_call_ops nfs41_call_sync_ops
= {
590 .rpc_call_prepare
= nfs41_call_sync_prepare
,
591 .rpc_call_done
= nfs41_call_sync_done
,
594 struct rpc_call_ops nfs41_call_priv_sync_ops
= {
595 .rpc_call_prepare
= nfs41_call_priv_sync_prepare
,
596 .rpc_call_done
= nfs41_call_sync_done
,
599 static int nfs4_call_sync_sequence(struct nfs_client
*clp
,
600 struct rpc_clnt
*clnt
,
601 struct rpc_message
*msg
,
602 struct nfs4_sequence_args
*args
,
603 struct nfs4_sequence_res
*res
,
608 struct rpc_task
*task
;
609 struct nfs41_call_sync_data data
= {
613 .cache_reply
= cache_reply
,
615 struct rpc_task_setup task_setup
= {
618 .callback_ops
= &nfs41_call_sync_ops
,
619 .callback_data
= &data
622 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
624 task_setup
.callback_ops
= &nfs41_call_priv_sync_ops
;
625 task
= rpc_run_task(&task_setup
);
629 ret
= task
->tk_status
;
635 int _nfs4_call_sync_session(struct nfs_server
*server
,
636 struct rpc_message
*msg
,
637 struct nfs4_sequence_args
*args
,
638 struct nfs4_sequence_res
*res
,
641 return nfs4_call_sync_sequence(server
->nfs_client
, server
->client
,
642 msg
, args
, res
, cache_reply
, 0);
645 #endif /* CONFIG_NFS_V4_1 */
647 int _nfs4_call_sync(struct nfs_server
*server
,
648 struct rpc_message
*msg
,
649 struct nfs4_sequence_args
*args
,
650 struct nfs4_sequence_res
*res
,
653 args
->sa_session
= res
->sr_session
= NULL
;
654 return rpc_call_sync(server
->client
, msg
, 0);
657 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
658 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
659 &(res)->seq_res, (cache_reply))
661 static void nfs4_sequence_done(const struct nfs_server
*server
,
662 struct nfs4_sequence_res
*res
, int rpc_status
)
664 #ifdef CONFIG_NFS_V4_1
665 if (nfs4_has_session(server
->nfs_client
))
666 nfs41_sequence_done(server
->nfs_client
, res
, rpc_status
);
667 #endif /* CONFIG_NFS_V4_1 */
670 static void update_changeattr(struct inode
*dir
, struct nfs4_change_info
*cinfo
)
672 struct nfs_inode
*nfsi
= NFS_I(dir
);
674 spin_lock(&dir
->i_lock
);
675 nfsi
->cache_validity
|= NFS_INO_INVALID_ATTR
|NFS_INO_REVAL_PAGECACHE
|NFS_INO_INVALID_DATA
;
676 if (!cinfo
->atomic
|| cinfo
->before
!= nfsi
->change_attr
)
677 nfs_force_lookup_revalidate(dir
);
678 nfsi
->change_attr
= cinfo
->after
;
679 spin_unlock(&dir
->i_lock
);
682 struct nfs4_opendata
{
684 struct nfs_openargs o_arg
;
685 struct nfs_openres o_res
;
686 struct nfs_open_confirmargs c_arg
;
687 struct nfs_open_confirmres c_res
;
688 struct nfs_fattr f_attr
;
689 struct nfs_fattr dir_attr
;
692 struct nfs4_state_owner
*owner
;
693 struct nfs4_state
*state
;
695 unsigned long timestamp
;
696 unsigned int rpc_done
: 1;
702 static void nfs4_init_opendata_res(struct nfs4_opendata
*p
)
704 p
->o_res
.f_attr
= &p
->f_attr
;
705 p
->o_res
.dir_attr
= &p
->dir_attr
;
706 p
->o_res
.seqid
= p
->o_arg
.seqid
;
707 p
->c_res
.seqid
= p
->c_arg
.seqid
;
708 p
->o_res
.server
= p
->o_arg
.server
;
709 nfs_fattr_init(&p
->f_attr
);
710 nfs_fattr_init(&p
->dir_attr
);
711 p
->o_res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
714 static struct nfs4_opendata
*nfs4_opendata_alloc(struct path
*path
,
715 struct nfs4_state_owner
*sp
, fmode_t fmode
, int flags
,
716 const struct iattr
*attrs
)
718 struct dentry
*parent
= dget_parent(path
->dentry
);
719 struct inode
*dir
= parent
->d_inode
;
720 struct nfs_server
*server
= NFS_SERVER(dir
);
721 struct nfs4_opendata
*p
;
723 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
726 p
->o_arg
.seqid
= nfs_alloc_seqid(&sp
->so_seqid
);
727 if (p
->o_arg
.seqid
== NULL
)
733 atomic_inc(&sp
->so_count
);
734 p
->o_arg
.fh
= NFS_FH(dir
);
735 p
->o_arg
.open_flags
= flags
;
736 p
->o_arg
.fmode
= fmode
& (FMODE_READ
|FMODE_WRITE
);
737 p
->o_arg
.clientid
= server
->nfs_client
->cl_clientid
;
738 p
->o_arg
.id
= sp
->so_owner_id
.id
;
739 p
->o_arg
.name
= &p
->path
.dentry
->d_name
;
740 p
->o_arg
.server
= server
;
741 p
->o_arg
.bitmask
= server
->attr_bitmask
;
742 p
->o_arg
.claim
= NFS4_OPEN_CLAIM_NULL
;
743 if (flags
& O_EXCL
) {
744 if (nfs4_has_persistent_session(server
->nfs_client
)) {
746 p
->o_arg
.u
.attrs
= &p
->attrs
;
747 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
748 } else { /* EXCLUSIVE4_1 */
749 u32
*s
= (u32
*) p
->o_arg
.u
.verifier
.data
;
753 } else if (flags
& O_CREAT
) {
754 p
->o_arg
.u
.attrs
= &p
->attrs
;
755 memcpy(&p
->attrs
, attrs
, sizeof(p
->attrs
));
757 p
->c_arg
.fh
= &p
->o_res
.fh
;
758 p
->c_arg
.stateid
= &p
->o_res
.stateid
;
759 p
->c_arg
.seqid
= p
->o_arg
.seqid
;
760 nfs4_init_opendata_res(p
);
770 static void nfs4_opendata_free(struct kref
*kref
)
772 struct nfs4_opendata
*p
= container_of(kref
,
773 struct nfs4_opendata
, kref
);
775 nfs_free_seqid(p
->o_arg
.seqid
);
776 if (p
->state
!= NULL
)
777 nfs4_put_open_state(p
->state
);
778 nfs4_put_state_owner(p
->owner
);
784 static void nfs4_opendata_put(struct nfs4_opendata
*p
)
787 kref_put(&p
->kref
, nfs4_opendata_free
);
790 static int nfs4_wait_for_completion_rpc_task(struct rpc_task
*task
)
794 ret
= rpc_wait_for_completion_task(task
);
798 static int can_open_cached(struct nfs4_state
*state
, fmode_t mode
, int open_mode
)
802 if (open_mode
& O_EXCL
)
804 switch (mode
& (FMODE_READ
|FMODE_WRITE
)) {
806 ret
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
) != 0
807 && state
->n_rdonly
!= 0;
810 ret
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
) != 0
811 && state
->n_wronly
!= 0;
813 case FMODE_READ
|FMODE_WRITE
:
814 ret
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
) != 0
815 && state
->n_rdwr
!= 0;
821 static int can_open_delegated(struct nfs_delegation
*delegation
, fmode_t fmode
)
823 if ((delegation
->type
& fmode
) != fmode
)
825 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
))
827 nfs_mark_delegation_referenced(delegation
);
831 static void update_open_stateflags(struct nfs4_state
*state
, fmode_t fmode
)
840 case FMODE_READ
|FMODE_WRITE
:
843 nfs4_state_set_mode_locked(state
, state
->state
| fmode
);
846 static void nfs_set_open_stateid_locked(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
848 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
849 memcpy(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
));
850 memcpy(state
->open_stateid
.data
, stateid
->data
, sizeof(state
->open_stateid
.data
));
853 set_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
856 set_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
858 case FMODE_READ
|FMODE_WRITE
:
859 set_bit(NFS_O_RDWR_STATE
, &state
->flags
);
863 static void nfs_set_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*stateid
, fmode_t fmode
)
865 write_seqlock(&state
->seqlock
);
866 nfs_set_open_stateid_locked(state
, stateid
, fmode
);
867 write_sequnlock(&state
->seqlock
);
870 static void __update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, const nfs4_stateid
*deleg_stateid
, fmode_t fmode
)
873 * Protect the call to nfs4_state_set_mode_locked and
874 * serialise the stateid update
876 write_seqlock(&state
->seqlock
);
877 if (deleg_stateid
!= NULL
) {
878 memcpy(state
->stateid
.data
, deleg_stateid
->data
, sizeof(state
->stateid
.data
));
879 set_bit(NFS_DELEGATED_STATE
, &state
->flags
);
881 if (open_stateid
!= NULL
)
882 nfs_set_open_stateid_locked(state
, open_stateid
, fmode
);
883 write_sequnlock(&state
->seqlock
);
884 spin_lock(&state
->owner
->so_lock
);
885 update_open_stateflags(state
, fmode
);
886 spin_unlock(&state
->owner
->so_lock
);
889 static int update_open_stateid(struct nfs4_state
*state
, nfs4_stateid
*open_stateid
, nfs4_stateid
*delegation
, fmode_t fmode
)
891 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
892 struct nfs_delegation
*deleg_cur
;
895 fmode
&= (FMODE_READ
|FMODE_WRITE
);
898 deleg_cur
= rcu_dereference(nfsi
->delegation
);
899 if (deleg_cur
== NULL
)
902 spin_lock(&deleg_cur
->lock
);
903 if (nfsi
->delegation
!= deleg_cur
||
904 (deleg_cur
->type
& fmode
) != fmode
)
905 goto no_delegation_unlock
;
907 if (delegation
== NULL
)
908 delegation
= &deleg_cur
->stateid
;
909 else if (memcmp(deleg_cur
->stateid
.data
, delegation
->data
, NFS4_STATEID_SIZE
) != 0)
910 goto no_delegation_unlock
;
912 nfs_mark_delegation_referenced(deleg_cur
);
913 __update_open_stateid(state
, open_stateid
, &deleg_cur
->stateid
, fmode
);
915 no_delegation_unlock
:
916 spin_unlock(&deleg_cur
->lock
);
920 if (!ret
&& open_stateid
!= NULL
) {
921 __update_open_stateid(state
, open_stateid
, NULL
, fmode
);
929 static void nfs4_return_incompatible_delegation(struct inode
*inode
, fmode_t fmode
)
931 struct nfs_delegation
*delegation
;
934 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
935 if (delegation
== NULL
|| (delegation
->type
& fmode
) == fmode
) {
940 nfs_inode_return_delegation(inode
);
943 static struct nfs4_state
*nfs4_try_open_cached(struct nfs4_opendata
*opendata
)
945 struct nfs4_state
*state
= opendata
->state
;
946 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
947 struct nfs_delegation
*delegation
;
948 int open_mode
= opendata
->o_arg
.open_flags
& O_EXCL
;
949 fmode_t fmode
= opendata
->o_arg
.fmode
;
950 nfs4_stateid stateid
;
954 if (can_open_cached(state
, fmode
, open_mode
)) {
955 spin_lock(&state
->owner
->so_lock
);
956 if (can_open_cached(state
, fmode
, open_mode
)) {
957 update_open_stateflags(state
, fmode
);
958 spin_unlock(&state
->owner
->so_lock
);
959 goto out_return_state
;
961 spin_unlock(&state
->owner
->so_lock
);
964 delegation
= rcu_dereference(nfsi
->delegation
);
965 if (delegation
== NULL
||
966 !can_open_delegated(delegation
, fmode
)) {
970 /* Save the delegation */
971 memcpy(stateid
.data
, delegation
->stateid
.data
, sizeof(stateid
.data
));
973 ret
= nfs_may_open(state
->inode
, state
->owner
->so_cred
, open_mode
);
978 /* Try to update the stateid using the delegation */
979 if (update_open_stateid(state
, NULL
, &stateid
, fmode
))
980 goto out_return_state
;
985 atomic_inc(&state
->count
);
989 static struct nfs4_state
*nfs4_opendata_to_nfs4_state(struct nfs4_opendata
*data
)
992 struct nfs4_state
*state
= NULL
;
993 struct nfs_delegation
*delegation
;
996 if (!data
->rpc_done
) {
997 state
= nfs4_try_open_cached(data
);
1002 if (!(data
->f_attr
.valid
& NFS_ATTR_FATTR
))
1004 inode
= nfs_fhget(data
->dir
->d_sb
, &data
->o_res
.fh
, &data
->f_attr
);
1005 ret
= PTR_ERR(inode
);
1009 state
= nfs4_get_open_state(inode
, data
->owner
);
1012 if (data
->o_res
.delegation_type
!= 0) {
1013 int delegation_flags
= 0;
1016 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1018 delegation_flags
= delegation
->flags
;
1020 if ((delegation_flags
& 1UL<<NFS_DELEGATION_NEED_RECLAIM
) == 0)
1021 nfs_inode_set_delegation(state
->inode
,
1022 data
->owner
->so_cred
,
1025 nfs_inode_reclaim_delegation(state
->inode
,
1026 data
->owner
->so_cred
,
1030 update_open_stateid(state
, &data
->o_res
.stateid
, NULL
,
1038 return ERR_PTR(ret
);
1041 static struct nfs_open_context
*nfs4_state_find_open_context(struct nfs4_state
*state
)
1043 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
1044 struct nfs_open_context
*ctx
;
1046 spin_lock(&state
->inode
->i_lock
);
1047 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1048 if (ctx
->state
!= state
)
1050 get_nfs_open_context(ctx
);
1051 spin_unlock(&state
->inode
->i_lock
);
1054 spin_unlock(&state
->inode
->i_lock
);
1055 return ERR_PTR(-ENOENT
);
1058 static struct nfs4_opendata
*nfs4_open_recoverdata_alloc(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1060 struct nfs4_opendata
*opendata
;
1062 opendata
= nfs4_opendata_alloc(&ctx
->path
, state
->owner
, 0, 0, NULL
);
1063 if (opendata
== NULL
)
1064 return ERR_PTR(-ENOMEM
);
1065 opendata
->state
= state
;
1066 atomic_inc(&state
->count
);
1070 static int nfs4_open_recover_helper(struct nfs4_opendata
*opendata
, fmode_t fmode
, struct nfs4_state
**res
)
1072 struct nfs4_state
*newstate
;
1075 opendata
->o_arg
.open_flags
= 0;
1076 opendata
->o_arg
.fmode
= fmode
;
1077 memset(&opendata
->o_res
, 0, sizeof(opendata
->o_res
));
1078 memset(&opendata
->c_res
, 0, sizeof(opendata
->c_res
));
1079 nfs4_init_opendata_res(opendata
);
1080 ret
= _nfs4_recover_proc_open(opendata
);
1083 newstate
= nfs4_opendata_to_nfs4_state(opendata
);
1084 if (IS_ERR(newstate
))
1085 return PTR_ERR(newstate
);
1086 nfs4_close_state(&opendata
->path
, newstate
, fmode
);
1091 static int nfs4_open_recover(struct nfs4_opendata
*opendata
, struct nfs4_state
*state
)
1093 struct nfs4_state
*newstate
;
1096 /* memory barrier prior to reading state->n_* */
1097 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1099 if (state
->n_rdwr
!= 0) {
1100 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
|FMODE_WRITE
, &newstate
);
1103 if (newstate
!= state
)
1106 if (state
->n_wronly
!= 0) {
1107 ret
= nfs4_open_recover_helper(opendata
, FMODE_WRITE
, &newstate
);
1110 if (newstate
!= state
)
1113 if (state
->n_rdonly
!= 0) {
1114 ret
= nfs4_open_recover_helper(opendata
, FMODE_READ
, &newstate
);
1117 if (newstate
!= state
)
1121 * We may have performed cached opens for all three recoveries.
1122 * Check if we need to update the current stateid.
1124 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0 &&
1125 memcmp(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
)) != 0) {
1126 write_seqlock(&state
->seqlock
);
1127 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) == 0)
1128 memcpy(state
->stateid
.data
, state
->open_stateid
.data
, sizeof(state
->stateid
.data
));
1129 write_sequnlock(&state
->seqlock
);
1136 * reclaim state on the server after a reboot.
1138 static int _nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1140 struct nfs_delegation
*delegation
;
1141 struct nfs4_opendata
*opendata
;
1142 fmode_t delegation_type
= 0;
1145 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1146 if (IS_ERR(opendata
))
1147 return PTR_ERR(opendata
);
1148 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_PREVIOUS
;
1149 opendata
->o_arg
.fh
= NFS_FH(state
->inode
);
1151 delegation
= rcu_dereference(NFS_I(state
->inode
)->delegation
);
1152 if (delegation
!= NULL
&& test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) != 0)
1153 delegation_type
= delegation
->type
;
1155 opendata
->o_arg
.u
.delegation_type
= delegation_type
;
1156 status
= nfs4_open_recover(opendata
, state
);
1157 nfs4_opendata_put(opendata
);
1161 static int nfs4_do_open_reclaim(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1163 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1164 struct nfs4_exception exception
= { };
1167 err
= _nfs4_do_open_reclaim(ctx
, state
);
1168 if (err
!= -NFS4ERR_DELAY
&& err
!= -EKEYEXPIRED
)
1170 nfs4_handle_exception(server
, err
, &exception
);
1171 } while (exception
.retry
);
1175 static int nfs4_open_reclaim(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1177 struct nfs_open_context
*ctx
;
1180 ctx
= nfs4_state_find_open_context(state
);
1182 return PTR_ERR(ctx
);
1183 ret
= nfs4_do_open_reclaim(ctx
, state
);
1184 put_nfs_open_context(ctx
);
1188 static int _nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1190 struct nfs4_opendata
*opendata
;
1193 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1194 if (IS_ERR(opendata
))
1195 return PTR_ERR(opendata
);
1196 opendata
->o_arg
.claim
= NFS4_OPEN_CLAIM_DELEGATE_CUR
;
1197 memcpy(opendata
->o_arg
.u
.delegation
.data
, stateid
->data
,
1198 sizeof(opendata
->o_arg
.u
.delegation
.data
));
1199 ret
= nfs4_open_recover(opendata
, state
);
1200 nfs4_opendata_put(opendata
);
1204 int nfs4_open_delegation_recall(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
1206 struct nfs4_exception exception
= { };
1207 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1210 err
= _nfs4_open_delegation_recall(ctx
, state
, stateid
);
1216 case -NFS4ERR_BADSESSION
:
1217 case -NFS4ERR_BADSLOT
:
1218 case -NFS4ERR_BAD_HIGH_SLOT
:
1219 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1220 case -NFS4ERR_DEADSESSION
:
1221 nfs4_schedule_state_recovery(
1222 server
->nfs_client
);
1224 case -NFS4ERR_STALE_CLIENTID
:
1225 case -NFS4ERR_STALE_STATEID
:
1226 case -NFS4ERR_EXPIRED
:
1227 /* Don't recall a delegation if it was lost */
1228 nfs4_schedule_state_recovery(server
->nfs_client
);
1232 * The show must go on: exit, but mark the
1233 * stateid as needing recovery.
1235 case -NFS4ERR_ADMIN_REVOKED
:
1236 case -NFS4ERR_BAD_STATEID
:
1237 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
1242 err
= nfs4_handle_exception(server
, err
, &exception
);
1243 } while (exception
.retry
);
1248 static void nfs4_open_confirm_done(struct rpc_task
*task
, void *calldata
)
1250 struct nfs4_opendata
*data
= calldata
;
1252 data
->rpc_status
= task
->tk_status
;
1253 if (RPC_ASSASSINATED(task
))
1255 if (data
->rpc_status
== 0) {
1256 memcpy(data
->o_res
.stateid
.data
, data
->c_res
.stateid
.data
,
1257 sizeof(data
->o_res
.stateid
.data
));
1258 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1259 renew_lease(data
->o_res
.server
, data
->timestamp
);
1264 static void nfs4_open_confirm_release(void *calldata
)
1266 struct nfs4_opendata
*data
= calldata
;
1267 struct nfs4_state
*state
= NULL
;
1269 /* If this request hasn't been cancelled, do nothing */
1270 if (data
->cancelled
== 0)
1272 /* In case of error, no cleanup! */
1273 if (!data
->rpc_done
)
1275 state
= nfs4_opendata_to_nfs4_state(data
);
1277 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1279 nfs4_opendata_put(data
);
1282 static const struct rpc_call_ops nfs4_open_confirm_ops
= {
1283 .rpc_call_done
= nfs4_open_confirm_done
,
1284 .rpc_release
= nfs4_open_confirm_release
,
1288 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1290 static int _nfs4_proc_open_confirm(struct nfs4_opendata
*data
)
1292 struct nfs_server
*server
= NFS_SERVER(data
->dir
->d_inode
);
1293 struct rpc_task
*task
;
1294 struct rpc_message msg
= {
1295 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_CONFIRM
],
1296 .rpc_argp
= &data
->c_arg
,
1297 .rpc_resp
= &data
->c_res
,
1298 .rpc_cred
= data
->owner
->so_cred
,
1300 struct rpc_task_setup task_setup_data
= {
1301 .rpc_client
= server
->client
,
1302 .rpc_message
= &msg
,
1303 .callback_ops
= &nfs4_open_confirm_ops
,
1304 .callback_data
= data
,
1305 .workqueue
= nfsiod_workqueue
,
1306 .flags
= RPC_TASK_ASYNC
,
1310 kref_get(&data
->kref
);
1312 data
->rpc_status
= 0;
1313 data
->timestamp
= jiffies
;
1314 task
= rpc_run_task(&task_setup_data
);
1316 return PTR_ERR(task
);
1317 status
= nfs4_wait_for_completion_rpc_task(task
);
1319 data
->cancelled
= 1;
1322 status
= data
->rpc_status
;
1327 static void nfs4_open_prepare(struct rpc_task
*task
, void *calldata
)
1329 struct nfs4_opendata
*data
= calldata
;
1330 struct nfs4_state_owner
*sp
= data
->owner
;
1332 if (nfs_wait_on_sequence(data
->o_arg
.seqid
, task
) != 0)
1335 * Check if we still need to send an OPEN call, or if we can use
1336 * a delegation instead.
1338 if (data
->state
!= NULL
) {
1339 struct nfs_delegation
*delegation
;
1341 if (can_open_cached(data
->state
, data
->o_arg
.fmode
, data
->o_arg
.open_flags
))
1344 delegation
= rcu_dereference(NFS_I(data
->state
->inode
)->delegation
);
1345 if (delegation
!= NULL
&&
1346 test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0) {
1352 /* Update sequence id. */
1353 data
->o_arg
.id
= sp
->so_owner_id
.id
;
1354 data
->o_arg
.clientid
= sp
->so_client
->cl_clientid
;
1355 if (data
->o_arg
.claim
== NFS4_OPEN_CLAIM_PREVIOUS
) {
1356 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_NOATTR
];
1357 nfs_copy_fh(&data
->o_res
.fh
, data
->o_arg
.fh
);
1359 data
->timestamp
= jiffies
;
1360 if (nfs4_setup_sequence(data
->o_arg
.server
->nfs_client
,
1361 &data
->o_arg
.seq_args
,
1362 &data
->o_res
.seq_res
, 1, task
))
1364 rpc_call_start(task
);
1367 task
->tk_action
= NULL
;
1371 static void nfs4_recover_open_prepare(struct rpc_task
*task
, void *calldata
)
1373 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
1374 nfs4_open_prepare(task
, calldata
);
1377 static void nfs4_open_done(struct rpc_task
*task
, void *calldata
)
1379 struct nfs4_opendata
*data
= calldata
;
1381 data
->rpc_status
= task
->tk_status
;
1383 nfs4_sequence_done(data
->o_arg
.server
, &data
->o_res
.seq_res
,
1386 if (RPC_ASSASSINATED(task
))
1388 if (task
->tk_status
== 0) {
1389 switch (data
->o_res
.f_attr
->mode
& S_IFMT
) {
1393 data
->rpc_status
= -ELOOP
;
1396 data
->rpc_status
= -EISDIR
;
1399 data
->rpc_status
= -ENOTDIR
;
1401 renew_lease(data
->o_res
.server
, data
->timestamp
);
1402 if (!(data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
))
1403 nfs_confirm_seqid(&data
->owner
->so_seqid
, 0);
1408 static void nfs4_open_release(void *calldata
)
1410 struct nfs4_opendata
*data
= calldata
;
1411 struct nfs4_state
*state
= NULL
;
1413 /* If this request hasn't been cancelled, do nothing */
1414 if (data
->cancelled
== 0)
1416 /* In case of error, no cleanup! */
1417 if (data
->rpc_status
!= 0 || !data
->rpc_done
)
1419 /* In case we need an open_confirm, no cleanup! */
1420 if (data
->o_res
.rflags
& NFS4_OPEN_RESULT_CONFIRM
)
1422 state
= nfs4_opendata_to_nfs4_state(data
);
1424 nfs4_close_state(&data
->path
, state
, data
->o_arg
.fmode
);
1426 nfs4_opendata_put(data
);
1429 static const struct rpc_call_ops nfs4_open_ops
= {
1430 .rpc_call_prepare
= nfs4_open_prepare
,
1431 .rpc_call_done
= nfs4_open_done
,
1432 .rpc_release
= nfs4_open_release
,
1435 static const struct rpc_call_ops nfs4_recover_open_ops
= {
1436 .rpc_call_prepare
= nfs4_recover_open_prepare
,
1437 .rpc_call_done
= nfs4_open_done
,
1438 .rpc_release
= nfs4_open_release
,
1441 static int nfs4_run_open_task(struct nfs4_opendata
*data
, int isrecover
)
1443 struct inode
*dir
= data
->dir
->d_inode
;
1444 struct nfs_server
*server
= NFS_SERVER(dir
);
1445 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1446 struct nfs_openres
*o_res
= &data
->o_res
;
1447 struct rpc_task
*task
;
1448 struct rpc_message msg
= {
1449 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN
],
1452 .rpc_cred
= data
->owner
->so_cred
,
1454 struct rpc_task_setup task_setup_data
= {
1455 .rpc_client
= server
->client
,
1456 .rpc_message
= &msg
,
1457 .callback_ops
= &nfs4_open_ops
,
1458 .callback_data
= data
,
1459 .workqueue
= nfsiod_workqueue
,
1460 .flags
= RPC_TASK_ASYNC
,
1464 kref_get(&data
->kref
);
1466 data
->rpc_status
= 0;
1467 data
->cancelled
= 0;
1469 task_setup_data
.callback_ops
= &nfs4_recover_open_ops
;
1470 task
= rpc_run_task(&task_setup_data
);
1472 return PTR_ERR(task
);
1473 status
= nfs4_wait_for_completion_rpc_task(task
);
1475 data
->cancelled
= 1;
1478 status
= data
->rpc_status
;
1484 static int _nfs4_recover_proc_open(struct nfs4_opendata
*data
)
1486 struct inode
*dir
= data
->dir
->d_inode
;
1487 struct nfs_openres
*o_res
= &data
->o_res
;
1490 status
= nfs4_run_open_task(data
, 1);
1491 if (status
!= 0 || !data
->rpc_done
)
1494 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1496 if (o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1497 status
= _nfs4_proc_open_confirm(data
);
1506 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1508 static int _nfs4_proc_open(struct nfs4_opendata
*data
)
1510 struct inode
*dir
= data
->dir
->d_inode
;
1511 struct nfs_server
*server
= NFS_SERVER(dir
);
1512 struct nfs_openargs
*o_arg
= &data
->o_arg
;
1513 struct nfs_openres
*o_res
= &data
->o_res
;
1516 status
= nfs4_run_open_task(data
, 0);
1517 if (status
!= 0 || !data
->rpc_done
)
1520 if (o_arg
->open_flags
& O_CREAT
) {
1521 update_changeattr(dir
, &o_res
->cinfo
);
1522 nfs_post_op_update_inode(dir
, o_res
->dir_attr
);
1524 nfs_refresh_inode(dir
, o_res
->dir_attr
);
1525 if(o_res
->rflags
& NFS4_OPEN_RESULT_CONFIRM
) {
1526 status
= _nfs4_proc_open_confirm(data
);
1530 if (!(o_res
->f_attr
->valid
& NFS_ATTR_FATTR
))
1531 _nfs4_proc_getattr(server
, &o_res
->fh
, o_res
->f_attr
);
1535 static int nfs4_recover_expired_lease(struct nfs_server
*server
)
1537 struct nfs_client
*clp
= server
->nfs_client
;
1541 for (loop
= NFS4_MAX_LOOP_ON_RECOVER
; loop
!= 0; loop
--) {
1542 ret
= nfs4_wait_clnt_recover(clp
);
1545 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) &&
1546 !test_bit(NFS4CLNT_CHECK_LEASE
,&clp
->cl_state
))
1548 nfs4_schedule_state_recovery(clp
);
1556 * reclaim state on the server after a network partition.
1557 * Assumes caller holds the appropriate lock
1559 static int _nfs4_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1561 struct nfs4_opendata
*opendata
;
1564 opendata
= nfs4_open_recoverdata_alloc(ctx
, state
);
1565 if (IS_ERR(opendata
))
1566 return PTR_ERR(opendata
);
1567 ret
= nfs4_open_recover(opendata
, state
);
1569 d_drop(ctx
->path
.dentry
);
1570 nfs4_opendata_put(opendata
);
1574 static int nfs4_do_open_expired(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
1576 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1577 struct nfs4_exception exception
= { };
1581 err
= _nfs4_open_expired(ctx
, state
);
1585 case -NFS4ERR_GRACE
:
1586 case -NFS4ERR_DELAY
:
1588 nfs4_handle_exception(server
, err
, &exception
);
1591 } while (exception
.retry
);
1596 static int nfs4_open_expired(struct nfs4_state_owner
*sp
, struct nfs4_state
*state
)
1598 struct nfs_open_context
*ctx
;
1601 ctx
= nfs4_state_find_open_context(state
);
1603 return PTR_ERR(ctx
);
1604 ret
= nfs4_do_open_expired(ctx
, state
);
1605 put_nfs_open_context(ctx
);
1610 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1611 * fields corresponding to attributes that were used to store the verifier.
1612 * Make sure we clobber those fields in the later setattr call
1614 static inline void nfs4_exclusive_attrset(struct nfs4_opendata
*opendata
, struct iattr
*sattr
)
1616 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_ACCESS
) &&
1617 !(sattr
->ia_valid
& ATTR_ATIME_SET
))
1618 sattr
->ia_valid
|= ATTR_ATIME
;
1620 if ((opendata
->o_res
.attrset
[1] & FATTR4_WORD1_TIME_MODIFY
) &&
1621 !(sattr
->ia_valid
& ATTR_MTIME_SET
))
1622 sattr
->ia_valid
|= ATTR_MTIME
;
1626 * Returns a referenced nfs4_state
1628 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
)
1630 struct nfs4_state_owner
*sp
;
1631 struct nfs4_state
*state
= NULL
;
1632 struct nfs_server
*server
= NFS_SERVER(dir
);
1633 struct nfs4_opendata
*opendata
;
1636 /* Protect against reboot recovery conflicts */
1638 if (!(sp
= nfs4_get_state_owner(server
, cred
))) {
1639 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1642 status
= nfs4_recover_expired_lease(server
);
1644 goto err_put_state_owner
;
1645 if (path
->dentry
->d_inode
!= NULL
)
1646 nfs4_return_incompatible_delegation(path
->dentry
->d_inode
, fmode
);
1648 opendata
= nfs4_opendata_alloc(path
, sp
, fmode
, flags
, sattr
);
1649 if (opendata
== NULL
)
1650 goto err_put_state_owner
;
1652 if (path
->dentry
->d_inode
!= NULL
)
1653 opendata
->state
= nfs4_get_open_state(path
->dentry
->d_inode
, sp
);
1655 status
= _nfs4_proc_open(opendata
);
1657 goto err_opendata_put
;
1659 if (opendata
->o_arg
.open_flags
& O_EXCL
)
1660 nfs4_exclusive_attrset(opendata
, sattr
);
1662 state
= nfs4_opendata_to_nfs4_state(opendata
);
1663 status
= PTR_ERR(state
);
1665 goto err_opendata_put
;
1666 if ((opendata
->o_res
.rflags
& NFS4_OPEN_RESULT_LOCKTYPE_POSIX
) != 0)
1667 set_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
);
1668 nfs4_opendata_put(opendata
);
1669 nfs4_put_state_owner(sp
);
1673 nfs4_opendata_put(opendata
);
1674 err_put_state_owner
:
1675 nfs4_put_state_owner(sp
);
1682 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
)
1684 struct nfs4_exception exception
= { };
1685 struct nfs4_state
*res
;
1689 status
= _nfs4_do_open(dir
, path
, fmode
, flags
, sattr
, cred
, &res
);
1692 /* NOTE: BAD_SEQID means the server and client disagree about the
1693 * book-keeping w.r.t. state-changing operations
1694 * (OPEN/CLOSE/LOCK/LOCKU...)
1695 * It is actually a sign of a bug on the client or on the server.
1697 * If we receive a BAD_SEQID error in the particular case of
1698 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1699 * have unhashed the old state_owner for us, and that we can
1700 * therefore safely retry using a new one. We should still warn
1701 * the user though...
1703 if (status
== -NFS4ERR_BAD_SEQID
) {
1704 printk(KERN_WARNING
"NFS: v4 server %s "
1705 " returned a bad sequence-id error!\n",
1706 NFS_SERVER(dir
)->nfs_client
->cl_hostname
);
1707 exception
.retry
= 1;
1711 * BAD_STATEID on OPEN means that the server cancelled our
1712 * state before it received the OPEN_CONFIRM.
1713 * Recover by retrying the request as per the discussion
1714 * on Page 181 of RFC3530.
1716 if (status
== -NFS4ERR_BAD_STATEID
) {
1717 exception
.retry
= 1;
1720 if (status
== -EAGAIN
) {
1721 /* We must have found a delegation */
1722 exception
.retry
= 1;
1725 res
= ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir
),
1726 status
, &exception
));
1727 } while (exception
.retry
);
1731 static int _nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1732 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1733 struct nfs4_state
*state
)
1735 struct nfs_server
*server
= NFS_SERVER(inode
);
1736 struct nfs_setattrargs arg
= {
1737 .fh
= NFS_FH(inode
),
1740 .bitmask
= server
->attr_bitmask
,
1742 struct nfs_setattrres res
= {
1746 struct rpc_message msg
= {
1747 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETATTR
],
1752 unsigned long timestamp
= jiffies
;
1755 nfs_fattr_init(fattr
);
1757 if (nfs4_copy_delegation_stateid(&arg
.stateid
, inode
)) {
1758 /* Use that stateid */
1759 } else if (state
!= NULL
) {
1760 nfs4_copy_stateid(&arg
.stateid
, state
, current
->files
);
1762 memcpy(&arg
.stateid
, &zero_stateid
, sizeof(arg
.stateid
));
1764 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
1765 if (status
== 0 && state
!= NULL
)
1766 renew_lease(server
, timestamp
);
1770 static int nfs4_do_setattr(struct inode
*inode
, struct rpc_cred
*cred
,
1771 struct nfs_fattr
*fattr
, struct iattr
*sattr
,
1772 struct nfs4_state
*state
)
1774 struct nfs_server
*server
= NFS_SERVER(inode
);
1775 struct nfs4_exception exception
= { };
1778 err
= nfs4_handle_exception(server
,
1779 _nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
),
1781 } while (exception
.retry
);
1785 struct nfs4_closedata
{
1787 struct inode
*inode
;
1788 struct nfs4_state
*state
;
1789 struct nfs_closeargs arg
;
1790 struct nfs_closeres res
;
1791 struct nfs_fattr fattr
;
1792 unsigned long timestamp
;
1795 static void nfs4_free_closedata(void *data
)
1797 struct nfs4_closedata
*calldata
= data
;
1798 struct nfs4_state_owner
*sp
= calldata
->state
->owner
;
1800 nfs4_put_open_state(calldata
->state
);
1801 nfs_free_seqid(calldata
->arg
.seqid
);
1802 nfs4_put_state_owner(sp
);
1803 path_put(&calldata
->path
);
1807 static void nfs4_close_clear_stateid_flags(struct nfs4_state
*state
,
1810 spin_lock(&state
->owner
->so_lock
);
1811 if (!(fmode
& FMODE_READ
))
1812 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1813 if (!(fmode
& FMODE_WRITE
))
1814 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1815 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1816 spin_unlock(&state
->owner
->so_lock
);
1819 static void nfs4_close_done(struct rpc_task
*task
, void *data
)
1821 struct nfs4_closedata
*calldata
= data
;
1822 struct nfs4_state
*state
= calldata
->state
;
1823 struct nfs_server
*server
= NFS_SERVER(calldata
->inode
);
1825 nfs4_sequence_done(server
, &calldata
->res
.seq_res
, task
->tk_status
);
1826 if (RPC_ASSASSINATED(task
))
1828 /* hmm. we are done with the inode, and in the process of freeing
1829 * the state_owner. we keep this around to process errors
1831 switch (task
->tk_status
) {
1833 nfs_set_open_stateid(state
, &calldata
->res
.stateid
, 0);
1834 renew_lease(server
, calldata
->timestamp
);
1835 nfs4_close_clear_stateid_flags(state
,
1836 calldata
->arg
.fmode
);
1838 case -NFS4ERR_STALE_STATEID
:
1839 case -NFS4ERR_OLD_STATEID
:
1840 case -NFS4ERR_BAD_STATEID
:
1841 case -NFS4ERR_EXPIRED
:
1842 if (calldata
->arg
.fmode
== 0)
1845 if (nfs4_async_handle_error(task
, server
, state
) == -EAGAIN
)
1846 rpc_restart_call_prepare(task
);
1848 nfs_release_seqid(calldata
->arg
.seqid
);
1849 nfs_refresh_inode(calldata
->inode
, calldata
->res
.fattr
);
1852 static void nfs4_close_prepare(struct rpc_task
*task
, void *data
)
1854 struct nfs4_closedata
*calldata
= data
;
1855 struct nfs4_state
*state
= calldata
->state
;
1858 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
1861 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_OPEN_DOWNGRADE
];
1862 calldata
->arg
.fmode
= FMODE_READ
|FMODE_WRITE
;
1863 spin_lock(&state
->owner
->so_lock
);
1864 /* Calculate the change in open mode */
1865 if (state
->n_rdwr
== 0) {
1866 if (state
->n_rdonly
== 0) {
1867 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1868 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1869 calldata
->arg
.fmode
&= ~FMODE_READ
;
1871 if (state
->n_wronly
== 0) {
1872 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1873 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1874 calldata
->arg
.fmode
&= ~FMODE_WRITE
;
1877 spin_unlock(&state
->owner
->so_lock
);
1880 /* Note: exit _without_ calling nfs4_close_done */
1881 task
->tk_action
= NULL
;
1885 if (calldata
->arg
.fmode
== 0)
1886 task
->tk_msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
];
1888 nfs_fattr_init(calldata
->res
.fattr
);
1889 calldata
->timestamp
= jiffies
;
1890 if (nfs4_setup_sequence((NFS_SERVER(calldata
->inode
))->nfs_client
,
1891 &calldata
->arg
.seq_args
, &calldata
->res
.seq_res
,
1894 rpc_call_start(task
);
1897 static const struct rpc_call_ops nfs4_close_ops
= {
1898 .rpc_call_prepare
= nfs4_close_prepare
,
1899 .rpc_call_done
= nfs4_close_done
,
1900 .rpc_release
= nfs4_free_closedata
,
1904 * It is possible for data to be read/written from a mem-mapped file
1905 * after the sys_close call (which hits the vfs layer as a flush).
1906 * This means that we can't safely call nfsv4 close on a file until
1907 * the inode is cleared. This in turn means that we are not good
1908 * NFSv4 citizens - we do not indicate to the server to update the file's
1909 * share state even when we are done with one of the three share
1910 * stateid's in the inode.
1912 * NOTE: Caller must be holding the sp->so_owner semaphore!
1914 int nfs4_do_close(struct path
*path
, struct nfs4_state
*state
, int wait
)
1916 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
1917 struct nfs4_closedata
*calldata
;
1918 struct nfs4_state_owner
*sp
= state
->owner
;
1919 struct rpc_task
*task
;
1920 struct rpc_message msg
= {
1921 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CLOSE
],
1922 .rpc_cred
= state
->owner
->so_cred
,
1924 struct rpc_task_setup task_setup_data
= {
1925 .rpc_client
= server
->client
,
1926 .rpc_message
= &msg
,
1927 .callback_ops
= &nfs4_close_ops
,
1928 .workqueue
= nfsiod_workqueue
,
1929 .flags
= RPC_TASK_ASYNC
,
1931 int status
= -ENOMEM
;
1933 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
1934 if (calldata
== NULL
)
1936 calldata
->inode
= state
->inode
;
1937 calldata
->state
= state
;
1938 calldata
->arg
.fh
= NFS_FH(state
->inode
);
1939 calldata
->arg
.stateid
= &state
->open_stateid
;
1940 /* Serialization for the sequence id */
1941 calldata
->arg
.seqid
= nfs_alloc_seqid(&state
->owner
->so_seqid
);
1942 if (calldata
->arg
.seqid
== NULL
)
1943 goto out_free_calldata
;
1944 calldata
->arg
.fmode
= 0;
1945 calldata
->arg
.bitmask
= server
->cache_consistency_bitmask
;
1946 calldata
->res
.fattr
= &calldata
->fattr
;
1947 calldata
->res
.seqid
= calldata
->arg
.seqid
;
1948 calldata
->res
.server
= server
;
1949 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
1951 calldata
->path
= *path
;
1953 msg
.rpc_argp
= &calldata
->arg
,
1954 msg
.rpc_resp
= &calldata
->res
,
1955 task_setup_data
.callback_data
= calldata
;
1956 task
= rpc_run_task(&task_setup_data
);
1958 return PTR_ERR(task
);
1961 status
= rpc_wait_for_completion_task(task
);
1967 nfs4_put_open_state(state
);
1968 nfs4_put_state_owner(sp
);
1972 static int nfs4_intent_set_file(struct nameidata
*nd
, struct path
*path
, struct nfs4_state
*state
, fmode_t fmode
)
1977 /* If the open_intent is for execute, we have an extra check to make */
1978 if (fmode
& FMODE_EXEC
) {
1979 ret
= nfs_may_open(state
->inode
,
1980 state
->owner
->so_cred
,
1981 nd
->intent
.open
.flags
);
1985 filp
= lookup_instantiate_filp(nd
, path
->dentry
, NULL
);
1986 if (!IS_ERR(filp
)) {
1987 struct nfs_open_context
*ctx
;
1988 ctx
= nfs_file_open_context(filp
);
1992 ret
= PTR_ERR(filp
);
1994 nfs4_close_sync(path
, state
, fmode
& (FMODE_READ
|FMODE_WRITE
));
1999 nfs4_atomic_open(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
2001 struct path path
= {
2002 .mnt
= nd
->path
.mnt
,
2005 struct dentry
*parent
;
2007 struct rpc_cred
*cred
;
2008 struct nfs4_state
*state
;
2010 fmode_t fmode
= nd
->intent
.open
.flags
& (FMODE_READ
| FMODE_WRITE
| FMODE_EXEC
);
2012 if (nd
->flags
& LOOKUP_CREATE
) {
2013 attr
.ia_mode
= nd
->intent
.open
.create_mode
;
2014 attr
.ia_valid
= ATTR_MODE
;
2015 if (!IS_POSIXACL(dir
))
2016 attr
.ia_mode
&= ~current_umask();
2019 BUG_ON(nd
->intent
.open
.flags
& O_CREAT
);
2022 cred
= rpc_lookup_cred();
2024 return (struct dentry
*)cred
;
2025 parent
= dentry
->d_parent
;
2026 /* Protect against concurrent sillydeletes */
2027 nfs_block_sillyrename(parent
);
2028 state
= nfs4_do_open(dir
, &path
, fmode
, nd
->intent
.open
.flags
, &attr
, cred
);
2030 if (IS_ERR(state
)) {
2031 if (PTR_ERR(state
) == -ENOENT
) {
2032 d_add(dentry
, NULL
);
2033 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2035 nfs_unblock_sillyrename(parent
);
2036 return (struct dentry
*)state
;
2038 res
= d_add_unique(dentry
, igrab(state
->inode
));
2041 nfs_set_verifier(path
.dentry
, nfs_save_change_attribute(dir
));
2042 nfs_unblock_sillyrename(parent
);
2043 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2048 nfs4_open_revalidate(struct inode
*dir
, struct dentry
*dentry
, int openflags
, struct nameidata
*nd
)
2050 struct path path
= {
2051 .mnt
= nd
->path
.mnt
,
2054 struct rpc_cred
*cred
;
2055 struct nfs4_state
*state
;
2056 fmode_t fmode
= openflags
& (FMODE_READ
| FMODE_WRITE
);
2058 cred
= rpc_lookup_cred();
2060 return PTR_ERR(cred
);
2061 state
= nfs4_do_open(dir
, &path
, fmode
, openflags
, NULL
, cred
);
2063 if (IS_ERR(state
)) {
2064 switch (PTR_ERR(state
)) {
2070 lookup_instantiate_filp(nd
, (struct dentry
*)state
, NULL
);
2076 if (state
->inode
== dentry
->d_inode
) {
2077 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2078 nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2081 nfs4_close_sync(&path
, state
, fmode
);
2087 static void nfs4_close_context(struct nfs_open_context
*ctx
, int is_sync
)
2089 if (ctx
->state
== NULL
)
2092 nfs4_close_sync(&ctx
->path
, ctx
->state
, ctx
->mode
);
2094 nfs4_close_state(&ctx
->path
, ctx
->state
, ctx
->mode
);
2097 static int _nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2099 struct nfs4_server_caps_arg args
= {
2102 struct nfs4_server_caps_res res
= {};
2103 struct rpc_message msg
= {
2104 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SERVER_CAPS
],
2110 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2112 memcpy(server
->attr_bitmask
, res
.attr_bitmask
, sizeof(server
->attr_bitmask
));
2113 server
->caps
&= ~(NFS_CAP_ACLS
|NFS_CAP_HARDLINKS
|
2114 NFS_CAP_SYMLINKS
|NFS_CAP_FILEID
|
2115 NFS_CAP_MODE
|NFS_CAP_NLINK
|NFS_CAP_OWNER
|
2116 NFS_CAP_OWNER_GROUP
|NFS_CAP_ATIME
|
2117 NFS_CAP_CTIME
|NFS_CAP_MTIME
);
2118 if (res
.attr_bitmask
[0] & FATTR4_WORD0_ACL
)
2119 server
->caps
|= NFS_CAP_ACLS
;
2120 if (res
.has_links
!= 0)
2121 server
->caps
|= NFS_CAP_HARDLINKS
;
2122 if (res
.has_symlinks
!= 0)
2123 server
->caps
|= NFS_CAP_SYMLINKS
;
2124 if (res
.attr_bitmask
[0] & FATTR4_WORD0_FILEID
)
2125 server
->caps
|= NFS_CAP_FILEID
;
2126 if (res
.attr_bitmask
[1] & FATTR4_WORD1_MODE
)
2127 server
->caps
|= NFS_CAP_MODE
;
2128 if (res
.attr_bitmask
[1] & FATTR4_WORD1_NUMLINKS
)
2129 server
->caps
|= NFS_CAP_NLINK
;
2130 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER
)
2131 server
->caps
|= NFS_CAP_OWNER
;
2132 if (res
.attr_bitmask
[1] & FATTR4_WORD1_OWNER_GROUP
)
2133 server
->caps
|= NFS_CAP_OWNER_GROUP
;
2134 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_ACCESS
)
2135 server
->caps
|= NFS_CAP_ATIME
;
2136 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_METADATA
)
2137 server
->caps
|= NFS_CAP_CTIME
;
2138 if (res
.attr_bitmask
[1] & FATTR4_WORD1_TIME_MODIFY
)
2139 server
->caps
|= NFS_CAP_MTIME
;
2141 memcpy(server
->cache_consistency_bitmask
, res
.attr_bitmask
, sizeof(server
->cache_consistency_bitmask
));
2142 server
->cache_consistency_bitmask
[0] &= FATTR4_WORD0_CHANGE
|FATTR4_WORD0_SIZE
;
2143 server
->cache_consistency_bitmask
[1] &= FATTR4_WORD1_TIME_METADATA
|FATTR4_WORD1_TIME_MODIFY
;
2144 server
->acl_bitmask
= res
.acl_bitmask
;
2150 int nfs4_server_capabilities(struct nfs_server
*server
, struct nfs_fh
*fhandle
)
2152 struct nfs4_exception exception
= { };
2155 err
= nfs4_handle_exception(server
,
2156 _nfs4_server_capabilities(server
, fhandle
),
2158 } while (exception
.retry
);
2162 static int _nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2163 struct nfs_fsinfo
*info
)
2165 struct nfs4_lookup_root_arg args
= {
2166 .bitmask
= nfs4_fattr_bitmap
,
2168 struct nfs4_lookup_res res
= {
2170 .fattr
= info
->fattr
,
2173 struct rpc_message msg
= {
2174 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP_ROOT
],
2179 nfs_fattr_init(info
->fattr
);
2180 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2183 static int nfs4_lookup_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2184 struct nfs_fsinfo
*info
)
2186 struct nfs4_exception exception
= { };
2189 err
= nfs4_handle_exception(server
,
2190 _nfs4_lookup_root(server
, fhandle
, info
),
2192 } while (exception
.retry
);
2197 * get the file handle for the "/" directory on the server
2199 static int nfs4_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2200 struct nfs_fsinfo
*info
)
2204 status
= nfs4_lookup_root(server
, fhandle
, info
);
2206 status
= nfs4_server_capabilities(server
, fhandle
);
2208 status
= nfs4_do_fsinfo(server
, fhandle
, info
);
2209 return nfs4_map_errors(status
);
2213 * Get locations and (maybe) other attributes of a referral.
2214 * Note that we'll actually follow the referral later when
2215 * we detect fsid mismatch in inode revalidation
2217 static int nfs4_get_referral(struct inode
*dir
, const struct qstr
*name
, struct nfs_fattr
*fattr
, struct nfs_fh
*fhandle
)
2219 int status
= -ENOMEM
;
2220 struct page
*page
= NULL
;
2221 struct nfs4_fs_locations
*locations
= NULL
;
2223 page
= alloc_page(GFP_KERNEL
);
2226 locations
= kmalloc(sizeof(struct nfs4_fs_locations
), GFP_KERNEL
);
2227 if (locations
== NULL
)
2230 status
= nfs4_proc_fs_locations(dir
, name
, locations
, page
);
2233 /* Make sure server returned a different fsid for the referral */
2234 if (nfs_fsid_equal(&NFS_SERVER(dir
)->fsid
, &locations
->fattr
.fsid
)) {
2235 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__
, name
->name
);
2240 memcpy(fattr
, &locations
->fattr
, sizeof(struct nfs_fattr
));
2241 fattr
->valid
|= NFS_ATTR_FATTR_V4_REFERRAL
;
2243 fattr
->mode
= S_IFDIR
;
2244 memset(fhandle
, 0, sizeof(struct nfs_fh
));
2253 static int _nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2255 struct nfs4_getattr_arg args
= {
2257 .bitmask
= server
->attr_bitmask
,
2259 struct nfs4_getattr_res res
= {
2263 struct rpc_message msg
= {
2264 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETATTR
],
2269 nfs_fattr_init(fattr
);
2270 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2273 static int nfs4_proc_getattr(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2275 struct nfs4_exception exception
= { };
2278 err
= nfs4_handle_exception(server
,
2279 _nfs4_proc_getattr(server
, fhandle
, fattr
),
2281 } while (exception
.retry
);
2286 * The file is not closed if it is opened due to the a request to change
2287 * the size of the file. The open call will not be needed once the
2288 * VFS layer lookup-intents are implemented.
2290 * Close is called when the inode is destroyed.
2291 * If we haven't opened the file for O_WRONLY, we
2292 * need to in the size_change case to obtain a stateid.
2295 * Because OPEN is always done by name in nfsv4, it is
2296 * possible that we opened a different file by the same
2297 * name. We can recognize this race condition, but we
2298 * can't do anything about it besides returning an error.
2300 * This will be fixed with VFS changes (lookup-intent).
2303 nfs4_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
2304 struct iattr
*sattr
)
2306 struct inode
*inode
= dentry
->d_inode
;
2307 struct rpc_cred
*cred
= NULL
;
2308 struct nfs4_state
*state
= NULL
;
2311 nfs_fattr_init(fattr
);
2313 /* Search for an existing open(O_WRITE) file */
2314 if (sattr
->ia_valid
& ATTR_FILE
) {
2315 struct nfs_open_context
*ctx
;
2317 ctx
= nfs_file_open_context(sattr
->ia_file
);
2324 status
= nfs4_do_setattr(inode
, cred
, fattr
, sattr
, state
);
2326 nfs_setattr_update_inode(inode
, sattr
);
2330 static int _nfs4_proc_lookupfh(struct nfs_server
*server
, const struct nfs_fh
*dirfh
,
2331 const struct qstr
*name
, struct nfs_fh
*fhandle
,
2332 struct nfs_fattr
*fattr
)
2335 struct nfs4_lookup_arg args
= {
2336 .bitmask
= server
->attr_bitmask
,
2340 struct nfs4_lookup_res res
= {
2345 struct rpc_message msg
= {
2346 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOOKUP
],
2351 nfs_fattr_init(fattr
);
2353 dprintk("NFS call lookupfh %s\n", name
->name
);
2354 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2355 dprintk("NFS reply lookupfh: %d\n", status
);
2359 static int nfs4_proc_lookupfh(struct nfs_server
*server
, struct nfs_fh
*dirfh
,
2360 struct qstr
*name
, struct nfs_fh
*fhandle
,
2361 struct nfs_fattr
*fattr
)
2363 struct nfs4_exception exception
= { };
2366 err
= _nfs4_proc_lookupfh(server
, dirfh
, name
, fhandle
, fattr
);
2368 if (err
== -NFS4ERR_MOVED
) {
2372 err
= nfs4_handle_exception(server
, err
, &exception
);
2373 } while (exception
.retry
);
2377 static int _nfs4_proc_lookup(struct inode
*dir
, const struct qstr
*name
,
2378 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2382 dprintk("NFS call lookup %s\n", name
->name
);
2383 status
= _nfs4_proc_lookupfh(NFS_SERVER(dir
), NFS_FH(dir
), name
, fhandle
, fattr
);
2384 if (status
== -NFS4ERR_MOVED
)
2385 status
= nfs4_get_referral(dir
, name
, fattr
, fhandle
);
2386 dprintk("NFS reply lookup: %d\n", status
);
2390 static int nfs4_proc_lookup(struct inode
*dir
, struct qstr
*name
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
2392 struct nfs4_exception exception
= { };
2395 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2396 _nfs4_proc_lookup(dir
, name
, fhandle
, fattr
),
2398 } while (exception
.retry
);
2402 static int _nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2404 struct nfs_server
*server
= NFS_SERVER(inode
);
2405 struct nfs_fattr fattr
;
2406 struct nfs4_accessargs args
= {
2407 .fh
= NFS_FH(inode
),
2408 .bitmask
= server
->attr_bitmask
,
2410 struct nfs4_accessres res
= {
2414 struct rpc_message msg
= {
2415 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_ACCESS
],
2418 .rpc_cred
= entry
->cred
,
2420 int mode
= entry
->mask
;
2424 * Determine which access bits we want to ask for...
2426 if (mode
& MAY_READ
)
2427 args
.access
|= NFS4_ACCESS_READ
;
2428 if (S_ISDIR(inode
->i_mode
)) {
2429 if (mode
& MAY_WRITE
)
2430 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
;
2431 if (mode
& MAY_EXEC
)
2432 args
.access
|= NFS4_ACCESS_LOOKUP
;
2434 if (mode
& MAY_WRITE
)
2435 args
.access
|= NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
;
2436 if (mode
& MAY_EXEC
)
2437 args
.access
|= NFS4_ACCESS_EXECUTE
;
2439 nfs_fattr_init(&fattr
);
2440 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2443 if (res
.access
& NFS4_ACCESS_READ
)
2444 entry
->mask
|= MAY_READ
;
2445 if (res
.access
& (NFS4_ACCESS_MODIFY
| NFS4_ACCESS_EXTEND
| NFS4_ACCESS_DELETE
))
2446 entry
->mask
|= MAY_WRITE
;
2447 if (res
.access
& (NFS4_ACCESS_LOOKUP
|NFS4_ACCESS_EXECUTE
))
2448 entry
->mask
|= MAY_EXEC
;
2449 nfs_refresh_inode(inode
, &fattr
);
2454 static int nfs4_proc_access(struct inode
*inode
, struct nfs_access_entry
*entry
)
2456 struct nfs4_exception exception
= { };
2459 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2460 _nfs4_proc_access(inode
, entry
),
2462 } while (exception
.retry
);
2467 * TODO: For the time being, we don't try to get any attributes
2468 * along with any of the zero-copy operations READ, READDIR,
2471 * In the case of the first three, we want to put the GETATTR
2472 * after the read-type operation -- this is because it is hard
2473 * to predict the length of a GETATTR response in v4, and thus
2474 * align the READ data correctly. This means that the GETATTR
2475 * may end up partially falling into the page cache, and we should
2476 * shift it into the 'tail' of the xdr_buf before processing.
2477 * To do this efficiently, we need to know the total length
2478 * of data received, which doesn't seem to be available outside
2481 * In the case of WRITE, we also want to put the GETATTR after
2482 * the operation -- in this case because we want to make sure
2483 * we get the post-operation mtime and size. This means that
2484 * we can't use xdr_encode_pages() as written: we need a variant
2485 * of it which would leave room in the 'tail' iovec.
2487 * Both of these changes to the XDR layer would in fact be quite
2488 * minor, but I decided to leave them for a subsequent patch.
2490 static int _nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2491 unsigned int pgbase
, unsigned int pglen
)
2493 struct nfs4_readlink args
= {
2494 .fh
= NFS_FH(inode
),
2499 struct nfs4_readlink_res res
;
2500 struct rpc_message msg
= {
2501 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READLINK
],
2506 return nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
2509 static int nfs4_proc_readlink(struct inode
*inode
, struct page
*page
,
2510 unsigned int pgbase
, unsigned int pglen
)
2512 struct nfs4_exception exception
= { };
2515 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2516 _nfs4_proc_readlink(inode
, page
, pgbase
, pglen
),
2518 } while (exception
.retry
);
2524 * We will need to arrange for the VFS layer to provide an atomic open.
2525 * Until then, this create/open method is prone to inefficiency and race
2526 * conditions due to the lookup, create, and open VFS calls from sys_open()
2527 * placed on the wire.
2529 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2530 * The file will be opened again in the subsequent VFS open call
2531 * (nfs4_proc_file_open).
2533 * The open for read will just hang around to be used by any process that
2534 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2538 nfs4_proc_create(struct inode
*dir
, struct dentry
*dentry
, struct iattr
*sattr
,
2539 int flags
, struct nameidata
*nd
)
2541 struct path path
= {
2542 .mnt
= nd
->path
.mnt
,
2545 struct nfs4_state
*state
;
2546 struct rpc_cred
*cred
;
2547 fmode_t fmode
= flags
& (FMODE_READ
| FMODE_WRITE
);
2550 cred
= rpc_lookup_cred();
2552 status
= PTR_ERR(cred
);
2555 state
= nfs4_do_open(dir
, &path
, fmode
, flags
, sattr
, cred
);
2557 if (IS_ERR(state
)) {
2558 status
= PTR_ERR(state
);
2561 d_add(dentry
, igrab(state
->inode
));
2562 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
2563 if (flags
& O_EXCL
) {
2564 struct nfs_fattr fattr
;
2565 status
= nfs4_do_setattr(state
->inode
, cred
, &fattr
, sattr
, state
);
2567 nfs_setattr_update_inode(state
->inode
, sattr
);
2568 nfs_post_op_update_inode(state
->inode
, &fattr
);
2570 if (status
== 0 && (nd
->flags
& LOOKUP_OPEN
) != 0)
2571 status
= nfs4_intent_set_file(nd
, &path
, state
, fmode
);
2573 nfs4_close_sync(&path
, state
, fmode
);
2580 static int _nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2582 struct nfs_server
*server
= NFS_SERVER(dir
);
2583 struct nfs_removeargs args
= {
2585 .name
.len
= name
->len
,
2586 .name
.name
= name
->name
,
2587 .bitmask
= server
->attr_bitmask
,
2589 struct nfs_removeres res
= {
2592 struct rpc_message msg
= {
2593 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
],
2599 nfs_fattr_init(&res
.dir_attr
);
2600 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 1);
2602 update_changeattr(dir
, &res
.cinfo
);
2603 nfs_post_op_update_inode(dir
, &res
.dir_attr
);
2608 static int nfs4_proc_remove(struct inode
*dir
, struct qstr
*name
)
2610 struct nfs4_exception exception
= { };
2613 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2614 _nfs4_proc_remove(dir
, name
),
2616 } while (exception
.retry
);
2620 static void nfs4_proc_unlink_setup(struct rpc_message
*msg
, struct inode
*dir
)
2622 struct nfs_server
*server
= NFS_SERVER(dir
);
2623 struct nfs_removeargs
*args
= msg
->rpc_argp
;
2624 struct nfs_removeres
*res
= msg
->rpc_resp
;
2626 args
->bitmask
= server
->cache_consistency_bitmask
;
2627 res
->server
= server
;
2628 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_REMOVE
];
2631 static int nfs4_proc_unlink_done(struct rpc_task
*task
, struct inode
*dir
)
2633 struct nfs_removeres
*res
= task
->tk_msg
.rpc_resp
;
2635 nfs4_sequence_done(res
->server
, &res
->seq_res
, task
->tk_status
);
2636 if (nfs4_async_handle_error(task
, res
->server
, NULL
) == -EAGAIN
)
2638 update_changeattr(dir
, &res
->cinfo
);
2639 nfs_post_op_update_inode(dir
, &res
->dir_attr
);
2643 static int _nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2644 struct inode
*new_dir
, struct qstr
*new_name
)
2646 struct nfs_server
*server
= NFS_SERVER(old_dir
);
2647 struct nfs4_rename_arg arg
= {
2648 .old_dir
= NFS_FH(old_dir
),
2649 .new_dir
= NFS_FH(new_dir
),
2650 .old_name
= old_name
,
2651 .new_name
= new_name
,
2652 .bitmask
= server
->attr_bitmask
,
2654 struct nfs_fattr old_fattr
, new_fattr
;
2655 struct nfs4_rename_res res
= {
2657 .old_fattr
= &old_fattr
,
2658 .new_fattr
= &new_fattr
,
2660 struct rpc_message msg
= {
2661 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENAME
],
2667 nfs_fattr_init(res
.old_fattr
);
2668 nfs_fattr_init(res
.new_fattr
);
2669 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2672 update_changeattr(old_dir
, &res
.old_cinfo
);
2673 nfs_post_op_update_inode(old_dir
, res
.old_fattr
);
2674 update_changeattr(new_dir
, &res
.new_cinfo
);
2675 nfs_post_op_update_inode(new_dir
, res
.new_fattr
);
2680 static int nfs4_proc_rename(struct inode
*old_dir
, struct qstr
*old_name
,
2681 struct inode
*new_dir
, struct qstr
*new_name
)
2683 struct nfs4_exception exception
= { };
2686 err
= nfs4_handle_exception(NFS_SERVER(old_dir
),
2687 _nfs4_proc_rename(old_dir
, old_name
,
2690 } while (exception
.retry
);
2694 static int _nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2696 struct nfs_server
*server
= NFS_SERVER(inode
);
2697 struct nfs4_link_arg arg
= {
2698 .fh
= NFS_FH(inode
),
2699 .dir_fh
= NFS_FH(dir
),
2701 .bitmask
= server
->attr_bitmask
,
2703 struct nfs_fattr fattr
, dir_attr
;
2704 struct nfs4_link_res res
= {
2707 .dir_attr
= &dir_attr
,
2709 struct rpc_message msg
= {
2710 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LINK
],
2716 nfs_fattr_init(res
.fattr
);
2717 nfs_fattr_init(res
.dir_attr
);
2718 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
2720 update_changeattr(dir
, &res
.cinfo
);
2721 nfs_post_op_update_inode(dir
, res
.dir_attr
);
2722 nfs_post_op_update_inode(inode
, res
.fattr
);
2728 static int nfs4_proc_link(struct inode
*inode
, struct inode
*dir
, struct qstr
*name
)
2730 struct nfs4_exception exception
= { };
2733 err
= nfs4_handle_exception(NFS_SERVER(inode
),
2734 _nfs4_proc_link(inode
, dir
, name
),
2736 } while (exception
.retry
);
2740 struct nfs4_createdata
{
2741 struct rpc_message msg
;
2742 struct nfs4_create_arg arg
;
2743 struct nfs4_create_res res
;
2745 struct nfs_fattr fattr
;
2746 struct nfs_fattr dir_fattr
;
2749 static struct nfs4_createdata
*nfs4_alloc_createdata(struct inode
*dir
,
2750 struct qstr
*name
, struct iattr
*sattr
, u32 ftype
)
2752 struct nfs4_createdata
*data
;
2754 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2756 struct nfs_server
*server
= NFS_SERVER(dir
);
2758 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE
];
2759 data
->msg
.rpc_argp
= &data
->arg
;
2760 data
->msg
.rpc_resp
= &data
->res
;
2761 data
->arg
.dir_fh
= NFS_FH(dir
);
2762 data
->arg
.server
= server
;
2763 data
->arg
.name
= name
;
2764 data
->arg
.attrs
= sattr
;
2765 data
->arg
.ftype
= ftype
;
2766 data
->arg
.bitmask
= server
->attr_bitmask
;
2767 data
->res
.server
= server
;
2768 data
->res
.fh
= &data
->fh
;
2769 data
->res
.fattr
= &data
->fattr
;
2770 data
->res
.dir_fattr
= &data
->dir_fattr
;
2771 nfs_fattr_init(data
->res
.fattr
);
2772 nfs_fattr_init(data
->res
.dir_fattr
);
2777 static int nfs4_do_create(struct inode
*dir
, struct dentry
*dentry
, struct nfs4_createdata
*data
)
2779 int status
= nfs4_call_sync(NFS_SERVER(dir
), &data
->msg
,
2780 &data
->arg
, &data
->res
, 1);
2782 update_changeattr(dir
, &data
->res
.dir_cinfo
);
2783 nfs_post_op_update_inode(dir
, data
->res
.dir_fattr
);
2784 status
= nfs_instantiate(dentry
, data
->res
.fh
, data
->res
.fattr
);
2789 static void nfs4_free_createdata(struct nfs4_createdata
*data
)
2794 static int _nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2795 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2797 struct nfs4_createdata
*data
;
2798 int status
= -ENAMETOOLONG
;
2800 if (len
> NFS4_MAXPATHLEN
)
2804 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4LNK
);
2808 data
->msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SYMLINK
];
2809 data
->arg
.u
.symlink
.pages
= &page
;
2810 data
->arg
.u
.symlink
.len
= len
;
2812 status
= nfs4_do_create(dir
, dentry
, data
);
2814 nfs4_free_createdata(data
);
2819 static int nfs4_proc_symlink(struct inode
*dir
, struct dentry
*dentry
,
2820 struct page
*page
, unsigned int len
, struct iattr
*sattr
)
2822 struct nfs4_exception exception
= { };
2825 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2826 _nfs4_proc_symlink(dir
, dentry
, page
,
2829 } while (exception
.retry
);
2833 static int _nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2834 struct iattr
*sattr
)
2836 struct nfs4_createdata
*data
;
2837 int status
= -ENOMEM
;
2839 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4DIR
);
2843 status
= nfs4_do_create(dir
, dentry
, data
);
2845 nfs4_free_createdata(data
);
2850 static int nfs4_proc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
2851 struct iattr
*sattr
)
2853 struct nfs4_exception exception
= { };
2856 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2857 _nfs4_proc_mkdir(dir
, dentry
, sattr
),
2859 } while (exception
.retry
);
2863 static int _nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2864 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2866 struct inode
*dir
= dentry
->d_inode
;
2867 struct nfs4_readdir_arg args
= {
2872 .bitmask
= NFS_SERVER(dentry
->d_inode
)->attr_bitmask
,
2874 struct nfs4_readdir_res res
;
2875 struct rpc_message msg
= {
2876 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READDIR
],
2883 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__
,
2884 dentry
->d_parent
->d_name
.name
,
2885 dentry
->d_name
.name
,
2886 (unsigned long long)cookie
);
2887 nfs4_setup_readdir(cookie
, NFS_COOKIEVERF(dir
), dentry
, &args
);
2888 res
.pgbase
= args
.pgbase
;
2889 status
= nfs4_call_sync(NFS_SERVER(dir
), &msg
, &args
, &res
, 0);
2891 memcpy(NFS_COOKIEVERF(dir
), res
.verifier
.data
, NFS4_VERIFIER_SIZE
);
2893 nfs_invalidate_atime(dir
);
2895 dprintk("%s: returns %d\n", __func__
, status
);
2899 static int nfs4_proc_readdir(struct dentry
*dentry
, struct rpc_cred
*cred
,
2900 u64 cookie
, struct page
*page
, unsigned int count
, int plus
)
2902 struct nfs4_exception exception
= { };
2905 err
= nfs4_handle_exception(NFS_SERVER(dentry
->d_inode
),
2906 _nfs4_proc_readdir(dentry
, cred
, cookie
,
2909 } while (exception
.retry
);
2913 static int _nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2914 struct iattr
*sattr
, dev_t rdev
)
2916 struct nfs4_createdata
*data
;
2917 int mode
= sattr
->ia_mode
;
2918 int status
= -ENOMEM
;
2920 BUG_ON(!(sattr
->ia_valid
& ATTR_MODE
));
2921 BUG_ON(!S_ISFIFO(mode
) && !S_ISBLK(mode
) && !S_ISCHR(mode
) && !S_ISSOCK(mode
));
2923 data
= nfs4_alloc_createdata(dir
, &dentry
->d_name
, sattr
, NF4SOCK
);
2928 data
->arg
.ftype
= NF4FIFO
;
2929 else if (S_ISBLK(mode
)) {
2930 data
->arg
.ftype
= NF4BLK
;
2931 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2932 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2934 else if (S_ISCHR(mode
)) {
2935 data
->arg
.ftype
= NF4CHR
;
2936 data
->arg
.u
.device
.specdata1
= MAJOR(rdev
);
2937 data
->arg
.u
.device
.specdata2
= MINOR(rdev
);
2940 status
= nfs4_do_create(dir
, dentry
, data
);
2942 nfs4_free_createdata(data
);
2947 static int nfs4_proc_mknod(struct inode
*dir
, struct dentry
*dentry
,
2948 struct iattr
*sattr
, dev_t rdev
)
2950 struct nfs4_exception exception
= { };
2953 err
= nfs4_handle_exception(NFS_SERVER(dir
),
2954 _nfs4_proc_mknod(dir
, dentry
, sattr
, rdev
),
2956 } while (exception
.retry
);
2960 static int _nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2961 struct nfs_fsstat
*fsstat
)
2963 struct nfs4_statfs_arg args
= {
2965 .bitmask
= server
->attr_bitmask
,
2967 struct nfs4_statfs_res res
= {
2970 struct rpc_message msg
= {
2971 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_STATFS
],
2976 nfs_fattr_init(fsstat
->fattr
);
2977 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
2980 static int nfs4_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsstat
*fsstat
)
2982 struct nfs4_exception exception
= { };
2985 err
= nfs4_handle_exception(server
,
2986 _nfs4_proc_statfs(server
, fhandle
, fsstat
),
2988 } while (exception
.retry
);
2992 static int _nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
2993 struct nfs_fsinfo
*fsinfo
)
2995 struct nfs4_fsinfo_arg args
= {
2997 .bitmask
= server
->attr_bitmask
,
2999 struct nfs4_fsinfo_res res
= {
3002 struct rpc_message msg
= {
3003 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FSINFO
],
3008 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3011 static int nfs4_do_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3013 struct nfs4_exception exception
= { };
3017 err
= nfs4_handle_exception(server
,
3018 _nfs4_do_fsinfo(server
, fhandle
, fsinfo
),
3020 } while (exception
.retry
);
3024 static int nfs4_proc_fsinfo(struct nfs_server
*server
, struct nfs_fh
*fhandle
, struct nfs_fsinfo
*fsinfo
)
3026 nfs_fattr_init(fsinfo
->fattr
);
3027 return nfs4_do_fsinfo(server
, fhandle
, fsinfo
);
3030 static int _nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3031 struct nfs_pathconf
*pathconf
)
3033 struct nfs4_pathconf_arg args
= {
3035 .bitmask
= server
->attr_bitmask
,
3037 struct nfs4_pathconf_res res
= {
3038 .pathconf
= pathconf
,
3040 struct rpc_message msg
= {
3041 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_PATHCONF
],
3046 /* None of the pathconf attributes are mandatory to implement */
3047 if ((args
.bitmask
[0] & nfs4_pathconf_bitmap
[0]) == 0) {
3048 memset(pathconf
, 0, sizeof(*pathconf
));
3052 nfs_fattr_init(pathconf
->fattr
);
3053 return nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
3056 static int nfs4_proc_pathconf(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
3057 struct nfs_pathconf
*pathconf
)
3059 struct nfs4_exception exception
= { };
3063 err
= nfs4_handle_exception(server
,
3064 _nfs4_proc_pathconf(server
, fhandle
, pathconf
),
3066 } while (exception
.retry
);
3070 static int nfs4_read_done(struct rpc_task
*task
, struct nfs_read_data
*data
)
3072 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3074 dprintk("--> %s\n", __func__
);
3076 nfs4_sequence_done(server
, &data
->res
.seq_res
, task
->tk_status
);
3078 if (nfs4_async_handle_error(task
, server
, data
->args
.context
->state
) == -EAGAIN
) {
3079 nfs_restart_rpc(task
, server
->nfs_client
);
3083 nfs_invalidate_atime(data
->inode
);
3084 if (task
->tk_status
> 0)
3085 renew_lease(server
, data
->timestamp
);
3089 static void nfs4_proc_read_setup(struct nfs_read_data
*data
, struct rpc_message
*msg
)
3091 data
->timestamp
= jiffies
;
3092 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_READ
];
3095 static int nfs4_write_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3097 struct inode
*inode
= data
->inode
;
3099 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3102 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), data
->args
.context
->state
) == -EAGAIN
) {
3103 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3106 if (task
->tk_status
>= 0) {
3107 renew_lease(NFS_SERVER(inode
), data
->timestamp
);
3108 nfs_post_op_update_inode_force_wcc(inode
, data
->res
.fattr
);
3113 static void nfs4_proc_write_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3115 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3117 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3118 data
->res
.server
= server
;
3119 data
->timestamp
= jiffies
;
3121 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_WRITE
];
3124 static int nfs4_commit_done(struct rpc_task
*task
, struct nfs_write_data
*data
)
3126 struct inode
*inode
= data
->inode
;
3128 nfs4_sequence_done(NFS_SERVER(inode
), &data
->res
.seq_res
,
3130 if (nfs4_async_handle_error(task
, NFS_SERVER(inode
), NULL
) == -EAGAIN
) {
3131 nfs_restart_rpc(task
, NFS_SERVER(inode
)->nfs_client
);
3134 nfs_refresh_inode(inode
, data
->res
.fattr
);
3138 static void nfs4_proc_commit_setup(struct nfs_write_data
*data
, struct rpc_message
*msg
)
3140 struct nfs_server
*server
= NFS_SERVER(data
->inode
);
3142 data
->args
.bitmask
= server
->cache_consistency_bitmask
;
3143 data
->res
.server
= server
;
3144 msg
->rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_COMMIT
];
3148 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3149 * standalone procedure for queueing an asynchronous RENEW.
3151 static void nfs4_renew_release(void *data
)
3153 struct nfs_client
*clp
= data
;
3155 if (atomic_read(&clp
->cl_count
) > 1)
3156 nfs4_schedule_state_renewal(clp
);
3157 nfs_put_client(clp
);
3160 static void nfs4_renew_done(struct rpc_task
*task
, void *data
)
3162 struct nfs_client
*clp
= data
;
3163 unsigned long timestamp
= task
->tk_start
;
3165 if (task
->tk_status
< 0) {
3166 /* Unless we're shutting down, schedule state recovery! */
3167 if (test_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
) != 0)
3168 nfs4_schedule_state_recovery(clp
);
3171 spin_lock(&clp
->cl_lock
);
3172 if (time_before(clp
->cl_last_renewal
,timestamp
))
3173 clp
->cl_last_renewal
= timestamp
;
3174 spin_unlock(&clp
->cl_lock
);
3177 static const struct rpc_call_ops nfs4_renew_ops
= {
3178 .rpc_call_done
= nfs4_renew_done
,
3179 .rpc_release
= nfs4_renew_release
,
3182 int nfs4_proc_async_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3184 struct rpc_message msg
= {
3185 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3190 if (!atomic_inc_not_zero(&clp
->cl_count
))
3192 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
3193 &nfs4_renew_ops
, clp
);
3196 int nfs4_proc_renew(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3198 struct rpc_message msg
= {
3199 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RENEW
],
3203 unsigned long now
= jiffies
;
3206 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3209 spin_lock(&clp
->cl_lock
);
3210 if (time_before(clp
->cl_last_renewal
,now
))
3211 clp
->cl_last_renewal
= now
;
3212 spin_unlock(&clp
->cl_lock
);
3216 static inline int nfs4_server_supports_acls(struct nfs_server
*server
)
3218 return (server
->caps
& NFS_CAP_ACLS
)
3219 && (server
->acl_bitmask
& ACL4_SUPPORT_ALLOW_ACL
)
3220 && (server
->acl_bitmask
& ACL4_SUPPORT_DENY_ACL
);
3223 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3224 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3227 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3229 static void buf_to_pages(const void *buf
, size_t buflen
,
3230 struct page
**pages
, unsigned int *pgbase
)
3232 const void *p
= buf
;
3234 *pgbase
= offset_in_page(buf
);
3236 while (p
< buf
+ buflen
) {
3237 *(pages
++) = virt_to_page(p
);
3238 p
+= PAGE_CACHE_SIZE
;
3242 struct nfs4_cached_acl
{
3248 static void nfs4_set_cached_acl(struct inode
*inode
, struct nfs4_cached_acl
*acl
)
3250 struct nfs_inode
*nfsi
= NFS_I(inode
);
3252 spin_lock(&inode
->i_lock
);
3253 kfree(nfsi
->nfs4_acl
);
3254 nfsi
->nfs4_acl
= acl
;
3255 spin_unlock(&inode
->i_lock
);
3258 static void nfs4_zap_acl_attr(struct inode
*inode
)
3260 nfs4_set_cached_acl(inode
, NULL
);
3263 static inline ssize_t
nfs4_read_cached_acl(struct inode
*inode
, char *buf
, size_t buflen
)
3265 struct nfs_inode
*nfsi
= NFS_I(inode
);
3266 struct nfs4_cached_acl
*acl
;
3269 spin_lock(&inode
->i_lock
);
3270 acl
= nfsi
->nfs4_acl
;
3273 if (buf
== NULL
) /* user is just asking for length */
3275 if (acl
->cached
== 0)
3277 ret
= -ERANGE
; /* see getxattr(2) man page */
3278 if (acl
->len
> buflen
)
3280 memcpy(buf
, acl
->data
, acl
->len
);
3284 spin_unlock(&inode
->i_lock
);
3288 static void nfs4_write_cached_acl(struct inode
*inode
, const char *buf
, size_t acl_len
)
3290 struct nfs4_cached_acl
*acl
;
3292 if (buf
&& acl_len
<= PAGE_SIZE
) {
3293 acl
= kmalloc(sizeof(*acl
) + acl_len
, GFP_KERNEL
);
3297 memcpy(acl
->data
, buf
, acl_len
);
3299 acl
= kmalloc(sizeof(*acl
), GFP_KERNEL
);
3306 nfs4_set_cached_acl(inode
, acl
);
3309 static ssize_t
__nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3311 struct page
*pages
[NFS4ACL_MAXPAGES
];
3312 struct nfs_getaclargs args
= {
3313 .fh
= NFS_FH(inode
),
3317 struct nfs_getaclres res
= {
3321 struct rpc_message msg
= {
3322 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GETACL
],
3326 struct page
*localpage
= NULL
;
3329 if (buflen
< PAGE_SIZE
) {
3330 /* As long as we're doing a round trip to the server anyway,
3331 * let's be prepared for a page of acl data. */
3332 localpage
= alloc_page(GFP_KERNEL
);
3333 resp_buf
= page_address(localpage
);
3334 if (localpage
== NULL
)
3336 args
.acl_pages
[0] = localpage
;
3337 args
.acl_pgbase
= 0;
3338 args
.acl_len
= PAGE_SIZE
;
3341 buf_to_pages(buf
, buflen
, args
.acl_pages
, &args
.acl_pgbase
);
3343 ret
= nfs4_call_sync(NFS_SERVER(inode
), &msg
, &args
, &res
, 0);
3346 if (res
.acl_len
> args
.acl_len
)
3347 nfs4_write_cached_acl(inode
, NULL
, res
.acl_len
);
3349 nfs4_write_cached_acl(inode
, resp_buf
, res
.acl_len
);
3352 if (res
.acl_len
> buflen
)
3355 memcpy(buf
, resp_buf
, res
.acl_len
);
3360 __free_page(localpage
);
3364 static ssize_t
nfs4_get_acl_uncached(struct inode
*inode
, void *buf
, size_t buflen
)
3366 struct nfs4_exception exception
= { };
3369 ret
= __nfs4_get_acl_uncached(inode
, buf
, buflen
);
3372 ret
= nfs4_handle_exception(NFS_SERVER(inode
), ret
, &exception
);
3373 } while (exception
.retry
);
3377 static ssize_t
nfs4_proc_get_acl(struct inode
*inode
, void *buf
, size_t buflen
)
3379 struct nfs_server
*server
= NFS_SERVER(inode
);
3382 if (!nfs4_server_supports_acls(server
))
3384 ret
= nfs_revalidate_inode(server
, inode
);
3387 ret
= nfs4_read_cached_acl(inode
, buf
, buflen
);
3390 return nfs4_get_acl_uncached(inode
, buf
, buflen
);
3393 static int __nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3395 struct nfs_server
*server
= NFS_SERVER(inode
);
3396 struct page
*pages
[NFS4ACL_MAXPAGES
];
3397 struct nfs_setaclargs arg
= {
3398 .fh
= NFS_FH(inode
),
3402 struct nfs_setaclres res
;
3403 struct rpc_message msg
= {
3404 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETACL
],
3410 if (!nfs4_server_supports_acls(server
))
3412 nfs_inode_return_delegation(inode
);
3413 buf_to_pages(buf
, buflen
, arg
.acl_pages
, &arg
.acl_pgbase
);
3414 ret
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3415 nfs_access_zap_cache(inode
);
3416 nfs_zap_acl_cache(inode
);
3420 static int nfs4_proc_set_acl(struct inode
*inode
, const void *buf
, size_t buflen
)
3422 struct nfs4_exception exception
= { };
3425 err
= nfs4_handle_exception(NFS_SERVER(inode
),
3426 __nfs4_proc_set_acl(inode
, buf
, buflen
),
3428 } while (exception
.retry
);
3433 _nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs_client
*clp
, struct nfs4_state
*state
)
3435 if (!clp
|| task
->tk_status
>= 0)
3437 switch(task
->tk_status
) {
3438 case -NFS4ERR_ADMIN_REVOKED
:
3439 case -NFS4ERR_BAD_STATEID
:
3440 case -NFS4ERR_OPENMODE
:
3443 nfs4_state_mark_reclaim_nograce(clp
, state
);
3444 goto do_state_recovery
;
3445 case -NFS4ERR_STALE_STATEID
:
3448 nfs4_state_mark_reclaim_reboot(clp
, state
);
3449 case -NFS4ERR_STALE_CLIENTID
:
3450 case -NFS4ERR_EXPIRED
:
3451 goto do_state_recovery
;
3452 #if defined(CONFIG_NFS_V4_1)
3453 case -NFS4ERR_BADSESSION
:
3454 case -NFS4ERR_BADSLOT
:
3455 case -NFS4ERR_BAD_HIGH_SLOT
:
3456 case -NFS4ERR_DEADSESSION
:
3457 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
3458 case -NFS4ERR_SEQ_FALSE_RETRY
:
3459 case -NFS4ERR_SEQ_MISORDERED
:
3460 dprintk("%s ERROR %d, Reset session\n", __func__
,
3462 nfs4_schedule_state_recovery(clp
);
3463 task
->tk_status
= 0;
3465 #endif /* CONFIG_NFS_V4_1 */
3466 case -NFS4ERR_DELAY
:
3468 nfs_inc_server_stats(server
, NFSIOS_DELAY
);
3469 case -NFS4ERR_GRACE
:
3471 rpc_delay(task
, NFS4_POLL_RETRY_MAX
);
3472 task
->tk_status
= 0;
3474 case -NFS4ERR_OLD_STATEID
:
3475 task
->tk_status
= 0;
3478 task
->tk_status
= nfs4_map_errors(task
->tk_status
);
3481 rpc_sleep_on(&clp
->cl_rpcwaitq
, task
, NULL
);
3482 nfs4_schedule_state_recovery(clp
);
3483 if (test_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) == 0)
3484 rpc_wake_up_queued_task(&clp
->cl_rpcwaitq
, task
);
3485 task
->tk_status
= 0;
3490 nfs4_async_handle_error(struct rpc_task
*task
, const struct nfs_server
*server
, struct nfs4_state
*state
)
3492 return _nfs4_async_handle_error(task
, server
, server
->nfs_client
, state
);
3495 int nfs4_proc_setclientid(struct nfs_client
*clp
, u32 program
, unsigned short port
, struct rpc_cred
*cred
)
3497 nfs4_verifier sc_verifier
;
3498 struct nfs4_setclientid setclientid
= {
3499 .sc_verifier
= &sc_verifier
,
3502 struct rpc_message msg
= {
3503 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID
],
3504 .rpc_argp
= &setclientid
,
3512 p
= (__be32
*)sc_verifier
.data
;
3513 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
3514 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
3517 setclientid
.sc_name_len
= scnprintf(setclientid
.sc_name
,
3518 sizeof(setclientid
.sc_name
), "%s/%s %s %s %u",
3520 rpc_peeraddr2str(clp
->cl_rpcclient
,
3522 rpc_peeraddr2str(clp
->cl_rpcclient
,
3524 clp
->cl_rpcclient
->cl_auth
->au_ops
->au_name
,
3525 clp
->cl_id_uniquifier
);
3526 setclientid
.sc_netid_len
= scnprintf(setclientid
.sc_netid
,
3527 sizeof(setclientid
.sc_netid
),
3528 rpc_peeraddr2str(clp
->cl_rpcclient
,
3529 RPC_DISPLAY_NETID
));
3530 setclientid
.sc_uaddr_len
= scnprintf(setclientid
.sc_uaddr
,
3531 sizeof(setclientid
.sc_uaddr
), "%s.%u.%u",
3532 clp
->cl_ipaddr
, port
>> 8, port
& 255);
3534 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3535 if (status
!= -NFS4ERR_CLID_INUSE
)
3540 ssleep(clp
->cl_lease_time
+ 1);
3542 if (++clp
->cl_id_uniquifier
== 0)
3548 static int _nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3550 struct nfs_fsinfo fsinfo
;
3551 struct rpc_message msg
= {
3552 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SETCLIENTID_CONFIRM
],
3554 .rpc_resp
= &fsinfo
,
3561 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
3563 spin_lock(&clp
->cl_lock
);
3564 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
3565 clp
->cl_last_renewal
= now
;
3566 spin_unlock(&clp
->cl_lock
);
3571 int nfs4_proc_setclientid_confirm(struct nfs_client
*clp
, struct rpc_cred
*cred
)
3576 err
= _nfs4_proc_setclientid_confirm(clp
, cred
);
3580 case -NFS4ERR_RESOURCE
:
3581 /* The IBM lawyers misread another document! */
3582 case -NFS4ERR_DELAY
:
3584 err
= nfs4_delay(clp
->cl_rpcclient
, &timeout
);
3590 struct nfs4_delegreturndata
{
3591 struct nfs4_delegreturnargs args
;
3592 struct nfs4_delegreturnres res
;
3594 nfs4_stateid stateid
;
3595 unsigned long timestamp
;
3596 struct nfs_fattr fattr
;
3600 static void nfs4_delegreturn_done(struct rpc_task
*task
, void *calldata
)
3602 struct nfs4_delegreturndata
*data
= calldata
;
3604 nfs4_sequence_done(data
->res
.server
, &data
->res
.seq_res
,
3607 switch (task
->tk_status
) {
3608 case -NFS4ERR_STALE_STATEID
:
3609 case -NFS4ERR_EXPIRED
:
3611 renew_lease(data
->res
.server
, data
->timestamp
);
3614 if (nfs4_async_handle_error(task
, data
->res
.server
, NULL
) ==
3616 nfs_restart_rpc(task
, data
->res
.server
->nfs_client
);
3620 data
->rpc_status
= task
->tk_status
;
3623 static void nfs4_delegreturn_release(void *calldata
)
3628 #if defined(CONFIG_NFS_V4_1)
3629 static void nfs4_delegreturn_prepare(struct rpc_task
*task
, void *data
)
3631 struct nfs4_delegreturndata
*d_data
;
3633 d_data
= (struct nfs4_delegreturndata
*)data
;
3635 if (nfs4_setup_sequence(d_data
->res
.server
->nfs_client
,
3636 &d_data
->args
.seq_args
,
3637 &d_data
->res
.seq_res
, 1, task
))
3639 rpc_call_start(task
);
3641 #endif /* CONFIG_NFS_V4_1 */
3643 static const struct rpc_call_ops nfs4_delegreturn_ops
= {
3644 #if defined(CONFIG_NFS_V4_1)
3645 .rpc_call_prepare
= nfs4_delegreturn_prepare
,
3646 #endif /* CONFIG_NFS_V4_1 */
3647 .rpc_call_done
= nfs4_delegreturn_done
,
3648 .rpc_release
= nfs4_delegreturn_release
,
3651 static int _nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3653 struct nfs4_delegreturndata
*data
;
3654 struct nfs_server
*server
= NFS_SERVER(inode
);
3655 struct rpc_task
*task
;
3656 struct rpc_message msg
= {
3657 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DELEGRETURN
],
3660 struct rpc_task_setup task_setup_data
= {
3661 .rpc_client
= server
->client
,
3662 .rpc_message
= &msg
,
3663 .callback_ops
= &nfs4_delegreturn_ops
,
3664 .flags
= RPC_TASK_ASYNC
,
3668 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
3671 data
->args
.fhandle
= &data
->fh
;
3672 data
->args
.stateid
= &data
->stateid
;
3673 data
->args
.bitmask
= server
->attr_bitmask
;
3674 nfs_copy_fh(&data
->fh
, NFS_FH(inode
));
3675 memcpy(&data
->stateid
, stateid
, sizeof(data
->stateid
));
3676 data
->res
.fattr
= &data
->fattr
;
3677 data
->res
.server
= server
;
3678 data
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3679 nfs_fattr_init(data
->res
.fattr
);
3680 data
->timestamp
= jiffies
;
3681 data
->rpc_status
= 0;
3683 task_setup_data
.callback_data
= data
;
3684 msg
.rpc_argp
= &data
->args
,
3685 msg
.rpc_resp
= &data
->res
,
3686 task
= rpc_run_task(&task_setup_data
);
3688 return PTR_ERR(task
);
3691 status
= nfs4_wait_for_completion_rpc_task(task
);
3694 status
= data
->rpc_status
;
3697 nfs_refresh_inode(inode
, &data
->fattr
);
3703 int nfs4_proc_delegreturn(struct inode
*inode
, struct rpc_cred
*cred
, const nfs4_stateid
*stateid
, int issync
)
3705 struct nfs_server
*server
= NFS_SERVER(inode
);
3706 struct nfs4_exception exception
= { };
3709 err
= _nfs4_proc_delegreturn(inode
, cred
, stateid
, issync
);
3711 case -NFS4ERR_STALE_STATEID
:
3712 case -NFS4ERR_EXPIRED
:
3716 err
= nfs4_handle_exception(server
, err
, &exception
);
3717 } while (exception
.retry
);
3721 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3722 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3725 * sleep, with exponential backoff, and retry the LOCK operation.
3727 static unsigned long
3728 nfs4_set_lock_task_retry(unsigned long timeout
)
3730 schedule_timeout_killable(timeout
);
3732 if (timeout
> NFS4_LOCK_MAXTIMEOUT
)
3733 return NFS4_LOCK_MAXTIMEOUT
;
3737 static int _nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3739 struct inode
*inode
= state
->inode
;
3740 struct nfs_server
*server
= NFS_SERVER(inode
);
3741 struct nfs_client
*clp
= server
->nfs_client
;
3742 struct nfs_lockt_args arg
= {
3743 .fh
= NFS_FH(inode
),
3746 struct nfs_lockt_res res
= {
3749 struct rpc_message msg
= {
3750 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKT
],
3753 .rpc_cred
= state
->owner
->so_cred
,
3755 struct nfs4_lock_state
*lsp
;
3758 arg
.lock_owner
.clientid
= clp
->cl_clientid
;
3759 status
= nfs4_set_lock_state(state
, request
);
3762 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3763 arg
.lock_owner
.id
= lsp
->ls_id
.id
;
3764 status
= nfs4_call_sync(server
, &msg
, &arg
, &res
, 1);
3767 request
->fl_type
= F_UNLCK
;
3769 case -NFS4ERR_DENIED
:
3772 request
->fl_ops
->fl_release_private(request
);
3777 static int nfs4_proc_getlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3779 struct nfs4_exception exception
= { };
3783 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
3784 _nfs4_proc_getlk(state
, cmd
, request
),
3786 } while (exception
.retry
);
3790 static int do_vfs_lock(struct file
*file
, struct file_lock
*fl
)
3793 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
3795 res
= posix_lock_file_wait(file
, fl
);
3798 res
= flock_lock_file_wait(file
, fl
);
3806 struct nfs4_unlockdata
{
3807 struct nfs_locku_args arg
;
3808 struct nfs_locku_res res
;
3809 struct nfs4_lock_state
*lsp
;
3810 struct nfs_open_context
*ctx
;
3811 struct file_lock fl
;
3812 const struct nfs_server
*server
;
3813 unsigned long timestamp
;
3816 static struct nfs4_unlockdata
*nfs4_alloc_unlockdata(struct file_lock
*fl
,
3817 struct nfs_open_context
*ctx
,
3818 struct nfs4_lock_state
*lsp
,
3819 struct nfs_seqid
*seqid
)
3821 struct nfs4_unlockdata
*p
;
3822 struct inode
*inode
= lsp
->ls_state
->inode
;
3824 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
3827 p
->arg
.fh
= NFS_FH(inode
);
3829 p
->arg
.seqid
= seqid
;
3830 p
->res
.seqid
= seqid
;
3831 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
3832 p
->arg
.stateid
= &lsp
->ls_stateid
;
3834 atomic_inc(&lsp
->ls_count
);
3835 /* Ensure we don't close file until we're done freeing locks! */
3836 p
->ctx
= get_nfs_open_context(ctx
);
3837 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
3838 p
->server
= NFS_SERVER(inode
);
3842 static void nfs4_locku_release_calldata(void *data
)
3844 struct nfs4_unlockdata
*calldata
= data
;
3845 nfs_free_seqid(calldata
->arg
.seqid
);
3846 nfs4_put_lock_state(calldata
->lsp
);
3847 put_nfs_open_context(calldata
->ctx
);
3851 static void nfs4_locku_done(struct rpc_task
*task
, void *data
)
3853 struct nfs4_unlockdata
*calldata
= data
;
3855 nfs4_sequence_done(calldata
->server
, &calldata
->res
.seq_res
,
3857 if (RPC_ASSASSINATED(task
))
3859 switch (task
->tk_status
) {
3861 memcpy(calldata
->lsp
->ls_stateid
.data
,
3862 calldata
->res
.stateid
.data
,
3863 sizeof(calldata
->lsp
->ls_stateid
.data
));
3864 renew_lease(calldata
->server
, calldata
->timestamp
);
3866 case -NFS4ERR_BAD_STATEID
:
3867 case -NFS4ERR_OLD_STATEID
:
3868 case -NFS4ERR_STALE_STATEID
:
3869 case -NFS4ERR_EXPIRED
:
3872 if (nfs4_async_handle_error(task
, calldata
->server
, NULL
) == -EAGAIN
)
3873 nfs_restart_rpc(task
,
3874 calldata
->server
->nfs_client
);
3878 static void nfs4_locku_prepare(struct rpc_task
*task
, void *data
)
3880 struct nfs4_unlockdata
*calldata
= data
;
3882 if (nfs_wait_on_sequence(calldata
->arg
.seqid
, task
) != 0)
3884 if ((calldata
->lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) == 0) {
3885 /* Note: exit _without_ running nfs4_locku_done */
3886 task
->tk_action
= NULL
;
3889 calldata
->timestamp
= jiffies
;
3890 if (nfs4_setup_sequence(calldata
->server
->nfs_client
,
3891 &calldata
->arg
.seq_args
,
3892 &calldata
->res
.seq_res
, 1, task
))
3894 rpc_call_start(task
);
3897 static const struct rpc_call_ops nfs4_locku_ops
= {
3898 .rpc_call_prepare
= nfs4_locku_prepare
,
3899 .rpc_call_done
= nfs4_locku_done
,
3900 .rpc_release
= nfs4_locku_release_calldata
,
3903 static struct rpc_task
*nfs4_do_unlck(struct file_lock
*fl
,
3904 struct nfs_open_context
*ctx
,
3905 struct nfs4_lock_state
*lsp
,
3906 struct nfs_seqid
*seqid
)
3908 struct nfs4_unlockdata
*data
;
3909 struct rpc_message msg
= {
3910 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCKU
],
3911 .rpc_cred
= ctx
->cred
,
3913 struct rpc_task_setup task_setup_data
= {
3914 .rpc_client
= NFS_CLIENT(lsp
->ls_state
->inode
),
3915 .rpc_message
= &msg
,
3916 .callback_ops
= &nfs4_locku_ops
,
3917 .workqueue
= nfsiod_workqueue
,
3918 .flags
= RPC_TASK_ASYNC
,
3921 /* Ensure this is an unlock - when canceling a lock, the
3922 * canceled lock is passed in, and it won't be an unlock.
3924 fl
->fl_type
= F_UNLCK
;
3926 data
= nfs4_alloc_unlockdata(fl
, ctx
, lsp
, seqid
);
3928 nfs_free_seqid(seqid
);
3929 return ERR_PTR(-ENOMEM
);
3932 msg
.rpc_argp
= &data
->arg
,
3933 msg
.rpc_resp
= &data
->res
,
3934 task_setup_data
.callback_data
= data
;
3935 return rpc_run_task(&task_setup_data
);
3938 static int nfs4_proc_unlck(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
3940 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
3941 struct nfs_seqid
*seqid
;
3942 struct nfs4_lock_state
*lsp
;
3943 struct rpc_task
*task
;
3945 unsigned char fl_flags
= request
->fl_flags
;
3947 status
= nfs4_set_lock_state(state
, request
);
3948 /* Unlock _before_ we do the RPC call */
3949 request
->fl_flags
|= FL_EXISTS
;
3950 down_read(&nfsi
->rwsem
);
3951 if (do_vfs_lock(request
->fl_file
, request
) == -ENOENT
) {
3952 up_read(&nfsi
->rwsem
);
3955 up_read(&nfsi
->rwsem
);
3958 /* Is this a delegated lock? */
3959 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
3961 lsp
= request
->fl_u
.nfs4_fl
.owner
;
3962 seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
3966 task
= nfs4_do_unlck(request
, nfs_file_open_context(request
->fl_file
), lsp
, seqid
);
3967 status
= PTR_ERR(task
);
3970 status
= nfs4_wait_for_completion_rpc_task(task
);
3973 request
->fl_flags
= fl_flags
;
3977 struct nfs4_lockdata
{
3978 struct nfs_lock_args arg
;
3979 struct nfs_lock_res res
;
3980 struct nfs4_lock_state
*lsp
;
3981 struct nfs_open_context
*ctx
;
3982 struct file_lock fl
;
3983 unsigned long timestamp
;
3986 struct nfs_server
*server
;
3989 static struct nfs4_lockdata
*nfs4_alloc_lockdata(struct file_lock
*fl
,
3990 struct nfs_open_context
*ctx
, struct nfs4_lock_state
*lsp
)
3992 struct nfs4_lockdata
*p
;
3993 struct inode
*inode
= lsp
->ls_state
->inode
;
3994 struct nfs_server
*server
= NFS_SERVER(inode
);
3996 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
4000 p
->arg
.fh
= NFS_FH(inode
);
4002 p
->arg
.open_seqid
= nfs_alloc_seqid(&lsp
->ls_state
->owner
->so_seqid
);
4003 if (p
->arg
.open_seqid
== NULL
)
4005 p
->arg
.lock_seqid
= nfs_alloc_seqid(&lsp
->ls_seqid
);
4006 if (p
->arg
.lock_seqid
== NULL
)
4007 goto out_free_seqid
;
4008 p
->arg
.lock_stateid
= &lsp
->ls_stateid
;
4009 p
->arg
.lock_owner
.clientid
= server
->nfs_client
->cl_clientid
;
4010 p
->arg
.lock_owner
.id
= lsp
->ls_id
.id
;
4011 p
->res
.lock_seqid
= p
->arg
.lock_seqid
;
4012 p
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4015 atomic_inc(&lsp
->ls_count
);
4016 p
->ctx
= get_nfs_open_context(ctx
);
4017 memcpy(&p
->fl
, fl
, sizeof(p
->fl
));
4020 nfs_free_seqid(p
->arg
.open_seqid
);
4026 static void nfs4_lock_prepare(struct rpc_task
*task
, void *calldata
)
4028 struct nfs4_lockdata
*data
= calldata
;
4029 struct nfs4_state
*state
= data
->lsp
->ls_state
;
4031 dprintk("%s: begin!\n", __func__
);
4032 if (nfs_wait_on_sequence(data
->arg
.lock_seqid
, task
) != 0)
4034 /* Do we need to do an open_to_lock_owner? */
4035 if (!(data
->arg
.lock_seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)) {
4036 if (nfs_wait_on_sequence(data
->arg
.open_seqid
, task
) != 0)
4038 data
->arg
.open_stateid
= &state
->stateid
;
4039 data
->arg
.new_lock_owner
= 1;
4040 data
->res
.open_seqid
= data
->arg
.open_seqid
;
4042 data
->arg
.new_lock_owner
= 0;
4043 data
->timestamp
= jiffies
;
4044 if (nfs4_setup_sequence(data
->server
->nfs_client
, &data
->arg
.seq_args
,
4045 &data
->res
.seq_res
, 1, task
))
4047 rpc_call_start(task
);
4048 dprintk("%s: done!, ret = %d\n", __func__
, data
->rpc_status
);
4051 static void nfs4_recover_lock_prepare(struct rpc_task
*task
, void *calldata
)
4053 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4054 nfs4_lock_prepare(task
, calldata
);
4057 static void nfs4_lock_done(struct rpc_task
*task
, void *calldata
)
4059 struct nfs4_lockdata
*data
= calldata
;
4061 dprintk("%s: begin!\n", __func__
);
4063 nfs4_sequence_done(data
->server
, &data
->res
.seq_res
,
4066 data
->rpc_status
= task
->tk_status
;
4067 if (RPC_ASSASSINATED(task
))
4069 if (data
->arg
.new_lock_owner
!= 0) {
4070 if (data
->rpc_status
== 0)
4071 nfs_confirm_seqid(&data
->lsp
->ls_seqid
, 0);
4075 if (data
->rpc_status
== 0) {
4076 memcpy(data
->lsp
->ls_stateid
.data
, data
->res
.stateid
.data
,
4077 sizeof(data
->lsp
->ls_stateid
.data
));
4078 data
->lsp
->ls_flags
|= NFS_LOCK_INITIALIZED
;
4079 renew_lease(NFS_SERVER(data
->ctx
->path
.dentry
->d_inode
), data
->timestamp
);
4082 dprintk("%s: done, ret = %d!\n", __func__
, data
->rpc_status
);
4085 static void nfs4_lock_release(void *calldata
)
4087 struct nfs4_lockdata
*data
= calldata
;
4089 dprintk("%s: begin!\n", __func__
);
4090 nfs_free_seqid(data
->arg
.open_seqid
);
4091 if (data
->cancelled
!= 0) {
4092 struct rpc_task
*task
;
4093 task
= nfs4_do_unlck(&data
->fl
, data
->ctx
, data
->lsp
,
4094 data
->arg
.lock_seqid
);
4097 dprintk("%s: cancelling lock!\n", __func__
);
4099 nfs_free_seqid(data
->arg
.lock_seqid
);
4100 nfs4_put_lock_state(data
->lsp
);
4101 put_nfs_open_context(data
->ctx
);
4103 dprintk("%s: done!\n", __func__
);
4106 static const struct rpc_call_ops nfs4_lock_ops
= {
4107 .rpc_call_prepare
= nfs4_lock_prepare
,
4108 .rpc_call_done
= nfs4_lock_done
,
4109 .rpc_release
= nfs4_lock_release
,
4112 static const struct rpc_call_ops nfs4_recover_lock_ops
= {
4113 .rpc_call_prepare
= nfs4_recover_lock_prepare
,
4114 .rpc_call_done
= nfs4_lock_done
,
4115 .rpc_release
= nfs4_lock_release
,
4118 static void nfs4_handle_setlk_error(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
, int new_lock_owner
, int error
)
4120 struct nfs_client
*clp
= server
->nfs_client
;
4121 struct nfs4_state
*state
= lsp
->ls_state
;
4124 case -NFS4ERR_ADMIN_REVOKED
:
4125 case -NFS4ERR_BAD_STATEID
:
4126 case -NFS4ERR_EXPIRED
:
4127 if (new_lock_owner
!= 0 ||
4128 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4129 nfs4_state_mark_reclaim_nograce(clp
, state
);
4130 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4132 case -NFS4ERR_STALE_STATEID
:
4133 if (new_lock_owner
!= 0 ||
4134 (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0)
4135 nfs4_state_mark_reclaim_reboot(clp
, state
);
4136 lsp
->ls_seqid
.flags
&= ~NFS_SEQID_CONFIRMED
;
4140 static int _nfs4_do_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*fl
, int recovery_type
)
4142 struct nfs4_lockdata
*data
;
4143 struct rpc_task
*task
;
4144 struct rpc_message msg
= {
4145 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_LOCK
],
4146 .rpc_cred
= state
->owner
->so_cred
,
4148 struct rpc_task_setup task_setup_data
= {
4149 .rpc_client
= NFS_CLIENT(state
->inode
),
4150 .rpc_message
= &msg
,
4151 .callback_ops
= &nfs4_lock_ops
,
4152 .workqueue
= nfsiod_workqueue
,
4153 .flags
= RPC_TASK_ASYNC
,
4157 dprintk("%s: begin!\n", __func__
);
4158 data
= nfs4_alloc_lockdata(fl
, nfs_file_open_context(fl
->fl_file
),
4159 fl
->fl_u
.nfs4_fl
.owner
);
4163 data
->arg
.block
= 1;
4164 if (recovery_type
> NFS_LOCK_NEW
) {
4165 if (recovery_type
== NFS_LOCK_RECLAIM
)
4166 data
->arg
.reclaim
= NFS_LOCK_RECLAIM
;
4167 task_setup_data
.callback_ops
= &nfs4_recover_lock_ops
;
4169 msg
.rpc_argp
= &data
->arg
,
4170 msg
.rpc_resp
= &data
->res
,
4171 task_setup_data
.callback_data
= data
;
4172 task
= rpc_run_task(&task_setup_data
);
4174 return PTR_ERR(task
);
4175 ret
= nfs4_wait_for_completion_rpc_task(task
);
4177 ret
= data
->rpc_status
;
4179 nfs4_handle_setlk_error(data
->server
, data
->lsp
,
4180 data
->arg
.new_lock_owner
, ret
);
4182 data
->cancelled
= 1;
4184 dprintk("%s: done, ret = %d!\n", __func__
, ret
);
4188 static int nfs4_lock_reclaim(struct nfs4_state
*state
, struct file_lock
*request
)
4190 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4191 struct nfs4_exception exception
= { };
4195 /* Cache the lock if possible... */
4196 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4198 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_RECLAIM
);
4199 if (err
!= -NFS4ERR_DELAY
&& err
!= -EKEYEXPIRED
)
4201 nfs4_handle_exception(server
, err
, &exception
);
4202 } while (exception
.retry
);
4206 static int nfs4_lock_expired(struct nfs4_state
*state
, struct file_lock
*request
)
4208 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4209 struct nfs4_exception exception
= { };
4212 err
= nfs4_set_lock_state(state
, request
);
4216 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
) != 0)
4218 err
= _nfs4_do_setlk(state
, F_SETLK
, request
, NFS_LOCK_EXPIRED
);
4222 case -NFS4ERR_GRACE
:
4223 case -NFS4ERR_DELAY
:
4225 nfs4_handle_exception(server
, err
, &exception
);
4228 } while (exception
.retry
);
4233 static int _nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4235 struct nfs_inode
*nfsi
= NFS_I(state
->inode
);
4236 unsigned char fl_flags
= request
->fl_flags
;
4237 int status
= -ENOLCK
;
4239 if ((fl_flags
& FL_POSIX
) &&
4240 !test_bit(NFS_STATE_POSIX_LOCKS
, &state
->flags
))
4242 /* Is this a delegated open? */
4243 status
= nfs4_set_lock_state(state
, request
);
4246 request
->fl_flags
|= FL_ACCESS
;
4247 status
= do_vfs_lock(request
->fl_file
, request
);
4250 down_read(&nfsi
->rwsem
);
4251 if (test_bit(NFS_DELEGATED_STATE
, &state
->flags
)) {
4252 /* Yes: cache locks! */
4253 /* ...but avoid races with delegation recall... */
4254 request
->fl_flags
= fl_flags
& ~FL_SLEEP
;
4255 status
= do_vfs_lock(request
->fl_file
, request
);
4258 status
= _nfs4_do_setlk(state
, cmd
, request
, NFS_LOCK_NEW
);
4261 /* Note: we always want to sleep here! */
4262 request
->fl_flags
= fl_flags
| FL_SLEEP
;
4263 if (do_vfs_lock(request
->fl_file
, request
) < 0)
4264 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n", __func__
);
4266 up_read(&nfsi
->rwsem
);
4268 request
->fl_flags
= fl_flags
;
4272 static int nfs4_proc_setlk(struct nfs4_state
*state
, int cmd
, struct file_lock
*request
)
4274 struct nfs4_exception exception
= { };
4278 err
= _nfs4_proc_setlk(state
, cmd
, request
);
4279 if (err
== -NFS4ERR_DENIED
)
4281 err
= nfs4_handle_exception(NFS_SERVER(state
->inode
),
4283 } while (exception
.retry
);
4288 nfs4_proc_lock(struct file
*filp
, int cmd
, struct file_lock
*request
)
4290 struct nfs_open_context
*ctx
;
4291 struct nfs4_state
*state
;
4292 unsigned long timeout
= NFS4_LOCK_MINTIMEOUT
;
4295 /* verify open state */
4296 ctx
= nfs_file_open_context(filp
);
4299 if (request
->fl_start
< 0 || request
->fl_end
< 0)
4302 if (IS_GETLK(cmd
)) {
4304 return nfs4_proc_getlk(state
, F_GETLK
, request
);
4308 if (!(IS_SETLK(cmd
) || IS_SETLKW(cmd
)))
4311 if (request
->fl_type
== F_UNLCK
) {
4313 return nfs4_proc_unlck(state
, cmd
, request
);
4320 status
= nfs4_proc_setlk(state
, cmd
, request
);
4321 if ((status
!= -EAGAIN
) || IS_SETLK(cmd
))
4323 timeout
= nfs4_set_lock_task_retry(timeout
);
4324 status
= -ERESTARTSYS
;
4327 } while(status
< 0);
4331 int nfs4_lock_delegation_recall(struct nfs4_state
*state
, struct file_lock
*fl
)
4333 struct nfs_server
*server
= NFS_SERVER(state
->inode
);
4334 struct nfs4_exception exception
= { };
4337 err
= nfs4_set_lock_state(state
, fl
);
4341 err
= _nfs4_do_setlk(state
, F_SETLK
, fl
, NFS_LOCK_NEW
);
4344 printk(KERN_ERR
"%s: unhandled error %d.\n",
4349 case -NFS4ERR_EXPIRED
:
4350 case -NFS4ERR_STALE_CLIENTID
:
4351 case -NFS4ERR_STALE_STATEID
:
4352 case -NFS4ERR_BADSESSION
:
4353 case -NFS4ERR_BADSLOT
:
4354 case -NFS4ERR_BAD_HIGH_SLOT
:
4355 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
4356 case -NFS4ERR_DEADSESSION
:
4357 nfs4_schedule_state_recovery(server
->nfs_client
);
4361 * The show must go on: exit, but mark the
4362 * stateid as needing recovery.
4364 case -NFS4ERR_ADMIN_REVOKED
:
4365 case -NFS4ERR_BAD_STATEID
:
4366 case -NFS4ERR_OPENMODE
:
4367 nfs4_state_mark_reclaim_nograce(server
->nfs_client
, state
);
4371 case -NFS4ERR_DENIED
:
4372 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4375 case -NFS4ERR_DELAY
:
4379 err
= nfs4_handle_exception(server
, err
, &exception
);
4380 } while (exception
.retry
);
4385 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4387 int nfs4_setxattr(struct dentry
*dentry
, const char *key
, const void *buf
,
4388 size_t buflen
, int flags
)
4390 struct inode
*inode
= dentry
->d_inode
;
4392 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4395 return nfs4_proc_set_acl(inode
, buf
, buflen
);
4398 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4399 * and that's what we'll do for e.g. user attributes that haven't been set.
4400 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4401 * attributes in kernel-managed attribute namespaces. */
4402 ssize_t
nfs4_getxattr(struct dentry
*dentry
, const char *key
, void *buf
,
4405 struct inode
*inode
= dentry
->d_inode
;
4407 if (strcmp(key
, XATTR_NAME_NFSV4_ACL
) != 0)
4410 return nfs4_proc_get_acl(inode
, buf
, buflen
);
4413 ssize_t
nfs4_listxattr(struct dentry
*dentry
, char *buf
, size_t buflen
)
4415 size_t len
= strlen(XATTR_NAME_NFSV4_ACL
) + 1;
4417 if (!nfs4_server_supports_acls(NFS_SERVER(dentry
->d_inode
)))
4419 if (buf
&& buflen
< len
)
4422 memcpy(buf
, XATTR_NAME_NFSV4_ACL
, len
);
4426 static void nfs_fixup_referral_attributes(struct nfs_fattr
*fattr
)
4428 if (!((fattr
->valid
& NFS_ATTR_FATTR_FILEID
) &&
4429 (fattr
->valid
& NFS_ATTR_FATTR_FSID
) &&
4430 (fattr
->valid
& NFS_ATTR_FATTR_V4_REFERRAL
)))
4433 fattr
->valid
|= NFS_ATTR_FATTR_TYPE
| NFS_ATTR_FATTR_MODE
|
4434 NFS_ATTR_FATTR_NLINK
;
4435 fattr
->mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
;
4439 int nfs4_proc_fs_locations(struct inode
*dir
, const struct qstr
*name
,
4440 struct nfs4_fs_locations
*fs_locations
, struct page
*page
)
4442 struct nfs_server
*server
= NFS_SERVER(dir
);
4444 [0] = FATTR4_WORD0_FSID
| FATTR4_WORD0_FS_LOCATIONS
,
4445 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID
,
4447 struct nfs4_fs_locations_arg args
= {
4448 .dir_fh
= NFS_FH(dir
),
4453 struct nfs4_fs_locations_res res
= {
4454 .fs_locations
= fs_locations
,
4456 struct rpc_message msg
= {
4457 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_FS_LOCATIONS
],
4463 dprintk("%s: start\n", __func__
);
4464 nfs_fattr_init(&fs_locations
->fattr
);
4465 fs_locations
->server
= server
;
4466 fs_locations
->nlocations
= 0;
4467 status
= nfs4_call_sync(server
, &msg
, &args
, &res
, 0);
4468 nfs_fixup_referral_attributes(&fs_locations
->fattr
);
4469 dprintk("%s: returned status = %d\n", __func__
, status
);
4473 #ifdef CONFIG_NFS_V4_1
4475 * nfs4_proc_exchange_id()
4477 * Since the clientid has expired, all compounds using sessions
4478 * associated with the stale clientid will be returning
4479 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4480 * be in some phase of session reset.
4482 int nfs4_proc_exchange_id(struct nfs_client
*clp
, struct rpc_cred
*cred
)
4484 nfs4_verifier verifier
;
4485 struct nfs41_exchange_id_args args
= {
4487 .flags
= clp
->cl_exchange_flags
,
4489 struct nfs41_exchange_id_res res
= {
4493 struct rpc_message msg
= {
4494 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_EXCHANGE_ID
],
4501 dprintk("--> %s\n", __func__
);
4502 BUG_ON(clp
== NULL
);
4504 /* Remove server-only flags */
4505 args
.flags
&= ~EXCHGID4_FLAG_CONFIRMED_R
;
4507 p
= (u32
*)verifier
.data
;
4508 *p
++ = htonl((u32
)clp
->cl_boot_time
.tv_sec
);
4509 *p
= htonl((u32
)clp
->cl_boot_time
.tv_nsec
);
4510 args
.verifier
= &verifier
;
4513 args
.id_len
= scnprintf(args
.id
, sizeof(args
.id
),
4516 rpc_peeraddr2str(clp
->cl_rpcclient
,
4518 clp
->cl_id_uniquifier
);
4520 status
= rpc_call_sync(clp
->cl_rpcclient
, &msg
, 0);
4522 if (status
!= -NFS4ERR_CLID_INUSE
)
4528 if (++clp
->cl_id_uniquifier
== 0)
4532 dprintk("<-- %s status= %d\n", __func__
, status
);
4536 struct nfs4_get_lease_time_data
{
4537 struct nfs4_get_lease_time_args
*args
;
4538 struct nfs4_get_lease_time_res
*res
;
4539 struct nfs_client
*clp
;
4542 static void nfs4_get_lease_time_prepare(struct rpc_task
*task
,
4546 struct nfs4_get_lease_time_data
*data
=
4547 (struct nfs4_get_lease_time_data
*)calldata
;
4549 dprintk("--> %s\n", __func__
);
4550 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
4551 /* just setup sequence, do not trigger session recovery
4552 since we're invoked within one */
4553 ret
= nfs41_setup_sequence(data
->clp
->cl_session
,
4554 &data
->args
->la_seq_args
,
4555 &data
->res
->lr_seq_res
, 0, task
);
4557 BUG_ON(ret
== -EAGAIN
);
4558 rpc_call_start(task
);
4559 dprintk("<-- %s\n", __func__
);
4563 * Called from nfs4_state_manager thread for session setup, so don't recover
4564 * from sequence operation or clientid errors.
4566 static void nfs4_get_lease_time_done(struct rpc_task
*task
, void *calldata
)
4568 struct nfs4_get_lease_time_data
*data
=
4569 (struct nfs4_get_lease_time_data
*)calldata
;
4571 dprintk("--> %s\n", __func__
);
4572 nfs41_sequence_done(data
->clp
, &data
->res
->lr_seq_res
, task
->tk_status
);
4573 switch (task
->tk_status
) {
4574 case -NFS4ERR_DELAY
:
4575 case -NFS4ERR_GRACE
:
4577 dprintk("%s Retry: tk_status %d\n", __func__
, task
->tk_status
);
4578 rpc_delay(task
, NFS4_POLL_RETRY_MIN
);
4579 task
->tk_status
= 0;
4580 nfs_restart_rpc(task
, data
->clp
);
4583 dprintk("<-- %s\n", __func__
);
4586 struct rpc_call_ops nfs4_get_lease_time_ops
= {
4587 .rpc_call_prepare
= nfs4_get_lease_time_prepare
,
4588 .rpc_call_done
= nfs4_get_lease_time_done
,
4591 int nfs4_proc_get_lease_time(struct nfs_client
*clp
, struct nfs_fsinfo
*fsinfo
)
4593 struct rpc_task
*task
;
4594 struct nfs4_get_lease_time_args args
;
4595 struct nfs4_get_lease_time_res res
= {
4596 .lr_fsinfo
= fsinfo
,
4598 struct nfs4_get_lease_time_data data
= {
4603 struct rpc_message msg
= {
4604 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_GET_LEASE_TIME
],
4608 struct rpc_task_setup task_setup
= {
4609 .rpc_client
= clp
->cl_rpcclient
,
4610 .rpc_message
= &msg
,
4611 .callback_ops
= &nfs4_get_lease_time_ops
,
4612 .callback_data
= &data
4616 res
.lr_seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
4617 dprintk("--> %s\n", __func__
);
4618 task
= rpc_run_task(&task_setup
);
4621 status
= PTR_ERR(task
);
4623 status
= task
->tk_status
;
4626 dprintk("<-- %s return %d\n", __func__
, status
);
4632 * Reset a slot table
4634 static int nfs4_reset_slot_table(struct nfs4_slot_table
*tbl
, u32 max_reqs
,
4637 struct nfs4_slot
*new = NULL
;
4641 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__
,
4642 max_reqs
, tbl
->max_slots
);
4644 /* Does the newly negotiated max_reqs match the existing slot table? */
4645 if (max_reqs
!= tbl
->max_slots
) {
4647 new = kmalloc(max_reqs
* sizeof(struct nfs4_slot
),
4654 spin_lock(&tbl
->slot_tbl_lock
);
4657 tbl
->max_slots
= max_reqs
;
4659 for (i
= 0; i
< tbl
->max_slots
; ++i
)
4660 tbl
->slots
[i
].seq_nr
= ivalue
;
4661 spin_unlock(&tbl
->slot_tbl_lock
);
4662 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4663 tbl
, tbl
->slots
, tbl
->max_slots
);
4665 dprintk("<-- %s: return %d\n", __func__
, ret
);
4670 * Reset the forechannel and backchannel slot tables
4672 static int nfs4_reset_slot_tables(struct nfs4_session
*session
)
4676 status
= nfs4_reset_slot_table(&session
->fc_slot_table
,
4677 session
->fc_attrs
.max_reqs
, 1);
4681 status
= nfs4_reset_slot_table(&session
->bc_slot_table
,
4682 session
->bc_attrs
.max_reqs
, 0);
4686 /* Destroy the slot table */
4687 static void nfs4_destroy_slot_tables(struct nfs4_session
*session
)
4689 if (session
->fc_slot_table
.slots
!= NULL
) {
4690 kfree(session
->fc_slot_table
.slots
);
4691 session
->fc_slot_table
.slots
= NULL
;
4693 if (session
->bc_slot_table
.slots
!= NULL
) {
4694 kfree(session
->bc_slot_table
.slots
);
4695 session
->bc_slot_table
.slots
= NULL
;
4701 * Initialize slot table
4703 static int nfs4_init_slot_table(struct nfs4_slot_table
*tbl
,
4704 int max_slots
, int ivalue
)
4706 struct nfs4_slot
*slot
;
4709 BUG_ON(max_slots
> NFS4_MAX_SLOT_TABLE
);
4711 dprintk("--> %s: max_reqs=%u\n", __func__
, max_slots
);
4713 slot
= kcalloc(max_slots
, sizeof(struct nfs4_slot
), GFP_KERNEL
);
4718 spin_lock(&tbl
->slot_tbl_lock
);
4719 tbl
->max_slots
= max_slots
;
4721 tbl
->highest_used_slotid
= -1; /* no slot is currently used */
4722 spin_unlock(&tbl
->slot_tbl_lock
);
4723 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__
,
4724 tbl
, tbl
->slots
, tbl
->max_slots
);
4726 dprintk("<-- %s: return %d\n", __func__
, ret
);
4731 * Initialize the forechannel and backchannel tables
4733 static int nfs4_init_slot_tables(struct nfs4_session
*session
)
4735 struct nfs4_slot_table
*tbl
;
4738 tbl
= &session
->fc_slot_table
;
4739 if (tbl
->slots
== NULL
) {
4740 status
= nfs4_init_slot_table(tbl
,
4741 session
->fc_attrs
.max_reqs
, 1);
4746 tbl
= &session
->bc_slot_table
;
4747 if (tbl
->slots
== NULL
) {
4748 status
= nfs4_init_slot_table(tbl
,
4749 session
->bc_attrs
.max_reqs
, 0);
4751 nfs4_destroy_slot_tables(session
);
4757 struct nfs4_session
*nfs4_alloc_session(struct nfs_client
*clp
)
4759 struct nfs4_session
*session
;
4760 struct nfs4_slot_table
*tbl
;
4762 session
= kzalloc(sizeof(struct nfs4_session
), GFP_KERNEL
);
4767 * The create session reply races with the server back
4768 * channel probe. Mark the client NFS_CS_SESSION_INITING
4769 * so that the client back channel can find the
4772 clp
->cl_cons_state
= NFS_CS_SESSION_INITING
;
4773 init_completion(&session
->complete
);
4775 tbl
= &session
->fc_slot_table
;
4776 tbl
->highest_used_slotid
= -1;
4777 spin_lock_init(&tbl
->slot_tbl_lock
);
4778 rpc_init_priority_wait_queue(&tbl
->slot_tbl_waitq
, "ForeChannel Slot table");
4780 tbl
= &session
->bc_slot_table
;
4781 tbl
->highest_used_slotid
= -1;
4782 spin_lock_init(&tbl
->slot_tbl_lock
);
4783 rpc_init_wait_queue(&tbl
->slot_tbl_waitq
, "BackChannel Slot table");
4789 void nfs4_destroy_session(struct nfs4_session
*session
)
4791 nfs4_proc_destroy_session(session
);
4792 dprintk("%s Destroy backchannel for xprt %p\n",
4793 __func__
, session
->clp
->cl_rpcclient
->cl_xprt
);
4794 xprt_destroy_backchannel(session
->clp
->cl_rpcclient
->cl_xprt
,
4795 NFS41_BC_MIN_CALLBACKS
);
4796 nfs4_destroy_slot_tables(session
);
4801 * Initialize the values to be used by the client in CREATE_SESSION
4802 * If nfs4_init_session set the fore channel request and response sizes,
4805 * Set the back channel max_resp_sz_cached to zero to force the client to
4806 * always set csa_cachethis to FALSE because the current implementation
4807 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4809 static void nfs4_init_channel_attrs(struct nfs41_create_session_args
*args
)
4811 struct nfs4_session
*session
= args
->client
->cl_session
;
4812 unsigned int mxrqst_sz
= session
->fc_attrs
.max_rqst_sz
,
4813 mxresp_sz
= session
->fc_attrs
.max_resp_sz
;
4816 mxrqst_sz
= NFS_MAX_FILE_IO_SIZE
;
4818 mxresp_sz
= NFS_MAX_FILE_IO_SIZE
;
4819 /* Fore channel attributes */
4820 args
->fc_attrs
.headerpadsz
= 0;
4821 args
->fc_attrs
.max_rqst_sz
= mxrqst_sz
;
4822 args
->fc_attrs
.max_resp_sz
= mxresp_sz
;
4823 args
->fc_attrs
.max_ops
= NFS4_MAX_OPS
;
4824 args
->fc_attrs
.max_reqs
= session
->clp
->cl_rpcclient
->cl_xprt
->max_reqs
;
4826 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4827 "max_ops=%u max_reqs=%u\n",
4829 args
->fc_attrs
.max_rqst_sz
, args
->fc_attrs
.max_resp_sz
,
4830 args
->fc_attrs
.max_ops
, args
->fc_attrs
.max_reqs
);
4832 /* Back channel attributes */
4833 args
->bc_attrs
.headerpadsz
= 0;
4834 args
->bc_attrs
.max_rqst_sz
= PAGE_SIZE
;
4835 args
->bc_attrs
.max_resp_sz
= PAGE_SIZE
;
4836 args
->bc_attrs
.max_resp_sz_cached
= 0;
4837 args
->bc_attrs
.max_ops
= NFS4_MAX_BACK_CHANNEL_OPS
;
4838 args
->bc_attrs
.max_reqs
= 1;
4840 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4841 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4843 args
->bc_attrs
.max_rqst_sz
, args
->bc_attrs
.max_resp_sz
,
4844 args
->bc_attrs
.max_resp_sz_cached
, args
->bc_attrs
.max_ops
,
4845 args
->bc_attrs
.max_reqs
);
4848 static int _verify_channel_attr(char *chan
, char *attr_name
, u32 sent
, u32 rcvd
)
4852 printk(KERN_WARNING
"%s: Session INVALID: %s channel %s increased. "
4853 "sent=%u rcvd=%u\n", __func__
, chan
, attr_name
, sent
, rcvd
);
4857 #define _verify_fore_channel_attr(_name_) \
4858 _verify_channel_attr("fore", #_name_, \
4859 args->fc_attrs._name_, \
4860 session->fc_attrs._name_)
4862 #define _verify_back_channel_attr(_name_) \
4863 _verify_channel_attr("back", #_name_, \
4864 args->bc_attrs._name_, \
4865 session->bc_attrs._name_)
4868 * The server is not allowed to increase the fore channel header pad size,
4869 * maximum response size, or maximum number of operations.
4871 * The back channel attributes are only negotiatied down: We send what the
4872 * (back channel) server insists upon.
4874 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args
*args
,
4875 struct nfs4_session
*session
)
4879 ret
|= _verify_fore_channel_attr(headerpadsz
);
4880 ret
|= _verify_fore_channel_attr(max_resp_sz
);
4881 ret
|= _verify_fore_channel_attr(max_ops
);
4883 ret
|= _verify_back_channel_attr(headerpadsz
);
4884 ret
|= _verify_back_channel_attr(max_rqst_sz
);
4885 ret
|= _verify_back_channel_attr(max_resp_sz
);
4886 ret
|= _verify_back_channel_attr(max_resp_sz_cached
);
4887 ret
|= _verify_back_channel_attr(max_ops
);
4888 ret
|= _verify_back_channel_attr(max_reqs
);
4893 static int _nfs4_proc_create_session(struct nfs_client
*clp
)
4895 struct nfs4_session
*session
= clp
->cl_session
;
4896 struct nfs41_create_session_args args
= {
4898 .cb_program
= NFS4_CALLBACK
,
4900 struct nfs41_create_session_res res
= {
4903 struct rpc_message msg
= {
4904 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_CREATE_SESSION
],
4910 nfs4_init_channel_attrs(&args
);
4911 args
.flags
= (SESSION4_PERSIST
| SESSION4_BACK_CHAN
);
4913 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4916 /* Verify the session's negotiated channel_attrs values */
4917 status
= nfs4_verify_channel_attrs(&args
, session
);
4919 /* Increment the clientid slot sequence id */
4927 * Issues a CREATE_SESSION operation to the server.
4928 * It is the responsibility of the caller to verify the session is
4929 * expired before calling this routine.
4931 int nfs4_proc_create_session(struct nfs_client
*clp
)
4935 struct nfs4_session
*session
= clp
->cl_session
;
4937 dprintk("--> %s clp=%p session=%p\n", __func__
, clp
, session
);
4939 status
= _nfs4_proc_create_session(clp
);
4943 /* Init and reset the fore channel */
4944 status
= nfs4_init_slot_tables(session
);
4945 dprintk("slot table initialization returned %d\n", status
);
4948 status
= nfs4_reset_slot_tables(session
);
4949 dprintk("slot table reset returned %d\n", status
);
4953 ptr
= (unsigned *)&session
->sess_id
.data
[0];
4954 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__
,
4955 clp
->cl_seqid
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
4957 dprintk("<-- %s\n", __func__
);
4962 * Issue the over-the-wire RPC DESTROY_SESSION.
4963 * The caller must serialize access to this routine.
4965 int nfs4_proc_destroy_session(struct nfs4_session
*session
)
4968 struct rpc_message msg
;
4970 dprintk("--> nfs4_proc_destroy_session\n");
4972 /* session is still being setup */
4973 if (session
->clp
->cl_cons_state
!= NFS_CS_READY
)
4976 msg
.rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_DESTROY_SESSION
];
4977 msg
.rpc_argp
= session
;
4978 msg
.rpc_resp
= NULL
;
4979 msg
.rpc_cred
= NULL
;
4980 status
= rpc_call_sync(session
->clp
->cl_rpcclient
, &msg
, 0);
4984 "Got error %d from the server on DESTROY_SESSION. "
4985 "Session has been destroyed regardless...\n", status
);
4987 dprintk("<-- nfs4_proc_destroy_session\n");
4991 int nfs4_init_session(struct nfs_server
*server
)
4993 struct nfs_client
*clp
= server
->nfs_client
;
4994 struct nfs4_session
*session
;
4995 unsigned int rsize
, wsize
;
4998 if (!nfs4_has_session(clp
))
5001 rsize
= server
->rsize
;
5003 rsize
= NFS_MAX_FILE_IO_SIZE
;
5004 wsize
= server
->wsize
;
5006 wsize
= NFS_MAX_FILE_IO_SIZE
;
5008 session
= clp
->cl_session
;
5009 session
->fc_attrs
.max_rqst_sz
= wsize
+ nfs41_maxwrite_overhead
;
5010 session
->fc_attrs
.max_resp_sz
= rsize
+ nfs41_maxread_overhead
;
5012 ret
= nfs4_recover_expired_lease(server
);
5014 ret
= nfs4_check_client_ready(clp
);
5019 * Renew the cl_session lease.
5021 static int nfs4_proc_sequence(struct nfs_client
*clp
, struct rpc_cred
*cred
)
5023 struct nfs4_sequence_args args
;
5024 struct nfs4_sequence_res res
;
5026 struct rpc_message msg
= {
5027 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5033 args
.sa_cache_this
= 0;
5035 return nfs4_call_sync_sequence(clp
, clp
->cl_rpcclient
, &msg
, &args
,
5036 &res
, args
.sa_cache_this
, 1);
5039 static void nfs41_sequence_release(void *data
)
5041 struct nfs_client
*clp
= (struct nfs_client
*)data
;
5043 if (atomic_read(&clp
->cl_count
) > 1)
5044 nfs4_schedule_state_renewal(clp
);
5045 nfs_put_client(clp
);
5048 static void nfs41_sequence_call_done(struct rpc_task
*task
, void *data
)
5050 struct nfs_client
*clp
= (struct nfs_client
*)data
;
5052 nfs41_sequence_done(clp
, task
->tk_msg
.rpc_resp
, task
->tk_status
);
5054 if (task
->tk_status
< 0) {
5055 dprintk("%s ERROR %d\n", __func__
, task
->tk_status
);
5056 if (atomic_read(&clp
->cl_count
) == 1)
5059 if (_nfs4_async_handle_error(task
, NULL
, clp
, NULL
)
5061 nfs_restart_rpc(task
, clp
);
5065 dprintk("%s rpc_cred %p\n", __func__
, task
->tk_msg
.rpc_cred
);
5067 kfree(task
->tk_msg
.rpc_argp
);
5068 kfree(task
->tk_msg
.rpc_resp
);
5070 dprintk("<-- %s\n", __func__
);
5073 static void nfs41_sequence_prepare(struct rpc_task
*task
, void *data
)
5075 struct nfs_client
*clp
;
5076 struct nfs4_sequence_args
*args
;
5077 struct nfs4_sequence_res
*res
;
5079 clp
= (struct nfs_client
*)data
;
5080 args
= task
->tk_msg
.rpc_argp
;
5081 res
= task
->tk_msg
.rpc_resp
;
5083 if (nfs4_setup_sequence(clp
, args
, res
, 0, task
))
5085 rpc_call_start(task
);
5088 static const struct rpc_call_ops nfs41_sequence_ops
= {
5089 .rpc_call_done
= nfs41_sequence_call_done
,
5090 .rpc_call_prepare
= nfs41_sequence_prepare
,
5091 .rpc_release
= nfs41_sequence_release
,
5094 static int nfs41_proc_async_sequence(struct nfs_client
*clp
,
5095 struct rpc_cred
*cred
)
5097 struct nfs4_sequence_args
*args
;
5098 struct nfs4_sequence_res
*res
;
5099 struct rpc_message msg
= {
5100 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_SEQUENCE
],
5104 if (!atomic_inc_not_zero(&clp
->cl_count
))
5106 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
5107 res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
5108 if (!args
|| !res
) {
5111 nfs_put_client(clp
);
5114 res
->sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5115 msg
.rpc_argp
= args
;
5118 return rpc_call_async(clp
->cl_rpcclient
, &msg
, RPC_TASK_SOFT
,
5119 &nfs41_sequence_ops
, (void *)clp
);
5122 struct nfs4_reclaim_complete_data
{
5123 struct nfs_client
*clp
;
5124 struct nfs41_reclaim_complete_args arg
;
5125 struct nfs41_reclaim_complete_res res
;
5128 static void nfs4_reclaim_complete_prepare(struct rpc_task
*task
, void *data
)
5130 struct nfs4_reclaim_complete_data
*calldata
= data
;
5132 rpc_task_set_priority(task
, RPC_PRIORITY_PRIVILEGED
);
5133 if (nfs4_setup_sequence(calldata
->clp
, &calldata
->arg
.seq_args
,
5134 &calldata
->res
.seq_res
, 0, task
))
5137 rpc_call_start(task
);
5140 static void nfs4_reclaim_complete_done(struct rpc_task
*task
, void *data
)
5142 struct nfs4_reclaim_complete_data
*calldata
= data
;
5143 struct nfs_client
*clp
= calldata
->clp
;
5144 struct nfs4_sequence_res
*res
= &calldata
->res
.seq_res
;
5146 dprintk("--> %s\n", __func__
);
5147 nfs41_sequence_done(clp
, res
, task
->tk_status
);
5148 switch (task
->tk_status
) {
5150 case -NFS4ERR_COMPLETE_ALREADY
:
5152 case -NFS4ERR_BADSESSION
:
5153 case -NFS4ERR_DEADSESSION
:
5155 * Handle the session error, but do not retry the operation, as
5156 * we have no way of telling whether the clientid had to be
5157 * reset before we got our reply. If reset, a new wave of
5158 * reclaim operations will follow, containing their own reclaim
5159 * complete. We don't want our retry to get on the way of
5160 * recovery by incorrectly indicating to the server that we're
5161 * done reclaiming state since the process had to be restarted.
5163 _nfs4_async_handle_error(task
, NULL
, clp
, NULL
);
5166 if (_nfs4_async_handle_error(
5167 task
, NULL
, clp
, NULL
) == -EAGAIN
) {
5168 rpc_restart_call_prepare(task
);
5173 dprintk("<-- %s\n", __func__
);
5176 static void nfs4_free_reclaim_complete_data(void *data
)
5178 struct nfs4_reclaim_complete_data
*calldata
= data
;
5183 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops
= {
5184 .rpc_call_prepare
= nfs4_reclaim_complete_prepare
,
5185 .rpc_call_done
= nfs4_reclaim_complete_done
,
5186 .rpc_release
= nfs4_free_reclaim_complete_data
,
5190 * Issue a global reclaim complete.
5192 static int nfs41_proc_reclaim_complete(struct nfs_client
*clp
)
5194 struct nfs4_reclaim_complete_data
*calldata
;
5195 struct rpc_task
*task
;
5196 struct rpc_message msg
= {
5197 .rpc_proc
= &nfs4_procedures
[NFSPROC4_CLNT_RECLAIM_COMPLETE
],
5199 struct rpc_task_setup task_setup_data
= {
5200 .rpc_client
= clp
->cl_rpcclient
,
5201 .rpc_message
= &msg
,
5202 .callback_ops
= &nfs4_reclaim_complete_call_ops
,
5203 .flags
= RPC_TASK_ASYNC
,
5205 int status
= -ENOMEM
;
5207 dprintk("--> %s\n", __func__
);
5208 calldata
= kzalloc(sizeof(*calldata
), GFP_KERNEL
);
5209 if (calldata
== NULL
)
5211 calldata
->clp
= clp
;
5212 calldata
->arg
.one_fs
= 0;
5213 calldata
->res
.seq_res
.sr_slotid
= NFS4_MAX_SLOT_TABLE
;
5215 msg
.rpc_argp
= &calldata
->arg
;
5216 msg
.rpc_resp
= &calldata
->res
;
5217 task_setup_data
.callback_data
= calldata
;
5218 task
= rpc_run_task(&task_setup_data
);
5220 status
= PTR_ERR(task
);
5223 dprintk("<-- %s status=%d\n", __func__
, status
);
5226 #endif /* CONFIG_NFS_V4_1 */
5228 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops
= {
5229 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5230 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5231 .recover_open
= nfs4_open_reclaim
,
5232 .recover_lock
= nfs4_lock_reclaim
,
5233 .establish_clid
= nfs4_init_clientid
,
5234 .get_clid_cred
= nfs4_get_setclientid_cred
,
5237 #if defined(CONFIG_NFS_V4_1)
5238 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops
= {
5239 .owner_flag_bit
= NFS_OWNER_RECLAIM_REBOOT
,
5240 .state_flag_bit
= NFS_STATE_RECLAIM_REBOOT
,
5241 .recover_open
= nfs4_open_reclaim
,
5242 .recover_lock
= nfs4_lock_reclaim
,
5243 .establish_clid
= nfs41_init_clientid
,
5244 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5245 .reclaim_complete
= nfs41_proc_reclaim_complete
,
5247 #endif /* CONFIG_NFS_V4_1 */
5249 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops
= {
5250 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5251 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5252 .recover_open
= nfs4_open_expired
,
5253 .recover_lock
= nfs4_lock_expired
,
5254 .establish_clid
= nfs4_init_clientid
,
5255 .get_clid_cred
= nfs4_get_setclientid_cred
,
5258 #if defined(CONFIG_NFS_V4_1)
5259 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops
= {
5260 .owner_flag_bit
= NFS_OWNER_RECLAIM_NOGRACE
,
5261 .state_flag_bit
= NFS_STATE_RECLAIM_NOGRACE
,
5262 .recover_open
= nfs4_open_expired
,
5263 .recover_lock
= nfs4_lock_expired
,
5264 .establish_clid
= nfs41_init_clientid
,
5265 .get_clid_cred
= nfs4_get_exchange_id_cred
,
5267 #endif /* CONFIG_NFS_V4_1 */
5269 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops
= {
5270 .sched_state_renewal
= nfs4_proc_async_renew
,
5271 .get_state_renewal_cred_locked
= nfs4_get_renew_cred_locked
,
5272 .renew_lease
= nfs4_proc_renew
,
5275 #if defined(CONFIG_NFS_V4_1)
5276 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops
= {
5277 .sched_state_renewal
= nfs41_proc_async_sequence
,
5278 .get_state_renewal_cred_locked
= nfs4_get_machine_cred_locked
,
5279 .renew_lease
= nfs4_proc_sequence
,
5284 * Per minor version reboot and network partition recovery ops
5287 struct nfs4_state_recovery_ops
*nfs4_reboot_recovery_ops
[] = {
5288 &nfs40_reboot_recovery_ops
,
5289 #if defined(CONFIG_NFS_V4_1)
5290 &nfs41_reboot_recovery_ops
,
5294 struct nfs4_state_recovery_ops
*nfs4_nograce_recovery_ops
[] = {
5295 &nfs40_nograce_recovery_ops
,
5296 #if defined(CONFIG_NFS_V4_1)
5297 &nfs41_nograce_recovery_ops
,
5301 struct nfs4_state_maintenance_ops
*nfs4_state_renewal_ops
[] = {
5302 &nfs40_state_renewal_ops
,
5303 #if defined(CONFIG_NFS_V4_1)
5304 &nfs41_state_renewal_ops
,
5308 static const struct inode_operations nfs4_file_inode_operations
= {
5309 .permission
= nfs_permission
,
5310 .getattr
= nfs_getattr
,
5311 .setattr
= nfs_setattr
,
5312 .getxattr
= nfs4_getxattr
,
5313 .setxattr
= nfs4_setxattr
,
5314 .listxattr
= nfs4_listxattr
,
5317 const struct nfs_rpc_ops nfs_v4_clientops
= {
5318 .version
= 4, /* protocol version */
5319 .dentry_ops
= &nfs4_dentry_operations
,
5320 .dir_inode_ops
= &nfs4_dir_inode_operations
,
5321 .file_inode_ops
= &nfs4_file_inode_operations
,
5322 .getroot
= nfs4_proc_get_root
,
5323 .getattr
= nfs4_proc_getattr
,
5324 .setattr
= nfs4_proc_setattr
,
5325 .lookupfh
= nfs4_proc_lookupfh
,
5326 .lookup
= nfs4_proc_lookup
,
5327 .access
= nfs4_proc_access
,
5328 .readlink
= nfs4_proc_readlink
,
5329 .create
= nfs4_proc_create
,
5330 .remove
= nfs4_proc_remove
,
5331 .unlink_setup
= nfs4_proc_unlink_setup
,
5332 .unlink_done
= nfs4_proc_unlink_done
,
5333 .rename
= nfs4_proc_rename
,
5334 .link
= nfs4_proc_link
,
5335 .symlink
= nfs4_proc_symlink
,
5336 .mkdir
= nfs4_proc_mkdir
,
5337 .rmdir
= nfs4_proc_remove
,
5338 .readdir
= nfs4_proc_readdir
,
5339 .mknod
= nfs4_proc_mknod
,
5340 .statfs
= nfs4_proc_statfs
,
5341 .fsinfo
= nfs4_proc_fsinfo
,
5342 .pathconf
= nfs4_proc_pathconf
,
5343 .set_capabilities
= nfs4_server_capabilities
,
5344 .decode_dirent
= nfs4_decode_dirent
,
5345 .read_setup
= nfs4_proc_read_setup
,
5346 .read_done
= nfs4_read_done
,
5347 .write_setup
= nfs4_proc_write_setup
,
5348 .write_done
= nfs4_write_done
,
5349 .commit_setup
= nfs4_proc_commit_setup
,
5350 .commit_done
= nfs4_commit_done
,
5351 .lock
= nfs4_proc_lock
,
5352 .clear_acl_cache
= nfs4_zap_acl_attr
,
5353 .close_context
= nfs4_close_context
,