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