NFSv4: Handle NFS4ERR_DELAY and NFS4ERR_GRACE in nfs4_lock_delegation_recall
[linux-2.6.git] / fs / nfs / nfs4proc.c
blobdc4037c52cf7104e9232c6ee26fb2aa484308674
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 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
98 return -EREMOTEIO;
99 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
108 case -NFS4ERR_ACCESS:
109 return -EACCES;
110 case -NFS4ERR_FILE_OPEN:
111 return -EBUSY;
112 default:
113 dprintk("%s could not handle NFSv4 error %d\n",
114 __func__, -err);
115 break;
117 return -EIO;
121 * This is our standard bitmap for GETATTR requests.
123 const u32 nfs4_fattr_bitmap[3] = {
124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
140 static const u32 nfs4_pnfs_open_bitmap[3] = {
141 FATTR4_WORD0_TYPE
142 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_SIZE
144 | FATTR4_WORD0_FSID
145 | FATTR4_WORD0_FILEID,
146 FATTR4_WORD1_MODE
147 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY,
155 FATTR4_WORD2_MDSTHRESHOLD
158 static const u32 nfs4_open_noattr_bitmap[3] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID,
164 const u32 nfs4_statfs_bitmap[2] = {
165 FATTR4_WORD0_FILES_AVAIL
166 | FATTR4_WORD0_FILES_FREE
167 | FATTR4_WORD0_FILES_TOTAL,
168 FATTR4_WORD1_SPACE_AVAIL
169 | FATTR4_WORD1_SPACE_FREE
170 | FATTR4_WORD1_SPACE_TOTAL
173 const u32 nfs4_pathconf_bitmap[2] = {
174 FATTR4_WORD0_MAXLINK
175 | FATTR4_WORD0_MAXNAME,
179 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
180 | FATTR4_WORD0_MAXREAD
181 | FATTR4_WORD0_MAXWRITE
182 | FATTR4_WORD0_LEASE_TIME,
183 FATTR4_WORD1_TIME_DELTA
184 | FATTR4_WORD1_FS_LAYOUT_TYPES,
185 FATTR4_WORD2_LAYOUT_BLKSIZE
188 const u32 nfs4_fs_locations_bitmap[2] = {
189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID
194 | FATTR4_WORD0_FS_LOCATIONS,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID
207 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
208 struct nfs4_readdir_arg *readdir)
210 __be32 *start, *p;
212 if (cookie > 2) {
213 readdir->cookie = cookie;
214 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
215 return;
218 readdir->cookie = 0;
219 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
220 if (cookie == 2)
221 return;
224 * NFSv4 servers do not return entries for '.' and '..'
225 * Therefore, we fake these entries here. We let '.'
226 * have cookie 0 and '..' have cookie 1. Note that
227 * when talking to the server, we always send cookie 0
228 * instead of 1 or 2.
230 start = p = kmap_atomic(*readdir->pages);
232 if (cookie == 0) {
233 *p++ = xdr_one; /* next */
234 *p++ = xdr_zero; /* cookie, first word */
235 *p++ = xdr_one; /* cookie, second word */
236 *p++ = xdr_one; /* entry len */
237 memcpy(p, ".\0\0\0", 4); /* entry */
238 p++;
239 *p++ = xdr_one; /* bitmap length */
240 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
241 *p++ = htonl(8); /* attribute buffer length */
242 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
245 *p++ = xdr_one; /* next */
246 *p++ = xdr_zero; /* cookie, first word */
247 *p++ = xdr_two; /* cookie, second word */
248 *p++ = xdr_two; /* entry len */
249 memcpy(p, "..\0\0", 4); /* entry */
250 p++;
251 *p++ = xdr_one; /* bitmap length */
252 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
253 *p++ = htonl(8); /* attribute buffer length */
254 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
256 readdir->pgbase = (char *)p - (char *)start;
257 readdir->count -= readdir->pgbase;
258 kunmap_atomic(start);
261 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
263 int res = 0;
265 might_sleep();
267 if (*timeout <= 0)
268 *timeout = NFS4_POLL_RETRY_MIN;
269 if (*timeout > NFS4_POLL_RETRY_MAX)
270 *timeout = NFS4_POLL_RETRY_MAX;
271 freezable_schedule_timeout_killable(*timeout);
272 if (fatal_signal_pending(current))
273 res = -ERESTARTSYS;
274 *timeout <<= 1;
275 return res;
278 /* This is the error handling routine for processes that are allowed
279 * to sleep.
281 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
283 struct nfs_client *clp = server->nfs_client;
284 struct nfs4_state *state = exception->state;
285 struct inode *inode = exception->inode;
286 int ret = errorcode;
288 exception->retry = 0;
289 switch(errorcode) {
290 case 0:
291 return 0;
292 case -NFS4ERR_OPENMODE:
293 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
294 nfs4_inode_return_delegation(inode);
295 exception->retry = 1;
296 return 0;
298 if (state == NULL)
299 break;
300 ret = nfs4_schedule_stateid_recovery(server, state);
301 if (ret < 0)
302 break;
303 goto wait_on_recovery;
304 case -NFS4ERR_DELEG_REVOKED:
305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
307 if (state == NULL)
308 break;
309 nfs_remove_bad_delegation(state->inode);
310 ret = nfs4_schedule_stateid_recovery(server, state);
311 if (ret < 0)
312 break;
313 goto wait_on_recovery;
314 case -NFS4ERR_EXPIRED:
315 if (state != NULL) {
316 ret = nfs4_schedule_stateid_recovery(server, state);
317 if (ret < 0)
318 break;
320 case -NFS4ERR_STALE_STATEID:
321 case -NFS4ERR_STALE_CLIENTID:
322 nfs4_schedule_lease_recovery(clp);
323 goto wait_on_recovery;
324 #if defined(CONFIG_NFS_V4_1)
325 case -NFS4ERR_BADSESSION:
326 case -NFS4ERR_BADSLOT:
327 case -NFS4ERR_BAD_HIGH_SLOT:
328 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
329 case -NFS4ERR_DEADSESSION:
330 case -NFS4ERR_SEQ_FALSE_RETRY:
331 case -NFS4ERR_SEQ_MISORDERED:
332 dprintk("%s ERROR: %d Reset session\n", __func__,
333 errorcode);
334 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
335 goto wait_on_recovery;
336 #endif /* defined(CONFIG_NFS_V4_1) */
337 case -NFS4ERR_FILE_OPEN:
338 if (exception->timeout > HZ) {
339 /* We have retried a decent amount, time to
340 * fail
342 ret = -EBUSY;
343 break;
345 case -NFS4ERR_GRACE:
346 case -NFS4ERR_DELAY:
347 ret = nfs4_delay(server->client, &exception->timeout);
348 if (ret != 0)
349 break;
350 case -NFS4ERR_RETRY_UNCACHED_REP:
351 case -NFS4ERR_OLD_STATEID:
352 exception->retry = 1;
353 break;
354 case -NFS4ERR_BADOWNER:
355 /* The following works around a Linux server bug! */
356 case -NFS4ERR_BADNAME:
357 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
358 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
359 exception->retry = 1;
360 printk(KERN_WARNING "NFS: v4 server %s "
361 "does not accept raw "
362 "uid/gids. "
363 "Reenabling the idmapper.\n",
364 server->nfs_client->cl_hostname);
367 /* We failed to handle the error */
368 return nfs4_map_errors(ret);
369 wait_on_recovery:
370 ret = nfs4_wait_clnt_recover(clp);
371 if (ret == 0)
372 exception->retry = 1;
373 return ret;
377 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
379 spin_lock(&clp->cl_lock);
380 if (time_before(clp->cl_last_renewal,timestamp))
381 clp->cl_last_renewal = timestamp;
382 spin_unlock(&clp->cl_lock);
385 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
387 do_renew_lease(server->nfs_client, timestamp);
390 #if defined(CONFIG_NFS_V4_1)
392 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
394 struct nfs4_session *session;
395 struct nfs4_slot_table *tbl;
396 bool send_new_highest_used_slotid = false;
398 if (!res->sr_slot) {
399 /* just wake up the next guy waiting since
400 * we may have not consumed a slot after all */
401 dprintk("%s: No slot\n", __func__);
402 return;
404 tbl = res->sr_slot->table;
405 session = tbl->session;
407 spin_lock(&tbl->slot_tbl_lock);
408 /* Be nice to the server: try to ensure that the last transmitted
409 * value for highest_user_slotid <= target_highest_slotid
411 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
412 send_new_highest_used_slotid = true;
414 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
415 send_new_highest_used_slotid = false;
416 goto out_unlock;
418 nfs4_free_slot(tbl, res->sr_slot);
420 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
421 send_new_highest_used_slotid = false;
422 out_unlock:
423 spin_unlock(&tbl->slot_tbl_lock);
424 res->sr_slot = NULL;
425 if (send_new_highest_used_slotid)
426 nfs41_server_notify_highest_slotid_update(session->clp);
429 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
431 struct nfs4_session *session;
432 struct nfs4_slot *slot;
433 struct nfs_client *clp;
434 bool interrupted = false;
435 int ret = 1;
437 /* don't increment the sequence number if the task wasn't sent */
438 if (!RPC_WAS_SENT(task))
439 goto out;
441 slot = res->sr_slot;
442 session = slot->table->session;
444 if (slot->interrupted) {
445 slot->interrupted = 0;
446 interrupted = true;
449 /* Check the SEQUENCE operation status */
450 switch (res->sr_status) {
451 case 0:
452 /* Update the slot's sequence and clientid lease timer */
453 ++slot->seq_nr;
454 clp = session->clp;
455 do_renew_lease(clp, res->sr_timestamp);
456 /* Check sequence flags */
457 if (res->sr_status_flags != 0)
458 nfs4_schedule_lease_recovery(clp);
459 nfs41_update_target_slotid(slot->table, slot, res);
460 break;
461 case 1:
463 * sr_status remains 1 if an RPC level error occurred.
464 * The server may or may not have processed the sequence
465 * operation..
466 * Mark the slot as having hosted an interrupted RPC call.
468 slot->interrupted = 1;
469 goto out;
470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
475 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
476 __func__,
477 slot->slot_nr,
478 slot->seq_nr);
479 goto out_retry;
480 case -NFS4ERR_BADSLOT:
482 * The slot id we used was probably retired. Try again
483 * using a different slot id.
485 goto retry_nowait;
486 case -NFS4ERR_SEQ_MISORDERED:
488 * Was the last operation on this sequence interrupted?
489 * If so, retry after bumping the sequence number.
491 if (interrupted) {
492 ++slot->seq_nr;
493 goto retry_nowait;
496 * Could this slot have been previously retired?
497 * If so, then the server may be expecting seq_nr = 1!
499 if (slot->seq_nr != 1) {
500 slot->seq_nr = 1;
501 goto retry_nowait;
503 break;
504 case -NFS4ERR_SEQ_FALSE_RETRY:
505 ++slot->seq_nr;
506 goto retry_nowait;
507 default:
508 /* Just update the slot sequence no. */
509 ++slot->seq_nr;
511 out:
512 /* The session may be reset by one of the error handlers. */
513 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
514 nfs41_sequence_free_slot(res);
515 return ret;
516 retry_nowait:
517 if (rpc_restart_call_prepare(task)) {
518 task->tk_status = 0;
519 ret = 0;
521 goto out;
522 out_retry:
523 if (!rpc_restart_call(task))
524 goto out;
525 rpc_delay(task, NFS4_POLL_RETRY_MAX);
526 return 0;
529 static int nfs4_sequence_done(struct rpc_task *task,
530 struct nfs4_sequence_res *res)
532 if (res->sr_slot == NULL)
533 return 1;
534 return nfs41_sequence_done(task, res);
537 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res, int cache_reply)
540 args->sa_slot = NULL;
541 args->sa_cache_this = 0;
542 args->sa_privileged = 0;
543 if (cache_reply)
544 args->sa_cache_this = 1;
545 res->sr_slot = NULL;
548 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
550 args->sa_privileged = 1;
553 int nfs41_setup_sequence(struct nfs4_session *session,
554 struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res,
556 struct rpc_task *task)
558 struct nfs4_slot *slot;
559 struct nfs4_slot_table *tbl;
561 dprintk("--> %s\n", __func__);
562 /* slot already allocated? */
563 if (res->sr_slot != NULL)
564 goto out_success;
566 tbl = &session->fc_slot_table;
568 task->tk_timeout = 0;
570 spin_lock(&tbl->slot_tbl_lock);
571 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
572 !args->sa_privileged) {
573 /* The state manager will wait until the slot table is empty */
574 dprintk("%s session is draining\n", __func__);
575 goto out_sleep;
578 slot = nfs4_alloc_slot(tbl);
579 if (IS_ERR(slot)) {
580 /* If out of memory, try again in 1/4 second */
581 if (slot == ERR_PTR(-ENOMEM))
582 task->tk_timeout = HZ >> 2;
583 dprintk("<-- %s: no free slots\n", __func__);
584 goto out_sleep;
586 spin_unlock(&tbl->slot_tbl_lock);
588 args->sa_slot = slot;
590 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
591 slot->slot_nr, slot->seq_nr);
593 res->sr_slot = slot;
594 res->sr_timestamp = jiffies;
595 res->sr_status_flags = 0;
597 * sr_status is only set in decode_sequence, and so will remain
598 * set to 1 if an rpc level failure occurs.
600 res->sr_status = 1;
601 out_success:
602 rpc_call_start(task);
603 return 0;
604 out_sleep:
605 /* Privileged tasks are queued with top priority */
606 if (args->sa_privileged)
607 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
608 NULL, RPC_PRIORITY_PRIVILEGED);
609 else
610 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
611 spin_unlock(&tbl->slot_tbl_lock);
612 return -EAGAIN;
614 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
616 int nfs4_setup_sequence(const struct nfs_server *server,
617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
619 struct rpc_task *task)
621 struct nfs4_session *session = nfs4_get_session(server);
622 int ret = 0;
624 if (session == NULL) {
625 rpc_call_start(task);
626 goto out;
629 dprintk("--> %s clp %p session %p sr_slot %d\n",
630 __func__, session->clp, session, res->sr_slot ?
631 res->sr_slot->slot_nr : -1);
633 ret = nfs41_setup_sequence(session, args, res, task);
634 out:
635 dprintk("<-- %s status=%d\n", __func__, ret);
636 return ret;
639 struct nfs41_call_sync_data {
640 const struct nfs_server *seq_server;
641 struct nfs4_sequence_args *seq_args;
642 struct nfs4_sequence_res *seq_res;
645 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
647 struct nfs41_call_sync_data *data = calldata;
648 struct nfs4_session *session = nfs4_get_session(data->seq_server);
650 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
652 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
655 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
657 struct nfs41_call_sync_data *data = calldata;
659 nfs41_sequence_done(task, data->seq_res);
662 static const struct rpc_call_ops nfs41_call_sync_ops = {
663 .rpc_call_prepare = nfs41_call_sync_prepare,
664 .rpc_call_done = nfs41_call_sync_done,
667 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
668 struct nfs_server *server,
669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
671 struct nfs4_sequence_res *res)
673 int ret;
674 struct rpc_task *task;
675 struct nfs41_call_sync_data data = {
676 .seq_server = server,
677 .seq_args = args,
678 .seq_res = res,
680 struct rpc_task_setup task_setup = {
681 .rpc_client = clnt,
682 .rpc_message = msg,
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
687 task = rpc_run_task(&task_setup);
688 if (IS_ERR(task))
689 ret = PTR_ERR(task);
690 else {
691 ret = task->tk_status;
692 rpc_put_task(task);
694 return ret;
697 #else
698 static
699 void nfs41_init_sequence(struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res, int cache_reply)
704 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
709 static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
712 return 1;
714 #endif /* CONFIG_NFS_V4_1 */
716 static
717 int _nfs4_call_sync(struct rpc_clnt *clnt,
718 struct nfs_server *server,
719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res)
723 return rpc_call_sync(clnt, msg, 0);
726 static
727 int nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 int cache_reply)
734 nfs41_init_sequence(args, res, cache_reply);
735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
736 args, res);
739 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
741 struct nfs_inode *nfsi = NFS_I(dir);
743 spin_lock(&dir->i_lock);
744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
745 if (!cinfo->atomic || cinfo->before != dir->i_version)
746 nfs_force_lookup_revalidate(dir);
747 dir->i_version = cinfo->after;
748 nfs_fscache_invalidate(dir);
749 spin_unlock(&dir->i_lock);
752 struct nfs4_opendata {
753 struct kref kref;
754 struct nfs_openargs o_arg;
755 struct nfs_openres o_res;
756 struct nfs_open_confirmargs c_arg;
757 struct nfs_open_confirmres c_res;
758 struct nfs4_string owner_name;
759 struct nfs4_string group_name;
760 struct nfs_fattr f_attr;
761 struct dentry *dir;
762 struct dentry *dentry;
763 struct nfs4_state_owner *owner;
764 struct nfs4_state *state;
765 struct iattr attrs;
766 unsigned long timestamp;
767 unsigned int rpc_done : 1;
768 int rpc_status;
769 int cancelled;
772 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
773 int err, struct nfs4_exception *exception)
775 if (err != -EINVAL)
776 return false;
777 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
778 return false;
779 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
780 exception->retry = 1;
781 return true;
784 static enum open_claim_type4
785 nfs4_map_atomic_open_claim(struct nfs_server *server,
786 enum open_claim_type4 claim)
788 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
789 return claim;
790 switch (claim) {
791 default:
792 return claim;
793 case NFS4_OPEN_CLAIM_FH:
794 return NFS4_OPEN_CLAIM_NULL;
795 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
796 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
798 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
802 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
804 p->o_res.f_attr = &p->f_attr;
805 p->o_res.seqid = p->o_arg.seqid;
806 p->c_res.seqid = p->c_arg.seqid;
807 p->o_res.server = p->o_arg.server;
808 p->o_res.access_request = p->o_arg.access;
809 nfs_fattr_init(&p->f_attr);
810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
813 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
815 const struct iattr *attrs,
816 enum open_claim_type4 claim,
817 gfp_t gfp_mask)
819 struct dentry *parent = dget_parent(dentry);
820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
824 p = kzalloc(sizeof(*p), gfp_mask);
825 if (p == NULL)
826 goto err;
827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
828 if (p->o_arg.seqid == NULL)
829 goto err_free;
830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
832 p->dir = parent;
833 p->owner = sp;
834 atomic_inc(&sp->so_count);
835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
837 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
838 * will return permission denied for all bits until close */
839 if (!(flags & O_EXCL)) {
840 /* ask server to check for all possible rights as results
841 * are cached */
842 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
843 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
845 p->o_arg.clientid = server->nfs_client->cl_clientid;
846 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
847 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
848 p->o_arg.name = &dentry->d_name;
849 p->o_arg.server = server;
850 p->o_arg.bitmask = server->attr_bitmask;
851 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
852 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
853 switch (p->o_arg.claim) {
854 case NFS4_OPEN_CLAIM_NULL:
855 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
856 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
857 p->o_arg.fh = NFS_FH(dir);
858 break;
859 case NFS4_OPEN_CLAIM_PREVIOUS:
860 case NFS4_OPEN_CLAIM_FH:
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
863 p->o_arg.fh = NFS_FH(dentry->d_inode);
865 if (attrs != NULL && attrs->ia_valid != 0) {
866 __be32 verf[2];
868 p->o_arg.u.attrs = &p->attrs;
869 memcpy(&p->attrs, attrs, sizeof(p->attrs));
871 verf[0] = jiffies;
872 verf[1] = current->pid;
873 memcpy(p->o_arg.u.verifier.data, verf,
874 sizeof(p->o_arg.u.verifier.data));
876 p->c_arg.fh = &p->o_res.fh;
877 p->c_arg.stateid = &p->o_res.stateid;
878 p->c_arg.seqid = p->o_arg.seqid;
879 nfs4_init_opendata_res(p);
880 kref_init(&p->kref);
881 return p;
882 err_free:
883 kfree(p);
884 err:
885 dput(parent);
886 return NULL;
889 static void nfs4_opendata_free(struct kref *kref)
891 struct nfs4_opendata *p = container_of(kref,
892 struct nfs4_opendata, kref);
893 struct super_block *sb = p->dentry->d_sb;
895 nfs_free_seqid(p->o_arg.seqid);
896 if (p->state != NULL)
897 nfs4_put_open_state(p->state);
898 nfs4_put_state_owner(p->owner);
899 dput(p->dir);
900 dput(p->dentry);
901 nfs_sb_deactive(sb);
902 nfs_fattr_free_names(&p->f_attr);
903 kfree(p);
906 static void nfs4_opendata_put(struct nfs4_opendata *p)
908 if (p != NULL)
909 kref_put(&p->kref, nfs4_opendata_free);
912 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
914 int ret;
916 ret = rpc_wait_for_completion_task(task);
917 return ret;
920 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
922 int ret = 0;
924 if (open_mode & (O_EXCL|O_TRUNC))
925 goto out;
926 switch (mode & (FMODE_READ|FMODE_WRITE)) {
927 case FMODE_READ:
928 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
929 && state->n_rdonly != 0;
930 break;
931 case FMODE_WRITE:
932 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
933 && state->n_wronly != 0;
934 break;
935 case FMODE_READ|FMODE_WRITE:
936 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
937 && state->n_rdwr != 0;
939 out:
940 return ret;
943 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
945 if (delegation == NULL)
946 return 0;
947 if ((delegation->type & fmode) != fmode)
948 return 0;
949 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
950 return 0;
951 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
952 return 0;
953 nfs_mark_delegation_referenced(delegation);
954 return 1;
957 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
959 switch (fmode) {
960 case FMODE_WRITE:
961 state->n_wronly++;
962 break;
963 case FMODE_READ:
964 state->n_rdonly++;
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 state->n_rdwr++;
969 nfs4_state_set_mode_locked(state, state->state | fmode);
972 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
974 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
975 nfs4_stateid_copy(&state->stateid, stateid);
976 nfs4_stateid_copy(&state->open_stateid, stateid);
977 switch (fmode) {
978 case FMODE_READ:
979 set_bit(NFS_O_RDONLY_STATE, &state->flags);
980 break;
981 case FMODE_WRITE:
982 set_bit(NFS_O_WRONLY_STATE, &state->flags);
983 break;
984 case FMODE_READ|FMODE_WRITE:
985 set_bit(NFS_O_RDWR_STATE, &state->flags);
989 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
991 write_seqlock(&state->seqlock);
992 nfs_set_open_stateid_locked(state, stateid, fmode);
993 write_sequnlock(&state->seqlock);
996 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
999 * Protect the call to nfs4_state_set_mode_locked and
1000 * serialise the stateid update
1002 write_seqlock(&state->seqlock);
1003 if (deleg_stateid != NULL) {
1004 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1005 set_bit(NFS_DELEGATED_STATE, &state->flags);
1007 if (open_stateid != NULL)
1008 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1009 write_sequnlock(&state->seqlock);
1010 spin_lock(&state->owner->so_lock);
1011 update_open_stateflags(state, fmode);
1012 spin_unlock(&state->owner->so_lock);
1015 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *deleg_cur;
1019 int ret = 0;
1021 fmode &= (FMODE_READ|FMODE_WRITE);
1023 rcu_read_lock();
1024 deleg_cur = rcu_dereference(nfsi->delegation);
1025 if (deleg_cur == NULL)
1026 goto no_delegation;
1028 spin_lock(&deleg_cur->lock);
1029 if (nfsi->delegation != deleg_cur ||
1030 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1031 (deleg_cur->type & fmode) != fmode)
1032 goto no_delegation_unlock;
1034 if (delegation == NULL)
1035 delegation = &deleg_cur->stateid;
1036 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1037 goto no_delegation_unlock;
1039 nfs_mark_delegation_referenced(deleg_cur);
1040 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1041 ret = 1;
1042 no_delegation_unlock:
1043 spin_unlock(&deleg_cur->lock);
1044 no_delegation:
1045 rcu_read_unlock();
1047 if (!ret && open_stateid != NULL) {
1048 __update_open_stateid(state, open_stateid, NULL, fmode);
1049 ret = 1;
1052 return ret;
1056 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1058 struct nfs_delegation *delegation;
1060 rcu_read_lock();
1061 delegation = rcu_dereference(NFS_I(inode)->delegation);
1062 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1063 rcu_read_unlock();
1064 return;
1066 rcu_read_unlock();
1067 nfs4_inode_return_delegation(inode);
1070 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1072 struct nfs4_state *state = opendata->state;
1073 struct nfs_inode *nfsi = NFS_I(state->inode);
1074 struct nfs_delegation *delegation;
1075 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1076 fmode_t fmode = opendata->o_arg.fmode;
1077 nfs4_stateid stateid;
1078 int ret = -EAGAIN;
1080 for (;;) {
1081 if (can_open_cached(state, fmode, open_mode)) {
1082 spin_lock(&state->owner->so_lock);
1083 if (can_open_cached(state, fmode, open_mode)) {
1084 update_open_stateflags(state, fmode);
1085 spin_unlock(&state->owner->so_lock);
1086 goto out_return_state;
1088 spin_unlock(&state->owner->so_lock);
1090 rcu_read_lock();
1091 delegation = rcu_dereference(nfsi->delegation);
1092 if (!can_open_delegated(delegation, fmode)) {
1093 rcu_read_unlock();
1094 break;
1096 /* Save the delegation */
1097 nfs4_stateid_copy(&stateid, &delegation->stateid);
1098 rcu_read_unlock();
1099 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1100 if (ret != 0)
1101 goto out;
1102 ret = -EAGAIN;
1104 /* Try to update the stateid using the delegation */
1105 if (update_open_stateid(state, NULL, &stateid, fmode))
1106 goto out_return_state;
1108 out:
1109 return ERR_PTR(ret);
1110 out_return_state:
1111 atomic_inc(&state->count);
1112 return state;
1115 static void
1116 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1118 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1119 struct nfs_delegation *delegation;
1120 int delegation_flags = 0;
1122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
1127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
1131 clp->cl_hostname);
1132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1143 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1144 * and update the nfs4_state.
1146 static struct nfs4_state *
1147 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1149 struct inode *inode = data->state->inode;
1150 struct nfs4_state *state = data->state;
1151 int ret;
1153 if (!data->rpc_done) {
1154 ret = data->rpc_status;
1155 goto err;
1158 ret = -ESTALE;
1159 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1160 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1161 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1162 goto err;
1164 ret = -ENOMEM;
1165 state = nfs4_get_open_state(inode, data->owner);
1166 if (state == NULL)
1167 goto err;
1169 ret = nfs_refresh_inode(inode, &data->f_attr);
1170 if (ret)
1171 goto err;
1173 if (data->o_res.delegation_type != 0)
1174 nfs4_opendata_check_deleg(data, state);
1175 update_open_stateid(state, &data->o_res.stateid, NULL,
1176 data->o_arg.fmode);
1178 return state;
1179 err:
1180 return ERR_PTR(ret);
1184 static struct nfs4_state *
1185 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1187 struct inode *inode;
1188 struct nfs4_state *state = NULL;
1189 int ret;
1191 if (!data->rpc_done) {
1192 state = nfs4_try_open_cached(data);
1193 goto out;
1196 ret = -EAGAIN;
1197 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1198 goto err;
1199 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1200 ret = PTR_ERR(inode);
1201 if (IS_ERR(inode))
1202 goto err;
1203 ret = -ENOMEM;
1204 state = nfs4_get_open_state(inode, data->owner);
1205 if (state == NULL)
1206 goto err_put_inode;
1207 if (data->o_res.delegation_type != 0)
1208 nfs4_opendata_check_deleg(data, state);
1209 update_open_stateid(state, &data->o_res.stateid, NULL,
1210 data->o_arg.fmode);
1211 iput(inode);
1212 out:
1213 nfs_release_seqid(data->o_arg.seqid);
1214 return state;
1215 err_put_inode:
1216 iput(inode);
1217 err:
1218 return ERR_PTR(ret);
1221 static struct nfs4_state *
1222 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1224 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1225 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1226 return _nfs4_opendata_to_nfs4_state(data);
1229 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1231 struct nfs_inode *nfsi = NFS_I(state->inode);
1232 struct nfs_open_context *ctx;
1234 spin_lock(&state->inode->i_lock);
1235 list_for_each_entry(ctx, &nfsi->open_files, list) {
1236 if (ctx->state != state)
1237 continue;
1238 get_nfs_open_context(ctx);
1239 spin_unlock(&state->inode->i_lock);
1240 return ctx;
1242 spin_unlock(&state->inode->i_lock);
1243 return ERR_PTR(-ENOENT);
1246 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1247 struct nfs4_state *state, enum open_claim_type4 claim)
1249 struct nfs4_opendata *opendata;
1251 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1252 NULL, claim, GFP_NOFS);
1253 if (opendata == NULL)
1254 return ERR_PTR(-ENOMEM);
1255 opendata->state = state;
1256 atomic_inc(&state->count);
1257 return opendata;
1260 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1262 struct nfs4_state *newstate;
1263 int ret;
1265 opendata->o_arg.open_flags = 0;
1266 opendata->o_arg.fmode = fmode;
1267 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1268 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1269 nfs4_init_opendata_res(opendata);
1270 ret = _nfs4_recover_proc_open(opendata);
1271 if (ret != 0)
1272 return ret;
1273 newstate = nfs4_opendata_to_nfs4_state(opendata);
1274 if (IS_ERR(newstate))
1275 return PTR_ERR(newstate);
1276 nfs4_close_state(newstate, fmode);
1277 *res = newstate;
1278 return 0;
1281 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1283 struct nfs4_state *newstate;
1284 int ret;
1286 /* memory barrier prior to reading state->n_* */
1287 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1288 smp_rmb();
1289 if (state->n_rdwr != 0) {
1290 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1291 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1292 if (ret != 0)
1293 return ret;
1294 if (newstate != state)
1295 return -ESTALE;
1297 if (state->n_wronly != 0) {
1298 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1299 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1300 if (ret != 0)
1301 return ret;
1302 if (newstate != state)
1303 return -ESTALE;
1305 if (state->n_rdonly != 0) {
1306 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1307 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1308 if (ret != 0)
1309 return ret;
1310 if (newstate != state)
1311 return -ESTALE;
1314 * We may have performed cached opens for all three recoveries.
1315 * Check if we need to update the current stateid.
1317 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1318 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1319 write_seqlock(&state->seqlock);
1320 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1322 write_sequnlock(&state->seqlock);
1324 return 0;
1328 * OPEN_RECLAIM:
1329 * reclaim state on the server after a reboot.
1331 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1333 struct nfs_delegation *delegation;
1334 struct nfs4_opendata *opendata;
1335 fmode_t delegation_type = 0;
1336 int status;
1338 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1339 NFS4_OPEN_CLAIM_PREVIOUS);
1340 if (IS_ERR(opendata))
1341 return PTR_ERR(opendata);
1342 rcu_read_lock();
1343 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1344 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1345 delegation_type = delegation->type;
1346 rcu_read_unlock();
1347 opendata->o_arg.u.delegation_type = delegation_type;
1348 status = nfs4_open_recover(opendata, state);
1349 nfs4_opendata_put(opendata);
1350 return status;
1353 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1355 struct nfs_server *server = NFS_SERVER(state->inode);
1356 struct nfs4_exception exception = { };
1357 int err;
1358 do {
1359 err = _nfs4_do_open_reclaim(ctx, state);
1360 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1361 continue;
1362 if (err != -NFS4ERR_DELAY)
1363 break;
1364 nfs4_handle_exception(server, err, &exception);
1365 } while (exception.retry);
1366 return err;
1369 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1371 struct nfs_open_context *ctx;
1372 int ret;
1374 ctx = nfs4_state_find_open_context(state);
1375 if (IS_ERR(ctx))
1376 return -EAGAIN;
1377 ret = nfs4_do_open_reclaim(ctx, state);
1378 put_nfs_open_context(ctx);
1379 return ret;
1382 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1384 struct nfs4_opendata *opendata;
1385 int ret;
1387 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1388 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1389 if (IS_ERR(opendata))
1390 return PTR_ERR(opendata);
1391 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1392 ret = nfs4_open_recover(opendata, state);
1393 nfs4_opendata_put(opendata);
1394 return ret;
1397 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1399 struct nfs4_exception exception = { };
1400 struct nfs_server *server = NFS_SERVER(state->inode);
1401 int err;
1402 do {
1403 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1404 switch (err) {
1405 case 0:
1406 case -ENOENT:
1407 case -ESTALE:
1408 goto out;
1409 case -NFS4ERR_BADSESSION:
1410 case -NFS4ERR_BADSLOT:
1411 case -NFS4ERR_BAD_HIGH_SLOT:
1412 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1413 case -NFS4ERR_DEADSESSION:
1414 set_bit(NFS_DELEGATED_STATE, &state->flags);
1415 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1416 err = -EAGAIN;
1417 goto out;
1418 case -NFS4ERR_STALE_CLIENTID:
1419 case -NFS4ERR_STALE_STATEID:
1420 set_bit(NFS_DELEGATED_STATE, &state->flags);
1421 case -NFS4ERR_EXPIRED:
1422 /* Don't recall a delegation if it was lost */
1423 nfs4_schedule_lease_recovery(server->nfs_client);
1424 err = -EAGAIN;
1425 goto out;
1426 case -NFS4ERR_DELEG_REVOKED:
1427 case -NFS4ERR_ADMIN_REVOKED:
1428 case -NFS4ERR_BAD_STATEID:
1429 nfs_inode_find_state_and_recover(state->inode,
1430 stateid);
1431 nfs4_schedule_stateid_recovery(server, state);
1432 case -ENOMEM:
1433 err = 0;
1434 goto out;
1436 set_bit(NFS_DELEGATED_STATE, &state->flags);
1437 err = nfs4_handle_exception(server, err, &exception);
1438 } while (exception.retry);
1439 out:
1440 return err;
1443 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1445 struct nfs4_opendata *data = calldata;
1447 data->rpc_status = task->tk_status;
1448 if (data->rpc_status == 0) {
1449 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1450 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1451 renew_lease(data->o_res.server, data->timestamp);
1452 data->rpc_done = 1;
1456 static void nfs4_open_confirm_release(void *calldata)
1458 struct nfs4_opendata *data = calldata;
1459 struct nfs4_state *state = NULL;
1461 /* If this request hasn't been cancelled, do nothing */
1462 if (data->cancelled == 0)
1463 goto out_free;
1464 /* In case of error, no cleanup! */
1465 if (!data->rpc_done)
1466 goto out_free;
1467 state = nfs4_opendata_to_nfs4_state(data);
1468 if (!IS_ERR(state))
1469 nfs4_close_state(state, data->o_arg.fmode);
1470 out_free:
1471 nfs4_opendata_put(data);
1474 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1475 .rpc_call_done = nfs4_open_confirm_done,
1476 .rpc_release = nfs4_open_confirm_release,
1480 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1482 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1484 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1485 struct rpc_task *task;
1486 struct rpc_message msg = {
1487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1488 .rpc_argp = &data->c_arg,
1489 .rpc_resp = &data->c_res,
1490 .rpc_cred = data->owner->so_cred,
1492 struct rpc_task_setup task_setup_data = {
1493 .rpc_client = server->client,
1494 .rpc_message = &msg,
1495 .callback_ops = &nfs4_open_confirm_ops,
1496 .callback_data = data,
1497 .workqueue = nfsiod_workqueue,
1498 .flags = RPC_TASK_ASYNC,
1500 int status;
1502 kref_get(&data->kref);
1503 data->rpc_done = 0;
1504 data->rpc_status = 0;
1505 data->timestamp = jiffies;
1506 task = rpc_run_task(&task_setup_data);
1507 if (IS_ERR(task))
1508 return PTR_ERR(task);
1509 status = nfs4_wait_for_completion_rpc_task(task);
1510 if (status != 0) {
1511 data->cancelled = 1;
1512 smp_wmb();
1513 } else
1514 status = data->rpc_status;
1515 rpc_put_task(task);
1516 return status;
1519 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1521 struct nfs4_opendata *data = calldata;
1522 struct nfs4_state_owner *sp = data->owner;
1524 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1525 goto out_wait;
1527 * Check if we still need to send an OPEN call, or if we can use
1528 * a delegation instead.
1530 if (data->state != NULL) {
1531 struct nfs_delegation *delegation;
1533 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1534 goto out_no_action;
1535 rcu_read_lock();
1536 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1537 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1538 can_open_delegated(delegation, data->o_arg.fmode))
1539 goto unlock_no_action;
1540 rcu_read_unlock();
1542 /* Update client id. */
1543 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1544 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1545 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1546 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1547 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1549 data->timestamp = jiffies;
1550 if (nfs4_setup_sequence(data->o_arg.server,
1551 &data->o_arg.seq_args,
1552 &data->o_res.seq_res,
1553 task) != 0)
1554 nfs_release_seqid(data->o_arg.seqid);
1555 return;
1556 unlock_no_action:
1557 rcu_read_unlock();
1558 out_no_action:
1559 task->tk_action = NULL;
1560 out_wait:
1561 nfs4_sequence_done(task, &data->o_res.seq_res);
1564 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1566 struct nfs4_opendata *data = calldata;
1568 data->rpc_status = task->tk_status;
1570 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1571 return;
1573 if (task->tk_status == 0) {
1574 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1575 switch (data->o_res.f_attr->mode & S_IFMT) {
1576 case S_IFREG:
1577 break;
1578 case S_IFLNK:
1579 data->rpc_status = -ELOOP;
1580 break;
1581 case S_IFDIR:
1582 data->rpc_status = -EISDIR;
1583 break;
1584 default:
1585 data->rpc_status = -ENOTDIR;
1588 renew_lease(data->o_res.server, data->timestamp);
1589 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1590 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1592 data->rpc_done = 1;
1595 static void nfs4_open_release(void *calldata)
1597 struct nfs4_opendata *data = calldata;
1598 struct nfs4_state *state = NULL;
1600 /* If this request hasn't been cancelled, do nothing */
1601 if (data->cancelled == 0)
1602 goto out_free;
1603 /* In case of error, no cleanup! */
1604 if (data->rpc_status != 0 || !data->rpc_done)
1605 goto out_free;
1606 /* In case we need an open_confirm, no cleanup! */
1607 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1608 goto out_free;
1609 state = nfs4_opendata_to_nfs4_state(data);
1610 if (!IS_ERR(state))
1611 nfs4_close_state(state, data->o_arg.fmode);
1612 out_free:
1613 nfs4_opendata_put(data);
1616 static const struct rpc_call_ops nfs4_open_ops = {
1617 .rpc_call_prepare = nfs4_open_prepare,
1618 .rpc_call_done = nfs4_open_done,
1619 .rpc_release = nfs4_open_release,
1622 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1624 struct inode *dir = data->dir->d_inode;
1625 struct nfs_server *server = NFS_SERVER(dir);
1626 struct nfs_openargs *o_arg = &data->o_arg;
1627 struct nfs_openres *o_res = &data->o_res;
1628 struct rpc_task *task;
1629 struct rpc_message msg = {
1630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1631 .rpc_argp = o_arg,
1632 .rpc_resp = o_res,
1633 .rpc_cred = data->owner->so_cred,
1635 struct rpc_task_setup task_setup_data = {
1636 .rpc_client = server->client,
1637 .rpc_message = &msg,
1638 .callback_ops = &nfs4_open_ops,
1639 .callback_data = data,
1640 .workqueue = nfsiod_workqueue,
1641 .flags = RPC_TASK_ASYNC,
1643 int status;
1645 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1646 kref_get(&data->kref);
1647 data->rpc_done = 0;
1648 data->rpc_status = 0;
1649 data->cancelled = 0;
1650 if (isrecover)
1651 nfs4_set_sequence_privileged(&o_arg->seq_args);
1652 task = rpc_run_task(&task_setup_data);
1653 if (IS_ERR(task))
1654 return PTR_ERR(task);
1655 status = nfs4_wait_for_completion_rpc_task(task);
1656 if (status != 0) {
1657 data->cancelled = 1;
1658 smp_wmb();
1659 } else
1660 status = data->rpc_status;
1661 rpc_put_task(task);
1663 return status;
1666 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1668 struct inode *dir = data->dir->d_inode;
1669 struct nfs_openres *o_res = &data->o_res;
1670 int status;
1672 status = nfs4_run_open_task(data, 1);
1673 if (status != 0 || !data->rpc_done)
1674 return status;
1676 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1678 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1679 status = _nfs4_proc_open_confirm(data);
1680 if (status != 0)
1681 return status;
1684 return status;
1687 static int nfs4_opendata_access(struct rpc_cred *cred,
1688 struct nfs4_opendata *opendata,
1689 struct nfs4_state *state, fmode_t fmode,
1690 int openflags)
1692 struct nfs_access_entry cache;
1693 u32 mask;
1695 /* access call failed or for some reason the server doesn't
1696 * support any access modes -- defer access call until later */
1697 if (opendata->o_res.access_supported == 0)
1698 return 0;
1700 mask = 0;
1701 /* don't check MAY_WRITE - a newly created file may not have
1702 * write mode bits, but POSIX allows the creating process to write.
1703 * use openflags to check for exec, because fmode won't
1704 * always have FMODE_EXEC set when file open for exec. */
1705 if (openflags & __FMODE_EXEC) {
1706 /* ONLY check for exec rights */
1707 mask = MAY_EXEC;
1708 } else if (fmode & FMODE_READ)
1709 mask = MAY_READ;
1711 cache.cred = cred;
1712 cache.jiffies = jiffies;
1713 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1714 nfs_access_add_cache(state->inode, &cache);
1716 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1717 return 0;
1719 /* even though OPEN succeeded, access is denied. Close the file */
1720 nfs4_close_state(state, fmode);
1721 return -EACCES;
1725 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1727 static int _nfs4_proc_open(struct nfs4_opendata *data)
1729 struct inode *dir = data->dir->d_inode;
1730 struct nfs_server *server = NFS_SERVER(dir);
1731 struct nfs_openargs *o_arg = &data->o_arg;
1732 struct nfs_openres *o_res = &data->o_res;
1733 int status;
1735 status = nfs4_run_open_task(data, 0);
1736 if (!data->rpc_done)
1737 return status;
1738 if (status != 0) {
1739 if (status == -NFS4ERR_BADNAME &&
1740 !(o_arg->open_flags & O_CREAT))
1741 return -ENOENT;
1742 return status;
1745 nfs_fattr_map_and_free_names(server, &data->f_attr);
1747 if (o_arg->open_flags & O_CREAT)
1748 update_changeattr(dir, &o_res->cinfo);
1749 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1750 server->caps &= ~NFS_CAP_POSIX_LOCK;
1751 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1752 status = _nfs4_proc_open_confirm(data);
1753 if (status != 0)
1754 return status;
1756 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1757 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1758 return 0;
1761 static int nfs4_recover_expired_lease(struct nfs_server *server)
1763 return nfs4_client_recover_expired_lease(server->nfs_client);
1767 * OPEN_EXPIRED:
1768 * reclaim state on the server after a network partition.
1769 * Assumes caller holds the appropriate lock
1771 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1773 struct nfs4_opendata *opendata;
1774 int ret;
1776 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1777 NFS4_OPEN_CLAIM_FH);
1778 if (IS_ERR(opendata))
1779 return PTR_ERR(opendata);
1780 ret = nfs4_open_recover(opendata, state);
1781 if (ret == -ESTALE)
1782 d_drop(ctx->dentry);
1783 nfs4_opendata_put(opendata);
1784 return ret;
1787 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1789 struct nfs_server *server = NFS_SERVER(state->inode);
1790 struct nfs4_exception exception = { };
1791 int err;
1793 do {
1794 err = _nfs4_open_expired(ctx, state);
1795 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1796 continue;
1797 switch (err) {
1798 default:
1799 goto out;
1800 case -NFS4ERR_GRACE:
1801 case -NFS4ERR_DELAY:
1802 nfs4_handle_exception(server, err, &exception);
1803 err = 0;
1805 } while (exception.retry);
1806 out:
1807 return err;
1810 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1812 struct nfs_open_context *ctx;
1813 int ret;
1815 ctx = nfs4_state_find_open_context(state);
1816 if (IS_ERR(ctx))
1817 return -EAGAIN;
1818 ret = nfs4_do_open_expired(ctx, state);
1819 put_nfs_open_context(ctx);
1820 return ret;
1823 #if defined(CONFIG_NFS_V4_1)
1824 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1826 struct nfs_server *server = NFS_SERVER(state->inode);
1827 nfs4_stateid *stateid = &state->stateid;
1828 int status;
1830 /* If a state reset has been done, test_stateid is unneeded */
1831 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1832 return;
1834 status = nfs41_test_stateid(server, stateid);
1835 if (status != NFS_OK) {
1836 /* Free the stateid unless the server explicitly
1837 * informs us the stateid is unrecognized. */
1838 if (status != -NFS4ERR_BAD_STATEID)
1839 nfs41_free_stateid(server, stateid);
1840 nfs_remove_bad_delegation(state->inode);
1842 write_seqlock(&state->seqlock);
1843 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1844 write_sequnlock(&state->seqlock);
1845 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1850 * nfs41_check_open_stateid - possibly free an open stateid
1852 * @state: NFSv4 state for an inode
1854 * Returns NFS_OK if recovery for this stateid is now finished.
1855 * Otherwise a negative NFS4ERR value is returned.
1857 static int nfs41_check_open_stateid(struct nfs4_state *state)
1859 struct nfs_server *server = NFS_SERVER(state->inode);
1860 nfs4_stateid *stateid = &state->open_stateid;
1861 int status;
1863 /* If a state reset has been done, test_stateid is unneeded */
1864 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1865 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1866 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1867 return -NFS4ERR_BAD_STATEID;
1869 status = nfs41_test_stateid(server, stateid);
1870 if (status != NFS_OK) {
1871 /* Free the stateid unless the server explicitly
1872 * informs us the stateid is unrecognized. */
1873 if (status != -NFS4ERR_BAD_STATEID)
1874 nfs41_free_stateid(server, stateid);
1876 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1877 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1878 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1880 return status;
1883 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1885 int status;
1887 nfs41_clear_delegation_stateid(state);
1888 status = nfs41_check_open_stateid(state);
1889 if (status != NFS_OK)
1890 status = nfs4_open_expired(sp, state);
1891 return status;
1893 #endif
1896 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1897 * fields corresponding to attributes that were used to store the verifier.
1898 * Make sure we clobber those fields in the later setattr call
1900 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1902 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1903 !(sattr->ia_valid & ATTR_ATIME_SET))
1904 sattr->ia_valid |= ATTR_ATIME;
1906 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1907 !(sattr->ia_valid & ATTR_MTIME_SET))
1908 sattr->ia_valid |= ATTR_MTIME;
1911 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1912 fmode_t fmode,
1913 int flags,
1914 struct nfs4_state **res)
1916 struct nfs4_state_owner *sp = opendata->owner;
1917 struct nfs_server *server = sp->so_server;
1918 struct nfs4_state *state;
1919 unsigned int seq;
1920 int ret;
1922 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1924 ret = _nfs4_proc_open(opendata);
1925 if (ret != 0)
1926 goto out;
1928 state = nfs4_opendata_to_nfs4_state(opendata);
1929 ret = PTR_ERR(state);
1930 if (IS_ERR(state))
1931 goto out;
1932 if (server->caps & NFS_CAP_POSIX_LOCK)
1933 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1935 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1936 if (ret != 0)
1937 goto out;
1939 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
1940 nfs4_schedule_stateid_recovery(server, state);
1941 *res = state;
1942 out:
1943 return ret;
1947 * Returns a referenced nfs4_state
1949 static int _nfs4_do_open(struct inode *dir,
1950 struct dentry *dentry,
1951 fmode_t fmode,
1952 int flags,
1953 struct iattr *sattr,
1954 struct rpc_cred *cred,
1955 struct nfs4_state **res,
1956 struct nfs4_threshold **ctx_th)
1958 struct nfs4_state_owner *sp;
1959 struct nfs4_state *state = NULL;
1960 struct nfs_server *server = NFS_SERVER(dir);
1961 struct nfs4_opendata *opendata;
1962 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
1963 int status;
1965 /* Protect against reboot recovery conflicts */
1966 status = -ENOMEM;
1967 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1968 if (sp == NULL) {
1969 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1970 goto out_err;
1972 status = nfs4_recover_expired_lease(server);
1973 if (status != 0)
1974 goto err_put_state_owner;
1975 if (dentry->d_inode != NULL)
1976 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1977 status = -ENOMEM;
1978 if (dentry->d_inode)
1979 claim = NFS4_OPEN_CLAIM_FH;
1980 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
1981 claim, GFP_KERNEL);
1982 if (opendata == NULL)
1983 goto err_put_state_owner;
1985 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1986 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1987 if (!opendata->f_attr.mdsthreshold)
1988 goto err_opendata_put;
1989 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1991 if (dentry->d_inode != NULL)
1992 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1994 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
1995 if (status != 0)
1996 goto err_opendata_put;
1998 if (opendata->o_arg.open_flags & O_EXCL) {
1999 nfs4_exclusive_attrset(opendata, sattr);
2001 nfs_fattr_init(opendata->o_res.f_attr);
2002 status = nfs4_do_setattr(state->inode, cred,
2003 opendata->o_res.f_attr, sattr,
2004 state);
2005 if (status == 0)
2006 nfs_setattr_update_inode(state->inode, sattr);
2007 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2010 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2011 *ctx_th = opendata->f_attr.mdsthreshold;
2012 else
2013 kfree(opendata->f_attr.mdsthreshold);
2014 opendata->f_attr.mdsthreshold = NULL;
2016 nfs4_opendata_put(opendata);
2017 nfs4_put_state_owner(sp);
2018 *res = state;
2019 return 0;
2020 err_opendata_put:
2021 kfree(opendata->f_attr.mdsthreshold);
2022 nfs4_opendata_put(opendata);
2023 err_put_state_owner:
2024 nfs4_put_state_owner(sp);
2025 out_err:
2026 *res = NULL;
2027 return status;
2031 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2032 struct dentry *dentry,
2033 fmode_t fmode,
2034 int flags,
2035 struct iattr *sattr,
2036 struct rpc_cred *cred,
2037 struct nfs4_threshold **ctx_th)
2039 struct nfs_server *server = NFS_SERVER(dir);
2040 struct nfs4_exception exception = { };
2041 struct nfs4_state *res;
2042 int status;
2044 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2045 do {
2046 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2047 &res, ctx_th);
2048 if (status == 0)
2049 break;
2050 /* NOTE: BAD_SEQID means the server and client disagree about the
2051 * book-keeping w.r.t. state-changing operations
2052 * (OPEN/CLOSE/LOCK/LOCKU...)
2053 * It is actually a sign of a bug on the client or on the server.
2055 * If we receive a BAD_SEQID error in the particular case of
2056 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2057 * have unhashed the old state_owner for us, and that we can
2058 * therefore safely retry using a new one. We should still warn
2059 * the user though...
2061 if (status == -NFS4ERR_BAD_SEQID) {
2062 pr_warn_ratelimited("NFS: v4 server %s "
2063 " returned a bad sequence-id error!\n",
2064 NFS_SERVER(dir)->nfs_client->cl_hostname);
2065 exception.retry = 1;
2066 continue;
2069 * BAD_STATEID on OPEN means that the server cancelled our
2070 * state before it received the OPEN_CONFIRM.
2071 * Recover by retrying the request as per the discussion
2072 * on Page 181 of RFC3530.
2074 if (status == -NFS4ERR_BAD_STATEID) {
2075 exception.retry = 1;
2076 continue;
2078 if (status == -EAGAIN) {
2079 /* We must have found a delegation */
2080 exception.retry = 1;
2081 continue;
2083 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2084 continue;
2085 res = ERR_PTR(nfs4_handle_exception(server,
2086 status, &exception));
2087 } while (exception.retry);
2088 return res;
2091 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2092 struct nfs_fattr *fattr, struct iattr *sattr,
2093 struct nfs4_state *state)
2095 struct nfs_server *server = NFS_SERVER(inode);
2096 struct nfs_setattrargs arg = {
2097 .fh = NFS_FH(inode),
2098 .iap = sattr,
2099 .server = server,
2100 .bitmask = server->attr_bitmask,
2102 struct nfs_setattrres res = {
2103 .fattr = fattr,
2104 .server = server,
2106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2108 .rpc_argp = &arg,
2109 .rpc_resp = &res,
2110 .rpc_cred = cred,
2112 unsigned long timestamp = jiffies;
2113 int status;
2115 nfs_fattr_init(fattr);
2117 if (state != NULL && nfs4_valid_open_stateid(state)) {
2118 struct nfs_lockowner lockowner = {
2119 .l_owner = current->files,
2120 .l_pid = current->tgid,
2122 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2123 &lockowner);
2124 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2125 FMODE_WRITE)) {
2126 /* Use that stateid */
2127 } else
2128 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2130 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2131 if (status == 0 && state != NULL)
2132 renew_lease(server, timestamp);
2133 return status;
2136 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2137 struct nfs_fattr *fattr, struct iattr *sattr,
2138 struct nfs4_state *state)
2140 struct nfs_server *server = NFS_SERVER(inode);
2141 struct nfs4_exception exception = {
2142 .state = state,
2143 .inode = inode,
2145 int err;
2146 do {
2147 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2148 switch (err) {
2149 case -NFS4ERR_OPENMODE:
2150 if (state && !(state->state & FMODE_WRITE)) {
2151 err = -EBADF;
2152 if (sattr->ia_valid & ATTR_OPEN)
2153 err = -EACCES;
2154 goto out;
2157 err = nfs4_handle_exception(server, err, &exception);
2158 } while (exception.retry);
2159 out:
2160 return err;
2163 struct nfs4_closedata {
2164 struct inode *inode;
2165 struct nfs4_state *state;
2166 struct nfs_closeargs arg;
2167 struct nfs_closeres res;
2168 struct nfs_fattr fattr;
2169 unsigned long timestamp;
2170 bool roc;
2171 u32 roc_barrier;
2174 static void nfs4_free_closedata(void *data)
2176 struct nfs4_closedata *calldata = data;
2177 struct nfs4_state_owner *sp = calldata->state->owner;
2178 struct super_block *sb = calldata->state->inode->i_sb;
2180 if (calldata->roc)
2181 pnfs_roc_release(calldata->state->inode);
2182 nfs4_put_open_state(calldata->state);
2183 nfs_free_seqid(calldata->arg.seqid);
2184 nfs4_put_state_owner(sp);
2185 nfs_sb_deactive(sb);
2186 kfree(calldata);
2189 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2190 fmode_t fmode)
2192 spin_lock(&state->owner->so_lock);
2193 if (!(fmode & FMODE_READ))
2194 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2195 if (!(fmode & FMODE_WRITE))
2196 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2197 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2198 spin_unlock(&state->owner->so_lock);
2201 static void nfs4_close_done(struct rpc_task *task, void *data)
2203 struct nfs4_closedata *calldata = data;
2204 struct nfs4_state *state = calldata->state;
2205 struct nfs_server *server = NFS_SERVER(calldata->inode);
2207 dprintk("%s: begin!\n", __func__);
2208 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2209 return;
2210 /* hmm. we are done with the inode, and in the process of freeing
2211 * the state_owner. we keep this around to process errors
2213 switch (task->tk_status) {
2214 case 0:
2215 if (calldata->roc)
2216 pnfs_roc_set_barrier(state->inode,
2217 calldata->roc_barrier);
2218 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2219 renew_lease(server, calldata->timestamp);
2220 nfs4_close_clear_stateid_flags(state,
2221 calldata->arg.fmode);
2222 break;
2223 case -NFS4ERR_STALE_STATEID:
2224 case -NFS4ERR_OLD_STATEID:
2225 case -NFS4ERR_BAD_STATEID:
2226 case -NFS4ERR_EXPIRED:
2227 if (calldata->arg.fmode == 0)
2228 break;
2229 default:
2230 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2231 rpc_restart_call_prepare(task);
2233 nfs_release_seqid(calldata->arg.seqid);
2234 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2235 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2238 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2240 struct nfs4_closedata *calldata = data;
2241 struct nfs4_state *state = calldata->state;
2242 struct inode *inode = calldata->inode;
2243 int call_close = 0;
2245 dprintk("%s: begin!\n", __func__);
2246 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2247 goto out_wait;
2249 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2250 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2251 spin_lock(&state->owner->so_lock);
2252 /* Calculate the change in open mode */
2253 if (state->n_rdwr == 0) {
2254 if (state->n_rdonly == 0) {
2255 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2256 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2257 calldata->arg.fmode &= ~FMODE_READ;
2259 if (state->n_wronly == 0) {
2260 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2262 calldata->arg.fmode &= ~FMODE_WRITE;
2265 if (!nfs4_valid_open_stateid(state))
2266 call_close = 0;
2267 spin_unlock(&state->owner->so_lock);
2269 if (!call_close) {
2270 /* Note: exit _without_ calling nfs4_close_done */
2271 goto out_no_action;
2274 if (calldata->arg.fmode == 0) {
2275 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2276 if (calldata->roc &&
2277 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2278 goto out_wait;
2281 nfs_fattr_init(calldata->res.fattr);
2282 calldata->timestamp = jiffies;
2283 if (nfs4_setup_sequence(NFS_SERVER(inode),
2284 &calldata->arg.seq_args,
2285 &calldata->res.seq_res,
2286 task) != 0)
2287 nfs_release_seqid(calldata->arg.seqid);
2288 dprintk("%s: done!\n", __func__);
2289 return;
2290 out_no_action:
2291 task->tk_action = NULL;
2292 out_wait:
2293 nfs4_sequence_done(task, &calldata->res.seq_res);
2296 static const struct rpc_call_ops nfs4_close_ops = {
2297 .rpc_call_prepare = nfs4_close_prepare,
2298 .rpc_call_done = nfs4_close_done,
2299 .rpc_release = nfs4_free_closedata,
2303 * It is possible for data to be read/written from a mem-mapped file
2304 * after the sys_close call (which hits the vfs layer as a flush).
2305 * This means that we can't safely call nfsv4 close on a file until
2306 * the inode is cleared. This in turn means that we are not good
2307 * NFSv4 citizens - we do not indicate to the server to update the file's
2308 * share state even when we are done with one of the three share
2309 * stateid's in the inode.
2311 * NOTE: Caller must be holding the sp->so_owner semaphore!
2313 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2315 struct nfs_server *server = NFS_SERVER(state->inode);
2316 struct nfs4_closedata *calldata;
2317 struct nfs4_state_owner *sp = state->owner;
2318 struct rpc_task *task;
2319 struct rpc_message msg = {
2320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2321 .rpc_cred = state->owner->so_cred,
2323 struct rpc_task_setup task_setup_data = {
2324 .rpc_client = server->client,
2325 .rpc_message = &msg,
2326 .callback_ops = &nfs4_close_ops,
2327 .workqueue = nfsiod_workqueue,
2328 .flags = RPC_TASK_ASYNC,
2330 int status = -ENOMEM;
2332 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2333 if (calldata == NULL)
2334 goto out;
2335 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2336 calldata->inode = state->inode;
2337 calldata->state = state;
2338 calldata->arg.fh = NFS_FH(state->inode);
2339 calldata->arg.stateid = &state->open_stateid;
2340 /* Serialization for the sequence id */
2341 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2342 if (calldata->arg.seqid == NULL)
2343 goto out_free_calldata;
2344 calldata->arg.fmode = 0;
2345 calldata->arg.bitmask = server->cache_consistency_bitmask;
2346 calldata->res.fattr = &calldata->fattr;
2347 calldata->res.seqid = calldata->arg.seqid;
2348 calldata->res.server = server;
2349 calldata->roc = pnfs_roc(state->inode);
2350 nfs_sb_active(calldata->inode->i_sb);
2352 msg.rpc_argp = &calldata->arg;
2353 msg.rpc_resp = &calldata->res;
2354 task_setup_data.callback_data = calldata;
2355 task = rpc_run_task(&task_setup_data);
2356 if (IS_ERR(task))
2357 return PTR_ERR(task);
2358 status = 0;
2359 if (wait)
2360 status = rpc_wait_for_completion_task(task);
2361 rpc_put_task(task);
2362 return status;
2363 out_free_calldata:
2364 kfree(calldata);
2365 out:
2366 nfs4_put_open_state(state);
2367 nfs4_put_state_owner(sp);
2368 return status;
2371 static struct inode *
2372 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2374 struct nfs4_state *state;
2376 /* Protect against concurrent sillydeletes */
2377 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2378 ctx->cred, &ctx->mdsthreshold);
2379 if (IS_ERR(state))
2380 return ERR_CAST(state);
2381 ctx->state = state;
2382 return igrab(state->inode);
2385 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2387 if (ctx->state == NULL)
2388 return;
2389 if (is_sync)
2390 nfs4_close_sync(ctx->state, ctx->mode);
2391 else
2392 nfs4_close_state(ctx->state, ctx->mode);
2395 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2397 struct nfs4_server_caps_arg args = {
2398 .fhandle = fhandle,
2400 struct nfs4_server_caps_res res = {};
2401 struct rpc_message msg = {
2402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2403 .rpc_argp = &args,
2404 .rpc_resp = &res,
2406 int status;
2408 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2409 if (status == 0) {
2410 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2411 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2412 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2413 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2414 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2415 NFS_CAP_CTIME|NFS_CAP_MTIME);
2416 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2417 server->caps |= NFS_CAP_ACLS;
2418 if (res.has_links != 0)
2419 server->caps |= NFS_CAP_HARDLINKS;
2420 if (res.has_symlinks != 0)
2421 server->caps |= NFS_CAP_SYMLINKS;
2422 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2423 server->caps |= NFS_CAP_FILEID;
2424 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2425 server->caps |= NFS_CAP_MODE;
2426 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2427 server->caps |= NFS_CAP_NLINK;
2428 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2429 server->caps |= NFS_CAP_OWNER;
2430 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2431 server->caps |= NFS_CAP_OWNER_GROUP;
2432 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2433 server->caps |= NFS_CAP_ATIME;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2435 server->caps |= NFS_CAP_CTIME;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2437 server->caps |= NFS_CAP_MTIME;
2439 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2440 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2441 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2442 server->acl_bitmask = res.acl_bitmask;
2443 server->fh_expire_type = res.fh_expire_type;
2446 return status;
2449 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2451 struct nfs4_exception exception = { };
2452 int err;
2453 do {
2454 err = nfs4_handle_exception(server,
2455 _nfs4_server_capabilities(server, fhandle),
2456 &exception);
2457 } while (exception.retry);
2458 return err;
2461 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2462 struct nfs_fsinfo *info)
2464 struct nfs4_lookup_root_arg args = {
2465 .bitmask = nfs4_fattr_bitmap,
2467 struct nfs4_lookup_res res = {
2468 .server = server,
2469 .fattr = info->fattr,
2470 .fh = fhandle,
2472 struct rpc_message msg = {
2473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2474 .rpc_argp = &args,
2475 .rpc_resp = &res,
2478 nfs_fattr_init(info->fattr);
2479 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2482 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2483 struct nfs_fsinfo *info)
2485 struct nfs4_exception exception = { };
2486 int err;
2487 do {
2488 err = _nfs4_lookup_root(server, fhandle, info);
2489 switch (err) {
2490 case 0:
2491 case -NFS4ERR_WRONGSEC:
2492 goto out;
2493 default:
2494 err = nfs4_handle_exception(server, err, &exception);
2496 } while (exception.retry);
2497 out:
2498 return err;
2501 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2502 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2504 struct rpc_auth *auth;
2505 int ret;
2507 auth = rpcauth_create(flavor, server->client);
2508 if (IS_ERR(auth)) {
2509 ret = -EIO;
2510 goto out;
2512 ret = nfs4_lookup_root(server, fhandle, info);
2513 out:
2514 return ret;
2517 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2518 struct nfs_fsinfo *info)
2520 int i, len, status = 0;
2521 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2523 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2524 if (len < 0)
2525 return len;
2527 for (i = 0; i < len; i++) {
2528 /* AUTH_UNIX is the default flavor if none was specified,
2529 * thus has already been tried. */
2530 if (flav_array[i] == RPC_AUTH_UNIX)
2531 continue;
2533 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2534 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2535 continue;
2536 break;
2539 * -EACCESS could mean that the user doesn't have correct permissions
2540 * to access the mount. It could also mean that we tried to mount
2541 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2542 * existing mount programs don't handle -EACCES very well so it should
2543 * be mapped to -EPERM instead.
2545 if (status == -EACCES)
2546 status = -EPERM;
2547 return status;
2551 * get the file handle for the "/" directory on the server
2553 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2554 struct nfs_fsinfo *info)
2556 int minor_version = server->nfs_client->cl_minorversion;
2557 int status = nfs4_lookup_root(server, fhandle, info);
2558 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2560 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2561 * by nfs4_map_errors() as this function exits.
2563 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2564 if (status == 0)
2565 status = nfs4_server_capabilities(server, fhandle);
2566 if (status == 0)
2567 status = nfs4_do_fsinfo(server, fhandle, info);
2568 return nfs4_map_errors(status);
2571 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2572 struct nfs_fsinfo *info)
2574 int error;
2575 struct nfs_fattr *fattr = info->fattr;
2577 error = nfs4_server_capabilities(server, mntfh);
2578 if (error < 0) {
2579 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2580 return error;
2583 error = nfs4_proc_getattr(server, mntfh, fattr);
2584 if (error < 0) {
2585 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2586 return error;
2589 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2590 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2591 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2593 return error;
2597 * Get locations and (maybe) other attributes of a referral.
2598 * Note that we'll actually follow the referral later when
2599 * we detect fsid mismatch in inode revalidation
2601 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2602 const struct qstr *name, struct nfs_fattr *fattr,
2603 struct nfs_fh *fhandle)
2605 int status = -ENOMEM;
2606 struct page *page = NULL;
2607 struct nfs4_fs_locations *locations = NULL;
2609 page = alloc_page(GFP_KERNEL);
2610 if (page == NULL)
2611 goto out;
2612 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2613 if (locations == NULL)
2614 goto out;
2616 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2617 if (status != 0)
2618 goto out;
2619 /* Make sure server returned a different fsid for the referral */
2620 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2621 dprintk("%s: server did not return a different fsid for"
2622 " a referral at %s\n", __func__, name->name);
2623 status = -EIO;
2624 goto out;
2626 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2627 nfs_fixup_referral_attributes(&locations->fattr);
2629 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2630 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2631 memset(fhandle, 0, sizeof(struct nfs_fh));
2632 out:
2633 if (page)
2634 __free_page(page);
2635 kfree(locations);
2636 return status;
2639 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2641 struct nfs4_getattr_arg args = {
2642 .fh = fhandle,
2643 .bitmask = server->attr_bitmask,
2645 struct nfs4_getattr_res res = {
2646 .fattr = fattr,
2647 .server = server,
2649 struct rpc_message msg = {
2650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2651 .rpc_argp = &args,
2652 .rpc_resp = &res,
2655 nfs_fattr_init(fattr);
2656 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2659 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2661 struct nfs4_exception exception = { };
2662 int err;
2663 do {
2664 err = nfs4_handle_exception(server,
2665 _nfs4_proc_getattr(server, fhandle, fattr),
2666 &exception);
2667 } while (exception.retry);
2668 return err;
2672 * The file is not closed if it is opened due to the a request to change
2673 * the size of the file. The open call will not be needed once the
2674 * VFS layer lookup-intents are implemented.
2676 * Close is called when the inode is destroyed.
2677 * If we haven't opened the file for O_WRONLY, we
2678 * need to in the size_change case to obtain a stateid.
2680 * Got race?
2681 * Because OPEN is always done by name in nfsv4, it is
2682 * possible that we opened a different file by the same
2683 * name. We can recognize this race condition, but we
2684 * can't do anything about it besides returning an error.
2686 * This will be fixed with VFS changes (lookup-intent).
2688 static int
2689 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2690 struct iattr *sattr)
2692 struct inode *inode = dentry->d_inode;
2693 struct rpc_cred *cred = NULL;
2694 struct nfs4_state *state = NULL;
2695 int status;
2697 if (pnfs_ld_layoutret_on_setattr(inode))
2698 pnfs_commit_and_return_layout(inode);
2700 nfs_fattr_init(fattr);
2702 /* Deal with open(O_TRUNC) */
2703 if (sattr->ia_valid & ATTR_OPEN)
2704 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2706 /* Optimization: if the end result is no change, don't RPC */
2707 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2708 return 0;
2710 /* Search for an existing open(O_WRITE) file */
2711 if (sattr->ia_valid & ATTR_FILE) {
2712 struct nfs_open_context *ctx;
2714 ctx = nfs_file_open_context(sattr->ia_file);
2715 if (ctx) {
2716 cred = ctx->cred;
2717 state = ctx->state;
2721 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2722 if (status == 0)
2723 nfs_setattr_update_inode(inode, sattr);
2724 return status;
2727 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2728 const struct qstr *name, struct nfs_fh *fhandle,
2729 struct nfs_fattr *fattr)
2731 struct nfs_server *server = NFS_SERVER(dir);
2732 int status;
2733 struct nfs4_lookup_arg args = {
2734 .bitmask = server->attr_bitmask,
2735 .dir_fh = NFS_FH(dir),
2736 .name = name,
2738 struct nfs4_lookup_res res = {
2739 .server = server,
2740 .fattr = fattr,
2741 .fh = fhandle,
2743 struct rpc_message msg = {
2744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2745 .rpc_argp = &args,
2746 .rpc_resp = &res,
2749 nfs_fattr_init(fattr);
2751 dprintk("NFS call lookup %s\n", name->name);
2752 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2753 dprintk("NFS reply lookup: %d\n", status);
2754 return status;
2757 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2759 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2760 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2761 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2762 fattr->nlink = 2;
2765 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2766 struct qstr *name, struct nfs_fh *fhandle,
2767 struct nfs_fattr *fattr)
2769 struct nfs4_exception exception = { };
2770 struct rpc_clnt *client = *clnt;
2771 int err;
2772 do {
2773 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2774 switch (err) {
2775 case -NFS4ERR_BADNAME:
2776 err = -ENOENT;
2777 goto out;
2778 case -NFS4ERR_MOVED:
2779 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2780 goto out;
2781 case -NFS4ERR_WRONGSEC:
2782 err = -EPERM;
2783 if (client != *clnt)
2784 goto out;
2786 client = nfs4_create_sec_client(client, dir, name);
2787 if (IS_ERR(client))
2788 return PTR_ERR(client);
2790 exception.retry = 1;
2791 break;
2792 default:
2793 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2795 } while (exception.retry);
2797 out:
2798 if (err == 0)
2799 *clnt = client;
2800 else if (client != *clnt)
2801 rpc_shutdown_client(client);
2803 return err;
2806 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2807 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2809 int status;
2810 struct rpc_clnt *client = NFS_CLIENT(dir);
2812 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2813 if (client != NFS_CLIENT(dir)) {
2814 rpc_shutdown_client(client);
2815 nfs_fixup_secinfo_attributes(fattr);
2817 return status;
2820 struct rpc_clnt *
2821 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2822 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2824 int status;
2825 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2827 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2828 if (status < 0) {
2829 rpc_shutdown_client(client);
2830 return ERR_PTR(status);
2832 return client;
2835 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2837 struct nfs_server *server = NFS_SERVER(inode);
2838 struct nfs4_accessargs args = {
2839 .fh = NFS_FH(inode),
2840 .bitmask = server->cache_consistency_bitmask,
2842 struct nfs4_accessres res = {
2843 .server = server,
2845 struct rpc_message msg = {
2846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2847 .rpc_argp = &args,
2848 .rpc_resp = &res,
2849 .rpc_cred = entry->cred,
2851 int mode = entry->mask;
2852 int status;
2855 * Determine which access bits we want to ask for...
2857 if (mode & MAY_READ)
2858 args.access |= NFS4_ACCESS_READ;
2859 if (S_ISDIR(inode->i_mode)) {
2860 if (mode & MAY_WRITE)
2861 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2862 if (mode & MAY_EXEC)
2863 args.access |= NFS4_ACCESS_LOOKUP;
2864 } else {
2865 if (mode & MAY_WRITE)
2866 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2867 if (mode & MAY_EXEC)
2868 args.access |= NFS4_ACCESS_EXECUTE;
2871 res.fattr = nfs_alloc_fattr();
2872 if (res.fattr == NULL)
2873 return -ENOMEM;
2875 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2876 if (!status) {
2877 nfs_access_set_mask(entry, res.access);
2878 nfs_refresh_inode(inode, res.fattr);
2880 nfs_free_fattr(res.fattr);
2881 return status;
2884 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2886 struct nfs4_exception exception = { };
2887 int err;
2888 do {
2889 err = nfs4_handle_exception(NFS_SERVER(inode),
2890 _nfs4_proc_access(inode, entry),
2891 &exception);
2892 } while (exception.retry);
2893 return err;
2897 * TODO: For the time being, we don't try to get any attributes
2898 * along with any of the zero-copy operations READ, READDIR,
2899 * READLINK, WRITE.
2901 * In the case of the first three, we want to put the GETATTR
2902 * after the read-type operation -- this is because it is hard
2903 * to predict the length of a GETATTR response in v4, and thus
2904 * align the READ data correctly. This means that the GETATTR
2905 * may end up partially falling into the page cache, and we should
2906 * shift it into the 'tail' of the xdr_buf before processing.
2907 * To do this efficiently, we need to know the total length
2908 * of data received, which doesn't seem to be available outside
2909 * of the RPC layer.
2911 * In the case of WRITE, we also want to put the GETATTR after
2912 * the operation -- in this case because we want to make sure
2913 * we get the post-operation mtime and size.
2915 * Both of these changes to the XDR layer would in fact be quite
2916 * minor, but I decided to leave them for a subsequent patch.
2918 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2919 unsigned int pgbase, unsigned int pglen)
2921 struct nfs4_readlink args = {
2922 .fh = NFS_FH(inode),
2923 .pgbase = pgbase,
2924 .pglen = pglen,
2925 .pages = &page,
2927 struct nfs4_readlink_res res;
2928 struct rpc_message msg = {
2929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2930 .rpc_argp = &args,
2931 .rpc_resp = &res,
2934 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2937 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2938 unsigned int pgbase, unsigned int pglen)
2940 struct nfs4_exception exception = { };
2941 int err;
2942 do {
2943 err = nfs4_handle_exception(NFS_SERVER(inode),
2944 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2945 &exception);
2946 } while (exception.retry);
2947 return err;
2951 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2953 static int
2954 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2955 int flags)
2957 struct nfs_open_context *ctx;
2958 struct nfs4_state *state;
2959 int status = 0;
2961 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2962 if (IS_ERR(ctx))
2963 return PTR_ERR(ctx);
2965 sattr->ia_mode &= ~current_umask();
2966 state = nfs4_do_open(dir, dentry, ctx->mode,
2967 flags, sattr, ctx->cred,
2968 &ctx->mdsthreshold);
2969 d_drop(dentry);
2970 if (IS_ERR(state)) {
2971 status = PTR_ERR(state);
2972 goto out;
2974 d_add(dentry, igrab(state->inode));
2975 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2976 ctx->state = state;
2977 out:
2978 put_nfs_open_context(ctx);
2979 return status;
2982 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2984 struct nfs_server *server = NFS_SERVER(dir);
2985 struct nfs_removeargs args = {
2986 .fh = NFS_FH(dir),
2987 .name = *name,
2989 struct nfs_removeres res = {
2990 .server = server,
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2994 .rpc_argp = &args,
2995 .rpc_resp = &res,
2997 int status;
2999 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3000 if (status == 0)
3001 update_changeattr(dir, &res.cinfo);
3002 return status;
3005 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3007 struct nfs4_exception exception = { };
3008 int err;
3009 do {
3010 err = nfs4_handle_exception(NFS_SERVER(dir),
3011 _nfs4_proc_remove(dir, name),
3012 &exception);
3013 } while (exception.retry);
3014 return err;
3017 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3019 struct nfs_server *server = NFS_SERVER(dir);
3020 struct nfs_removeargs *args = msg->rpc_argp;
3021 struct nfs_removeres *res = msg->rpc_resp;
3023 res->server = server;
3024 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3025 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3028 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3030 nfs4_setup_sequence(NFS_SERVER(data->dir),
3031 &data->args.seq_args,
3032 &data->res.seq_res,
3033 task);
3036 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3038 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3040 if (!nfs4_sequence_done(task, &res->seq_res))
3041 return 0;
3042 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3043 return 0;
3044 update_changeattr(dir, &res->cinfo);
3045 return 1;
3048 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3050 struct nfs_server *server = NFS_SERVER(dir);
3051 struct nfs_renameargs *arg = msg->rpc_argp;
3052 struct nfs_renameres *res = msg->rpc_resp;
3054 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3055 res->server = server;
3056 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3059 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3061 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3062 &data->args.seq_args,
3063 &data->res.seq_res,
3064 task);
3067 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3068 struct inode *new_dir)
3070 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3072 if (!nfs4_sequence_done(task, &res->seq_res))
3073 return 0;
3074 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3075 return 0;
3077 update_changeattr(old_dir, &res->old_cinfo);
3078 update_changeattr(new_dir, &res->new_cinfo);
3079 return 1;
3082 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3083 struct inode *new_dir, struct qstr *new_name)
3085 struct nfs_server *server = NFS_SERVER(old_dir);
3086 struct nfs_renameargs arg = {
3087 .old_dir = NFS_FH(old_dir),
3088 .new_dir = NFS_FH(new_dir),
3089 .old_name = old_name,
3090 .new_name = new_name,
3092 struct nfs_renameres res = {
3093 .server = server,
3095 struct rpc_message msg = {
3096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3097 .rpc_argp = &arg,
3098 .rpc_resp = &res,
3100 int status = -ENOMEM;
3102 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3103 if (!status) {
3104 update_changeattr(old_dir, &res.old_cinfo);
3105 update_changeattr(new_dir, &res.new_cinfo);
3107 return status;
3110 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3111 struct inode *new_dir, struct qstr *new_name)
3113 struct nfs4_exception exception = { };
3114 int err;
3115 do {
3116 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3117 _nfs4_proc_rename(old_dir, old_name,
3118 new_dir, new_name),
3119 &exception);
3120 } while (exception.retry);
3121 return err;
3124 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3126 struct nfs_server *server = NFS_SERVER(inode);
3127 struct nfs4_link_arg arg = {
3128 .fh = NFS_FH(inode),
3129 .dir_fh = NFS_FH(dir),
3130 .name = name,
3131 .bitmask = server->attr_bitmask,
3133 struct nfs4_link_res res = {
3134 .server = server,
3136 struct rpc_message msg = {
3137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3138 .rpc_argp = &arg,
3139 .rpc_resp = &res,
3141 int status = -ENOMEM;
3143 res.fattr = nfs_alloc_fattr();
3144 if (res.fattr == NULL)
3145 goto out;
3147 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3148 if (!status) {
3149 update_changeattr(dir, &res.cinfo);
3150 nfs_post_op_update_inode(inode, res.fattr);
3152 out:
3153 nfs_free_fattr(res.fattr);
3154 return status;
3157 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3159 struct nfs4_exception exception = { };
3160 int err;
3161 do {
3162 err = nfs4_handle_exception(NFS_SERVER(inode),
3163 _nfs4_proc_link(inode, dir, name),
3164 &exception);
3165 } while (exception.retry);
3166 return err;
3169 struct nfs4_createdata {
3170 struct rpc_message msg;
3171 struct nfs4_create_arg arg;
3172 struct nfs4_create_res res;
3173 struct nfs_fh fh;
3174 struct nfs_fattr fattr;
3177 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3178 struct qstr *name, struct iattr *sattr, u32 ftype)
3180 struct nfs4_createdata *data;
3182 data = kzalloc(sizeof(*data), GFP_KERNEL);
3183 if (data != NULL) {
3184 struct nfs_server *server = NFS_SERVER(dir);
3186 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3187 data->msg.rpc_argp = &data->arg;
3188 data->msg.rpc_resp = &data->res;
3189 data->arg.dir_fh = NFS_FH(dir);
3190 data->arg.server = server;
3191 data->arg.name = name;
3192 data->arg.attrs = sattr;
3193 data->arg.ftype = ftype;
3194 data->arg.bitmask = server->attr_bitmask;
3195 data->res.server = server;
3196 data->res.fh = &data->fh;
3197 data->res.fattr = &data->fattr;
3198 nfs_fattr_init(data->res.fattr);
3200 return data;
3203 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3205 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3206 &data->arg.seq_args, &data->res.seq_res, 1);
3207 if (status == 0) {
3208 update_changeattr(dir, &data->res.dir_cinfo);
3209 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3211 return status;
3214 static void nfs4_free_createdata(struct nfs4_createdata *data)
3216 kfree(data);
3219 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3220 struct page *page, unsigned int len, struct iattr *sattr)
3222 struct nfs4_createdata *data;
3223 int status = -ENAMETOOLONG;
3225 if (len > NFS4_MAXPATHLEN)
3226 goto out;
3228 status = -ENOMEM;
3229 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3230 if (data == NULL)
3231 goto out;
3233 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3234 data->arg.u.symlink.pages = &page;
3235 data->arg.u.symlink.len = len;
3237 status = nfs4_do_create(dir, dentry, data);
3239 nfs4_free_createdata(data);
3240 out:
3241 return status;
3244 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3245 struct page *page, unsigned int len, struct iattr *sattr)
3247 struct nfs4_exception exception = { };
3248 int err;
3249 do {
3250 err = nfs4_handle_exception(NFS_SERVER(dir),
3251 _nfs4_proc_symlink(dir, dentry, page,
3252 len, sattr),
3253 &exception);
3254 } while (exception.retry);
3255 return err;
3258 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3259 struct iattr *sattr)
3261 struct nfs4_createdata *data;
3262 int status = -ENOMEM;
3264 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3265 if (data == NULL)
3266 goto out;
3268 status = nfs4_do_create(dir, dentry, data);
3270 nfs4_free_createdata(data);
3271 out:
3272 return status;
3275 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3276 struct iattr *sattr)
3278 struct nfs4_exception exception = { };
3279 int err;
3281 sattr->ia_mode &= ~current_umask();
3282 do {
3283 err = nfs4_handle_exception(NFS_SERVER(dir),
3284 _nfs4_proc_mkdir(dir, dentry, sattr),
3285 &exception);
3286 } while (exception.retry);
3287 return err;
3290 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3291 u64 cookie, struct page **pages, unsigned int count, int plus)
3293 struct inode *dir = dentry->d_inode;
3294 struct nfs4_readdir_arg args = {
3295 .fh = NFS_FH(dir),
3296 .pages = pages,
3297 .pgbase = 0,
3298 .count = count,
3299 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3300 .plus = plus,
3302 struct nfs4_readdir_res res;
3303 struct rpc_message msg = {
3304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3305 .rpc_argp = &args,
3306 .rpc_resp = &res,
3307 .rpc_cred = cred,
3309 int status;
3311 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3312 dentry->d_parent->d_name.name,
3313 dentry->d_name.name,
3314 (unsigned long long)cookie);
3315 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3316 res.pgbase = args.pgbase;
3317 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3318 if (status >= 0) {
3319 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3320 status += args.pgbase;
3323 nfs_invalidate_atime(dir);
3325 dprintk("%s: returns %d\n", __func__, status);
3326 return status;
3329 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3330 u64 cookie, struct page **pages, unsigned int count, int plus)
3332 struct nfs4_exception exception = { };
3333 int err;
3334 do {
3335 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3336 _nfs4_proc_readdir(dentry, cred, cookie,
3337 pages, count, plus),
3338 &exception);
3339 } while (exception.retry);
3340 return err;
3343 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3344 struct iattr *sattr, dev_t rdev)
3346 struct nfs4_createdata *data;
3347 int mode = sattr->ia_mode;
3348 int status = -ENOMEM;
3350 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3351 if (data == NULL)
3352 goto out;
3354 if (S_ISFIFO(mode))
3355 data->arg.ftype = NF4FIFO;
3356 else if (S_ISBLK(mode)) {
3357 data->arg.ftype = NF4BLK;
3358 data->arg.u.device.specdata1 = MAJOR(rdev);
3359 data->arg.u.device.specdata2 = MINOR(rdev);
3361 else if (S_ISCHR(mode)) {
3362 data->arg.ftype = NF4CHR;
3363 data->arg.u.device.specdata1 = MAJOR(rdev);
3364 data->arg.u.device.specdata2 = MINOR(rdev);
3365 } else if (!S_ISSOCK(mode)) {
3366 status = -EINVAL;
3367 goto out_free;
3370 status = nfs4_do_create(dir, dentry, data);
3371 out_free:
3372 nfs4_free_createdata(data);
3373 out:
3374 return status;
3377 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3378 struct iattr *sattr, dev_t rdev)
3380 struct nfs4_exception exception = { };
3381 int err;
3383 sattr->ia_mode &= ~current_umask();
3384 do {
3385 err = nfs4_handle_exception(NFS_SERVER(dir),
3386 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3387 &exception);
3388 } while (exception.retry);
3389 return err;
3392 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3393 struct nfs_fsstat *fsstat)
3395 struct nfs4_statfs_arg args = {
3396 .fh = fhandle,
3397 .bitmask = server->attr_bitmask,
3399 struct nfs4_statfs_res res = {
3400 .fsstat = fsstat,
3402 struct rpc_message msg = {
3403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3404 .rpc_argp = &args,
3405 .rpc_resp = &res,
3408 nfs_fattr_init(fsstat->fattr);
3409 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3412 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3414 struct nfs4_exception exception = { };
3415 int err;
3416 do {
3417 err = nfs4_handle_exception(server,
3418 _nfs4_proc_statfs(server, fhandle, fsstat),
3419 &exception);
3420 } while (exception.retry);
3421 return err;
3424 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3425 struct nfs_fsinfo *fsinfo)
3427 struct nfs4_fsinfo_arg args = {
3428 .fh = fhandle,
3429 .bitmask = server->attr_bitmask,
3431 struct nfs4_fsinfo_res res = {
3432 .fsinfo = fsinfo,
3434 struct rpc_message msg = {
3435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3436 .rpc_argp = &args,
3437 .rpc_resp = &res,
3440 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3443 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3445 struct nfs4_exception exception = { };
3446 int err;
3448 do {
3449 err = nfs4_handle_exception(server,
3450 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3451 &exception);
3452 } while (exception.retry);
3453 return err;
3456 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3458 int error;
3460 nfs_fattr_init(fsinfo->fattr);
3461 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3462 if (error == 0) {
3463 /* block layout checks this! */
3464 server->pnfs_blksize = fsinfo->blksize;
3465 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3468 return error;
3471 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3472 struct nfs_pathconf *pathconf)
3474 struct nfs4_pathconf_arg args = {
3475 .fh = fhandle,
3476 .bitmask = server->attr_bitmask,
3478 struct nfs4_pathconf_res res = {
3479 .pathconf = pathconf,
3481 struct rpc_message msg = {
3482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3483 .rpc_argp = &args,
3484 .rpc_resp = &res,
3487 /* None of the pathconf attributes are mandatory to implement */
3488 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3489 memset(pathconf, 0, sizeof(*pathconf));
3490 return 0;
3493 nfs_fattr_init(pathconf->fattr);
3494 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3497 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3498 struct nfs_pathconf *pathconf)
3500 struct nfs4_exception exception = { };
3501 int err;
3503 do {
3504 err = nfs4_handle_exception(server,
3505 _nfs4_proc_pathconf(server, fhandle, pathconf),
3506 &exception);
3507 } while (exception.retry);
3508 return err;
3511 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3512 const struct nfs_open_context *ctx,
3513 const struct nfs_lock_context *l_ctx,
3514 fmode_t fmode)
3516 const struct nfs_lockowner *lockowner = NULL;
3518 if (l_ctx != NULL)
3519 lockowner = &l_ctx->lockowner;
3520 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
3522 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3524 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3525 const struct nfs_open_context *ctx,
3526 const struct nfs_lock_context *l_ctx,
3527 fmode_t fmode)
3529 nfs4_stateid current_stateid;
3531 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3532 return false;
3533 return nfs4_stateid_match(stateid, &current_stateid);
3536 static bool nfs4_error_stateid_expired(int err)
3538 switch (err) {
3539 case -NFS4ERR_DELEG_REVOKED:
3540 case -NFS4ERR_ADMIN_REVOKED:
3541 case -NFS4ERR_BAD_STATEID:
3542 case -NFS4ERR_STALE_STATEID:
3543 case -NFS4ERR_OLD_STATEID:
3544 case -NFS4ERR_OPENMODE:
3545 case -NFS4ERR_EXPIRED:
3546 return true;
3548 return false;
3551 void __nfs4_read_done_cb(struct nfs_read_data *data)
3553 nfs_invalidate_atime(data->header->inode);
3556 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3558 struct nfs_server *server = NFS_SERVER(data->header->inode);
3560 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3561 rpc_restart_call_prepare(task);
3562 return -EAGAIN;
3565 __nfs4_read_done_cb(data);
3566 if (task->tk_status > 0)
3567 renew_lease(server, data->timestamp);
3568 return 0;
3571 static bool nfs4_read_stateid_changed(struct rpc_task *task,
3572 struct nfs_readargs *args)
3575 if (!nfs4_error_stateid_expired(task->tk_status) ||
3576 nfs4_stateid_is_current(&args->stateid,
3577 args->context,
3578 args->lock_context,
3579 FMODE_READ))
3580 return false;
3581 rpc_restart_call_prepare(task);
3582 return true;
3585 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3588 dprintk("--> %s\n", __func__);
3590 if (!nfs4_sequence_done(task, &data->res.seq_res))
3591 return -EAGAIN;
3592 if (nfs4_read_stateid_changed(task, &data->args))
3593 return -EAGAIN;
3594 return data->read_done_cb ? data->read_done_cb(task, data) :
3595 nfs4_read_done_cb(task, data);
3598 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3600 data->timestamp = jiffies;
3601 data->read_done_cb = nfs4_read_done_cb;
3602 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3603 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3606 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3608 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3609 &data->args.seq_args,
3610 &data->res.seq_res,
3611 task))
3612 return;
3613 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3614 data->args.lock_context, FMODE_READ);
3617 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3619 struct inode *inode = data->header->inode;
3621 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3622 rpc_restart_call_prepare(task);
3623 return -EAGAIN;
3625 if (task->tk_status >= 0) {
3626 renew_lease(NFS_SERVER(inode), data->timestamp);
3627 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3629 return 0;
3632 static bool nfs4_write_stateid_changed(struct rpc_task *task,
3633 struct nfs_writeargs *args)
3636 if (!nfs4_error_stateid_expired(task->tk_status) ||
3637 nfs4_stateid_is_current(&args->stateid,
3638 args->context,
3639 args->lock_context,
3640 FMODE_WRITE))
3641 return false;
3642 rpc_restart_call_prepare(task);
3643 return true;
3646 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3648 if (!nfs4_sequence_done(task, &data->res.seq_res))
3649 return -EAGAIN;
3650 if (nfs4_write_stateid_changed(task, &data->args))
3651 return -EAGAIN;
3652 return data->write_done_cb ? data->write_done_cb(task, data) :
3653 nfs4_write_done_cb(task, data);
3656 static
3657 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3659 const struct nfs_pgio_header *hdr = data->header;
3661 /* Don't request attributes for pNFS or O_DIRECT writes */
3662 if (data->ds_clp != NULL || hdr->dreq != NULL)
3663 return false;
3664 /* Otherwise, request attributes if and only if we don't hold
3665 * a delegation
3667 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3670 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3672 struct nfs_server *server = NFS_SERVER(data->header->inode);
3674 if (!nfs4_write_need_cache_consistency_data(data)) {
3675 data->args.bitmask = NULL;
3676 data->res.fattr = NULL;
3677 } else
3678 data->args.bitmask = server->cache_consistency_bitmask;
3680 if (!data->write_done_cb)
3681 data->write_done_cb = nfs4_write_done_cb;
3682 data->res.server = server;
3683 data->timestamp = jiffies;
3685 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3686 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3689 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3691 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3692 &data->args.seq_args,
3693 &data->res.seq_res,
3694 task))
3695 return;
3696 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3697 data->args.lock_context, FMODE_WRITE);
3700 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3702 nfs4_setup_sequence(NFS_SERVER(data->inode),
3703 &data->args.seq_args,
3704 &data->res.seq_res,
3705 task);
3708 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3710 struct inode *inode = data->inode;
3712 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3713 rpc_restart_call_prepare(task);
3714 return -EAGAIN;
3716 return 0;
3719 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3721 if (!nfs4_sequence_done(task, &data->res.seq_res))
3722 return -EAGAIN;
3723 return data->commit_done_cb(task, data);
3726 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3728 struct nfs_server *server = NFS_SERVER(data->inode);
3730 if (data->commit_done_cb == NULL)
3731 data->commit_done_cb = nfs4_commit_done_cb;
3732 data->res.server = server;
3733 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3734 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3737 struct nfs4_renewdata {
3738 struct nfs_client *client;
3739 unsigned long timestamp;
3743 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3744 * standalone procedure for queueing an asynchronous RENEW.
3746 static void nfs4_renew_release(void *calldata)
3748 struct nfs4_renewdata *data = calldata;
3749 struct nfs_client *clp = data->client;
3751 if (atomic_read(&clp->cl_count) > 1)
3752 nfs4_schedule_state_renewal(clp);
3753 nfs_put_client(clp);
3754 kfree(data);
3757 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3759 struct nfs4_renewdata *data = calldata;
3760 struct nfs_client *clp = data->client;
3761 unsigned long timestamp = data->timestamp;
3763 if (task->tk_status < 0) {
3764 /* Unless we're shutting down, schedule state recovery! */
3765 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3766 return;
3767 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3768 nfs4_schedule_lease_recovery(clp);
3769 return;
3771 nfs4_schedule_path_down_recovery(clp);
3773 do_renew_lease(clp, timestamp);
3776 static const struct rpc_call_ops nfs4_renew_ops = {
3777 .rpc_call_done = nfs4_renew_done,
3778 .rpc_release = nfs4_renew_release,
3781 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3783 struct rpc_message msg = {
3784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3785 .rpc_argp = clp,
3786 .rpc_cred = cred,
3788 struct nfs4_renewdata *data;
3790 if (renew_flags == 0)
3791 return 0;
3792 if (!atomic_inc_not_zero(&clp->cl_count))
3793 return -EIO;
3794 data = kmalloc(sizeof(*data), GFP_NOFS);
3795 if (data == NULL)
3796 return -ENOMEM;
3797 data->client = clp;
3798 data->timestamp = jiffies;
3799 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3800 &nfs4_renew_ops, data);
3803 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3805 struct rpc_message msg = {
3806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3807 .rpc_argp = clp,
3808 .rpc_cred = cred,
3810 unsigned long now = jiffies;
3811 int status;
3813 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3814 if (status < 0)
3815 return status;
3816 do_renew_lease(clp, now);
3817 return 0;
3820 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3822 return (server->caps & NFS_CAP_ACLS)
3823 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3824 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3827 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3828 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3829 * the stack.
3831 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3833 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3834 struct page **pages, unsigned int *pgbase)
3836 struct page *newpage, **spages;
3837 int rc = 0;
3838 size_t len;
3839 spages = pages;
3841 do {
3842 len = min_t(size_t, PAGE_SIZE, buflen);
3843 newpage = alloc_page(GFP_KERNEL);
3845 if (newpage == NULL)
3846 goto unwind;
3847 memcpy(page_address(newpage), buf, len);
3848 buf += len;
3849 buflen -= len;
3850 *pages++ = newpage;
3851 rc++;
3852 } while (buflen != 0);
3854 return rc;
3856 unwind:
3857 for(; rc > 0; rc--)
3858 __free_page(spages[rc-1]);
3859 return -ENOMEM;
3862 struct nfs4_cached_acl {
3863 int cached;
3864 size_t len;
3865 char data[0];
3868 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3870 struct nfs_inode *nfsi = NFS_I(inode);
3872 spin_lock(&inode->i_lock);
3873 kfree(nfsi->nfs4_acl);
3874 nfsi->nfs4_acl = acl;
3875 spin_unlock(&inode->i_lock);
3878 static void nfs4_zap_acl_attr(struct inode *inode)
3880 nfs4_set_cached_acl(inode, NULL);
3883 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3885 struct nfs_inode *nfsi = NFS_I(inode);
3886 struct nfs4_cached_acl *acl;
3887 int ret = -ENOENT;
3889 spin_lock(&inode->i_lock);
3890 acl = nfsi->nfs4_acl;
3891 if (acl == NULL)
3892 goto out;
3893 if (buf == NULL) /* user is just asking for length */
3894 goto out_len;
3895 if (acl->cached == 0)
3896 goto out;
3897 ret = -ERANGE; /* see getxattr(2) man page */
3898 if (acl->len > buflen)
3899 goto out;
3900 memcpy(buf, acl->data, acl->len);
3901 out_len:
3902 ret = acl->len;
3903 out:
3904 spin_unlock(&inode->i_lock);
3905 return ret;
3908 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3910 struct nfs4_cached_acl *acl;
3911 size_t buflen = sizeof(*acl) + acl_len;
3913 if (buflen <= PAGE_SIZE) {
3914 acl = kmalloc(buflen, GFP_KERNEL);
3915 if (acl == NULL)
3916 goto out;
3917 acl->cached = 1;
3918 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3919 } else {
3920 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3921 if (acl == NULL)
3922 goto out;
3923 acl->cached = 0;
3925 acl->len = acl_len;
3926 out:
3927 nfs4_set_cached_acl(inode, acl);
3931 * The getxattr API returns the required buffer length when called with a
3932 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3933 * the required buf. On a NULL buf, we send a page of data to the server
3934 * guessing that the ACL request can be serviced by a page. If so, we cache
3935 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3936 * the cache. If not so, we throw away the page, and cache the required
3937 * length. The next getxattr call will then produce another round trip to
3938 * the server, this time with the input buf of the required size.
3940 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3942 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3943 struct nfs_getaclargs args = {
3944 .fh = NFS_FH(inode),
3945 .acl_pages = pages,
3946 .acl_len = buflen,
3948 struct nfs_getaclres res = {
3949 .acl_len = buflen,
3951 struct rpc_message msg = {
3952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3953 .rpc_argp = &args,
3954 .rpc_resp = &res,
3956 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3957 int ret = -ENOMEM, i;
3959 /* As long as we're doing a round trip to the server anyway,
3960 * let's be prepared for a page of acl data. */
3961 if (npages == 0)
3962 npages = 1;
3963 if (npages > ARRAY_SIZE(pages))
3964 return -ERANGE;
3966 for (i = 0; i < npages; i++) {
3967 pages[i] = alloc_page(GFP_KERNEL);
3968 if (!pages[i])
3969 goto out_free;
3972 /* for decoding across pages */
3973 res.acl_scratch = alloc_page(GFP_KERNEL);
3974 if (!res.acl_scratch)
3975 goto out_free;
3977 args.acl_len = npages * PAGE_SIZE;
3978 args.acl_pgbase = 0;
3980 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3981 __func__, buf, buflen, npages, args.acl_len);
3982 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3983 &msg, &args.seq_args, &res.seq_res, 0);
3984 if (ret)
3985 goto out_free;
3987 /* Handle the case where the passed-in buffer is too short */
3988 if (res.acl_flags & NFS4_ACL_TRUNC) {
3989 /* Did the user only issue a request for the acl length? */
3990 if (buf == NULL)
3991 goto out_ok;
3992 ret = -ERANGE;
3993 goto out_free;
3995 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3996 if (buf) {
3997 if (res.acl_len > buflen) {
3998 ret = -ERANGE;
3999 goto out_free;
4001 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4003 out_ok:
4004 ret = res.acl_len;
4005 out_free:
4006 for (i = 0; i < npages; i++)
4007 if (pages[i])
4008 __free_page(pages[i]);
4009 if (res.acl_scratch)
4010 __free_page(res.acl_scratch);
4011 return ret;
4014 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4016 struct nfs4_exception exception = { };
4017 ssize_t ret;
4018 do {
4019 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4020 if (ret >= 0)
4021 break;
4022 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4023 } while (exception.retry);
4024 return ret;
4027 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4029 struct nfs_server *server = NFS_SERVER(inode);
4030 int ret;
4032 if (!nfs4_server_supports_acls(server))
4033 return -EOPNOTSUPP;
4034 ret = nfs_revalidate_inode(server, inode);
4035 if (ret < 0)
4036 return ret;
4037 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4038 nfs_zap_acl_cache(inode);
4039 ret = nfs4_read_cached_acl(inode, buf, buflen);
4040 if (ret != -ENOENT)
4041 /* -ENOENT is returned if there is no ACL or if there is an ACL
4042 * but no cached acl data, just the acl length */
4043 return ret;
4044 return nfs4_get_acl_uncached(inode, buf, buflen);
4047 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4049 struct nfs_server *server = NFS_SERVER(inode);
4050 struct page *pages[NFS4ACL_MAXPAGES];
4051 struct nfs_setaclargs arg = {
4052 .fh = NFS_FH(inode),
4053 .acl_pages = pages,
4054 .acl_len = buflen,
4056 struct nfs_setaclres res;
4057 struct rpc_message msg = {
4058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4059 .rpc_argp = &arg,
4060 .rpc_resp = &res,
4062 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4063 int ret, i;
4065 if (!nfs4_server_supports_acls(server))
4066 return -EOPNOTSUPP;
4067 if (npages > ARRAY_SIZE(pages))
4068 return -ERANGE;
4069 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4070 if (i < 0)
4071 return i;
4072 nfs4_inode_return_delegation(inode);
4073 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4076 * Free each page after tx, so the only ref left is
4077 * held by the network stack
4079 for (; i > 0; i--)
4080 put_page(pages[i-1]);
4083 * Acl update can result in inode attribute update.
4084 * so mark the attribute cache invalid.
4086 spin_lock(&inode->i_lock);
4087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4088 spin_unlock(&inode->i_lock);
4089 nfs_access_zap_cache(inode);
4090 nfs_zap_acl_cache(inode);
4091 return ret;
4094 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4096 struct nfs4_exception exception = { };
4097 int err;
4098 do {
4099 err = nfs4_handle_exception(NFS_SERVER(inode),
4100 __nfs4_proc_set_acl(inode, buf, buflen),
4101 &exception);
4102 } while (exception.retry);
4103 return err;
4106 static int
4107 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4109 struct nfs_client *clp = server->nfs_client;
4111 if (task->tk_status >= 0)
4112 return 0;
4113 switch(task->tk_status) {
4114 case -NFS4ERR_DELEG_REVOKED:
4115 case -NFS4ERR_ADMIN_REVOKED:
4116 case -NFS4ERR_BAD_STATEID:
4117 if (state == NULL)
4118 break;
4119 nfs_remove_bad_delegation(state->inode);
4120 case -NFS4ERR_OPENMODE:
4121 if (state == NULL)
4122 break;
4123 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4124 goto stateid_invalid;
4125 goto wait_on_recovery;
4126 case -NFS4ERR_EXPIRED:
4127 if (state != NULL) {
4128 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4129 goto stateid_invalid;
4131 case -NFS4ERR_STALE_STATEID:
4132 case -NFS4ERR_STALE_CLIENTID:
4133 nfs4_schedule_lease_recovery(clp);
4134 goto wait_on_recovery;
4135 #if defined(CONFIG_NFS_V4_1)
4136 case -NFS4ERR_BADSESSION:
4137 case -NFS4ERR_BADSLOT:
4138 case -NFS4ERR_BAD_HIGH_SLOT:
4139 case -NFS4ERR_DEADSESSION:
4140 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4141 case -NFS4ERR_SEQ_FALSE_RETRY:
4142 case -NFS4ERR_SEQ_MISORDERED:
4143 dprintk("%s ERROR %d, Reset session\n", __func__,
4144 task->tk_status);
4145 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4146 task->tk_status = 0;
4147 return -EAGAIN;
4148 #endif /* CONFIG_NFS_V4_1 */
4149 case -NFS4ERR_DELAY:
4150 nfs_inc_server_stats(server, NFSIOS_DELAY);
4151 case -NFS4ERR_GRACE:
4152 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4153 task->tk_status = 0;
4154 return -EAGAIN;
4155 case -NFS4ERR_RETRY_UNCACHED_REP:
4156 case -NFS4ERR_OLD_STATEID:
4157 task->tk_status = 0;
4158 return -EAGAIN;
4160 task->tk_status = nfs4_map_errors(task->tk_status);
4161 return 0;
4162 stateid_invalid:
4163 task->tk_status = -EIO;
4164 return 0;
4165 wait_on_recovery:
4166 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4167 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4168 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4169 task->tk_status = 0;
4170 return -EAGAIN;
4173 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4174 nfs4_verifier *bootverf)
4176 __be32 verf[2];
4178 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4179 /* An impossible timestamp guarantees this value
4180 * will never match a generated boot time. */
4181 verf[0] = 0;
4182 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4183 } else {
4184 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4185 verf[0] = (__be32)nn->boot_time.tv_sec;
4186 verf[1] = (__be32)nn->boot_time.tv_nsec;
4188 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4191 static unsigned int
4192 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4193 char *buf, size_t len)
4195 unsigned int result;
4197 rcu_read_lock();
4198 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4199 clp->cl_ipaddr,
4200 rpc_peeraddr2str(clp->cl_rpcclient,
4201 RPC_DISPLAY_ADDR),
4202 rpc_peeraddr2str(clp->cl_rpcclient,
4203 RPC_DISPLAY_PROTO));
4204 rcu_read_unlock();
4205 return result;
4208 static unsigned int
4209 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4210 char *buf, size_t len)
4212 char *nodename = clp->cl_rpcclient->cl_nodename;
4214 if (nfs4_client_id_uniquifier[0] != '\0')
4215 nodename = nfs4_client_id_uniquifier;
4216 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4217 clp->rpc_ops->version, clp->cl_minorversion,
4218 nodename);
4222 * nfs4_proc_setclientid - Negotiate client ID
4223 * @clp: state data structure
4224 * @program: RPC program for NFSv4 callback service
4225 * @port: IP port number for NFS4 callback service
4226 * @cred: RPC credential to use for this call
4227 * @res: where to place the result
4229 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4231 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4232 unsigned short port, struct rpc_cred *cred,
4233 struct nfs4_setclientid_res *res)
4235 nfs4_verifier sc_verifier;
4236 struct nfs4_setclientid setclientid = {
4237 .sc_verifier = &sc_verifier,
4238 .sc_prog = program,
4239 .sc_cb_ident = clp->cl_cb_ident,
4241 struct rpc_message msg = {
4242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4243 .rpc_argp = &setclientid,
4244 .rpc_resp = res,
4245 .rpc_cred = cred,
4247 int status;
4249 /* nfs_client_id4 */
4250 nfs4_init_boot_verifier(clp, &sc_verifier);
4251 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4252 setclientid.sc_name_len =
4253 nfs4_init_uniform_client_string(clp,
4254 setclientid.sc_name,
4255 sizeof(setclientid.sc_name));
4256 else
4257 setclientid.sc_name_len =
4258 nfs4_init_nonuniform_client_string(clp,
4259 setclientid.sc_name,
4260 sizeof(setclientid.sc_name));
4261 /* cb_client4 */
4262 rcu_read_lock();
4263 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4264 sizeof(setclientid.sc_netid),
4265 rpc_peeraddr2str(clp->cl_rpcclient,
4266 RPC_DISPLAY_NETID));
4267 rcu_read_unlock();
4268 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4269 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4270 clp->cl_ipaddr, port >> 8, port & 255);
4272 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4273 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4274 setclientid.sc_name_len, setclientid.sc_name);
4275 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4276 dprintk("NFS reply setclientid: %d\n", status);
4277 return status;
4281 * nfs4_proc_setclientid_confirm - Confirm client ID
4282 * @clp: state data structure
4283 * @res: result of a previous SETCLIENTID
4284 * @cred: RPC credential to use for this call
4286 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4288 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4289 struct nfs4_setclientid_res *arg,
4290 struct rpc_cred *cred)
4292 struct nfs_fsinfo fsinfo;
4293 struct rpc_message msg = {
4294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4295 .rpc_argp = arg,
4296 .rpc_resp = &fsinfo,
4297 .rpc_cred = cred,
4299 unsigned long now;
4300 int status;
4302 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4304 clp->cl_clientid);
4305 now = jiffies;
4306 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4307 if (status == 0) {
4308 spin_lock(&clp->cl_lock);
4309 clp->cl_lease_time = fsinfo.lease_time * HZ;
4310 clp->cl_last_renewal = now;
4311 spin_unlock(&clp->cl_lock);
4313 dprintk("NFS reply setclientid_confirm: %d\n", status);
4314 return status;
4317 struct nfs4_delegreturndata {
4318 struct nfs4_delegreturnargs args;
4319 struct nfs4_delegreturnres res;
4320 struct nfs_fh fh;
4321 nfs4_stateid stateid;
4322 unsigned long timestamp;
4323 struct nfs_fattr fattr;
4324 int rpc_status;
4327 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4329 struct nfs4_delegreturndata *data = calldata;
4331 if (!nfs4_sequence_done(task, &data->res.seq_res))
4332 return;
4334 switch (task->tk_status) {
4335 case -NFS4ERR_STALE_STATEID:
4336 case -NFS4ERR_EXPIRED:
4337 case 0:
4338 renew_lease(data->res.server, data->timestamp);
4339 break;
4340 default:
4341 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4342 -EAGAIN) {
4343 rpc_restart_call_prepare(task);
4344 return;
4347 data->rpc_status = task->tk_status;
4350 static void nfs4_delegreturn_release(void *calldata)
4352 kfree(calldata);
4355 #if defined(CONFIG_NFS_V4_1)
4356 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4358 struct nfs4_delegreturndata *d_data;
4360 d_data = (struct nfs4_delegreturndata *)data;
4362 nfs4_setup_sequence(d_data->res.server,
4363 &d_data->args.seq_args,
4364 &d_data->res.seq_res,
4365 task);
4367 #endif /* CONFIG_NFS_V4_1 */
4369 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4370 #if defined(CONFIG_NFS_V4_1)
4371 .rpc_call_prepare = nfs4_delegreturn_prepare,
4372 #endif /* CONFIG_NFS_V4_1 */
4373 .rpc_call_done = nfs4_delegreturn_done,
4374 .rpc_release = nfs4_delegreturn_release,
4377 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4379 struct nfs4_delegreturndata *data;
4380 struct nfs_server *server = NFS_SERVER(inode);
4381 struct rpc_task *task;
4382 struct rpc_message msg = {
4383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4384 .rpc_cred = cred,
4386 struct rpc_task_setup task_setup_data = {
4387 .rpc_client = server->client,
4388 .rpc_message = &msg,
4389 .callback_ops = &nfs4_delegreturn_ops,
4390 .flags = RPC_TASK_ASYNC,
4392 int status = 0;
4394 data = kzalloc(sizeof(*data), GFP_NOFS);
4395 if (data == NULL)
4396 return -ENOMEM;
4397 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4398 data->args.fhandle = &data->fh;
4399 data->args.stateid = &data->stateid;
4400 data->args.bitmask = server->cache_consistency_bitmask;
4401 nfs_copy_fh(&data->fh, NFS_FH(inode));
4402 nfs4_stateid_copy(&data->stateid, stateid);
4403 data->res.fattr = &data->fattr;
4404 data->res.server = server;
4405 nfs_fattr_init(data->res.fattr);
4406 data->timestamp = jiffies;
4407 data->rpc_status = 0;
4409 task_setup_data.callback_data = data;
4410 msg.rpc_argp = &data->args;
4411 msg.rpc_resp = &data->res;
4412 task = rpc_run_task(&task_setup_data);
4413 if (IS_ERR(task))
4414 return PTR_ERR(task);
4415 if (!issync)
4416 goto out;
4417 status = nfs4_wait_for_completion_rpc_task(task);
4418 if (status != 0)
4419 goto out;
4420 status = data->rpc_status;
4421 if (status == 0)
4422 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4423 else
4424 nfs_refresh_inode(inode, &data->fattr);
4425 out:
4426 rpc_put_task(task);
4427 return status;
4430 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4432 struct nfs_server *server = NFS_SERVER(inode);
4433 struct nfs4_exception exception = { };
4434 int err;
4435 do {
4436 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4437 switch (err) {
4438 case -NFS4ERR_STALE_STATEID:
4439 case -NFS4ERR_EXPIRED:
4440 case 0:
4441 return 0;
4443 err = nfs4_handle_exception(server, err, &exception);
4444 } while (exception.retry);
4445 return err;
4448 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4449 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4452 * sleep, with exponential backoff, and retry the LOCK operation.
4454 static unsigned long
4455 nfs4_set_lock_task_retry(unsigned long timeout)
4457 freezable_schedule_timeout_killable(timeout);
4458 timeout <<= 1;
4459 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4460 return NFS4_LOCK_MAXTIMEOUT;
4461 return timeout;
4464 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4466 struct inode *inode = state->inode;
4467 struct nfs_server *server = NFS_SERVER(inode);
4468 struct nfs_client *clp = server->nfs_client;
4469 struct nfs_lockt_args arg = {
4470 .fh = NFS_FH(inode),
4471 .fl = request,
4473 struct nfs_lockt_res res = {
4474 .denied = request,
4476 struct rpc_message msg = {
4477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4478 .rpc_argp = &arg,
4479 .rpc_resp = &res,
4480 .rpc_cred = state->owner->so_cred,
4482 struct nfs4_lock_state *lsp;
4483 int status;
4485 arg.lock_owner.clientid = clp->cl_clientid;
4486 status = nfs4_set_lock_state(state, request);
4487 if (status != 0)
4488 goto out;
4489 lsp = request->fl_u.nfs4_fl.owner;
4490 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4491 arg.lock_owner.s_dev = server->s_dev;
4492 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4493 switch (status) {
4494 case 0:
4495 request->fl_type = F_UNLCK;
4496 break;
4497 case -NFS4ERR_DENIED:
4498 status = 0;
4500 request->fl_ops->fl_release_private(request);
4501 out:
4502 return status;
4505 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4507 struct nfs4_exception exception = { };
4508 int err;
4510 do {
4511 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4512 _nfs4_proc_getlk(state, cmd, request),
4513 &exception);
4514 } while (exception.retry);
4515 return err;
4518 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4520 int res = 0;
4521 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4522 case FL_POSIX:
4523 res = posix_lock_file_wait(file, fl);
4524 break;
4525 case FL_FLOCK:
4526 res = flock_lock_file_wait(file, fl);
4527 break;
4528 default:
4529 BUG();
4531 return res;
4534 struct nfs4_unlockdata {
4535 struct nfs_locku_args arg;
4536 struct nfs_locku_res res;
4537 struct nfs4_lock_state *lsp;
4538 struct nfs_open_context *ctx;
4539 struct file_lock fl;
4540 const struct nfs_server *server;
4541 unsigned long timestamp;
4544 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4545 struct nfs_open_context *ctx,
4546 struct nfs4_lock_state *lsp,
4547 struct nfs_seqid *seqid)
4549 struct nfs4_unlockdata *p;
4550 struct inode *inode = lsp->ls_state->inode;
4552 p = kzalloc(sizeof(*p), GFP_NOFS);
4553 if (p == NULL)
4554 return NULL;
4555 p->arg.fh = NFS_FH(inode);
4556 p->arg.fl = &p->fl;
4557 p->arg.seqid = seqid;
4558 p->res.seqid = seqid;
4559 p->arg.stateid = &lsp->ls_stateid;
4560 p->lsp = lsp;
4561 atomic_inc(&lsp->ls_count);
4562 /* Ensure we don't close file until we're done freeing locks! */
4563 p->ctx = get_nfs_open_context(ctx);
4564 memcpy(&p->fl, fl, sizeof(p->fl));
4565 p->server = NFS_SERVER(inode);
4566 return p;
4569 static void nfs4_locku_release_calldata(void *data)
4571 struct nfs4_unlockdata *calldata = data;
4572 nfs_free_seqid(calldata->arg.seqid);
4573 nfs4_put_lock_state(calldata->lsp);
4574 put_nfs_open_context(calldata->ctx);
4575 kfree(calldata);
4578 static void nfs4_locku_done(struct rpc_task *task, void *data)
4580 struct nfs4_unlockdata *calldata = data;
4582 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4583 return;
4584 switch (task->tk_status) {
4585 case 0:
4586 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4587 &calldata->res.stateid);
4588 renew_lease(calldata->server, calldata->timestamp);
4589 break;
4590 case -NFS4ERR_BAD_STATEID:
4591 case -NFS4ERR_OLD_STATEID:
4592 case -NFS4ERR_STALE_STATEID:
4593 case -NFS4ERR_EXPIRED:
4594 break;
4595 default:
4596 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4597 rpc_restart_call_prepare(task);
4599 nfs_release_seqid(calldata->arg.seqid);
4602 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4604 struct nfs4_unlockdata *calldata = data;
4606 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4607 goto out_wait;
4608 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4609 /* Note: exit _without_ running nfs4_locku_done */
4610 goto out_no_action;
4612 calldata->timestamp = jiffies;
4613 if (nfs4_setup_sequence(calldata->server,
4614 &calldata->arg.seq_args,
4615 &calldata->res.seq_res,
4616 task) != 0)
4617 nfs_release_seqid(calldata->arg.seqid);
4618 return;
4619 out_no_action:
4620 task->tk_action = NULL;
4621 out_wait:
4622 nfs4_sequence_done(task, &calldata->res.seq_res);
4625 static const struct rpc_call_ops nfs4_locku_ops = {
4626 .rpc_call_prepare = nfs4_locku_prepare,
4627 .rpc_call_done = nfs4_locku_done,
4628 .rpc_release = nfs4_locku_release_calldata,
4631 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4632 struct nfs_open_context *ctx,
4633 struct nfs4_lock_state *lsp,
4634 struct nfs_seqid *seqid)
4636 struct nfs4_unlockdata *data;
4637 struct rpc_message msg = {
4638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4639 .rpc_cred = ctx->cred,
4641 struct rpc_task_setup task_setup_data = {
4642 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4643 .rpc_message = &msg,
4644 .callback_ops = &nfs4_locku_ops,
4645 .workqueue = nfsiod_workqueue,
4646 .flags = RPC_TASK_ASYNC,
4649 /* Ensure this is an unlock - when canceling a lock, the
4650 * canceled lock is passed in, and it won't be an unlock.
4652 fl->fl_type = F_UNLCK;
4654 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4655 if (data == NULL) {
4656 nfs_free_seqid(seqid);
4657 return ERR_PTR(-ENOMEM);
4660 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4661 msg.rpc_argp = &data->arg;
4662 msg.rpc_resp = &data->res;
4663 task_setup_data.callback_data = data;
4664 return rpc_run_task(&task_setup_data);
4667 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4669 struct inode *inode = state->inode;
4670 struct nfs4_state_owner *sp = state->owner;
4671 struct nfs_inode *nfsi = NFS_I(inode);
4672 struct nfs_seqid *seqid;
4673 struct nfs4_lock_state *lsp;
4674 struct rpc_task *task;
4675 int status = 0;
4676 unsigned char fl_flags = request->fl_flags;
4678 status = nfs4_set_lock_state(state, request);
4679 /* Unlock _before_ we do the RPC call */
4680 request->fl_flags |= FL_EXISTS;
4681 /* Exclude nfs_delegation_claim_locks() */
4682 mutex_lock(&sp->so_delegreturn_mutex);
4683 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
4684 down_read(&nfsi->rwsem);
4685 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4686 up_read(&nfsi->rwsem);
4687 mutex_unlock(&sp->so_delegreturn_mutex);
4688 goto out;
4690 up_read(&nfsi->rwsem);
4691 mutex_unlock(&sp->so_delegreturn_mutex);
4692 if (status != 0)
4693 goto out;
4694 /* Is this a delegated lock? */
4695 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4696 goto out;
4697 lsp = request->fl_u.nfs4_fl.owner;
4698 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4699 status = -ENOMEM;
4700 if (seqid == NULL)
4701 goto out;
4702 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4703 status = PTR_ERR(task);
4704 if (IS_ERR(task))
4705 goto out;
4706 status = nfs4_wait_for_completion_rpc_task(task);
4707 rpc_put_task(task);
4708 out:
4709 request->fl_flags = fl_flags;
4710 return status;
4713 struct nfs4_lockdata {
4714 struct nfs_lock_args arg;
4715 struct nfs_lock_res res;
4716 struct nfs4_lock_state *lsp;
4717 struct nfs_open_context *ctx;
4718 struct file_lock fl;
4719 unsigned long timestamp;
4720 int rpc_status;
4721 int cancelled;
4722 struct nfs_server *server;
4725 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4726 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4727 gfp_t gfp_mask)
4729 struct nfs4_lockdata *p;
4730 struct inode *inode = lsp->ls_state->inode;
4731 struct nfs_server *server = NFS_SERVER(inode);
4733 p = kzalloc(sizeof(*p), gfp_mask);
4734 if (p == NULL)
4735 return NULL;
4737 p->arg.fh = NFS_FH(inode);
4738 p->arg.fl = &p->fl;
4739 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4740 if (p->arg.open_seqid == NULL)
4741 goto out_free;
4742 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4743 if (p->arg.lock_seqid == NULL)
4744 goto out_free_seqid;
4745 p->arg.lock_stateid = &lsp->ls_stateid;
4746 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4747 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4748 p->arg.lock_owner.s_dev = server->s_dev;
4749 p->res.lock_seqid = p->arg.lock_seqid;
4750 p->lsp = lsp;
4751 p->server = server;
4752 atomic_inc(&lsp->ls_count);
4753 p->ctx = get_nfs_open_context(ctx);
4754 memcpy(&p->fl, fl, sizeof(p->fl));
4755 return p;
4756 out_free_seqid:
4757 nfs_free_seqid(p->arg.open_seqid);
4758 out_free:
4759 kfree(p);
4760 return NULL;
4763 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4765 struct nfs4_lockdata *data = calldata;
4766 struct nfs4_state *state = data->lsp->ls_state;
4768 dprintk("%s: begin!\n", __func__);
4769 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4770 goto out_wait;
4771 /* Do we need to do an open_to_lock_owner? */
4772 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4773 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4774 goto out_release_lock_seqid;
4776 data->arg.open_stateid = &state->stateid;
4777 data->arg.new_lock_owner = 1;
4778 data->res.open_seqid = data->arg.open_seqid;
4779 } else
4780 data->arg.new_lock_owner = 0;
4781 if (!nfs4_valid_open_stateid(state)) {
4782 data->rpc_status = -EBADF;
4783 task->tk_action = NULL;
4784 goto out_release_open_seqid;
4786 data->timestamp = jiffies;
4787 if (nfs4_setup_sequence(data->server,
4788 &data->arg.seq_args,
4789 &data->res.seq_res,
4790 task) == 0)
4791 return;
4792 out_release_open_seqid:
4793 nfs_release_seqid(data->arg.open_seqid);
4794 out_release_lock_seqid:
4795 nfs_release_seqid(data->arg.lock_seqid);
4796 out_wait:
4797 nfs4_sequence_done(task, &data->res.seq_res);
4798 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4801 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4803 struct nfs4_lockdata *data = calldata;
4805 dprintk("%s: begin!\n", __func__);
4807 if (!nfs4_sequence_done(task, &data->res.seq_res))
4808 return;
4810 data->rpc_status = task->tk_status;
4811 if (data->arg.new_lock_owner != 0) {
4812 if (data->rpc_status == 0)
4813 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4814 else
4815 goto out;
4817 if (data->rpc_status == 0) {
4818 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4819 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4820 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4822 out:
4823 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4826 static void nfs4_lock_release(void *calldata)
4828 struct nfs4_lockdata *data = calldata;
4830 dprintk("%s: begin!\n", __func__);
4831 nfs_free_seqid(data->arg.open_seqid);
4832 if (data->cancelled != 0) {
4833 struct rpc_task *task;
4834 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4835 data->arg.lock_seqid);
4836 if (!IS_ERR(task))
4837 rpc_put_task_async(task);
4838 dprintk("%s: cancelling lock!\n", __func__);
4839 } else
4840 nfs_free_seqid(data->arg.lock_seqid);
4841 nfs4_put_lock_state(data->lsp);
4842 put_nfs_open_context(data->ctx);
4843 kfree(data);
4844 dprintk("%s: done!\n", __func__);
4847 static const struct rpc_call_ops nfs4_lock_ops = {
4848 .rpc_call_prepare = nfs4_lock_prepare,
4849 .rpc_call_done = nfs4_lock_done,
4850 .rpc_release = nfs4_lock_release,
4853 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4855 switch (error) {
4856 case -NFS4ERR_ADMIN_REVOKED:
4857 case -NFS4ERR_BAD_STATEID:
4858 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4859 if (new_lock_owner != 0 ||
4860 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4861 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4862 break;
4863 case -NFS4ERR_STALE_STATEID:
4864 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4865 case -NFS4ERR_EXPIRED:
4866 nfs4_schedule_lease_recovery(server->nfs_client);
4870 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4872 struct nfs4_lockdata *data;
4873 struct rpc_task *task;
4874 struct rpc_message msg = {
4875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4876 .rpc_cred = state->owner->so_cred,
4878 struct rpc_task_setup task_setup_data = {
4879 .rpc_client = NFS_CLIENT(state->inode),
4880 .rpc_message = &msg,
4881 .callback_ops = &nfs4_lock_ops,
4882 .workqueue = nfsiod_workqueue,
4883 .flags = RPC_TASK_ASYNC,
4885 int ret;
4887 dprintk("%s: begin!\n", __func__);
4888 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4889 fl->fl_u.nfs4_fl.owner,
4890 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4891 if (data == NULL)
4892 return -ENOMEM;
4893 if (IS_SETLKW(cmd))
4894 data->arg.block = 1;
4895 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4896 msg.rpc_argp = &data->arg;
4897 msg.rpc_resp = &data->res;
4898 task_setup_data.callback_data = data;
4899 if (recovery_type > NFS_LOCK_NEW) {
4900 if (recovery_type == NFS_LOCK_RECLAIM)
4901 data->arg.reclaim = NFS_LOCK_RECLAIM;
4902 nfs4_set_sequence_privileged(&data->arg.seq_args);
4904 task = rpc_run_task(&task_setup_data);
4905 if (IS_ERR(task))
4906 return PTR_ERR(task);
4907 ret = nfs4_wait_for_completion_rpc_task(task);
4908 if (ret == 0) {
4909 ret = data->rpc_status;
4910 if (ret)
4911 nfs4_handle_setlk_error(data->server, data->lsp,
4912 data->arg.new_lock_owner, ret);
4913 } else
4914 data->cancelled = 1;
4915 rpc_put_task(task);
4916 dprintk("%s: done, ret = %d!\n", __func__, ret);
4917 return ret;
4920 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4922 struct nfs_server *server = NFS_SERVER(state->inode);
4923 struct nfs4_exception exception = {
4924 .inode = state->inode,
4926 int err;
4928 do {
4929 /* Cache the lock if possible... */
4930 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4931 return 0;
4932 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4933 if (err != -NFS4ERR_DELAY)
4934 break;
4935 nfs4_handle_exception(server, err, &exception);
4936 } while (exception.retry);
4937 return err;
4940 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4942 struct nfs_server *server = NFS_SERVER(state->inode);
4943 struct nfs4_exception exception = {
4944 .inode = state->inode,
4946 int err;
4948 err = nfs4_set_lock_state(state, request);
4949 if (err != 0)
4950 return err;
4951 do {
4952 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4953 return 0;
4954 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4955 switch (err) {
4956 default:
4957 goto out;
4958 case -NFS4ERR_GRACE:
4959 case -NFS4ERR_DELAY:
4960 nfs4_handle_exception(server, err, &exception);
4961 err = 0;
4963 } while (exception.retry);
4964 out:
4965 return err;
4968 #if defined(CONFIG_NFS_V4_1)
4970 * nfs41_check_expired_locks - possibly free a lock stateid
4972 * @state: NFSv4 state for an inode
4974 * Returns NFS_OK if recovery for this stateid is now finished.
4975 * Otherwise a negative NFS4ERR value is returned.
4977 static int nfs41_check_expired_locks(struct nfs4_state *state)
4979 int status, ret = -NFS4ERR_BAD_STATEID;
4980 struct nfs4_lock_state *lsp;
4981 struct nfs_server *server = NFS_SERVER(state->inode);
4983 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4984 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4985 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4986 if (status != NFS_OK) {
4987 /* Free the stateid unless the server
4988 * informs us the stateid is unrecognized. */
4989 if (status != -NFS4ERR_BAD_STATEID)
4990 nfs41_free_stateid(server,
4991 &lsp->ls_stateid);
4992 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4993 ret = status;
4998 return ret;
5001 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5003 int status = NFS_OK;
5005 if (test_bit(LK_STATE_IN_USE, &state->flags))
5006 status = nfs41_check_expired_locks(state);
5007 if (status != NFS_OK)
5008 status = nfs4_lock_expired(state, request);
5009 return status;
5011 #endif
5013 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5015 struct nfs4_state_owner *sp = state->owner;
5016 struct nfs_inode *nfsi = NFS_I(state->inode);
5017 unsigned char fl_flags = request->fl_flags;
5018 unsigned int seq;
5019 int status = -ENOLCK;
5021 if ((fl_flags & FL_POSIX) &&
5022 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5023 goto out;
5024 /* Is this a delegated open? */
5025 status = nfs4_set_lock_state(state, request);
5026 if (status != 0)
5027 goto out;
5028 request->fl_flags |= FL_ACCESS;
5029 status = do_vfs_lock(request->fl_file, request);
5030 if (status < 0)
5031 goto out;
5032 down_read(&nfsi->rwsem);
5033 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5034 /* Yes: cache locks! */
5035 /* ...but avoid races with delegation recall... */
5036 request->fl_flags = fl_flags & ~FL_SLEEP;
5037 status = do_vfs_lock(request->fl_file, request);
5038 goto out_unlock;
5040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5041 up_read(&nfsi->rwsem);
5042 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5043 if (status != 0)
5044 goto out;
5045 down_read(&nfsi->rwsem);
5046 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5047 status = -NFS4ERR_DELAY;
5048 goto out_unlock;
5050 /* Note: we always want to sleep here! */
5051 request->fl_flags = fl_flags | FL_SLEEP;
5052 if (do_vfs_lock(request->fl_file, request) < 0)
5053 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5054 "manager!\n", __func__);
5055 out_unlock:
5056 up_read(&nfsi->rwsem);
5057 out:
5058 request->fl_flags = fl_flags;
5059 return status;
5062 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5064 struct nfs4_exception exception = {
5065 .state = state,
5066 .inode = state->inode,
5068 int err;
5070 do {
5071 err = _nfs4_proc_setlk(state, cmd, request);
5072 if (err == -NFS4ERR_DENIED)
5073 err = -EAGAIN;
5074 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5075 err, &exception);
5076 } while (exception.retry);
5077 return err;
5080 static int
5081 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5083 struct nfs_open_context *ctx;
5084 struct nfs4_state *state;
5085 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5086 int status;
5088 /* verify open state */
5089 ctx = nfs_file_open_context(filp);
5090 state = ctx->state;
5092 if (request->fl_start < 0 || request->fl_end < 0)
5093 return -EINVAL;
5095 if (IS_GETLK(cmd)) {
5096 if (state != NULL)
5097 return nfs4_proc_getlk(state, F_GETLK, request);
5098 return 0;
5101 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5102 return -EINVAL;
5104 if (request->fl_type == F_UNLCK) {
5105 if (state != NULL)
5106 return nfs4_proc_unlck(state, cmd, request);
5107 return 0;
5110 if (state == NULL)
5111 return -ENOLCK;
5113 * Don't rely on the VFS having checked the file open mode,
5114 * since it won't do this for flock() locks.
5116 switch (request->fl_type) {
5117 case F_RDLCK:
5118 if (!(filp->f_mode & FMODE_READ))
5119 return -EBADF;
5120 break;
5121 case F_WRLCK:
5122 if (!(filp->f_mode & FMODE_WRITE))
5123 return -EBADF;
5126 do {
5127 status = nfs4_proc_setlk(state, cmd, request);
5128 if ((status != -EAGAIN) || IS_SETLK(cmd))
5129 break;
5130 timeout = nfs4_set_lock_task_retry(timeout);
5131 status = -ERESTARTSYS;
5132 if (signalled())
5133 break;
5134 } while(status < 0);
5135 return status;
5138 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5140 struct nfs_server *server = NFS_SERVER(state->inode);
5141 struct nfs4_exception exception = { };
5142 int err;
5144 err = nfs4_set_lock_state(state, fl);
5145 if (err != 0)
5146 goto out;
5147 do {
5148 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5149 switch (err) {
5150 default:
5151 printk(KERN_ERR "NFS: %s: unhandled error "
5152 "%d.\n", __func__, err);
5153 case 0:
5154 case -ESTALE:
5155 goto out;
5156 case -NFS4ERR_STALE_CLIENTID:
5157 case -NFS4ERR_STALE_STATEID:
5158 set_bit(NFS_DELEGATED_STATE, &state->flags);
5159 case -NFS4ERR_EXPIRED:
5160 nfs4_schedule_lease_recovery(server->nfs_client);
5161 err = -EAGAIN;
5162 goto out;
5163 case -NFS4ERR_BADSESSION:
5164 case -NFS4ERR_BADSLOT:
5165 case -NFS4ERR_BAD_HIGH_SLOT:
5166 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5167 case -NFS4ERR_DEADSESSION:
5168 set_bit(NFS_DELEGATED_STATE, &state->flags);
5169 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5170 err = -EAGAIN;
5171 goto out;
5172 case -NFS4ERR_DELEG_REVOKED:
5173 case -NFS4ERR_ADMIN_REVOKED:
5174 case -NFS4ERR_BAD_STATEID:
5175 case -NFS4ERR_OPENMODE:
5176 nfs4_schedule_stateid_recovery(server, state);
5177 err = 0;
5178 goto out;
5179 case -NFS4ERR_DELAY:
5180 case -NFS4ERR_GRACE:
5181 set_bit(NFS_DELEGATED_STATE, &state->flags);
5182 ssleep(1);
5183 err = -EAGAIN;
5184 goto out;
5185 case -ENOMEM:
5186 case -NFS4ERR_DENIED:
5187 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5188 err = 0;
5189 goto out;
5191 set_bit(NFS_DELEGATED_STATE, &state->flags);
5192 err = nfs4_handle_exception(server, err, &exception);
5193 } while (exception.retry);
5194 out:
5195 return err;
5198 struct nfs_release_lockowner_data {
5199 struct nfs4_lock_state *lsp;
5200 struct nfs_server *server;
5201 struct nfs_release_lockowner_args args;
5204 static void nfs4_release_lockowner_release(void *calldata)
5206 struct nfs_release_lockowner_data *data = calldata;
5207 nfs4_free_lock_state(data->server, data->lsp);
5208 kfree(calldata);
5211 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5212 .rpc_release = nfs4_release_lockowner_release,
5215 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5217 struct nfs_server *server = lsp->ls_state->owner->so_server;
5218 struct nfs_release_lockowner_data *data;
5219 struct rpc_message msg = {
5220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5223 if (server->nfs_client->cl_mvops->minor_version != 0)
5224 return -EINVAL;
5225 data = kmalloc(sizeof(*data), GFP_NOFS);
5226 if (!data)
5227 return -ENOMEM;
5228 data->lsp = lsp;
5229 data->server = server;
5230 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5231 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5232 data->args.lock_owner.s_dev = server->s_dev;
5233 msg.rpc_argp = &data->args;
5234 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5235 return 0;
5238 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5240 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5241 const void *buf, size_t buflen,
5242 int flags, int type)
5244 if (strcmp(key, "") != 0)
5245 return -EINVAL;
5247 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5250 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5251 void *buf, size_t buflen, int type)
5253 if (strcmp(key, "") != 0)
5254 return -EINVAL;
5256 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5259 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5260 size_t list_len, const char *name,
5261 size_t name_len, int type)
5263 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5265 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5266 return 0;
5268 if (list && len <= list_len)
5269 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5270 return len;
5274 * nfs_fhget will use either the mounted_on_fileid or the fileid
5276 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5278 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5279 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5280 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5281 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5282 return;
5284 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5285 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5286 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5287 fattr->nlink = 2;
5290 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5291 const struct qstr *name,
5292 struct nfs4_fs_locations *fs_locations,
5293 struct page *page)
5295 struct nfs_server *server = NFS_SERVER(dir);
5296 u32 bitmask[2] = {
5297 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5299 struct nfs4_fs_locations_arg args = {
5300 .dir_fh = NFS_FH(dir),
5301 .name = name,
5302 .page = page,
5303 .bitmask = bitmask,
5305 struct nfs4_fs_locations_res res = {
5306 .fs_locations = fs_locations,
5308 struct rpc_message msg = {
5309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5310 .rpc_argp = &args,
5311 .rpc_resp = &res,
5313 int status;
5315 dprintk("%s: start\n", __func__);
5317 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5318 * is not supported */
5319 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5320 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5321 else
5322 bitmask[0] |= FATTR4_WORD0_FILEID;
5324 nfs_fattr_init(&fs_locations->fattr);
5325 fs_locations->server = server;
5326 fs_locations->nlocations = 0;
5327 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5328 dprintk("%s: returned status = %d\n", __func__, status);
5329 return status;
5332 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5333 const struct qstr *name,
5334 struct nfs4_fs_locations *fs_locations,
5335 struct page *page)
5337 struct nfs4_exception exception = { };
5338 int err;
5339 do {
5340 err = nfs4_handle_exception(NFS_SERVER(dir),
5341 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5342 &exception);
5343 } while (exception.retry);
5344 return err;
5347 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5349 int status;
5350 struct nfs4_secinfo_arg args = {
5351 .dir_fh = NFS_FH(dir),
5352 .name = name,
5354 struct nfs4_secinfo_res res = {
5355 .flavors = flavors,
5357 struct rpc_message msg = {
5358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5359 .rpc_argp = &args,
5360 .rpc_resp = &res,
5363 dprintk("NFS call secinfo %s\n", name->name);
5364 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5365 dprintk("NFS reply secinfo: %d\n", status);
5366 return status;
5369 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5370 struct nfs4_secinfo_flavors *flavors)
5372 struct nfs4_exception exception = { };
5373 int err;
5374 do {
5375 err = nfs4_handle_exception(NFS_SERVER(dir),
5376 _nfs4_proc_secinfo(dir, name, flavors),
5377 &exception);
5378 } while (exception.retry);
5379 return err;
5382 #ifdef CONFIG_NFS_V4_1
5384 * Check the exchange flags returned by the server for invalid flags, having
5385 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5386 * DS flags set.
5388 static int nfs4_check_cl_exchange_flags(u32 flags)
5390 if (flags & ~EXCHGID4_FLAG_MASK_R)
5391 goto out_inval;
5392 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5393 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5394 goto out_inval;
5395 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5396 goto out_inval;
5397 return NFS_OK;
5398 out_inval:
5399 return -NFS4ERR_INVAL;
5402 static bool
5403 nfs41_same_server_scope(struct nfs41_server_scope *a,
5404 struct nfs41_server_scope *b)
5406 if (a->server_scope_sz == b->server_scope_sz &&
5407 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5408 return true;
5410 return false;
5414 * nfs4_proc_bind_conn_to_session()
5416 * The 4.1 client currently uses the same TCP connection for the
5417 * fore and backchannel.
5419 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5421 int status;
5422 struct nfs41_bind_conn_to_session_res res;
5423 struct rpc_message msg = {
5424 .rpc_proc =
5425 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5426 .rpc_argp = clp,
5427 .rpc_resp = &res,
5428 .rpc_cred = cred,
5431 dprintk("--> %s\n", __func__);
5433 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5434 if (unlikely(res.session == NULL)) {
5435 status = -ENOMEM;
5436 goto out;
5439 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5440 if (status == 0) {
5441 if (memcmp(res.session->sess_id.data,
5442 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5443 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5444 status = -EIO;
5445 goto out_session;
5447 if (res.dir != NFS4_CDFS4_BOTH) {
5448 dprintk("NFS: %s: Unexpected direction from server\n",
5449 __func__);
5450 status = -EIO;
5451 goto out_session;
5453 if (res.use_conn_in_rdma_mode) {
5454 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5455 __func__);
5456 status = -EIO;
5457 goto out_session;
5460 out_session:
5461 kfree(res.session);
5462 out:
5463 dprintk("<-- %s status= %d\n", __func__, status);
5464 return status;
5468 * nfs4_proc_exchange_id()
5470 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5472 * Since the clientid has expired, all compounds using sessions
5473 * associated with the stale clientid will be returning
5474 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5475 * be in some phase of session reset.
5477 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5479 nfs4_verifier verifier;
5480 struct nfs41_exchange_id_args args = {
5481 .verifier = &verifier,
5482 .client = clp,
5483 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5485 struct nfs41_exchange_id_res res = {
5488 int status;
5489 struct rpc_message msg = {
5490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5491 .rpc_argp = &args,
5492 .rpc_resp = &res,
5493 .rpc_cred = cred,
5496 nfs4_init_boot_verifier(clp, &verifier);
5497 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5498 sizeof(args.id));
5499 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5500 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5501 args.id_len, args.id);
5503 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5504 GFP_NOFS);
5505 if (unlikely(res.server_owner == NULL)) {
5506 status = -ENOMEM;
5507 goto out;
5510 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5511 GFP_NOFS);
5512 if (unlikely(res.server_scope == NULL)) {
5513 status = -ENOMEM;
5514 goto out_server_owner;
5517 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5518 if (unlikely(res.impl_id == NULL)) {
5519 status = -ENOMEM;
5520 goto out_server_scope;
5523 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5524 if (status == 0)
5525 status = nfs4_check_cl_exchange_flags(res.flags);
5527 if (status == 0) {
5528 clp->cl_clientid = res.clientid;
5529 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5530 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5531 clp->cl_seqid = res.seqid;
5533 kfree(clp->cl_serverowner);
5534 clp->cl_serverowner = res.server_owner;
5535 res.server_owner = NULL;
5537 /* use the most recent implementation id */
5538 kfree(clp->cl_implid);
5539 clp->cl_implid = res.impl_id;
5541 if (clp->cl_serverscope != NULL &&
5542 !nfs41_same_server_scope(clp->cl_serverscope,
5543 res.server_scope)) {
5544 dprintk("%s: server_scope mismatch detected\n",
5545 __func__);
5546 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5547 kfree(clp->cl_serverscope);
5548 clp->cl_serverscope = NULL;
5551 if (clp->cl_serverscope == NULL) {
5552 clp->cl_serverscope = res.server_scope;
5553 goto out;
5555 } else
5556 kfree(res.impl_id);
5558 out_server_owner:
5559 kfree(res.server_owner);
5560 out_server_scope:
5561 kfree(res.server_scope);
5562 out:
5563 if (clp->cl_implid != NULL)
5564 dprintk("NFS reply exchange_id: Server Implementation ID: "
5565 "domain: %s, name: %s, date: %llu,%u\n",
5566 clp->cl_implid->domain, clp->cl_implid->name,
5567 clp->cl_implid->date.seconds,
5568 clp->cl_implid->date.nseconds);
5569 dprintk("NFS reply exchange_id: %d\n", status);
5570 return status;
5573 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5574 struct rpc_cred *cred)
5576 struct rpc_message msg = {
5577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5578 .rpc_argp = clp,
5579 .rpc_cred = cred,
5581 int status;
5583 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5584 if (status)
5585 dprintk("NFS: Got error %d from the server %s on "
5586 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5587 return status;
5590 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5591 struct rpc_cred *cred)
5593 unsigned int loop;
5594 int ret;
5596 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5597 ret = _nfs4_proc_destroy_clientid(clp, cred);
5598 switch (ret) {
5599 case -NFS4ERR_DELAY:
5600 case -NFS4ERR_CLIENTID_BUSY:
5601 ssleep(1);
5602 break;
5603 default:
5604 return ret;
5607 return 0;
5610 int nfs4_destroy_clientid(struct nfs_client *clp)
5612 struct rpc_cred *cred;
5613 int ret = 0;
5615 if (clp->cl_mvops->minor_version < 1)
5616 goto out;
5617 if (clp->cl_exchange_flags == 0)
5618 goto out;
5619 if (clp->cl_preserve_clid)
5620 goto out;
5621 cred = nfs4_get_exchange_id_cred(clp);
5622 ret = nfs4_proc_destroy_clientid(clp, cred);
5623 if (cred)
5624 put_rpccred(cred);
5625 switch (ret) {
5626 case 0:
5627 case -NFS4ERR_STALE_CLIENTID:
5628 clp->cl_exchange_flags = 0;
5630 out:
5631 return ret;
5634 struct nfs4_get_lease_time_data {
5635 struct nfs4_get_lease_time_args *args;
5636 struct nfs4_get_lease_time_res *res;
5637 struct nfs_client *clp;
5640 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5641 void *calldata)
5643 struct nfs4_get_lease_time_data *data =
5644 (struct nfs4_get_lease_time_data *)calldata;
5646 dprintk("--> %s\n", __func__);
5647 /* just setup sequence, do not trigger session recovery
5648 since we're invoked within one */
5649 nfs41_setup_sequence(data->clp->cl_session,
5650 &data->args->la_seq_args,
5651 &data->res->lr_seq_res,
5652 task);
5653 dprintk("<-- %s\n", __func__);
5657 * Called from nfs4_state_manager thread for session setup, so don't recover
5658 * from sequence operation or clientid errors.
5660 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5662 struct nfs4_get_lease_time_data *data =
5663 (struct nfs4_get_lease_time_data *)calldata;
5665 dprintk("--> %s\n", __func__);
5666 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5667 return;
5668 switch (task->tk_status) {
5669 case -NFS4ERR_DELAY:
5670 case -NFS4ERR_GRACE:
5671 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5672 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5673 task->tk_status = 0;
5674 /* fall through */
5675 case -NFS4ERR_RETRY_UNCACHED_REP:
5676 rpc_restart_call_prepare(task);
5677 return;
5679 dprintk("<-- %s\n", __func__);
5682 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5683 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5684 .rpc_call_done = nfs4_get_lease_time_done,
5687 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5689 struct rpc_task *task;
5690 struct nfs4_get_lease_time_args args;
5691 struct nfs4_get_lease_time_res res = {
5692 .lr_fsinfo = fsinfo,
5694 struct nfs4_get_lease_time_data data = {
5695 .args = &args,
5696 .res = &res,
5697 .clp = clp,
5699 struct rpc_message msg = {
5700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5701 .rpc_argp = &args,
5702 .rpc_resp = &res,
5704 struct rpc_task_setup task_setup = {
5705 .rpc_client = clp->cl_rpcclient,
5706 .rpc_message = &msg,
5707 .callback_ops = &nfs4_get_lease_time_ops,
5708 .callback_data = &data,
5709 .flags = RPC_TASK_TIMEOUT,
5711 int status;
5713 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5714 nfs4_set_sequence_privileged(&args.la_seq_args);
5715 dprintk("--> %s\n", __func__);
5716 task = rpc_run_task(&task_setup);
5718 if (IS_ERR(task))
5719 status = PTR_ERR(task);
5720 else {
5721 status = task->tk_status;
5722 rpc_put_task(task);
5724 dprintk("<-- %s return %d\n", __func__, status);
5726 return status;
5730 * Initialize the values to be used by the client in CREATE_SESSION
5731 * If nfs4_init_session set the fore channel request and response sizes,
5732 * use them.
5734 * Set the back channel max_resp_sz_cached to zero to force the client to
5735 * always set csa_cachethis to FALSE because the current implementation
5736 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5738 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5740 struct nfs4_session *session = args->client->cl_session;
5741 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5742 mxresp_sz = session->fc_target_max_resp_sz;
5744 if (mxrqst_sz == 0)
5745 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5746 if (mxresp_sz == 0)
5747 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5748 /* Fore channel attributes */
5749 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5750 args->fc_attrs.max_resp_sz = mxresp_sz;
5751 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5752 args->fc_attrs.max_reqs = max_session_slots;
5754 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5755 "max_ops=%u max_reqs=%u\n",
5756 __func__,
5757 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5758 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5760 /* Back channel attributes */
5761 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5762 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5763 args->bc_attrs.max_resp_sz_cached = 0;
5764 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5765 args->bc_attrs.max_reqs = 1;
5767 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5768 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5769 __func__,
5770 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5771 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5772 args->bc_attrs.max_reqs);
5775 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5777 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5778 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5780 if (rcvd->max_resp_sz > sent->max_resp_sz)
5781 return -EINVAL;
5783 * Our requested max_ops is the minimum we need; we're not
5784 * prepared to break up compounds into smaller pieces than that.
5785 * So, no point even trying to continue if the server won't
5786 * cooperate:
5788 if (rcvd->max_ops < sent->max_ops)
5789 return -EINVAL;
5790 if (rcvd->max_reqs == 0)
5791 return -EINVAL;
5792 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5793 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5794 return 0;
5797 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5799 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5800 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5802 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5803 return -EINVAL;
5804 if (rcvd->max_resp_sz < sent->max_resp_sz)
5805 return -EINVAL;
5806 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5807 return -EINVAL;
5808 /* These would render the backchannel useless: */
5809 if (rcvd->max_ops != sent->max_ops)
5810 return -EINVAL;
5811 if (rcvd->max_reqs != sent->max_reqs)
5812 return -EINVAL;
5813 return 0;
5816 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5817 struct nfs4_session *session)
5819 int ret;
5821 ret = nfs4_verify_fore_channel_attrs(args, session);
5822 if (ret)
5823 return ret;
5824 return nfs4_verify_back_channel_attrs(args, session);
5827 static int _nfs4_proc_create_session(struct nfs_client *clp,
5828 struct rpc_cred *cred)
5830 struct nfs4_session *session = clp->cl_session;
5831 struct nfs41_create_session_args args = {
5832 .client = clp,
5833 .cb_program = NFS4_CALLBACK,
5835 struct nfs41_create_session_res res = {
5836 .client = clp,
5838 struct rpc_message msg = {
5839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5840 .rpc_argp = &args,
5841 .rpc_resp = &res,
5842 .rpc_cred = cred,
5844 int status;
5846 nfs4_init_channel_attrs(&args);
5847 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5849 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5851 if (!status) {
5852 /* Verify the session's negotiated channel_attrs values */
5853 status = nfs4_verify_channel_attrs(&args, session);
5854 /* Increment the clientid slot sequence id */
5855 clp->cl_seqid++;
5858 return status;
5862 * Issues a CREATE_SESSION operation to the server.
5863 * It is the responsibility of the caller to verify the session is
5864 * expired before calling this routine.
5866 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5868 int status;
5869 unsigned *ptr;
5870 struct nfs4_session *session = clp->cl_session;
5872 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5874 status = _nfs4_proc_create_session(clp, cred);
5875 if (status)
5876 goto out;
5878 /* Init or reset the session slot tables */
5879 status = nfs4_setup_session_slot_tables(session);
5880 dprintk("slot table setup returned %d\n", status);
5881 if (status)
5882 goto out;
5884 ptr = (unsigned *)&session->sess_id.data[0];
5885 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5886 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5887 out:
5888 dprintk("<-- %s\n", __func__);
5889 return status;
5893 * Issue the over-the-wire RPC DESTROY_SESSION.
5894 * The caller must serialize access to this routine.
5896 int nfs4_proc_destroy_session(struct nfs4_session *session,
5897 struct rpc_cred *cred)
5899 struct rpc_message msg = {
5900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5901 .rpc_argp = session,
5902 .rpc_cred = cred,
5904 int status = 0;
5906 dprintk("--> nfs4_proc_destroy_session\n");
5908 /* session is still being setup */
5909 if (session->clp->cl_cons_state != NFS_CS_READY)
5910 return status;
5912 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5914 if (status)
5915 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5916 "Session has been destroyed regardless...\n", status);
5918 dprintk("<-- nfs4_proc_destroy_session\n");
5919 return status;
5923 * Renew the cl_session lease.
5925 struct nfs4_sequence_data {
5926 struct nfs_client *clp;
5927 struct nfs4_sequence_args args;
5928 struct nfs4_sequence_res res;
5931 static void nfs41_sequence_release(void *data)
5933 struct nfs4_sequence_data *calldata = data;
5934 struct nfs_client *clp = calldata->clp;
5936 if (atomic_read(&clp->cl_count) > 1)
5937 nfs4_schedule_state_renewal(clp);
5938 nfs_put_client(clp);
5939 kfree(calldata);
5942 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5944 switch(task->tk_status) {
5945 case -NFS4ERR_DELAY:
5946 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5947 return -EAGAIN;
5948 default:
5949 nfs4_schedule_lease_recovery(clp);
5951 return 0;
5954 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5956 struct nfs4_sequence_data *calldata = data;
5957 struct nfs_client *clp = calldata->clp;
5959 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5960 return;
5962 if (task->tk_status < 0) {
5963 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5964 if (atomic_read(&clp->cl_count) == 1)
5965 goto out;
5967 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5968 rpc_restart_call_prepare(task);
5969 return;
5972 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5973 out:
5974 dprintk("<-- %s\n", __func__);
5977 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5979 struct nfs4_sequence_data *calldata = data;
5980 struct nfs_client *clp = calldata->clp;
5981 struct nfs4_sequence_args *args;
5982 struct nfs4_sequence_res *res;
5984 args = task->tk_msg.rpc_argp;
5985 res = task->tk_msg.rpc_resp;
5987 nfs41_setup_sequence(clp->cl_session, args, res, task);
5990 static const struct rpc_call_ops nfs41_sequence_ops = {
5991 .rpc_call_done = nfs41_sequence_call_done,
5992 .rpc_call_prepare = nfs41_sequence_prepare,
5993 .rpc_release = nfs41_sequence_release,
5996 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5997 struct rpc_cred *cred,
5998 bool is_privileged)
6000 struct nfs4_sequence_data *calldata;
6001 struct rpc_message msg = {
6002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6003 .rpc_cred = cred,
6005 struct rpc_task_setup task_setup_data = {
6006 .rpc_client = clp->cl_rpcclient,
6007 .rpc_message = &msg,
6008 .callback_ops = &nfs41_sequence_ops,
6009 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6012 if (!atomic_inc_not_zero(&clp->cl_count))
6013 return ERR_PTR(-EIO);
6014 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6015 if (calldata == NULL) {
6016 nfs_put_client(clp);
6017 return ERR_PTR(-ENOMEM);
6019 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6020 if (is_privileged)
6021 nfs4_set_sequence_privileged(&calldata->args);
6022 msg.rpc_argp = &calldata->args;
6023 msg.rpc_resp = &calldata->res;
6024 calldata->clp = clp;
6025 task_setup_data.callback_data = calldata;
6027 return rpc_run_task(&task_setup_data);
6030 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6032 struct rpc_task *task;
6033 int ret = 0;
6035 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6036 return 0;
6037 task = _nfs41_proc_sequence(clp, cred, false);
6038 if (IS_ERR(task))
6039 ret = PTR_ERR(task);
6040 else
6041 rpc_put_task_async(task);
6042 dprintk("<-- %s status=%d\n", __func__, ret);
6043 return ret;
6046 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6048 struct rpc_task *task;
6049 int ret;
6051 task = _nfs41_proc_sequence(clp, cred, true);
6052 if (IS_ERR(task)) {
6053 ret = PTR_ERR(task);
6054 goto out;
6056 ret = rpc_wait_for_completion_task(task);
6057 if (!ret) {
6058 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6060 if (task->tk_status == 0)
6061 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6062 ret = task->tk_status;
6064 rpc_put_task(task);
6065 out:
6066 dprintk("<-- %s status=%d\n", __func__, ret);
6067 return ret;
6070 struct nfs4_reclaim_complete_data {
6071 struct nfs_client *clp;
6072 struct nfs41_reclaim_complete_args arg;
6073 struct nfs41_reclaim_complete_res res;
6076 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6078 struct nfs4_reclaim_complete_data *calldata = data;
6080 nfs41_setup_sequence(calldata->clp->cl_session,
6081 &calldata->arg.seq_args,
6082 &calldata->res.seq_res,
6083 task);
6086 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6088 switch(task->tk_status) {
6089 case 0:
6090 case -NFS4ERR_COMPLETE_ALREADY:
6091 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6092 break;
6093 case -NFS4ERR_DELAY:
6094 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6095 /* fall through */
6096 case -NFS4ERR_RETRY_UNCACHED_REP:
6097 return -EAGAIN;
6098 default:
6099 nfs4_schedule_lease_recovery(clp);
6101 return 0;
6104 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6106 struct nfs4_reclaim_complete_data *calldata = data;
6107 struct nfs_client *clp = calldata->clp;
6108 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6110 dprintk("--> %s\n", __func__);
6111 if (!nfs41_sequence_done(task, res))
6112 return;
6114 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6115 rpc_restart_call_prepare(task);
6116 return;
6118 dprintk("<-- %s\n", __func__);
6121 static void nfs4_free_reclaim_complete_data(void *data)
6123 struct nfs4_reclaim_complete_data *calldata = data;
6125 kfree(calldata);
6128 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6129 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6130 .rpc_call_done = nfs4_reclaim_complete_done,
6131 .rpc_release = nfs4_free_reclaim_complete_data,
6135 * Issue a global reclaim complete.
6137 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6139 struct nfs4_reclaim_complete_data *calldata;
6140 struct rpc_task *task;
6141 struct rpc_message msg = {
6142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = clp->cl_rpcclient,
6146 .rpc_message = &msg,
6147 .callback_ops = &nfs4_reclaim_complete_call_ops,
6148 .flags = RPC_TASK_ASYNC,
6150 int status = -ENOMEM;
6152 dprintk("--> %s\n", __func__);
6153 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6154 if (calldata == NULL)
6155 goto out;
6156 calldata->clp = clp;
6157 calldata->arg.one_fs = 0;
6159 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6160 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
6161 msg.rpc_argp = &calldata->arg;
6162 msg.rpc_resp = &calldata->res;
6163 task_setup_data.callback_data = calldata;
6164 task = rpc_run_task(&task_setup_data);
6165 if (IS_ERR(task)) {
6166 status = PTR_ERR(task);
6167 goto out;
6169 status = nfs4_wait_for_completion_rpc_task(task);
6170 if (status == 0)
6171 status = task->tk_status;
6172 rpc_put_task(task);
6173 return 0;
6174 out:
6175 dprintk("<-- %s status=%d\n", __func__, status);
6176 return status;
6179 static void
6180 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6182 struct nfs4_layoutget *lgp = calldata;
6183 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6184 struct nfs4_session *session = nfs4_get_session(server);
6186 dprintk("--> %s\n", __func__);
6187 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6188 * right now covering the LAYOUTGET we are about to send.
6189 * However, that is not so catastrophic, and there seems
6190 * to be no way to prevent it completely.
6192 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
6193 &lgp->res.seq_res, task))
6194 return;
6195 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6196 NFS_I(lgp->args.inode)->layout,
6197 lgp->args.ctx->state)) {
6198 rpc_exit(task, NFS4_OK);
6202 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6204 struct nfs4_layoutget *lgp = calldata;
6205 struct inode *inode = lgp->args.inode;
6206 struct nfs_server *server = NFS_SERVER(inode);
6207 struct pnfs_layout_hdr *lo;
6208 struct nfs4_state *state = NULL;
6209 unsigned long timeo, giveup;
6211 dprintk("--> %s\n", __func__);
6213 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6214 goto out;
6216 switch (task->tk_status) {
6217 case 0:
6218 goto out;
6219 case -NFS4ERR_LAYOUTTRYLATER:
6220 case -NFS4ERR_RECALLCONFLICT:
6221 timeo = rpc_get_timeout(task->tk_client);
6222 giveup = lgp->args.timestamp + timeo;
6223 if (time_after(giveup, jiffies))
6224 task->tk_status = -NFS4ERR_DELAY;
6225 break;
6226 case -NFS4ERR_EXPIRED:
6227 case -NFS4ERR_BAD_STATEID:
6228 spin_lock(&inode->i_lock);
6229 lo = NFS_I(inode)->layout;
6230 if (!lo || list_empty(&lo->plh_segs)) {
6231 spin_unlock(&inode->i_lock);
6232 /* If the open stateid was bad, then recover it. */
6233 state = lgp->args.ctx->state;
6234 } else {
6235 LIST_HEAD(head);
6237 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6238 spin_unlock(&inode->i_lock);
6239 /* Mark the bad layout state as invalid, then
6240 * retry using the open stateid. */
6241 pnfs_free_lseg_list(&head);
6244 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6245 rpc_restart_call_prepare(task);
6246 out:
6247 dprintk("<-- %s\n", __func__);
6250 static size_t max_response_pages(struct nfs_server *server)
6252 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6253 return nfs_page_array_len(0, max_resp_sz);
6256 static void nfs4_free_pages(struct page **pages, size_t size)
6258 int i;
6260 if (!pages)
6261 return;
6263 for (i = 0; i < size; i++) {
6264 if (!pages[i])
6265 break;
6266 __free_page(pages[i]);
6268 kfree(pages);
6271 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6273 struct page **pages;
6274 int i;
6276 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6277 if (!pages) {
6278 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6279 return NULL;
6282 for (i = 0; i < size; i++) {
6283 pages[i] = alloc_page(gfp_flags);
6284 if (!pages[i]) {
6285 dprintk("%s: failed to allocate page\n", __func__);
6286 nfs4_free_pages(pages, size);
6287 return NULL;
6291 return pages;
6294 static void nfs4_layoutget_release(void *calldata)
6296 struct nfs4_layoutget *lgp = calldata;
6297 struct inode *inode = lgp->args.inode;
6298 struct nfs_server *server = NFS_SERVER(inode);
6299 size_t max_pages = max_response_pages(server);
6301 dprintk("--> %s\n", __func__);
6302 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6303 pnfs_put_layout_hdr(NFS_I(inode)->layout);
6304 put_nfs_open_context(lgp->args.ctx);
6305 kfree(calldata);
6306 dprintk("<-- %s\n", __func__);
6309 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6310 .rpc_call_prepare = nfs4_layoutget_prepare,
6311 .rpc_call_done = nfs4_layoutget_done,
6312 .rpc_release = nfs4_layoutget_release,
6315 struct pnfs_layout_segment *
6316 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6318 struct inode *inode = lgp->args.inode;
6319 struct nfs_server *server = NFS_SERVER(inode);
6320 size_t max_pages = max_response_pages(server);
6321 struct rpc_task *task;
6322 struct rpc_message msg = {
6323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6324 .rpc_argp = &lgp->args,
6325 .rpc_resp = &lgp->res,
6327 struct rpc_task_setup task_setup_data = {
6328 .rpc_client = server->client,
6329 .rpc_message = &msg,
6330 .callback_ops = &nfs4_layoutget_call_ops,
6331 .callback_data = lgp,
6332 .flags = RPC_TASK_ASYNC,
6334 struct pnfs_layout_segment *lseg = NULL;
6335 int status = 0;
6337 dprintk("--> %s\n", __func__);
6339 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6340 if (!lgp->args.layout.pages) {
6341 nfs4_layoutget_release(lgp);
6342 return ERR_PTR(-ENOMEM);
6344 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6345 lgp->args.timestamp = jiffies;
6347 lgp->res.layoutp = &lgp->args.layout;
6348 lgp->res.seq_res.sr_slot = NULL;
6349 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6351 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6352 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6354 task = rpc_run_task(&task_setup_data);
6355 if (IS_ERR(task))
6356 return ERR_CAST(task);
6357 status = nfs4_wait_for_completion_rpc_task(task);
6358 if (status == 0)
6359 status = task->tk_status;
6360 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6361 if (status == 0 && lgp->res.layoutp->len)
6362 lseg = pnfs_layout_process(lgp);
6363 rpc_put_task(task);
6364 dprintk("<-- %s status=%d\n", __func__, status);
6365 if (status)
6366 return ERR_PTR(status);
6367 return lseg;
6370 static void
6371 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6373 struct nfs4_layoutreturn *lrp = calldata;
6375 dprintk("--> %s\n", __func__);
6376 nfs41_setup_sequence(lrp->clp->cl_session,
6377 &lrp->args.seq_args,
6378 &lrp->res.seq_res,
6379 task);
6382 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6384 struct nfs4_layoutreturn *lrp = calldata;
6385 struct nfs_server *server;
6387 dprintk("--> %s\n", __func__);
6389 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6390 return;
6392 server = NFS_SERVER(lrp->args.inode);
6393 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6394 rpc_restart_call_prepare(task);
6395 return;
6397 dprintk("<-- %s\n", __func__);
6400 static void nfs4_layoutreturn_release(void *calldata)
6402 struct nfs4_layoutreturn *lrp = calldata;
6403 struct pnfs_layout_hdr *lo = lrp->args.layout;
6405 dprintk("--> %s\n", __func__);
6406 spin_lock(&lo->plh_inode->i_lock);
6407 if (lrp->res.lrs_present)
6408 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6409 lo->plh_block_lgets--;
6410 spin_unlock(&lo->plh_inode->i_lock);
6411 pnfs_put_layout_hdr(lrp->args.layout);
6412 kfree(calldata);
6413 dprintk("<-- %s\n", __func__);
6416 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6417 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6418 .rpc_call_done = nfs4_layoutreturn_done,
6419 .rpc_release = nfs4_layoutreturn_release,
6422 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6424 struct rpc_task *task;
6425 struct rpc_message msg = {
6426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6427 .rpc_argp = &lrp->args,
6428 .rpc_resp = &lrp->res,
6430 struct rpc_task_setup task_setup_data = {
6431 .rpc_client = lrp->clp->cl_rpcclient,
6432 .rpc_message = &msg,
6433 .callback_ops = &nfs4_layoutreturn_call_ops,
6434 .callback_data = lrp,
6436 int status;
6438 dprintk("--> %s\n", __func__);
6439 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6440 task = rpc_run_task(&task_setup_data);
6441 if (IS_ERR(task))
6442 return PTR_ERR(task);
6443 status = task->tk_status;
6444 dprintk("<-- %s status=%d\n", __func__, status);
6445 rpc_put_task(task);
6446 return status;
6450 * Retrieve the list of Data Server devices from the MDS.
6452 static int _nfs4_getdevicelist(struct nfs_server *server,
6453 const struct nfs_fh *fh,
6454 struct pnfs_devicelist *devlist)
6456 struct nfs4_getdevicelist_args args = {
6457 .fh = fh,
6458 .layoutclass = server->pnfs_curr_ld->id,
6460 struct nfs4_getdevicelist_res res = {
6461 .devlist = devlist,
6463 struct rpc_message msg = {
6464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6465 .rpc_argp = &args,
6466 .rpc_resp = &res,
6468 int status;
6470 dprintk("--> %s\n", __func__);
6471 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6472 &res.seq_res, 0);
6473 dprintk("<-- %s status=%d\n", __func__, status);
6474 return status;
6477 int nfs4_proc_getdevicelist(struct nfs_server *server,
6478 const struct nfs_fh *fh,
6479 struct pnfs_devicelist *devlist)
6481 struct nfs4_exception exception = { };
6482 int err;
6484 do {
6485 err = nfs4_handle_exception(server,
6486 _nfs4_getdevicelist(server, fh, devlist),
6487 &exception);
6488 } while (exception.retry);
6490 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6491 err, devlist->num_devs);
6493 return err;
6495 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6497 static int
6498 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6500 struct nfs4_getdeviceinfo_args args = {
6501 .pdev = pdev,
6503 struct nfs4_getdeviceinfo_res res = {
6504 .pdev = pdev,
6506 struct rpc_message msg = {
6507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6508 .rpc_argp = &args,
6509 .rpc_resp = &res,
6511 int status;
6513 dprintk("--> %s\n", __func__);
6514 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6515 dprintk("<-- %s status=%d\n", __func__, status);
6517 return status;
6520 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6522 struct nfs4_exception exception = { };
6523 int err;
6525 do {
6526 err = nfs4_handle_exception(server,
6527 _nfs4_proc_getdeviceinfo(server, pdev),
6528 &exception);
6529 } while (exception.retry);
6530 return err;
6532 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6534 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6536 struct nfs4_layoutcommit_data *data = calldata;
6537 struct nfs_server *server = NFS_SERVER(data->args.inode);
6538 struct nfs4_session *session = nfs4_get_session(server);
6540 nfs41_setup_sequence(session,
6541 &data->args.seq_args,
6542 &data->res.seq_res,
6543 task);
6546 static void
6547 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6549 struct nfs4_layoutcommit_data *data = calldata;
6550 struct nfs_server *server = NFS_SERVER(data->args.inode);
6552 if (!nfs41_sequence_done(task, &data->res.seq_res))
6553 return;
6555 switch (task->tk_status) { /* Just ignore these failures */
6556 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6557 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6558 case -NFS4ERR_BADLAYOUT: /* no layout */
6559 case -NFS4ERR_GRACE: /* loca_recalim always false */
6560 task->tk_status = 0;
6561 break;
6562 case 0:
6563 nfs_post_op_update_inode_force_wcc(data->args.inode,
6564 data->res.fattr);
6565 break;
6566 default:
6567 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6568 rpc_restart_call_prepare(task);
6569 return;
6574 static void nfs4_layoutcommit_release(void *calldata)
6576 struct nfs4_layoutcommit_data *data = calldata;
6578 pnfs_cleanup_layoutcommit(data);
6579 put_rpccred(data->cred);
6580 kfree(data);
6583 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6584 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6585 .rpc_call_done = nfs4_layoutcommit_done,
6586 .rpc_release = nfs4_layoutcommit_release,
6590 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6592 struct rpc_message msg = {
6593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6594 .rpc_argp = &data->args,
6595 .rpc_resp = &data->res,
6596 .rpc_cred = data->cred,
6598 struct rpc_task_setup task_setup_data = {
6599 .task = &data->task,
6600 .rpc_client = NFS_CLIENT(data->args.inode),
6601 .rpc_message = &msg,
6602 .callback_ops = &nfs4_layoutcommit_ops,
6603 .callback_data = data,
6604 .flags = RPC_TASK_ASYNC,
6606 struct rpc_task *task;
6607 int status = 0;
6609 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6610 "lbw: %llu inode %lu\n",
6611 data->task.tk_pid, sync,
6612 data->args.lastbytewritten,
6613 data->args.inode->i_ino);
6615 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6616 task = rpc_run_task(&task_setup_data);
6617 if (IS_ERR(task))
6618 return PTR_ERR(task);
6619 if (sync == false)
6620 goto out;
6621 status = nfs4_wait_for_completion_rpc_task(task);
6622 if (status != 0)
6623 goto out;
6624 status = task->tk_status;
6625 out:
6626 dprintk("%s: status %d\n", __func__, status);
6627 rpc_put_task(task);
6628 return status;
6631 static int
6632 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6633 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6635 struct nfs41_secinfo_no_name_args args = {
6636 .style = SECINFO_STYLE_CURRENT_FH,
6638 struct nfs4_secinfo_res res = {
6639 .flavors = flavors,
6641 struct rpc_message msg = {
6642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6643 .rpc_argp = &args,
6644 .rpc_resp = &res,
6646 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6649 static int
6650 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6651 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6653 struct nfs4_exception exception = { };
6654 int err;
6655 do {
6656 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6657 switch (err) {
6658 case 0:
6659 case -NFS4ERR_WRONGSEC:
6660 case -NFS4ERR_NOTSUPP:
6661 goto out;
6662 default:
6663 err = nfs4_handle_exception(server, err, &exception);
6665 } while (exception.retry);
6666 out:
6667 return err;
6670 static int
6671 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6672 struct nfs_fsinfo *info)
6674 int err;
6675 struct page *page;
6676 rpc_authflavor_t flavor;
6677 struct nfs4_secinfo_flavors *flavors;
6679 page = alloc_page(GFP_KERNEL);
6680 if (!page) {
6681 err = -ENOMEM;
6682 goto out;
6685 flavors = page_address(page);
6686 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6689 * Fall back on "guess and check" method if
6690 * the server doesn't support SECINFO_NO_NAME
6692 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6693 err = nfs4_find_root_sec(server, fhandle, info);
6694 goto out_freepage;
6696 if (err)
6697 goto out_freepage;
6699 flavor = nfs_find_best_sec(flavors);
6700 if (err == 0)
6701 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6703 out_freepage:
6704 put_page(page);
6705 if (err == -EACCES)
6706 return -EPERM;
6707 out:
6708 return err;
6711 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6713 int status;
6714 struct nfs41_test_stateid_args args = {
6715 .stateid = stateid,
6717 struct nfs41_test_stateid_res res;
6718 struct rpc_message msg = {
6719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6720 .rpc_argp = &args,
6721 .rpc_resp = &res,
6724 dprintk("NFS call test_stateid %p\n", stateid);
6725 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6726 nfs4_set_sequence_privileged(&args.seq_args);
6727 status = nfs4_call_sync_sequence(server->client, server, &msg,
6728 &args.seq_args, &res.seq_res);
6729 if (status != NFS_OK) {
6730 dprintk("NFS reply test_stateid: failed, %d\n", status);
6731 return status;
6733 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6734 return -res.status;
6738 * nfs41_test_stateid - perform a TEST_STATEID operation
6740 * @server: server / transport on which to perform the operation
6741 * @stateid: state ID to test
6743 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6744 * Otherwise a negative NFS4ERR value is returned if the operation
6745 * failed or the state ID is not currently valid.
6747 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6749 struct nfs4_exception exception = { };
6750 int err;
6751 do {
6752 err = _nfs41_test_stateid(server, stateid);
6753 if (err != -NFS4ERR_DELAY)
6754 break;
6755 nfs4_handle_exception(server, err, &exception);
6756 } while (exception.retry);
6757 return err;
6760 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6762 struct nfs41_free_stateid_args args = {
6763 .stateid = stateid,
6765 struct nfs41_free_stateid_res res;
6766 struct rpc_message msg = {
6767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6768 .rpc_argp = &args,
6769 .rpc_resp = &res,
6771 int status;
6773 dprintk("NFS call free_stateid %p\n", stateid);
6774 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6775 nfs4_set_sequence_privileged(&args.seq_args);
6776 status = nfs4_call_sync_sequence(server->client, server, &msg,
6777 &args.seq_args, &res.seq_res);
6778 dprintk("NFS reply free_stateid: %d\n", status);
6779 return status;
6783 * nfs41_free_stateid - perform a FREE_STATEID operation
6785 * @server: server / transport on which to perform the operation
6786 * @stateid: state ID to release
6788 * Returns NFS_OK if the server freed "stateid". Otherwise a
6789 * negative NFS4ERR value is returned.
6791 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6793 struct nfs4_exception exception = { };
6794 int err;
6795 do {
6796 err = _nfs4_free_stateid(server, stateid);
6797 if (err != -NFS4ERR_DELAY)
6798 break;
6799 nfs4_handle_exception(server, err, &exception);
6800 } while (exception.retry);
6801 return err;
6804 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6805 const nfs4_stateid *s2)
6807 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6808 return false;
6810 if (s1->seqid == s2->seqid)
6811 return true;
6812 if (s1->seqid == 0 || s2->seqid == 0)
6813 return true;
6815 return false;
6818 #endif /* CONFIG_NFS_V4_1 */
6820 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6821 const nfs4_stateid *s2)
6823 return nfs4_stateid_match(s1, s2);
6827 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6828 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6829 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6830 .recover_open = nfs4_open_reclaim,
6831 .recover_lock = nfs4_lock_reclaim,
6832 .establish_clid = nfs4_init_clientid,
6833 .get_clid_cred = nfs4_get_setclientid_cred,
6834 .detect_trunking = nfs40_discover_server_trunking,
6837 #if defined(CONFIG_NFS_V4_1)
6838 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6839 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6840 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6841 .recover_open = nfs4_open_reclaim,
6842 .recover_lock = nfs4_lock_reclaim,
6843 .establish_clid = nfs41_init_clientid,
6844 .get_clid_cred = nfs4_get_exchange_id_cred,
6845 .reclaim_complete = nfs41_proc_reclaim_complete,
6846 .detect_trunking = nfs41_discover_server_trunking,
6848 #endif /* CONFIG_NFS_V4_1 */
6850 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6851 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6852 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6853 .recover_open = nfs4_open_expired,
6854 .recover_lock = nfs4_lock_expired,
6855 .establish_clid = nfs4_init_clientid,
6856 .get_clid_cred = nfs4_get_setclientid_cred,
6859 #if defined(CONFIG_NFS_V4_1)
6860 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6861 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6862 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6863 .recover_open = nfs41_open_expired,
6864 .recover_lock = nfs41_lock_expired,
6865 .establish_clid = nfs41_init_clientid,
6866 .get_clid_cred = nfs4_get_exchange_id_cred,
6868 #endif /* CONFIG_NFS_V4_1 */
6870 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6871 .sched_state_renewal = nfs4_proc_async_renew,
6872 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6873 .renew_lease = nfs4_proc_renew,
6876 #if defined(CONFIG_NFS_V4_1)
6877 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6878 .sched_state_renewal = nfs41_proc_async_sequence,
6879 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6880 .renew_lease = nfs4_proc_sequence,
6882 #endif
6884 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6885 .minor_version = 0,
6886 .init_caps = NFS_CAP_READDIRPLUS
6887 | NFS_CAP_ATOMIC_OPEN
6888 | NFS_CAP_CHANGE_ATTR
6889 | NFS_CAP_POSIX_LOCK,
6890 .call_sync = _nfs4_call_sync,
6891 .match_stateid = nfs4_match_stateid,
6892 .find_root_sec = nfs4_find_root_sec,
6893 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6894 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6895 .state_renewal_ops = &nfs40_state_renewal_ops,
6898 #if defined(CONFIG_NFS_V4_1)
6899 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6900 .minor_version = 1,
6901 .init_caps = NFS_CAP_READDIRPLUS
6902 | NFS_CAP_ATOMIC_OPEN
6903 | NFS_CAP_CHANGE_ATTR
6904 | NFS_CAP_POSIX_LOCK
6905 | NFS_CAP_STATEID_NFSV41
6906 | NFS_CAP_ATOMIC_OPEN_V1,
6907 .call_sync = nfs4_call_sync_sequence,
6908 .match_stateid = nfs41_match_stateid,
6909 .find_root_sec = nfs41_find_root_sec,
6910 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6911 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6912 .state_renewal_ops = &nfs41_state_renewal_ops,
6914 #endif
6916 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6917 [0] = &nfs_v4_0_minor_ops,
6918 #if defined(CONFIG_NFS_V4_1)
6919 [1] = &nfs_v4_1_minor_ops,
6920 #endif
6923 const struct inode_operations nfs4_dir_inode_operations = {
6924 .create = nfs_create,
6925 .lookup = nfs_lookup,
6926 .atomic_open = nfs_atomic_open,
6927 .link = nfs_link,
6928 .unlink = nfs_unlink,
6929 .symlink = nfs_symlink,
6930 .mkdir = nfs_mkdir,
6931 .rmdir = nfs_rmdir,
6932 .mknod = nfs_mknod,
6933 .rename = nfs_rename,
6934 .permission = nfs_permission,
6935 .getattr = nfs_getattr,
6936 .setattr = nfs_setattr,
6937 .getxattr = generic_getxattr,
6938 .setxattr = generic_setxattr,
6939 .listxattr = generic_listxattr,
6940 .removexattr = generic_removexattr,
6943 static const struct inode_operations nfs4_file_inode_operations = {
6944 .permission = nfs_permission,
6945 .getattr = nfs_getattr,
6946 .setattr = nfs_setattr,
6947 .getxattr = generic_getxattr,
6948 .setxattr = generic_setxattr,
6949 .listxattr = generic_listxattr,
6950 .removexattr = generic_removexattr,
6953 const struct nfs_rpc_ops nfs_v4_clientops = {
6954 .version = 4, /* protocol version */
6955 .dentry_ops = &nfs4_dentry_operations,
6956 .dir_inode_ops = &nfs4_dir_inode_operations,
6957 .file_inode_ops = &nfs4_file_inode_operations,
6958 .file_ops = &nfs4_file_operations,
6959 .getroot = nfs4_proc_get_root,
6960 .submount = nfs4_submount,
6961 .try_mount = nfs4_try_mount,
6962 .getattr = nfs4_proc_getattr,
6963 .setattr = nfs4_proc_setattr,
6964 .lookup = nfs4_proc_lookup,
6965 .access = nfs4_proc_access,
6966 .readlink = nfs4_proc_readlink,
6967 .create = nfs4_proc_create,
6968 .remove = nfs4_proc_remove,
6969 .unlink_setup = nfs4_proc_unlink_setup,
6970 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6971 .unlink_done = nfs4_proc_unlink_done,
6972 .rename = nfs4_proc_rename,
6973 .rename_setup = nfs4_proc_rename_setup,
6974 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6975 .rename_done = nfs4_proc_rename_done,
6976 .link = nfs4_proc_link,
6977 .symlink = nfs4_proc_symlink,
6978 .mkdir = nfs4_proc_mkdir,
6979 .rmdir = nfs4_proc_remove,
6980 .readdir = nfs4_proc_readdir,
6981 .mknod = nfs4_proc_mknod,
6982 .statfs = nfs4_proc_statfs,
6983 .fsinfo = nfs4_proc_fsinfo,
6984 .pathconf = nfs4_proc_pathconf,
6985 .set_capabilities = nfs4_server_capabilities,
6986 .decode_dirent = nfs4_decode_dirent,
6987 .read_setup = nfs4_proc_read_setup,
6988 .read_pageio_init = pnfs_pageio_init_read,
6989 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6990 .read_done = nfs4_read_done,
6991 .write_setup = nfs4_proc_write_setup,
6992 .write_pageio_init = pnfs_pageio_init_write,
6993 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6994 .write_done = nfs4_write_done,
6995 .commit_setup = nfs4_proc_commit_setup,
6996 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6997 .commit_done = nfs4_commit_done,
6998 .lock = nfs4_proc_lock,
6999 .clear_acl_cache = nfs4_zap_acl_attr,
7000 .close_context = nfs4_close_context,
7001 .open_context = nfs4_atomic_open,
7002 .have_delegation = nfs4_have_delegation,
7003 .return_delegation = nfs4_inode_return_delegation,
7004 .alloc_client = nfs4_alloc_client,
7005 .init_client = nfs4_init_client,
7006 .free_client = nfs4_free_client,
7007 .create_server = nfs4_create_server,
7008 .clone_server = nfs_clone_server,
7011 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7012 .prefix = XATTR_NAME_NFSV4_ACL,
7013 .list = nfs4_xattr_list_nfs4_acl,
7014 .get = nfs4_xattr_get_nfs4_acl,
7015 .set = nfs4_xattr_set_nfs4_acl,
7018 const struct xattr_handler *nfs4_xattr_handlers[] = {
7019 &nfs4_xattr_nfs4_acl_handler,
7020 NULL
7024 * Local variables:
7025 * c-basic-offset: 8
7026 * End: