Linux 4.15-rc5
[cris-mirror.git] / fs / nfs / nfs4proc.c
blob56fa5a16e0978e73d898b4f32cd4db44b9c18221
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/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
58 #include "nfs4_fs.h"
59 #include "delegation.h"
60 #include "internal.h"
61 #include "iostat.h"
62 #include "callback.h"
63 #include "pnfs.h"
64 #include "netns.h"
65 #include "nfs4idmap.h"
66 #include "nfs4session.h"
67 #include "fscache.h"
69 #include "nfs4trace.h"
71 #define NFSDBG_FACILITY NFSDBG_PROC
73 #define NFS4_POLL_RETRY_MIN (HZ/10)
74 #define NFS4_POLL_RETRY_MAX (15*HZ)
76 /* file attributes which can be mapped to nfs attributes */
77 #define NFS4_VALID_ATTRS (ATTR_MODE \
78 | ATTR_UID \
79 | ATTR_GID \
80 | ATTR_SIZE \
81 | ATTR_ATIME \
82 | ATTR_MTIME \
83 | ATTR_CTIME \
84 | ATTR_ATIME_SET \
85 | ATTR_MTIME_SET)
87 struct nfs4_opendata;
88 static int _nfs4_proc_open(struct nfs4_opendata *data);
89 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
90 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
91 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
92 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
93 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
94 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
95 struct nfs_fattr *fattr, struct iattr *sattr,
96 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
97 struct nfs4_label *olabel);
98 #ifdef CONFIG_NFS_V4_1
99 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
100 struct rpc_cred *cred,
101 struct nfs4_slot *slot,
102 bool is_privileged);
103 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
104 struct rpc_cred *);
105 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
106 struct rpc_cred *, bool);
107 #endif
109 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
110 static inline struct nfs4_label *
111 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
112 struct iattr *sattr, struct nfs4_label *label)
114 int err;
116 if (label == NULL)
117 return NULL;
119 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
120 return NULL;
122 err = security_dentry_init_security(dentry, sattr->ia_mode,
123 &dentry->d_name, (void **)&label->label, &label->len);
124 if (err == 0)
125 return label;
127 return NULL;
129 static inline void
130 nfs4_label_release_security(struct nfs4_label *label)
132 if (label)
133 security_release_secctx(label->label, label->len);
135 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137 if (label)
138 return server->attr_bitmask;
140 return server->attr_bitmask_nl;
142 #else
143 static inline struct nfs4_label *
144 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
145 struct iattr *sattr, struct nfs4_label *l)
146 { return NULL; }
147 static inline void
148 nfs4_label_release_security(struct nfs4_label *label)
149 { return; }
150 static inline u32 *
151 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
152 { return server->attr_bitmask; }
153 #endif
155 /* Prevent leaks of NFSv4 errors into userland */
156 static int nfs4_map_errors(int err)
158 if (err >= -1000)
159 return err;
160 switch (err) {
161 case -NFS4ERR_RESOURCE:
162 case -NFS4ERR_LAYOUTTRYLATER:
163 case -NFS4ERR_RECALLCONFLICT:
164 return -EREMOTEIO;
165 case -NFS4ERR_WRONGSEC:
166 case -NFS4ERR_WRONG_CRED:
167 return -EPERM;
168 case -NFS4ERR_BADOWNER:
169 case -NFS4ERR_BADNAME:
170 return -EINVAL;
171 case -NFS4ERR_SHARE_DENIED:
172 return -EACCES;
173 case -NFS4ERR_MINOR_VERS_MISMATCH:
174 return -EPROTONOSUPPORT;
175 case -NFS4ERR_FILE_OPEN:
176 return -EBUSY;
177 default:
178 dprintk("%s could not handle NFSv4 error %d\n",
179 __func__, -err);
180 break;
182 return -EIO;
186 * This is our standard bitmap for GETATTR requests.
188 const u32 nfs4_fattr_bitmap[3] = {
189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID,
194 FATTR4_WORD1_MODE
195 | FATTR4_WORD1_NUMLINKS
196 | FATTR4_WORD1_OWNER
197 | FATTR4_WORD1_OWNER_GROUP
198 | FATTR4_WORD1_RAWDEV
199 | FATTR4_WORD1_SPACE_USED
200 | FATTR4_WORD1_TIME_ACCESS
201 | FATTR4_WORD1_TIME_METADATA
202 | FATTR4_WORD1_TIME_MODIFY
203 | FATTR4_WORD1_MOUNTED_ON_FILEID,
204 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
205 FATTR4_WORD2_SECURITY_LABEL
206 #endif
209 static const u32 nfs4_pnfs_open_bitmap[3] = {
210 FATTR4_WORD0_TYPE
211 | FATTR4_WORD0_CHANGE
212 | FATTR4_WORD0_SIZE
213 | FATTR4_WORD0_FSID
214 | FATTR4_WORD0_FILEID,
215 FATTR4_WORD1_MODE
216 | FATTR4_WORD1_NUMLINKS
217 | FATTR4_WORD1_OWNER
218 | FATTR4_WORD1_OWNER_GROUP
219 | FATTR4_WORD1_RAWDEV
220 | FATTR4_WORD1_SPACE_USED
221 | FATTR4_WORD1_TIME_ACCESS
222 | FATTR4_WORD1_TIME_METADATA
223 | FATTR4_WORD1_TIME_MODIFY,
224 FATTR4_WORD2_MDSTHRESHOLD
225 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
226 | FATTR4_WORD2_SECURITY_LABEL
227 #endif
230 static const u32 nfs4_open_noattr_bitmap[3] = {
231 FATTR4_WORD0_TYPE
232 | FATTR4_WORD0_FILEID,
235 const u32 nfs4_statfs_bitmap[3] = {
236 FATTR4_WORD0_FILES_AVAIL
237 | FATTR4_WORD0_FILES_FREE
238 | FATTR4_WORD0_FILES_TOTAL,
239 FATTR4_WORD1_SPACE_AVAIL
240 | FATTR4_WORD1_SPACE_FREE
241 | FATTR4_WORD1_SPACE_TOTAL
244 const u32 nfs4_pathconf_bitmap[3] = {
245 FATTR4_WORD0_MAXLINK
246 | FATTR4_WORD0_MAXNAME,
250 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
251 | FATTR4_WORD0_MAXREAD
252 | FATTR4_WORD0_MAXWRITE
253 | FATTR4_WORD0_LEASE_TIME,
254 FATTR4_WORD1_TIME_DELTA
255 | FATTR4_WORD1_FS_LAYOUT_TYPES,
256 FATTR4_WORD2_LAYOUT_BLKSIZE
257 | FATTR4_WORD2_CLONE_BLKSIZE
260 const u32 nfs4_fs_locations_bitmap[3] = {
261 FATTR4_WORD0_CHANGE
262 | FATTR4_WORD0_SIZE
263 | FATTR4_WORD0_FSID
264 | FATTR4_WORD0_FILEID
265 | FATTR4_WORD0_FS_LOCATIONS,
266 FATTR4_WORD1_OWNER
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
276 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
277 struct nfs4_readdir_arg *readdir)
279 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
280 __be32 *start, *p;
282 if (cookie > 2) {
283 readdir->cookie = cookie;
284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
300 start = p = kmap_atomic(*readdir->pages);
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(attrs); /* bitmap */
311 *p++ = htonl(12); /* attribute buffer length */
312 *p++ = htonl(NF4DIR);
313 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
316 *p++ = xdr_one; /* next */
317 *p++ = xdr_zero; /* cookie, first word */
318 *p++ = xdr_two; /* cookie, second word */
319 *p++ = xdr_two; /* entry len */
320 memcpy(p, "..\0\0", 4); /* entry */
321 p++;
322 *p++ = xdr_one; /* bitmap length */
323 *p++ = htonl(attrs); /* bitmap */
324 *p++ = htonl(12); /* attribute buffer length */
325 *p++ = htonl(NF4DIR);
326 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
328 readdir->pgbase = (char *)p - (char *)start;
329 readdir->count -= readdir->pgbase;
330 kunmap_atomic(start);
333 static void nfs4_test_and_free_stateid(struct nfs_server *server,
334 nfs4_stateid *stateid,
335 struct rpc_cred *cred)
337 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
339 ops->test_and_free_expired(server, stateid, cred);
342 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
343 nfs4_stateid *stateid,
344 struct rpc_cred *cred)
346 stateid->type = NFS4_REVOKED_STATEID_TYPE;
347 nfs4_test_and_free_stateid(server, stateid, cred);
350 static void nfs4_free_revoked_stateid(struct nfs_server *server,
351 const nfs4_stateid *stateid,
352 struct rpc_cred *cred)
354 nfs4_stateid tmp;
356 nfs4_stateid_copy(&tmp, stateid);
357 __nfs4_free_revoked_stateid(server, &tmp, cred);
360 static long nfs4_update_delay(long *timeout)
362 long ret;
363 if (!timeout)
364 return NFS4_POLL_RETRY_MAX;
365 if (*timeout <= 0)
366 *timeout = NFS4_POLL_RETRY_MIN;
367 if (*timeout > NFS4_POLL_RETRY_MAX)
368 *timeout = NFS4_POLL_RETRY_MAX;
369 ret = *timeout;
370 *timeout <<= 1;
371 return ret;
374 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
376 int res = 0;
378 might_sleep();
380 freezable_schedule_timeout_killable_unsafe(
381 nfs4_update_delay(timeout));
382 if (fatal_signal_pending(current))
383 res = -ERESTARTSYS;
384 return res;
387 /* This is the error handling routine for processes that are allowed
388 * to sleep.
390 static int nfs4_do_handle_exception(struct nfs_server *server,
391 int errorcode, struct nfs4_exception *exception)
393 struct nfs_client *clp = server->nfs_client;
394 struct nfs4_state *state = exception->state;
395 const nfs4_stateid *stateid = exception->stateid;
396 struct inode *inode = exception->inode;
397 int ret = errorcode;
399 exception->delay = 0;
400 exception->recovering = 0;
401 exception->retry = 0;
403 if (stateid == NULL && state != NULL)
404 stateid = &state->stateid;
406 switch(errorcode) {
407 case 0:
408 return 0;
409 case -NFS4ERR_DELEG_REVOKED:
410 case -NFS4ERR_ADMIN_REVOKED:
411 case -NFS4ERR_EXPIRED:
412 case -NFS4ERR_BAD_STATEID:
413 if (inode != NULL && stateid != NULL) {
414 nfs_inode_find_state_and_recover(inode,
415 stateid);
416 goto wait_on_recovery;
418 case -NFS4ERR_OPENMODE:
419 if (inode) {
420 int err;
422 err = nfs_async_inode_return_delegation(inode,
423 stateid);
424 if (err == 0)
425 goto wait_on_recovery;
426 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
427 exception->retry = 1;
428 break;
431 if (state == NULL)
432 break;
433 ret = nfs4_schedule_stateid_recovery(server, state);
434 if (ret < 0)
435 break;
436 goto wait_on_recovery;
437 case -NFS4ERR_STALE_STATEID:
438 case -NFS4ERR_STALE_CLIENTID:
439 nfs4_schedule_lease_recovery(clp);
440 goto wait_on_recovery;
441 case -NFS4ERR_MOVED:
442 ret = nfs4_schedule_migration_recovery(server);
443 if (ret < 0)
444 break;
445 goto wait_on_recovery;
446 case -NFS4ERR_LEASE_MOVED:
447 nfs4_schedule_lease_moved_recovery(clp);
448 goto wait_on_recovery;
449 #if defined(CONFIG_NFS_V4_1)
450 case -NFS4ERR_BADSESSION:
451 case -NFS4ERR_BADSLOT:
452 case -NFS4ERR_BAD_HIGH_SLOT:
453 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
454 case -NFS4ERR_DEADSESSION:
455 case -NFS4ERR_SEQ_FALSE_RETRY:
456 case -NFS4ERR_SEQ_MISORDERED:
457 dprintk("%s ERROR: %d Reset session\n", __func__,
458 errorcode);
459 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
460 goto wait_on_recovery;
461 #endif /* defined(CONFIG_NFS_V4_1) */
462 case -NFS4ERR_FILE_OPEN:
463 if (exception->timeout > HZ) {
464 /* We have retried a decent amount, time to
465 * fail
467 ret = -EBUSY;
468 break;
470 case -NFS4ERR_DELAY:
471 nfs_inc_server_stats(server, NFSIOS_DELAY);
472 case -NFS4ERR_GRACE:
473 case -NFS4ERR_LAYOUTTRYLATER:
474 case -NFS4ERR_RECALLCONFLICT:
475 exception->delay = 1;
476 return 0;
478 case -NFS4ERR_RETRY_UNCACHED_REP:
479 case -NFS4ERR_OLD_STATEID:
480 exception->retry = 1;
481 break;
482 case -NFS4ERR_BADOWNER:
483 /* The following works around a Linux server bug! */
484 case -NFS4ERR_BADNAME:
485 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
486 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
487 exception->retry = 1;
488 printk(KERN_WARNING "NFS: v4 server %s "
489 "does not accept raw "
490 "uid/gids. "
491 "Reenabling the idmapper.\n",
492 server->nfs_client->cl_hostname);
495 /* We failed to handle the error */
496 return nfs4_map_errors(ret);
497 wait_on_recovery:
498 exception->recovering = 1;
499 return 0;
502 /* This is the error handling routine for processes that are allowed
503 * to sleep.
505 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
507 struct nfs_client *clp = server->nfs_client;
508 int ret;
510 ret = nfs4_do_handle_exception(server, errorcode, exception);
511 if (exception->delay) {
512 ret = nfs4_delay(server->client, &exception->timeout);
513 goto out_retry;
515 if (exception->recovering) {
516 ret = nfs4_wait_clnt_recover(clp);
517 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
518 return -EIO;
519 goto out_retry;
521 return ret;
522 out_retry:
523 if (ret == 0)
524 exception->retry = 1;
525 return ret;
528 static int
529 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
530 int errorcode, struct nfs4_exception *exception)
532 struct nfs_client *clp = server->nfs_client;
533 int ret;
535 ret = nfs4_do_handle_exception(server, errorcode, exception);
536 if (exception->delay) {
537 rpc_delay(task, nfs4_update_delay(&exception->timeout));
538 goto out_retry;
540 if (exception->recovering) {
541 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
542 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
543 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
544 goto out_retry;
546 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
547 ret = -EIO;
548 return ret;
549 out_retry:
550 if (ret == 0)
551 exception->retry = 1;
552 return ret;
555 static int
556 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
557 struct nfs4_state *state, long *timeout)
559 struct nfs4_exception exception = {
560 .state = state,
563 if (task->tk_status >= 0)
564 return 0;
565 if (timeout)
566 exception.timeout = *timeout;
567 task->tk_status = nfs4_async_handle_exception(task, server,
568 task->tk_status,
569 &exception);
570 if (exception.delay && timeout)
571 *timeout = exception.timeout;
572 if (exception.retry)
573 return -EAGAIN;
574 return 0;
578 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
579 * or 'false' otherwise.
581 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
584 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
587 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
589 spin_lock(&clp->cl_lock);
590 if (time_before(clp->cl_last_renewal,timestamp))
591 clp->cl_last_renewal = timestamp;
592 spin_unlock(&clp->cl_lock);
595 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597 struct nfs_client *clp = server->nfs_client;
599 if (!nfs4_has_session(clp))
600 do_renew_lease(clp, timestamp);
603 struct nfs4_call_sync_data {
604 const struct nfs_server *seq_server;
605 struct nfs4_sequence_args *seq_args;
606 struct nfs4_sequence_res *seq_res;
609 void nfs4_init_sequence(struct nfs4_sequence_args *args,
610 struct nfs4_sequence_res *res, int cache_reply)
612 args->sa_slot = NULL;
613 args->sa_cache_this = cache_reply;
614 args->sa_privileged = 0;
616 res->sr_slot = NULL;
619 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621 args->sa_privileged = 1;
624 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
626 struct nfs4_slot *slot = res->sr_slot;
627 struct nfs4_slot_table *tbl;
629 tbl = slot->table;
630 spin_lock(&tbl->slot_tbl_lock);
631 if (!nfs41_wake_and_assign_slot(tbl, slot))
632 nfs4_free_slot(tbl, slot);
633 spin_unlock(&tbl->slot_tbl_lock);
635 res->sr_slot = NULL;
638 static int nfs40_sequence_done(struct rpc_task *task,
639 struct nfs4_sequence_res *res)
641 if (res->sr_slot != NULL)
642 nfs40_sequence_free_slot(res);
643 return 1;
646 #if defined(CONFIG_NFS_V4_1)
648 static void nfs41_release_slot(struct nfs4_slot *slot)
650 struct nfs4_session *session;
651 struct nfs4_slot_table *tbl;
652 bool send_new_highest_used_slotid = false;
654 if (!slot)
655 return;
656 tbl = slot->table;
657 session = tbl->session;
659 /* Bump the slot sequence number */
660 if (slot->seq_done)
661 slot->seq_nr++;
662 slot->seq_done = 0;
664 spin_lock(&tbl->slot_tbl_lock);
665 /* Be nice to the server: try to ensure that the last transmitted
666 * value for highest_user_slotid <= target_highest_slotid
668 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
669 send_new_highest_used_slotid = true;
671 if (nfs41_wake_and_assign_slot(tbl, slot)) {
672 send_new_highest_used_slotid = false;
673 goto out_unlock;
675 nfs4_free_slot(tbl, slot);
677 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
678 send_new_highest_used_slotid = false;
679 out_unlock:
680 spin_unlock(&tbl->slot_tbl_lock);
681 if (send_new_highest_used_slotid)
682 nfs41_notify_server(session->clp);
683 if (waitqueue_active(&tbl->slot_waitq))
684 wake_up_all(&tbl->slot_waitq);
687 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
689 nfs41_release_slot(res->sr_slot);
690 res->sr_slot = NULL;
693 static int nfs41_sequence_process(struct rpc_task *task,
694 struct nfs4_sequence_res *res)
696 struct nfs4_session *session;
697 struct nfs4_slot *slot = res->sr_slot;
698 struct nfs_client *clp;
699 bool interrupted = false;
700 int ret = 1;
702 if (slot == NULL)
703 goto out_noaction;
704 /* don't increment the sequence number if the task wasn't sent */
705 if (!RPC_WAS_SENT(task))
706 goto out;
708 session = slot->table->session;
710 if (slot->interrupted) {
711 if (res->sr_status != -NFS4ERR_DELAY)
712 slot->interrupted = 0;
713 interrupted = true;
716 trace_nfs4_sequence_done(session, res);
717 /* Check the SEQUENCE operation status */
718 switch (res->sr_status) {
719 case 0:
720 /* Update the slot's sequence and clientid lease timer */
721 slot->seq_done = 1;
722 clp = session->clp;
723 do_renew_lease(clp, res->sr_timestamp);
724 /* Check sequence flags */
725 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
726 !!slot->privileged);
727 nfs41_update_target_slotid(slot->table, slot, res);
728 break;
729 case 1:
731 * sr_status remains 1 if an RPC level error occurred.
732 * The server may or may not have processed the sequence
733 * operation..
734 * Mark the slot as having hosted an interrupted RPC call.
736 slot->interrupted = 1;
737 goto out;
738 case -NFS4ERR_DELAY:
739 /* The server detected a resend of the RPC call and
740 * returned NFS4ERR_DELAY as per Section 2.10.6.2
741 * of RFC5661.
743 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
744 __func__,
745 slot->slot_nr,
746 slot->seq_nr);
747 goto out_retry;
748 case -NFS4ERR_BADSLOT:
750 * The slot id we used was probably retired. Try again
751 * using a different slot id.
753 if (slot->seq_nr < slot->table->target_highest_slotid)
754 goto session_recover;
755 goto retry_nowait;
756 case -NFS4ERR_SEQ_MISORDERED:
758 * Was the last operation on this sequence interrupted?
759 * If so, retry after bumping the sequence number.
761 if (interrupted)
762 goto retry_new_seq;
764 * Could this slot have been previously retired?
765 * If so, then the server may be expecting seq_nr = 1!
767 if (slot->seq_nr != 1) {
768 slot->seq_nr = 1;
769 goto retry_nowait;
771 goto session_recover;
772 case -NFS4ERR_SEQ_FALSE_RETRY:
773 if (interrupted)
774 goto retry_new_seq;
775 goto session_recover;
776 default:
777 /* Just update the slot sequence no. */
778 slot->seq_done = 1;
780 out:
781 /* The session may be reset by one of the error handlers. */
782 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
783 out_noaction:
784 return ret;
785 session_recover:
786 nfs4_schedule_session_recovery(session, res->sr_status);
787 goto retry_nowait;
788 retry_new_seq:
789 ++slot->seq_nr;
790 retry_nowait:
791 if (rpc_restart_call_prepare(task)) {
792 nfs41_sequence_free_slot(res);
793 task->tk_status = 0;
794 ret = 0;
796 goto out;
797 out_retry:
798 if (!rpc_restart_call(task))
799 goto out;
800 rpc_delay(task, NFS4_POLL_RETRY_MAX);
801 return 0;
804 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
806 if (!nfs41_sequence_process(task, res))
807 return 0;
808 if (res->sr_slot != NULL)
809 nfs41_sequence_free_slot(res);
810 return 1;
813 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
815 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
817 if (res->sr_slot == NULL)
818 return 1;
819 if (res->sr_slot->table->session != NULL)
820 return nfs41_sequence_process(task, res);
821 return nfs40_sequence_done(task, res);
824 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
826 if (res->sr_slot != NULL) {
827 if (res->sr_slot->table->session != NULL)
828 nfs41_sequence_free_slot(res);
829 else
830 nfs40_sequence_free_slot(res);
834 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
836 if (res->sr_slot == NULL)
837 return 1;
838 if (!res->sr_slot->table->session)
839 return nfs40_sequence_done(task, res);
840 return nfs41_sequence_done(task, res);
842 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
844 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
846 struct nfs4_call_sync_data *data = calldata;
848 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
850 nfs4_setup_sequence(data->seq_server->nfs_client,
851 data->seq_args, data->seq_res, task);
854 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
856 struct nfs4_call_sync_data *data = calldata;
858 nfs41_sequence_done(task, data->seq_res);
861 static const struct rpc_call_ops nfs41_call_sync_ops = {
862 .rpc_call_prepare = nfs41_call_sync_prepare,
863 .rpc_call_done = nfs41_call_sync_done,
866 static void
867 nfs4_sequence_process_interrupted(struct nfs_client *client,
868 struct nfs4_slot *slot, struct rpc_cred *cred)
870 struct rpc_task *task;
872 task = _nfs41_proc_sequence(client, cred, slot, true);
873 if (!IS_ERR(task))
874 rpc_put_task_async(task);
877 #else /* !CONFIG_NFS_V4_1 */
879 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
881 return nfs40_sequence_done(task, res);
884 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
886 if (res->sr_slot != NULL)
887 nfs40_sequence_free_slot(res);
890 int nfs4_sequence_done(struct rpc_task *task,
891 struct nfs4_sequence_res *res)
893 return nfs40_sequence_done(task, res);
895 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
897 static void
898 nfs4_sequence_process_interrupted(struct nfs_client *client,
899 struct nfs4_slot *slot, struct rpc_cred *cred)
901 WARN_ON_ONCE(1);
902 slot->interrupted = 0;
905 #endif /* !CONFIG_NFS_V4_1 */
907 static
908 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
909 struct nfs4_sequence_res *res,
910 struct nfs4_slot *slot)
912 if (!slot)
913 return;
914 slot->privileged = args->sa_privileged ? 1 : 0;
915 args->sa_slot = slot;
917 res->sr_slot = slot;
918 res->sr_timestamp = jiffies;
919 res->sr_status_flags = 0;
920 res->sr_status = 1;
924 int nfs4_setup_sequence(struct nfs_client *client,
925 struct nfs4_sequence_args *args,
926 struct nfs4_sequence_res *res,
927 struct rpc_task *task)
929 struct nfs4_session *session = nfs4_get_session(client);
930 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
931 struct nfs4_slot *slot;
933 /* slot already allocated? */
934 if (res->sr_slot != NULL)
935 goto out_start;
937 if (session) {
938 tbl = &session->fc_slot_table;
939 task->tk_timeout = 0;
942 for (;;) {
943 spin_lock(&tbl->slot_tbl_lock);
944 /* The state manager will wait until the slot table is empty */
945 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
946 goto out_sleep;
948 slot = nfs4_alloc_slot(tbl);
949 if (IS_ERR(slot)) {
950 /* Try again in 1/4 second */
951 if (slot == ERR_PTR(-ENOMEM))
952 task->tk_timeout = HZ >> 2;
953 goto out_sleep;
955 spin_unlock(&tbl->slot_tbl_lock);
957 if (likely(!slot->interrupted))
958 break;
959 nfs4_sequence_process_interrupted(client,
960 slot, task->tk_msg.rpc_cred);
963 nfs4_sequence_attach_slot(args, res, slot);
965 trace_nfs4_setup_sequence(session, args);
966 out_start:
967 rpc_call_start(task);
968 return 0;
970 out_sleep:
971 if (args->sa_privileged)
972 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
973 NULL, RPC_PRIORITY_PRIVILEGED);
974 else
975 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
976 spin_unlock(&tbl->slot_tbl_lock);
977 return -EAGAIN;
979 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
981 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
983 struct nfs4_call_sync_data *data = calldata;
984 nfs4_setup_sequence(data->seq_server->nfs_client,
985 data->seq_args, data->seq_res, task);
988 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
990 struct nfs4_call_sync_data *data = calldata;
991 nfs4_sequence_done(task, data->seq_res);
994 static const struct rpc_call_ops nfs40_call_sync_ops = {
995 .rpc_call_prepare = nfs40_call_sync_prepare,
996 .rpc_call_done = nfs40_call_sync_done,
999 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1000 struct nfs_server *server,
1001 struct rpc_message *msg,
1002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res)
1005 int ret;
1006 struct rpc_task *task;
1007 struct nfs_client *clp = server->nfs_client;
1008 struct nfs4_call_sync_data data = {
1009 .seq_server = server,
1010 .seq_args = args,
1011 .seq_res = res,
1013 struct rpc_task_setup task_setup = {
1014 .rpc_client = clnt,
1015 .rpc_message = msg,
1016 .callback_ops = clp->cl_mvops->call_sync_ops,
1017 .callback_data = &data
1020 task = rpc_run_task(&task_setup);
1021 if (IS_ERR(task))
1022 ret = PTR_ERR(task);
1023 else {
1024 ret = task->tk_status;
1025 rpc_put_task(task);
1027 return ret;
1030 int nfs4_call_sync(struct rpc_clnt *clnt,
1031 struct nfs_server *server,
1032 struct rpc_message *msg,
1033 struct nfs4_sequence_args *args,
1034 struct nfs4_sequence_res *res,
1035 int cache_reply)
1037 nfs4_init_sequence(args, res, cache_reply);
1038 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1041 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1042 unsigned long timestamp)
1044 struct nfs_inode *nfsi = NFS_I(dir);
1046 spin_lock(&dir->i_lock);
1047 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1048 if (cinfo->atomic && cinfo->before == dir->i_version) {
1049 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1050 nfsi->attrtimeo_timestamp = jiffies;
1051 } else {
1052 nfs_force_lookup_revalidate(dir);
1053 if (cinfo->before != dir->i_version)
1054 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1055 NFS_INO_INVALID_ACL;
1057 dir->i_version = cinfo->after;
1058 nfsi->read_cache_jiffies = timestamp;
1059 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1060 nfs_fscache_invalidate(dir);
1061 spin_unlock(&dir->i_lock);
1064 struct nfs4_opendata {
1065 struct kref kref;
1066 struct nfs_openargs o_arg;
1067 struct nfs_openres o_res;
1068 struct nfs_open_confirmargs c_arg;
1069 struct nfs_open_confirmres c_res;
1070 struct nfs4_string owner_name;
1071 struct nfs4_string group_name;
1072 struct nfs4_label *a_label;
1073 struct nfs_fattr f_attr;
1074 struct nfs4_label *f_label;
1075 struct dentry *dir;
1076 struct dentry *dentry;
1077 struct nfs4_state_owner *owner;
1078 struct nfs4_state *state;
1079 struct iattr attrs;
1080 unsigned long timestamp;
1081 bool rpc_done;
1082 bool file_created;
1083 bool is_recover;
1084 bool cancelled;
1085 int rpc_status;
1088 struct nfs4_open_createattrs {
1089 struct nfs4_label *label;
1090 struct iattr *sattr;
1091 const __u32 verf[2];
1094 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1095 int err, struct nfs4_exception *exception)
1097 if (err != -EINVAL)
1098 return false;
1099 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1100 return false;
1101 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1102 exception->retry = 1;
1103 return true;
1106 static u32
1107 nfs4_map_atomic_open_share(struct nfs_server *server,
1108 fmode_t fmode, int openflags)
1110 u32 res = 0;
1112 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1113 case FMODE_READ:
1114 res = NFS4_SHARE_ACCESS_READ;
1115 break;
1116 case FMODE_WRITE:
1117 res = NFS4_SHARE_ACCESS_WRITE;
1118 break;
1119 case FMODE_READ|FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_BOTH;
1122 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1123 goto out;
1124 /* Want no delegation if we're using O_DIRECT */
1125 if (openflags & O_DIRECT)
1126 res |= NFS4_SHARE_WANT_NO_DELEG;
1127 out:
1128 return res;
1131 static enum open_claim_type4
1132 nfs4_map_atomic_open_claim(struct nfs_server *server,
1133 enum open_claim_type4 claim)
1135 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1136 return claim;
1137 switch (claim) {
1138 default:
1139 return claim;
1140 case NFS4_OPEN_CLAIM_FH:
1141 return NFS4_OPEN_CLAIM_NULL;
1142 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1143 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1144 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1145 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1149 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1151 p->o_res.f_attr = &p->f_attr;
1152 p->o_res.f_label = p->f_label;
1153 p->o_res.seqid = p->o_arg.seqid;
1154 p->c_res.seqid = p->c_arg.seqid;
1155 p->o_res.server = p->o_arg.server;
1156 p->o_res.access_request = p->o_arg.access;
1157 nfs_fattr_init(&p->f_attr);
1158 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1161 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1162 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1163 const struct nfs4_open_createattrs *c,
1164 enum open_claim_type4 claim,
1165 gfp_t gfp_mask)
1167 struct dentry *parent = dget_parent(dentry);
1168 struct inode *dir = d_inode(parent);
1169 struct nfs_server *server = NFS_SERVER(dir);
1170 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1171 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1172 struct nfs4_opendata *p;
1174 p = kzalloc(sizeof(*p), gfp_mask);
1175 if (p == NULL)
1176 goto err;
1178 p->f_label = nfs4_label_alloc(server, gfp_mask);
1179 if (IS_ERR(p->f_label))
1180 goto err_free_p;
1182 p->a_label = nfs4_label_alloc(server, gfp_mask);
1183 if (IS_ERR(p->a_label))
1184 goto err_free_f;
1186 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1187 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1188 if (IS_ERR(p->o_arg.seqid))
1189 goto err_free_label;
1190 nfs_sb_active(dentry->d_sb);
1191 p->dentry = dget(dentry);
1192 p->dir = parent;
1193 p->owner = sp;
1194 atomic_inc(&sp->so_count);
1195 p->o_arg.open_flags = flags;
1196 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1197 p->o_arg.umask = current_umask();
1198 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1199 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1200 fmode, flags);
1201 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1202 * will return permission denied for all bits until close */
1203 if (!(flags & O_EXCL)) {
1204 /* ask server to check for all possible rights as results
1205 * are cached */
1206 switch (p->o_arg.claim) {
1207 default:
1208 break;
1209 case NFS4_OPEN_CLAIM_NULL:
1210 case NFS4_OPEN_CLAIM_FH:
1211 p->o_arg.access = NFS4_ACCESS_READ |
1212 NFS4_ACCESS_MODIFY |
1213 NFS4_ACCESS_EXTEND |
1214 NFS4_ACCESS_EXECUTE;
1217 p->o_arg.clientid = server->nfs_client->cl_clientid;
1218 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1219 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1220 p->o_arg.name = &dentry->d_name;
1221 p->o_arg.server = server;
1222 p->o_arg.bitmask = nfs4_bitmask(server, label);
1223 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1224 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1225 switch (p->o_arg.claim) {
1226 case NFS4_OPEN_CLAIM_NULL:
1227 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1228 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1229 p->o_arg.fh = NFS_FH(dir);
1230 break;
1231 case NFS4_OPEN_CLAIM_PREVIOUS:
1232 case NFS4_OPEN_CLAIM_FH:
1233 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1234 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1235 p->o_arg.fh = NFS_FH(d_inode(dentry));
1237 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
1238 p->o_arg.u.attrs = &p->attrs;
1239 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1241 memcpy(p->o_arg.u.verifier.data, c->verf,
1242 sizeof(p->o_arg.u.verifier.data));
1244 p->c_arg.fh = &p->o_res.fh;
1245 p->c_arg.stateid = &p->o_res.stateid;
1246 p->c_arg.seqid = p->o_arg.seqid;
1247 nfs4_init_opendata_res(p);
1248 kref_init(&p->kref);
1249 return p;
1251 err_free_label:
1252 nfs4_label_free(p->a_label);
1253 err_free_f:
1254 nfs4_label_free(p->f_label);
1255 err_free_p:
1256 kfree(p);
1257 err:
1258 dput(parent);
1259 return NULL;
1262 static void nfs4_opendata_free(struct kref *kref)
1264 struct nfs4_opendata *p = container_of(kref,
1265 struct nfs4_opendata, kref);
1266 struct super_block *sb = p->dentry->d_sb;
1268 nfs_free_seqid(p->o_arg.seqid);
1269 nfs4_sequence_free_slot(&p->o_res.seq_res);
1270 if (p->state != NULL)
1271 nfs4_put_open_state(p->state);
1272 nfs4_put_state_owner(p->owner);
1274 nfs4_label_free(p->a_label);
1275 nfs4_label_free(p->f_label);
1277 dput(p->dir);
1278 dput(p->dentry);
1279 nfs_sb_deactive(sb);
1280 nfs_fattr_free_names(&p->f_attr);
1281 kfree(p->f_attr.mdsthreshold);
1282 kfree(p);
1285 static void nfs4_opendata_put(struct nfs4_opendata *p)
1287 if (p != NULL)
1288 kref_put(&p->kref, nfs4_opendata_free);
1291 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1292 fmode_t fmode)
1294 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1295 case FMODE_READ|FMODE_WRITE:
1296 return state->n_rdwr != 0;
1297 case FMODE_WRITE:
1298 return state->n_wronly != 0;
1299 case FMODE_READ:
1300 return state->n_rdonly != 0;
1302 WARN_ON_ONCE(1);
1303 return false;
1306 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1308 int ret = 0;
1310 if (open_mode & (O_EXCL|O_TRUNC))
1311 goto out;
1312 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1313 case FMODE_READ:
1314 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1315 && state->n_rdonly != 0;
1316 break;
1317 case FMODE_WRITE:
1318 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1319 && state->n_wronly != 0;
1320 break;
1321 case FMODE_READ|FMODE_WRITE:
1322 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1323 && state->n_rdwr != 0;
1325 out:
1326 return ret;
1329 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1330 enum open_claim_type4 claim)
1332 if (delegation == NULL)
1333 return 0;
1334 if ((delegation->type & fmode) != fmode)
1335 return 0;
1336 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1337 return 0;
1338 switch (claim) {
1339 case NFS4_OPEN_CLAIM_NULL:
1340 case NFS4_OPEN_CLAIM_FH:
1341 break;
1342 case NFS4_OPEN_CLAIM_PREVIOUS:
1343 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1344 break;
1345 default:
1346 return 0;
1348 nfs_mark_delegation_referenced(delegation);
1349 return 1;
1352 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1354 switch (fmode) {
1355 case FMODE_WRITE:
1356 state->n_wronly++;
1357 break;
1358 case FMODE_READ:
1359 state->n_rdonly++;
1360 break;
1361 case FMODE_READ|FMODE_WRITE:
1362 state->n_rdwr++;
1364 nfs4_state_set_mode_locked(state, state->state | fmode);
1367 #ifdef CONFIG_NFS_V4_1
1368 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1370 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1371 return true;
1372 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1373 return true;
1374 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1375 return true;
1376 return false;
1378 #endif /* CONFIG_NFS_V4_1 */
1380 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1382 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1383 wake_up_all(&state->waitq);
1386 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1387 const nfs4_stateid *stateid)
1389 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1390 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1392 if (stateid_seqid == state_seqid + 1U ||
1393 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1394 nfs_state_log_update_open_stateid(state);
1395 else
1396 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1399 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1401 struct nfs_client *clp = state->owner->so_server->nfs_client;
1402 bool need_recover = false;
1404 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1405 need_recover = true;
1406 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1407 need_recover = true;
1408 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1409 need_recover = true;
1410 if (need_recover)
1411 nfs4_state_mark_reclaim_nograce(clp, state);
1415 * Check for whether or not the caller may update the open stateid
1416 * to the value passed in by stateid.
1418 * Note: This function relies heavily on the server implementing
1419 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1420 * correctly.
1421 * i.e. The stateid seqids have to be initialised to 1, and
1422 * are then incremented on every state transition.
1424 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1425 const nfs4_stateid *stateid)
1427 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1428 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1429 if (stateid->seqid == cpu_to_be32(1))
1430 nfs_state_log_update_open_stateid(state);
1431 else
1432 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1433 return true;
1436 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1437 nfs_state_log_out_of_order_open_stateid(state, stateid);
1438 return true;
1440 return false;
1443 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1445 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1446 return;
1447 if (state->n_wronly)
1448 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1449 if (state->n_rdonly)
1450 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1451 if (state->n_rdwr)
1452 set_bit(NFS_O_RDWR_STATE, &state->flags);
1453 set_bit(NFS_OPEN_STATE, &state->flags);
1456 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1457 nfs4_stateid *stateid, fmode_t fmode)
1459 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1460 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1461 case FMODE_WRITE:
1462 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1463 break;
1464 case FMODE_READ:
1465 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1466 break;
1467 case 0:
1468 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 clear_bit(NFS_OPEN_STATE, &state->flags);
1472 if (stateid == NULL)
1473 return;
1474 /* Handle OPEN+OPEN_DOWNGRADE races */
1475 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1476 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1477 nfs_resync_open_stateid_locked(state);
1478 goto out;
1480 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1481 nfs4_stateid_copy(&state->stateid, stateid);
1482 nfs4_stateid_copy(&state->open_stateid, stateid);
1483 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1484 out:
1485 nfs_state_log_update_open_stateid(state);
1488 static void nfs_clear_open_stateid(struct nfs4_state *state,
1489 nfs4_stateid *arg_stateid,
1490 nfs4_stateid *stateid, fmode_t fmode)
1492 write_seqlock(&state->seqlock);
1493 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1494 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1495 nfs_clear_open_stateid_locked(state, stateid, fmode);
1496 write_sequnlock(&state->seqlock);
1497 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1498 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1501 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1502 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1504 DEFINE_WAIT(wait);
1505 int status = 0;
1506 for (;;) {
1508 if (!nfs_need_update_open_stateid(state, stateid))
1509 return;
1510 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1511 break;
1512 if (status)
1513 break;
1514 /* Rely on seqids for serialisation with NFSv4.0 */
1515 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1516 break;
1518 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1520 * Ensure we process the state changes in the same order
1521 * in which the server processed them by delaying the
1522 * update of the stateid until we are in sequence.
1524 write_sequnlock(&state->seqlock);
1525 spin_unlock(&state->owner->so_lock);
1526 rcu_read_unlock();
1527 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1528 if (!signal_pending(current)) {
1529 if (schedule_timeout(5*HZ) == 0)
1530 status = -EAGAIN;
1531 else
1532 status = 0;
1533 } else
1534 status = -EINTR;
1535 finish_wait(&state->waitq, &wait);
1536 rcu_read_lock();
1537 spin_lock(&state->owner->so_lock);
1538 write_seqlock(&state->seqlock);
1541 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1542 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1543 nfs4_stateid_copy(freeme, &state->open_stateid);
1544 nfs_test_and_clear_all_open_stateid(state);
1547 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1548 nfs4_stateid_copy(&state->stateid, stateid);
1549 nfs4_stateid_copy(&state->open_stateid, stateid);
1550 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1551 nfs_state_log_update_open_stateid(state);
1554 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1555 const nfs4_stateid *open_stateid,
1556 fmode_t fmode,
1557 nfs4_stateid *freeme)
1560 * Protect the call to nfs4_state_set_mode_locked and
1561 * serialise the stateid update
1563 write_seqlock(&state->seqlock);
1564 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1565 switch (fmode) {
1566 case FMODE_READ:
1567 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1568 break;
1569 case FMODE_WRITE:
1570 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_READ|FMODE_WRITE:
1573 set_bit(NFS_O_RDWR_STATE, &state->flags);
1575 set_bit(NFS_OPEN_STATE, &state->flags);
1576 write_sequnlock(&state->seqlock);
1579 static void nfs_state_set_delegation(struct nfs4_state *state,
1580 const nfs4_stateid *deleg_stateid,
1581 fmode_t fmode)
1584 * Protect the call to nfs4_state_set_mode_locked and
1585 * serialise the stateid update
1587 write_seqlock(&state->seqlock);
1588 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1589 set_bit(NFS_DELEGATED_STATE, &state->flags);
1590 write_sequnlock(&state->seqlock);
1593 static int update_open_stateid(struct nfs4_state *state,
1594 const nfs4_stateid *open_stateid,
1595 const nfs4_stateid *delegation,
1596 fmode_t fmode)
1598 struct nfs_server *server = NFS_SERVER(state->inode);
1599 struct nfs_client *clp = server->nfs_client;
1600 struct nfs_inode *nfsi = NFS_I(state->inode);
1601 struct nfs_delegation *deleg_cur;
1602 nfs4_stateid freeme = { };
1603 int ret = 0;
1605 fmode &= (FMODE_READ|FMODE_WRITE);
1607 rcu_read_lock();
1608 spin_lock(&state->owner->so_lock);
1609 if (open_stateid != NULL) {
1610 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1611 ret = 1;
1614 deleg_cur = rcu_dereference(nfsi->delegation);
1615 if (deleg_cur == NULL)
1616 goto no_delegation;
1618 spin_lock(&deleg_cur->lock);
1619 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1620 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1621 (deleg_cur->type & fmode) != fmode)
1622 goto no_delegation_unlock;
1624 if (delegation == NULL)
1625 delegation = &deleg_cur->stateid;
1626 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1627 goto no_delegation_unlock;
1629 nfs_mark_delegation_referenced(deleg_cur);
1630 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1631 ret = 1;
1632 no_delegation_unlock:
1633 spin_unlock(&deleg_cur->lock);
1634 no_delegation:
1635 if (ret)
1636 update_open_stateflags(state, fmode);
1637 spin_unlock(&state->owner->so_lock);
1638 rcu_read_unlock();
1640 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1641 nfs4_schedule_state_manager(clp);
1642 if (freeme.type != 0)
1643 nfs4_test_and_free_stateid(server, &freeme,
1644 state->owner->so_cred);
1646 return ret;
1649 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1650 const nfs4_stateid *stateid)
1652 struct nfs4_state *state = lsp->ls_state;
1653 bool ret = false;
1655 spin_lock(&state->state_lock);
1656 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1657 goto out_noupdate;
1658 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1659 goto out_noupdate;
1660 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1661 ret = true;
1662 out_noupdate:
1663 spin_unlock(&state->state_lock);
1664 return ret;
1667 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1669 struct nfs_delegation *delegation;
1671 rcu_read_lock();
1672 delegation = rcu_dereference(NFS_I(inode)->delegation);
1673 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1674 rcu_read_unlock();
1675 return;
1677 rcu_read_unlock();
1678 nfs4_inode_return_delegation(inode);
1681 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1683 struct nfs4_state *state = opendata->state;
1684 struct nfs_inode *nfsi = NFS_I(state->inode);
1685 struct nfs_delegation *delegation;
1686 int open_mode = opendata->o_arg.open_flags;
1687 fmode_t fmode = opendata->o_arg.fmode;
1688 enum open_claim_type4 claim = opendata->o_arg.claim;
1689 nfs4_stateid stateid;
1690 int ret = -EAGAIN;
1692 for (;;) {
1693 spin_lock(&state->owner->so_lock);
1694 if (can_open_cached(state, fmode, open_mode)) {
1695 update_open_stateflags(state, fmode);
1696 spin_unlock(&state->owner->so_lock);
1697 goto out_return_state;
1699 spin_unlock(&state->owner->so_lock);
1700 rcu_read_lock();
1701 delegation = rcu_dereference(nfsi->delegation);
1702 if (!can_open_delegated(delegation, fmode, claim)) {
1703 rcu_read_unlock();
1704 break;
1706 /* Save the delegation */
1707 nfs4_stateid_copy(&stateid, &delegation->stateid);
1708 rcu_read_unlock();
1709 nfs_release_seqid(opendata->o_arg.seqid);
1710 if (!opendata->is_recover) {
1711 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1712 if (ret != 0)
1713 goto out;
1715 ret = -EAGAIN;
1717 /* Try to update the stateid using the delegation */
1718 if (update_open_stateid(state, NULL, &stateid, fmode))
1719 goto out_return_state;
1721 out:
1722 return ERR_PTR(ret);
1723 out_return_state:
1724 atomic_inc(&state->count);
1725 return state;
1728 static void
1729 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1731 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1732 struct nfs_delegation *delegation;
1733 int delegation_flags = 0;
1735 rcu_read_lock();
1736 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1737 if (delegation)
1738 delegation_flags = delegation->flags;
1739 rcu_read_unlock();
1740 switch (data->o_arg.claim) {
1741 default:
1742 break;
1743 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1744 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1745 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1746 "returning a delegation for "
1747 "OPEN(CLAIM_DELEGATE_CUR)\n",
1748 clp->cl_hostname);
1749 return;
1751 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1752 nfs_inode_set_delegation(state->inode,
1753 data->owner->so_cred,
1754 &data->o_res);
1755 else
1756 nfs_inode_reclaim_delegation(state->inode,
1757 data->owner->so_cred,
1758 &data->o_res);
1762 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1763 * and update the nfs4_state.
1765 static struct nfs4_state *
1766 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1768 struct inode *inode = data->state->inode;
1769 struct nfs4_state *state = data->state;
1770 int ret;
1772 if (!data->rpc_done) {
1773 if (data->rpc_status)
1774 return ERR_PTR(data->rpc_status);
1775 /* cached opens have already been processed */
1776 goto update;
1779 ret = nfs_refresh_inode(inode, &data->f_attr);
1780 if (ret)
1781 return ERR_PTR(ret);
1783 if (data->o_res.delegation_type != 0)
1784 nfs4_opendata_check_deleg(data, state);
1785 update:
1786 update_open_stateid(state, &data->o_res.stateid, NULL,
1787 data->o_arg.fmode);
1788 atomic_inc(&state->count);
1790 return state;
1793 static struct inode *
1794 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1796 struct inode *inode;
1798 switch (data->o_arg.claim) {
1799 case NFS4_OPEN_CLAIM_NULL:
1800 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1801 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1802 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1803 return ERR_PTR(-EAGAIN);
1804 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1805 &data->f_attr, data->f_label);
1806 break;
1807 default:
1808 inode = d_inode(data->dentry);
1809 ihold(inode);
1810 nfs_refresh_inode(inode, &data->f_attr);
1812 return inode;
1815 static struct nfs4_state *
1816 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1818 struct nfs4_state *state;
1819 struct inode *inode;
1821 inode = nfs4_opendata_get_inode(data);
1822 if (IS_ERR(inode))
1823 return ERR_CAST(inode);
1824 if (data->state != NULL && data->state->inode == inode) {
1825 state = data->state;
1826 atomic_inc(&state->count);
1827 } else
1828 state = nfs4_get_open_state(inode, data->owner);
1829 iput(inode);
1830 if (state == NULL)
1831 state = ERR_PTR(-ENOMEM);
1832 return state;
1835 static struct nfs4_state *
1836 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1838 struct nfs4_state *state;
1840 if (!data->rpc_done) {
1841 state = nfs4_try_open_cached(data);
1842 trace_nfs4_cached_open(data->state);
1843 goto out;
1846 state = nfs4_opendata_find_nfs4_state(data);
1847 if (IS_ERR(state))
1848 goto out;
1850 if (data->o_res.delegation_type != 0)
1851 nfs4_opendata_check_deleg(data, state);
1852 update_open_stateid(state, &data->o_res.stateid, NULL,
1853 data->o_arg.fmode);
1854 out:
1855 nfs_release_seqid(data->o_arg.seqid);
1856 return state;
1859 static struct nfs4_state *
1860 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1862 struct nfs4_state *ret;
1864 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1865 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1866 else
1867 ret = _nfs4_opendata_to_nfs4_state(data);
1868 nfs4_sequence_free_slot(&data->o_res.seq_res);
1869 return ret;
1872 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1874 struct nfs_inode *nfsi = NFS_I(state->inode);
1875 struct nfs_open_context *ctx;
1877 spin_lock(&state->inode->i_lock);
1878 list_for_each_entry(ctx, &nfsi->open_files, list) {
1879 if (ctx->state != state)
1880 continue;
1881 get_nfs_open_context(ctx);
1882 spin_unlock(&state->inode->i_lock);
1883 return ctx;
1885 spin_unlock(&state->inode->i_lock);
1886 return ERR_PTR(-ENOENT);
1889 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1890 struct nfs4_state *state, enum open_claim_type4 claim)
1892 struct nfs4_opendata *opendata;
1894 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1895 NULL, claim, GFP_NOFS);
1896 if (opendata == NULL)
1897 return ERR_PTR(-ENOMEM);
1898 opendata->state = state;
1899 atomic_inc(&state->count);
1900 return opendata;
1903 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1904 fmode_t fmode)
1906 struct nfs4_state *newstate;
1907 int ret;
1909 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1910 return 0;
1911 opendata->o_arg.open_flags = 0;
1912 opendata->o_arg.fmode = fmode;
1913 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1914 NFS_SB(opendata->dentry->d_sb),
1915 fmode, 0);
1916 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1917 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1918 nfs4_init_opendata_res(opendata);
1919 ret = _nfs4_recover_proc_open(opendata);
1920 if (ret != 0)
1921 return ret;
1922 newstate = nfs4_opendata_to_nfs4_state(opendata);
1923 if (IS_ERR(newstate))
1924 return PTR_ERR(newstate);
1925 if (newstate != opendata->state)
1926 ret = -ESTALE;
1927 nfs4_close_state(newstate, fmode);
1928 return ret;
1931 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1933 int ret;
1935 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1936 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1937 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1938 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1939 /* memory barrier prior to reading state->n_* */
1940 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1941 clear_bit(NFS_OPEN_STATE, &state->flags);
1942 smp_rmb();
1943 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1944 if (ret != 0)
1945 return ret;
1946 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1947 if (ret != 0)
1948 return ret;
1949 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1950 if (ret != 0)
1951 return ret;
1953 * We may have performed cached opens for all three recoveries.
1954 * Check if we need to update the current stateid.
1956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1957 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1958 write_seqlock(&state->seqlock);
1959 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1960 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1961 write_sequnlock(&state->seqlock);
1963 return 0;
1967 * OPEN_RECLAIM:
1968 * reclaim state on the server after a reboot.
1970 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1972 struct nfs_delegation *delegation;
1973 struct nfs4_opendata *opendata;
1974 fmode_t delegation_type = 0;
1975 int status;
1977 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1978 NFS4_OPEN_CLAIM_PREVIOUS);
1979 if (IS_ERR(opendata))
1980 return PTR_ERR(opendata);
1981 rcu_read_lock();
1982 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1983 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1984 delegation_type = delegation->type;
1985 rcu_read_unlock();
1986 opendata->o_arg.u.delegation_type = delegation_type;
1987 status = nfs4_open_recover(opendata, state);
1988 nfs4_opendata_put(opendata);
1989 return status;
1992 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1994 struct nfs_server *server = NFS_SERVER(state->inode);
1995 struct nfs4_exception exception = { };
1996 int err;
1997 do {
1998 err = _nfs4_do_open_reclaim(ctx, state);
1999 trace_nfs4_open_reclaim(ctx, 0, err);
2000 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2001 continue;
2002 if (err != -NFS4ERR_DELAY)
2003 break;
2004 nfs4_handle_exception(server, err, &exception);
2005 } while (exception.retry);
2006 return err;
2009 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2011 struct nfs_open_context *ctx;
2012 int ret;
2014 ctx = nfs4_state_find_open_context(state);
2015 if (IS_ERR(ctx))
2016 return -EAGAIN;
2017 ret = nfs4_do_open_reclaim(ctx, state);
2018 put_nfs_open_context(ctx);
2019 return ret;
2022 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
2024 switch (err) {
2025 default:
2026 printk(KERN_ERR "NFS: %s: unhandled error "
2027 "%d.\n", __func__, err);
2028 case 0:
2029 case -ENOENT:
2030 case -EAGAIN:
2031 case -ESTALE:
2032 break;
2033 case -NFS4ERR_BADSESSION:
2034 case -NFS4ERR_BADSLOT:
2035 case -NFS4ERR_BAD_HIGH_SLOT:
2036 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2037 case -NFS4ERR_DEADSESSION:
2038 set_bit(NFS_DELEGATED_STATE, &state->flags);
2039 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2040 return -EAGAIN;
2041 case -NFS4ERR_STALE_CLIENTID:
2042 case -NFS4ERR_STALE_STATEID:
2043 set_bit(NFS_DELEGATED_STATE, &state->flags);
2044 /* Don't recall a delegation if it was lost */
2045 nfs4_schedule_lease_recovery(server->nfs_client);
2046 return -EAGAIN;
2047 case -NFS4ERR_MOVED:
2048 nfs4_schedule_migration_recovery(server);
2049 return -EAGAIN;
2050 case -NFS4ERR_LEASE_MOVED:
2051 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2052 return -EAGAIN;
2053 case -NFS4ERR_DELEG_REVOKED:
2054 case -NFS4ERR_ADMIN_REVOKED:
2055 case -NFS4ERR_EXPIRED:
2056 case -NFS4ERR_BAD_STATEID:
2057 case -NFS4ERR_OPENMODE:
2058 nfs_inode_find_state_and_recover(state->inode,
2059 stateid);
2060 nfs4_schedule_stateid_recovery(server, state);
2061 return -EAGAIN;
2062 case -NFS4ERR_DELAY:
2063 case -NFS4ERR_GRACE:
2064 set_bit(NFS_DELEGATED_STATE, &state->flags);
2065 ssleep(1);
2066 return -EAGAIN;
2067 case -ENOMEM:
2068 case -NFS4ERR_DENIED:
2069 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2070 return 0;
2072 return err;
2075 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2076 struct nfs4_state *state, const nfs4_stateid *stateid,
2077 fmode_t type)
2079 struct nfs_server *server = NFS_SERVER(state->inode);
2080 struct nfs4_opendata *opendata;
2081 int err = 0;
2083 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2084 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2085 if (IS_ERR(opendata))
2086 return PTR_ERR(opendata);
2087 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2088 write_seqlock(&state->seqlock);
2089 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2090 write_sequnlock(&state->seqlock);
2091 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2092 switch (type & (FMODE_READ|FMODE_WRITE)) {
2093 case FMODE_READ|FMODE_WRITE:
2094 case FMODE_WRITE:
2095 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2096 if (err)
2097 break;
2098 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2099 if (err)
2100 break;
2101 case FMODE_READ:
2102 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2104 nfs4_opendata_put(opendata);
2105 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2108 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2110 struct nfs4_opendata *data = calldata;
2112 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2113 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2116 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2118 struct nfs4_opendata *data = calldata;
2120 nfs40_sequence_done(task, &data->c_res.seq_res);
2122 data->rpc_status = task->tk_status;
2123 if (data->rpc_status == 0) {
2124 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2125 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2126 renew_lease(data->o_res.server, data->timestamp);
2127 data->rpc_done = true;
2131 static void nfs4_open_confirm_release(void *calldata)
2133 struct nfs4_opendata *data = calldata;
2134 struct nfs4_state *state = NULL;
2136 /* If this request hasn't been cancelled, do nothing */
2137 if (!data->cancelled)
2138 goto out_free;
2139 /* In case of error, no cleanup! */
2140 if (!data->rpc_done)
2141 goto out_free;
2142 state = nfs4_opendata_to_nfs4_state(data);
2143 if (!IS_ERR(state))
2144 nfs4_close_state(state, data->o_arg.fmode);
2145 out_free:
2146 nfs4_opendata_put(data);
2149 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2150 .rpc_call_prepare = nfs4_open_confirm_prepare,
2151 .rpc_call_done = nfs4_open_confirm_done,
2152 .rpc_release = nfs4_open_confirm_release,
2156 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2158 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2160 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2161 struct rpc_task *task;
2162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2164 .rpc_argp = &data->c_arg,
2165 .rpc_resp = &data->c_res,
2166 .rpc_cred = data->owner->so_cred,
2168 struct rpc_task_setup task_setup_data = {
2169 .rpc_client = server->client,
2170 .rpc_message = &msg,
2171 .callback_ops = &nfs4_open_confirm_ops,
2172 .callback_data = data,
2173 .workqueue = nfsiod_workqueue,
2174 .flags = RPC_TASK_ASYNC,
2176 int status;
2178 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2179 kref_get(&data->kref);
2180 data->rpc_done = false;
2181 data->rpc_status = 0;
2182 data->timestamp = jiffies;
2183 if (data->is_recover)
2184 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2185 task = rpc_run_task(&task_setup_data);
2186 if (IS_ERR(task))
2187 return PTR_ERR(task);
2188 status = rpc_wait_for_completion_task(task);
2189 if (status != 0) {
2190 data->cancelled = true;
2191 smp_wmb();
2192 } else
2193 status = data->rpc_status;
2194 rpc_put_task(task);
2195 return status;
2198 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2200 struct nfs4_opendata *data = calldata;
2201 struct nfs4_state_owner *sp = data->owner;
2202 struct nfs_client *clp = sp->so_server->nfs_client;
2203 enum open_claim_type4 claim = data->o_arg.claim;
2205 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2206 goto out_wait;
2208 * Check if we still need to send an OPEN call, or if we can use
2209 * a delegation instead.
2211 if (data->state != NULL) {
2212 struct nfs_delegation *delegation;
2214 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2215 goto out_no_action;
2216 rcu_read_lock();
2217 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2218 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2219 goto unlock_no_action;
2220 rcu_read_unlock();
2222 /* Update client id. */
2223 data->o_arg.clientid = clp->cl_clientid;
2224 switch (claim) {
2225 default:
2226 break;
2227 case NFS4_OPEN_CLAIM_PREVIOUS:
2228 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2229 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2230 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2231 case NFS4_OPEN_CLAIM_FH:
2232 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2234 data->timestamp = jiffies;
2235 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2236 &data->o_arg.seq_args,
2237 &data->o_res.seq_res,
2238 task) != 0)
2239 nfs_release_seqid(data->o_arg.seqid);
2241 /* Set the create mode (note dependency on the session type) */
2242 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2243 if (data->o_arg.open_flags & O_EXCL) {
2244 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2245 if (nfs4_has_persistent_session(clp))
2246 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2247 else if (clp->cl_mvops->minor_version > 0)
2248 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2250 return;
2251 unlock_no_action:
2252 trace_nfs4_cached_open(data->state);
2253 rcu_read_unlock();
2254 out_no_action:
2255 task->tk_action = NULL;
2256 out_wait:
2257 nfs4_sequence_done(task, &data->o_res.seq_res);
2260 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2262 struct nfs4_opendata *data = calldata;
2264 data->rpc_status = task->tk_status;
2266 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2267 return;
2269 if (task->tk_status == 0) {
2270 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2271 switch (data->o_res.f_attr->mode & S_IFMT) {
2272 case S_IFREG:
2273 break;
2274 case S_IFLNK:
2275 data->rpc_status = -ELOOP;
2276 break;
2277 case S_IFDIR:
2278 data->rpc_status = -EISDIR;
2279 break;
2280 default:
2281 data->rpc_status = -ENOTDIR;
2284 renew_lease(data->o_res.server, data->timestamp);
2285 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2286 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2288 data->rpc_done = true;
2291 static void nfs4_open_release(void *calldata)
2293 struct nfs4_opendata *data = calldata;
2294 struct nfs4_state *state = NULL;
2296 /* If this request hasn't been cancelled, do nothing */
2297 if (!data->cancelled)
2298 goto out_free;
2299 /* In case of error, no cleanup! */
2300 if (data->rpc_status != 0 || !data->rpc_done)
2301 goto out_free;
2302 /* In case we need an open_confirm, no cleanup! */
2303 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2304 goto out_free;
2305 state = nfs4_opendata_to_nfs4_state(data);
2306 if (!IS_ERR(state))
2307 nfs4_close_state(state, data->o_arg.fmode);
2308 out_free:
2309 nfs4_opendata_put(data);
2312 static const struct rpc_call_ops nfs4_open_ops = {
2313 .rpc_call_prepare = nfs4_open_prepare,
2314 .rpc_call_done = nfs4_open_done,
2315 .rpc_release = nfs4_open_release,
2318 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2320 struct inode *dir = d_inode(data->dir);
2321 struct nfs_server *server = NFS_SERVER(dir);
2322 struct nfs_openargs *o_arg = &data->o_arg;
2323 struct nfs_openres *o_res = &data->o_res;
2324 struct rpc_task *task;
2325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2327 .rpc_argp = o_arg,
2328 .rpc_resp = o_res,
2329 .rpc_cred = data->owner->so_cred,
2331 struct rpc_task_setup task_setup_data = {
2332 .rpc_client = server->client,
2333 .rpc_message = &msg,
2334 .callback_ops = &nfs4_open_ops,
2335 .callback_data = data,
2336 .workqueue = nfsiod_workqueue,
2337 .flags = RPC_TASK_ASYNC,
2339 int status;
2341 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2342 kref_get(&data->kref);
2343 data->rpc_done = false;
2344 data->rpc_status = 0;
2345 data->cancelled = false;
2346 data->is_recover = false;
2347 if (isrecover) {
2348 nfs4_set_sequence_privileged(&o_arg->seq_args);
2349 data->is_recover = true;
2351 task = rpc_run_task(&task_setup_data);
2352 if (IS_ERR(task))
2353 return PTR_ERR(task);
2354 status = rpc_wait_for_completion_task(task);
2355 if (status != 0) {
2356 data->cancelled = true;
2357 smp_wmb();
2358 } else
2359 status = data->rpc_status;
2360 rpc_put_task(task);
2362 return status;
2365 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2367 struct inode *dir = d_inode(data->dir);
2368 struct nfs_openres *o_res = &data->o_res;
2369 int status;
2371 status = nfs4_run_open_task(data, 1);
2372 if (status != 0 || !data->rpc_done)
2373 return status;
2375 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2377 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2378 status = _nfs4_proc_open_confirm(data);
2380 return status;
2384 * Additional permission checks in order to distinguish between an
2385 * open for read, and an open for execute. This works around the
2386 * fact that NFSv4 OPEN treats read and execute permissions as being
2387 * the same.
2388 * Note that in the non-execute case, we want to turn off permission
2389 * checking if we just created a new file (POSIX open() semantics).
2391 static int nfs4_opendata_access(struct rpc_cred *cred,
2392 struct nfs4_opendata *opendata,
2393 struct nfs4_state *state, fmode_t fmode,
2394 int openflags)
2396 struct nfs_access_entry cache;
2397 u32 mask, flags;
2399 /* access call failed or for some reason the server doesn't
2400 * support any access modes -- defer access call until later */
2401 if (opendata->o_res.access_supported == 0)
2402 return 0;
2404 mask = 0;
2406 * Use openflags to check for exec, because fmode won't
2407 * always have FMODE_EXEC set when file open for exec.
2409 if (openflags & __FMODE_EXEC) {
2410 /* ONLY check for exec rights */
2411 if (S_ISDIR(state->inode->i_mode))
2412 mask = NFS4_ACCESS_LOOKUP;
2413 else
2414 mask = NFS4_ACCESS_EXECUTE;
2415 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2416 mask = NFS4_ACCESS_READ;
2418 cache.cred = cred;
2419 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2420 nfs_access_add_cache(state->inode, &cache);
2422 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2423 if ((mask & ~cache.mask & flags) == 0)
2424 return 0;
2426 return -EACCES;
2430 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2432 static int _nfs4_proc_open(struct nfs4_opendata *data)
2434 struct inode *dir = d_inode(data->dir);
2435 struct nfs_server *server = NFS_SERVER(dir);
2436 struct nfs_openargs *o_arg = &data->o_arg;
2437 struct nfs_openres *o_res = &data->o_res;
2438 int status;
2440 status = nfs4_run_open_task(data, 0);
2441 if (!data->rpc_done)
2442 return status;
2443 if (status != 0) {
2444 if (status == -NFS4ERR_BADNAME &&
2445 !(o_arg->open_flags & O_CREAT))
2446 return -ENOENT;
2447 return status;
2450 nfs_fattr_map_and_free_names(server, &data->f_attr);
2452 if (o_arg->open_flags & O_CREAT) {
2453 if (o_arg->open_flags & O_EXCL)
2454 data->file_created = true;
2455 else if (o_res->cinfo.before != o_res->cinfo.after)
2456 data->file_created = true;
2457 if (data->file_created || dir->i_version != o_res->cinfo.after)
2458 update_changeattr(dir, &o_res->cinfo,
2459 o_res->f_attr->time_start);
2461 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2462 server->caps &= ~NFS_CAP_POSIX_LOCK;
2463 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2464 status = _nfs4_proc_open_confirm(data);
2465 if (status != 0)
2466 return status;
2468 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2469 nfs4_sequence_free_slot(&o_res->seq_res);
2470 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2472 return 0;
2476 * OPEN_EXPIRED:
2477 * reclaim state on the server after a network partition.
2478 * Assumes caller holds the appropriate lock
2480 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2482 struct nfs4_opendata *opendata;
2483 int ret;
2485 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2486 NFS4_OPEN_CLAIM_FH);
2487 if (IS_ERR(opendata))
2488 return PTR_ERR(opendata);
2489 ret = nfs4_open_recover(opendata, state);
2490 if (ret == -ESTALE)
2491 d_drop(ctx->dentry);
2492 nfs4_opendata_put(opendata);
2493 return ret;
2496 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2498 struct nfs_server *server = NFS_SERVER(state->inode);
2499 struct nfs4_exception exception = { };
2500 int err;
2502 do {
2503 err = _nfs4_open_expired(ctx, state);
2504 trace_nfs4_open_expired(ctx, 0, err);
2505 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2506 continue;
2507 switch (err) {
2508 default:
2509 goto out;
2510 case -NFS4ERR_GRACE:
2511 case -NFS4ERR_DELAY:
2512 nfs4_handle_exception(server, err, &exception);
2513 err = 0;
2515 } while (exception.retry);
2516 out:
2517 return err;
2520 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2522 struct nfs_open_context *ctx;
2523 int ret;
2525 ctx = nfs4_state_find_open_context(state);
2526 if (IS_ERR(ctx))
2527 return -EAGAIN;
2528 ret = nfs4_do_open_expired(ctx, state);
2529 put_nfs_open_context(ctx);
2530 return ret;
2533 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2534 const nfs4_stateid *stateid)
2536 nfs_remove_bad_delegation(state->inode, stateid);
2537 write_seqlock(&state->seqlock);
2538 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2539 write_sequnlock(&state->seqlock);
2540 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2543 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2545 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2546 nfs_finish_clear_delegation_stateid(state, NULL);
2549 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2551 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2552 nfs40_clear_delegation_stateid(state);
2553 return nfs4_open_expired(sp, state);
2556 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2557 nfs4_stateid *stateid,
2558 struct rpc_cred *cred)
2560 return -NFS4ERR_BAD_STATEID;
2563 #if defined(CONFIG_NFS_V4_1)
2564 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2565 nfs4_stateid *stateid,
2566 struct rpc_cred *cred)
2568 int status;
2570 switch (stateid->type) {
2571 default:
2572 break;
2573 case NFS4_INVALID_STATEID_TYPE:
2574 case NFS4_SPECIAL_STATEID_TYPE:
2575 return -NFS4ERR_BAD_STATEID;
2576 case NFS4_REVOKED_STATEID_TYPE:
2577 goto out_free;
2580 status = nfs41_test_stateid(server, stateid, cred);
2581 switch (status) {
2582 case -NFS4ERR_EXPIRED:
2583 case -NFS4ERR_ADMIN_REVOKED:
2584 case -NFS4ERR_DELEG_REVOKED:
2585 break;
2586 default:
2587 return status;
2589 out_free:
2590 /* Ack the revoked state to the server */
2591 nfs41_free_stateid(server, stateid, cred, true);
2592 return -NFS4ERR_EXPIRED;
2595 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2597 struct nfs_server *server = NFS_SERVER(state->inode);
2598 nfs4_stateid stateid;
2599 struct nfs_delegation *delegation;
2600 struct rpc_cred *cred;
2601 int status;
2603 /* Get the delegation credential for use by test/free_stateid */
2604 rcu_read_lock();
2605 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2606 if (delegation == NULL) {
2607 rcu_read_unlock();
2608 return;
2611 nfs4_stateid_copy(&stateid, &delegation->stateid);
2612 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2613 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2614 &delegation->flags)) {
2615 rcu_read_unlock();
2616 nfs_finish_clear_delegation_stateid(state, &stateid);
2617 return;
2620 cred = get_rpccred(delegation->cred);
2621 rcu_read_unlock();
2622 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2623 trace_nfs4_test_delegation_stateid(state, NULL, status);
2624 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2625 nfs_finish_clear_delegation_stateid(state, &stateid);
2627 put_rpccred(cred);
2631 * nfs41_check_expired_locks - possibly free a lock stateid
2633 * @state: NFSv4 state for an inode
2635 * Returns NFS_OK if recovery for this stateid is now finished.
2636 * Otherwise a negative NFS4ERR value is returned.
2638 static int nfs41_check_expired_locks(struct nfs4_state *state)
2640 int status, ret = NFS_OK;
2641 struct nfs4_lock_state *lsp, *prev = NULL;
2642 struct nfs_server *server = NFS_SERVER(state->inode);
2644 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2645 goto out;
2647 spin_lock(&state->state_lock);
2648 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2649 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2650 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2652 refcount_inc(&lsp->ls_count);
2653 spin_unlock(&state->state_lock);
2655 nfs4_put_lock_state(prev);
2656 prev = lsp;
2658 status = nfs41_test_and_free_expired_stateid(server,
2659 &lsp->ls_stateid,
2660 cred);
2661 trace_nfs4_test_lock_stateid(state, lsp, status);
2662 if (status == -NFS4ERR_EXPIRED ||
2663 status == -NFS4ERR_BAD_STATEID) {
2664 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2665 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2666 if (!recover_lost_locks)
2667 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2668 } else if (status != NFS_OK) {
2669 ret = status;
2670 nfs4_put_lock_state(prev);
2671 goto out;
2673 spin_lock(&state->state_lock);
2676 spin_unlock(&state->state_lock);
2677 nfs4_put_lock_state(prev);
2678 out:
2679 return ret;
2683 * nfs41_check_open_stateid - possibly free an open stateid
2685 * @state: NFSv4 state for an inode
2687 * Returns NFS_OK if recovery for this stateid is now finished.
2688 * Otherwise a negative NFS4ERR value is returned.
2690 static int nfs41_check_open_stateid(struct nfs4_state *state)
2692 struct nfs_server *server = NFS_SERVER(state->inode);
2693 nfs4_stateid *stateid = &state->open_stateid;
2694 struct rpc_cred *cred = state->owner->so_cred;
2695 int status;
2697 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2698 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2699 if (nfs4_have_delegation(state->inode, state->state))
2700 return NFS_OK;
2701 return -NFS4ERR_OPENMODE;
2703 return -NFS4ERR_BAD_STATEID;
2705 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2706 trace_nfs4_test_open_stateid(state, NULL, status);
2707 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2708 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2709 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2710 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2711 clear_bit(NFS_OPEN_STATE, &state->flags);
2712 stateid->type = NFS4_INVALID_STATEID_TYPE;
2713 return status;
2715 if (nfs_open_stateid_recover_openmode(state))
2716 return -NFS4ERR_OPENMODE;
2717 return NFS_OK;
2720 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2722 int status;
2724 nfs41_check_delegation_stateid(state);
2725 status = nfs41_check_expired_locks(state);
2726 if (status != NFS_OK)
2727 return status;
2728 status = nfs41_check_open_stateid(state);
2729 if (status != NFS_OK)
2730 status = nfs4_open_expired(sp, state);
2731 return status;
2733 #endif
2736 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2737 * fields corresponding to attributes that were used to store the verifier.
2738 * Make sure we clobber those fields in the later setattr call
2740 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2741 struct iattr *sattr, struct nfs4_label **label)
2743 const u32 *attrset = opendata->o_res.attrset;
2745 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2746 !(sattr->ia_valid & ATTR_ATIME_SET))
2747 sattr->ia_valid |= ATTR_ATIME;
2749 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2750 !(sattr->ia_valid & ATTR_MTIME_SET))
2751 sattr->ia_valid |= ATTR_MTIME;
2753 /* Except MODE, it seems harmless of setting twice. */
2754 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2755 (attrset[1] & FATTR4_WORD1_MODE ||
2756 attrset[2] & FATTR4_WORD2_MODE_UMASK))
2757 sattr->ia_valid &= ~ATTR_MODE;
2759 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2760 *label = NULL;
2763 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2764 fmode_t fmode,
2765 int flags,
2766 struct nfs_open_context *ctx)
2768 struct nfs4_state_owner *sp = opendata->owner;
2769 struct nfs_server *server = sp->so_server;
2770 struct dentry *dentry;
2771 struct nfs4_state *state;
2772 unsigned int seq;
2773 int ret;
2775 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2777 ret = _nfs4_proc_open(opendata);
2778 if (ret != 0)
2779 goto out;
2781 state = nfs4_opendata_to_nfs4_state(opendata);
2782 ret = PTR_ERR(state);
2783 if (IS_ERR(state))
2784 goto out;
2785 ctx->state = state;
2786 if (server->caps & NFS_CAP_POSIX_LOCK)
2787 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2788 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2789 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2791 dentry = opendata->dentry;
2792 if (d_really_is_negative(dentry)) {
2793 struct dentry *alias;
2794 d_drop(dentry);
2795 alias = d_exact_alias(dentry, state->inode);
2796 if (!alias)
2797 alias = d_splice_alias(igrab(state->inode), dentry);
2798 /* d_splice_alias() can't fail here - it's a non-directory */
2799 if (alias) {
2800 dput(ctx->dentry);
2801 ctx->dentry = dentry = alias;
2803 nfs_set_verifier(dentry,
2804 nfs_save_change_attribute(d_inode(opendata->dir)));
2807 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2808 if (ret != 0)
2809 goto out;
2811 if (d_inode(dentry) == state->inode) {
2812 nfs_inode_attach_open_context(ctx);
2813 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2814 nfs4_schedule_stateid_recovery(server, state);
2816 out:
2817 return ret;
2821 * Returns a referenced nfs4_state
2823 static int _nfs4_do_open(struct inode *dir,
2824 struct nfs_open_context *ctx,
2825 int flags,
2826 const struct nfs4_open_createattrs *c,
2827 int *opened)
2829 struct nfs4_state_owner *sp;
2830 struct nfs4_state *state = NULL;
2831 struct nfs_server *server = NFS_SERVER(dir);
2832 struct nfs4_opendata *opendata;
2833 struct dentry *dentry = ctx->dentry;
2834 struct rpc_cred *cred = ctx->cred;
2835 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2836 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2837 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2838 struct iattr *sattr = c->sattr;
2839 struct nfs4_label *label = c->label;
2840 struct nfs4_label *olabel = NULL;
2841 int status;
2843 /* Protect against reboot recovery conflicts */
2844 status = -ENOMEM;
2845 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2846 if (sp == NULL) {
2847 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2848 goto out_err;
2850 status = nfs4_client_recover_expired_lease(server->nfs_client);
2851 if (status != 0)
2852 goto err_put_state_owner;
2853 if (d_really_is_positive(dentry))
2854 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2855 status = -ENOMEM;
2856 if (d_really_is_positive(dentry))
2857 claim = NFS4_OPEN_CLAIM_FH;
2858 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2859 c, claim, GFP_KERNEL);
2860 if (opendata == NULL)
2861 goto err_put_state_owner;
2863 if (label) {
2864 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2865 if (IS_ERR(olabel)) {
2866 status = PTR_ERR(olabel);
2867 goto err_opendata_put;
2871 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2872 if (!opendata->f_attr.mdsthreshold) {
2873 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2874 if (!opendata->f_attr.mdsthreshold)
2875 goto err_free_label;
2877 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2879 if (d_really_is_positive(dentry))
2880 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2882 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2883 if (status != 0)
2884 goto err_free_label;
2885 state = ctx->state;
2887 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2888 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2889 nfs4_exclusive_attrset(opendata, sattr, &label);
2891 * send create attributes which was not set by open
2892 * with an extra setattr.
2894 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2895 nfs_fattr_init(opendata->o_res.f_attr);
2896 status = nfs4_do_setattr(state->inode, cred,
2897 opendata->o_res.f_attr, sattr,
2898 ctx, label, olabel);
2899 if (status == 0) {
2900 nfs_setattr_update_inode(state->inode, sattr,
2901 opendata->o_res.f_attr);
2902 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2906 if (opened && opendata->file_created)
2907 *opened |= FILE_CREATED;
2909 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2910 *ctx_th = opendata->f_attr.mdsthreshold;
2911 opendata->f_attr.mdsthreshold = NULL;
2914 nfs4_label_free(olabel);
2916 nfs4_opendata_put(opendata);
2917 nfs4_put_state_owner(sp);
2918 return 0;
2919 err_free_label:
2920 nfs4_label_free(olabel);
2921 err_opendata_put:
2922 nfs4_opendata_put(opendata);
2923 err_put_state_owner:
2924 nfs4_put_state_owner(sp);
2925 out_err:
2926 return status;
2930 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2931 struct nfs_open_context *ctx,
2932 int flags,
2933 struct iattr *sattr,
2934 struct nfs4_label *label,
2935 int *opened)
2937 struct nfs_server *server = NFS_SERVER(dir);
2938 struct nfs4_exception exception = { };
2939 struct nfs4_state *res;
2940 struct nfs4_open_createattrs c = {
2941 .label = label,
2942 .sattr = sattr,
2943 .verf = {
2944 [0] = (__u32)jiffies,
2945 [1] = (__u32)current->pid,
2948 int status;
2950 do {
2951 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
2952 res = ctx->state;
2953 trace_nfs4_open_file(ctx, flags, status);
2954 if (status == 0)
2955 break;
2956 /* NOTE: BAD_SEQID means the server and client disagree about the
2957 * book-keeping w.r.t. state-changing operations
2958 * (OPEN/CLOSE/LOCK/LOCKU...)
2959 * It is actually a sign of a bug on the client or on the server.
2961 * If we receive a BAD_SEQID error in the particular case of
2962 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2963 * have unhashed the old state_owner for us, and that we can
2964 * therefore safely retry using a new one. We should still warn
2965 * the user though...
2967 if (status == -NFS4ERR_BAD_SEQID) {
2968 pr_warn_ratelimited("NFS: v4 server %s "
2969 " returned a bad sequence-id error!\n",
2970 NFS_SERVER(dir)->nfs_client->cl_hostname);
2971 exception.retry = 1;
2972 continue;
2975 * BAD_STATEID on OPEN means that the server cancelled our
2976 * state before it received the OPEN_CONFIRM.
2977 * Recover by retrying the request as per the discussion
2978 * on Page 181 of RFC3530.
2980 if (status == -NFS4ERR_BAD_STATEID) {
2981 exception.retry = 1;
2982 continue;
2984 if (status == -EAGAIN) {
2985 /* We must have found a delegation */
2986 exception.retry = 1;
2987 continue;
2989 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2990 continue;
2991 res = ERR_PTR(nfs4_handle_exception(server,
2992 status, &exception));
2993 } while (exception.retry);
2994 return res;
2997 static int _nfs4_do_setattr(struct inode *inode,
2998 struct nfs_setattrargs *arg,
2999 struct nfs_setattrres *res,
3000 struct rpc_cred *cred,
3001 struct nfs_open_context *ctx)
3003 struct nfs_server *server = NFS_SERVER(inode);
3004 struct rpc_message msg = {
3005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3006 .rpc_argp = arg,
3007 .rpc_resp = res,
3008 .rpc_cred = cred,
3010 struct rpc_cred *delegation_cred = NULL;
3011 unsigned long timestamp = jiffies;
3012 fmode_t fmode;
3013 bool truncate;
3014 int status;
3016 nfs_fattr_init(res->fattr);
3018 /* Servers should only apply open mode checks for file size changes */
3019 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3020 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3022 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3023 /* Use that stateid */
3024 } else if (truncate && ctx != NULL) {
3025 struct nfs_lock_context *l_ctx;
3026 if (!nfs4_valid_open_stateid(ctx->state))
3027 return -EBADF;
3028 l_ctx = nfs_get_lock_context(ctx);
3029 if (IS_ERR(l_ctx))
3030 return PTR_ERR(l_ctx);
3031 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3032 &arg->stateid, &delegation_cred);
3033 nfs_put_lock_context(l_ctx);
3034 if (status == -EIO)
3035 return -EBADF;
3036 } else
3037 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3038 if (delegation_cred)
3039 msg.rpc_cred = delegation_cred;
3041 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3043 put_rpccred(delegation_cred);
3044 if (status == 0 && ctx != NULL)
3045 renew_lease(server, timestamp);
3046 trace_nfs4_setattr(inode, &arg->stateid, status);
3047 return status;
3050 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3051 struct nfs_fattr *fattr, struct iattr *sattr,
3052 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3053 struct nfs4_label *olabel)
3055 struct nfs_server *server = NFS_SERVER(inode);
3056 struct nfs4_state *state = ctx ? ctx->state : NULL;
3057 struct nfs_setattrargs arg = {
3058 .fh = NFS_FH(inode),
3059 .iap = sattr,
3060 .server = server,
3061 .bitmask = server->attr_bitmask,
3062 .label = ilabel,
3064 struct nfs_setattrres res = {
3065 .fattr = fattr,
3066 .label = olabel,
3067 .server = server,
3069 struct nfs4_exception exception = {
3070 .state = state,
3071 .inode = inode,
3072 .stateid = &arg.stateid,
3074 int err;
3076 arg.bitmask = nfs4_bitmask(server, ilabel);
3077 if (ilabel)
3078 arg.bitmask = nfs4_bitmask(server, olabel);
3080 do {
3081 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3082 switch (err) {
3083 case -NFS4ERR_OPENMODE:
3084 if (!(sattr->ia_valid & ATTR_SIZE)) {
3085 pr_warn_once("NFSv4: server %s is incorrectly "
3086 "applying open mode checks to "
3087 "a SETATTR that is not "
3088 "changing file size.\n",
3089 server->nfs_client->cl_hostname);
3091 if (state && !(state->state & FMODE_WRITE)) {
3092 err = -EBADF;
3093 if (sattr->ia_valid & ATTR_OPEN)
3094 err = -EACCES;
3095 goto out;
3098 err = nfs4_handle_exception(server, err, &exception);
3099 } while (exception.retry);
3100 out:
3101 return err;
3104 static bool
3105 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3107 if (inode == NULL || !nfs_have_layout(inode))
3108 return false;
3110 return pnfs_wait_on_layoutreturn(inode, task);
3113 struct nfs4_closedata {
3114 struct inode *inode;
3115 struct nfs4_state *state;
3116 struct nfs_closeargs arg;
3117 struct nfs_closeres res;
3118 struct {
3119 struct nfs4_layoutreturn_args arg;
3120 struct nfs4_layoutreturn_res res;
3121 struct nfs4_xdr_opaque_data ld_private;
3122 u32 roc_barrier;
3123 bool roc;
3124 } lr;
3125 struct nfs_fattr fattr;
3126 unsigned long timestamp;
3129 static void nfs4_free_closedata(void *data)
3131 struct nfs4_closedata *calldata = data;
3132 struct nfs4_state_owner *sp = calldata->state->owner;
3133 struct super_block *sb = calldata->state->inode->i_sb;
3135 if (calldata->lr.roc)
3136 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3137 calldata->res.lr_ret);
3138 nfs4_put_open_state(calldata->state);
3139 nfs_free_seqid(calldata->arg.seqid);
3140 nfs4_put_state_owner(sp);
3141 nfs_sb_deactive(sb);
3142 kfree(calldata);
3145 static void nfs4_close_done(struct rpc_task *task, void *data)
3147 struct nfs4_closedata *calldata = data;
3148 struct nfs4_state *state = calldata->state;
3149 struct nfs_server *server = NFS_SERVER(calldata->inode);
3150 nfs4_stateid *res_stateid = NULL;
3152 dprintk("%s: begin!\n", __func__);
3153 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3154 return;
3155 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3157 /* Handle Layoutreturn errors */
3158 if (calldata->arg.lr_args && task->tk_status != 0) {
3159 switch (calldata->res.lr_ret) {
3160 default:
3161 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3162 break;
3163 case 0:
3164 calldata->arg.lr_args = NULL;
3165 calldata->res.lr_res = NULL;
3166 break;
3167 case -NFS4ERR_OLD_STATEID:
3168 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
3169 calldata->inode))
3170 goto lr_restart;
3171 /* Fallthrough */
3172 case -NFS4ERR_ADMIN_REVOKED:
3173 case -NFS4ERR_DELEG_REVOKED:
3174 case -NFS4ERR_EXPIRED:
3175 case -NFS4ERR_BAD_STATEID:
3176 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3177 case -NFS4ERR_WRONG_CRED:
3178 calldata->arg.lr_args = NULL;
3179 calldata->res.lr_res = NULL;
3180 goto lr_restart;
3184 /* hmm. we are done with the inode, and in the process of freeing
3185 * the state_owner. we keep this around to process errors
3187 switch (task->tk_status) {
3188 case 0:
3189 res_stateid = &calldata->res.stateid;
3190 renew_lease(server, calldata->timestamp);
3191 break;
3192 case -NFS4ERR_ACCESS:
3193 if (calldata->arg.bitmask != NULL) {
3194 calldata->arg.bitmask = NULL;
3195 calldata->res.fattr = NULL;
3196 goto out_restart;
3199 break;
3200 case -NFS4ERR_OLD_STATEID:
3201 /* Did we race with OPEN? */
3202 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3203 state))
3204 goto out_restart;
3205 goto out_release;
3206 case -NFS4ERR_ADMIN_REVOKED:
3207 case -NFS4ERR_STALE_STATEID:
3208 case -NFS4ERR_EXPIRED:
3209 nfs4_free_revoked_stateid(server,
3210 &calldata->arg.stateid,
3211 task->tk_msg.rpc_cred);
3212 /* Fallthrough */
3213 case -NFS4ERR_BAD_STATEID:
3214 break;
3215 default:
3216 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN)
3217 goto out_restart;
3219 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3220 res_stateid, calldata->arg.fmode);
3221 out_release:
3222 task->tk_status = 0;
3223 nfs_release_seqid(calldata->arg.seqid);
3224 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3225 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3226 return;
3227 lr_restart:
3228 calldata->res.lr_ret = 0;
3229 out_restart:
3230 task->tk_status = 0;
3231 rpc_restart_call_prepare(task);
3232 goto out_release;
3235 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3237 struct nfs4_closedata *calldata = data;
3238 struct nfs4_state *state = calldata->state;
3239 struct inode *inode = calldata->inode;
3240 bool is_rdonly, is_wronly, is_rdwr;
3241 int call_close = 0;
3243 dprintk("%s: begin!\n", __func__);
3244 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3245 goto out_wait;
3247 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3248 spin_lock(&state->owner->so_lock);
3249 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3250 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3251 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3252 /* Calculate the change in open mode */
3253 calldata->arg.fmode = 0;
3254 if (state->n_rdwr == 0) {
3255 if (state->n_rdonly == 0)
3256 call_close |= is_rdonly;
3257 else if (is_rdonly)
3258 calldata->arg.fmode |= FMODE_READ;
3259 if (state->n_wronly == 0)
3260 call_close |= is_wronly;
3261 else if (is_wronly)
3262 calldata->arg.fmode |= FMODE_WRITE;
3263 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3264 call_close |= is_rdwr;
3265 } else if (is_rdwr)
3266 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3268 if (!nfs4_valid_open_stateid(state) ||
3269 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3270 call_close = 0;
3271 spin_unlock(&state->owner->so_lock);
3273 if (!call_close) {
3274 /* Note: exit _without_ calling nfs4_close_done */
3275 goto out_no_action;
3278 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3279 nfs_release_seqid(calldata->arg.seqid);
3280 goto out_wait;
3283 if (calldata->arg.fmode == 0)
3284 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3286 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3287 /* Close-to-open cache consistency revalidation */
3288 if (!nfs4_have_delegation(inode, FMODE_READ))
3289 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3290 else
3291 calldata->arg.bitmask = NULL;
3294 calldata->arg.share_access =
3295 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3296 calldata->arg.fmode, 0);
3298 if (calldata->res.fattr == NULL)
3299 calldata->arg.bitmask = NULL;
3300 else if (calldata->arg.bitmask == NULL)
3301 calldata->res.fattr = NULL;
3302 calldata->timestamp = jiffies;
3303 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3304 &calldata->arg.seq_args,
3305 &calldata->res.seq_res,
3306 task) != 0)
3307 nfs_release_seqid(calldata->arg.seqid);
3308 dprintk("%s: done!\n", __func__);
3309 return;
3310 out_no_action:
3311 task->tk_action = NULL;
3312 out_wait:
3313 nfs4_sequence_done(task, &calldata->res.seq_res);
3316 static const struct rpc_call_ops nfs4_close_ops = {
3317 .rpc_call_prepare = nfs4_close_prepare,
3318 .rpc_call_done = nfs4_close_done,
3319 .rpc_release = nfs4_free_closedata,
3323 * It is possible for data to be read/written from a mem-mapped file
3324 * after the sys_close call (which hits the vfs layer as a flush).
3325 * This means that we can't safely call nfsv4 close on a file until
3326 * the inode is cleared. This in turn means that we are not good
3327 * NFSv4 citizens - we do not indicate to the server to update the file's
3328 * share state even when we are done with one of the three share
3329 * stateid's in the inode.
3331 * NOTE: Caller must be holding the sp->so_owner semaphore!
3333 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3335 struct nfs_server *server = NFS_SERVER(state->inode);
3336 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3337 struct nfs4_closedata *calldata;
3338 struct nfs4_state_owner *sp = state->owner;
3339 struct rpc_task *task;
3340 struct rpc_message msg = {
3341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3342 .rpc_cred = state->owner->so_cred,
3344 struct rpc_task_setup task_setup_data = {
3345 .rpc_client = server->client,
3346 .rpc_message = &msg,
3347 .callback_ops = &nfs4_close_ops,
3348 .workqueue = nfsiod_workqueue,
3349 .flags = RPC_TASK_ASYNC,
3351 int status = -ENOMEM;
3353 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3354 &task_setup_data.rpc_client, &msg);
3356 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3357 if (calldata == NULL)
3358 goto out;
3359 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3360 calldata->inode = state->inode;
3361 calldata->state = state;
3362 calldata->arg.fh = NFS_FH(state->inode);
3363 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3364 goto out_free_calldata;
3365 /* Serialization for the sequence id */
3366 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3367 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3368 if (IS_ERR(calldata->arg.seqid))
3369 goto out_free_calldata;
3370 nfs_fattr_init(&calldata->fattr);
3371 calldata->arg.fmode = 0;
3372 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3373 calldata->res.fattr = &calldata->fattr;
3374 calldata->res.seqid = calldata->arg.seqid;
3375 calldata->res.server = server;
3376 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3377 calldata->lr.roc = pnfs_roc(state->inode,
3378 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3379 if (calldata->lr.roc) {
3380 calldata->arg.lr_args = &calldata->lr.arg;
3381 calldata->res.lr_res = &calldata->lr.res;
3383 nfs_sb_active(calldata->inode->i_sb);
3385 msg.rpc_argp = &calldata->arg;
3386 msg.rpc_resp = &calldata->res;
3387 task_setup_data.callback_data = calldata;
3388 task = rpc_run_task(&task_setup_data);
3389 if (IS_ERR(task))
3390 return PTR_ERR(task);
3391 status = 0;
3392 if (wait)
3393 status = rpc_wait_for_completion_task(task);
3394 rpc_put_task(task);
3395 return status;
3396 out_free_calldata:
3397 kfree(calldata);
3398 out:
3399 nfs4_put_open_state(state);
3400 nfs4_put_state_owner(sp);
3401 return status;
3404 static struct inode *
3405 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3406 int open_flags, struct iattr *attr, int *opened)
3408 struct nfs4_state *state;
3409 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3411 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3413 /* Protect against concurrent sillydeletes */
3414 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3416 nfs4_label_release_security(label);
3418 if (IS_ERR(state))
3419 return ERR_CAST(state);
3420 return state->inode;
3423 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3425 if (ctx->state == NULL)
3426 return;
3427 if (is_sync)
3428 nfs4_close_sync(ctx->state, ctx->mode);
3429 else
3430 nfs4_close_state(ctx->state, ctx->mode);
3433 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3434 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3435 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3437 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3439 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3440 struct nfs4_server_caps_arg args = {
3441 .fhandle = fhandle,
3442 .bitmask = bitmask,
3444 struct nfs4_server_caps_res res = {};
3445 struct rpc_message msg = {
3446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3447 .rpc_argp = &args,
3448 .rpc_resp = &res,
3450 int status;
3451 int i;
3453 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3454 FATTR4_WORD0_FH_EXPIRE_TYPE |
3455 FATTR4_WORD0_LINK_SUPPORT |
3456 FATTR4_WORD0_SYMLINK_SUPPORT |
3457 FATTR4_WORD0_ACLSUPPORT;
3458 if (minorversion)
3459 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3461 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3462 if (status == 0) {
3463 /* Sanity check the server answers */
3464 switch (minorversion) {
3465 case 0:
3466 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3467 res.attr_bitmask[2] = 0;
3468 break;
3469 case 1:
3470 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3471 break;
3472 case 2:
3473 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3475 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3476 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3477 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3478 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3479 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3480 NFS_CAP_CTIME|NFS_CAP_MTIME|
3481 NFS_CAP_SECURITY_LABEL);
3482 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3483 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3484 server->caps |= NFS_CAP_ACLS;
3485 if (res.has_links != 0)
3486 server->caps |= NFS_CAP_HARDLINKS;
3487 if (res.has_symlinks != 0)
3488 server->caps |= NFS_CAP_SYMLINKS;
3489 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3490 server->caps |= NFS_CAP_FILEID;
3491 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3492 server->caps |= NFS_CAP_MODE;
3493 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3494 server->caps |= NFS_CAP_NLINK;
3495 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3496 server->caps |= NFS_CAP_OWNER;
3497 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3498 server->caps |= NFS_CAP_OWNER_GROUP;
3499 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3500 server->caps |= NFS_CAP_ATIME;
3501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3502 server->caps |= NFS_CAP_CTIME;
3503 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3504 server->caps |= NFS_CAP_MTIME;
3505 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3506 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3507 server->caps |= NFS_CAP_SECURITY_LABEL;
3508 #endif
3509 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3510 sizeof(server->attr_bitmask));
3511 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3513 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3514 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3515 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3516 server->cache_consistency_bitmask[2] = 0;
3518 /* Avoid a regression due to buggy server */
3519 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3520 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3521 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3522 sizeof(server->exclcreat_bitmask));
3524 server->acl_bitmask = res.acl_bitmask;
3525 server->fh_expire_type = res.fh_expire_type;
3528 return status;
3531 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3533 struct nfs4_exception exception = { };
3534 int err;
3535 do {
3536 err = nfs4_handle_exception(server,
3537 _nfs4_server_capabilities(server, fhandle),
3538 &exception);
3539 } while (exception.retry);
3540 return err;
3543 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3544 struct nfs_fsinfo *info)
3546 u32 bitmask[3];
3547 struct nfs4_lookup_root_arg args = {
3548 .bitmask = bitmask,
3550 struct nfs4_lookup_res res = {
3551 .server = server,
3552 .fattr = info->fattr,
3553 .fh = fhandle,
3555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3557 .rpc_argp = &args,
3558 .rpc_resp = &res,
3561 bitmask[0] = nfs4_fattr_bitmap[0];
3562 bitmask[1] = nfs4_fattr_bitmap[1];
3564 * Process the label in the upcoming getfattr
3566 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3568 nfs_fattr_init(info->fattr);
3569 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3572 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3573 struct nfs_fsinfo *info)
3575 struct nfs4_exception exception = { };
3576 int err;
3577 do {
3578 err = _nfs4_lookup_root(server, fhandle, info);
3579 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3580 switch (err) {
3581 case 0:
3582 case -NFS4ERR_WRONGSEC:
3583 goto out;
3584 default:
3585 err = nfs4_handle_exception(server, err, &exception);
3587 } while (exception.retry);
3588 out:
3589 return err;
3592 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3593 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3595 struct rpc_auth_create_args auth_args = {
3596 .pseudoflavor = flavor,
3598 struct rpc_auth *auth;
3600 auth = rpcauth_create(&auth_args, server->client);
3601 if (IS_ERR(auth))
3602 return -EACCES;
3603 return nfs4_lookup_root(server, fhandle, info);
3607 * Retry pseudoroot lookup with various security flavors. We do this when:
3609 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3610 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3612 * Returns zero on success, or a negative NFS4ERR value, or a
3613 * negative errno value.
3615 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3616 struct nfs_fsinfo *info)
3618 /* Per 3530bis 15.33.5 */
3619 static const rpc_authflavor_t flav_array[] = {
3620 RPC_AUTH_GSS_KRB5P,
3621 RPC_AUTH_GSS_KRB5I,
3622 RPC_AUTH_GSS_KRB5,
3623 RPC_AUTH_UNIX, /* courtesy */
3624 RPC_AUTH_NULL,
3626 int status = -EPERM;
3627 size_t i;
3629 if (server->auth_info.flavor_len > 0) {
3630 /* try each flavor specified by user */
3631 for (i = 0; i < server->auth_info.flavor_len; i++) {
3632 status = nfs4_lookup_root_sec(server, fhandle, info,
3633 server->auth_info.flavors[i]);
3634 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3635 continue;
3636 break;
3638 } else {
3639 /* no flavors specified by user, try default list */
3640 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3641 status = nfs4_lookup_root_sec(server, fhandle, info,
3642 flav_array[i]);
3643 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3644 continue;
3645 break;
3650 * -EACCESS could mean that the user doesn't have correct permissions
3651 * to access the mount. It could also mean that we tried to mount
3652 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3653 * existing mount programs don't handle -EACCES very well so it should
3654 * be mapped to -EPERM instead.
3656 if (status == -EACCES)
3657 status = -EPERM;
3658 return status;
3662 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3663 * @server: initialized nfs_server handle
3664 * @fhandle: we fill in the pseudo-fs root file handle
3665 * @info: we fill in an FSINFO struct
3666 * @auth_probe: probe the auth flavours
3668 * Returns zero on success, or a negative errno.
3670 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3671 struct nfs_fsinfo *info,
3672 bool auth_probe)
3674 int status = 0;
3676 if (!auth_probe)
3677 status = nfs4_lookup_root(server, fhandle, info);
3679 if (auth_probe || status == NFS4ERR_WRONGSEC)
3680 status = server->nfs_client->cl_mvops->find_root_sec(server,
3681 fhandle, info);
3683 if (status == 0)
3684 status = nfs4_server_capabilities(server, fhandle);
3685 if (status == 0)
3686 status = nfs4_do_fsinfo(server, fhandle, info);
3688 return nfs4_map_errors(status);
3691 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3692 struct nfs_fsinfo *info)
3694 int error;
3695 struct nfs_fattr *fattr = info->fattr;
3696 struct nfs4_label *label = NULL;
3698 error = nfs4_server_capabilities(server, mntfh);
3699 if (error < 0) {
3700 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3701 return error;
3704 label = nfs4_label_alloc(server, GFP_KERNEL);
3705 if (IS_ERR(label))
3706 return PTR_ERR(label);
3708 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3709 if (error < 0) {
3710 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3711 goto err_free_label;
3714 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3715 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3716 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3718 err_free_label:
3719 nfs4_label_free(label);
3721 return error;
3725 * Get locations and (maybe) other attributes of a referral.
3726 * Note that we'll actually follow the referral later when
3727 * we detect fsid mismatch in inode revalidation
3729 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3730 const struct qstr *name, struct nfs_fattr *fattr,
3731 struct nfs_fh *fhandle)
3733 int status = -ENOMEM;
3734 struct page *page = NULL;
3735 struct nfs4_fs_locations *locations = NULL;
3737 page = alloc_page(GFP_KERNEL);
3738 if (page == NULL)
3739 goto out;
3740 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3741 if (locations == NULL)
3742 goto out;
3744 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3745 if (status != 0)
3746 goto out;
3749 * If the fsid didn't change, this is a migration event, not a
3750 * referral. Cause us to drop into the exception handler, which
3751 * will kick off migration recovery.
3753 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3754 dprintk("%s: server did not return a different fsid for"
3755 " a referral at %s\n", __func__, name->name);
3756 status = -NFS4ERR_MOVED;
3757 goto out;
3759 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3760 nfs_fixup_referral_attributes(&locations->fattr);
3762 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3763 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3764 memset(fhandle, 0, sizeof(struct nfs_fh));
3765 out:
3766 if (page)
3767 __free_page(page);
3768 kfree(locations);
3769 return status;
3772 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3773 struct nfs_fattr *fattr, struct nfs4_label *label)
3775 struct nfs4_getattr_arg args = {
3776 .fh = fhandle,
3777 .bitmask = server->attr_bitmask,
3779 struct nfs4_getattr_res res = {
3780 .fattr = fattr,
3781 .label = label,
3782 .server = server,
3784 struct rpc_message msg = {
3785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3786 .rpc_argp = &args,
3787 .rpc_resp = &res,
3790 args.bitmask = nfs4_bitmask(server, label);
3792 nfs_fattr_init(fattr);
3793 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3796 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3797 struct nfs_fattr *fattr, struct nfs4_label *label)
3799 struct nfs4_exception exception = { };
3800 int err;
3801 do {
3802 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3803 trace_nfs4_getattr(server, fhandle, fattr, err);
3804 err = nfs4_handle_exception(server, err,
3805 &exception);
3806 } while (exception.retry);
3807 return err;
3811 * The file is not closed if it is opened due to the a request to change
3812 * the size of the file. The open call will not be needed once the
3813 * VFS layer lookup-intents are implemented.
3815 * Close is called when the inode is destroyed.
3816 * If we haven't opened the file for O_WRONLY, we
3817 * need to in the size_change case to obtain a stateid.
3819 * Got race?
3820 * Because OPEN is always done by name in nfsv4, it is
3821 * possible that we opened a different file by the same
3822 * name. We can recognize this race condition, but we
3823 * can't do anything about it besides returning an error.
3825 * This will be fixed with VFS changes (lookup-intent).
3827 static int
3828 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3829 struct iattr *sattr)
3831 struct inode *inode = d_inode(dentry);
3832 struct rpc_cred *cred = NULL;
3833 struct nfs_open_context *ctx = NULL;
3834 struct nfs4_label *label = NULL;
3835 int status;
3837 if (pnfs_ld_layoutret_on_setattr(inode) &&
3838 sattr->ia_valid & ATTR_SIZE &&
3839 sattr->ia_size < i_size_read(inode))
3840 pnfs_commit_and_return_layout(inode);
3842 nfs_fattr_init(fattr);
3844 /* Deal with open(O_TRUNC) */
3845 if (sattr->ia_valid & ATTR_OPEN)
3846 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3848 /* Optimization: if the end result is no change, don't RPC */
3849 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3850 return 0;
3852 /* Search for an existing open(O_WRITE) file */
3853 if (sattr->ia_valid & ATTR_FILE) {
3855 ctx = nfs_file_open_context(sattr->ia_file);
3856 if (ctx)
3857 cred = ctx->cred;
3860 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3861 if (IS_ERR(label))
3862 return PTR_ERR(label);
3864 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
3865 if (status == 0) {
3866 nfs_setattr_update_inode(inode, sattr, fattr);
3867 nfs_setsecurity(inode, fattr, label);
3869 nfs4_label_free(label);
3870 return status;
3873 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3874 const struct qstr *name, struct nfs_fh *fhandle,
3875 struct nfs_fattr *fattr, struct nfs4_label *label)
3877 struct nfs_server *server = NFS_SERVER(dir);
3878 int status;
3879 struct nfs4_lookup_arg args = {
3880 .bitmask = server->attr_bitmask,
3881 .dir_fh = NFS_FH(dir),
3882 .name = name,
3884 struct nfs4_lookup_res res = {
3885 .server = server,
3886 .fattr = fattr,
3887 .label = label,
3888 .fh = fhandle,
3890 struct rpc_message msg = {
3891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3892 .rpc_argp = &args,
3893 .rpc_resp = &res,
3896 args.bitmask = nfs4_bitmask(server, label);
3898 nfs_fattr_init(fattr);
3900 dprintk("NFS call lookup %s\n", name->name);
3901 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3902 dprintk("NFS reply lookup: %d\n", status);
3903 return status;
3906 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3908 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3909 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3910 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3911 fattr->nlink = 2;
3914 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3915 const struct qstr *name, struct nfs_fh *fhandle,
3916 struct nfs_fattr *fattr, struct nfs4_label *label)
3918 struct nfs4_exception exception = { };
3919 struct rpc_clnt *client = *clnt;
3920 int err;
3921 do {
3922 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3923 trace_nfs4_lookup(dir, name, err);
3924 switch (err) {
3925 case -NFS4ERR_BADNAME:
3926 err = -ENOENT;
3927 goto out;
3928 case -NFS4ERR_MOVED:
3929 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3930 if (err == -NFS4ERR_MOVED)
3931 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3932 goto out;
3933 case -NFS4ERR_WRONGSEC:
3934 err = -EPERM;
3935 if (client != *clnt)
3936 goto out;
3937 client = nfs4_negotiate_security(client, dir, name);
3938 if (IS_ERR(client))
3939 return PTR_ERR(client);
3941 exception.retry = 1;
3942 break;
3943 default:
3944 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3946 } while (exception.retry);
3948 out:
3949 if (err == 0)
3950 *clnt = client;
3951 else if (client != *clnt)
3952 rpc_shutdown_client(client);
3954 return err;
3957 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3958 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3959 struct nfs4_label *label)
3961 int status;
3962 struct rpc_clnt *client = NFS_CLIENT(dir);
3964 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3965 if (client != NFS_CLIENT(dir)) {
3966 rpc_shutdown_client(client);
3967 nfs_fixup_secinfo_attributes(fattr);
3969 return status;
3972 struct rpc_clnt *
3973 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3974 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3976 struct rpc_clnt *client = NFS_CLIENT(dir);
3977 int status;
3979 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3980 if (status < 0)
3981 return ERR_PTR(status);
3982 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3985 static int _nfs4_proc_lookupp(struct inode *inode,
3986 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3987 struct nfs4_label *label)
3989 struct rpc_clnt *clnt = NFS_CLIENT(inode);
3990 struct nfs_server *server = NFS_SERVER(inode);
3991 int status;
3992 struct nfs4_lookupp_arg args = {
3993 .bitmask = server->attr_bitmask,
3994 .fh = NFS_FH(inode),
3996 struct nfs4_lookupp_res res = {
3997 .server = server,
3998 .fattr = fattr,
3999 .label = label,
4000 .fh = fhandle,
4002 struct rpc_message msg = {
4003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4004 .rpc_argp = &args,
4005 .rpc_resp = &res,
4008 args.bitmask = nfs4_bitmask(server, label);
4010 nfs_fattr_init(fattr);
4012 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4013 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4014 &res.seq_res, 0);
4015 dprintk("NFS reply lookupp: %d\n", status);
4016 return status;
4019 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4020 struct nfs_fattr *fattr, struct nfs4_label *label)
4022 struct nfs4_exception exception = { };
4023 int err;
4024 do {
4025 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4026 trace_nfs4_lookupp(inode, err);
4027 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4028 &exception);
4029 } while (exception.retry);
4030 return err;
4033 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4035 struct nfs_server *server = NFS_SERVER(inode);
4036 struct nfs4_accessargs args = {
4037 .fh = NFS_FH(inode),
4038 .bitmask = server->cache_consistency_bitmask,
4039 .access = entry->mask,
4041 struct nfs4_accessres res = {
4042 .server = server,
4044 struct rpc_message msg = {
4045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4046 .rpc_argp = &args,
4047 .rpc_resp = &res,
4048 .rpc_cred = entry->cred,
4050 int status = 0;
4052 res.fattr = nfs_alloc_fattr();
4053 if (res.fattr == NULL)
4054 return -ENOMEM;
4056 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4057 if (!status) {
4058 nfs_access_set_mask(entry, res.access);
4059 nfs_refresh_inode(inode, res.fattr);
4061 nfs_free_fattr(res.fattr);
4062 return status;
4065 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4067 struct nfs4_exception exception = { };
4068 int err;
4069 do {
4070 err = _nfs4_proc_access(inode, entry);
4071 trace_nfs4_access(inode, err);
4072 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4073 &exception);
4074 } while (exception.retry);
4075 return err;
4079 * TODO: For the time being, we don't try to get any attributes
4080 * along with any of the zero-copy operations READ, READDIR,
4081 * READLINK, WRITE.
4083 * In the case of the first three, we want to put the GETATTR
4084 * after the read-type operation -- this is because it is hard
4085 * to predict the length of a GETATTR response in v4, and thus
4086 * align the READ data correctly. This means that the GETATTR
4087 * may end up partially falling into the page cache, and we should
4088 * shift it into the 'tail' of the xdr_buf before processing.
4089 * To do this efficiently, we need to know the total length
4090 * of data received, which doesn't seem to be available outside
4091 * of the RPC layer.
4093 * In the case of WRITE, we also want to put the GETATTR after
4094 * the operation -- in this case because we want to make sure
4095 * we get the post-operation mtime and size.
4097 * Both of these changes to the XDR layer would in fact be quite
4098 * minor, but I decided to leave them for a subsequent patch.
4100 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4101 unsigned int pgbase, unsigned int pglen)
4103 struct nfs4_readlink args = {
4104 .fh = NFS_FH(inode),
4105 .pgbase = pgbase,
4106 .pglen = pglen,
4107 .pages = &page,
4109 struct nfs4_readlink_res res;
4110 struct rpc_message msg = {
4111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4112 .rpc_argp = &args,
4113 .rpc_resp = &res,
4116 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4119 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4120 unsigned int pgbase, unsigned int pglen)
4122 struct nfs4_exception exception = { };
4123 int err;
4124 do {
4125 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4126 trace_nfs4_readlink(inode, err);
4127 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4128 &exception);
4129 } while (exception.retry);
4130 return err;
4134 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4136 static int
4137 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4138 int flags)
4140 struct nfs_server *server = NFS_SERVER(dir);
4141 struct nfs4_label l, *ilabel = NULL;
4142 struct nfs_open_context *ctx;
4143 struct nfs4_state *state;
4144 int status = 0;
4146 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4147 if (IS_ERR(ctx))
4148 return PTR_ERR(ctx);
4150 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4152 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4153 sattr->ia_mode &= ~current_umask();
4154 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4155 if (IS_ERR(state)) {
4156 status = PTR_ERR(state);
4157 goto out;
4159 out:
4160 nfs4_label_release_security(ilabel);
4161 put_nfs_open_context(ctx);
4162 return status;
4165 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4167 struct nfs_server *server = NFS_SERVER(dir);
4168 struct nfs_removeargs args = {
4169 .fh = NFS_FH(dir),
4170 .name = *name,
4172 struct nfs_removeres res = {
4173 .server = server,
4175 struct rpc_message msg = {
4176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4177 .rpc_argp = &args,
4178 .rpc_resp = &res,
4180 unsigned long timestamp = jiffies;
4181 int status;
4183 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4184 if (status == 0)
4185 update_changeattr(dir, &res.cinfo, timestamp);
4186 return status;
4189 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4191 struct nfs4_exception exception = { };
4192 int err;
4193 do {
4194 err = _nfs4_proc_remove(dir, name);
4195 trace_nfs4_remove(dir, name, err);
4196 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4197 &exception);
4198 } while (exception.retry);
4199 return err;
4202 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4204 struct nfs_server *server = NFS_SERVER(dir);
4205 struct nfs_removeargs *args = msg->rpc_argp;
4206 struct nfs_removeres *res = msg->rpc_resp;
4208 res->server = server;
4209 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4210 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4212 nfs_fattr_init(res->dir_attr);
4215 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4217 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4218 &data->args.seq_args,
4219 &data->res.seq_res,
4220 task);
4223 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4225 struct nfs_unlinkdata *data = task->tk_calldata;
4226 struct nfs_removeres *res = &data->res;
4228 if (!nfs4_sequence_done(task, &res->seq_res))
4229 return 0;
4230 if (nfs4_async_handle_error(task, res->server, NULL,
4231 &data->timeout) == -EAGAIN)
4232 return 0;
4233 if (task->tk_status == 0)
4234 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
4235 return 1;
4238 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4240 struct nfs_server *server = NFS_SERVER(dir);
4241 struct nfs_renameargs *arg = msg->rpc_argp;
4242 struct nfs_renameres *res = msg->rpc_resp;
4244 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4245 res->server = server;
4246 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4249 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4251 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4252 &data->args.seq_args,
4253 &data->res.seq_res,
4254 task);
4257 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4258 struct inode *new_dir)
4260 struct nfs_renamedata *data = task->tk_calldata;
4261 struct nfs_renameres *res = &data->res;
4263 if (!nfs4_sequence_done(task, &res->seq_res))
4264 return 0;
4265 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4266 return 0;
4268 if (task->tk_status == 0) {
4269 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
4270 if (new_dir != old_dir)
4271 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
4273 return 1;
4276 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4278 struct nfs_server *server = NFS_SERVER(inode);
4279 struct nfs4_link_arg arg = {
4280 .fh = NFS_FH(inode),
4281 .dir_fh = NFS_FH(dir),
4282 .name = name,
4283 .bitmask = server->attr_bitmask,
4285 struct nfs4_link_res res = {
4286 .server = server,
4287 .label = NULL,
4289 struct rpc_message msg = {
4290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4291 .rpc_argp = &arg,
4292 .rpc_resp = &res,
4294 int status = -ENOMEM;
4296 res.fattr = nfs_alloc_fattr();
4297 if (res.fattr == NULL)
4298 goto out;
4300 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4301 if (IS_ERR(res.label)) {
4302 status = PTR_ERR(res.label);
4303 goto out;
4305 arg.bitmask = nfs4_bitmask(server, res.label);
4307 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4308 if (!status) {
4309 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
4310 status = nfs_post_op_update_inode(inode, res.fattr);
4311 if (!status)
4312 nfs_setsecurity(inode, res.fattr, res.label);
4316 nfs4_label_free(res.label);
4318 out:
4319 nfs_free_fattr(res.fattr);
4320 return status;
4323 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4325 struct nfs4_exception exception = { };
4326 int err;
4327 do {
4328 err = nfs4_handle_exception(NFS_SERVER(inode),
4329 _nfs4_proc_link(inode, dir, name),
4330 &exception);
4331 } while (exception.retry);
4332 return err;
4335 struct nfs4_createdata {
4336 struct rpc_message msg;
4337 struct nfs4_create_arg arg;
4338 struct nfs4_create_res res;
4339 struct nfs_fh fh;
4340 struct nfs_fattr fattr;
4341 struct nfs4_label *label;
4344 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4345 const struct qstr *name, struct iattr *sattr, u32 ftype)
4347 struct nfs4_createdata *data;
4349 data = kzalloc(sizeof(*data), GFP_KERNEL);
4350 if (data != NULL) {
4351 struct nfs_server *server = NFS_SERVER(dir);
4353 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4354 if (IS_ERR(data->label))
4355 goto out_free;
4357 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4358 data->msg.rpc_argp = &data->arg;
4359 data->msg.rpc_resp = &data->res;
4360 data->arg.dir_fh = NFS_FH(dir);
4361 data->arg.server = server;
4362 data->arg.name = name;
4363 data->arg.attrs = sattr;
4364 data->arg.ftype = ftype;
4365 data->arg.bitmask = nfs4_bitmask(server, data->label);
4366 data->arg.umask = current_umask();
4367 data->res.server = server;
4368 data->res.fh = &data->fh;
4369 data->res.fattr = &data->fattr;
4370 data->res.label = data->label;
4371 nfs_fattr_init(data->res.fattr);
4373 return data;
4374 out_free:
4375 kfree(data);
4376 return NULL;
4379 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4381 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4382 &data->arg.seq_args, &data->res.seq_res, 1);
4383 if (status == 0) {
4384 update_changeattr(dir, &data->res.dir_cinfo,
4385 data->res.fattr->time_start);
4386 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4388 return status;
4391 static void nfs4_free_createdata(struct nfs4_createdata *data)
4393 nfs4_label_free(data->label);
4394 kfree(data);
4397 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4398 struct page *page, unsigned int len, struct iattr *sattr,
4399 struct nfs4_label *label)
4401 struct nfs4_createdata *data;
4402 int status = -ENAMETOOLONG;
4404 if (len > NFS4_MAXPATHLEN)
4405 goto out;
4407 status = -ENOMEM;
4408 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4409 if (data == NULL)
4410 goto out;
4412 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4413 data->arg.u.symlink.pages = &page;
4414 data->arg.u.symlink.len = len;
4415 data->arg.label = label;
4417 status = nfs4_do_create(dir, dentry, data);
4419 nfs4_free_createdata(data);
4420 out:
4421 return status;
4424 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4425 struct page *page, unsigned int len, struct iattr *sattr)
4427 struct nfs4_exception exception = { };
4428 struct nfs4_label l, *label = NULL;
4429 int err;
4431 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4433 do {
4434 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4435 trace_nfs4_symlink(dir, &dentry->d_name, err);
4436 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4437 &exception);
4438 } while (exception.retry);
4440 nfs4_label_release_security(label);
4441 return err;
4444 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4445 struct iattr *sattr, struct nfs4_label *label)
4447 struct nfs4_createdata *data;
4448 int status = -ENOMEM;
4450 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4451 if (data == NULL)
4452 goto out;
4454 data->arg.label = label;
4455 status = nfs4_do_create(dir, dentry, data);
4457 nfs4_free_createdata(data);
4458 out:
4459 return status;
4462 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4463 struct iattr *sattr)
4465 struct nfs_server *server = NFS_SERVER(dir);
4466 struct nfs4_exception exception = { };
4467 struct nfs4_label l, *label = NULL;
4468 int err;
4470 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4472 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4473 sattr->ia_mode &= ~current_umask();
4474 do {
4475 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4476 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4477 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4478 &exception);
4479 } while (exception.retry);
4480 nfs4_label_release_security(label);
4482 return err;
4485 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4486 u64 cookie, struct page **pages, unsigned int count, bool plus)
4488 struct inode *dir = d_inode(dentry);
4489 struct nfs4_readdir_arg args = {
4490 .fh = NFS_FH(dir),
4491 .pages = pages,
4492 .pgbase = 0,
4493 .count = count,
4494 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4495 .plus = plus,
4497 struct nfs4_readdir_res res;
4498 struct rpc_message msg = {
4499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4500 .rpc_argp = &args,
4501 .rpc_resp = &res,
4502 .rpc_cred = cred,
4504 int status;
4506 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4507 dentry,
4508 (unsigned long long)cookie);
4509 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4510 res.pgbase = args.pgbase;
4511 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4512 if (status >= 0) {
4513 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4514 status += args.pgbase;
4517 nfs_invalidate_atime(dir);
4519 dprintk("%s: returns %d\n", __func__, status);
4520 return status;
4523 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4524 u64 cookie, struct page **pages, unsigned int count, bool plus)
4526 struct nfs4_exception exception = { };
4527 int err;
4528 do {
4529 err = _nfs4_proc_readdir(dentry, cred, cookie,
4530 pages, count, plus);
4531 trace_nfs4_readdir(d_inode(dentry), err);
4532 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4533 &exception);
4534 } while (exception.retry);
4535 return err;
4538 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4539 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4541 struct nfs4_createdata *data;
4542 int mode = sattr->ia_mode;
4543 int status = -ENOMEM;
4545 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4546 if (data == NULL)
4547 goto out;
4549 if (S_ISFIFO(mode))
4550 data->arg.ftype = NF4FIFO;
4551 else if (S_ISBLK(mode)) {
4552 data->arg.ftype = NF4BLK;
4553 data->arg.u.device.specdata1 = MAJOR(rdev);
4554 data->arg.u.device.specdata2 = MINOR(rdev);
4556 else if (S_ISCHR(mode)) {
4557 data->arg.ftype = NF4CHR;
4558 data->arg.u.device.specdata1 = MAJOR(rdev);
4559 data->arg.u.device.specdata2 = MINOR(rdev);
4560 } else if (!S_ISSOCK(mode)) {
4561 status = -EINVAL;
4562 goto out_free;
4565 data->arg.label = label;
4566 status = nfs4_do_create(dir, dentry, data);
4567 out_free:
4568 nfs4_free_createdata(data);
4569 out:
4570 return status;
4573 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4574 struct iattr *sattr, dev_t rdev)
4576 struct nfs_server *server = NFS_SERVER(dir);
4577 struct nfs4_exception exception = { };
4578 struct nfs4_label l, *label = NULL;
4579 int err;
4581 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4583 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4584 sattr->ia_mode &= ~current_umask();
4585 do {
4586 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4587 trace_nfs4_mknod(dir, &dentry->d_name, err);
4588 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4589 &exception);
4590 } while (exception.retry);
4592 nfs4_label_release_security(label);
4594 return err;
4597 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4598 struct nfs_fsstat *fsstat)
4600 struct nfs4_statfs_arg args = {
4601 .fh = fhandle,
4602 .bitmask = server->attr_bitmask,
4604 struct nfs4_statfs_res res = {
4605 .fsstat = fsstat,
4607 struct rpc_message msg = {
4608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4609 .rpc_argp = &args,
4610 .rpc_resp = &res,
4613 nfs_fattr_init(fsstat->fattr);
4614 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4617 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4619 struct nfs4_exception exception = { };
4620 int err;
4621 do {
4622 err = nfs4_handle_exception(server,
4623 _nfs4_proc_statfs(server, fhandle, fsstat),
4624 &exception);
4625 } while (exception.retry);
4626 return err;
4629 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4630 struct nfs_fsinfo *fsinfo)
4632 struct nfs4_fsinfo_arg args = {
4633 .fh = fhandle,
4634 .bitmask = server->attr_bitmask,
4636 struct nfs4_fsinfo_res res = {
4637 .fsinfo = fsinfo,
4639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4641 .rpc_argp = &args,
4642 .rpc_resp = &res,
4645 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4648 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4650 struct nfs4_exception exception = { };
4651 unsigned long now = jiffies;
4652 int err;
4654 do {
4655 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4656 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4657 if (err == 0) {
4658 nfs4_set_lease_period(server->nfs_client,
4659 fsinfo->lease_time * HZ,
4660 now);
4661 break;
4663 err = nfs4_handle_exception(server, err, &exception);
4664 } while (exception.retry);
4665 return err;
4668 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4670 int error;
4672 nfs_fattr_init(fsinfo->fattr);
4673 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4674 if (error == 0) {
4675 /* block layout checks this! */
4676 server->pnfs_blksize = fsinfo->blksize;
4677 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4680 return error;
4683 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4684 struct nfs_pathconf *pathconf)
4686 struct nfs4_pathconf_arg args = {
4687 .fh = fhandle,
4688 .bitmask = server->attr_bitmask,
4690 struct nfs4_pathconf_res res = {
4691 .pathconf = pathconf,
4693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4695 .rpc_argp = &args,
4696 .rpc_resp = &res,
4699 /* None of the pathconf attributes are mandatory to implement */
4700 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4701 memset(pathconf, 0, sizeof(*pathconf));
4702 return 0;
4705 nfs_fattr_init(pathconf->fattr);
4706 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4709 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4710 struct nfs_pathconf *pathconf)
4712 struct nfs4_exception exception = { };
4713 int err;
4715 do {
4716 err = nfs4_handle_exception(server,
4717 _nfs4_proc_pathconf(server, fhandle, pathconf),
4718 &exception);
4719 } while (exception.retry);
4720 return err;
4723 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4724 const struct nfs_open_context *ctx,
4725 const struct nfs_lock_context *l_ctx,
4726 fmode_t fmode)
4728 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4730 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4732 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4733 const struct nfs_open_context *ctx,
4734 const struct nfs_lock_context *l_ctx,
4735 fmode_t fmode)
4737 nfs4_stateid current_stateid;
4739 /* If the current stateid represents a lost lock, then exit */
4740 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4741 return true;
4742 return nfs4_stateid_match(stateid, &current_stateid);
4745 static bool nfs4_error_stateid_expired(int err)
4747 switch (err) {
4748 case -NFS4ERR_DELEG_REVOKED:
4749 case -NFS4ERR_ADMIN_REVOKED:
4750 case -NFS4ERR_BAD_STATEID:
4751 case -NFS4ERR_STALE_STATEID:
4752 case -NFS4ERR_OLD_STATEID:
4753 case -NFS4ERR_OPENMODE:
4754 case -NFS4ERR_EXPIRED:
4755 return true;
4757 return false;
4760 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4762 struct nfs_server *server = NFS_SERVER(hdr->inode);
4764 trace_nfs4_read(hdr, task->tk_status);
4765 if (task->tk_status < 0) {
4766 struct nfs4_exception exception = {
4767 .inode = hdr->inode,
4768 .state = hdr->args.context->state,
4769 .stateid = &hdr->args.stateid,
4771 task->tk_status = nfs4_async_handle_exception(task,
4772 server, task->tk_status, &exception);
4773 if (exception.retry) {
4774 rpc_restart_call_prepare(task);
4775 return -EAGAIN;
4779 if (task->tk_status > 0)
4780 renew_lease(server, hdr->timestamp);
4781 return 0;
4784 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4785 struct nfs_pgio_args *args)
4788 if (!nfs4_error_stateid_expired(task->tk_status) ||
4789 nfs4_stateid_is_current(&args->stateid,
4790 args->context,
4791 args->lock_context,
4792 FMODE_READ))
4793 return false;
4794 rpc_restart_call_prepare(task);
4795 return true;
4798 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4801 dprintk("--> %s\n", __func__);
4803 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4804 return -EAGAIN;
4805 if (nfs4_read_stateid_changed(task, &hdr->args))
4806 return -EAGAIN;
4807 if (task->tk_status > 0)
4808 nfs_invalidate_atime(hdr->inode);
4809 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4810 nfs4_read_done_cb(task, hdr);
4813 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4814 struct rpc_message *msg)
4816 hdr->timestamp = jiffies;
4817 if (!hdr->pgio_done_cb)
4818 hdr->pgio_done_cb = nfs4_read_done_cb;
4819 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4820 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4823 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4824 struct nfs_pgio_header *hdr)
4826 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
4827 &hdr->args.seq_args,
4828 &hdr->res.seq_res,
4829 task))
4830 return 0;
4831 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4832 hdr->args.lock_context,
4833 hdr->rw_mode) == -EIO)
4834 return -EIO;
4835 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4836 return -EIO;
4837 return 0;
4840 static int nfs4_write_done_cb(struct rpc_task *task,
4841 struct nfs_pgio_header *hdr)
4843 struct inode *inode = hdr->inode;
4845 trace_nfs4_write(hdr, task->tk_status);
4846 if (task->tk_status < 0) {
4847 struct nfs4_exception exception = {
4848 .inode = hdr->inode,
4849 .state = hdr->args.context->state,
4850 .stateid = &hdr->args.stateid,
4852 task->tk_status = nfs4_async_handle_exception(task,
4853 NFS_SERVER(inode), task->tk_status,
4854 &exception);
4855 if (exception.retry) {
4856 rpc_restart_call_prepare(task);
4857 return -EAGAIN;
4860 if (task->tk_status >= 0) {
4861 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4862 nfs_writeback_update_inode(hdr);
4864 return 0;
4867 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4868 struct nfs_pgio_args *args)
4871 if (!nfs4_error_stateid_expired(task->tk_status) ||
4872 nfs4_stateid_is_current(&args->stateid,
4873 args->context,
4874 args->lock_context,
4875 FMODE_WRITE))
4876 return false;
4877 rpc_restart_call_prepare(task);
4878 return true;
4881 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4883 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4884 return -EAGAIN;
4885 if (nfs4_write_stateid_changed(task, &hdr->args))
4886 return -EAGAIN;
4887 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4888 nfs4_write_done_cb(task, hdr);
4891 static
4892 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4894 /* Don't request attributes for pNFS or O_DIRECT writes */
4895 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4896 return false;
4897 /* Otherwise, request attributes if and only if we don't hold
4898 * a delegation
4900 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4903 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4904 struct rpc_message *msg)
4906 struct nfs_server *server = NFS_SERVER(hdr->inode);
4908 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4909 hdr->args.bitmask = NULL;
4910 hdr->res.fattr = NULL;
4911 } else
4912 hdr->args.bitmask = server->cache_consistency_bitmask;
4914 if (!hdr->pgio_done_cb)
4915 hdr->pgio_done_cb = nfs4_write_done_cb;
4916 hdr->res.server = server;
4917 hdr->timestamp = jiffies;
4919 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4920 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4923 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4925 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
4926 &data->args.seq_args,
4927 &data->res.seq_res,
4928 task);
4931 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4933 struct inode *inode = data->inode;
4935 trace_nfs4_commit(data, task->tk_status);
4936 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4937 NULL, NULL) == -EAGAIN) {
4938 rpc_restart_call_prepare(task);
4939 return -EAGAIN;
4941 return 0;
4944 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4946 if (!nfs4_sequence_done(task, &data->res.seq_res))
4947 return -EAGAIN;
4948 return data->commit_done_cb(task, data);
4951 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4953 struct nfs_server *server = NFS_SERVER(data->inode);
4955 if (data->commit_done_cb == NULL)
4956 data->commit_done_cb = nfs4_commit_done_cb;
4957 data->res.server = server;
4958 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4959 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4962 struct nfs4_renewdata {
4963 struct nfs_client *client;
4964 unsigned long timestamp;
4968 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4969 * standalone procedure for queueing an asynchronous RENEW.
4971 static void nfs4_renew_release(void *calldata)
4973 struct nfs4_renewdata *data = calldata;
4974 struct nfs_client *clp = data->client;
4976 if (refcount_read(&clp->cl_count) > 1)
4977 nfs4_schedule_state_renewal(clp);
4978 nfs_put_client(clp);
4979 kfree(data);
4982 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4984 struct nfs4_renewdata *data = calldata;
4985 struct nfs_client *clp = data->client;
4986 unsigned long timestamp = data->timestamp;
4988 trace_nfs4_renew_async(clp, task->tk_status);
4989 switch (task->tk_status) {
4990 case 0:
4991 break;
4992 case -NFS4ERR_LEASE_MOVED:
4993 nfs4_schedule_lease_moved_recovery(clp);
4994 break;
4995 default:
4996 /* Unless we're shutting down, schedule state recovery! */
4997 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4998 return;
4999 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5000 nfs4_schedule_lease_recovery(clp);
5001 return;
5003 nfs4_schedule_path_down_recovery(clp);
5005 do_renew_lease(clp, timestamp);
5008 static const struct rpc_call_ops nfs4_renew_ops = {
5009 .rpc_call_done = nfs4_renew_done,
5010 .rpc_release = nfs4_renew_release,
5013 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5015 struct rpc_message msg = {
5016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5017 .rpc_argp = clp,
5018 .rpc_cred = cred,
5020 struct nfs4_renewdata *data;
5022 if (renew_flags == 0)
5023 return 0;
5024 if (!refcount_inc_not_zero(&clp->cl_count))
5025 return -EIO;
5026 data = kmalloc(sizeof(*data), GFP_NOFS);
5027 if (data == NULL) {
5028 nfs_put_client(clp);
5029 return -ENOMEM;
5031 data->client = clp;
5032 data->timestamp = jiffies;
5033 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5034 &nfs4_renew_ops, data);
5037 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
5039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5041 .rpc_argp = clp,
5042 .rpc_cred = cred,
5044 unsigned long now = jiffies;
5045 int status;
5047 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5048 if (status < 0)
5049 return status;
5050 do_renew_lease(clp, now);
5051 return 0;
5054 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5056 return server->caps & NFS_CAP_ACLS;
5059 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5060 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5061 * the stack.
5063 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5065 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5066 struct page **pages)
5068 struct page *newpage, **spages;
5069 int rc = 0;
5070 size_t len;
5071 spages = pages;
5073 do {
5074 len = min_t(size_t, PAGE_SIZE, buflen);
5075 newpage = alloc_page(GFP_KERNEL);
5077 if (newpage == NULL)
5078 goto unwind;
5079 memcpy(page_address(newpage), buf, len);
5080 buf += len;
5081 buflen -= len;
5082 *pages++ = newpage;
5083 rc++;
5084 } while (buflen != 0);
5086 return rc;
5088 unwind:
5089 for(; rc > 0; rc--)
5090 __free_page(spages[rc-1]);
5091 return -ENOMEM;
5094 struct nfs4_cached_acl {
5095 int cached;
5096 size_t len;
5097 char data[0];
5100 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5102 struct nfs_inode *nfsi = NFS_I(inode);
5104 spin_lock(&inode->i_lock);
5105 kfree(nfsi->nfs4_acl);
5106 nfsi->nfs4_acl = acl;
5107 spin_unlock(&inode->i_lock);
5110 static void nfs4_zap_acl_attr(struct inode *inode)
5112 nfs4_set_cached_acl(inode, NULL);
5115 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5117 struct nfs_inode *nfsi = NFS_I(inode);
5118 struct nfs4_cached_acl *acl;
5119 int ret = -ENOENT;
5121 spin_lock(&inode->i_lock);
5122 acl = nfsi->nfs4_acl;
5123 if (acl == NULL)
5124 goto out;
5125 if (buf == NULL) /* user is just asking for length */
5126 goto out_len;
5127 if (acl->cached == 0)
5128 goto out;
5129 ret = -ERANGE; /* see getxattr(2) man page */
5130 if (acl->len > buflen)
5131 goto out;
5132 memcpy(buf, acl->data, acl->len);
5133 out_len:
5134 ret = acl->len;
5135 out:
5136 spin_unlock(&inode->i_lock);
5137 return ret;
5140 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5142 struct nfs4_cached_acl *acl;
5143 size_t buflen = sizeof(*acl) + acl_len;
5145 if (buflen <= PAGE_SIZE) {
5146 acl = kmalloc(buflen, GFP_KERNEL);
5147 if (acl == NULL)
5148 goto out;
5149 acl->cached = 1;
5150 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5151 } else {
5152 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5153 if (acl == NULL)
5154 goto out;
5155 acl->cached = 0;
5157 acl->len = acl_len;
5158 out:
5159 nfs4_set_cached_acl(inode, acl);
5163 * The getxattr API returns the required buffer length when called with a
5164 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5165 * the required buf. On a NULL buf, we send a page of data to the server
5166 * guessing that the ACL request can be serviced by a page. If so, we cache
5167 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5168 * the cache. If not so, we throw away the page, and cache the required
5169 * length. The next getxattr call will then produce another round trip to
5170 * the server, this time with the input buf of the required size.
5172 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5174 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5175 struct nfs_getaclargs args = {
5176 .fh = NFS_FH(inode),
5177 .acl_pages = pages,
5178 .acl_len = buflen,
5180 struct nfs_getaclres res = {
5181 .acl_len = buflen,
5183 struct rpc_message msg = {
5184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5185 .rpc_argp = &args,
5186 .rpc_resp = &res,
5188 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5189 int ret = -ENOMEM, i;
5191 if (npages > ARRAY_SIZE(pages))
5192 return -ERANGE;
5194 for (i = 0; i < npages; i++) {
5195 pages[i] = alloc_page(GFP_KERNEL);
5196 if (!pages[i])
5197 goto out_free;
5200 /* for decoding across pages */
5201 res.acl_scratch = alloc_page(GFP_KERNEL);
5202 if (!res.acl_scratch)
5203 goto out_free;
5205 args.acl_len = npages * PAGE_SIZE;
5207 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5208 __func__, buf, buflen, npages, args.acl_len);
5209 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5210 &msg, &args.seq_args, &res.seq_res, 0);
5211 if (ret)
5212 goto out_free;
5214 /* Handle the case where the passed-in buffer is too short */
5215 if (res.acl_flags & NFS4_ACL_TRUNC) {
5216 /* Did the user only issue a request for the acl length? */
5217 if (buf == NULL)
5218 goto out_ok;
5219 ret = -ERANGE;
5220 goto out_free;
5222 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5223 if (buf) {
5224 if (res.acl_len > buflen) {
5225 ret = -ERANGE;
5226 goto out_free;
5228 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5230 out_ok:
5231 ret = res.acl_len;
5232 out_free:
5233 for (i = 0; i < npages; i++)
5234 if (pages[i])
5235 __free_page(pages[i]);
5236 if (res.acl_scratch)
5237 __free_page(res.acl_scratch);
5238 return ret;
5241 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5243 struct nfs4_exception exception = { };
5244 ssize_t ret;
5245 do {
5246 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5247 trace_nfs4_get_acl(inode, ret);
5248 if (ret >= 0)
5249 break;
5250 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5251 } while (exception.retry);
5252 return ret;
5255 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5257 struct nfs_server *server = NFS_SERVER(inode);
5258 int ret;
5260 if (!nfs4_server_supports_acls(server))
5261 return -EOPNOTSUPP;
5262 ret = nfs_revalidate_inode(server, inode);
5263 if (ret < 0)
5264 return ret;
5265 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5266 nfs_zap_acl_cache(inode);
5267 ret = nfs4_read_cached_acl(inode, buf, buflen);
5268 if (ret != -ENOENT)
5269 /* -ENOENT is returned if there is no ACL or if there is an ACL
5270 * but no cached acl data, just the acl length */
5271 return ret;
5272 return nfs4_get_acl_uncached(inode, buf, buflen);
5275 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5277 struct nfs_server *server = NFS_SERVER(inode);
5278 struct page *pages[NFS4ACL_MAXPAGES];
5279 struct nfs_setaclargs arg = {
5280 .fh = NFS_FH(inode),
5281 .acl_pages = pages,
5282 .acl_len = buflen,
5284 struct nfs_setaclres res;
5285 struct rpc_message msg = {
5286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5287 .rpc_argp = &arg,
5288 .rpc_resp = &res,
5290 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5291 int ret, i;
5293 if (!nfs4_server_supports_acls(server))
5294 return -EOPNOTSUPP;
5295 if (npages > ARRAY_SIZE(pages))
5296 return -ERANGE;
5297 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5298 if (i < 0)
5299 return i;
5300 nfs4_inode_return_delegation(inode);
5301 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5304 * Free each page after tx, so the only ref left is
5305 * held by the network stack
5307 for (; i > 0; i--)
5308 put_page(pages[i-1]);
5311 * Acl update can result in inode attribute update.
5312 * so mark the attribute cache invalid.
5314 spin_lock(&inode->i_lock);
5315 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5316 spin_unlock(&inode->i_lock);
5317 nfs_access_zap_cache(inode);
5318 nfs_zap_acl_cache(inode);
5319 return ret;
5322 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5324 struct nfs4_exception exception = { };
5325 int err;
5326 do {
5327 err = __nfs4_proc_set_acl(inode, buf, buflen);
5328 trace_nfs4_set_acl(inode, err);
5329 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5330 &exception);
5331 } while (exception.retry);
5332 return err;
5335 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5336 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5337 size_t buflen)
5339 struct nfs_server *server = NFS_SERVER(inode);
5340 struct nfs_fattr fattr;
5341 struct nfs4_label label = {0, 0, buflen, buf};
5343 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5344 struct nfs4_getattr_arg arg = {
5345 .fh = NFS_FH(inode),
5346 .bitmask = bitmask,
5348 struct nfs4_getattr_res res = {
5349 .fattr = &fattr,
5350 .label = &label,
5351 .server = server,
5353 struct rpc_message msg = {
5354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5355 .rpc_argp = &arg,
5356 .rpc_resp = &res,
5358 int ret;
5360 nfs_fattr_init(&fattr);
5362 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5363 if (ret)
5364 return ret;
5365 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5366 return -ENOENT;
5367 if (buflen < label.len)
5368 return -ERANGE;
5369 return 0;
5372 static int nfs4_get_security_label(struct inode *inode, void *buf,
5373 size_t buflen)
5375 struct nfs4_exception exception = { };
5376 int err;
5378 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5379 return -EOPNOTSUPP;
5381 do {
5382 err = _nfs4_get_security_label(inode, buf, buflen);
5383 trace_nfs4_get_security_label(inode, err);
5384 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5385 &exception);
5386 } while (exception.retry);
5387 return err;
5390 static int _nfs4_do_set_security_label(struct inode *inode,
5391 struct nfs4_label *ilabel,
5392 struct nfs_fattr *fattr,
5393 struct nfs4_label *olabel)
5396 struct iattr sattr = {0};
5397 struct nfs_server *server = NFS_SERVER(inode);
5398 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5399 struct nfs_setattrargs arg = {
5400 .fh = NFS_FH(inode),
5401 .iap = &sattr,
5402 .server = server,
5403 .bitmask = bitmask,
5404 .label = ilabel,
5406 struct nfs_setattrres res = {
5407 .fattr = fattr,
5408 .label = olabel,
5409 .server = server,
5411 struct rpc_message msg = {
5412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5413 .rpc_argp = &arg,
5414 .rpc_resp = &res,
5416 int status;
5418 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5420 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5421 if (status)
5422 dprintk("%s failed: %d\n", __func__, status);
5424 return status;
5427 static int nfs4_do_set_security_label(struct inode *inode,
5428 struct nfs4_label *ilabel,
5429 struct nfs_fattr *fattr,
5430 struct nfs4_label *olabel)
5432 struct nfs4_exception exception = { };
5433 int err;
5435 do {
5436 err = _nfs4_do_set_security_label(inode, ilabel,
5437 fattr, olabel);
5438 trace_nfs4_set_security_label(inode, err);
5439 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5440 &exception);
5441 } while (exception.retry);
5442 return err;
5445 static int
5446 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5448 struct nfs4_label ilabel, *olabel = NULL;
5449 struct nfs_fattr fattr;
5450 struct rpc_cred *cred;
5451 int status;
5453 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5454 return -EOPNOTSUPP;
5456 nfs_fattr_init(&fattr);
5458 ilabel.pi = 0;
5459 ilabel.lfs = 0;
5460 ilabel.label = (char *)buf;
5461 ilabel.len = buflen;
5463 cred = rpc_lookup_cred();
5464 if (IS_ERR(cred))
5465 return PTR_ERR(cred);
5467 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5468 if (IS_ERR(olabel)) {
5469 status = -PTR_ERR(olabel);
5470 goto out;
5473 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5474 if (status == 0)
5475 nfs_setsecurity(inode, &fattr, olabel);
5477 nfs4_label_free(olabel);
5478 out:
5479 put_rpccred(cred);
5480 return status;
5482 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5485 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5486 nfs4_verifier *bootverf)
5488 __be32 verf[2];
5490 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5491 /* An impossible timestamp guarantees this value
5492 * will never match a generated boot time. */
5493 verf[0] = cpu_to_be32(U32_MAX);
5494 verf[1] = cpu_to_be32(U32_MAX);
5495 } else {
5496 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5497 u64 ns = ktime_to_ns(nn->boot_time);
5499 verf[0] = cpu_to_be32(ns >> 32);
5500 verf[1] = cpu_to_be32(ns);
5502 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5505 static int
5506 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5508 size_t len;
5509 char *str;
5511 if (clp->cl_owner_id != NULL)
5512 return 0;
5514 rcu_read_lock();
5515 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5516 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5518 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5520 rcu_read_unlock();
5522 if (len > NFS4_OPAQUE_LIMIT + 1)
5523 return -EINVAL;
5526 * Since this string is allocated at mount time, and held until the
5527 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5528 * about a memory-reclaim deadlock.
5530 str = kmalloc(len, GFP_KERNEL);
5531 if (!str)
5532 return -ENOMEM;
5534 rcu_read_lock();
5535 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5536 clp->cl_ipaddr,
5537 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5538 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5539 rcu_read_unlock();
5541 clp->cl_owner_id = str;
5542 return 0;
5545 static int
5546 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5548 size_t len;
5549 char *str;
5551 len = 10 + 10 + 1 + 10 + 1 +
5552 strlen(nfs4_client_id_uniquifier) + 1 +
5553 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5555 if (len > NFS4_OPAQUE_LIMIT + 1)
5556 return -EINVAL;
5559 * Since this string is allocated at mount time, and held until the
5560 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5561 * about a memory-reclaim deadlock.
5563 str = kmalloc(len, GFP_KERNEL);
5564 if (!str)
5565 return -ENOMEM;
5567 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5568 clp->rpc_ops->version, clp->cl_minorversion,
5569 nfs4_client_id_uniquifier,
5570 clp->cl_rpcclient->cl_nodename);
5571 clp->cl_owner_id = str;
5572 return 0;
5575 static int
5576 nfs4_init_uniform_client_string(struct nfs_client *clp)
5578 size_t len;
5579 char *str;
5581 if (clp->cl_owner_id != NULL)
5582 return 0;
5584 if (nfs4_client_id_uniquifier[0] != '\0')
5585 return nfs4_init_uniquifier_client_string(clp);
5587 len = 10 + 10 + 1 + 10 + 1 +
5588 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5590 if (len > NFS4_OPAQUE_LIMIT + 1)
5591 return -EINVAL;
5594 * Since this string is allocated at mount time, and held until the
5595 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5596 * about a memory-reclaim deadlock.
5598 str = kmalloc(len, GFP_KERNEL);
5599 if (!str)
5600 return -ENOMEM;
5602 scnprintf(str, len, "Linux NFSv%u.%u %s",
5603 clp->rpc_ops->version, clp->cl_minorversion,
5604 clp->cl_rpcclient->cl_nodename);
5605 clp->cl_owner_id = str;
5606 return 0;
5610 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5611 * services. Advertise one based on the address family of the
5612 * clientaddr.
5614 static unsigned int
5615 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5617 if (strchr(clp->cl_ipaddr, ':') != NULL)
5618 return scnprintf(buf, len, "tcp6");
5619 else
5620 return scnprintf(buf, len, "tcp");
5623 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5625 struct nfs4_setclientid *sc = calldata;
5627 if (task->tk_status == 0)
5628 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5631 static const struct rpc_call_ops nfs4_setclientid_ops = {
5632 .rpc_call_done = nfs4_setclientid_done,
5636 * nfs4_proc_setclientid - Negotiate client ID
5637 * @clp: state data structure
5638 * @program: RPC program for NFSv4 callback service
5639 * @port: IP port number for NFS4 callback service
5640 * @cred: RPC credential to use for this call
5641 * @res: where to place the result
5643 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5645 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5646 unsigned short port, struct rpc_cred *cred,
5647 struct nfs4_setclientid_res *res)
5649 nfs4_verifier sc_verifier;
5650 struct nfs4_setclientid setclientid = {
5651 .sc_verifier = &sc_verifier,
5652 .sc_prog = program,
5653 .sc_clnt = clp,
5655 struct rpc_message msg = {
5656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5657 .rpc_argp = &setclientid,
5658 .rpc_resp = res,
5659 .rpc_cred = cred,
5661 struct rpc_task *task;
5662 struct rpc_task_setup task_setup_data = {
5663 .rpc_client = clp->cl_rpcclient,
5664 .rpc_message = &msg,
5665 .callback_ops = &nfs4_setclientid_ops,
5666 .callback_data = &setclientid,
5667 .flags = RPC_TASK_TIMEOUT,
5669 int status;
5671 /* nfs_client_id4 */
5672 nfs4_init_boot_verifier(clp, &sc_verifier);
5674 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5675 status = nfs4_init_uniform_client_string(clp);
5676 else
5677 status = nfs4_init_nonuniform_client_string(clp);
5679 if (status)
5680 goto out;
5682 /* cb_client4 */
5683 setclientid.sc_netid_len =
5684 nfs4_init_callback_netid(clp,
5685 setclientid.sc_netid,
5686 sizeof(setclientid.sc_netid));
5687 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5688 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5689 clp->cl_ipaddr, port >> 8, port & 255);
5691 dprintk("NFS call setclientid auth=%s, '%s'\n",
5692 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5693 clp->cl_owner_id);
5694 task = rpc_run_task(&task_setup_data);
5695 if (IS_ERR(task)) {
5696 status = PTR_ERR(task);
5697 goto out;
5699 status = task->tk_status;
5700 if (setclientid.sc_cred) {
5701 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5702 put_rpccred(setclientid.sc_cred);
5704 rpc_put_task(task);
5705 out:
5706 trace_nfs4_setclientid(clp, status);
5707 dprintk("NFS reply setclientid: %d\n", status);
5708 return status;
5712 * nfs4_proc_setclientid_confirm - Confirm client ID
5713 * @clp: state data structure
5714 * @res: result of a previous SETCLIENTID
5715 * @cred: RPC credential to use for this call
5717 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5719 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5720 struct nfs4_setclientid_res *arg,
5721 struct rpc_cred *cred)
5723 struct rpc_message msg = {
5724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5725 .rpc_argp = arg,
5726 .rpc_cred = cred,
5728 int status;
5730 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5731 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5732 clp->cl_clientid);
5733 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5734 trace_nfs4_setclientid_confirm(clp, status);
5735 dprintk("NFS reply setclientid_confirm: %d\n", status);
5736 return status;
5739 struct nfs4_delegreturndata {
5740 struct nfs4_delegreturnargs args;
5741 struct nfs4_delegreturnres res;
5742 struct nfs_fh fh;
5743 nfs4_stateid stateid;
5744 unsigned long timestamp;
5745 struct {
5746 struct nfs4_layoutreturn_args arg;
5747 struct nfs4_layoutreturn_res res;
5748 struct nfs4_xdr_opaque_data ld_private;
5749 u32 roc_barrier;
5750 bool roc;
5751 } lr;
5752 struct nfs_fattr fattr;
5753 int rpc_status;
5754 struct inode *inode;
5757 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5759 struct nfs4_delegreturndata *data = calldata;
5761 if (!nfs4_sequence_done(task, &data->res.seq_res))
5762 return;
5764 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5766 /* Handle Layoutreturn errors */
5767 if (data->args.lr_args && task->tk_status != 0) {
5768 switch(data->res.lr_ret) {
5769 default:
5770 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5771 break;
5772 case 0:
5773 data->args.lr_args = NULL;
5774 data->res.lr_res = NULL;
5775 break;
5776 case -NFS4ERR_OLD_STATEID:
5777 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
5778 data->inode))
5779 goto lr_restart;
5780 /* Fallthrough */
5781 case -NFS4ERR_ADMIN_REVOKED:
5782 case -NFS4ERR_DELEG_REVOKED:
5783 case -NFS4ERR_EXPIRED:
5784 case -NFS4ERR_BAD_STATEID:
5785 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5786 case -NFS4ERR_WRONG_CRED:
5787 data->args.lr_args = NULL;
5788 data->res.lr_res = NULL;
5789 goto lr_restart;
5793 switch (task->tk_status) {
5794 case 0:
5795 renew_lease(data->res.server, data->timestamp);
5796 break;
5797 case -NFS4ERR_ADMIN_REVOKED:
5798 case -NFS4ERR_DELEG_REVOKED:
5799 case -NFS4ERR_EXPIRED:
5800 nfs4_free_revoked_stateid(data->res.server,
5801 data->args.stateid,
5802 task->tk_msg.rpc_cred);
5803 /* Fallthrough */
5804 case -NFS4ERR_BAD_STATEID:
5805 case -NFS4ERR_STALE_STATEID:
5806 task->tk_status = 0;
5807 break;
5808 case -NFS4ERR_OLD_STATEID:
5809 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5810 goto out_restart;
5811 task->tk_status = 0;
5812 break;
5813 case -NFS4ERR_ACCESS:
5814 if (data->args.bitmask) {
5815 data->args.bitmask = NULL;
5816 data->res.fattr = NULL;
5817 goto out_restart;
5819 /* Fallthrough */
5820 default:
5821 if (nfs4_async_handle_error(task, data->res.server,
5822 NULL, NULL) == -EAGAIN) {
5823 goto out_restart;
5826 data->rpc_status = task->tk_status;
5827 return;
5828 lr_restart:
5829 data->res.lr_ret = 0;
5830 out_restart:
5831 task->tk_status = 0;
5832 rpc_restart_call_prepare(task);
5835 static void nfs4_delegreturn_release(void *calldata)
5837 struct nfs4_delegreturndata *data = calldata;
5838 struct inode *inode = data->inode;
5840 if (inode) {
5841 if (data->lr.roc)
5842 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5843 data->res.lr_ret);
5844 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5845 nfs_iput_and_deactive(inode);
5847 kfree(calldata);
5850 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5852 struct nfs4_delegreturndata *d_data;
5854 d_data = (struct nfs4_delegreturndata *)data;
5856 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
5857 return;
5859 nfs4_setup_sequence(d_data->res.server->nfs_client,
5860 &d_data->args.seq_args,
5861 &d_data->res.seq_res,
5862 task);
5865 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5866 .rpc_call_prepare = nfs4_delegreturn_prepare,
5867 .rpc_call_done = nfs4_delegreturn_done,
5868 .rpc_release = nfs4_delegreturn_release,
5871 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5873 struct nfs4_delegreturndata *data;
5874 struct nfs_server *server = NFS_SERVER(inode);
5875 struct rpc_task *task;
5876 struct rpc_message msg = {
5877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5878 .rpc_cred = cred,
5880 struct rpc_task_setup task_setup_data = {
5881 .rpc_client = server->client,
5882 .rpc_message = &msg,
5883 .callback_ops = &nfs4_delegreturn_ops,
5884 .flags = RPC_TASK_ASYNC,
5886 int status = 0;
5888 data = kzalloc(sizeof(*data), GFP_NOFS);
5889 if (data == NULL)
5890 return -ENOMEM;
5891 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5893 nfs4_state_protect(server->nfs_client,
5894 NFS_SP4_MACH_CRED_CLEANUP,
5895 &task_setup_data.rpc_client, &msg);
5897 data->args.fhandle = &data->fh;
5898 data->args.stateid = &data->stateid;
5899 data->args.bitmask = server->cache_consistency_bitmask;
5900 nfs_copy_fh(&data->fh, NFS_FH(inode));
5901 nfs4_stateid_copy(&data->stateid, stateid);
5902 data->res.fattr = &data->fattr;
5903 data->res.server = server;
5904 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5905 data->lr.arg.ld_private = &data->lr.ld_private;
5906 nfs_fattr_init(data->res.fattr);
5907 data->timestamp = jiffies;
5908 data->rpc_status = 0;
5909 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
5910 data->inode = nfs_igrab_and_active(inode);
5911 if (data->inode) {
5912 if (data->lr.roc) {
5913 data->args.lr_args = &data->lr.arg;
5914 data->res.lr_res = &data->lr.res;
5916 } else if (data->lr.roc) {
5917 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5918 data->lr.roc = false;
5921 task_setup_data.callback_data = data;
5922 msg.rpc_argp = &data->args;
5923 msg.rpc_resp = &data->res;
5924 task = rpc_run_task(&task_setup_data);
5925 if (IS_ERR(task))
5926 return PTR_ERR(task);
5927 if (!issync)
5928 goto out;
5929 status = rpc_wait_for_completion_task(task);
5930 if (status != 0)
5931 goto out;
5932 status = data->rpc_status;
5933 out:
5934 rpc_put_task(task);
5935 return status;
5938 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5940 struct nfs_server *server = NFS_SERVER(inode);
5941 struct nfs4_exception exception = { };
5942 int err;
5943 do {
5944 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5945 trace_nfs4_delegreturn(inode, stateid, err);
5946 switch (err) {
5947 case -NFS4ERR_STALE_STATEID:
5948 case -NFS4ERR_EXPIRED:
5949 case 0:
5950 return 0;
5952 err = nfs4_handle_exception(server, err, &exception);
5953 } while (exception.retry);
5954 return err;
5957 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5959 struct inode *inode = state->inode;
5960 struct nfs_server *server = NFS_SERVER(inode);
5961 struct nfs_client *clp = server->nfs_client;
5962 struct nfs_lockt_args arg = {
5963 .fh = NFS_FH(inode),
5964 .fl = request,
5966 struct nfs_lockt_res res = {
5967 .denied = request,
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5971 .rpc_argp = &arg,
5972 .rpc_resp = &res,
5973 .rpc_cred = state->owner->so_cred,
5975 struct nfs4_lock_state *lsp;
5976 int status;
5978 arg.lock_owner.clientid = clp->cl_clientid;
5979 status = nfs4_set_lock_state(state, request);
5980 if (status != 0)
5981 goto out;
5982 lsp = request->fl_u.nfs4_fl.owner;
5983 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5984 arg.lock_owner.s_dev = server->s_dev;
5985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5986 switch (status) {
5987 case 0:
5988 request->fl_type = F_UNLCK;
5989 break;
5990 case -NFS4ERR_DENIED:
5991 status = 0;
5993 request->fl_ops->fl_release_private(request);
5994 request->fl_ops = NULL;
5995 out:
5996 return status;
5999 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6001 struct nfs4_exception exception = { };
6002 int err;
6004 do {
6005 err = _nfs4_proc_getlk(state, cmd, request);
6006 trace_nfs4_get_lock(request, state, cmd, err);
6007 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6008 &exception);
6009 } while (exception.retry);
6010 return err;
6013 struct nfs4_unlockdata {
6014 struct nfs_locku_args arg;
6015 struct nfs_locku_res res;
6016 struct nfs4_lock_state *lsp;
6017 struct nfs_open_context *ctx;
6018 struct nfs_lock_context *l_ctx;
6019 struct file_lock fl;
6020 struct nfs_server *server;
6021 unsigned long timestamp;
6024 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6025 struct nfs_open_context *ctx,
6026 struct nfs4_lock_state *lsp,
6027 struct nfs_seqid *seqid)
6029 struct nfs4_unlockdata *p;
6030 struct inode *inode = lsp->ls_state->inode;
6032 p = kzalloc(sizeof(*p), GFP_NOFS);
6033 if (p == NULL)
6034 return NULL;
6035 p->arg.fh = NFS_FH(inode);
6036 p->arg.fl = &p->fl;
6037 p->arg.seqid = seqid;
6038 p->res.seqid = seqid;
6039 p->lsp = lsp;
6040 refcount_inc(&lsp->ls_count);
6041 /* Ensure we don't close file until we're done freeing locks! */
6042 p->ctx = get_nfs_open_context(ctx);
6043 p->l_ctx = nfs_get_lock_context(ctx);
6044 memcpy(&p->fl, fl, sizeof(p->fl));
6045 p->server = NFS_SERVER(inode);
6046 return p;
6049 static void nfs4_locku_release_calldata(void *data)
6051 struct nfs4_unlockdata *calldata = data;
6052 nfs_free_seqid(calldata->arg.seqid);
6053 nfs4_put_lock_state(calldata->lsp);
6054 nfs_put_lock_context(calldata->l_ctx);
6055 put_nfs_open_context(calldata->ctx);
6056 kfree(calldata);
6059 static void nfs4_locku_done(struct rpc_task *task, void *data)
6061 struct nfs4_unlockdata *calldata = data;
6063 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6064 return;
6065 switch (task->tk_status) {
6066 case 0:
6067 renew_lease(calldata->server, calldata->timestamp);
6068 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6069 if (nfs4_update_lock_stateid(calldata->lsp,
6070 &calldata->res.stateid))
6071 break;
6072 case -NFS4ERR_ADMIN_REVOKED:
6073 case -NFS4ERR_EXPIRED:
6074 nfs4_free_revoked_stateid(calldata->server,
6075 &calldata->arg.stateid,
6076 task->tk_msg.rpc_cred);
6077 case -NFS4ERR_BAD_STATEID:
6078 case -NFS4ERR_OLD_STATEID:
6079 case -NFS4ERR_STALE_STATEID:
6080 if (!nfs4_stateid_match(&calldata->arg.stateid,
6081 &calldata->lsp->ls_stateid))
6082 rpc_restart_call_prepare(task);
6083 break;
6084 default:
6085 if (nfs4_async_handle_error(task, calldata->server,
6086 NULL, NULL) == -EAGAIN)
6087 rpc_restart_call_prepare(task);
6089 nfs_release_seqid(calldata->arg.seqid);
6092 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6094 struct nfs4_unlockdata *calldata = data;
6096 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6097 nfs_async_iocounter_wait(task, calldata->l_ctx))
6098 return;
6100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6101 goto out_wait;
6102 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6103 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6104 /* Note: exit _without_ running nfs4_locku_done */
6105 goto out_no_action;
6107 calldata->timestamp = jiffies;
6108 if (nfs4_setup_sequence(calldata->server->nfs_client,
6109 &calldata->arg.seq_args,
6110 &calldata->res.seq_res,
6111 task) != 0)
6112 nfs_release_seqid(calldata->arg.seqid);
6113 return;
6114 out_no_action:
6115 task->tk_action = NULL;
6116 out_wait:
6117 nfs4_sequence_done(task, &calldata->res.seq_res);
6120 static const struct rpc_call_ops nfs4_locku_ops = {
6121 .rpc_call_prepare = nfs4_locku_prepare,
6122 .rpc_call_done = nfs4_locku_done,
6123 .rpc_release = nfs4_locku_release_calldata,
6126 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6127 struct nfs_open_context *ctx,
6128 struct nfs4_lock_state *lsp,
6129 struct nfs_seqid *seqid)
6131 struct nfs4_unlockdata *data;
6132 struct rpc_message msg = {
6133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6134 .rpc_cred = ctx->cred,
6136 struct rpc_task_setup task_setup_data = {
6137 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6138 .rpc_message = &msg,
6139 .callback_ops = &nfs4_locku_ops,
6140 .workqueue = nfsiod_workqueue,
6141 .flags = RPC_TASK_ASYNC,
6144 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6145 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6147 /* Ensure this is an unlock - when canceling a lock, the
6148 * canceled lock is passed in, and it won't be an unlock.
6150 fl->fl_type = F_UNLCK;
6151 if (fl->fl_flags & FL_CLOSE)
6152 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6154 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6155 if (data == NULL) {
6156 nfs_free_seqid(seqid);
6157 return ERR_PTR(-ENOMEM);
6160 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6161 msg.rpc_argp = &data->arg;
6162 msg.rpc_resp = &data->res;
6163 task_setup_data.callback_data = data;
6164 return rpc_run_task(&task_setup_data);
6167 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6169 struct inode *inode = state->inode;
6170 struct nfs4_state_owner *sp = state->owner;
6171 struct nfs_inode *nfsi = NFS_I(inode);
6172 struct nfs_seqid *seqid;
6173 struct nfs4_lock_state *lsp;
6174 struct rpc_task *task;
6175 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6176 int status = 0;
6177 unsigned char fl_flags = request->fl_flags;
6179 status = nfs4_set_lock_state(state, request);
6180 /* Unlock _before_ we do the RPC call */
6181 request->fl_flags |= FL_EXISTS;
6182 /* Exclude nfs_delegation_claim_locks() */
6183 mutex_lock(&sp->so_delegreturn_mutex);
6184 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6185 down_read(&nfsi->rwsem);
6186 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6187 up_read(&nfsi->rwsem);
6188 mutex_unlock(&sp->so_delegreturn_mutex);
6189 goto out;
6191 up_read(&nfsi->rwsem);
6192 mutex_unlock(&sp->so_delegreturn_mutex);
6193 if (status != 0)
6194 goto out;
6195 /* Is this a delegated lock? */
6196 lsp = request->fl_u.nfs4_fl.owner;
6197 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6198 goto out;
6199 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6200 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6201 status = -ENOMEM;
6202 if (IS_ERR(seqid))
6203 goto out;
6204 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6205 status = PTR_ERR(task);
6206 if (IS_ERR(task))
6207 goto out;
6208 status = rpc_wait_for_completion_task(task);
6209 rpc_put_task(task);
6210 out:
6211 request->fl_flags = fl_flags;
6212 trace_nfs4_unlock(request, state, F_SETLK, status);
6213 return status;
6216 struct nfs4_lockdata {
6217 struct nfs_lock_args arg;
6218 struct nfs_lock_res res;
6219 struct nfs4_lock_state *lsp;
6220 struct nfs_open_context *ctx;
6221 struct file_lock fl;
6222 unsigned long timestamp;
6223 int rpc_status;
6224 int cancelled;
6225 struct nfs_server *server;
6228 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6229 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6230 gfp_t gfp_mask)
6232 struct nfs4_lockdata *p;
6233 struct inode *inode = lsp->ls_state->inode;
6234 struct nfs_server *server = NFS_SERVER(inode);
6235 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6237 p = kzalloc(sizeof(*p), gfp_mask);
6238 if (p == NULL)
6239 return NULL;
6241 p->arg.fh = NFS_FH(inode);
6242 p->arg.fl = &p->fl;
6243 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6244 if (IS_ERR(p->arg.open_seqid))
6245 goto out_free;
6246 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6247 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6248 if (IS_ERR(p->arg.lock_seqid))
6249 goto out_free_seqid;
6250 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6251 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6252 p->arg.lock_owner.s_dev = server->s_dev;
6253 p->res.lock_seqid = p->arg.lock_seqid;
6254 p->lsp = lsp;
6255 p->server = server;
6256 refcount_inc(&lsp->ls_count);
6257 p->ctx = get_nfs_open_context(ctx);
6258 memcpy(&p->fl, fl, sizeof(p->fl));
6259 return p;
6260 out_free_seqid:
6261 nfs_free_seqid(p->arg.open_seqid);
6262 out_free:
6263 kfree(p);
6264 return NULL;
6267 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6269 struct nfs4_lockdata *data = calldata;
6270 struct nfs4_state *state = data->lsp->ls_state;
6272 dprintk("%s: begin!\n", __func__);
6273 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6274 goto out_wait;
6275 /* Do we need to do an open_to_lock_owner? */
6276 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6277 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6278 goto out_release_lock_seqid;
6280 nfs4_stateid_copy(&data->arg.open_stateid,
6281 &state->open_stateid);
6282 data->arg.new_lock_owner = 1;
6283 data->res.open_seqid = data->arg.open_seqid;
6284 } else {
6285 data->arg.new_lock_owner = 0;
6286 nfs4_stateid_copy(&data->arg.lock_stateid,
6287 &data->lsp->ls_stateid);
6289 if (!nfs4_valid_open_stateid(state)) {
6290 data->rpc_status = -EBADF;
6291 task->tk_action = NULL;
6292 goto out_release_open_seqid;
6294 data->timestamp = jiffies;
6295 if (nfs4_setup_sequence(data->server->nfs_client,
6296 &data->arg.seq_args,
6297 &data->res.seq_res,
6298 task) == 0)
6299 return;
6300 out_release_open_seqid:
6301 nfs_release_seqid(data->arg.open_seqid);
6302 out_release_lock_seqid:
6303 nfs_release_seqid(data->arg.lock_seqid);
6304 out_wait:
6305 nfs4_sequence_done(task, &data->res.seq_res);
6306 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6309 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6311 struct nfs4_lockdata *data = calldata;
6312 struct nfs4_lock_state *lsp = data->lsp;
6314 dprintk("%s: begin!\n", __func__);
6316 if (!nfs4_sequence_done(task, &data->res.seq_res))
6317 return;
6319 data->rpc_status = task->tk_status;
6320 switch (task->tk_status) {
6321 case 0:
6322 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6323 data->timestamp);
6324 if (data->arg.new_lock) {
6325 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6326 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6327 rpc_restart_call_prepare(task);
6328 break;
6331 if (data->arg.new_lock_owner != 0) {
6332 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6333 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6334 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6335 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6336 rpc_restart_call_prepare(task);
6337 break;
6338 case -NFS4ERR_BAD_STATEID:
6339 case -NFS4ERR_OLD_STATEID:
6340 case -NFS4ERR_STALE_STATEID:
6341 case -NFS4ERR_EXPIRED:
6342 if (data->arg.new_lock_owner != 0) {
6343 if (!nfs4_stateid_match(&data->arg.open_stateid,
6344 &lsp->ls_state->open_stateid))
6345 rpc_restart_call_prepare(task);
6346 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6347 &lsp->ls_stateid))
6348 rpc_restart_call_prepare(task);
6350 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6353 static void nfs4_lock_release(void *calldata)
6355 struct nfs4_lockdata *data = calldata;
6357 dprintk("%s: begin!\n", __func__);
6358 nfs_free_seqid(data->arg.open_seqid);
6359 if (data->cancelled) {
6360 struct rpc_task *task;
6361 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6362 data->arg.lock_seqid);
6363 if (!IS_ERR(task))
6364 rpc_put_task_async(task);
6365 dprintk("%s: cancelling lock!\n", __func__);
6366 } else
6367 nfs_free_seqid(data->arg.lock_seqid);
6368 nfs4_put_lock_state(data->lsp);
6369 put_nfs_open_context(data->ctx);
6370 kfree(data);
6371 dprintk("%s: done!\n", __func__);
6374 static const struct rpc_call_ops nfs4_lock_ops = {
6375 .rpc_call_prepare = nfs4_lock_prepare,
6376 .rpc_call_done = nfs4_lock_done,
6377 .rpc_release = nfs4_lock_release,
6380 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6382 switch (error) {
6383 case -NFS4ERR_ADMIN_REVOKED:
6384 case -NFS4ERR_EXPIRED:
6385 case -NFS4ERR_BAD_STATEID:
6386 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6387 if (new_lock_owner != 0 ||
6388 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6389 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6390 break;
6391 case -NFS4ERR_STALE_STATEID:
6392 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6393 nfs4_schedule_lease_recovery(server->nfs_client);
6397 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6399 struct nfs4_lockdata *data;
6400 struct rpc_task *task;
6401 struct rpc_message msg = {
6402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6403 .rpc_cred = state->owner->so_cred,
6405 struct rpc_task_setup task_setup_data = {
6406 .rpc_client = NFS_CLIENT(state->inode),
6407 .rpc_message = &msg,
6408 .callback_ops = &nfs4_lock_ops,
6409 .workqueue = nfsiod_workqueue,
6410 .flags = RPC_TASK_ASYNC,
6412 int ret;
6414 dprintk("%s: begin!\n", __func__);
6415 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6416 fl->fl_u.nfs4_fl.owner,
6417 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6418 if (data == NULL)
6419 return -ENOMEM;
6420 if (IS_SETLKW(cmd))
6421 data->arg.block = 1;
6422 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6423 msg.rpc_argp = &data->arg;
6424 msg.rpc_resp = &data->res;
6425 task_setup_data.callback_data = data;
6426 if (recovery_type > NFS_LOCK_NEW) {
6427 if (recovery_type == NFS_LOCK_RECLAIM)
6428 data->arg.reclaim = NFS_LOCK_RECLAIM;
6429 nfs4_set_sequence_privileged(&data->arg.seq_args);
6430 } else
6431 data->arg.new_lock = 1;
6432 task = rpc_run_task(&task_setup_data);
6433 if (IS_ERR(task))
6434 return PTR_ERR(task);
6435 ret = rpc_wait_for_completion_task(task);
6436 if (ret == 0) {
6437 ret = data->rpc_status;
6438 if (ret)
6439 nfs4_handle_setlk_error(data->server, data->lsp,
6440 data->arg.new_lock_owner, ret);
6441 } else
6442 data->cancelled = true;
6443 rpc_put_task(task);
6444 dprintk("%s: done, ret = %d!\n", __func__, ret);
6445 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6446 return ret;
6449 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6451 struct nfs_server *server = NFS_SERVER(state->inode);
6452 struct nfs4_exception exception = {
6453 .inode = state->inode,
6455 int err;
6457 do {
6458 /* Cache the lock if possible... */
6459 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6460 return 0;
6461 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6462 if (err != -NFS4ERR_DELAY)
6463 break;
6464 nfs4_handle_exception(server, err, &exception);
6465 } while (exception.retry);
6466 return err;
6469 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6471 struct nfs_server *server = NFS_SERVER(state->inode);
6472 struct nfs4_exception exception = {
6473 .inode = state->inode,
6475 int err;
6477 err = nfs4_set_lock_state(state, request);
6478 if (err != 0)
6479 return err;
6480 if (!recover_lost_locks) {
6481 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6482 return 0;
6484 do {
6485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6486 return 0;
6487 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6488 switch (err) {
6489 default:
6490 goto out;
6491 case -NFS4ERR_GRACE:
6492 case -NFS4ERR_DELAY:
6493 nfs4_handle_exception(server, err, &exception);
6494 err = 0;
6496 } while (exception.retry);
6497 out:
6498 return err;
6501 #if defined(CONFIG_NFS_V4_1)
6502 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6504 struct nfs4_lock_state *lsp;
6505 int status;
6507 status = nfs4_set_lock_state(state, request);
6508 if (status != 0)
6509 return status;
6510 lsp = request->fl_u.nfs4_fl.owner;
6511 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6512 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6513 return 0;
6514 return nfs4_lock_expired(state, request);
6516 #endif
6518 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6520 struct nfs_inode *nfsi = NFS_I(state->inode);
6521 struct nfs4_state_owner *sp = state->owner;
6522 unsigned char fl_flags = request->fl_flags;
6523 int status;
6525 request->fl_flags |= FL_ACCESS;
6526 status = locks_lock_inode_wait(state->inode, request);
6527 if (status < 0)
6528 goto out;
6529 mutex_lock(&sp->so_delegreturn_mutex);
6530 down_read(&nfsi->rwsem);
6531 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6532 /* Yes: cache locks! */
6533 /* ...but avoid races with delegation recall... */
6534 request->fl_flags = fl_flags & ~FL_SLEEP;
6535 status = locks_lock_inode_wait(state->inode, request);
6536 up_read(&nfsi->rwsem);
6537 mutex_unlock(&sp->so_delegreturn_mutex);
6538 goto out;
6540 up_read(&nfsi->rwsem);
6541 mutex_unlock(&sp->so_delegreturn_mutex);
6542 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6543 out:
6544 request->fl_flags = fl_flags;
6545 return status;
6548 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6550 struct nfs4_exception exception = {
6551 .state = state,
6552 .inode = state->inode,
6554 int err;
6556 do {
6557 err = _nfs4_proc_setlk(state, cmd, request);
6558 if (err == -NFS4ERR_DENIED)
6559 err = -EAGAIN;
6560 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6561 err, &exception);
6562 } while (exception.retry);
6563 return err;
6566 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6567 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6569 static int
6570 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6571 struct file_lock *request)
6573 int status = -ERESTARTSYS;
6574 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6576 while(!signalled()) {
6577 status = nfs4_proc_setlk(state, cmd, request);
6578 if ((status != -EAGAIN) || IS_SETLK(cmd))
6579 break;
6580 freezable_schedule_timeout_interruptible(timeout);
6581 timeout *= 2;
6582 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6583 status = -ERESTARTSYS;
6585 return status;
6588 #ifdef CONFIG_NFS_V4_1
6589 struct nfs4_lock_waiter {
6590 struct task_struct *task;
6591 struct inode *inode;
6592 struct nfs_lowner *owner;
6593 bool notified;
6596 static int
6597 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6599 int ret;
6600 struct cb_notify_lock_args *cbnl = key;
6601 struct nfs4_lock_waiter *waiter = wait->private;
6602 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6603 *wowner = waiter->owner;
6605 /* Only wake if the callback was for the same owner */
6606 if (lowner->clientid != wowner->clientid ||
6607 lowner->id != wowner->id ||
6608 lowner->s_dev != wowner->s_dev)
6609 return 0;
6611 /* Make sure it's for the right inode */
6612 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6613 return 0;
6615 waiter->notified = true;
6617 /* override "private" so we can use default_wake_function */
6618 wait->private = waiter->task;
6619 ret = autoremove_wake_function(wait, mode, flags, key);
6620 wait->private = waiter;
6621 return ret;
6624 static int
6625 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6627 int status = -ERESTARTSYS;
6628 unsigned long flags;
6629 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6630 struct nfs_server *server = NFS_SERVER(state->inode);
6631 struct nfs_client *clp = server->nfs_client;
6632 wait_queue_head_t *q = &clp->cl_lock_waitq;
6633 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6634 .id = lsp->ls_seqid.owner_id,
6635 .s_dev = server->s_dev };
6636 struct nfs4_lock_waiter waiter = { .task = current,
6637 .inode = state->inode,
6638 .owner = &owner,
6639 .notified = false };
6640 wait_queue_entry_t wait;
6642 /* Don't bother with waitqueue if we don't expect a callback */
6643 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6644 return nfs4_retry_setlk_simple(state, cmd, request);
6646 init_wait(&wait);
6647 wait.private = &waiter;
6648 wait.func = nfs4_wake_lock_waiter;
6649 add_wait_queue(q, &wait);
6651 while(!signalled()) {
6652 status = nfs4_proc_setlk(state, cmd, request);
6653 if ((status != -EAGAIN) || IS_SETLK(cmd))
6654 break;
6656 status = -ERESTARTSYS;
6657 spin_lock_irqsave(&q->lock, flags);
6658 if (waiter.notified) {
6659 spin_unlock_irqrestore(&q->lock, flags);
6660 continue;
6662 set_current_state(TASK_INTERRUPTIBLE);
6663 spin_unlock_irqrestore(&q->lock, flags);
6665 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6668 finish_wait(q, &wait);
6669 return status;
6671 #else /* !CONFIG_NFS_V4_1 */
6672 static inline int
6673 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6675 return nfs4_retry_setlk_simple(state, cmd, request);
6677 #endif
6679 static int
6680 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6682 struct nfs_open_context *ctx;
6683 struct nfs4_state *state;
6684 int status;
6686 /* verify open state */
6687 ctx = nfs_file_open_context(filp);
6688 state = ctx->state;
6690 if (IS_GETLK(cmd)) {
6691 if (state != NULL)
6692 return nfs4_proc_getlk(state, F_GETLK, request);
6693 return 0;
6696 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6697 return -EINVAL;
6699 if (request->fl_type == F_UNLCK) {
6700 if (state != NULL)
6701 return nfs4_proc_unlck(state, cmd, request);
6702 return 0;
6705 if (state == NULL)
6706 return -ENOLCK;
6708 if ((request->fl_flags & FL_POSIX) &&
6709 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6710 return -ENOLCK;
6713 * Don't rely on the VFS having checked the file open mode,
6714 * since it won't do this for flock() locks.
6716 switch (request->fl_type) {
6717 case F_RDLCK:
6718 if (!(filp->f_mode & FMODE_READ))
6719 return -EBADF;
6720 break;
6721 case F_WRLCK:
6722 if (!(filp->f_mode & FMODE_WRITE))
6723 return -EBADF;
6726 status = nfs4_set_lock_state(state, request);
6727 if (status != 0)
6728 return status;
6730 return nfs4_retry_setlk(state, cmd, request);
6733 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6735 struct nfs_server *server = NFS_SERVER(state->inode);
6736 int err;
6738 err = nfs4_set_lock_state(state, fl);
6739 if (err != 0)
6740 return err;
6741 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6742 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6745 struct nfs_release_lockowner_data {
6746 struct nfs4_lock_state *lsp;
6747 struct nfs_server *server;
6748 struct nfs_release_lockowner_args args;
6749 struct nfs_release_lockowner_res res;
6750 unsigned long timestamp;
6753 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6755 struct nfs_release_lockowner_data *data = calldata;
6756 struct nfs_server *server = data->server;
6757 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6758 &data->res.seq_res, task);
6759 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6760 data->timestamp = jiffies;
6763 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6765 struct nfs_release_lockowner_data *data = calldata;
6766 struct nfs_server *server = data->server;
6768 nfs40_sequence_done(task, &data->res.seq_res);
6770 switch (task->tk_status) {
6771 case 0:
6772 renew_lease(server, data->timestamp);
6773 break;
6774 case -NFS4ERR_STALE_CLIENTID:
6775 case -NFS4ERR_EXPIRED:
6776 nfs4_schedule_lease_recovery(server->nfs_client);
6777 break;
6778 case -NFS4ERR_LEASE_MOVED:
6779 case -NFS4ERR_DELAY:
6780 if (nfs4_async_handle_error(task, server,
6781 NULL, NULL) == -EAGAIN)
6782 rpc_restart_call_prepare(task);
6786 static void nfs4_release_lockowner_release(void *calldata)
6788 struct nfs_release_lockowner_data *data = calldata;
6789 nfs4_free_lock_state(data->server, data->lsp);
6790 kfree(calldata);
6793 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6794 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6795 .rpc_call_done = nfs4_release_lockowner_done,
6796 .rpc_release = nfs4_release_lockowner_release,
6799 static void
6800 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6802 struct nfs_release_lockowner_data *data;
6803 struct rpc_message msg = {
6804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6807 if (server->nfs_client->cl_mvops->minor_version != 0)
6808 return;
6810 data = kmalloc(sizeof(*data), GFP_NOFS);
6811 if (!data)
6812 return;
6813 data->lsp = lsp;
6814 data->server = server;
6815 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6816 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6817 data->args.lock_owner.s_dev = server->s_dev;
6819 msg.rpc_argp = &data->args;
6820 msg.rpc_resp = &data->res;
6821 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6822 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6825 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6827 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6828 struct dentry *unused, struct inode *inode,
6829 const char *key, const void *buf,
6830 size_t buflen, int flags)
6832 return nfs4_proc_set_acl(inode, buf, buflen);
6835 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6836 struct dentry *unused, struct inode *inode,
6837 const char *key, void *buf, size_t buflen)
6839 return nfs4_proc_get_acl(inode, buf, buflen);
6842 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6844 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6847 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6849 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6850 struct dentry *unused, struct inode *inode,
6851 const char *key, const void *buf,
6852 size_t buflen, int flags)
6854 if (security_ismaclabel(key))
6855 return nfs4_set_security_label(inode, buf, buflen);
6857 return -EOPNOTSUPP;
6860 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6861 struct dentry *unused, struct inode *inode,
6862 const char *key, void *buf, size_t buflen)
6864 if (security_ismaclabel(key))
6865 return nfs4_get_security_label(inode, buf, buflen);
6866 return -EOPNOTSUPP;
6869 static ssize_t
6870 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6872 int len = 0;
6874 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6875 len = security_inode_listsecurity(inode, list, list_len);
6876 if (list_len && len > list_len)
6877 return -ERANGE;
6879 return len;
6882 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6883 .prefix = XATTR_SECURITY_PREFIX,
6884 .get = nfs4_xattr_get_nfs4_label,
6885 .set = nfs4_xattr_set_nfs4_label,
6888 #else
6890 static ssize_t
6891 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6893 return 0;
6896 #endif
6899 * nfs_fhget will use either the mounted_on_fileid or the fileid
6901 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6903 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6904 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6905 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6906 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6907 return;
6909 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6910 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6911 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6912 fattr->nlink = 2;
6915 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6916 const struct qstr *name,
6917 struct nfs4_fs_locations *fs_locations,
6918 struct page *page)
6920 struct nfs_server *server = NFS_SERVER(dir);
6921 u32 bitmask[3];
6922 struct nfs4_fs_locations_arg args = {
6923 .dir_fh = NFS_FH(dir),
6924 .name = name,
6925 .page = page,
6926 .bitmask = bitmask,
6928 struct nfs4_fs_locations_res res = {
6929 .fs_locations = fs_locations,
6931 struct rpc_message msg = {
6932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6933 .rpc_argp = &args,
6934 .rpc_resp = &res,
6936 int status;
6938 dprintk("%s: start\n", __func__);
6940 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6941 bitmask[1] = nfs4_fattr_bitmap[1];
6943 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6944 * is not supported */
6945 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6946 bitmask[0] &= ~FATTR4_WORD0_FILEID;
6947 else
6948 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
6950 nfs_fattr_init(&fs_locations->fattr);
6951 fs_locations->server = server;
6952 fs_locations->nlocations = 0;
6953 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6954 dprintk("%s: returned status = %d\n", __func__, status);
6955 return status;
6958 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6959 const struct qstr *name,
6960 struct nfs4_fs_locations *fs_locations,
6961 struct page *page)
6963 struct nfs4_exception exception = { };
6964 int err;
6965 do {
6966 err = _nfs4_proc_fs_locations(client, dir, name,
6967 fs_locations, page);
6968 trace_nfs4_get_fs_locations(dir, name, err);
6969 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6970 &exception);
6971 } while (exception.retry);
6972 return err;
6976 * This operation also signals the server that this client is
6977 * performing migration recovery. The server can stop returning
6978 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6979 * appended to this compound to identify the client ID which is
6980 * performing recovery.
6982 static int _nfs40_proc_get_locations(struct inode *inode,
6983 struct nfs4_fs_locations *locations,
6984 struct page *page, struct rpc_cred *cred)
6986 struct nfs_server *server = NFS_SERVER(inode);
6987 struct rpc_clnt *clnt = server->client;
6988 u32 bitmask[2] = {
6989 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6991 struct nfs4_fs_locations_arg args = {
6992 .clientid = server->nfs_client->cl_clientid,
6993 .fh = NFS_FH(inode),
6994 .page = page,
6995 .bitmask = bitmask,
6996 .migration = 1, /* skip LOOKUP */
6997 .renew = 1, /* append RENEW */
6999 struct nfs4_fs_locations_res res = {
7000 .fs_locations = locations,
7001 .migration = 1,
7002 .renew = 1,
7004 struct rpc_message msg = {
7005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7006 .rpc_argp = &args,
7007 .rpc_resp = &res,
7008 .rpc_cred = cred,
7010 unsigned long now = jiffies;
7011 int status;
7013 nfs_fattr_init(&locations->fattr);
7014 locations->server = server;
7015 locations->nlocations = 0;
7017 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7018 nfs4_set_sequence_privileged(&args.seq_args);
7019 status = nfs4_call_sync_sequence(clnt, server, &msg,
7020 &args.seq_args, &res.seq_res);
7021 if (status)
7022 return status;
7024 renew_lease(server, now);
7025 return 0;
7028 #ifdef CONFIG_NFS_V4_1
7031 * This operation also signals the server that this client is
7032 * performing migration recovery. The server can stop asserting
7033 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7034 * performing this operation is identified in the SEQUENCE
7035 * operation in this compound.
7037 * When the client supports GETATTR(fs_locations_info), it can
7038 * be plumbed in here.
7040 static int _nfs41_proc_get_locations(struct inode *inode,
7041 struct nfs4_fs_locations *locations,
7042 struct page *page, struct rpc_cred *cred)
7044 struct nfs_server *server = NFS_SERVER(inode);
7045 struct rpc_clnt *clnt = server->client;
7046 u32 bitmask[2] = {
7047 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7049 struct nfs4_fs_locations_arg args = {
7050 .fh = NFS_FH(inode),
7051 .page = page,
7052 .bitmask = bitmask,
7053 .migration = 1, /* skip LOOKUP */
7055 struct nfs4_fs_locations_res res = {
7056 .fs_locations = locations,
7057 .migration = 1,
7059 struct rpc_message msg = {
7060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7061 .rpc_argp = &args,
7062 .rpc_resp = &res,
7063 .rpc_cred = cred,
7065 int status;
7067 nfs_fattr_init(&locations->fattr);
7068 locations->server = server;
7069 locations->nlocations = 0;
7071 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7072 nfs4_set_sequence_privileged(&args.seq_args);
7073 status = nfs4_call_sync_sequence(clnt, server, &msg,
7074 &args.seq_args, &res.seq_res);
7075 if (status == NFS4_OK &&
7076 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7077 status = -NFS4ERR_LEASE_MOVED;
7078 return status;
7081 #endif /* CONFIG_NFS_V4_1 */
7084 * nfs4_proc_get_locations - discover locations for a migrated FSID
7085 * @inode: inode on FSID that is migrating
7086 * @locations: result of query
7087 * @page: buffer
7088 * @cred: credential to use for this operation
7090 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7091 * operation failed, or a negative errno if a local error occurred.
7093 * On success, "locations" is filled in, but if the server has
7094 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7095 * asserted.
7097 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7098 * from this client that require migration recovery.
7100 int nfs4_proc_get_locations(struct inode *inode,
7101 struct nfs4_fs_locations *locations,
7102 struct page *page, struct rpc_cred *cred)
7104 struct nfs_server *server = NFS_SERVER(inode);
7105 struct nfs_client *clp = server->nfs_client;
7106 const struct nfs4_mig_recovery_ops *ops =
7107 clp->cl_mvops->mig_recovery_ops;
7108 struct nfs4_exception exception = { };
7109 int status;
7111 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7112 (unsigned long long)server->fsid.major,
7113 (unsigned long long)server->fsid.minor,
7114 clp->cl_hostname);
7115 nfs_display_fhandle(NFS_FH(inode), __func__);
7117 do {
7118 status = ops->get_locations(inode, locations, page, cred);
7119 if (status != -NFS4ERR_DELAY)
7120 break;
7121 nfs4_handle_exception(server, status, &exception);
7122 } while (exception.retry);
7123 return status;
7127 * This operation also signals the server that this client is
7128 * performing "lease moved" recovery. The server can stop
7129 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7130 * is appended to this compound to identify the client ID which is
7131 * performing recovery.
7133 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7135 struct nfs_server *server = NFS_SERVER(inode);
7136 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7137 struct rpc_clnt *clnt = server->client;
7138 struct nfs4_fsid_present_arg args = {
7139 .fh = NFS_FH(inode),
7140 .clientid = clp->cl_clientid,
7141 .renew = 1, /* append RENEW */
7143 struct nfs4_fsid_present_res res = {
7144 .renew = 1,
7146 struct rpc_message msg = {
7147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7148 .rpc_argp = &args,
7149 .rpc_resp = &res,
7150 .rpc_cred = cred,
7152 unsigned long now = jiffies;
7153 int status;
7155 res.fh = nfs_alloc_fhandle();
7156 if (res.fh == NULL)
7157 return -ENOMEM;
7159 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7160 nfs4_set_sequence_privileged(&args.seq_args);
7161 status = nfs4_call_sync_sequence(clnt, server, &msg,
7162 &args.seq_args, &res.seq_res);
7163 nfs_free_fhandle(res.fh);
7164 if (status)
7165 return status;
7167 do_renew_lease(clp, now);
7168 return 0;
7171 #ifdef CONFIG_NFS_V4_1
7174 * This operation also signals the server that this client is
7175 * performing "lease moved" recovery. The server can stop asserting
7176 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7177 * this operation is identified in the SEQUENCE operation in this
7178 * compound.
7180 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7182 struct nfs_server *server = NFS_SERVER(inode);
7183 struct rpc_clnt *clnt = server->client;
7184 struct nfs4_fsid_present_arg args = {
7185 .fh = NFS_FH(inode),
7187 struct nfs4_fsid_present_res res = {
7189 struct rpc_message msg = {
7190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7191 .rpc_argp = &args,
7192 .rpc_resp = &res,
7193 .rpc_cred = cred,
7195 int status;
7197 res.fh = nfs_alloc_fhandle();
7198 if (res.fh == NULL)
7199 return -ENOMEM;
7201 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7202 nfs4_set_sequence_privileged(&args.seq_args);
7203 status = nfs4_call_sync_sequence(clnt, server, &msg,
7204 &args.seq_args, &res.seq_res);
7205 nfs_free_fhandle(res.fh);
7206 if (status == NFS4_OK &&
7207 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7208 status = -NFS4ERR_LEASE_MOVED;
7209 return status;
7212 #endif /* CONFIG_NFS_V4_1 */
7215 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7216 * @inode: inode on FSID to check
7217 * @cred: credential to use for this operation
7219 * Server indicates whether the FSID is present, moved, or not
7220 * recognized. This operation is necessary to clear a LEASE_MOVED
7221 * condition for this client ID.
7223 * Returns NFS4_OK if the FSID is present on this server,
7224 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7225 * NFS4ERR code if some error occurred on the server, or a
7226 * negative errno if a local failure occurred.
7228 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7230 struct nfs_server *server = NFS_SERVER(inode);
7231 struct nfs_client *clp = server->nfs_client;
7232 const struct nfs4_mig_recovery_ops *ops =
7233 clp->cl_mvops->mig_recovery_ops;
7234 struct nfs4_exception exception = { };
7235 int status;
7237 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7238 (unsigned long long)server->fsid.major,
7239 (unsigned long long)server->fsid.minor,
7240 clp->cl_hostname);
7241 nfs_display_fhandle(NFS_FH(inode), __func__);
7243 do {
7244 status = ops->fsid_present(inode, cred);
7245 if (status != -NFS4ERR_DELAY)
7246 break;
7247 nfs4_handle_exception(server, status, &exception);
7248 } while (exception.retry);
7249 return status;
7253 * If 'use_integrity' is true and the state managment nfs_client
7254 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7255 * and the machine credential as per RFC3530bis and RFC5661 Security
7256 * Considerations sections. Otherwise, just use the user cred with the
7257 * filesystem's rpc_client.
7259 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7261 int status;
7262 struct nfs4_secinfo_arg args = {
7263 .dir_fh = NFS_FH(dir),
7264 .name = name,
7266 struct nfs4_secinfo_res res = {
7267 .flavors = flavors,
7269 struct rpc_message msg = {
7270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7271 .rpc_argp = &args,
7272 .rpc_resp = &res,
7274 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7275 struct rpc_cred *cred = NULL;
7277 if (use_integrity) {
7278 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7279 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7280 msg.rpc_cred = cred;
7283 dprintk("NFS call secinfo %s\n", name->name);
7285 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7286 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7288 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7289 &res.seq_res, 0);
7290 dprintk("NFS reply secinfo: %d\n", status);
7292 if (cred)
7293 put_rpccred(cred);
7295 return status;
7298 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7299 struct nfs4_secinfo_flavors *flavors)
7301 struct nfs4_exception exception = { };
7302 int err;
7303 do {
7304 err = -NFS4ERR_WRONGSEC;
7306 /* try to use integrity protection with machine cred */
7307 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7308 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7311 * if unable to use integrity protection, or SECINFO with
7312 * integrity protection returns NFS4ERR_WRONGSEC (which is
7313 * disallowed by spec, but exists in deployed servers) use
7314 * the current filesystem's rpc_client and the user cred.
7316 if (err == -NFS4ERR_WRONGSEC)
7317 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7319 trace_nfs4_secinfo(dir, name, err);
7320 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7321 &exception);
7322 } while (exception.retry);
7323 return err;
7326 #ifdef CONFIG_NFS_V4_1
7328 * Check the exchange flags returned by the server for invalid flags, having
7329 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7330 * DS flags set.
7332 static int nfs4_check_cl_exchange_flags(u32 flags)
7334 if (flags & ~EXCHGID4_FLAG_MASK_R)
7335 goto out_inval;
7336 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7337 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7338 goto out_inval;
7339 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7340 goto out_inval;
7341 return NFS_OK;
7342 out_inval:
7343 return -NFS4ERR_INVAL;
7346 static bool
7347 nfs41_same_server_scope(struct nfs41_server_scope *a,
7348 struct nfs41_server_scope *b)
7350 if (a->server_scope_sz != b->server_scope_sz)
7351 return false;
7352 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7355 static void
7356 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7360 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7361 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7365 * nfs4_proc_bind_one_conn_to_session()
7367 * The 4.1 client currently uses the same TCP connection for the
7368 * fore and backchannel.
7370 static
7371 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7372 struct rpc_xprt *xprt,
7373 struct nfs_client *clp,
7374 struct rpc_cred *cred)
7376 int status;
7377 struct nfs41_bind_conn_to_session_args args = {
7378 .client = clp,
7379 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7381 struct nfs41_bind_conn_to_session_res res;
7382 struct rpc_message msg = {
7383 .rpc_proc =
7384 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7385 .rpc_argp = &args,
7386 .rpc_resp = &res,
7387 .rpc_cred = cred,
7389 struct rpc_task_setup task_setup_data = {
7390 .rpc_client = clnt,
7391 .rpc_xprt = xprt,
7392 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7393 .rpc_message = &msg,
7394 .flags = RPC_TASK_TIMEOUT,
7396 struct rpc_task *task;
7398 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7399 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7400 args.dir = NFS4_CDFC4_FORE;
7402 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7403 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7404 args.dir = NFS4_CDFC4_FORE;
7406 task = rpc_run_task(&task_setup_data);
7407 if (!IS_ERR(task)) {
7408 status = task->tk_status;
7409 rpc_put_task(task);
7410 } else
7411 status = PTR_ERR(task);
7412 trace_nfs4_bind_conn_to_session(clp, status);
7413 if (status == 0) {
7414 if (memcmp(res.sessionid.data,
7415 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7416 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7417 return -EIO;
7419 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7420 dprintk("NFS: %s: Unexpected direction from server\n",
7421 __func__);
7422 return -EIO;
7424 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7425 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7426 __func__);
7427 return -EIO;
7431 return status;
7434 struct rpc_bind_conn_calldata {
7435 struct nfs_client *clp;
7436 struct rpc_cred *cred;
7439 static int
7440 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7441 struct rpc_xprt *xprt,
7442 void *calldata)
7444 struct rpc_bind_conn_calldata *p = calldata;
7446 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7449 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7451 struct rpc_bind_conn_calldata data = {
7452 .clp = clp,
7453 .cred = cred,
7455 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7456 nfs4_proc_bind_conn_to_session_callback, &data);
7460 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7461 * and operations we'd like to see to enable certain features in the allow map
7463 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7464 .how = SP4_MACH_CRED,
7465 .enforce.u.words = {
7466 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7467 1 << (OP_EXCHANGE_ID - 32) |
7468 1 << (OP_CREATE_SESSION - 32) |
7469 1 << (OP_DESTROY_SESSION - 32) |
7470 1 << (OP_DESTROY_CLIENTID - 32)
7472 .allow.u.words = {
7473 [0] = 1 << (OP_CLOSE) |
7474 1 << (OP_OPEN_DOWNGRADE) |
7475 1 << (OP_LOCKU) |
7476 1 << (OP_DELEGRETURN) |
7477 1 << (OP_COMMIT),
7478 [1] = 1 << (OP_SECINFO - 32) |
7479 1 << (OP_SECINFO_NO_NAME - 32) |
7480 1 << (OP_LAYOUTRETURN - 32) |
7481 1 << (OP_TEST_STATEID - 32) |
7482 1 << (OP_FREE_STATEID - 32) |
7483 1 << (OP_WRITE - 32)
7488 * Select the state protection mode for client `clp' given the server results
7489 * from exchange_id in `sp'.
7491 * Returns 0 on success, negative errno otherwise.
7493 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7494 struct nfs41_state_protection *sp)
7496 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7497 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7498 1 << (OP_EXCHANGE_ID - 32) |
7499 1 << (OP_CREATE_SESSION - 32) |
7500 1 << (OP_DESTROY_SESSION - 32) |
7501 1 << (OP_DESTROY_CLIENTID - 32)
7503 unsigned long flags = 0;
7504 unsigned int i;
7505 int ret = 0;
7507 if (sp->how == SP4_MACH_CRED) {
7508 /* Print state protect result */
7509 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7510 for (i = 0; i <= LAST_NFS4_OP; i++) {
7511 if (test_bit(i, sp->enforce.u.longs))
7512 dfprintk(MOUNT, " enforce op %d\n", i);
7513 if (test_bit(i, sp->allow.u.longs))
7514 dfprintk(MOUNT, " allow op %d\n", i);
7517 /* make sure nothing is on enforce list that isn't supported */
7518 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7519 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7520 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7521 ret = -EINVAL;
7522 goto out;
7527 * Minimal mode - state operations are allowed to use machine
7528 * credential. Note this already happens by default, so the
7529 * client doesn't have to do anything more than the negotiation.
7531 * NOTE: we don't care if EXCHANGE_ID is in the list -
7532 * we're already using the machine cred for exchange_id
7533 * and will never use a different cred.
7535 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7536 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7537 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7538 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7539 dfprintk(MOUNT, "sp4_mach_cred:\n");
7540 dfprintk(MOUNT, " minimal mode enabled\n");
7541 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7542 } else {
7543 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7544 ret = -EINVAL;
7545 goto out;
7548 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7549 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7550 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7551 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7552 dfprintk(MOUNT, " cleanup mode enabled\n");
7553 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7556 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7557 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7558 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7561 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7562 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7563 dfprintk(MOUNT, " secinfo mode enabled\n");
7564 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7567 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7568 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7569 dfprintk(MOUNT, " stateid mode enabled\n");
7570 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7573 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7574 dfprintk(MOUNT, " write mode enabled\n");
7575 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7578 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7579 dfprintk(MOUNT, " commit mode enabled\n");
7580 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7583 out:
7584 clp->cl_sp4_flags = flags;
7585 return 0;
7588 struct nfs41_exchange_id_data {
7589 struct nfs41_exchange_id_res res;
7590 struct nfs41_exchange_id_args args;
7593 static void nfs4_exchange_id_release(void *data)
7595 struct nfs41_exchange_id_data *cdata =
7596 (struct nfs41_exchange_id_data *)data;
7598 nfs_put_client(cdata->args.client);
7599 kfree(cdata->res.impl_id);
7600 kfree(cdata->res.server_scope);
7601 kfree(cdata->res.server_owner);
7602 kfree(cdata);
7605 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7606 .rpc_release = nfs4_exchange_id_release,
7610 * _nfs4_proc_exchange_id()
7612 * Wrapper for EXCHANGE_ID operation.
7614 static struct rpc_task *
7615 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7616 u32 sp4_how, struct rpc_xprt *xprt)
7618 struct rpc_message msg = {
7619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7620 .rpc_cred = cred,
7622 struct rpc_task_setup task_setup_data = {
7623 .rpc_client = clp->cl_rpcclient,
7624 .callback_ops = &nfs4_exchange_id_call_ops,
7625 .rpc_message = &msg,
7626 .flags = RPC_TASK_TIMEOUT,
7628 struct nfs41_exchange_id_data *calldata;
7629 int status;
7631 if (!refcount_inc_not_zero(&clp->cl_count))
7632 return ERR_PTR(-EIO);
7634 status = -ENOMEM;
7635 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7636 if (!calldata)
7637 goto out;
7639 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7641 status = nfs4_init_uniform_client_string(clp);
7642 if (status)
7643 goto out_calldata;
7645 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7646 GFP_NOFS);
7647 status = -ENOMEM;
7648 if (unlikely(calldata->res.server_owner == NULL))
7649 goto out_calldata;
7651 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7652 GFP_NOFS);
7653 if (unlikely(calldata->res.server_scope == NULL))
7654 goto out_server_owner;
7656 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7657 if (unlikely(calldata->res.impl_id == NULL))
7658 goto out_server_scope;
7660 switch (sp4_how) {
7661 case SP4_NONE:
7662 calldata->args.state_protect.how = SP4_NONE;
7663 break;
7665 case SP4_MACH_CRED:
7666 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7667 break;
7669 default:
7670 /* unsupported! */
7671 WARN_ON_ONCE(1);
7672 status = -EINVAL;
7673 goto out_impl_id;
7675 if (xprt) {
7676 task_setup_data.rpc_xprt = xprt;
7677 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7678 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7679 sizeof(calldata->args.verifier.data));
7681 calldata->args.client = clp;
7682 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7683 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7684 #ifdef CONFIG_NFS_V4_1_MIGRATION
7685 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7686 #endif
7687 msg.rpc_argp = &calldata->args;
7688 msg.rpc_resp = &calldata->res;
7689 task_setup_data.callback_data = calldata;
7691 return rpc_run_task(&task_setup_data);
7693 out_impl_id:
7694 kfree(calldata->res.impl_id);
7695 out_server_scope:
7696 kfree(calldata->res.server_scope);
7697 out_server_owner:
7698 kfree(calldata->res.server_owner);
7699 out_calldata:
7700 kfree(calldata);
7701 out:
7702 nfs_put_client(clp);
7703 return ERR_PTR(status);
7707 * _nfs4_proc_exchange_id()
7709 * Wrapper for EXCHANGE_ID operation.
7711 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7712 u32 sp4_how)
7714 struct rpc_task *task;
7715 struct nfs41_exchange_id_args *argp;
7716 struct nfs41_exchange_id_res *resp;
7717 int status;
7719 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7720 if (IS_ERR(task))
7721 return PTR_ERR(task);
7723 argp = task->tk_msg.rpc_argp;
7724 resp = task->tk_msg.rpc_resp;
7725 status = task->tk_status;
7726 if (status != 0)
7727 goto out;
7729 status = nfs4_check_cl_exchange_flags(resp->flags);
7730 if (status != 0)
7731 goto out;
7733 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7734 if (status != 0)
7735 goto out;
7737 clp->cl_clientid = resp->clientid;
7738 clp->cl_exchange_flags = resp->flags;
7739 clp->cl_seqid = resp->seqid;
7740 /* Client ID is not confirmed */
7741 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7742 clear_bit(NFS4_SESSION_ESTABLISHED,
7743 &clp->cl_session->session_state);
7745 if (clp->cl_serverscope != NULL &&
7746 !nfs41_same_server_scope(clp->cl_serverscope,
7747 resp->server_scope)) {
7748 dprintk("%s: server_scope mismatch detected\n",
7749 __func__);
7750 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7753 swap(clp->cl_serverowner, resp->server_owner);
7754 swap(clp->cl_serverscope, resp->server_scope);
7755 swap(clp->cl_implid, resp->impl_id);
7757 /* Save the EXCHANGE_ID verifier session trunk tests */
7758 memcpy(clp->cl_confirm.data, argp->verifier.data,
7759 sizeof(clp->cl_confirm.data));
7760 out:
7761 trace_nfs4_exchange_id(clp, status);
7762 rpc_put_task(task);
7763 return status;
7767 * nfs4_proc_exchange_id()
7769 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7771 * Since the clientid has expired, all compounds using sessions
7772 * associated with the stale clientid will be returning
7773 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7774 * be in some phase of session reset.
7776 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7778 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7780 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7781 int status;
7783 /* try SP4_MACH_CRED if krb5i/p */
7784 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7785 authflavor == RPC_AUTH_GSS_KRB5P) {
7786 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7787 if (!status)
7788 return 0;
7791 /* try SP4_NONE */
7792 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7796 * nfs4_test_session_trunk
7798 * This is an add_xprt_test() test function called from
7799 * rpc_clnt_setup_test_and_add_xprt.
7801 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7802 * and is dereferrenced in nfs4_exchange_id_release
7804 * Upon success, add the new transport to the rpc_clnt
7806 * @clnt: struct rpc_clnt to get new transport
7807 * @xprt: the rpc_xprt to test
7808 * @data: call data for _nfs4_proc_exchange_id.
7810 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7811 void *data)
7813 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7814 struct rpc_task *task;
7815 int status;
7817 u32 sp4_how;
7819 dprintk("--> %s try %s\n", __func__,
7820 xprt->address_strings[RPC_DISPLAY_ADDR]);
7822 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7824 /* Test connection for session trunking. Async exchange_id call */
7825 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7826 if (IS_ERR(task))
7827 return PTR_ERR(task);
7829 status = task->tk_status;
7830 if (status == 0)
7831 status = nfs4_detect_session_trunking(adata->clp,
7832 task->tk_msg.rpc_resp, xprt);
7834 rpc_put_task(task);
7835 return status;
7837 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7839 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7840 struct rpc_cred *cred)
7842 struct rpc_message msg = {
7843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7844 .rpc_argp = clp,
7845 .rpc_cred = cred,
7847 int status;
7849 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7850 trace_nfs4_destroy_clientid(clp, status);
7851 if (status)
7852 dprintk("NFS: Got error %d from the server %s on "
7853 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7854 return status;
7857 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7858 struct rpc_cred *cred)
7860 unsigned int loop;
7861 int ret;
7863 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7864 ret = _nfs4_proc_destroy_clientid(clp, cred);
7865 switch (ret) {
7866 case -NFS4ERR_DELAY:
7867 case -NFS4ERR_CLIENTID_BUSY:
7868 ssleep(1);
7869 break;
7870 default:
7871 return ret;
7874 return 0;
7877 int nfs4_destroy_clientid(struct nfs_client *clp)
7879 struct rpc_cred *cred;
7880 int ret = 0;
7882 if (clp->cl_mvops->minor_version < 1)
7883 goto out;
7884 if (clp->cl_exchange_flags == 0)
7885 goto out;
7886 if (clp->cl_preserve_clid)
7887 goto out;
7888 cred = nfs4_get_clid_cred(clp);
7889 ret = nfs4_proc_destroy_clientid(clp, cred);
7890 if (cred)
7891 put_rpccred(cred);
7892 switch (ret) {
7893 case 0:
7894 case -NFS4ERR_STALE_CLIENTID:
7895 clp->cl_exchange_flags = 0;
7897 out:
7898 return ret;
7901 struct nfs4_get_lease_time_data {
7902 struct nfs4_get_lease_time_args *args;
7903 struct nfs4_get_lease_time_res *res;
7904 struct nfs_client *clp;
7907 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7908 void *calldata)
7910 struct nfs4_get_lease_time_data *data =
7911 (struct nfs4_get_lease_time_data *)calldata;
7913 dprintk("--> %s\n", __func__);
7914 /* just setup sequence, do not trigger session recovery
7915 since we're invoked within one */
7916 nfs4_setup_sequence(data->clp,
7917 &data->args->la_seq_args,
7918 &data->res->lr_seq_res,
7919 task);
7920 dprintk("<-- %s\n", __func__);
7924 * Called from nfs4_state_manager thread for session setup, so don't recover
7925 * from sequence operation or clientid errors.
7927 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7929 struct nfs4_get_lease_time_data *data =
7930 (struct nfs4_get_lease_time_data *)calldata;
7932 dprintk("--> %s\n", __func__);
7933 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7934 return;
7935 switch (task->tk_status) {
7936 case -NFS4ERR_DELAY:
7937 case -NFS4ERR_GRACE:
7938 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7939 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7940 task->tk_status = 0;
7941 /* fall through */
7942 case -NFS4ERR_RETRY_UNCACHED_REP:
7943 rpc_restart_call_prepare(task);
7944 return;
7946 dprintk("<-- %s\n", __func__);
7949 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7950 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7951 .rpc_call_done = nfs4_get_lease_time_done,
7954 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7956 struct rpc_task *task;
7957 struct nfs4_get_lease_time_args args;
7958 struct nfs4_get_lease_time_res res = {
7959 .lr_fsinfo = fsinfo,
7961 struct nfs4_get_lease_time_data data = {
7962 .args = &args,
7963 .res = &res,
7964 .clp = clp,
7966 struct rpc_message msg = {
7967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7968 .rpc_argp = &args,
7969 .rpc_resp = &res,
7971 struct rpc_task_setup task_setup = {
7972 .rpc_client = clp->cl_rpcclient,
7973 .rpc_message = &msg,
7974 .callback_ops = &nfs4_get_lease_time_ops,
7975 .callback_data = &data,
7976 .flags = RPC_TASK_TIMEOUT,
7978 int status;
7980 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7981 nfs4_set_sequence_privileged(&args.la_seq_args);
7982 task = rpc_run_task(&task_setup);
7984 if (IS_ERR(task))
7985 return PTR_ERR(task);
7987 status = task->tk_status;
7988 rpc_put_task(task);
7989 return status;
7993 * Initialize the values to be used by the client in CREATE_SESSION
7994 * If nfs4_init_session set the fore channel request and response sizes,
7995 * use them.
7997 * Set the back channel max_resp_sz_cached to zero to force the client to
7998 * always set csa_cachethis to FALSE because the current implementation
7999 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8001 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8002 struct rpc_clnt *clnt)
8004 unsigned int max_rqst_sz, max_resp_sz;
8005 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8007 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8008 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8010 /* Fore channel attributes */
8011 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8012 args->fc_attrs.max_resp_sz = max_resp_sz;
8013 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8014 args->fc_attrs.max_reqs = max_session_slots;
8016 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8017 "max_ops=%u max_reqs=%u\n",
8018 __func__,
8019 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8020 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8022 /* Back channel attributes */
8023 args->bc_attrs.max_rqst_sz = max_bc_payload;
8024 args->bc_attrs.max_resp_sz = max_bc_payload;
8025 args->bc_attrs.max_resp_sz_cached = 0;
8026 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8027 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
8029 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8030 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8031 __func__,
8032 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8033 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8034 args->bc_attrs.max_reqs);
8037 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8038 struct nfs41_create_session_res *res)
8040 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8041 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8043 if (rcvd->max_resp_sz > sent->max_resp_sz)
8044 return -EINVAL;
8046 * Our requested max_ops is the minimum we need; we're not
8047 * prepared to break up compounds into smaller pieces than that.
8048 * So, no point even trying to continue if the server won't
8049 * cooperate:
8051 if (rcvd->max_ops < sent->max_ops)
8052 return -EINVAL;
8053 if (rcvd->max_reqs == 0)
8054 return -EINVAL;
8055 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8056 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8057 return 0;
8060 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8061 struct nfs41_create_session_res *res)
8063 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8064 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8066 if (!(res->flags & SESSION4_BACK_CHAN))
8067 goto out;
8068 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8069 return -EINVAL;
8070 if (rcvd->max_resp_sz < sent->max_resp_sz)
8071 return -EINVAL;
8072 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8073 return -EINVAL;
8074 if (rcvd->max_ops > sent->max_ops)
8075 return -EINVAL;
8076 if (rcvd->max_reqs > sent->max_reqs)
8077 return -EINVAL;
8078 out:
8079 return 0;
8082 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8083 struct nfs41_create_session_res *res)
8085 int ret;
8087 ret = nfs4_verify_fore_channel_attrs(args, res);
8088 if (ret)
8089 return ret;
8090 return nfs4_verify_back_channel_attrs(args, res);
8093 static void nfs4_update_session(struct nfs4_session *session,
8094 struct nfs41_create_session_res *res)
8096 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8097 /* Mark client id and session as being confirmed */
8098 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8099 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8100 session->flags = res->flags;
8101 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8102 if (res->flags & SESSION4_BACK_CHAN)
8103 memcpy(&session->bc_attrs, &res->bc_attrs,
8104 sizeof(session->bc_attrs));
8107 static int _nfs4_proc_create_session(struct nfs_client *clp,
8108 struct rpc_cred *cred)
8110 struct nfs4_session *session = clp->cl_session;
8111 struct nfs41_create_session_args args = {
8112 .client = clp,
8113 .clientid = clp->cl_clientid,
8114 .seqid = clp->cl_seqid,
8115 .cb_program = NFS4_CALLBACK,
8117 struct nfs41_create_session_res res;
8119 struct rpc_message msg = {
8120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8121 .rpc_argp = &args,
8122 .rpc_resp = &res,
8123 .rpc_cred = cred,
8125 int status;
8127 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8128 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8130 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8131 trace_nfs4_create_session(clp, status);
8133 switch (status) {
8134 case -NFS4ERR_STALE_CLIENTID:
8135 case -NFS4ERR_DELAY:
8136 case -ETIMEDOUT:
8137 case -EACCES:
8138 case -EAGAIN:
8139 goto out;
8142 clp->cl_seqid++;
8143 if (!status) {
8144 /* Verify the session's negotiated channel_attrs values */
8145 status = nfs4_verify_channel_attrs(&args, &res);
8146 /* Increment the clientid slot sequence id */
8147 if (status)
8148 goto out;
8149 nfs4_update_session(session, &res);
8151 out:
8152 return status;
8156 * Issues a CREATE_SESSION operation to the server.
8157 * It is the responsibility of the caller to verify the session is
8158 * expired before calling this routine.
8160 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8162 int status;
8163 unsigned *ptr;
8164 struct nfs4_session *session = clp->cl_session;
8166 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8168 status = _nfs4_proc_create_session(clp, cred);
8169 if (status)
8170 goto out;
8172 /* Init or reset the session slot tables */
8173 status = nfs4_setup_session_slot_tables(session);
8174 dprintk("slot table setup returned %d\n", status);
8175 if (status)
8176 goto out;
8178 ptr = (unsigned *)&session->sess_id.data[0];
8179 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8180 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8181 out:
8182 dprintk("<-- %s\n", __func__);
8183 return status;
8187 * Issue the over-the-wire RPC DESTROY_SESSION.
8188 * The caller must serialize access to this routine.
8190 int nfs4_proc_destroy_session(struct nfs4_session *session,
8191 struct rpc_cred *cred)
8193 struct rpc_message msg = {
8194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8195 .rpc_argp = session,
8196 .rpc_cred = cred,
8198 int status = 0;
8200 dprintk("--> nfs4_proc_destroy_session\n");
8202 /* session is still being setup */
8203 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8204 return 0;
8206 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8207 trace_nfs4_destroy_session(session->clp, status);
8209 if (status)
8210 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8211 "Session has been destroyed regardless...\n", status);
8213 dprintk("<-- nfs4_proc_destroy_session\n");
8214 return status;
8218 * Renew the cl_session lease.
8220 struct nfs4_sequence_data {
8221 struct nfs_client *clp;
8222 struct nfs4_sequence_args args;
8223 struct nfs4_sequence_res res;
8226 static void nfs41_sequence_release(void *data)
8228 struct nfs4_sequence_data *calldata = data;
8229 struct nfs_client *clp = calldata->clp;
8231 if (refcount_read(&clp->cl_count) > 1)
8232 nfs4_schedule_state_renewal(clp);
8233 nfs_put_client(clp);
8234 kfree(calldata);
8237 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8239 switch(task->tk_status) {
8240 case -NFS4ERR_DELAY:
8241 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8242 return -EAGAIN;
8243 default:
8244 nfs4_schedule_lease_recovery(clp);
8246 return 0;
8249 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8251 struct nfs4_sequence_data *calldata = data;
8252 struct nfs_client *clp = calldata->clp;
8254 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8255 return;
8257 trace_nfs4_sequence(clp, task->tk_status);
8258 if (task->tk_status < 0) {
8259 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8260 if (refcount_read(&clp->cl_count) == 1)
8261 goto out;
8263 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8264 rpc_restart_call_prepare(task);
8265 return;
8268 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8269 out:
8270 dprintk("<-- %s\n", __func__);
8273 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8275 struct nfs4_sequence_data *calldata = data;
8276 struct nfs_client *clp = calldata->clp;
8277 struct nfs4_sequence_args *args;
8278 struct nfs4_sequence_res *res;
8280 args = task->tk_msg.rpc_argp;
8281 res = task->tk_msg.rpc_resp;
8283 nfs4_setup_sequence(clp, args, res, task);
8286 static const struct rpc_call_ops nfs41_sequence_ops = {
8287 .rpc_call_done = nfs41_sequence_call_done,
8288 .rpc_call_prepare = nfs41_sequence_prepare,
8289 .rpc_release = nfs41_sequence_release,
8292 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8293 struct rpc_cred *cred,
8294 struct nfs4_slot *slot,
8295 bool is_privileged)
8297 struct nfs4_sequence_data *calldata;
8298 struct rpc_message msg = {
8299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8300 .rpc_cred = cred,
8302 struct rpc_task_setup task_setup_data = {
8303 .rpc_client = clp->cl_rpcclient,
8304 .rpc_message = &msg,
8305 .callback_ops = &nfs41_sequence_ops,
8306 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8308 struct rpc_task *ret;
8310 ret = ERR_PTR(-EIO);
8311 if (!refcount_inc_not_zero(&clp->cl_count))
8312 goto out_err;
8314 ret = ERR_PTR(-ENOMEM);
8315 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8316 if (calldata == NULL)
8317 goto out_put_clp;
8318 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8319 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8320 if (is_privileged)
8321 nfs4_set_sequence_privileged(&calldata->args);
8322 msg.rpc_argp = &calldata->args;
8323 msg.rpc_resp = &calldata->res;
8324 calldata->clp = clp;
8325 task_setup_data.callback_data = calldata;
8327 ret = rpc_run_task(&task_setup_data);
8328 if (IS_ERR(ret))
8329 goto out_err;
8330 return ret;
8331 out_put_clp:
8332 nfs_put_client(clp);
8333 out_err:
8334 nfs41_release_slot(slot);
8335 return ret;
8338 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8340 struct rpc_task *task;
8341 int ret = 0;
8343 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8344 return -EAGAIN;
8345 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8346 if (IS_ERR(task))
8347 ret = PTR_ERR(task);
8348 else
8349 rpc_put_task_async(task);
8350 dprintk("<-- %s status=%d\n", __func__, ret);
8351 return ret;
8354 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8356 struct rpc_task *task;
8357 int ret;
8359 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8360 if (IS_ERR(task)) {
8361 ret = PTR_ERR(task);
8362 goto out;
8364 ret = rpc_wait_for_completion_task(task);
8365 if (!ret)
8366 ret = task->tk_status;
8367 rpc_put_task(task);
8368 out:
8369 dprintk("<-- %s status=%d\n", __func__, ret);
8370 return ret;
8373 struct nfs4_reclaim_complete_data {
8374 struct nfs_client *clp;
8375 struct nfs41_reclaim_complete_args arg;
8376 struct nfs41_reclaim_complete_res res;
8379 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8381 struct nfs4_reclaim_complete_data *calldata = data;
8383 nfs4_setup_sequence(calldata->clp,
8384 &calldata->arg.seq_args,
8385 &calldata->res.seq_res,
8386 task);
8389 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8391 switch(task->tk_status) {
8392 case 0:
8393 case -NFS4ERR_COMPLETE_ALREADY:
8394 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8395 break;
8396 case -NFS4ERR_DELAY:
8397 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8398 /* fall through */
8399 case -NFS4ERR_RETRY_UNCACHED_REP:
8400 return -EAGAIN;
8401 case -NFS4ERR_BADSESSION:
8402 case -NFS4ERR_DEADSESSION:
8403 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8404 nfs4_schedule_session_recovery(clp->cl_session,
8405 task->tk_status);
8406 break;
8407 default:
8408 nfs4_schedule_lease_recovery(clp);
8410 return 0;
8413 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8415 struct nfs4_reclaim_complete_data *calldata = data;
8416 struct nfs_client *clp = calldata->clp;
8417 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8419 dprintk("--> %s\n", __func__);
8420 if (!nfs41_sequence_done(task, res))
8421 return;
8423 trace_nfs4_reclaim_complete(clp, task->tk_status);
8424 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8425 rpc_restart_call_prepare(task);
8426 return;
8428 dprintk("<-- %s\n", __func__);
8431 static void nfs4_free_reclaim_complete_data(void *data)
8433 struct nfs4_reclaim_complete_data *calldata = data;
8435 kfree(calldata);
8438 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8439 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8440 .rpc_call_done = nfs4_reclaim_complete_done,
8441 .rpc_release = nfs4_free_reclaim_complete_data,
8445 * Issue a global reclaim complete.
8447 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8448 struct rpc_cred *cred)
8450 struct nfs4_reclaim_complete_data *calldata;
8451 struct rpc_task *task;
8452 struct rpc_message msg = {
8453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8454 .rpc_cred = cred,
8456 struct rpc_task_setup task_setup_data = {
8457 .rpc_client = clp->cl_rpcclient,
8458 .rpc_message = &msg,
8459 .callback_ops = &nfs4_reclaim_complete_call_ops,
8460 .flags = RPC_TASK_ASYNC,
8462 int status = -ENOMEM;
8464 dprintk("--> %s\n", __func__);
8465 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8466 if (calldata == NULL)
8467 goto out;
8468 calldata->clp = clp;
8469 calldata->arg.one_fs = 0;
8471 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8472 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8473 msg.rpc_argp = &calldata->arg;
8474 msg.rpc_resp = &calldata->res;
8475 task_setup_data.callback_data = calldata;
8476 task = rpc_run_task(&task_setup_data);
8477 if (IS_ERR(task)) {
8478 status = PTR_ERR(task);
8479 goto out;
8481 status = rpc_wait_for_completion_task(task);
8482 if (status == 0)
8483 status = task->tk_status;
8484 rpc_put_task(task);
8485 out:
8486 dprintk("<-- %s status=%d\n", __func__, status);
8487 return status;
8490 static void
8491 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8493 struct nfs4_layoutget *lgp = calldata;
8494 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8496 dprintk("--> %s\n", __func__);
8497 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8498 &lgp->res.seq_res, task);
8499 dprintk("<-- %s\n", __func__);
8502 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8504 struct nfs4_layoutget *lgp = calldata;
8506 dprintk("--> %s\n", __func__);
8507 nfs41_sequence_process(task, &lgp->res.seq_res);
8508 dprintk("<-- %s\n", __func__);
8511 static int
8512 nfs4_layoutget_handle_exception(struct rpc_task *task,
8513 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8515 struct inode *inode = lgp->args.inode;
8516 struct nfs_server *server = NFS_SERVER(inode);
8517 struct pnfs_layout_hdr *lo;
8518 int nfs4err = task->tk_status;
8519 int err, status = 0;
8520 LIST_HEAD(head);
8522 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8524 switch (nfs4err) {
8525 case 0:
8526 goto out;
8529 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8530 * on the file. set tk_status to -ENODATA to tell upper layer to
8531 * retry go inband.
8533 case -NFS4ERR_LAYOUTUNAVAILABLE:
8534 status = -ENODATA;
8535 goto out;
8537 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8538 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8540 case -NFS4ERR_BADLAYOUT:
8541 status = -EOVERFLOW;
8542 goto out;
8544 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8545 * (or clients) writing to the same RAID stripe except when
8546 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8548 * Treat it like we would RECALLCONFLICT -- we retry for a little
8549 * while, and then eventually give up.
8551 case -NFS4ERR_LAYOUTTRYLATER:
8552 if (lgp->args.minlength == 0) {
8553 status = -EOVERFLOW;
8554 goto out;
8556 status = -EBUSY;
8557 break;
8558 case -NFS4ERR_RECALLCONFLICT:
8559 status = -ERECALLCONFLICT;
8560 break;
8561 case -NFS4ERR_DELEG_REVOKED:
8562 case -NFS4ERR_ADMIN_REVOKED:
8563 case -NFS4ERR_EXPIRED:
8564 case -NFS4ERR_BAD_STATEID:
8565 exception->timeout = 0;
8566 spin_lock(&inode->i_lock);
8567 lo = NFS_I(inode)->layout;
8568 /* If the open stateid was bad, then recover it. */
8569 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8570 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8571 spin_unlock(&inode->i_lock);
8572 exception->state = lgp->args.ctx->state;
8573 exception->stateid = &lgp->args.stateid;
8574 break;
8578 * Mark the bad layout state as invalid, then retry
8580 pnfs_mark_layout_stateid_invalid(lo, &head);
8581 spin_unlock(&inode->i_lock);
8582 nfs_commit_inode(inode, 0);
8583 pnfs_free_lseg_list(&head);
8584 status = -EAGAIN;
8585 goto out;
8588 nfs4_sequence_free_slot(&lgp->res.seq_res);
8589 err = nfs4_handle_exception(server, nfs4err, exception);
8590 if (!status) {
8591 if (exception->retry)
8592 status = -EAGAIN;
8593 else
8594 status = err;
8596 out:
8597 dprintk("<-- %s\n", __func__);
8598 return status;
8601 static size_t max_response_pages(struct nfs_server *server)
8603 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8604 return nfs_page_array_len(0, max_resp_sz);
8607 static void nfs4_free_pages(struct page **pages, size_t size)
8609 int i;
8611 if (!pages)
8612 return;
8614 for (i = 0; i < size; i++) {
8615 if (!pages[i])
8616 break;
8617 __free_page(pages[i]);
8619 kfree(pages);
8622 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8624 struct page **pages;
8625 int i;
8627 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8628 if (!pages) {
8629 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8630 return NULL;
8633 for (i = 0; i < size; i++) {
8634 pages[i] = alloc_page(gfp_flags);
8635 if (!pages[i]) {
8636 dprintk("%s: failed to allocate page\n", __func__);
8637 nfs4_free_pages(pages, size);
8638 return NULL;
8642 return pages;
8645 static void nfs4_layoutget_release(void *calldata)
8647 struct nfs4_layoutget *lgp = calldata;
8648 struct inode *inode = lgp->args.inode;
8649 struct nfs_server *server = NFS_SERVER(inode);
8650 size_t max_pages = max_response_pages(server);
8652 dprintk("--> %s\n", __func__);
8653 nfs4_sequence_free_slot(&lgp->res.seq_res);
8654 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8655 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8656 put_nfs_open_context(lgp->args.ctx);
8657 kfree(calldata);
8658 dprintk("<-- %s\n", __func__);
8661 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8662 .rpc_call_prepare = nfs4_layoutget_prepare,
8663 .rpc_call_done = nfs4_layoutget_done,
8664 .rpc_release = nfs4_layoutget_release,
8667 struct pnfs_layout_segment *
8668 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8670 struct inode *inode = lgp->args.inode;
8671 struct nfs_server *server = NFS_SERVER(inode);
8672 size_t max_pages = max_response_pages(server);
8673 struct rpc_task *task;
8674 struct rpc_message msg = {
8675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8676 .rpc_argp = &lgp->args,
8677 .rpc_resp = &lgp->res,
8678 .rpc_cred = lgp->cred,
8680 struct rpc_task_setup task_setup_data = {
8681 .rpc_client = server->client,
8682 .rpc_message = &msg,
8683 .callback_ops = &nfs4_layoutget_call_ops,
8684 .callback_data = lgp,
8685 .flags = RPC_TASK_ASYNC,
8687 struct pnfs_layout_segment *lseg = NULL;
8688 struct nfs4_exception exception = {
8689 .inode = inode,
8690 .timeout = *timeout,
8692 int status = 0;
8694 dprintk("--> %s\n", __func__);
8696 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8697 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8699 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8700 if (!lgp->args.layout.pages) {
8701 nfs4_layoutget_release(lgp);
8702 return ERR_PTR(-ENOMEM);
8704 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8706 lgp->res.layoutp = &lgp->args.layout;
8707 lgp->res.seq_res.sr_slot = NULL;
8708 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8710 task = rpc_run_task(&task_setup_data);
8711 if (IS_ERR(task))
8712 return ERR_CAST(task);
8713 status = rpc_wait_for_completion_task(task);
8714 if (status == 0) {
8715 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8716 *timeout = exception.timeout;
8719 trace_nfs4_layoutget(lgp->args.ctx,
8720 &lgp->args.range,
8721 &lgp->res.range,
8722 &lgp->res.stateid,
8723 status);
8725 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8726 if (status == 0 && lgp->res.layoutp->len)
8727 lseg = pnfs_layout_process(lgp);
8728 rpc_put_task(task);
8729 dprintk("<-- %s status=%d\n", __func__, status);
8730 if (status)
8731 return ERR_PTR(status);
8732 return lseg;
8735 static void
8736 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8738 struct nfs4_layoutreturn *lrp = calldata;
8740 dprintk("--> %s\n", __func__);
8741 nfs4_setup_sequence(lrp->clp,
8742 &lrp->args.seq_args,
8743 &lrp->res.seq_res,
8744 task);
8747 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8749 struct nfs4_layoutreturn *lrp = calldata;
8750 struct nfs_server *server;
8752 dprintk("--> %s\n", __func__);
8754 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8755 return;
8757 server = NFS_SERVER(lrp->args.inode);
8758 switch (task->tk_status) {
8759 case -NFS4ERR_OLD_STATEID:
8760 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8761 lrp->args.inode))
8762 goto out_restart;
8763 /* Fallthrough */
8764 default:
8765 task->tk_status = 0;
8766 /* Fallthrough */
8767 case 0:
8768 break;
8769 case -NFS4ERR_DELAY:
8770 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8771 break;
8772 goto out_restart;
8774 dprintk("<-- %s\n", __func__);
8775 return;
8776 out_restart:
8777 task->tk_status = 0;
8778 nfs4_sequence_free_slot(&lrp->res.seq_res);
8779 rpc_restart_call_prepare(task);
8782 static void nfs4_layoutreturn_release(void *calldata)
8784 struct nfs4_layoutreturn *lrp = calldata;
8785 struct pnfs_layout_hdr *lo = lrp->args.layout;
8787 dprintk("--> %s\n", __func__);
8788 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
8789 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
8790 nfs4_sequence_free_slot(&lrp->res.seq_res);
8791 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8792 lrp->ld_private.ops->free(&lrp->ld_private);
8793 pnfs_put_layout_hdr(lrp->args.layout);
8794 nfs_iput_and_deactive(lrp->inode);
8795 kfree(calldata);
8796 dprintk("<-- %s\n", __func__);
8799 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8800 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8801 .rpc_call_done = nfs4_layoutreturn_done,
8802 .rpc_release = nfs4_layoutreturn_release,
8805 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8807 struct rpc_task *task;
8808 struct rpc_message msg = {
8809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8810 .rpc_argp = &lrp->args,
8811 .rpc_resp = &lrp->res,
8812 .rpc_cred = lrp->cred,
8814 struct rpc_task_setup task_setup_data = {
8815 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8816 .rpc_message = &msg,
8817 .callback_ops = &nfs4_layoutreturn_call_ops,
8818 .callback_data = lrp,
8820 int status = 0;
8822 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8823 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8824 &task_setup_data.rpc_client, &msg);
8826 dprintk("--> %s\n", __func__);
8827 if (!sync) {
8828 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8829 if (!lrp->inode) {
8830 nfs4_layoutreturn_release(lrp);
8831 return -EAGAIN;
8833 task_setup_data.flags |= RPC_TASK_ASYNC;
8835 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8836 task = rpc_run_task(&task_setup_data);
8837 if (IS_ERR(task))
8838 return PTR_ERR(task);
8839 if (sync)
8840 status = task->tk_status;
8841 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8842 dprintk("<-- %s status=%d\n", __func__, status);
8843 rpc_put_task(task);
8844 return status;
8847 static int
8848 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8849 struct pnfs_device *pdev,
8850 struct rpc_cred *cred)
8852 struct nfs4_getdeviceinfo_args args = {
8853 .pdev = pdev,
8854 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8855 NOTIFY_DEVICEID4_DELETE,
8857 struct nfs4_getdeviceinfo_res res = {
8858 .pdev = pdev,
8860 struct rpc_message msg = {
8861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8862 .rpc_argp = &args,
8863 .rpc_resp = &res,
8864 .rpc_cred = cred,
8866 int status;
8868 dprintk("--> %s\n", __func__);
8869 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8870 if (res.notification & ~args.notify_types)
8871 dprintk("%s: unsupported notification\n", __func__);
8872 if (res.notification != args.notify_types)
8873 pdev->nocache = 1;
8875 dprintk("<-- %s status=%d\n", __func__, status);
8877 return status;
8880 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8881 struct pnfs_device *pdev,
8882 struct rpc_cred *cred)
8884 struct nfs4_exception exception = { };
8885 int err;
8887 do {
8888 err = nfs4_handle_exception(server,
8889 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8890 &exception);
8891 } while (exception.retry);
8892 return err;
8894 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8896 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8898 struct nfs4_layoutcommit_data *data = calldata;
8899 struct nfs_server *server = NFS_SERVER(data->args.inode);
8901 nfs4_setup_sequence(server->nfs_client,
8902 &data->args.seq_args,
8903 &data->res.seq_res,
8904 task);
8907 static void
8908 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8910 struct nfs4_layoutcommit_data *data = calldata;
8911 struct nfs_server *server = NFS_SERVER(data->args.inode);
8913 if (!nfs41_sequence_done(task, &data->res.seq_res))
8914 return;
8916 switch (task->tk_status) { /* Just ignore these failures */
8917 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8918 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8919 case -NFS4ERR_BADLAYOUT: /* no layout */
8920 case -NFS4ERR_GRACE: /* loca_recalim always false */
8921 task->tk_status = 0;
8922 case 0:
8923 break;
8924 default:
8925 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8926 rpc_restart_call_prepare(task);
8927 return;
8932 static void nfs4_layoutcommit_release(void *calldata)
8934 struct nfs4_layoutcommit_data *data = calldata;
8936 pnfs_cleanup_layoutcommit(data);
8937 nfs_post_op_update_inode_force_wcc(data->args.inode,
8938 data->res.fattr);
8939 put_rpccred(data->cred);
8940 nfs_iput_and_deactive(data->inode);
8941 kfree(data);
8944 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8945 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8946 .rpc_call_done = nfs4_layoutcommit_done,
8947 .rpc_release = nfs4_layoutcommit_release,
8951 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8953 struct rpc_message msg = {
8954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8955 .rpc_argp = &data->args,
8956 .rpc_resp = &data->res,
8957 .rpc_cred = data->cred,
8959 struct rpc_task_setup task_setup_data = {
8960 .task = &data->task,
8961 .rpc_client = NFS_CLIENT(data->args.inode),
8962 .rpc_message = &msg,
8963 .callback_ops = &nfs4_layoutcommit_ops,
8964 .callback_data = data,
8966 struct rpc_task *task;
8967 int status = 0;
8969 dprintk("NFS: initiating layoutcommit call. sync %d "
8970 "lbw: %llu inode %lu\n", sync,
8971 data->args.lastbytewritten,
8972 data->args.inode->i_ino);
8974 if (!sync) {
8975 data->inode = nfs_igrab_and_active(data->args.inode);
8976 if (data->inode == NULL) {
8977 nfs4_layoutcommit_release(data);
8978 return -EAGAIN;
8980 task_setup_data.flags = RPC_TASK_ASYNC;
8982 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8983 task = rpc_run_task(&task_setup_data);
8984 if (IS_ERR(task))
8985 return PTR_ERR(task);
8986 if (sync)
8987 status = task->tk_status;
8988 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8989 dprintk("%s: status %d\n", __func__, status);
8990 rpc_put_task(task);
8991 return status;
8995 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8996 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8998 static int
8999 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9000 struct nfs_fsinfo *info,
9001 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9003 struct nfs41_secinfo_no_name_args args = {
9004 .style = SECINFO_STYLE_CURRENT_FH,
9006 struct nfs4_secinfo_res res = {
9007 .flavors = flavors,
9009 struct rpc_message msg = {
9010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9011 .rpc_argp = &args,
9012 .rpc_resp = &res,
9014 struct rpc_clnt *clnt = server->client;
9015 struct rpc_cred *cred = NULL;
9016 int status;
9018 if (use_integrity) {
9019 clnt = server->nfs_client->cl_rpcclient;
9020 cred = nfs4_get_clid_cred(server->nfs_client);
9021 msg.rpc_cred = cred;
9024 dprintk("--> %s\n", __func__);
9025 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9026 &res.seq_res, 0);
9027 dprintk("<-- %s status=%d\n", __func__, status);
9029 if (cred)
9030 put_rpccred(cred);
9032 return status;
9035 static int
9036 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9037 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9039 struct nfs4_exception exception = { };
9040 int err;
9041 do {
9042 /* first try using integrity protection */
9043 err = -NFS4ERR_WRONGSEC;
9045 /* try to use integrity protection with machine cred */
9046 if (_nfs4_is_integrity_protected(server->nfs_client))
9047 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9048 flavors, true);
9051 * if unable to use integrity protection, or SECINFO with
9052 * integrity protection returns NFS4ERR_WRONGSEC (which is
9053 * disallowed by spec, but exists in deployed servers) use
9054 * the current filesystem's rpc_client and the user cred.
9056 if (err == -NFS4ERR_WRONGSEC)
9057 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9058 flavors, false);
9060 switch (err) {
9061 case 0:
9062 case -NFS4ERR_WRONGSEC:
9063 case -ENOTSUPP:
9064 goto out;
9065 default:
9066 err = nfs4_handle_exception(server, err, &exception);
9068 } while (exception.retry);
9069 out:
9070 return err;
9073 static int
9074 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9075 struct nfs_fsinfo *info)
9077 int err;
9078 struct page *page;
9079 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9080 struct nfs4_secinfo_flavors *flavors;
9081 struct nfs4_secinfo4 *secinfo;
9082 int i;
9084 page = alloc_page(GFP_KERNEL);
9085 if (!page) {
9086 err = -ENOMEM;
9087 goto out;
9090 flavors = page_address(page);
9091 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9094 * Fall back on "guess and check" method if
9095 * the server doesn't support SECINFO_NO_NAME
9097 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9098 err = nfs4_find_root_sec(server, fhandle, info);
9099 goto out_freepage;
9101 if (err)
9102 goto out_freepage;
9104 for (i = 0; i < flavors->num_flavors; i++) {
9105 secinfo = &flavors->flavors[i];
9107 switch (secinfo->flavor) {
9108 case RPC_AUTH_NULL:
9109 case RPC_AUTH_UNIX:
9110 case RPC_AUTH_GSS:
9111 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9112 &secinfo->flavor_info);
9113 break;
9114 default:
9115 flavor = RPC_AUTH_MAXFLAVOR;
9116 break;
9119 if (!nfs_auth_info_match(&server->auth_info, flavor))
9120 flavor = RPC_AUTH_MAXFLAVOR;
9122 if (flavor != RPC_AUTH_MAXFLAVOR) {
9123 err = nfs4_lookup_root_sec(server, fhandle,
9124 info, flavor);
9125 if (!err)
9126 break;
9130 if (flavor == RPC_AUTH_MAXFLAVOR)
9131 err = -EPERM;
9133 out_freepage:
9134 put_page(page);
9135 if (err == -EACCES)
9136 return -EPERM;
9137 out:
9138 return err;
9141 static int _nfs41_test_stateid(struct nfs_server *server,
9142 nfs4_stateid *stateid,
9143 struct rpc_cred *cred)
9145 int status;
9146 struct nfs41_test_stateid_args args = {
9147 .stateid = stateid,
9149 struct nfs41_test_stateid_res res;
9150 struct rpc_message msg = {
9151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9152 .rpc_argp = &args,
9153 .rpc_resp = &res,
9154 .rpc_cred = cred,
9156 struct rpc_clnt *rpc_client = server->client;
9158 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9159 &rpc_client, &msg);
9161 dprintk("NFS call test_stateid %p\n", stateid);
9162 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
9163 nfs4_set_sequence_privileged(&args.seq_args);
9164 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9165 &args.seq_args, &res.seq_res);
9166 if (status != NFS_OK) {
9167 dprintk("NFS reply test_stateid: failed, %d\n", status);
9168 return status;
9170 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9171 return -res.status;
9174 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9175 int err, struct nfs4_exception *exception)
9177 exception->retry = 0;
9178 switch(err) {
9179 case -NFS4ERR_DELAY:
9180 case -NFS4ERR_RETRY_UNCACHED_REP:
9181 nfs4_handle_exception(server, err, exception);
9182 break;
9183 case -NFS4ERR_BADSESSION:
9184 case -NFS4ERR_BADSLOT:
9185 case -NFS4ERR_BAD_HIGH_SLOT:
9186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9187 case -NFS4ERR_DEADSESSION:
9188 nfs4_do_handle_exception(server, err, exception);
9193 * nfs41_test_stateid - perform a TEST_STATEID operation
9195 * @server: server / transport on which to perform the operation
9196 * @stateid: state ID to test
9197 * @cred: credential
9199 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9200 * Otherwise a negative NFS4ERR value is returned if the operation
9201 * failed or the state ID is not currently valid.
9203 static int nfs41_test_stateid(struct nfs_server *server,
9204 nfs4_stateid *stateid,
9205 struct rpc_cred *cred)
9207 struct nfs4_exception exception = { };
9208 int err;
9209 do {
9210 err = _nfs41_test_stateid(server, stateid, cred);
9211 nfs4_handle_delay_or_session_error(server, err, &exception);
9212 } while (exception.retry);
9213 return err;
9216 struct nfs_free_stateid_data {
9217 struct nfs_server *server;
9218 struct nfs41_free_stateid_args args;
9219 struct nfs41_free_stateid_res res;
9222 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9224 struct nfs_free_stateid_data *data = calldata;
9225 nfs4_setup_sequence(data->server->nfs_client,
9226 &data->args.seq_args,
9227 &data->res.seq_res,
9228 task);
9231 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9233 struct nfs_free_stateid_data *data = calldata;
9235 nfs41_sequence_done(task, &data->res.seq_res);
9237 switch (task->tk_status) {
9238 case -NFS4ERR_DELAY:
9239 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9240 rpc_restart_call_prepare(task);
9244 static void nfs41_free_stateid_release(void *calldata)
9246 kfree(calldata);
9249 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9250 .rpc_call_prepare = nfs41_free_stateid_prepare,
9251 .rpc_call_done = nfs41_free_stateid_done,
9252 .rpc_release = nfs41_free_stateid_release,
9255 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9256 const nfs4_stateid *stateid,
9257 struct rpc_cred *cred,
9258 bool privileged)
9260 struct rpc_message msg = {
9261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9262 .rpc_cred = cred,
9264 struct rpc_task_setup task_setup = {
9265 .rpc_client = server->client,
9266 .rpc_message = &msg,
9267 .callback_ops = &nfs41_free_stateid_ops,
9268 .flags = RPC_TASK_ASYNC,
9270 struct nfs_free_stateid_data *data;
9272 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9273 &task_setup.rpc_client, &msg);
9275 dprintk("NFS call free_stateid %p\n", stateid);
9276 data = kmalloc(sizeof(*data), GFP_NOFS);
9277 if (!data)
9278 return ERR_PTR(-ENOMEM);
9279 data->server = server;
9280 nfs4_stateid_copy(&data->args.stateid, stateid);
9282 task_setup.callback_data = data;
9284 msg.rpc_argp = &data->args;
9285 msg.rpc_resp = &data->res;
9286 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9287 if (privileged)
9288 nfs4_set_sequence_privileged(&data->args.seq_args);
9290 return rpc_run_task(&task_setup);
9294 * nfs41_free_stateid - perform a FREE_STATEID operation
9296 * @server: server / transport on which to perform the operation
9297 * @stateid: state ID to release
9298 * @cred: credential
9299 * @is_recovery: set to true if this call needs to be privileged
9301 * Note: this function is always asynchronous.
9303 static int nfs41_free_stateid(struct nfs_server *server,
9304 const nfs4_stateid *stateid,
9305 struct rpc_cred *cred,
9306 bool is_recovery)
9308 struct rpc_task *task;
9310 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9311 if (IS_ERR(task))
9312 return PTR_ERR(task);
9313 rpc_put_task(task);
9314 return 0;
9317 static void
9318 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9320 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9322 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9323 nfs4_free_lock_state(server, lsp);
9326 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9327 const nfs4_stateid *s2)
9329 if (s1->type != s2->type)
9330 return false;
9332 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9333 return false;
9335 if (s1->seqid == s2->seqid)
9336 return true;
9338 return s1->seqid == 0 || s2->seqid == 0;
9341 #endif /* CONFIG_NFS_V4_1 */
9343 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9344 const nfs4_stateid *s2)
9346 return nfs4_stateid_match(s1, s2);
9350 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9351 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9352 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9353 .recover_open = nfs4_open_reclaim,
9354 .recover_lock = nfs4_lock_reclaim,
9355 .establish_clid = nfs4_init_clientid,
9356 .detect_trunking = nfs40_discover_server_trunking,
9359 #if defined(CONFIG_NFS_V4_1)
9360 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9361 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9362 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9363 .recover_open = nfs4_open_reclaim,
9364 .recover_lock = nfs4_lock_reclaim,
9365 .establish_clid = nfs41_init_clientid,
9366 .reclaim_complete = nfs41_proc_reclaim_complete,
9367 .detect_trunking = nfs41_discover_server_trunking,
9369 #endif /* CONFIG_NFS_V4_1 */
9371 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9372 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9373 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9374 .recover_open = nfs40_open_expired,
9375 .recover_lock = nfs4_lock_expired,
9376 .establish_clid = nfs4_init_clientid,
9379 #if defined(CONFIG_NFS_V4_1)
9380 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9381 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9382 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9383 .recover_open = nfs41_open_expired,
9384 .recover_lock = nfs41_lock_expired,
9385 .establish_clid = nfs41_init_clientid,
9387 #endif /* CONFIG_NFS_V4_1 */
9389 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9390 .sched_state_renewal = nfs4_proc_async_renew,
9391 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9392 .renew_lease = nfs4_proc_renew,
9395 #if defined(CONFIG_NFS_V4_1)
9396 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9397 .sched_state_renewal = nfs41_proc_async_sequence,
9398 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9399 .renew_lease = nfs4_proc_sequence,
9401 #endif
9403 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9404 .get_locations = _nfs40_proc_get_locations,
9405 .fsid_present = _nfs40_proc_fsid_present,
9408 #if defined(CONFIG_NFS_V4_1)
9409 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9410 .get_locations = _nfs41_proc_get_locations,
9411 .fsid_present = _nfs41_proc_fsid_present,
9413 #endif /* CONFIG_NFS_V4_1 */
9415 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9416 .minor_version = 0,
9417 .init_caps = NFS_CAP_READDIRPLUS
9418 | NFS_CAP_ATOMIC_OPEN
9419 | NFS_CAP_POSIX_LOCK,
9420 .init_client = nfs40_init_client,
9421 .shutdown_client = nfs40_shutdown_client,
9422 .match_stateid = nfs4_match_stateid,
9423 .find_root_sec = nfs4_find_root_sec,
9424 .free_lock_state = nfs4_release_lockowner,
9425 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9426 .alloc_seqid = nfs_alloc_seqid,
9427 .call_sync_ops = &nfs40_call_sync_ops,
9428 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9429 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9430 .state_renewal_ops = &nfs40_state_renewal_ops,
9431 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9434 #if defined(CONFIG_NFS_V4_1)
9435 static struct nfs_seqid *
9436 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9438 return NULL;
9441 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9442 .minor_version = 1,
9443 .init_caps = NFS_CAP_READDIRPLUS
9444 | NFS_CAP_ATOMIC_OPEN
9445 | NFS_CAP_POSIX_LOCK
9446 | NFS_CAP_STATEID_NFSV41
9447 | NFS_CAP_ATOMIC_OPEN_V1,
9448 .init_client = nfs41_init_client,
9449 .shutdown_client = nfs41_shutdown_client,
9450 .match_stateid = nfs41_match_stateid,
9451 .find_root_sec = nfs41_find_root_sec,
9452 .free_lock_state = nfs41_free_lock_state,
9453 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9454 .alloc_seqid = nfs_alloc_no_seqid,
9455 .session_trunk = nfs4_test_session_trunk,
9456 .call_sync_ops = &nfs41_call_sync_ops,
9457 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9458 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9459 .state_renewal_ops = &nfs41_state_renewal_ops,
9460 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9462 #endif
9464 #if defined(CONFIG_NFS_V4_2)
9465 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9466 .minor_version = 2,
9467 .init_caps = NFS_CAP_READDIRPLUS
9468 | NFS_CAP_ATOMIC_OPEN
9469 | NFS_CAP_POSIX_LOCK
9470 | NFS_CAP_STATEID_NFSV41
9471 | NFS_CAP_ATOMIC_OPEN_V1
9472 | NFS_CAP_ALLOCATE
9473 | NFS_CAP_COPY
9474 | NFS_CAP_DEALLOCATE
9475 | NFS_CAP_SEEK
9476 | NFS_CAP_LAYOUTSTATS
9477 | NFS_CAP_CLONE,
9478 .init_client = nfs41_init_client,
9479 .shutdown_client = nfs41_shutdown_client,
9480 .match_stateid = nfs41_match_stateid,
9481 .find_root_sec = nfs41_find_root_sec,
9482 .free_lock_state = nfs41_free_lock_state,
9483 .call_sync_ops = &nfs41_call_sync_ops,
9484 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9485 .alloc_seqid = nfs_alloc_no_seqid,
9486 .session_trunk = nfs4_test_session_trunk,
9487 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9488 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9489 .state_renewal_ops = &nfs41_state_renewal_ops,
9490 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9492 #endif
9494 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9495 [0] = &nfs_v4_0_minor_ops,
9496 #if defined(CONFIG_NFS_V4_1)
9497 [1] = &nfs_v4_1_minor_ops,
9498 #endif
9499 #if defined(CONFIG_NFS_V4_2)
9500 [2] = &nfs_v4_2_minor_ops,
9501 #endif
9504 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9506 ssize_t error, error2;
9508 error = generic_listxattr(dentry, list, size);
9509 if (error < 0)
9510 return error;
9511 if (list) {
9512 list += error;
9513 size -= error;
9516 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9517 if (error2 < 0)
9518 return error2;
9519 return error + error2;
9522 static const struct inode_operations nfs4_dir_inode_operations = {
9523 .create = nfs_create,
9524 .lookup = nfs_lookup,
9525 .atomic_open = nfs_atomic_open,
9526 .link = nfs_link,
9527 .unlink = nfs_unlink,
9528 .symlink = nfs_symlink,
9529 .mkdir = nfs_mkdir,
9530 .rmdir = nfs_rmdir,
9531 .mknod = nfs_mknod,
9532 .rename = nfs_rename,
9533 .permission = nfs_permission,
9534 .getattr = nfs_getattr,
9535 .setattr = nfs_setattr,
9536 .listxattr = nfs4_listxattr,
9539 static const struct inode_operations nfs4_file_inode_operations = {
9540 .permission = nfs_permission,
9541 .getattr = nfs_getattr,
9542 .setattr = nfs_setattr,
9543 .listxattr = nfs4_listxattr,
9546 const struct nfs_rpc_ops nfs_v4_clientops = {
9547 .version = 4, /* protocol version */
9548 .dentry_ops = &nfs4_dentry_operations,
9549 .dir_inode_ops = &nfs4_dir_inode_operations,
9550 .file_inode_ops = &nfs4_file_inode_operations,
9551 .file_ops = &nfs4_file_operations,
9552 .getroot = nfs4_proc_get_root,
9553 .submount = nfs4_submount,
9554 .try_mount = nfs4_try_mount,
9555 .getattr = nfs4_proc_getattr,
9556 .setattr = nfs4_proc_setattr,
9557 .lookup = nfs4_proc_lookup,
9558 .lookupp = nfs4_proc_lookupp,
9559 .access = nfs4_proc_access,
9560 .readlink = nfs4_proc_readlink,
9561 .create = nfs4_proc_create,
9562 .remove = nfs4_proc_remove,
9563 .unlink_setup = nfs4_proc_unlink_setup,
9564 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9565 .unlink_done = nfs4_proc_unlink_done,
9566 .rename_setup = nfs4_proc_rename_setup,
9567 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9568 .rename_done = nfs4_proc_rename_done,
9569 .link = nfs4_proc_link,
9570 .symlink = nfs4_proc_symlink,
9571 .mkdir = nfs4_proc_mkdir,
9572 .rmdir = nfs4_proc_remove,
9573 .readdir = nfs4_proc_readdir,
9574 .mknod = nfs4_proc_mknod,
9575 .statfs = nfs4_proc_statfs,
9576 .fsinfo = nfs4_proc_fsinfo,
9577 .pathconf = nfs4_proc_pathconf,
9578 .set_capabilities = nfs4_server_capabilities,
9579 .decode_dirent = nfs4_decode_dirent,
9580 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9581 .read_setup = nfs4_proc_read_setup,
9582 .read_done = nfs4_read_done,
9583 .write_setup = nfs4_proc_write_setup,
9584 .write_done = nfs4_write_done,
9585 .commit_setup = nfs4_proc_commit_setup,
9586 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9587 .commit_done = nfs4_commit_done,
9588 .lock = nfs4_proc_lock,
9589 .clear_acl_cache = nfs4_zap_acl_attr,
9590 .close_context = nfs4_close_context,
9591 .open_context = nfs4_atomic_open,
9592 .have_delegation = nfs4_have_delegation,
9593 .return_delegation = nfs4_inode_return_delegation,
9594 .alloc_client = nfs4_alloc_client,
9595 .init_client = nfs4_init_client,
9596 .free_client = nfs4_free_client,
9597 .create_server = nfs4_create_server,
9598 .clone_server = nfs_clone_server,
9601 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9602 .name = XATTR_NAME_NFSV4_ACL,
9603 .list = nfs4_xattr_list_nfs4_acl,
9604 .get = nfs4_xattr_get_nfs4_acl,
9605 .set = nfs4_xattr_set_nfs4_acl,
9608 const struct xattr_handler *nfs4_xattr_handlers[] = {
9609 &nfs4_xattr_nfs4_acl_handler,
9610 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9611 &nfs4_xattr_nfs4_label_handler,
9612 #endif
9613 NULL
9617 * Local variables:
9618 * c-basic-offset: 8
9619 * End: