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