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