LOCKD: Ensure that nlmclnt_block resets block->b_status after a server reboot
[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/string.h>
42#include <linux/ratelimit.h>
43#include <linux/printk.h>
44#include <linux/slab.h>
45#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
50#include <linux/nfs_mount.h>
51#include <linux/namei.h>
52#include <linux/mount.h>
53#include <linux/module.h>
54#include <linux/nfs_idmap.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 "nfs4session.h"
67#include "fscache.h"
68
69#define NFSDBG_FACILITY NFSDBG_PROC
70
71#define NFS4_POLL_RETRY_MIN (HZ/10)
72#define NFS4_POLL_RETRY_MAX (15*HZ)
73
74struct nfs4_opendata;
75static int _nfs4_proc_open(struct nfs4_opendata *data);
76static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
77static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
78static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
79static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
80static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
81static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
82static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
85#ifdef CONFIG_NFS_V4_1
86static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
88#endif
89/* Prevent leaks of NFSv4 errors into userland */
90static int nfs4_map_errors(int err)
91{
92 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 case -NFS4ERR_LAYOUTTRYLATER:
97 case -NFS4ERR_RECALLCONFLICT:
98 return -EREMOTEIO;
99 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
106 case -NFS4ERR_MINOR_VERS_MISMATCH:
107 return -EPROTONOSUPPORT;
108 case -NFS4ERR_ACCESS:
109 return -EACCES;
110 case -NFS4ERR_FILE_OPEN:
111 return -EBUSY;
112 default:
113 dprintk("%s could not handle NFSv4 error %d\n",
114 __func__, -err);
115 break;
116 }
117 return -EIO;
118}
119
120/*
121 * This is our standard bitmap for GETATTR requests.
122 */
123const u32 nfs4_fattr_bitmap[3] = {
124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138};
139
140static const u32 nfs4_pnfs_open_bitmap[3] = {
141 FATTR4_WORD0_TYPE
142 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_SIZE
144 | FATTR4_WORD0_FSID
145 | FATTR4_WORD0_FILEID,
146 FATTR4_WORD1_MODE
147 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY,
155 FATTR4_WORD2_MDSTHRESHOLD
156};
157
158static const u32 nfs4_open_noattr_bitmap[3] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID,
162};
163
164const u32 nfs4_statfs_bitmap[2] = {
165 FATTR4_WORD0_FILES_AVAIL
166 | FATTR4_WORD0_FILES_FREE
167 | FATTR4_WORD0_FILES_TOTAL,
168 FATTR4_WORD1_SPACE_AVAIL
169 | FATTR4_WORD1_SPACE_FREE
170 | FATTR4_WORD1_SPACE_TOTAL
171};
172
173const u32 nfs4_pathconf_bitmap[2] = {
174 FATTR4_WORD0_MAXLINK
175 | FATTR4_WORD0_MAXNAME,
176 0
177};
178
179const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
180 | FATTR4_WORD0_MAXREAD
181 | FATTR4_WORD0_MAXWRITE
182 | FATTR4_WORD0_LEASE_TIME,
183 FATTR4_WORD1_TIME_DELTA
184 | FATTR4_WORD1_FS_LAYOUT_TYPES,
185 FATTR4_WORD2_LAYOUT_BLKSIZE
186};
187
188const u32 nfs4_fs_locations_bitmap[2] = {
189 FATTR4_WORD0_TYPE
190 | FATTR4_WORD0_CHANGE
191 | FATTR4_WORD0_SIZE
192 | FATTR4_WORD0_FSID
193 | FATTR4_WORD0_FILEID
194 | FATTR4_WORD0_FS_LOCATIONS,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID
205};
206
207static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
208 struct nfs4_readdir_arg *readdir)
209{
210 __be32 *start, *p;
211
212 if (cookie > 2) {
213 readdir->cookie = cookie;
214 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
215 return;
216 }
217
218 readdir->cookie = 0;
219 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
220 if (cookie == 2)
221 return;
222
223 /*
224 * NFSv4 servers do not return entries for '.' and '..'
225 * Therefore, we fake these entries here. We let '.'
226 * have cookie 0 and '..' have cookie 1. Note that
227 * when talking to the server, we always send cookie 0
228 * instead of 1 or 2.
229 */
230 start = p = kmap_atomic(*readdir->pages);
231
232 if (cookie == 0) {
233 *p++ = xdr_one; /* next */
234 *p++ = xdr_zero; /* cookie, first word */
235 *p++ = xdr_one; /* cookie, second word */
236 *p++ = xdr_one; /* entry len */
237 memcpy(p, ".\0\0\0", 4); /* entry */
238 p++;
239 *p++ = xdr_one; /* bitmap length */
240 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
241 *p++ = htonl(8); /* attribute buffer length */
242 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
243 }
244
245 *p++ = xdr_one; /* next */
246 *p++ = xdr_zero; /* cookie, first word */
247 *p++ = xdr_two; /* cookie, second word */
248 *p++ = xdr_two; /* entry len */
249 memcpy(p, "..\0\0", 4); /* entry */
250 p++;
251 *p++ = xdr_one; /* bitmap length */
252 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
253 *p++ = htonl(8); /* attribute buffer length */
254 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
255
256 readdir->pgbase = (char *)p - (char *)start;
257 readdir->count -= readdir->pgbase;
258 kunmap_atomic(start);
259}
260
261static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
262{
263 int res = 0;
264
265 might_sleep();
266
267 if (*timeout <= 0)
268 *timeout = NFS4_POLL_RETRY_MIN;
269 if (*timeout > NFS4_POLL_RETRY_MAX)
270 *timeout = NFS4_POLL_RETRY_MAX;
271 freezable_schedule_timeout_killable(*timeout);
272 if (fatal_signal_pending(current))
273 res = -ERESTARTSYS;
274 *timeout <<= 1;
275 return res;
276}
277
278/* This is the error handling routine for processes that are allowed
279 * to sleep.
280 */
281static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
282{
283 struct nfs_client *clp = server->nfs_client;
284 struct nfs4_state *state = exception->state;
285 struct inode *inode = exception->inode;
286 int ret = errorcode;
287
288 exception->retry = 0;
289 switch(errorcode) {
290 case 0:
291 return 0;
292 case -NFS4ERR_OPENMODE:
293 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
294 nfs4_inode_return_delegation(inode);
295 exception->retry = 1;
296 return 0;
297 }
298 if (state == NULL)
299 break;
300 ret = nfs4_schedule_stateid_recovery(server, state);
301 if (ret < 0)
302 break;
303 goto wait_on_recovery;
304 case -NFS4ERR_DELEG_REVOKED:
305 case -NFS4ERR_ADMIN_REVOKED:
306 case -NFS4ERR_BAD_STATEID:
307 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
308 nfs_remove_bad_delegation(inode);
309 exception->retry = 1;
310 break;
311 }
312 if (state == NULL)
313 break;
314 ret = nfs4_schedule_stateid_recovery(server, state);
315 if (ret < 0)
316 break;
317 goto wait_on_recovery;
318 case -NFS4ERR_EXPIRED:
319 if (state != NULL) {
320 ret = nfs4_schedule_stateid_recovery(server, state);
321 if (ret < 0)
322 break;
323 }
324 case -NFS4ERR_STALE_STATEID:
325 case -NFS4ERR_STALE_CLIENTID:
326 nfs4_schedule_lease_recovery(clp);
327 goto wait_on_recovery;
328#if defined(CONFIG_NFS_V4_1)
329 case -NFS4ERR_BADSESSION:
330 case -NFS4ERR_BADSLOT:
331 case -NFS4ERR_BAD_HIGH_SLOT:
332 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
333 case -NFS4ERR_DEADSESSION:
334 case -NFS4ERR_SEQ_FALSE_RETRY:
335 case -NFS4ERR_SEQ_MISORDERED:
336 dprintk("%s ERROR: %d Reset session\n", __func__,
337 errorcode);
338 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
339 goto wait_on_recovery;
340#endif /* defined(CONFIG_NFS_V4_1) */
341 case -NFS4ERR_FILE_OPEN:
342 if (exception->timeout > HZ) {
343 /* We have retried a decent amount, time to
344 * fail
345 */
346 ret = -EBUSY;
347 break;
348 }
349 case -NFS4ERR_GRACE:
350 case -NFS4ERR_DELAY:
351 ret = nfs4_delay(server->client, &exception->timeout);
352 if (ret != 0)
353 break;
354 case -NFS4ERR_RETRY_UNCACHED_REP:
355 case -NFS4ERR_OLD_STATEID:
356 exception->retry = 1;
357 break;
358 case -NFS4ERR_BADOWNER:
359 /* The following works around a Linux server bug! */
360 case -NFS4ERR_BADNAME:
361 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
362 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
363 exception->retry = 1;
364 printk(KERN_WARNING "NFS: v4 server %s "
365 "does not accept raw "
366 "uid/gids. "
367 "Reenabling the idmapper.\n",
368 server->nfs_client->cl_hostname);
369 }
370 }
371 /* We failed to handle the error */
372 return nfs4_map_errors(ret);
373wait_on_recovery:
374 ret = nfs4_wait_clnt_recover(clp);
375 if (ret == 0)
376 exception->retry = 1;
377 return ret;
378}
379
380
381static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
382{
383 spin_lock(&clp->cl_lock);
384 if (time_before(clp->cl_last_renewal,timestamp))
385 clp->cl_last_renewal = timestamp;
386 spin_unlock(&clp->cl_lock);
387}
388
389static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
390{
391 do_renew_lease(server->nfs_client, timestamp);
392}
393
394#if defined(CONFIG_NFS_V4_1)
395
396static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
397{
398 struct nfs4_session *session;
399 struct nfs4_slot_table *tbl;
400 bool send_new_highest_used_slotid = false;
401
402 if (!res->sr_slot) {
403 /* just wake up the next guy waiting since
404 * we may have not consumed a slot after all */
405 dprintk("%s: No slot\n", __func__);
406 return;
407 }
408 tbl = res->sr_slot->table;
409 session = tbl->session;
410
411 spin_lock(&tbl->slot_tbl_lock);
412 /* Be nice to the server: try to ensure that the last transmitted
413 * value for highest_user_slotid <= target_highest_slotid
414 */
415 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
416 send_new_highest_used_slotid = true;
417
418 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
419 send_new_highest_used_slotid = false;
420 goto out_unlock;
421 }
422 nfs4_free_slot(tbl, res->sr_slot);
423
424 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
425 send_new_highest_used_slotid = false;
426out_unlock:
427 spin_unlock(&tbl->slot_tbl_lock);
428 res->sr_slot = NULL;
429 if (send_new_highest_used_slotid)
430 nfs41_server_notify_highest_slotid_update(session->clp);
431}
432
433static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
434{
435 struct nfs4_session *session;
436 struct nfs4_slot *slot;
437 struct nfs_client *clp;
438 bool interrupted = false;
439 int ret = 1;
440
441 /* don't increment the sequence number if the task wasn't sent */
442 if (!RPC_WAS_SENT(task))
443 goto out;
444
445 slot = res->sr_slot;
446 session = slot->table->session;
447
448 if (slot->interrupted) {
449 slot->interrupted = 0;
450 interrupted = true;
451 }
452
453 /* Check the SEQUENCE operation status */
454 switch (res->sr_status) {
455 case 0:
456 /* Update the slot's sequence and clientid lease timer */
457 ++slot->seq_nr;
458 clp = session->clp;
459 do_renew_lease(clp, res->sr_timestamp);
460 /* Check sequence flags */
461 if (res->sr_status_flags != 0)
462 nfs4_schedule_lease_recovery(clp);
463 nfs41_update_target_slotid(slot->table, slot, res);
464 break;
465 case 1:
466 /*
467 * sr_status remains 1 if an RPC level error occurred.
468 * The server may or may not have processed the sequence
469 * operation..
470 * Mark the slot as having hosted an interrupted RPC call.
471 */
472 slot->interrupted = 1;
473 goto out;
474 case -NFS4ERR_DELAY:
475 /* The server detected a resend of the RPC call and
476 * returned NFS4ERR_DELAY as per Section 2.10.6.2
477 * of RFC5661.
478 */
479 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
480 __func__,
481 slot->slot_nr,
482 slot->seq_nr);
483 goto out_retry;
484 case -NFS4ERR_BADSLOT:
485 /*
486 * The slot id we used was probably retired. Try again
487 * using a different slot id.
488 */
489 goto retry_nowait;
490 case -NFS4ERR_SEQ_MISORDERED:
491 /*
492 * Was the last operation on this sequence interrupted?
493 * If so, retry after bumping the sequence number.
494 */
495 if (interrupted) {
496 ++slot->seq_nr;
497 goto retry_nowait;
498 }
499 /*
500 * Could this slot have been previously retired?
501 * If so, then the server may be expecting seq_nr = 1!
502 */
503 if (slot->seq_nr != 1) {
504 slot->seq_nr = 1;
505 goto retry_nowait;
506 }
507 break;
508 case -NFS4ERR_SEQ_FALSE_RETRY:
509 ++slot->seq_nr;
510 goto retry_nowait;
511 default:
512 /* Just update the slot sequence no. */
513 ++slot->seq_nr;
514 }
515out:
516 /* The session may be reset by one of the error handlers. */
517 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
518 nfs41_sequence_free_slot(res);
519 return ret;
520retry_nowait:
521 if (rpc_restart_call_prepare(task)) {
522 task->tk_status = 0;
523 ret = 0;
524 }
525 goto out;
526out_retry:
527 if (!rpc_restart_call(task))
528 goto out;
529 rpc_delay(task, NFS4_POLL_RETRY_MAX);
530 return 0;
531}
532
533static int nfs4_sequence_done(struct rpc_task *task,
534 struct nfs4_sequence_res *res)
535{
536 if (res->sr_slot == NULL)
537 return 1;
538 return nfs41_sequence_done(task, res);
539}
540
541static void nfs41_init_sequence(struct nfs4_sequence_args *args,
542 struct nfs4_sequence_res *res, int cache_reply)
543{
544 args->sa_slot = NULL;
545 args->sa_cache_this = 0;
546 args->sa_privileged = 0;
547 if (cache_reply)
548 args->sa_cache_this = 1;
549 res->sr_slot = NULL;
550}
551
552static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
553{
554 args->sa_privileged = 1;
555}
556
557int nfs41_setup_sequence(struct nfs4_session *session,
558 struct nfs4_sequence_args *args,
559 struct nfs4_sequence_res *res,
560 struct rpc_task *task)
561{
562 struct nfs4_slot *slot;
563 struct nfs4_slot_table *tbl;
564
565 dprintk("--> %s\n", __func__);
566 /* slot already allocated? */
567 if (res->sr_slot != NULL)
568 goto out_success;
569
570 tbl = &session->fc_slot_table;
571
572 task->tk_timeout = 0;
573
574 spin_lock(&tbl->slot_tbl_lock);
575 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
576 !args->sa_privileged) {
577 /* The state manager will wait until the slot table is empty */
578 dprintk("%s session is draining\n", __func__);
579 goto out_sleep;
580 }
581
582 slot = nfs4_alloc_slot(tbl);
583 if (IS_ERR(slot)) {
584 /* If out of memory, try again in 1/4 second */
585 if (slot == ERR_PTR(-ENOMEM))
586 task->tk_timeout = HZ >> 2;
587 dprintk("<-- %s: no free slots\n", __func__);
588 goto out_sleep;
589 }
590 spin_unlock(&tbl->slot_tbl_lock);
591
592 args->sa_slot = slot;
593
594 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
595 slot->slot_nr, slot->seq_nr);
596
597 res->sr_slot = slot;
598 res->sr_timestamp = jiffies;
599 res->sr_status_flags = 0;
600 /*
601 * sr_status is only set in decode_sequence, and so will remain
602 * set to 1 if an rpc level failure occurs.
603 */
604 res->sr_status = 1;
605out_success:
606 rpc_call_start(task);
607 return 0;
608out_sleep:
609 /* Privileged tasks are queued with top priority */
610 if (args->sa_privileged)
611 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
612 NULL, RPC_PRIORITY_PRIVILEGED);
613 else
614 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
615 spin_unlock(&tbl->slot_tbl_lock);
616 return -EAGAIN;
617}
618EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
619
620int nfs4_setup_sequence(const struct nfs_server *server,
621 struct nfs4_sequence_args *args,
622 struct nfs4_sequence_res *res,
623 struct rpc_task *task)
624{
625 struct nfs4_session *session = nfs4_get_session(server);
626 int ret = 0;
627
628 if (session == NULL) {
629 rpc_call_start(task);
630 goto out;
631 }
632
633 dprintk("--> %s clp %p session %p sr_slot %d\n",
634 __func__, session->clp, session, res->sr_slot ?
635 res->sr_slot->slot_nr : -1);
636
637 ret = nfs41_setup_sequence(session, args, res, task);
638out:
639 dprintk("<-- %s status=%d\n", __func__, ret);
640 return ret;
641}
642
643struct nfs41_call_sync_data {
644 const struct nfs_server *seq_server;
645 struct nfs4_sequence_args *seq_args;
646 struct nfs4_sequence_res *seq_res;
647};
648
649static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
650{
651 struct nfs41_call_sync_data *data = calldata;
652 struct nfs4_session *session = nfs4_get_session(data->seq_server);
653
654 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
655
656 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
657}
658
659static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
660{
661 struct nfs41_call_sync_data *data = calldata;
662
663 nfs41_sequence_done(task, data->seq_res);
664}
665
666static const struct rpc_call_ops nfs41_call_sync_ops = {
667 .rpc_call_prepare = nfs41_call_sync_prepare,
668 .rpc_call_done = nfs41_call_sync_done,
669};
670
671static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
672 struct nfs_server *server,
673 struct rpc_message *msg,
674 struct nfs4_sequence_args *args,
675 struct nfs4_sequence_res *res)
676{
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
680 .seq_server = server,
681 .seq_args = args,
682 .seq_res = res,
683 };
684 struct rpc_task_setup task_setup = {
685 .rpc_client = clnt,
686 .rpc_message = msg,
687 .callback_ops = &nfs41_call_sync_ops,
688 .callback_data = &data
689 };
690
691 task = rpc_run_task(&task_setup);
692 if (IS_ERR(task))
693 ret = PTR_ERR(task);
694 else {
695 ret = task->tk_status;
696 rpc_put_task(task);
697 }
698 return ret;
699}
700
701#else
702static
703void nfs41_init_sequence(struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res, int cache_reply)
705{
706}
707
708static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
709{
710}
711
712
713static int nfs4_sequence_done(struct rpc_task *task,
714 struct nfs4_sequence_res *res)
715{
716 return 1;
717}
718#endif /* CONFIG_NFS_V4_1 */
719
720static
721int _nfs4_call_sync(struct rpc_clnt *clnt,
722 struct nfs_server *server,
723 struct rpc_message *msg,
724 struct nfs4_sequence_args *args,
725 struct nfs4_sequence_res *res)
726{
727 return rpc_call_sync(clnt, msg, 0);
728}
729
730static
731int nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
738 nfs41_init_sequence(args, res, cache_reply);
739 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
740 args, res);
741}
742
743static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
744{
745 struct nfs_inode *nfsi = NFS_I(dir);
746
747 spin_lock(&dir->i_lock);
748 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
749 if (!cinfo->atomic || cinfo->before != dir->i_version)
750 nfs_force_lookup_revalidate(dir);
751 dir->i_version = cinfo->after;
752 nfs_fscache_invalidate(dir);
753 spin_unlock(&dir->i_lock);
754}
755
756struct nfs4_opendata {
757 struct kref kref;
758 struct nfs_openargs o_arg;
759 struct nfs_openres o_res;
760 struct nfs_open_confirmargs c_arg;
761 struct nfs_open_confirmres c_res;
762 struct nfs4_string owner_name;
763 struct nfs4_string group_name;
764 struct nfs_fattr f_attr;
765 struct dentry *dir;
766 struct dentry *dentry;
767 struct nfs4_state_owner *owner;
768 struct nfs4_state *state;
769 struct iattr attrs;
770 unsigned long timestamp;
771 unsigned int rpc_done : 1;
772 int rpc_status;
773 int cancelled;
774};
775
776static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
777 int err, struct nfs4_exception *exception)
778{
779 if (err != -EINVAL)
780 return false;
781 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
782 return false;
783 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
784 exception->retry = 1;
785 return true;
786}
787
788static enum open_claim_type4
789nfs4_map_atomic_open_claim(struct nfs_server *server,
790 enum open_claim_type4 claim)
791{
792 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
793 return claim;
794 switch (claim) {
795 default:
796 return claim;
797 case NFS4_OPEN_CLAIM_FH:
798 return NFS4_OPEN_CLAIM_NULL;
799 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
800 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
801 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
802 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
803 }
804}
805
806static void nfs4_init_opendata_res(struct nfs4_opendata *p)
807{
808 p->o_res.f_attr = &p->f_attr;
809 p->o_res.seqid = p->o_arg.seqid;
810 p->c_res.seqid = p->c_arg.seqid;
811 p->o_res.server = p->o_arg.server;
812 p->o_res.access_request = p->o_arg.access;
813 nfs_fattr_init(&p->f_attr);
814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
815}
816
817static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
819 const struct iattr *attrs,
820 enum open_claim_type4 claim,
821 gfp_t gfp_mask)
822{
823 struct dentry *parent = dget_parent(dentry);
824 struct inode *dir = parent->d_inode;
825 struct nfs_server *server = NFS_SERVER(dir);
826 struct nfs4_opendata *p;
827
828 p = kzalloc(sizeof(*p), gfp_mask);
829 if (p == NULL)
830 goto err;
831 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
832 if (p->o_arg.seqid == NULL)
833 goto err_free;
834 nfs_sb_active(dentry->d_sb);
835 p->dentry = dget(dentry);
836 p->dir = parent;
837 p->owner = sp;
838 atomic_inc(&sp->so_count);
839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
841 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
842 * will return permission denied for all bits until close */
843 if (!(flags & O_EXCL)) {
844 /* ask server to check for all possible rights as results
845 * are cached */
846 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
847 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
848 }
849 p->o_arg.clientid = server->nfs_client->cl_clientid;
850 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
851 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
852 p->o_arg.name = &dentry->d_name;
853 p->o_arg.server = server;
854 p->o_arg.bitmask = server->attr_bitmask;
855 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
856 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
857 switch (p->o_arg.claim) {
858 case NFS4_OPEN_CLAIM_NULL:
859 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
860 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
861 p->o_arg.fh = NFS_FH(dir);
862 break;
863 case NFS4_OPEN_CLAIM_PREVIOUS:
864 case NFS4_OPEN_CLAIM_FH:
865 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
866 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
867 p->o_arg.fh = NFS_FH(dentry->d_inode);
868 }
869 if (attrs != NULL && attrs->ia_valid != 0) {
870 __be32 verf[2];
871
872 p->o_arg.u.attrs = &p->attrs;
873 memcpy(&p->attrs, attrs, sizeof(p->attrs));
874
875 verf[0] = jiffies;
876 verf[1] = current->pid;
877 memcpy(p->o_arg.u.verifier.data, verf,
878 sizeof(p->o_arg.u.verifier.data));
879 }
880 p->c_arg.fh = &p->o_res.fh;
881 p->c_arg.stateid = &p->o_res.stateid;
882 p->c_arg.seqid = p->o_arg.seqid;
883 nfs4_init_opendata_res(p);
884 kref_init(&p->kref);
885 return p;
886err_free:
887 kfree(p);
888err:
889 dput(parent);
890 return NULL;
891}
892
893static void nfs4_opendata_free(struct kref *kref)
894{
895 struct nfs4_opendata *p = container_of(kref,
896 struct nfs4_opendata, kref);
897 struct super_block *sb = p->dentry->d_sb;
898
899 nfs_free_seqid(p->o_arg.seqid);
900 if (p->state != NULL)
901 nfs4_put_open_state(p->state);
902 nfs4_put_state_owner(p->owner);
903 dput(p->dir);
904 dput(p->dentry);
905 nfs_sb_deactive(sb);
906 nfs_fattr_free_names(&p->f_attr);
907 kfree(p);
908}
909
910static void nfs4_opendata_put(struct nfs4_opendata *p)
911{
912 if (p != NULL)
913 kref_put(&p->kref, nfs4_opendata_free);
914}
915
916static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
917{
918 int ret;
919
920 ret = rpc_wait_for_completion_task(task);
921 return ret;
922}
923
924static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
925{
926 int ret = 0;
927
928 if (open_mode & (O_EXCL|O_TRUNC))
929 goto out;
930 switch (mode & (FMODE_READ|FMODE_WRITE)) {
931 case FMODE_READ:
932 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
933 && state->n_rdonly != 0;
934 break;
935 case FMODE_WRITE:
936 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
937 && state->n_wronly != 0;
938 break;
939 case FMODE_READ|FMODE_WRITE:
940 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
941 && state->n_rdwr != 0;
942 }
943out:
944 return ret;
945}
946
947static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
948{
949 if (delegation == NULL)
950 return 0;
951 if ((delegation->type & fmode) != fmode)
952 return 0;
953 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
954 return 0;
955 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
956 return 0;
957 nfs_mark_delegation_referenced(delegation);
958 return 1;
959}
960
961static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
962{
963 switch (fmode) {
964 case FMODE_WRITE:
965 state->n_wronly++;
966 break;
967 case FMODE_READ:
968 state->n_rdonly++;
969 break;
970 case FMODE_READ|FMODE_WRITE:
971 state->n_rdwr++;
972 }
973 nfs4_state_set_mode_locked(state, state->state | fmode);
974}
975
976static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
977{
978 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
979 nfs4_stateid_copy(&state->stateid, stateid);
980 nfs4_stateid_copy(&state->open_stateid, stateid);
981 set_bit(NFS_OPEN_STATE, &state->flags);
982 switch (fmode) {
983 case FMODE_READ:
984 set_bit(NFS_O_RDONLY_STATE, &state->flags);
985 break;
986 case FMODE_WRITE:
987 set_bit(NFS_O_WRONLY_STATE, &state->flags);
988 break;
989 case FMODE_READ|FMODE_WRITE:
990 set_bit(NFS_O_RDWR_STATE, &state->flags);
991 }
992}
993
994static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
995{
996 write_seqlock(&state->seqlock);
997 nfs_set_open_stateid_locked(state, stateid, fmode);
998 write_sequnlock(&state->seqlock);
999}
1000
1001static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1002{
1003 /*
1004 * Protect the call to nfs4_state_set_mode_locked and
1005 * serialise the stateid update
1006 */
1007 write_seqlock(&state->seqlock);
1008 if (deleg_stateid != NULL) {
1009 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1010 set_bit(NFS_DELEGATED_STATE, &state->flags);
1011 }
1012 if (open_stateid != NULL)
1013 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1014 write_sequnlock(&state->seqlock);
1015 spin_lock(&state->owner->so_lock);
1016 update_open_stateflags(state, fmode);
1017 spin_unlock(&state->owner->so_lock);
1018}
1019
1020static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1021{
1022 struct nfs_inode *nfsi = NFS_I(state->inode);
1023 struct nfs_delegation *deleg_cur;
1024 int ret = 0;
1025
1026 fmode &= (FMODE_READ|FMODE_WRITE);
1027
1028 rcu_read_lock();
1029 deleg_cur = rcu_dereference(nfsi->delegation);
1030 if (deleg_cur == NULL)
1031 goto no_delegation;
1032
1033 spin_lock(&deleg_cur->lock);
1034 if (nfsi->delegation != deleg_cur ||
1035 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1036 (deleg_cur->type & fmode) != fmode)
1037 goto no_delegation_unlock;
1038
1039 if (delegation == NULL)
1040 delegation = &deleg_cur->stateid;
1041 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1042 goto no_delegation_unlock;
1043
1044 nfs_mark_delegation_referenced(deleg_cur);
1045 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1046 ret = 1;
1047no_delegation_unlock:
1048 spin_unlock(&deleg_cur->lock);
1049no_delegation:
1050 rcu_read_unlock();
1051
1052 if (!ret && open_stateid != NULL) {
1053 __update_open_stateid(state, open_stateid, NULL, fmode);
1054 ret = 1;
1055 }
1056
1057 return ret;
1058}
1059
1060
1061static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1062{
1063 struct nfs_delegation *delegation;
1064
1065 rcu_read_lock();
1066 delegation = rcu_dereference(NFS_I(inode)->delegation);
1067 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1068 rcu_read_unlock();
1069 return;
1070 }
1071 rcu_read_unlock();
1072 nfs4_inode_return_delegation(inode);
1073}
1074
1075static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1076{
1077 struct nfs4_state *state = opendata->state;
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_delegation *delegation;
1080 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1081 fmode_t fmode = opendata->o_arg.fmode;
1082 nfs4_stateid stateid;
1083 int ret = -EAGAIN;
1084
1085 for (;;) {
1086 if (can_open_cached(state, fmode, open_mode)) {
1087 spin_lock(&state->owner->so_lock);
1088 if (can_open_cached(state, fmode, open_mode)) {
1089 update_open_stateflags(state, fmode);
1090 spin_unlock(&state->owner->so_lock);
1091 goto out_return_state;
1092 }
1093 spin_unlock(&state->owner->so_lock);
1094 }
1095 rcu_read_lock();
1096 delegation = rcu_dereference(nfsi->delegation);
1097 if (!can_open_delegated(delegation, fmode)) {
1098 rcu_read_unlock();
1099 break;
1100 }
1101 /* Save the delegation */
1102 nfs4_stateid_copy(&stateid, &delegation->stateid);
1103 rcu_read_unlock();
1104 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1105 if (ret != 0)
1106 goto out;
1107 ret = -EAGAIN;
1108
1109 /* Try to update the stateid using the delegation */
1110 if (update_open_stateid(state, NULL, &stateid, fmode))
1111 goto out_return_state;
1112 }
1113out:
1114 return ERR_PTR(ret);
1115out_return_state:
1116 atomic_inc(&state->count);
1117 return state;
1118}
1119
1120static void
1121nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1122{
1123 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1124 struct nfs_delegation *delegation;
1125 int delegation_flags = 0;
1126
1127 rcu_read_lock();
1128 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1129 if (delegation)
1130 delegation_flags = delegation->flags;
1131 rcu_read_unlock();
1132 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1133 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1134 "returning a delegation for "
1135 "OPEN(CLAIM_DELEGATE_CUR)\n",
1136 clp->cl_hostname);
1137 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1138 nfs_inode_set_delegation(state->inode,
1139 data->owner->so_cred,
1140 &data->o_res);
1141 else
1142 nfs_inode_reclaim_delegation(state->inode,
1143 data->owner->so_cred,
1144 &data->o_res);
1145}
1146
1147/*
1148 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1149 * and update the nfs4_state.
1150 */
1151static struct nfs4_state *
1152_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1153{
1154 struct inode *inode = data->state->inode;
1155 struct nfs4_state *state = data->state;
1156 int ret;
1157
1158 if (!data->rpc_done) {
1159 ret = data->rpc_status;
1160 goto err;
1161 }
1162
1163 ret = -ESTALE;
1164 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1165 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1166 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1167 goto err;
1168
1169 ret = -ENOMEM;
1170 state = nfs4_get_open_state(inode, data->owner);
1171 if (state == NULL)
1172 goto err;
1173
1174 ret = nfs_refresh_inode(inode, &data->f_attr);
1175 if (ret)
1176 goto err;
1177
1178 if (data->o_res.delegation_type != 0)
1179 nfs4_opendata_check_deleg(data, state);
1180 update_open_stateid(state, &data->o_res.stateid, NULL,
1181 data->o_arg.fmode);
1182
1183 return state;
1184err:
1185 return ERR_PTR(ret);
1186
1187}
1188
1189static struct nfs4_state *
1190_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1191{
1192 struct inode *inode;
1193 struct nfs4_state *state = NULL;
1194 int ret;
1195
1196 if (!data->rpc_done) {
1197 state = nfs4_try_open_cached(data);
1198 goto out;
1199 }
1200
1201 ret = -EAGAIN;
1202 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1203 goto err;
1204 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1205 ret = PTR_ERR(inode);
1206 if (IS_ERR(inode))
1207 goto err;
1208 ret = -ENOMEM;
1209 state = nfs4_get_open_state(inode, data->owner);
1210 if (state == NULL)
1211 goto err_put_inode;
1212 if (data->o_res.delegation_type != 0)
1213 nfs4_opendata_check_deleg(data, state);
1214 update_open_stateid(state, &data->o_res.stateid, NULL,
1215 data->o_arg.fmode);
1216 iput(inode);
1217out:
1218 nfs_release_seqid(data->o_arg.seqid);
1219 return state;
1220err_put_inode:
1221 iput(inode);
1222err:
1223 return ERR_PTR(ret);
1224}
1225
1226static struct nfs4_state *
1227nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1228{
1229 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1230 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1231 return _nfs4_opendata_to_nfs4_state(data);
1232}
1233
1234static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1235{
1236 struct nfs_inode *nfsi = NFS_I(state->inode);
1237 struct nfs_open_context *ctx;
1238
1239 spin_lock(&state->inode->i_lock);
1240 list_for_each_entry(ctx, &nfsi->open_files, list) {
1241 if (ctx->state != state)
1242 continue;
1243 get_nfs_open_context(ctx);
1244 spin_unlock(&state->inode->i_lock);
1245 return ctx;
1246 }
1247 spin_unlock(&state->inode->i_lock);
1248 return ERR_PTR(-ENOENT);
1249}
1250
1251static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1252 struct nfs4_state *state, enum open_claim_type4 claim)
1253{
1254 struct nfs4_opendata *opendata;
1255
1256 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1257 NULL, claim, GFP_NOFS);
1258 if (opendata == NULL)
1259 return ERR_PTR(-ENOMEM);
1260 opendata->state = state;
1261 atomic_inc(&state->count);
1262 return opendata;
1263}
1264
1265static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1266{
1267 struct nfs4_state *newstate;
1268 int ret;
1269
1270 opendata->o_arg.open_flags = 0;
1271 opendata->o_arg.fmode = fmode;
1272 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1273 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1274 nfs4_init_opendata_res(opendata);
1275 ret = _nfs4_recover_proc_open(opendata);
1276 if (ret != 0)
1277 return ret;
1278 newstate = nfs4_opendata_to_nfs4_state(opendata);
1279 if (IS_ERR(newstate))
1280 return PTR_ERR(newstate);
1281 nfs4_close_state(newstate, fmode);
1282 *res = newstate;
1283 return 0;
1284}
1285
1286static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1287{
1288 struct nfs4_state *newstate;
1289 int ret;
1290
1291 /* memory barrier prior to reading state->n_* */
1292 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1293 smp_rmb();
1294 if (state->n_rdwr != 0) {
1295 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1296 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1297 if (ret != 0)
1298 return ret;
1299 if (newstate != state)
1300 return -ESTALE;
1301 }
1302 if (state->n_wronly != 0) {
1303 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1304 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1305 if (ret != 0)
1306 return ret;
1307 if (newstate != state)
1308 return -ESTALE;
1309 }
1310 if (state->n_rdonly != 0) {
1311 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1312 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1313 if (ret != 0)
1314 return ret;
1315 if (newstate != state)
1316 return -ESTALE;
1317 }
1318 /*
1319 * We may have performed cached opens for all three recoveries.
1320 * Check if we need to update the current stateid.
1321 */
1322 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1323 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1324 write_seqlock(&state->seqlock);
1325 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1326 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1327 write_sequnlock(&state->seqlock);
1328 }
1329 return 0;
1330}
1331
1332/*
1333 * OPEN_RECLAIM:
1334 * reclaim state on the server after a reboot.
1335 */
1336static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1337{
1338 struct nfs_delegation *delegation;
1339 struct nfs4_opendata *opendata;
1340 fmode_t delegation_type = 0;
1341 int status;
1342
1343 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1344 NFS4_OPEN_CLAIM_PREVIOUS);
1345 if (IS_ERR(opendata))
1346 return PTR_ERR(opendata);
1347 rcu_read_lock();
1348 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1349 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1350 delegation_type = delegation->type;
1351 rcu_read_unlock();
1352 opendata->o_arg.u.delegation_type = delegation_type;
1353 status = nfs4_open_recover(opendata, state);
1354 nfs4_opendata_put(opendata);
1355 return status;
1356}
1357
1358static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1359{
1360 struct nfs_server *server = NFS_SERVER(state->inode);
1361 struct nfs4_exception exception = { };
1362 int err;
1363 do {
1364 err = _nfs4_do_open_reclaim(ctx, state);
1365 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1366 continue;
1367 if (err != -NFS4ERR_DELAY)
1368 break;
1369 nfs4_handle_exception(server, err, &exception);
1370 } while (exception.retry);
1371 return err;
1372}
1373
1374static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1375{
1376 struct nfs_open_context *ctx;
1377 int ret;
1378
1379 ctx = nfs4_state_find_open_context(state);
1380 if (IS_ERR(ctx))
1381 return -EAGAIN;
1382 ret = nfs4_do_open_reclaim(ctx, state);
1383 put_nfs_open_context(ctx);
1384 return ret;
1385}
1386
1387static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1388{
1389 switch (err) {
1390 default:
1391 printk(KERN_ERR "NFS: %s: unhandled error "
1392 "%d.\n", __func__, err);
1393 case 0:
1394 case -ENOENT:
1395 case -ESTALE:
1396 break;
1397 case -NFS4ERR_BADSESSION:
1398 case -NFS4ERR_BADSLOT:
1399 case -NFS4ERR_BAD_HIGH_SLOT:
1400 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1401 case -NFS4ERR_DEADSESSION:
1402 set_bit(NFS_DELEGATED_STATE, &state->flags);
1403 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1404 return -EAGAIN;
1405 case -NFS4ERR_STALE_CLIENTID:
1406 case -NFS4ERR_STALE_STATEID:
1407 set_bit(NFS_DELEGATED_STATE, &state->flags);
1408 case -NFS4ERR_EXPIRED:
1409 /* Don't recall a delegation if it was lost */
1410 nfs4_schedule_lease_recovery(server->nfs_client);
1411 return -EAGAIN;
1412 case -NFS4ERR_DELEG_REVOKED:
1413 case -NFS4ERR_ADMIN_REVOKED:
1414 case -NFS4ERR_BAD_STATEID:
1415 case -NFS4ERR_OPENMODE:
1416 nfs_inode_find_state_and_recover(state->inode,
1417 stateid);
1418 nfs4_schedule_stateid_recovery(server, state);
1419 return 0;
1420 case -NFS4ERR_DELAY:
1421 case -NFS4ERR_GRACE:
1422 set_bit(NFS_DELEGATED_STATE, &state->flags);
1423 ssleep(1);
1424 return -EAGAIN;
1425 case -ENOMEM:
1426 case -NFS4ERR_DENIED:
1427 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1428 return 0;
1429 }
1430 return err;
1431}
1432
1433int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1434{
1435 struct nfs_server *server = NFS_SERVER(state->inode);
1436 struct nfs4_opendata *opendata;
1437 int err;
1438
1439 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1440 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1441 if (IS_ERR(opendata))
1442 return PTR_ERR(opendata);
1443 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1444 err = nfs4_open_recover(opendata, state);
1445 nfs4_opendata_put(opendata);
1446 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1447}
1448
1449static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1450{
1451 struct nfs4_opendata *data = calldata;
1452
1453 data->rpc_status = task->tk_status;
1454 if (data->rpc_status == 0) {
1455 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1456 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1457 renew_lease(data->o_res.server, data->timestamp);
1458 data->rpc_done = 1;
1459 }
1460}
1461
1462static void nfs4_open_confirm_release(void *calldata)
1463{
1464 struct nfs4_opendata *data = calldata;
1465 struct nfs4_state *state = NULL;
1466
1467 /* If this request hasn't been cancelled, do nothing */
1468 if (data->cancelled == 0)
1469 goto out_free;
1470 /* In case of error, no cleanup! */
1471 if (!data->rpc_done)
1472 goto out_free;
1473 state = nfs4_opendata_to_nfs4_state(data);
1474 if (!IS_ERR(state))
1475 nfs4_close_state(state, data->o_arg.fmode);
1476out_free:
1477 nfs4_opendata_put(data);
1478}
1479
1480static const struct rpc_call_ops nfs4_open_confirm_ops = {
1481 .rpc_call_done = nfs4_open_confirm_done,
1482 .rpc_release = nfs4_open_confirm_release,
1483};
1484
1485/*
1486 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1487 */
1488static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1489{
1490 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1491 struct rpc_task *task;
1492 struct rpc_message msg = {
1493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1494 .rpc_argp = &data->c_arg,
1495 .rpc_resp = &data->c_res,
1496 .rpc_cred = data->owner->so_cred,
1497 };
1498 struct rpc_task_setup task_setup_data = {
1499 .rpc_client = server->client,
1500 .rpc_message = &msg,
1501 .callback_ops = &nfs4_open_confirm_ops,
1502 .callback_data = data,
1503 .workqueue = nfsiod_workqueue,
1504 .flags = RPC_TASK_ASYNC,
1505 };
1506 int status;
1507
1508 kref_get(&data->kref);
1509 data->rpc_done = 0;
1510 data->rpc_status = 0;
1511 data->timestamp = jiffies;
1512 task = rpc_run_task(&task_setup_data);
1513 if (IS_ERR(task))
1514 return PTR_ERR(task);
1515 status = nfs4_wait_for_completion_rpc_task(task);
1516 if (status != 0) {
1517 data->cancelled = 1;
1518 smp_wmb();
1519 } else
1520 status = data->rpc_status;
1521 rpc_put_task(task);
1522 return status;
1523}
1524
1525static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1526{
1527 struct nfs4_opendata *data = calldata;
1528 struct nfs4_state_owner *sp = data->owner;
1529 struct nfs_client *clp = sp->so_server->nfs_client;
1530
1531 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1532 goto out_wait;
1533 /*
1534 * Check if we still need to send an OPEN call, or if we can use
1535 * a delegation instead.
1536 */
1537 if (data->state != NULL) {
1538 struct nfs_delegation *delegation;
1539
1540 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1541 goto out_no_action;
1542 rcu_read_lock();
1543 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1544 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1545 can_open_delegated(delegation, data->o_arg.fmode))
1546 goto unlock_no_action;
1547 rcu_read_unlock();
1548 }
1549 /* Update client id. */
1550 data->o_arg.clientid = clp->cl_clientid;
1551 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1552 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1553 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1554 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1555 }
1556 data->timestamp = jiffies;
1557 if (nfs4_setup_sequence(data->o_arg.server,
1558 &data->o_arg.seq_args,
1559 &data->o_res.seq_res,
1560 task) != 0)
1561 nfs_release_seqid(data->o_arg.seqid);
1562
1563 /* Set the create mode (note dependency on the session type) */
1564 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1565 if (data->o_arg.open_flags & O_EXCL) {
1566 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1567 if (nfs4_has_persistent_session(clp))
1568 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1569 else if (clp->cl_mvops->minor_version > 0)
1570 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1571 }
1572 return;
1573unlock_no_action:
1574 rcu_read_unlock();
1575out_no_action:
1576 task->tk_action = NULL;
1577out_wait:
1578 nfs4_sequence_done(task, &data->o_res.seq_res);
1579}
1580
1581static void nfs4_open_done(struct rpc_task *task, void *calldata)
1582{
1583 struct nfs4_opendata *data = calldata;
1584
1585 data->rpc_status = task->tk_status;
1586
1587 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1588 return;
1589
1590 if (task->tk_status == 0) {
1591 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1592 switch (data->o_res.f_attr->mode & S_IFMT) {
1593 case S_IFREG:
1594 break;
1595 case S_IFLNK:
1596 data->rpc_status = -ELOOP;
1597 break;
1598 case S_IFDIR:
1599 data->rpc_status = -EISDIR;
1600 break;
1601 default:
1602 data->rpc_status = -ENOTDIR;
1603 }
1604 }
1605 renew_lease(data->o_res.server, data->timestamp);
1606 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1607 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1608 }
1609 data->rpc_done = 1;
1610}
1611
1612static void nfs4_open_release(void *calldata)
1613{
1614 struct nfs4_opendata *data = calldata;
1615 struct nfs4_state *state = NULL;
1616
1617 /* If this request hasn't been cancelled, do nothing */
1618 if (data->cancelled == 0)
1619 goto out_free;
1620 /* In case of error, no cleanup! */
1621 if (data->rpc_status != 0 || !data->rpc_done)
1622 goto out_free;
1623 /* In case we need an open_confirm, no cleanup! */
1624 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1625 goto out_free;
1626 state = nfs4_opendata_to_nfs4_state(data);
1627 if (!IS_ERR(state))
1628 nfs4_close_state(state, data->o_arg.fmode);
1629out_free:
1630 nfs4_opendata_put(data);
1631}
1632
1633static const struct rpc_call_ops nfs4_open_ops = {
1634 .rpc_call_prepare = nfs4_open_prepare,
1635 .rpc_call_done = nfs4_open_done,
1636 .rpc_release = nfs4_open_release,
1637};
1638
1639static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1640{
1641 struct inode *dir = data->dir->d_inode;
1642 struct nfs_server *server = NFS_SERVER(dir);
1643 struct nfs_openargs *o_arg = &data->o_arg;
1644 struct nfs_openres *o_res = &data->o_res;
1645 struct rpc_task *task;
1646 struct rpc_message msg = {
1647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1648 .rpc_argp = o_arg,
1649 .rpc_resp = o_res,
1650 .rpc_cred = data->owner->so_cred,
1651 };
1652 struct rpc_task_setup task_setup_data = {
1653 .rpc_client = server->client,
1654 .rpc_message = &msg,
1655 .callback_ops = &nfs4_open_ops,
1656 .callback_data = data,
1657 .workqueue = nfsiod_workqueue,
1658 .flags = RPC_TASK_ASYNC,
1659 };
1660 int status;
1661
1662 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1663 kref_get(&data->kref);
1664 data->rpc_done = 0;
1665 data->rpc_status = 0;
1666 data->cancelled = 0;
1667 if (isrecover)
1668 nfs4_set_sequence_privileged(&o_arg->seq_args);
1669 task = rpc_run_task(&task_setup_data);
1670 if (IS_ERR(task))
1671 return PTR_ERR(task);
1672 status = nfs4_wait_for_completion_rpc_task(task);
1673 if (status != 0) {
1674 data->cancelled = 1;
1675 smp_wmb();
1676 } else
1677 status = data->rpc_status;
1678 rpc_put_task(task);
1679
1680 return status;
1681}
1682
1683static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1684{
1685 struct inode *dir = data->dir->d_inode;
1686 struct nfs_openres *o_res = &data->o_res;
1687 int status;
1688
1689 status = nfs4_run_open_task(data, 1);
1690 if (status != 0 || !data->rpc_done)
1691 return status;
1692
1693 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1694
1695 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1696 status = _nfs4_proc_open_confirm(data);
1697 if (status != 0)
1698 return status;
1699 }
1700
1701 return status;
1702}
1703
1704static int nfs4_opendata_access(struct rpc_cred *cred,
1705 struct nfs4_opendata *opendata,
1706 struct nfs4_state *state, fmode_t fmode,
1707 int openflags)
1708{
1709 struct nfs_access_entry cache;
1710 u32 mask;
1711
1712 /* access call failed or for some reason the server doesn't
1713 * support any access modes -- defer access call until later */
1714 if (opendata->o_res.access_supported == 0)
1715 return 0;
1716
1717 mask = 0;
1718 /* don't check MAY_WRITE - a newly created file may not have
1719 * write mode bits, but POSIX allows the creating process to write.
1720 * use openflags to check for exec, because fmode won't
1721 * always have FMODE_EXEC set when file open for exec. */
1722 if (openflags & __FMODE_EXEC) {
1723 /* ONLY check for exec rights */
1724 mask = MAY_EXEC;
1725 } else if (fmode & FMODE_READ)
1726 mask = MAY_READ;
1727
1728 cache.cred = cred;
1729 cache.jiffies = jiffies;
1730 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1731 nfs_access_add_cache(state->inode, &cache);
1732
1733 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1734 return 0;
1735
1736 /* even though OPEN succeeded, access is denied. Close the file */
1737 nfs4_close_state(state, fmode);
1738 return -EACCES;
1739}
1740
1741/*
1742 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1743 */
1744static int _nfs4_proc_open(struct nfs4_opendata *data)
1745{
1746 struct inode *dir = data->dir->d_inode;
1747 struct nfs_server *server = NFS_SERVER(dir);
1748 struct nfs_openargs *o_arg = &data->o_arg;
1749 struct nfs_openres *o_res = &data->o_res;
1750 int status;
1751
1752 status = nfs4_run_open_task(data, 0);
1753 if (!data->rpc_done)
1754 return status;
1755 if (status != 0) {
1756 if (status == -NFS4ERR_BADNAME &&
1757 !(o_arg->open_flags & O_CREAT))
1758 return -ENOENT;
1759 return status;
1760 }
1761
1762 nfs_fattr_map_and_free_names(server, &data->f_attr);
1763
1764 if (o_arg->open_flags & O_CREAT)
1765 update_changeattr(dir, &o_res->cinfo);
1766 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1767 server->caps &= ~NFS_CAP_POSIX_LOCK;
1768 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1769 status = _nfs4_proc_open_confirm(data);
1770 if (status != 0)
1771 return status;
1772 }
1773 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1774 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1775 return 0;
1776}
1777
1778static int nfs4_recover_expired_lease(struct nfs_server *server)
1779{
1780 return nfs4_client_recover_expired_lease(server->nfs_client);
1781}
1782
1783/*
1784 * OPEN_EXPIRED:
1785 * reclaim state on the server after a network partition.
1786 * Assumes caller holds the appropriate lock
1787 */
1788static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1789{
1790 struct nfs4_opendata *opendata;
1791 int ret;
1792
1793 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1794 NFS4_OPEN_CLAIM_FH);
1795 if (IS_ERR(opendata))
1796 return PTR_ERR(opendata);
1797 ret = nfs4_open_recover(opendata, state);
1798 if (ret == -ESTALE)
1799 d_drop(ctx->dentry);
1800 nfs4_opendata_put(opendata);
1801 return ret;
1802}
1803
1804static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1805{
1806 struct nfs_server *server = NFS_SERVER(state->inode);
1807 struct nfs4_exception exception = { };
1808 int err;
1809
1810 do {
1811 err = _nfs4_open_expired(ctx, state);
1812 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1813 continue;
1814 switch (err) {
1815 default:
1816 goto out;
1817 case -NFS4ERR_GRACE:
1818 case -NFS4ERR_DELAY:
1819 nfs4_handle_exception(server, err, &exception);
1820 err = 0;
1821 }
1822 } while (exception.retry);
1823out:
1824 return err;
1825}
1826
1827static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1828{
1829 struct nfs_open_context *ctx;
1830 int ret;
1831
1832 ctx = nfs4_state_find_open_context(state);
1833 if (IS_ERR(ctx))
1834 return -EAGAIN;
1835 ret = nfs4_do_open_expired(ctx, state);
1836 put_nfs_open_context(ctx);
1837 return ret;
1838}
1839
1840#if defined(CONFIG_NFS_V4_1)
1841static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1842{
1843 struct nfs_server *server = NFS_SERVER(state->inode);
1844 nfs4_stateid *stateid = &state->stateid;
1845 int status;
1846
1847 /* If a state reset has been done, test_stateid is unneeded */
1848 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1849 return;
1850
1851 status = nfs41_test_stateid(server, stateid);
1852 if (status != NFS_OK) {
1853 /* Free the stateid unless the server explicitly
1854 * informs us the stateid is unrecognized. */
1855 if (status != -NFS4ERR_BAD_STATEID)
1856 nfs41_free_stateid(server, stateid);
1857 nfs_remove_bad_delegation(state->inode);
1858
1859 write_seqlock(&state->seqlock);
1860 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1861 write_sequnlock(&state->seqlock);
1862 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1863 }
1864}
1865
1866/**
1867 * nfs41_check_open_stateid - possibly free an open stateid
1868 *
1869 * @state: NFSv4 state for an inode
1870 *
1871 * Returns NFS_OK if recovery for this stateid is now finished.
1872 * Otherwise a negative NFS4ERR value is returned.
1873 */
1874static int nfs41_check_open_stateid(struct nfs4_state *state)
1875{
1876 struct nfs_server *server = NFS_SERVER(state->inode);
1877 nfs4_stateid *stateid = &state->open_stateid;
1878 int status;
1879
1880 /* If a state reset has been done, test_stateid is unneeded */
1881 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1882 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1883 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1884 return -NFS4ERR_BAD_STATEID;
1885
1886 status = nfs41_test_stateid(server, stateid);
1887 if (status != NFS_OK) {
1888 /* Free the stateid unless the server explicitly
1889 * informs us the stateid is unrecognized. */
1890 if (status != -NFS4ERR_BAD_STATEID)
1891 nfs41_free_stateid(server, stateid);
1892
1893 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1894 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1895 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1896 }
1897 return status;
1898}
1899
1900static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1901{
1902 int status;
1903
1904 nfs41_clear_delegation_stateid(state);
1905 status = nfs41_check_open_stateid(state);
1906 if (status != NFS_OK)
1907 status = nfs4_open_expired(sp, state);
1908 return status;
1909}
1910#endif
1911
1912/*
1913 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1914 * fields corresponding to attributes that were used to store the verifier.
1915 * Make sure we clobber those fields in the later setattr call
1916 */
1917static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1918{
1919 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1920 !(sattr->ia_valid & ATTR_ATIME_SET))
1921 sattr->ia_valid |= ATTR_ATIME;
1922
1923 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1924 !(sattr->ia_valid & ATTR_MTIME_SET))
1925 sattr->ia_valid |= ATTR_MTIME;
1926}
1927
1928static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1929 fmode_t fmode,
1930 int flags,
1931 struct nfs4_state **res)
1932{
1933 struct nfs4_state_owner *sp = opendata->owner;
1934 struct nfs_server *server = sp->so_server;
1935 struct nfs4_state *state;
1936 unsigned int seq;
1937 int ret;
1938
1939 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1940
1941 ret = _nfs4_proc_open(opendata);
1942 if (ret != 0)
1943 goto out;
1944
1945 state = nfs4_opendata_to_nfs4_state(opendata);
1946 ret = PTR_ERR(state);
1947 if (IS_ERR(state))
1948 goto out;
1949 if (server->caps & NFS_CAP_POSIX_LOCK)
1950 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1951
1952 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1953 if (ret != 0)
1954 goto out;
1955
1956 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
1957 nfs4_schedule_stateid_recovery(server, state);
1958 *res = state;
1959out:
1960 return ret;
1961}
1962
1963/*
1964 * Returns a referenced nfs4_state
1965 */
1966static int _nfs4_do_open(struct inode *dir,
1967 struct dentry *dentry,
1968 fmode_t fmode,
1969 int flags,
1970 struct iattr *sattr,
1971 struct rpc_cred *cred,
1972 struct nfs4_state **res,
1973 struct nfs4_threshold **ctx_th)
1974{
1975 struct nfs4_state_owner *sp;
1976 struct nfs4_state *state = NULL;
1977 struct nfs_server *server = NFS_SERVER(dir);
1978 struct nfs4_opendata *opendata;
1979 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
1980 int status;
1981
1982 /* Protect against reboot recovery conflicts */
1983 status = -ENOMEM;
1984 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1985 if (sp == NULL) {
1986 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1987 goto out_err;
1988 }
1989 status = nfs4_recover_expired_lease(server);
1990 if (status != 0)
1991 goto err_put_state_owner;
1992 if (dentry->d_inode != NULL)
1993 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1994 status = -ENOMEM;
1995 if (dentry->d_inode)
1996 claim = NFS4_OPEN_CLAIM_FH;
1997 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
1998 claim, GFP_KERNEL);
1999 if (opendata == NULL)
2000 goto err_put_state_owner;
2001
2002 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2003 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2004 if (!opendata->f_attr.mdsthreshold)
2005 goto err_opendata_put;
2006 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2007 }
2008 if (dentry->d_inode != NULL)
2009 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2010
2011 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
2012 if (status != 0)
2013 goto err_opendata_put;
2014
2015 if ((opendata->o_arg.open_flags & O_EXCL) &&
2016 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2017 nfs4_exclusive_attrset(opendata, sattr);
2018
2019 nfs_fattr_init(opendata->o_res.f_attr);
2020 status = nfs4_do_setattr(state->inode, cred,
2021 opendata->o_res.f_attr, sattr,
2022 state);
2023 if (status == 0)
2024 nfs_setattr_update_inode(state->inode, sattr);
2025 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2026 }
2027
2028 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2029 *ctx_th = opendata->f_attr.mdsthreshold;
2030 else
2031 kfree(opendata->f_attr.mdsthreshold);
2032 opendata->f_attr.mdsthreshold = NULL;
2033
2034 nfs4_opendata_put(opendata);
2035 nfs4_put_state_owner(sp);
2036 *res = state;
2037 return 0;
2038err_opendata_put:
2039 kfree(opendata->f_attr.mdsthreshold);
2040 nfs4_opendata_put(opendata);
2041err_put_state_owner:
2042 nfs4_put_state_owner(sp);
2043out_err:
2044 *res = NULL;
2045 return status;
2046}
2047
2048
2049static struct nfs4_state *nfs4_do_open(struct inode *dir,
2050 struct dentry *dentry,
2051 fmode_t fmode,
2052 int flags,
2053 struct iattr *sattr,
2054 struct rpc_cred *cred,
2055 struct nfs4_threshold **ctx_th)
2056{
2057 struct nfs_server *server = NFS_SERVER(dir);
2058 struct nfs4_exception exception = { };
2059 struct nfs4_state *res;
2060 int status;
2061
2062 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2063 do {
2064 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2065 &res, ctx_th);
2066 if (status == 0)
2067 break;
2068 /* NOTE: BAD_SEQID means the server and client disagree about the
2069 * book-keeping w.r.t. state-changing operations
2070 * (OPEN/CLOSE/LOCK/LOCKU...)
2071 * It is actually a sign of a bug on the client or on the server.
2072 *
2073 * If we receive a BAD_SEQID error in the particular case of
2074 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2075 * have unhashed the old state_owner for us, and that we can
2076 * therefore safely retry using a new one. We should still warn
2077 * the user though...
2078 */
2079 if (status == -NFS4ERR_BAD_SEQID) {
2080 pr_warn_ratelimited("NFS: v4 server %s "
2081 " returned a bad sequence-id error!\n",
2082 NFS_SERVER(dir)->nfs_client->cl_hostname);
2083 exception.retry = 1;
2084 continue;
2085 }
2086 /*
2087 * BAD_STATEID on OPEN means that the server cancelled our
2088 * state before it received the OPEN_CONFIRM.
2089 * Recover by retrying the request as per the discussion
2090 * on Page 181 of RFC3530.
2091 */
2092 if (status == -NFS4ERR_BAD_STATEID) {
2093 exception.retry = 1;
2094 continue;
2095 }
2096 if (status == -EAGAIN) {
2097 /* We must have found a delegation */
2098 exception.retry = 1;
2099 continue;
2100 }
2101 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2102 continue;
2103 res = ERR_PTR(nfs4_handle_exception(server,
2104 status, &exception));
2105 } while (exception.retry);
2106 return res;
2107}
2108
2109static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2110 struct nfs_fattr *fattr, struct iattr *sattr,
2111 struct nfs4_state *state)
2112{
2113 struct nfs_server *server = NFS_SERVER(inode);
2114 struct nfs_setattrargs arg = {
2115 .fh = NFS_FH(inode),
2116 .iap = sattr,
2117 .server = server,
2118 .bitmask = server->attr_bitmask,
2119 };
2120 struct nfs_setattrres res = {
2121 .fattr = fattr,
2122 .server = server,
2123 };
2124 struct rpc_message msg = {
2125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2126 .rpc_argp = &arg,
2127 .rpc_resp = &res,
2128 .rpc_cred = cred,
2129 };
2130 unsigned long timestamp = jiffies;
2131 int status;
2132
2133 nfs_fattr_init(fattr);
2134
2135 if (state != NULL && nfs4_valid_open_stateid(state)) {
2136 struct nfs_lockowner lockowner = {
2137 .l_owner = current->files,
2138 .l_pid = current->tgid,
2139 };
2140 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2141 &lockowner);
2142 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2143 FMODE_WRITE)) {
2144 /* Use that stateid */
2145 } else
2146 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2147
2148 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2149 if (status == 0 && state != NULL)
2150 renew_lease(server, timestamp);
2151 return status;
2152}
2153
2154static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2155 struct nfs_fattr *fattr, struct iattr *sattr,
2156 struct nfs4_state *state)
2157{
2158 struct nfs_server *server = NFS_SERVER(inode);
2159 struct nfs4_exception exception = {
2160 .state = state,
2161 .inode = inode,
2162 };
2163 int err;
2164 do {
2165 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2166 switch (err) {
2167 case -NFS4ERR_OPENMODE:
2168 if (state && !(state->state & FMODE_WRITE)) {
2169 err = -EBADF;
2170 if (sattr->ia_valid & ATTR_OPEN)
2171 err = -EACCES;
2172 goto out;
2173 }
2174 }
2175 err = nfs4_handle_exception(server, err, &exception);
2176 } while (exception.retry);
2177out:
2178 return err;
2179}
2180
2181struct nfs4_closedata {
2182 struct inode *inode;
2183 struct nfs4_state *state;
2184 struct nfs_closeargs arg;
2185 struct nfs_closeres res;
2186 struct nfs_fattr fattr;
2187 unsigned long timestamp;
2188 bool roc;
2189 u32 roc_barrier;
2190};
2191
2192static void nfs4_free_closedata(void *data)
2193{
2194 struct nfs4_closedata *calldata = data;
2195 struct nfs4_state_owner *sp = calldata->state->owner;
2196 struct super_block *sb = calldata->state->inode->i_sb;
2197
2198 if (calldata->roc)
2199 pnfs_roc_release(calldata->state->inode);
2200 nfs4_put_open_state(calldata->state);
2201 nfs_free_seqid(calldata->arg.seqid);
2202 nfs4_put_state_owner(sp);
2203 nfs_sb_deactive(sb);
2204 kfree(calldata);
2205}
2206
2207static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2208 fmode_t fmode)
2209{
2210 spin_lock(&state->owner->so_lock);
2211 if (!(fmode & FMODE_READ))
2212 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2213 if (!(fmode & FMODE_WRITE))
2214 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2215 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2216 spin_unlock(&state->owner->so_lock);
2217}
2218
2219static void nfs4_close_done(struct rpc_task *task, void *data)
2220{
2221 struct nfs4_closedata *calldata = data;
2222 struct nfs4_state *state = calldata->state;
2223 struct nfs_server *server = NFS_SERVER(calldata->inode);
2224
2225 dprintk("%s: begin!\n", __func__);
2226 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2227 return;
2228 /* hmm. we are done with the inode, and in the process of freeing
2229 * the state_owner. we keep this around to process errors
2230 */
2231 switch (task->tk_status) {
2232 case 0:
2233 if (calldata->roc)
2234 pnfs_roc_set_barrier(state->inode,
2235 calldata->roc_barrier);
2236 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2237 renew_lease(server, calldata->timestamp);
2238 nfs4_close_clear_stateid_flags(state,
2239 calldata->arg.fmode);
2240 break;
2241 case -NFS4ERR_STALE_STATEID:
2242 case -NFS4ERR_OLD_STATEID:
2243 case -NFS4ERR_BAD_STATEID:
2244 case -NFS4ERR_EXPIRED:
2245 if (calldata->arg.fmode == 0)
2246 break;
2247 default:
2248 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2249 rpc_restart_call_prepare(task);
2250 }
2251 nfs_release_seqid(calldata->arg.seqid);
2252 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2253 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2254}
2255
2256static void nfs4_close_prepare(struct rpc_task *task, void *data)
2257{
2258 struct nfs4_closedata *calldata = data;
2259 struct nfs4_state *state = calldata->state;
2260 struct inode *inode = calldata->inode;
2261 int call_close = 0;
2262
2263 dprintk("%s: begin!\n", __func__);
2264 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2265 goto out_wait;
2266
2267 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2268 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2269 spin_lock(&state->owner->so_lock);
2270 /* Calculate the change in open mode */
2271 if (state->n_rdwr == 0) {
2272 if (state->n_rdonly == 0) {
2273 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2274 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2275 calldata->arg.fmode &= ~FMODE_READ;
2276 }
2277 if (state->n_wronly == 0) {
2278 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2279 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2280 calldata->arg.fmode &= ~FMODE_WRITE;
2281 }
2282 }
2283 if (!nfs4_valid_open_stateid(state))
2284 call_close = 0;
2285 spin_unlock(&state->owner->so_lock);
2286
2287 if (!call_close) {
2288 /* Note: exit _without_ calling nfs4_close_done */
2289 goto out_no_action;
2290 }
2291
2292 if (calldata->arg.fmode == 0) {
2293 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2294 if (calldata->roc &&
2295 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2296 nfs_release_seqid(calldata->arg.seqid);
2297 goto out_wait;
2298 }
2299 }
2300
2301 nfs_fattr_init(calldata->res.fattr);
2302 calldata->timestamp = jiffies;
2303 if (nfs4_setup_sequence(NFS_SERVER(inode),
2304 &calldata->arg.seq_args,
2305 &calldata->res.seq_res,
2306 task) != 0)
2307 nfs_release_seqid(calldata->arg.seqid);
2308 dprintk("%s: done!\n", __func__);
2309 return;
2310out_no_action:
2311 task->tk_action = NULL;
2312out_wait:
2313 nfs4_sequence_done(task, &calldata->res.seq_res);
2314}
2315
2316static const struct rpc_call_ops nfs4_close_ops = {
2317 .rpc_call_prepare = nfs4_close_prepare,
2318 .rpc_call_done = nfs4_close_done,
2319 .rpc_release = nfs4_free_closedata,
2320};
2321
2322/*
2323 * It is possible for data to be read/written from a mem-mapped file
2324 * after the sys_close call (which hits the vfs layer as a flush).
2325 * This means that we can't safely call nfsv4 close on a file until
2326 * the inode is cleared. This in turn means that we are not good
2327 * NFSv4 citizens - we do not indicate to the server to update the file's
2328 * share state even when we are done with one of the three share
2329 * stateid's in the inode.
2330 *
2331 * NOTE: Caller must be holding the sp->so_owner semaphore!
2332 */
2333int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2334{
2335 struct nfs_server *server = NFS_SERVER(state->inode);
2336 struct nfs4_closedata *calldata;
2337 struct nfs4_state_owner *sp = state->owner;
2338 struct rpc_task *task;
2339 struct rpc_message msg = {
2340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2341 .rpc_cred = state->owner->so_cred,
2342 };
2343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
2345 .rpc_message = &msg,
2346 .callback_ops = &nfs4_close_ops,
2347 .workqueue = nfsiod_workqueue,
2348 .flags = RPC_TASK_ASYNC,
2349 };
2350 int status = -ENOMEM;
2351
2352 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2353 if (calldata == NULL)
2354 goto out;
2355 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2356 calldata->inode = state->inode;
2357 calldata->state = state;
2358 calldata->arg.fh = NFS_FH(state->inode);
2359 calldata->arg.stateid = &state->open_stateid;
2360 /* Serialization for the sequence id */
2361 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2362 if (calldata->arg.seqid == NULL)
2363 goto out_free_calldata;
2364 calldata->arg.fmode = 0;
2365 calldata->arg.bitmask = server->cache_consistency_bitmask;
2366 calldata->res.fattr = &calldata->fattr;
2367 calldata->res.seqid = calldata->arg.seqid;
2368 calldata->res.server = server;
2369 calldata->roc = pnfs_roc(state->inode);
2370 nfs_sb_active(calldata->inode->i_sb);
2371
2372 msg.rpc_argp = &calldata->arg;
2373 msg.rpc_resp = &calldata->res;
2374 task_setup_data.callback_data = calldata;
2375 task = rpc_run_task(&task_setup_data);
2376 if (IS_ERR(task))
2377 return PTR_ERR(task);
2378 status = 0;
2379 if (wait)
2380 status = rpc_wait_for_completion_task(task);
2381 rpc_put_task(task);
2382 return status;
2383out_free_calldata:
2384 kfree(calldata);
2385out:
2386 nfs4_put_open_state(state);
2387 nfs4_put_state_owner(sp);
2388 return status;
2389}
2390
2391static struct inode *
2392nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2393{
2394 struct nfs4_state *state;
2395
2396 /* Protect against concurrent sillydeletes */
2397 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2398 ctx->cred, &ctx->mdsthreshold);
2399 if (IS_ERR(state))
2400 return ERR_CAST(state);
2401 ctx->state = state;
2402 return igrab(state->inode);
2403}
2404
2405static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2406{
2407 if (ctx->state == NULL)
2408 return;
2409 if (is_sync)
2410 nfs4_close_sync(ctx->state, ctx->mode);
2411 else
2412 nfs4_close_state(ctx->state, ctx->mode);
2413}
2414
2415static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2416{
2417 struct nfs4_server_caps_arg args = {
2418 .fhandle = fhandle,
2419 };
2420 struct nfs4_server_caps_res res = {};
2421 struct rpc_message msg = {
2422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2423 .rpc_argp = &args,
2424 .rpc_resp = &res,
2425 };
2426 int status;
2427
2428 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2429 if (status == 0) {
2430 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2431 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2432 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2433 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2434 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2435 NFS_CAP_CTIME|NFS_CAP_MTIME);
2436 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2437 server->caps |= NFS_CAP_ACLS;
2438 if (res.has_links != 0)
2439 server->caps |= NFS_CAP_HARDLINKS;
2440 if (res.has_symlinks != 0)
2441 server->caps |= NFS_CAP_SYMLINKS;
2442 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2443 server->caps |= NFS_CAP_FILEID;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2445 server->caps |= NFS_CAP_MODE;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2447 server->caps |= NFS_CAP_NLINK;
2448 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2449 server->caps |= NFS_CAP_OWNER;
2450 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2451 server->caps |= NFS_CAP_OWNER_GROUP;
2452 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2453 server->caps |= NFS_CAP_ATIME;
2454 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2455 server->caps |= NFS_CAP_CTIME;
2456 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2457 server->caps |= NFS_CAP_MTIME;
2458
2459 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2460 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2461 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2462 server->acl_bitmask = res.acl_bitmask;
2463 server->fh_expire_type = res.fh_expire_type;
2464 }
2465
2466 return status;
2467}
2468
2469int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2470{
2471 struct nfs4_exception exception = { };
2472 int err;
2473 do {
2474 err = nfs4_handle_exception(server,
2475 _nfs4_server_capabilities(server, fhandle),
2476 &exception);
2477 } while (exception.retry);
2478 return err;
2479}
2480
2481static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2482 struct nfs_fsinfo *info)
2483{
2484 struct nfs4_lookup_root_arg args = {
2485 .bitmask = nfs4_fattr_bitmap,
2486 };
2487 struct nfs4_lookup_res res = {
2488 .server = server,
2489 .fattr = info->fattr,
2490 .fh = fhandle,
2491 };
2492 struct rpc_message msg = {
2493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2494 .rpc_argp = &args,
2495 .rpc_resp = &res,
2496 };
2497
2498 nfs_fattr_init(info->fattr);
2499 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2500}
2501
2502static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2503 struct nfs_fsinfo *info)
2504{
2505 struct nfs4_exception exception = { };
2506 int err;
2507 do {
2508 err = _nfs4_lookup_root(server, fhandle, info);
2509 switch (err) {
2510 case 0:
2511 case -NFS4ERR_WRONGSEC:
2512 goto out;
2513 default:
2514 err = nfs4_handle_exception(server, err, &exception);
2515 }
2516 } while (exception.retry);
2517out:
2518 return err;
2519}
2520
2521static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2522 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2523{
2524 struct rpc_auth *auth;
2525 int ret;
2526
2527 auth = rpcauth_create(flavor, server->client);
2528 if (IS_ERR(auth)) {
2529 ret = -EIO;
2530 goto out;
2531 }
2532 ret = nfs4_lookup_root(server, fhandle, info);
2533out:
2534 return ret;
2535}
2536
2537static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2538 struct nfs_fsinfo *info)
2539{
2540 int i, len, status = 0;
2541 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2542
2543 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2544 if (len < 0)
2545 return len;
2546
2547 for (i = 0; i < len; i++) {
2548 /* AUTH_UNIX is the default flavor if none was specified,
2549 * thus has already been tried. */
2550 if (flav_array[i] == RPC_AUTH_UNIX)
2551 continue;
2552
2553 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2554 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2555 continue;
2556 break;
2557 }
2558 /*
2559 * -EACCESS could mean that the user doesn't have correct permissions
2560 * to access the mount. It could also mean that we tried to mount
2561 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2562 * existing mount programs don't handle -EACCES very well so it should
2563 * be mapped to -EPERM instead.
2564 */
2565 if (status == -EACCES)
2566 status = -EPERM;
2567 return status;
2568}
2569
2570/*
2571 * get the file handle for the "/" directory on the server
2572 */
2573int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2574 struct nfs_fsinfo *info)
2575{
2576 int minor_version = server->nfs_client->cl_minorversion;
2577 int status = nfs4_lookup_root(server, fhandle, info);
2578 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2579 /*
2580 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2581 * by nfs4_map_errors() as this function exits.
2582 */
2583 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2584 if (status == 0)
2585 status = nfs4_server_capabilities(server, fhandle);
2586 if (status == 0)
2587 status = nfs4_do_fsinfo(server, fhandle, info);
2588 return nfs4_map_errors(status);
2589}
2590
2591static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2592 struct nfs_fsinfo *info)
2593{
2594 int error;
2595 struct nfs_fattr *fattr = info->fattr;
2596
2597 error = nfs4_server_capabilities(server, mntfh);
2598 if (error < 0) {
2599 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2600 return error;
2601 }
2602
2603 error = nfs4_proc_getattr(server, mntfh, fattr);
2604 if (error < 0) {
2605 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2606 return error;
2607 }
2608
2609 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2610 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2611 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2612
2613 return error;
2614}
2615
2616/*
2617 * Get locations and (maybe) other attributes of a referral.
2618 * Note that we'll actually follow the referral later when
2619 * we detect fsid mismatch in inode revalidation
2620 */
2621static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2622 const struct qstr *name, struct nfs_fattr *fattr,
2623 struct nfs_fh *fhandle)
2624{
2625 int status = -ENOMEM;
2626 struct page *page = NULL;
2627 struct nfs4_fs_locations *locations = NULL;
2628
2629 page = alloc_page(GFP_KERNEL);
2630 if (page == NULL)
2631 goto out;
2632 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2633 if (locations == NULL)
2634 goto out;
2635
2636 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2637 if (status != 0)
2638 goto out;
2639 /* Make sure server returned a different fsid for the referral */
2640 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2641 dprintk("%s: server did not return a different fsid for"
2642 " a referral at %s\n", __func__, name->name);
2643 status = -EIO;
2644 goto out;
2645 }
2646 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2647 nfs_fixup_referral_attributes(&locations->fattr);
2648
2649 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2650 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2651 memset(fhandle, 0, sizeof(struct nfs_fh));
2652out:
2653 if (page)
2654 __free_page(page);
2655 kfree(locations);
2656 return status;
2657}
2658
2659static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2660{
2661 struct nfs4_getattr_arg args = {
2662 .fh = fhandle,
2663 .bitmask = server->attr_bitmask,
2664 };
2665 struct nfs4_getattr_res res = {
2666 .fattr = fattr,
2667 .server = server,
2668 };
2669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2671 .rpc_argp = &args,
2672 .rpc_resp = &res,
2673 };
2674
2675 nfs_fattr_init(fattr);
2676 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2677}
2678
2679static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2680{
2681 struct nfs4_exception exception = { };
2682 int err;
2683 do {
2684 err = nfs4_handle_exception(server,
2685 _nfs4_proc_getattr(server, fhandle, fattr),
2686 &exception);
2687 } while (exception.retry);
2688 return err;
2689}
2690
2691/*
2692 * The file is not closed if it is opened due to the a request to change
2693 * the size of the file. The open call will not be needed once the
2694 * VFS layer lookup-intents are implemented.
2695 *
2696 * Close is called when the inode is destroyed.
2697 * If we haven't opened the file for O_WRONLY, we
2698 * need to in the size_change case to obtain a stateid.
2699 *
2700 * Got race?
2701 * Because OPEN is always done by name in nfsv4, it is
2702 * possible that we opened a different file by the same
2703 * name. We can recognize this race condition, but we
2704 * can't do anything about it besides returning an error.
2705 *
2706 * This will be fixed with VFS changes (lookup-intent).
2707 */
2708static int
2709nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2710 struct iattr *sattr)
2711{
2712 struct inode *inode = dentry->d_inode;
2713 struct rpc_cred *cred = NULL;
2714 struct nfs4_state *state = NULL;
2715 int status;
2716
2717 if (pnfs_ld_layoutret_on_setattr(inode))
2718 pnfs_commit_and_return_layout(inode);
2719
2720 nfs_fattr_init(fattr);
2721
2722 /* Deal with open(O_TRUNC) */
2723 if (sattr->ia_valid & ATTR_OPEN)
2724 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2725
2726 /* Optimization: if the end result is no change, don't RPC */
2727 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2728 return 0;
2729
2730 /* Search for an existing open(O_WRITE) file */
2731 if (sattr->ia_valid & ATTR_FILE) {
2732 struct nfs_open_context *ctx;
2733
2734 ctx = nfs_file_open_context(sattr->ia_file);
2735 if (ctx) {
2736 cred = ctx->cred;
2737 state = ctx->state;
2738 }
2739 }
2740
2741 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2742 if (status == 0)
2743 nfs_setattr_update_inode(inode, sattr);
2744 return status;
2745}
2746
2747static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2748 const struct qstr *name, struct nfs_fh *fhandle,
2749 struct nfs_fattr *fattr)
2750{
2751 struct nfs_server *server = NFS_SERVER(dir);
2752 int status;
2753 struct nfs4_lookup_arg args = {
2754 .bitmask = server->attr_bitmask,
2755 .dir_fh = NFS_FH(dir),
2756 .name = name,
2757 };
2758 struct nfs4_lookup_res res = {
2759 .server = server,
2760 .fattr = fattr,
2761 .fh = fhandle,
2762 };
2763 struct rpc_message msg = {
2764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2765 .rpc_argp = &args,
2766 .rpc_resp = &res,
2767 };
2768
2769 nfs_fattr_init(fattr);
2770
2771 dprintk("NFS call lookup %s\n", name->name);
2772 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2773 dprintk("NFS reply lookup: %d\n", status);
2774 return status;
2775}
2776
2777static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2778{
2779 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2780 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2781 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2782 fattr->nlink = 2;
2783}
2784
2785static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2786 struct qstr *name, struct nfs_fh *fhandle,
2787 struct nfs_fattr *fattr)
2788{
2789 struct nfs4_exception exception = { };
2790 struct rpc_clnt *client = *clnt;
2791 int err;
2792 do {
2793 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2794 switch (err) {
2795 case -NFS4ERR_BADNAME:
2796 err = -ENOENT;
2797 goto out;
2798 case -NFS4ERR_MOVED:
2799 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2800 goto out;
2801 case -NFS4ERR_WRONGSEC:
2802 err = -EPERM;
2803 if (client != *clnt)
2804 goto out;
2805
2806 client = nfs4_create_sec_client(client, dir, name);
2807 if (IS_ERR(client))
2808 return PTR_ERR(client);
2809
2810 exception.retry = 1;
2811 break;
2812 default:
2813 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2814 }
2815 } while (exception.retry);
2816
2817out:
2818 if (err == 0)
2819 *clnt = client;
2820 else if (client != *clnt)
2821 rpc_shutdown_client(client);
2822
2823 return err;
2824}
2825
2826static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2827 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2828{
2829 int status;
2830 struct rpc_clnt *client = NFS_CLIENT(dir);
2831
2832 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2833 if (client != NFS_CLIENT(dir)) {
2834 rpc_shutdown_client(client);
2835 nfs_fixup_secinfo_attributes(fattr);
2836 }
2837 return status;
2838}
2839
2840struct rpc_clnt *
2841nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2842 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2843{
2844 int status;
2845 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2846
2847 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2848 if (status < 0) {
2849 rpc_shutdown_client(client);
2850 return ERR_PTR(status);
2851 }
2852 return client;
2853}
2854
2855static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2856{
2857 struct nfs_server *server = NFS_SERVER(inode);
2858 struct nfs4_accessargs args = {
2859 .fh = NFS_FH(inode),
2860 .bitmask = server->cache_consistency_bitmask,
2861 };
2862 struct nfs4_accessres res = {
2863 .server = server,
2864 };
2865 struct rpc_message msg = {
2866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2867 .rpc_argp = &args,
2868 .rpc_resp = &res,
2869 .rpc_cred = entry->cred,
2870 };
2871 int mode = entry->mask;
2872 int status;
2873
2874 /*
2875 * Determine which access bits we want to ask for...
2876 */
2877 if (mode & MAY_READ)
2878 args.access |= NFS4_ACCESS_READ;
2879 if (S_ISDIR(inode->i_mode)) {
2880 if (mode & MAY_WRITE)
2881 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2882 if (mode & MAY_EXEC)
2883 args.access |= NFS4_ACCESS_LOOKUP;
2884 } else {
2885 if (mode & MAY_WRITE)
2886 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2887 if (mode & MAY_EXEC)
2888 args.access |= NFS4_ACCESS_EXECUTE;
2889 }
2890
2891 res.fattr = nfs_alloc_fattr();
2892 if (res.fattr == NULL)
2893 return -ENOMEM;
2894
2895 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2896 if (!status) {
2897 nfs_access_set_mask(entry, res.access);
2898 nfs_refresh_inode(inode, res.fattr);
2899 }
2900 nfs_free_fattr(res.fattr);
2901 return status;
2902}
2903
2904static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(inode),
2910 _nfs4_proc_access(inode, entry),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
2916/*
2917 * TODO: For the time being, we don't try to get any attributes
2918 * along with any of the zero-copy operations READ, READDIR,
2919 * READLINK, WRITE.
2920 *
2921 * In the case of the first three, we want to put the GETATTR
2922 * after the read-type operation -- this is because it is hard
2923 * to predict the length of a GETATTR response in v4, and thus
2924 * align the READ data correctly. This means that the GETATTR
2925 * may end up partially falling into the page cache, and we should
2926 * shift it into the 'tail' of the xdr_buf before processing.
2927 * To do this efficiently, we need to know the total length
2928 * of data received, which doesn't seem to be available outside
2929 * of the RPC layer.
2930 *
2931 * In the case of WRITE, we also want to put the GETATTR after
2932 * the operation -- in this case because we want to make sure
2933 * we get the post-operation mtime and size.
2934 *
2935 * Both of these changes to the XDR layer would in fact be quite
2936 * minor, but I decided to leave them for a subsequent patch.
2937 */
2938static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2939 unsigned int pgbase, unsigned int pglen)
2940{
2941 struct nfs4_readlink args = {
2942 .fh = NFS_FH(inode),
2943 .pgbase = pgbase,
2944 .pglen = pglen,
2945 .pages = &page,
2946 };
2947 struct nfs4_readlink_res res;
2948 struct rpc_message msg = {
2949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2950 .rpc_argp = &args,
2951 .rpc_resp = &res,
2952 };
2953
2954 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2955}
2956
2957static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2958 unsigned int pgbase, unsigned int pglen)
2959{
2960 struct nfs4_exception exception = { };
2961 int err;
2962 do {
2963 err = nfs4_handle_exception(NFS_SERVER(inode),
2964 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2965 &exception);
2966 } while (exception.retry);
2967 return err;
2968}
2969
2970/*
2971 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2972 */
2973static int
2974nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2975 int flags)
2976{
2977 struct nfs_open_context *ctx;
2978 struct nfs4_state *state;
2979 int status = 0;
2980
2981 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2982 if (IS_ERR(ctx))
2983 return PTR_ERR(ctx);
2984
2985 sattr->ia_mode &= ~current_umask();
2986 state = nfs4_do_open(dir, dentry, ctx->mode,
2987 flags, sattr, ctx->cred,
2988 &ctx->mdsthreshold);
2989 d_drop(dentry);
2990 if (IS_ERR(state)) {
2991 status = PTR_ERR(state);
2992 goto out;
2993 }
2994 d_add(dentry, igrab(state->inode));
2995 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2996 ctx->state = state;
2997out:
2998 put_nfs_open_context(ctx);
2999 return status;
3000}
3001
3002static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3003{
3004 struct nfs_server *server = NFS_SERVER(dir);
3005 struct nfs_removeargs args = {
3006 .fh = NFS_FH(dir),
3007 .name = *name,
3008 };
3009 struct nfs_removeres res = {
3010 .server = server,
3011 };
3012 struct rpc_message msg = {
3013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3014 .rpc_argp = &args,
3015 .rpc_resp = &res,
3016 };
3017 int status;
3018
3019 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3020 if (status == 0)
3021 update_changeattr(dir, &res.cinfo);
3022 return status;
3023}
3024
3025static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3026{
3027 struct nfs4_exception exception = { };
3028 int err;
3029 do {
3030 err = nfs4_handle_exception(NFS_SERVER(dir),
3031 _nfs4_proc_remove(dir, name),
3032 &exception);
3033 } while (exception.retry);
3034 return err;
3035}
3036
3037static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3038{
3039 struct nfs_server *server = NFS_SERVER(dir);
3040 struct nfs_removeargs *args = msg->rpc_argp;
3041 struct nfs_removeres *res = msg->rpc_resp;
3042
3043 res->server = server;
3044 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3045 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3046}
3047
3048static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3049{
3050 nfs4_setup_sequence(NFS_SERVER(data->dir),
3051 &data->args.seq_args,
3052 &data->res.seq_res,
3053 task);
3054}
3055
3056static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3057{
3058 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3059
3060 if (!nfs4_sequence_done(task, &res->seq_res))
3061 return 0;
3062 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3063 return 0;
3064 update_changeattr(dir, &res->cinfo);
3065 return 1;
3066}
3067
3068static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3069{
3070 struct nfs_server *server = NFS_SERVER(dir);
3071 struct nfs_renameargs *arg = msg->rpc_argp;
3072 struct nfs_renameres *res = msg->rpc_resp;
3073
3074 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3075 res->server = server;
3076 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3077}
3078
3079static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3080{
3081 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3082 &data->args.seq_args,
3083 &data->res.seq_res,
3084 task);
3085}
3086
3087static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3088 struct inode *new_dir)
3089{
3090 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3091
3092 if (!nfs4_sequence_done(task, &res->seq_res))
3093 return 0;
3094 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3095 return 0;
3096
3097 update_changeattr(old_dir, &res->old_cinfo);
3098 update_changeattr(new_dir, &res->new_cinfo);
3099 return 1;
3100}
3101
3102static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3103 struct inode *new_dir, struct qstr *new_name)
3104{
3105 struct nfs_server *server = NFS_SERVER(old_dir);
3106 struct nfs_renameargs arg = {
3107 .old_dir = NFS_FH(old_dir),
3108 .new_dir = NFS_FH(new_dir),
3109 .old_name = old_name,
3110 .new_name = new_name,
3111 };
3112 struct nfs_renameres res = {
3113 .server = server,
3114 };
3115 struct rpc_message msg = {
3116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3117 .rpc_argp = &arg,
3118 .rpc_resp = &res,
3119 };
3120 int status = -ENOMEM;
3121
3122 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3123 if (!status) {
3124 update_changeattr(old_dir, &res.old_cinfo);
3125 update_changeattr(new_dir, &res.new_cinfo);
3126 }
3127 return status;
3128}
3129
3130static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3131 struct inode *new_dir, struct qstr *new_name)
3132{
3133 struct nfs4_exception exception = { };
3134 int err;
3135 do {
3136 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3137 _nfs4_proc_rename(old_dir, old_name,
3138 new_dir, new_name),
3139 &exception);
3140 } while (exception.retry);
3141 return err;
3142}
3143
3144static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3145{
3146 struct nfs_server *server = NFS_SERVER(inode);
3147 struct nfs4_link_arg arg = {
3148 .fh = NFS_FH(inode),
3149 .dir_fh = NFS_FH(dir),
3150 .name = name,
3151 .bitmask = server->attr_bitmask,
3152 };
3153 struct nfs4_link_res res = {
3154 .server = server,
3155 };
3156 struct rpc_message msg = {
3157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3158 .rpc_argp = &arg,
3159 .rpc_resp = &res,
3160 };
3161 int status = -ENOMEM;
3162
3163 res.fattr = nfs_alloc_fattr();
3164 if (res.fattr == NULL)
3165 goto out;
3166
3167 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3168 if (!status) {
3169 update_changeattr(dir, &res.cinfo);
3170 nfs_post_op_update_inode(inode, res.fattr);
3171 }
3172out:
3173 nfs_free_fattr(res.fattr);
3174 return status;
3175}
3176
3177static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3178{
3179 struct nfs4_exception exception = { };
3180 int err;
3181 do {
3182 err = nfs4_handle_exception(NFS_SERVER(inode),
3183 _nfs4_proc_link(inode, dir, name),
3184 &exception);
3185 } while (exception.retry);
3186 return err;
3187}
3188
3189struct nfs4_createdata {
3190 struct rpc_message msg;
3191 struct nfs4_create_arg arg;
3192 struct nfs4_create_res res;
3193 struct nfs_fh fh;
3194 struct nfs_fattr fattr;
3195};
3196
3197static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3198 struct qstr *name, struct iattr *sattr, u32 ftype)
3199{
3200 struct nfs4_createdata *data;
3201
3202 data = kzalloc(sizeof(*data), GFP_KERNEL);
3203 if (data != NULL) {
3204 struct nfs_server *server = NFS_SERVER(dir);
3205
3206 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3207 data->msg.rpc_argp = &data->arg;
3208 data->msg.rpc_resp = &data->res;
3209 data->arg.dir_fh = NFS_FH(dir);
3210 data->arg.server = server;
3211 data->arg.name = name;
3212 data->arg.attrs = sattr;
3213 data->arg.ftype = ftype;
3214 data->arg.bitmask = server->attr_bitmask;
3215 data->res.server = server;
3216 data->res.fh = &data->fh;
3217 data->res.fattr = &data->fattr;
3218 nfs_fattr_init(data->res.fattr);
3219 }
3220 return data;
3221}
3222
3223static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3224{
3225 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3226 &data->arg.seq_args, &data->res.seq_res, 1);
3227 if (status == 0) {
3228 update_changeattr(dir, &data->res.dir_cinfo);
3229 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3230 }
3231 return status;
3232}
3233
3234static void nfs4_free_createdata(struct nfs4_createdata *data)
3235{
3236 kfree(data);
3237}
3238
3239static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3240 struct page *page, unsigned int len, struct iattr *sattr)
3241{
3242 struct nfs4_createdata *data;
3243 int status = -ENAMETOOLONG;
3244
3245 if (len > NFS4_MAXPATHLEN)
3246 goto out;
3247
3248 status = -ENOMEM;
3249 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3250 if (data == NULL)
3251 goto out;
3252
3253 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3254 data->arg.u.symlink.pages = &page;
3255 data->arg.u.symlink.len = len;
3256
3257 status = nfs4_do_create(dir, dentry, data);
3258
3259 nfs4_free_createdata(data);
3260out:
3261 return status;
3262}
3263
3264static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3265 struct page *page, unsigned int len, struct iattr *sattr)
3266{
3267 struct nfs4_exception exception = { };
3268 int err;
3269 do {
3270 err = nfs4_handle_exception(NFS_SERVER(dir),
3271 _nfs4_proc_symlink(dir, dentry, page,
3272 len, sattr),
3273 &exception);
3274 } while (exception.retry);
3275 return err;
3276}
3277
3278static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3279 struct iattr *sattr)
3280{
3281 struct nfs4_createdata *data;
3282 int status = -ENOMEM;
3283
3284 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3285 if (data == NULL)
3286 goto out;
3287
3288 status = nfs4_do_create(dir, dentry, data);
3289
3290 nfs4_free_createdata(data);
3291out:
3292 return status;
3293}
3294
3295static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3296 struct iattr *sattr)
3297{
3298 struct nfs4_exception exception = { };
3299 int err;
3300
3301 sattr->ia_mode &= ~current_umask();
3302 do {
3303 err = nfs4_handle_exception(NFS_SERVER(dir),
3304 _nfs4_proc_mkdir(dir, dentry, sattr),
3305 &exception);
3306 } while (exception.retry);
3307 return err;
3308}
3309
3310static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3311 u64 cookie, struct page **pages, unsigned int count, int plus)
3312{
3313 struct inode *dir = dentry->d_inode;
3314 struct nfs4_readdir_arg args = {
3315 .fh = NFS_FH(dir),
3316 .pages = pages,
3317 .pgbase = 0,
3318 .count = count,
3319 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3320 .plus = plus,
3321 };
3322 struct nfs4_readdir_res res;
3323 struct rpc_message msg = {
3324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3325 .rpc_argp = &args,
3326 .rpc_resp = &res,
3327 .rpc_cred = cred,
3328 };
3329 int status;
3330
3331 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3332 dentry->d_parent->d_name.name,
3333 dentry->d_name.name,
3334 (unsigned long long)cookie);
3335 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3336 res.pgbase = args.pgbase;
3337 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3338 if (status >= 0) {
3339 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3340 status += args.pgbase;
3341 }
3342
3343 nfs_invalidate_atime(dir);
3344
3345 dprintk("%s: returns %d\n", __func__, status);
3346 return status;
3347}
3348
3349static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3350 u64 cookie, struct page **pages, unsigned int count, int plus)
3351{
3352 struct nfs4_exception exception = { };
3353 int err;
3354 do {
3355 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3356 _nfs4_proc_readdir(dentry, cred, cookie,
3357 pages, count, plus),
3358 &exception);
3359 } while (exception.retry);
3360 return err;
3361}
3362
3363static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3364 struct iattr *sattr, dev_t rdev)
3365{
3366 struct nfs4_createdata *data;
3367 int mode = sattr->ia_mode;
3368 int status = -ENOMEM;
3369
3370 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3371 if (data == NULL)
3372 goto out;
3373
3374 if (S_ISFIFO(mode))
3375 data->arg.ftype = NF4FIFO;
3376 else if (S_ISBLK(mode)) {
3377 data->arg.ftype = NF4BLK;
3378 data->arg.u.device.specdata1 = MAJOR(rdev);
3379 data->arg.u.device.specdata2 = MINOR(rdev);
3380 }
3381 else if (S_ISCHR(mode)) {
3382 data->arg.ftype = NF4CHR;
3383 data->arg.u.device.specdata1 = MAJOR(rdev);
3384 data->arg.u.device.specdata2 = MINOR(rdev);
3385 } else if (!S_ISSOCK(mode)) {
3386 status = -EINVAL;
3387 goto out_free;
3388 }
3389
3390 status = nfs4_do_create(dir, dentry, data);
3391out_free:
3392 nfs4_free_createdata(data);
3393out:
3394 return status;
3395}
3396
3397static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3398 struct iattr *sattr, dev_t rdev)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
3402
3403 sattr->ia_mode &= ~current_umask();
3404 do {
3405 err = nfs4_handle_exception(NFS_SERVER(dir),
3406 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3407 &exception);
3408 } while (exception.retry);
3409 return err;
3410}
3411
3412static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3413 struct nfs_fsstat *fsstat)
3414{
3415 struct nfs4_statfs_arg args = {
3416 .fh = fhandle,
3417 .bitmask = server->attr_bitmask,
3418 };
3419 struct nfs4_statfs_res res = {
3420 .fsstat = fsstat,
3421 };
3422 struct rpc_message msg = {
3423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3424 .rpc_argp = &args,
3425 .rpc_resp = &res,
3426 };
3427
3428 nfs_fattr_init(fsstat->fattr);
3429 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3430}
3431
3432static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3433{
3434 struct nfs4_exception exception = { };
3435 int err;
3436 do {
3437 err = nfs4_handle_exception(server,
3438 _nfs4_proc_statfs(server, fhandle, fsstat),
3439 &exception);
3440 } while (exception.retry);
3441 return err;
3442}
3443
3444static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3445 struct nfs_fsinfo *fsinfo)
3446{
3447 struct nfs4_fsinfo_arg args = {
3448 .fh = fhandle,
3449 .bitmask = server->attr_bitmask,
3450 };
3451 struct nfs4_fsinfo_res res = {
3452 .fsinfo = fsinfo,
3453 };
3454 struct rpc_message msg = {
3455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3456 .rpc_argp = &args,
3457 .rpc_resp = &res,
3458 };
3459
3460 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3461}
3462
3463static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3464{
3465 struct nfs4_exception exception = { };
3466 int err;
3467
3468 do {
3469 err = nfs4_handle_exception(server,
3470 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3471 &exception);
3472 } while (exception.retry);
3473 return err;
3474}
3475
3476static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3477{
3478 int error;
3479
3480 nfs_fattr_init(fsinfo->fattr);
3481 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3482 if (error == 0) {
3483 /* block layout checks this! */
3484 server->pnfs_blksize = fsinfo->blksize;
3485 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3486 }
3487
3488 return error;
3489}
3490
3491static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3492 struct nfs_pathconf *pathconf)
3493{
3494 struct nfs4_pathconf_arg args = {
3495 .fh = fhandle,
3496 .bitmask = server->attr_bitmask,
3497 };
3498 struct nfs4_pathconf_res res = {
3499 .pathconf = pathconf,
3500 };
3501 struct rpc_message msg = {
3502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3503 .rpc_argp = &args,
3504 .rpc_resp = &res,
3505 };
3506
3507 /* None of the pathconf attributes are mandatory to implement */
3508 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3509 memset(pathconf, 0, sizeof(*pathconf));
3510 return 0;
3511 }
3512
3513 nfs_fattr_init(pathconf->fattr);
3514 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3515}
3516
3517static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3518 struct nfs_pathconf *pathconf)
3519{
3520 struct nfs4_exception exception = { };
3521 int err;
3522
3523 do {
3524 err = nfs4_handle_exception(server,
3525 _nfs4_proc_pathconf(server, fhandle, pathconf),
3526 &exception);
3527 } while (exception.retry);
3528 return err;
3529}
3530
3531int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3532 const struct nfs_open_context *ctx,
3533 const struct nfs_lock_context *l_ctx,
3534 fmode_t fmode)
3535{
3536 const struct nfs_lockowner *lockowner = NULL;
3537
3538 if (l_ctx != NULL)
3539 lockowner = &l_ctx->lockowner;
3540 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
3541}
3542EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3543
3544static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3545 const struct nfs_open_context *ctx,
3546 const struct nfs_lock_context *l_ctx,
3547 fmode_t fmode)
3548{
3549 nfs4_stateid current_stateid;
3550
3551 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3552 return false;
3553 return nfs4_stateid_match(stateid, &current_stateid);
3554}
3555
3556static bool nfs4_error_stateid_expired(int err)
3557{
3558 switch (err) {
3559 case -NFS4ERR_DELEG_REVOKED:
3560 case -NFS4ERR_ADMIN_REVOKED:
3561 case -NFS4ERR_BAD_STATEID:
3562 case -NFS4ERR_STALE_STATEID:
3563 case -NFS4ERR_OLD_STATEID:
3564 case -NFS4ERR_OPENMODE:
3565 case -NFS4ERR_EXPIRED:
3566 return true;
3567 }
3568 return false;
3569}
3570
3571void __nfs4_read_done_cb(struct nfs_read_data *data)
3572{
3573 nfs_invalidate_atime(data->header->inode);
3574}
3575
3576static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3577{
3578 struct nfs_server *server = NFS_SERVER(data->header->inode);
3579
3580 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3581 rpc_restart_call_prepare(task);
3582 return -EAGAIN;
3583 }
3584
3585 __nfs4_read_done_cb(data);
3586 if (task->tk_status > 0)
3587 renew_lease(server, data->timestamp);
3588 return 0;
3589}
3590
3591static bool nfs4_read_stateid_changed(struct rpc_task *task,
3592 struct nfs_readargs *args)
3593{
3594
3595 if (!nfs4_error_stateid_expired(task->tk_status) ||
3596 nfs4_stateid_is_current(&args->stateid,
3597 args->context,
3598 args->lock_context,
3599 FMODE_READ))
3600 return false;
3601 rpc_restart_call_prepare(task);
3602 return true;
3603}
3604
3605static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3606{
3607
3608 dprintk("--> %s\n", __func__);
3609
3610 if (!nfs4_sequence_done(task, &data->res.seq_res))
3611 return -EAGAIN;
3612 if (nfs4_read_stateid_changed(task, &data->args))
3613 return -EAGAIN;
3614 return data->read_done_cb ? data->read_done_cb(task, data) :
3615 nfs4_read_done_cb(task, data);
3616}
3617
3618static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3619{
3620 data->timestamp = jiffies;
3621 data->read_done_cb = nfs4_read_done_cb;
3622 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3623 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3624}
3625
3626static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3627{
3628 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3629 &data->args.seq_args,
3630 &data->res.seq_res,
3631 task))
3632 return;
3633 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3634 data->args.lock_context, FMODE_READ);
3635}
3636
3637static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3638{
3639 struct inode *inode = data->header->inode;
3640
3641 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3642 rpc_restart_call_prepare(task);
3643 return -EAGAIN;
3644 }
3645 if (task->tk_status >= 0) {
3646 renew_lease(NFS_SERVER(inode), data->timestamp);
3647 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3648 }
3649 return 0;
3650}
3651
3652static bool nfs4_write_stateid_changed(struct rpc_task *task,
3653 struct nfs_writeargs *args)
3654{
3655
3656 if (!nfs4_error_stateid_expired(task->tk_status) ||
3657 nfs4_stateid_is_current(&args->stateid,
3658 args->context,
3659 args->lock_context,
3660 FMODE_WRITE))
3661 return false;
3662 rpc_restart_call_prepare(task);
3663 return true;
3664}
3665
3666static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3667{
3668 if (!nfs4_sequence_done(task, &data->res.seq_res))
3669 return -EAGAIN;
3670 if (nfs4_write_stateid_changed(task, &data->args))
3671 return -EAGAIN;
3672 return data->write_done_cb ? data->write_done_cb(task, data) :
3673 nfs4_write_done_cb(task, data);
3674}
3675
3676static
3677bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3678{
3679 const struct nfs_pgio_header *hdr = data->header;
3680
3681 /* Don't request attributes for pNFS or O_DIRECT writes */
3682 if (data->ds_clp != NULL || hdr->dreq != NULL)
3683 return false;
3684 /* Otherwise, request attributes if and only if we don't hold
3685 * a delegation
3686 */
3687 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3688}
3689
3690static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3691{
3692 struct nfs_server *server = NFS_SERVER(data->header->inode);
3693
3694 if (!nfs4_write_need_cache_consistency_data(data)) {
3695 data->args.bitmask = NULL;
3696 data->res.fattr = NULL;
3697 } else
3698 data->args.bitmask = server->cache_consistency_bitmask;
3699
3700 if (!data->write_done_cb)
3701 data->write_done_cb = nfs4_write_done_cb;
3702 data->res.server = server;
3703 data->timestamp = jiffies;
3704
3705 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3706 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3707}
3708
3709static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3710{
3711 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3712 &data->args.seq_args,
3713 &data->res.seq_res,
3714 task))
3715 return;
3716 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3717 data->args.lock_context, FMODE_WRITE);
3718}
3719
3720static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3721{
3722 nfs4_setup_sequence(NFS_SERVER(data->inode),
3723 &data->args.seq_args,
3724 &data->res.seq_res,
3725 task);
3726}
3727
3728static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3729{
3730 struct inode *inode = data->inode;
3731
3732 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3733 rpc_restart_call_prepare(task);
3734 return -EAGAIN;
3735 }
3736 return 0;
3737}
3738
3739static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3740{
3741 if (!nfs4_sequence_done(task, &data->res.seq_res))
3742 return -EAGAIN;
3743 return data->commit_done_cb(task, data);
3744}
3745
3746static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3747{
3748 struct nfs_server *server = NFS_SERVER(data->inode);
3749
3750 if (data->commit_done_cb == NULL)
3751 data->commit_done_cb = nfs4_commit_done_cb;
3752 data->res.server = server;
3753 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3754 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3755}
3756
3757struct nfs4_renewdata {
3758 struct nfs_client *client;
3759 unsigned long timestamp;
3760};
3761
3762/*
3763 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3764 * standalone procedure for queueing an asynchronous RENEW.
3765 */
3766static void nfs4_renew_release(void *calldata)
3767{
3768 struct nfs4_renewdata *data = calldata;
3769 struct nfs_client *clp = data->client;
3770
3771 if (atomic_read(&clp->cl_count) > 1)
3772 nfs4_schedule_state_renewal(clp);
3773 nfs_put_client(clp);
3774 kfree(data);
3775}
3776
3777static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3778{
3779 struct nfs4_renewdata *data = calldata;
3780 struct nfs_client *clp = data->client;
3781 unsigned long timestamp = data->timestamp;
3782
3783 if (task->tk_status < 0) {
3784 /* Unless we're shutting down, schedule state recovery! */
3785 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3786 return;
3787 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3788 nfs4_schedule_lease_recovery(clp);
3789 return;
3790 }
3791 nfs4_schedule_path_down_recovery(clp);
3792 }
3793 do_renew_lease(clp, timestamp);
3794}
3795
3796static const struct rpc_call_ops nfs4_renew_ops = {
3797 .rpc_call_done = nfs4_renew_done,
3798 .rpc_release = nfs4_renew_release,
3799};
3800
3801static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3802{
3803 struct rpc_message msg = {
3804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3805 .rpc_argp = clp,
3806 .rpc_cred = cred,
3807 };
3808 struct nfs4_renewdata *data;
3809
3810 if (renew_flags == 0)
3811 return 0;
3812 if (!atomic_inc_not_zero(&clp->cl_count))
3813 return -EIO;
3814 data = kmalloc(sizeof(*data), GFP_NOFS);
3815 if (data == NULL)
3816 return -ENOMEM;
3817 data->client = clp;
3818 data->timestamp = jiffies;
3819 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
3820 &nfs4_renew_ops, data);
3821}
3822
3823static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3824{
3825 struct rpc_message msg = {
3826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3827 .rpc_argp = clp,
3828 .rpc_cred = cred,
3829 };
3830 unsigned long now = jiffies;
3831 int status;
3832
3833 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3834 if (status < 0)
3835 return status;
3836 do_renew_lease(clp, now);
3837 return 0;
3838}
3839
3840static inline int nfs4_server_supports_acls(struct nfs_server *server)
3841{
3842 return (server->caps & NFS_CAP_ACLS)
3843 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3844 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3845}
3846
3847/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3848 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3849 * the stack.
3850 */
3851#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3852
3853static int buf_to_pages_noslab(const void *buf, size_t buflen,
3854 struct page **pages, unsigned int *pgbase)
3855{
3856 struct page *newpage, **spages;
3857 int rc = 0;
3858 size_t len;
3859 spages = pages;
3860
3861 do {
3862 len = min_t(size_t, PAGE_SIZE, buflen);
3863 newpage = alloc_page(GFP_KERNEL);
3864
3865 if (newpage == NULL)
3866 goto unwind;
3867 memcpy(page_address(newpage), buf, len);
3868 buf += len;
3869 buflen -= len;
3870 *pages++ = newpage;
3871 rc++;
3872 } while (buflen != 0);
3873
3874 return rc;
3875
3876unwind:
3877 for(; rc > 0; rc--)
3878 __free_page(spages[rc-1]);
3879 return -ENOMEM;
3880}
3881
3882struct nfs4_cached_acl {
3883 int cached;
3884 size_t len;
3885 char data[0];
3886};
3887
3888static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3889{
3890 struct nfs_inode *nfsi = NFS_I(inode);
3891
3892 spin_lock(&inode->i_lock);
3893 kfree(nfsi->nfs4_acl);
3894 nfsi->nfs4_acl = acl;
3895 spin_unlock(&inode->i_lock);
3896}
3897
3898static void nfs4_zap_acl_attr(struct inode *inode)
3899{
3900 nfs4_set_cached_acl(inode, NULL);
3901}
3902
3903static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3904{
3905 struct nfs_inode *nfsi = NFS_I(inode);
3906 struct nfs4_cached_acl *acl;
3907 int ret = -ENOENT;
3908
3909 spin_lock(&inode->i_lock);
3910 acl = nfsi->nfs4_acl;
3911 if (acl == NULL)
3912 goto out;
3913 if (buf == NULL) /* user is just asking for length */
3914 goto out_len;
3915 if (acl->cached == 0)
3916 goto out;
3917 ret = -ERANGE; /* see getxattr(2) man page */
3918 if (acl->len > buflen)
3919 goto out;
3920 memcpy(buf, acl->data, acl->len);
3921out_len:
3922 ret = acl->len;
3923out:
3924 spin_unlock(&inode->i_lock);
3925 return ret;
3926}
3927
3928static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3929{
3930 struct nfs4_cached_acl *acl;
3931 size_t buflen = sizeof(*acl) + acl_len;
3932
3933 if (buflen <= PAGE_SIZE) {
3934 acl = kmalloc(buflen, GFP_KERNEL);
3935 if (acl == NULL)
3936 goto out;
3937 acl->cached = 1;
3938 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3939 } else {
3940 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3941 if (acl == NULL)
3942 goto out;
3943 acl->cached = 0;
3944 }
3945 acl->len = acl_len;
3946out:
3947 nfs4_set_cached_acl(inode, acl);
3948}
3949
3950/*
3951 * The getxattr API returns the required buffer length when called with a
3952 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3953 * the required buf. On a NULL buf, we send a page of data to the server
3954 * guessing that the ACL request can be serviced by a page. If so, we cache
3955 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3956 * the cache. If not so, we throw away the page, and cache the required
3957 * length. The next getxattr call will then produce another round trip to
3958 * the server, this time with the input buf of the required size.
3959 */
3960static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3961{
3962 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3963 struct nfs_getaclargs args = {
3964 .fh = NFS_FH(inode),
3965 .acl_pages = pages,
3966 .acl_len = buflen,
3967 };
3968 struct nfs_getaclres res = {
3969 .acl_len = buflen,
3970 };
3971 struct rpc_message msg = {
3972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3973 .rpc_argp = &args,
3974 .rpc_resp = &res,
3975 };
3976 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3977 int ret = -ENOMEM, i;
3978
3979 /* As long as we're doing a round trip to the server anyway,
3980 * let's be prepared for a page of acl data. */
3981 if (npages == 0)
3982 npages = 1;
3983 if (npages > ARRAY_SIZE(pages))
3984 return -ERANGE;
3985
3986 for (i = 0; i < npages; i++) {
3987 pages[i] = alloc_page(GFP_KERNEL);
3988 if (!pages[i])
3989 goto out_free;
3990 }
3991
3992 /* for decoding across pages */
3993 res.acl_scratch = alloc_page(GFP_KERNEL);
3994 if (!res.acl_scratch)
3995 goto out_free;
3996
3997 args.acl_len = npages * PAGE_SIZE;
3998 args.acl_pgbase = 0;
3999
4000 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4001 __func__, buf, buflen, npages, args.acl_len);
4002 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4003 &msg, &args.seq_args, &res.seq_res, 0);
4004 if (ret)
4005 goto out_free;
4006
4007 /* Handle the case where the passed-in buffer is too short */
4008 if (res.acl_flags & NFS4_ACL_TRUNC) {
4009 /* Did the user only issue a request for the acl length? */
4010 if (buf == NULL)
4011 goto out_ok;
4012 ret = -ERANGE;
4013 goto out_free;
4014 }
4015 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4016 if (buf) {
4017 if (res.acl_len > buflen) {
4018 ret = -ERANGE;
4019 goto out_free;
4020 }
4021 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4022 }
4023out_ok:
4024 ret = res.acl_len;
4025out_free:
4026 for (i = 0; i < npages; i++)
4027 if (pages[i])
4028 __free_page(pages[i]);
4029 if (res.acl_scratch)
4030 __free_page(res.acl_scratch);
4031 return ret;
4032}
4033
4034static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4035{
4036 struct nfs4_exception exception = { };
4037 ssize_t ret;
4038 do {
4039 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4040 if (ret >= 0)
4041 break;
4042 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4043 } while (exception.retry);
4044 return ret;
4045}
4046
4047static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4048{
4049 struct nfs_server *server = NFS_SERVER(inode);
4050 int ret;
4051
4052 if (!nfs4_server_supports_acls(server))
4053 return -EOPNOTSUPP;
4054 ret = nfs_revalidate_inode(server, inode);
4055 if (ret < 0)
4056 return ret;
4057 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4058 nfs_zap_acl_cache(inode);
4059 ret = nfs4_read_cached_acl(inode, buf, buflen);
4060 if (ret != -ENOENT)
4061 /* -ENOENT is returned if there is no ACL or if there is an ACL
4062 * but no cached acl data, just the acl length */
4063 return ret;
4064 return nfs4_get_acl_uncached(inode, buf, buflen);
4065}
4066
4067static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4068{
4069 struct nfs_server *server = NFS_SERVER(inode);
4070 struct page *pages[NFS4ACL_MAXPAGES];
4071 struct nfs_setaclargs arg = {
4072 .fh = NFS_FH(inode),
4073 .acl_pages = pages,
4074 .acl_len = buflen,
4075 };
4076 struct nfs_setaclres res;
4077 struct rpc_message msg = {
4078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4079 .rpc_argp = &arg,
4080 .rpc_resp = &res,
4081 };
4082 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4083 int ret, i;
4084
4085 if (!nfs4_server_supports_acls(server))
4086 return -EOPNOTSUPP;
4087 if (npages > ARRAY_SIZE(pages))
4088 return -ERANGE;
4089 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4090 if (i < 0)
4091 return i;
4092 nfs4_inode_return_delegation(inode);
4093 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4094
4095 /*
4096 * Free each page after tx, so the only ref left is
4097 * held by the network stack
4098 */
4099 for (; i > 0; i--)
4100 put_page(pages[i-1]);
4101
4102 /*
4103 * Acl update can result in inode attribute update.
4104 * so mark the attribute cache invalid.
4105 */
4106 spin_lock(&inode->i_lock);
4107 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4108 spin_unlock(&inode->i_lock);
4109 nfs_access_zap_cache(inode);
4110 nfs_zap_acl_cache(inode);
4111 return ret;
4112}
4113
4114static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4115{
4116 struct nfs4_exception exception = { };
4117 int err;
4118 do {
4119 err = nfs4_handle_exception(NFS_SERVER(inode),
4120 __nfs4_proc_set_acl(inode, buf, buflen),
4121 &exception);
4122 } while (exception.retry);
4123 return err;
4124}
4125
4126static int
4127nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4128{
4129 struct nfs_client *clp = server->nfs_client;
4130
4131 if (task->tk_status >= 0)
4132 return 0;
4133 switch(task->tk_status) {
4134 case -NFS4ERR_DELEG_REVOKED:
4135 case -NFS4ERR_ADMIN_REVOKED:
4136 case -NFS4ERR_BAD_STATEID:
4137 if (state == NULL)
4138 break;
4139 nfs_remove_bad_delegation(state->inode);
4140 case -NFS4ERR_OPENMODE:
4141 if (state == NULL)
4142 break;
4143 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4144 goto stateid_invalid;
4145 goto wait_on_recovery;
4146 case -NFS4ERR_EXPIRED:
4147 if (state != NULL) {
4148 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4149 goto stateid_invalid;
4150 }
4151 case -NFS4ERR_STALE_STATEID:
4152 case -NFS4ERR_STALE_CLIENTID:
4153 nfs4_schedule_lease_recovery(clp);
4154 goto wait_on_recovery;
4155#if defined(CONFIG_NFS_V4_1)
4156 case -NFS4ERR_BADSESSION:
4157 case -NFS4ERR_BADSLOT:
4158 case -NFS4ERR_BAD_HIGH_SLOT:
4159 case -NFS4ERR_DEADSESSION:
4160 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4161 case -NFS4ERR_SEQ_FALSE_RETRY:
4162 case -NFS4ERR_SEQ_MISORDERED:
4163 dprintk("%s ERROR %d, Reset session\n", __func__,
4164 task->tk_status);
4165 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4166 task->tk_status = 0;
4167 return -EAGAIN;
4168#endif /* CONFIG_NFS_V4_1 */
4169 case -NFS4ERR_DELAY:
4170 nfs_inc_server_stats(server, NFSIOS_DELAY);
4171 case -NFS4ERR_GRACE:
4172 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4173 task->tk_status = 0;
4174 return -EAGAIN;
4175 case -NFS4ERR_RETRY_UNCACHED_REP:
4176 case -NFS4ERR_OLD_STATEID:
4177 task->tk_status = 0;
4178 return -EAGAIN;
4179 }
4180 task->tk_status = nfs4_map_errors(task->tk_status);
4181 return 0;
4182stateid_invalid:
4183 task->tk_status = -EIO;
4184 return 0;
4185wait_on_recovery:
4186 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4187 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4188 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4189 task->tk_status = 0;
4190 return -EAGAIN;
4191}
4192
4193static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4194 nfs4_verifier *bootverf)
4195{
4196 __be32 verf[2];
4197
4198 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4199 /* An impossible timestamp guarantees this value
4200 * will never match a generated boot time. */
4201 verf[0] = 0;
4202 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4203 } else {
4204 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4205 verf[0] = (__be32)nn->boot_time.tv_sec;
4206 verf[1] = (__be32)nn->boot_time.tv_nsec;
4207 }
4208 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4209}
4210
4211static unsigned int
4212nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4213 char *buf, size_t len)
4214{
4215 unsigned int result;
4216
4217 rcu_read_lock();
4218 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4219 clp->cl_ipaddr,
4220 rpc_peeraddr2str(clp->cl_rpcclient,
4221 RPC_DISPLAY_ADDR),
4222 rpc_peeraddr2str(clp->cl_rpcclient,
4223 RPC_DISPLAY_PROTO));
4224 rcu_read_unlock();
4225 return result;
4226}
4227
4228static unsigned int
4229nfs4_init_uniform_client_string(const struct nfs_client *clp,
4230 char *buf, size_t len)
4231{
4232 char *nodename = clp->cl_rpcclient->cl_nodename;
4233
4234 if (nfs4_client_id_uniquifier[0] != '\0')
4235 nodename = nfs4_client_id_uniquifier;
4236 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4237 clp->rpc_ops->version, clp->cl_minorversion,
4238 nodename);
4239}
4240
4241/**
4242 * nfs4_proc_setclientid - Negotiate client ID
4243 * @clp: state data structure
4244 * @program: RPC program for NFSv4 callback service
4245 * @port: IP port number for NFS4 callback service
4246 * @cred: RPC credential to use for this call
4247 * @res: where to place the result
4248 *
4249 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4250 */
4251int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4252 unsigned short port, struct rpc_cred *cred,
4253 struct nfs4_setclientid_res *res)
4254{
4255 nfs4_verifier sc_verifier;
4256 struct nfs4_setclientid setclientid = {
4257 .sc_verifier = &sc_verifier,
4258 .sc_prog = program,
4259 .sc_cb_ident = clp->cl_cb_ident,
4260 };
4261 struct rpc_message msg = {
4262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4263 .rpc_argp = &setclientid,
4264 .rpc_resp = res,
4265 .rpc_cred = cred,
4266 };
4267 int status;
4268
4269 /* nfs_client_id4 */
4270 nfs4_init_boot_verifier(clp, &sc_verifier);
4271 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4272 setclientid.sc_name_len =
4273 nfs4_init_uniform_client_string(clp,
4274 setclientid.sc_name,
4275 sizeof(setclientid.sc_name));
4276 else
4277 setclientid.sc_name_len =
4278 nfs4_init_nonuniform_client_string(clp,
4279 setclientid.sc_name,
4280 sizeof(setclientid.sc_name));
4281 /* cb_client4 */
4282 rcu_read_lock();
4283 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4284 sizeof(setclientid.sc_netid),
4285 rpc_peeraddr2str(clp->cl_rpcclient,
4286 RPC_DISPLAY_NETID));
4287 rcu_read_unlock();
4288 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4289 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4290 clp->cl_ipaddr, port >> 8, port & 255);
4291
4292 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4293 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4294 setclientid.sc_name_len, setclientid.sc_name);
4295 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4296 dprintk("NFS reply setclientid: %d\n", status);
4297 return status;
4298}
4299
4300/**
4301 * nfs4_proc_setclientid_confirm - Confirm client ID
4302 * @clp: state data structure
4303 * @res: result of a previous SETCLIENTID
4304 * @cred: RPC credential to use for this call
4305 *
4306 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4307 */
4308int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4309 struct nfs4_setclientid_res *arg,
4310 struct rpc_cred *cred)
4311{
4312 struct nfs_fsinfo fsinfo;
4313 struct rpc_message msg = {
4314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4315 .rpc_argp = arg,
4316 .rpc_resp = &fsinfo,
4317 .rpc_cred = cred,
4318 };
4319 unsigned long now;
4320 int status;
4321
4322 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4323 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4324 clp->cl_clientid);
4325 now = jiffies;
4326 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4327 if (status == 0) {
4328 spin_lock(&clp->cl_lock);
4329 clp->cl_lease_time = fsinfo.lease_time * HZ;
4330 clp->cl_last_renewal = now;
4331 spin_unlock(&clp->cl_lock);
4332 }
4333 dprintk("NFS reply setclientid_confirm: %d\n", status);
4334 return status;
4335}
4336
4337struct nfs4_delegreturndata {
4338 struct nfs4_delegreturnargs args;
4339 struct nfs4_delegreturnres res;
4340 struct nfs_fh fh;
4341 nfs4_stateid stateid;
4342 unsigned long timestamp;
4343 struct nfs_fattr fattr;
4344 int rpc_status;
4345};
4346
4347static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4348{
4349 struct nfs4_delegreturndata *data = calldata;
4350
4351 if (!nfs4_sequence_done(task, &data->res.seq_res))
4352 return;
4353
4354 switch (task->tk_status) {
4355 case -NFS4ERR_STALE_STATEID:
4356 case -NFS4ERR_EXPIRED:
4357 case 0:
4358 renew_lease(data->res.server, data->timestamp);
4359 break;
4360 default:
4361 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4362 -EAGAIN) {
4363 rpc_restart_call_prepare(task);
4364 return;
4365 }
4366 }
4367 data->rpc_status = task->tk_status;
4368}
4369
4370static void nfs4_delegreturn_release(void *calldata)
4371{
4372 kfree(calldata);
4373}
4374
4375#if defined(CONFIG_NFS_V4_1)
4376static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4377{
4378 struct nfs4_delegreturndata *d_data;
4379
4380 d_data = (struct nfs4_delegreturndata *)data;
4381
4382 nfs4_setup_sequence(d_data->res.server,
4383 &d_data->args.seq_args,
4384 &d_data->res.seq_res,
4385 task);
4386}
4387#endif /* CONFIG_NFS_V4_1 */
4388
4389static const struct rpc_call_ops nfs4_delegreturn_ops = {
4390#if defined(CONFIG_NFS_V4_1)
4391 .rpc_call_prepare = nfs4_delegreturn_prepare,
4392#endif /* CONFIG_NFS_V4_1 */
4393 .rpc_call_done = nfs4_delegreturn_done,
4394 .rpc_release = nfs4_delegreturn_release,
4395};
4396
4397static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4398{
4399 struct nfs4_delegreturndata *data;
4400 struct nfs_server *server = NFS_SERVER(inode);
4401 struct rpc_task *task;
4402 struct rpc_message msg = {
4403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4404 .rpc_cred = cred,
4405 };
4406 struct rpc_task_setup task_setup_data = {
4407 .rpc_client = server->client,
4408 .rpc_message = &msg,
4409 .callback_ops = &nfs4_delegreturn_ops,
4410 .flags = RPC_TASK_ASYNC,
4411 };
4412 int status = 0;
4413
4414 data = kzalloc(sizeof(*data), GFP_NOFS);
4415 if (data == NULL)
4416 return -ENOMEM;
4417 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4418 data->args.fhandle = &data->fh;
4419 data->args.stateid = &data->stateid;
4420 data->args.bitmask = server->cache_consistency_bitmask;
4421 nfs_copy_fh(&data->fh, NFS_FH(inode));
4422 nfs4_stateid_copy(&data->stateid, stateid);
4423 data->res.fattr = &data->fattr;
4424 data->res.server = server;
4425 nfs_fattr_init(data->res.fattr);
4426 data->timestamp = jiffies;
4427 data->rpc_status = 0;
4428
4429 task_setup_data.callback_data = data;
4430 msg.rpc_argp = &data->args;
4431 msg.rpc_resp = &data->res;
4432 task = rpc_run_task(&task_setup_data);
4433 if (IS_ERR(task))
4434 return PTR_ERR(task);
4435 if (!issync)
4436 goto out;
4437 status = nfs4_wait_for_completion_rpc_task(task);
4438 if (status != 0)
4439 goto out;
4440 status = data->rpc_status;
4441 if (status == 0)
4442 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4443 else
4444 nfs_refresh_inode(inode, &data->fattr);
4445out:
4446 rpc_put_task(task);
4447 return status;
4448}
4449
4450int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4451{
4452 struct nfs_server *server = NFS_SERVER(inode);
4453 struct nfs4_exception exception = { };
4454 int err;
4455 do {
4456 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4457 switch (err) {
4458 case -NFS4ERR_STALE_STATEID:
4459 case -NFS4ERR_EXPIRED:
4460 case 0:
4461 return 0;
4462 }
4463 err = nfs4_handle_exception(server, err, &exception);
4464 } while (exception.retry);
4465 return err;
4466}
4467
4468#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4469#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4470
4471/*
4472 * sleep, with exponential backoff, and retry the LOCK operation.
4473 */
4474static unsigned long
4475nfs4_set_lock_task_retry(unsigned long timeout)
4476{
4477 freezable_schedule_timeout_killable(timeout);
4478 timeout <<= 1;
4479 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4480 return NFS4_LOCK_MAXTIMEOUT;
4481 return timeout;
4482}
4483
4484static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4485{
4486 struct inode *inode = state->inode;
4487 struct nfs_server *server = NFS_SERVER(inode);
4488 struct nfs_client *clp = server->nfs_client;
4489 struct nfs_lockt_args arg = {
4490 .fh = NFS_FH(inode),
4491 .fl = request,
4492 };
4493 struct nfs_lockt_res res = {
4494 .denied = request,
4495 };
4496 struct rpc_message msg = {
4497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4498 .rpc_argp = &arg,
4499 .rpc_resp = &res,
4500 .rpc_cred = state->owner->so_cred,
4501 };
4502 struct nfs4_lock_state *lsp;
4503 int status;
4504
4505 arg.lock_owner.clientid = clp->cl_clientid;
4506 status = nfs4_set_lock_state(state, request);
4507 if (status != 0)
4508 goto out;
4509 lsp = request->fl_u.nfs4_fl.owner;
4510 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4511 arg.lock_owner.s_dev = server->s_dev;
4512 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4513 switch (status) {
4514 case 0:
4515 request->fl_type = F_UNLCK;
4516 break;
4517 case -NFS4ERR_DENIED:
4518 status = 0;
4519 }
4520 request->fl_ops->fl_release_private(request);
4521out:
4522 return status;
4523}
4524
4525static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4526{
4527 struct nfs4_exception exception = { };
4528 int err;
4529
4530 do {
4531 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4532 _nfs4_proc_getlk(state, cmd, request),
4533 &exception);
4534 } while (exception.retry);
4535 return err;
4536}
4537
4538static int do_vfs_lock(struct file *file, struct file_lock *fl)
4539{
4540 int res = 0;
4541 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4542 case FL_POSIX:
4543 res = posix_lock_file_wait(file, fl);
4544 break;
4545 case FL_FLOCK:
4546 res = flock_lock_file_wait(file, fl);
4547 break;
4548 default:
4549 BUG();
4550 }
4551 return res;
4552}
4553
4554struct nfs4_unlockdata {
4555 struct nfs_locku_args arg;
4556 struct nfs_locku_res res;
4557 struct nfs4_lock_state *lsp;
4558 struct nfs_open_context *ctx;
4559 struct file_lock fl;
4560 const struct nfs_server *server;
4561 unsigned long timestamp;
4562};
4563
4564static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4565 struct nfs_open_context *ctx,
4566 struct nfs4_lock_state *lsp,
4567 struct nfs_seqid *seqid)
4568{
4569 struct nfs4_unlockdata *p;
4570 struct inode *inode = lsp->ls_state->inode;
4571
4572 p = kzalloc(sizeof(*p), GFP_NOFS);
4573 if (p == NULL)
4574 return NULL;
4575 p->arg.fh = NFS_FH(inode);
4576 p->arg.fl = &p->fl;
4577 p->arg.seqid = seqid;
4578 p->res.seqid = seqid;
4579 p->arg.stateid = &lsp->ls_stateid;
4580 p->lsp = lsp;
4581 atomic_inc(&lsp->ls_count);
4582 /* Ensure we don't close file until we're done freeing locks! */
4583 p->ctx = get_nfs_open_context(ctx);
4584 memcpy(&p->fl, fl, sizeof(p->fl));
4585 p->server = NFS_SERVER(inode);
4586 return p;
4587}
4588
4589static void nfs4_locku_release_calldata(void *data)
4590{
4591 struct nfs4_unlockdata *calldata = data;
4592 nfs_free_seqid(calldata->arg.seqid);
4593 nfs4_put_lock_state(calldata->lsp);
4594 put_nfs_open_context(calldata->ctx);
4595 kfree(calldata);
4596}
4597
4598static void nfs4_locku_done(struct rpc_task *task, void *data)
4599{
4600 struct nfs4_unlockdata *calldata = data;
4601
4602 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4603 return;
4604 switch (task->tk_status) {
4605 case 0:
4606 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4607 &calldata->res.stateid);
4608 renew_lease(calldata->server, calldata->timestamp);
4609 break;
4610 case -NFS4ERR_BAD_STATEID:
4611 case -NFS4ERR_OLD_STATEID:
4612 case -NFS4ERR_STALE_STATEID:
4613 case -NFS4ERR_EXPIRED:
4614 break;
4615 default:
4616 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4617 rpc_restart_call_prepare(task);
4618 }
4619 nfs_release_seqid(calldata->arg.seqid);
4620}
4621
4622static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4623{
4624 struct nfs4_unlockdata *calldata = data;
4625
4626 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4627 goto out_wait;
4628 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4629 /* Note: exit _without_ running nfs4_locku_done */
4630 goto out_no_action;
4631 }
4632 calldata->timestamp = jiffies;
4633 if (nfs4_setup_sequence(calldata->server,
4634 &calldata->arg.seq_args,
4635 &calldata->res.seq_res,
4636 task) != 0)
4637 nfs_release_seqid(calldata->arg.seqid);
4638 return;
4639out_no_action:
4640 task->tk_action = NULL;
4641out_wait:
4642 nfs4_sequence_done(task, &calldata->res.seq_res);
4643}
4644
4645static const struct rpc_call_ops nfs4_locku_ops = {
4646 .rpc_call_prepare = nfs4_locku_prepare,
4647 .rpc_call_done = nfs4_locku_done,
4648 .rpc_release = nfs4_locku_release_calldata,
4649};
4650
4651static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4652 struct nfs_open_context *ctx,
4653 struct nfs4_lock_state *lsp,
4654 struct nfs_seqid *seqid)
4655{
4656 struct nfs4_unlockdata *data;
4657 struct rpc_message msg = {
4658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4659 .rpc_cred = ctx->cred,
4660 };
4661 struct rpc_task_setup task_setup_data = {
4662 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4663 .rpc_message = &msg,
4664 .callback_ops = &nfs4_locku_ops,
4665 .workqueue = nfsiod_workqueue,
4666 .flags = RPC_TASK_ASYNC,
4667 };
4668
4669 /* Ensure this is an unlock - when canceling a lock, the
4670 * canceled lock is passed in, and it won't be an unlock.
4671 */
4672 fl->fl_type = F_UNLCK;
4673
4674 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4675 if (data == NULL) {
4676 nfs_free_seqid(seqid);
4677 return ERR_PTR(-ENOMEM);
4678 }
4679
4680 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4681 msg.rpc_argp = &data->arg;
4682 msg.rpc_resp = &data->res;
4683 task_setup_data.callback_data = data;
4684 return rpc_run_task(&task_setup_data);
4685}
4686
4687static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4688{
4689 struct inode *inode = state->inode;
4690 struct nfs4_state_owner *sp = state->owner;
4691 struct nfs_inode *nfsi = NFS_I(inode);
4692 struct nfs_seqid *seqid;
4693 struct nfs4_lock_state *lsp;
4694 struct rpc_task *task;
4695 int status = 0;
4696 unsigned char fl_flags = request->fl_flags;
4697
4698 status = nfs4_set_lock_state(state, request);
4699 /* Unlock _before_ we do the RPC call */
4700 request->fl_flags |= FL_EXISTS;
4701 /* Exclude nfs_delegation_claim_locks() */
4702 mutex_lock(&sp->so_delegreturn_mutex);
4703 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
4704 down_read(&nfsi->rwsem);
4705 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4706 up_read(&nfsi->rwsem);
4707 mutex_unlock(&sp->so_delegreturn_mutex);
4708 goto out;
4709 }
4710 up_read(&nfsi->rwsem);
4711 mutex_unlock(&sp->so_delegreturn_mutex);
4712 if (status != 0)
4713 goto out;
4714 /* Is this a delegated lock? */
4715 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4716 goto out;
4717 lsp = request->fl_u.nfs4_fl.owner;
4718 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4719 status = -ENOMEM;
4720 if (seqid == NULL)
4721 goto out;
4722 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4723 status = PTR_ERR(task);
4724 if (IS_ERR(task))
4725 goto out;
4726 status = nfs4_wait_for_completion_rpc_task(task);
4727 rpc_put_task(task);
4728out:
4729 request->fl_flags = fl_flags;
4730 return status;
4731}
4732
4733struct nfs4_lockdata {
4734 struct nfs_lock_args arg;
4735 struct nfs_lock_res res;
4736 struct nfs4_lock_state *lsp;
4737 struct nfs_open_context *ctx;
4738 struct file_lock fl;
4739 unsigned long timestamp;
4740 int rpc_status;
4741 int cancelled;
4742 struct nfs_server *server;
4743};
4744
4745static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4746 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4747 gfp_t gfp_mask)
4748{
4749 struct nfs4_lockdata *p;
4750 struct inode *inode = lsp->ls_state->inode;
4751 struct nfs_server *server = NFS_SERVER(inode);
4752
4753 p = kzalloc(sizeof(*p), gfp_mask);
4754 if (p == NULL)
4755 return NULL;
4756
4757 p->arg.fh = NFS_FH(inode);
4758 p->arg.fl = &p->fl;
4759 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4760 if (p->arg.open_seqid == NULL)
4761 goto out_free;
4762 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4763 if (p->arg.lock_seqid == NULL)
4764 goto out_free_seqid;
4765 p->arg.lock_stateid = &lsp->ls_stateid;
4766 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4767 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4768 p->arg.lock_owner.s_dev = server->s_dev;
4769 p->res.lock_seqid = p->arg.lock_seqid;
4770 p->lsp = lsp;
4771 p->server = server;
4772 atomic_inc(&lsp->ls_count);
4773 p->ctx = get_nfs_open_context(ctx);
4774 memcpy(&p->fl, fl, sizeof(p->fl));
4775 return p;
4776out_free_seqid:
4777 nfs_free_seqid(p->arg.open_seqid);
4778out_free:
4779 kfree(p);
4780 return NULL;
4781}
4782
4783static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4784{
4785 struct nfs4_lockdata *data = calldata;
4786 struct nfs4_state *state = data->lsp->ls_state;
4787
4788 dprintk("%s: begin!\n", __func__);
4789 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4790 goto out_wait;
4791 /* Do we need to do an open_to_lock_owner? */
4792 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4793 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4794 goto out_release_lock_seqid;
4795 }
4796 data->arg.open_stateid = &state->open_stateid;
4797 data->arg.new_lock_owner = 1;
4798 data->res.open_seqid = data->arg.open_seqid;
4799 } else
4800 data->arg.new_lock_owner = 0;
4801 if (!nfs4_valid_open_stateid(state)) {
4802 data->rpc_status = -EBADF;
4803 task->tk_action = NULL;
4804 goto out_release_open_seqid;
4805 }
4806 data->timestamp = jiffies;
4807 if (nfs4_setup_sequence(data->server,
4808 &data->arg.seq_args,
4809 &data->res.seq_res,
4810 task) == 0)
4811 return;
4812out_release_open_seqid:
4813 nfs_release_seqid(data->arg.open_seqid);
4814out_release_lock_seqid:
4815 nfs_release_seqid(data->arg.lock_seqid);
4816out_wait:
4817 nfs4_sequence_done(task, &data->res.seq_res);
4818 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4819}
4820
4821static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4822{
4823 struct nfs4_lockdata *data = calldata;
4824
4825 dprintk("%s: begin!\n", __func__);
4826
4827 if (!nfs4_sequence_done(task, &data->res.seq_res))
4828 return;
4829
4830 data->rpc_status = task->tk_status;
4831 if (data->arg.new_lock_owner != 0) {
4832 if (data->rpc_status == 0)
4833 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4834 else
4835 goto out;
4836 }
4837 if (data->rpc_status == 0) {
4838 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4839 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4840 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4841 }
4842out:
4843 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4844}
4845
4846static void nfs4_lock_release(void *calldata)
4847{
4848 struct nfs4_lockdata *data = calldata;
4849
4850 dprintk("%s: begin!\n", __func__);
4851 nfs_free_seqid(data->arg.open_seqid);
4852 if (data->cancelled != 0) {
4853 struct rpc_task *task;
4854 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4855 data->arg.lock_seqid);
4856 if (!IS_ERR(task))
4857 rpc_put_task_async(task);
4858 dprintk("%s: cancelling lock!\n", __func__);
4859 } else
4860 nfs_free_seqid(data->arg.lock_seqid);
4861 nfs4_put_lock_state(data->lsp);
4862 put_nfs_open_context(data->ctx);
4863 kfree(data);
4864 dprintk("%s: done!\n", __func__);
4865}
4866
4867static const struct rpc_call_ops nfs4_lock_ops = {
4868 .rpc_call_prepare = nfs4_lock_prepare,
4869 .rpc_call_done = nfs4_lock_done,
4870 .rpc_release = nfs4_lock_release,
4871};
4872
4873static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4874{
4875 switch (error) {
4876 case -NFS4ERR_ADMIN_REVOKED:
4877 case -NFS4ERR_BAD_STATEID:
4878 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4879 if (new_lock_owner != 0 ||
4880 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4881 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4882 break;
4883 case -NFS4ERR_STALE_STATEID:
4884 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4885 case -NFS4ERR_EXPIRED:
4886 nfs4_schedule_lease_recovery(server->nfs_client);
4887 };
4888}
4889
4890static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4891{
4892 struct nfs4_lockdata *data;
4893 struct rpc_task *task;
4894 struct rpc_message msg = {
4895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4896 .rpc_cred = state->owner->so_cred,
4897 };
4898 struct rpc_task_setup task_setup_data = {
4899 .rpc_client = NFS_CLIENT(state->inode),
4900 .rpc_message = &msg,
4901 .callback_ops = &nfs4_lock_ops,
4902 .workqueue = nfsiod_workqueue,
4903 .flags = RPC_TASK_ASYNC,
4904 };
4905 int ret;
4906
4907 dprintk("%s: begin!\n", __func__);
4908 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4909 fl->fl_u.nfs4_fl.owner,
4910 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4911 if (data == NULL)
4912 return -ENOMEM;
4913 if (IS_SETLKW(cmd))
4914 data->arg.block = 1;
4915 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4916 msg.rpc_argp = &data->arg;
4917 msg.rpc_resp = &data->res;
4918 task_setup_data.callback_data = data;
4919 if (recovery_type > NFS_LOCK_NEW) {
4920 if (recovery_type == NFS_LOCK_RECLAIM)
4921 data->arg.reclaim = NFS_LOCK_RECLAIM;
4922 nfs4_set_sequence_privileged(&data->arg.seq_args);
4923 }
4924 task = rpc_run_task(&task_setup_data);
4925 if (IS_ERR(task))
4926 return PTR_ERR(task);
4927 ret = nfs4_wait_for_completion_rpc_task(task);
4928 if (ret == 0) {
4929 ret = data->rpc_status;
4930 if (ret)
4931 nfs4_handle_setlk_error(data->server, data->lsp,
4932 data->arg.new_lock_owner, ret);
4933 } else
4934 data->cancelled = 1;
4935 rpc_put_task(task);
4936 dprintk("%s: done, ret = %d!\n", __func__, ret);
4937 return ret;
4938}
4939
4940static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4941{
4942 struct nfs_server *server = NFS_SERVER(state->inode);
4943 struct nfs4_exception exception = {
4944 .inode = state->inode,
4945 };
4946 int err;
4947
4948 do {
4949 /* Cache the lock if possible... */
4950 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4951 return 0;
4952 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4953 if (err != -NFS4ERR_DELAY)
4954 break;
4955 nfs4_handle_exception(server, err, &exception);
4956 } while (exception.retry);
4957 return err;
4958}
4959
4960static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4961{
4962 struct nfs_server *server = NFS_SERVER(state->inode);
4963 struct nfs4_exception exception = {
4964 .inode = state->inode,
4965 };
4966 int err;
4967
4968 err = nfs4_set_lock_state(state, request);
4969 if (err != 0)
4970 return err;
4971 do {
4972 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4973 return 0;
4974 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4975 switch (err) {
4976 default:
4977 goto out;
4978 case -NFS4ERR_GRACE:
4979 case -NFS4ERR_DELAY:
4980 nfs4_handle_exception(server, err, &exception);
4981 err = 0;
4982 }
4983 } while (exception.retry);
4984out:
4985 return err;
4986}
4987
4988#if defined(CONFIG_NFS_V4_1)
4989/**
4990 * nfs41_check_expired_locks - possibly free a lock stateid
4991 *
4992 * @state: NFSv4 state for an inode
4993 *
4994 * Returns NFS_OK if recovery for this stateid is now finished.
4995 * Otherwise a negative NFS4ERR value is returned.
4996 */
4997static int nfs41_check_expired_locks(struct nfs4_state *state)
4998{
4999 int status, ret = -NFS4ERR_BAD_STATEID;
5000 struct nfs4_lock_state *lsp;
5001 struct nfs_server *server = NFS_SERVER(state->inode);
5002
5003 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5004 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5005 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5006 if (status != NFS_OK) {
5007 /* Free the stateid unless the server
5008 * informs us the stateid is unrecognized. */
5009 if (status != -NFS4ERR_BAD_STATEID)
5010 nfs41_free_stateid(server,
5011 &lsp->ls_stateid);
5012 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5013 ret = status;
5014 }
5015 }
5016 };
5017
5018 return ret;
5019}
5020
5021static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5022{
5023 int status = NFS_OK;
5024
5025 if (test_bit(LK_STATE_IN_USE, &state->flags))
5026 status = nfs41_check_expired_locks(state);
5027 if (status != NFS_OK)
5028 status = nfs4_lock_expired(state, request);
5029 return status;
5030}
5031#endif
5032
5033static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5034{
5035 struct nfs4_state_owner *sp = state->owner;
5036 struct nfs_inode *nfsi = NFS_I(state->inode);
5037 unsigned char fl_flags = request->fl_flags;
5038 unsigned int seq;
5039 int status = -ENOLCK;
5040
5041 if ((fl_flags & FL_POSIX) &&
5042 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5043 goto out;
5044 /* Is this a delegated open? */
5045 status = nfs4_set_lock_state(state, request);
5046 if (status != 0)
5047 goto out;
5048 request->fl_flags |= FL_ACCESS;
5049 status = do_vfs_lock(request->fl_file, request);
5050 if (status < 0)
5051 goto out;
5052 down_read(&nfsi->rwsem);
5053 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5054 /* Yes: cache locks! */
5055 /* ...but avoid races with delegation recall... */
5056 request->fl_flags = fl_flags & ~FL_SLEEP;
5057 status = do_vfs_lock(request->fl_file, request);
5058 goto out_unlock;
5059 }
5060 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5061 up_read(&nfsi->rwsem);
5062 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5063 if (status != 0)
5064 goto out;
5065 down_read(&nfsi->rwsem);
5066 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5067 status = -NFS4ERR_DELAY;
5068 goto out_unlock;
5069 }
5070 /* Note: we always want to sleep here! */
5071 request->fl_flags = fl_flags | FL_SLEEP;
5072 if (do_vfs_lock(request->fl_file, request) < 0)
5073 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5074 "manager!\n", __func__);
5075out_unlock:
5076 up_read(&nfsi->rwsem);
5077out:
5078 request->fl_flags = fl_flags;
5079 return status;
5080}
5081
5082static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5083{
5084 struct nfs4_exception exception = {
5085 .state = state,
5086 .inode = state->inode,
5087 };
5088 int err;
5089
5090 do {
5091 err = _nfs4_proc_setlk(state, cmd, request);
5092 if (err == -NFS4ERR_DENIED)
5093 err = -EAGAIN;
5094 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5095 err, &exception);
5096 } while (exception.retry);
5097 return err;
5098}
5099
5100static int
5101nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5102{
5103 struct nfs_open_context *ctx;
5104 struct nfs4_state *state;
5105 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5106 int status;
5107
5108 /* verify open state */
5109 ctx = nfs_file_open_context(filp);
5110 state = ctx->state;
5111
5112 if (request->fl_start < 0 || request->fl_end < 0)
5113 return -EINVAL;
5114
5115 if (IS_GETLK(cmd)) {
5116 if (state != NULL)
5117 return nfs4_proc_getlk(state, F_GETLK, request);
5118 return 0;
5119 }
5120
5121 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5122 return -EINVAL;
5123
5124 if (request->fl_type == F_UNLCK) {
5125 if (state != NULL)
5126 return nfs4_proc_unlck(state, cmd, request);
5127 return 0;
5128 }
5129
5130 if (state == NULL)
5131 return -ENOLCK;
5132 /*
5133 * Don't rely on the VFS having checked the file open mode,
5134 * since it won't do this for flock() locks.
5135 */
5136 switch (request->fl_type) {
5137 case F_RDLCK:
5138 if (!(filp->f_mode & FMODE_READ))
5139 return -EBADF;
5140 break;
5141 case F_WRLCK:
5142 if (!(filp->f_mode & FMODE_WRITE))
5143 return -EBADF;
5144 }
5145
5146 do {
5147 status = nfs4_proc_setlk(state, cmd, request);
5148 if ((status != -EAGAIN) || IS_SETLK(cmd))
5149 break;
5150 timeout = nfs4_set_lock_task_retry(timeout);
5151 status = -ERESTARTSYS;
5152 if (signalled())
5153 break;
5154 } while(status < 0);
5155 return status;
5156}
5157
5158int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5159{
5160 struct nfs_server *server = NFS_SERVER(state->inode);
5161 int err;
5162
5163 err = nfs4_set_lock_state(state, fl);
5164 if (err != 0)
5165 return err;
5166 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5167 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5168}
5169
5170struct nfs_release_lockowner_data {
5171 struct nfs4_lock_state *lsp;
5172 struct nfs_server *server;
5173 struct nfs_release_lockowner_args args;
5174};
5175
5176static void nfs4_release_lockowner_release(void *calldata)
5177{
5178 struct nfs_release_lockowner_data *data = calldata;
5179 nfs4_free_lock_state(data->server, data->lsp);
5180 kfree(calldata);
5181}
5182
5183static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5184 .rpc_release = nfs4_release_lockowner_release,
5185};
5186
5187int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5188{
5189 struct nfs_server *server = lsp->ls_state->owner->so_server;
5190 struct nfs_release_lockowner_data *data;
5191 struct rpc_message msg = {
5192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5193 };
5194
5195 if (server->nfs_client->cl_mvops->minor_version != 0)
5196 return -EINVAL;
5197 data = kmalloc(sizeof(*data), GFP_NOFS);
5198 if (!data)
5199 return -ENOMEM;
5200 data->lsp = lsp;
5201 data->server = server;
5202 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5203 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5204 data->args.lock_owner.s_dev = server->s_dev;
5205 msg.rpc_argp = &data->args;
5206 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5207 return 0;
5208}
5209
5210#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5211
5212static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5213 const void *buf, size_t buflen,
5214 int flags, int type)
5215{
5216 if (strcmp(key, "") != 0)
5217 return -EINVAL;
5218
5219 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5220}
5221
5222static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5223 void *buf, size_t buflen, int type)
5224{
5225 if (strcmp(key, "") != 0)
5226 return -EINVAL;
5227
5228 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5229}
5230
5231static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5232 size_t list_len, const char *name,
5233 size_t name_len, int type)
5234{
5235 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5236
5237 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5238 return 0;
5239
5240 if (list && len <= list_len)
5241 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5242 return len;
5243}
5244
5245/*
5246 * nfs_fhget will use either the mounted_on_fileid or the fileid
5247 */
5248static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5249{
5250 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5251 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5252 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5253 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5254 return;
5255
5256 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5257 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5258 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5259 fattr->nlink = 2;
5260}
5261
5262static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5263 const struct qstr *name,
5264 struct nfs4_fs_locations *fs_locations,
5265 struct page *page)
5266{
5267 struct nfs_server *server = NFS_SERVER(dir);
5268 u32 bitmask[2] = {
5269 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5270 };
5271 struct nfs4_fs_locations_arg args = {
5272 .dir_fh = NFS_FH(dir),
5273 .name = name,
5274 .page = page,
5275 .bitmask = bitmask,
5276 };
5277 struct nfs4_fs_locations_res res = {
5278 .fs_locations = fs_locations,
5279 };
5280 struct rpc_message msg = {
5281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5282 .rpc_argp = &args,
5283 .rpc_resp = &res,
5284 };
5285 int status;
5286
5287 dprintk("%s: start\n", __func__);
5288
5289 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5290 * is not supported */
5291 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5292 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5293 else
5294 bitmask[0] |= FATTR4_WORD0_FILEID;
5295
5296 nfs_fattr_init(&fs_locations->fattr);
5297 fs_locations->server = server;
5298 fs_locations->nlocations = 0;
5299 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5300 dprintk("%s: returned status = %d\n", __func__, status);
5301 return status;
5302}
5303
5304int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5305 const struct qstr *name,
5306 struct nfs4_fs_locations *fs_locations,
5307 struct page *page)
5308{
5309 struct nfs4_exception exception = { };
5310 int err;
5311 do {
5312 err = nfs4_handle_exception(NFS_SERVER(dir),
5313 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5314 &exception);
5315 } while (exception.retry);
5316 return err;
5317}
5318
5319static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5320{
5321 int status;
5322 struct nfs4_secinfo_arg args = {
5323 .dir_fh = NFS_FH(dir),
5324 .name = name,
5325 };
5326 struct nfs4_secinfo_res res = {
5327 .flavors = flavors,
5328 };
5329 struct rpc_message msg = {
5330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5331 .rpc_argp = &args,
5332 .rpc_resp = &res,
5333 };
5334
5335 dprintk("NFS call secinfo %s\n", name->name);
5336 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5337 dprintk("NFS reply secinfo: %d\n", status);
5338 return status;
5339}
5340
5341int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5342 struct nfs4_secinfo_flavors *flavors)
5343{
5344 struct nfs4_exception exception = { };
5345 int err;
5346 do {
5347 err = nfs4_handle_exception(NFS_SERVER(dir),
5348 _nfs4_proc_secinfo(dir, name, flavors),
5349 &exception);
5350 } while (exception.retry);
5351 return err;
5352}
5353
5354#ifdef CONFIG_NFS_V4_1
5355/*
5356 * Check the exchange flags returned by the server for invalid flags, having
5357 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5358 * DS flags set.
5359 */
5360static int nfs4_check_cl_exchange_flags(u32 flags)
5361{
5362 if (flags & ~EXCHGID4_FLAG_MASK_R)
5363 goto out_inval;
5364 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5365 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5366 goto out_inval;
5367 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5368 goto out_inval;
5369 return NFS_OK;
5370out_inval:
5371 return -NFS4ERR_INVAL;
5372}
5373
5374static bool
5375nfs41_same_server_scope(struct nfs41_server_scope *a,
5376 struct nfs41_server_scope *b)
5377{
5378 if (a->server_scope_sz == b->server_scope_sz &&
5379 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5380 return true;
5381
5382 return false;
5383}
5384
5385/*
5386 * nfs4_proc_bind_conn_to_session()
5387 *
5388 * The 4.1 client currently uses the same TCP connection for the
5389 * fore and backchannel.
5390 */
5391int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5392{
5393 int status;
5394 struct nfs41_bind_conn_to_session_res res;
5395 struct rpc_message msg = {
5396 .rpc_proc =
5397 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5398 .rpc_argp = clp,
5399 .rpc_resp = &res,
5400 .rpc_cred = cred,
5401 };
5402
5403 dprintk("--> %s\n", __func__);
5404
5405 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5406 if (unlikely(res.session == NULL)) {
5407 status = -ENOMEM;
5408 goto out;
5409 }
5410
5411 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5412 if (status == 0) {
5413 if (memcmp(res.session->sess_id.data,
5414 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5415 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5416 status = -EIO;
5417 goto out_session;
5418 }
5419 if (res.dir != NFS4_CDFS4_BOTH) {
5420 dprintk("NFS: %s: Unexpected direction from server\n",
5421 __func__);
5422 status = -EIO;
5423 goto out_session;
5424 }
5425 if (res.use_conn_in_rdma_mode) {
5426 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5427 __func__);
5428 status = -EIO;
5429 goto out_session;
5430 }
5431 }
5432out_session:
5433 kfree(res.session);
5434out:
5435 dprintk("<-- %s status= %d\n", __func__, status);
5436 return status;
5437}
5438
5439/*
5440 * nfs4_proc_exchange_id()
5441 *
5442 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5443 *
5444 * Since the clientid has expired, all compounds using sessions
5445 * associated with the stale clientid will be returning
5446 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5447 * be in some phase of session reset.
5448 */
5449int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5450{
5451 nfs4_verifier verifier;
5452 struct nfs41_exchange_id_args args = {
5453 .verifier = &verifier,
5454 .client = clp,
5455 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5456 };
5457 struct nfs41_exchange_id_res res = {
5458 0
5459 };
5460 int status;
5461 struct rpc_message msg = {
5462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5463 .rpc_argp = &args,
5464 .rpc_resp = &res,
5465 .rpc_cred = cred,
5466 };
5467
5468 nfs4_init_boot_verifier(clp, &verifier);
5469 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5470 sizeof(args.id));
5471 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5472 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5473 args.id_len, args.id);
5474
5475 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5476 GFP_NOFS);
5477 if (unlikely(res.server_owner == NULL)) {
5478 status = -ENOMEM;
5479 goto out;
5480 }
5481
5482 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5483 GFP_NOFS);
5484 if (unlikely(res.server_scope == NULL)) {
5485 status = -ENOMEM;
5486 goto out_server_owner;
5487 }
5488
5489 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5490 if (unlikely(res.impl_id == NULL)) {
5491 status = -ENOMEM;
5492 goto out_server_scope;
5493 }
5494
5495 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5496 if (status == 0)
5497 status = nfs4_check_cl_exchange_flags(res.flags);
5498
5499 if (status == 0) {
5500 clp->cl_clientid = res.clientid;
5501 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5502 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5503 clp->cl_seqid = res.seqid;
5504
5505 kfree(clp->cl_serverowner);
5506 clp->cl_serverowner = res.server_owner;
5507 res.server_owner = NULL;
5508
5509 /* use the most recent implementation id */
5510 kfree(clp->cl_implid);
5511 clp->cl_implid = res.impl_id;
5512
5513 if (clp->cl_serverscope != NULL &&
5514 !nfs41_same_server_scope(clp->cl_serverscope,
5515 res.server_scope)) {
5516 dprintk("%s: server_scope mismatch detected\n",
5517 __func__);
5518 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5519 kfree(clp->cl_serverscope);
5520 clp->cl_serverscope = NULL;
5521 }
5522
5523 if (clp->cl_serverscope == NULL) {
5524 clp->cl_serverscope = res.server_scope;
5525 goto out;
5526 }
5527 } else
5528 kfree(res.impl_id);
5529
5530out_server_owner:
5531 kfree(res.server_owner);
5532out_server_scope:
5533 kfree(res.server_scope);
5534out:
5535 if (clp->cl_implid != NULL)
5536 dprintk("NFS reply exchange_id: Server Implementation ID: "
5537 "domain: %s, name: %s, date: %llu,%u\n",
5538 clp->cl_implid->domain, clp->cl_implid->name,
5539 clp->cl_implid->date.seconds,
5540 clp->cl_implid->date.nseconds);
5541 dprintk("NFS reply exchange_id: %d\n", status);
5542 return status;
5543}
5544
5545static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5546 struct rpc_cred *cred)
5547{
5548 struct rpc_message msg = {
5549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5550 .rpc_argp = clp,
5551 .rpc_cred = cred,
5552 };
5553 int status;
5554
5555 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5556 if (status)
5557 dprintk("NFS: Got error %d from the server %s on "
5558 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5559 return status;
5560}
5561
5562static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5563 struct rpc_cred *cred)
5564{
5565 unsigned int loop;
5566 int ret;
5567
5568 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5569 ret = _nfs4_proc_destroy_clientid(clp, cred);
5570 switch (ret) {
5571 case -NFS4ERR_DELAY:
5572 case -NFS4ERR_CLIENTID_BUSY:
5573 ssleep(1);
5574 break;
5575 default:
5576 return ret;
5577 }
5578 }
5579 return 0;
5580}
5581
5582int nfs4_destroy_clientid(struct nfs_client *clp)
5583{
5584 struct rpc_cred *cred;
5585 int ret = 0;
5586
5587 if (clp->cl_mvops->minor_version < 1)
5588 goto out;
5589 if (clp->cl_exchange_flags == 0)
5590 goto out;
5591 if (clp->cl_preserve_clid)
5592 goto out;
5593 cred = nfs4_get_exchange_id_cred(clp);
5594 ret = nfs4_proc_destroy_clientid(clp, cred);
5595 if (cred)
5596 put_rpccred(cred);
5597 switch (ret) {
5598 case 0:
5599 case -NFS4ERR_STALE_CLIENTID:
5600 clp->cl_exchange_flags = 0;
5601 }
5602out:
5603 return ret;
5604}
5605
5606struct nfs4_get_lease_time_data {
5607 struct nfs4_get_lease_time_args *args;
5608 struct nfs4_get_lease_time_res *res;
5609 struct nfs_client *clp;
5610};
5611
5612static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5613 void *calldata)
5614{
5615 struct nfs4_get_lease_time_data *data =
5616 (struct nfs4_get_lease_time_data *)calldata;
5617
5618 dprintk("--> %s\n", __func__);
5619 /* just setup sequence, do not trigger session recovery
5620 since we're invoked within one */
5621 nfs41_setup_sequence(data->clp->cl_session,
5622 &data->args->la_seq_args,
5623 &data->res->lr_seq_res,
5624 task);
5625 dprintk("<-- %s\n", __func__);
5626}
5627
5628/*
5629 * Called from nfs4_state_manager thread for session setup, so don't recover
5630 * from sequence operation or clientid errors.
5631 */
5632static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5633{
5634 struct nfs4_get_lease_time_data *data =
5635 (struct nfs4_get_lease_time_data *)calldata;
5636
5637 dprintk("--> %s\n", __func__);
5638 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5639 return;
5640 switch (task->tk_status) {
5641 case -NFS4ERR_DELAY:
5642 case -NFS4ERR_GRACE:
5643 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5644 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5645 task->tk_status = 0;
5646 /* fall through */
5647 case -NFS4ERR_RETRY_UNCACHED_REP:
5648 rpc_restart_call_prepare(task);
5649 return;
5650 }
5651 dprintk("<-- %s\n", __func__);
5652}
5653
5654static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5655 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5656 .rpc_call_done = nfs4_get_lease_time_done,
5657};
5658
5659int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5660{
5661 struct rpc_task *task;
5662 struct nfs4_get_lease_time_args args;
5663 struct nfs4_get_lease_time_res res = {
5664 .lr_fsinfo = fsinfo,
5665 };
5666 struct nfs4_get_lease_time_data data = {
5667 .args = &args,
5668 .res = &res,
5669 .clp = clp,
5670 };
5671 struct rpc_message msg = {
5672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5673 .rpc_argp = &args,
5674 .rpc_resp = &res,
5675 };
5676 struct rpc_task_setup task_setup = {
5677 .rpc_client = clp->cl_rpcclient,
5678 .rpc_message = &msg,
5679 .callback_ops = &nfs4_get_lease_time_ops,
5680 .callback_data = &data,
5681 .flags = RPC_TASK_TIMEOUT,
5682 };
5683 int status;
5684
5685 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5686 nfs4_set_sequence_privileged(&args.la_seq_args);
5687 dprintk("--> %s\n", __func__);
5688 task = rpc_run_task(&task_setup);
5689
5690 if (IS_ERR(task))
5691 status = PTR_ERR(task);
5692 else {
5693 status = task->tk_status;
5694 rpc_put_task(task);
5695 }
5696 dprintk("<-- %s return %d\n", __func__, status);
5697
5698 return status;
5699}
5700
5701/*
5702 * Initialize the values to be used by the client in CREATE_SESSION
5703 * If nfs4_init_session set the fore channel request and response sizes,
5704 * use them.
5705 *
5706 * Set the back channel max_resp_sz_cached to zero to force the client to
5707 * always set csa_cachethis to FALSE because the current implementation
5708 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5709 */
5710static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5711{
5712 struct nfs4_session *session = args->client->cl_session;
5713 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5714 mxresp_sz = session->fc_target_max_resp_sz;
5715
5716 if (mxrqst_sz == 0)
5717 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5718 if (mxresp_sz == 0)
5719 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5720 /* Fore channel attributes */
5721 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5722 args->fc_attrs.max_resp_sz = mxresp_sz;
5723 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5724 args->fc_attrs.max_reqs = max_session_slots;
5725
5726 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5727 "max_ops=%u max_reqs=%u\n",
5728 __func__,
5729 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5730 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5731
5732 /* Back channel attributes */
5733 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5734 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5735 args->bc_attrs.max_resp_sz_cached = 0;
5736 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5737 args->bc_attrs.max_reqs = 1;
5738
5739 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5740 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5741 __func__,
5742 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5743 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5744 args->bc_attrs.max_reqs);
5745}
5746
5747static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5748{
5749 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5750 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5751
5752 if (rcvd->max_resp_sz > sent->max_resp_sz)
5753 return -EINVAL;
5754 /*
5755 * Our requested max_ops is the minimum we need; we're not
5756 * prepared to break up compounds into smaller pieces than that.
5757 * So, no point even trying to continue if the server won't
5758 * cooperate:
5759 */
5760 if (rcvd->max_ops < sent->max_ops)
5761 return -EINVAL;
5762 if (rcvd->max_reqs == 0)
5763 return -EINVAL;
5764 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5765 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5766 return 0;
5767}
5768
5769static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5770{
5771 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5772 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5773
5774 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5775 return -EINVAL;
5776 if (rcvd->max_resp_sz < sent->max_resp_sz)
5777 return -EINVAL;
5778 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5779 return -EINVAL;
5780 /* These would render the backchannel useless: */
5781 if (rcvd->max_ops != sent->max_ops)
5782 return -EINVAL;
5783 if (rcvd->max_reqs != sent->max_reqs)
5784 return -EINVAL;
5785 return 0;
5786}
5787
5788static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5789 struct nfs4_session *session)
5790{
5791 int ret;
5792
5793 ret = nfs4_verify_fore_channel_attrs(args, session);
5794 if (ret)
5795 return ret;
5796 return nfs4_verify_back_channel_attrs(args, session);
5797}
5798
5799static int _nfs4_proc_create_session(struct nfs_client *clp,
5800 struct rpc_cred *cred)
5801{
5802 struct nfs4_session *session = clp->cl_session;
5803 struct nfs41_create_session_args args = {
5804 .client = clp,
5805 .cb_program = NFS4_CALLBACK,
5806 };
5807 struct nfs41_create_session_res res = {
5808 .client = clp,
5809 };
5810 struct rpc_message msg = {
5811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5812 .rpc_argp = &args,
5813 .rpc_resp = &res,
5814 .rpc_cred = cred,
5815 };
5816 int status;
5817
5818 nfs4_init_channel_attrs(&args);
5819 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5820
5821 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5822
5823 if (!status) {
5824 /* Verify the session's negotiated channel_attrs values */
5825 status = nfs4_verify_channel_attrs(&args, session);
5826 /* Increment the clientid slot sequence id */
5827 clp->cl_seqid++;
5828 }
5829
5830 return status;
5831}
5832
5833/*
5834 * Issues a CREATE_SESSION operation to the server.
5835 * It is the responsibility of the caller to verify the session is
5836 * expired before calling this routine.
5837 */
5838int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5839{
5840 int status;
5841 unsigned *ptr;
5842 struct nfs4_session *session = clp->cl_session;
5843
5844 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5845
5846 status = _nfs4_proc_create_session(clp, cred);
5847 if (status)
5848 goto out;
5849
5850 /* Init or reset the session slot tables */
5851 status = nfs4_setup_session_slot_tables(session);
5852 dprintk("slot table setup returned %d\n", status);
5853 if (status)
5854 goto out;
5855
5856 ptr = (unsigned *)&session->sess_id.data[0];
5857 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5858 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5859out:
5860 dprintk("<-- %s\n", __func__);
5861 return status;
5862}
5863
5864/*
5865 * Issue the over-the-wire RPC DESTROY_SESSION.
5866 * The caller must serialize access to this routine.
5867 */
5868int nfs4_proc_destroy_session(struct nfs4_session *session,
5869 struct rpc_cred *cred)
5870{
5871 struct rpc_message msg = {
5872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5873 .rpc_argp = session,
5874 .rpc_cred = cred,
5875 };
5876 int status = 0;
5877
5878 dprintk("--> nfs4_proc_destroy_session\n");
5879
5880 /* session is still being setup */
5881 if (session->clp->cl_cons_state != NFS_CS_READY)
5882 return status;
5883
5884 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5885
5886 if (status)
5887 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5888 "Session has been destroyed regardless...\n", status);
5889
5890 dprintk("<-- nfs4_proc_destroy_session\n");
5891 return status;
5892}
5893
5894/*
5895 * Renew the cl_session lease.
5896 */
5897struct nfs4_sequence_data {
5898 struct nfs_client *clp;
5899 struct nfs4_sequence_args args;
5900 struct nfs4_sequence_res res;
5901};
5902
5903static void nfs41_sequence_release(void *data)
5904{
5905 struct nfs4_sequence_data *calldata = data;
5906 struct nfs_client *clp = calldata->clp;
5907
5908 if (atomic_read(&clp->cl_count) > 1)
5909 nfs4_schedule_state_renewal(clp);
5910 nfs_put_client(clp);
5911 kfree(calldata);
5912}
5913
5914static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5915{
5916 switch(task->tk_status) {
5917 case -NFS4ERR_DELAY:
5918 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5919 return -EAGAIN;
5920 default:
5921 nfs4_schedule_lease_recovery(clp);
5922 }
5923 return 0;
5924}
5925
5926static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5927{
5928 struct nfs4_sequence_data *calldata = data;
5929 struct nfs_client *clp = calldata->clp;
5930
5931 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5932 return;
5933
5934 if (task->tk_status < 0) {
5935 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5936 if (atomic_read(&clp->cl_count) == 1)
5937 goto out;
5938
5939 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5940 rpc_restart_call_prepare(task);
5941 return;
5942 }
5943 }
5944 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5945out:
5946 dprintk("<-- %s\n", __func__);
5947}
5948
5949static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5950{
5951 struct nfs4_sequence_data *calldata = data;
5952 struct nfs_client *clp = calldata->clp;
5953 struct nfs4_sequence_args *args;
5954 struct nfs4_sequence_res *res;
5955
5956 args = task->tk_msg.rpc_argp;
5957 res = task->tk_msg.rpc_resp;
5958
5959 nfs41_setup_sequence(clp->cl_session, args, res, task);
5960}
5961
5962static const struct rpc_call_ops nfs41_sequence_ops = {
5963 .rpc_call_done = nfs41_sequence_call_done,
5964 .rpc_call_prepare = nfs41_sequence_prepare,
5965 .rpc_release = nfs41_sequence_release,
5966};
5967
5968static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5969 struct rpc_cred *cred,
5970 bool is_privileged)
5971{
5972 struct nfs4_sequence_data *calldata;
5973 struct rpc_message msg = {
5974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5975 .rpc_cred = cred,
5976 };
5977 struct rpc_task_setup task_setup_data = {
5978 .rpc_client = clp->cl_rpcclient,
5979 .rpc_message = &msg,
5980 .callback_ops = &nfs41_sequence_ops,
5981 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
5982 };
5983
5984 if (!atomic_inc_not_zero(&clp->cl_count))
5985 return ERR_PTR(-EIO);
5986 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5987 if (calldata == NULL) {
5988 nfs_put_client(clp);
5989 return ERR_PTR(-ENOMEM);
5990 }
5991 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5992 if (is_privileged)
5993 nfs4_set_sequence_privileged(&calldata->args);
5994 msg.rpc_argp = &calldata->args;
5995 msg.rpc_resp = &calldata->res;
5996 calldata->clp = clp;
5997 task_setup_data.callback_data = calldata;
5998
5999 return rpc_run_task(&task_setup_data);
6000}
6001
6002static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6003{
6004 struct rpc_task *task;
6005 int ret = 0;
6006
6007 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6008 return 0;
6009 task = _nfs41_proc_sequence(clp, cred, false);
6010 if (IS_ERR(task))
6011 ret = PTR_ERR(task);
6012 else
6013 rpc_put_task_async(task);
6014 dprintk("<-- %s status=%d\n", __func__, ret);
6015 return ret;
6016}
6017
6018static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6019{
6020 struct rpc_task *task;
6021 int ret;
6022
6023 task = _nfs41_proc_sequence(clp, cred, true);
6024 if (IS_ERR(task)) {
6025 ret = PTR_ERR(task);
6026 goto out;
6027 }
6028 ret = rpc_wait_for_completion_task(task);
6029 if (!ret) {
6030 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6031
6032 if (task->tk_status == 0)
6033 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6034 ret = task->tk_status;
6035 }
6036 rpc_put_task(task);
6037out:
6038 dprintk("<-- %s status=%d\n", __func__, ret);
6039 return ret;
6040}
6041
6042struct nfs4_reclaim_complete_data {
6043 struct nfs_client *clp;
6044 struct nfs41_reclaim_complete_args arg;
6045 struct nfs41_reclaim_complete_res res;
6046};
6047
6048static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6049{
6050 struct nfs4_reclaim_complete_data *calldata = data;
6051
6052 nfs41_setup_sequence(calldata->clp->cl_session,
6053 &calldata->arg.seq_args,
6054 &calldata->res.seq_res,
6055 task);
6056}
6057
6058static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6059{
6060 switch(task->tk_status) {
6061 case 0:
6062 case -NFS4ERR_COMPLETE_ALREADY:
6063 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6064 break;
6065 case -NFS4ERR_DELAY:
6066 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6067 /* fall through */
6068 case -NFS4ERR_RETRY_UNCACHED_REP:
6069 return -EAGAIN;
6070 default:
6071 nfs4_schedule_lease_recovery(clp);
6072 }
6073 return 0;
6074}
6075
6076static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6077{
6078 struct nfs4_reclaim_complete_data *calldata = data;
6079 struct nfs_client *clp = calldata->clp;
6080 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6081
6082 dprintk("--> %s\n", __func__);
6083 if (!nfs41_sequence_done(task, res))
6084 return;
6085
6086 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6087 rpc_restart_call_prepare(task);
6088 return;
6089 }
6090 dprintk("<-- %s\n", __func__);
6091}
6092
6093static void nfs4_free_reclaim_complete_data(void *data)
6094{
6095 struct nfs4_reclaim_complete_data *calldata = data;
6096
6097 kfree(calldata);
6098}
6099
6100static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6101 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6102 .rpc_call_done = nfs4_reclaim_complete_done,
6103 .rpc_release = nfs4_free_reclaim_complete_data,
6104};
6105
6106/*
6107 * Issue a global reclaim complete.
6108 */
6109static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6110{
6111 struct nfs4_reclaim_complete_data *calldata;
6112 struct rpc_task *task;
6113 struct rpc_message msg = {
6114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6115 };
6116 struct rpc_task_setup task_setup_data = {
6117 .rpc_client = clp->cl_rpcclient,
6118 .rpc_message = &msg,
6119 .callback_ops = &nfs4_reclaim_complete_call_ops,
6120 .flags = RPC_TASK_ASYNC,
6121 };
6122 int status = -ENOMEM;
6123
6124 dprintk("--> %s\n", __func__);
6125 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6126 if (calldata == NULL)
6127 goto out;
6128 calldata->clp = clp;
6129 calldata->arg.one_fs = 0;
6130
6131 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6132 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
6133 msg.rpc_argp = &calldata->arg;
6134 msg.rpc_resp = &calldata->res;
6135 task_setup_data.callback_data = calldata;
6136 task = rpc_run_task(&task_setup_data);
6137 if (IS_ERR(task)) {
6138 status = PTR_ERR(task);
6139 goto out;
6140 }
6141 status = nfs4_wait_for_completion_rpc_task(task);
6142 if (status == 0)
6143 status = task->tk_status;
6144 rpc_put_task(task);
6145 return 0;
6146out:
6147 dprintk("<-- %s status=%d\n", __func__, status);
6148 return status;
6149}
6150
6151static void
6152nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6153{
6154 struct nfs4_layoutget *lgp = calldata;
6155 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6156 struct nfs4_session *session = nfs4_get_session(server);
6157
6158 dprintk("--> %s\n", __func__);
6159 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6160 * right now covering the LAYOUTGET we are about to send.
6161 * However, that is not so catastrophic, and there seems
6162 * to be no way to prevent it completely.
6163 */
6164 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
6165 &lgp->res.seq_res, task))
6166 return;
6167 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6168 NFS_I(lgp->args.inode)->layout,
6169 lgp->args.ctx->state)) {
6170 rpc_exit(task, NFS4_OK);
6171 }
6172}
6173
6174static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6175{
6176 struct nfs4_layoutget *lgp = calldata;
6177 struct inode *inode = lgp->args.inode;
6178 struct nfs_server *server = NFS_SERVER(inode);
6179 struct pnfs_layout_hdr *lo;
6180 struct nfs4_state *state = NULL;
6181 unsigned long timeo, giveup;
6182
6183 dprintk("--> %s\n", __func__);
6184
6185 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6186 goto out;
6187
6188 switch (task->tk_status) {
6189 case 0:
6190 goto out;
6191 case -NFS4ERR_LAYOUTTRYLATER:
6192 case -NFS4ERR_RECALLCONFLICT:
6193 timeo = rpc_get_timeout(task->tk_client);
6194 giveup = lgp->args.timestamp + timeo;
6195 if (time_after(giveup, jiffies))
6196 task->tk_status = -NFS4ERR_DELAY;
6197 break;
6198 case -NFS4ERR_EXPIRED:
6199 case -NFS4ERR_BAD_STATEID:
6200 spin_lock(&inode->i_lock);
6201 lo = NFS_I(inode)->layout;
6202 if (!lo || list_empty(&lo->plh_segs)) {
6203 spin_unlock(&inode->i_lock);
6204 /* If the open stateid was bad, then recover it. */
6205 state = lgp->args.ctx->state;
6206 } else {
6207 LIST_HEAD(head);
6208
6209 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6210 spin_unlock(&inode->i_lock);
6211 /* Mark the bad layout state as invalid, then
6212 * retry using the open stateid. */
6213 pnfs_free_lseg_list(&head);
6214 }
6215 }
6216 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6217 rpc_restart_call_prepare(task);
6218out:
6219 dprintk("<-- %s\n", __func__);
6220}
6221
6222static size_t max_response_pages(struct nfs_server *server)
6223{
6224 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6225 return nfs_page_array_len(0, max_resp_sz);
6226}
6227
6228static void nfs4_free_pages(struct page **pages, size_t size)
6229{
6230 int i;
6231
6232 if (!pages)
6233 return;
6234
6235 for (i = 0; i < size; i++) {
6236 if (!pages[i])
6237 break;
6238 __free_page(pages[i]);
6239 }
6240 kfree(pages);
6241}
6242
6243static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6244{
6245 struct page **pages;
6246 int i;
6247
6248 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6249 if (!pages) {
6250 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6251 return NULL;
6252 }
6253
6254 for (i = 0; i < size; i++) {
6255 pages[i] = alloc_page(gfp_flags);
6256 if (!pages[i]) {
6257 dprintk("%s: failed to allocate page\n", __func__);
6258 nfs4_free_pages(pages, size);
6259 return NULL;
6260 }
6261 }
6262
6263 return pages;
6264}
6265
6266static void nfs4_layoutget_release(void *calldata)
6267{
6268 struct nfs4_layoutget *lgp = calldata;
6269 struct inode *inode = lgp->args.inode;
6270 struct nfs_server *server = NFS_SERVER(inode);
6271 size_t max_pages = max_response_pages(server);
6272
6273 dprintk("--> %s\n", __func__);
6274 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6275 pnfs_put_layout_hdr(NFS_I(inode)->layout);
6276 put_nfs_open_context(lgp->args.ctx);
6277 kfree(calldata);
6278 dprintk("<-- %s\n", __func__);
6279}
6280
6281static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6282 .rpc_call_prepare = nfs4_layoutget_prepare,
6283 .rpc_call_done = nfs4_layoutget_done,
6284 .rpc_release = nfs4_layoutget_release,
6285};
6286
6287struct pnfs_layout_segment *
6288nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6289{
6290 struct inode *inode = lgp->args.inode;
6291 struct nfs_server *server = NFS_SERVER(inode);
6292 size_t max_pages = max_response_pages(server);
6293 struct rpc_task *task;
6294 struct rpc_message msg = {
6295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6296 .rpc_argp = &lgp->args,
6297 .rpc_resp = &lgp->res,
6298 };
6299 struct rpc_task_setup task_setup_data = {
6300 .rpc_client = server->client,
6301 .rpc_message = &msg,
6302 .callback_ops = &nfs4_layoutget_call_ops,
6303 .callback_data = lgp,
6304 .flags = RPC_TASK_ASYNC,
6305 };
6306 struct pnfs_layout_segment *lseg = NULL;
6307 int status = 0;
6308
6309 dprintk("--> %s\n", __func__);
6310
6311 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6312 if (!lgp->args.layout.pages) {
6313 nfs4_layoutget_release(lgp);
6314 return ERR_PTR(-ENOMEM);
6315 }
6316 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6317 lgp->args.timestamp = jiffies;
6318
6319 lgp->res.layoutp = &lgp->args.layout;
6320 lgp->res.seq_res.sr_slot = NULL;
6321 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6322
6323 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6324 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6325
6326 task = rpc_run_task(&task_setup_data);
6327 if (IS_ERR(task))
6328 return ERR_CAST(task);
6329 status = nfs4_wait_for_completion_rpc_task(task);
6330 if (status == 0)
6331 status = task->tk_status;
6332 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6333 if (status == 0 && lgp->res.layoutp->len)
6334 lseg = pnfs_layout_process(lgp);
6335 rpc_put_task(task);
6336 dprintk("<-- %s status=%d\n", __func__, status);
6337 if (status)
6338 return ERR_PTR(status);
6339 return lseg;
6340}
6341
6342static void
6343nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6344{
6345 struct nfs4_layoutreturn *lrp = calldata;
6346
6347 dprintk("--> %s\n", __func__);
6348 nfs41_setup_sequence(lrp->clp->cl_session,
6349 &lrp->args.seq_args,
6350 &lrp->res.seq_res,
6351 task);
6352}
6353
6354static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6355{
6356 struct nfs4_layoutreturn *lrp = calldata;
6357 struct nfs_server *server;
6358
6359 dprintk("--> %s\n", __func__);
6360
6361 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6362 return;
6363
6364 server = NFS_SERVER(lrp->args.inode);
6365 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6366 rpc_restart_call_prepare(task);
6367 return;
6368 }
6369 dprintk("<-- %s\n", __func__);
6370}
6371
6372static void nfs4_layoutreturn_release(void *calldata)
6373{
6374 struct nfs4_layoutreturn *lrp = calldata;
6375 struct pnfs_layout_hdr *lo = lrp->args.layout;
6376
6377 dprintk("--> %s\n", __func__);
6378 spin_lock(&lo->plh_inode->i_lock);
6379 if (lrp->res.lrs_present)
6380 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6381 lo->plh_block_lgets--;
6382 spin_unlock(&lo->plh_inode->i_lock);
6383 pnfs_put_layout_hdr(lrp->args.layout);
6384 kfree(calldata);
6385 dprintk("<-- %s\n", __func__);
6386}
6387
6388static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6389 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6390 .rpc_call_done = nfs4_layoutreturn_done,
6391 .rpc_release = nfs4_layoutreturn_release,
6392};
6393
6394int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6395{
6396 struct rpc_task *task;
6397 struct rpc_message msg = {
6398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6399 .rpc_argp = &lrp->args,
6400 .rpc_resp = &lrp->res,
6401 };
6402 struct rpc_task_setup task_setup_data = {
6403 .rpc_client = lrp->clp->cl_rpcclient,
6404 .rpc_message = &msg,
6405 .callback_ops = &nfs4_layoutreturn_call_ops,
6406 .callback_data = lrp,
6407 };
6408 int status;
6409
6410 dprintk("--> %s\n", __func__);
6411 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6412 task = rpc_run_task(&task_setup_data);
6413 if (IS_ERR(task))
6414 return PTR_ERR(task);
6415 status = task->tk_status;
6416 dprintk("<-- %s status=%d\n", __func__, status);
6417 rpc_put_task(task);
6418 return status;
6419}
6420
6421/*
6422 * Retrieve the list of Data Server devices from the MDS.
6423 */
6424static int _nfs4_getdevicelist(struct nfs_server *server,
6425 const struct nfs_fh *fh,
6426 struct pnfs_devicelist *devlist)
6427{
6428 struct nfs4_getdevicelist_args args = {
6429 .fh = fh,
6430 .layoutclass = server->pnfs_curr_ld->id,
6431 };
6432 struct nfs4_getdevicelist_res res = {
6433 .devlist = devlist,
6434 };
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6437 .rpc_argp = &args,
6438 .rpc_resp = &res,
6439 };
6440 int status;
6441
6442 dprintk("--> %s\n", __func__);
6443 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6444 &res.seq_res, 0);
6445 dprintk("<-- %s status=%d\n", __func__, status);
6446 return status;
6447}
6448
6449int nfs4_proc_getdevicelist(struct nfs_server *server,
6450 const struct nfs_fh *fh,
6451 struct pnfs_devicelist *devlist)
6452{
6453 struct nfs4_exception exception = { };
6454 int err;
6455
6456 do {
6457 err = nfs4_handle_exception(server,
6458 _nfs4_getdevicelist(server, fh, devlist),
6459 &exception);
6460 } while (exception.retry);
6461
6462 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6463 err, devlist->num_devs);
6464
6465 return err;
6466}
6467EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6468
6469static int
6470_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6471{
6472 struct nfs4_getdeviceinfo_args args = {
6473 .pdev = pdev,
6474 };
6475 struct nfs4_getdeviceinfo_res res = {
6476 .pdev = pdev,
6477 };
6478 struct rpc_message msg = {
6479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6480 .rpc_argp = &args,
6481 .rpc_resp = &res,
6482 };
6483 int status;
6484
6485 dprintk("--> %s\n", __func__);
6486 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6487 dprintk("<-- %s status=%d\n", __func__, status);
6488
6489 return status;
6490}
6491
6492int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6493{
6494 struct nfs4_exception exception = { };
6495 int err;
6496
6497 do {
6498 err = nfs4_handle_exception(server,
6499 _nfs4_proc_getdeviceinfo(server, pdev),
6500 &exception);
6501 } while (exception.retry);
6502 return err;
6503}
6504EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6505
6506static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6507{
6508 struct nfs4_layoutcommit_data *data = calldata;
6509 struct nfs_server *server = NFS_SERVER(data->args.inode);
6510 struct nfs4_session *session = nfs4_get_session(server);
6511
6512 nfs41_setup_sequence(session,
6513 &data->args.seq_args,
6514 &data->res.seq_res,
6515 task);
6516}
6517
6518static void
6519nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6520{
6521 struct nfs4_layoutcommit_data *data = calldata;
6522 struct nfs_server *server = NFS_SERVER(data->args.inode);
6523
6524 if (!nfs41_sequence_done(task, &data->res.seq_res))
6525 return;
6526
6527 switch (task->tk_status) { /* Just ignore these failures */
6528 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6529 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6530 case -NFS4ERR_BADLAYOUT: /* no layout */
6531 case -NFS4ERR_GRACE: /* loca_recalim always false */
6532 task->tk_status = 0;
6533 break;
6534 case 0:
6535 nfs_post_op_update_inode_force_wcc(data->args.inode,
6536 data->res.fattr);
6537 break;
6538 default:
6539 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6540 rpc_restart_call_prepare(task);
6541 return;
6542 }
6543 }
6544}
6545
6546static void nfs4_layoutcommit_release(void *calldata)
6547{
6548 struct nfs4_layoutcommit_data *data = calldata;
6549
6550 pnfs_cleanup_layoutcommit(data);
6551 put_rpccred(data->cred);
6552 kfree(data);
6553}
6554
6555static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6556 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6557 .rpc_call_done = nfs4_layoutcommit_done,
6558 .rpc_release = nfs4_layoutcommit_release,
6559};
6560
6561int
6562nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6563{
6564 struct rpc_message msg = {
6565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6566 .rpc_argp = &data->args,
6567 .rpc_resp = &data->res,
6568 .rpc_cred = data->cred,
6569 };
6570 struct rpc_task_setup task_setup_data = {
6571 .task = &data->task,
6572 .rpc_client = NFS_CLIENT(data->args.inode),
6573 .rpc_message = &msg,
6574 .callback_ops = &nfs4_layoutcommit_ops,
6575 .callback_data = data,
6576 .flags = RPC_TASK_ASYNC,
6577 };
6578 struct rpc_task *task;
6579 int status = 0;
6580
6581 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6582 "lbw: %llu inode %lu\n",
6583 data->task.tk_pid, sync,
6584 data->args.lastbytewritten,
6585 data->args.inode->i_ino);
6586
6587 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6588 task = rpc_run_task(&task_setup_data);
6589 if (IS_ERR(task))
6590 return PTR_ERR(task);
6591 if (sync == false)
6592 goto out;
6593 status = nfs4_wait_for_completion_rpc_task(task);
6594 if (status != 0)
6595 goto out;
6596 status = task->tk_status;
6597out:
6598 dprintk("%s: status %d\n", __func__, status);
6599 rpc_put_task(task);
6600 return status;
6601}
6602
6603static int
6604_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6605 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6606{
6607 struct nfs41_secinfo_no_name_args args = {
6608 .style = SECINFO_STYLE_CURRENT_FH,
6609 };
6610 struct nfs4_secinfo_res res = {
6611 .flavors = flavors,
6612 };
6613 struct rpc_message msg = {
6614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6615 .rpc_argp = &args,
6616 .rpc_resp = &res,
6617 };
6618 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6619}
6620
6621static int
6622nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6623 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6624{
6625 struct nfs4_exception exception = { };
6626 int err;
6627 do {
6628 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6629 switch (err) {
6630 case 0:
6631 case -NFS4ERR_WRONGSEC:
6632 case -NFS4ERR_NOTSUPP:
6633 goto out;
6634 default:
6635 err = nfs4_handle_exception(server, err, &exception);
6636 }
6637 } while (exception.retry);
6638out:
6639 return err;
6640}
6641
6642static int
6643nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6644 struct nfs_fsinfo *info)
6645{
6646 int err;
6647 struct page *page;
6648 rpc_authflavor_t flavor;
6649 struct nfs4_secinfo_flavors *flavors;
6650
6651 page = alloc_page(GFP_KERNEL);
6652 if (!page) {
6653 err = -ENOMEM;
6654 goto out;
6655 }
6656
6657 flavors = page_address(page);
6658 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6659
6660 /*
6661 * Fall back on "guess and check" method if
6662 * the server doesn't support SECINFO_NO_NAME
6663 */
6664 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6665 err = nfs4_find_root_sec(server, fhandle, info);
6666 goto out_freepage;
6667 }
6668 if (err)
6669 goto out_freepage;
6670
6671 flavor = nfs_find_best_sec(flavors);
6672 if (err == 0)
6673 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6674
6675out_freepage:
6676 put_page(page);
6677 if (err == -EACCES)
6678 return -EPERM;
6679out:
6680 return err;
6681}
6682
6683static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6684{
6685 int status;
6686 struct nfs41_test_stateid_args args = {
6687 .stateid = stateid,
6688 };
6689 struct nfs41_test_stateid_res res;
6690 struct rpc_message msg = {
6691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6692 .rpc_argp = &args,
6693 .rpc_resp = &res,
6694 };
6695
6696 dprintk("NFS call test_stateid %p\n", stateid);
6697 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6698 nfs4_set_sequence_privileged(&args.seq_args);
6699 status = nfs4_call_sync_sequence(server->client, server, &msg,
6700 &args.seq_args, &res.seq_res);
6701 if (status != NFS_OK) {
6702 dprintk("NFS reply test_stateid: failed, %d\n", status);
6703 return status;
6704 }
6705 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6706 return -res.status;
6707}
6708
6709/**
6710 * nfs41_test_stateid - perform a TEST_STATEID operation
6711 *
6712 * @server: server / transport on which to perform the operation
6713 * @stateid: state ID to test
6714 *
6715 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6716 * Otherwise a negative NFS4ERR value is returned if the operation
6717 * failed or the state ID is not currently valid.
6718 */
6719static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6720{
6721 struct nfs4_exception exception = { };
6722 int err;
6723 do {
6724 err = _nfs41_test_stateid(server, stateid);
6725 if (err != -NFS4ERR_DELAY)
6726 break;
6727 nfs4_handle_exception(server, err, &exception);
6728 } while (exception.retry);
6729 return err;
6730}
6731
6732static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6733{
6734 struct nfs41_free_stateid_args args = {
6735 .stateid = stateid,
6736 };
6737 struct nfs41_free_stateid_res res;
6738 struct rpc_message msg = {
6739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6740 .rpc_argp = &args,
6741 .rpc_resp = &res,
6742 };
6743 int status;
6744
6745 dprintk("NFS call free_stateid %p\n", stateid);
6746 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6747 nfs4_set_sequence_privileged(&args.seq_args);
6748 status = nfs4_call_sync_sequence(server->client, server, &msg,
6749 &args.seq_args, &res.seq_res);
6750 dprintk("NFS reply free_stateid: %d\n", status);
6751 return status;
6752}
6753
6754/**
6755 * nfs41_free_stateid - perform a FREE_STATEID operation
6756 *
6757 * @server: server / transport on which to perform the operation
6758 * @stateid: state ID to release
6759 *
6760 * Returns NFS_OK if the server freed "stateid". Otherwise a
6761 * negative NFS4ERR value is returned.
6762 */
6763static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6764{
6765 struct nfs4_exception exception = { };
6766 int err;
6767 do {
6768 err = _nfs4_free_stateid(server, stateid);
6769 if (err != -NFS4ERR_DELAY)
6770 break;
6771 nfs4_handle_exception(server, err, &exception);
6772 } while (exception.retry);
6773 return err;
6774}
6775
6776static bool nfs41_match_stateid(const nfs4_stateid *s1,
6777 const nfs4_stateid *s2)
6778{
6779 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6780 return false;
6781
6782 if (s1->seqid == s2->seqid)
6783 return true;
6784 if (s1->seqid == 0 || s2->seqid == 0)
6785 return true;
6786
6787 return false;
6788}
6789
6790#endif /* CONFIG_NFS_V4_1 */
6791
6792static bool nfs4_match_stateid(const nfs4_stateid *s1,
6793 const nfs4_stateid *s2)
6794{
6795 return nfs4_stateid_match(s1, s2);
6796}
6797
6798
6799static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6800 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6801 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6802 .recover_open = nfs4_open_reclaim,
6803 .recover_lock = nfs4_lock_reclaim,
6804 .establish_clid = nfs4_init_clientid,
6805 .get_clid_cred = nfs4_get_setclientid_cred,
6806 .detect_trunking = nfs40_discover_server_trunking,
6807};
6808
6809#if defined(CONFIG_NFS_V4_1)
6810static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6811 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6812 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6813 .recover_open = nfs4_open_reclaim,
6814 .recover_lock = nfs4_lock_reclaim,
6815 .establish_clid = nfs41_init_clientid,
6816 .get_clid_cred = nfs4_get_exchange_id_cred,
6817 .reclaim_complete = nfs41_proc_reclaim_complete,
6818 .detect_trunking = nfs41_discover_server_trunking,
6819};
6820#endif /* CONFIG_NFS_V4_1 */
6821
6822static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6823 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6824 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6825 .recover_open = nfs4_open_expired,
6826 .recover_lock = nfs4_lock_expired,
6827 .establish_clid = nfs4_init_clientid,
6828 .get_clid_cred = nfs4_get_setclientid_cred,
6829};
6830
6831#if defined(CONFIG_NFS_V4_1)
6832static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6833 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6834 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6835 .recover_open = nfs41_open_expired,
6836 .recover_lock = nfs41_lock_expired,
6837 .establish_clid = nfs41_init_clientid,
6838 .get_clid_cred = nfs4_get_exchange_id_cred,
6839};
6840#endif /* CONFIG_NFS_V4_1 */
6841
6842static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6843 .sched_state_renewal = nfs4_proc_async_renew,
6844 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6845 .renew_lease = nfs4_proc_renew,
6846};
6847
6848#if defined(CONFIG_NFS_V4_1)
6849static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6850 .sched_state_renewal = nfs41_proc_async_sequence,
6851 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6852 .renew_lease = nfs4_proc_sequence,
6853};
6854#endif
6855
6856static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6857 .minor_version = 0,
6858 .init_caps = NFS_CAP_READDIRPLUS
6859 | NFS_CAP_ATOMIC_OPEN
6860 | NFS_CAP_CHANGE_ATTR
6861 | NFS_CAP_POSIX_LOCK,
6862 .call_sync = _nfs4_call_sync,
6863 .match_stateid = nfs4_match_stateid,
6864 .find_root_sec = nfs4_find_root_sec,
6865 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6866 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6867 .state_renewal_ops = &nfs40_state_renewal_ops,
6868};
6869
6870#if defined(CONFIG_NFS_V4_1)
6871static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6872 .minor_version = 1,
6873 .init_caps = NFS_CAP_READDIRPLUS
6874 | NFS_CAP_ATOMIC_OPEN
6875 | NFS_CAP_CHANGE_ATTR
6876 | NFS_CAP_POSIX_LOCK
6877 | NFS_CAP_STATEID_NFSV41
6878 | NFS_CAP_ATOMIC_OPEN_V1,
6879 .call_sync = nfs4_call_sync_sequence,
6880 .match_stateid = nfs41_match_stateid,
6881 .find_root_sec = nfs41_find_root_sec,
6882 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6883 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6884 .state_renewal_ops = &nfs41_state_renewal_ops,
6885};
6886#endif
6887
6888const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6889 [0] = &nfs_v4_0_minor_ops,
6890#if defined(CONFIG_NFS_V4_1)
6891 [1] = &nfs_v4_1_minor_ops,
6892#endif
6893};
6894
6895const struct inode_operations nfs4_dir_inode_operations = {
6896 .create = nfs_create,
6897 .lookup = nfs_lookup,
6898 .atomic_open = nfs_atomic_open,
6899 .link = nfs_link,
6900 .unlink = nfs_unlink,
6901 .symlink = nfs_symlink,
6902 .mkdir = nfs_mkdir,
6903 .rmdir = nfs_rmdir,
6904 .mknod = nfs_mknod,
6905 .rename = nfs_rename,
6906 .permission = nfs_permission,
6907 .getattr = nfs_getattr,
6908 .setattr = nfs_setattr,
6909 .getxattr = generic_getxattr,
6910 .setxattr = generic_setxattr,
6911 .listxattr = generic_listxattr,
6912 .removexattr = generic_removexattr,
6913};
6914
6915static const struct inode_operations nfs4_file_inode_operations = {
6916 .permission = nfs_permission,
6917 .getattr = nfs_getattr,
6918 .setattr = nfs_setattr,
6919 .getxattr = generic_getxattr,
6920 .setxattr = generic_setxattr,
6921 .listxattr = generic_listxattr,
6922 .removexattr = generic_removexattr,
6923};
6924
6925const struct nfs_rpc_ops nfs_v4_clientops = {
6926 .version = 4, /* protocol version */
6927 .dentry_ops = &nfs4_dentry_operations,
6928 .dir_inode_ops = &nfs4_dir_inode_operations,
6929 .file_inode_ops = &nfs4_file_inode_operations,
6930 .file_ops = &nfs4_file_operations,
6931 .getroot = nfs4_proc_get_root,
6932 .submount = nfs4_submount,
6933 .try_mount = nfs4_try_mount,
6934 .getattr = nfs4_proc_getattr,
6935 .setattr = nfs4_proc_setattr,
6936 .lookup = nfs4_proc_lookup,
6937 .access = nfs4_proc_access,
6938 .readlink = nfs4_proc_readlink,
6939 .create = nfs4_proc_create,
6940 .remove = nfs4_proc_remove,
6941 .unlink_setup = nfs4_proc_unlink_setup,
6942 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6943 .unlink_done = nfs4_proc_unlink_done,
6944 .rename = nfs4_proc_rename,
6945 .rename_setup = nfs4_proc_rename_setup,
6946 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6947 .rename_done = nfs4_proc_rename_done,
6948 .link = nfs4_proc_link,
6949 .symlink = nfs4_proc_symlink,
6950 .mkdir = nfs4_proc_mkdir,
6951 .rmdir = nfs4_proc_remove,
6952 .readdir = nfs4_proc_readdir,
6953 .mknod = nfs4_proc_mknod,
6954 .statfs = nfs4_proc_statfs,
6955 .fsinfo = nfs4_proc_fsinfo,
6956 .pathconf = nfs4_proc_pathconf,
6957 .set_capabilities = nfs4_server_capabilities,
6958 .decode_dirent = nfs4_decode_dirent,
6959 .read_setup = nfs4_proc_read_setup,
6960 .read_pageio_init = pnfs_pageio_init_read,
6961 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6962 .read_done = nfs4_read_done,
6963 .write_setup = nfs4_proc_write_setup,
6964 .write_pageio_init = pnfs_pageio_init_write,
6965 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6966 .write_done = nfs4_write_done,
6967 .commit_setup = nfs4_proc_commit_setup,
6968 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6969 .commit_done = nfs4_commit_done,
6970 .lock = nfs4_proc_lock,
6971 .clear_acl_cache = nfs4_zap_acl_attr,
6972 .close_context = nfs4_close_context,
6973 .open_context = nfs4_atomic_open,
6974 .have_delegation = nfs4_have_delegation,
6975 .return_delegation = nfs4_inode_return_delegation,
6976 .alloc_client = nfs4_alloc_client,
6977 .init_client = nfs4_init_client,
6978 .free_client = nfs4_free_client,
6979 .create_server = nfs4_create_server,
6980 .clone_server = nfs_clone_server,
6981};
6982
6983static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6984 .prefix = XATTR_NAME_NFSV4_ACL,
6985 .list = nfs4_xattr_list_nfs4_acl,
6986 .get = nfs4_xattr_get_nfs4_acl,
6987 .set = nfs4_xattr_set_nfs4_acl,
6988};
6989
6990const struct xattr_handler *nfs4_xattr_handlers[] = {
6991 &nfs4_xattr_nfs4_acl_handler,
6992 NULL
6993};
6994
6995/*
6996 * Local variables:
6997 * c-basic-offset: 8
6998 * End:
6999 */