NFSv4.1 convert layoutcommit sync to boolean
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4proc.c
blob43045fa447100a33944f90c42f4f28e435c17c3f
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>
52 #include <linux/xattr.h>
53 #include <linux/utsname.h>
55 #include "nfs4_fs.h"
56 #include "delegation.h"
57 #include "internal.h"
58 #include "iostat.h"
59 #include "callback.h"
60 #include "pnfs.h"
62 #define NFSDBG_FACILITY NFSDBG_PROC
64 #define NFS4_POLL_RETRY_MIN (HZ/10)
65 #define NFS4_POLL_RETRY_MAX (15*HZ)
67 #define NFS4_MAX_LOOP_ON_RECOVER (10)
69 struct nfs4_opendata;
70 static int _nfs4_proc_open(struct nfs4_opendata *data);
71 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
72 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
73 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
74 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
75 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
76 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
77 struct nfs_fattr *fattr, struct iattr *sattr,
78 struct nfs4_state *state);
80 /* Prevent leaks of NFSv4 errors into userland */
81 static int nfs4_map_errors(int err)
83 if (err >= -1000)
84 return err;
85 switch (err) {
86 case -NFS4ERR_RESOURCE:
87 return -EREMOTEIO;
88 case -NFS4ERR_BADOWNER:
89 case -NFS4ERR_BADNAME:
90 return -EINVAL;
91 default:
92 dprintk("%s could not handle NFSv4 error %d\n",
93 __func__, -err);
94 break;
96 return -EIO;
100 * This is our standard bitmap for GETATTR requests.
102 const u32 nfs4_fattr_bitmap[2] = {
103 FATTR4_WORD0_TYPE
104 | FATTR4_WORD0_CHANGE
105 | FATTR4_WORD0_SIZE
106 | FATTR4_WORD0_FSID
107 | FATTR4_WORD0_FILEID,
108 FATTR4_WORD1_MODE
109 | FATTR4_WORD1_NUMLINKS
110 | FATTR4_WORD1_OWNER
111 | FATTR4_WORD1_OWNER_GROUP
112 | FATTR4_WORD1_RAWDEV
113 | FATTR4_WORD1_SPACE_USED
114 | FATTR4_WORD1_TIME_ACCESS
115 | FATTR4_WORD1_TIME_METADATA
116 | FATTR4_WORD1_TIME_MODIFY
119 const u32 nfs4_statfs_bitmap[2] = {
120 FATTR4_WORD0_FILES_AVAIL
121 | FATTR4_WORD0_FILES_FREE
122 | FATTR4_WORD0_FILES_TOTAL,
123 FATTR4_WORD1_SPACE_AVAIL
124 | FATTR4_WORD1_SPACE_FREE
125 | FATTR4_WORD1_SPACE_TOTAL
128 const u32 nfs4_pathconf_bitmap[2] = {
129 FATTR4_WORD0_MAXLINK
130 | FATTR4_WORD0_MAXNAME,
134 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
135 | FATTR4_WORD0_MAXREAD
136 | FATTR4_WORD0_MAXWRITE
137 | FATTR4_WORD0_LEASE_TIME,
138 FATTR4_WORD1_TIME_DELTA
139 | FATTR4_WORD1_FS_LAYOUT_TYPES
142 const u32 nfs4_fs_locations_bitmap[2] = {
143 FATTR4_WORD0_TYPE
144 | FATTR4_WORD0_CHANGE
145 | FATTR4_WORD0_SIZE
146 | FATTR4_WORD0_FSID
147 | FATTR4_WORD0_FILEID
148 | FATTR4_WORD0_FS_LOCATIONS,
149 FATTR4_WORD1_MODE
150 | FATTR4_WORD1_NUMLINKS
151 | FATTR4_WORD1_OWNER
152 | FATTR4_WORD1_OWNER_GROUP
153 | FATTR4_WORD1_RAWDEV
154 | FATTR4_WORD1_SPACE_USED
155 | FATTR4_WORD1_TIME_ACCESS
156 | FATTR4_WORD1_TIME_METADATA
157 | FATTR4_WORD1_TIME_MODIFY
158 | FATTR4_WORD1_MOUNTED_ON_FILEID
161 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
162 struct nfs4_readdir_arg *readdir)
164 __be32 *start, *p;
166 BUG_ON(readdir->count < 80);
167 if (cookie > 2) {
168 readdir->cookie = cookie;
169 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
170 return;
173 readdir->cookie = 0;
174 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
175 if (cookie == 2)
176 return;
179 * NFSv4 servers do not return entries for '.' and '..'
180 * Therefore, we fake these entries here. We let '.'
181 * have cookie 0 and '..' have cookie 1. Note that
182 * when talking to the server, we always send cookie 0
183 * instead of 1 or 2.
185 start = p = kmap_atomic(*readdir->pages, KM_USER0);
187 if (cookie == 0) {
188 *p++ = xdr_one; /* next */
189 *p++ = xdr_zero; /* cookie, first word */
190 *p++ = xdr_one; /* cookie, second word */
191 *p++ = xdr_one; /* entry len */
192 memcpy(p, ".\0\0\0", 4); /* entry */
193 p++;
194 *p++ = xdr_one; /* bitmap length */
195 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
196 *p++ = htonl(8); /* attribute buffer length */
197 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
200 *p++ = xdr_one; /* next */
201 *p++ = xdr_zero; /* cookie, first word */
202 *p++ = xdr_two; /* cookie, second word */
203 *p++ = xdr_two; /* entry len */
204 memcpy(p, "..\0\0", 4); /* entry */
205 p++;
206 *p++ = xdr_one; /* bitmap length */
207 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
208 *p++ = htonl(8); /* attribute buffer length */
209 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
211 readdir->pgbase = (char *)p - (char *)start;
212 readdir->count -= readdir->pgbase;
213 kunmap_atomic(start, KM_USER0);
216 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
218 int res;
220 might_sleep();
222 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
223 nfs_wait_bit_killable, TASK_KILLABLE);
224 return res;
227 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
229 int res = 0;
231 might_sleep();
233 if (*timeout <= 0)
234 *timeout = NFS4_POLL_RETRY_MIN;
235 if (*timeout > NFS4_POLL_RETRY_MAX)
236 *timeout = NFS4_POLL_RETRY_MAX;
237 schedule_timeout_killable(*timeout);
238 if (fatal_signal_pending(current))
239 res = -ERESTARTSYS;
240 *timeout <<= 1;
241 return res;
244 /* This is the error handling routine for processes that are allowed
245 * to sleep.
247 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
249 struct nfs_client *clp = server->nfs_client;
250 struct nfs4_state *state = exception->state;
251 int ret = errorcode;
253 exception->retry = 0;
254 switch(errorcode) {
255 case 0:
256 return 0;
257 case -NFS4ERR_ADMIN_REVOKED:
258 case -NFS4ERR_BAD_STATEID:
259 case -NFS4ERR_OPENMODE:
260 if (state == NULL)
261 break;
262 nfs4_schedule_stateid_recovery(server, state);
263 goto wait_on_recovery;
264 case -NFS4ERR_STALE_STATEID:
265 case -NFS4ERR_STALE_CLIENTID:
266 case -NFS4ERR_EXPIRED:
267 nfs4_schedule_lease_recovery(clp);
268 goto wait_on_recovery;
269 #if defined(CONFIG_NFS_V4_1)
270 case -NFS4ERR_BADSESSION:
271 case -NFS4ERR_BADSLOT:
272 case -NFS4ERR_BAD_HIGH_SLOT:
273 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
274 case -NFS4ERR_DEADSESSION:
275 case -NFS4ERR_SEQ_FALSE_RETRY:
276 case -NFS4ERR_SEQ_MISORDERED:
277 dprintk("%s ERROR: %d Reset session\n", __func__,
278 errorcode);
279 nfs4_schedule_session_recovery(clp->cl_session);
280 exception->retry = 1;
281 break;
282 #endif /* defined(CONFIG_NFS_V4_1) */
283 case -NFS4ERR_FILE_OPEN:
284 if (exception->timeout > HZ) {
285 /* We have retried a decent amount, time to
286 * fail
288 ret = -EBUSY;
289 break;
291 case -NFS4ERR_GRACE:
292 case -NFS4ERR_DELAY:
293 case -EKEYEXPIRED:
294 ret = nfs4_delay(server->client, &exception->timeout);
295 if (ret != 0)
296 break;
297 case -NFS4ERR_OLD_STATEID:
298 exception->retry = 1;
299 break;
300 case -NFS4ERR_BADOWNER:
301 /* The following works around a Linux server bug! */
302 case -NFS4ERR_BADNAME:
303 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
304 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
305 exception->retry = 1;
306 printk(KERN_WARNING "NFS: v4 server %s "
307 "does not accept raw "
308 "uid/gids. "
309 "Reenabling the idmapper.\n",
310 server->nfs_client->cl_hostname);
313 /* We failed to handle the error */
314 return nfs4_map_errors(ret);
315 wait_on_recovery:
316 ret = nfs4_wait_clnt_recover(clp);
317 if (ret == 0)
318 exception->retry = 1;
319 return ret;
323 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
325 spin_lock(&clp->cl_lock);
326 if (time_before(clp->cl_last_renewal,timestamp))
327 clp->cl_last_renewal = timestamp;
328 spin_unlock(&clp->cl_lock);
331 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
333 do_renew_lease(server->nfs_client, timestamp);
336 #if defined(CONFIG_NFS_V4_1)
339 * nfs4_free_slot - free a slot and efficiently update slot table.
341 * freeing a slot is trivially done by clearing its respective bit
342 * in the bitmap.
343 * If the freed slotid equals highest_used_slotid we want to update it
344 * so that the server would be able to size down the slot table if needed,
345 * otherwise we know that the highest_used_slotid is still in use.
346 * When updating highest_used_slotid there may be "holes" in the bitmap
347 * so we need to scan down from highest_used_slotid to 0 looking for the now
348 * highest slotid in use.
349 * If none found, highest_used_slotid is set to -1.
351 * Must be called while holding tbl->slot_tbl_lock
353 static void
354 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
356 int free_slotid = free_slot - tbl->slots;
357 int slotid = free_slotid;
359 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
360 /* clear used bit in bitmap */
361 __clear_bit(slotid, tbl->used_slots);
363 /* update highest_used_slotid when it is freed */
364 if (slotid == tbl->highest_used_slotid) {
365 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
366 if (slotid < tbl->max_slots)
367 tbl->highest_used_slotid = slotid;
368 else
369 tbl->highest_used_slotid = -1;
371 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
372 free_slotid, tbl->highest_used_slotid);
376 * Signal state manager thread if session fore channel is drained
378 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
380 struct rpc_task *task;
382 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
383 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
384 if (task)
385 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
386 return;
389 if (ses->fc_slot_table.highest_used_slotid != -1)
390 return;
392 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
393 complete(&ses->fc_slot_table.complete);
397 * Signal state manager thread if session back channel is drained
399 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
401 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
402 ses->bc_slot_table.highest_used_slotid != -1)
403 return;
404 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
405 complete(&ses->bc_slot_table.complete);
408 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
410 struct nfs4_slot_table *tbl;
412 tbl = &res->sr_session->fc_slot_table;
413 if (!res->sr_slot) {
414 /* just wake up the next guy waiting since
415 * we may have not consumed a slot after all */
416 dprintk("%s: No slot\n", __func__);
417 return;
420 spin_lock(&tbl->slot_tbl_lock);
421 nfs4_free_slot(tbl, res->sr_slot);
422 nfs4_check_drain_fc_complete(res->sr_session);
423 spin_unlock(&tbl->slot_tbl_lock);
424 res->sr_slot = NULL;
427 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
429 unsigned long timestamp;
430 struct nfs_client *clp;
433 * sr_status remains 1 if an RPC level error occurred. The server
434 * may or may not have processed the sequence operation..
435 * Proceed as if the server received and processed the sequence
436 * operation.
438 if (res->sr_status == 1)
439 res->sr_status = NFS_OK;
441 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
442 if (!res->sr_slot)
443 goto out;
445 /* Check the SEQUENCE operation status */
446 switch (res->sr_status) {
447 case 0:
448 /* Update the slot's sequence and clientid lease timer */
449 ++res->sr_slot->seq_nr;
450 timestamp = res->sr_renewal_time;
451 clp = res->sr_session->clp;
452 do_renew_lease(clp, timestamp);
453 /* Check sequence flags */
454 if (res->sr_status_flags != 0)
455 nfs4_schedule_lease_recovery(clp);
456 break;
457 case -NFS4ERR_DELAY:
458 /* The server detected a resend of the RPC call and
459 * returned NFS4ERR_DELAY as per Section 2.10.6.2
460 * of RFC5661.
462 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
463 __func__,
464 res->sr_slot - res->sr_session->fc_slot_table.slots,
465 res->sr_slot->seq_nr);
466 goto out_retry;
467 default:
468 /* Just update the slot sequence no. */
469 ++res->sr_slot->seq_nr;
471 out:
472 /* The session may be reset by one of the error handlers. */
473 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
474 nfs41_sequence_free_slot(res);
475 return 1;
476 out_retry:
477 if (!rpc_restart_call(task))
478 goto out;
479 rpc_delay(task, NFS4_POLL_RETRY_MAX);
480 return 0;
483 static int nfs4_sequence_done(struct rpc_task *task,
484 struct nfs4_sequence_res *res)
486 if (res->sr_session == NULL)
487 return 1;
488 return nfs41_sequence_done(task, res);
492 * nfs4_find_slot - efficiently look for a free slot
494 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
495 * If found, we mark the slot as used, update the highest_used_slotid,
496 * and respectively set up the sequence operation args.
497 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
499 * Note: must be called with under the slot_tbl_lock.
501 static u8
502 nfs4_find_slot(struct nfs4_slot_table *tbl)
504 int slotid;
505 u8 ret_id = NFS4_MAX_SLOT_TABLE;
506 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
508 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
509 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
510 tbl->max_slots);
511 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
512 if (slotid >= tbl->max_slots)
513 goto out;
514 __set_bit(slotid, tbl->used_slots);
515 if (slotid > tbl->highest_used_slotid)
516 tbl->highest_used_slotid = slotid;
517 ret_id = slotid;
518 out:
519 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
520 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
521 return ret_id;
524 int nfs41_setup_sequence(struct nfs4_session *session,
525 struct nfs4_sequence_args *args,
526 struct nfs4_sequence_res *res,
527 int cache_reply,
528 struct rpc_task *task)
530 struct nfs4_slot *slot;
531 struct nfs4_slot_table *tbl;
532 u8 slotid;
534 dprintk("--> %s\n", __func__);
535 /* slot already allocated? */
536 if (res->sr_slot != NULL)
537 return 0;
539 tbl = &session->fc_slot_table;
541 spin_lock(&tbl->slot_tbl_lock);
542 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
543 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
545 * The state manager will wait until the slot table is empty.
546 * Schedule the reset thread
548 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
549 spin_unlock(&tbl->slot_tbl_lock);
550 dprintk("%s Schedule Session Reset\n", __func__);
551 return -EAGAIN;
554 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
555 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
556 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
557 spin_unlock(&tbl->slot_tbl_lock);
558 dprintk("%s enforce FIFO order\n", __func__);
559 return -EAGAIN;
562 slotid = nfs4_find_slot(tbl);
563 if (slotid == NFS4_MAX_SLOT_TABLE) {
564 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
565 spin_unlock(&tbl->slot_tbl_lock);
566 dprintk("<-- %s: no free slots\n", __func__);
567 return -EAGAIN;
569 spin_unlock(&tbl->slot_tbl_lock);
571 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
572 slot = tbl->slots + slotid;
573 args->sa_session = session;
574 args->sa_slotid = slotid;
575 args->sa_cache_this = cache_reply;
577 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
579 res->sr_session = session;
580 res->sr_slot = slot;
581 res->sr_renewal_time = jiffies;
582 res->sr_status_flags = 0;
584 * sr_status is only set in decode_sequence, and so will remain
585 * set to 1 if an rpc level failure occurs.
587 res->sr_status = 1;
588 return 0;
590 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
592 int nfs4_setup_sequence(const struct nfs_server *server,
593 struct nfs4_sequence_args *args,
594 struct nfs4_sequence_res *res,
595 int cache_reply,
596 struct rpc_task *task)
598 struct nfs4_session *session = nfs4_get_session(server);
599 int ret = 0;
601 if (session == NULL) {
602 args->sa_session = NULL;
603 res->sr_session = NULL;
604 goto out;
607 dprintk("--> %s clp %p session %p sr_slot %td\n",
608 __func__, session->clp, session, res->sr_slot ?
609 res->sr_slot - session->fc_slot_table.slots : -1);
611 ret = nfs41_setup_sequence(session, args, res, cache_reply,
612 task);
613 out:
614 dprintk("<-- %s status=%d\n", __func__, ret);
615 return ret;
618 struct nfs41_call_sync_data {
619 const struct nfs_server *seq_server;
620 struct nfs4_sequence_args *seq_args;
621 struct nfs4_sequence_res *seq_res;
622 int cache_reply;
625 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
627 struct nfs41_call_sync_data *data = calldata;
629 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
631 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
632 data->seq_res, data->cache_reply, task))
633 return;
634 rpc_call_start(task);
637 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
639 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
640 nfs41_call_sync_prepare(task, calldata);
643 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
645 struct nfs41_call_sync_data *data = calldata;
647 nfs41_sequence_done(task, data->seq_res);
650 struct rpc_call_ops nfs41_call_sync_ops = {
651 .rpc_call_prepare = nfs41_call_sync_prepare,
652 .rpc_call_done = nfs41_call_sync_done,
655 struct rpc_call_ops nfs41_call_priv_sync_ops = {
656 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
657 .rpc_call_done = nfs41_call_sync_done,
660 static int nfs4_call_sync_sequence(struct nfs_server *server,
661 struct rpc_message *msg,
662 struct nfs4_sequence_args *args,
663 struct nfs4_sequence_res *res,
664 int cache_reply,
665 int privileged)
667 int ret;
668 struct rpc_task *task;
669 struct nfs41_call_sync_data data = {
670 .seq_server = server,
671 .seq_args = args,
672 .seq_res = res,
673 .cache_reply = cache_reply,
675 struct rpc_task_setup task_setup = {
676 .rpc_client = server->client,
677 .rpc_message = msg,
678 .callback_ops = &nfs41_call_sync_ops,
679 .callback_data = &data
682 res->sr_slot = NULL;
683 if (privileged)
684 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
685 task = rpc_run_task(&task_setup);
686 if (IS_ERR(task))
687 ret = PTR_ERR(task);
688 else {
689 ret = task->tk_status;
690 rpc_put_task(task);
692 return ret;
695 int _nfs4_call_sync_session(struct nfs_server *server,
696 struct rpc_message *msg,
697 struct nfs4_sequence_args *args,
698 struct nfs4_sequence_res *res,
699 int cache_reply)
701 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
704 #else
705 static int nfs4_sequence_done(struct rpc_task *task,
706 struct nfs4_sequence_res *res)
708 return 1;
710 #endif /* CONFIG_NFS_V4_1 */
712 int _nfs4_call_sync(struct nfs_server *server,
713 struct rpc_message *msg,
714 struct nfs4_sequence_args *args,
715 struct nfs4_sequence_res *res,
716 int cache_reply)
718 args->sa_session = res->sr_session = NULL;
719 return rpc_call_sync(server->client, msg, 0);
722 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
723 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
724 &(res)->seq_res, (cache_reply))
726 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
728 struct nfs_inode *nfsi = NFS_I(dir);
730 spin_lock(&dir->i_lock);
731 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
732 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
733 nfs_force_lookup_revalidate(dir);
734 nfsi->change_attr = cinfo->after;
735 spin_unlock(&dir->i_lock);
738 struct nfs4_opendata {
739 struct kref kref;
740 struct nfs_openargs o_arg;
741 struct nfs_openres o_res;
742 struct nfs_open_confirmargs c_arg;
743 struct nfs_open_confirmres c_res;
744 struct nfs_fattr f_attr;
745 struct nfs_fattr dir_attr;
746 struct path path;
747 struct dentry *dir;
748 struct nfs4_state_owner *owner;
749 struct nfs4_state *state;
750 struct iattr attrs;
751 unsigned long timestamp;
752 unsigned int rpc_done : 1;
753 int rpc_status;
754 int cancelled;
758 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
760 p->o_res.f_attr = &p->f_attr;
761 p->o_res.dir_attr = &p->dir_attr;
762 p->o_res.seqid = p->o_arg.seqid;
763 p->c_res.seqid = p->c_arg.seqid;
764 p->o_res.server = p->o_arg.server;
765 nfs_fattr_init(&p->f_attr);
766 nfs_fattr_init(&p->dir_attr);
769 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
770 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
771 const struct iattr *attrs,
772 gfp_t gfp_mask)
774 struct dentry *parent = dget_parent(path->dentry);
775 struct inode *dir = parent->d_inode;
776 struct nfs_server *server = NFS_SERVER(dir);
777 struct nfs4_opendata *p;
779 p = kzalloc(sizeof(*p), gfp_mask);
780 if (p == NULL)
781 goto err;
782 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
783 if (p->o_arg.seqid == NULL)
784 goto err_free;
785 path_get(path);
786 p->path = *path;
787 p->dir = parent;
788 p->owner = sp;
789 atomic_inc(&sp->so_count);
790 p->o_arg.fh = NFS_FH(dir);
791 p->o_arg.open_flags = flags;
792 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
793 p->o_arg.clientid = server->nfs_client->cl_clientid;
794 p->o_arg.id = sp->so_owner_id.id;
795 p->o_arg.name = &p->path.dentry->d_name;
796 p->o_arg.server = server;
797 p->o_arg.bitmask = server->attr_bitmask;
798 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
799 if (flags & O_CREAT) {
800 u32 *s;
802 p->o_arg.u.attrs = &p->attrs;
803 memcpy(&p->attrs, attrs, sizeof(p->attrs));
804 s = (u32 *) p->o_arg.u.verifier.data;
805 s[0] = jiffies;
806 s[1] = current->pid;
808 p->c_arg.fh = &p->o_res.fh;
809 p->c_arg.stateid = &p->o_res.stateid;
810 p->c_arg.seqid = p->o_arg.seqid;
811 nfs4_init_opendata_res(p);
812 kref_init(&p->kref);
813 return p;
814 err_free:
815 kfree(p);
816 err:
817 dput(parent);
818 return NULL;
821 static void nfs4_opendata_free(struct kref *kref)
823 struct nfs4_opendata *p = container_of(kref,
824 struct nfs4_opendata, kref);
826 nfs_free_seqid(p->o_arg.seqid);
827 if (p->state != NULL)
828 nfs4_put_open_state(p->state);
829 nfs4_put_state_owner(p->owner);
830 dput(p->dir);
831 path_put(&p->path);
832 kfree(p);
835 static void nfs4_opendata_put(struct nfs4_opendata *p)
837 if (p != NULL)
838 kref_put(&p->kref, nfs4_opendata_free);
841 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
843 int ret;
845 ret = rpc_wait_for_completion_task(task);
846 return ret;
849 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
851 int ret = 0;
853 if (open_mode & O_EXCL)
854 goto out;
855 switch (mode & (FMODE_READ|FMODE_WRITE)) {
856 case FMODE_READ:
857 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
858 && state->n_rdonly != 0;
859 break;
860 case FMODE_WRITE:
861 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
862 && state->n_wronly != 0;
863 break;
864 case FMODE_READ|FMODE_WRITE:
865 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
866 && state->n_rdwr != 0;
868 out:
869 return ret;
872 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
874 if ((delegation->type & fmode) != fmode)
875 return 0;
876 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
877 return 0;
878 nfs_mark_delegation_referenced(delegation);
879 return 1;
882 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
884 switch (fmode) {
885 case FMODE_WRITE:
886 state->n_wronly++;
887 break;
888 case FMODE_READ:
889 state->n_rdonly++;
890 break;
891 case FMODE_READ|FMODE_WRITE:
892 state->n_rdwr++;
894 nfs4_state_set_mode_locked(state, state->state | fmode);
897 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
899 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
900 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
901 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
902 switch (fmode) {
903 case FMODE_READ:
904 set_bit(NFS_O_RDONLY_STATE, &state->flags);
905 break;
906 case FMODE_WRITE:
907 set_bit(NFS_O_WRONLY_STATE, &state->flags);
908 break;
909 case FMODE_READ|FMODE_WRITE:
910 set_bit(NFS_O_RDWR_STATE, &state->flags);
914 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
916 write_seqlock(&state->seqlock);
917 nfs_set_open_stateid_locked(state, stateid, fmode);
918 write_sequnlock(&state->seqlock);
921 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
924 * Protect the call to nfs4_state_set_mode_locked and
925 * serialise the stateid update
927 write_seqlock(&state->seqlock);
928 if (deleg_stateid != NULL) {
929 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
930 set_bit(NFS_DELEGATED_STATE, &state->flags);
932 if (open_stateid != NULL)
933 nfs_set_open_stateid_locked(state, open_stateid, fmode);
934 write_sequnlock(&state->seqlock);
935 spin_lock(&state->owner->so_lock);
936 update_open_stateflags(state, fmode);
937 spin_unlock(&state->owner->so_lock);
940 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
942 struct nfs_inode *nfsi = NFS_I(state->inode);
943 struct nfs_delegation *deleg_cur;
944 int ret = 0;
946 fmode &= (FMODE_READ|FMODE_WRITE);
948 rcu_read_lock();
949 deleg_cur = rcu_dereference(nfsi->delegation);
950 if (deleg_cur == NULL)
951 goto no_delegation;
953 spin_lock(&deleg_cur->lock);
954 if (nfsi->delegation != deleg_cur ||
955 (deleg_cur->type & fmode) != fmode)
956 goto no_delegation_unlock;
958 if (delegation == NULL)
959 delegation = &deleg_cur->stateid;
960 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
961 goto no_delegation_unlock;
963 nfs_mark_delegation_referenced(deleg_cur);
964 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
965 ret = 1;
966 no_delegation_unlock:
967 spin_unlock(&deleg_cur->lock);
968 no_delegation:
969 rcu_read_unlock();
971 if (!ret && open_stateid != NULL) {
972 __update_open_stateid(state, open_stateid, NULL, fmode);
973 ret = 1;
976 return ret;
980 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
982 struct nfs_delegation *delegation;
984 rcu_read_lock();
985 delegation = rcu_dereference(NFS_I(inode)->delegation);
986 if (delegation == NULL || (delegation->type & fmode) == fmode) {
987 rcu_read_unlock();
988 return;
990 rcu_read_unlock();
991 nfs_inode_return_delegation(inode);
994 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
996 struct nfs4_state *state = opendata->state;
997 struct nfs_inode *nfsi = NFS_I(state->inode);
998 struct nfs_delegation *delegation;
999 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1000 fmode_t fmode = opendata->o_arg.fmode;
1001 nfs4_stateid stateid;
1002 int ret = -EAGAIN;
1004 for (;;) {
1005 if (can_open_cached(state, fmode, open_mode)) {
1006 spin_lock(&state->owner->so_lock);
1007 if (can_open_cached(state, fmode, open_mode)) {
1008 update_open_stateflags(state, fmode);
1009 spin_unlock(&state->owner->so_lock);
1010 goto out_return_state;
1012 spin_unlock(&state->owner->so_lock);
1014 rcu_read_lock();
1015 delegation = rcu_dereference(nfsi->delegation);
1016 if (delegation == NULL ||
1017 !can_open_delegated(delegation, fmode)) {
1018 rcu_read_unlock();
1019 break;
1021 /* Save the delegation */
1022 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1023 rcu_read_unlock();
1024 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1025 if (ret != 0)
1026 goto out;
1027 ret = -EAGAIN;
1029 /* Try to update the stateid using the delegation */
1030 if (update_open_stateid(state, NULL, &stateid, fmode))
1031 goto out_return_state;
1033 out:
1034 return ERR_PTR(ret);
1035 out_return_state:
1036 atomic_inc(&state->count);
1037 return state;
1040 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1042 struct inode *inode;
1043 struct nfs4_state *state = NULL;
1044 struct nfs_delegation *delegation;
1045 int ret;
1047 if (!data->rpc_done) {
1048 state = nfs4_try_open_cached(data);
1049 goto out;
1052 ret = -EAGAIN;
1053 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1054 goto err;
1055 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1056 ret = PTR_ERR(inode);
1057 if (IS_ERR(inode))
1058 goto err;
1059 ret = -ENOMEM;
1060 state = nfs4_get_open_state(inode, data->owner);
1061 if (state == NULL)
1062 goto err_put_inode;
1063 if (data->o_res.delegation_type != 0) {
1064 int delegation_flags = 0;
1066 rcu_read_lock();
1067 delegation = rcu_dereference(NFS_I(inode)->delegation);
1068 if (delegation)
1069 delegation_flags = delegation->flags;
1070 rcu_read_unlock();
1071 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1072 nfs_inode_set_delegation(state->inode,
1073 data->owner->so_cred,
1074 &data->o_res);
1075 else
1076 nfs_inode_reclaim_delegation(state->inode,
1077 data->owner->so_cred,
1078 &data->o_res);
1081 update_open_stateid(state, &data->o_res.stateid, NULL,
1082 data->o_arg.fmode);
1083 iput(inode);
1084 out:
1085 return state;
1086 err_put_inode:
1087 iput(inode);
1088 err:
1089 return ERR_PTR(ret);
1092 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1094 struct nfs_inode *nfsi = NFS_I(state->inode);
1095 struct nfs_open_context *ctx;
1097 spin_lock(&state->inode->i_lock);
1098 list_for_each_entry(ctx, &nfsi->open_files, list) {
1099 if (ctx->state != state)
1100 continue;
1101 get_nfs_open_context(ctx);
1102 spin_unlock(&state->inode->i_lock);
1103 return ctx;
1105 spin_unlock(&state->inode->i_lock);
1106 return ERR_PTR(-ENOENT);
1109 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1111 struct nfs4_opendata *opendata;
1113 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1114 if (opendata == NULL)
1115 return ERR_PTR(-ENOMEM);
1116 opendata->state = state;
1117 atomic_inc(&state->count);
1118 return opendata;
1121 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1123 struct nfs4_state *newstate;
1124 int ret;
1126 opendata->o_arg.open_flags = 0;
1127 opendata->o_arg.fmode = fmode;
1128 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1129 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1130 nfs4_init_opendata_res(opendata);
1131 ret = _nfs4_recover_proc_open(opendata);
1132 if (ret != 0)
1133 return ret;
1134 newstate = nfs4_opendata_to_nfs4_state(opendata);
1135 if (IS_ERR(newstate))
1136 return PTR_ERR(newstate);
1137 nfs4_close_state(&opendata->path, newstate, fmode);
1138 *res = newstate;
1139 return 0;
1142 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1144 struct nfs4_state *newstate;
1145 int ret;
1147 /* memory barrier prior to reading state->n_* */
1148 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1149 smp_rmb();
1150 if (state->n_rdwr != 0) {
1151 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1152 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1153 if (ret != 0)
1154 return ret;
1155 if (newstate != state)
1156 return -ESTALE;
1158 if (state->n_wronly != 0) {
1159 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1160 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1161 if (ret != 0)
1162 return ret;
1163 if (newstate != state)
1164 return -ESTALE;
1166 if (state->n_rdonly != 0) {
1167 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1168 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1169 if (ret != 0)
1170 return ret;
1171 if (newstate != state)
1172 return -ESTALE;
1175 * We may have performed cached opens for all three recoveries.
1176 * Check if we need to update the current stateid.
1178 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1179 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1180 write_seqlock(&state->seqlock);
1181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1182 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1183 write_sequnlock(&state->seqlock);
1185 return 0;
1189 * OPEN_RECLAIM:
1190 * reclaim state on the server after a reboot.
1192 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1194 struct nfs_delegation *delegation;
1195 struct nfs4_opendata *opendata;
1196 fmode_t delegation_type = 0;
1197 int status;
1199 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1200 if (IS_ERR(opendata))
1201 return PTR_ERR(opendata);
1202 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1203 opendata->o_arg.fh = NFS_FH(state->inode);
1204 rcu_read_lock();
1205 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1206 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1207 delegation_type = delegation->type;
1208 rcu_read_unlock();
1209 opendata->o_arg.u.delegation_type = delegation_type;
1210 status = nfs4_open_recover(opendata, state);
1211 nfs4_opendata_put(opendata);
1212 return status;
1215 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1217 struct nfs_server *server = NFS_SERVER(state->inode);
1218 struct nfs4_exception exception = { };
1219 int err;
1220 do {
1221 err = _nfs4_do_open_reclaim(ctx, state);
1222 if (err != -NFS4ERR_DELAY)
1223 break;
1224 nfs4_handle_exception(server, err, &exception);
1225 } while (exception.retry);
1226 return err;
1229 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1231 struct nfs_open_context *ctx;
1232 int ret;
1234 ctx = nfs4_state_find_open_context(state);
1235 if (IS_ERR(ctx))
1236 return PTR_ERR(ctx);
1237 ret = nfs4_do_open_reclaim(ctx, state);
1238 put_nfs_open_context(ctx);
1239 return ret;
1242 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1244 struct nfs4_opendata *opendata;
1245 int ret;
1247 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1248 if (IS_ERR(opendata))
1249 return PTR_ERR(opendata);
1250 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1251 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1252 sizeof(opendata->o_arg.u.delegation.data));
1253 ret = nfs4_open_recover(opendata, state);
1254 nfs4_opendata_put(opendata);
1255 return ret;
1258 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1260 struct nfs4_exception exception = { };
1261 struct nfs_server *server = NFS_SERVER(state->inode);
1262 int err;
1263 do {
1264 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1265 switch (err) {
1266 case 0:
1267 case -ENOENT:
1268 case -ESTALE:
1269 goto out;
1270 case -NFS4ERR_BADSESSION:
1271 case -NFS4ERR_BADSLOT:
1272 case -NFS4ERR_BAD_HIGH_SLOT:
1273 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1274 case -NFS4ERR_DEADSESSION:
1275 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1276 goto out;
1277 case -NFS4ERR_STALE_CLIENTID:
1278 case -NFS4ERR_STALE_STATEID:
1279 case -NFS4ERR_EXPIRED:
1280 /* Don't recall a delegation if it was lost */
1281 nfs4_schedule_lease_recovery(server->nfs_client);
1282 goto out;
1283 case -ERESTARTSYS:
1285 * The show must go on: exit, but mark the
1286 * stateid as needing recovery.
1288 case -NFS4ERR_ADMIN_REVOKED:
1289 case -NFS4ERR_BAD_STATEID:
1290 nfs4_schedule_stateid_recovery(server, state);
1291 case -EKEYEXPIRED:
1293 * User RPCSEC_GSS context has expired.
1294 * We cannot recover this stateid now, so
1295 * skip it and allow recovery thread to
1296 * proceed.
1298 case -ENOMEM:
1299 err = 0;
1300 goto out;
1302 err = nfs4_handle_exception(server, err, &exception);
1303 } while (exception.retry);
1304 out:
1305 return err;
1308 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1310 struct nfs4_opendata *data = calldata;
1312 data->rpc_status = task->tk_status;
1313 if (data->rpc_status == 0) {
1314 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1315 sizeof(data->o_res.stateid.data));
1316 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1317 renew_lease(data->o_res.server, data->timestamp);
1318 data->rpc_done = 1;
1322 static void nfs4_open_confirm_release(void *calldata)
1324 struct nfs4_opendata *data = calldata;
1325 struct nfs4_state *state = NULL;
1327 /* If this request hasn't been cancelled, do nothing */
1328 if (data->cancelled == 0)
1329 goto out_free;
1330 /* In case of error, no cleanup! */
1331 if (!data->rpc_done)
1332 goto out_free;
1333 state = nfs4_opendata_to_nfs4_state(data);
1334 if (!IS_ERR(state))
1335 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1336 out_free:
1337 nfs4_opendata_put(data);
1340 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1341 .rpc_call_done = nfs4_open_confirm_done,
1342 .rpc_release = nfs4_open_confirm_release,
1346 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1348 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1350 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1351 struct rpc_task *task;
1352 struct rpc_message msg = {
1353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1354 .rpc_argp = &data->c_arg,
1355 .rpc_resp = &data->c_res,
1356 .rpc_cred = data->owner->so_cred,
1358 struct rpc_task_setup task_setup_data = {
1359 .rpc_client = server->client,
1360 .rpc_message = &msg,
1361 .callback_ops = &nfs4_open_confirm_ops,
1362 .callback_data = data,
1363 .workqueue = nfsiod_workqueue,
1364 .flags = RPC_TASK_ASYNC,
1366 int status;
1368 kref_get(&data->kref);
1369 data->rpc_done = 0;
1370 data->rpc_status = 0;
1371 data->timestamp = jiffies;
1372 task = rpc_run_task(&task_setup_data);
1373 if (IS_ERR(task))
1374 return PTR_ERR(task);
1375 status = nfs4_wait_for_completion_rpc_task(task);
1376 if (status != 0) {
1377 data->cancelled = 1;
1378 smp_wmb();
1379 } else
1380 status = data->rpc_status;
1381 rpc_put_task(task);
1382 return status;
1385 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1387 struct nfs4_opendata *data = calldata;
1388 struct nfs4_state_owner *sp = data->owner;
1390 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1391 return;
1393 * Check if we still need to send an OPEN call, or if we can use
1394 * a delegation instead.
1396 if (data->state != NULL) {
1397 struct nfs_delegation *delegation;
1399 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1400 goto out_no_action;
1401 rcu_read_lock();
1402 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1403 if (delegation != NULL &&
1404 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1405 rcu_read_unlock();
1406 goto out_no_action;
1408 rcu_read_unlock();
1410 /* Update sequence id. */
1411 data->o_arg.id = sp->so_owner_id.id;
1412 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1413 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1414 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1415 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1417 data->timestamp = jiffies;
1418 if (nfs4_setup_sequence(data->o_arg.server,
1419 &data->o_arg.seq_args,
1420 &data->o_res.seq_res, 1, task))
1421 return;
1422 rpc_call_start(task);
1423 return;
1424 out_no_action:
1425 task->tk_action = NULL;
1429 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1431 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1432 nfs4_open_prepare(task, calldata);
1435 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1437 struct nfs4_opendata *data = calldata;
1439 data->rpc_status = task->tk_status;
1441 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1442 return;
1444 if (task->tk_status == 0) {
1445 switch (data->o_res.f_attr->mode & S_IFMT) {
1446 case S_IFREG:
1447 break;
1448 case S_IFLNK:
1449 data->rpc_status = -ELOOP;
1450 break;
1451 case S_IFDIR:
1452 data->rpc_status = -EISDIR;
1453 break;
1454 default:
1455 data->rpc_status = -ENOTDIR;
1457 renew_lease(data->o_res.server, data->timestamp);
1458 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1459 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1461 data->rpc_done = 1;
1464 static void nfs4_open_release(void *calldata)
1466 struct nfs4_opendata *data = calldata;
1467 struct nfs4_state *state = NULL;
1469 /* If this request hasn't been cancelled, do nothing */
1470 if (data->cancelled == 0)
1471 goto out_free;
1472 /* In case of error, no cleanup! */
1473 if (data->rpc_status != 0 || !data->rpc_done)
1474 goto out_free;
1475 /* In case we need an open_confirm, no cleanup! */
1476 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1477 goto out_free;
1478 state = nfs4_opendata_to_nfs4_state(data);
1479 if (!IS_ERR(state))
1480 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1481 out_free:
1482 nfs4_opendata_put(data);
1485 static const struct rpc_call_ops nfs4_open_ops = {
1486 .rpc_call_prepare = nfs4_open_prepare,
1487 .rpc_call_done = nfs4_open_done,
1488 .rpc_release = nfs4_open_release,
1491 static const struct rpc_call_ops nfs4_recover_open_ops = {
1492 .rpc_call_prepare = nfs4_recover_open_prepare,
1493 .rpc_call_done = nfs4_open_done,
1494 .rpc_release = nfs4_open_release,
1497 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1499 struct inode *dir = data->dir->d_inode;
1500 struct nfs_server *server = NFS_SERVER(dir);
1501 struct nfs_openargs *o_arg = &data->o_arg;
1502 struct nfs_openres *o_res = &data->o_res;
1503 struct rpc_task *task;
1504 struct rpc_message msg = {
1505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1506 .rpc_argp = o_arg,
1507 .rpc_resp = o_res,
1508 .rpc_cred = data->owner->so_cred,
1510 struct rpc_task_setup task_setup_data = {
1511 .rpc_client = server->client,
1512 .rpc_message = &msg,
1513 .callback_ops = &nfs4_open_ops,
1514 .callback_data = data,
1515 .workqueue = nfsiod_workqueue,
1516 .flags = RPC_TASK_ASYNC,
1518 int status;
1520 kref_get(&data->kref);
1521 data->rpc_done = 0;
1522 data->rpc_status = 0;
1523 data->cancelled = 0;
1524 if (isrecover)
1525 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1526 task = rpc_run_task(&task_setup_data);
1527 if (IS_ERR(task))
1528 return PTR_ERR(task);
1529 status = nfs4_wait_for_completion_rpc_task(task);
1530 if (status != 0) {
1531 data->cancelled = 1;
1532 smp_wmb();
1533 } else
1534 status = data->rpc_status;
1535 rpc_put_task(task);
1537 return status;
1540 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1542 struct inode *dir = data->dir->d_inode;
1543 struct nfs_openres *o_res = &data->o_res;
1544 int status;
1546 status = nfs4_run_open_task(data, 1);
1547 if (status != 0 || !data->rpc_done)
1548 return status;
1550 nfs_refresh_inode(dir, o_res->dir_attr);
1552 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1553 status = _nfs4_proc_open_confirm(data);
1554 if (status != 0)
1555 return status;
1558 return status;
1562 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1564 static int _nfs4_proc_open(struct nfs4_opendata *data)
1566 struct inode *dir = data->dir->d_inode;
1567 struct nfs_server *server = NFS_SERVER(dir);
1568 struct nfs_openargs *o_arg = &data->o_arg;
1569 struct nfs_openres *o_res = &data->o_res;
1570 int status;
1572 status = nfs4_run_open_task(data, 0);
1573 if (status != 0 || !data->rpc_done)
1574 return status;
1576 if (o_arg->open_flags & O_CREAT) {
1577 update_changeattr(dir, &o_res->cinfo);
1578 nfs_post_op_update_inode(dir, o_res->dir_attr);
1579 } else
1580 nfs_refresh_inode(dir, o_res->dir_attr);
1581 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1582 server->caps &= ~NFS_CAP_POSIX_LOCK;
1583 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1584 status = _nfs4_proc_open_confirm(data);
1585 if (status != 0)
1586 return status;
1588 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1589 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1590 return 0;
1593 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1595 unsigned int loop;
1596 int ret;
1598 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1599 ret = nfs4_wait_clnt_recover(clp);
1600 if (ret != 0)
1601 break;
1602 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1603 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1604 break;
1605 nfs4_schedule_state_manager(clp);
1606 ret = -EIO;
1608 return ret;
1611 static int nfs4_recover_expired_lease(struct nfs_server *server)
1613 return nfs4_client_recover_expired_lease(server->nfs_client);
1617 * OPEN_EXPIRED:
1618 * reclaim state on the server after a network partition.
1619 * Assumes caller holds the appropriate lock
1621 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1623 struct nfs4_opendata *opendata;
1624 int ret;
1626 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1627 if (IS_ERR(opendata))
1628 return PTR_ERR(opendata);
1629 ret = nfs4_open_recover(opendata, state);
1630 if (ret == -ESTALE)
1631 d_drop(ctx->path.dentry);
1632 nfs4_opendata_put(opendata);
1633 return ret;
1636 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1638 struct nfs_server *server = NFS_SERVER(state->inode);
1639 struct nfs4_exception exception = { };
1640 int err;
1642 do {
1643 err = _nfs4_open_expired(ctx, state);
1644 switch (err) {
1645 default:
1646 goto out;
1647 case -NFS4ERR_GRACE:
1648 case -NFS4ERR_DELAY:
1649 nfs4_handle_exception(server, err, &exception);
1650 err = 0;
1652 } while (exception.retry);
1653 out:
1654 return err;
1657 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1659 struct nfs_open_context *ctx;
1660 int ret;
1662 ctx = nfs4_state_find_open_context(state);
1663 if (IS_ERR(ctx))
1664 return PTR_ERR(ctx);
1665 ret = nfs4_do_open_expired(ctx, state);
1666 put_nfs_open_context(ctx);
1667 return ret;
1671 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1672 * fields corresponding to attributes that were used to store the verifier.
1673 * Make sure we clobber those fields in the later setattr call
1675 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1677 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1678 !(sattr->ia_valid & ATTR_ATIME_SET))
1679 sattr->ia_valid |= ATTR_ATIME;
1681 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1682 !(sattr->ia_valid & ATTR_MTIME_SET))
1683 sattr->ia_valid |= ATTR_MTIME;
1687 * Returns a referenced nfs4_state
1689 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)
1691 struct nfs4_state_owner *sp;
1692 struct nfs4_state *state = NULL;
1693 struct nfs_server *server = NFS_SERVER(dir);
1694 struct nfs4_opendata *opendata;
1695 int status;
1697 /* Protect against reboot recovery conflicts */
1698 status = -ENOMEM;
1699 if (!(sp = nfs4_get_state_owner(server, cred))) {
1700 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1701 goto out_err;
1703 status = nfs4_recover_expired_lease(server);
1704 if (status != 0)
1705 goto err_put_state_owner;
1706 if (path->dentry->d_inode != NULL)
1707 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1708 status = -ENOMEM;
1709 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1710 if (opendata == NULL)
1711 goto err_put_state_owner;
1713 if (path->dentry->d_inode != NULL)
1714 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1716 status = _nfs4_proc_open(opendata);
1717 if (status != 0)
1718 goto err_opendata_put;
1720 state = nfs4_opendata_to_nfs4_state(opendata);
1721 status = PTR_ERR(state);
1722 if (IS_ERR(state))
1723 goto err_opendata_put;
1724 if (server->caps & NFS_CAP_POSIX_LOCK)
1725 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1727 if (opendata->o_arg.open_flags & O_EXCL) {
1728 nfs4_exclusive_attrset(opendata, sattr);
1730 nfs_fattr_init(opendata->o_res.f_attr);
1731 status = nfs4_do_setattr(state->inode, cred,
1732 opendata->o_res.f_attr, sattr,
1733 state);
1734 if (status == 0)
1735 nfs_setattr_update_inode(state->inode, sattr);
1736 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1738 nfs4_opendata_put(opendata);
1739 nfs4_put_state_owner(sp);
1740 *res = state;
1741 return 0;
1742 err_opendata_put:
1743 nfs4_opendata_put(opendata);
1744 err_put_state_owner:
1745 nfs4_put_state_owner(sp);
1746 out_err:
1747 *res = NULL;
1748 return status;
1752 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)
1754 struct nfs4_exception exception = { };
1755 struct nfs4_state *res;
1756 int status;
1758 do {
1759 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1760 if (status == 0)
1761 break;
1762 /* NOTE: BAD_SEQID means the server and client disagree about the
1763 * book-keeping w.r.t. state-changing operations
1764 * (OPEN/CLOSE/LOCK/LOCKU...)
1765 * It is actually a sign of a bug on the client or on the server.
1767 * If we receive a BAD_SEQID error in the particular case of
1768 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1769 * have unhashed the old state_owner for us, and that we can
1770 * therefore safely retry using a new one. We should still warn
1771 * the user though...
1773 if (status == -NFS4ERR_BAD_SEQID) {
1774 printk(KERN_WARNING "NFS: v4 server %s "
1775 " returned a bad sequence-id error!\n",
1776 NFS_SERVER(dir)->nfs_client->cl_hostname);
1777 exception.retry = 1;
1778 continue;
1781 * BAD_STATEID on OPEN means that the server cancelled our
1782 * state before it received the OPEN_CONFIRM.
1783 * Recover by retrying the request as per the discussion
1784 * on Page 181 of RFC3530.
1786 if (status == -NFS4ERR_BAD_STATEID) {
1787 exception.retry = 1;
1788 continue;
1790 if (status == -EAGAIN) {
1791 /* We must have found a delegation */
1792 exception.retry = 1;
1793 continue;
1795 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1796 status, &exception));
1797 } while (exception.retry);
1798 return res;
1801 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1802 struct nfs_fattr *fattr, struct iattr *sattr,
1803 struct nfs4_state *state)
1805 struct nfs_server *server = NFS_SERVER(inode);
1806 struct nfs_setattrargs arg = {
1807 .fh = NFS_FH(inode),
1808 .iap = sattr,
1809 .server = server,
1810 .bitmask = server->attr_bitmask,
1812 struct nfs_setattrres res = {
1813 .fattr = fattr,
1814 .server = server,
1816 struct rpc_message msg = {
1817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1818 .rpc_argp = &arg,
1819 .rpc_resp = &res,
1820 .rpc_cred = cred,
1822 unsigned long timestamp = jiffies;
1823 int status;
1825 nfs_fattr_init(fattr);
1827 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1828 /* Use that stateid */
1829 } else if (state != NULL) {
1830 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1831 } else
1832 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1834 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1835 if (status == 0 && state != NULL)
1836 renew_lease(server, timestamp);
1837 return status;
1840 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1841 struct nfs_fattr *fattr, struct iattr *sattr,
1842 struct nfs4_state *state)
1844 struct nfs_server *server = NFS_SERVER(inode);
1845 struct nfs4_exception exception = { };
1846 int err;
1847 do {
1848 err = nfs4_handle_exception(server,
1849 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1850 &exception);
1851 } while (exception.retry);
1852 return err;
1855 struct nfs4_closedata {
1856 struct path path;
1857 struct inode *inode;
1858 struct nfs4_state *state;
1859 struct nfs_closeargs arg;
1860 struct nfs_closeres res;
1861 struct nfs_fattr fattr;
1862 unsigned long timestamp;
1863 bool roc;
1864 u32 roc_barrier;
1867 static void nfs4_free_closedata(void *data)
1869 struct nfs4_closedata *calldata = data;
1870 struct nfs4_state_owner *sp = calldata->state->owner;
1872 if (calldata->roc)
1873 pnfs_roc_release(calldata->state->inode);
1874 nfs4_put_open_state(calldata->state);
1875 nfs_free_seqid(calldata->arg.seqid);
1876 nfs4_put_state_owner(sp);
1877 path_put(&calldata->path);
1878 kfree(calldata);
1881 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1882 fmode_t fmode)
1884 spin_lock(&state->owner->so_lock);
1885 if (!(fmode & FMODE_READ))
1886 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1887 if (!(fmode & FMODE_WRITE))
1888 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1889 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1890 spin_unlock(&state->owner->so_lock);
1893 static void nfs4_close_done(struct rpc_task *task, void *data)
1895 struct nfs4_closedata *calldata = data;
1896 struct nfs4_state *state = calldata->state;
1897 struct nfs_server *server = NFS_SERVER(calldata->inode);
1899 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1900 return;
1901 /* hmm. we are done with the inode, and in the process of freeing
1902 * the state_owner. we keep this around to process errors
1904 switch (task->tk_status) {
1905 case 0:
1906 if (calldata->roc)
1907 pnfs_roc_set_barrier(state->inode,
1908 calldata->roc_barrier);
1909 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1910 renew_lease(server, calldata->timestamp);
1911 nfs4_close_clear_stateid_flags(state,
1912 calldata->arg.fmode);
1913 break;
1914 case -NFS4ERR_STALE_STATEID:
1915 case -NFS4ERR_OLD_STATEID:
1916 case -NFS4ERR_BAD_STATEID:
1917 case -NFS4ERR_EXPIRED:
1918 if (calldata->arg.fmode == 0)
1919 break;
1920 default:
1921 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1922 rpc_restart_call_prepare(task);
1924 nfs_release_seqid(calldata->arg.seqid);
1925 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1928 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1930 struct nfs4_closedata *calldata = data;
1931 struct nfs4_state *state = calldata->state;
1932 int call_close = 0;
1934 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1935 return;
1937 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1938 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1939 spin_lock(&state->owner->so_lock);
1940 /* Calculate the change in open mode */
1941 if (state->n_rdwr == 0) {
1942 if (state->n_rdonly == 0) {
1943 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1944 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1945 calldata->arg.fmode &= ~FMODE_READ;
1947 if (state->n_wronly == 0) {
1948 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1949 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1950 calldata->arg.fmode &= ~FMODE_WRITE;
1953 spin_unlock(&state->owner->so_lock);
1955 if (!call_close) {
1956 /* Note: exit _without_ calling nfs4_close_done */
1957 task->tk_action = NULL;
1958 return;
1961 if (calldata->arg.fmode == 0) {
1962 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1963 if (calldata->roc &&
1964 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1965 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1966 task, NULL);
1967 return;
1971 nfs_fattr_init(calldata->res.fattr);
1972 calldata->timestamp = jiffies;
1973 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1974 &calldata->arg.seq_args, &calldata->res.seq_res,
1975 1, task))
1976 return;
1977 rpc_call_start(task);
1980 static const struct rpc_call_ops nfs4_close_ops = {
1981 .rpc_call_prepare = nfs4_close_prepare,
1982 .rpc_call_done = nfs4_close_done,
1983 .rpc_release = nfs4_free_closedata,
1987 * It is possible for data to be read/written from a mem-mapped file
1988 * after the sys_close call (which hits the vfs layer as a flush).
1989 * This means that we can't safely call nfsv4 close on a file until
1990 * the inode is cleared. This in turn means that we are not good
1991 * NFSv4 citizens - we do not indicate to the server to update the file's
1992 * share state even when we are done with one of the three share
1993 * stateid's in the inode.
1995 * NOTE: Caller must be holding the sp->so_owner semaphore!
1997 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
1999 struct nfs_server *server = NFS_SERVER(state->inode);
2000 struct nfs4_closedata *calldata;
2001 struct nfs4_state_owner *sp = state->owner;
2002 struct rpc_task *task;
2003 struct rpc_message msg = {
2004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2005 .rpc_cred = state->owner->so_cred,
2007 struct rpc_task_setup task_setup_data = {
2008 .rpc_client = server->client,
2009 .rpc_message = &msg,
2010 .callback_ops = &nfs4_close_ops,
2011 .workqueue = nfsiod_workqueue,
2012 .flags = RPC_TASK_ASYNC,
2014 int status = -ENOMEM;
2016 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2017 if (calldata == NULL)
2018 goto out;
2019 calldata->inode = state->inode;
2020 calldata->state = state;
2021 calldata->arg.fh = NFS_FH(state->inode);
2022 calldata->arg.stateid = &state->open_stateid;
2023 /* Serialization for the sequence id */
2024 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2025 if (calldata->arg.seqid == NULL)
2026 goto out_free_calldata;
2027 calldata->arg.fmode = 0;
2028 calldata->arg.bitmask = server->cache_consistency_bitmask;
2029 calldata->res.fattr = &calldata->fattr;
2030 calldata->res.seqid = calldata->arg.seqid;
2031 calldata->res.server = server;
2032 calldata->roc = roc;
2033 path_get(path);
2034 calldata->path = *path;
2036 msg.rpc_argp = &calldata->arg;
2037 msg.rpc_resp = &calldata->res;
2038 task_setup_data.callback_data = calldata;
2039 task = rpc_run_task(&task_setup_data);
2040 if (IS_ERR(task))
2041 return PTR_ERR(task);
2042 status = 0;
2043 if (wait)
2044 status = rpc_wait_for_completion_task(task);
2045 rpc_put_task(task);
2046 return status;
2047 out_free_calldata:
2048 kfree(calldata);
2049 out:
2050 if (roc)
2051 pnfs_roc_release(state->inode);
2052 nfs4_put_open_state(state);
2053 nfs4_put_state_owner(sp);
2054 return status;
2057 static struct inode *
2058 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2060 struct nfs4_state *state;
2062 /* Protect against concurrent sillydeletes */
2063 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2064 if (IS_ERR(state))
2065 return ERR_CAST(state);
2066 ctx->state = state;
2067 return igrab(state->inode);
2070 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2072 if (ctx->state == NULL)
2073 return;
2074 if (is_sync)
2075 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2076 else
2077 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2080 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2082 struct nfs4_server_caps_arg args = {
2083 .fhandle = fhandle,
2085 struct nfs4_server_caps_res res = {};
2086 struct rpc_message msg = {
2087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2088 .rpc_argp = &args,
2089 .rpc_resp = &res,
2091 int status;
2093 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2094 if (status == 0) {
2095 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2096 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2097 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2098 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2099 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2100 NFS_CAP_CTIME|NFS_CAP_MTIME);
2101 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2102 server->caps |= NFS_CAP_ACLS;
2103 if (res.has_links != 0)
2104 server->caps |= NFS_CAP_HARDLINKS;
2105 if (res.has_symlinks != 0)
2106 server->caps |= NFS_CAP_SYMLINKS;
2107 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2108 server->caps |= NFS_CAP_FILEID;
2109 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2110 server->caps |= NFS_CAP_MODE;
2111 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2112 server->caps |= NFS_CAP_NLINK;
2113 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2114 server->caps |= NFS_CAP_OWNER;
2115 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2116 server->caps |= NFS_CAP_OWNER_GROUP;
2117 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2118 server->caps |= NFS_CAP_ATIME;
2119 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2120 server->caps |= NFS_CAP_CTIME;
2121 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2122 server->caps |= NFS_CAP_MTIME;
2124 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2125 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2126 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2127 server->acl_bitmask = res.acl_bitmask;
2130 return status;
2133 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2135 struct nfs4_exception exception = { };
2136 int err;
2137 do {
2138 err = nfs4_handle_exception(server,
2139 _nfs4_server_capabilities(server, fhandle),
2140 &exception);
2141 } while (exception.retry);
2142 return err;
2145 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2146 struct nfs_fsinfo *info)
2148 struct nfs4_lookup_root_arg args = {
2149 .bitmask = nfs4_fattr_bitmap,
2151 struct nfs4_lookup_res res = {
2152 .server = server,
2153 .fattr = info->fattr,
2154 .fh = fhandle,
2156 struct rpc_message msg = {
2157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2158 .rpc_argp = &args,
2159 .rpc_resp = &res,
2162 nfs_fattr_init(info->fattr);
2163 return nfs4_call_sync(server, &msg, &args, &res, 0);
2166 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2167 struct nfs_fsinfo *info)
2169 struct nfs4_exception exception = { };
2170 int err;
2171 do {
2172 err = nfs4_handle_exception(server,
2173 _nfs4_lookup_root(server, fhandle, info),
2174 &exception);
2175 } while (exception.retry);
2176 return err;
2180 * get the file handle for the "/" directory on the server
2182 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2183 struct nfs_fsinfo *info)
2185 int status;
2187 status = nfs4_lookup_root(server, fhandle, info);
2188 if (status == 0)
2189 status = nfs4_server_capabilities(server, fhandle);
2190 if (status == 0)
2191 status = nfs4_do_fsinfo(server, fhandle, info);
2192 return nfs4_map_errors(status);
2196 * Get locations and (maybe) other attributes of a referral.
2197 * Note that we'll actually follow the referral later when
2198 * we detect fsid mismatch in inode revalidation
2200 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2202 int status = -ENOMEM;
2203 struct page *page = NULL;
2204 struct nfs4_fs_locations *locations = NULL;
2206 page = alloc_page(GFP_KERNEL);
2207 if (page == NULL)
2208 goto out;
2209 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2210 if (locations == NULL)
2211 goto out;
2213 status = nfs4_proc_fs_locations(dir, name, locations, page);
2214 if (status != 0)
2215 goto out;
2216 /* Make sure server returned a different fsid for the referral */
2217 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2218 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2219 status = -EIO;
2220 goto out;
2223 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2224 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2225 if (!fattr->mode)
2226 fattr->mode = S_IFDIR;
2227 memset(fhandle, 0, sizeof(struct nfs_fh));
2228 out:
2229 if (page)
2230 __free_page(page);
2231 kfree(locations);
2232 return status;
2235 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2237 struct nfs4_getattr_arg args = {
2238 .fh = fhandle,
2239 .bitmask = server->attr_bitmask,
2241 struct nfs4_getattr_res res = {
2242 .fattr = fattr,
2243 .server = server,
2245 struct rpc_message msg = {
2246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2247 .rpc_argp = &args,
2248 .rpc_resp = &res,
2251 nfs_fattr_init(fattr);
2252 return nfs4_call_sync(server, &msg, &args, &res, 0);
2255 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2257 struct nfs4_exception exception = { };
2258 int err;
2259 do {
2260 err = nfs4_handle_exception(server,
2261 _nfs4_proc_getattr(server, fhandle, fattr),
2262 &exception);
2263 } while (exception.retry);
2264 return err;
2268 * The file is not closed if it is opened due to the a request to change
2269 * the size of the file. The open call will not be needed once the
2270 * VFS layer lookup-intents are implemented.
2272 * Close is called when the inode is destroyed.
2273 * If we haven't opened the file for O_WRONLY, we
2274 * need to in the size_change case to obtain a stateid.
2276 * Got race?
2277 * Because OPEN is always done by name in nfsv4, it is
2278 * possible that we opened a different file by the same
2279 * name. We can recognize this race condition, but we
2280 * can't do anything about it besides returning an error.
2282 * This will be fixed with VFS changes (lookup-intent).
2284 static int
2285 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2286 struct iattr *sattr)
2288 struct inode *inode = dentry->d_inode;
2289 struct rpc_cred *cred = NULL;
2290 struct nfs4_state *state = NULL;
2291 int status;
2293 nfs_fattr_init(fattr);
2295 /* Search for an existing open(O_WRITE) file */
2296 if (sattr->ia_valid & ATTR_FILE) {
2297 struct nfs_open_context *ctx;
2299 ctx = nfs_file_open_context(sattr->ia_file);
2300 if (ctx) {
2301 cred = ctx->cred;
2302 state = ctx->state;
2306 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2307 if (status == 0)
2308 nfs_setattr_update_inode(inode, sattr);
2309 return status;
2312 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2313 const struct qstr *name, struct nfs_fh *fhandle,
2314 struct nfs_fattr *fattr)
2316 int status;
2317 struct nfs4_lookup_arg args = {
2318 .bitmask = server->attr_bitmask,
2319 .dir_fh = dirfh,
2320 .name = name,
2322 struct nfs4_lookup_res res = {
2323 .server = server,
2324 .fattr = fattr,
2325 .fh = fhandle,
2327 struct rpc_message msg = {
2328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2329 .rpc_argp = &args,
2330 .rpc_resp = &res,
2333 nfs_fattr_init(fattr);
2335 dprintk("NFS call lookupfh %s\n", name->name);
2336 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2337 dprintk("NFS reply lookupfh: %d\n", status);
2338 return status;
2341 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2342 struct qstr *name, struct nfs_fh *fhandle,
2343 struct nfs_fattr *fattr)
2345 struct nfs4_exception exception = { };
2346 int err;
2347 do {
2348 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2349 /* FIXME: !!!! */
2350 if (err == -NFS4ERR_MOVED) {
2351 err = -EREMOTE;
2352 break;
2354 err = nfs4_handle_exception(server, err, &exception);
2355 } while (exception.retry);
2356 return err;
2359 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2360 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2362 int status;
2364 dprintk("NFS call lookup %s\n", name->name);
2365 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2366 if (status == -NFS4ERR_MOVED)
2367 status = nfs4_get_referral(dir, name, fattr, fhandle);
2368 dprintk("NFS reply lookup: %d\n", status);
2369 return status;
2372 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2374 struct nfs4_exception exception = { };
2375 int err;
2376 do {
2377 err = nfs4_handle_exception(NFS_SERVER(dir),
2378 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2379 &exception);
2380 } while (exception.retry);
2381 return err;
2384 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2386 struct nfs_server *server = NFS_SERVER(inode);
2387 struct nfs4_accessargs args = {
2388 .fh = NFS_FH(inode),
2389 .bitmask = server->attr_bitmask,
2391 struct nfs4_accessres res = {
2392 .server = server,
2394 struct rpc_message msg = {
2395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2396 .rpc_argp = &args,
2397 .rpc_resp = &res,
2398 .rpc_cred = entry->cred,
2400 int mode = entry->mask;
2401 int status;
2404 * Determine which access bits we want to ask for...
2406 if (mode & MAY_READ)
2407 args.access |= NFS4_ACCESS_READ;
2408 if (S_ISDIR(inode->i_mode)) {
2409 if (mode & MAY_WRITE)
2410 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2411 if (mode & MAY_EXEC)
2412 args.access |= NFS4_ACCESS_LOOKUP;
2413 } else {
2414 if (mode & MAY_WRITE)
2415 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2416 if (mode & MAY_EXEC)
2417 args.access |= NFS4_ACCESS_EXECUTE;
2420 res.fattr = nfs_alloc_fattr();
2421 if (res.fattr == NULL)
2422 return -ENOMEM;
2424 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2425 if (!status) {
2426 entry->mask = 0;
2427 if (res.access & NFS4_ACCESS_READ)
2428 entry->mask |= MAY_READ;
2429 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2430 entry->mask |= MAY_WRITE;
2431 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2432 entry->mask |= MAY_EXEC;
2433 nfs_refresh_inode(inode, res.fattr);
2435 nfs_free_fattr(res.fattr);
2436 return status;
2439 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2441 struct nfs4_exception exception = { };
2442 int err;
2443 do {
2444 err = nfs4_handle_exception(NFS_SERVER(inode),
2445 _nfs4_proc_access(inode, entry),
2446 &exception);
2447 } while (exception.retry);
2448 return err;
2452 * TODO: For the time being, we don't try to get any attributes
2453 * along with any of the zero-copy operations READ, READDIR,
2454 * READLINK, WRITE.
2456 * In the case of the first three, we want to put the GETATTR
2457 * after the read-type operation -- this is because it is hard
2458 * to predict the length of a GETATTR response in v4, and thus
2459 * align the READ data correctly. This means that the GETATTR
2460 * may end up partially falling into the page cache, and we should
2461 * shift it into the 'tail' of the xdr_buf before processing.
2462 * To do this efficiently, we need to know the total length
2463 * of data received, which doesn't seem to be available outside
2464 * of the RPC layer.
2466 * In the case of WRITE, we also want to put the GETATTR after
2467 * the operation -- in this case because we want to make sure
2468 * we get the post-operation mtime and size. This means that
2469 * we can't use xdr_encode_pages() as written: we need a variant
2470 * of it which would leave room in the 'tail' iovec.
2472 * Both of these changes to the XDR layer would in fact be quite
2473 * minor, but I decided to leave them for a subsequent patch.
2475 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2476 unsigned int pgbase, unsigned int pglen)
2478 struct nfs4_readlink args = {
2479 .fh = NFS_FH(inode),
2480 .pgbase = pgbase,
2481 .pglen = pglen,
2482 .pages = &page,
2484 struct nfs4_readlink_res res;
2485 struct rpc_message msg = {
2486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2487 .rpc_argp = &args,
2488 .rpc_resp = &res,
2491 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2494 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2495 unsigned int pgbase, unsigned int pglen)
2497 struct nfs4_exception exception = { };
2498 int err;
2499 do {
2500 err = nfs4_handle_exception(NFS_SERVER(inode),
2501 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2502 &exception);
2503 } while (exception.retry);
2504 return err;
2508 * Got race?
2509 * We will need to arrange for the VFS layer to provide an atomic open.
2510 * Until then, this create/open method is prone to inefficiency and race
2511 * conditions due to the lookup, create, and open VFS calls from sys_open()
2512 * placed on the wire.
2514 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2515 * The file will be opened again in the subsequent VFS open call
2516 * (nfs4_proc_file_open).
2518 * The open for read will just hang around to be used by any process that
2519 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2522 static int
2523 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2524 int flags, struct nfs_open_context *ctx)
2526 struct path my_path = {
2527 .dentry = dentry,
2529 struct path *path = &my_path;
2530 struct nfs4_state *state;
2531 struct rpc_cred *cred = NULL;
2532 fmode_t fmode = 0;
2533 int status = 0;
2535 if (ctx != NULL) {
2536 cred = ctx->cred;
2537 path = &ctx->path;
2538 fmode = ctx->mode;
2540 sattr->ia_mode &= ~current_umask();
2541 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2542 d_drop(dentry);
2543 if (IS_ERR(state)) {
2544 status = PTR_ERR(state);
2545 goto out;
2547 d_add(dentry, igrab(state->inode));
2548 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2549 if (ctx != NULL)
2550 ctx->state = state;
2551 else
2552 nfs4_close_sync(path, state, fmode);
2553 out:
2554 return status;
2557 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2559 struct nfs_server *server = NFS_SERVER(dir);
2560 struct nfs_removeargs args = {
2561 .fh = NFS_FH(dir),
2562 .name.len = name->len,
2563 .name.name = name->name,
2564 .bitmask = server->attr_bitmask,
2566 struct nfs_removeres res = {
2567 .server = server,
2569 struct rpc_message msg = {
2570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2571 .rpc_argp = &args,
2572 .rpc_resp = &res,
2574 int status = -ENOMEM;
2576 res.dir_attr = nfs_alloc_fattr();
2577 if (res.dir_attr == NULL)
2578 goto out;
2580 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2581 if (status == 0) {
2582 update_changeattr(dir, &res.cinfo);
2583 nfs_post_op_update_inode(dir, res.dir_attr);
2585 nfs_free_fattr(res.dir_attr);
2586 out:
2587 return status;
2590 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2592 struct nfs4_exception exception = { };
2593 int err;
2594 do {
2595 err = nfs4_handle_exception(NFS_SERVER(dir),
2596 _nfs4_proc_remove(dir, name),
2597 &exception);
2598 } while (exception.retry);
2599 return err;
2602 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2604 struct nfs_server *server = NFS_SERVER(dir);
2605 struct nfs_removeargs *args = msg->rpc_argp;
2606 struct nfs_removeres *res = msg->rpc_resp;
2608 args->bitmask = server->cache_consistency_bitmask;
2609 res->server = server;
2610 res->seq_res.sr_slot = NULL;
2611 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2614 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2616 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2618 if (!nfs4_sequence_done(task, &res->seq_res))
2619 return 0;
2620 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2621 return 0;
2622 update_changeattr(dir, &res->cinfo);
2623 nfs_post_op_update_inode(dir, res->dir_attr);
2624 return 1;
2627 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2629 struct nfs_server *server = NFS_SERVER(dir);
2630 struct nfs_renameargs *arg = msg->rpc_argp;
2631 struct nfs_renameres *res = msg->rpc_resp;
2633 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2634 arg->bitmask = server->attr_bitmask;
2635 res->server = server;
2638 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2639 struct inode *new_dir)
2641 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2643 if (!nfs4_sequence_done(task, &res->seq_res))
2644 return 0;
2645 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2646 return 0;
2648 update_changeattr(old_dir, &res->old_cinfo);
2649 nfs_post_op_update_inode(old_dir, res->old_fattr);
2650 update_changeattr(new_dir, &res->new_cinfo);
2651 nfs_post_op_update_inode(new_dir, res->new_fattr);
2652 return 1;
2655 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2656 struct inode *new_dir, struct qstr *new_name)
2658 struct nfs_server *server = NFS_SERVER(old_dir);
2659 struct nfs_renameargs arg = {
2660 .old_dir = NFS_FH(old_dir),
2661 .new_dir = NFS_FH(new_dir),
2662 .old_name = old_name,
2663 .new_name = new_name,
2664 .bitmask = server->attr_bitmask,
2666 struct nfs_renameres res = {
2667 .server = server,
2669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2671 .rpc_argp = &arg,
2672 .rpc_resp = &res,
2674 int status = -ENOMEM;
2676 res.old_fattr = nfs_alloc_fattr();
2677 res.new_fattr = nfs_alloc_fattr();
2678 if (res.old_fattr == NULL || res.new_fattr == NULL)
2679 goto out;
2681 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2682 if (!status) {
2683 update_changeattr(old_dir, &res.old_cinfo);
2684 nfs_post_op_update_inode(old_dir, res.old_fattr);
2685 update_changeattr(new_dir, &res.new_cinfo);
2686 nfs_post_op_update_inode(new_dir, res.new_fattr);
2688 out:
2689 nfs_free_fattr(res.new_fattr);
2690 nfs_free_fattr(res.old_fattr);
2691 return status;
2694 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2695 struct inode *new_dir, struct qstr *new_name)
2697 struct nfs4_exception exception = { };
2698 int err;
2699 do {
2700 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2701 _nfs4_proc_rename(old_dir, old_name,
2702 new_dir, new_name),
2703 &exception);
2704 } while (exception.retry);
2705 return err;
2708 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2710 struct nfs_server *server = NFS_SERVER(inode);
2711 struct nfs4_link_arg arg = {
2712 .fh = NFS_FH(inode),
2713 .dir_fh = NFS_FH(dir),
2714 .name = name,
2715 .bitmask = server->attr_bitmask,
2717 struct nfs4_link_res res = {
2718 .server = server,
2720 struct rpc_message msg = {
2721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2722 .rpc_argp = &arg,
2723 .rpc_resp = &res,
2725 int status = -ENOMEM;
2727 res.fattr = nfs_alloc_fattr();
2728 res.dir_attr = nfs_alloc_fattr();
2729 if (res.fattr == NULL || res.dir_attr == NULL)
2730 goto out;
2732 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2733 if (!status) {
2734 update_changeattr(dir, &res.cinfo);
2735 nfs_post_op_update_inode(dir, res.dir_attr);
2736 nfs_post_op_update_inode(inode, res.fattr);
2738 out:
2739 nfs_free_fattr(res.dir_attr);
2740 nfs_free_fattr(res.fattr);
2741 return status;
2744 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2746 struct nfs4_exception exception = { };
2747 int err;
2748 do {
2749 err = nfs4_handle_exception(NFS_SERVER(inode),
2750 _nfs4_proc_link(inode, dir, name),
2751 &exception);
2752 } while (exception.retry);
2753 return err;
2756 struct nfs4_createdata {
2757 struct rpc_message msg;
2758 struct nfs4_create_arg arg;
2759 struct nfs4_create_res res;
2760 struct nfs_fh fh;
2761 struct nfs_fattr fattr;
2762 struct nfs_fattr dir_fattr;
2765 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2766 struct qstr *name, struct iattr *sattr, u32 ftype)
2768 struct nfs4_createdata *data;
2770 data = kzalloc(sizeof(*data), GFP_KERNEL);
2771 if (data != NULL) {
2772 struct nfs_server *server = NFS_SERVER(dir);
2774 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2775 data->msg.rpc_argp = &data->arg;
2776 data->msg.rpc_resp = &data->res;
2777 data->arg.dir_fh = NFS_FH(dir);
2778 data->arg.server = server;
2779 data->arg.name = name;
2780 data->arg.attrs = sattr;
2781 data->arg.ftype = ftype;
2782 data->arg.bitmask = server->attr_bitmask;
2783 data->res.server = server;
2784 data->res.fh = &data->fh;
2785 data->res.fattr = &data->fattr;
2786 data->res.dir_fattr = &data->dir_fattr;
2787 nfs_fattr_init(data->res.fattr);
2788 nfs_fattr_init(data->res.dir_fattr);
2790 return data;
2793 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2795 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2796 &data->arg, &data->res, 1);
2797 if (status == 0) {
2798 update_changeattr(dir, &data->res.dir_cinfo);
2799 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2800 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2802 return status;
2805 static void nfs4_free_createdata(struct nfs4_createdata *data)
2807 kfree(data);
2810 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2811 struct page *page, unsigned int len, struct iattr *sattr)
2813 struct nfs4_createdata *data;
2814 int status = -ENAMETOOLONG;
2816 if (len > NFS4_MAXPATHLEN)
2817 goto out;
2819 status = -ENOMEM;
2820 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2821 if (data == NULL)
2822 goto out;
2824 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2825 data->arg.u.symlink.pages = &page;
2826 data->arg.u.symlink.len = len;
2828 status = nfs4_do_create(dir, dentry, data);
2830 nfs4_free_createdata(data);
2831 out:
2832 return status;
2835 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2836 struct page *page, unsigned int len, struct iattr *sattr)
2838 struct nfs4_exception exception = { };
2839 int err;
2840 do {
2841 err = nfs4_handle_exception(NFS_SERVER(dir),
2842 _nfs4_proc_symlink(dir, dentry, page,
2843 len, sattr),
2844 &exception);
2845 } while (exception.retry);
2846 return err;
2849 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2850 struct iattr *sattr)
2852 struct nfs4_createdata *data;
2853 int status = -ENOMEM;
2855 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2856 if (data == NULL)
2857 goto out;
2859 status = nfs4_do_create(dir, dentry, data);
2861 nfs4_free_createdata(data);
2862 out:
2863 return status;
2866 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2867 struct iattr *sattr)
2869 struct nfs4_exception exception = { };
2870 int err;
2872 sattr->ia_mode &= ~current_umask();
2873 do {
2874 err = nfs4_handle_exception(NFS_SERVER(dir),
2875 _nfs4_proc_mkdir(dir, dentry, sattr),
2876 &exception);
2877 } while (exception.retry);
2878 return err;
2881 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2882 u64 cookie, struct page **pages, unsigned int count, int plus)
2884 struct inode *dir = dentry->d_inode;
2885 struct nfs4_readdir_arg args = {
2886 .fh = NFS_FH(dir),
2887 .pages = pages,
2888 .pgbase = 0,
2889 .count = count,
2890 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2891 .plus = plus,
2893 struct nfs4_readdir_res res;
2894 struct rpc_message msg = {
2895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2896 .rpc_argp = &args,
2897 .rpc_resp = &res,
2898 .rpc_cred = cred,
2900 int status;
2902 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2903 dentry->d_parent->d_name.name,
2904 dentry->d_name.name,
2905 (unsigned long long)cookie);
2906 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2907 res.pgbase = args.pgbase;
2908 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2909 if (status >= 0) {
2910 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2911 status += args.pgbase;
2914 nfs_invalidate_atime(dir);
2916 dprintk("%s: returns %d\n", __func__, status);
2917 return status;
2920 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2921 u64 cookie, struct page **pages, unsigned int count, int plus)
2923 struct nfs4_exception exception = { };
2924 int err;
2925 do {
2926 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2927 _nfs4_proc_readdir(dentry, cred, cookie,
2928 pages, count, plus),
2929 &exception);
2930 } while (exception.retry);
2931 return err;
2934 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2935 struct iattr *sattr, dev_t rdev)
2937 struct nfs4_createdata *data;
2938 int mode = sattr->ia_mode;
2939 int status = -ENOMEM;
2941 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2942 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2944 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2945 if (data == NULL)
2946 goto out;
2948 if (S_ISFIFO(mode))
2949 data->arg.ftype = NF4FIFO;
2950 else if (S_ISBLK(mode)) {
2951 data->arg.ftype = NF4BLK;
2952 data->arg.u.device.specdata1 = MAJOR(rdev);
2953 data->arg.u.device.specdata2 = MINOR(rdev);
2955 else if (S_ISCHR(mode)) {
2956 data->arg.ftype = NF4CHR;
2957 data->arg.u.device.specdata1 = MAJOR(rdev);
2958 data->arg.u.device.specdata2 = MINOR(rdev);
2961 status = nfs4_do_create(dir, dentry, data);
2963 nfs4_free_createdata(data);
2964 out:
2965 return status;
2968 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2969 struct iattr *sattr, dev_t rdev)
2971 struct nfs4_exception exception = { };
2972 int err;
2974 sattr->ia_mode &= ~current_umask();
2975 do {
2976 err = nfs4_handle_exception(NFS_SERVER(dir),
2977 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2978 &exception);
2979 } while (exception.retry);
2980 return err;
2983 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2984 struct nfs_fsstat *fsstat)
2986 struct nfs4_statfs_arg args = {
2987 .fh = fhandle,
2988 .bitmask = server->attr_bitmask,
2990 struct nfs4_statfs_res res = {
2991 .fsstat = fsstat,
2993 struct rpc_message msg = {
2994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2995 .rpc_argp = &args,
2996 .rpc_resp = &res,
2999 nfs_fattr_init(fsstat->fattr);
3000 return nfs4_call_sync(server, &msg, &args, &res, 0);
3003 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3005 struct nfs4_exception exception = { };
3006 int err;
3007 do {
3008 err = nfs4_handle_exception(server,
3009 _nfs4_proc_statfs(server, fhandle, fsstat),
3010 &exception);
3011 } while (exception.retry);
3012 return err;
3015 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3016 struct nfs_fsinfo *fsinfo)
3018 struct nfs4_fsinfo_arg args = {
3019 .fh = fhandle,
3020 .bitmask = server->attr_bitmask,
3022 struct nfs4_fsinfo_res res = {
3023 .fsinfo = fsinfo,
3025 struct rpc_message msg = {
3026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3027 .rpc_argp = &args,
3028 .rpc_resp = &res,
3031 return nfs4_call_sync(server, &msg, &args, &res, 0);
3034 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3036 struct nfs4_exception exception = { };
3037 int err;
3039 do {
3040 err = nfs4_handle_exception(server,
3041 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3042 &exception);
3043 } while (exception.retry);
3044 return err;
3047 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3049 nfs_fattr_init(fsinfo->fattr);
3050 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3053 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3054 struct nfs_pathconf *pathconf)
3056 struct nfs4_pathconf_arg args = {
3057 .fh = fhandle,
3058 .bitmask = server->attr_bitmask,
3060 struct nfs4_pathconf_res res = {
3061 .pathconf = pathconf,
3063 struct rpc_message msg = {
3064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3065 .rpc_argp = &args,
3066 .rpc_resp = &res,
3069 /* None of the pathconf attributes are mandatory to implement */
3070 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3071 memset(pathconf, 0, sizeof(*pathconf));
3072 return 0;
3075 nfs_fattr_init(pathconf->fattr);
3076 return nfs4_call_sync(server, &msg, &args, &res, 0);
3079 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3080 struct nfs_pathconf *pathconf)
3082 struct nfs4_exception exception = { };
3083 int err;
3085 do {
3086 err = nfs4_handle_exception(server,
3087 _nfs4_proc_pathconf(server, fhandle, pathconf),
3088 &exception);
3089 } while (exception.retry);
3090 return err;
3093 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3095 struct nfs_server *server = NFS_SERVER(data->inode);
3097 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3098 nfs_restart_rpc(task, server->nfs_client);
3099 return -EAGAIN;
3102 nfs_invalidate_atime(data->inode);
3103 if (task->tk_status > 0)
3104 renew_lease(server, data->timestamp);
3105 return 0;
3108 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3111 dprintk("--> %s\n", __func__);
3113 if (!nfs4_sequence_done(task, &data->res.seq_res))
3114 return -EAGAIN;
3116 return data->read_done_cb(task, data);
3119 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3121 data->timestamp = jiffies;
3122 data->read_done_cb = nfs4_read_done_cb;
3123 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3126 /* Reset the the nfs_read_data to send the read to the MDS. */
3127 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3129 dprintk("%s Reset task for i/o through\n", __func__);
3130 put_lseg(data->lseg);
3131 data->lseg = NULL;
3132 /* offsets will differ in the dense stripe case */
3133 data->args.offset = data->mds_offset;
3134 data->ds_clp = NULL;
3135 data->args.fh = NFS_FH(data->inode);
3136 data->read_done_cb = nfs4_read_done_cb;
3137 task->tk_ops = data->mds_ops;
3138 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3140 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3142 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3144 struct inode *inode = data->inode;
3146 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3147 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3148 return -EAGAIN;
3150 if (task->tk_status >= 0) {
3151 renew_lease(NFS_SERVER(inode), data->timestamp);
3152 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3154 return 0;
3157 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3159 if (!nfs4_sequence_done(task, &data->res.seq_res))
3160 return -EAGAIN;
3161 return data->write_done_cb(task, data);
3164 /* Reset the the nfs_write_data to send the write to the MDS. */
3165 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3167 dprintk("%s Reset task for i/o through\n", __func__);
3168 put_lseg(data->lseg);
3169 data->lseg = NULL;
3170 data->ds_clp = NULL;
3171 data->write_done_cb = nfs4_write_done_cb;
3172 data->args.fh = NFS_FH(data->inode);
3173 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3174 data->args.offset = data->mds_offset;
3175 data->res.fattr = &data->fattr;
3176 task->tk_ops = data->mds_ops;
3177 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3179 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3181 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3183 struct nfs_server *server = NFS_SERVER(data->inode);
3185 if (data->lseg) {
3186 data->args.bitmask = NULL;
3187 data->res.fattr = NULL;
3188 } else
3189 data->args.bitmask = server->cache_consistency_bitmask;
3190 if (!data->write_done_cb)
3191 data->write_done_cb = nfs4_write_done_cb;
3192 data->res.server = server;
3193 data->timestamp = jiffies;
3195 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3198 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3200 struct inode *inode = data->inode;
3202 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3203 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3204 return -EAGAIN;
3206 nfs_refresh_inode(inode, data->res.fattr);
3207 return 0;
3210 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3212 if (!nfs4_sequence_done(task, &data->res.seq_res))
3213 return -EAGAIN;
3214 return data->write_done_cb(task, data);
3217 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3219 struct nfs_server *server = NFS_SERVER(data->inode);
3221 if (data->lseg) {
3222 data->args.bitmask = NULL;
3223 data->res.fattr = NULL;
3224 } else
3225 data->args.bitmask = server->cache_consistency_bitmask;
3226 if (!data->write_done_cb)
3227 data->write_done_cb = nfs4_commit_done_cb;
3228 data->res.server = server;
3229 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3232 struct nfs4_renewdata {
3233 struct nfs_client *client;
3234 unsigned long timestamp;
3238 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3239 * standalone procedure for queueing an asynchronous RENEW.
3241 static void nfs4_renew_release(void *calldata)
3243 struct nfs4_renewdata *data = calldata;
3244 struct nfs_client *clp = data->client;
3246 if (atomic_read(&clp->cl_count) > 1)
3247 nfs4_schedule_state_renewal(clp);
3248 nfs_put_client(clp);
3249 kfree(data);
3252 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3254 struct nfs4_renewdata *data = calldata;
3255 struct nfs_client *clp = data->client;
3256 unsigned long timestamp = data->timestamp;
3258 if (task->tk_status < 0) {
3259 /* Unless we're shutting down, schedule state recovery! */
3260 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3261 nfs4_schedule_lease_recovery(clp);
3262 return;
3264 do_renew_lease(clp, timestamp);
3267 static const struct rpc_call_ops nfs4_renew_ops = {
3268 .rpc_call_done = nfs4_renew_done,
3269 .rpc_release = nfs4_renew_release,
3272 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3274 struct rpc_message msg = {
3275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3276 .rpc_argp = clp,
3277 .rpc_cred = cred,
3279 struct nfs4_renewdata *data;
3281 if (!atomic_inc_not_zero(&clp->cl_count))
3282 return -EIO;
3283 data = kmalloc(sizeof(*data), GFP_KERNEL);
3284 if (data == NULL)
3285 return -ENOMEM;
3286 data->client = clp;
3287 data->timestamp = jiffies;
3288 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3289 &nfs4_renew_ops, data);
3292 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3294 struct rpc_message msg = {
3295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3296 .rpc_argp = clp,
3297 .rpc_cred = cred,
3299 unsigned long now = jiffies;
3300 int status;
3302 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3303 if (status < 0)
3304 return status;
3305 do_renew_lease(clp, now);
3306 return 0;
3309 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3311 return (server->caps & NFS_CAP_ACLS)
3312 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3313 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3316 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3317 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3318 * the stack.
3320 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3322 static void buf_to_pages(const void *buf, size_t buflen,
3323 struct page **pages, unsigned int *pgbase)
3325 const void *p = buf;
3327 *pgbase = offset_in_page(buf);
3328 p -= *pgbase;
3329 while (p < buf + buflen) {
3330 *(pages++) = virt_to_page(p);
3331 p += PAGE_CACHE_SIZE;
3335 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3336 struct page **pages, unsigned int *pgbase)
3338 struct page *newpage, **spages;
3339 int rc = 0;
3340 size_t len;
3341 spages = pages;
3343 do {
3344 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3345 newpage = alloc_page(GFP_KERNEL);
3347 if (newpage == NULL)
3348 goto unwind;
3349 memcpy(page_address(newpage), buf, len);
3350 buf += len;
3351 buflen -= len;
3352 *pages++ = newpage;
3353 rc++;
3354 } while (buflen != 0);
3356 return rc;
3358 unwind:
3359 for(; rc > 0; rc--)
3360 __free_page(spages[rc-1]);
3361 return -ENOMEM;
3364 struct nfs4_cached_acl {
3365 int cached;
3366 size_t len;
3367 char data[0];
3370 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3372 struct nfs_inode *nfsi = NFS_I(inode);
3374 spin_lock(&inode->i_lock);
3375 kfree(nfsi->nfs4_acl);
3376 nfsi->nfs4_acl = acl;
3377 spin_unlock(&inode->i_lock);
3380 static void nfs4_zap_acl_attr(struct inode *inode)
3382 nfs4_set_cached_acl(inode, NULL);
3385 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3387 struct nfs_inode *nfsi = NFS_I(inode);
3388 struct nfs4_cached_acl *acl;
3389 int ret = -ENOENT;
3391 spin_lock(&inode->i_lock);
3392 acl = nfsi->nfs4_acl;
3393 if (acl == NULL)
3394 goto out;
3395 if (buf == NULL) /* user is just asking for length */
3396 goto out_len;
3397 if (acl->cached == 0)
3398 goto out;
3399 ret = -ERANGE; /* see getxattr(2) man page */
3400 if (acl->len > buflen)
3401 goto out;
3402 memcpy(buf, acl->data, acl->len);
3403 out_len:
3404 ret = acl->len;
3405 out:
3406 spin_unlock(&inode->i_lock);
3407 return ret;
3410 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3412 struct nfs4_cached_acl *acl;
3414 if (buf && acl_len <= PAGE_SIZE) {
3415 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3416 if (acl == NULL)
3417 goto out;
3418 acl->cached = 1;
3419 memcpy(acl->data, buf, acl_len);
3420 } else {
3421 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3422 if (acl == NULL)
3423 goto out;
3424 acl->cached = 0;
3426 acl->len = acl_len;
3427 out:
3428 nfs4_set_cached_acl(inode, acl);
3431 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3433 struct page *pages[NFS4ACL_MAXPAGES];
3434 struct nfs_getaclargs args = {
3435 .fh = NFS_FH(inode),
3436 .acl_pages = pages,
3437 .acl_len = buflen,
3439 struct nfs_getaclres res = {
3440 .acl_len = buflen,
3442 void *resp_buf;
3443 struct rpc_message msg = {
3444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3445 .rpc_argp = &args,
3446 .rpc_resp = &res,
3448 struct page *localpage = NULL;
3449 int ret;
3451 if (buflen < PAGE_SIZE) {
3452 /* As long as we're doing a round trip to the server anyway,
3453 * let's be prepared for a page of acl data. */
3454 localpage = alloc_page(GFP_KERNEL);
3455 resp_buf = page_address(localpage);
3456 if (localpage == NULL)
3457 return -ENOMEM;
3458 args.acl_pages[0] = localpage;
3459 args.acl_pgbase = 0;
3460 args.acl_len = PAGE_SIZE;
3461 } else {
3462 resp_buf = buf;
3463 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3465 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3466 if (ret)
3467 goto out_free;
3468 if (res.acl_len > args.acl_len)
3469 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3470 else
3471 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3472 if (buf) {
3473 ret = -ERANGE;
3474 if (res.acl_len > buflen)
3475 goto out_free;
3476 if (localpage)
3477 memcpy(buf, resp_buf, res.acl_len);
3479 ret = res.acl_len;
3480 out_free:
3481 if (localpage)
3482 __free_page(localpage);
3483 return ret;
3486 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3488 struct nfs4_exception exception = { };
3489 ssize_t ret;
3490 do {
3491 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3492 if (ret >= 0)
3493 break;
3494 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3495 } while (exception.retry);
3496 return ret;
3499 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3501 struct nfs_server *server = NFS_SERVER(inode);
3502 int ret;
3504 if (!nfs4_server_supports_acls(server))
3505 return -EOPNOTSUPP;
3506 ret = nfs_revalidate_inode(server, inode);
3507 if (ret < 0)
3508 return ret;
3509 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3510 nfs_zap_acl_cache(inode);
3511 ret = nfs4_read_cached_acl(inode, buf, buflen);
3512 if (ret != -ENOENT)
3513 return ret;
3514 return nfs4_get_acl_uncached(inode, buf, buflen);
3517 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3519 struct nfs_server *server = NFS_SERVER(inode);
3520 struct page *pages[NFS4ACL_MAXPAGES];
3521 struct nfs_setaclargs arg = {
3522 .fh = NFS_FH(inode),
3523 .acl_pages = pages,
3524 .acl_len = buflen,
3526 struct nfs_setaclres res;
3527 struct rpc_message msg = {
3528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3529 .rpc_argp = &arg,
3530 .rpc_resp = &res,
3532 int ret, i;
3534 if (!nfs4_server_supports_acls(server))
3535 return -EOPNOTSUPP;
3536 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3537 if (i < 0)
3538 return i;
3539 nfs_inode_return_delegation(inode);
3540 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3543 * Free each page after tx, so the only ref left is
3544 * held by the network stack
3546 for (; i > 0; i--)
3547 put_page(pages[i-1]);
3550 * Acl update can result in inode attribute update.
3551 * so mark the attribute cache invalid.
3553 spin_lock(&inode->i_lock);
3554 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3555 spin_unlock(&inode->i_lock);
3556 nfs_access_zap_cache(inode);
3557 nfs_zap_acl_cache(inode);
3558 return ret;
3561 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3563 struct nfs4_exception exception = { };
3564 int err;
3565 do {
3566 err = nfs4_handle_exception(NFS_SERVER(inode),
3567 __nfs4_proc_set_acl(inode, buf, buflen),
3568 &exception);
3569 } while (exception.retry);
3570 return err;
3573 static int
3574 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3576 struct nfs_client *clp = server->nfs_client;
3578 if (task->tk_status >= 0)
3579 return 0;
3580 switch(task->tk_status) {
3581 case -NFS4ERR_ADMIN_REVOKED:
3582 case -NFS4ERR_BAD_STATEID:
3583 case -NFS4ERR_OPENMODE:
3584 if (state == NULL)
3585 break;
3586 nfs4_schedule_stateid_recovery(server, state);
3587 goto wait_on_recovery;
3588 case -NFS4ERR_STALE_STATEID:
3589 case -NFS4ERR_STALE_CLIENTID:
3590 case -NFS4ERR_EXPIRED:
3591 nfs4_schedule_lease_recovery(clp);
3592 goto wait_on_recovery;
3593 #if defined(CONFIG_NFS_V4_1)
3594 case -NFS4ERR_BADSESSION:
3595 case -NFS4ERR_BADSLOT:
3596 case -NFS4ERR_BAD_HIGH_SLOT:
3597 case -NFS4ERR_DEADSESSION:
3598 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3599 case -NFS4ERR_SEQ_FALSE_RETRY:
3600 case -NFS4ERR_SEQ_MISORDERED:
3601 dprintk("%s ERROR %d, Reset session\n", __func__,
3602 task->tk_status);
3603 nfs4_schedule_session_recovery(clp->cl_session);
3604 task->tk_status = 0;
3605 return -EAGAIN;
3606 #endif /* CONFIG_NFS_V4_1 */
3607 case -NFS4ERR_DELAY:
3608 nfs_inc_server_stats(server, NFSIOS_DELAY);
3609 case -NFS4ERR_GRACE:
3610 case -EKEYEXPIRED:
3611 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3612 task->tk_status = 0;
3613 return -EAGAIN;
3614 case -NFS4ERR_OLD_STATEID:
3615 task->tk_status = 0;
3616 return -EAGAIN;
3618 task->tk_status = nfs4_map_errors(task->tk_status);
3619 return 0;
3620 wait_on_recovery:
3621 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3622 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3623 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3624 task->tk_status = 0;
3625 return -EAGAIN;
3628 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3629 unsigned short port, struct rpc_cred *cred,
3630 struct nfs4_setclientid_res *res)
3632 nfs4_verifier sc_verifier;
3633 struct nfs4_setclientid setclientid = {
3634 .sc_verifier = &sc_verifier,
3635 .sc_prog = program,
3636 .sc_cb_ident = clp->cl_cb_ident,
3638 struct rpc_message msg = {
3639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3640 .rpc_argp = &setclientid,
3641 .rpc_resp = res,
3642 .rpc_cred = cred,
3644 __be32 *p;
3645 int loop = 0;
3646 int status;
3648 p = (__be32*)sc_verifier.data;
3649 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3650 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3652 for(;;) {
3653 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3654 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3655 clp->cl_ipaddr,
3656 rpc_peeraddr2str(clp->cl_rpcclient,
3657 RPC_DISPLAY_ADDR),
3658 rpc_peeraddr2str(clp->cl_rpcclient,
3659 RPC_DISPLAY_PROTO),
3660 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3661 clp->cl_id_uniquifier);
3662 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3663 sizeof(setclientid.sc_netid),
3664 rpc_peeraddr2str(clp->cl_rpcclient,
3665 RPC_DISPLAY_NETID));
3666 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3667 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3668 clp->cl_ipaddr, port >> 8, port & 255);
3670 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3671 if (status != -NFS4ERR_CLID_INUSE)
3672 break;
3673 if (signalled())
3674 break;
3675 if (loop++ & 1)
3676 ssleep(clp->cl_lease_time / HZ + 1);
3677 else
3678 if (++clp->cl_id_uniquifier == 0)
3679 break;
3681 return status;
3684 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3685 struct nfs4_setclientid_res *arg,
3686 struct rpc_cred *cred)
3688 struct nfs_fsinfo fsinfo;
3689 struct rpc_message msg = {
3690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3691 .rpc_argp = arg,
3692 .rpc_resp = &fsinfo,
3693 .rpc_cred = cred,
3695 unsigned long now;
3696 int status;
3698 now = jiffies;
3699 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3700 if (status == 0) {
3701 spin_lock(&clp->cl_lock);
3702 clp->cl_lease_time = fsinfo.lease_time * HZ;
3703 clp->cl_last_renewal = now;
3704 spin_unlock(&clp->cl_lock);
3706 return status;
3709 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3710 struct nfs4_setclientid_res *arg,
3711 struct rpc_cred *cred)
3713 long timeout = 0;
3714 int err;
3715 do {
3716 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3717 switch (err) {
3718 case 0:
3719 return err;
3720 case -NFS4ERR_RESOURCE:
3721 /* The IBM lawyers misread another document! */
3722 case -NFS4ERR_DELAY:
3723 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3725 } while (err == 0);
3726 return err;
3729 struct nfs4_delegreturndata {
3730 struct nfs4_delegreturnargs args;
3731 struct nfs4_delegreturnres res;
3732 struct nfs_fh fh;
3733 nfs4_stateid stateid;
3734 unsigned long timestamp;
3735 struct nfs_fattr fattr;
3736 int rpc_status;
3739 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3741 struct nfs4_delegreturndata *data = calldata;
3743 if (!nfs4_sequence_done(task, &data->res.seq_res))
3744 return;
3746 switch (task->tk_status) {
3747 case -NFS4ERR_STALE_STATEID:
3748 case -NFS4ERR_EXPIRED:
3749 case 0:
3750 renew_lease(data->res.server, data->timestamp);
3751 break;
3752 default:
3753 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3754 -EAGAIN) {
3755 nfs_restart_rpc(task, data->res.server->nfs_client);
3756 return;
3759 data->rpc_status = task->tk_status;
3762 static void nfs4_delegreturn_release(void *calldata)
3764 kfree(calldata);
3767 #if defined(CONFIG_NFS_V4_1)
3768 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3770 struct nfs4_delegreturndata *d_data;
3772 d_data = (struct nfs4_delegreturndata *)data;
3774 if (nfs4_setup_sequence(d_data->res.server,
3775 &d_data->args.seq_args,
3776 &d_data->res.seq_res, 1, task))
3777 return;
3778 rpc_call_start(task);
3780 #endif /* CONFIG_NFS_V4_1 */
3782 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3783 #if defined(CONFIG_NFS_V4_1)
3784 .rpc_call_prepare = nfs4_delegreturn_prepare,
3785 #endif /* CONFIG_NFS_V4_1 */
3786 .rpc_call_done = nfs4_delegreturn_done,
3787 .rpc_release = nfs4_delegreturn_release,
3790 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3792 struct nfs4_delegreturndata *data;
3793 struct nfs_server *server = NFS_SERVER(inode);
3794 struct rpc_task *task;
3795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3797 .rpc_cred = cred,
3799 struct rpc_task_setup task_setup_data = {
3800 .rpc_client = server->client,
3801 .rpc_message = &msg,
3802 .callback_ops = &nfs4_delegreturn_ops,
3803 .flags = RPC_TASK_ASYNC,
3805 int status = 0;
3807 data = kzalloc(sizeof(*data), GFP_NOFS);
3808 if (data == NULL)
3809 return -ENOMEM;
3810 data->args.fhandle = &data->fh;
3811 data->args.stateid = &data->stateid;
3812 data->args.bitmask = server->attr_bitmask;
3813 nfs_copy_fh(&data->fh, NFS_FH(inode));
3814 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3815 data->res.fattr = &data->fattr;
3816 data->res.server = server;
3817 nfs_fattr_init(data->res.fattr);
3818 data->timestamp = jiffies;
3819 data->rpc_status = 0;
3821 task_setup_data.callback_data = data;
3822 msg.rpc_argp = &data->args;
3823 msg.rpc_resp = &data->res;
3824 task = rpc_run_task(&task_setup_data);
3825 if (IS_ERR(task))
3826 return PTR_ERR(task);
3827 if (!issync)
3828 goto out;
3829 status = nfs4_wait_for_completion_rpc_task(task);
3830 if (status != 0)
3831 goto out;
3832 status = data->rpc_status;
3833 if (status != 0)
3834 goto out;
3835 nfs_refresh_inode(inode, &data->fattr);
3836 out:
3837 rpc_put_task(task);
3838 return status;
3841 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3843 struct nfs_server *server = NFS_SERVER(inode);
3844 struct nfs4_exception exception = { };
3845 int err;
3846 do {
3847 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3848 switch (err) {
3849 case -NFS4ERR_STALE_STATEID:
3850 case -NFS4ERR_EXPIRED:
3851 case 0:
3852 return 0;
3854 err = nfs4_handle_exception(server, err, &exception);
3855 } while (exception.retry);
3856 return err;
3859 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3860 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3863 * sleep, with exponential backoff, and retry the LOCK operation.
3865 static unsigned long
3866 nfs4_set_lock_task_retry(unsigned long timeout)
3868 schedule_timeout_killable(timeout);
3869 timeout <<= 1;
3870 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3871 return NFS4_LOCK_MAXTIMEOUT;
3872 return timeout;
3875 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3877 struct inode *inode = state->inode;
3878 struct nfs_server *server = NFS_SERVER(inode);
3879 struct nfs_client *clp = server->nfs_client;
3880 struct nfs_lockt_args arg = {
3881 .fh = NFS_FH(inode),
3882 .fl = request,
3884 struct nfs_lockt_res res = {
3885 .denied = request,
3887 struct rpc_message msg = {
3888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3889 .rpc_argp = &arg,
3890 .rpc_resp = &res,
3891 .rpc_cred = state->owner->so_cred,
3893 struct nfs4_lock_state *lsp;
3894 int status;
3896 arg.lock_owner.clientid = clp->cl_clientid;
3897 status = nfs4_set_lock_state(state, request);
3898 if (status != 0)
3899 goto out;
3900 lsp = request->fl_u.nfs4_fl.owner;
3901 arg.lock_owner.id = lsp->ls_id.id;
3902 arg.lock_owner.s_dev = server->s_dev;
3903 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3904 switch (status) {
3905 case 0:
3906 request->fl_type = F_UNLCK;
3907 break;
3908 case -NFS4ERR_DENIED:
3909 status = 0;
3911 request->fl_ops->fl_release_private(request);
3912 out:
3913 return status;
3916 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3918 struct nfs4_exception exception = { };
3919 int err;
3921 do {
3922 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3923 _nfs4_proc_getlk(state, cmd, request),
3924 &exception);
3925 } while (exception.retry);
3926 return err;
3929 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3931 int res = 0;
3932 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3933 case FL_POSIX:
3934 res = posix_lock_file_wait(file, fl);
3935 break;
3936 case FL_FLOCK:
3937 res = flock_lock_file_wait(file, fl);
3938 break;
3939 default:
3940 BUG();
3942 return res;
3945 struct nfs4_unlockdata {
3946 struct nfs_locku_args arg;
3947 struct nfs_locku_res res;
3948 struct nfs4_lock_state *lsp;
3949 struct nfs_open_context *ctx;
3950 struct file_lock fl;
3951 const struct nfs_server *server;
3952 unsigned long timestamp;
3955 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3956 struct nfs_open_context *ctx,
3957 struct nfs4_lock_state *lsp,
3958 struct nfs_seqid *seqid)
3960 struct nfs4_unlockdata *p;
3961 struct inode *inode = lsp->ls_state->inode;
3963 p = kzalloc(sizeof(*p), GFP_NOFS);
3964 if (p == NULL)
3965 return NULL;
3966 p->arg.fh = NFS_FH(inode);
3967 p->arg.fl = &p->fl;
3968 p->arg.seqid = seqid;
3969 p->res.seqid = seqid;
3970 p->arg.stateid = &lsp->ls_stateid;
3971 p->lsp = lsp;
3972 atomic_inc(&lsp->ls_count);
3973 /* Ensure we don't close file until we're done freeing locks! */
3974 p->ctx = get_nfs_open_context(ctx);
3975 memcpy(&p->fl, fl, sizeof(p->fl));
3976 p->server = NFS_SERVER(inode);
3977 return p;
3980 static void nfs4_locku_release_calldata(void *data)
3982 struct nfs4_unlockdata *calldata = data;
3983 nfs_free_seqid(calldata->arg.seqid);
3984 nfs4_put_lock_state(calldata->lsp);
3985 put_nfs_open_context(calldata->ctx);
3986 kfree(calldata);
3989 static void nfs4_locku_done(struct rpc_task *task, void *data)
3991 struct nfs4_unlockdata *calldata = data;
3993 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3994 return;
3995 switch (task->tk_status) {
3996 case 0:
3997 memcpy(calldata->lsp->ls_stateid.data,
3998 calldata->res.stateid.data,
3999 sizeof(calldata->lsp->ls_stateid.data));
4000 renew_lease(calldata->server, calldata->timestamp);
4001 break;
4002 case -NFS4ERR_BAD_STATEID:
4003 case -NFS4ERR_OLD_STATEID:
4004 case -NFS4ERR_STALE_STATEID:
4005 case -NFS4ERR_EXPIRED:
4006 break;
4007 default:
4008 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4009 nfs_restart_rpc(task,
4010 calldata->server->nfs_client);
4014 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4016 struct nfs4_unlockdata *calldata = data;
4018 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4019 return;
4020 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4021 /* Note: exit _without_ running nfs4_locku_done */
4022 task->tk_action = NULL;
4023 return;
4025 calldata->timestamp = jiffies;
4026 if (nfs4_setup_sequence(calldata->server,
4027 &calldata->arg.seq_args,
4028 &calldata->res.seq_res, 1, task))
4029 return;
4030 rpc_call_start(task);
4033 static const struct rpc_call_ops nfs4_locku_ops = {
4034 .rpc_call_prepare = nfs4_locku_prepare,
4035 .rpc_call_done = nfs4_locku_done,
4036 .rpc_release = nfs4_locku_release_calldata,
4039 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4040 struct nfs_open_context *ctx,
4041 struct nfs4_lock_state *lsp,
4042 struct nfs_seqid *seqid)
4044 struct nfs4_unlockdata *data;
4045 struct rpc_message msg = {
4046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4047 .rpc_cred = ctx->cred,
4049 struct rpc_task_setup task_setup_data = {
4050 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4051 .rpc_message = &msg,
4052 .callback_ops = &nfs4_locku_ops,
4053 .workqueue = nfsiod_workqueue,
4054 .flags = RPC_TASK_ASYNC,
4057 /* Ensure this is an unlock - when canceling a lock, the
4058 * canceled lock is passed in, and it won't be an unlock.
4060 fl->fl_type = F_UNLCK;
4062 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4063 if (data == NULL) {
4064 nfs_free_seqid(seqid);
4065 return ERR_PTR(-ENOMEM);
4068 msg.rpc_argp = &data->arg;
4069 msg.rpc_resp = &data->res;
4070 task_setup_data.callback_data = data;
4071 return rpc_run_task(&task_setup_data);
4074 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4076 struct nfs_inode *nfsi = NFS_I(state->inode);
4077 struct nfs_seqid *seqid;
4078 struct nfs4_lock_state *lsp;
4079 struct rpc_task *task;
4080 int status = 0;
4081 unsigned char fl_flags = request->fl_flags;
4083 status = nfs4_set_lock_state(state, request);
4084 /* Unlock _before_ we do the RPC call */
4085 request->fl_flags |= FL_EXISTS;
4086 down_read(&nfsi->rwsem);
4087 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4088 up_read(&nfsi->rwsem);
4089 goto out;
4091 up_read(&nfsi->rwsem);
4092 if (status != 0)
4093 goto out;
4094 /* Is this a delegated lock? */
4095 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4096 goto out;
4097 lsp = request->fl_u.nfs4_fl.owner;
4098 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4099 status = -ENOMEM;
4100 if (seqid == NULL)
4101 goto out;
4102 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4103 status = PTR_ERR(task);
4104 if (IS_ERR(task))
4105 goto out;
4106 status = nfs4_wait_for_completion_rpc_task(task);
4107 rpc_put_task(task);
4108 out:
4109 request->fl_flags = fl_flags;
4110 return status;
4113 struct nfs4_lockdata {
4114 struct nfs_lock_args arg;
4115 struct nfs_lock_res res;
4116 struct nfs4_lock_state *lsp;
4117 struct nfs_open_context *ctx;
4118 struct file_lock fl;
4119 unsigned long timestamp;
4120 int rpc_status;
4121 int cancelled;
4122 struct nfs_server *server;
4125 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4126 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4127 gfp_t gfp_mask)
4129 struct nfs4_lockdata *p;
4130 struct inode *inode = lsp->ls_state->inode;
4131 struct nfs_server *server = NFS_SERVER(inode);
4133 p = kzalloc(sizeof(*p), gfp_mask);
4134 if (p == NULL)
4135 return NULL;
4137 p->arg.fh = NFS_FH(inode);
4138 p->arg.fl = &p->fl;
4139 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4140 if (p->arg.open_seqid == NULL)
4141 goto out_free;
4142 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4143 if (p->arg.lock_seqid == NULL)
4144 goto out_free_seqid;
4145 p->arg.lock_stateid = &lsp->ls_stateid;
4146 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4147 p->arg.lock_owner.id = lsp->ls_id.id;
4148 p->arg.lock_owner.s_dev = server->s_dev;
4149 p->res.lock_seqid = p->arg.lock_seqid;
4150 p->lsp = lsp;
4151 p->server = server;
4152 atomic_inc(&lsp->ls_count);
4153 p->ctx = get_nfs_open_context(ctx);
4154 memcpy(&p->fl, fl, sizeof(p->fl));
4155 return p;
4156 out_free_seqid:
4157 nfs_free_seqid(p->arg.open_seqid);
4158 out_free:
4159 kfree(p);
4160 return NULL;
4163 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4165 struct nfs4_lockdata *data = calldata;
4166 struct nfs4_state *state = data->lsp->ls_state;
4168 dprintk("%s: begin!\n", __func__);
4169 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4170 return;
4171 /* Do we need to do an open_to_lock_owner? */
4172 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4173 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4174 return;
4175 data->arg.open_stateid = &state->stateid;
4176 data->arg.new_lock_owner = 1;
4177 data->res.open_seqid = data->arg.open_seqid;
4178 } else
4179 data->arg.new_lock_owner = 0;
4180 data->timestamp = jiffies;
4181 if (nfs4_setup_sequence(data->server,
4182 &data->arg.seq_args,
4183 &data->res.seq_res, 1, task))
4184 return;
4185 rpc_call_start(task);
4186 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4189 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4191 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4192 nfs4_lock_prepare(task, calldata);
4195 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4197 struct nfs4_lockdata *data = calldata;
4199 dprintk("%s: begin!\n", __func__);
4201 if (!nfs4_sequence_done(task, &data->res.seq_res))
4202 return;
4204 data->rpc_status = task->tk_status;
4205 if (data->arg.new_lock_owner != 0) {
4206 if (data->rpc_status == 0)
4207 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4208 else
4209 goto out;
4211 if (data->rpc_status == 0) {
4212 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4213 sizeof(data->lsp->ls_stateid.data));
4214 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4215 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4217 out:
4218 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4221 static void nfs4_lock_release(void *calldata)
4223 struct nfs4_lockdata *data = calldata;
4225 dprintk("%s: begin!\n", __func__);
4226 nfs_free_seqid(data->arg.open_seqid);
4227 if (data->cancelled != 0) {
4228 struct rpc_task *task;
4229 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4230 data->arg.lock_seqid);
4231 if (!IS_ERR(task))
4232 rpc_put_task_async(task);
4233 dprintk("%s: cancelling lock!\n", __func__);
4234 } else
4235 nfs_free_seqid(data->arg.lock_seqid);
4236 nfs4_put_lock_state(data->lsp);
4237 put_nfs_open_context(data->ctx);
4238 kfree(data);
4239 dprintk("%s: done!\n", __func__);
4242 static const struct rpc_call_ops nfs4_lock_ops = {
4243 .rpc_call_prepare = nfs4_lock_prepare,
4244 .rpc_call_done = nfs4_lock_done,
4245 .rpc_release = nfs4_lock_release,
4248 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4249 .rpc_call_prepare = nfs4_recover_lock_prepare,
4250 .rpc_call_done = nfs4_lock_done,
4251 .rpc_release = nfs4_lock_release,
4254 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4256 switch (error) {
4257 case -NFS4ERR_ADMIN_REVOKED:
4258 case -NFS4ERR_BAD_STATEID:
4259 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4260 if (new_lock_owner != 0 ||
4261 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4262 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4263 break;
4264 case -NFS4ERR_STALE_STATEID:
4265 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4266 case -NFS4ERR_EXPIRED:
4267 nfs4_schedule_lease_recovery(server->nfs_client);
4271 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4273 struct nfs4_lockdata *data;
4274 struct rpc_task *task;
4275 struct rpc_message msg = {
4276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4277 .rpc_cred = state->owner->so_cred,
4279 struct rpc_task_setup task_setup_data = {
4280 .rpc_client = NFS_CLIENT(state->inode),
4281 .rpc_message = &msg,
4282 .callback_ops = &nfs4_lock_ops,
4283 .workqueue = nfsiod_workqueue,
4284 .flags = RPC_TASK_ASYNC,
4286 int ret;
4288 dprintk("%s: begin!\n", __func__);
4289 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4290 fl->fl_u.nfs4_fl.owner,
4291 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4292 if (data == NULL)
4293 return -ENOMEM;
4294 if (IS_SETLKW(cmd))
4295 data->arg.block = 1;
4296 if (recovery_type > NFS_LOCK_NEW) {
4297 if (recovery_type == NFS_LOCK_RECLAIM)
4298 data->arg.reclaim = NFS_LOCK_RECLAIM;
4299 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4301 msg.rpc_argp = &data->arg;
4302 msg.rpc_resp = &data->res;
4303 task_setup_data.callback_data = data;
4304 task = rpc_run_task(&task_setup_data);
4305 if (IS_ERR(task))
4306 return PTR_ERR(task);
4307 ret = nfs4_wait_for_completion_rpc_task(task);
4308 if (ret == 0) {
4309 ret = data->rpc_status;
4310 if (ret)
4311 nfs4_handle_setlk_error(data->server, data->lsp,
4312 data->arg.new_lock_owner, ret);
4313 } else
4314 data->cancelled = 1;
4315 rpc_put_task(task);
4316 dprintk("%s: done, ret = %d!\n", __func__, ret);
4317 return ret;
4320 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4322 struct nfs_server *server = NFS_SERVER(state->inode);
4323 struct nfs4_exception exception = { };
4324 int err;
4326 do {
4327 /* Cache the lock if possible... */
4328 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4329 return 0;
4330 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4331 if (err != -NFS4ERR_DELAY)
4332 break;
4333 nfs4_handle_exception(server, err, &exception);
4334 } while (exception.retry);
4335 return err;
4338 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4340 struct nfs_server *server = NFS_SERVER(state->inode);
4341 struct nfs4_exception exception = { };
4342 int err;
4344 err = nfs4_set_lock_state(state, request);
4345 if (err != 0)
4346 return err;
4347 do {
4348 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4349 return 0;
4350 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4351 switch (err) {
4352 default:
4353 goto out;
4354 case -NFS4ERR_GRACE:
4355 case -NFS4ERR_DELAY:
4356 nfs4_handle_exception(server, err, &exception);
4357 err = 0;
4359 } while (exception.retry);
4360 out:
4361 return err;
4364 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4366 struct nfs_inode *nfsi = NFS_I(state->inode);
4367 unsigned char fl_flags = request->fl_flags;
4368 int status = -ENOLCK;
4370 if ((fl_flags & FL_POSIX) &&
4371 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4372 goto out;
4373 /* Is this a delegated open? */
4374 status = nfs4_set_lock_state(state, request);
4375 if (status != 0)
4376 goto out;
4377 request->fl_flags |= FL_ACCESS;
4378 status = do_vfs_lock(request->fl_file, request);
4379 if (status < 0)
4380 goto out;
4381 down_read(&nfsi->rwsem);
4382 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4383 /* Yes: cache locks! */
4384 /* ...but avoid races with delegation recall... */
4385 request->fl_flags = fl_flags & ~FL_SLEEP;
4386 status = do_vfs_lock(request->fl_file, request);
4387 goto out_unlock;
4389 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4390 if (status != 0)
4391 goto out_unlock;
4392 /* Note: we always want to sleep here! */
4393 request->fl_flags = fl_flags | FL_SLEEP;
4394 if (do_vfs_lock(request->fl_file, request) < 0)
4395 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4396 out_unlock:
4397 up_read(&nfsi->rwsem);
4398 out:
4399 request->fl_flags = fl_flags;
4400 return status;
4403 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4405 struct nfs4_exception exception = { };
4406 int err;
4408 do {
4409 err = _nfs4_proc_setlk(state, cmd, request);
4410 if (err == -NFS4ERR_DENIED)
4411 err = -EAGAIN;
4412 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4413 err, &exception);
4414 } while (exception.retry);
4415 return err;
4418 static int
4419 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4421 struct nfs_open_context *ctx;
4422 struct nfs4_state *state;
4423 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4424 int status;
4426 /* verify open state */
4427 ctx = nfs_file_open_context(filp);
4428 state = ctx->state;
4430 if (request->fl_start < 0 || request->fl_end < 0)
4431 return -EINVAL;
4433 if (IS_GETLK(cmd)) {
4434 if (state != NULL)
4435 return nfs4_proc_getlk(state, F_GETLK, request);
4436 return 0;
4439 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4440 return -EINVAL;
4442 if (request->fl_type == F_UNLCK) {
4443 if (state != NULL)
4444 return nfs4_proc_unlck(state, cmd, request);
4445 return 0;
4448 if (state == NULL)
4449 return -ENOLCK;
4450 do {
4451 status = nfs4_proc_setlk(state, cmd, request);
4452 if ((status != -EAGAIN) || IS_SETLK(cmd))
4453 break;
4454 timeout = nfs4_set_lock_task_retry(timeout);
4455 status = -ERESTARTSYS;
4456 if (signalled())
4457 break;
4458 } while(status < 0);
4459 return status;
4462 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4464 struct nfs_server *server = NFS_SERVER(state->inode);
4465 struct nfs4_exception exception = { };
4466 int err;
4468 err = nfs4_set_lock_state(state, fl);
4469 if (err != 0)
4470 goto out;
4471 do {
4472 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4473 switch (err) {
4474 default:
4475 printk(KERN_ERR "%s: unhandled error %d.\n",
4476 __func__, err);
4477 case 0:
4478 case -ESTALE:
4479 goto out;
4480 case -NFS4ERR_EXPIRED:
4481 case -NFS4ERR_STALE_CLIENTID:
4482 case -NFS4ERR_STALE_STATEID:
4483 nfs4_schedule_lease_recovery(server->nfs_client);
4484 goto out;
4485 case -NFS4ERR_BADSESSION:
4486 case -NFS4ERR_BADSLOT:
4487 case -NFS4ERR_BAD_HIGH_SLOT:
4488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4489 case -NFS4ERR_DEADSESSION:
4490 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4491 goto out;
4492 case -ERESTARTSYS:
4494 * The show must go on: exit, but mark the
4495 * stateid as needing recovery.
4497 case -NFS4ERR_ADMIN_REVOKED:
4498 case -NFS4ERR_BAD_STATEID:
4499 case -NFS4ERR_OPENMODE:
4500 nfs4_schedule_stateid_recovery(server, state);
4501 err = 0;
4502 goto out;
4503 case -EKEYEXPIRED:
4505 * User RPCSEC_GSS context has expired.
4506 * We cannot recover this stateid now, so
4507 * skip it and allow recovery thread to
4508 * proceed.
4510 err = 0;
4511 goto out;
4512 case -ENOMEM:
4513 case -NFS4ERR_DENIED:
4514 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4515 err = 0;
4516 goto out;
4517 case -NFS4ERR_DELAY:
4518 break;
4520 err = nfs4_handle_exception(server, err, &exception);
4521 } while (exception.retry);
4522 out:
4523 return err;
4526 static void nfs4_release_lockowner_release(void *calldata)
4528 kfree(calldata);
4531 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4532 .rpc_release = nfs4_release_lockowner_release,
4535 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4537 struct nfs_server *server = lsp->ls_state->owner->so_server;
4538 struct nfs_release_lockowner_args *args;
4539 struct rpc_message msg = {
4540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4543 if (server->nfs_client->cl_mvops->minor_version != 0)
4544 return;
4545 args = kmalloc(sizeof(*args), GFP_NOFS);
4546 if (!args)
4547 return;
4548 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4549 args->lock_owner.id = lsp->ls_id.id;
4550 args->lock_owner.s_dev = server->s_dev;
4551 msg.rpc_argp = args;
4552 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4555 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4557 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4558 const void *buf, size_t buflen,
4559 int flags, int type)
4561 if (strcmp(key, "") != 0)
4562 return -EINVAL;
4564 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4567 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4568 void *buf, size_t buflen, int type)
4570 if (strcmp(key, "") != 0)
4571 return -EINVAL;
4573 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4576 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4577 size_t list_len, const char *name,
4578 size_t name_len, int type)
4580 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4582 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4583 return 0;
4585 if (list && len <= list_len)
4586 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4587 return len;
4590 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4592 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4593 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4594 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4595 return;
4597 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4598 NFS_ATTR_FATTR_NLINK;
4599 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4600 fattr->nlink = 2;
4603 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4604 struct nfs4_fs_locations *fs_locations, struct page *page)
4606 struct nfs_server *server = NFS_SERVER(dir);
4607 u32 bitmask[2] = {
4608 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4609 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4611 struct nfs4_fs_locations_arg args = {
4612 .dir_fh = NFS_FH(dir),
4613 .name = name,
4614 .page = page,
4615 .bitmask = bitmask,
4617 struct nfs4_fs_locations_res res = {
4618 .fs_locations = fs_locations,
4620 struct rpc_message msg = {
4621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4622 .rpc_argp = &args,
4623 .rpc_resp = &res,
4625 int status;
4627 dprintk("%s: start\n", __func__);
4628 nfs_fattr_init(&fs_locations->fattr);
4629 fs_locations->server = server;
4630 fs_locations->nlocations = 0;
4631 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4632 nfs_fixup_referral_attributes(&fs_locations->fattr);
4633 dprintk("%s: returned status = %d\n", __func__, status);
4634 return status;
4637 #ifdef CONFIG_NFS_V4_1
4639 * Check the exchange flags returned by the server for invalid flags, having
4640 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4641 * DS flags set.
4643 static int nfs4_check_cl_exchange_flags(u32 flags)
4645 if (flags & ~EXCHGID4_FLAG_MASK_R)
4646 goto out_inval;
4647 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4648 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4649 goto out_inval;
4650 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4651 goto out_inval;
4652 return NFS_OK;
4653 out_inval:
4654 return -NFS4ERR_INVAL;
4658 * nfs4_proc_exchange_id()
4660 * Since the clientid has expired, all compounds using sessions
4661 * associated with the stale clientid will be returning
4662 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4663 * be in some phase of session reset.
4665 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4667 nfs4_verifier verifier;
4668 struct nfs41_exchange_id_args args = {
4669 .client = clp,
4670 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4672 struct nfs41_exchange_id_res res = {
4673 .client = clp,
4675 int status;
4676 struct rpc_message msg = {
4677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4678 .rpc_argp = &args,
4679 .rpc_resp = &res,
4680 .rpc_cred = cred,
4682 __be32 *p;
4684 dprintk("--> %s\n", __func__);
4685 BUG_ON(clp == NULL);
4687 p = (u32 *)verifier.data;
4688 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4689 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4690 args.verifier = &verifier;
4692 args.id_len = scnprintf(args.id, sizeof(args.id),
4693 "%s/%s.%s/%u",
4694 clp->cl_ipaddr,
4695 init_utsname()->nodename,
4696 init_utsname()->domainname,
4697 clp->cl_rpcclient->cl_auth->au_flavor);
4699 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4700 if (!status)
4701 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4702 dprintk("<-- %s status= %d\n", __func__, status);
4703 return status;
4706 struct nfs4_get_lease_time_data {
4707 struct nfs4_get_lease_time_args *args;
4708 struct nfs4_get_lease_time_res *res;
4709 struct nfs_client *clp;
4712 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4713 void *calldata)
4715 int ret;
4716 struct nfs4_get_lease_time_data *data =
4717 (struct nfs4_get_lease_time_data *)calldata;
4719 dprintk("--> %s\n", __func__);
4720 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4721 /* just setup sequence, do not trigger session recovery
4722 since we're invoked within one */
4723 ret = nfs41_setup_sequence(data->clp->cl_session,
4724 &data->args->la_seq_args,
4725 &data->res->lr_seq_res, 0, task);
4727 BUG_ON(ret == -EAGAIN);
4728 rpc_call_start(task);
4729 dprintk("<-- %s\n", __func__);
4733 * Called from nfs4_state_manager thread for session setup, so don't recover
4734 * from sequence operation or clientid errors.
4736 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4738 struct nfs4_get_lease_time_data *data =
4739 (struct nfs4_get_lease_time_data *)calldata;
4741 dprintk("--> %s\n", __func__);
4742 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4743 return;
4744 switch (task->tk_status) {
4745 case -NFS4ERR_DELAY:
4746 case -NFS4ERR_GRACE:
4747 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4748 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4749 task->tk_status = 0;
4750 nfs_restart_rpc(task, data->clp);
4751 return;
4753 dprintk("<-- %s\n", __func__);
4756 struct rpc_call_ops nfs4_get_lease_time_ops = {
4757 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4758 .rpc_call_done = nfs4_get_lease_time_done,
4761 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4763 struct rpc_task *task;
4764 struct nfs4_get_lease_time_args args;
4765 struct nfs4_get_lease_time_res res = {
4766 .lr_fsinfo = fsinfo,
4768 struct nfs4_get_lease_time_data data = {
4769 .args = &args,
4770 .res = &res,
4771 .clp = clp,
4773 struct rpc_message msg = {
4774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4775 .rpc_argp = &args,
4776 .rpc_resp = &res,
4778 struct rpc_task_setup task_setup = {
4779 .rpc_client = clp->cl_rpcclient,
4780 .rpc_message = &msg,
4781 .callback_ops = &nfs4_get_lease_time_ops,
4782 .callback_data = &data
4784 int status;
4786 dprintk("--> %s\n", __func__);
4787 task = rpc_run_task(&task_setup);
4789 if (IS_ERR(task))
4790 status = PTR_ERR(task);
4791 else {
4792 status = task->tk_status;
4793 rpc_put_task(task);
4795 dprintk("<-- %s return %d\n", __func__, status);
4797 return status;
4801 * Reset a slot table
4803 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4804 int ivalue)
4806 struct nfs4_slot *new = NULL;
4807 int i;
4808 int ret = 0;
4810 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4811 max_reqs, tbl->max_slots);
4813 /* Does the newly negotiated max_reqs match the existing slot table? */
4814 if (max_reqs != tbl->max_slots) {
4815 ret = -ENOMEM;
4816 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4817 GFP_NOFS);
4818 if (!new)
4819 goto out;
4820 ret = 0;
4821 kfree(tbl->slots);
4823 spin_lock(&tbl->slot_tbl_lock);
4824 if (new) {
4825 tbl->slots = new;
4826 tbl->max_slots = max_reqs;
4828 for (i = 0; i < tbl->max_slots; ++i)
4829 tbl->slots[i].seq_nr = ivalue;
4830 spin_unlock(&tbl->slot_tbl_lock);
4831 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4832 tbl, tbl->slots, tbl->max_slots);
4833 out:
4834 dprintk("<-- %s: return %d\n", __func__, ret);
4835 return ret;
4839 * Reset the forechannel and backchannel slot tables
4841 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4843 int status;
4845 status = nfs4_reset_slot_table(&session->fc_slot_table,
4846 session->fc_attrs.max_reqs, 1);
4847 if (status)
4848 return status;
4850 status = nfs4_reset_slot_table(&session->bc_slot_table,
4851 session->bc_attrs.max_reqs, 0);
4852 return status;
4855 /* Destroy the slot table */
4856 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4858 if (session->fc_slot_table.slots != NULL) {
4859 kfree(session->fc_slot_table.slots);
4860 session->fc_slot_table.slots = NULL;
4862 if (session->bc_slot_table.slots != NULL) {
4863 kfree(session->bc_slot_table.slots);
4864 session->bc_slot_table.slots = NULL;
4866 return;
4870 * Initialize slot table
4872 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4873 int max_slots, int ivalue)
4875 struct nfs4_slot *slot;
4876 int ret = -ENOMEM;
4878 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4880 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4882 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4883 if (!slot)
4884 goto out;
4885 ret = 0;
4887 spin_lock(&tbl->slot_tbl_lock);
4888 tbl->max_slots = max_slots;
4889 tbl->slots = slot;
4890 tbl->highest_used_slotid = -1; /* no slot is currently used */
4891 spin_unlock(&tbl->slot_tbl_lock);
4892 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4893 tbl, tbl->slots, tbl->max_slots);
4894 out:
4895 dprintk("<-- %s: return %d\n", __func__, ret);
4896 return ret;
4900 * Initialize the forechannel and backchannel tables
4902 static int nfs4_init_slot_tables(struct nfs4_session *session)
4904 struct nfs4_slot_table *tbl;
4905 int status = 0;
4907 tbl = &session->fc_slot_table;
4908 if (tbl->slots == NULL) {
4909 status = nfs4_init_slot_table(tbl,
4910 session->fc_attrs.max_reqs, 1);
4911 if (status)
4912 return status;
4915 tbl = &session->bc_slot_table;
4916 if (tbl->slots == NULL) {
4917 status = nfs4_init_slot_table(tbl,
4918 session->bc_attrs.max_reqs, 0);
4919 if (status)
4920 nfs4_destroy_slot_tables(session);
4923 return status;
4926 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4928 struct nfs4_session *session;
4929 struct nfs4_slot_table *tbl;
4931 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4932 if (!session)
4933 return NULL;
4935 tbl = &session->fc_slot_table;
4936 tbl->highest_used_slotid = -1;
4937 spin_lock_init(&tbl->slot_tbl_lock);
4938 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4939 init_completion(&tbl->complete);
4941 tbl = &session->bc_slot_table;
4942 tbl->highest_used_slotid = -1;
4943 spin_lock_init(&tbl->slot_tbl_lock);
4944 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4945 init_completion(&tbl->complete);
4947 session->session_state = 1<<NFS4_SESSION_INITING;
4949 session->clp = clp;
4950 return session;
4953 void nfs4_destroy_session(struct nfs4_session *session)
4955 nfs4_proc_destroy_session(session);
4956 dprintk("%s Destroy backchannel for xprt %p\n",
4957 __func__, session->clp->cl_rpcclient->cl_xprt);
4958 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4959 NFS41_BC_MIN_CALLBACKS);
4960 nfs4_destroy_slot_tables(session);
4961 kfree(session);
4965 * Initialize the values to be used by the client in CREATE_SESSION
4966 * If nfs4_init_session set the fore channel request and response sizes,
4967 * use them.
4969 * Set the back channel max_resp_sz_cached to zero to force the client to
4970 * always set csa_cachethis to FALSE because the current implementation
4971 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4973 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4975 struct nfs4_session *session = args->client->cl_session;
4976 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4977 mxresp_sz = session->fc_attrs.max_resp_sz;
4979 if (mxrqst_sz == 0)
4980 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4981 if (mxresp_sz == 0)
4982 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4983 /* Fore channel attributes */
4984 args->fc_attrs.headerpadsz = 0;
4985 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4986 args->fc_attrs.max_resp_sz = mxresp_sz;
4987 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4988 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4990 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4991 "max_ops=%u max_reqs=%u\n",
4992 __func__,
4993 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4994 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4996 /* Back channel attributes */
4997 args->bc_attrs.headerpadsz = 0;
4998 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4999 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5000 args->bc_attrs.max_resp_sz_cached = 0;
5001 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5002 args->bc_attrs.max_reqs = 1;
5004 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5005 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5006 __func__,
5007 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5008 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5009 args->bc_attrs.max_reqs);
5012 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5014 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5015 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5017 if (rcvd->headerpadsz > sent->headerpadsz)
5018 return -EINVAL;
5019 if (rcvd->max_resp_sz > sent->max_resp_sz)
5020 return -EINVAL;
5022 * Our requested max_ops is the minimum we need; we're not
5023 * prepared to break up compounds into smaller pieces than that.
5024 * So, no point even trying to continue if the server won't
5025 * cooperate:
5027 if (rcvd->max_ops < sent->max_ops)
5028 return -EINVAL;
5029 if (rcvd->max_reqs == 0)
5030 return -EINVAL;
5031 return 0;
5034 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5036 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5037 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5039 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5040 return -EINVAL;
5041 if (rcvd->max_resp_sz < sent->max_resp_sz)
5042 return -EINVAL;
5043 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5044 return -EINVAL;
5045 /* These would render the backchannel useless: */
5046 if (rcvd->max_ops == 0)
5047 return -EINVAL;
5048 if (rcvd->max_reqs == 0)
5049 return -EINVAL;
5050 return 0;
5053 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5054 struct nfs4_session *session)
5056 int ret;
5058 ret = nfs4_verify_fore_channel_attrs(args, session);
5059 if (ret)
5060 return ret;
5061 return nfs4_verify_back_channel_attrs(args, session);
5064 static int _nfs4_proc_create_session(struct nfs_client *clp)
5066 struct nfs4_session *session = clp->cl_session;
5067 struct nfs41_create_session_args args = {
5068 .client = clp,
5069 .cb_program = NFS4_CALLBACK,
5071 struct nfs41_create_session_res res = {
5072 .client = clp,
5074 struct rpc_message msg = {
5075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5076 .rpc_argp = &args,
5077 .rpc_resp = &res,
5079 int status;
5081 nfs4_init_channel_attrs(&args);
5082 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5084 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5086 if (!status)
5087 /* Verify the session's negotiated channel_attrs values */
5088 status = nfs4_verify_channel_attrs(&args, session);
5089 if (!status) {
5090 /* Increment the clientid slot sequence id */
5091 clp->cl_seqid++;
5094 return status;
5098 * Issues a CREATE_SESSION operation to the server.
5099 * It is the responsibility of the caller to verify the session is
5100 * expired before calling this routine.
5102 int nfs4_proc_create_session(struct nfs_client *clp)
5104 int status;
5105 unsigned *ptr;
5106 struct nfs4_session *session = clp->cl_session;
5107 long timeout = 0;
5108 int err;
5110 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5112 do {
5113 status = _nfs4_proc_create_session(clp);
5114 if (status == -NFS4ERR_DELAY) {
5115 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5116 if (err)
5117 status = err;
5119 } while (status == -NFS4ERR_DELAY);
5121 if (status)
5122 goto out;
5124 /* Init and reset the fore channel */
5125 status = nfs4_init_slot_tables(session);
5126 dprintk("slot table initialization returned %d\n", status);
5127 if (status)
5128 goto out;
5129 status = nfs4_reset_slot_tables(session);
5130 dprintk("slot table reset returned %d\n", status);
5131 if (status)
5132 goto out;
5134 ptr = (unsigned *)&session->sess_id.data[0];
5135 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5136 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5137 out:
5138 dprintk("<-- %s\n", __func__);
5139 return status;
5143 * Issue the over-the-wire RPC DESTROY_SESSION.
5144 * The caller must serialize access to this routine.
5146 int nfs4_proc_destroy_session(struct nfs4_session *session)
5148 int status = 0;
5149 struct rpc_message msg;
5151 dprintk("--> nfs4_proc_destroy_session\n");
5153 /* session is still being setup */
5154 if (session->clp->cl_cons_state != NFS_CS_READY)
5155 return status;
5157 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5158 msg.rpc_argp = session;
5159 msg.rpc_resp = NULL;
5160 msg.rpc_cred = NULL;
5161 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5163 if (status)
5164 printk(KERN_WARNING
5165 "Got error %d from the server on DESTROY_SESSION. "
5166 "Session has been destroyed regardless...\n", status);
5168 dprintk("<-- nfs4_proc_destroy_session\n");
5169 return status;
5172 int nfs4_init_session(struct nfs_server *server)
5174 struct nfs_client *clp = server->nfs_client;
5175 struct nfs4_session *session;
5176 unsigned int rsize, wsize;
5177 int ret;
5179 if (!nfs4_has_session(clp))
5180 return 0;
5182 session = clp->cl_session;
5183 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5184 return 0;
5186 rsize = server->rsize;
5187 if (rsize == 0)
5188 rsize = NFS_MAX_FILE_IO_SIZE;
5189 wsize = server->wsize;
5190 if (wsize == 0)
5191 wsize = NFS_MAX_FILE_IO_SIZE;
5193 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5194 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5196 ret = nfs4_recover_expired_lease(server);
5197 if (!ret)
5198 ret = nfs4_check_client_ready(clp);
5199 return ret;
5202 int nfs4_init_ds_session(struct nfs_client *clp)
5204 struct nfs4_session *session = clp->cl_session;
5205 int ret;
5207 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5208 return 0;
5210 ret = nfs4_client_recover_expired_lease(clp);
5211 if (!ret)
5212 /* Test for the DS role */
5213 if (!is_ds_client(clp))
5214 ret = -ENODEV;
5215 if (!ret)
5216 ret = nfs4_check_client_ready(clp);
5217 return ret;
5220 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5224 * Renew the cl_session lease.
5226 struct nfs4_sequence_data {
5227 struct nfs_client *clp;
5228 struct nfs4_sequence_args args;
5229 struct nfs4_sequence_res res;
5232 static void nfs41_sequence_release(void *data)
5234 struct nfs4_sequence_data *calldata = data;
5235 struct nfs_client *clp = calldata->clp;
5237 if (atomic_read(&clp->cl_count) > 1)
5238 nfs4_schedule_state_renewal(clp);
5239 nfs_put_client(clp);
5240 kfree(calldata);
5243 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5245 switch(task->tk_status) {
5246 case -NFS4ERR_DELAY:
5247 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5248 return -EAGAIN;
5249 default:
5250 nfs4_schedule_lease_recovery(clp);
5252 return 0;
5255 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5257 struct nfs4_sequence_data *calldata = data;
5258 struct nfs_client *clp = calldata->clp;
5260 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5261 return;
5263 if (task->tk_status < 0) {
5264 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5265 if (atomic_read(&clp->cl_count) == 1)
5266 goto out;
5268 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5269 rpc_restart_call_prepare(task);
5270 return;
5273 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5274 out:
5275 dprintk("<-- %s\n", __func__);
5278 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5280 struct nfs4_sequence_data *calldata = data;
5281 struct nfs_client *clp = calldata->clp;
5282 struct nfs4_sequence_args *args;
5283 struct nfs4_sequence_res *res;
5285 args = task->tk_msg.rpc_argp;
5286 res = task->tk_msg.rpc_resp;
5288 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5289 return;
5290 rpc_call_start(task);
5293 static const struct rpc_call_ops nfs41_sequence_ops = {
5294 .rpc_call_done = nfs41_sequence_call_done,
5295 .rpc_call_prepare = nfs41_sequence_prepare,
5296 .rpc_release = nfs41_sequence_release,
5299 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5301 struct nfs4_sequence_data *calldata;
5302 struct rpc_message msg = {
5303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5304 .rpc_cred = cred,
5306 struct rpc_task_setup task_setup_data = {
5307 .rpc_client = clp->cl_rpcclient,
5308 .rpc_message = &msg,
5309 .callback_ops = &nfs41_sequence_ops,
5310 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5313 if (!atomic_inc_not_zero(&clp->cl_count))
5314 return ERR_PTR(-EIO);
5315 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5316 if (calldata == NULL) {
5317 nfs_put_client(clp);
5318 return ERR_PTR(-ENOMEM);
5320 msg.rpc_argp = &calldata->args;
5321 msg.rpc_resp = &calldata->res;
5322 calldata->clp = clp;
5323 task_setup_data.callback_data = calldata;
5325 return rpc_run_task(&task_setup_data);
5328 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5330 struct rpc_task *task;
5331 int ret = 0;
5333 task = _nfs41_proc_sequence(clp, cred);
5334 if (IS_ERR(task))
5335 ret = PTR_ERR(task);
5336 else
5337 rpc_put_task_async(task);
5338 dprintk("<-- %s status=%d\n", __func__, ret);
5339 return ret;
5342 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5344 struct rpc_task *task;
5345 int ret;
5347 task = _nfs41_proc_sequence(clp, cred);
5348 if (IS_ERR(task)) {
5349 ret = PTR_ERR(task);
5350 goto out;
5352 ret = rpc_wait_for_completion_task(task);
5353 if (!ret) {
5354 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5356 if (task->tk_status == 0)
5357 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5358 ret = task->tk_status;
5360 rpc_put_task(task);
5361 out:
5362 dprintk("<-- %s status=%d\n", __func__, ret);
5363 return ret;
5366 struct nfs4_reclaim_complete_data {
5367 struct nfs_client *clp;
5368 struct nfs41_reclaim_complete_args arg;
5369 struct nfs41_reclaim_complete_res res;
5372 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5374 struct nfs4_reclaim_complete_data *calldata = data;
5376 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5377 if (nfs41_setup_sequence(calldata->clp->cl_session,
5378 &calldata->arg.seq_args,
5379 &calldata->res.seq_res, 0, task))
5380 return;
5382 rpc_call_start(task);
5385 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5387 switch(task->tk_status) {
5388 case 0:
5389 case -NFS4ERR_COMPLETE_ALREADY:
5390 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5391 break;
5392 case -NFS4ERR_DELAY:
5393 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5394 return -EAGAIN;
5395 default:
5396 nfs4_schedule_lease_recovery(clp);
5398 return 0;
5401 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5403 struct nfs4_reclaim_complete_data *calldata = data;
5404 struct nfs_client *clp = calldata->clp;
5405 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5407 dprintk("--> %s\n", __func__);
5408 if (!nfs41_sequence_done(task, res))
5409 return;
5411 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5412 rpc_restart_call_prepare(task);
5413 return;
5415 dprintk("<-- %s\n", __func__);
5418 static void nfs4_free_reclaim_complete_data(void *data)
5420 struct nfs4_reclaim_complete_data *calldata = data;
5422 kfree(calldata);
5425 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5426 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5427 .rpc_call_done = nfs4_reclaim_complete_done,
5428 .rpc_release = nfs4_free_reclaim_complete_data,
5432 * Issue a global reclaim complete.
5434 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5436 struct nfs4_reclaim_complete_data *calldata;
5437 struct rpc_task *task;
5438 struct rpc_message msg = {
5439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5441 struct rpc_task_setup task_setup_data = {
5442 .rpc_client = clp->cl_rpcclient,
5443 .rpc_message = &msg,
5444 .callback_ops = &nfs4_reclaim_complete_call_ops,
5445 .flags = RPC_TASK_ASYNC,
5447 int status = -ENOMEM;
5449 dprintk("--> %s\n", __func__);
5450 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5451 if (calldata == NULL)
5452 goto out;
5453 calldata->clp = clp;
5454 calldata->arg.one_fs = 0;
5456 msg.rpc_argp = &calldata->arg;
5457 msg.rpc_resp = &calldata->res;
5458 task_setup_data.callback_data = calldata;
5459 task = rpc_run_task(&task_setup_data);
5460 if (IS_ERR(task)) {
5461 status = PTR_ERR(task);
5462 goto out;
5464 status = nfs4_wait_for_completion_rpc_task(task);
5465 if (status == 0)
5466 status = task->tk_status;
5467 rpc_put_task(task);
5468 return 0;
5469 out:
5470 dprintk("<-- %s status=%d\n", __func__, status);
5471 return status;
5474 static void
5475 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5477 struct nfs4_layoutget *lgp = calldata;
5478 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5480 dprintk("--> %s\n", __func__);
5481 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5482 * right now covering the LAYOUTGET we are about to send.
5483 * However, that is not so catastrophic, and there seems
5484 * to be no way to prevent it completely.
5486 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5487 &lgp->res.seq_res, 0, task))
5488 return;
5489 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5490 NFS_I(lgp->args.inode)->layout,
5491 lgp->args.ctx->state)) {
5492 rpc_exit(task, NFS4_OK);
5493 return;
5495 rpc_call_start(task);
5498 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5500 struct nfs4_layoutget *lgp = calldata;
5501 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5503 dprintk("--> %s\n", __func__);
5505 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5506 return;
5508 switch (task->tk_status) {
5509 case 0:
5510 break;
5511 case -NFS4ERR_LAYOUTTRYLATER:
5512 case -NFS4ERR_RECALLCONFLICT:
5513 task->tk_status = -NFS4ERR_DELAY;
5514 /* Fall through */
5515 default:
5516 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5517 rpc_restart_call_prepare(task);
5518 return;
5521 dprintk("<-- %s\n", __func__);
5524 static void nfs4_layoutget_release(void *calldata)
5526 struct nfs4_layoutget *lgp = calldata;
5528 dprintk("--> %s\n", __func__);
5529 if (lgp->res.layout.buf != NULL)
5530 free_page((unsigned long) lgp->res.layout.buf);
5531 put_nfs_open_context(lgp->args.ctx);
5532 kfree(calldata);
5533 dprintk("<-- %s\n", __func__);
5536 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5537 .rpc_call_prepare = nfs4_layoutget_prepare,
5538 .rpc_call_done = nfs4_layoutget_done,
5539 .rpc_release = nfs4_layoutget_release,
5542 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5544 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5545 struct rpc_task *task;
5546 struct rpc_message msg = {
5547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5548 .rpc_argp = &lgp->args,
5549 .rpc_resp = &lgp->res,
5551 struct rpc_task_setup task_setup_data = {
5552 .rpc_client = server->client,
5553 .rpc_message = &msg,
5554 .callback_ops = &nfs4_layoutget_call_ops,
5555 .callback_data = lgp,
5556 .flags = RPC_TASK_ASYNC,
5558 int status = 0;
5560 dprintk("--> %s\n", __func__);
5562 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5563 if (lgp->res.layout.buf == NULL) {
5564 nfs4_layoutget_release(lgp);
5565 return -ENOMEM;
5568 lgp->res.seq_res.sr_slot = NULL;
5569 task = rpc_run_task(&task_setup_data);
5570 if (IS_ERR(task))
5571 return PTR_ERR(task);
5572 status = nfs4_wait_for_completion_rpc_task(task);
5573 if (status == 0)
5574 status = task->tk_status;
5575 if (status == 0)
5576 status = pnfs_layout_process(lgp);
5577 rpc_put_task(task);
5578 dprintk("<-- %s status=%d\n", __func__, status);
5579 return status;
5582 static int
5583 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5585 struct nfs4_getdeviceinfo_args args = {
5586 .pdev = pdev,
5588 struct nfs4_getdeviceinfo_res res = {
5589 .pdev = pdev,
5591 struct rpc_message msg = {
5592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5593 .rpc_argp = &args,
5594 .rpc_resp = &res,
5596 int status;
5598 dprintk("--> %s\n", __func__);
5599 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5600 dprintk("<-- %s status=%d\n", __func__, status);
5602 return status;
5605 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5607 struct nfs4_exception exception = { };
5608 int err;
5610 do {
5611 err = nfs4_handle_exception(server,
5612 _nfs4_proc_getdeviceinfo(server, pdev),
5613 &exception);
5614 } while (exception.retry);
5615 return err;
5617 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5619 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5621 struct nfs4_layoutcommit_data *data = calldata;
5622 struct nfs_server *server = NFS_SERVER(data->args.inode);
5624 if (nfs4_setup_sequence(server, &data->args.seq_args,
5625 &data->res.seq_res, 1, task))
5626 return;
5627 rpc_call_start(task);
5630 static void
5631 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5633 struct nfs4_layoutcommit_data *data = calldata;
5634 struct nfs_server *server = NFS_SERVER(data->args.inode);
5636 if (!nfs4_sequence_done(task, &data->res.seq_res))
5637 return;
5639 switch (task->tk_status) { /* Just ignore these failures */
5640 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5641 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5642 case NFS4ERR_BADLAYOUT: /* no layout */
5643 case NFS4ERR_GRACE: /* loca_recalim always false */
5644 task->tk_status = 0;
5647 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5648 nfs_restart_rpc(task, server->nfs_client);
5649 return;
5652 if (task->tk_status == 0)
5653 nfs_post_op_update_inode_force_wcc(data->args.inode,
5654 data->res.fattr);
5657 static void nfs4_layoutcommit_release(void *calldata)
5659 struct nfs4_layoutcommit_data *data = calldata;
5661 /* Matched by references in pnfs_set_layoutcommit */
5662 put_lseg(data->lseg);
5663 put_rpccred(data->cred);
5664 kfree(data);
5667 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5668 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5669 .rpc_call_done = nfs4_layoutcommit_done,
5670 .rpc_release = nfs4_layoutcommit_release,
5674 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
5676 struct rpc_message msg = {
5677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5678 .rpc_argp = &data->args,
5679 .rpc_resp = &data->res,
5680 .rpc_cred = data->cred,
5682 struct rpc_task_setup task_setup_data = {
5683 .task = &data->task,
5684 .rpc_client = NFS_CLIENT(data->args.inode),
5685 .rpc_message = &msg,
5686 .callback_ops = &nfs4_layoutcommit_ops,
5687 .callback_data = data,
5688 .flags = RPC_TASK_ASYNC,
5690 struct rpc_task *task;
5691 int status = 0;
5693 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5694 "lbw: %llu inode %lu\n",
5695 data->task.tk_pid, sync,
5696 data->args.lastbytewritten,
5697 data->args.inode->i_ino);
5699 task = rpc_run_task(&task_setup_data);
5700 if (IS_ERR(task))
5701 return PTR_ERR(task);
5702 if (sync == false)
5703 goto out;
5704 status = nfs4_wait_for_completion_rpc_task(task);
5705 if (status != 0)
5706 goto out;
5707 status = task->tk_status;
5708 out:
5709 dprintk("%s: status %d\n", __func__, status);
5710 rpc_put_task(task);
5711 return status;
5713 #endif /* CONFIG_NFS_V4_1 */
5715 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5716 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5717 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5718 .recover_open = nfs4_open_reclaim,
5719 .recover_lock = nfs4_lock_reclaim,
5720 .establish_clid = nfs4_init_clientid,
5721 .get_clid_cred = nfs4_get_setclientid_cred,
5724 #if defined(CONFIG_NFS_V4_1)
5725 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5726 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5727 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5728 .recover_open = nfs4_open_reclaim,
5729 .recover_lock = nfs4_lock_reclaim,
5730 .establish_clid = nfs41_init_clientid,
5731 .get_clid_cred = nfs4_get_exchange_id_cred,
5732 .reclaim_complete = nfs41_proc_reclaim_complete,
5734 #endif /* CONFIG_NFS_V4_1 */
5736 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5737 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5738 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5739 .recover_open = nfs4_open_expired,
5740 .recover_lock = nfs4_lock_expired,
5741 .establish_clid = nfs4_init_clientid,
5742 .get_clid_cred = nfs4_get_setclientid_cred,
5745 #if defined(CONFIG_NFS_V4_1)
5746 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5747 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5748 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5749 .recover_open = nfs4_open_expired,
5750 .recover_lock = nfs4_lock_expired,
5751 .establish_clid = nfs41_init_clientid,
5752 .get_clid_cred = nfs4_get_exchange_id_cred,
5754 #endif /* CONFIG_NFS_V4_1 */
5756 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5757 .sched_state_renewal = nfs4_proc_async_renew,
5758 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5759 .renew_lease = nfs4_proc_renew,
5762 #if defined(CONFIG_NFS_V4_1)
5763 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5764 .sched_state_renewal = nfs41_proc_async_sequence,
5765 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5766 .renew_lease = nfs4_proc_sequence,
5768 #endif
5770 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5771 .minor_version = 0,
5772 .call_sync = _nfs4_call_sync,
5773 .validate_stateid = nfs4_validate_delegation_stateid,
5774 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5775 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5776 .state_renewal_ops = &nfs40_state_renewal_ops,
5779 #if defined(CONFIG_NFS_V4_1)
5780 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5781 .minor_version = 1,
5782 .call_sync = _nfs4_call_sync_session,
5783 .validate_stateid = nfs41_validate_delegation_stateid,
5784 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5785 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5786 .state_renewal_ops = &nfs41_state_renewal_ops,
5788 #endif
5790 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5791 [0] = &nfs_v4_0_minor_ops,
5792 #if defined(CONFIG_NFS_V4_1)
5793 [1] = &nfs_v4_1_minor_ops,
5794 #endif
5797 static const struct inode_operations nfs4_file_inode_operations = {
5798 .permission = nfs_permission,
5799 .getattr = nfs_getattr,
5800 .setattr = nfs_setattr,
5801 .getxattr = generic_getxattr,
5802 .setxattr = generic_setxattr,
5803 .listxattr = generic_listxattr,
5804 .removexattr = generic_removexattr,
5807 const struct nfs_rpc_ops nfs_v4_clientops = {
5808 .version = 4, /* protocol version */
5809 .dentry_ops = &nfs4_dentry_operations,
5810 .dir_inode_ops = &nfs4_dir_inode_operations,
5811 .file_inode_ops = &nfs4_file_inode_operations,
5812 .getroot = nfs4_proc_get_root,
5813 .getattr = nfs4_proc_getattr,
5814 .setattr = nfs4_proc_setattr,
5815 .lookupfh = nfs4_proc_lookupfh,
5816 .lookup = nfs4_proc_lookup,
5817 .access = nfs4_proc_access,
5818 .readlink = nfs4_proc_readlink,
5819 .create = nfs4_proc_create,
5820 .remove = nfs4_proc_remove,
5821 .unlink_setup = nfs4_proc_unlink_setup,
5822 .unlink_done = nfs4_proc_unlink_done,
5823 .rename = nfs4_proc_rename,
5824 .rename_setup = nfs4_proc_rename_setup,
5825 .rename_done = nfs4_proc_rename_done,
5826 .link = nfs4_proc_link,
5827 .symlink = nfs4_proc_symlink,
5828 .mkdir = nfs4_proc_mkdir,
5829 .rmdir = nfs4_proc_remove,
5830 .readdir = nfs4_proc_readdir,
5831 .mknod = nfs4_proc_mknod,
5832 .statfs = nfs4_proc_statfs,
5833 .fsinfo = nfs4_proc_fsinfo,
5834 .pathconf = nfs4_proc_pathconf,
5835 .set_capabilities = nfs4_server_capabilities,
5836 .decode_dirent = nfs4_decode_dirent,
5837 .read_setup = nfs4_proc_read_setup,
5838 .read_done = nfs4_read_done,
5839 .write_setup = nfs4_proc_write_setup,
5840 .write_done = nfs4_write_done,
5841 .commit_setup = nfs4_proc_commit_setup,
5842 .commit_done = nfs4_commit_done,
5843 .lock = nfs4_proc_lock,
5844 .clear_acl_cache = nfs4_zap_acl_attr,
5845 .close_context = nfs4_close_context,
5846 .open_context = nfs4_atomic_open,
5847 .init_client = nfs4_init_client,
5850 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5851 .prefix = XATTR_NAME_NFSV4_ACL,
5852 .list = nfs4_xattr_list_nfs4_acl,
5853 .get = nfs4_xattr_get_nfs4_acl,
5854 .set = nfs4_xattr_set_nfs4_acl,
5857 const struct xattr_handler *nfs4_xattr_handlers[] = {
5858 &nfs4_xattr_nfs4_acl_handler,
5859 NULL
5863 * Local variables:
5864 * c-basic-offset: 8
5865 * End: