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