Linux 3.8-rc7
[cris-mirror.git] / fs / nfs / nfs4proc.c
blobcf747ef8665078a2a684b256402ab63484a77665
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/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
65 #include "netns.h"
66 #include "nfs4session.h"
67 #include "fscache.h"
69 #define NFSDBG_FACILITY NFSDBG_PROC
71 #define NFS4_POLL_RETRY_MIN (HZ/10)
72 #define NFS4_POLL_RETRY_MAX (15*HZ)
74 struct nfs4_opendata;
75 static int _nfs4_proc_open(struct nfs4_opendata *data);
76 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
77 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
78 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
79 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
80 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
81 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
82 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
85 #ifdef CONFIG_NFS_V4_1
86 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
88 #endif
89 /* Prevent leaks of NFSv4 errors into userland */
90 static int nfs4_map_errors(int err)
92 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
97 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
99 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
106 case -NFS4ERR_ACCESS:
107 return -EACCES;
108 default:
109 dprintk("%s could not handle NFSv4 error %d\n",
110 __func__, -err);
111 break;
113 return -EIO;
117 * This is our standard bitmap for GETATTR requests.
119 const u32 nfs4_fattr_bitmap[3] = {
120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
136 static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
154 static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
160 const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
169 const u32 nfs4_pathconf_bitmap[2] = {
170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
175 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
179 FATTR4_WORD1_TIME_DELTA
180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
184 const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
203 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
204 struct nfs4_readdir_arg *readdir)
206 __be32 *start, *p;
208 if (cookie > 2) {
209 readdir->cookie = cookie;
210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
226 start = p = kmap_atomic(*readdir->pages);
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
254 kunmap_atomic(start);
257 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
259 int res = 0;
261 might_sleep();
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
267 freezable_schedule_timeout_killable(*timeout);
268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
274 /* This is the error handling routine for processes that are allowed
275 * to sleep.
277 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
279 struct nfs_client *clp = server->nfs_client;
280 struct nfs4_state *state = exception->state;
281 struct inode *inode = exception->inode;
282 int ret = errorcode;
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
288 case -NFS4ERR_OPENMODE:
289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
290 nfs4_inode_return_delegation(inode);
291 exception->retry = 1;
292 return 0;
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
298 case -NFS4ERR_DELEG_REVOKED:
299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
301 if (state == NULL)
302 break;
303 nfs_remove_bad_delegation(state->inode);
304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
309 case -NFS4ERR_STALE_STATEID:
310 case -NFS4ERR_STALE_CLIENTID:
311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
313 #if defined(CONFIG_NFS_V4_1)
314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
324 goto wait_on_recovery;
325 #endif /* defined(CONFIG_NFS_V4_1) */
326 case -NFS4ERR_FILE_OPEN:
327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
331 ret = -EBUSY;
332 break;
334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
336 ret = nfs4_delay(server->client, &exception->timeout);
337 if (ret != 0)
338 break;
339 case -NFS4ERR_RETRY_UNCACHED_REP:
340 case -NFS4ERR_OLD_STATEID:
341 exception->retry = 1;
342 break;
343 case -NFS4ERR_BADOWNER:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME:
346 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 exception->retry = 1;
349 printk(KERN_WARNING "NFS: v4 server %s "
350 "does not accept raw "
351 "uid/gids. "
352 "Reenabling the idmapper.\n",
353 server->nfs_client->cl_hostname);
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret);
358 wait_on_recovery:
359 ret = nfs4_wait_clnt_recover(clp);
360 if (ret == 0)
361 exception->retry = 1;
362 return ret;
366 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
368 spin_lock(&clp->cl_lock);
369 if (time_before(clp->cl_last_renewal,timestamp))
370 clp->cl_last_renewal = timestamp;
371 spin_unlock(&clp->cl_lock);
374 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376 do_renew_lease(server->nfs_client, timestamp);
379 #if defined(CONFIG_NFS_V4_1)
381 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
383 struct nfs4_session *session;
384 struct nfs4_slot_table *tbl;
385 bool send_new_highest_used_slotid = false;
387 if (!res->sr_slot) {
388 /* just wake up the next guy waiting since
389 * we may have not consumed a slot after all */
390 dprintk("%s: No slot\n", __func__);
391 return;
393 tbl = res->sr_slot->table;
394 session = tbl->session;
396 spin_lock(&tbl->slot_tbl_lock);
397 /* Be nice to the server: try to ensure that the last transmitted
398 * value for highest_user_slotid <= target_highest_slotid
400 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
401 send_new_highest_used_slotid = true;
403 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
404 send_new_highest_used_slotid = false;
405 goto out_unlock;
407 nfs4_free_slot(tbl, res->sr_slot);
409 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
410 send_new_highest_used_slotid = false;
411 out_unlock:
412 spin_unlock(&tbl->slot_tbl_lock);
413 res->sr_slot = NULL;
414 if (send_new_highest_used_slotid)
415 nfs41_server_notify_highest_slotid_update(session->clp);
418 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
420 struct nfs4_session *session;
421 struct nfs4_slot *slot;
422 struct nfs_client *clp;
423 bool interrupted = false;
424 int ret = 1;
426 /* don't increment the sequence number if the task wasn't sent */
427 if (!RPC_WAS_SENT(task))
428 goto out;
430 slot = res->sr_slot;
431 session = slot->table->session;
433 if (slot->interrupted) {
434 slot->interrupted = 0;
435 interrupted = true;
438 /* Check the SEQUENCE operation status */
439 switch (res->sr_status) {
440 case 0:
441 /* Update the slot's sequence and clientid lease timer */
442 ++slot->seq_nr;
443 clp = session->clp;
444 do_renew_lease(clp, res->sr_timestamp);
445 /* Check sequence flags */
446 if (res->sr_status_flags != 0)
447 nfs4_schedule_lease_recovery(clp);
448 nfs41_update_target_slotid(slot->table, slot, res);
449 break;
450 case 1:
452 * sr_status remains 1 if an RPC level error occurred.
453 * The server may or may not have processed the sequence
454 * operation..
455 * Mark the slot as having hosted an interrupted RPC call.
457 slot->interrupted = 1;
458 goto out;
459 case -NFS4ERR_DELAY:
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
462 * of RFC5661.
464 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
465 __func__,
466 slot->slot_nr,
467 slot->seq_nr);
468 goto out_retry;
469 case -NFS4ERR_BADSLOT:
471 * The slot id we used was probably retired. Try again
472 * using a different slot id.
474 goto retry_nowait;
475 case -NFS4ERR_SEQ_MISORDERED:
477 * Was the last operation on this sequence interrupted?
478 * If so, retry after bumping the sequence number.
480 if (interrupted) {
481 ++slot->seq_nr;
482 goto retry_nowait;
485 * Could this slot have been previously retired?
486 * If so, then the server may be expecting seq_nr = 1!
488 if (slot->seq_nr != 1) {
489 slot->seq_nr = 1;
490 goto retry_nowait;
492 break;
493 case -NFS4ERR_SEQ_FALSE_RETRY:
494 ++slot->seq_nr;
495 goto retry_nowait;
496 default:
497 /* Just update the slot sequence no. */
498 ++slot->seq_nr;
500 out:
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
503 nfs41_sequence_free_slot(res);
504 return ret;
505 retry_nowait:
506 if (rpc_restart_call_prepare(task)) {
507 task->tk_status = 0;
508 ret = 0;
510 goto out;
511 out_retry:
512 if (!rpc_restart_call(task))
513 goto out;
514 rpc_delay(task, NFS4_POLL_RETRY_MAX);
515 return 0;
518 static int nfs4_sequence_done(struct rpc_task *task,
519 struct nfs4_sequence_res *res)
521 if (res->sr_slot == NULL)
522 return 1;
523 return nfs41_sequence_done(task, res);
526 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
527 struct nfs4_sequence_res *res, int cache_reply)
529 args->sa_slot = NULL;
530 args->sa_cache_this = 0;
531 args->sa_privileged = 0;
532 if (cache_reply)
533 args->sa_cache_this = 1;
534 res->sr_slot = NULL;
537 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
539 args->sa_privileged = 1;
542 int nfs41_setup_sequence(struct nfs4_session *session,
543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
545 struct rpc_task *task)
547 struct nfs4_slot *slot;
548 struct nfs4_slot_table *tbl;
550 dprintk("--> %s\n", __func__);
551 /* slot already allocated? */
552 if (res->sr_slot != NULL)
553 goto out_success;
555 tbl = &session->fc_slot_table;
557 task->tk_timeout = 0;
559 spin_lock(&tbl->slot_tbl_lock);
560 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
561 !args->sa_privileged) {
562 /* The state manager will wait until the slot table is empty */
563 dprintk("%s session is draining\n", __func__);
564 goto out_sleep;
567 slot = nfs4_alloc_slot(tbl);
568 if (IS_ERR(slot)) {
569 /* If out of memory, try again in 1/4 second */
570 if (slot == ERR_PTR(-ENOMEM))
571 task->tk_timeout = HZ >> 2;
572 dprintk("<-- %s: no free slots\n", __func__);
573 goto out_sleep;
575 spin_unlock(&tbl->slot_tbl_lock);
577 args->sa_slot = slot;
579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
580 slot->slot_nr, slot->seq_nr);
582 res->sr_slot = slot;
583 res->sr_timestamp = jiffies;
584 res->sr_status_flags = 0;
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
589 res->sr_status = 1;
590 out_success:
591 rpc_call_start(task);
592 return 0;
593 out_sleep:
594 /* Privileged tasks are queued with top priority */
595 if (args->sa_privileged)
596 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
597 NULL, RPC_PRIORITY_PRIVILEGED);
598 else
599 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
600 spin_unlock(&tbl->slot_tbl_lock);
601 return -EAGAIN;
603 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
605 int nfs4_setup_sequence(const struct nfs_server *server,
606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
608 struct rpc_task *task)
610 struct nfs4_session *session = nfs4_get_session(server);
611 int ret = 0;
613 if (session == NULL) {
614 rpc_call_start(task);
615 goto out;
618 dprintk("--> %s clp %p session %p sr_slot %d\n",
619 __func__, session->clp, session, res->sr_slot ?
620 res->sr_slot->slot_nr : -1);
622 ret = nfs41_setup_sequence(session, args, res, task);
623 out:
624 dprintk("<-- %s status=%d\n", __func__, ret);
625 return ret;
628 struct nfs41_call_sync_data {
629 const struct nfs_server *seq_server;
630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
634 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
636 struct nfs41_call_sync_data *data = calldata;
637 struct nfs4_session *session = nfs4_get_session(data->seq_server);
639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
641 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
644 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
646 struct nfs41_call_sync_data *data = calldata;
648 nfs41_sequence_done(task, data->seq_res);
651 static const struct rpc_call_ops nfs41_call_sync_ops = {
652 .rpc_call_prepare = nfs41_call_sync_prepare,
653 .rpc_call_done = nfs41_call_sync_done,
656 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
657 struct nfs_server *server,
658 struct rpc_message *msg,
659 struct nfs4_sequence_args *args,
660 struct nfs4_sequence_res *res)
662 int ret;
663 struct rpc_task *task;
664 struct nfs41_call_sync_data data = {
665 .seq_server = server,
666 .seq_args = args,
667 .seq_res = res,
669 struct rpc_task_setup task_setup = {
670 .rpc_client = clnt,
671 .rpc_message = msg,
672 .callback_ops = &nfs41_call_sync_ops,
673 .callback_data = &data
676 task = rpc_run_task(&task_setup);
677 if (IS_ERR(task))
678 ret = PTR_ERR(task);
679 else {
680 ret = task->tk_status;
681 rpc_put_task(task);
683 return ret;
686 #else
687 static
688 void nfs41_init_sequence(struct nfs4_sequence_args *args,
689 struct nfs4_sequence_res *res, int cache_reply)
693 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
698 static int nfs4_sequence_done(struct rpc_task *task,
699 struct nfs4_sequence_res *res)
701 return 1;
703 #endif /* CONFIG_NFS_V4_1 */
705 static
706 int _nfs4_call_sync(struct rpc_clnt *clnt,
707 struct nfs_server *server,
708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
710 struct nfs4_sequence_res *res)
712 return rpc_call_sync(clnt, msg, 0);
715 static
716 int nfs4_call_sync(struct rpc_clnt *clnt,
717 struct nfs_server *server,
718 struct rpc_message *msg,
719 struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res,
721 int cache_reply)
723 nfs41_init_sequence(args, res, cache_reply);
724 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
725 args, res);
728 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
730 struct nfs_inode *nfsi = NFS_I(dir);
732 spin_lock(&dir->i_lock);
733 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
734 if (!cinfo->atomic || cinfo->before != dir->i_version)
735 nfs_force_lookup_revalidate(dir);
736 dir->i_version = cinfo->after;
737 nfs_fscache_invalidate(dir);
738 spin_unlock(&dir->i_lock);
741 struct nfs4_opendata {
742 struct kref kref;
743 struct nfs_openargs o_arg;
744 struct nfs_openres o_res;
745 struct nfs_open_confirmargs c_arg;
746 struct nfs_open_confirmres c_res;
747 struct nfs4_string owner_name;
748 struct nfs4_string group_name;
749 struct nfs_fattr f_attr;
750 struct dentry *dir;
751 struct dentry *dentry;
752 struct nfs4_state_owner *owner;
753 struct nfs4_state *state;
754 struct iattr attrs;
755 unsigned long timestamp;
756 unsigned int rpc_done : 1;
757 int rpc_status;
758 int cancelled;
762 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
764 p->o_res.f_attr = &p->f_attr;
765 p->o_res.seqid = p->o_arg.seqid;
766 p->c_res.seqid = p->c_arg.seqid;
767 p->o_res.server = p->o_arg.server;
768 p->o_res.access_request = p->o_arg.access;
769 nfs_fattr_init(&p->f_attr);
770 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
773 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
774 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
775 const struct iattr *attrs,
776 gfp_t gfp_mask)
778 struct dentry *parent = dget_parent(dentry);
779 struct inode *dir = parent->d_inode;
780 struct nfs_server *server = NFS_SERVER(dir);
781 struct nfs4_opendata *p;
783 p = kzalloc(sizeof(*p), gfp_mask);
784 if (p == NULL)
785 goto err;
786 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
787 if (p->o_arg.seqid == NULL)
788 goto err_free;
789 nfs_sb_active(dentry->d_sb);
790 p->dentry = dget(dentry);
791 p->dir = parent;
792 p->owner = sp;
793 atomic_inc(&sp->so_count);
794 p->o_arg.fh = NFS_FH(dir);
795 p->o_arg.open_flags = flags;
796 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
797 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
798 * will return permission denied for all bits until close */
799 if (!(flags & O_EXCL)) {
800 /* ask server to check for all possible rights as results
801 * are cached */
802 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
803 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
805 p->o_arg.clientid = server->nfs_client->cl_clientid;
806 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
807 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
808 p->o_arg.name = &dentry->d_name;
809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
811 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
812 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
813 if (attrs != NULL && attrs->ia_valid != 0) {
814 __be32 verf[2];
816 p->o_arg.u.attrs = &p->attrs;
817 memcpy(&p->attrs, attrs, sizeof(p->attrs));
819 verf[0] = jiffies;
820 verf[1] = current->pid;
821 memcpy(p->o_arg.u.verifier.data, verf,
822 sizeof(p->o_arg.u.verifier.data));
824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
827 nfs4_init_opendata_res(p);
828 kref_init(&p->kref);
829 return p;
830 err_free:
831 kfree(p);
832 err:
833 dput(parent);
834 return NULL;
837 static void nfs4_opendata_free(struct kref *kref)
839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
841 struct super_block *sb = p->dentry->d_sb;
843 nfs_free_seqid(p->o_arg.seqid);
844 if (p->state != NULL)
845 nfs4_put_open_state(p->state);
846 nfs4_put_state_owner(p->owner);
847 dput(p->dir);
848 dput(p->dentry);
849 nfs_sb_deactive(sb);
850 nfs_fattr_free_names(&p->f_attr);
851 kfree(p);
854 static void nfs4_opendata_put(struct nfs4_opendata *p)
856 if (p != NULL)
857 kref_put(&p->kref, nfs4_opendata_free);
860 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
862 int ret;
864 ret = rpc_wait_for_completion_task(task);
865 return ret;
868 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
870 int ret = 0;
872 if (open_mode & (O_EXCL|O_TRUNC))
873 goto out;
874 switch (mode & (FMODE_READ|FMODE_WRITE)) {
875 case FMODE_READ:
876 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
877 && state->n_rdonly != 0;
878 break;
879 case FMODE_WRITE:
880 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
881 && state->n_wronly != 0;
882 break;
883 case FMODE_READ|FMODE_WRITE:
884 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
885 && state->n_rdwr != 0;
887 out:
888 return ret;
891 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
893 if (delegation == NULL)
894 return 0;
895 if ((delegation->type & fmode) != fmode)
896 return 0;
897 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
898 return 0;
899 nfs_mark_delegation_referenced(delegation);
900 return 1;
903 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
905 switch (fmode) {
906 case FMODE_WRITE:
907 state->n_wronly++;
908 break;
909 case FMODE_READ:
910 state->n_rdonly++;
911 break;
912 case FMODE_READ|FMODE_WRITE:
913 state->n_rdwr++;
915 nfs4_state_set_mode_locked(state, state->state | fmode);
918 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
920 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
921 nfs4_stateid_copy(&state->stateid, stateid);
922 nfs4_stateid_copy(&state->open_stateid, stateid);
923 switch (fmode) {
924 case FMODE_READ:
925 set_bit(NFS_O_RDONLY_STATE, &state->flags);
926 break;
927 case FMODE_WRITE:
928 set_bit(NFS_O_WRONLY_STATE, &state->flags);
929 break;
930 case FMODE_READ|FMODE_WRITE:
931 set_bit(NFS_O_RDWR_STATE, &state->flags);
935 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
937 write_seqlock(&state->seqlock);
938 nfs_set_open_stateid_locked(state, stateid, fmode);
939 write_sequnlock(&state->seqlock);
942 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
945 * Protect the call to nfs4_state_set_mode_locked and
946 * serialise the stateid update
948 write_seqlock(&state->seqlock);
949 if (deleg_stateid != NULL) {
950 nfs4_stateid_copy(&state->stateid, deleg_stateid);
951 set_bit(NFS_DELEGATED_STATE, &state->flags);
953 if (open_stateid != NULL)
954 nfs_set_open_stateid_locked(state, open_stateid, fmode);
955 write_sequnlock(&state->seqlock);
956 spin_lock(&state->owner->so_lock);
957 update_open_stateflags(state, fmode);
958 spin_unlock(&state->owner->so_lock);
961 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
963 struct nfs_inode *nfsi = NFS_I(state->inode);
964 struct nfs_delegation *deleg_cur;
965 int ret = 0;
967 fmode &= (FMODE_READ|FMODE_WRITE);
969 rcu_read_lock();
970 deleg_cur = rcu_dereference(nfsi->delegation);
971 if (deleg_cur == NULL)
972 goto no_delegation;
974 spin_lock(&deleg_cur->lock);
975 if (nfsi->delegation != deleg_cur ||
976 (deleg_cur->type & fmode) != fmode)
977 goto no_delegation_unlock;
979 if (delegation == NULL)
980 delegation = &deleg_cur->stateid;
981 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
982 goto no_delegation_unlock;
984 nfs_mark_delegation_referenced(deleg_cur);
985 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
986 ret = 1;
987 no_delegation_unlock:
988 spin_unlock(&deleg_cur->lock);
989 no_delegation:
990 rcu_read_unlock();
992 if (!ret && open_stateid != NULL) {
993 __update_open_stateid(state, open_stateid, NULL, fmode);
994 ret = 1;
997 return ret;
1001 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1003 struct nfs_delegation *delegation;
1005 rcu_read_lock();
1006 delegation = rcu_dereference(NFS_I(inode)->delegation);
1007 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1008 rcu_read_unlock();
1009 return;
1011 rcu_read_unlock();
1012 nfs4_inode_return_delegation(inode);
1015 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1017 struct nfs4_state *state = opendata->state;
1018 struct nfs_inode *nfsi = NFS_I(state->inode);
1019 struct nfs_delegation *delegation;
1020 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1021 fmode_t fmode = opendata->o_arg.fmode;
1022 nfs4_stateid stateid;
1023 int ret = -EAGAIN;
1025 for (;;) {
1026 if (can_open_cached(state, fmode, open_mode)) {
1027 spin_lock(&state->owner->so_lock);
1028 if (can_open_cached(state, fmode, open_mode)) {
1029 update_open_stateflags(state, fmode);
1030 spin_unlock(&state->owner->so_lock);
1031 goto out_return_state;
1033 spin_unlock(&state->owner->so_lock);
1035 rcu_read_lock();
1036 delegation = rcu_dereference(nfsi->delegation);
1037 if (!can_open_delegated(delegation, fmode)) {
1038 rcu_read_unlock();
1039 break;
1041 /* Save the delegation */
1042 nfs4_stateid_copy(&stateid, &delegation->stateid);
1043 rcu_read_unlock();
1044 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1045 if (ret != 0)
1046 goto out;
1047 ret = -EAGAIN;
1049 /* Try to update the stateid using the delegation */
1050 if (update_open_stateid(state, NULL, &stateid, fmode))
1051 goto out_return_state;
1053 out:
1054 return ERR_PTR(ret);
1055 out_return_state:
1056 atomic_inc(&state->count);
1057 return state;
1060 static void
1061 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1063 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1064 struct nfs_delegation *delegation;
1065 int delegation_flags = 0;
1067 rcu_read_lock();
1068 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1069 if (delegation)
1070 delegation_flags = delegation->flags;
1071 rcu_read_unlock();
1072 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1073 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1074 "returning a delegation for "
1075 "OPEN(CLAIM_DELEGATE_CUR)\n",
1076 clp->cl_hostname);
1077 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1078 nfs_inode_set_delegation(state->inode,
1079 data->owner->so_cred,
1080 &data->o_res);
1081 else
1082 nfs_inode_reclaim_delegation(state->inode,
1083 data->owner->so_cred,
1084 &data->o_res);
1088 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1089 * and update the nfs4_state.
1091 static struct nfs4_state *
1092 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1094 struct inode *inode = data->state->inode;
1095 struct nfs4_state *state = data->state;
1096 int ret;
1098 if (!data->rpc_done) {
1099 ret = data->rpc_status;
1100 goto err;
1103 ret = -ESTALE;
1104 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1105 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1106 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1107 goto err;
1109 ret = -ENOMEM;
1110 state = nfs4_get_open_state(inode, data->owner);
1111 if (state == NULL)
1112 goto err;
1114 ret = nfs_refresh_inode(inode, &data->f_attr);
1115 if (ret)
1116 goto err;
1118 if (data->o_res.delegation_type != 0)
1119 nfs4_opendata_check_deleg(data, state);
1120 update_open_stateid(state, &data->o_res.stateid, NULL,
1121 data->o_arg.fmode);
1123 return state;
1124 err:
1125 return ERR_PTR(ret);
1129 static struct nfs4_state *
1130 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1132 struct inode *inode;
1133 struct nfs4_state *state = NULL;
1134 int ret;
1136 if (!data->rpc_done) {
1137 state = nfs4_try_open_cached(data);
1138 goto out;
1141 ret = -EAGAIN;
1142 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1143 goto err;
1144 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1145 ret = PTR_ERR(inode);
1146 if (IS_ERR(inode))
1147 goto err;
1148 ret = -ENOMEM;
1149 state = nfs4_get_open_state(inode, data->owner);
1150 if (state == NULL)
1151 goto err_put_inode;
1152 if (data->o_res.delegation_type != 0)
1153 nfs4_opendata_check_deleg(data, state);
1154 update_open_stateid(state, &data->o_res.stateid, NULL,
1155 data->o_arg.fmode);
1156 iput(inode);
1157 out:
1158 return state;
1159 err_put_inode:
1160 iput(inode);
1161 err:
1162 return ERR_PTR(ret);
1165 static struct nfs4_state *
1166 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1168 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1169 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1170 return _nfs4_opendata_to_nfs4_state(data);
1173 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1175 struct nfs_inode *nfsi = NFS_I(state->inode);
1176 struct nfs_open_context *ctx;
1178 spin_lock(&state->inode->i_lock);
1179 list_for_each_entry(ctx, &nfsi->open_files, list) {
1180 if (ctx->state != state)
1181 continue;
1182 get_nfs_open_context(ctx);
1183 spin_unlock(&state->inode->i_lock);
1184 return ctx;
1186 spin_unlock(&state->inode->i_lock);
1187 return ERR_PTR(-ENOENT);
1190 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1192 struct nfs4_opendata *opendata;
1194 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1195 if (opendata == NULL)
1196 return ERR_PTR(-ENOMEM);
1197 opendata->state = state;
1198 atomic_inc(&state->count);
1199 return opendata;
1202 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1204 struct nfs4_state *newstate;
1205 int ret;
1207 opendata->o_arg.open_flags = 0;
1208 opendata->o_arg.fmode = fmode;
1209 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1210 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1211 nfs4_init_opendata_res(opendata);
1212 ret = _nfs4_recover_proc_open(opendata);
1213 if (ret != 0)
1214 return ret;
1215 newstate = nfs4_opendata_to_nfs4_state(opendata);
1216 if (IS_ERR(newstate))
1217 return PTR_ERR(newstate);
1218 nfs4_close_state(newstate, fmode);
1219 *res = newstate;
1220 return 0;
1223 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1225 struct nfs4_state *newstate;
1226 int ret;
1228 /* memory barrier prior to reading state->n_* */
1229 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1230 smp_rmb();
1231 if (state->n_rdwr != 0) {
1232 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1233 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1234 if (ret != 0)
1235 return ret;
1236 if (newstate != state)
1237 return -ESTALE;
1239 if (state->n_wronly != 0) {
1240 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1241 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1242 if (ret != 0)
1243 return ret;
1244 if (newstate != state)
1245 return -ESTALE;
1247 if (state->n_rdonly != 0) {
1248 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1249 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1250 if (ret != 0)
1251 return ret;
1252 if (newstate != state)
1253 return -ESTALE;
1256 * We may have performed cached opens for all three recoveries.
1257 * Check if we need to update the current stateid.
1259 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1260 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1261 write_seqlock(&state->seqlock);
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1263 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1264 write_sequnlock(&state->seqlock);
1266 return 0;
1270 * OPEN_RECLAIM:
1271 * reclaim state on the server after a reboot.
1273 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1275 struct nfs_delegation *delegation;
1276 struct nfs4_opendata *opendata;
1277 fmode_t delegation_type = 0;
1278 int status;
1280 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1281 if (IS_ERR(opendata))
1282 return PTR_ERR(opendata);
1283 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1284 opendata->o_arg.fh = NFS_FH(state->inode);
1285 rcu_read_lock();
1286 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1287 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1288 delegation_type = delegation->type;
1289 rcu_read_unlock();
1290 opendata->o_arg.u.delegation_type = delegation_type;
1291 status = nfs4_open_recover(opendata, state);
1292 nfs4_opendata_put(opendata);
1293 return status;
1296 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1298 struct nfs_server *server = NFS_SERVER(state->inode);
1299 struct nfs4_exception exception = { };
1300 int err;
1301 do {
1302 err = _nfs4_do_open_reclaim(ctx, state);
1303 if (err != -NFS4ERR_DELAY)
1304 break;
1305 nfs4_handle_exception(server, err, &exception);
1306 } while (exception.retry);
1307 return err;
1310 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1312 struct nfs_open_context *ctx;
1313 int ret;
1315 ctx = nfs4_state_find_open_context(state);
1316 if (IS_ERR(ctx))
1317 return PTR_ERR(ctx);
1318 ret = nfs4_do_open_reclaim(ctx, state);
1319 put_nfs_open_context(ctx);
1320 return ret;
1323 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1325 struct nfs4_opendata *opendata;
1326 int ret;
1328 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1329 if (IS_ERR(opendata))
1330 return PTR_ERR(opendata);
1331 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1332 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1333 ret = nfs4_open_recover(opendata, state);
1334 nfs4_opendata_put(opendata);
1335 return ret;
1338 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1340 struct nfs4_exception exception = { };
1341 struct nfs_server *server = NFS_SERVER(state->inode);
1342 int err;
1343 do {
1344 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1345 switch (err) {
1346 case 0:
1347 case -ENOENT:
1348 case -ESTALE:
1349 goto out;
1350 case -NFS4ERR_BADSESSION:
1351 case -NFS4ERR_BADSLOT:
1352 case -NFS4ERR_BAD_HIGH_SLOT:
1353 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1354 case -NFS4ERR_DEADSESSION:
1355 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1356 goto out;
1357 case -NFS4ERR_STALE_CLIENTID:
1358 case -NFS4ERR_STALE_STATEID:
1359 case -NFS4ERR_EXPIRED:
1360 /* Don't recall a delegation if it was lost */
1361 nfs4_schedule_lease_recovery(server->nfs_client);
1362 goto out;
1363 case -ERESTARTSYS:
1365 * The show must go on: exit, but mark the
1366 * stateid as needing recovery.
1368 case -NFS4ERR_DELEG_REVOKED:
1369 case -NFS4ERR_ADMIN_REVOKED:
1370 case -NFS4ERR_BAD_STATEID:
1371 nfs_inode_find_state_and_recover(state->inode,
1372 stateid);
1373 nfs4_schedule_stateid_recovery(server, state);
1374 case -ENOMEM:
1375 err = 0;
1376 goto out;
1378 err = nfs4_handle_exception(server, err, &exception);
1379 } while (exception.retry);
1380 out:
1381 return err;
1384 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1386 struct nfs4_opendata *data = calldata;
1388 data->rpc_status = task->tk_status;
1389 if (data->rpc_status == 0) {
1390 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1391 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1392 renew_lease(data->o_res.server, data->timestamp);
1393 data->rpc_done = 1;
1397 static void nfs4_open_confirm_release(void *calldata)
1399 struct nfs4_opendata *data = calldata;
1400 struct nfs4_state *state = NULL;
1402 /* If this request hasn't been cancelled, do nothing */
1403 if (data->cancelled == 0)
1404 goto out_free;
1405 /* In case of error, no cleanup! */
1406 if (!data->rpc_done)
1407 goto out_free;
1408 state = nfs4_opendata_to_nfs4_state(data);
1409 if (!IS_ERR(state))
1410 nfs4_close_state(state, data->o_arg.fmode);
1411 out_free:
1412 nfs4_opendata_put(data);
1415 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1416 .rpc_call_done = nfs4_open_confirm_done,
1417 .rpc_release = nfs4_open_confirm_release,
1421 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1423 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1425 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1426 struct rpc_task *task;
1427 struct rpc_message msg = {
1428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1429 .rpc_argp = &data->c_arg,
1430 .rpc_resp = &data->c_res,
1431 .rpc_cred = data->owner->so_cred,
1433 struct rpc_task_setup task_setup_data = {
1434 .rpc_client = server->client,
1435 .rpc_message = &msg,
1436 .callback_ops = &nfs4_open_confirm_ops,
1437 .callback_data = data,
1438 .workqueue = nfsiod_workqueue,
1439 .flags = RPC_TASK_ASYNC,
1441 int status;
1443 kref_get(&data->kref);
1444 data->rpc_done = 0;
1445 data->rpc_status = 0;
1446 data->timestamp = jiffies;
1447 task = rpc_run_task(&task_setup_data);
1448 if (IS_ERR(task))
1449 return PTR_ERR(task);
1450 status = nfs4_wait_for_completion_rpc_task(task);
1451 if (status != 0) {
1452 data->cancelled = 1;
1453 smp_wmb();
1454 } else
1455 status = data->rpc_status;
1456 rpc_put_task(task);
1457 return status;
1460 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1462 struct nfs4_opendata *data = calldata;
1463 struct nfs4_state_owner *sp = data->owner;
1465 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1466 return;
1468 * Check if we still need to send an OPEN call, or if we can use
1469 * a delegation instead.
1471 if (data->state != NULL) {
1472 struct nfs_delegation *delegation;
1474 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1475 goto out_no_action;
1476 rcu_read_lock();
1477 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1478 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1479 can_open_delegated(delegation, data->o_arg.fmode))
1480 goto unlock_no_action;
1481 rcu_read_unlock();
1483 /* Update client id. */
1484 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1485 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1486 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1487 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1488 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1490 data->timestamp = jiffies;
1491 if (nfs4_setup_sequence(data->o_arg.server,
1492 &data->o_arg.seq_args,
1493 &data->o_res.seq_res,
1494 task) != 0)
1495 nfs_release_seqid(data->o_arg.seqid);
1496 return;
1497 unlock_no_action:
1498 rcu_read_unlock();
1499 out_no_action:
1500 task->tk_action = NULL;
1501 nfs4_sequence_done(task, &data->o_res.seq_res);
1504 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1506 struct nfs4_opendata *data = calldata;
1508 data->rpc_status = task->tk_status;
1510 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1511 return;
1513 if (task->tk_status == 0) {
1514 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1515 switch (data->o_res.f_attr->mode & S_IFMT) {
1516 case S_IFREG:
1517 break;
1518 case S_IFLNK:
1519 data->rpc_status = -ELOOP;
1520 break;
1521 case S_IFDIR:
1522 data->rpc_status = -EISDIR;
1523 break;
1524 default:
1525 data->rpc_status = -ENOTDIR;
1528 renew_lease(data->o_res.server, data->timestamp);
1529 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1530 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1532 data->rpc_done = 1;
1535 static void nfs4_open_release(void *calldata)
1537 struct nfs4_opendata *data = calldata;
1538 struct nfs4_state *state = NULL;
1540 /* If this request hasn't been cancelled, do nothing */
1541 if (data->cancelled == 0)
1542 goto out_free;
1543 /* In case of error, no cleanup! */
1544 if (data->rpc_status != 0 || !data->rpc_done)
1545 goto out_free;
1546 /* In case we need an open_confirm, no cleanup! */
1547 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1548 goto out_free;
1549 state = nfs4_opendata_to_nfs4_state(data);
1550 if (!IS_ERR(state))
1551 nfs4_close_state(state, data->o_arg.fmode);
1552 out_free:
1553 nfs4_opendata_put(data);
1556 static const struct rpc_call_ops nfs4_open_ops = {
1557 .rpc_call_prepare = nfs4_open_prepare,
1558 .rpc_call_done = nfs4_open_done,
1559 .rpc_release = nfs4_open_release,
1562 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1564 struct inode *dir = data->dir->d_inode;
1565 struct nfs_server *server = NFS_SERVER(dir);
1566 struct nfs_openargs *o_arg = &data->o_arg;
1567 struct nfs_openres *o_res = &data->o_res;
1568 struct rpc_task *task;
1569 struct rpc_message msg = {
1570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1571 .rpc_argp = o_arg,
1572 .rpc_resp = o_res,
1573 .rpc_cred = data->owner->so_cred,
1575 struct rpc_task_setup task_setup_data = {
1576 .rpc_client = server->client,
1577 .rpc_message = &msg,
1578 .callback_ops = &nfs4_open_ops,
1579 .callback_data = data,
1580 .workqueue = nfsiod_workqueue,
1581 .flags = RPC_TASK_ASYNC,
1583 int status;
1585 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1586 kref_get(&data->kref);
1587 data->rpc_done = 0;
1588 data->rpc_status = 0;
1589 data->cancelled = 0;
1590 if (isrecover)
1591 nfs4_set_sequence_privileged(&o_arg->seq_args);
1592 task = rpc_run_task(&task_setup_data);
1593 if (IS_ERR(task))
1594 return PTR_ERR(task);
1595 status = nfs4_wait_for_completion_rpc_task(task);
1596 if (status != 0) {
1597 data->cancelled = 1;
1598 smp_wmb();
1599 } else
1600 status = data->rpc_status;
1601 rpc_put_task(task);
1603 return status;
1606 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1608 struct inode *dir = data->dir->d_inode;
1609 struct nfs_openres *o_res = &data->o_res;
1610 int status;
1612 status = nfs4_run_open_task(data, 1);
1613 if (status != 0 || !data->rpc_done)
1614 return status;
1616 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1618 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1619 status = _nfs4_proc_open_confirm(data);
1620 if (status != 0)
1621 return status;
1624 return status;
1627 static int nfs4_opendata_access(struct rpc_cred *cred,
1628 struct nfs4_opendata *opendata,
1629 struct nfs4_state *state, fmode_t fmode,
1630 int openflags)
1632 struct nfs_access_entry cache;
1633 u32 mask;
1635 /* access call failed or for some reason the server doesn't
1636 * support any access modes -- defer access call until later */
1637 if (opendata->o_res.access_supported == 0)
1638 return 0;
1640 mask = 0;
1641 /* don't check MAY_WRITE - a newly created file may not have
1642 * write mode bits, but POSIX allows the creating process to write.
1643 * use openflags to check for exec, because fmode won't
1644 * always have FMODE_EXEC set when file open for exec. */
1645 if (openflags & __FMODE_EXEC) {
1646 /* ONLY check for exec rights */
1647 mask = MAY_EXEC;
1648 } else if (fmode & FMODE_READ)
1649 mask = MAY_READ;
1651 cache.cred = cred;
1652 cache.jiffies = jiffies;
1653 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1654 nfs_access_add_cache(state->inode, &cache);
1656 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1657 return 0;
1659 /* even though OPEN succeeded, access is denied. Close the file */
1660 nfs4_close_state(state, fmode);
1661 return -EACCES;
1665 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1667 static int _nfs4_proc_open(struct nfs4_opendata *data)
1669 struct inode *dir = data->dir->d_inode;
1670 struct nfs_server *server = NFS_SERVER(dir);
1671 struct nfs_openargs *o_arg = &data->o_arg;
1672 struct nfs_openres *o_res = &data->o_res;
1673 int status;
1675 status = nfs4_run_open_task(data, 0);
1676 if (!data->rpc_done)
1677 return status;
1678 if (status != 0) {
1679 if (status == -NFS4ERR_BADNAME &&
1680 !(o_arg->open_flags & O_CREAT))
1681 return -ENOENT;
1682 return status;
1685 nfs_fattr_map_and_free_names(server, &data->f_attr);
1687 if (o_arg->open_flags & O_CREAT)
1688 update_changeattr(dir, &o_res->cinfo);
1689 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1690 server->caps &= ~NFS_CAP_POSIX_LOCK;
1691 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1692 status = _nfs4_proc_open_confirm(data);
1693 if (status != 0)
1694 return status;
1696 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1697 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1698 return 0;
1701 static int nfs4_recover_expired_lease(struct nfs_server *server)
1703 return nfs4_client_recover_expired_lease(server->nfs_client);
1707 * OPEN_EXPIRED:
1708 * reclaim state on the server after a network partition.
1709 * Assumes caller holds the appropriate lock
1711 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1713 struct nfs4_opendata *opendata;
1714 int ret;
1716 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1717 if (IS_ERR(opendata))
1718 return PTR_ERR(opendata);
1719 ret = nfs4_open_recover(opendata, state);
1720 if (ret == -ESTALE)
1721 d_drop(ctx->dentry);
1722 nfs4_opendata_put(opendata);
1723 return ret;
1726 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1728 struct nfs_server *server = NFS_SERVER(state->inode);
1729 struct nfs4_exception exception = { };
1730 int err;
1732 do {
1733 err = _nfs4_open_expired(ctx, state);
1734 switch (err) {
1735 default:
1736 goto out;
1737 case -NFS4ERR_GRACE:
1738 case -NFS4ERR_DELAY:
1739 nfs4_handle_exception(server, err, &exception);
1740 err = 0;
1742 } while (exception.retry);
1743 out:
1744 return err;
1747 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1749 struct nfs_open_context *ctx;
1750 int ret;
1752 ctx = nfs4_state_find_open_context(state);
1753 if (IS_ERR(ctx))
1754 return PTR_ERR(ctx);
1755 ret = nfs4_do_open_expired(ctx, state);
1756 put_nfs_open_context(ctx);
1757 return ret;
1760 #if defined(CONFIG_NFS_V4_1)
1761 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1763 struct nfs_server *server = NFS_SERVER(state->inode);
1764 nfs4_stateid *stateid = &state->stateid;
1765 int status;
1767 /* If a state reset has been done, test_stateid is unneeded */
1768 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1769 return;
1771 status = nfs41_test_stateid(server, stateid);
1772 if (status != NFS_OK) {
1773 /* Free the stateid unless the server explicitly
1774 * informs us the stateid is unrecognized. */
1775 if (status != -NFS4ERR_BAD_STATEID)
1776 nfs41_free_stateid(server, stateid);
1777 nfs_remove_bad_delegation(state->inode);
1779 write_seqlock(&state->seqlock);
1780 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1781 write_sequnlock(&state->seqlock);
1782 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1787 * nfs41_check_open_stateid - possibly free an open stateid
1789 * @state: NFSv4 state for an inode
1791 * Returns NFS_OK if recovery for this stateid is now finished.
1792 * Otherwise a negative NFS4ERR value is returned.
1794 static int nfs41_check_open_stateid(struct nfs4_state *state)
1796 struct nfs_server *server = NFS_SERVER(state->inode);
1797 nfs4_stateid *stateid = &state->open_stateid;
1798 int status;
1800 /* If a state reset has been done, test_stateid is unneeded */
1801 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1802 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1803 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1804 return -NFS4ERR_BAD_STATEID;
1806 status = nfs41_test_stateid(server, stateid);
1807 if (status != NFS_OK) {
1808 /* Free the stateid unless the server explicitly
1809 * informs us the stateid is unrecognized. */
1810 if (status != -NFS4ERR_BAD_STATEID)
1811 nfs41_free_stateid(server, stateid);
1813 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1814 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1815 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1817 return status;
1820 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1822 int status;
1824 nfs41_clear_delegation_stateid(state);
1825 status = nfs41_check_open_stateid(state);
1826 if (status != NFS_OK)
1827 status = nfs4_open_expired(sp, state);
1828 return status;
1830 #endif
1833 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1834 * fields corresponding to attributes that were used to store the verifier.
1835 * Make sure we clobber those fields in the later setattr call
1837 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1839 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1840 !(sattr->ia_valid & ATTR_ATIME_SET))
1841 sattr->ia_valid |= ATTR_ATIME;
1843 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1844 !(sattr->ia_valid & ATTR_MTIME_SET))
1845 sattr->ia_valid |= ATTR_MTIME;
1849 * Returns a referenced nfs4_state
1851 static int _nfs4_do_open(struct inode *dir,
1852 struct dentry *dentry,
1853 fmode_t fmode,
1854 int flags,
1855 struct iattr *sattr,
1856 struct rpc_cred *cred,
1857 struct nfs4_state **res,
1858 struct nfs4_threshold **ctx_th)
1860 struct nfs4_state_owner *sp;
1861 struct nfs4_state *state = NULL;
1862 struct nfs_server *server = NFS_SERVER(dir);
1863 struct nfs4_opendata *opendata;
1864 int status;
1866 /* Protect against reboot recovery conflicts */
1867 status = -ENOMEM;
1868 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1869 if (sp == NULL) {
1870 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1871 goto out_err;
1873 status = nfs4_recover_expired_lease(server);
1874 if (status != 0)
1875 goto err_put_state_owner;
1876 if (dentry->d_inode != NULL)
1877 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1878 status = -ENOMEM;
1879 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1880 if (opendata == NULL)
1881 goto err_put_state_owner;
1883 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1884 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1885 if (!opendata->f_attr.mdsthreshold)
1886 goto err_opendata_put;
1887 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1889 if (dentry->d_inode != NULL)
1890 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1892 status = _nfs4_proc_open(opendata);
1893 if (status != 0)
1894 goto err_opendata_put;
1896 state = nfs4_opendata_to_nfs4_state(opendata);
1897 status = PTR_ERR(state);
1898 if (IS_ERR(state))
1899 goto err_opendata_put;
1900 if (server->caps & NFS_CAP_POSIX_LOCK)
1901 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1903 status = nfs4_opendata_access(cred, opendata, state, fmode, flags);
1904 if (status != 0)
1905 goto err_opendata_put;
1907 if (opendata->o_arg.open_flags & O_EXCL) {
1908 nfs4_exclusive_attrset(opendata, sattr);
1910 nfs_fattr_init(opendata->o_res.f_attr);
1911 status = nfs4_do_setattr(state->inode, cred,
1912 opendata->o_res.f_attr, sattr,
1913 state);
1914 if (status == 0)
1915 nfs_setattr_update_inode(state->inode, sattr);
1916 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1919 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1920 *ctx_th = opendata->f_attr.mdsthreshold;
1921 else
1922 kfree(opendata->f_attr.mdsthreshold);
1923 opendata->f_attr.mdsthreshold = NULL;
1925 nfs4_opendata_put(opendata);
1926 nfs4_put_state_owner(sp);
1927 *res = state;
1928 return 0;
1929 err_opendata_put:
1930 kfree(opendata->f_attr.mdsthreshold);
1931 nfs4_opendata_put(opendata);
1932 err_put_state_owner:
1933 nfs4_put_state_owner(sp);
1934 out_err:
1935 *res = NULL;
1936 return status;
1940 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1941 struct dentry *dentry,
1942 fmode_t fmode,
1943 int flags,
1944 struct iattr *sattr,
1945 struct rpc_cred *cred,
1946 struct nfs4_threshold **ctx_th)
1948 struct nfs4_exception exception = { };
1949 struct nfs4_state *res;
1950 int status;
1952 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1953 do {
1954 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1955 &res, ctx_th);
1956 if (status == 0)
1957 break;
1958 /* NOTE: BAD_SEQID means the server and client disagree about the
1959 * book-keeping w.r.t. state-changing operations
1960 * (OPEN/CLOSE/LOCK/LOCKU...)
1961 * It is actually a sign of a bug on the client or on the server.
1963 * If we receive a BAD_SEQID error in the particular case of
1964 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1965 * have unhashed the old state_owner for us, and that we can
1966 * therefore safely retry using a new one. We should still warn
1967 * the user though...
1969 if (status == -NFS4ERR_BAD_SEQID) {
1970 pr_warn_ratelimited("NFS: v4 server %s "
1971 " returned a bad sequence-id error!\n",
1972 NFS_SERVER(dir)->nfs_client->cl_hostname);
1973 exception.retry = 1;
1974 continue;
1977 * BAD_STATEID on OPEN means that the server cancelled our
1978 * state before it received the OPEN_CONFIRM.
1979 * Recover by retrying the request as per the discussion
1980 * on Page 181 of RFC3530.
1982 if (status == -NFS4ERR_BAD_STATEID) {
1983 exception.retry = 1;
1984 continue;
1986 if (status == -EAGAIN) {
1987 /* We must have found a delegation */
1988 exception.retry = 1;
1989 continue;
1991 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1992 status, &exception));
1993 } while (exception.retry);
1994 return res;
1997 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1998 struct nfs_fattr *fattr, struct iattr *sattr,
1999 struct nfs4_state *state)
2001 struct nfs_server *server = NFS_SERVER(inode);
2002 struct nfs_setattrargs arg = {
2003 .fh = NFS_FH(inode),
2004 .iap = sattr,
2005 .server = server,
2006 .bitmask = server->attr_bitmask,
2008 struct nfs_setattrres res = {
2009 .fattr = fattr,
2010 .server = server,
2012 struct rpc_message msg = {
2013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2014 .rpc_argp = &arg,
2015 .rpc_resp = &res,
2016 .rpc_cred = cred,
2018 unsigned long timestamp = jiffies;
2019 int status;
2021 nfs_fattr_init(fattr);
2023 if (state != NULL) {
2024 struct nfs_lockowner lockowner = {
2025 .l_owner = current->files,
2026 .l_pid = current->tgid,
2028 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2029 &lockowner);
2030 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2031 FMODE_WRITE)) {
2032 /* Use that stateid */
2033 } else
2034 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2036 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2037 if (status == 0 && state != NULL)
2038 renew_lease(server, timestamp);
2039 return status;
2042 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2043 struct nfs_fattr *fattr, struct iattr *sattr,
2044 struct nfs4_state *state)
2046 struct nfs_server *server = NFS_SERVER(inode);
2047 struct nfs4_exception exception = {
2048 .state = state,
2049 .inode = inode,
2051 int err;
2052 do {
2053 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2054 switch (err) {
2055 case -NFS4ERR_OPENMODE:
2056 if (state && !(state->state & FMODE_WRITE)) {
2057 err = -EBADF;
2058 if (sattr->ia_valid & ATTR_OPEN)
2059 err = -EACCES;
2060 goto out;
2063 err = nfs4_handle_exception(server, err, &exception);
2064 } while (exception.retry);
2065 out:
2066 return err;
2069 struct nfs4_closedata {
2070 struct inode *inode;
2071 struct nfs4_state *state;
2072 struct nfs_closeargs arg;
2073 struct nfs_closeres res;
2074 struct nfs_fattr fattr;
2075 unsigned long timestamp;
2076 bool roc;
2077 u32 roc_barrier;
2080 static void nfs4_free_closedata(void *data)
2082 struct nfs4_closedata *calldata = data;
2083 struct nfs4_state_owner *sp = calldata->state->owner;
2084 struct super_block *sb = calldata->state->inode->i_sb;
2086 if (calldata->roc)
2087 pnfs_roc_release(calldata->state->inode);
2088 nfs4_put_open_state(calldata->state);
2089 nfs_free_seqid(calldata->arg.seqid);
2090 nfs4_put_state_owner(sp);
2091 nfs_sb_deactive_async(sb);
2092 kfree(calldata);
2095 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2096 fmode_t fmode)
2098 spin_lock(&state->owner->so_lock);
2099 if (!(fmode & FMODE_READ))
2100 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2101 if (!(fmode & FMODE_WRITE))
2102 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2103 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2104 spin_unlock(&state->owner->so_lock);
2107 static void nfs4_close_done(struct rpc_task *task, void *data)
2109 struct nfs4_closedata *calldata = data;
2110 struct nfs4_state *state = calldata->state;
2111 struct nfs_server *server = NFS_SERVER(calldata->inode);
2113 dprintk("%s: begin!\n", __func__);
2114 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2115 return;
2116 /* hmm. we are done with the inode, and in the process of freeing
2117 * the state_owner. we keep this around to process errors
2119 switch (task->tk_status) {
2120 case 0:
2121 if (calldata->roc)
2122 pnfs_roc_set_barrier(state->inode,
2123 calldata->roc_barrier);
2124 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2125 renew_lease(server, calldata->timestamp);
2126 nfs4_close_clear_stateid_flags(state,
2127 calldata->arg.fmode);
2128 break;
2129 case -NFS4ERR_STALE_STATEID:
2130 case -NFS4ERR_OLD_STATEID:
2131 case -NFS4ERR_BAD_STATEID:
2132 case -NFS4ERR_EXPIRED:
2133 if (calldata->arg.fmode == 0)
2134 break;
2135 default:
2136 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2137 rpc_restart_call_prepare(task);
2139 nfs_release_seqid(calldata->arg.seqid);
2140 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2141 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2144 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2146 struct nfs4_closedata *calldata = data;
2147 struct nfs4_state *state = calldata->state;
2148 struct inode *inode = calldata->inode;
2149 int call_close = 0;
2151 dprintk("%s: begin!\n", __func__);
2152 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2153 return;
2155 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2156 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2157 spin_lock(&state->owner->so_lock);
2158 /* Calculate the change in open mode */
2159 if (state->n_rdwr == 0) {
2160 if (state->n_rdonly == 0) {
2161 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2162 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2163 calldata->arg.fmode &= ~FMODE_READ;
2165 if (state->n_wronly == 0) {
2166 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2167 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2168 calldata->arg.fmode &= ~FMODE_WRITE;
2171 spin_unlock(&state->owner->so_lock);
2173 if (!call_close) {
2174 /* Note: exit _without_ calling nfs4_close_done */
2175 task->tk_action = NULL;
2176 nfs4_sequence_done(task, &calldata->res.seq_res);
2177 goto out;
2180 if (calldata->arg.fmode == 0) {
2181 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2182 if (calldata->roc &&
2183 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2184 goto out;
2187 nfs_fattr_init(calldata->res.fattr);
2188 calldata->timestamp = jiffies;
2189 if (nfs4_setup_sequence(NFS_SERVER(inode),
2190 &calldata->arg.seq_args,
2191 &calldata->res.seq_res,
2192 task) != 0)
2193 nfs_release_seqid(calldata->arg.seqid);
2194 out:
2195 dprintk("%s: done!\n", __func__);
2198 static const struct rpc_call_ops nfs4_close_ops = {
2199 .rpc_call_prepare = nfs4_close_prepare,
2200 .rpc_call_done = nfs4_close_done,
2201 .rpc_release = nfs4_free_closedata,
2205 * It is possible for data to be read/written from a mem-mapped file
2206 * after the sys_close call (which hits the vfs layer as a flush).
2207 * This means that we can't safely call nfsv4 close on a file until
2208 * the inode is cleared. This in turn means that we are not good
2209 * NFSv4 citizens - we do not indicate to the server to update the file's
2210 * share state even when we are done with one of the three share
2211 * stateid's in the inode.
2213 * NOTE: Caller must be holding the sp->so_owner semaphore!
2215 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2217 struct nfs_server *server = NFS_SERVER(state->inode);
2218 struct nfs4_closedata *calldata;
2219 struct nfs4_state_owner *sp = state->owner;
2220 struct rpc_task *task;
2221 struct rpc_message msg = {
2222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2223 .rpc_cred = state->owner->so_cred,
2225 struct rpc_task_setup task_setup_data = {
2226 .rpc_client = server->client,
2227 .rpc_message = &msg,
2228 .callback_ops = &nfs4_close_ops,
2229 .workqueue = nfsiod_workqueue,
2230 .flags = RPC_TASK_ASYNC,
2232 int status = -ENOMEM;
2234 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2235 if (calldata == NULL)
2236 goto out;
2237 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2238 calldata->inode = state->inode;
2239 calldata->state = state;
2240 calldata->arg.fh = NFS_FH(state->inode);
2241 calldata->arg.stateid = &state->open_stateid;
2242 /* Serialization for the sequence id */
2243 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2244 if (calldata->arg.seqid == NULL)
2245 goto out_free_calldata;
2246 calldata->arg.fmode = 0;
2247 calldata->arg.bitmask = server->cache_consistency_bitmask;
2248 calldata->res.fattr = &calldata->fattr;
2249 calldata->res.seqid = calldata->arg.seqid;
2250 calldata->res.server = server;
2251 calldata->roc = pnfs_roc(state->inode);
2252 nfs_sb_active(calldata->inode->i_sb);
2254 msg.rpc_argp = &calldata->arg;
2255 msg.rpc_resp = &calldata->res;
2256 task_setup_data.callback_data = calldata;
2257 task = rpc_run_task(&task_setup_data);
2258 if (IS_ERR(task))
2259 return PTR_ERR(task);
2260 status = 0;
2261 if (wait)
2262 status = rpc_wait_for_completion_task(task);
2263 rpc_put_task(task);
2264 return status;
2265 out_free_calldata:
2266 kfree(calldata);
2267 out:
2268 nfs4_put_open_state(state);
2269 nfs4_put_state_owner(sp);
2270 return status;
2273 static struct inode *
2274 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2276 struct nfs4_state *state;
2278 /* Protect against concurrent sillydeletes */
2279 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2280 ctx->cred, &ctx->mdsthreshold);
2281 if (IS_ERR(state))
2282 return ERR_CAST(state);
2283 ctx->state = state;
2284 return igrab(state->inode);
2287 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2289 if (ctx->state == NULL)
2290 return;
2291 if (is_sync)
2292 nfs4_close_sync(ctx->state, ctx->mode);
2293 else
2294 nfs4_close_state(ctx->state, ctx->mode);
2297 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2299 struct nfs4_server_caps_arg args = {
2300 .fhandle = fhandle,
2302 struct nfs4_server_caps_res res = {};
2303 struct rpc_message msg = {
2304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2305 .rpc_argp = &args,
2306 .rpc_resp = &res,
2308 int status;
2310 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2311 if (status == 0) {
2312 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2313 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2314 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2315 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2316 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2317 NFS_CAP_CTIME|NFS_CAP_MTIME);
2318 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2319 server->caps |= NFS_CAP_ACLS;
2320 if (res.has_links != 0)
2321 server->caps |= NFS_CAP_HARDLINKS;
2322 if (res.has_symlinks != 0)
2323 server->caps |= NFS_CAP_SYMLINKS;
2324 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2325 server->caps |= NFS_CAP_FILEID;
2326 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2327 server->caps |= NFS_CAP_MODE;
2328 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2329 server->caps |= NFS_CAP_NLINK;
2330 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2331 server->caps |= NFS_CAP_OWNER;
2332 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2333 server->caps |= NFS_CAP_OWNER_GROUP;
2334 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2335 server->caps |= NFS_CAP_ATIME;
2336 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2337 server->caps |= NFS_CAP_CTIME;
2338 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2339 server->caps |= NFS_CAP_MTIME;
2341 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2342 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2343 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2344 server->acl_bitmask = res.acl_bitmask;
2345 server->fh_expire_type = res.fh_expire_type;
2348 return status;
2351 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2353 struct nfs4_exception exception = { };
2354 int err;
2355 do {
2356 err = nfs4_handle_exception(server,
2357 _nfs4_server_capabilities(server, fhandle),
2358 &exception);
2359 } while (exception.retry);
2360 return err;
2363 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2364 struct nfs_fsinfo *info)
2366 struct nfs4_lookup_root_arg args = {
2367 .bitmask = nfs4_fattr_bitmap,
2369 struct nfs4_lookup_res res = {
2370 .server = server,
2371 .fattr = info->fattr,
2372 .fh = fhandle,
2374 struct rpc_message msg = {
2375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2376 .rpc_argp = &args,
2377 .rpc_resp = &res,
2380 nfs_fattr_init(info->fattr);
2381 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2384 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2385 struct nfs_fsinfo *info)
2387 struct nfs4_exception exception = { };
2388 int err;
2389 do {
2390 err = _nfs4_lookup_root(server, fhandle, info);
2391 switch (err) {
2392 case 0:
2393 case -NFS4ERR_WRONGSEC:
2394 goto out;
2395 default:
2396 err = nfs4_handle_exception(server, err, &exception);
2398 } while (exception.retry);
2399 out:
2400 return err;
2403 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2404 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2406 struct rpc_auth *auth;
2407 int ret;
2409 auth = rpcauth_create(flavor, server->client);
2410 if (IS_ERR(auth)) {
2411 ret = -EIO;
2412 goto out;
2414 ret = nfs4_lookup_root(server, fhandle, info);
2415 out:
2416 return ret;
2419 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2420 struct nfs_fsinfo *info)
2422 int i, len, status = 0;
2423 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2425 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2426 if (len < 0)
2427 return len;
2429 for (i = 0; i < len; i++) {
2430 /* AUTH_UNIX is the default flavor if none was specified,
2431 * thus has already been tried. */
2432 if (flav_array[i] == RPC_AUTH_UNIX)
2433 continue;
2435 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2436 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2437 continue;
2438 break;
2441 * -EACCESS could mean that the user doesn't have correct permissions
2442 * to access the mount. It could also mean that we tried to mount
2443 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2444 * existing mount programs don't handle -EACCES very well so it should
2445 * be mapped to -EPERM instead.
2447 if (status == -EACCES)
2448 status = -EPERM;
2449 return status;
2453 * get the file handle for the "/" directory on the server
2455 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2456 struct nfs_fsinfo *info)
2458 int minor_version = server->nfs_client->cl_minorversion;
2459 int status = nfs4_lookup_root(server, fhandle, info);
2460 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2462 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2463 * by nfs4_map_errors() as this function exits.
2465 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2466 if (status == 0)
2467 status = nfs4_server_capabilities(server, fhandle);
2468 if (status == 0)
2469 status = nfs4_do_fsinfo(server, fhandle, info);
2470 return nfs4_map_errors(status);
2473 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2474 struct nfs_fsinfo *info)
2476 int error;
2477 struct nfs_fattr *fattr = info->fattr;
2479 error = nfs4_server_capabilities(server, mntfh);
2480 if (error < 0) {
2481 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2482 return error;
2485 error = nfs4_proc_getattr(server, mntfh, fattr);
2486 if (error < 0) {
2487 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2488 return error;
2491 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2492 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2493 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2495 return error;
2499 * Get locations and (maybe) other attributes of a referral.
2500 * Note that we'll actually follow the referral later when
2501 * we detect fsid mismatch in inode revalidation
2503 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2504 const struct qstr *name, struct nfs_fattr *fattr,
2505 struct nfs_fh *fhandle)
2507 int status = -ENOMEM;
2508 struct page *page = NULL;
2509 struct nfs4_fs_locations *locations = NULL;
2511 page = alloc_page(GFP_KERNEL);
2512 if (page == NULL)
2513 goto out;
2514 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2515 if (locations == NULL)
2516 goto out;
2518 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2519 if (status != 0)
2520 goto out;
2521 /* Make sure server returned a different fsid for the referral */
2522 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2523 dprintk("%s: server did not return a different fsid for"
2524 " a referral at %s\n", __func__, name->name);
2525 status = -EIO;
2526 goto out;
2528 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2529 nfs_fixup_referral_attributes(&locations->fattr);
2531 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2532 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2533 memset(fhandle, 0, sizeof(struct nfs_fh));
2534 out:
2535 if (page)
2536 __free_page(page);
2537 kfree(locations);
2538 return status;
2541 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2543 struct nfs4_getattr_arg args = {
2544 .fh = fhandle,
2545 .bitmask = server->attr_bitmask,
2547 struct nfs4_getattr_res res = {
2548 .fattr = fattr,
2549 .server = server,
2551 struct rpc_message msg = {
2552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2553 .rpc_argp = &args,
2554 .rpc_resp = &res,
2557 nfs_fattr_init(fattr);
2558 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2561 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2563 struct nfs4_exception exception = { };
2564 int err;
2565 do {
2566 err = nfs4_handle_exception(server,
2567 _nfs4_proc_getattr(server, fhandle, fattr),
2568 &exception);
2569 } while (exception.retry);
2570 return err;
2574 * The file is not closed if it is opened due to the a request to change
2575 * the size of the file. The open call will not be needed once the
2576 * VFS layer lookup-intents are implemented.
2578 * Close is called when the inode is destroyed.
2579 * If we haven't opened the file for O_WRONLY, we
2580 * need to in the size_change case to obtain a stateid.
2582 * Got race?
2583 * Because OPEN is always done by name in nfsv4, it is
2584 * possible that we opened a different file by the same
2585 * name. We can recognize this race condition, but we
2586 * can't do anything about it besides returning an error.
2588 * This will be fixed with VFS changes (lookup-intent).
2590 static int
2591 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2592 struct iattr *sattr)
2594 struct inode *inode = dentry->d_inode;
2595 struct rpc_cred *cred = NULL;
2596 struct nfs4_state *state = NULL;
2597 int status;
2599 if (pnfs_ld_layoutret_on_setattr(inode))
2600 pnfs_return_layout(inode);
2602 nfs_fattr_init(fattr);
2604 /* Deal with open(O_TRUNC) */
2605 if (sattr->ia_valid & ATTR_OPEN)
2606 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2608 /* Optimization: if the end result is no change, don't RPC */
2609 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2610 return 0;
2612 /* Search for an existing open(O_WRITE) file */
2613 if (sattr->ia_valid & ATTR_FILE) {
2614 struct nfs_open_context *ctx;
2616 ctx = nfs_file_open_context(sattr->ia_file);
2617 if (ctx) {
2618 cred = ctx->cred;
2619 state = ctx->state;
2623 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2624 if (status == 0)
2625 nfs_setattr_update_inode(inode, sattr);
2626 return status;
2629 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2630 const struct qstr *name, struct nfs_fh *fhandle,
2631 struct nfs_fattr *fattr)
2633 struct nfs_server *server = NFS_SERVER(dir);
2634 int status;
2635 struct nfs4_lookup_arg args = {
2636 .bitmask = server->attr_bitmask,
2637 .dir_fh = NFS_FH(dir),
2638 .name = name,
2640 struct nfs4_lookup_res res = {
2641 .server = server,
2642 .fattr = fattr,
2643 .fh = fhandle,
2645 struct rpc_message msg = {
2646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2647 .rpc_argp = &args,
2648 .rpc_resp = &res,
2651 nfs_fattr_init(fattr);
2653 dprintk("NFS call lookup %s\n", name->name);
2654 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2655 dprintk("NFS reply lookup: %d\n", status);
2656 return status;
2659 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2661 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2662 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2663 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2664 fattr->nlink = 2;
2667 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2668 struct qstr *name, struct nfs_fh *fhandle,
2669 struct nfs_fattr *fattr)
2671 struct nfs4_exception exception = { };
2672 struct rpc_clnt *client = *clnt;
2673 int err;
2674 do {
2675 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2676 switch (err) {
2677 case -NFS4ERR_BADNAME:
2678 err = -ENOENT;
2679 goto out;
2680 case -NFS4ERR_MOVED:
2681 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2682 goto out;
2683 case -NFS4ERR_WRONGSEC:
2684 err = -EPERM;
2685 if (client != *clnt)
2686 goto out;
2688 client = nfs4_create_sec_client(client, dir, name);
2689 if (IS_ERR(client))
2690 return PTR_ERR(client);
2692 exception.retry = 1;
2693 break;
2694 default:
2695 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2697 } while (exception.retry);
2699 out:
2700 if (err == 0)
2701 *clnt = client;
2702 else if (client != *clnt)
2703 rpc_shutdown_client(client);
2705 return err;
2708 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2709 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2711 int status;
2712 struct rpc_clnt *client = NFS_CLIENT(dir);
2714 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2715 if (client != NFS_CLIENT(dir)) {
2716 rpc_shutdown_client(client);
2717 nfs_fixup_secinfo_attributes(fattr);
2719 return status;
2722 struct rpc_clnt *
2723 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2724 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2726 int status;
2727 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2729 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2730 if (status < 0) {
2731 rpc_shutdown_client(client);
2732 return ERR_PTR(status);
2734 return client;
2737 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2739 struct nfs_server *server = NFS_SERVER(inode);
2740 struct nfs4_accessargs args = {
2741 .fh = NFS_FH(inode),
2742 .bitmask = server->cache_consistency_bitmask,
2744 struct nfs4_accessres res = {
2745 .server = server,
2747 struct rpc_message msg = {
2748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2749 .rpc_argp = &args,
2750 .rpc_resp = &res,
2751 .rpc_cred = entry->cred,
2753 int mode = entry->mask;
2754 int status;
2757 * Determine which access bits we want to ask for...
2759 if (mode & MAY_READ)
2760 args.access |= NFS4_ACCESS_READ;
2761 if (S_ISDIR(inode->i_mode)) {
2762 if (mode & MAY_WRITE)
2763 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2764 if (mode & MAY_EXEC)
2765 args.access |= NFS4_ACCESS_LOOKUP;
2766 } else {
2767 if (mode & MAY_WRITE)
2768 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2769 if (mode & MAY_EXEC)
2770 args.access |= NFS4_ACCESS_EXECUTE;
2773 res.fattr = nfs_alloc_fattr();
2774 if (res.fattr == NULL)
2775 return -ENOMEM;
2777 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2778 if (!status) {
2779 nfs_access_set_mask(entry, res.access);
2780 nfs_refresh_inode(inode, res.fattr);
2782 nfs_free_fattr(res.fattr);
2783 return status;
2786 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2788 struct nfs4_exception exception = { };
2789 int err;
2790 do {
2791 err = nfs4_handle_exception(NFS_SERVER(inode),
2792 _nfs4_proc_access(inode, entry),
2793 &exception);
2794 } while (exception.retry);
2795 return err;
2799 * TODO: For the time being, we don't try to get any attributes
2800 * along with any of the zero-copy operations READ, READDIR,
2801 * READLINK, WRITE.
2803 * In the case of the first three, we want to put the GETATTR
2804 * after the read-type operation -- this is because it is hard
2805 * to predict the length of a GETATTR response in v4, and thus
2806 * align the READ data correctly. This means that the GETATTR
2807 * may end up partially falling into the page cache, and we should
2808 * shift it into the 'tail' of the xdr_buf before processing.
2809 * To do this efficiently, we need to know the total length
2810 * of data received, which doesn't seem to be available outside
2811 * of the RPC layer.
2813 * In the case of WRITE, we also want to put the GETATTR after
2814 * the operation -- in this case because we want to make sure
2815 * we get the post-operation mtime and size.
2817 * Both of these changes to the XDR layer would in fact be quite
2818 * minor, but I decided to leave them for a subsequent patch.
2820 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2821 unsigned int pgbase, unsigned int pglen)
2823 struct nfs4_readlink args = {
2824 .fh = NFS_FH(inode),
2825 .pgbase = pgbase,
2826 .pglen = pglen,
2827 .pages = &page,
2829 struct nfs4_readlink_res res;
2830 struct rpc_message msg = {
2831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2832 .rpc_argp = &args,
2833 .rpc_resp = &res,
2836 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2839 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2840 unsigned int pgbase, unsigned int pglen)
2842 struct nfs4_exception exception = { };
2843 int err;
2844 do {
2845 err = nfs4_handle_exception(NFS_SERVER(inode),
2846 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2847 &exception);
2848 } while (exception.retry);
2849 return err;
2853 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2855 static int
2856 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2857 int flags)
2859 struct nfs_open_context *ctx;
2860 struct nfs4_state *state;
2861 int status = 0;
2863 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2864 if (IS_ERR(ctx))
2865 return PTR_ERR(ctx);
2867 sattr->ia_mode &= ~current_umask();
2868 state = nfs4_do_open(dir, dentry, ctx->mode,
2869 flags, sattr, ctx->cred,
2870 &ctx->mdsthreshold);
2871 d_drop(dentry);
2872 if (IS_ERR(state)) {
2873 status = PTR_ERR(state);
2874 goto out;
2876 d_add(dentry, igrab(state->inode));
2877 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2878 ctx->state = state;
2879 out:
2880 put_nfs_open_context(ctx);
2881 return status;
2884 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2886 struct nfs_server *server = NFS_SERVER(dir);
2887 struct nfs_removeargs args = {
2888 .fh = NFS_FH(dir),
2889 .name = *name,
2891 struct nfs_removeres res = {
2892 .server = server,
2894 struct rpc_message msg = {
2895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2896 .rpc_argp = &args,
2897 .rpc_resp = &res,
2899 int status;
2901 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2902 if (status == 0)
2903 update_changeattr(dir, &res.cinfo);
2904 return status;
2907 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2909 struct nfs4_exception exception = { };
2910 int err;
2911 do {
2912 err = nfs4_handle_exception(NFS_SERVER(dir),
2913 _nfs4_proc_remove(dir, name),
2914 &exception);
2915 } while (exception.retry);
2916 return err;
2919 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2921 struct nfs_server *server = NFS_SERVER(dir);
2922 struct nfs_removeargs *args = msg->rpc_argp;
2923 struct nfs_removeres *res = msg->rpc_resp;
2925 res->server = server;
2926 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2927 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2930 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2932 nfs4_setup_sequence(NFS_SERVER(data->dir),
2933 &data->args.seq_args,
2934 &data->res.seq_res,
2935 task);
2938 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2940 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2942 if (!nfs4_sequence_done(task, &res->seq_res))
2943 return 0;
2944 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2945 return 0;
2946 update_changeattr(dir, &res->cinfo);
2947 return 1;
2950 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2952 struct nfs_server *server = NFS_SERVER(dir);
2953 struct nfs_renameargs *arg = msg->rpc_argp;
2954 struct nfs_renameres *res = msg->rpc_resp;
2956 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2957 res->server = server;
2958 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2961 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2963 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2964 &data->args.seq_args,
2965 &data->res.seq_res,
2966 task);
2969 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2970 struct inode *new_dir)
2972 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2974 if (!nfs4_sequence_done(task, &res->seq_res))
2975 return 0;
2976 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2977 return 0;
2979 update_changeattr(old_dir, &res->old_cinfo);
2980 update_changeattr(new_dir, &res->new_cinfo);
2981 return 1;
2984 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2985 struct inode *new_dir, struct qstr *new_name)
2987 struct nfs_server *server = NFS_SERVER(old_dir);
2988 struct nfs_renameargs arg = {
2989 .old_dir = NFS_FH(old_dir),
2990 .new_dir = NFS_FH(new_dir),
2991 .old_name = old_name,
2992 .new_name = new_name,
2994 struct nfs_renameres res = {
2995 .server = server,
2997 struct rpc_message msg = {
2998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2999 .rpc_argp = &arg,
3000 .rpc_resp = &res,
3002 int status = -ENOMEM;
3004 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3005 if (!status) {
3006 update_changeattr(old_dir, &res.old_cinfo);
3007 update_changeattr(new_dir, &res.new_cinfo);
3009 return status;
3012 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3013 struct inode *new_dir, struct qstr *new_name)
3015 struct nfs4_exception exception = { };
3016 int err;
3017 do {
3018 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3019 _nfs4_proc_rename(old_dir, old_name,
3020 new_dir, new_name),
3021 &exception);
3022 } while (exception.retry);
3023 return err;
3026 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3028 struct nfs_server *server = NFS_SERVER(inode);
3029 struct nfs4_link_arg arg = {
3030 .fh = NFS_FH(inode),
3031 .dir_fh = NFS_FH(dir),
3032 .name = name,
3033 .bitmask = server->attr_bitmask,
3035 struct nfs4_link_res res = {
3036 .server = server,
3038 struct rpc_message msg = {
3039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3040 .rpc_argp = &arg,
3041 .rpc_resp = &res,
3043 int status = -ENOMEM;
3045 res.fattr = nfs_alloc_fattr();
3046 if (res.fattr == NULL)
3047 goto out;
3049 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3050 if (!status) {
3051 update_changeattr(dir, &res.cinfo);
3052 nfs_post_op_update_inode(inode, res.fattr);
3054 out:
3055 nfs_free_fattr(res.fattr);
3056 return status;
3059 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3061 struct nfs4_exception exception = { };
3062 int err;
3063 do {
3064 err = nfs4_handle_exception(NFS_SERVER(inode),
3065 _nfs4_proc_link(inode, dir, name),
3066 &exception);
3067 } while (exception.retry);
3068 return err;
3071 struct nfs4_createdata {
3072 struct rpc_message msg;
3073 struct nfs4_create_arg arg;
3074 struct nfs4_create_res res;
3075 struct nfs_fh fh;
3076 struct nfs_fattr fattr;
3079 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3080 struct qstr *name, struct iattr *sattr, u32 ftype)
3082 struct nfs4_createdata *data;
3084 data = kzalloc(sizeof(*data), GFP_KERNEL);
3085 if (data != NULL) {
3086 struct nfs_server *server = NFS_SERVER(dir);
3088 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3089 data->msg.rpc_argp = &data->arg;
3090 data->msg.rpc_resp = &data->res;
3091 data->arg.dir_fh = NFS_FH(dir);
3092 data->arg.server = server;
3093 data->arg.name = name;
3094 data->arg.attrs = sattr;
3095 data->arg.ftype = ftype;
3096 data->arg.bitmask = server->attr_bitmask;
3097 data->res.server = server;
3098 data->res.fh = &data->fh;
3099 data->res.fattr = &data->fattr;
3100 nfs_fattr_init(data->res.fattr);
3102 return data;
3105 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3107 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3108 &data->arg.seq_args, &data->res.seq_res, 1);
3109 if (status == 0) {
3110 update_changeattr(dir, &data->res.dir_cinfo);
3111 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3113 return status;
3116 static void nfs4_free_createdata(struct nfs4_createdata *data)
3118 kfree(data);
3121 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3122 struct page *page, unsigned int len, struct iattr *sattr)
3124 struct nfs4_createdata *data;
3125 int status = -ENAMETOOLONG;
3127 if (len > NFS4_MAXPATHLEN)
3128 goto out;
3130 status = -ENOMEM;
3131 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3132 if (data == NULL)
3133 goto out;
3135 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3136 data->arg.u.symlink.pages = &page;
3137 data->arg.u.symlink.len = len;
3139 status = nfs4_do_create(dir, dentry, data);
3141 nfs4_free_createdata(data);
3142 out:
3143 return status;
3146 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3147 struct page *page, unsigned int len, struct iattr *sattr)
3149 struct nfs4_exception exception = { };
3150 int err;
3151 do {
3152 err = nfs4_handle_exception(NFS_SERVER(dir),
3153 _nfs4_proc_symlink(dir, dentry, page,
3154 len, sattr),
3155 &exception);
3156 } while (exception.retry);
3157 return err;
3160 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3161 struct iattr *sattr)
3163 struct nfs4_createdata *data;
3164 int status = -ENOMEM;
3166 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3167 if (data == NULL)
3168 goto out;
3170 status = nfs4_do_create(dir, dentry, data);
3172 nfs4_free_createdata(data);
3173 out:
3174 return status;
3177 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3178 struct iattr *sattr)
3180 struct nfs4_exception exception = { };
3181 int err;
3183 sattr->ia_mode &= ~current_umask();
3184 do {
3185 err = nfs4_handle_exception(NFS_SERVER(dir),
3186 _nfs4_proc_mkdir(dir, dentry, sattr),
3187 &exception);
3188 } while (exception.retry);
3189 return err;
3192 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3193 u64 cookie, struct page **pages, unsigned int count, int plus)
3195 struct inode *dir = dentry->d_inode;
3196 struct nfs4_readdir_arg args = {
3197 .fh = NFS_FH(dir),
3198 .pages = pages,
3199 .pgbase = 0,
3200 .count = count,
3201 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3202 .plus = plus,
3204 struct nfs4_readdir_res res;
3205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3207 .rpc_argp = &args,
3208 .rpc_resp = &res,
3209 .rpc_cred = cred,
3211 int status;
3213 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3214 dentry->d_parent->d_name.name,
3215 dentry->d_name.name,
3216 (unsigned long long)cookie);
3217 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3218 res.pgbase = args.pgbase;
3219 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3220 if (status >= 0) {
3221 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3222 status += args.pgbase;
3225 nfs_invalidate_atime(dir);
3227 dprintk("%s: returns %d\n", __func__, status);
3228 return status;
3231 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3232 u64 cookie, struct page **pages, unsigned int count, int plus)
3234 struct nfs4_exception exception = { };
3235 int err;
3236 do {
3237 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3238 _nfs4_proc_readdir(dentry, cred, cookie,
3239 pages, count, plus),
3240 &exception);
3241 } while (exception.retry);
3242 return err;
3245 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3246 struct iattr *sattr, dev_t rdev)
3248 struct nfs4_createdata *data;
3249 int mode = sattr->ia_mode;
3250 int status = -ENOMEM;
3252 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3253 if (data == NULL)
3254 goto out;
3256 if (S_ISFIFO(mode))
3257 data->arg.ftype = NF4FIFO;
3258 else if (S_ISBLK(mode)) {
3259 data->arg.ftype = NF4BLK;
3260 data->arg.u.device.specdata1 = MAJOR(rdev);
3261 data->arg.u.device.specdata2 = MINOR(rdev);
3263 else if (S_ISCHR(mode)) {
3264 data->arg.ftype = NF4CHR;
3265 data->arg.u.device.specdata1 = MAJOR(rdev);
3266 data->arg.u.device.specdata2 = MINOR(rdev);
3267 } else if (!S_ISSOCK(mode)) {
3268 status = -EINVAL;
3269 goto out_free;
3272 status = nfs4_do_create(dir, dentry, data);
3273 out_free:
3274 nfs4_free_createdata(data);
3275 out:
3276 return status;
3279 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3280 struct iattr *sattr, dev_t rdev)
3282 struct nfs4_exception exception = { };
3283 int err;
3285 sattr->ia_mode &= ~current_umask();
3286 do {
3287 err = nfs4_handle_exception(NFS_SERVER(dir),
3288 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3289 &exception);
3290 } while (exception.retry);
3291 return err;
3294 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3295 struct nfs_fsstat *fsstat)
3297 struct nfs4_statfs_arg args = {
3298 .fh = fhandle,
3299 .bitmask = server->attr_bitmask,
3301 struct nfs4_statfs_res res = {
3302 .fsstat = fsstat,
3304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3306 .rpc_argp = &args,
3307 .rpc_resp = &res,
3310 nfs_fattr_init(fsstat->fattr);
3311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3314 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3316 struct nfs4_exception exception = { };
3317 int err;
3318 do {
3319 err = nfs4_handle_exception(server,
3320 _nfs4_proc_statfs(server, fhandle, fsstat),
3321 &exception);
3322 } while (exception.retry);
3323 return err;
3326 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3327 struct nfs_fsinfo *fsinfo)
3329 struct nfs4_fsinfo_arg args = {
3330 .fh = fhandle,
3331 .bitmask = server->attr_bitmask,
3333 struct nfs4_fsinfo_res res = {
3334 .fsinfo = fsinfo,
3336 struct rpc_message msg = {
3337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3338 .rpc_argp = &args,
3339 .rpc_resp = &res,
3342 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3345 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3347 struct nfs4_exception exception = { };
3348 int err;
3350 do {
3351 err = nfs4_handle_exception(server,
3352 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3353 &exception);
3354 } while (exception.retry);
3355 return err;
3358 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3360 int error;
3362 nfs_fattr_init(fsinfo->fattr);
3363 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3364 if (error == 0) {
3365 /* block layout checks this! */
3366 server->pnfs_blksize = fsinfo->blksize;
3367 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3370 return error;
3373 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3374 struct nfs_pathconf *pathconf)
3376 struct nfs4_pathconf_arg args = {
3377 .fh = fhandle,
3378 .bitmask = server->attr_bitmask,
3380 struct nfs4_pathconf_res res = {
3381 .pathconf = pathconf,
3383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3385 .rpc_argp = &args,
3386 .rpc_resp = &res,
3389 /* None of the pathconf attributes are mandatory to implement */
3390 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3391 memset(pathconf, 0, sizeof(*pathconf));
3392 return 0;
3395 nfs_fattr_init(pathconf->fattr);
3396 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3399 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3400 struct nfs_pathconf *pathconf)
3402 struct nfs4_exception exception = { };
3403 int err;
3405 do {
3406 err = nfs4_handle_exception(server,
3407 _nfs4_proc_pathconf(server, fhandle, pathconf),
3408 &exception);
3409 } while (exception.retry);
3410 return err;
3413 void __nfs4_read_done_cb(struct nfs_read_data *data)
3415 nfs_invalidate_atime(data->header->inode);
3418 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3420 struct nfs_server *server = NFS_SERVER(data->header->inode);
3422 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3423 rpc_restart_call_prepare(task);
3424 return -EAGAIN;
3427 __nfs4_read_done_cb(data);
3428 if (task->tk_status > 0)
3429 renew_lease(server, data->timestamp);
3430 return 0;
3433 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3436 dprintk("--> %s\n", __func__);
3438 if (!nfs4_sequence_done(task, &data->res.seq_res))
3439 return -EAGAIN;
3441 return data->read_done_cb ? data->read_done_cb(task, data) :
3442 nfs4_read_done_cb(task, data);
3445 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3447 data->timestamp = jiffies;
3448 data->read_done_cb = nfs4_read_done_cb;
3449 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3450 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3453 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3455 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3456 &data->args.seq_args,
3457 &data->res.seq_res,
3458 task);
3461 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3463 struct inode *inode = data->header->inode;
3465 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3466 rpc_restart_call_prepare(task);
3467 return -EAGAIN;
3469 if (task->tk_status >= 0) {
3470 renew_lease(NFS_SERVER(inode), data->timestamp);
3471 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3473 return 0;
3476 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3478 if (!nfs4_sequence_done(task, &data->res.seq_res))
3479 return -EAGAIN;
3480 return data->write_done_cb ? data->write_done_cb(task, data) :
3481 nfs4_write_done_cb(task, data);
3484 static
3485 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3487 const struct nfs_pgio_header *hdr = data->header;
3489 /* Don't request attributes for pNFS or O_DIRECT writes */
3490 if (data->ds_clp != NULL || hdr->dreq != NULL)
3491 return false;
3492 /* Otherwise, request attributes if and only if we don't hold
3493 * a delegation
3495 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3498 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3500 struct nfs_server *server = NFS_SERVER(data->header->inode);
3502 if (!nfs4_write_need_cache_consistency_data(data)) {
3503 data->args.bitmask = NULL;
3504 data->res.fattr = NULL;
3505 } else
3506 data->args.bitmask = server->cache_consistency_bitmask;
3508 if (!data->write_done_cb)
3509 data->write_done_cb = nfs4_write_done_cb;
3510 data->res.server = server;
3511 data->timestamp = jiffies;
3513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3514 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3517 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3519 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3520 &data->args.seq_args,
3521 &data->res.seq_res,
3522 task);
3525 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3527 nfs4_setup_sequence(NFS_SERVER(data->inode),
3528 &data->args.seq_args,
3529 &data->res.seq_res,
3530 task);
3533 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3535 struct inode *inode = data->inode;
3537 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3538 rpc_restart_call_prepare(task);
3539 return -EAGAIN;
3541 return 0;
3544 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3546 if (!nfs4_sequence_done(task, &data->res.seq_res))
3547 return -EAGAIN;
3548 return data->commit_done_cb(task, data);
3551 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3553 struct nfs_server *server = NFS_SERVER(data->inode);
3555 if (data->commit_done_cb == NULL)
3556 data->commit_done_cb = nfs4_commit_done_cb;
3557 data->res.server = server;
3558 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3559 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3562 struct nfs4_renewdata {
3563 struct nfs_client *client;
3564 unsigned long timestamp;
3568 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3569 * standalone procedure for queueing an asynchronous RENEW.
3571 static void nfs4_renew_release(void *calldata)
3573 struct nfs4_renewdata *data = calldata;
3574 struct nfs_client *clp = data->client;
3576 if (atomic_read(&clp->cl_count) > 1)
3577 nfs4_schedule_state_renewal(clp);
3578 nfs_put_client(clp);
3579 kfree(data);
3582 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3584 struct nfs4_renewdata *data = calldata;
3585 struct nfs_client *clp = data->client;
3586 unsigned long timestamp = data->timestamp;
3588 if (task->tk_status < 0) {
3589 /* Unless we're shutting down, schedule state recovery! */
3590 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3591 return;
3592 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3593 nfs4_schedule_lease_recovery(clp);
3594 return;
3596 nfs4_schedule_path_down_recovery(clp);
3598 do_renew_lease(clp, timestamp);
3601 static const struct rpc_call_ops nfs4_renew_ops = {
3602 .rpc_call_done = nfs4_renew_done,
3603 .rpc_release = nfs4_renew_release,
3606 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3610 .rpc_argp = clp,
3611 .rpc_cred = cred,
3613 struct nfs4_renewdata *data;
3615 if (renew_flags == 0)
3616 return 0;
3617 if (!atomic_inc_not_zero(&clp->cl_count))
3618 return -EIO;
3619 data = kmalloc(sizeof(*data), GFP_NOFS);
3620 if (data == NULL)
3621 return -ENOMEM;
3622 data->client = clp;
3623 data->timestamp = jiffies;
3624 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3625 &nfs4_renew_ops, data);
3628 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3630 struct rpc_message msg = {
3631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3632 .rpc_argp = clp,
3633 .rpc_cred = cred,
3635 unsigned long now = jiffies;
3636 int status;
3638 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3639 if (status < 0)
3640 return status;
3641 do_renew_lease(clp, now);
3642 return 0;
3645 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3647 return (server->caps & NFS_CAP_ACLS)
3648 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3649 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3652 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3653 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3654 * the stack.
3656 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3658 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3659 struct page **pages, unsigned int *pgbase)
3661 struct page *newpage, **spages;
3662 int rc = 0;
3663 size_t len;
3664 spages = pages;
3666 do {
3667 len = min_t(size_t, PAGE_SIZE, buflen);
3668 newpage = alloc_page(GFP_KERNEL);
3670 if (newpage == NULL)
3671 goto unwind;
3672 memcpy(page_address(newpage), buf, len);
3673 buf += len;
3674 buflen -= len;
3675 *pages++ = newpage;
3676 rc++;
3677 } while (buflen != 0);
3679 return rc;
3681 unwind:
3682 for(; rc > 0; rc--)
3683 __free_page(spages[rc-1]);
3684 return -ENOMEM;
3687 struct nfs4_cached_acl {
3688 int cached;
3689 size_t len;
3690 char data[0];
3693 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3695 struct nfs_inode *nfsi = NFS_I(inode);
3697 spin_lock(&inode->i_lock);
3698 kfree(nfsi->nfs4_acl);
3699 nfsi->nfs4_acl = acl;
3700 spin_unlock(&inode->i_lock);
3703 static void nfs4_zap_acl_attr(struct inode *inode)
3705 nfs4_set_cached_acl(inode, NULL);
3708 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3710 struct nfs_inode *nfsi = NFS_I(inode);
3711 struct nfs4_cached_acl *acl;
3712 int ret = -ENOENT;
3714 spin_lock(&inode->i_lock);
3715 acl = nfsi->nfs4_acl;
3716 if (acl == NULL)
3717 goto out;
3718 if (buf == NULL) /* user is just asking for length */
3719 goto out_len;
3720 if (acl->cached == 0)
3721 goto out;
3722 ret = -ERANGE; /* see getxattr(2) man page */
3723 if (acl->len > buflen)
3724 goto out;
3725 memcpy(buf, acl->data, acl->len);
3726 out_len:
3727 ret = acl->len;
3728 out:
3729 spin_unlock(&inode->i_lock);
3730 return ret;
3733 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3735 struct nfs4_cached_acl *acl;
3736 size_t buflen = sizeof(*acl) + acl_len;
3738 if (buflen <= PAGE_SIZE) {
3739 acl = kmalloc(buflen, GFP_KERNEL);
3740 if (acl == NULL)
3741 goto out;
3742 acl->cached = 1;
3743 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3744 } else {
3745 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3746 if (acl == NULL)
3747 goto out;
3748 acl->cached = 0;
3750 acl->len = acl_len;
3751 out:
3752 nfs4_set_cached_acl(inode, acl);
3756 * The getxattr API returns the required buffer length when called with a
3757 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3758 * the required buf. On a NULL buf, we send a page of data to the server
3759 * guessing that the ACL request can be serviced by a page. If so, we cache
3760 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3761 * the cache. If not so, we throw away the page, and cache the required
3762 * length. The next getxattr call will then produce another round trip to
3763 * the server, this time with the input buf of the required size.
3765 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3767 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3768 struct nfs_getaclargs args = {
3769 .fh = NFS_FH(inode),
3770 .acl_pages = pages,
3771 .acl_len = buflen,
3773 struct nfs_getaclres res = {
3774 .acl_len = buflen,
3776 struct rpc_message msg = {
3777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3778 .rpc_argp = &args,
3779 .rpc_resp = &res,
3781 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3782 int ret = -ENOMEM, i;
3784 /* As long as we're doing a round trip to the server anyway,
3785 * let's be prepared for a page of acl data. */
3786 if (npages == 0)
3787 npages = 1;
3788 if (npages > ARRAY_SIZE(pages))
3789 return -ERANGE;
3791 for (i = 0; i < npages; i++) {
3792 pages[i] = alloc_page(GFP_KERNEL);
3793 if (!pages[i])
3794 goto out_free;
3797 /* for decoding across pages */
3798 res.acl_scratch = alloc_page(GFP_KERNEL);
3799 if (!res.acl_scratch)
3800 goto out_free;
3802 args.acl_len = npages * PAGE_SIZE;
3803 args.acl_pgbase = 0;
3805 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3806 __func__, buf, buflen, npages, args.acl_len);
3807 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3808 &msg, &args.seq_args, &res.seq_res, 0);
3809 if (ret)
3810 goto out_free;
3812 /* Handle the case where the passed-in buffer is too short */
3813 if (res.acl_flags & NFS4_ACL_TRUNC) {
3814 /* Did the user only issue a request for the acl length? */
3815 if (buf == NULL)
3816 goto out_ok;
3817 ret = -ERANGE;
3818 goto out_free;
3820 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3821 if (buf) {
3822 if (res.acl_len > buflen) {
3823 ret = -ERANGE;
3824 goto out_free;
3826 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3828 out_ok:
3829 ret = res.acl_len;
3830 out_free:
3831 for (i = 0; i < npages; i++)
3832 if (pages[i])
3833 __free_page(pages[i]);
3834 if (res.acl_scratch)
3835 __free_page(res.acl_scratch);
3836 return ret;
3839 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3841 struct nfs4_exception exception = { };
3842 ssize_t ret;
3843 do {
3844 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3845 if (ret >= 0)
3846 break;
3847 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3848 } while (exception.retry);
3849 return ret;
3852 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3854 struct nfs_server *server = NFS_SERVER(inode);
3855 int ret;
3857 if (!nfs4_server_supports_acls(server))
3858 return -EOPNOTSUPP;
3859 ret = nfs_revalidate_inode(server, inode);
3860 if (ret < 0)
3861 return ret;
3862 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3863 nfs_zap_acl_cache(inode);
3864 ret = nfs4_read_cached_acl(inode, buf, buflen);
3865 if (ret != -ENOENT)
3866 /* -ENOENT is returned if there is no ACL or if there is an ACL
3867 * but no cached acl data, just the acl length */
3868 return ret;
3869 return nfs4_get_acl_uncached(inode, buf, buflen);
3872 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3874 struct nfs_server *server = NFS_SERVER(inode);
3875 struct page *pages[NFS4ACL_MAXPAGES];
3876 struct nfs_setaclargs arg = {
3877 .fh = NFS_FH(inode),
3878 .acl_pages = pages,
3879 .acl_len = buflen,
3881 struct nfs_setaclres res;
3882 struct rpc_message msg = {
3883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3884 .rpc_argp = &arg,
3885 .rpc_resp = &res,
3887 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3888 int ret, i;
3890 if (!nfs4_server_supports_acls(server))
3891 return -EOPNOTSUPP;
3892 if (npages > ARRAY_SIZE(pages))
3893 return -ERANGE;
3894 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3895 if (i < 0)
3896 return i;
3897 nfs4_inode_return_delegation(inode);
3898 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3901 * Free each page after tx, so the only ref left is
3902 * held by the network stack
3904 for (; i > 0; i--)
3905 put_page(pages[i-1]);
3908 * Acl update can result in inode attribute update.
3909 * so mark the attribute cache invalid.
3911 spin_lock(&inode->i_lock);
3912 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3913 spin_unlock(&inode->i_lock);
3914 nfs_access_zap_cache(inode);
3915 nfs_zap_acl_cache(inode);
3916 return ret;
3919 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3921 struct nfs4_exception exception = { };
3922 int err;
3923 do {
3924 err = nfs4_handle_exception(NFS_SERVER(inode),
3925 __nfs4_proc_set_acl(inode, buf, buflen),
3926 &exception);
3927 } while (exception.retry);
3928 return err;
3931 static int
3932 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3934 struct nfs_client *clp = server->nfs_client;
3936 if (task->tk_status >= 0)
3937 return 0;
3938 switch(task->tk_status) {
3939 case -NFS4ERR_DELEG_REVOKED:
3940 case -NFS4ERR_ADMIN_REVOKED:
3941 case -NFS4ERR_BAD_STATEID:
3942 if (state == NULL)
3943 break;
3944 nfs_remove_bad_delegation(state->inode);
3945 case -NFS4ERR_OPENMODE:
3946 if (state == NULL)
3947 break;
3948 nfs4_schedule_stateid_recovery(server, state);
3949 goto wait_on_recovery;
3950 case -NFS4ERR_EXPIRED:
3951 if (state != NULL)
3952 nfs4_schedule_stateid_recovery(server, state);
3953 case -NFS4ERR_STALE_STATEID:
3954 case -NFS4ERR_STALE_CLIENTID:
3955 nfs4_schedule_lease_recovery(clp);
3956 goto wait_on_recovery;
3957 #if defined(CONFIG_NFS_V4_1)
3958 case -NFS4ERR_BADSESSION:
3959 case -NFS4ERR_BADSLOT:
3960 case -NFS4ERR_BAD_HIGH_SLOT:
3961 case -NFS4ERR_DEADSESSION:
3962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3963 case -NFS4ERR_SEQ_FALSE_RETRY:
3964 case -NFS4ERR_SEQ_MISORDERED:
3965 dprintk("%s ERROR %d, Reset session\n", __func__,
3966 task->tk_status);
3967 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
3968 task->tk_status = 0;
3969 return -EAGAIN;
3970 #endif /* CONFIG_NFS_V4_1 */
3971 case -NFS4ERR_DELAY:
3972 nfs_inc_server_stats(server, NFSIOS_DELAY);
3973 case -NFS4ERR_GRACE:
3974 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3975 task->tk_status = 0;
3976 return -EAGAIN;
3977 case -NFS4ERR_RETRY_UNCACHED_REP:
3978 case -NFS4ERR_OLD_STATEID:
3979 task->tk_status = 0;
3980 return -EAGAIN;
3982 task->tk_status = nfs4_map_errors(task->tk_status);
3983 return 0;
3984 wait_on_recovery:
3985 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3986 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3987 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3988 task->tk_status = 0;
3989 return -EAGAIN;
3992 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3993 nfs4_verifier *bootverf)
3995 __be32 verf[2];
3997 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3998 /* An impossible timestamp guarantees this value
3999 * will never match a generated boot time. */
4000 verf[0] = 0;
4001 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4002 } else {
4003 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4004 verf[0] = (__be32)nn->boot_time.tv_sec;
4005 verf[1] = (__be32)nn->boot_time.tv_nsec;
4007 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4010 static unsigned int
4011 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4012 char *buf, size_t len)
4014 unsigned int result;
4016 rcu_read_lock();
4017 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4018 clp->cl_ipaddr,
4019 rpc_peeraddr2str(clp->cl_rpcclient,
4020 RPC_DISPLAY_ADDR),
4021 rpc_peeraddr2str(clp->cl_rpcclient,
4022 RPC_DISPLAY_PROTO));
4023 rcu_read_unlock();
4024 return result;
4027 static unsigned int
4028 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4029 char *buf, size_t len)
4031 char *nodename = clp->cl_rpcclient->cl_nodename;
4033 if (nfs4_client_id_uniquifier[0] != '\0')
4034 nodename = nfs4_client_id_uniquifier;
4035 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4036 clp->rpc_ops->version, clp->cl_minorversion,
4037 nodename);
4041 * nfs4_proc_setclientid - Negotiate client ID
4042 * @clp: state data structure
4043 * @program: RPC program for NFSv4 callback service
4044 * @port: IP port number for NFS4 callback service
4045 * @cred: RPC credential to use for this call
4046 * @res: where to place the result
4048 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4050 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4051 unsigned short port, struct rpc_cred *cred,
4052 struct nfs4_setclientid_res *res)
4054 nfs4_verifier sc_verifier;
4055 struct nfs4_setclientid setclientid = {
4056 .sc_verifier = &sc_verifier,
4057 .sc_prog = program,
4058 .sc_cb_ident = clp->cl_cb_ident,
4060 struct rpc_message msg = {
4061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4062 .rpc_argp = &setclientid,
4063 .rpc_resp = res,
4064 .rpc_cred = cred,
4066 int status;
4068 /* nfs_client_id4 */
4069 nfs4_init_boot_verifier(clp, &sc_verifier);
4070 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4071 setclientid.sc_name_len =
4072 nfs4_init_uniform_client_string(clp,
4073 setclientid.sc_name,
4074 sizeof(setclientid.sc_name));
4075 else
4076 setclientid.sc_name_len =
4077 nfs4_init_nonuniform_client_string(clp,
4078 setclientid.sc_name,
4079 sizeof(setclientid.sc_name));
4080 /* cb_client4 */
4081 rcu_read_lock();
4082 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4083 sizeof(setclientid.sc_netid),
4084 rpc_peeraddr2str(clp->cl_rpcclient,
4085 RPC_DISPLAY_NETID));
4086 rcu_read_unlock();
4087 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4088 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4089 clp->cl_ipaddr, port >> 8, port & 255);
4091 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4092 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4093 setclientid.sc_name_len, setclientid.sc_name);
4094 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4095 dprintk("NFS reply setclientid: %d\n", status);
4096 return status;
4100 * nfs4_proc_setclientid_confirm - Confirm client ID
4101 * @clp: state data structure
4102 * @res: result of a previous SETCLIENTID
4103 * @cred: RPC credential to use for this call
4105 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4107 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4108 struct nfs4_setclientid_res *arg,
4109 struct rpc_cred *cred)
4111 struct nfs_fsinfo fsinfo;
4112 struct rpc_message msg = {
4113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4114 .rpc_argp = arg,
4115 .rpc_resp = &fsinfo,
4116 .rpc_cred = cred,
4118 unsigned long now;
4119 int status;
4121 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4122 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4123 clp->cl_clientid);
4124 now = jiffies;
4125 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4126 if (status == 0) {
4127 spin_lock(&clp->cl_lock);
4128 clp->cl_lease_time = fsinfo.lease_time * HZ;
4129 clp->cl_last_renewal = now;
4130 spin_unlock(&clp->cl_lock);
4132 dprintk("NFS reply setclientid_confirm: %d\n", status);
4133 return status;
4136 struct nfs4_delegreturndata {
4137 struct nfs4_delegreturnargs args;
4138 struct nfs4_delegreturnres res;
4139 struct nfs_fh fh;
4140 nfs4_stateid stateid;
4141 unsigned long timestamp;
4142 struct nfs_fattr fattr;
4143 int rpc_status;
4146 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4148 struct nfs4_delegreturndata *data = calldata;
4150 if (!nfs4_sequence_done(task, &data->res.seq_res))
4151 return;
4153 switch (task->tk_status) {
4154 case -NFS4ERR_STALE_STATEID:
4155 case -NFS4ERR_EXPIRED:
4156 case 0:
4157 renew_lease(data->res.server, data->timestamp);
4158 break;
4159 default:
4160 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4161 -EAGAIN) {
4162 rpc_restart_call_prepare(task);
4163 return;
4166 data->rpc_status = task->tk_status;
4169 static void nfs4_delegreturn_release(void *calldata)
4171 kfree(calldata);
4174 #if defined(CONFIG_NFS_V4_1)
4175 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4177 struct nfs4_delegreturndata *d_data;
4179 d_data = (struct nfs4_delegreturndata *)data;
4181 nfs4_setup_sequence(d_data->res.server,
4182 &d_data->args.seq_args,
4183 &d_data->res.seq_res,
4184 task);
4186 #endif /* CONFIG_NFS_V4_1 */
4188 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4189 #if defined(CONFIG_NFS_V4_1)
4190 .rpc_call_prepare = nfs4_delegreturn_prepare,
4191 #endif /* CONFIG_NFS_V4_1 */
4192 .rpc_call_done = nfs4_delegreturn_done,
4193 .rpc_release = nfs4_delegreturn_release,
4196 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4198 struct nfs4_delegreturndata *data;
4199 struct nfs_server *server = NFS_SERVER(inode);
4200 struct rpc_task *task;
4201 struct rpc_message msg = {
4202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4203 .rpc_cred = cred,
4205 struct rpc_task_setup task_setup_data = {
4206 .rpc_client = server->client,
4207 .rpc_message = &msg,
4208 .callback_ops = &nfs4_delegreturn_ops,
4209 .flags = RPC_TASK_ASYNC,
4211 int status = 0;
4213 data = kzalloc(sizeof(*data), GFP_NOFS);
4214 if (data == NULL)
4215 return -ENOMEM;
4216 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4217 data->args.fhandle = &data->fh;
4218 data->args.stateid = &data->stateid;
4219 data->args.bitmask = server->cache_consistency_bitmask;
4220 nfs_copy_fh(&data->fh, NFS_FH(inode));
4221 nfs4_stateid_copy(&data->stateid, stateid);
4222 data->res.fattr = &data->fattr;
4223 data->res.server = server;
4224 nfs_fattr_init(data->res.fattr);
4225 data->timestamp = jiffies;
4226 data->rpc_status = 0;
4228 task_setup_data.callback_data = data;
4229 msg.rpc_argp = &data->args;
4230 msg.rpc_resp = &data->res;
4231 task = rpc_run_task(&task_setup_data);
4232 if (IS_ERR(task))
4233 return PTR_ERR(task);
4234 if (!issync)
4235 goto out;
4236 status = nfs4_wait_for_completion_rpc_task(task);
4237 if (status != 0)
4238 goto out;
4239 status = data->rpc_status;
4240 if (status == 0)
4241 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4242 else
4243 nfs_refresh_inode(inode, &data->fattr);
4244 out:
4245 rpc_put_task(task);
4246 return status;
4249 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4251 struct nfs_server *server = NFS_SERVER(inode);
4252 struct nfs4_exception exception = { };
4253 int err;
4254 do {
4255 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4256 switch (err) {
4257 case -NFS4ERR_STALE_STATEID:
4258 case -NFS4ERR_EXPIRED:
4259 case 0:
4260 return 0;
4262 err = nfs4_handle_exception(server, err, &exception);
4263 } while (exception.retry);
4264 return err;
4267 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4268 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4271 * sleep, with exponential backoff, and retry the LOCK operation.
4273 static unsigned long
4274 nfs4_set_lock_task_retry(unsigned long timeout)
4276 freezable_schedule_timeout_killable(timeout);
4277 timeout <<= 1;
4278 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4279 return NFS4_LOCK_MAXTIMEOUT;
4280 return timeout;
4283 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4285 struct inode *inode = state->inode;
4286 struct nfs_server *server = NFS_SERVER(inode);
4287 struct nfs_client *clp = server->nfs_client;
4288 struct nfs_lockt_args arg = {
4289 .fh = NFS_FH(inode),
4290 .fl = request,
4292 struct nfs_lockt_res res = {
4293 .denied = request,
4295 struct rpc_message msg = {
4296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4297 .rpc_argp = &arg,
4298 .rpc_resp = &res,
4299 .rpc_cred = state->owner->so_cred,
4301 struct nfs4_lock_state *lsp;
4302 int status;
4304 arg.lock_owner.clientid = clp->cl_clientid;
4305 status = nfs4_set_lock_state(state, request);
4306 if (status != 0)
4307 goto out;
4308 lsp = request->fl_u.nfs4_fl.owner;
4309 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4310 arg.lock_owner.s_dev = server->s_dev;
4311 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4312 switch (status) {
4313 case 0:
4314 request->fl_type = F_UNLCK;
4315 break;
4316 case -NFS4ERR_DENIED:
4317 status = 0;
4319 request->fl_ops->fl_release_private(request);
4320 out:
4321 return status;
4324 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4326 struct nfs4_exception exception = { };
4327 int err;
4329 do {
4330 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4331 _nfs4_proc_getlk(state, cmd, request),
4332 &exception);
4333 } while (exception.retry);
4334 return err;
4337 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4339 int res = 0;
4340 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4341 case FL_POSIX:
4342 res = posix_lock_file_wait(file, fl);
4343 break;
4344 case FL_FLOCK:
4345 res = flock_lock_file_wait(file, fl);
4346 break;
4347 default:
4348 BUG();
4350 return res;
4353 struct nfs4_unlockdata {
4354 struct nfs_locku_args arg;
4355 struct nfs_locku_res res;
4356 struct nfs4_lock_state *lsp;
4357 struct nfs_open_context *ctx;
4358 struct file_lock fl;
4359 const struct nfs_server *server;
4360 unsigned long timestamp;
4363 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4364 struct nfs_open_context *ctx,
4365 struct nfs4_lock_state *lsp,
4366 struct nfs_seqid *seqid)
4368 struct nfs4_unlockdata *p;
4369 struct inode *inode = lsp->ls_state->inode;
4371 p = kzalloc(sizeof(*p), GFP_NOFS);
4372 if (p == NULL)
4373 return NULL;
4374 p->arg.fh = NFS_FH(inode);
4375 p->arg.fl = &p->fl;
4376 p->arg.seqid = seqid;
4377 p->res.seqid = seqid;
4378 p->arg.stateid = &lsp->ls_stateid;
4379 p->lsp = lsp;
4380 atomic_inc(&lsp->ls_count);
4381 /* Ensure we don't close file until we're done freeing locks! */
4382 p->ctx = get_nfs_open_context(ctx);
4383 memcpy(&p->fl, fl, sizeof(p->fl));
4384 p->server = NFS_SERVER(inode);
4385 return p;
4388 static void nfs4_locku_release_calldata(void *data)
4390 struct nfs4_unlockdata *calldata = data;
4391 nfs_free_seqid(calldata->arg.seqid);
4392 nfs4_put_lock_state(calldata->lsp);
4393 put_nfs_open_context(calldata->ctx);
4394 kfree(calldata);
4397 static void nfs4_locku_done(struct rpc_task *task, void *data)
4399 struct nfs4_unlockdata *calldata = data;
4401 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4402 return;
4403 switch (task->tk_status) {
4404 case 0:
4405 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4406 &calldata->res.stateid);
4407 renew_lease(calldata->server, calldata->timestamp);
4408 break;
4409 case -NFS4ERR_BAD_STATEID:
4410 case -NFS4ERR_OLD_STATEID:
4411 case -NFS4ERR_STALE_STATEID:
4412 case -NFS4ERR_EXPIRED:
4413 break;
4414 default:
4415 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4416 rpc_restart_call_prepare(task);
4418 nfs_release_seqid(calldata->arg.seqid);
4421 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4423 struct nfs4_unlockdata *calldata = data;
4425 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4426 return;
4427 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4428 /* Note: exit _without_ running nfs4_locku_done */
4429 task->tk_action = NULL;
4430 nfs4_sequence_done(task, &calldata->res.seq_res);
4431 return;
4433 calldata->timestamp = jiffies;
4434 if (nfs4_setup_sequence(calldata->server,
4435 &calldata->arg.seq_args,
4436 &calldata->res.seq_res,
4437 task) != 0)
4438 nfs_release_seqid(calldata->arg.seqid);
4441 static const struct rpc_call_ops nfs4_locku_ops = {
4442 .rpc_call_prepare = nfs4_locku_prepare,
4443 .rpc_call_done = nfs4_locku_done,
4444 .rpc_release = nfs4_locku_release_calldata,
4447 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4448 struct nfs_open_context *ctx,
4449 struct nfs4_lock_state *lsp,
4450 struct nfs_seqid *seqid)
4452 struct nfs4_unlockdata *data;
4453 struct rpc_message msg = {
4454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4455 .rpc_cred = ctx->cred,
4457 struct rpc_task_setup task_setup_data = {
4458 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4459 .rpc_message = &msg,
4460 .callback_ops = &nfs4_locku_ops,
4461 .workqueue = nfsiod_workqueue,
4462 .flags = RPC_TASK_ASYNC,
4465 /* Ensure this is an unlock - when canceling a lock, the
4466 * canceled lock is passed in, and it won't be an unlock.
4468 fl->fl_type = F_UNLCK;
4470 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4471 if (data == NULL) {
4472 nfs_free_seqid(seqid);
4473 return ERR_PTR(-ENOMEM);
4476 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4477 msg.rpc_argp = &data->arg;
4478 msg.rpc_resp = &data->res;
4479 task_setup_data.callback_data = data;
4480 return rpc_run_task(&task_setup_data);
4483 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4485 struct nfs_inode *nfsi = NFS_I(state->inode);
4486 struct nfs_seqid *seqid;
4487 struct nfs4_lock_state *lsp;
4488 struct rpc_task *task;
4489 int status = 0;
4490 unsigned char fl_flags = request->fl_flags;
4492 status = nfs4_set_lock_state(state, request);
4493 /* Unlock _before_ we do the RPC call */
4494 request->fl_flags |= FL_EXISTS;
4495 down_read(&nfsi->rwsem);
4496 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4497 up_read(&nfsi->rwsem);
4498 goto out;
4500 up_read(&nfsi->rwsem);
4501 if (status != 0)
4502 goto out;
4503 /* Is this a delegated lock? */
4504 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4505 goto out;
4506 lsp = request->fl_u.nfs4_fl.owner;
4507 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4508 status = -ENOMEM;
4509 if (seqid == NULL)
4510 goto out;
4511 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4512 status = PTR_ERR(task);
4513 if (IS_ERR(task))
4514 goto out;
4515 status = nfs4_wait_for_completion_rpc_task(task);
4516 rpc_put_task(task);
4517 out:
4518 request->fl_flags = fl_flags;
4519 return status;
4522 struct nfs4_lockdata {
4523 struct nfs_lock_args arg;
4524 struct nfs_lock_res res;
4525 struct nfs4_lock_state *lsp;
4526 struct nfs_open_context *ctx;
4527 struct file_lock fl;
4528 unsigned long timestamp;
4529 int rpc_status;
4530 int cancelled;
4531 struct nfs_server *server;
4534 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4535 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4536 gfp_t gfp_mask)
4538 struct nfs4_lockdata *p;
4539 struct inode *inode = lsp->ls_state->inode;
4540 struct nfs_server *server = NFS_SERVER(inode);
4542 p = kzalloc(sizeof(*p), gfp_mask);
4543 if (p == NULL)
4544 return NULL;
4546 p->arg.fh = NFS_FH(inode);
4547 p->arg.fl = &p->fl;
4548 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4549 if (p->arg.open_seqid == NULL)
4550 goto out_free;
4551 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4552 if (p->arg.lock_seqid == NULL)
4553 goto out_free_seqid;
4554 p->arg.lock_stateid = &lsp->ls_stateid;
4555 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4556 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4557 p->arg.lock_owner.s_dev = server->s_dev;
4558 p->res.lock_seqid = p->arg.lock_seqid;
4559 p->lsp = lsp;
4560 p->server = server;
4561 atomic_inc(&lsp->ls_count);
4562 p->ctx = get_nfs_open_context(ctx);
4563 memcpy(&p->fl, fl, sizeof(p->fl));
4564 return p;
4565 out_free_seqid:
4566 nfs_free_seqid(p->arg.open_seqid);
4567 out_free:
4568 kfree(p);
4569 return NULL;
4572 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4574 struct nfs4_lockdata *data = calldata;
4575 struct nfs4_state *state = data->lsp->ls_state;
4577 dprintk("%s: begin!\n", __func__);
4578 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4579 return;
4580 /* Do we need to do an open_to_lock_owner? */
4581 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4582 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4583 goto out_release_lock_seqid;
4585 data->arg.open_stateid = &state->stateid;
4586 data->arg.new_lock_owner = 1;
4587 data->res.open_seqid = data->arg.open_seqid;
4588 } else
4589 data->arg.new_lock_owner = 0;
4590 data->timestamp = jiffies;
4591 if (nfs4_setup_sequence(data->server,
4592 &data->arg.seq_args,
4593 &data->res.seq_res,
4594 task) == 0)
4595 return;
4596 nfs_release_seqid(data->arg.open_seqid);
4597 out_release_lock_seqid:
4598 nfs_release_seqid(data->arg.lock_seqid);
4599 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4602 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4604 struct nfs4_lockdata *data = calldata;
4606 dprintk("%s: begin!\n", __func__);
4608 if (!nfs4_sequence_done(task, &data->res.seq_res))
4609 return;
4611 data->rpc_status = task->tk_status;
4612 if (data->arg.new_lock_owner != 0) {
4613 if (data->rpc_status == 0)
4614 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4615 else
4616 goto out;
4618 if (data->rpc_status == 0) {
4619 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4620 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4621 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4623 out:
4624 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4627 static void nfs4_lock_release(void *calldata)
4629 struct nfs4_lockdata *data = calldata;
4631 dprintk("%s: begin!\n", __func__);
4632 nfs_free_seqid(data->arg.open_seqid);
4633 if (data->cancelled != 0) {
4634 struct rpc_task *task;
4635 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4636 data->arg.lock_seqid);
4637 if (!IS_ERR(task))
4638 rpc_put_task_async(task);
4639 dprintk("%s: cancelling lock!\n", __func__);
4640 } else
4641 nfs_free_seqid(data->arg.lock_seqid);
4642 nfs4_put_lock_state(data->lsp);
4643 put_nfs_open_context(data->ctx);
4644 kfree(data);
4645 dprintk("%s: done!\n", __func__);
4648 static const struct rpc_call_ops nfs4_lock_ops = {
4649 .rpc_call_prepare = nfs4_lock_prepare,
4650 .rpc_call_done = nfs4_lock_done,
4651 .rpc_release = nfs4_lock_release,
4654 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4656 switch (error) {
4657 case -NFS4ERR_ADMIN_REVOKED:
4658 case -NFS4ERR_BAD_STATEID:
4659 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4660 if (new_lock_owner != 0 ||
4661 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4662 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4663 break;
4664 case -NFS4ERR_STALE_STATEID:
4665 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4666 case -NFS4ERR_EXPIRED:
4667 nfs4_schedule_lease_recovery(server->nfs_client);
4671 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4673 struct nfs4_lockdata *data;
4674 struct rpc_task *task;
4675 struct rpc_message msg = {
4676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4677 .rpc_cred = state->owner->so_cred,
4679 struct rpc_task_setup task_setup_data = {
4680 .rpc_client = NFS_CLIENT(state->inode),
4681 .rpc_message = &msg,
4682 .callback_ops = &nfs4_lock_ops,
4683 .workqueue = nfsiod_workqueue,
4684 .flags = RPC_TASK_ASYNC,
4686 int ret;
4688 dprintk("%s: begin!\n", __func__);
4689 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4690 fl->fl_u.nfs4_fl.owner,
4691 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4692 if (data == NULL)
4693 return -ENOMEM;
4694 if (IS_SETLKW(cmd))
4695 data->arg.block = 1;
4696 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4697 msg.rpc_argp = &data->arg;
4698 msg.rpc_resp = &data->res;
4699 task_setup_data.callback_data = data;
4700 if (recovery_type > NFS_LOCK_NEW) {
4701 if (recovery_type == NFS_LOCK_RECLAIM)
4702 data->arg.reclaim = NFS_LOCK_RECLAIM;
4703 nfs4_set_sequence_privileged(&data->arg.seq_args);
4705 task = rpc_run_task(&task_setup_data);
4706 if (IS_ERR(task))
4707 return PTR_ERR(task);
4708 ret = nfs4_wait_for_completion_rpc_task(task);
4709 if (ret == 0) {
4710 ret = data->rpc_status;
4711 if (ret)
4712 nfs4_handle_setlk_error(data->server, data->lsp,
4713 data->arg.new_lock_owner, ret);
4714 } else
4715 data->cancelled = 1;
4716 rpc_put_task(task);
4717 dprintk("%s: done, ret = %d!\n", __func__, ret);
4718 return ret;
4721 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4723 struct nfs_server *server = NFS_SERVER(state->inode);
4724 struct nfs4_exception exception = {
4725 .inode = state->inode,
4727 int err;
4729 do {
4730 /* Cache the lock if possible... */
4731 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4732 return 0;
4733 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4734 if (err != -NFS4ERR_DELAY)
4735 break;
4736 nfs4_handle_exception(server, err, &exception);
4737 } while (exception.retry);
4738 return err;
4741 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4743 struct nfs_server *server = NFS_SERVER(state->inode);
4744 struct nfs4_exception exception = {
4745 .inode = state->inode,
4747 int err;
4749 err = nfs4_set_lock_state(state, request);
4750 if (err != 0)
4751 return err;
4752 do {
4753 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4754 return 0;
4755 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4756 switch (err) {
4757 default:
4758 goto out;
4759 case -NFS4ERR_GRACE:
4760 case -NFS4ERR_DELAY:
4761 nfs4_handle_exception(server, err, &exception);
4762 err = 0;
4764 } while (exception.retry);
4765 out:
4766 return err;
4769 #if defined(CONFIG_NFS_V4_1)
4771 * nfs41_check_expired_locks - possibly free a lock stateid
4773 * @state: NFSv4 state for an inode
4775 * Returns NFS_OK if recovery for this stateid is now finished.
4776 * Otherwise a negative NFS4ERR value is returned.
4778 static int nfs41_check_expired_locks(struct nfs4_state *state)
4780 int status, ret = -NFS4ERR_BAD_STATEID;
4781 struct nfs4_lock_state *lsp;
4782 struct nfs_server *server = NFS_SERVER(state->inode);
4784 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4785 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4786 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4787 if (status != NFS_OK) {
4788 /* Free the stateid unless the server
4789 * informs us the stateid is unrecognized. */
4790 if (status != -NFS4ERR_BAD_STATEID)
4791 nfs41_free_stateid(server,
4792 &lsp->ls_stateid);
4793 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4794 ret = status;
4799 return ret;
4802 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4804 int status = NFS_OK;
4806 if (test_bit(LK_STATE_IN_USE, &state->flags))
4807 status = nfs41_check_expired_locks(state);
4808 if (status != NFS_OK)
4809 status = nfs4_lock_expired(state, request);
4810 return status;
4812 #endif
4814 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4816 struct nfs_inode *nfsi = NFS_I(state->inode);
4817 unsigned char fl_flags = request->fl_flags;
4818 int status = -ENOLCK;
4820 if ((fl_flags & FL_POSIX) &&
4821 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4822 goto out;
4823 /* Is this a delegated open? */
4824 status = nfs4_set_lock_state(state, request);
4825 if (status != 0)
4826 goto out;
4827 request->fl_flags |= FL_ACCESS;
4828 status = do_vfs_lock(request->fl_file, request);
4829 if (status < 0)
4830 goto out;
4831 down_read(&nfsi->rwsem);
4832 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4833 /* Yes: cache locks! */
4834 /* ...but avoid races with delegation recall... */
4835 request->fl_flags = fl_flags & ~FL_SLEEP;
4836 status = do_vfs_lock(request->fl_file, request);
4837 goto out_unlock;
4839 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4840 if (status != 0)
4841 goto out_unlock;
4842 /* Note: we always want to sleep here! */
4843 request->fl_flags = fl_flags | FL_SLEEP;
4844 if (do_vfs_lock(request->fl_file, request) < 0)
4845 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4846 "manager!\n", __func__);
4847 out_unlock:
4848 up_read(&nfsi->rwsem);
4849 out:
4850 request->fl_flags = fl_flags;
4851 return status;
4854 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4856 struct nfs4_exception exception = {
4857 .state = state,
4858 .inode = state->inode,
4860 int err;
4862 do {
4863 err = _nfs4_proc_setlk(state, cmd, request);
4864 if (err == -NFS4ERR_DENIED)
4865 err = -EAGAIN;
4866 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4867 err, &exception);
4868 } while (exception.retry);
4869 return err;
4872 static int
4873 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4875 struct nfs_open_context *ctx;
4876 struct nfs4_state *state;
4877 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4878 int status;
4880 /* verify open state */
4881 ctx = nfs_file_open_context(filp);
4882 state = ctx->state;
4884 if (request->fl_start < 0 || request->fl_end < 0)
4885 return -EINVAL;
4887 if (IS_GETLK(cmd)) {
4888 if (state != NULL)
4889 return nfs4_proc_getlk(state, F_GETLK, request);
4890 return 0;
4893 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4894 return -EINVAL;
4896 if (request->fl_type == F_UNLCK) {
4897 if (state != NULL)
4898 return nfs4_proc_unlck(state, cmd, request);
4899 return 0;
4902 if (state == NULL)
4903 return -ENOLCK;
4905 * Don't rely on the VFS having checked the file open mode,
4906 * since it won't do this for flock() locks.
4908 switch (request->fl_type) {
4909 case F_RDLCK:
4910 if (!(filp->f_mode & FMODE_READ))
4911 return -EBADF;
4912 break;
4913 case F_WRLCK:
4914 if (!(filp->f_mode & FMODE_WRITE))
4915 return -EBADF;
4918 do {
4919 status = nfs4_proc_setlk(state, cmd, request);
4920 if ((status != -EAGAIN) || IS_SETLK(cmd))
4921 break;
4922 timeout = nfs4_set_lock_task_retry(timeout);
4923 status = -ERESTARTSYS;
4924 if (signalled())
4925 break;
4926 } while(status < 0);
4927 return status;
4930 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4932 struct nfs_server *server = NFS_SERVER(state->inode);
4933 struct nfs4_exception exception = { };
4934 int err;
4936 err = nfs4_set_lock_state(state, fl);
4937 if (err != 0)
4938 goto out;
4939 do {
4940 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4941 switch (err) {
4942 default:
4943 printk(KERN_ERR "NFS: %s: unhandled error "
4944 "%d.\n", __func__, err);
4945 case 0:
4946 case -ESTALE:
4947 goto out;
4948 case -NFS4ERR_EXPIRED:
4949 nfs4_schedule_stateid_recovery(server, state);
4950 case -NFS4ERR_STALE_CLIENTID:
4951 case -NFS4ERR_STALE_STATEID:
4952 nfs4_schedule_lease_recovery(server->nfs_client);
4953 goto out;
4954 case -NFS4ERR_BADSESSION:
4955 case -NFS4ERR_BADSLOT:
4956 case -NFS4ERR_BAD_HIGH_SLOT:
4957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4958 case -NFS4ERR_DEADSESSION:
4959 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
4960 goto out;
4961 case -ERESTARTSYS:
4963 * The show must go on: exit, but mark the
4964 * stateid as needing recovery.
4966 case -NFS4ERR_DELEG_REVOKED:
4967 case -NFS4ERR_ADMIN_REVOKED:
4968 case -NFS4ERR_BAD_STATEID:
4969 case -NFS4ERR_OPENMODE:
4970 nfs4_schedule_stateid_recovery(server, state);
4971 err = 0;
4972 goto out;
4973 case -ENOMEM:
4974 case -NFS4ERR_DENIED:
4975 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4976 err = 0;
4977 goto out;
4978 case -NFS4ERR_DELAY:
4979 break;
4981 err = nfs4_handle_exception(server, err, &exception);
4982 } while (exception.retry);
4983 out:
4984 return err;
4987 struct nfs_release_lockowner_data {
4988 struct nfs4_lock_state *lsp;
4989 struct nfs_server *server;
4990 struct nfs_release_lockowner_args args;
4993 static void nfs4_release_lockowner_release(void *calldata)
4995 struct nfs_release_lockowner_data *data = calldata;
4996 nfs4_free_lock_state(data->server, data->lsp);
4997 kfree(calldata);
5000 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5001 .rpc_release = nfs4_release_lockowner_release,
5004 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5006 struct nfs_server *server = lsp->ls_state->owner->so_server;
5007 struct nfs_release_lockowner_data *data;
5008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5012 if (server->nfs_client->cl_mvops->minor_version != 0)
5013 return -EINVAL;
5014 data = kmalloc(sizeof(*data), GFP_NOFS);
5015 if (!data)
5016 return -ENOMEM;
5017 data->lsp = lsp;
5018 data->server = server;
5019 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5020 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5021 data->args.lock_owner.s_dev = server->s_dev;
5022 msg.rpc_argp = &data->args;
5023 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5024 return 0;
5027 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5029 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5030 const void *buf, size_t buflen,
5031 int flags, int type)
5033 if (strcmp(key, "") != 0)
5034 return -EINVAL;
5036 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5039 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5040 void *buf, size_t buflen, int type)
5042 if (strcmp(key, "") != 0)
5043 return -EINVAL;
5045 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5048 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5049 size_t list_len, const char *name,
5050 size_t name_len, int type)
5052 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5054 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5055 return 0;
5057 if (list && len <= list_len)
5058 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5059 return len;
5063 * nfs_fhget will use either the mounted_on_fileid or the fileid
5065 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5067 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5068 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5069 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5070 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5071 return;
5073 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5074 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5075 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5076 fattr->nlink = 2;
5079 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5080 const struct qstr *name,
5081 struct nfs4_fs_locations *fs_locations,
5082 struct page *page)
5084 struct nfs_server *server = NFS_SERVER(dir);
5085 u32 bitmask[2] = {
5086 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5088 struct nfs4_fs_locations_arg args = {
5089 .dir_fh = NFS_FH(dir),
5090 .name = name,
5091 .page = page,
5092 .bitmask = bitmask,
5094 struct nfs4_fs_locations_res res = {
5095 .fs_locations = fs_locations,
5097 struct rpc_message msg = {
5098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5099 .rpc_argp = &args,
5100 .rpc_resp = &res,
5102 int status;
5104 dprintk("%s: start\n", __func__);
5106 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5107 * is not supported */
5108 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5109 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5110 else
5111 bitmask[0] |= FATTR4_WORD0_FILEID;
5113 nfs_fattr_init(&fs_locations->fattr);
5114 fs_locations->server = server;
5115 fs_locations->nlocations = 0;
5116 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5117 dprintk("%s: returned status = %d\n", __func__, status);
5118 return status;
5121 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5122 const struct qstr *name,
5123 struct nfs4_fs_locations *fs_locations,
5124 struct page *page)
5126 struct nfs4_exception exception = { };
5127 int err;
5128 do {
5129 err = nfs4_handle_exception(NFS_SERVER(dir),
5130 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5131 &exception);
5132 } while (exception.retry);
5133 return err;
5136 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5138 int status;
5139 struct nfs4_secinfo_arg args = {
5140 .dir_fh = NFS_FH(dir),
5141 .name = name,
5143 struct nfs4_secinfo_res res = {
5144 .flavors = flavors,
5146 struct rpc_message msg = {
5147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5148 .rpc_argp = &args,
5149 .rpc_resp = &res,
5152 dprintk("NFS call secinfo %s\n", name->name);
5153 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5154 dprintk("NFS reply secinfo: %d\n", status);
5155 return status;
5158 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5159 struct nfs4_secinfo_flavors *flavors)
5161 struct nfs4_exception exception = { };
5162 int err;
5163 do {
5164 err = nfs4_handle_exception(NFS_SERVER(dir),
5165 _nfs4_proc_secinfo(dir, name, flavors),
5166 &exception);
5167 } while (exception.retry);
5168 return err;
5171 #ifdef CONFIG_NFS_V4_1
5173 * Check the exchange flags returned by the server for invalid flags, having
5174 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5175 * DS flags set.
5177 static int nfs4_check_cl_exchange_flags(u32 flags)
5179 if (flags & ~EXCHGID4_FLAG_MASK_R)
5180 goto out_inval;
5181 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5182 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5183 goto out_inval;
5184 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5185 goto out_inval;
5186 return NFS_OK;
5187 out_inval:
5188 return -NFS4ERR_INVAL;
5191 static bool
5192 nfs41_same_server_scope(struct nfs41_server_scope *a,
5193 struct nfs41_server_scope *b)
5195 if (a->server_scope_sz == b->server_scope_sz &&
5196 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5197 return true;
5199 return false;
5203 * nfs4_proc_bind_conn_to_session()
5205 * The 4.1 client currently uses the same TCP connection for the
5206 * fore and backchannel.
5208 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5210 int status;
5211 struct nfs41_bind_conn_to_session_res res;
5212 struct rpc_message msg = {
5213 .rpc_proc =
5214 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5215 .rpc_argp = clp,
5216 .rpc_resp = &res,
5217 .rpc_cred = cred,
5220 dprintk("--> %s\n", __func__);
5222 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5223 if (unlikely(res.session == NULL)) {
5224 status = -ENOMEM;
5225 goto out;
5228 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5229 if (status == 0) {
5230 if (memcmp(res.session->sess_id.data,
5231 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5232 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5233 status = -EIO;
5234 goto out_session;
5236 if (res.dir != NFS4_CDFS4_BOTH) {
5237 dprintk("NFS: %s: Unexpected direction from server\n",
5238 __func__);
5239 status = -EIO;
5240 goto out_session;
5242 if (res.use_conn_in_rdma_mode) {
5243 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5244 __func__);
5245 status = -EIO;
5246 goto out_session;
5249 out_session:
5250 kfree(res.session);
5251 out:
5252 dprintk("<-- %s status= %d\n", __func__, status);
5253 return status;
5257 * nfs4_proc_exchange_id()
5259 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5261 * Since the clientid has expired, all compounds using sessions
5262 * associated with the stale clientid will be returning
5263 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5264 * be in some phase of session reset.
5266 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5268 nfs4_verifier verifier;
5269 struct nfs41_exchange_id_args args = {
5270 .verifier = &verifier,
5271 .client = clp,
5272 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5274 struct nfs41_exchange_id_res res = {
5277 int status;
5278 struct rpc_message msg = {
5279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5280 .rpc_argp = &args,
5281 .rpc_resp = &res,
5282 .rpc_cred = cred,
5285 nfs4_init_boot_verifier(clp, &verifier);
5286 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5287 sizeof(args.id));
5288 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5289 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5290 args.id_len, args.id);
5292 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5293 GFP_NOFS);
5294 if (unlikely(res.server_owner == NULL)) {
5295 status = -ENOMEM;
5296 goto out;
5299 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5300 GFP_NOFS);
5301 if (unlikely(res.server_scope == NULL)) {
5302 status = -ENOMEM;
5303 goto out_server_owner;
5306 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5307 if (unlikely(res.impl_id == NULL)) {
5308 status = -ENOMEM;
5309 goto out_server_scope;
5312 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5313 if (status == 0)
5314 status = nfs4_check_cl_exchange_flags(res.flags);
5316 if (status == 0) {
5317 clp->cl_clientid = res.clientid;
5318 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5319 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5320 clp->cl_seqid = res.seqid;
5322 kfree(clp->cl_serverowner);
5323 clp->cl_serverowner = res.server_owner;
5324 res.server_owner = NULL;
5326 /* use the most recent implementation id */
5327 kfree(clp->cl_implid);
5328 clp->cl_implid = res.impl_id;
5330 if (clp->cl_serverscope != NULL &&
5331 !nfs41_same_server_scope(clp->cl_serverscope,
5332 res.server_scope)) {
5333 dprintk("%s: server_scope mismatch detected\n",
5334 __func__);
5335 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5336 kfree(clp->cl_serverscope);
5337 clp->cl_serverscope = NULL;
5340 if (clp->cl_serverscope == NULL) {
5341 clp->cl_serverscope = res.server_scope;
5342 goto out;
5344 } else
5345 kfree(res.impl_id);
5347 out_server_owner:
5348 kfree(res.server_owner);
5349 out_server_scope:
5350 kfree(res.server_scope);
5351 out:
5352 if (clp->cl_implid != NULL)
5353 dprintk("NFS reply exchange_id: Server Implementation ID: "
5354 "domain: %s, name: %s, date: %llu,%u\n",
5355 clp->cl_implid->domain, clp->cl_implid->name,
5356 clp->cl_implid->date.seconds,
5357 clp->cl_implid->date.nseconds);
5358 dprintk("NFS reply exchange_id: %d\n", status);
5359 return status;
5362 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5363 struct rpc_cred *cred)
5365 struct rpc_message msg = {
5366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5367 .rpc_argp = clp,
5368 .rpc_cred = cred,
5370 int status;
5372 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5373 if (status)
5374 dprintk("NFS: Got error %d from the server %s on "
5375 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5376 return status;
5379 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5380 struct rpc_cred *cred)
5382 unsigned int loop;
5383 int ret;
5385 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5386 ret = _nfs4_proc_destroy_clientid(clp, cred);
5387 switch (ret) {
5388 case -NFS4ERR_DELAY:
5389 case -NFS4ERR_CLIENTID_BUSY:
5390 ssleep(1);
5391 break;
5392 default:
5393 return ret;
5396 return 0;
5399 int nfs4_destroy_clientid(struct nfs_client *clp)
5401 struct rpc_cred *cred;
5402 int ret = 0;
5404 if (clp->cl_mvops->minor_version < 1)
5405 goto out;
5406 if (clp->cl_exchange_flags == 0)
5407 goto out;
5408 if (clp->cl_preserve_clid)
5409 goto out;
5410 cred = nfs4_get_exchange_id_cred(clp);
5411 ret = nfs4_proc_destroy_clientid(clp, cred);
5412 if (cred)
5413 put_rpccred(cred);
5414 switch (ret) {
5415 case 0:
5416 case -NFS4ERR_STALE_CLIENTID:
5417 clp->cl_exchange_flags = 0;
5419 out:
5420 return ret;
5423 struct nfs4_get_lease_time_data {
5424 struct nfs4_get_lease_time_args *args;
5425 struct nfs4_get_lease_time_res *res;
5426 struct nfs_client *clp;
5429 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5430 void *calldata)
5432 struct nfs4_get_lease_time_data *data =
5433 (struct nfs4_get_lease_time_data *)calldata;
5435 dprintk("--> %s\n", __func__);
5436 /* just setup sequence, do not trigger session recovery
5437 since we're invoked within one */
5438 nfs41_setup_sequence(data->clp->cl_session,
5439 &data->args->la_seq_args,
5440 &data->res->lr_seq_res,
5441 task);
5442 dprintk("<-- %s\n", __func__);
5446 * Called from nfs4_state_manager thread for session setup, so don't recover
5447 * from sequence operation or clientid errors.
5449 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5451 struct nfs4_get_lease_time_data *data =
5452 (struct nfs4_get_lease_time_data *)calldata;
5454 dprintk("--> %s\n", __func__);
5455 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5456 return;
5457 switch (task->tk_status) {
5458 case -NFS4ERR_DELAY:
5459 case -NFS4ERR_GRACE:
5460 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5461 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5462 task->tk_status = 0;
5463 /* fall through */
5464 case -NFS4ERR_RETRY_UNCACHED_REP:
5465 rpc_restart_call_prepare(task);
5466 return;
5468 dprintk("<-- %s\n", __func__);
5471 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5472 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5473 .rpc_call_done = nfs4_get_lease_time_done,
5476 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5478 struct rpc_task *task;
5479 struct nfs4_get_lease_time_args args;
5480 struct nfs4_get_lease_time_res res = {
5481 .lr_fsinfo = fsinfo,
5483 struct nfs4_get_lease_time_data data = {
5484 .args = &args,
5485 .res = &res,
5486 .clp = clp,
5488 struct rpc_message msg = {
5489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5490 .rpc_argp = &args,
5491 .rpc_resp = &res,
5493 struct rpc_task_setup task_setup = {
5494 .rpc_client = clp->cl_rpcclient,
5495 .rpc_message = &msg,
5496 .callback_ops = &nfs4_get_lease_time_ops,
5497 .callback_data = &data,
5498 .flags = RPC_TASK_TIMEOUT,
5500 int status;
5502 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5503 nfs4_set_sequence_privileged(&args.la_seq_args);
5504 dprintk("--> %s\n", __func__);
5505 task = rpc_run_task(&task_setup);
5507 if (IS_ERR(task))
5508 status = PTR_ERR(task);
5509 else {
5510 status = task->tk_status;
5511 rpc_put_task(task);
5513 dprintk("<-- %s return %d\n", __func__, status);
5515 return status;
5519 * Initialize the values to be used by the client in CREATE_SESSION
5520 * If nfs4_init_session set the fore channel request and response sizes,
5521 * use them.
5523 * Set the back channel max_resp_sz_cached to zero to force the client to
5524 * always set csa_cachethis to FALSE because the current implementation
5525 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5527 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5529 struct nfs4_session *session = args->client->cl_session;
5530 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5531 mxresp_sz = session->fc_target_max_resp_sz;
5533 if (mxrqst_sz == 0)
5534 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5535 if (mxresp_sz == 0)
5536 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5537 /* Fore channel attributes */
5538 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5539 args->fc_attrs.max_resp_sz = mxresp_sz;
5540 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5541 args->fc_attrs.max_reqs = max_session_slots;
5543 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5544 "max_ops=%u max_reqs=%u\n",
5545 __func__,
5546 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5547 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5549 /* Back channel attributes */
5550 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5551 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5552 args->bc_attrs.max_resp_sz_cached = 0;
5553 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5554 args->bc_attrs.max_reqs = 1;
5556 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5557 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5558 __func__,
5559 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5560 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5561 args->bc_attrs.max_reqs);
5564 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5566 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5567 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5569 if (rcvd->max_resp_sz > sent->max_resp_sz)
5570 return -EINVAL;
5572 * Our requested max_ops is the minimum we need; we're not
5573 * prepared to break up compounds into smaller pieces than that.
5574 * So, no point even trying to continue if the server won't
5575 * cooperate:
5577 if (rcvd->max_ops < sent->max_ops)
5578 return -EINVAL;
5579 if (rcvd->max_reqs == 0)
5580 return -EINVAL;
5581 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5582 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5583 return 0;
5586 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5588 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5589 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5591 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5592 return -EINVAL;
5593 if (rcvd->max_resp_sz < sent->max_resp_sz)
5594 return -EINVAL;
5595 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5596 return -EINVAL;
5597 /* These would render the backchannel useless: */
5598 if (rcvd->max_ops != sent->max_ops)
5599 return -EINVAL;
5600 if (rcvd->max_reqs != sent->max_reqs)
5601 return -EINVAL;
5602 return 0;
5605 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5606 struct nfs4_session *session)
5608 int ret;
5610 ret = nfs4_verify_fore_channel_attrs(args, session);
5611 if (ret)
5612 return ret;
5613 return nfs4_verify_back_channel_attrs(args, session);
5616 static int _nfs4_proc_create_session(struct nfs_client *clp,
5617 struct rpc_cred *cred)
5619 struct nfs4_session *session = clp->cl_session;
5620 struct nfs41_create_session_args args = {
5621 .client = clp,
5622 .cb_program = NFS4_CALLBACK,
5624 struct nfs41_create_session_res res = {
5625 .client = clp,
5627 struct rpc_message msg = {
5628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5629 .rpc_argp = &args,
5630 .rpc_resp = &res,
5631 .rpc_cred = cred,
5633 int status;
5635 nfs4_init_channel_attrs(&args);
5636 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5638 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5640 if (!status) {
5641 /* Verify the session's negotiated channel_attrs values */
5642 status = nfs4_verify_channel_attrs(&args, session);
5643 /* Increment the clientid slot sequence id */
5644 clp->cl_seqid++;
5647 return status;
5651 * Issues a CREATE_SESSION operation to the server.
5652 * It is the responsibility of the caller to verify the session is
5653 * expired before calling this routine.
5655 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5657 int status;
5658 unsigned *ptr;
5659 struct nfs4_session *session = clp->cl_session;
5661 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5663 status = _nfs4_proc_create_session(clp, cred);
5664 if (status)
5665 goto out;
5667 /* Init or reset the session slot tables */
5668 status = nfs4_setup_session_slot_tables(session);
5669 dprintk("slot table setup returned %d\n", status);
5670 if (status)
5671 goto out;
5673 ptr = (unsigned *)&session->sess_id.data[0];
5674 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5675 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5676 out:
5677 dprintk("<-- %s\n", __func__);
5678 return status;
5682 * Issue the over-the-wire RPC DESTROY_SESSION.
5683 * The caller must serialize access to this routine.
5685 int nfs4_proc_destroy_session(struct nfs4_session *session,
5686 struct rpc_cred *cred)
5688 struct rpc_message msg = {
5689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5690 .rpc_argp = session,
5691 .rpc_cred = cred,
5693 int status = 0;
5695 dprintk("--> nfs4_proc_destroy_session\n");
5697 /* session is still being setup */
5698 if (session->clp->cl_cons_state != NFS_CS_READY)
5699 return status;
5701 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5703 if (status)
5704 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5705 "Session has been destroyed regardless...\n", status);
5707 dprintk("<-- nfs4_proc_destroy_session\n");
5708 return status;
5712 * Renew the cl_session lease.
5714 struct nfs4_sequence_data {
5715 struct nfs_client *clp;
5716 struct nfs4_sequence_args args;
5717 struct nfs4_sequence_res res;
5720 static void nfs41_sequence_release(void *data)
5722 struct nfs4_sequence_data *calldata = data;
5723 struct nfs_client *clp = calldata->clp;
5725 if (atomic_read(&clp->cl_count) > 1)
5726 nfs4_schedule_state_renewal(clp);
5727 nfs_put_client(clp);
5728 kfree(calldata);
5731 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5733 switch(task->tk_status) {
5734 case -NFS4ERR_DELAY:
5735 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5736 return -EAGAIN;
5737 default:
5738 nfs4_schedule_lease_recovery(clp);
5740 return 0;
5743 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5745 struct nfs4_sequence_data *calldata = data;
5746 struct nfs_client *clp = calldata->clp;
5748 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5749 return;
5751 if (task->tk_status < 0) {
5752 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5753 if (atomic_read(&clp->cl_count) == 1)
5754 goto out;
5756 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5757 rpc_restart_call_prepare(task);
5758 return;
5761 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5762 out:
5763 dprintk("<-- %s\n", __func__);
5766 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5768 struct nfs4_sequence_data *calldata = data;
5769 struct nfs_client *clp = calldata->clp;
5770 struct nfs4_sequence_args *args;
5771 struct nfs4_sequence_res *res;
5773 args = task->tk_msg.rpc_argp;
5774 res = task->tk_msg.rpc_resp;
5776 nfs41_setup_sequence(clp->cl_session, args, res, task);
5779 static const struct rpc_call_ops nfs41_sequence_ops = {
5780 .rpc_call_done = nfs41_sequence_call_done,
5781 .rpc_call_prepare = nfs41_sequence_prepare,
5782 .rpc_release = nfs41_sequence_release,
5785 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5786 struct rpc_cred *cred,
5787 bool is_privileged)
5789 struct nfs4_sequence_data *calldata;
5790 struct rpc_message msg = {
5791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5792 .rpc_cred = cred,
5794 struct rpc_task_setup task_setup_data = {
5795 .rpc_client = clp->cl_rpcclient,
5796 .rpc_message = &msg,
5797 .callback_ops = &nfs41_sequence_ops,
5798 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5801 if (!atomic_inc_not_zero(&clp->cl_count))
5802 return ERR_PTR(-EIO);
5803 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5804 if (calldata == NULL) {
5805 nfs_put_client(clp);
5806 return ERR_PTR(-ENOMEM);
5808 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5809 if (is_privileged)
5810 nfs4_set_sequence_privileged(&calldata->args);
5811 msg.rpc_argp = &calldata->args;
5812 msg.rpc_resp = &calldata->res;
5813 calldata->clp = clp;
5814 task_setup_data.callback_data = calldata;
5816 return rpc_run_task(&task_setup_data);
5819 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5821 struct rpc_task *task;
5822 int ret = 0;
5824 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5825 return 0;
5826 task = _nfs41_proc_sequence(clp, cred, false);
5827 if (IS_ERR(task))
5828 ret = PTR_ERR(task);
5829 else
5830 rpc_put_task_async(task);
5831 dprintk("<-- %s status=%d\n", __func__, ret);
5832 return ret;
5835 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5837 struct rpc_task *task;
5838 int ret;
5840 task = _nfs41_proc_sequence(clp, cred, true);
5841 if (IS_ERR(task)) {
5842 ret = PTR_ERR(task);
5843 goto out;
5845 ret = rpc_wait_for_completion_task(task);
5846 if (!ret) {
5847 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5849 if (task->tk_status == 0)
5850 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5851 ret = task->tk_status;
5853 rpc_put_task(task);
5854 out:
5855 dprintk("<-- %s status=%d\n", __func__, ret);
5856 return ret;
5859 struct nfs4_reclaim_complete_data {
5860 struct nfs_client *clp;
5861 struct nfs41_reclaim_complete_args arg;
5862 struct nfs41_reclaim_complete_res res;
5865 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5867 struct nfs4_reclaim_complete_data *calldata = data;
5869 nfs41_setup_sequence(calldata->clp->cl_session,
5870 &calldata->arg.seq_args,
5871 &calldata->res.seq_res,
5872 task);
5875 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5877 switch(task->tk_status) {
5878 case 0:
5879 case -NFS4ERR_COMPLETE_ALREADY:
5880 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5881 break;
5882 case -NFS4ERR_DELAY:
5883 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5884 /* fall through */
5885 case -NFS4ERR_RETRY_UNCACHED_REP:
5886 return -EAGAIN;
5887 default:
5888 nfs4_schedule_lease_recovery(clp);
5890 return 0;
5893 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5895 struct nfs4_reclaim_complete_data *calldata = data;
5896 struct nfs_client *clp = calldata->clp;
5897 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5899 dprintk("--> %s\n", __func__);
5900 if (!nfs41_sequence_done(task, res))
5901 return;
5903 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5904 rpc_restart_call_prepare(task);
5905 return;
5907 dprintk("<-- %s\n", __func__);
5910 static void nfs4_free_reclaim_complete_data(void *data)
5912 struct nfs4_reclaim_complete_data *calldata = data;
5914 kfree(calldata);
5917 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5918 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5919 .rpc_call_done = nfs4_reclaim_complete_done,
5920 .rpc_release = nfs4_free_reclaim_complete_data,
5924 * Issue a global reclaim complete.
5926 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5928 struct nfs4_reclaim_complete_data *calldata;
5929 struct rpc_task *task;
5930 struct rpc_message msg = {
5931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5933 struct rpc_task_setup task_setup_data = {
5934 .rpc_client = clp->cl_rpcclient,
5935 .rpc_message = &msg,
5936 .callback_ops = &nfs4_reclaim_complete_call_ops,
5937 .flags = RPC_TASK_ASYNC,
5939 int status = -ENOMEM;
5941 dprintk("--> %s\n", __func__);
5942 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5943 if (calldata == NULL)
5944 goto out;
5945 calldata->clp = clp;
5946 calldata->arg.one_fs = 0;
5948 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5949 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
5950 msg.rpc_argp = &calldata->arg;
5951 msg.rpc_resp = &calldata->res;
5952 task_setup_data.callback_data = calldata;
5953 task = rpc_run_task(&task_setup_data);
5954 if (IS_ERR(task)) {
5955 status = PTR_ERR(task);
5956 goto out;
5958 status = nfs4_wait_for_completion_rpc_task(task);
5959 if (status == 0)
5960 status = task->tk_status;
5961 rpc_put_task(task);
5962 return 0;
5963 out:
5964 dprintk("<-- %s status=%d\n", __func__, status);
5965 return status;
5968 static void
5969 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5971 struct nfs4_layoutget *lgp = calldata;
5972 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5973 struct nfs4_session *session = nfs4_get_session(server);
5975 dprintk("--> %s\n", __func__);
5976 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5977 * right now covering the LAYOUTGET we are about to send.
5978 * However, that is not so catastrophic, and there seems
5979 * to be no way to prevent it completely.
5981 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
5982 &lgp->res.seq_res, task))
5983 return;
5984 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5985 NFS_I(lgp->args.inode)->layout,
5986 lgp->args.ctx->state)) {
5987 rpc_exit(task, NFS4_OK);
5991 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5993 struct nfs4_layoutget *lgp = calldata;
5994 struct inode *inode = lgp->args.inode;
5995 struct nfs_server *server = NFS_SERVER(inode);
5996 struct pnfs_layout_hdr *lo;
5997 struct nfs4_state *state = NULL;
5999 dprintk("--> %s\n", __func__);
6001 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6002 goto out;
6004 switch (task->tk_status) {
6005 case 0:
6006 goto out;
6007 case -NFS4ERR_LAYOUTTRYLATER:
6008 case -NFS4ERR_RECALLCONFLICT:
6009 task->tk_status = -NFS4ERR_DELAY;
6010 break;
6011 case -NFS4ERR_EXPIRED:
6012 case -NFS4ERR_BAD_STATEID:
6013 spin_lock(&inode->i_lock);
6014 lo = NFS_I(inode)->layout;
6015 if (!lo || list_empty(&lo->plh_segs)) {
6016 spin_unlock(&inode->i_lock);
6017 /* If the open stateid was bad, then recover it. */
6018 state = lgp->args.ctx->state;
6019 } else {
6020 LIST_HEAD(head);
6022 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6023 spin_unlock(&inode->i_lock);
6024 /* Mark the bad layout state as invalid, then
6025 * retry using the open stateid. */
6026 pnfs_free_lseg_list(&head);
6029 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6030 rpc_restart_call_prepare(task);
6031 out:
6032 dprintk("<-- %s\n", __func__);
6035 static size_t max_response_pages(struct nfs_server *server)
6037 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6038 return nfs_page_array_len(0, max_resp_sz);
6041 static void nfs4_free_pages(struct page **pages, size_t size)
6043 int i;
6045 if (!pages)
6046 return;
6048 for (i = 0; i < size; i++) {
6049 if (!pages[i])
6050 break;
6051 __free_page(pages[i]);
6053 kfree(pages);
6056 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6058 struct page **pages;
6059 int i;
6061 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6062 if (!pages) {
6063 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6064 return NULL;
6067 for (i = 0; i < size; i++) {
6068 pages[i] = alloc_page(gfp_flags);
6069 if (!pages[i]) {
6070 dprintk("%s: failed to allocate page\n", __func__);
6071 nfs4_free_pages(pages, size);
6072 return NULL;
6076 return pages;
6079 static void nfs4_layoutget_release(void *calldata)
6081 struct nfs4_layoutget *lgp = calldata;
6082 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6083 size_t max_pages = max_response_pages(server);
6085 dprintk("--> %s\n", __func__);
6086 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6087 put_nfs_open_context(lgp->args.ctx);
6088 kfree(calldata);
6089 dprintk("<-- %s\n", __func__);
6092 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6093 .rpc_call_prepare = nfs4_layoutget_prepare,
6094 .rpc_call_done = nfs4_layoutget_done,
6095 .rpc_release = nfs4_layoutget_release,
6098 struct pnfs_layout_segment *
6099 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6101 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6102 size_t max_pages = max_response_pages(server);
6103 struct rpc_task *task;
6104 struct rpc_message msg = {
6105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6106 .rpc_argp = &lgp->args,
6107 .rpc_resp = &lgp->res,
6109 struct rpc_task_setup task_setup_data = {
6110 .rpc_client = server->client,
6111 .rpc_message = &msg,
6112 .callback_ops = &nfs4_layoutget_call_ops,
6113 .callback_data = lgp,
6114 .flags = RPC_TASK_ASYNC,
6116 struct pnfs_layout_segment *lseg = NULL;
6117 int status = 0;
6119 dprintk("--> %s\n", __func__);
6121 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6122 if (!lgp->args.layout.pages) {
6123 nfs4_layoutget_release(lgp);
6124 return ERR_PTR(-ENOMEM);
6126 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6128 lgp->res.layoutp = &lgp->args.layout;
6129 lgp->res.seq_res.sr_slot = NULL;
6130 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6131 task = rpc_run_task(&task_setup_data);
6132 if (IS_ERR(task))
6133 return ERR_CAST(task);
6134 status = nfs4_wait_for_completion_rpc_task(task);
6135 if (status == 0)
6136 status = task->tk_status;
6137 if (status == 0)
6138 lseg = pnfs_layout_process(lgp);
6139 rpc_put_task(task);
6140 dprintk("<-- %s status=%d\n", __func__, status);
6141 if (status)
6142 return ERR_PTR(status);
6143 return lseg;
6146 static void
6147 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6149 struct nfs4_layoutreturn *lrp = calldata;
6151 dprintk("--> %s\n", __func__);
6152 nfs41_setup_sequence(lrp->clp->cl_session,
6153 &lrp->args.seq_args,
6154 &lrp->res.seq_res,
6155 task);
6158 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6160 struct nfs4_layoutreturn *lrp = calldata;
6161 struct nfs_server *server;
6163 dprintk("--> %s\n", __func__);
6165 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6166 return;
6168 server = NFS_SERVER(lrp->args.inode);
6169 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6170 rpc_restart_call_prepare(task);
6171 return;
6173 dprintk("<-- %s\n", __func__);
6176 static void nfs4_layoutreturn_release(void *calldata)
6178 struct nfs4_layoutreturn *lrp = calldata;
6179 struct pnfs_layout_hdr *lo = lrp->args.layout;
6181 dprintk("--> %s\n", __func__);
6182 spin_lock(&lo->plh_inode->i_lock);
6183 if (lrp->res.lrs_present)
6184 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6185 lo->plh_block_lgets--;
6186 spin_unlock(&lo->plh_inode->i_lock);
6187 pnfs_put_layout_hdr(lrp->args.layout);
6188 kfree(calldata);
6189 dprintk("<-- %s\n", __func__);
6192 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6193 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6194 .rpc_call_done = nfs4_layoutreturn_done,
6195 .rpc_release = nfs4_layoutreturn_release,
6198 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6200 struct rpc_task *task;
6201 struct rpc_message msg = {
6202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6203 .rpc_argp = &lrp->args,
6204 .rpc_resp = &lrp->res,
6206 struct rpc_task_setup task_setup_data = {
6207 .rpc_client = lrp->clp->cl_rpcclient,
6208 .rpc_message = &msg,
6209 .callback_ops = &nfs4_layoutreturn_call_ops,
6210 .callback_data = lrp,
6212 int status;
6214 dprintk("--> %s\n", __func__);
6215 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6216 task = rpc_run_task(&task_setup_data);
6217 if (IS_ERR(task))
6218 return PTR_ERR(task);
6219 status = task->tk_status;
6220 dprintk("<-- %s status=%d\n", __func__, status);
6221 rpc_put_task(task);
6222 return status;
6226 * Retrieve the list of Data Server devices from the MDS.
6228 static int _nfs4_getdevicelist(struct nfs_server *server,
6229 const struct nfs_fh *fh,
6230 struct pnfs_devicelist *devlist)
6232 struct nfs4_getdevicelist_args args = {
6233 .fh = fh,
6234 .layoutclass = server->pnfs_curr_ld->id,
6236 struct nfs4_getdevicelist_res res = {
6237 .devlist = devlist,
6239 struct rpc_message msg = {
6240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6241 .rpc_argp = &args,
6242 .rpc_resp = &res,
6244 int status;
6246 dprintk("--> %s\n", __func__);
6247 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6248 &res.seq_res, 0);
6249 dprintk("<-- %s status=%d\n", __func__, status);
6250 return status;
6253 int nfs4_proc_getdevicelist(struct nfs_server *server,
6254 const struct nfs_fh *fh,
6255 struct pnfs_devicelist *devlist)
6257 struct nfs4_exception exception = { };
6258 int err;
6260 do {
6261 err = nfs4_handle_exception(server,
6262 _nfs4_getdevicelist(server, fh, devlist),
6263 &exception);
6264 } while (exception.retry);
6266 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6267 err, devlist->num_devs);
6269 return err;
6271 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6273 static int
6274 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6276 struct nfs4_getdeviceinfo_args args = {
6277 .pdev = pdev,
6279 struct nfs4_getdeviceinfo_res res = {
6280 .pdev = pdev,
6282 struct rpc_message msg = {
6283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6284 .rpc_argp = &args,
6285 .rpc_resp = &res,
6287 int status;
6289 dprintk("--> %s\n", __func__);
6290 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6291 dprintk("<-- %s status=%d\n", __func__, status);
6293 return status;
6296 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6298 struct nfs4_exception exception = { };
6299 int err;
6301 do {
6302 err = nfs4_handle_exception(server,
6303 _nfs4_proc_getdeviceinfo(server, pdev),
6304 &exception);
6305 } while (exception.retry);
6306 return err;
6308 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6310 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6312 struct nfs4_layoutcommit_data *data = calldata;
6313 struct nfs_server *server = NFS_SERVER(data->args.inode);
6314 struct nfs4_session *session = nfs4_get_session(server);
6316 nfs41_setup_sequence(session,
6317 &data->args.seq_args,
6318 &data->res.seq_res,
6319 task);
6322 static void
6323 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6325 struct nfs4_layoutcommit_data *data = calldata;
6326 struct nfs_server *server = NFS_SERVER(data->args.inode);
6328 if (!nfs41_sequence_done(task, &data->res.seq_res))
6329 return;
6331 switch (task->tk_status) { /* Just ignore these failures */
6332 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6333 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6334 case -NFS4ERR_BADLAYOUT: /* no layout */
6335 case -NFS4ERR_GRACE: /* loca_recalim always false */
6336 task->tk_status = 0;
6337 break;
6338 case 0:
6339 nfs_post_op_update_inode_force_wcc(data->args.inode,
6340 data->res.fattr);
6341 break;
6342 default:
6343 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6344 rpc_restart_call_prepare(task);
6345 return;
6350 static void nfs4_layoutcommit_release(void *calldata)
6352 struct nfs4_layoutcommit_data *data = calldata;
6353 struct pnfs_layout_segment *lseg, *tmp;
6354 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6356 pnfs_cleanup_layoutcommit(data);
6357 /* Matched by references in pnfs_set_layoutcommit */
6358 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6359 list_del_init(&lseg->pls_lc_list);
6360 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6361 &lseg->pls_flags))
6362 pnfs_put_lseg(lseg);
6365 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6366 smp_mb__after_clear_bit();
6367 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6369 put_rpccred(data->cred);
6370 kfree(data);
6373 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6374 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6375 .rpc_call_done = nfs4_layoutcommit_done,
6376 .rpc_release = nfs4_layoutcommit_release,
6380 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6382 struct rpc_message msg = {
6383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6384 .rpc_argp = &data->args,
6385 .rpc_resp = &data->res,
6386 .rpc_cred = data->cred,
6388 struct rpc_task_setup task_setup_data = {
6389 .task = &data->task,
6390 .rpc_client = NFS_CLIENT(data->args.inode),
6391 .rpc_message = &msg,
6392 .callback_ops = &nfs4_layoutcommit_ops,
6393 .callback_data = data,
6394 .flags = RPC_TASK_ASYNC,
6396 struct rpc_task *task;
6397 int status = 0;
6399 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6400 "lbw: %llu inode %lu\n",
6401 data->task.tk_pid, sync,
6402 data->args.lastbytewritten,
6403 data->args.inode->i_ino);
6405 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6406 task = rpc_run_task(&task_setup_data);
6407 if (IS_ERR(task))
6408 return PTR_ERR(task);
6409 if (sync == false)
6410 goto out;
6411 status = nfs4_wait_for_completion_rpc_task(task);
6412 if (status != 0)
6413 goto out;
6414 status = task->tk_status;
6415 out:
6416 dprintk("%s: status %d\n", __func__, status);
6417 rpc_put_task(task);
6418 return status;
6421 static int
6422 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6423 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6425 struct nfs41_secinfo_no_name_args args = {
6426 .style = SECINFO_STYLE_CURRENT_FH,
6428 struct nfs4_secinfo_res res = {
6429 .flavors = flavors,
6431 struct rpc_message msg = {
6432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6433 .rpc_argp = &args,
6434 .rpc_resp = &res,
6436 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6439 static int
6440 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6441 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6443 struct nfs4_exception exception = { };
6444 int err;
6445 do {
6446 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6447 switch (err) {
6448 case 0:
6449 case -NFS4ERR_WRONGSEC:
6450 case -NFS4ERR_NOTSUPP:
6451 goto out;
6452 default:
6453 err = nfs4_handle_exception(server, err, &exception);
6455 } while (exception.retry);
6456 out:
6457 return err;
6460 static int
6461 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6462 struct nfs_fsinfo *info)
6464 int err;
6465 struct page *page;
6466 rpc_authflavor_t flavor;
6467 struct nfs4_secinfo_flavors *flavors;
6469 page = alloc_page(GFP_KERNEL);
6470 if (!page) {
6471 err = -ENOMEM;
6472 goto out;
6475 flavors = page_address(page);
6476 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6479 * Fall back on "guess and check" method if
6480 * the server doesn't support SECINFO_NO_NAME
6482 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6483 err = nfs4_find_root_sec(server, fhandle, info);
6484 goto out_freepage;
6486 if (err)
6487 goto out_freepage;
6489 flavor = nfs_find_best_sec(flavors);
6490 if (err == 0)
6491 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6493 out_freepage:
6494 put_page(page);
6495 if (err == -EACCES)
6496 return -EPERM;
6497 out:
6498 return err;
6501 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6503 int status;
6504 struct nfs41_test_stateid_args args = {
6505 .stateid = stateid,
6507 struct nfs41_test_stateid_res res;
6508 struct rpc_message msg = {
6509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6510 .rpc_argp = &args,
6511 .rpc_resp = &res,
6514 dprintk("NFS call test_stateid %p\n", stateid);
6515 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6516 nfs4_set_sequence_privileged(&args.seq_args);
6517 status = nfs4_call_sync_sequence(server->client, server, &msg,
6518 &args.seq_args, &res.seq_res);
6519 if (status != NFS_OK) {
6520 dprintk("NFS reply test_stateid: failed, %d\n", status);
6521 return status;
6523 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6524 return -res.status;
6528 * nfs41_test_stateid - perform a TEST_STATEID operation
6530 * @server: server / transport on which to perform the operation
6531 * @stateid: state ID to test
6533 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6534 * Otherwise a negative NFS4ERR value is returned if the operation
6535 * failed or the state ID is not currently valid.
6537 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6539 struct nfs4_exception exception = { };
6540 int err;
6541 do {
6542 err = _nfs41_test_stateid(server, stateid);
6543 if (err != -NFS4ERR_DELAY)
6544 break;
6545 nfs4_handle_exception(server, err, &exception);
6546 } while (exception.retry);
6547 return err;
6550 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6552 struct nfs41_free_stateid_args args = {
6553 .stateid = stateid,
6555 struct nfs41_free_stateid_res res;
6556 struct rpc_message msg = {
6557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6558 .rpc_argp = &args,
6559 .rpc_resp = &res,
6561 int status;
6563 dprintk("NFS call free_stateid %p\n", stateid);
6564 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6565 nfs4_set_sequence_privileged(&args.seq_args);
6566 status = nfs4_call_sync_sequence(server->client, server, &msg,
6567 &args.seq_args, &res.seq_res);
6568 dprintk("NFS reply free_stateid: %d\n", status);
6569 return status;
6573 * nfs41_free_stateid - perform a FREE_STATEID operation
6575 * @server: server / transport on which to perform the operation
6576 * @stateid: state ID to release
6578 * Returns NFS_OK if the server freed "stateid". Otherwise a
6579 * negative NFS4ERR value is returned.
6581 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6583 struct nfs4_exception exception = { };
6584 int err;
6585 do {
6586 err = _nfs4_free_stateid(server, stateid);
6587 if (err != -NFS4ERR_DELAY)
6588 break;
6589 nfs4_handle_exception(server, err, &exception);
6590 } while (exception.retry);
6591 return err;
6594 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6595 const nfs4_stateid *s2)
6597 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6598 return false;
6600 if (s1->seqid == s2->seqid)
6601 return true;
6602 if (s1->seqid == 0 || s2->seqid == 0)
6603 return true;
6605 return false;
6608 #endif /* CONFIG_NFS_V4_1 */
6610 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6611 const nfs4_stateid *s2)
6613 return nfs4_stateid_match(s1, s2);
6617 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6618 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6619 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6620 .recover_open = nfs4_open_reclaim,
6621 .recover_lock = nfs4_lock_reclaim,
6622 .establish_clid = nfs4_init_clientid,
6623 .get_clid_cred = nfs4_get_setclientid_cred,
6624 .detect_trunking = nfs40_discover_server_trunking,
6627 #if defined(CONFIG_NFS_V4_1)
6628 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6629 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6630 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6631 .recover_open = nfs4_open_reclaim,
6632 .recover_lock = nfs4_lock_reclaim,
6633 .establish_clid = nfs41_init_clientid,
6634 .get_clid_cred = nfs4_get_exchange_id_cred,
6635 .reclaim_complete = nfs41_proc_reclaim_complete,
6636 .detect_trunking = nfs41_discover_server_trunking,
6638 #endif /* CONFIG_NFS_V4_1 */
6640 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6641 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6642 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6643 .recover_open = nfs4_open_expired,
6644 .recover_lock = nfs4_lock_expired,
6645 .establish_clid = nfs4_init_clientid,
6646 .get_clid_cred = nfs4_get_setclientid_cred,
6649 #if defined(CONFIG_NFS_V4_1)
6650 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6651 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6652 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6653 .recover_open = nfs41_open_expired,
6654 .recover_lock = nfs41_lock_expired,
6655 .establish_clid = nfs41_init_clientid,
6656 .get_clid_cred = nfs4_get_exchange_id_cred,
6658 #endif /* CONFIG_NFS_V4_1 */
6660 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6661 .sched_state_renewal = nfs4_proc_async_renew,
6662 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6663 .renew_lease = nfs4_proc_renew,
6666 #if defined(CONFIG_NFS_V4_1)
6667 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6668 .sched_state_renewal = nfs41_proc_async_sequence,
6669 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6670 .renew_lease = nfs4_proc_sequence,
6672 #endif
6674 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6675 .minor_version = 0,
6676 .call_sync = _nfs4_call_sync,
6677 .match_stateid = nfs4_match_stateid,
6678 .find_root_sec = nfs4_find_root_sec,
6679 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6680 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6681 .state_renewal_ops = &nfs40_state_renewal_ops,
6684 #if defined(CONFIG_NFS_V4_1)
6685 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6686 .minor_version = 1,
6687 .call_sync = nfs4_call_sync_sequence,
6688 .match_stateid = nfs41_match_stateid,
6689 .find_root_sec = nfs41_find_root_sec,
6690 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6691 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6692 .state_renewal_ops = &nfs41_state_renewal_ops,
6694 #endif
6696 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6697 [0] = &nfs_v4_0_minor_ops,
6698 #if defined(CONFIG_NFS_V4_1)
6699 [1] = &nfs_v4_1_minor_ops,
6700 #endif
6703 const struct inode_operations nfs4_dir_inode_operations = {
6704 .create = nfs_create,
6705 .lookup = nfs_lookup,
6706 .atomic_open = nfs_atomic_open,
6707 .link = nfs_link,
6708 .unlink = nfs_unlink,
6709 .symlink = nfs_symlink,
6710 .mkdir = nfs_mkdir,
6711 .rmdir = nfs_rmdir,
6712 .mknod = nfs_mknod,
6713 .rename = nfs_rename,
6714 .permission = nfs_permission,
6715 .getattr = nfs_getattr,
6716 .setattr = nfs_setattr,
6717 .getxattr = generic_getxattr,
6718 .setxattr = generic_setxattr,
6719 .listxattr = generic_listxattr,
6720 .removexattr = generic_removexattr,
6723 static const struct inode_operations nfs4_file_inode_operations = {
6724 .permission = nfs_permission,
6725 .getattr = nfs_getattr,
6726 .setattr = nfs_setattr,
6727 .getxattr = generic_getxattr,
6728 .setxattr = generic_setxattr,
6729 .listxattr = generic_listxattr,
6730 .removexattr = generic_removexattr,
6733 const struct nfs_rpc_ops nfs_v4_clientops = {
6734 .version = 4, /* protocol version */
6735 .dentry_ops = &nfs4_dentry_operations,
6736 .dir_inode_ops = &nfs4_dir_inode_operations,
6737 .file_inode_ops = &nfs4_file_inode_operations,
6738 .file_ops = &nfs4_file_operations,
6739 .getroot = nfs4_proc_get_root,
6740 .submount = nfs4_submount,
6741 .try_mount = nfs4_try_mount,
6742 .getattr = nfs4_proc_getattr,
6743 .setattr = nfs4_proc_setattr,
6744 .lookup = nfs4_proc_lookup,
6745 .access = nfs4_proc_access,
6746 .readlink = nfs4_proc_readlink,
6747 .create = nfs4_proc_create,
6748 .remove = nfs4_proc_remove,
6749 .unlink_setup = nfs4_proc_unlink_setup,
6750 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6751 .unlink_done = nfs4_proc_unlink_done,
6752 .rename = nfs4_proc_rename,
6753 .rename_setup = nfs4_proc_rename_setup,
6754 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6755 .rename_done = nfs4_proc_rename_done,
6756 .link = nfs4_proc_link,
6757 .symlink = nfs4_proc_symlink,
6758 .mkdir = nfs4_proc_mkdir,
6759 .rmdir = nfs4_proc_remove,
6760 .readdir = nfs4_proc_readdir,
6761 .mknod = nfs4_proc_mknod,
6762 .statfs = nfs4_proc_statfs,
6763 .fsinfo = nfs4_proc_fsinfo,
6764 .pathconf = nfs4_proc_pathconf,
6765 .set_capabilities = nfs4_server_capabilities,
6766 .decode_dirent = nfs4_decode_dirent,
6767 .read_setup = nfs4_proc_read_setup,
6768 .read_pageio_init = pnfs_pageio_init_read,
6769 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6770 .read_done = nfs4_read_done,
6771 .write_setup = nfs4_proc_write_setup,
6772 .write_pageio_init = pnfs_pageio_init_write,
6773 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6774 .write_done = nfs4_write_done,
6775 .commit_setup = nfs4_proc_commit_setup,
6776 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6777 .commit_done = nfs4_commit_done,
6778 .lock = nfs4_proc_lock,
6779 .clear_acl_cache = nfs4_zap_acl_attr,
6780 .close_context = nfs4_close_context,
6781 .open_context = nfs4_atomic_open,
6782 .have_delegation = nfs4_have_delegation,
6783 .return_delegation = nfs4_inode_return_delegation,
6784 .alloc_client = nfs4_alloc_client,
6785 .init_client = nfs4_init_client,
6786 .free_client = nfs4_free_client,
6787 .create_server = nfs4_create_server,
6788 .clone_server = nfs_clone_server,
6791 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6792 .prefix = XATTR_NAME_NFSV4_ACL,
6793 .list = nfs4_xattr_list_nfs4_acl,
6794 .get = nfs4_xattr_get_nfs4_acl,
6795 .set = nfs4_xattr_set_nfs4_acl,
6798 const struct xattr_handler *nfs4_xattr_handlers[] = {
6799 &nfs4_xattr_nfs4_acl_handler,
6800 NULL
6804 * Local variables:
6805 * c-basic-offset: 8
6806 * End: