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