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