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