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