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