Avoid beyond bounds copy while caching ACL
[zen-stable.git] / fs / nfs / nfs4proc.c
blobf3919dca02b51c90067967be7a5d80bf092fd39b
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/nfs_idmap.h>
56 #include <linux/sunrpc/bc_xprt.h>
57 #include <linux/xattr.h>
58 #include <linux/utsname.h>
59 #include <linux/freezer.h>
61 #include "nfs4_fs.h"
62 #include "delegation.h"
63 #include "internal.h"
64 #include "iostat.h"
65 #include "callback.h"
66 #include "pnfs.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
75 struct nfs4_opendata;
76 static int _nfs4_proc_open(struct nfs4_opendata *data);
77 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
78 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
79 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
80 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
81 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
82 struct nfs_fattr *fattr, struct iattr *sattr,
83 struct nfs4_state *state);
84 #ifdef CONFIG_NFS_V4_1
85 static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
86 static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
87 #endif
88 /* Prevent leaks of NFSv4 errors into userland */
89 static int nfs4_map_errors(int err)
91 if (err >= -1000)
92 return err;
93 switch (err) {
94 case -NFS4ERR_RESOURCE:
95 return -EREMOTEIO;
96 case -NFS4ERR_WRONGSEC:
97 return -EPERM;
98 case -NFS4ERR_BADOWNER:
99 case -NFS4ERR_BADNAME:
100 return -EINVAL;
101 default:
102 dprintk("%s could not handle NFSv4 error %d\n",
103 __func__, -err);
104 break;
106 return -EIO;
110 * This is our standard bitmap for GETATTR requests.
112 const u32 nfs4_fattr_bitmap[2] = {
113 FATTR4_WORD0_TYPE
114 | FATTR4_WORD0_CHANGE
115 | FATTR4_WORD0_SIZE
116 | FATTR4_WORD0_FSID
117 | FATTR4_WORD0_FILEID,
118 FATTR4_WORD1_MODE
119 | FATTR4_WORD1_NUMLINKS
120 | FATTR4_WORD1_OWNER
121 | FATTR4_WORD1_OWNER_GROUP
122 | FATTR4_WORD1_RAWDEV
123 | FATTR4_WORD1_SPACE_USED
124 | FATTR4_WORD1_TIME_ACCESS
125 | FATTR4_WORD1_TIME_METADATA
126 | FATTR4_WORD1_TIME_MODIFY
129 const u32 nfs4_statfs_bitmap[2] = {
130 FATTR4_WORD0_FILES_AVAIL
131 | FATTR4_WORD0_FILES_FREE
132 | FATTR4_WORD0_FILES_TOTAL,
133 FATTR4_WORD1_SPACE_AVAIL
134 | FATTR4_WORD1_SPACE_FREE
135 | FATTR4_WORD1_SPACE_TOTAL
138 const u32 nfs4_pathconf_bitmap[2] = {
139 FATTR4_WORD0_MAXLINK
140 | FATTR4_WORD0_MAXNAME,
144 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
145 | FATTR4_WORD0_MAXREAD
146 | FATTR4_WORD0_MAXWRITE
147 | FATTR4_WORD0_LEASE_TIME,
148 FATTR4_WORD1_TIME_DELTA
149 | FATTR4_WORD1_FS_LAYOUT_TYPES,
150 FATTR4_WORD2_LAYOUT_BLKSIZE
153 const u32 nfs4_fs_locations_bitmap[2] = {
154 FATTR4_WORD0_TYPE
155 | FATTR4_WORD0_CHANGE
156 | FATTR4_WORD0_SIZE
157 | FATTR4_WORD0_FSID
158 | FATTR4_WORD0_FILEID
159 | FATTR4_WORD0_FS_LOCATIONS,
160 FATTR4_WORD1_MODE
161 | FATTR4_WORD1_NUMLINKS
162 | FATTR4_WORD1_OWNER
163 | FATTR4_WORD1_OWNER_GROUP
164 | FATTR4_WORD1_RAWDEV
165 | FATTR4_WORD1_SPACE_USED
166 | FATTR4_WORD1_TIME_ACCESS
167 | FATTR4_WORD1_TIME_METADATA
168 | FATTR4_WORD1_TIME_MODIFY
169 | FATTR4_WORD1_MOUNTED_ON_FILEID
172 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
173 struct nfs4_readdir_arg *readdir)
175 __be32 *start, *p;
177 BUG_ON(readdir->count < 80);
178 if (cookie > 2) {
179 readdir->cookie = cookie;
180 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
181 return;
184 readdir->cookie = 0;
185 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
186 if (cookie == 2)
187 return;
190 * NFSv4 servers do not return entries for '.' and '..'
191 * Therefore, we fake these entries here. We let '.'
192 * have cookie 0 and '..' have cookie 1. Note that
193 * when talking to the server, we always send cookie 0
194 * instead of 1 or 2.
196 start = p = kmap_atomic(*readdir->pages, KM_USER0);
198 if (cookie == 0) {
199 *p++ = xdr_one; /* next */
200 *p++ = xdr_zero; /* cookie, first word */
201 *p++ = xdr_one; /* cookie, second word */
202 *p++ = xdr_one; /* entry len */
203 memcpy(p, ".\0\0\0", 4); /* entry */
204 p++;
205 *p++ = xdr_one; /* bitmap length */
206 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
207 *p++ = htonl(8); /* attribute buffer length */
208 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
211 *p++ = xdr_one; /* next */
212 *p++ = xdr_zero; /* cookie, first word */
213 *p++ = xdr_two; /* cookie, second word */
214 *p++ = xdr_two; /* entry len */
215 memcpy(p, "..\0\0", 4); /* entry */
216 p++;
217 *p++ = xdr_one; /* bitmap length */
218 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
219 *p++ = htonl(8); /* attribute buffer length */
220 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
222 readdir->pgbase = (char *)p - (char *)start;
223 readdir->count -= readdir->pgbase;
224 kunmap_atomic(start, KM_USER0);
227 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
229 int res;
231 might_sleep();
233 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
234 nfs_wait_bit_killable, TASK_KILLABLE);
235 return res;
238 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
240 int res = 0;
242 might_sleep();
244 if (*timeout <= 0)
245 *timeout = NFS4_POLL_RETRY_MIN;
246 if (*timeout > NFS4_POLL_RETRY_MAX)
247 *timeout = NFS4_POLL_RETRY_MAX;
248 freezable_schedule_timeout_killable(*timeout);
249 if (fatal_signal_pending(current))
250 res = -ERESTARTSYS;
251 *timeout <<= 1;
252 return res;
255 /* This is the error handling routine for processes that are allowed
256 * to sleep.
258 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
260 struct nfs_client *clp = server->nfs_client;
261 struct nfs4_state *state = exception->state;
262 struct inode *inode = exception->inode;
263 int ret = errorcode;
265 exception->retry = 0;
266 switch(errorcode) {
267 case 0:
268 return 0;
269 case -NFS4ERR_OPENMODE:
270 if (nfs_have_delegation(inode, FMODE_READ)) {
271 nfs_inode_return_delegation(inode);
272 exception->retry = 1;
273 return 0;
275 if (state == NULL)
276 break;
277 nfs4_schedule_stateid_recovery(server, state);
278 goto wait_on_recovery;
279 case -NFS4ERR_DELEG_REVOKED:
280 case -NFS4ERR_ADMIN_REVOKED:
281 case -NFS4ERR_BAD_STATEID:
282 if (state != NULL)
283 nfs_remove_bad_delegation(state->inode);
284 if (state == NULL)
285 break;
286 nfs4_schedule_stateid_recovery(server, state);
287 goto wait_on_recovery;
288 case -NFS4ERR_EXPIRED:
289 if (state != NULL)
290 nfs4_schedule_stateid_recovery(server, state);
291 case -NFS4ERR_STALE_STATEID:
292 case -NFS4ERR_STALE_CLIENTID:
293 nfs4_schedule_lease_recovery(clp);
294 goto wait_on_recovery;
295 #if defined(CONFIG_NFS_V4_1)
296 case -NFS4ERR_BADSESSION:
297 case -NFS4ERR_BADSLOT:
298 case -NFS4ERR_BAD_HIGH_SLOT:
299 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
300 case -NFS4ERR_DEADSESSION:
301 case -NFS4ERR_SEQ_FALSE_RETRY:
302 case -NFS4ERR_SEQ_MISORDERED:
303 dprintk("%s ERROR: %d Reset session\n", __func__,
304 errorcode);
305 nfs4_schedule_session_recovery(clp->cl_session);
306 exception->retry = 1;
307 break;
308 #endif /* defined(CONFIG_NFS_V4_1) */
309 case -NFS4ERR_FILE_OPEN:
310 if (exception->timeout > HZ) {
311 /* We have retried a decent amount, time to
312 * fail
314 ret = -EBUSY;
315 break;
317 case -NFS4ERR_GRACE:
318 case -NFS4ERR_DELAY:
319 case -EKEYEXPIRED:
320 ret = nfs4_delay(server->client, &exception->timeout);
321 if (ret != 0)
322 break;
323 case -NFS4ERR_RETRY_UNCACHED_REP:
324 case -NFS4ERR_OLD_STATEID:
325 exception->retry = 1;
326 break;
327 case -NFS4ERR_BADOWNER:
328 /* The following works around a Linux server bug! */
329 case -NFS4ERR_BADNAME:
330 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
331 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
332 exception->retry = 1;
333 printk(KERN_WARNING "NFS: v4 server %s "
334 "does not accept raw "
335 "uid/gids. "
336 "Reenabling the idmapper.\n",
337 server->nfs_client->cl_hostname);
340 /* We failed to handle the error */
341 return nfs4_map_errors(ret);
342 wait_on_recovery:
343 ret = nfs4_wait_clnt_recover(clp);
344 if (ret == 0)
345 exception->retry = 1;
346 return ret;
350 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
352 spin_lock(&clp->cl_lock);
353 if (time_before(clp->cl_last_renewal,timestamp))
354 clp->cl_last_renewal = timestamp;
355 spin_unlock(&clp->cl_lock);
358 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
360 do_renew_lease(server->nfs_client, timestamp);
363 #if defined(CONFIG_NFS_V4_1)
366 * nfs4_free_slot - free a slot and efficiently update slot table.
368 * freeing a slot is trivially done by clearing its respective bit
369 * in the bitmap.
370 * If the freed slotid equals highest_used_slotid we want to update it
371 * so that the server would be able to size down the slot table if needed,
372 * otherwise we know that the highest_used_slotid is still in use.
373 * When updating highest_used_slotid there may be "holes" in the bitmap
374 * so we need to scan down from highest_used_slotid to 0 looking for the now
375 * highest slotid in use.
376 * If none found, highest_used_slotid is set to -1.
378 * Must be called while holding tbl->slot_tbl_lock
380 static void
381 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
383 int slotid = free_slotid;
385 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
386 /* clear used bit in bitmap */
387 __clear_bit(slotid, tbl->used_slots);
389 /* update highest_used_slotid when it is freed */
390 if (slotid == tbl->highest_used_slotid) {
391 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
392 if (slotid < tbl->max_slots)
393 tbl->highest_used_slotid = slotid;
394 else
395 tbl->highest_used_slotid = -1;
397 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
398 free_slotid, tbl->highest_used_slotid);
402 * Signal state manager thread if session fore channel is drained
404 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
406 struct rpc_task *task;
408 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
409 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
410 if (task)
411 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
412 return;
415 if (ses->fc_slot_table.highest_used_slotid != -1)
416 return;
418 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
419 complete(&ses->fc_slot_table.complete);
423 * Signal state manager thread if session back channel is drained
425 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
427 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
428 ses->bc_slot_table.highest_used_slotid != -1)
429 return;
430 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
431 complete(&ses->bc_slot_table.complete);
434 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
436 struct nfs4_slot_table *tbl;
438 tbl = &res->sr_session->fc_slot_table;
439 if (!res->sr_slot) {
440 /* just wake up the next guy waiting since
441 * we may have not consumed a slot after all */
442 dprintk("%s: No slot\n", __func__);
443 return;
446 spin_lock(&tbl->slot_tbl_lock);
447 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
448 nfs4_check_drain_fc_complete(res->sr_session);
449 spin_unlock(&tbl->slot_tbl_lock);
450 res->sr_slot = NULL;
453 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
455 unsigned long timestamp;
456 struct nfs_client *clp;
459 * sr_status remains 1 if an RPC level error occurred. The server
460 * may or may not have processed the sequence operation..
461 * Proceed as if the server received and processed the sequence
462 * operation.
464 if (res->sr_status == 1)
465 res->sr_status = NFS_OK;
467 /* don't increment the sequence number if the task wasn't sent */
468 if (!RPC_WAS_SENT(task))
469 goto out;
471 /* Check the SEQUENCE operation status */
472 switch (res->sr_status) {
473 case 0:
474 /* Update the slot's sequence and clientid lease timer */
475 ++res->sr_slot->seq_nr;
476 timestamp = res->sr_renewal_time;
477 clp = res->sr_session->clp;
478 do_renew_lease(clp, timestamp);
479 /* Check sequence flags */
480 if (res->sr_status_flags != 0)
481 nfs4_schedule_lease_recovery(clp);
482 break;
483 case -NFS4ERR_DELAY:
484 /* The server detected a resend of the RPC call and
485 * returned NFS4ERR_DELAY as per Section 2.10.6.2
486 * of RFC5661.
488 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
489 __func__,
490 res->sr_slot - res->sr_session->fc_slot_table.slots,
491 res->sr_slot->seq_nr);
492 goto out_retry;
493 default:
494 /* Just update the slot sequence no. */
495 ++res->sr_slot->seq_nr;
497 out:
498 /* The session may be reset by one of the error handlers. */
499 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
500 nfs41_sequence_free_slot(res);
501 return 1;
502 out_retry:
503 if (!rpc_restart_call(task))
504 goto out;
505 rpc_delay(task, NFS4_POLL_RETRY_MAX);
506 return 0;
509 static int nfs4_sequence_done(struct rpc_task *task,
510 struct nfs4_sequence_res *res)
512 if (res->sr_session == NULL)
513 return 1;
514 return nfs41_sequence_done(task, res);
518 * nfs4_find_slot - efficiently look for a free slot
520 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
521 * If found, we mark the slot as used, update the highest_used_slotid,
522 * and respectively set up the sequence operation args.
523 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
525 * Note: must be called with under the slot_tbl_lock.
527 static u8
528 nfs4_find_slot(struct nfs4_slot_table *tbl)
530 int slotid;
531 u8 ret_id = NFS4_MAX_SLOT_TABLE;
532 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
534 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
535 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
536 tbl->max_slots);
537 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
538 if (slotid >= tbl->max_slots)
539 goto out;
540 __set_bit(slotid, tbl->used_slots);
541 if (slotid > tbl->highest_used_slotid)
542 tbl->highest_used_slotid = slotid;
543 ret_id = slotid;
544 out:
545 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
546 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
547 return ret_id;
550 int nfs41_setup_sequence(struct nfs4_session *session,
551 struct nfs4_sequence_args *args,
552 struct nfs4_sequence_res *res,
553 int cache_reply,
554 struct rpc_task *task)
556 struct nfs4_slot *slot;
557 struct nfs4_slot_table *tbl;
558 u8 slotid;
560 dprintk("--> %s\n", __func__);
561 /* slot already allocated? */
562 if (res->sr_slot != NULL)
563 return 0;
565 tbl = &session->fc_slot_table;
567 spin_lock(&tbl->slot_tbl_lock);
568 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
569 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
570 /* The state manager will wait until the slot table is empty */
571 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
572 spin_unlock(&tbl->slot_tbl_lock);
573 dprintk("%s session is draining\n", __func__);
574 return -EAGAIN;
577 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
578 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
579 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
580 spin_unlock(&tbl->slot_tbl_lock);
581 dprintk("%s enforce FIFO order\n", __func__);
582 return -EAGAIN;
585 slotid = nfs4_find_slot(tbl);
586 if (slotid == NFS4_MAX_SLOT_TABLE) {
587 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
588 spin_unlock(&tbl->slot_tbl_lock);
589 dprintk("<-- %s: no free slots\n", __func__);
590 return -EAGAIN;
592 spin_unlock(&tbl->slot_tbl_lock);
594 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
595 slot = tbl->slots + slotid;
596 args->sa_session = session;
597 args->sa_slotid = slotid;
598 args->sa_cache_this = cache_reply;
600 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
602 res->sr_session = session;
603 res->sr_slot = slot;
604 res->sr_renewal_time = jiffies;
605 res->sr_status_flags = 0;
607 * sr_status is only set in decode_sequence, and so will remain
608 * set to 1 if an rpc level failure occurs.
610 res->sr_status = 1;
611 return 0;
613 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
615 int nfs4_setup_sequence(const struct nfs_server *server,
616 struct nfs4_sequence_args *args,
617 struct nfs4_sequence_res *res,
618 int cache_reply,
619 struct rpc_task *task)
621 struct nfs4_session *session = nfs4_get_session(server);
622 int ret = 0;
624 if (session == NULL) {
625 args->sa_session = NULL;
626 res->sr_session = NULL;
627 goto out;
630 dprintk("--> %s clp %p session %p sr_slot %td\n",
631 __func__, session->clp, session, res->sr_slot ?
632 res->sr_slot - session->fc_slot_table.slots : -1);
634 ret = nfs41_setup_sequence(session, args, res, cache_reply,
635 task);
636 out:
637 dprintk("<-- %s status=%d\n", __func__, ret);
638 return ret;
641 struct nfs41_call_sync_data {
642 const struct nfs_server *seq_server;
643 struct nfs4_sequence_args *seq_args;
644 struct nfs4_sequence_res *seq_res;
645 int cache_reply;
648 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
650 struct nfs41_call_sync_data *data = calldata;
652 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
654 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
655 data->seq_res, data->cache_reply, task))
656 return;
657 rpc_call_start(task);
660 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
662 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
663 nfs41_call_sync_prepare(task, calldata);
666 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
668 struct nfs41_call_sync_data *data = calldata;
670 nfs41_sequence_done(task, data->seq_res);
673 struct rpc_call_ops nfs41_call_sync_ops = {
674 .rpc_call_prepare = nfs41_call_sync_prepare,
675 .rpc_call_done = nfs41_call_sync_done,
678 struct rpc_call_ops nfs41_call_priv_sync_ops = {
679 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
680 .rpc_call_done = nfs41_call_sync_done,
683 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
684 struct nfs_server *server,
685 struct rpc_message *msg,
686 struct nfs4_sequence_args *args,
687 struct nfs4_sequence_res *res,
688 int cache_reply,
689 int privileged)
691 int ret;
692 struct rpc_task *task;
693 struct nfs41_call_sync_data data = {
694 .seq_server = server,
695 .seq_args = args,
696 .seq_res = res,
697 .cache_reply = cache_reply,
699 struct rpc_task_setup task_setup = {
700 .rpc_client = clnt,
701 .rpc_message = msg,
702 .callback_ops = &nfs41_call_sync_ops,
703 .callback_data = &data
706 res->sr_slot = NULL;
707 if (privileged)
708 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
709 task = rpc_run_task(&task_setup);
710 if (IS_ERR(task))
711 ret = PTR_ERR(task);
712 else {
713 ret = task->tk_status;
714 rpc_put_task(task);
716 return ret;
719 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
720 struct nfs_server *server,
721 struct rpc_message *msg,
722 struct nfs4_sequence_args *args,
723 struct nfs4_sequence_res *res,
724 int cache_reply)
726 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
729 #else
730 static int nfs4_sequence_done(struct rpc_task *task,
731 struct nfs4_sequence_res *res)
733 return 1;
735 #endif /* CONFIG_NFS_V4_1 */
737 int _nfs4_call_sync(struct rpc_clnt *clnt,
738 struct nfs_server *server,
739 struct rpc_message *msg,
740 struct nfs4_sequence_args *args,
741 struct nfs4_sequence_res *res,
742 int cache_reply)
744 args->sa_session = res->sr_session = NULL;
745 return rpc_call_sync(clnt, msg, 0);
748 static inline
749 int nfs4_call_sync(struct rpc_clnt *clnt,
750 struct nfs_server *server,
751 struct rpc_message *msg,
752 struct nfs4_sequence_args *args,
753 struct nfs4_sequence_res *res,
754 int cache_reply)
756 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
757 args, res, cache_reply);
760 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
762 struct nfs_inode *nfsi = NFS_I(dir);
764 spin_lock(&dir->i_lock);
765 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
766 if (!cinfo->atomic || cinfo->before != dir->i_version)
767 nfs_force_lookup_revalidate(dir);
768 dir->i_version = cinfo->after;
769 spin_unlock(&dir->i_lock);
772 struct nfs4_opendata {
773 struct kref kref;
774 struct nfs_openargs o_arg;
775 struct nfs_openres o_res;
776 struct nfs_open_confirmargs c_arg;
777 struct nfs_open_confirmres c_res;
778 struct nfs4_string owner_name;
779 struct nfs4_string group_name;
780 struct nfs_fattr f_attr;
781 struct nfs_fattr dir_attr;
782 struct dentry *dir;
783 struct dentry *dentry;
784 struct nfs4_state_owner *owner;
785 struct nfs4_state *state;
786 struct iattr attrs;
787 unsigned long timestamp;
788 unsigned int rpc_done : 1;
789 int rpc_status;
790 int cancelled;
794 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
796 p->o_res.f_attr = &p->f_attr;
797 p->o_res.dir_attr = &p->dir_attr;
798 p->o_res.seqid = p->o_arg.seqid;
799 p->c_res.seqid = p->c_arg.seqid;
800 p->o_res.server = p->o_arg.server;
801 nfs_fattr_init(&p->f_attr);
802 nfs_fattr_init(&p->dir_attr);
803 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
806 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
807 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
808 const struct iattr *attrs,
809 gfp_t gfp_mask)
811 struct dentry *parent = dget_parent(dentry);
812 struct inode *dir = parent->d_inode;
813 struct nfs_server *server = NFS_SERVER(dir);
814 struct nfs4_opendata *p;
816 p = kzalloc(sizeof(*p), gfp_mask);
817 if (p == NULL)
818 goto err;
819 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
820 if (p->o_arg.seqid == NULL)
821 goto err_free;
822 nfs_sb_active(dentry->d_sb);
823 p->dentry = dget(dentry);
824 p->dir = parent;
825 p->owner = sp;
826 atomic_inc(&sp->so_count);
827 p->o_arg.fh = NFS_FH(dir);
828 p->o_arg.open_flags = flags;
829 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
830 p->o_arg.clientid = server->nfs_client->cl_clientid;
831 p->o_arg.id = sp->so_owner_id.id;
832 p->o_arg.name = &dentry->d_name;
833 p->o_arg.server = server;
834 p->o_arg.bitmask = server->attr_bitmask;
835 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
836 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
837 if (flags & O_CREAT) {
838 u32 *s;
840 p->o_arg.u.attrs = &p->attrs;
841 memcpy(&p->attrs, attrs, sizeof(p->attrs));
842 s = (u32 *) p->o_arg.u.verifier.data;
843 s[0] = jiffies;
844 s[1] = current->pid;
846 p->c_arg.fh = &p->o_res.fh;
847 p->c_arg.stateid = &p->o_res.stateid;
848 p->c_arg.seqid = p->o_arg.seqid;
849 nfs4_init_opendata_res(p);
850 kref_init(&p->kref);
851 return p;
852 err_free:
853 kfree(p);
854 err:
855 dput(parent);
856 return NULL;
859 static void nfs4_opendata_free(struct kref *kref)
861 struct nfs4_opendata *p = container_of(kref,
862 struct nfs4_opendata, kref);
863 struct super_block *sb = p->dentry->d_sb;
865 nfs_free_seqid(p->o_arg.seqid);
866 if (p->state != NULL)
867 nfs4_put_open_state(p->state);
868 nfs4_put_state_owner(p->owner);
869 dput(p->dir);
870 dput(p->dentry);
871 nfs_sb_deactive(sb);
872 nfs_fattr_free_names(&p->f_attr);
873 kfree(p);
876 static void nfs4_opendata_put(struct nfs4_opendata *p)
878 if (p != NULL)
879 kref_put(&p->kref, nfs4_opendata_free);
882 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
884 int ret;
886 ret = rpc_wait_for_completion_task(task);
887 return ret;
890 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
892 int ret = 0;
894 if (open_mode & O_EXCL)
895 goto out;
896 switch (mode & (FMODE_READ|FMODE_WRITE)) {
897 case FMODE_READ:
898 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
899 && state->n_rdonly != 0;
900 break;
901 case FMODE_WRITE:
902 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
903 && state->n_wronly != 0;
904 break;
905 case FMODE_READ|FMODE_WRITE:
906 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
907 && state->n_rdwr != 0;
909 out:
910 return ret;
913 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
915 if (delegation == NULL)
916 return 0;
917 if ((delegation->type & fmode) != fmode)
918 return 0;
919 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
920 return 0;
921 nfs_mark_delegation_referenced(delegation);
922 return 1;
925 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
927 switch (fmode) {
928 case FMODE_WRITE:
929 state->n_wronly++;
930 break;
931 case FMODE_READ:
932 state->n_rdonly++;
933 break;
934 case FMODE_READ|FMODE_WRITE:
935 state->n_rdwr++;
937 nfs4_state_set_mode_locked(state, state->state | fmode);
940 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
943 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
944 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
945 switch (fmode) {
946 case FMODE_READ:
947 set_bit(NFS_O_RDONLY_STATE, &state->flags);
948 break;
949 case FMODE_WRITE:
950 set_bit(NFS_O_WRONLY_STATE, &state->flags);
951 break;
952 case FMODE_READ|FMODE_WRITE:
953 set_bit(NFS_O_RDWR_STATE, &state->flags);
957 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
959 write_seqlock(&state->seqlock);
960 nfs_set_open_stateid_locked(state, stateid, fmode);
961 write_sequnlock(&state->seqlock);
964 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
967 * Protect the call to nfs4_state_set_mode_locked and
968 * serialise the stateid update
970 write_seqlock(&state->seqlock);
971 if (deleg_stateid != NULL) {
972 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
973 set_bit(NFS_DELEGATED_STATE, &state->flags);
975 if (open_stateid != NULL)
976 nfs_set_open_stateid_locked(state, open_stateid, fmode);
977 write_sequnlock(&state->seqlock);
978 spin_lock(&state->owner->so_lock);
979 update_open_stateflags(state, fmode);
980 spin_unlock(&state->owner->so_lock);
983 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
985 struct nfs_inode *nfsi = NFS_I(state->inode);
986 struct nfs_delegation *deleg_cur;
987 int ret = 0;
989 fmode &= (FMODE_READ|FMODE_WRITE);
991 rcu_read_lock();
992 deleg_cur = rcu_dereference(nfsi->delegation);
993 if (deleg_cur == NULL)
994 goto no_delegation;
996 spin_lock(&deleg_cur->lock);
997 if (nfsi->delegation != deleg_cur ||
998 (deleg_cur->type & fmode) != fmode)
999 goto no_delegation_unlock;
1001 if (delegation == NULL)
1002 delegation = &deleg_cur->stateid;
1003 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1004 goto no_delegation_unlock;
1006 nfs_mark_delegation_referenced(deleg_cur);
1007 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1008 ret = 1;
1009 no_delegation_unlock:
1010 spin_unlock(&deleg_cur->lock);
1011 no_delegation:
1012 rcu_read_unlock();
1014 if (!ret && open_stateid != NULL) {
1015 __update_open_stateid(state, open_stateid, NULL, fmode);
1016 ret = 1;
1019 return ret;
1023 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1025 struct nfs_delegation *delegation;
1027 rcu_read_lock();
1028 delegation = rcu_dereference(NFS_I(inode)->delegation);
1029 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1030 rcu_read_unlock();
1031 return;
1033 rcu_read_unlock();
1034 nfs_inode_return_delegation(inode);
1037 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1039 struct nfs4_state *state = opendata->state;
1040 struct nfs_inode *nfsi = NFS_I(state->inode);
1041 struct nfs_delegation *delegation;
1042 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1043 fmode_t fmode = opendata->o_arg.fmode;
1044 nfs4_stateid stateid;
1045 int ret = -EAGAIN;
1047 for (;;) {
1048 if (can_open_cached(state, fmode, open_mode)) {
1049 spin_lock(&state->owner->so_lock);
1050 if (can_open_cached(state, fmode, open_mode)) {
1051 update_open_stateflags(state, fmode);
1052 spin_unlock(&state->owner->so_lock);
1053 goto out_return_state;
1055 spin_unlock(&state->owner->so_lock);
1057 rcu_read_lock();
1058 delegation = rcu_dereference(nfsi->delegation);
1059 if (!can_open_delegated(delegation, fmode)) {
1060 rcu_read_unlock();
1061 break;
1063 /* Save the delegation */
1064 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1065 rcu_read_unlock();
1066 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1067 if (ret != 0)
1068 goto out;
1069 ret = -EAGAIN;
1071 /* Try to update the stateid using the delegation */
1072 if (update_open_stateid(state, NULL, &stateid, fmode))
1073 goto out_return_state;
1075 out:
1076 return ERR_PTR(ret);
1077 out_return_state:
1078 atomic_inc(&state->count);
1079 return state;
1082 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1084 struct inode *inode;
1085 struct nfs4_state *state = NULL;
1086 struct nfs_delegation *delegation;
1087 int ret;
1089 if (!data->rpc_done) {
1090 state = nfs4_try_open_cached(data);
1091 goto out;
1094 ret = -EAGAIN;
1095 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1096 goto err;
1097 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1098 ret = PTR_ERR(inode);
1099 if (IS_ERR(inode))
1100 goto err;
1101 ret = -ENOMEM;
1102 state = nfs4_get_open_state(inode, data->owner);
1103 if (state == NULL)
1104 goto err_put_inode;
1105 if (data->o_res.delegation_type != 0) {
1106 int delegation_flags = 0;
1108 rcu_read_lock();
1109 delegation = rcu_dereference(NFS_I(inode)->delegation);
1110 if (delegation)
1111 delegation_flags = delegation->flags;
1112 rcu_read_unlock();
1113 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1114 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1115 "returning a delegation for "
1116 "OPEN(CLAIM_DELEGATE_CUR)\n",
1117 NFS_CLIENT(inode)->cl_server);
1118 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1119 nfs_inode_set_delegation(state->inode,
1120 data->owner->so_cred,
1121 &data->o_res);
1122 else
1123 nfs_inode_reclaim_delegation(state->inode,
1124 data->owner->so_cred,
1125 &data->o_res);
1128 update_open_stateid(state, &data->o_res.stateid, NULL,
1129 data->o_arg.fmode);
1130 iput(inode);
1131 out:
1132 return state;
1133 err_put_inode:
1134 iput(inode);
1135 err:
1136 return ERR_PTR(ret);
1139 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1141 struct nfs_inode *nfsi = NFS_I(state->inode);
1142 struct nfs_open_context *ctx;
1144 spin_lock(&state->inode->i_lock);
1145 list_for_each_entry(ctx, &nfsi->open_files, list) {
1146 if (ctx->state != state)
1147 continue;
1148 get_nfs_open_context(ctx);
1149 spin_unlock(&state->inode->i_lock);
1150 return ctx;
1152 spin_unlock(&state->inode->i_lock);
1153 return ERR_PTR(-ENOENT);
1156 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1158 struct nfs4_opendata *opendata;
1160 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1161 if (opendata == NULL)
1162 return ERR_PTR(-ENOMEM);
1163 opendata->state = state;
1164 atomic_inc(&state->count);
1165 return opendata;
1168 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1170 struct nfs4_state *newstate;
1171 int ret;
1173 opendata->o_arg.open_flags = 0;
1174 opendata->o_arg.fmode = fmode;
1175 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1176 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1177 nfs4_init_opendata_res(opendata);
1178 ret = _nfs4_recover_proc_open(opendata);
1179 if (ret != 0)
1180 return ret;
1181 newstate = nfs4_opendata_to_nfs4_state(opendata);
1182 if (IS_ERR(newstate))
1183 return PTR_ERR(newstate);
1184 nfs4_close_state(newstate, fmode);
1185 *res = newstate;
1186 return 0;
1189 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1191 struct nfs4_state *newstate;
1192 int ret;
1194 /* memory barrier prior to reading state->n_* */
1195 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1196 smp_rmb();
1197 if (state->n_rdwr != 0) {
1198 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1199 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1200 if (ret != 0)
1201 return ret;
1202 if (newstate != state)
1203 return -ESTALE;
1205 if (state->n_wronly != 0) {
1206 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1207 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1208 if (ret != 0)
1209 return ret;
1210 if (newstate != state)
1211 return -ESTALE;
1213 if (state->n_rdonly != 0) {
1214 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1215 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1216 if (ret != 0)
1217 return ret;
1218 if (newstate != state)
1219 return -ESTALE;
1222 * We may have performed cached opens for all three recoveries.
1223 * Check if we need to update the current stateid.
1225 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1226 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1227 write_seqlock(&state->seqlock);
1228 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1229 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1230 write_sequnlock(&state->seqlock);
1232 return 0;
1236 * OPEN_RECLAIM:
1237 * reclaim state on the server after a reboot.
1239 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1241 struct nfs_delegation *delegation;
1242 struct nfs4_opendata *opendata;
1243 fmode_t delegation_type = 0;
1244 int status;
1246 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1247 if (IS_ERR(opendata))
1248 return PTR_ERR(opendata);
1249 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1250 opendata->o_arg.fh = NFS_FH(state->inode);
1251 rcu_read_lock();
1252 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1253 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1254 delegation_type = delegation->type;
1255 rcu_read_unlock();
1256 opendata->o_arg.u.delegation_type = delegation_type;
1257 status = nfs4_open_recover(opendata, state);
1258 nfs4_opendata_put(opendata);
1259 return status;
1262 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1264 struct nfs_server *server = NFS_SERVER(state->inode);
1265 struct nfs4_exception exception = { };
1266 int err;
1267 do {
1268 err = _nfs4_do_open_reclaim(ctx, state);
1269 if (err != -NFS4ERR_DELAY)
1270 break;
1271 nfs4_handle_exception(server, err, &exception);
1272 } while (exception.retry);
1273 return err;
1276 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1278 struct nfs_open_context *ctx;
1279 int ret;
1281 ctx = nfs4_state_find_open_context(state);
1282 if (IS_ERR(ctx))
1283 return PTR_ERR(ctx);
1284 ret = nfs4_do_open_reclaim(ctx, state);
1285 put_nfs_open_context(ctx);
1286 return ret;
1289 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1291 struct nfs4_opendata *opendata;
1292 int ret;
1294 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1295 if (IS_ERR(opendata))
1296 return PTR_ERR(opendata);
1297 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1298 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1299 sizeof(opendata->o_arg.u.delegation.data));
1300 ret = nfs4_open_recover(opendata, state);
1301 nfs4_opendata_put(opendata);
1302 return ret;
1305 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1307 struct nfs4_exception exception = { };
1308 struct nfs_server *server = NFS_SERVER(state->inode);
1309 int err;
1310 do {
1311 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1312 switch (err) {
1313 case 0:
1314 case -ENOENT:
1315 case -ESTALE:
1316 goto out;
1317 case -NFS4ERR_BADSESSION:
1318 case -NFS4ERR_BADSLOT:
1319 case -NFS4ERR_BAD_HIGH_SLOT:
1320 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1321 case -NFS4ERR_DEADSESSION:
1322 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1323 goto out;
1324 case -NFS4ERR_STALE_CLIENTID:
1325 case -NFS4ERR_STALE_STATEID:
1326 case -NFS4ERR_EXPIRED:
1327 /* Don't recall a delegation if it was lost */
1328 nfs4_schedule_lease_recovery(server->nfs_client);
1329 goto out;
1330 case -ERESTARTSYS:
1332 * The show must go on: exit, but mark the
1333 * stateid as needing recovery.
1335 case -NFS4ERR_DELEG_REVOKED:
1336 case -NFS4ERR_ADMIN_REVOKED:
1337 case -NFS4ERR_BAD_STATEID:
1338 nfs_inode_find_state_and_recover(state->inode,
1339 stateid);
1340 nfs4_schedule_stateid_recovery(server, state);
1341 case -EKEYEXPIRED:
1343 * User RPCSEC_GSS context has expired.
1344 * We cannot recover this stateid now, so
1345 * skip it and allow recovery thread to
1346 * proceed.
1348 case -ENOMEM:
1349 err = 0;
1350 goto out;
1352 err = nfs4_handle_exception(server, err, &exception);
1353 } while (exception.retry);
1354 out:
1355 return err;
1358 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1360 struct nfs4_opendata *data = calldata;
1362 data->rpc_status = task->tk_status;
1363 if (data->rpc_status == 0) {
1364 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1365 sizeof(data->o_res.stateid.data));
1366 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1367 renew_lease(data->o_res.server, data->timestamp);
1368 data->rpc_done = 1;
1372 static void nfs4_open_confirm_release(void *calldata)
1374 struct nfs4_opendata *data = calldata;
1375 struct nfs4_state *state = NULL;
1377 /* If this request hasn't been cancelled, do nothing */
1378 if (data->cancelled == 0)
1379 goto out_free;
1380 /* In case of error, no cleanup! */
1381 if (!data->rpc_done)
1382 goto out_free;
1383 state = nfs4_opendata_to_nfs4_state(data);
1384 if (!IS_ERR(state))
1385 nfs4_close_state(state, data->o_arg.fmode);
1386 out_free:
1387 nfs4_opendata_put(data);
1390 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1391 .rpc_call_done = nfs4_open_confirm_done,
1392 .rpc_release = nfs4_open_confirm_release,
1396 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1398 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1400 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1401 struct rpc_task *task;
1402 struct rpc_message msg = {
1403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1404 .rpc_argp = &data->c_arg,
1405 .rpc_resp = &data->c_res,
1406 .rpc_cred = data->owner->so_cred,
1408 struct rpc_task_setup task_setup_data = {
1409 .rpc_client = server->client,
1410 .rpc_message = &msg,
1411 .callback_ops = &nfs4_open_confirm_ops,
1412 .callback_data = data,
1413 .workqueue = nfsiod_workqueue,
1414 .flags = RPC_TASK_ASYNC,
1416 int status;
1418 kref_get(&data->kref);
1419 data->rpc_done = 0;
1420 data->rpc_status = 0;
1421 data->timestamp = jiffies;
1422 task = rpc_run_task(&task_setup_data);
1423 if (IS_ERR(task))
1424 return PTR_ERR(task);
1425 status = nfs4_wait_for_completion_rpc_task(task);
1426 if (status != 0) {
1427 data->cancelled = 1;
1428 smp_wmb();
1429 } else
1430 status = data->rpc_status;
1431 rpc_put_task(task);
1432 return status;
1435 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1437 struct nfs4_opendata *data = calldata;
1438 struct nfs4_state_owner *sp = data->owner;
1440 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1441 return;
1443 * Check if we still need to send an OPEN call, or if we can use
1444 * a delegation instead.
1446 if (data->state != NULL) {
1447 struct nfs_delegation *delegation;
1449 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1450 goto out_no_action;
1451 rcu_read_lock();
1452 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1453 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1454 can_open_delegated(delegation, data->o_arg.fmode))
1455 goto unlock_no_action;
1456 rcu_read_unlock();
1458 /* Update sequence id. */
1459 data->o_arg.id = sp->so_owner_id.id;
1460 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1461 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1462 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1463 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1465 data->timestamp = jiffies;
1466 if (nfs4_setup_sequence(data->o_arg.server,
1467 &data->o_arg.seq_args,
1468 &data->o_res.seq_res, 1, task))
1469 return;
1470 rpc_call_start(task);
1471 return;
1472 unlock_no_action:
1473 rcu_read_unlock();
1474 out_no_action:
1475 task->tk_action = NULL;
1479 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1481 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1482 nfs4_open_prepare(task, calldata);
1485 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1487 struct nfs4_opendata *data = calldata;
1489 data->rpc_status = task->tk_status;
1491 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1492 return;
1494 if (task->tk_status == 0) {
1495 switch (data->o_res.f_attr->mode & S_IFMT) {
1496 case S_IFREG:
1497 break;
1498 case S_IFLNK:
1499 data->rpc_status = -ELOOP;
1500 break;
1501 case S_IFDIR:
1502 data->rpc_status = -EISDIR;
1503 break;
1504 default:
1505 data->rpc_status = -ENOTDIR;
1507 renew_lease(data->o_res.server, data->timestamp);
1508 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1509 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1511 data->rpc_done = 1;
1514 static void nfs4_open_release(void *calldata)
1516 struct nfs4_opendata *data = calldata;
1517 struct nfs4_state *state = NULL;
1519 /* If this request hasn't been cancelled, do nothing */
1520 if (data->cancelled == 0)
1521 goto out_free;
1522 /* In case of error, no cleanup! */
1523 if (data->rpc_status != 0 || !data->rpc_done)
1524 goto out_free;
1525 /* In case we need an open_confirm, no cleanup! */
1526 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1527 goto out_free;
1528 state = nfs4_opendata_to_nfs4_state(data);
1529 if (!IS_ERR(state))
1530 nfs4_close_state(state, data->o_arg.fmode);
1531 out_free:
1532 nfs4_opendata_put(data);
1535 static const struct rpc_call_ops nfs4_open_ops = {
1536 .rpc_call_prepare = nfs4_open_prepare,
1537 .rpc_call_done = nfs4_open_done,
1538 .rpc_release = nfs4_open_release,
1541 static const struct rpc_call_ops nfs4_recover_open_ops = {
1542 .rpc_call_prepare = nfs4_recover_open_prepare,
1543 .rpc_call_done = nfs4_open_done,
1544 .rpc_release = nfs4_open_release,
1547 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_server *server = NFS_SERVER(dir);
1551 struct nfs_openargs *o_arg = &data->o_arg;
1552 struct nfs_openres *o_res = &data->o_res;
1553 struct rpc_task *task;
1554 struct rpc_message msg = {
1555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1556 .rpc_argp = o_arg,
1557 .rpc_resp = o_res,
1558 .rpc_cred = data->owner->so_cred,
1560 struct rpc_task_setup task_setup_data = {
1561 .rpc_client = server->client,
1562 .rpc_message = &msg,
1563 .callback_ops = &nfs4_open_ops,
1564 .callback_data = data,
1565 .workqueue = nfsiod_workqueue,
1566 .flags = RPC_TASK_ASYNC,
1568 int status;
1570 kref_get(&data->kref);
1571 data->rpc_done = 0;
1572 data->rpc_status = 0;
1573 data->cancelled = 0;
1574 if (isrecover)
1575 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1576 task = rpc_run_task(&task_setup_data);
1577 if (IS_ERR(task))
1578 return PTR_ERR(task);
1579 status = nfs4_wait_for_completion_rpc_task(task);
1580 if (status != 0) {
1581 data->cancelled = 1;
1582 smp_wmb();
1583 } else
1584 status = data->rpc_status;
1585 rpc_put_task(task);
1587 return status;
1590 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1592 struct inode *dir = data->dir->d_inode;
1593 struct nfs_openres *o_res = &data->o_res;
1594 int status;
1596 status = nfs4_run_open_task(data, 1);
1597 if (status != 0 || !data->rpc_done)
1598 return status;
1600 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1602 nfs_refresh_inode(dir, o_res->dir_attr);
1604 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1605 status = _nfs4_proc_open_confirm(data);
1606 if (status != 0)
1607 return status;
1610 return status;
1614 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1616 static int _nfs4_proc_open(struct nfs4_opendata *data)
1618 struct inode *dir = data->dir->d_inode;
1619 struct nfs_server *server = NFS_SERVER(dir);
1620 struct nfs_openargs *o_arg = &data->o_arg;
1621 struct nfs_openres *o_res = &data->o_res;
1622 int status;
1624 status = nfs4_run_open_task(data, 0);
1625 if (!data->rpc_done)
1626 return status;
1627 if (status != 0) {
1628 if (status == -NFS4ERR_BADNAME &&
1629 !(o_arg->open_flags & O_CREAT))
1630 return -ENOENT;
1631 return status;
1634 nfs_fattr_map_and_free_names(server, &data->f_attr);
1636 if (o_arg->open_flags & O_CREAT) {
1637 update_changeattr(dir, &o_res->cinfo);
1638 nfs_post_op_update_inode(dir, o_res->dir_attr);
1639 } else
1640 nfs_refresh_inode(dir, o_res->dir_attr);
1641 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1642 server->caps &= ~NFS_CAP_POSIX_LOCK;
1643 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1644 status = _nfs4_proc_open_confirm(data);
1645 if (status != 0)
1646 return status;
1648 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1649 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1650 return 0;
1653 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1655 unsigned int loop;
1656 int ret;
1658 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1659 ret = nfs4_wait_clnt_recover(clp);
1660 if (ret != 0)
1661 break;
1662 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1663 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1664 break;
1665 nfs4_schedule_state_manager(clp);
1666 ret = -EIO;
1668 return ret;
1671 static int nfs4_recover_expired_lease(struct nfs_server *server)
1673 return nfs4_client_recover_expired_lease(server->nfs_client);
1677 * OPEN_EXPIRED:
1678 * reclaim state on the server after a network partition.
1679 * Assumes caller holds the appropriate lock
1681 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1683 struct nfs4_opendata *opendata;
1684 int ret;
1686 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1687 if (IS_ERR(opendata))
1688 return PTR_ERR(opendata);
1689 ret = nfs4_open_recover(opendata, state);
1690 if (ret == -ESTALE)
1691 d_drop(ctx->dentry);
1692 nfs4_opendata_put(opendata);
1693 return ret;
1696 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1698 struct nfs_server *server = NFS_SERVER(state->inode);
1699 struct nfs4_exception exception = { };
1700 int err;
1702 do {
1703 err = _nfs4_open_expired(ctx, state);
1704 switch (err) {
1705 default:
1706 goto out;
1707 case -NFS4ERR_GRACE:
1708 case -NFS4ERR_DELAY:
1709 nfs4_handle_exception(server, err, &exception);
1710 err = 0;
1712 } while (exception.retry);
1713 out:
1714 return err;
1717 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1719 struct nfs_open_context *ctx;
1720 int ret;
1722 ctx = nfs4_state_find_open_context(state);
1723 if (IS_ERR(ctx))
1724 return PTR_ERR(ctx);
1725 ret = nfs4_do_open_expired(ctx, state);
1726 put_nfs_open_context(ctx);
1727 return ret;
1730 #if defined(CONFIG_NFS_V4_1)
1731 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1733 int status;
1734 struct nfs_server *server = NFS_SERVER(state->inode);
1736 status = nfs41_test_stateid(server, state);
1737 if (status == NFS_OK)
1738 return 0;
1739 nfs41_free_stateid(server, state);
1740 return nfs4_open_expired(sp, state);
1742 #endif
1745 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1746 * fields corresponding to attributes that were used to store the verifier.
1747 * Make sure we clobber those fields in the later setattr call
1749 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1751 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1752 !(sattr->ia_valid & ATTR_ATIME_SET))
1753 sattr->ia_valid |= ATTR_ATIME;
1755 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1756 !(sattr->ia_valid & ATTR_MTIME_SET))
1757 sattr->ia_valid |= ATTR_MTIME;
1761 * Returns a referenced nfs4_state
1763 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)
1765 struct nfs4_state_owner *sp;
1766 struct nfs4_state *state = NULL;
1767 struct nfs_server *server = NFS_SERVER(dir);
1768 struct nfs4_opendata *opendata;
1769 int status;
1771 /* Protect against reboot recovery conflicts */
1772 status = -ENOMEM;
1773 if (!(sp = nfs4_get_state_owner(server, cred))) {
1774 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1775 goto out_err;
1777 status = nfs4_recover_expired_lease(server);
1778 if (status != 0)
1779 goto err_put_state_owner;
1780 if (dentry->d_inode != NULL)
1781 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1782 status = -ENOMEM;
1783 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1784 if (opendata == NULL)
1785 goto err_put_state_owner;
1787 if (dentry->d_inode != NULL)
1788 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1790 status = _nfs4_proc_open(opendata);
1791 if (status != 0)
1792 goto err_opendata_put;
1794 state = nfs4_opendata_to_nfs4_state(opendata);
1795 status = PTR_ERR(state);
1796 if (IS_ERR(state))
1797 goto err_opendata_put;
1798 if (server->caps & NFS_CAP_POSIX_LOCK)
1799 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1801 if (opendata->o_arg.open_flags & O_EXCL) {
1802 nfs4_exclusive_attrset(opendata, sattr);
1804 nfs_fattr_init(opendata->o_res.f_attr);
1805 status = nfs4_do_setattr(state->inode, cred,
1806 opendata->o_res.f_attr, sattr,
1807 state);
1808 if (status == 0)
1809 nfs_setattr_update_inode(state->inode, sattr);
1810 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1812 nfs4_opendata_put(opendata);
1813 nfs4_put_state_owner(sp);
1814 *res = state;
1815 return 0;
1816 err_opendata_put:
1817 nfs4_opendata_put(opendata);
1818 err_put_state_owner:
1819 nfs4_put_state_owner(sp);
1820 out_err:
1821 *res = NULL;
1822 return status;
1826 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)
1828 struct nfs4_exception exception = { };
1829 struct nfs4_state *res;
1830 int status;
1832 do {
1833 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1834 if (status == 0)
1835 break;
1836 /* NOTE: BAD_SEQID means the server and client disagree about the
1837 * book-keeping w.r.t. state-changing operations
1838 * (OPEN/CLOSE/LOCK/LOCKU...)
1839 * It is actually a sign of a bug on the client or on the server.
1841 * If we receive a BAD_SEQID error in the particular case of
1842 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1843 * have unhashed the old state_owner for us, and that we can
1844 * therefore safely retry using a new one. We should still warn
1845 * the user though...
1847 if (status == -NFS4ERR_BAD_SEQID) {
1848 pr_warn_ratelimited("NFS: v4 server %s "
1849 " returned a bad sequence-id error!\n",
1850 NFS_SERVER(dir)->nfs_client->cl_hostname);
1851 exception.retry = 1;
1852 continue;
1855 * BAD_STATEID on OPEN means that the server cancelled our
1856 * state before it received the OPEN_CONFIRM.
1857 * Recover by retrying the request as per the discussion
1858 * on Page 181 of RFC3530.
1860 if (status == -NFS4ERR_BAD_STATEID) {
1861 exception.retry = 1;
1862 continue;
1864 if (status == -EAGAIN) {
1865 /* We must have found a delegation */
1866 exception.retry = 1;
1867 continue;
1869 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1870 status, &exception));
1871 } while (exception.retry);
1872 return res;
1875 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1876 struct nfs_fattr *fattr, struct iattr *sattr,
1877 struct nfs4_state *state)
1879 struct nfs_server *server = NFS_SERVER(inode);
1880 struct nfs_setattrargs arg = {
1881 .fh = NFS_FH(inode),
1882 .iap = sattr,
1883 .server = server,
1884 .bitmask = server->attr_bitmask,
1886 struct nfs_setattrres res = {
1887 .fattr = fattr,
1888 .server = server,
1890 struct rpc_message msg = {
1891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1892 .rpc_argp = &arg,
1893 .rpc_resp = &res,
1894 .rpc_cred = cred,
1896 unsigned long timestamp = jiffies;
1897 int status;
1899 nfs_fattr_init(fattr);
1901 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1902 /* Use that stateid */
1903 } else if (state != NULL) {
1904 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1905 } else
1906 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1908 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1909 if (status == 0 && state != NULL)
1910 renew_lease(server, timestamp);
1911 return status;
1914 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1915 struct nfs_fattr *fattr, struct iattr *sattr,
1916 struct nfs4_state *state)
1918 struct nfs_server *server = NFS_SERVER(inode);
1919 struct nfs4_exception exception = {
1920 .state = state,
1921 .inode = inode,
1923 int err;
1924 do {
1925 err = nfs4_handle_exception(server,
1926 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1927 &exception);
1928 } while (exception.retry);
1929 return err;
1932 struct nfs4_closedata {
1933 struct inode *inode;
1934 struct nfs4_state *state;
1935 struct nfs_closeargs arg;
1936 struct nfs_closeres res;
1937 struct nfs_fattr fattr;
1938 unsigned long timestamp;
1939 bool roc;
1940 u32 roc_barrier;
1943 static void nfs4_free_closedata(void *data)
1945 struct nfs4_closedata *calldata = data;
1946 struct nfs4_state_owner *sp = calldata->state->owner;
1947 struct super_block *sb = calldata->state->inode->i_sb;
1949 if (calldata->roc)
1950 pnfs_roc_release(calldata->state->inode);
1951 nfs4_put_open_state(calldata->state);
1952 nfs_free_seqid(calldata->arg.seqid);
1953 nfs4_put_state_owner(sp);
1954 nfs_sb_deactive(sb);
1955 kfree(calldata);
1958 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1959 fmode_t fmode)
1961 spin_lock(&state->owner->so_lock);
1962 if (!(fmode & FMODE_READ))
1963 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1964 if (!(fmode & FMODE_WRITE))
1965 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1966 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1967 spin_unlock(&state->owner->so_lock);
1970 static void nfs4_close_done(struct rpc_task *task, void *data)
1972 struct nfs4_closedata *calldata = data;
1973 struct nfs4_state *state = calldata->state;
1974 struct nfs_server *server = NFS_SERVER(calldata->inode);
1976 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1977 return;
1978 /* hmm. we are done with the inode, and in the process of freeing
1979 * the state_owner. we keep this around to process errors
1981 switch (task->tk_status) {
1982 case 0:
1983 if (calldata->roc)
1984 pnfs_roc_set_barrier(state->inode,
1985 calldata->roc_barrier);
1986 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1987 renew_lease(server, calldata->timestamp);
1988 nfs4_close_clear_stateid_flags(state,
1989 calldata->arg.fmode);
1990 break;
1991 case -NFS4ERR_STALE_STATEID:
1992 case -NFS4ERR_OLD_STATEID:
1993 case -NFS4ERR_BAD_STATEID:
1994 case -NFS4ERR_EXPIRED:
1995 if (calldata->arg.fmode == 0)
1996 break;
1997 default:
1998 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1999 rpc_restart_call_prepare(task);
2001 nfs_release_seqid(calldata->arg.seqid);
2002 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2005 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2007 struct nfs4_closedata *calldata = data;
2008 struct nfs4_state *state = calldata->state;
2009 int call_close = 0;
2011 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2012 return;
2014 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2015 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2016 spin_lock(&state->owner->so_lock);
2017 /* Calculate the change in open mode */
2018 if (state->n_rdwr == 0) {
2019 if (state->n_rdonly == 0) {
2020 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2021 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2022 calldata->arg.fmode &= ~FMODE_READ;
2024 if (state->n_wronly == 0) {
2025 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2026 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2027 calldata->arg.fmode &= ~FMODE_WRITE;
2030 spin_unlock(&state->owner->so_lock);
2032 if (!call_close) {
2033 /* Note: exit _without_ calling nfs4_close_done */
2034 task->tk_action = NULL;
2035 return;
2038 if (calldata->arg.fmode == 0) {
2039 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2040 if (calldata->roc &&
2041 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2042 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2043 task, NULL);
2044 return;
2048 nfs_fattr_init(calldata->res.fattr);
2049 calldata->timestamp = jiffies;
2050 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2051 &calldata->arg.seq_args, &calldata->res.seq_res,
2052 1, task))
2053 return;
2054 rpc_call_start(task);
2057 static const struct rpc_call_ops nfs4_close_ops = {
2058 .rpc_call_prepare = nfs4_close_prepare,
2059 .rpc_call_done = nfs4_close_done,
2060 .rpc_release = nfs4_free_closedata,
2064 * It is possible for data to be read/written from a mem-mapped file
2065 * after the sys_close call (which hits the vfs layer as a flush).
2066 * This means that we can't safely call nfsv4 close on a file until
2067 * the inode is cleared. This in turn means that we are not good
2068 * NFSv4 citizens - we do not indicate to the server to update the file's
2069 * share state even when we are done with one of the three share
2070 * stateid's in the inode.
2072 * NOTE: Caller must be holding the sp->so_owner semaphore!
2074 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2076 struct nfs_server *server = NFS_SERVER(state->inode);
2077 struct nfs4_closedata *calldata;
2078 struct nfs4_state_owner *sp = state->owner;
2079 struct rpc_task *task;
2080 struct rpc_message msg = {
2081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2082 .rpc_cred = state->owner->so_cred,
2084 struct rpc_task_setup task_setup_data = {
2085 .rpc_client = server->client,
2086 .rpc_message = &msg,
2087 .callback_ops = &nfs4_close_ops,
2088 .workqueue = nfsiod_workqueue,
2089 .flags = RPC_TASK_ASYNC,
2091 int status = -ENOMEM;
2093 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2094 if (calldata == NULL)
2095 goto out;
2096 calldata->inode = state->inode;
2097 calldata->state = state;
2098 calldata->arg.fh = NFS_FH(state->inode);
2099 calldata->arg.stateid = &state->open_stateid;
2100 /* Serialization for the sequence id */
2101 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2102 if (calldata->arg.seqid == NULL)
2103 goto out_free_calldata;
2104 calldata->arg.fmode = 0;
2105 calldata->arg.bitmask = server->cache_consistency_bitmask;
2106 calldata->res.fattr = &calldata->fattr;
2107 calldata->res.seqid = calldata->arg.seqid;
2108 calldata->res.server = server;
2109 calldata->roc = roc;
2110 nfs_sb_active(calldata->inode->i_sb);
2112 msg.rpc_argp = &calldata->arg;
2113 msg.rpc_resp = &calldata->res;
2114 task_setup_data.callback_data = calldata;
2115 task = rpc_run_task(&task_setup_data);
2116 if (IS_ERR(task))
2117 return PTR_ERR(task);
2118 status = 0;
2119 if (wait)
2120 status = rpc_wait_for_completion_task(task);
2121 rpc_put_task(task);
2122 return status;
2123 out_free_calldata:
2124 kfree(calldata);
2125 out:
2126 if (roc)
2127 pnfs_roc_release(state->inode);
2128 nfs4_put_open_state(state);
2129 nfs4_put_state_owner(sp);
2130 return status;
2133 static struct inode *
2134 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2136 struct nfs4_state *state;
2138 /* Protect against concurrent sillydeletes */
2139 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2140 if (IS_ERR(state))
2141 return ERR_CAST(state);
2142 ctx->state = state;
2143 return igrab(state->inode);
2146 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2148 if (ctx->state == NULL)
2149 return;
2150 if (is_sync)
2151 nfs4_close_sync(ctx->state, ctx->mode);
2152 else
2153 nfs4_close_state(ctx->state, ctx->mode);
2156 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2158 struct nfs4_server_caps_arg args = {
2159 .fhandle = fhandle,
2161 struct nfs4_server_caps_res res = {};
2162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2164 .rpc_argp = &args,
2165 .rpc_resp = &res,
2167 int status;
2169 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2170 if (status == 0) {
2171 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2172 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2173 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2174 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2175 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2176 NFS_CAP_CTIME|NFS_CAP_MTIME);
2177 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2178 server->caps |= NFS_CAP_ACLS;
2179 if (res.has_links != 0)
2180 server->caps |= NFS_CAP_HARDLINKS;
2181 if (res.has_symlinks != 0)
2182 server->caps |= NFS_CAP_SYMLINKS;
2183 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2184 server->caps |= NFS_CAP_FILEID;
2185 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2186 server->caps |= NFS_CAP_MODE;
2187 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2188 server->caps |= NFS_CAP_NLINK;
2189 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2190 server->caps |= NFS_CAP_OWNER;
2191 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2192 server->caps |= NFS_CAP_OWNER_GROUP;
2193 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2194 server->caps |= NFS_CAP_ATIME;
2195 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2196 server->caps |= NFS_CAP_CTIME;
2197 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2198 server->caps |= NFS_CAP_MTIME;
2200 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2201 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2202 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2203 server->acl_bitmask = res.acl_bitmask;
2206 return status;
2209 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2211 struct nfs4_exception exception = { };
2212 int err;
2213 do {
2214 err = nfs4_handle_exception(server,
2215 _nfs4_server_capabilities(server, fhandle),
2216 &exception);
2217 } while (exception.retry);
2218 return err;
2221 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2222 struct nfs_fsinfo *info)
2224 struct nfs4_lookup_root_arg args = {
2225 .bitmask = nfs4_fattr_bitmap,
2227 struct nfs4_lookup_res res = {
2228 .server = server,
2229 .fattr = info->fattr,
2230 .fh = fhandle,
2232 struct rpc_message msg = {
2233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2234 .rpc_argp = &args,
2235 .rpc_resp = &res,
2238 nfs_fattr_init(info->fattr);
2239 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2242 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2243 struct nfs_fsinfo *info)
2245 struct nfs4_exception exception = { };
2246 int err;
2247 do {
2248 err = _nfs4_lookup_root(server, fhandle, info);
2249 switch (err) {
2250 case 0:
2251 case -NFS4ERR_WRONGSEC:
2252 goto out;
2253 default:
2254 err = nfs4_handle_exception(server, err, &exception);
2256 } while (exception.retry);
2257 out:
2258 return err;
2261 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2262 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2264 struct rpc_auth *auth;
2265 int ret;
2267 auth = rpcauth_create(flavor, server->client);
2268 if (!auth) {
2269 ret = -EIO;
2270 goto out;
2272 ret = nfs4_lookup_root(server, fhandle, info);
2273 out:
2274 return ret;
2277 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2278 struct nfs_fsinfo *info)
2280 int i, len, status = 0;
2281 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2283 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2284 flav_array[len] = RPC_AUTH_NULL;
2285 len += 1;
2287 for (i = 0; i < len; i++) {
2288 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2289 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2290 continue;
2291 break;
2294 * -EACCESS could mean that the user doesn't have correct permissions
2295 * to access the mount. It could also mean that we tried to mount
2296 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2297 * existing mount programs don't handle -EACCES very well so it should
2298 * be mapped to -EPERM instead.
2300 if (status == -EACCES)
2301 status = -EPERM;
2302 return status;
2306 * get the file handle for the "/" directory on the server
2308 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2309 struct nfs_fsinfo *info)
2311 int minor_version = server->nfs_client->cl_minorversion;
2312 int status = nfs4_lookup_root(server, fhandle, info);
2313 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2315 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2316 * by nfs4_map_errors() as this function exits.
2318 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2319 if (status == 0)
2320 status = nfs4_server_capabilities(server, fhandle);
2321 if (status == 0)
2322 status = nfs4_do_fsinfo(server, fhandle, info);
2323 return nfs4_map_errors(status);
2326 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2328 * Get locations and (maybe) other attributes of a referral.
2329 * Note that we'll actually follow the referral later when
2330 * we detect fsid mismatch in inode revalidation
2332 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2333 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2335 int status = -ENOMEM;
2336 struct page *page = NULL;
2337 struct nfs4_fs_locations *locations = NULL;
2339 page = alloc_page(GFP_KERNEL);
2340 if (page == NULL)
2341 goto out;
2342 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2343 if (locations == NULL)
2344 goto out;
2346 status = nfs4_proc_fs_locations(dir, name, locations, page);
2347 if (status != 0)
2348 goto out;
2349 /* Make sure server returned a different fsid for the referral */
2350 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2351 dprintk("%s: server did not return a different fsid for"
2352 " a referral at %s\n", __func__, name->name);
2353 status = -EIO;
2354 goto out;
2356 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2357 nfs_fixup_referral_attributes(&locations->fattr);
2359 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2360 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2361 memset(fhandle, 0, sizeof(struct nfs_fh));
2362 out:
2363 if (page)
2364 __free_page(page);
2365 kfree(locations);
2366 return status;
2369 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2371 struct nfs4_getattr_arg args = {
2372 .fh = fhandle,
2373 .bitmask = server->attr_bitmask,
2375 struct nfs4_getattr_res res = {
2376 .fattr = fattr,
2377 .server = server,
2379 struct rpc_message msg = {
2380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2381 .rpc_argp = &args,
2382 .rpc_resp = &res,
2385 nfs_fattr_init(fattr);
2386 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2389 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2391 struct nfs4_exception exception = { };
2392 int err;
2393 do {
2394 err = nfs4_handle_exception(server,
2395 _nfs4_proc_getattr(server, fhandle, fattr),
2396 &exception);
2397 } while (exception.retry);
2398 return err;
2402 * The file is not closed if it is opened due to the a request to change
2403 * the size of the file. The open call will not be needed once the
2404 * VFS layer lookup-intents are implemented.
2406 * Close is called when the inode is destroyed.
2407 * If we haven't opened the file for O_WRONLY, we
2408 * need to in the size_change case to obtain a stateid.
2410 * Got race?
2411 * Because OPEN is always done by name in nfsv4, it is
2412 * possible that we opened a different file by the same
2413 * name. We can recognize this race condition, but we
2414 * can't do anything about it besides returning an error.
2416 * This will be fixed with VFS changes (lookup-intent).
2418 static int
2419 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2420 struct iattr *sattr)
2422 struct inode *inode = dentry->d_inode;
2423 struct rpc_cred *cred = NULL;
2424 struct nfs4_state *state = NULL;
2425 int status;
2427 if (pnfs_ld_layoutret_on_setattr(inode))
2428 pnfs_return_layout(inode);
2430 nfs_fattr_init(fattr);
2432 /* Search for an existing open(O_WRITE) file */
2433 if (sattr->ia_valid & ATTR_FILE) {
2434 struct nfs_open_context *ctx;
2436 ctx = nfs_file_open_context(sattr->ia_file);
2437 if (ctx) {
2438 cred = ctx->cred;
2439 state = ctx->state;
2443 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2444 if (status == 0)
2445 nfs_setattr_update_inode(inode, sattr);
2446 return status;
2449 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2450 const struct qstr *name, struct nfs_fh *fhandle,
2451 struct nfs_fattr *fattr)
2453 struct nfs_server *server = NFS_SERVER(dir);
2454 int status;
2455 struct nfs4_lookup_arg args = {
2456 .bitmask = server->attr_bitmask,
2457 .dir_fh = NFS_FH(dir),
2458 .name = name,
2460 struct nfs4_lookup_res res = {
2461 .server = server,
2462 .fattr = fattr,
2463 .fh = fhandle,
2465 struct rpc_message msg = {
2466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2467 .rpc_argp = &args,
2468 .rpc_resp = &res,
2471 nfs_fattr_init(fattr);
2473 dprintk("NFS call lookup %s\n", name->name);
2474 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2475 dprintk("NFS reply lookup: %d\n", status);
2476 return status;
2479 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2481 memset(fh, 0, sizeof(struct nfs_fh));
2482 fattr->fsid.major = 1;
2483 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2484 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2485 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2486 fattr->nlink = 2;
2489 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2490 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2492 struct nfs4_exception exception = { };
2493 int err;
2494 do {
2495 int status;
2497 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2498 switch (status) {
2499 case -NFS4ERR_BADNAME:
2500 return -ENOENT;
2501 case -NFS4ERR_MOVED:
2502 return nfs4_get_referral(dir, name, fattr, fhandle);
2503 case -NFS4ERR_WRONGSEC:
2504 nfs_fixup_secinfo_attributes(fattr, fhandle);
2506 err = nfs4_handle_exception(NFS_SERVER(dir),
2507 status, &exception);
2508 } while (exception.retry);
2509 return err;
2512 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2514 struct nfs_server *server = NFS_SERVER(inode);
2515 struct nfs4_accessargs args = {
2516 .fh = NFS_FH(inode),
2517 .bitmask = server->attr_bitmask,
2519 struct nfs4_accessres res = {
2520 .server = server,
2522 struct rpc_message msg = {
2523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2524 .rpc_argp = &args,
2525 .rpc_resp = &res,
2526 .rpc_cred = entry->cred,
2528 int mode = entry->mask;
2529 int status;
2532 * Determine which access bits we want to ask for...
2534 if (mode & MAY_READ)
2535 args.access |= NFS4_ACCESS_READ;
2536 if (S_ISDIR(inode->i_mode)) {
2537 if (mode & MAY_WRITE)
2538 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2539 if (mode & MAY_EXEC)
2540 args.access |= NFS4_ACCESS_LOOKUP;
2541 } else {
2542 if (mode & MAY_WRITE)
2543 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2544 if (mode & MAY_EXEC)
2545 args.access |= NFS4_ACCESS_EXECUTE;
2548 res.fattr = nfs_alloc_fattr();
2549 if (res.fattr == NULL)
2550 return -ENOMEM;
2552 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2553 if (!status) {
2554 entry->mask = 0;
2555 if (res.access & NFS4_ACCESS_READ)
2556 entry->mask |= MAY_READ;
2557 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2558 entry->mask |= MAY_WRITE;
2559 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2560 entry->mask |= MAY_EXEC;
2561 nfs_refresh_inode(inode, res.fattr);
2563 nfs_free_fattr(res.fattr);
2564 return status;
2567 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2569 struct nfs4_exception exception = { };
2570 int err;
2571 do {
2572 err = nfs4_handle_exception(NFS_SERVER(inode),
2573 _nfs4_proc_access(inode, entry),
2574 &exception);
2575 } while (exception.retry);
2576 return err;
2580 * TODO: For the time being, we don't try to get any attributes
2581 * along with any of the zero-copy operations READ, READDIR,
2582 * READLINK, WRITE.
2584 * In the case of the first three, we want to put the GETATTR
2585 * after the read-type operation -- this is because it is hard
2586 * to predict the length of a GETATTR response in v4, and thus
2587 * align the READ data correctly. This means that the GETATTR
2588 * may end up partially falling into the page cache, and we should
2589 * shift it into the 'tail' of the xdr_buf before processing.
2590 * To do this efficiently, we need to know the total length
2591 * of data received, which doesn't seem to be available outside
2592 * of the RPC layer.
2594 * In the case of WRITE, we also want to put the GETATTR after
2595 * the operation -- in this case because we want to make sure
2596 * we get the post-operation mtime and size. This means that
2597 * we can't use xdr_encode_pages() as written: we need a variant
2598 * of it which would leave room in the 'tail' iovec.
2600 * Both of these changes to the XDR layer would in fact be quite
2601 * minor, but I decided to leave them for a subsequent patch.
2603 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2604 unsigned int pgbase, unsigned int pglen)
2606 struct nfs4_readlink args = {
2607 .fh = NFS_FH(inode),
2608 .pgbase = pgbase,
2609 .pglen = pglen,
2610 .pages = &page,
2612 struct nfs4_readlink_res res;
2613 struct rpc_message msg = {
2614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2615 .rpc_argp = &args,
2616 .rpc_resp = &res,
2619 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2622 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2623 unsigned int pgbase, unsigned int pglen)
2625 struct nfs4_exception exception = { };
2626 int err;
2627 do {
2628 err = nfs4_handle_exception(NFS_SERVER(inode),
2629 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2630 &exception);
2631 } while (exception.retry);
2632 return err;
2636 * Got race?
2637 * We will need to arrange for the VFS layer to provide an atomic open.
2638 * Until then, this create/open method is prone to inefficiency and race
2639 * conditions due to the lookup, create, and open VFS calls from sys_open()
2640 * placed on the wire.
2642 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2643 * The file will be opened again in the subsequent VFS open call
2644 * (nfs4_proc_file_open).
2646 * The open for read will just hang around to be used by any process that
2647 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2650 static int
2651 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2652 int flags, struct nfs_open_context *ctx)
2654 struct dentry *de = dentry;
2655 struct nfs4_state *state;
2656 struct rpc_cred *cred = NULL;
2657 fmode_t fmode = 0;
2658 int status = 0;
2660 if (ctx != NULL) {
2661 cred = ctx->cred;
2662 de = ctx->dentry;
2663 fmode = ctx->mode;
2665 sattr->ia_mode &= ~current_umask();
2666 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2667 d_drop(dentry);
2668 if (IS_ERR(state)) {
2669 status = PTR_ERR(state);
2670 goto out;
2672 d_add(dentry, igrab(state->inode));
2673 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2674 if (ctx != NULL)
2675 ctx->state = state;
2676 else
2677 nfs4_close_sync(state, fmode);
2678 out:
2679 return status;
2682 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2684 struct nfs_server *server = NFS_SERVER(dir);
2685 struct nfs_removeargs args = {
2686 .fh = NFS_FH(dir),
2687 .name.len = name->len,
2688 .name.name = name->name,
2689 .bitmask = server->attr_bitmask,
2691 struct nfs_removeres res = {
2692 .server = server,
2694 struct rpc_message msg = {
2695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2696 .rpc_argp = &args,
2697 .rpc_resp = &res,
2699 int status = -ENOMEM;
2701 res.dir_attr = nfs_alloc_fattr();
2702 if (res.dir_attr == NULL)
2703 goto out;
2705 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2706 if (status == 0) {
2707 update_changeattr(dir, &res.cinfo);
2708 nfs_post_op_update_inode(dir, res.dir_attr);
2710 nfs_free_fattr(res.dir_attr);
2711 out:
2712 return status;
2715 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2717 struct nfs4_exception exception = { };
2718 int err;
2719 do {
2720 err = nfs4_handle_exception(NFS_SERVER(dir),
2721 _nfs4_proc_remove(dir, name),
2722 &exception);
2723 } while (exception.retry);
2724 return err;
2727 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2729 struct nfs_server *server = NFS_SERVER(dir);
2730 struct nfs_removeargs *args = msg->rpc_argp;
2731 struct nfs_removeres *res = msg->rpc_resp;
2733 args->bitmask = server->cache_consistency_bitmask;
2734 res->server = server;
2735 res->seq_res.sr_slot = NULL;
2736 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2739 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2741 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2743 if (!nfs4_sequence_done(task, &res->seq_res))
2744 return 0;
2745 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2746 return 0;
2747 update_changeattr(dir, &res->cinfo);
2748 nfs_post_op_update_inode(dir, res->dir_attr);
2749 return 1;
2752 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2754 struct nfs_server *server = NFS_SERVER(dir);
2755 struct nfs_renameargs *arg = msg->rpc_argp;
2756 struct nfs_renameres *res = msg->rpc_resp;
2758 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2759 arg->bitmask = server->attr_bitmask;
2760 res->server = server;
2763 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2764 struct inode *new_dir)
2766 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2768 if (!nfs4_sequence_done(task, &res->seq_res))
2769 return 0;
2770 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2771 return 0;
2773 update_changeattr(old_dir, &res->old_cinfo);
2774 nfs_post_op_update_inode(old_dir, res->old_fattr);
2775 update_changeattr(new_dir, &res->new_cinfo);
2776 nfs_post_op_update_inode(new_dir, res->new_fattr);
2777 return 1;
2780 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2781 struct inode *new_dir, struct qstr *new_name)
2783 struct nfs_server *server = NFS_SERVER(old_dir);
2784 struct nfs_renameargs arg = {
2785 .old_dir = NFS_FH(old_dir),
2786 .new_dir = NFS_FH(new_dir),
2787 .old_name = old_name,
2788 .new_name = new_name,
2789 .bitmask = server->attr_bitmask,
2791 struct nfs_renameres res = {
2792 .server = server,
2794 struct rpc_message msg = {
2795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2796 .rpc_argp = &arg,
2797 .rpc_resp = &res,
2799 int status = -ENOMEM;
2801 res.old_fattr = nfs_alloc_fattr();
2802 res.new_fattr = nfs_alloc_fattr();
2803 if (res.old_fattr == NULL || res.new_fattr == NULL)
2804 goto out;
2806 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2807 if (!status) {
2808 update_changeattr(old_dir, &res.old_cinfo);
2809 nfs_post_op_update_inode(old_dir, res.old_fattr);
2810 update_changeattr(new_dir, &res.new_cinfo);
2811 nfs_post_op_update_inode(new_dir, res.new_fattr);
2813 out:
2814 nfs_free_fattr(res.new_fattr);
2815 nfs_free_fattr(res.old_fattr);
2816 return status;
2819 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2820 struct inode *new_dir, struct qstr *new_name)
2822 struct nfs4_exception exception = { };
2823 int err;
2824 do {
2825 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2826 _nfs4_proc_rename(old_dir, old_name,
2827 new_dir, new_name),
2828 &exception);
2829 } while (exception.retry);
2830 return err;
2833 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2835 struct nfs_server *server = NFS_SERVER(inode);
2836 struct nfs4_link_arg arg = {
2837 .fh = NFS_FH(inode),
2838 .dir_fh = NFS_FH(dir),
2839 .name = name,
2840 .bitmask = server->attr_bitmask,
2842 struct nfs4_link_res res = {
2843 .server = server,
2845 struct rpc_message msg = {
2846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2847 .rpc_argp = &arg,
2848 .rpc_resp = &res,
2850 int status = -ENOMEM;
2852 res.fattr = nfs_alloc_fattr();
2853 res.dir_attr = nfs_alloc_fattr();
2854 if (res.fattr == NULL || res.dir_attr == NULL)
2855 goto out;
2857 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2858 if (!status) {
2859 update_changeattr(dir, &res.cinfo);
2860 nfs_post_op_update_inode(dir, res.dir_attr);
2861 nfs_post_op_update_inode(inode, res.fattr);
2863 out:
2864 nfs_free_fattr(res.dir_attr);
2865 nfs_free_fattr(res.fattr);
2866 return status;
2869 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2871 struct nfs4_exception exception = { };
2872 int err;
2873 do {
2874 err = nfs4_handle_exception(NFS_SERVER(inode),
2875 _nfs4_proc_link(inode, dir, name),
2876 &exception);
2877 } while (exception.retry);
2878 return err;
2881 struct nfs4_createdata {
2882 struct rpc_message msg;
2883 struct nfs4_create_arg arg;
2884 struct nfs4_create_res res;
2885 struct nfs_fh fh;
2886 struct nfs_fattr fattr;
2887 struct nfs_fattr dir_fattr;
2890 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2891 struct qstr *name, struct iattr *sattr, u32 ftype)
2893 struct nfs4_createdata *data;
2895 data = kzalloc(sizeof(*data), GFP_KERNEL);
2896 if (data != NULL) {
2897 struct nfs_server *server = NFS_SERVER(dir);
2899 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2900 data->msg.rpc_argp = &data->arg;
2901 data->msg.rpc_resp = &data->res;
2902 data->arg.dir_fh = NFS_FH(dir);
2903 data->arg.server = server;
2904 data->arg.name = name;
2905 data->arg.attrs = sattr;
2906 data->arg.ftype = ftype;
2907 data->arg.bitmask = server->attr_bitmask;
2908 data->res.server = server;
2909 data->res.fh = &data->fh;
2910 data->res.fattr = &data->fattr;
2911 data->res.dir_fattr = &data->dir_fattr;
2912 nfs_fattr_init(data->res.fattr);
2913 nfs_fattr_init(data->res.dir_fattr);
2915 return data;
2918 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2920 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2921 &data->arg.seq_args, &data->res.seq_res, 1);
2922 if (status == 0) {
2923 update_changeattr(dir, &data->res.dir_cinfo);
2924 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2925 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2927 return status;
2930 static void nfs4_free_createdata(struct nfs4_createdata *data)
2932 kfree(data);
2935 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2936 struct page *page, unsigned int len, struct iattr *sattr)
2938 struct nfs4_createdata *data;
2939 int status = -ENAMETOOLONG;
2941 if (len > NFS4_MAXPATHLEN)
2942 goto out;
2944 status = -ENOMEM;
2945 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2946 if (data == NULL)
2947 goto out;
2949 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2950 data->arg.u.symlink.pages = &page;
2951 data->arg.u.symlink.len = len;
2953 status = nfs4_do_create(dir, dentry, data);
2955 nfs4_free_createdata(data);
2956 out:
2957 return status;
2960 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2961 struct page *page, unsigned int len, struct iattr *sattr)
2963 struct nfs4_exception exception = { };
2964 int err;
2965 do {
2966 err = nfs4_handle_exception(NFS_SERVER(dir),
2967 _nfs4_proc_symlink(dir, dentry, page,
2968 len, sattr),
2969 &exception);
2970 } while (exception.retry);
2971 return err;
2974 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2975 struct iattr *sattr)
2977 struct nfs4_createdata *data;
2978 int status = -ENOMEM;
2980 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2981 if (data == NULL)
2982 goto out;
2984 status = nfs4_do_create(dir, dentry, data);
2986 nfs4_free_createdata(data);
2987 out:
2988 return status;
2991 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2992 struct iattr *sattr)
2994 struct nfs4_exception exception = { };
2995 int err;
2997 sattr->ia_mode &= ~current_umask();
2998 do {
2999 err = nfs4_handle_exception(NFS_SERVER(dir),
3000 _nfs4_proc_mkdir(dir, dentry, sattr),
3001 &exception);
3002 } while (exception.retry);
3003 return err;
3006 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3007 u64 cookie, struct page **pages, unsigned int count, int plus)
3009 struct inode *dir = dentry->d_inode;
3010 struct nfs4_readdir_arg args = {
3011 .fh = NFS_FH(dir),
3012 .pages = pages,
3013 .pgbase = 0,
3014 .count = count,
3015 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3016 .plus = plus,
3018 struct nfs4_readdir_res res;
3019 struct rpc_message msg = {
3020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3021 .rpc_argp = &args,
3022 .rpc_resp = &res,
3023 .rpc_cred = cred,
3025 int status;
3027 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3028 dentry->d_parent->d_name.name,
3029 dentry->d_name.name,
3030 (unsigned long long)cookie);
3031 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3032 res.pgbase = args.pgbase;
3033 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3034 if (status >= 0) {
3035 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3036 status += args.pgbase;
3039 nfs_invalidate_atime(dir);
3041 dprintk("%s: returns %d\n", __func__, status);
3042 return status;
3045 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3046 u64 cookie, struct page **pages, unsigned int count, int plus)
3048 struct nfs4_exception exception = { };
3049 int err;
3050 do {
3051 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3052 _nfs4_proc_readdir(dentry, cred, cookie,
3053 pages, count, plus),
3054 &exception);
3055 } while (exception.retry);
3056 return err;
3059 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3060 struct iattr *sattr, dev_t rdev)
3062 struct nfs4_createdata *data;
3063 int mode = sattr->ia_mode;
3064 int status = -ENOMEM;
3066 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3067 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3069 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3070 if (data == NULL)
3071 goto out;
3073 if (S_ISFIFO(mode))
3074 data->arg.ftype = NF4FIFO;
3075 else if (S_ISBLK(mode)) {
3076 data->arg.ftype = NF4BLK;
3077 data->arg.u.device.specdata1 = MAJOR(rdev);
3078 data->arg.u.device.specdata2 = MINOR(rdev);
3080 else if (S_ISCHR(mode)) {
3081 data->arg.ftype = NF4CHR;
3082 data->arg.u.device.specdata1 = MAJOR(rdev);
3083 data->arg.u.device.specdata2 = MINOR(rdev);
3086 status = nfs4_do_create(dir, dentry, data);
3088 nfs4_free_createdata(data);
3089 out:
3090 return status;
3093 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3094 struct iattr *sattr, dev_t rdev)
3096 struct nfs4_exception exception = { };
3097 int err;
3099 sattr->ia_mode &= ~current_umask();
3100 do {
3101 err = nfs4_handle_exception(NFS_SERVER(dir),
3102 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3103 &exception);
3104 } while (exception.retry);
3105 return err;
3108 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3109 struct nfs_fsstat *fsstat)
3111 struct nfs4_statfs_arg args = {
3112 .fh = fhandle,
3113 .bitmask = server->attr_bitmask,
3115 struct nfs4_statfs_res res = {
3116 .fsstat = fsstat,
3118 struct rpc_message msg = {
3119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3120 .rpc_argp = &args,
3121 .rpc_resp = &res,
3124 nfs_fattr_init(fsstat->fattr);
3125 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3128 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3130 struct nfs4_exception exception = { };
3131 int err;
3132 do {
3133 err = nfs4_handle_exception(server,
3134 _nfs4_proc_statfs(server, fhandle, fsstat),
3135 &exception);
3136 } while (exception.retry);
3137 return err;
3140 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3141 struct nfs_fsinfo *fsinfo)
3143 struct nfs4_fsinfo_arg args = {
3144 .fh = fhandle,
3145 .bitmask = server->attr_bitmask,
3147 struct nfs4_fsinfo_res res = {
3148 .fsinfo = fsinfo,
3150 struct rpc_message msg = {
3151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3152 .rpc_argp = &args,
3153 .rpc_resp = &res,
3156 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3159 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3161 struct nfs4_exception exception = { };
3162 int err;
3164 do {
3165 err = nfs4_handle_exception(server,
3166 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3167 &exception);
3168 } while (exception.retry);
3169 return err;
3172 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3174 nfs_fattr_init(fsinfo->fattr);
3175 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3178 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3179 struct nfs_pathconf *pathconf)
3181 struct nfs4_pathconf_arg args = {
3182 .fh = fhandle,
3183 .bitmask = server->attr_bitmask,
3185 struct nfs4_pathconf_res res = {
3186 .pathconf = pathconf,
3188 struct rpc_message msg = {
3189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3190 .rpc_argp = &args,
3191 .rpc_resp = &res,
3194 /* None of the pathconf attributes are mandatory to implement */
3195 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3196 memset(pathconf, 0, sizeof(*pathconf));
3197 return 0;
3200 nfs_fattr_init(pathconf->fattr);
3201 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3204 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3205 struct nfs_pathconf *pathconf)
3207 struct nfs4_exception exception = { };
3208 int err;
3210 do {
3211 err = nfs4_handle_exception(server,
3212 _nfs4_proc_pathconf(server, fhandle, pathconf),
3213 &exception);
3214 } while (exception.retry);
3215 return err;
3218 void __nfs4_read_done_cb(struct nfs_read_data *data)
3220 nfs_invalidate_atime(data->inode);
3223 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3225 struct nfs_server *server = NFS_SERVER(data->inode);
3227 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3228 rpc_restart_call_prepare(task);
3229 return -EAGAIN;
3232 __nfs4_read_done_cb(data);
3233 if (task->tk_status > 0)
3234 renew_lease(server, data->timestamp);
3235 return 0;
3238 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3241 dprintk("--> %s\n", __func__);
3243 if (!nfs4_sequence_done(task, &data->res.seq_res))
3244 return -EAGAIN;
3246 return data->read_done_cb ? data->read_done_cb(task, data) :
3247 nfs4_read_done_cb(task, data);
3250 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3252 data->timestamp = jiffies;
3253 data->read_done_cb = nfs4_read_done_cb;
3254 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3257 /* Reset the the nfs_read_data to send the read to the MDS. */
3258 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3260 dprintk("%s Reset task for i/o through\n", __func__);
3261 put_lseg(data->lseg);
3262 data->lseg = NULL;
3263 /* offsets will differ in the dense stripe case */
3264 data->args.offset = data->mds_offset;
3265 data->ds_clp = NULL;
3266 data->args.fh = NFS_FH(data->inode);
3267 data->read_done_cb = nfs4_read_done_cb;
3268 task->tk_ops = data->mds_ops;
3269 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3271 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3273 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3275 struct inode *inode = data->inode;
3277 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3278 rpc_restart_call_prepare(task);
3279 return -EAGAIN;
3281 if (task->tk_status >= 0) {
3282 renew_lease(NFS_SERVER(inode), data->timestamp);
3283 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3285 return 0;
3288 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3290 if (!nfs4_sequence_done(task, &data->res.seq_res))
3291 return -EAGAIN;
3292 return data->write_done_cb ? data->write_done_cb(task, data) :
3293 nfs4_write_done_cb(task, data);
3296 /* Reset the the nfs_write_data to send the write to the MDS. */
3297 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3299 dprintk("%s Reset task for i/o through\n", __func__);
3300 put_lseg(data->lseg);
3301 data->lseg = NULL;
3302 data->ds_clp = NULL;
3303 data->write_done_cb = nfs4_write_done_cb;
3304 data->args.fh = NFS_FH(data->inode);
3305 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3306 data->args.offset = data->mds_offset;
3307 data->res.fattr = &data->fattr;
3308 task->tk_ops = data->mds_ops;
3309 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3311 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3313 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3315 struct nfs_server *server = NFS_SERVER(data->inode);
3317 if (data->lseg) {
3318 data->args.bitmask = NULL;
3319 data->res.fattr = NULL;
3320 } else
3321 data->args.bitmask = server->cache_consistency_bitmask;
3322 if (!data->write_done_cb)
3323 data->write_done_cb = nfs4_write_done_cb;
3324 data->res.server = server;
3325 data->timestamp = jiffies;
3327 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3330 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3332 struct inode *inode = data->inode;
3334 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3335 rpc_restart_call_prepare(task);
3336 return -EAGAIN;
3338 nfs_refresh_inode(inode, data->res.fattr);
3339 return 0;
3342 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3344 if (!nfs4_sequence_done(task, &data->res.seq_res))
3345 return -EAGAIN;
3346 return data->write_done_cb(task, data);
3349 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3351 struct nfs_server *server = NFS_SERVER(data->inode);
3353 if (data->lseg) {
3354 data->args.bitmask = NULL;
3355 data->res.fattr = NULL;
3356 } else
3357 data->args.bitmask = server->cache_consistency_bitmask;
3358 if (!data->write_done_cb)
3359 data->write_done_cb = nfs4_commit_done_cb;
3360 data->res.server = server;
3361 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3364 struct nfs4_renewdata {
3365 struct nfs_client *client;
3366 unsigned long timestamp;
3370 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3371 * standalone procedure for queueing an asynchronous RENEW.
3373 static void nfs4_renew_release(void *calldata)
3375 struct nfs4_renewdata *data = calldata;
3376 struct nfs_client *clp = data->client;
3378 if (atomic_read(&clp->cl_count) > 1)
3379 nfs4_schedule_state_renewal(clp);
3380 nfs_put_client(clp);
3381 kfree(data);
3384 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3386 struct nfs4_renewdata *data = calldata;
3387 struct nfs_client *clp = data->client;
3388 unsigned long timestamp = data->timestamp;
3390 if (task->tk_status < 0) {
3391 /* Unless we're shutting down, schedule state recovery! */
3392 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3393 return;
3394 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3395 nfs4_schedule_lease_recovery(clp);
3396 return;
3398 nfs4_schedule_path_down_recovery(clp);
3400 do_renew_lease(clp, timestamp);
3403 static const struct rpc_call_ops nfs4_renew_ops = {
3404 .rpc_call_done = nfs4_renew_done,
3405 .rpc_release = nfs4_renew_release,
3408 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3410 struct rpc_message msg = {
3411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3412 .rpc_argp = clp,
3413 .rpc_cred = cred,
3415 struct nfs4_renewdata *data;
3417 if (renew_flags == 0)
3418 return 0;
3419 if (!atomic_inc_not_zero(&clp->cl_count))
3420 return -EIO;
3421 data = kmalloc(sizeof(*data), GFP_NOFS);
3422 if (data == NULL)
3423 return -ENOMEM;
3424 data->client = clp;
3425 data->timestamp = jiffies;
3426 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3427 &nfs4_renew_ops, data);
3430 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3434 .rpc_argp = clp,
3435 .rpc_cred = cred,
3437 unsigned long now = jiffies;
3438 int status;
3440 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3441 if (status < 0)
3442 return status;
3443 do_renew_lease(clp, now);
3444 return 0;
3447 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3449 return (server->caps & NFS_CAP_ACLS)
3450 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3451 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3454 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3455 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3456 * the stack.
3458 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3460 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3461 struct page **pages, unsigned int *pgbase)
3463 struct page *newpage, **spages;
3464 int rc = 0;
3465 size_t len;
3466 spages = pages;
3468 do {
3469 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3470 newpage = alloc_page(GFP_KERNEL);
3472 if (newpage == NULL)
3473 goto unwind;
3474 memcpy(page_address(newpage), buf, len);
3475 buf += len;
3476 buflen -= len;
3477 *pages++ = newpage;
3478 rc++;
3479 } while (buflen != 0);
3481 return rc;
3483 unwind:
3484 for(; rc > 0; rc--)
3485 __free_page(spages[rc-1]);
3486 return -ENOMEM;
3489 struct nfs4_cached_acl {
3490 int cached;
3491 size_t len;
3492 char data[0];
3495 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3497 struct nfs_inode *nfsi = NFS_I(inode);
3499 spin_lock(&inode->i_lock);
3500 kfree(nfsi->nfs4_acl);
3501 nfsi->nfs4_acl = acl;
3502 spin_unlock(&inode->i_lock);
3505 static void nfs4_zap_acl_attr(struct inode *inode)
3507 nfs4_set_cached_acl(inode, NULL);
3510 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3512 struct nfs_inode *nfsi = NFS_I(inode);
3513 struct nfs4_cached_acl *acl;
3514 int ret = -ENOENT;
3516 spin_lock(&inode->i_lock);
3517 acl = nfsi->nfs4_acl;
3518 if (acl == NULL)
3519 goto out;
3520 if (buf == NULL) /* user is just asking for length */
3521 goto out_len;
3522 if (acl->cached == 0)
3523 goto out;
3524 ret = -ERANGE; /* see getxattr(2) man page */
3525 if (acl->len > buflen)
3526 goto out;
3527 memcpy(buf, acl->data, acl->len);
3528 out_len:
3529 ret = acl->len;
3530 out:
3531 spin_unlock(&inode->i_lock);
3532 return ret;
3535 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3537 struct nfs4_cached_acl *acl;
3539 if (pages && acl_len <= PAGE_SIZE) {
3540 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3541 if (acl == NULL)
3542 goto out;
3543 acl->cached = 1;
3544 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3545 } else {
3546 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3547 if (acl == NULL)
3548 goto out;
3549 acl->cached = 0;
3551 acl->len = acl_len;
3552 out:
3553 nfs4_set_cached_acl(inode, acl);
3557 * The getxattr API returns the required buffer length when called with a
3558 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3559 * the required buf. On a NULL buf, we send a page of data to the server
3560 * guessing that the ACL request can be serviced by a page. If so, we cache
3561 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3562 * the cache. If not so, we throw away the page, and cache the required
3563 * length. The next getxattr call will then produce another round trip to
3564 * the server, this time with the input buf of the required size.
3566 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3568 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3569 struct nfs_getaclargs args = {
3570 .fh = NFS_FH(inode),
3571 .acl_pages = pages,
3572 .acl_len = buflen,
3574 struct nfs_getaclres res = {
3575 .acl_len = buflen,
3577 struct rpc_message msg = {
3578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3579 .rpc_argp = &args,
3580 .rpc_resp = &res,
3582 int ret = -ENOMEM, npages, i, acl_len = 0;
3584 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3585 /* As long as we're doing a round trip to the server anyway,
3586 * let's be prepared for a page of acl data. */
3587 if (npages == 0)
3588 npages = 1;
3590 for (i = 0; i < npages; i++) {
3591 pages[i] = alloc_page(GFP_KERNEL);
3592 if (!pages[i])
3593 goto out_free;
3595 if (npages > 1) {
3596 /* for decoding across pages */
3597 res.acl_scratch = alloc_page(GFP_KERNEL);
3598 if (!res.acl_scratch)
3599 goto out_free;
3601 args.acl_len = npages * PAGE_SIZE;
3602 args.acl_pgbase = 0;
3603 /* Let decode_getfacl know not to fail if the ACL data is larger than
3604 * the page we send as a guess */
3605 if (buf == NULL)
3606 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3608 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3609 __func__, buf, buflen, npages, args.acl_len);
3610 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3611 &msg, &args.seq_args, &res.seq_res, 0);
3612 if (ret)
3613 goto out_free;
3615 acl_len = res.acl_len - res.acl_data_offset;
3616 if (acl_len > args.acl_len)
3617 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3618 else
3619 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3620 acl_len);
3621 if (buf) {
3622 ret = -ERANGE;
3623 if (acl_len > buflen)
3624 goto out_free;
3625 _copy_from_pages(buf, pages, res.acl_data_offset,
3626 acl_len);
3628 ret = acl_len;
3629 out_free:
3630 for (i = 0; i < npages; i++)
3631 if (pages[i])
3632 __free_page(pages[i]);
3633 if (res.acl_scratch)
3634 __free_page(res.acl_scratch);
3635 return ret;
3638 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3640 struct nfs4_exception exception = { };
3641 ssize_t ret;
3642 do {
3643 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3644 if (ret >= 0)
3645 break;
3646 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3647 } while (exception.retry);
3648 return ret;
3651 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3653 struct nfs_server *server = NFS_SERVER(inode);
3654 int ret;
3656 if (!nfs4_server_supports_acls(server))
3657 return -EOPNOTSUPP;
3658 ret = nfs_revalidate_inode(server, inode);
3659 if (ret < 0)
3660 return ret;
3661 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3662 nfs_zap_acl_cache(inode);
3663 ret = nfs4_read_cached_acl(inode, buf, buflen);
3664 if (ret != -ENOENT)
3665 /* -ENOENT is returned if there is no ACL or if there is an ACL
3666 * but no cached acl data, just the acl length */
3667 return ret;
3668 return nfs4_get_acl_uncached(inode, buf, buflen);
3671 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3673 struct nfs_server *server = NFS_SERVER(inode);
3674 struct page *pages[NFS4ACL_MAXPAGES];
3675 struct nfs_setaclargs arg = {
3676 .fh = NFS_FH(inode),
3677 .acl_pages = pages,
3678 .acl_len = buflen,
3680 struct nfs_setaclres res;
3681 struct rpc_message msg = {
3682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3683 .rpc_argp = &arg,
3684 .rpc_resp = &res,
3686 int ret, i;
3688 if (!nfs4_server_supports_acls(server))
3689 return -EOPNOTSUPP;
3690 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3691 if (i < 0)
3692 return i;
3693 nfs_inode_return_delegation(inode);
3694 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3697 * Free each page after tx, so the only ref left is
3698 * held by the network stack
3700 for (; i > 0; i--)
3701 put_page(pages[i-1]);
3704 * Acl update can result in inode attribute update.
3705 * so mark the attribute cache invalid.
3707 spin_lock(&inode->i_lock);
3708 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3709 spin_unlock(&inode->i_lock);
3710 nfs_access_zap_cache(inode);
3711 nfs_zap_acl_cache(inode);
3712 return ret;
3715 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3717 struct nfs4_exception exception = { };
3718 int err;
3719 do {
3720 err = nfs4_handle_exception(NFS_SERVER(inode),
3721 __nfs4_proc_set_acl(inode, buf, buflen),
3722 &exception);
3723 } while (exception.retry);
3724 return err;
3727 static int
3728 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3730 struct nfs_client *clp = server->nfs_client;
3732 if (task->tk_status >= 0)
3733 return 0;
3734 switch(task->tk_status) {
3735 case -NFS4ERR_DELEG_REVOKED:
3736 case -NFS4ERR_ADMIN_REVOKED:
3737 case -NFS4ERR_BAD_STATEID:
3738 if (state != NULL)
3739 nfs_remove_bad_delegation(state->inode);
3740 case -NFS4ERR_OPENMODE:
3741 if (state == NULL)
3742 break;
3743 nfs4_schedule_stateid_recovery(server, state);
3744 goto wait_on_recovery;
3745 case -NFS4ERR_EXPIRED:
3746 if (state != NULL)
3747 nfs4_schedule_stateid_recovery(server, state);
3748 case -NFS4ERR_STALE_STATEID:
3749 case -NFS4ERR_STALE_CLIENTID:
3750 nfs4_schedule_lease_recovery(clp);
3751 goto wait_on_recovery;
3752 #if defined(CONFIG_NFS_V4_1)
3753 case -NFS4ERR_BADSESSION:
3754 case -NFS4ERR_BADSLOT:
3755 case -NFS4ERR_BAD_HIGH_SLOT:
3756 case -NFS4ERR_DEADSESSION:
3757 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3758 case -NFS4ERR_SEQ_FALSE_RETRY:
3759 case -NFS4ERR_SEQ_MISORDERED:
3760 dprintk("%s ERROR %d, Reset session\n", __func__,
3761 task->tk_status);
3762 nfs4_schedule_session_recovery(clp->cl_session);
3763 task->tk_status = 0;
3764 return -EAGAIN;
3765 #endif /* CONFIG_NFS_V4_1 */
3766 case -NFS4ERR_DELAY:
3767 nfs_inc_server_stats(server, NFSIOS_DELAY);
3768 case -NFS4ERR_GRACE:
3769 case -EKEYEXPIRED:
3770 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3771 task->tk_status = 0;
3772 return -EAGAIN;
3773 case -NFS4ERR_RETRY_UNCACHED_REP:
3774 case -NFS4ERR_OLD_STATEID:
3775 task->tk_status = 0;
3776 return -EAGAIN;
3778 task->tk_status = nfs4_map_errors(task->tk_status);
3779 return 0;
3780 wait_on_recovery:
3781 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3782 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3783 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3784 task->tk_status = 0;
3785 return -EAGAIN;
3788 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3789 unsigned short port, struct rpc_cred *cred,
3790 struct nfs4_setclientid_res *res)
3792 nfs4_verifier sc_verifier;
3793 struct nfs4_setclientid setclientid = {
3794 .sc_verifier = &sc_verifier,
3795 .sc_prog = program,
3796 .sc_cb_ident = clp->cl_cb_ident,
3798 struct rpc_message msg = {
3799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3800 .rpc_argp = &setclientid,
3801 .rpc_resp = res,
3802 .rpc_cred = cred,
3804 __be32 *p;
3805 int loop = 0;
3806 int status;
3808 p = (__be32*)sc_verifier.data;
3809 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3810 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3812 for(;;) {
3813 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3814 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3815 clp->cl_ipaddr,
3816 rpc_peeraddr2str(clp->cl_rpcclient,
3817 RPC_DISPLAY_ADDR),
3818 rpc_peeraddr2str(clp->cl_rpcclient,
3819 RPC_DISPLAY_PROTO),
3820 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3821 clp->cl_id_uniquifier);
3822 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3823 sizeof(setclientid.sc_netid),
3824 rpc_peeraddr2str(clp->cl_rpcclient,
3825 RPC_DISPLAY_NETID));
3826 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3827 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3828 clp->cl_ipaddr, port >> 8, port & 255);
3830 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3831 if (status != -NFS4ERR_CLID_INUSE)
3832 break;
3833 if (loop != 0) {
3834 ++clp->cl_id_uniquifier;
3835 break;
3837 ++loop;
3838 ssleep(clp->cl_lease_time / HZ + 1);
3840 return status;
3843 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3844 struct nfs4_setclientid_res *arg,
3845 struct rpc_cred *cred)
3847 struct nfs_fsinfo fsinfo;
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3850 .rpc_argp = arg,
3851 .rpc_resp = &fsinfo,
3852 .rpc_cred = cred,
3854 unsigned long now;
3855 int status;
3857 now = jiffies;
3858 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3859 if (status == 0) {
3860 spin_lock(&clp->cl_lock);
3861 clp->cl_lease_time = fsinfo.lease_time * HZ;
3862 clp->cl_last_renewal = now;
3863 spin_unlock(&clp->cl_lock);
3865 return status;
3868 struct nfs4_delegreturndata {
3869 struct nfs4_delegreturnargs args;
3870 struct nfs4_delegreturnres res;
3871 struct nfs_fh fh;
3872 nfs4_stateid stateid;
3873 unsigned long timestamp;
3874 struct nfs_fattr fattr;
3875 int rpc_status;
3878 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3880 struct nfs4_delegreturndata *data = calldata;
3882 if (!nfs4_sequence_done(task, &data->res.seq_res))
3883 return;
3885 switch (task->tk_status) {
3886 case -NFS4ERR_STALE_STATEID:
3887 case -NFS4ERR_EXPIRED:
3888 case 0:
3889 renew_lease(data->res.server, data->timestamp);
3890 break;
3891 default:
3892 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3893 -EAGAIN) {
3894 rpc_restart_call_prepare(task);
3895 return;
3898 data->rpc_status = task->tk_status;
3901 static void nfs4_delegreturn_release(void *calldata)
3903 kfree(calldata);
3906 #if defined(CONFIG_NFS_V4_1)
3907 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3909 struct nfs4_delegreturndata *d_data;
3911 d_data = (struct nfs4_delegreturndata *)data;
3913 if (nfs4_setup_sequence(d_data->res.server,
3914 &d_data->args.seq_args,
3915 &d_data->res.seq_res, 1, task))
3916 return;
3917 rpc_call_start(task);
3919 #endif /* CONFIG_NFS_V4_1 */
3921 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3922 #if defined(CONFIG_NFS_V4_1)
3923 .rpc_call_prepare = nfs4_delegreturn_prepare,
3924 #endif /* CONFIG_NFS_V4_1 */
3925 .rpc_call_done = nfs4_delegreturn_done,
3926 .rpc_release = nfs4_delegreturn_release,
3929 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3931 struct nfs4_delegreturndata *data;
3932 struct nfs_server *server = NFS_SERVER(inode);
3933 struct rpc_task *task;
3934 struct rpc_message msg = {
3935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3936 .rpc_cred = cred,
3938 struct rpc_task_setup task_setup_data = {
3939 .rpc_client = server->client,
3940 .rpc_message = &msg,
3941 .callback_ops = &nfs4_delegreturn_ops,
3942 .flags = RPC_TASK_ASYNC,
3944 int status = 0;
3946 data = kzalloc(sizeof(*data), GFP_NOFS);
3947 if (data == NULL)
3948 return -ENOMEM;
3949 data->args.fhandle = &data->fh;
3950 data->args.stateid = &data->stateid;
3951 data->args.bitmask = server->attr_bitmask;
3952 nfs_copy_fh(&data->fh, NFS_FH(inode));
3953 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3954 data->res.fattr = &data->fattr;
3955 data->res.server = server;
3956 nfs_fattr_init(data->res.fattr);
3957 data->timestamp = jiffies;
3958 data->rpc_status = 0;
3960 task_setup_data.callback_data = data;
3961 msg.rpc_argp = &data->args;
3962 msg.rpc_resp = &data->res;
3963 task = rpc_run_task(&task_setup_data);
3964 if (IS_ERR(task))
3965 return PTR_ERR(task);
3966 if (!issync)
3967 goto out;
3968 status = nfs4_wait_for_completion_rpc_task(task);
3969 if (status != 0)
3970 goto out;
3971 status = data->rpc_status;
3972 if (status != 0)
3973 goto out;
3974 nfs_refresh_inode(inode, &data->fattr);
3975 out:
3976 rpc_put_task(task);
3977 return status;
3980 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3982 struct nfs_server *server = NFS_SERVER(inode);
3983 struct nfs4_exception exception = { };
3984 int err;
3985 do {
3986 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3987 switch (err) {
3988 case -NFS4ERR_STALE_STATEID:
3989 case -NFS4ERR_EXPIRED:
3990 case 0:
3991 return 0;
3993 err = nfs4_handle_exception(server, err, &exception);
3994 } while (exception.retry);
3995 return err;
3998 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3999 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4002 * sleep, with exponential backoff, and retry the LOCK operation.
4004 static unsigned long
4005 nfs4_set_lock_task_retry(unsigned long timeout)
4007 freezable_schedule_timeout_killable(timeout);
4008 timeout <<= 1;
4009 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4010 return NFS4_LOCK_MAXTIMEOUT;
4011 return timeout;
4014 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4016 struct inode *inode = state->inode;
4017 struct nfs_server *server = NFS_SERVER(inode);
4018 struct nfs_client *clp = server->nfs_client;
4019 struct nfs_lockt_args arg = {
4020 .fh = NFS_FH(inode),
4021 .fl = request,
4023 struct nfs_lockt_res res = {
4024 .denied = request,
4026 struct rpc_message msg = {
4027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4028 .rpc_argp = &arg,
4029 .rpc_resp = &res,
4030 .rpc_cred = state->owner->so_cred,
4032 struct nfs4_lock_state *lsp;
4033 int status;
4035 arg.lock_owner.clientid = clp->cl_clientid;
4036 status = nfs4_set_lock_state(state, request);
4037 if (status != 0)
4038 goto out;
4039 lsp = request->fl_u.nfs4_fl.owner;
4040 arg.lock_owner.id = lsp->ls_id.id;
4041 arg.lock_owner.s_dev = server->s_dev;
4042 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4043 switch (status) {
4044 case 0:
4045 request->fl_type = F_UNLCK;
4046 break;
4047 case -NFS4ERR_DENIED:
4048 status = 0;
4050 request->fl_ops->fl_release_private(request);
4051 out:
4052 return status;
4055 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4057 struct nfs4_exception exception = { };
4058 int err;
4060 do {
4061 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4062 _nfs4_proc_getlk(state, cmd, request),
4063 &exception);
4064 } while (exception.retry);
4065 return err;
4068 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4070 int res = 0;
4071 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4072 case FL_POSIX:
4073 res = posix_lock_file_wait(file, fl);
4074 break;
4075 case FL_FLOCK:
4076 res = flock_lock_file_wait(file, fl);
4077 break;
4078 default:
4079 BUG();
4081 return res;
4084 struct nfs4_unlockdata {
4085 struct nfs_locku_args arg;
4086 struct nfs_locku_res res;
4087 struct nfs4_lock_state *lsp;
4088 struct nfs_open_context *ctx;
4089 struct file_lock fl;
4090 const struct nfs_server *server;
4091 unsigned long timestamp;
4094 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4095 struct nfs_open_context *ctx,
4096 struct nfs4_lock_state *lsp,
4097 struct nfs_seqid *seqid)
4099 struct nfs4_unlockdata *p;
4100 struct inode *inode = lsp->ls_state->inode;
4102 p = kzalloc(sizeof(*p), GFP_NOFS);
4103 if (p == NULL)
4104 return NULL;
4105 p->arg.fh = NFS_FH(inode);
4106 p->arg.fl = &p->fl;
4107 p->arg.seqid = seqid;
4108 p->res.seqid = seqid;
4109 p->arg.stateid = &lsp->ls_stateid;
4110 p->lsp = lsp;
4111 atomic_inc(&lsp->ls_count);
4112 /* Ensure we don't close file until we're done freeing locks! */
4113 p->ctx = get_nfs_open_context(ctx);
4114 memcpy(&p->fl, fl, sizeof(p->fl));
4115 p->server = NFS_SERVER(inode);
4116 return p;
4119 static void nfs4_locku_release_calldata(void *data)
4121 struct nfs4_unlockdata *calldata = data;
4122 nfs_free_seqid(calldata->arg.seqid);
4123 nfs4_put_lock_state(calldata->lsp);
4124 put_nfs_open_context(calldata->ctx);
4125 kfree(calldata);
4128 static void nfs4_locku_done(struct rpc_task *task, void *data)
4130 struct nfs4_unlockdata *calldata = data;
4132 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4133 return;
4134 switch (task->tk_status) {
4135 case 0:
4136 memcpy(calldata->lsp->ls_stateid.data,
4137 calldata->res.stateid.data,
4138 sizeof(calldata->lsp->ls_stateid.data));
4139 renew_lease(calldata->server, calldata->timestamp);
4140 break;
4141 case -NFS4ERR_BAD_STATEID:
4142 case -NFS4ERR_OLD_STATEID:
4143 case -NFS4ERR_STALE_STATEID:
4144 case -NFS4ERR_EXPIRED:
4145 break;
4146 default:
4147 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4148 rpc_restart_call_prepare(task);
4152 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4154 struct nfs4_unlockdata *calldata = data;
4156 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4157 return;
4158 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4159 /* Note: exit _without_ running nfs4_locku_done */
4160 task->tk_action = NULL;
4161 return;
4163 calldata->timestamp = jiffies;
4164 if (nfs4_setup_sequence(calldata->server,
4165 &calldata->arg.seq_args,
4166 &calldata->res.seq_res, 1, task))
4167 return;
4168 rpc_call_start(task);
4171 static const struct rpc_call_ops nfs4_locku_ops = {
4172 .rpc_call_prepare = nfs4_locku_prepare,
4173 .rpc_call_done = nfs4_locku_done,
4174 .rpc_release = nfs4_locku_release_calldata,
4177 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4178 struct nfs_open_context *ctx,
4179 struct nfs4_lock_state *lsp,
4180 struct nfs_seqid *seqid)
4182 struct nfs4_unlockdata *data;
4183 struct rpc_message msg = {
4184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4185 .rpc_cred = ctx->cred,
4187 struct rpc_task_setup task_setup_data = {
4188 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4189 .rpc_message = &msg,
4190 .callback_ops = &nfs4_locku_ops,
4191 .workqueue = nfsiod_workqueue,
4192 .flags = RPC_TASK_ASYNC,
4195 /* Ensure this is an unlock - when canceling a lock, the
4196 * canceled lock is passed in, and it won't be an unlock.
4198 fl->fl_type = F_UNLCK;
4200 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4201 if (data == NULL) {
4202 nfs_free_seqid(seqid);
4203 return ERR_PTR(-ENOMEM);
4206 msg.rpc_argp = &data->arg;
4207 msg.rpc_resp = &data->res;
4208 task_setup_data.callback_data = data;
4209 return rpc_run_task(&task_setup_data);
4212 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4214 struct nfs_inode *nfsi = NFS_I(state->inode);
4215 struct nfs_seqid *seqid;
4216 struct nfs4_lock_state *lsp;
4217 struct rpc_task *task;
4218 int status = 0;
4219 unsigned char fl_flags = request->fl_flags;
4221 status = nfs4_set_lock_state(state, request);
4222 /* Unlock _before_ we do the RPC call */
4223 request->fl_flags |= FL_EXISTS;
4224 down_read(&nfsi->rwsem);
4225 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4226 up_read(&nfsi->rwsem);
4227 goto out;
4229 up_read(&nfsi->rwsem);
4230 if (status != 0)
4231 goto out;
4232 /* Is this a delegated lock? */
4233 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4234 goto out;
4235 lsp = request->fl_u.nfs4_fl.owner;
4236 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4237 status = -ENOMEM;
4238 if (seqid == NULL)
4239 goto out;
4240 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4241 status = PTR_ERR(task);
4242 if (IS_ERR(task))
4243 goto out;
4244 status = nfs4_wait_for_completion_rpc_task(task);
4245 rpc_put_task(task);
4246 out:
4247 request->fl_flags = fl_flags;
4248 return status;
4251 struct nfs4_lockdata {
4252 struct nfs_lock_args arg;
4253 struct nfs_lock_res res;
4254 struct nfs4_lock_state *lsp;
4255 struct nfs_open_context *ctx;
4256 struct file_lock fl;
4257 unsigned long timestamp;
4258 int rpc_status;
4259 int cancelled;
4260 struct nfs_server *server;
4263 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4264 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4265 gfp_t gfp_mask)
4267 struct nfs4_lockdata *p;
4268 struct inode *inode = lsp->ls_state->inode;
4269 struct nfs_server *server = NFS_SERVER(inode);
4271 p = kzalloc(sizeof(*p), gfp_mask);
4272 if (p == NULL)
4273 return NULL;
4275 p->arg.fh = NFS_FH(inode);
4276 p->arg.fl = &p->fl;
4277 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4278 if (p->arg.open_seqid == NULL)
4279 goto out_free;
4280 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4281 if (p->arg.lock_seqid == NULL)
4282 goto out_free_seqid;
4283 p->arg.lock_stateid = &lsp->ls_stateid;
4284 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4285 p->arg.lock_owner.id = lsp->ls_id.id;
4286 p->arg.lock_owner.s_dev = server->s_dev;
4287 p->res.lock_seqid = p->arg.lock_seqid;
4288 p->lsp = lsp;
4289 p->server = server;
4290 atomic_inc(&lsp->ls_count);
4291 p->ctx = get_nfs_open_context(ctx);
4292 memcpy(&p->fl, fl, sizeof(p->fl));
4293 return p;
4294 out_free_seqid:
4295 nfs_free_seqid(p->arg.open_seqid);
4296 out_free:
4297 kfree(p);
4298 return NULL;
4301 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4303 struct nfs4_lockdata *data = calldata;
4304 struct nfs4_state *state = data->lsp->ls_state;
4306 dprintk("%s: begin!\n", __func__);
4307 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4308 return;
4309 /* Do we need to do an open_to_lock_owner? */
4310 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4311 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4312 return;
4313 data->arg.open_stateid = &state->stateid;
4314 data->arg.new_lock_owner = 1;
4315 data->res.open_seqid = data->arg.open_seqid;
4316 } else
4317 data->arg.new_lock_owner = 0;
4318 data->timestamp = jiffies;
4319 if (nfs4_setup_sequence(data->server,
4320 &data->arg.seq_args,
4321 &data->res.seq_res, 1, task))
4322 return;
4323 rpc_call_start(task);
4324 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4327 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4329 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4330 nfs4_lock_prepare(task, calldata);
4333 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4335 struct nfs4_lockdata *data = calldata;
4337 dprintk("%s: begin!\n", __func__);
4339 if (!nfs4_sequence_done(task, &data->res.seq_res))
4340 return;
4342 data->rpc_status = task->tk_status;
4343 if (data->arg.new_lock_owner != 0) {
4344 if (data->rpc_status == 0)
4345 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4346 else
4347 goto out;
4349 if (data->rpc_status == 0) {
4350 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4351 sizeof(data->lsp->ls_stateid.data));
4352 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4353 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4355 out:
4356 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4359 static void nfs4_lock_release(void *calldata)
4361 struct nfs4_lockdata *data = calldata;
4363 dprintk("%s: begin!\n", __func__);
4364 nfs_free_seqid(data->arg.open_seqid);
4365 if (data->cancelled != 0) {
4366 struct rpc_task *task;
4367 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4368 data->arg.lock_seqid);
4369 if (!IS_ERR(task))
4370 rpc_put_task_async(task);
4371 dprintk("%s: cancelling lock!\n", __func__);
4372 } else
4373 nfs_free_seqid(data->arg.lock_seqid);
4374 nfs4_put_lock_state(data->lsp);
4375 put_nfs_open_context(data->ctx);
4376 kfree(data);
4377 dprintk("%s: done!\n", __func__);
4380 static const struct rpc_call_ops nfs4_lock_ops = {
4381 .rpc_call_prepare = nfs4_lock_prepare,
4382 .rpc_call_done = nfs4_lock_done,
4383 .rpc_release = nfs4_lock_release,
4386 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4387 .rpc_call_prepare = nfs4_recover_lock_prepare,
4388 .rpc_call_done = nfs4_lock_done,
4389 .rpc_release = nfs4_lock_release,
4392 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4394 switch (error) {
4395 case -NFS4ERR_ADMIN_REVOKED:
4396 case -NFS4ERR_BAD_STATEID:
4397 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4398 if (new_lock_owner != 0 ||
4399 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4400 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4401 break;
4402 case -NFS4ERR_STALE_STATEID:
4403 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4404 case -NFS4ERR_EXPIRED:
4405 nfs4_schedule_lease_recovery(server->nfs_client);
4409 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4411 struct nfs4_lockdata *data;
4412 struct rpc_task *task;
4413 struct rpc_message msg = {
4414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4415 .rpc_cred = state->owner->so_cred,
4417 struct rpc_task_setup task_setup_data = {
4418 .rpc_client = NFS_CLIENT(state->inode),
4419 .rpc_message = &msg,
4420 .callback_ops = &nfs4_lock_ops,
4421 .workqueue = nfsiod_workqueue,
4422 .flags = RPC_TASK_ASYNC,
4424 int ret;
4426 dprintk("%s: begin!\n", __func__);
4427 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4428 fl->fl_u.nfs4_fl.owner,
4429 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4430 if (data == NULL)
4431 return -ENOMEM;
4432 if (IS_SETLKW(cmd))
4433 data->arg.block = 1;
4434 if (recovery_type > NFS_LOCK_NEW) {
4435 if (recovery_type == NFS_LOCK_RECLAIM)
4436 data->arg.reclaim = NFS_LOCK_RECLAIM;
4437 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4439 msg.rpc_argp = &data->arg;
4440 msg.rpc_resp = &data->res;
4441 task_setup_data.callback_data = data;
4442 task = rpc_run_task(&task_setup_data);
4443 if (IS_ERR(task))
4444 return PTR_ERR(task);
4445 ret = nfs4_wait_for_completion_rpc_task(task);
4446 if (ret == 0) {
4447 ret = data->rpc_status;
4448 if (ret)
4449 nfs4_handle_setlk_error(data->server, data->lsp,
4450 data->arg.new_lock_owner, ret);
4451 } else
4452 data->cancelled = 1;
4453 rpc_put_task(task);
4454 dprintk("%s: done, ret = %d!\n", __func__, ret);
4455 return ret;
4458 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4460 struct nfs_server *server = NFS_SERVER(state->inode);
4461 struct nfs4_exception exception = {
4462 .inode = state->inode,
4464 int err;
4466 do {
4467 /* Cache the lock if possible... */
4468 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4469 return 0;
4470 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4471 if (err != -NFS4ERR_DELAY)
4472 break;
4473 nfs4_handle_exception(server, err, &exception);
4474 } while (exception.retry);
4475 return err;
4478 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4480 struct nfs_server *server = NFS_SERVER(state->inode);
4481 struct nfs4_exception exception = {
4482 .inode = state->inode,
4484 int err;
4486 err = nfs4_set_lock_state(state, request);
4487 if (err != 0)
4488 return err;
4489 do {
4490 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4491 return 0;
4492 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4493 switch (err) {
4494 default:
4495 goto out;
4496 case -NFS4ERR_GRACE:
4497 case -NFS4ERR_DELAY:
4498 nfs4_handle_exception(server, err, &exception);
4499 err = 0;
4501 } while (exception.retry);
4502 out:
4503 return err;
4506 #if defined(CONFIG_NFS_V4_1)
4507 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4509 int status;
4510 struct nfs_server *server = NFS_SERVER(state->inode);
4512 status = nfs41_test_stateid(server, state);
4513 if (status == NFS_OK)
4514 return 0;
4515 nfs41_free_stateid(server, state);
4516 return nfs4_lock_expired(state, request);
4518 #endif
4520 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4522 struct nfs_inode *nfsi = NFS_I(state->inode);
4523 unsigned char fl_flags = request->fl_flags;
4524 int status = -ENOLCK;
4526 if ((fl_flags & FL_POSIX) &&
4527 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4528 goto out;
4529 /* Is this a delegated open? */
4530 status = nfs4_set_lock_state(state, request);
4531 if (status != 0)
4532 goto out;
4533 request->fl_flags |= FL_ACCESS;
4534 status = do_vfs_lock(request->fl_file, request);
4535 if (status < 0)
4536 goto out;
4537 down_read(&nfsi->rwsem);
4538 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4539 /* Yes: cache locks! */
4540 /* ...but avoid races with delegation recall... */
4541 request->fl_flags = fl_flags & ~FL_SLEEP;
4542 status = do_vfs_lock(request->fl_file, request);
4543 goto out_unlock;
4545 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4546 if (status != 0)
4547 goto out_unlock;
4548 /* Note: we always want to sleep here! */
4549 request->fl_flags = fl_flags | FL_SLEEP;
4550 if (do_vfs_lock(request->fl_file, request) < 0)
4551 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4552 out_unlock:
4553 up_read(&nfsi->rwsem);
4554 out:
4555 request->fl_flags = fl_flags;
4556 return status;
4559 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4561 struct nfs4_exception exception = {
4562 .state = state,
4563 .inode = state->inode,
4565 int err;
4567 do {
4568 err = _nfs4_proc_setlk(state, cmd, request);
4569 if (err == -NFS4ERR_DENIED)
4570 err = -EAGAIN;
4571 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4572 err, &exception);
4573 } while (exception.retry);
4574 return err;
4577 static int
4578 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4580 struct nfs_open_context *ctx;
4581 struct nfs4_state *state;
4582 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4583 int status;
4585 /* verify open state */
4586 ctx = nfs_file_open_context(filp);
4587 state = ctx->state;
4589 if (request->fl_start < 0 || request->fl_end < 0)
4590 return -EINVAL;
4592 if (IS_GETLK(cmd)) {
4593 if (state != NULL)
4594 return nfs4_proc_getlk(state, F_GETLK, request);
4595 return 0;
4598 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4599 return -EINVAL;
4601 if (request->fl_type == F_UNLCK) {
4602 if (state != NULL)
4603 return nfs4_proc_unlck(state, cmd, request);
4604 return 0;
4607 if (state == NULL)
4608 return -ENOLCK;
4610 * Don't rely on the VFS having checked the file open mode,
4611 * since it won't do this for flock() locks.
4613 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4614 case F_RDLCK:
4615 if (!(filp->f_mode & FMODE_READ))
4616 return -EBADF;
4617 break;
4618 case F_WRLCK:
4619 if (!(filp->f_mode & FMODE_WRITE))
4620 return -EBADF;
4623 do {
4624 status = nfs4_proc_setlk(state, cmd, request);
4625 if ((status != -EAGAIN) || IS_SETLK(cmd))
4626 break;
4627 timeout = nfs4_set_lock_task_retry(timeout);
4628 status = -ERESTARTSYS;
4629 if (signalled())
4630 break;
4631 } while(status < 0);
4632 return status;
4635 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4637 struct nfs_server *server = NFS_SERVER(state->inode);
4638 struct nfs4_exception exception = { };
4639 int err;
4641 err = nfs4_set_lock_state(state, fl);
4642 if (err != 0)
4643 goto out;
4644 do {
4645 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4646 switch (err) {
4647 default:
4648 printk(KERN_ERR "%s: unhandled error %d.\n",
4649 __func__, err);
4650 case 0:
4651 case -ESTALE:
4652 goto out;
4653 case -NFS4ERR_EXPIRED:
4654 nfs4_schedule_stateid_recovery(server, state);
4655 case -NFS4ERR_STALE_CLIENTID:
4656 case -NFS4ERR_STALE_STATEID:
4657 nfs4_schedule_lease_recovery(server->nfs_client);
4658 goto out;
4659 case -NFS4ERR_BADSESSION:
4660 case -NFS4ERR_BADSLOT:
4661 case -NFS4ERR_BAD_HIGH_SLOT:
4662 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4663 case -NFS4ERR_DEADSESSION:
4664 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4665 goto out;
4666 case -ERESTARTSYS:
4668 * The show must go on: exit, but mark the
4669 * stateid as needing recovery.
4671 case -NFS4ERR_DELEG_REVOKED:
4672 case -NFS4ERR_ADMIN_REVOKED:
4673 case -NFS4ERR_BAD_STATEID:
4674 case -NFS4ERR_OPENMODE:
4675 nfs4_schedule_stateid_recovery(server, state);
4676 err = 0;
4677 goto out;
4678 case -EKEYEXPIRED:
4680 * User RPCSEC_GSS context has expired.
4681 * We cannot recover this stateid now, so
4682 * skip it and allow recovery thread to
4683 * proceed.
4685 err = 0;
4686 goto out;
4687 case -ENOMEM:
4688 case -NFS4ERR_DENIED:
4689 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4690 err = 0;
4691 goto out;
4692 case -NFS4ERR_DELAY:
4693 break;
4695 err = nfs4_handle_exception(server, err, &exception);
4696 } while (exception.retry);
4697 out:
4698 return err;
4701 static void nfs4_release_lockowner_release(void *calldata)
4703 kfree(calldata);
4706 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4707 .rpc_release = nfs4_release_lockowner_release,
4710 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4712 struct nfs_server *server = lsp->ls_state->owner->so_server;
4713 struct nfs_release_lockowner_args *args;
4714 struct rpc_message msg = {
4715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4718 if (server->nfs_client->cl_mvops->minor_version != 0)
4719 return;
4720 args = kmalloc(sizeof(*args), GFP_NOFS);
4721 if (!args)
4722 return;
4723 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4724 args->lock_owner.id = lsp->ls_id.id;
4725 args->lock_owner.s_dev = server->s_dev;
4726 msg.rpc_argp = args;
4727 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4730 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4732 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4733 const void *buf, size_t buflen,
4734 int flags, int type)
4736 if (strcmp(key, "") != 0)
4737 return -EINVAL;
4739 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4742 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4743 void *buf, size_t buflen, int type)
4745 if (strcmp(key, "") != 0)
4746 return -EINVAL;
4748 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4751 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4752 size_t list_len, const char *name,
4753 size_t name_len, int type)
4755 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4757 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4758 return 0;
4760 if (list && len <= list_len)
4761 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4762 return len;
4766 * nfs_fhget will use either the mounted_on_fileid or the fileid
4768 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4770 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4771 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4772 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4773 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4774 return;
4776 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4777 NFS_ATTR_FATTR_NLINK;
4778 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4779 fattr->nlink = 2;
4782 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4783 struct nfs4_fs_locations *fs_locations, struct page *page)
4785 struct nfs_server *server = NFS_SERVER(dir);
4786 u32 bitmask[2] = {
4787 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4789 struct nfs4_fs_locations_arg args = {
4790 .dir_fh = NFS_FH(dir),
4791 .name = name,
4792 .page = page,
4793 .bitmask = bitmask,
4795 struct nfs4_fs_locations_res res = {
4796 .fs_locations = fs_locations,
4798 struct rpc_message msg = {
4799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4800 .rpc_argp = &args,
4801 .rpc_resp = &res,
4803 int status;
4805 dprintk("%s: start\n", __func__);
4807 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4808 * is not supported */
4809 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4810 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4811 else
4812 bitmask[0] |= FATTR4_WORD0_FILEID;
4814 nfs_fattr_init(&fs_locations->fattr);
4815 fs_locations->server = server;
4816 fs_locations->nlocations = 0;
4817 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4818 dprintk("%s: returned status = %d\n", __func__, status);
4819 return status;
4822 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4824 int status;
4825 struct nfs4_secinfo_arg args = {
4826 .dir_fh = NFS_FH(dir),
4827 .name = name,
4829 struct nfs4_secinfo_res res = {
4830 .flavors = flavors,
4832 struct rpc_message msg = {
4833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4834 .rpc_argp = &args,
4835 .rpc_resp = &res,
4838 dprintk("NFS call secinfo %s\n", name->name);
4839 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4840 dprintk("NFS reply secinfo: %d\n", status);
4841 return status;
4844 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4846 struct nfs4_exception exception = { };
4847 int err;
4848 do {
4849 err = nfs4_handle_exception(NFS_SERVER(dir),
4850 _nfs4_proc_secinfo(dir, name, flavors),
4851 &exception);
4852 } while (exception.retry);
4853 return err;
4856 #ifdef CONFIG_NFS_V4_1
4858 * Check the exchange flags returned by the server for invalid flags, having
4859 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4860 * DS flags set.
4862 static int nfs4_check_cl_exchange_flags(u32 flags)
4864 if (flags & ~EXCHGID4_FLAG_MASK_R)
4865 goto out_inval;
4866 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4867 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4868 goto out_inval;
4869 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4870 goto out_inval;
4871 return NFS_OK;
4872 out_inval:
4873 return -NFS4ERR_INVAL;
4876 static bool
4877 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4879 if (a->server_scope_sz == b->server_scope_sz &&
4880 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4881 return true;
4883 return false;
4887 * nfs4_proc_exchange_id()
4889 * Since the clientid has expired, all compounds using sessions
4890 * associated with the stale clientid will be returning
4891 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4892 * be in some phase of session reset.
4894 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4896 nfs4_verifier verifier;
4897 struct nfs41_exchange_id_args args = {
4898 .client = clp,
4899 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4901 struct nfs41_exchange_id_res res = {
4902 .client = clp,
4904 int status;
4905 struct rpc_message msg = {
4906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4907 .rpc_argp = &args,
4908 .rpc_resp = &res,
4909 .rpc_cred = cred,
4911 __be32 *p;
4913 dprintk("--> %s\n", __func__);
4914 BUG_ON(clp == NULL);
4916 p = (u32 *)verifier.data;
4917 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4918 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4919 args.verifier = &verifier;
4921 args.id_len = scnprintf(args.id, sizeof(args.id),
4922 "%s/%s.%s/%u",
4923 clp->cl_ipaddr,
4924 init_utsname()->nodename,
4925 init_utsname()->domainname,
4926 clp->cl_rpcclient->cl_auth->au_flavor);
4928 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4929 if (unlikely(!res.server_scope)) {
4930 status = -ENOMEM;
4931 goto out;
4934 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4935 if (!status)
4936 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4938 if (!status) {
4939 if (clp->server_scope &&
4940 !nfs41_same_server_scope(clp->server_scope,
4941 res.server_scope)) {
4942 dprintk("%s: server_scope mismatch detected\n",
4943 __func__);
4944 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4945 kfree(clp->server_scope);
4946 clp->server_scope = NULL;
4949 if (!clp->server_scope) {
4950 clp->server_scope = res.server_scope;
4951 goto out;
4954 kfree(res.server_scope);
4955 out:
4956 dprintk("<-- %s status= %d\n", __func__, status);
4957 return status;
4960 struct nfs4_get_lease_time_data {
4961 struct nfs4_get_lease_time_args *args;
4962 struct nfs4_get_lease_time_res *res;
4963 struct nfs_client *clp;
4966 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4967 void *calldata)
4969 int ret;
4970 struct nfs4_get_lease_time_data *data =
4971 (struct nfs4_get_lease_time_data *)calldata;
4973 dprintk("--> %s\n", __func__);
4974 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4975 /* just setup sequence, do not trigger session recovery
4976 since we're invoked within one */
4977 ret = nfs41_setup_sequence(data->clp->cl_session,
4978 &data->args->la_seq_args,
4979 &data->res->lr_seq_res, 0, task);
4981 BUG_ON(ret == -EAGAIN);
4982 rpc_call_start(task);
4983 dprintk("<-- %s\n", __func__);
4987 * Called from nfs4_state_manager thread for session setup, so don't recover
4988 * from sequence operation or clientid errors.
4990 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4992 struct nfs4_get_lease_time_data *data =
4993 (struct nfs4_get_lease_time_data *)calldata;
4995 dprintk("--> %s\n", __func__);
4996 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4997 return;
4998 switch (task->tk_status) {
4999 case -NFS4ERR_DELAY:
5000 case -NFS4ERR_GRACE:
5001 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5002 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5003 task->tk_status = 0;
5004 /* fall through */
5005 case -NFS4ERR_RETRY_UNCACHED_REP:
5006 rpc_restart_call_prepare(task);
5007 return;
5009 dprintk("<-- %s\n", __func__);
5012 struct rpc_call_ops nfs4_get_lease_time_ops = {
5013 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5014 .rpc_call_done = nfs4_get_lease_time_done,
5017 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5019 struct rpc_task *task;
5020 struct nfs4_get_lease_time_args args;
5021 struct nfs4_get_lease_time_res res = {
5022 .lr_fsinfo = fsinfo,
5024 struct nfs4_get_lease_time_data data = {
5025 .args = &args,
5026 .res = &res,
5027 .clp = clp,
5029 struct rpc_message msg = {
5030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5031 .rpc_argp = &args,
5032 .rpc_resp = &res,
5034 struct rpc_task_setup task_setup = {
5035 .rpc_client = clp->cl_rpcclient,
5036 .rpc_message = &msg,
5037 .callback_ops = &nfs4_get_lease_time_ops,
5038 .callback_data = &data,
5039 .flags = RPC_TASK_TIMEOUT,
5041 int status;
5043 dprintk("--> %s\n", __func__);
5044 task = rpc_run_task(&task_setup);
5046 if (IS_ERR(task))
5047 status = PTR_ERR(task);
5048 else {
5049 status = task->tk_status;
5050 rpc_put_task(task);
5052 dprintk("<-- %s return %d\n", __func__, status);
5054 return status;
5057 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5059 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5062 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5063 struct nfs4_slot *new,
5064 u32 max_slots,
5065 u32 ivalue)
5067 struct nfs4_slot *old = NULL;
5068 u32 i;
5070 spin_lock(&tbl->slot_tbl_lock);
5071 if (new) {
5072 old = tbl->slots;
5073 tbl->slots = new;
5074 tbl->max_slots = max_slots;
5076 tbl->highest_used_slotid = -1; /* no slot is currently used */
5077 for (i = 0; i < tbl->max_slots; i++)
5078 tbl->slots[i].seq_nr = ivalue;
5079 spin_unlock(&tbl->slot_tbl_lock);
5080 kfree(old);
5084 * (re)Initialise a slot table
5086 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5087 u32 ivalue)
5089 struct nfs4_slot *new = NULL;
5090 int ret = -ENOMEM;
5092 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5093 max_reqs, tbl->max_slots);
5095 /* Does the newly negotiated max_reqs match the existing slot table? */
5096 if (max_reqs != tbl->max_slots) {
5097 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5098 if (!new)
5099 goto out;
5101 ret = 0;
5103 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5104 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5105 tbl, tbl->slots, tbl->max_slots);
5106 out:
5107 dprintk("<-- %s: return %d\n", __func__, ret);
5108 return ret;
5111 /* Destroy the slot table */
5112 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5114 if (session->fc_slot_table.slots != NULL) {
5115 kfree(session->fc_slot_table.slots);
5116 session->fc_slot_table.slots = NULL;
5118 if (session->bc_slot_table.slots != NULL) {
5119 kfree(session->bc_slot_table.slots);
5120 session->bc_slot_table.slots = NULL;
5122 return;
5126 * Initialize or reset the forechannel and backchannel tables
5128 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5130 struct nfs4_slot_table *tbl;
5131 int status;
5133 dprintk("--> %s\n", __func__);
5134 /* Fore channel */
5135 tbl = &ses->fc_slot_table;
5136 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5137 if (status) /* -ENOMEM */
5138 return status;
5139 /* Back channel */
5140 tbl = &ses->bc_slot_table;
5141 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5142 if (status && tbl->slots == NULL)
5143 /* Fore and back channel share a connection so get
5144 * both slot tables or neither */
5145 nfs4_destroy_slot_tables(ses);
5146 return status;
5149 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5151 struct nfs4_session *session;
5152 struct nfs4_slot_table *tbl;
5154 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5155 if (!session)
5156 return NULL;
5158 tbl = &session->fc_slot_table;
5159 tbl->highest_used_slotid = -1;
5160 spin_lock_init(&tbl->slot_tbl_lock);
5161 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5162 init_completion(&tbl->complete);
5164 tbl = &session->bc_slot_table;
5165 tbl->highest_used_slotid = -1;
5166 spin_lock_init(&tbl->slot_tbl_lock);
5167 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5168 init_completion(&tbl->complete);
5170 session->session_state = 1<<NFS4_SESSION_INITING;
5172 session->clp = clp;
5173 return session;
5176 void nfs4_destroy_session(struct nfs4_session *session)
5178 nfs4_proc_destroy_session(session);
5179 dprintk("%s Destroy backchannel for xprt %p\n",
5180 __func__, session->clp->cl_rpcclient->cl_xprt);
5181 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5182 NFS41_BC_MIN_CALLBACKS);
5183 nfs4_destroy_slot_tables(session);
5184 kfree(session);
5188 * Initialize the values to be used by the client in CREATE_SESSION
5189 * If nfs4_init_session set the fore channel request and response sizes,
5190 * use them.
5192 * Set the back channel max_resp_sz_cached to zero to force the client to
5193 * always set csa_cachethis to FALSE because the current implementation
5194 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5196 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5198 struct nfs4_session *session = args->client->cl_session;
5199 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5200 mxresp_sz = session->fc_attrs.max_resp_sz;
5202 if (mxrqst_sz == 0)
5203 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5204 if (mxresp_sz == 0)
5205 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5206 /* Fore channel attributes */
5207 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5208 args->fc_attrs.max_resp_sz = mxresp_sz;
5209 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5210 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5212 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5213 "max_ops=%u max_reqs=%u\n",
5214 __func__,
5215 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5216 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5218 /* Back channel attributes */
5219 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5220 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5221 args->bc_attrs.max_resp_sz_cached = 0;
5222 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5223 args->bc_attrs.max_reqs = 1;
5225 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5226 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5227 __func__,
5228 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5229 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5230 args->bc_attrs.max_reqs);
5233 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5235 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5236 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5238 if (rcvd->max_resp_sz > sent->max_resp_sz)
5239 return -EINVAL;
5241 * Our requested max_ops is the minimum we need; we're not
5242 * prepared to break up compounds into smaller pieces than that.
5243 * So, no point even trying to continue if the server won't
5244 * cooperate:
5246 if (rcvd->max_ops < sent->max_ops)
5247 return -EINVAL;
5248 if (rcvd->max_reqs == 0)
5249 return -EINVAL;
5250 return 0;
5253 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5255 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5256 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5258 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5259 return -EINVAL;
5260 if (rcvd->max_resp_sz < sent->max_resp_sz)
5261 return -EINVAL;
5262 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5263 return -EINVAL;
5264 /* These would render the backchannel useless: */
5265 if (rcvd->max_ops == 0)
5266 return -EINVAL;
5267 if (rcvd->max_reqs == 0)
5268 return -EINVAL;
5269 return 0;
5272 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5273 struct nfs4_session *session)
5275 int ret;
5277 ret = nfs4_verify_fore_channel_attrs(args, session);
5278 if (ret)
5279 return ret;
5280 return nfs4_verify_back_channel_attrs(args, session);
5283 static int _nfs4_proc_create_session(struct nfs_client *clp)
5285 struct nfs4_session *session = clp->cl_session;
5286 struct nfs41_create_session_args args = {
5287 .client = clp,
5288 .cb_program = NFS4_CALLBACK,
5290 struct nfs41_create_session_res res = {
5291 .client = clp,
5293 struct rpc_message msg = {
5294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5295 .rpc_argp = &args,
5296 .rpc_resp = &res,
5298 int status;
5300 nfs4_init_channel_attrs(&args);
5301 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5303 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5305 if (!status)
5306 /* Verify the session's negotiated channel_attrs values */
5307 status = nfs4_verify_channel_attrs(&args, session);
5308 if (!status) {
5309 /* Increment the clientid slot sequence id */
5310 clp->cl_seqid++;
5313 return status;
5317 * Issues a CREATE_SESSION operation to the server.
5318 * It is the responsibility of the caller to verify the session is
5319 * expired before calling this routine.
5321 int nfs4_proc_create_session(struct nfs_client *clp)
5323 int status;
5324 unsigned *ptr;
5325 struct nfs4_session *session = clp->cl_session;
5327 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5329 status = _nfs4_proc_create_session(clp);
5330 if (status)
5331 goto out;
5333 /* Init or reset the session slot tables */
5334 status = nfs4_setup_session_slot_tables(session);
5335 dprintk("slot table setup returned %d\n", status);
5336 if (status)
5337 goto out;
5339 ptr = (unsigned *)&session->sess_id.data[0];
5340 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5341 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5342 out:
5343 dprintk("<-- %s\n", __func__);
5344 return status;
5348 * Issue the over-the-wire RPC DESTROY_SESSION.
5349 * The caller must serialize access to this routine.
5351 int nfs4_proc_destroy_session(struct nfs4_session *session)
5353 int status = 0;
5354 struct rpc_message msg;
5356 dprintk("--> nfs4_proc_destroy_session\n");
5358 /* session is still being setup */
5359 if (session->clp->cl_cons_state != NFS_CS_READY)
5360 return status;
5362 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5363 msg.rpc_argp = session;
5364 msg.rpc_resp = NULL;
5365 msg.rpc_cred = NULL;
5366 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5368 if (status)
5369 printk(KERN_WARNING
5370 "Got error %d from the server on DESTROY_SESSION. "
5371 "Session has been destroyed regardless...\n", status);
5373 dprintk("<-- nfs4_proc_destroy_session\n");
5374 return status;
5377 int nfs4_init_session(struct nfs_server *server)
5379 struct nfs_client *clp = server->nfs_client;
5380 struct nfs4_session *session;
5381 unsigned int rsize, wsize;
5382 int ret;
5384 if (!nfs4_has_session(clp))
5385 return 0;
5387 session = clp->cl_session;
5388 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5389 return 0;
5391 rsize = server->rsize;
5392 if (rsize == 0)
5393 rsize = NFS_MAX_FILE_IO_SIZE;
5394 wsize = server->wsize;
5395 if (wsize == 0)
5396 wsize = NFS_MAX_FILE_IO_SIZE;
5398 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5399 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5401 ret = nfs4_recover_expired_lease(server);
5402 if (!ret)
5403 ret = nfs4_check_client_ready(clp);
5404 return ret;
5407 int nfs4_init_ds_session(struct nfs_client *clp)
5409 struct nfs4_session *session = clp->cl_session;
5410 int ret;
5412 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5413 return 0;
5415 ret = nfs4_client_recover_expired_lease(clp);
5416 if (!ret)
5417 /* Test for the DS role */
5418 if (!is_ds_client(clp))
5419 ret = -ENODEV;
5420 if (!ret)
5421 ret = nfs4_check_client_ready(clp);
5422 return ret;
5425 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5429 * Renew the cl_session lease.
5431 struct nfs4_sequence_data {
5432 struct nfs_client *clp;
5433 struct nfs4_sequence_args args;
5434 struct nfs4_sequence_res res;
5437 static void nfs41_sequence_release(void *data)
5439 struct nfs4_sequence_data *calldata = data;
5440 struct nfs_client *clp = calldata->clp;
5442 if (atomic_read(&clp->cl_count) > 1)
5443 nfs4_schedule_state_renewal(clp);
5444 nfs_put_client(clp);
5445 kfree(calldata);
5448 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5450 switch(task->tk_status) {
5451 case -NFS4ERR_DELAY:
5452 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5453 return -EAGAIN;
5454 default:
5455 nfs4_schedule_lease_recovery(clp);
5457 return 0;
5460 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5462 struct nfs4_sequence_data *calldata = data;
5463 struct nfs_client *clp = calldata->clp;
5465 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5466 return;
5468 if (task->tk_status < 0) {
5469 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5470 if (atomic_read(&clp->cl_count) == 1)
5471 goto out;
5473 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5474 rpc_restart_call_prepare(task);
5475 return;
5478 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5479 out:
5480 dprintk("<-- %s\n", __func__);
5483 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5485 struct nfs4_sequence_data *calldata = data;
5486 struct nfs_client *clp = calldata->clp;
5487 struct nfs4_sequence_args *args;
5488 struct nfs4_sequence_res *res;
5490 args = task->tk_msg.rpc_argp;
5491 res = task->tk_msg.rpc_resp;
5493 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5494 return;
5495 rpc_call_start(task);
5498 static const struct rpc_call_ops nfs41_sequence_ops = {
5499 .rpc_call_done = nfs41_sequence_call_done,
5500 .rpc_call_prepare = nfs41_sequence_prepare,
5501 .rpc_release = nfs41_sequence_release,
5504 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5506 struct nfs4_sequence_data *calldata;
5507 struct rpc_message msg = {
5508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5509 .rpc_cred = cred,
5511 struct rpc_task_setup task_setup_data = {
5512 .rpc_client = clp->cl_rpcclient,
5513 .rpc_message = &msg,
5514 .callback_ops = &nfs41_sequence_ops,
5515 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5518 if (!atomic_inc_not_zero(&clp->cl_count))
5519 return ERR_PTR(-EIO);
5520 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5521 if (calldata == NULL) {
5522 nfs_put_client(clp);
5523 return ERR_PTR(-ENOMEM);
5525 msg.rpc_argp = &calldata->args;
5526 msg.rpc_resp = &calldata->res;
5527 calldata->clp = clp;
5528 task_setup_data.callback_data = calldata;
5530 return rpc_run_task(&task_setup_data);
5533 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5535 struct rpc_task *task;
5536 int ret = 0;
5538 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5539 return 0;
5540 task = _nfs41_proc_sequence(clp, cred);
5541 if (IS_ERR(task))
5542 ret = PTR_ERR(task);
5543 else
5544 rpc_put_task_async(task);
5545 dprintk("<-- %s status=%d\n", __func__, ret);
5546 return ret;
5549 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5551 struct rpc_task *task;
5552 int ret;
5554 task = _nfs41_proc_sequence(clp, cred);
5555 if (IS_ERR(task)) {
5556 ret = PTR_ERR(task);
5557 goto out;
5559 ret = rpc_wait_for_completion_task(task);
5560 if (!ret) {
5561 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5563 if (task->tk_status == 0)
5564 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5565 ret = task->tk_status;
5567 rpc_put_task(task);
5568 out:
5569 dprintk("<-- %s status=%d\n", __func__, ret);
5570 return ret;
5573 struct nfs4_reclaim_complete_data {
5574 struct nfs_client *clp;
5575 struct nfs41_reclaim_complete_args arg;
5576 struct nfs41_reclaim_complete_res res;
5579 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5581 struct nfs4_reclaim_complete_data *calldata = data;
5583 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5584 if (nfs41_setup_sequence(calldata->clp->cl_session,
5585 &calldata->arg.seq_args,
5586 &calldata->res.seq_res, 0, task))
5587 return;
5589 rpc_call_start(task);
5592 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5594 switch(task->tk_status) {
5595 case 0:
5596 case -NFS4ERR_COMPLETE_ALREADY:
5597 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5598 break;
5599 case -NFS4ERR_DELAY:
5600 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5601 /* fall through */
5602 case -NFS4ERR_RETRY_UNCACHED_REP:
5603 return -EAGAIN;
5604 default:
5605 nfs4_schedule_lease_recovery(clp);
5607 return 0;
5610 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5612 struct nfs4_reclaim_complete_data *calldata = data;
5613 struct nfs_client *clp = calldata->clp;
5614 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5616 dprintk("--> %s\n", __func__);
5617 if (!nfs41_sequence_done(task, res))
5618 return;
5620 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5621 rpc_restart_call_prepare(task);
5622 return;
5624 dprintk("<-- %s\n", __func__);
5627 static void nfs4_free_reclaim_complete_data(void *data)
5629 struct nfs4_reclaim_complete_data *calldata = data;
5631 kfree(calldata);
5634 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5635 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5636 .rpc_call_done = nfs4_reclaim_complete_done,
5637 .rpc_release = nfs4_free_reclaim_complete_data,
5641 * Issue a global reclaim complete.
5643 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5645 struct nfs4_reclaim_complete_data *calldata;
5646 struct rpc_task *task;
5647 struct rpc_message msg = {
5648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5650 struct rpc_task_setup task_setup_data = {
5651 .rpc_client = clp->cl_rpcclient,
5652 .rpc_message = &msg,
5653 .callback_ops = &nfs4_reclaim_complete_call_ops,
5654 .flags = RPC_TASK_ASYNC,
5656 int status = -ENOMEM;
5658 dprintk("--> %s\n", __func__);
5659 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5660 if (calldata == NULL)
5661 goto out;
5662 calldata->clp = clp;
5663 calldata->arg.one_fs = 0;
5665 msg.rpc_argp = &calldata->arg;
5666 msg.rpc_resp = &calldata->res;
5667 task_setup_data.callback_data = calldata;
5668 task = rpc_run_task(&task_setup_data);
5669 if (IS_ERR(task)) {
5670 status = PTR_ERR(task);
5671 goto out;
5673 status = nfs4_wait_for_completion_rpc_task(task);
5674 if (status == 0)
5675 status = task->tk_status;
5676 rpc_put_task(task);
5677 return 0;
5678 out:
5679 dprintk("<-- %s status=%d\n", __func__, status);
5680 return status;
5683 static void
5684 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5686 struct nfs4_layoutget *lgp = calldata;
5687 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5689 dprintk("--> %s\n", __func__);
5690 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5691 * right now covering the LAYOUTGET we are about to send.
5692 * However, that is not so catastrophic, and there seems
5693 * to be no way to prevent it completely.
5695 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5696 &lgp->res.seq_res, 0, task))
5697 return;
5698 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5699 NFS_I(lgp->args.inode)->layout,
5700 lgp->args.ctx->state)) {
5701 rpc_exit(task, NFS4_OK);
5702 return;
5704 rpc_call_start(task);
5707 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5709 struct nfs4_layoutget *lgp = calldata;
5710 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5712 dprintk("--> %s\n", __func__);
5714 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5715 return;
5717 switch (task->tk_status) {
5718 case 0:
5719 break;
5720 case -NFS4ERR_LAYOUTTRYLATER:
5721 case -NFS4ERR_RECALLCONFLICT:
5722 task->tk_status = -NFS4ERR_DELAY;
5723 /* Fall through */
5724 default:
5725 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5726 rpc_restart_call_prepare(task);
5727 return;
5730 dprintk("<-- %s\n", __func__);
5733 static void nfs4_layoutget_release(void *calldata)
5735 struct nfs4_layoutget *lgp = calldata;
5737 dprintk("--> %s\n", __func__);
5738 put_nfs_open_context(lgp->args.ctx);
5739 kfree(calldata);
5740 dprintk("<-- %s\n", __func__);
5743 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5744 .rpc_call_prepare = nfs4_layoutget_prepare,
5745 .rpc_call_done = nfs4_layoutget_done,
5746 .rpc_release = nfs4_layoutget_release,
5749 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5751 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5752 struct rpc_task *task;
5753 struct rpc_message msg = {
5754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5755 .rpc_argp = &lgp->args,
5756 .rpc_resp = &lgp->res,
5758 struct rpc_task_setup task_setup_data = {
5759 .rpc_client = server->client,
5760 .rpc_message = &msg,
5761 .callback_ops = &nfs4_layoutget_call_ops,
5762 .callback_data = lgp,
5763 .flags = RPC_TASK_ASYNC,
5765 int status = 0;
5767 dprintk("--> %s\n", __func__);
5769 lgp->res.layoutp = &lgp->args.layout;
5770 lgp->res.seq_res.sr_slot = NULL;
5771 task = rpc_run_task(&task_setup_data);
5772 if (IS_ERR(task))
5773 return PTR_ERR(task);
5774 status = nfs4_wait_for_completion_rpc_task(task);
5775 if (status == 0)
5776 status = task->tk_status;
5777 if (status == 0)
5778 status = pnfs_layout_process(lgp);
5779 rpc_put_task(task);
5780 dprintk("<-- %s status=%d\n", __func__, status);
5781 return status;
5784 static void
5785 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5787 struct nfs4_layoutreturn *lrp = calldata;
5789 dprintk("--> %s\n", __func__);
5790 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5791 &lrp->res.seq_res, 0, task))
5792 return;
5793 rpc_call_start(task);
5796 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5798 struct nfs4_layoutreturn *lrp = calldata;
5799 struct nfs_server *server;
5800 struct pnfs_layout_hdr *lo = lrp->args.layout;
5802 dprintk("--> %s\n", __func__);
5804 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5805 return;
5807 server = NFS_SERVER(lrp->args.inode);
5808 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5809 rpc_restart_call_prepare(task);
5810 return;
5812 spin_lock(&lo->plh_inode->i_lock);
5813 if (task->tk_status == 0) {
5814 if (lrp->res.lrs_present) {
5815 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5816 } else
5817 BUG_ON(!list_empty(&lo->plh_segs));
5819 lo->plh_block_lgets--;
5820 spin_unlock(&lo->plh_inode->i_lock);
5821 dprintk("<-- %s\n", __func__);
5824 static void nfs4_layoutreturn_release(void *calldata)
5826 struct nfs4_layoutreturn *lrp = calldata;
5828 dprintk("--> %s\n", __func__);
5829 put_layout_hdr(lrp->args.layout);
5830 kfree(calldata);
5831 dprintk("<-- %s\n", __func__);
5834 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5835 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5836 .rpc_call_done = nfs4_layoutreturn_done,
5837 .rpc_release = nfs4_layoutreturn_release,
5840 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5842 struct rpc_task *task;
5843 struct rpc_message msg = {
5844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5845 .rpc_argp = &lrp->args,
5846 .rpc_resp = &lrp->res,
5848 struct rpc_task_setup task_setup_data = {
5849 .rpc_client = lrp->clp->cl_rpcclient,
5850 .rpc_message = &msg,
5851 .callback_ops = &nfs4_layoutreturn_call_ops,
5852 .callback_data = lrp,
5854 int status;
5856 dprintk("--> %s\n", __func__);
5857 task = rpc_run_task(&task_setup_data);
5858 if (IS_ERR(task))
5859 return PTR_ERR(task);
5860 status = task->tk_status;
5861 dprintk("<-- %s status=%d\n", __func__, status);
5862 rpc_put_task(task);
5863 return status;
5867 * Retrieve the list of Data Server devices from the MDS.
5869 static int _nfs4_getdevicelist(struct nfs_server *server,
5870 const struct nfs_fh *fh,
5871 struct pnfs_devicelist *devlist)
5873 struct nfs4_getdevicelist_args args = {
5874 .fh = fh,
5875 .layoutclass = server->pnfs_curr_ld->id,
5877 struct nfs4_getdevicelist_res res = {
5878 .devlist = devlist,
5880 struct rpc_message msg = {
5881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5882 .rpc_argp = &args,
5883 .rpc_resp = &res,
5885 int status;
5887 dprintk("--> %s\n", __func__);
5888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5889 &res.seq_res, 0);
5890 dprintk("<-- %s status=%d\n", __func__, status);
5891 return status;
5894 int nfs4_proc_getdevicelist(struct nfs_server *server,
5895 const struct nfs_fh *fh,
5896 struct pnfs_devicelist *devlist)
5898 struct nfs4_exception exception = { };
5899 int err;
5901 do {
5902 err = nfs4_handle_exception(server,
5903 _nfs4_getdevicelist(server, fh, devlist),
5904 &exception);
5905 } while (exception.retry);
5907 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5908 err, devlist->num_devs);
5910 return err;
5912 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5914 static int
5915 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5917 struct nfs4_getdeviceinfo_args args = {
5918 .pdev = pdev,
5920 struct nfs4_getdeviceinfo_res res = {
5921 .pdev = pdev,
5923 struct rpc_message msg = {
5924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5925 .rpc_argp = &args,
5926 .rpc_resp = &res,
5928 int status;
5930 dprintk("--> %s\n", __func__);
5931 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5932 dprintk("<-- %s status=%d\n", __func__, status);
5934 return status;
5937 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5939 struct nfs4_exception exception = { };
5940 int err;
5942 do {
5943 err = nfs4_handle_exception(server,
5944 _nfs4_proc_getdeviceinfo(server, pdev),
5945 &exception);
5946 } while (exception.retry);
5947 return err;
5949 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5951 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5953 struct nfs4_layoutcommit_data *data = calldata;
5954 struct nfs_server *server = NFS_SERVER(data->args.inode);
5956 if (nfs4_setup_sequence(server, &data->args.seq_args,
5957 &data->res.seq_res, 1, task))
5958 return;
5959 rpc_call_start(task);
5962 static void
5963 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5965 struct nfs4_layoutcommit_data *data = calldata;
5966 struct nfs_server *server = NFS_SERVER(data->args.inode);
5968 if (!nfs4_sequence_done(task, &data->res.seq_res))
5969 return;
5971 switch (task->tk_status) { /* Just ignore these failures */
5972 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5973 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5974 case -NFS4ERR_BADLAYOUT: /* no layout */
5975 case -NFS4ERR_GRACE: /* loca_recalim always false */
5976 task->tk_status = 0;
5977 break;
5978 case 0:
5979 nfs_post_op_update_inode_force_wcc(data->args.inode,
5980 data->res.fattr);
5981 break;
5982 default:
5983 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5984 rpc_restart_call_prepare(task);
5985 return;
5990 static void nfs4_layoutcommit_release(void *calldata)
5992 struct nfs4_layoutcommit_data *data = calldata;
5993 struct pnfs_layout_segment *lseg, *tmp;
5994 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
5996 pnfs_cleanup_layoutcommit(data);
5997 /* Matched by references in pnfs_set_layoutcommit */
5998 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5999 list_del_init(&lseg->pls_lc_list);
6000 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6001 &lseg->pls_flags))
6002 put_lseg(lseg);
6005 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6006 smp_mb__after_clear_bit();
6007 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6009 put_rpccred(data->cred);
6010 kfree(data);
6013 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6014 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6015 .rpc_call_done = nfs4_layoutcommit_done,
6016 .rpc_release = nfs4_layoutcommit_release,
6020 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6022 struct rpc_message msg = {
6023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6024 .rpc_argp = &data->args,
6025 .rpc_resp = &data->res,
6026 .rpc_cred = data->cred,
6028 struct rpc_task_setup task_setup_data = {
6029 .task = &data->task,
6030 .rpc_client = NFS_CLIENT(data->args.inode),
6031 .rpc_message = &msg,
6032 .callback_ops = &nfs4_layoutcommit_ops,
6033 .callback_data = data,
6034 .flags = RPC_TASK_ASYNC,
6036 struct rpc_task *task;
6037 int status = 0;
6039 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6040 "lbw: %llu inode %lu\n",
6041 data->task.tk_pid, sync,
6042 data->args.lastbytewritten,
6043 data->args.inode->i_ino);
6045 task = rpc_run_task(&task_setup_data);
6046 if (IS_ERR(task))
6047 return PTR_ERR(task);
6048 if (sync == false)
6049 goto out;
6050 status = nfs4_wait_for_completion_rpc_task(task);
6051 if (status != 0)
6052 goto out;
6053 status = task->tk_status;
6054 out:
6055 dprintk("%s: status %d\n", __func__, status);
6056 rpc_put_task(task);
6057 return status;
6060 static int
6061 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6062 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6064 struct nfs41_secinfo_no_name_args args = {
6065 .style = SECINFO_STYLE_CURRENT_FH,
6067 struct nfs4_secinfo_res res = {
6068 .flavors = flavors,
6070 struct rpc_message msg = {
6071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6072 .rpc_argp = &args,
6073 .rpc_resp = &res,
6075 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6078 static int
6079 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6080 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6082 struct nfs4_exception exception = { };
6083 int err;
6084 do {
6085 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6086 switch (err) {
6087 case 0:
6088 case -NFS4ERR_WRONGSEC:
6089 case -NFS4ERR_NOTSUPP:
6090 goto out;
6091 default:
6092 err = nfs4_handle_exception(server, err, &exception);
6094 } while (exception.retry);
6095 out:
6096 return err;
6099 static int
6100 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6101 struct nfs_fsinfo *info)
6103 int err;
6104 struct page *page;
6105 rpc_authflavor_t flavor;
6106 struct nfs4_secinfo_flavors *flavors;
6108 page = alloc_page(GFP_KERNEL);
6109 if (!page) {
6110 err = -ENOMEM;
6111 goto out;
6114 flavors = page_address(page);
6115 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6118 * Fall back on "guess and check" method if
6119 * the server doesn't support SECINFO_NO_NAME
6121 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6122 err = nfs4_find_root_sec(server, fhandle, info);
6123 goto out_freepage;
6125 if (err)
6126 goto out_freepage;
6128 flavor = nfs_find_best_sec(flavors);
6129 if (err == 0)
6130 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6132 out_freepage:
6133 put_page(page);
6134 if (err == -EACCES)
6135 return -EPERM;
6136 out:
6137 return err;
6139 static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6141 int status;
6142 struct nfs41_test_stateid_args args = {
6143 .stateid = &state->stateid,
6145 struct nfs41_test_stateid_res res;
6146 struct rpc_message msg = {
6147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6148 .rpc_argp = &args,
6149 .rpc_resp = &res,
6151 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6152 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6153 return status;
6156 static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6158 struct nfs4_exception exception = { };
6159 int err;
6160 do {
6161 err = nfs4_handle_exception(server,
6162 _nfs41_test_stateid(server, state),
6163 &exception);
6164 } while (exception.retry);
6165 return err;
6168 static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6170 int status;
6171 struct nfs41_free_stateid_args args = {
6172 .stateid = &state->stateid,
6174 struct nfs41_free_stateid_res res;
6175 struct rpc_message msg = {
6176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6177 .rpc_argp = &args,
6178 .rpc_resp = &res,
6181 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6182 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6183 return status;
6186 static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6188 struct nfs4_exception exception = { };
6189 int err;
6190 do {
6191 err = nfs4_handle_exception(server,
6192 _nfs4_free_stateid(server, state),
6193 &exception);
6194 } while (exception.retry);
6195 return err;
6197 #endif /* CONFIG_NFS_V4_1 */
6199 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6200 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6201 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6202 .recover_open = nfs4_open_reclaim,
6203 .recover_lock = nfs4_lock_reclaim,
6204 .establish_clid = nfs4_init_clientid,
6205 .get_clid_cred = nfs4_get_setclientid_cred,
6208 #if defined(CONFIG_NFS_V4_1)
6209 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6210 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6211 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6212 .recover_open = nfs4_open_reclaim,
6213 .recover_lock = nfs4_lock_reclaim,
6214 .establish_clid = nfs41_init_clientid,
6215 .get_clid_cred = nfs4_get_exchange_id_cred,
6216 .reclaim_complete = nfs41_proc_reclaim_complete,
6218 #endif /* CONFIG_NFS_V4_1 */
6220 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6221 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6222 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6223 .recover_open = nfs4_open_expired,
6224 .recover_lock = nfs4_lock_expired,
6225 .establish_clid = nfs4_init_clientid,
6226 .get_clid_cred = nfs4_get_setclientid_cred,
6229 #if defined(CONFIG_NFS_V4_1)
6230 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6231 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6232 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6233 .recover_open = nfs41_open_expired,
6234 .recover_lock = nfs41_lock_expired,
6235 .establish_clid = nfs41_init_clientid,
6236 .get_clid_cred = nfs4_get_exchange_id_cred,
6238 #endif /* CONFIG_NFS_V4_1 */
6240 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6241 .sched_state_renewal = nfs4_proc_async_renew,
6242 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6243 .renew_lease = nfs4_proc_renew,
6246 #if defined(CONFIG_NFS_V4_1)
6247 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6248 .sched_state_renewal = nfs41_proc_async_sequence,
6249 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6250 .renew_lease = nfs4_proc_sequence,
6252 #endif
6254 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6255 .minor_version = 0,
6256 .call_sync = _nfs4_call_sync,
6257 .validate_stateid = nfs4_validate_delegation_stateid,
6258 .find_root_sec = nfs4_find_root_sec,
6259 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6260 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6261 .state_renewal_ops = &nfs40_state_renewal_ops,
6264 #if defined(CONFIG_NFS_V4_1)
6265 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6266 .minor_version = 1,
6267 .call_sync = _nfs4_call_sync_session,
6268 .validate_stateid = nfs41_validate_delegation_stateid,
6269 .find_root_sec = nfs41_find_root_sec,
6270 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6271 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6272 .state_renewal_ops = &nfs41_state_renewal_ops,
6274 #endif
6276 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6277 [0] = &nfs_v4_0_minor_ops,
6278 #if defined(CONFIG_NFS_V4_1)
6279 [1] = &nfs_v4_1_minor_ops,
6280 #endif
6283 static const struct inode_operations nfs4_file_inode_operations = {
6284 .permission = nfs_permission,
6285 .getattr = nfs_getattr,
6286 .setattr = nfs_setattr,
6287 .getxattr = generic_getxattr,
6288 .setxattr = generic_setxattr,
6289 .listxattr = generic_listxattr,
6290 .removexattr = generic_removexattr,
6293 const struct nfs_rpc_ops nfs_v4_clientops = {
6294 .version = 4, /* protocol version */
6295 .dentry_ops = &nfs4_dentry_operations,
6296 .dir_inode_ops = &nfs4_dir_inode_operations,
6297 .file_inode_ops = &nfs4_file_inode_operations,
6298 .file_ops = &nfs4_file_operations,
6299 .getroot = nfs4_proc_get_root,
6300 .getattr = nfs4_proc_getattr,
6301 .setattr = nfs4_proc_setattr,
6302 .lookup = nfs4_proc_lookup,
6303 .access = nfs4_proc_access,
6304 .readlink = nfs4_proc_readlink,
6305 .create = nfs4_proc_create,
6306 .remove = nfs4_proc_remove,
6307 .unlink_setup = nfs4_proc_unlink_setup,
6308 .unlink_done = nfs4_proc_unlink_done,
6309 .rename = nfs4_proc_rename,
6310 .rename_setup = nfs4_proc_rename_setup,
6311 .rename_done = nfs4_proc_rename_done,
6312 .link = nfs4_proc_link,
6313 .symlink = nfs4_proc_symlink,
6314 .mkdir = nfs4_proc_mkdir,
6315 .rmdir = nfs4_proc_remove,
6316 .readdir = nfs4_proc_readdir,
6317 .mknod = nfs4_proc_mknod,
6318 .statfs = nfs4_proc_statfs,
6319 .fsinfo = nfs4_proc_fsinfo,
6320 .pathconf = nfs4_proc_pathconf,
6321 .set_capabilities = nfs4_server_capabilities,
6322 .decode_dirent = nfs4_decode_dirent,
6323 .read_setup = nfs4_proc_read_setup,
6324 .read_done = nfs4_read_done,
6325 .write_setup = nfs4_proc_write_setup,
6326 .write_done = nfs4_write_done,
6327 .commit_setup = nfs4_proc_commit_setup,
6328 .commit_done = nfs4_commit_done,
6329 .lock = nfs4_proc_lock,
6330 .clear_acl_cache = nfs4_zap_acl_attr,
6331 .close_context = nfs4_close_context,
6332 .open_context = nfs4_atomic_open,
6333 .init_client = nfs4_init_client,
6334 .secinfo = nfs4_proc_secinfo,
6337 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6338 .prefix = XATTR_NAME_NFSV4_ACL,
6339 .list = nfs4_xattr_list_nfs4_acl,
6340 .get = nfs4_xattr_get_nfs4_acl,
6341 .set = nfs4_xattr_set_nfs4_acl,
6344 const struct xattr_handler *nfs4_xattr_handlers[] = {
6345 &nfs4_xattr_nfs4_acl_handler,
6346 NULL
6350 * Local variables:
6351 * c-basic-offset: 8
6352 * End: