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