OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / fs / nfs / nfs4proc.c
blob2612223d96e3ee48dd220a13d9696aaddd02e94e
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, const char *buf, size_t acl_len)
3537 struct nfs4_cached_acl *acl;
3539 if (buf && 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 memcpy(acl->data, buf, 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 void *resp_buf;
3578 struct rpc_message msg = {
3579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3580 .rpc_argp = &args,
3581 .rpc_resp = &res,
3583 int ret = -ENOMEM, npages, i, acl_len = 0;
3585 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3586 /* As long as we're doing a round trip to the server anyway,
3587 * let's be prepared for a page of acl data. */
3588 if (npages == 0)
3589 npages = 1;
3591 for (i = 0; i < npages; i++) {
3592 pages[i] = alloc_page(GFP_KERNEL);
3593 if (!pages[i])
3594 goto out_free;
3596 if (npages > 1) {
3597 /* for decoding across pages */
3598 res.acl_scratch = alloc_page(GFP_KERNEL);
3599 if (!res.acl_scratch)
3600 goto out_free;
3602 args.acl_len = npages * PAGE_SIZE;
3603 args.acl_pgbase = 0;
3604 /* Let decode_getfacl know not to fail if the ACL data is larger than
3605 * the page we send as a guess */
3606 if (buf == NULL)
3607 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3608 resp_buf = page_address(pages[0]);
3610 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3611 __func__, buf, buflen, npages, args.acl_len);
3612 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3613 &msg, &args.seq_args, &res.seq_res, 0);
3614 if (ret)
3615 goto out_free;
3617 acl_len = res.acl_len - res.acl_data_offset;
3618 if (acl_len > args.acl_len)
3619 nfs4_write_cached_acl(inode, NULL, acl_len);
3620 else
3621 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3622 acl_len);
3623 if (buf) {
3624 ret = -ERANGE;
3625 if (acl_len > buflen)
3626 goto out_free;
3627 _copy_from_pages(buf, pages, res.acl_data_offset,
3628 acl_len);
3630 ret = acl_len;
3631 out_free:
3632 for (i = 0; i < npages; i++)
3633 if (pages[i])
3634 __free_page(pages[i]);
3635 if (res.acl_scratch)
3636 __free_page(res.acl_scratch);
3637 return ret;
3640 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3642 struct nfs4_exception exception = { };
3643 ssize_t ret;
3644 do {
3645 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3646 if (ret >= 0)
3647 break;
3648 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3649 } while (exception.retry);
3650 return ret;
3653 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3655 struct nfs_server *server = NFS_SERVER(inode);
3656 int ret;
3658 if (!nfs4_server_supports_acls(server))
3659 return -EOPNOTSUPP;
3660 ret = nfs_revalidate_inode(server, inode);
3661 if (ret < 0)
3662 return ret;
3663 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3664 nfs_zap_acl_cache(inode);
3665 ret = nfs4_read_cached_acl(inode, buf, buflen);
3666 if (ret != -ENOENT)
3667 /* -ENOENT is returned if there is no ACL or if there is an ACL
3668 * but no cached acl data, just the acl length */
3669 return ret;
3670 return nfs4_get_acl_uncached(inode, buf, buflen);
3673 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3675 struct nfs_server *server = NFS_SERVER(inode);
3676 struct page *pages[NFS4ACL_MAXPAGES];
3677 struct nfs_setaclargs arg = {
3678 .fh = NFS_FH(inode),
3679 .acl_pages = pages,
3680 .acl_len = buflen,
3682 struct nfs_setaclres res;
3683 struct rpc_message msg = {
3684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3685 .rpc_argp = &arg,
3686 .rpc_resp = &res,
3688 int ret, i;
3690 if (!nfs4_server_supports_acls(server))
3691 return -EOPNOTSUPP;
3692 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3693 if (i < 0)
3694 return i;
3695 nfs_inode_return_delegation(inode);
3696 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3699 * Free each page after tx, so the only ref left is
3700 * held by the network stack
3702 for (; i > 0; i--)
3703 put_page(pages[i-1]);
3706 * Acl update can result in inode attribute update.
3707 * so mark the attribute cache invalid.
3709 spin_lock(&inode->i_lock);
3710 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3711 spin_unlock(&inode->i_lock);
3712 nfs_access_zap_cache(inode);
3713 nfs_zap_acl_cache(inode);
3714 return ret;
3717 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3719 struct nfs4_exception exception = { };
3720 int err;
3721 do {
3722 err = nfs4_handle_exception(NFS_SERVER(inode),
3723 __nfs4_proc_set_acl(inode, buf, buflen),
3724 &exception);
3725 } while (exception.retry);
3726 return err;
3729 static int
3730 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3732 struct nfs_client *clp = server->nfs_client;
3734 if (task->tk_status >= 0)
3735 return 0;
3736 switch(task->tk_status) {
3737 case -NFS4ERR_DELEG_REVOKED:
3738 case -NFS4ERR_ADMIN_REVOKED:
3739 case -NFS4ERR_BAD_STATEID:
3740 if (state != NULL)
3741 nfs_remove_bad_delegation(state->inode);
3742 case -NFS4ERR_OPENMODE:
3743 if (state == NULL)
3744 break;
3745 nfs4_schedule_stateid_recovery(server, state);
3746 goto wait_on_recovery;
3747 case -NFS4ERR_EXPIRED:
3748 if (state != NULL)
3749 nfs4_schedule_stateid_recovery(server, state);
3750 case -NFS4ERR_STALE_STATEID:
3751 case -NFS4ERR_STALE_CLIENTID:
3752 nfs4_schedule_lease_recovery(clp);
3753 goto wait_on_recovery;
3754 #if defined(CONFIG_NFS_V4_1)
3755 case -NFS4ERR_BADSESSION:
3756 case -NFS4ERR_BADSLOT:
3757 case -NFS4ERR_BAD_HIGH_SLOT:
3758 case -NFS4ERR_DEADSESSION:
3759 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3760 case -NFS4ERR_SEQ_FALSE_RETRY:
3761 case -NFS4ERR_SEQ_MISORDERED:
3762 dprintk("%s ERROR %d, Reset session\n", __func__,
3763 task->tk_status);
3764 nfs4_schedule_session_recovery(clp->cl_session);
3765 task->tk_status = 0;
3766 return -EAGAIN;
3767 #endif /* CONFIG_NFS_V4_1 */
3768 case -NFS4ERR_DELAY:
3769 nfs_inc_server_stats(server, NFSIOS_DELAY);
3770 case -NFS4ERR_GRACE:
3771 case -EKEYEXPIRED:
3772 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3773 task->tk_status = 0;
3774 return -EAGAIN;
3775 case -NFS4ERR_RETRY_UNCACHED_REP:
3776 case -NFS4ERR_OLD_STATEID:
3777 task->tk_status = 0;
3778 return -EAGAIN;
3780 task->tk_status = nfs4_map_errors(task->tk_status);
3781 return 0;
3782 wait_on_recovery:
3783 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3784 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3785 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3786 task->tk_status = 0;
3787 return -EAGAIN;
3790 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3791 unsigned short port, struct rpc_cred *cred,
3792 struct nfs4_setclientid_res *res)
3794 nfs4_verifier sc_verifier;
3795 struct nfs4_setclientid setclientid = {
3796 .sc_verifier = &sc_verifier,
3797 .sc_prog = program,
3798 .sc_cb_ident = clp->cl_cb_ident,
3800 struct rpc_message msg = {
3801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3802 .rpc_argp = &setclientid,
3803 .rpc_resp = res,
3804 .rpc_cred = cred,
3806 __be32 *p;
3807 int loop = 0;
3808 int status;
3810 p = (__be32*)sc_verifier.data;
3811 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3812 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3814 for(;;) {
3815 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3816 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3817 clp->cl_ipaddr,
3818 rpc_peeraddr2str(clp->cl_rpcclient,
3819 RPC_DISPLAY_ADDR),
3820 rpc_peeraddr2str(clp->cl_rpcclient,
3821 RPC_DISPLAY_PROTO),
3822 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3823 clp->cl_id_uniquifier);
3824 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3825 sizeof(setclientid.sc_netid),
3826 rpc_peeraddr2str(clp->cl_rpcclient,
3827 RPC_DISPLAY_NETID));
3828 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3829 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3830 clp->cl_ipaddr, port >> 8, port & 255);
3832 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3833 if (status != -NFS4ERR_CLID_INUSE)
3834 break;
3835 if (loop != 0) {
3836 ++clp->cl_id_uniquifier;
3837 break;
3839 ++loop;
3840 ssleep(clp->cl_lease_time / HZ + 1);
3842 return status;
3845 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3846 struct nfs4_setclientid_res *arg,
3847 struct rpc_cred *cred)
3849 struct nfs_fsinfo fsinfo;
3850 struct rpc_message msg = {
3851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3852 .rpc_argp = arg,
3853 .rpc_resp = &fsinfo,
3854 .rpc_cred = cred,
3856 unsigned long now;
3857 int status;
3859 now = jiffies;
3860 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3861 if (status == 0) {
3862 spin_lock(&clp->cl_lock);
3863 clp->cl_lease_time = fsinfo.lease_time * HZ;
3864 clp->cl_last_renewal = now;
3865 spin_unlock(&clp->cl_lock);
3867 return status;
3870 struct nfs4_delegreturndata {
3871 struct nfs4_delegreturnargs args;
3872 struct nfs4_delegreturnres res;
3873 struct nfs_fh fh;
3874 nfs4_stateid stateid;
3875 unsigned long timestamp;
3876 struct nfs_fattr fattr;
3877 int rpc_status;
3880 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3882 struct nfs4_delegreturndata *data = calldata;
3884 if (!nfs4_sequence_done(task, &data->res.seq_res))
3885 return;
3887 switch (task->tk_status) {
3888 case -NFS4ERR_STALE_STATEID:
3889 case -NFS4ERR_EXPIRED:
3890 case 0:
3891 renew_lease(data->res.server, data->timestamp);
3892 break;
3893 default:
3894 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3895 -EAGAIN) {
3896 rpc_restart_call_prepare(task);
3897 return;
3900 data->rpc_status = task->tk_status;
3903 static void nfs4_delegreturn_release(void *calldata)
3905 kfree(calldata);
3908 #if defined(CONFIG_NFS_V4_1)
3909 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3911 struct nfs4_delegreturndata *d_data;
3913 d_data = (struct nfs4_delegreturndata *)data;
3915 if (nfs4_setup_sequence(d_data->res.server,
3916 &d_data->args.seq_args,
3917 &d_data->res.seq_res, 1, task))
3918 return;
3919 rpc_call_start(task);
3921 #endif /* CONFIG_NFS_V4_1 */
3923 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3924 #if defined(CONFIG_NFS_V4_1)
3925 .rpc_call_prepare = nfs4_delegreturn_prepare,
3926 #endif /* CONFIG_NFS_V4_1 */
3927 .rpc_call_done = nfs4_delegreturn_done,
3928 .rpc_release = nfs4_delegreturn_release,
3931 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3933 struct nfs4_delegreturndata *data;
3934 struct nfs_server *server = NFS_SERVER(inode);
3935 struct rpc_task *task;
3936 struct rpc_message msg = {
3937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3938 .rpc_cred = cred,
3940 struct rpc_task_setup task_setup_data = {
3941 .rpc_client = server->client,
3942 .rpc_message = &msg,
3943 .callback_ops = &nfs4_delegreturn_ops,
3944 .flags = RPC_TASK_ASYNC,
3946 int status = 0;
3948 data = kzalloc(sizeof(*data), GFP_NOFS);
3949 if (data == NULL)
3950 return -ENOMEM;
3951 data->args.fhandle = &data->fh;
3952 data->args.stateid = &data->stateid;
3953 data->args.bitmask = server->attr_bitmask;
3954 nfs_copy_fh(&data->fh, NFS_FH(inode));
3955 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3956 data->res.fattr = &data->fattr;
3957 data->res.server = server;
3958 nfs_fattr_init(data->res.fattr);
3959 data->timestamp = jiffies;
3960 data->rpc_status = 0;
3962 task_setup_data.callback_data = data;
3963 msg.rpc_argp = &data->args;
3964 msg.rpc_resp = &data->res;
3965 task = rpc_run_task(&task_setup_data);
3966 if (IS_ERR(task))
3967 return PTR_ERR(task);
3968 if (!issync)
3969 goto out;
3970 status = nfs4_wait_for_completion_rpc_task(task);
3971 if (status != 0)
3972 goto out;
3973 status = data->rpc_status;
3974 if (status != 0)
3975 goto out;
3976 nfs_refresh_inode(inode, &data->fattr);
3977 out:
3978 rpc_put_task(task);
3979 return status;
3982 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3984 struct nfs_server *server = NFS_SERVER(inode);
3985 struct nfs4_exception exception = { };
3986 int err;
3987 do {
3988 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3989 switch (err) {
3990 case -NFS4ERR_STALE_STATEID:
3991 case -NFS4ERR_EXPIRED:
3992 case 0:
3993 return 0;
3995 err = nfs4_handle_exception(server, err, &exception);
3996 } while (exception.retry);
3997 return err;
4000 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4001 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4004 * sleep, with exponential backoff, and retry the LOCK operation.
4006 static unsigned long
4007 nfs4_set_lock_task_retry(unsigned long timeout)
4009 freezable_schedule_timeout_killable(timeout);
4010 timeout <<= 1;
4011 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4012 return NFS4_LOCK_MAXTIMEOUT;
4013 return timeout;
4016 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4018 struct inode *inode = state->inode;
4019 struct nfs_server *server = NFS_SERVER(inode);
4020 struct nfs_client *clp = server->nfs_client;
4021 struct nfs_lockt_args arg = {
4022 .fh = NFS_FH(inode),
4023 .fl = request,
4025 struct nfs_lockt_res res = {
4026 .denied = request,
4028 struct rpc_message msg = {
4029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4030 .rpc_argp = &arg,
4031 .rpc_resp = &res,
4032 .rpc_cred = state->owner->so_cred,
4034 struct nfs4_lock_state *lsp;
4035 int status;
4037 arg.lock_owner.clientid = clp->cl_clientid;
4038 status = nfs4_set_lock_state(state, request);
4039 if (status != 0)
4040 goto out;
4041 lsp = request->fl_u.nfs4_fl.owner;
4042 arg.lock_owner.id = lsp->ls_id.id;
4043 arg.lock_owner.s_dev = server->s_dev;
4044 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4045 switch (status) {
4046 case 0:
4047 request->fl_type = F_UNLCK;
4048 break;
4049 case -NFS4ERR_DENIED:
4050 status = 0;
4052 request->fl_ops->fl_release_private(request);
4053 out:
4054 return status;
4057 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4059 struct nfs4_exception exception = { };
4060 int err;
4062 do {
4063 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4064 _nfs4_proc_getlk(state, cmd, request),
4065 &exception);
4066 } while (exception.retry);
4067 return err;
4070 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4072 int res = 0;
4073 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4074 case FL_POSIX:
4075 res = posix_lock_file_wait(file, fl);
4076 break;
4077 case FL_FLOCK:
4078 res = flock_lock_file_wait(file, fl);
4079 break;
4080 default:
4081 BUG();
4083 return res;
4086 struct nfs4_unlockdata {
4087 struct nfs_locku_args arg;
4088 struct nfs_locku_res res;
4089 struct nfs4_lock_state *lsp;
4090 struct nfs_open_context *ctx;
4091 struct file_lock fl;
4092 const struct nfs_server *server;
4093 unsigned long timestamp;
4096 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4097 struct nfs_open_context *ctx,
4098 struct nfs4_lock_state *lsp,
4099 struct nfs_seqid *seqid)
4101 struct nfs4_unlockdata *p;
4102 struct inode *inode = lsp->ls_state->inode;
4104 p = kzalloc(sizeof(*p), GFP_NOFS);
4105 if (p == NULL)
4106 return NULL;
4107 p->arg.fh = NFS_FH(inode);
4108 p->arg.fl = &p->fl;
4109 p->arg.seqid = seqid;
4110 p->res.seqid = seqid;
4111 p->arg.stateid = &lsp->ls_stateid;
4112 p->lsp = lsp;
4113 atomic_inc(&lsp->ls_count);
4114 /* Ensure we don't close file until we're done freeing locks! */
4115 p->ctx = get_nfs_open_context(ctx);
4116 memcpy(&p->fl, fl, sizeof(p->fl));
4117 p->server = NFS_SERVER(inode);
4118 return p;
4121 static void nfs4_locku_release_calldata(void *data)
4123 struct nfs4_unlockdata *calldata = data;
4124 nfs_free_seqid(calldata->arg.seqid);
4125 nfs4_put_lock_state(calldata->lsp);
4126 put_nfs_open_context(calldata->ctx);
4127 kfree(calldata);
4130 static void nfs4_locku_done(struct rpc_task *task, void *data)
4132 struct nfs4_unlockdata *calldata = data;
4134 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4135 return;
4136 switch (task->tk_status) {
4137 case 0:
4138 memcpy(calldata->lsp->ls_stateid.data,
4139 calldata->res.stateid.data,
4140 sizeof(calldata->lsp->ls_stateid.data));
4141 renew_lease(calldata->server, calldata->timestamp);
4142 break;
4143 case -NFS4ERR_BAD_STATEID:
4144 case -NFS4ERR_OLD_STATEID:
4145 case -NFS4ERR_STALE_STATEID:
4146 case -NFS4ERR_EXPIRED:
4147 break;
4148 default:
4149 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4150 rpc_restart_call_prepare(task);
4154 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4156 struct nfs4_unlockdata *calldata = data;
4158 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4159 return;
4160 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4161 /* Note: exit _without_ running nfs4_locku_done */
4162 task->tk_action = NULL;
4163 return;
4165 calldata->timestamp = jiffies;
4166 if (nfs4_setup_sequence(calldata->server,
4167 &calldata->arg.seq_args,
4168 &calldata->res.seq_res, 1, task))
4169 return;
4170 rpc_call_start(task);
4173 static const struct rpc_call_ops nfs4_locku_ops = {
4174 .rpc_call_prepare = nfs4_locku_prepare,
4175 .rpc_call_done = nfs4_locku_done,
4176 .rpc_release = nfs4_locku_release_calldata,
4179 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4180 struct nfs_open_context *ctx,
4181 struct nfs4_lock_state *lsp,
4182 struct nfs_seqid *seqid)
4184 struct nfs4_unlockdata *data;
4185 struct rpc_message msg = {
4186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4187 .rpc_cred = ctx->cred,
4189 struct rpc_task_setup task_setup_data = {
4190 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4191 .rpc_message = &msg,
4192 .callback_ops = &nfs4_locku_ops,
4193 .workqueue = nfsiod_workqueue,
4194 .flags = RPC_TASK_ASYNC,
4197 /* Ensure this is an unlock - when canceling a lock, the
4198 * canceled lock is passed in, and it won't be an unlock.
4200 fl->fl_type = F_UNLCK;
4202 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4203 if (data == NULL) {
4204 nfs_free_seqid(seqid);
4205 return ERR_PTR(-ENOMEM);
4208 msg.rpc_argp = &data->arg;
4209 msg.rpc_resp = &data->res;
4210 task_setup_data.callback_data = data;
4211 return rpc_run_task(&task_setup_data);
4214 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4216 struct nfs_inode *nfsi = NFS_I(state->inode);
4217 struct nfs_seqid *seqid;
4218 struct nfs4_lock_state *lsp;
4219 struct rpc_task *task;
4220 int status = 0;
4221 unsigned char fl_flags = request->fl_flags;
4223 status = nfs4_set_lock_state(state, request);
4224 /* Unlock _before_ we do the RPC call */
4225 request->fl_flags |= FL_EXISTS;
4226 down_read(&nfsi->rwsem);
4227 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4228 up_read(&nfsi->rwsem);
4229 goto out;
4231 up_read(&nfsi->rwsem);
4232 if (status != 0)
4233 goto out;
4234 /* Is this a delegated lock? */
4235 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4236 goto out;
4237 lsp = request->fl_u.nfs4_fl.owner;
4238 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4239 status = -ENOMEM;
4240 if (seqid == NULL)
4241 goto out;
4242 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4243 status = PTR_ERR(task);
4244 if (IS_ERR(task))
4245 goto out;
4246 status = nfs4_wait_for_completion_rpc_task(task);
4247 rpc_put_task(task);
4248 out:
4249 request->fl_flags = fl_flags;
4250 return status;
4253 struct nfs4_lockdata {
4254 struct nfs_lock_args arg;
4255 struct nfs_lock_res res;
4256 struct nfs4_lock_state *lsp;
4257 struct nfs_open_context *ctx;
4258 struct file_lock fl;
4259 unsigned long timestamp;
4260 int rpc_status;
4261 int cancelled;
4262 struct nfs_server *server;
4265 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4266 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4267 gfp_t gfp_mask)
4269 struct nfs4_lockdata *p;
4270 struct inode *inode = lsp->ls_state->inode;
4271 struct nfs_server *server = NFS_SERVER(inode);
4273 p = kzalloc(sizeof(*p), gfp_mask);
4274 if (p == NULL)
4275 return NULL;
4277 p->arg.fh = NFS_FH(inode);
4278 p->arg.fl = &p->fl;
4279 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4280 if (p->arg.open_seqid == NULL)
4281 goto out_free;
4282 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4283 if (p->arg.lock_seqid == NULL)
4284 goto out_free_seqid;
4285 p->arg.lock_stateid = &lsp->ls_stateid;
4286 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4287 p->arg.lock_owner.id = lsp->ls_id.id;
4288 p->arg.lock_owner.s_dev = server->s_dev;
4289 p->res.lock_seqid = p->arg.lock_seqid;
4290 p->lsp = lsp;
4291 p->server = server;
4292 atomic_inc(&lsp->ls_count);
4293 p->ctx = get_nfs_open_context(ctx);
4294 memcpy(&p->fl, fl, sizeof(p->fl));
4295 return p;
4296 out_free_seqid:
4297 nfs_free_seqid(p->arg.open_seqid);
4298 out_free:
4299 kfree(p);
4300 return NULL;
4303 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4305 struct nfs4_lockdata *data = calldata;
4306 struct nfs4_state *state = data->lsp->ls_state;
4308 dprintk("%s: begin!\n", __func__);
4309 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4310 return;
4311 /* Do we need to do an open_to_lock_owner? */
4312 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4313 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4314 return;
4315 data->arg.open_stateid = &state->stateid;
4316 data->arg.new_lock_owner = 1;
4317 data->res.open_seqid = data->arg.open_seqid;
4318 } else
4319 data->arg.new_lock_owner = 0;
4320 data->timestamp = jiffies;
4321 if (nfs4_setup_sequence(data->server,
4322 &data->arg.seq_args,
4323 &data->res.seq_res, 1, task))
4324 return;
4325 rpc_call_start(task);
4326 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4329 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4331 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4332 nfs4_lock_prepare(task, calldata);
4335 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4337 struct nfs4_lockdata *data = calldata;
4339 dprintk("%s: begin!\n", __func__);
4341 if (!nfs4_sequence_done(task, &data->res.seq_res))
4342 return;
4344 data->rpc_status = task->tk_status;
4345 if (data->arg.new_lock_owner != 0) {
4346 if (data->rpc_status == 0)
4347 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4348 else
4349 goto out;
4351 if (data->rpc_status == 0) {
4352 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4353 sizeof(data->lsp->ls_stateid.data));
4354 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4355 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4357 out:
4358 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4361 static void nfs4_lock_release(void *calldata)
4363 struct nfs4_lockdata *data = calldata;
4365 dprintk("%s: begin!\n", __func__);
4366 nfs_free_seqid(data->arg.open_seqid);
4367 if (data->cancelled != 0) {
4368 struct rpc_task *task;
4369 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4370 data->arg.lock_seqid);
4371 if (!IS_ERR(task))
4372 rpc_put_task_async(task);
4373 dprintk("%s: cancelling lock!\n", __func__);
4374 } else
4375 nfs_free_seqid(data->arg.lock_seqid);
4376 nfs4_put_lock_state(data->lsp);
4377 put_nfs_open_context(data->ctx);
4378 kfree(data);
4379 dprintk("%s: done!\n", __func__);
4382 static const struct rpc_call_ops nfs4_lock_ops = {
4383 .rpc_call_prepare = nfs4_lock_prepare,
4384 .rpc_call_done = nfs4_lock_done,
4385 .rpc_release = nfs4_lock_release,
4388 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4389 .rpc_call_prepare = nfs4_recover_lock_prepare,
4390 .rpc_call_done = nfs4_lock_done,
4391 .rpc_release = nfs4_lock_release,
4394 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4396 switch (error) {
4397 case -NFS4ERR_ADMIN_REVOKED:
4398 case -NFS4ERR_BAD_STATEID:
4399 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4400 if (new_lock_owner != 0 ||
4401 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4402 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4403 break;
4404 case -NFS4ERR_STALE_STATEID:
4405 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4406 case -NFS4ERR_EXPIRED:
4407 nfs4_schedule_lease_recovery(server->nfs_client);
4411 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4413 struct nfs4_lockdata *data;
4414 struct rpc_task *task;
4415 struct rpc_message msg = {
4416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4417 .rpc_cred = state->owner->so_cred,
4419 struct rpc_task_setup task_setup_data = {
4420 .rpc_client = NFS_CLIENT(state->inode),
4421 .rpc_message = &msg,
4422 .callback_ops = &nfs4_lock_ops,
4423 .workqueue = nfsiod_workqueue,
4424 .flags = RPC_TASK_ASYNC,
4426 int ret;
4428 dprintk("%s: begin!\n", __func__);
4429 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4430 fl->fl_u.nfs4_fl.owner,
4431 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4432 if (data == NULL)
4433 return -ENOMEM;
4434 if (IS_SETLKW(cmd))
4435 data->arg.block = 1;
4436 if (recovery_type > NFS_LOCK_NEW) {
4437 if (recovery_type == NFS_LOCK_RECLAIM)
4438 data->arg.reclaim = NFS_LOCK_RECLAIM;
4439 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4441 msg.rpc_argp = &data->arg;
4442 msg.rpc_resp = &data->res;
4443 task_setup_data.callback_data = data;
4444 task = rpc_run_task(&task_setup_data);
4445 if (IS_ERR(task))
4446 return PTR_ERR(task);
4447 ret = nfs4_wait_for_completion_rpc_task(task);
4448 if (ret == 0) {
4449 ret = data->rpc_status;
4450 if (ret)
4451 nfs4_handle_setlk_error(data->server, data->lsp,
4452 data->arg.new_lock_owner, ret);
4453 } else
4454 data->cancelled = 1;
4455 rpc_put_task(task);
4456 dprintk("%s: done, ret = %d!\n", __func__, ret);
4457 return ret;
4460 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4462 struct nfs_server *server = NFS_SERVER(state->inode);
4463 struct nfs4_exception exception = {
4464 .inode = state->inode,
4466 int err;
4468 do {
4469 /* Cache the lock if possible... */
4470 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4471 return 0;
4472 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4473 if (err != -NFS4ERR_DELAY)
4474 break;
4475 nfs4_handle_exception(server, err, &exception);
4476 } while (exception.retry);
4477 return err;
4480 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4482 struct nfs_server *server = NFS_SERVER(state->inode);
4483 struct nfs4_exception exception = {
4484 .inode = state->inode,
4486 int err;
4488 err = nfs4_set_lock_state(state, request);
4489 if (err != 0)
4490 return err;
4491 do {
4492 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4493 return 0;
4494 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4495 switch (err) {
4496 default:
4497 goto out;
4498 case -NFS4ERR_GRACE:
4499 case -NFS4ERR_DELAY:
4500 nfs4_handle_exception(server, err, &exception);
4501 err = 0;
4503 } while (exception.retry);
4504 out:
4505 return err;
4508 #if defined(CONFIG_NFS_V4_1)
4509 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4511 int status;
4512 struct nfs_server *server = NFS_SERVER(state->inode);
4514 status = nfs41_test_stateid(server, state);
4515 if (status == NFS_OK)
4516 return 0;
4517 nfs41_free_stateid(server, state);
4518 return nfs4_lock_expired(state, request);
4520 #endif
4522 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4524 struct nfs_inode *nfsi = NFS_I(state->inode);
4525 unsigned char fl_flags = request->fl_flags;
4526 int status = -ENOLCK;
4528 if ((fl_flags & FL_POSIX) &&
4529 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4530 goto out;
4531 /* Is this a delegated open? */
4532 status = nfs4_set_lock_state(state, request);
4533 if (status != 0)
4534 goto out;
4535 request->fl_flags |= FL_ACCESS;
4536 status = do_vfs_lock(request->fl_file, request);
4537 if (status < 0)
4538 goto out;
4539 down_read(&nfsi->rwsem);
4540 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4541 /* Yes: cache locks! */
4542 /* ...but avoid races with delegation recall... */
4543 request->fl_flags = fl_flags & ~FL_SLEEP;
4544 status = do_vfs_lock(request->fl_file, request);
4545 goto out_unlock;
4547 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4548 if (status != 0)
4549 goto out_unlock;
4550 /* Note: we always want to sleep here! */
4551 request->fl_flags = fl_flags | FL_SLEEP;
4552 if (do_vfs_lock(request->fl_file, request) < 0)
4553 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4554 out_unlock:
4555 up_read(&nfsi->rwsem);
4556 out:
4557 request->fl_flags = fl_flags;
4558 return status;
4561 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4563 struct nfs4_exception exception = {
4564 .state = state,
4565 .inode = state->inode,
4567 int err;
4569 do {
4570 err = _nfs4_proc_setlk(state, cmd, request);
4571 if (err == -NFS4ERR_DENIED)
4572 err = -EAGAIN;
4573 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4574 err, &exception);
4575 } while (exception.retry);
4576 return err;
4579 static int
4580 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4582 struct nfs_open_context *ctx;
4583 struct nfs4_state *state;
4584 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4585 int status;
4587 /* verify open state */
4588 ctx = nfs_file_open_context(filp);
4589 state = ctx->state;
4591 if (request->fl_start < 0 || request->fl_end < 0)
4592 return -EINVAL;
4594 if (IS_GETLK(cmd)) {
4595 if (state != NULL)
4596 return nfs4_proc_getlk(state, F_GETLK, request);
4597 return 0;
4600 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4601 return -EINVAL;
4603 if (request->fl_type == F_UNLCK) {
4604 if (state != NULL)
4605 return nfs4_proc_unlck(state, cmd, request);
4606 return 0;
4609 if (state == NULL)
4610 return -ENOLCK;
4612 * Don't rely on the VFS having checked the file open mode,
4613 * since it won't do this for flock() locks.
4615 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4616 case F_RDLCK:
4617 if (!(filp->f_mode & FMODE_READ))
4618 return -EBADF;
4619 break;
4620 case F_WRLCK:
4621 if (!(filp->f_mode & FMODE_WRITE))
4622 return -EBADF;
4625 do {
4626 status = nfs4_proc_setlk(state, cmd, request);
4627 if ((status != -EAGAIN) || IS_SETLK(cmd))
4628 break;
4629 timeout = nfs4_set_lock_task_retry(timeout);
4630 status = -ERESTARTSYS;
4631 if (signalled())
4632 break;
4633 } while(status < 0);
4634 return status;
4637 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4639 struct nfs_server *server = NFS_SERVER(state->inode);
4640 struct nfs4_exception exception = { };
4641 int err;
4643 err = nfs4_set_lock_state(state, fl);
4644 if (err != 0)
4645 goto out;
4646 do {
4647 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4648 switch (err) {
4649 default:
4650 printk(KERN_ERR "%s: unhandled error %d.\n",
4651 __func__, err);
4652 case 0:
4653 case -ESTALE:
4654 goto out;
4655 case -NFS4ERR_EXPIRED:
4656 nfs4_schedule_stateid_recovery(server, state);
4657 case -NFS4ERR_STALE_CLIENTID:
4658 case -NFS4ERR_STALE_STATEID:
4659 nfs4_schedule_lease_recovery(server->nfs_client);
4660 goto out;
4661 case -NFS4ERR_BADSESSION:
4662 case -NFS4ERR_BADSLOT:
4663 case -NFS4ERR_BAD_HIGH_SLOT:
4664 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4665 case -NFS4ERR_DEADSESSION:
4666 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4667 goto out;
4668 case -ERESTARTSYS:
4670 * The show must go on: exit, but mark the
4671 * stateid as needing recovery.
4673 case -NFS4ERR_DELEG_REVOKED:
4674 case -NFS4ERR_ADMIN_REVOKED:
4675 case -NFS4ERR_BAD_STATEID:
4676 case -NFS4ERR_OPENMODE:
4677 nfs4_schedule_stateid_recovery(server, state);
4678 err = 0;
4679 goto out;
4680 case -EKEYEXPIRED:
4682 * User RPCSEC_GSS context has expired.
4683 * We cannot recover this stateid now, so
4684 * skip it and allow recovery thread to
4685 * proceed.
4687 err = 0;
4688 goto out;
4689 case -ENOMEM:
4690 case -NFS4ERR_DENIED:
4691 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4692 err = 0;
4693 goto out;
4694 case -NFS4ERR_DELAY:
4695 break;
4697 err = nfs4_handle_exception(server, err, &exception);
4698 } while (exception.retry);
4699 out:
4700 return err;
4703 static void nfs4_release_lockowner_release(void *calldata)
4705 kfree(calldata);
4708 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4709 .rpc_release = nfs4_release_lockowner_release,
4712 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4714 struct nfs_server *server = lsp->ls_state->owner->so_server;
4715 struct nfs_release_lockowner_args *args;
4716 struct rpc_message msg = {
4717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4720 if (server->nfs_client->cl_mvops->minor_version != 0)
4721 return;
4722 args = kmalloc(sizeof(*args), GFP_NOFS);
4723 if (!args)
4724 return;
4725 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4726 args->lock_owner.id = lsp->ls_id.id;
4727 args->lock_owner.s_dev = server->s_dev;
4728 msg.rpc_argp = args;
4729 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4732 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4734 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4735 const void *buf, size_t buflen,
4736 int flags, int type)
4738 if (strcmp(key, "") != 0)
4739 return -EINVAL;
4741 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4744 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4745 void *buf, size_t buflen, int type)
4747 if (strcmp(key, "") != 0)
4748 return -EINVAL;
4750 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4753 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4754 size_t list_len, const char *name,
4755 size_t name_len, int type)
4757 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4759 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4760 return 0;
4762 if (list && len <= list_len)
4763 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4764 return len;
4768 * nfs_fhget will use either the mounted_on_fileid or the fileid
4770 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4772 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4773 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4774 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4775 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4776 return;
4778 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4779 NFS_ATTR_FATTR_NLINK;
4780 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4781 fattr->nlink = 2;
4784 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4785 struct nfs4_fs_locations *fs_locations, struct page *page)
4787 struct nfs_server *server = NFS_SERVER(dir);
4788 u32 bitmask[2] = {
4789 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4791 struct nfs4_fs_locations_arg args = {
4792 .dir_fh = NFS_FH(dir),
4793 .name = name,
4794 .page = page,
4795 .bitmask = bitmask,
4797 struct nfs4_fs_locations_res res = {
4798 .fs_locations = fs_locations,
4800 struct rpc_message msg = {
4801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4802 .rpc_argp = &args,
4803 .rpc_resp = &res,
4805 int status;
4807 dprintk("%s: start\n", __func__);
4809 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4810 * is not supported */
4811 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4812 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4813 else
4814 bitmask[0] |= FATTR4_WORD0_FILEID;
4816 nfs_fattr_init(&fs_locations->fattr);
4817 fs_locations->server = server;
4818 fs_locations->nlocations = 0;
4819 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4820 dprintk("%s: returned status = %d\n", __func__, status);
4821 return status;
4824 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4826 int status;
4827 struct nfs4_secinfo_arg args = {
4828 .dir_fh = NFS_FH(dir),
4829 .name = name,
4831 struct nfs4_secinfo_res res = {
4832 .flavors = flavors,
4834 struct rpc_message msg = {
4835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4836 .rpc_argp = &args,
4837 .rpc_resp = &res,
4840 dprintk("NFS call secinfo %s\n", name->name);
4841 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4842 dprintk("NFS reply secinfo: %d\n", status);
4843 return status;
4846 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4848 struct nfs4_exception exception = { };
4849 int err;
4850 do {
4851 err = nfs4_handle_exception(NFS_SERVER(dir),
4852 _nfs4_proc_secinfo(dir, name, flavors),
4853 &exception);
4854 } while (exception.retry);
4855 return err;
4858 #ifdef CONFIG_NFS_V4_1
4860 * Check the exchange flags returned by the server for invalid flags, having
4861 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4862 * DS flags set.
4864 static int nfs4_check_cl_exchange_flags(u32 flags)
4866 if (flags & ~EXCHGID4_FLAG_MASK_R)
4867 goto out_inval;
4868 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4869 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4870 goto out_inval;
4871 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4872 goto out_inval;
4873 return NFS_OK;
4874 out_inval:
4875 return -NFS4ERR_INVAL;
4878 static bool
4879 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4881 if (a->server_scope_sz == b->server_scope_sz &&
4882 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4883 return true;
4885 return false;
4889 * nfs4_proc_exchange_id()
4891 * Since the clientid has expired, all compounds using sessions
4892 * associated with the stale clientid will be returning
4893 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4894 * be in some phase of session reset.
4896 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4898 nfs4_verifier verifier;
4899 struct nfs41_exchange_id_args args = {
4900 .client = clp,
4901 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4903 struct nfs41_exchange_id_res res = {
4904 .client = clp,
4906 int status;
4907 struct rpc_message msg = {
4908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4909 .rpc_argp = &args,
4910 .rpc_resp = &res,
4911 .rpc_cred = cred,
4913 __be32 *p;
4915 dprintk("--> %s\n", __func__);
4916 BUG_ON(clp == NULL);
4918 p = (u32 *)verifier.data;
4919 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4920 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4921 args.verifier = &verifier;
4923 args.id_len = scnprintf(args.id, sizeof(args.id),
4924 "%s/%s.%s/%u",
4925 clp->cl_ipaddr,
4926 init_utsname()->nodename,
4927 init_utsname()->domainname,
4928 clp->cl_rpcclient->cl_auth->au_flavor);
4930 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4931 if (unlikely(!res.server_scope)) {
4932 status = -ENOMEM;
4933 goto out;
4936 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4937 if (!status)
4938 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4940 if (!status) {
4941 if (clp->server_scope &&
4942 !nfs41_same_server_scope(clp->server_scope,
4943 res.server_scope)) {
4944 dprintk("%s: server_scope mismatch detected\n",
4945 __func__);
4946 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4947 kfree(clp->server_scope);
4948 clp->server_scope = NULL;
4951 if (!clp->server_scope) {
4952 clp->server_scope = res.server_scope;
4953 goto out;
4956 kfree(res.server_scope);
4957 out:
4958 dprintk("<-- %s status= %d\n", __func__, status);
4959 return status;
4962 struct nfs4_get_lease_time_data {
4963 struct nfs4_get_lease_time_args *args;
4964 struct nfs4_get_lease_time_res *res;
4965 struct nfs_client *clp;
4968 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4969 void *calldata)
4971 int ret;
4972 struct nfs4_get_lease_time_data *data =
4973 (struct nfs4_get_lease_time_data *)calldata;
4975 dprintk("--> %s\n", __func__);
4976 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4977 /* just setup sequence, do not trigger session recovery
4978 since we're invoked within one */
4979 ret = nfs41_setup_sequence(data->clp->cl_session,
4980 &data->args->la_seq_args,
4981 &data->res->lr_seq_res, 0, task);
4983 BUG_ON(ret == -EAGAIN);
4984 rpc_call_start(task);
4985 dprintk("<-- %s\n", __func__);
4989 * Called from nfs4_state_manager thread for session setup, so don't recover
4990 * from sequence operation or clientid errors.
4992 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4994 struct nfs4_get_lease_time_data *data =
4995 (struct nfs4_get_lease_time_data *)calldata;
4997 dprintk("--> %s\n", __func__);
4998 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4999 return;
5000 switch (task->tk_status) {
5001 case -NFS4ERR_DELAY:
5002 case -NFS4ERR_GRACE:
5003 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5004 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5005 task->tk_status = 0;
5006 /* fall through */
5007 case -NFS4ERR_RETRY_UNCACHED_REP:
5008 rpc_restart_call_prepare(task);
5009 return;
5011 dprintk("<-- %s\n", __func__);
5014 struct rpc_call_ops nfs4_get_lease_time_ops = {
5015 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5016 .rpc_call_done = nfs4_get_lease_time_done,
5019 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5021 struct rpc_task *task;
5022 struct nfs4_get_lease_time_args args;
5023 struct nfs4_get_lease_time_res res = {
5024 .lr_fsinfo = fsinfo,
5026 struct nfs4_get_lease_time_data data = {
5027 .args = &args,
5028 .res = &res,
5029 .clp = clp,
5031 struct rpc_message msg = {
5032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5033 .rpc_argp = &args,
5034 .rpc_resp = &res,
5036 struct rpc_task_setup task_setup = {
5037 .rpc_client = clp->cl_rpcclient,
5038 .rpc_message = &msg,
5039 .callback_ops = &nfs4_get_lease_time_ops,
5040 .callback_data = &data,
5041 .flags = RPC_TASK_TIMEOUT,
5043 int status;
5045 dprintk("--> %s\n", __func__);
5046 task = rpc_run_task(&task_setup);
5048 if (IS_ERR(task))
5049 status = PTR_ERR(task);
5050 else {
5051 status = task->tk_status;
5052 rpc_put_task(task);
5054 dprintk("<-- %s return %d\n", __func__, status);
5056 return status;
5059 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5061 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5064 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5065 struct nfs4_slot *new,
5066 u32 max_slots,
5067 u32 ivalue)
5069 struct nfs4_slot *old = NULL;
5070 u32 i;
5072 spin_lock(&tbl->slot_tbl_lock);
5073 if (new) {
5074 old = tbl->slots;
5075 tbl->slots = new;
5076 tbl->max_slots = max_slots;
5078 tbl->highest_used_slotid = -1; /* no slot is currently used */
5079 for (i = 0; i < tbl->max_slots; i++)
5080 tbl->slots[i].seq_nr = ivalue;
5081 spin_unlock(&tbl->slot_tbl_lock);
5082 kfree(old);
5086 * (re)Initialise a slot table
5088 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5089 u32 ivalue)
5091 struct nfs4_slot *new = NULL;
5092 int ret = -ENOMEM;
5094 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5095 max_reqs, tbl->max_slots);
5097 /* Does the newly negotiated max_reqs match the existing slot table? */
5098 if (max_reqs != tbl->max_slots) {
5099 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5100 if (!new)
5101 goto out;
5103 ret = 0;
5105 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5106 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5107 tbl, tbl->slots, tbl->max_slots);
5108 out:
5109 dprintk("<-- %s: return %d\n", __func__, ret);
5110 return ret;
5113 /* Destroy the slot table */
5114 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5116 if (session->fc_slot_table.slots != NULL) {
5117 kfree(session->fc_slot_table.slots);
5118 session->fc_slot_table.slots = NULL;
5120 if (session->bc_slot_table.slots != NULL) {
5121 kfree(session->bc_slot_table.slots);
5122 session->bc_slot_table.slots = NULL;
5124 return;
5128 * Initialize or reset the forechannel and backchannel tables
5130 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5132 struct nfs4_slot_table *tbl;
5133 int status;
5135 dprintk("--> %s\n", __func__);
5136 /* Fore channel */
5137 tbl = &ses->fc_slot_table;
5138 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5139 if (status) /* -ENOMEM */
5140 return status;
5141 /* Back channel */
5142 tbl = &ses->bc_slot_table;
5143 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5144 if (status && tbl->slots == NULL)
5145 /* Fore and back channel share a connection so get
5146 * both slot tables or neither */
5147 nfs4_destroy_slot_tables(ses);
5148 return status;
5151 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5153 struct nfs4_session *session;
5154 struct nfs4_slot_table *tbl;
5156 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5157 if (!session)
5158 return NULL;
5160 tbl = &session->fc_slot_table;
5161 tbl->highest_used_slotid = -1;
5162 spin_lock_init(&tbl->slot_tbl_lock);
5163 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5164 init_completion(&tbl->complete);
5166 tbl = &session->bc_slot_table;
5167 tbl->highest_used_slotid = -1;
5168 spin_lock_init(&tbl->slot_tbl_lock);
5169 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5170 init_completion(&tbl->complete);
5172 session->session_state = 1<<NFS4_SESSION_INITING;
5174 session->clp = clp;
5175 return session;
5178 void nfs4_destroy_session(struct nfs4_session *session)
5180 nfs4_proc_destroy_session(session);
5181 dprintk("%s Destroy backchannel for xprt %p\n",
5182 __func__, session->clp->cl_rpcclient->cl_xprt);
5183 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5184 NFS41_BC_MIN_CALLBACKS);
5185 nfs4_destroy_slot_tables(session);
5186 kfree(session);
5190 * Initialize the values to be used by the client in CREATE_SESSION
5191 * If nfs4_init_session set the fore channel request and response sizes,
5192 * use them.
5194 * Set the back channel max_resp_sz_cached to zero to force the client to
5195 * always set csa_cachethis to FALSE because the current implementation
5196 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5198 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5200 struct nfs4_session *session = args->client->cl_session;
5201 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5202 mxresp_sz = session->fc_attrs.max_resp_sz;
5204 if (mxrqst_sz == 0)
5205 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5206 if (mxresp_sz == 0)
5207 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5208 /* Fore channel attributes */
5209 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5210 args->fc_attrs.max_resp_sz = mxresp_sz;
5211 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5212 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5214 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5215 "max_ops=%u max_reqs=%u\n",
5216 __func__,
5217 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5218 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5220 /* Back channel attributes */
5221 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5222 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5223 args->bc_attrs.max_resp_sz_cached = 0;
5224 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5225 args->bc_attrs.max_reqs = 1;
5227 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5228 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5229 __func__,
5230 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5231 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5232 args->bc_attrs.max_reqs);
5235 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5237 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5238 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5240 if (rcvd->max_resp_sz > sent->max_resp_sz)
5241 return -EINVAL;
5243 * Our requested max_ops is the minimum we need; we're not
5244 * prepared to break up compounds into smaller pieces than that.
5245 * So, no point even trying to continue if the server won't
5246 * cooperate:
5248 if (rcvd->max_ops < sent->max_ops)
5249 return -EINVAL;
5250 if (rcvd->max_reqs == 0)
5251 return -EINVAL;
5252 return 0;
5255 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5257 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5258 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5260 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5261 return -EINVAL;
5262 if (rcvd->max_resp_sz < sent->max_resp_sz)
5263 return -EINVAL;
5264 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5265 return -EINVAL;
5266 /* These would render the backchannel useless: */
5267 if (rcvd->max_ops == 0)
5268 return -EINVAL;
5269 if (rcvd->max_reqs == 0)
5270 return -EINVAL;
5271 return 0;
5274 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5275 struct nfs4_session *session)
5277 int ret;
5279 ret = nfs4_verify_fore_channel_attrs(args, session);
5280 if (ret)
5281 return ret;
5282 return nfs4_verify_back_channel_attrs(args, session);
5285 static int _nfs4_proc_create_session(struct nfs_client *clp)
5287 struct nfs4_session *session = clp->cl_session;
5288 struct nfs41_create_session_args args = {
5289 .client = clp,
5290 .cb_program = NFS4_CALLBACK,
5292 struct nfs41_create_session_res res = {
5293 .client = clp,
5295 struct rpc_message msg = {
5296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5297 .rpc_argp = &args,
5298 .rpc_resp = &res,
5300 int status;
5302 nfs4_init_channel_attrs(&args);
5303 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5305 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5307 if (!status)
5308 /* Verify the session's negotiated channel_attrs values */
5309 status = nfs4_verify_channel_attrs(&args, session);
5310 if (!status) {
5311 /* Increment the clientid slot sequence id */
5312 clp->cl_seqid++;
5315 return status;
5319 * Issues a CREATE_SESSION operation to the server.
5320 * It is the responsibility of the caller to verify the session is
5321 * expired before calling this routine.
5323 int nfs4_proc_create_session(struct nfs_client *clp)
5325 int status;
5326 unsigned *ptr;
5327 struct nfs4_session *session = clp->cl_session;
5329 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5331 status = _nfs4_proc_create_session(clp);
5332 if (status)
5333 goto out;
5335 /* Init or reset the session slot tables */
5336 status = nfs4_setup_session_slot_tables(session);
5337 dprintk("slot table setup returned %d\n", status);
5338 if (status)
5339 goto out;
5341 ptr = (unsigned *)&session->sess_id.data[0];
5342 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5343 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5344 out:
5345 dprintk("<-- %s\n", __func__);
5346 return status;
5350 * Issue the over-the-wire RPC DESTROY_SESSION.
5351 * The caller must serialize access to this routine.
5353 int nfs4_proc_destroy_session(struct nfs4_session *session)
5355 int status = 0;
5356 struct rpc_message msg;
5358 dprintk("--> nfs4_proc_destroy_session\n");
5360 /* session is still being setup */
5361 if (session->clp->cl_cons_state != NFS_CS_READY)
5362 return status;
5364 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5365 msg.rpc_argp = session;
5366 msg.rpc_resp = NULL;
5367 msg.rpc_cred = NULL;
5368 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5370 if (status)
5371 printk(KERN_WARNING
5372 "Got error %d from the server on DESTROY_SESSION. "
5373 "Session has been destroyed regardless...\n", status);
5375 dprintk("<-- nfs4_proc_destroy_session\n");
5376 return status;
5379 int nfs4_init_session(struct nfs_server *server)
5381 struct nfs_client *clp = server->nfs_client;
5382 struct nfs4_session *session;
5383 unsigned int rsize, wsize;
5384 int ret;
5386 if (!nfs4_has_session(clp))
5387 return 0;
5389 session = clp->cl_session;
5390 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5391 return 0;
5393 rsize = server->rsize;
5394 if (rsize == 0)
5395 rsize = NFS_MAX_FILE_IO_SIZE;
5396 wsize = server->wsize;
5397 if (wsize == 0)
5398 wsize = NFS_MAX_FILE_IO_SIZE;
5400 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5401 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5403 ret = nfs4_recover_expired_lease(server);
5404 if (!ret)
5405 ret = nfs4_check_client_ready(clp);
5406 return ret;
5409 int nfs4_init_ds_session(struct nfs_client *clp)
5411 struct nfs4_session *session = clp->cl_session;
5412 int ret;
5414 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5415 return 0;
5417 ret = nfs4_client_recover_expired_lease(clp);
5418 if (!ret)
5419 /* Test for the DS role */
5420 if (!is_ds_client(clp))
5421 ret = -ENODEV;
5422 if (!ret)
5423 ret = nfs4_check_client_ready(clp);
5424 return ret;
5427 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5431 * Renew the cl_session lease.
5433 struct nfs4_sequence_data {
5434 struct nfs_client *clp;
5435 struct nfs4_sequence_args args;
5436 struct nfs4_sequence_res res;
5439 static void nfs41_sequence_release(void *data)
5441 struct nfs4_sequence_data *calldata = data;
5442 struct nfs_client *clp = calldata->clp;
5444 if (atomic_read(&clp->cl_count) > 1)
5445 nfs4_schedule_state_renewal(clp);
5446 nfs_put_client(clp);
5447 kfree(calldata);
5450 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5452 switch(task->tk_status) {
5453 case -NFS4ERR_DELAY:
5454 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5455 return -EAGAIN;
5456 default:
5457 nfs4_schedule_lease_recovery(clp);
5459 return 0;
5462 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5464 struct nfs4_sequence_data *calldata = data;
5465 struct nfs_client *clp = calldata->clp;
5467 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5468 return;
5470 if (task->tk_status < 0) {
5471 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5472 if (atomic_read(&clp->cl_count) == 1)
5473 goto out;
5475 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5476 rpc_restart_call_prepare(task);
5477 return;
5480 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5481 out:
5482 dprintk("<-- %s\n", __func__);
5485 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5487 struct nfs4_sequence_data *calldata = data;
5488 struct nfs_client *clp = calldata->clp;
5489 struct nfs4_sequence_args *args;
5490 struct nfs4_sequence_res *res;
5492 args = task->tk_msg.rpc_argp;
5493 res = task->tk_msg.rpc_resp;
5495 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5496 return;
5497 rpc_call_start(task);
5500 static const struct rpc_call_ops nfs41_sequence_ops = {
5501 .rpc_call_done = nfs41_sequence_call_done,
5502 .rpc_call_prepare = nfs41_sequence_prepare,
5503 .rpc_release = nfs41_sequence_release,
5506 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5508 struct nfs4_sequence_data *calldata;
5509 struct rpc_message msg = {
5510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5511 .rpc_cred = cred,
5513 struct rpc_task_setup task_setup_data = {
5514 .rpc_client = clp->cl_rpcclient,
5515 .rpc_message = &msg,
5516 .callback_ops = &nfs41_sequence_ops,
5517 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5520 if (!atomic_inc_not_zero(&clp->cl_count))
5521 return ERR_PTR(-EIO);
5522 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5523 if (calldata == NULL) {
5524 nfs_put_client(clp);
5525 return ERR_PTR(-ENOMEM);
5527 msg.rpc_argp = &calldata->args;
5528 msg.rpc_resp = &calldata->res;
5529 calldata->clp = clp;
5530 task_setup_data.callback_data = calldata;
5532 return rpc_run_task(&task_setup_data);
5535 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5537 struct rpc_task *task;
5538 int ret = 0;
5540 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5541 return 0;
5542 task = _nfs41_proc_sequence(clp, cred);
5543 if (IS_ERR(task))
5544 ret = PTR_ERR(task);
5545 else
5546 rpc_put_task_async(task);
5547 dprintk("<-- %s status=%d\n", __func__, ret);
5548 return ret;
5551 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5553 struct rpc_task *task;
5554 int ret;
5556 task = _nfs41_proc_sequence(clp, cred);
5557 if (IS_ERR(task)) {
5558 ret = PTR_ERR(task);
5559 goto out;
5561 ret = rpc_wait_for_completion_task(task);
5562 if (!ret) {
5563 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5565 if (task->tk_status == 0)
5566 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5567 ret = task->tk_status;
5569 rpc_put_task(task);
5570 out:
5571 dprintk("<-- %s status=%d\n", __func__, ret);
5572 return ret;
5575 struct nfs4_reclaim_complete_data {
5576 struct nfs_client *clp;
5577 struct nfs41_reclaim_complete_args arg;
5578 struct nfs41_reclaim_complete_res res;
5581 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5583 struct nfs4_reclaim_complete_data *calldata = data;
5585 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5586 if (nfs41_setup_sequence(calldata->clp->cl_session,
5587 &calldata->arg.seq_args,
5588 &calldata->res.seq_res, 0, task))
5589 return;
5591 rpc_call_start(task);
5594 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5596 switch(task->tk_status) {
5597 case 0:
5598 case -NFS4ERR_COMPLETE_ALREADY:
5599 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5600 break;
5601 case -NFS4ERR_DELAY:
5602 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5603 /* fall through */
5604 case -NFS4ERR_RETRY_UNCACHED_REP:
5605 return -EAGAIN;
5606 default:
5607 nfs4_schedule_lease_recovery(clp);
5609 return 0;
5612 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5614 struct nfs4_reclaim_complete_data *calldata = data;
5615 struct nfs_client *clp = calldata->clp;
5616 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5618 dprintk("--> %s\n", __func__);
5619 if (!nfs41_sequence_done(task, res))
5620 return;
5622 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5623 rpc_restart_call_prepare(task);
5624 return;
5626 dprintk("<-- %s\n", __func__);
5629 static void nfs4_free_reclaim_complete_data(void *data)
5631 struct nfs4_reclaim_complete_data *calldata = data;
5633 kfree(calldata);
5636 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5637 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5638 .rpc_call_done = nfs4_reclaim_complete_done,
5639 .rpc_release = nfs4_free_reclaim_complete_data,
5643 * Issue a global reclaim complete.
5645 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5647 struct nfs4_reclaim_complete_data *calldata;
5648 struct rpc_task *task;
5649 struct rpc_message msg = {
5650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5652 struct rpc_task_setup task_setup_data = {
5653 .rpc_client = clp->cl_rpcclient,
5654 .rpc_message = &msg,
5655 .callback_ops = &nfs4_reclaim_complete_call_ops,
5656 .flags = RPC_TASK_ASYNC,
5658 int status = -ENOMEM;
5660 dprintk("--> %s\n", __func__);
5661 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5662 if (calldata == NULL)
5663 goto out;
5664 calldata->clp = clp;
5665 calldata->arg.one_fs = 0;
5667 msg.rpc_argp = &calldata->arg;
5668 msg.rpc_resp = &calldata->res;
5669 task_setup_data.callback_data = calldata;
5670 task = rpc_run_task(&task_setup_data);
5671 if (IS_ERR(task)) {
5672 status = PTR_ERR(task);
5673 goto out;
5675 status = nfs4_wait_for_completion_rpc_task(task);
5676 if (status == 0)
5677 status = task->tk_status;
5678 rpc_put_task(task);
5679 return 0;
5680 out:
5681 dprintk("<-- %s status=%d\n", __func__, status);
5682 return status;
5685 static void
5686 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5688 struct nfs4_layoutget *lgp = calldata;
5689 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5691 dprintk("--> %s\n", __func__);
5692 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5693 * right now covering the LAYOUTGET we are about to send.
5694 * However, that is not so catastrophic, and there seems
5695 * to be no way to prevent it completely.
5697 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5698 &lgp->res.seq_res, 0, task))
5699 return;
5700 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5701 NFS_I(lgp->args.inode)->layout,
5702 lgp->args.ctx->state)) {
5703 rpc_exit(task, NFS4_OK);
5704 return;
5706 rpc_call_start(task);
5709 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5711 struct nfs4_layoutget *lgp = calldata;
5712 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5714 dprintk("--> %s\n", __func__);
5716 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5717 return;
5719 switch (task->tk_status) {
5720 case 0:
5721 break;
5722 case -NFS4ERR_LAYOUTTRYLATER:
5723 case -NFS4ERR_RECALLCONFLICT:
5724 task->tk_status = -NFS4ERR_DELAY;
5725 /* Fall through */
5726 default:
5727 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5728 rpc_restart_call_prepare(task);
5729 return;
5732 dprintk("<-- %s\n", __func__);
5735 static void nfs4_layoutget_release(void *calldata)
5737 struct nfs4_layoutget *lgp = calldata;
5739 dprintk("--> %s\n", __func__);
5740 put_nfs_open_context(lgp->args.ctx);
5741 kfree(calldata);
5742 dprintk("<-- %s\n", __func__);
5745 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5746 .rpc_call_prepare = nfs4_layoutget_prepare,
5747 .rpc_call_done = nfs4_layoutget_done,
5748 .rpc_release = nfs4_layoutget_release,
5751 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5753 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5754 struct rpc_task *task;
5755 struct rpc_message msg = {
5756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5757 .rpc_argp = &lgp->args,
5758 .rpc_resp = &lgp->res,
5760 struct rpc_task_setup task_setup_data = {
5761 .rpc_client = server->client,
5762 .rpc_message = &msg,
5763 .callback_ops = &nfs4_layoutget_call_ops,
5764 .callback_data = lgp,
5765 .flags = RPC_TASK_ASYNC,
5767 int status = 0;
5769 dprintk("--> %s\n", __func__);
5771 lgp->res.layoutp = &lgp->args.layout;
5772 lgp->res.seq_res.sr_slot = NULL;
5773 task = rpc_run_task(&task_setup_data);
5774 if (IS_ERR(task))
5775 return PTR_ERR(task);
5776 status = nfs4_wait_for_completion_rpc_task(task);
5777 if (status == 0)
5778 status = task->tk_status;
5779 if (status == 0)
5780 status = pnfs_layout_process(lgp);
5781 rpc_put_task(task);
5782 dprintk("<-- %s status=%d\n", __func__, status);
5783 return status;
5786 static void
5787 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5789 struct nfs4_layoutreturn *lrp = calldata;
5791 dprintk("--> %s\n", __func__);
5792 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5793 &lrp->res.seq_res, 0, task))
5794 return;
5795 rpc_call_start(task);
5798 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5800 struct nfs4_layoutreturn *lrp = calldata;
5801 struct nfs_server *server;
5802 struct pnfs_layout_hdr *lo = lrp->args.layout;
5804 dprintk("--> %s\n", __func__);
5806 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5807 return;
5809 server = NFS_SERVER(lrp->args.inode);
5810 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5811 rpc_restart_call_prepare(task);
5812 return;
5814 spin_lock(&lo->plh_inode->i_lock);
5815 if (task->tk_status == 0) {
5816 if (lrp->res.lrs_present) {
5817 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5818 } else
5819 BUG_ON(!list_empty(&lo->plh_segs));
5821 lo->plh_block_lgets--;
5822 spin_unlock(&lo->plh_inode->i_lock);
5823 dprintk("<-- %s\n", __func__);
5826 static void nfs4_layoutreturn_release(void *calldata)
5828 struct nfs4_layoutreturn *lrp = calldata;
5830 dprintk("--> %s\n", __func__);
5831 put_layout_hdr(lrp->args.layout);
5832 kfree(calldata);
5833 dprintk("<-- %s\n", __func__);
5836 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5837 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5838 .rpc_call_done = nfs4_layoutreturn_done,
5839 .rpc_release = nfs4_layoutreturn_release,
5842 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5844 struct rpc_task *task;
5845 struct rpc_message msg = {
5846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5847 .rpc_argp = &lrp->args,
5848 .rpc_resp = &lrp->res,
5850 struct rpc_task_setup task_setup_data = {
5851 .rpc_client = lrp->clp->cl_rpcclient,
5852 .rpc_message = &msg,
5853 .callback_ops = &nfs4_layoutreturn_call_ops,
5854 .callback_data = lrp,
5856 int status;
5858 dprintk("--> %s\n", __func__);
5859 task = rpc_run_task(&task_setup_data);
5860 if (IS_ERR(task))
5861 return PTR_ERR(task);
5862 status = task->tk_status;
5863 dprintk("<-- %s status=%d\n", __func__, status);
5864 rpc_put_task(task);
5865 return status;
5869 * Retrieve the list of Data Server devices from the MDS.
5871 static int _nfs4_getdevicelist(struct nfs_server *server,
5872 const struct nfs_fh *fh,
5873 struct pnfs_devicelist *devlist)
5875 struct nfs4_getdevicelist_args args = {
5876 .fh = fh,
5877 .layoutclass = server->pnfs_curr_ld->id,
5879 struct nfs4_getdevicelist_res res = {
5880 .devlist = devlist,
5882 struct rpc_message msg = {
5883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5884 .rpc_argp = &args,
5885 .rpc_resp = &res,
5887 int status;
5889 dprintk("--> %s\n", __func__);
5890 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5891 &res.seq_res, 0);
5892 dprintk("<-- %s status=%d\n", __func__, status);
5893 return status;
5896 int nfs4_proc_getdevicelist(struct nfs_server *server,
5897 const struct nfs_fh *fh,
5898 struct pnfs_devicelist *devlist)
5900 struct nfs4_exception exception = { };
5901 int err;
5903 do {
5904 err = nfs4_handle_exception(server,
5905 _nfs4_getdevicelist(server, fh, devlist),
5906 &exception);
5907 } while (exception.retry);
5909 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5910 err, devlist->num_devs);
5912 return err;
5914 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5916 static int
5917 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5919 struct nfs4_getdeviceinfo_args args = {
5920 .pdev = pdev,
5922 struct nfs4_getdeviceinfo_res res = {
5923 .pdev = pdev,
5925 struct rpc_message msg = {
5926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5927 .rpc_argp = &args,
5928 .rpc_resp = &res,
5930 int status;
5932 dprintk("--> %s\n", __func__);
5933 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5934 dprintk("<-- %s status=%d\n", __func__, status);
5936 return status;
5939 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5941 struct nfs4_exception exception = { };
5942 int err;
5944 do {
5945 err = nfs4_handle_exception(server,
5946 _nfs4_proc_getdeviceinfo(server, pdev),
5947 &exception);
5948 } while (exception.retry);
5949 return err;
5951 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5953 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5955 struct nfs4_layoutcommit_data *data = calldata;
5956 struct nfs_server *server = NFS_SERVER(data->args.inode);
5958 if (nfs4_setup_sequence(server, &data->args.seq_args,
5959 &data->res.seq_res, 1, task))
5960 return;
5961 rpc_call_start(task);
5964 static void
5965 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5967 struct nfs4_layoutcommit_data *data = calldata;
5968 struct nfs_server *server = NFS_SERVER(data->args.inode);
5970 if (!nfs4_sequence_done(task, &data->res.seq_res))
5971 return;
5973 switch (task->tk_status) { /* Just ignore these failures */
5974 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5975 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5976 case -NFS4ERR_BADLAYOUT: /* no layout */
5977 case -NFS4ERR_GRACE: /* loca_recalim always false */
5978 task->tk_status = 0;
5979 break;
5980 case 0:
5981 nfs_post_op_update_inode_force_wcc(data->args.inode,
5982 data->res.fattr);
5983 break;
5984 default:
5985 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5986 rpc_restart_call_prepare(task);
5987 return;
5992 static void nfs4_layoutcommit_release(void *calldata)
5994 struct nfs4_layoutcommit_data *data = calldata;
5995 struct pnfs_layout_segment *lseg, *tmp;
5996 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
5998 pnfs_cleanup_layoutcommit(data);
5999 /* Matched by references in pnfs_set_layoutcommit */
6000 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6001 list_del_init(&lseg->pls_lc_list);
6002 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6003 &lseg->pls_flags))
6004 put_lseg(lseg);
6007 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6008 smp_mb__after_clear_bit();
6009 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6011 put_rpccred(data->cred);
6012 kfree(data);
6015 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6016 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6017 .rpc_call_done = nfs4_layoutcommit_done,
6018 .rpc_release = nfs4_layoutcommit_release,
6022 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6024 struct rpc_message msg = {
6025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6026 .rpc_argp = &data->args,
6027 .rpc_resp = &data->res,
6028 .rpc_cred = data->cred,
6030 struct rpc_task_setup task_setup_data = {
6031 .task = &data->task,
6032 .rpc_client = NFS_CLIENT(data->args.inode),
6033 .rpc_message = &msg,
6034 .callback_ops = &nfs4_layoutcommit_ops,
6035 .callback_data = data,
6036 .flags = RPC_TASK_ASYNC,
6038 struct rpc_task *task;
6039 int status = 0;
6041 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6042 "lbw: %llu inode %lu\n",
6043 data->task.tk_pid, sync,
6044 data->args.lastbytewritten,
6045 data->args.inode->i_ino);
6047 task = rpc_run_task(&task_setup_data);
6048 if (IS_ERR(task))
6049 return PTR_ERR(task);
6050 if (sync == false)
6051 goto out;
6052 status = nfs4_wait_for_completion_rpc_task(task);
6053 if (status != 0)
6054 goto out;
6055 status = task->tk_status;
6056 out:
6057 dprintk("%s: status %d\n", __func__, status);
6058 rpc_put_task(task);
6059 return status;
6062 static int
6063 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6064 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6066 struct nfs41_secinfo_no_name_args args = {
6067 .style = SECINFO_STYLE_CURRENT_FH,
6069 struct nfs4_secinfo_res res = {
6070 .flavors = flavors,
6072 struct rpc_message msg = {
6073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6074 .rpc_argp = &args,
6075 .rpc_resp = &res,
6077 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6080 static int
6081 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6082 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6084 struct nfs4_exception exception = { };
6085 int err;
6086 do {
6087 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6088 switch (err) {
6089 case 0:
6090 case -NFS4ERR_WRONGSEC:
6091 case -NFS4ERR_NOTSUPP:
6092 goto out;
6093 default:
6094 err = nfs4_handle_exception(server, err, &exception);
6096 } while (exception.retry);
6097 out:
6098 return err;
6101 static int
6102 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6103 struct nfs_fsinfo *info)
6105 int err;
6106 struct page *page;
6107 rpc_authflavor_t flavor;
6108 struct nfs4_secinfo_flavors *flavors;
6110 page = alloc_page(GFP_KERNEL);
6111 if (!page) {
6112 err = -ENOMEM;
6113 goto out;
6116 flavors = page_address(page);
6117 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6120 * Fall back on "guess and check" method if
6121 * the server doesn't support SECINFO_NO_NAME
6123 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6124 err = nfs4_find_root_sec(server, fhandle, info);
6125 goto out_freepage;
6127 if (err)
6128 goto out_freepage;
6130 flavor = nfs_find_best_sec(flavors);
6131 if (err == 0)
6132 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6134 out_freepage:
6135 put_page(page);
6136 if (err == -EACCES)
6137 return -EPERM;
6138 out:
6139 return err;
6141 static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6143 int status;
6144 struct nfs41_test_stateid_args args = {
6145 .stateid = &state->stateid,
6147 struct nfs41_test_stateid_res res;
6148 struct rpc_message msg = {
6149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6150 .rpc_argp = &args,
6151 .rpc_resp = &res,
6153 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6154 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6155 return status;
6158 static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6160 struct nfs4_exception exception = { };
6161 int err;
6162 do {
6163 err = nfs4_handle_exception(server,
6164 _nfs41_test_stateid(server, state),
6165 &exception);
6166 } while (exception.retry);
6167 return err;
6170 static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6172 int status;
6173 struct nfs41_free_stateid_args args = {
6174 .stateid = &state->stateid,
6176 struct nfs41_free_stateid_res res;
6177 struct rpc_message msg = {
6178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6179 .rpc_argp = &args,
6180 .rpc_resp = &res,
6183 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6184 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6185 return status;
6188 static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6190 struct nfs4_exception exception = { };
6191 int err;
6192 do {
6193 err = nfs4_handle_exception(server,
6194 _nfs4_free_stateid(server, state),
6195 &exception);
6196 } while (exception.retry);
6197 return err;
6199 #endif /* CONFIG_NFS_V4_1 */
6201 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6202 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6203 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6204 .recover_open = nfs4_open_reclaim,
6205 .recover_lock = nfs4_lock_reclaim,
6206 .establish_clid = nfs4_init_clientid,
6207 .get_clid_cred = nfs4_get_setclientid_cred,
6210 #if defined(CONFIG_NFS_V4_1)
6211 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6212 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6213 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6214 .recover_open = nfs4_open_reclaim,
6215 .recover_lock = nfs4_lock_reclaim,
6216 .establish_clid = nfs41_init_clientid,
6217 .get_clid_cred = nfs4_get_exchange_id_cred,
6218 .reclaim_complete = nfs41_proc_reclaim_complete,
6220 #endif /* CONFIG_NFS_V4_1 */
6222 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6223 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6224 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6225 .recover_open = nfs4_open_expired,
6226 .recover_lock = nfs4_lock_expired,
6227 .establish_clid = nfs4_init_clientid,
6228 .get_clid_cred = nfs4_get_setclientid_cred,
6231 #if defined(CONFIG_NFS_V4_1)
6232 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6233 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6234 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6235 .recover_open = nfs41_open_expired,
6236 .recover_lock = nfs41_lock_expired,
6237 .establish_clid = nfs41_init_clientid,
6238 .get_clid_cred = nfs4_get_exchange_id_cred,
6240 #endif /* CONFIG_NFS_V4_1 */
6242 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6243 .sched_state_renewal = nfs4_proc_async_renew,
6244 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6245 .renew_lease = nfs4_proc_renew,
6248 #if defined(CONFIG_NFS_V4_1)
6249 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6250 .sched_state_renewal = nfs41_proc_async_sequence,
6251 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6252 .renew_lease = nfs4_proc_sequence,
6254 #endif
6256 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6257 .minor_version = 0,
6258 .call_sync = _nfs4_call_sync,
6259 .validate_stateid = nfs4_validate_delegation_stateid,
6260 .find_root_sec = nfs4_find_root_sec,
6261 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6262 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6263 .state_renewal_ops = &nfs40_state_renewal_ops,
6266 #if defined(CONFIG_NFS_V4_1)
6267 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6268 .minor_version = 1,
6269 .call_sync = _nfs4_call_sync_session,
6270 .validate_stateid = nfs41_validate_delegation_stateid,
6271 .find_root_sec = nfs41_find_root_sec,
6272 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6273 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6274 .state_renewal_ops = &nfs41_state_renewal_ops,
6276 #endif
6278 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6279 [0] = &nfs_v4_0_minor_ops,
6280 #if defined(CONFIG_NFS_V4_1)
6281 [1] = &nfs_v4_1_minor_ops,
6282 #endif
6285 static const struct inode_operations nfs4_file_inode_operations = {
6286 .permission = nfs_permission,
6287 .getattr = nfs_getattr,
6288 .setattr = nfs_setattr,
6289 .getxattr = generic_getxattr,
6290 .setxattr = generic_setxattr,
6291 .listxattr = generic_listxattr,
6292 .removexattr = generic_removexattr,
6295 const struct nfs_rpc_ops nfs_v4_clientops = {
6296 .version = 4, /* protocol version */
6297 .dentry_ops = &nfs4_dentry_operations,
6298 .dir_inode_ops = &nfs4_dir_inode_operations,
6299 .file_inode_ops = &nfs4_file_inode_operations,
6300 .file_ops = &nfs4_file_operations,
6301 .getroot = nfs4_proc_get_root,
6302 .getattr = nfs4_proc_getattr,
6303 .setattr = nfs4_proc_setattr,
6304 .lookup = nfs4_proc_lookup,
6305 .access = nfs4_proc_access,
6306 .readlink = nfs4_proc_readlink,
6307 .create = nfs4_proc_create,
6308 .remove = nfs4_proc_remove,
6309 .unlink_setup = nfs4_proc_unlink_setup,
6310 .unlink_done = nfs4_proc_unlink_done,
6311 .rename = nfs4_proc_rename,
6312 .rename_setup = nfs4_proc_rename_setup,
6313 .rename_done = nfs4_proc_rename_done,
6314 .link = nfs4_proc_link,
6315 .symlink = nfs4_proc_symlink,
6316 .mkdir = nfs4_proc_mkdir,
6317 .rmdir = nfs4_proc_remove,
6318 .readdir = nfs4_proc_readdir,
6319 .mknod = nfs4_proc_mknod,
6320 .statfs = nfs4_proc_statfs,
6321 .fsinfo = nfs4_proc_fsinfo,
6322 .pathconf = nfs4_proc_pathconf,
6323 .set_capabilities = nfs4_server_capabilities,
6324 .decode_dirent = nfs4_decode_dirent,
6325 .read_setup = nfs4_proc_read_setup,
6326 .read_done = nfs4_read_done,
6327 .write_setup = nfs4_proc_write_setup,
6328 .write_done = nfs4_write_done,
6329 .commit_setup = nfs4_proc_commit_setup,
6330 .commit_done = nfs4_commit_done,
6331 .lock = nfs4_proc_lock,
6332 .clear_acl_cache = nfs4_zap_acl_attr,
6333 .close_context = nfs4_close_context,
6334 .open_context = nfs4_atomic_open,
6335 .init_client = nfs4_init_client,
6336 .secinfo = nfs4_proc_secinfo,
6339 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6340 .prefix = XATTR_NAME_NFSV4_ACL,
6341 .list = nfs4_xattr_list_nfs4_acl,
6342 .get = nfs4_xattr_get_nfs4_acl,
6343 .set = nfs4_xattr_set_nfs4_acl,
6346 const struct xattr_handler *nfs4_xattr_handlers[] = {
6347 &nfs4_xattr_nfs4_acl_handler,
6348 NULL
6352 * Local variables:
6353 * c-basic-offset: 8
6354 * End: