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 unsigned short retrans;
3640 };
3641
nfs4_free_closedata(void * data)3642 static void nfs4_free_closedata(void *data)
3643 {
3644 struct nfs4_closedata *calldata = data;
3645 struct nfs4_state_owner *sp = calldata->state->owner;
3646 struct super_block *sb = calldata->state->inode->i_sb;
3647
3648 if (calldata->lr.roc)
3649 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3650 calldata->res.lr_ret);
3651 nfs4_put_open_state(calldata->state);
3652 nfs_free_seqid(calldata->arg.seqid);
3653 nfs4_put_state_owner(sp);
3654 nfs_sb_deactive(sb);
3655 kfree(calldata);
3656 }
3657
nfs4_close_done(struct rpc_task * task,void * data)3658 static void nfs4_close_done(struct rpc_task *task, void *data)
3659 {
3660 struct nfs4_closedata *calldata = data;
3661 struct nfs4_state *state = calldata->state;
3662 struct nfs_server *server = NFS_SERVER(calldata->inode);
3663 nfs4_stateid *res_stateid = NULL;
3664 struct nfs4_exception exception = {
3665 .state = state,
3666 .inode = calldata->inode,
3667 .stateid = &calldata->arg.stateid,
3668 .retrans = calldata->retrans,
3669 };
3670
3671 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3672 return;
3673 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3674
3675 /* Handle Layoutreturn errors */
3676 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3677 &calldata->res.lr_ret) == -EAGAIN)
3678 goto out_restart;
3679
3680 /* hmm. we are done with the inode, and in the process of freeing
3681 * the state_owner. we keep this around to process errors
3682 */
3683 switch (task->tk_status) {
3684 case 0:
3685 res_stateid = &calldata->res.stateid;
3686 renew_lease(server, calldata->timestamp);
3687 break;
3688 case -NFS4ERR_ACCESS:
3689 if (calldata->arg.bitmask != NULL) {
3690 calldata->arg.bitmask = NULL;
3691 calldata->res.fattr = NULL;
3692 goto out_restart;
3693
3694 }
3695 break;
3696 case -NFS4ERR_OLD_STATEID:
3697 /* Did we race with OPEN? */
3698 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3699 state))
3700 goto out_restart;
3701 goto out_release;
3702 case -NFS4ERR_ADMIN_REVOKED:
3703 case -NFS4ERR_STALE_STATEID:
3704 case -NFS4ERR_EXPIRED:
3705 nfs4_free_revoked_stateid(server,
3706 &calldata->arg.stateid,
3707 task->tk_msg.rpc_cred);
3708 fallthrough;
3709 case -NFS4ERR_BAD_STATEID:
3710 if (calldata->arg.fmode == 0)
3711 break;
3712 fallthrough;
3713 default:
3714 task->tk_status = nfs4_async_handle_exception(task,
3715 server, task->tk_status, &exception);
3716 calldata->retrans = exception.retrans;
3717 if (exception.retry)
3718 goto out_restart;
3719 }
3720 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3721 res_stateid, calldata->arg.fmode);
3722 out_release:
3723 task->tk_status = 0;
3724 nfs_release_seqid(calldata->arg.seqid);
3725 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3726 dprintk("%s: ret = %d\n", __func__, task->tk_status);
3727 return;
3728 out_restart:
3729 task->tk_status = 0;
3730 rpc_restart_call_prepare(task);
3731 goto out_release;
3732 }
3733
nfs4_close_prepare(struct rpc_task * task,void * data)3734 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3735 {
3736 struct nfs4_closedata *calldata = data;
3737 struct nfs4_state *state = calldata->state;
3738 struct inode *inode = calldata->inode;
3739 struct nfs_server *server = NFS_SERVER(inode);
3740 struct pnfs_layout_hdr *lo;
3741 bool is_rdonly, is_wronly, is_rdwr;
3742 int call_close = 0;
3743
3744 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3745 goto out_wait;
3746
3747 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3748 spin_lock(&state->owner->so_lock);
3749 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3750 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3751 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3752 /* Calculate the change in open mode */
3753 calldata->arg.fmode = 0;
3754 if (state->n_rdwr == 0) {
3755 if (state->n_rdonly == 0)
3756 call_close |= is_rdonly;
3757 else if (is_rdonly)
3758 calldata->arg.fmode |= FMODE_READ;
3759 if (state->n_wronly == 0)
3760 call_close |= is_wronly;
3761 else if (is_wronly)
3762 calldata->arg.fmode |= FMODE_WRITE;
3763 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3764 call_close |= is_rdwr;
3765 } else if (is_rdwr)
3766 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3767
3768 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3769 if (!nfs4_valid_open_stateid(state))
3770 call_close = 0;
3771 spin_unlock(&state->owner->so_lock);
3772
3773 if (!call_close) {
3774 /* Note: exit _without_ calling nfs4_close_done */
3775 goto out_no_action;
3776 }
3777
3778 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3779 nfs_release_seqid(calldata->arg.seqid);
3780 goto out_wait;
3781 }
3782
3783 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3784 if (lo && !pnfs_layout_is_valid(lo)) {
3785 calldata->arg.lr_args = NULL;
3786 calldata->res.lr_res = NULL;
3787 }
3788
3789 if (calldata->arg.fmode == 0)
3790 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3791
3792 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3793 /* Close-to-open cache consistency revalidation */
3794 if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
3795 nfs4_bitmask_set(calldata->arg.bitmask_store,
3796 server->cache_consistency_bitmask,
3797 inode, 0);
3798 calldata->arg.bitmask = calldata->arg.bitmask_store;
3799 } else
3800 calldata->arg.bitmask = NULL;
3801 }
3802
3803 calldata->arg.share_access =
3804 nfs4_fmode_to_share_access(calldata->arg.fmode);
3805
3806 if (calldata->res.fattr == NULL)
3807 calldata->arg.bitmask = NULL;
3808 else if (calldata->arg.bitmask == NULL)
3809 calldata->res.fattr = NULL;
3810 calldata->timestamp = jiffies;
3811 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3812 &calldata->arg.seq_args,
3813 &calldata->res.seq_res,
3814 task) != 0)
3815 nfs_release_seqid(calldata->arg.seqid);
3816 return;
3817 out_no_action:
3818 task->tk_action = NULL;
3819 out_wait:
3820 nfs4_sequence_done(task, &calldata->res.seq_res);
3821 }
3822
3823 static const struct rpc_call_ops nfs4_close_ops = {
3824 .rpc_call_prepare = nfs4_close_prepare,
3825 .rpc_call_done = nfs4_close_done,
3826 .rpc_release = nfs4_free_closedata,
3827 };
3828
3829 /*
3830 * It is possible for data to be read/written from a mem-mapped file
3831 * after the sys_close call (which hits the vfs layer as a flush).
3832 * This means that we can't safely call nfsv4 close on a file until
3833 * the inode is cleared. This in turn means that we are not good
3834 * NFSv4 citizens - we do not indicate to the server to update the file's
3835 * share state even when we are done with one of the three share
3836 * stateid's in the inode.
3837 *
3838 * NOTE: Caller must be holding the sp->so_owner semaphore!
3839 */
nfs4_do_close(struct nfs4_state * state,gfp_t gfp_mask,int wait)3840 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3841 {
3842 struct nfs_server *server = NFS_SERVER(state->inode);
3843 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3844 struct nfs4_closedata *calldata;
3845 struct nfs4_state_owner *sp = state->owner;
3846 struct rpc_task *task;
3847 struct rpc_message msg = {
3848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3849 .rpc_cred = state->owner->so_cred,
3850 };
3851 struct rpc_task_setup task_setup_data = {
3852 .rpc_client = server->client,
3853 .rpc_message = &msg,
3854 .callback_ops = &nfs4_close_ops,
3855 .workqueue = nfsiod_workqueue,
3856 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3857 };
3858 int status = -ENOMEM;
3859
3860 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3861 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3862
3863 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3864 &task_setup_data.rpc_client, &msg);
3865
3866 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3867 if (calldata == NULL)
3868 goto out;
3869 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3870 calldata->inode = state->inode;
3871 calldata->state = state;
3872 calldata->arg.fh = NFS_FH(state->inode);
3873 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3874 goto out_free_calldata;
3875 /* Serialization for the sequence id */
3876 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3877 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3878 if (IS_ERR(calldata->arg.seqid))
3879 goto out_free_calldata;
3880 nfs_fattr_init(&calldata->fattr);
3881 calldata->arg.fmode = 0;
3882 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3883 calldata->res.fattr = &calldata->fattr;
3884 calldata->res.seqid = calldata->arg.seqid;
3885 calldata->res.server = server;
3886 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3887 calldata->lr.roc = pnfs_roc(state->inode,
3888 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3889 if (calldata->lr.roc) {
3890 calldata->arg.lr_args = &calldata->lr.arg;
3891 calldata->res.lr_res = &calldata->lr.res;
3892 }
3893 nfs_sb_active(calldata->inode->i_sb);
3894
3895 msg.rpc_argp = &calldata->arg;
3896 msg.rpc_resp = &calldata->res;
3897 task_setup_data.callback_data = calldata;
3898 task = rpc_run_task(&task_setup_data);
3899 if (IS_ERR(task))
3900 return PTR_ERR(task);
3901 status = 0;
3902 if (wait)
3903 status = rpc_wait_for_completion_task(task);
3904 rpc_put_task(task);
3905 return status;
3906 out_free_calldata:
3907 kfree(calldata);
3908 out:
3909 nfs4_put_open_state(state);
3910 nfs4_put_state_owner(sp);
3911 return status;
3912 }
3913
3914 static struct inode *
nfs4_atomic_open(struct inode * dir,struct nfs_open_context * ctx,int open_flags,struct iattr * attr,int * opened)3915 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3916 int open_flags, struct iattr *attr, int *opened)
3917 {
3918 struct nfs4_state *state;
3919 struct nfs4_label l, *label;
3920
3921 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3922
3923 /* Protect against concurrent sillydeletes */
3924 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3925
3926 nfs4_label_release_security(label);
3927
3928 if (IS_ERR(state))
3929 return ERR_CAST(state);
3930 return state->inode;
3931 }
3932
nfs4_close_context(struct nfs_open_context * ctx,int is_sync)3933 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3934 {
3935 struct dentry *dentry = ctx->dentry;
3936 if (ctx->state == NULL)
3937 return;
3938 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
3939 nfs4_inode_set_return_delegation_on_close(d_inode(dentry));
3940 if (is_sync)
3941 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3942 else
3943 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3944 }
3945
3946 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3947 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3948 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_OPEN_ARGUMENTS - 1UL)
3949
3950 #define FATTR4_WORD2_NFS42_TIME_DELEG_MASK \
3951 (FATTR4_WORD2_TIME_DELEG_MODIFY|FATTR4_WORD2_TIME_DELEG_ACCESS)
nfs4_server_delegtime_capable(struct nfs4_server_caps_res * res)3952 static bool nfs4_server_delegtime_capable(struct nfs4_server_caps_res *res)
3953 {
3954 u32 share_access_want = res->open_caps.oa_share_access_want[0];
3955 u32 attr_bitmask = res->attr_bitmask[2];
3956
3957 return (share_access_want & NFS4_SHARE_WANT_DELEG_TIMESTAMPS) &&
3958 ((attr_bitmask & FATTR4_WORD2_NFS42_TIME_DELEG_MASK) ==
3959 FATTR4_WORD2_NFS42_TIME_DELEG_MASK);
3960 }
3961
_nfs4_server_capabilities(struct nfs_server * server,struct nfs_fh * fhandle)3962 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3963 {
3964 u32 minorversion = server->nfs_client->cl_minorversion;
3965 u32 bitmask[3] = {
3966 [0] = FATTR4_WORD0_SUPPORTED_ATTRS,
3967 };
3968 struct nfs4_server_caps_arg args = {
3969 .fhandle = fhandle,
3970 .bitmask = bitmask,
3971 };
3972 struct nfs4_server_caps_res res = {};
3973 struct rpc_message msg = {
3974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3975 .rpc_argp = &args,
3976 .rpc_resp = &res,
3977 };
3978 int status;
3979 int i;
3980
3981 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3982 FATTR4_WORD0_FH_EXPIRE_TYPE |
3983 FATTR4_WORD0_LINK_SUPPORT |
3984 FATTR4_WORD0_SYMLINK_SUPPORT |
3985 FATTR4_WORD0_ACLSUPPORT |
3986 FATTR4_WORD0_CASE_INSENSITIVE |
3987 FATTR4_WORD0_CASE_PRESERVING;
3988 if (minorversion)
3989 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3990 if (minorversion > 1)
3991 bitmask[2] |= FATTR4_WORD2_OPEN_ARGUMENTS;
3992
3993 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3994 if (status == 0) {
3995 bitmask[0] = (FATTR4_WORD0_SUPPORTED_ATTRS |
3996 FATTR4_WORD0_FH_EXPIRE_TYPE |
3997 FATTR4_WORD0_LINK_SUPPORT |
3998 FATTR4_WORD0_SYMLINK_SUPPORT |
3999 FATTR4_WORD0_ACLSUPPORT |
4000 FATTR4_WORD0_CASE_INSENSITIVE |
4001 FATTR4_WORD0_CASE_PRESERVING) &
4002 res.attr_bitmask[0];
4003 /* Sanity check the server answers */
4004 switch (minorversion) {
4005 case 0:
4006 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
4007 res.attr_bitmask[2] = 0;
4008 break;
4009 case 1:
4010 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
4011 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT &
4012 res.attr_bitmask[2];
4013 break;
4014 case 2:
4015 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
4016 bitmask[2] = (FATTR4_WORD2_SUPPATTR_EXCLCREAT |
4017 FATTR4_WORD2_OPEN_ARGUMENTS) &
4018 res.attr_bitmask[2];
4019 }
4020 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
4021 server->caps &=
4022 ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS | NFS_CAP_SYMLINKS |
4023 NFS_CAP_SECURITY_LABEL | NFS_CAP_FS_LOCATIONS |
4024 NFS_CAP_OPEN_XOR | NFS_CAP_DELEGTIME);
4025 server->fattr_valid = NFS_ATTR_FATTR_V4;
4026 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
4027 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4028 server->caps |= NFS_CAP_ACLS;
4029 if (res.has_links != 0)
4030 server->caps |= NFS_CAP_HARDLINKS;
4031 if (res.has_symlinks != 0)
4032 server->caps |= NFS_CAP_SYMLINKS;
4033 if (res.case_insensitive)
4034 server->caps |= NFS_CAP_CASE_INSENSITIVE;
4035 if (res.case_preserving)
4036 server->caps |= NFS_CAP_CASE_PRESERVING;
4037 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4038 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
4039 server->caps |= NFS_CAP_SECURITY_LABEL;
4040 #endif
4041 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
4042 server->caps |= NFS_CAP_FS_LOCATIONS;
4043 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
4044 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
4045 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
4046 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
4047 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
4048 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
4049 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
4050 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
4051 NFS_ATTR_FATTR_OWNER_NAME);
4052 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
4053 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
4054 NFS_ATTR_FATTR_GROUP_NAME);
4055 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
4056 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
4057 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
4058 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
4059 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
4060 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
4061 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
4062 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
4063 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
4064 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
4065 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_CREATE))
4066 server->fattr_valid &= ~NFS_ATTR_FATTR_BTIME;
4067 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
4068 sizeof(server->attr_bitmask));
4069 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
4070
4071 if (res.open_caps.oa_share_access_want[0] &
4072 NFS4_SHARE_WANT_OPEN_XOR_DELEGATION)
4073 server->caps |= NFS_CAP_OPEN_XOR;
4074 if (nfs4_server_delegtime_capable(&res))
4075 server->caps |= NFS_CAP_DELEGTIME;
4076
4077 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
4078 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
4079 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
4080 server->cache_consistency_bitmask[2] = 0;
4081
4082 /* Avoid a regression due to buggy server */
4083 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
4084 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
4085 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
4086 sizeof(server->exclcreat_bitmask));
4087
4088 server->acl_bitmask = res.acl_bitmask;
4089 server->fh_expire_type = res.fh_expire_type;
4090 }
4091
4092 return status;
4093 }
4094
nfs4_server_capabilities(struct nfs_server * server,struct nfs_fh * fhandle)4095 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
4096 {
4097 struct nfs4_exception exception = {
4098 .interruptible = true,
4099 };
4100 int err;
4101
4102 do {
4103 err = nfs4_handle_exception(server,
4104 _nfs4_server_capabilities(server, fhandle),
4105 &exception);
4106 } while (exception.retry);
4107 return err;
4108 }
4109
test_fs_location_for_trunking(struct nfs4_fs_location * location,struct nfs_client * clp,struct nfs_server * server)4110 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
4111 struct nfs_client *clp,
4112 struct nfs_server *server)
4113 {
4114 int i;
4115
4116 for (i = 0; i < location->nservers; i++) {
4117 struct nfs4_string *srv_loc = &location->servers[i];
4118 struct sockaddr_storage addr;
4119 size_t addrlen;
4120 struct xprt_create xprt_args = {
4121 .ident = 0,
4122 .net = clp->cl_net,
4123 };
4124 struct nfs4_add_xprt_data xprtdata = {
4125 .clp = clp,
4126 };
4127 struct rpc_add_xprt_test rpcdata = {
4128 .add_xprt_test = clp->cl_mvops->session_trunk,
4129 .data = &xprtdata,
4130 };
4131 char *servername = NULL;
4132
4133 if (!srv_loc->len)
4134 continue;
4135
4136 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
4137 &addr, sizeof(addr),
4138 clp->cl_net, server->port);
4139 if (!addrlen)
4140 return;
4141 xprt_args.dstaddr = (struct sockaddr *)&addr;
4142 xprt_args.addrlen = addrlen;
4143 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
4144 if (!servername)
4145 return;
4146 memcpy(servername, srv_loc->data, srv_loc->len);
4147 servername[srv_loc->len] = '\0';
4148 xprt_args.servername = servername;
4149
4150 xprtdata.cred = nfs4_get_clid_cred(clp);
4151 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
4152 rpc_clnt_setup_test_and_add_xprt,
4153 &rpcdata);
4154 if (xprtdata.cred)
4155 put_cred(xprtdata.cred);
4156 kfree(servername);
4157 }
4158 }
4159
_is_same_nfs4_pathname(struct nfs4_pathname * path1,struct nfs4_pathname * path2)4160 static bool _is_same_nfs4_pathname(struct nfs4_pathname *path1,
4161 struct nfs4_pathname *path2)
4162 {
4163 int i;
4164
4165 if (path1->ncomponents != path2->ncomponents)
4166 return false;
4167 for (i = 0; i < path1->ncomponents; i++) {
4168 if (path1->components[i].len != path2->components[i].len)
4169 return false;
4170 if (memcmp(path1->components[i].data, path2->components[i].data,
4171 path1->components[i].len))
4172 return false;
4173 }
4174 return true;
4175 }
4176
_nfs4_discover_trunking(struct nfs_server * server,struct nfs_fh * fhandle)4177 static int _nfs4_discover_trunking(struct nfs_server *server,
4178 struct nfs_fh *fhandle)
4179 {
4180 struct nfs4_fs_locations *locations = NULL;
4181 struct page *page;
4182 const struct cred *cred;
4183 struct nfs_client *clp = server->nfs_client;
4184 const struct nfs4_state_maintenance_ops *ops =
4185 clp->cl_mvops->state_renewal_ops;
4186 int status = -ENOMEM, i;
4187
4188 cred = ops->get_state_renewal_cred(clp);
4189 if (cred == NULL) {
4190 cred = nfs4_get_clid_cred(clp);
4191 if (cred == NULL)
4192 return -ENOKEY;
4193 }
4194
4195 page = alloc_page(GFP_KERNEL);
4196 if (!page)
4197 goto out_put_cred;
4198 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4199 if (!locations)
4200 goto out_free;
4201 locations->fattr = nfs_alloc_fattr();
4202 if (!locations->fattr)
4203 goto out_free_2;
4204
4205 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4206 cred);
4207 if (status)
4208 goto out_free_3;
4209
4210 for (i = 0; i < locations->nlocations; i++) {
4211 if (!_is_same_nfs4_pathname(&locations->fs_path,
4212 &locations->locations[i].rootpath))
4213 continue;
4214 test_fs_location_for_trunking(&locations->locations[i], clp,
4215 server);
4216 }
4217 out_free_3:
4218 kfree(locations->fattr);
4219 out_free_2:
4220 kfree(locations);
4221 out_free:
4222 __free_page(page);
4223 out_put_cred:
4224 put_cred(cred);
4225 return status;
4226 }
4227
nfs4_discover_trunking(struct nfs_server * server,struct nfs_fh * fhandle)4228 static int nfs4_discover_trunking(struct nfs_server *server,
4229 struct nfs_fh *fhandle)
4230 {
4231 struct nfs4_exception exception = {
4232 .interruptible = true,
4233 };
4234 struct nfs_client *clp = server->nfs_client;
4235 int err = 0;
4236
4237 if (!nfs4_has_session(clp))
4238 goto out;
4239 do {
4240 err = nfs4_handle_exception(server,
4241 _nfs4_discover_trunking(server, fhandle),
4242 &exception);
4243 } while (exception.retry);
4244 out:
4245 return err;
4246 }
4247
_nfs4_lookup_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4248 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4249 struct nfs_fattr *fattr)
4250 {
4251 u32 bitmask[3] = {
4252 [0] = FATTR4_WORD0_TYPE | FATTR4_WORD0_CHANGE |
4253 FATTR4_WORD0_SIZE | FATTR4_WORD0_FSID,
4254 };
4255 struct nfs4_lookup_root_arg args = {
4256 .bitmask = bitmask,
4257 };
4258 struct nfs4_lookup_res res = {
4259 .server = server,
4260 .fattr = fattr,
4261 .fh = fhandle,
4262 };
4263 struct rpc_message msg = {
4264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4265 .rpc_argp = &args,
4266 .rpc_resp = &res,
4267 };
4268
4269 nfs_fattr_init(fattr);
4270 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4271 }
4272
nfs4_lookup_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4273 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4274 struct nfs_fattr *fattr)
4275 {
4276 struct nfs4_exception exception = {
4277 .interruptible = true,
4278 };
4279 int err;
4280 do {
4281 err = _nfs4_lookup_root(server, fhandle, fattr);
4282 trace_nfs4_lookup_root(server, fhandle, fattr, err);
4283 switch (err) {
4284 case 0:
4285 case -NFS4ERR_WRONGSEC:
4286 goto out;
4287 default:
4288 err = nfs4_handle_exception(server, err, &exception);
4289 }
4290 } while (exception.retry);
4291 out:
4292 return err;
4293 }
4294
nfs4_lookup_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,rpc_authflavor_t flavor)4295 static int nfs4_lookup_root_sec(struct nfs_server *server,
4296 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4297 rpc_authflavor_t flavor)
4298 {
4299 struct rpc_auth_create_args auth_args = {
4300 .pseudoflavor = flavor,
4301 };
4302 struct rpc_auth *auth;
4303
4304 auth = rpcauth_create(&auth_args, server->client);
4305 if (IS_ERR(auth))
4306 return -EACCES;
4307 return nfs4_lookup_root(server, fhandle, fattr);
4308 }
4309
4310 /*
4311 * Retry pseudoroot lookup with various security flavors. We do this when:
4312 *
4313 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4314 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4315 *
4316 * Returns zero on success, or a negative NFS4ERR value, or a
4317 * negative errno value.
4318 */
nfs4_find_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4319 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4320 struct nfs_fattr *fattr)
4321 {
4322 /* Per 3530bis 15.33.5 */
4323 static const rpc_authflavor_t flav_array[] = {
4324 RPC_AUTH_GSS_KRB5P,
4325 RPC_AUTH_GSS_KRB5I,
4326 RPC_AUTH_GSS_KRB5,
4327 RPC_AUTH_UNIX, /* courtesy */
4328 RPC_AUTH_NULL,
4329 };
4330 int status = -EPERM;
4331 size_t i;
4332
4333 if (server->auth_info.flavor_len > 0) {
4334 /* try each flavor specified by user */
4335 for (i = 0; i < server->auth_info.flavor_len; i++) {
4336 status = nfs4_lookup_root_sec(
4337 server, fhandle, fattr,
4338 server->auth_info.flavors[i]);
4339 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4340 continue;
4341 break;
4342 }
4343 } else {
4344 /* no flavors specified by user, try default list */
4345 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4346 status = nfs4_lookup_root_sec(server, fhandle, fattr,
4347 flav_array[i]);
4348 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4349 continue;
4350 break;
4351 }
4352 }
4353
4354 /*
4355 * -EACCES could mean that the user doesn't have correct permissions
4356 * to access the mount. It could also mean that we tried to mount
4357 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4358 * existing mount programs don't handle -EACCES very well so it should
4359 * be mapped to -EPERM instead.
4360 */
4361 if (status == -EACCES)
4362 status = -EPERM;
4363 return status;
4364 }
4365
4366 /**
4367 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4368 * @server: initialized nfs_server handle
4369 * @fhandle: we fill in the pseudo-fs root file handle
4370 * @fattr: we fill in a bare bones struct fattr
4371 * @auth_probe: probe the auth flavours
4372 *
4373 * Returns zero on success, or a negative errno.
4374 */
nfs4_proc_get_rootfh(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,bool auth_probe)4375 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4376 struct nfs_fattr *fattr, bool auth_probe)
4377 {
4378 int status = 0;
4379
4380 if (!auth_probe)
4381 status = nfs4_lookup_root(server, fhandle, fattr);
4382
4383 if (auth_probe || status == NFS4ERR_WRONGSEC)
4384 status = server->nfs_client->cl_mvops->find_root_sec(
4385 server, fhandle, fattr);
4386
4387 return nfs4_map_errors(status);
4388 }
4389
nfs4_proc_get_root(struct nfs_server * server,struct nfs_fh * mntfh,struct nfs_fsinfo * info)4390 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4391 struct nfs_fsinfo *info)
4392 {
4393 int error;
4394 struct nfs_fattr *fattr = info->fattr;
4395
4396 error = nfs4_server_capabilities(server, mntfh);
4397 if (error < 0) {
4398 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4399 return error;
4400 }
4401
4402 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4403 if (error < 0) {
4404 dprintk("nfs4_get_root: getattr error = %d\n", -error);
4405 goto out;
4406 }
4407
4408 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4409 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4410 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4411
4412 out:
4413 return error;
4414 }
4415
4416 /*
4417 * Get locations and (maybe) other attributes of a referral.
4418 * Note that we'll actually follow the referral later when
4419 * we detect fsid mismatch in inode revalidation
4420 */
nfs4_get_referral(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs_fattr * fattr,struct nfs_fh * fhandle)4421 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4422 const struct qstr *name, struct nfs_fattr *fattr,
4423 struct nfs_fh *fhandle)
4424 {
4425 int status = -ENOMEM;
4426 struct page *page = NULL;
4427 struct nfs4_fs_locations *locations = NULL;
4428
4429 page = alloc_page(GFP_KERNEL);
4430 if (page == NULL)
4431 goto out;
4432 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4433 if (locations == NULL)
4434 goto out;
4435
4436 locations->fattr = fattr;
4437
4438 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4439 if (status != 0)
4440 goto out;
4441
4442 /*
4443 * If the fsid didn't change, this is a migration event, not a
4444 * referral. Cause us to drop into the exception handler, which
4445 * will kick off migration recovery.
4446 */
4447 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4448 dprintk("%s: server did not return a different fsid for"
4449 " a referral at %s\n", __func__, name->name);
4450 status = -NFS4ERR_MOVED;
4451 goto out;
4452 }
4453 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4454 nfs_fixup_referral_attributes(fattr);
4455 memset(fhandle, 0, sizeof(struct nfs_fh));
4456 out:
4457 if (page)
4458 __free_page(page);
4459 kfree(locations);
4460 return status;
4461 }
4462
_nfs4_proc_getattr(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct inode * inode)4463 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4464 struct nfs_fattr *fattr, struct inode *inode)
4465 {
4466 __u32 bitmask[NFS4_BITMASK_SZ];
4467 struct nfs4_getattr_arg args = {
4468 .fh = fhandle,
4469 .bitmask = bitmask,
4470 };
4471 struct nfs4_getattr_res res = {
4472 .fattr = fattr,
4473 .server = server,
4474 };
4475 struct rpc_message msg = {
4476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4477 .rpc_argp = &args,
4478 .rpc_resp = &res,
4479 };
4480 unsigned short task_flags = 0;
4481
4482 if (nfs4_has_session(server->nfs_client))
4483 task_flags = RPC_TASK_MOVEABLE;
4484
4485 /* Is this is an attribute revalidation, subject to softreval? */
4486 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4487 task_flags |= RPC_TASK_TIMEOUT;
4488
4489 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4490 nfs_fattr_init(fattr);
4491 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4492 return nfs4_do_call_sync(server->client, server, &msg,
4493 &args.seq_args, &res.seq_res, task_flags);
4494 }
4495
nfs4_proc_getattr(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct inode * inode)4496 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4497 struct nfs_fattr *fattr, struct inode *inode)
4498 {
4499 struct nfs4_exception exception = {
4500 .interruptible = true,
4501 };
4502 int err;
4503 do {
4504 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4505 trace_nfs4_getattr(server, fhandle, fattr, err);
4506 err = nfs4_handle_exception(server, err,
4507 &exception);
4508 } while (exception.retry);
4509 return err;
4510 }
4511
4512 /*
4513 * The file is not closed if it is opened due to the a request to change
4514 * the size of the file. The open call will not be needed once the
4515 * VFS layer lookup-intents are implemented.
4516 *
4517 * Close is called when the inode is destroyed.
4518 * If we haven't opened the file for O_WRONLY, we
4519 * need to in the size_change case to obtain a stateid.
4520 *
4521 * Got race?
4522 * Because OPEN is always done by name in nfsv4, it is
4523 * possible that we opened a different file by the same
4524 * name. We can recognize this race condition, but we
4525 * can't do anything about it besides returning an error.
4526 *
4527 * This will be fixed with VFS changes (lookup-intent).
4528 */
4529 static int
nfs4_proc_setattr(struct dentry * dentry,struct nfs_fattr * fattr,struct iattr * sattr)4530 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4531 struct iattr *sattr)
4532 {
4533 struct inode *inode = d_inode(dentry);
4534 const struct cred *cred = NULL;
4535 struct nfs_open_context *ctx = NULL;
4536 int status;
4537
4538 if (pnfs_ld_layoutret_on_setattr(inode) &&
4539 sattr->ia_valid & ATTR_SIZE &&
4540 sattr->ia_size < i_size_read(inode))
4541 pnfs_commit_and_return_layout(inode);
4542
4543 nfs_fattr_init(fattr);
4544
4545 /* Deal with open(O_TRUNC) */
4546 if (sattr->ia_valid & ATTR_OPEN)
4547 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4548
4549 /* Optimization: if the end result is no change, don't RPC */
4550 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4551 return 0;
4552
4553 /* Search for an existing open(O_WRITE) file */
4554 if (sattr->ia_valid & ATTR_FILE) {
4555
4556 ctx = nfs_file_open_context(sattr->ia_file);
4557 if (ctx)
4558 cred = ctx->cred;
4559 }
4560
4561 /* Return any delegations if we're going to change ACLs */
4562 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4563 nfs4_inode_make_writeable(inode);
4564
4565 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4566 if (status == 0) {
4567 nfs_setattr_update_inode(inode, sattr, fattr);
4568 nfs_setsecurity(inode, fattr);
4569 }
4570 return status;
4571 }
4572
_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)4573 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4574 struct dentry *dentry, const struct qstr *name,
4575 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4576 {
4577 struct nfs_server *server = NFS_SERVER(dir);
4578 int status;
4579 struct nfs4_lookup_arg args = {
4580 .bitmask = server->attr_bitmask,
4581 .dir_fh = NFS_FH(dir),
4582 .name = name,
4583 };
4584 struct nfs4_lookup_res res = {
4585 .server = server,
4586 .fattr = fattr,
4587 .fh = fhandle,
4588 };
4589 struct rpc_message msg = {
4590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4591 .rpc_argp = &args,
4592 .rpc_resp = &res,
4593 };
4594 unsigned short task_flags = 0;
4595
4596 if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4597 task_flags = RPC_TASK_MOVEABLE;
4598
4599 /* Is this is an attribute revalidation, subject to softreval? */
4600 if (nfs_lookup_is_soft_revalidate(dentry))
4601 task_flags |= RPC_TASK_TIMEOUT;
4602
4603 args.bitmask = nfs4_bitmask(server, fattr->label);
4604
4605 nfs_fattr_init(fattr);
4606
4607 dprintk("NFS call lookup %pd2\n", dentry);
4608 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4609 status = nfs4_do_call_sync(clnt, server, &msg,
4610 &args.seq_args, &res.seq_res, task_flags);
4611 dprintk("NFS reply lookup: %d\n", status);
4612 return status;
4613 }
4614
nfs_fixup_secinfo_attributes(struct nfs_fattr * fattr)4615 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4616 {
4617 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4618 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4619 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4620 fattr->nlink = 2;
4621 }
4622
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)4623 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4624 struct dentry *dentry, const struct qstr *name,
4625 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4626 {
4627 struct nfs4_exception exception = {
4628 .interruptible = true,
4629 };
4630 struct rpc_clnt *client = *clnt;
4631 int err;
4632 do {
4633 err = _nfs4_proc_lookup(client, dir, dentry, name, fhandle, fattr);
4634 trace_nfs4_lookup(dir, name, err);
4635 switch (err) {
4636 case -NFS4ERR_BADNAME:
4637 err = -ENOENT;
4638 goto out;
4639 case -NFS4ERR_MOVED:
4640 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4641 if (err == -NFS4ERR_MOVED)
4642 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4643 goto out;
4644 case -NFS4ERR_WRONGSEC:
4645 err = -EPERM;
4646 if (client != *clnt)
4647 goto out;
4648 client = nfs4_negotiate_security(client, dir, name);
4649 if (IS_ERR(client))
4650 return PTR_ERR(client);
4651
4652 exception.retry = 1;
4653 break;
4654 default:
4655 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4656 }
4657 } while (exception.retry);
4658
4659 out:
4660 if (err == 0)
4661 *clnt = client;
4662 else if (client != *clnt)
4663 rpc_shutdown_client(client);
4664
4665 return err;
4666 }
4667
nfs4_proc_lookup(struct inode * dir,struct dentry * dentry,const struct qstr * name,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4668 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry, const struct qstr *name,
4669 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4670 {
4671 int status;
4672 struct rpc_clnt *client = NFS_CLIENT(dir);
4673
4674 status = nfs4_proc_lookup_common(&client, dir, dentry, name, fhandle, fattr);
4675 if (client != NFS_CLIENT(dir)) {
4676 rpc_shutdown_client(client);
4677 nfs_fixup_secinfo_attributes(fattr);
4678 }
4679 return status;
4680 }
4681
4682 struct rpc_clnt *
nfs4_proc_lookup_mountpoint(struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4683 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4684 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4685 {
4686 struct rpc_clnt *client = NFS_CLIENT(dir);
4687 int status;
4688
4689 status = nfs4_proc_lookup_common(&client, dir, dentry, &dentry->d_name,
4690 fhandle, fattr);
4691 if (status < 0)
4692 return ERR_PTR(status);
4693 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4694 }
4695
_nfs4_proc_lookupp(struct inode * inode,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4696 static int _nfs4_proc_lookupp(struct inode *inode,
4697 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4698 {
4699 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4700 struct nfs_server *server = NFS_SERVER(inode);
4701 int status;
4702 struct nfs4_lookupp_arg args = {
4703 .bitmask = server->attr_bitmask,
4704 .fh = NFS_FH(inode),
4705 };
4706 struct nfs4_lookupp_res res = {
4707 .server = server,
4708 .fattr = fattr,
4709 .fh = fhandle,
4710 };
4711 struct rpc_message msg = {
4712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4713 .rpc_argp = &args,
4714 .rpc_resp = &res,
4715 };
4716 unsigned short task_flags = 0;
4717
4718 if (server->flags & NFS_MOUNT_SOFTREVAL)
4719 task_flags |= RPC_TASK_TIMEOUT;
4720 if (server->caps & NFS_CAP_MOVEABLE)
4721 task_flags |= RPC_TASK_MOVEABLE;
4722
4723 args.bitmask = nfs4_bitmask(server, fattr->label);
4724
4725 nfs_fattr_init(fattr);
4726 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4727
4728 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4729 status = nfs4_do_call_sync(clnt, server, &msg, &args.seq_args,
4730 &res.seq_res, task_flags);
4731 dprintk("NFS reply lookupp: %d\n", status);
4732 return status;
4733 }
4734
nfs4_proc_lookupp(struct inode * inode,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4735 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4736 struct nfs_fattr *fattr)
4737 {
4738 struct nfs4_exception exception = {
4739 .interruptible = true,
4740 };
4741 int err;
4742 do {
4743 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4744 trace_nfs4_lookupp(inode, err);
4745 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4746 &exception);
4747 } while (exception.retry);
4748 return err;
4749 }
4750
_nfs4_proc_access(struct inode * inode,struct nfs_access_entry * entry,const struct cred * cred)4751 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4752 const struct cred *cred)
4753 {
4754 struct nfs_server *server = NFS_SERVER(inode);
4755 struct nfs4_accessargs args = {
4756 .fh = NFS_FH(inode),
4757 .access = entry->mask,
4758 };
4759 struct nfs4_accessres res = {
4760 .server = server,
4761 };
4762 struct rpc_message msg = {
4763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4764 .rpc_argp = &args,
4765 .rpc_resp = &res,
4766 .rpc_cred = cred,
4767 };
4768 int status = 0;
4769
4770 if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
4771 res.fattr = nfs_alloc_fattr();
4772 if (res.fattr == NULL)
4773 return -ENOMEM;
4774 args.bitmask = server->cache_consistency_bitmask;
4775 }
4776 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4777 if (!status) {
4778 nfs_access_set_mask(entry, res.access);
4779 if (res.fattr)
4780 nfs_refresh_inode(inode, res.fattr);
4781 }
4782 nfs_free_fattr(res.fattr);
4783 return status;
4784 }
4785
nfs4_proc_access(struct inode * inode,struct nfs_access_entry * entry,const struct cred * cred)4786 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4787 const struct cred *cred)
4788 {
4789 struct nfs4_exception exception = {
4790 .interruptible = true,
4791 };
4792 int err;
4793 do {
4794 err = _nfs4_proc_access(inode, entry, cred);
4795 trace_nfs4_access(inode, err);
4796 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4797 &exception);
4798 } while (exception.retry);
4799 return err;
4800 }
4801
4802 /*
4803 * TODO: For the time being, we don't try to get any attributes
4804 * along with any of the zero-copy operations READ, READDIR,
4805 * READLINK, WRITE.
4806 *
4807 * In the case of the first three, we want to put the GETATTR
4808 * after the read-type operation -- this is because it is hard
4809 * to predict the length of a GETATTR response in v4, and thus
4810 * align the READ data correctly. This means that the GETATTR
4811 * may end up partially falling into the page cache, and we should
4812 * shift it into the 'tail' of the xdr_buf before processing.
4813 * To do this efficiently, we need to know the total length
4814 * of data received, which doesn't seem to be available outside
4815 * of the RPC layer.
4816 *
4817 * In the case of WRITE, we also want to put the GETATTR after
4818 * the operation -- in this case because we want to make sure
4819 * we get the post-operation mtime and size.
4820 *
4821 * Both of these changes to the XDR layer would in fact be quite
4822 * minor, but I decided to leave them for a subsequent patch.
4823 */
_nfs4_proc_readlink(struct inode * inode,struct page * page,unsigned int pgbase,unsigned int pglen)4824 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4825 unsigned int pgbase, unsigned int pglen)
4826 {
4827 struct nfs4_readlink args = {
4828 .fh = NFS_FH(inode),
4829 .pgbase = pgbase,
4830 .pglen = pglen,
4831 .pages = &page,
4832 };
4833 struct nfs4_readlink_res res;
4834 struct rpc_message msg = {
4835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4836 .rpc_argp = &args,
4837 .rpc_resp = &res,
4838 };
4839
4840 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4841 }
4842
nfs4_proc_readlink(struct inode * inode,struct page * page,unsigned int pgbase,unsigned int pglen)4843 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4844 unsigned int pgbase, unsigned int pglen)
4845 {
4846 struct nfs4_exception exception = {
4847 .interruptible = true,
4848 };
4849 int err;
4850 do {
4851 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4852 trace_nfs4_readlink(inode, err);
4853 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4854 &exception);
4855 } while (exception.retry);
4856 return err;
4857 }
4858
4859 /*
4860 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4861 */
4862 static int
nfs4_proc_create(struct inode * dir,struct dentry * dentry,struct iattr * sattr,int flags)4863 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4864 int flags)
4865 {
4866 struct nfs_server *server = NFS_SERVER(dir);
4867 struct nfs4_label l, *ilabel;
4868 struct nfs_open_context *ctx;
4869 struct nfs4_state *state;
4870 int status = 0;
4871
4872 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4873 if (IS_ERR(ctx))
4874 return PTR_ERR(ctx);
4875
4876 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4877
4878 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4879 sattr->ia_mode &= ~current_umask();
4880 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4881 if (IS_ERR(state)) {
4882 status = PTR_ERR(state);
4883 goto out;
4884 }
4885 out:
4886 nfs4_label_release_security(ilabel);
4887 put_nfs_open_context(ctx);
4888 return status;
4889 }
4890
4891 static int
_nfs4_proc_remove(struct inode * dir,const struct qstr * name,u32 ftype)4892 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4893 {
4894 struct nfs_server *server = NFS_SERVER(dir);
4895 struct nfs_removeargs args = {
4896 .fh = NFS_FH(dir),
4897 .name = *name,
4898 };
4899 struct nfs_removeres res = {
4900 .server = server,
4901 };
4902 struct rpc_message msg = {
4903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4904 .rpc_argp = &args,
4905 .rpc_resp = &res,
4906 };
4907 unsigned long timestamp = jiffies;
4908 int status;
4909
4910 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4911 if (status == 0) {
4912 spin_lock(&dir->i_lock);
4913 /* Removing a directory decrements nlink in the parent */
4914 if (ftype == NF4DIR && dir->i_nlink > 2)
4915 nfs4_dec_nlink_locked(dir);
4916 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4917 NFS_INO_INVALID_DATA);
4918 spin_unlock(&dir->i_lock);
4919 }
4920 return status;
4921 }
4922
nfs4_proc_remove(struct inode * dir,struct dentry * dentry)4923 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4924 {
4925 struct nfs4_exception exception = {
4926 .interruptible = true,
4927 };
4928 struct inode *inode = d_inode(dentry);
4929 int err;
4930
4931 if (inode) {
4932 if (inode->i_nlink == 1)
4933 nfs4_inode_return_delegation(inode);
4934 else
4935 nfs4_inode_make_writeable(inode);
4936 }
4937 do {
4938 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4939 trace_nfs4_remove(dir, &dentry->d_name, err);
4940 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4941 &exception);
4942 } while (exception.retry);
4943 return err;
4944 }
4945
nfs4_proc_rmdir(struct inode * dir,const struct qstr * name)4946 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4947 {
4948 struct nfs4_exception exception = {
4949 .interruptible = true,
4950 };
4951 int err;
4952
4953 do {
4954 err = _nfs4_proc_remove(dir, name, NF4DIR);
4955 trace_nfs4_remove(dir, name, err);
4956 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4957 &exception);
4958 } while (exception.retry);
4959 return err;
4960 }
4961
nfs4_proc_unlink_setup(struct rpc_message * msg,struct dentry * dentry,struct inode * inode)4962 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4963 struct dentry *dentry,
4964 struct inode *inode)
4965 {
4966 struct nfs_removeargs *args = msg->rpc_argp;
4967 struct nfs_removeres *res = msg->rpc_resp;
4968
4969 res->server = NFS_SB(dentry->d_sb);
4970 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4971 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4972
4973 nfs_fattr_init(res->dir_attr);
4974
4975 if (inode) {
4976 nfs4_inode_return_delegation(inode);
4977 nfs_d_prune_case_insensitive_aliases(inode);
4978 }
4979 }
4980
nfs4_proc_unlink_rpc_prepare(struct rpc_task * task,struct nfs_unlinkdata * data)4981 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4982 {
4983 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4984 &data->args.seq_args,
4985 &data->res.seq_res,
4986 task);
4987 }
4988
nfs4_proc_unlink_done(struct rpc_task * task,struct inode * dir)4989 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4990 {
4991 struct nfs_unlinkdata *data = task->tk_calldata;
4992 struct nfs_removeres *res = &data->res;
4993
4994 if (!nfs4_sequence_done(task, &res->seq_res))
4995 return 0;
4996 if (nfs4_async_handle_error(task, res->server, NULL,
4997 &data->timeout) == -EAGAIN)
4998 return 0;
4999 if (task->tk_status == 0)
5000 nfs4_update_changeattr(dir, &res->cinfo,
5001 res->dir_attr->time_start,
5002 NFS_INO_INVALID_DATA);
5003 return 1;
5004 }
5005
nfs4_proc_rename_setup(struct rpc_message * msg,struct dentry * old_dentry,struct dentry * new_dentry)5006 static void nfs4_proc_rename_setup(struct rpc_message *msg,
5007 struct dentry *old_dentry,
5008 struct dentry *new_dentry)
5009 {
5010 struct nfs_renameargs *arg = msg->rpc_argp;
5011 struct nfs_renameres *res = msg->rpc_resp;
5012 struct inode *old_inode = d_inode(old_dentry);
5013 struct inode *new_inode = d_inode(new_dentry);
5014
5015 if (old_inode)
5016 nfs4_inode_make_writeable(old_inode);
5017 if (new_inode)
5018 nfs4_inode_return_delegation(new_inode);
5019 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
5020 res->server = NFS_SB(old_dentry->d_sb);
5021 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
5022 }
5023
nfs4_proc_rename_rpc_prepare(struct rpc_task * task,struct nfs_renamedata * data)5024 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
5025 {
5026 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
5027 &data->args.seq_args,
5028 &data->res.seq_res,
5029 task);
5030 }
5031
nfs4_proc_rename_done(struct rpc_task * task,struct inode * old_dir,struct inode * new_dir)5032 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
5033 struct inode *new_dir)
5034 {
5035 struct nfs_renamedata *data = task->tk_calldata;
5036 struct nfs_renameres *res = &data->res;
5037
5038 if (!nfs4_sequence_done(task, &res->seq_res))
5039 return 0;
5040 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
5041 return 0;
5042
5043 if (task->tk_status == 0) {
5044 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
5045 if (new_dir != old_dir) {
5046 /* Note: If we moved a directory, nlink will change */
5047 nfs4_update_changeattr(old_dir, &res->old_cinfo,
5048 res->old_fattr->time_start,
5049 NFS_INO_INVALID_NLINK |
5050 NFS_INO_INVALID_DATA);
5051 nfs4_update_changeattr(new_dir, &res->new_cinfo,
5052 res->new_fattr->time_start,
5053 NFS_INO_INVALID_NLINK |
5054 NFS_INO_INVALID_DATA);
5055 } else
5056 nfs4_update_changeattr(old_dir, &res->old_cinfo,
5057 res->old_fattr->time_start,
5058 NFS_INO_INVALID_DATA);
5059 }
5060 return 1;
5061 }
5062
_nfs4_proc_link(struct inode * inode,struct inode * dir,const struct qstr * name)5063 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
5064 {
5065 struct nfs_server *server = NFS_SERVER(inode);
5066 __u32 bitmask[NFS4_BITMASK_SZ];
5067 struct nfs4_link_arg arg = {
5068 .fh = NFS_FH(inode),
5069 .dir_fh = NFS_FH(dir),
5070 .name = name,
5071 .bitmask = bitmask,
5072 };
5073 struct nfs4_link_res res = {
5074 .server = server,
5075 };
5076 struct rpc_message msg = {
5077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
5078 .rpc_argp = &arg,
5079 .rpc_resp = &res,
5080 };
5081 int status = -ENOMEM;
5082
5083 res.fattr = nfs_alloc_fattr_with_label(server);
5084 if (res.fattr == NULL)
5085 goto out;
5086
5087 nfs4_inode_make_writeable(inode);
5088 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label),
5089 inode,
5090 NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME);
5091 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5092 if (!status) {
5093 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
5094 NFS_INO_INVALID_DATA);
5095 nfs4_inc_nlink(inode);
5096 status = nfs_post_op_update_inode(inode, res.fattr);
5097 if (!status)
5098 nfs_setsecurity(inode, res.fattr);
5099 }
5100
5101 out:
5102 nfs_free_fattr(res.fattr);
5103 return status;
5104 }
5105
nfs4_proc_link(struct inode * inode,struct inode * dir,const struct qstr * name)5106 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
5107 {
5108 struct nfs4_exception exception = {
5109 .interruptible = true,
5110 };
5111 int err;
5112 do {
5113 err = nfs4_handle_exception(NFS_SERVER(inode),
5114 _nfs4_proc_link(inode, dir, name),
5115 &exception);
5116 } while (exception.retry);
5117 return err;
5118 }
5119
5120 struct nfs4_createdata {
5121 struct rpc_message msg;
5122 struct nfs4_create_arg arg;
5123 struct nfs4_create_res res;
5124 struct nfs_fh fh;
5125 struct nfs_fattr fattr;
5126 };
5127
nfs4_alloc_createdata(struct inode * dir,const struct qstr * name,struct iattr * sattr,u32 ftype)5128 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
5129 const struct qstr *name, struct iattr *sattr, u32 ftype)
5130 {
5131 struct nfs4_createdata *data;
5132
5133 data = kzalloc(sizeof(*data), GFP_KERNEL);
5134 if (data != NULL) {
5135 struct nfs_server *server = NFS_SERVER(dir);
5136
5137 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
5138 if (IS_ERR(data->fattr.label))
5139 goto out_free;
5140
5141 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
5142 data->msg.rpc_argp = &data->arg;
5143 data->msg.rpc_resp = &data->res;
5144 data->arg.dir_fh = NFS_FH(dir);
5145 data->arg.server = server;
5146 data->arg.name = name;
5147 data->arg.attrs = sattr;
5148 data->arg.ftype = ftype;
5149 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
5150 data->arg.umask = current_umask();
5151 data->res.server = server;
5152 data->res.fh = &data->fh;
5153 data->res.fattr = &data->fattr;
5154 nfs_fattr_init(data->res.fattr);
5155 }
5156 return data;
5157 out_free:
5158 kfree(data);
5159 return NULL;
5160 }
5161
nfs4_do_create(struct inode * dir,struct dentry * dentry,struct nfs4_createdata * data)5162 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
5163 {
5164 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
5165 &data->arg.seq_args, &data->res.seq_res, 1);
5166 if (status == 0) {
5167 spin_lock(&dir->i_lock);
5168 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5169 data->res.fattr->time_start,
5170 NFS_INO_INVALID_DATA);
5171 spin_unlock(&dir->i_lock);
5172 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5173 }
5174 return status;
5175 }
5176
nfs4_do_mkdir(struct inode * dir,struct dentry * dentry,struct nfs4_createdata * data,int * statusp)5177 static struct dentry *nfs4_do_mkdir(struct inode *dir, struct dentry *dentry,
5178 struct nfs4_createdata *data, int *statusp)
5179 {
5180 struct dentry *ret;
5181
5182 *statusp = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
5183 &data->arg.seq_args, &data->res.seq_res, 1);
5184
5185 if (*statusp)
5186 return NULL;
5187
5188 spin_lock(&dir->i_lock);
5189 /* Creating a directory bumps nlink in the parent */
5190 nfs4_inc_nlink_locked(dir);
5191 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5192 data->res.fattr->time_start,
5193 NFS_INO_INVALID_DATA);
5194 spin_unlock(&dir->i_lock);
5195 ret = nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr);
5196 if (!IS_ERR(ret))
5197 return ret;
5198 *statusp = PTR_ERR(ret);
5199 return NULL;
5200 }
5201
nfs4_free_createdata(struct nfs4_createdata * data)5202 static void nfs4_free_createdata(struct nfs4_createdata *data)
5203 {
5204 nfs4_label_free(data->fattr.label);
5205 kfree(data);
5206 }
5207
_nfs4_proc_symlink(struct inode * dir,struct dentry * dentry,struct folio * folio,unsigned int len,struct iattr * sattr,struct nfs4_label * label)5208 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5209 struct folio *folio, unsigned int len, struct iattr *sattr,
5210 struct nfs4_label *label)
5211 {
5212 struct page *page = &folio->page;
5213 struct nfs4_createdata *data;
5214 int status = -ENAMETOOLONG;
5215
5216 if (len > NFS4_MAXPATHLEN)
5217 goto out;
5218
5219 status = -ENOMEM;
5220 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5221 if (data == NULL)
5222 goto out;
5223
5224 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5225 data->arg.u.symlink.pages = &page;
5226 data->arg.u.symlink.len = len;
5227 data->arg.label = label;
5228
5229 status = nfs4_do_create(dir, dentry, data);
5230
5231 nfs4_free_createdata(data);
5232 out:
5233 return status;
5234 }
5235
nfs4_proc_symlink(struct inode * dir,struct dentry * dentry,struct folio * folio,unsigned int len,struct iattr * sattr)5236 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5237 struct folio *folio, unsigned int len, struct iattr *sattr)
5238 {
5239 struct nfs4_exception exception = {
5240 .interruptible = true,
5241 };
5242 struct nfs4_label l, *label;
5243 int err;
5244
5245 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5246
5247 do {
5248 err = _nfs4_proc_symlink(dir, dentry, folio, len, sattr, label);
5249 trace_nfs4_symlink(dir, &dentry->d_name, err);
5250 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5251 &exception);
5252 } while (exception.retry);
5253
5254 nfs4_label_release_security(label);
5255 return err;
5256 }
5257
_nfs4_proc_mkdir(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * label,int * statusp)5258 static struct dentry *_nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5259 struct iattr *sattr,
5260 struct nfs4_label *label, int *statusp)
5261 {
5262 struct nfs4_createdata *data;
5263 struct dentry *ret = NULL;
5264
5265 *statusp = -ENOMEM;
5266 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5267 if (data == NULL)
5268 goto out;
5269
5270 data->arg.label = label;
5271 ret = nfs4_do_mkdir(dir, dentry, data, statusp);
5272
5273 nfs4_free_createdata(data);
5274 out:
5275 return ret;
5276 }
5277
nfs4_proc_mkdir(struct inode * dir,struct dentry * dentry,struct iattr * sattr)5278 static struct dentry *nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5279 struct iattr *sattr)
5280 {
5281 struct nfs_server *server = NFS_SERVER(dir);
5282 struct nfs4_exception exception = {
5283 .interruptible = true,
5284 };
5285 struct nfs4_label l, *label;
5286 struct dentry *alias;
5287 int err;
5288
5289 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5290
5291 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5292 sattr->ia_mode &= ~current_umask();
5293 do {
5294 alias = _nfs4_proc_mkdir(dir, dentry, sattr, label, &err);
5295 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5296 if (err)
5297 alias = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
5298 err,
5299 &exception));
5300 } while (exception.retry);
5301 nfs4_label_release_security(label);
5302
5303 return alias;
5304 }
5305
_nfs4_proc_readdir(struct nfs_readdir_arg * nr_arg,struct nfs_readdir_res * nr_res)5306 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5307 struct nfs_readdir_res *nr_res)
5308 {
5309 struct inode *dir = d_inode(nr_arg->dentry);
5310 struct nfs_server *server = NFS_SERVER(dir);
5311 struct nfs4_readdir_arg args = {
5312 .fh = NFS_FH(dir),
5313 .pages = nr_arg->pages,
5314 .pgbase = 0,
5315 .count = nr_arg->page_len,
5316 .plus = nr_arg->plus,
5317 };
5318 struct nfs4_readdir_res res;
5319 struct rpc_message msg = {
5320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5321 .rpc_argp = &args,
5322 .rpc_resp = &res,
5323 .rpc_cred = nr_arg->cred,
5324 };
5325 int status;
5326
5327 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5328 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5329 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5330 args.bitmask = server->attr_bitmask_nl;
5331 else
5332 args.bitmask = server->attr_bitmask;
5333
5334 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5335 res.pgbase = args.pgbase;
5336 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5337 &res.seq_res, 0);
5338 if (status >= 0) {
5339 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5340 status += args.pgbase;
5341 }
5342
5343 nfs_invalidate_atime(dir);
5344
5345 dprintk("%s: returns %d\n", __func__, status);
5346 return status;
5347 }
5348
nfs4_proc_readdir(struct nfs_readdir_arg * arg,struct nfs_readdir_res * res)5349 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5350 struct nfs_readdir_res *res)
5351 {
5352 struct nfs4_exception exception = {
5353 .interruptible = true,
5354 };
5355 int err;
5356 do {
5357 err = _nfs4_proc_readdir(arg, res);
5358 trace_nfs4_readdir(d_inode(arg->dentry), err);
5359 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5360 err, &exception);
5361 } while (exception.retry);
5362 return err;
5363 }
5364
_nfs4_proc_mknod(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * label,dev_t rdev)5365 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5366 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5367 {
5368 struct nfs4_createdata *data;
5369 int mode = sattr->ia_mode;
5370 int status = -ENOMEM;
5371
5372 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5373 if (data == NULL)
5374 goto out;
5375
5376 if (S_ISFIFO(mode))
5377 data->arg.ftype = NF4FIFO;
5378 else if (S_ISBLK(mode)) {
5379 data->arg.ftype = NF4BLK;
5380 data->arg.u.device.specdata1 = MAJOR(rdev);
5381 data->arg.u.device.specdata2 = MINOR(rdev);
5382 }
5383 else if (S_ISCHR(mode)) {
5384 data->arg.ftype = NF4CHR;
5385 data->arg.u.device.specdata1 = MAJOR(rdev);
5386 data->arg.u.device.specdata2 = MINOR(rdev);
5387 } else if (!S_ISSOCK(mode)) {
5388 status = -EINVAL;
5389 goto out_free;
5390 }
5391
5392 data->arg.label = label;
5393 status = nfs4_do_create(dir, dentry, data);
5394 out_free:
5395 nfs4_free_createdata(data);
5396 out:
5397 return status;
5398 }
5399
nfs4_proc_mknod(struct inode * dir,struct dentry * dentry,struct iattr * sattr,dev_t rdev)5400 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5401 struct iattr *sattr, dev_t rdev)
5402 {
5403 struct nfs_server *server = NFS_SERVER(dir);
5404 struct nfs4_exception exception = {
5405 .interruptible = true,
5406 };
5407 struct nfs4_label l, *label;
5408 int err;
5409
5410 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5411
5412 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5413 sattr->ia_mode &= ~current_umask();
5414 do {
5415 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5416 trace_nfs4_mknod(dir, &dentry->d_name, err);
5417 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5418 &exception);
5419 } while (exception.retry);
5420
5421 nfs4_label_release_security(label);
5422
5423 return err;
5424 }
5425
_nfs4_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsstat * fsstat)5426 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5427 struct nfs_fsstat *fsstat)
5428 {
5429 struct nfs4_statfs_arg args = {
5430 .fh = fhandle,
5431 .bitmask = server->attr_bitmask,
5432 };
5433 struct nfs4_statfs_res res = {
5434 .fsstat = fsstat,
5435 };
5436 struct rpc_message msg = {
5437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5438 .rpc_argp = &args,
5439 .rpc_resp = &res,
5440 };
5441
5442 nfs_fattr_init(fsstat->fattr);
5443 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5444 }
5445
nfs4_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsstat * fsstat)5446 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5447 {
5448 struct nfs4_exception exception = {
5449 .interruptible = true,
5450 };
5451 int err;
5452 do {
5453 err = nfs4_handle_exception(server,
5454 _nfs4_proc_statfs(server, fhandle, fsstat),
5455 &exception);
5456 } while (exception.retry);
5457 return err;
5458 }
5459
_nfs4_do_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5460 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5461 struct nfs_fsinfo *fsinfo)
5462 {
5463 struct nfs4_fsinfo_arg args = {
5464 .fh = fhandle,
5465 .bitmask = server->attr_bitmask,
5466 };
5467 struct nfs4_fsinfo_res res = {
5468 .fsinfo = fsinfo,
5469 };
5470 struct rpc_message msg = {
5471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5472 .rpc_argp = &args,
5473 .rpc_resp = &res,
5474 };
5475
5476 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5477 }
5478
nfs4_do_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5479 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5480 {
5481 struct nfs4_exception exception = {
5482 .interruptible = true,
5483 };
5484 int err;
5485
5486 do {
5487 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5488 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5489 if (err == 0) {
5490 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5491 break;
5492 }
5493 err = nfs4_handle_exception(server, err, &exception);
5494 } while (exception.retry);
5495 return err;
5496 }
5497
nfs4_proc_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5498 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5499 {
5500 int error;
5501
5502 nfs_fattr_init(fsinfo->fattr);
5503 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5504 if (error == 0) {
5505 /* block layout checks this! */
5506 server->pnfs_blksize = fsinfo->blksize;
5507 set_pnfs_layoutdriver(server, fhandle, fsinfo);
5508 }
5509
5510 return error;
5511 }
5512
_nfs4_proc_pathconf(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_pathconf * pathconf)5513 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5514 struct nfs_pathconf *pathconf)
5515 {
5516 struct nfs4_pathconf_arg args = {
5517 .fh = fhandle,
5518 .bitmask = server->attr_bitmask,
5519 };
5520 struct nfs4_pathconf_res res = {
5521 .pathconf = pathconf,
5522 };
5523 struct rpc_message msg = {
5524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5525 .rpc_argp = &args,
5526 .rpc_resp = &res,
5527 };
5528
5529 /* None of the pathconf attributes are mandatory to implement */
5530 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5531 memset(pathconf, 0, sizeof(*pathconf));
5532 return 0;
5533 }
5534
5535 nfs_fattr_init(pathconf->fattr);
5536 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5537 }
5538
nfs4_proc_pathconf(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_pathconf * pathconf)5539 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5540 struct nfs_pathconf *pathconf)
5541 {
5542 struct nfs4_exception exception = {
5543 .interruptible = true,
5544 };
5545 int err;
5546
5547 do {
5548 err = nfs4_handle_exception(server,
5549 _nfs4_proc_pathconf(server, fhandle, pathconf),
5550 &exception);
5551 } while (exception.retry);
5552 return err;
5553 }
5554
nfs4_set_rw_stateid(nfs4_stateid * stateid,const struct nfs_open_context * ctx,const struct nfs_lock_context * l_ctx,fmode_t fmode)5555 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5556 const struct nfs_open_context *ctx,
5557 const struct nfs_lock_context *l_ctx,
5558 fmode_t fmode)
5559 {
5560 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5561 }
5562 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5563
nfs4_stateid_is_current(nfs4_stateid * stateid,const struct nfs_open_context * ctx,const struct nfs_lock_context * l_ctx,fmode_t fmode)5564 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5565 const struct nfs_open_context *ctx,
5566 const struct nfs_lock_context *l_ctx,
5567 fmode_t fmode)
5568 {
5569 nfs4_stateid _current_stateid;
5570
5571 /* If the current stateid represents a lost lock, then exit */
5572 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5573 return true;
5574 return nfs4_stateid_match(stateid, &_current_stateid);
5575 }
5576
nfs4_error_stateid_expired(int err)5577 static bool nfs4_error_stateid_expired(int err)
5578 {
5579 switch (err) {
5580 case -NFS4ERR_DELEG_REVOKED:
5581 case -NFS4ERR_ADMIN_REVOKED:
5582 case -NFS4ERR_BAD_STATEID:
5583 case -NFS4ERR_STALE_STATEID:
5584 case -NFS4ERR_OLD_STATEID:
5585 case -NFS4ERR_OPENMODE:
5586 case -NFS4ERR_EXPIRED:
5587 return true;
5588 }
5589 return false;
5590 }
5591
nfs4_read_done_cb(struct rpc_task * task,struct nfs_pgio_header * hdr)5592 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5593 {
5594 struct nfs_server *server = NFS_SERVER(hdr->inode);
5595
5596 trace_nfs4_read(hdr, task->tk_status);
5597 if (task->tk_status < 0) {
5598 struct nfs4_exception exception = {
5599 .inode = hdr->inode,
5600 .state = hdr->args.context->state,
5601 .stateid = &hdr->args.stateid,
5602 .retrans = hdr->retrans,
5603 };
5604 task->tk_status = nfs4_async_handle_exception(task,
5605 server, task->tk_status, &exception);
5606 hdr->retrans = exception.retrans;
5607 if (exception.retry) {
5608 rpc_restart_call_prepare(task);
5609 return -EAGAIN;
5610 }
5611 }
5612
5613 if (task->tk_status > 0)
5614 renew_lease(server, hdr->timestamp);
5615 return 0;
5616 }
5617
nfs4_read_stateid_changed(struct rpc_task * task,struct nfs_pgio_args * args)5618 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5619 struct nfs_pgio_args *args)
5620 {
5621
5622 if (!nfs4_error_stateid_expired(task->tk_status) ||
5623 nfs4_stateid_is_current(&args->stateid,
5624 args->context,
5625 args->lock_context,
5626 FMODE_READ))
5627 return false;
5628 rpc_restart_call_prepare(task);
5629 return true;
5630 }
5631
nfs4_read_plus_not_supported(struct rpc_task * task,struct nfs_pgio_header * hdr)5632 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5633 struct nfs_pgio_header *hdr)
5634 {
5635 struct nfs_server *server = NFS_SERVER(hdr->inode);
5636 struct rpc_message *msg = &task->tk_msg;
5637
5638 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5639 task->tk_status == -ENOTSUPP) {
5640 server->caps &= ~NFS_CAP_READ_PLUS;
5641 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5642 rpc_restart_call_prepare(task);
5643 return true;
5644 }
5645 return false;
5646 }
5647
nfs4_read_done(struct rpc_task * task,struct nfs_pgio_header * hdr)5648 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5649 {
5650 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5651 return -EAGAIN;
5652 if (nfs4_read_stateid_changed(task, &hdr->args))
5653 return -EAGAIN;
5654 if (nfs4_read_plus_not_supported(task, hdr))
5655 return -EAGAIN;
5656 if (task->tk_status > 0)
5657 nfs_invalidate_atime(hdr->inode);
5658 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5659 nfs4_read_done_cb(task, hdr);
5660 }
5661
5662 #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)5663 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5664 struct rpc_message *msg)
5665 {
5666 /* Note: We don't use READ_PLUS with pNFS yet */
5667 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5668 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5669 return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5670 }
5671 return false;
5672 }
5673 #else
nfs42_read_plus_support(struct nfs_pgio_header * hdr,struct rpc_message * msg)5674 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5675 struct rpc_message *msg)
5676 {
5677 return false;
5678 }
5679 #endif /* CONFIG_NFS_V4_2 */
5680
nfs4_proc_read_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg)5681 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5682 struct rpc_message *msg)
5683 {
5684 hdr->timestamp = jiffies;
5685 if (!hdr->pgio_done_cb)
5686 hdr->pgio_done_cb = nfs4_read_done_cb;
5687 if (!nfs42_read_plus_support(hdr, msg))
5688 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5689 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5690 }
5691
nfs4_proc_pgio_rpc_prepare(struct rpc_task * task,struct nfs_pgio_header * hdr)5692 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5693 struct nfs_pgio_header *hdr)
5694 {
5695 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5696 &hdr->args.seq_args,
5697 &hdr->res.seq_res,
5698 task))
5699 return 0;
5700 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5701 hdr->args.lock_context,
5702 hdr->rw_mode) == -EIO)
5703 return -EIO;
5704 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5705 return -EIO;
5706 return 0;
5707 }
5708
nfs4_write_done_cb(struct rpc_task * task,struct nfs_pgio_header * hdr)5709 static int nfs4_write_done_cb(struct rpc_task *task,
5710 struct nfs_pgio_header *hdr)
5711 {
5712 struct inode *inode = hdr->inode;
5713
5714 trace_nfs4_write(hdr, task->tk_status);
5715 if (task->tk_status < 0) {
5716 struct nfs4_exception exception = {
5717 .inode = hdr->inode,
5718 .state = hdr->args.context->state,
5719 .stateid = &hdr->args.stateid,
5720 .retrans = hdr->retrans,
5721 };
5722 task->tk_status = nfs4_async_handle_exception(task,
5723 NFS_SERVER(inode), task->tk_status,
5724 &exception);
5725 hdr->retrans = exception.retrans;
5726 if (exception.retry) {
5727 rpc_restart_call_prepare(task);
5728 return -EAGAIN;
5729 }
5730 }
5731 if (task->tk_status >= 0) {
5732 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5733 nfs_writeback_update_inode(hdr);
5734 }
5735 return 0;
5736 }
5737
nfs4_write_stateid_changed(struct rpc_task * task,struct nfs_pgio_args * args)5738 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5739 struct nfs_pgio_args *args)
5740 {
5741
5742 if (!nfs4_error_stateid_expired(task->tk_status) ||
5743 nfs4_stateid_is_current(&args->stateid,
5744 args->context,
5745 args->lock_context,
5746 FMODE_WRITE))
5747 return false;
5748 rpc_restart_call_prepare(task);
5749 return true;
5750 }
5751
nfs4_write_done(struct rpc_task * task,struct nfs_pgio_header * hdr)5752 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5753 {
5754 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5755 return -EAGAIN;
5756 if (nfs4_write_stateid_changed(task, &hdr->args))
5757 return -EAGAIN;
5758 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5759 nfs4_write_done_cb(task, hdr);
5760 }
5761
5762 static
nfs4_write_need_cache_consistency_data(struct nfs_pgio_header * hdr)5763 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5764 {
5765 /* Don't request attributes for pNFS or O_DIRECT writes */
5766 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5767 return false;
5768 /* Otherwise, request attributes if and only if we don't hold
5769 * a delegation
5770 */
5771 return nfs4_have_delegation(hdr->inode, FMODE_READ, 0) == 0;
5772 }
5773
nfs4_bitmask_set(__u32 bitmask[],const __u32 src[],struct inode * inode,unsigned long cache_validity)5774 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5775 struct inode *inode, unsigned long cache_validity)
5776 {
5777 struct nfs_server *server = NFS_SERVER(inode);
5778 unsigned int i;
5779
5780 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5781 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5782
5783 if (cache_validity & NFS_INO_INVALID_CHANGE)
5784 bitmask[0] |= FATTR4_WORD0_CHANGE;
5785 if (cache_validity & NFS_INO_INVALID_ATIME)
5786 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5787 if (cache_validity & NFS_INO_INVALID_MODE)
5788 bitmask[1] |= FATTR4_WORD1_MODE;
5789 if (cache_validity & NFS_INO_INVALID_OTHER)
5790 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5791 if (cache_validity & NFS_INO_INVALID_NLINK)
5792 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5793 if (cache_validity & NFS_INO_INVALID_CTIME)
5794 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5795 if (cache_validity & NFS_INO_INVALID_MTIME)
5796 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5797 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5798 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5799 if (cache_validity & NFS_INO_INVALID_BTIME)
5800 bitmask[1] |= FATTR4_WORD1_TIME_CREATE;
5801
5802 if (cache_validity & NFS_INO_INVALID_SIZE)
5803 bitmask[0] |= FATTR4_WORD0_SIZE;
5804
5805 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5806 bitmask[i] &= server->attr_bitmask[i];
5807 }
5808
nfs4_proc_write_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg,struct rpc_clnt ** clnt)5809 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5810 struct rpc_message *msg,
5811 struct rpc_clnt **clnt)
5812 {
5813 struct nfs_server *server = NFS_SERVER(hdr->inode);
5814
5815 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5816 hdr->args.bitmask = NULL;
5817 hdr->res.fattr = NULL;
5818 } else {
5819 nfs4_bitmask_set(hdr->args.bitmask_store,
5820 server->cache_consistency_bitmask,
5821 hdr->inode, NFS_INO_INVALID_BLOCKS);
5822 hdr->args.bitmask = hdr->args.bitmask_store;
5823 }
5824
5825 if (!hdr->pgio_done_cb)
5826 hdr->pgio_done_cb = nfs4_write_done_cb;
5827 hdr->res.server = server;
5828 hdr->timestamp = jiffies;
5829
5830 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5831 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5832 nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
5833 }
5834
nfs4_proc_commit_rpc_prepare(struct rpc_task * task,struct nfs_commit_data * data)5835 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5836 {
5837 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5838 &data->args.seq_args,
5839 &data->res.seq_res,
5840 task);
5841 }
5842
nfs4_commit_done_cb(struct rpc_task * task,struct nfs_commit_data * data)5843 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5844 {
5845 struct inode *inode = data->inode;
5846
5847 trace_nfs4_commit(data, task->tk_status);
5848 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5849 NULL, NULL) == -EAGAIN) {
5850 rpc_restart_call_prepare(task);
5851 return -EAGAIN;
5852 }
5853 return 0;
5854 }
5855
nfs4_commit_done(struct rpc_task * task,struct nfs_commit_data * data)5856 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5857 {
5858 if (!nfs4_sequence_done(task, &data->res.seq_res))
5859 return -EAGAIN;
5860 return data->commit_done_cb(task, data);
5861 }
5862
nfs4_proc_commit_setup(struct nfs_commit_data * data,struct rpc_message * msg,struct rpc_clnt ** clnt)5863 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5864 struct rpc_clnt **clnt)
5865 {
5866 struct nfs_server *server = NFS_SERVER(data->inode);
5867
5868 if (data->commit_done_cb == NULL)
5869 data->commit_done_cb = nfs4_commit_done_cb;
5870 data->res.server = server;
5871 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5872 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5873 nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
5874 NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5875 }
5876
_nfs4_proc_commit(struct file * dst,struct nfs_commitargs * args,struct nfs_commitres * res)5877 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5878 struct nfs_commitres *res)
5879 {
5880 struct inode *dst_inode = file_inode(dst);
5881 struct nfs_server *server = NFS_SERVER(dst_inode);
5882 struct rpc_message msg = {
5883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5884 .rpc_argp = args,
5885 .rpc_resp = res,
5886 };
5887
5888 args->fh = NFS_FH(dst_inode);
5889 return nfs4_call_sync(server->client, server, &msg,
5890 &args->seq_args, &res->seq_res, 1);
5891 }
5892
nfs4_proc_commit(struct file * dst,__u64 offset,__u32 count,struct nfs_commitres * res)5893 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5894 {
5895 struct nfs_commitargs args = {
5896 .offset = offset,
5897 .count = count,
5898 };
5899 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5900 struct nfs4_exception exception = { };
5901 int status;
5902
5903 do {
5904 status = _nfs4_proc_commit(dst, &args, res);
5905 status = nfs4_handle_exception(dst_server, status, &exception);
5906 } while (exception.retry);
5907
5908 return status;
5909 }
5910
5911 struct nfs4_renewdata {
5912 struct nfs_client *client;
5913 unsigned long timestamp;
5914 };
5915
5916 /*
5917 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5918 * standalone procedure for queueing an asynchronous RENEW.
5919 */
nfs4_renew_release(void * calldata)5920 static void nfs4_renew_release(void *calldata)
5921 {
5922 struct nfs4_renewdata *data = calldata;
5923 struct nfs_client *clp = data->client;
5924
5925 if (refcount_read(&clp->cl_count) > 1)
5926 nfs4_schedule_state_renewal(clp);
5927 nfs_put_client(clp);
5928 kfree(data);
5929 }
5930
nfs4_renew_done(struct rpc_task * task,void * calldata)5931 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5932 {
5933 struct nfs4_renewdata *data = calldata;
5934 struct nfs_client *clp = data->client;
5935 unsigned long timestamp = data->timestamp;
5936
5937 trace_nfs4_renew_async(clp, task->tk_status);
5938 switch (task->tk_status) {
5939 case 0:
5940 break;
5941 case -NFS4ERR_LEASE_MOVED:
5942 nfs4_schedule_lease_moved_recovery(clp);
5943 break;
5944 default:
5945 /* Unless we're shutting down, schedule state recovery! */
5946 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5947 return;
5948 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5949 nfs4_schedule_lease_recovery(clp);
5950 return;
5951 }
5952 nfs4_schedule_path_down_recovery(clp);
5953 }
5954 do_renew_lease(clp, timestamp);
5955 }
5956
5957 static const struct rpc_call_ops nfs4_renew_ops = {
5958 .rpc_call_done = nfs4_renew_done,
5959 .rpc_release = nfs4_renew_release,
5960 };
5961
nfs4_proc_async_renew(struct nfs_client * clp,const struct cred * cred,unsigned renew_flags)5962 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5963 {
5964 struct rpc_message msg = {
5965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5966 .rpc_argp = clp,
5967 .rpc_cred = cred,
5968 };
5969 struct nfs4_renewdata *data;
5970
5971 if (renew_flags == 0)
5972 return 0;
5973 if (!refcount_inc_not_zero(&clp->cl_count))
5974 return -EIO;
5975 data = kmalloc(sizeof(*data), GFP_NOFS);
5976 if (data == NULL) {
5977 nfs_put_client(clp);
5978 return -ENOMEM;
5979 }
5980 data->client = clp;
5981 data->timestamp = jiffies;
5982 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5983 &nfs4_renew_ops, data);
5984 }
5985
nfs4_proc_renew(struct nfs_client * clp,const struct cred * cred)5986 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5987 {
5988 struct rpc_message msg = {
5989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5990 .rpc_argp = clp,
5991 .rpc_cred = cred,
5992 };
5993 unsigned long now = jiffies;
5994 int status;
5995
5996 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5997 if (status < 0)
5998 return status;
5999 do_renew_lease(clp, now);
6000 return 0;
6001 }
6002
nfs4_server_supports_acls(const struct nfs_server * server,enum nfs4_acl_type type)6003 static bool nfs4_server_supports_acls(const struct nfs_server *server,
6004 enum nfs4_acl_type type)
6005 {
6006 switch (type) {
6007 default:
6008 return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
6009 case NFS4ACL_DACL:
6010 return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
6011 case NFS4ACL_SACL:
6012 return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
6013 }
6014 }
6015
6016 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
6017 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
6018 * the stack.
6019 */
6020 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
6021
nfs4_buf_to_pages_noslab(const void * buf,size_t buflen,struct page ** pages)6022 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
6023 struct page **pages)
6024 {
6025 struct page *newpage, **spages;
6026 int rc = 0;
6027 size_t len;
6028 spages = pages;
6029
6030 do {
6031 len = min_t(size_t, PAGE_SIZE, buflen);
6032 newpage = alloc_page(GFP_KERNEL);
6033
6034 if (newpage == NULL)
6035 goto unwind;
6036 memcpy(page_address(newpage), buf, len);
6037 buf += len;
6038 buflen -= len;
6039 *pages++ = newpage;
6040 rc++;
6041 } while (buflen != 0);
6042
6043 return rc;
6044
6045 unwind:
6046 for(; rc > 0; rc--)
6047 __free_page(spages[rc-1]);
6048 return -ENOMEM;
6049 }
6050
6051 struct nfs4_cached_acl {
6052 enum nfs4_acl_type type;
6053 int cached;
6054 size_t len;
6055 char data[];
6056 };
6057
nfs4_set_cached_acl(struct inode * inode,struct nfs4_cached_acl * acl)6058 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
6059 {
6060 struct nfs_inode *nfsi = NFS_I(inode);
6061
6062 spin_lock(&inode->i_lock);
6063 kfree(nfsi->nfs4_acl);
6064 nfsi->nfs4_acl = acl;
6065 spin_unlock(&inode->i_lock);
6066 }
6067
nfs4_zap_acl_attr(struct inode * inode)6068 static void nfs4_zap_acl_attr(struct inode *inode)
6069 {
6070 nfs4_set_cached_acl(inode, NULL);
6071 }
6072
nfs4_read_cached_acl(struct inode * inode,char * buf,size_t buflen,enum nfs4_acl_type type)6073 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
6074 size_t buflen, enum nfs4_acl_type type)
6075 {
6076 struct nfs_inode *nfsi = NFS_I(inode);
6077 struct nfs4_cached_acl *acl;
6078 int ret = -ENOENT;
6079
6080 spin_lock(&inode->i_lock);
6081 acl = nfsi->nfs4_acl;
6082 if (acl == NULL)
6083 goto out;
6084 if (acl->type != type)
6085 goto out;
6086 if (buf == NULL) /* user is just asking for length */
6087 goto out_len;
6088 if (acl->cached == 0)
6089 goto out;
6090 ret = -ERANGE; /* see getxattr(2) man page */
6091 if (acl->len > buflen)
6092 goto out;
6093 memcpy(buf, acl->data, acl->len);
6094 out_len:
6095 ret = acl->len;
6096 out:
6097 spin_unlock(&inode->i_lock);
6098 return ret;
6099 }
6100
nfs4_write_cached_acl(struct inode * inode,struct page ** pages,size_t pgbase,size_t acl_len,enum nfs4_acl_type type)6101 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
6102 size_t pgbase, size_t acl_len,
6103 enum nfs4_acl_type type)
6104 {
6105 struct nfs4_cached_acl *acl;
6106 size_t buflen = sizeof(*acl) + acl_len;
6107
6108 if (buflen <= PAGE_SIZE) {
6109 acl = kmalloc(buflen, GFP_KERNEL);
6110 if (acl == NULL)
6111 goto out;
6112 acl->cached = 1;
6113 _copy_from_pages(acl->data, pages, pgbase, acl_len);
6114 } else {
6115 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
6116 if (acl == NULL)
6117 goto out;
6118 acl->cached = 0;
6119 }
6120 acl->type = type;
6121 acl->len = acl_len;
6122 out:
6123 nfs4_set_cached_acl(inode, acl);
6124 }
6125
6126 /*
6127 * The getxattr API returns the required buffer length when called with a
6128 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
6129 * the required buf. On a NULL buf, we send a page of data to the server
6130 * guessing that the ACL request can be serviced by a page. If so, we cache
6131 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
6132 * the cache. If not so, we throw away the page, and cache the required
6133 * length. The next getxattr call will then produce another round trip to
6134 * the server, this time with the input buf of the required size.
6135 */
__nfs4_get_acl_uncached(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6136 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
6137 size_t buflen, enum nfs4_acl_type type)
6138 {
6139 struct page **pages;
6140 struct nfs_getaclargs args = {
6141 .fh = NFS_FH(inode),
6142 .acl_type = type,
6143 .acl_len = buflen,
6144 };
6145 struct nfs_getaclres res = {
6146 .acl_type = type,
6147 .acl_len = buflen,
6148 };
6149 struct rpc_message msg = {
6150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
6151 .rpc_argp = &args,
6152 .rpc_resp = &res,
6153 };
6154 unsigned int npages;
6155 int ret = -ENOMEM, i;
6156 struct nfs_server *server = NFS_SERVER(inode);
6157
6158 if (buflen == 0)
6159 buflen = server->rsize;
6160
6161 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
6162 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
6163 if (!pages)
6164 return -ENOMEM;
6165
6166 args.acl_pages = pages;
6167
6168 for (i = 0; i < npages; i++) {
6169 pages[i] = alloc_page(GFP_KERNEL);
6170 if (!pages[i])
6171 goto out_free;
6172 }
6173
6174 /* for decoding across pages */
6175 res.acl_scratch = folio_alloc(GFP_KERNEL, 0);
6176 if (!res.acl_scratch)
6177 goto out_free;
6178
6179 args.acl_len = npages * PAGE_SIZE;
6180
6181 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
6182 __func__, buf, buflen, npages, args.acl_len);
6183 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
6184 &msg, &args.seq_args, &res.seq_res, 0);
6185 if (ret)
6186 goto out_free;
6187
6188 /* Handle the case where the passed-in buffer is too short */
6189 if (res.acl_flags & NFS4_ACL_TRUNC) {
6190 /* Did the user only issue a request for the acl length? */
6191 if (buf == NULL)
6192 goto out_ok;
6193 ret = -ERANGE;
6194 goto out_free;
6195 }
6196 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
6197 type);
6198 if (buf) {
6199 if (res.acl_len > buflen) {
6200 ret = -ERANGE;
6201 goto out_free;
6202 }
6203 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6204 }
6205 out_ok:
6206 ret = res.acl_len;
6207 out_free:
6208 while (--i >= 0)
6209 __free_page(pages[i]);
6210 if (res.acl_scratch)
6211 folio_put(res.acl_scratch);
6212 kfree(pages);
6213 return ret;
6214 }
6215
nfs4_get_acl_uncached(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6216 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6217 size_t buflen, enum nfs4_acl_type type)
6218 {
6219 struct nfs4_exception exception = {
6220 .interruptible = true,
6221 };
6222 ssize_t ret;
6223 do {
6224 ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6225 trace_nfs4_get_acl(inode, ret);
6226 if (ret >= 0)
6227 break;
6228 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6229 } while (exception.retry);
6230 return ret;
6231 }
6232
nfs4_proc_get_acl(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6233 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6234 enum nfs4_acl_type type)
6235 {
6236 struct nfs_server *server = NFS_SERVER(inode);
6237 int ret;
6238
6239 if (unlikely(NFS_FH(inode)->size == 0))
6240 return -ENODATA;
6241 if (!nfs4_server_supports_acls(server, type))
6242 return -EOPNOTSUPP;
6243 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6244 if (ret < 0)
6245 return ret;
6246 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6247 nfs_zap_acl_cache(inode);
6248 ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6249 if (ret != -ENOENT)
6250 /* -ENOENT is returned if there is no ACL or if there is an ACL
6251 * but no cached acl data, just the acl length */
6252 return ret;
6253 return nfs4_get_acl_uncached(inode, buf, buflen, type);
6254 }
6255
__nfs4_proc_set_acl(struct inode * inode,const void * buf,size_t buflen,enum nfs4_acl_type type)6256 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6257 size_t buflen, enum nfs4_acl_type type)
6258 {
6259 struct nfs_server *server = NFS_SERVER(inode);
6260 struct page *pages[NFS4ACL_MAXPAGES];
6261 struct nfs_setaclargs arg = {
6262 .fh = NFS_FH(inode),
6263 .acl_type = type,
6264 .acl_len = buflen,
6265 .acl_pages = pages,
6266 };
6267 struct nfs_setaclres res;
6268 struct rpc_message msg = {
6269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6270 .rpc_argp = &arg,
6271 .rpc_resp = &res,
6272 };
6273 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6274 int ret, i;
6275
6276 /* You can't remove system.nfs4_acl: */
6277 if (buflen == 0)
6278 return -EINVAL;
6279 if (!nfs4_server_supports_acls(server, type))
6280 return -EOPNOTSUPP;
6281 if (npages > ARRAY_SIZE(pages))
6282 return -ERANGE;
6283 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6284 if (i < 0)
6285 return i;
6286 nfs4_inode_make_writeable(inode);
6287 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6288
6289 /*
6290 * Free each page after tx, so the only ref left is
6291 * held by the network stack
6292 */
6293 for (; i > 0; i--)
6294 put_page(pages[i-1]);
6295
6296 /*
6297 * Acl update can result in inode attribute update.
6298 * so mark the attribute cache invalid.
6299 */
6300 spin_lock(&inode->i_lock);
6301 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6302 NFS_INO_INVALID_CTIME |
6303 NFS_INO_REVAL_FORCED);
6304 spin_unlock(&inode->i_lock);
6305 nfs_access_zap_cache(inode);
6306 nfs_zap_acl_cache(inode);
6307 return ret;
6308 }
6309
nfs4_proc_set_acl(struct inode * inode,const void * buf,size_t buflen,enum nfs4_acl_type type)6310 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6311 size_t buflen, enum nfs4_acl_type type)
6312 {
6313 struct nfs4_exception exception = { };
6314 int err;
6315
6316 if (unlikely(NFS_FH(inode)->size == 0))
6317 return -ENODATA;
6318 do {
6319 err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6320 trace_nfs4_set_acl(inode, err);
6321 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6322 /*
6323 * no need to retry since the kernel
6324 * isn't involved in encoding the ACEs.
6325 */
6326 err = -EINVAL;
6327 break;
6328 }
6329 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6330 &exception);
6331 } while (exception.retry);
6332 return err;
6333 }
6334
6335 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
_nfs4_get_security_label(struct inode * inode,void * buf,size_t buflen)6336 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6337 size_t buflen)
6338 {
6339 struct nfs_server *server = NFS_SERVER(inode);
6340 struct nfs4_label label = {0, 0, 0, buflen, buf};
6341
6342 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6343 struct nfs_fattr fattr = {
6344 .label = &label,
6345 };
6346 struct nfs4_getattr_arg arg = {
6347 .fh = NFS_FH(inode),
6348 .bitmask = bitmask,
6349 };
6350 struct nfs4_getattr_res res = {
6351 .fattr = &fattr,
6352 .server = server,
6353 };
6354 struct rpc_message msg = {
6355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6356 .rpc_argp = &arg,
6357 .rpc_resp = &res,
6358 };
6359 int ret;
6360
6361 nfs_fattr_init(&fattr);
6362
6363 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6364 if (ret)
6365 return ret;
6366 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6367 return -ENOENT;
6368 return label.len;
6369 }
6370
nfs4_get_security_label(struct inode * inode,void * buf,size_t buflen)6371 static int nfs4_get_security_label(struct inode *inode, void *buf,
6372 size_t buflen)
6373 {
6374 struct nfs4_exception exception = {
6375 .interruptible = true,
6376 };
6377 int err;
6378
6379 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6380 return -EOPNOTSUPP;
6381
6382 do {
6383 err = _nfs4_get_security_label(inode, buf, buflen);
6384 trace_nfs4_get_security_label(inode, err);
6385 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6386 &exception);
6387 } while (exception.retry);
6388 return err;
6389 }
6390
_nfs4_do_set_security_label(struct inode * inode,struct nfs4_label * ilabel,struct nfs_fattr * fattr)6391 static int _nfs4_do_set_security_label(struct inode *inode,
6392 struct nfs4_label *ilabel,
6393 struct nfs_fattr *fattr)
6394 {
6395
6396 struct iattr sattr = {0};
6397 struct nfs_server *server = NFS_SERVER(inode);
6398 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6399 struct nfs_setattrargs arg = {
6400 .fh = NFS_FH(inode),
6401 .iap = &sattr,
6402 .server = server,
6403 .bitmask = bitmask,
6404 .label = ilabel,
6405 };
6406 struct nfs_setattrres res = {
6407 .fattr = fattr,
6408 .server = server,
6409 };
6410 struct rpc_message msg = {
6411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6412 .rpc_argp = &arg,
6413 .rpc_resp = &res,
6414 };
6415 int status;
6416
6417 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6418
6419 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6420 if (status)
6421 dprintk("%s failed: %d\n", __func__, status);
6422
6423 return status;
6424 }
6425
nfs4_do_set_security_label(struct inode * inode,struct nfs4_label * ilabel,struct nfs_fattr * fattr)6426 static int nfs4_do_set_security_label(struct inode *inode,
6427 struct nfs4_label *ilabel,
6428 struct nfs_fattr *fattr)
6429 {
6430 struct nfs4_exception exception = { };
6431 int err;
6432
6433 do {
6434 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6435 trace_nfs4_set_security_label(inode, err);
6436 err = nfs4_handle_exception(NFS_SERVER(inode), err,
6437 &exception);
6438 } while (exception.retry);
6439 return err;
6440 }
6441
6442 static int
nfs4_set_security_label(struct inode * inode,const void * buf,size_t buflen)6443 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6444 {
6445 struct nfs4_label ilabel = {0, 0, 0, buflen, (char *)buf };
6446 struct nfs_fattr *fattr;
6447 int status;
6448
6449 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6450 return -EOPNOTSUPP;
6451
6452 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6453 if (fattr == NULL)
6454 return -ENOMEM;
6455
6456 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6457 if (status == 0)
6458 nfs_setsecurity(inode, fattr);
6459
6460 nfs_free_fattr(fattr);
6461 return status;
6462 }
6463 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6464
6465
nfs4_init_boot_verifier(const struct nfs_client * clp,nfs4_verifier * bootverf)6466 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6467 nfs4_verifier *bootverf)
6468 {
6469 __be32 verf[2];
6470
6471 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6472 /* An impossible timestamp guarantees this value
6473 * will never match a generated boot time. */
6474 verf[0] = cpu_to_be32(U32_MAX);
6475 verf[1] = cpu_to_be32(U32_MAX);
6476 } else {
6477 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6478 u64 ns = ktime_to_ns(nn->boot_time);
6479
6480 verf[0] = cpu_to_be32(ns >> 32);
6481 verf[1] = cpu_to_be32(ns);
6482 }
6483 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6484 }
6485
6486 static size_t
nfs4_get_uniquifier(struct nfs_client * clp,char * buf,size_t buflen)6487 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6488 {
6489 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6490 struct nfs_netns_client *nn_clp = nn->nfs_client;
6491 const char *id;
6492
6493 buf[0] = '\0';
6494
6495 if (nn_clp) {
6496 rcu_read_lock();
6497 id = rcu_dereference(nn_clp->identifier);
6498 if (id)
6499 strscpy(buf, id, buflen);
6500 rcu_read_unlock();
6501 }
6502
6503 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6504 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6505
6506 return strlen(buf);
6507 }
6508
6509 static int
nfs4_init_nonuniform_client_string(struct nfs_client * clp)6510 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6511 {
6512 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6513 size_t buflen;
6514 size_t len;
6515 char *str;
6516
6517 if (clp->cl_owner_id != NULL)
6518 return 0;
6519
6520 rcu_read_lock();
6521 len = 14 +
6522 strlen(clp->cl_rpcclient->cl_nodename) +
6523 1 +
6524 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6525 1;
6526 rcu_read_unlock();
6527
6528 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6529 if (buflen)
6530 len += buflen + 1;
6531
6532 if (len > NFS4_OPAQUE_LIMIT + 1)
6533 return -EINVAL;
6534
6535 /*
6536 * Since this string is allocated at mount time, and held until the
6537 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6538 * about a memory-reclaim deadlock.
6539 */
6540 str = kmalloc(len, GFP_KERNEL);
6541 if (!str)
6542 return -ENOMEM;
6543
6544 rcu_read_lock();
6545 if (buflen)
6546 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6547 clp->cl_rpcclient->cl_nodename, buf,
6548 rpc_peeraddr2str(clp->cl_rpcclient,
6549 RPC_DISPLAY_ADDR));
6550 else
6551 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6552 clp->cl_rpcclient->cl_nodename,
6553 rpc_peeraddr2str(clp->cl_rpcclient,
6554 RPC_DISPLAY_ADDR));
6555 rcu_read_unlock();
6556
6557 clp->cl_owner_id = str;
6558 return 0;
6559 }
6560
6561 static int
nfs4_init_uniform_client_string(struct nfs_client * clp)6562 nfs4_init_uniform_client_string(struct nfs_client *clp)
6563 {
6564 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6565 size_t buflen;
6566 size_t len;
6567 char *str;
6568
6569 if (clp->cl_owner_id != NULL)
6570 return 0;
6571
6572 len = 10 + 10 + 1 + 10 + 1 +
6573 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6574
6575 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6576 if (buflen)
6577 len += buflen + 1;
6578
6579 if (len > NFS4_OPAQUE_LIMIT + 1)
6580 return -EINVAL;
6581
6582 /*
6583 * Since this string is allocated at mount time, and held until the
6584 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6585 * about a memory-reclaim deadlock.
6586 */
6587 str = kmalloc(len, GFP_KERNEL);
6588 if (!str)
6589 return -ENOMEM;
6590
6591 if (buflen)
6592 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6593 clp->rpc_ops->version, clp->cl_minorversion,
6594 buf, clp->cl_rpcclient->cl_nodename);
6595 else
6596 scnprintf(str, len, "Linux NFSv%u.%u %s",
6597 clp->rpc_ops->version, clp->cl_minorversion,
6598 clp->cl_rpcclient->cl_nodename);
6599 clp->cl_owner_id = str;
6600 return 0;
6601 }
6602
6603 /*
6604 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6605 * services. Advertise one based on the address family of the
6606 * clientaddr.
6607 */
6608 static unsigned int
nfs4_init_callback_netid(const struct nfs_client * clp,char * buf,size_t len)6609 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6610 {
6611 if (strchr(clp->cl_ipaddr, ':') != NULL)
6612 return scnprintf(buf, len, "tcp6");
6613 else
6614 return scnprintf(buf, len, "tcp");
6615 }
6616
nfs4_setclientid_done(struct rpc_task * task,void * calldata)6617 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6618 {
6619 struct nfs4_setclientid *sc = calldata;
6620
6621 if (task->tk_status == 0)
6622 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6623 }
6624
6625 static const struct rpc_call_ops nfs4_setclientid_ops = {
6626 .rpc_call_done = nfs4_setclientid_done,
6627 };
6628
6629 /**
6630 * nfs4_proc_setclientid - Negotiate client ID
6631 * @clp: state data structure
6632 * @program: RPC program for NFSv4 callback service
6633 * @port: IP port number for NFS4 callback service
6634 * @cred: credential to use for this call
6635 * @res: where to place the result
6636 *
6637 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6638 */
nfs4_proc_setclientid(struct nfs_client * clp,u32 program,unsigned short port,const struct cred * cred,struct nfs4_setclientid_res * res)6639 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6640 unsigned short port, const struct cred *cred,
6641 struct nfs4_setclientid_res *res)
6642 {
6643 nfs4_verifier sc_verifier;
6644 struct nfs4_setclientid setclientid = {
6645 .sc_verifier = &sc_verifier,
6646 .sc_prog = program,
6647 .sc_clnt = clp,
6648 };
6649 struct rpc_message msg = {
6650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6651 .rpc_argp = &setclientid,
6652 .rpc_resp = res,
6653 .rpc_cred = cred,
6654 };
6655 struct rpc_task_setup task_setup_data = {
6656 .rpc_client = clp->cl_rpcclient,
6657 .rpc_message = &msg,
6658 .callback_ops = &nfs4_setclientid_ops,
6659 .callback_data = &setclientid,
6660 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6661 };
6662 unsigned long now = jiffies;
6663 int status;
6664
6665 /* nfs_client_id4 */
6666 nfs4_init_boot_verifier(clp, &sc_verifier);
6667
6668 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6669 status = nfs4_init_uniform_client_string(clp);
6670 else
6671 status = nfs4_init_nonuniform_client_string(clp);
6672
6673 if (status)
6674 goto out;
6675
6676 /* cb_client4 */
6677 setclientid.sc_netid_len =
6678 nfs4_init_callback_netid(clp,
6679 setclientid.sc_netid,
6680 sizeof(setclientid.sc_netid));
6681 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6682 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6683 clp->cl_ipaddr, port >> 8, port & 255);
6684
6685 dprintk("NFS call setclientid auth=%s, '%s'\n",
6686 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6687 clp->cl_owner_id);
6688
6689 status = nfs4_call_sync_custom(&task_setup_data);
6690 if (setclientid.sc_cred) {
6691 kfree(clp->cl_acceptor);
6692 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6693 put_rpccred(setclientid.sc_cred);
6694 }
6695
6696 if (status == 0)
6697 do_renew_lease(clp, now);
6698 out:
6699 trace_nfs4_setclientid(clp, status);
6700 dprintk("NFS reply setclientid: %d\n", status);
6701 return status;
6702 }
6703
6704 /**
6705 * nfs4_proc_setclientid_confirm - Confirm client ID
6706 * @clp: state data structure
6707 * @arg: result of a previous SETCLIENTID
6708 * @cred: credential to use for this call
6709 *
6710 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6711 */
nfs4_proc_setclientid_confirm(struct nfs_client * clp,struct nfs4_setclientid_res * arg,const struct cred * cred)6712 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6713 struct nfs4_setclientid_res *arg,
6714 const struct cred *cred)
6715 {
6716 struct rpc_message msg = {
6717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6718 .rpc_argp = arg,
6719 .rpc_cred = cred,
6720 };
6721 int status;
6722
6723 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6724 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6725 clp->cl_clientid);
6726 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6727 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6728 trace_nfs4_setclientid_confirm(clp, status);
6729 dprintk("NFS reply setclientid_confirm: %d\n", status);
6730 return status;
6731 }
6732
6733 struct nfs4_delegreturndata {
6734 struct nfs4_delegreturnargs args;
6735 struct nfs4_delegreturnres res;
6736 struct nfs_fh fh;
6737 nfs4_stateid stateid;
6738 unsigned long timestamp;
6739 unsigned short retrans;
6740 struct {
6741 struct nfs4_layoutreturn_args arg;
6742 struct nfs4_layoutreturn_res res;
6743 struct nfs4_xdr_opaque_data ld_private;
6744 u32 roc_barrier;
6745 bool roc;
6746 } lr;
6747 struct nfs4_delegattr sattr;
6748 struct nfs_fattr fattr;
6749 int rpc_status;
6750 struct inode *inode;
6751 };
6752
nfs4_delegreturn_done(struct rpc_task * task,void * calldata)6753 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6754 {
6755 struct nfs4_delegreturndata *data = calldata;
6756 struct nfs4_exception exception = {
6757 .inode = data->inode,
6758 .stateid = &data->stateid,
6759 .task_is_privileged = data->args.seq_args.sa_privileged,
6760 .retrans = data->retrans,
6761 };
6762
6763 if (!nfs4_sequence_done(task, &data->res.seq_res))
6764 return;
6765
6766 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6767
6768 /* Handle Layoutreturn errors */
6769 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6770 &data->res.lr_ret) == -EAGAIN)
6771 goto out_restart;
6772
6773 if (data->args.sattr_args && task->tk_status != 0) {
6774 switch(data->res.sattr_ret) {
6775 case 0:
6776 data->args.sattr_args = NULL;
6777 data->res.sattr_res = false;
6778 break;
6779 case -NFS4ERR_ADMIN_REVOKED:
6780 case -NFS4ERR_DELEG_REVOKED:
6781 case -NFS4ERR_EXPIRED:
6782 case -NFS4ERR_BAD_STATEID:
6783 /* Let the main handler below do stateid recovery */
6784 break;
6785 case -NFS4ERR_OLD_STATEID:
6786 if (nfs4_refresh_delegation_stateid(&data->stateid,
6787 data->inode))
6788 goto out_restart;
6789 fallthrough;
6790 default:
6791 data->args.sattr_args = NULL;
6792 data->res.sattr_res = false;
6793 goto out_restart;
6794 }
6795 }
6796
6797 switch (task->tk_status) {
6798 case 0:
6799 renew_lease(data->res.server, data->timestamp);
6800 break;
6801 case -NFS4ERR_ADMIN_REVOKED:
6802 case -NFS4ERR_DELEG_REVOKED:
6803 case -NFS4ERR_EXPIRED:
6804 nfs4_free_revoked_stateid(data->res.server,
6805 data->args.stateid,
6806 task->tk_msg.rpc_cred);
6807 fallthrough;
6808 case -NFS4ERR_BAD_STATEID:
6809 case -NFS4ERR_STALE_STATEID:
6810 case -ETIMEDOUT:
6811 task->tk_status = 0;
6812 break;
6813 case -NFS4ERR_OLD_STATEID:
6814 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6815 nfs4_stateid_seqid_inc(&data->stateid);
6816 if (data->args.bitmask) {
6817 data->args.bitmask = NULL;
6818 data->res.fattr = NULL;
6819 }
6820 goto out_restart;
6821 case -NFS4ERR_ACCESS:
6822 if (data->args.bitmask) {
6823 data->args.bitmask = NULL;
6824 data->res.fattr = NULL;
6825 goto out_restart;
6826 }
6827 fallthrough;
6828 default:
6829 task->tk_status = nfs4_async_handle_exception(task,
6830 data->res.server, task->tk_status,
6831 &exception);
6832 data->retrans = exception.retrans;
6833 if (exception.retry)
6834 goto out_restart;
6835 }
6836 nfs_delegation_mark_returned(data->inode, data->args.stateid);
6837 data->rpc_status = task->tk_status;
6838 return;
6839 out_restart:
6840 task->tk_status = 0;
6841 rpc_restart_call_prepare(task);
6842 }
6843
nfs4_delegreturn_release(void * calldata)6844 static void nfs4_delegreturn_release(void *calldata)
6845 {
6846 struct nfs4_delegreturndata *data = calldata;
6847 struct inode *inode = data->inode;
6848
6849 if (data->lr.roc)
6850 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6851 data->res.lr_ret);
6852 if (inode) {
6853 nfs4_fattr_set_prechange(&data->fattr,
6854 inode_peek_iversion_raw(inode));
6855 nfs_refresh_inode(inode, &data->fattr);
6856 nfs_iput_and_deactive(inode);
6857 }
6858 kfree(calldata);
6859 }
6860
nfs4_delegreturn_prepare(struct rpc_task * task,void * data)6861 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6862 {
6863 struct nfs4_delegreturndata *d_data;
6864 struct pnfs_layout_hdr *lo;
6865
6866 d_data = data;
6867
6868 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6869 nfs4_sequence_done(task, &d_data->res.seq_res);
6870 return;
6871 }
6872
6873 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6874 if (lo && !pnfs_layout_is_valid(lo)) {
6875 d_data->args.lr_args = NULL;
6876 d_data->res.lr_res = NULL;
6877 }
6878
6879 nfs4_setup_sequence(d_data->res.server->nfs_client,
6880 &d_data->args.seq_args,
6881 &d_data->res.seq_res,
6882 task);
6883 }
6884
6885 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6886 .rpc_call_prepare = nfs4_delegreturn_prepare,
6887 .rpc_call_done = nfs4_delegreturn_done,
6888 .rpc_release = nfs4_delegreturn_release,
6889 };
6890
_nfs4_proc_delegreturn(struct inode * inode,const struct cred * cred,const nfs4_stateid * stateid,struct nfs_delegation * delegation,int issync)6891 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
6892 const nfs4_stateid *stateid,
6893 struct nfs_delegation *delegation,
6894 int issync)
6895 {
6896 struct nfs4_delegreturndata *data;
6897 struct nfs_server *server = NFS_SERVER(inode);
6898 struct rpc_task *task;
6899 struct rpc_message msg = {
6900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6901 .rpc_cred = cred,
6902 };
6903 struct rpc_task_setup task_setup_data = {
6904 .rpc_client = server->client,
6905 .rpc_message = &msg,
6906 .callback_ops = &nfs4_delegreturn_ops,
6907 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6908 };
6909 int status = 0;
6910
6911 if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6912 task_setup_data.flags |= RPC_TASK_MOVEABLE;
6913
6914 data = kzalloc(sizeof(*data), GFP_KERNEL);
6915 if (data == NULL)
6916 return -ENOMEM;
6917
6918 nfs4_state_protect(server->nfs_client,
6919 NFS_SP4_MACH_CRED_CLEANUP,
6920 &task_setup_data.rpc_client, &msg);
6921
6922 data->args.fhandle = &data->fh;
6923 data->args.stateid = &data->stateid;
6924 nfs4_bitmask_set(data->args.bitmask_store,
6925 server->cache_consistency_bitmask, inode, 0);
6926 data->args.bitmask = data->args.bitmask_store;
6927 nfs_copy_fh(&data->fh, NFS_FH(inode));
6928 nfs4_stateid_copy(&data->stateid, stateid);
6929 data->res.fattr = &data->fattr;
6930 data->res.server = server;
6931 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6932 data->lr.arg.ld_private = &data->lr.ld_private;
6933 nfs_fattr_init(data->res.fattr);
6934 data->timestamp = jiffies;
6935 data->rpc_status = 0;
6936 data->inode = nfs_igrab_and_active(inode);
6937 if (data->inode || issync) {
6938 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6939 cred);
6940 if (data->lr.roc) {
6941 data->args.lr_args = &data->lr.arg;
6942 data->res.lr_res = &data->lr.res;
6943 }
6944 }
6945
6946 if (delegation &&
6947 test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags)) {
6948 if (delegation->type & FMODE_READ) {
6949 data->sattr.atime = inode_get_atime(inode);
6950 data->sattr.atime_set = true;
6951 }
6952 if (delegation->type & FMODE_WRITE) {
6953 data->sattr.mtime = inode_get_mtime(inode);
6954 data->sattr.mtime_set = true;
6955 }
6956 data->args.sattr_args = &data->sattr;
6957 data->res.sattr_res = true;
6958 }
6959
6960 if (!data->inode)
6961 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6962 1);
6963 else
6964 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6965 0);
6966
6967 task_setup_data.callback_data = data;
6968 msg.rpc_argp = &data->args;
6969 msg.rpc_resp = &data->res;
6970 task = rpc_run_task(&task_setup_data);
6971 if (IS_ERR(task))
6972 return PTR_ERR(task);
6973 if (!issync)
6974 goto out;
6975 status = rpc_wait_for_completion_task(task);
6976 if (status != 0)
6977 goto out;
6978 status = data->rpc_status;
6979 out:
6980 rpc_put_task(task);
6981 return status;
6982 }
6983
nfs4_proc_delegreturn(struct inode * inode,const struct cred * cred,const nfs4_stateid * stateid,struct nfs_delegation * delegation,int issync)6984 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
6985 const nfs4_stateid *stateid,
6986 struct nfs_delegation *delegation, int issync)
6987 {
6988 struct nfs_server *server = NFS_SERVER(inode);
6989 struct nfs4_exception exception = { };
6990 int err;
6991 do {
6992 err = _nfs4_proc_delegreturn(inode, cred, stateid,
6993 delegation, issync);
6994 trace_nfs4_delegreturn(inode, stateid, err);
6995 switch (err) {
6996 case -NFS4ERR_STALE_STATEID:
6997 case -NFS4ERR_EXPIRED:
6998 case 0:
6999 return 0;
7000 }
7001 err = nfs4_handle_exception(server, err, &exception);
7002 } while (exception.retry);
7003 return err;
7004 }
7005
_nfs4_proc_getlk(struct nfs4_state * state,int cmd,struct file_lock * request)7006 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7007 {
7008 struct inode *inode = state->inode;
7009 struct nfs_server *server = NFS_SERVER(inode);
7010 struct nfs_client *clp = server->nfs_client;
7011 struct nfs_lockt_args arg = {
7012 .fh = NFS_FH(inode),
7013 .fl = request,
7014 };
7015 struct nfs_lockt_res res = {
7016 .denied = request,
7017 };
7018 struct rpc_message msg = {
7019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
7020 .rpc_argp = &arg,
7021 .rpc_resp = &res,
7022 .rpc_cred = state->owner->so_cred,
7023 };
7024 struct nfs4_lock_state *lsp;
7025 int status;
7026
7027 arg.lock_owner.clientid = clp->cl_clientid;
7028 status = nfs4_set_lock_state(state, request);
7029 if (status != 0)
7030 goto out;
7031 lsp = request->fl_u.nfs4_fl.owner;
7032 arg.lock_owner.id = lsp->ls_seqid.owner_id;
7033 arg.lock_owner.s_dev = server->s_dev;
7034 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
7035 switch (status) {
7036 case 0:
7037 request->c.flc_type = F_UNLCK;
7038 break;
7039 case -NFS4ERR_DENIED:
7040 status = 0;
7041 }
7042 request->fl_ops->fl_release_private(request);
7043 request->fl_ops = NULL;
7044 out:
7045 return status;
7046 }
7047
nfs4_proc_getlk(struct nfs4_state * state,int cmd,struct file_lock * request)7048 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7049 {
7050 struct nfs4_exception exception = {
7051 .interruptible = true,
7052 };
7053 int err;
7054
7055 do {
7056 err = _nfs4_proc_getlk(state, cmd, request);
7057 trace_nfs4_get_lock(request, state, cmd, err);
7058 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
7059 &exception);
7060 } while (exception.retry);
7061 return err;
7062 }
7063
7064 /*
7065 * Update the seqid of a lock stateid after receiving
7066 * NFS4ERR_OLD_STATEID
7067 */
nfs4_refresh_lock_old_stateid(nfs4_stateid * dst,struct nfs4_lock_state * lsp)7068 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
7069 struct nfs4_lock_state *lsp)
7070 {
7071 struct nfs4_state *state = lsp->ls_state;
7072 bool ret = false;
7073
7074 spin_lock(&state->state_lock);
7075 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
7076 goto out;
7077 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
7078 nfs4_stateid_seqid_inc(dst);
7079 else
7080 dst->seqid = lsp->ls_stateid.seqid;
7081 ret = true;
7082 out:
7083 spin_unlock(&state->state_lock);
7084 return ret;
7085 }
7086
nfs4_sync_lock_stateid(nfs4_stateid * dst,struct nfs4_lock_state * lsp)7087 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
7088 struct nfs4_lock_state *lsp)
7089 {
7090 struct nfs4_state *state = lsp->ls_state;
7091 bool ret;
7092
7093 spin_lock(&state->state_lock);
7094 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
7095 nfs4_stateid_copy(dst, &lsp->ls_stateid);
7096 spin_unlock(&state->state_lock);
7097 return ret;
7098 }
7099
7100 struct nfs4_unlockdata {
7101 struct nfs_locku_args arg;
7102 struct nfs_locku_res res;
7103 struct nfs4_lock_state *lsp;
7104 struct nfs_open_context *ctx;
7105 struct nfs_lock_context *l_ctx;
7106 struct file_lock fl;
7107 struct nfs_server *server;
7108 unsigned long timestamp;
7109 unsigned short retrans;
7110 };
7111
nfs4_alloc_unlockdata(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,struct nfs_seqid * seqid)7112 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
7113 struct nfs_open_context *ctx,
7114 struct nfs4_lock_state *lsp,
7115 struct nfs_seqid *seqid)
7116 {
7117 struct nfs4_unlockdata *p;
7118 struct nfs4_state *state = lsp->ls_state;
7119 struct inode *inode = state->inode;
7120 struct nfs_lock_context *l_ctx;
7121
7122 p = kzalloc(sizeof(*p), GFP_KERNEL);
7123 if (p == NULL)
7124 return NULL;
7125 l_ctx = nfs_get_lock_context(ctx);
7126 if (!IS_ERR(l_ctx)) {
7127 p->l_ctx = l_ctx;
7128 } else {
7129 kfree(p);
7130 return NULL;
7131 }
7132 p->arg.fh = NFS_FH(inode);
7133 p->arg.fl = &p->fl;
7134 p->arg.seqid = seqid;
7135 p->res.seqid = seqid;
7136 p->lsp = lsp;
7137 /* Ensure we don't close file until we're done freeing locks! */
7138 p->ctx = get_nfs_open_context(ctx);
7139 locks_init_lock(&p->fl);
7140 locks_copy_lock(&p->fl, fl);
7141 p->server = NFS_SERVER(inode);
7142 spin_lock(&state->state_lock);
7143 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
7144 spin_unlock(&state->state_lock);
7145 return p;
7146 }
7147
nfs4_locku_release_calldata(void * data)7148 static void nfs4_locku_release_calldata(void *data)
7149 {
7150 struct nfs4_unlockdata *calldata = data;
7151 nfs_free_seqid(calldata->arg.seqid);
7152 nfs4_put_lock_state(calldata->lsp);
7153 nfs_put_lock_context(calldata->l_ctx);
7154 put_nfs_open_context(calldata->ctx);
7155 kfree(calldata);
7156 }
7157
nfs4_locku_done(struct rpc_task * task,void * data)7158 static void nfs4_locku_done(struct rpc_task *task, void *data)
7159 {
7160 struct nfs4_unlockdata *calldata = data;
7161 struct nfs4_exception exception = {
7162 .inode = calldata->lsp->ls_state->inode,
7163 .stateid = &calldata->arg.stateid,
7164 .retrans = calldata->retrans,
7165 };
7166
7167 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
7168 return;
7169 switch (task->tk_status) {
7170 case 0:
7171 renew_lease(calldata->server, calldata->timestamp);
7172 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
7173 if (nfs4_update_lock_stateid(calldata->lsp,
7174 &calldata->res.stateid))
7175 break;
7176 fallthrough;
7177 case -NFS4ERR_ADMIN_REVOKED:
7178 case -NFS4ERR_EXPIRED:
7179 nfs4_free_revoked_stateid(calldata->server,
7180 &calldata->arg.stateid,
7181 task->tk_msg.rpc_cred);
7182 fallthrough;
7183 case -NFS4ERR_BAD_STATEID:
7184 case -NFS4ERR_STALE_STATEID:
7185 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
7186 calldata->lsp))
7187 rpc_restart_call_prepare(task);
7188 break;
7189 case -NFS4ERR_OLD_STATEID:
7190 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
7191 calldata->lsp))
7192 rpc_restart_call_prepare(task);
7193 break;
7194 default:
7195 task->tk_status = nfs4_async_handle_exception(task,
7196 calldata->server, task->tk_status,
7197 &exception);
7198 calldata->retrans = exception.retrans;
7199 if (exception.retry)
7200 rpc_restart_call_prepare(task);
7201 }
7202 nfs_release_seqid(calldata->arg.seqid);
7203 }
7204
nfs4_locku_prepare(struct rpc_task * task,void * data)7205 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
7206 {
7207 struct nfs4_unlockdata *calldata = data;
7208
7209 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
7210 nfs_async_iocounter_wait(task, calldata->l_ctx))
7211 return;
7212
7213 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
7214 goto out_wait;
7215 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
7216 /* Note: exit _without_ running nfs4_locku_done */
7217 goto out_no_action;
7218 }
7219 calldata->timestamp = jiffies;
7220 if (nfs4_setup_sequence(calldata->server->nfs_client,
7221 &calldata->arg.seq_args,
7222 &calldata->res.seq_res,
7223 task) != 0)
7224 nfs_release_seqid(calldata->arg.seqid);
7225 return;
7226 out_no_action:
7227 task->tk_action = NULL;
7228 out_wait:
7229 nfs4_sequence_done(task, &calldata->res.seq_res);
7230 }
7231
7232 static const struct rpc_call_ops nfs4_locku_ops = {
7233 .rpc_call_prepare = nfs4_locku_prepare,
7234 .rpc_call_done = nfs4_locku_done,
7235 .rpc_release = nfs4_locku_release_calldata,
7236 };
7237
nfs4_do_unlck(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,struct nfs_seqid * seqid)7238 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
7239 struct nfs_open_context *ctx,
7240 struct nfs4_lock_state *lsp,
7241 struct nfs_seqid *seqid)
7242 {
7243 struct nfs4_unlockdata *data;
7244 struct rpc_message msg = {
7245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
7246 .rpc_cred = ctx->cred,
7247 };
7248 struct rpc_task_setup task_setup_data = {
7249 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
7250 .rpc_message = &msg,
7251 .callback_ops = &nfs4_locku_ops,
7252 .workqueue = nfsiod_workqueue,
7253 .flags = RPC_TASK_ASYNC,
7254 };
7255
7256 if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
7257 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7258
7259 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
7260 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
7261
7262 /* Ensure this is an unlock - when canceling a lock, the
7263 * canceled lock is passed in, and it won't be an unlock.
7264 */
7265 fl->c.flc_type = F_UNLCK;
7266 if (fl->c.flc_flags & FL_CLOSE)
7267 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
7268
7269 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7270 if (data == NULL) {
7271 nfs_free_seqid(seqid);
7272 return ERR_PTR(-ENOMEM);
7273 }
7274
7275 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7276 msg.rpc_argp = &data->arg;
7277 msg.rpc_resp = &data->res;
7278 task_setup_data.callback_data = data;
7279 return rpc_run_task(&task_setup_data);
7280 }
7281
nfs4_proc_unlck(struct nfs4_state * state,int cmd,struct file_lock * request)7282 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7283 {
7284 struct inode *inode = state->inode;
7285 struct nfs4_state_owner *sp = state->owner;
7286 struct nfs_inode *nfsi = NFS_I(inode);
7287 struct nfs_seqid *seqid;
7288 struct nfs4_lock_state *lsp;
7289 struct rpc_task *task;
7290 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7291 int status = 0;
7292 unsigned char saved_flags = request->c.flc_flags;
7293
7294 status = nfs4_set_lock_state(state, request);
7295 /* Unlock _before_ we do the RPC call */
7296 request->c.flc_flags |= FL_EXISTS;
7297 /* Exclude nfs_delegation_claim_locks() */
7298 mutex_lock(&sp->so_delegreturn_mutex);
7299 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7300 down_read(&nfsi->rwsem);
7301 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7302 up_read(&nfsi->rwsem);
7303 mutex_unlock(&sp->so_delegreturn_mutex);
7304 goto out;
7305 }
7306 lsp = request->fl_u.nfs4_fl.owner;
7307 set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7308 up_read(&nfsi->rwsem);
7309 mutex_unlock(&sp->so_delegreturn_mutex);
7310 if (status != 0)
7311 goto out;
7312 /* Is this a delegated lock? */
7313 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7314 goto out;
7315 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7316 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7317 status = -ENOMEM;
7318 if (IS_ERR(seqid))
7319 goto out;
7320 task = nfs4_do_unlck(request,
7321 nfs_file_open_context(request->c.flc_file),
7322 lsp, seqid);
7323 status = PTR_ERR(task);
7324 if (IS_ERR(task))
7325 goto out;
7326 status = rpc_wait_for_completion_task(task);
7327 rpc_put_task(task);
7328 out:
7329 request->c.flc_flags = saved_flags;
7330 trace_nfs4_unlock(request, state, F_SETLK, status);
7331 return status;
7332 }
7333
7334 struct nfs4_lockdata {
7335 struct nfs_lock_args arg;
7336 struct nfs_lock_res res;
7337 struct nfs4_lock_state *lsp;
7338 struct nfs_open_context *ctx;
7339 struct file_lock fl;
7340 unsigned long timestamp;
7341 int rpc_status;
7342 int cancelled;
7343 struct nfs_server *server;
7344 };
7345
nfs4_alloc_lockdata(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,gfp_t gfp_mask)7346 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7347 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7348 gfp_t gfp_mask)
7349 {
7350 struct nfs4_lockdata *p;
7351 struct inode *inode = lsp->ls_state->inode;
7352 struct nfs_server *server = NFS_SERVER(inode);
7353 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7354
7355 p = kzalloc(sizeof(*p), gfp_mask);
7356 if (p == NULL)
7357 return NULL;
7358
7359 p->arg.fh = NFS_FH(inode);
7360 p->arg.fl = &p->fl;
7361 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7362 if (IS_ERR(p->arg.open_seqid))
7363 goto out_free;
7364 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7365 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7366 if (IS_ERR(p->arg.lock_seqid))
7367 goto out_free_seqid;
7368 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7369 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7370 p->arg.lock_owner.s_dev = server->s_dev;
7371 p->res.lock_seqid = p->arg.lock_seqid;
7372 p->lsp = lsp;
7373 p->server = server;
7374 p->ctx = get_nfs_open_context(ctx);
7375 locks_init_lock(&p->fl);
7376 locks_copy_lock(&p->fl, fl);
7377 return p;
7378 out_free_seqid:
7379 nfs_free_seqid(p->arg.open_seqid);
7380 out_free:
7381 kfree(p);
7382 return NULL;
7383 }
7384
nfs4_lock_prepare(struct rpc_task * task,void * calldata)7385 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7386 {
7387 struct nfs4_lockdata *data = calldata;
7388 struct nfs4_state *state = data->lsp->ls_state;
7389
7390 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7391 goto out_wait;
7392 /* Do we need to do an open_to_lock_owner? */
7393 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7394 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7395 goto out_release_lock_seqid;
7396 }
7397 nfs4_stateid_copy(&data->arg.open_stateid,
7398 &state->open_stateid);
7399 data->arg.new_lock_owner = 1;
7400 data->res.open_seqid = data->arg.open_seqid;
7401 } else {
7402 data->arg.new_lock_owner = 0;
7403 nfs4_stateid_copy(&data->arg.lock_stateid,
7404 &data->lsp->ls_stateid);
7405 }
7406 if (!nfs4_valid_open_stateid(state)) {
7407 data->rpc_status = -EBADF;
7408 task->tk_action = NULL;
7409 goto out_release_open_seqid;
7410 }
7411 data->timestamp = jiffies;
7412 if (nfs4_setup_sequence(data->server->nfs_client,
7413 &data->arg.seq_args,
7414 &data->res.seq_res,
7415 task) == 0)
7416 return;
7417 out_release_open_seqid:
7418 nfs_release_seqid(data->arg.open_seqid);
7419 out_release_lock_seqid:
7420 nfs_release_seqid(data->arg.lock_seqid);
7421 out_wait:
7422 nfs4_sequence_done(task, &data->res.seq_res);
7423 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7424 }
7425
nfs4_lock_done(struct rpc_task * task,void * calldata)7426 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7427 {
7428 struct nfs4_lockdata *data = calldata;
7429 struct nfs4_lock_state *lsp = data->lsp;
7430
7431 if (!nfs4_sequence_done(task, &data->res.seq_res))
7432 return;
7433
7434 data->rpc_status = task->tk_status;
7435 switch (task->tk_status) {
7436 case 0:
7437 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7438 data->timestamp);
7439 if (data->arg.new_lock && !data->cancelled) {
7440 data->fl.c.flc_flags &= ~(FL_SLEEP | FL_ACCESS);
7441 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7442 goto out_restart;
7443 }
7444 if (data->arg.new_lock_owner != 0) {
7445 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7446 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7447 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7448 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7449 goto out_restart;
7450 break;
7451 case -NFS4ERR_OLD_STATEID:
7452 if (data->arg.new_lock_owner != 0 &&
7453 nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7454 lsp->ls_state))
7455 goto out_restart;
7456 if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7457 goto out_restart;
7458 fallthrough;
7459 case -NFS4ERR_BAD_STATEID:
7460 case -NFS4ERR_STALE_STATEID:
7461 case -NFS4ERR_EXPIRED:
7462 if (data->arg.new_lock_owner != 0) {
7463 if (!nfs4_stateid_match(&data->arg.open_stateid,
7464 &lsp->ls_state->open_stateid))
7465 goto out_restart;
7466 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7467 &lsp->ls_stateid))
7468 goto out_restart;
7469 }
7470 out_done:
7471 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7472 return;
7473 out_restart:
7474 if (!data->cancelled)
7475 rpc_restart_call_prepare(task);
7476 goto out_done;
7477 }
7478
nfs4_lock_release(void * calldata)7479 static void nfs4_lock_release(void *calldata)
7480 {
7481 struct nfs4_lockdata *data = calldata;
7482
7483 nfs_free_seqid(data->arg.open_seqid);
7484 if (data->cancelled && data->rpc_status == 0) {
7485 struct rpc_task *task;
7486 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7487 data->arg.lock_seqid);
7488 if (!IS_ERR(task))
7489 rpc_put_task_async(task);
7490 dprintk("%s: cancelling lock!\n", __func__);
7491 } else
7492 nfs_free_seqid(data->arg.lock_seqid);
7493 nfs4_put_lock_state(data->lsp);
7494 put_nfs_open_context(data->ctx);
7495 kfree(data);
7496 }
7497
7498 static const struct rpc_call_ops nfs4_lock_ops = {
7499 .rpc_call_prepare = nfs4_lock_prepare,
7500 .rpc_call_done = nfs4_lock_done,
7501 .rpc_release = nfs4_lock_release,
7502 };
7503
nfs4_handle_setlk_error(struct nfs_server * server,struct nfs4_lock_state * lsp,int new_lock_owner,int error)7504 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7505 {
7506 switch (error) {
7507 case -NFS4ERR_ADMIN_REVOKED:
7508 case -NFS4ERR_EXPIRED:
7509 case -NFS4ERR_BAD_STATEID:
7510 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7511 if (new_lock_owner != 0 ||
7512 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7513 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7514 break;
7515 case -NFS4ERR_STALE_STATEID:
7516 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7517 nfs4_schedule_lease_recovery(server->nfs_client);
7518 }
7519 }
7520
_nfs4_do_setlk(struct nfs4_state * state,int cmd,struct file_lock * fl,int recovery_type)7521 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7522 {
7523 struct nfs4_lockdata *data;
7524 struct rpc_task *task;
7525 struct rpc_message msg = {
7526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7527 .rpc_cred = state->owner->so_cred,
7528 };
7529 struct rpc_task_setup task_setup_data = {
7530 .rpc_client = NFS_CLIENT(state->inode),
7531 .rpc_message = &msg,
7532 .callback_ops = &nfs4_lock_ops,
7533 .workqueue = nfsiod_workqueue,
7534 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7535 };
7536 int ret;
7537
7538 if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7539 task_setup_data.flags |= RPC_TASK_MOVEABLE;
7540
7541 data = nfs4_alloc_lockdata(fl,
7542 nfs_file_open_context(fl->c.flc_file),
7543 fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7544 if (data == NULL)
7545 return -ENOMEM;
7546 if (IS_SETLKW(cmd))
7547 data->arg.block = 1;
7548 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7549 recovery_type > NFS_LOCK_NEW);
7550 msg.rpc_argp = &data->arg;
7551 msg.rpc_resp = &data->res;
7552 task_setup_data.callback_data = data;
7553 if (recovery_type > NFS_LOCK_NEW) {
7554 if (recovery_type == NFS_LOCK_RECLAIM)
7555 data->arg.reclaim = NFS_LOCK_RECLAIM;
7556 } else
7557 data->arg.new_lock = 1;
7558 task = rpc_run_task(&task_setup_data);
7559 if (IS_ERR(task))
7560 return PTR_ERR(task);
7561 ret = rpc_wait_for_completion_task(task);
7562 if (ret == 0) {
7563 ret = data->rpc_status;
7564 if (ret)
7565 nfs4_handle_setlk_error(data->server, data->lsp,
7566 data->arg.new_lock_owner, ret);
7567 } else
7568 data->cancelled = true;
7569 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7570 rpc_put_task(task);
7571 dprintk("%s: ret = %d\n", __func__, ret);
7572 return ret;
7573 }
7574
nfs4_lock_reclaim(struct nfs4_state * state,struct file_lock * request)7575 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7576 {
7577 struct nfs_server *server = NFS_SERVER(state->inode);
7578 struct nfs4_exception exception = {
7579 .inode = state->inode,
7580 };
7581 int err;
7582
7583 do {
7584 /* Cache the lock if possible... */
7585 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7586 return 0;
7587 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7588 if (err != -NFS4ERR_DELAY)
7589 break;
7590 nfs4_handle_exception(server, err, &exception);
7591 } while (exception.retry);
7592 return err;
7593 }
7594
nfs4_lock_expired(struct nfs4_state * state,struct file_lock * request)7595 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7596 {
7597 struct nfs_server *server = NFS_SERVER(state->inode);
7598 struct nfs4_exception exception = {
7599 .inode = state->inode,
7600 };
7601 int err;
7602
7603 err = nfs4_set_lock_state(state, request);
7604 if (err != 0)
7605 return err;
7606 if (!recover_lost_locks) {
7607 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7608 return 0;
7609 }
7610 do {
7611 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7612 return 0;
7613 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7614 switch (err) {
7615 default:
7616 goto out;
7617 case -NFS4ERR_GRACE:
7618 case -NFS4ERR_DELAY:
7619 nfs4_handle_exception(server, err, &exception);
7620 err = 0;
7621 }
7622 } while (exception.retry);
7623 out:
7624 return err;
7625 }
7626
7627 #if defined(CONFIG_NFS_V4_1)
nfs41_lock_expired(struct nfs4_state * state,struct file_lock * request)7628 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7629 {
7630 struct nfs4_lock_state *lsp;
7631 int status;
7632
7633 status = nfs4_set_lock_state(state, request);
7634 if (status != 0)
7635 return status;
7636 lsp = request->fl_u.nfs4_fl.owner;
7637 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7638 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7639 return 0;
7640 return nfs4_lock_expired(state, request);
7641 }
7642 #endif
7643
_nfs4_proc_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7644 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7645 {
7646 struct nfs_inode *nfsi = NFS_I(state->inode);
7647 struct nfs4_state_owner *sp = state->owner;
7648 unsigned char flags = request->c.flc_flags;
7649 int status;
7650
7651 request->c.flc_flags |= FL_ACCESS;
7652 status = locks_lock_inode_wait(state->inode, request);
7653 if (status < 0)
7654 goto out;
7655 mutex_lock(&sp->so_delegreturn_mutex);
7656 down_read(&nfsi->rwsem);
7657 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7658 /* Yes: cache locks! */
7659 /* ...but avoid races with delegation recall... */
7660 request->c.flc_flags = flags & ~FL_SLEEP;
7661 status = locks_lock_inode_wait(state->inode, request);
7662 up_read(&nfsi->rwsem);
7663 mutex_unlock(&sp->so_delegreturn_mutex);
7664 goto out;
7665 }
7666 up_read(&nfsi->rwsem);
7667 mutex_unlock(&sp->so_delegreturn_mutex);
7668 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7669 out:
7670 request->c.flc_flags = flags;
7671 return status;
7672 }
7673
nfs4_proc_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7674 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7675 {
7676 struct nfs4_exception exception = {
7677 .state = state,
7678 .inode = state->inode,
7679 .interruptible = true,
7680 };
7681 int err;
7682
7683 do {
7684 err = _nfs4_proc_setlk(state, cmd, request);
7685 if (err == -NFS4ERR_DENIED)
7686 err = -EAGAIN;
7687 err = nfs4_handle_exception(NFS_SERVER(state->inode),
7688 err, &exception);
7689 } while (exception.retry);
7690 return err;
7691 }
7692
7693 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7694 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7695
7696 static int
nfs4_retry_setlk_simple(struct nfs4_state * state,int cmd,struct file_lock * request)7697 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7698 struct file_lock *request)
7699 {
7700 int status = -ERESTARTSYS;
7701 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7702
7703 while(!signalled()) {
7704 status = nfs4_proc_setlk(state, cmd, request);
7705 if ((status != -EAGAIN) || IS_SETLK(cmd))
7706 break;
7707 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7708 schedule_timeout(timeout);
7709 timeout *= 2;
7710 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7711 status = -ERESTARTSYS;
7712 }
7713 return status;
7714 }
7715
7716 #ifdef CONFIG_NFS_V4_1
7717 struct nfs4_lock_waiter {
7718 struct inode *inode;
7719 struct nfs_lowner owner;
7720 wait_queue_entry_t wait;
7721 };
7722
7723 static int
nfs4_wake_lock_waiter(wait_queue_entry_t * wait,unsigned int mode,int flags,void * key)7724 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7725 {
7726 struct nfs4_lock_waiter *waiter =
7727 container_of(wait, struct nfs4_lock_waiter, wait);
7728
7729 /* NULL key means to wake up everyone */
7730 if (key) {
7731 struct cb_notify_lock_args *cbnl = key;
7732 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7733 *wowner = &waiter->owner;
7734
7735 /* Only wake if the callback was for the same owner. */
7736 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7737 return 0;
7738
7739 /* Make sure it's for the right inode */
7740 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7741 return 0;
7742 }
7743
7744 return woken_wake_function(wait, mode, flags, key);
7745 }
7746
7747 static int
nfs4_retry_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7748 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7749 {
7750 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7751 struct nfs_server *server = NFS_SERVER(state->inode);
7752 struct nfs_client *clp = server->nfs_client;
7753 wait_queue_head_t *q = &clp->cl_lock_waitq;
7754 struct nfs4_lock_waiter waiter = {
7755 .inode = state->inode,
7756 .owner = { .clientid = clp->cl_clientid,
7757 .id = lsp->ls_seqid.owner_id,
7758 .s_dev = server->s_dev },
7759 };
7760 int status;
7761
7762 /* Don't bother with waitqueue if we don't expect a callback */
7763 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7764 return nfs4_retry_setlk_simple(state, cmd, request);
7765
7766 init_wait(&waiter.wait);
7767 waiter.wait.func = nfs4_wake_lock_waiter;
7768 add_wait_queue(q, &waiter.wait);
7769
7770 do {
7771 status = nfs4_proc_setlk(state, cmd, request);
7772 if (status != -EAGAIN || IS_SETLK(cmd))
7773 break;
7774
7775 status = -ERESTARTSYS;
7776 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7777 NFS4_LOCK_MAXTIMEOUT);
7778 } while (!signalled());
7779
7780 remove_wait_queue(q, &waiter.wait);
7781
7782 return status;
7783 }
7784 #else /* !CONFIG_NFS_V4_1 */
7785 static inline int
nfs4_retry_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7786 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7787 {
7788 return nfs4_retry_setlk_simple(state, cmd, request);
7789 }
7790 #endif
7791
7792 static int
nfs4_proc_lock(struct file * filp,int cmd,struct file_lock * request)7793 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7794 {
7795 struct nfs_open_context *ctx;
7796 struct nfs4_state *state;
7797 int status;
7798
7799 /* verify open state */
7800 ctx = nfs_file_open_context(filp);
7801 state = ctx->state;
7802
7803 if (IS_GETLK(cmd)) {
7804 if (state != NULL)
7805 return nfs4_proc_getlk(state, F_GETLK, request);
7806 return 0;
7807 }
7808
7809 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7810 return -EINVAL;
7811
7812 if (lock_is_unlock(request)) {
7813 if (state != NULL)
7814 return nfs4_proc_unlck(state, cmd, request);
7815 return 0;
7816 }
7817
7818 if (state == NULL)
7819 return -ENOLCK;
7820
7821 if ((request->c.flc_flags & FL_POSIX) &&
7822 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7823 return -ENOLCK;
7824
7825 /*
7826 * Don't rely on the VFS having checked the file open mode,
7827 * since it won't do this for flock() locks.
7828 */
7829 switch (request->c.flc_type) {
7830 case F_RDLCK:
7831 if (!(filp->f_mode & FMODE_READ))
7832 return -EBADF;
7833 break;
7834 case F_WRLCK:
7835 if (!(filp->f_mode & FMODE_WRITE))
7836 return -EBADF;
7837 }
7838
7839 status = nfs4_set_lock_state(state, request);
7840 if (status != 0)
7841 return status;
7842
7843 return nfs4_retry_setlk(state, cmd, request);
7844 }
7845
nfs4_delete_lease(struct file * file,void ** priv)7846 static int nfs4_delete_lease(struct file *file, void **priv)
7847 {
7848 return generic_setlease(file, F_UNLCK, NULL, priv);
7849 }
7850
nfs4_add_lease(struct file * file,int arg,struct file_lease ** lease,void ** priv)7851 static int nfs4_add_lease(struct file *file, int arg, struct file_lease **lease,
7852 void **priv)
7853 {
7854 struct inode *inode = file_inode(file);
7855 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7856 int ret;
7857
7858 /* No delegation, no lease */
7859 if (!nfs4_have_delegation(inode, type, 0))
7860 return -EAGAIN;
7861 ret = generic_setlease(file, arg, lease, priv);
7862 if (ret || nfs4_have_delegation(inode, type, 0))
7863 return ret;
7864 /* We raced with a delegation return */
7865 nfs4_delete_lease(file, priv);
7866 return -EAGAIN;
7867 }
7868
nfs4_proc_setlease(struct file * file,int arg,struct file_lease ** lease,void ** priv)7869 int nfs4_proc_setlease(struct file *file, int arg, struct file_lease **lease,
7870 void **priv)
7871 {
7872 switch (arg) {
7873 case F_RDLCK:
7874 case F_WRLCK:
7875 return nfs4_add_lease(file, arg, lease, priv);
7876 case F_UNLCK:
7877 return nfs4_delete_lease(file, priv);
7878 default:
7879 return -EINVAL;
7880 }
7881 }
7882
nfs4_lock_delegation_recall(struct file_lock * fl,struct nfs4_state * state,const nfs4_stateid * stateid)7883 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7884 {
7885 struct nfs_server *server = NFS_SERVER(state->inode);
7886 int err;
7887
7888 err = nfs4_set_lock_state(state, fl);
7889 if (err != 0)
7890 return err;
7891 do {
7892 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7893 if (err != -NFS4ERR_DELAY && err != -NFS4ERR_GRACE)
7894 break;
7895 ssleep(1);
7896 } while (err == -NFS4ERR_DELAY || err == -NFSERR_GRACE);
7897 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7898 }
7899
7900 struct nfs_release_lockowner_data {
7901 struct nfs4_lock_state *lsp;
7902 struct nfs_server *server;
7903 struct nfs_release_lockowner_args args;
7904 struct nfs_release_lockowner_res res;
7905 unsigned long timestamp;
7906 };
7907
nfs4_release_lockowner_prepare(struct rpc_task * task,void * calldata)7908 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7909 {
7910 struct nfs_release_lockowner_data *data = calldata;
7911 struct nfs_server *server = data->server;
7912 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7913 &data->res.seq_res, task);
7914 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7915 data->timestamp = jiffies;
7916 }
7917
nfs4_release_lockowner_done(struct rpc_task * task,void * calldata)7918 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7919 {
7920 struct nfs_release_lockowner_data *data = calldata;
7921 struct nfs_server *server = data->server;
7922
7923 nfs40_sequence_done(task, &data->res.seq_res);
7924
7925 switch (task->tk_status) {
7926 case 0:
7927 renew_lease(server, data->timestamp);
7928 break;
7929 case -NFS4ERR_STALE_CLIENTID:
7930 case -NFS4ERR_EXPIRED:
7931 nfs4_schedule_lease_recovery(server->nfs_client);
7932 break;
7933 case -NFS4ERR_LEASE_MOVED:
7934 case -NFS4ERR_DELAY:
7935 if (nfs4_async_handle_error(task, server,
7936 NULL, NULL) == -EAGAIN)
7937 rpc_restart_call_prepare(task);
7938 }
7939 }
7940
nfs4_release_lockowner_release(void * calldata)7941 static void nfs4_release_lockowner_release(void *calldata)
7942 {
7943 struct nfs_release_lockowner_data *data = calldata;
7944 nfs4_free_lock_state(data->server, data->lsp);
7945 kfree(calldata);
7946 }
7947
7948 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7949 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7950 .rpc_call_done = nfs4_release_lockowner_done,
7951 .rpc_release = nfs4_release_lockowner_release,
7952 };
7953
7954 static void
nfs4_release_lockowner(struct nfs_server * server,struct nfs4_lock_state * lsp)7955 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7956 {
7957 struct nfs_release_lockowner_data *data;
7958 struct rpc_message msg = {
7959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7960 };
7961
7962 if (server->nfs_client->cl_mvops->minor_version != 0)
7963 return;
7964
7965 data = kmalloc(sizeof(*data), GFP_KERNEL);
7966 if (!data)
7967 return;
7968 data->lsp = lsp;
7969 data->server = server;
7970 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7971 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7972 data->args.lock_owner.s_dev = server->s_dev;
7973
7974 msg.rpc_argp = &data->args;
7975 msg.rpc_resp = &data->res;
7976 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7977 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7978 }
7979
7980 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7981
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)7982 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7983 struct mnt_idmap *idmap,
7984 struct dentry *unused, struct inode *inode,
7985 const char *key, const void *buf,
7986 size_t buflen, int flags)
7987 {
7988 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7989 }
7990
nfs4_xattr_get_nfs4_acl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7991 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7992 struct dentry *unused, struct inode *inode,
7993 const char *key, void *buf, size_t buflen)
7994 {
7995 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7996 }
7997
nfs4_xattr_list_nfs4_acl(struct dentry * dentry)7998 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7999 {
8000 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
8001 }
8002
8003 #if defined(CONFIG_NFS_V4_1)
8004 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
8005
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)8006 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
8007 struct mnt_idmap *idmap,
8008 struct dentry *unused, struct inode *inode,
8009 const char *key, const void *buf,
8010 size_t buflen, int flags)
8011 {
8012 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
8013 }
8014
nfs4_xattr_get_nfs4_dacl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)8015 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
8016 struct dentry *unused, struct inode *inode,
8017 const char *key, void *buf, size_t buflen)
8018 {
8019 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
8020 }
8021
nfs4_xattr_list_nfs4_dacl(struct dentry * dentry)8022 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
8023 {
8024 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
8025 }
8026
8027 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
8028
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)8029 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
8030 struct mnt_idmap *idmap,
8031 struct dentry *unused, struct inode *inode,
8032 const char *key, const void *buf,
8033 size_t buflen, int flags)
8034 {
8035 return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
8036 }
8037
nfs4_xattr_get_nfs4_sacl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)8038 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
8039 struct dentry *unused, struct inode *inode,
8040 const char *key, void *buf, size_t buflen)
8041 {
8042 return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
8043 }
8044
nfs4_xattr_list_nfs4_sacl(struct dentry * dentry)8045 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
8046 {
8047 return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
8048 }
8049
8050 #endif
8051
8052 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8053
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)8054 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
8055 struct mnt_idmap *idmap,
8056 struct dentry *unused, struct inode *inode,
8057 const char *key, const void *buf,
8058 size_t buflen, int flags)
8059 {
8060 if (security_ismaclabel(key))
8061 return nfs4_set_security_label(inode, buf, buflen);
8062
8063 return -EOPNOTSUPP;
8064 }
8065
nfs4_xattr_get_nfs4_label(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)8066 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
8067 struct dentry *unused, struct inode *inode,
8068 const char *key, void *buf, size_t buflen)
8069 {
8070 if (security_ismaclabel(key))
8071 return nfs4_get_security_label(inode, buf, buflen);
8072 return -EOPNOTSUPP;
8073 }
8074
8075 static ssize_t
nfs4_listxattr_nfs4_label(struct inode * inode,char * list,size_t list_len)8076 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
8077 {
8078 int len = 0;
8079
8080 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
8081 len = security_inode_listsecurity(inode, list, list_len);
8082 if (len >= 0 && list_len && len > list_len)
8083 return -ERANGE;
8084 }
8085 return len;
8086 }
8087
8088 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
8089 .prefix = XATTR_SECURITY_PREFIX,
8090 .get = nfs4_xattr_get_nfs4_label,
8091 .set = nfs4_xattr_set_nfs4_label,
8092 };
8093
8094 #else
8095
8096 static ssize_t
nfs4_listxattr_nfs4_label(struct inode * inode,char * list,size_t list_len)8097 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
8098 {
8099 return 0;
8100 }
8101
8102 #endif
8103
8104 #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)8105 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
8106 struct mnt_idmap *idmap,
8107 struct dentry *unused, struct inode *inode,
8108 const char *key, const void *buf,
8109 size_t buflen, int flags)
8110 {
8111 u32 mask;
8112 int ret;
8113
8114 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8115 return -EOPNOTSUPP;
8116
8117 /*
8118 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
8119 * flags right now. Handling of xattr operations use the normal
8120 * file read/write permissions.
8121 *
8122 * Just in case the server has other ideas (which RFC 8276 allows),
8123 * do a cached access check for the XA* flags to possibly avoid
8124 * doing an RPC and getting EACCES back.
8125 */
8126 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8127 if (!(mask & NFS_ACCESS_XAWRITE))
8128 return -EACCES;
8129 }
8130
8131 if (buf == NULL) {
8132 ret = nfs42_proc_removexattr(inode, key);
8133 if (!ret)
8134 nfs4_xattr_cache_remove(inode, key);
8135 } else {
8136 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
8137 if (!ret)
8138 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
8139 }
8140
8141 return ret;
8142 }
8143
nfs4_xattr_get_nfs4_user(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)8144 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
8145 struct dentry *unused, struct inode *inode,
8146 const char *key, void *buf, size_t buflen)
8147 {
8148 u32 mask;
8149 ssize_t ret;
8150
8151 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8152 return -EOPNOTSUPP;
8153
8154 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8155 if (!(mask & NFS_ACCESS_XAREAD))
8156 return -EACCES;
8157 }
8158
8159 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
8160 if (ret)
8161 return ret;
8162
8163 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
8164 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
8165 return ret;
8166
8167 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
8168
8169 return ret;
8170 }
8171
8172 static ssize_t
nfs4_listxattr_nfs4_user(struct inode * inode,char * list,size_t list_len)8173 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
8174 {
8175 u64 cookie;
8176 bool eof;
8177 ssize_t ret, size;
8178 char *buf;
8179 size_t buflen;
8180 u32 mask;
8181
8182 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8183 return 0;
8184
8185 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8186 if (!(mask & NFS_ACCESS_XALIST))
8187 return 0;
8188 }
8189
8190 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
8191 if (ret)
8192 return ret;
8193
8194 ret = nfs4_xattr_cache_list(inode, list, list_len);
8195 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
8196 return ret;
8197
8198 cookie = 0;
8199 eof = false;
8200 buflen = list_len ? list_len : XATTR_LIST_MAX;
8201 buf = list_len ? list : NULL;
8202 size = 0;
8203
8204 while (!eof) {
8205 ret = nfs42_proc_listxattrs(inode, buf, buflen,
8206 &cookie, &eof);
8207 if (ret < 0)
8208 return ret;
8209
8210 if (list_len) {
8211 buf += ret;
8212 buflen -= ret;
8213 }
8214 size += ret;
8215 }
8216
8217 if (list_len)
8218 nfs4_xattr_cache_set_list(inode, list, size);
8219
8220 return size;
8221 }
8222
8223 #else
8224
8225 static ssize_t
nfs4_listxattr_nfs4_user(struct inode * inode,char * list,size_t list_len)8226 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
8227 {
8228 return 0;
8229 }
8230 #endif /* CONFIG_NFS_V4_2 */
8231
8232 /*
8233 * nfs_fhget will use either the mounted_on_fileid or the fileid
8234 */
nfs_fixup_referral_attributes(struct nfs_fattr * fattr)8235 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
8236 {
8237 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
8238 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
8239 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
8240 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
8241 return;
8242
8243 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
8244 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
8245 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
8246 fattr->nlink = 2;
8247 }
8248
_nfs4_proc_fs_locations(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs4_fs_locations * fs_locations,struct page * page)8249 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8250 const struct qstr *name,
8251 struct nfs4_fs_locations *fs_locations,
8252 struct page *page)
8253 {
8254 struct nfs_server *server = NFS_SERVER(dir);
8255 u32 bitmask[3];
8256 struct nfs4_fs_locations_arg args = {
8257 .dir_fh = NFS_FH(dir),
8258 .name = name,
8259 .page = page,
8260 .bitmask = bitmask,
8261 };
8262 struct nfs4_fs_locations_res res = {
8263 .fs_locations = fs_locations,
8264 };
8265 struct rpc_message msg = {
8266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8267 .rpc_argp = &args,
8268 .rpc_resp = &res,
8269 };
8270 int status;
8271
8272 dprintk("%s: start\n", __func__);
8273
8274 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8275 bitmask[1] = nfs4_fattr_bitmap[1];
8276
8277 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
8278 * is not supported */
8279 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8280 bitmask[0] &= ~FATTR4_WORD0_FILEID;
8281 else
8282 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8283
8284 nfs_fattr_init(fs_locations->fattr);
8285 fs_locations->server = server;
8286 fs_locations->nlocations = 0;
8287 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8288 dprintk("%s: returned status = %d\n", __func__, status);
8289 return status;
8290 }
8291
nfs4_proc_fs_locations(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs4_fs_locations * fs_locations,struct page * page)8292 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8293 const struct qstr *name,
8294 struct nfs4_fs_locations *fs_locations,
8295 struct page *page)
8296 {
8297 struct nfs4_exception exception = {
8298 .interruptible = true,
8299 };
8300 int err;
8301 do {
8302 err = _nfs4_proc_fs_locations(client, dir, name,
8303 fs_locations, page);
8304 trace_nfs4_get_fs_locations(dir, name, err);
8305 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8306 &exception);
8307 } while (exception.retry);
8308 return err;
8309 }
8310
8311 /*
8312 * This operation also signals the server that this client is
8313 * performing migration recovery. The server can stop returning
8314 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
8315 * appended to this compound to identify the client ID which is
8316 * performing recovery.
8317 */
_nfs40_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8318 static int _nfs40_proc_get_locations(struct nfs_server *server,
8319 struct nfs_fh *fhandle,
8320 struct nfs4_fs_locations *locations,
8321 struct page *page, const struct cred *cred)
8322 {
8323 struct rpc_clnt *clnt = server->client;
8324 u32 bitmask[2] = {
8325 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8326 };
8327 struct nfs4_fs_locations_arg args = {
8328 .clientid = server->nfs_client->cl_clientid,
8329 .fh = fhandle,
8330 .page = page,
8331 .bitmask = bitmask,
8332 .migration = 1, /* skip LOOKUP */
8333 .renew = 1, /* append RENEW */
8334 };
8335 struct nfs4_fs_locations_res res = {
8336 .fs_locations = locations,
8337 .migration = 1,
8338 .renew = 1,
8339 };
8340 struct rpc_message msg = {
8341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8342 .rpc_argp = &args,
8343 .rpc_resp = &res,
8344 .rpc_cred = cred,
8345 };
8346 unsigned long now = jiffies;
8347 int status;
8348
8349 nfs_fattr_init(locations->fattr);
8350 locations->server = server;
8351 locations->nlocations = 0;
8352
8353 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8354 status = nfs4_call_sync_sequence(clnt, server, &msg,
8355 &args.seq_args, &res.seq_res);
8356 if (status)
8357 return status;
8358
8359 renew_lease(server, now);
8360 return 0;
8361 }
8362
8363 #ifdef CONFIG_NFS_V4_1
8364
8365 /*
8366 * This operation also signals the server that this client is
8367 * performing migration recovery. The server can stop asserting
8368 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
8369 * performing this operation is identified in the SEQUENCE
8370 * operation in this compound.
8371 *
8372 * When the client supports GETATTR(fs_locations_info), it can
8373 * be plumbed in here.
8374 */
_nfs41_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8375 static int _nfs41_proc_get_locations(struct nfs_server *server,
8376 struct nfs_fh *fhandle,
8377 struct nfs4_fs_locations *locations,
8378 struct page *page, const struct cred *cred)
8379 {
8380 struct rpc_clnt *clnt = server->client;
8381 u32 bitmask[2] = {
8382 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8383 };
8384 struct nfs4_fs_locations_arg args = {
8385 .fh = fhandle,
8386 .page = page,
8387 .bitmask = bitmask,
8388 .migration = 1, /* skip LOOKUP */
8389 };
8390 struct nfs4_fs_locations_res res = {
8391 .fs_locations = locations,
8392 .migration = 1,
8393 };
8394 struct rpc_message msg = {
8395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8396 .rpc_argp = &args,
8397 .rpc_resp = &res,
8398 .rpc_cred = cred,
8399 };
8400 struct nfs4_call_sync_data data = {
8401 .seq_server = server,
8402 .seq_args = &args.seq_args,
8403 .seq_res = &res.seq_res,
8404 };
8405 struct rpc_task_setup task_setup_data = {
8406 .rpc_client = clnt,
8407 .rpc_message = &msg,
8408 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8409 .callback_data = &data,
8410 .flags = RPC_TASK_NO_ROUND_ROBIN,
8411 };
8412 int status;
8413
8414 nfs_fattr_init(locations->fattr);
8415 locations->server = server;
8416 locations->nlocations = 0;
8417
8418 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8419 status = nfs4_call_sync_custom(&task_setup_data);
8420 if (status == NFS4_OK &&
8421 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8422 status = -NFS4ERR_LEASE_MOVED;
8423 return status;
8424 }
8425
8426 #endif /* CONFIG_NFS_V4_1 */
8427
8428 /**
8429 * nfs4_proc_get_locations - discover locations for a migrated FSID
8430 * @server: pointer to nfs_server to process
8431 * @fhandle: pointer to the kernel NFS client file handle
8432 * @locations: result of query
8433 * @page: buffer
8434 * @cred: credential to use for this operation
8435 *
8436 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8437 * operation failed, or a negative errno if a local error occurred.
8438 *
8439 * On success, "locations" is filled in, but if the server has
8440 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8441 * asserted.
8442 *
8443 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8444 * from this client that require migration recovery.
8445 */
nfs4_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8446 int nfs4_proc_get_locations(struct nfs_server *server,
8447 struct nfs_fh *fhandle,
8448 struct nfs4_fs_locations *locations,
8449 struct page *page, const struct cred *cred)
8450 {
8451 struct nfs_client *clp = server->nfs_client;
8452 const struct nfs4_mig_recovery_ops *ops =
8453 clp->cl_mvops->mig_recovery_ops;
8454 struct nfs4_exception exception = {
8455 .interruptible = true,
8456 };
8457 int status;
8458
8459 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8460 (unsigned long long)server->fsid.major,
8461 (unsigned long long)server->fsid.minor,
8462 clp->cl_hostname);
8463 nfs_display_fhandle(fhandle, __func__);
8464
8465 do {
8466 status = ops->get_locations(server, fhandle, locations, page,
8467 cred);
8468 if (status != -NFS4ERR_DELAY)
8469 break;
8470 nfs4_handle_exception(server, status, &exception);
8471 } while (exception.retry);
8472 return status;
8473 }
8474
8475 /*
8476 * This operation also signals the server that this client is
8477 * performing "lease moved" recovery. The server can stop
8478 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8479 * is appended to this compound to identify the client ID which is
8480 * performing recovery.
8481 */
_nfs40_proc_fsid_present(struct inode * inode,const struct cred * cred)8482 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8483 {
8484 struct nfs_server *server = NFS_SERVER(inode);
8485 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8486 struct rpc_clnt *clnt = server->client;
8487 struct nfs4_fsid_present_arg args = {
8488 .fh = NFS_FH(inode),
8489 .clientid = clp->cl_clientid,
8490 .renew = 1, /* append RENEW */
8491 };
8492 struct nfs4_fsid_present_res res = {
8493 .renew = 1,
8494 };
8495 struct rpc_message msg = {
8496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8497 .rpc_argp = &args,
8498 .rpc_resp = &res,
8499 .rpc_cred = cred,
8500 };
8501 unsigned long now = jiffies;
8502 int status;
8503
8504 res.fh = nfs_alloc_fhandle();
8505 if (res.fh == NULL)
8506 return -ENOMEM;
8507
8508 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8509 status = nfs4_call_sync_sequence(clnt, server, &msg,
8510 &args.seq_args, &res.seq_res);
8511 nfs_free_fhandle(res.fh);
8512 if (status)
8513 return status;
8514
8515 do_renew_lease(clp, now);
8516 return 0;
8517 }
8518
8519 #ifdef CONFIG_NFS_V4_1
8520
8521 /*
8522 * This operation also signals the server that this client is
8523 * performing "lease moved" recovery. The server can stop asserting
8524 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8525 * this operation is identified in the SEQUENCE operation in this
8526 * compound.
8527 */
_nfs41_proc_fsid_present(struct inode * inode,const struct cred * cred)8528 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8529 {
8530 struct nfs_server *server = NFS_SERVER(inode);
8531 struct rpc_clnt *clnt = server->client;
8532 struct nfs4_fsid_present_arg args = {
8533 .fh = NFS_FH(inode),
8534 };
8535 struct nfs4_fsid_present_res res = {
8536 };
8537 struct rpc_message msg = {
8538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8539 .rpc_argp = &args,
8540 .rpc_resp = &res,
8541 .rpc_cred = cred,
8542 };
8543 int status;
8544
8545 res.fh = nfs_alloc_fhandle();
8546 if (res.fh == NULL)
8547 return -ENOMEM;
8548
8549 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8550 status = nfs4_call_sync_sequence(clnt, server, &msg,
8551 &args.seq_args, &res.seq_res);
8552 nfs_free_fhandle(res.fh);
8553 if (status == NFS4_OK &&
8554 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8555 status = -NFS4ERR_LEASE_MOVED;
8556 return status;
8557 }
8558
8559 #endif /* CONFIG_NFS_V4_1 */
8560
8561 /**
8562 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8563 * @inode: inode on FSID to check
8564 * @cred: credential to use for this operation
8565 *
8566 * Server indicates whether the FSID is present, moved, or not
8567 * recognized. This operation is necessary to clear a LEASE_MOVED
8568 * condition for this client ID.
8569 *
8570 * Returns NFS4_OK if the FSID is present on this server,
8571 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8572 * NFS4ERR code if some error occurred on the server, or a
8573 * negative errno if a local failure occurred.
8574 */
nfs4_proc_fsid_present(struct inode * inode,const struct cred * cred)8575 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8576 {
8577 struct nfs_server *server = NFS_SERVER(inode);
8578 struct nfs_client *clp = server->nfs_client;
8579 const struct nfs4_mig_recovery_ops *ops =
8580 clp->cl_mvops->mig_recovery_ops;
8581 struct nfs4_exception exception = {
8582 .interruptible = true,
8583 };
8584 int status;
8585
8586 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8587 (unsigned long long)server->fsid.major,
8588 (unsigned long long)server->fsid.minor,
8589 clp->cl_hostname);
8590 nfs_display_fhandle(NFS_FH(inode), __func__);
8591
8592 do {
8593 status = ops->fsid_present(inode, cred);
8594 if (status != -NFS4ERR_DELAY)
8595 break;
8596 nfs4_handle_exception(server, status, &exception);
8597 } while (exception.retry);
8598 return status;
8599 }
8600
8601 /*
8602 * If 'use_integrity' is true and the state managment nfs_client
8603 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8604 * and the machine credential as per RFC3530bis and RFC5661 Security
8605 * Considerations sections. Otherwise, just use the user cred with the
8606 * filesystem's rpc_client.
8607 */
_nfs4_proc_secinfo(struct inode * dir,const struct qstr * name,struct nfs4_secinfo_flavors * flavors,bool use_integrity)8608 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8609 {
8610 int status;
8611 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8612 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8613 struct nfs4_secinfo_arg args = {
8614 .dir_fh = NFS_FH(dir),
8615 .name = name,
8616 };
8617 struct nfs4_secinfo_res res = {
8618 .flavors = flavors,
8619 };
8620 struct rpc_message msg = {
8621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8622 .rpc_argp = &args,
8623 .rpc_resp = &res,
8624 };
8625 struct nfs4_call_sync_data data = {
8626 .seq_server = NFS_SERVER(dir),
8627 .seq_args = &args.seq_args,
8628 .seq_res = &res.seq_res,
8629 };
8630 struct rpc_task_setup task_setup = {
8631 .rpc_client = clnt,
8632 .rpc_message = &msg,
8633 .callback_ops = clp->cl_mvops->call_sync_ops,
8634 .callback_data = &data,
8635 .flags = RPC_TASK_NO_ROUND_ROBIN,
8636 };
8637 const struct cred *cred = NULL;
8638
8639 if (use_integrity) {
8640 clnt = clp->cl_rpcclient;
8641 task_setup.rpc_client = clnt;
8642
8643 cred = nfs4_get_clid_cred(clp);
8644 msg.rpc_cred = cred;
8645 }
8646
8647 dprintk("NFS call secinfo %s\n", name->name);
8648
8649 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8650 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8651 status = nfs4_call_sync_custom(&task_setup);
8652
8653 dprintk("NFS reply secinfo: %d\n", status);
8654
8655 put_cred(cred);
8656 return status;
8657 }
8658
nfs4_proc_secinfo(struct inode * dir,const struct qstr * name,struct nfs4_secinfo_flavors * flavors)8659 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8660 struct nfs4_secinfo_flavors *flavors)
8661 {
8662 struct nfs4_exception exception = {
8663 .interruptible = true,
8664 };
8665 int err;
8666 do {
8667 err = -NFS4ERR_WRONGSEC;
8668
8669 /* try to use integrity protection with machine cred */
8670 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8671 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8672
8673 /*
8674 * if unable to use integrity protection, or SECINFO with
8675 * integrity protection returns NFS4ERR_WRONGSEC (which is
8676 * disallowed by spec, but exists in deployed servers) use
8677 * the current filesystem's rpc_client and the user cred.
8678 */
8679 if (err == -NFS4ERR_WRONGSEC)
8680 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8681
8682 trace_nfs4_secinfo(dir, name, err);
8683 err = nfs4_handle_exception(NFS_SERVER(dir), err,
8684 &exception);
8685 } while (exception.retry);
8686 return err;
8687 }
8688
8689 #ifdef CONFIG_NFS_V4_1
8690 /*
8691 * Check the exchange flags returned by the server for invalid flags, having
8692 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8693 * DS flags set.
8694 */
nfs4_check_cl_exchange_flags(u32 flags,u32 version)8695 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8696 {
8697 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8698 goto out_inval;
8699 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8700 goto out_inval;
8701 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8702 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8703 goto out_inval;
8704 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8705 goto out_inval;
8706 return NFS_OK;
8707 out_inval:
8708 return -NFS4ERR_INVAL;
8709 }
8710
8711 static bool
nfs41_same_server_scope(struct nfs41_server_scope * a,struct nfs41_server_scope * b)8712 nfs41_same_server_scope(struct nfs41_server_scope *a,
8713 struct nfs41_server_scope *b)
8714 {
8715 if (a->server_scope_sz != b->server_scope_sz)
8716 return false;
8717 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8718 }
8719
8720 static void
nfs4_bind_one_conn_to_session_done(struct rpc_task * task,void * calldata)8721 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8722 {
8723 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8724 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8725 struct nfs_client *clp = args->client;
8726
8727 switch (task->tk_status) {
8728 case -NFS4ERR_BADSESSION:
8729 case -NFS4ERR_DEADSESSION:
8730 nfs4_schedule_session_recovery(clp->cl_session,
8731 task->tk_status);
8732 return;
8733 }
8734 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8735 res->dir != NFS4_CDFS4_BOTH) {
8736 rpc_task_close_connection(task);
8737 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8738 rpc_restart_call(task);
8739 }
8740 }
8741
8742 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8743 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
8744 };
8745
8746 /*
8747 * nfs4_proc_bind_one_conn_to_session()
8748 *
8749 * The 4.1 client currently uses the same TCP connection for the
8750 * fore and backchannel.
8751 */
8752 static
nfs4_proc_bind_one_conn_to_session(struct rpc_clnt * clnt,struct rpc_xprt * xprt,struct nfs_client * clp,const struct cred * cred)8753 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8754 struct rpc_xprt *xprt,
8755 struct nfs_client *clp,
8756 const struct cred *cred)
8757 {
8758 int status;
8759 struct nfs41_bind_conn_to_session_args args = {
8760 .client = clp,
8761 .dir = NFS4_CDFC4_FORE_OR_BOTH,
8762 .retries = 0,
8763 };
8764 struct nfs41_bind_conn_to_session_res res;
8765 struct rpc_message msg = {
8766 .rpc_proc =
8767 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8768 .rpc_argp = &args,
8769 .rpc_resp = &res,
8770 .rpc_cred = cred,
8771 };
8772 struct rpc_task_setup task_setup_data = {
8773 .rpc_client = clnt,
8774 .rpc_xprt = xprt,
8775 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
8776 .rpc_message = &msg,
8777 .flags = RPC_TASK_TIMEOUT,
8778 };
8779 struct rpc_task *task;
8780
8781 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8782 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8783 args.dir = NFS4_CDFC4_FORE;
8784
8785 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8786 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8787 args.dir = NFS4_CDFC4_FORE;
8788
8789 task = rpc_run_task(&task_setup_data);
8790 if (!IS_ERR(task)) {
8791 status = task->tk_status;
8792 rpc_put_task(task);
8793 } else
8794 status = PTR_ERR(task);
8795 trace_nfs4_bind_conn_to_session(clp, status);
8796 if (status == 0) {
8797 if (memcmp(res.sessionid.data,
8798 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8799 dprintk("NFS: %s: Session ID mismatch\n", __func__);
8800 return -EIO;
8801 }
8802 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8803 dprintk("NFS: %s: Unexpected direction from server\n",
8804 __func__);
8805 return -EIO;
8806 }
8807 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8808 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8809 __func__);
8810 return -EIO;
8811 }
8812 }
8813
8814 return status;
8815 }
8816
8817 struct rpc_bind_conn_calldata {
8818 struct nfs_client *clp;
8819 const struct cred *cred;
8820 };
8821
8822 static int
nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt * clnt,struct rpc_xprt * xprt,void * calldata)8823 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8824 struct rpc_xprt *xprt,
8825 void *calldata)
8826 {
8827 struct rpc_bind_conn_calldata *p = calldata;
8828
8829 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8830 }
8831
nfs4_proc_bind_conn_to_session(struct nfs_client * clp,const struct cred * cred)8832 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8833 {
8834 struct rpc_bind_conn_calldata data = {
8835 .clp = clp,
8836 .cred = cred,
8837 };
8838 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8839 nfs4_proc_bind_conn_to_session_callback, &data);
8840 }
8841
8842 /*
8843 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8844 * and operations we'd like to see to enable certain features in the allow map
8845 */
8846 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8847 .how = SP4_MACH_CRED,
8848 .enforce.u.words = {
8849 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8850 1 << (OP_EXCHANGE_ID - 32) |
8851 1 << (OP_CREATE_SESSION - 32) |
8852 1 << (OP_DESTROY_SESSION - 32) |
8853 1 << (OP_DESTROY_CLIENTID - 32)
8854 },
8855 .allow.u.words = {
8856 [0] = 1 << (OP_CLOSE) |
8857 1 << (OP_OPEN_DOWNGRADE) |
8858 1 << (OP_LOCKU) |
8859 1 << (OP_DELEGRETURN) |
8860 1 << (OP_COMMIT),
8861 [1] = 1 << (OP_SECINFO - 32) |
8862 1 << (OP_SECINFO_NO_NAME - 32) |
8863 1 << (OP_LAYOUTRETURN - 32) |
8864 1 << (OP_TEST_STATEID - 32) |
8865 1 << (OP_FREE_STATEID - 32) |
8866 1 << (OP_WRITE - 32)
8867 }
8868 };
8869
8870 /*
8871 * Select the state protection mode for client `clp' given the server results
8872 * from exchange_id in `sp'.
8873 *
8874 * Returns 0 on success, negative errno otherwise.
8875 */
nfs4_sp4_select_mode(struct nfs_client * clp,struct nfs41_state_protection * sp)8876 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8877 struct nfs41_state_protection *sp)
8878 {
8879 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8880 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8881 1 << (OP_EXCHANGE_ID - 32) |
8882 1 << (OP_CREATE_SESSION - 32) |
8883 1 << (OP_DESTROY_SESSION - 32) |
8884 1 << (OP_DESTROY_CLIENTID - 32)
8885 };
8886 unsigned long flags = 0;
8887 unsigned int i;
8888 int ret = 0;
8889
8890 if (sp->how == SP4_MACH_CRED) {
8891 /* Print state protect result */
8892 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8893 for (i = 0; i <= LAST_NFS4_OP; i++) {
8894 if (test_bit(i, sp->enforce.u.longs))
8895 dfprintk(MOUNT, " enforce op %d\n", i);
8896 if (test_bit(i, sp->allow.u.longs))
8897 dfprintk(MOUNT, " allow op %d\n", i);
8898 }
8899
8900 /* make sure nothing is on enforce list that isn't supported */
8901 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8902 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8903 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8904 ret = -EINVAL;
8905 goto out;
8906 }
8907 }
8908
8909 /*
8910 * Minimal mode - state operations are allowed to use machine
8911 * credential. Note this already happens by default, so the
8912 * client doesn't have to do anything more than the negotiation.
8913 *
8914 * NOTE: we don't care if EXCHANGE_ID is in the list -
8915 * we're already using the machine cred for exchange_id
8916 * and will never use a different cred.
8917 */
8918 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8919 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8920 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8921 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8922 dfprintk(MOUNT, "sp4_mach_cred:\n");
8923 dfprintk(MOUNT, " minimal mode enabled\n");
8924 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8925 } else {
8926 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8927 ret = -EINVAL;
8928 goto out;
8929 }
8930
8931 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8932 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8933 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8934 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8935 dfprintk(MOUNT, " cleanup mode enabled\n");
8936 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8937 }
8938
8939 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8940 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
8941 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8942 }
8943
8944 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8945 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8946 dfprintk(MOUNT, " secinfo mode enabled\n");
8947 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8948 }
8949
8950 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8951 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8952 dfprintk(MOUNT, " stateid mode enabled\n");
8953 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8954 }
8955
8956 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8957 dfprintk(MOUNT, " write mode enabled\n");
8958 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8959 }
8960
8961 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8962 dfprintk(MOUNT, " commit mode enabled\n");
8963 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8964 }
8965 }
8966 out:
8967 clp->cl_sp4_flags = flags;
8968 return ret;
8969 }
8970
8971 struct nfs41_exchange_id_data {
8972 struct nfs41_exchange_id_res res;
8973 struct nfs41_exchange_id_args args;
8974 };
8975
nfs4_exchange_id_release(void * data)8976 static void nfs4_exchange_id_release(void *data)
8977 {
8978 struct nfs41_exchange_id_data *cdata =
8979 (struct nfs41_exchange_id_data *)data;
8980
8981 nfs_put_client(cdata->args.client);
8982 kfree(cdata->res.impl_id);
8983 kfree(cdata->res.server_scope);
8984 kfree(cdata->res.server_owner);
8985 kfree(cdata);
8986 }
8987
8988 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8989 .rpc_release = nfs4_exchange_id_release,
8990 };
8991
8992 /*
8993 * _nfs4_proc_exchange_id()
8994 *
8995 * Wrapper for EXCHANGE_ID operation.
8996 */
8997 static struct rpc_task *
nfs4_run_exchange_id(struct nfs_client * clp,const struct cred * cred,u32 sp4_how,struct rpc_xprt * xprt)8998 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8999 u32 sp4_how, struct rpc_xprt *xprt)
9000 {
9001 struct rpc_message msg = {
9002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
9003 .rpc_cred = cred,
9004 };
9005 struct rpc_task_setup task_setup_data = {
9006 .rpc_client = clp->cl_rpcclient,
9007 .callback_ops = &nfs4_exchange_id_call_ops,
9008 .rpc_message = &msg,
9009 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
9010 };
9011 struct nfs41_exchange_id_data *calldata;
9012 int status;
9013
9014 if (!refcount_inc_not_zero(&clp->cl_count))
9015 return ERR_PTR(-EIO);
9016
9017 status = -ENOMEM;
9018 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9019 if (!calldata)
9020 goto out;
9021
9022 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
9023
9024 status = nfs4_init_uniform_client_string(clp);
9025 if (status)
9026 goto out_calldata;
9027
9028 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
9029 GFP_NOFS);
9030 status = -ENOMEM;
9031 if (unlikely(calldata->res.server_owner == NULL))
9032 goto out_calldata;
9033
9034 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
9035 GFP_NOFS);
9036 if (unlikely(calldata->res.server_scope == NULL))
9037 goto out_server_owner;
9038
9039 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
9040 if (unlikely(calldata->res.impl_id == NULL))
9041 goto out_server_scope;
9042
9043 switch (sp4_how) {
9044 case SP4_NONE:
9045 calldata->args.state_protect.how = SP4_NONE;
9046 break;
9047
9048 case SP4_MACH_CRED:
9049 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
9050 break;
9051
9052 default:
9053 /* unsupported! */
9054 WARN_ON_ONCE(1);
9055 status = -EINVAL;
9056 goto out_impl_id;
9057 }
9058 if (xprt) {
9059 task_setup_data.rpc_xprt = xprt;
9060 task_setup_data.flags |= RPC_TASK_SOFTCONN;
9061 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
9062 sizeof(calldata->args.verifier.data));
9063 }
9064 calldata->args.client = clp;
9065 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
9066 EXCHGID4_FLAG_BIND_PRINC_STATEID;
9067 #ifdef CONFIG_NFS_V4_1_MIGRATION
9068 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
9069 #endif
9070 if (test_bit(NFS_CS_PNFS, &clp->cl_flags))
9071 calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
9072 msg.rpc_argp = &calldata->args;
9073 msg.rpc_resp = &calldata->res;
9074 task_setup_data.callback_data = calldata;
9075
9076 return rpc_run_task(&task_setup_data);
9077
9078 out_impl_id:
9079 kfree(calldata->res.impl_id);
9080 out_server_scope:
9081 kfree(calldata->res.server_scope);
9082 out_server_owner:
9083 kfree(calldata->res.server_owner);
9084 out_calldata:
9085 kfree(calldata);
9086 out:
9087 nfs_put_client(clp);
9088 return ERR_PTR(status);
9089 }
9090
9091 /*
9092 * _nfs4_proc_exchange_id()
9093 *
9094 * Wrapper for EXCHANGE_ID operation.
9095 */
_nfs4_proc_exchange_id(struct nfs_client * clp,const struct cred * cred,u32 sp4_how)9096 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
9097 u32 sp4_how)
9098 {
9099 struct rpc_task *task;
9100 struct nfs41_exchange_id_args *argp;
9101 struct nfs41_exchange_id_res *resp;
9102 unsigned long now = jiffies;
9103 int status;
9104
9105 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
9106 if (IS_ERR(task))
9107 return PTR_ERR(task);
9108
9109 argp = task->tk_msg.rpc_argp;
9110 resp = task->tk_msg.rpc_resp;
9111 status = task->tk_status;
9112 if (status != 0)
9113 goto out;
9114
9115 status = nfs4_check_cl_exchange_flags(resp->flags,
9116 clp->cl_mvops->minor_version);
9117 if (status != 0)
9118 goto out;
9119
9120 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
9121 if (status != 0)
9122 goto out;
9123
9124 do_renew_lease(clp, now);
9125
9126 clp->cl_clientid = resp->clientid;
9127 clp->cl_exchange_flags = resp->flags;
9128 clp->cl_seqid = resp->seqid;
9129 /* Client ID is not confirmed */
9130 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
9131 clear_bit(NFS4_SESSION_ESTABLISHED,
9132 &clp->cl_session->session_state);
9133
9134 if (clp->cl_serverscope != NULL &&
9135 !nfs41_same_server_scope(clp->cl_serverscope,
9136 resp->server_scope)) {
9137 dprintk("%s: server_scope mismatch detected\n",
9138 __func__);
9139 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
9140 }
9141
9142 swap(clp->cl_serverowner, resp->server_owner);
9143 swap(clp->cl_serverscope, resp->server_scope);
9144 swap(clp->cl_implid, resp->impl_id);
9145
9146 /* Save the EXCHANGE_ID verifier session trunk tests */
9147 memcpy(clp->cl_confirm.data, argp->verifier.data,
9148 sizeof(clp->cl_confirm.data));
9149 out:
9150 trace_nfs4_exchange_id(clp, status);
9151 rpc_put_task(task);
9152 return status;
9153 }
9154
9155 /*
9156 * nfs4_proc_exchange_id()
9157 *
9158 * Returns zero, a negative errno, or a negative NFS4ERR status code.
9159 *
9160 * Since the clientid has expired, all compounds using sessions
9161 * associated with the stale clientid will be returning
9162 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
9163 * be in some phase of session reset.
9164 *
9165 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
9166 */
nfs4_proc_exchange_id(struct nfs_client * clp,const struct cred * cred)9167 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
9168 {
9169 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
9170 int status;
9171
9172 /* try SP4_MACH_CRED if krb5i/p */
9173 if (authflavor == RPC_AUTH_GSS_KRB5I ||
9174 authflavor == RPC_AUTH_GSS_KRB5P) {
9175 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
9176 if (!status)
9177 return 0;
9178 }
9179
9180 /* try SP4_NONE */
9181 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
9182 }
9183
9184 /**
9185 * nfs4_test_session_trunk
9186 *
9187 * This is an add_xprt_test() test function called from
9188 * rpc_clnt_setup_test_and_add_xprt.
9189 *
9190 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
9191 * and is dereferrenced in nfs4_exchange_id_release
9192 *
9193 * Upon success, add the new transport to the rpc_clnt
9194 *
9195 * @clnt: struct rpc_clnt to get new transport
9196 * @xprt: the rpc_xprt to test
9197 * @data: call data for _nfs4_proc_exchange_id.
9198 */
nfs4_test_session_trunk(struct rpc_clnt * clnt,struct rpc_xprt * xprt,void * data)9199 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
9200 void *data)
9201 {
9202 struct nfs4_add_xprt_data *adata = data;
9203 struct rpc_task *task;
9204 int status;
9205
9206 u32 sp4_how;
9207
9208 dprintk("--> %s try %s\n", __func__,
9209 xprt->address_strings[RPC_DISPLAY_ADDR]);
9210
9211 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
9212
9213 try_again:
9214 /* Test connection for session trunking. Async exchange_id call */
9215 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
9216 if (IS_ERR(task))
9217 return;
9218
9219 status = task->tk_status;
9220 if (status == 0) {
9221 status = nfs4_detect_session_trunking(adata->clp,
9222 task->tk_msg.rpc_resp, xprt);
9223 trace_nfs4_trunked_exchange_id(adata->clp,
9224 xprt->address_strings[RPC_DISPLAY_ADDR], status);
9225 }
9226 if (status == 0)
9227 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
9228 else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt,
9229 (struct sockaddr *)&xprt->addr))
9230 rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
9231
9232 rpc_put_task(task);
9233 if (status == -NFS4ERR_DELAY) {
9234 ssleep(1);
9235 goto try_again;
9236 }
9237 }
9238 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
9239
_nfs4_proc_destroy_clientid(struct nfs_client * clp,const struct cred * cred)9240 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
9241 const struct cred *cred)
9242 {
9243 struct rpc_message msg = {
9244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
9245 .rpc_argp = clp,
9246 .rpc_cred = cred,
9247 };
9248 int status;
9249
9250 status = rpc_call_sync(clp->cl_rpcclient, &msg,
9251 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9252 trace_nfs4_destroy_clientid(clp, status);
9253 if (status)
9254 dprintk("NFS: Got error %d from the server %s on "
9255 "DESTROY_CLIENTID.", status, clp->cl_hostname);
9256 return status;
9257 }
9258
nfs4_proc_destroy_clientid(struct nfs_client * clp,const struct cred * cred)9259 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
9260 const struct cred *cred)
9261 {
9262 unsigned int loop;
9263 int ret;
9264
9265 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
9266 ret = _nfs4_proc_destroy_clientid(clp, cred);
9267 switch (ret) {
9268 case -NFS4ERR_DELAY:
9269 case -NFS4ERR_CLIENTID_BUSY:
9270 ssleep(1);
9271 break;
9272 default:
9273 return ret;
9274 }
9275 }
9276 return 0;
9277 }
9278
nfs4_destroy_clientid(struct nfs_client * clp)9279 int nfs4_destroy_clientid(struct nfs_client *clp)
9280 {
9281 const struct cred *cred;
9282 int ret = 0;
9283
9284 if (clp->cl_mvops->minor_version < 1)
9285 goto out;
9286 if (clp->cl_exchange_flags == 0)
9287 goto out;
9288 if (clp->cl_preserve_clid)
9289 goto out;
9290 cred = nfs4_get_clid_cred(clp);
9291 ret = nfs4_proc_destroy_clientid(clp, cred);
9292 put_cred(cred);
9293 switch (ret) {
9294 case 0:
9295 case -NFS4ERR_STALE_CLIENTID:
9296 clp->cl_exchange_flags = 0;
9297 }
9298 out:
9299 return ret;
9300 }
9301
9302 #endif /* CONFIG_NFS_V4_1 */
9303
9304 struct nfs4_get_lease_time_data {
9305 struct nfs4_get_lease_time_args *args;
9306 struct nfs4_get_lease_time_res *res;
9307 struct nfs_client *clp;
9308 };
9309
nfs4_get_lease_time_prepare(struct rpc_task * task,void * calldata)9310 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9311 void *calldata)
9312 {
9313 struct nfs4_get_lease_time_data *data =
9314 (struct nfs4_get_lease_time_data *)calldata;
9315
9316 /* just setup sequence, do not trigger session recovery
9317 since we're invoked within one */
9318 nfs4_setup_sequence(data->clp,
9319 &data->args->la_seq_args,
9320 &data->res->lr_seq_res,
9321 task);
9322 }
9323
9324 /*
9325 * Called from nfs4_state_manager thread for session setup, so don't recover
9326 * from sequence operation or clientid errors.
9327 */
nfs4_get_lease_time_done(struct rpc_task * task,void * calldata)9328 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9329 {
9330 struct nfs4_get_lease_time_data *data =
9331 (struct nfs4_get_lease_time_data *)calldata;
9332
9333 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9334 return;
9335 switch (task->tk_status) {
9336 case -NFS4ERR_DELAY:
9337 case -NFS4ERR_GRACE:
9338 rpc_delay(task, NFS4_POLL_RETRY_MIN);
9339 task->tk_status = 0;
9340 fallthrough;
9341 case -NFS4ERR_RETRY_UNCACHED_REP:
9342 rpc_restart_call_prepare(task);
9343 return;
9344 }
9345 }
9346
9347 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9348 .rpc_call_prepare = nfs4_get_lease_time_prepare,
9349 .rpc_call_done = nfs4_get_lease_time_done,
9350 };
9351
nfs4_proc_get_lease_time(struct nfs_client * clp,struct nfs_fsinfo * fsinfo)9352 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9353 {
9354 struct nfs4_get_lease_time_args args;
9355 struct nfs4_get_lease_time_res res = {
9356 .lr_fsinfo = fsinfo,
9357 };
9358 struct nfs4_get_lease_time_data data = {
9359 .args = &args,
9360 .res = &res,
9361 .clp = clp,
9362 };
9363 struct rpc_message msg = {
9364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9365 .rpc_argp = &args,
9366 .rpc_resp = &res,
9367 };
9368 struct rpc_task_setup task_setup = {
9369 .rpc_client = clp->cl_rpcclient,
9370 .rpc_message = &msg,
9371 .callback_ops = &nfs4_get_lease_time_ops,
9372 .callback_data = &data,
9373 .flags = RPC_TASK_TIMEOUT,
9374 };
9375
9376 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9377 return nfs4_call_sync_custom(&task_setup);
9378 }
9379
9380 #ifdef CONFIG_NFS_V4_1
9381
9382 /*
9383 * Initialize the values to be used by the client in CREATE_SESSION
9384 * If nfs4_init_session set the fore channel request and response sizes,
9385 * use them.
9386 *
9387 * Set the back channel max_resp_sz_cached to zero to force the client to
9388 * always set csa_cachethis to FALSE because the current implementation
9389 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9390 */
nfs4_init_channel_attrs(struct nfs41_create_session_args * args,struct rpc_clnt * clnt)9391 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9392 struct rpc_clnt *clnt)
9393 {
9394 unsigned int max_rqst_sz, max_resp_sz;
9395 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9396 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9397
9398 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9399 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9400
9401 /* Fore channel attributes */
9402 args->fc_attrs.max_rqst_sz = max_rqst_sz;
9403 args->fc_attrs.max_resp_sz = max_resp_sz;
9404 args->fc_attrs.max_ops = NFS4_MAX_OPS;
9405 args->fc_attrs.max_reqs = max_session_slots;
9406
9407 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9408 "max_ops=%u max_reqs=%u\n",
9409 __func__,
9410 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9411 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9412
9413 /* Back channel attributes */
9414 args->bc_attrs.max_rqst_sz = max_bc_payload;
9415 args->bc_attrs.max_resp_sz = max_bc_payload;
9416 args->bc_attrs.max_resp_sz_cached = 0;
9417 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9418 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9419 if (args->bc_attrs.max_reqs > max_bc_slots)
9420 args->bc_attrs.max_reqs = max_bc_slots;
9421
9422 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9423 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9424 __func__,
9425 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9426 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9427 args->bc_attrs.max_reqs);
9428 }
9429
nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9430 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9431 struct nfs41_create_session_res *res)
9432 {
9433 struct nfs4_channel_attrs *sent = &args->fc_attrs;
9434 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9435
9436 if (rcvd->max_resp_sz > sent->max_resp_sz)
9437 return -EINVAL;
9438 /*
9439 * Our requested max_ops is the minimum we need; we're not
9440 * prepared to break up compounds into smaller pieces than that.
9441 * So, no point even trying to continue if the server won't
9442 * cooperate:
9443 */
9444 if (rcvd->max_ops < sent->max_ops)
9445 return -EINVAL;
9446 if (rcvd->max_reqs == 0)
9447 return -EINVAL;
9448 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9449 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9450 return 0;
9451 }
9452
nfs4_verify_back_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9453 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9454 struct nfs41_create_session_res *res)
9455 {
9456 struct nfs4_channel_attrs *sent = &args->bc_attrs;
9457 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9458
9459 if (!(res->flags & SESSION4_BACK_CHAN))
9460 goto out;
9461 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9462 return -EINVAL;
9463 if (rcvd->max_resp_sz > sent->max_resp_sz)
9464 return -EINVAL;
9465 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9466 return -EINVAL;
9467 if (rcvd->max_ops > sent->max_ops)
9468 return -EINVAL;
9469 if (rcvd->max_reqs > sent->max_reqs)
9470 return -EINVAL;
9471 out:
9472 return 0;
9473 }
9474
nfs4_verify_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9475 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9476 struct nfs41_create_session_res *res)
9477 {
9478 int ret;
9479
9480 ret = nfs4_verify_fore_channel_attrs(args, res);
9481 if (ret)
9482 return ret;
9483 return nfs4_verify_back_channel_attrs(args, res);
9484 }
9485
nfs4_update_session(struct nfs4_session * session,struct nfs41_create_session_res * res)9486 static void nfs4_update_session(struct nfs4_session *session,
9487 struct nfs41_create_session_res *res)
9488 {
9489 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9490 /* Mark client id and session as being confirmed */
9491 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9492 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9493 session->flags = res->flags;
9494 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9495 if (res->flags & SESSION4_BACK_CHAN)
9496 memcpy(&session->bc_attrs, &res->bc_attrs,
9497 sizeof(session->bc_attrs));
9498 }
9499
_nfs4_proc_create_session(struct nfs_client * clp,const struct cred * cred)9500 static int _nfs4_proc_create_session(struct nfs_client *clp,
9501 const struct cred *cred)
9502 {
9503 struct nfs4_session *session = clp->cl_session;
9504 struct nfs41_create_session_args args = {
9505 .client = clp,
9506 .clientid = clp->cl_clientid,
9507 .seqid = clp->cl_seqid,
9508 .cb_program = NFS4_CALLBACK,
9509 };
9510 struct nfs41_create_session_res res;
9511
9512 struct rpc_message msg = {
9513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9514 .rpc_argp = &args,
9515 .rpc_resp = &res,
9516 .rpc_cred = cred,
9517 };
9518 int status;
9519
9520 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9521 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9522
9523 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9524 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9525 trace_nfs4_create_session(clp, status);
9526
9527 switch (status) {
9528 case -NFS4ERR_STALE_CLIENTID:
9529 case -NFS4ERR_DELAY:
9530 case -ETIMEDOUT:
9531 case -EACCES:
9532 case -EAGAIN:
9533 goto out;
9534 }
9535
9536 clp->cl_seqid++;
9537 if (!status) {
9538 /* Verify the session's negotiated channel_attrs values */
9539 status = nfs4_verify_channel_attrs(&args, &res);
9540 /* Increment the clientid slot sequence id */
9541 if (status)
9542 goto out;
9543 nfs4_update_session(session, &res);
9544 }
9545 out:
9546 return status;
9547 }
9548
9549 /*
9550 * Issues a CREATE_SESSION operation to the server.
9551 * It is the responsibility of the caller to verify the session is
9552 * expired before calling this routine.
9553 */
nfs4_proc_create_session(struct nfs_client * clp,const struct cred * cred)9554 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9555 {
9556 int status;
9557 unsigned *ptr;
9558 struct nfs4_session *session = clp->cl_session;
9559 struct nfs4_add_xprt_data xprtdata = {
9560 .clp = clp,
9561 };
9562 struct rpc_add_xprt_test rpcdata = {
9563 .add_xprt_test = clp->cl_mvops->session_trunk,
9564 .data = &xprtdata,
9565 };
9566
9567 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9568
9569 status = _nfs4_proc_create_session(clp, cred);
9570 if (status)
9571 goto out;
9572
9573 /* Init or reset the session slot tables */
9574 status = nfs4_setup_session_slot_tables(session);
9575 dprintk("slot table setup returned %d\n", status);
9576 if (status)
9577 goto out;
9578
9579 ptr = (unsigned *)&session->sess_id.data[0];
9580 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9581 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9582 rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9583 out:
9584 return status;
9585 }
9586
9587 /*
9588 * Issue the over-the-wire RPC DESTROY_SESSION.
9589 * The caller must serialize access to this routine.
9590 */
nfs4_proc_destroy_session(struct nfs4_session * session,const struct cred * cred)9591 int nfs4_proc_destroy_session(struct nfs4_session *session,
9592 const struct cred *cred)
9593 {
9594 struct rpc_message msg = {
9595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9596 .rpc_argp = session,
9597 .rpc_cred = cred,
9598 };
9599 int status = 0;
9600
9601 /* session is still being setup */
9602 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9603 return 0;
9604
9605 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9606 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9607 trace_nfs4_destroy_session(session->clp, status);
9608
9609 if (status)
9610 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9611 "Session has been destroyed regardless...\n", status);
9612 rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9613 return status;
9614 }
9615
9616 /*
9617 * Renew the cl_session lease.
9618 */
9619 struct nfs4_sequence_data {
9620 struct nfs_client *clp;
9621 struct nfs4_sequence_args args;
9622 struct nfs4_sequence_res res;
9623 };
9624
nfs41_sequence_release(void * data)9625 static void nfs41_sequence_release(void *data)
9626 {
9627 struct nfs4_sequence_data *calldata = data;
9628 struct nfs_client *clp = calldata->clp;
9629
9630 if (refcount_read(&clp->cl_count) > 1)
9631 nfs4_schedule_state_renewal(clp);
9632 nfs_put_client(clp);
9633 kfree(calldata);
9634 }
9635
nfs41_sequence_handle_errors(struct rpc_task * task,struct nfs_client * clp)9636 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9637 {
9638 switch(task->tk_status) {
9639 case -NFS4ERR_DELAY:
9640 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9641 return -EAGAIN;
9642 default:
9643 nfs4_schedule_lease_recovery(clp);
9644 }
9645 return 0;
9646 }
9647
nfs41_sequence_call_done(struct rpc_task * task,void * data)9648 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9649 {
9650 struct nfs4_sequence_data *calldata = data;
9651 struct nfs_client *clp = calldata->clp;
9652
9653 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9654 return;
9655
9656 trace_nfs4_sequence(clp, task->tk_status);
9657 if (task->tk_status < 0 && clp->cl_cons_state >= 0) {
9658 dprintk("%s ERROR %d\n", __func__, task->tk_status);
9659 if (refcount_read(&clp->cl_count) == 1)
9660 return;
9661
9662 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9663 rpc_restart_call_prepare(task);
9664 return;
9665 }
9666 }
9667 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9668 }
9669
nfs41_sequence_prepare(struct rpc_task * task,void * data)9670 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9671 {
9672 struct nfs4_sequence_data *calldata = data;
9673 struct nfs_client *clp = calldata->clp;
9674 struct nfs4_sequence_args *args;
9675 struct nfs4_sequence_res *res;
9676
9677 args = task->tk_msg.rpc_argp;
9678 res = task->tk_msg.rpc_resp;
9679
9680 nfs4_setup_sequence(clp, args, res, task);
9681 }
9682
9683 static const struct rpc_call_ops nfs41_sequence_ops = {
9684 .rpc_call_done = nfs41_sequence_call_done,
9685 .rpc_call_prepare = nfs41_sequence_prepare,
9686 .rpc_release = nfs41_sequence_release,
9687 };
9688
_nfs41_proc_sequence(struct nfs_client * clp,const struct cred * cred,struct nfs4_slot * slot,bool is_privileged)9689 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9690 const struct cred *cred,
9691 struct nfs4_slot *slot,
9692 bool is_privileged)
9693 {
9694 struct nfs4_sequence_data *calldata;
9695 struct rpc_message msg = {
9696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9697 .rpc_cred = cred,
9698 };
9699 struct rpc_task_setup task_setup_data = {
9700 .rpc_client = clp->cl_rpcclient,
9701 .rpc_message = &msg,
9702 .callback_ops = &nfs41_sequence_ops,
9703 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9704 };
9705 struct rpc_task *ret;
9706
9707 ret = ERR_PTR(-EIO);
9708 if (!refcount_inc_not_zero(&clp->cl_count))
9709 goto out_err;
9710
9711 ret = ERR_PTR(-ENOMEM);
9712 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9713 if (calldata == NULL)
9714 goto out_put_clp;
9715 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9716 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9717 msg.rpc_argp = &calldata->args;
9718 msg.rpc_resp = &calldata->res;
9719 calldata->clp = clp;
9720 task_setup_data.callback_data = calldata;
9721
9722 ret = rpc_run_task(&task_setup_data);
9723 if (IS_ERR(ret))
9724 goto out_err;
9725 return ret;
9726 out_put_clp:
9727 nfs_put_client(clp);
9728 out_err:
9729 nfs41_release_slot(slot);
9730 return ret;
9731 }
9732
nfs41_proc_async_sequence(struct nfs_client * clp,const struct cred * cred,unsigned renew_flags)9733 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9734 {
9735 struct rpc_task *task;
9736 int ret = 0;
9737
9738 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9739 return -EAGAIN;
9740 task = _nfs41_proc_sequence(clp, cred, NULL, false);
9741 if (IS_ERR(task))
9742 ret = PTR_ERR(task);
9743 else
9744 rpc_put_task_async(task);
9745 dprintk("<-- %s status=%d\n", __func__, ret);
9746 return ret;
9747 }
9748
nfs4_proc_sequence(struct nfs_client * clp,const struct cred * cred)9749 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9750 {
9751 struct rpc_task *task;
9752 int ret;
9753
9754 task = _nfs41_proc_sequence(clp, cred, NULL, true);
9755 if (IS_ERR(task)) {
9756 ret = PTR_ERR(task);
9757 goto out;
9758 }
9759 ret = rpc_wait_for_completion_task(task);
9760 if (!ret)
9761 ret = task->tk_status;
9762 rpc_put_task(task);
9763 out:
9764 dprintk("<-- %s status=%d\n", __func__, ret);
9765 return ret;
9766 }
9767
9768 struct nfs4_reclaim_complete_data {
9769 struct nfs_client *clp;
9770 struct nfs41_reclaim_complete_args arg;
9771 struct nfs41_reclaim_complete_res res;
9772 };
9773
nfs4_reclaim_complete_prepare(struct rpc_task * task,void * data)9774 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9775 {
9776 struct nfs4_reclaim_complete_data *calldata = data;
9777
9778 nfs4_setup_sequence(calldata->clp,
9779 &calldata->arg.seq_args,
9780 &calldata->res.seq_res,
9781 task);
9782 }
9783
nfs41_reclaim_complete_handle_errors(struct rpc_task * task,struct nfs_client * clp)9784 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9785 {
9786 switch(task->tk_status) {
9787 case 0:
9788 wake_up_all(&clp->cl_lock_waitq);
9789 fallthrough;
9790 case -NFS4ERR_COMPLETE_ALREADY:
9791 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9792 break;
9793 case -NFS4ERR_DELAY:
9794 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9795 fallthrough;
9796 case -NFS4ERR_RETRY_UNCACHED_REP:
9797 case -EACCES:
9798 dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9799 __func__, task->tk_status, clp->cl_hostname);
9800 return -EAGAIN;
9801 case -NFS4ERR_BADSESSION:
9802 case -NFS4ERR_DEADSESSION:
9803 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9804 break;
9805 default:
9806 nfs4_schedule_lease_recovery(clp);
9807 }
9808 return 0;
9809 }
9810
nfs4_reclaim_complete_done(struct rpc_task * task,void * data)9811 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9812 {
9813 struct nfs4_reclaim_complete_data *calldata = data;
9814 struct nfs_client *clp = calldata->clp;
9815 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9816
9817 if (!nfs41_sequence_done(task, res))
9818 return;
9819
9820 trace_nfs4_reclaim_complete(clp, task->tk_status);
9821 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9822 rpc_restart_call_prepare(task);
9823 return;
9824 }
9825 }
9826
nfs4_free_reclaim_complete_data(void * data)9827 static void nfs4_free_reclaim_complete_data(void *data)
9828 {
9829 struct nfs4_reclaim_complete_data *calldata = data;
9830
9831 kfree(calldata);
9832 }
9833
9834 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9835 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9836 .rpc_call_done = nfs4_reclaim_complete_done,
9837 .rpc_release = nfs4_free_reclaim_complete_data,
9838 };
9839
9840 /*
9841 * Issue a global reclaim complete.
9842 */
nfs41_proc_reclaim_complete(struct nfs_client * clp,const struct cred * cred)9843 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9844 const struct cred *cred)
9845 {
9846 struct nfs4_reclaim_complete_data *calldata;
9847 struct rpc_message msg = {
9848 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9849 .rpc_cred = cred,
9850 };
9851 struct rpc_task_setup task_setup_data = {
9852 .rpc_client = clp->cl_rpcclient,
9853 .rpc_message = &msg,
9854 .callback_ops = &nfs4_reclaim_complete_call_ops,
9855 .flags = RPC_TASK_NO_ROUND_ROBIN,
9856 };
9857 int status = -ENOMEM;
9858
9859 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9860 if (calldata == NULL)
9861 goto out;
9862 calldata->clp = clp;
9863 calldata->arg.one_fs = 0;
9864
9865 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9866 msg.rpc_argp = &calldata->arg;
9867 msg.rpc_resp = &calldata->res;
9868 task_setup_data.callback_data = calldata;
9869 status = nfs4_call_sync_custom(&task_setup_data);
9870 out:
9871 dprintk("<-- %s status=%d\n", __func__, status);
9872 return status;
9873 }
9874
9875 static void
nfs4_layoutget_prepare(struct rpc_task * task,void * calldata)9876 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9877 {
9878 struct nfs4_layoutget *lgp = calldata;
9879 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9880
9881 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9882 &lgp->res.seq_res, task);
9883 }
9884
nfs4_layoutget_done(struct rpc_task * task,void * calldata)9885 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9886 {
9887 struct nfs4_layoutget *lgp = calldata;
9888
9889 nfs41_sequence_process(task, &lgp->res.seq_res);
9890 }
9891
9892 static int
nfs4_layoutget_handle_exception(struct rpc_task * task,struct nfs4_layoutget * lgp,struct nfs4_exception * exception)9893 nfs4_layoutget_handle_exception(struct rpc_task *task,
9894 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9895 {
9896 struct inode *inode = lgp->args.inode;
9897 struct nfs_server *server = NFS_SERVER(inode);
9898 struct pnfs_layout_hdr *lo = lgp->lo;
9899 int nfs4err = task->tk_status;
9900 int err, status = 0;
9901 LIST_HEAD(head);
9902
9903 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9904
9905 nfs4_sequence_free_slot(&lgp->res.seq_res);
9906
9907 exception->state = NULL;
9908 exception->stateid = NULL;
9909
9910 switch (nfs4err) {
9911 case 0:
9912 goto out;
9913
9914 /*
9915 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9916 * on the file. set tk_status to -ENODATA to tell upper layer to
9917 * retry go inband.
9918 */
9919 case -NFS4ERR_LAYOUTUNAVAILABLE:
9920 status = -ENODATA;
9921 goto out;
9922 /*
9923 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9924 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9925 */
9926 case -NFS4ERR_BADLAYOUT:
9927 status = -EOVERFLOW;
9928 goto out;
9929 /*
9930 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9931 * (or clients) writing to the same RAID stripe except when
9932 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9933 *
9934 * Treat it like we would RECALLCONFLICT -- we retry for a little
9935 * while, and then eventually give up.
9936 */
9937 case -NFS4ERR_LAYOUTTRYLATER:
9938 if (lgp->args.minlength == 0) {
9939 status = -EOVERFLOW;
9940 goto out;
9941 }
9942 status = -EBUSY;
9943 break;
9944 case -NFS4ERR_RECALLCONFLICT:
9945 case -NFS4ERR_RETURNCONFLICT:
9946 status = -ERECALLCONFLICT;
9947 break;
9948 case -NFS4ERR_DELEG_REVOKED:
9949 case -NFS4ERR_ADMIN_REVOKED:
9950 case -NFS4ERR_EXPIRED:
9951 case -NFS4ERR_BAD_STATEID:
9952 exception->timeout = 0;
9953 spin_lock(&inode->i_lock);
9954 /* If the open stateid was bad, then recover it. */
9955 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9956 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9957 spin_unlock(&inode->i_lock);
9958 exception->state = lgp->args.ctx->state;
9959 exception->stateid = &lgp->args.stateid;
9960 break;
9961 }
9962
9963 /*
9964 * Mark the bad layout state as invalid, then retry
9965 */
9966 pnfs_mark_layout_stateid_invalid(lo, &head);
9967 spin_unlock(&inode->i_lock);
9968 nfs_commit_inode(inode, 0);
9969 pnfs_free_lseg_list(&head);
9970 status = -EAGAIN;
9971 goto out;
9972 }
9973
9974 err = nfs4_handle_exception(server, nfs4err, exception);
9975 if (!status) {
9976 if (exception->retry)
9977 status = -EAGAIN;
9978 else
9979 status = err;
9980 }
9981 out:
9982 return status;
9983 }
9984
max_response_pages(struct nfs_server * server)9985 size_t max_response_pages(struct nfs_server *server)
9986 {
9987 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9988 return nfs_page_array_len(0, max_resp_sz);
9989 }
9990
nfs4_layoutget_release(void * calldata)9991 static void nfs4_layoutget_release(void *calldata)
9992 {
9993 struct nfs4_layoutget *lgp = calldata;
9994
9995 nfs4_sequence_free_slot(&lgp->res.seq_res);
9996 pnfs_layoutget_free(lgp);
9997 }
9998
9999 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
10000 .rpc_call_prepare = nfs4_layoutget_prepare,
10001 .rpc_call_done = nfs4_layoutget_done,
10002 .rpc_release = nfs4_layoutget_release,
10003 };
10004
10005 struct pnfs_layout_segment *
nfs4_proc_layoutget(struct nfs4_layoutget * lgp,struct nfs4_exception * exception)10006 nfs4_proc_layoutget(struct nfs4_layoutget *lgp,
10007 struct nfs4_exception *exception)
10008 {
10009 struct inode *inode = lgp->args.inode;
10010 struct nfs_server *server = NFS_SERVER(inode);
10011 struct rpc_task *task;
10012 struct rpc_message msg = {
10013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
10014 .rpc_argp = &lgp->args,
10015 .rpc_resp = &lgp->res,
10016 .rpc_cred = lgp->cred,
10017 };
10018 struct rpc_task_setup task_setup_data = {
10019 .rpc_client = server->client,
10020 .rpc_message = &msg,
10021 .callback_ops = &nfs4_layoutget_call_ops,
10022 .callback_data = lgp,
10023 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
10024 RPC_TASK_MOVEABLE,
10025 };
10026 struct pnfs_layout_segment *lseg = NULL;
10027 int status = 0;
10028
10029 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
10030 exception->retry = 0;
10031
10032 task = rpc_run_task(&task_setup_data);
10033 if (IS_ERR(task))
10034 return ERR_CAST(task);
10035
10036 status = rpc_wait_for_completion_task(task);
10037 if (status != 0)
10038 goto out;
10039
10040 if (task->tk_status < 0) {
10041 exception->retry = 1;
10042 status = nfs4_layoutget_handle_exception(task, lgp, exception);
10043 } else if (lgp->res.layoutp->len == 0) {
10044 exception->retry = 1;
10045 status = -EAGAIN;
10046 nfs4_update_delay(&exception->timeout);
10047 } else
10048 lseg = pnfs_layout_process(lgp);
10049 out:
10050 trace_nfs4_layoutget(lgp->args.ctx,
10051 &lgp->args.range,
10052 &lgp->res.range,
10053 &lgp->res.stateid,
10054 status);
10055
10056 rpc_put_task(task);
10057 dprintk("<-- %s status=%d\n", __func__, status);
10058 if (status)
10059 return ERR_PTR(status);
10060 return lseg;
10061 }
10062
10063 static void
nfs4_layoutreturn_prepare(struct rpc_task * task,void * calldata)10064 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
10065 {
10066 struct nfs4_layoutreturn *lrp = calldata;
10067
10068 nfs4_setup_sequence(lrp->clp,
10069 &lrp->args.seq_args,
10070 &lrp->res.seq_res,
10071 task);
10072 if (!pnfs_layout_is_valid(lrp->args.layout))
10073 rpc_exit(task, 0);
10074 }
10075
nfs4_layoutreturn_done(struct rpc_task * task,void * calldata)10076 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
10077 {
10078 struct nfs4_layoutreturn *lrp = calldata;
10079 struct nfs_server *server;
10080
10081 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
10082 return;
10083
10084 if (task->tk_rpc_status == -ETIMEDOUT) {
10085 lrp->rpc_status = -EAGAIN;
10086 lrp->res.lrs_present = 0;
10087 return;
10088 }
10089 /*
10090 * Was there an RPC level error? Assume the call succeeded,
10091 * and that we need to release the layout
10092 */
10093 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
10094 lrp->res.lrs_present = 0;
10095 return;
10096 }
10097
10098 server = NFS_SERVER(lrp->args.inode);
10099 switch (task->tk_status) {
10100 case -NFS4ERR_OLD_STATEID:
10101 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
10102 &lrp->args.range,
10103 lrp->args.inode))
10104 goto out_restart;
10105 fallthrough;
10106 default:
10107 task->tk_status = 0;
10108 lrp->res.lrs_present = 0;
10109 fallthrough;
10110 case 0:
10111 break;
10112 case -NFS4ERR_BADSESSION:
10113 case -NFS4ERR_DEADSESSION:
10114 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10115 nfs4_schedule_session_recovery(server->nfs_client->cl_session,
10116 task->tk_status);
10117 lrp->res.lrs_present = 0;
10118 lrp->rpc_status = -EAGAIN;
10119 task->tk_status = 0;
10120 break;
10121 case -NFS4ERR_DELAY:
10122 if (nfs4_async_handle_error(task, server, NULL, NULL) ==
10123 -EAGAIN)
10124 goto out_restart;
10125 lrp->res.lrs_present = 0;
10126 break;
10127 }
10128 return;
10129 out_restart:
10130 task->tk_status = 0;
10131 nfs4_sequence_free_slot(&lrp->res.seq_res);
10132 rpc_restart_call_prepare(task);
10133 }
10134
nfs4_layoutreturn_release(void * calldata)10135 static void nfs4_layoutreturn_release(void *calldata)
10136 {
10137 struct nfs4_layoutreturn *lrp = calldata;
10138 struct pnfs_layout_hdr *lo = lrp->args.layout;
10139
10140 if (lrp->rpc_status == 0 || !lrp->inode)
10141 pnfs_layoutreturn_free_lsegs(
10142 lo, &lrp->args.stateid, &lrp->args.range,
10143 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
10144 else
10145 pnfs_layoutreturn_retry_later(lo, &lrp->args.stateid,
10146 &lrp->args.range);
10147 nfs4_sequence_free_slot(&lrp->res.seq_res);
10148 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
10149 lrp->ld_private.ops->free(&lrp->ld_private);
10150 pnfs_put_layout_hdr(lrp->args.layout);
10151 nfs_iput_and_deactive(lrp->inode);
10152 put_cred(lrp->cred);
10153 kfree(calldata);
10154 }
10155
10156 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
10157 .rpc_call_prepare = nfs4_layoutreturn_prepare,
10158 .rpc_call_done = nfs4_layoutreturn_done,
10159 .rpc_release = nfs4_layoutreturn_release,
10160 };
10161
nfs4_proc_layoutreturn(struct nfs4_layoutreturn * lrp,unsigned int flags)10162 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags)
10163 {
10164 struct rpc_task *task;
10165 struct rpc_message msg = {
10166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
10167 .rpc_argp = &lrp->args,
10168 .rpc_resp = &lrp->res,
10169 .rpc_cred = lrp->cred,
10170 };
10171 struct rpc_task_setup task_setup_data = {
10172 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
10173 .rpc_message = &msg,
10174 .callback_ops = &nfs4_layoutreturn_call_ops,
10175 .callback_data = lrp,
10176 .flags = RPC_TASK_MOVEABLE,
10177 };
10178 int status = 0;
10179
10180 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
10181 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
10182 &task_setup_data.rpc_client, &msg);
10183
10184 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
10185 if (flags & PNFS_FL_LAYOUTRETURN_ASYNC) {
10186 if (!lrp->inode) {
10187 nfs4_layoutreturn_release(lrp);
10188 return -EAGAIN;
10189 }
10190 task_setup_data.flags |= RPC_TASK_ASYNC;
10191 }
10192 if (!lrp->inode)
10193 flags |= PNFS_FL_LAYOUTRETURN_PRIVILEGED;
10194 if (flags & PNFS_FL_LAYOUTRETURN_PRIVILEGED)
10195 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
10196 1);
10197 else
10198 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
10199 0);
10200 task = rpc_run_task(&task_setup_data);
10201 if (IS_ERR(task))
10202 return PTR_ERR(task);
10203 if (!(flags & PNFS_FL_LAYOUTRETURN_ASYNC))
10204 status = task->tk_status;
10205 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
10206 dprintk("<-- %s status=%d\n", __func__, status);
10207 rpc_put_task(task);
10208 return status;
10209 }
10210
10211 static int
_nfs4_proc_getdeviceinfo(struct nfs_server * server,struct pnfs_device * pdev,const struct cred * cred)10212 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
10213 struct pnfs_device *pdev,
10214 const struct cred *cred)
10215 {
10216 struct nfs4_getdeviceinfo_args args = {
10217 .pdev = pdev,
10218 .notify_types = NOTIFY_DEVICEID4_CHANGE |
10219 NOTIFY_DEVICEID4_DELETE,
10220 };
10221 struct nfs4_getdeviceinfo_res res = {
10222 .pdev = pdev,
10223 };
10224 struct rpc_message msg = {
10225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
10226 .rpc_argp = &args,
10227 .rpc_resp = &res,
10228 .rpc_cred = cred,
10229 };
10230 int status;
10231
10232 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
10233 if (res.notification & ~args.notify_types)
10234 dprintk("%s: unsupported notification\n", __func__);
10235 if (res.notification != args.notify_types)
10236 pdev->nocache = 1;
10237
10238 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
10239
10240 dprintk("<-- %s status=%d\n", __func__, status);
10241
10242 return status;
10243 }
10244
nfs4_proc_getdeviceinfo(struct nfs_server * server,struct pnfs_device * pdev,const struct cred * cred)10245 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
10246 struct pnfs_device *pdev,
10247 const struct cred *cred)
10248 {
10249 struct nfs4_exception exception = { };
10250 int err;
10251
10252 do {
10253 err = nfs4_handle_exception(server,
10254 _nfs4_proc_getdeviceinfo(server, pdev, cred),
10255 &exception);
10256 } while (exception.retry);
10257 return err;
10258 }
10259 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
10260
nfs4_layoutcommit_prepare(struct rpc_task * task,void * calldata)10261 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
10262 {
10263 struct nfs4_layoutcommit_data *data = calldata;
10264 struct nfs_server *server = NFS_SERVER(data->args.inode);
10265
10266 nfs4_setup_sequence(server->nfs_client,
10267 &data->args.seq_args,
10268 &data->res.seq_res,
10269 task);
10270 }
10271
10272 static void
nfs4_layoutcommit_done(struct rpc_task * task,void * calldata)10273 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
10274 {
10275 struct nfs4_layoutcommit_data *data = calldata;
10276 struct nfs_server *server = NFS_SERVER(data->args.inode);
10277
10278 if (!nfs41_sequence_done(task, &data->res.seq_res))
10279 return;
10280
10281 switch (task->tk_status) { /* Just ignore these failures */
10282 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
10283 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
10284 case -NFS4ERR_BADLAYOUT: /* no layout */
10285 case -NFS4ERR_GRACE: /* loca_recalim always false */
10286 task->tk_status = 0;
10287 break;
10288 case 0:
10289 break;
10290 default:
10291 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
10292 rpc_restart_call_prepare(task);
10293 return;
10294 }
10295 }
10296 }
10297
nfs4_layoutcommit_release(void * calldata)10298 static void nfs4_layoutcommit_release(void *calldata)
10299 {
10300 struct nfs4_layoutcommit_data *data = calldata;
10301
10302 pnfs_cleanup_layoutcommit(data);
10303 nfs_post_op_update_inode_force_wcc(data->args.inode,
10304 data->res.fattr);
10305 put_cred(data->cred);
10306 nfs_iput_and_deactive(data->inode);
10307 kfree(data);
10308 }
10309
10310 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10311 .rpc_call_prepare = nfs4_layoutcommit_prepare,
10312 .rpc_call_done = nfs4_layoutcommit_done,
10313 .rpc_release = nfs4_layoutcommit_release,
10314 };
10315
10316 int
nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data * data,bool sync)10317 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10318 {
10319 struct rpc_message msg = {
10320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10321 .rpc_argp = &data->args,
10322 .rpc_resp = &data->res,
10323 .rpc_cred = data->cred,
10324 };
10325 struct rpc_task_setup task_setup_data = {
10326 .task = &data->task,
10327 .rpc_client = NFS_CLIENT(data->args.inode),
10328 .rpc_message = &msg,
10329 .callback_ops = &nfs4_layoutcommit_ops,
10330 .callback_data = data,
10331 .flags = RPC_TASK_MOVEABLE,
10332 };
10333 struct rpc_task *task;
10334 int status = 0;
10335
10336 dprintk("NFS: initiating layoutcommit call. sync %d "
10337 "lbw: %llu inode %lu\n", sync,
10338 data->args.lastbytewritten,
10339 data->args.inode->i_ino);
10340
10341 if (!sync) {
10342 data->inode = nfs_igrab_and_active(data->args.inode);
10343 if (data->inode == NULL) {
10344 nfs4_layoutcommit_release(data);
10345 return -EAGAIN;
10346 }
10347 task_setup_data.flags = RPC_TASK_ASYNC;
10348 }
10349 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10350 task = rpc_run_task(&task_setup_data);
10351 if (IS_ERR(task))
10352 return PTR_ERR(task);
10353 if (sync)
10354 status = task->tk_status;
10355 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10356 dprintk("%s: status %d\n", __func__, status);
10357 rpc_put_task(task);
10358 return status;
10359 }
10360
10361 /*
10362 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10363 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10364 */
_nfs41_proc_secinfo_no_name(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_secinfo_flavors * flavors,bool use_integrity)10365 static int _nfs41_proc_secinfo_no_name(struct nfs_server *server,
10366 struct nfs_fh *fhandle,
10367 struct nfs4_secinfo_flavors *flavors,
10368 bool use_integrity)
10369 {
10370 struct nfs41_secinfo_no_name_args args = {
10371 .style = SECINFO_STYLE_CURRENT_FH,
10372 };
10373 struct nfs4_secinfo_res res = {
10374 .flavors = flavors,
10375 };
10376 struct rpc_message msg = {
10377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10378 .rpc_argp = &args,
10379 .rpc_resp = &res,
10380 };
10381 struct nfs4_call_sync_data data = {
10382 .seq_server = server,
10383 .seq_args = &args.seq_args,
10384 .seq_res = &res.seq_res,
10385 };
10386 struct rpc_task_setup task_setup = {
10387 .rpc_client = server->client,
10388 .rpc_message = &msg,
10389 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10390 .callback_data = &data,
10391 .flags = RPC_TASK_NO_ROUND_ROBIN,
10392 };
10393 const struct cred *cred = NULL;
10394 int status;
10395
10396 if (use_integrity) {
10397 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10398
10399 cred = nfs4_get_clid_cred(server->nfs_client);
10400 msg.rpc_cred = cred;
10401 }
10402
10403 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10404 status = nfs4_call_sync_custom(&task_setup);
10405 dprintk("<-- %s status=%d\n", __func__, status);
10406
10407 put_cred(cred);
10408
10409 return status;
10410 }
10411
nfs41_proc_secinfo_no_name(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_secinfo_flavors * flavors)10412 static int nfs41_proc_secinfo_no_name(struct nfs_server *server,
10413 struct nfs_fh *fhandle,
10414 struct nfs4_secinfo_flavors *flavors)
10415 {
10416 struct nfs4_exception exception = {
10417 .interruptible = true,
10418 };
10419 int err;
10420 do {
10421 /* first try using integrity protection */
10422 err = -NFS4ERR_WRONGSEC;
10423
10424 /* try to use integrity protection with machine cred */
10425 if (_nfs4_is_integrity_protected(server->nfs_client))
10426 err = _nfs41_proc_secinfo_no_name(server, fhandle,
10427 flavors, true);
10428
10429 /*
10430 * if unable to use integrity protection, or SECINFO with
10431 * integrity protection returns NFS4ERR_WRONGSEC (which is
10432 * disallowed by spec, but exists in deployed servers) use
10433 * the current filesystem's rpc_client and the user cred.
10434 */
10435 if (err == -NFS4ERR_WRONGSEC)
10436 err = _nfs41_proc_secinfo_no_name(server, fhandle,
10437 flavors, false);
10438
10439 switch (err) {
10440 case 0:
10441 case -NFS4ERR_WRONGSEC:
10442 case -ENOTSUPP:
10443 goto out;
10444 default:
10445 err = nfs4_handle_exception(server, err, &exception);
10446 }
10447 } while (exception.retry);
10448 out:
10449 return err;
10450 }
10451
nfs41_find_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr)10452 static int nfs41_find_root_sec(struct nfs_server *server,
10453 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
10454 {
10455 int err;
10456 struct page *page;
10457 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10458 struct nfs4_secinfo_flavors *flavors;
10459 struct nfs4_secinfo4 *secinfo;
10460 int i;
10461
10462 page = alloc_page(GFP_KERNEL);
10463 if (!page) {
10464 err = -ENOMEM;
10465 goto out;
10466 }
10467
10468 flavors = page_address(page);
10469 err = nfs41_proc_secinfo_no_name(server, fhandle, flavors);
10470
10471 /*
10472 * Fall back on "guess and check" method if
10473 * the server doesn't support SECINFO_NO_NAME
10474 */
10475 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10476 err = nfs4_find_root_sec(server, fhandle, fattr);
10477 goto out_freepage;
10478 }
10479 if (err)
10480 goto out_freepage;
10481
10482 for (i = 0; i < flavors->num_flavors; i++) {
10483 secinfo = &flavors->flavors[i];
10484
10485 switch (secinfo->flavor) {
10486 case RPC_AUTH_NULL:
10487 case RPC_AUTH_UNIX:
10488 case RPC_AUTH_GSS:
10489 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10490 &secinfo->flavor_info);
10491 break;
10492 default:
10493 flavor = RPC_AUTH_MAXFLAVOR;
10494 break;
10495 }
10496
10497 if (!nfs_auth_info_match(&server->auth_info, flavor))
10498 flavor = RPC_AUTH_MAXFLAVOR;
10499
10500 if (flavor != RPC_AUTH_MAXFLAVOR) {
10501 err = nfs4_lookup_root_sec(server, fhandle, fattr,
10502 flavor);
10503 if (!err)
10504 break;
10505 }
10506 }
10507
10508 if (flavor == RPC_AUTH_MAXFLAVOR)
10509 err = -EPERM;
10510
10511 out_freepage:
10512 put_page(page);
10513 if (err == -EACCES)
10514 return -EPERM;
10515 out:
10516 return err;
10517 }
10518
_nfs41_test_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)10519 static int _nfs41_test_stateid(struct nfs_server *server,
10520 const nfs4_stateid *stateid,
10521 const struct cred *cred)
10522 {
10523 int status;
10524 struct nfs41_test_stateid_args args = {
10525 .stateid = *stateid,
10526 };
10527 struct nfs41_test_stateid_res res;
10528 struct rpc_message msg = {
10529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10530 .rpc_argp = &args,
10531 .rpc_resp = &res,
10532 .rpc_cred = cred,
10533 };
10534 struct rpc_clnt *rpc_client = server->client;
10535
10536 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10537 &rpc_client, &msg);
10538
10539 dprintk("NFS call test_stateid %p\n", stateid);
10540 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10541 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10542 &args.seq_args, &res.seq_res);
10543 if (status != NFS_OK) {
10544 dprintk("NFS reply test_stateid: failed, %d\n", status);
10545 return status;
10546 }
10547 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10548 return -res.status;
10549 }
10550
nfs4_handle_delay_or_session_error(struct nfs_server * server,int err,struct nfs4_exception * exception)10551 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10552 int err, struct nfs4_exception *exception)
10553 {
10554 exception->retry = 0;
10555 switch(err) {
10556 case -NFS4ERR_DELAY:
10557 case -NFS4ERR_RETRY_UNCACHED_REP:
10558 nfs4_handle_exception(server, err, exception);
10559 break;
10560 case -NFS4ERR_BADSESSION:
10561 case -NFS4ERR_BADSLOT:
10562 case -NFS4ERR_BAD_HIGH_SLOT:
10563 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10564 case -NFS4ERR_DEADSESSION:
10565 nfs4_do_handle_exception(server, err, exception);
10566 }
10567 }
10568
10569 /**
10570 * nfs41_test_stateid - perform a TEST_STATEID operation
10571 *
10572 * @server: server / transport on which to perform the operation
10573 * @stateid: state ID to test
10574 * @cred: credential
10575 *
10576 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10577 * Otherwise a negative NFS4ERR value is returned if the operation
10578 * failed or the state ID is not currently valid.
10579 */
nfs41_test_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)10580 static int nfs41_test_stateid(struct nfs_server *server,
10581 const nfs4_stateid *stateid,
10582 const struct cred *cred)
10583 {
10584 struct nfs4_exception exception = {
10585 .interruptible = true,
10586 };
10587 int err;
10588 do {
10589 err = _nfs41_test_stateid(server, stateid, cred);
10590 nfs4_handle_delay_or_session_error(server, err, &exception);
10591 } while (exception.retry);
10592 return err;
10593 }
10594
10595 struct nfs_free_stateid_data {
10596 struct nfs_server *server;
10597 struct nfs41_free_stateid_args args;
10598 struct nfs41_free_stateid_res res;
10599 };
10600
nfs41_free_stateid_prepare(struct rpc_task * task,void * calldata)10601 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10602 {
10603 struct nfs_free_stateid_data *data = calldata;
10604 nfs4_setup_sequence(data->server->nfs_client,
10605 &data->args.seq_args,
10606 &data->res.seq_res,
10607 task);
10608 }
10609
nfs41_free_stateid_done(struct rpc_task * task,void * calldata)10610 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10611 {
10612 struct nfs_free_stateid_data *data = calldata;
10613
10614 nfs41_sequence_done(task, &data->res.seq_res);
10615
10616 switch (task->tk_status) {
10617 case -NFS4ERR_DELAY:
10618 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10619 rpc_restart_call_prepare(task);
10620 }
10621 }
10622
nfs41_free_stateid_release(void * calldata)10623 static void nfs41_free_stateid_release(void *calldata)
10624 {
10625 struct nfs_free_stateid_data *data = calldata;
10626 struct nfs_client *clp = data->server->nfs_client;
10627
10628 nfs_put_client(clp);
10629 kfree(calldata);
10630 }
10631
10632 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10633 .rpc_call_prepare = nfs41_free_stateid_prepare,
10634 .rpc_call_done = nfs41_free_stateid_done,
10635 .rpc_release = nfs41_free_stateid_release,
10636 };
10637
10638 /**
10639 * nfs41_free_stateid - perform a FREE_STATEID operation
10640 *
10641 * @server: server / transport on which to perform the operation
10642 * @stateid: state ID to release
10643 * @cred: credential
10644 * @privileged: set to true if this call needs to be privileged
10645 *
10646 * Note: this function is always asynchronous.
10647 */
nfs41_free_stateid(struct nfs_server * server,nfs4_stateid * stateid,const struct cred * cred,bool privileged)10648 static int nfs41_free_stateid(struct nfs_server *server,
10649 nfs4_stateid *stateid,
10650 const struct cred *cred,
10651 bool privileged)
10652 {
10653 struct rpc_message msg = {
10654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10655 .rpc_cred = cred,
10656 };
10657 struct rpc_task_setup task_setup = {
10658 .rpc_client = server->client,
10659 .rpc_message = &msg,
10660 .callback_ops = &nfs41_free_stateid_ops,
10661 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10662 };
10663 struct nfs_free_stateid_data *data;
10664 struct rpc_task *task;
10665 struct nfs_client *clp = server->nfs_client;
10666
10667 if (!refcount_inc_not_zero(&clp->cl_count))
10668 return -EIO;
10669
10670 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10671 &task_setup.rpc_client, &msg);
10672
10673 dprintk("NFS call free_stateid %p\n", stateid);
10674 data = kmalloc(sizeof(*data), GFP_KERNEL);
10675 if (!data)
10676 return -ENOMEM;
10677 data->server = server;
10678 nfs4_stateid_copy(&data->args.stateid, stateid);
10679
10680 task_setup.callback_data = data;
10681
10682 msg.rpc_argp = &data->args;
10683 msg.rpc_resp = &data->res;
10684 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10685 task = rpc_run_task(&task_setup);
10686 if (IS_ERR(task))
10687 return PTR_ERR(task);
10688 rpc_put_task(task);
10689 stateid->type = NFS4_FREED_STATEID_TYPE;
10690 return 0;
10691 }
10692
10693 static void
nfs41_free_lock_state(struct nfs_server * server,struct nfs4_lock_state * lsp)10694 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10695 {
10696 const struct cred *cred = lsp->ls_state->owner->so_cred;
10697
10698 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10699 nfs4_free_lock_state(server, lsp);
10700 }
10701
nfs41_match_stateid(const nfs4_stateid * s1,const nfs4_stateid * s2)10702 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10703 const nfs4_stateid *s2)
10704 {
10705 trace_nfs41_match_stateid(s1, s2);
10706
10707 if (s1->type != s2->type)
10708 return false;
10709
10710 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10711 return false;
10712
10713 if (s1->seqid == s2->seqid)
10714 return true;
10715
10716 return s1->seqid == 0 || s2->seqid == 0;
10717 }
10718
10719 #endif /* CONFIG_NFS_V4_1 */
10720
nfs4_match_stateid(const nfs4_stateid * s1,const nfs4_stateid * s2)10721 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10722 const nfs4_stateid *s2)
10723 {
10724 trace_nfs4_match_stateid(s1, s2);
10725
10726 return nfs4_stateid_match(s1, s2);
10727 }
10728
10729
10730 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10731 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10732 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10733 .recover_open = nfs4_open_reclaim,
10734 .recover_lock = nfs4_lock_reclaim,
10735 .establish_clid = nfs4_init_clientid,
10736 .detect_trunking = nfs40_discover_server_trunking,
10737 };
10738
10739 #if defined(CONFIG_NFS_V4_1)
10740 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10741 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10742 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10743 .recover_open = nfs4_open_reclaim,
10744 .recover_lock = nfs4_lock_reclaim,
10745 .establish_clid = nfs41_init_clientid,
10746 .reclaim_complete = nfs41_proc_reclaim_complete,
10747 .detect_trunking = nfs41_discover_server_trunking,
10748 };
10749 #endif /* CONFIG_NFS_V4_1 */
10750
10751 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10752 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10753 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10754 .recover_open = nfs40_open_expired,
10755 .recover_lock = nfs4_lock_expired,
10756 .establish_clid = nfs4_init_clientid,
10757 };
10758
10759 #if defined(CONFIG_NFS_V4_1)
10760 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10761 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10762 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
10763 .recover_open = nfs41_open_expired,
10764 .recover_lock = nfs41_lock_expired,
10765 .establish_clid = nfs41_init_clientid,
10766 };
10767 #endif /* CONFIG_NFS_V4_1 */
10768
10769 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10770 .sched_state_renewal = nfs4_proc_async_renew,
10771 .get_state_renewal_cred = nfs4_get_renew_cred,
10772 .renew_lease = nfs4_proc_renew,
10773 };
10774
10775 #if defined(CONFIG_NFS_V4_1)
10776 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10777 .sched_state_renewal = nfs41_proc_async_sequence,
10778 .get_state_renewal_cred = nfs4_get_machine_cred,
10779 .renew_lease = nfs4_proc_sequence,
10780 };
10781 #endif
10782
10783 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10784 .get_locations = _nfs40_proc_get_locations,
10785 .fsid_present = _nfs40_proc_fsid_present,
10786 };
10787
10788 #if defined(CONFIG_NFS_V4_1)
10789 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10790 .get_locations = _nfs41_proc_get_locations,
10791 .fsid_present = _nfs41_proc_fsid_present,
10792 };
10793 #endif /* CONFIG_NFS_V4_1 */
10794
10795 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10796 .minor_version = 0,
10797 .init_caps = NFS_CAP_READDIRPLUS
10798 | NFS_CAP_ATOMIC_OPEN
10799 | NFS_CAP_POSIX_LOCK,
10800 .init_client = nfs40_init_client,
10801 .shutdown_client = nfs40_shutdown_client,
10802 .match_stateid = nfs4_match_stateid,
10803 .find_root_sec = nfs4_find_root_sec,
10804 .free_lock_state = nfs4_release_lockowner,
10805 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
10806 .alloc_seqid = nfs_alloc_seqid,
10807 .call_sync_ops = &nfs40_call_sync_ops,
10808 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10809 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10810 .state_renewal_ops = &nfs40_state_renewal_ops,
10811 .mig_recovery_ops = &nfs40_mig_recovery_ops,
10812 };
10813
10814 #if defined(CONFIG_NFS_V4_1)
10815 static struct nfs_seqid *
nfs_alloc_no_seqid(struct nfs_seqid_counter * arg1,gfp_t arg2)10816 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10817 {
10818 return NULL;
10819 }
10820
10821 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10822 .minor_version = 1,
10823 .init_caps = NFS_CAP_READDIRPLUS
10824 | NFS_CAP_ATOMIC_OPEN
10825 | NFS_CAP_POSIX_LOCK
10826 | NFS_CAP_STATEID_NFSV41
10827 | NFS_CAP_ATOMIC_OPEN_V1
10828 | NFS_CAP_LGOPEN
10829 | NFS_CAP_MOVEABLE,
10830 .init_client = nfs41_init_client,
10831 .shutdown_client = nfs41_shutdown_client,
10832 .match_stateid = nfs41_match_stateid,
10833 .find_root_sec = nfs41_find_root_sec,
10834 .free_lock_state = nfs41_free_lock_state,
10835 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10836 .alloc_seqid = nfs_alloc_no_seqid,
10837 .session_trunk = nfs4_test_session_trunk,
10838 .call_sync_ops = &nfs41_call_sync_ops,
10839 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10840 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10841 .state_renewal_ops = &nfs41_state_renewal_ops,
10842 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10843 };
10844 #endif
10845
10846 #if defined(CONFIG_NFS_V4_2)
10847 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10848 .minor_version = 2,
10849 .init_caps = NFS_CAP_READDIRPLUS
10850 | NFS_CAP_ATOMIC_OPEN
10851 | NFS_CAP_POSIX_LOCK
10852 | NFS_CAP_STATEID_NFSV41
10853 | NFS_CAP_ATOMIC_OPEN_V1
10854 | NFS_CAP_LGOPEN
10855 | NFS_CAP_ALLOCATE
10856 | NFS_CAP_COPY
10857 | NFS_CAP_OFFLOAD_CANCEL
10858 | NFS_CAP_COPY_NOTIFY
10859 | NFS_CAP_DEALLOCATE
10860 | NFS_CAP_ZERO_RANGE
10861 | NFS_CAP_SEEK
10862 | NFS_CAP_LAYOUTSTATS
10863 | NFS_CAP_CLONE
10864 | NFS_CAP_LAYOUTERROR
10865 | NFS_CAP_READ_PLUS
10866 | NFS_CAP_MOVEABLE
10867 | NFS_CAP_OFFLOAD_STATUS,
10868 .init_client = nfs41_init_client,
10869 .shutdown_client = nfs41_shutdown_client,
10870 .match_stateid = nfs41_match_stateid,
10871 .find_root_sec = nfs41_find_root_sec,
10872 .free_lock_state = nfs41_free_lock_state,
10873 .call_sync_ops = &nfs41_call_sync_ops,
10874 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
10875 .alloc_seqid = nfs_alloc_no_seqid,
10876 .session_trunk = nfs4_test_session_trunk,
10877 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10878 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10879 .state_renewal_ops = &nfs41_state_renewal_ops,
10880 .mig_recovery_ops = &nfs41_mig_recovery_ops,
10881 };
10882 #endif
10883
10884 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10885 [0] = &nfs_v4_0_minor_ops,
10886 #if defined(CONFIG_NFS_V4_1)
10887 [1] = &nfs_v4_1_minor_ops,
10888 #endif
10889 #if defined(CONFIG_NFS_V4_2)
10890 [2] = &nfs_v4_2_minor_ops,
10891 #endif
10892 };
10893
nfs4_listxattr(struct dentry * dentry,char * list,size_t size)10894 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10895 {
10896 ssize_t error, error2, error3, error4 = 0;
10897 size_t left = size;
10898
10899 error = generic_listxattr(dentry, list, left);
10900 if (error < 0)
10901 return error;
10902 if (list) {
10903 list += error;
10904 left -= error;
10905 }
10906
10907 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, left);
10908 if (error2 < 0)
10909 return error2;
10910
10911 if (list) {
10912 list += error2;
10913 left -= error2;
10914 }
10915
10916 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left);
10917 if (error3 < 0)
10918 return error3;
10919 if (list) {
10920 list += error3;
10921 left -= error3;
10922 }
10923
10924 if (!nfs_server_capable(d_inode(dentry), NFS_CAP_SECURITY_LABEL)) {
10925 error4 = security_inode_listsecurity(d_inode(dentry), list, left);
10926 if (error4 < 0)
10927 return error4;
10928 }
10929
10930 error += error2 + error3 + error4;
10931 if (size && error > size)
10932 return -ERANGE;
10933 return error;
10934 }
10935
nfs4_enable_swap(struct inode * inode)10936 static void nfs4_enable_swap(struct inode *inode)
10937 {
10938 /* The state manager thread must always be running.
10939 * It will notice the client is a swapper, and stay put.
10940 */
10941 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10942
10943 nfs4_schedule_state_manager(clp);
10944 }
10945
nfs4_disable_swap(struct inode * inode)10946 static void nfs4_disable_swap(struct inode *inode)
10947 {
10948 /* The state manager thread will now exit once it is
10949 * woken.
10950 */
10951 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10952
10953 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10954 clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10955 wake_up_var(&clp->cl_state);
10956 }
10957
10958 static const struct inode_operations nfs4_dir_inode_operations = {
10959 .create = nfs_create,
10960 .lookup = nfs_lookup,
10961 .atomic_open = nfs_atomic_open,
10962 .link = nfs_link,
10963 .unlink = nfs_unlink,
10964 .symlink = nfs_symlink,
10965 .mkdir = nfs_mkdir,
10966 .rmdir = nfs_rmdir,
10967 .mknod = nfs_mknod,
10968 .rename = nfs_rename,
10969 .permission = nfs_permission,
10970 .getattr = nfs_getattr,
10971 .setattr = nfs_setattr,
10972 .listxattr = nfs4_listxattr,
10973 };
10974
10975 static const struct inode_operations nfs4_file_inode_operations = {
10976 .permission = nfs_permission,
10977 .getattr = nfs_getattr,
10978 .setattr = nfs_setattr,
10979 .listxattr = nfs4_listxattr,
10980 };
10981
nfs4_clone_server(struct nfs_server * source,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t flavor)10982 static struct nfs_server *nfs4_clone_server(struct nfs_server *source,
10983 struct nfs_fh *fh, struct nfs_fattr *fattr,
10984 rpc_authflavor_t flavor)
10985 {
10986 struct nfs_server *server;
10987 int error;
10988
10989 server = nfs_clone_server(source, fh, fattr, flavor);
10990 if (IS_ERR(server))
10991 return server;
10992
10993 error = nfs4_delegation_hash_alloc(server);
10994 if (error) {
10995 nfs_free_server(server);
10996 return ERR_PTR(error);
10997 }
10998
10999 return server;
11000 }
11001
11002 const struct nfs_rpc_ops nfs_v4_clientops = {
11003 .version = 4, /* protocol version */
11004 .dentry_ops = &nfs4_dentry_operations,
11005 .dir_inode_ops = &nfs4_dir_inode_operations,
11006 .file_inode_ops = &nfs4_file_inode_operations,
11007 .file_ops = &nfs4_file_operations,
11008 .getroot = nfs4_proc_get_root,
11009 .submount = nfs4_submount,
11010 .try_get_tree = nfs4_try_get_tree,
11011 .getattr = nfs4_proc_getattr,
11012 .setattr = nfs4_proc_setattr,
11013 .lookup = nfs4_proc_lookup,
11014 .lookupp = nfs4_proc_lookupp,
11015 .access = nfs4_proc_access,
11016 .readlink = nfs4_proc_readlink,
11017 .create = nfs4_proc_create,
11018 .remove = nfs4_proc_remove,
11019 .unlink_setup = nfs4_proc_unlink_setup,
11020 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
11021 .unlink_done = nfs4_proc_unlink_done,
11022 .rename_setup = nfs4_proc_rename_setup,
11023 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
11024 .rename_done = nfs4_proc_rename_done,
11025 .link = nfs4_proc_link,
11026 .symlink = nfs4_proc_symlink,
11027 .mkdir = nfs4_proc_mkdir,
11028 .rmdir = nfs4_proc_rmdir,
11029 .readdir = nfs4_proc_readdir,
11030 .mknod = nfs4_proc_mknod,
11031 .statfs = nfs4_proc_statfs,
11032 .fsinfo = nfs4_proc_fsinfo,
11033 .pathconf = nfs4_proc_pathconf,
11034 .set_capabilities = nfs4_server_capabilities,
11035 .decode_dirent = nfs4_decode_dirent,
11036 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
11037 .read_setup = nfs4_proc_read_setup,
11038 .read_done = nfs4_read_done,
11039 .write_setup = nfs4_proc_write_setup,
11040 .write_done = nfs4_write_done,
11041 .commit_setup = nfs4_proc_commit_setup,
11042 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
11043 .commit_done = nfs4_commit_done,
11044 .lock = nfs4_proc_lock,
11045 .clear_acl_cache = nfs4_zap_acl_attr,
11046 .close_context = nfs4_close_context,
11047 .open_context = nfs4_atomic_open,
11048 .have_delegation = nfs4_have_delegation,
11049 .return_delegation = nfs4_inode_return_delegation,
11050 .alloc_client = nfs4_alloc_client,
11051 .init_client = nfs4_init_client,
11052 .free_client = nfs4_free_client,
11053 .create_server = nfs4_create_server,
11054 .clone_server = nfs4_clone_server,
11055 .discover_trunking = nfs4_discover_trunking,
11056 .enable_swap = nfs4_enable_swap,
11057 .disable_swap = nfs4_disable_swap,
11058 };
11059
11060 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
11061 .name = XATTR_NAME_NFSV4_ACL,
11062 .list = nfs4_xattr_list_nfs4_acl,
11063 .get = nfs4_xattr_get_nfs4_acl,
11064 .set = nfs4_xattr_set_nfs4_acl,
11065 };
11066
11067 #if defined(CONFIG_NFS_V4_1)
11068 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
11069 .name = XATTR_NAME_NFSV4_DACL,
11070 .list = nfs4_xattr_list_nfs4_dacl,
11071 .get = nfs4_xattr_get_nfs4_dacl,
11072 .set = nfs4_xattr_set_nfs4_dacl,
11073 };
11074
11075 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
11076 .name = XATTR_NAME_NFSV4_SACL,
11077 .list = nfs4_xattr_list_nfs4_sacl,
11078 .get = nfs4_xattr_get_nfs4_sacl,
11079 .set = nfs4_xattr_set_nfs4_sacl,
11080 };
11081 #endif
11082
11083 #ifdef CONFIG_NFS_V4_2
11084 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
11085 .prefix = XATTR_USER_PREFIX,
11086 .get = nfs4_xattr_get_nfs4_user,
11087 .set = nfs4_xattr_set_nfs4_user,
11088 };
11089 #endif
11090
11091 const struct xattr_handler * const nfs4_xattr_handlers[] = {
11092 &nfs4_xattr_nfs4_acl_handler,
11093 #if defined(CONFIG_NFS_V4_1)
11094 &nfs4_xattr_nfs4_dacl_handler,
11095 &nfs4_xattr_nfs4_sacl_handler,
11096 #endif
11097 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
11098 &nfs4_xattr_nfs4_label_handler,
11099 #endif
11100 #ifdef CONFIG_NFS_V4_2
11101 &nfs4_xattr_nfs4_user_handler,
11102 #endif
11103 NULL
11104 };
11105