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