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