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