NFS: Pass i_size to fscache_unuse_cookie() when a file is released
[linux-2.6-block.git] / fs / nfs / nfs4proc.c
... / ...
CommitLineData
1/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
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.
24 *
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.
36 */
37
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>
57#include <linux/iversion.h>
58
59#include "nfs4_fs.h"
60#include "delegation.h"
61#include "internal.h"
62#include "iostat.h"
63#include "callback.h"
64#include "pnfs.h"
65#include "netns.h"
66#include "sysfs.h"
67#include "nfs4idmap.h"
68#include "nfs4session.h"
69#include "fscache.h"
70#include "nfs42.h"
71
72#include "nfs4trace.h"
73
74#define NFSDBG_FACILITY NFSDBG_PROC
75
76#define NFS4_BITMASK_SZ 3
77
78#define NFS4_POLL_RETRY_MIN (HZ/10)
79#define NFS4_POLL_RETRY_MAX (15*HZ)
80
81/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
92struct nfs4_opendata;
93static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr, struct inode *inode);
98static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 struct nfs_fattr *fattr, struct iattr *sattr,
100 struct nfs_open_context *ctx, struct nfs4_label *ilabel);
101#ifdef CONFIG_NFS_V4_1
102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 const struct cred *cred,
104 struct nfs4_slot *slot,
105 bool is_privileged);
106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
107 const struct cred *);
108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 const struct cred *, bool);
110#endif
111
112#ifdef CONFIG_NFS_V4_SECURITY_LABEL
113static inline struct nfs4_label *
114nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 struct iattr *sattr, struct nfs4_label *label)
116{
117 int err;
118
119 if (label == NULL)
120 return NULL;
121
122 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
123 return NULL;
124
125 err = security_dentry_init_security(dentry, sattr->ia_mode,
126 &dentry->d_name, NULL,
127 (void **)&label->label, &label->len);
128 if (err == 0)
129 return label;
130
131 return NULL;
132}
133static inline void
134nfs4_label_release_security(struct nfs4_label *label)
135{
136 if (label)
137 security_release_secctx(label->label, label->len);
138}
139static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
140{
141 if (label)
142 return server->attr_bitmask;
143
144 return server->attr_bitmask_nl;
145}
146#else
147static inline struct nfs4_label *
148nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
149 struct iattr *sattr, struct nfs4_label *l)
150{ return NULL; }
151static inline void
152nfs4_label_release_security(struct nfs4_label *label)
153{ return; }
154static inline u32 *
155nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
156{ return server->attr_bitmask; }
157#endif
158
159/* Prevent leaks of NFSv4 errors into userland */
160static int nfs4_map_errors(int err)
161{
162 if (err >= -1000)
163 return err;
164 switch (err) {
165 case -NFS4ERR_RESOURCE:
166 case -NFS4ERR_LAYOUTTRYLATER:
167 case -NFS4ERR_RECALLCONFLICT:
168 return -EREMOTEIO;
169 case -NFS4ERR_WRONGSEC:
170 case -NFS4ERR_WRONG_CRED:
171 return -EPERM;
172 case -NFS4ERR_BADOWNER:
173 case -NFS4ERR_BADNAME:
174 return -EINVAL;
175 case -NFS4ERR_SHARE_DENIED:
176 return -EACCES;
177 case -NFS4ERR_MINOR_VERS_MISMATCH:
178 return -EPROTONOSUPPORT;
179 case -NFS4ERR_FILE_OPEN:
180 return -EBUSY;
181 case -NFS4ERR_NOT_SAME:
182 return -ENOTSYNC;
183 default:
184 dprintk("%s could not handle NFSv4 error %d\n",
185 __func__, -err);
186 break;
187 }
188 return -EIO;
189}
190
191/*
192 * This is our standard bitmap for GETATTR requests.
193 */
194const u32 nfs4_fattr_bitmap[3] = {
195 FATTR4_WORD0_TYPE
196 | FATTR4_WORD0_CHANGE
197 | FATTR4_WORD0_SIZE
198 | FATTR4_WORD0_FSID
199 | FATTR4_WORD0_FILEID,
200 FATTR4_WORD1_MODE
201 | FATTR4_WORD1_NUMLINKS
202 | FATTR4_WORD1_OWNER
203 | FATTR4_WORD1_OWNER_GROUP
204 | FATTR4_WORD1_RAWDEV
205 | FATTR4_WORD1_SPACE_USED
206 | FATTR4_WORD1_TIME_ACCESS
207 | FATTR4_WORD1_TIME_METADATA
208 | FATTR4_WORD1_TIME_MODIFY
209 | FATTR4_WORD1_MOUNTED_ON_FILEID,
210#ifdef CONFIG_NFS_V4_SECURITY_LABEL
211 FATTR4_WORD2_SECURITY_LABEL
212#endif
213};
214
215static const u32 nfs4_pnfs_open_bitmap[3] = {
216 FATTR4_WORD0_TYPE
217 | FATTR4_WORD0_CHANGE
218 | FATTR4_WORD0_SIZE
219 | FATTR4_WORD0_FSID
220 | FATTR4_WORD0_FILEID,
221 FATTR4_WORD1_MODE
222 | FATTR4_WORD1_NUMLINKS
223 | FATTR4_WORD1_OWNER
224 | FATTR4_WORD1_OWNER_GROUP
225 | FATTR4_WORD1_RAWDEV
226 | FATTR4_WORD1_SPACE_USED
227 | FATTR4_WORD1_TIME_ACCESS
228 | FATTR4_WORD1_TIME_METADATA
229 | FATTR4_WORD1_TIME_MODIFY,
230 FATTR4_WORD2_MDSTHRESHOLD
231#ifdef CONFIG_NFS_V4_SECURITY_LABEL
232 | FATTR4_WORD2_SECURITY_LABEL
233#endif
234};
235
236static const u32 nfs4_open_noattr_bitmap[3] = {
237 FATTR4_WORD0_TYPE
238 | FATTR4_WORD0_FILEID,
239};
240
241const u32 nfs4_statfs_bitmap[3] = {
242 FATTR4_WORD0_FILES_AVAIL
243 | FATTR4_WORD0_FILES_FREE
244 | FATTR4_WORD0_FILES_TOTAL,
245 FATTR4_WORD1_SPACE_AVAIL
246 | FATTR4_WORD1_SPACE_FREE
247 | FATTR4_WORD1_SPACE_TOTAL
248};
249
250const u32 nfs4_pathconf_bitmap[3] = {
251 FATTR4_WORD0_MAXLINK
252 | FATTR4_WORD0_MAXNAME,
253 0
254};
255
256const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
257 | FATTR4_WORD0_MAXREAD
258 | FATTR4_WORD0_MAXWRITE
259 | FATTR4_WORD0_LEASE_TIME,
260 FATTR4_WORD1_TIME_DELTA
261 | FATTR4_WORD1_FS_LAYOUT_TYPES,
262 FATTR4_WORD2_LAYOUT_BLKSIZE
263 | FATTR4_WORD2_CLONE_BLKSIZE
264 | FATTR4_WORD2_CHANGE_ATTR_TYPE
265 | FATTR4_WORD2_XATTR_SUPPORT
266};
267
268const u32 nfs4_fs_locations_bitmap[3] = {
269 FATTR4_WORD0_CHANGE
270 | FATTR4_WORD0_SIZE
271 | FATTR4_WORD0_FSID
272 | FATTR4_WORD0_FILEID
273 | FATTR4_WORD0_FS_LOCATIONS,
274 FATTR4_WORD1_OWNER
275 | FATTR4_WORD1_OWNER_GROUP
276 | FATTR4_WORD1_RAWDEV
277 | FATTR4_WORD1_SPACE_USED
278 | FATTR4_WORD1_TIME_ACCESS
279 | FATTR4_WORD1_TIME_METADATA
280 | FATTR4_WORD1_TIME_MODIFY
281 | FATTR4_WORD1_MOUNTED_ON_FILEID,
282};
283
284static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
285 struct inode *inode, unsigned long flags)
286{
287 unsigned long cache_validity;
288
289 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
290 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
291 return;
292
293 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
294
295 /* Remove the attributes over which we have full control */
296 dst[1] &= ~FATTR4_WORD1_RAWDEV;
297 if (!(cache_validity & NFS_INO_INVALID_SIZE))
298 dst[0] &= ~FATTR4_WORD0_SIZE;
299
300 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
301 dst[0] &= ~FATTR4_WORD0_CHANGE;
302
303 if (!(cache_validity & NFS_INO_INVALID_MODE))
304 dst[1] &= ~FATTR4_WORD1_MODE;
305 if (!(cache_validity & NFS_INO_INVALID_OTHER))
306 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
307}
308
309static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
310 struct nfs4_readdir_arg *readdir)
311{
312 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
313 __be32 *start, *p;
314
315 if (cookie > 2) {
316 readdir->cookie = cookie;
317 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 return;
319 }
320
321 readdir->cookie = 0;
322 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 if (cookie == 2)
324 return;
325
326 /*
327 * NFSv4 servers do not return entries for '.' and '..'
328 * Therefore, we fake these entries here. We let '.'
329 * have cookie 0 and '..' have cookie 1. Note that
330 * when talking to the server, we always send cookie 0
331 * instead of 1 or 2.
332 */
333 start = p = kmap_atomic(*readdir->pages);
334
335 if (cookie == 0) {
336 *p++ = xdr_one; /* next */
337 *p++ = xdr_zero; /* cookie, first word */
338 *p++ = xdr_one; /* cookie, second word */
339 *p++ = xdr_one; /* entry len */
340 memcpy(p, ".\0\0\0", 4); /* entry */
341 p++;
342 *p++ = xdr_one; /* bitmap length */
343 *p++ = htonl(attrs); /* bitmap */
344 *p++ = htonl(12); /* attribute buffer length */
345 *p++ = htonl(NF4DIR);
346 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
347 }
348
349 *p++ = xdr_one; /* next */
350 *p++ = xdr_zero; /* cookie, first word */
351 *p++ = xdr_two; /* cookie, second word */
352 *p++ = xdr_two; /* entry len */
353 memcpy(p, "..\0\0", 4); /* entry */
354 p++;
355 *p++ = xdr_one; /* bitmap length */
356 *p++ = htonl(attrs); /* bitmap */
357 *p++ = htonl(12); /* attribute buffer length */
358 *p++ = htonl(NF4DIR);
359 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
360
361 readdir->pgbase = (char *)p - (char *)start;
362 readdir->count -= readdir->pgbase;
363 kunmap_atomic(start);
364}
365
366static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
367{
368 if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
369 fattr->pre_change_attr = version;
370 fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
371 }
372}
373
374static void nfs4_test_and_free_stateid(struct nfs_server *server,
375 nfs4_stateid *stateid,
376 const struct cred *cred)
377{
378 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
379
380 ops->test_and_free_expired(server, stateid, cred);
381}
382
383static void __nfs4_free_revoked_stateid(struct nfs_server *server,
384 nfs4_stateid *stateid,
385 const struct cred *cred)
386{
387 stateid->type = NFS4_REVOKED_STATEID_TYPE;
388 nfs4_test_and_free_stateid(server, stateid, cred);
389}
390
391static void nfs4_free_revoked_stateid(struct nfs_server *server,
392 const nfs4_stateid *stateid,
393 const struct cred *cred)
394{
395 nfs4_stateid tmp;
396
397 nfs4_stateid_copy(&tmp, stateid);
398 __nfs4_free_revoked_stateid(server, &tmp, cred);
399}
400
401static long nfs4_update_delay(long *timeout)
402{
403 long ret;
404 if (!timeout)
405 return NFS4_POLL_RETRY_MAX;
406 if (*timeout <= 0)
407 *timeout = NFS4_POLL_RETRY_MIN;
408 if (*timeout > NFS4_POLL_RETRY_MAX)
409 *timeout = NFS4_POLL_RETRY_MAX;
410 ret = *timeout;
411 *timeout <<= 1;
412 return ret;
413}
414
415static int nfs4_delay_killable(long *timeout)
416{
417 might_sleep();
418
419 freezable_schedule_timeout_killable_unsafe(
420 nfs4_update_delay(timeout));
421 if (!__fatal_signal_pending(current))
422 return 0;
423 return -EINTR;
424}
425
426static int nfs4_delay_interruptible(long *timeout)
427{
428 might_sleep();
429
430 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
431 if (!signal_pending(current))
432 return 0;
433 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
434}
435
436static int nfs4_delay(long *timeout, bool interruptible)
437{
438 if (interruptible)
439 return nfs4_delay_interruptible(timeout);
440 return nfs4_delay_killable(timeout);
441}
442
443static const nfs4_stateid *
444nfs4_recoverable_stateid(const nfs4_stateid *stateid)
445{
446 if (!stateid)
447 return NULL;
448 switch (stateid->type) {
449 case NFS4_OPEN_STATEID_TYPE:
450 case NFS4_LOCK_STATEID_TYPE:
451 case NFS4_DELEGATION_STATEID_TYPE:
452 return stateid;
453 default:
454 break;
455 }
456 return NULL;
457}
458
459/* This is the error handling routine for processes that are allowed
460 * to sleep.
461 */
462static int nfs4_do_handle_exception(struct nfs_server *server,
463 int errorcode, struct nfs4_exception *exception)
464{
465 struct nfs_client *clp = server->nfs_client;
466 struct nfs4_state *state = exception->state;
467 const nfs4_stateid *stateid;
468 struct inode *inode = exception->inode;
469 int ret = errorcode;
470
471 exception->delay = 0;
472 exception->recovering = 0;
473 exception->retry = 0;
474
475 stateid = nfs4_recoverable_stateid(exception->stateid);
476 if (stateid == NULL && state != NULL)
477 stateid = nfs4_recoverable_stateid(&state->stateid);
478
479 switch(errorcode) {
480 case 0:
481 return 0;
482 case -NFS4ERR_BADHANDLE:
483 case -ESTALE:
484 if (inode != NULL && S_ISREG(inode->i_mode))
485 pnfs_destroy_layout(NFS_I(inode));
486 break;
487 case -NFS4ERR_DELEG_REVOKED:
488 case -NFS4ERR_ADMIN_REVOKED:
489 case -NFS4ERR_EXPIRED:
490 case -NFS4ERR_BAD_STATEID:
491 case -NFS4ERR_PARTNER_NO_AUTH:
492 if (inode != NULL && stateid != NULL) {
493 nfs_inode_find_state_and_recover(inode,
494 stateid);
495 goto wait_on_recovery;
496 }
497 fallthrough;
498 case -NFS4ERR_OPENMODE:
499 if (inode) {
500 int err;
501
502 err = nfs_async_inode_return_delegation(inode,
503 stateid);
504 if (err == 0)
505 goto wait_on_recovery;
506 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
507 exception->retry = 1;
508 break;
509 }
510 }
511 if (state == NULL)
512 break;
513 ret = nfs4_schedule_stateid_recovery(server, state);
514 if (ret < 0)
515 break;
516 goto wait_on_recovery;
517 case -NFS4ERR_STALE_STATEID:
518 case -NFS4ERR_STALE_CLIENTID:
519 nfs4_schedule_lease_recovery(clp);
520 goto wait_on_recovery;
521 case -NFS4ERR_MOVED:
522 ret = nfs4_schedule_migration_recovery(server);
523 if (ret < 0)
524 break;
525 goto wait_on_recovery;
526 case -NFS4ERR_LEASE_MOVED:
527 nfs4_schedule_lease_moved_recovery(clp);
528 goto wait_on_recovery;
529#if defined(CONFIG_NFS_V4_1)
530 case -NFS4ERR_BADSESSION:
531 case -NFS4ERR_BADSLOT:
532 case -NFS4ERR_BAD_HIGH_SLOT:
533 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
534 case -NFS4ERR_DEADSESSION:
535 case -NFS4ERR_SEQ_FALSE_RETRY:
536 case -NFS4ERR_SEQ_MISORDERED:
537 /* Handled in nfs41_sequence_process() */
538 goto wait_on_recovery;
539#endif /* defined(CONFIG_NFS_V4_1) */
540 case -NFS4ERR_FILE_OPEN:
541 if (exception->timeout > HZ) {
542 /* We have retried a decent amount, time to
543 * fail
544 */
545 ret = -EBUSY;
546 break;
547 }
548 fallthrough;
549 case -NFS4ERR_DELAY:
550 nfs_inc_server_stats(server, NFSIOS_DELAY);
551 fallthrough;
552 case -NFS4ERR_GRACE:
553 case -NFS4ERR_LAYOUTTRYLATER:
554 case -NFS4ERR_RECALLCONFLICT:
555 exception->delay = 1;
556 return 0;
557
558 case -NFS4ERR_RETRY_UNCACHED_REP:
559 case -NFS4ERR_OLD_STATEID:
560 exception->retry = 1;
561 break;
562 case -NFS4ERR_BADOWNER:
563 /* The following works around a Linux server bug! */
564 case -NFS4ERR_BADNAME:
565 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
566 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
567 exception->retry = 1;
568 printk(KERN_WARNING "NFS: v4 server %s "
569 "does not accept raw "
570 "uid/gids. "
571 "Reenabling the idmapper.\n",
572 server->nfs_client->cl_hostname);
573 }
574 }
575 /* We failed to handle the error */
576 return nfs4_map_errors(ret);
577wait_on_recovery:
578 exception->recovering = 1;
579 return 0;
580}
581
582/* This is the error handling routine for processes that are allowed
583 * to sleep.
584 */
585int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
586{
587 struct nfs_client *clp = server->nfs_client;
588 int ret;
589
590 ret = nfs4_do_handle_exception(server, errorcode, exception);
591 if (exception->delay) {
592 ret = nfs4_delay(&exception->timeout,
593 exception->interruptible);
594 goto out_retry;
595 }
596 if (exception->recovering) {
597 if (exception->task_is_privileged)
598 return -EDEADLOCK;
599 ret = nfs4_wait_clnt_recover(clp);
600 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
601 return -EIO;
602 goto out_retry;
603 }
604 return ret;
605out_retry:
606 if (ret == 0)
607 exception->retry = 1;
608 return ret;
609}
610
611static int
612nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
613 int errorcode, struct nfs4_exception *exception)
614{
615 struct nfs_client *clp = server->nfs_client;
616 int ret;
617
618 ret = nfs4_do_handle_exception(server, errorcode, exception);
619 if (exception->delay) {
620 rpc_delay(task, nfs4_update_delay(&exception->timeout));
621 goto out_retry;
622 }
623 if (exception->recovering) {
624 if (exception->task_is_privileged)
625 return -EDEADLOCK;
626 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
627 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
628 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
629 goto out_retry;
630 }
631 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
632 ret = -EIO;
633 return ret;
634out_retry:
635 if (ret == 0) {
636 exception->retry = 1;
637 /*
638 * For NFS4ERR_MOVED, the client transport will need to
639 * be recomputed after migration recovery has completed.
640 */
641 if (errorcode == -NFS4ERR_MOVED)
642 rpc_task_release_transport(task);
643 }
644 return ret;
645}
646
647int
648nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
649 struct nfs4_state *state, long *timeout)
650{
651 struct nfs4_exception exception = {
652 .state = state,
653 };
654
655 if (task->tk_status >= 0)
656 return 0;
657 if (timeout)
658 exception.timeout = *timeout;
659 task->tk_status = nfs4_async_handle_exception(task, server,
660 task->tk_status,
661 &exception);
662 if (exception.delay && timeout)
663 *timeout = exception.timeout;
664 if (exception.retry)
665 return -EAGAIN;
666 return 0;
667}
668
669/*
670 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
671 * or 'false' otherwise.
672 */
673static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
674{
675 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
676 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
677}
678
679static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
680{
681 spin_lock(&clp->cl_lock);
682 if (time_before(clp->cl_last_renewal,timestamp))
683 clp->cl_last_renewal = timestamp;
684 spin_unlock(&clp->cl_lock);
685}
686
687static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
688{
689 struct nfs_client *clp = server->nfs_client;
690
691 if (!nfs4_has_session(clp))
692 do_renew_lease(clp, timestamp);
693}
694
695struct nfs4_call_sync_data {
696 const struct nfs_server *seq_server;
697 struct nfs4_sequence_args *seq_args;
698 struct nfs4_sequence_res *seq_res;
699};
700
701void nfs4_init_sequence(struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res, int cache_reply,
703 int privileged)
704{
705 args->sa_slot = NULL;
706 args->sa_cache_this = cache_reply;
707 args->sa_privileged = privileged;
708
709 res->sr_slot = NULL;
710}
711
712static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
713{
714 struct nfs4_slot *slot = res->sr_slot;
715 struct nfs4_slot_table *tbl;
716
717 tbl = slot->table;
718 spin_lock(&tbl->slot_tbl_lock);
719 if (!nfs41_wake_and_assign_slot(tbl, slot))
720 nfs4_free_slot(tbl, slot);
721 spin_unlock(&tbl->slot_tbl_lock);
722
723 res->sr_slot = NULL;
724}
725
726static int nfs40_sequence_done(struct rpc_task *task,
727 struct nfs4_sequence_res *res)
728{
729 if (res->sr_slot != NULL)
730 nfs40_sequence_free_slot(res);
731 return 1;
732}
733
734#if defined(CONFIG_NFS_V4_1)
735
736static void nfs41_release_slot(struct nfs4_slot *slot)
737{
738 struct nfs4_session *session;
739 struct nfs4_slot_table *tbl;
740 bool send_new_highest_used_slotid = false;
741
742 if (!slot)
743 return;
744 tbl = slot->table;
745 session = tbl->session;
746
747 /* Bump the slot sequence number */
748 if (slot->seq_done)
749 slot->seq_nr++;
750 slot->seq_done = 0;
751
752 spin_lock(&tbl->slot_tbl_lock);
753 /* Be nice to the server: try to ensure that the last transmitted
754 * value for highest_user_slotid <= target_highest_slotid
755 */
756 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
757 send_new_highest_used_slotid = true;
758
759 if (nfs41_wake_and_assign_slot(tbl, slot)) {
760 send_new_highest_used_slotid = false;
761 goto out_unlock;
762 }
763 nfs4_free_slot(tbl, slot);
764
765 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
766 send_new_highest_used_slotid = false;
767out_unlock:
768 spin_unlock(&tbl->slot_tbl_lock);
769 if (send_new_highest_used_slotid)
770 nfs41_notify_server(session->clp);
771 if (waitqueue_active(&tbl->slot_waitq))
772 wake_up_all(&tbl->slot_waitq);
773}
774
775static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
776{
777 nfs41_release_slot(res->sr_slot);
778 res->sr_slot = NULL;
779}
780
781static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
782 u32 seqnr)
783{
784 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
785 slot->seq_nr_highest_sent = seqnr;
786}
787static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
788 u32 seqnr)
789{
790 slot->seq_nr_highest_sent = seqnr;
791 slot->seq_nr_last_acked = seqnr;
792}
793
794static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
795 struct nfs4_slot *slot)
796{
797 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
798 if (!IS_ERR(task))
799 rpc_put_task_async(task);
800}
801
802static int nfs41_sequence_process(struct rpc_task *task,
803 struct nfs4_sequence_res *res)
804{
805 struct nfs4_session *session;
806 struct nfs4_slot *slot = res->sr_slot;
807 struct nfs_client *clp;
808 int status;
809 int ret = 1;
810
811 if (slot == NULL)
812 goto out_noaction;
813 /* don't increment the sequence number if the task wasn't sent */
814 if (!RPC_WAS_SENT(task) || slot->seq_done)
815 goto out;
816
817 session = slot->table->session;
818 clp = session->clp;
819
820 trace_nfs4_sequence_done(session, res);
821
822 status = res->sr_status;
823 if (task->tk_status == -NFS4ERR_DEADSESSION)
824 status = -NFS4ERR_DEADSESSION;
825
826 /* Check the SEQUENCE operation status */
827 switch (status) {
828 case 0:
829 /* Mark this sequence number as having been acked */
830 nfs4_slot_sequence_acked(slot, slot->seq_nr);
831 /* Update the slot's sequence and clientid lease timer */
832 slot->seq_done = 1;
833 do_renew_lease(clp, res->sr_timestamp);
834 /* Check sequence flags */
835 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
836 !!slot->privileged);
837 nfs41_update_target_slotid(slot->table, slot, res);
838 break;
839 case 1:
840 /*
841 * sr_status remains 1 if an RPC level error occurred.
842 * The server may or may not have processed the sequence
843 * operation..
844 */
845 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
846 slot->seq_done = 1;
847 goto out;
848 case -NFS4ERR_DELAY:
849 /* The server detected a resend of the RPC call and
850 * returned NFS4ERR_DELAY as per Section 2.10.6.2
851 * of RFC5661.
852 */
853 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
854 __func__,
855 slot->slot_nr,
856 slot->seq_nr);
857 nfs4_slot_sequence_acked(slot, slot->seq_nr);
858 goto out_retry;
859 case -NFS4ERR_RETRY_UNCACHED_REP:
860 case -NFS4ERR_SEQ_FALSE_RETRY:
861 /*
862 * The server thinks we tried to replay a request.
863 * Retry the call after bumping the sequence ID.
864 */
865 nfs4_slot_sequence_acked(slot, slot->seq_nr);
866 goto retry_new_seq;
867 case -NFS4ERR_BADSLOT:
868 /*
869 * The slot id we used was probably retired. Try again
870 * using a different slot id.
871 */
872 if (slot->slot_nr < slot->table->target_highest_slotid)
873 goto session_recover;
874 goto retry_nowait;
875 case -NFS4ERR_SEQ_MISORDERED:
876 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
877 /*
878 * Were one or more calls using this slot interrupted?
879 * If the server never received the request, then our
880 * transmitted slot sequence number may be too high. However,
881 * if the server did receive the request then it might
882 * accidentally give us a reply with a mismatched operation.
883 * We can sort this out by sending a lone sequence operation
884 * to the server on the same slot.
885 */
886 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
887 slot->seq_nr--;
888 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
889 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
890 res->sr_slot = NULL;
891 }
892 goto retry_nowait;
893 }
894 /*
895 * RFC5661:
896 * A retry might be sent while the original request is
897 * still in progress on the replier. The replier SHOULD
898 * deal with the issue by returning NFS4ERR_DELAY as the
899 * reply to SEQUENCE or CB_SEQUENCE operation, but
900 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
901 *
902 * Restart the search after a delay.
903 */
904 slot->seq_nr = slot->seq_nr_highest_sent;
905 goto out_retry;
906 case -NFS4ERR_BADSESSION:
907 case -NFS4ERR_DEADSESSION:
908 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
909 goto session_recover;
910 default:
911 /* Just update the slot sequence no. */
912 slot->seq_done = 1;
913 }
914out:
915 /* The session may be reset by one of the error handlers. */
916 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
917out_noaction:
918 return ret;
919session_recover:
920 nfs4_schedule_session_recovery(session, status);
921 dprintk("%s ERROR: %d Reset session\n", __func__, status);
922 nfs41_sequence_free_slot(res);
923 goto out;
924retry_new_seq:
925 ++slot->seq_nr;
926retry_nowait:
927 if (rpc_restart_call_prepare(task)) {
928 nfs41_sequence_free_slot(res);
929 task->tk_status = 0;
930 ret = 0;
931 }
932 goto out;
933out_retry:
934 if (!rpc_restart_call(task))
935 goto out;
936 rpc_delay(task, NFS4_POLL_RETRY_MAX);
937 return 0;
938}
939
940int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
941{
942 if (!nfs41_sequence_process(task, res))
943 return 0;
944 if (res->sr_slot != NULL)
945 nfs41_sequence_free_slot(res);
946 return 1;
947
948}
949EXPORT_SYMBOL_GPL(nfs41_sequence_done);
950
951static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
952{
953 if (res->sr_slot == NULL)
954 return 1;
955 if (res->sr_slot->table->session != NULL)
956 return nfs41_sequence_process(task, res);
957 return nfs40_sequence_done(task, res);
958}
959
960static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
961{
962 if (res->sr_slot != NULL) {
963 if (res->sr_slot->table->session != NULL)
964 nfs41_sequence_free_slot(res);
965 else
966 nfs40_sequence_free_slot(res);
967 }
968}
969
970int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
971{
972 if (res->sr_slot == NULL)
973 return 1;
974 if (!res->sr_slot->table->session)
975 return nfs40_sequence_done(task, res);
976 return nfs41_sequence_done(task, res);
977}
978EXPORT_SYMBOL_GPL(nfs4_sequence_done);
979
980static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
981{
982 struct nfs4_call_sync_data *data = calldata;
983
984 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
985
986 nfs4_setup_sequence(data->seq_server->nfs_client,
987 data->seq_args, data->seq_res, task);
988}
989
990static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
991{
992 struct nfs4_call_sync_data *data = calldata;
993
994 nfs41_sequence_done(task, data->seq_res);
995}
996
997static const struct rpc_call_ops nfs41_call_sync_ops = {
998 .rpc_call_prepare = nfs41_call_sync_prepare,
999 .rpc_call_done = nfs41_call_sync_done,
1000};
1001
1002#else /* !CONFIG_NFS_V4_1 */
1003
1004static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1005{
1006 return nfs40_sequence_done(task, res);
1007}
1008
1009static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1010{
1011 if (res->sr_slot != NULL)
1012 nfs40_sequence_free_slot(res);
1013}
1014
1015int nfs4_sequence_done(struct rpc_task *task,
1016 struct nfs4_sequence_res *res)
1017{
1018 return nfs40_sequence_done(task, res);
1019}
1020EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1021
1022#endif /* !CONFIG_NFS_V4_1 */
1023
1024static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1025{
1026 res->sr_timestamp = jiffies;
1027 res->sr_status_flags = 0;
1028 res->sr_status = 1;
1029}
1030
1031static
1032void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1033 struct nfs4_sequence_res *res,
1034 struct nfs4_slot *slot)
1035{
1036 if (!slot)
1037 return;
1038 slot->privileged = args->sa_privileged ? 1 : 0;
1039 args->sa_slot = slot;
1040
1041 res->sr_slot = slot;
1042}
1043
1044int nfs4_setup_sequence(struct nfs_client *client,
1045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res,
1047 struct rpc_task *task)
1048{
1049 struct nfs4_session *session = nfs4_get_session(client);
1050 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
1051 struct nfs4_slot *slot;
1052
1053 /* slot already allocated? */
1054 if (res->sr_slot != NULL)
1055 goto out_start;
1056
1057 if (session)
1058 tbl = &session->fc_slot_table;
1059
1060 spin_lock(&tbl->slot_tbl_lock);
1061 /* The state manager will wait until the slot table is empty */
1062 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1063 goto out_sleep;
1064
1065 slot = nfs4_alloc_slot(tbl);
1066 if (IS_ERR(slot)) {
1067 if (slot == ERR_PTR(-ENOMEM))
1068 goto out_sleep_timeout;
1069 goto out_sleep;
1070 }
1071 spin_unlock(&tbl->slot_tbl_lock);
1072
1073 nfs4_sequence_attach_slot(args, res, slot);
1074
1075 trace_nfs4_setup_sequence(session, args);
1076out_start:
1077 nfs41_sequence_res_init(res);
1078 rpc_call_start(task);
1079 return 0;
1080out_sleep_timeout:
1081 /* Try again in 1/4 second */
1082 if (args->sa_privileged)
1083 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1084 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1085 else
1086 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1087 NULL, jiffies + (HZ >> 2));
1088 spin_unlock(&tbl->slot_tbl_lock);
1089 return -EAGAIN;
1090out_sleep:
1091 if (args->sa_privileged)
1092 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1093 RPC_PRIORITY_PRIVILEGED);
1094 else
1095 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1096 spin_unlock(&tbl->slot_tbl_lock);
1097 return -EAGAIN;
1098}
1099EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1100
1101static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1102{
1103 struct nfs4_call_sync_data *data = calldata;
1104 nfs4_setup_sequence(data->seq_server->nfs_client,
1105 data->seq_args, data->seq_res, task);
1106}
1107
1108static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1109{
1110 struct nfs4_call_sync_data *data = calldata;
1111 nfs4_sequence_done(task, data->seq_res);
1112}
1113
1114static const struct rpc_call_ops nfs40_call_sync_ops = {
1115 .rpc_call_prepare = nfs40_call_sync_prepare,
1116 .rpc_call_done = nfs40_call_sync_done,
1117};
1118
1119static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1120{
1121 int ret;
1122 struct rpc_task *task;
1123
1124 task = rpc_run_task(task_setup);
1125 if (IS_ERR(task))
1126 return PTR_ERR(task);
1127
1128 ret = task->tk_status;
1129 rpc_put_task(task);
1130 return ret;
1131}
1132
1133static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1134 struct nfs_server *server,
1135 struct rpc_message *msg,
1136 struct nfs4_sequence_args *args,
1137 struct nfs4_sequence_res *res,
1138 unsigned short task_flags)
1139{
1140 struct nfs_client *clp = server->nfs_client;
1141 struct nfs4_call_sync_data data = {
1142 .seq_server = server,
1143 .seq_args = args,
1144 .seq_res = res,
1145 };
1146 struct rpc_task_setup task_setup = {
1147 .rpc_client = clnt,
1148 .rpc_message = msg,
1149 .callback_ops = clp->cl_mvops->call_sync_ops,
1150 .callback_data = &data,
1151 .flags = task_flags,
1152 };
1153
1154 return nfs4_call_sync_custom(&task_setup);
1155}
1156
1157static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1158 struct nfs_server *server,
1159 struct rpc_message *msg,
1160 struct nfs4_sequence_args *args,
1161 struct nfs4_sequence_res *res)
1162{
1163 unsigned short task_flags = 0;
1164
1165 if (server->nfs_client->cl_minorversion)
1166 task_flags = RPC_TASK_MOVEABLE;
1167 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1168}
1169
1170
1171int nfs4_call_sync(struct rpc_clnt *clnt,
1172 struct nfs_server *server,
1173 struct rpc_message *msg,
1174 struct nfs4_sequence_args *args,
1175 struct nfs4_sequence_res *res,
1176 int cache_reply)
1177{
1178 nfs4_init_sequence(args, res, cache_reply, 0);
1179 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1180}
1181
1182static void
1183nfs4_inc_nlink_locked(struct inode *inode)
1184{
1185 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1186 NFS_INO_INVALID_CTIME |
1187 NFS_INO_INVALID_NLINK);
1188 inc_nlink(inode);
1189}
1190
1191static void
1192nfs4_inc_nlink(struct inode *inode)
1193{
1194 spin_lock(&inode->i_lock);
1195 nfs4_inc_nlink_locked(inode);
1196 spin_unlock(&inode->i_lock);
1197}
1198
1199static void
1200nfs4_dec_nlink_locked(struct inode *inode)
1201{
1202 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1203 NFS_INO_INVALID_CTIME |
1204 NFS_INO_INVALID_NLINK);
1205 drop_nlink(inode);
1206}
1207
1208static void
1209nfs4_update_changeattr_locked(struct inode *inode,
1210 struct nfs4_change_info *cinfo,
1211 unsigned long timestamp, unsigned long cache_validity)
1212{
1213 struct nfs_inode *nfsi = NFS_I(inode);
1214 u64 change_attr = inode_peek_iversion_raw(inode);
1215
1216 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1217 if (S_ISDIR(inode->i_mode))
1218 cache_validity |= NFS_INO_INVALID_DATA;
1219
1220 switch (NFS_SERVER(inode)->change_attr_type) {
1221 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1222 if (cinfo->after == change_attr)
1223 goto out;
1224 break;
1225 default:
1226 if ((s64)(change_attr - cinfo->after) >= 0)
1227 goto out;
1228 }
1229
1230 inode_set_iversion_raw(inode, cinfo->after);
1231 if (!cinfo->atomic || cinfo->before != change_attr) {
1232 if (S_ISDIR(inode->i_mode))
1233 nfs_force_lookup_revalidate(inode);
1234
1235 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1236 cache_validity |=
1237 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1238 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1239 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1240 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1241 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1242 }
1243 nfsi->attrtimeo_timestamp = jiffies;
1244 nfsi->read_cache_jiffies = timestamp;
1245 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1246 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1247out:
1248 nfs_set_cache_invalid(inode, cache_validity);
1249}
1250
1251void
1252nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1253 unsigned long timestamp, unsigned long cache_validity)
1254{
1255 spin_lock(&dir->i_lock);
1256 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1257 spin_unlock(&dir->i_lock);
1258}
1259
1260struct nfs4_open_createattrs {
1261 struct nfs4_label *label;
1262 struct iattr *sattr;
1263 const __u32 verf[2];
1264};
1265
1266static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1267 int err, struct nfs4_exception *exception)
1268{
1269 if (err != -EINVAL)
1270 return false;
1271 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1272 return false;
1273 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1274 exception->retry = 1;
1275 return true;
1276}
1277
1278static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1279{
1280 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1281}
1282
1283static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1284{
1285 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1286
1287 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1288}
1289
1290static u32
1291nfs4_map_atomic_open_share(struct nfs_server *server,
1292 fmode_t fmode, int openflags)
1293{
1294 u32 res = 0;
1295
1296 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1297 case FMODE_READ:
1298 res = NFS4_SHARE_ACCESS_READ;
1299 break;
1300 case FMODE_WRITE:
1301 res = NFS4_SHARE_ACCESS_WRITE;
1302 break;
1303 case FMODE_READ|FMODE_WRITE:
1304 res = NFS4_SHARE_ACCESS_BOTH;
1305 }
1306 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1307 goto out;
1308 /* Want no delegation if we're using O_DIRECT */
1309 if (openflags & O_DIRECT)
1310 res |= NFS4_SHARE_WANT_NO_DELEG;
1311out:
1312 return res;
1313}
1314
1315static enum open_claim_type4
1316nfs4_map_atomic_open_claim(struct nfs_server *server,
1317 enum open_claim_type4 claim)
1318{
1319 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1320 return claim;
1321 switch (claim) {
1322 default:
1323 return claim;
1324 case NFS4_OPEN_CLAIM_FH:
1325 return NFS4_OPEN_CLAIM_NULL;
1326 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1327 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1328 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1329 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1330 }
1331}
1332
1333static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1334{
1335 p->o_res.f_attr = &p->f_attr;
1336 p->o_res.seqid = p->o_arg.seqid;
1337 p->c_res.seqid = p->c_arg.seqid;
1338 p->o_res.server = p->o_arg.server;
1339 p->o_res.access_request = p->o_arg.access;
1340 nfs_fattr_init(&p->f_attr);
1341 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1342}
1343
1344static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1345 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1346 const struct nfs4_open_createattrs *c,
1347 enum open_claim_type4 claim,
1348 gfp_t gfp_mask)
1349{
1350 struct dentry *parent = dget_parent(dentry);
1351 struct inode *dir = d_inode(parent);
1352 struct nfs_server *server = NFS_SERVER(dir);
1353 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1354 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1355 struct nfs4_opendata *p;
1356
1357 p = kzalloc(sizeof(*p), gfp_mask);
1358 if (p == NULL)
1359 goto err;
1360
1361 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1362 if (IS_ERR(p->f_attr.label))
1363 goto err_free_p;
1364
1365 p->a_label = nfs4_label_alloc(server, gfp_mask);
1366 if (IS_ERR(p->a_label))
1367 goto err_free_f;
1368
1369 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1370 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1371 if (IS_ERR(p->o_arg.seqid))
1372 goto err_free_label;
1373 nfs_sb_active(dentry->d_sb);
1374 p->dentry = dget(dentry);
1375 p->dir = parent;
1376 p->owner = sp;
1377 atomic_inc(&sp->so_count);
1378 p->o_arg.open_flags = flags;
1379 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1380 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1381 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1382 fmode, flags);
1383 if (flags & O_CREAT) {
1384 p->o_arg.umask = current_umask();
1385 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1386 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1387 p->o_arg.u.attrs = &p->attrs;
1388 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1389
1390 memcpy(p->o_arg.u.verifier.data, c->verf,
1391 sizeof(p->o_arg.u.verifier.data));
1392 }
1393 }
1394 /* ask server to check for all possible rights as results
1395 * are cached */
1396 switch (p->o_arg.claim) {
1397 default:
1398 break;
1399 case NFS4_OPEN_CLAIM_NULL:
1400 case NFS4_OPEN_CLAIM_FH:
1401 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1402 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1403 NFS4_ACCESS_EXECUTE |
1404 nfs_access_xattr_mask(server);
1405 }
1406 p->o_arg.clientid = server->nfs_client->cl_clientid;
1407 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1408 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1409 p->o_arg.name = &dentry->d_name;
1410 p->o_arg.server = server;
1411 p->o_arg.bitmask = nfs4_bitmask(server, label);
1412 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1413 switch (p->o_arg.claim) {
1414 case NFS4_OPEN_CLAIM_NULL:
1415 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1416 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1417 p->o_arg.fh = NFS_FH(dir);
1418 break;
1419 case NFS4_OPEN_CLAIM_PREVIOUS:
1420 case NFS4_OPEN_CLAIM_FH:
1421 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1422 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1423 p->o_arg.fh = NFS_FH(d_inode(dentry));
1424 }
1425 p->c_arg.fh = &p->o_res.fh;
1426 p->c_arg.stateid = &p->o_res.stateid;
1427 p->c_arg.seqid = p->o_arg.seqid;
1428 nfs4_init_opendata_res(p);
1429 kref_init(&p->kref);
1430 return p;
1431
1432err_free_label:
1433 nfs4_label_free(p->a_label);
1434err_free_f:
1435 nfs4_label_free(p->f_attr.label);
1436err_free_p:
1437 kfree(p);
1438err:
1439 dput(parent);
1440 return NULL;
1441}
1442
1443static void nfs4_opendata_free(struct kref *kref)
1444{
1445 struct nfs4_opendata *p = container_of(kref,
1446 struct nfs4_opendata, kref);
1447 struct super_block *sb = p->dentry->d_sb;
1448
1449 nfs4_lgopen_release(p->lgp);
1450 nfs_free_seqid(p->o_arg.seqid);
1451 nfs4_sequence_free_slot(&p->o_res.seq_res);
1452 if (p->state != NULL)
1453 nfs4_put_open_state(p->state);
1454 nfs4_put_state_owner(p->owner);
1455
1456 nfs4_label_free(p->a_label);
1457 nfs4_label_free(p->f_attr.label);
1458
1459 dput(p->dir);
1460 dput(p->dentry);
1461 nfs_sb_deactive(sb);
1462 nfs_fattr_free_names(&p->f_attr);
1463 kfree(p->f_attr.mdsthreshold);
1464 kfree(p);
1465}
1466
1467static void nfs4_opendata_put(struct nfs4_opendata *p)
1468{
1469 if (p != NULL)
1470 kref_put(&p->kref, nfs4_opendata_free);
1471}
1472
1473static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1474 fmode_t fmode)
1475{
1476 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1477 case FMODE_READ|FMODE_WRITE:
1478 return state->n_rdwr != 0;
1479 case FMODE_WRITE:
1480 return state->n_wronly != 0;
1481 case FMODE_READ:
1482 return state->n_rdonly != 0;
1483 }
1484 WARN_ON_ONCE(1);
1485 return false;
1486}
1487
1488static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1489 int open_mode, enum open_claim_type4 claim)
1490{
1491 int ret = 0;
1492
1493 if (open_mode & (O_EXCL|O_TRUNC))
1494 goto out;
1495 switch (claim) {
1496 case NFS4_OPEN_CLAIM_NULL:
1497 case NFS4_OPEN_CLAIM_FH:
1498 goto out;
1499 default:
1500 break;
1501 }
1502 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1503 case FMODE_READ:
1504 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1505 && state->n_rdonly != 0;
1506 break;
1507 case FMODE_WRITE:
1508 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1509 && state->n_wronly != 0;
1510 break;
1511 case FMODE_READ|FMODE_WRITE:
1512 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1513 && state->n_rdwr != 0;
1514 }
1515out:
1516 return ret;
1517}
1518
1519static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1520 enum open_claim_type4 claim)
1521{
1522 if (delegation == NULL)
1523 return 0;
1524 if ((delegation->type & fmode) != fmode)
1525 return 0;
1526 switch (claim) {
1527 case NFS4_OPEN_CLAIM_NULL:
1528 case NFS4_OPEN_CLAIM_FH:
1529 break;
1530 case NFS4_OPEN_CLAIM_PREVIOUS:
1531 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1532 break;
1533 fallthrough;
1534 default:
1535 return 0;
1536 }
1537 nfs_mark_delegation_referenced(delegation);
1538 return 1;
1539}
1540
1541static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1542{
1543 switch (fmode) {
1544 case FMODE_WRITE:
1545 state->n_wronly++;
1546 break;
1547 case FMODE_READ:
1548 state->n_rdonly++;
1549 break;
1550 case FMODE_READ|FMODE_WRITE:
1551 state->n_rdwr++;
1552 }
1553 nfs4_state_set_mode_locked(state, state->state | fmode);
1554}
1555
1556#ifdef CONFIG_NFS_V4_1
1557static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1558{
1559 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1560 return true;
1561 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1562 return true;
1563 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1564 return true;
1565 return false;
1566}
1567#endif /* CONFIG_NFS_V4_1 */
1568
1569static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1570{
1571 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1572 wake_up_all(&state->waitq);
1573}
1574
1575static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1576{
1577 struct nfs_client *clp = state->owner->so_server->nfs_client;
1578 bool need_recover = false;
1579
1580 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1581 need_recover = true;
1582 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1583 need_recover = true;
1584 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1585 need_recover = true;
1586 if (need_recover)
1587 nfs4_state_mark_reclaim_nograce(clp, state);
1588}
1589
1590/*
1591 * Check for whether or not the caller may update the open stateid
1592 * to the value passed in by stateid.
1593 *
1594 * Note: This function relies heavily on the server implementing
1595 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1596 * correctly.
1597 * i.e. The stateid seqids have to be initialised to 1, and
1598 * are then incremented on every state transition.
1599 */
1600static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1601 const nfs4_stateid *stateid)
1602{
1603 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1604 /* The common case - we're updating to a new sequence number */
1605 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1606 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1607 return true;
1608 return false;
1609 }
1610 /* The server returned a new stateid */
1611 }
1612 /* This is the first OPEN in this generation */
1613 if (stateid->seqid == cpu_to_be32(1))
1614 return true;
1615 return false;
1616}
1617
1618static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1619{
1620 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1621 return;
1622 if (state->n_wronly)
1623 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1624 if (state->n_rdonly)
1625 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1626 if (state->n_rdwr)
1627 set_bit(NFS_O_RDWR_STATE, &state->flags);
1628 set_bit(NFS_OPEN_STATE, &state->flags);
1629}
1630
1631static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1632 nfs4_stateid *stateid, fmode_t fmode)
1633{
1634 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1635 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1636 case FMODE_WRITE:
1637 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1638 break;
1639 case FMODE_READ:
1640 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1641 break;
1642 case 0:
1643 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1644 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1645 clear_bit(NFS_OPEN_STATE, &state->flags);
1646 }
1647 if (stateid == NULL)
1648 return;
1649 /* Handle OPEN+OPEN_DOWNGRADE races */
1650 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1651 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1652 nfs_resync_open_stateid_locked(state);
1653 goto out;
1654 }
1655 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1656 nfs4_stateid_copy(&state->stateid, stateid);
1657 nfs4_stateid_copy(&state->open_stateid, stateid);
1658 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1659out:
1660 nfs_state_log_update_open_stateid(state);
1661}
1662
1663static void nfs_clear_open_stateid(struct nfs4_state *state,
1664 nfs4_stateid *arg_stateid,
1665 nfs4_stateid *stateid, fmode_t fmode)
1666{
1667 write_seqlock(&state->seqlock);
1668 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1669 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1670 nfs_clear_open_stateid_locked(state, stateid, fmode);
1671 write_sequnlock(&state->seqlock);
1672 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1673 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1674}
1675
1676static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1677 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1678 __must_hold(&state->owner->so_lock)
1679 __must_hold(&state->seqlock)
1680 __must_hold(RCU)
1681
1682{
1683 DEFINE_WAIT(wait);
1684 int status = 0;
1685 for (;;) {
1686
1687 if (nfs_stateid_is_sequential(state, stateid))
1688 break;
1689
1690 if (status)
1691 break;
1692 /* Rely on seqids for serialisation with NFSv4.0 */
1693 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1694 break;
1695
1696 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1697 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1698 /*
1699 * Ensure we process the state changes in the same order
1700 * in which the server processed them by delaying the
1701 * update of the stateid until we are in sequence.
1702 */
1703 write_sequnlock(&state->seqlock);
1704 spin_unlock(&state->owner->so_lock);
1705 rcu_read_unlock();
1706 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1707
1708 if (!fatal_signal_pending(current)) {
1709 if (schedule_timeout(5*HZ) == 0)
1710 status = -EAGAIN;
1711 else
1712 status = 0;
1713 } else
1714 status = -EINTR;
1715 finish_wait(&state->waitq, &wait);
1716 rcu_read_lock();
1717 spin_lock(&state->owner->so_lock);
1718 write_seqlock(&state->seqlock);
1719 }
1720
1721 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1722 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1723 nfs4_stateid_copy(freeme, &state->open_stateid);
1724 nfs_test_and_clear_all_open_stateid(state);
1725 }
1726
1727 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1728 nfs4_stateid_copy(&state->stateid, stateid);
1729 nfs4_stateid_copy(&state->open_stateid, stateid);
1730 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1731 nfs_state_log_update_open_stateid(state);
1732}
1733
1734static void nfs_state_set_open_stateid(struct nfs4_state *state,
1735 const nfs4_stateid *open_stateid,
1736 fmode_t fmode,
1737 nfs4_stateid *freeme)
1738{
1739 /*
1740 * Protect the call to nfs4_state_set_mode_locked and
1741 * serialise the stateid update
1742 */
1743 write_seqlock(&state->seqlock);
1744 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1745 switch (fmode) {
1746 case FMODE_READ:
1747 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1748 break;
1749 case FMODE_WRITE:
1750 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1751 break;
1752 case FMODE_READ|FMODE_WRITE:
1753 set_bit(NFS_O_RDWR_STATE, &state->flags);
1754 }
1755 set_bit(NFS_OPEN_STATE, &state->flags);
1756 write_sequnlock(&state->seqlock);
1757}
1758
1759static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1760{
1761 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1762 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1763 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1764 clear_bit(NFS_OPEN_STATE, &state->flags);
1765}
1766
1767static void nfs_state_set_delegation(struct nfs4_state *state,
1768 const nfs4_stateid *deleg_stateid,
1769 fmode_t fmode)
1770{
1771 /*
1772 * Protect the call to nfs4_state_set_mode_locked and
1773 * serialise the stateid update
1774 */
1775 write_seqlock(&state->seqlock);
1776 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1777 set_bit(NFS_DELEGATED_STATE, &state->flags);
1778 write_sequnlock(&state->seqlock);
1779}
1780
1781static void nfs_state_clear_delegation(struct nfs4_state *state)
1782{
1783 write_seqlock(&state->seqlock);
1784 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1785 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1786 write_sequnlock(&state->seqlock);
1787}
1788
1789int update_open_stateid(struct nfs4_state *state,
1790 const nfs4_stateid *open_stateid,
1791 const nfs4_stateid *delegation,
1792 fmode_t fmode)
1793{
1794 struct nfs_server *server = NFS_SERVER(state->inode);
1795 struct nfs_client *clp = server->nfs_client;
1796 struct nfs_inode *nfsi = NFS_I(state->inode);
1797 struct nfs_delegation *deleg_cur;
1798 nfs4_stateid freeme = { };
1799 int ret = 0;
1800
1801 fmode &= (FMODE_READ|FMODE_WRITE);
1802
1803 rcu_read_lock();
1804 spin_lock(&state->owner->so_lock);
1805 if (open_stateid != NULL) {
1806 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1807 ret = 1;
1808 }
1809
1810 deleg_cur = nfs4_get_valid_delegation(state->inode);
1811 if (deleg_cur == NULL)
1812 goto no_delegation;
1813
1814 spin_lock(&deleg_cur->lock);
1815 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1816 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1817 (deleg_cur->type & fmode) != fmode)
1818 goto no_delegation_unlock;
1819
1820 if (delegation == NULL)
1821 delegation = &deleg_cur->stateid;
1822 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1823 goto no_delegation_unlock;
1824
1825 nfs_mark_delegation_referenced(deleg_cur);
1826 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1827 ret = 1;
1828no_delegation_unlock:
1829 spin_unlock(&deleg_cur->lock);
1830no_delegation:
1831 if (ret)
1832 update_open_stateflags(state, fmode);
1833 spin_unlock(&state->owner->so_lock);
1834 rcu_read_unlock();
1835
1836 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1837 nfs4_schedule_state_manager(clp);
1838 if (freeme.type != 0)
1839 nfs4_test_and_free_stateid(server, &freeme,
1840 state->owner->so_cred);
1841
1842 return ret;
1843}
1844
1845static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1846 const nfs4_stateid *stateid)
1847{
1848 struct nfs4_state *state = lsp->ls_state;
1849 bool ret = false;
1850
1851 spin_lock(&state->state_lock);
1852 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1853 goto out_noupdate;
1854 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1855 goto out_noupdate;
1856 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1857 ret = true;
1858out_noupdate:
1859 spin_unlock(&state->state_lock);
1860 return ret;
1861}
1862
1863static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1864{
1865 struct nfs_delegation *delegation;
1866
1867 fmode &= FMODE_READ|FMODE_WRITE;
1868 rcu_read_lock();
1869 delegation = nfs4_get_valid_delegation(inode);
1870 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1871 rcu_read_unlock();
1872 return;
1873 }
1874 rcu_read_unlock();
1875 nfs4_inode_return_delegation(inode);
1876}
1877
1878static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1879{
1880 struct nfs4_state *state = opendata->state;
1881 struct nfs_delegation *delegation;
1882 int open_mode = opendata->o_arg.open_flags;
1883 fmode_t fmode = opendata->o_arg.fmode;
1884 enum open_claim_type4 claim = opendata->o_arg.claim;
1885 nfs4_stateid stateid;
1886 int ret = -EAGAIN;
1887
1888 for (;;) {
1889 spin_lock(&state->owner->so_lock);
1890 if (can_open_cached(state, fmode, open_mode, claim)) {
1891 update_open_stateflags(state, fmode);
1892 spin_unlock(&state->owner->so_lock);
1893 goto out_return_state;
1894 }
1895 spin_unlock(&state->owner->so_lock);
1896 rcu_read_lock();
1897 delegation = nfs4_get_valid_delegation(state->inode);
1898 if (!can_open_delegated(delegation, fmode, claim)) {
1899 rcu_read_unlock();
1900 break;
1901 }
1902 /* Save the delegation */
1903 nfs4_stateid_copy(&stateid, &delegation->stateid);
1904 rcu_read_unlock();
1905 nfs_release_seqid(opendata->o_arg.seqid);
1906 if (!opendata->is_recover) {
1907 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1908 if (ret != 0)
1909 goto out;
1910 }
1911 ret = -EAGAIN;
1912
1913 /* Try to update the stateid using the delegation */
1914 if (update_open_stateid(state, NULL, &stateid, fmode))
1915 goto out_return_state;
1916 }
1917out:
1918 return ERR_PTR(ret);
1919out_return_state:
1920 refcount_inc(&state->count);
1921 return state;
1922}
1923
1924static void
1925nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1926{
1927 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1928 struct nfs_delegation *delegation;
1929 int delegation_flags = 0;
1930
1931 rcu_read_lock();
1932 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1933 if (delegation)
1934 delegation_flags = delegation->flags;
1935 rcu_read_unlock();
1936 switch (data->o_arg.claim) {
1937 default:
1938 break;
1939 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1940 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1941 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1942 "returning a delegation for "
1943 "OPEN(CLAIM_DELEGATE_CUR)\n",
1944 clp->cl_hostname);
1945 return;
1946 }
1947 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1948 nfs_inode_set_delegation(state->inode,
1949 data->owner->so_cred,
1950 data->o_res.delegation_type,
1951 &data->o_res.delegation,
1952 data->o_res.pagemod_limit);
1953 else
1954 nfs_inode_reclaim_delegation(state->inode,
1955 data->owner->so_cred,
1956 data->o_res.delegation_type,
1957 &data->o_res.delegation,
1958 data->o_res.pagemod_limit);
1959
1960 if (data->o_res.do_recall)
1961 nfs_async_inode_return_delegation(state->inode,
1962 &data->o_res.delegation);
1963}
1964
1965/*
1966 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1967 * and update the nfs4_state.
1968 */
1969static struct nfs4_state *
1970_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1971{
1972 struct inode *inode = data->state->inode;
1973 struct nfs4_state *state = data->state;
1974 int ret;
1975
1976 if (!data->rpc_done) {
1977 if (data->rpc_status)
1978 return ERR_PTR(data->rpc_status);
1979 /* cached opens have already been processed */
1980 goto update;
1981 }
1982
1983 ret = nfs_refresh_inode(inode, &data->f_attr);
1984 if (ret)
1985 return ERR_PTR(ret);
1986
1987 if (data->o_res.delegation_type != 0)
1988 nfs4_opendata_check_deleg(data, state);
1989update:
1990 if (!update_open_stateid(state, &data->o_res.stateid,
1991 NULL, data->o_arg.fmode))
1992 return ERR_PTR(-EAGAIN);
1993 refcount_inc(&state->count);
1994
1995 return state;
1996}
1997
1998static struct inode *
1999nfs4_opendata_get_inode(struct nfs4_opendata *data)
2000{
2001 struct inode *inode;
2002
2003 switch (data->o_arg.claim) {
2004 case NFS4_OPEN_CLAIM_NULL:
2005 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2006 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2007 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2008 return ERR_PTR(-EAGAIN);
2009 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2010 &data->f_attr);
2011 break;
2012 default:
2013 inode = d_inode(data->dentry);
2014 ihold(inode);
2015 nfs_refresh_inode(inode, &data->f_attr);
2016 }
2017 return inode;
2018}
2019
2020static struct nfs4_state *
2021nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2022{
2023 struct nfs4_state *state;
2024 struct inode *inode;
2025
2026 inode = nfs4_opendata_get_inode(data);
2027 if (IS_ERR(inode))
2028 return ERR_CAST(inode);
2029 if (data->state != NULL && data->state->inode == inode) {
2030 state = data->state;
2031 refcount_inc(&state->count);
2032 } else
2033 state = nfs4_get_open_state(inode, data->owner);
2034 iput(inode);
2035 if (state == NULL)
2036 state = ERR_PTR(-ENOMEM);
2037 return state;
2038}
2039
2040static struct nfs4_state *
2041_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2042{
2043 struct nfs4_state *state;
2044
2045 if (!data->rpc_done) {
2046 state = nfs4_try_open_cached(data);
2047 trace_nfs4_cached_open(data->state);
2048 goto out;
2049 }
2050
2051 state = nfs4_opendata_find_nfs4_state(data);
2052 if (IS_ERR(state))
2053 goto out;
2054
2055 if (data->o_res.delegation_type != 0)
2056 nfs4_opendata_check_deleg(data, state);
2057 if (!update_open_stateid(state, &data->o_res.stateid,
2058 NULL, data->o_arg.fmode)) {
2059 nfs4_put_open_state(state);
2060 state = ERR_PTR(-EAGAIN);
2061 }
2062out:
2063 nfs_release_seqid(data->o_arg.seqid);
2064 return state;
2065}
2066
2067static struct nfs4_state *
2068nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2069{
2070 struct nfs4_state *ret;
2071
2072 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2073 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2074 else
2075 ret = _nfs4_opendata_to_nfs4_state(data);
2076 nfs4_sequence_free_slot(&data->o_res.seq_res);
2077 return ret;
2078}
2079
2080static struct nfs_open_context *
2081nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2082{
2083 struct nfs_inode *nfsi = NFS_I(state->inode);
2084 struct nfs_open_context *ctx;
2085
2086 rcu_read_lock();
2087 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2088 if (ctx->state != state)
2089 continue;
2090 if ((ctx->mode & mode) != mode)
2091 continue;
2092 if (!get_nfs_open_context(ctx))
2093 continue;
2094 rcu_read_unlock();
2095 return ctx;
2096 }
2097 rcu_read_unlock();
2098 return ERR_PTR(-ENOENT);
2099}
2100
2101static struct nfs_open_context *
2102nfs4_state_find_open_context(struct nfs4_state *state)
2103{
2104 struct nfs_open_context *ctx;
2105
2106 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2107 if (!IS_ERR(ctx))
2108 return ctx;
2109 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2110 if (!IS_ERR(ctx))
2111 return ctx;
2112 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2113}
2114
2115static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2116 struct nfs4_state *state, enum open_claim_type4 claim)
2117{
2118 struct nfs4_opendata *opendata;
2119
2120 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2121 NULL, claim, GFP_NOFS);
2122 if (opendata == NULL)
2123 return ERR_PTR(-ENOMEM);
2124 opendata->state = state;
2125 refcount_inc(&state->count);
2126 return opendata;
2127}
2128
2129static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2130 fmode_t fmode)
2131{
2132 struct nfs4_state *newstate;
2133 int ret;
2134
2135 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2136 return 0;
2137 opendata->o_arg.open_flags = 0;
2138 opendata->o_arg.fmode = fmode;
2139 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2140 NFS_SB(opendata->dentry->d_sb),
2141 fmode, 0);
2142 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2143 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2144 nfs4_init_opendata_res(opendata);
2145 ret = _nfs4_recover_proc_open(opendata);
2146 if (ret != 0)
2147 return ret;
2148 newstate = nfs4_opendata_to_nfs4_state(opendata);
2149 if (IS_ERR(newstate))
2150 return PTR_ERR(newstate);
2151 if (newstate != opendata->state)
2152 ret = -ESTALE;
2153 nfs4_close_state(newstate, fmode);
2154 return ret;
2155}
2156
2157static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2158{
2159 int ret;
2160
2161 /* memory barrier prior to reading state->n_* */
2162 smp_rmb();
2163 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2164 if (ret != 0)
2165 return ret;
2166 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2167 if (ret != 0)
2168 return ret;
2169 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2170 if (ret != 0)
2171 return ret;
2172 /*
2173 * We may have performed cached opens for all three recoveries.
2174 * Check if we need to update the current stateid.
2175 */
2176 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2177 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2178 write_seqlock(&state->seqlock);
2179 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2180 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2181 write_sequnlock(&state->seqlock);
2182 }
2183 return 0;
2184}
2185
2186/*
2187 * OPEN_RECLAIM:
2188 * reclaim state on the server after a reboot.
2189 */
2190static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2191{
2192 struct nfs_delegation *delegation;
2193 struct nfs4_opendata *opendata;
2194 fmode_t delegation_type = 0;
2195 int status;
2196
2197 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2198 NFS4_OPEN_CLAIM_PREVIOUS);
2199 if (IS_ERR(opendata))
2200 return PTR_ERR(opendata);
2201 rcu_read_lock();
2202 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2203 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2204 delegation_type = delegation->type;
2205 rcu_read_unlock();
2206 opendata->o_arg.u.delegation_type = delegation_type;
2207 status = nfs4_open_recover(opendata, state);
2208 nfs4_opendata_put(opendata);
2209 return status;
2210}
2211
2212static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2213{
2214 struct nfs_server *server = NFS_SERVER(state->inode);
2215 struct nfs4_exception exception = { };
2216 int err;
2217 do {
2218 err = _nfs4_do_open_reclaim(ctx, state);
2219 trace_nfs4_open_reclaim(ctx, 0, err);
2220 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2221 continue;
2222 if (err != -NFS4ERR_DELAY)
2223 break;
2224 nfs4_handle_exception(server, err, &exception);
2225 } while (exception.retry);
2226 return err;
2227}
2228
2229static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2230{
2231 struct nfs_open_context *ctx;
2232 int ret;
2233
2234 ctx = nfs4_state_find_open_context(state);
2235 if (IS_ERR(ctx))
2236 return -EAGAIN;
2237 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2238 nfs_state_clear_open_state_flags(state);
2239 ret = nfs4_do_open_reclaim(ctx, state);
2240 put_nfs_open_context(ctx);
2241 return ret;
2242}
2243
2244static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2245{
2246 switch (err) {
2247 default:
2248 printk(KERN_ERR "NFS: %s: unhandled error "
2249 "%d.\n", __func__, err);
2250 fallthrough;
2251 case 0:
2252 case -ENOENT:
2253 case -EAGAIN:
2254 case -ESTALE:
2255 case -ETIMEDOUT:
2256 break;
2257 case -NFS4ERR_BADSESSION:
2258 case -NFS4ERR_BADSLOT:
2259 case -NFS4ERR_BAD_HIGH_SLOT:
2260 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2261 case -NFS4ERR_DEADSESSION:
2262 return -EAGAIN;
2263 case -NFS4ERR_STALE_CLIENTID:
2264 case -NFS4ERR_STALE_STATEID:
2265 /* Don't recall a delegation if it was lost */
2266 nfs4_schedule_lease_recovery(server->nfs_client);
2267 return -EAGAIN;
2268 case -NFS4ERR_MOVED:
2269 nfs4_schedule_migration_recovery(server);
2270 return -EAGAIN;
2271 case -NFS4ERR_LEASE_MOVED:
2272 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2273 return -EAGAIN;
2274 case -NFS4ERR_DELEG_REVOKED:
2275 case -NFS4ERR_ADMIN_REVOKED:
2276 case -NFS4ERR_EXPIRED:
2277 case -NFS4ERR_BAD_STATEID:
2278 case -NFS4ERR_OPENMODE:
2279 nfs_inode_find_state_and_recover(state->inode,
2280 stateid);
2281 nfs4_schedule_stateid_recovery(server, state);
2282 return -EAGAIN;
2283 case -NFS4ERR_DELAY:
2284 case -NFS4ERR_GRACE:
2285 ssleep(1);
2286 return -EAGAIN;
2287 case -ENOMEM:
2288 case -NFS4ERR_DENIED:
2289 if (fl) {
2290 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2291 if (lsp)
2292 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2293 }
2294 return 0;
2295 }
2296 return err;
2297}
2298
2299int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2300 struct nfs4_state *state, const nfs4_stateid *stateid)
2301{
2302 struct nfs_server *server = NFS_SERVER(state->inode);
2303 struct nfs4_opendata *opendata;
2304 int err = 0;
2305
2306 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2307 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2308 if (IS_ERR(opendata))
2309 return PTR_ERR(opendata);
2310 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2311 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2312 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2313 if (err)
2314 goto out;
2315 }
2316 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2317 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2318 if (err)
2319 goto out;
2320 }
2321 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2322 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2323 if (err)
2324 goto out;
2325 }
2326 nfs_state_clear_delegation(state);
2327out:
2328 nfs4_opendata_put(opendata);
2329 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2330}
2331
2332static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2333{
2334 struct nfs4_opendata *data = calldata;
2335
2336 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2337 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2338}
2339
2340static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2341{
2342 struct nfs4_opendata *data = calldata;
2343
2344 nfs40_sequence_done(task, &data->c_res.seq_res);
2345
2346 data->rpc_status = task->tk_status;
2347 if (data->rpc_status == 0) {
2348 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2349 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2350 renew_lease(data->o_res.server, data->timestamp);
2351 data->rpc_done = true;
2352 }
2353}
2354
2355static void nfs4_open_confirm_release(void *calldata)
2356{
2357 struct nfs4_opendata *data = calldata;
2358 struct nfs4_state *state = NULL;
2359
2360 /* If this request hasn't been cancelled, do nothing */
2361 if (!data->cancelled)
2362 goto out_free;
2363 /* In case of error, no cleanup! */
2364 if (!data->rpc_done)
2365 goto out_free;
2366 state = nfs4_opendata_to_nfs4_state(data);
2367 if (!IS_ERR(state))
2368 nfs4_close_state(state, data->o_arg.fmode);
2369out_free:
2370 nfs4_opendata_put(data);
2371}
2372
2373static const struct rpc_call_ops nfs4_open_confirm_ops = {
2374 .rpc_call_prepare = nfs4_open_confirm_prepare,
2375 .rpc_call_done = nfs4_open_confirm_done,
2376 .rpc_release = nfs4_open_confirm_release,
2377};
2378
2379/*
2380 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2381 */
2382static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2383{
2384 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2385 struct rpc_task *task;
2386 struct rpc_message msg = {
2387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2388 .rpc_argp = &data->c_arg,
2389 .rpc_resp = &data->c_res,
2390 .rpc_cred = data->owner->so_cred,
2391 };
2392 struct rpc_task_setup task_setup_data = {
2393 .rpc_client = server->client,
2394 .rpc_message = &msg,
2395 .callback_ops = &nfs4_open_confirm_ops,
2396 .callback_data = data,
2397 .workqueue = nfsiod_workqueue,
2398 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2399 };
2400 int status;
2401
2402 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2403 data->is_recover);
2404 kref_get(&data->kref);
2405 data->rpc_done = false;
2406 data->rpc_status = 0;
2407 data->timestamp = jiffies;
2408 task = rpc_run_task(&task_setup_data);
2409 if (IS_ERR(task))
2410 return PTR_ERR(task);
2411 status = rpc_wait_for_completion_task(task);
2412 if (status != 0) {
2413 data->cancelled = true;
2414 smp_wmb();
2415 } else
2416 status = data->rpc_status;
2417 rpc_put_task(task);
2418 return status;
2419}
2420
2421static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2422{
2423 struct nfs4_opendata *data = calldata;
2424 struct nfs4_state_owner *sp = data->owner;
2425 struct nfs_client *clp = sp->so_server->nfs_client;
2426 enum open_claim_type4 claim = data->o_arg.claim;
2427
2428 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2429 goto out_wait;
2430 /*
2431 * Check if we still need to send an OPEN call, or if we can use
2432 * a delegation instead.
2433 */
2434 if (data->state != NULL) {
2435 struct nfs_delegation *delegation;
2436
2437 if (can_open_cached(data->state, data->o_arg.fmode,
2438 data->o_arg.open_flags, claim))
2439 goto out_no_action;
2440 rcu_read_lock();
2441 delegation = nfs4_get_valid_delegation(data->state->inode);
2442 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2443 goto unlock_no_action;
2444 rcu_read_unlock();
2445 }
2446 /* Update client id. */
2447 data->o_arg.clientid = clp->cl_clientid;
2448 switch (claim) {
2449 default:
2450 break;
2451 case NFS4_OPEN_CLAIM_PREVIOUS:
2452 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2453 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2454 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2455 fallthrough;
2456 case NFS4_OPEN_CLAIM_FH:
2457 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2458 }
2459 data->timestamp = jiffies;
2460 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2461 &data->o_arg.seq_args,
2462 &data->o_res.seq_res,
2463 task) != 0)
2464 nfs_release_seqid(data->o_arg.seqid);
2465
2466 /* Set the create mode (note dependency on the session type) */
2467 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2468 if (data->o_arg.open_flags & O_EXCL) {
2469 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2470 if (clp->cl_mvops->minor_version == 0) {
2471 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2472 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2473 * because ACCESS will return permission denied for
2474 * all bits until close */
2475 data->o_res.access_request = data->o_arg.access = 0;
2476 } else if (nfs4_has_persistent_session(clp))
2477 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2478 }
2479 return;
2480unlock_no_action:
2481 trace_nfs4_cached_open(data->state);
2482 rcu_read_unlock();
2483out_no_action:
2484 task->tk_action = NULL;
2485out_wait:
2486 nfs4_sequence_done(task, &data->o_res.seq_res);
2487}
2488
2489static void nfs4_open_done(struct rpc_task *task, void *calldata)
2490{
2491 struct nfs4_opendata *data = calldata;
2492
2493 data->rpc_status = task->tk_status;
2494
2495 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2496 return;
2497
2498 if (task->tk_status == 0) {
2499 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2500 switch (data->o_res.f_attr->mode & S_IFMT) {
2501 case S_IFREG:
2502 break;
2503 case S_IFLNK:
2504 data->rpc_status = -ELOOP;
2505 break;
2506 case S_IFDIR:
2507 data->rpc_status = -EISDIR;
2508 break;
2509 default:
2510 data->rpc_status = -ENOTDIR;
2511 }
2512 }
2513 renew_lease(data->o_res.server, data->timestamp);
2514 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2515 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2516 }
2517 data->rpc_done = true;
2518}
2519
2520static void nfs4_open_release(void *calldata)
2521{
2522 struct nfs4_opendata *data = calldata;
2523 struct nfs4_state *state = NULL;
2524
2525 /* If this request hasn't been cancelled, do nothing */
2526 if (!data->cancelled)
2527 goto out_free;
2528 /* In case of error, no cleanup! */
2529 if (data->rpc_status != 0 || !data->rpc_done)
2530 goto out_free;
2531 /* In case we need an open_confirm, no cleanup! */
2532 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2533 goto out_free;
2534 state = nfs4_opendata_to_nfs4_state(data);
2535 if (!IS_ERR(state))
2536 nfs4_close_state(state, data->o_arg.fmode);
2537out_free:
2538 nfs4_opendata_put(data);
2539}
2540
2541static const struct rpc_call_ops nfs4_open_ops = {
2542 .rpc_call_prepare = nfs4_open_prepare,
2543 .rpc_call_done = nfs4_open_done,
2544 .rpc_release = nfs4_open_release,
2545};
2546
2547static int nfs4_run_open_task(struct nfs4_opendata *data,
2548 struct nfs_open_context *ctx)
2549{
2550 struct inode *dir = d_inode(data->dir);
2551 struct nfs_server *server = NFS_SERVER(dir);
2552 struct nfs_openargs *o_arg = &data->o_arg;
2553 struct nfs_openres *o_res = &data->o_res;
2554 struct rpc_task *task;
2555 struct rpc_message msg = {
2556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2557 .rpc_argp = o_arg,
2558 .rpc_resp = o_res,
2559 .rpc_cred = data->owner->so_cred,
2560 };
2561 struct rpc_task_setup task_setup_data = {
2562 .rpc_client = server->client,
2563 .rpc_message = &msg,
2564 .callback_ops = &nfs4_open_ops,
2565 .callback_data = data,
2566 .workqueue = nfsiod_workqueue,
2567 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2568 };
2569 int status;
2570
2571 if (server->nfs_client->cl_minorversion)
2572 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2573
2574 kref_get(&data->kref);
2575 data->rpc_done = false;
2576 data->rpc_status = 0;
2577 data->cancelled = false;
2578 data->is_recover = false;
2579 if (!ctx) {
2580 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2581 data->is_recover = true;
2582 task_setup_data.flags |= RPC_TASK_TIMEOUT;
2583 } else {
2584 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2585 pnfs_lgopen_prepare(data, ctx);
2586 }
2587 task = rpc_run_task(&task_setup_data);
2588 if (IS_ERR(task))
2589 return PTR_ERR(task);
2590 status = rpc_wait_for_completion_task(task);
2591 if (status != 0) {
2592 data->cancelled = true;
2593 smp_wmb();
2594 } else
2595 status = data->rpc_status;
2596 rpc_put_task(task);
2597
2598 return status;
2599}
2600
2601static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2602{
2603 struct inode *dir = d_inode(data->dir);
2604 struct nfs_openres *o_res = &data->o_res;
2605 int status;
2606
2607 status = nfs4_run_open_task(data, NULL);
2608 if (status != 0 || !data->rpc_done)
2609 return status;
2610
2611 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2612
2613 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2614 status = _nfs4_proc_open_confirm(data);
2615
2616 return status;
2617}
2618
2619/*
2620 * Additional permission checks in order to distinguish between an
2621 * open for read, and an open for execute. This works around the
2622 * fact that NFSv4 OPEN treats read and execute permissions as being
2623 * the same.
2624 * Note that in the non-execute case, we want to turn off permission
2625 * checking if we just created a new file (POSIX open() semantics).
2626 */
2627static int nfs4_opendata_access(const struct cred *cred,
2628 struct nfs4_opendata *opendata,
2629 struct nfs4_state *state, fmode_t fmode,
2630 int openflags)
2631{
2632 struct nfs_access_entry cache;
2633 u32 mask, flags;
2634
2635 /* access call failed or for some reason the server doesn't
2636 * support any access modes -- defer access call until later */
2637 if (opendata->o_res.access_supported == 0)
2638 return 0;
2639
2640 mask = 0;
2641 /*
2642 * Use openflags to check for exec, because fmode won't
2643 * always have FMODE_EXEC set when file open for exec.
2644 */
2645 if (openflags & __FMODE_EXEC) {
2646 /* ONLY check for exec rights */
2647 if (S_ISDIR(state->inode->i_mode))
2648 mask = NFS4_ACCESS_LOOKUP;
2649 else
2650 mask = NFS4_ACCESS_EXECUTE;
2651 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2652 mask = NFS4_ACCESS_READ;
2653
2654 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2655 nfs_access_add_cache(state->inode, &cache, cred);
2656
2657 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2658 if ((mask & ~cache.mask & flags) == 0)
2659 return 0;
2660
2661 return -EACCES;
2662}
2663
2664/*
2665 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2666 */
2667static int _nfs4_proc_open(struct nfs4_opendata *data,
2668 struct nfs_open_context *ctx)
2669{
2670 struct inode *dir = d_inode(data->dir);
2671 struct nfs_server *server = NFS_SERVER(dir);
2672 struct nfs_openargs *o_arg = &data->o_arg;
2673 struct nfs_openres *o_res = &data->o_res;
2674 int status;
2675
2676 status = nfs4_run_open_task(data, ctx);
2677 if (!data->rpc_done)
2678 return status;
2679 if (status != 0) {
2680 if (status == -NFS4ERR_BADNAME &&
2681 !(o_arg->open_flags & O_CREAT))
2682 return -ENOENT;
2683 return status;
2684 }
2685
2686 nfs_fattr_map_and_free_names(server, &data->f_attr);
2687
2688 if (o_arg->open_flags & O_CREAT) {
2689 if (o_arg->open_flags & O_EXCL)
2690 data->file_created = true;
2691 else if (o_res->cinfo.before != o_res->cinfo.after)
2692 data->file_created = true;
2693 if (data->file_created ||
2694 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2695 nfs4_update_changeattr(dir, &o_res->cinfo,
2696 o_res->f_attr->time_start,
2697 NFS_INO_INVALID_DATA);
2698 }
2699 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2700 server->caps &= ~NFS_CAP_POSIX_LOCK;
2701 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2702 status = _nfs4_proc_open_confirm(data);
2703 if (status != 0)
2704 return status;
2705 }
2706 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2707 nfs4_sequence_free_slot(&o_res->seq_res);
2708 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
2709 }
2710 return 0;
2711}
2712
2713/*
2714 * OPEN_EXPIRED:
2715 * reclaim state on the server after a network partition.
2716 * Assumes caller holds the appropriate lock
2717 */
2718static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2719{
2720 struct nfs4_opendata *opendata;
2721 int ret;
2722
2723 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2724 NFS4_OPEN_CLAIM_FH);
2725 if (IS_ERR(opendata))
2726 return PTR_ERR(opendata);
2727 ret = nfs4_open_recover(opendata, state);
2728 if (ret == -ESTALE)
2729 d_drop(ctx->dentry);
2730 nfs4_opendata_put(opendata);
2731 return ret;
2732}
2733
2734static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2735{
2736 struct nfs_server *server = NFS_SERVER(state->inode);
2737 struct nfs4_exception exception = { };
2738 int err;
2739
2740 do {
2741 err = _nfs4_open_expired(ctx, state);
2742 trace_nfs4_open_expired(ctx, 0, err);
2743 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2744 continue;
2745 switch (err) {
2746 default:
2747 goto out;
2748 case -NFS4ERR_GRACE:
2749 case -NFS4ERR_DELAY:
2750 nfs4_handle_exception(server, err, &exception);
2751 err = 0;
2752 }
2753 } while (exception.retry);
2754out:
2755 return err;
2756}
2757
2758static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2759{
2760 struct nfs_open_context *ctx;
2761 int ret;
2762
2763 ctx = nfs4_state_find_open_context(state);
2764 if (IS_ERR(ctx))
2765 return -EAGAIN;
2766 ret = nfs4_do_open_expired(ctx, state);
2767 put_nfs_open_context(ctx);
2768 return ret;
2769}
2770
2771static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2772 const nfs4_stateid *stateid)
2773{
2774 nfs_remove_bad_delegation(state->inode, stateid);
2775 nfs_state_clear_delegation(state);
2776}
2777
2778static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2779{
2780 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2781 nfs_finish_clear_delegation_stateid(state, NULL);
2782}
2783
2784static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2785{
2786 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2787 nfs40_clear_delegation_stateid(state);
2788 nfs_state_clear_open_state_flags(state);
2789 return nfs4_open_expired(sp, state);
2790}
2791
2792static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2793 nfs4_stateid *stateid,
2794 const struct cred *cred)
2795{
2796 return -NFS4ERR_BAD_STATEID;
2797}
2798
2799#if defined(CONFIG_NFS_V4_1)
2800static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2801 nfs4_stateid *stateid,
2802 const struct cred *cred)
2803{
2804 int status;
2805
2806 switch (stateid->type) {
2807 default:
2808 break;
2809 case NFS4_INVALID_STATEID_TYPE:
2810 case NFS4_SPECIAL_STATEID_TYPE:
2811 return -NFS4ERR_BAD_STATEID;
2812 case NFS4_REVOKED_STATEID_TYPE:
2813 goto out_free;
2814 }
2815
2816 status = nfs41_test_stateid(server, stateid, cred);
2817 switch (status) {
2818 case -NFS4ERR_EXPIRED:
2819 case -NFS4ERR_ADMIN_REVOKED:
2820 case -NFS4ERR_DELEG_REVOKED:
2821 break;
2822 default:
2823 return status;
2824 }
2825out_free:
2826 /* Ack the revoked state to the server */
2827 nfs41_free_stateid(server, stateid, cred, true);
2828 return -NFS4ERR_EXPIRED;
2829}
2830
2831static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2832{
2833 struct nfs_server *server = NFS_SERVER(state->inode);
2834 nfs4_stateid stateid;
2835 struct nfs_delegation *delegation;
2836 const struct cred *cred = NULL;
2837 int status, ret = NFS_OK;
2838
2839 /* Get the delegation credential for use by test/free_stateid */
2840 rcu_read_lock();
2841 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2842 if (delegation == NULL) {
2843 rcu_read_unlock();
2844 nfs_state_clear_delegation(state);
2845 return NFS_OK;
2846 }
2847
2848 spin_lock(&delegation->lock);
2849 nfs4_stateid_copy(&stateid, &delegation->stateid);
2850
2851 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2852 &delegation->flags)) {
2853 spin_unlock(&delegation->lock);
2854 rcu_read_unlock();
2855 return NFS_OK;
2856 }
2857
2858 if (delegation->cred)
2859 cred = get_cred(delegation->cred);
2860 spin_unlock(&delegation->lock);
2861 rcu_read_unlock();
2862 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2863 trace_nfs4_test_delegation_stateid(state, NULL, status);
2864 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2865 nfs_finish_clear_delegation_stateid(state, &stateid);
2866 else
2867 ret = status;
2868
2869 put_cred(cred);
2870 return ret;
2871}
2872
2873static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2874{
2875 nfs4_stateid tmp;
2876
2877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2878 nfs4_copy_delegation_stateid(state->inode, state->state,
2879 &tmp, NULL) &&
2880 nfs4_stateid_match_other(&state->stateid, &tmp))
2881 nfs_state_set_delegation(state, &tmp, state->state);
2882 else
2883 nfs_state_clear_delegation(state);
2884}
2885
2886/**
2887 * nfs41_check_expired_locks - possibly free a lock stateid
2888 *
2889 * @state: NFSv4 state for an inode
2890 *
2891 * Returns NFS_OK if recovery for this stateid is now finished.
2892 * Otherwise a negative NFS4ERR value is returned.
2893 */
2894static int nfs41_check_expired_locks(struct nfs4_state *state)
2895{
2896 int status, ret = NFS_OK;
2897 struct nfs4_lock_state *lsp, *prev = NULL;
2898 struct nfs_server *server = NFS_SERVER(state->inode);
2899
2900 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2901 goto out;
2902
2903 spin_lock(&state->state_lock);
2904 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2905 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2906 const struct cred *cred = lsp->ls_state->owner->so_cred;
2907
2908 refcount_inc(&lsp->ls_count);
2909 spin_unlock(&state->state_lock);
2910
2911 nfs4_put_lock_state(prev);
2912 prev = lsp;
2913
2914 status = nfs41_test_and_free_expired_stateid(server,
2915 &lsp->ls_stateid,
2916 cred);
2917 trace_nfs4_test_lock_stateid(state, lsp, status);
2918 if (status == -NFS4ERR_EXPIRED ||
2919 status == -NFS4ERR_BAD_STATEID) {
2920 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2921 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2922 if (!recover_lost_locks)
2923 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2924 } else if (status != NFS_OK) {
2925 ret = status;
2926 nfs4_put_lock_state(prev);
2927 goto out;
2928 }
2929 spin_lock(&state->state_lock);
2930 }
2931 }
2932 spin_unlock(&state->state_lock);
2933 nfs4_put_lock_state(prev);
2934out:
2935 return ret;
2936}
2937
2938/**
2939 * nfs41_check_open_stateid - possibly free an open stateid
2940 *
2941 * @state: NFSv4 state for an inode
2942 *
2943 * Returns NFS_OK if recovery for this stateid is now finished.
2944 * Otherwise a negative NFS4ERR value is returned.
2945 */
2946static int nfs41_check_open_stateid(struct nfs4_state *state)
2947{
2948 struct nfs_server *server = NFS_SERVER(state->inode);
2949 nfs4_stateid *stateid = &state->open_stateid;
2950 const struct cred *cred = state->owner->so_cred;
2951 int status;
2952
2953 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
2954 return -NFS4ERR_BAD_STATEID;
2955 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2956 trace_nfs4_test_open_stateid(state, NULL, status);
2957 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2958 nfs_state_clear_open_state_flags(state);
2959 stateid->type = NFS4_INVALID_STATEID_TYPE;
2960 return status;
2961 }
2962 if (nfs_open_stateid_recover_openmode(state))
2963 return -NFS4ERR_OPENMODE;
2964 return NFS_OK;
2965}
2966
2967static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2968{
2969 int status;
2970
2971 status = nfs41_check_delegation_stateid(state);
2972 if (status != NFS_OK)
2973 return status;
2974 nfs41_delegation_recover_stateid(state);
2975
2976 status = nfs41_check_expired_locks(state);
2977 if (status != NFS_OK)
2978 return status;
2979 status = nfs41_check_open_stateid(state);
2980 if (status != NFS_OK)
2981 status = nfs4_open_expired(sp, state);
2982 return status;
2983}
2984#endif
2985
2986/*
2987 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2988 * fields corresponding to attributes that were used to store the verifier.
2989 * Make sure we clobber those fields in the later setattr call
2990 */
2991static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2992 struct iattr *sattr, struct nfs4_label **label)
2993{
2994 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2995 __u32 attrset[3];
2996 unsigned ret;
2997 unsigned i;
2998
2999 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3000 attrset[i] = opendata->o_res.attrset[i];
3001 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3002 attrset[i] &= ~bitmask[i];
3003 }
3004
3005 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3006 sattr->ia_valid : 0;
3007
3008 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3009 if (sattr->ia_valid & ATTR_ATIME_SET)
3010 ret |= ATTR_ATIME_SET;
3011 else
3012 ret |= ATTR_ATIME;
3013 }
3014
3015 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3016 if (sattr->ia_valid & ATTR_MTIME_SET)
3017 ret |= ATTR_MTIME_SET;
3018 else
3019 ret |= ATTR_MTIME;
3020 }
3021
3022 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3023 *label = NULL;
3024 return ret;
3025}
3026
3027static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3028 int flags, struct nfs_open_context *ctx)
3029{
3030 struct nfs4_state_owner *sp = opendata->owner;
3031 struct nfs_server *server = sp->so_server;
3032 struct dentry *dentry;
3033 struct nfs4_state *state;
3034 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3035 struct inode *dir = d_inode(opendata->dir);
3036 unsigned long dir_verifier;
3037 unsigned int seq;
3038 int ret;
3039
3040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
3041 dir_verifier = nfs_save_change_attribute(dir);
3042
3043 ret = _nfs4_proc_open(opendata, ctx);
3044 if (ret != 0)
3045 goto out;
3046
3047 state = _nfs4_opendata_to_nfs4_state(opendata);
3048 ret = PTR_ERR(state);
3049 if (IS_ERR(state))
3050 goto out;
3051 ctx->state = state;
3052 if (server->caps & NFS_CAP_POSIX_LOCK)
3053 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3054 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3055 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3056 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3057 set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3058
3059 dentry = opendata->dentry;
3060 if (d_really_is_negative(dentry)) {
3061 struct dentry *alias;
3062 d_drop(dentry);
3063 alias = d_exact_alias(dentry, state->inode);
3064 if (!alias)
3065 alias = d_splice_alias(igrab(state->inode), dentry);
3066 /* d_splice_alias() can't fail here - it's a non-directory */
3067 if (alias) {
3068 dput(ctx->dentry);
3069 ctx->dentry = dentry = alias;
3070 }
3071 }
3072
3073 switch(opendata->o_arg.claim) {
3074 default:
3075 break;
3076 case NFS4_OPEN_CLAIM_NULL:
3077 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3078 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3079 if (!opendata->rpc_done)
3080 break;
3081 if (opendata->o_res.delegation_type != 0)
3082 dir_verifier = nfs_save_change_attribute(dir);
3083 nfs_set_verifier(dentry, dir_verifier);
3084 }
3085
3086 /* Parse layoutget results before we check for access */
3087 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3088
3089 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3090 acc_mode, flags);
3091 if (ret != 0)
3092 goto out;
3093
3094 if (d_inode(dentry) == state->inode) {
3095 nfs_inode_attach_open_context(ctx);
3096 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3097 nfs4_schedule_stateid_recovery(server, state);
3098 }
3099
3100out:
3101 if (opendata->lgp) {
3102 nfs4_lgopen_release(opendata->lgp);
3103 opendata->lgp = NULL;
3104 }
3105 if (!opendata->cancelled)
3106 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3107 return ret;
3108}
3109
3110/*
3111 * Returns a referenced nfs4_state
3112 */
3113static int _nfs4_do_open(struct inode *dir,
3114 struct nfs_open_context *ctx,
3115 int flags,
3116 const struct nfs4_open_createattrs *c,
3117 int *opened)
3118{
3119 struct nfs4_state_owner *sp;
3120 struct nfs4_state *state = NULL;
3121 struct nfs_server *server = NFS_SERVER(dir);
3122 struct nfs4_opendata *opendata;
3123 struct dentry *dentry = ctx->dentry;
3124 const struct cred *cred = ctx->cred;
3125 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3126 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3127 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3128 struct iattr *sattr = c->sattr;
3129 struct nfs4_label *label = c->label;
3130 int status;
3131
3132 /* Protect against reboot recovery conflicts */
3133 status = -ENOMEM;
3134 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3135 if (sp == NULL) {
3136 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3137 goto out_err;
3138 }
3139 status = nfs4_client_recover_expired_lease(server->nfs_client);
3140 if (status != 0)
3141 goto err_put_state_owner;
3142 if (d_really_is_positive(dentry))
3143 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3144 status = -ENOMEM;
3145 if (d_really_is_positive(dentry))
3146 claim = NFS4_OPEN_CLAIM_FH;
3147 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3148 c, claim, GFP_KERNEL);
3149 if (opendata == NULL)
3150 goto err_put_state_owner;
3151
3152 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3153 if (!opendata->f_attr.mdsthreshold) {
3154 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3155 if (!opendata->f_attr.mdsthreshold)
3156 goto err_opendata_put;
3157 }
3158 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3159 }
3160 if (d_really_is_positive(dentry))
3161 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3162
3163 status = _nfs4_open_and_get_state(opendata, flags, ctx);
3164 if (status != 0)
3165 goto err_opendata_put;
3166 state = ctx->state;
3167
3168 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3169 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3170 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3171 /*
3172 * send create attributes which was not set by open
3173 * with an extra setattr.
3174 */
3175 if (attrs || label) {
3176 unsigned ia_old = sattr->ia_valid;
3177
3178 sattr->ia_valid = attrs;
3179 nfs_fattr_init(opendata->o_res.f_attr);
3180 status = nfs4_do_setattr(state->inode, cred,
3181 opendata->o_res.f_attr, sattr,
3182 ctx, label);
3183 if (status == 0) {
3184 nfs_setattr_update_inode(state->inode, sattr,
3185 opendata->o_res.f_attr);
3186 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3187 }
3188 sattr->ia_valid = ia_old;
3189 }
3190 }
3191 if (opened && opendata->file_created)
3192 *opened = 1;
3193
3194 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3195 *ctx_th = opendata->f_attr.mdsthreshold;
3196 opendata->f_attr.mdsthreshold = NULL;
3197 }
3198
3199 nfs4_opendata_put(opendata);
3200 nfs4_put_state_owner(sp);
3201 return 0;
3202err_opendata_put:
3203 nfs4_opendata_put(opendata);
3204err_put_state_owner:
3205 nfs4_put_state_owner(sp);
3206out_err:
3207 return status;
3208}
3209
3210
3211static struct nfs4_state *nfs4_do_open(struct inode *dir,
3212 struct nfs_open_context *ctx,
3213 int flags,
3214 struct iattr *sattr,
3215 struct nfs4_label *label,
3216 int *opened)
3217{
3218 struct nfs_server *server = NFS_SERVER(dir);
3219 struct nfs4_exception exception = {
3220 .interruptible = true,
3221 };
3222 struct nfs4_state *res;
3223 struct nfs4_open_createattrs c = {
3224 .label = label,
3225 .sattr = sattr,
3226 .verf = {
3227 [0] = (__u32)jiffies,
3228 [1] = (__u32)current->pid,
3229 },
3230 };
3231 int status;
3232
3233 do {
3234 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3235 res = ctx->state;
3236 trace_nfs4_open_file(ctx, flags, status);
3237 if (status == 0)
3238 break;
3239 /* NOTE: BAD_SEQID means the server and client disagree about the
3240 * book-keeping w.r.t. state-changing operations
3241 * (OPEN/CLOSE/LOCK/LOCKU...)
3242 * It is actually a sign of a bug on the client or on the server.
3243 *
3244 * If we receive a BAD_SEQID error in the particular case of
3245 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3246 * have unhashed the old state_owner for us, and that we can
3247 * therefore safely retry using a new one. We should still warn
3248 * the user though...
3249 */
3250 if (status == -NFS4ERR_BAD_SEQID) {
3251 pr_warn_ratelimited("NFS: v4 server %s "
3252 " returned a bad sequence-id error!\n",
3253 NFS_SERVER(dir)->nfs_client->cl_hostname);
3254 exception.retry = 1;
3255 continue;
3256 }
3257 /*
3258 * BAD_STATEID on OPEN means that the server cancelled our
3259 * state before it received the OPEN_CONFIRM.
3260 * Recover by retrying the request as per the discussion
3261 * on Page 181 of RFC3530.
3262 */
3263 if (status == -NFS4ERR_BAD_STATEID) {
3264 exception.retry = 1;
3265 continue;
3266 }
3267 if (status == -NFS4ERR_EXPIRED) {
3268 nfs4_schedule_lease_recovery(server->nfs_client);
3269 exception.retry = 1;
3270 continue;
3271 }
3272 if (status == -EAGAIN) {
3273 /* We must have found a delegation */
3274 exception.retry = 1;
3275 continue;
3276 }
3277 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3278 continue;
3279 res = ERR_PTR(nfs4_handle_exception(server,
3280 status, &exception));
3281 } while (exception.retry);
3282 return res;
3283}
3284
3285static int _nfs4_do_setattr(struct inode *inode,
3286 struct nfs_setattrargs *arg,
3287 struct nfs_setattrres *res,
3288 const struct cred *cred,
3289 struct nfs_open_context *ctx)
3290{
3291 struct nfs_server *server = NFS_SERVER(inode);
3292 struct rpc_message msg = {
3293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3294 .rpc_argp = arg,
3295 .rpc_resp = res,
3296 .rpc_cred = cred,
3297 };
3298 const struct cred *delegation_cred = NULL;
3299 unsigned long timestamp = jiffies;
3300 bool truncate;
3301 int status;
3302
3303 nfs_fattr_init(res->fattr);
3304
3305 /* Servers should only apply open mode checks for file size changes */
3306 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3307 if (!truncate) {
3308 nfs4_inode_make_writeable(inode);
3309 goto zero_stateid;
3310 }
3311
3312 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3313 /* Use that stateid */
3314 } else if (ctx != NULL && ctx->state) {
3315 struct nfs_lock_context *l_ctx;
3316 if (!nfs4_valid_open_stateid(ctx->state))
3317 return -EBADF;
3318 l_ctx = nfs_get_lock_context(ctx);
3319 if (IS_ERR(l_ctx))
3320 return PTR_ERR(l_ctx);
3321 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3322 &arg->stateid, &delegation_cred);
3323 nfs_put_lock_context(l_ctx);
3324 if (status == -EIO)
3325 return -EBADF;
3326 else if (status == -EAGAIN)
3327 goto zero_stateid;
3328 } else {
3329zero_stateid:
3330 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3331 }
3332 if (delegation_cred)
3333 msg.rpc_cred = delegation_cred;
3334
3335 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3336
3337 put_cred(delegation_cred);
3338 if (status == 0 && ctx != NULL)
3339 renew_lease(server, timestamp);
3340 trace_nfs4_setattr(inode, &arg->stateid, status);
3341 return status;
3342}
3343
3344static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3345 struct nfs_fattr *fattr, struct iattr *sattr,
3346 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3347{
3348 struct nfs_server *server = NFS_SERVER(inode);
3349 __u32 bitmask[NFS4_BITMASK_SZ];
3350 struct nfs4_state *state = ctx ? ctx->state : NULL;
3351 struct nfs_setattrargs arg = {
3352 .fh = NFS_FH(inode),
3353 .iap = sattr,
3354 .server = server,
3355 .bitmask = bitmask,
3356 .label = ilabel,
3357 };
3358 struct nfs_setattrres res = {
3359 .fattr = fattr,
3360 .server = server,
3361 };
3362 struct nfs4_exception exception = {
3363 .state = state,
3364 .inode = inode,
3365 .stateid = &arg.stateid,
3366 };
3367 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
3368 int err;
3369
3370 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3371 adjust_flags |= NFS_INO_INVALID_MODE;
3372 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3373 adjust_flags |= NFS_INO_INVALID_OTHER;
3374
3375 do {
3376 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3377 inode, adjust_flags);
3378
3379 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3380 switch (err) {
3381 case -NFS4ERR_OPENMODE:
3382 if (!(sattr->ia_valid & ATTR_SIZE)) {
3383 pr_warn_once("NFSv4: server %s is incorrectly "
3384 "applying open mode checks to "
3385 "a SETATTR that is not "
3386 "changing file size.\n",
3387 server->nfs_client->cl_hostname);
3388 }
3389 if (state && !(state->state & FMODE_WRITE)) {
3390 err = -EBADF;
3391 if (sattr->ia_valid & ATTR_OPEN)
3392 err = -EACCES;
3393 goto out;
3394 }
3395 }
3396 err = nfs4_handle_exception(server, err, &exception);
3397 } while (exception.retry);
3398out:
3399 return err;
3400}
3401
3402static bool
3403nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3404{
3405 if (inode == NULL || !nfs_have_layout(inode))
3406 return false;
3407
3408 return pnfs_wait_on_layoutreturn(inode, task);
3409}
3410
3411/*
3412 * Update the seqid of an open stateid
3413 */
3414static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3415 struct nfs4_state *state)
3416{
3417 __be32 seqid_open;
3418 u32 dst_seqid;
3419 int seq;
3420
3421 for (;;) {
3422 if (!nfs4_valid_open_stateid(state))
3423 break;
3424 seq = read_seqbegin(&state->seqlock);
3425 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3426 nfs4_stateid_copy(dst, &state->open_stateid);
3427 if (read_seqretry(&state->seqlock, seq))
3428 continue;
3429 break;
3430 }
3431 seqid_open = state->open_stateid.seqid;
3432 if (read_seqretry(&state->seqlock, seq))
3433 continue;
3434
3435 dst_seqid = be32_to_cpu(dst->seqid);
3436 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3437 dst->seqid = seqid_open;
3438 break;
3439 }
3440}
3441
3442/*
3443 * Update the seqid of an open stateid after receiving
3444 * NFS4ERR_OLD_STATEID
3445 */
3446static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3447 struct nfs4_state *state)
3448{
3449 __be32 seqid_open;
3450 u32 dst_seqid;
3451 bool ret;
3452 int seq, status = -EAGAIN;
3453 DEFINE_WAIT(wait);
3454
3455 for (;;) {
3456 ret = false;
3457 if (!nfs4_valid_open_stateid(state))
3458 break;
3459 seq = read_seqbegin(&state->seqlock);
3460 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3461 if (read_seqretry(&state->seqlock, seq))
3462 continue;
3463 break;
3464 }
3465
3466 write_seqlock(&state->seqlock);
3467 seqid_open = state->open_stateid.seqid;
3468
3469 dst_seqid = be32_to_cpu(dst->seqid);
3470
3471 /* Did another OPEN bump the state's seqid? try again: */
3472 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3473 dst->seqid = seqid_open;
3474 write_sequnlock(&state->seqlock);
3475 ret = true;
3476 break;
3477 }
3478
3479 /* server says we're behind but we haven't seen the update yet */
3480 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3481 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3482 write_sequnlock(&state->seqlock);
3483 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3484
3485 if (fatal_signal_pending(current))
3486 status = -EINTR;
3487 else
3488 if (schedule_timeout(5*HZ) != 0)
3489 status = 0;
3490
3491 finish_wait(&state->waitq, &wait);
3492
3493 if (!status)
3494 continue;
3495 if (status == -EINTR)
3496 break;
3497
3498 /* we slept the whole 5 seconds, we must have lost a seqid */
3499 dst->seqid = cpu_to_be32(dst_seqid + 1);
3500 ret = true;
3501 break;
3502 }
3503
3504 return ret;
3505}
3506
3507struct nfs4_closedata {
3508 struct inode *inode;
3509 struct nfs4_state *state;
3510 struct nfs_closeargs arg;
3511 struct nfs_closeres res;
3512 struct {
3513 struct nfs4_layoutreturn_args arg;
3514 struct nfs4_layoutreturn_res res;
3515 struct nfs4_xdr_opaque_data ld_private;
3516 u32 roc_barrier;
3517 bool roc;
3518 } lr;
3519 struct nfs_fattr fattr;
3520 unsigned long timestamp;
3521};
3522
3523static void nfs4_free_closedata(void *data)
3524{
3525 struct nfs4_closedata *calldata = data;
3526 struct nfs4_state_owner *sp = calldata->state->owner;
3527 struct super_block *sb = calldata->state->inode->i_sb;
3528
3529 if (calldata->lr.roc)
3530 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3531 calldata->res.lr_ret);
3532 nfs4_put_open_state(calldata->state);
3533 nfs_free_seqid(calldata->arg.seqid);
3534 nfs4_put_state_owner(sp);
3535 nfs_sb_deactive(sb);
3536 kfree(calldata);
3537}
3538
3539static void nfs4_close_done(struct rpc_task *task, void *data)
3540{
3541 struct nfs4_closedata *calldata = data;
3542 struct nfs4_state *state = calldata->state;
3543 struct nfs_server *server = NFS_SERVER(calldata->inode);
3544 nfs4_stateid *res_stateid = NULL;
3545 struct nfs4_exception exception = {
3546 .state = state,
3547 .inode = calldata->inode,
3548 .stateid = &calldata->arg.stateid,
3549 };
3550
3551 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3552 return;
3553 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3554
3555 /* Handle Layoutreturn errors */
3556 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3557 &calldata->res.lr_ret) == -EAGAIN)
3558 goto out_restart;
3559
3560 /* hmm. we are done with the inode, and in the process of freeing
3561 * the state_owner. we keep this around to process errors
3562 */
3563 switch (task->tk_status) {
3564 case 0:
3565 res_stateid = &calldata->res.stateid;
3566 renew_lease(server, calldata->timestamp);
3567 break;
3568 case -NFS4ERR_ACCESS:
3569 if (calldata->arg.bitmask != NULL) {
3570 calldata->arg.bitmask = NULL;
3571 calldata->res.fattr = NULL;
3572 goto out_restart;
3573
3574 }
3575 break;
3576 case -NFS4ERR_OLD_STATEID:
3577 /* Did we race with OPEN? */
3578 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3579 state))
3580 goto out_restart;
3581 goto out_release;
3582 case -NFS4ERR_ADMIN_REVOKED:
3583 case -NFS4ERR_STALE_STATEID:
3584 case -NFS4ERR_EXPIRED:
3585 nfs4_free_revoked_stateid(server,
3586 &calldata->arg.stateid,
3587 task->tk_msg.rpc_cred);
3588 fallthrough;
3589 case -NFS4ERR_BAD_STATEID:
3590 if (calldata->arg.fmode == 0)
3591 break;
3592 fallthrough;
3593 default:
3594 task->tk_status = nfs4_async_handle_exception(task,
3595 server, task->tk_status, &exception);
3596 if (exception.retry)
3597 goto out_restart;
3598 }
3599 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3600 res_stateid, calldata->arg.fmode);
3601out_release:
3602 task->tk_status = 0;
3603 nfs_release_seqid(calldata->arg.seqid);
3604 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3605 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3606 return;
3607out_restart:
3608 task->tk_status = 0;
3609 rpc_restart_call_prepare(task);
3610 goto out_release;
3611}
3612
3613static void nfs4_close_prepare(struct rpc_task *task, void *data)
3614{
3615 struct nfs4_closedata *calldata = data;
3616 struct nfs4_state *state = calldata->state;
3617 struct inode *inode = calldata->inode;
3618 struct nfs_server *server = NFS_SERVER(inode);
3619 struct pnfs_layout_hdr *lo;
3620 bool is_rdonly, is_wronly, is_rdwr;
3621 int call_close = 0;
3622
3623 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3624 goto out_wait;
3625
3626 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3627 spin_lock(&state->owner->so_lock);
3628 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3629 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3630 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3631 /* Calculate the change in open mode */
3632 calldata->arg.fmode = 0;
3633 if (state->n_rdwr == 0) {
3634 if (state->n_rdonly == 0)
3635 call_close |= is_rdonly;
3636 else if (is_rdonly)
3637 calldata->arg.fmode |= FMODE_READ;
3638 if (state->n_wronly == 0)
3639 call_close |= is_wronly;
3640 else if (is_wronly)
3641 calldata->arg.fmode |= FMODE_WRITE;
3642 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3643 call_close |= is_rdwr;
3644 } else if (is_rdwr)
3645 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3646
3647 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3648 if (!nfs4_valid_open_stateid(state))
3649 call_close = 0;
3650 spin_unlock(&state->owner->so_lock);
3651
3652 if (!call_close) {
3653 /* Note: exit _without_ calling nfs4_close_done */
3654 goto out_no_action;
3655 }
3656
3657 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3658 nfs_release_seqid(calldata->arg.seqid);
3659 goto out_wait;
3660 }
3661
3662 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3663 if (lo && !pnfs_layout_is_valid(lo)) {
3664 calldata->arg.lr_args = NULL;
3665 calldata->res.lr_res = NULL;
3666 }
3667
3668 if (calldata->arg.fmode == 0)
3669 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3670
3671 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3672 /* Close-to-open cache consistency revalidation */
3673 if (!nfs4_have_delegation(inode, FMODE_READ)) {
3674 nfs4_bitmask_set(calldata->arg.bitmask_store,
3675 server->cache_consistency_bitmask,
3676 inode, 0);
3677 calldata->arg.bitmask = calldata->arg.bitmask_store;
3678 } else
3679 calldata->arg.bitmask = NULL;
3680 }
3681
3682 calldata->arg.share_access =
3683 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3684 calldata->arg.fmode, 0);
3685
3686 if (calldata->res.fattr == NULL)
3687 calldata->arg.bitmask = NULL;
3688 else if (calldata->arg.bitmask == NULL)
3689 calldata->res.fattr = NULL;
3690 calldata->timestamp = jiffies;
3691 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3692 &calldata->arg.seq_args,
3693 &calldata->res.seq_res,
3694 task) != 0)
3695 nfs_release_seqid(calldata->arg.seqid);
3696 return;
3697out_no_action:
3698 task->tk_action = NULL;
3699out_wait:
3700 nfs4_sequence_done(task, &calldata->res.seq_res);
3701}
3702
3703static const struct rpc_call_ops nfs4_close_ops = {
3704 .rpc_call_prepare = nfs4_close_prepare,
3705 .rpc_call_done = nfs4_close_done,
3706 .rpc_release = nfs4_free_closedata,
3707};
3708
3709/*
3710 * It is possible for data to be read/written from a mem-mapped file
3711 * after the sys_close call (which hits the vfs layer as a flush).
3712 * This means that we can't safely call nfsv4 close on a file until
3713 * the inode is cleared. This in turn means that we are not good
3714 * NFSv4 citizens - we do not indicate to the server to update the file's
3715 * share state even when we are done with one of the three share
3716 * stateid's in the inode.
3717 *
3718 * NOTE: Caller must be holding the sp->so_owner semaphore!
3719 */
3720int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3721{
3722 struct nfs_server *server = NFS_SERVER(state->inode);
3723 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3724 struct nfs4_closedata *calldata;
3725 struct nfs4_state_owner *sp = state->owner;
3726 struct rpc_task *task;
3727 struct rpc_message msg = {
3728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3729 .rpc_cred = state->owner->so_cred,
3730 };
3731 struct rpc_task_setup task_setup_data = {
3732 .rpc_client = server->client,
3733 .rpc_message = &msg,
3734 .callback_ops = &nfs4_close_ops,
3735 .workqueue = nfsiod_workqueue,
3736 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3737 };
3738 int status = -ENOMEM;
3739
3740 if (server->nfs_client->cl_minorversion)
3741 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3742
3743 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3744 &task_setup_data.rpc_client, &msg);
3745
3746 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3747 if (calldata == NULL)
3748 goto out;
3749 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3750 calldata->inode = state->inode;
3751 calldata->state = state;
3752 calldata->arg.fh = NFS_FH(state->inode);
3753 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3754 goto out_free_calldata;
3755 /* Serialization for the sequence id */
3756 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3757 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3758 if (IS_ERR(calldata->arg.seqid))
3759 goto out_free_calldata;
3760 nfs_fattr_init(&calldata->fattr);
3761 calldata->arg.fmode = 0;
3762 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3763 calldata->res.fattr = &calldata->fattr;
3764 calldata->res.seqid = calldata->arg.seqid;
3765 calldata->res.server = server;
3766 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3767 calldata->lr.roc = pnfs_roc(state->inode,
3768 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3769 if (calldata->lr.roc) {
3770 calldata->arg.lr_args = &calldata->lr.arg;
3771 calldata->res.lr_res = &calldata->lr.res;
3772 }
3773 nfs_sb_active(calldata->inode->i_sb);
3774
3775 msg.rpc_argp = &calldata->arg;
3776 msg.rpc_resp = &calldata->res;
3777 task_setup_data.callback_data = calldata;
3778 task = rpc_run_task(&task_setup_data);
3779 if (IS_ERR(task))
3780 return PTR_ERR(task);
3781 status = 0;
3782 if (wait)
3783 status = rpc_wait_for_completion_task(task);
3784 rpc_put_task(task);
3785 return status;
3786out_free_calldata:
3787 kfree(calldata);
3788out:
3789 nfs4_put_open_state(state);
3790 nfs4_put_state_owner(sp);
3791 return status;
3792}
3793
3794static struct inode *
3795nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3796 int open_flags, struct iattr *attr, int *opened)
3797{
3798 struct nfs4_state *state;
3799 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3800
3801 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3802
3803 /* Protect against concurrent sillydeletes */
3804 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3805
3806 nfs4_label_release_security(label);
3807
3808 if (IS_ERR(state))
3809 return ERR_CAST(state);
3810 return state->inode;
3811}
3812
3813static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3814{
3815 if (ctx->state == NULL)
3816 return;
3817 if (is_sync)
3818 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3819 else
3820 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3821}
3822
3823#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3824#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3825#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
3826
3827static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3828{
3829 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3830 struct nfs4_server_caps_arg args = {
3831 .fhandle = fhandle,
3832 .bitmask = bitmask,
3833 };
3834 struct nfs4_server_caps_res res = {};
3835 struct rpc_message msg = {
3836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3837 .rpc_argp = &args,
3838 .rpc_resp = &res,
3839 };
3840 int status;
3841 int i;
3842
3843 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3844 FATTR4_WORD0_FH_EXPIRE_TYPE |
3845 FATTR4_WORD0_LINK_SUPPORT |
3846 FATTR4_WORD0_SYMLINK_SUPPORT |
3847 FATTR4_WORD0_ACLSUPPORT |
3848 FATTR4_WORD0_CASE_INSENSITIVE |
3849 FATTR4_WORD0_CASE_PRESERVING;
3850 if (minorversion)
3851 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3852
3853 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3854 if (status == 0) {
3855 /* Sanity check the server answers */
3856 switch (minorversion) {
3857 case 0:
3858 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3859 res.attr_bitmask[2] = 0;
3860 break;
3861 case 1:
3862 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3863 break;
3864 case 2:
3865 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3866 }
3867 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3868 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3869 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3870 server->fattr_valid = NFS_ATTR_FATTR_V4;
3871 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3872 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3873 server->caps |= NFS_CAP_ACLS;
3874 if (res.has_links != 0)
3875 server->caps |= NFS_CAP_HARDLINKS;
3876 if (res.has_symlinks != 0)
3877 server->caps |= NFS_CAP_SYMLINKS;
3878 if (res.case_insensitive)
3879 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3880 if (res.case_preserving)
3881 server->caps |= NFS_CAP_CASE_PRESERVING;
3882#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3883 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3884 server->caps |= NFS_CAP_SECURITY_LABEL;
3885#endif
3886 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3887 server->caps |= NFS_CAP_FS_LOCATIONS;
3888 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3889 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3890 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3891 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3892 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3893 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3894 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3895 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3896 NFS_ATTR_FATTR_OWNER_NAME);
3897 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3898 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3899 NFS_ATTR_FATTR_GROUP_NAME);
3900 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3901 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3902 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3903 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3904 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3905 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3906 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3907 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
3908 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3909 sizeof(server->attr_bitmask));
3910 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3911
3912 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3913 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3914 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3915 server->cache_consistency_bitmask[2] = 0;
3916
3917 /* Avoid a regression due to buggy server */
3918 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3919 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3920 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3921 sizeof(server->exclcreat_bitmask));
3922
3923 server->acl_bitmask = res.acl_bitmask;
3924 server->fh_expire_type = res.fh_expire_type;
3925 }
3926
3927 return status;
3928}
3929
3930int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3931{
3932 struct nfs4_exception exception = {
3933 .interruptible = true,
3934 };
3935 int err;
3936
3937 nfs4_server_set_init_caps(server);
3938 do {
3939 err = nfs4_handle_exception(server,
3940 _nfs4_server_capabilities(server, fhandle),
3941 &exception);
3942 } while (exception.retry);
3943 return err;
3944}
3945
3946static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3947 struct nfs_client *clp,
3948 struct nfs_server *server)
3949{
3950 int i;
3951
3952 for (i = 0; i < location->nservers; i++) {
3953 struct nfs4_string *srv_loc = &location->servers[i];
3954 struct sockaddr addr;
3955 size_t addrlen;
3956 struct xprt_create xprt_args = {
3957 .ident = 0,
3958 .net = clp->cl_net,
3959 };
3960 struct nfs4_add_xprt_data xprtdata = {
3961 .clp = clp,
3962 };
3963 struct rpc_add_xprt_test rpcdata = {
3964 .add_xprt_test = clp->cl_mvops->session_trunk,
3965 .data = &xprtdata,
3966 };
3967 char *servername = NULL;
3968
3969 if (!srv_loc->len)
3970 continue;
3971
3972 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3973 &addr, sizeof(addr),
3974 clp->cl_net, server->port);
3975 if (!addrlen)
3976 return;
3977 xprt_args.dstaddr = &addr;
3978 xprt_args.addrlen = addrlen;
3979 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3980 if (!servername)
3981 return;
3982 memcpy(servername, srv_loc->data, srv_loc->len);
3983 servername[srv_loc->len] = '\0';
3984 xprt_args.servername = servername;
3985
3986 xprtdata.cred = nfs4_get_clid_cred(clp);
3987 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3988 rpc_clnt_setup_test_and_add_xprt,
3989 &rpcdata);
3990 if (xprtdata.cred)
3991 put_cred(xprtdata.cred);
3992 kfree(servername);
3993 }
3994}
3995
3996static int _nfs4_discover_trunking(struct nfs_server *server,
3997 struct nfs_fh *fhandle)
3998{
3999 struct nfs4_fs_locations *locations = NULL;
4000 struct page *page;
4001 const struct cred *cred;
4002 struct nfs_client *clp = server->nfs_client;
4003 const struct nfs4_state_maintenance_ops *ops =
4004 clp->cl_mvops->state_renewal_ops;
4005 int status = -ENOMEM, i;
4006
4007 cred = ops->get_state_renewal_cred(clp);
4008 if (cred == NULL) {
4009 cred = nfs4_get_clid_cred(clp);
4010 if (cred == NULL)
4011 return -ENOKEY;
4012 }
4013
4014 page = alloc_page(GFP_KERNEL);
4015 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4016 if (page == NULL || locations == NULL)
4017 goto out;
4018
4019 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4020 cred);
4021 if (status)
4022 goto out;
4023
4024 for (i = 0; i < locations->nlocations; i++)
4025 test_fs_location_for_trunking(&locations->locations[i], clp,
4026 server);
4027out:
4028 if (page)
4029 __free_page(page);
4030 kfree(locations);
4031 return status;
4032}
4033
4034static int nfs4_discover_trunking(struct nfs_server *server,
4035 struct nfs_fh *fhandle)
4036{
4037 struct nfs4_exception exception = {
4038 .interruptible = true,
4039 };
4040 struct nfs_client *clp = server->nfs_client;
4041 int err = 0;
4042
4043 if (!nfs4_has_session(clp))
4044 goto out;
4045 do {
4046 err = nfs4_handle_exception(server,
4047 _nfs4_discover_trunking(server, fhandle),
4048 &exception);
4049 } while (exception.retry);
4050out:
4051 return err;
4052}
4053
4054static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4055 struct nfs_fsinfo *info)
4056{
4057 u32 bitmask[3];
4058 struct nfs4_lookup_root_arg args = {
4059 .bitmask = bitmask,
4060 };
4061 struct nfs4_lookup_res res = {
4062 .server = server,
4063 .fattr = info->fattr,
4064 .fh = fhandle,
4065 };
4066 struct rpc_message msg = {
4067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4068 .rpc_argp = &args,
4069 .rpc_resp = &res,
4070 };
4071
4072 bitmask[0] = nfs4_fattr_bitmap[0];
4073 bitmask[1] = nfs4_fattr_bitmap[1];
4074 /*
4075 * Process the label in the upcoming getfattr
4076 */
4077 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4078
4079 nfs_fattr_init(info->fattr);
4080 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4081}
4082
4083static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4084 struct nfs_fsinfo *info)
4085{
4086 struct nfs4_exception exception = {
4087 .interruptible = true,
4088 };
4089 int err;
4090 do {
4091 err = _nfs4_lookup_root(server, fhandle, info);
4092 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4093 switch (err) {
4094 case 0:
4095 case -NFS4ERR_WRONGSEC:
4096 goto out;
4097 default:
4098 err = nfs4_handle_exception(server, err, &exception);
4099 }
4100 } while (exception.retry);
4101out:
4102 return err;
4103}
4104
4105static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4106 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4107{
4108 struct rpc_auth_create_args auth_args = {
4109 .pseudoflavor = flavor,
4110 };
4111 struct rpc_auth *auth;
4112
4113 auth = rpcauth_create(&auth_args, server->client);
4114 if (IS_ERR(auth))
4115 return -EACCES;
4116 return nfs4_lookup_root(server, fhandle, info);
4117}
4118
4119/*
4120 * Retry pseudoroot lookup with various security flavors. We do this when:
4121 *
4122 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4123 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4124 *
4125 * Returns zero on success, or a negative NFS4ERR value, or a
4126 * negative errno value.
4127 */
4128static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4129 struct nfs_fsinfo *info)
4130{
4131 /* Per 3530bis 15.33.5 */
4132 static const rpc_authflavor_t flav_array[] = {
4133 RPC_AUTH_GSS_KRB5P,
4134 RPC_AUTH_GSS_KRB5I,
4135 RPC_AUTH_GSS_KRB5,
4136 RPC_AUTH_UNIX, /* courtesy */
4137 RPC_AUTH_NULL,
4138 };
4139 int status = -EPERM;
4140 size_t i;
4141
4142 if (server->auth_info.flavor_len > 0) {
4143 /* try each flavor specified by user */
4144 for (i = 0; i < server->auth_info.flavor_len; i++) {
4145 status = nfs4_lookup_root_sec(server, fhandle, info,
4146 server->auth_info.flavors[i]);
4147 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4148 continue;
4149 break;
4150 }
4151 } else {
4152 /* no flavors specified by user, try default list */
4153 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4154 status = nfs4_lookup_root_sec(server, fhandle, info,
4155 flav_array[i]);
4156 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4157 continue;
4158 break;
4159 }
4160 }
4161
4162 /*
4163 * -EACCES could mean that the user doesn't have correct permissions
4164 * to access the mount. It could also mean that we tried to mount
4165 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4166 * existing mount programs don't handle -EACCES very well so it should
4167 * be mapped to -EPERM instead.
4168 */
4169 if (status == -EACCES)
4170 status = -EPERM;
4171 return status;
4172}
4173
4174/**
4175 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4176 * @server: initialized nfs_server handle
4177 * @fhandle: we fill in the pseudo-fs root file handle
4178 * @info: we fill in an FSINFO struct
4179 * @auth_probe: probe the auth flavours
4180 *
4181 * Returns zero on success, or a negative errno.
4182 */
4183int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4184 struct nfs_fsinfo *info,
4185 bool auth_probe)
4186{
4187 int status = 0;
4188
4189 if (!auth_probe)
4190 status = nfs4_lookup_root(server, fhandle, info);
4191
4192 if (auth_probe || status == NFS4ERR_WRONGSEC)
4193 status = server->nfs_client->cl_mvops->find_root_sec(server,
4194 fhandle, info);
4195
4196 if (status == 0)
4197 status = nfs4_server_capabilities(server, fhandle);
4198 if (status == 0)
4199 status = nfs4_do_fsinfo(server, fhandle, info);
4200
4201 return nfs4_map_errors(status);
4202}
4203
4204static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4205 struct nfs_fsinfo *info)
4206{
4207 int error;
4208 struct nfs_fattr *fattr = info->fattr;
4209
4210 error = nfs4_server_capabilities(server, mntfh);
4211 if (error < 0) {
4212 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4213 return error;
4214 }
4215
4216 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4217 if (error < 0) {
4218 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4219 goto out;
4220 }
4221
4222 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4223 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4224 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4225
4226out:
4227 return error;
4228}
4229
4230/*
4231 * Get locations and (maybe) other attributes of a referral.
4232 * Note that we'll actually follow the referral later when
4233 * we detect fsid mismatch in inode revalidation
4234 */
4235static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4236 const struct qstr *name, struct nfs_fattr *fattr,
4237 struct nfs_fh *fhandle)
4238{
4239 int status = -ENOMEM;
4240 struct page *page = NULL;
4241 struct nfs4_fs_locations *locations = NULL;
4242
4243 page = alloc_page(GFP_KERNEL);
4244 if (page == NULL)
4245 goto out;
4246 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4247 if (locations == NULL)
4248 goto out;
4249
4250 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4251 if (status != 0)
4252 goto out;
4253
4254 /*
4255 * If the fsid didn't change, this is a migration event, not a
4256 * referral. Cause us to drop into the exception handler, which
4257 * will kick off migration recovery.
4258 */
4259 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
4260 dprintk("%s: server did not return a different fsid for"
4261 " a referral at %s\n", __func__, name->name);
4262 status = -NFS4ERR_MOVED;
4263 goto out;
4264 }
4265 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4266 nfs_fixup_referral_attributes(&locations->fattr);
4267
4268 /* replace the lookup nfs_fattr with the locations nfs_fattr */
4269 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
4270 memset(fhandle, 0, sizeof(struct nfs_fh));
4271out:
4272 if (page)
4273 __free_page(page);
4274 kfree(locations);
4275 return status;
4276}
4277
4278static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4279 struct nfs_fattr *fattr, struct inode *inode)
4280{
4281 __u32 bitmask[NFS4_BITMASK_SZ];
4282 struct nfs4_getattr_arg args = {
4283 .fh = fhandle,
4284 .bitmask = bitmask,
4285 };
4286 struct nfs4_getattr_res res = {
4287 .fattr = fattr,
4288 .server = server,
4289 };
4290 struct rpc_message msg = {
4291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4292 .rpc_argp = &args,
4293 .rpc_resp = &res,
4294 };
4295 unsigned short task_flags = 0;
4296
4297 if (nfs4_has_session(server->nfs_client))
4298 task_flags = RPC_TASK_MOVEABLE;
4299
4300 /* Is this is an attribute revalidation, subject to softreval? */
4301 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4302 task_flags |= RPC_TASK_TIMEOUT;
4303
4304 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4305 nfs_fattr_init(fattr);
4306 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4307 return nfs4_do_call_sync(server->client, server, &msg,
4308 &args.seq_args, &res.seq_res, task_flags);
4309}
4310
4311int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4312 struct nfs_fattr *fattr, struct inode *inode)
4313{
4314 struct nfs4_exception exception = {
4315 .interruptible = true,
4316 };
4317 int err;
4318 do {
4319 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4320 trace_nfs4_getattr(server, fhandle, fattr, err);
4321 err = nfs4_handle_exception(server, err,
4322 &exception);
4323 } while (exception.retry);
4324 return err;
4325}
4326
4327/*
4328 * The file is not closed if it is opened due to the a request to change
4329 * the size of the file. The open call will not be needed once the
4330 * VFS layer lookup-intents are implemented.
4331 *
4332 * Close is called when the inode is destroyed.
4333 * If we haven't opened the file for O_WRONLY, we
4334 * need to in the size_change case to obtain a stateid.
4335 *
4336 * Got race?
4337 * Because OPEN is always done by name in nfsv4, it is
4338 * possible that we opened a different file by the same
4339 * name. We can recognize this race condition, but we
4340 * can't do anything about it besides returning an error.
4341 *
4342 * This will be fixed with VFS changes (lookup-intent).
4343 */
4344static int
4345nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4346 struct iattr *sattr)
4347{
4348 struct inode *inode = d_inode(dentry);
4349 const struct cred *cred = NULL;
4350 struct nfs_open_context *ctx = NULL;
4351 int status;
4352
4353 if (pnfs_ld_layoutret_on_setattr(inode) &&
4354 sattr->ia_valid & ATTR_SIZE &&
4355 sattr->ia_size < i_size_read(inode))
4356 pnfs_commit_and_return_layout(inode);
4357
4358 nfs_fattr_init(fattr);
4359
4360 /* Deal with open(O_TRUNC) */
4361 if (sattr->ia_valid & ATTR_OPEN)
4362 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4363
4364 /* Optimization: if the end result is no change, don't RPC */
4365 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4366 return 0;
4367
4368 /* Search for an existing open(O_WRITE) file */
4369 if (sattr->ia_valid & ATTR_FILE) {
4370
4371 ctx = nfs_file_open_context(sattr->ia_file);
4372 if (ctx)
4373 cred = ctx->cred;
4374 }
4375
4376 /* Return any delegations if we're going to change ACLs */
4377 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4378 nfs4_inode_make_writeable(inode);
4379
4380 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4381 if (status == 0) {
4382 nfs_setattr_update_inode(inode, sattr, fattr);
4383 nfs_setsecurity(inode, fattr);
4384 }
4385 return status;
4386}
4387
4388static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4389 struct dentry *dentry, struct nfs_fh *fhandle,
4390 struct nfs_fattr *fattr)
4391{
4392 struct nfs_server *server = NFS_SERVER(dir);
4393 int status;
4394 struct nfs4_lookup_arg args = {
4395 .bitmask = server->attr_bitmask,
4396 .dir_fh = NFS_FH(dir),
4397 .name = &dentry->d_name,
4398 };
4399 struct nfs4_lookup_res res = {
4400 .server = server,
4401 .fattr = fattr,
4402 .fh = fhandle,
4403 };
4404 struct rpc_message msg = {
4405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4406 .rpc_argp = &args,
4407 .rpc_resp = &res,
4408 };
4409 unsigned short task_flags = 0;
4410
4411 if (server->nfs_client->cl_minorversion)
4412 task_flags = RPC_TASK_MOVEABLE;
4413
4414 /* Is this is an attribute revalidation, subject to softreval? */
4415 if (nfs_lookup_is_soft_revalidate(dentry))
4416 task_flags |= RPC_TASK_TIMEOUT;
4417
4418 args.bitmask = nfs4_bitmask(server, fattr->label);
4419
4420 nfs_fattr_init(fattr);
4421
4422 dprintk("NFS call lookup %pd2\n", dentry);
4423 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4424 status = nfs4_do_call_sync(clnt, server, &msg,
4425 &args.seq_args, &res.seq_res, task_flags);
4426 dprintk("NFS reply lookup: %d\n", status);
4427 return status;
4428}
4429
4430static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4431{
4432 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4433 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4434 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4435 fattr->nlink = 2;
4436}
4437
4438static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4439 struct dentry *dentry, struct nfs_fh *fhandle,
4440 struct nfs_fattr *fattr)
4441{
4442 struct nfs4_exception exception = {
4443 .interruptible = true,
4444 };
4445 struct rpc_clnt *client = *clnt;
4446 const struct qstr *name = &dentry->d_name;
4447 int err;
4448 do {
4449 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4450 trace_nfs4_lookup(dir, name, err);
4451 switch (err) {
4452 case -NFS4ERR_BADNAME:
4453 err = -ENOENT;
4454 goto out;
4455 case -NFS4ERR_MOVED:
4456 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4457 if (err == -NFS4ERR_MOVED)
4458 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4459 goto out;
4460 case -NFS4ERR_WRONGSEC:
4461 err = -EPERM;
4462 if (client != *clnt)
4463 goto out;
4464 client = nfs4_negotiate_security(client, dir, name);
4465 if (IS_ERR(client))
4466 return PTR_ERR(client);
4467
4468 exception.retry = 1;
4469 break;
4470 default:
4471 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4472 }
4473 } while (exception.retry);
4474
4475out:
4476 if (err == 0)
4477 *clnt = client;
4478 else if (client != *clnt)
4479 rpc_shutdown_client(client);
4480
4481 return err;
4482}
4483
4484static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4485 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4486{
4487 int status;
4488 struct rpc_clnt *client = NFS_CLIENT(dir);
4489
4490 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4491 if (client != NFS_CLIENT(dir)) {
4492 rpc_shutdown_client(client);
4493 nfs_fixup_secinfo_attributes(fattr);
4494 }
4495 return status;
4496}
4497
4498struct rpc_clnt *
4499nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4500 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4501{
4502 struct rpc_clnt *client = NFS_CLIENT(dir);
4503 int status;
4504
4505 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4506 if (status < 0)
4507 return ERR_PTR(status);
4508 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4509}
4510
4511static int _nfs4_proc_lookupp(struct inode *inode,
4512 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4513{
4514 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4515 struct nfs_server *server = NFS_SERVER(inode);
4516 int status;
4517 struct nfs4_lookupp_arg args = {
4518 .bitmask = server->attr_bitmask,
4519 .fh = NFS_FH(inode),
4520 };
4521 struct nfs4_lookupp_res res = {
4522 .server = server,
4523 .fattr = fattr,
4524 .fh = fhandle,
4525 };
4526 struct rpc_message msg = {
4527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4528 .rpc_argp = &args,
4529 .rpc_resp = &res,
4530 };
4531 unsigned short task_flags = 0;
4532
4533 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4534 task_flags |= RPC_TASK_TIMEOUT;
4535
4536 args.bitmask = nfs4_bitmask(server, fattr->label);
4537
4538 nfs_fattr_init(fattr);
4539
4540 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4541 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4542 &res.seq_res, task_flags);
4543 dprintk("NFS reply lookupp: %d\n", status);
4544 return status;
4545}
4546
4547static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4548 struct nfs_fattr *fattr)
4549{
4550 struct nfs4_exception exception = {
4551 .interruptible = true,
4552 };
4553 int err;
4554 do {
4555 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4556 trace_nfs4_lookupp(inode, err);
4557 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4558 &exception);
4559 } while (exception.retry);
4560 return err;
4561}
4562
4563static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4564 const struct cred *cred)
4565{
4566 struct nfs_server *server = NFS_SERVER(inode);
4567 struct nfs4_accessargs args = {
4568 .fh = NFS_FH(inode),
4569 .access = entry->mask,
4570 };
4571 struct nfs4_accessres res = {
4572 .server = server,
4573 };
4574 struct rpc_message msg = {
4575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4576 .rpc_argp = &args,
4577 .rpc_resp = &res,
4578 .rpc_cred = cred,
4579 };
4580 int status = 0;
4581
4582 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4583 res.fattr = nfs_alloc_fattr();
4584 if (res.fattr == NULL)
4585 return -ENOMEM;
4586 args.bitmask = server->cache_consistency_bitmask;
4587 }
4588 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4589 if (!status) {
4590 nfs_access_set_mask(entry, res.access);
4591 if (res.fattr)
4592 nfs_refresh_inode(inode, res.fattr);
4593 }
4594 nfs_free_fattr(res.fattr);
4595 return status;
4596}
4597
4598static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4599 const struct cred *cred)
4600{
4601 struct nfs4_exception exception = {
4602 .interruptible = true,
4603 };
4604 int err;
4605 do {
4606 err = _nfs4_proc_access(inode, entry, cred);
4607 trace_nfs4_access(inode, err);
4608 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4609 &exception);
4610 } while (exception.retry);
4611 return err;
4612}
4613
4614/*
4615 * TODO: For the time being, we don't try to get any attributes
4616 * along with any of the zero-copy operations READ, READDIR,
4617 * READLINK, WRITE.
4618 *
4619 * In the case of the first three, we want to put the GETATTR
4620 * after the read-type operation -- this is because it is hard
4621 * to predict the length of a GETATTR response in v4, and thus
4622 * align the READ data correctly. This means that the GETATTR
4623 * may end up partially falling into the page cache, and we should
4624 * shift it into the 'tail' of the xdr_buf before processing.
4625 * To do this efficiently, we need to know the total length
4626 * of data received, which doesn't seem to be available outside
4627 * of the RPC layer.
4628 *
4629 * In the case of WRITE, we also want to put the GETATTR after
4630 * the operation -- in this case because we want to make sure
4631 * we get the post-operation mtime and size.
4632 *
4633 * Both of these changes to the XDR layer would in fact be quite
4634 * minor, but I decided to leave them for a subsequent patch.
4635 */
4636static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4637 unsigned int pgbase, unsigned int pglen)
4638{
4639 struct nfs4_readlink args = {
4640 .fh = NFS_FH(inode),
4641 .pgbase = pgbase,
4642 .pglen = pglen,
4643 .pages = &page,
4644 };
4645 struct nfs4_readlink_res res;
4646 struct rpc_message msg = {
4647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4648 .rpc_argp = &args,
4649 .rpc_resp = &res,
4650 };
4651
4652 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4653}
4654
4655static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4656 unsigned int pgbase, unsigned int pglen)
4657{
4658 struct nfs4_exception exception = {
4659 .interruptible = true,
4660 };
4661 int err;
4662 do {
4663 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4664 trace_nfs4_readlink(inode, err);
4665 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4666 &exception);
4667 } while (exception.retry);
4668 return err;
4669}
4670
4671/*
4672 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4673 */
4674static int
4675nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4676 int flags)
4677{
4678 struct nfs_server *server = NFS_SERVER(dir);
4679 struct nfs4_label l, *ilabel = NULL;
4680 struct nfs_open_context *ctx;
4681 struct nfs4_state *state;
4682 int status = 0;
4683
4684 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4685 if (IS_ERR(ctx))
4686 return PTR_ERR(ctx);
4687
4688 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4689
4690 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4691 sattr->ia_mode &= ~current_umask();
4692 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4693 if (IS_ERR(state)) {
4694 status = PTR_ERR(state);
4695 goto out;
4696 }
4697out:
4698 nfs4_label_release_security(ilabel);
4699 put_nfs_open_context(ctx);
4700 return status;
4701}
4702
4703static int
4704_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4705{
4706 struct nfs_server *server = NFS_SERVER(dir);
4707 struct nfs_removeargs args = {
4708 .fh = NFS_FH(dir),
4709 .name = *name,
4710 };
4711 struct nfs_removeres res = {
4712 .server = server,
4713 };
4714 struct rpc_message msg = {
4715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4716 .rpc_argp = &args,
4717 .rpc_resp = &res,
4718 };
4719 unsigned long timestamp = jiffies;
4720 int status;
4721
4722 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4723 if (status == 0) {
4724 spin_lock(&dir->i_lock);
4725 /* Removing a directory decrements nlink in the parent */
4726 if (ftype == NF4DIR && dir->i_nlink > 2)
4727 nfs4_dec_nlink_locked(dir);
4728 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4729 NFS_INO_INVALID_DATA);
4730 spin_unlock(&dir->i_lock);
4731 }
4732 return status;
4733}
4734
4735static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4736{
4737 struct nfs4_exception exception = {
4738 .interruptible = true,
4739 };
4740 struct inode *inode = d_inode(dentry);
4741 int err;
4742
4743 if (inode) {
4744 if (inode->i_nlink == 1)
4745 nfs4_inode_return_delegation(inode);
4746 else
4747 nfs4_inode_make_writeable(inode);
4748 }
4749 do {
4750 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4751 trace_nfs4_remove(dir, &dentry->d_name, err);
4752 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4753 &exception);
4754 } while (exception.retry);
4755 return err;
4756}
4757
4758static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4759{
4760 struct nfs4_exception exception = {
4761 .interruptible = true,
4762 };
4763 int err;
4764
4765 do {
4766 err = _nfs4_proc_remove(dir, name, NF4DIR);
4767 trace_nfs4_remove(dir, name, err);
4768 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4769 &exception);
4770 } while (exception.retry);
4771 return err;
4772}
4773
4774static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4775 struct dentry *dentry,
4776 struct inode *inode)
4777{
4778 struct nfs_removeargs *args = msg->rpc_argp;
4779 struct nfs_removeres *res = msg->rpc_resp;
4780
4781 res->server = NFS_SB(dentry->d_sb);
4782 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4783 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4784
4785 nfs_fattr_init(res->dir_attr);
4786
4787 if (inode) {
4788 nfs4_inode_return_delegation(inode);
4789 nfs_d_prune_case_insensitive_aliases(inode);
4790 }
4791}
4792
4793static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4794{
4795 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4796 &data->args.seq_args,
4797 &data->res.seq_res,
4798 task);
4799}
4800
4801static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4802{
4803 struct nfs_unlinkdata *data = task->tk_calldata;
4804 struct nfs_removeres *res = &data->res;
4805
4806 if (!nfs4_sequence_done(task, &res->seq_res))
4807 return 0;
4808 if (nfs4_async_handle_error(task, res->server, NULL,
4809 &data->timeout) == -EAGAIN)
4810 return 0;
4811 if (task->tk_status == 0)
4812 nfs4_update_changeattr(dir, &res->cinfo,
4813 res->dir_attr->time_start,
4814 NFS_INO_INVALID_DATA);
4815 return 1;
4816}
4817
4818static void nfs4_proc_rename_setup(struct rpc_message *msg,
4819 struct dentry *old_dentry,
4820 struct dentry *new_dentry)
4821{
4822 struct nfs_renameargs *arg = msg->rpc_argp;
4823 struct nfs_renameres *res = msg->rpc_resp;
4824 struct inode *old_inode = d_inode(old_dentry);
4825 struct inode *new_inode = d_inode(new_dentry);
4826
4827 if (old_inode)
4828 nfs4_inode_make_writeable(old_inode);
4829 if (new_inode)
4830 nfs4_inode_return_delegation(new_inode);
4831 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4832 res->server = NFS_SB(old_dentry->d_sb);
4833 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4834}
4835
4836static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4837{
4838 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4839 &data->args.seq_args,
4840 &data->res.seq_res,
4841 task);
4842}
4843
4844static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4845 struct inode *new_dir)
4846{
4847 struct nfs_renamedata *data = task->tk_calldata;
4848 struct nfs_renameres *res = &data->res;
4849
4850 if (!nfs4_sequence_done(task, &res->seq_res))
4851 return 0;
4852 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4853 return 0;
4854
4855 if (task->tk_status == 0) {
4856 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
4857 if (new_dir != old_dir) {
4858 /* Note: If we moved a directory, nlink will change */
4859 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4860 res->old_fattr->time_start,
4861 NFS_INO_INVALID_NLINK |
4862 NFS_INO_INVALID_DATA);
4863 nfs4_update_changeattr(new_dir, &res->new_cinfo,
4864 res->new_fattr->time_start,
4865 NFS_INO_INVALID_NLINK |
4866 NFS_INO_INVALID_DATA);
4867 } else
4868 nfs4_update_changeattr(old_dir, &res->old_cinfo,
4869 res->old_fattr->time_start,
4870 NFS_INO_INVALID_DATA);
4871 }
4872 return 1;
4873}
4874
4875static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4876{
4877 struct nfs_server *server = NFS_SERVER(inode);
4878 __u32 bitmask[NFS4_BITMASK_SZ];
4879 struct nfs4_link_arg arg = {
4880 .fh = NFS_FH(inode),
4881 .dir_fh = NFS_FH(dir),
4882 .name = name,
4883 .bitmask = bitmask,
4884 };
4885 struct nfs4_link_res res = {
4886 .server = server,
4887 };
4888 struct rpc_message msg = {
4889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4890 .rpc_argp = &arg,
4891 .rpc_resp = &res,
4892 };
4893 int status = -ENOMEM;
4894
4895 res.fattr = nfs_alloc_fattr_with_label(server);
4896 if (res.fattr == NULL)
4897 goto out;
4898
4899 nfs4_inode_make_writeable(inode);
4900 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
4901 NFS_INO_INVALID_CHANGE);
4902 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4903 if (!status) {
4904 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4905 NFS_INO_INVALID_DATA);
4906 nfs4_inc_nlink(inode);
4907 status = nfs_post_op_update_inode(inode, res.fattr);
4908 if (!status)
4909 nfs_setsecurity(inode, res.fattr);
4910 }
4911
4912out:
4913 nfs_free_fattr(res.fattr);
4914 return status;
4915}
4916
4917static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4918{
4919 struct nfs4_exception exception = {
4920 .interruptible = true,
4921 };
4922 int err;
4923 do {
4924 err = nfs4_handle_exception(NFS_SERVER(inode),
4925 _nfs4_proc_link(inode, dir, name),
4926 &exception);
4927 } while (exception.retry);
4928 return err;
4929}
4930
4931struct nfs4_createdata {
4932 struct rpc_message msg;
4933 struct nfs4_create_arg arg;
4934 struct nfs4_create_res res;
4935 struct nfs_fh fh;
4936 struct nfs_fattr fattr;
4937};
4938
4939static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4940 const struct qstr *name, struct iattr *sattr, u32 ftype)
4941{
4942 struct nfs4_createdata *data;
4943
4944 data = kzalloc(sizeof(*data), GFP_KERNEL);
4945 if (data != NULL) {
4946 struct nfs_server *server = NFS_SERVER(dir);
4947
4948 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4949 if (IS_ERR(data->fattr.label))
4950 goto out_free;
4951
4952 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4953 data->msg.rpc_argp = &data->arg;
4954 data->msg.rpc_resp = &data->res;
4955 data->arg.dir_fh = NFS_FH(dir);
4956 data->arg.server = server;
4957 data->arg.name = name;
4958 data->arg.attrs = sattr;
4959 data->arg.ftype = ftype;
4960 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
4961 data->arg.umask = current_umask();
4962 data->res.server = server;
4963 data->res.fh = &data->fh;
4964 data->res.fattr = &data->fattr;
4965 nfs_fattr_init(data->res.fattr);
4966 }
4967 return data;
4968out_free:
4969 kfree(data);
4970 return NULL;
4971}
4972
4973static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4974{
4975 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4976 &data->arg.seq_args, &data->res.seq_res, 1);
4977 if (status == 0) {
4978 spin_lock(&dir->i_lock);
4979 /* Creating a directory bumps nlink in the parent */
4980 if (data->arg.ftype == NF4DIR)
4981 nfs4_inc_nlink_locked(dir);
4982 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4983 data->res.fattr->time_start,
4984 NFS_INO_INVALID_DATA);
4985 spin_unlock(&dir->i_lock);
4986 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
4987 }
4988 return status;
4989}
4990
4991static void nfs4_free_createdata(struct nfs4_createdata *data)
4992{
4993 nfs4_label_free(data->fattr.label);
4994 kfree(data);
4995}
4996
4997static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4998 struct page *page, unsigned int len, struct iattr *sattr,
4999 struct nfs4_label *label)
5000{
5001 struct nfs4_createdata *data;
5002 int status = -ENAMETOOLONG;
5003
5004 if (len > NFS4_MAXPATHLEN)
5005 goto out;
5006
5007 status = -ENOMEM;
5008 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5009 if (data == NULL)
5010 goto out;
5011
5012 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5013 data->arg.u.symlink.pages = &page;
5014 data->arg.u.symlink.len = len;
5015 data->arg.label = label;
5016
5017 status = nfs4_do_create(dir, dentry, data);
5018
5019 nfs4_free_createdata(data);
5020out:
5021 return status;
5022}
5023
5024static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5025 struct page *page, unsigned int len, struct iattr *sattr)
5026{
5027 struct nfs4_exception exception = {
5028 .interruptible = true,
5029 };
5030 struct nfs4_label l, *label = NULL;
5031 int err;
5032
5033 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5034
5035 do {
5036 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5037 trace_nfs4_symlink(dir, &dentry->d_name, err);
5038 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5039 &exception);
5040 } while (exception.retry);
5041
5042 nfs4_label_release_security(label);
5043 return err;
5044}
5045
5046static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5047 struct iattr *sattr, struct nfs4_label *label)
5048{
5049 struct nfs4_createdata *data;
5050 int status = -ENOMEM;
5051
5052 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5053 if (data == NULL)
5054 goto out;
5055
5056 data->arg.label = label;
5057 status = nfs4_do_create(dir, dentry, data);
5058
5059 nfs4_free_createdata(data);
5060out:
5061 return status;
5062}
5063
5064static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5065 struct iattr *sattr)
5066{
5067 struct nfs_server *server = NFS_SERVER(dir);
5068 struct nfs4_exception exception = {
5069 .interruptible = true,
5070 };
5071 struct nfs4_label l, *label = NULL;
5072 int err;
5073
5074 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5075
5076 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5077 sattr->ia_mode &= ~current_umask();
5078 do {
5079 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5080 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5081 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5082 &exception);
5083 } while (exception.retry);
5084 nfs4_label_release_security(label);
5085
5086 return err;
5087}
5088
5089static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5090 struct nfs_readdir_res *nr_res)
5091{
5092 struct inode *dir = d_inode(nr_arg->dentry);
5093 struct nfs_server *server = NFS_SERVER(dir);
5094 struct nfs4_readdir_arg args = {
5095 .fh = NFS_FH(dir),
5096 .pages = nr_arg->pages,
5097 .pgbase = 0,
5098 .count = nr_arg->page_len,
5099 .plus = nr_arg->plus,
5100 };
5101 struct nfs4_readdir_res res;
5102 struct rpc_message msg = {
5103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5104 .rpc_argp = &args,
5105 .rpc_resp = &res,
5106 .rpc_cred = nr_arg->cred,
5107 };
5108 int status;
5109
5110 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5111 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5112 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5113 args.bitmask = server->attr_bitmask_nl;
5114 else
5115 args.bitmask = server->attr_bitmask;
5116
5117 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5118 res.pgbase = args.pgbase;
5119 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5120 &res.seq_res, 0);
5121 if (status >= 0) {
5122 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5123 status += args.pgbase;
5124 }
5125
5126 nfs_invalidate_atime(dir);
5127
5128 dprintk("%s: returns %d\n", __func__, status);
5129 return status;
5130}
5131
5132static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5133 struct nfs_readdir_res *res)
5134{
5135 struct nfs4_exception exception = {
5136 .interruptible = true,
5137 };
5138 int err;
5139 do {
5140 err = _nfs4_proc_readdir(arg, res);
5141 trace_nfs4_readdir(d_inode(arg->dentry), err);
5142 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5143 err, &exception);
5144 } while (exception.retry);
5145 return err;
5146}
5147
5148static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5149 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5150{
5151 struct nfs4_createdata *data;
5152 int mode = sattr->ia_mode;
5153 int status = -ENOMEM;
5154
5155 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5156 if (data == NULL)
5157 goto out;
5158
5159 if (S_ISFIFO(mode))
5160 data->arg.ftype = NF4FIFO;
5161 else if (S_ISBLK(mode)) {
5162 data->arg.ftype = NF4BLK;
5163 data->arg.u.device.specdata1 = MAJOR(rdev);
5164 data->arg.u.device.specdata2 = MINOR(rdev);
5165 }
5166 else if (S_ISCHR(mode)) {
5167 data->arg.ftype = NF4CHR;
5168 data->arg.u.device.specdata1 = MAJOR(rdev);
5169 data->arg.u.device.specdata2 = MINOR(rdev);
5170 } else if (!S_ISSOCK(mode)) {
5171 status = -EINVAL;
5172 goto out_free;
5173 }
5174
5175 data->arg.label = label;
5176 status = nfs4_do_create(dir, dentry, data);
5177out_free:
5178 nfs4_free_createdata(data);
5179out:
5180 return status;
5181}
5182
5183static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5184 struct iattr *sattr, dev_t rdev)
5185{
5186 struct nfs_server *server = NFS_SERVER(dir);
5187 struct nfs4_exception exception = {
5188 .interruptible = true,
5189 };
5190 struct nfs4_label l, *label = NULL;
5191 int err;
5192
5193 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5194
5195 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5196 sattr->ia_mode &= ~current_umask();
5197 do {
5198 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5199 trace_nfs4_mknod(dir, &dentry->d_name, err);
5200 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5201 &exception);
5202 } while (exception.retry);
5203
5204 nfs4_label_release_security(label);
5205
5206 return err;
5207}
5208
5209static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5210 struct nfs_fsstat *fsstat)
5211{
5212 struct nfs4_statfs_arg args = {
5213 .fh = fhandle,
5214 .bitmask = server->attr_bitmask,
5215 };
5216 struct nfs4_statfs_res res = {
5217 .fsstat = fsstat,
5218 };
5219 struct rpc_message msg = {
5220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5221 .rpc_argp = &args,
5222 .rpc_resp = &res,
5223 };
5224
5225 nfs_fattr_init(fsstat->fattr);
5226 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5227}
5228
5229static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5230{
5231 struct nfs4_exception exception = {
5232 .interruptible = true,
5233 };
5234 int err;
5235 do {
5236 err = nfs4_handle_exception(server,
5237 _nfs4_proc_statfs(server, fhandle, fsstat),
5238 &exception);
5239 } while (exception.retry);
5240 return err;
5241}
5242
5243static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5244 struct nfs_fsinfo *fsinfo)
5245{
5246 struct nfs4_fsinfo_arg args = {
5247 .fh = fhandle,
5248 .bitmask = server->attr_bitmask,
5249 };
5250 struct nfs4_fsinfo_res res = {
5251 .fsinfo = fsinfo,
5252 };
5253 struct rpc_message msg = {
5254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5255 .rpc_argp = &args,
5256 .rpc_resp = &res,
5257 };
5258
5259 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5260}
5261
5262static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5263{
5264 struct nfs4_exception exception = {
5265 .interruptible = true,
5266 };
5267 int err;
5268
5269 do {
5270 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5271 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5272 if (err == 0) {
5273 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5274 break;
5275 }
5276 err = nfs4_handle_exception(server, err, &exception);
5277 } while (exception.retry);
5278 return err;
5279}
5280
5281static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5282{
5283 int error;
5284
5285 nfs_fattr_init(fsinfo->fattr);
5286 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5287 if (error == 0) {
5288 /* block layout checks this! */
5289 server->pnfs_blksize = fsinfo->blksize;
5290 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5291 }
5292
5293 return error;
5294}
5295
5296static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5297 struct nfs_pathconf *pathconf)
5298{
5299 struct nfs4_pathconf_arg args = {
5300 .fh = fhandle,
5301 .bitmask = server->attr_bitmask,
5302 };
5303 struct nfs4_pathconf_res res = {
5304 .pathconf = pathconf,
5305 };
5306 struct rpc_message msg = {
5307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5308 .rpc_argp = &args,
5309 .rpc_resp = &res,
5310 };
5311
5312 /* None of the pathconf attributes are mandatory to implement */
5313 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5314 memset(pathconf, 0, sizeof(*pathconf));
5315 return 0;
5316 }
5317
5318 nfs_fattr_init(pathconf->fattr);
5319 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5320}
5321
5322static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5323 struct nfs_pathconf *pathconf)
5324{
5325 struct nfs4_exception exception = {
5326 .interruptible = true,
5327 };
5328 int err;
5329
5330 do {
5331 err = nfs4_handle_exception(server,
5332 _nfs4_proc_pathconf(server, fhandle, pathconf),
5333 &exception);
5334 } while (exception.retry);
5335 return err;
5336}
5337
5338int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5339 const struct nfs_open_context *ctx,
5340 const struct nfs_lock_context *l_ctx,
5341 fmode_t fmode)
5342{
5343 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5344}
5345EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5346
5347static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5348 const struct nfs_open_context *ctx,
5349 const struct nfs_lock_context *l_ctx,
5350 fmode_t fmode)
5351{
5352 nfs4_stateid _current_stateid;
5353
5354 /* If the current stateid represents a lost lock, then exit */
5355 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5356 return true;
5357 return nfs4_stateid_match(stateid, &_current_stateid);
5358}
5359
5360static bool nfs4_error_stateid_expired(int err)
5361{
5362 switch (err) {
5363 case -NFS4ERR_DELEG_REVOKED:
5364 case -NFS4ERR_ADMIN_REVOKED:
5365 case -NFS4ERR_BAD_STATEID:
5366 case -NFS4ERR_STALE_STATEID:
5367 case -NFS4ERR_OLD_STATEID:
5368 case -NFS4ERR_OPENMODE:
5369 case -NFS4ERR_EXPIRED:
5370 return true;
5371 }
5372 return false;
5373}
5374
5375static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5376{
5377 struct nfs_server *server = NFS_SERVER(hdr->inode);
5378
5379 trace_nfs4_read(hdr, task->tk_status);
5380 if (task->tk_status < 0) {
5381 struct nfs4_exception exception = {
5382 .inode = hdr->inode,
5383 .state = hdr->args.context->state,
5384 .stateid = &hdr->args.stateid,
5385 };
5386 task->tk_status = nfs4_async_handle_exception(task,
5387 server, task->tk_status, &exception);
5388 if (exception.retry) {
5389 rpc_restart_call_prepare(task);
5390 return -EAGAIN;
5391 }
5392 }
5393
5394 if (task->tk_status > 0)
5395 renew_lease(server, hdr->timestamp);
5396 return 0;
5397}
5398
5399static bool nfs4_read_stateid_changed(struct rpc_task *task,
5400 struct nfs_pgio_args *args)
5401{
5402
5403 if (!nfs4_error_stateid_expired(task->tk_status) ||
5404 nfs4_stateid_is_current(&args->stateid,
5405 args->context,
5406 args->lock_context,
5407 FMODE_READ))
5408 return false;
5409 rpc_restart_call_prepare(task);
5410 return true;
5411}
5412
5413static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5414 struct nfs_pgio_header *hdr)
5415{
5416 struct nfs_server *server = NFS_SERVER(hdr->inode);
5417 struct rpc_message *msg = &task->tk_msg;
5418
5419 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5420 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5421 server->caps &= ~NFS_CAP_READ_PLUS;
5422 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5423 rpc_restart_call_prepare(task);
5424 return true;
5425 }
5426 return false;
5427}
5428
5429static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5430{
5431 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5432 return -EAGAIN;
5433 if (nfs4_read_stateid_changed(task, &hdr->args))
5434 return -EAGAIN;
5435 if (nfs4_read_plus_not_supported(task, hdr))
5436 return -EAGAIN;
5437 if (task->tk_status > 0)
5438 nfs_invalidate_atime(hdr->inode);
5439 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5440 nfs4_read_done_cb(task, hdr);
5441}
5442
5443#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
5444static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5445 struct rpc_message *msg)
5446{
5447 /* Note: We don't use READ_PLUS with pNFS yet */
5448 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
5449 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5450}
5451#else
5452static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5453 struct rpc_message *msg)
5454{
5455}
5456#endif /* CONFIG_NFS_V4_2 */
5457
5458static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5459 struct rpc_message *msg)
5460{
5461 hdr->timestamp = jiffies;
5462 if (!hdr->pgio_done_cb)
5463 hdr->pgio_done_cb = nfs4_read_done_cb;
5464 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5465 nfs42_read_plus_support(hdr, msg);
5466 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5467}
5468
5469static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5470 struct nfs_pgio_header *hdr)
5471{
5472 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5473 &hdr->args.seq_args,
5474 &hdr->res.seq_res,
5475 task))
5476 return 0;
5477 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5478 hdr->args.lock_context,
5479 hdr->rw_mode) == -EIO)
5480 return -EIO;
5481 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5482 return -EIO;
5483 return 0;
5484}
5485
5486static int nfs4_write_done_cb(struct rpc_task *task,
5487 struct nfs_pgio_header *hdr)
5488{
5489 struct inode *inode = hdr->inode;
5490
5491 trace_nfs4_write(hdr, task->tk_status);
5492 if (task->tk_status < 0) {
5493 struct nfs4_exception exception = {
5494 .inode = hdr->inode,
5495 .state = hdr->args.context->state,
5496 .stateid = &hdr->args.stateid,
5497 };
5498 task->tk_status = nfs4_async_handle_exception(task,
5499 NFS_SERVER(inode), task->tk_status,
5500 &exception);
5501 if (exception.retry) {
5502 rpc_restart_call_prepare(task);
5503 return -EAGAIN;
5504 }
5505 }
5506 if (task->tk_status >= 0) {
5507 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5508 nfs_writeback_update_inode(hdr);
5509 }
5510 return 0;
5511}
5512
5513static bool nfs4_write_stateid_changed(struct rpc_task *task,
5514 struct nfs_pgio_args *args)
5515{
5516
5517 if (!nfs4_error_stateid_expired(task->tk_status) ||
5518 nfs4_stateid_is_current(&args->stateid,
5519 args->context,
5520 args->lock_context,
5521 FMODE_WRITE))
5522 return false;
5523 rpc_restart_call_prepare(task);
5524 return true;
5525}
5526
5527static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5528{
5529 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5530 return -EAGAIN;
5531 if (nfs4_write_stateid_changed(task, &hdr->args))
5532 return -EAGAIN;
5533 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5534 nfs4_write_done_cb(task, hdr);
5535}
5536
5537static
5538bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5539{
5540 /* Don't request attributes for pNFS or O_DIRECT writes */
5541 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5542 return false;
5543 /* Otherwise, request attributes if and only if we don't hold
5544 * a delegation
5545 */
5546 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5547}
5548
5549void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5550 struct inode *inode, unsigned long cache_validity)
5551{
5552 struct nfs_server *server = NFS_SERVER(inode);
5553 unsigned int i;
5554
5555 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5556 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5557
5558 if (cache_validity & NFS_INO_INVALID_CHANGE)
5559 bitmask[0] |= FATTR4_WORD0_CHANGE;
5560 if (cache_validity & NFS_INO_INVALID_ATIME)
5561 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5562 if (cache_validity & NFS_INO_INVALID_MODE)
5563 bitmask[1] |= FATTR4_WORD1_MODE;
5564 if (cache_validity & NFS_INO_INVALID_OTHER)
5565 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5566 if (cache_validity & NFS_INO_INVALID_NLINK)
5567 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5568 if (cache_validity & NFS_INO_INVALID_CTIME)
5569 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5570 if (cache_validity & NFS_INO_INVALID_MTIME)
5571 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5572 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5573 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5574
5575 if (cache_validity & NFS_INO_INVALID_SIZE)
5576 bitmask[0] |= FATTR4_WORD0_SIZE;
5577
5578 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5579 bitmask[i] &= server->attr_bitmask[i];
5580}
5581
5582static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5583 struct rpc_message *msg,
5584 struct rpc_clnt **clnt)
5585{
5586 struct nfs_server *server = NFS_SERVER(hdr->inode);
5587
5588 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5589 hdr->args.bitmask = NULL;
5590 hdr->res.fattr = NULL;
5591 } else {
5592 nfs4_bitmask_set(hdr->args.bitmask_store,
5593 server->cache_consistency_bitmask,
5594 hdr->inode, NFS_INO_INVALID_BLOCKS);
5595 hdr->args.bitmask = hdr->args.bitmask_store;
5596 }
5597
5598 if (!hdr->pgio_done_cb)
5599 hdr->pgio_done_cb = nfs4_write_done_cb;
5600 hdr->res.server = server;
5601 hdr->timestamp = jiffies;
5602
5603 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5604 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5605 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5606}
5607
5608static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5609{
5610 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5611 &data->args.seq_args,
5612 &data->res.seq_res,
5613 task);
5614}
5615
5616static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5617{
5618 struct inode *inode = data->inode;
5619
5620 trace_nfs4_commit(data, task->tk_status);
5621 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5622 NULL, NULL) == -EAGAIN) {
5623 rpc_restart_call_prepare(task);
5624 return -EAGAIN;
5625 }
5626 return 0;
5627}
5628
5629static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5630{
5631 if (!nfs4_sequence_done(task, &data->res.seq_res))
5632 return -EAGAIN;
5633 return data->commit_done_cb(task, data);
5634}
5635
5636static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5637 struct rpc_clnt **clnt)
5638{
5639 struct nfs_server *server = NFS_SERVER(data->inode);
5640
5641 if (data->commit_done_cb == NULL)
5642 data->commit_done_cb = nfs4_commit_done_cb;
5643 data->res.server = server;
5644 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5645 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5646 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5647}
5648
5649static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5650 struct nfs_commitres *res)
5651{
5652 struct inode *dst_inode = file_inode(dst);
5653 struct nfs_server *server = NFS_SERVER(dst_inode);
5654 struct rpc_message msg = {
5655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5656 .rpc_argp = args,
5657 .rpc_resp = res,
5658 };
5659
5660 args->fh = NFS_FH(dst_inode);
5661 return nfs4_call_sync(server->client, server, &msg,
5662 &args->seq_args, &res->seq_res, 1);
5663}
5664
5665int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5666{
5667 struct nfs_commitargs args = {
5668 .offset = offset,
5669 .count = count,
5670 };
5671 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5672 struct nfs4_exception exception = { };
5673 int status;
5674
5675 do {
5676 status = _nfs4_proc_commit(dst, &args, res);
5677 status = nfs4_handle_exception(dst_server, status, &exception);
5678 } while (exception.retry);
5679
5680 return status;
5681}
5682
5683struct nfs4_renewdata {
5684 struct nfs_client *client;
5685 unsigned long timestamp;
5686};
5687
5688/*
5689 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5690 * standalone procedure for queueing an asynchronous RENEW.
5691 */
5692static void nfs4_renew_release(void *calldata)
5693{
5694 struct nfs4_renewdata *data = calldata;
5695 struct nfs_client *clp = data->client;
5696
5697 if (refcount_read(&clp->cl_count) > 1)
5698 nfs4_schedule_state_renewal(clp);
5699 nfs_put_client(clp);
5700 kfree(data);
5701}
5702
5703static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5704{
5705 struct nfs4_renewdata *data = calldata;
5706 struct nfs_client *clp = data->client;
5707 unsigned long timestamp = data->timestamp;
5708
5709 trace_nfs4_renew_async(clp, task->tk_status);
5710 switch (task->tk_status) {
5711 case 0:
5712 break;
5713 case -NFS4ERR_LEASE_MOVED:
5714 nfs4_schedule_lease_moved_recovery(clp);
5715 break;
5716 default:
5717 /* Unless we're shutting down, schedule state recovery! */
5718 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5719 return;
5720 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5721 nfs4_schedule_lease_recovery(clp);
5722 return;
5723 }
5724 nfs4_schedule_path_down_recovery(clp);
5725 }
5726 do_renew_lease(clp, timestamp);
5727}
5728
5729static const struct rpc_call_ops nfs4_renew_ops = {
5730 .rpc_call_done = nfs4_renew_done,
5731 .rpc_release = nfs4_renew_release,
5732};
5733
5734static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5735{
5736 struct rpc_message msg = {
5737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5738 .rpc_argp = clp,
5739 .rpc_cred = cred,
5740 };
5741 struct nfs4_renewdata *data;
5742
5743 if (renew_flags == 0)
5744 return 0;
5745 if (!refcount_inc_not_zero(&clp->cl_count))
5746 return -EIO;
5747 data = kmalloc(sizeof(*data), GFP_NOFS);
5748 if (data == NULL) {
5749 nfs_put_client(clp);
5750 return -ENOMEM;
5751 }
5752 data->client = clp;
5753 data->timestamp = jiffies;
5754 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5755 &nfs4_renew_ops, data);
5756}
5757
5758static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5759{
5760 struct rpc_message msg = {
5761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5762 .rpc_argp = clp,
5763 .rpc_cred = cred,
5764 };
5765 unsigned long now = jiffies;
5766 int status;
5767
5768 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5769 if (status < 0)
5770 return status;
5771 do_renew_lease(clp, now);
5772 return 0;
5773}
5774
5775static bool nfs4_server_supports_acls(const struct nfs_server *server,
5776 enum nfs4_acl_type type)
5777{
5778 switch (type) {
5779 default:
5780 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5781 case NFS4ACL_DACL:
5782 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5783 case NFS4ACL_SACL:
5784 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5785 }
5786}
5787
5788/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5789 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5790 * the stack.
5791 */
5792#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5793
5794int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5795 struct page **pages)
5796{
5797 struct page *newpage, **spages;
5798 int rc = 0;
5799 size_t len;
5800 spages = pages;
5801
5802 do {
5803 len = min_t(size_t, PAGE_SIZE, buflen);
5804 newpage = alloc_page(GFP_KERNEL);
5805
5806 if (newpage == NULL)
5807 goto unwind;
5808 memcpy(page_address(newpage), buf, len);
5809 buf += len;
5810 buflen -= len;
5811 *pages++ = newpage;
5812 rc++;
5813 } while (buflen != 0);
5814
5815 return rc;
5816
5817unwind:
5818 for(; rc > 0; rc--)
5819 __free_page(spages[rc-1]);
5820 return -ENOMEM;
5821}
5822
5823struct nfs4_cached_acl {
5824 enum nfs4_acl_type type;
5825 int cached;
5826 size_t len;
5827 char data[];
5828};
5829
5830static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5831{
5832 struct nfs_inode *nfsi = NFS_I(inode);
5833
5834 spin_lock(&inode->i_lock);
5835 kfree(nfsi->nfs4_acl);
5836 nfsi->nfs4_acl = acl;
5837 spin_unlock(&inode->i_lock);
5838}
5839
5840static void nfs4_zap_acl_attr(struct inode *inode)
5841{
5842 nfs4_set_cached_acl(inode, NULL);
5843}
5844
5845static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5846 size_t buflen, enum nfs4_acl_type type)
5847{
5848 struct nfs_inode *nfsi = NFS_I(inode);
5849 struct nfs4_cached_acl *acl;
5850 int ret = -ENOENT;
5851
5852 spin_lock(&inode->i_lock);
5853 acl = nfsi->nfs4_acl;
5854 if (acl == NULL)
5855 goto out;
5856 if (acl->type != type)
5857 goto out;
5858 if (buf == NULL) /* user is just asking for length */
5859 goto out_len;
5860 if (acl->cached == 0)
5861 goto out;
5862 ret = -ERANGE; /* see getxattr(2) man page */
5863 if (acl->len > buflen)
5864 goto out;
5865 memcpy(buf, acl->data, acl->len);
5866out_len:
5867 ret = acl->len;
5868out:
5869 spin_unlock(&inode->i_lock);
5870 return ret;
5871}
5872
5873static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
5874 size_t pgbase, size_t acl_len,
5875 enum nfs4_acl_type type)
5876{
5877 struct nfs4_cached_acl *acl;
5878 size_t buflen = sizeof(*acl) + acl_len;
5879
5880 if (buflen <= PAGE_SIZE) {
5881 acl = kmalloc(buflen, GFP_KERNEL);
5882 if (acl == NULL)
5883 goto out;
5884 acl->cached = 1;
5885 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5886 } else {
5887 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5888 if (acl == NULL)
5889 goto out;
5890 acl->cached = 0;
5891 }
5892 acl->type = type;
5893 acl->len = acl_len;
5894out:
5895 nfs4_set_cached_acl(inode, acl);
5896}
5897
5898/*
5899 * The getxattr API returns the required buffer length when called with a
5900 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5901 * the required buf. On a NULL buf, we send a page of data to the server
5902 * guessing that the ACL request can be serviced by a page. If so, we cache
5903 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5904 * the cache. If not so, we throw away the page, and cache the required
5905 * length. The next getxattr call will then produce another round trip to
5906 * the server, this time with the input buf of the required size.
5907 */
5908static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
5909 size_t buflen, enum nfs4_acl_type type)
5910{
5911 struct page **pages;
5912 struct nfs_getaclargs args = {
5913 .fh = NFS_FH(inode),
5914 .acl_type = type,
5915 .acl_len = buflen,
5916 };
5917 struct nfs_getaclres res = {
5918 .acl_type = type,
5919 .acl_len = buflen,
5920 };
5921 struct rpc_message msg = {
5922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5923 .rpc_argp = &args,
5924 .rpc_resp = &res,
5925 };
5926 unsigned int npages;
5927 int ret = -ENOMEM, i;
5928 struct nfs_server *server = NFS_SERVER(inode);
5929
5930 if (buflen == 0)
5931 buflen = server->rsize;
5932
5933 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5934 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
5935 if (!pages)
5936 return -ENOMEM;
5937
5938 args.acl_pages = pages;
5939
5940 for (i = 0; i < npages; i++) {
5941 pages[i] = alloc_page(GFP_KERNEL);
5942 if (!pages[i])
5943 goto out_free;
5944 }
5945
5946 /* for decoding across pages */
5947 res.acl_scratch = alloc_page(GFP_KERNEL);
5948 if (!res.acl_scratch)
5949 goto out_free;
5950
5951 args.acl_len = npages * PAGE_SIZE;
5952
5953 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5954 __func__, buf, buflen, npages, args.acl_len);
5955 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5956 &msg, &args.seq_args, &res.seq_res, 0);
5957 if (ret)
5958 goto out_free;
5959
5960 /* Handle the case where the passed-in buffer is too short */
5961 if (res.acl_flags & NFS4_ACL_TRUNC) {
5962 /* Did the user only issue a request for the acl length? */
5963 if (buf == NULL)
5964 goto out_ok;
5965 ret = -ERANGE;
5966 goto out_free;
5967 }
5968 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
5969 type);
5970 if (buf) {
5971 if (res.acl_len > buflen) {
5972 ret = -ERANGE;
5973 goto out_free;
5974 }
5975 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5976 }
5977out_ok:
5978 ret = res.acl_len;
5979out_free:
5980 for (i = 0; i < npages; i++)
5981 if (pages[i])
5982 __free_page(pages[i]);
5983 if (res.acl_scratch)
5984 __free_page(res.acl_scratch);
5985 kfree(pages);
5986 return ret;
5987}
5988
5989static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
5990 size_t buflen, enum nfs4_acl_type type)
5991{
5992 struct nfs4_exception exception = {
5993 .interruptible = true,
5994 };
5995 ssize_t ret;
5996 do {
5997 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
5998 trace_nfs4_get_acl(inode, ret);
5999 if (ret >= 0)
6000 break;
6001 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6002 } while (exception.retry);
6003 return ret;
6004}
6005
6006static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6007 enum nfs4_acl_type type)
6008{
6009 struct nfs_server *server = NFS_SERVER(inode);
6010 int ret;
6011
6012 if (!nfs4_server_supports_acls(server, type))
6013 return -EOPNOTSUPP;
6014 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6015 if (ret < 0)
6016 return ret;
6017 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6018 nfs_zap_acl_cache(inode);
6019 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6020 if (ret != -ENOENT)
6021 /* -ENOENT is returned if there is no ACL or if there is an ACL
6022 * but no cached acl data, just the acl length */
6023 return ret;
6024 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6025}
6026
6027static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6028 size_t buflen, enum nfs4_acl_type type)
6029{
6030 struct nfs_server *server = NFS_SERVER(inode);
6031 struct page *pages[NFS4ACL_MAXPAGES];
6032 struct nfs_setaclargs arg = {
6033 .fh = NFS_FH(inode),
6034 .acl_type = type,
6035 .acl_len = buflen,
6036 .acl_pages = pages,
6037 };
6038 struct nfs_setaclres res;
6039 struct rpc_message msg = {
6040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6041 .rpc_argp = &arg,
6042 .rpc_resp = &res,
6043 };
6044 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6045 int ret, i;
6046
6047 /* You can't remove system.nfs4_acl: */
6048 if (buflen == 0)
6049 return -EINVAL;
6050 if (!nfs4_server_supports_acls(server, type))
6051 return -EOPNOTSUPP;
6052 if (npages > ARRAY_SIZE(pages))
6053 return -ERANGE;
6054 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6055 if (i < 0)
6056 return i;
6057 nfs4_inode_make_writeable(inode);
6058 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6059
6060 /*
6061 * Free each page after tx, so the only ref left is
6062 * held by the network stack
6063 */
6064 for (; i > 0; i--)
6065 put_page(pages[i-1]);
6066
6067 /*
6068 * Acl update can result in inode attribute update.
6069 * so mark the attribute cache invalid.
6070 */
6071 spin_lock(&inode->i_lock);
6072 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6073 NFS_INO_INVALID_CTIME |
6074 NFS_INO_REVAL_FORCED);
6075 spin_unlock(&inode->i_lock);
6076 nfs_access_zap_cache(inode);
6077 nfs_zap_acl_cache(inode);
6078 return ret;
6079}
6080
6081static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6082 size_t buflen, enum nfs4_acl_type type)
6083{
6084 struct nfs4_exception exception = { };
6085 int err;
6086 do {
6087 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6088 trace_nfs4_set_acl(inode, err);
6089 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6090 /*
6091 * no need to retry since the kernel
6092 * isn't involved in encoding the ACEs.
6093 */
6094 err = -EINVAL;
6095 break;
6096 }
6097 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6098 &exception);
6099 } while (exception.retry);
6100 return err;
6101}
6102
6103#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6104static int _nfs4_get_security_label(struct inode *inode, void *buf,
6105 size_t buflen)
6106{
6107 struct nfs_server *server = NFS_SERVER(inode);
6108 struct nfs4_label label = {0, 0, buflen, buf};
6109
6110 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6111 struct nfs_fattr fattr = {
6112 .label = &label,
6113 };
6114 struct nfs4_getattr_arg arg = {
6115 .fh = NFS_FH(inode),
6116 .bitmask = bitmask,
6117 };
6118 struct nfs4_getattr_res res = {
6119 .fattr = &fattr,
6120 .server = server,
6121 };
6122 struct rpc_message msg = {
6123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6124 .rpc_argp = &arg,
6125 .rpc_resp = &res,
6126 };
6127 int ret;
6128
6129 nfs_fattr_init(&fattr);
6130
6131 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6132 if (ret)
6133 return ret;
6134 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6135 return -ENOENT;
6136 return label.len;
6137}
6138
6139static int nfs4_get_security_label(struct inode *inode, void *buf,
6140 size_t buflen)
6141{
6142 struct nfs4_exception exception = {
6143 .interruptible = true,
6144 };
6145 int err;
6146
6147 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6148 return -EOPNOTSUPP;
6149
6150 do {
6151 err = _nfs4_get_security_label(inode, buf, buflen);
6152 trace_nfs4_get_security_label(inode, err);
6153 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6154 &exception);
6155 } while (exception.retry);
6156 return err;
6157}
6158
6159static int _nfs4_do_set_security_label(struct inode *inode,
6160 struct nfs4_label *ilabel,
6161 struct nfs_fattr *fattr)
6162{
6163
6164 struct iattr sattr = {0};
6165 struct nfs_server *server = NFS_SERVER(inode);
6166 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6167 struct nfs_setattrargs arg = {
6168 .fh = NFS_FH(inode),
6169 .iap = &sattr,
6170 .server = server,
6171 .bitmask = bitmask,
6172 .label = ilabel,
6173 };
6174 struct nfs_setattrres res = {
6175 .fattr = fattr,
6176 .server = server,
6177 };
6178 struct rpc_message msg = {
6179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6180 .rpc_argp = &arg,
6181 .rpc_resp = &res,
6182 };
6183 int status;
6184
6185 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6186
6187 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6188 if (status)
6189 dprintk("%s failed: %d\n", __func__, status);
6190
6191 return status;
6192}
6193
6194static int nfs4_do_set_security_label(struct inode *inode,
6195 struct nfs4_label *ilabel,
6196 struct nfs_fattr *fattr)
6197{
6198 struct nfs4_exception exception = { };
6199 int err;
6200
6201 do {
6202 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6203 trace_nfs4_set_security_label(inode, err);
6204 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6205 &exception);
6206 } while (exception.retry);
6207 return err;
6208}
6209
6210static int
6211nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6212{
6213 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6214 struct nfs_fattr *fattr;
6215 int status;
6216
6217 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6218 return -EOPNOTSUPP;
6219
6220 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6221 if (fattr == NULL)
6222 return -ENOMEM;
6223
6224 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6225 if (status == 0)
6226 nfs_setsecurity(inode, fattr);
6227
6228 return status;
6229}
6230#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6231
6232
6233static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6234 nfs4_verifier *bootverf)
6235{
6236 __be32 verf[2];
6237
6238 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6239 /* An impossible timestamp guarantees this value
6240 * will never match a generated boot time. */
6241 verf[0] = cpu_to_be32(U32_MAX);
6242 verf[1] = cpu_to_be32(U32_MAX);
6243 } else {
6244 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6245 u64 ns = ktime_to_ns(nn->boot_time);
6246
6247 verf[0] = cpu_to_be32(ns >> 32);
6248 verf[1] = cpu_to_be32(ns);
6249 }
6250 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6251}
6252
6253static size_t
6254nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6255{
6256 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6257 struct nfs_netns_client *nn_clp = nn->nfs_client;
6258 const char *id;
6259
6260 buf[0] = '\0';
6261
6262 if (nn_clp) {
6263 rcu_read_lock();
6264 id = rcu_dereference(nn_clp->identifier);
6265 if (id)
6266 strscpy(buf, id, buflen);
6267 rcu_read_unlock();
6268 }
6269
6270 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6271 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6272
6273 return strlen(buf);
6274}
6275
6276static int
6277nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6278{
6279 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6280 size_t buflen;
6281 size_t len;
6282 char *str;
6283
6284 if (clp->cl_owner_id != NULL)
6285 return 0;
6286
6287 rcu_read_lock();
6288 len = 14 +
6289 strlen(clp->cl_rpcclient->cl_nodename) +
6290 1 +
6291 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6292 1;
6293 rcu_read_unlock();
6294
6295 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6296 if (buflen)
6297 len += buflen + 1;
6298
6299 if (len > NFS4_OPAQUE_LIMIT + 1)
6300 return -EINVAL;
6301
6302 /*
6303 * Since this string is allocated at mount time, and held until the
6304 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6305 * about a memory-reclaim deadlock.
6306 */
6307 str = kmalloc(len, GFP_KERNEL);
6308 if (!str)
6309 return -ENOMEM;
6310
6311 rcu_read_lock();
6312 if (buflen)
6313 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6314 clp->cl_rpcclient->cl_nodename, buf,
6315 rpc_peeraddr2str(clp->cl_rpcclient,
6316 RPC_DISPLAY_ADDR));
6317 else
6318 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6319 clp->cl_rpcclient->cl_nodename,
6320 rpc_peeraddr2str(clp->cl_rpcclient,
6321 RPC_DISPLAY_ADDR));
6322 rcu_read_unlock();
6323
6324 clp->cl_owner_id = str;
6325 return 0;
6326}
6327
6328static int
6329nfs4_init_uniform_client_string(struct nfs_client *clp)
6330{
6331 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6332 size_t buflen;
6333 size_t len;
6334 char *str;
6335
6336 if (clp->cl_owner_id != NULL)
6337 return 0;
6338
6339 len = 10 + 10 + 1 + 10 + 1 +
6340 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6341
6342 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6343 if (buflen)
6344 len += buflen + 1;
6345
6346 if (len > NFS4_OPAQUE_LIMIT + 1)
6347 return -EINVAL;
6348
6349 /*
6350 * Since this string is allocated at mount time, and held until the
6351 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6352 * about a memory-reclaim deadlock.
6353 */
6354 str = kmalloc(len, GFP_KERNEL);
6355 if (!str)
6356 return -ENOMEM;
6357
6358 if (buflen)
6359 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6360 clp->rpc_ops->version, clp->cl_minorversion,
6361 buf, clp->cl_rpcclient->cl_nodename);
6362 else
6363 scnprintf(str, len, "Linux NFSv%u.%u %s",
6364 clp->rpc_ops->version, clp->cl_minorversion,
6365 clp->cl_rpcclient->cl_nodename);
6366 clp->cl_owner_id = str;
6367 return 0;
6368}
6369
6370/*
6371 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6372 * services. Advertise one based on the address family of the
6373 * clientaddr.
6374 */
6375static unsigned int
6376nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6377{
6378 if (strchr(clp->cl_ipaddr, ':') != NULL)
6379 return scnprintf(buf, len, "tcp6");
6380 else
6381 return scnprintf(buf, len, "tcp");
6382}
6383
6384static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6385{
6386 struct nfs4_setclientid *sc = calldata;
6387
6388 if (task->tk_status == 0)
6389 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6390}
6391
6392static const struct rpc_call_ops nfs4_setclientid_ops = {
6393 .rpc_call_done = nfs4_setclientid_done,
6394};
6395
6396/**
6397 * nfs4_proc_setclientid - Negotiate client ID
6398 * @clp: state data structure
6399 * @program: RPC program for NFSv4 callback service
6400 * @port: IP port number for NFS4 callback service
6401 * @cred: credential to use for this call
6402 * @res: where to place the result
6403 *
6404 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6405 */
6406int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6407 unsigned short port, const struct cred *cred,
6408 struct nfs4_setclientid_res *res)
6409{
6410 nfs4_verifier sc_verifier;
6411 struct nfs4_setclientid setclientid = {
6412 .sc_verifier = &sc_verifier,
6413 .sc_prog = program,
6414 .sc_clnt = clp,
6415 };
6416 struct rpc_message msg = {
6417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6418 .rpc_argp = &setclientid,
6419 .rpc_resp = res,
6420 .rpc_cred = cred,
6421 };
6422 struct rpc_task_setup task_setup_data = {
6423 .rpc_client = clp->cl_rpcclient,
6424 .rpc_message = &msg,
6425 .callback_ops = &nfs4_setclientid_ops,
6426 .callback_data = &setclientid,
6427 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6428 };
6429 unsigned long now = jiffies;
6430 int status;
6431
6432 /* nfs_client_id4 */
6433 nfs4_init_boot_verifier(clp, &sc_verifier);
6434
6435 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6436 status = nfs4_init_uniform_client_string(clp);
6437 else
6438 status = nfs4_init_nonuniform_client_string(clp);
6439
6440 if (status)
6441 goto out;
6442
6443 /* cb_client4 */
6444 setclientid.sc_netid_len =
6445 nfs4_init_callback_netid(clp,
6446 setclientid.sc_netid,
6447 sizeof(setclientid.sc_netid));
6448 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6449 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6450 clp->cl_ipaddr, port >> 8, port & 255);
6451
6452 dprintk("NFS call setclientid auth=%s, '%s'\n",
6453 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6454 clp->cl_owner_id);
6455
6456 status = nfs4_call_sync_custom(&task_setup_data);
6457 if (setclientid.sc_cred) {
6458 kfree(clp->cl_acceptor);
6459 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6460 put_rpccred(setclientid.sc_cred);
6461 }
6462
6463 if (status == 0)
6464 do_renew_lease(clp, now);
6465out:
6466 trace_nfs4_setclientid(clp, status);
6467 dprintk("NFS reply setclientid: %d\n", status);
6468 return status;
6469}
6470
6471/**
6472 * nfs4_proc_setclientid_confirm - Confirm client ID
6473 * @clp: state data structure
6474 * @arg: result of a previous SETCLIENTID
6475 * @cred: credential to use for this call
6476 *
6477 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6478 */
6479int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6480 struct nfs4_setclientid_res *arg,
6481 const struct cred *cred)
6482{
6483 struct rpc_message msg = {
6484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6485 .rpc_argp = arg,
6486 .rpc_cred = cred,
6487 };
6488 int status;
6489
6490 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6491 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6492 clp->cl_clientid);
6493 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6494 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6495 trace_nfs4_setclientid_confirm(clp, status);
6496 dprintk("NFS reply setclientid_confirm: %d\n", status);
6497 return status;
6498}
6499
6500struct nfs4_delegreturndata {
6501 struct nfs4_delegreturnargs args;
6502 struct nfs4_delegreturnres res;
6503 struct nfs_fh fh;
6504 nfs4_stateid stateid;
6505 unsigned long timestamp;
6506 struct {
6507 struct nfs4_layoutreturn_args arg;
6508 struct nfs4_layoutreturn_res res;
6509 struct nfs4_xdr_opaque_data ld_private;
6510 u32 roc_barrier;
6511 bool roc;
6512 } lr;
6513 struct nfs_fattr fattr;
6514 int rpc_status;
6515 struct inode *inode;
6516};
6517
6518static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6519{
6520 struct nfs4_delegreturndata *data = calldata;
6521 struct nfs4_exception exception = {
6522 .inode = data->inode,
6523 .stateid = &data->stateid,
6524 .task_is_privileged = data->args.seq_args.sa_privileged,
6525 };
6526
6527 if (!nfs4_sequence_done(task, &data->res.seq_res))
6528 return;
6529
6530 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6531
6532 /* Handle Layoutreturn errors */
6533 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6534 &data->res.lr_ret) == -EAGAIN)
6535 goto out_restart;
6536
6537 switch (task->tk_status) {
6538 case 0:
6539 renew_lease(data->res.server, data->timestamp);
6540 break;
6541 case -NFS4ERR_ADMIN_REVOKED:
6542 case -NFS4ERR_DELEG_REVOKED:
6543 case -NFS4ERR_EXPIRED:
6544 nfs4_free_revoked_stateid(data->res.server,
6545 data->args.stateid,
6546 task->tk_msg.rpc_cred);
6547 fallthrough;
6548 case -NFS4ERR_BAD_STATEID:
6549 case -NFS4ERR_STALE_STATEID:
6550 case -ETIMEDOUT:
6551 task->tk_status = 0;
6552 break;
6553 case -NFS4ERR_OLD_STATEID:
6554 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6555 nfs4_stateid_seqid_inc(&data->stateid);
6556 if (data->args.bitmask) {
6557 data->args.bitmask = NULL;
6558 data->res.fattr = NULL;
6559 }
6560 goto out_restart;
6561 case -NFS4ERR_ACCESS:
6562 if (data->args.bitmask) {
6563 data->args.bitmask = NULL;
6564 data->res.fattr = NULL;
6565 goto out_restart;
6566 }
6567 fallthrough;
6568 default:
6569 task->tk_status = nfs4_async_handle_exception(task,
6570 data->res.server, task->tk_status,
6571 &exception);
6572 if (exception.retry)
6573 goto out_restart;
6574 }
6575 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6576 data->rpc_status = task->tk_status;
6577 return;
6578out_restart:
6579 task->tk_status = 0;
6580 rpc_restart_call_prepare(task);
6581}
6582
6583static void nfs4_delegreturn_release(void *calldata)
6584{
6585 struct nfs4_delegreturndata *data = calldata;
6586 struct inode *inode = data->inode;
6587
6588 if (data->lr.roc)
6589 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6590 data->res.lr_ret);
6591 if (inode) {
6592 nfs4_fattr_set_prechange(&data->fattr,
6593 inode_peek_iversion_raw(inode));
6594 nfs_refresh_inode(inode, &data->fattr);
6595 nfs_iput_and_deactive(inode);
6596 }
6597 kfree(calldata);
6598}
6599
6600static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6601{
6602 struct nfs4_delegreturndata *d_data;
6603 struct pnfs_layout_hdr *lo;
6604
6605 d_data = (struct nfs4_delegreturndata *)data;
6606
6607 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6608 nfs4_sequence_done(task, &d_data->res.seq_res);
6609 return;
6610 }
6611
6612 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6613 if (lo && !pnfs_layout_is_valid(lo)) {
6614 d_data->args.lr_args = NULL;
6615 d_data->res.lr_res = NULL;
6616 }
6617
6618 nfs4_setup_sequence(d_data->res.server->nfs_client,
6619 &d_data->args.seq_args,
6620 &d_data->res.seq_res,
6621 task);
6622}
6623
6624static const struct rpc_call_ops nfs4_delegreturn_ops = {
6625 .rpc_call_prepare = nfs4_delegreturn_prepare,
6626 .rpc_call_done = nfs4_delegreturn_done,
6627 .rpc_release = nfs4_delegreturn_release,
6628};
6629
6630static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6631{
6632 struct nfs4_delegreturndata *data;
6633 struct nfs_server *server = NFS_SERVER(inode);
6634 struct rpc_task *task;
6635 struct rpc_message msg = {
6636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6637 .rpc_cred = cred,
6638 };
6639 struct rpc_task_setup task_setup_data = {
6640 .rpc_client = server->client,
6641 .rpc_message = &msg,
6642 .callback_ops = &nfs4_delegreturn_ops,
6643 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
6644 };
6645 int status = 0;
6646
6647 data = kzalloc(sizeof(*data), GFP_KERNEL);
6648 if (data == NULL)
6649 return -ENOMEM;
6650
6651 nfs4_state_protect(server->nfs_client,
6652 NFS_SP4_MACH_CRED_CLEANUP,
6653 &task_setup_data.rpc_client, &msg);
6654
6655 data->args.fhandle = &data->fh;
6656 data->args.stateid = &data->stateid;
6657 nfs4_bitmask_set(data->args.bitmask_store,
6658 server->cache_consistency_bitmask, inode, 0);
6659 data->args.bitmask = data->args.bitmask_store;
6660 nfs_copy_fh(&data->fh, NFS_FH(inode));
6661 nfs4_stateid_copy(&data->stateid, stateid);
6662 data->res.fattr = &data->fattr;
6663 data->res.server = server;
6664 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6665 data->lr.arg.ld_private = &data->lr.ld_private;
6666 nfs_fattr_init(data->res.fattr);
6667 data->timestamp = jiffies;
6668 data->rpc_status = 0;
6669 data->inode = nfs_igrab_and_active(inode);
6670 if (data->inode || issync) {
6671 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6672 cred);
6673 if (data->lr.roc) {
6674 data->args.lr_args = &data->lr.arg;
6675 data->res.lr_res = &data->lr.res;
6676 }
6677 }
6678
6679 if (!data->inode)
6680 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6681 1);
6682 else
6683 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6684 0);
6685 task_setup_data.callback_data = data;
6686 msg.rpc_argp = &data->args;
6687 msg.rpc_resp = &data->res;
6688 task = rpc_run_task(&task_setup_data);
6689 if (IS_ERR(task))
6690 return PTR_ERR(task);
6691 if (!issync)
6692 goto out;
6693 status = rpc_wait_for_completion_task(task);
6694 if (status != 0)
6695 goto out;
6696 status = data->rpc_status;
6697out:
6698 rpc_put_task(task);
6699 return status;
6700}
6701
6702int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
6703{
6704 struct nfs_server *server = NFS_SERVER(inode);
6705 struct nfs4_exception exception = { };
6706 int err;
6707 do {
6708 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6709 trace_nfs4_delegreturn(inode, stateid, err);
6710 switch (err) {
6711 case -NFS4ERR_STALE_STATEID:
6712 case -NFS4ERR_EXPIRED:
6713 case 0:
6714 return 0;
6715 }
6716 err = nfs4_handle_exception(server, err, &exception);
6717 } while (exception.retry);
6718 return err;
6719}
6720
6721static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6722{
6723 struct inode *inode = state->inode;
6724 struct nfs_server *server = NFS_SERVER(inode);
6725 struct nfs_client *clp = server->nfs_client;
6726 struct nfs_lockt_args arg = {
6727 .fh = NFS_FH(inode),
6728 .fl = request,
6729 };
6730 struct nfs_lockt_res res = {
6731 .denied = request,
6732 };
6733 struct rpc_message msg = {
6734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6735 .rpc_argp = &arg,
6736 .rpc_resp = &res,
6737 .rpc_cred = state->owner->so_cred,
6738 };
6739 struct nfs4_lock_state *lsp;
6740 int status;
6741
6742 arg.lock_owner.clientid = clp->cl_clientid;
6743 status = nfs4_set_lock_state(state, request);
6744 if (status != 0)
6745 goto out;
6746 lsp = request->fl_u.nfs4_fl.owner;
6747 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6748 arg.lock_owner.s_dev = server->s_dev;
6749 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6750 switch (status) {
6751 case 0:
6752 request->fl_type = F_UNLCK;
6753 break;
6754 case -NFS4ERR_DENIED:
6755 status = 0;
6756 }
6757 request->fl_ops->fl_release_private(request);
6758 request->fl_ops = NULL;
6759out:
6760 return status;
6761}
6762
6763static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6764{
6765 struct nfs4_exception exception = {
6766 .interruptible = true,
6767 };
6768 int err;
6769
6770 do {
6771 err = _nfs4_proc_getlk(state, cmd, request);
6772 trace_nfs4_get_lock(request, state, cmd, err);
6773 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6774 &exception);
6775 } while (exception.retry);
6776 return err;
6777}
6778
6779/*
6780 * Update the seqid of a lock stateid after receiving
6781 * NFS4ERR_OLD_STATEID
6782 */
6783static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6784 struct nfs4_lock_state *lsp)
6785{
6786 struct nfs4_state *state = lsp->ls_state;
6787 bool ret = false;
6788
6789 spin_lock(&state->state_lock);
6790 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6791 goto out;
6792 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6793 nfs4_stateid_seqid_inc(dst);
6794 else
6795 dst->seqid = lsp->ls_stateid.seqid;
6796 ret = true;
6797out:
6798 spin_unlock(&state->state_lock);
6799 return ret;
6800}
6801
6802static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6803 struct nfs4_lock_state *lsp)
6804{
6805 struct nfs4_state *state = lsp->ls_state;
6806 bool ret;
6807
6808 spin_lock(&state->state_lock);
6809 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6810 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6811 spin_unlock(&state->state_lock);
6812 return ret;
6813}
6814
6815struct nfs4_unlockdata {
6816 struct nfs_locku_args arg;
6817 struct nfs_locku_res res;
6818 struct nfs4_lock_state *lsp;
6819 struct nfs_open_context *ctx;
6820 struct nfs_lock_context *l_ctx;
6821 struct file_lock fl;
6822 struct nfs_server *server;
6823 unsigned long timestamp;
6824};
6825
6826static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6827 struct nfs_open_context *ctx,
6828 struct nfs4_lock_state *lsp,
6829 struct nfs_seqid *seqid)
6830{
6831 struct nfs4_unlockdata *p;
6832 struct nfs4_state *state = lsp->ls_state;
6833 struct inode *inode = state->inode;
6834
6835 p = kzalloc(sizeof(*p), GFP_KERNEL);
6836 if (p == NULL)
6837 return NULL;
6838 p->arg.fh = NFS_FH(inode);
6839 p->arg.fl = &p->fl;
6840 p->arg.seqid = seqid;
6841 p->res.seqid = seqid;
6842 p->lsp = lsp;
6843 /* Ensure we don't close file until we're done freeing locks! */
6844 p->ctx = get_nfs_open_context(ctx);
6845 p->l_ctx = nfs_get_lock_context(ctx);
6846 locks_init_lock(&p->fl);
6847 locks_copy_lock(&p->fl, fl);
6848 p->server = NFS_SERVER(inode);
6849 spin_lock(&state->state_lock);
6850 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6851 spin_unlock(&state->state_lock);
6852 return p;
6853}
6854
6855static void nfs4_locku_release_calldata(void *data)
6856{
6857 struct nfs4_unlockdata *calldata = data;
6858 nfs_free_seqid(calldata->arg.seqid);
6859 nfs4_put_lock_state(calldata->lsp);
6860 nfs_put_lock_context(calldata->l_ctx);
6861 put_nfs_open_context(calldata->ctx);
6862 kfree(calldata);
6863}
6864
6865static void nfs4_locku_done(struct rpc_task *task, void *data)
6866{
6867 struct nfs4_unlockdata *calldata = data;
6868 struct nfs4_exception exception = {
6869 .inode = calldata->lsp->ls_state->inode,
6870 .stateid = &calldata->arg.stateid,
6871 };
6872
6873 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6874 return;
6875 switch (task->tk_status) {
6876 case 0:
6877 renew_lease(calldata->server, calldata->timestamp);
6878 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6879 if (nfs4_update_lock_stateid(calldata->lsp,
6880 &calldata->res.stateid))
6881 break;
6882 fallthrough;
6883 case -NFS4ERR_ADMIN_REVOKED:
6884 case -NFS4ERR_EXPIRED:
6885 nfs4_free_revoked_stateid(calldata->server,
6886 &calldata->arg.stateid,
6887 task->tk_msg.rpc_cred);
6888 fallthrough;
6889 case -NFS4ERR_BAD_STATEID:
6890 case -NFS4ERR_STALE_STATEID:
6891 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6892 calldata->lsp))
6893 rpc_restart_call_prepare(task);
6894 break;
6895 case -NFS4ERR_OLD_STATEID:
6896 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6897 calldata->lsp))
6898 rpc_restart_call_prepare(task);
6899 break;
6900 default:
6901 task->tk_status = nfs4_async_handle_exception(task,
6902 calldata->server, task->tk_status,
6903 &exception);
6904 if (exception.retry)
6905 rpc_restart_call_prepare(task);
6906 }
6907 nfs_release_seqid(calldata->arg.seqid);
6908}
6909
6910static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6911{
6912 struct nfs4_unlockdata *calldata = data;
6913
6914 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6915 nfs_async_iocounter_wait(task, calldata->l_ctx))
6916 return;
6917
6918 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6919 goto out_wait;
6920 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6921 /* Note: exit _without_ running nfs4_locku_done */
6922 goto out_no_action;
6923 }
6924 calldata->timestamp = jiffies;
6925 if (nfs4_setup_sequence(calldata->server->nfs_client,
6926 &calldata->arg.seq_args,
6927 &calldata->res.seq_res,
6928 task) != 0)
6929 nfs_release_seqid(calldata->arg.seqid);
6930 return;
6931out_no_action:
6932 task->tk_action = NULL;
6933out_wait:
6934 nfs4_sequence_done(task, &calldata->res.seq_res);
6935}
6936
6937static const struct rpc_call_ops nfs4_locku_ops = {
6938 .rpc_call_prepare = nfs4_locku_prepare,
6939 .rpc_call_done = nfs4_locku_done,
6940 .rpc_release = nfs4_locku_release_calldata,
6941};
6942
6943static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6944 struct nfs_open_context *ctx,
6945 struct nfs4_lock_state *lsp,
6946 struct nfs_seqid *seqid)
6947{
6948 struct nfs4_unlockdata *data;
6949 struct rpc_message msg = {
6950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6951 .rpc_cred = ctx->cred,
6952 };
6953 struct rpc_task_setup task_setup_data = {
6954 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6955 .rpc_message = &msg,
6956 .callback_ops = &nfs4_locku_ops,
6957 .workqueue = nfsiod_workqueue,
6958 .flags = RPC_TASK_ASYNC,
6959 };
6960 struct nfs_client *client =
6961 NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6962
6963 if (client->cl_minorversion)
6964 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6965
6966 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6967 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6968
6969 /* Ensure this is an unlock - when canceling a lock, the
6970 * canceled lock is passed in, and it won't be an unlock.
6971 */
6972 fl->fl_type = F_UNLCK;
6973 if (fl->fl_flags & FL_CLOSE)
6974 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6975
6976 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6977 if (data == NULL) {
6978 nfs_free_seqid(seqid);
6979 return ERR_PTR(-ENOMEM);
6980 }
6981
6982 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6983 msg.rpc_argp = &data->arg;
6984 msg.rpc_resp = &data->res;
6985 task_setup_data.callback_data = data;
6986 return rpc_run_task(&task_setup_data);
6987}
6988
6989static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6990{
6991 struct inode *inode = state->inode;
6992 struct nfs4_state_owner *sp = state->owner;
6993 struct nfs_inode *nfsi = NFS_I(inode);
6994 struct nfs_seqid *seqid;
6995 struct nfs4_lock_state *lsp;
6996 struct rpc_task *task;
6997 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6998 int status = 0;
6999 unsigned char fl_flags = request->fl_flags;
7000
7001 status = nfs4_set_lock_state(state, request);
7002 /* Unlock _before_ we do the RPC call */
7003 request->fl_flags |= FL_EXISTS;
7004 /* Exclude nfs_delegation_claim_locks() */
7005 mutex_lock(&sp->so_delegreturn_mutex);
7006 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7007 down_read(&nfsi->rwsem);
7008 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7009 up_read(&nfsi->rwsem);
7010 mutex_unlock(&sp->so_delegreturn_mutex);
7011 goto out;
7012 }
7013 up_read(&nfsi->rwsem);
7014 mutex_unlock(&sp->so_delegreturn_mutex);
7015 if (status != 0)
7016 goto out;
7017 /* Is this a delegated lock? */
7018 lsp = request->fl_u.nfs4_fl.owner;
7019 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7020 goto out;
7021 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7022 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7023 status = -ENOMEM;
7024 if (IS_ERR(seqid))
7025 goto out;
7026 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
7027 status = PTR_ERR(task);
7028 if (IS_ERR(task))
7029 goto out;
7030 status = rpc_wait_for_completion_task(task);
7031 rpc_put_task(task);
7032out:
7033 request->fl_flags = fl_flags;
7034 trace_nfs4_unlock(request, state, F_SETLK, status);
7035 return status;
7036}
7037
7038struct nfs4_lockdata {
7039 struct nfs_lock_args arg;
7040 struct nfs_lock_res res;
7041 struct nfs4_lock_state *lsp;
7042 struct nfs_open_context *ctx;
7043 struct file_lock fl;
7044 unsigned long timestamp;
7045 int rpc_status;
7046 int cancelled;
7047 struct nfs_server *server;
7048};
7049
7050static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7051 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7052 gfp_t gfp_mask)
7053{
7054 struct nfs4_lockdata *p;
7055 struct inode *inode = lsp->ls_state->inode;
7056 struct nfs_server *server = NFS_SERVER(inode);
7057 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7058
7059 p = kzalloc(sizeof(*p), gfp_mask);
7060 if (p == NULL)
7061 return NULL;
7062
7063 p->arg.fh = NFS_FH(inode);
7064 p->arg.fl = &p->fl;
7065 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7066 if (IS_ERR(p->arg.open_seqid))
7067 goto out_free;
7068 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7069 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7070 if (IS_ERR(p->arg.lock_seqid))
7071 goto out_free_seqid;
7072 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7073 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7074 p->arg.lock_owner.s_dev = server->s_dev;
7075 p->res.lock_seqid = p->arg.lock_seqid;
7076 p->lsp = lsp;
7077 p->server = server;
7078 p->ctx = get_nfs_open_context(ctx);
7079 locks_init_lock(&p->fl);
7080 locks_copy_lock(&p->fl, fl);
7081 return p;
7082out_free_seqid:
7083 nfs_free_seqid(p->arg.open_seqid);
7084out_free:
7085 kfree(p);
7086 return NULL;
7087}
7088
7089static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7090{
7091 struct nfs4_lockdata *data = calldata;
7092 struct nfs4_state *state = data->lsp->ls_state;
7093
7094 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7095 goto out_wait;
7096 /* Do we need to do an open_to_lock_owner? */
7097 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7098 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7099 goto out_release_lock_seqid;
7100 }
7101 nfs4_stateid_copy(&data->arg.open_stateid,
7102 &state->open_stateid);
7103 data->arg.new_lock_owner = 1;
7104 data->res.open_seqid = data->arg.open_seqid;
7105 } else {
7106 data->arg.new_lock_owner = 0;
7107 nfs4_stateid_copy(&data->arg.lock_stateid,
7108 &data->lsp->ls_stateid);
7109 }
7110 if (!nfs4_valid_open_stateid(state)) {
7111 data->rpc_status = -EBADF;
7112 task->tk_action = NULL;
7113 goto out_release_open_seqid;
7114 }
7115 data->timestamp = jiffies;
7116 if (nfs4_setup_sequence(data->server->nfs_client,
7117 &data->arg.seq_args,
7118 &data->res.seq_res,
7119 task) == 0)
7120 return;
7121out_release_open_seqid:
7122 nfs_release_seqid(data->arg.open_seqid);
7123out_release_lock_seqid:
7124 nfs_release_seqid(data->arg.lock_seqid);
7125out_wait:
7126 nfs4_sequence_done(task, &data->res.seq_res);
7127 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7128}
7129
7130static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7131{
7132 struct nfs4_lockdata *data = calldata;
7133 struct nfs4_lock_state *lsp = data->lsp;
7134
7135 if (!nfs4_sequence_done(task, &data->res.seq_res))
7136 return;
7137
7138 data->rpc_status = task->tk_status;
7139 switch (task->tk_status) {
7140 case 0:
7141 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7142 data->timestamp);
7143 if (data->arg.new_lock && !data->cancelled) {
7144 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
7145 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7146 goto out_restart;
7147 }
7148 if (data->arg.new_lock_owner != 0) {
7149 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7150 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7151 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7152 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7153 goto out_restart;
7154 break;
7155 case -NFS4ERR_BAD_STATEID:
7156 case -NFS4ERR_OLD_STATEID:
7157 case -NFS4ERR_STALE_STATEID:
7158 case -NFS4ERR_EXPIRED:
7159 if (data->arg.new_lock_owner != 0) {
7160 if (!nfs4_stateid_match(&data->arg.open_stateid,
7161 &lsp->ls_state->open_stateid))
7162 goto out_restart;
7163 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7164 &lsp->ls_stateid))
7165 goto out_restart;
7166 }
7167out_done:
7168 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7169 return;
7170out_restart:
7171 if (!data->cancelled)
7172 rpc_restart_call_prepare(task);
7173 goto out_done;
7174}
7175
7176static void nfs4_lock_release(void *calldata)
7177{
7178 struct nfs4_lockdata *data = calldata;
7179
7180 nfs_free_seqid(data->arg.open_seqid);
7181 if (data->cancelled && data->rpc_status == 0) {
7182 struct rpc_task *task;
7183 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7184 data->arg.lock_seqid);
7185 if (!IS_ERR(task))
7186 rpc_put_task_async(task);
7187 dprintk("%s: cancelling lock!\n", __func__);
7188 } else
7189 nfs_free_seqid(data->arg.lock_seqid);
7190 nfs4_put_lock_state(data->lsp);
7191 put_nfs_open_context(data->ctx);
7192 kfree(data);
7193}
7194
7195static const struct rpc_call_ops nfs4_lock_ops = {
7196 .rpc_call_prepare = nfs4_lock_prepare,
7197 .rpc_call_done = nfs4_lock_done,
7198 .rpc_release = nfs4_lock_release,
7199};
7200
7201static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7202{
7203 switch (error) {
7204 case -NFS4ERR_ADMIN_REVOKED:
7205 case -NFS4ERR_EXPIRED:
7206 case -NFS4ERR_BAD_STATEID:
7207 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7208 if (new_lock_owner != 0 ||
7209 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7210 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7211 break;
7212 case -NFS4ERR_STALE_STATEID:
7213 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7214 nfs4_schedule_lease_recovery(server->nfs_client);
7215 }
7216}
7217
7218static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7219{
7220 struct nfs4_lockdata *data;
7221 struct rpc_task *task;
7222 struct rpc_message msg = {
7223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7224 .rpc_cred = state->owner->so_cred,
7225 };
7226 struct rpc_task_setup task_setup_data = {
7227 .rpc_client = NFS_CLIENT(state->inode),
7228 .rpc_message = &msg,
7229 .callback_ops = &nfs4_lock_ops,
7230 .workqueue = nfsiod_workqueue,
7231 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7232 };
7233 int ret;
7234 struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7235
7236 if (client->cl_minorversion)
7237 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7238
7239 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
7240 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7241 if (data == NULL)
7242 return -ENOMEM;
7243 if (IS_SETLKW(cmd))
7244 data->arg.block = 1;
7245 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7246 recovery_type > NFS_LOCK_NEW);
7247 msg.rpc_argp = &data->arg;
7248 msg.rpc_resp = &data->res;
7249 task_setup_data.callback_data = data;
7250 if (recovery_type > NFS_LOCK_NEW) {
7251 if (recovery_type == NFS_LOCK_RECLAIM)
7252 data->arg.reclaim = NFS_LOCK_RECLAIM;
7253 } else
7254 data->arg.new_lock = 1;
7255 task = rpc_run_task(&task_setup_data);
7256 if (IS_ERR(task))
7257 return PTR_ERR(task);
7258 ret = rpc_wait_for_completion_task(task);
7259 if (ret == 0) {
7260 ret = data->rpc_status;
7261 if (ret)
7262 nfs4_handle_setlk_error(data->server, data->lsp,
7263 data->arg.new_lock_owner, ret);
7264 } else
7265 data->cancelled = true;
7266 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7267 rpc_put_task(task);
7268 dprintk("%s: ret = %d\n", __func__, ret);
7269 return ret;
7270}
7271
7272static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7273{
7274 struct nfs_server *server = NFS_SERVER(state->inode);
7275 struct nfs4_exception exception = {
7276 .inode = state->inode,
7277 };
7278 int err;
7279
7280 do {
7281 /* Cache the lock if possible... */
7282 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7283 return 0;
7284 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7285 if (err != -NFS4ERR_DELAY)
7286 break;
7287 nfs4_handle_exception(server, err, &exception);
7288 } while (exception.retry);
7289 return err;
7290}
7291
7292static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7293{
7294 struct nfs_server *server = NFS_SERVER(state->inode);
7295 struct nfs4_exception exception = {
7296 .inode = state->inode,
7297 };
7298 int err;
7299
7300 err = nfs4_set_lock_state(state, request);
7301 if (err != 0)
7302 return err;
7303 if (!recover_lost_locks) {
7304 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7305 return 0;
7306 }
7307 do {
7308 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7309 return 0;
7310 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7311 switch (err) {
7312 default:
7313 goto out;
7314 case -NFS4ERR_GRACE:
7315 case -NFS4ERR_DELAY:
7316 nfs4_handle_exception(server, err, &exception);
7317 err = 0;
7318 }
7319 } while (exception.retry);
7320out:
7321 return err;
7322}
7323
7324#if defined(CONFIG_NFS_V4_1)
7325static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7326{
7327 struct nfs4_lock_state *lsp;
7328 int status;
7329
7330 status = nfs4_set_lock_state(state, request);
7331 if (status != 0)
7332 return status;
7333 lsp = request->fl_u.nfs4_fl.owner;
7334 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7335 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7336 return 0;
7337 return nfs4_lock_expired(state, request);
7338}
7339#endif
7340
7341static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7342{
7343 struct nfs_inode *nfsi = NFS_I(state->inode);
7344 struct nfs4_state_owner *sp = state->owner;
7345 unsigned char fl_flags = request->fl_flags;
7346 int status;
7347
7348 request->fl_flags |= FL_ACCESS;
7349 status = locks_lock_inode_wait(state->inode, request);
7350 if (status < 0)
7351 goto out;
7352 mutex_lock(&sp->so_delegreturn_mutex);
7353 down_read(&nfsi->rwsem);
7354 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7355 /* Yes: cache locks! */
7356 /* ...but avoid races with delegation recall... */
7357 request->fl_flags = fl_flags & ~FL_SLEEP;
7358 status = locks_lock_inode_wait(state->inode, request);
7359 up_read(&nfsi->rwsem);
7360 mutex_unlock(&sp->so_delegreturn_mutex);
7361 goto out;
7362 }
7363 up_read(&nfsi->rwsem);
7364 mutex_unlock(&sp->so_delegreturn_mutex);
7365 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7366out:
7367 request->fl_flags = fl_flags;
7368 return status;
7369}
7370
7371static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7372{
7373 struct nfs4_exception exception = {
7374 .state = state,
7375 .inode = state->inode,
7376 .interruptible = true,
7377 };
7378 int err;
7379
7380 do {
7381 err = _nfs4_proc_setlk(state, cmd, request);
7382 if (err == -NFS4ERR_DENIED)
7383 err = -EAGAIN;
7384 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7385 err, &exception);
7386 } while (exception.retry);
7387 return err;
7388}
7389
7390#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7391#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7392
7393static int
7394nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7395 struct file_lock *request)
7396{
7397 int status = -ERESTARTSYS;
7398 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7399
7400 while(!signalled()) {
7401 status = nfs4_proc_setlk(state, cmd, request);
7402 if ((status != -EAGAIN) || IS_SETLK(cmd))
7403 break;
7404 freezable_schedule_timeout_interruptible(timeout);
7405 timeout *= 2;
7406 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7407 status = -ERESTARTSYS;
7408 }
7409 return status;
7410}
7411
7412#ifdef CONFIG_NFS_V4_1
7413struct nfs4_lock_waiter {
7414 struct inode *inode;
7415 struct nfs_lowner owner;
7416 wait_queue_entry_t wait;
7417};
7418
7419static int
7420nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7421{
7422 struct nfs4_lock_waiter *waiter =
7423 container_of(wait, struct nfs4_lock_waiter, wait);
7424
7425 /* NULL key means to wake up everyone */
7426 if (key) {
7427 struct cb_notify_lock_args *cbnl = key;
7428 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7429 *wowner = &waiter->owner;
7430
7431 /* Only wake if the callback was for the same owner. */
7432 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7433 return 0;
7434
7435 /* Make sure it's for the right inode */
7436 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7437 return 0;
7438 }
7439
7440 return woken_wake_function(wait, mode, flags, key);
7441}
7442
7443static int
7444nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7445{
7446 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7447 struct nfs_server *server = NFS_SERVER(state->inode);
7448 struct nfs_client *clp = server->nfs_client;
7449 wait_queue_head_t *q = &clp->cl_lock_waitq;
7450 struct nfs4_lock_waiter waiter = {
7451 .inode = state->inode,
7452 .owner = { .clientid = clp->cl_clientid,
7453 .id = lsp->ls_seqid.owner_id,
7454 .s_dev = server->s_dev },
7455 };
7456 int status;
7457
7458 /* Don't bother with waitqueue if we don't expect a callback */
7459 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7460 return nfs4_retry_setlk_simple(state, cmd, request);
7461
7462 init_wait(&waiter.wait);
7463 waiter.wait.func = nfs4_wake_lock_waiter;
7464 add_wait_queue(q, &waiter.wait);
7465
7466 do {
7467 status = nfs4_proc_setlk(state, cmd, request);
7468 if (status != -EAGAIN || IS_SETLK(cmd))
7469 break;
7470
7471 status = -ERESTARTSYS;
7472 freezer_do_not_count();
7473 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7474 NFS4_LOCK_MAXTIMEOUT);
7475 freezer_count();
7476 } while (!signalled());
7477
7478 remove_wait_queue(q, &waiter.wait);
7479
7480 return status;
7481}
7482#else /* !CONFIG_NFS_V4_1 */
7483static inline int
7484nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7485{
7486 return nfs4_retry_setlk_simple(state, cmd, request);
7487}
7488#endif
7489
7490static int
7491nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7492{
7493 struct nfs_open_context *ctx;
7494 struct nfs4_state *state;
7495 int status;
7496
7497 /* verify open state */
7498 ctx = nfs_file_open_context(filp);
7499 state = ctx->state;
7500
7501 if (IS_GETLK(cmd)) {
7502 if (state != NULL)
7503 return nfs4_proc_getlk(state, F_GETLK, request);
7504 return 0;
7505 }
7506
7507 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7508 return -EINVAL;
7509
7510 if (request->fl_type == F_UNLCK) {
7511 if (state != NULL)
7512 return nfs4_proc_unlck(state, cmd, request);
7513 return 0;
7514 }
7515
7516 if (state == NULL)
7517 return -ENOLCK;
7518
7519 if ((request->fl_flags & FL_POSIX) &&
7520 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7521 return -ENOLCK;
7522
7523 /*
7524 * Don't rely on the VFS having checked the file open mode,
7525 * since it won't do this for flock() locks.
7526 */
7527 switch (request->fl_type) {
7528 case F_RDLCK:
7529 if (!(filp->f_mode & FMODE_READ))
7530 return -EBADF;
7531 break;
7532 case F_WRLCK:
7533 if (!(filp->f_mode & FMODE_WRITE))
7534 return -EBADF;
7535 }
7536
7537 status = nfs4_set_lock_state(state, request);
7538 if (status != 0)
7539 return status;
7540
7541 return nfs4_retry_setlk(state, cmd, request);
7542}
7543
7544static int nfs4_delete_lease(struct file *file, void **priv)
7545{
7546 return generic_setlease(file, F_UNLCK, NULL, priv);
7547}
7548
7549static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7550 void **priv)
7551{
7552 struct inode *inode = file_inode(file);
7553 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7554 int ret;
7555
7556 /* No delegation, no lease */
7557 if (!nfs4_have_delegation(inode, type))
7558 return -EAGAIN;
7559 ret = generic_setlease(file, arg, lease, priv);
7560 if (ret || nfs4_have_delegation(inode, type))
7561 return ret;
7562 /* We raced with a delegation return */
7563 nfs4_delete_lease(file, priv);
7564 return -EAGAIN;
7565}
7566
7567int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7568 void **priv)
7569{
7570 switch (arg) {
7571 case F_RDLCK:
7572 case F_WRLCK:
7573 return nfs4_add_lease(file, arg, lease, priv);
7574 case F_UNLCK:
7575 return nfs4_delete_lease(file, priv);
7576 default:
7577 return -EINVAL;
7578 }
7579}
7580
7581int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7582{
7583 struct nfs_server *server = NFS_SERVER(state->inode);
7584 int err;
7585
7586 err = nfs4_set_lock_state(state, fl);
7587 if (err != 0)
7588 return err;
7589 do {
7590 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7591 if (err != -NFS4ERR_DELAY)
7592 break;
7593 ssleep(1);
7594 } while (err == -NFS4ERR_DELAY);
7595 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7596}
7597
7598struct nfs_release_lockowner_data {
7599 struct nfs4_lock_state *lsp;
7600 struct nfs_server *server;
7601 struct nfs_release_lockowner_args args;
7602 struct nfs_release_lockowner_res res;
7603 unsigned long timestamp;
7604};
7605
7606static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7607{
7608 struct nfs_release_lockowner_data *data = calldata;
7609 struct nfs_server *server = data->server;
7610 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7611 &data->res.seq_res, task);
7612 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7613 data->timestamp = jiffies;
7614}
7615
7616static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7617{
7618 struct nfs_release_lockowner_data *data = calldata;
7619 struct nfs_server *server = data->server;
7620
7621 nfs40_sequence_done(task, &data->res.seq_res);
7622
7623 switch (task->tk_status) {
7624 case 0:
7625 renew_lease(server, data->timestamp);
7626 break;
7627 case -NFS4ERR_STALE_CLIENTID:
7628 case -NFS4ERR_EXPIRED:
7629 nfs4_schedule_lease_recovery(server->nfs_client);
7630 break;
7631 case -NFS4ERR_LEASE_MOVED:
7632 case -NFS4ERR_DELAY:
7633 if (nfs4_async_handle_error(task, server,
7634 NULL, NULL) == -EAGAIN)
7635 rpc_restart_call_prepare(task);
7636 }
7637}
7638
7639static void nfs4_release_lockowner_release(void *calldata)
7640{
7641 struct nfs_release_lockowner_data *data = calldata;
7642 nfs4_free_lock_state(data->server, data->lsp);
7643 kfree(calldata);
7644}
7645
7646static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7647 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7648 .rpc_call_done = nfs4_release_lockowner_done,
7649 .rpc_release = nfs4_release_lockowner_release,
7650};
7651
7652static void
7653nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7654{
7655 struct nfs_release_lockowner_data *data;
7656 struct rpc_message msg = {
7657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7658 };
7659
7660 if (server->nfs_client->cl_mvops->minor_version != 0)
7661 return;
7662
7663 data = kmalloc(sizeof(*data), GFP_KERNEL);
7664 if (!data)
7665 return;
7666 data->lsp = lsp;
7667 data->server = server;
7668 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7669 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7670 data->args.lock_owner.s_dev = server->s_dev;
7671
7672 msg.rpc_argp = &data->args;
7673 msg.rpc_resp = &data->res;
7674 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7675 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7676}
7677
7678#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7679
7680static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7681 struct user_namespace *mnt_userns,
7682 struct dentry *unused, struct inode *inode,
7683 const char *key, const void *buf,
7684 size_t buflen, int flags)
7685{
7686 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7687}
7688
7689static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7690 struct dentry *unused, struct inode *inode,
7691 const char *key, void *buf, size_t buflen)
7692{
7693 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7694}
7695
7696static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7697{
7698 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7699}
7700
7701#if defined(CONFIG_NFS_V4_1)
7702#define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7703
7704static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7705 struct user_namespace *mnt_userns,
7706 struct dentry *unused, struct inode *inode,
7707 const char *key, const void *buf,
7708 size_t buflen, int flags)
7709{
7710 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7711}
7712
7713static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7714 struct dentry *unused, struct inode *inode,
7715 const char *key, void *buf, size_t buflen)
7716{
7717 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7718}
7719
7720static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7721{
7722 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7723}
7724
7725#define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7726
7727static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7728 struct user_namespace *mnt_userns,
7729 struct dentry *unused, struct inode *inode,
7730 const char *key, const void *buf,
7731 size_t buflen, int flags)
7732{
7733 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7734}
7735
7736static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7737 struct dentry *unused, struct inode *inode,
7738 const char *key, void *buf, size_t buflen)
7739{
7740 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7741}
7742
7743static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7744{
7745 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7746}
7747
7748#endif
7749
7750#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7751
7752static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7753 struct user_namespace *mnt_userns,
7754 struct dentry *unused, struct inode *inode,
7755 const char *key, const void *buf,
7756 size_t buflen, int flags)
7757{
7758 if (security_ismaclabel(key))
7759 return nfs4_set_security_label(inode, buf, buflen);
7760
7761 return -EOPNOTSUPP;
7762}
7763
7764static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7765 struct dentry *unused, struct inode *inode,
7766 const char *key, void *buf, size_t buflen)
7767{
7768 if (security_ismaclabel(key))
7769 return nfs4_get_security_label(inode, buf, buflen);
7770 return -EOPNOTSUPP;
7771}
7772
7773static ssize_t
7774nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7775{
7776 int len = 0;
7777
7778 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7779 len = security_inode_listsecurity(inode, list, list_len);
7780 if (len >= 0 && list_len && len > list_len)
7781 return -ERANGE;
7782 }
7783 return len;
7784}
7785
7786static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7787 .prefix = XATTR_SECURITY_PREFIX,
7788 .get = nfs4_xattr_get_nfs4_label,
7789 .set = nfs4_xattr_set_nfs4_label,
7790};
7791
7792#else
7793
7794static ssize_t
7795nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7796{
7797 return 0;
7798}
7799
7800#endif
7801
7802#ifdef CONFIG_NFS_V4_2
7803static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7804 struct user_namespace *mnt_userns,
7805 struct dentry *unused, struct inode *inode,
7806 const char *key, const void *buf,
7807 size_t buflen, int flags)
7808{
7809 u32 mask;
7810 int ret;
7811
7812 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7813 return -EOPNOTSUPP;
7814
7815 /*
7816 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7817 * flags right now. Handling of xattr operations use the normal
7818 * file read/write permissions.
7819 *
7820 * Just in case the server has other ideas (which RFC 8276 allows),
7821 * do a cached access check for the XA* flags to possibly avoid
7822 * doing an RPC and getting EACCES back.
7823 */
7824 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7825 if (!(mask & NFS_ACCESS_XAWRITE))
7826 return -EACCES;
7827 }
7828
7829 if (buf == NULL) {
7830 ret = nfs42_proc_removexattr(inode, key);
7831 if (!ret)
7832 nfs4_xattr_cache_remove(inode, key);
7833 } else {
7834 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7835 if (!ret)
7836 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7837 }
7838
7839 return ret;
7840}
7841
7842static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7843 struct dentry *unused, struct inode *inode,
7844 const char *key, void *buf, size_t buflen)
7845{
7846 u32 mask;
7847 ssize_t ret;
7848
7849 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7850 return -EOPNOTSUPP;
7851
7852 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7853 if (!(mask & NFS_ACCESS_XAREAD))
7854 return -EACCES;
7855 }
7856
7857 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7858 if (ret)
7859 return ret;
7860
7861 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7862 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7863 return ret;
7864
7865 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7866
7867 return ret;
7868}
7869
7870static ssize_t
7871nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7872{
7873 u64 cookie;
7874 bool eof;
7875 ssize_t ret, size;
7876 char *buf;
7877 size_t buflen;
7878 u32 mask;
7879
7880 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7881 return 0;
7882
7883 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7884 if (!(mask & NFS_ACCESS_XALIST))
7885 return 0;
7886 }
7887
7888 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
7889 if (ret)
7890 return ret;
7891
7892 ret = nfs4_xattr_cache_list(inode, list, list_len);
7893 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7894 return ret;
7895
7896 cookie = 0;
7897 eof = false;
7898 buflen = list_len ? list_len : XATTR_LIST_MAX;
7899 buf = list_len ? list : NULL;
7900 size = 0;
7901
7902 while (!eof) {
7903 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7904 &cookie, &eof);
7905 if (ret < 0)
7906 return ret;
7907
7908 if (list_len) {
7909 buf += ret;
7910 buflen -= ret;
7911 }
7912 size += ret;
7913 }
7914
7915 if (list_len)
7916 nfs4_xattr_cache_set_list(inode, list, size);
7917
7918 return size;
7919}
7920
7921#else
7922
7923static ssize_t
7924nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7925{
7926 return 0;
7927}
7928#endif /* CONFIG_NFS_V4_2 */
7929
7930/*
7931 * nfs_fhget will use either the mounted_on_fileid or the fileid
7932 */
7933static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7934{
7935 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7936 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7937 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7938 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7939 return;
7940
7941 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7942 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7943 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7944 fattr->nlink = 2;
7945}
7946
7947static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7948 const struct qstr *name,
7949 struct nfs4_fs_locations *fs_locations,
7950 struct page *page)
7951{
7952 struct nfs_server *server = NFS_SERVER(dir);
7953 u32 bitmask[3];
7954 struct nfs4_fs_locations_arg args = {
7955 .dir_fh = NFS_FH(dir),
7956 .name = name,
7957 .page = page,
7958 .bitmask = bitmask,
7959 };
7960 struct nfs4_fs_locations_res res = {
7961 .fs_locations = fs_locations,
7962 };
7963 struct rpc_message msg = {
7964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7965 .rpc_argp = &args,
7966 .rpc_resp = &res,
7967 };
7968 int status;
7969
7970 dprintk("%s: start\n", __func__);
7971
7972 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7973 bitmask[1] = nfs4_fattr_bitmap[1];
7974
7975 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7976 * is not supported */
7977 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7978 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7979 else
7980 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7981
7982 nfs_fattr_init(&fs_locations->fattr);
7983 fs_locations->server = server;
7984 fs_locations->nlocations = 0;
7985 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7986 dprintk("%s: returned status = %d\n", __func__, status);
7987 return status;
7988}
7989
7990int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7991 const struct qstr *name,
7992 struct nfs4_fs_locations *fs_locations,
7993 struct page *page)
7994{
7995 struct nfs4_exception exception = {
7996 .interruptible = true,
7997 };
7998 int err;
7999 do {
8000 err = _nfs4_proc_fs_locations(client, dir, name,
8001 fs_locations, page);
8002 trace_nfs4_get_fs_locations(dir, name, err);
8003 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8004 &exception);
8005 } while (exception.retry);
8006 return err;
8007}
8008
8009/*
8010 * This operation also signals the server that this client is
8011 * performing migration recovery. The server can stop returning
8012 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8013 * appended to this compound to identify the client ID which is
8014 * performing recovery.
8015 */
8016static int _nfs40_proc_get_locations(struct nfs_server *server,
8017 struct nfs_fh *fhandle,
8018 struct nfs4_fs_locations *locations,
8019 struct page *page, const struct cred *cred)
8020{
8021 struct rpc_clnt *clnt = server->client;
8022 u32 bitmask[2] = {
8023 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8024 };
8025 struct nfs4_fs_locations_arg args = {
8026 .clientid = server->nfs_client->cl_clientid,
8027 .fh = fhandle,
8028 .page = page,
8029 .bitmask = bitmask,
8030 .migration = 1, /* skip LOOKUP */
8031 .renew = 1, /* append RENEW */
8032 };
8033 struct nfs4_fs_locations_res res = {
8034 .fs_locations = locations,
8035 .migration = 1,
8036 .renew = 1,
8037 };
8038 struct rpc_message msg = {
8039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8040 .rpc_argp = &args,
8041 .rpc_resp = &res,
8042 .rpc_cred = cred,
8043 };
8044 unsigned long now = jiffies;
8045 int status;
8046
8047 nfs_fattr_init(&locations->fattr);
8048 locations->server = server;
8049 locations->nlocations = 0;
8050
8051 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8052 status = nfs4_call_sync_sequence(clnt, server, &msg,
8053 &args.seq_args, &res.seq_res);
8054 if (status)
8055 return status;
8056
8057 renew_lease(server, now);
8058 return 0;
8059}
8060
8061#ifdef CONFIG_NFS_V4_1
8062
8063/*
8064 * This operation also signals the server that this client is
8065 * performing migration recovery. The server can stop asserting
8066 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8067 * performing this operation is identified in the SEQUENCE
8068 * operation in this compound.
8069 *
8070 * When the client supports GETATTR(fs_locations_info), it can
8071 * be plumbed in here.
8072 */
8073static int _nfs41_proc_get_locations(struct nfs_server *server,
8074 struct nfs_fh *fhandle,
8075 struct nfs4_fs_locations *locations,
8076 struct page *page, const struct cred *cred)
8077{
8078 struct rpc_clnt *clnt = server->client;
8079 u32 bitmask[2] = {
8080 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8081 };
8082 struct nfs4_fs_locations_arg args = {
8083 .fh = fhandle,
8084 .page = page,
8085 .bitmask = bitmask,
8086 .migration = 1, /* skip LOOKUP */
8087 };
8088 struct nfs4_fs_locations_res res = {
8089 .fs_locations = locations,
8090 .migration = 1,
8091 };
8092 struct rpc_message msg = {
8093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8094 .rpc_argp = &args,
8095 .rpc_resp = &res,
8096 .rpc_cred = cred,
8097 };
8098 struct nfs4_call_sync_data data = {
8099 .seq_server = server,
8100 .seq_args = &args.seq_args,
8101 .seq_res = &res.seq_res,
8102 };
8103 struct rpc_task_setup task_setup_data = {
8104 .rpc_client = clnt,
8105 .rpc_message = &msg,
8106 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8107 .callback_data = &data,
8108 .flags = RPC_TASK_NO_ROUND_ROBIN,
8109 };
8110 int status;
8111
8112 nfs_fattr_init(&locations->fattr);
8113 locations->server = server;
8114 locations->nlocations = 0;
8115
8116 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8117 status = nfs4_call_sync_custom(&task_setup_data);
8118 if (status == NFS4_OK &&
8119 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8120 status = -NFS4ERR_LEASE_MOVED;
8121 return status;
8122}
8123
8124#endif /* CONFIG_NFS_V4_1 */
8125
8126/**
8127 * nfs4_proc_get_locations - discover locations for a migrated FSID
8128 * @server: pointer to nfs_server to process
8129 * @fhandle: pointer to the kernel NFS client file handle
8130 * @locations: result of query
8131 * @page: buffer
8132 * @cred: credential to use for this operation
8133 *
8134 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8135 * operation failed, or a negative errno if a local error occurred.
8136 *
8137 * On success, "locations" is filled in, but if the server has
8138 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8139 * asserted.
8140 *
8141 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8142 * from this client that require migration recovery.
8143 */
8144int nfs4_proc_get_locations(struct nfs_server *server,
8145 struct nfs_fh *fhandle,
8146 struct nfs4_fs_locations *locations,
8147 struct page *page, const struct cred *cred)
8148{
8149 struct nfs_client *clp = server->nfs_client;
8150 const struct nfs4_mig_recovery_ops *ops =
8151 clp->cl_mvops->mig_recovery_ops;
8152 struct nfs4_exception exception = {
8153 .interruptible = true,
8154 };
8155 int status;
8156
8157 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8158 (unsigned long long)server->fsid.major,
8159 (unsigned long long)server->fsid.minor,
8160 clp->cl_hostname);
8161 nfs_display_fhandle(fhandle, __func__);
8162
8163 do {
8164 status = ops->get_locations(server, fhandle, locations, page,
8165 cred);
8166 if (status != -NFS4ERR_DELAY)
8167 break;
8168 nfs4_handle_exception(server, status, &exception);
8169 } while (exception.retry);
8170 return status;
8171}
8172
8173/*
8174 * This operation also signals the server that this client is
8175 * performing "lease moved" recovery. The server can stop
8176 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8177 * is appended to this compound to identify the client ID which is
8178 * performing recovery.
8179 */
8180static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8181{
8182 struct nfs_server *server = NFS_SERVER(inode);
8183 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8184 struct rpc_clnt *clnt = server->client;
8185 struct nfs4_fsid_present_arg args = {
8186 .fh = NFS_FH(inode),
8187 .clientid = clp->cl_clientid,
8188 .renew = 1, /* append RENEW */
8189 };
8190 struct nfs4_fsid_present_res res = {
8191 .renew = 1,
8192 };
8193 struct rpc_message msg = {
8194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8195 .rpc_argp = &args,
8196 .rpc_resp = &res,
8197 .rpc_cred = cred,
8198 };
8199 unsigned long now = jiffies;
8200 int status;
8201
8202 res.fh = nfs_alloc_fhandle();
8203 if (res.fh == NULL)
8204 return -ENOMEM;
8205
8206 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8207 status = nfs4_call_sync_sequence(clnt, server, &msg,
8208 &args.seq_args, &res.seq_res);
8209 nfs_free_fhandle(res.fh);
8210 if (status)
8211 return status;
8212
8213 do_renew_lease(clp, now);
8214 return 0;
8215}
8216
8217#ifdef CONFIG_NFS_V4_1
8218
8219/*
8220 * This operation also signals the server that this client is
8221 * performing "lease moved" recovery. The server can stop asserting
8222 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8223 * this operation is identified in the SEQUENCE operation in this
8224 * compound.
8225 */
8226static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8227{
8228 struct nfs_server *server = NFS_SERVER(inode);
8229 struct rpc_clnt *clnt = server->client;
8230 struct nfs4_fsid_present_arg args = {
8231 .fh = NFS_FH(inode),
8232 };
8233 struct nfs4_fsid_present_res res = {
8234 };
8235 struct rpc_message msg = {
8236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8237 .rpc_argp = &args,
8238 .rpc_resp = &res,
8239 .rpc_cred = cred,
8240 };
8241 int status;
8242
8243 res.fh = nfs_alloc_fhandle();
8244 if (res.fh == NULL)
8245 return -ENOMEM;
8246
8247 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8248 status = nfs4_call_sync_sequence(clnt, server, &msg,
8249 &args.seq_args, &res.seq_res);
8250 nfs_free_fhandle(res.fh);
8251 if (status == NFS4_OK &&
8252 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8253 status = -NFS4ERR_LEASE_MOVED;
8254 return status;
8255}
8256
8257#endif /* CONFIG_NFS_V4_1 */
8258
8259/**
8260 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8261 * @inode: inode on FSID to check
8262 * @cred: credential to use for this operation
8263 *
8264 * Server indicates whether the FSID is present, moved, or not
8265 * recognized. This operation is necessary to clear a LEASE_MOVED
8266 * condition for this client ID.
8267 *
8268 * Returns NFS4_OK if the FSID is present on this server,
8269 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8270 * NFS4ERR code if some error occurred on the server, or a
8271 * negative errno if a local failure occurred.
8272 */
8273int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8274{
8275 struct nfs_server *server = NFS_SERVER(inode);
8276 struct nfs_client *clp = server->nfs_client;
8277 const struct nfs4_mig_recovery_ops *ops =
8278 clp->cl_mvops->mig_recovery_ops;
8279 struct nfs4_exception exception = {
8280 .interruptible = true,
8281 };
8282 int status;
8283
8284 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8285 (unsigned long long)server->fsid.major,
8286 (unsigned long long)server->fsid.minor,
8287 clp->cl_hostname);
8288 nfs_display_fhandle(NFS_FH(inode), __func__);
8289
8290 do {
8291 status = ops->fsid_present(inode, cred);
8292 if (status != -NFS4ERR_DELAY)
8293 break;
8294 nfs4_handle_exception(server, status, &exception);
8295 } while (exception.retry);
8296 return status;
8297}
8298
8299/*
8300 * If 'use_integrity' is true and the state managment nfs_client
8301 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8302 * and the machine credential as per RFC3530bis and RFC5661 Security
8303 * Considerations sections. Otherwise, just use the user cred with the
8304 * filesystem's rpc_client.
8305 */
8306static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8307{
8308 int status;
8309 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8310 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8311 struct nfs4_secinfo_arg args = {
8312 .dir_fh = NFS_FH(dir),
8313 .name = name,
8314 };
8315 struct nfs4_secinfo_res res = {
8316 .flavors = flavors,
8317 };
8318 struct rpc_message msg = {
8319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8320 .rpc_argp = &args,
8321 .rpc_resp = &res,
8322 };
8323 struct nfs4_call_sync_data data = {
8324 .seq_server = NFS_SERVER(dir),
8325 .seq_args = &args.seq_args,
8326 .seq_res = &res.seq_res,
8327 };
8328 struct rpc_task_setup task_setup = {
8329 .rpc_client = clnt,
8330 .rpc_message = &msg,
8331 .callback_ops = clp->cl_mvops->call_sync_ops,
8332 .callback_data = &data,
8333 .flags = RPC_TASK_NO_ROUND_ROBIN,
8334 };
8335 const struct cred *cred = NULL;
8336
8337 if (use_integrity) {
8338 clnt = clp->cl_rpcclient;
8339 task_setup.rpc_client = clnt;
8340
8341 cred = nfs4_get_clid_cred(clp);
8342 msg.rpc_cred = cred;
8343 }
8344
8345 dprintk("NFS call secinfo %s\n", name->name);
8346
8347 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8348 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8349 status = nfs4_call_sync_custom(&task_setup);
8350
8351 dprintk("NFS reply secinfo: %d\n", status);
8352
8353 put_cred(cred);
8354 return status;
8355}
8356
8357int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8358 struct nfs4_secinfo_flavors *flavors)
8359{
8360 struct nfs4_exception exception = {
8361 .interruptible = true,
8362 };
8363 int err;
8364 do {
8365 err = -NFS4ERR_WRONGSEC;
8366
8367 /* try to use integrity protection with machine cred */
8368 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8369 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8370
8371 /*
8372 * if unable to use integrity protection, or SECINFO with
8373 * integrity protection returns NFS4ERR_WRONGSEC (which is
8374 * disallowed by spec, but exists in deployed servers) use
8375 * the current filesystem's rpc_client and the user cred.
8376 */
8377 if (err == -NFS4ERR_WRONGSEC)
8378 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8379
8380 trace_nfs4_secinfo(dir, name, err);
8381 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8382 &exception);
8383 } while (exception.retry);
8384 return err;
8385}
8386
8387#ifdef CONFIG_NFS_V4_1
8388/*
8389 * Check the exchange flags returned by the server for invalid flags, having
8390 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8391 * DS flags set.
8392 */
8393static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8394{
8395 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8396 goto out_inval;
8397 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8398 goto out_inval;
8399 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8400 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8401 goto out_inval;
8402 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8403 goto out_inval;
8404 return NFS_OK;
8405out_inval:
8406 return -NFS4ERR_INVAL;
8407}
8408
8409static bool
8410nfs41_same_server_scope(struct nfs41_server_scope *a,
8411 struct nfs41_server_scope *b)
8412{
8413 if (a->server_scope_sz != b->server_scope_sz)
8414 return false;
8415 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8416}
8417
8418static void
8419nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8420{
8421 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8422 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8423 struct nfs_client *clp = args->client;
8424
8425 switch (task->tk_status) {
8426 case -NFS4ERR_BADSESSION:
8427 case -NFS4ERR_DEADSESSION:
8428 nfs4_schedule_session_recovery(clp->cl_session,
8429 task->tk_status);
8430 return;
8431 }
8432 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8433 res->dir != NFS4_CDFS4_BOTH) {
8434 rpc_task_close_connection(task);
8435 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8436 rpc_restart_call(task);
8437 }
8438}
8439
8440static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8441 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8442};
8443
8444/*
8445 * nfs4_proc_bind_one_conn_to_session()
8446 *
8447 * The 4.1 client currently uses the same TCP connection for the
8448 * fore and backchannel.
8449 */
8450static
8451int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8452 struct rpc_xprt *xprt,
8453 struct nfs_client *clp,
8454 const struct cred *cred)
8455{
8456 int status;
8457 struct nfs41_bind_conn_to_session_args args = {
8458 .client = clp,
8459 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8460 .retries = 0,
8461 };
8462 struct nfs41_bind_conn_to_session_res res;
8463 struct rpc_message msg = {
8464 .rpc_proc =
8465 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8466 .rpc_argp = &args,
8467 .rpc_resp = &res,
8468 .rpc_cred = cred,
8469 };
8470 struct rpc_task_setup task_setup_data = {
8471 .rpc_client = clnt,
8472 .rpc_xprt = xprt,
8473 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8474 .rpc_message = &msg,
8475 .flags = RPC_TASK_TIMEOUT,
8476 };
8477 struct rpc_task *task;
8478
8479 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8480 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8481 args.dir = NFS4_CDFC4_FORE;
8482
8483 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8484 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8485 args.dir = NFS4_CDFC4_FORE;
8486
8487 task = rpc_run_task(&task_setup_data);
8488 if (!IS_ERR(task)) {
8489 status = task->tk_status;
8490 rpc_put_task(task);
8491 } else
8492 status = PTR_ERR(task);
8493 trace_nfs4_bind_conn_to_session(clp, status);
8494 if (status == 0) {
8495 if (memcmp(res.sessionid.data,
8496 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8497 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8498 return -EIO;
8499 }
8500 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8501 dprintk("NFS: %s: Unexpected direction from server\n",
8502 __func__);
8503 return -EIO;
8504 }
8505 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8506 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8507 __func__);
8508 return -EIO;
8509 }
8510 }
8511
8512 return status;
8513}
8514
8515struct rpc_bind_conn_calldata {
8516 struct nfs_client *clp;
8517 const struct cred *cred;
8518};
8519
8520static int
8521nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8522 struct rpc_xprt *xprt,
8523 void *calldata)
8524{
8525 struct rpc_bind_conn_calldata *p = calldata;
8526
8527 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8528}
8529
8530int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8531{
8532 struct rpc_bind_conn_calldata data = {
8533 .clp = clp,
8534 .cred = cred,
8535 };
8536 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8537 nfs4_proc_bind_conn_to_session_callback, &data);
8538}
8539
8540/*
8541 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8542 * and operations we'd like to see to enable certain features in the allow map
8543 */
8544static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8545 .how = SP4_MACH_CRED,
8546 .enforce.u.words = {
8547 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8548 1 << (OP_EXCHANGE_ID - 32) |
8549 1 << (OP_CREATE_SESSION - 32) |
8550 1 << (OP_DESTROY_SESSION - 32) |
8551 1 << (OP_DESTROY_CLIENTID - 32)
8552 },
8553 .allow.u.words = {
8554 [0] = 1 << (OP_CLOSE) |
8555 1 << (OP_OPEN_DOWNGRADE) |
8556 1 << (OP_LOCKU) |
8557 1 << (OP_DELEGRETURN) |
8558 1 << (OP_COMMIT),
8559 [1] = 1 << (OP_SECINFO - 32) |
8560 1 << (OP_SECINFO_NO_NAME - 32) |
8561 1 << (OP_LAYOUTRETURN - 32) |
8562 1 << (OP_TEST_STATEID - 32) |
8563 1 << (OP_FREE_STATEID - 32) |
8564 1 << (OP_WRITE - 32)
8565 }
8566};
8567
8568/*
8569 * Select the state protection mode for client `clp' given the server results
8570 * from exchange_id in `sp'.
8571 *
8572 * Returns 0 on success, negative errno otherwise.
8573 */
8574static int nfs4_sp4_select_mode(struct nfs_client *clp,
8575 struct nfs41_state_protection *sp)
8576{
8577 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8578 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8579 1 << (OP_EXCHANGE_ID - 32) |
8580 1 << (OP_CREATE_SESSION - 32) |
8581 1 << (OP_DESTROY_SESSION - 32) |
8582 1 << (OP_DESTROY_CLIENTID - 32)
8583 };
8584 unsigned long flags = 0;
8585 unsigned int i;
8586 int ret = 0;
8587
8588 if (sp->how == SP4_MACH_CRED) {
8589 /* Print state protect result */
8590 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8591 for (i = 0; i <= LAST_NFS4_OP; i++) {
8592 if (test_bit(i, sp->enforce.u.longs))
8593 dfprintk(MOUNT, " enforce op %d\n", i);
8594 if (test_bit(i, sp->allow.u.longs))
8595 dfprintk(MOUNT, " allow op %d\n", i);
8596 }
8597
8598 /* make sure nothing is on enforce list that isn't supported */
8599 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8600 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8601 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8602 ret = -EINVAL;
8603 goto out;
8604 }
8605 }
8606
8607 /*
8608 * Minimal mode - state operations are allowed to use machine
8609 * credential. Note this already happens by default, so the
8610 * client doesn't have to do anything more than the negotiation.
8611 *
8612 * NOTE: we don't care if EXCHANGE_ID is in the list -
8613 * we're already using the machine cred for exchange_id
8614 * and will never use a different cred.
8615 */
8616 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8617 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8618 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8619 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8620 dfprintk(MOUNT, "sp4_mach_cred:\n");
8621 dfprintk(MOUNT, " minimal mode enabled\n");
8622 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8623 } else {
8624 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8625 ret = -EINVAL;
8626 goto out;
8627 }
8628
8629 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8630 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8631 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8632 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8633 dfprintk(MOUNT, " cleanup mode enabled\n");
8634 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8635 }
8636
8637 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8638 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8639 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8640 }
8641
8642 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8643 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8644 dfprintk(MOUNT, " secinfo mode enabled\n");
8645 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8646 }
8647
8648 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8649 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8650 dfprintk(MOUNT, " stateid mode enabled\n");
8651 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8652 }
8653
8654 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8655 dfprintk(MOUNT, " write mode enabled\n");
8656 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8657 }
8658
8659 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8660 dfprintk(MOUNT, " commit mode enabled\n");
8661 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8662 }
8663 }
8664out:
8665 clp->cl_sp4_flags = flags;
8666 return ret;
8667}
8668
8669struct nfs41_exchange_id_data {
8670 struct nfs41_exchange_id_res res;
8671 struct nfs41_exchange_id_args args;
8672};
8673
8674static void nfs4_exchange_id_release(void *data)
8675{
8676 struct nfs41_exchange_id_data *cdata =
8677 (struct nfs41_exchange_id_data *)data;
8678
8679 nfs_put_client(cdata->args.client);
8680 kfree(cdata->res.impl_id);
8681 kfree(cdata->res.server_scope);
8682 kfree(cdata->res.server_owner);
8683 kfree(cdata);
8684}
8685
8686static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8687 .rpc_release = nfs4_exchange_id_release,
8688};
8689
8690/*
8691 * _nfs4_proc_exchange_id()
8692 *
8693 * Wrapper for EXCHANGE_ID operation.
8694 */
8695static struct rpc_task *
8696nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8697 u32 sp4_how, struct rpc_xprt *xprt)
8698{
8699 struct rpc_message msg = {
8700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8701 .rpc_cred = cred,
8702 };
8703 struct rpc_task_setup task_setup_data = {
8704 .rpc_client = clp->cl_rpcclient,
8705 .callback_ops = &nfs4_exchange_id_call_ops,
8706 .rpc_message = &msg,
8707 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8708 };
8709 struct nfs41_exchange_id_data *calldata;
8710 int status;
8711
8712 if (!refcount_inc_not_zero(&clp->cl_count))
8713 return ERR_PTR(-EIO);
8714
8715 status = -ENOMEM;
8716 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8717 if (!calldata)
8718 goto out;
8719
8720 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8721
8722 status = nfs4_init_uniform_client_string(clp);
8723 if (status)
8724 goto out_calldata;
8725
8726 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8727 GFP_NOFS);
8728 status = -ENOMEM;
8729 if (unlikely(calldata->res.server_owner == NULL))
8730 goto out_calldata;
8731
8732 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8733 GFP_NOFS);
8734 if (unlikely(calldata->res.server_scope == NULL))
8735 goto out_server_owner;
8736
8737 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8738 if (unlikely(calldata->res.impl_id == NULL))
8739 goto out_server_scope;
8740
8741 switch (sp4_how) {
8742 case SP4_NONE:
8743 calldata->args.state_protect.how = SP4_NONE;
8744 break;
8745
8746 case SP4_MACH_CRED:
8747 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8748 break;
8749
8750 default:
8751 /* unsupported! */
8752 WARN_ON_ONCE(1);
8753 status = -EINVAL;
8754 goto out_impl_id;
8755 }
8756 if (xprt) {
8757 task_setup_data.rpc_xprt = xprt;
8758 task_setup_data.flags |= RPC_TASK_SOFTCONN;
8759 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8760 sizeof(calldata->args.verifier.data));
8761 }
8762 calldata->args.client = clp;
8763 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8764 EXCHGID4_FLAG_BIND_PRINC_STATEID;
8765#ifdef CONFIG_NFS_V4_1_MIGRATION
8766 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8767#endif
8768 msg.rpc_argp = &calldata->args;
8769 msg.rpc_resp = &calldata->res;
8770 task_setup_data.callback_data = calldata;
8771
8772 return rpc_run_task(&task_setup_data);
8773
8774out_impl_id:
8775 kfree(calldata->res.impl_id);
8776out_server_scope:
8777 kfree(calldata->res.server_scope);
8778out_server_owner:
8779 kfree(calldata->res.server_owner);
8780out_calldata:
8781 kfree(calldata);
8782out:
8783 nfs_put_client(clp);
8784 return ERR_PTR(status);
8785}
8786
8787/*
8788 * _nfs4_proc_exchange_id()
8789 *
8790 * Wrapper for EXCHANGE_ID operation.
8791 */
8792static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
8793 u32 sp4_how)
8794{
8795 struct rpc_task *task;
8796 struct nfs41_exchange_id_args *argp;
8797 struct nfs41_exchange_id_res *resp;
8798 unsigned long now = jiffies;
8799 int status;
8800
8801 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8802 if (IS_ERR(task))
8803 return PTR_ERR(task);
8804
8805 argp = task->tk_msg.rpc_argp;
8806 resp = task->tk_msg.rpc_resp;
8807 status = task->tk_status;
8808 if (status != 0)
8809 goto out;
8810
8811 status = nfs4_check_cl_exchange_flags(resp->flags,
8812 clp->cl_mvops->minor_version);
8813 if (status != 0)
8814 goto out;
8815
8816 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8817 if (status != 0)
8818 goto out;
8819
8820 do_renew_lease(clp, now);
8821
8822 clp->cl_clientid = resp->clientid;
8823 clp->cl_exchange_flags = resp->flags;
8824 clp->cl_seqid = resp->seqid;
8825 /* Client ID is not confirmed */
8826 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8827 clear_bit(NFS4_SESSION_ESTABLISHED,
8828 &clp->cl_session->session_state);
8829
8830 if (clp->cl_serverscope != NULL &&
8831 !nfs41_same_server_scope(clp->cl_serverscope,
8832 resp->server_scope)) {
8833 dprintk("%s: server_scope mismatch detected\n",
8834 __func__);
8835 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8836 }
8837
8838 swap(clp->cl_serverowner, resp->server_owner);
8839 swap(clp->cl_serverscope, resp->server_scope);
8840 swap(clp->cl_implid, resp->impl_id);
8841
8842 /* Save the EXCHANGE_ID verifier session trunk tests */
8843 memcpy(clp->cl_confirm.data, argp->verifier.data,
8844 sizeof(clp->cl_confirm.data));
8845out:
8846 trace_nfs4_exchange_id(clp, status);
8847 rpc_put_task(task);
8848 return status;
8849}
8850
8851/*
8852 * nfs4_proc_exchange_id()
8853 *
8854 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8855 *
8856 * Since the clientid has expired, all compounds using sessions
8857 * associated with the stale clientid will be returning
8858 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8859 * be in some phase of session reset.
8860 *
8861 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8862 */
8863int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
8864{
8865 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8866 int status;
8867
8868 /* try SP4_MACH_CRED if krb5i/p */
8869 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8870 authflavor == RPC_AUTH_GSS_KRB5P) {
8871 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8872 if (!status)
8873 return 0;
8874 }
8875
8876 /* try SP4_NONE */
8877 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8878}
8879
8880/**
8881 * nfs4_test_session_trunk
8882 *
8883 * This is an add_xprt_test() test function called from
8884 * rpc_clnt_setup_test_and_add_xprt.
8885 *
8886 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8887 * and is dereferrenced in nfs4_exchange_id_release
8888 *
8889 * Upon success, add the new transport to the rpc_clnt
8890 *
8891 * @clnt: struct rpc_clnt to get new transport
8892 * @xprt: the rpc_xprt to test
8893 * @data: call data for _nfs4_proc_exchange_id.
8894 */
8895void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8896 void *data)
8897{
8898 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8899 struct rpc_task *task;
8900 int status;
8901
8902 u32 sp4_how;
8903
8904 dprintk("--> %s try %s\n", __func__,
8905 xprt->address_strings[RPC_DISPLAY_ADDR]);
8906
8907 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8908
8909 /* Test connection for session trunking. Async exchange_id call */
8910 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8911 if (IS_ERR(task))
8912 return;
8913
8914 status = task->tk_status;
8915 if (status == 0)
8916 status = nfs4_detect_session_trunking(adata->clp,
8917 task->tk_msg.rpc_resp, xprt);
8918
8919 if (status == 0)
8920 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8921
8922 rpc_put_task(task);
8923}
8924EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8925
8926static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8927 const struct cred *cred)
8928{
8929 struct rpc_message msg = {
8930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8931 .rpc_argp = clp,
8932 .rpc_cred = cred,
8933 };
8934 int status;
8935
8936 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8937 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
8938 trace_nfs4_destroy_clientid(clp, status);
8939 if (status)
8940 dprintk("NFS: Got error %d from the server %s on "
8941 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8942 return status;
8943}
8944
8945static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8946 const struct cred *cred)
8947{
8948 unsigned int loop;
8949 int ret;
8950
8951 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8952 ret = _nfs4_proc_destroy_clientid(clp, cred);
8953 switch (ret) {
8954 case -NFS4ERR_DELAY:
8955 case -NFS4ERR_CLIENTID_BUSY:
8956 ssleep(1);
8957 break;
8958 default:
8959 return ret;
8960 }
8961 }
8962 return 0;
8963}
8964
8965int nfs4_destroy_clientid(struct nfs_client *clp)
8966{
8967 const struct cred *cred;
8968 int ret = 0;
8969
8970 if (clp->cl_mvops->minor_version < 1)
8971 goto out;
8972 if (clp->cl_exchange_flags == 0)
8973 goto out;
8974 if (clp->cl_preserve_clid)
8975 goto out;
8976 cred = nfs4_get_clid_cred(clp);
8977 ret = nfs4_proc_destroy_clientid(clp, cred);
8978 put_cred(cred);
8979 switch (ret) {
8980 case 0:
8981 case -NFS4ERR_STALE_CLIENTID:
8982 clp->cl_exchange_flags = 0;
8983 }
8984out:
8985 return ret;
8986}
8987
8988#endif /* CONFIG_NFS_V4_1 */
8989
8990struct nfs4_get_lease_time_data {
8991 struct nfs4_get_lease_time_args *args;
8992 struct nfs4_get_lease_time_res *res;
8993 struct nfs_client *clp;
8994};
8995
8996static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8997 void *calldata)
8998{
8999 struct nfs4_get_lease_time_data *data =
9000 (struct nfs4_get_lease_time_data *)calldata;
9001
9002 /* just setup sequence, do not trigger session recovery
9003 since we're invoked within one */
9004 nfs4_setup_sequence(data->clp,
9005 &data->args->la_seq_args,
9006 &data->res->lr_seq_res,
9007 task);
9008}
9009
9010/*
9011 * Called from nfs4_state_manager thread for session setup, so don't recover
9012 * from sequence operation or clientid errors.
9013 */
9014static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9015{
9016 struct nfs4_get_lease_time_data *data =
9017 (struct nfs4_get_lease_time_data *)calldata;
9018
9019 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9020 return;
9021 switch (task->tk_status) {
9022 case -NFS4ERR_DELAY:
9023 case -NFS4ERR_GRACE:
9024 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9025 task->tk_status = 0;
9026 fallthrough;
9027 case -NFS4ERR_RETRY_UNCACHED_REP:
9028 rpc_restart_call_prepare(task);
9029 return;
9030 }
9031}
9032
9033static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9034 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9035 .rpc_call_done = nfs4_get_lease_time_done,
9036};
9037
9038int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9039{
9040 struct nfs4_get_lease_time_args args;
9041 struct nfs4_get_lease_time_res res = {
9042 .lr_fsinfo = fsinfo,
9043 };
9044 struct nfs4_get_lease_time_data data = {
9045 .args = &args,
9046 .res = &res,
9047 .clp = clp,
9048 };
9049 struct rpc_message msg = {
9050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9051 .rpc_argp = &args,
9052 .rpc_resp = &res,
9053 };
9054 struct rpc_task_setup task_setup = {
9055 .rpc_client = clp->cl_rpcclient,
9056 .rpc_message = &msg,
9057 .callback_ops = &nfs4_get_lease_time_ops,
9058 .callback_data = &data,
9059 .flags = RPC_TASK_TIMEOUT,
9060 };
9061
9062 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9063 return nfs4_call_sync_custom(&task_setup);
9064}
9065
9066#ifdef CONFIG_NFS_V4_1
9067
9068/*
9069 * Initialize the values to be used by the client in CREATE_SESSION
9070 * If nfs4_init_session set the fore channel request and response sizes,
9071 * use them.
9072 *
9073 * Set the back channel max_resp_sz_cached to zero to force the client to
9074 * always set csa_cachethis to FALSE because the current implementation
9075 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9076 */
9077static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9078 struct rpc_clnt *clnt)
9079{
9080 unsigned int max_rqst_sz, max_resp_sz;
9081 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9082 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9083
9084 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9085 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9086
9087 /* Fore channel attributes */
9088 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9089 args->fc_attrs.max_resp_sz = max_resp_sz;
9090 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9091 args->fc_attrs.max_reqs = max_session_slots;
9092
9093 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9094 "max_ops=%u max_reqs=%u\n",
9095 __func__,
9096 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9097 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9098
9099 /* Back channel attributes */
9100 args->bc_attrs.max_rqst_sz = max_bc_payload;
9101 args->bc_attrs.max_resp_sz = max_bc_payload;
9102 args->bc_attrs.max_resp_sz_cached = 0;
9103 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9104 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9105 if (args->bc_attrs.max_reqs > max_bc_slots)
9106 args->bc_attrs.max_reqs = max_bc_slots;
9107
9108 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9109 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9110 __func__,
9111 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9112 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9113 args->bc_attrs.max_reqs);
9114}
9115
9116static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9117 struct nfs41_create_session_res *res)
9118{
9119 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9120 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9121
9122 if (rcvd->max_resp_sz > sent->max_resp_sz)
9123 return -EINVAL;
9124 /*
9125 * Our requested max_ops is the minimum we need; we're not
9126 * prepared to break up compounds into smaller pieces than that.
9127 * So, no point even trying to continue if the server won't
9128 * cooperate:
9129 */
9130 if (rcvd->max_ops < sent->max_ops)
9131 return -EINVAL;
9132 if (rcvd->max_reqs == 0)
9133 return -EINVAL;
9134 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9135 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9136 return 0;
9137}
9138
9139static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9140 struct nfs41_create_session_res *res)
9141{
9142 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9143 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9144
9145 if (!(res->flags & SESSION4_BACK_CHAN))
9146 goto out;
9147 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9148 return -EINVAL;
9149 if (rcvd->max_resp_sz < sent->max_resp_sz)
9150 return -EINVAL;
9151 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9152 return -EINVAL;
9153 if (rcvd->max_ops > sent->max_ops)
9154 return -EINVAL;
9155 if (rcvd->max_reqs > sent->max_reqs)
9156 return -EINVAL;
9157out:
9158 return 0;
9159}
9160
9161static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9162 struct nfs41_create_session_res *res)
9163{
9164 int ret;
9165
9166 ret = nfs4_verify_fore_channel_attrs(args, res);
9167 if (ret)
9168 return ret;
9169 return nfs4_verify_back_channel_attrs(args, res);
9170}
9171
9172static void nfs4_update_session(struct nfs4_session *session,
9173 struct nfs41_create_session_res *res)
9174{
9175 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9176 /* Mark client id and session as being confirmed */
9177 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9178 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9179 session->flags = res->flags;
9180 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9181 if (res->flags & SESSION4_BACK_CHAN)
9182 memcpy(&session->bc_attrs, &res->bc_attrs,
9183 sizeof(session->bc_attrs));
9184}
9185
9186static int _nfs4_proc_create_session(struct nfs_client *clp,
9187 const struct cred *cred)
9188{
9189 struct nfs4_session *session = clp->cl_session;
9190 struct nfs41_create_session_args args = {
9191 .client = clp,
9192 .clientid = clp->cl_clientid,
9193 .seqid = clp->cl_seqid,
9194 .cb_program = NFS4_CALLBACK,
9195 };
9196 struct nfs41_create_session_res res;
9197
9198 struct rpc_message msg = {
9199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9200 .rpc_argp = &args,
9201 .rpc_resp = &res,
9202 .rpc_cred = cred,
9203 };
9204 int status;
9205
9206 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9207 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9208
9209 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9210 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9211 trace_nfs4_create_session(clp, status);
9212
9213 switch (status) {
9214 case -NFS4ERR_STALE_CLIENTID:
9215 case -NFS4ERR_DELAY:
9216 case -ETIMEDOUT:
9217 case -EACCES:
9218 case -EAGAIN:
9219 goto out;
9220 }
9221
9222 clp->cl_seqid++;
9223 if (!status) {
9224 /* Verify the session's negotiated channel_attrs values */
9225 status = nfs4_verify_channel_attrs(&args, &res);
9226 /* Increment the clientid slot sequence id */
9227 if (status)
9228 goto out;
9229 nfs4_update_session(session, &res);
9230 }
9231out:
9232 return status;
9233}
9234
9235/*
9236 * Issues a CREATE_SESSION operation to the server.
9237 * It is the responsibility of the caller to verify the session is
9238 * expired before calling this routine.
9239 */
9240int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9241{
9242 int status;
9243 unsigned *ptr;
9244 struct nfs4_session *session = clp->cl_session;
9245
9246 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9247
9248 status = _nfs4_proc_create_session(clp, cred);
9249 if (status)
9250 goto out;
9251
9252 /* Init or reset the session slot tables */
9253 status = nfs4_setup_session_slot_tables(session);
9254 dprintk("slot table setup returned %d\n", status);
9255 if (status)
9256 goto out;
9257
9258 ptr = (unsigned *)&session->sess_id.data[0];
9259 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9260 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9261out:
9262 return status;
9263}
9264
9265/*
9266 * Issue the over-the-wire RPC DESTROY_SESSION.
9267 * The caller must serialize access to this routine.
9268 */
9269int nfs4_proc_destroy_session(struct nfs4_session *session,
9270 const struct cred *cred)
9271{
9272 struct rpc_message msg = {
9273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9274 .rpc_argp = session,
9275 .rpc_cred = cred,
9276 };
9277 int status = 0;
9278
9279 /* session is still being setup */
9280 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9281 return 0;
9282
9283 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9284 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9285 trace_nfs4_destroy_session(session->clp, status);
9286
9287 if (status)
9288 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9289 "Session has been destroyed regardless...\n", status);
9290 return status;
9291}
9292
9293/*
9294 * Renew the cl_session lease.
9295 */
9296struct nfs4_sequence_data {
9297 struct nfs_client *clp;
9298 struct nfs4_sequence_args args;
9299 struct nfs4_sequence_res res;
9300};
9301
9302static void nfs41_sequence_release(void *data)
9303{
9304 struct nfs4_sequence_data *calldata = data;
9305 struct nfs_client *clp = calldata->clp;
9306
9307 if (refcount_read(&clp->cl_count) > 1)
9308 nfs4_schedule_state_renewal(clp);
9309 nfs_put_client(clp);
9310 kfree(calldata);
9311}
9312
9313static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9314{
9315 switch(task->tk_status) {
9316 case -NFS4ERR_DELAY:
9317 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9318 return -EAGAIN;
9319 default:
9320 nfs4_schedule_lease_recovery(clp);
9321 }
9322 return 0;
9323}
9324
9325static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9326{
9327 struct nfs4_sequence_data *calldata = data;
9328 struct nfs_client *clp = calldata->clp;
9329
9330 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9331 return;
9332
9333 trace_nfs4_sequence(clp, task->tk_status);
9334 if (task->tk_status < 0) {
9335 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9336 if (refcount_read(&clp->cl_count) == 1)
9337 return;
9338
9339 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9340 rpc_restart_call_prepare(task);
9341 return;
9342 }
9343 }
9344 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9345}
9346
9347static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9348{
9349 struct nfs4_sequence_data *calldata = data;
9350 struct nfs_client *clp = calldata->clp;
9351 struct nfs4_sequence_args *args;
9352 struct nfs4_sequence_res *res;
9353
9354 args = task->tk_msg.rpc_argp;
9355 res = task->tk_msg.rpc_resp;
9356
9357 nfs4_setup_sequence(clp, args, res, task);
9358}
9359
9360static const struct rpc_call_ops nfs41_sequence_ops = {
9361 .rpc_call_done = nfs41_sequence_call_done,
9362 .rpc_call_prepare = nfs41_sequence_prepare,
9363 .rpc_release = nfs41_sequence_release,
9364};
9365
9366static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9367 const struct cred *cred,
9368 struct nfs4_slot *slot,
9369 bool is_privileged)
9370{
9371 struct nfs4_sequence_data *calldata;
9372 struct rpc_message msg = {
9373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9374 .rpc_cred = cred,
9375 };
9376 struct rpc_task_setup task_setup_data = {
9377 .rpc_client = clp->cl_rpcclient,
9378 .rpc_message = &msg,
9379 .callback_ops = &nfs41_sequence_ops,
9380 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9381 };
9382 struct rpc_task *ret;
9383
9384 ret = ERR_PTR(-EIO);
9385 if (!refcount_inc_not_zero(&clp->cl_count))
9386 goto out_err;
9387
9388 ret = ERR_PTR(-ENOMEM);
9389 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9390 if (calldata == NULL)
9391 goto out_put_clp;
9392 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9393 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9394 msg.rpc_argp = &calldata->args;
9395 msg.rpc_resp = &calldata->res;
9396 calldata->clp = clp;
9397 task_setup_data.callback_data = calldata;
9398
9399 ret = rpc_run_task(&task_setup_data);
9400 if (IS_ERR(ret))
9401 goto out_err;
9402 return ret;
9403out_put_clp:
9404 nfs_put_client(clp);
9405out_err:
9406 nfs41_release_slot(slot);
9407 return ret;
9408}
9409
9410static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9411{
9412 struct rpc_task *task;
9413 int ret = 0;
9414
9415 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9416 return -EAGAIN;
9417 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9418 if (IS_ERR(task))
9419 ret = PTR_ERR(task);
9420 else
9421 rpc_put_task_async(task);
9422 dprintk("<-- %s status=%d\n", __func__, ret);
9423 return ret;
9424}
9425
9426static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9427{
9428 struct rpc_task *task;
9429 int ret;
9430
9431 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9432 if (IS_ERR(task)) {
9433 ret = PTR_ERR(task);
9434 goto out;
9435 }
9436 ret = rpc_wait_for_completion_task(task);
9437 if (!ret)
9438 ret = task->tk_status;
9439 rpc_put_task(task);
9440out:
9441 dprintk("<-- %s status=%d\n", __func__, ret);
9442 return ret;
9443}
9444
9445struct nfs4_reclaim_complete_data {
9446 struct nfs_client *clp;
9447 struct nfs41_reclaim_complete_args arg;
9448 struct nfs41_reclaim_complete_res res;
9449};
9450
9451static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9452{
9453 struct nfs4_reclaim_complete_data *calldata = data;
9454
9455 nfs4_setup_sequence(calldata->clp,
9456 &calldata->arg.seq_args,
9457 &calldata->res.seq_res,
9458 task);
9459}
9460
9461static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9462{
9463 switch(task->tk_status) {
9464 case 0:
9465 wake_up_all(&clp->cl_lock_waitq);
9466 fallthrough;
9467 case -NFS4ERR_COMPLETE_ALREADY:
9468 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9469 break;
9470 case -NFS4ERR_DELAY:
9471 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9472 fallthrough;
9473 case -NFS4ERR_RETRY_UNCACHED_REP:
9474 return -EAGAIN;
9475 case -NFS4ERR_BADSESSION:
9476 case -NFS4ERR_DEADSESSION:
9477 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9478 break;
9479 default:
9480 nfs4_schedule_lease_recovery(clp);
9481 }
9482 return 0;
9483}
9484
9485static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9486{
9487 struct nfs4_reclaim_complete_data *calldata = data;
9488 struct nfs_client *clp = calldata->clp;
9489 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9490
9491 if (!nfs41_sequence_done(task, res))
9492 return;
9493
9494 trace_nfs4_reclaim_complete(clp, task->tk_status);
9495 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9496 rpc_restart_call_prepare(task);
9497 return;
9498 }
9499}
9500
9501static void nfs4_free_reclaim_complete_data(void *data)
9502{
9503 struct nfs4_reclaim_complete_data *calldata = data;
9504
9505 kfree(calldata);
9506}
9507
9508static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9509 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9510 .rpc_call_done = nfs4_reclaim_complete_done,
9511 .rpc_release = nfs4_free_reclaim_complete_data,
9512};
9513
9514/*
9515 * Issue a global reclaim complete.
9516 */
9517static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9518 const struct cred *cred)
9519{
9520 struct nfs4_reclaim_complete_data *calldata;
9521 struct rpc_message msg = {
9522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9523 .rpc_cred = cred,
9524 };
9525 struct rpc_task_setup task_setup_data = {
9526 .rpc_client = clp->cl_rpcclient,
9527 .rpc_message = &msg,
9528 .callback_ops = &nfs4_reclaim_complete_call_ops,
9529 .flags = RPC_TASK_NO_ROUND_ROBIN,
9530 };
9531 int status = -ENOMEM;
9532
9533 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9534 if (calldata == NULL)
9535 goto out;
9536 calldata->clp = clp;
9537 calldata->arg.one_fs = 0;
9538
9539 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9540 msg.rpc_argp = &calldata->arg;
9541 msg.rpc_resp = &calldata->res;
9542 task_setup_data.callback_data = calldata;
9543 status = nfs4_call_sync_custom(&task_setup_data);
9544out:
9545 dprintk("<-- %s status=%d\n", __func__, status);
9546 return status;
9547}
9548
9549static void
9550nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9551{
9552 struct nfs4_layoutget *lgp = calldata;
9553 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9554
9555 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9556 &lgp->res.seq_res, task);
9557}
9558
9559static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9560{
9561 struct nfs4_layoutget *lgp = calldata;
9562
9563 nfs41_sequence_process(task, &lgp->res.seq_res);
9564}
9565
9566static int
9567nfs4_layoutget_handle_exception(struct rpc_task *task,
9568 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9569{
9570 struct inode *inode = lgp->args.inode;
9571 struct nfs_server *server = NFS_SERVER(inode);
9572 struct pnfs_layout_hdr *lo = lgp->lo;
9573 int nfs4err = task->tk_status;
9574 int err, status = 0;
9575 LIST_HEAD(head);
9576
9577 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9578
9579 nfs4_sequence_free_slot(&lgp->res.seq_res);
9580
9581 switch (nfs4err) {
9582 case 0:
9583 goto out;
9584
9585 /*
9586 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9587 * on the file. set tk_status to -ENODATA to tell upper layer to
9588 * retry go inband.
9589 */
9590 case -NFS4ERR_LAYOUTUNAVAILABLE:
9591 status = -ENODATA;
9592 goto out;
9593 /*
9594 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9595 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9596 */
9597 case -NFS4ERR_BADLAYOUT:
9598 status = -EOVERFLOW;
9599 goto out;
9600 /*
9601 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9602 * (or clients) writing to the same RAID stripe except when
9603 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9604 *
9605 * Treat it like we would RECALLCONFLICT -- we retry for a little
9606 * while, and then eventually give up.
9607 */
9608 case -NFS4ERR_LAYOUTTRYLATER:
9609 if (lgp->args.minlength == 0) {
9610 status = -EOVERFLOW;
9611 goto out;
9612 }
9613 status = -EBUSY;
9614 break;
9615 case -NFS4ERR_RECALLCONFLICT:
9616 status = -ERECALLCONFLICT;
9617 break;
9618 case -NFS4ERR_DELEG_REVOKED:
9619 case -NFS4ERR_ADMIN_REVOKED:
9620 case -NFS4ERR_EXPIRED:
9621 case -NFS4ERR_BAD_STATEID:
9622 exception->timeout = 0;
9623 spin_lock(&inode->i_lock);
9624 /* If the open stateid was bad, then recover it. */
9625 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9626 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9627 spin_unlock(&inode->i_lock);
9628 exception->state = lgp->args.ctx->state;
9629 exception->stateid = &lgp->args.stateid;
9630 break;
9631 }
9632
9633 /*
9634 * Mark the bad layout state as invalid, then retry
9635 */
9636 pnfs_mark_layout_stateid_invalid(lo, &head);
9637 spin_unlock(&inode->i_lock);
9638 nfs_commit_inode(inode, 0);
9639 pnfs_free_lseg_list(&head);
9640 status = -EAGAIN;
9641 goto out;
9642 }
9643
9644 err = nfs4_handle_exception(server, nfs4err, exception);
9645 if (!status) {
9646 if (exception->retry)
9647 status = -EAGAIN;
9648 else
9649 status = err;
9650 }
9651out:
9652 return status;
9653}
9654
9655size_t max_response_pages(struct nfs_server *server)
9656{
9657 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9658 return nfs_page_array_len(0, max_resp_sz);
9659}
9660
9661static void nfs4_layoutget_release(void *calldata)
9662{
9663 struct nfs4_layoutget *lgp = calldata;
9664
9665 nfs4_sequence_free_slot(&lgp->res.seq_res);
9666 pnfs_layoutget_free(lgp);
9667}
9668
9669static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9670 .rpc_call_prepare = nfs4_layoutget_prepare,
9671 .rpc_call_done = nfs4_layoutget_done,
9672 .rpc_release = nfs4_layoutget_release,
9673};
9674
9675struct pnfs_layout_segment *
9676nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
9677{
9678 struct inode *inode = lgp->args.inode;
9679 struct nfs_server *server = NFS_SERVER(inode);
9680 struct rpc_task *task;
9681 struct rpc_message msg = {
9682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9683 .rpc_argp = &lgp->args,
9684 .rpc_resp = &lgp->res,
9685 .rpc_cred = lgp->cred,
9686 };
9687 struct rpc_task_setup task_setup_data = {
9688 .rpc_client = server->client,
9689 .rpc_message = &msg,
9690 .callback_ops = &nfs4_layoutget_call_ops,
9691 .callback_data = lgp,
9692 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9693 RPC_TASK_MOVEABLE,
9694 };
9695 struct pnfs_layout_segment *lseg = NULL;
9696 struct nfs4_exception exception = {
9697 .inode = inode,
9698 .timeout = *timeout,
9699 };
9700 int status = 0;
9701
9702 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9703
9704 task = rpc_run_task(&task_setup_data);
9705 if (IS_ERR(task))
9706 return ERR_CAST(task);
9707
9708 status = rpc_wait_for_completion_task(task);
9709 if (status != 0)
9710 goto out;
9711
9712 if (task->tk_status < 0) {
9713 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9714 *timeout = exception.timeout;
9715 } else if (lgp->res.layoutp->len == 0) {
9716 status = -EAGAIN;
9717 *timeout = nfs4_update_delay(&exception.timeout);
9718 } else
9719 lseg = pnfs_layout_process(lgp);
9720out:
9721 trace_nfs4_layoutget(lgp->args.ctx,
9722 &lgp->args.range,
9723 &lgp->res.range,
9724 &lgp->res.stateid,
9725 status);
9726
9727 rpc_put_task(task);
9728 dprintk("<-- %s status=%d\n", __func__, status);
9729 if (status)
9730 return ERR_PTR(status);
9731 return lseg;
9732}
9733
9734static void
9735nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9736{
9737 struct nfs4_layoutreturn *lrp = calldata;
9738
9739 nfs4_setup_sequence(lrp->clp,
9740 &lrp->args.seq_args,
9741 &lrp->res.seq_res,
9742 task);
9743 if (!pnfs_layout_is_valid(lrp->args.layout))
9744 rpc_exit(task, 0);
9745}
9746
9747static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9748{
9749 struct nfs4_layoutreturn *lrp = calldata;
9750 struct nfs_server *server;
9751
9752 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9753 return;
9754
9755 /*
9756 * Was there an RPC level error? Assume the call succeeded,
9757 * and that we need to release the layout
9758 */
9759 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9760 lrp->res.lrs_present = 0;
9761 return;
9762 }
9763
9764 server = NFS_SERVER(lrp->args.inode);
9765 switch (task->tk_status) {
9766 case -NFS4ERR_OLD_STATEID:
9767 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
9768 &lrp->args.range,
9769 lrp->args.inode))
9770 goto out_restart;
9771 fallthrough;
9772 default:
9773 task->tk_status = 0;
9774 fallthrough;
9775 case 0:
9776 break;
9777 case -NFS4ERR_DELAY:
9778 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
9779 break;
9780 goto out_restart;
9781 }
9782 return;
9783out_restart:
9784 task->tk_status = 0;
9785 nfs4_sequence_free_slot(&lrp->res.seq_res);
9786 rpc_restart_call_prepare(task);
9787}
9788
9789static void nfs4_layoutreturn_release(void *calldata)
9790{
9791 struct nfs4_layoutreturn *lrp = calldata;
9792 struct pnfs_layout_hdr *lo = lrp->args.layout;
9793
9794 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9795 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9796 nfs4_sequence_free_slot(&lrp->res.seq_res);
9797 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9798 lrp->ld_private.ops->free(&lrp->ld_private);
9799 pnfs_put_layout_hdr(lrp->args.layout);
9800 nfs_iput_and_deactive(lrp->inode);
9801 put_cred(lrp->cred);
9802 kfree(calldata);
9803}
9804
9805static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9806 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9807 .rpc_call_done = nfs4_layoutreturn_done,
9808 .rpc_release = nfs4_layoutreturn_release,
9809};
9810
9811int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9812{
9813 struct rpc_task *task;
9814 struct rpc_message msg = {
9815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9816 .rpc_argp = &lrp->args,
9817 .rpc_resp = &lrp->res,
9818 .rpc_cred = lrp->cred,
9819 };
9820 struct rpc_task_setup task_setup_data = {
9821 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9822 .rpc_message = &msg,
9823 .callback_ops = &nfs4_layoutreturn_call_ops,
9824 .callback_data = lrp,
9825 .flags = RPC_TASK_MOVEABLE,
9826 };
9827 int status = 0;
9828
9829 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9830 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9831 &task_setup_data.rpc_client, &msg);
9832
9833 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9834 if (!sync) {
9835 if (!lrp->inode) {
9836 nfs4_layoutreturn_release(lrp);
9837 return -EAGAIN;
9838 }
9839 task_setup_data.flags |= RPC_TASK_ASYNC;
9840 }
9841 if (!lrp->inode)
9842 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9843 1);
9844 else
9845 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9846 0);
9847 task = rpc_run_task(&task_setup_data);
9848 if (IS_ERR(task))
9849 return PTR_ERR(task);
9850 if (sync)
9851 status = task->tk_status;
9852 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9853 dprintk("<-- %s status=%d\n", __func__, status);
9854 rpc_put_task(task);
9855 return status;
9856}
9857
9858static int
9859_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9860 struct pnfs_device *pdev,
9861 const struct cred *cred)
9862{
9863 struct nfs4_getdeviceinfo_args args = {
9864 .pdev = pdev,
9865 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9866 NOTIFY_DEVICEID4_DELETE,
9867 };
9868 struct nfs4_getdeviceinfo_res res = {
9869 .pdev = pdev,
9870 };
9871 struct rpc_message msg = {
9872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9873 .rpc_argp = &args,
9874 .rpc_resp = &res,
9875 .rpc_cred = cred,
9876 };
9877 int status;
9878
9879 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9880 if (res.notification & ~args.notify_types)
9881 dprintk("%s: unsupported notification\n", __func__);
9882 if (res.notification != args.notify_types)
9883 pdev->nocache = 1;
9884
9885 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9886
9887 dprintk("<-- %s status=%d\n", __func__, status);
9888
9889 return status;
9890}
9891
9892int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9893 struct pnfs_device *pdev,
9894 const struct cred *cred)
9895{
9896 struct nfs4_exception exception = { };
9897 int err;
9898
9899 do {
9900 err = nfs4_handle_exception(server,
9901 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9902 &exception);
9903 } while (exception.retry);
9904 return err;
9905}
9906EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9907
9908static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9909{
9910 struct nfs4_layoutcommit_data *data = calldata;
9911 struct nfs_server *server = NFS_SERVER(data->args.inode);
9912
9913 nfs4_setup_sequence(server->nfs_client,
9914 &data->args.seq_args,
9915 &data->res.seq_res,
9916 task);
9917}
9918
9919static void
9920nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9921{
9922 struct nfs4_layoutcommit_data *data = calldata;
9923 struct nfs_server *server = NFS_SERVER(data->args.inode);
9924
9925 if (!nfs41_sequence_done(task, &data->res.seq_res))
9926 return;
9927
9928 switch (task->tk_status) { /* Just ignore these failures */
9929 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9930 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9931 case -NFS4ERR_BADLAYOUT: /* no layout */
9932 case -NFS4ERR_GRACE: /* loca_recalim always false */
9933 task->tk_status = 0;
9934 break;
9935 case 0:
9936 break;
9937 default:
9938 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9939 rpc_restart_call_prepare(task);
9940 return;
9941 }
9942 }
9943}
9944
9945static void nfs4_layoutcommit_release(void *calldata)
9946{
9947 struct nfs4_layoutcommit_data *data = calldata;
9948
9949 pnfs_cleanup_layoutcommit(data);
9950 nfs_post_op_update_inode_force_wcc(data->args.inode,
9951 data->res.fattr);
9952 put_cred(data->cred);
9953 nfs_iput_and_deactive(data->inode);
9954 kfree(data);
9955}
9956
9957static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9958 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9959 .rpc_call_done = nfs4_layoutcommit_done,
9960 .rpc_release = nfs4_layoutcommit_release,
9961};
9962
9963int
9964nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9965{
9966 struct rpc_message msg = {
9967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9968 .rpc_argp = &data->args,
9969 .rpc_resp = &data->res,
9970 .rpc_cred = data->cred,
9971 };
9972 struct rpc_task_setup task_setup_data = {
9973 .task = &data->task,
9974 .rpc_client = NFS_CLIENT(data->args.inode),
9975 .rpc_message = &msg,
9976 .callback_ops = &nfs4_layoutcommit_ops,
9977 .callback_data = data,
9978 .flags = RPC_TASK_MOVEABLE,
9979 };
9980 struct rpc_task *task;
9981 int status = 0;
9982
9983 dprintk("NFS: initiating layoutcommit call. sync %d "
9984 "lbw: %llu inode %lu\n", sync,
9985 data->args.lastbytewritten,
9986 data->args.inode->i_ino);
9987
9988 if (!sync) {
9989 data->inode = nfs_igrab_and_active(data->args.inode);
9990 if (data->inode == NULL) {
9991 nfs4_layoutcommit_release(data);
9992 return -EAGAIN;
9993 }
9994 task_setup_data.flags = RPC_TASK_ASYNC;
9995 }
9996 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9997 task = rpc_run_task(&task_setup_data);
9998 if (IS_ERR(task))
9999 return PTR_ERR(task);
10000 if (sync)
10001 status = task->tk_status;
10002 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10003 dprintk("%s: status %d\n", __func__, status);
10004 rpc_put_task(task);
10005 return status;
10006}
10007
10008/*
10009 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10010 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10011 */
10012static int
10013_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10014 struct nfs_fsinfo *info,
10015 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10016{
10017 struct nfs41_secinfo_no_name_args args = {
10018 .style = SECINFO_STYLE_CURRENT_FH,
10019 };
10020 struct nfs4_secinfo_res res = {
10021 .flavors = flavors,
10022 };
10023 struct rpc_message msg = {
10024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10025 .rpc_argp = &args,
10026 .rpc_resp = &res,
10027 };
10028 struct nfs4_call_sync_data data = {
10029 .seq_server = server,
10030 .seq_args = &args.seq_args,
10031 .seq_res = &res.seq_res,
10032 };
10033 struct rpc_task_setup task_setup = {
10034 .rpc_client = server->client,
10035 .rpc_message = &msg,
10036 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10037 .callback_data = &data,
10038 .flags = RPC_TASK_NO_ROUND_ROBIN,
10039 };
10040 const struct cred *cred = NULL;
10041 int status;
10042
10043 if (use_integrity) {
10044 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10045
10046 cred = nfs4_get_clid_cred(server->nfs_client);
10047 msg.rpc_cred = cred;
10048 }
10049
10050 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10051 status = nfs4_call_sync_custom(&task_setup);
10052 dprintk("<-- %s status=%d\n", __func__, status);
10053
10054 put_cred(cred);
10055
10056 return status;
10057}
10058
10059static int
10060nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10061 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10062{
10063 struct nfs4_exception exception = {
10064 .interruptible = true,
10065 };
10066 int err;
10067 do {
10068 /* first try using integrity protection */
10069 err = -NFS4ERR_WRONGSEC;
10070
10071 /* try to use integrity protection with machine cred */
10072 if (_nfs4_is_integrity_protected(server->nfs_client))
10073 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10074 flavors, true);
10075
10076 /*
10077 * if unable to use integrity protection, or SECINFO with
10078 * integrity protection returns NFS4ERR_WRONGSEC (which is
10079 * disallowed by spec, but exists in deployed servers) use
10080 * the current filesystem's rpc_client and the user cred.
10081 */
10082 if (err == -NFS4ERR_WRONGSEC)
10083 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10084 flavors, false);
10085
10086 switch (err) {
10087 case 0:
10088 case -NFS4ERR_WRONGSEC:
10089 case -ENOTSUPP:
10090 goto out;
10091 default:
10092 err = nfs4_handle_exception(server, err, &exception);
10093 }
10094 } while (exception.retry);
10095out:
10096 return err;
10097}
10098
10099static int
10100nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10101 struct nfs_fsinfo *info)
10102{
10103 int err;
10104 struct page *page;
10105 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10106 struct nfs4_secinfo_flavors *flavors;
10107 struct nfs4_secinfo4 *secinfo;
10108 int i;
10109
10110 page = alloc_page(GFP_KERNEL);
10111 if (!page) {
10112 err = -ENOMEM;
10113 goto out;
10114 }
10115
10116 flavors = page_address(page);
10117 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10118
10119 /*
10120 * Fall back on "guess and check" method if
10121 * the server doesn't support SECINFO_NO_NAME
10122 */
10123 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10124 err = nfs4_find_root_sec(server, fhandle, info);
10125 goto out_freepage;
10126 }
10127 if (err)
10128 goto out_freepage;
10129
10130 for (i = 0; i < flavors->num_flavors; i++) {
10131 secinfo = &flavors->flavors[i];
10132
10133 switch (secinfo->flavor) {
10134 case RPC_AUTH_NULL:
10135 case RPC_AUTH_UNIX:
10136 case RPC_AUTH_GSS:
10137 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10138 &secinfo->flavor_info);
10139 break;
10140 default:
10141 flavor = RPC_AUTH_MAXFLAVOR;
10142 break;
10143 }
10144
10145 if (!nfs_auth_info_match(&server->auth_info, flavor))
10146 flavor = RPC_AUTH_MAXFLAVOR;
10147
10148 if (flavor != RPC_AUTH_MAXFLAVOR) {
10149 err = nfs4_lookup_root_sec(server, fhandle,
10150 info, flavor);
10151 if (!err)
10152 break;
10153 }
10154 }
10155
10156 if (flavor == RPC_AUTH_MAXFLAVOR)
10157 err = -EPERM;
10158
10159out_freepage:
10160 put_page(page);
10161 if (err == -EACCES)
10162 return -EPERM;
10163out:
10164 return err;
10165}
10166
10167static int _nfs41_test_stateid(struct nfs_server *server,
10168 nfs4_stateid *stateid,
10169 const struct cred *cred)
10170{
10171 int status;
10172 struct nfs41_test_stateid_args args = {
10173 .stateid = stateid,
10174 };
10175 struct nfs41_test_stateid_res res;
10176 struct rpc_message msg = {
10177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10178 .rpc_argp = &args,
10179 .rpc_resp = &res,
10180 .rpc_cred = cred,
10181 };
10182 struct rpc_clnt *rpc_client = server->client;
10183
10184 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10185 &rpc_client, &msg);
10186
10187 dprintk("NFS call test_stateid %p\n", stateid);
10188 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10189 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10190 &args.seq_args, &res.seq_res);
10191 if (status != NFS_OK) {
10192 dprintk("NFS reply test_stateid: failed, %d\n", status);
10193 return status;
10194 }
10195 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10196 return -res.status;
10197}
10198
10199static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10200 int err, struct nfs4_exception *exception)
10201{
10202 exception->retry = 0;
10203 switch(err) {
10204 case -NFS4ERR_DELAY:
10205 case -NFS4ERR_RETRY_UNCACHED_REP:
10206 nfs4_handle_exception(server, err, exception);
10207 break;
10208 case -NFS4ERR_BADSESSION:
10209 case -NFS4ERR_BADSLOT:
10210 case -NFS4ERR_BAD_HIGH_SLOT:
10211 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10212 case -NFS4ERR_DEADSESSION:
10213 nfs4_do_handle_exception(server, err, exception);
10214 }
10215}
10216
10217/**
10218 * nfs41_test_stateid - perform a TEST_STATEID operation
10219 *
10220 * @server: server / transport on which to perform the operation
10221 * @stateid: state ID to test
10222 * @cred: credential
10223 *
10224 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10225 * Otherwise a negative NFS4ERR value is returned if the operation
10226 * failed or the state ID is not currently valid.
10227 */
10228static int nfs41_test_stateid(struct nfs_server *server,
10229 nfs4_stateid *stateid,
10230 const struct cred *cred)
10231{
10232 struct nfs4_exception exception = {
10233 .interruptible = true,
10234 };
10235 int err;
10236 do {
10237 err = _nfs41_test_stateid(server, stateid, cred);
10238 nfs4_handle_delay_or_session_error(server, err, &exception);
10239 } while (exception.retry);
10240 return err;
10241}
10242
10243struct nfs_free_stateid_data {
10244 struct nfs_server *server;
10245 struct nfs41_free_stateid_args args;
10246 struct nfs41_free_stateid_res res;
10247};
10248
10249static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10250{
10251 struct nfs_free_stateid_data *data = calldata;
10252 nfs4_setup_sequence(data->server->nfs_client,
10253 &data->args.seq_args,
10254 &data->res.seq_res,
10255 task);
10256}
10257
10258static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10259{
10260 struct nfs_free_stateid_data *data = calldata;
10261
10262 nfs41_sequence_done(task, &data->res.seq_res);
10263
10264 switch (task->tk_status) {
10265 case -NFS4ERR_DELAY:
10266 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10267 rpc_restart_call_prepare(task);
10268 }
10269}
10270
10271static void nfs41_free_stateid_release(void *calldata)
10272{
10273 struct nfs_free_stateid_data *data = calldata;
10274 struct nfs_client *clp = data->server->nfs_client;
10275
10276 nfs_put_client(clp);
10277 kfree(calldata);
10278}
10279
10280static const struct rpc_call_ops nfs41_free_stateid_ops = {
10281 .rpc_call_prepare = nfs41_free_stateid_prepare,
10282 .rpc_call_done = nfs41_free_stateid_done,
10283 .rpc_release = nfs41_free_stateid_release,
10284};
10285
10286/**
10287 * nfs41_free_stateid - perform a FREE_STATEID operation
10288 *
10289 * @server: server / transport on which to perform the operation
10290 * @stateid: state ID to release
10291 * @cred: credential
10292 * @privileged: set to true if this call needs to be privileged
10293 *
10294 * Note: this function is always asynchronous.
10295 */
10296static int nfs41_free_stateid(struct nfs_server *server,
10297 const nfs4_stateid *stateid,
10298 const struct cred *cred,
10299 bool privileged)
10300{
10301 struct rpc_message msg = {
10302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10303 .rpc_cred = cred,
10304 };
10305 struct rpc_task_setup task_setup = {
10306 .rpc_client = server->client,
10307 .rpc_message = &msg,
10308 .callback_ops = &nfs41_free_stateid_ops,
10309 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10310 };
10311 struct nfs_free_stateid_data *data;
10312 struct rpc_task *task;
10313 struct nfs_client *clp = server->nfs_client;
10314
10315 if (!refcount_inc_not_zero(&clp->cl_count))
10316 return -EIO;
10317
10318 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10319 &task_setup.rpc_client, &msg);
10320
10321 dprintk("NFS call free_stateid %p\n", stateid);
10322 data = kmalloc(sizeof(*data), GFP_KERNEL);
10323 if (!data)
10324 return -ENOMEM;
10325 data->server = server;
10326 nfs4_stateid_copy(&data->args.stateid, stateid);
10327
10328 task_setup.callback_data = data;
10329
10330 msg.rpc_argp = &data->args;
10331 msg.rpc_resp = &data->res;
10332 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10333 task = rpc_run_task(&task_setup);
10334 if (IS_ERR(task))
10335 return PTR_ERR(task);
10336 rpc_put_task(task);
10337 return 0;
10338}
10339
10340static void
10341nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10342{
10343 const struct cred *cred = lsp->ls_state->owner->so_cred;
10344
10345 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10346 nfs4_free_lock_state(server, lsp);
10347}
10348
10349static bool nfs41_match_stateid(const nfs4_stateid *s1,
10350 const nfs4_stateid *s2)
10351{
10352 if (s1->type != s2->type)
10353 return false;
10354
10355 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10356 return false;
10357
10358 if (s1->seqid == s2->seqid)
10359 return true;
10360
10361 return s1->seqid == 0 || s2->seqid == 0;
10362}
10363
10364#endif /* CONFIG_NFS_V4_1 */
10365
10366static bool nfs4_match_stateid(const nfs4_stateid *s1,
10367 const nfs4_stateid *s2)
10368{
10369 return nfs4_stateid_match(s1, s2);
10370}
10371
10372
10373static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10374 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10375 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10376 .recover_open = nfs4_open_reclaim,
10377 .recover_lock = nfs4_lock_reclaim,
10378 .establish_clid = nfs4_init_clientid,
10379 .detect_trunking = nfs40_discover_server_trunking,
10380};
10381
10382#if defined(CONFIG_NFS_V4_1)
10383static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10384 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10385 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10386 .recover_open = nfs4_open_reclaim,
10387 .recover_lock = nfs4_lock_reclaim,
10388 .establish_clid = nfs41_init_clientid,
10389 .reclaim_complete = nfs41_proc_reclaim_complete,
10390 .detect_trunking = nfs41_discover_server_trunking,
10391};
10392#endif /* CONFIG_NFS_V4_1 */
10393
10394static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10395 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10396 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10397 .recover_open = nfs40_open_expired,
10398 .recover_lock = nfs4_lock_expired,
10399 .establish_clid = nfs4_init_clientid,
10400};
10401
10402#if defined(CONFIG_NFS_V4_1)
10403static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10404 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10405 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10406 .recover_open = nfs41_open_expired,
10407 .recover_lock = nfs41_lock_expired,
10408 .establish_clid = nfs41_init_clientid,
10409};
10410#endif /* CONFIG_NFS_V4_1 */
10411
10412static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10413 .sched_state_renewal = nfs4_proc_async_renew,
10414 .get_state_renewal_cred = nfs4_get_renew_cred,
10415 .renew_lease = nfs4_proc_renew,
10416};
10417
10418#if defined(CONFIG_NFS_V4_1)
10419static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10420 .sched_state_renewal = nfs41_proc_async_sequence,
10421 .get_state_renewal_cred = nfs4_get_machine_cred,
10422 .renew_lease = nfs4_proc_sequence,
10423};
10424#endif
10425
10426static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10427 .get_locations = _nfs40_proc_get_locations,
10428 .fsid_present = _nfs40_proc_fsid_present,
10429};
10430
10431#if defined(CONFIG_NFS_V4_1)
10432static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10433 .get_locations = _nfs41_proc_get_locations,
10434 .fsid_present = _nfs41_proc_fsid_present,
10435};
10436#endif /* CONFIG_NFS_V4_1 */
10437
10438static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10439 .minor_version = 0,
10440 .init_caps = NFS_CAP_READDIRPLUS
10441 | NFS_CAP_ATOMIC_OPEN
10442 | NFS_CAP_POSIX_LOCK,
10443 .init_client = nfs40_init_client,
10444 .shutdown_client = nfs40_shutdown_client,
10445 .match_stateid = nfs4_match_stateid,
10446 .find_root_sec = nfs4_find_root_sec,
10447 .free_lock_state = nfs4_release_lockowner,
10448 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10449 .alloc_seqid = nfs_alloc_seqid,
10450 .call_sync_ops = &nfs40_call_sync_ops,
10451 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10452 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10453 .state_renewal_ops = &nfs40_state_renewal_ops,
10454 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10455};
10456
10457#if defined(CONFIG_NFS_V4_1)
10458static struct nfs_seqid *
10459nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10460{
10461 return NULL;
10462}
10463
10464static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10465 .minor_version = 1,
10466 .init_caps = NFS_CAP_READDIRPLUS
10467 | NFS_CAP_ATOMIC_OPEN
10468 | NFS_CAP_POSIX_LOCK
10469 | NFS_CAP_STATEID_NFSV41
10470 | NFS_CAP_ATOMIC_OPEN_V1
10471 | NFS_CAP_LGOPEN,
10472 .init_client = nfs41_init_client,
10473 .shutdown_client = nfs41_shutdown_client,
10474 .match_stateid = nfs41_match_stateid,
10475 .find_root_sec = nfs41_find_root_sec,
10476 .free_lock_state = nfs41_free_lock_state,
10477 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10478 .alloc_seqid = nfs_alloc_no_seqid,
10479 .session_trunk = nfs4_test_session_trunk,
10480 .call_sync_ops = &nfs41_call_sync_ops,
10481 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10482 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10483 .state_renewal_ops = &nfs41_state_renewal_ops,
10484 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10485};
10486#endif
10487
10488#if defined(CONFIG_NFS_V4_2)
10489static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10490 .minor_version = 2,
10491 .init_caps = NFS_CAP_READDIRPLUS
10492 | NFS_CAP_ATOMIC_OPEN
10493 | NFS_CAP_POSIX_LOCK
10494 | NFS_CAP_STATEID_NFSV41
10495 | NFS_CAP_ATOMIC_OPEN_V1
10496 | NFS_CAP_LGOPEN
10497 | NFS_CAP_ALLOCATE
10498 | NFS_CAP_COPY
10499 | NFS_CAP_OFFLOAD_CANCEL
10500 | NFS_CAP_COPY_NOTIFY
10501 | NFS_CAP_DEALLOCATE
10502 | NFS_CAP_SEEK
10503 | NFS_CAP_LAYOUTSTATS
10504 | NFS_CAP_CLONE
10505 | NFS_CAP_LAYOUTERROR
10506 | NFS_CAP_READ_PLUS,
10507 .init_client = nfs41_init_client,
10508 .shutdown_client = nfs41_shutdown_client,
10509 .match_stateid = nfs41_match_stateid,
10510 .find_root_sec = nfs41_find_root_sec,
10511 .free_lock_state = nfs41_free_lock_state,
10512 .call_sync_ops = &nfs41_call_sync_ops,
10513 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10514 .alloc_seqid = nfs_alloc_no_seqid,
10515 .session_trunk = nfs4_test_session_trunk,
10516 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10517 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10518 .state_renewal_ops = &nfs41_state_renewal_ops,
10519 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10520};
10521#endif
10522
10523const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10524 [0] = &nfs_v4_0_minor_ops,
10525#if defined(CONFIG_NFS_V4_1)
10526 [1] = &nfs_v4_1_minor_ops,
10527#endif
10528#if defined(CONFIG_NFS_V4_2)
10529 [2] = &nfs_v4_2_minor_ops,
10530#endif
10531};
10532
10533static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10534{
10535 ssize_t error, error2, error3;
10536
10537 error = generic_listxattr(dentry, list, size);
10538 if (error < 0)
10539 return error;
10540 if (list) {
10541 list += error;
10542 size -= error;
10543 }
10544
10545 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10546 if (error2 < 0)
10547 return error2;
10548
10549 if (list) {
10550 list += error2;
10551 size -= error2;
10552 }
10553
10554 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10555 if (error3 < 0)
10556 return error3;
10557
10558 return error + error2 + error3;
10559}
10560
10561static void nfs4_enable_swap(struct inode *inode)
10562{
10563 /* The state manager thread must always be running.
10564 * It will notice the client is a swapper, and stay put.
10565 */
10566 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10567
10568 nfs4_schedule_state_manager(clp);
10569}
10570
10571static void nfs4_disable_swap(struct inode *inode)
10572{
10573 /* The state manager thread will now exit once it is
10574 * woken.
10575 */
10576 wake_up_var(&NFS_SERVER(inode)->nfs_client->cl_state);
10577}
10578
10579static const struct inode_operations nfs4_dir_inode_operations = {
10580 .create = nfs_create,
10581 .lookup = nfs_lookup,
10582 .atomic_open = nfs_atomic_open,
10583 .link = nfs_link,
10584 .unlink = nfs_unlink,
10585 .symlink = nfs_symlink,
10586 .mkdir = nfs_mkdir,
10587 .rmdir = nfs_rmdir,
10588 .mknod = nfs_mknod,
10589 .rename = nfs_rename,
10590 .permission = nfs_permission,
10591 .getattr = nfs_getattr,
10592 .setattr = nfs_setattr,
10593 .listxattr = nfs4_listxattr,
10594};
10595
10596static const struct inode_operations nfs4_file_inode_operations = {
10597 .permission = nfs_permission,
10598 .getattr = nfs_getattr,
10599 .setattr = nfs_setattr,
10600 .listxattr = nfs4_listxattr,
10601};
10602
10603const struct nfs_rpc_ops nfs_v4_clientops = {
10604 .version = 4, /* protocol version */
10605 .dentry_ops = &nfs4_dentry_operations,
10606 .dir_inode_ops = &nfs4_dir_inode_operations,
10607 .file_inode_ops = &nfs4_file_inode_operations,
10608 .file_ops = &nfs4_file_operations,
10609 .getroot = nfs4_proc_get_root,
10610 .submount = nfs4_submount,
10611 .try_get_tree = nfs4_try_get_tree,
10612 .getattr = nfs4_proc_getattr,
10613 .setattr = nfs4_proc_setattr,
10614 .lookup = nfs4_proc_lookup,
10615 .lookupp = nfs4_proc_lookupp,
10616 .access = nfs4_proc_access,
10617 .readlink = nfs4_proc_readlink,
10618 .create = nfs4_proc_create,
10619 .remove = nfs4_proc_remove,
10620 .unlink_setup = nfs4_proc_unlink_setup,
10621 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10622 .unlink_done = nfs4_proc_unlink_done,
10623 .rename_setup = nfs4_proc_rename_setup,
10624 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10625 .rename_done = nfs4_proc_rename_done,
10626 .link = nfs4_proc_link,
10627 .symlink = nfs4_proc_symlink,
10628 .mkdir = nfs4_proc_mkdir,
10629 .rmdir = nfs4_proc_rmdir,
10630 .readdir = nfs4_proc_readdir,
10631 .mknod = nfs4_proc_mknod,
10632 .statfs = nfs4_proc_statfs,
10633 .fsinfo = nfs4_proc_fsinfo,
10634 .pathconf = nfs4_proc_pathconf,
10635 .set_capabilities = nfs4_server_capabilities,
10636 .decode_dirent = nfs4_decode_dirent,
10637 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10638 .read_setup = nfs4_proc_read_setup,
10639 .read_done = nfs4_read_done,
10640 .write_setup = nfs4_proc_write_setup,
10641 .write_done = nfs4_write_done,
10642 .commit_setup = nfs4_proc_commit_setup,
10643 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10644 .commit_done = nfs4_commit_done,
10645 .lock = nfs4_proc_lock,
10646 .clear_acl_cache = nfs4_zap_acl_attr,
10647 .close_context = nfs4_close_context,
10648 .open_context = nfs4_atomic_open,
10649 .have_delegation = nfs4_have_delegation,
10650 .alloc_client = nfs4_alloc_client,
10651 .init_client = nfs4_init_client,
10652 .free_client = nfs4_free_client,
10653 .create_server = nfs4_create_server,
10654 .clone_server = nfs_clone_server,
10655 .discover_trunking = nfs4_discover_trunking,
10656 .enable_swap = nfs4_enable_swap,
10657 .disable_swap = nfs4_disable_swap,
10658};
10659
10660static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10661 .name = XATTR_NAME_NFSV4_ACL,
10662 .list = nfs4_xattr_list_nfs4_acl,
10663 .get = nfs4_xattr_get_nfs4_acl,
10664 .set = nfs4_xattr_set_nfs4_acl,
10665};
10666
10667#if defined(CONFIG_NFS_V4_1)
10668static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10669 .name = XATTR_NAME_NFSV4_DACL,
10670 .list = nfs4_xattr_list_nfs4_dacl,
10671 .get = nfs4_xattr_get_nfs4_dacl,
10672 .set = nfs4_xattr_set_nfs4_dacl,
10673};
10674
10675static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10676 .name = XATTR_NAME_NFSV4_SACL,
10677 .list = nfs4_xattr_list_nfs4_sacl,
10678 .get = nfs4_xattr_get_nfs4_sacl,
10679 .set = nfs4_xattr_set_nfs4_sacl,
10680};
10681#endif
10682
10683#ifdef CONFIG_NFS_V4_2
10684static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10685 .prefix = XATTR_USER_PREFIX,
10686 .get = nfs4_xattr_get_nfs4_user,
10687 .set = nfs4_xattr_set_nfs4_user,
10688};
10689#endif
10690
10691const struct xattr_handler *nfs4_xattr_handlers[] = {
10692 &nfs4_xattr_nfs4_acl_handler,
10693#if defined(CONFIG_NFS_V4_1)
10694 &nfs4_xattr_nfs4_dacl_handler,
10695 &nfs4_xattr_nfs4_sacl_handler,
10696#endif
10697#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10698 &nfs4_xattr_nfs4_label_handler,
10699#endif
10700#ifdef CONFIG_NFS_V4_2
10701 &nfs4_xattr_nfs4_user_handler,
10702#endif
10703 NULL
10704};