xhci: Fix USB 3.0 device restart on resume.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4proc.c
blob055d7027f7a984dc5d0a56699b011c328621e968
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/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
66 #define NFSDBG_FACILITY NFSDBG_PROC
68 #define NFS4_POLL_RETRY_MIN (HZ/10)
69 #define NFS4_POLL_RETRY_MAX (15*HZ)
71 #define NFS4_MAX_LOOP_ON_RECOVER (10)
73 struct nfs4_opendata;
74 static int _nfs4_proc_open(struct nfs4_opendata *data);
75 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
76 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
77 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
78 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
79 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
80 struct nfs_fattr *fattr, struct iattr *sattr,
81 struct nfs4_state *state);
82 #ifdef CONFIG_NFS_V4_1
83 static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
84 static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
85 #endif
86 /* Prevent leaks of NFSv4 errors into userland */
87 static int nfs4_map_errors(int err)
89 if (err >= -1000)
90 return err;
91 switch (err) {
92 case -NFS4ERR_RESOURCE:
93 return -EREMOTEIO;
94 case -NFS4ERR_WRONGSEC:
95 return -EPERM;
96 case -NFS4ERR_BADOWNER:
97 case -NFS4ERR_BADNAME:
98 return -EINVAL;
99 default:
100 dprintk("%s could not handle NFSv4 error %d\n",
101 __func__, -err);
102 break;
104 return -EIO;
108 * This is our standard bitmap for GETATTR requests.
110 const u32 nfs4_fattr_bitmap[2] = {
111 FATTR4_WORD0_TYPE
112 | FATTR4_WORD0_CHANGE
113 | FATTR4_WORD0_SIZE
114 | FATTR4_WORD0_FSID
115 | FATTR4_WORD0_FILEID,
116 FATTR4_WORD1_MODE
117 | FATTR4_WORD1_NUMLINKS
118 | FATTR4_WORD1_OWNER
119 | FATTR4_WORD1_OWNER_GROUP
120 | FATTR4_WORD1_RAWDEV
121 | FATTR4_WORD1_SPACE_USED
122 | FATTR4_WORD1_TIME_ACCESS
123 | FATTR4_WORD1_TIME_METADATA
124 | FATTR4_WORD1_TIME_MODIFY
127 const u32 nfs4_statfs_bitmap[2] = {
128 FATTR4_WORD0_FILES_AVAIL
129 | FATTR4_WORD0_FILES_FREE
130 | FATTR4_WORD0_FILES_TOTAL,
131 FATTR4_WORD1_SPACE_AVAIL
132 | FATTR4_WORD1_SPACE_FREE
133 | FATTR4_WORD1_SPACE_TOTAL
136 const u32 nfs4_pathconf_bitmap[2] = {
137 FATTR4_WORD0_MAXLINK
138 | FATTR4_WORD0_MAXNAME,
142 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
143 | FATTR4_WORD0_MAXREAD
144 | FATTR4_WORD0_MAXWRITE
145 | FATTR4_WORD0_LEASE_TIME,
146 FATTR4_WORD1_TIME_DELTA
147 | FATTR4_WORD1_FS_LAYOUT_TYPES,
148 FATTR4_WORD2_LAYOUT_BLKSIZE
151 const u32 nfs4_fs_locations_bitmap[2] = {
152 FATTR4_WORD0_TYPE
153 | FATTR4_WORD0_CHANGE
154 | FATTR4_WORD0_SIZE
155 | FATTR4_WORD0_FSID
156 | FATTR4_WORD0_FILEID
157 | FATTR4_WORD0_FS_LOCATIONS,
158 FATTR4_WORD1_MODE
159 | FATTR4_WORD1_NUMLINKS
160 | FATTR4_WORD1_OWNER
161 | FATTR4_WORD1_OWNER_GROUP
162 | FATTR4_WORD1_RAWDEV
163 | FATTR4_WORD1_SPACE_USED
164 | FATTR4_WORD1_TIME_ACCESS
165 | FATTR4_WORD1_TIME_METADATA
166 | FATTR4_WORD1_TIME_MODIFY
167 | FATTR4_WORD1_MOUNTED_ON_FILEID
170 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
171 struct nfs4_readdir_arg *readdir)
173 __be32 *start, *p;
175 BUG_ON(readdir->count < 80);
176 if (cookie > 2) {
177 readdir->cookie = cookie;
178 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
179 return;
182 readdir->cookie = 0;
183 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
184 if (cookie == 2)
185 return;
188 * NFSv4 servers do not return entries for '.' and '..'
189 * Therefore, we fake these entries here. We let '.'
190 * have cookie 0 and '..' have cookie 1. Note that
191 * when talking to the server, we always send cookie 0
192 * instead of 1 or 2.
194 start = p = kmap_atomic(*readdir->pages, KM_USER0);
196 if (cookie == 0) {
197 *p++ = xdr_one; /* next */
198 *p++ = xdr_zero; /* cookie, first word */
199 *p++ = xdr_one; /* cookie, second word */
200 *p++ = xdr_one; /* entry len */
201 memcpy(p, ".\0\0\0", 4); /* entry */
202 p++;
203 *p++ = xdr_one; /* bitmap length */
204 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
205 *p++ = htonl(8); /* attribute buffer length */
206 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
209 *p++ = xdr_one; /* next */
210 *p++ = xdr_zero; /* cookie, first word */
211 *p++ = xdr_two; /* cookie, second word */
212 *p++ = xdr_two; /* entry len */
213 memcpy(p, "..\0\0", 4); /* entry */
214 p++;
215 *p++ = xdr_one; /* bitmap length */
216 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
217 *p++ = htonl(8); /* attribute buffer length */
218 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
220 readdir->pgbase = (char *)p - (char *)start;
221 readdir->count -= readdir->pgbase;
222 kunmap_atomic(start, KM_USER0);
225 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
227 int res;
229 might_sleep();
231 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
232 nfs_wait_bit_killable, TASK_KILLABLE);
233 return res;
236 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
238 int res = 0;
240 might_sleep();
242 if (*timeout <= 0)
243 *timeout = NFS4_POLL_RETRY_MIN;
244 if (*timeout > NFS4_POLL_RETRY_MAX)
245 *timeout = NFS4_POLL_RETRY_MAX;
246 schedule_timeout_killable(*timeout);
247 if (fatal_signal_pending(current))
248 res = -ERESTARTSYS;
249 *timeout <<= 1;
250 return res;
253 /* This is the error handling routine for processes that are allowed
254 * to sleep.
256 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
258 struct nfs_client *clp = server->nfs_client;
259 struct nfs4_state *state = exception->state;
260 int ret = errorcode;
262 exception->retry = 0;
263 switch(errorcode) {
264 case 0:
265 return 0;
266 case -NFS4ERR_ADMIN_REVOKED:
267 case -NFS4ERR_BAD_STATEID:
268 case -NFS4ERR_OPENMODE:
269 if (state == NULL)
270 break;
271 nfs4_schedule_stateid_recovery(server, state);
272 goto wait_on_recovery;
273 case -NFS4ERR_EXPIRED:
274 if (state != NULL)
275 nfs4_schedule_stateid_recovery(server, state);
276 case -NFS4ERR_STALE_STATEID:
277 case -NFS4ERR_STALE_CLIENTID:
278 nfs4_schedule_lease_recovery(clp);
279 goto wait_on_recovery;
280 #if defined(CONFIG_NFS_V4_1)
281 case -NFS4ERR_BADSESSION:
282 case -NFS4ERR_BADSLOT:
283 case -NFS4ERR_BAD_HIGH_SLOT:
284 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
285 case -NFS4ERR_DEADSESSION:
286 case -NFS4ERR_SEQ_FALSE_RETRY:
287 case -NFS4ERR_SEQ_MISORDERED:
288 dprintk("%s ERROR: %d Reset session\n", __func__,
289 errorcode);
290 nfs4_schedule_session_recovery(clp->cl_session);
291 exception->retry = 1;
292 break;
293 #endif /* defined(CONFIG_NFS_V4_1) */
294 case -NFS4ERR_FILE_OPEN:
295 if (exception->timeout > HZ) {
296 /* We have retried a decent amount, time to
297 * fail
299 ret = -EBUSY;
300 break;
302 case -NFS4ERR_GRACE:
303 case -NFS4ERR_DELAY:
304 case -EKEYEXPIRED:
305 ret = nfs4_delay(server->client, &exception->timeout);
306 if (ret != 0)
307 break;
308 case -NFS4ERR_RETRY_UNCACHED_REP:
309 case -NFS4ERR_OLD_STATEID:
310 exception->retry = 1;
311 break;
312 case -NFS4ERR_BADOWNER:
313 /* The following works around a Linux server bug! */
314 case -NFS4ERR_BADNAME:
315 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
316 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
317 exception->retry = 1;
318 printk(KERN_WARNING "NFS: v4 server %s "
319 "does not accept raw "
320 "uid/gids. "
321 "Reenabling the idmapper.\n",
322 server->nfs_client->cl_hostname);
325 /* We failed to handle the error */
326 return nfs4_map_errors(ret);
327 wait_on_recovery:
328 ret = nfs4_wait_clnt_recover(clp);
329 if (ret == 0)
330 exception->retry = 1;
331 return ret;
335 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
337 spin_lock(&clp->cl_lock);
338 if (time_before(clp->cl_last_renewal,timestamp))
339 clp->cl_last_renewal = timestamp;
340 spin_unlock(&clp->cl_lock);
343 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
345 do_renew_lease(server->nfs_client, timestamp);
348 #if defined(CONFIG_NFS_V4_1)
351 * nfs4_free_slot - free a slot and efficiently update slot table.
353 * freeing a slot is trivially done by clearing its respective bit
354 * in the bitmap.
355 * If the freed slotid equals highest_used_slotid we want to update it
356 * so that the server would be able to size down the slot table if needed,
357 * otherwise we know that the highest_used_slotid is still in use.
358 * When updating highest_used_slotid there may be "holes" in the bitmap
359 * so we need to scan down from highest_used_slotid to 0 looking for the now
360 * highest slotid in use.
361 * If none found, highest_used_slotid is set to -1.
363 * Must be called while holding tbl->slot_tbl_lock
365 static void
366 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
368 int free_slotid = free_slot - tbl->slots;
369 int slotid = free_slotid;
371 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
372 /* clear used bit in bitmap */
373 __clear_bit(slotid, tbl->used_slots);
375 /* update highest_used_slotid when it is freed */
376 if (slotid == tbl->highest_used_slotid) {
377 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
378 if (slotid < tbl->max_slots)
379 tbl->highest_used_slotid = slotid;
380 else
381 tbl->highest_used_slotid = -1;
383 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
384 free_slotid, tbl->highest_used_slotid);
388 * Signal state manager thread if session fore channel is drained
390 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
392 struct rpc_task *task;
394 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
395 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
396 if (task)
397 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
398 return;
401 if (ses->fc_slot_table.highest_used_slotid != -1)
402 return;
404 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
405 complete(&ses->fc_slot_table.complete);
409 * Signal state manager thread if session back channel is drained
411 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
414 ses->bc_slot_table.highest_used_slotid != -1)
415 return;
416 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
417 complete(&ses->bc_slot_table.complete);
420 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
422 struct nfs4_slot_table *tbl;
424 tbl = &res->sr_session->fc_slot_table;
425 if (!res->sr_slot) {
426 /* just wake up the next guy waiting since
427 * we may have not consumed a slot after all */
428 dprintk("%s: No slot\n", __func__);
429 return;
432 spin_lock(&tbl->slot_tbl_lock);
433 nfs4_free_slot(tbl, res->sr_slot);
434 nfs4_check_drain_fc_complete(res->sr_session);
435 spin_unlock(&tbl->slot_tbl_lock);
436 res->sr_slot = NULL;
439 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
441 unsigned long timestamp;
442 struct nfs_client *clp;
445 * sr_status remains 1 if an RPC level error occurred. The server
446 * may or may not have processed the sequence operation..
447 * Proceed as if the server received and processed the sequence
448 * operation.
450 if (res->sr_status == 1)
451 res->sr_status = NFS_OK;
453 /* don't increment the sequence number if the task wasn't sent */
454 if (!RPC_WAS_SENT(task))
455 goto out;
457 /* Check the SEQUENCE operation status */
458 switch (res->sr_status) {
459 case 0:
460 /* Update the slot's sequence and clientid lease timer */
461 ++res->sr_slot->seq_nr;
462 timestamp = res->sr_renewal_time;
463 clp = res->sr_session->clp;
464 do_renew_lease(clp, timestamp);
465 /* Check sequence flags */
466 if (res->sr_status_flags != 0)
467 nfs4_schedule_lease_recovery(clp);
468 break;
469 case -NFS4ERR_DELAY:
470 /* The server detected a resend of the RPC call and
471 * returned NFS4ERR_DELAY as per Section 2.10.6.2
472 * of RFC5661.
474 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
475 __func__,
476 res->sr_slot - res->sr_session->fc_slot_table.slots,
477 res->sr_slot->seq_nr);
478 goto out_retry;
479 default:
480 /* Just update the slot sequence no. */
481 ++res->sr_slot->seq_nr;
483 out:
484 /* The session may be reset by one of the error handlers. */
485 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
486 nfs41_sequence_free_slot(res);
487 return 1;
488 out_retry:
489 if (!rpc_restart_call(task))
490 goto out;
491 rpc_delay(task, NFS4_POLL_RETRY_MAX);
492 return 0;
495 static int nfs4_sequence_done(struct rpc_task *task,
496 struct nfs4_sequence_res *res)
498 if (res->sr_session == NULL)
499 return 1;
500 return nfs41_sequence_done(task, res);
504 * nfs4_find_slot - efficiently look for a free slot
506 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
507 * If found, we mark the slot as used, update the highest_used_slotid,
508 * and respectively set up the sequence operation args.
509 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
511 * Note: must be called with under the slot_tbl_lock.
513 static u8
514 nfs4_find_slot(struct nfs4_slot_table *tbl)
516 int slotid;
517 u8 ret_id = NFS4_MAX_SLOT_TABLE;
518 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
520 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
521 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
522 tbl->max_slots);
523 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
524 if (slotid >= tbl->max_slots)
525 goto out;
526 __set_bit(slotid, tbl->used_slots);
527 if (slotid > tbl->highest_used_slotid)
528 tbl->highest_used_slotid = slotid;
529 ret_id = slotid;
530 out:
531 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
532 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
533 return ret_id;
536 int nfs41_setup_sequence(struct nfs4_session *session,
537 struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res,
539 int cache_reply,
540 struct rpc_task *task)
542 struct nfs4_slot *slot;
543 struct nfs4_slot_table *tbl;
544 u8 slotid;
546 dprintk("--> %s\n", __func__);
547 /* slot already allocated? */
548 if (res->sr_slot != NULL)
549 return 0;
551 tbl = &session->fc_slot_table;
553 spin_lock(&tbl->slot_tbl_lock);
554 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
555 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
557 * The state manager will wait until the slot table is empty.
558 * Schedule the reset thread
560 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
561 spin_unlock(&tbl->slot_tbl_lock);
562 dprintk("%s Schedule Session Reset\n", __func__);
563 return -EAGAIN;
566 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
567 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
568 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
569 spin_unlock(&tbl->slot_tbl_lock);
570 dprintk("%s enforce FIFO order\n", __func__);
571 return -EAGAIN;
574 slotid = nfs4_find_slot(tbl);
575 if (slotid == NFS4_MAX_SLOT_TABLE) {
576 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
577 spin_unlock(&tbl->slot_tbl_lock);
578 dprintk("<-- %s: no free slots\n", __func__);
579 return -EAGAIN;
581 spin_unlock(&tbl->slot_tbl_lock);
583 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
584 slot = tbl->slots + slotid;
585 args->sa_session = session;
586 args->sa_slotid = slotid;
587 args->sa_cache_this = cache_reply;
589 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
591 res->sr_session = session;
592 res->sr_slot = slot;
593 res->sr_renewal_time = jiffies;
594 res->sr_status_flags = 0;
596 * sr_status is only set in decode_sequence, and so will remain
597 * set to 1 if an rpc level failure occurs.
599 res->sr_status = 1;
600 return 0;
602 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
604 int nfs4_setup_sequence(const struct nfs_server *server,
605 struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res,
607 int cache_reply,
608 struct rpc_task *task)
610 struct nfs4_session *session = nfs4_get_session(server);
611 int ret = 0;
613 if (session == NULL) {
614 args->sa_session = NULL;
615 res->sr_session = NULL;
616 goto out;
619 dprintk("--> %s clp %p session %p sr_slot %td\n",
620 __func__, session->clp, session, res->sr_slot ?
621 res->sr_slot - session->fc_slot_table.slots : -1);
623 ret = nfs41_setup_sequence(session, args, res, cache_reply,
624 task);
625 out:
626 dprintk("<-- %s status=%d\n", __func__, ret);
627 return ret;
630 struct nfs41_call_sync_data {
631 const struct nfs_server *seq_server;
632 struct nfs4_sequence_args *seq_args;
633 struct nfs4_sequence_res *seq_res;
634 int cache_reply;
637 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
639 struct nfs41_call_sync_data *data = calldata;
641 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
643 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
644 data->seq_res, data->cache_reply, task))
645 return;
646 rpc_call_start(task);
649 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
651 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
652 nfs41_call_sync_prepare(task, calldata);
655 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
657 struct nfs41_call_sync_data *data = calldata;
659 nfs41_sequence_done(task, data->seq_res);
662 struct rpc_call_ops nfs41_call_sync_ops = {
663 .rpc_call_prepare = nfs41_call_sync_prepare,
664 .rpc_call_done = nfs41_call_sync_done,
667 struct rpc_call_ops nfs41_call_priv_sync_ops = {
668 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
669 .rpc_call_done = nfs41_call_sync_done,
672 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
673 struct nfs_server *server,
674 struct rpc_message *msg,
675 struct nfs4_sequence_args *args,
676 struct nfs4_sequence_res *res,
677 int cache_reply,
678 int privileged)
680 int ret;
681 struct rpc_task *task;
682 struct nfs41_call_sync_data data = {
683 .seq_server = server,
684 .seq_args = args,
685 .seq_res = res,
686 .cache_reply = cache_reply,
688 struct rpc_task_setup task_setup = {
689 .rpc_client = clnt,
690 .rpc_message = msg,
691 .callback_ops = &nfs41_call_sync_ops,
692 .callback_data = &data
695 res->sr_slot = NULL;
696 if (privileged)
697 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
698 task = rpc_run_task(&task_setup);
699 if (IS_ERR(task))
700 ret = PTR_ERR(task);
701 else {
702 ret = task->tk_status;
703 rpc_put_task(task);
705 return ret;
708 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
709 struct nfs_server *server,
710 struct rpc_message *msg,
711 struct nfs4_sequence_args *args,
712 struct nfs4_sequence_res *res,
713 int cache_reply)
715 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
718 #else
719 static int nfs4_sequence_done(struct rpc_task *task,
720 struct nfs4_sequence_res *res)
722 return 1;
724 #endif /* CONFIG_NFS_V4_1 */
726 int _nfs4_call_sync(struct rpc_clnt *clnt,
727 struct nfs_server *server,
728 struct rpc_message *msg,
729 struct nfs4_sequence_args *args,
730 struct nfs4_sequence_res *res,
731 int cache_reply)
733 args->sa_session = res->sr_session = NULL;
734 return rpc_call_sync(clnt, msg, 0);
737 static inline
738 int nfs4_call_sync(struct rpc_clnt *clnt,
739 struct nfs_server *server,
740 struct rpc_message *msg,
741 struct nfs4_sequence_args *args,
742 struct nfs4_sequence_res *res,
743 int cache_reply)
745 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
746 args, res, cache_reply);
749 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
751 struct nfs_inode *nfsi = NFS_I(dir);
753 spin_lock(&dir->i_lock);
754 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
755 if (!cinfo->atomic || cinfo->before != dir->i_version)
756 nfs_force_lookup_revalidate(dir);
757 dir->i_version = cinfo->after;
758 spin_unlock(&dir->i_lock);
761 struct nfs4_opendata {
762 struct kref kref;
763 struct nfs_openargs o_arg;
764 struct nfs_openres o_res;
765 struct nfs_open_confirmargs c_arg;
766 struct nfs_open_confirmres c_res;
767 struct nfs_fattr f_attr;
768 struct nfs_fattr dir_attr;
769 struct dentry *dir;
770 struct dentry *dentry;
771 struct nfs4_state_owner *owner;
772 struct nfs4_state *state;
773 struct iattr attrs;
774 unsigned long timestamp;
775 unsigned int rpc_done : 1;
776 int rpc_status;
777 int cancelled;
781 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
783 p->o_res.f_attr = &p->f_attr;
784 p->o_res.dir_attr = &p->dir_attr;
785 p->o_res.seqid = p->o_arg.seqid;
786 p->c_res.seqid = p->c_arg.seqid;
787 p->o_res.server = p->o_arg.server;
788 nfs_fattr_init(&p->f_attr);
789 nfs_fattr_init(&p->dir_attr);
792 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
793 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
794 const struct iattr *attrs,
795 gfp_t gfp_mask)
797 struct dentry *parent = dget_parent(dentry);
798 struct inode *dir = parent->d_inode;
799 struct nfs_server *server = NFS_SERVER(dir);
800 struct nfs4_opendata *p;
802 p = kzalloc(sizeof(*p), gfp_mask);
803 if (p == NULL)
804 goto err;
805 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
806 if (p->o_arg.seqid == NULL)
807 goto err_free;
808 nfs_sb_active(dentry->d_sb);
809 p->dentry = dget(dentry);
810 p->dir = parent;
811 p->owner = sp;
812 atomic_inc(&sp->so_count);
813 p->o_arg.fh = NFS_FH(dir);
814 p->o_arg.open_flags = flags;
815 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
816 p->o_arg.clientid = server->nfs_client->cl_clientid;
817 p->o_arg.id = sp->so_owner_id.id;
818 p->o_arg.name = &dentry->d_name;
819 p->o_arg.server = server;
820 p->o_arg.bitmask = server->attr_bitmask;
821 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
822 if (flags & O_CREAT) {
823 u32 *s;
825 p->o_arg.u.attrs = &p->attrs;
826 memcpy(&p->attrs, attrs, sizeof(p->attrs));
827 s = (u32 *) p->o_arg.u.verifier.data;
828 s[0] = jiffies;
829 s[1] = current->pid;
831 p->c_arg.fh = &p->o_res.fh;
832 p->c_arg.stateid = &p->o_res.stateid;
833 p->c_arg.seqid = p->o_arg.seqid;
834 nfs4_init_opendata_res(p);
835 kref_init(&p->kref);
836 return p;
837 err_free:
838 kfree(p);
839 err:
840 dput(parent);
841 return NULL;
844 static void nfs4_opendata_free(struct kref *kref)
846 struct nfs4_opendata *p = container_of(kref,
847 struct nfs4_opendata, kref);
848 struct super_block *sb = p->dentry->d_sb;
850 nfs_free_seqid(p->o_arg.seqid);
851 if (p->state != NULL)
852 nfs4_put_open_state(p->state);
853 nfs4_put_state_owner(p->owner);
854 dput(p->dir);
855 dput(p->dentry);
856 nfs_sb_deactive(sb);
857 kfree(p);
860 static void nfs4_opendata_put(struct nfs4_opendata *p)
862 if (p != NULL)
863 kref_put(&p->kref, nfs4_opendata_free);
866 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
868 int ret;
870 ret = rpc_wait_for_completion_task(task);
871 return ret;
874 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
876 int ret = 0;
878 if (open_mode & O_EXCL)
879 goto out;
880 switch (mode & (FMODE_READ|FMODE_WRITE)) {
881 case FMODE_READ:
882 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
883 && state->n_rdonly != 0;
884 break;
885 case FMODE_WRITE:
886 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
887 && state->n_wronly != 0;
888 break;
889 case FMODE_READ|FMODE_WRITE:
890 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
891 && state->n_rdwr != 0;
893 out:
894 return ret;
897 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
899 if (delegation == NULL)
900 return 0;
901 if ((delegation->type & fmode) != fmode)
902 return 0;
903 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
904 return 0;
905 nfs_mark_delegation_referenced(delegation);
906 return 1;
909 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
911 switch (fmode) {
912 case FMODE_WRITE:
913 state->n_wronly++;
914 break;
915 case FMODE_READ:
916 state->n_rdonly++;
917 break;
918 case FMODE_READ|FMODE_WRITE:
919 state->n_rdwr++;
921 nfs4_state_set_mode_locked(state, state->state | fmode);
924 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
926 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
927 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
928 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
929 switch (fmode) {
930 case FMODE_READ:
931 set_bit(NFS_O_RDONLY_STATE, &state->flags);
932 break;
933 case FMODE_WRITE:
934 set_bit(NFS_O_WRONLY_STATE, &state->flags);
935 break;
936 case FMODE_READ|FMODE_WRITE:
937 set_bit(NFS_O_RDWR_STATE, &state->flags);
941 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
943 write_seqlock(&state->seqlock);
944 nfs_set_open_stateid_locked(state, stateid, fmode);
945 write_sequnlock(&state->seqlock);
948 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
951 * Protect the call to nfs4_state_set_mode_locked and
952 * serialise the stateid update
954 write_seqlock(&state->seqlock);
955 if (deleg_stateid != NULL) {
956 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
957 set_bit(NFS_DELEGATED_STATE, &state->flags);
959 if (open_stateid != NULL)
960 nfs_set_open_stateid_locked(state, open_stateid, fmode);
961 write_sequnlock(&state->seqlock);
962 spin_lock(&state->owner->so_lock);
963 update_open_stateflags(state, fmode);
964 spin_unlock(&state->owner->so_lock);
967 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
969 struct nfs_inode *nfsi = NFS_I(state->inode);
970 struct nfs_delegation *deleg_cur;
971 int ret = 0;
973 fmode &= (FMODE_READ|FMODE_WRITE);
975 rcu_read_lock();
976 deleg_cur = rcu_dereference(nfsi->delegation);
977 if (deleg_cur == NULL)
978 goto no_delegation;
980 spin_lock(&deleg_cur->lock);
981 if (nfsi->delegation != deleg_cur ||
982 (deleg_cur->type & fmode) != fmode)
983 goto no_delegation_unlock;
985 if (delegation == NULL)
986 delegation = &deleg_cur->stateid;
987 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
988 goto no_delegation_unlock;
990 nfs_mark_delegation_referenced(deleg_cur);
991 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
992 ret = 1;
993 no_delegation_unlock:
994 spin_unlock(&deleg_cur->lock);
995 no_delegation:
996 rcu_read_unlock();
998 if (!ret && open_stateid != NULL) {
999 __update_open_stateid(state, open_stateid, NULL, fmode);
1000 ret = 1;
1003 return ret;
1007 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1009 struct nfs_delegation *delegation;
1011 rcu_read_lock();
1012 delegation = rcu_dereference(NFS_I(inode)->delegation);
1013 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1014 rcu_read_unlock();
1015 return;
1017 rcu_read_unlock();
1018 nfs_inode_return_delegation(inode);
1021 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1023 struct nfs4_state *state = opendata->state;
1024 struct nfs_inode *nfsi = NFS_I(state->inode);
1025 struct nfs_delegation *delegation;
1026 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1027 fmode_t fmode = opendata->o_arg.fmode;
1028 nfs4_stateid stateid;
1029 int ret = -EAGAIN;
1031 for (;;) {
1032 if (can_open_cached(state, fmode, open_mode)) {
1033 spin_lock(&state->owner->so_lock);
1034 if (can_open_cached(state, fmode, open_mode)) {
1035 update_open_stateflags(state, fmode);
1036 spin_unlock(&state->owner->so_lock);
1037 goto out_return_state;
1039 spin_unlock(&state->owner->so_lock);
1041 rcu_read_lock();
1042 delegation = rcu_dereference(nfsi->delegation);
1043 if (!can_open_delegated(delegation, fmode)) {
1044 rcu_read_unlock();
1045 break;
1047 /* Save the delegation */
1048 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1049 rcu_read_unlock();
1050 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1051 if (ret != 0)
1052 goto out;
1053 ret = -EAGAIN;
1055 /* Try to update the stateid using the delegation */
1056 if (update_open_stateid(state, NULL, &stateid, fmode))
1057 goto out_return_state;
1059 out:
1060 return ERR_PTR(ret);
1061 out_return_state:
1062 atomic_inc(&state->count);
1063 return state;
1066 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1068 struct inode *inode;
1069 struct nfs4_state *state = NULL;
1070 struct nfs_delegation *delegation;
1071 int ret;
1073 if (!data->rpc_done) {
1074 state = nfs4_try_open_cached(data);
1075 goto out;
1078 ret = -EAGAIN;
1079 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1080 goto err;
1081 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1082 ret = PTR_ERR(inode);
1083 if (IS_ERR(inode))
1084 goto err;
1085 ret = -ENOMEM;
1086 state = nfs4_get_open_state(inode, data->owner);
1087 if (state == NULL)
1088 goto err_put_inode;
1089 if (data->o_res.delegation_type != 0) {
1090 int delegation_flags = 0;
1092 rcu_read_lock();
1093 delegation = rcu_dereference(NFS_I(inode)->delegation);
1094 if (delegation)
1095 delegation_flags = delegation->flags;
1096 rcu_read_unlock();
1097 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1098 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1099 "returning a delegation for "
1100 "OPEN(CLAIM_DELEGATE_CUR)\n",
1101 NFS_CLIENT(inode)->cl_server);
1102 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1103 nfs_inode_set_delegation(state->inode,
1104 data->owner->so_cred,
1105 &data->o_res);
1106 else
1107 nfs_inode_reclaim_delegation(state->inode,
1108 data->owner->so_cred,
1109 &data->o_res);
1112 update_open_stateid(state, &data->o_res.stateid, NULL,
1113 data->o_arg.fmode);
1114 iput(inode);
1115 out:
1116 return state;
1117 err_put_inode:
1118 iput(inode);
1119 err:
1120 return ERR_PTR(ret);
1123 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1125 struct nfs_inode *nfsi = NFS_I(state->inode);
1126 struct nfs_open_context *ctx;
1128 spin_lock(&state->inode->i_lock);
1129 list_for_each_entry(ctx, &nfsi->open_files, list) {
1130 if (ctx->state != state)
1131 continue;
1132 get_nfs_open_context(ctx);
1133 spin_unlock(&state->inode->i_lock);
1134 return ctx;
1136 spin_unlock(&state->inode->i_lock);
1137 return ERR_PTR(-ENOENT);
1140 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1142 struct nfs4_opendata *opendata;
1144 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1145 if (opendata == NULL)
1146 return ERR_PTR(-ENOMEM);
1147 opendata->state = state;
1148 atomic_inc(&state->count);
1149 return opendata;
1152 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1154 struct nfs4_state *newstate;
1155 int ret;
1157 opendata->o_arg.open_flags = 0;
1158 opendata->o_arg.fmode = fmode;
1159 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1160 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1161 nfs4_init_opendata_res(opendata);
1162 ret = _nfs4_recover_proc_open(opendata);
1163 if (ret != 0)
1164 return ret;
1165 newstate = nfs4_opendata_to_nfs4_state(opendata);
1166 if (IS_ERR(newstate))
1167 return PTR_ERR(newstate);
1168 nfs4_close_state(newstate, fmode);
1169 *res = newstate;
1170 return 0;
1173 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1175 struct nfs4_state *newstate;
1176 int ret;
1178 /* memory barrier prior to reading state->n_* */
1179 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1180 smp_rmb();
1181 if (state->n_rdwr != 0) {
1182 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1183 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1184 if (ret != 0)
1185 return ret;
1186 if (newstate != state)
1187 return -ESTALE;
1189 if (state->n_wronly != 0) {
1190 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1191 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1192 if (ret != 0)
1193 return ret;
1194 if (newstate != state)
1195 return -ESTALE;
1197 if (state->n_rdonly != 0) {
1198 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1199 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1200 if (ret != 0)
1201 return ret;
1202 if (newstate != state)
1203 return -ESTALE;
1206 * We may have performed cached opens for all three recoveries.
1207 * Check if we need to update the current stateid.
1209 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1210 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1211 write_seqlock(&state->seqlock);
1212 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1213 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1214 write_sequnlock(&state->seqlock);
1216 return 0;
1220 * OPEN_RECLAIM:
1221 * reclaim state on the server after a reboot.
1223 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1225 struct nfs_delegation *delegation;
1226 struct nfs4_opendata *opendata;
1227 fmode_t delegation_type = 0;
1228 int status;
1230 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1231 if (IS_ERR(opendata))
1232 return PTR_ERR(opendata);
1233 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1234 opendata->o_arg.fh = NFS_FH(state->inode);
1235 rcu_read_lock();
1236 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1237 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1238 delegation_type = delegation->type;
1239 rcu_read_unlock();
1240 opendata->o_arg.u.delegation_type = delegation_type;
1241 status = nfs4_open_recover(opendata, state);
1242 nfs4_opendata_put(opendata);
1243 return status;
1246 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1248 struct nfs_server *server = NFS_SERVER(state->inode);
1249 struct nfs4_exception exception = { };
1250 int err;
1251 do {
1252 err = _nfs4_do_open_reclaim(ctx, state);
1253 if (err != -NFS4ERR_DELAY)
1254 break;
1255 nfs4_handle_exception(server, err, &exception);
1256 } while (exception.retry);
1257 return err;
1260 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1262 struct nfs_open_context *ctx;
1263 int ret;
1265 ctx = nfs4_state_find_open_context(state);
1266 if (IS_ERR(ctx))
1267 return PTR_ERR(ctx);
1268 ret = nfs4_do_open_reclaim(ctx, state);
1269 put_nfs_open_context(ctx);
1270 return ret;
1273 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1275 struct nfs4_opendata *opendata;
1276 int ret;
1278 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1279 if (IS_ERR(opendata))
1280 return PTR_ERR(opendata);
1281 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1282 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1283 sizeof(opendata->o_arg.u.delegation.data));
1284 ret = nfs4_open_recover(opendata, state);
1285 nfs4_opendata_put(opendata);
1286 return ret;
1289 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1291 struct nfs4_exception exception = { };
1292 struct nfs_server *server = NFS_SERVER(state->inode);
1293 int err;
1294 do {
1295 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1296 switch (err) {
1297 case 0:
1298 case -ENOENT:
1299 case -ESTALE:
1300 goto out;
1301 case -NFS4ERR_BADSESSION:
1302 case -NFS4ERR_BADSLOT:
1303 case -NFS4ERR_BAD_HIGH_SLOT:
1304 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1305 case -NFS4ERR_DEADSESSION:
1306 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1307 goto out;
1308 case -NFS4ERR_STALE_CLIENTID:
1309 case -NFS4ERR_STALE_STATEID:
1310 case -NFS4ERR_EXPIRED:
1311 /* Don't recall a delegation if it was lost */
1312 nfs4_schedule_lease_recovery(server->nfs_client);
1313 goto out;
1314 case -ERESTARTSYS:
1316 * The show must go on: exit, but mark the
1317 * stateid as needing recovery.
1319 case -NFS4ERR_ADMIN_REVOKED:
1320 case -NFS4ERR_BAD_STATEID:
1321 nfs4_schedule_stateid_recovery(server, state);
1322 case -EKEYEXPIRED:
1324 * User RPCSEC_GSS context has expired.
1325 * We cannot recover this stateid now, so
1326 * skip it and allow recovery thread to
1327 * proceed.
1329 case -ENOMEM:
1330 err = 0;
1331 goto out;
1333 err = nfs4_handle_exception(server, err, &exception);
1334 } while (exception.retry);
1335 out:
1336 return err;
1339 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1341 struct nfs4_opendata *data = calldata;
1343 data->rpc_status = task->tk_status;
1344 if (data->rpc_status == 0) {
1345 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1346 sizeof(data->o_res.stateid.data));
1347 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1348 renew_lease(data->o_res.server, data->timestamp);
1349 data->rpc_done = 1;
1353 static void nfs4_open_confirm_release(void *calldata)
1355 struct nfs4_opendata *data = calldata;
1356 struct nfs4_state *state = NULL;
1358 /* If this request hasn't been cancelled, do nothing */
1359 if (data->cancelled == 0)
1360 goto out_free;
1361 /* In case of error, no cleanup! */
1362 if (!data->rpc_done)
1363 goto out_free;
1364 state = nfs4_opendata_to_nfs4_state(data);
1365 if (!IS_ERR(state))
1366 nfs4_close_state(state, data->o_arg.fmode);
1367 out_free:
1368 nfs4_opendata_put(data);
1371 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1372 .rpc_call_done = nfs4_open_confirm_done,
1373 .rpc_release = nfs4_open_confirm_release,
1377 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1379 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1381 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1382 struct rpc_task *task;
1383 struct rpc_message msg = {
1384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1385 .rpc_argp = &data->c_arg,
1386 .rpc_resp = &data->c_res,
1387 .rpc_cred = data->owner->so_cred,
1389 struct rpc_task_setup task_setup_data = {
1390 .rpc_client = server->client,
1391 .rpc_message = &msg,
1392 .callback_ops = &nfs4_open_confirm_ops,
1393 .callback_data = data,
1394 .workqueue = nfsiod_workqueue,
1395 .flags = RPC_TASK_ASYNC,
1397 int status;
1399 kref_get(&data->kref);
1400 data->rpc_done = 0;
1401 data->rpc_status = 0;
1402 data->timestamp = jiffies;
1403 task = rpc_run_task(&task_setup_data);
1404 if (IS_ERR(task))
1405 return PTR_ERR(task);
1406 status = nfs4_wait_for_completion_rpc_task(task);
1407 if (status != 0) {
1408 data->cancelled = 1;
1409 smp_wmb();
1410 } else
1411 status = data->rpc_status;
1412 rpc_put_task(task);
1413 return status;
1416 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1418 struct nfs4_opendata *data = calldata;
1419 struct nfs4_state_owner *sp = data->owner;
1421 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1422 return;
1424 * Check if we still need to send an OPEN call, or if we can use
1425 * a delegation instead.
1427 if (data->state != NULL) {
1428 struct nfs_delegation *delegation;
1430 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1431 goto out_no_action;
1432 rcu_read_lock();
1433 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1434 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1435 can_open_delegated(delegation, data->o_arg.fmode))
1436 goto unlock_no_action;
1437 rcu_read_unlock();
1439 /* Update sequence id. */
1440 data->o_arg.id = sp->so_owner_id.id;
1441 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1442 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1443 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1444 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1446 data->timestamp = jiffies;
1447 if (nfs4_setup_sequence(data->o_arg.server,
1448 &data->o_arg.seq_args,
1449 &data->o_res.seq_res, 1, task))
1450 return;
1451 rpc_call_start(task);
1452 return;
1453 unlock_no_action:
1454 rcu_read_unlock();
1455 out_no_action:
1456 task->tk_action = NULL;
1460 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1462 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1463 nfs4_open_prepare(task, calldata);
1466 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1468 struct nfs4_opendata *data = calldata;
1470 data->rpc_status = task->tk_status;
1472 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1473 return;
1475 if (task->tk_status == 0) {
1476 switch (data->o_res.f_attr->mode & S_IFMT) {
1477 case S_IFREG:
1478 break;
1479 case S_IFLNK:
1480 data->rpc_status = -ELOOP;
1481 break;
1482 case S_IFDIR:
1483 data->rpc_status = -EISDIR;
1484 break;
1485 default:
1486 data->rpc_status = -ENOTDIR;
1488 renew_lease(data->o_res.server, data->timestamp);
1489 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1490 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1492 data->rpc_done = 1;
1495 static void nfs4_open_release(void *calldata)
1497 struct nfs4_opendata *data = calldata;
1498 struct nfs4_state *state = NULL;
1500 /* If this request hasn't been cancelled, do nothing */
1501 if (data->cancelled == 0)
1502 goto out_free;
1503 /* In case of error, no cleanup! */
1504 if (data->rpc_status != 0 || !data->rpc_done)
1505 goto out_free;
1506 /* In case we need an open_confirm, no cleanup! */
1507 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1508 goto out_free;
1509 state = nfs4_opendata_to_nfs4_state(data);
1510 if (!IS_ERR(state))
1511 nfs4_close_state(state, data->o_arg.fmode);
1512 out_free:
1513 nfs4_opendata_put(data);
1516 static const struct rpc_call_ops nfs4_open_ops = {
1517 .rpc_call_prepare = nfs4_open_prepare,
1518 .rpc_call_done = nfs4_open_done,
1519 .rpc_release = nfs4_open_release,
1522 static const struct rpc_call_ops nfs4_recover_open_ops = {
1523 .rpc_call_prepare = nfs4_recover_open_prepare,
1524 .rpc_call_done = nfs4_open_done,
1525 .rpc_release = nfs4_open_release,
1528 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1530 struct inode *dir = data->dir->d_inode;
1531 struct nfs_server *server = NFS_SERVER(dir);
1532 struct nfs_openargs *o_arg = &data->o_arg;
1533 struct nfs_openres *o_res = &data->o_res;
1534 struct rpc_task *task;
1535 struct rpc_message msg = {
1536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1537 .rpc_argp = o_arg,
1538 .rpc_resp = o_res,
1539 .rpc_cred = data->owner->so_cred,
1541 struct rpc_task_setup task_setup_data = {
1542 .rpc_client = server->client,
1543 .rpc_message = &msg,
1544 .callback_ops = &nfs4_open_ops,
1545 .callback_data = data,
1546 .workqueue = nfsiod_workqueue,
1547 .flags = RPC_TASK_ASYNC,
1549 int status;
1551 kref_get(&data->kref);
1552 data->rpc_done = 0;
1553 data->rpc_status = 0;
1554 data->cancelled = 0;
1555 if (isrecover)
1556 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1557 task = rpc_run_task(&task_setup_data);
1558 if (IS_ERR(task))
1559 return PTR_ERR(task);
1560 status = nfs4_wait_for_completion_rpc_task(task);
1561 if (status != 0) {
1562 data->cancelled = 1;
1563 smp_wmb();
1564 } else
1565 status = data->rpc_status;
1566 rpc_put_task(task);
1568 return status;
1571 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1573 struct inode *dir = data->dir->d_inode;
1574 struct nfs_openres *o_res = &data->o_res;
1575 int status;
1577 status = nfs4_run_open_task(data, 1);
1578 if (status != 0 || !data->rpc_done)
1579 return status;
1581 nfs_refresh_inode(dir, o_res->dir_attr);
1583 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1584 status = _nfs4_proc_open_confirm(data);
1585 if (status != 0)
1586 return status;
1589 return status;
1593 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1595 static int _nfs4_proc_open(struct nfs4_opendata *data)
1597 struct inode *dir = data->dir->d_inode;
1598 struct nfs_server *server = NFS_SERVER(dir);
1599 struct nfs_openargs *o_arg = &data->o_arg;
1600 struct nfs_openres *o_res = &data->o_res;
1601 int status;
1603 status = nfs4_run_open_task(data, 0);
1604 if (!data->rpc_done)
1605 return status;
1606 if (status != 0) {
1607 if (status == -NFS4ERR_BADNAME &&
1608 !(o_arg->open_flags & O_CREAT))
1609 return -ENOENT;
1610 return status;
1613 if (o_arg->open_flags & O_CREAT) {
1614 update_changeattr(dir, &o_res->cinfo);
1615 nfs_post_op_update_inode(dir, o_res->dir_attr);
1616 } else
1617 nfs_refresh_inode(dir, o_res->dir_attr);
1618 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1619 server->caps &= ~NFS_CAP_POSIX_LOCK;
1620 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1621 status = _nfs4_proc_open_confirm(data);
1622 if (status != 0)
1623 return status;
1625 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1626 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1627 return 0;
1630 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1632 unsigned int loop;
1633 int ret;
1635 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1636 ret = nfs4_wait_clnt_recover(clp);
1637 if (ret != 0)
1638 break;
1639 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1640 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1641 break;
1642 nfs4_schedule_state_manager(clp);
1643 ret = -EIO;
1645 return ret;
1648 static int nfs4_recover_expired_lease(struct nfs_server *server)
1650 return nfs4_client_recover_expired_lease(server->nfs_client);
1654 * OPEN_EXPIRED:
1655 * reclaim state on the server after a network partition.
1656 * Assumes caller holds the appropriate lock
1658 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1660 struct nfs4_opendata *opendata;
1661 int ret;
1663 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1664 if (IS_ERR(opendata))
1665 return PTR_ERR(opendata);
1666 ret = nfs4_open_recover(opendata, state);
1667 if (ret == -ESTALE)
1668 d_drop(ctx->dentry);
1669 nfs4_opendata_put(opendata);
1670 return ret;
1673 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1675 struct nfs_server *server = NFS_SERVER(state->inode);
1676 struct nfs4_exception exception = { };
1677 int err;
1679 do {
1680 err = _nfs4_open_expired(ctx, state);
1681 switch (err) {
1682 default:
1683 goto out;
1684 case -NFS4ERR_GRACE:
1685 case -NFS4ERR_DELAY:
1686 nfs4_handle_exception(server, err, &exception);
1687 err = 0;
1689 } while (exception.retry);
1690 out:
1691 return err;
1694 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1696 struct nfs_open_context *ctx;
1697 int ret;
1699 ctx = nfs4_state_find_open_context(state);
1700 if (IS_ERR(ctx))
1701 return PTR_ERR(ctx);
1702 ret = nfs4_do_open_expired(ctx, state);
1703 put_nfs_open_context(ctx);
1704 return ret;
1707 #if defined(CONFIG_NFS_V4_1)
1708 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1710 int status;
1711 struct nfs_server *server = NFS_SERVER(state->inode);
1713 status = nfs41_test_stateid(server, state);
1714 if (status == NFS_OK)
1715 return 0;
1716 nfs41_free_stateid(server, state);
1717 return nfs4_open_expired(sp, state);
1719 #endif
1722 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1723 * fields corresponding to attributes that were used to store the verifier.
1724 * Make sure we clobber those fields in the later setattr call
1726 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1728 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1729 !(sattr->ia_valid & ATTR_ATIME_SET))
1730 sattr->ia_valid |= ATTR_ATIME;
1732 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1733 !(sattr->ia_valid & ATTR_MTIME_SET))
1734 sattr->ia_valid |= ATTR_MTIME;
1738 * Returns a referenced nfs4_state
1740 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1742 struct nfs4_state_owner *sp;
1743 struct nfs4_state *state = NULL;
1744 struct nfs_server *server = NFS_SERVER(dir);
1745 struct nfs4_opendata *opendata;
1746 int status;
1748 /* Protect against reboot recovery conflicts */
1749 status = -ENOMEM;
1750 if (!(sp = nfs4_get_state_owner(server, cred))) {
1751 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1752 goto out_err;
1754 status = nfs4_recover_expired_lease(server);
1755 if (status != 0)
1756 goto err_put_state_owner;
1757 if (dentry->d_inode != NULL)
1758 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1759 status = -ENOMEM;
1760 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1761 if (opendata == NULL)
1762 goto err_put_state_owner;
1764 if (dentry->d_inode != NULL)
1765 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1767 status = _nfs4_proc_open(opendata);
1768 if (status != 0)
1769 goto err_opendata_put;
1771 state = nfs4_opendata_to_nfs4_state(opendata);
1772 status = PTR_ERR(state);
1773 if (IS_ERR(state))
1774 goto err_opendata_put;
1775 if (server->caps & NFS_CAP_POSIX_LOCK)
1776 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1778 if (opendata->o_arg.open_flags & O_EXCL) {
1779 nfs4_exclusive_attrset(opendata, sattr);
1781 nfs_fattr_init(opendata->o_res.f_attr);
1782 status = nfs4_do_setattr(state->inode, cred,
1783 opendata->o_res.f_attr, sattr,
1784 state);
1785 if (status == 0)
1786 nfs_setattr_update_inode(state->inode, sattr);
1787 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1789 nfs4_opendata_put(opendata);
1790 nfs4_put_state_owner(sp);
1791 *res = state;
1792 return 0;
1793 err_opendata_put:
1794 nfs4_opendata_put(opendata);
1795 err_put_state_owner:
1796 nfs4_put_state_owner(sp);
1797 out_err:
1798 *res = NULL;
1799 return status;
1803 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1805 struct nfs4_exception exception = { };
1806 struct nfs4_state *res;
1807 int status;
1809 do {
1810 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1811 if (status == 0)
1812 break;
1813 /* NOTE: BAD_SEQID means the server and client disagree about the
1814 * book-keeping w.r.t. state-changing operations
1815 * (OPEN/CLOSE/LOCK/LOCKU...)
1816 * It is actually a sign of a bug on the client or on the server.
1818 * If we receive a BAD_SEQID error in the particular case of
1819 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1820 * have unhashed the old state_owner for us, and that we can
1821 * therefore safely retry using a new one. We should still warn
1822 * the user though...
1824 if (status == -NFS4ERR_BAD_SEQID) {
1825 printk(KERN_WARNING "NFS: v4 server %s "
1826 " returned a bad sequence-id error!\n",
1827 NFS_SERVER(dir)->nfs_client->cl_hostname);
1828 exception.retry = 1;
1829 continue;
1832 * BAD_STATEID on OPEN means that the server cancelled our
1833 * state before it received the OPEN_CONFIRM.
1834 * Recover by retrying the request as per the discussion
1835 * on Page 181 of RFC3530.
1837 if (status == -NFS4ERR_BAD_STATEID) {
1838 exception.retry = 1;
1839 continue;
1841 if (status == -EAGAIN) {
1842 /* We must have found a delegation */
1843 exception.retry = 1;
1844 continue;
1846 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1847 status, &exception));
1848 } while (exception.retry);
1849 return res;
1852 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1853 struct nfs_fattr *fattr, struct iattr *sattr,
1854 struct nfs4_state *state)
1856 struct nfs_server *server = NFS_SERVER(inode);
1857 struct nfs_setattrargs arg = {
1858 .fh = NFS_FH(inode),
1859 .iap = sattr,
1860 .server = server,
1861 .bitmask = server->attr_bitmask,
1863 struct nfs_setattrres res = {
1864 .fattr = fattr,
1865 .server = server,
1867 struct rpc_message msg = {
1868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1869 .rpc_argp = &arg,
1870 .rpc_resp = &res,
1871 .rpc_cred = cred,
1873 unsigned long timestamp = jiffies;
1874 int status;
1876 nfs_fattr_init(fattr);
1878 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1879 /* Use that stateid */
1880 } else if (state != NULL) {
1881 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1882 } else
1883 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1885 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1886 if (status == 0 && state != NULL)
1887 renew_lease(server, timestamp);
1888 return status;
1891 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1892 struct nfs_fattr *fattr, struct iattr *sattr,
1893 struct nfs4_state *state)
1895 struct nfs_server *server = NFS_SERVER(inode);
1896 struct nfs4_exception exception = { };
1897 int err;
1898 do {
1899 err = nfs4_handle_exception(server,
1900 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1901 &exception);
1902 } while (exception.retry);
1903 return err;
1906 struct nfs4_closedata {
1907 struct inode *inode;
1908 struct nfs4_state *state;
1909 struct nfs_closeargs arg;
1910 struct nfs_closeres res;
1911 struct nfs_fattr fattr;
1912 unsigned long timestamp;
1913 bool roc;
1914 u32 roc_barrier;
1917 static void nfs4_free_closedata(void *data)
1919 struct nfs4_closedata *calldata = data;
1920 struct nfs4_state_owner *sp = calldata->state->owner;
1921 struct super_block *sb = calldata->state->inode->i_sb;
1923 if (calldata->roc)
1924 pnfs_roc_release(calldata->state->inode);
1925 nfs4_put_open_state(calldata->state);
1926 nfs_free_seqid(calldata->arg.seqid);
1927 nfs4_put_state_owner(sp);
1928 nfs_sb_deactive(sb);
1929 kfree(calldata);
1932 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1933 fmode_t fmode)
1935 spin_lock(&state->owner->so_lock);
1936 if (!(fmode & FMODE_READ))
1937 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1938 if (!(fmode & FMODE_WRITE))
1939 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1940 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1941 spin_unlock(&state->owner->so_lock);
1944 static void nfs4_close_done(struct rpc_task *task, void *data)
1946 struct nfs4_closedata *calldata = data;
1947 struct nfs4_state *state = calldata->state;
1948 struct nfs_server *server = NFS_SERVER(calldata->inode);
1950 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1951 return;
1952 /* hmm. we are done with the inode, and in the process of freeing
1953 * the state_owner. we keep this around to process errors
1955 switch (task->tk_status) {
1956 case 0:
1957 if (calldata->roc)
1958 pnfs_roc_set_barrier(state->inode,
1959 calldata->roc_barrier);
1960 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1961 renew_lease(server, calldata->timestamp);
1962 nfs4_close_clear_stateid_flags(state,
1963 calldata->arg.fmode);
1964 break;
1965 case -NFS4ERR_STALE_STATEID:
1966 case -NFS4ERR_OLD_STATEID:
1967 case -NFS4ERR_BAD_STATEID:
1968 case -NFS4ERR_EXPIRED:
1969 if (calldata->arg.fmode == 0)
1970 break;
1971 default:
1972 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1973 rpc_restart_call_prepare(task);
1975 nfs_release_seqid(calldata->arg.seqid);
1976 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1979 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1981 struct nfs4_closedata *calldata = data;
1982 struct nfs4_state *state = calldata->state;
1983 int call_close = 0;
1985 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1986 return;
1988 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1989 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1990 spin_lock(&state->owner->so_lock);
1991 /* Calculate the change in open mode */
1992 if (state->n_rdwr == 0) {
1993 if (state->n_rdonly == 0) {
1994 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1995 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1996 calldata->arg.fmode &= ~FMODE_READ;
1998 if (state->n_wronly == 0) {
1999 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2000 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2001 calldata->arg.fmode &= ~FMODE_WRITE;
2004 spin_unlock(&state->owner->so_lock);
2006 if (!call_close) {
2007 /* Note: exit _without_ calling nfs4_close_done */
2008 task->tk_action = NULL;
2009 return;
2012 if (calldata->arg.fmode == 0) {
2013 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2014 if (calldata->roc &&
2015 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2016 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2017 task, NULL);
2018 return;
2022 nfs_fattr_init(calldata->res.fattr);
2023 calldata->timestamp = jiffies;
2024 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2025 &calldata->arg.seq_args, &calldata->res.seq_res,
2026 1, task))
2027 return;
2028 rpc_call_start(task);
2031 static const struct rpc_call_ops nfs4_close_ops = {
2032 .rpc_call_prepare = nfs4_close_prepare,
2033 .rpc_call_done = nfs4_close_done,
2034 .rpc_release = nfs4_free_closedata,
2038 * It is possible for data to be read/written from a mem-mapped file
2039 * after the sys_close call (which hits the vfs layer as a flush).
2040 * This means that we can't safely call nfsv4 close on a file until
2041 * the inode is cleared. This in turn means that we are not good
2042 * NFSv4 citizens - we do not indicate to the server to update the file's
2043 * share state even when we are done with one of the three share
2044 * stateid's in the inode.
2046 * NOTE: Caller must be holding the sp->so_owner semaphore!
2048 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2050 struct nfs_server *server = NFS_SERVER(state->inode);
2051 struct nfs4_closedata *calldata;
2052 struct nfs4_state_owner *sp = state->owner;
2053 struct rpc_task *task;
2054 struct rpc_message msg = {
2055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2056 .rpc_cred = state->owner->so_cred,
2058 struct rpc_task_setup task_setup_data = {
2059 .rpc_client = server->client,
2060 .rpc_message = &msg,
2061 .callback_ops = &nfs4_close_ops,
2062 .workqueue = nfsiod_workqueue,
2063 .flags = RPC_TASK_ASYNC,
2065 int status = -ENOMEM;
2067 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2068 if (calldata == NULL)
2069 goto out;
2070 calldata->inode = state->inode;
2071 calldata->state = state;
2072 calldata->arg.fh = NFS_FH(state->inode);
2073 calldata->arg.stateid = &state->open_stateid;
2074 /* Serialization for the sequence id */
2075 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2076 if (calldata->arg.seqid == NULL)
2077 goto out_free_calldata;
2078 calldata->arg.fmode = 0;
2079 calldata->arg.bitmask = server->cache_consistency_bitmask;
2080 calldata->res.fattr = &calldata->fattr;
2081 calldata->res.seqid = calldata->arg.seqid;
2082 calldata->res.server = server;
2083 calldata->roc = roc;
2084 nfs_sb_active(calldata->inode->i_sb);
2086 msg.rpc_argp = &calldata->arg;
2087 msg.rpc_resp = &calldata->res;
2088 task_setup_data.callback_data = calldata;
2089 task = rpc_run_task(&task_setup_data);
2090 if (IS_ERR(task))
2091 return PTR_ERR(task);
2092 status = 0;
2093 if (wait)
2094 status = rpc_wait_for_completion_task(task);
2095 rpc_put_task(task);
2096 return status;
2097 out_free_calldata:
2098 kfree(calldata);
2099 out:
2100 if (roc)
2101 pnfs_roc_release(state->inode);
2102 nfs4_put_open_state(state);
2103 nfs4_put_state_owner(sp);
2104 return status;
2107 static struct inode *
2108 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2110 struct nfs4_state *state;
2112 /* Protect against concurrent sillydeletes */
2113 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2114 if (IS_ERR(state))
2115 return ERR_CAST(state);
2116 ctx->state = state;
2117 return igrab(state->inode);
2120 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2122 if (ctx->state == NULL)
2123 return;
2124 if (is_sync)
2125 nfs4_close_sync(ctx->state, ctx->mode);
2126 else
2127 nfs4_close_state(ctx->state, ctx->mode);
2130 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2132 struct nfs4_server_caps_arg args = {
2133 .fhandle = fhandle,
2135 struct nfs4_server_caps_res res = {};
2136 struct rpc_message msg = {
2137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2138 .rpc_argp = &args,
2139 .rpc_resp = &res,
2141 int status;
2143 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2144 if (status == 0) {
2145 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2146 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2147 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2148 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2149 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2150 NFS_CAP_CTIME|NFS_CAP_MTIME);
2151 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2152 server->caps |= NFS_CAP_ACLS;
2153 if (res.has_links != 0)
2154 server->caps |= NFS_CAP_HARDLINKS;
2155 if (res.has_symlinks != 0)
2156 server->caps |= NFS_CAP_SYMLINKS;
2157 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2158 server->caps |= NFS_CAP_FILEID;
2159 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2160 server->caps |= NFS_CAP_MODE;
2161 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2162 server->caps |= NFS_CAP_NLINK;
2163 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2164 server->caps |= NFS_CAP_OWNER;
2165 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2166 server->caps |= NFS_CAP_OWNER_GROUP;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2168 server->caps |= NFS_CAP_ATIME;
2169 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2170 server->caps |= NFS_CAP_CTIME;
2171 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2172 server->caps |= NFS_CAP_MTIME;
2174 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2175 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2176 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2177 server->acl_bitmask = res.acl_bitmask;
2180 return status;
2183 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2185 struct nfs4_exception exception = { };
2186 int err;
2187 do {
2188 err = nfs4_handle_exception(server,
2189 _nfs4_server_capabilities(server, fhandle),
2190 &exception);
2191 } while (exception.retry);
2192 return err;
2195 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2196 struct nfs_fsinfo *info)
2198 struct nfs4_lookup_root_arg args = {
2199 .bitmask = nfs4_fattr_bitmap,
2201 struct nfs4_lookup_res res = {
2202 .server = server,
2203 .fattr = info->fattr,
2204 .fh = fhandle,
2206 struct rpc_message msg = {
2207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2208 .rpc_argp = &args,
2209 .rpc_resp = &res,
2212 nfs_fattr_init(info->fattr);
2213 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2216 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2217 struct nfs_fsinfo *info)
2219 struct nfs4_exception exception = { };
2220 int err;
2221 do {
2222 err = _nfs4_lookup_root(server, fhandle, info);
2223 switch (err) {
2224 case 0:
2225 case -NFS4ERR_WRONGSEC:
2226 break;
2227 default:
2228 err = nfs4_handle_exception(server, err, &exception);
2230 } while (exception.retry);
2231 return err;
2234 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2235 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2237 struct rpc_auth *auth;
2238 int ret;
2240 auth = rpcauth_create(flavor, server->client);
2241 if (!auth) {
2242 ret = -EIO;
2243 goto out;
2245 ret = nfs4_lookup_root(server, fhandle, info);
2246 out:
2247 return ret;
2250 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2251 struct nfs_fsinfo *info)
2253 int i, len, status = 0;
2254 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2256 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2257 flav_array[len] = RPC_AUTH_NULL;
2258 len += 1;
2260 for (i = 0; i < len; i++) {
2261 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2262 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2263 continue;
2264 break;
2267 * -EACCESS could mean that the user doesn't have correct permissions
2268 * to access the mount. It could also mean that we tried to mount
2269 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2270 * existing mount programs don't handle -EACCES very well so it should
2271 * be mapped to -EPERM instead.
2273 if (status == -EACCES)
2274 status = -EPERM;
2275 return status;
2279 * get the file handle for the "/" directory on the server
2281 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2282 struct nfs_fsinfo *info)
2284 int minor_version = server->nfs_client->cl_minorversion;
2285 int status = nfs4_lookup_root(server, fhandle, info);
2286 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2288 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2289 * by nfs4_map_errors() as this function exits.
2291 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2292 if (status == 0)
2293 status = nfs4_server_capabilities(server, fhandle);
2294 if (status == 0)
2295 status = nfs4_do_fsinfo(server, fhandle, info);
2296 return nfs4_map_errors(status);
2299 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2301 * Get locations and (maybe) other attributes of a referral.
2302 * Note that we'll actually follow the referral later when
2303 * we detect fsid mismatch in inode revalidation
2305 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2306 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2308 int status = -ENOMEM;
2309 struct page *page = NULL;
2310 struct nfs4_fs_locations *locations = NULL;
2312 page = alloc_page(GFP_KERNEL);
2313 if (page == NULL)
2314 goto out;
2315 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2316 if (locations == NULL)
2317 goto out;
2319 status = nfs4_proc_fs_locations(dir, name, locations, page);
2320 if (status != 0)
2321 goto out;
2322 /* Make sure server returned a different fsid for the referral */
2323 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2324 dprintk("%s: server did not return a different fsid for"
2325 " a referral at %s\n", __func__, name->name);
2326 status = -EIO;
2327 goto out;
2329 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2330 nfs_fixup_referral_attributes(&locations->fattr);
2332 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2333 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2334 memset(fhandle, 0, sizeof(struct nfs_fh));
2335 out:
2336 if (page)
2337 __free_page(page);
2338 kfree(locations);
2339 return status;
2342 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2344 struct nfs4_getattr_arg args = {
2345 .fh = fhandle,
2346 .bitmask = server->attr_bitmask,
2348 struct nfs4_getattr_res res = {
2349 .fattr = fattr,
2350 .server = server,
2352 struct rpc_message msg = {
2353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2354 .rpc_argp = &args,
2355 .rpc_resp = &res,
2358 nfs_fattr_init(fattr);
2359 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2362 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2364 struct nfs4_exception exception = { };
2365 int err;
2366 do {
2367 err = nfs4_handle_exception(server,
2368 _nfs4_proc_getattr(server, fhandle, fattr),
2369 &exception);
2370 } while (exception.retry);
2371 return err;
2375 * The file is not closed if it is opened due to the a request to change
2376 * the size of the file. The open call will not be needed once the
2377 * VFS layer lookup-intents are implemented.
2379 * Close is called when the inode is destroyed.
2380 * If we haven't opened the file for O_WRONLY, we
2381 * need to in the size_change case to obtain a stateid.
2383 * Got race?
2384 * Because OPEN is always done by name in nfsv4, it is
2385 * possible that we opened a different file by the same
2386 * name. We can recognize this race condition, but we
2387 * can't do anything about it besides returning an error.
2389 * This will be fixed with VFS changes (lookup-intent).
2391 static int
2392 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2393 struct iattr *sattr)
2395 struct inode *inode = dentry->d_inode;
2396 struct rpc_cred *cred = NULL;
2397 struct nfs4_state *state = NULL;
2398 int status;
2400 if (pnfs_ld_layoutret_on_setattr(inode))
2401 pnfs_return_layout(inode);
2403 nfs_fattr_init(fattr);
2405 /* Search for an existing open(O_WRITE) file */
2406 if (sattr->ia_valid & ATTR_FILE) {
2407 struct nfs_open_context *ctx;
2409 ctx = nfs_file_open_context(sattr->ia_file);
2410 if (ctx) {
2411 cred = ctx->cred;
2412 state = ctx->state;
2416 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2417 if (status == 0)
2418 nfs_setattr_update_inode(inode, sattr);
2419 return status;
2422 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2423 const struct qstr *name, struct nfs_fh *fhandle,
2424 struct nfs_fattr *fattr)
2426 struct nfs_server *server = NFS_SERVER(dir);
2427 int status;
2428 struct nfs4_lookup_arg args = {
2429 .bitmask = server->attr_bitmask,
2430 .dir_fh = NFS_FH(dir),
2431 .name = name,
2433 struct nfs4_lookup_res res = {
2434 .server = server,
2435 .fattr = fattr,
2436 .fh = fhandle,
2438 struct rpc_message msg = {
2439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2440 .rpc_argp = &args,
2441 .rpc_resp = &res,
2444 nfs_fattr_init(fattr);
2446 dprintk("NFS call lookup %s\n", name->name);
2447 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2448 dprintk("NFS reply lookup: %d\n", status);
2449 return status;
2452 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2454 memset(fh, 0, sizeof(struct nfs_fh));
2455 fattr->fsid.major = 1;
2456 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2457 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2458 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2459 fattr->nlink = 2;
2462 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2463 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2465 struct nfs4_exception exception = { };
2466 int err;
2467 do {
2468 int status;
2470 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2471 switch (status) {
2472 case -NFS4ERR_BADNAME:
2473 return -ENOENT;
2474 case -NFS4ERR_MOVED:
2475 return nfs4_get_referral(dir, name, fattr, fhandle);
2476 case -NFS4ERR_WRONGSEC:
2477 nfs_fixup_secinfo_attributes(fattr, fhandle);
2479 err = nfs4_handle_exception(NFS_SERVER(dir),
2480 status, &exception);
2481 } while (exception.retry);
2482 return err;
2485 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2487 struct nfs_server *server = NFS_SERVER(inode);
2488 struct nfs4_accessargs args = {
2489 .fh = NFS_FH(inode),
2490 .bitmask = server->attr_bitmask,
2492 struct nfs4_accessres res = {
2493 .server = server,
2495 struct rpc_message msg = {
2496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2497 .rpc_argp = &args,
2498 .rpc_resp = &res,
2499 .rpc_cred = entry->cred,
2501 int mode = entry->mask;
2502 int status;
2505 * Determine which access bits we want to ask for...
2507 if (mode & MAY_READ)
2508 args.access |= NFS4_ACCESS_READ;
2509 if (S_ISDIR(inode->i_mode)) {
2510 if (mode & MAY_WRITE)
2511 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2512 if (mode & MAY_EXEC)
2513 args.access |= NFS4_ACCESS_LOOKUP;
2514 } else {
2515 if (mode & MAY_WRITE)
2516 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2517 if (mode & MAY_EXEC)
2518 args.access |= NFS4_ACCESS_EXECUTE;
2521 res.fattr = nfs_alloc_fattr();
2522 if (res.fattr == NULL)
2523 return -ENOMEM;
2525 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2526 if (!status) {
2527 entry->mask = 0;
2528 if (res.access & NFS4_ACCESS_READ)
2529 entry->mask |= MAY_READ;
2530 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2531 entry->mask |= MAY_WRITE;
2532 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2533 entry->mask |= MAY_EXEC;
2534 nfs_refresh_inode(inode, res.fattr);
2536 nfs_free_fattr(res.fattr);
2537 return status;
2540 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(NFS_SERVER(inode),
2546 _nfs4_proc_access(inode, entry),
2547 &exception);
2548 } while (exception.retry);
2549 return err;
2553 * TODO: For the time being, we don't try to get any attributes
2554 * along with any of the zero-copy operations READ, READDIR,
2555 * READLINK, WRITE.
2557 * In the case of the first three, we want to put the GETATTR
2558 * after the read-type operation -- this is because it is hard
2559 * to predict the length of a GETATTR response in v4, and thus
2560 * align the READ data correctly. This means that the GETATTR
2561 * may end up partially falling into the page cache, and we should
2562 * shift it into the 'tail' of the xdr_buf before processing.
2563 * To do this efficiently, we need to know the total length
2564 * of data received, which doesn't seem to be available outside
2565 * of the RPC layer.
2567 * In the case of WRITE, we also want to put the GETATTR after
2568 * the operation -- in this case because we want to make sure
2569 * we get the post-operation mtime and size. This means that
2570 * we can't use xdr_encode_pages() as written: we need a variant
2571 * of it which would leave room in the 'tail' iovec.
2573 * Both of these changes to the XDR layer would in fact be quite
2574 * minor, but I decided to leave them for a subsequent patch.
2576 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2577 unsigned int pgbase, unsigned int pglen)
2579 struct nfs4_readlink args = {
2580 .fh = NFS_FH(inode),
2581 .pgbase = pgbase,
2582 .pglen = pglen,
2583 .pages = &page,
2585 struct nfs4_readlink_res res;
2586 struct rpc_message msg = {
2587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2588 .rpc_argp = &args,
2589 .rpc_resp = &res,
2592 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2595 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2596 unsigned int pgbase, unsigned int pglen)
2598 struct nfs4_exception exception = { };
2599 int err;
2600 do {
2601 err = nfs4_handle_exception(NFS_SERVER(inode),
2602 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2603 &exception);
2604 } while (exception.retry);
2605 return err;
2609 * Got race?
2610 * We will need to arrange for the VFS layer to provide an atomic open.
2611 * Until then, this create/open method is prone to inefficiency and race
2612 * conditions due to the lookup, create, and open VFS calls from sys_open()
2613 * placed on the wire.
2615 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2616 * The file will be opened again in the subsequent VFS open call
2617 * (nfs4_proc_file_open).
2619 * The open for read will just hang around to be used by any process that
2620 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2623 static int
2624 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2625 int flags, struct nfs_open_context *ctx)
2627 struct dentry *de = dentry;
2628 struct nfs4_state *state;
2629 struct rpc_cred *cred = NULL;
2630 fmode_t fmode = 0;
2631 int status = 0;
2633 if (ctx != NULL) {
2634 cred = ctx->cred;
2635 de = ctx->dentry;
2636 fmode = ctx->mode;
2638 sattr->ia_mode &= ~current_umask();
2639 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2640 d_drop(dentry);
2641 if (IS_ERR(state)) {
2642 status = PTR_ERR(state);
2643 goto out;
2645 d_add(dentry, igrab(state->inode));
2646 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2647 if (ctx != NULL)
2648 ctx->state = state;
2649 else
2650 nfs4_close_sync(state, fmode);
2651 out:
2652 return status;
2655 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2657 struct nfs_server *server = NFS_SERVER(dir);
2658 struct nfs_removeargs args = {
2659 .fh = NFS_FH(dir),
2660 .name.len = name->len,
2661 .name.name = name->name,
2662 .bitmask = server->attr_bitmask,
2664 struct nfs_removeres res = {
2665 .server = server,
2667 struct rpc_message msg = {
2668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2669 .rpc_argp = &args,
2670 .rpc_resp = &res,
2672 int status = -ENOMEM;
2674 res.dir_attr = nfs_alloc_fattr();
2675 if (res.dir_attr == NULL)
2676 goto out;
2678 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2679 if (status == 0) {
2680 update_changeattr(dir, &res.cinfo);
2681 nfs_post_op_update_inode(dir, res.dir_attr);
2683 nfs_free_fattr(res.dir_attr);
2684 out:
2685 return status;
2688 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2690 struct nfs4_exception exception = { };
2691 int err;
2692 do {
2693 err = nfs4_handle_exception(NFS_SERVER(dir),
2694 _nfs4_proc_remove(dir, name),
2695 &exception);
2696 } while (exception.retry);
2697 return err;
2700 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2702 struct nfs_server *server = NFS_SERVER(dir);
2703 struct nfs_removeargs *args = msg->rpc_argp;
2704 struct nfs_removeres *res = msg->rpc_resp;
2706 args->bitmask = server->cache_consistency_bitmask;
2707 res->server = server;
2708 res->seq_res.sr_slot = NULL;
2709 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2712 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2714 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2716 if (!nfs4_sequence_done(task, &res->seq_res))
2717 return 0;
2718 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2719 return 0;
2720 update_changeattr(dir, &res->cinfo);
2721 nfs_post_op_update_inode(dir, res->dir_attr);
2722 return 1;
2725 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2727 struct nfs_server *server = NFS_SERVER(dir);
2728 struct nfs_renameargs *arg = msg->rpc_argp;
2729 struct nfs_renameres *res = msg->rpc_resp;
2731 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2732 arg->bitmask = server->attr_bitmask;
2733 res->server = server;
2736 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2737 struct inode *new_dir)
2739 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2741 if (!nfs4_sequence_done(task, &res->seq_res))
2742 return 0;
2743 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2744 return 0;
2746 update_changeattr(old_dir, &res->old_cinfo);
2747 nfs_post_op_update_inode(old_dir, res->old_fattr);
2748 update_changeattr(new_dir, &res->new_cinfo);
2749 nfs_post_op_update_inode(new_dir, res->new_fattr);
2750 return 1;
2753 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2754 struct inode *new_dir, struct qstr *new_name)
2756 struct nfs_server *server = NFS_SERVER(old_dir);
2757 struct nfs_renameargs arg = {
2758 .old_dir = NFS_FH(old_dir),
2759 .new_dir = NFS_FH(new_dir),
2760 .old_name = old_name,
2761 .new_name = new_name,
2762 .bitmask = server->attr_bitmask,
2764 struct nfs_renameres res = {
2765 .server = server,
2767 struct rpc_message msg = {
2768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2769 .rpc_argp = &arg,
2770 .rpc_resp = &res,
2772 int status = -ENOMEM;
2774 res.old_fattr = nfs_alloc_fattr();
2775 res.new_fattr = nfs_alloc_fattr();
2776 if (res.old_fattr == NULL || res.new_fattr == NULL)
2777 goto out;
2779 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2780 if (!status) {
2781 update_changeattr(old_dir, &res.old_cinfo);
2782 nfs_post_op_update_inode(old_dir, res.old_fattr);
2783 update_changeattr(new_dir, &res.new_cinfo);
2784 nfs_post_op_update_inode(new_dir, res.new_fattr);
2786 out:
2787 nfs_free_fattr(res.new_fattr);
2788 nfs_free_fattr(res.old_fattr);
2789 return status;
2792 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2793 struct inode *new_dir, struct qstr *new_name)
2795 struct nfs4_exception exception = { };
2796 int err;
2797 do {
2798 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2799 _nfs4_proc_rename(old_dir, old_name,
2800 new_dir, new_name),
2801 &exception);
2802 } while (exception.retry);
2803 return err;
2806 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2808 struct nfs_server *server = NFS_SERVER(inode);
2809 struct nfs4_link_arg arg = {
2810 .fh = NFS_FH(inode),
2811 .dir_fh = NFS_FH(dir),
2812 .name = name,
2813 .bitmask = server->attr_bitmask,
2815 struct nfs4_link_res res = {
2816 .server = server,
2818 struct rpc_message msg = {
2819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2820 .rpc_argp = &arg,
2821 .rpc_resp = &res,
2823 int status = -ENOMEM;
2825 res.fattr = nfs_alloc_fattr();
2826 res.dir_attr = nfs_alloc_fattr();
2827 if (res.fattr == NULL || res.dir_attr == NULL)
2828 goto out;
2830 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2831 if (!status) {
2832 update_changeattr(dir, &res.cinfo);
2833 nfs_post_op_update_inode(dir, res.dir_attr);
2834 nfs_post_op_update_inode(inode, res.fattr);
2836 out:
2837 nfs_free_fattr(res.dir_attr);
2838 nfs_free_fattr(res.fattr);
2839 return status;
2842 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2844 struct nfs4_exception exception = { };
2845 int err;
2846 do {
2847 err = nfs4_handle_exception(NFS_SERVER(inode),
2848 _nfs4_proc_link(inode, dir, name),
2849 &exception);
2850 } while (exception.retry);
2851 return err;
2854 struct nfs4_createdata {
2855 struct rpc_message msg;
2856 struct nfs4_create_arg arg;
2857 struct nfs4_create_res res;
2858 struct nfs_fh fh;
2859 struct nfs_fattr fattr;
2860 struct nfs_fattr dir_fattr;
2863 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2864 struct qstr *name, struct iattr *sattr, u32 ftype)
2866 struct nfs4_createdata *data;
2868 data = kzalloc(sizeof(*data), GFP_KERNEL);
2869 if (data != NULL) {
2870 struct nfs_server *server = NFS_SERVER(dir);
2872 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2873 data->msg.rpc_argp = &data->arg;
2874 data->msg.rpc_resp = &data->res;
2875 data->arg.dir_fh = NFS_FH(dir);
2876 data->arg.server = server;
2877 data->arg.name = name;
2878 data->arg.attrs = sattr;
2879 data->arg.ftype = ftype;
2880 data->arg.bitmask = server->attr_bitmask;
2881 data->res.server = server;
2882 data->res.fh = &data->fh;
2883 data->res.fattr = &data->fattr;
2884 data->res.dir_fattr = &data->dir_fattr;
2885 nfs_fattr_init(data->res.fattr);
2886 nfs_fattr_init(data->res.dir_fattr);
2888 return data;
2891 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2893 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2894 &data->arg.seq_args, &data->res.seq_res, 1);
2895 if (status == 0) {
2896 update_changeattr(dir, &data->res.dir_cinfo);
2897 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2898 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2900 return status;
2903 static void nfs4_free_createdata(struct nfs4_createdata *data)
2905 kfree(data);
2908 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2909 struct page *page, unsigned int len, struct iattr *sattr)
2911 struct nfs4_createdata *data;
2912 int status = -ENAMETOOLONG;
2914 if (len > NFS4_MAXPATHLEN)
2915 goto out;
2917 status = -ENOMEM;
2918 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2919 if (data == NULL)
2920 goto out;
2922 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2923 data->arg.u.symlink.pages = &page;
2924 data->arg.u.symlink.len = len;
2926 status = nfs4_do_create(dir, dentry, data);
2928 nfs4_free_createdata(data);
2929 out:
2930 return status;
2933 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2934 struct page *page, unsigned int len, struct iattr *sattr)
2936 struct nfs4_exception exception = { };
2937 int err;
2938 do {
2939 err = nfs4_handle_exception(NFS_SERVER(dir),
2940 _nfs4_proc_symlink(dir, dentry, page,
2941 len, sattr),
2942 &exception);
2943 } while (exception.retry);
2944 return err;
2947 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2948 struct iattr *sattr)
2950 struct nfs4_createdata *data;
2951 int status = -ENOMEM;
2953 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2954 if (data == NULL)
2955 goto out;
2957 status = nfs4_do_create(dir, dentry, data);
2959 nfs4_free_createdata(data);
2960 out:
2961 return status;
2964 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2965 struct iattr *sattr)
2967 struct nfs4_exception exception = { };
2968 int err;
2970 sattr->ia_mode &= ~current_umask();
2971 do {
2972 err = nfs4_handle_exception(NFS_SERVER(dir),
2973 _nfs4_proc_mkdir(dir, dentry, sattr),
2974 &exception);
2975 } while (exception.retry);
2976 return err;
2979 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2980 u64 cookie, struct page **pages, unsigned int count, int plus)
2982 struct inode *dir = dentry->d_inode;
2983 struct nfs4_readdir_arg args = {
2984 .fh = NFS_FH(dir),
2985 .pages = pages,
2986 .pgbase = 0,
2987 .count = count,
2988 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2989 .plus = plus,
2991 struct nfs4_readdir_res res;
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2994 .rpc_argp = &args,
2995 .rpc_resp = &res,
2996 .rpc_cred = cred,
2998 int status;
3000 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3001 dentry->d_parent->d_name.name,
3002 dentry->d_name.name,
3003 (unsigned long long)cookie);
3004 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3005 res.pgbase = args.pgbase;
3006 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3007 if (status >= 0) {
3008 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3009 status += args.pgbase;
3012 nfs_invalidate_atime(dir);
3014 dprintk("%s: returns %d\n", __func__, status);
3015 return status;
3018 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3019 u64 cookie, struct page **pages, unsigned int count, int plus)
3021 struct nfs4_exception exception = { };
3022 int err;
3023 do {
3024 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3025 _nfs4_proc_readdir(dentry, cred, cookie,
3026 pages, count, plus),
3027 &exception);
3028 } while (exception.retry);
3029 return err;
3032 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3033 struct iattr *sattr, dev_t rdev)
3035 struct nfs4_createdata *data;
3036 int mode = sattr->ia_mode;
3037 int status = -ENOMEM;
3039 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3040 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3042 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3043 if (data == NULL)
3044 goto out;
3046 if (S_ISFIFO(mode))
3047 data->arg.ftype = NF4FIFO;
3048 else if (S_ISBLK(mode)) {
3049 data->arg.ftype = NF4BLK;
3050 data->arg.u.device.specdata1 = MAJOR(rdev);
3051 data->arg.u.device.specdata2 = MINOR(rdev);
3053 else if (S_ISCHR(mode)) {
3054 data->arg.ftype = NF4CHR;
3055 data->arg.u.device.specdata1 = MAJOR(rdev);
3056 data->arg.u.device.specdata2 = MINOR(rdev);
3059 status = nfs4_do_create(dir, dentry, data);
3061 nfs4_free_createdata(data);
3062 out:
3063 return status;
3066 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3067 struct iattr *sattr, dev_t rdev)
3069 struct nfs4_exception exception = { };
3070 int err;
3072 sattr->ia_mode &= ~current_umask();
3073 do {
3074 err = nfs4_handle_exception(NFS_SERVER(dir),
3075 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3076 &exception);
3077 } while (exception.retry);
3078 return err;
3081 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3082 struct nfs_fsstat *fsstat)
3084 struct nfs4_statfs_arg args = {
3085 .fh = fhandle,
3086 .bitmask = server->attr_bitmask,
3088 struct nfs4_statfs_res res = {
3089 .fsstat = fsstat,
3091 struct rpc_message msg = {
3092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3093 .rpc_argp = &args,
3094 .rpc_resp = &res,
3097 nfs_fattr_init(fsstat->fattr);
3098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3101 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3103 struct nfs4_exception exception = { };
3104 int err;
3105 do {
3106 err = nfs4_handle_exception(server,
3107 _nfs4_proc_statfs(server, fhandle, fsstat),
3108 &exception);
3109 } while (exception.retry);
3110 return err;
3113 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3114 struct nfs_fsinfo *fsinfo)
3116 struct nfs4_fsinfo_arg args = {
3117 .fh = fhandle,
3118 .bitmask = server->attr_bitmask,
3120 struct nfs4_fsinfo_res res = {
3121 .fsinfo = fsinfo,
3123 struct rpc_message msg = {
3124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3125 .rpc_argp = &args,
3126 .rpc_resp = &res,
3129 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3132 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3134 struct nfs4_exception exception = { };
3135 int err;
3137 do {
3138 err = nfs4_handle_exception(server,
3139 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3140 &exception);
3141 } while (exception.retry);
3142 return err;
3145 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3147 nfs_fattr_init(fsinfo->fattr);
3148 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3151 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3152 struct nfs_pathconf *pathconf)
3154 struct nfs4_pathconf_arg args = {
3155 .fh = fhandle,
3156 .bitmask = server->attr_bitmask,
3158 struct nfs4_pathconf_res res = {
3159 .pathconf = pathconf,
3161 struct rpc_message msg = {
3162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3163 .rpc_argp = &args,
3164 .rpc_resp = &res,
3167 /* None of the pathconf attributes are mandatory to implement */
3168 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3169 memset(pathconf, 0, sizeof(*pathconf));
3170 return 0;
3173 nfs_fattr_init(pathconf->fattr);
3174 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3177 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3178 struct nfs_pathconf *pathconf)
3180 struct nfs4_exception exception = { };
3181 int err;
3183 do {
3184 err = nfs4_handle_exception(server,
3185 _nfs4_proc_pathconf(server, fhandle, pathconf),
3186 &exception);
3187 } while (exception.retry);
3188 return err;
3191 void __nfs4_read_done_cb(struct nfs_read_data *data)
3193 nfs_invalidate_atime(data->inode);
3196 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3198 struct nfs_server *server = NFS_SERVER(data->inode);
3200 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3201 rpc_restart_call_prepare(task);
3202 return -EAGAIN;
3205 __nfs4_read_done_cb(data);
3206 if (task->tk_status > 0)
3207 renew_lease(server, data->timestamp);
3208 return 0;
3211 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3214 dprintk("--> %s\n", __func__);
3216 if (!nfs4_sequence_done(task, &data->res.seq_res))
3217 return -EAGAIN;
3219 return data->read_done_cb ? data->read_done_cb(task, data) :
3220 nfs4_read_done_cb(task, data);
3223 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3225 data->timestamp = jiffies;
3226 data->read_done_cb = nfs4_read_done_cb;
3227 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3230 /* Reset the the nfs_read_data to send the read to the MDS. */
3231 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3233 dprintk("%s Reset task for i/o through\n", __func__);
3234 put_lseg(data->lseg);
3235 data->lseg = NULL;
3236 /* offsets will differ in the dense stripe case */
3237 data->args.offset = data->mds_offset;
3238 data->ds_clp = NULL;
3239 data->args.fh = NFS_FH(data->inode);
3240 data->read_done_cb = nfs4_read_done_cb;
3241 task->tk_ops = data->mds_ops;
3242 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3244 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3246 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3248 struct inode *inode = data->inode;
3250 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3251 rpc_restart_call_prepare(task);
3252 return -EAGAIN;
3254 if (task->tk_status >= 0) {
3255 renew_lease(NFS_SERVER(inode), data->timestamp);
3256 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3258 return 0;
3261 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3263 if (!nfs4_sequence_done(task, &data->res.seq_res))
3264 return -EAGAIN;
3265 return data->write_done_cb ? data->write_done_cb(task, data) :
3266 nfs4_write_done_cb(task, data);
3269 /* Reset the the nfs_write_data to send the write to the MDS. */
3270 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3272 dprintk("%s Reset task for i/o through\n", __func__);
3273 put_lseg(data->lseg);
3274 data->lseg = NULL;
3275 data->ds_clp = NULL;
3276 data->write_done_cb = nfs4_write_done_cb;
3277 data->args.fh = NFS_FH(data->inode);
3278 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3279 data->args.offset = data->mds_offset;
3280 data->res.fattr = &data->fattr;
3281 task->tk_ops = data->mds_ops;
3282 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3284 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3286 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3288 struct nfs_server *server = NFS_SERVER(data->inode);
3290 if (data->lseg) {
3291 data->args.bitmask = NULL;
3292 data->res.fattr = NULL;
3293 } else
3294 data->args.bitmask = server->cache_consistency_bitmask;
3295 if (!data->write_done_cb)
3296 data->write_done_cb = nfs4_write_done_cb;
3297 data->res.server = server;
3298 data->timestamp = jiffies;
3300 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3303 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3305 struct inode *inode = data->inode;
3307 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3308 rpc_restart_call_prepare(task);
3309 return -EAGAIN;
3311 nfs_refresh_inode(inode, data->res.fattr);
3312 return 0;
3315 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3317 if (!nfs4_sequence_done(task, &data->res.seq_res))
3318 return -EAGAIN;
3319 return data->write_done_cb(task, data);
3322 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3324 struct nfs_server *server = NFS_SERVER(data->inode);
3326 if (data->lseg) {
3327 data->args.bitmask = NULL;
3328 data->res.fattr = NULL;
3329 } else
3330 data->args.bitmask = server->cache_consistency_bitmask;
3331 if (!data->write_done_cb)
3332 data->write_done_cb = nfs4_commit_done_cb;
3333 data->res.server = server;
3334 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3337 struct nfs4_renewdata {
3338 struct nfs_client *client;
3339 unsigned long timestamp;
3343 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3344 * standalone procedure for queueing an asynchronous RENEW.
3346 static void nfs4_renew_release(void *calldata)
3348 struct nfs4_renewdata *data = calldata;
3349 struct nfs_client *clp = data->client;
3351 if (atomic_read(&clp->cl_count) > 1)
3352 nfs4_schedule_state_renewal(clp);
3353 nfs_put_client(clp);
3354 kfree(data);
3357 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3359 struct nfs4_renewdata *data = calldata;
3360 struct nfs_client *clp = data->client;
3361 unsigned long timestamp = data->timestamp;
3363 if (task->tk_status < 0) {
3364 /* Unless we're shutting down, schedule state recovery! */
3365 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3366 return;
3367 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3368 nfs4_schedule_lease_recovery(clp);
3369 return;
3371 nfs4_schedule_path_down_recovery(clp);
3373 do_renew_lease(clp, timestamp);
3376 static const struct rpc_call_ops nfs4_renew_ops = {
3377 .rpc_call_done = nfs4_renew_done,
3378 .rpc_release = nfs4_renew_release,
3381 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3385 .rpc_argp = clp,
3386 .rpc_cred = cred,
3388 struct nfs4_renewdata *data;
3390 if (renew_flags == 0)
3391 return 0;
3392 if (!atomic_inc_not_zero(&clp->cl_count))
3393 return -EIO;
3394 data = kmalloc(sizeof(*data), GFP_NOFS);
3395 if (data == NULL)
3396 return -ENOMEM;
3397 data->client = clp;
3398 data->timestamp = jiffies;
3399 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3400 &nfs4_renew_ops, data);
3403 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3407 .rpc_argp = clp,
3408 .rpc_cred = cred,
3410 unsigned long now = jiffies;
3411 int status;
3413 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3414 if (status < 0)
3415 return status;
3416 do_renew_lease(clp, now);
3417 return 0;
3420 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3422 return (server->caps & NFS_CAP_ACLS)
3423 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3424 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3427 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3428 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3429 * the stack.
3431 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3433 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3434 struct page **pages, unsigned int *pgbase)
3436 struct page *newpage, **spages;
3437 int rc = 0;
3438 size_t len;
3439 spages = pages;
3441 do {
3442 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3443 newpage = alloc_page(GFP_KERNEL);
3445 if (newpage == NULL)
3446 goto unwind;
3447 memcpy(page_address(newpage), buf, len);
3448 buf += len;
3449 buflen -= len;
3450 *pages++ = newpage;
3451 rc++;
3452 } while (buflen != 0);
3454 return rc;
3456 unwind:
3457 for(; rc > 0; rc--)
3458 __free_page(spages[rc-1]);
3459 return -ENOMEM;
3462 struct nfs4_cached_acl {
3463 int cached;
3464 size_t len;
3465 char data[0];
3468 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3470 struct nfs_inode *nfsi = NFS_I(inode);
3472 spin_lock(&inode->i_lock);
3473 kfree(nfsi->nfs4_acl);
3474 nfsi->nfs4_acl = acl;
3475 spin_unlock(&inode->i_lock);
3478 static void nfs4_zap_acl_attr(struct inode *inode)
3480 nfs4_set_cached_acl(inode, NULL);
3483 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3485 struct nfs_inode *nfsi = NFS_I(inode);
3486 struct nfs4_cached_acl *acl;
3487 int ret = -ENOENT;
3489 spin_lock(&inode->i_lock);
3490 acl = nfsi->nfs4_acl;
3491 if (acl == NULL)
3492 goto out;
3493 if (buf == NULL) /* user is just asking for length */
3494 goto out_len;
3495 if (acl->cached == 0)
3496 goto out;
3497 ret = -ERANGE; /* see getxattr(2) man page */
3498 if (acl->len > buflen)
3499 goto out;
3500 memcpy(buf, acl->data, acl->len);
3501 out_len:
3502 ret = acl->len;
3503 out:
3504 spin_unlock(&inode->i_lock);
3505 return ret;
3508 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3510 struct nfs4_cached_acl *acl;
3512 if (buf && acl_len <= PAGE_SIZE) {
3513 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3514 if (acl == NULL)
3515 goto out;
3516 acl->cached = 1;
3517 memcpy(acl->data, buf, acl_len);
3518 } else {
3519 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3520 if (acl == NULL)
3521 goto out;
3522 acl->cached = 0;
3524 acl->len = acl_len;
3525 out:
3526 nfs4_set_cached_acl(inode, acl);
3530 * The getxattr API returns the required buffer length when called with a
3531 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3532 * the required buf. On a NULL buf, we send a page of data to the server
3533 * guessing that the ACL request can be serviced by a page. If so, we cache
3534 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3535 * the cache. If not so, we throw away the page, and cache the required
3536 * length. The next getxattr call will then produce another round trip to
3537 * the server, this time with the input buf of the required size.
3539 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3541 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3542 struct nfs_getaclargs args = {
3543 .fh = NFS_FH(inode),
3544 .acl_pages = pages,
3545 .acl_len = buflen,
3547 struct nfs_getaclres res = {
3548 .acl_len = buflen,
3550 void *resp_buf;
3551 struct rpc_message msg = {
3552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3553 .rpc_argp = &args,
3554 .rpc_resp = &res,
3556 int ret = -ENOMEM, npages, i, acl_len = 0;
3558 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3559 /* As long as we're doing a round trip to the server anyway,
3560 * let's be prepared for a page of acl data. */
3561 if (npages == 0)
3562 npages = 1;
3564 for (i = 0; i < npages; i++) {
3565 pages[i] = alloc_page(GFP_KERNEL);
3566 if (!pages[i])
3567 goto out_free;
3569 if (npages > 1) {
3570 /* for decoding across pages */
3571 args.acl_scratch = alloc_page(GFP_KERNEL);
3572 if (!args.acl_scratch)
3573 goto out_free;
3575 args.acl_len = npages * PAGE_SIZE;
3576 args.acl_pgbase = 0;
3577 /* Let decode_getfacl know not to fail if the ACL data is larger than
3578 * the page we send as a guess */
3579 if (buf == NULL)
3580 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3581 resp_buf = page_address(pages[0]);
3583 dprintk("%s buf %p buflen %ld npages %d args.acl_len %ld\n",
3584 __func__, buf, buflen, npages, args.acl_len);
3585 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3586 &msg, &args.seq_args, &res.seq_res, 0);
3587 if (ret)
3588 goto out_free;
3590 acl_len = res.acl_len - res.acl_data_offset;
3591 if (acl_len > args.acl_len)
3592 nfs4_write_cached_acl(inode, NULL, acl_len);
3593 else
3594 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3595 acl_len);
3596 if (buf) {
3597 ret = -ERANGE;
3598 if (acl_len > buflen)
3599 goto out_free;
3600 _copy_from_pages(buf, pages, res.acl_data_offset,
3601 res.acl_len);
3603 ret = acl_len;
3604 out_free:
3605 for (i = 0; i < npages; i++)
3606 if (pages[i])
3607 __free_page(pages[i]);
3608 if (args.acl_scratch)
3609 __free_page(args.acl_scratch);
3610 return ret;
3613 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3615 struct nfs4_exception exception = { };
3616 ssize_t ret;
3617 do {
3618 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3619 if (ret >= 0)
3620 break;
3621 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3622 } while (exception.retry);
3623 return ret;
3626 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3628 struct nfs_server *server = NFS_SERVER(inode);
3629 int ret;
3631 if (!nfs4_server_supports_acls(server))
3632 return -EOPNOTSUPP;
3633 ret = nfs_revalidate_inode(server, inode);
3634 if (ret < 0)
3635 return ret;
3636 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3637 nfs_zap_acl_cache(inode);
3638 ret = nfs4_read_cached_acl(inode, buf, buflen);
3639 if (ret != -ENOENT)
3640 /* -ENOENT is returned if there is no ACL or if there is an ACL
3641 * but no cached acl data, just the acl length */
3642 return ret;
3643 return nfs4_get_acl_uncached(inode, buf, buflen);
3646 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3648 struct nfs_server *server = NFS_SERVER(inode);
3649 struct page *pages[NFS4ACL_MAXPAGES];
3650 struct nfs_setaclargs arg = {
3651 .fh = NFS_FH(inode),
3652 .acl_pages = pages,
3653 .acl_len = buflen,
3655 struct nfs_setaclres res;
3656 struct rpc_message msg = {
3657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3658 .rpc_argp = &arg,
3659 .rpc_resp = &res,
3661 int ret, i;
3663 if (!nfs4_server_supports_acls(server))
3664 return -EOPNOTSUPP;
3665 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3666 if (i < 0)
3667 return i;
3668 nfs_inode_return_delegation(inode);
3669 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3672 * Free each page after tx, so the only ref left is
3673 * held by the network stack
3675 for (; i > 0; i--)
3676 put_page(pages[i-1]);
3679 * Acl update can result in inode attribute update.
3680 * so mark the attribute cache invalid.
3682 spin_lock(&inode->i_lock);
3683 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3684 spin_unlock(&inode->i_lock);
3685 nfs_access_zap_cache(inode);
3686 nfs_zap_acl_cache(inode);
3687 return ret;
3690 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3692 struct nfs4_exception exception = { };
3693 int err;
3694 do {
3695 err = nfs4_handle_exception(NFS_SERVER(inode),
3696 __nfs4_proc_set_acl(inode, buf, buflen),
3697 &exception);
3698 } while (exception.retry);
3699 return err;
3702 static int
3703 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3705 struct nfs_client *clp = server->nfs_client;
3707 if (task->tk_status >= 0)
3708 return 0;
3709 switch(task->tk_status) {
3710 case -NFS4ERR_ADMIN_REVOKED:
3711 case -NFS4ERR_BAD_STATEID:
3712 case -NFS4ERR_OPENMODE:
3713 if (state == NULL)
3714 break;
3715 nfs4_schedule_stateid_recovery(server, state);
3716 goto wait_on_recovery;
3717 case -NFS4ERR_EXPIRED:
3718 if (state != NULL)
3719 nfs4_schedule_stateid_recovery(server, state);
3720 case -NFS4ERR_STALE_STATEID:
3721 case -NFS4ERR_STALE_CLIENTID:
3722 nfs4_schedule_lease_recovery(clp);
3723 goto wait_on_recovery;
3724 #if defined(CONFIG_NFS_V4_1)
3725 case -NFS4ERR_BADSESSION:
3726 case -NFS4ERR_BADSLOT:
3727 case -NFS4ERR_BAD_HIGH_SLOT:
3728 case -NFS4ERR_DEADSESSION:
3729 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3730 case -NFS4ERR_SEQ_FALSE_RETRY:
3731 case -NFS4ERR_SEQ_MISORDERED:
3732 dprintk("%s ERROR %d, Reset session\n", __func__,
3733 task->tk_status);
3734 nfs4_schedule_session_recovery(clp->cl_session);
3735 task->tk_status = 0;
3736 return -EAGAIN;
3737 #endif /* CONFIG_NFS_V4_1 */
3738 case -NFS4ERR_DELAY:
3739 nfs_inc_server_stats(server, NFSIOS_DELAY);
3740 case -NFS4ERR_GRACE:
3741 case -EKEYEXPIRED:
3742 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3743 task->tk_status = 0;
3744 return -EAGAIN;
3745 case -NFS4ERR_RETRY_UNCACHED_REP:
3746 case -NFS4ERR_OLD_STATEID:
3747 task->tk_status = 0;
3748 return -EAGAIN;
3750 task->tk_status = nfs4_map_errors(task->tk_status);
3751 return 0;
3752 wait_on_recovery:
3753 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3754 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3755 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3756 task->tk_status = 0;
3757 return -EAGAIN;
3760 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3761 unsigned short port, struct rpc_cred *cred,
3762 struct nfs4_setclientid_res *res)
3764 nfs4_verifier sc_verifier;
3765 struct nfs4_setclientid setclientid = {
3766 .sc_verifier = &sc_verifier,
3767 .sc_prog = program,
3768 .sc_cb_ident = clp->cl_cb_ident,
3770 struct rpc_message msg = {
3771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3772 .rpc_argp = &setclientid,
3773 .rpc_resp = res,
3774 .rpc_cred = cred,
3776 __be32 *p;
3777 int loop = 0;
3778 int status;
3780 p = (__be32*)sc_verifier.data;
3781 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3782 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3784 for(;;) {
3785 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3786 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3787 clp->cl_ipaddr,
3788 rpc_peeraddr2str(clp->cl_rpcclient,
3789 RPC_DISPLAY_ADDR),
3790 rpc_peeraddr2str(clp->cl_rpcclient,
3791 RPC_DISPLAY_PROTO),
3792 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3793 clp->cl_id_uniquifier);
3794 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3795 sizeof(setclientid.sc_netid),
3796 rpc_peeraddr2str(clp->cl_rpcclient,
3797 RPC_DISPLAY_NETID));
3798 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3799 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3800 clp->cl_ipaddr, port >> 8, port & 255);
3802 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3803 if (status != -NFS4ERR_CLID_INUSE)
3804 break;
3805 if (loop != 0) {
3806 ++clp->cl_id_uniquifier;
3807 break;
3809 ++loop;
3810 ssleep(clp->cl_lease_time / HZ + 1);
3812 return status;
3815 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3816 struct nfs4_setclientid_res *arg,
3817 struct rpc_cred *cred)
3819 struct nfs_fsinfo fsinfo;
3820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3822 .rpc_argp = arg,
3823 .rpc_resp = &fsinfo,
3824 .rpc_cred = cred,
3826 unsigned long now;
3827 int status;
3829 now = jiffies;
3830 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3831 if (status == 0) {
3832 spin_lock(&clp->cl_lock);
3833 clp->cl_lease_time = fsinfo.lease_time * HZ;
3834 clp->cl_last_renewal = now;
3835 spin_unlock(&clp->cl_lock);
3837 return status;
3840 struct nfs4_delegreturndata {
3841 struct nfs4_delegreturnargs args;
3842 struct nfs4_delegreturnres res;
3843 struct nfs_fh fh;
3844 nfs4_stateid stateid;
3845 unsigned long timestamp;
3846 struct nfs_fattr fattr;
3847 int rpc_status;
3850 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3852 struct nfs4_delegreturndata *data = calldata;
3854 if (!nfs4_sequence_done(task, &data->res.seq_res))
3855 return;
3857 switch (task->tk_status) {
3858 case -NFS4ERR_STALE_STATEID:
3859 case -NFS4ERR_EXPIRED:
3860 case 0:
3861 renew_lease(data->res.server, data->timestamp);
3862 break;
3863 default:
3864 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3865 -EAGAIN) {
3866 rpc_restart_call_prepare(task);
3867 return;
3870 data->rpc_status = task->tk_status;
3873 static void nfs4_delegreturn_release(void *calldata)
3875 kfree(calldata);
3878 #if defined(CONFIG_NFS_V4_1)
3879 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3881 struct nfs4_delegreturndata *d_data;
3883 d_data = (struct nfs4_delegreturndata *)data;
3885 if (nfs4_setup_sequence(d_data->res.server,
3886 &d_data->args.seq_args,
3887 &d_data->res.seq_res, 1, task))
3888 return;
3889 rpc_call_start(task);
3891 #endif /* CONFIG_NFS_V4_1 */
3893 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3894 #if defined(CONFIG_NFS_V4_1)
3895 .rpc_call_prepare = nfs4_delegreturn_prepare,
3896 #endif /* CONFIG_NFS_V4_1 */
3897 .rpc_call_done = nfs4_delegreturn_done,
3898 .rpc_release = nfs4_delegreturn_release,
3901 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3903 struct nfs4_delegreturndata *data;
3904 struct nfs_server *server = NFS_SERVER(inode);
3905 struct rpc_task *task;
3906 struct rpc_message msg = {
3907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3908 .rpc_cred = cred,
3910 struct rpc_task_setup task_setup_data = {
3911 .rpc_client = server->client,
3912 .rpc_message = &msg,
3913 .callback_ops = &nfs4_delegreturn_ops,
3914 .flags = RPC_TASK_ASYNC,
3916 int status = 0;
3918 data = kzalloc(sizeof(*data), GFP_NOFS);
3919 if (data == NULL)
3920 return -ENOMEM;
3921 data->args.fhandle = &data->fh;
3922 data->args.stateid = &data->stateid;
3923 data->args.bitmask = server->attr_bitmask;
3924 nfs_copy_fh(&data->fh, NFS_FH(inode));
3925 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3926 data->res.fattr = &data->fattr;
3927 data->res.server = server;
3928 nfs_fattr_init(data->res.fattr);
3929 data->timestamp = jiffies;
3930 data->rpc_status = 0;
3932 task_setup_data.callback_data = data;
3933 msg.rpc_argp = &data->args;
3934 msg.rpc_resp = &data->res;
3935 task = rpc_run_task(&task_setup_data);
3936 if (IS_ERR(task))
3937 return PTR_ERR(task);
3938 if (!issync)
3939 goto out;
3940 status = nfs4_wait_for_completion_rpc_task(task);
3941 if (status != 0)
3942 goto out;
3943 status = data->rpc_status;
3944 if (status != 0)
3945 goto out;
3946 nfs_refresh_inode(inode, &data->fattr);
3947 out:
3948 rpc_put_task(task);
3949 return status;
3952 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3954 struct nfs_server *server = NFS_SERVER(inode);
3955 struct nfs4_exception exception = { };
3956 int err;
3957 do {
3958 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3959 switch (err) {
3960 case -NFS4ERR_STALE_STATEID:
3961 case -NFS4ERR_EXPIRED:
3962 case 0:
3963 return 0;
3965 err = nfs4_handle_exception(server, err, &exception);
3966 } while (exception.retry);
3967 return err;
3970 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3971 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3974 * sleep, with exponential backoff, and retry the LOCK operation.
3976 static unsigned long
3977 nfs4_set_lock_task_retry(unsigned long timeout)
3979 schedule_timeout_killable(timeout);
3980 timeout <<= 1;
3981 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3982 return NFS4_LOCK_MAXTIMEOUT;
3983 return timeout;
3986 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3988 struct inode *inode = state->inode;
3989 struct nfs_server *server = NFS_SERVER(inode);
3990 struct nfs_client *clp = server->nfs_client;
3991 struct nfs_lockt_args arg = {
3992 .fh = NFS_FH(inode),
3993 .fl = request,
3995 struct nfs_lockt_res res = {
3996 .denied = request,
3998 struct rpc_message msg = {
3999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4000 .rpc_argp = &arg,
4001 .rpc_resp = &res,
4002 .rpc_cred = state->owner->so_cred,
4004 struct nfs4_lock_state *lsp;
4005 int status;
4007 arg.lock_owner.clientid = clp->cl_clientid;
4008 status = nfs4_set_lock_state(state, request);
4009 if (status != 0)
4010 goto out;
4011 lsp = request->fl_u.nfs4_fl.owner;
4012 arg.lock_owner.id = lsp->ls_id.id;
4013 arg.lock_owner.s_dev = server->s_dev;
4014 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4015 switch (status) {
4016 case 0:
4017 request->fl_type = F_UNLCK;
4018 break;
4019 case -NFS4ERR_DENIED:
4020 status = 0;
4022 request->fl_ops->fl_release_private(request);
4023 out:
4024 return status;
4027 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4029 struct nfs4_exception exception = { };
4030 int err;
4032 do {
4033 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4034 _nfs4_proc_getlk(state, cmd, request),
4035 &exception);
4036 } while (exception.retry);
4037 return err;
4040 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4042 int res = 0;
4043 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4044 case FL_POSIX:
4045 res = posix_lock_file_wait(file, fl);
4046 break;
4047 case FL_FLOCK:
4048 res = flock_lock_file_wait(file, fl);
4049 break;
4050 default:
4051 BUG();
4053 return res;
4056 struct nfs4_unlockdata {
4057 struct nfs_locku_args arg;
4058 struct nfs_locku_res res;
4059 struct nfs4_lock_state *lsp;
4060 struct nfs_open_context *ctx;
4061 struct file_lock fl;
4062 const struct nfs_server *server;
4063 unsigned long timestamp;
4066 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4067 struct nfs_open_context *ctx,
4068 struct nfs4_lock_state *lsp,
4069 struct nfs_seqid *seqid)
4071 struct nfs4_unlockdata *p;
4072 struct inode *inode = lsp->ls_state->inode;
4074 p = kzalloc(sizeof(*p), GFP_NOFS);
4075 if (p == NULL)
4076 return NULL;
4077 p->arg.fh = NFS_FH(inode);
4078 p->arg.fl = &p->fl;
4079 p->arg.seqid = seqid;
4080 p->res.seqid = seqid;
4081 p->arg.stateid = &lsp->ls_stateid;
4082 p->lsp = lsp;
4083 atomic_inc(&lsp->ls_count);
4084 /* Ensure we don't close file until we're done freeing locks! */
4085 p->ctx = get_nfs_open_context(ctx);
4086 memcpy(&p->fl, fl, sizeof(p->fl));
4087 p->server = NFS_SERVER(inode);
4088 return p;
4091 static void nfs4_locku_release_calldata(void *data)
4093 struct nfs4_unlockdata *calldata = data;
4094 nfs_free_seqid(calldata->arg.seqid);
4095 nfs4_put_lock_state(calldata->lsp);
4096 put_nfs_open_context(calldata->ctx);
4097 kfree(calldata);
4100 static void nfs4_locku_done(struct rpc_task *task, void *data)
4102 struct nfs4_unlockdata *calldata = data;
4104 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4105 return;
4106 switch (task->tk_status) {
4107 case 0:
4108 memcpy(calldata->lsp->ls_stateid.data,
4109 calldata->res.stateid.data,
4110 sizeof(calldata->lsp->ls_stateid.data));
4111 renew_lease(calldata->server, calldata->timestamp);
4112 break;
4113 case -NFS4ERR_BAD_STATEID:
4114 case -NFS4ERR_OLD_STATEID:
4115 case -NFS4ERR_STALE_STATEID:
4116 case -NFS4ERR_EXPIRED:
4117 break;
4118 default:
4119 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4120 rpc_restart_call_prepare(task);
4124 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4126 struct nfs4_unlockdata *calldata = data;
4128 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4129 return;
4130 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4131 /* Note: exit _without_ running nfs4_locku_done */
4132 task->tk_action = NULL;
4133 return;
4135 calldata->timestamp = jiffies;
4136 if (nfs4_setup_sequence(calldata->server,
4137 &calldata->arg.seq_args,
4138 &calldata->res.seq_res, 1, task))
4139 return;
4140 rpc_call_start(task);
4143 static const struct rpc_call_ops nfs4_locku_ops = {
4144 .rpc_call_prepare = nfs4_locku_prepare,
4145 .rpc_call_done = nfs4_locku_done,
4146 .rpc_release = nfs4_locku_release_calldata,
4149 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4150 struct nfs_open_context *ctx,
4151 struct nfs4_lock_state *lsp,
4152 struct nfs_seqid *seqid)
4154 struct nfs4_unlockdata *data;
4155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4157 .rpc_cred = ctx->cred,
4159 struct rpc_task_setup task_setup_data = {
4160 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4161 .rpc_message = &msg,
4162 .callback_ops = &nfs4_locku_ops,
4163 .workqueue = nfsiod_workqueue,
4164 .flags = RPC_TASK_ASYNC,
4167 /* Ensure this is an unlock - when canceling a lock, the
4168 * canceled lock is passed in, and it won't be an unlock.
4170 fl->fl_type = F_UNLCK;
4172 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4173 if (data == NULL) {
4174 nfs_free_seqid(seqid);
4175 return ERR_PTR(-ENOMEM);
4178 msg.rpc_argp = &data->arg;
4179 msg.rpc_resp = &data->res;
4180 task_setup_data.callback_data = data;
4181 return rpc_run_task(&task_setup_data);
4184 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4186 struct nfs_inode *nfsi = NFS_I(state->inode);
4187 struct nfs_seqid *seqid;
4188 struct nfs4_lock_state *lsp;
4189 struct rpc_task *task;
4190 int status = 0;
4191 unsigned char fl_flags = request->fl_flags;
4193 status = nfs4_set_lock_state(state, request);
4194 /* Unlock _before_ we do the RPC call */
4195 request->fl_flags |= FL_EXISTS;
4196 down_read(&nfsi->rwsem);
4197 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4198 up_read(&nfsi->rwsem);
4199 goto out;
4201 up_read(&nfsi->rwsem);
4202 if (status != 0)
4203 goto out;
4204 /* Is this a delegated lock? */
4205 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4206 goto out;
4207 lsp = request->fl_u.nfs4_fl.owner;
4208 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4209 status = -ENOMEM;
4210 if (seqid == NULL)
4211 goto out;
4212 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4213 status = PTR_ERR(task);
4214 if (IS_ERR(task))
4215 goto out;
4216 status = nfs4_wait_for_completion_rpc_task(task);
4217 rpc_put_task(task);
4218 out:
4219 request->fl_flags = fl_flags;
4220 return status;
4223 struct nfs4_lockdata {
4224 struct nfs_lock_args arg;
4225 struct nfs_lock_res res;
4226 struct nfs4_lock_state *lsp;
4227 struct nfs_open_context *ctx;
4228 struct file_lock fl;
4229 unsigned long timestamp;
4230 int rpc_status;
4231 int cancelled;
4232 struct nfs_server *server;
4235 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4236 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4237 gfp_t gfp_mask)
4239 struct nfs4_lockdata *p;
4240 struct inode *inode = lsp->ls_state->inode;
4241 struct nfs_server *server = NFS_SERVER(inode);
4243 p = kzalloc(sizeof(*p), gfp_mask);
4244 if (p == NULL)
4245 return NULL;
4247 p->arg.fh = NFS_FH(inode);
4248 p->arg.fl = &p->fl;
4249 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4250 if (p->arg.open_seqid == NULL)
4251 goto out_free;
4252 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4253 if (p->arg.lock_seqid == NULL)
4254 goto out_free_seqid;
4255 p->arg.lock_stateid = &lsp->ls_stateid;
4256 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4257 p->arg.lock_owner.id = lsp->ls_id.id;
4258 p->arg.lock_owner.s_dev = server->s_dev;
4259 p->res.lock_seqid = p->arg.lock_seqid;
4260 p->lsp = lsp;
4261 p->server = server;
4262 atomic_inc(&lsp->ls_count);
4263 p->ctx = get_nfs_open_context(ctx);
4264 memcpy(&p->fl, fl, sizeof(p->fl));
4265 return p;
4266 out_free_seqid:
4267 nfs_free_seqid(p->arg.open_seqid);
4268 out_free:
4269 kfree(p);
4270 return NULL;
4273 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4275 struct nfs4_lockdata *data = calldata;
4276 struct nfs4_state *state = data->lsp->ls_state;
4278 dprintk("%s: begin!\n", __func__);
4279 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4280 return;
4281 /* Do we need to do an open_to_lock_owner? */
4282 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4283 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4284 return;
4285 data->arg.open_stateid = &state->stateid;
4286 data->arg.new_lock_owner = 1;
4287 data->res.open_seqid = data->arg.open_seqid;
4288 } else
4289 data->arg.new_lock_owner = 0;
4290 data->timestamp = jiffies;
4291 if (nfs4_setup_sequence(data->server,
4292 &data->arg.seq_args,
4293 &data->res.seq_res, 1, task))
4294 return;
4295 rpc_call_start(task);
4296 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4299 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4301 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4302 nfs4_lock_prepare(task, calldata);
4305 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4307 struct nfs4_lockdata *data = calldata;
4309 dprintk("%s: begin!\n", __func__);
4311 if (!nfs4_sequence_done(task, &data->res.seq_res))
4312 return;
4314 data->rpc_status = task->tk_status;
4315 if (data->arg.new_lock_owner != 0) {
4316 if (data->rpc_status == 0)
4317 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4318 else
4319 goto out;
4321 if (data->rpc_status == 0) {
4322 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4323 sizeof(data->lsp->ls_stateid.data));
4324 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4325 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4327 out:
4328 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4331 static void nfs4_lock_release(void *calldata)
4333 struct nfs4_lockdata *data = calldata;
4335 dprintk("%s: begin!\n", __func__);
4336 nfs_free_seqid(data->arg.open_seqid);
4337 if (data->cancelled != 0) {
4338 struct rpc_task *task;
4339 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4340 data->arg.lock_seqid);
4341 if (!IS_ERR(task))
4342 rpc_put_task_async(task);
4343 dprintk("%s: cancelling lock!\n", __func__);
4344 } else
4345 nfs_free_seqid(data->arg.lock_seqid);
4346 nfs4_put_lock_state(data->lsp);
4347 put_nfs_open_context(data->ctx);
4348 kfree(data);
4349 dprintk("%s: done!\n", __func__);
4352 static const struct rpc_call_ops nfs4_lock_ops = {
4353 .rpc_call_prepare = nfs4_lock_prepare,
4354 .rpc_call_done = nfs4_lock_done,
4355 .rpc_release = nfs4_lock_release,
4358 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4359 .rpc_call_prepare = nfs4_recover_lock_prepare,
4360 .rpc_call_done = nfs4_lock_done,
4361 .rpc_release = nfs4_lock_release,
4364 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4366 switch (error) {
4367 case -NFS4ERR_ADMIN_REVOKED:
4368 case -NFS4ERR_BAD_STATEID:
4369 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4370 if (new_lock_owner != 0 ||
4371 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4372 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4373 break;
4374 case -NFS4ERR_STALE_STATEID:
4375 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4376 case -NFS4ERR_EXPIRED:
4377 nfs4_schedule_lease_recovery(server->nfs_client);
4381 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4383 struct nfs4_lockdata *data;
4384 struct rpc_task *task;
4385 struct rpc_message msg = {
4386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4387 .rpc_cred = state->owner->so_cred,
4389 struct rpc_task_setup task_setup_data = {
4390 .rpc_client = NFS_CLIENT(state->inode),
4391 .rpc_message = &msg,
4392 .callback_ops = &nfs4_lock_ops,
4393 .workqueue = nfsiod_workqueue,
4394 .flags = RPC_TASK_ASYNC,
4396 int ret;
4398 dprintk("%s: begin!\n", __func__);
4399 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4400 fl->fl_u.nfs4_fl.owner,
4401 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4402 if (data == NULL)
4403 return -ENOMEM;
4404 if (IS_SETLKW(cmd))
4405 data->arg.block = 1;
4406 if (recovery_type > NFS_LOCK_NEW) {
4407 if (recovery_type == NFS_LOCK_RECLAIM)
4408 data->arg.reclaim = NFS_LOCK_RECLAIM;
4409 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4411 msg.rpc_argp = &data->arg;
4412 msg.rpc_resp = &data->res;
4413 task_setup_data.callback_data = data;
4414 task = rpc_run_task(&task_setup_data);
4415 if (IS_ERR(task))
4416 return PTR_ERR(task);
4417 ret = nfs4_wait_for_completion_rpc_task(task);
4418 if (ret == 0) {
4419 ret = data->rpc_status;
4420 if (ret)
4421 nfs4_handle_setlk_error(data->server, data->lsp,
4422 data->arg.new_lock_owner, ret);
4423 } else
4424 data->cancelled = 1;
4425 rpc_put_task(task);
4426 dprintk("%s: done, ret = %d!\n", __func__, ret);
4427 return ret;
4430 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4432 struct nfs_server *server = NFS_SERVER(state->inode);
4433 struct nfs4_exception exception = { };
4434 int err;
4436 do {
4437 /* Cache the lock if possible... */
4438 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4439 return 0;
4440 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4441 if (err != -NFS4ERR_DELAY)
4442 break;
4443 nfs4_handle_exception(server, err, &exception);
4444 } while (exception.retry);
4445 return err;
4448 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4450 struct nfs_server *server = NFS_SERVER(state->inode);
4451 struct nfs4_exception exception = { };
4452 int err;
4454 err = nfs4_set_lock_state(state, request);
4455 if (err != 0)
4456 return err;
4457 do {
4458 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4459 return 0;
4460 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4461 switch (err) {
4462 default:
4463 goto out;
4464 case -NFS4ERR_GRACE:
4465 case -NFS4ERR_DELAY:
4466 nfs4_handle_exception(server, err, &exception);
4467 err = 0;
4469 } while (exception.retry);
4470 out:
4471 return err;
4474 #if defined(CONFIG_NFS_V4_1)
4475 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4477 int status;
4478 struct nfs_server *server = NFS_SERVER(state->inode);
4480 status = nfs41_test_stateid(server, state);
4481 if (status == NFS_OK)
4482 return 0;
4483 nfs41_free_stateid(server, state);
4484 return nfs4_lock_expired(state, request);
4486 #endif
4488 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4490 struct nfs_inode *nfsi = NFS_I(state->inode);
4491 unsigned char fl_flags = request->fl_flags;
4492 int status = -ENOLCK;
4494 if ((fl_flags & FL_POSIX) &&
4495 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4496 goto out;
4497 /* Is this a delegated open? */
4498 status = nfs4_set_lock_state(state, request);
4499 if (status != 0)
4500 goto out;
4501 request->fl_flags |= FL_ACCESS;
4502 status = do_vfs_lock(request->fl_file, request);
4503 if (status < 0)
4504 goto out;
4505 down_read(&nfsi->rwsem);
4506 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4507 /* Yes: cache locks! */
4508 /* ...but avoid races with delegation recall... */
4509 request->fl_flags = fl_flags & ~FL_SLEEP;
4510 status = do_vfs_lock(request->fl_file, request);
4511 goto out_unlock;
4513 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4514 if (status != 0)
4515 goto out_unlock;
4516 /* Note: we always want to sleep here! */
4517 request->fl_flags = fl_flags | FL_SLEEP;
4518 if (do_vfs_lock(request->fl_file, request) < 0)
4519 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4520 out_unlock:
4521 up_read(&nfsi->rwsem);
4522 out:
4523 request->fl_flags = fl_flags;
4524 return status;
4527 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4529 struct nfs4_exception exception = { };
4530 int err;
4532 do {
4533 err = _nfs4_proc_setlk(state, cmd, request);
4534 if (err == -NFS4ERR_DENIED)
4535 err = -EAGAIN;
4536 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4537 err, &exception);
4538 } while (exception.retry);
4539 return err;
4542 static int
4543 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4545 struct nfs_open_context *ctx;
4546 struct nfs4_state *state;
4547 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4548 int status;
4550 /* verify open state */
4551 ctx = nfs_file_open_context(filp);
4552 state = ctx->state;
4554 if (request->fl_start < 0 || request->fl_end < 0)
4555 return -EINVAL;
4557 if (IS_GETLK(cmd)) {
4558 if (state != NULL)
4559 return nfs4_proc_getlk(state, F_GETLK, request);
4560 return 0;
4563 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4564 return -EINVAL;
4566 if (request->fl_type == F_UNLCK) {
4567 if (state != NULL)
4568 return nfs4_proc_unlck(state, cmd, request);
4569 return 0;
4572 if (state == NULL)
4573 return -ENOLCK;
4574 do {
4575 status = nfs4_proc_setlk(state, cmd, request);
4576 if ((status != -EAGAIN) || IS_SETLK(cmd))
4577 break;
4578 timeout = nfs4_set_lock_task_retry(timeout);
4579 status = -ERESTARTSYS;
4580 if (signalled())
4581 break;
4582 } while(status < 0);
4583 return status;
4586 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4588 struct nfs_server *server = NFS_SERVER(state->inode);
4589 struct nfs4_exception exception = { };
4590 int err;
4592 err = nfs4_set_lock_state(state, fl);
4593 if (err != 0)
4594 goto out;
4595 do {
4596 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4597 switch (err) {
4598 default:
4599 printk(KERN_ERR "%s: unhandled error %d.\n",
4600 __func__, err);
4601 case 0:
4602 case -ESTALE:
4603 goto out;
4604 case -NFS4ERR_EXPIRED:
4605 nfs4_schedule_stateid_recovery(server, state);
4606 case -NFS4ERR_STALE_CLIENTID:
4607 case -NFS4ERR_STALE_STATEID:
4608 nfs4_schedule_lease_recovery(server->nfs_client);
4609 goto out;
4610 case -NFS4ERR_BADSESSION:
4611 case -NFS4ERR_BADSLOT:
4612 case -NFS4ERR_BAD_HIGH_SLOT:
4613 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4614 case -NFS4ERR_DEADSESSION:
4615 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4616 goto out;
4617 case -ERESTARTSYS:
4619 * The show must go on: exit, but mark the
4620 * stateid as needing recovery.
4622 case -NFS4ERR_ADMIN_REVOKED:
4623 case -NFS4ERR_BAD_STATEID:
4624 case -NFS4ERR_OPENMODE:
4625 nfs4_schedule_stateid_recovery(server, state);
4626 err = 0;
4627 goto out;
4628 case -EKEYEXPIRED:
4630 * User RPCSEC_GSS context has expired.
4631 * We cannot recover this stateid now, so
4632 * skip it and allow recovery thread to
4633 * proceed.
4635 err = 0;
4636 goto out;
4637 case -ENOMEM:
4638 case -NFS4ERR_DENIED:
4639 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4640 err = 0;
4641 goto out;
4642 case -NFS4ERR_DELAY:
4643 break;
4645 err = nfs4_handle_exception(server, err, &exception);
4646 } while (exception.retry);
4647 out:
4648 return err;
4651 static void nfs4_release_lockowner_release(void *calldata)
4653 kfree(calldata);
4656 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4657 .rpc_release = nfs4_release_lockowner_release,
4660 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4662 struct nfs_server *server = lsp->ls_state->owner->so_server;
4663 struct nfs_release_lockowner_args *args;
4664 struct rpc_message msg = {
4665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4668 if (server->nfs_client->cl_mvops->minor_version != 0)
4669 return;
4670 args = kmalloc(sizeof(*args), GFP_NOFS);
4671 if (!args)
4672 return;
4673 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4674 args->lock_owner.id = lsp->ls_id.id;
4675 args->lock_owner.s_dev = server->s_dev;
4676 msg.rpc_argp = args;
4677 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4680 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4682 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4683 const void *buf, size_t buflen,
4684 int flags, int type)
4686 if (strcmp(key, "") != 0)
4687 return -EINVAL;
4689 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4692 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4693 void *buf, size_t buflen, int type)
4695 if (strcmp(key, "") != 0)
4696 return -EINVAL;
4698 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4701 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4702 size_t list_len, const char *name,
4703 size_t name_len, int type)
4705 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4707 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4708 return 0;
4710 if (list && len <= list_len)
4711 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4712 return len;
4716 * nfs_fhget will use either the mounted_on_fileid or the fileid
4718 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4720 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4721 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4722 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4723 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4724 return;
4726 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4727 NFS_ATTR_FATTR_NLINK;
4728 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4729 fattr->nlink = 2;
4732 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4733 struct nfs4_fs_locations *fs_locations, struct page *page)
4735 struct nfs_server *server = NFS_SERVER(dir);
4736 u32 bitmask[2] = {
4737 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4739 struct nfs4_fs_locations_arg args = {
4740 .dir_fh = NFS_FH(dir),
4741 .name = name,
4742 .page = page,
4743 .bitmask = bitmask,
4745 struct nfs4_fs_locations_res res = {
4746 .fs_locations = fs_locations,
4748 struct rpc_message msg = {
4749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4750 .rpc_argp = &args,
4751 .rpc_resp = &res,
4753 int status;
4755 dprintk("%s: start\n", __func__);
4757 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4758 * is not supported */
4759 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4760 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4761 else
4762 bitmask[0] |= FATTR4_WORD0_FILEID;
4764 nfs_fattr_init(&fs_locations->fattr);
4765 fs_locations->server = server;
4766 fs_locations->nlocations = 0;
4767 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4768 dprintk("%s: returned status = %d\n", __func__, status);
4769 return status;
4772 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4774 int status;
4775 struct nfs4_secinfo_arg args = {
4776 .dir_fh = NFS_FH(dir),
4777 .name = name,
4779 struct nfs4_secinfo_res res = {
4780 .flavors = flavors,
4782 struct rpc_message msg = {
4783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4784 .rpc_argp = &args,
4785 .rpc_resp = &res,
4788 dprintk("NFS call secinfo %s\n", name->name);
4789 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4790 dprintk("NFS reply secinfo: %d\n", status);
4791 return status;
4794 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4796 struct nfs4_exception exception = { };
4797 int err;
4798 do {
4799 err = nfs4_handle_exception(NFS_SERVER(dir),
4800 _nfs4_proc_secinfo(dir, name, flavors),
4801 &exception);
4802 } while (exception.retry);
4803 return err;
4806 #ifdef CONFIG_NFS_V4_1
4808 * Check the exchange flags returned by the server for invalid flags, having
4809 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4810 * DS flags set.
4812 static int nfs4_check_cl_exchange_flags(u32 flags)
4814 if (flags & ~EXCHGID4_FLAG_MASK_R)
4815 goto out_inval;
4816 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4817 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4818 goto out_inval;
4819 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4820 goto out_inval;
4821 return NFS_OK;
4822 out_inval:
4823 return -NFS4ERR_INVAL;
4826 static bool
4827 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4829 if (a->server_scope_sz == b->server_scope_sz &&
4830 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4831 return true;
4833 return false;
4837 * nfs4_proc_exchange_id()
4839 * Since the clientid has expired, all compounds using sessions
4840 * associated with the stale clientid will be returning
4841 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4842 * be in some phase of session reset.
4844 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4846 nfs4_verifier verifier;
4847 struct nfs41_exchange_id_args args = {
4848 .client = clp,
4849 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4851 struct nfs41_exchange_id_res res = {
4852 .client = clp,
4854 int status;
4855 struct rpc_message msg = {
4856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4857 .rpc_argp = &args,
4858 .rpc_resp = &res,
4859 .rpc_cred = cred,
4861 __be32 *p;
4863 dprintk("--> %s\n", __func__);
4864 BUG_ON(clp == NULL);
4866 p = (u32 *)verifier.data;
4867 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4868 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4869 args.verifier = &verifier;
4871 args.id_len = scnprintf(args.id, sizeof(args.id),
4872 "%s/%s.%s/%u",
4873 clp->cl_ipaddr,
4874 init_utsname()->nodename,
4875 init_utsname()->domainname,
4876 clp->cl_rpcclient->cl_auth->au_flavor);
4878 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4879 if (unlikely(!res.server_scope))
4880 return -ENOMEM;
4882 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4883 if (!status)
4884 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4886 if (!status) {
4887 if (clp->server_scope &&
4888 !nfs41_same_server_scope(clp->server_scope,
4889 res.server_scope)) {
4890 dprintk("%s: server_scope mismatch detected\n",
4891 __func__);
4892 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4893 kfree(clp->server_scope);
4894 clp->server_scope = NULL;
4897 if (!clp->server_scope)
4898 clp->server_scope = res.server_scope;
4899 else
4900 kfree(res.server_scope);
4903 dprintk("<-- %s status= %d\n", __func__, status);
4904 return status;
4907 struct nfs4_get_lease_time_data {
4908 struct nfs4_get_lease_time_args *args;
4909 struct nfs4_get_lease_time_res *res;
4910 struct nfs_client *clp;
4913 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4914 void *calldata)
4916 int ret;
4917 struct nfs4_get_lease_time_data *data =
4918 (struct nfs4_get_lease_time_data *)calldata;
4920 dprintk("--> %s\n", __func__);
4921 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4922 /* just setup sequence, do not trigger session recovery
4923 since we're invoked within one */
4924 ret = nfs41_setup_sequence(data->clp->cl_session,
4925 &data->args->la_seq_args,
4926 &data->res->lr_seq_res, 0, task);
4928 BUG_ON(ret == -EAGAIN);
4929 rpc_call_start(task);
4930 dprintk("<-- %s\n", __func__);
4934 * Called from nfs4_state_manager thread for session setup, so don't recover
4935 * from sequence operation or clientid errors.
4937 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4939 struct nfs4_get_lease_time_data *data =
4940 (struct nfs4_get_lease_time_data *)calldata;
4942 dprintk("--> %s\n", __func__);
4943 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4944 return;
4945 switch (task->tk_status) {
4946 case -NFS4ERR_DELAY:
4947 case -NFS4ERR_GRACE:
4948 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4949 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4950 task->tk_status = 0;
4951 /* fall through */
4952 case -NFS4ERR_RETRY_UNCACHED_REP:
4953 rpc_restart_call_prepare(task);
4954 return;
4956 dprintk("<-- %s\n", __func__);
4959 struct rpc_call_ops nfs4_get_lease_time_ops = {
4960 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4961 .rpc_call_done = nfs4_get_lease_time_done,
4964 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4966 struct rpc_task *task;
4967 struct nfs4_get_lease_time_args args;
4968 struct nfs4_get_lease_time_res res = {
4969 .lr_fsinfo = fsinfo,
4971 struct nfs4_get_lease_time_data data = {
4972 .args = &args,
4973 .res = &res,
4974 .clp = clp,
4976 struct rpc_message msg = {
4977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4978 .rpc_argp = &args,
4979 .rpc_resp = &res,
4981 struct rpc_task_setup task_setup = {
4982 .rpc_client = clp->cl_rpcclient,
4983 .rpc_message = &msg,
4984 .callback_ops = &nfs4_get_lease_time_ops,
4985 .callback_data = &data,
4986 .flags = RPC_TASK_TIMEOUT,
4988 int status;
4990 dprintk("--> %s\n", __func__);
4991 task = rpc_run_task(&task_setup);
4993 if (IS_ERR(task))
4994 status = PTR_ERR(task);
4995 else {
4996 status = task->tk_status;
4997 rpc_put_task(task);
4999 dprintk("<-- %s return %d\n", __func__, status);
5001 return status;
5005 * Reset a slot table
5007 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5008 int ivalue)
5010 struct nfs4_slot *new = NULL;
5011 int i;
5012 int ret = 0;
5014 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5015 max_reqs, tbl->max_slots);
5017 /* Does the newly negotiated max_reqs match the existing slot table? */
5018 if (max_reqs != tbl->max_slots) {
5019 ret = -ENOMEM;
5020 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
5021 GFP_NOFS);
5022 if (!new)
5023 goto out;
5024 ret = 0;
5025 kfree(tbl->slots);
5027 spin_lock(&tbl->slot_tbl_lock);
5028 if (new) {
5029 tbl->slots = new;
5030 tbl->max_slots = max_reqs;
5032 for (i = 0; i < tbl->max_slots; ++i)
5033 tbl->slots[i].seq_nr = ivalue;
5034 spin_unlock(&tbl->slot_tbl_lock);
5035 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5036 tbl, tbl->slots, tbl->max_slots);
5037 out:
5038 dprintk("<-- %s: return %d\n", __func__, ret);
5039 return ret;
5043 * Reset the forechannel and backchannel slot tables
5045 static int nfs4_reset_slot_tables(struct nfs4_session *session)
5047 int status;
5049 status = nfs4_reset_slot_table(&session->fc_slot_table,
5050 session->fc_attrs.max_reqs, 1);
5051 if (status)
5052 return status;
5054 status = nfs4_reset_slot_table(&session->bc_slot_table,
5055 session->bc_attrs.max_reqs, 0);
5056 return status;
5059 /* Destroy the slot table */
5060 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5062 if (session->fc_slot_table.slots != NULL) {
5063 kfree(session->fc_slot_table.slots);
5064 session->fc_slot_table.slots = NULL;
5066 if (session->bc_slot_table.slots != NULL) {
5067 kfree(session->bc_slot_table.slots);
5068 session->bc_slot_table.slots = NULL;
5070 return;
5074 * Initialize slot table
5076 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5077 int max_slots, int ivalue)
5079 struct nfs4_slot *slot;
5080 int ret = -ENOMEM;
5082 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5084 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
5086 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
5087 if (!slot)
5088 goto out;
5089 ret = 0;
5091 spin_lock(&tbl->slot_tbl_lock);
5092 tbl->max_slots = max_slots;
5093 tbl->slots = slot;
5094 tbl->highest_used_slotid = -1; /* no slot is currently used */
5095 spin_unlock(&tbl->slot_tbl_lock);
5096 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5097 tbl, tbl->slots, tbl->max_slots);
5098 out:
5099 dprintk("<-- %s: return %d\n", __func__, ret);
5100 return ret;
5104 * Initialize the forechannel and backchannel tables
5106 static int nfs4_init_slot_tables(struct nfs4_session *session)
5108 struct nfs4_slot_table *tbl;
5109 int status = 0;
5111 tbl = &session->fc_slot_table;
5112 if (tbl->slots == NULL) {
5113 status = nfs4_init_slot_table(tbl,
5114 session->fc_attrs.max_reqs, 1);
5115 if (status)
5116 return status;
5119 tbl = &session->bc_slot_table;
5120 if (tbl->slots == NULL) {
5121 status = nfs4_init_slot_table(tbl,
5122 session->bc_attrs.max_reqs, 0);
5123 if (status)
5124 nfs4_destroy_slot_tables(session);
5127 return status;
5130 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5132 struct nfs4_session *session;
5133 struct nfs4_slot_table *tbl;
5135 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5136 if (!session)
5137 return NULL;
5139 tbl = &session->fc_slot_table;
5140 tbl->highest_used_slotid = -1;
5141 spin_lock_init(&tbl->slot_tbl_lock);
5142 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5143 init_completion(&tbl->complete);
5145 tbl = &session->bc_slot_table;
5146 tbl->highest_used_slotid = -1;
5147 spin_lock_init(&tbl->slot_tbl_lock);
5148 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5149 init_completion(&tbl->complete);
5151 session->session_state = 1<<NFS4_SESSION_INITING;
5153 session->clp = clp;
5154 return session;
5157 void nfs4_destroy_session(struct nfs4_session *session)
5159 nfs4_proc_destroy_session(session);
5160 dprintk("%s Destroy backchannel for xprt %p\n",
5161 __func__, session->clp->cl_rpcclient->cl_xprt);
5162 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5163 NFS41_BC_MIN_CALLBACKS);
5164 nfs4_destroy_slot_tables(session);
5165 kfree(session);
5169 * Initialize the values to be used by the client in CREATE_SESSION
5170 * If nfs4_init_session set the fore channel request and response sizes,
5171 * use them.
5173 * Set the back channel max_resp_sz_cached to zero to force the client to
5174 * always set csa_cachethis to FALSE because the current implementation
5175 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5177 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5179 struct nfs4_session *session = args->client->cl_session;
5180 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5181 mxresp_sz = session->fc_attrs.max_resp_sz;
5183 if (mxrqst_sz == 0)
5184 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5185 if (mxresp_sz == 0)
5186 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5187 /* Fore channel attributes */
5188 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5189 args->fc_attrs.max_resp_sz = mxresp_sz;
5190 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5191 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5193 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5194 "max_ops=%u max_reqs=%u\n",
5195 __func__,
5196 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5197 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5199 /* Back channel attributes */
5200 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5201 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5202 args->bc_attrs.max_resp_sz_cached = 0;
5203 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5204 args->bc_attrs.max_reqs = 1;
5206 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5207 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5208 __func__,
5209 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5210 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5211 args->bc_attrs.max_reqs);
5214 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5216 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5217 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5219 if (rcvd->max_resp_sz > sent->max_resp_sz)
5220 return -EINVAL;
5222 * Our requested max_ops is the minimum we need; we're not
5223 * prepared to break up compounds into smaller pieces than that.
5224 * So, no point even trying to continue if the server won't
5225 * cooperate:
5227 if (rcvd->max_ops < sent->max_ops)
5228 return -EINVAL;
5229 if (rcvd->max_reqs == 0)
5230 return -EINVAL;
5231 return 0;
5234 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5236 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5237 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5239 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5240 return -EINVAL;
5241 if (rcvd->max_resp_sz < sent->max_resp_sz)
5242 return -EINVAL;
5243 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5244 return -EINVAL;
5245 /* These would render the backchannel useless: */
5246 if (rcvd->max_ops == 0)
5247 return -EINVAL;
5248 if (rcvd->max_reqs == 0)
5249 return -EINVAL;
5250 return 0;
5253 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5254 struct nfs4_session *session)
5256 int ret;
5258 ret = nfs4_verify_fore_channel_attrs(args, session);
5259 if (ret)
5260 return ret;
5261 return nfs4_verify_back_channel_attrs(args, session);
5264 static int _nfs4_proc_create_session(struct nfs_client *clp)
5266 struct nfs4_session *session = clp->cl_session;
5267 struct nfs41_create_session_args args = {
5268 .client = clp,
5269 .cb_program = NFS4_CALLBACK,
5271 struct nfs41_create_session_res res = {
5272 .client = clp,
5274 struct rpc_message msg = {
5275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5276 .rpc_argp = &args,
5277 .rpc_resp = &res,
5279 int status;
5281 nfs4_init_channel_attrs(&args);
5282 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5284 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5286 if (!status)
5287 /* Verify the session's negotiated channel_attrs values */
5288 status = nfs4_verify_channel_attrs(&args, session);
5289 if (!status) {
5290 /* Increment the clientid slot sequence id */
5291 clp->cl_seqid++;
5294 return status;
5298 * Issues a CREATE_SESSION operation to the server.
5299 * It is the responsibility of the caller to verify the session is
5300 * expired before calling this routine.
5302 int nfs4_proc_create_session(struct nfs_client *clp)
5304 int status;
5305 unsigned *ptr;
5306 struct nfs4_session *session = clp->cl_session;
5308 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5310 status = _nfs4_proc_create_session(clp);
5311 if (status)
5312 goto out;
5314 /* Init and reset the fore channel */
5315 status = nfs4_init_slot_tables(session);
5316 dprintk("slot table initialization returned %d\n", status);
5317 if (status)
5318 goto out;
5319 status = nfs4_reset_slot_tables(session);
5320 dprintk("slot table reset returned %d\n", status);
5321 if (status)
5322 goto out;
5324 ptr = (unsigned *)&session->sess_id.data[0];
5325 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5326 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5327 out:
5328 dprintk("<-- %s\n", __func__);
5329 return status;
5333 * Issue the over-the-wire RPC DESTROY_SESSION.
5334 * The caller must serialize access to this routine.
5336 int nfs4_proc_destroy_session(struct nfs4_session *session)
5338 int status = 0;
5339 struct rpc_message msg;
5341 dprintk("--> nfs4_proc_destroy_session\n");
5343 /* session is still being setup */
5344 if (session->clp->cl_cons_state != NFS_CS_READY)
5345 return status;
5347 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5348 msg.rpc_argp = session;
5349 msg.rpc_resp = NULL;
5350 msg.rpc_cred = NULL;
5351 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5353 if (status)
5354 printk(KERN_WARNING
5355 "Got error %d from the server on DESTROY_SESSION. "
5356 "Session has been destroyed regardless...\n", status);
5358 dprintk("<-- nfs4_proc_destroy_session\n");
5359 return status;
5362 int nfs4_init_session(struct nfs_server *server)
5364 struct nfs_client *clp = server->nfs_client;
5365 struct nfs4_session *session;
5366 unsigned int rsize, wsize;
5367 int ret;
5369 if (!nfs4_has_session(clp))
5370 return 0;
5372 session = clp->cl_session;
5373 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5374 return 0;
5376 rsize = server->rsize;
5377 if (rsize == 0)
5378 rsize = NFS_MAX_FILE_IO_SIZE;
5379 wsize = server->wsize;
5380 if (wsize == 0)
5381 wsize = NFS_MAX_FILE_IO_SIZE;
5383 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5384 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5386 ret = nfs4_recover_expired_lease(server);
5387 if (!ret)
5388 ret = nfs4_check_client_ready(clp);
5389 return ret;
5392 int nfs4_init_ds_session(struct nfs_client *clp)
5394 struct nfs4_session *session = clp->cl_session;
5395 int ret;
5397 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5398 return 0;
5400 ret = nfs4_client_recover_expired_lease(clp);
5401 if (!ret)
5402 /* Test for the DS role */
5403 if (!is_ds_client(clp))
5404 ret = -ENODEV;
5405 if (!ret)
5406 ret = nfs4_check_client_ready(clp);
5407 return ret;
5410 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5414 * Renew the cl_session lease.
5416 struct nfs4_sequence_data {
5417 struct nfs_client *clp;
5418 struct nfs4_sequence_args args;
5419 struct nfs4_sequence_res res;
5422 static void nfs41_sequence_release(void *data)
5424 struct nfs4_sequence_data *calldata = data;
5425 struct nfs_client *clp = calldata->clp;
5427 if (atomic_read(&clp->cl_count) > 1)
5428 nfs4_schedule_state_renewal(clp);
5429 nfs_put_client(clp);
5430 kfree(calldata);
5433 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5435 switch(task->tk_status) {
5436 case -NFS4ERR_DELAY:
5437 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5438 return -EAGAIN;
5439 default:
5440 nfs4_schedule_lease_recovery(clp);
5442 return 0;
5445 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5447 struct nfs4_sequence_data *calldata = data;
5448 struct nfs_client *clp = calldata->clp;
5450 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5451 return;
5453 if (task->tk_status < 0) {
5454 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5455 if (atomic_read(&clp->cl_count) == 1)
5456 goto out;
5458 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5459 rpc_restart_call_prepare(task);
5460 return;
5463 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5464 out:
5465 dprintk("<-- %s\n", __func__);
5468 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5470 struct nfs4_sequence_data *calldata = data;
5471 struct nfs_client *clp = calldata->clp;
5472 struct nfs4_sequence_args *args;
5473 struct nfs4_sequence_res *res;
5475 args = task->tk_msg.rpc_argp;
5476 res = task->tk_msg.rpc_resp;
5478 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5479 return;
5480 rpc_call_start(task);
5483 static const struct rpc_call_ops nfs41_sequence_ops = {
5484 .rpc_call_done = nfs41_sequence_call_done,
5485 .rpc_call_prepare = nfs41_sequence_prepare,
5486 .rpc_release = nfs41_sequence_release,
5489 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5491 struct nfs4_sequence_data *calldata;
5492 struct rpc_message msg = {
5493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5494 .rpc_cred = cred,
5496 struct rpc_task_setup task_setup_data = {
5497 .rpc_client = clp->cl_rpcclient,
5498 .rpc_message = &msg,
5499 .callback_ops = &nfs41_sequence_ops,
5500 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5503 if (!atomic_inc_not_zero(&clp->cl_count))
5504 return ERR_PTR(-EIO);
5505 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5506 if (calldata == NULL) {
5507 nfs_put_client(clp);
5508 return ERR_PTR(-ENOMEM);
5510 msg.rpc_argp = &calldata->args;
5511 msg.rpc_resp = &calldata->res;
5512 calldata->clp = clp;
5513 task_setup_data.callback_data = calldata;
5515 return rpc_run_task(&task_setup_data);
5518 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5520 struct rpc_task *task;
5521 int ret = 0;
5523 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5524 return 0;
5525 task = _nfs41_proc_sequence(clp, cred);
5526 if (IS_ERR(task))
5527 ret = PTR_ERR(task);
5528 else
5529 rpc_put_task_async(task);
5530 dprintk("<-- %s status=%d\n", __func__, ret);
5531 return ret;
5534 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5536 struct rpc_task *task;
5537 int ret;
5539 task = _nfs41_proc_sequence(clp, cred);
5540 if (IS_ERR(task)) {
5541 ret = PTR_ERR(task);
5542 goto out;
5544 ret = rpc_wait_for_completion_task(task);
5545 if (!ret) {
5546 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5548 if (task->tk_status == 0)
5549 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5550 ret = task->tk_status;
5552 rpc_put_task(task);
5553 out:
5554 dprintk("<-- %s status=%d\n", __func__, ret);
5555 return ret;
5558 struct nfs4_reclaim_complete_data {
5559 struct nfs_client *clp;
5560 struct nfs41_reclaim_complete_args arg;
5561 struct nfs41_reclaim_complete_res res;
5564 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5566 struct nfs4_reclaim_complete_data *calldata = data;
5568 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5569 if (nfs41_setup_sequence(calldata->clp->cl_session,
5570 &calldata->arg.seq_args,
5571 &calldata->res.seq_res, 0, task))
5572 return;
5574 rpc_call_start(task);
5577 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5579 switch(task->tk_status) {
5580 case 0:
5581 case -NFS4ERR_COMPLETE_ALREADY:
5582 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5583 break;
5584 case -NFS4ERR_DELAY:
5585 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5586 /* fall through */
5587 case -NFS4ERR_RETRY_UNCACHED_REP:
5588 return -EAGAIN;
5589 default:
5590 nfs4_schedule_lease_recovery(clp);
5592 return 0;
5595 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5597 struct nfs4_reclaim_complete_data *calldata = data;
5598 struct nfs_client *clp = calldata->clp;
5599 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5601 dprintk("--> %s\n", __func__);
5602 if (!nfs41_sequence_done(task, res))
5603 return;
5605 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5606 rpc_restart_call_prepare(task);
5607 return;
5609 dprintk("<-- %s\n", __func__);
5612 static void nfs4_free_reclaim_complete_data(void *data)
5614 struct nfs4_reclaim_complete_data *calldata = data;
5616 kfree(calldata);
5619 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5620 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5621 .rpc_call_done = nfs4_reclaim_complete_done,
5622 .rpc_release = nfs4_free_reclaim_complete_data,
5626 * Issue a global reclaim complete.
5628 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5630 struct nfs4_reclaim_complete_data *calldata;
5631 struct rpc_task *task;
5632 struct rpc_message msg = {
5633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5635 struct rpc_task_setup task_setup_data = {
5636 .rpc_client = clp->cl_rpcclient,
5637 .rpc_message = &msg,
5638 .callback_ops = &nfs4_reclaim_complete_call_ops,
5639 .flags = RPC_TASK_ASYNC,
5641 int status = -ENOMEM;
5643 dprintk("--> %s\n", __func__);
5644 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5645 if (calldata == NULL)
5646 goto out;
5647 calldata->clp = clp;
5648 calldata->arg.one_fs = 0;
5650 msg.rpc_argp = &calldata->arg;
5651 msg.rpc_resp = &calldata->res;
5652 task_setup_data.callback_data = calldata;
5653 task = rpc_run_task(&task_setup_data);
5654 if (IS_ERR(task)) {
5655 status = PTR_ERR(task);
5656 goto out;
5658 status = nfs4_wait_for_completion_rpc_task(task);
5659 if (status == 0)
5660 status = task->tk_status;
5661 rpc_put_task(task);
5662 return 0;
5663 out:
5664 dprintk("<-- %s status=%d\n", __func__, status);
5665 return status;
5668 static void
5669 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5671 struct nfs4_layoutget *lgp = calldata;
5672 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5674 dprintk("--> %s\n", __func__);
5675 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5676 * right now covering the LAYOUTGET we are about to send.
5677 * However, that is not so catastrophic, and there seems
5678 * to be no way to prevent it completely.
5680 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5681 &lgp->res.seq_res, 0, task))
5682 return;
5683 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5684 NFS_I(lgp->args.inode)->layout,
5685 lgp->args.ctx->state)) {
5686 rpc_exit(task, NFS4_OK);
5687 return;
5689 rpc_call_start(task);
5692 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5694 struct nfs4_layoutget *lgp = calldata;
5695 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5697 dprintk("--> %s\n", __func__);
5699 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5700 return;
5702 switch (task->tk_status) {
5703 case 0:
5704 break;
5705 case -NFS4ERR_LAYOUTTRYLATER:
5706 case -NFS4ERR_RECALLCONFLICT:
5707 task->tk_status = -NFS4ERR_DELAY;
5708 /* Fall through */
5709 default:
5710 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5711 rpc_restart_call_prepare(task);
5712 return;
5715 dprintk("<-- %s\n", __func__);
5718 static void nfs4_layoutget_release(void *calldata)
5720 struct nfs4_layoutget *lgp = calldata;
5722 dprintk("--> %s\n", __func__);
5723 put_nfs_open_context(lgp->args.ctx);
5724 kfree(calldata);
5725 dprintk("<-- %s\n", __func__);
5728 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5729 .rpc_call_prepare = nfs4_layoutget_prepare,
5730 .rpc_call_done = nfs4_layoutget_done,
5731 .rpc_release = nfs4_layoutget_release,
5734 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5736 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5737 struct rpc_task *task;
5738 struct rpc_message msg = {
5739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5740 .rpc_argp = &lgp->args,
5741 .rpc_resp = &lgp->res,
5743 struct rpc_task_setup task_setup_data = {
5744 .rpc_client = server->client,
5745 .rpc_message = &msg,
5746 .callback_ops = &nfs4_layoutget_call_ops,
5747 .callback_data = lgp,
5748 .flags = RPC_TASK_ASYNC,
5750 int status = 0;
5752 dprintk("--> %s\n", __func__);
5754 lgp->res.layoutp = &lgp->args.layout;
5755 lgp->res.seq_res.sr_slot = NULL;
5756 task = rpc_run_task(&task_setup_data);
5757 if (IS_ERR(task))
5758 return PTR_ERR(task);
5759 status = nfs4_wait_for_completion_rpc_task(task);
5760 if (status == 0)
5761 status = task->tk_status;
5762 if (status == 0)
5763 status = pnfs_layout_process(lgp);
5764 rpc_put_task(task);
5765 dprintk("<-- %s status=%d\n", __func__, status);
5766 return status;
5769 static void
5770 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5772 struct nfs4_layoutreturn *lrp = calldata;
5774 dprintk("--> %s\n", __func__);
5775 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5776 &lrp->res.seq_res, 0, task))
5777 return;
5778 rpc_call_start(task);
5781 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5783 struct nfs4_layoutreturn *lrp = calldata;
5784 struct nfs_server *server;
5785 struct pnfs_layout_hdr *lo = lrp->args.layout;
5787 dprintk("--> %s\n", __func__);
5789 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5790 return;
5792 server = NFS_SERVER(lrp->args.inode);
5793 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5794 rpc_restart_call_prepare(task);
5795 return;
5797 spin_lock(&lo->plh_inode->i_lock);
5798 if (task->tk_status == 0) {
5799 if (lrp->res.lrs_present) {
5800 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5801 } else
5802 BUG_ON(!list_empty(&lo->plh_segs));
5804 lo->plh_block_lgets--;
5805 spin_unlock(&lo->plh_inode->i_lock);
5806 dprintk("<-- %s\n", __func__);
5809 static void nfs4_layoutreturn_release(void *calldata)
5811 struct nfs4_layoutreturn *lrp = calldata;
5813 dprintk("--> %s\n", __func__);
5814 put_layout_hdr(lrp->args.layout);
5815 kfree(calldata);
5816 dprintk("<-- %s\n", __func__);
5819 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5820 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5821 .rpc_call_done = nfs4_layoutreturn_done,
5822 .rpc_release = nfs4_layoutreturn_release,
5825 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5827 struct rpc_task *task;
5828 struct rpc_message msg = {
5829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5830 .rpc_argp = &lrp->args,
5831 .rpc_resp = &lrp->res,
5833 struct rpc_task_setup task_setup_data = {
5834 .rpc_client = lrp->clp->cl_rpcclient,
5835 .rpc_message = &msg,
5836 .callback_ops = &nfs4_layoutreturn_call_ops,
5837 .callback_data = lrp,
5839 int status;
5841 dprintk("--> %s\n", __func__);
5842 task = rpc_run_task(&task_setup_data);
5843 if (IS_ERR(task))
5844 return PTR_ERR(task);
5845 status = task->tk_status;
5846 dprintk("<-- %s status=%d\n", __func__, status);
5847 rpc_put_task(task);
5848 return status;
5852 * Retrieve the list of Data Server devices from the MDS.
5854 static int _nfs4_getdevicelist(struct nfs_server *server,
5855 const struct nfs_fh *fh,
5856 struct pnfs_devicelist *devlist)
5858 struct nfs4_getdevicelist_args args = {
5859 .fh = fh,
5860 .layoutclass = server->pnfs_curr_ld->id,
5862 struct nfs4_getdevicelist_res res = {
5863 .devlist = devlist,
5865 struct rpc_message msg = {
5866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5867 .rpc_argp = &args,
5868 .rpc_resp = &res,
5870 int status;
5872 dprintk("--> %s\n", __func__);
5873 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5874 &res.seq_res, 0);
5875 dprintk("<-- %s status=%d\n", __func__, status);
5876 return status;
5879 int nfs4_proc_getdevicelist(struct nfs_server *server,
5880 const struct nfs_fh *fh,
5881 struct pnfs_devicelist *devlist)
5883 struct nfs4_exception exception = { };
5884 int err;
5886 do {
5887 err = nfs4_handle_exception(server,
5888 _nfs4_getdevicelist(server, fh, devlist),
5889 &exception);
5890 } while (exception.retry);
5892 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5893 err, devlist->num_devs);
5895 return err;
5897 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5899 static int
5900 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5902 struct nfs4_getdeviceinfo_args args = {
5903 .pdev = pdev,
5905 struct nfs4_getdeviceinfo_res res = {
5906 .pdev = pdev,
5908 struct rpc_message msg = {
5909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5910 .rpc_argp = &args,
5911 .rpc_resp = &res,
5913 int status;
5915 dprintk("--> %s\n", __func__);
5916 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5917 dprintk("<-- %s status=%d\n", __func__, status);
5919 return status;
5922 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5924 struct nfs4_exception exception = { };
5925 int err;
5927 do {
5928 err = nfs4_handle_exception(server,
5929 _nfs4_proc_getdeviceinfo(server, pdev),
5930 &exception);
5931 } while (exception.retry);
5932 return err;
5934 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5936 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5938 struct nfs4_layoutcommit_data *data = calldata;
5939 struct nfs_server *server = NFS_SERVER(data->args.inode);
5941 if (nfs4_setup_sequence(server, &data->args.seq_args,
5942 &data->res.seq_res, 1, task))
5943 return;
5944 rpc_call_start(task);
5947 static void
5948 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5950 struct nfs4_layoutcommit_data *data = calldata;
5951 struct nfs_server *server = NFS_SERVER(data->args.inode);
5953 if (!nfs4_sequence_done(task, &data->res.seq_res))
5954 return;
5956 switch (task->tk_status) { /* Just ignore these failures */
5957 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5958 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5959 case NFS4ERR_BADLAYOUT: /* no layout */
5960 case NFS4ERR_GRACE: /* loca_recalim always false */
5961 task->tk_status = 0;
5964 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5965 rpc_restart_call_prepare(task);
5966 return;
5969 if (task->tk_status == 0)
5970 nfs_post_op_update_inode_force_wcc(data->args.inode,
5971 data->res.fattr);
5974 static void nfs4_layoutcommit_release(void *calldata)
5976 struct nfs4_layoutcommit_data *data = calldata;
5977 struct pnfs_layout_segment *lseg, *tmp;
5978 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
5980 pnfs_cleanup_layoutcommit(data);
5981 /* Matched by references in pnfs_set_layoutcommit */
5982 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5983 list_del_init(&lseg->pls_lc_list);
5984 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5985 &lseg->pls_flags))
5986 put_lseg(lseg);
5989 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5990 smp_mb__after_clear_bit();
5991 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5993 put_rpccred(data->cred);
5994 kfree(data);
5997 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5998 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5999 .rpc_call_done = nfs4_layoutcommit_done,
6000 .rpc_release = nfs4_layoutcommit_release,
6004 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6006 struct rpc_message msg = {
6007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6008 .rpc_argp = &data->args,
6009 .rpc_resp = &data->res,
6010 .rpc_cred = data->cred,
6012 struct rpc_task_setup task_setup_data = {
6013 .task = &data->task,
6014 .rpc_client = NFS_CLIENT(data->args.inode),
6015 .rpc_message = &msg,
6016 .callback_ops = &nfs4_layoutcommit_ops,
6017 .callback_data = data,
6018 .flags = RPC_TASK_ASYNC,
6020 struct rpc_task *task;
6021 int status = 0;
6023 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6024 "lbw: %llu inode %lu\n",
6025 data->task.tk_pid, sync,
6026 data->args.lastbytewritten,
6027 data->args.inode->i_ino);
6029 task = rpc_run_task(&task_setup_data);
6030 if (IS_ERR(task))
6031 return PTR_ERR(task);
6032 if (sync == false)
6033 goto out;
6034 status = nfs4_wait_for_completion_rpc_task(task);
6035 if (status != 0)
6036 goto out;
6037 status = task->tk_status;
6038 out:
6039 dprintk("%s: status %d\n", __func__, status);
6040 rpc_put_task(task);
6041 return status;
6044 static int
6045 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6046 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6048 struct nfs41_secinfo_no_name_args args = {
6049 .style = SECINFO_STYLE_CURRENT_FH,
6051 struct nfs4_secinfo_res res = {
6052 .flavors = flavors,
6054 struct rpc_message msg = {
6055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6056 .rpc_argp = &args,
6057 .rpc_resp = &res,
6059 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6062 static int
6063 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6064 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6066 struct nfs4_exception exception = { };
6067 int err;
6068 do {
6069 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6070 switch (err) {
6071 case 0:
6072 case -NFS4ERR_WRONGSEC:
6073 case -NFS4ERR_NOTSUPP:
6074 break;
6075 default:
6076 err = nfs4_handle_exception(server, err, &exception);
6078 } while (exception.retry);
6079 return err;
6082 static int
6083 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6084 struct nfs_fsinfo *info)
6086 int err;
6087 struct page *page;
6088 rpc_authflavor_t flavor;
6089 struct nfs4_secinfo_flavors *flavors;
6091 page = alloc_page(GFP_KERNEL);
6092 if (!page) {
6093 err = -ENOMEM;
6094 goto out;
6097 flavors = page_address(page);
6098 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6101 * Fall back on "guess and check" method if
6102 * the server doesn't support SECINFO_NO_NAME
6104 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6105 err = nfs4_find_root_sec(server, fhandle, info);
6106 goto out_freepage;
6108 if (err)
6109 goto out_freepage;
6111 flavor = nfs_find_best_sec(flavors);
6112 if (err == 0)
6113 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6115 out_freepage:
6116 put_page(page);
6117 if (err == -EACCES)
6118 return -EPERM;
6119 out:
6120 return err;
6122 static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6124 int status;
6125 struct nfs41_test_stateid_args args = {
6126 .stateid = &state->stateid,
6128 struct nfs41_test_stateid_res res;
6129 struct rpc_message msg = {
6130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6131 .rpc_argp = &args,
6132 .rpc_resp = &res,
6134 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6135 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6136 return status;
6139 static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6141 struct nfs4_exception exception = { };
6142 int err;
6143 do {
6144 err = nfs4_handle_exception(server,
6145 _nfs41_test_stateid(server, state),
6146 &exception);
6147 } while (exception.retry);
6148 return err;
6151 static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6153 int status;
6154 struct nfs41_free_stateid_args args = {
6155 .stateid = &state->stateid,
6157 struct nfs41_free_stateid_res res;
6158 struct rpc_message msg = {
6159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6160 .rpc_argp = &args,
6161 .rpc_resp = &res,
6164 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6165 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6166 return status;
6169 static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6171 struct nfs4_exception exception = { };
6172 int err;
6173 do {
6174 err = nfs4_handle_exception(server,
6175 _nfs4_free_stateid(server, state),
6176 &exception);
6177 } while (exception.retry);
6178 return err;
6180 #endif /* CONFIG_NFS_V4_1 */
6182 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6183 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6184 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6185 .recover_open = nfs4_open_reclaim,
6186 .recover_lock = nfs4_lock_reclaim,
6187 .establish_clid = nfs4_init_clientid,
6188 .get_clid_cred = nfs4_get_setclientid_cred,
6191 #if defined(CONFIG_NFS_V4_1)
6192 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6193 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6194 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6195 .recover_open = nfs4_open_reclaim,
6196 .recover_lock = nfs4_lock_reclaim,
6197 .establish_clid = nfs41_init_clientid,
6198 .get_clid_cred = nfs4_get_exchange_id_cred,
6199 .reclaim_complete = nfs41_proc_reclaim_complete,
6201 #endif /* CONFIG_NFS_V4_1 */
6203 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6204 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6205 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6206 .recover_open = nfs4_open_expired,
6207 .recover_lock = nfs4_lock_expired,
6208 .establish_clid = nfs4_init_clientid,
6209 .get_clid_cred = nfs4_get_setclientid_cred,
6212 #if defined(CONFIG_NFS_V4_1)
6213 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6214 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6215 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6216 .recover_open = nfs41_open_expired,
6217 .recover_lock = nfs41_lock_expired,
6218 .establish_clid = nfs41_init_clientid,
6219 .get_clid_cred = nfs4_get_exchange_id_cred,
6221 #endif /* CONFIG_NFS_V4_1 */
6223 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6224 .sched_state_renewal = nfs4_proc_async_renew,
6225 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6226 .renew_lease = nfs4_proc_renew,
6229 #if defined(CONFIG_NFS_V4_1)
6230 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6231 .sched_state_renewal = nfs41_proc_async_sequence,
6232 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6233 .renew_lease = nfs4_proc_sequence,
6235 #endif
6237 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6238 .minor_version = 0,
6239 .call_sync = _nfs4_call_sync,
6240 .validate_stateid = nfs4_validate_delegation_stateid,
6241 .find_root_sec = nfs4_find_root_sec,
6242 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6243 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6244 .state_renewal_ops = &nfs40_state_renewal_ops,
6247 #if defined(CONFIG_NFS_V4_1)
6248 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6249 .minor_version = 1,
6250 .call_sync = _nfs4_call_sync_session,
6251 .validate_stateid = nfs41_validate_delegation_stateid,
6252 .find_root_sec = nfs41_find_root_sec,
6253 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6254 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6255 .state_renewal_ops = &nfs41_state_renewal_ops,
6257 #endif
6259 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6260 [0] = &nfs_v4_0_minor_ops,
6261 #if defined(CONFIG_NFS_V4_1)
6262 [1] = &nfs_v4_1_minor_ops,
6263 #endif
6266 static const struct inode_operations nfs4_file_inode_operations = {
6267 .permission = nfs_permission,
6268 .getattr = nfs_getattr,
6269 .setattr = nfs_setattr,
6270 .getxattr = generic_getxattr,
6271 .setxattr = generic_setxattr,
6272 .listxattr = generic_listxattr,
6273 .removexattr = generic_removexattr,
6276 const struct nfs_rpc_ops nfs_v4_clientops = {
6277 .version = 4, /* protocol version */
6278 .dentry_ops = &nfs4_dentry_operations,
6279 .dir_inode_ops = &nfs4_dir_inode_operations,
6280 .file_inode_ops = &nfs4_file_inode_operations,
6281 .file_ops = &nfs4_file_operations,
6282 .getroot = nfs4_proc_get_root,
6283 .getattr = nfs4_proc_getattr,
6284 .setattr = nfs4_proc_setattr,
6285 .lookup = nfs4_proc_lookup,
6286 .access = nfs4_proc_access,
6287 .readlink = nfs4_proc_readlink,
6288 .create = nfs4_proc_create,
6289 .remove = nfs4_proc_remove,
6290 .unlink_setup = nfs4_proc_unlink_setup,
6291 .unlink_done = nfs4_proc_unlink_done,
6292 .rename = nfs4_proc_rename,
6293 .rename_setup = nfs4_proc_rename_setup,
6294 .rename_done = nfs4_proc_rename_done,
6295 .link = nfs4_proc_link,
6296 .symlink = nfs4_proc_symlink,
6297 .mkdir = nfs4_proc_mkdir,
6298 .rmdir = nfs4_proc_remove,
6299 .readdir = nfs4_proc_readdir,
6300 .mknod = nfs4_proc_mknod,
6301 .statfs = nfs4_proc_statfs,
6302 .fsinfo = nfs4_proc_fsinfo,
6303 .pathconf = nfs4_proc_pathconf,
6304 .set_capabilities = nfs4_server_capabilities,
6305 .decode_dirent = nfs4_decode_dirent,
6306 .read_setup = nfs4_proc_read_setup,
6307 .read_done = nfs4_read_done,
6308 .write_setup = nfs4_proc_write_setup,
6309 .write_done = nfs4_write_done,
6310 .commit_setup = nfs4_proc_commit_setup,
6311 .commit_done = nfs4_commit_done,
6312 .lock = nfs4_proc_lock,
6313 .clear_acl_cache = nfs4_zap_acl_attr,
6314 .close_context = nfs4_close_context,
6315 .open_context = nfs4_atomic_open,
6316 .init_client = nfs4_init_client,
6317 .secinfo = nfs4_proc_secinfo,
6320 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6321 .prefix = XATTR_NAME_NFSV4_ACL,
6322 .list = nfs4_xattr_list_nfs4_acl,
6323 .get = nfs4_xattr_get_nfs4_acl,
6324 .set = nfs4_xattr_set_nfs4_acl,
6327 const struct xattr_handler *nfs4_xattr_handlers[] = {
6328 &nfs4_xattr_nfs4_acl_handler,
6329 NULL
6333 * Local variables:
6334 * c-basic-offset: 8
6335 * End: