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