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