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