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