NFS: Readdir plus in v4
[linux-2.6/btrfs-unstable.git] / fs / nfs / nfs4proc.c
blobf5ab216e8870b671a6e452cb1ca68b8c8be75cc6
1 /*
2 * fs/nfs/nfs4proc.c
4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
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
14 * are met:
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.
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
53 #include "nfs4_fs.h"
54 #include "delegation.h"
55 #include "internal.h"
56 #include "iostat.h"
57 #include "callback.h"
59 #define NFSDBG_FACILITY NFSDBG_PROC
61 #define NFS4_POLL_RETRY_MIN (HZ/10)
62 #define NFS4_POLL_RETRY_MAX (15*HZ)
64 #define NFS4_MAX_LOOP_ON_RECOVER (10)
66 struct nfs4_opendata;
67 static int _nfs4_proc_open(struct nfs4_opendata *data);
68 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
69 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
70 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
71 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
73 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
77 /* Prevent leaks of NFSv4 errors into userland */
78 static int nfs4_map_errors(int err)
80 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
86 dprintk("%s could not handle NFSv4 error %d\n",
87 __func__, -err);
88 break;
90 return -EIO;
94 * This is our standard bitmap for GETATTR requests.
96 const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
113 const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
122 const u32 nfs4_pathconf_bitmap[2] = {
123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
128 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
135 const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
154 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
155 struct nfs4_readdir_arg *readdir)
157 __be32 *start, *p;
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
161 readdir->cookie = cookie;
162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
209 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
211 int res;
213 might_sleep();
215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
216 nfs_wait_bit_killable, TASK_KILLABLE);
217 return res;
220 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
222 int res = 0;
224 might_sleep();
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
237 /* This is the error handling routine for processes that are allowed
238 * to sleep.
240 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
242 struct nfs_client *clp = server->nfs_client;
243 struct nfs4_state *state = exception->state;
244 int ret = errorcode;
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
256 goto do_state_recovery;
257 case -NFS4ERR_STALE_STATEID:
258 case -NFS4ERR_STALE_CLIENTID:
259 case -NFS4ERR_EXPIRED:
260 goto do_state_recovery;
261 #if defined(CONFIG_NFS_V4_1)
262 case -NFS4ERR_BADSESSION:
263 case -NFS4ERR_BADSLOT:
264 case -NFS4ERR_BAD_HIGH_SLOT:
265 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
266 case -NFS4ERR_DEADSESSION:
267 case -NFS4ERR_SEQ_FALSE_RETRY:
268 case -NFS4ERR_SEQ_MISORDERED:
269 dprintk("%s ERROR: %d Reset session\n", __func__,
270 errorcode);
271 nfs4_schedule_state_recovery(clp);
272 exception->retry = 1;
273 break;
274 #endif /* defined(CONFIG_NFS_V4_1) */
275 case -NFS4ERR_FILE_OPEN:
276 if (exception->timeout > HZ) {
277 /* We have retried a decent amount, time to
278 * fail
280 ret = -EBUSY;
281 break;
283 case -NFS4ERR_GRACE:
284 case -NFS4ERR_DELAY:
285 case -EKEYEXPIRED:
286 ret = nfs4_delay(server->client, &exception->timeout);
287 if (ret != 0)
288 break;
289 case -NFS4ERR_OLD_STATEID:
290 exception->retry = 1;
292 /* We failed to handle the error */
293 return nfs4_map_errors(ret);
294 do_state_recovery:
295 nfs4_schedule_state_recovery(clp);
296 ret = nfs4_wait_clnt_recover(clp);
297 if (ret == 0)
298 exception->retry = 1;
299 return ret;
303 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
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 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
313 do_renew_lease(server->nfs_client, timestamp);
316 #if defined(CONFIG_NFS_V4_1)
319 * nfs4_free_slot - free a slot and efficiently update slot table.
321 * freeing a slot is trivially done by clearing its respective bit
322 * in the bitmap.
323 * If the freed slotid equals highest_used_slotid we want to update it
324 * so that the server would be able to size down the slot table if needed,
325 * otherwise we know that the highest_used_slotid is still in use.
326 * When updating highest_used_slotid there may be "holes" in the bitmap
327 * so we need to scan down from highest_used_slotid to 0 looking for the now
328 * highest slotid in use.
329 * If none found, highest_used_slotid is set to -1.
331 * Must be called while holding tbl->slot_tbl_lock
333 static void
334 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
336 int free_slotid = free_slot - tbl->slots;
337 int slotid = free_slotid;
339 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
340 /* clear used bit in bitmap */
341 __clear_bit(slotid, tbl->used_slots);
343 /* update highest_used_slotid when it is freed */
344 if (slotid == tbl->highest_used_slotid) {
345 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
346 if (slotid < tbl->max_slots)
347 tbl->highest_used_slotid = slotid;
348 else
349 tbl->highest_used_slotid = -1;
351 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
352 free_slotid, tbl->highest_used_slotid);
356 * Signal state manager thread if session is drained
358 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
360 struct rpc_task *task;
362 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
363 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
364 if (task)
365 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
366 return;
369 if (ses->fc_slot_table.highest_used_slotid != -1)
370 return;
372 dprintk("%s COMPLETE: Session Drained\n", __func__);
373 complete(&ses->complete);
376 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
378 struct nfs4_slot_table *tbl;
380 tbl = &res->sr_session->fc_slot_table;
381 if (!res->sr_slot) {
382 /* just wake up the next guy waiting since
383 * we may have not consumed a slot after all */
384 dprintk("%s: No slot\n", __func__);
385 return;
388 spin_lock(&tbl->slot_tbl_lock);
389 nfs4_free_slot(tbl, res->sr_slot);
390 nfs41_check_drain_session_complete(res->sr_session);
391 spin_unlock(&tbl->slot_tbl_lock);
392 res->sr_slot = NULL;
395 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
397 unsigned long timestamp;
398 struct nfs_client *clp;
401 * sr_status remains 1 if an RPC level error occurred. The server
402 * may or may not have processed the sequence operation..
403 * Proceed as if the server received and processed the sequence
404 * operation.
406 if (res->sr_status == 1)
407 res->sr_status = NFS_OK;
409 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
410 if (!res->sr_slot)
411 goto out;
413 /* Check the SEQUENCE operation status */
414 switch (res->sr_status) {
415 case 0:
416 /* Update the slot's sequence and clientid lease timer */
417 ++res->sr_slot->seq_nr;
418 timestamp = res->sr_renewal_time;
419 clp = res->sr_session->clp;
420 do_renew_lease(clp, timestamp);
421 /* Check sequence flags */
422 if (atomic_read(&clp->cl_count) > 1)
423 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
424 break;
425 case -NFS4ERR_DELAY:
426 /* The server detected a resend of the RPC call and
427 * returned NFS4ERR_DELAY as per Section 2.10.6.2
428 * of RFC5661.
430 dprintk("%s: slot=%ld seq=%d: Operation in progress\n",
431 __func__,
432 res->sr_slot - res->sr_session->fc_slot_table.slots,
433 res->sr_slot->seq_nr);
434 goto out_retry;
435 default:
436 /* Just update the slot sequence no. */
437 ++res->sr_slot->seq_nr;
439 out:
440 /* The session may be reset by one of the error handlers. */
441 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
442 nfs41_sequence_free_slot(res);
443 return 1;
444 out_retry:
445 if (!rpc_restart_call(task))
446 goto out;
447 rpc_delay(task, NFS4_POLL_RETRY_MAX);
448 return 0;
451 static int nfs4_sequence_done(struct rpc_task *task,
452 struct nfs4_sequence_res *res)
454 if (res->sr_session == NULL)
455 return 1;
456 return nfs41_sequence_done(task, res);
460 * nfs4_find_slot - efficiently look for a free slot
462 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
463 * If found, we mark the slot as used, update the highest_used_slotid,
464 * and respectively set up the sequence operation args.
465 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
467 * Note: must be called with under the slot_tbl_lock.
469 static u8
470 nfs4_find_slot(struct nfs4_slot_table *tbl)
472 int slotid;
473 u8 ret_id = NFS4_MAX_SLOT_TABLE;
474 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
476 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
477 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
478 tbl->max_slots);
479 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
480 if (slotid >= tbl->max_slots)
481 goto out;
482 __set_bit(slotid, tbl->used_slots);
483 if (slotid > tbl->highest_used_slotid)
484 tbl->highest_used_slotid = slotid;
485 ret_id = slotid;
486 out:
487 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
488 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
489 return ret_id;
492 static int nfs41_setup_sequence(struct nfs4_session *session,
493 struct nfs4_sequence_args *args,
494 struct nfs4_sequence_res *res,
495 int cache_reply,
496 struct rpc_task *task)
498 struct nfs4_slot *slot;
499 struct nfs4_slot_table *tbl;
500 u8 slotid;
502 dprintk("--> %s\n", __func__);
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
505 return 0;
507 tbl = &session->fc_slot_table;
509 spin_lock(&tbl->slot_tbl_lock);
510 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
511 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
513 * The state manager will wait until the slot table is empty.
514 * Schedule the reset thread
516 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
517 spin_unlock(&tbl->slot_tbl_lock);
518 dprintk("%s Schedule Session Reset\n", __func__);
519 return -EAGAIN;
522 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
523 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
524 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
525 spin_unlock(&tbl->slot_tbl_lock);
526 dprintk("%s enforce FIFO order\n", __func__);
527 return -EAGAIN;
530 slotid = nfs4_find_slot(tbl);
531 if (slotid == NFS4_MAX_SLOT_TABLE) {
532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
533 spin_unlock(&tbl->slot_tbl_lock);
534 dprintk("<-- %s: no free slots\n", __func__);
535 return -EAGAIN;
537 spin_unlock(&tbl->slot_tbl_lock);
539 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
540 slot = tbl->slots + slotid;
541 args->sa_session = session;
542 args->sa_slotid = slotid;
543 args->sa_cache_this = cache_reply;
545 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
547 res->sr_session = session;
548 res->sr_slot = slot;
549 res->sr_renewal_time = jiffies;
550 res->sr_status_flags = 0;
552 * sr_status is only set in decode_sequence, and so will remain
553 * set to 1 if an rpc level failure occurs.
555 res->sr_status = 1;
556 return 0;
559 int nfs4_setup_sequence(const struct nfs_server *server,
560 struct nfs4_sequence_args *args,
561 struct nfs4_sequence_res *res,
562 int cache_reply,
563 struct rpc_task *task)
565 struct nfs4_session *session = nfs4_get_session(server);
566 int ret = 0;
568 if (session == NULL) {
569 args->sa_session = NULL;
570 res->sr_session = NULL;
571 goto out;
574 dprintk("--> %s clp %p session %p sr_slot %ld\n",
575 __func__, session->clp, session, res->sr_slot ?
576 res->sr_slot - session->fc_slot_table.slots : -1);
578 ret = nfs41_setup_sequence(session, args, res, cache_reply,
579 task);
580 out:
581 dprintk("<-- %s status=%d\n", __func__, ret);
582 return ret;
585 struct nfs41_call_sync_data {
586 const struct nfs_server *seq_server;
587 struct nfs4_sequence_args *seq_args;
588 struct nfs4_sequence_res *seq_res;
589 int cache_reply;
592 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
594 struct nfs41_call_sync_data *data = calldata;
596 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
598 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
599 data->seq_res, data->cache_reply, task))
600 return;
601 rpc_call_start(task);
604 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
606 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
607 nfs41_call_sync_prepare(task, calldata);
610 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
612 struct nfs41_call_sync_data *data = calldata;
614 nfs41_sequence_done(task, data->seq_res);
617 struct rpc_call_ops nfs41_call_sync_ops = {
618 .rpc_call_prepare = nfs41_call_sync_prepare,
619 .rpc_call_done = nfs41_call_sync_done,
622 struct rpc_call_ops nfs41_call_priv_sync_ops = {
623 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
624 .rpc_call_done = nfs41_call_sync_done,
627 static int nfs4_call_sync_sequence(struct nfs_server *server,
628 struct rpc_message *msg,
629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
631 int cache_reply,
632 int privileged)
634 int ret;
635 struct rpc_task *task;
636 struct nfs41_call_sync_data data = {
637 .seq_server = server,
638 .seq_args = args,
639 .seq_res = res,
640 .cache_reply = cache_reply,
642 struct rpc_task_setup task_setup = {
643 .rpc_client = server->client,
644 .rpc_message = msg,
645 .callback_ops = &nfs41_call_sync_ops,
646 .callback_data = &data
649 res->sr_slot = NULL;
650 if (privileged)
651 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
652 task = rpc_run_task(&task_setup);
653 if (IS_ERR(task))
654 ret = PTR_ERR(task);
655 else {
656 ret = task->tk_status;
657 rpc_put_task(task);
659 return ret;
662 int _nfs4_call_sync_session(struct nfs_server *server,
663 struct rpc_message *msg,
664 struct nfs4_sequence_args *args,
665 struct nfs4_sequence_res *res,
666 int cache_reply)
668 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
671 #else
672 static int nfs4_sequence_done(struct rpc_task *task,
673 struct nfs4_sequence_res *res)
675 return 1;
677 #endif /* CONFIG_NFS_V4_1 */
679 int _nfs4_call_sync(struct nfs_server *server,
680 struct rpc_message *msg,
681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
683 int cache_reply)
685 args->sa_session = res->sr_session = NULL;
686 return rpc_call_sync(server->client, msg, 0);
689 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
690 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
691 &(res)->seq_res, (cache_reply))
693 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
695 struct nfs_inode *nfsi = NFS_I(dir);
697 spin_lock(&dir->i_lock);
698 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
699 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
700 nfs_force_lookup_revalidate(dir);
701 nfsi->change_attr = cinfo->after;
702 spin_unlock(&dir->i_lock);
705 struct nfs4_opendata {
706 struct kref kref;
707 struct nfs_openargs o_arg;
708 struct nfs_openres o_res;
709 struct nfs_open_confirmargs c_arg;
710 struct nfs_open_confirmres c_res;
711 struct nfs_fattr f_attr;
712 struct nfs_fattr dir_attr;
713 struct path path;
714 struct dentry *dir;
715 struct nfs4_state_owner *owner;
716 struct nfs4_state *state;
717 struct iattr attrs;
718 unsigned long timestamp;
719 unsigned int rpc_done : 1;
720 int rpc_status;
721 int cancelled;
725 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
727 p->o_res.f_attr = &p->f_attr;
728 p->o_res.dir_attr = &p->dir_attr;
729 p->o_res.seqid = p->o_arg.seqid;
730 p->c_res.seqid = p->c_arg.seqid;
731 p->o_res.server = p->o_arg.server;
732 nfs_fattr_init(&p->f_attr);
733 nfs_fattr_init(&p->dir_attr);
736 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
737 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
738 const struct iattr *attrs,
739 gfp_t gfp_mask)
741 struct dentry *parent = dget_parent(path->dentry);
742 struct inode *dir = parent->d_inode;
743 struct nfs_server *server = NFS_SERVER(dir);
744 struct nfs4_opendata *p;
746 p = kzalloc(sizeof(*p), gfp_mask);
747 if (p == NULL)
748 goto err;
749 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
750 if (p->o_arg.seqid == NULL)
751 goto err_free;
752 path_get(path);
753 p->path = *path;
754 p->dir = parent;
755 p->owner = sp;
756 atomic_inc(&sp->so_count);
757 p->o_arg.fh = NFS_FH(dir);
758 p->o_arg.open_flags = flags;
759 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
760 p->o_arg.clientid = server->nfs_client->cl_clientid;
761 p->o_arg.id = sp->so_owner_id.id;
762 p->o_arg.name = &p->path.dentry->d_name;
763 p->o_arg.server = server;
764 p->o_arg.bitmask = server->attr_bitmask;
765 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
766 if (flags & O_CREAT) {
767 u32 *s;
769 p->o_arg.u.attrs = &p->attrs;
770 memcpy(&p->attrs, attrs, sizeof(p->attrs));
771 s = (u32 *) p->o_arg.u.verifier.data;
772 s[0] = jiffies;
773 s[1] = current->pid;
775 p->c_arg.fh = &p->o_res.fh;
776 p->c_arg.stateid = &p->o_res.stateid;
777 p->c_arg.seqid = p->o_arg.seqid;
778 nfs4_init_opendata_res(p);
779 kref_init(&p->kref);
780 return p;
781 err_free:
782 kfree(p);
783 err:
784 dput(parent);
785 return NULL;
788 static void nfs4_opendata_free(struct kref *kref)
790 struct nfs4_opendata *p = container_of(kref,
791 struct nfs4_opendata, kref);
793 nfs_free_seqid(p->o_arg.seqid);
794 if (p->state != NULL)
795 nfs4_put_open_state(p->state);
796 nfs4_put_state_owner(p->owner);
797 dput(p->dir);
798 path_put(&p->path);
799 kfree(p);
802 static void nfs4_opendata_put(struct nfs4_opendata *p)
804 if (p != NULL)
805 kref_put(&p->kref, nfs4_opendata_free);
808 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
810 int ret;
812 ret = rpc_wait_for_completion_task(task);
813 return ret;
816 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
818 int ret = 0;
820 if (open_mode & O_EXCL)
821 goto out;
822 switch (mode & (FMODE_READ|FMODE_WRITE)) {
823 case FMODE_READ:
824 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
825 && state->n_rdonly != 0;
826 break;
827 case FMODE_WRITE:
828 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
829 && state->n_wronly != 0;
830 break;
831 case FMODE_READ|FMODE_WRITE:
832 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
833 && state->n_rdwr != 0;
835 out:
836 return ret;
839 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
841 if ((delegation->type & fmode) != fmode)
842 return 0;
843 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
844 return 0;
845 nfs_mark_delegation_referenced(delegation);
846 return 1;
849 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
851 switch (fmode) {
852 case FMODE_WRITE:
853 state->n_wronly++;
854 break;
855 case FMODE_READ:
856 state->n_rdonly++;
857 break;
858 case FMODE_READ|FMODE_WRITE:
859 state->n_rdwr++;
861 nfs4_state_set_mode_locked(state, state->state | fmode);
864 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
866 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
867 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
868 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
869 switch (fmode) {
870 case FMODE_READ:
871 set_bit(NFS_O_RDONLY_STATE, &state->flags);
872 break;
873 case FMODE_WRITE:
874 set_bit(NFS_O_WRONLY_STATE, &state->flags);
875 break;
876 case FMODE_READ|FMODE_WRITE:
877 set_bit(NFS_O_RDWR_STATE, &state->flags);
881 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
883 write_seqlock(&state->seqlock);
884 nfs_set_open_stateid_locked(state, stateid, fmode);
885 write_sequnlock(&state->seqlock);
888 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
891 * Protect the call to nfs4_state_set_mode_locked and
892 * serialise the stateid update
894 write_seqlock(&state->seqlock);
895 if (deleg_stateid != NULL) {
896 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
897 set_bit(NFS_DELEGATED_STATE, &state->flags);
899 if (open_stateid != NULL)
900 nfs_set_open_stateid_locked(state, open_stateid, fmode);
901 write_sequnlock(&state->seqlock);
902 spin_lock(&state->owner->so_lock);
903 update_open_stateflags(state, fmode);
904 spin_unlock(&state->owner->so_lock);
907 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
909 struct nfs_inode *nfsi = NFS_I(state->inode);
910 struct nfs_delegation *deleg_cur;
911 int ret = 0;
913 fmode &= (FMODE_READ|FMODE_WRITE);
915 rcu_read_lock();
916 deleg_cur = rcu_dereference(nfsi->delegation);
917 if (deleg_cur == NULL)
918 goto no_delegation;
920 spin_lock(&deleg_cur->lock);
921 if (nfsi->delegation != deleg_cur ||
922 (deleg_cur->type & fmode) != fmode)
923 goto no_delegation_unlock;
925 if (delegation == NULL)
926 delegation = &deleg_cur->stateid;
927 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
928 goto no_delegation_unlock;
930 nfs_mark_delegation_referenced(deleg_cur);
931 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
932 ret = 1;
933 no_delegation_unlock:
934 spin_unlock(&deleg_cur->lock);
935 no_delegation:
936 rcu_read_unlock();
938 if (!ret && open_stateid != NULL) {
939 __update_open_stateid(state, open_stateid, NULL, fmode);
940 ret = 1;
943 return ret;
947 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
949 struct nfs_delegation *delegation;
951 rcu_read_lock();
952 delegation = rcu_dereference(NFS_I(inode)->delegation);
953 if (delegation == NULL || (delegation->type & fmode) == fmode) {
954 rcu_read_unlock();
955 return;
957 rcu_read_unlock();
958 nfs_inode_return_delegation(inode);
961 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
963 struct nfs4_state *state = opendata->state;
964 struct nfs_inode *nfsi = NFS_I(state->inode);
965 struct nfs_delegation *delegation;
966 int open_mode = opendata->o_arg.open_flags & O_EXCL;
967 fmode_t fmode = opendata->o_arg.fmode;
968 nfs4_stateid stateid;
969 int ret = -EAGAIN;
971 for (;;) {
972 if (can_open_cached(state, fmode, open_mode)) {
973 spin_lock(&state->owner->so_lock);
974 if (can_open_cached(state, fmode, open_mode)) {
975 update_open_stateflags(state, fmode);
976 spin_unlock(&state->owner->so_lock);
977 goto out_return_state;
979 spin_unlock(&state->owner->so_lock);
981 rcu_read_lock();
982 delegation = rcu_dereference(nfsi->delegation);
983 if (delegation == NULL ||
984 !can_open_delegated(delegation, fmode)) {
985 rcu_read_unlock();
986 break;
988 /* Save the delegation */
989 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
990 rcu_read_unlock();
991 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
992 if (ret != 0)
993 goto out;
994 ret = -EAGAIN;
996 /* Try to update the stateid using the delegation */
997 if (update_open_stateid(state, NULL, &stateid, fmode))
998 goto out_return_state;
1000 out:
1001 return ERR_PTR(ret);
1002 out_return_state:
1003 atomic_inc(&state->count);
1004 return state;
1007 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1009 struct inode *inode;
1010 struct nfs4_state *state = NULL;
1011 struct nfs_delegation *delegation;
1012 int ret;
1014 if (!data->rpc_done) {
1015 state = nfs4_try_open_cached(data);
1016 goto out;
1019 ret = -EAGAIN;
1020 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1021 goto err;
1022 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1023 ret = PTR_ERR(inode);
1024 if (IS_ERR(inode))
1025 goto err;
1026 ret = -ENOMEM;
1027 state = nfs4_get_open_state(inode, data->owner);
1028 if (state == NULL)
1029 goto err_put_inode;
1030 if (data->o_res.delegation_type != 0) {
1031 int delegation_flags = 0;
1033 rcu_read_lock();
1034 delegation = rcu_dereference(NFS_I(inode)->delegation);
1035 if (delegation)
1036 delegation_flags = delegation->flags;
1037 rcu_read_unlock();
1038 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1039 nfs_inode_set_delegation(state->inode,
1040 data->owner->so_cred,
1041 &data->o_res);
1042 else
1043 nfs_inode_reclaim_delegation(state->inode,
1044 data->owner->so_cred,
1045 &data->o_res);
1048 update_open_stateid(state, &data->o_res.stateid, NULL,
1049 data->o_arg.fmode);
1050 iput(inode);
1051 out:
1052 return state;
1053 err_put_inode:
1054 iput(inode);
1055 err:
1056 return ERR_PTR(ret);
1059 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1061 struct nfs_inode *nfsi = NFS_I(state->inode);
1062 struct nfs_open_context *ctx;
1064 spin_lock(&state->inode->i_lock);
1065 list_for_each_entry(ctx, &nfsi->open_files, list) {
1066 if (ctx->state != state)
1067 continue;
1068 get_nfs_open_context(ctx);
1069 spin_unlock(&state->inode->i_lock);
1070 return ctx;
1072 spin_unlock(&state->inode->i_lock);
1073 return ERR_PTR(-ENOENT);
1076 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1078 struct nfs4_opendata *opendata;
1080 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1081 if (opendata == NULL)
1082 return ERR_PTR(-ENOMEM);
1083 opendata->state = state;
1084 atomic_inc(&state->count);
1085 return opendata;
1088 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1090 struct nfs4_state *newstate;
1091 int ret;
1093 opendata->o_arg.open_flags = 0;
1094 opendata->o_arg.fmode = fmode;
1095 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1096 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1097 nfs4_init_opendata_res(opendata);
1098 ret = _nfs4_recover_proc_open(opendata);
1099 if (ret != 0)
1100 return ret;
1101 newstate = nfs4_opendata_to_nfs4_state(opendata);
1102 if (IS_ERR(newstate))
1103 return PTR_ERR(newstate);
1104 nfs4_close_state(&opendata->path, newstate, fmode);
1105 *res = newstate;
1106 return 0;
1109 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1111 struct nfs4_state *newstate;
1112 int ret;
1114 /* memory barrier prior to reading state->n_* */
1115 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1116 smp_rmb();
1117 if (state->n_rdwr != 0) {
1118 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1119 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1120 if (ret != 0)
1121 return ret;
1122 if (newstate != state)
1123 return -ESTALE;
1125 if (state->n_wronly != 0) {
1126 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1127 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1128 if (ret != 0)
1129 return ret;
1130 if (newstate != state)
1131 return -ESTALE;
1133 if (state->n_rdonly != 0) {
1134 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1135 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1136 if (ret != 0)
1137 return ret;
1138 if (newstate != state)
1139 return -ESTALE;
1142 * We may have performed cached opens for all three recoveries.
1143 * Check if we need to update the current stateid.
1145 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1146 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1147 write_seqlock(&state->seqlock);
1148 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1149 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1150 write_sequnlock(&state->seqlock);
1152 return 0;
1156 * OPEN_RECLAIM:
1157 * reclaim state on the server after a reboot.
1159 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1161 struct nfs_delegation *delegation;
1162 struct nfs4_opendata *opendata;
1163 fmode_t delegation_type = 0;
1164 int status;
1166 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1167 if (IS_ERR(opendata))
1168 return PTR_ERR(opendata);
1169 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1170 opendata->o_arg.fh = NFS_FH(state->inode);
1171 rcu_read_lock();
1172 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1173 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1174 delegation_type = delegation->type;
1175 rcu_read_unlock();
1176 opendata->o_arg.u.delegation_type = delegation_type;
1177 status = nfs4_open_recover(opendata, state);
1178 nfs4_opendata_put(opendata);
1179 return status;
1182 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1184 struct nfs_server *server = NFS_SERVER(state->inode);
1185 struct nfs4_exception exception = { };
1186 int err;
1187 do {
1188 err = _nfs4_do_open_reclaim(ctx, state);
1189 if (err != -NFS4ERR_DELAY)
1190 break;
1191 nfs4_handle_exception(server, err, &exception);
1192 } while (exception.retry);
1193 return err;
1196 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1198 struct nfs_open_context *ctx;
1199 int ret;
1201 ctx = nfs4_state_find_open_context(state);
1202 if (IS_ERR(ctx))
1203 return PTR_ERR(ctx);
1204 ret = nfs4_do_open_reclaim(ctx, state);
1205 put_nfs_open_context(ctx);
1206 return ret;
1209 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1211 struct nfs4_opendata *opendata;
1212 int ret;
1214 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1215 if (IS_ERR(opendata))
1216 return PTR_ERR(opendata);
1217 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1218 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1219 sizeof(opendata->o_arg.u.delegation.data));
1220 ret = nfs4_open_recover(opendata, state);
1221 nfs4_opendata_put(opendata);
1222 return ret;
1225 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1227 struct nfs4_exception exception = { };
1228 struct nfs_server *server = NFS_SERVER(state->inode);
1229 int err;
1230 do {
1231 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1232 switch (err) {
1233 case 0:
1234 case -ENOENT:
1235 case -ESTALE:
1236 goto out;
1237 case -NFS4ERR_BADSESSION:
1238 case -NFS4ERR_BADSLOT:
1239 case -NFS4ERR_BAD_HIGH_SLOT:
1240 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1241 case -NFS4ERR_DEADSESSION:
1242 nfs4_schedule_state_recovery(
1243 server->nfs_client);
1244 goto out;
1245 case -NFS4ERR_STALE_CLIENTID:
1246 case -NFS4ERR_STALE_STATEID:
1247 case -NFS4ERR_EXPIRED:
1248 /* Don't recall a delegation if it was lost */
1249 nfs4_schedule_state_recovery(server->nfs_client);
1250 goto out;
1251 case -ERESTARTSYS:
1253 * The show must go on: exit, but mark the
1254 * stateid as needing recovery.
1256 case -NFS4ERR_ADMIN_REVOKED:
1257 case -NFS4ERR_BAD_STATEID:
1258 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1259 case -EKEYEXPIRED:
1261 * User RPCSEC_GSS context has expired.
1262 * We cannot recover this stateid now, so
1263 * skip it and allow recovery thread to
1264 * proceed.
1266 case -ENOMEM:
1267 err = 0;
1268 goto out;
1270 err = nfs4_handle_exception(server, err, &exception);
1271 } while (exception.retry);
1272 out:
1273 return err;
1276 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1278 struct nfs4_opendata *data = calldata;
1280 data->rpc_status = task->tk_status;
1281 if (data->rpc_status == 0) {
1282 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1283 sizeof(data->o_res.stateid.data));
1284 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1285 renew_lease(data->o_res.server, data->timestamp);
1286 data->rpc_done = 1;
1290 static void nfs4_open_confirm_release(void *calldata)
1292 struct nfs4_opendata *data = calldata;
1293 struct nfs4_state *state = NULL;
1295 /* If this request hasn't been cancelled, do nothing */
1296 if (data->cancelled == 0)
1297 goto out_free;
1298 /* In case of error, no cleanup! */
1299 if (!data->rpc_done)
1300 goto out_free;
1301 state = nfs4_opendata_to_nfs4_state(data);
1302 if (!IS_ERR(state))
1303 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1304 out_free:
1305 nfs4_opendata_put(data);
1308 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1309 .rpc_call_done = nfs4_open_confirm_done,
1310 .rpc_release = nfs4_open_confirm_release,
1314 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1316 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1318 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1319 struct rpc_task *task;
1320 struct rpc_message msg = {
1321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1322 .rpc_argp = &data->c_arg,
1323 .rpc_resp = &data->c_res,
1324 .rpc_cred = data->owner->so_cred,
1326 struct rpc_task_setup task_setup_data = {
1327 .rpc_client = server->client,
1328 .rpc_message = &msg,
1329 .callback_ops = &nfs4_open_confirm_ops,
1330 .callback_data = data,
1331 .workqueue = nfsiod_workqueue,
1332 .flags = RPC_TASK_ASYNC,
1334 int status;
1336 kref_get(&data->kref);
1337 data->rpc_done = 0;
1338 data->rpc_status = 0;
1339 data->timestamp = jiffies;
1340 task = rpc_run_task(&task_setup_data);
1341 if (IS_ERR(task))
1342 return PTR_ERR(task);
1343 status = nfs4_wait_for_completion_rpc_task(task);
1344 if (status != 0) {
1345 data->cancelled = 1;
1346 smp_wmb();
1347 } else
1348 status = data->rpc_status;
1349 rpc_put_task(task);
1350 return status;
1353 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1355 struct nfs4_opendata *data = calldata;
1356 struct nfs4_state_owner *sp = data->owner;
1358 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1359 return;
1361 * Check if we still need to send an OPEN call, or if we can use
1362 * a delegation instead.
1364 if (data->state != NULL) {
1365 struct nfs_delegation *delegation;
1367 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1368 goto out_no_action;
1369 rcu_read_lock();
1370 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1371 if (delegation != NULL &&
1372 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1373 rcu_read_unlock();
1374 goto out_no_action;
1376 rcu_read_unlock();
1378 /* Update sequence id. */
1379 data->o_arg.id = sp->so_owner_id.id;
1380 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1381 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1382 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1383 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1385 data->timestamp = jiffies;
1386 if (nfs4_setup_sequence(data->o_arg.server,
1387 &data->o_arg.seq_args,
1388 &data->o_res.seq_res, 1, task))
1389 return;
1390 rpc_call_start(task);
1391 return;
1392 out_no_action:
1393 task->tk_action = NULL;
1397 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1399 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1400 nfs4_open_prepare(task, calldata);
1403 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1405 struct nfs4_opendata *data = calldata;
1407 data->rpc_status = task->tk_status;
1409 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1410 return;
1412 if (task->tk_status == 0) {
1413 switch (data->o_res.f_attr->mode & S_IFMT) {
1414 case S_IFREG:
1415 break;
1416 case S_IFLNK:
1417 data->rpc_status = -ELOOP;
1418 break;
1419 case S_IFDIR:
1420 data->rpc_status = -EISDIR;
1421 break;
1422 default:
1423 data->rpc_status = -ENOTDIR;
1425 renew_lease(data->o_res.server, data->timestamp);
1426 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1427 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1429 data->rpc_done = 1;
1432 static void nfs4_open_release(void *calldata)
1434 struct nfs4_opendata *data = calldata;
1435 struct nfs4_state *state = NULL;
1437 /* If this request hasn't been cancelled, do nothing */
1438 if (data->cancelled == 0)
1439 goto out_free;
1440 /* In case of error, no cleanup! */
1441 if (data->rpc_status != 0 || !data->rpc_done)
1442 goto out_free;
1443 /* In case we need an open_confirm, no cleanup! */
1444 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1445 goto out_free;
1446 state = nfs4_opendata_to_nfs4_state(data);
1447 if (!IS_ERR(state))
1448 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1449 out_free:
1450 nfs4_opendata_put(data);
1453 static const struct rpc_call_ops nfs4_open_ops = {
1454 .rpc_call_prepare = nfs4_open_prepare,
1455 .rpc_call_done = nfs4_open_done,
1456 .rpc_release = nfs4_open_release,
1459 static const struct rpc_call_ops nfs4_recover_open_ops = {
1460 .rpc_call_prepare = nfs4_recover_open_prepare,
1461 .rpc_call_done = nfs4_open_done,
1462 .rpc_release = nfs4_open_release,
1465 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1467 struct inode *dir = data->dir->d_inode;
1468 struct nfs_server *server = NFS_SERVER(dir);
1469 struct nfs_openargs *o_arg = &data->o_arg;
1470 struct nfs_openres *o_res = &data->o_res;
1471 struct rpc_task *task;
1472 struct rpc_message msg = {
1473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1474 .rpc_argp = o_arg,
1475 .rpc_resp = o_res,
1476 .rpc_cred = data->owner->so_cred,
1478 struct rpc_task_setup task_setup_data = {
1479 .rpc_client = server->client,
1480 .rpc_message = &msg,
1481 .callback_ops = &nfs4_open_ops,
1482 .callback_data = data,
1483 .workqueue = nfsiod_workqueue,
1484 .flags = RPC_TASK_ASYNC,
1486 int status;
1488 kref_get(&data->kref);
1489 data->rpc_done = 0;
1490 data->rpc_status = 0;
1491 data->cancelled = 0;
1492 if (isrecover)
1493 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1494 task = rpc_run_task(&task_setup_data);
1495 if (IS_ERR(task))
1496 return PTR_ERR(task);
1497 status = nfs4_wait_for_completion_rpc_task(task);
1498 if (status != 0) {
1499 data->cancelled = 1;
1500 smp_wmb();
1501 } else
1502 status = data->rpc_status;
1503 rpc_put_task(task);
1505 return status;
1508 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1510 struct inode *dir = data->dir->d_inode;
1511 struct nfs_openres *o_res = &data->o_res;
1512 int status;
1514 status = nfs4_run_open_task(data, 1);
1515 if (status != 0 || !data->rpc_done)
1516 return status;
1518 nfs_refresh_inode(dir, o_res->dir_attr);
1520 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1521 status = _nfs4_proc_open_confirm(data);
1522 if (status != 0)
1523 return status;
1526 return status;
1530 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1532 static int _nfs4_proc_open(struct nfs4_opendata *data)
1534 struct inode *dir = data->dir->d_inode;
1535 struct nfs_server *server = NFS_SERVER(dir);
1536 struct nfs_openargs *o_arg = &data->o_arg;
1537 struct nfs_openres *o_res = &data->o_res;
1538 int status;
1540 status = nfs4_run_open_task(data, 0);
1541 if (status != 0 || !data->rpc_done)
1542 return status;
1544 if (o_arg->open_flags & O_CREAT) {
1545 update_changeattr(dir, &o_res->cinfo);
1546 nfs_post_op_update_inode(dir, o_res->dir_attr);
1547 } else
1548 nfs_refresh_inode(dir, o_res->dir_attr);
1549 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1550 server->caps &= ~NFS_CAP_POSIX_LOCK;
1551 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1552 status = _nfs4_proc_open_confirm(data);
1553 if (status != 0)
1554 return status;
1556 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1557 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1558 return 0;
1561 static int nfs4_recover_expired_lease(struct nfs_server *server)
1563 struct nfs_client *clp = server->nfs_client;
1564 unsigned int loop;
1565 int ret;
1567 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1568 ret = nfs4_wait_clnt_recover(clp);
1569 if (ret != 0)
1570 break;
1571 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1572 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1573 break;
1574 nfs4_schedule_state_recovery(clp);
1575 ret = -EIO;
1577 return ret;
1581 * OPEN_EXPIRED:
1582 * reclaim state on the server after a network partition.
1583 * Assumes caller holds the appropriate lock
1585 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1587 struct nfs4_opendata *opendata;
1588 int ret;
1590 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1591 if (IS_ERR(opendata))
1592 return PTR_ERR(opendata);
1593 ret = nfs4_open_recover(opendata, state);
1594 if (ret == -ESTALE)
1595 d_drop(ctx->path.dentry);
1596 nfs4_opendata_put(opendata);
1597 return ret;
1600 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1602 struct nfs_server *server = NFS_SERVER(state->inode);
1603 struct nfs4_exception exception = { };
1604 int err;
1606 do {
1607 err = _nfs4_open_expired(ctx, state);
1608 switch (err) {
1609 default:
1610 goto out;
1611 case -NFS4ERR_GRACE:
1612 case -NFS4ERR_DELAY:
1613 nfs4_handle_exception(server, err, &exception);
1614 err = 0;
1616 } while (exception.retry);
1617 out:
1618 return err;
1621 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1623 struct nfs_open_context *ctx;
1624 int ret;
1626 ctx = nfs4_state_find_open_context(state);
1627 if (IS_ERR(ctx))
1628 return PTR_ERR(ctx);
1629 ret = nfs4_do_open_expired(ctx, state);
1630 put_nfs_open_context(ctx);
1631 return ret;
1635 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1636 * fields corresponding to attributes that were used to store the verifier.
1637 * Make sure we clobber those fields in the later setattr call
1639 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1641 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1642 !(sattr->ia_valid & ATTR_ATIME_SET))
1643 sattr->ia_valid |= ATTR_ATIME;
1645 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1646 !(sattr->ia_valid & ATTR_MTIME_SET))
1647 sattr->ia_valid |= ATTR_MTIME;
1651 * Returns a referenced nfs4_state
1653 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)
1655 struct nfs4_state_owner *sp;
1656 struct nfs4_state *state = NULL;
1657 struct nfs_server *server = NFS_SERVER(dir);
1658 struct nfs4_opendata *opendata;
1659 int status;
1661 /* Protect against reboot recovery conflicts */
1662 status = -ENOMEM;
1663 if (!(sp = nfs4_get_state_owner(server, cred))) {
1664 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1665 goto out_err;
1667 status = nfs4_recover_expired_lease(server);
1668 if (status != 0)
1669 goto err_put_state_owner;
1670 if (path->dentry->d_inode != NULL)
1671 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1672 status = -ENOMEM;
1673 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1674 if (opendata == NULL)
1675 goto err_put_state_owner;
1677 if (path->dentry->d_inode != NULL)
1678 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1680 status = _nfs4_proc_open(opendata);
1681 if (status != 0)
1682 goto err_opendata_put;
1684 state = nfs4_opendata_to_nfs4_state(opendata);
1685 status = PTR_ERR(state);
1686 if (IS_ERR(state))
1687 goto err_opendata_put;
1688 if (server->caps & NFS_CAP_POSIX_LOCK)
1689 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1691 if (opendata->o_arg.open_flags & O_EXCL) {
1692 nfs4_exclusive_attrset(opendata, sattr);
1694 nfs_fattr_init(opendata->o_res.f_attr);
1695 status = nfs4_do_setattr(state->inode, cred,
1696 opendata->o_res.f_attr, sattr,
1697 state);
1698 if (status == 0)
1699 nfs_setattr_update_inode(state->inode, sattr);
1700 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1702 nfs4_opendata_put(opendata);
1703 nfs4_put_state_owner(sp);
1704 *res = state;
1705 return 0;
1706 err_opendata_put:
1707 nfs4_opendata_put(opendata);
1708 err_put_state_owner:
1709 nfs4_put_state_owner(sp);
1710 out_err:
1711 *res = NULL;
1712 return status;
1716 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)
1718 struct nfs4_exception exception = { };
1719 struct nfs4_state *res;
1720 int status;
1722 do {
1723 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1724 if (status == 0)
1725 break;
1726 /* NOTE: BAD_SEQID means the server and client disagree about the
1727 * book-keeping w.r.t. state-changing operations
1728 * (OPEN/CLOSE/LOCK/LOCKU...)
1729 * It is actually a sign of a bug on the client or on the server.
1731 * If we receive a BAD_SEQID error in the particular case of
1732 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1733 * have unhashed the old state_owner for us, and that we can
1734 * therefore safely retry using a new one. We should still warn
1735 * the user though...
1737 if (status == -NFS4ERR_BAD_SEQID) {
1738 printk(KERN_WARNING "NFS: v4 server %s "
1739 " returned a bad sequence-id error!\n",
1740 NFS_SERVER(dir)->nfs_client->cl_hostname);
1741 exception.retry = 1;
1742 continue;
1745 * BAD_STATEID on OPEN means that the server cancelled our
1746 * state before it received the OPEN_CONFIRM.
1747 * Recover by retrying the request as per the discussion
1748 * on Page 181 of RFC3530.
1750 if (status == -NFS4ERR_BAD_STATEID) {
1751 exception.retry = 1;
1752 continue;
1754 if (status == -EAGAIN) {
1755 /* We must have found a delegation */
1756 exception.retry = 1;
1757 continue;
1759 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1760 status, &exception));
1761 } while (exception.retry);
1762 return res;
1765 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1766 struct nfs_fattr *fattr, struct iattr *sattr,
1767 struct nfs4_state *state)
1769 struct nfs_server *server = NFS_SERVER(inode);
1770 struct nfs_setattrargs arg = {
1771 .fh = NFS_FH(inode),
1772 .iap = sattr,
1773 .server = server,
1774 .bitmask = server->attr_bitmask,
1776 struct nfs_setattrres res = {
1777 .fattr = fattr,
1778 .server = server,
1780 struct rpc_message msg = {
1781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1782 .rpc_argp = &arg,
1783 .rpc_resp = &res,
1784 .rpc_cred = cred,
1786 unsigned long timestamp = jiffies;
1787 int status;
1789 nfs_fattr_init(fattr);
1791 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1792 /* Use that stateid */
1793 } else if (state != NULL) {
1794 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1795 } else
1796 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1798 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1799 if (status == 0 && state != NULL)
1800 renew_lease(server, timestamp);
1801 return status;
1804 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1805 struct nfs_fattr *fattr, struct iattr *sattr,
1806 struct nfs4_state *state)
1808 struct nfs_server *server = NFS_SERVER(inode);
1809 struct nfs4_exception exception = { };
1810 int err;
1811 do {
1812 err = nfs4_handle_exception(server,
1813 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1814 &exception);
1815 } while (exception.retry);
1816 return err;
1819 struct nfs4_closedata {
1820 struct path path;
1821 struct inode *inode;
1822 struct nfs4_state *state;
1823 struct nfs_closeargs arg;
1824 struct nfs_closeres res;
1825 struct nfs_fattr fattr;
1826 unsigned long timestamp;
1829 static void nfs4_free_closedata(void *data)
1831 struct nfs4_closedata *calldata = data;
1832 struct nfs4_state_owner *sp = calldata->state->owner;
1834 nfs4_put_open_state(calldata->state);
1835 nfs_free_seqid(calldata->arg.seqid);
1836 nfs4_put_state_owner(sp);
1837 path_put(&calldata->path);
1838 kfree(calldata);
1841 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1842 fmode_t fmode)
1844 spin_lock(&state->owner->so_lock);
1845 if (!(fmode & FMODE_READ))
1846 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1847 if (!(fmode & FMODE_WRITE))
1848 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1849 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1850 spin_unlock(&state->owner->so_lock);
1853 static void nfs4_close_done(struct rpc_task *task, void *data)
1855 struct nfs4_closedata *calldata = data;
1856 struct nfs4_state *state = calldata->state;
1857 struct nfs_server *server = NFS_SERVER(calldata->inode);
1859 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1860 return;
1861 /* hmm. we are done with the inode, and in the process of freeing
1862 * the state_owner. we keep this around to process errors
1864 switch (task->tk_status) {
1865 case 0:
1866 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1867 renew_lease(server, calldata->timestamp);
1868 nfs4_close_clear_stateid_flags(state,
1869 calldata->arg.fmode);
1870 break;
1871 case -NFS4ERR_STALE_STATEID:
1872 case -NFS4ERR_OLD_STATEID:
1873 case -NFS4ERR_BAD_STATEID:
1874 case -NFS4ERR_EXPIRED:
1875 if (calldata->arg.fmode == 0)
1876 break;
1877 default:
1878 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1879 rpc_restart_call_prepare(task);
1881 nfs_release_seqid(calldata->arg.seqid);
1882 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1885 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1887 struct nfs4_closedata *calldata = data;
1888 struct nfs4_state *state = calldata->state;
1889 int call_close = 0;
1891 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1892 return;
1894 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1895 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1896 spin_lock(&state->owner->so_lock);
1897 /* Calculate the change in open mode */
1898 if (state->n_rdwr == 0) {
1899 if (state->n_rdonly == 0) {
1900 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1901 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1902 calldata->arg.fmode &= ~FMODE_READ;
1904 if (state->n_wronly == 0) {
1905 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1906 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1907 calldata->arg.fmode &= ~FMODE_WRITE;
1910 spin_unlock(&state->owner->so_lock);
1912 if (!call_close) {
1913 /* Note: exit _without_ calling nfs4_close_done */
1914 task->tk_action = NULL;
1915 return;
1918 if (calldata->arg.fmode == 0)
1919 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1921 nfs_fattr_init(calldata->res.fattr);
1922 calldata->timestamp = jiffies;
1923 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1924 &calldata->arg.seq_args, &calldata->res.seq_res,
1925 1, task))
1926 return;
1927 rpc_call_start(task);
1930 static const struct rpc_call_ops nfs4_close_ops = {
1931 .rpc_call_prepare = nfs4_close_prepare,
1932 .rpc_call_done = nfs4_close_done,
1933 .rpc_release = nfs4_free_closedata,
1937 * It is possible for data to be read/written from a mem-mapped file
1938 * after the sys_close call (which hits the vfs layer as a flush).
1939 * This means that we can't safely call nfsv4 close on a file until
1940 * the inode is cleared. This in turn means that we are not good
1941 * NFSv4 citizens - we do not indicate to the server to update the file's
1942 * share state even when we are done with one of the three share
1943 * stateid's in the inode.
1945 * NOTE: Caller must be holding the sp->so_owner semaphore!
1947 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
1949 struct nfs_server *server = NFS_SERVER(state->inode);
1950 struct nfs4_closedata *calldata;
1951 struct nfs4_state_owner *sp = state->owner;
1952 struct rpc_task *task;
1953 struct rpc_message msg = {
1954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1955 .rpc_cred = state->owner->so_cred,
1957 struct rpc_task_setup task_setup_data = {
1958 .rpc_client = server->client,
1959 .rpc_message = &msg,
1960 .callback_ops = &nfs4_close_ops,
1961 .workqueue = nfsiod_workqueue,
1962 .flags = RPC_TASK_ASYNC,
1964 int status = -ENOMEM;
1966 calldata = kzalloc(sizeof(*calldata), gfp_mask);
1967 if (calldata == NULL)
1968 goto out;
1969 calldata->inode = state->inode;
1970 calldata->state = state;
1971 calldata->arg.fh = NFS_FH(state->inode);
1972 calldata->arg.stateid = &state->open_stateid;
1973 /* Serialization for the sequence id */
1974 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
1975 if (calldata->arg.seqid == NULL)
1976 goto out_free_calldata;
1977 calldata->arg.fmode = 0;
1978 calldata->arg.bitmask = server->cache_consistency_bitmask;
1979 calldata->res.fattr = &calldata->fattr;
1980 calldata->res.seqid = calldata->arg.seqid;
1981 calldata->res.server = server;
1982 path_get(path);
1983 calldata->path = *path;
1985 msg.rpc_argp = &calldata->arg,
1986 msg.rpc_resp = &calldata->res,
1987 task_setup_data.callback_data = calldata;
1988 task = rpc_run_task(&task_setup_data);
1989 if (IS_ERR(task))
1990 return PTR_ERR(task);
1991 status = 0;
1992 if (wait)
1993 status = rpc_wait_for_completion_task(task);
1994 rpc_put_task(task);
1995 return status;
1996 out_free_calldata:
1997 kfree(calldata);
1998 out:
1999 nfs4_put_open_state(state);
2000 nfs4_put_state_owner(sp);
2001 return status;
2004 static struct inode *
2005 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2007 struct nfs4_state *state;
2009 /* Protect against concurrent sillydeletes */
2010 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2011 if (IS_ERR(state))
2012 return ERR_CAST(state);
2013 ctx->state = state;
2014 return igrab(state->inode);
2017 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2019 if (ctx->state == NULL)
2020 return;
2021 if (is_sync)
2022 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2023 else
2024 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2027 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2029 struct nfs4_server_caps_arg args = {
2030 .fhandle = fhandle,
2032 struct nfs4_server_caps_res res = {};
2033 struct rpc_message msg = {
2034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2035 .rpc_argp = &args,
2036 .rpc_resp = &res,
2038 int status;
2040 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2041 if (status == 0) {
2042 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2043 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2044 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2045 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2046 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2047 NFS_CAP_CTIME|NFS_CAP_MTIME);
2048 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2049 server->caps |= NFS_CAP_ACLS;
2050 if (res.has_links != 0)
2051 server->caps |= NFS_CAP_HARDLINKS;
2052 if (res.has_symlinks != 0)
2053 server->caps |= NFS_CAP_SYMLINKS;
2054 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2055 server->caps |= NFS_CAP_FILEID;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2057 server->caps |= NFS_CAP_MODE;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2059 server->caps |= NFS_CAP_NLINK;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2061 server->caps |= NFS_CAP_OWNER;
2062 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2063 server->caps |= NFS_CAP_OWNER_GROUP;
2064 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2065 server->caps |= NFS_CAP_ATIME;
2066 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2067 server->caps |= NFS_CAP_CTIME;
2068 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2069 server->caps |= NFS_CAP_MTIME;
2071 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2072 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2073 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2074 server->acl_bitmask = res.acl_bitmask;
2077 return status;
2080 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2082 struct nfs4_exception exception = { };
2083 int err;
2084 do {
2085 err = nfs4_handle_exception(server,
2086 _nfs4_server_capabilities(server, fhandle),
2087 &exception);
2088 } while (exception.retry);
2089 return err;
2092 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2093 struct nfs_fsinfo *info)
2095 struct nfs4_lookup_root_arg args = {
2096 .bitmask = nfs4_fattr_bitmap,
2098 struct nfs4_lookup_res res = {
2099 .server = server,
2100 .fattr = info->fattr,
2101 .fh = fhandle,
2103 struct rpc_message msg = {
2104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2105 .rpc_argp = &args,
2106 .rpc_resp = &res,
2109 nfs_fattr_init(info->fattr);
2110 return nfs4_call_sync(server, &msg, &args, &res, 0);
2113 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2114 struct nfs_fsinfo *info)
2116 struct nfs4_exception exception = { };
2117 int err;
2118 do {
2119 err = nfs4_handle_exception(server,
2120 _nfs4_lookup_root(server, fhandle, info),
2121 &exception);
2122 } while (exception.retry);
2123 return err;
2127 * get the file handle for the "/" directory on the server
2129 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2130 struct nfs_fsinfo *info)
2132 int status;
2134 status = nfs4_lookup_root(server, fhandle, info);
2135 if (status == 0)
2136 status = nfs4_server_capabilities(server, fhandle);
2137 if (status == 0)
2138 status = nfs4_do_fsinfo(server, fhandle, info);
2139 return nfs4_map_errors(status);
2143 * Get locations and (maybe) other attributes of a referral.
2144 * Note that we'll actually follow the referral later when
2145 * we detect fsid mismatch in inode revalidation
2147 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2149 int status = -ENOMEM;
2150 struct page *page = NULL;
2151 struct nfs4_fs_locations *locations = NULL;
2153 page = alloc_page(GFP_KERNEL);
2154 if (page == NULL)
2155 goto out;
2156 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2157 if (locations == NULL)
2158 goto out;
2160 status = nfs4_proc_fs_locations(dir, name, locations, page);
2161 if (status != 0)
2162 goto out;
2163 /* Make sure server returned a different fsid for the referral */
2164 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2165 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2166 status = -EIO;
2167 goto out;
2170 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2171 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2172 if (!fattr->mode)
2173 fattr->mode = S_IFDIR;
2174 memset(fhandle, 0, sizeof(struct nfs_fh));
2175 out:
2176 if (page)
2177 __free_page(page);
2178 kfree(locations);
2179 return status;
2182 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2184 struct nfs4_getattr_arg args = {
2185 .fh = fhandle,
2186 .bitmask = server->attr_bitmask,
2188 struct nfs4_getattr_res res = {
2189 .fattr = fattr,
2190 .server = server,
2192 struct rpc_message msg = {
2193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2194 .rpc_argp = &args,
2195 .rpc_resp = &res,
2198 nfs_fattr_init(fattr);
2199 return nfs4_call_sync(server, &msg, &args, &res, 0);
2202 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2204 struct nfs4_exception exception = { };
2205 int err;
2206 do {
2207 err = nfs4_handle_exception(server,
2208 _nfs4_proc_getattr(server, fhandle, fattr),
2209 &exception);
2210 } while (exception.retry);
2211 return err;
2215 * The file is not closed if it is opened due to the a request to change
2216 * the size of the file. The open call will not be needed once the
2217 * VFS layer lookup-intents are implemented.
2219 * Close is called when the inode is destroyed.
2220 * If we haven't opened the file for O_WRONLY, we
2221 * need to in the size_change case to obtain a stateid.
2223 * Got race?
2224 * Because OPEN is always done by name in nfsv4, it is
2225 * possible that we opened a different file by the same
2226 * name. We can recognize this race condition, but we
2227 * can't do anything about it besides returning an error.
2229 * This will be fixed with VFS changes (lookup-intent).
2231 static int
2232 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2233 struct iattr *sattr)
2235 struct inode *inode = dentry->d_inode;
2236 struct rpc_cred *cred = NULL;
2237 struct nfs4_state *state = NULL;
2238 int status;
2240 nfs_fattr_init(fattr);
2242 /* Search for an existing open(O_WRITE) file */
2243 if (sattr->ia_valid & ATTR_FILE) {
2244 struct nfs_open_context *ctx;
2246 ctx = nfs_file_open_context(sattr->ia_file);
2247 if (ctx) {
2248 cred = ctx->cred;
2249 state = ctx->state;
2253 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2254 if (status == 0)
2255 nfs_setattr_update_inode(inode, sattr);
2256 return status;
2259 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2260 const struct qstr *name, struct nfs_fh *fhandle,
2261 struct nfs_fattr *fattr)
2263 int status;
2264 struct nfs4_lookup_arg args = {
2265 .bitmask = server->attr_bitmask,
2266 .dir_fh = dirfh,
2267 .name = name,
2269 struct nfs4_lookup_res res = {
2270 .server = server,
2271 .fattr = fattr,
2272 .fh = fhandle,
2274 struct rpc_message msg = {
2275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2276 .rpc_argp = &args,
2277 .rpc_resp = &res,
2280 nfs_fattr_init(fattr);
2282 dprintk("NFS call lookupfh %s\n", name->name);
2283 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2284 dprintk("NFS reply lookupfh: %d\n", status);
2285 return status;
2288 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2289 struct qstr *name, struct nfs_fh *fhandle,
2290 struct nfs_fattr *fattr)
2292 struct nfs4_exception exception = { };
2293 int err;
2294 do {
2295 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2296 /* FIXME: !!!! */
2297 if (err == -NFS4ERR_MOVED) {
2298 err = -EREMOTE;
2299 break;
2301 err = nfs4_handle_exception(server, err, &exception);
2302 } while (exception.retry);
2303 return err;
2306 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2307 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2309 int status;
2311 dprintk("NFS call lookup %s\n", name->name);
2312 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2313 if (status == -NFS4ERR_MOVED)
2314 status = nfs4_get_referral(dir, name, fattr, fhandle);
2315 dprintk("NFS reply lookup: %d\n", status);
2316 return status;
2319 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2321 struct nfs4_exception exception = { };
2322 int err;
2323 do {
2324 err = nfs4_handle_exception(NFS_SERVER(dir),
2325 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2326 &exception);
2327 } while (exception.retry);
2328 return err;
2331 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2333 struct nfs_server *server = NFS_SERVER(inode);
2334 struct nfs4_accessargs args = {
2335 .fh = NFS_FH(inode),
2336 .bitmask = server->attr_bitmask,
2338 struct nfs4_accessres res = {
2339 .server = server,
2341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2343 .rpc_argp = &args,
2344 .rpc_resp = &res,
2345 .rpc_cred = entry->cred,
2347 int mode = entry->mask;
2348 int status;
2351 * Determine which access bits we want to ask for...
2353 if (mode & MAY_READ)
2354 args.access |= NFS4_ACCESS_READ;
2355 if (S_ISDIR(inode->i_mode)) {
2356 if (mode & MAY_WRITE)
2357 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2358 if (mode & MAY_EXEC)
2359 args.access |= NFS4_ACCESS_LOOKUP;
2360 } else {
2361 if (mode & MAY_WRITE)
2362 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2363 if (mode & MAY_EXEC)
2364 args.access |= NFS4_ACCESS_EXECUTE;
2367 res.fattr = nfs_alloc_fattr();
2368 if (res.fattr == NULL)
2369 return -ENOMEM;
2371 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2372 if (!status) {
2373 entry->mask = 0;
2374 if (res.access & NFS4_ACCESS_READ)
2375 entry->mask |= MAY_READ;
2376 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2377 entry->mask |= MAY_WRITE;
2378 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2379 entry->mask |= MAY_EXEC;
2380 nfs_refresh_inode(inode, res.fattr);
2382 nfs_free_fattr(res.fattr);
2383 return status;
2386 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2388 struct nfs4_exception exception = { };
2389 int err;
2390 do {
2391 err = nfs4_handle_exception(NFS_SERVER(inode),
2392 _nfs4_proc_access(inode, entry),
2393 &exception);
2394 } while (exception.retry);
2395 return err;
2399 * TODO: For the time being, we don't try to get any attributes
2400 * along with any of the zero-copy operations READ, READDIR,
2401 * READLINK, WRITE.
2403 * In the case of the first three, we want to put the GETATTR
2404 * after the read-type operation -- this is because it is hard
2405 * to predict the length of a GETATTR response in v4, and thus
2406 * align the READ data correctly. This means that the GETATTR
2407 * may end up partially falling into the page cache, and we should
2408 * shift it into the 'tail' of the xdr_buf before processing.
2409 * To do this efficiently, we need to know the total length
2410 * of data received, which doesn't seem to be available outside
2411 * of the RPC layer.
2413 * In the case of WRITE, we also want to put the GETATTR after
2414 * the operation -- in this case because we want to make sure
2415 * we get the post-operation mtime and size. This means that
2416 * we can't use xdr_encode_pages() as written: we need a variant
2417 * of it which would leave room in the 'tail' iovec.
2419 * Both of these changes to the XDR layer would in fact be quite
2420 * minor, but I decided to leave them for a subsequent patch.
2422 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2423 unsigned int pgbase, unsigned int pglen)
2425 struct nfs4_readlink args = {
2426 .fh = NFS_FH(inode),
2427 .pgbase = pgbase,
2428 .pglen = pglen,
2429 .pages = &page,
2431 struct nfs4_readlink_res res;
2432 struct rpc_message msg = {
2433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2434 .rpc_argp = &args,
2435 .rpc_resp = &res,
2438 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2441 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2442 unsigned int pgbase, unsigned int pglen)
2444 struct nfs4_exception exception = { };
2445 int err;
2446 do {
2447 err = nfs4_handle_exception(NFS_SERVER(inode),
2448 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2449 &exception);
2450 } while (exception.retry);
2451 return err;
2455 * Got race?
2456 * We will need to arrange for the VFS layer to provide an atomic open.
2457 * Until then, this create/open method is prone to inefficiency and race
2458 * conditions due to the lookup, create, and open VFS calls from sys_open()
2459 * placed on the wire.
2461 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2462 * The file will be opened again in the subsequent VFS open call
2463 * (nfs4_proc_file_open).
2465 * The open for read will just hang around to be used by any process that
2466 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2469 static int
2470 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2471 int flags, struct nfs_open_context *ctx)
2473 struct path my_path = {
2474 .dentry = dentry,
2476 struct path *path = &my_path;
2477 struct nfs4_state *state;
2478 struct rpc_cred *cred = NULL;
2479 fmode_t fmode = 0;
2480 int status = 0;
2482 if (ctx != NULL) {
2483 cred = ctx->cred;
2484 path = &ctx->path;
2485 fmode = ctx->mode;
2487 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2488 d_drop(dentry);
2489 if (IS_ERR(state)) {
2490 status = PTR_ERR(state);
2491 goto out;
2493 d_add(dentry, igrab(state->inode));
2494 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2495 if (ctx != NULL)
2496 ctx->state = state;
2497 else
2498 nfs4_close_sync(path, state, fmode);
2499 out:
2500 return status;
2503 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2505 struct nfs_server *server = NFS_SERVER(dir);
2506 struct nfs_removeargs args = {
2507 .fh = NFS_FH(dir),
2508 .name.len = name->len,
2509 .name.name = name->name,
2510 .bitmask = server->attr_bitmask,
2512 struct nfs_removeres res = {
2513 .server = server,
2515 struct rpc_message msg = {
2516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2517 .rpc_argp = &args,
2518 .rpc_resp = &res,
2520 int status = -ENOMEM;
2522 res.dir_attr = nfs_alloc_fattr();
2523 if (res.dir_attr == NULL)
2524 goto out;
2526 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2527 if (status == 0) {
2528 update_changeattr(dir, &res.cinfo);
2529 nfs_post_op_update_inode(dir, res.dir_attr);
2531 nfs_free_fattr(res.dir_attr);
2532 out:
2533 return status;
2536 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2538 struct nfs4_exception exception = { };
2539 int err;
2540 do {
2541 err = nfs4_handle_exception(NFS_SERVER(dir),
2542 _nfs4_proc_remove(dir, name),
2543 &exception);
2544 } while (exception.retry);
2545 return err;
2548 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2550 struct nfs_server *server = NFS_SERVER(dir);
2551 struct nfs_removeargs *args = msg->rpc_argp;
2552 struct nfs_removeres *res = msg->rpc_resp;
2554 args->bitmask = server->cache_consistency_bitmask;
2555 res->server = server;
2556 res->seq_res.sr_slot = NULL;
2557 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2560 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2562 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2564 if (!nfs4_sequence_done(task, &res->seq_res))
2565 return 0;
2566 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2567 return 0;
2568 update_changeattr(dir, &res->cinfo);
2569 nfs_post_op_update_inode(dir, res->dir_attr);
2570 return 1;
2573 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2575 struct nfs_server *server = NFS_SERVER(dir);
2576 struct nfs_renameargs *arg = msg->rpc_argp;
2577 struct nfs_renameres *res = msg->rpc_resp;
2579 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2580 arg->bitmask = server->attr_bitmask;
2581 res->server = server;
2584 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2585 struct inode *new_dir)
2587 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2589 if (!nfs4_sequence_done(task, &res->seq_res))
2590 return 0;
2591 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2592 return 0;
2594 update_changeattr(old_dir, &res->old_cinfo);
2595 nfs_post_op_update_inode(old_dir, res->old_fattr);
2596 update_changeattr(new_dir, &res->new_cinfo);
2597 nfs_post_op_update_inode(new_dir, res->new_fattr);
2598 return 1;
2601 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2602 struct inode *new_dir, struct qstr *new_name)
2604 struct nfs_server *server = NFS_SERVER(old_dir);
2605 struct nfs_renameargs arg = {
2606 .old_dir = NFS_FH(old_dir),
2607 .new_dir = NFS_FH(new_dir),
2608 .old_name = old_name,
2609 .new_name = new_name,
2610 .bitmask = server->attr_bitmask,
2612 struct nfs_renameres res = {
2613 .server = server,
2615 struct rpc_message msg = {
2616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2617 .rpc_argp = &arg,
2618 .rpc_resp = &res,
2620 int status = -ENOMEM;
2622 res.old_fattr = nfs_alloc_fattr();
2623 res.new_fattr = nfs_alloc_fattr();
2624 if (res.old_fattr == NULL || res.new_fattr == NULL)
2625 goto out;
2627 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2628 if (!status) {
2629 update_changeattr(old_dir, &res.old_cinfo);
2630 nfs_post_op_update_inode(old_dir, res.old_fattr);
2631 update_changeattr(new_dir, &res.new_cinfo);
2632 nfs_post_op_update_inode(new_dir, res.new_fattr);
2634 out:
2635 nfs_free_fattr(res.new_fattr);
2636 nfs_free_fattr(res.old_fattr);
2637 return status;
2640 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2641 struct inode *new_dir, struct qstr *new_name)
2643 struct nfs4_exception exception = { };
2644 int err;
2645 do {
2646 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2647 _nfs4_proc_rename(old_dir, old_name,
2648 new_dir, new_name),
2649 &exception);
2650 } while (exception.retry);
2651 return err;
2654 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2656 struct nfs_server *server = NFS_SERVER(inode);
2657 struct nfs4_link_arg arg = {
2658 .fh = NFS_FH(inode),
2659 .dir_fh = NFS_FH(dir),
2660 .name = name,
2661 .bitmask = server->attr_bitmask,
2663 struct nfs4_link_res res = {
2664 .server = server,
2666 struct rpc_message msg = {
2667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2668 .rpc_argp = &arg,
2669 .rpc_resp = &res,
2671 int status = -ENOMEM;
2673 res.fattr = nfs_alloc_fattr();
2674 res.dir_attr = nfs_alloc_fattr();
2675 if (res.fattr == NULL || res.dir_attr == NULL)
2676 goto out;
2678 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2679 if (!status) {
2680 update_changeattr(dir, &res.cinfo);
2681 nfs_post_op_update_inode(dir, res.dir_attr);
2682 nfs_post_op_update_inode(inode, res.fattr);
2684 out:
2685 nfs_free_fattr(res.dir_attr);
2686 nfs_free_fattr(res.fattr);
2687 return status;
2690 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2692 struct nfs4_exception exception = { };
2693 int err;
2694 do {
2695 err = nfs4_handle_exception(NFS_SERVER(inode),
2696 _nfs4_proc_link(inode, dir, name),
2697 &exception);
2698 } while (exception.retry);
2699 return err;
2702 struct nfs4_createdata {
2703 struct rpc_message msg;
2704 struct nfs4_create_arg arg;
2705 struct nfs4_create_res res;
2706 struct nfs_fh fh;
2707 struct nfs_fattr fattr;
2708 struct nfs_fattr dir_fattr;
2711 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2712 struct qstr *name, struct iattr *sattr, u32 ftype)
2714 struct nfs4_createdata *data;
2716 data = kzalloc(sizeof(*data), GFP_KERNEL);
2717 if (data != NULL) {
2718 struct nfs_server *server = NFS_SERVER(dir);
2720 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2721 data->msg.rpc_argp = &data->arg;
2722 data->msg.rpc_resp = &data->res;
2723 data->arg.dir_fh = NFS_FH(dir);
2724 data->arg.server = server;
2725 data->arg.name = name;
2726 data->arg.attrs = sattr;
2727 data->arg.ftype = ftype;
2728 data->arg.bitmask = server->attr_bitmask;
2729 data->res.server = server;
2730 data->res.fh = &data->fh;
2731 data->res.fattr = &data->fattr;
2732 data->res.dir_fattr = &data->dir_fattr;
2733 nfs_fattr_init(data->res.fattr);
2734 nfs_fattr_init(data->res.dir_fattr);
2736 return data;
2739 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2741 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2742 &data->arg, &data->res, 1);
2743 if (status == 0) {
2744 update_changeattr(dir, &data->res.dir_cinfo);
2745 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2746 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2748 return status;
2751 static void nfs4_free_createdata(struct nfs4_createdata *data)
2753 kfree(data);
2756 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2757 struct page *page, unsigned int len, struct iattr *sattr)
2759 struct nfs4_createdata *data;
2760 int status = -ENAMETOOLONG;
2762 if (len > NFS4_MAXPATHLEN)
2763 goto out;
2765 status = -ENOMEM;
2766 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2767 if (data == NULL)
2768 goto out;
2770 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2771 data->arg.u.symlink.pages = &page;
2772 data->arg.u.symlink.len = len;
2774 status = nfs4_do_create(dir, dentry, data);
2776 nfs4_free_createdata(data);
2777 out:
2778 return status;
2781 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2782 struct page *page, unsigned int len, struct iattr *sattr)
2784 struct nfs4_exception exception = { };
2785 int err;
2786 do {
2787 err = nfs4_handle_exception(NFS_SERVER(dir),
2788 _nfs4_proc_symlink(dir, dentry, page,
2789 len, sattr),
2790 &exception);
2791 } while (exception.retry);
2792 return err;
2795 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2796 struct iattr *sattr)
2798 struct nfs4_createdata *data;
2799 int status = -ENOMEM;
2801 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2802 if (data == NULL)
2803 goto out;
2805 status = nfs4_do_create(dir, dentry, data);
2807 nfs4_free_createdata(data);
2808 out:
2809 return status;
2812 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2813 struct iattr *sattr)
2815 struct nfs4_exception exception = { };
2816 int err;
2817 do {
2818 err = nfs4_handle_exception(NFS_SERVER(dir),
2819 _nfs4_proc_mkdir(dir, dentry, sattr),
2820 &exception);
2821 } while (exception.retry);
2822 return err;
2825 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2826 u64 cookie, struct page **pages, unsigned int count, int plus)
2828 struct inode *dir = dentry->d_inode;
2829 struct nfs4_readdir_arg args = {
2830 .fh = NFS_FH(dir),
2831 .pages = pages,
2832 .pgbase = 0,
2833 .count = count,
2834 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2835 .plus = plus,
2837 struct nfs4_readdir_res res;
2838 struct rpc_message msg = {
2839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2840 .rpc_argp = &args,
2841 .rpc_resp = &res,
2842 .rpc_cred = cred,
2844 int status;
2846 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2847 dentry->d_parent->d_name.name,
2848 dentry->d_name.name,
2849 (unsigned long long)cookie);
2850 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2851 res.pgbase = args.pgbase;
2852 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2853 if (status == 0)
2854 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2856 nfs_invalidate_atime(dir);
2858 dprintk("%s: returns %d\n", __func__, status);
2859 return status;
2862 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2863 u64 cookie, struct page **pages, unsigned int count, int plus)
2865 struct nfs4_exception exception = { };
2866 int err;
2867 do {
2868 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2869 _nfs4_proc_readdir(dentry, cred, cookie,
2870 pages, count, plus),
2871 &exception);
2872 } while (exception.retry);
2873 return err;
2876 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2877 struct iattr *sattr, dev_t rdev)
2879 struct nfs4_createdata *data;
2880 int mode = sattr->ia_mode;
2881 int status = -ENOMEM;
2883 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2884 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2886 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2887 if (data == NULL)
2888 goto out;
2890 if (S_ISFIFO(mode))
2891 data->arg.ftype = NF4FIFO;
2892 else if (S_ISBLK(mode)) {
2893 data->arg.ftype = NF4BLK;
2894 data->arg.u.device.specdata1 = MAJOR(rdev);
2895 data->arg.u.device.specdata2 = MINOR(rdev);
2897 else if (S_ISCHR(mode)) {
2898 data->arg.ftype = NF4CHR;
2899 data->arg.u.device.specdata1 = MAJOR(rdev);
2900 data->arg.u.device.specdata2 = MINOR(rdev);
2903 status = nfs4_do_create(dir, dentry, data);
2905 nfs4_free_createdata(data);
2906 out:
2907 return status;
2910 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2911 struct iattr *sattr, dev_t rdev)
2913 struct nfs4_exception exception = { };
2914 int err;
2915 do {
2916 err = nfs4_handle_exception(NFS_SERVER(dir),
2917 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2918 &exception);
2919 } while (exception.retry);
2920 return err;
2923 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2924 struct nfs_fsstat *fsstat)
2926 struct nfs4_statfs_arg args = {
2927 .fh = fhandle,
2928 .bitmask = server->attr_bitmask,
2930 struct nfs4_statfs_res res = {
2931 .fsstat = fsstat,
2933 struct rpc_message msg = {
2934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2935 .rpc_argp = &args,
2936 .rpc_resp = &res,
2939 nfs_fattr_init(fsstat->fattr);
2940 return nfs4_call_sync(server, &msg, &args, &res, 0);
2943 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2945 struct nfs4_exception exception = { };
2946 int err;
2947 do {
2948 err = nfs4_handle_exception(server,
2949 _nfs4_proc_statfs(server, fhandle, fsstat),
2950 &exception);
2951 } while (exception.retry);
2952 return err;
2955 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2956 struct nfs_fsinfo *fsinfo)
2958 struct nfs4_fsinfo_arg args = {
2959 .fh = fhandle,
2960 .bitmask = server->attr_bitmask,
2962 struct nfs4_fsinfo_res res = {
2963 .fsinfo = fsinfo,
2965 struct rpc_message msg = {
2966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2967 .rpc_argp = &args,
2968 .rpc_resp = &res,
2971 return nfs4_call_sync(server, &msg, &args, &res, 0);
2974 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2976 struct nfs4_exception exception = { };
2977 int err;
2979 do {
2980 err = nfs4_handle_exception(server,
2981 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2982 &exception);
2983 } while (exception.retry);
2984 return err;
2987 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2989 nfs_fattr_init(fsinfo->fattr);
2990 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2993 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2994 struct nfs_pathconf *pathconf)
2996 struct nfs4_pathconf_arg args = {
2997 .fh = fhandle,
2998 .bitmask = server->attr_bitmask,
3000 struct nfs4_pathconf_res res = {
3001 .pathconf = pathconf,
3003 struct rpc_message msg = {
3004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3005 .rpc_argp = &args,
3006 .rpc_resp = &res,
3009 /* None of the pathconf attributes are mandatory to implement */
3010 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3011 memset(pathconf, 0, sizeof(*pathconf));
3012 return 0;
3015 nfs_fattr_init(pathconf->fattr);
3016 return nfs4_call_sync(server, &msg, &args, &res, 0);
3019 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3020 struct nfs_pathconf *pathconf)
3022 struct nfs4_exception exception = { };
3023 int err;
3025 do {
3026 err = nfs4_handle_exception(server,
3027 _nfs4_proc_pathconf(server, fhandle, pathconf),
3028 &exception);
3029 } while (exception.retry);
3030 return err;
3033 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3035 struct nfs_server *server = NFS_SERVER(data->inode);
3037 dprintk("--> %s\n", __func__);
3039 if (!nfs4_sequence_done(task, &data->res.seq_res))
3040 return -EAGAIN;
3042 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3043 nfs_restart_rpc(task, server->nfs_client);
3044 return -EAGAIN;
3047 nfs_invalidate_atime(data->inode);
3048 if (task->tk_status > 0)
3049 renew_lease(server, data->timestamp);
3050 return 0;
3053 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3055 data->timestamp = jiffies;
3056 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3059 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3061 struct inode *inode = data->inode;
3063 if (!nfs4_sequence_done(task, &data->res.seq_res))
3064 return -EAGAIN;
3066 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3067 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3068 return -EAGAIN;
3070 if (task->tk_status >= 0) {
3071 renew_lease(NFS_SERVER(inode), data->timestamp);
3072 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3074 return 0;
3077 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3079 struct nfs_server *server = NFS_SERVER(data->inode);
3081 data->args.bitmask = server->cache_consistency_bitmask;
3082 data->res.server = server;
3083 data->timestamp = jiffies;
3085 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3088 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3090 struct inode *inode = data->inode;
3092 if (!nfs4_sequence_done(task, &data->res.seq_res))
3093 return -EAGAIN;
3095 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3096 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3097 return -EAGAIN;
3099 nfs_refresh_inode(inode, data->res.fattr);
3100 return 0;
3103 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3105 struct nfs_server *server = NFS_SERVER(data->inode);
3107 data->args.bitmask = server->cache_consistency_bitmask;
3108 data->res.server = server;
3109 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3112 struct nfs4_renewdata {
3113 struct nfs_client *client;
3114 unsigned long timestamp;
3118 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3119 * standalone procedure for queueing an asynchronous RENEW.
3121 static void nfs4_renew_release(void *calldata)
3123 struct nfs4_renewdata *data = calldata;
3124 struct nfs_client *clp = data->client;
3126 if (atomic_read(&clp->cl_count) > 1)
3127 nfs4_schedule_state_renewal(clp);
3128 nfs_put_client(clp);
3129 kfree(data);
3132 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3134 struct nfs4_renewdata *data = calldata;
3135 struct nfs_client *clp = data->client;
3136 unsigned long timestamp = data->timestamp;
3138 if (task->tk_status < 0) {
3139 /* Unless we're shutting down, schedule state recovery! */
3140 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3141 nfs4_schedule_state_recovery(clp);
3142 return;
3144 do_renew_lease(clp, timestamp);
3147 static const struct rpc_call_ops nfs4_renew_ops = {
3148 .rpc_call_done = nfs4_renew_done,
3149 .rpc_release = nfs4_renew_release,
3152 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3156 .rpc_argp = clp,
3157 .rpc_cred = cred,
3159 struct nfs4_renewdata *data;
3161 if (!atomic_inc_not_zero(&clp->cl_count))
3162 return -EIO;
3163 data = kmalloc(sizeof(*data), GFP_KERNEL);
3164 if (data == NULL)
3165 return -ENOMEM;
3166 data->client = clp;
3167 data->timestamp = jiffies;
3168 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3169 &nfs4_renew_ops, data);
3172 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3174 struct rpc_message msg = {
3175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3176 .rpc_argp = clp,
3177 .rpc_cred = cred,
3179 unsigned long now = jiffies;
3180 int status;
3182 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3183 if (status < 0)
3184 return status;
3185 do_renew_lease(clp, now);
3186 return 0;
3189 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3191 return (server->caps & NFS_CAP_ACLS)
3192 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3193 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3196 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3197 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3198 * the stack.
3200 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3202 static void buf_to_pages(const void *buf, size_t buflen,
3203 struct page **pages, unsigned int *pgbase)
3205 const void *p = buf;
3207 *pgbase = offset_in_page(buf);
3208 p -= *pgbase;
3209 while (p < buf + buflen) {
3210 *(pages++) = virt_to_page(p);
3211 p += PAGE_CACHE_SIZE;
3215 struct nfs4_cached_acl {
3216 int cached;
3217 size_t len;
3218 char data[0];
3221 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3223 struct nfs_inode *nfsi = NFS_I(inode);
3225 spin_lock(&inode->i_lock);
3226 kfree(nfsi->nfs4_acl);
3227 nfsi->nfs4_acl = acl;
3228 spin_unlock(&inode->i_lock);
3231 static void nfs4_zap_acl_attr(struct inode *inode)
3233 nfs4_set_cached_acl(inode, NULL);
3236 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3238 struct nfs_inode *nfsi = NFS_I(inode);
3239 struct nfs4_cached_acl *acl;
3240 int ret = -ENOENT;
3242 spin_lock(&inode->i_lock);
3243 acl = nfsi->nfs4_acl;
3244 if (acl == NULL)
3245 goto out;
3246 if (buf == NULL) /* user is just asking for length */
3247 goto out_len;
3248 if (acl->cached == 0)
3249 goto out;
3250 ret = -ERANGE; /* see getxattr(2) man page */
3251 if (acl->len > buflen)
3252 goto out;
3253 memcpy(buf, acl->data, acl->len);
3254 out_len:
3255 ret = acl->len;
3256 out:
3257 spin_unlock(&inode->i_lock);
3258 return ret;
3261 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3263 struct nfs4_cached_acl *acl;
3265 if (buf && acl_len <= PAGE_SIZE) {
3266 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3267 if (acl == NULL)
3268 goto out;
3269 acl->cached = 1;
3270 memcpy(acl->data, buf, acl_len);
3271 } else {
3272 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3273 if (acl == NULL)
3274 goto out;
3275 acl->cached = 0;
3277 acl->len = acl_len;
3278 out:
3279 nfs4_set_cached_acl(inode, acl);
3282 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3284 struct page *pages[NFS4ACL_MAXPAGES];
3285 struct nfs_getaclargs args = {
3286 .fh = NFS_FH(inode),
3287 .acl_pages = pages,
3288 .acl_len = buflen,
3290 struct nfs_getaclres res = {
3291 .acl_len = buflen,
3293 void *resp_buf;
3294 struct rpc_message msg = {
3295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3296 .rpc_argp = &args,
3297 .rpc_resp = &res,
3299 struct page *localpage = NULL;
3300 int ret;
3302 if (buflen < PAGE_SIZE) {
3303 /* As long as we're doing a round trip to the server anyway,
3304 * let's be prepared for a page of acl data. */
3305 localpage = alloc_page(GFP_KERNEL);
3306 resp_buf = page_address(localpage);
3307 if (localpage == NULL)
3308 return -ENOMEM;
3309 args.acl_pages[0] = localpage;
3310 args.acl_pgbase = 0;
3311 args.acl_len = PAGE_SIZE;
3312 } else {
3313 resp_buf = buf;
3314 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3316 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3317 if (ret)
3318 goto out_free;
3319 if (res.acl_len > args.acl_len)
3320 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3321 else
3322 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3323 if (buf) {
3324 ret = -ERANGE;
3325 if (res.acl_len > buflen)
3326 goto out_free;
3327 if (localpage)
3328 memcpy(buf, resp_buf, res.acl_len);
3330 ret = res.acl_len;
3331 out_free:
3332 if (localpage)
3333 __free_page(localpage);
3334 return ret;
3337 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3339 struct nfs4_exception exception = { };
3340 ssize_t ret;
3341 do {
3342 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3343 if (ret >= 0)
3344 break;
3345 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3346 } while (exception.retry);
3347 return ret;
3350 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3352 struct nfs_server *server = NFS_SERVER(inode);
3353 int ret;
3355 if (!nfs4_server_supports_acls(server))
3356 return -EOPNOTSUPP;
3357 ret = nfs_revalidate_inode(server, inode);
3358 if (ret < 0)
3359 return ret;
3360 ret = nfs4_read_cached_acl(inode, buf, buflen);
3361 if (ret != -ENOENT)
3362 return ret;
3363 return nfs4_get_acl_uncached(inode, buf, buflen);
3366 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3368 struct nfs_server *server = NFS_SERVER(inode);
3369 struct page *pages[NFS4ACL_MAXPAGES];
3370 struct nfs_setaclargs arg = {
3371 .fh = NFS_FH(inode),
3372 .acl_pages = pages,
3373 .acl_len = buflen,
3375 struct nfs_setaclres res;
3376 struct rpc_message msg = {
3377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3378 .rpc_argp = &arg,
3379 .rpc_resp = &res,
3381 int ret;
3383 if (!nfs4_server_supports_acls(server))
3384 return -EOPNOTSUPP;
3385 nfs_inode_return_delegation(inode);
3386 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3387 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3388 nfs_access_zap_cache(inode);
3389 nfs_zap_acl_cache(inode);
3390 return ret;
3393 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3395 struct nfs4_exception exception = { };
3396 int err;
3397 do {
3398 err = nfs4_handle_exception(NFS_SERVER(inode),
3399 __nfs4_proc_set_acl(inode, buf, buflen),
3400 &exception);
3401 } while (exception.retry);
3402 return err;
3405 static int
3406 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3408 struct nfs_client *clp = server->nfs_client;
3410 if (task->tk_status >= 0)
3411 return 0;
3412 switch(task->tk_status) {
3413 case -NFS4ERR_ADMIN_REVOKED:
3414 case -NFS4ERR_BAD_STATEID:
3415 case -NFS4ERR_OPENMODE:
3416 if (state == NULL)
3417 break;
3418 nfs4_state_mark_reclaim_nograce(clp, state);
3419 goto do_state_recovery;
3420 case -NFS4ERR_STALE_STATEID:
3421 case -NFS4ERR_STALE_CLIENTID:
3422 case -NFS4ERR_EXPIRED:
3423 goto do_state_recovery;
3424 #if defined(CONFIG_NFS_V4_1)
3425 case -NFS4ERR_BADSESSION:
3426 case -NFS4ERR_BADSLOT:
3427 case -NFS4ERR_BAD_HIGH_SLOT:
3428 case -NFS4ERR_DEADSESSION:
3429 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3430 case -NFS4ERR_SEQ_FALSE_RETRY:
3431 case -NFS4ERR_SEQ_MISORDERED:
3432 dprintk("%s ERROR %d, Reset session\n", __func__,
3433 task->tk_status);
3434 nfs4_schedule_state_recovery(clp);
3435 task->tk_status = 0;
3436 return -EAGAIN;
3437 #endif /* CONFIG_NFS_V4_1 */
3438 case -NFS4ERR_DELAY:
3439 nfs_inc_server_stats(server, NFSIOS_DELAY);
3440 case -NFS4ERR_GRACE:
3441 case -EKEYEXPIRED:
3442 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3443 task->tk_status = 0;
3444 return -EAGAIN;
3445 case -NFS4ERR_OLD_STATEID:
3446 task->tk_status = 0;
3447 return -EAGAIN;
3449 task->tk_status = nfs4_map_errors(task->tk_status);
3450 return 0;
3451 do_state_recovery:
3452 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3453 nfs4_schedule_state_recovery(clp);
3454 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3455 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3456 task->tk_status = 0;
3457 return -EAGAIN;
3460 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3461 unsigned short port, struct rpc_cred *cred,
3462 struct nfs4_setclientid_res *res)
3464 nfs4_verifier sc_verifier;
3465 struct nfs4_setclientid setclientid = {
3466 .sc_verifier = &sc_verifier,
3467 .sc_prog = program,
3469 struct rpc_message msg = {
3470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3471 .rpc_argp = &setclientid,
3472 .rpc_resp = res,
3473 .rpc_cred = cred,
3475 __be32 *p;
3476 int loop = 0;
3477 int status;
3479 p = (__be32*)sc_verifier.data;
3480 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3481 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3483 for(;;) {
3484 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3485 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3486 clp->cl_ipaddr,
3487 rpc_peeraddr2str(clp->cl_rpcclient,
3488 RPC_DISPLAY_ADDR),
3489 rpc_peeraddr2str(clp->cl_rpcclient,
3490 RPC_DISPLAY_PROTO),
3491 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3492 clp->cl_id_uniquifier);
3493 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3494 sizeof(setclientid.sc_netid),
3495 rpc_peeraddr2str(clp->cl_rpcclient,
3496 RPC_DISPLAY_NETID));
3497 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3498 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3499 clp->cl_ipaddr, port >> 8, port & 255);
3501 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3502 if (status != -NFS4ERR_CLID_INUSE)
3503 break;
3504 if (signalled())
3505 break;
3506 if (loop++ & 1)
3507 ssleep(clp->cl_lease_time + 1);
3508 else
3509 if (++clp->cl_id_uniquifier == 0)
3510 break;
3512 return status;
3515 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3516 struct nfs4_setclientid_res *arg,
3517 struct rpc_cred *cred)
3519 struct nfs_fsinfo fsinfo;
3520 struct rpc_message msg = {
3521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3522 .rpc_argp = arg,
3523 .rpc_resp = &fsinfo,
3524 .rpc_cred = cred,
3526 unsigned long now;
3527 int status;
3529 now = jiffies;
3530 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3531 if (status == 0) {
3532 spin_lock(&clp->cl_lock);
3533 clp->cl_lease_time = fsinfo.lease_time * HZ;
3534 clp->cl_last_renewal = now;
3535 spin_unlock(&clp->cl_lock);
3537 return status;
3540 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3541 struct nfs4_setclientid_res *arg,
3542 struct rpc_cred *cred)
3544 long timeout = 0;
3545 int err;
3546 do {
3547 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3548 switch (err) {
3549 case 0:
3550 return err;
3551 case -NFS4ERR_RESOURCE:
3552 /* The IBM lawyers misread another document! */
3553 case -NFS4ERR_DELAY:
3554 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3556 } while (err == 0);
3557 return err;
3560 struct nfs4_delegreturndata {
3561 struct nfs4_delegreturnargs args;
3562 struct nfs4_delegreturnres res;
3563 struct nfs_fh fh;
3564 nfs4_stateid stateid;
3565 unsigned long timestamp;
3566 struct nfs_fattr fattr;
3567 int rpc_status;
3570 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3572 struct nfs4_delegreturndata *data = calldata;
3574 if (!nfs4_sequence_done(task, &data->res.seq_res))
3575 return;
3577 switch (task->tk_status) {
3578 case -NFS4ERR_STALE_STATEID:
3579 case -NFS4ERR_EXPIRED:
3580 case 0:
3581 renew_lease(data->res.server, data->timestamp);
3582 break;
3583 default:
3584 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3585 -EAGAIN) {
3586 nfs_restart_rpc(task, data->res.server->nfs_client);
3587 return;
3590 data->rpc_status = task->tk_status;
3593 static void nfs4_delegreturn_release(void *calldata)
3595 kfree(calldata);
3598 #if defined(CONFIG_NFS_V4_1)
3599 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3601 struct nfs4_delegreturndata *d_data;
3603 d_data = (struct nfs4_delegreturndata *)data;
3605 if (nfs4_setup_sequence(d_data->res.server,
3606 &d_data->args.seq_args,
3607 &d_data->res.seq_res, 1, task))
3608 return;
3609 rpc_call_start(task);
3611 #endif /* CONFIG_NFS_V4_1 */
3613 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3614 #if defined(CONFIG_NFS_V4_1)
3615 .rpc_call_prepare = nfs4_delegreturn_prepare,
3616 #endif /* CONFIG_NFS_V4_1 */
3617 .rpc_call_done = nfs4_delegreturn_done,
3618 .rpc_release = nfs4_delegreturn_release,
3621 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3623 struct nfs4_delegreturndata *data;
3624 struct nfs_server *server = NFS_SERVER(inode);
3625 struct rpc_task *task;
3626 struct rpc_message msg = {
3627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3628 .rpc_cred = cred,
3630 struct rpc_task_setup task_setup_data = {
3631 .rpc_client = server->client,
3632 .rpc_message = &msg,
3633 .callback_ops = &nfs4_delegreturn_ops,
3634 .flags = RPC_TASK_ASYNC,
3636 int status = 0;
3638 data = kzalloc(sizeof(*data), GFP_NOFS);
3639 if (data == NULL)
3640 return -ENOMEM;
3641 data->args.fhandle = &data->fh;
3642 data->args.stateid = &data->stateid;
3643 data->args.bitmask = server->attr_bitmask;
3644 nfs_copy_fh(&data->fh, NFS_FH(inode));
3645 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3646 data->res.fattr = &data->fattr;
3647 data->res.server = server;
3648 nfs_fattr_init(data->res.fattr);
3649 data->timestamp = jiffies;
3650 data->rpc_status = 0;
3652 task_setup_data.callback_data = data;
3653 msg.rpc_argp = &data->args,
3654 msg.rpc_resp = &data->res,
3655 task = rpc_run_task(&task_setup_data);
3656 if (IS_ERR(task))
3657 return PTR_ERR(task);
3658 if (!issync)
3659 goto out;
3660 status = nfs4_wait_for_completion_rpc_task(task);
3661 if (status != 0)
3662 goto out;
3663 status = data->rpc_status;
3664 if (status != 0)
3665 goto out;
3666 nfs_refresh_inode(inode, &data->fattr);
3667 out:
3668 rpc_put_task(task);
3669 return status;
3672 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3674 struct nfs_server *server = NFS_SERVER(inode);
3675 struct nfs4_exception exception = { };
3676 int err;
3677 do {
3678 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3679 switch (err) {
3680 case -NFS4ERR_STALE_STATEID:
3681 case -NFS4ERR_EXPIRED:
3682 case 0:
3683 return 0;
3685 err = nfs4_handle_exception(server, err, &exception);
3686 } while (exception.retry);
3687 return err;
3690 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3691 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3694 * sleep, with exponential backoff, and retry the LOCK operation.
3696 static unsigned long
3697 nfs4_set_lock_task_retry(unsigned long timeout)
3699 schedule_timeout_killable(timeout);
3700 timeout <<= 1;
3701 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3702 return NFS4_LOCK_MAXTIMEOUT;
3703 return timeout;
3706 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3708 struct inode *inode = state->inode;
3709 struct nfs_server *server = NFS_SERVER(inode);
3710 struct nfs_client *clp = server->nfs_client;
3711 struct nfs_lockt_args arg = {
3712 .fh = NFS_FH(inode),
3713 .fl = request,
3715 struct nfs_lockt_res res = {
3716 .denied = request,
3718 struct rpc_message msg = {
3719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3720 .rpc_argp = &arg,
3721 .rpc_resp = &res,
3722 .rpc_cred = state->owner->so_cred,
3724 struct nfs4_lock_state *lsp;
3725 int status;
3727 arg.lock_owner.clientid = clp->cl_clientid;
3728 status = nfs4_set_lock_state(state, request);
3729 if (status != 0)
3730 goto out;
3731 lsp = request->fl_u.nfs4_fl.owner;
3732 arg.lock_owner.id = lsp->ls_id.id;
3733 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3734 switch (status) {
3735 case 0:
3736 request->fl_type = F_UNLCK;
3737 break;
3738 case -NFS4ERR_DENIED:
3739 status = 0;
3741 request->fl_ops->fl_release_private(request);
3742 out:
3743 return status;
3746 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3748 struct nfs4_exception exception = { };
3749 int err;
3751 do {
3752 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3753 _nfs4_proc_getlk(state, cmd, request),
3754 &exception);
3755 } while (exception.retry);
3756 return err;
3759 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3761 int res = 0;
3762 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3763 case FL_POSIX:
3764 res = posix_lock_file_wait(file, fl);
3765 break;
3766 case FL_FLOCK:
3767 res = flock_lock_file_wait(file, fl);
3768 break;
3769 default:
3770 BUG();
3772 return res;
3775 struct nfs4_unlockdata {
3776 struct nfs_locku_args arg;
3777 struct nfs_locku_res res;
3778 struct nfs4_lock_state *lsp;
3779 struct nfs_open_context *ctx;
3780 struct file_lock fl;
3781 const struct nfs_server *server;
3782 unsigned long timestamp;
3785 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3786 struct nfs_open_context *ctx,
3787 struct nfs4_lock_state *lsp,
3788 struct nfs_seqid *seqid)
3790 struct nfs4_unlockdata *p;
3791 struct inode *inode = lsp->ls_state->inode;
3793 p = kzalloc(sizeof(*p), GFP_NOFS);
3794 if (p == NULL)
3795 return NULL;
3796 p->arg.fh = NFS_FH(inode);
3797 p->arg.fl = &p->fl;
3798 p->arg.seqid = seqid;
3799 p->res.seqid = seqid;
3800 p->arg.stateid = &lsp->ls_stateid;
3801 p->lsp = lsp;
3802 atomic_inc(&lsp->ls_count);
3803 /* Ensure we don't close file until we're done freeing locks! */
3804 p->ctx = get_nfs_open_context(ctx);
3805 memcpy(&p->fl, fl, sizeof(p->fl));
3806 p->server = NFS_SERVER(inode);
3807 return p;
3810 static void nfs4_locku_release_calldata(void *data)
3812 struct nfs4_unlockdata *calldata = data;
3813 nfs_free_seqid(calldata->arg.seqid);
3814 nfs4_put_lock_state(calldata->lsp);
3815 put_nfs_open_context(calldata->ctx);
3816 kfree(calldata);
3819 static void nfs4_locku_done(struct rpc_task *task, void *data)
3821 struct nfs4_unlockdata *calldata = data;
3823 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3824 return;
3825 switch (task->tk_status) {
3826 case 0:
3827 memcpy(calldata->lsp->ls_stateid.data,
3828 calldata->res.stateid.data,
3829 sizeof(calldata->lsp->ls_stateid.data));
3830 renew_lease(calldata->server, calldata->timestamp);
3831 break;
3832 case -NFS4ERR_BAD_STATEID:
3833 case -NFS4ERR_OLD_STATEID:
3834 case -NFS4ERR_STALE_STATEID:
3835 case -NFS4ERR_EXPIRED:
3836 break;
3837 default:
3838 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3839 nfs_restart_rpc(task,
3840 calldata->server->nfs_client);
3844 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3846 struct nfs4_unlockdata *calldata = data;
3848 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3849 return;
3850 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3851 /* Note: exit _without_ running nfs4_locku_done */
3852 task->tk_action = NULL;
3853 return;
3855 calldata->timestamp = jiffies;
3856 if (nfs4_setup_sequence(calldata->server,
3857 &calldata->arg.seq_args,
3858 &calldata->res.seq_res, 1, task))
3859 return;
3860 rpc_call_start(task);
3863 static const struct rpc_call_ops nfs4_locku_ops = {
3864 .rpc_call_prepare = nfs4_locku_prepare,
3865 .rpc_call_done = nfs4_locku_done,
3866 .rpc_release = nfs4_locku_release_calldata,
3869 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3870 struct nfs_open_context *ctx,
3871 struct nfs4_lock_state *lsp,
3872 struct nfs_seqid *seqid)
3874 struct nfs4_unlockdata *data;
3875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3877 .rpc_cred = ctx->cred,
3879 struct rpc_task_setup task_setup_data = {
3880 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3881 .rpc_message = &msg,
3882 .callback_ops = &nfs4_locku_ops,
3883 .workqueue = nfsiod_workqueue,
3884 .flags = RPC_TASK_ASYNC,
3887 /* Ensure this is an unlock - when canceling a lock, the
3888 * canceled lock is passed in, and it won't be an unlock.
3890 fl->fl_type = F_UNLCK;
3892 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3893 if (data == NULL) {
3894 nfs_free_seqid(seqid);
3895 return ERR_PTR(-ENOMEM);
3898 msg.rpc_argp = &data->arg,
3899 msg.rpc_resp = &data->res,
3900 task_setup_data.callback_data = data;
3901 return rpc_run_task(&task_setup_data);
3904 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3906 struct nfs_inode *nfsi = NFS_I(state->inode);
3907 struct nfs_seqid *seqid;
3908 struct nfs4_lock_state *lsp;
3909 struct rpc_task *task;
3910 int status = 0;
3911 unsigned char fl_flags = request->fl_flags;
3913 status = nfs4_set_lock_state(state, request);
3914 /* Unlock _before_ we do the RPC call */
3915 request->fl_flags |= FL_EXISTS;
3916 down_read(&nfsi->rwsem);
3917 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3918 up_read(&nfsi->rwsem);
3919 goto out;
3921 up_read(&nfsi->rwsem);
3922 if (status != 0)
3923 goto out;
3924 /* Is this a delegated lock? */
3925 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3926 goto out;
3927 lsp = request->fl_u.nfs4_fl.owner;
3928 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
3929 status = -ENOMEM;
3930 if (seqid == NULL)
3931 goto out;
3932 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3933 status = PTR_ERR(task);
3934 if (IS_ERR(task))
3935 goto out;
3936 status = nfs4_wait_for_completion_rpc_task(task);
3937 rpc_put_task(task);
3938 out:
3939 request->fl_flags = fl_flags;
3940 return status;
3943 struct nfs4_lockdata {
3944 struct nfs_lock_args arg;
3945 struct nfs_lock_res res;
3946 struct nfs4_lock_state *lsp;
3947 struct nfs_open_context *ctx;
3948 struct file_lock fl;
3949 unsigned long timestamp;
3950 int rpc_status;
3951 int cancelled;
3952 struct nfs_server *server;
3955 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3956 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3957 gfp_t gfp_mask)
3959 struct nfs4_lockdata *p;
3960 struct inode *inode = lsp->ls_state->inode;
3961 struct nfs_server *server = NFS_SERVER(inode);
3963 p = kzalloc(sizeof(*p), gfp_mask);
3964 if (p == NULL)
3965 return NULL;
3967 p->arg.fh = NFS_FH(inode);
3968 p->arg.fl = &p->fl;
3969 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
3970 if (p->arg.open_seqid == NULL)
3971 goto out_free;
3972 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
3973 if (p->arg.lock_seqid == NULL)
3974 goto out_free_seqid;
3975 p->arg.lock_stateid = &lsp->ls_stateid;
3976 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3977 p->arg.lock_owner.id = lsp->ls_id.id;
3978 p->res.lock_seqid = p->arg.lock_seqid;
3979 p->lsp = lsp;
3980 p->server = server;
3981 atomic_inc(&lsp->ls_count);
3982 p->ctx = get_nfs_open_context(ctx);
3983 memcpy(&p->fl, fl, sizeof(p->fl));
3984 return p;
3985 out_free_seqid:
3986 nfs_free_seqid(p->arg.open_seqid);
3987 out_free:
3988 kfree(p);
3989 return NULL;
3992 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3994 struct nfs4_lockdata *data = calldata;
3995 struct nfs4_state *state = data->lsp->ls_state;
3997 dprintk("%s: begin!\n", __func__);
3998 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3999 return;
4000 /* Do we need to do an open_to_lock_owner? */
4001 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4002 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4003 return;
4004 data->arg.open_stateid = &state->stateid;
4005 data->arg.new_lock_owner = 1;
4006 data->res.open_seqid = data->arg.open_seqid;
4007 } else
4008 data->arg.new_lock_owner = 0;
4009 data->timestamp = jiffies;
4010 if (nfs4_setup_sequence(data->server,
4011 &data->arg.seq_args,
4012 &data->res.seq_res, 1, task))
4013 return;
4014 rpc_call_start(task);
4015 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4018 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4020 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4021 nfs4_lock_prepare(task, calldata);
4024 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4026 struct nfs4_lockdata *data = calldata;
4028 dprintk("%s: begin!\n", __func__);
4030 if (!nfs4_sequence_done(task, &data->res.seq_res))
4031 return;
4033 data->rpc_status = task->tk_status;
4034 if (data->arg.new_lock_owner != 0) {
4035 if (data->rpc_status == 0)
4036 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4037 else
4038 goto out;
4040 if (data->rpc_status == 0) {
4041 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4042 sizeof(data->lsp->ls_stateid.data));
4043 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4044 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4046 out:
4047 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4050 static void nfs4_lock_release(void *calldata)
4052 struct nfs4_lockdata *data = calldata;
4054 dprintk("%s: begin!\n", __func__);
4055 nfs_free_seqid(data->arg.open_seqid);
4056 if (data->cancelled != 0) {
4057 struct rpc_task *task;
4058 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4059 data->arg.lock_seqid);
4060 if (!IS_ERR(task))
4061 rpc_put_task(task);
4062 dprintk("%s: cancelling lock!\n", __func__);
4063 } else
4064 nfs_free_seqid(data->arg.lock_seqid);
4065 nfs4_put_lock_state(data->lsp);
4066 put_nfs_open_context(data->ctx);
4067 kfree(data);
4068 dprintk("%s: done!\n", __func__);
4071 static const struct rpc_call_ops nfs4_lock_ops = {
4072 .rpc_call_prepare = nfs4_lock_prepare,
4073 .rpc_call_done = nfs4_lock_done,
4074 .rpc_release = nfs4_lock_release,
4077 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4078 .rpc_call_prepare = nfs4_recover_lock_prepare,
4079 .rpc_call_done = nfs4_lock_done,
4080 .rpc_release = nfs4_lock_release,
4083 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4085 struct nfs_client *clp = server->nfs_client;
4086 struct nfs4_state *state = lsp->ls_state;
4088 switch (error) {
4089 case -NFS4ERR_ADMIN_REVOKED:
4090 case -NFS4ERR_BAD_STATEID:
4091 case -NFS4ERR_EXPIRED:
4092 if (new_lock_owner != 0 ||
4093 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4094 nfs4_state_mark_reclaim_nograce(clp, state);
4095 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4096 break;
4097 case -NFS4ERR_STALE_STATEID:
4098 if (new_lock_owner != 0 ||
4099 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4100 nfs4_state_mark_reclaim_reboot(clp, state);
4101 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4105 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4107 struct nfs4_lockdata *data;
4108 struct rpc_task *task;
4109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4111 .rpc_cred = state->owner->so_cred,
4113 struct rpc_task_setup task_setup_data = {
4114 .rpc_client = NFS_CLIENT(state->inode),
4115 .rpc_message = &msg,
4116 .callback_ops = &nfs4_lock_ops,
4117 .workqueue = nfsiod_workqueue,
4118 .flags = RPC_TASK_ASYNC,
4120 int ret;
4122 dprintk("%s: begin!\n", __func__);
4123 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4124 fl->fl_u.nfs4_fl.owner,
4125 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4126 if (data == NULL)
4127 return -ENOMEM;
4128 if (IS_SETLKW(cmd))
4129 data->arg.block = 1;
4130 if (recovery_type > NFS_LOCK_NEW) {
4131 if (recovery_type == NFS_LOCK_RECLAIM)
4132 data->arg.reclaim = NFS_LOCK_RECLAIM;
4133 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4135 msg.rpc_argp = &data->arg,
4136 msg.rpc_resp = &data->res,
4137 task_setup_data.callback_data = data;
4138 task = rpc_run_task(&task_setup_data);
4139 if (IS_ERR(task))
4140 return PTR_ERR(task);
4141 ret = nfs4_wait_for_completion_rpc_task(task);
4142 if (ret == 0) {
4143 ret = data->rpc_status;
4144 if (ret)
4145 nfs4_handle_setlk_error(data->server, data->lsp,
4146 data->arg.new_lock_owner, ret);
4147 } else
4148 data->cancelled = 1;
4149 rpc_put_task(task);
4150 dprintk("%s: done, ret = %d!\n", __func__, ret);
4151 return ret;
4154 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4156 struct nfs_server *server = NFS_SERVER(state->inode);
4157 struct nfs4_exception exception = { };
4158 int err;
4160 do {
4161 /* Cache the lock if possible... */
4162 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4163 return 0;
4164 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4165 if (err != -NFS4ERR_DELAY)
4166 break;
4167 nfs4_handle_exception(server, err, &exception);
4168 } while (exception.retry);
4169 return err;
4172 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4174 struct nfs_server *server = NFS_SERVER(state->inode);
4175 struct nfs4_exception exception = { };
4176 int err;
4178 err = nfs4_set_lock_state(state, request);
4179 if (err != 0)
4180 return err;
4181 do {
4182 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4183 return 0;
4184 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4185 switch (err) {
4186 default:
4187 goto out;
4188 case -NFS4ERR_GRACE:
4189 case -NFS4ERR_DELAY:
4190 nfs4_handle_exception(server, err, &exception);
4191 err = 0;
4193 } while (exception.retry);
4194 out:
4195 return err;
4198 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4200 struct nfs_inode *nfsi = NFS_I(state->inode);
4201 unsigned char fl_flags = request->fl_flags;
4202 int status = -ENOLCK;
4204 if ((fl_flags & FL_POSIX) &&
4205 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4206 goto out;
4207 /* Is this a delegated open? */
4208 status = nfs4_set_lock_state(state, request);
4209 if (status != 0)
4210 goto out;
4211 request->fl_flags |= FL_ACCESS;
4212 status = do_vfs_lock(request->fl_file, request);
4213 if (status < 0)
4214 goto out;
4215 down_read(&nfsi->rwsem);
4216 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4217 /* Yes: cache locks! */
4218 /* ...but avoid races with delegation recall... */
4219 request->fl_flags = fl_flags & ~FL_SLEEP;
4220 status = do_vfs_lock(request->fl_file, request);
4221 goto out_unlock;
4223 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4224 if (status != 0)
4225 goto out_unlock;
4226 /* Note: we always want to sleep here! */
4227 request->fl_flags = fl_flags | FL_SLEEP;
4228 if (do_vfs_lock(request->fl_file, request) < 0)
4229 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4230 out_unlock:
4231 up_read(&nfsi->rwsem);
4232 out:
4233 request->fl_flags = fl_flags;
4234 return status;
4237 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4239 struct nfs4_exception exception = { };
4240 int err;
4242 do {
4243 err = _nfs4_proc_setlk(state, cmd, request);
4244 if (err == -NFS4ERR_DENIED)
4245 err = -EAGAIN;
4246 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4247 err, &exception);
4248 } while (exception.retry);
4249 return err;
4252 static int
4253 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4255 struct nfs_open_context *ctx;
4256 struct nfs4_state *state;
4257 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4258 int status;
4260 /* verify open state */
4261 ctx = nfs_file_open_context(filp);
4262 state = ctx->state;
4264 if (request->fl_start < 0 || request->fl_end < 0)
4265 return -EINVAL;
4267 if (IS_GETLK(cmd)) {
4268 if (state != NULL)
4269 return nfs4_proc_getlk(state, F_GETLK, request);
4270 return 0;
4273 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4274 return -EINVAL;
4276 if (request->fl_type == F_UNLCK) {
4277 if (state != NULL)
4278 return nfs4_proc_unlck(state, cmd, request);
4279 return 0;
4282 if (state == NULL)
4283 return -ENOLCK;
4284 do {
4285 status = nfs4_proc_setlk(state, cmd, request);
4286 if ((status != -EAGAIN) || IS_SETLK(cmd))
4287 break;
4288 timeout = nfs4_set_lock_task_retry(timeout);
4289 status = -ERESTARTSYS;
4290 if (signalled())
4291 break;
4292 } while(status < 0);
4293 return status;
4296 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4298 struct nfs_server *server = NFS_SERVER(state->inode);
4299 struct nfs4_exception exception = { };
4300 int err;
4302 err = nfs4_set_lock_state(state, fl);
4303 if (err != 0)
4304 goto out;
4305 do {
4306 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4307 switch (err) {
4308 default:
4309 printk(KERN_ERR "%s: unhandled error %d.\n",
4310 __func__, err);
4311 case 0:
4312 case -ESTALE:
4313 goto out;
4314 case -NFS4ERR_EXPIRED:
4315 case -NFS4ERR_STALE_CLIENTID:
4316 case -NFS4ERR_STALE_STATEID:
4317 case -NFS4ERR_BADSESSION:
4318 case -NFS4ERR_BADSLOT:
4319 case -NFS4ERR_BAD_HIGH_SLOT:
4320 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4321 case -NFS4ERR_DEADSESSION:
4322 nfs4_schedule_state_recovery(server->nfs_client);
4323 goto out;
4324 case -ERESTARTSYS:
4326 * The show must go on: exit, but mark the
4327 * stateid as needing recovery.
4329 case -NFS4ERR_ADMIN_REVOKED:
4330 case -NFS4ERR_BAD_STATEID:
4331 case -NFS4ERR_OPENMODE:
4332 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4333 err = 0;
4334 goto out;
4335 case -EKEYEXPIRED:
4337 * User RPCSEC_GSS context has expired.
4338 * We cannot recover this stateid now, so
4339 * skip it and allow recovery thread to
4340 * proceed.
4342 err = 0;
4343 goto out;
4344 case -ENOMEM:
4345 case -NFS4ERR_DENIED:
4346 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4347 err = 0;
4348 goto out;
4349 case -NFS4ERR_DELAY:
4350 break;
4352 err = nfs4_handle_exception(server, err, &exception);
4353 } while (exception.retry);
4354 out:
4355 return err;
4358 static void nfs4_release_lockowner_release(void *calldata)
4360 kfree(calldata);
4363 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4364 .rpc_release = nfs4_release_lockowner_release,
4367 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4369 struct nfs_server *server = lsp->ls_state->owner->so_server;
4370 struct nfs_release_lockowner_args *args;
4371 struct rpc_message msg = {
4372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4375 if (server->nfs_client->cl_mvops->minor_version != 0)
4376 return;
4377 args = kmalloc(sizeof(*args), GFP_NOFS);
4378 if (!args)
4379 return;
4380 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4381 args->lock_owner.id = lsp->ls_id.id;
4382 msg.rpc_argp = args;
4383 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4386 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4388 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4389 size_t buflen, int flags)
4391 struct inode *inode = dentry->d_inode;
4393 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4394 return -EOPNOTSUPP;
4396 return nfs4_proc_set_acl(inode, buf, buflen);
4399 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4400 * and that's what we'll do for e.g. user attributes that haven't been set.
4401 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4402 * attributes in kernel-managed attribute namespaces. */
4403 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4404 size_t buflen)
4406 struct inode *inode = dentry->d_inode;
4408 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4409 return -EOPNOTSUPP;
4411 return nfs4_proc_get_acl(inode, buf, buflen);
4414 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4416 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4418 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4419 return 0;
4420 if (buf && buflen < len)
4421 return -ERANGE;
4422 if (buf)
4423 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4424 return len;
4427 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4429 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4430 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4431 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4432 return;
4434 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4435 NFS_ATTR_FATTR_NLINK;
4436 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4437 fattr->nlink = 2;
4440 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4441 struct nfs4_fs_locations *fs_locations, struct page *page)
4443 struct nfs_server *server = NFS_SERVER(dir);
4444 u32 bitmask[2] = {
4445 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4446 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4448 struct nfs4_fs_locations_arg args = {
4449 .dir_fh = NFS_FH(dir),
4450 .name = name,
4451 .page = page,
4452 .bitmask = bitmask,
4454 struct nfs4_fs_locations_res res = {
4455 .fs_locations = fs_locations,
4457 struct rpc_message msg = {
4458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4459 .rpc_argp = &args,
4460 .rpc_resp = &res,
4462 int status;
4464 dprintk("%s: start\n", __func__);
4465 nfs_fattr_init(&fs_locations->fattr);
4466 fs_locations->server = server;
4467 fs_locations->nlocations = 0;
4468 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4469 nfs_fixup_referral_attributes(&fs_locations->fattr);
4470 dprintk("%s: returned status = %d\n", __func__, status);
4471 return status;
4474 #ifdef CONFIG_NFS_V4_1
4476 * nfs4_proc_exchange_id()
4478 * Since the clientid has expired, all compounds using sessions
4479 * associated with the stale clientid will be returning
4480 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4481 * be in some phase of session reset.
4483 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4485 nfs4_verifier verifier;
4486 struct nfs41_exchange_id_args args = {
4487 .client = clp,
4488 .flags = clp->cl_exchange_flags,
4490 struct nfs41_exchange_id_res res = {
4491 .client = clp,
4493 int status;
4494 struct rpc_message msg = {
4495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4496 .rpc_argp = &args,
4497 .rpc_resp = &res,
4498 .rpc_cred = cred,
4500 __be32 *p;
4502 dprintk("--> %s\n", __func__);
4503 BUG_ON(clp == NULL);
4505 /* Remove server-only flags */
4506 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4508 p = (u32 *)verifier.data;
4509 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4510 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4511 args.verifier = &verifier;
4513 while (1) {
4514 args.id_len = scnprintf(args.id, sizeof(args.id),
4515 "%s/%s %u",
4516 clp->cl_ipaddr,
4517 rpc_peeraddr2str(clp->cl_rpcclient,
4518 RPC_DISPLAY_ADDR),
4519 clp->cl_id_uniquifier);
4521 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4523 if (status != -NFS4ERR_CLID_INUSE)
4524 break;
4526 if (signalled())
4527 break;
4529 if (++clp->cl_id_uniquifier == 0)
4530 break;
4533 dprintk("<-- %s status= %d\n", __func__, status);
4534 return status;
4537 struct nfs4_get_lease_time_data {
4538 struct nfs4_get_lease_time_args *args;
4539 struct nfs4_get_lease_time_res *res;
4540 struct nfs_client *clp;
4543 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4544 void *calldata)
4546 int ret;
4547 struct nfs4_get_lease_time_data *data =
4548 (struct nfs4_get_lease_time_data *)calldata;
4550 dprintk("--> %s\n", __func__);
4551 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4552 /* just setup sequence, do not trigger session recovery
4553 since we're invoked within one */
4554 ret = nfs41_setup_sequence(data->clp->cl_session,
4555 &data->args->la_seq_args,
4556 &data->res->lr_seq_res, 0, task);
4558 BUG_ON(ret == -EAGAIN);
4559 rpc_call_start(task);
4560 dprintk("<-- %s\n", __func__);
4564 * Called from nfs4_state_manager thread for session setup, so don't recover
4565 * from sequence operation or clientid errors.
4567 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4569 struct nfs4_get_lease_time_data *data =
4570 (struct nfs4_get_lease_time_data *)calldata;
4572 dprintk("--> %s\n", __func__);
4573 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4574 return;
4575 switch (task->tk_status) {
4576 case -NFS4ERR_DELAY:
4577 case -NFS4ERR_GRACE:
4578 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4579 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4580 task->tk_status = 0;
4581 nfs_restart_rpc(task, data->clp);
4582 return;
4584 dprintk("<-- %s\n", __func__);
4587 struct rpc_call_ops nfs4_get_lease_time_ops = {
4588 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4589 .rpc_call_done = nfs4_get_lease_time_done,
4592 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4594 struct rpc_task *task;
4595 struct nfs4_get_lease_time_args args;
4596 struct nfs4_get_lease_time_res res = {
4597 .lr_fsinfo = fsinfo,
4599 struct nfs4_get_lease_time_data data = {
4600 .args = &args,
4601 .res = &res,
4602 .clp = clp,
4604 struct rpc_message msg = {
4605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4606 .rpc_argp = &args,
4607 .rpc_resp = &res,
4609 struct rpc_task_setup task_setup = {
4610 .rpc_client = clp->cl_rpcclient,
4611 .rpc_message = &msg,
4612 .callback_ops = &nfs4_get_lease_time_ops,
4613 .callback_data = &data
4615 int status;
4617 dprintk("--> %s\n", __func__);
4618 task = rpc_run_task(&task_setup);
4620 if (IS_ERR(task))
4621 status = PTR_ERR(task);
4622 else {
4623 status = task->tk_status;
4624 rpc_put_task(task);
4626 dprintk("<-- %s return %d\n", __func__, status);
4628 return status;
4632 * Reset a slot table
4634 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4635 int ivalue)
4637 struct nfs4_slot *new = NULL;
4638 int i;
4639 int ret = 0;
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) {
4646 ret = -ENOMEM;
4647 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4648 GFP_NOFS);
4649 if (!new)
4650 goto out;
4651 ret = 0;
4652 kfree(tbl->slots);
4654 spin_lock(&tbl->slot_tbl_lock);
4655 if (new) {
4656 tbl->slots = new;
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);
4664 out:
4665 dprintk("<-- %s: return %d\n", __func__, ret);
4666 return ret;
4670 * Reset the forechannel and backchannel slot tables
4672 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4674 int status;
4676 status = nfs4_reset_slot_table(&session->fc_slot_table,
4677 session->fc_attrs.max_reqs, 1);
4678 if (status)
4679 return status;
4681 status = nfs4_reset_slot_table(&session->bc_slot_table,
4682 session->bc_attrs.max_reqs, 0);
4683 return status;
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;
4697 return;
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;
4707 int ret = -ENOMEM;
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_NOFS);
4714 if (!slot)
4715 goto out;
4716 ret = 0;
4718 spin_lock(&tbl->slot_tbl_lock);
4719 tbl->max_slots = max_slots;
4720 tbl->slots = slot;
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);
4725 out:
4726 dprintk("<-- %s: return %d\n", __func__, ret);
4727 return 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;
4736 int status = 0;
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);
4742 if (status)
4743 return status;
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);
4750 if (status)
4751 nfs4_destroy_slot_tables(session);
4754 return status;
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_NOFS);
4763 if (!session)
4764 return NULL;
4766 init_completion(&session->complete);
4768 tbl = &session->fc_slot_table;
4769 tbl->highest_used_slotid = -1;
4770 spin_lock_init(&tbl->slot_tbl_lock);
4771 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4773 tbl = &session->bc_slot_table;
4774 tbl->highest_used_slotid = -1;
4775 spin_lock_init(&tbl->slot_tbl_lock);
4776 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4778 session->session_state = 1<<NFS4_SESSION_INITING;
4780 session->clp = clp;
4781 return session;
4784 void nfs4_destroy_session(struct nfs4_session *session)
4786 nfs4_proc_destroy_session(session);
4787 dprintk("%s Destroy backchannel for xprt %p\n",
4788 __func__, session->clp->cl_rpcclient->cl_xprt);
4789 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4790 NFS41_BC_MIN_CALLBACKS);
4791 nfs4_destroy_slot_tables(session);
4792 kfree(session);
4796 * Initialize the values to be used by the client in CREATE_SESSION
4797 * If nfs4_init_session set the fore channel request and response sizes,
4798 * use them.
4800 * Set the back channel max_resp_sz_cached to zero to force the client to
4801 * always set csa_cachethis to FALSE because the current implementation
4802 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4804 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4806 struct nfs4_session *session = args->client->cl_session;
4807 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4808 mxresp_sz = session->fc_attrs.max_resp_sz;
4810 if (mxrqst_sz == 0)
4811 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4812 if (mxresp_sz == 0)
4813 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4814 /* Fore channel attributes */
4815 args->fc_attrs.headerpadsz = 0;
4816 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4817 args->fc_attrs.max_resp_sz = mxresp_sz;
4818 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4819 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4821 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4822 "max_ops=%u max_reqs=%u\n",
4823 __func__,
4824 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4825 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4827 /* Back channel attributes */
4828 args->bc_attrs.headerpadsz = 0;
4829 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4830 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4831 args->bc_attrs.max_resp_sz_cached = 0;
4832 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4833 args->bc_attrs.max_reqs = 1;
4835 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4836 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4837 __func__,
4838 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4839 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4840 args->bc_attrs.max_reqs);
4843 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4845 if (rcvd <= sent)
4846 return 0;
4847 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4848 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4849 return -EINVAL;
4852 #define _verify_fore_channel_attr(_name_) \
4853 _verify_channel_attr("fore", #_name_, \
4854 args->fc_attrs._name_, \
4855 session->fc_attrs._name_)
4857 #define _verify_back_channel_attr(_name_) \
4858 _verify_channel_attr("back", #_name_, \
4859 args->bc_attrs._name_, \
4860 session->bc_attrs._name_)
4863 * The server is not allowed to increase the fore channel header pad size,
4864 * maximum response size, or maximum number of operations.
4866 * The back channel attributes are only negotiatied down: We send what the
4867 * (back channel) server insists upon.
4869 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4870 struct nfs4_session *session)
4872 int ret = 0;
4874 ret |= _verify_fore_channel_attr(headerpadsz);
4875 ret |= _verify_fore_channel_attr(max_resp_sz);
4876 ret |= _verify_fore_channel_attr(max_ops);
4878 ret |= _verify_back_channel_attr(headerpadsz);
4879 ret |= _verify_back_channel_attr(max_rqst_sz);
4880 ret |= _verify_back_channel_attr(max_resp_sz);
4881 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4882 ret |= _verify_back_channel_attr(max_ops);
4883 ret |= _verify_back_channel_attr(max_reqs);
4885 return ret;
4888 static int _nfs4_proc_create_session(struct nfs_client *clp)
4890 struct nfs4_session *session = clp->cl_session;
4891 struct nfs41_create_session_args args = {
4892 .client = clp,
4893 .cb_program = NFS4_CALLBACK,
4895 struct nfs41_create_session_res res = {
4896 .client = clp,
4898 struct rpc_message msg = {
4899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4900 .rpc_argp = &args,
4901 .rpc_resp = &res,
4903 int status;
4905 nfs4_init_channel_attrs(&args);
4906 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4908 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4910 if (!status)
4911 /* Verify the session's negotiated channel_attrs values */
4912 status = nfs4_verify_channel_attrs(&args, session);
4913 if (!status) {
4914 /* Increment the clientid slot sequence id */
4915 clp->cl_seqid++;
4918 return status;
4922 * Issues a CREATE_SESSION operation to the server.
4923 * It is the responsibility of the caller to verify the session is
4924 * expired before calling this routine.
4926 int nfs4_proc_create_session(struct nfs_client *clp)
4928 int status;
4929 unsigned *ptr;
4930 struct nfs4_session *session = clp->cl_session;
4932 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4934 status = _nfs4_proc_create_session(clp);
4935 if (status)
4936 goto out;
4938 /* Init and reset the fore channel */
4939 status = nfs4_init_slot_tables(session);
4940 dprintk("slot table initialization returned %d\n", status);
4941 if (status)
4942 goto out;
4943 status = nfs4_reset_slot_tables(session);
4944 dprintk("slot table reset returned %d\n", status);
4945 if (status)
4946 goto out;
4948 ptr = (unsigned *)&session->sess_id.data[0];
4949 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4950 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4951 out:
4952 dprintk("<-- %s\n", __func__);
4953 return status;
4957 * Issue the over-the-wire RPC DESTROY_SESSION.
4958 * The caller must serialize access to this routine.
4960 int nfs4_proc_destroy_session(struct nfs4_session *session)
4962 int status = 0;
4963 struct rpc_message msg;
4965 dprintk("--> nfs4_proc_destroy_session\n");
4967 /* session is still being setup */
4968 if (session->clp->cl_cons_state != NFS_CS_READY)
4969 return status;
4971 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4972 msg.rpc_argp = session;
4973 msg.rpc_resp = NULL;
4974 msg.rpc_cred = NULL;
4975 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4977 if (status)
4978 printk(KERN_WARNING
4979 "Got error %d from the server on DESTROY_SESSION. "
4980 "Session has been destroyed regardless...\n", status);
4982 dprintk("<-- nfs4_proc_destroy_session\n");
4983 return status;
4986 int nfs4_init_session(struct nfs_server *server)
4988 struct nfs_client *clp = server->nfs_client;
4989 struct nfs4_session *session;
4990 unsigned int rsize, wsize;
4991 int ret;
4993 if (!nfs4_has_session(clp))
4994 return 0;
4996 session = clp->cl_session;
4997 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
4998 return 0;
5000 rsize = server->rsize;
5001 if (rsize == 0)
5002 rsize = NFS_MAX_FILE_IO_SIZE;
5003 wsize = server->wsize;
5004 if (wsize == 0)
5005 wsize = NFS_MAX_FILE_IO_SIZE;
5007 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5008 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5010 ret = nfs4_recover_expired_lease(server);
5011 if (!ret)
5012 ret = nfs4_check_client_ready(clp);
5013 return ret;
5017 * Renew the cl_session lease.
5019 struct nfs4_sequence_data {
5020 struct nfs_client *clp;
5021 struct nfs4_sequence_args args;
5022 struct nfs4_sequence_res res;
5025 static void nfs41_sequence_release(void *data)
5027 struct nfs4_sequence_data *calldata = data;
5028 struct nfs_client *clp = calldata->clp;
5030 if (atomic_read(&clp->cl_count) > 1)
5031 nfs4_schedule_state_renewal(clp);
5032 nfs_put_client(clp);
5033 kfree(calldata);
5036 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5038 switch(task->tk_status) {
5039 case -NFS4ERR_DELAY:
5040 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5041 return -EAGAIN;
5042 default:
5043 nfs4_schedule_state_recovery(clp);
5045 return 0;
5048 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5050 struct nfs4_sequence_data *calldata = data;
5051 struct nfs_client *clp = calldata->clp;
5053 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5054 return;
5056 if (task->tk_status < 0) {
5057 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5058 if (atomic_read(&clp->cl_count) == 1)
5059 goto out;
5061 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5062 rpc_restart_call_prepare(task);
5063 return;
5066 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5067 out:
5068 dprintk("<-- %s\n", __func__);
5071 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5073 struct nfs4_sequence_data *calldata = data;
5074 struct nfs_client *clp = calldata->clp;
5075 struct nfs4_sequence_args *args;
5076 struct nfs4_sequence_res *res;
5078 args = task->tk_msg.rpc_argp;
5079 res = task->tk_msg.rpc_resp;
5081 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5082 return;
5083 rpc_call_start(task);
5086 static const struct rpc_call_ops nfs41_sequence_ops = {
5087 .rpc_call_done = nfs41_sequence_call_done,
5088 .rpc_call_prepare = nfs41_sequence_prepare,
5089 .rpc_release = nfs41_sequence_release,
5092 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5094 struct nfs4_sequence_data *calldata;
5095 struct rpc_message msg = {
5096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5097 .rpc_cred = cred,
5099 struct rpc_task_setup task_setup_data = {
5100 .rpc_client = clp->cl_rpcclient,
5101 .rpc_message = &msg,
5102 .callback_ops = &nfs41_sequence_ops,
5103 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5106 if (!atomic_inc_not_zero(&clp->cl_count))
5107 return ERR_PTR(-EIO);
5108 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5109 if (calldata == NULL) {
5110 nfs_put_client(clp);
5111 return ERR_PTR(-ENOMEM);
5113 msg.rpc_argp = &calldata->args;
5114 msg.rpc_resp = &calldata->res;
5115 calldata->clp = clp;
5116 task_setup_data.callback_data = calldata;
5118 return rpc_run_task(&task_setup_data);
5121 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5123 struct rpc_task *task;
5124 int ret = 0;
5126 task = _nfs41_proc_sequence(clp, cred);
5127 if (IS_ERR(task))
5128 ret = PTR_ERR(task);
5129 else
5130 rpc_put_task(task);
5131 dprintk("<-- %s status=%d\n", __func__, ret);
5132 return ret;
5135 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5137 struct rpc_task *task;
5138 int ret;
5140 task = _nfs41_proc_sequence(clp, cred);
5141 if (IS_ERR(task)) {
5142 ret = PTR_ERR(task);
5143 goto out;
5145 ret = rpc_wait_for_completion_task(task);
5146 if (!ret)
5147 ret = task->tk_status;
5148 rpc_put_task(task);
5149 out:
5150 dprintk("<-- %s status=%d\n", __func__, ret);
5151 return ret;
5154 struct nfs4_reclaim_complete_data {
5155 struct nfs_client *clp;
5156 struct nfs41_reclaim_complete_args arg;
5157 struct nfs41_reclaim_complete_res res;
5160 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5162 struct nfs4_reclaim_complete_data *calldata = data;
5164 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5165 if (nfs41_setup_sequence(calldata->clp->cl_session,
5166 &calldata->arg.seq_args,
5167 &calldata->res.seq_res, 0, task))
5168 return;
5170 rpc_call_start(task);
5173 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5175 switch(task->tk_status) {
5176 case 0:
5177 case -NFS4ERR_COMPLETE_ALREADY:
5178 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5179 break;
5180 case -NFS4ERR_DELAY:
5181 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5182 return -EAGAIN;
5183 default:
5184 nfs4_schedule_state_recovery(clp);
5186 return 0;
5189 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5191 struct nfs4_reclaim_complete_data *calldata = data;
5192 struct nfs_client *clp = calldata->clp;
5193 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5195 dprintk("--> %s\n", __func__);
5196 if (!nfs41_sequence_done(task, res))
5197 return;
5199 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5200 rpc_restart_call_prepare(task);
5201 return;
5203 dprintk("<-- %s\n", __func__);
5206 static void nfs4_free_reclaim_complete_data(void *data)
5208 struct nfs4_reclaim_complete_data *calldata = data;
5210 kfree(calldata);
5213 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5214 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5215 .rpc_call_done = nfs4_reclaim_complete_done,
5216 .rpc_release = nfs4_free_reclaim_complete_data,
5220 * Issue a global reclaim complete.
5222 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5224 struct nfs4_reclaim_complete_data *calldata;
5225 struct rpc_task *task;
5226 struct rpc_message msg = {
5227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5229 struct rpc_task_setup task_setup_data = {
5230 .rpc_client = clp->cl_rpcclient,
5231 .rpc_message = &msg,
5232 .callback_ops = &nfs4_reclaim_complete_call_ops,
5233 .flags = RPC_TASK_ASYNC,
5235 int status = -ENOMEM;
5237 dprintk("--> %s\n", __func__);
5238 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5239 if (calldata == NULL)
5240 goto out;
5241 calldata->clp = clp;
5242 calldata->arg.one_fs = 0;
5244 msg.rpc_argp = &calldata->arg;
5245 msg.rpc_resp = &calldata->res;
5246 task_setup_data.callback_data = calldata;
5247 task = rpc_run_task(&task_setup_data);
5248 if (IS_ERR(task)) {
5249 status = PTR_ERR(task);
5250 goto out;
5252 rpc_put_task(task);
5253 return 0;
5254 out:
5255 dprintk("<-- %s status=%d\n", __func__, status);
5256 return status;
5258 #endif /* CONFIG_NFS_V4_1 */
5260 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5261 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5262 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5263 .recover_open = nfs4_open_reclaim,
5264 .recover_lock = nfs4_lock_reclaim,
5265 .establish_clid = nfs4_init_clientid,
5266 .get_clid_cred = nfs4_get_setclientid_cred,
5269 #if defined(CONFIG_NFS_V4_1)
5270 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5271 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5272 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5273 .recover_open = nfs4_open_reclaim,
5274 .recover_lock = nfs4_lock_reclaim,
5275 .establish_clid = nfs41_init_clientid,
5276 .get_clid_cred = nfs4_get_exchange_id_cred,
5277 .reclaim_complete = nfs41_proc_reclaim_complete,
5279 #endif /* CONFIG_NFS_V4_1 */
5281 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5282 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5283 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5284 .recover_open = nfs4_open_expired,
5285 .recover_lock = nfs4_lock_expired,
5286 .establish_clid = nfs4_init_clientid,
5287 .get_clid_cred = nfs4_get_setclientid_cred,
5290 #if defined(CONFIG_NFS_V4_1)
5291 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5292 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5293 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5294 .recover_open = nfs4_open_expired,
5295 .recover_lock = nfs4_lock_expired,
5296 .establish_clid = nfs41_init_clientid,
5297 .get_clid_cred = nfs4_get_exchange_id_cred,
5299 #endif /* CONFIG_NFS_V4_1 */
5301 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5302 .sched_state_renewal = nfs4_proc_async_renew,
5303 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5304 .renew_lease = nfs4_proc_renew,
5307 #if defined(CONFIG_NFS_V4_1)
5308 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5309 .sched_state_renewal = nfs41_proc_async_sequence,
5310 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5311 .renew_lease = nfs4_proc_sequence,
5313 #endif
5315 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5316 .minor_version = 0,
5317 .call_sync = _nfs4_call_sync,
5318 .validate_stateid = nfs4_validate_delegation_stateid,
5319 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5320 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5321 .state_renewal_ops = &nfs40_state_renewal_ops,
5324 #if defined(CONFIG_NFS_V4_1)
5325 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5326 .minor_version = 1,
5327 .call_sync = _nfs4_call_sync_session,
5328 .validate_stateid = nfs41_validate_delegation_stateid,
5329 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5330 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5331 .state_renewal_ops = &nfs41_state_renewal_ops,
5333 #endif
5335 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5336 [0] = &nfs_v4_0_minor_ops,
5337 #if defined(CONFIG_NFS_V4_1)
5338 [1] = &nfs_v4_1_minor_ops,
5339 #endif
5342 static const struct inode_operations nfs4_file_inode_operations = {
5343 .permission = nfs_permission,
5344 .getattr = nfs_getattr,
5345 .setattr = nfs_setattr,
5346 .getxattr = nfs4_getxattr,
5347 .setxattr = nfs4_setxattr,
5348 .listxattr = nfs4_listxattr,
5351 const struct nfs_rpc_ops nfs_v4_clientops = {
5352 .version = 4, /* protocol version */
5353 .dentry_ops = &nfs4_dentry_operations,
5354 .dir_inode_ops = &nfs4_dir_inode_operations,
5355 .file_inode_ops = &nfs4_file_inode_operations,
5356 .getroot = nfs4_proc_get_root,
5357 .getattr = nfs4_proc_getattr,
5358 .setattr = nfs4_proc_setattr,
5359 .lookupfh = nfs4_proc_lookupfh,
5360 .lookup = nfs4_proc_lookup,
5361 .access = nfs4_proc_access,
5362 .readlink = nfs4_proc_readlink,
5363 .create = nfs4_proc_create,
5364 .remove = nfs4_proc_remove,
5365 .unlink_setup = nfs4_proc_unlink_setup,
5366 .unlink_done = nfs4_proc_unlink_done,
5367 .rename = nfs4_proc_rename,
5368 .rename_setup = nfs4_proc_rename_setup,
5369 .rename_done = nfs4_proc_rename_done,
5370 .link = nfs4_proc_link,
5371 .symlink = nfs4_proc_symlink,
5372 .mkdir = nfs4_proc_mkdir,
5373 .rmdir = nfs4_proc_remove,
5374 .readdir = nfs4_proc_readdir,
5375 .mknod = nfs4_proc_mknod,
5376 .statfs = nfs4_proc_statfs,
5377 .fsinfo = nfs4_proc_fsinfo,
5378 .pathconf = nfs4_proc_pathconf,
5379 .set_capabilities = nfs4_server_capabilities,
5380 .decode_dirent = nfs4_decode_dirent,
5381 .read_setup = nfs4_proc_read_setup,
5382 .read_done = nfs4_read_done,
5383 .write_setup = nfs4_proc_write_setup,
5384 .write_done = nfs4_write_done,
5385 .commit_setup = nfs4_proc_commit_setup,
5386 .commit_done = nfs4_commit_done,
5387 .lock = nfs4_proc_lock,
5388 .clear_acl_cache = nfs4_zap_acl_attr,
5389 .close_context = nfs4_close_context,
5390 .open_context = nfs4_atomic_open,
5394 * Local variables:
5395 * c-basic-offset: 8
5396 * End: