NFSv4: Refactor _nfs4_open_and_get_state to set ctx->state
[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 nfs_open_context *ctx)
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 ctx->state = state;
1989 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
1990 nfs4_schedule_stateid_recovery(server, 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{
2003 struct nfs4_state_owner *sp;
2004 struct nfs4_state *state = NULL;
2005 struct nfs_server *server = NFS_SERVER(dir);
2006 struct nfs4_opendata *opendata;
2007 struct dentry *dentry = ctx->dentry;
2008 struct rpc_cred *cred = ctx->cred;
2009 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2010 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2011 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2012 int status;
2013
2014 /* Protect against reboot recovery conflicts */
2015 status = -ENOMEM;
2016 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2017 if (sp == NULL) {
2018 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2019 goto out_err;
2020 }
2021 status = nfs4_recover_expired_lease(server);
2022 if (status != 0)
2023 goto err_put_state_owner;
2024 if (dentry->d_inode != NULL)
2025 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2026 status = -ENOMEM;
2027 if (dentry->d_inode)
2028 claim = NFS4_OPEN_CLAIM_FH;
2029 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2030 claim, GFP_KERNEL);
2031 if (opendata == NULL)
2032 goto err_put_state_owner;
2033
2034 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2035 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2036 if (!opendata->f_attr.mdsthreshold)
2037 goto err_opendata_put;
2038 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2039 }
2040 if (dentry->d_inode != NULL)
2041 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2042
2043 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2044 if (status != 0)
2045 goto err_opendata_put;
2046 state = ctx->state;
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 return 0;
2070err_opendata_put:
2071 kfree(opendata->f_attr.mdsthreshold);
2072 nfs4_opendata_put(opendata);
2073err_put_state_owner:
2074 nfs4_put_state_owner(sp);
2075out_err:
2076 return status;
2077}
2078
2079
2080static struct nfs4_state *nfs4_do_open(struct inode *dir,
2081 struct nfs_open_context *ctx,
2082 int flags,
2083 struct iattr *sattr)
2084{
2085 struct nfs_server *server = NFS_SERVER(dir);
2086 struct nfs4_exception exception = { };
2087 struct nfs4_state *res;
2088 int status;
2089
2090 do {
2091 status = _nfs4_do_open(dir, ctx, flags, sattr);
2092 res = ctx->state;
2093 if (status == 0)
2094 break;
2095 /* NOTE: BAD_SEQID means the server and client disagree about the
2096 * book-keeping w.r.t. state-changing operations
2097 * (OPEN/CLOSE/LOCK/LOCKU...)
2098 * It is actually a sign of a bug on the client or on the server.
2099 *
2100 * If we receive a BAD_SEQID error in the particular case of
2101 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2102 * have unhashed the old state_owner for us, and that we can
2103 * therefore safely retry using a new one. We should still warn
2104 * the user though...
2105 */
2106 if (status == -NFS4ERR_BAD_SEQID) {
2107 pr_warn_ratelimited("NFS: v4 server %s "
2108 " returned a bad sequence-id error!\n",
2109 NFS_SERVER(dir)->nfs_client->cl_hostname);
2110 exception.retry = 1;
2111 continue;
2112 }
2113 /*
2114 * BAD_STATEID on OPEN means that the server cancelled our
2115 * state before it received the OPEN_CONFIRM.
2116 * Recover by retrying the request as per the discussion
2117 * on Page 181 of RFC3530.
2118 */
2119 if (status == -NFS4ERR_BAD_STATEID) {
2120 exception.retry = 1;
2121 continue;
2122 }
2123 if (status == -EAGAIN) {
2124 /* We must have found a delegation */
2125 exception.retry = 1;
2126 continue;
2127 }
2128 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2129 continue;
2130 res = ERR_PTR(nfs4_handle_exception(server,
2131 status, &exception));
2132 } while (exception.retry);
2133 return res;
2134}
2135
2136static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2137 struct nfs_fattr *fattr, struct iattr *sattr,
2138 struct nfs4_state *state)
2139{
2140 struct nfs_server *server = NFS_SERVER(inode);
2141 struct nfs_setattrargs arg = {
2142 .fh = NFS_FH(inode),
2143 .iap = sattr,
2144 .server = server,
2145 .bitmask = server->attr_bitmask,
2146 };
2147 struct nfs_setattrres res = {
2148 .fattr = fattr,
2149 .server = server,
2150 };
2151 struct rpc_message msg = {
2152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2153 .rpc_argp = &arg,
2154 .rpc_resp = &res,
2155 .rpc_cred = cred,
2156 };
2157 unsigned long timestamp = jiffies;
2158 fmode_t fmode;
2159 bool truncate;
2160 int status;
2161
2162 nfs_fattr_init(fattr);
2163
2164 /* Servers should only apply open mode checks for file size changes */
2165 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2166 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2167
2168 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2169 /* Use that stateid */
2170 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
2171 struct nfs_lockowner lockowner = {
2172 .l_owner = current->files,
2173 .l_pid = current->tgid,
2174 };
2175 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2176 &lockowner);
2177 } else
2178 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2179
2180 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2181 if (status == 0 && state != NULL)
2182 renew_lease(server, timestamp);
2183 return status;
2184}
2185
2186static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2187 struct nfs_fattr *fattr, struct iattr *sattr,
2188 struct nfs4_state *state)
2189{
2190 struct nfs_server *server = NFS_SERVER(inode);
2191 struct nfs4_exception exception = {
2192 .state = state,
2193 .inode = inode,
2194 };
2195 int err;
2196 do {
2197 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2198 switch (err) {
2199 case -NFS4ERR_OPENMODE:
2200 if (!(sattr->ia_valid & ATTR_SIZE)) {
2201 pr_warn_once("NFSv4: server %s is incorrectly "
2202 "applying open mode checks to "
2203 "a SETATTR that is not "
2204 "changing file size.\n",
2205 server->nfs_client->cl_hostname);
2206 }
2207 if (state && !(state->state & FMODE_WRITE)) {
2208 err = -EBADF;
2209 if (sattr->ia_valid & ATTR_OPEN)
2210 err = -EACCES;
2211 goto out;
2212 }
2213 }
2214 err = nfs4_handle_exception(server, err, &exception);
2215 } while (exception.retry);
2216out:
2217 return err;
2218}
2219
2220struct nfs4_closedata {
2221 struct inode *inode;
2222 struct nfs4_state *state;
2223 struct nfs_closeargs arg;
2224 struct nfs_closeres res;
2225 struct nfs_fattr fattr;
2226 unsigned long timestamp;
2227 bool roc;
2228 u32 roc_barrier;
2229};
2230
2231static void nfs4_free_closedata(void *data)
2232{
2233 struct nfs4_closedata *calldata = data;
2234 struct nfs4_state_owner *sp = calldata->state->owner;
2235 struct super_block *sb = calldata->state->inode->i_sb;
2236
2237 if (calldata->roc)
2238 pnfs_roc_release(calldata->state->inode);
2239 nfs4_put_open_state(calldata->state);
2240 nfs_free_seqid(calldata->arg.seqid);
2241 nfs4_put_state_owner(sp);
2242 nfs_sb_deactive(sb);
2243 kfree(calldata);
2244}
2245
2246static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2247 fmode_t fmode)
2248{
2249 spin_lock(&state->owner->so_lock);
2250 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2251 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2252 case FMODE_WRITE:
2253 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2254 break;
2255 case FMODE_READ:
2256 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2257 break;
2258 case 0:
2259 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2260 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2261 clear_bit(NFS_OPEN_STATE, &state->flags);
2262 }
2263 spin_unlock(&state->owner->so_lock);
2264}
2265
2266static void nfs4_close_done(struct rpc_task *task, void *data)
2267{
2268 struct nfs4_closedata *calldata = data;
2269 struct nfs4_state *state = calldata->state;
2270 struct nfs_server *server = NFS_SERVER(calldata->inode);
2271
2272 dprintk("%s: begin!\n", __func__);
2273 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2274 return;
2275 /* hmm. we are done with the inode, and in the process of freeing
2276 * the state_owner. we keep this around to process errors
2277 */
2278 switch (task->tk_status) {
2279 case 0:
2280 if (calldata->roc)
2281 pnfs_roc_set_barrier(state->inode,
2282 calldata->roc_barrier);
2283 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2284 renew_lease(server, calldata->timestamp);
2285 nfs4_close_clear_stateid_flags(state,
2286 calldata->arg.fmode);
2287 break;
2288 case -NFS4ERR_STALE_STATEID:
2289 case -NFS4ERR_OLD_STATEID:
2290 case -NFS4ERR_BAD_STATEID:
2291 case -NFS4ERR_EXPIRED:
2292 if (calldata->arg.fmode == 0)
2293 break;
2294 default:
2295 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2296 rpc_restart_call_prepare(task);
2297 }
2298 nfs_release_seqid(calldata->arg.seqid);
2299 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2300 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2301}
2302
2303static void nfs4_close_prepare(struct rpc_task *task, void *data)
2304{
2305 struct nfs4_closedata *calldata = data;
2306 struct nfs4_state *state = calldata->state;
2307 struct inode *inode = calldata->inode;
2308 int call_close = 0;
2309
2310 dprintk("%s: begin!\n", __func__);
2311 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2312 goto out_wait;
2313
2314 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2315 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2316 spin_lock(&state->owner->so_lock);
2317 /* Calculate the change in open mode */
2318 if (state->n_rdwr == 0) {
2319 if (state->n_rdonly == 0) {
2320 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2321 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2322 calldata->arg.fmode &= ~FMODE_READ;
2323 }
2324 if (state->n_wronly == 0) {
2325 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2326 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2327 calldata->arg.fmode &= ~FMODE_WRITE;
2328 }
2329 }
2330 if (!nfs4_valid_open_stateid(state))
2331 call_close = 0;
2332 spin_unlock(&state->owner->so_lock);
2333
2334 if (!call_close) {
2335 /* Note: exit _without_ calling nfs4_close_done */
2336 goto out_no_action;
2337 }
2338
2339 if (calldata->arg.fmode == 0) {
2340 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2341 if (calldata->roc &&
2342 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2343 nfs_release_seqid(calldata->arg.seqid);
2344 goto out_wait;
2345 }
2346 }
2347
2348 nfs_fattr_init(calldata->res.fattr);
2349 calldata->timestamp = jiffies;
2350 if (nfs4_setup_sequence(NFS_SERVER(inode),
2351 &calldata->arg.seq_args,
2352 &calldata->res.seq_res,
2353 task) != 0)
2354 nfs_release_seqid(calldata->arg.seqid);
2355 dprintk("%s: done!\n", __func__);
2356 return;
2357out_no_action:
2358 task->tk_action = NULL;
2359out_wait:
2360 nfs4_sequence_done(task, &calldata->res.seq_res);
2361}
2362
2363static const struct rpc_call_ops nfs4_close_ops = {
2364 .rpc_call_prepare = nfs4_close_prepare,
2365 .rpc_call_done = nfs4_close_done,
2366 .rpc_release = nfs4_free_closedata,
2367};
2368
2369/*
2370 * It is possible for data to be read/written from a mem-mapped file
2371 * after the sys_close call (which hits the vfs layer as a flush).
2372 * This means that we can't safely call nfsv4 close on a file until
2373 * the inode is cleared. This in turn means that we are not good
2374 * NFSv4 citizens - we do not indicate to the server to update the file's
2375 * share state even when we are done with one of the three share
2376 * stateid's in the inode.
2377 *
2378 * NOTE: Caller must be holding the sp->so_owner semaphore!
2379 */
2380int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2381{
2382 struct nfs_server *server = NFS_SERVER(state->inode);
2383 struct nfs4_closedata *calldata;
2384 struct nfs4_state_owner *sp = state->owner;
2385 struct rpc_task *task;
2386 struct rpc_message msg = {
2387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2388 .rpc_cred = state->owner->so_cred,
2389 };
2390 struct rpc_task_setup task_setup_data = {
2391 .rpc_client = server->client,
2392 .rpc_message = &msg,
2393 .callback_ops = &nfs4_close_ops,
2394 .workqueue = nfsiod_workqueue,
2395 .flags = RPC_TASK_ASYNC,
2396 };
2397 int status = -ENOMEM;
2398
2399 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2400 if (calldata == NULL)
2401 goto out;
2402 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2403 calldata->inode = state->inode;
2404 calldata->state = state;
2405 calldata->arg.fh = NFS_FH(state->inode);
2406 calldata->arg.stateid = &state->open_stateid;
2407 /* Serialization for the sequence id */
2408 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2409 if (calldata->arg.seqid == NULL)
2410 goto out_free_calldata;
2411 calldata->arg.fmode = 0;
2412 calldata->arg.bitmask = server->cache_consistency_bitmask;
2413 calldata->res.fattr = &calldata->fattr;
2414 calldata->res.seqid = calldata->arg.seqid;
2415 calldata->res.server = server;
2416 calldata->roc = pnfs_roc(state->inode);
2417 nfs_sb_active(calldata->inode->i_sb);
2418
2419 msg.rpc_argp = &calldata->arg;
2420 msg.rpc_resp = &calldata->res;
2421 task_setup_data.callback_data = calldata;
2422 task = rpc_run_task(&task_setup_data);
2423 if (IS_ERR(task))
2424 return PTR_ERR(task);
2425 status = 0;
2426 if (wait)
2427 status = rpc_wait_for_completion_task(task);
2428 rpc_put_task(task);
2429 return status;
2430out_free_calldata:
2431 kfree(calldata);
2432out:
2433 nfs4_put_open_state(state);
2434 nfs4_put_state_owner(sp);
2435 return status;
2436}
2437
2438static struct inode *
2439nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2440{
2441 struct nfs4_state *state;
2442
2443 /* Protect against concurrent sillydeletes */
2444 state = nfs4_do_open(dir, ctx, open_flags, attr);
2445 if (IS_ERR(state))
2446 return ERR_CAST(state);
2447 return igrab(state->inode);
2448}
2449
2450static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2451{
2452 if (ctx->state == NULL)
2453 return;
2454 if (is_sync)
2455 nfs4_close_sync(ctx->state, ctx->mode);
2456 else
2457 nfs4_close_state(ctx->state, ctx->mode);
2458}
2459
2460static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2461{
2462 struct nfs4_server_caps_arg args = {
2463 .fhandle = fhandle,
2464 };
2465 struct nfs4_server_caps_res res = {};
2466 struct rpc_message msg = {
2467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2468 .rpc_argp = &args,
2469 .rpc_resp = &res,
2470 };
2471 int status;
2472
2473 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2474 if (status == 0) {
2475 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2476 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2477 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2478 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2479 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2480 NFS_CAP_CTIME|NFS_CAP_MTIME);
2481 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2482 server->caps |= NFS_CAP_ACLS;
2483 if (res.has_links != 0)
2484 server->caps |= NFS_CAP_HARDLINKS;
2485 if (res.has_symlinks != 0)
2486 server->caps |= NFS_CAP_SYMLINKS;
2487 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2488 server->caps |= NFS_CAP_FILEID;
2489 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2490 server->caps |= NFS_CAP_MODE;
2491 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2492 server->caps |= NFS_CAP_NLINK;
2493 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2494 server->caps |= NFS_CAP_OWNER;
2495 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2496 server->caps |= NFS_CAP_OWNER_GROUP;
2497 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2498 server->caps |= NFS_CAP_ATIME;
2499 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2500 server->caps |= NFS_CAP_CTIME;
2501 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2502 server->caps |= NFS_CAP_MTIME;
2503
2504 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2505 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2506 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2507 server->acl_bitmask = res.acl_bitmask;
2508 server->fh_expire_type = res.fh_expire_type;
2509 }
2510
2511 return status;
2512}
2513
2514int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2515{
2516 struct nfs4_exception exception = { };
2517 int err;
2518 do {
2519 err = nfs4_handle_exception(server,
2520 _nfs4_server_capabilities(server, fhandle),
2521 &exception);
2522 } while (exception.retry);
2523 return err;
2524}
2525
2526static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2527 struct nfs_fsinfo *info)
2528{
2529 struct nfs4_lookup_root_arg args = {
2530 .bitmask = nfs4_fattr_bitmap,
2531 };
2532 struct nfs4_lookup_res res = {
2533 .server = server,
2534 .fattr = info->fattr,
2535 .fh = fhandle,
2536 };
2537 struct rpc_message msg = {
2538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2539 .rpc_argp = &args,
2540 .rpc_resp = &res,
2541 };
2542
2543 nfs_fattr_init(info->fattr);
2544 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2545}
2546
2547static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2548 struct nfs_fsinfo *info)
2549{
2550 struct nfs4_exception exception = { };
2551 int err;
2552 do {
2553 err = _nfs4_lookup_root(server, fhandle, info);
2554 switch (err) {
2555 case 0:
2556 case -NFS4ERR_WRONGSEC:
2557 goto out;
2558 default:
2559 err = nfs4_handle_exception(server, err, &exception);
2560 }
2561 } while (exception.retry);
2562out:
2563 return err;
2564}
2565
2566static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2567 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2568{
2569 struct rpc_auth *auth;
2570 int ret;
2571
2572 auth = rpcauth_create(flavor, server->client);
2573 if (IS_ERR(auth)) {
2574 ret = -EACCES;
2575 goto out;
2576 }
2577 ret = nfs4_lookup_root(server, fhandle, info);
2578out:
2579 return ret;
2580}
2581
2582/*
2583 * Retry pseudoroot lookup with various security flavors. We do this when:
2584 *
2585 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2586 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2587 *
2588 * Returns zero on success, or a negative NFS4ERR value, or a
2589 * negative errno value.
2590 */
2591static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2592 struct nfs_fsinfo *info)
2593{
2594 /* Per 3530bis 15.33.5 */
2595 static const rpc_authflavor_t flav_array[] = {
2596 RPC_AUTH_GSS_KRB5P,
2597 RPC_AUTH_GSS_KRB5I,
2598 RPC_AUTH_GSS_KRB5,
2599 RPC_AUTH_UNIX, /* courtesy */
2600 RPC_AUTH_NULL,
2601 };
2602 int status = -EPERM;
2603 size_t i;
2604
2605 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2606 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2607 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2608 continue;
2609 break;
2610 }
2611
2612 /*
2613 * -EACCESS could mean that the user doesn't have correct permissions
2614 * to access the mount. It could also mean that we tried to mount
2615 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2616 * existing mount programs don't handle -EACCES very well so it should
2617 * be mapped to -EPERM instead.
2618 */
2619 if (status == -EACCES)
2620 status = -EPERM;
2621 return status;
2622}
2623
2624static int nfs4_do_find_root_sec(struct nfs_server *server,
2625 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2626{
2627 int mv = server->nfs_client->cl_minorversion;
2628 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2629}
2630
2631/**
2632 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2633 * @server: initialized nfs_server handle
2634 * @fhandle: we fill in the pseudo-fs root file handle
2635 * @info: we fill in an FSINFO struct
2636 *
2637 * Returns zero on success, or a negative errno.
2638 */
2639int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2640 struct nfs_fsinfo *info)
2641{
2642 int status;
2643
2644 status = nfs4_lookup_root(server, fhandle, info);
2645 if ((status == -NFS4ERR_WRONGSEC) &&
2646 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2647 status = nfs4_do_find_root_sec(server, fhandle, info);
2648
2649 if (status == 0)
2650 status = nfs4_server_capabilities(server, fhandle);
2651 if (status == 0)
2652 status = nfs4_do_fsinfo(server, fhandle, info);
2653
2654 return nfs4_map_errors(status);
2655}
2656
2657static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2658 struct nfs_fsinfo *info)
2659{
2660 int error;
2661 struct nfs_fattr *fattr = info->fattr;
2662
2663 error = nfs4_server_capabilities(server, mntfh);
2664 if (error < 0) {
2665 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2666 return error;
2667 }
2668
2669 error = nfs4_proc_getattr(server, mntfh, fattr);
2670 if (error < 0) {
2671 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2672 return error;
2673 }
2674
2675 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2676 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2677 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2678
2679 return error;
2680}
2681
2682/*
2683 * Get locations and (maybe) other attributes of a referral.
2684 * Note that we'll actually follow the referral later when
2685 * we detect fsid mismatch in inode revalidation
2686 */
2687static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2688 const struct qstr *name, struct nfs_fattr *fattr,
2689 struct nfs_fh *fhandle)
2690{
2691 int status = -ENOMEM;
2692 struct page *page = NULL;
2693 struct nfs4_fs_locations *locations = NULL;
2694
2695 page = alloc_page(GFP_KERNEL);
2696 if (page == NULL)
2697 goto out;
2698 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2699 if (locations == NULL)
2700 goto out;
2701
2702 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2703 if (status != 0)
2704 goto out;
2705 /* Make sure server returned a different fsid for the referral */
2706 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2707 dprintk("%s: server did not return a different fsid for"
2708 " a referral at %s\n", __func__, name->name);
2709 status = -EIO;
2710 goto out;
2711 }
2712 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2713 nfs_fixup_referral_attributes(&locations->fattr);
2714
2715 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2716 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2717 memset(fhandle, 0, sizeof(struct nfs_fh));
2718out:
2719 if (page)
2720 __free_page(page);
2721 kfree(locations);
2722 return status;
2723}
2724
2725static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2726{
2727 struct nfs4_getattr_arg args = {
2728 .fh = fhandle,
2729 .bitmask = server->attr_bitmask,
2730 };
2731 struct nfs4_getattr_res res = {
2732 .fattr = fattr,
2733 .server = server,
2734 };
2735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2737 .rpc_argp = &args,
2738 .rpc_resp = &res,
2739 };
2740
2741 nfs_fattr_init(fattr);
2742 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2743}
2744
2745static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2746{
2747 struct nfs4_exception exception = { };
2748 int err;
2749 do {
2750 err = nfs4_handle_exception(server,
2751 _nfs4_proc_getattr(server, fhandle, fattr),
2752 &exception);
2753 } while (exception.retry);
2754 return err;
2755}
2756
2757/*
2758 * The file is not closed if it is opened due to the a request to change
2759 * the size of the file. The open call will not be needed once the
2760 * VFS layer lookup-intents are implemented.
2761 *
2762 * Close is called when the inode is destroyed.
2763 * If we haven't opened the file for O_WRONLY, we
2764 * need to in the size_change case to obtain a stateid.
2765 *
2766 * Got race?
2767 * Because OPEN is always done by name in nfsv4, it is
2768 * possible that we opened a different file by the same
2769 * name. We can recognize this race condition, but we
2770 * can't do anything about it besides returning an error.
2771 *
2772 * This will be fixed with VFS changes (lookup-intent).
2773 */
2774static int
2775nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2776 struct iattr *sattr)
2777{
2778 struct inode *inode = dentry->d_inode;
2779 struct rpc_cred *cred = NULL;
2780 struct nfs4_state *state = NULL;
2781 int status;
2782
2783 if (pnfs_ld_layoutret_on_setattr(inode))
2784 pnfs_commit_and_return_layout(inode);
2785
2786 nfs_fattr_init(fattr);
2787
2788 /* Deal with open(O_TRUNC) */
2789 if (sattr->ia_valid & ATTR_OPEN)
2790 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2791
2792 /* Optimization: if the end result is no change, don't RPC */
2793 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2794 return 0;
2795
2796 /* Search for an existing open(O_WRITE) file */
2797 if (sattr->ia_valid & ATTR_FILE) {
2798 struct nfs_open_context *ctx;
2799
2800 ctx = nfs_file_open_context(sattr->ia_file);
2801 if (ctx) {
2802 cred = ctx->cred;
2803 state = ctx->state;
2804 }
2805 }
2806
2807 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2808 if (status == 0)
2809 nfs_setattr_update_inode(inode, sattr);
2810 return status;
2811}
2812
2813static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2814 const struct qstr *name, struct nfs_fh *fhandle,
2815 struct nfs_fattr *fattr)
2816{
2817 struct nfs_server *server = NFS_SERVER(dir);
2818 int status;
2819 struct nfs4_lookup_arg args = {
2820 .bitmask = server->attr_bitmask,
2821 .dir_fh = NFS_FH(dir),
2822 .name = name,
2823 };
2824 struct nfs4_lookup_res res = {
2825 .server = server,
2826 .fattr = fattr,
2827 .fh = fhandle,
2828 };
2829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2831 .rpc_argp = &args,
2832 .rpc_resp = &res,
2833 };
2834
2835 nfs_fattr_init(fattr);
2836
2837 dprintk("NFS call lookup %s\n", name->name);
2838 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2839 dprintk("NFS reply lookup: %d\n", status);
2840 return status;
2841}
2842
2843static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2844{
2845 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2846 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2847 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2848 fattr->nlink = 2;
2849}
2850
2851static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2852 struct qstr *name, struct nfs_fh *fhandle,
2853 struct nfs_fattr *fattr)
2854{
2855 struct nfs4_exception exception = { };
2856 struct rpc_clnt *client = *clnt;
2857 int err;
2858 do {
2859 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2860 switch (err) {
2861 case -NFS4ERR_BADNAME:
2862 err = -ENOENT;
2863 goto out;
2864 case -NFS4ERR_MOVED:
2865 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2866 goto out;
2867 case -NFS4ERR_WRONGSEC:
2868 err = -EPERM;
2869 if (client != *clnt)
2870 goto out;
2871
2872 client = nfs4_create_sec_client(client, dir, name);
2873 if (IS_ERR(client))
2874 return PTR_ERR(client);
2875
2876 exception.retry = 1;
2877 break;
2878 default:
2879 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2880 }
2881 } while (exception.retry);
2882
2883out:
2884 if (err == 0)
2885 *clnt = client;
2886 else if (client != *clnt)
2887 rpc_shutdown_client(client);
2888
2889 return err;
2890}
2891
2892static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2893 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2894{
2895 int status;
2896 struct rpc_clnt *client = NFS_CLIENT(dir);
2897
2898 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2899 if (client != NFS_CLIENT(dir)) {
2900 rpc_shutdown_client(client);
2901 nfs_fixup_secinfo_attributes(fattr);
2902 }
2903 return status;
2904}
2905
2906struct rpc_clnt *
2907nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2908 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2909{
2910 int status;
2911 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2912
2913 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2914 if (status < 0) {
2915 rpc_shutdown_client(client);
2916 return ERR_PTR(status);
2917 }
2918 return client;
2919}
2920
2921static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2922{
2923 struct nfs_server *server = NFS_SERVER(inode);
2924 struct nfs4_accessargs args = {
2925 .fh = NFS_FH(inode),
2926 .bitmask = server->cache_consistency_bitmask,
2927 };
2928 struct nfs4_accessres res = {
2929 .server = server,
2930 };
2931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2933 .rpc_argp = &args,
2934 .rpc_resp = &res,
2935 .rpc_cred = entry->cred,
2936 };
2937 int mode = entry->mask;
2938 int status;
2939
2940 /*
2941 * Determine which access bits we want to ask for...
2942 */
2943 if (mode & MAY_READ)
2944 args.access |= NFS4_ACCESS_READ;
2945 if (S_ISDIR(inode->i_mode)) {
2946 if (mode & MAY_WRITE)
2947 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2948 if (mode & MAY_EXEC)
2949 args.access |= NFS4_ACCESS_LOOKUP;
2950 } else {
2951 if (mode & MAY_WRITE)
2952 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2953 if (mode & MAY_EXEC)
2954 args.access |= NFS4_ACCESS_EXECUTE;
2955 }
2956
2957 res.fattr = nfs_alloc_fattr();
2958 if (res.fattr == NULL)
2959 return -ENOMEM;
2960
2961 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2962 if (!status) {
2963 nfs_access_set_mask(entry, res.access);
2964 nfs_refresh_inode(inode, res.fattr);
2965 }
2966 nfs_free_fattr(res.fattr);
2967 return status;
2968}
2969
2970static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2971{
2972 struct nfs4_exception exception = { };
2973 int err;
2974 do {
2975 err = nfs4_handle_exception(NFS_SERVER(inode),
2976 _nfs4_proc_access(inode, entry),
2977 &exception);
2978 } while (exception.retry);
2979 return err;
2980}
2981
2982/*
2983 * TODO: For the time being, we don't try to get any attributes
2984 * along with any of the zero-copy operations READ, READDIR,
2985 * READLINK, WRITE.
2986 *
2987 * In the case of the first three, we want to put the GETATTR
2988 * after the read-type operation -- this is because it is hard
2989 * to predict the length of a GETATTR response in v4, and thus
2990 * align the READ data correctly. This means that the GETATTR
2991 * may end up partially falling into the page cache, and we should
2992 * shift it into the 'tail' of the xdr_buf before processing.
2993 * To do this efficiently, we need to know the total length
2994 * of data received, which doesn't seem to be available outside
2995 * of the RPC layer.
2996 *
2997 * In the case of WRITE, we also want to put the GETATTR after
2998 * the operation -- in this case because we want to make sure
2999 * we get the post-operation mtime and size.
3000 *
3001 * Both of these changes to the XDR layer would in fact be quite
3002 * minor, but I decided to leave them for a subsequent patch.
3003 */
3004static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3005 unsigned int pgbase, unsigned int pglen)
3006{
3007 struct nfs4_readlink args = {
3008 .fh = NFS_FH(inode),
3009 .pgbase = pgbase,
3010 .pglen = pglen,
3011 .pages = &page,
3012 };
3013 struct nfs4_readlink_res res;
3014 struct rpc_message msg = {
3015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3016 .rpc_argp = &args,
3017 .rpc_resp = &res,
3018 };
3019
3020 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3021}
3022
3023static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3024 unsigned int pgbase, unsigned int pglen)
3025{
3026 struct nfs4_exception exception = { };
3027 int err;
3028 do {
3029 err = nfs4_handle_exception(NFS_SERVER(inode),
3030 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3031 &exception);
3032 } while (exception.retry);
3033 return err;
3034}
3035
3036/*
3037 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3038 */
3039static int
3040nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3041 int flags)
3042{
3043 struct nfs_open_context *ctx;
3044 struct nfs4_state *state;
3045 int status = 0;
3046
3047 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3048 if (IS_ERR(ctx))
3049 return PTR_ERR(ctx);
3050
3051 sattr->ia_mode &= ~current_umask();
3052 state = nfs4_do_open(dir, ctx, flags, sattr);
3053 d_drop(dentry);
3054 if (IS_ERR(state)) {
3055 status = PTR_ERR(state);
3056 goto out;
3057 }
3058 d_add(dentry, igrab(state->inode));
3059 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
3060out:
3061 put_nfs_open_context(ctx);
3062 return status;
3063}
3064
3065static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3066{
3067 struct nfs_server *server = NFS_SERVER(dir);
3068 struct nfs_removeargs args = {
3069 .fh = NFS_FH(dir),
3070 .name = *name,
3071 };
3072 struct nfs_removeres res = {
3073 .server = server,
3074 };
3075 struct rpc_message msg = {
3076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3077 .rpc_argp = &args,
3078 .rpc_resp = &res,
3079 };
3080 int status;
3081
3082 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3083 if (status == 0)
3084 update_changeattr(dir, &res.cinfo);
3085 return status;
3086}
3087
3088static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3089{
3090 struct nfs4_exception exception = { };
3091 int err;
3092 do {
3093 err = nfs4_handle_exception(NFS_SERVER(dir),
3094 _nfs4_proc_remove(dir, name),
3095 &exception);
3096 } while (exception.retry);
3097 return err;
3098}
3099
3100static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3101{
3102 struct nfs_server *server = NFS_SERVER(dir);
3103 struct nfs_removeargs *args = msg->rpc_argp;
3104 struct nfs_removeres *res = msg->rpc_resp;
3105
3106 res->server = server;
3107 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3108 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3109}
3110
3111static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3112{
3113 nfs4_setup_sequence(NFS_SERVER(data->dir),
3114 &data->args.seq_args,
3115 &data->res.seq_res,
3116 task);
3117}
3118
3119static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3120{
3121 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3122
3123 if (!nfs4_sequence_done(task, &res->seq_res))
3124 return 0;
3125 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3126 return 0;
3127 update_changeattr(dir, &res->cinfo);
3128 return 1;
3129}
3130
3131static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3132{
3133 struct nfs_server *server = NFS_SERVER(dir);
3134 struct nfs_renameargs *arg = msg->rpc_argp;
3135 struct nfs_renameres *res = msg->rpc_resp;
3136
3137 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3138 res->server = server;
3139 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3140}
3141
3142static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3143{
3144 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3145 &data->args.seq_args,
3146 &data->res.seq_res,
3147 task);
3148}
3149
3150static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3151 struct inode *new_dir)
3152{
3153 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3154
3155 if (!nfs4_sequence_done(task, &res->seq_res))
3156 return 0;
3157 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3158 return 0;
3159
3160 update_changeattr(old_dir, &res->old_cinfo);
3161 update_changeattr(new_dir, &res->new_cinfo);
3162 return 1;
3163}
3164
3165static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3166 struct inode *new_dir, struct qstr *new_name)
3167{
3168 struct nfs_server *server = NFS_SERVER(old_dir);
3169 struct nfs_renameargs arg = {
3170 .old_dir = NFS_FH(old_dir),
3171 .new_dir = NFS_FH(new_dir),
3172 .old_name = old_name,
3173 .new_name = new_name,
3174 };
3175 struct nfs_renameres res = {
3176 .server = server,
3177 };
3178 struct rpc_message msg = {
3179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3180 .rpc_argp = &arg,
3181 .rpc_resp = &res,
3182 };
3183 int status = -ENOMEM;
3184
3185 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3186 if (!status) {
3187 update_changeattr(old_dir, &res.old_cinfo);
3188 update_changeattr(new_dir, &res.new_cinfo);
3189 }
3190 return status;
3191}
3192
3193static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3194 struct inode *new_dir, struct qstr *new_name)
3195{
3196 struct nfs4_exception exception = { };
3197 int err;
3198 do {
3199 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3200 _nfs4_proc_rename(old_dir, old_name,
3201 new_dir, new_name),
3202 &exception);
3203 } while (exception.retry);
3204 return err;
3205}
3206
3207static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3208{
3209 struct nfs_server *server = NFS_SERVER(inode);
3210 struct nfs4_link_arg arg = {
3211 .fh = NFS_FH(inode),
3212 .dir_fh = NFS_FH(dir),
3213 .name = name,
3214 .bitmask = server->attr_bitmask,
3215 };
3216 struct nfs4_link_res res = {
3217 .server = server,
3218 };
3219 struct rpc_message msg = {
3220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3221 .rpc_argp = &arg,
3222 .rpc_resp = &res,
3223 };
3224 int status = -ENOMEM;
3225
3226 res.fattr = nfs_alloc_fattr();
3227 if (res.fattr == NULL)
3228 goto out;
3229
3230 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3231 if (!status) {
3232 update_changeattr(dir, &res.cinfo);
3233 nfs_post_op_update_inode(inode, res.fattr);
3234 }
3235out:
3236 nfs_free_fattr(res.fattr);
3237 return status;
3238}
3239
3240static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3241{
3242 struct nfs4_exception exception = { };
3243 int err;
3244 do {
3245 err = nfs4_handle_exception(NFS_SERVER(inode),
3246 _nfs4_proc_link(inode, dir, name),
3247 &exception);
3248 } while (exception.retry);
3249 return err;
3250}
3251
3252struct nfs4_createdata {
3253 struct rpc_message msg;
3254 struct nfs4_create_arg arg;
3255 struct nfs4_create_res res;
3256 struct nfs_fh fh;
3257 struct nfs_fattr fattr;
3258};
3259
3260static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3261 struct qstr *name, struct iattr *sattr, u32 ftype)
3262{
3263 struct nfs4_createdata *data;
3264
3265 data = kzalloc(sizeof(*data), GFP_KERNEL);
3266 if (data != NULL) {
3267 struct nfs_server *server = NFS_SERVER(dir);
3268
3269 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3270 data->msg.rpc_argp = &data->arg;
3271 data->msg.rpc_resp = &data->res;
3272 data->arg.dir_fh = NFS_FH(dir);
3273 data->arg.server = server;
3274 data->arg.name = name;
3275 data->arg.attrs = sattr;
3276 data->arg.ftype = ftype;
3277 data->arg.bitmask = server->attr_bitmask;
3278 data->res.server = server;
3279 data->res.fh = &data->fh;
3280 data->res.fattr = &data->fattr;
3281 nfs_fattr_init(data->res.fattr);
3282 }
3283 return data;
3284}
3285
3286static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3287{
3288 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3289 &data->arg.seq_args, &data->res.seq_res, 1);
3290 if (status == 0) {
3291 update_changeattr(dir, &data->res.dir_cinfo);
3292 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3293 }
3294 return status;
3295}
3296
3297static void nfs4_free_createdata(struct nfs4_createdata *data)
3298{
3299 kfree(data);
3300}
3301
3302static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3303 struct page *page, unsigned int len, struct iattr *sattr)
3304{
3305 struct nfs4_createdata *data;
3306 int status = -ENAMETOOLONG;
3307
3308 if (len > NFS4_MAXPATHLEN)
3309 goto out;
3310
3311 status = -ENOMEM;
3312 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3313 if (data == NULL)
3314 goto out;
3315
3316 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3317 data->arg.u.symlink.pages = &page;
3318 data->arg.u.symlink.len = len;
3319
3320 status = nfs4_do_create(dir, dentry, data);
3321
3322 nfs4_free_createdata(data);
3323out:
3324 return status;
3325}
3326
3327static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3328 struct page *page, unsigned int len, struct iattr *sattr)
3329{
3330 struct nfs4_exception exception = { };
3331 int err;
3332 do {
3333 err = nfs4_handle_exception(NFS_SERVER(dir),
3334 _nfs4_proc_symlink(dir, dentry, page,
3335 len, sattr),
3336 &exception);
3337 } while (exception.retry);
3338 return err;
3339}
3340
3341static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3342 struct iattr *sattr)
3343{
3344 struct nfs4_createdata *data;
3345 int status = -ENOMEM;
3346
3347 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3348 if (data == NULL)
3349 goto out;
3350
3351 status = nfs4_do_create(dir, dentry, data);
3352
3353 nfs4_free_createdata(data);
3354out:
3355 return status;
3356}
3357
3358static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3359 struct iattr *sattr)
3360{
3361 struct nfs4_exception exception = { };
3362 int err;
3363
3364 sattr->ia_mode &= ~current_umask();
3365 do {
3366 err = nfs4_handle_exception(NFS_SERVER(dir),
3367 _nfs4_proc_mkdir(dir, dentry, sattr),
3368 &exception);
3369 } while (exception.retry);
3370 return err;
3371}
3372
3373static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3374 u64 cookie, struct page **pages, unsigned int count, int plus)
3375{
3376 struct inode *dir = dentry->d_inode;
3377 struct nfs4_readdir_arg args = {
3378 .fh = NFS_FH(dir),
3379 .pages = pages,
3380 .pgbase = 0,
3381 .count = count,
3382 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3383 .plus = plus,
3384 };
3385 struct nfs4_readdir_res res;
3386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3388 .rpc_argp = &args,
3389 .rpc_resp = &res,
3390 .rpc_cred = cred,
3391 };
3392 int status;
3393
3394 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3395 dentry->d_parent->d_name.name,
3396 dentry->d_name.name,
3397 (unsigned long long)cookie);
3398 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3399 res.pgbase = args.pgbase;
3400 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3401 if (status >= 0) {
3402 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3403 status += args.pgbase;
3404 }
3405
3406 nfs_invalidate_atime(dir);
3407
3408 dprintk("%s: returns %d\n", __func__, status);
3409 return status;
3410}
3411
3412static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3413 u64 cookie, struct page **pages, unsigned int count, int plus)
3414{
3415 struct nfs4_exception exception = { };
3416 int err;
3417 do {
3418 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3419 _nfs4_proc_readdir(dentry, cred, cookie,
3420 pages, count, plus),
3421 &exception);
3422 } while (exception.retry);
3423 return err;
3424}
3425
3426static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3427 struct iattr *sattr, dev_t rdev)
3428{
3429 struct nfs4_createdata *data;
3430 int mode = sattr->ia_mode;
3431 int status = -ENOMEM;
3432
3433 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3434 if (data == NULL)
3435 goto out;
3436
3437 if (S_ISFIFO(mode))
3438 data->arg.ftype = NF4FIFO;
3439 else if (S_ISBLK(mode)) {
3440 data->arg.ftype = NF4BLK;
3441 data->arg.u.device.specdata1 = MAJOR(rdev);
3442 data->arg.u.device.specdata2 = MINOR(rdev);
3443 }
3444 else if (S_ISCHR(mode)) {
3445 data->arg.ftype = NF4CHR;
3446 data->arg.u.device.specdata1 = MAJOR(rdev);
3447 data->arg.u.device.specdata2 = MINOR(rdev);
3448 } else if (!S_ISSOCK(mode)) {
3449 status = -EINVAL;
3450 goto out_free;
3451 }
3452
3453 status = nfs4_do_create(dir, dentry, data);
3454out_free:
3455 nfs4_free_createdata(data);
3456out:
3457 return status;
3458}
3459
3460static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3461 struct iattr *sattr, dev_t rdev)
3462{
3463 struct nfs4_exception exception = { };
3464 int err;
3465
3466 sattr->ia_mode &= ~current_umask();
3467 do {
3468 err = nfs4_handle_exception(NFS_SERVER(dir),
3469 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3470 &exception);
3471 } while (exception.retry);
3472 return err;
3473}
3474
3475static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3476 struct nfs_fsstat *fsstat)
3477{
3478 struct nfs4_statfs_arg args = {
3479 .fh = fhandle,
3480 .bitmask = server->attr_bitmask,
3481 };
3482 struct nfs4_statfs_res res = {
3483 .fsstat = fsstat,
3484 };
3485 struct rpc_message msg = {
3486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3487 .rpc_argp = &args,
3488 .rpc_resp = &res,
3489 };
3490
3491 nfs_fattr_init(fsstat->fattr);
3492 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3493}
3494
3495static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3496{
3497 struct nfs4_exception exception = { };
3498 int err;
3499 do {
3500 err = nfs4_handle_exception(server,
3501 _nfs4_proc_statfs(server, fhandle, fsstat),
3502 &exception);
3503 } while (exception.retry);
3504 return err;
3505}
3506
3507static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3508 struct nfs_fsinfo *fsinfo)
3509{
3510 struct nfs4_fsinfo_arg args = {
3511 .fh = fhandle,
3512 .bitmask = server->attr_bitmask,
3513 };
3514 struct nfs4_fsinfo_res res = {
3515 .fsinfo = fsinfo,
3516 };
3517 struct rpc_message msg = {
3518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3519 .rpc_argp = &args,
3520 .rpc_resp = &res,
3521 };
3522
3523 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3524}
3525
3526static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3527{
3528 struct nfs4_exception exception = { };
3529 unsigned long now = jiffies;
3530 int err;
3531
3532 do {
3533 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3534 if (err == 0) {
3535 struct nfs_client *clp = server->nfs_client;
3536
3537 spin_lock(&clp->cl_lock);
3538 clp->cl_lease_time = fsinfo->lease_time * HZ;
3539 clp->cl_last_renewal = now;
3540 spin_unlock(&clp->cl_lock);
3541 break;
3542 }
3543 err = nfs4_handle_exception(server, err, &exception);
3544 } while (exception.retry);
3545 return err;
3546}
3547
3548static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3549{
3550 int error;
3551
3552 nfs_fattr_init(fsinfo->fattr);
3553 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3554 if (error == 0) {
3555 /* block layout checks this! */
3556 server->pnfs_blksize = fsinfo->blksize;
3557 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3558 }
3559
3560 return error;
3561}
3562
3563static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3564 struct nfs_pathconf *pathconf)
3565{
3566 struct nfs4_pathconf_arg args = {
3567 .fh = fhandle,
3568 .bitmask = server->attr_bitmask,
3569 };
3570 struct nfs4_pathconf_res res = {
3571 .pathconf = pathconf,
3572 };
3573 struct rpc_message msg = {
3574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3575 .rpc_argp = &args,
3576 .rpc_resp = &res,
3577 };
3578
3579 /* None of the pathconf attributes are mandatory to implement */
3580 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3581 memset(pathconf, 0, sizeof(*pathconf));
3582 return 0;
3583 }
3584
3585 nfs_fattr_init(pathconf->fattr);
3586 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3587}
3588
3589static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3590 struct nfs_pathconf *pathconf)
3591{
3592 struct nfs4_exception exception = { };
3593 int err;
3594
3595 do {
3596 err = nfs4_handle_exception(server,
3597 _nfs4_proc_pathconf(server, fhandle, pathconf),
3598 &exception);
3599 } while (exception.retry);
3600 return err;
3601}
3602
3603int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3604 const struct nfs_open_context *ctx,
3605 const struct nfs_lock_context *l_ctx,
3606 fmode_t fmode)
3607{
3608 const struct nfs_lockowner *lockowner = NULL;
3609
3610 if (l_ctx != NULL)
3611 lockowner = &l_ctx->lockowner;
3612 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
3613}
3614EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3615
3616static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3617 const struct nfs_open_context *ctx,
3618 const struct nfs_lock_context *l_ctx,
3619 fmode_t fmode)
3620{
3621 nfs4_stateid current_stateid;
3622
3623 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3624 return false;
3625 return nfs4_stateid_match(stateid, &current_stateid);
3626}
3627
3628static bool nfs4_error_stateid_expired(int err)
3629{
3630 switch (err) {
3631 case -NFS4ERR_DELEG_REVOKED:
3632 case -NFS4ERR_ADMIN_REVOKED:
3633 case -NFS4ERR_BAD_STATEID:
3634 case -NFS4ERR_STALE_STATEID:
3635 case -NFS4ERR_OLD_STATEID:
3636 case -NFS4ERR_OPENMODE:
3637 case -NFS4ERR_EXPIRED:
3638 return true;
3639 }
3640 return false;
3641}
3642
3643void __nfs4_read_done_cb(struct nfs_read_data *data)
3644{
3645 nfs_invalidate_atime(data->header->inode);
3646}
3647
3648static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3649{
3650 struct nfs_server *server = NFS_SERVER(data->header->inode);
3651
3652 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3653 rpc_restart_call_prepare(task);
3654 return -EAGAIN;
3655 }
3656
3657 __nfs4_read_done_cb(data);
3658 if (task->tk_status > 0)
3659 renew_lease(server, data->timestamp);
3660 return 0;
3661}
3662
3663static bool nfs4_read_stateid_changed(struct rpc_task *task,
3664 struct nfs_readargs *args)
3665{
3666
3667 if (!nfs4_error_stateid_expired(task->tk_status) ||
3668 nfs4_stateid_is_current(&args->stateid,
3669 args->context,
3670 args->lock_context,
3671 FMODE_READ))
3672 return false;
3673 rpc_restart_call_prepare(task);
3674 return true;
3675}
3676
3677static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3678{
3679
3680 dprintk("--> %s\n", __func__);
3681
3682 if (!nfs4_sequence_done(task, &data->res.seq_res))
3683 return -EAGAIN;
3684 if (nfs4_read_stateid_changed(task, &data->args))
3685 return -EAGAIN;
3686 return data->read_done_cb ? data->read_done_cb(task, data) :
3687 nfs4_read_done_cb(task, data);
3688}
3689
3690static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3691{
3692 data->timestamp = jiffies;
3693 data->read_done_cb = nfs4_read_done_cb;
3694 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3695 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3696}
3697
3698static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3699{
3700 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3701 &data->args.seq_args,
3702 &data->res.seq_res,
3703 task))
3704 return;
3705 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3706 data->args.lock_context, FMODE_READ);
3707}
3708
3709static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3710{
3711 struct inode *inode = data->header->inode;
3712
3713 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3714 rpc_restart_call_prepare(task);
3715 return -EAGAIN;
3716 }
3717 if (task->tk_status >= 0) {
3718 renew_lease(NFS_SERVER(inode), data->timestamp);
3719 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3720 }
3721 return 0;
3722}
3723
3724static bool nfs4_write_stateid_changed(struct rpc_task *task,
3725 struct nfs_writeargs *args)
3726{
3727
3728 if (!nfs4_error_stateid_expired(task->tk_status) ||
3729 nfs4_stateid_is_current(&args->stateid,
3730 args->context,
3731 args->lock_context,
3732 FMODE_WRITE))
3733 return false;
3734 rpc_restart_call_prepare(task);
3735 return true;
3736}
3737
3738static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3739{
3740 if (!nfs4_sequence_done(task, &data->res.seq_res))
3741 return -EAGAIN;
3742 if (nfs4_write_stateid_changed(task, &data->args))
3743 return -EAGAIN;
3744 return data->write_done_cb ? data->write_done_cb(task, data) :
3745 nfs4_write_done_cb(task, data);
3746}
3747
3748static
3749bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3750{
3751 const struct nfs_pgio_header *hdr = data->header;
3752
3753 /* Don't request attributes for pNFS or O_DIRECT writes */
3754 if (data->ds_clp != NULL || hdr->dreq != NULL)
3755 return false;
3756 /* Otherwise, request attributes if and only if we don't hold
3757 * a delegation
3758 */
3759 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3760}
3761
3762static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3763{
3764 struct nfs_server *server = NFS_SERVER(data->header->inode);
3765
3766 if (!nfs4_write_need_cache_consistency_data(data)) {
3767 data->args.bitmask = NULL;
3768 data->res.fattr = NULL;
3769 } else
3770 data->args.bitmask = server->cache_consistency_bitmask;
3771
3772 if (!data->write_done_cb)
3773 data->write_done_cb = nfs4_write_done_cb;
3774 data->res.server = server;
3775 data->timestamp = jiffies;
3776
3777 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3778 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3779}
3780
3781static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3782{
3783 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3784 &data->args.seq_args,
3785 &data->res.seq_res,
3786 task))
3787 return;
3788 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3789 data->args.lock_context, FMODE_WRITE);
3790}
3791
3792static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3793{
3794 nfs4_setup_sequence(NFS_SERVER(data->inode),
3795 &data->args.seq_args,
3796 &data->res.seq_res,
3797 task);
3798}
3799
3800static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3801{
3802 struct inode *inode = data->inode;
3803
3804 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3805 rpc_restart_call_prepare(task);
3806 return -EAGAIN;
3807 }
3808 return 0;
3809}
3810
3811static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3812{
3813 if (!nfs4_sequence_done(task, &data->res.seq_res))
3814 return -EAGAIN;
3815 return data->commit_done_cb(task, data);
3816}
3817
3818static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3819{
3820 struct nfs_server *server = NFS_SERVER(data->inode);
3821
3822 if (data->commit_done_cb == NULL)
3823 data->commit_done_cb = nfs4_commit_done_cb;
3824 data->res.server = server;
3825 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3826 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3827}
3828
3829struct nfs4_renewdata {
3830 struct nfs_client *client;
3831 unsigned long timestamp;
3832};
3833
3834/*
3835 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3836 * standalone procedure for queueing an asynchronous RENEW.
3837 */
3838static void nfs4_renew_release(void *calldata)
3839{
3840 struct nfs4_renewdata *data = calldata;
3841 struct nfs_client *clp = data->client;
3842
3843 if (atomic_read(&clp->cl_count) > 1)
3844 nfs4_schedule_state_renewal(clp);
3845 nfs_put_client(clp);
3846 kfree(data);
3847}
3848
3849static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3850{
3851 struct nfs4_renewdata *data = calldata;
3852 struct nfs_client *clp = data->client;
3853 unsigned long timestamp = data->timestamp;
3854
3855 if (task->tk_status < 0) {
3856 /* Unless we're shutting down, schedule state recovery! */
3857 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3858 return;
3859 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3860 nfs4_schedule_lease_recovery(clp);
3861 return;
3862 }
3863 nfs4_schedule_path_down_recovery(clp);
3864 }
3865 do_renew_lease(clp, timestamp);
3866}
3867
3868static const struct rpc_call_ops nfs4_renew_ops = {
3869 .rpc_call_done = nfs4_renew_done,
3870 .rpc_release = nfs4_renew_release,
3871};
3872
3873static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3874{
3875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3877 .rpc_argp = clp,
3878 .rpc_cred = cred,
3879 };
3880 struct nfs4_renewdata *data;
3881
3882 if (renew_flags == 0)
3883 return 0;
3884 if (!atomic_inc_not_zero(&clp->cl_count))
3885 return -EIO;
3886 data = kmalloc(sizeof(*data), GFP_NOFS);
3887 if (data == NULL)
3888 return -ENOMEM;
3889 data->client = clp;
3890 data->timestamp = jiffies;
3891 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
3892 &nfs4_renew_ops, data);
3893}
3894
3895static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3896{
3897 struct rpc_message msg = {
3898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3899 .rpc_argp = clp,
3900 .rpc_cred = cred,
3901 };
3902 unsigned long now = jiffies;
3903 int status;
3904
3905 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3906 if (status < 0)
3907 return status;
3908 do_renew_lease(clp, now);
3909 return 0;
3910}
3911
3912static inline int nfs4_server_supports_acls(struct nfs_server *server)
3913{
3914 return (server->caps & NFS_CAP_ACLS)
3915 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3916 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3917}
3918
3919/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3920 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3921 * the stack.
3922 */
3923#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3924
3925static int buf_to_pages_noslab(const void *buf, size_t buflen,
3926 struct page **pages, unsigned int *pgbase)
3927{
3928 struct page *newpage, **spages;
3929 int rc = 0;
3930 size_t len;
3931 spages = pages;
3932
3933 do {
3934 len = min_t(size_t, PAGE_SIZE, buflen);
3935 newpage = alloc_page(GFP_KERNEL);
3936
3937 if (newpage == NULL)
3938 goto unwind;
3939 memcpy(page_address(newpage), buf, len);
3940 buf += len;
3941 buflen -= len;
3942 *pages++ = newpage;
3943 rc++;
3944 } while (buflen != 0);
3945
3946 return rc;
3947
3948unwind:
3949 for(; rc > 0; rc--)
3950 __free_page(spages[rc-1]);
3951 return -ENOMEM;
3952}
3953
3954struct nfs4_cached_acl {
3955 int cached;
3956 size_t len;
3957 char data[0];
3958};
3959
3960static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3961{
3962 struct nfs_inode *nfsi = NFS_I(inode);
3963
3964 spin_lock(&inode->i_lock);
3965 kfree(nfsi->nfs4_acl);
3966 nfsi->nfs4_acl = acl;
3967 spin_unlock(&inode->i_lock);
3968}
3969
3970static void nfs4_zap_acl_attr(struct inode *inode)
3971{
3972 nfs4_set_cached_acl(inode, NULL);
3973}
3974
3975static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3976{
3977 struct nfs_inode *nfsi = NFS_I(inode);
3978 struct nfs4_cached_acl *acl;
3979 int ret = -ENOENT;
3980
3981 spin_lock(&inode->i_lock);
3982 acl = nfsi->nfs4_acl;
3983 if (acl == NULL)
3984 goto out;
3985 if (buf == NULL) /* user is just asking for length */
3986 goto out_len;
3987 if (acl->cached == 0)
3988 goto out;
3989 ret = -ERANGE; /* see getxattr(2) man page */
3990 if (acl->len > buflen)
3991 goto out;
3992 memcpy(buf, acl->data, acl->len);
3993out_len:
3994 ret = acl->len;
3995out:
3996 spin_unlock(&inode->i_lock);
3997 return ret;
3998}
3999
4000static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4001{
4002 struct nfs4_cached_acl *acl;
4003 size_t buflen = sizeof(*acl) + acl_len;
4004
4005 if (buflen <= PAGE_SIZE) {
4006 acl = kmalloc(buflen, GFP_KERNEL);
4007 if (acl == NULL)
4008 goto out;
4009 acl->cached = 1;
4010 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4011 } else {
4012 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4013 if (acl == NULL)
4014 goto out;
4015 acl->cached = 0;
4016 }
4017 acl->len = acl_len;
4018out:
4019 nfs4_set_cached_acl(inode, acl);
4020}
4021
4022/*
4023 * The getxattr API returns the required buffer length when called with a
4024 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4025 * the required buf. On a NULL buf, we send a page of data to the server
4026 * guessing that the ACL request can be serviced by a page. If so, we cache
4027 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4028 * the cache. If not so, we throw away the page, and cache the required
4029 * length. The next getxattr call will then produce another round trip to
4030 * the server, this time with the input buf of the required size.
4031 */
4032static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4033{
4034 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4035 struct nfs_getaclargs args = {
4036 .fh = NFS_FH(inode),
4037 .acl_pages = pages,
4038 .acl_len = buflen,
4039 };
4040 struct nfs_getaclres res = {
4041 .acl_len = buflen,
4042 };
4043 struct rpc_message msg = {
4044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4045 .rpc_argp = &args,
4046 .rpc_resp = &res,
4047 };
4048 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4049 int ret = -ENOMEM, i;
4050
4051 /* As long as we're doing a round trip to the server anyway,
4052 * let's be prepared for a page of acl data. */
4053 if (npages == 0)
4054 npages = 1;
4055 if (npages > ARRAY_SIZE(pages))
4056 return -ERANGE;
4057
4058 for (i = 0; i < npages; i++) {
4059 pages[i] = alloc_page(GFP_KERNEL);
4060 if (!pages[i])
4061 goto out_free;
4062 }
4063
4064 /* for decoding across pages */
4065 res.acl_scratch = alloc_page(GFP_KERNEL);
4066 if (!res.acl_scratch)
4067 goto out_free;
4068
4069 args.acl_len = npages * PAGE_SIZE;
4070 args.acl_pgbase = 0;
4071
4072 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4073 __func__, buf, buflen, npages, args.acl_len);
4074 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4075 &msg, &args.seq_args, &res.seq_res, 0);
4076 if (ret)
4077 goto out_free;
4078
4079 /* Handle the case where the passed-in buffer is too short */
4080 if (res.acl_flags & NFS4_ACL_TRUNC) {
4081 /* Did the user only issue a request for the acl length? */
4082 if (buf == NULL)
4083 goto out_ok;
4084 ret = -ERANGE;
4085 goto out_free;
4086 }
4087 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4088 if (buf) {
4089 if (res.acl_len > buflen) {
4090 ret = -ERANGE;
4091 goto out_free;
4092 }
4093 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4094 }
4095out_ok:
4096 ret = res.acl_len;
4097out_free:
4098 for (i = 0; i < npages; i++)
4099 if (pages[i])
4100 __free_page(pages[i]);
4101 if (res.acl_scratch)
4102 __free_page(res.acl_scratch);
4103 return ret;
4104}
4105
4106static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4107{
4108 struct nfs4_exception exception = { };
4109 ssize_t ret;
4110 do {
4111 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4112 if (ret >= 0)
4113 break;
4114 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4115 } while (exception.retry);
4116 return ret;
4117}
4118
4119static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4120{
4121 struct nfs_server *server = NFS_SERVER(inode);
4122 int ret;
4123
4124 if (!nfs4_server_supports_acls(server))
4125 return -EOPNOTSUPP;
4126 ret = nfs_revalidate_inode(server, inode);
4127 if (ret < 0)
4128 return ret;
4129 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4130 nfs_zap_acl_cache(inode);
4131 ret = nfs4_read_cached_acl(inode, buf, buflen);
4132 if (ret != -ENOENT)
4133 /* -ENOENT is returned if there is no ACL or if there is an ACL
4134 * but no cached acl data, just the acl length */
4135 return ret;
4136 return nfs4_get_acl_uncached(inode, buf, buflen);
4137}
4138
4139static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4140{
4141 struct nfs_server *server = NFS_SERVER(inode);
4142 struct page *pages[NFS4ACL_MAXPAGES];
4143 struct nfs_setaclargs arg = {
4144 .fh = NFS_FH(inode),
4145 .acl_pages = pages,
4146 .acl_len = buflen,
4147 };
4148 struct nfs_setaclres res;
4149 struct rpc_message msg = {
4150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4151 .rpc_argp = &arg,
4152 .rpc_resp = &res,
4153 };
4154 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4155 int ret, i;
4156
4157 if (!nfs4_server_supports_acls(server))
4158 return -EOPNOTSUPP;
4159 if (npages > ARRAY_SIZE(pages))
4160 return -ERANGE;
4161 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4162 if (i < 0)
4163 return i;
4164 nfs4_inode_return_delegation(inode);
4165 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4166
4167 /*
4168 * Free each page after tx, so the only ref left is
4169 * held by the network stack
4170 */
4171 for (; i > 0; i--)
4172 put_page(pages[i-1]);
4173
4174 /*
4175 * Acl update can result in inode attribute update.
4176 * so mark the attribute cache invalid.
4177 */
4178 spin_lock(&inode->i_lock);
4179 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4180 spin_unlock(&inode->i_lock);
4181 nfs_access_zap_cache(inode);
4182 nfs_zap_acl_cache(inode);
4183 return ret;
4184}
4185
4186static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4187{
4188 struct nfs4_exception exception = { };
4189 int err;
4190 do {
4191 err = nfs4_handle_exception(NFS_SERVER(inode),
4192 __nfs4_proc_set_acl(inode, buf, buflen),
4193 &exception);
4194 } while (exception.retry);
4195 return err;
4196}
4197
4198static int
4199nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4200{
4201 struct nfs_client *clp = server->nfs_client;
4202
4203 if (task->tk_status >= 0)
4204 return 0;
4205 switch(task->tk_status) {
4206 case -NFS4ERR_DELEG_REVOKED:
4207 case -NFS4ERR_ADMIN_REVOKED:
4208 case -NFS4ERR_BAD_STATEID:
4209 if (state == NULL)
4210 break;
4211 nfs_remove_bad_delegation(state->inode);
4212 case -NFS4ERR_OPENMODE:
4213 if (state == NULL)
4214 break;
4215 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4216 goto stateid_invalid;
4217 goto wait_on_recovery;
4218 case -NFS4ERR_EXPIRED:
4219 if (state != NULL) {
4220 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4221 goto stateid_invalid;
4222 }
4223 case -NFS4ERR_STALE_STATEID:
4224 case -NFS4ERR_STALE_CLIENTID:
4225 nfs4_schedule_lease_recovery(clp);
4226 goto wait_on_recovery;
4227#if defined(CONFIG_NFS_V4_1)
4228 case -NFS4ERR_BADSESSION:
4229 case -NFS4ERR_BADSLOT:
4230 case -NFS4ERR_BAD_HIGH_SLOT:
4231 case -NFS4ERR_DEADSESSION:
4232 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4233 case -NFS4ERR_SEQ_FALSE_RETRY:
4234 case -NFS4ERR_SEQ_MISORDERED:
4235 dprintk("%s ERROR %d, Reset session\n", __func__,
4236 task->tk_status);
4237 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4238 task->tk_status = 0;
4239 return -EAGAIN;
4240#endif /* CONFIG_NFS_V4_1 */
4241 case -NFS4ERR_DELAY:
4242 nfs_inc_server_stats(server, NFSIOS_DELAY);
4243 case -NFS4ERR_GRACE:
4244 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4245 task->tk_status = 0;
4246 return -EAGAIN;
4247 case -NFS4ERR_RETRY_UNCACHED_REP:
4248 case -NFS4ERR_OLD_STATEID:
4249 task->tk_status = 0;
4250 return -EAGAIN;
4251 }
4252 task->tk_status = nfs4_map_errors(task->tk_status);
4253 return 0;
4254stateid_invalid:
4255 task->tk_status = -EIO;
4256 return 0;
4257wait_on_recovery:
4258 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4259 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4260 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4261 task->tk_status = 0;
4262 return -EAGAIN;
4263}
4264
4265static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4266 nfs4_verifier *bootverf)
4267{
4268 __be32 verf[2];
4269
4270 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4271 /* An impossible timestamp guarantees this value
4272 * will never match a generated boot time. */
4273 verf[0] = 0;
4274 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4275 } else {
4276 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4277 verf[0] = (__be32)nn->boot_time.tv_sec;
4278 verf[1] = (__be32)nn->boot_time.tv_nsec;
4279 }
4280 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4281}
4282
4283static unsigned int
4284nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4285 char *buf, size_t len)
4286{
4287 unsigned int result;
4288
4289 rcu_read_lock();
4290 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4291 clp->cl_ipaddr,
4292 rpc_peeraddr2str(clp->cl_rpcclient,
4293 RPC_DISPLAY_ADDR),
4294 rpc_peeraddr2str(clp->cl_rpcclient,
4295 RPC_DISPLAY_PROTO));
4296 rcu_read_unlock();
4297 return result;
4298}
4299
4300static unsigned int
4301nfs4_init_uniform_client_string(const struct nfs_client *clp,
4302 char *buf, size_t len)
4303{
4304 char *nodename = clp->cl_rpcclient->cl_nodename;
4305
4306 if (nfs4_client_id_uniquifier[0] != '\0')
4307 nodename = nfs4_client_id_uniquifier;
4308 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4309 clp->rpc_ops->version, clp->cl_minorversion,
4310 nodename);
4311}
4312
4313/**
4314 * nfs4_proc_setclientid - Negotiate client ID
4315 * @clp: state data structure
4316 * @program: RPC program for NFSv4 callback service
4317 * @port: IP port number for NFS4 callback service
4318 * @cred: RPC credential to use for this call
4319 * @res: where to place the result
4320 *
4321 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4322 */
4323int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4324 unsigned short port, struct rpc_cred *cred,
4325 struct nfs4_setclientid_res *res)
4326{
4327 nfs4_verifier sc_verifier;
4328 struct nfs4_setclientid setclientid = {
4329 .sc_verifier = &sc_verifier,
4330 .sc_prog = program,
4331 .sc_cb_ident = clp->cl_cb_ident,
4332 };
4333 struct rpc_message msg = {
4334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4335 .rpc_argp = &setclientid,
4336 .rpc_resp = res,
4337 .rpc_cred = cred,
4338 };
4339 int status;
4340
4341 /* nfs_client_id4 */
4342 nfs4_init_boot_verifier(clp, &sc_verifier);
4343 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4344 setclientid.sc_name_len =
4345 nfs4_init_uniform_client_string(clp,
4346 setclientid.sc_name,
4347 sizeof(setclientid.sc_name));
4348 else
4349 setclientid.sc_name_len =
4350 nfs4_init_nonuniform_client_string(clp,
4351 setclientid.sc_name,
4352 sizeof(setclientid.sc_name));
4353 /* cb_client4 */
4354 rcu_read_lock();
4355 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4356 sizeof(setclientid.sc_netid),
4357 rpc_peeraddr2str(clp->cl_rpcclient,
4358 RPC_DISPLAY_NETID));
4359 rcu_read_unlock();
4360 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4361 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4362 clp->cl_ipaddr, port >> 8, port & 255);
4363
4364 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4365 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4366 setclientid.sc_name_len, setclientid.sc_name);
4367 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4368 dprintk("NFS reply setclientid: %d\n", status);
4369 return status;
4370}
4371
4372/**
4373 * nfs4_proc_setclientid_confirm - Confirm client ID
4374 * @clp: state data structure
4375 * @res: result of a previous SETCLIENTID
4376 * @cred: RPC credential to use for this call
4377 *
4378 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4379 */
4380int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4381 struct nfs4_setclientid_res *arg,
4382 struct rpc_cred *cred)
4383{
4384 struct rpc_message msg = {
4385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4386 .rpc_argp = arg,
4387 .rpc_cred = cred,
4388 };
4389 int status;
4390
4391 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4392 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4393 clp->cl_clientid);
4394 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4395 dprintk("NFS reply setclientid_confirm: %d\n", status);
4396 return status;
4397}
4398
4399struct nfs4_delegreturndata {
4400 struct nfs4_delegreturnargs args;
4401 struct nfs4_delegreturnres res;
4402 struct nfs_fh fh;
4403 nfs4_stateid stateid;
4404 unsigned long timestamp;
4405 struct nfs_fattr fattr;
4406 int rpc_status;
4407};
4408
4409static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4410{
4411 struct nfs4_delegreturndata *data = calldata;
4412
4413 if (!nfs4_sequence_done(task, &data->res.seq_res))
4414 return;
4415
4416 switch (task->tk_status) {
4417 case -NFS4ERR_STALE_STATEID:
4418 case -NFS4ERR_EXPIRED:
4419 case 0:
4420 renew_lease(data->res.server, data->timestamp);
4421 break;
4422 default:
4423 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4424 -EAGAIN) {
4425 rpc_restart_call_prepare(task);
4426 return;
4427 }
4428 }
4429 data->rpc_status = task->tk_status;
4430}
4431
4432static void nfs4_delegreturn_release(void *calldata)
4433{
4434 kfree(calldata);
4435}
4436
4437#if defined(CONFIG_NFS_V4_1)
4438static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4439{
4440 struct nfs4_delegreturndata *d_data;
4441
4442 d_data = (struct nfs4_delegreturndata *)data;
4443
4444 nfs4_setup_sequence(d_data->res.server,
4445 &d_data->args.seq_args,
4446 &d_data->res.seq_res,
4447 task);
4448}
4449#endif /* CONFIG_NFS_V4_1 */
4450
4451static const struct rpc_call_ops nfs4_delegreturn_ops = {
4452#if defined(CONFIG_NFS_V4_1)
4453 .rpc_call_prepare = nfs4_delegreturn_prepare,
4454#endif /* CONFIG_NFS_V4_1 */
4455 .rpc_call_done = nfs4_delegreturn_done,
4456 .rpc_release = nfs4_delegreturn_release,
4457};
4458
4459static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4460{
4461 struct nfs4_delegreturndata *data;
4462 struct nfs_server *server = NFS_SERVER(inode);
4463 struct rpc_task *task;
4464 struct rpc_message msg = {
4465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4466 .rpc_cred = cred,
4467 };
4468 struct rpc_task_setup task_setup_data = {
4469 .rpc_client = server->client,
4470 .rpc_message = &msg,
4471 .callback_ops = &nfs4_delegreturn_ops,
4472 .flags = RPC_TASK_ASYNC,
4473 };
4474 int status = 0;
4475
4476 data = kzalloc(sizeof(*data), GFP_NOFS);
4477 if (data == NULL)
4478 return -ENOMEM;
4479 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4480 data->args.fhandle = &data->fh;
4481 data->args.stateid = &data->stateid;
4482 data->args.bitmask = server->cache_consistency_bitmask;
4483 nfs_copy_fh(&data->fh, NFS_FH(inode));
4484 nfs4_stateid_copy(&data->stateid, stateid);
4485 data->res.fattr = &data->fattr;
4486 data->res.server = server;
4487 nfs_fattr_init(data->res.fattr);
4488 data->timestamp = jiffies;
4489 data->rpc_status = 0;
4490
4491 task_setup_data.callback_data = data;
4492 msg.rpc_argp = &data->args;
4493 msg.rpc_resp = &data->res;
4494 task = rpc_run_task(&task_setup_data);
4495 if (IS_ERR(task))
4496 return PTR_ERR(task);
4497 if (!issync)
4498 goto out;
4499 status = nfs4_wait_for_completion_rpc_task(task);
4500 if (status != 0)
4501 goto out;
4502 status = data->rpc_status;
4503 if (status == 0)
4504 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4505 else
4506 nfs_refresh_inode(inode, &data->fattr);
4507out:
4508 rpc_put_task(task);
4509 return status;
4510}
4511
4512int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4513{
4514 struct nfs_server *server = NFS_SERVER(inode);
4515 struct nfs4_exception exception = { };
4516 int err;
4517 do {
4518 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4519 switch (err) {
4520 case -NFS4ERR_STALE_STATEID:
4521 case -NFS4ERR_EXPIRED:
4522 case 0:
4523 return 0;
4524 }
4525 err = nfs4_handle_exception(server, err, &exception);
4526 } while (exception.retry);
4527 return err;
4528}
4529
4530#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4531#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4532
4533/*
4534 * sleep, with exponential backoff, and retry the LOCK operation.
4535 */
4536static unsigned long
4537nfs4_set_lock_task_retry(unsigned long timeout)
4538{
4539 freezable_schedule_timeout_killable(timeout);
4540 timeout <<= 1;
4541 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4542 return NFS4_LOCK_MAXTIMEOUT;
4543 return timeout;
4544}
4545
4546static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4547{
4548 struct inode *inode = state->inode;
4549 struct nfs_server *server = NFS_SERVER(inode);
4550 struct nfs_client *clp = server->nfs_client;
4551 struct nfs_lockt_args arg = {
4552 .fh = NFS_FH(inode),
4553 .fl = request,
4554 };
4555 struct nfs_lockt_res res = {
4556 .denied = request,
4557 };
4558 struct rpc_message msg = {
4559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4560 .rpc_argp = &arg,
4561 .rpc_resp = &res,
4562 .rpc_cred = state->owner->so_cred,
4563 };
4564 struct nfs4_lock_state *lsp;
4565 int status;
4566
4567 arg.lock_owner.clientid = clp->cl_clientid;
4568 status = nfs4_set_lock_state(state, request);
4569 if (status != 0)
4570 goto out;
4571 lsp = request->fl_u.nfs4_fl.owner;
4572 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4573 arg.lock_owner.s_dev = server->s_dev;
4574 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4575 switch (status) {
4576 case 0:
4577 request->fl_type = F_UNLCK;
4578 break;
4579 case -NFS4ERR_DENIED:
4580 status = 0;
4581 }
4582 request->fl_ops->fl_release_private(request);
4583out:
4584 return status;
4585}
4586
4587static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4588{
4589 struct nfs4_exception exception = { };
4590 int err;
4591
4592 do {
4593 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4594 _nfs4_proc_getlk(state, cmd, request),
4595 &exception);
4596 } while (exception.retry);
4597 return err;
4598}
4599
4600static int do_vfs_lock(struct file *file, struct file_lock *fl)
4601{
4602 int res = 0;
4603 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4604 case FL_POSIX:
4605 res = posix_lock_file_wait(file, fl);
4606 break;
4607 case FL_FLOCK:
4608 res = flock_lock_file_wait(file, fl);
4609 break;
4610 default:
4611 BUG();
4612 }
4613 return res;
4614}
4615
4616struct nfs4_unlockdata {
4617 struct nfs_locku_args arg;
4618 struct nfs_locku_res res;
4619 struct nfs4_lock_state *lsp;
4620 struct nfs_open_context *ctx;
4621 struct file_lock fl;
4622 const struct nfs_server *server;
4623 unsigned long timestamp;
4624};
4625
4626static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4627 struct nfs_open_context *ctx,
4628 struct nfs4_lock_state *lsp,
4629 struct nfs_seqid *seqid)
4630{
4631 struct nfs4_unlockdata *p;
4632 struct inode *inode = lsp->ls_state->inode;
4633
4634 p = kzalloc(sizeof(*p), GFP_NOFS);
4635 if (p == NULL)
4636 return NULL;
4637 p->arg.fh = NFS_FH(inode);
4638 p->arg.fl = &p->fl;
4639 p->arg.seqid = seqid;
4640 p->res.seqid = seqid;
4641 p->arg.stateid = &lsp->ls_stateid;
4642 p->lsp = lsp;
4643 atomic_inc(&lsp->ls_count);
4644 /* Ensure we don't close file until we're done freeing locks! */
4645 p->ctx = get_nfs_open_context(ctx);
4646 memcpy(&p->fl, fl, sizeof(p->fl));
4647 p->server = NFS_SERVER(inode);
4648 return p;
4649}
4650
4651static void nfs4_locku_release_calldata(void *data)
4652{
4653 struct nfs4_unlockdata *calldata = data;
4654 nfs_free_seqid(calldata->arg.seqid);
4655 nfs4_put_lock_state(calldata->lsp);
4656 put_nfs_open_context(calldata->ctx);
4657 kfree(calldata);
4658}
4659
4660static void nfs4_locku_done(struct rpc_task *task, void *data)
4661{
4662 struct nfs4_unlockdata *calldata = data;
4663
4664 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4665 return;
4666 switch (task->tk_status) {
4667 case 0:
4668 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4669 &calldata->res.stateid);
4670 renew_lease(calldata->server, calldata->timestamp);
4671 break;
4672 case -NFS4ERR_BAD_STATEID:
4673 case -NFS4ERR_OLD_STATEID:
4674 case -NFS4ERR_STALE_STATEID:
4675 case -NFS4ERR_EXPIRED:
4676 break;
4677 default:
4678 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4679 rpc_restart_call_prepare(task);
4680 }
4681 nfs_release_seqid(calldata->arg.seqid);
4682}
4683
4684static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4685{
4686 struct nfs4_unlockdata *calldata = data;
4687
4688 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4689 goto out_wait;
4690 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4691 /* Note: exit _without_ running nfs4_locku_done */
4692 goto out_no_action;
4693 }
4694 calldata->timestamp = jiffies;
4695 if (nfs4_setup_sequence(calldata->server,
4696 &calldata->arg.seq_args,
4697 &calldata->res.seq_res,
4698 task) != 0)
4699 nfs_release_seqid(calldata->arg.seqid);
4700 return;
4701out_no_action:
4702 task->tk_action = NULL;
4703out_wait:
4704 nfs4_sequence_done(task, &calldata->res.seq_res);
4705}
4706
4707static const struct rpc_call_ops nfs4_locku_ops = {
4708 .rpc_call_prepare = nfs4_locku_prepare,
4709 .rpc_call_done = nfs4_locku_done,
4710 .rpc_release = nfs4_locku_release_calldata,
4711};
4712
4713static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4714 struct nfs_open_context *ctx,
4715 struct nfs4_lock_state *lsp,
4716 struct nfs_seqid *seqid)
4717{
4718 struct nfs4_unlockdata *data;
4719 struct rpc_message msg = {
4720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4721 .rpc_cred = ctx->cred,
4722 };
4723 struct rpc_task_setup task_setup_data = {
4724 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4725 .rpc_message = &msg,
4726 .callback_ops = &nfs4_locku_ops,
4727 .workqueue = nfsiod_workqueue,
4728 .flags = RPC_TASK_ASYNC,
4729 };
4730
4731 /* Ensure this is an unlock - when canceling a lock, the
4732 * canceled lock is passed in, and it won't be an unlock.
4733 */
4734 fl->fl_type = F_UNLCK;
4735
4736 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4737 if (data == NULL) {
4738 nfs_free_seqid(seqid);
4739 return ERR_PTR(-ENOMEM);
4740 }
4741
4742 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4743 msg.rpc_argp = &data->arg;
4744 msg.rpc_resp = &data->res;
4745 task_setup_data.callback_data = data;
4746 return rpc_run_task(&task_setup_data);
4747}
4748
4749static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4750{
4751 struct inode *inode = state->inode;
4752 struct nfs4_state_owner *sp = state->owner;
4753 struct nfs_inode *nfsi = NFS_I(inode);
4754 struct nfs_seqid *seqid;
4755 struct nfs4_lock_state *lsp;
4756 struct rpc_task *task;
4757 int status = 0;
4758 unsigned char fl_flags = request->fl_flags;
4759
4760 status = nfs4_set_lock_state(state, request);
4761 /* Unlock _before_ we do the RPC call */
4762 request->fl_flags |= FL_EXISTS;
4763 /* Exclude nfs_delegation_claim_locks() */
4764 mutex_lock(&sp->so_delegreturn_mutex);
4765 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
4766 down_read(&nfsi->rwsem);
4767 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4768 up_read(&nfsi->rwsem);
4769 mutex_unlock(&sp->so_delegreturn_mutex);
4770 goto out;
4771 }
4772 up_read(&nfsi->rwsem);
4773 mutex_unlock(&sp->so_delegreturn_mutex);
4774 if (status != 0)
4775 goto out;
4776 /* Is this a delegated lock? */
4777 lsp = request->fl_u.nfs4_fl.owner;
4778 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
4779 goto out;
4780 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4781 status = -ENOMEM;
4782 if (seqid == NULL)
4783 goto out;
4784 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4785 status = PTR_ERR(task);
4786 if (IS_ERR(task))
4787 goto out;
4788 status = nfs4_wait_for_completion_rpc_task(task);
4789 rpc_put_task(task);
4790out:
4791 request->fl_flags = fl_flags;
4792 return status;
4793}
4794
4795struct nfs4_lockdata {
4796 struct nfs_lock_args arg;
4797 struct nfs_lock_res res;
4798 struct nfs4_lock_state *lsp;
4799 struct nfs_open_context *ctx;
4800 struct file_lock fl;
4801 unsigned long timestamp;
4802 int rpc_status;
4803 int cancelled;
4804 struct nfs_server *server;
4805};
4806
4807static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4808 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4809 gfp_t gfp_mask)
4810{
4811 struct nfs4_lockdata *p;
4812 struct inode *inode = lsp->ls_state->inode;
4813 struct nfs_server *server = NFS_SERVER(inode);
4814
4815 p = kzalloc(sizeof(*p), gfp_mask);
4816 if (p == NULL)
4817 return NULL;
4818
4819 p->arg.fh = NFS_FH(inode);
4820 p->arg.fl = &p->fl;
4821 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4822 if (p->arg.open_seqid == NULL)
4823 goto out_free;
4824 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4825 if (p->arg.lock_seqid == NULL)
4826 goto out_free_seqid;
4827 p->arg.lock_stateid = &lsp->ls_stateid;
4828 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4829 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4830 p->arg.lock_owner.s_dev = server->s_dev;
4831 p->res.lock_seqid = p->arg.lock_seqid;
4832 p->lsp = lsp;
4833 p->server = server;
4834 atomic_inc(&lsp->ls_count);
4835 p->ctx = get_nfs_open_context(ctx);
4836 memcpy(&p->fl, fl, sizeof(p->fl));
4837 return p;
4838out_free_seqid:
4839 nfs_free_seqid(p->arg.open_seqid);
4840out_free:
4841 kfree(p);
4842 return NULL;
4843}
4844
4845static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4846{
4847 struct nfs4_lockdata *data = calldata;
4848 struct nfs4_state *state = data->lsp->ls_state;
4849
4850 dprintk("%s: begin!\n", __func__);
4851 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4852 goto out_wait;
4853 /* Do we need to do an open_to_lock_owner? */
4854 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4855 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4856 goto out_release_lock_seqid;
4857 }
4858 data->arg.open_stateid = &state->open_stateid;
4859 data->arg.new_lock_owner = 1;
4860 data->res.open_seqid = data->arg.open_seqid;
4861 } else
4862 data->arg.new_lock_owner = 0;
4863 if (!nfs4_valid_open_stateid(state)) {
4864 data->rpc_status = -EBADF;
4865 task->tk_action = NULL;
4866 goto out_release_open_seqid;
4867 }
4868 data->timestamp = jiffies;
4869 if (nfs4_setup_sequence(data->server,
4870 &data->arg.seq_args,
4871 &data->res.seq_res,
4872 task) == 0)
4873 return;
4874out_release_open_seqid:
4875 nfs_release_seqid(data->arg.open_seqid);
4876out_release_lock_seqid:
4877 nfs_release_seqid(data->arg.lock_seqid);
4878out_wait:
4879 nfs4_sequence_done(task, &data->res.seq_res);
4880 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4881}
4882
4883static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4884{
4885 struct nfs4_lockdata *data = calldata;
4886
4887 dprintk("%s: begin!\n", __func__);
4888
4889 if (!nfs4_sequence_done(task, &data->res.seq_res))
4890 return;
4891
4892 data->rpc_status = task->tk_status;
4893 if (data->arg.new_lock_owner != 0) {
4894 if (data->rpc_status == 0)
4895 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4896 else
4897 goto out;
4898 }
4899 if (data->rpc_status == 0) {
4900 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4901 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4902 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4903 }
4904out:
4905 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4906}
4907
4908static void nfs4_lock_release(void *calldata)
4909{
4910 struct nfs4_lockdata *data = calldata;
4911
4912 dprintk("%s: begin!\n", __func__);
4913 nfs_free_seqid(data->arg.open_seqid);
4914 if (data->cancelled != 0) {
4915 struct rpc_task *task;
4916 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4917 data->arg.lock_seqid);
4918 if (!IS_ERR(task))
4919 rpc_put_task_async(task);
4920 dprintk("%s: cancelling lock!\n", __func__);
4921 } else
4922 nfs_free_seqid(data->arg.lock_seqid);
4923 nfs4_put_lock_state(data->lsp);
4924 put_nfs_open_context(data->ctx);
4925 kfree(data);
4926 dprintk("%s: done!\n", __func__);
4927}
4928
4929static const struct rpc_call_ops nfs4_lock_ops = {
4930 .rpc_call_prepare = nfs4_lock_prepare,
4931 .rpc_call_done = nfs4_lock_done,
4932 .rpc_release = nfs4_lock_release,
4933};
4934
4935static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4936{
4937 switch (error) {
4938 case -NFS4ERR_ADMIN_REVOKED:
4939 case -NFS4ERR_BAD_STATEID:
4940 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4941 if (new_lock_owner != 0 ||
4942 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4943 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4944 break;
4945 case -NFS4ERR_STALE_STATEID:
4946 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4947 case -NFS4ERR_EXPIRED:
4948 nfs4_schedule_lease_recovery(server->nfs_client);
4949 };
4950}
4951
4952static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4953{
4954 struct nfs4_lockdata *data;
4955 struct rpc_task *task;
4956 struct rpc_message msg = {
4957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4958 .rpc_cred = state->owner->so_cred,
4959 };
4960 struct rpc_task_setup task_setup_data = {
4961 .rpc_client = NFS_CLIENT(state->inode),
4962 .rpc_message = &msg,
4963 .callback_ops = &nfs4_lock_ops,
4964 .workqueue = nfsiod_workqueue,
4965 .flags = RPC_TASK_ASYNC,
4966 };
4967 int ret;
4968
4969 dprintk("%s: begin!\n", __func__);
4970 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4971 fl->fl_u.nfs4_fl.owner,
4972 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4973 if (data == NULL)
4974 return -ENOMEM;
4975 if (IS_SETLKW(cmd))
4976 data->arg.block = 1;
4977 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4978 msg.rpc_argp = &data->arg;
4979 msg.rpc_resp = &data->res;
4980 task_setup_data.callback_data = data;
4981 if (recovery_type > NFS_LOCK_NEW) {
4982 if (recovery_type == NFS_LOCK_RECLAIM)
4983 data->arg.reclaim = NFS_LOCK_RECLAIM;
4984 nfs4_set_sequence_privileged(&data->arg.seq_args);
4985 }
4986 task = rpc_run_task(&task_setup_data);
4987 if (IS_ERR(task))
4988 return PTR_ERR(task);
4989 ret = nfs4_wait_for_completion_rpc_task(task);
4990 if (ret == 0) {
4991 ret = data->rpc_status;
4992 if (ret)
4993 nfs4_handle_setlk_error(data->server, data->lsp,
4994 data->arg.new_lock_owner, ret);
4995 } else
4996 data->cancelled = 1;
4997 rpc_put_task(task);
4998 dprintk("%s: done, ret = %d!\n", __func__, ret);
4999 return ret;
5000}
5001
5002static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5003{
5004 struct nfs_server *server = NFS_SERVER(state->inode);
5005 struct nfs4_exception exception = {
5006 .inode = state->inode,
5007 };
5008 int err;
5009
5010 do {
5011 /* Cache the lock if possible... */
5012 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5013 return 0;
5014 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5015 if (err != -NFS4ERR_DELAY)
5016 break;
5017 nfs4_handle_exception(server, err, &exception);
5018 } while (exception.retry);
5019 return err;
5020}
5021
5022static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5023{
5024 struct nfs_server *server = NFS_SERVER(state->inode);
5025 struct nfs4_exception exception = {
5026 .inode = state->inode,
5027 };
5028 int err;
5029
5030 err = nfs4_set_lock_state(state, request);
5031 if (err != 0)
5032 return err;
5033 do {
5034 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5035 return 0;
5036 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5037 switch (err) {
5038 default:
5039 goto out;
5040 case -NFS4ERR_GRACE:
5041 case -NFS4ERR_DELAY:
5042 nfs4_handle_exception(server, err, &exception);
5043 err = 0;
5044 }
5045 } while (exception.retry);
5046out:
5047 return err;
5048}
5049
5050#if defined(CONFIG_NFS_V4_1)
5051/**
5052 * nfs41_check_expired_locks - possibly free a lock stateid
5053 *
5054 * @state: NFSv4 state for an inode
5055 *
5056 * Returns NFS_OK if recovery for this stateid is now finished.
5057 * Otherwise a negative NFS4ERR value is returned.
5058 */
5059static int nfs41_check_expired_locks(struct nfs4_state *state)
5060{
5061 int status, ret = -NFS4ERR_BAD_STATEID;
5062 struct nfs4_lock_state *lsp;
5063 struct nfs_server *server = NFS_SERVER(state->inode);
5064
5065 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5066 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5067 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5068
5069 status = nfs41_test_stateid(server,
5070 &lsp->ls_stateid,
5071 cred);
5072 if (status != NFS_OK) {
5073 /* Free the stateid unless the server
5074 * informs us the stateid is unrecognized. */
5075 if (status != -NFS4ERR_BAD_STATEID)
5076 nfs41_free_stateid(server,
5077 &lsp->ls_stateid,
5078 cred);
5079 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5080 ret = status;
5081 }
5082 }
5083 };
5084
5085 return ret;
5086}
5087
5088static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5089{
5090 int status = NFS_OK;
5091
5092 if (test_bit(LK_STATE_IN_USE, &state->flags))
5093 status = nfs41_check_expired_locks(state);
5094 if (status != NFS_OK)
5095 status = nfs4_lock_expired(state, request);
5096 return status;
5097}
5098#endif
5099
5100static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5101{
5102 struct nfs4_state_owner *sp = state->owner;
5103 struct nfs_inode *nfsi = NFS_I(state->inode);
5104 unsigned char fl_flags = request->fl_flags;
5105 unsigned int seq;
5106 int status = -ENOLCK;
5107
5108 if ((fl_flags & FL_POSIX) &&
5109 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5110 goto out;
5111 /* Is this a delegated open? */
5112 status = nfs4_set_lock_state(state, request);
5113 if (status != 0)
5114 goto out;
5115 request->fl_flags |= FL_ACCESS;
5116 status = do_vfs_lock(request->fl_file, request);
5117 if (status < 0)
5118 goto out;
5119 down_read(&nfsi->rwsem);
5120 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5121 /* Yes: cache locks! */
5122 /* ...but avoid races with delegation recall... */
5123 request->fl_flags = fl_flags & ~FL_SLEEP;
5124 status = do_vfs_lock(request->fl_file, request);
5125 goto out_unlock;
5126 }
5127 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5128 up_read(&nfsi->rwsem);
5129 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5130 if (status != 0)
5131 goto out;
5132 down_read(&nfsi->rwsem);
5133 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5134 status = -NFS4ERR_DELAY;
5135 goto out_unlock;
5136 }
5137 /* Note: we always want to sleep here! */
5138 request->fl_flags = fl_flags | FL_SLEEP;
5139 if (do_vfs_lock(request->fl_file, request) < 0)
5140 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5141 "manager!\n", __func__);
5142out_unlock:
5143 up_read(&nfsi->rwsem);
5144out:
5145 request->fl_flags = fl_flags;
5146 return status;
5147}
5148
5149static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5150{
5151 struct nfs4_exception exception = {
5152 .state = state,
5153 .inode = state->inode,
5154 };
5155 int err;
5156
5157 do {
5158 err = _nfs4_proc_setlk(state, cmd, request);
5159 if (err == -NFS4ERR_DENIED)
5160 err = -EAGAIN;
5161 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5162 err, &exception);
5163 } while (exception.retry);
5164 return err;
5165}
5166
5167static int
5168nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5169{
5170 struct nfs_open_context *ctx;
5171 struct nfs4_state *state;
5172 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5173 int status;
5174
5175 /* verify open state */
5176 ctx = nfs_file_open_context(filp);
5177 state = ctx->state;
5178
5179 if (request->fl_start < 0 || request->fl_end < 0)
5180 return -EINVAL;
5181
5182 if (IS_GETLK(cmd)) {
5183 if (state != NULL)
5184 return nfs4_proc_getlk(state, F_GETLK, request);
5185 return 0;
5186 }
5187
5188 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5189 return -EINVAL;
5190
5191 if (request->fl_type == F_UNLCK) {
5192 if (state != NULL)
5193 return nfs4_proc_unlck(state, cmd, request);
5194 return 0;
5195 }
5196
5197 if (state == NULL)
5198 return -ENOLCK;
5199 /*
5200 * Don't rely on the VFS having checked the file open mode,
5201 * since it won't do this for flock() locks.
5202 */
5203 switch (request->fl_type) {
5204 case F_RDLCK:
5205 if (!(filp->f_mode & FMODE_READ))
5206 return -EBADF;
5207 break;
5208 case F_WRLCK:
5209 if (!(filp->f_mode & FMODE_WRITE))
5210 return -EBADF;
5211 }
5212
5213 do {
5214 status = nfs4_proc_setlk(state, cmd, request);
5215 if ((status != -EAGAIN) || IS_SETLK(cmd))
5216 break;
5217 timeout = nfs4_set_lock_task_retry(timeout);
5218 status = -ERESTARTSYS;
5219 if (signalled())
5220 break;
5221 } while(status < 0);
5222 return status;
5223}
5224
5225int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5226{
5227 struct nfs_server *server = NFS_SERVER(state->inode);
5228 int err;
5229
5230 err = nfs4_set_lock_state(state, fl);
5231 if (err != 0)
5232 return err;
5233 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5234 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5235}
5236
5237struct nfs_release_lockowner_data {
5238 struct nfs4_lock_state *lsp;
5239 struct nfs_server *server;
5240 struct nfs_release_lockowner_args args;
5241};
5242
5243static void nfs4_release_lockowner_release(void *calldata)
5244{
5245 struct nfs_release_lockowner_data *data = calldata;
5246 nfs4_free_lock_state(data->server, data->lsp);
5247 kfree(calldata);
5248}
5249
5250static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5251 .rpc_release = nfs4_release_lockowner_release,
5252};
5253
5254static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5255{
5256 struct nfs_release_lockowner_data *data;
5257 struct rpc_message msg = {
5258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5259 };
5260
5261 if (server->nfs_client->cl_mvops->minor_version != 0)
5262 return -EINVAL;
5263 data = kmalloc(sizeof(*data), GFP_NOFS);
5264 if (!data)
5265 return -ENOMEM;
5266 data->lsp = lsp;
5267 data->server = server;
5268 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5269 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5270 data->args.lock_owner.s_dev = server->s_dev;
5271 msg.rpc_argp = &data->args;
5272 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5273 return 0;
5274}
5275
5276#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5277
5278static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5279 const void *buf, size_t buflen,
5280 int flags, int type)
5281{
5282 if (strcmp(key, "") != 0)
5283 return -EINVAL;
5284
5285 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5286}
5287
5288static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5289 void *buf, size_t buflen, int type)
5290{
5291 if (strcmp(key, "") != 0)
5292 return -EINVAL;
5293
5294 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5295}
5296
5297static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5298 size_t list_len, const char *name,
5299 size_t name_len, int type)
5300{
5301 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5302
5303 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5304 return 0;
5305
5306 if (list && len <= list_len)
5307 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5308 return len;
5309}
5310
5311/*
5312 * nfs_fhget will use either the mounted_on_fileid or the fileid
5313 */
5314static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5315{
5316 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5317 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5318 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5319 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5320 return;
5321
5322 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5323 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5324 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5325 fattr->nlink = 2;
5326}
5327
5328static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5329 const struct qstr *name,
5330 struct nfs4_fs_locations *fs_locations,
5331 struct page *page)
5332{
5333 struct nfs_server *server = NFS_SERVER(dir);
5334 u32 bitmask[2] = {
5335 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5336 };
5337 struct nfs4_fs_locations_arg args = {
5338 .dir_fh = NFS_FH(dir),
5339 .name = name,
5340 .page = page,
5341 .bitmask = bitmask,
5342 };
5343 struct nfs4_fs_locations_res res = {
5344 .fs_locations = fs_locations,
5345 };
5346 struct rpc_message msg = {
5347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5348 .rpc_argp = &args,
5349 .rpc_resp = &res,
5350 };
5351 int status;
5352
5353 dprintk("%s: start\n", __func__);
5354
5355 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5356 * is not supported */
5357 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5358 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5359 else
5360 bitmask[0] |= FATTR4_WORD0_FILEID;
5361
5362 nfs_fattr_init(&fs_locations->fattr);
5363 fs_locations->server = server;
5364 fs_locations->nlocations = 0;
5365 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5366 dprintk("%s: returned status = %d\n", __func__, status);
5367 return status;
5368}
5369
5370int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5371 const struct qstr *name,
5372 struct nfs4_fs_locations *fs_locations,
5373 struct page *page)
5374{
5375 struct nfs4_exception exception = { };
5376 int err;
5377 do {
5378 err = nfs4_handle_exception(NFS_SERVER(dir),
5379 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5380 &exception);
5381 } while (exception.retry);
5382 return err;
5383}
5384
5385static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5386{
5387 int status;
5388 struct nfs4_secinfo_arg args = {
5389 .dir_fh = NFS_FH(dir),
5390 .name = name,
5391 };
5392 struct nfs4_secinfo_res res = {
5393 .flavors = flavors,
5394 };
5395 struct rpc_message msg = {
5396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5397 .rpc_argp = &args,
5398 .rpc_resp = &res,
5399 };
5400
5401 dprintk("NFS call secinfo %s\n", name->name);
5402 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5403 dprintk("NFS reply secinfo: %d\n", status);
5404 return status;
5405}
5406
5407int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5408 struct nfs4_secinfo_flavors *flavors)
5409{
5410 struct nfs4_exception exception = { };
5411 int err;
5412 do {
5413 err = nfs4_handle_exception(NFS_SERVER(dir),
5414 _nfs4_proc_secinfo(dir, name, flavors),
5415 &exception);
5416 } while (exception.retry);
5417 return err;
5418}
5419
5420#ifdef CONFIG_NFS_V4_1
5421/*
5422 * Check the exchange flags returned by the server for invalid flags, having
5423 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5424 * DS flags set.
5425 */
5426static int nfs4_check_cl_exchange_flags(u32 flags)
5427{
5428 if (flags & ~EXCHGID4_FLAG_MASK_R)
5429 goto out_inval;
5430 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5431 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5432 goto out_inval;
5433 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5434 goto out_inval;
5435 return NFS_OK;
5436out_inval:
5437 return -NFS4ERR_INVAL;
5438}
5439
5440static bool
5441nfs41_same_server_scope(struct nfs41_server_scope *a,
5442 struct nfs41_server_scope *b)
5443{
5444 if (a->server_scope_sz == b->server_scope_sz &&
5445 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5446 return true;
5447
5448 return false;
5449}
5450
5451/*
5452 * nfs4_proc_bind_conn_to_session()
5453 *
5454 * The 4.1 client currently uses the same TCP connection for the
5455 * fore and backchannel.
5456 */
5457int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5458{
5459 int status;
5460 struct nfs41_bind_conn_to_session_res res;
5461 struct rpc_message msg = {
5462 .rpc_proc =
5463 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5464 .rpc_argp = clp,
5465 .rpc_resp = &res,
5466 .rpc_cred = cred,
5467 };
5468
5469 dprintk("--> %s\n", __func__);
5470
5471 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5472 if (unlikely(res.session == NULL)) {
5473 status = -ENOMEM;
5474 goto out;
5475 }
5476
5477 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5478 if (status == 0) {
5479 if (memcmp(res.session->sess_id.data,
5480 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5481 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5482 status = -EIO;
5483 goto out_session;
5484 }
5485 if (res.dir != NFS4_CDFS4_BOTH) {
5486 dprintk("NFS: %s: Unexpected direction from server\n",
5487 __func__);
5488 status = -EIO;
5489 goto out_session;
5490 }
5491 if (res.use_conn_in_rdma_mode) {
5492 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5493 __func__);
5494 status = -EIO;
5495 goto out_session;
5496 }
5497 }
5498out_session:
5499 kfree(res.session);
5500out:
5501 dprintk("<-- %s status= %d\n", __func__, status);
5502 return status;
5503}
5504
5505/*
5506 * nfs4_proc_exchange_id()
5507 *
5508 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5509 *
5510 * Since the clientid has expired, all compounds using sessions
5511 * associated with the stale clientid will be returning
5512 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5513 * be in some phase of session reset.
5514 */
5515int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5516{
5517 nfs4_verifier verifier;
5518 struct nfs41_exchange_id_args args = {
5519 .verifier = &verifier,
5520 .client = clp,
5521 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
5522 EXCHGID4_FLAG_BIND_PRINC_STATEID,
5523 };
5524 struct nfs41_exchange_id_res res = {
5525 0
5526 };
5527 int status;
5528 struct rpc_message msg = {
5529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5530 .rpc_argp = &args,
5531 .rpc_resp = &res,
5532 .rpc_cred = cred,
5533 };
5534
5535 nfs4_init_boot_verifier(clp, &verifier);
5536 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5537 sizeof(args.id));
5538 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5539 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5540 args.id_len, args.id);
5541
5542 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5543 GFP_NOFS);
5544 if (unlikely(res.server_owner == NULL)) {
5545 status = -ENOMEM;
5546 goto out;
5547 }
5548
5549 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5550 GFP_NOFS);
5551 if (unlikely(res.server_scope == NULL)) {
5552 status = -ENOMEM;
5553 goto out_server_owner;
5554 }
5555
5556 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5557 if (unlikely(res.impl_id == NULL)) {
5558 status = -ENOMEM;
5559 goto out_server_scope;
5560 }
5561
5562 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5563 if (status == 0)
5564 status = nfs4_check_cl_exchange_flags(res.flags);
5565
5566 if (status == 0) {
5567 clp->cl_clientid = res.clientid;
5568 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5569 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5570 clp->cl_seqid = res.seqid;
5571
5572 kfree(clp->cl_serverowner);
5573 clp->cl_serverowner = res.server_owner;
5574 res.server_owner = NULL;
5575
5576 /* use the most recent implementation id */
5577 kfree(clp->cl_implid);
5578 clp->cl_implid = res.impl_id;
5579
5580 if (clp->cl_serverscope != NULL &&
5581 !nfs41_same_server_scope(clp->cl_serverscope,
5582 res.server_scope)) {
5583 dprintk("%s: server_scope mismatch detected\n",
5584 __func__);
5585 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5586 kfree(clp->cl_serverscope);
5587 clp->cl_serverscope = NULL;
5588 }
5589
5590 if (clp->cl_serverscope == NULL) {
5591 clp->cl_serverscope = res.server_scope;
5592 goto out;
5593 }
5594 } else
5595 kfree(res.impl_id);
5596
5597out_server_owner:
5598 kfree(res.server_owner);
5599out_server_scope:
5600 kfree(res.server_scope);
5601out:
5602 if (clp->cl_implid != NULL)
5603 dprintk("NFS reply exchange_id: Server Implementation ID: "
5604 "domain: %s, name: %s, date: %llu,%u\n",
5605 clp->cl_implid->domain, clp->cl_implid->name,
5606 clp->cl_implid->date.seconds,
5607 clp->cl_implid->date.nseconds);
5608 dprintk("NFS reply exchange_id: %d\n", status);
5609 return status;
5610}
5611
5612static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5613 struct rpc_cred *cred)
5614{
5615 struct rpc_message msg = {
5616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5617 .rpc_argp = clp,
5618 .rpc_cred = cred,
5619 };
5620 int status;
5621
5622 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5623 if (status)
5624 dprintk("NFS: Got error %d from the server %s on "
5625 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5626 return status;
5627}
5628
5629static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5630 struct rpc_cred *cred)
5631{
5632 unsigned int loop;
5633 int ret;
5634
5635 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5636 ret = _nfs4_proc_destroy_clientid(clp, cred);
5637 switch (ret) {
5638 case -NFS4ERR_DELAY:
5639 case -NFS4ERR_CLIENTID_BUSY:
5640 ssleep(1);
5641 break;
5642 default:
5643 return ret;
5644 }
5645 }
5646 return 0;
5647}
5648
5649int nfs4_destroy_clientid(struct nfs_client *clp)
5650{
5651 struct rpc_cred *cred;
5652 int ret = 0;
5653
5654 if (clp->cl_mvops->minor_version < 1)
5655 goto out;
5656 if (clp->cl_exchange_flags == 0)
5657 goto out;
5658 if (clp->cl_preserve_clid)
5659 goto out;
5660 cred = nfs4_get_exchange_id_cred(clp);
5661 ret = nfs4_proc_destroy_clientid(clp, cred);
5662 if (cred)
5663 put_rpccred(cred);
5664 switch (ret) {
5665 case 0:
5666 case -NFS4ERR_STALE_CLIENTID:
5667 clp->cl_exchange_flags = 0;
5668 }
5669out:
5670 return ret;
5671}
5672
5673struct nfs4_get_lease_time_data {
5674 struct nfs4_get_lease_time_args *args;
5675 struct nfs4_get_lease_time_res *res;
5676 struct nfs_client *clp;
5677};
5678
5679static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5680 void *calldata)
5681{
5682 struct nfs4_get_lease_time_data *data =
5683 (struct nfs4_get_lease_time_data *)calldata;
5684
5685 dprintk("--> %s\n", __func__);
5686 /* just setup sequence, do not trigger session recovery
5687 since we're invoked within one */
5688 nfs41_setup_sequence(data->clp->cl_session,
5689 &data->args->la_seq_args,
5690 &data->res->lr_seq_res,
5691 task);
5692 dprintk("<-- %s\n", __func__);
5693}
5694
5695/*
5696 * Called from nfs4_state_manager thread for session setup, so don't recover
5697 * from sequence operation or clientid errors.
5698 */
5699static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5700{
5701 struct nfs4_get_lease_time_data *data =
5702 (struct nfs4_get_lease_time_data *)calldata;
5703
5704 dprintk("--> %s\n", __func__);
5705 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5706 return;
5707 switch (task->tk_status) {
5708 case -NFS4ERR_DELAY:
5709 case -NFS4ERR_GRACE:
5710 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5711 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5712 task->tk_status = 0;
5713 /* fall through */
5714 case -NFS4ERR_RETRY_UNCACHED_REP:
5715 rpc_restart_call_prepare(task);
5716 return;
5717 }
5718 dprintk("<-- %s\n", __func__);
5719}
5720
5721static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5722 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5723 .rpc_call_done = nfs4_get_lease_time_done,
5724};
5725
5726int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5727{
5728 struct rpc_task *task;
5729 struct nfs4_get_lease_time_args args;
5730 struct nfs4_get_lease_time_res res = {
5731 .lr_fsinfo = fsinfo,
5732 };
5733 struct nfs4_get_lease_time_data data = {
5734 .args = &args,
5735 .res = &res,
5736 .clp = clp,
5737 };
5738 struct rpc_message msg = {
5739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5740 .rpc_argp = &args,
5741 .rpc_resp = &res,
5742 };
5743 struct rpc_task_setup task_setup = {
5744 .rpc_client = clp->cl_rpcclient,
5745 .rpc_message = &msg,
5746 .callback_ops = &nfs4_get_lease_time_ops,
5747 .callback_data = &data,
5748 .flags = RPC_TASK_TIMEOUT,
5749 };
5750 int status;
5751
5752 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5753 nfs4_set_sequence_privileged(&args.la_seq_args);
5754 dprintk("--> %s\n", __func__);
5755 task = rpc_run_task(&task_setup);
5756
5757 if (IS_ERR(task))
5758 status = PTR_ERR(task);
5759 else {
5760 status = task->tk_status;
5761 rpc_put_task(task);
5762 }
5763 dprintk("<-- %s return %d\n", __func__, status);
5764
5765 return status;
5766}
5767
5768/*
5769 * Initialize the values to be used by the client in CREATE_SESSION
5770 * If nfs4_init_session set the fore channel request and response sizes,
5771 * use them.
5772 *
5773 * Set the back channel max_resp_sz_cached to zero to force the client to
5774 * always set csa_cachethis to FALSE because the current implementation
5775 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5776 */
5777static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5778{
5779 struct nfs4_session *session = args->client->cl_session;
5780 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5781 mxresp_sz = session->fc_target_max_resp_sz;
5782
5783 if (mxrqst_sz == 0)
5784 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5785 if (mxresp_sz == 0)
5786 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5787 /* Fore channel attributes */
5788 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5789 args->fc_attrs.max_resp_sz = mxresp_sz;
5790 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5791 args->fc_attrs.max_reqs = max_session_slots;
5792
5793 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5794 "max_ops=%u max_reqs=%u\n",
5795 __func__,
5796 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5797 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5798
5799 /* Back channel attributes */
5800 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5801 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5802 args->bc_attrs.max_resp_sz_cached = 0;
5803 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5804 args->bc_attrs.max_reqs = 1;
5805
5806 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5807 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5808 __func__,
5809 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5810 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5811 args->bc_attrs.max_reqs);
5812}
5813
5814static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5815{
5816 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5817 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5818
5819 if (rcvd->max_resp_sz > sent->max_resp_sz)
5820 return -EINVAL;
5821 /*
5822 * Our requested max_ops is the minimum we need; we're not
5823 * prepared to break up compounds into smaller pieces than that.
5824 * So, no point even trying to continue if the server won't
5825 * cooperate:
5826 */
5827 if (rcvd->max_ops < sent->max_ops)
5828 return -EINVAL;
5829 if (rcvd->max_reqs == 0)
5830 return -EINVAL;
5831 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5832 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5833 return 0;
5834}
5835
5836static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5837{
5838 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5839 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5840
5841 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5842 return -EINVAL;
5843 if (rcvd->max_resp_sz < sent->max_resp_sz)
5844 return -EINVAL;
5845 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5846 return -EINVAL;
5847 /* These would render the backchannel useless: */
5848 if (rcvd->max_ops != sent->max_ops)
5849 return -EINVAL;
5850 if (rcvd->max_reqs != sent->max_reqs)
5851 return -EINVAL;
5852 return 0;
5853}
5854
5855static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5856 struct nfs4_session *session)
5857{
5858 int ret;
5859
5860 ret = nfs4_verify_fore_channel_attrs(args, session);
5861 if (ret)
5862 return ret;
5863 return nfs4_verify_back_channel_attrs(args, session);
5864}
5865
5866static int _nfs4_proc_create_session(struct nfs_client *clp,
5867 struct rpc_cred *cred)
5868{
5869 struct nfs4_session *session = clp->cl_session;
5870 struct nfs41_create_session_args args = {
5871 .client = clp,
5872 .cb_program = NFS4_CALLBACK,
5873 };
5874 struct nfs41_create_session_res res = {
5875 .client = clp,
5876 };
5877 struct rpc_message msg = {
5878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5879 .rpc_argp = &args,
5880 .rpc_resp = &res,
5881 .rpc_cred = cred,
5882 };
5883 int status;
5884
5885 nfs4_init_channel_attrs(&args);
5886 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5887
5888 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5889
5890 if (!status) {
5891 /* Verify the session's negotiated channel_attrs values */
5892 status = nfs4_verify_channel_attrs(&args, session);
5893 /* Increment the clientid slot sequence id */
5894 clp->cl_seqid++;
5895 }
5896
5897 return status;
5898}
5899
5900/*
5901 * Issues a CREATE_SESSION operation to the server.
5902 * It is the responsibility of the caller to verify the session is
5903 * expired before calling this routine.
5904 */
5905int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5906{
5907 int status;
5908 unsigned *ptr;
5909 struct nfs4_session *session = clp->cl_session;
5910
5911 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5912
5913 status = _nfs4_proc_create_session(clp, cred);
5914 if (status)
5915 goto out;
5916
5917 /* Init or reset the session slot tables */
5918 status = nfs4_setup_session_slot_tables(session);
5919 dprintk("slot table setup returned %d\n", status);
5920 if (status)
5921 goto out;
5922
5923 ptr = (unsigned *)&session->sess_id.data[0];
5924 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5925 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5926out:
5927 dprintk("<-- %s\n", __func__);
5928 return status;
5929}
5930
5931/*
5932 * Issue the over-the-wire RPC DESTROY_SESSION.
5933 * The caller must serialize access to this routine.
5934 */
5935int nfs4_proc_destroy_session(struct nfs4_session *session,
5936 struct rpc_cred *cred)
5937{
5938 struct rpc_message msg = {
5939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5940 .rpc_argp = session,
5941 .rpc_cred = cred,
5942 };
5943 int status = 0;
5944
5945 dprintk("--> nfs4_proc_destroy_session\n");
5946
5947 /* session is still being setup */
5948 if (session->clp->cl_cons_state != NFS_CS_READY)
5949 return status;
5950
5951 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5952
5953 if (status)
5954 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5955 "Session has been destroyed regardless...\n", status);
5956
5957 dprintk("<-- nfs4_proc_destroy_session\n");
5958 return status;
5959}
5960
5961/*
5962 * Renew the cl_session lease.
5963 */
5964struct nfs4_sequence_data {
5965 struct nfs_client *clp;
5966 struct nfs4_sequence_args args;
5967 struct nfs4_sequence_res res;
5968};
5969
5970static void nfs41_sequence_release(void *data)
5971{
5972 struct nfs4_sequence_data *calldata = data;
5973 struct nfs_client *clp = calldata->clp;
5974
5975 if (atomic_read(&clp->cl_count) > 1)
5976 nfs4_schedule_state_renewal(clp);
5977 nfs_put_client(clp);
5978 kfree(calldata);
5979}
5980
5981static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5982{
5983 switch(task->tk_status) {
5984 case -NFS4ERR_DELAY:
5985 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5986 return -EAGAIN;
5987 default:
5988 nfs4_schedule_lease_recovery(clp);
5989 }
5990 return 0;
5991}
5992
5993static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5994{
5995 struct nfs4_sequence_data *calldata = data;
5996 struct nfs_client *clp = calldata->clp;
5997
5998 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5999 return;
6000
6001 if (task->tk_status < 0) {
6002 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6003 if (atomic_read(&clp->cl_count) == 1)
6004 goto out;
6005
6006 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6007 rpc_restart_call_prepare(task);
6008 return;
6009 }
6010 }
6011 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6012out:
6013 dprintk("<-- %s\n", __func__);
6014}
6015
6016static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6017{
6018 struct nfs4_sequence_data *calldata = data;
6019 struct nfs_client *clp = calldata->clp;
6020 struct nfs4_sequence_args *args;
6021 struct nfs4_sequence_res *res;
6022
6023 args = task->tk_msg.rpc_argp;
6024 res = task->tk_msg.rpc_resp;
6025
6026 nfs41_setup_sequence(clp->cl_session, args, res, task);
6027}
6028
6029static const struct rpc_call_ops nfs41_sequence_ops = {
6030 .rpc_call_done = nfs41_sequence_call_done,
6031 .rpc_call_prepare = nfs41_sequence_prepare,
6032 .rpc_release = nfs41_sequence_release,
6033};
6034
6035static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6036 struct rpc_cred *cred,
6037 bool is_privileged)
6038{
6039 struct nfs4_sequence_data *calldata;
6040 struct rpc_message msg = {
6041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6042 .rpc_cred = cred,
6043 };
6044 struct rpc_task_setup task_setup_data = {
6045 .rpc_client = clp->cl_rpcclient,
6046 .rpc_message = &msg,
6047 .callback_ops = &nfs41_sequence_ops,
6048 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6049 };
6050
6051 if (!atomic_inc_not_zero(&clp->cl_count))
6052 return ERR_PTR(-EIO);
6053 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6054 if (calldata == NULL) {
6055 nfs_put_client(clp);
6056 return ERR_PTR(-ENOMEM);
6057 }
6058 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6059 if (is_privileged)
6060 nfs4_set_sequence_privileged(&calldata->args);
6061 msg.rpc_argp = &calldata->args;
6062 msg.rpc_resp = &calldata->res;
6063 calldata->clp = clp;
6064 task_setup_data.callback_data = calldata;
6065
6066 return rpc_run_task(&task_setup_data);
6067}
6068
6069static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6070{
6071 struct rpc_task *task;
6072 int ret = 0;
6073
6074 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6075 return 0;
6076 task = _nfs41_proc_sequence(clp, cred, false);
6077 if (IS_ERR(task))
6078 ret = PTR_ERR(task);
6079 else
6080 rpc_put_task_async(task);
6081 dprintk("<-- %s status=%d\n", __func__, ret);
6082 return ret;
6083}
6084
6085static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6086{
6087 struct rpc_task *task;
6088 int ret;
6089
6090 task = _nfs41_proc_sequence(clp, cred, true);
6091 if (IS_ERR(task)) {
6092 ret = PTR_ERR(task);
6093 goto out;
6094 }
6095 ret = rpc_wait_for_completion_task(task);
6096 if (!ret) {
6097 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6098
6099 if (task->tk_status == 0)
6100 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6101 ret = task->tk_status;
6102 }
6103 rpc_put_task(task);
6104out:
6105 dprintk("<-- %s status=%d\n", __func__, ret);
6106 return ret;
6107}
6108
6109struct nfs4_reclaim_complete_data {
6110 struct nfs_client *clp;
6111 struct nfs41_reclaim_complete_args arg;
6112 struct nfs41_reclaim_complete_res res;
6113};
6114
6115static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6116{
6117 struct nfs4_reclaim_complete_data *calldata = data;
6118
6119 nfs41_setup_sequence(calldata->clp->cl_session,
6120 &calldata->arg.seq_args,
6121 &calldata->res.seq_res,
6122 task);
6123}
6124
6125static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6126{
6127 switch(task->tk_status) {
6128 case 0:
6129 case -NFS4ERR_COMPLETE_ALREADY:
6130 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6131 break;
6132 case -NFS4ERR_DELAY:
6133 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6134 /* fall through */
6135 case -NFS4ERR_RETRY_UNCACHED_REP:
6136 return -EAGAIN;
6137 default:
6138 nfs4_schedule_lease_recovery(clp);
6139 }
6140 return 0;
6141}
6142
6143static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6144{
6145 struct nfs4_reclaim_complete_data *calldata = data;
6146 struct nfs_client *clp = calldata->clp;
6147 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6148
6149 dprintk("--> %s\n", __func__);
6150 if (!nfs41_sequence_done(task, res))
6151 return;
6152
6153 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6154 rpc_restart_call_prepare(task);
6155 return;
6156 }
6157 dprintk("<-- %s\n", __func__);
6158}
6159
6160static void nfs4_free_reclaim_complete_data(void *data)
6161{
6162 struct nfs4_reclaim_complete_data *calldata = data;
6163
6164 kfree(calldata);
6165}
6166
6167static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6168 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6169 .rpc_call_done = nfs4_reclaim_complete_done,
6170 .rpc_release = nfs4_free_reclaim_complete_data,
6171};
6172
6173/*
6174 * Issue a global reclaim complete.
6175 */
6176static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6177 struct rpc_cred *cred)
6178{
6179 struct nfs4_reclaim_complete_data *calldata;
6180 struct rpc_task *task;
6181 struct rpc_message msg = {
6182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6183 .rpc_cred = cred,
6184 };
6185 struct rpc_task_setup task_setup_data = {
6186 .rpc_client = clp->cl_rpcclient,
6187 .rpc_message = &msg,
6188 .callback_ops = &nfs4_reclaim_complete_call_ops,
6189 .flags = RPC_TASK_ASYNC,
6190 };
6191 int status = -ENOMEM;
6192
6193 dprintk("--> %s\n", __func__);
6194 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6195 if (calldata == NULL)
6196 goto out;
6197 calldata->clp = clp;
6198 calldata->arg.one_fs = 0;
6199
6200 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6201 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
6202 msg.rpc_argp = &calldata->arg;
6203 msg.rpc_resp = &calldata->res;
6204 task_setup_data.callback_data = calldata;
6205 task = rpc_run_task(&task_setup_data);
6206 if (IS_ERR(task)) {
6207 status = PTR_ERR(task);
6208 goto out;
6209 }
6210 status = nfs4_wait_for_completion_rpc_task(task);
6211 if (status == 0)
6212 status = task->tk_status;
6213 rpc_put_task(task);
6214 return 0;
6215out:
6216 dprintk("<-- %s status=%d\n", __func__, status);
6217 return status;
6218}
6219
6220static void
6221nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6222{
6223 struct nfs4_layoutget *lgp = calldata;
6224 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6225 struct nfs4_session *session = nfs4_get_session(server);
6226
6227 dprintk("--> %s\n", __func__);
6228 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6229 * right now covering the LAYOUTGET we are about to send.
6230 * However, that is not so catastrophic, and there seems
6231 * to be no way to prevent it completely.
6232 */
6233 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
6234 &lgp->res.seq_res, task))
6235 return;
6236 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6237 NFS_I(lgp->args.inode)->layout,
6238 lgp->args.ctx->state)) {
6239 rpc_exit(task, NFS4_OK);
6240 }
6241}
6242
6243static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6244{
6245 struct nfs4_layoutget *lgp = calldata;
6246 struct inode *inode = lgp->args.inode;
6247 struct nfs_server *server = NFS_SERVER(inode);
6248 struct pnfs_layout_hdr *lo;
6249 struct nfs4_state *state = NULL;
6250 unsigned long timeo, giveup;
6251
6252 dprintk("--> %s\n", __func__);
6253
6254 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6255 goto out;
6256
6257 switch (task->tk_status) {
6258 case 0:
6259 goto out;
6260 case -NFS4ERR_LAYOUTTRYLATER:
6261 case -NFS4ERR_RECALLCONFLICT:
6262 timeo = rpc_get_timeout(task->tk_client);
6263 giveup = lgp->args.timestamp + timeo;
6264 if (time_after(giveup, jiffies))
6265 task->tk_status = -NFS4ERR_DELAY;
6266 break;
6267 case -NFS4ERR_EXPIRED:
6268 case -NFS4ERR_BAD_STATEID:
6269 spin_lock(&inode->i_lock);
6270 lo = NFS_I(inode)->layout;
6271 if (!lo || list_empty(&lo->plh_segs)) {
6272 spin_unlock(&inode->i_lock);
6273 /* If the open stateid was bad, then recover it. */
6274 state = lgp->args.ctx->state;
6275 } else {
6276 LIST_HEAD(head);
6277
6278 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6279 spin_unlock(&inode->i_lock);
6280 /* Mark the bad layout state as invalid, then
6281 * retry using the open stateid. */
6282 pnfs_free_lseg_list(&head);
6283 }
6284 }
6285 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6286 rpc_restart_call_prepare(task);
6287out:
6288 dprintk("<-- %s\n", __func__);
6289}
6290
6291static size_t max_response_pages(struct nfs_server *server)
6292{
6293 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6294 return nfs_page_array_len(0, max_resp_sz);
6295}
6296
6297static void nfs4_free_pages(struct page **pages, size_t size)
6298{
6299 int i;
6300
6301 if (!pages)
6302 return;
6303
6304 for (i = 0; i < size; i++) {
6305 if (!pages[i])
6306 break;
6307 __free_page(pages[i]);
6308 }
6309 kfree(pages);
6310}
6311
6312static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6313{
6314 struct page **pages;
6315 int i;
6316
6317 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6318 if (!pages) {
6319 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6320 return NULL;
6321 }
6322
6323 for (i = 0; i < size; i++) {
6324 pages[i] = alloc_page(gfp_flags);
6325 if (!pages[i]) {
6326 dprintk("%s: failed to allocate page\n", __func__);
6327 nfs4_free_pages(pages, size);
6328 return NULL;
6329 }
6330 }
6331
6332 return pages;
6333}
6334
6335static void nfs4_layoutget_release(void *calldata)
6336{
6337 struct nfs4_layoutget *lgp = calldata;
6338 struct inode *inode = lgp->args.inode;
6339 struct nfs_server *server = NFS_SERVER(inode);
6340 size_t max_pages = max_response_pages(server);
6341
6342 dprintk("--> %s\n", __func__);
6343 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6344 pnfs_put_layout_hdr(NFS_I(inode)->layout);
6345 put_nfs_open_context(lgp->args.ctx);
6346 kfree(calldata);
6347 dprintk("<-- %s\n", __func__);
6348}
6349
6350static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6351 .rpc_call_prepare = nfs4_layoutget_prepare,
6352 .rpc_call_done = nfs4_layoutget_done,
6353 .rpc_release = nfs4_layoutget_release,
6354};
6355
6356struct pnfs_layout_segment *
6357nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6358{
6359 struct inode *inode = lgp->args.inode;
6360 struct nfs_server *server = NFS_SERVER(inode);
6361 size_t max_pages = max_response_pages(server);
6362 struct rpc_task *task;
6363 struct rpc_message msg = {
6364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6365 .rpc_argp = &lgp->args,
6366 .rpc_resp = &lgp->res,
6367 .rpc_cred = lgp->cred,
6368 };
6369 struct rpc_task_setup task_setup_data = {
6370 .rpc_client = server->client,
6371 .rpc_message = &msg,
6372 .callback_ops = &nfs4_layoutget_call_ops,
6373 .callback_data = lgp,
6374 .flags = RPC_TASK_ASYNC,
6375 };
6376 struct pnfs_layout_segment *lseg = NULL;
6377 int status = 0;
6378
6379 dprintk("--> %s\n", __func__);
6380
6381 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6382 if (!lgp->args.layout.pages) {
6383 nfs4_layoutget_release(lgp);
6384 return ERR_PTR(-ENOMEM);
6385 }
6386 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6387 lgp->args.timestamp = jiffies;
6388
6389 lgp->res.layoutp = &lgp->args.layout;
6390 lgp->res.seq_res.sr_slot = NULL;
6391 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6392
6393 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6394 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6395
6396 task = rpc_run_task(&task_setup_data);
6397 if (IS_ERR(task))
6398 return ERR_CAST(task);
6399 status = nfs4_wait_for_completion_rpc_task(task);
6400 if (status == 0)
6401 status = task->tk_status;
6402 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6403 if (status == 0 && lgp->res.layoutp->len)
6404 lseg = pnfs_layout_process(lgp);
6405 rpc_put_task(task);
6406 dprintk("<-- %s status=%d\n", __func__, status);
6407 if (status)
6408 return ERR_PTR(status);
6409 return lseg;
6410}
6411
6412static void
6413nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6414{
6415 struct nfs4_layoutreturn *lrp = calldata;
6416
6417 dprintk("--> %s\n", __func__);
6418 nfs41_setup_sequence(lrp->clp->cl_session,
6419 &lrp->args.seq_args,
6420 &lrp->res.seq_res,
6421 task);
6422}
6423
6424static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6425{
6426 struct nfs4_layoutreturn *lrp = calldata;
6427 struct nfs_server *server;
6428
6429 dprintk("--> %s\n", __func__);
6430
6431 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6432 return;
6433
6434 server = NFS_SERVER(lrp->args.inode);
6435 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6436 rpc_restart_call_prepare(task);
6437 return;
6438 }
6439 dprintk("<-- %s\n", __func__);
6440}
6441
6442static void nfs4_layoutreturn_release(void *calldata)
6443{
6444 struct nfs4_layoutreturn *lrp = calldata;
6445 struct pnfs_layout_hdr *lo = lrp->args.layout;
6446
6447 dprintk("--> %s\n", __func__);
6448 spin_lock(&lo->plh_inode->i_lock);
6449 if (lrp->res.lrs_present)
6450 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6451 lo->plh_block_lgets--;
6452 spin_unlock(&lo->plh_inode->i_lock);
6453 pnfs_put_layout_hdr(lrp->args.layout);
6454 kfree(calldata);
6455 dprintk("<-- %s\n", __func__);
6456}
6457
6458static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6459 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6460 .rpc_call_done = nfs4_layoutreturn_done,
6461 .rpc_release = nfs4_layoutreturn_release,
6462};
6463
6464int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6465{
6466 struct rpc_task *task;
6467 struct rpc_message msg = {
6468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6469 .rpc_argp = &lrp->args,
6470 .rpc_resp = &lrp->res,
6471 .rpc_cred = lrp->cred,
6472 };
6473 struct rpc_task_setup task_setup_data = {
6474 .rpc_client = lrp->clp->cl_rpcclient,
6475 .rpc_message = &msg,
6476 .callback_ops = &nfs4_layoutreturn_call_ops,
6477 .callback_data = lrp,
6478 };
6479 int status;
6480
6481 dprintk("--> %s\n", __func__);
6482 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6483 task = rpc_run_task(&task_setup_data);
6484 if (IS_ERR(task))
6485 return PTR_ERR(task);
6486 status = task->tk_status;
6487 dprintk("<-- %s status=%d\n", __func__, status);
6488 rpc_put_task(task);
6489 return status;
6490}
6491
6492/*
6493 * Retrieve the list of Data Server devices from the MDS.
6494 */
6495static int _nfs4_getdevicelist(struct nfs_server *server,
6496 const struct nfs_fh *fh,
6497 struct pnfs_devicelist *devlist)
6498{
6499 struct nfs4_getdevicelist_args args = {
6500 .fh = fh,
6501 .layoutclass = server->pnfs_curr_ld->id,
6502 };
6503 struct nfs4_getdevicelist_res res = {
6504 .devlist = devlist,
6505 };
6506 struct rpc_message msg = {
6507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6508 .rpc_argp = &args,
6509 .rpc_resp = &res,
6510 };
6511 int status;
6512
6513 dprintk("--> %s\n", __func__);
6514 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6515 &res.seq_res, 0);
6516 dprintk("<-- %s status=%d\n", __func__, status);
6517 return status;
6518}
6519
6520int nfs4_proc_getdevicelist(struct nfs_server *server,
6521 const struct nfs_fh *fh,
6522 struct pnfs_devicelist *devlist)
6523{
6524 struct nfs4_exception exception = { };
6525 int err;
6526
6527 do {
6528 err = nfs4_handle_exception(server,
6529 _nfs4_getdevicelist(server, fh, devlist),
6530 &exception);
6531 } while (exception.retry);
6532
6533 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6534 err, devlist->num_devs);
6535
6536 return err;
6537}
6538EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6539
6540static int
6541_nfs4_proc_getdeviceinfo(struct nfs_server *server,
6542 struct pnfs_device *pdev,
6543 struct rpc_cred *cred)
6544{
6545 struct nfs4_getdeviceinfo_args args = {
6546 .pdev = pdev,
6547 };
6548 struct nfs4_getdeviceinfo_res res = {
6549 .pdev = pdev,
6550 };
6551 struct rpc_message msg = {
6552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6553 .rpc_argp = &args,
6554 .rpc_resp = &res,
6555 .rpc_cred = cred,
6556 };
6557 int status;
6558
6559 dprintk("--> %s\n", __func__);
6560 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6561 dprintk("<-- %s status=%d\n", __func__, status);
6562
6563 return status;
6564}
6565
6566int nfs4_proc_getdeviceinfo(struct nfs_server *server,
6567 struct pnfs_device *pdev,
6568 struct rpc_cred *cred)
6569{
6570 struct nfs4_exception exception = { };
6571 int err;
6572
6573 do {
6574 err = nfs4_handle_exception(server,
6575 _nfs4_proc_getdeviceinfo(server, pdev, cred),
6576 &exception);
6577 } while (exception.retry);
6578 return err;
6579}
6580EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6581
6582static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6583{
6584 struct nfs4_layoutcommit_data *data = calldata;
6585 struct nfs_server *server = NFS_SERVER(data->args.inode);
6586 struct nfs4_session *session = nfs4_get_session(server);
6587
6588 nfs41_setup_sequence(session,
6589 &data->args.seq_args,
6590 &data->res.seq_res,
6591 task);
6592}
6593
6594static void
6595nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6596{
6597 struct nfs4_layoutcommit_data *data = calldata;
6598 struct nfs_server *server = NFS_SERVER(data->args.inode);
6599
6600 if (!nfs41_sequence_done(task, &data->res.seq_res))
6601 return;
6602
6603 switch (task->tk_status) { /* Just ignore these failures */
6604 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6605 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6606 case -NFS4ERR_BADLAYOUT: /* no layout */
6607 case -NFS4ERR_GRACE: /* loca_recalim always false */
6608 task->tk_status = 0;
6609 break;
6610 case 0:
6611 nfs_post_op_update_inode_force_wcc(data->args.inode,
6612 data->res.fattr);
6613 break;
6614 default:
6615 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6616 rpc_restart_call_prepare(task);
6617 return;
6618 }
6619 }
6620}
6621
6622static void nfs4_layoutcommit_release(void *calldata)
6623{
6624 struct nfs4_layoutcommit_data *data = calldata;
6625
6626 pnfs_cleanup_layoutcommit(data);
6627 put_rpccred(data->cred);
6628 kfree(data);
6629}
6630
6631static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6632 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6633 .rpc_call_done = nfs4_layoutcommit_done,
6634 .rpc_release = nfs4_layoutcommit_release,
6635};
6636
6637int
6638nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6639{
6640 struct rpc_message msg = {
6641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6642 .rpc_argp = &data->args,
6643 .rpc_resp = &data->res,
6644 .rpc_cred = data->cred,
6645 };
6646 struct rpc_task_setup task_setup_data = {
6647 .task = &data->task,
6648 .rpc_client = NFS_CLIENT(data->args.inode),
6649 .rpc_message = &msg,
6650 .callback_ops = &nfs4_layoutcommit_ops,
6651 .callback_data = data,
6652 .flags = RPC_TASK_ASYNC,
6653 };
6654 struct rpc_task *task;
6655 int status = 0;
6656
6657 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6658 "lbw: %llu inode %lu\n",
6659 data->task.tk_pid, sync,
6660 data->args.lastbytewritten,
6661 data->args.inode->i_ino);
6662
6663 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6664 task = rpc_run_task(&task_setup_data);
6665 if (IS_ERR(task))
6666 return PTR_ERR(task);
6667 if (sync == false)
6668 goto out;
6669 status = nfs4_wait_for_completion_rpc_task(task);
6670 if (status != 0)
6671 goto out;
6672 status = task->tk_status;
6673out:
6674 dprintk("%s: status %d\n", __func__, status);
6675 rpc_put_task(task);
6676 return status;
6677}
6678
6679static int
6680_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6681 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6682{
6683 struct nfs41_secinfo_no_name_args args = {
6684 .style = SECINFO_STYLE_CURRENT_FH,
6685 };
6686 struct nfs4_secinfo_res res = {
6687 .flavors = flavors,
6688 };
6689 struct rpc_message msg = {
6690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6691 .rpc_argp = &args,
6692 .rpc_resp = &res,
6693 };
6694 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6695}
6696
6697static int
6698nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6699 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6700{
6701 struct nfs4_exception exception = { };
6702 int err;
6703 do {
6704 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6705 switch (err) {
6706 case 0:
6707 case -NFS4ERR_WRONGSEC:
6708 case -NFS4ERR_NOTSUPP:
6709 goto out;
6710 default:
6711 err = nfs4_handle_exception(server, err, &exception);
6712 }
6713 } while (exception.retry);
6714out:
6715 return err;
6716}
6717
6718static int
6719nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6720 struct nfs_fsinfo *info)
6721{
6722 int err;
6723 struct page *page;
6724 rpc_authflavor_t flavor;
6725 struct nfs4_secinfo_flavors *flavors;
6726
6727 page = alloc_page(GFP_KERNEL);
6728 if (!page) {
6729 err = -ENOMEM;
6730 goto out;
6731 }
6732
6733 flavors = page_address(page);
6734 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6735
6736 /*
6737 * Fall back on "guess and check" method if
6738 * the server doesn't support SECINFO_NO_NAME
6739 */
6740 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6741 err = nfs4_find_root_sec(server, fhandle, info);
6742 goto out_freepage;
6743 }
6744 if (err)
6745 goto out_freepage;
6746
6747 flavor = nfs_find_best_sec(flavors);
6748 if (err == 0)
6749 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6750
6751out_freepage:
6752 put_page(page);
6753 if (err == -EACCES)
6754 return -EPERM;
6755out:
6756 return err;
6757}
6758
6759static int _nfs41_test_stateid(struct nfs_server *server,
6760 nfs4_stateid *stateid,
6761 struct rpc_cred *cred)
6762{
6763 int status;
6764 struct nfs41_test_stateid_args args = {
6765 .stateid = stateid,
6766 };
6767 struct nfs41_test_stateid_res res;
6768 struct rpc_message msg = {
6769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6770 .rpc_argp = &args,
6771 .rpc_resp = &res,
6772 .rpc_cred = cred,
6773 };
6774
6775 dprintk("NFS call test_stateid %p\n", stateid);
6776 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6777 nfs4_set_sequence_privileged(&args.seq_args);
6778 status = nfs4_call_sync_sequence(server->client, server, &msg,
6779 &args.seq_args, &res.seq_res);
6780 if (status != NFS_OK) {
6781 dprintk("NFS reply test_stateid: failed, %d\n", status);
6782 return status;
6783 }
6784 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6785 return -res.status;
6786}
6787
6788/**
6789 * nfs41_test_stateid - perform a TEST_STATEID operation
6790 *
6791 * @server: server / transport on which to perform the operation
6792 * @stateid: state ID to test
6793 * @cred: credential
6794 *
6795 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6796 * Otherwise a negative NFS4ERR value is returned if the operation
6797 * failed or the state ID is not currently valid.
6798 */
6799static int nfs41_test_stateid(struct nfs_server *server,
6800 nfs4_stateid *stateid,
6801 struct rpc_cred *cred)
6802{
6803 struct nfs4_exception exception = { };
6804 int err;
6805 do {
6806 err = _nfs41_test_stateid(server, stateid, cred);
6807 if (err != -NFS4ERR_DELAY)
6808 break;
6809 nfs4_handle_exception(server, err, &exception);
6810 } while (exception.retry);
6811 return err;
6812}
6813
6814struct nfs_free_stateid_data {
6815 struct nfs_server *server;
6816 struct nfs41_free_stateid_args args;
6817 struct nfs41_free_stateid_res res;
6818};
6819
6820static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
6821{
6822 struct nfs_free_stateid_data *data = calldata;
6823 nfs41_setup_sequence(nfs4_get_session(data->server),
6824 &data->args.seq_args,
6825 &data->res.seq_res,
6826 task);
6827}
6828
6829static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
6830{
6831 struct nfs_free_stateid_data *data = calldata;
6832
6833 nfs41_sequence_done(task, &data->res.seq_res);
6834
6835 switch (task->tk_status) {
6836 case -NFS4ERR_DELAY:
6837 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
6838 rpc_restart_call_prepare(task);
6839 }
6840}
6841
6842static void nfs41_free_stateid_release(void *calldata)
6843{
6844 kfree(calldata);
6845}
6846
6847const struct rpc_call_ops nfs41_free_stateid_ops = {
6848 .rpc_call_prepare = nfs41_free_stateid_prepare,
6849 .rpc_call_done = nfs41_free_stateid_done,
6850 .rpc_release = nfs41_free_stateid_release,
6851};
6852
6853static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
6854 nfs4_stateid *stateid,
6855 struct rpc_cred *cred,
6856 bool privileged)
6857{
6858 struct rpc_message msg = {
6859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6860 .rpc_cred = cred,
6861 };
6862 struct rpc_task_setup task_setup = {
6863 .rpc_client = server->client,
6864 .rpc_message = &msg,
6865 .callback_ops = &nfs41_free_stateid_ops,
6866 .flags = RPC_TASK_ASYNC,
6867 };
6868 struct nfs_free_stateid_data *data;
6869
6870 dprintk("NFS call free_stateid %p\n", stateid);
6871 data = kmalloc(sizeof(*data), GFP_NOFS);
6872 if (!data)
6873 return ERR_PTR(-ENOMEM);
6874 data->server = server;
6875 nfs4_stateid_copy(&data->args.stateid, stateid);
6876
6877 task_setup.callback_data = data;
6878
6879 msg.rpc_argp = &data->args;
6880 msg.rpc_resp = &data->res;
6881 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6882 if (privileged)
6883 nfs4_set_sequence_privileged(&data->args.seq_args);
6884
6885 return rpc_run_task(&task_setup);
6886}
6887
6888/**
6889 * nfs41_free_stateid - perform a FREE_STATEID operation
6890 *
6891 * @server: server / transport on which to perform the operation
6892 * @stateid: state ID to release
6893 * @cred: credential
6894 *
6895 * Returns NFS_OK if the server freed "stateid". Otherwise a
6896 * negative NFS4ERR value is returned.
6897 */
6898static int nfs41_free_stateid(struct nfs_server *server,
6899 nfs4_stateid *stateid,
6900 struct rpc_cred *cred)
6901{
6902 struct rpc_task *task;
6903 int ret;
6904
6905 task = _nfs41_free_stateid(server, stateid, cred, true);
6906 if (IS_ERR(task))
6907 return PTR_ERR(task);
6908 ret = rpc_wait_for_completion_task(task);
6909 if (!ret)
6910 ret = task->tk_status;
6911 rpc_put_task(task);
6912 return ret;
6913}
6914
6915static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
6916{
6917 struct rpc_task *task;
6918 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6919
6920 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
6921 nfs4_free_lock_state(server, lsp);
6922 if (IS_ERR(task))
6923 return PTR_ERR(task);
6924 rpc_put_task(task);
6925 return 0;
6926}
6927
6928static bool nfs41_match_stateid(const nfs4_stateid *s1,
6929 const nfs4_stateid *s2)
6930{
6931 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6932 return false;
6933
6934 if (s1->seqid == s2->seqid)
6935 return true;
6936 if (s1->seqid == 0 || s2->seqid == 0)
6937 return true;
6938
6939 return false;
6940}
6941
6942#endif /* CONFIG_NFS_V4_1 */
6943
6944static bool nfs4_match_stateid(const nfs4_stateid *s1,
6945 const nfs4_stateid *s2)
6946{
6947 return nfs4_stateid_match(s1, s2);
6948}
6949
6950
6951static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6952 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6953 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6954 .recover_open = nfs4_open_reclaim,
6955 .recover_lock = nfs4_lock_reclaim,
6956 .establish_clid = nfs4_init_clientid,
6957 .get_clid_cred = nfs4_get_setclientid_cred,
6958 .detect_trunking = nfs40_discover_server_trunking,
6959};
6960
6961#if defined(CONFIG_NFS_V4_1)
6962static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6963 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6964 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6965 .recover_open = nfs4_open_reclaim,
6966 .recover_lock = nfs4_lock_reclaim,
6967 .establish_clid = nfs41_init_clientid,
6968 .get_clid_cred = nfs4_get_exchange_id_cred,
6969 .reclaim_complete = nfs41_proc_reclaim_complete,
6970 .detect_trunking = nfs41_discover_server_trunking,
6971};
6972#endif /* CONFIG_NFS_V4_1 */
6973
6974static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6975 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6976 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6977 .recover_open = nfs4_open_expired,
6978 .recover_lock = nfs4_lock_expired,
6979 .establish_clid = nfs4_init_clientid,
6980 .get_clid_cred = nfs4_get_setclientid_cred,
6981};
6982
6983#if defined(CONFIG_NFS_V4_1)
6984static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6985 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6986 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6987 .recover_open = nfs41_open_expired,
6988 .recover_lock = nfs41_lock_expired,
6989 .establish_clid = nfs41_init_clientid,
6990 .get_clid_cred = nfs4_get_exchange_id_cred,
6991};
6992#endif /* CONFIG_NFS_V4_1 */
6993
6994static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6995 .sched_state_renewal = nfs4_proc_async_renew,
6996 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6997 .renew_lease = nfs4_proc_renew,
6998};
6999
7000#if defined(CONFIG_NFS_V4_1)
7001static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7002 .sched_state_renewal = nfs41_proc_async_sequence,
7003 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7004 .renew_lease = nfs4_proc_sequence,
7005};
7006#endif
7007
7008static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7009 .minor_version = 0,
7010 .init_caps = NFS_CAP_READDIRPLUS
7011 | NFS_CAP_ATOMIC_OPEN
7012 | NFS_CAP_CHANGE_ATTR
7013 | NFS_CAP_POSIX_LOCK,
7014 .call_sync = _nfs4_call_sync,
7015 .match_stateid = nfs4_match_stateid,
7016 .find_root_sec = nfs4_find_root_sec,
7017 .free_lock_state = nfs4_release_lockowner,
7018 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7019 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7020 .state_renewal_ops = &nfs40_state_renewal_ops,
7021};
7022
7023#if defined(CONFIG_NFS_V4_1)
7024static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7025 .minor_version = 1,
7026 .init_caps = NFS_CAP_READDIRPLUS
7027 | NFS_CAP_ATOMIC_OPEN
7028 | NFS_CAP_CHANGE_ATTR
7029 | NFS_CAP_POSIX_LOCK
7030 | NFS_CAP_STATEID_NFSV41
7031 | NFS_CAP_ATOMIC_OPEN_V1,
7032 .call_sync = nfs4_call_sync_sequence,
7033 .match_stateid = nfs41_match_stateid,
7034 .find_root_sec = nfs41_find_root_sec,
7035 .free_lock_state = nfs41_free_lock_state,
7036 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7037 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7038 .state_renewal_ops = &nfs41_state_renewal_ops,
7039};
7040#endif
7041
7042const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7043 [0] = &nfs_v4_0_minor_ops,
7044#if defined(CONFIG_NFS_V4_1)
7045 [1] = &nfs_v4_1_minor_ops,
7046#endif
7047};
7048
7049const struct inode_operations nfs4_dir_inode_operations = {
7050 .create = nfs_create,
7051 .lookup = nfs_lookup,
7052 .atomic_open = nfs_atomic_open,
7053 .link = nfs_link,
7054 .unlink = nfs_unlink,
7055 .symlink = nfs_symlink,
7056 .mkdir = nfs_mkdir,
7057 .rmdir = nfs_rmdir,
7058 .mknod = nfs_mknod,
7059 .rename = nfs_rename,
7060 .permission = nfs_permission,
7061 .getattr = nfs_getattr,
7062 .setattr = nfs_setattr,
7063 .getxattr = generic_getxattr,
7064 .setxattr = generic_setxattr,
7065 .listxattr = generic_listxattr,
7066 .removexattr = generic_removexattr,
7067};
7068
7069static const struct inode_operations nfs4_file_inode_operations = {
7070 .permission = nfs_permission,
7071 .getattr = nfs_getattr,
7072 .setattr = nfs_setattr,
7073 .getxattr = generic_getxattr,
7074 .setxattr = generic_setxattr,
7075 .listxattr = generic_listxattr,
7076 .removexattr = generic_removexattr,
7077};
7078
7079const struct nfs_rpc_ops nfs_v4_clientops = {
7080 .version = 4, /* protocol version */
7081 .dentry_ops = &nfs4_dentry_operations,
7082 .dir_inode_ops = &nfs4_dir_inode_operations,
7083 .file_inode_ops = &nfs4_file_inode_operations,
7084 .file_ops = &nfs4_file_operations,
7085 .getroot = nfs4_proc_get_root,
7086 .submount = nfs4_submount,
7087 .try_mount = nfs4_try_mount,
7088 .getattr = nfs4_proc_getattr,
7089 .setattr = nfs4_proc_setattr,
7090 .lookup = nfs4_proc_lookup,
7091 .access = nfs4_proc_access,
7092 .readlink = nfs4_proc_readlink,
7093 .create = nfs4_proc_create,
7094 .remove = nfs4_proc_remove,
7095 .unlink_setup = nfs4_proc_unlink_setup,
7096 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7097 .unlink_done = nfs4_proc_unlink_done,
7098 .rename = nfs4_proc_rename,
7099 .rename_setup = nfs4_proc_rename_setup,
7100 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7101 .rename_done = nfs4_proc_rename_done,
7102 .link = nfs4_proc_link,
7103 .symlink = nfs4_proc_symlink,
7104 .mkdir = nfs4_proc_mkdir,
7105 .rmdir = nfs4_proc_remove,
7106 .readdir = nfs4_proc_readdir,
7107 .mknod = nfs4_proc_mknod,
7108 .statfs = nfs4_proc_statfs,
7109 .fsinfo = nfs4_proc_fsinfo,
7110 .pathconf = nfs4_proc_pathconf,
7111 .set_capabilities = nfs4_server_capabilities,
7112 .decode_dirent = nfs4_decode_dirent,
7113 .read_setup = nfs4_proc_read_setup,
7114 .read_pageio_init = pnfs_pageio_init_read,
7115 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7116 .read_done = nfs4_read_done,
7117 .write_setup = nfs4_proc_write_setup,
7118 .write_pageio_init = pnfs_pageio_init_write,
7119 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7120 .write_done = nfs4_write_done,
7121 .commit_setup = nfs4_proc_commit_setup,
7122 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7123 .commit_done = nfs4_commit_done,
7124 .lock = nfs4_proc_lock,
7125 .clear_acl_cache = nfs4_zap_acl_attr,
7126 .close_context = nfs4_close_context,
7127 .open_context = nfs4_atomic_open,
7128 .have_delegation = nfs4_have_delegation,
7129 .return_delegation = nfs4_inode_return_delegation,
7130 .alloc_client = nfs4_alloc_client,
7131 .init_client = nfs4_init_client,
7132 .free_client = nfs4_free_client,
7133 .create_server = nfs4_create_server,
7134 .clone_server = nfs_clone_server,
7135};
7136
7137static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7138 .prefix = XATTR_NAME_NFSV4_ACL,
7139 .list = nfs4_xattr_list_nfs4_acl,
7140 .get = nfs4_xattr_get_nfs4_acl,
7141 .set = nfs4_xattr_set_nfs4_acl,
7142};
7143
7144const struct xattr_handler *nfs4_xattr_handlers[] = {
7145 &nfs4_xattr_nfs4_acl_handler,
7146 NULL
7147};
7148
7149/*
7150 * Local variables:
7151 * c-basic-offset: 8
7152 * End:
7153 */