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