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