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