NFSv4.1: allow nfs_fhget to succeed with mounted on fileid
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4proc.c
blobd348326a8a3dba0b10d4c4cfa8c6fdb6044c71fe
1 /*
2 * fs/nfs/nfs4proc.c
4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
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.
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/sunrpc/gss_api.h>
45 #include <linux/nfs.h>
46 #include <linux/nfs4.h>
47 #include <linux/nfs_fs.h>
48 #include <linux/nfs_page.h>
49 #include <linux/nfs_mount.h>
50 #include <linux/namei.h>
51 #include <linux/mount.h>
52 #include <linux/module.h>
53 #include <linux/sunrpc/bc_xprt.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
57 #include "nfs4_fs.h"
58 #include "delegation.h"
59 #include "internal.h"
60 #include "iostat.h"
61 #include "callback.h"
62 #include "pnfs.h"
64 #define NFSDBG_FACILITY NFSDBG_PROC
66 #define NFS4_POLL_RETRY_MIN (HZ/10)
67 #define NFS4_POLL_RETRY_MAX (15*HZ)
69 #define NFS4_MAX_LOOP_ON_RECOVER (10)
71 struct nfs4_opendata;
72 static int _nfs4_proc_open(struct nfs4_opendata *data);
73 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
74 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
75 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
76 static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
77 const struct qstr *name, struct nfs_fh *fhandle,
78 struct nfs_fattr *fattr);
79 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
80 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
81 struct nfs_fattr *fattr, struct iattr *sattr,
82 struct nfs4_state *state);
84 /* Prevent leaks of NFSv4 errors into userland */
85 static int nfs4_map_errors(int err)
87 if (err >= -1000)
88 return err;
89 switch (err) {
90 case -NFS4ERR_RESOURCE:
91 return -EREMOTEIO;
92 case -NFS4ERR_WRONGSEC:
93 return -EPERM;
94 case -NFS4ERR_BADOWNER:
95 case -NFS4ERR_BADNAME:
96 return -EINVAL;
97 default:
98 dprintk("%s could not handle NFSv4 error %d\n",
99 __func__, -err);
100 break;
102 return -EIO;
106 * This is our standard bitmap for GETATTR requests.
108 const u32 nfs4_fattr_bitmap[2] = {
109 FATTR4_WORD0_TYPE
110 | FATTR4_WORD0_CHANGE
111 | FATTR4_WORD0_SIZE
112 | FATTR4_WORD0_FSID
113 | FATTR4_WORD0_FILEID,
114 FATTR4_WORD1_MODE
115 | FATTR4_WORD1_NUMLINKS
116 | FATTR4_WORD1_OWNER
117 | FATTR4_WORD1_OWNER_GROUP
118 | FATTR4_WORD1_RAWDEV
119 | FATTR4_WORD1_SPACE_USED
120 | FATTR4_WORD1_TIME_ACCESS
121 | FATTR4_WORD1_TIME_METADATA
122 | FATTR4_WORD1_TIME_MODIFY
125 const u32 nfs4_statfs_bitmap[2] = {
126 FATTR4_WORD0_FILES_AVAIL
127 | FATTR4_WORD0_FILES_FREE
128 | FATTR4_WORD0_FILES_TOTAL,
129 FATTR4_WORD1_SPACE_AVAIL
130 | FATTR4_WORD1_SPACE_FREE
131 | FATTR4_WORD1_SPACE_TOTAL
134 const u32 nfs4_pathconf_bitmap[2] = {
135 FATTR4_WORD0_MAXLINK
136 | FATTR4_WORD0_MAXNAME,
140 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
141 | FATTR4_WORD0_MAXREAD
142 | FATTR4_WORD0_MAXWRITE
143 | FATTR4_WORD0_LEASE_TIME,
144 FATTR4_WORD1_TIME_DELTA
145 | FATTR4_WORD1_FS_LAYOUT_TYPES
148 const u32 nfs4_fs_locations_bitmap[2] = {
149 FATTR4_WORD0_TYPE
150 | FATTR4_WORD0_CHANGE
151 | FATTR4_WORD0_SIZE
152 | FATTR4_WORD0_FSID
153 | FATTR4_WORD0_FILEID
154 | FATTR4_WORD0_FS_LOCATIONS,
155 FATTR4_WORD1_MODE
156 | FATTR4_WORD1_NUMLINKS
157 | FATTR4_WORD1_OWNER
158 | FATTR4_WORD1_OWNER_GROUP
159 | FATTR4_WORD1_RAWDEV
160 | FATTR4_WORD1_SPACE_USED
161 | FATTR4_WORD1_TIME_ACCESS
162 | FATTR4_WORD1_TIME_METADATA
163 | FATTR4_WORD1_TIME_MODIFY
164 | FATTR4_WORD1_MOUNTED_ON_FILEID
167 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
168 struct nfs4_readdir_arg *readdir)
170 __be32 *start, *p;
172 BUG_ON(readdir->count < 80);
173 if (cookie > 2) {
174 readdir->cookie = cookie;
175 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
176 return;
179 readdir->cookie = 0;
180 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
181 if (cookie == 2)
182 return;
185 * NFSv4 servers do not return entries for '.' and '..'
186 * Therefore, we fake these entries here. We let '.'
187 * have cookie 0 and '..' have cookie 1. Note that
188 * when talking to the server, we always send cookie 0
189 * instead of 1 or 2.
191 start = p = kmap_atomic(*readdir->pages, KM_USER0);
193 if (cookie == 0) {
194 *p++ = xdr_one; /* next */
195 *p++ = xdr_zero; /* cookie, first word */
196 *p++ = xdr_one; /* cookie, second word */
197 *p++ = xdr_one; /* entry len */
198 memcpy(p, ".\0\0\0", 4); /* entry */
199 p++;
200 *p++ = xdr_one; /* bitmap length */
201 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
202 *p++ = htonl(8); /* attribute buffer length */
203 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
206 *p++ = xdr_one; /* next */
207 *p++ = xdr_zero; /* cookie, first word */
208 *p++ = xdr_two; /* cookie, second word */
209 *p++ = xdr_two; /* entry len */
210 memcpy(p, "..\0\0", 4); /* entry */
211 p++;
212 *p++ = xdr_one; /* bitmap length */
213 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
214 *p++ = htonl(8); /* attribute buffer length */
215 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
217 readdir->pgbase = (char *)p - (char *)start;
218 readdir->count -= readdir->pgbase;
219 kunmap_atomic(start, KM_USER0);
222 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
224 int res;
226 might_sleep();
228 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
229 nfs_wait_bit_killable, TASK_KILLABLE);
230 return res;
233 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
235 int res = 0;
237 might_sleep();
239 if (*timeout <= 0)
240 *timeout = NFS4_POLL_RETRY_MIN;
241 if (*timeout > NFS4_POLL_RETRY_MAX)
242 *timeout = NFS4_POLL_RETRY_MAX;
243 schedule_timeout_killable(*timeout);
244 if (fatal_signal_pending(current))
245 res = -ERESTARTSYS;
246 *timeout <<= 1;
247 return res;
250 /* This is the error handling routine for processes that are allowed
251 * to sleep.
253 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
255 struct nfs_client *clp = server->nfs_client;
256 struct nfs4_state *state = exception->state;
257 int ret = errorcode;
259 exception->retry = 0;
260 switch(errorcode) {
261 case 0:
262 return 0;
263 case -NFS4ERR_ADMIN_REVOKED:
264 case -NFS4ERR_BAD_STATEID:
265 case -NFS4ERR_OPENMODE:
266 if (state == NULL)
267 break;
268 nfs4_schedule_stateid_recovery(server, state);
269 goto wait_on_recovery;
270 case -NFS4ERR_EXPIRED:
271 if (state != NULL)
272 nfs4_schedule_stateid_recovery(server, state);
273 case -NFS4ERR_STALE_STATEID:
274 case -NFS4ERR_STALE_CLIENTID:
275 nfs4_schedule_lease_recovery(clp);
276 goto wait_on_recovery;
277 #if defined(CONFIG_NFS_V4_1)
278 case -NFS4ERR_BADSESSION:
279 case -NFS4ERR_BADSLOT:
280 case -NFS4ERR_BAD_HIGH_SLOT:
281 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
282 case -NFS4ERR_DEADSESSION:
283 case -NFS4ERR_SEQ_FALSE_RETRY:
284 case -NFS4ERR_SEQ_MISORDERED:
285 dprintk("%s ERROR: %d Reset session\n", __func__,
286 errorcode);
287 nfs4_schedule_session_recovery(clp->cl_session);
288 exception->retry = 1;
289 break;
290 #endif /* defined(CONFIG_NFS_V4_1) */
291 case -NFS4ERR_FILE_OPEN:
292 if (exception->timeout > HZ) {
293 /* We have retried a decent amount, time to
294 * fail
296 ret = -EBUSY;
297 break;
299 case -NFS4ERR_GRACE:
300 case -NFS4ERR_DELAY:
301 case -EKEYEXPIRED:
302 ret = nfs4_delay(server->client, &exception->timeout);
303 if (ret != 0)
304 break;
305 case -NFS4ERR_RETRY_UNCACHED_REP:
306 case -NFS4ERR_OLD_STATEID:
307 exception->retry = 1;
308 break;
309 case -NFS4ERR_BADOWNER:
310 /* The following works around a Linux server bug! */
311 case -NFS4ERR_BADNAME:
312 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
313 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
314 exception->retry = 1;
315 printk(KERN_WARNING "NFS: v4 server %s "
316 "does not accept raw "
317 "uid/gids. "
318 "Reenabling the idmapper.\n",
319 server->nfs_client->cl_hostname);
322 /* We failed to handle the error */
323 return nfs4_map_errors(ret);
324 wait_on_recovery:
325 ret = nfs4_wait_clnt_recover(clp);
326 if (ret == 0)
327 exception->retry = 1;
328 return ret;
332 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
334 spin_lock(&clp->cl_lock);
335 if (time_before(clp->cl_last_renewal,timestamp))
336 clp->cl_last_renewal = timestamp;
337 spin_unlock(&clp->cl_lock);
340 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
342 do_renew_lease(server->nfs_client, timestamp);
345 #if defined(CONFIG_NFS_V4_1)
348 * nfs4_free_slot - free a slot and efficiently update slot table.
350 * freeing a slot is trivially done by clearing its respective bit
351 * in the bitmap.
352 * If the freed slotid equals highest_used_slotid we want to update it
353 * so that the server would be able to size down the slot table if needed,
354 * otherwise we know that the highest_used_slotid is still in use.
355 * When updating highest_used_slotid there may be "holes" in the bitmap
356 * so we need to scan down from highest_used_slotid to 0 looking for the now
357 * highest slotid in use.
358 * If none found, highest_used_slotid is set to -1.
360 * Must be called while holding tbl->slot_tbl_lock
362 static void
363 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
365 int free_slotid = free_slot - tbl->slots;
366 int slotid = free_slotid;
368 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
369 /* clear used bit in bitmap */
370 __clear_bit(slotid, tbl->used_slots);
372 /* update highest_used_slotid when it is freed */
373 if (slotid == tbl->highest_used_slotid) {
374 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
375 if (slotid < tbl->max_slots)
376 tbl->highest_used_slotid = slotid;
377 else
378 tbl->highest_used_slotid = -1;
380 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
381 free_slotid, tbl->highest_used_slotid);
385 * Signal state manager thread if session fore channel is drained
387 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
389 struct rpc_task *task;
391 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
392 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
393 if (task)
394 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
395 return;
398 if (ses->fc_slot_table.highest_used_slotid != -1)
399 return;
401 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
402 complete(&ses->fc_slot_table.complete);
406 * Signal state manager thread if session back channel is drained
408 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
410 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
411 ses->bc_slot_table.highest_used_slotid != -1)
412 return;
413 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
414 complete(&ses->bc_slot_table.complete);
417 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
419 struct nfs4_slot_table *tbl;
421 tbl = &res->sr_session->fc_slot_table;
422 if (!res->sr_slot) {
423 /* just wake up the next guy waiting since
424 * we may have not consumed a slot after all */
425 dprintk("%s: No slot\n", __func__);
426 return;
429 spin_lock(&tbl->slot_tbl_lock);
430 nfs4_free_slot(tbl, res->sr_slot);
431 nfs4_check_drain_fc_complete(res->sr_session);
432 spin_unlock(&tbl->slot_tbl_lock);
433 res->sr_slot = NULL;
436 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
438 unsigned long timestamp;
439 struct nfs_client *clp;
442 * sr_status remains 1 if an RPC level error occurred. The server
443 * may or may not have processed the sequence operation..
444 * Proceed as if the server received and processed the sequence
445 * operation.
447 if (res->sr_status == 1)
448 res->sr_status = NFS_OK;
450 /* don't increment the sequence number if the task wasn't sent */
451 if (!RPC_WAS_SENT(task))
452 goto out;
454 /* Check the SEQUENCE operation status */
455 switch (res->sr_status) {
456 case 0:
457 /* Update the slot's sequence and clientid lease timer */
458 ++res->sr_slot->seq_nr;
459 timestamp = res->sr_renewal_time;
460 clp = res->sr_session->clp;
461 do_renew_lease(clp, timestamp);
462 /* Check sequence flags */
463 if (res->sr_status_flags != 0)
464 nfs4_schedule_lease_recovery(clp);
465 break;
466 case -NFS4ERR_DELAY:
467 /* The server detected a resend of the RPC call and
468 * returned NFS4ERR_DELAY as per Section 2.10.6.2
469 * of RFC5661.
471 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
472 __func__,
473 res->sr_slot - res->sr_session->fc_slot_table.slots,
474 res->sr_slot->seq_nr);
475 goto out_retry;
476 default:
477 /* Just update the slot sequence no. */
478 ++res->sr_slot->seq_nr;
480 out:
481 /* The session may be reset by one of the error handlers. */
482 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
483 nfs41_sequence_free_slot(res);
484 return 1;
485 out_retry:
486 if (!rpc_restart_call(task))
487 goto out;
488 rpc_delay(task, NFS4_POLL_RETRY_MAX);
489 return 0;
492 static int nfs4_sequence_done(struct rpc_task *task,
493 struct nfs4_sequence_res *res)
495 if (res->sr_session == NULL)
496 return 1;
497 return nfs41_sequence_done(task, res);
501 * nfs4_find_slot - efficiently look for a free slot
503 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
504 * If found, we mark the slot as used, update the highest_used_slotid,
505 * and respectively set up the sequence operation args.
506 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
508 * Note: must be called with under the slot_tbl_lock.
510 static u8
511 nfs4_find_slot(struct nfs4_slot_table *tbl)
513 int slotid;
514 u8 ret_id = NFS4_MAX_SLOT_TABLE;
515 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
517 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
518 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
519 tbl->max_slots);
520 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
521 if (slotid >= tbl->max_slots)
522 goto out;
523 __set_bit(slotid, tbl->used_slots);
524 if (slotid > tbl->highest_used_slotid)
525 tbl->highest_used_slotid = slotid;
526 ret_id = slotid;
527 out:
528 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
529 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
530 return ret_id;
533 int nfs41_setup_sequence(struct nfs4_session *session,
534 struct nfs4_sequence_args *args,
535 struct nfs4_sequence_res *res,
536 int cache_reply,
537 struct rpc_task *task)
539 struct nfs4_slot *slot;
540 struct nfs4_slot_table *tbl;
541 u8 slotid;
543 dprintk("--> %s\n", __func__);
544 /* slot already allocated? */
545 if (res->sr_slot != NULL)
546 return 0;
548 tbl = &session->fc_slot_table;
550 spin_lock(&tbl->slot_tbl_lock);
551 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
552 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
554 * The state manager will wait until the slot table is empty.
555 * Schedule the reset thread
557 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
558 spin_unlock(&tbl->slot_tbl_lock);
559 dprintk("%s Schedule Session Reset\n", __func__);
560 return -EAGAIN;
563 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
564 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
565 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
566 spin_unlock(&tbl->slot_tbl_lock);
567 dprintk("%s enforce FIFO order\n", __func__);
568 return -EAGAIN;
571 slotid = nfs4_find_slot(tbl);
572 if (slotid == NFS4_MAX_SLOT_TABLE) {
573 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
574 spin_unlock(&tbl->slot_tbl_lock);
575 dprintk("<-- %s: no free slots\n", __func__);
576 return -EAGAIN;
578 spin_unlock(&tbl->slot_tbl_lock);
580 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
581 slot = tbl->slots + slotid;
582 args->sa_session = session;
583 args->sa_slotid = slotid;
584 args->sa_cache_this = cache_reply;
586 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
588 res->sr_session = session;
589 res->sr_slot = slot;
590 res->sr_renewal_time = jiffies;
591 res->sr_status_flags = 0;
593 * sr_status is only set in decode_sequence, and so will remain
594 * set to 1 if an rpc level failure occurs.
596 res->sr_status = 1;
597 return 0;
599 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
601 int nfs4_setup_sequence(const struct nfs_server *server,
602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
604 int cache_reply,
605 struct rpc_task *task)
607 struct nfs4_session *session = nfs4_get_session(server);
608 int ret = 0;
610 if (session == NULL) {
611 args->sa_session = NULL;
612 res->sr_session = NULL;
613 goto out;
616 dprintk("--> %s clp %p session %p sr_slot %td\n",
617 __func__, session->clp, session, res->sr_slot ?
618 res->sr_slot - session->fc_slot_table.slots : -1);
620 ret = nfs41_setup_sequence(session, args, res, cache_reply,
621 task);
622 out:
623 dprintk("<-- %s status=%d\n", __func__, ret);
624 return ret;
627 struct nfs41_call_sync_data {
628 const struct nfs_server *seq_server;
629 struct nfs4_sequence_args *seq_args;
630 struct nfs4_sequence_res *seq_res;
631 int cache_reply;
634 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
636 struct nfs41_call_sync_data *data = calldata;
638 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
641 data->seq_res, data->cache_reply, task))
642 return;
643 rpc_call_start(task);
646 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
648 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
649 nfs41_call_sync_prepare(task, calldata);
652 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
654 struct nfs41_call_sync_data *data = calldata;
656 nfs41_sequence_done(task, data->seq_res);
659 struct rpc_call_ops nfs41_call_sync_ops = {
660 .rpc_call_prepare = nfs41_call_sync_prepare,
661 .rpc_call_done = nfs41_call_sync_done,
664 struct rpc_call_ops nfs41_call_priv_sync_ops = {
665 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
666 .rpc_call_done = nfs41_call_sync_done,
669 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
670 struct nfs_server *server,
671 struct rpc_message *msg,
672 struct nfs4_sequence_args *args,
673 struct nfs4_sequence_res *res,
674 int cache_reply,
675 int privileged)
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
680 .seq_server = server,
681 .seq_args = args,
682 .seq_res = res,
683 .cache_reply = cache_reply,
685 struct rpc_task_setup task_setup = {
686 .rpc_client = clnt,
687 .rpc_message = msg,
688 .callback_ops = &nfs41_call_sync_ops,
689 .callback_data = &data
692 res->sr_slot = NULL;
693 if (privileged)
694 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
695 task = rpc_run_task(&task_setup);
696 if (IS_ERR(task))
697 ret = PTR_ERR(task);
698 else {
699 ret = task->tk_status;
700 rpc_put_task(task);
702 return ret;
705 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
706 struct nfs_server *server,
707 struct rpc_message *msg,
708 struct nfs4_sequence_args *args,
709 struct nfs4_sequence_res *res,
710 int cache_reply)
712 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
715 #else
716 static int nfs4_sequence_done(struct rpc_task *task,
717 struct nfs4_sequence_res *res)
719 return 1;
721 #endif /* CONFIG_NFS_V4_1 */
723 int _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 int cache_reply)
730 args->sa_session = res->sr_session = NULL;
731 return rpc_call_sync(clnt, msg, 0);
734 static inline
735 int nfs4_call_sync(struct rpc_clnt *clnt,
736 struct nfs_server *server,
737 struct rpc_message *msg,
738 struct nfs4_sequence_args *args,
739 struct nfs4_sequence_res *res,
740 int cache_reply)
742 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
743 args, res, cache_reply);
746 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
748 struct nfs_inode *nfsi = NFS_I(dir);
750 spin_lock(&dir->i_lock);
751 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
752 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
753 nfs_force_lookup_revalidate(dir);
754 nfsi->change_attr = cinfo->after;
755 spin_unlock(&dir->i_lock);
758 struct 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 nfs_fattr f_attr;
765 struct nfs_fattr dir_attr;
766 struct path path;
767 struct dentry *dir;
768 struct nfs4_state_owner *owner;
769 struct nfs4_state *state;
770 struct iattr attrs;
771 unsigned long timestamp;
772 unsigned int rpc_done : 1;
773 int rpc_status;
774 int cancelled;
778 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
780 p->o_res.f_attr = &p->f_attr;
781 p->o_res.dir_attr = &p->dir_attr;
782 p->o_res.seqid = p->o_arg.seqid;
783 p->c_res.seqid = p->c_arg.seqid;
784 p->o_res.server = p->o_arg.server;
785 nfs_fattr_init(&p->f_attr);
786 nfs_fattr_init(&p->dir_attr);
789 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
790 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
791 const struct iattr *attrs,
792 gfp_t gfp_mask)
794 struct dentry *parent = dget_parent(path->dentry);
795 struct inode *dir = parent->d_inode;
796 struct nfs_server *server = NFS_SERVER(dir);
797 struct nfs4_opendata *p;
799 p = kzalloc(sizeof(*p), gfp_mask);
800 if (p == NULL)
801 goto err;
802 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
803 if (p->o_arg.seqid == NULL)
804 goto err_free;
805 path_get(path);
806 p->path = *path;
807 p->dir = parent;
808 p->owner = sp;
809 atomic_inc(&sp->so_count);
810 p->o_arg.fh = NFS_FH(dir);
811 p->o_arg.open_flags = flags;
812 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
813 p->o_arg.clientid = server->nfs_client->cl_clientid;
814 p->o_arg.id = sp->so_owner_id.id;
815 p->o_arg.name = &p->path.dentry->d_name;
816 p->o_arg.server = server;
817 p->o_arg.bitmask = server->attr_bitmask;
818 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
819 if (flags & O_CREAT) {
820 u32 *s;
822 p->o_arg.u.attrs = &p->attrs;
823 memcpy(&p->attrs, attrs, sizeof(p->attrs));
824 s = (u32 *) p->o_arg.u.verifier.data;
825 s[0] = jiffies;
826 s[1] = current->pid;
828 p->c_arg.fh = &p->o_res.fh;
829 p->c_arg.stateid = &p->o_res.stateid;
830 p->c_arg.seqid = p->o_arg.seqid;
831 nfs4_init_opendata_res(p);
832 kref_init(&p->kref);
833 return p;
834 err_free:
835 kfree(p);
836 err:
837 dput(parent);
838 return NULL;
841 static void nfs4_opendata_free(struct kref *kref)
843 struct nfs4_opendata *p = container_of(kref,
844 struct nfs4_opendata, kref);
846 nfs_free_seqid(p->o_arg.seqid);
847 if (p->state != NULL)
848 nfs4_put_open_state(p->state);
849 nfs4_put_state_owner(p->owner);
850 dput(p->dir);
851 path_put(&p->path);
852 kfree(p);
855 static void nfs4_opendata_put(struct nfs4_opendata *p)
857 if (p != NULL)
858 kref_put(&p->kref, nfs4_opendata_free);
861 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
863 int ret;
865 ret = rpc_wait_for_completion_task(task);
866 return ret;
869 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
871 int ret = 0;
873 if (open_mode & O_EXCL)
874 goto out;
875 switch (mode & (FMODE_READ|FMODE_WRITE)) {
876 case FMODE_READ:
877 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
878 && state->n_rdonly != 0;
879 break;
880 case FMODE_WRITE:
881 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
882 && state->n_wronly != 0;
883 break;
884 case FMODE_READ|FMODE_WRITE:
885 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
886 && state->n_rdwr != 0;
888 out:
889 return ret;
892 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
894 if ((delegation->type & fmode) != fmode)
895 return 0;
896 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
897 return 0;
898 nfs_mark_delegation_referenced(delegation);
899 return 1;
902 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
904 switch (fmode) {
905 case FMODE_WRITE:
906 state->n_wronly++;
907 break;
908 case FMODE_READ:
909 state->n_rdonly++;
910 break;
911 case FMODE_READ|FMODE_WRITE:
912 state->n_rdwr++;
914 nfs4_state_set_mode_locked(state, state->state | fmode);
917 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
919 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
920 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
921 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
922 switch (fmode) {
923 case FMODE_READ:
924 set_bit(NFS_O_RDONLY_STATE, &state->flags);
925 break;
926 case FMODE_WRITE:
927 set_bit(NFS_O_WRONLY_STATE, &state->flags);
928 break;
929 case FMODE_READ|FMODE_WRITE:
930 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
936 write_seqlock(&state->seqlock);
937 nfs_set_open_stateid_locked(state, stateid, fmode);
938 write_sequnlock(&state->seqlock);
941 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
944 * Protect the call to nfs4_state_set_mode_locked and
945 * serialise the stateid update
947 write_seqlock(&state->seqlock);
948 if (deleg_stateid != NULL) {
949 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
950 set_bit(NFS_DELEGATED_STATE, &state->flags);
952 if (open_stateid != NULL)
953 nfs_set_open_stateid_locked(state, open_stateid, fmode);
954 write_sequnlock(&state->seqlock);
955 spin_lock(&state->owner->so_lock);
956 update_open_stateflags(state, fmode);
957 spin_unlock(&state->owner->so_lock);
960 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
962 struct nfs_inode *nfsi = NFS_I(state->inode);
963 struct nfs_delegation *deleg_cur;
964 int ret = 0;
966 fmode &= (FMODE_READ|FMODE_WRITE);
968 rcu_read_lock();
969 deleg_cur = rcu_dereference(nfsi->delegation);
970 if (deleg_cur == NULL)
971 goto no_delegation;
973 spin_lock(&deleg_cur->lock);
974 if (nfsi->delegation != deleg_cur ||
975 (deleg_cur->type & fmode) != fmode)
976 goto no_delegation_unlock;
978 if (delegation == NULL)
979 delegation = &deleg_cur->stateid;
980 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
981 goto no_delegation_unlock;
983 nfs_mark_delegation_referenced(deleg_cur);
984 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
985 ret = 1;
986 no_delegation_unlock:
987 spin_unlock(&deleg_cur->lock);
988 no_delegation:
989 rcu_read_unlock();
991 if (!ret && open_stateid != NULL) {
992 __update_open_stateid(state, open_stateid, NULL, fmode);
993 ret = 1;
996 return ret;
1000 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1002 struct nfs_delegation *delegation;
1004 rcu_read_lock();
1005 delegation = rcu_dereference(NFS_I(inode)->delegation);
1006 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1007 rcu_read_unlock();
1008 return;
1010 rcu_read_unlock();
1011 nfs_inode_return_delegation(inode);
1014 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1016 struct nfs4_state *state = opendata->state;
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *delegation;
1019 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1020 fmode_t fmode = opendata->o_arg.fmode;
1021 nfs4_stateid stateid;
1022 int ret = -EAGAIN;
1024 for (;;) {
1025 if (can_open_cached(state, fmode, open_mode)) {
1026 spin_lock(&state->owner->so_lock);
1027 if (can_open_cached(state, fmode, open_mode)) {
1028 update_open_stateflags(state, fmode);
1029 spin_unlock(&state->owner->so_lock);
1030 goto out_return_state;
1032 spin_unlock(&state->owner->so_lock);
1034 rcu_read_lock();
1035 delegation = rcu_dereference(nfsi->delegation);
1036 if (delegation == NULL ||
1037 !can_open_delegated(delegation, fmode)) {
1038 rcu_read_unlock();
1039 break;
1041 /* Save the delegation */
1042 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1043 rcu_read_unlock();
1044 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1045 if (ret != 0)
1046 goto out;
1047 ret = -EAGAIN;
1049 /* Try to update the stateid using the delegation */
1050 if (update_open_stateid(state, NULL, &stateid, fmode))
1051 goto out_return_state;
1053 out:
1054 return ERR_PTR(ret);
1055 out_return_state:
1056 atomic_inc(&state->count);
1057 return state;
1060 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1062 struct inode *inode;
1063 struct nfs4_state *state = NULL;
1064 struct nfs_delegation *delegation;
1065 int ret;
1067 if (!data->rpc_done) {
1068 state = nfs4_try_open_cached(data);
1069 goto out;
1072 ret = -EAGAIN;
1073 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1074 goto err;
1075 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1076 ret = PTR_ERR(inode);
1077 if (IS_ERR(inode))
1078 goto err;
1079 ret = -ENOMEM;
1080 state = nfs4_get_open_state(inode, data->owner);
1081 if (state == NULL)
1082 goto err_put_inode;
1083 if (data->o_res.delegation_type != 0) {
1084 int delegation_flags = 0;
1086 rcu_read_lock();
1087 delegation = rcu_dereference(NFS_I(inode)->delegation);
1088 if (delegation)
1089 delegation_flags = delegation->flags;
1090 rcu_read_unlock();
1091 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1092 nfs_inode_set_delegation(state->inode,
1093 data->owner->so_cred,
1094 &data->o_res);
1095 else
1096 nfs_inode_reclaim_delegation(state->inode,
1097 data->owner->so_cred,
1098 &data->o_res);
1101 update_open_stateid(state, &data->o_res.stateid, NULL,
1102 data->o_arg.fmode);
1103 iput(inode);
1104 out:
1105 return state;
1106 err_put_inode:
1107 iput(inode);
1108 err:
1109 return ERR_PTR(ret);
1112 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1114 struct nfs_inode *nfsi = NFS_I(state->inode);
1115 struct nfs_open_context *ctx;
1117 spin_lock(&state->inode->i_lock);
1118 list_for_each_entry(ctx, &nfsi->open_files, list) {
1119 if (ctx->state != state)
1120 continue;
1121 get_nfs_open_context(ctx);
1122 spin_unlock(&state->inode->i_lock);
1123 return ctx;
1125 spin_unlock(&state->inode->i_lock);
1126 return ERR_PTR(-ENOENT);
1129 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1131 struct nfs4_opendata *opendata;
1133 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1134 if (opendata == NULL)
1135 return ERR_PTR(-ENOMEM);
1136 opendata->state = state;
1137 atomic_inc(&state->count);
1138 return opendata;
1141 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1143 struct nfs4_state *newstate;
1144 int ret;
1146 opendata->o_arg.open_flags = 0;
1147 opendata->o_arg.fmode = fmode;
1148 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1149 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1150 nfs4_init_opendata_res(opendata);
1151 ret = _nfs4_recover_proc_open(opendata);
1152 if (ret != 0)
1153 return ret;
1154 newstate = nfs4_opendata_to_nfs4_state(opendata);
1155 if (IS_ERR(newstate))
1156 return PTR_ERR(newstate);
1157 nfs4_close_state(&opendata->path, newstate, fmode);
1158 *res = newstate;
1159 return 0;
1162 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1164 struct nfs4_state *newstate;
1165 int ret;
1167 /* memory barrier prior to reading state->n_* */
1168 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1169 smp_rmb();
1170 if (state->n_rdwr != 0) {
1171 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1172 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1173 if (ret != 0)
1174 return ret;
1175 if (newstate != state)
1176 return -ESTALE;
1178 if (state->n_wronly != 0) {
1179 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1180 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1181 if (ret != 0)
1182 return ret;
1183 if (newstate != state)
1184 return -ESTALE;
1186 if (state->n_rdonly != 0) {
1187 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1188 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1189 if (ret != 0)
1190 return ret;
1191 if (newstate != state)
1192 return -ESTALE;
1195 * We may have performed cached opens for all three recoveries.
1196 * Check if we need to update the current stateid.
1198 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1199 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1200 write_seqlock(&state->seqlock);
1201 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1202 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1203 write_sequnlock(&state->seqlock);
1205 return 0;
1209 * OPEN_RECLAIM:
1210 * reclaim state on the server after a reboot.
1212 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1214 struct nfs_delegation *delegation;
1215 struct nfs4_opendata *opendata;
1216 fmode_t delegation_type = 0;
1217 int status;
1219 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1220 if (IS_ERR(opendata))
1221 return PTR_ERR(opendata);
1222 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1223 opendata->o_arg.fh = NFS_FH(state->inode);
1224 rcu_read_lock();
1225 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1226 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1227 delegation_type = delegation->type;
1228 rcu_read_unlock();
1229 opendata->o_arg.u.delegation_type = delegation_type;
1230 status = nfs4_open_recover(opendata, state);
1231 nfs4_opendata_put(opendata);
1232 return status;
1235 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1237 struct nfs_server *server = NFS_SERVER(state->inode);
1238 struct nfs4_exception exception = { };
1239 int err;
1240 do {
1241 err = _nfs4_do_open_reclaim(ctx, state);
1242 if (err != -NFS4ERR_DELAY)
1243 break;
1244 nfs4_handle_exception(server, err, &exception);
1245 } while (exception.retry);
1246 return err;
1249 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1251 struct nfs_open_context *ctx;
1252 int ret;
1254 ctx = nfs4_state_find_open_context(state);
1255 if (IS_ERR(ctx))
1256 return PTR_ERR(ctx);
1257 ret = nfs4_do_open_reclaim(ctx, state);
1258 put_nfs_open_context(ctx);
1259 return ret;
1262 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1264 struct nfs4_opendata *opendata;
1265 int ret;
1267 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1268 if (IS_ERR(opendata))
1269 return PTR_ERR(opendata);
1270 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1271 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1272 sizeof(opendata->o_arg.u.delegation.data));
1273 ret = nfs4_open_recover(opendata, state);
1274 nfs4_opendata_put(opendata);
1275 return ret;
1278 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1280 struct nfs4_exception exception = { };
1281 struct nfs_server *server = NFS_SERVER(state->inode);
1282 int err;
1283 do {
1284 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1285 switch (err) {
1286 case 0:
1287 case -ENOENT:
1288 case -ESTALE:
1289 goto out;
1290 case -NFS4ERR_BADSESSION:
1291 case -NFS4ERR_BADSLOT:
1292 case -NFS4ERR_BAD_HIGH_SLOT:
1293 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1294 case -NFS4ERR_DEADSESSION:
1295 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1296 goto out;
1297 case -NFS4ERR_STALE_CLIENTID:
1298 case -NFS4ERR_STALE_STATEID:
1299 case -NFS4ERR_EXPIRED:
1300 /* Don't recall a delegation if it was lost */
1301 nfs4_schedule_lease_recovery(server->nfs_client);
1302 goto out;
1303 case -ERESTARTSYS:
1305 * The show must go on: exit, but mark the
1306 * stateid as needing recovery.
1308 case -NFS4ERR_ADMIN_REVOKED:
1309 case -NFS4ERR_BAD_STATEID:
1310 nfs4_schedule_stateid_recovery(server, state);
1311 case -EKEYEXPIRED:
1313 * User RPCSEC_GSS context has expired.
1314 * We cannot recover this stateid now, so
1315 * skip it and allow recovery thread to
1316 * proceed.
1318 case -ENOMEM:
1319 err = 0;
1320 goto out;
1322 err = nfs4_handle_exception(server, err, &exception);
1323 } while (exception.retry);
1324 out:
1325 return err;
1328 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1330 struct nfs4_opendata *data = calldata;
1332 data->rpc_status = task->tk_status;
1333 if (data->rpc_status == 0) {
1334 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1335 sizeof(data->o_res.stateid.data));
1336 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1337 renew_lease(data->o_res.server, data->timestamp);
1338 data->rpc_done = 1;
1342 static void nfs4_open_confirm_release(void *calldata)
1344 struct nfs4_opendata *data = calldata;
1345 struct nfs4_state *state = NULL;
1347 /* If this request hasn't been cancelled, do nothing */
1348 if (data->cancelled == 0)
1349 goto out_free;
1350 /* In case of error, no cleanup! */
1351 if (!data->rpc_done)
1352 goto out_free;
1353 state = nfs4_opendata_to_nfs4_state(data);
1354 if (!IS_ERR(state))
1355 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1356 out_free:
1357 nfs4_opendata_put(data);
1360 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1361 .rpc_call_done = nfs4_open_confirm_done,
1362 .rpc_release = nfs4_open_confirm_release,
1366 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1368 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1370 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1371 struct rpc_task *task;
1372 struct rpc_message msg = {
1373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1374 .rpc_argp = &data->c_arg,
1375 .rpc_resp = &data->c_res,
1376 .rpc_cred = data->owner->so_cred,
1378 struct rpc_task_setup task_setup_data = {
1379 .rpc_client = server->client,
1380 .rpc_message = &msg,
1381 .callback_ops = &nfs4_open_confirm_ops,
1382 .callback_data = data,
1383 .workqueue = nfsiod_workqueue,
1384 .flags = RPC_TASK_ASYNC,
1386 int status;
1388 kref_get(&data->kref);
1389 data->rpc_done = 0;
1390 data->rpc_status = 0;
1391 data->timestamp = jiffies;
1392 task = rpc_run_task(&task_setup_data);
1393 if (IS_ERR(task))
1394 return PTR_ERR(task);
1395 status = nfs4_wait_for_completion_rpc_task(task);
1396 if (status != 0) {
1397 data->cancelled = 1;
1398 smp_wmb();
1399 } else
1400 status = data->rpc_status;
1401 rpc_put_task(task);
1402 return status;
1405 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1407 struct nfs4_opendata *data = calldata;
1408 struct nfs4_state_owner *sp = data->owner;
1410 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1411 return;
1413 * Check if we still need to send an OPEN call, or if we can use
1414 * a delegation instead.
1416 if (data->state != NULL) {
1417 struct nfs_delegation *delegation;
1419 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1420 goto out_no_action;
1421 rcu_read_lock();
1422 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1423 if (delegation != NULL &&
1424 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1425 rcu_read_unlock();
1426 goto out_no_action;
1428 rcu_read_unlock();
1430 /* Update sequence id. */
1431 data->o_arg.id = sp->so_owner_id.id;
1432 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1433 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1434 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1435 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1437 data->timestamp = jiffies;
1438 if (nfs4_setup_sequence(data->o_arg.server,
1439 &data->o_arg.seq_args,
1440 &data->o_res.seq_res, 1, task))
1441 return;
1442 rpc_call_start(task);
1443 return;
1444 out_no_action:
1445 task->tk_action = NULL;
1449 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1451 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1452 nfs4_open_prepare(task, calldata);
1455 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1457 struct nfs4_opendata *data = calldata;
1459 data->rpc_status = task->tk_status;
1461 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1462 return;
1464 if (task->tk_status == 0) {
1465 switch (data->o_res.f_attr->mode & S_IFMT) {
1466 case S_IFREG:
1467 break;
1468 case S_IFLNK:
1469 data->rpc_status = -ELOOP;
1470 break;
1471 case S_IFDIR:
1472 data->rpc_status = -EISDIR;
1473 break;
1474 default:
1475 data->rpc_status = -ENOTDIR;
1477 renew_lease(data->o_res.server, data->timestamp);
1478 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1479 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1481 data->rpc_done = 1;
1484 static void nfs4_open_release(void *calldata)
1486 struct nfs4_opendata *data = calldata;
1487 struct nfs4_state *state = NULL;
1489 /* If this request hasn't been cancelled, do nothing */
1490 if (data->cancelled == 0)
1491 goto out_free;
1492 /* In case of error, no cleanup! */
1493 if (data->rpc_status != 0 || !data->rpc_done)
1494 goto out_free;
1495 /* In case we need an open_confirm, no cleanup! */
1496 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1497 goto out_free;
1498 state = nfs4_opendata_to_nfs4_state(data);
1499 if (!IS_ERR(state))
1500 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1501 out_free:
1502 nfs4_opendata_put(data);
1505 static const struct rpc_call_ops nfs4_open_ops = {
1506 .rpc_call_prepare = nfs4_open_prepare,
1507 .rpc_call_done = nfs4_open_done,
1508 .rpc_release = nfs4_open_release,
1511 static const struct rpc_call_ops nfs4_recover_open_ops = {
1512 .rpc_call_prepare = nfs4_recover_open_prepare,
1513 .rpc_call_done = nfs4_open_done,
1514 .rpc_release = nfs4_open_release,
1517 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1519 struct inode *dir = data->dir->d_inode;
1520 struct nfs_server *server = NFS_SERVER(dir);
1521 struct nfs_openargs *o_arg = &data->o_arg;
1522 struct nfs_openres *o_res = &data->o_res;
1523 struct rpc_task *task;
1524 struct rpc_message msg = {
1525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1526 .rpc_argp = o_arg,
1527 .rpc_resp = o_res,
1528 .rpc_cred = data->owner->so_cred,
1530 struct rpc_task_setup task_setup_data = {
1531 .rpc_client = server->client,
1532 .rpc_message = &msg,
1533 .callback_ops = &nfs4_open_ops,
1534 .callback_data = data,
1535 .workqueue = nfsiod_workqueue,
1536 .flags = RPC_TASK_ASYNC,
1538 int status;
1540 kref_get(&data->kref);
1541 data->rpc_done = 0;
1542 data->rpc_status = 0;
1543 data->cancelled = 0;
1544 if (isrecover)
1545 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1546 task = rpc_run_task(&task_setup_data);
1547 if (IS_ERR(task))
1548 return PTR_ERR(task);
1549 status = nfs4_wait_for_completion_rpc_task(task);
1550 if (status != 0) {
1551 data->cancelled = 1;
1552 smp_wmb();
1553 } else
1554 status = data->rpc_status;
1555 rpc_put_task(task);
1557 return status;
1560 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1562 struct inode *dir = data->dir->d_inode;
1563 struct nfs_openres *o_res = &data->o_res;
1564 int status;
1566 status = nfs4_run_open_task(data, 1);
1567 if (status != 0 || !data->rpc_done)
1568 return status;
1570 nfs_refresh_inode(dir, o_res->dir_attr);
1572 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1573 status = _nfs4_proc_open_confirm(data);
1574 if (status != 0)
1575 return status;
1578 return status;
1582 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1584 static int _nfs4_proc_open(struct nfs4_opendata *data)
1586 struct inode *dir = data->dir->d_inode;
1587 struct nfs_server *server = NFS_SERVER(dir);
1588 struct nfs_openargs *o_arg = &data->o_arg;
1589 struct nfs_openres *o_res = &data->o_res;
1590 int status;
1592 status = nfs4_run_open_task(data, 0);
1593 if (status != 0 || !data->rpc_done)
1594 return status;
1596 if (o_arg->open_flags & O_CREAT) {
1597 update_changeattr(dir, &o_res->cinfo);
1598 nfs_post_op_update_inode(dir, o_res->dir_attr);
1599 } else
1600 nfs_refresh_inode(dir, o_res->dir_attr);
1601 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1602 server->caps &= ~NFS_CAP_POSIX_LOCK;
1603 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1604 status = _nfs4_proc_open_confirm(data);
1605 if (status != 0)
1606 return status;
1608 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1609 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1610 return 0;
1613 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1615 unsigned int loop;
1616 int ret;
1618 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1619 ret = nfs4_wait_clnt_recover(clp);
1620 if (ret != 0)
1621 break;
1622 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1623 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1624 break;
1625 nfs4_schedule_state_manager(clp);
1626 ret = -EIO;
1628 return ret;
1631 static int nfs4_recover_expired_lease(struct nfs_server *server)
1633 return nfs4_client_recover_expired_lease(server->nfs_client);
1637 * OPEN_EXPIRED:
1638 * reclaim state on the server after a network partition.
1639 * Assumes caller holds the appropriate lock
1641 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1643 struct nfs4_opendata *opendata;
1644 int ret;
1646 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1647 if (IS_ERR(opendata))
1648 return PTR_ERR(opendata);
1649 ret = nfs4_open_recover(opendata, state);
1650 if (ret == -ESTALE)
1651 d_drop(ctx->path.dentry);
1652 nfs4_opendata_put(opendata);
1653 return ret;
1656 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1658 struct nfs_server *server = NFS_SERVER(state->inode);
1659 struct nfs4_exception exception = { };
1660 int err;
1662 do {
1663 err = _nfs4_open_expired(ctx, state);
1664 switch (err) {
1665 default:
1666 goto out;
1667 case -NFS4ERR_GRACE:
1668 case -NFS4ERR_DELAY:
1669 nfs4_handle_exception(server, err, &exception);
1670 err = 0;
1672 } while (exception.retry);
1673 out:
1674 return err;
1677 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1679 struct nfs_open_context *ctx;
1680 int ret;
1682 ctx = nfs4_state_find_open_context(state);
1683 if (IS_ERR(ctx))
1684 return PTR_ERR(ctx);
1685 ret = nfs4_do_open_expired(ctx, state);
1686 put_nfs_open_context(ctx);
1687 return ret;
1691 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1692 * fields corresponding to attributes that were used to store the verifier.
1693 * Make sure we clobber those fields in the later setattr call
1695 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1697 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1698 !(sattr->ia_valid & ATTR_ATIME_SET))
1699 sattr->ia_valid |= ATTR_ATIME;
1701 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1702 !(sattr->ia_valid & ATTR_MTIME_SET))
1703 sattr->ia_valid |= ATTR_MTIME;
1707 * Returns a referenced nfs4_state
1709 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1711 struct nfs4_state_owner *sp;
1712 struct nfs4_state *state = NULL;
1713 struct nfs_server *server = NFS_SERVER(dir);
1714 struct nfs4_opendata *opendata;
1715 int status;
1717 /* Protect against reboot recovery conflicts */
1718 status = -ENOMEM;
1719 if (!(sp = nfs4_get_state_owner(server, cred))) {
1720 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1721 goto out_err;
1723 status = nfs4_recover_expired_lease(server);
1724 if (status != 0)
1725 goto err_put_state_owner;
1726 if (path->dentry->d_inode != NULL)
1727 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1728 status = -ENOMEM;
1729 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1730 if (opendata == NULL)
1731 goto err_put_state_owner;
1733 if (path->dentry->d_inode != NULL)
1734 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1736 status = _nfs4_proc_open(opendata);
1737 if (status != 0)
1738 goto err_opendata_put;
1740 state = nfs4_opendata_to_nfs4_state(opendata);
1741 status = PTR_ERR(state);
1742 if (IS_ERR(state))
1743 goto err_opendata_put;
1744 if (server->caps & NFS_CAP_POSIX_LOCK)
1745 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1747 if (opendata->o_arg.open_flags & O_EXCL) {
1748 nfs4_exclusive_attrset(opendata, sattr);
1750 nfs_fattr_init(opendata->o_res.f_attr);
1751 status = nfs4_do_setattr(state->inode, cred,
1752 opendata->o_res.f_attr, sattr,
1753 state);
1754 if (status == 0)
1755 nfs_setattr_update_inode(state->inode, sattr);
1756 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1758 nfs4_opendata_put(opendata);
1759 nfs4_put_state_owner(sp);
1760 *res = state;
1761 return 0;
1762 err_opendata_put:
1763 nfs4_opendata_put(opendata);
1764 err_put_state_owner:
1765 nfs4_put_state_owner(sp);
1766 out_err:
1767 *res = NULL;
1768 return status;
1772 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1774 struct nfs4_exception exception = { };
1775 struct nfs4_state *res;
1776 int status;
1778 do {
1779 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1780 if (status == 0)
1781 break;
1782 /* NOTE: BAD_SEQID means the server and client disagree about the
1783 * book-keeping w.r.t. state-changing operations
1784 * (OPEN/CLOSE/LOCK/LOCKU...)
1785 * It is actually a sign of a bug on the client or on the server.
1787 * If we receive a BAD_SEQID error in the particular case of
1788 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1789 * have unhashed the old state_owner for us, and that we can
1790 * therefore safely retry using a new one. We should still warn
1791 * the user though...
1793 if (status == -NFS4ERR_BAD_SEQID) {
1794 printk(KERN_WARNING "NFS: v4 server %s "
1795 " returned a bad sequence-id error!\n",
1796 NFS_SERVER(dir)->nfs_client->cl_hostname);
1797 exception.retry = 1;
1798 continue;
1801 * BAD_STATEID on OPEN means that the server cancelled our
1802 * state before it received the OPEN_CONFIRM.
1803 * Recover by retrying the request as per the discussion
1804 * on Page 181 of RFC3530.
1806 if (status == -NFS4ERR_BAD_STATEID) {
1807 exception.retry = 1;
1808 continue;
1810 if (status == -EAGAIN) {
1811 /* We must have found a delegation */
1812 exception.retry = 1;
1813 continue;
1815 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1816 status, &exception));
1817 } while (exception.retry);
1818 return res;
1821 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1822 struct nfs_fattr *fattr, struct iattr *sattr,
1823 struct nfs4_state *state)
1825 struct nfs_server *server = NFS_SERVER(inode);
1826 struct nfs_setattrargs arg = {
1827 .fh = NFS_FH(inode),
1828 .iap = sattr,
1829 .server = server,
1830 .bitmask = server->attr_bitmask,
1832 struct nfs_setattrres res = {
1833 .fattr = fattr,
1834 .server = server,
1836 struct rpc_message msg = {
1837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1838 .rpc_argp = &arg,
1839 .rpc_resp = &res,
1840 .rpc_cred = cred,
1842 unsigned long timestamp = jiffies;
1843 int status;
1845 nfs_fattr_init(fattr);
1847 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1848 /* Use that stateid */
1849 } else if (state != NULL) {
1850 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1851 } else
1852 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1854 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1855 if (status == 0 && state != NULL)
1856 renew_lease(server, timestamp);
1857 return status;
1860 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1861 struct nfs_fattr *fattr, struct iattr *sattr,
1862 struct nfs4_state *state)
1864 struct nfs_server *server = NFS_SERVER(inode);
1865 struct nfs4_exception exception = { };
1866 int err;
1867 do {
1868 err = nfs4_handle_exception(server,
1869 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1870 &exception);
1871 } while (exception.retry);
1872 return err;
1875 struct nfs4_closedata {
1876 struct path path;
1877 struct inode *inode;
1878 struct nfs4_state *state;
1879 struct nfs_closeargs arg;
1880 struct nfs_closeres res;
1881 struct nfs_fattr fattr;
1882 unsigned long timestamp;
1883 bool roc;
1884 u32 roc_barrier;
1887 static void nfs4_free_closedata(void *data)
1889 struct nfs4_closedata *calldata = data;
1890 struct nfs4_state_owner *sp = calldata->state->owner;
1892 if (calldata->roc)
1893 pnfs_roc_release(calldata->state->inode);
1894 nfs4_put_open_state(calldata->state);
1895 nfs_free_seqid(calldata->arg.seqid);
1896 nfs4_put_state_owner(sp);
1897 path_put(&calldata->path);
1898 kfree(calldata);
1901 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1902 fmode_t fmode)
1904 spin_lock(&state->owner->so_lock);
1905 if (!(fmode & FMODE_READ))
1906 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1907 if (!(fmode & FMODE_WRITE))
1908 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1909 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1910 spin_unlock(&state->owner->so_lock);
1913 static void nfs4_close_done(struct rpc_task *task, void *data)
1915 struct nfs4_closedata *calldata = data;
1916 struct nfs4_state *state = calldata->state;
1917 struct nfs_server *server = NFS_SERVER(calldata->inode);
1919 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1920 return;
1921 /* hmm. we are done with the inode, and in the process of freeing
1922 * the state_owner. we keep this around to process errors
1924 switch (task->tk_status) {
1925 case 0:
1926 if (calldata->roc)
1927 pnfs_roc_set_barrier(state->inode,
1928 calldata->roc_barrier);
1929 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1930 renew_lease(server, calldata->timestamp);
1931 nfs4_close_clear_stateid_flags(state,
1932 calldata->arg.fmode);
1933 break;
1934 case -NFS4ERR_STALE_STATEID:
1935 case -NFS4ERR_OLD_STATEID:
1936 case -NFS4ERR_BAD_STATEID:
1937 case -NFS4ERR_EXPIRED:
1938 if (calldata->arg.fmode == 0)
1939 break;
1940 default:
1941 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1942 rpc_restart_call_prepare(task);
1944 nfs_release_seqid(calldata->arg.seqid);
1945 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1948 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1950 struct nfs4_closedata *calldata = data;
1951 struct nfs4_state *state = calldata->state;
1952 int call_close = 0;
1954 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1955 return;
1957 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1958 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1959 spin_lock(&state->owner->so_lock);
1960 /* Calculate the change in open mode */
1961 if (state->n_rdwr == 0) {
1962 if (state->n_rdonly == 0) {
1963 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1964 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1965 calldata->arg.fmode &= ~FMODE_READ;
1967 if (state->n_wronly == 0) {
1968 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1969 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1970 calldata->arg.fmode &= ~FMODE_WRITE;
1973 spin_unlock(&state->owner->so_lock);
1975 if (!call_close) {
1976 /* Note: exit _without_ calling nfs4_close_done */
1977 task->tk_action = NULL;
1978 return;
1981 if (calldata->arg.fmode == 0) {
1982 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1983 if (calldata->roc &&
1984 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1985 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1986 task, NULL);
1987 return;
1991 nfs_fattr_init(calldata->res.fattr);
1992 calldata->timestamp = jiffies;
1993 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1994 &calldata->arg.seq_args, &calldata->res.seq_res,
1995 1, task))
1996 return;
1997 rpc_call_start(task);
2000 static const struct rpc_call_ops nfs4_close_ops = {
2001 .rpc_call_prepare = nfs4_close_prepare,
2002 .rpc_call_done = nfs4_close_done,
2003 .rpc_release = nfs4_free_closedata,
2007 * It is possible for data to be read/written from a mem-mapped file
2008 * after the sys_close call (which hits the vfs layer as a flush).
2009 * This means that we can't safely call nfsv4 close on a file until
2010 * the inode is cleared. This in turn means that we are not good
2011 * NFSv4 citizens - we do not indicate to the server to update the file's
2012 * share state even when we are done with one of the three share
2013 * stateid's in the inode.
2015 * NOTE: Caller must be holding the sp->so_owner semaphore!
2017 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2019 struct nfs_server *server = NFS_SERVER(state->inode);
2020 struct nfs4_closedata *calldata;
2021 struct nfs4_state_owner *sp = state->owner;
2022 struct rpc_task *task;
2023 struct rpc_message msg = {
2024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2025 .rpc_cred = state->owner->so_cred,
2027 struct rpc_task_setup task_setup_data = {
2028 .rpc_client = server->client,
2029 .rpc_message = &msg,
2030 .callback_ops = &nfs4_close_ops,
2031 .workqueue = nfsiod_workqueue,
2032 .flags = RPC_TASK_ASYNC,
2034 int status = -ENOMEM;
2036 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2037 if (calldata == NULL)
2038 goto out;
2039 calldata->inode = state->inode;
2040 calldata->state = state;
2041 calldata->arg.fh = NFS_FH(state->inode);
2042 calldata->arg.stateid = &state->open_stateid;
2043 /* Serialization for the sequence id */
2044 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2045 if (calldata->arg.seqid == NULL)
2046 goto out_free_calldata;
2047 calldata->arg.fmode = 0;
2048 calldata->arg.bitmask = server->cache_consistency_bitmask;
2049 calldata->res.fattr = &calldata->fattr;
2050 calldata->res.seqid = calldata->arg.seqid;
2051 calldata->res.server = server;
2052 calldata->roc = roc;
2053 path_get(path);
2054 calldata->path = *path;
2056 msg.rpc_argp = &calldata->arg;
2057 msg.rpc_resp = &calldata->res;
2058 task_setup_data.callback_data = calldata;
2059 task = rpc_run_task(&task_setup_data);
2060 if (IS_ERR(task))
2061 return PTR_ERR(task);
2062 status = 0;
2063 if (wait)
2064 status = rpc_wait_for_completion_task(task);
2065 rpc_put_task(task);
2066 return status;
2067 out_free_calldata:
2068 kfree(calldata);
2069 out:
2070 if (roc)
2071 pnfs_roc_release(state->inode);
2072 nfs4_put_open_state(state);
2073 nfs4_put_state_owner(sp);
2074 return status;
2077 static struct inode *
2078 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2080 struct nfs4_state *state;
2082 /* Protect against concurrent sillydeletes */
2083 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2084 if (IS_ERR(state))
2085 return ERR_CAST(state);
2086 ctx->state = state;
2087 return igrab(state->inode);
2090 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2092 if (ctx->state == NULL)
2093 return;
2094 if (is_sync)
2095 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2096 else
2097 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2100 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2102 struct nfs4_server_caps_arg args = {
2103 .fhandle = fhandle,
2105 struct nfs4_server_caps_res res = {};
2106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2108 .rpc_argp = &args,
2109 .rpc_resp = &res,
2111 int status;
2113 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2114 if (status == 0) {
2115 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2116 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2117 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2118 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2119 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2120 NFS_CAP_CTIME|NFS_CAP_MTIME);
2121 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2122 server->caps |= NFS_CAP_ACLS;
2123 if (res.has_links != 0)
2124 server->caps |= NFS_CAP_HARDLINKS;
2125 if (res.has_symlinks != 0)
2126 server->caps |= NFS_CAP_SYMLINKS;
2127 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2128 server->caps |= NFS_CAP_FILEID;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2130 server->caps |= NFS_CAP_MODE;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2132 server->caps |= NFS_CAP_NLINK;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2134 server->caps |= NFS_CAP_OWNER;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2136 server->caps |= NFS_CAP_OWNER_GROUP;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2138 server->caps |= NFS_CAP_ATIME;
2139 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2140 server->caps |= NFS_CAP_CTIME;
2141 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2142 server->caps |= NFS_CAP_MTIME;
2144 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2145 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2146 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2147 server->acl_bitmask = res.acl_bitmask;
2150 return status;
2153 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2155 struct nfs4_exception exception = { };
2156 int err;
2157 do {
2158 err = nfs4_handle_exception(server,
2159 _nfs4_server_capabilities(server, fhandle),
2160 &exception);
2161 } while (exception.retry);
2162 return err;
2165 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2166 struct nfs_fsinfo *info)
2168 struct nfs4_lookup_root_arg args = {
2169 .bitmask = nfs4_fattr_bitmap,
2171 struct nfs4_lookup_res res = {
2172 .server = server,
2173 .fattr = info->fattr,
2174 .fh = fhandle,
2176 struct rpc_message msg = {
2177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2178 .rpc_argp = &args,
2179 .rpc_resp = &res,
2182 nfs_fattr_init(info->fattr);
2183 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2186 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2187 struct nfs_fsinfo *info)
2189 struct nfs4_exception exception = { };
2190 int err;
2191 do {
2192 err = _nfs4_lookup_root(server, fhandle, info);
2193 switch (err) {
2194 case 0:
2195 case -NFS4ERR_WRONGSEC:
2196 break;
2197 default:
2198 err = nfs4_handle_exception(server, err, &exception);
2200 } while (exception.retry);
2201 return err;
2204 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2205 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2207 struct rpc_auth *auth;
2208 int ret;
2210 auth = rpcauth_create(flavor, server->client);
2211 if (!auth) {
2212 ret = -EIO;
2213 goto out;
2215 ret = nfs4_lookup_root(server, fhandle, info);
2216 out:
2217 return ret;
2220 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsinfo *info)
2223 int i, len, status = 0;
2224 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2226 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2227 flav_array[len] = RPC_AUTH_NULL;
2228 len += 1;
2230 for (i = 0; i < len; i++) {
2231 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2232 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2233 continue;
2234 break;
2237 * -EACCESS could mean that the user doesn't have correct permissions
2238 * to access the mount. It could also mean that we tried to mount
2239 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2240 * existing mount programs don't handle -EACCES very well so it should
2241 * be mapped to -EPERM instead.
2243 if (status == -EACCES)
2244 status = -EPERM;
2245 return status;
2249 * get the file handle for the "/" directory on the server
2251 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2252 struct nfs_fsinfo *info)
2254 int status = nfs4_lookup_root(server, fhandle, info);
2255 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2257 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2258 * by nfs4_map_errors() as this function exits.
2260 status = nfs4_find_root_sec(server, fhandle, info);
2261 if (status == 0)
2262 status = nfs4_server_capabilities(server, fhandle);
2263 if (status == 0)
2264 status = nfs4_do_fsinfo(server, fhandle, info);
2265 return nfs4_map_errors(status);
2268 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2270 * Get locations and (maybe) other attributes of a referral.
2271 * Note that we'll actually follow the referral later when
2272 * we detect fsid mismatch in inode revalidation
2274 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2275 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2277 int status = -ENOMEM;
2278 struct page *page = NULL;
2279 struct nfs4_fs_locations *locations = NULL;
2281 page = alloc_page(GFP_KERNEL);
2282 if (page == NULL)
2283 goto out;
2284 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2285 if (locations == NULL)
2286 goto out;
2288 status = nfs4_proc_fs_locations(dir, name, locations, page);
2289 if (status != 0)
2290 goto out;
2291 /* Make sure server returned a different fsid for the referral */
2292 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2293 dprintk("%s: server did not return a different fsid for"
2294 " a referral at %s\n", __func__, name->name);
2295 status = -EIO;
2296 goto out;
2298 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2299 nfs_fixup_referral_attributes(&locations->fattr);
2301 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2302 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2303 memset(fhandle, 0, sizeof(struct nfs_fh));
2304 out:
2305 if (page)
2306 __free_page(page);
2307 kfree(locations);
2308 return status;
2311 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2313 struct nfs4_getattr_arg args = {
2314 .fh = fhandle,
2315 .bitmask = server->attr_bitmask,
2317 struct nfs4_getattr_res res = {
2318 .fattr = fattr,
2319 .server = server,
2321 struct rpc_message msg = {
2322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2323 .rpc_argp = &args,
2324 .rpc_resp = &res,
2327 nfs_fattr_init(fattr);
2328 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2331 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2333 struct nfs4_exception exception = { };
2334 int err;
2335 do {
2336 err = nfs4_handle_exception(server,
2337 _nfs4_proc_getattr(server, fhandle, fattr),
2338 &exception);
2339 } while (exception.retry);
2340 return err;
2344 * The file is not closed if it is opened due to the a request to change
2345 * the size of the file. The open call will not be needed once the
2346 * VFS layer lookup-intents are implemented.
2348 * Close is called when the inode is destroyed.
2349 * If we haven't opened the file for O_WRONLY, we
2350 * need to in the size_change case to obtain a stateid.
2352 * Got race?
2353 * Because OPEN is always done by name in nfsv4, it is
2354 * possible that we opened a different file by the same
2355 * name. We can recognize this race condition, but we
2356 * can't do anything about it besides returning an error.
2358 * This will be fixed with VFS changes (lookup-intent).
2360 static int
2361 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2362 struct iattr *sattr)
2364 struct inode *inode = dentry->d_inode;
2365 struct rpc_cred *cred = NULL;
2366 struct nfs4_state *state = NULL;
2367 int status;
2369 nfs_fattr_init(fattr);
2371 /* Search for an existing open(O_WRITE) file */
2372 if (sattr->ia_valid & ATTR_FILE) {
2373 struct nfs_open_context *ctx;
2375 ctx = nfs_file_open_context(sattr->ia_file);
2376 if (ctx) {
2377 cred = ctx->cred;
2378 state = ctx->state;
2382 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2383 if (status == 0)
2384 nfs_setattr_update_inode(inode, sattr);
2385 return status;
2388 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2389 const struct nfs_fh *dirfh, const struct qstr *name,
2390 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2392 int status;
2393 struct nfs4_lookup_arg args = {
2394 .bitmask = server->attr_bitmask,
2395 .dir_fh = dirfh,
2396 .name = name,
2398 struct nfs4_lookup_res res = {
2399 .server = server,
2400 .fattr = fattr,
2401 .fh = fhandle,
2403 struct rpc_message msg = {
2404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2405 .rpc_argp = &args,
2406 .rpc_resp = &res,
2409 nfs_fattr_init(fattr);
2411 dprintk("NFS call lookupfh %s\n", name->name);
2412 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2413 dprintk("NFS reply lookupfh: %d\n", status);
2414 return status;
2417 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2418 struct qstr *name, struct nfs_fh *fhandle,
2419 struct nfs_fattr *fattr)
2421 struct nfs4_exception exception = { };
2422 int err;
2423 do {
2424 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2425 /* FIXME: !!!! */
2426 if (err == -NFS4ERR_MOVED) {
2427 err = -EREMOTE;
2428 break;
2430 err = nfs4_handle_exception(server, err, &exception);
2431 } while (exception.retry);
2432 return err;
2435 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2436 const struct qstr *name, struct nfs_fh *fhandle,
2437 struct nfs_fattr *fattr)
2439 int status;
2441 dprintk("NFS call lookup %s\n", name->name);
2442 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2443 if (status == -NFS4ERR_MOVED)
2444 status = nfs4_get_referral(dir, name, fattr, fhandle);
2445 dprintk("NFS reply lookup: %d\n", status);
2446 return status;
2449 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2451 memset(fh, 0, sizeof(struct nfs_fh));
2452 fattr->fsid.major = 1;
2453 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2454 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2455 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2456 fattr->nlink = 2;
2459 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2460 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2462 struct nfs4_exception exception = { };
2463 int err;
2464 do {
2465 err = nfs4_handle_exception(NFS_SERVER(dir),
2466 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2467 &exception);
2468 if (err == -EPERM)
2469 nfs_fixup_secinfo_attributes(fattr, fhandle);
2470 } while (exception.retry);
2471 return err;
2474 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2476 struct nfs_server *server = NFS_SERVER(inode);
2477 struct nfs4_accessargs args = {
2478 .fh = NFS_FH(inode),
2479 .bitmask = server->attr_bitmask,
2481 struct nfs4_accessres res = {
2482 .server = server,
2484 struct rpc_message msg = {
2485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2486 .rpc_argp = &args,
2487 .rpc_resp = &res,
2488 .rpc_cred = entry->cred,
2490 int mode = entry->mask;
2491 int status;
2494 * Determine which access bits we want to ask for...
2496 if (mode & MAY_READ)
2497 args.access |= NFS4_ACCESS_READ;
2498 if (S_ISDIR(inode->i_mode)) {
2499 if (mode & MAY_WRITE)
2500 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2501 if (mode & MAY_EXEC)
2502 args.access |= NFS4_ACCESS_LOOKUP;
2503 } else {
2504 if (mode & MAY_WRITE)
2505 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2506 if (mode & MAY_EXEC)
2507 args.access |= NFS4_ACCESS_EXECUTE;
2510 res.fattr = nfs_alloc_fattr();
2511 if (res.fattr == NULL)
2512 return -ENOMEM;
2514 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2515 if (!status) {
2516 entry->mask = 0;
2517 if (res.access & NFS4_ACCESS_READ)
2518 entry->mask |= MAY_READ;
2519 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2520 entry->mask |= MAY_WRITE;
2521 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2522 entry->mask |= MAY_EXEC;
2523 nfs_refresh_inode(inode, res.fattr);
2525 nfs_free_fattr(res.fattr);
2526 return status;
2529 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2531 struct nfs4_exception exception = { };
2532 int err;
2533 do {
2534 err = nfs4_handle_exception(NFS_SERVER(inode),
2535 _nfs4_proc_access(inode, entry),
2536 &exception);
2537 } while (exception.retry);
2538 return err;
2542 * TODO: For the time being, we don't try to get any attributes
2543 * along with any of the zero-copy operations READ, READDIR,
2544 * READLINK, WRITE.
2546 * In the case of the first three, we want to put the GETATTR
2547 * after the read-type operation -- this is because it is hard
2548 * to predict the length of a GETATTR response in v4, and thus
2549 * align the READ data correctly. This means that the GETATTR
2550 * may end up partially falling into the page cache, and we should
2551 * shift it into the 'tail' of the xdr_buf before processing.
2552 * To do this efficiently, we need to know the total length
2553 * of data received, which doesn't seem to be available outside
2554 * of the RPC layer.
2556 * In the case of WRITE, we also want to put the GETATTR after
2557 * the operation -- in this case because we want to make sure
2558 * we get the post-operation mtime and size. This means that
2559 * we can't use xdr_encode_pages() as written: we need a variant
2560 * of it which would leave room in the 'tail' iovec.
2562 * Both of these changes to the XDR layer would in fact be quite
2563 * minor, but I decided to leave them for a subsequent patch.
2565 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2566 unsigned int pgbase, unsigned int pglen)
2568 struct nfs4_readlink args = {
2569 .fh = NFS_FH(inode),
2570 .pgbase = pgbase,
2571 .pglen = pglen,
2572 .pages = &page,
2574 struct nfs4_readlink_res res;
2575 struct rpc_message msg = {
2576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2577 .rpc_argp = &args,
2578 .rpc_resp = &res,
2581 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2584 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2585 unsigned int pgbase, unsigned int pglen)
2587 struct nfs4_exception exception = { };
2588 int err;
2589 do {
2590 err = nfs4_handle_exception(NFS_SERVER(inode),
2591 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2592 &exception);
2593 } while (exception.retry);
2594 return err;
2598 * Got race?
2599 * We will need to arrange for the VFS layer to provide an atomic open.
2600 * Until then, this create/open method is prone to inefficiency and race
2601 * conditions due to the lookup, create, and open VFS calls from sys_open()
2602 * placed on the wire.
2604 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2605 * The file will be opened again in the subsequent VFS open call
2606 * (nfs4_proc_file_open).
2608 * The open for read will just hang around to be used by any process that
2609 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2612 static int
2613 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2614 int flags, struct nfs_open_context *ctx)
2616 struct path my_path = {
2617 .dentry = dentry,
2619 struct path *path = &my_path;
2620 struct nfs4_state *state;
2621 struct rpc_cred *cred = NULL;
2622 fmode_t fmode = 0;
2623 int status = 0;
2625 if (ctx != NULL) {
2626 cred = ctx->cred;
2627 path = &ctx->path;
2628 fmode = ctx->mode;
2630 sattr->ia_mode &= ~current_umask();
2631 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2632 d_drop(dentry);
2633 if (IS_ERR(state)) {
2634 status = PTR_ERR(state);
2635 goto out;
2637 d_add(dentry, igrab(state->inode));
2638 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2639 if (ctx != NULL)
2640 ctx->state = state;
2641 else
2642 nfs4_close_sync(path, state, fmode);
2643 out:
2644 return status;
2647 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2649 struct nfs_server *server = NFS_SERVER(dir);
2650 struct nfs_removeargs args = {
2651 .fh = NFS_FH(dir),
2652 .name.len = name->len,
2653 .name.name = name->name,
2654 .bitmask = server->attr_bitmask,
2656 struct nfs_removeres res = {
2657 .server = server,
2659 struct rpc_message msg = {
2660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2661 .rpc_argp = &args,
2662 .rpc_resp = &res,
2664 int status = -ENOMEM;
2666 res.dir_attr = nfs_alloc_fattr();
2667 if (res.dir_attr == NULL)
2668 goto out;
2670 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2671 if (status == 0) {
2672 update_changeattr(dir, &res.cinfo);
2673 nfs_post_op_update_inode(dir, res.dir_attr);
2675 nfs_free_fattr(res.dir_attr);
2676 out:
2677 return status;
2680 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2682 struct nfs4_exception exception = { };
2683 int err;
2684 do {
2685 err = nfs4_handle_exception(NFS_SERVER(dir),
2686 _nfs4_proc_remove(dir, name),
2687 &exception);
2688 } while (exception.retry);
2689 return err;
2692 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2694 struct nfs_server *server = NFS_SERVER(dir);
2695 struct nfs_removeargs *args = msg->rpc_argp;
2696 struct nfs_removeres *res = msg->rpc_resp;
2698 args->bitmask = server->cache_consistency_bitmask;
2699 res->server = server;
2700 res->seq_res.sr_slot = NULL;
2701 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2704 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2706 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2708 if (!nfs4_sequence_done(task, &res->seq_res))
2709 return 0;
2710 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2711 return 0;
2712 update_changeattr(dir, &res->cinfo);
2713 nfs_post_op_update_inode(dir, res->dir_attr);
2714 return 1;
2717 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2719 struct nfs_server *server = NFS_SERVER(dir);
2720 struct nfs_renameargs *arg = msg->rpc_argp;
2721 struct nfs_renameres *res = msg->rpc_resp;
2723 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2724 arg->bitmask = server->attr_bitmask;
2725 res->server = server;
2728 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2729 struct inode *new_dir)
2731 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2733 if (!nfs4_sequence_done(task, &res->seq_res))
2734 return 0;
2735 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2736 return 0;
2738 update_changeattr(old_dir, &res->old_cinfo);
2739 nfs_post_op_update_inode(old_dir, res->old_fattr);
2740 update_changeattr(new_dir, &res->new_cinfo);
2741 nfs_post_op_update_inode(new_dir, res->new_fattr);
2742 return 1;
2745 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2746 struct inode *new_dir, struct qstr *new_name)
2748 struct nfs_server *server = NFS_SERVER(old_dir);
2749 struct nfs_renameargs arg = {
2750 .old_dir = NFS_FH(old_dir),
2751 .new_dir = NFS_FH(new_dir),
2752 .old_name = old_name,
2753 .new_name = new_name,
2754 .bitmask = server->attr_bitmask,
2756 struct nfs_renameres res = {
2757 .server = server,
2759 struct rpc_message msg = {
2760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2761 .rpc_argp = &arg,
2762 .rpc_resp = &res,
2764 int status = -ENOMEM;
2766 res.old_fattr = nfs_alloc_fattr();
2767 res.new_fattr = nfs_alloc_fattr();
2768 if (res.old_fattr == NULL || res.new_fattr == NULL)
2769 goto out;
2771 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2772 if (!status) {
2773 update_changeattr(old_dir, &res.old_cinfo);
2774 nfs_post_op_update_inode(old_dir, res.old_fattr);
2775 update_changeattr(new_dir, &res.new_cinfo);
2776 nfs_post_op_update_inode(new_dir, res.new_fattr);
2778 out:
2779 nfs_free_fattr(res.new_fattr);
2780 nfs_free_fattr(res.old_fattr);
2781 return status;
2784 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2785 struct inode *new_dir, struct qstr *new_name)
2787 struct nfs4_exception exception = { };
2788 int err;
2789 do {
2790 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2791 _nfs4_proc_rename(old_dir, old_name,
2792 new_dir, new_name),
2793 &exception);
2794 } while (exception.retry);
2795 return err;
2798 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2800 struct nfs_server *server = NFS_SERVER(inode);
2801 struct nfs4_link_arg arg = {
2802 .fh = NFS_FH(inode),
2803 .dir_fh = NFS_FH(dir),
2804 .name = name,
2805 .bitmask = server->attr_bitmask,
2807 struct nfs4_link_res res = {
2808 .server = server,
2810 struct rpc_message msg = {
2811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2812 .rpc_argp = &arg,
2813 .rpc_resp = &res,
2815 int status = -ENOMEM;
2817 res.fattr = nfs_alloc_fattr();
2818 res.dir_attr = nfs_alloc_fattr();
2819 if (res.fattr == NULL || res.dir_attr == NULL)
2820 goto out;
2822 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2823 if (!status) {
2824 update_changeattr(dir, &res.cinfo);
2825 nfs_post_op_update_inode(dir, res.dir_attr);
2826 nfs_post_op_update_inode(inode, res.fattr);
2828 out:
2829 nfs_free_fattr(res.dir_attr);
2830 nfs_free_fattr(res.fattr);
2831 return status;
2834 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2836 struct nfs4_exception exception = { };
2837 int err;
2838 do {
2839 err = nfs4_handle_exception(NFS_SERVER(inode),
2840 _nfs4_proc_link(inode, dir, name),
2841 &exception);
2842 } while (exception.retry);
2843 return err;
2846 struct nfs4_createdata {
2847 struct rpc_message msg;
2848 struct nfs4_create_arg arg;
2849 struct nfs4_create_res res;
2850 struct nfs_fh fh;
2851 struct nfs_fattr fattr;
2852 struct nfs_fattr dir_fattr;
2855 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2856 struct qstr *name, struct iattr *sattr, u32 ftype)
2858 struct nfs4_createdata *data;
2860 data = kzalloc(sizeof(*data), GFP_KERNEL);
2861 if (data != NULL) {
2862 struct nfs_server *server = NFS_SERVER(dir);
2864 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2865 data->msg.rpc_argp = &data->arg;
2866 data->msg.rpc_resp = &data->res;
2867 data->arg.dir_fh = NFS_FH(dir);
2868 data->arg.server = server;
2869 data->arg.name = name;
2870 data->arg.attrs = sattr;
2871 data->arg.ftype = ftype;
2872 data->arg.bitmask = server->attr_bitmask;
2873 data->res.server = server;
2874 data->res.fh = &data->fh;
2875 data->res.fattr = &data->fattr;
2876 data->res.dir_fattr = &data->dir_fattr;
2877 nfs_fattr_init(data->res.fattr);
2878 nfs_fattr_init(data->res.dir_fattr);
2880 return data;
2883 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2885 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2886 &data->arg.seq_args, &data->res.seq_res, 1);
2887 if (status == 0) {
2888 update_changeattr(dir, &data->res.dir_cinfo);
2889 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2890 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2892 return status;
2895 static void nfs4_free_createdata(struct nfs4_createdata *data)
2897 kfree(data);
2900 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2901 struct page *page, unsigned int len, struct iattr *sattr)
2903 struct nfs4_createdata *data;
2904 int status = -ENAMETOOLONG;
2906 if (len > NFS4_MAXPATHLEN)
2907 goto out;
2909 status = -ENOMEM;
2910 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2911 if (data == NULL)
2912 goto out;
2914 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2915 data->arg.u.symlink.pages = &page;
2916 data->arg.u.symlink.len = len;
2918 status = nfs4_do_create(dir, dentry, data);
2920 nfs4_free_createdata(data);
2921 out:
2922 return status;
2925 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2926 struct page *page, unsigned int len, struct iattr *sattr)
2928 struct nfs4_exception exception = { };
2929 int err;
2930 do {
2931 err = nfs4_handle_exception(NFS_SERVER(dir),
2932 _nfs4_proc_symlink(dir, dentry, page,
2933 len, sattr),
2934 &exception);
2935 } while (exception.retry);
2936 return err;
2939 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2940 struct iattr *sattr)
2942 struct nfs4_createdata *data;
2943 int status = -ENOMEM;
2945 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2946 if (data == NULL)
2947 goto out;
2949 status = nfs4_do_create(dir, dentry, data);
2951 nfs4_free_createdata(data);
2952 out:
2953 return status;
2956 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2957 struct iattr *sattr)
2959 struct nfs4_exception exception = { };
2960 int err;
2962 sattr->ia_mode &= ~current_umask();
2963 do {
2964 err = nfs4_handle_exception(NFS_SERVER(dir),
2965 _nfs4_proc_mkdir(dir, dentry, sattr),
2966 &exception);
2967 } while (exception.retry);
2968 return err;
2971 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2972 u64 cookie, struct page **pages, unsigned int count, int plus)
2974 struct inode *dir = dentry->d_inode;
2975 struct nfs4_readdir_arg args = {
2976 .fh = NFS_FH(dir),
2977 .pages = pages,
2978 .pgbase = 0,
2979 .count = count,
2980 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2981 .plus = plus,
2983 struct nfs4_readdir_res res;
2984 struct rpc_message msg = {
2985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2986 .rpc_argp = &args,
2987 .rpc_resp = &res,
2988 .rpc_cred = cred,
2990 int status;
2992 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2993 dentry->d_parent->d_name.name,
2994 dentry->d_name.name,
2995 (unsigned long long)cookie);
2996 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2997 res.pgbase = args.pgbase;
2998 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
2999 if (status >= 0) {
3000 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3001 status += args.pgbase;
3004 nfs_invalidate_atime(dir);
3006 dprintk("%s: returns %d\n", __func__, status);
3007 return status;
3010 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3011 u64 cookie, struct page **pages, unsigned int count, int plus)
3013 struct nfs4_exception exception = { };
3014 int err;
3015 do {
3016 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3017 _nfs4_proc_readdir(dentry, cred, cookie,
3018 pages, count, plus),
3019 &exception);
3020 } while (exception.retry);
3021 return err;
3024 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3025 struct iattr *sattr, dev_t rdev)
3027 struct nfs4_createdata *data;
3028 int mode = sattr->ia_mode;
3029 int status = -ENOMEM;
3031 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3032 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3034 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3035 if (data == NULL)
3036 goto out;
3038 if (S_ISFIFO(mode))
3039 data->arg.ftype = NF4FIFO;
3040 else if (S_ISBLK(mode)) {
3041 data->arg.ftype = NF4BLK;
3042 data->arg.u.device.specdata1 = MAJOR(rdev);
3043 data->arg.u.device.specdata2 = MINOR(rdev);
3045 else if (S_ISCHR(mode)) {
3046 data->arg.ftype = NF4CHR;
3047 data->arg.u.device.specdata1 = MAJOR(rdev);
3048 data->arg.u.device.specdata2 = MINOR(rdev);
3051 status = nfs4_do_create(dir, dentry, data);
3053 nfs4_free_createdata(data);
3054 out:
3055 return status;
3058 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3059 struct iattr *sattr, dev_t rdev)
3061 struct nfs4_exception exception = { };
3062 int err;
3064 sattr->ia_mode &= ~current_umask();
3065 do {
3066 err = nfs4_handle_exception(NFS_SERVER(dir),
3067 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3068 &exception);
3069 } while (exception.retry);
3070 return err;
3073 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3074 struct nfs_fsstat *fsstat)
3076 struct nfs4_statfs_arg args = {
3077 .fh = fhandle,
3078 .bitmask = server->attr_bitmask,
3080 struct nfs4_statfs_res res = {
3081 .fsstat = fsstat,
3083 struct rpc_message msg = {
3084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3085 .rpc_argp = &args,
3086 .rpc_resp = &res,
3089 nfs_fattr_init(fsstat->fattr);
3090 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3093 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3095 struct nfs4_exception exception = { };
3096 int err;
3097 do {
3098 err = nfs4_handle_exception(server,
3099 _nfs4_proc_statfs(server, fhandle, fsstat),
3100 &exception);
3101 } while (exception.retry);
3102 return err;
3105 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3106 struct nfs_fsinfo *fsinfo)
3108 struct nfs4_fsinfo_arg args = {
3109 .fh = fhandle,
3110 .bitmask = server->attr_bitmask,
3112 struct nfs4_fsinfo_res res = {
3113 .fsinfo = fsinfo,
3115 struct rpc_message msg = {
3116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3117 .rpc_argp = &args,
3118 .rpc_resp = &res,
3121 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3124 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3126 struct nfs4_exception exception = { };
3127 int err;
3129 do {
3130 err = nfs4_handle_exception(server,
3131 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3132 &exception);
3133 } while (exception.retry);
3134 return err;
3137 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3139 nfs_fattr_init(fsinfo->fattr);
3140 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3143 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3144 struct nfs_pathconf *pathconf)
3146 struct nfs4_pathconf_arg args = {
3147 .fh = fhandle,
3148 .bitmask = server->attr_bitmask,
3150 struct nfs4_pathconf_res res = {
3151 .pathconf = pathconf,
3153 struct rpc_message msg = {
3154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3155 .rpc_argp = &args,
3156 .rpc_resp = &res,
3159 /* None of the pathconf attributes are mandatory to implement */
3160 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3161 memset(pathconf, 0, sizeof(*pathconf));
3162 return 0;
3165 nfs_fattr_init(pathconf->fattr);
3166 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3169 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3170 struct nfs_pathconf *pathconf)
3172 struct nfs4_exception exception = { };
3173 int err;
3175 do {
3176 err = nfs4_handle_exception(server,
3177 _nfs4_proc_pathconf(server, fhandle, pathconf),
3178 &exception);
3179 } while (exception.retry);
3180 return err;
3183 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3185 struct nfs_server *server = NFS_SERVER(data->inode);
3187 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3188 nfs_restart_rpc(task, server->nfs_client);
3189 return -EAGAIN;
3192 nfs_invalidate_atime(data->inode);
3193 if (task->tk_status > 0)
3194 renew_lease(server, data->timestamp);
3195 return 0;
3198 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3201 dprintk("--> %s\n", __func__);
3203 if (!nfs4_sequence_done(task, &data->res.seq_res))
3204 return -EAGAIN;
3206 return data->read_done_cb(task, data);
3209 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3211 data->timestamp = jiffies;
3212 data->read_done_cb = nfs4_read_done_cb;
3213 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3216 /* Reset the the nfs_read_data to send the read to the MDS. */
3217 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3219 dprintk("%s Reset task for i/o through\n", __func__);
3220 put_lseg(data->lseg);
3221 data->lseg = NULL;
3222 /* offsets will differ in the dense stripe case */
3223 data->args.offset = data->mds_offset;
3224 data->ds_clp = NULL;
3225 data->args.fh = NFS_FH(data->inode);
3226 data->read_done_cb = nfs4_read_done_cb;
3227 task->tk_ops = data->mds_ops;
3228 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3230 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3232 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3234 struct inode *inode = data->inode;
3236 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3237 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3238 return -EAGAIN;
3240 if (task->tk_status >= 0) {
3241 renew_lease(NFS_SERVER(inode), data->timestamp);
3242 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3244 return 0;
3247 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3249 if (!nfs4_sequence_done(task, &data->res.seq_res))
3250 return -EAGAIN;
3251 return data->write_done_cb(task, data);
3254 /* Reset the the nfs_write_data to send the write to the MDS. */
3255 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3257 dprintk("%s Reset task for i/o through\n", __func__);
3258 put_lseg(data->lseg);
3259 data->lseg = NULL;
3260 data->ds_clp = NULL;
3261 data->write_done_cb = nfs4_write_done_cb;
3262 data->args.fh = NFS_FH(data->inode);
3263 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3264 data->args.offset = data->mds_offset;
3265 data->res.fattr = &data->fattr;
3266 task->tk_ops = data->mds_ops;
3267 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3269 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3271 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3273 struct nfs_server *server = NFS_SERVER(data->inode);
3275 if (data->lseg) {
3276 data->args.bitmask = NULL;
3277 data->res.fattr = NULL;
3278 } else
3279 data->args.bitmask = server->cache_consistency_bitmask;
3280 if (!data->write_done_cb)
3281 data->write_done_cb = nfs4_write_done_cb;
3282 data->res.server = server;
3283 data->timestamp = jiffies;
3285 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3288 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3290 struct inode *inode = data->inode;
3292 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3293 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3294 return -EAGAIN;
3296 nfs_refresh_inode(inode, data->res.fattr);
3297 return 0;
3300 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3302 if (!nfs4_sequence_done(task, &data->res.seq_res))
3303 return -EAGAIN;
3304 return data->write_done_cb(task, data);
3307 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3309 struct nfs_server *server = NFS_SERVER(data->inode);
3311 if (data->lseg) {
3312 data->args.bitmask = NULL;
3313 data->res.fattr = NULL;
3314 } else
3315 data->args.bitmask = server->cache_consistency_bitmask;
3316 if (!data->write_done_cb)
3317 data->write_done_cb = nfs4_commit_done_cb;
3318 data->res.server = server;
3319 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3322 struct nfs4_renewdata {
3323 struct nfs_client *client;
3324 unsigned long timestamp;
3328 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3329 * standalone procedure for queueing an asynchronous RENEW.
3331 static void nfs4_renew_release(void *calldata)
3333 struct nfs4_renewdata *data = calldata;
3334 struct nfs_client *clp = data->client;
3336 if (atomic_read(&clp->cl_count) > 1)
3337 nfs4_schedule_state_renewal(clp);
3338 nfs_put_client(clp);
3339 kfree(data);
3342 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3344 struct nfs4_renewdata *data = calldata;
3345 struct nfs_client *clp = data->client;
3346 unsigned long timestamp = data->timestamp;
3348 if (task->tk_status < 0) {
3349 /* Unless we're shutting down, schedule state recovery! */
3350 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3351 nfs4_schedule_lease_recovery(clp);
3352 return;
3354 do_renew_lease(clp, timestamp);
3357 static const struct rpc_call_ops nfs4_renew_ops = {
3358 .rpc_call_done = nfs4_renew_done,
3359 .rpc_release = nfs4_renew_release,
3362 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3364 struct rpc_message msg = {
3365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3366 .rpc_argp = clp,
3367 .rpc_cred = cred,
3369 struct nfs4_renewdata *data;
3371 if (!atomic_inc_not_zero(&clp->cl_count))
3372 return -EIO;
3373 data = kmalloc(sizeof(*data), GFP_KERNEL);
3374 if (data == NULL)
3375 return -ENOMEM;
3376 data->client = clp;
3377 data->timestamp = jiffies;
3378 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3379 &nfs4_renew_ops, data);
3382 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3384 struct rpc_message msg = {
3385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3386 .rpc_argp = clp,
3387 .rpc_cred = cred,
3389 unsigned long now = jiffies;
3390 int status;
3392 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3393 if (status < 0)
3394 return status;
3395 do_renew_lease(clp, now);
3396 return 0;
3399 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3401 return (server->caps & NFS_CAP_ACLS)
3402 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3403 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3406 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3407 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3408 * the stack.
3410 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3412 static void buf_to_pages(const void *buf, size_t buflen,
3413 struct page **pages, unsigned int *pgbase)
3415 const void *p = buf;
3417 *pgbase = offset_in_page(buf);
3418 p -= *pgbase;
3419 while (p < buf + buflen) {
3420 *(pages++) = virt_to_page(p);
3421 p += PAGE_CACHE_SIZE;
3425 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3426 struct page **pages, unsigned int *pgbase)
3428 struct page *newpage, **spages;
3429 int rc = 0;
3430 size_t len;
3431 spages = pages;
3433 do {
3434 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3435 newpage = alloc_page(GFP_KERNEL);
3437 if (newpage == NULL)
3438 goto unwind;
3439 memcpy(page_address(newpage), buf, len);
3440 buf += len;
3441 buflen -= len;
3442 *pages++ = newpage;
3443 rc++;
3444 } while (buflen != 0);
3446 return rc;
3448 unwind:
3449 for(; rc > 0; rc--)
3450 __free_page(spages[rc-1]);
3451 return -ENOMEM;
3454 struct nfs4_cached_acl {
3455 int cached;
3456 size_t len;
3457 char data[0];
3460 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3462 struct nfs_inode *nfsi = NFS_I(inode);
3464 spin_lock(&inode->i_lock);
3465 kfree(nfsi->nfs4_acl);
3466 nfsi->nfs4_acl = acl;
3467 spin_unlock(&inode->i_lock);
3470 static void nfs4_zap_acl_attr(struct inode *inode)
3472 nfs4_set_cached_acl(inode, NULL);
3475 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3477 struct nfs_inode *nfsi = NFS_I(inode);
3478 struct nfs4_cached_acl *acl;
3479 int ret = -ENOENT;
3481 spin_lock(&inode->i_lock);
3482 acl = nfsi->nfs4_acl;
3483 if (acl == NULL)
3484 goto out;
3485 if (buf == NULL) /* user is just asking for length */
3486 goto out_len;
3487 if (acl->cached == 0)
3488 goto out;
3489 ret = -ERANGE; /* see getxattr(2) man page */
3490 if (acl->len > buflen)
3491 goto out;
3492 memcpy(buf, acl->data, acl->len);
3493 out_len:
3494 ret = acl->len;
3495 out:
3496 spin_unlock(&inode->i_lock);
3497 return ret;
3500 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3502 struct nfs4_cached_acl *acl;
3504 if (buf && acl_len <= PAGE_SIZE) {
3505 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3506 if (acl == NULL)
3507 goto out;
3508 acl->cached = 1;
3509 memcpy(acl->data, buf, acl_len);
3510 } else {
3511 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3512 if (acl == NULL)
3513 goto out;
3514 acl->cached = 0;
3516 acl->len = acl_len;
3517 out:
3518 nfs4_set_cached_acl(inode, acl);
3521 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3523 struct page *pages[NFS4ACL_MAXPAGES];
3524 struct nfs_getaclargs args = {
3525 .fh = NFS_FH(inode),
3526 .acl_pages = pages,
3527 .acl_len = buflen,
3529 struct nfs_getaclres res = {
3530 .acl_len = buflen,
3532 void *resp_buf;
3533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3535 .rpc_argp = &args,
3536 .rpc_resp = &res,
3538 struct page *localpage = NULL;
3539 int ret;
3541 if (buflen < PAGE_SIZE) {
3542 /* As long as we're doing a round trip to the server anyway,
3543 * let's be prepared for a page of acl data. */
3544 localpage = alloc_page(GFP_KERNEL);
3545 resp_buf = page_address(localpage);
3546 if (localpage == NULL)
3547 return -ENOMEM;
3548 args.acl_pages[0] = localpage;
3549 args.acl_pgbase = 0;
3550 args.acl_len = PAGE_SIZE;
3551 } else {
3552 resp_buf = buf;
3553 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3555 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3556 if (ret)
3557 goto out_free;
3558 if (res.acl_len > args.acl_len)
3559 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3560 else
3561 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3562 if (buf) {
3563 ret = -ERANGE;
3564 if (res.acl_len > buflen)
3565 goto out_free;
3566 if (localpage)
3567 memcpy(buf, resp_buf, res.acl_len);
3569 ret = res.acl_len;
3570 out_free:
3571 if (localpage)
3572 __free_page(localpage);
3573 return ret;
3576 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3578 struct nfs4_exception exception = { };
3579 ssize_t ret;
3580 do {
3581 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3582 if (ret >= 0)
3583 break;
3584 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3585 } while (exception.retry);
3586 return ret;
3589 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3591 struct nfs_server *server = NFS_SERVER(inode);
3592 int ret;
3594 if (!nfs4_server_supports_acls(server))
3595 return -EOPNOTSUPP;
3596 ret = nfs_revalidate_inode(server, inode);
3597 if (ret < 0)
3598 return ret;
3599 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3600 nfs_zap_acl_cache(inode);
3601 ret = nfs4_read_cached_acl(inode, buf, buflen);
3602 if (ret != -ENOENT)
3603 return ret;
3604 return nfs4_get_acl_uncached(inode, buf, buflen);
3607 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3609 struct nfs_server *server = NFS_SERVER(inode);
3610 struct page *pages[NFS4ACL_MAXPAGES];
3611 struct nfs_setaclargs arg = {
3612 .fh = NFS_FH(inode),
3613 .acl_pages = pages,
3614 .acl_len = buflen,
3616 struct nfs_setaclres res;
3617 struct rpc_message msg = {
3618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3619 .rpc_argp = &arg,
3620 .rpc_resp = &res,
3622 int ret, i;
3624 if (!nfs4_server_supports_acls(server))
3625 return -EOPNOTSUPP;
3626 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3627 if (i < 0)
3628 return i;
3629 nfs_inode_return_delegation(inode);
3630 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3633 * Free each page after tx, so the only ref left is
3634 * held by the network stack
3636 for (; i > 0; i--)
3637 put_page(pages[i-1]);
3640 * Acl update can result in inode attribute update.
3641 * so mark the attribute cache invalid.
3643 spin_lock(&inode->i_lock);
3644 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3645 spin_unlock(&inode->i_lock);
3646 nfs_access_zap_cache(inode);
3647 nfs_zap_acl_cache(inode);
3648 return ret;
3651 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3653 struct nfs4_exception exception = { };
3654 int err;
3655 do {
3656 err = nfs4_handle_exception(NFS_SERVER(inode),
3657 __nfs4_proc_set_acl(inode, buf, buflen),
3658 &exception);
3659 } while (exception.retry);
3660 return err;
3663 static int
3664 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3666 struct nfs_client *clp = server->nfs_client;
3668 if (task->tk_status >= 0)
3669 return 0;
3670 switch(task->tk_status) {
3671 case -NFS4ERR_ADMIN_REVOKED:
3672 case -NFS4ERR_BAD_STATEID:
3673 case -NFS4ERR_OPENMODE:
3674 if (state == NULL)
3675 break;
3676 nfs4_schedule_stateid_recovery(server, state);
3677 goto wait_on_recovery;
3678 case -NFS4ERR_EXPIRED:
3679 if (state != NULL)
3680 nfs4_schedule_stateid_recovery(server, state);
3681 case -NFS4ERR_STALE_STATEID:
3682 case -NFS4ERR_STALE_CLIENTID:
3683 nfs4_schedule_lease_recovery(clp);
3684 goto wait_on_recovery;
3685 #if defined(CONFIG_NFS_V4_1)
3686 case -NFS4ERR_BADSESSION:
3687 case -NFS4ERR_BADSLOT:
3688 case -NFS4ERR_BAD_HIGH_SLOT:
3689 case -NFS4ERR_DEADSESSION:
3690 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3691 case -NFS4ERR_SEQ_FALSE_RETRY:
3692 case -NFS4ERR_SEQ_MISORDERED:
3693 dprintk("%s ERROR %d, Reset session\n", __func__,
3694 task->tk_status);
3695 nfs4_schedule_session_recovery(clp->cl_session);
3696 task->tk_status = 0;
3697 return -EAGAIN;
3698 #endif /* CONFIG_NFS_V4_1 */
3699 case -NFS4ERR_DELAY:
3700 nfs_inc_server_stats(server, NFSIOS_DELAY);
3701 case -NFS4ERR_GRACE:
3702 case -EKEYEXPIRED:
3703 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3704 task->tk_status = 0;
3705 return -EAGAIN;
3706 case -NFS4ERR_RETRY_UNCACHED_REP:
3707 case -NFS4ERR_OLD_STATEID:
3708 task->tk_status = 0;
3709 return -EAGAIN;
3711 task->tk_status = nfs4_map_errors(task->tk_status);
3712 return 0;
3713 wait_on_recovery:
3714 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3715 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3716 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3717 task->tk_status = 0;
3718 return -EAGAIN;
3721 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3722 unsigned short port, struct rpc_cred *cred,
3723 struct nfs4_setclientid_res *res)
3725 nfs4_verifier sc_verifier;
3726 struct nfs4_setclientid setclientid = {
3727 .sc_verifier = &sc_verifier,
3728 .sc_prog = program,
3729 .sc_cb_ident = clp->cl_cb_ident,
3731 struct rpc_message msg = {
3732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3733 .rpc_argp = &setclientid,
3734 .rpc_resp = res,
3735 .rpc_cred = cred,
3737 __be32 *p;
3738 int loop = 0;
3739 int status;
3741 p = (__be32*)sc_verifier.data;
3742 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3743 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3745 for(;;) {
3746 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3747 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3748 clp->cl_ipaddr,
3749 rpc_peeraddr2str(clp->cl_rpcclient,
3750 RPC_DISPLAY_ADDR),
3751 rpc_peeraddr2str(clp->cl_rpcclient,
3752 RPC_DISPLAY_PROTO),
3753 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3754 clp->cl_id_uniquifier);
3755 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3756 sizeof(setclientid.sc_netid),
3757 rpc_peeraddr2str(clp->cl_rpcclient,
3758 RPC_DISPLAY_NETID));
3759 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3760 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3761 clp->cl_ipaddr, port >> 8, port & 255);
3763 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3764 if (status != -NFS4ERR_CLID_INUSE)
3765 break;
3766 if (loop != 0) {
3767 ++clp->cl_id_uniquifier;
3768 break;
3770 ++loop;
3771 ssleep(clp->cl_lease_time / HZ + 1);
3773 return status;
3776 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3777 struct nfs4_setclientid_res *arg,
3778 struct rpc_cred *cred)
3780 struct nfs_fsinfo fsinfo;
3781 struct rpc_message msg = {
3782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3783 .rpc_argp = arg,
3784 .rpc_resp = &fsinfo,
3785 .rpc_cred = cred,
3787 unsigned long now;
3788 int status;
3790 now = jiffies;
3791 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3792 if (status == 0) {
3793 spin_lock(&clp->cl_lock);
3794 clp->cl_lease_time = fsinfo.lease_time * HZ;
3795 clp->cl_last_renewal = now;
3796 spin_unlock(&clp->cl_lock);
3798 return status;
3801 struct nfs4_delegreturndata {
3802 struct nfs4_delegreturnargs args;
3803 struct nfs4_delegreturnres res;
3804 struct nfs_fh fh;
3805 nfs4_stateid stateid;
3806 unsigned long timestamp;
3807 struct nfs_fattr fattr;
3808 int rpc_status;
3811 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3813 struct nfs4_delegreturndata *data = calldata;
3815 if (!nfs4_sequence_done(task, &data->res.seq_res))
3816 return;
3818 switch (task->tk_status) {
3819 case -NFS4ERR_STALE_STATEID:
3820 case -NFS4ERR_EXPIRED:
3821 case 0:
3822 renew_lease(data->res.server, data->timestamp);
3823 break;
3824 default:
3825 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3826 -EAGAIN) {
3827 nfs_restart_rpc(task, data->res.server->nfs_client);
3828 return;
3831 data->rpc_status = task->tk_status;
3834 static void nfs4_delegreturn_release(void *calldata)
3836 kfree(calldata);
3839 #if defined(CONFIG_NFS_V4_1)
3840 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3842 struct nfs4_delegreturndata *d_data;
3844 d_data = (struct nfs4_delegreturndata *)data;
3846 if (nfs4_setup_sequence(d_data->res.server,
3847 &d_data->args.seq_args,
3848 &d_data->res.seq_res, 1, task))
3849 return;
3850 rpc_call_start(task);
3852 #endif /* CONFIG_NFS_V4_1 */
3854 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3855 #if defined(CONFIG_NFS_V4_1)
3856 .rpc_call_prepare = nfs4_delegreturn_prepare,
3857 #endif /* CONFIG_NFS_V4_1 */
3858 .rpc_call_done = nfs4_delegreturn_done,
3859 .rpc_release = nfs4_delegreturn_release,
3862 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3864 struct nfs4_delegreturndata *data;
3865 struct nfs_server *server = NFS_SERVER(inode);
3866 struct rpc_task *task;
3867 struct rpc_message msg = {
3868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3869 .rpc_cred = cred,
3871 struct rpc_task_setup task_setup_data = {
3872 .rpc_client = server->client,
3873 .rpc_message = &msg,
3874 .callback_ops = &nfs4_delegreturn_ops,
3875 .flags = RPC_TASK_ASYNC,
3877 int status = 0;
3879 data = kzalloc(sizeof(*data), GFP_NOFS);
3880 if (data == NULL)
3881 return -ENOMEM;
3882 data->args.fhandle = &data->fh;
3883 data->args.stateid = &data->stateid;
3884 data->args.bitmask = server->attr_bitmask;
3885 nfs_copy_fh(&data->fh, NFS_FH(inode));
3886 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3887 data->res.fattr = &data->fattr;
3888 data->res.server = server;
3889 nfs_fattr_init(data->res.fattr);
3890 data->timestamp = jiffies;
3891 data->rpc_status = 0;
3893 task_setup_data.callback_data = data;
3894 msg.rpc_argp = &data->args;
3895 msg.rpc_resp = &data->res;
3896 task = rpc_run_task(&task_setup_data);
3897 if (IS_ERR(task))
3898 return PTR_ERR(task);
3899 if (!issync)
3900 goto out;
3901 status = nfs4_wait_for_completion_rpc_task(task);
3902 if (status != 0)
3903 goto out;
3904 status = data->rpc_status;
3905 if (status != 0)
3906 goto out;
3907 nfs_refresh_inode(inode, &data->fattr);
3908 out:
3909 rpc_put_task(task);
3910 return status;
3913 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3915 struct nfs_server *server = NFS_SERVER(inode);
3916 struct nfs4_exception exception = { };
3917 int err;
3918 do {
3919 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3920 switch (err) {
3921 case -NFS4ERR_STALE_STATEID:
3922 case -NFS4ERR_EXPIRED:
3923 case 0:
3924 return 0;
3926 err = nfs4_handle_exception(server, err, &exception);
3927 } while (exception.retry);
3928 return err;
3931 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3932 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3935 * sleep, with exponential backoff, and retry the LOCK operation.
3937 static unsigned long
3938 nfs4_set_lock_task_retry(unsigned long timeout)
3940 schedule_timeout_killable(timeout);
3941 timeout <<= 1;
3942 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3943 return NFS4_LOCK_MAXTIMEOUT;
3944 return timeout;
3947 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3949 struct inode *inode = state->inode;
3950 struct nfs_server *server = NFS_SERVER(inode);
3951 struct nfs_client *clp = server->nfs_client;
3952 struct nfs_lockt_args arg = {
3953 .fh = NFS_FH(inode),
3954 .fl = request,
3956 struct nfs_lockt_res res = {
3957 .denied = request,
3959 struct rpc_message msg = {
3960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3961 .rpc_argp = &arg,
3962 .rpc_resp = &res,
3963 .rpc_cred = state->owner->so_cred,
3965 struct nfs4_lock_state *lsp;
3966 int status;
3968 arg.lock_owner.clientid = clp->cl_clientid;
3969 status = nfs4_set_lock_state(state, request);
3970 if (status != 0)
3971 goto out;
3972 lsp = request->fl_u.nfs4_fl.owner;
3973 arg.lock_owner.id = lsp->ls_id.id;
3974 arg.lock_owner.s_dev = server->s_dev;
3975 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3976 switch (status) {
3977 case 0:
3978 request->fl_type = F_UNLCK;
3979 break;
3980 case -NFS4ERR_DENIED:
3981 status = 0;
3983 request->fl_ops->fl_release_private(request);
3984 out:
3985 return status;
3988 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3990 struct nfs4_exception exception = { };
3991 int err;
3993 do {
3994 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3995 _nfs4_proc_getlk(state, cmd, request),
3996 &exception);
3997 } while (exception.retry);
3998 return err;
4001 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4003 int res = 0;
4004 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4005 case FL_POSIX:
4006 res = posix_lock_file_wait(file, fl);
4007 break;
4008 case FL_FLOCK:
4009 res = flock_lock_file_wait(file, fl);
4010 break;
4011 default:
4012 BUG();
4014 return res;
4017 struct nfs4_unlockdata {
4018 struct nfs_locku_args arg;
4019 struct nfs_locku_res res;
4020 struct nfs4_lock_state *lsp;
4021 struct nfs_open_context *ctx;
4022 struct file_lock fl;
4023 const struct nfs_server *server;
4024 unsigned long timestamp;
4027 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4028 struct nfs_open_context *ctx,
4029 struct nfs4_lock_state *lsp,
4030 struct nfs_seqid *seqid)
4032 struct nfs4_unlockdata *p;
4033 struct inode *inode = lsp->ls_state->inode;
4035 p = kzalloc(sizeof(*p), GFP_NOFS);
4036 if (p == NULL)
4037 return NULL;
4038 p->arg.fh = NFS_FH(inode);
4039 p->arg.fl = &p->fl;
4040 p->arg.seqid = seqid;
4041 p->res.seqid = seqid;
4042 p->arg.stateid = &lsp->ls_stateid;
4043 p->lsp = lsp;
4044 atomic_inc(&lsp->ls_count);
4045 /* Ensure we don't close file until we're done freeing locks! */
4046 p->ctx = get_nfs_open_context(ctx);
4047 memcpy(&p->fl, fl, sizeof(p->fl));
4048 p->server = NFS_SERVER(inode);
4049 return p;
4052 static void nfs4_locku_release_calldata(void *data)
4054 struct nfs4_unlockdata *calldata = data;
4055 nfs_free_seqid(calldata->arg.seqid);
4056 nfs4_put_lock_state(calldata->lsp);
4057 put_nfs_open_context(calldata->ctx);
4058 kfree(calldata);
4061 static void nfs4_locku_done(struct rpc_task *task, void *data)
4063 struct nfs4_unlockdata *calldata = data;
4065 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4066 return;
4067 switch (task->tk_status) {
4068 case 0:
4069 memcpy(calldata->lsp->ls_stateid.data,
4070 calldata->res.stateid.data,
4071 sizeof(calldata->lsp->ls_stateid.data));
4072 renew_lease(calldata->server, calldata->timestamp);
4073 break;
4074 case -NFS4ERR_BAD_STATEID:
4075 case -NFS4ERR_OLD_STATEID:
4076 case -NFS4ERR_STALE_STATEID:
4077 case -NFS4ERR_EXPIRED:
4078 break;
4079 default:
4080 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4081 nfs_restart_rpc(task,
4082 calldata->server->nfs_client);
4086 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4088 struct nfs4_unlockdata *calldata = data;
4090 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4091 return;
4092 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4093 /* Note: exit _without_ running nfs4_locku_done */
4094 task->tk_action = NULL;
4095 return;
4097 calldata->timestamp = jiffies;
4098 if (nfs4_setup_sequence(calldata->server,
4099 &calldata->arg.seq_args,
4100 &calldata->res.seq_res, 1, task))
4101 return;
4102 rpc_call_start(task);
4105 static const struct rpc_call_ops nfs4_locku_ops = {
4106 .rpc_call_prepare = nfs4_locku_prepare,
4107 .rpc_call_done = nfs4_locku_done,
4108 .rpc_release = nfs4_locku_release_calldata,
4111 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4112 struct nfs_open_context *ctx,
4113 struct nfs4_lock_state *lsp,
4114 struct nfs_seqid *seqid)
4116 struct nfs4_unlockdata *data;
4117 struct rpc_message msg = {
4118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4119 .rpc_cred = ctx->cred,
4121 struct rpc_task_setup task_setup_data = {
4122 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4123 .rpc_message = &msg,
4124 .callback_ops = &nfs4_locku_ops,
4125 .workqueue = nfsiod_workqueue,
4126 .flags = RPC_TASK_ASYNC,
4129 /* Ensure this is an unlock - when canceling a lock, the
4130 * canceled lock is passed in, and it won't be an unlock.
4132 fl->fl_type = F_UNLCK;
4134 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4135 if (data == NULL) {
4136 nfs_free_seqid(seqid);
4137 return ERR_PTR(-ENOMEM);
4140 msg.rpc_argp = &data->arg;
4141 msg.rpc_resp = &data->res;
4142 task_setup_data.callback_data = data;
4143 return rpc_run_task(&task_setup_data);
4146 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4148 struct nfs_inode *nfsi = NFS_I(state->inode);
4149 struct nfs_seqid *seqid;
4150 struct nfs4_lock_state *lsp;
4151 struct rpc_task *task;
4152 int status = 0;
4153 unsigned char fl_flags = request->fl_flags;
4155 status = nfs4_set_lock_state(state, request);
4156 /* Unlock _before_ we do the RPC call */
4157 request->fl_flags |= FL_EXISTS;
4158 down_read(&nfsi->rwsem);
4159 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4160 up_read(&nfsi->rwsem);
4161 goto out;
4163 up_read(&nfsi->rwsem);
4164 if (status != 0)
4165 goto out;
4166 /* Is this a delegated lock? */
4167 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4168 goto out;
4169 lsp = request->fl_u.nfs4_fl.owner;
4170 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4171 status = -ENOMEM;
4172 if (seqid == NULL)
4173 goto out;
4174 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4175 status = PTR_ERR(task);
4176 if (IS_ERR(task))
4177 goto out;
4178 status = nfs4_wait_for_completion_rpc_task(task);
4179 rpc_put_task(task);
4180 out:
4181 request->fl_flags = fl_flags;
4182 return status;
4185 struct nfs4_lockdata {
4186 struct nfs_lock_args arg;
4187 struct nfs_lock_res res;
4188 struct nfs4_lock_state *lsp;
4189 struct nfs_open_context *ctx;
4190 struct file_lock fl;
4191 unsigned long timestamp;
4192 int rpc_status;
4193 int cancelled;
4194 struct nfs_server *server;
4197 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4198 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4199 gfp_t gfp_mask)
4201 struct nfs4_lockdata *p;
4202 struct inode *inode = lsp->ls_state->inode;
4203 struct nfs_server *server = NFS_SERVER(inode);
4205 p = kzalloc(sizeof(*p), gfp_mask);
4206 if (p == NULL)
4207 return NULL;
4209 p->arg.fh = NFS_FH(inode);
4210 p->arg.fl = &p->fl;
4211 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4212 if (p->arg.open_seqid == NULL)
4213 goto out_free;
4214 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4215 if (p->arg.lock_seqid == NULL)
4216 goto out_free_seqid;
4217 p->arg.lock_stateid = &lsp->ls_stateid;
4218 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4219 p->arg.lock_owner.id = lsp->ls_id.id;
4220 p->arg.lock_owner.s_dev = server->s_dev;
4221 p->res.lock_seqid = p->arg.lock_seqid;
4222 p->lsp = lsp;
4223 p->server = server;
4224 atomic_inc(&lsp->ls_count);
4225 p->ctx = get_nfs_open_context(ctx);
4226 memcpy(&p->fl, fl, sizeof(p->fl));
4227 return p;
4228 out_free_seqid:
4229 nfs_free_seqid(p->arg.open_seqid);
4230 out_free:
4231 kfree(p);
4232 return NULL;
4235 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4237 struct nfs4_lockdata *data = calldata;
4238 struct nfs4_state *state = data->lsp->ls_state;
4240 dprintk("%s: begin!\n", __func__);
4241 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4242 return;
4243 /* Do we need to do an open_to_lock_owner? */
4244 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4245 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4246 return;
4247 data->arg.open_stateid = &state->stateid;
4248 data->arg.new_lock_owner = 1;
4249 data->res.open_seqid = data->arg.open_seqid;
4250 } else
4251 data->arg.new_lock_owner = 0;
4252 data->timestamp = jiffies;
4253 if (nfs4_setup_sequence(data->server,
4254 &data->arg.seq_args,
4255 &data->res.seq_res, 1, task))
4256 return;
4257 rpc_call_start(task);
4258 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4261 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4263 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4264 nfs4_lock_prepare(task, calldata);
4267 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4269 struct nfs4_lockdata *data = calldata;
4271 dprintk("%s: begin!\n", __func__);
4273 if (!nfs4_sequence_done(task, &data->res.seq_res))
4274 return;
4276 data->rpc_status = task->tk_status;
4277 if (data->arg.new_lock_owner != 0) {
4278 if (data->rpc_status == 0)
4279 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4280 else
4281 goto out;
4283 if (data->rpc_status == 0) {
4284 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4285 sizeof(data->lsp->ls_stateid.data));
4286 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4287 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4289 out:
4290 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4293 static void nfs4_lock_release(void *calldata)
4295 struct nfs4_lockdata *data = calldata;
4297 dprintk("%s: begin!\n", __func__);
4298 nfs_free_seqid(data->arg.open_seqid);
4299 if (data->cancelled != 0) {
4300 struct rpc_task *task;
4301 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4302 data->arg.lock_seqid);
4303 if (!IS_ERR(task))
4304 rpc_put_task_async(task);
4305 dprintk("%s: cancelling lock!\n", __func__);
4306 } else
4307 nfs_free_seqid(data->arg.lock_seqid);
4308 nfs4_put_lock_state(data->lsp);
4309 put_nfs_open_context(data->ctx);
4310 kfree(data);
4311 dprintk("%s: done!\n", __func__);
4314 static const struct rpc_call_ops nfs4_lock_ops = {
4315 .rpc_call_prepare = nfs4_lock_prepare,
4316 .rpc_call_done = nfs4_lock_done,
4317 .rpc_release = nfs4_lock_release,
4320 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4321 .rpc_call_prepare = nfs4_recover_lock_prepare,
4322 .rpc_call_done = nfs4_lock_done,
4323 .rpc_release = nfs4_lock_release,
4326 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4328 switch (error) {
4329 case -NFS4ERR_ADMIN_REVOKED:
4330 case -NFS4ERR_BAD_STATEID:
4331 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4332 if (new_lock_owner != 0 ||
4333 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4334 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4335 break;
4336 case -NFS4ERR_STALE_STATEID:
4337 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4338 case -NFS4ERR_EXPIRED:
4339 nfs4_schedule_lease_recovery(server->nfs_client);
4343 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4345 struct nfs4_lockdata *data;
4346 struct rpc_task *task;
4347 struct rpc_message msg = {
4348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4349 .rpc_cred = state->owner->so_cred,
4351 struct rpc_task_setup task_setup_data = {
4352 .rpc_client = NFS_CLIENT(state->inode),
4353 .rpc_message = &msg,
4354 .callback_ops = &nfs4_lock_ops,
4355 .workqueue = nfsiod_workqueue,
4356 .flags = RPC_TASK_ASYNC,
4358 int ret;
4360 dprintk("%s: begin!\n", __func__);
4361 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4362 fl->fl_u.nfs4_fl.owner,
4363 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4364 if (data == NULL)
4365 return -ENOMEM;
4366 if (IS_SETLKW(cmd))
4367 data->arg.block = 1;
4368 if (recovery_type > NFS_LOCK_NEW) {
4369 if (recovery_type == NFS_LOCK_RECLAIM)
4370 data->arg.reclaim = NFS_LOCK_RECLAIM;
4371 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4373 msg.rpc_argp = &data->arg;
4374 msg.rpc_resp = &data->res;
4375 task_setup_data.callback_data = data;
4376 task = rpc_run_task(&task_setup_data);
4377 if (IS_ERR(task))
4378 return PTR_ERR(task);
4379 ret = nfs4_wait_for_completion_rpc_task(task);
4380 if (ret == 0) {
4381 ret = data->rpc_status;
4382 if (ret)
4383 nfs4_handle_setlk_error(data->server, data->lsp,
4384 data->arg.new_lock_owner, ret);
4385 } else
4386 data->cancelled = 1;
4387 rpc_put_task(task);
4388 dprintk("%s: done, ret = %d!\n", __func__, ret);
4389 return ret;
4392 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4394 struct nfs_server *server = NFS_SERVER(state->inode);
4395 struct nfs4_exception exception = { };
4396 int err;
4398 do {
4399 /* Cache the lock if possible... */
4400 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4401 return 0;
4402 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4403 if (err != -NFS4ERR_DELAY)
4404 break;
4405 nfs4_handle_exception(server, err, &exception);
4406 } while (exception.retry);
4407 return err;
4410 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4412 struct nfs_server *server = NFS_SERVER(state->inode);
4413 struct nfs4_exception exception = { };
4414 int err;
4416 err = nfs4_set_lock_state(state, request);
4417 if (err != 0)
4418 return err;
4419 do {
4420 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4421 return 0;
4422 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4423 switch (err) {
4424 default:
4425 goto out;
4426 case -NFS4ERR_GRACE:
4427 case -NFS4ERR_DELAY:
4428 nfs4_handle_exception(server, err, &exception);
4429 err = 0;
4431 } while (exception.retry);
4432 out:
4433 return err;
4436 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4438 struct nfs_inode *nfsi = NFS_I(state->inode);
4439 unsigned char fl_flags = request->fl_flags;
4440 int status = -ENOLCK;
4442 if ((fl_flags & FL_POSIX) &&
4443 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4444 goto out;
4445 /* Is this a delegated open? */
4446 status = nfs4_set_lock_state(state, request);
4447 if (status != 0)
4448 goto out;
4449 request->fl_flags |= FL_ACCESS;
4450 status = do_vfs_lock(request->fl_file, request);
4451 if (status < 0)
4452 goto out;
4453 down_read(&nfsi->rwsem);
4454 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4455 /* Yes: cache locks! */
4456 /* ...but avoid races with delegation recall... */
4457 request->fl_flags = fl_flags & ~FL_SLEEP;
4458 status = do_vfs_lock(request->fl_file, request);
4459 goto out_unlock;
4461 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4462 if (status != 0)
4463 goto out_unlock;
4464 /* Note: we always want to sleep here! */
4465 request->fl_flags = fl_flags | FL_SLEEP;
4466 if (do_vfs_lock(request->fl_file, request) < 0)
4467 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4468 out_unlock:
4469 up_read(&nfsi->rwsem);
4470 out:
4471 request->fl_flags = fl_flags;
4472 return status;
4475 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4477 struct nfs4_exception exception = { };
4478 int err;
4480 do {
4481 err = _nfs4_proc_setlk(state, cmd, request);
4482 if (err == -NFS4ERR_DENIED)
4483 err = -EAGAIN;
4484 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4485 err, &exception);
4486 } while (exception.retry);
4487 return err;
4490 static int
4491 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4493 struct nfs_open_context *ctx;
4494 struct nfs4_state *state;
4495 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4496 int status;
4498 /* verify open state */
4499 ctx = nfs_file_open_context(filp);
4500 state = ctx->state;
4502 if (request->fl_start < 0 || request->fl_end < 0)
4503 return -EINVAL;
4505 if (IS_GETLK(cmd)) {
4506 if (state != NULL)
4507 return nfs4_proc_getlk(state, F_GETLK, request);
4508 return 0;
4511 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4512 return -EINVAL;
4514 if (request->fl_type == F_UNLCK) {
4515 if (state != NULL)
4516 return nfs4_proc_unlck(state, cmd, request);
4517 return 0;
4520 if (state == NULL)
4521 return -ENOLCK;
4522 do {
4523 status = nfs4_proc_setlk(state, cmd, request);
4524 if ((status != -EAGAIN) || IS_SETLK(cmd))
4525 break;
4526 timeout = nfs4_set_lock_task_retry(timeout);
4527 status = -ERESTARTSYS;
4528 if (signalled())
4529 break;
4530 } while(status < 0);
4531 return status;
4534 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4536 struct nfs_server *server = NFS_SERVER(state->inode);
4537 struct nfs4_exception exception = { };
4538 int err;
4540 err = nfs4_set_lock_state(state, fl);
4541 if (err != 0)
4542 goto out;
4543 do {
4544 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4545 switch (err) {
4546 default:
4547 printk(KERN_ERR "%s: unhandled error %d.\n",
4548 __func__, err);
4549 case 0:
4550 case -ESTALE:
4551 goto out;
4552 case -NFS4ERR_EXPIRED:
4553 nfs4_schedule_stateid_recovery(server, state);
4554 case -NFS4ERR_STALE_CLIENTID:
4555 case -NFS4ERR_STALE_STATEID:
4556 nfs4_schedule_lease_recovery(server->nfs_client);
4557 goto out;
4558 case -NFS4ERR_BADSESSION:
4559 case -NFS4ERR_BADSLOT:
4560 case -NFS4ERR_BAD_HIGH_SLOT:
4561 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4562 case -NFS4ERR_DEADSESSION:
4563 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4564 goto out;
4565 case -ERESTARTSYS:
4567 * The show must go on: exit, but mark the
4568 * stateid as needing recovery.
4570 case -NFS4ERR_ADMIN_REVOKED:
4571 case -NFS4ERR_BAD_STATEID:
4572 case -NFS4ERR_OPENMODE:
4573 nfs4_schedule_stateid_recovery(server, state);
4574 err = 0;
4575 goto out;
4576 case -EKEYEXPIRED:
4578 * User RPCSEC_GSS context has expired.
4579 * We cannot recover this stateid now, so
4580 * skip it and allow recovery thread to
4581 * proceed.
4583 err = 0;
4584 goto out;
4585 case -ENOMEM:
4586 case -NFS4ERR_DENIED:
4587 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4588 err = 0;
4589 goto out;
4590 case -NFS4ERR_DELAY:
4591 break;
4593 err = nfs4_handle_exception(server, err, &exception);
4594 } while (exception.retry);
4595 out:
4596 return err;
4599 static void nfs4_release_lockowner_release(void *calldata)
4601 kfree(calldata);
4604 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4605 .rpc_release = nfs4_release_lockowner_release,
4608 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4610 struct nfs_server *server = lsp->ls_state->owner->so_server;
4611 struct nfs_release_lockowner_args *args;
4612 struct rpc_message msg = {
4613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4616 if (server->nfs_client->cl_mvops->minor_version != 0)
4617 return;
4618 args = kmalloc(sizeof(*args), GFP_NOFS);
4619 if (!args)
4620 return;
4621 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4622 args->lock_owner.id = lsp->ls_id.id;
4623 args->lock_owner.s_dev = server->s_dev;
4624 msg.rpc_argp = args;
4625 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4628 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4630 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4631 const void *buf, size_t buflen,
4632 int flags, int type)
4634 if (strcmp(key, "") != 0)
4635 return -EINVAL;
4637 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4640 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4641 void *buf, size_t buflen, int type)
4643 if (strcmp(key, "") != 0)
4644 return -EINVAL;
4646 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4649 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4650 size_t list_len, const char *name,
4651 size_t name_len, int type)
4653 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4655 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4656 return 0;
4658 if (list && len <= list_len)
4659 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4660 return len;
4664 * nfs_fhget will use either the mounted_on_fileid or the fileid
4666 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4668 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4669 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4670 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4671 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4672 return;
4674 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4675 NFS_ATTR_FATTR_NLINK;
4676 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4677 fattr->nlink = 2;
4680 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4681 struct nfs4_fs_locations *fs_locations, struct page *page)
4683 struct nfs_server *server = NFS_SERVER(dir);
4684 u32 bitmask[2] = {
4685 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4687 struct nfs4_fs_locations_arg args = {
4688 .dir_fh = NFS_FH(dir),
4689 .name = name,
4690 .page = page,
4691 .bitmask = bitmask,
4693 struct nfs4_fs_locations_res res = {
4694 .fs_locations = fs_locations,
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4698 .rpc_argp = &args,
4699 .rpc_resp = &res,
4701 int status;
4703 dprintk("%s: start\n", __func__);
4705 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4706 * is not supported */
4707 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4708 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4709 else
4710 bitmask[0] |= FATTR4_WORD0_FILEID;
4712 nfs_fattr_init(&fs_locations->fattr);
4713 fs_locations->server = server;
4714 fs_locations->nlocations = 0;
4715 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4716 dprintk("%s: returned status = %d\n", __func__, status);
4717 return status;
4720 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4722 int status;
4723 struct nfs4_secinfo_arg args = {
4724 .dir_fh = NFS_FH(dir),
4725 .name = name,
4727 struct nfs4_secinfo_res res = {
4728 .flavors = flavors,
4730 struct rpc_message msg = {
4731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4732 .rpc_argp = &args,
4733 .rpc_resp = &res,
4736 dprintk("NFS call secinfo %s\n", name->name);
4737 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4738 dprintk("NFS reply secinfo: %d\n", status);
4739 return status;
4742 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4744 struct nfs4_exception exception = { };
4745 int err;
4746 do {
4747 err = nfs4_handle_exception(NFS_SERVER(dir),
4748 _nfs4_proc_secinfo(dir, name, flavors),
4749 &exception);
4750 } while (exception.retry);
4751 return err;
4754 #ifdef CONFIG_NFS_V4_1
4756 * Check the exchange flags returned by the server for invalid flags, having
4757 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4758 * DS flags set.
4760 static int nfs4_check_cl_exchange_flags(u32 flags)
4762 if (flags & ~EXCHGID4_FLAG_MASK_R)
4763 goto out_inval;
4764 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4765 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4766 goto out_inval;
4767 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4768 goto out_inval;
4769 return NFS_OK;
4770 out_inval:
4771 return -NFS4ERR_INVAL;
4775 * nfs4_proc_exchange_id()
4777 * Since the clientid has expired, all compounds using sessions
4778 * associated with the stale clientid will be returning
4779 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4780 * be in some phase of session reset.
4782 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4784 nfs4_verifier verifier;
4785 struct nfs41_exchange_id_args args = {
4786 .client = clp,
4787 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4789 struct nfs41_exchange_id_res res = {
4790 .client = clp,
4792 int status;
4793 struct rpc_message msg = {
4794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4795 .rpc_argp = &args,
4796 .rpc_resp = &res,
4797 .rpc_cred = cred,
4799 __be32 *p;
4801 dprintk("--> %s\n", __func__);
4802 BUG_ON(clp == NULL);
4804 p = (u32 *)verifier.data;
4805 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4806 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4807 args.verifier = &verifier;
4809 args.id_len = scnprintf(args.id, sizeof(args.id),
4810 "%s/%s.%s/%u",
4811 clp->cl_ipaddr,
4812 init_utsname()->nodename,
4813 init_utsname()->domainname,
4814 clp->cl_rpcclient->cl_auth->au_flavor);
4816 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4817 if (!status)
4818 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4819 dprintk("<-- %s status= %d\n", __func__, status);
4820 return status;
4823 struct nfs4_get_lease_time_data {
4824 struct nfs4_get_lease_time_args *args;
4825 struct nfs4_get_lease_time_res *res;
4826 struct nfs_client *clp;
4829 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4830 void *calldata)
4832 int ret;
4833 struct nfs4_get_lease_time_data *data =
4834 (struct nfs4_get_lease_time_data *)calldata;
4836 dprintk("--> %s\n", __func__);
4837 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4838 /* just setup sequence, do not trigger session recovery
4839 since we're invoked within one */
4840 ret = nfs41_setup_sequence(data->clp->cl_session,
4841 &data->args->la_seq_args,
4842 &data->res->lr_seq_res, 0, task);
4844 BUG_ON(ret == -EAGAIN);
4845 rpc_call_start(task);
4846 dprintk("<-- %s\n", __func__);
4850 * Called from nfs4_state_manager thread for session setup, so don't recover
4851 * from sequence operation or clientid errors.
4853 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4855 struct nfs4_get_lease_time_data *data =
4856 (struct nfs4_get_lease_time_data *)calldata;
4858 dprintk("--> %s\n", __func__);
4859 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4860 return;
4861 switch (task->tk_status) {
4862 case -NFS4ERR_DELAY:
4863 case -NFS4ERR_GRACE:
4864 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4865 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4866 task->tk_status = 0;
4867 /* fall through */
4868 case -NFS4ERR_RETRY_UNCACHED_REP:
4869 nfs_restart_rpc(task, data->clp);
4870 return;
4872 dprintk("<-- %s\n", __func__);
4875 struct rpc_call_ops nfs4_get_lease_time_ops = {
4876 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4877 .rpc_call_done = nfs4_get_lease_time_done,
4880 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4882 struct rpc_task *task;
4883 struct nfs4_get_lease_time_args args;
4884 struct nfs4_get_lease_time_res res = {
4885 .lr_fsinfo = fsinfo,
4887 struct nfs4_get_lease_time_data data = {
4888 .args = &args,
4889 .res = &res,
4890 .clp = clp,
4892 struct rpc_message msg = {
4893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4894 .rpc_argp = &args,
4895 .rpc_resp = &res,
4897 struct rpc_task_setup task_setup = {
4898 .rpc_client = clp->cl_rpcclient,
4899 .rpc_message = &msg,
4900 .callback_ops = &nfs4_get_lease_time_ops,
4901 .callback_data = &data,
4902 .flags = RPC_TASK_TIMEOUT,
4904 int status;
4906 dprintk("--> %s\n", __func__);
4907 task = rpc_run_task(&task_setup);
4909 if (IS_ERR(task))
4910 status = PTR_ERR(task);
4911 else {
4912 status = task->tk_status;
4913 rpc_put_task(task);
4915 dprintk("<-- %s return %d\n", __func__, status);
4917 return status;
4921 * Reset a slot table
4923 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4924 int ivalue)
4926 struct nfs4_slot *new = NULL;
4927 int i;
4928 int ret = 0;
4930 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4931 max_reqs, tbl->max_slots);
4933 /* Does the newly negotiated max_reqs match the existing slot table? */
4934 if (max_reqs != tbl->max_slots) {
4935 ret = -ENOMEM;
4936 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4937 GFP_NOFS);
4938 if (!new)
4939 goto out;
4940 ret = 0;
4941 kfree(tbl->slots);
4943 spin_lock(&tbl->slot_tbl_lock);
4944 if (new) {
4945 tbl->slots = new;
4946 tbl->max_slots = max_reqs;
4948 for (i = 0; i < tbl->max_slots; ++i)
4949 tbl->slots[i].seq_nr = ivalue;
4950 spin_unlock(&tbl->slot_tbl_lock);
4951 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4952 tbl, tbl->slots, tbl->max_slots);
4953 out:
4954 dprintk("<-- %s: return %d\n", __func__, ret);
4955 return ret;
4959 * Reset the forechannel and backchannel slot tables
4961 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4963 int status;
4965 status = nfs4_reset_slot_table(&session->fc_slot_table,
4966 session->fc_attrs.max_reqs, 1);
4967 if (status)
4968 return status;
4970 status = nfs4_reset_slot_table(&session->bc_slot_table,
4971 session->bc_attrs.max_reqs, 0);
4972 return status;
4975 /* Destroy the slot table */
4976 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4978 if (session->fc_slot_table.slots != NULL) {
4979 kfree(session->fc_slot_table.slots);
4980 session->fc_slot_table.slots = NULL;
4982 if (session->bc_slot_table.slots != NULL) {
4983 kfree(session->bc_slot_table.slots);
4984 session->bc_slot_table.slots = NULL;
4986 return;
4990 * Initialize slot table
4992 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4993 int max_slots, int ivalue)
4995 struct nfs4_slot *slot;
4996 int ret = -ENOMEM;
4998 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5000 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
5002 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
5003 if (!slot)
5004 goto out;
5005 ret = 0;
5007 spin_lock(&tbl->slot_tbl_lock);
5008 tbl->max_slots = max_slots;
5009 tbl->slots = slot;
5010 tbl->highest_used_slotid = -1; /* no slot is currently used */
5011 spin_unlock(&tbl->slot_tbl_lock);
5012 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5013 tbl, tbl->slots, tbl->max_slots);
5014 out:
5015 dprintk("<-- %s: return %d\n", __func__, ret);
5016 return ret;
5020 * Initialize the forechannel and backchannel tables
5022 static int nfs4_init_slot_tables(struct nfs4_session *session)
5024 struct nfs4_slot_table *tbl;
5025 int status = 0;
5027 tbl = &session->fc_slot_table;
5028 if (tbl->slots == NULL) {
5029 status = nfs4_init_slot_table(tbl,
5030 session->fc_attrs.max_reqs, 1);
5031 if (status)
5032 return status;
5035 tbl = &session->bc_slot_table;
5036 if (tbl->slots == NULL) {
5037 status = nfs4_init_slot_table(tbl,
5038 session->bc_attrs.max_reqs, 0);
5039 if (status)
5040 nfs4_destroy_slot_tables(session);
5043 return status;
5046 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5048 struct nfs4_session *session;
5049 struct nfs4_slot_table *tbl;
5051 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5052 if (!session)
5053 return NULL;
5055 tbl = &session->fc_slot_table;
5056 tbl->highest_used_slotid = -1;
5057 spin_lock_init(&tbl->slot_tbl_lock);
5058 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5059 init_completion(&tbl->complete);
5061 tbl = &session->bc_slot_table;
5062 tbl->highest_used_slotid = -1;
5063 spin_lock_init(&tbl->slot_tbl_lock);
5064 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5065 init_completion(&tbl->complete);
5067 session->session_state = 1<<NFS4_SESSION_INITING;
5069 session->clp = clp;
5070 return session;
5073 void nfs4_destroy_session(struct nfs4_session *session)
5075 nfs4_proc_destroy_session(session);
5076 dprintk("%s Destroy backchannel for xprt %p\n",
5077 __func__, session->clp->cl_rpcclient->cl_xprt);
5078 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5079 NFS41_BC_MIN_CALLBACKS);
5080 nfs4_destroy_slot_tables(session);
5081 kfree(session);
5085 * Initialize the values to be used by the client in CREATE_SESSION
5086 * If nfs4_init_session set the fore channel request and response sizes,
5087 * use them.
5089 * Set the back channel max_resp_sz_cached to zero to force the client to
5090 * always set csa_cachethis to FALSE because the current implementation
5091 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5093 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5095 struct nfs4_session *session = args->client->cl_session;
5096 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5097 mxresp_sz = session->fc_attrs.max_resp_sz;
5099 if (mxrqst_sz == 0)
5100 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5101 if (mxresp_sz == 0)
5102 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5103 /* Fore channel attributes */
5104 args->fc_attrs.headerpadsz = 0;
5105 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5106 args->fc_attrs.max_resp_sz = mxresp_sz;
5107 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5108 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5110 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5111 "max_ops=%u max_reqs=%u\n",
5112 __func__,
5113 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5114 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5116 /* Back channel attributes */
5117 args->bc_attrs.headerpadsz = 0;
5118 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5119 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5120 args->bc_attrs.max_resp_sz_cached = 0;
5121 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5122 args->bc_attrs.max_reqs = 1;
5124 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5125 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5126 __func__,
5127 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5128 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5129 args->bc_attrs.max_reqs);
5132 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5134 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5135 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5137 if (rcvd->headerpadsz > sent->headerpadsz)
5138 return -EINVAL;
5139 if (rcvd->max_resp_sz > sent->max_resp_sz)
5140 return -EINVAL;
5142 * Our requested max_ops is the minimum we need; we're not
5143 * prepared to break up compounds into smaller pieces than that.
5144 * So, no point even trying to continue if the server won't
5145 * cooperate:
5147 if (rcvd->max_ops < sent->max_ops)
5148 return -EINVAL;
5149 if (rcvd->max_reqs == 0)
5150 return -EINVAL;
5151 return 0;
5154 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5156 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5157 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5159 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5160 return -EINVAL;
5161 if (rcvd->max_resp_sz < sent->max_resp_sz)
5162 return -EINVAL;
5163 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5164 return -EINVAL;
5165 /* These would render the backchannel useless: */
5166 if (rcvd->max_ops == 0)
5167 return -EINVAL;
5168 if (rcvd->max_reqs == 0)
5169 return -EINVAL;
5170 return 0;
5173 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5174 struct nfs4_session *session)
5176 int ret;
5178 ret = nfs4_verify_fore_channel_attrs(args, session);
5179 if (ret)
5180 return ret;
5181 return nfs4_verify_back_channel_attrs(args, session);
5184 static int _nfs4_proc_create_session(struct nfs_client *clp)
5186 struct nfs4_session *session = clp->cl_session;
5187 struct nfs41_create_session_args args = {
5188 .client = clp,
5189 .cb_program = NFS4_CALLBACK,
5191 struct nfs41_create_session_res res = {
5192 .client = clp,
5194 struct rpc_message msg = {
5195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5196 .rpc_argp = &args,
5197 .rpc_resp = &res,
5199 int status;
5201 nfs4_init_channel_attrs(&args);
5202 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5204 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5206 if (!status)
5207 /* Verify the session's negotiated channel_attrs values */
5208 status = nfs4_verify_channel_attrs(&args, session);
5209 if (!status) {
5210 /* Increment the clientid slot sequence id */
5211 clp->cl_seqid++;
5214 return status;
5218 * Issues a CREATE_SESSION operation to the server.
5219 * It is the responsibility of the caller to verify the session is
5220 * expired before calling this routine.
5222 int nfs4_proc_create_session(struct nfs_client *clp)
5224 int status;
5225 unsigned *ptr;
5226 struct nfs4_session *session = clp->cl_session;
5228 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5230 status = _nfs4_proc_create_session(clp);
5231 if (status)
5232 goto out;
5234 /* Init and reset the fore channel */
5235 status = nfs4_init_slot_tables(session);
5236 dprintk("slot table initialization returned %d\n", status);
5237 if (status)
5238 goto out;
5239 status = nfs4_reset_slot_tables(session);
5240 dprintk("slot table reset returned %d\n", status);
5241 if (status)
5242 goto out;
5244 ptr = (unsigned *)&session->sess_id.data[0];
5245 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5246 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5247 out:
5248 dprintk("<-- %s\n", __func__);
5249 return status;
5253 * Issue the over-the-wire RPC DESTROY_SESSION.
5254 * The caller must serialize access to this routine.
5256 int nfs4_proc_destroy_session(struct nfs4_session *session)
5258 int status = 0;
5259 struct rpc_message msg;
5261 dprintk("--> nfs4_proc_destroy_session\n");
5263 /* session is still being setup */
5264 if (session->clp->cl_cons_state != NFS_CS_READY)
5265 return status;
5267 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5268 msg.rpc_argp = session;
5269 msg.rpc_resp = NULL;
5270 msg.rpc_cred = NULL;
5271 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5273 if (status)
5274 printk(KERN_WARNING
5275 "Got error %d from the server on DESTROY_SESSION. "
5276 "Session has been destroyed regardless...\n", status);
5278 dprintk("<-- nfs4_proc_destroy_session\n");
5279 return status;
5282 int nfs4_init_session(struct nfs_server *server)
5284 struct nfs_client *clp = server->nfs_client;
5285 struct nfs4_session *session;
5286 unsigned int rsize, wsize;
5287 int ret;
5289 if (!nfs4_has_session(clp))
5290 return 0;
5292 session = clp->cl_session;
5293 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5294 return 0;
5296 rsize = server->rsize;
5297 if (rsize == 0)
5298 rsize = NFS_MAX_FILE_IO_SIZE;
5299 wsize = server->wsize;
5300 if (wsize == 0)
5301 wsize = NFS_MAX_FILE_IO_SIZE;
5303 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5304 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5306 ret = nfs4_recover_expired_lease(server);
5307 if (!ret)
5308 ret = nfs4_check_client_ready(clp);
5309 return ret;
5312 int nfs4_init_ds_session(struct nfs_client *clp)
5314 struct nfs4_session *session = clp->cl_session;
5315 int ret;
5317 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5318 return 0;
5320 ret = nfs4_client_recover_expired_lease(clp);
5321 if (!ret)
5322 /* Test for the DS role */
5323 if (!is_ds_client(clp))
5324 ret = -ENODEV;
5325 if (!ret)
5326 ret = nfs4_check_client_ready(clp);
5327 return ret;
5330 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5334 * Renew the cl_session lease.
5336 struct nfs4_sequence_data {
5337 struct nfs_client *clp;
5338 struct nfs4_sequence_args args;
5339 struct nfs4_sequence_res res;
5342 static void nfs41_sequence_release(void *data)
5344 struct nfs4_sequence_data *calldata = data;
5345 struct nfs_client *clp = calldata->clp;
5347 if (atomic_read(&clp->cl_count) > 1)
5348 nfs4_schedule_state_renewal(clp);
5349 nfs_put_client(clp);
5350 kfree(calldata);
5353 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5355 switch(task->tk_status) {
5356 case -NFS4ERR_DELAY:
5357 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5358 return -EAGAIN;
5359 default:
5360 nfs4_schedule_lease_recovery(clp);
5362 return 0;
5365 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5367 struct nfs4_sequence_data *calldata = data;
5368 struct nfs_client *clp = calldata->clp;
5370 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5371 return;
5373 if (task->tk_status < 0) {
5374 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5375 if (atomic_read(&clp->cl_count) == 1)
5376 goto out;
5378 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5379 rpc_restart_call_prepare(task);
5380 return;
5383 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5384 out:
5385 dprintk("<-- %s\n", __func__);
5388 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5390 struct nfs4_sequence_data *calldata = data;
5391 struct nfs_client *clp = calldata->clp;
5392 struct nfs4_sequence_args *args;
5393 struct nfs4_sequence_res *res;
5395 args = task->tk_msg.rpc_argp;
5396 res = task->tk_msg.rpc_resp;
5398 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5399 return;
5400 rpc_call_start(task);
5403 static const struct rpc_call_ops nfs41_sequence_ops = {
5404 .rpc_call_done = nfs41_sequence_call_done,
5405 .rpc_call_prepare = nfs41_sequence_prepare,
5406 .rpc_release = nfs41_sequence_release,
5409 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5411 struct nfs4_sequence_data *calldata;
5412 struct rpc_message msg = {
5413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5414 .rpc_cred = cred,
5416 struct rpc_task_setup task_setup_data = {
5417 .rpc_client = clp->cl_rpcclient,
5418 .rpc_message = &msg,
5419 .callback_ops = &nfs41_sequence_ops,
5420 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5423 if (!atomic_inc_not_zero(&clp->cl_count))
5424 return ERR_PTR(-EIO);
5425 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5426 if (calldata == NULL) {
5427 nfs_put_client(clp);
5428 return ERR_PTR(-ENOMEM);
5430 msg.rpc_argp = &calldata->args;
5431 msg.rpc_resp = &calldata->res;
5432 calldata->clp = clp;
5433 task_setup_data.callback_data = calldata;
5435 return rpc_run_task(&task_setup_data);
5438 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5440 struct rpc_task *task;
5441 int ret = 0;
5443 task = _nfs41_proc_sequence(clp, cred);
5444 if (IS_ERR(task))
5445 ret = PTR_ERR(task);
5446 else
5447 rpc_put_task_async(task);
5448 dprintk("<-- %s status=%d\n", __func__, ret);
5449 return ret;
5452 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5454 struct rpc_task *task;
5455 int ret;
5457 task = _nfs41_proc_sequence(clp, cred);
5458 if (IS_ERR(task)) {
5459 ret = PTR_ERR(task);
5460 goto out;
5462 ret = rpc_wait_for_completion_task(task);
5463 if (!ret) {
5464 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5466 if (task->tk_status == 0)
5467 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5468 ret = task->tk_status;
5470 rpc_put_task(task);
5471 out:
5472 dprintk("<-- %s status=%d\n", __func__, ret);
5473 return ret;
5476 struct nfs4_reclaim_complete_data {
5477 struct nfs_client *clp;
5478 struct nfs41_reclaim_complete_args arg;
5479 struct nfs41_reclaim_complete_res res;
5482 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5484 struct nfs4_reclaim_complete_data *calldata = data;
5486 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5487 if (nfs41_setup_sequence(calldata->clp->cl_session,
5488 &calldata->arg.seq_args,
5489 &calldata->res.seq_res, 0, task))
5490 return;
5492 rpc_call_start(task);
5495 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5497 switch(task->tk_status) {
5498 case 0:
5499 case -NFS4ERR_COMPLETE_ALREADY:
5500 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5501 break;
5502 case -NFS4ERR_DELAY:
5503 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5504 /* fall through */
5505 case -NFS4ERR_RETRY_UNCACHED_REP:
5506 return -EAGAIN;
5507 default:
5508 nfs4_schedule_lease_recovery(clp);
5510 return 0;
5513 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5515 struct nfs4_reclaim_complete_data *calldata = data;
5516 struct nfs_client *clp = calldata->clp;
5517 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5519 dprintk("--> %s\n", __func__);
5520 if (!nfs41_sequence_done(task, res))
5521 return;
5523 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5524 rpc_restart_call_prepare(task);
5525 return;
5527 dprintk("<-- %s\n", __func__);
5530 static void nfs4_free_reclaim_complete_data(void *data)
5532 struct nfs4_reclaim_complete_data *calldata = data;
5534 kfree(calldata);
5537 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5538 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5539 .rpc_call_done = nfs4_reclaim_complete_done,
5540 .rpc_release = nfs4_free_reclaim_complete_data,
5544 * Issue a global reclaim complete.
5546 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5548 struct nfs4_reclaim_complete_data *calldata;
5549 struct rpc_task *task;
5550 struct rpc_message msg = {
5551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5553 struct rpc_task_setup task_setup_data = {
5554 .rpc_client = clp->cl_rpcclient,
5555 .rpc_message = &msg,
5556 .callback_ops = &nfs4_reclaim_complete_call_ops,
5557 .flags = RPC_TASK_ASYNC,
5559 int status = -ENOMEM;
5561 dprintk("--> %s\n", __func__);
5562 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5563 if (calldata == NULL)
5564 goto out;
5565 calldata->clp = clp;
5566 calldata->arg.one_fs = 0;
5568 msg.rpc_argp = &calldata->arg;
5569 msg.rpc_resp = &calldata->res;
5570 task_setup_data.callback_data = calldata;
5571 task = rpc_run_task(&task_setup_data);
5572 if (IS_ERR(task)) {
5573 status = PTR_ERR(task);
5574 goto out;
5576 status = nfs4_wait_for_completion_rpc_task(task);
5577 if (status == 0)
5578 status = task->tk_status;
5579 rpc_put_task(task);
5580 return 0;
5581 out:
5582 dprintk("<-- %s status=%d\n", __func__, status);
5583 return status;
5586 static void
5587 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5589 struct nfs4_layoutget *lgp = calldata;
5590 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5592 dprintk("--> %s\n", __func__);
5593 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5594 * right now covering the LAYOUTGET we are about to send.
5595 * However, that is not so catastrophic, and there seems
5596 * to be no way to prevent it completely.
5598 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5599 &lgp->res.seq_res, 0, task))
5600 return;
5601 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5602 NFS_I(lgp->args.inode)->layout,
5603 lgp->args.ctx->state)) {
5604 rpc_exit(task, NFS4_OK);
5605 return;
5607 rpc_call_start(task);
5610 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5612 struct nfs4_layoutget *lgp = calldata;
5613 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5615 dprintk("--> %s\n", __func__);
5617 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5618 return;
5620 switch (task->tk_status) {
5621 case 0:
5622 break;
5623 case -NFS4ERR_LAYOUTTRYLATER:
5624 case -NFS4ERR_RECALLCONFLICT:
5625 task->tk_status = -NFS4ERR_DELAY;
5626 /* Fall through */
5627 default:
5628 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5629 rpc_restart_call_prepare(task);
5630 return;
5633 dprintk("<-- %s\n", __func__);
5636 static void nfs4_layoutget_release(void *calldata)
5638 struct nfs4_layoutget *lgp = calldata;
5640 dprintk("--> %s\n", __func__);
5641 put_nfs_open_context(lgp->args.ctx);
5642 kfree(calldata);
5643 dprintk("<-- %s\n", __func__);
5646 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5647 .rpc_call_prepare = nfs4_layoutget_prepare,
5648 .rpc_call_done = nfs4_layoutget_done,
5649 .rpc_release = nfs4_layoutget_release,
5652 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5654 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5655 struct rpc_task *task;
5656 struct rpc_message msg = {
5657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5658 .rpc_argp = &lgp->args,
5659 .rpc_resp = &lgp->res,
5661 struct rpc_task_setup task_setup_data = {
5662 .rpc_client = server->client,
5663 .rpc_message = &msg,
5664 .callback_ops = &nfs4_layoutget_call_ops,
5665 .callback_data = lgp,
5666 .flags = RPC_TASK_ASYNC,
5668 int status = 0;
5670 dprintk("--> %s\n", __func__);
5672 lgp->res.layoutp = &lgp->args.layout;
5673 lgp->res.seq_res.sr_slot = NULL;
5674 task = rpc_run_task(&task_setup_data);
5675 if (IS_ERR(task))
5676 return PTR_ERR(task);
5677 status = nfs4_wait_for_completion_rpc_task(task);
5678 if (status == 0)
5679 status = task->tk_status;
5680 if (status == 0)
5681 status = pnfs_layout_process(lgp);
5682 rpc_put_task(task);
5683 dprintk("<-- %s status=%d\n", __func__, status);
5684 return status;
5687 static int
5688 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5690 struct nfs4_getdeviceinfo_args args = {
5691 .pdev = pdev,
5693 struct nfs4_getdeviceinfo_res res = {
5694 .pdev = pdev,
5696 struct rpc_message msg = {
5697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5698 .rpc_argp = &args,
5699 .rpc_resp = &res,
5701 int status;
5703 dprintk("--> %s\n", __func__);
5704 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5705 dprintk("<-- %s status=%d\n", __func__, status);
5707 return status;
5710 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5712 struct nfs4_exception exception = { };
5713 int err;
5715 do {
5716 err = nfs4_handle_exception(server,
5717 _nfs4_proc_getdeviceinfo(server, pdev),
5718 &exception);
5719 } while (exception.retry);
5720 return err;
5722 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5724 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5726 struct nfs4_layoutcommit_data *data = calldata;
5727 struct nfs_server *server = NFS_SERVER(data->args.inode);
5729 if (nfs4_setup_sequence(server, &data->args.seq_args,
5730 &data->res.seq_res, 1, task))
5731 return;
5732 rpc_call_start(task);
5735 static void
5736 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5738 struct nfs4_layoutcommit_data *data = calldata;
5739 struct nfs_server *server = NFS_SERVER(data->args.inode);
5741 if (!nfs4_sequence_done(task, &data->res.seq_res))
5742 return;
5744 switch (task->tk_status) { /* Just ignore these failures */
5745 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5746 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5747 case NFS4ERR_BADLAYOUT: /* no layout */
5748 case NFS4ERR_GRACE: /* loca_recalim always false */
5749 task->tk_status = 0;
5752 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5753 nfs_restart_rpc(task, server->nfs_client);
5754 return;
5757 if (task->tk_status == 0)
5758 nfs_post_op_update_inode_force_wcc(data->args.inode,
5759 data->res.fattr);
5762 static void nfs4_layoutcommit_release(void *calldata)
5764 struct nfs4_layoutcommit_data *data = calldata;
5766 /* Matched by references in pnfs_set_layoutcommit */
5767 put_lseg(data->lseg);
5768 put_rpccred(data->cred);
5769 kfree(data);
5772 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5773 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5774 .rpc_call_done = nfs4_layoutcommit_done,
5775 .rpc_release = nfs4_layoutcommit_release,
5779 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
5781 struct rpc_message msg = {
5782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5783 .rpc_argp = &data->args,
5784 .rpc_resp = &data->res,
5785 .rpc_cred = data->cred,
5787 struct rpc_task_setup task_setup_data = {
5788 .task = &data->task,
5789 .rpc_client = NFS_CLIENT(data->args.inode),
5790 .rpc_message = &msg,
5791 .callback_ops = &nfs4_layoutcommit_ops,
5792 .callback_data = data,
5793 .flags = RPC_TASK_ASYNC,
5795 struct rpc_task *task;
5796 int status = 0;
5798 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5799 "lbw: %llu inode %lu\n",
5800 data->task.tk_pid, sync,
5801 data->args.lastbytewritten,
5802 data->args.inode->i_ino);
5804 task = rpc_run_task(&task_setup_data);
5805 if (IS_ERR(task))
5806 return PTR_ERR(task);
5807 if (sync == false)
5808 goto out;
5809 status = nfs4_wait_for_completion_rpc_task(task);
5810 if (status != 0)
5811 goto out;
5812 status = task->tk_status;
5813 out:
5814 dprintk("%s: status %d\n", __func__, status);
5815 rpc_put_task(task);
5816 return status;
5818 #endif /* CONFIG_NFS_V4_1 */
5820 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5821 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5822 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5823 .recover_open = nfs4_open_reclaim,
5824 .recover_lock = nfs4_lock_reclaim,
5825 .establish_clid = nfs4_init_clientid,
5826 .get_clid_cred = nfs4_get_setclientid_cred,
5829 #if defined(CONFIG_NFS_V4_1)
5830 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5831 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5832 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5833 .recover_open = nfs4_open_reclaim,
5834 .recover_lock = nfs4_lock_reclaim,
5835 .establish_clid = nfs41_init_clientid,
5836 .get_clid_cred = nfs4_get_exchange_id_cred,
5837 .reclaim_complete = nfs41_proc_reclaim_complete,
5839 #endif /* CONFIG_NFS_V4_1 */
5841 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5842 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5843 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5844 .recover_open = nfs4_open_expired,
5845 .recover_lock = nfs4_lock_expired,
5846 .establish_clid = nfs4_init_clientid,
5847 .get_clid_cred = nfs4_get_setclientid_cred,
5850 #if defined(CONFIG_NFS_V4_1)
5851 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5852 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5853 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5854 .recover_open = nfs4_open_expired,
5855 .recover_lock = nfs4_lock_expired,
5856 .establish_clid = nfs41_init_clientid,
5857 .get_clid_cred = nfs4_get_exchange_id_cred,
5859 #endif /* CONFIG_NFS_V4_1 */
5861 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5862 .sched_state_renewal = nfs4_proc_async_renew,
5863 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5864 .renew_lease = nfs4_proc_renew,
5867 #if defined(CONFIG_NFS_V4_1)
5868 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5869 .sched_state_renewal = nfs41_proc_async_sequence,
5870 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5871 .renew_lease = nfs4_proc_sequence,
5873 #endif
5875 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5876 .minor_version = 0,
5877 .call_sync = _nfs4_call_sync,
5878 .validate_stateid = nfs4_validate_delegation_stateid,
5879 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5880 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5881 .state_renewal_ops = &nfs40_state_renewal_ops,
5884 #if defined(CONFIG_NFS_V4_1)
5885 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5886 .minor_version = 1,
5887 .call_sync = _nfs4_call_sync_session,
5888 .validate_stateid = nfs41_validate_delegation_stateid,
5889 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5890 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5891 .state_renewal_ops = &nfs41_state_renewal_ops,
5893 #endif
5895 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5896 [0] = &nfs_v4_0_minor_ops,
5897 #if defined(CONFIG_NFS_V4_1)
5898 [1] = &nfs_v4_1_minor_ops,
5899 #endif
5902 static const struct inode_operations nfs4_file_inode_operations = {
5903 .permission = nfs_permission,
5904 .getattr = nfs_getattr,
5905 .setattr = nfs_setattr,
5906 .getxattr = generic_getxattr,
5907 .setxattr = generic_setxattr,
5908 .listxattr = generic_listxattr,
5909 .removexattr = generic_removexattr,
5912 const struct nfs_rpc_ops nfs_v4_clientops = {
5913 .version = 4, /* protocol version */
5914 .dentry_ops = &nfs4_dentry_operations,
5915 .dir_inode_ops = &nfs4_dir_inode_operations,
5916 .file_inode_ops = &nfs4_file_inode_operations,
5917 .getroot = nfs4_proc_get_root,
5918 .getattr = nfs4_proc_getattr,
5919 .setattr = nfs4_proc_setattr,
5920 .lookupfh = nfs4_proc_lookupfh,
5921 .lookup = nfs4_proc_lookup,
5922 .access = nfs4_proc_access,
5923 .readlink = nfs4_proc_readlink,
5924 .create = nfs4_proc_create,
5925 .remove = nfs4_proc_remove,
5926 .unlink_setup = nfs4_proc_unlink_setup,
5927 .unlink_done = nfs4_proc_unlink_done,
5928 .rename = nfs4_proc_rename,
5929 .rename_setup = nfs4_proc_rename_setup,
5930 .rename_done = nfs4_proc_rename_done,
5931 .link = nfs4_proc_link,
5932 .symlink = nfs4_proc_symlink,
5933 .mkdir = nfs4_proc_mkdir,
5934 .rmdir = nfs4_proc_remove,
5935 .readdir = nfs4_proc_readdir,
5936 .mknod = nfs4_proc_mknod,
5937 .statfs = nfs4_proc_statfs,
5938 .fsinfo = nfs4_proc_fsinfo,
5939 .pathconf = nfs4_proc_pathconf,
5940 .set_capabilities = nfs4_server_capabilities,
5941 .decode_dirent = nfs4_decode_dirent,
5942 .read_setup = nfs4_proc_read_setup,
5943 .read_done = nfs4_read_done,
5944 .write_setup = nfs4_proc_write_setup,
5945 .write_done = nfs4_write_done,
5946 .commit_setup = nfs4_proc_commit_setup,
5947 .commit_done = nfs4_commit_done,
5948 .lock = nfs4_proc_lock,
5949 .clear_acl_cache = nfs4_zap_acl_attr,
5950 .close_context = nfs4_close_context,
5951 .open_context = nfs4_atomic_open,
5952 .init_client = nfs4_init_client,
5953 .secinfo = nfs4_proc_secinfo,
5956 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5957 .prefix = XATTR_NAME_NFSV4_ACL,
5958 .list = nfs4_xattr_list_nfs4_acl,
5959 .get = nfs4_xattr_get_nfs4_acl,
5960 .set = nfs4_xattr_set_nfs4_acl,
5963 const struct xattr_handler *nfs4_xattr_handlers[] = {
5964 &nfs4_xattr_nfs4_acl_handler,
5965 NULL
5969 * Local variables:
5970 * c-basic-offset: 8
5971 * End: