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