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