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