ARM: pxa/cm-x300: fix V3020 RTC functionality
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4proc.c
blob80943d88252a9aa0bcb74af438ecfa04ec459fad
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/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
53 #include "nfs4_fs.h"
54 #include "delegation.h"
55 #include "internal.h"
56 #include "iostat.h"
57 #include "callback.h"
59 #define NFSDBG_FACILITY NFSDBG_PROC
61 #define NFS4_POLL_RETRY_MIN (HZ/10)
62 #define NFS4_POLL_RETRY_MAX (15*HZ)
64 #define NFS4_MAX_LOOP_ON_RECOVER (10)
66 struct nfs4_opendata;
67 static int _nfs4_proc_open(struct nfs4_opendata *data);
68 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
69 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
70 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
71 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
73 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
77 /* Prevent leaks of NFSv4 errors into userland */
78 static int nfs4_map_errors(int err)
80 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
86 dprintk("%s could not handle NFSv4 error %d\n",
87 __func__, -err);
88 break;
90 return -EIO;
94 * This is our standard bitmap for GETATTR requests.
96 const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
113 const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
122 const u32 nfs4_pathconf_bitmap[2] = {
123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
128 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
135 const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
154 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
155 struct nfs4_readdir_arg *readdir)
157 __be32 *start, *p;
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
161 readdir->cookie = cookie;
162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
209 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
211 int res;
213 might_sleep();
215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
216 nfs_wait_bit_killable, TASK_KILLABLE);
217 return res;
220 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
222 int res = 0;
224 might_sleep();
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
237 /* This is the error handling routine for processes that are allowed
238 * to sleep.
240 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
242 struct nfs_client *clp = server->nfs_client;
243 struct nfs4_state *state = exception->state;
244 int ret = errorcode;
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
256 goto do_state_recovery;
257 case -NFS4ERR_STALE_STATEID:
258 case -NFS4ERR_STALE_CLIENTID:
259 case -NFS4ERR_EXPIRED:
260 goto do_state_recovery;
261 #if defined(CONFIG_NFS_V4_1)
262 case -NFS4ERR_BADSESSION:
263 case -NFS4ERR_BADSLOT:
264 case -NFS4ERR_BAD_HIGH_SLOT:
265 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
266 case -NFS4ERR_DEADSESSION:
267 case -NFS4ERR_SEQ_FALSE_RETRY:
268 case -NFS4ERR_SEQ_MISORDERED:
269 dprintk("%s ERROR: %d Reset session\n", __func__,
270 errorcode);
271 nfs4_schedule_state_recovery(clp);
272 exception->retry = 1;
273 break;
274 #endif /* defined(CONFIG_NFS_V4_1) */
275 case -NFS4ERR_FILE_OPEN:
276 if (exception->timeout > HZ) {
277 /* We have retried a decent amount, time to
278 * fail
280 ret = -EBUSY;
281 break;
283 case -NFS4ERR_GRACE:
284 case -NFS4ERR_DELAY:
285 case -EKEYEXPIRED:
286 ret = nfs4_delay(server->client, &exception->timeout);
287 if (ret != 0)
288 break;
289 case -NFS4ERR_OLD_STATEID:
290 exception->retry = 1;
292 /* We failed to handle the error */
293 return nfs4_map_errors(ret);
294 do_state_recovery:
295 nfs4_schedule_state_recovery(clp);
296 ret = nfs4_wait_clnt_recover(clp);
297 if (ret == 0)
298 exception->retry = 1;
299 return ret;
303 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
305 struct nfs_client *clp = server->nfs_client;
306 spin_lock(&clp->cl_lock);
307 if (time_before(clp->cl_last_renewal,timestamp))
308 clp->cl_last_renewal = timestamp;
309 spin_unlock(&clp->cl_lock);
312 #if defined(CONFIG_NFS_V4_1)
315 * nfs4_free_slot - free a slot and efficiently update slot table.
317 * freeing a slot is trivially done by clearing its respective bit
318 * in the bitmap.
319 * If the freed slotid equals highest_used_slotid we want to update it
320 * so that the server would be able to size down the slot table if needed,
321 * otherwise we know that the highest_used_slotid is still in use.
322 * When updating highest_used_slotid there may be "holes" in the bitmap
323 * so we need to scan down from highest_used_slotid to 0 looking for the now
324 * highest slotid in use.
325 * If none found, highest_used_slotid is set to -1.
327 * Must be called while holding tbl->slot_tbl_lock
329 static void
330 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
332 int slotid = free_slotid;
334 /* clear used bit in bitmap */
335 __clear_bit(slotid, tbl->used_slots);
337 /* update highest_used_slotid when it is freed */
338 if (slotid == tbl->highest_used_slotid) {
339 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
340 if (slotid < tbl->max_slots)
341 tbl->highest_used_slotid = slotid;
342 else
343 tbl->highest_used_slotid = -1;
345 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
346 free_slotid, tbl->highest_used_slotid);
350 * Signal state manager thread if session is drained
352 static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
354 struct rpc_task *task;
356 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
357 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
358 if (task)
359 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
360 return;
363 if (ses->fc_slot_table.highest_used_slotid != -1)
364 return;
366 dprintk("%s COMPLETE: Session Drained\n", __func__);
367 complete(&ses->complete);
370 static void nfs41_sequence_free_slot(const struct nfs_client *clp,
371 struct nfs4_sequence_res *res)
373 struct nfs4_slot_table *tbl;
375 tbl = &clp->cl_session->fc_slot_table;
376 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
377 /* just wake up the next guy waiting since
378 * we may have not consumed a slot after all */
379 dprintk("%s: No slot\n", __func__);
380 return;
383 spin_lock(&tbl->slot_tbl_lock);
384 nfs4_free_slot(tbl, res->sr_slotid);
385 nfs41_check_drain_session_complete(clp->cl_session);
386 spin_unlock(&tbl->slot_tbl_lock);
387 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
390 static void nfs41_sequence_done(struct nfs_client *clp,
391 struct nfs4_sequence_res *res,
392 int rpc_status)
394 unsigned long timestamp;
395 struct nfs4_slot_table *tbl;
396 struct nfs4_slot *slot;
399 * sr_status remains 1 if an RPC level error occurred. The server
400 * may or may not have processed the sequence operation..
401 * Proceed as if the server received and processed the sequence
402 * operation.
404 if (res->sr_status == 1)
405 res->sr_status = NFS_OK;
407 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
408 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
409 goto out;
411 /* Check the SEQUENCE operation status */
412 if (res->sr_status == 0) {
413 tbl = &clp->cl_session->fc_slot_table;
414 slot = tbl->slots + res->sr_slotid;
415 /* Update the slot's sequence and clientid lease timer */
416 ++slot->seq_nr;
417 timestamp = res->sr_renewal_time;
418 spin_lock(&clp->cl_lock);
419 if (time_before(clp->cl_last_renewal, timestamp))
420 clp->cl_last_renewal = timestamp;
421 spin_unlock(&clp->cl_lock);
422 /* Check sequence flags */
423 if (atomic_read(&clp->cl_count) > 1)
424 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
426 out:
427 /* The session may be reset by one of the error handlers. */
428 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
429 nfs41_sequence_free_slot(clp, res);
433 * nfs4_find_slot - efficiently look for a free slot
435 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
436 * If found, we mark the slot as used, update the highest_used_slotid,
437 * and respectively set up the sequence operation args.
438 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
440 * Note: must be called with under the slot_tbl_lock.
442 static u8
443 nfs4_find_slot(struct nfs4_slot_table *tbl)
445 int slotid;
446 u8 ret_id = NFS4_MAX_SLOT_TABLE;
447 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
449 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
450 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
451 tbl->max_slots);
452 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
453 if (slotid >= tbl->max_slots)
454 goto out;
455 __set_bit(slotid, tbl->used_slots);
456 if (slotid > tbl->highest_used_slotid)
457 tbl->highest_used_slotid = slotid;
458 ret_id = slotid;
459 out:
460 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
461 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
462 return ret_id;
465 static int nfs41_setup_sequence(struct nfs4_session *session,
466 struct nfs4_sequence_args *args,
467 struct nfs4_sequence_res *res,
468 int cache_reply,
469 struct rpc_task *task)
471 struct nfs4_slot *slot;
472 struct nfs4_slot_table *tbl;
473 u8 slotid;
475 dprintk("--> %s\n", __func__);
476 /* slot already allocated? */
477 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
478 return 0;
480 memset(res, 0, sizeof(*res));
481 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
482 tbl = &session->fc_slot_table;
484 spin_lock(&tbl->slot_tbl_lock);
485 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
486 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
488 * The state manager will wait until the slot table is empty.
489 * Schedule the reset thread
491 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
492 spin_unlock(&tbl->slot_tbl_lock);
493 dprintk("%s Schedule Session Reset\n", __func__);
494 return -EAGAIN;
497 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
498 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
499 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
500 spin_unlock(&tbl->slot_tbl_lock);
501 dprintk("%s enforce FIFO order\n", __func__);
502 return -EAGAIN;
505 slotid = nfs4_find_slot(tbl);
506 if (slotid == NFS4_MAX_SLOT_TABLE) {
507 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
508 spin_unlock(&tbl->slot_tbl_lock);
509 dprintk("<-- %s: no free slots\n", __func__);
510 return -EAGAIN;
512 spin_unlock(&tbl->slot_tbl_lock);
514 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
515 slot = tbl->slots + slotid;
516 args->sa_session = session;
517 args->sa_slotid = slotid;
518 args->sa_cache_this = cache_reply;
520 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
522 res->sr_session = session;
523 res->sr_slotid = slotid;
524 res->sr_renewal_time = jiffies;
526 * sr_status is only set in decode_sequence, and so will remain
527 * set to 1 if an rpc level failure occurs.
529 res->sr_status = 1;
530 return 0;
533 int nfs4_setup_sequence(struct nfs_client *clp,
534 struct nfs4_sequence_args *args,
535 struct nfs4_sequence_res *res,
536 int cache_reply,
537 struct rpc_task *task)
539 int ret = 0;
541 dprintk("--> %s clp %p session %p sr_slotid %d\n",
542 __func__, clp, clp->cl_session, res->sr_slotid);
544 if (!nfs4_has_session(clp))
545 goto out;
546 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
547 task);
548 if (ret && ret != -EAGAIN) {
549 /* terminate rpc task */
550 task->tk_status = ret;
551 task->tk_action = NULL;
553 out:
554 dprintk("<-- %s status=%d\n", __func__, ret);
555 return ret;
558 struct nfs41_call_sync_data {
559 struct nfs_client *clp;
560 struct nfs4_sequence_args *seq_args;
561 struct nfs4_sequence_res *seq_res;
562 int cache_reply;
565 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
567 struct nfs41_call_sync_data *data = calldata;
569 dprintk("--> %s data->clp->cl_session %p\n", __func__,
570 data->clp->cl_session);
571 if (nfs4_setup_sequence(data->clp, data->seq_args,
572 data->seq_res, data->cache_reply, task))
573 return;
574 rpc_call_start(task);
577 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
579 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
580 nfs41_call_sync_prepare(task, calldata);
583 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
585 struct nfs41_call_sync_data *data = calldata;
587 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
590 struct rpc_call_ops nfs41_call_sync_ops = {
591 .rpc_call_prepare = nfs41_call_sync_prepare,
592 .rpc_call_done = nfs41_call_sync_done,
595 struct rpc_call_ops nfs41_call_priv_sync_ops = {
596 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
597 .rpc_call_done = nfs41_call_sync_done,
600 static int nfs4_call_sync_sequence(struct nfs_client *clp,
601 struct rpc_clnt *clnt,
602 struct rpc_message *msg,
603 struct nfs4_sequence_args *args,
604 struct nfs4_sequence_res *res,
605 int cache_reply,
606 int privileged)
608 int ret;
609 struct rpc_task *task;
610 struct nfs41_call_sync_data data = {
611 .clp = clp,
612 .seq_args = args,
613 .seq_res = res,
614 .cache_reply = cache_reply,
616 struct rpc_task_setup task_setup = {
617 .rpc_client = clnt,
618 .rpc_message = msg,
619 .callback_ops = &nfs41_call_sync_ops,
620 .callback_data = &data
623 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
624 if (privileged)
625 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
626 task = rpc_run_task(&task_setup);
627 if (IS_ERR(task))
628 ret = PTR_ERR(task);
629 else {
630 ret = task->tk_status;
631 rpc_put_task(task);
633 return ret;
636 int _nfs4_call_sync_session(struct nfs_server *server,
637 struct rpc_message *msg,
638 struct nfs4_sequence_args *args,
639 struct nfs4_sequence_res *res,
640 int cache_reply)
642 return nfs4_call_sync_sequence(server->nfs_client, server->client,
643 msg, args, res, cache_reply, 0);
646 #endif /* CONFIG_NFS_V4_1 */
648 int _nfs4_call_sync(struct nfs_server *server,
649 struct rpc_message *msg,
650 struct nfs4_sequence_args *args,
651 struct nfs4_sequence_res *res,
652 int cache_reply)
654 args->sa_session = res->sr_session = NULL;
655 return rpc_call_sync(server->client, msg, 0);
658 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
659 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
660 &(res)->seq_res, (cache_reply))
662 static void nfs4_sequence_done(const struct nfs_server *server,
663 struct nfs4_sequence_res *res, int rpc_status)
665 #ifdef CONFIG_NFS_V4_1
666 if (nfs4_has_session(server->nfs_client))
667 nfs41_sequence_done(server->nfs_client, res, rpc_status);
668 #endif /* CONFIG_NFS_V4_1 */
671 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
673 struct nfs_inode *nfsi = NFS_I(dir);
675 spin_lock(&dir->i_lock);
676 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
677 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
678 nfs_force_lookup_revalidate(dir);
679 nfsi->change_attr = cinfo->after;
680 spin_unlock(&dir->i_lock);
683 struct nfs4_opendata {
684 struct kref kref;
685 struct nfs_openargs o_arg;
686 struct nfs_openres o_res;
687 struct nfs_open_confirmargs c_arg;
688 struct nfs_open_confirmres c_res;
689 struct nfs_fattr f_attr;
690 struct nfs_fattr dir_attr;
691 struct path path;
692 struct dentry *dir;
693 struct nfs4_state_owner *owner;
694 struct nfs4_state *state;
695 struct iattr attrs;
696 unsigned long timestamp;
697 unsigned int rpc_done : 1;
698 int rpc_status;
699 int cancelled;
703 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
705 p->o_res.f_attr = &p->f_attr;
706 p->o_res.dir_attr = &p->dir_attr;
707 p->o_res.seqid = p->o_arg.seqid;
708 p->c_res.seqid = p->c_arg.seqid;
709 p->o_res.server = p->o_arg.server;
710 nfs_fattr_init(&p->f_attr);
711 nfs_fattr_init(&p->dir_attr);
712 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
715 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
716 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
717 const struct iattr *attrs,
718 gfp_t gfp_mask)
720 struct dentry *parent = dget_parent(path->dentry);
721 struct inode *dir = parent->d_inode;
722 struct nfs_server *server = NFS_SERVER(dir);
723 struct nfs4_opendata *p;
725 p = kzalloc(sizeof(*p), gfp_mask);
726 if (p == NULL)
727 goto err;
728 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
729 if (p->o_arg.seqid == NULL)
730 goto err_free;
731 path_get(path);
732 p->path = *path;
733 p->dir = parent;
734 p->owner = sp;
735 atomic_inc(&sp->so_count);
736 p->o_arg.fh = NFS_FH(dir);
737 p->o_arg.open_flags = flags;
738 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
739 p->o_arg.clientid = server->nfs_client->cl_clientid;
740 p->o_arg.id = sp->so_owner_id.id;
741 p->o_arg.name = &p->path.dentry->d_name;
742 p->o_arg.server = server;
743 p->o_arg.bitmask = server->attr_bitmask;
744 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
745 if (flags & O_EXCL) {
746 if (nfs4_has_persistent_session(server->nfs_client)) {
747 /* GUARDED */
748 p->o_arg.u.attrs = &p->attrs;
749 memcpy(&p->attrs, attrs, sizeof(p->attrs));
750 } else { /* EXCLUSIVE4_1 */
751 u32 *s = (u32 *) p->o_arg.u.verifier.data;
752 s[0] = jiffies;
753 s[1] = current->pid;
755 } else if (flags & O_CREAT) {
756 p->o_arg.u.attrs = &p->attrs;
757 memcpy(&p->attrs, attrs, sizeof(p->attrs));
759 p->c_arg.fh = &p->o_res.fh;
760 p->c_arg.stateid = &p->o_res.stateid;
761 p->c_arg.seqid = p->o_arg.seqid;
762 nfs4_init_opendata_res(p);
763 kref_init(&p->kref);
764 return p;
765 err_free:
766 kfree(p);
767 err:
768 dput(parent);
769 return NULL;
772 static void nfs4_opendata_free(struct kref *kref)
774 struct nfs4_opendata *p = container_of(kref,
775 struct nfs4_opendata, kref);
777 nfs_free_seqid(p->o_arg.seqid);
778 if (p->state != NULL)
779 nfs4_put_open_state(p->state);
780 nfs4_put_state_owner(p->owner);
781 dput(p->dir);
782 path_put(&p->path);
783 kfree(p);
786 static void nfs4_opendata_put(struct nfs4_opendata *p)
788 if (p != NULL)
789 kref_put(&p->kref, nfs4_opendata_free);
792 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
794 int ret;
796 ret = rpc_wait_for_completion_task(task);
797 return ret;
800 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
802 int ret = 0;
804 if (open_mode & O_EXCL)
805 goto out;
806 switch (mode & (FMODE_READ|FMODE_WRITE)) {
807 case FMODE_READ:
808 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
809 && state->n_rdonly != 0;
810 break;
811 case FMODE_WRITE:
812 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
813 && state->n_wronly != 0;
814 break;
815 case FMODE_READ|FMODE_WRITE:
816 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
817 && state->n_rdwr != 0;
819 out:
820 return ret;
823 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
825 if ((delegation->type & fmode) != fmode)
826 return 0;
827 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
828 return 0;
829 nfs_mark_delegation_referenced(delegation);
830 return 1;
833 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
835 switch (fmode) {
836 case FMODE_WRITE:
837 state->n_wronly++;
838 break;
839 case FMODE_READ:
840 state->n_rdonly++;
841 break;
842 case FMODE_READ|FMODE_WRITE:
843 state->n_rdwr++;
845 nfs4_state_set_mode_locked(state, state->state | fmode);
848 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
850 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
851 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
852 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
853 switch (fmode) {
854 case FMODE_READ:
855 set_bit(NFS_O_RDONLY_STATE, &state->flags);
856 break;
857 case FMODE_WRITE:
858 set_bit(NFS_O_WRONLY_STATE, &state->flags);
859 break;
860 case FMODE_READ|FMODE_WRITE:
861 set_bit(NFS_O_RDWR_STATE, &state->flags);
865 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
867 write_seqlock(&state->seqlock);
868 nfs_set_open_stateid_locked(state, stateid, fmode);
869 write_sequnlock(&state->seqlock);
872 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
875 * Protect the call to nfs4_state_set_mode_locked and
876 * serialise the stateid update
878 write_seqlock(&state->seqlock);
879 if (deleg_stateid != NULL) {
880 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
881 set_bit(NFS_DELEGATED_STATE, &state->flags);
883 if (open_stateid != NULL)
884 nfs_set_open_stateid_locked(state, open_stateid, fmode);
885 write_sequnlock(&state->seqlock);
886 spin_lock(&state->owner->so_lock);
887 update_open_stateflags(state, fmode);
888 spin_unlock(&state->owner->so_lock);
891 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
893 struct nfs_inode *nfsi = NFS_I(state->inode);
894 struct nfs_delegation *deleg_cur;
895 int ret = 0;
897 fmode &= (FMODE_READ|FMODE_WRITE);
899 rcu_read_lock();
900 deleg_cur = rcu_dereference(nfsi->delegation);
901 if (deleg_cur == NULL)
902 goto no_delegation;
904 spin_lock(&deleg_cur->lock);
905 if (nfsi->delegation != deleg_cur ||
906 (deleg_cur->type & fmode) != fmode)
907 goto no_delegation_unlock;
909 if (delegation == NULL)
910 delegation = &deleg_cur->stateid;
911 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
912 goto no_delegation_unlock;
914 nfs_mark_delegation_referenced(deleg_cur);
915 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
916 ret = 1;
917 no_delegation_unlock:
918 spin_unlock(&deleg_cur->lock);
919 no_delegation:
920 rcu_read_unlock();
922 if (!ret && open_stateid != NULL) {
923 __update_open_stateid(state, open_stateid, NULL, fmode);
924 ret = 1;
927 return ret;
931 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
933 struct nfs_delegation *delegation;
935 rcu_read_lock();
936 delegation = rcu_dereference(NFS_I(inode)->delegation);
937 if (delegation == NULL || (delegation->type & fmode) == fmode) {
938 rcu_read_unlock();
939 return;
941 rcu_read_unlock();
942 nfs_inode_return_delegation(inode);
945 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
947 struct nfs4_state *state = opendata->state;
948 struct nfs_inode *nfsi = NFS_I(state->inode);
949 struct nfs_delegation *delegation;
950 int open_mode = opendata->o_arg.open_flags & O_EXCL;
951 fmode_t fmode = opendata->o_arg.fmode;
952 nfs4_stateid stateid;
953 int ret = -EAGAIN;
955 for (;;) {
956 if (can_open_cached(state, fmode, open_mode)) {
957 spin_lock(&state->owner->so_lock);
958 if (can_open_cached(state, fmode, open_mode)) {
959 update_open_stateflags(state, fmode);
960 spin_unlock(&state->owner->so_lock);
961 goto out_return_state;
963 spin_unlock(&state->owner->so_lock);
965 rcu_read_lock();
966 delegation = rcu_dereference(nfsi->delegation);
967 if (delegation == NULL ||
968 !can_open_delegated(delegation, fmode)) {
969 rcu_read_unlock();
970 break;
972 /* Save the delegation */
973 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
974 rcu_read_unlock();
975 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
976 if (ret != 0)
977 goto out;
978 ret = -EAGAIN;
980 /* Try to update the stateid using the delegation */
981 if (update_open_stateid(state, NULL, &stateid, fmode))
982 goto out_return_state;
984 out:
985 return ERR_PTR(ret);
986 out_return_state:
987 atomic_inc(&state->count);
988 return state;
991 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
993 struct inode *inode;
994 struct nfs4_state *state = NULL;
995 struct nfs_delegation *delegation;
996 int ret;
998 if (!data->rpc_done) {
999 state = nfs4_try_open_cached(data);
1000 goto out;
1003 ret = -EAGAIN;
1004 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1005 goto err;
1006 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1007 ret = PTR_ERR(inode);
1008 if (IS_ERR(inode))
1009 goto err;
1010 ret = -ENOMEM;
1011 state = nfs4_get_open_state(inode, data->owner);
1012 if (state == NULL)
1013 goto err_put_inode;
1014 if (data->o_res.delegation_type != 0) {
1015 int delegation_flags = 0;
1017 rcu_read_lock();
1018 delegation = rcu_dereference(NFS_I(inode)->delegation);
1019 if (delegation)
1020 delegation_flags = delegation->flags;
1021 rcu_read_unlock();
1022 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1023 nfs_inode_set_delegation(state->inode,
1024 data->owner->so_cred,
1025 &data->o_res);
1026 else
1027 nfs_inode_reclaim_delegation(state->inode,
1028 data->owner->so_cred,
1029 &data->o_res);
1032 update_open_stateid(state, &data->o_res.stateid, NULL,
1033 data->o_arg.fmode);
1034 iput(inode);
1035 out:
1036 return state;
1037 err_put_inode:
1038 iput(inode);
1039 err:
1040 return ERR_PTR(ret);
1043 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1045 struct nfs_inode *nfsi = NFS_I(state->inode);
1046 struct nfs_open_context *ctx;
1048 spin_lock(&state->inode->i_lock);
1049 list_for_each_entry(ctx, &nfsi->open_files, list) {
1050 if (ctx->state != state)
1051 continue;
1052 get_nfs_open_context(ctx);
1053 spin_unlock(&state->inode->i_lock);
1054 return ctx;
1056 spin_unlock(&state->inode->i_lock);
1057 return ERR_PTR(-ENOENT);
1060 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1062 struct nfs4_opendata *opendata;
1064 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1065 if (opendata == NULL)
1066 return ERR_PTR(-ENOMEM);
1067 opendata->state = state;
1068 atomic_inc(&state->count);
1069 return opendata;
1072 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1074 struct nfs4_state *newstate;
1075 int ret;
1077 opendata->o_arg.open_flags = 0;
1078 opendata->o_arg.fmode = fmode;
1079 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1080 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1081 nfs4_init_opendata_res(opendata);
1082 ret = _nfs4_recover_proc_open(opendata);
1083 if (ret != 0)
1084 return ret;
1085 newstate = nfs4_opendata_to_nfs4_state(opendata);
1086 if (IS_ERR(newstate))
1087 return PTR_ERR(newstate);
1088 nfs4_close_state(&opendata->path, newstate, fmode);
1089 *res = newstate;
1090 return 0;
1093 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1095 struct nfs4_state *newstate;
1096 int ret;
1098 /* memory barrier prior to reading state->n_* */
1099 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1100 smp_rmb();
1101 if (state->n_rdwr != 0) {
1102 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1103 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1104 if (ret != 0)
1105 return ret;
1106 if (newstate != state)
1107 return -ESTALE;
1109 if (state->n_wronly != 0) {
1110 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1111 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1112 if (ret != 0)
1113 return ret;
1114 if (newstate != state)
1115 return -ESTALE;
1117 if (state->n_rdonly != 0) {
1118 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1119 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1120 if (ret != 0)
1121 return ret;
1122 if (newstate != state)
1123 return -ESTALE;
1126 * We may have performed cached opens for all three recoveries.
1127 * Check if we need to update the current stateid.
1129 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1130 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1131 write_seqlock(&state->seqlock);
1132 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1133 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1134 write_sequnlock(&state->seqlock);
1136 return 0;
1140 * OPEN_RECLAIM:
1141 * reclaim state on the server after a reboot.
1143 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1145 struct nfs_delegation *delegation;
1146 struct nfs4_opendata *opendata;
1147 fmode_t delegation_type = 0;
1148 int status;
1150 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1151 if (IS_ERR(opendata))
1152 return PTR_ERR(opendata);
1153 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1154 opendata->o_arg.fh = NFS_FH(state->inode);
1155 rcu_read_lock();
1156 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1157 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1158 delegation_type = delegation->type;
1159 rcu_read_unlock();
1160 opendata->o_arg.u.delegation_type = delegation_type;
1161 status = nfs4_open_recover(opendata, state);
1162 nfs4_opendata_put(opendata);
1163 return status;
1166 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1168 struct nfs_server *server = NFS_SERVER(state->inode);
1169 struct nfs4_exception exception = { };
1170 int err;
1171 do {
1172 err = _nfs4_do_open_reclaim(ctx, state);
1173 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
1174 break;
1175 nfs4_handle_exception(server, err, &exception);
1176 } while (exception.retry);
1177 return err;
1180 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1182 struct nfs_open_context *ctx;
1183 int ret;
1185 ctx = nfs4_state_find_open_context(state);
1186 if (IS_ERR(ctx))
1187 return PTR_ERR(ctx);
1188 ret = nfs4_do_open_reclaim(ctx, state);
1189 put_nfs_open_context(ctx);
1190 return ret;
1193 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1195 struct nfs4_opendata *opendata;
1196 int ret;
1198 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1199 if (IS_ERR(opendata))
1200 return PTR_ERR(opendata);
1201 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1202 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1203 sizeof(opendata->o_arg.u.delegation.data));
1204 ret = nfs4_open_recover(opendata, state);
1205 nfs4_opendata_put(opendata);
1206 return ret;
1209 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1211 struct nfs4_exception exception = { };
1212 struct nfs_server *server = NFS_SERVER(state->inode);
1213 int err;
1214 do {
1215 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1216 switch (err) {
1217 case 0:
1218 case -ENOENT:
1219 case -ESTALE:
1220 goto out;
1221 case -NFS4ERR_BADSESSION:
1222 case -NFS4ERR_BADSLOT:
1223 case -NFS4ERR_BAD_HIGH_SLOT:
1224 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1225 case -NFS4ERR_DEADSESSION:
1226 nfs4_schedule_state_recovery(
1227 server->nfs_client);
1228 goto out;
1229 case -NFS4ERR_STALE_CLIENTID:
1230 case -NFS4ERR_STALE_STATEID:
1231 case -NFS4ERR_EXPIRED:
1232 /* Don't recall a delegation if it was lost */
1233 nfs4_schedule_state_recovery(server->nfs_client);
1234 goto out;
1235 case -ERESTARTSYS:
1237 * The show must go on: exit, but mark the
1238 * stateid as needing recovery.
1240 case -NFS4ERR_ADMIN_REVOKED:
1241 case -NFS4ERR_BAD_STATEID:
1242 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1243 case -ENOMEM:
1244 err = 0;
1245 goto out;
1247 err = nfs4_handle_exception(server, err, &exception);
1248 } while (exception.retry);
1249 out:
1250 return err;
1253 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1255 struct nfs4_opendata *data = calldata;
1257 data->rpc_status = task->tk_status;
1258 if (RPC_ASSASSINATED(task))
1259 return;
1260 if (data->rpc_status == 0) {
1261 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1262 sizeof(data->o_res.stateid.data));
1263 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1264 renew_lease(data->o_res.server, data->timestamp);
1265 data->rpc_done = 1;
1269 static void nfs4_open_confirm_release(void *calldata)
1271 struct nfs4_opendata *data = calldata;
1272 struct nfs4_state *state = NULL;
1274 /* If this request hasn't been cancelled, do nothing */
1275 if (data->cancelled == 0)
1276 goto out_free;
1277 /* In case of error, no cleanup! */
1278 if (!data->rpc_done)
1279 goto out_free;
1280 state = nfs4_opendata_to_nfs4_state(data);
1281 if (!IS_ERR(state))
1282 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1283 out_free:
1284 nfs4_opendata_put(data);
1287 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1288 .rpc_call_done = nfs4_open_confirm_done,
1289 .rpc_release = nfs4_open_confirm_release,
1293 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1295 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1297 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1298 struct rpc_task *task;
1299 struct rpc_message msg = {
1300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1301 .rpc_argp = &data->c_arg,
1302 .rpc_resp = &data->c_res,
1303 .rpc_cred = data->owner->so_cred,
1305 struct rpc_task_setup task_setup_data = {
1306 .rpc_client = server->client,
1307 .rpc_message = &msg,
1308 .callback_ops = &nfs4_open_confirm_ops,
1309 .callback_data = data,
1310 .workqueue = nfsiod_workqueue,
1311 .flags = RPC_TASK_ASYNC,
1313 int status;
1315 kref_get(&data->kref);
1316 data->rpc_done = 0;
1317 data->rpc_status = 0;
1318 data->timestamp = jiffies;
1319 task = rpc_run_task(&task_setup_data);
1320 if (IS_ERR(task))
1321 return PTR_ERR(task);
1322 status = nfs4_wait_for_completion_rpc_task(task);
1323 if (status != 0) {
1324 data->cancelled = 1;
1325 smp_wmb();
1326 } else
1327 status = data->rpc_status;
1328 rpc_put_task(task);
1329 return status;
1332 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1334 struct nfs4_opendata *data = calldata;
1335 struct nfs4_state_owner *sp = data->owner;
1337 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1338 return;
1340 * Check if we still need to send an OPEN call, or if we can use
1341 * a delegation instead.
1343 if (data->state != NULL) {
1344 struct nfs_delegation *delegation;
1346 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1347 goto out_no_action;
1348 rcu_read_lock();
1349 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1350 if (delegation != NULL &&
1351 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1352 rcu_read_unlock();
1353 goto out_no_action;
1355 rcu_read_unlock();
1357 /* Update sequence id. */
1358 data->o_arg.id = sp->so_owner_id.id;
1359 data->o_arg.clientid = sp->so_client->cl_clientid;
1360 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1361 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1362 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1364 data->timestamp = jiffies;
1365 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1366 &data->o_arg.seq_args,
1367 &data->o_res.seq_res, 1, task))
1368 return;
1369 rpc_call_start(task);
1370 return;
1371 out_no_action:
1372 task->tk_action = NULL;
1376 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1378 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1379 nfs4_open_prepare(task, calldata);
1382 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1384 struct nfs4_opendata *data = calldata;
1386 data->rpc_status = task->tk_status;
1388 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1389 task->tk_status);
1391 if (RPC_ASSASSINATED(task))
1392 return;
1393 if (task->tk_status == 0) {
1394 switch (data->o_res.f_attr->mode & S_IFMT) {
1395 case S_IFREG:
1396 break;
1397 case S_IFLNK:
1398 data->rpc_status = -ELOOP;
1399 break;
1400 case S_IFDIR:
1401 data->rpc_status = -EISDIR;
1402 break;
1403 default:
1404 data->rpc_status = -ENOTDIR;
1406 renew_lease(data->o_res.server, data->timestamp);
1407 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1408 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1410 data->rpc_done = 1;
1413 static void nfs4_open_release(void *calldata)
1415 struct nfs4_opendata *data = calldata;
1416 struct nfs4_state *state = NULL;
1418 /* If this request hasn't been cancelled, do nothing */
1419 if (data->cancelled == 0)
1420 goto out_free;
1421 /* In case of error, no cleanup! */
1422 if (data->rpc_status != 0 || !data->rpc_done)
1423 goto out_free;
1424 /* In case we need an open_confirm, no cleanup! */
1425 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1426 goto out_free;
1427 state = nfs4_opendata_to_nfs4_state(data);
1428 if (!IS_ERR(state))
1429 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1430 out_free:
1431 nfs4_opendata_put(data);
1434 static const struct rpc_call_ops nfs4_open_ops = {
1435 .rpc_call_prepare = nfs4_open_prepare,
1436 .rpc_call_done = nfs4_open_done,
1437 .rpc_release = nfs4_open_release,
1440 static const struct rpc_call_ops nfs4_recover_open_ops = {
1441 .rpc_call_prepare = nfs4_recover_open_prepare,
1442 .rpc_call_done = nfs4_open_done,
1443 .rpc_release = nfs4_open_release,
1446 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1448 struct inode *dir = data->dir->d_inode;
1449 struct nfs_server *server = NFS_SERVER(dir);
1450 struct nfs_openargs *o_arg = &data->o_arg;
1451 struct nfs_openres *o_res = &data->o_res;
1452 struct rpc_task *task;
1453 struct rpc_message msg = {
1454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1455 .rpc_argp = o_arg,
1456 .rpc_resp = o_res,
1457 .rpc_cred = data->owner->so_cred,
1459 struct rpc_task_setup task_setup_data = {
1460 .rpc_client = server->client,
1461 .rpc_message = &msg,
1462 .callback_ops = &nfs4_open_ops,
1463 .callback_data = data,
1464 .workqueue = nfsiod_workqueue,
1465 .flags = RPC_TASK_ASYNC,
1467 int status;
1469 kref_get(&data->kref);
1470 data->rpc_done = 0;
1471 data->rpc_status = 0;
1472 data->cancelled = 0;
1473 if (isrecover)
1474 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1475 task = rpc_run_task(&task_setup_data);
1476 if (IS_ERR(task))
1477 return PTR_ERR(task);
1478 status = nfs4_wait_for_completion_rpc_task(task);
1479 if (status != 0) {
1480 data->cancelled = 1;
1481 smp_wmb();
1482 } else
1483 status = data->rpc_status;
1484 rpc_put_task(task);
1486 return status;
1489 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1491 struct inode *dir = data->dir->d_inode;
1492 struct nfs_openres *o_res = &data->o_res;
1493 int status;
1495 status = nfs4_run_open_task(data, 1);
1496 if (status != 0 || !data->rpc_done)
1497 return status;
1499 nfs_refresh_inode(dir, o_res->dir_attr);
1501 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1502 status = _nfs4_proc_open_confirm(data);
1503 if (status != 0)
1504 return status;
1507 return status;
1511 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1513 static int _nfs4_proc_open(struct nfs4_opendata *data)
1515 struct inode *dir = data->dir->d_inode;
1516 struct nfs_server *server = NFS_SERVER(dir);
1517 struct nfs_openargs *o_arg = &data->o_arg;
1518 struct nfs_openres *o_res = &data->o_res;
1519 int status;
1521 status = nfs4_run_open_task(data, 0);
1522 if (status != 0 || !data->rpc_done)
1523 return status;
1525 if (o_arg->open_flags & O_CREAT) {
1526 update_changeattr(dir, &o_res->cinfo);
1527 nfs_post_op_update_inode(dir, o_res->dir_attr);
1528 } else
1529 nfs_refresh_inode(dir, o_res->dir_attr);
1530 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1531 server->caps &= ~NFS_CAP_POSIX_LOCK;
1532 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1533 status = _nfs4_proc_open_confirm(data);
1534 if (status != 0)
1535 return status;
1537 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1538 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1539 return 0;
1542 static int nfs4_recover_expired_lease(struct nfs_server *server)
1544 struct nfs_client *clp = server->nfs_client;
1545 unsigned int loop;
1546 int ret;
1548 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1549 ret = nfs4_wait_clnt_recover(clp);
1550 if (ret != 0)
1551 break;
1552 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1553 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1554 break;
1555 nfs4_schedule_state_recovery(clp);
1556 ret = -EIO;
1558 return ret;
1562 * OPEN_EXPIRED:
1563 * reclaim state on the server after a network partition.
1564 * Assumes caller holds the appropriate lock
1566 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1568 struct nfs4_opendata *opendata;
1569 int ret;
1571 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1572 if (IS_ERR(opendata))
1573 return PTR_ERR(opendata);
1574 ret = nfs4_open_recover(opendata, state);
1575 if (ret == -ESTALE)
1576 d_drop(ctx->path.dentry);
1577 nfs4_opendata_put(opendata);
1578 return ret;
1581 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1583 struct nfs_server *server = NFS_SERVER(state->inode);
1584 struct nfs4_exception exception = { };
1585 int err;
1587 do {
1588 err = _nfs4_open_expired(ctx, state);
1589 switch (err) {
1590 default:
1591 goto out;
1592 case -NFS4ERR_GRACE:
1593 case -NFS4ERR_DELAY:
1594 case -EKEYEXPIRED:
1595 nfs4_handle_exception(server, err, &exception);
1596 err = 0;
1598 } while (exception.retry);
1599 out:
1600 return err;
1603 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1605 struct nfs_open_context *ctx;
1606 int ret;
1608 ctx = nfs4_state_find_open_context(state);
1609 if (IS_ERR(ctx))
1610 return PTR_ERR(ctx);
1611 ret = nfs4_do_open_expired(ctx, state);
1612 put_nfs_open_context(ctx);
1613 return ret;
1617 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1618 * fields corresponding to attributes that were used to store the verifier.
1619 * Make sure we clobber those fields in the later setattr call
1621 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1623 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1624 !(sattr->ia_valid & ATTR_ATIME_SET))
1625 sattr->ia_valid |= ATTR_ATIME;
1627 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1628 !(sattr->ia_valid & ATTR_MTIME_SET))
1629 sattr->ia_valid |= ATTR_MTIME;
1633 * Returns a referenced nfs4_state
1635 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)
1637 struct nfs4_state_owner *sp;
1638 struct nfs4_state *state = NULL;
1639 struct nfs_server *server = NFS_SERVER(dir);
1640 struct nfs4_opendata *opendata;
1641 int status;
1643 /* Protect against reboot recovery conflicts */
1644 status = -ENOMEM;
1645 if (!(sp = nfs4_get_state_owner(server, cred))) {
1646 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1647 goto out_err;
1649 status = nfs4_recover_expired_lease(server);
1650 if (status != 0)
1651 goto err_put_state_owner;
1652 if (path->dentry->d_inode != NULL)
1653 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1654 status = -ENOMEM;
1655 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1656 if (opendata == NULL)
1657 goto err_put_state_owner;
1659 if (path->dentry->d_inode != NULL)
1660 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1662 status = _nfs4_proc_open(opendata);
1663 if (status != 0)
1664 goto err_opendata_put;
1666 state = nfs4_opendata_to_nfs4_state(opendata);
1667 status = PTR_ERR(state);
1668 if (IS_ERR(state))
1669 goto err_opendata_put;
1670 if (server->caps & NFS_CAP_POSIX_LOCK)
1671 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1673 if (opendata->o_arg.open_flags & O_EXCL) {
1674 nfs4_exclusive_attrset(opendata, sattr);
1676 nfs_fattr_init(opendata->o_res.f_attr);
1677 status = nfs4_do_setattr(state->inode, cred,
1678 opendata->o_res.f_attr, sattr,
1679 state);
1680 if (status == 0)
1681 nfs_setattr_update_inode(state->inode, sattr);
1682 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1684 nfs4_opendata_put(opendata);
1685 nfs4_put_state_owner(sp);
1686 *res = state;
1687 return 0;
1688 err_opendata_put:
1689 nfs4_opendata_put(opendata);
1690 err_put_state_owner:
1691 nfs4_put_state_owner(sp);
1692 out_err:
1693 *res = NULL;
1694 return status;
1698 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)
1700 struct nfs4_exception exception = { };
1701 struct nfs4_state *res;
1702 int status;
1704 do {
1705 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1706 if (status == 0)
1707 break;
1708 /* NOTE: BAD_SEQID means the server and client disagree about the
1709 * book-keeping w.r.t. state-changing operations
1710 * (OPEN/CLOSE/LOCK/LOCKU...)
1711 * It is actually a sign of a bug on the client or on the server.
1713 * If we receive a BAD_SEQID error in the particular case of
1714 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1715 * have unhashed the old state_owner for us, and that we can
1716 * therefore safely retry using a new one. We should still warn
1717 * the user though...
1719 if (status == -NFS4ERR_BAD_SEQID) {
1720 printk(KERN_WARNING "NFS: v4 server %s "
1721 " returned a bad sequence-id error!\n",
1722 NFS_SERVER(dir)->nfs_client->cl_hostname);
1723 exception.retry = 1;
1724 continue;
1727 * BAD_STATEID on OPEN means that the server cancelled our
1728 * state before it received the OPEN_CONFIRM.
1729 * Recover by retrying the request as per the discussion
1730 * on Page 181 of RFC3530.
1732 if (status == -NFS4ERR_BAD_STATEID) {
1733 exception.retry = 1;
1734 continue;
1736 if (status == -EAGAIN) {
1737 /* We must have found a delegation */
1738 exception.retry = 1;
1739 continue;
1741 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1742 status, &exception));
1743 } while (exception.retry);
1744 return res;
1747 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1748 struct nfs_fattr *fattr, struct iattr *sattr,
1749 struct nfs4_state *state)
1751 struct nfs_server *server = NFS_SERVER(inode);
1752 struct nfs_setattrargs arg = {
1753 .fh = NFS_FH(inode),
1754 .iap = sattr,
1755 .server = server,
1756 .bitmask = server->attr_bitmask,
1758 struct nfs_setattrres res = {
1759 .fattr = fattr,
1760 .server = server,
1762 struct rpc_message msg = {
1763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1764 .rpc_argp = &arg,
1765 .rpc_resp = &res,
1766 .rpc_cred = cred,
1768 unsigned long timestamp = jiffies;
1769 int status;
1771 nfs_fattr_init(fattr);
1773 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1774 /* Use that stateid */
1775 } else if (state != NULL) {
1776 nfs4_copy_stateid(&arg.stateid, state, current->files);
1777 } else
1778 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1780 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1781 if (status == 0 && state != NULL)
1782 renew_lease(server, timestamp);
1783 return status;
1786 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1787 struct nfs_fattr *fattr, struct iattr *sattr,
1788 struct nfs4_state *state)
1790 struct nfs_server *server = NFS_SERVER(inode);
1791 struct nfs4_exception exception = { };
1792 int err;
1793 do {
1794 err = nfs4_handle_exception(server,
1795 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1796 &exception);
1797 } while (exception.retry);
1798 return err;
1801 struct nfs4_closedata {
1802 struct path path;
1803 struct inode *inode;
1804 struct nfs4_state *state;
1805 struct nfs_closeargs arg;
1806 struct nfs_closeres res;
1807 struct nfs_fattr fattr;
1808 unsigned long timestamp;
1811 static void nfs4_free_closedata(void *data)
1813 struct nfs4_closedata *calldata = data;
1814 struct nfs4_state_owner *sp = calldata->state->owner;
1816 nfs4_put_open_state(calldata->state);
1817 nfs_free_seqid(calldata->arg.seqid);
1818 nfs4_put_state_owner(sp);
1819 path_put(&calldata->path);
1820 kfree(calldata);
1823 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1824 fmode_t fmode)
1826 spin_lock(&state->owner->so_lock);
1827 if (!(fmode & FMODE_READ))
1828 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1829 if (!(fmode & FMODE_WRITE))
1830 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1831 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1832 spin_unlock(&state->owner->so_lock);
1835 static void nfs4_close_done(struct rpc_task *task, void *data)
1837 struct nfs4_closedata *calldata = data;
1838 struct nfs4_state *state = calldata->state;
1839 struct nfs_server *server = NFS_SERVER(calldata->inode);
1841 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1842 if (RPC_ASSASSINATED(task))
1843 return;
1844 /* hmm. we are done with the inode, and in the process of freeing
1845 * the state_owner. we keep this around to process errors
1847 switch (task->tk_status) {
1848 case 0:
1849 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1850 renew_lease(server, calldata->timestamp);
1851 nfs4_close_clear_stateid_flags(state,
1852 calldata->arg.fmode);
1853 break;
1854 case -NFS4ERR_STALE_STATEID:
1855 case -NFS4ERR_OLD_STATEID:
1856 case -NFS4ERR_BAD_STATEID:
1857 case -NFS4ERR_EXPIRED:
1858 if (calldata->arg.fmode == 0)
1859 break;
1860 default:
1861 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1862 rpc_restart_call_prepare(task);
1864 nfs_release_seqid(calldata->arg.seqid);
1865 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1868 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1870 struct nfs4_closedata *calldata = data;
1871 struct nfs4_state *state = calldata->state;
1872 int call_close = 0;
1874 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1875 return;
1877 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1878 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1879 spin_lock(&state->owner->so_lock);
1880 /* Calculate the change in open mode */
1881 if (state->n_rdwr == 0) {
1882 if (state->n_rdonly == 0) {
1883 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1884 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1885 calldata->arg.fmode &= ~FMODE_READ;
1887 if (state->n_wronly == 0) {
1888 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1889 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1890 calldata->arg.fmode &= ~FMODE_WRITE;
1893 spin_unlock(&state->owner->so_lock);
1895 if (!call_close) {
1896 /* Note: exit _without_ calling nfs4_close_done */
1897 task->tk_action = NULL;
1898 return;
1901 if (calldata->arg.fmode == 0)
1902 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1904 nfs_fattr_init(calldata->res.fattr);
1905 calldata->timestamp = jiffies;
1906 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1907 &calldata->arg.seq_args, &calldata->res.seq_res,
1908 1, task))
1909 return;
1910 rpc_call_start(task);
1913 static const struct rpc_call_ops nfs4_close_ops = {
1914 .rpc_call_prepare = nfs4_close_prepare,
1915 .rpc_call_done = nfs4_close_done,
1916 .rpc_release = nfs4_free_closedata,
1920 * It is possible for data to be read/written from a mem-mapped file
1921 * after the sys_close call (which hits the vfs layer as a flush).
1922 * This means that we can't safely call nfsv4 close on a file until
1923 * the inode is cleared. This in turn means that we are not good
1924 * NFSv4 citizens - we do not indicate to the server to update the file's
1925 * share state even when we are done with one of the three share
1926 * stateid's in the inode.
1928 * NOTE: Caller must be holding the sp->so_owner semaphore!
1930 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
1932 struct nfs_server *server = NFS_SERVER(state->inode);
1933 struct nfs4_closedata *calldata;
1934 struct nfs4_state_owner *sp = state->owner;
1935 struct rpc_task *task;
1936 struct rpc_message msg = {
1937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1938 .rpc_cred = state->owner->so_cred,
1940 struct rpc_task_setup task_setup_data = {
1941 .rpc_client = server->client,
1942 .rpc_message = &msg,
1943 .callback_ops = &nfs4_close_ops,
1944 .workqueue = nfsiod_workqueue,
1945 .flags = RPC_TASK_ASYNC,
1947 int status = -ENOMEM;
1949 calldata = kzalloc(sizeof(*calldata), gfp_mask);
1950 if (calldata == NULL)
1951 goto out;
1952 calldata->inode = state->inode;
1953 calldata->state = state;
1954 calldata->arg.fh = NFS_FH(state->inode);
1955 calldata->arg.stateid = &state->open_stateid;
1956 /* Serialization for the sequence id */
1957 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
1958 if (calldata->arg.seqid == NULL)
1959 goto out_free_calldata;
1960 calldata->arg.fmode = 0;
1961 calldata->arg.bitmask = server->cache_consistency_bitmask;
1962 calldata->res.fattr = &calldata->fattr;
1963 calldata->res.seqid = calldata->arg.seqid;
1964 calldata->res.server = server;
1965 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1966 path_get(path);
1967 calldata->path = *path;
1969 msg.rpc_argp = &calldata->arg,
1970 msg.rpc_resp = &calldata->res,
1971 task_setup_data.callback_data = calldata;
1972 task = rpc_run_task(&task_setup_data);
1973 if (IS_ERR(task))
1974 return PTR_ERR(task);
1975 status = 0;
1976 if (wait)
1977 status = rpc_wait_for_completion_task(task);
1978 rpc_put_task(task);
1979 return status;
1980 out_free_calldata:
1981 kfree(calldata);
1982 out:
1983 nfs4_put_open_state(state);
1984 nfs4_put_state_owner(sp);
1985 return status;
1988 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1990 struct file *filp;
1991 int ret;
1993 /* If the open_intent is for execute, we have an extra check to make */
1994 if (fmode & FMODE_EXEC) {
1995 ret = nfs_may_open(state->inode,
1996 state->owner->so_cred,
1997 nd->intent.open.flags);
1998 if (ret < 0)
1999 goto out_close;
2001 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
2002 if (!IS_ERR(filp)) {
2003 struct nfs_open_context *ctx;
2004 ctx = nfs_file_open_context(filp);
2005 ctx->state = state;
2006 return 0;
2008 ret = PTR_ERR(filp);
2009 out_close:
2010 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
2011 return ret;
2014 struct dentry *
2015 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2017 struct path path = {
2018 .mnt = nd->path.mnt,
2019 .dentry = dentry,
2021 struct dentry *parent;
2022 struct iattr attr;
2023 struct rpc_cred *cred;
2024 struct nfs4_state *state;
2025 struct dentry *res;
2026 int open_flags = nd->intent.open.flags;
2027 fmode_t fmode = open_flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
2029 if (nd->flags & LOOKUP_CREATE) {
2030 attr.ia_mode = nd->intent.open.create_mode;
2031 attr.ia_valid = ATTR_MODE;
2032 if (!IS_POSIXACL(dir))
2033 attr.ia_mode &= ~current_umask();
2034 } else {
2035 open_flags &= ~O_EXCL;
2036 attr.ia_valid = 0;
2037 BUG_ON(open_flags & O_CREAT);
2040 cred = rpc_lookup_cred();
2041 if (IS_ERR(cred))
2042 return (struct dentry *)cred;
2043 parent = dentry->d_parent;
2044 /* Protect against concurrent sillydeletes */
2045 nfs_block_sillyrename(parent);
2046 state = nfs4_do_open(dir, &path, fmode, open_flags, &attr, cred);
2047 put_rpccred(cred);
2048 if (IS_ERR(state)) {
2049 if (PTR_ERR(state) == -ENOENT) {
2050 d_add(dentry, NULL);
2051 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2053 nfs_unblock_sillyrename(parent);
2054 return (struct dentry *)state;
2056 res = d_add_unique(dentry, igrab(state->inode));
2057 if (res != NULL)
2058 path.dentry = res;
2059 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
2060 nfs_unblock_sillyrename(parent);
2061 nfs4_intent_set_file(nd, &path, state, fmode);
2062 return res;
2066 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
2068 struct path path = {
2069 .mnt = nd->path.mnt,
2070 .dentry = dentry,
2072 struct rpc_cred *cred;
2073 struct nfs4_state *state;
2074 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
2076 cred = rpc_lookup_cred();
2077 if (IS_ERR(cred))
2078 return PTR_ERR(cred);
2079 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
2080 put_rpccred(cred);
2081 if (IS_ERR(state)) {
2082 switch (PTR_ERR(state)) {
2083 case -EPERM:
2084 case -EACCES:
2085 case -EDQUOT:
2086 case -ENOSPC:
2087 case -EROFS:
2088 return PTR_ERR(state);
2089 default:
2090 goto out_drop;
2093 if (state->inode == dentry->d_inode) {
2094 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2095 nfs4_intent_set_file(nd, &path, state, fmode);
2096 return 1;
2098 nfs4_close_sync(&path, state, fmode);
2099 out_drop:
2100 d_drop(dentry);
2101 return 0;
2104 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2106 if (ctx->state == NULL)
2107 return;
2108 if (is_sync)
2109 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2110 else
2111 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2114 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2116 struct nfs4_server_caps_arg args = {
2117 .fhandle = fhandle,
2119 struct nfs4_server_caps_res res = {};
2120 struct rpc_message msg = {
2121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2122 .rpc_argp = &args,
2123 .rpc_resp = &res,
2125 int status;
2127 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2128 if (status == 0) {
2129 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2130 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2131 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2132 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2133 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2134 NFS_CAP_CTIME|NFS_CAP_MTIME);
2135 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2136 server->caps |= NFS_CAP_ACLS;
2137 if (res.has_links != 0)
2138 server->caps |= NFS_CAP_HARDLINKS;
2139 if (res.has_symlinks != 0)
2140 server->caps |= NFS_CAP_SYMLINKS;
2141 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2142 server->caps |= NFS_CAP_FILEID;
2143 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2144 server->caps |= NFS_CAP_MODE;
2145 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2146 server->caps |= NFS_CAP_NLINK;
2147 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2148 server->caps |= NFS_CAP_OWNER;
2149 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2150 server->caps |= NFS_CAP_OWNER_GROUP;
2151 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2152 server->caps |= NFS_CAP_ATIME;
2153 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2154 server->caps |= NFS_CAP_CTIME;
2155 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2156 server->caps |= NFS_CAP_MTIME;
2158 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2159 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2160 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2161 server->acl_bitmask = res.acl_bitmask;
2164 return status;
2167 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2169 struct nfs4_exception exception = { };
2170 int err;
2171 do {
2172 err = nfs4_handle_exception(server,
2173 _nfs4_server_capabilities(server, fhandle),
2174 &exception);
2175 } while (exception.retry);
2176 return err;
2179 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2180 struct nfs_fsinfo *info)
2182 struct nfs4_lookup_root_arg args = {
2183 .bitmask = nfs4_fattr_bitmap,
2185 struct nfs4_lookup_res res = {
2186 .server = server,
2187 .fattr = info->fattr,
2188 .fh = fhandle,
2190 struct rpc_message msg = {
2191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2192 .rpc_argp = &args,
2193 .rpc_resp = &res,
2196 nfs_fattr_init(info->fattr);
2197 return nfs4_call_sync(server, &msg, &args, &res, 0);
2200 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2201 struct nfs_fsinfo *info)
2203 struct nfs4_exception exception = { };
2204 int err;
2205 do {
2206 err = nfs4_handle_exception(server,
2207 _nfs4_lookup_root(server, fhandle, info),
2208 &exception);
2209 } while (exception.retry);
2210 return err;
2214 * get the file handle for the "/" directory on the server
2216 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2217 struct nfs_fsinfo *info)
2219 int status;
2221 status = nfs4_lookup_root(server, fhandle, info);
2222 if (status == 0)
2223 status = nfs4_server_capabilities(server, fhandle);
2224 if (status == 0)
2225 status = nfs4_do_fsinfo(server, fhandle, info);
2226 return nfs4_map_errors(status);
2230 * Get locations and (maybe) other attributes of a referral.
2231 * Note that we'll actually follow the referral later when
2232 * we detect fsid mismatch in inode revalidation
2234 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2236 int status = -ENOMEM;
2237 struct page *page = NULL;
2238 struct nfs4_fs_locations *locations = NULL;
2240 page = alloc_page(GFP_KERNEL);
2241 if (page == NULL)
2242 goto out;
2243 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2244 if (locations == NULL)
2245 goto out;
2247 status = nfs4_proc_fs_locations(dir, name, locations, page);
2248 if (status != 0)
2249 goto out;
2250 /* Make sure server returned a different fsid for the referral */
2251 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2252 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2253 status = -EIO;
2254 goto out;
2257 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2258 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2259 if (!fattr->mode)
2260 fattr->mode = S_IFDIR;
2261 memset(fhandle, 0, sizeof(struct nfs_fh));
2262 out:
2263 if (page)
2264 __free_page(page);
2265 if (locations)
2266 kfree(locations);
2267 return status;
2270 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2272 struct nfs4_getattr_arg args = {
2273 .fh = fhandle,
2274 .bitmask = server->attr_bitmask,
2276 struct nfs4_getattr_res res = {
2277 .fattr = fattr,
2278 .server = server,
2280 struct rpc_message msg = {
2281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2282 .rpc_argp = &args,
2283 .rpc_resp = &res,
2286 nfs_fattr_init(fattr);
2287 return nfs4_call_sync(server, &msg, &args, &res, 0);
2290 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2292 struct nfs4_exception exception = { };
2293 int err;
2294 do {
2295 err = nfs4_handle_exception(server,
2296 _nfs4_proc_getattr(server, fhandle, fattr),
2297 &exception);
2298 } while (exception.retry);
2299 return err;
2303 * The file is not closed if it is opened due to the a request to change
2304 * the size of the file. The open call will not be needed once the
2305 * VFS layer lookup-intents are implemented.
2307 * Close is called when the inode is destroyed.
2308 * If we haven't opened the file for O_WRONLY, we
2309 * need to in the size_change case to obtain a stateid.
2311 * Got race?
2312 * Because OPEN is always done by name in nfsv4, it is
2313 * possible that we opened a different file by the same
2314 * name. We can recognize this race condition, but we
2315 * can't do anything about it besides returning an error.
2317 * This will be fixed with VFS changes (lookup-intent).
2319 static int
2320 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2321 struct iattr *sattr)
2323 struct inode *inode = dentry->d_inode;
2324 struct rpc_cred *cred = NULL;
2325 struct nfs4_state *state = NULL;
2326 int status;
2328 nfs_fattr_init(fattr);
2330 /* Search for an existing open(O_WRITE) file */
2331 if (sattr->ia_valid & ATTR_FILE) {
2332 struct nfs_open_context *ctx;
2334 ctx = nfs_file_open_context(sattr->ia_file);
2335 if (ctx) {
2336 cred = ctx->cred;
2337 state = ctx->state;
2341 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2342 if (status == 0)
2343 nfs_setattr_update_inode(inode, sattr);
2344 return status;
2347 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2348 const struct qstr *name, struct nfs_fh *fhandle,
2349 struct nfs_fattr *fattr)
2351 int status;
2352 struct nfs4_lookup_arg args = {
2353 .bitmask = server->attr_bitmask,
2354 .dir_fh = dirfh,
2355 .name = name,
2357 struct nfs4_lookup_res res = {
2358 .server = server,
2359 .fattr = fattr,
2360 .fh = fhandle,
2362 struct rpc_message msg = {
2363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2364 .rpc_argp = &args,
2365 .rpc_resp = &res,
2368 nfs_fattr_init(fattr);
2370 dprintk("NFS call lookupfh %s\n", name->name);
2371 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2372 dprintk("NFS reply lookupfh: %d\n", status);
2373 return status;
2376 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2377 struct qstr *name, struct nfs_fh *fhandle,
2378 struct nfs_fattr *fattr)
2380 struct nfs4_exception exception = { };
2381 int err;
2382 do {
2383 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2384 /* FIXME: !!!! */
2385 if (err == -NFS4ERR_MOVED) {
2386 err = -EREMOTE;
2387 break;
2389 err = nfs4_handle_exception(server, err, &exception);
2390 } while (exception.retry);
2391 return err;
2394 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2395 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2397 int status;
2399 dprintk("NFS call lookup %s\n", name->name);
2400 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2401 if (status == -NFS4ERR_MOVED)
2402 status = nfs4_get_referral(dir, name, fattr, fhandle);
2403 dprintk("NFS reply lookup: %d\n", status);
2404 return status;
2407 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2409 struct nfs4_exception exception = { };
2410 int err;
2411 do {
2412 err = nfs4_handle_exception(NFS_SERVER(dir),
2413 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2414 &exception);
2415 } while (exception.retry);
2416 return err;
2419 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2421 struct nfs_server *server = NFS_SERVER(inode);
2422 struct nfs4_accessargs args = {
2423 .fh = NFS_FH(inode),
2424 .bitmask = server->attr_bitmask,
2426 struct nfs4_accessres res = {
2427 .server = server,
2429 struct rpc_message msg = {
2430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2431 .rpc_argp = &args,
2432 .rpc_resp = &res,
2433 .rpc_cred = entry->cred,
2435 int mode = entry->mask;
2436 int status;
2439 * Determine which access bits we want to ask for...
2441 if (mode & MAY_READ)
2442 args.access |= NFS4_ACCESS_READ;
2443 if (S_ISDIR(inode->i_mode)) {
2444 if (mode & MAY_WRITE)
2445 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2446 if (mode & MAY_EXEC)
2447 args.access |= NFS4_ACCESS_LOOKUP;
2448 } else {
2449 if (mode & MAY_WRITE)
2450 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2451 if (mode & MAY_EXEC)
2452 args.access |= NFS4_ACCESS_EXECUTE;
2455 res.fattr = nfs_alloc_fattr();
2456 if (res.fattr == NULL)
2457 return -ENOMEM;
2459 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2460 if (!status) {
2461 entry->mask = 0;
2462 if (res.access & NFS4_ACCESS_READ)
2463 entry->mask |= MAY_READ;
2464 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2465 entry->mask |= MAY_WRITE;
2466 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2467 entry->mask |= MAY_EXEC;
2468 nfs_refresh_inode(inode, res.fattr);
2470 nfs_free_fattr(res.fattr);
2471 return status;
2474 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2476 struct nfs4_exception exception = { };
2477 int err;
2478 do {
2479 err = nfs4_handle_exception(NFS_SERVER(inode),
2480 _nfs4_proc_access(inode, entry),
2481 &exception);
2482 } while (exception.retry);
2483 return err;
2487 * TODO: For the time being, we don't try to get any attributes
2488 * along with any of the zero-copy operations READ, READDIR,
2489 * READLINK, WRITE.
2491 * In the case of the first three, we want to put the GETATTR
2492 * after the read-type operation -- this is because it is hard
2493 * to predict the length of a GETATTR response in v4, and thus
2494 * align the READ data correctly. This means that the GETATTR
2495 * may end up partially falling into the page cache, and we should
2496 * shift it into the 'tail' of the xdr_buf before processing.
2497 * To do this efficiently, we need to know the total length
2498 * of data received, which doesn't seem to be available outside
2499 * of the RPC layer.
2501 * In the case of WRITE, we also want to put the GETATTR after
2502 * the operation -- in this case because we want to make sure
2503 * we get the post-operation mtime and size. This means that
2504 * we can't use xdr_encode_pages() as written: we need a variant
2505 * of it which would leave room in the 'tail' iovec.
2507 * Both of these changes to the XDR layer would in fact be quite
2508 * minor, but I decided to leave them for a subsequent patch.
2510 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2511 unsigned int pgbase, unsigned int pglen)
2513 struct nfs4_readlink args = {
2514 .fh = NFS_FH(inode),
2515 .pgbase = pgbase,
2516 .pglen = pglen,
2517 .pages = &page,
2519 struct nfs4_readlink_res res;
2520 struct rpc_message msg = {
2521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2522 .rpc_argp = &args,
2523 .rpc_resp = &res,
2526 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2529 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2530 unsigned int pgbase, unsigned int pglen)
2532 struct nfs4_exception exception = { };
2533 int err;
2534 do {
2535 err = nfs4_handle_exception(NFS_SERVER(inode),
2536 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2537 &exception);
2538 } while (exception.retry);
2539 return err;
2543 * Got race?
2544 * We will need to arrange for the VFS layer to provide an atomic open.
2545 * Until then, this create/open method is prone to inefficiency and race
2546 * conditions due to the lookup, create, and open VFS calls from sys_open()
2547 * placed on the wire.
2549 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2550 * The file will be opened again in the subsequent VFS open call
2551 * (nfs4_proc_file_open).
2553 * The open for read will just hang around to be used by any process that
2554 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2557 static int
2558 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2559 int flags, struct nameidata *nd)
2561 struct path path = {
2562 .mnt = nd->path.mnt,
2563 .dentry = dentry,
2565 struct nfs4_state *state;
2566 struct rpc_cred *cred;
2567 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2568 int status = 0;
2570 cred = rpc_lookup_cred();
2571 if (IS_ERR(cred)) {
2572 status = PTR_ERR(cred);
2573 goto out;
2575 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2576 d_drop(dentry);
2577 if (IS_ERR(state)) {
2578 status = PTR_ERR(state);
2579 goto out_putcred;
2581 d_add(dentry, igrab(state->inode));
2582 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2583 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2584 status = nfs4_intent_set_file(nd, &path, state, fmode);
2585 else
2586 nfs4_close_sync(&path, state, fmode);
2587 out_putcred:
2588 put_rpccred(cred);
2589 out:
2590 return status;
2593 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2595 struct nfs_server *server = NFS_SERVER(dir);
2596 struct nfs_removeargs args = {
2597 .fh = NFS_FH(dir),
2598 .name.len = name->len,
2599 .name.name = name->name,
2600 .bitmask = server->attr_bitmask,
2602 struct nfs_removeres res = {
2603 .server = server,
2605 struct rpc_message msg = {
2606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2607 .rpc_argp = &args,
2608 .rpc_resp = &res,
2610 int status = -ENOMEM;
2612 res.dir_attr = nfs_alloc_fattr();
2613 if (res.dir_attr == NULL)
2614 goto out;
2616 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2617 if (status == 0) {
2618 update_changeattr(dir, &res.cinfo);
2619 nfs_post_op_update_inode(dir, res.dir_attr);
2621 nfs_free_fattr(res.dir_attr);
2622 out:
2623 return status;
2626 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2628 struct nfs4_exception exception = { };
2629 int err;
2630 do {
2631 err = nfs4_handle_exception(NFS_SERVER(dir),
2632 _nfs4_proc_remove(dir, name),
2633 &exception);
2634 } while (exception.retry);
2635 return err;
2638 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2640 struct nfs_server *server = NFS_SERVER(dir);
2641 struct nfs_removeargs *args = msg->rpc_argp;
2642 struct nfs_removeres *res = msg->rpc_resp;
2644 args->bitmask = server->cache_consistency_bitmask;
2645 res->server = server;
2646 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2649 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2651 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2653 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2654 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2655 return 0;
2656 update_changeattr(dir, &res->cinfo);
2657 nfs_post_op_update_inode(dir, res->dir_attr);
2658 return 1;
2661 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2662 struct inode *new_dir, struct qstr *new_name)
2664 struct nfs_server *server = NFS_SERVER(old_dir);
2665 struct nfs4_rename_arg arg = {
2666 .old_dir = NFS_FH(old_dir),
2667 .new_dir = NFS_FH(new_dir),
2668 .old_name = old_name,
2669 .new_name = new_name,
2670 .bitmask = server->attr_bitmask,
2672 struct nfs4_rename_res res = {
2673 .server = server,
2675 struct rpc_message msg = {
2676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2677 .rpc_argp = &arg,
2678 .rpc_resp = &res,
2680 int status = -ENOMEM;
2682 res.old_fattr = nfs_alloc_fattr();
2683 res.new_fattr = nfs_alloc_fattr();
2684 if (res.old_fattr == NULL || res.new_fattr == NULL)
2685 goto out;
2687 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2688 if (!status) {
2689 update_changeattr(old_dir, &res.old_cinfo);
2690 nfs_post_op_update_inode(old_dir, res.old_fattr);
2691 update_changeattr(new_dir, &res.new_cinfo);
2692 nfs_post_op_update_inode(new_dir, res.new_fattr);
2694 out:
2695 nfs_free_fattr(res.new_fattr);
2696 nfs_free_fattr(res.old_fattr);
2697 return status;
2700 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2701 struct inode *new_dir, struct qstr *new_name)
2703 struct nfs4_exception exception = { };
2704 int err;
2705 do {
2706 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2707 _nfs4_proc_rename(old_dir, old_name,
2708 new_dir, new_name),
2709 &exception);
2710 } while (exception.retry);
2711 return err;
2714 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2716 struct nfs_server *server = NFS_SERVER(inode);
2717 struct nfs4_link_arg arg = {
2718 .fh = NFS_FH(inode),
2719 .dir_fh = NFS_FH(dir),
2720 .name = name,
2721 .bitmask = server->attr_bitmask,
2723 struct nfs4_link_res res = {
2724 .server = server,
2726 struct rpc_message msg = {
2727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2728 .rpc_argp = &arg,
2729 .rpc_resp = &res,
2731 int status = -ENOMEM;
2733 res.fattr = nfs_alloc_fattr();
2734 res.dir_attr = nfs_alloc_fattr();
2735 if (res.fattr == NULL || res.dir_attr == NULL)
2736 goto out;
2738 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2739 if (!status) {
2740 update_changeattr(dir, &res.cinfo);
2741 nfs_post_op_update_inode(dir, res.dir_attr);
2742 nfs_post_op_update_inode(inode, res.fattr);
2744 out:
2745 nfs_free_fattr(res.dir_attr);
2746 nfs_free_fattr(res.fattr);
2747 return status;
2750 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2752 struct nfs4_exception exception = { };
2753 int err;
2754 do {
2755 err = nfs4_handle_exception(NFS_SERVER(inode),
2756 _nfs4_proc_link(inode, dir, name),
2757 &exception);
2758 } while (exception.retry);
2759 return err;
2762 struct nfs4_createdata {
2763 struct rpc_message msg;
2764 struct nfs4_create_arg arg;
2765 struct nfs4_create_res res;
2766 struct nfs_fh fh;
2767 struct nfs_fattr fattr;
2768 struct nfs_fattr dir_fattr;
2771 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2772 struct qstr *name, struct iattr *sattr, u32 ftype)
2774 struct nfs4_createdata *data;
2776 data = kzalloc(sizeof(*data), GFP_KERNEL);
2777 if (data != NULL) {
2778 struct nfs_server *server = NFS_SERVER(dir);
2780 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2781 data->msg.rpc_argp = &data->arg;
2782 data->msg.rpc_resp = &data->res;
2783 data->arg.dir_fh = NFS_FH(dir);
2784 data->arg.server = server;
2785 data->arg.name = name;
2786 data->arg.attrs = sattr;
2787 data->arg.ftype = ftype;
2788 data->arg.bitmask = server->attr_bitmask;
2789 data->res.server = server;
2790 data->res.fh = &data->fh;
2791 data->res.fattr = &data->fattr;
2792 data->res.dir_fattr = &data->dir_fattr;
2793 nfs_fattr_init(data->res.fattr);
2794 nfs_fattr_init(data->res.dir_fattr);
2796 return data;
2799 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2801 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2802 &data->arg, &data->res, 1);
2803 if (status == 0) {
2804 update_changeattr(dir, &data->res.dir_cinfo);
2805 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2806 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2808 return status;
2811 static void nfs4_free_createdata(struct nfs4_createdata *data)
2813 kfree(data);
2816 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2817 struct page *page, unsigned int len, struct iattr *sattr)
2819 struct nfs4_createdata *data;
2820 int status = -ENAMETOOLONG;
2822 if (len > NFS4_MAXPATHLEN)
2823 goto out;
2825 status = -ENOMEM;
2826 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2827 if (data == NULL)
2828 goto out;
2830 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2831 data->arg.u.symlink.pages = &page;
2832 data->arg.u.symlink.len = len;
2834 status = nfs4_do_create(dir, dentry, data);
2836 nfs4_free_createdata(data);
2837 out:
2838 return status;
2841 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2842 struct page *page, unsigned int len, struct iattr *sattr)
2844 struct nfs4_exception exception = { };
2845 int err;
2846 do {
2847 err = nfs4_handle_exception(NFS_SERVER(dir),
2848 _nfs4_proc_symlink(dir, dentry, page,
2849 len, sattr),
2850 &exception);
2851 } while (exception.retry);
2852 return err;
2855 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2856 struct iattr *sattr)
2858 struct nfs4_createdata *data;
2859 int status = -ENOMEM;
2861 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2862 if (data == NULL)
2863 goto out;
2865 status = nfs4_do_create(dir, dentry, data);
2867 nfs4_free_createdata(data);
2868 out:
2869 return status;
2872 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2873 struct iattr *sattr)
2875 struct nfs4_exception exception = { };
2876 int err;
2877 do {
2878 err = nfs4_handle_exception(NFS_SERVER(dir),
2879 _nfs4_proc_mkdir(dir, dentry, sattr),
2880 &exception);
2881 } while (exception.retry);
2882 return err;
2885 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2886 u64 cookie, struct page *page, unsigned int count, int plus)
2888 struct inode *dir = dentry->d_inode;
2889 struct nfs4_readdir_arg args = {
2890 .fh = NFS_FH(dir),
2891 .pages = &page,
2892 .pgbase = 0,
2893 .count = count,
2894 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2896 struct nfs4_readdir_res res;
2897 struct rpc_message msg = {
2898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2899 .rpc_argp = &args,
2900 .rpc_resp = &res,
2901 .rpc_cred = cred,
2903 int status;
2905 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2906 dentry->d_parent->d_name.name,
2907 dentry->d_name.name,
2908 (unsigned long long)cookie);
2909 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2910 res.pgbase = args.pgbase;
2911 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2912 if (status == 0)
2913 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2915 nfs_invalidate_atime(dir);
2917 dprintk("%s: returns %d\n", __func__, status);
2918 return status;
2921 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2922 u64 cookie, struct page *page, unsigned int count, int plus)
2924 struct nfs4_exception exception = { };
2925 int err;
2926 do {
2927 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2928 _nfs4_proc_readdir(dentry, cred, cookie,
2929 page, count, plus),
2930 &exception);
2931 } while (exception.retry);
2932 return err;
2935 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2936 struct iattr *sattr, dev_t rdev)
2938 struct nfs4_createdata *data;
2939 int mode = sattr->ia_mode;
2940 int status = -ENOMEM;
2942 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2943 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2945 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2946 if (data == NULL)
2947 goto out;
2949 if (S_ISFIFO(mode))
2950 data->arg.ftype = NF4FIFO;
2951 else if (S_ISBLK(mode)) {
2952 data->arg.ftype = NF4BLK;
2953 data->arg.u.device.specdata1 = MAJOR(rdev);
2954 data->arg.u.device.specdata2 = MINOR(rdev);
2956 else if (S_ISCHR(mode)) {
2957 data->arg.ftype = NF4CHR;
2958 data->arg.u.device.specdata1 = MAJOR(rdev);
2959 data->arg.u.device.specdata2 = MINOR(rdev);
2962 status = nfs4_do_create(dir, dentry, data);
2964 nfs4_free_createdata(data);
2965 out:
2966 return status;
2969 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2970 struct iattr *sattr, dev_t rdev)
2972 struct nfs4_exception exception = { };
2973 int err;
2974 do {
2975 err = nfs4_handle_exception(NFS_SERVER(dir),
2976 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2977 &exception);
2978 } while (exception.retry);
2979 return err;
2982 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2983 struct nfs_fsstat *fsstat)
2985 struct nfs4_statfs_arg args = {
2986 .fh = fhandle,
2987 .bitmask = server->attr_bitmask,
2989 struct nfs4_statfs_res res = {
2990 .fsstat = fsstat,
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2994 .rpc_argp = &args,
2995 .rpc_resp = &res,
2998 nfs_fattr_init(fsstat->fattr);
2999 return nfs4_call_sync(server, &msg, &args, &res, 0);
3002 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3004 struct nfs4_exception exception = { };
3005 int err;
3006 do {
3007 err = nfs4_handle_exception(server,
3008 _nfs4_proc_statfs(server, fhandle, fsstat),
3009 &exception);
3010 } while (exception.retry);
3011 return err;
3014 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3015 struct nfs_fsinfo *fsinfo)
3017 struct nfs4_fsinfo_arg args = {
3018 .fh = fhandle,
3019 .bitmask = server->attr_bitmask,
3021 struct nfs4_fsinfo_res res = {
3022 .fsinfo = fsinfo,
3024 struct rpc_message msg = {
3025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3026 .rpc_argp = &args,
3027 .rpc_resp = &res,
3030 return nfs4_call_sync(server, &msg, &args, &res, 0);
3033 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3035 struct nfs4_exception exception = { };
3036 int err;
3038 do {
3039 err = nfs4_handle_exception(server,
3040 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3041 &exception);
3042 } while (exception.retry);
3043 return err;
3046 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3048 nfs_fattr_init(fsinfo->fattr);
3049 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3052 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3053 struct nfs_pathconf *pathconf)
3055 struct nfs4_pathconf_arg args = {
3056 .fh = fhandle,
3057 .bitmask = server->attr_bitmask,
3059 struct nfs4_pathconf_res res = {
3060 .pathconf = pathconf,
3062 struct rpc_message msg = {
3063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3064 .rpc_argp = &args,
3065 .rpc_resp = &res,
3068 /* None of the pathconf attributes are mandatory to implement */
3069 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3070 memset(pathconf, 0, sizeof(*pathconf));
3071 return 0;
3074 nfs_fattr_init(pathconf->fattr);
3075 return nfs4_call_sync(server, &msg, &args, &res, 0);
3078 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3079 struct nfs_pathconf *pathconf)
3081 struct nfs4_exception exception = { };
3082 int err;
3084 do {
3085 err = nfs4_handle_exception(server,
3086 _nfs4_proc_pathconf(server, fhandle, pathconf),
3087 &exception);
3088 } while (exception.retry);
3089 return err;
3092 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3094 struct nfs_server *server = NFS_SERVER(data->inode);
3096 dprintk("--> %s\n", __func__);
3098 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3100 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3101 nfs_restart_rpc(task, server->nfs_client);
3102 return -EAGAIN;
3105 nfs_invalidate_atime(data->inode);
3106 if (task->tk_status > 0)
3107 renew_lease(server, data->timestamp);
3108 return 0;
3111 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3113 data->timestamp = jiffies;
3114 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3117 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3119 struct inode *inode = data->inode;
3121 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3122 task->tk_status);
3124 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3125 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3126 return -EAGAIN;
3128 if (task->tk_status >= 0) {
3129 renew_lease(NFS_SERVER(inode), data->timestamp);
3130 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3132 return 0;
3135 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3137 struct nfs_server *server = NFS_SERVER(data->inode);
3139 data->args.bitmask = server->cache_consistency_bitmask;
3140 data->res.server = server;
3141 data->timestamp = jiffies;
3143 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3146 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3148 struct inode *inode = data->inode;
3150 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3151 task->tk_status);
3152 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3153 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3154 return -EAGAIN;
3156 nfs_refresh_inode(inode, data->res.fattr);
3157 return 0;
3160 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3162 struct nfs_server *server = NFS_SERVER(data->inode);
3164 data->args.bitmask = server->cache_consistency_bitmask;
3165 data->res.server = server;
3166 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3169 struct nfs4_renewdata {
3170 struct nfs_client *client;
3171 unsigned long timestamp;
3175 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3176 * standalone procedure for queueing an asynchronous RENEW.
3178 static void nfs4_renew_release(void *calldata)
3180 struct nfs4_renewdata *data = calldata;
3181 struct nfs_client *clp = data->client;
3183 if (atomic_read(&clp->cl_count) > 1)
3184 nfs4_schedule_state_renewal(clp);
3185 nfs_put_client(clp);
3186 kfree(data);
3189 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3191 struct nfs4_renewdata *data = calldata;
3192 struct nfs_client *clp = data->client;
3193 unsigned long timestamp = data->timestamp;
3195 if (task->tk_status < 0) {
3196 /* Unless we're shutting down, schedule state recovery! */
3197 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3198 nfs4_schedule_state_recovery(clp);
3199 return;
3201 spin_lock(&clp->cl_lock);
3202 if (time_before(clp->cl_last_renewal,timestamp))
3203 clp->cl_last_renewal = timestamp;
3204 spin_unlock(&clp->cl_lock);
3207 static const struct rpc_call_ops nfs4_renew_ops = {
3208 .rpc_call_done = nfs4_renew_done,
3209 .rpc_release = nfs4_renew_release,
3212 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3214 struct rpc_message msg = {
3215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3216 .rpc_argp = clp,
3217 .rpc_cred = cred,
3219 struct nfs4_renewdata *data;
3221 if (!atomic_inc_not_zero(&clp->cl_count))
3222 return -EIO;
3223 data = kmalloc(sizeof(*data), GFP_KERNEL);
3224 if (data == NULL)
3225 return -ENOMEM;
3226 data->client = clp;
3227 data->timestamp = jiffies;
3228 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3229 &nfs4_renew_ops, data);
3232 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3234 struct rpc_message msg = {
3235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3236 .rpc_argp = clp,
3237 .rpc_cred = cred,
3239 unsigned long now = jiffies;
3240 int status;
3242 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3243 if (status < 0)
3244 return status;
3245 spin_lock(&clp->cl_lock);
3246 if (time_before(clp->cl_last_renewal,now))
3247 clp->cl_last_renewal = now;
3248 spin_unlock(&clp->cl_lock);
3249 return 0;
3252 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3254 return (server->caps & NFS_CAP_ACLS)
3255 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3256 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3259 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3260 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3261 * the stack.
3263 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3265 static void buf_to_pages(const void *buf, size_t buflen,
3266 struct page **pages, unsigned int *pgbase)
3268 const void *p = buf;
3270 *pgbase = offset_in_page(buf);
3271 p -= *pgbase;
3272 while (p < buf + buflen) {
3273 *(pages++) = virt_to_page(p);
3274 p += PAGE_CACHE_SIZE;
3278 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3279 struct page **pages, unsigned int *pgbase)
3281 struct page *newpage, **spages;
3282 int rc = 0;
3283 size_t len;
3284 spages = pages;
3286 do {
3287 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3288 newpage = alloc_page(GFP_KERNEL);
3290 if (newpage == NULL)
3291 goto unwind;
3292 memcpy(page_address(newpage), buf, len);
3293 buf += len;
3294 buflen -= len;
3295 *pages++ = newpage;
3296 rc++;
3297 } while (buflen != 0);
3299 return rc;
3301 unwind:
3302 for(; rc > 0; rc--)
3303 __free_page(spages[rc-1]);
3304 return -ENOMEM;
3307 struct nfs4_cached_acl {
3308 int cached;
3309 size_t len;
3310 char data[0];
3313 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3315 struct nfs_inode *nfsi = NFS_I(inode);
3317 spin_lock(&inode->i_lock);
3318 kfree(nfsi->nfs4_acl);
3319 nfsi->nfs4_acl = acl;
3320 spin_unlock(&inode->i_lock);
3323 static void nfs4_zap_acl_attr(struct inode *inode)
3325 nfs4_set_cached_acl(inode, NULL);
3328 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3330 struct nfs_inode *nfsi = NFS_I(inode);
3331 struct nfs4_cached_acl *acl;
3332 int ret = -ENOENT;
3334 spin_lock(&inode->i_lock);
3335 acl = nfsi->nfs4_acl;
3336 if (acl == NULL)
3337 goto out;
3338 if (buf == NULL) /* user is just asking for length */
3339 goto out_len;
3340 if (acl->cached == 0)
3341 goto out;
3342 ret = -ERANGE; /* see getxattr(2) man page */
3343 if (acl->len > buflen)
3344 goto out;
3345 memcpy(buf, acl->data, acl->len);
3346 out_len:
3347 ret = acl->len;
3348 out:
3349 spin_unlock(&inode->i_lock);
3350 return ret;
3353 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3355 struct nfs4_cached_acl *acl;
3357 if (buf && acl_len <= PAGE_SIZE) {
3358 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3359 if (acl == NULL)
3360 goto out;
3361 acl->cached = 1;
3362 memcpy(acl->data, buf, acl_len);
3363 } else {
3364 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3365 if (acl == NULL)
3366 goto out;
3367 acl->cached = 0;
3369 acl->len = acl_len;
3370 out:
3371 nfs4_set_cached_acl(inode, acl);
3374 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3376 struct page *pages[NFS4ACL_MAXPAGES];
3377 struct nfs_getaclargs args = {
3378 .fh = NFS_FH(inode),
3379 .acl_pages = pages,
3380 .acl_len = buflen,
3382 struct nfs_getaclres res = {
3383 .acl_len = buflen,
3385 void *resp_buf;
3386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3388 .rpc_argp = &args,
3389 .rpc_resp = &res,
3391 struct page *localpage = NULL;
3392 int ret;
3394 if (buflen < PAGE_SIZE) {
3395 /* As long as we're doing a round trip to the server anyway,
3396 * let's be prepared for a page of acl data. */
3397 localpage = alloc_page(GFP_KERNEL);
3398 resp_buf = page_address(localpage);
3399 if (localpage == NULL)
3400 return -ENOMEM;
3401 args.acl_pages[0] = localpage;
3402 args.acl_pgbase = 0;
3403 args.acl_len = PAGE_SIZE;
3404 } else {
3405 resp_buf = buf;
3406 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3408 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3409 if (ret)
3410 goto out_free;
3411 if (res.acl_len > args.acl_len)
3412 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3413 else
3414 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3415 if (buf) {
3416 ret = -ERANGE;
3417 if (res.acl_len > buflen)
3418 goto out_free;
3419 if (localpage)
3420 memcpy(buf, resp_buf, res.acl_len);
3422 ret = res.acl_len;
3423 out_free:
3424 if (localpage)
3425 __free_page(localpage);
3426 return ret;
3429 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3431 struct nfs4_exception exception = { };
3432 ssize_t ret;
3433 do {
3434 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3435 if (ret >= 0)
3436 break;
3437 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3438 } while (exception.retry);
3439 return ret;
3442 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3444 struct nfs_server *server = NFS_SERVER(inode);
3445 int ret;
3447 if (!nfs4_server_supports_acls(server))
3448 return -EOPNOTSUPP;
3449 ret = nfs_revalidate_inode(server, inode);
3450 if (ret < 0)
3451 return ret;
3452 ret = nfs4_read_cached_acl(inode, buf, buflen);
3453 if (ret != -ENOENT)
3454 return ret;
3455 return nfs4_get_acl_uncached(inode, buf, buflen);
3458 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3460 struct nfs_server *server = NFS_SERVER(inode);
3461 struct page *pages[NFS4ACL_MAXPAGES];
3462 struct nfs_setaclargs arg = {
3463 .fh = NFS_FH(inode),
3464 .acl_pages = pages,
3465 .acl_len = buflen,
3467 struct nfs_setaclres res;
3468 struct rpc_message msg = {
3469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3470 .rpc_argp = &arg,
3471 .rpc_resp = &res,
3473 int ret, i;
3475 if (!nfs4_server_supports_acls(server))
3476 return -EOPNOTSUPP;
3477 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3478 if (i < 0)
3479 return i;
3480 nfs_inode_return_delegation(inode);
3481 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3484 * Free each page after tx, so the only ref left is
3485 * held by the network stack
3487 for (; i > 0; i--)
3488 put_page(pages[i-1]);
3490 nfs_access_zap_cache(inode);
3491 nfs_zap_acl_cache(inode);
3492 return ret;
3495 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3497 struct nfs4_exception exception = { };
3498 int err;
3499 do {
3500 err = nfs4_handle_exception(NFS_SERVER(inode),
3501 __nfs4_proc_set_acl(inode, buf, buflen),
3502 &exception);
3503 } while (exception.retry);
3504 return err;
3507 static int
3508 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3510 if (!clp || task->tk_status >= 0)
3511 return 0;
3512 switch(task->tk_status) {
3513 case -NFS4ERR_ADMIN_REVOKED:
3514 case -NFS4ERR_BAD_STATEID:
3515 case -NFS4ERR_OPENMODE:
3516 if (state == NULL)
3517 break;
3518 nfs4_state_mark_reclaim_nograce(clp, state);
3519 goto do_state_recovery;
3520 case -NFS4ERR_STALE_STATEID:
3521 case -NFS4ERR_STALE_CLIENTID:
3522 case -NFS4ERR_EXPIRED:
3523 goto do_state_recovery;
3524 #if defined(CONFIG_NFS_V4_1)
3525 case -NFS4ERR_BADSESSION:
3526 case -NFS4ERR_BADSLOT:
3527 case -NFS4ERR_BAD_HIGH_SLOT:
3528 case -NFS4ERR_DEADSESSION:
3529 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3530 case -NFS4ERR_SEQ_FALSE_RETRY:
3531 case -NFS4ERR_SEQ_MISORDERED:
3532 dprintk("%s ERROR %d, Reset session\n", __func__,
3533 task->tk_status);
3534 nfs4_schedule_state_recovery(clp);
3535 task->tk_status = 0;
3536 return -EAGAIN;
3537 #endif /* CONFIG_NFS_V4_1 */
3538 case -NFS4ERR_DELAY:
3539 if (server)
3540 nfs_inc_server_stats(server, NFSIOS_DELAY);
3541 case -NFS4ERR_GRACE:
3542 case -EKEYEXPIRED:
3543 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3544 task->tk_status = 0;
3545 return -EAGAIN;
3546 case -NFS4ERR_OLD_STATEID:
3547 task->tk_status = 0;
3548 return -EAGAIN;
3550 task->tk_status = nfs4_map_errors(task->tk_status);
3551 return 0;
3552 do_state_recovery:
3553 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3554 nfs4_schedule_state_recovery(clp);
3555 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3556 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3557 task->tk_status = 0;
3558 return -EAGAIN;
3561 static int
3562 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3564 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3567 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3568 unsigned short port, struct rpc_cred *cred,
3569 struct nfs4_setclientid_res *res)
3571 nfs4_verifier sc_verifier;
3572 struct nfs4_setclientid setclientid = {
3573 .sc_verifier = &sc_verifier,
3574 .sc_prog = program,
3576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3578 .rpc_argp = &setclientid,
3579 .rpc_resp = res,
3580 .rpc_cred = cred,
3582 __be32 *p;
3583 int loop = 0;
3584 int status;
3586 p = (__be32*)sc_verifier.data;
3587 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3588 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3590 for(;;) {
3591 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3592 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3593 clp->cl_ipaddr,
3594 rpc_peeraddr2str(clp->cl_rpcclient,
3595 RPC_DISPLAY_ADDR),
3596 rpc_peeraddr2str(clp->cl_rpcclient,
3597 RPC_DISPLAY_PROTO),
3598 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3599 clp->cl_id_uniquifier);
3600 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3601 sizeof(setclientid.sc_netid),
3602 rpc_peeraddr2str(clp->cl_rpcclient,
3603 RPC_DISPLAY_NETID));
3604 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3605 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3606 clp->cl_ipaddr, port >> 8, port & 255);
3608 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3609 if (status != -NFS4ERR_CLID_INUSE)
3610 break;
3611 if (signalled())
3612 break;
3613 if (loop++ & 1)
3614 ssleep(clp->cl_lease_time + 1);
3615 else
3616 if (++clp->cl_id_uniquifier == 0)
3617 break;
3619 return status;
3622 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3623 struct nfs4_setclientid_res *arg,
3624 struct rpc_cred *cred)
3626 struct nfs_fsinfo fsinfo;
3627 struct rpc_message msg = {
3628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3629 .rpc_argp = arg,
3630 .rpc_resp = &fsinfo,
3631 .rpc_cred = cred,
3633 unsigned long now;
3634 int status;
3636 now = jiffies;
3637 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3638 if (status == 0) {
3639 spin_lock(&clp->cl_lock);
3640 clp->cl_lease_time = fsinfo.lease_time * HZ;
3641 clp->cl_last_renewal = now;
3642 spin_unlock(&clp->cl_lock);
3644 return status;
3647 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3648 struct nfs4_setclientid_res *arg,
3649 struct rpc_cred *cred)
3651 long timeout = 0;
3652 int err;
3653 do {
3654 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3655 switch (err) {
3656 case 0:
3657 return err;
3658 case -NFS4ERR_RESOURCE:
3659 /* The IBM lawyers misread another document! */
3660 case -NFS4ERR_DELAY:
3661 case -EKEYEXPIRED:
3662 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3664 } while (err == 0);
3665 return err;
3668 struct nfs4_delegreturndata {
3669 struct nfs4_delegreturnargs args;
3670 struct nfs4_delegreturnres res;
3671 struct nfs_fh fh;
3672 nfs4_stateid stateid;
3673 unsigned long timestamp;
3674 struct nfs_fattr fattr;
3675 int rpc_status;
3678 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3680 struct nfs4_delegreturndata *data = calldata;
3682 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3683 task->tk_status);
3685 switch (task->tk_status) {
3686 case -NFS4ERR_STALE_STATEID:
3687 case -NFS4ERR_EXPIRED:
3688 case 0:
3689 renew_lease(data->res.server, data->timestamp);
3690 break;
3691 default:
3692 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3693 -EAGAIN) {
3694 nfs_restart_rpc(task, data->res.server->nfs_client);
3695 return;
3698 data->rpc_status = task->tk_status;
3701 static void nfs4_delegreturn_release(void *calldata)
3703 kfree(calldata);
3706 #if defined(CONFIG_NFS_V4_1)
3707 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3709 struct nfs4_delegreturndata *d_data;
3711 d_data = (struct nfs4_delegreturndata *)data;
3713 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3714 &d_data->args.seq_args,
3715 &d_data->res.seq_res, 1, task))
3716 return;
3717 rpc_call_start(task);
3719 #endif /* CONFIG_NFS_V4_1 */
3721 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3722 #if defined(CONFIG_NFS_V4_1)
3723 .rpc_call_prepare = nfs4_delegreturn_prepare,
3724 #endif /* CONFIG_NFS_V4_1 */
3725 .rpc_call_done = nfs4_delegreturn_done,
3726 .rpc_release = nfs4_delegreturn_release,
3729 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3731 struct nfs4_delegreturndata *data;
3732 struct nfs_server *server = NFS_SERVER(inode);
3733 struct rpc_task *task;
3734 struct rpc_message msg = {
3735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3736 .rpc_cred = cred,
3738 struct rpc_task_setup task_setup_data = {
3739 .rpc_client = server->client,
3740 .rpc_message = &msg,
3741 .callback_ops = &nfs4_delegreturn_ops,
3742 .flags = RPC_TASK_ASYNC,
3744 int status = 0;
3746 data = kzalloc(sizeof(*data), GFP_NOFS);
3747 if (data == NULL)
3748 return -ENOMEM;
3749 data->args.fhandle = &data->fh;
3750 data->args.stateid = &data->stateid;
3751 data->args.bitmask = server->attr_bitmask;
3752 nfs_copy_fh(&data->fh, NFS_FH(inode));
3753 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3754 data->res.fattr = &data->fattr;
3755 data->res.server = server;
3756 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3757 nfs_fattr_init(data->res.fattr);
3758 data->timestamp = jiffies;
3759 data->rpc_status = 0;
3761 task_setup_data.callback_data = data;
3762 msg.rpc_argp = &data->args,
3763 msg.rpc_resp = &data->res,
3764 task = rpc_run_task(&task_setup_data);
3765 if (IS_ERR(task))
3766 return PTR_ERR(task);
3767 if (!issync)
3768 goto out;
3769 status = nfs4_wait_for_completion_rpc_task(task);
3770 if (status != 0)
3771 goto out;
3772 status = data->rpc_status;
3773 if (status != 0)
3774 goto out;
3775 nfs_refresh_inode(inode, &data->fattr);
3776 out:
3777 rpc_put_task(task);
3778 return status;
3781 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3783 struct nfs_server *server = NFS_SERVER(inode);
3784 struct nfs4_exception exception = { };
3785 int err;
3786 do {
3787 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3788 switch (err) {
3789 case -NFS4ERR_STALE_STATEID:
3790 case -NFS4ERR_EXPIRED:
3791 case 0:
3792 return 0;
3794 err = nfs4_handle_exception(server, err, &exception);
3795 } while (exception.retry);
3796 return err;
3799 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3800 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3803 * sleep, with exponential backoff, and retry the LOCK operation.
3805 static unsigned long
3806 nfs4_set_lock_task_retry(unsigned long timeout)
3808 schedule_timeout_killable(timeout);
3809 timeout <<= 1;
3810 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3811 return NFS4_LOCK_MAXTIMEOUT;
3812 return timeout;
3815 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3817 struct inode *inode = state->inode;
3818 struct nfs_server *server = NFS_SERVER(inode);
3819 struct nfs_client *clp = server->nfs_client;
3820 struct nfs_lockt_args arg = {
3821 .fh = NFS_FH(inode),
3822 .fl = request,
3824 struct nfs_lockt_res res = {
3825 .denied = request,
3827 struct rpc_message msg = {
3828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3829 .rpc_argp = &arg,
3830 .rpc_resp = &res,
3831 .rpc_cred = state->owner->so_cred,
3833 struct nfs4_lock_state *lsp;
3834 int status;
3836 arg.lock_owner.clientid = clp->cl_clientid;
3837 status = nfs4_set_lock_state(state, request);
3838 if (status != 0)
3839 goto out;
3840 lsp = request->fl_u.nfs4_fl.owner;
3841 arg.lock_owner.id = lsp->ls_id.id;
3842 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3843 switch (status) {
3844 case 0:
3845 request->fl_type = F_UNLCK;
3846 break;
3847 case -NFS4ERR_DENIED:
3848 status = 0;
3850 request->fl_ops->fl_release_private(request);
3851 out:
3852 return status;
3855 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3857 struct nfs4_exception exception = { };
3858 int err;
3860 do {
3861 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3862 _nfs4_proc_getlk(state, cmd, request),
3863 &exception);
3864 } while (exception.retry);
3865 return err;
3868 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3870 int res = 0;
3871 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3872 case FL_POSIX:
3873 res = posix_lock_file_wait(file, fl);
3874 break;
3875 case FL_FLOCK:
3876 res = flock_lock_file_wait(file, fl);
3877 break;
3878 default:
3879 BUG();
3881 return res;
3884 struct nfs4_unlockdata {
3885 struct nfs_locku_args arg;
3886 struct nfs_locku_res res;
3887 struct nfs4_lock_state *lsp;
3888 struct nfs_open_context *ctx;
3889 struct file_lock fl;
3890 const struct nfs_server *server;
3891 unsigned long timestamp;
3894 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3895 struct nfs_open_context *ctx,
3896 struct nfs4_lock_state *lsp,
3897 struct nfs_seqid *seqid)
3899 struct nfs4_unlockdata *p;
3900 struct inode *inode = lsp->ls_state->inode;
3902 p = kzalloc(sizeof(*p), GFP_NOFS);
3903 if (p == NULL)
3904 return NULL;
3905 p->arg.fh = NFS_FH(inode);
3906 p->arg.fl = &p->fl;
3907 p->arg.seqid = seqid;
3908 p->res.seqid = seqid;
3909 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3910 p->arg.stateid = &lsp->ls_stateid;
3911 p->lsp = lsp;
3912 atomic_inc(&lsp->ls_count);
3913 /* Ensure we don't close file until we're done freeing locks! */
3914 p->ctx = get_nfs_open_context(ctx);
3915 memcpy(&p->fl, fl, sizeof(p->fl));
3916 p->server = NFS_SERVER(inode);
3917 return p;
3920 static void nfs4_locku_release_calldata(void *data)
3922 struct nfs4_unlockdata *calldata = data;
3923 nfs_free_seqid(calldata->arg.seqid);
3924 nfs4_put_lock_state(calldata->lsp);
3925 put_nfs_open_context(calldata->ctx);
3926 kfree(calldata);
3929 static void nfs4_locku_done(struct rpc_task *task, void *data)
3931 struct nfs4_unlockdata *calldata = data;
3933 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3934 task->tk_status);
3935 if (RPC_ASSASSINATED(task))
3936 return;
3937 switch (task->tk_status) {
3938 case 0:
3939 memcpy(calldata->lsp->ls_stateid.data,
3940 calldata->res.stateid.data,
3941 sizeof(calldata->lsp->ls_stateid.data));
3942 renew_lease(calldata->server, calldata->timestamp);
3943 break;
3944 case -NFS4ERR_BAD_STATEID:
3945 case -NFS4ERR_OLD_STATEID:
3946 case -NFS4ERR_STALE_STATEID:
3947 case -NFS4ERR_EXPIRED:
3948 break;
3949 default:
3950 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3951 nfs_restart_rpc(task,
3952 calldata->server->nfs_client);
3956 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3958 struct nfs4_unlockdata *calldata = data;
3960 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3961 return;
3962 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3963 /* Note: exit _without_ running nfs4_locku_done */
3964 task->tk_action = NULL;
3965 return;
3967 calldata->timestamp = jiffies;
3968 if (nfs4_setup_sequence(calldata->server->nfs_client,
3969 &calldata->arg.seq_args,
3970 &calldata->res.seq_res, 1, task))
3971 return;
3972 rpc_call_start(task);
3975 static const struct rpc_call_ops nfs4_locku_ops = {
3976 .rpc_call_prepare = nfs4_locku_prepare,
3977 .rpc_call_done = nfs4_locku_done,
3978 .rpc_release = nfs4_locku_release_calldata,
3981 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3982 struct nfs_open_context *ctx,
3983 struct nfs4_lock_state *lsp,
3984 struct nfs_seqid *seqid)
3986 struct nfs4_unlockdata *data;
3987 struct rpc_message msg = {
3988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3989 .rpc_cred = ctx->cred,
3991 struct rpc_task_setup task_setup_data = {
3992 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3993 .rpc_message = &msg,
3994 .callback_ops = &nfs4_locku_ops,
3995 .workqueue = nfsiod_workqueue,
3996 .flags = RPC_TASK_ASYNC,
3999 /* Ensure this is an unlock - when canceling a lock, the
4000 * canceled lock is passed in, and it won't be an unlock.
4002 fl->fl_type = F_UNLCK;
4004 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4005 if (data == NULL) {
4006 nfs_free_seqid(seqid);
4007 return ERR_PTR(-ENOMEM);
4010 msg.rpc_argp = &data->arg,
4011 msg.rpc_resp = &data->res,
4012 task_setup_data.callback_data = data;
4013 return rpc_run_task(&task_setup_data);
4016 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4018 struct nfs_inode *nfsi = NFS_I(state->inode);
4019 struct nfs_seqid *seqid;
4020 struct nfs4_lock_state *lsp;
4021 struct rpc_task *task;
4022 int status = 0;
4023 unsigned char fl_flags = request->fl_flags;
4025 status = nfs4_set_lock_state(state, request);
4026 /* Unlock _before_ we do the RPC call */
4027 request->fl_flags |= FL_EXISTS;
4028 down_read(&nfsi->rwsem);
4029 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4030 up_read(&nfsi->rwsem);
4031 goto out;
4033 up_read(&nfsi->rwsem);
4034 if (status != 0)
4035 goto out;
4036 /* Is this a delegated lock? */
4037 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4038 goto out;
4039 lsp = request->fl_u.nfs4_fl.owner;
4040 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4041 status = -ENOMEM;
4042 if (seqid == NULL)
4043 goto out;
4044 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4045 status = PTR_ERR(task);
4046 if (IS_ERR(task))
4047 goto out;
4048 status = nfs4_wait_for_completion_rpc_task(task);
4049 rpc_put_task(task);
4050 out:
4051 request->fl_flags = fl_flags;
4052 return status;
4055 struct nfs4_lockdata {
4056 struct nfs_lock_args arg;
4057 struct nfs_lock_res res;
4058 struct nfs4_lock_state *lsp;
4059 struct nfs_open_context *ctx;
4060 struct file_lock fl;
4061 unsigned long timestamp;
4062 int rpc_status;
4063 int cancelled;
4064 struct nfs_server *server;
4067 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4068 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4069 gfp_t gfp_mask)
4071 struct nfs4_lockdata *p;
4072 struct inode *inode = lsp->ls_state->inode;
4073 struct nfs_server *server = NFS_SERVER(inode);
4075 p = kzalloc(sizeof(*p), gfp_mask);
4076 if (p == NULL)
4077 return NULL;
4079 p->arg.fh = NFS_FH(inode);
4080 p->arg.fl = &p->fl;
4081 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4082 if (p->arg.open_seqid == NULL)
4083 goto out_free;
4084 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4085 if (p->arg.lock_seqid == NULL)
4086 goto out_free_seqid;
4087 p->arg.lock_stateid = &lsp->ls_stateid;
4088 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4089 p->arg.lock_owner.id = lsp->ls_id.id;
4090 p->res.lock_seqid = p->arg.lock_seqid;
4091 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4092 p->lsp = lsp;
4093 p->server = server;
4094 atomic_inc(&lsp->ls_count);
4095 p->ctx = get_nfs_open_context(ctx);
4096 memcpy(&p->fl, fl, sizeof(p->fl));
4097 return p;
4098 out_free_seqid:
4099 nfs_free_seqid(p->arg.open_seqid);
4100 out_free:
4101 kfree(p);
4102 return NULL;
4105 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4107 struct nfs4_lockdata *data = calldata;
4108 struct nfs4_state *state = data->lsp->ls_state;
4110 dprintk("%s: begin!\n", __func__);
4111 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4112 return;
4113 /* Do we need to do an open_to_lock_owner? */
4114 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4115 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4116 return;
4117 data->arg.open_stateid = &state->stateid;
4118 data->arg.new_lock_owner = 1;
4119 data->res.open_seqid = data->arg.open_seqid;
4120 } else
4121 data->arg.new_lock_owner = 0;
4122 data->timestamp = jiffies;
4123 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
4124 &data->res.seq_res, 1, task))
4125 return;
4126 rpc_call_start(task);
4127 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4130 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4132 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4133 nfs4_lock_prepare(task, calldata);
4136 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4138 struct nfs4_lockdata *data = calldata;
4140 dprintk("%s: begin!\n", __func__);
4142 nfs4_sequence_done(data->server, &data->res.seq_res,
4143 task->tk_status);
4145 data->rpc_status = task->tk_status;
4146 if (RPC_ASSASSINATED(task))
4147 goto out;
4148 if (data->arg.new_lock_owner != 0) {
4149 if (data->rpc_status == 0)
4150 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4151 else
4152 goto out;
4154 if (data->rpc_status == 0) {
4155 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4156 sizeof(data->lsp->ls_stateid.data));
4157 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4158 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4160 out:
4161 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4164 static void nfs4_lock_release(void *calldata)
4166 struct nfs4_lockdata *data = calldata;
4168 dprintk("%s: begin!\n", __func__);
4169 nfs_free_seqid(data->arg.open_seqid);
4170 if (data->cancelled != 0) {
4171 struct rpc_task *task;
4172 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4173 data->arg.lock_seqid);
4174 if (!IS_ERR(task))
4175 rpc_put_task(task);
4176 dprintk("%s: cancelling lock!\n", __func__);
4177 } else
4178 nfs_free_seqid(data->arg.lock_seqid);
4179 nfs4_put_lock_state(data->lsp);
4180 put_nfs_open_context(data->ctx);
4181 kfree(data);
4182 dprintk("%s: done!\n", __func__);
4185 static const struct rpc_call_ops nfs4_lock_ops = {
4186 .rpc_call_prepare = nfs4_lock_prepare,
4187 .rpc_call_done = nfs4_lock_done,
4188 .rpc_release = nfs4_lock_release,
4191 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4192 .rpc_call_prepare = nfs4_recover_lock_prepare,
4193 .rpc_call_done = nfs4_lock_done,
4194 .rpc_release = nfs4_lock_release,
4197 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4199 struct nfs_client *clp = server->nfs_client;
4200 struct nfs4_state *state = lsp->ls_state;
4202 switch (error) {
4203 case -NFS4ERR_ADMIN_REVOKED:
4204 case -NFS4ERR_BAD_STATEID:
4205 case -NFS4ERR_EXPIRED:
4206 if (new_lock_owner != 0 ||
4207 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4208 nfs4_state_mark_reclaim_nograce(clp, state);
4209 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4210 break;
4211 case -NFS4ERR_STALE_STATEID:
4212 if (new_lock_owner != 0 ||
4213 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4214 nfs4_state_mark_reclaim_reboot(clp, state);
4215 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4219 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4221 struct nfs4_lockdata *data;
4222 struct rpc_task *task;
4223 struct rpc_message msg = {
4224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4225 .rpc_cred = state->owner->so_cred,
4227 struct rpc_task_setup task_setup_data = {
4228 .rpc_client = NFS_CLIENT(state->inode),
4229 .rpc_message = &msg,
4230 .callback_ops = &nfs4_lock_ops,
4231 .workqueue = nfsiod_workqueue,
4232 .flags = RPC_TASK_ASYNC,
4234 int ret;
4236 dprintk("%s: begin!\n", __func__);
4237 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4238 fl->fl_u.nfs4_fl.owner,
4239 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4240 if (data == NULL)
4241 return -ENOMEM;
4242 if (IS_SETLKW(cmd))
4243 data->arg.block = 1;
4244 if (recovery_type > NFS_LOCK_NEW) {
4245 if (recovery_type == NFS_LOCK_RECLAIM)
4246 data->arg.reclaim = NFS_LOCK_RECLAIM;
4247 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4249 msg.rpc_argp = &data->arg,
4250 msg.rpc_resp = &data->res,
4251 task_setup_data.callback_data = data;
4252 task = rpc_run_task(&task_setup_data);
4253 if (IS_ERR(task))
4254 return PTR_ERR(task);
4255 ret = nfs4_wait_for_completion_rpc_task(task);
4256 if (ret == 0) {
4257 ret = data->rpc_status;
4258 if (ret)
4259 nfs4_handle_setlk_error(data->server, data->lsp,
4260 data->arg.new_lock_owner, ret);
4261 } else
4262 data->cancelled = 1;
4263 rpc_put_task(task);
4264 dprintk("%s: done, ret = %d!\n", __func__, ret);
4265 return ret;
4268 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4270 struct nfs_server *server = NFS_SERVER(state->inode);
4271 struct nfs4_exception exception = { };
4272 int err;
4274 do {
4275 /* Cache the lock if possible... */
4276 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4277 return 0;
4278 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4279 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
4280 break;
4281 nfs4_handle_exception(server, err, &exception);
4282 } while (exception.retry);
4283 return err;
4286 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4288 struct nfs_server *server = NFS_SERVER(state->inode);
4289 struct nfs4_exception exception = { };
4290 int err;
4292 err = nfs4_set_lock_state(state, request);
4293 if (err != 0)
4294 return err;
4295 do {
4296 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4297 return 0;
4298 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4299 switch (err) {
4300 default:
4301 goto out;
4302 case -NFS4ERR_GRACE:
4303 case -NFS4ERR_DELAY:
4304 case -EKEYEXPIRED:
4305 nfs4_handle_exception(server, err, &exception);
4306 err = 0;
4308 } while (exception.retry);
4309 out:
4310 return err;
4313 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4315 struct nfs_inode *nfsi = NFS_I(state->inode);
4316 unsigned char fl_flags = request->fl_flags;
4317 int status = -ENOLCK;
4319 if ((fl_flags & FL_POSIX) &&
4320 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4321 goto out;
4322 /* Is this a delegated open? */
4323 status = nfs4_set_lock_state(state, request);
4324 if (status != 0)
4325 goto out;
4326 request->fl_flags |= FL_ACCESS;
4327 status = do_vfs_lock(request->fl_file, request);
4328 if (status < 0)
4329 goto out;
4330 down_read(&nfsi->rwsem);
4331 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4332 /* Yes: cache locks! */
4333 /* ...but avoid races with delegation recall... */
4334 request->fl_flags = fl_flags & ~FL_SLEEP;
4335 status = do_vfs_lock(request->fl_file, request);
4336 goto out_unlock;
4338 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4339 if (status != 0)
4340 goto out_unlock;
4341 /* Note: we always want to sleep here! */
4342 request->fl_flags = fl_flags | FL_SLEEP;
4343 if (do_vfs_lock(request->fl_file, request) < 0)
4344 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4345 out_unlock:
4346 up_read(&nfsi->rwsem);
4347 out:
4348 request->fl_flags = fl_flags;
4349 return status;
4352 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4354 struct nfs4_exception exception = { };
4355 int err;
4357 do {
4358 err = _nfs4_proc_setlk(state, cmd, request);
4359 if (err == -NFS4ERR_DENIED)
4360 err = -EAGAIN;
4361 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4362 err, &exception);
4363 } while (exception.retry);
4364 return err;
4367 static int
4368 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4370 struct nfs_open_context *ctx;
4371 struct nfs4_state *state;
4372 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4373 int status;
4375 /* verify open state */
4376 ctx = nfs_file_open_context(filp);
4377 state = ctx->state;
4379 if (request->fl_start < 0 || request->fl_end < 0)
4380 return -EINVAL;
4382 if (IS_GETLK(cmd)) {
4383 if (state != NULL)
4384 return nfs4_proc_getlk(state, F_GETLK, request);
4385 return 0;
4388 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4389 return -EINVAL;
4391 if (request->fl_type == F_UNLCK) {
4392 if (state != NULL)
4393 return nfs4_proc_unlck(state, cmd, request);
4394 return 0;
4397 if (state == NULL)
4398 return -ENOLCK;
4399 do {
4400 status = nfs4_proc_setlk(state, cmd, request);
4401 if ((status != -EAGAIN) || IS_SETLK(cmd))
4402 break;
4403 timeout = nfs4_set_lock_task_retry(timeout);
4404 status = -ERESTARTSYS;
4405 if (signalled())
4406 break;
4407 } while(status < 0);
4408 return status;
4411 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4413 struct nfs_server *server = NFS_SERVER(state->inode);
4414 struct nfs4_exception exception = { };
4415 int err;
4417 err = nfs4_set_lock_state(state, fl);
4418 if (err != 0)
4419 goto out;
4420 do {
4421 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4422 switch (err) {
4423 default:
4424 printk(KERN_ERR "%s: unhandled error %d.\n",
4425 __func__, err);
4426 case 0:
4427 case -ESTALE:
4428 goto out;
4429 case -NFS4ERR_EXPIRED:
4430 case -NFS4ERR_STALE_CLIENTID:
4431 case -NFS4ERR_STALE_STATEID:
4432 case -NFS4ERR_BADSESSION:
4433 case -NFS4ERR_BADSLOT:
4434 case -NFS4ERR_BAD_HIGH_SLOT:
4435 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4436 case -NFS4ERR_DEADSESSION:
4437 nfs4_schedule_state_recovery(server->nfs_client);
4438 goto out;
4439 case -ERESTARTSYS:
4441 * The show must go on: exit, but mark the
4442 * stateid as needing recovery.
4444 case -NFS4ERR_ADMIN_REVOKED:
4445 case -NFS4ERR_BAD_STATEID:
4446 case -NFS4ERR_OPENMODE:
4447 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4448 err = 0;
4449 goto out;
4450 case -ENOMEM:
4451 case -NFS4ERR_DENIED:
4452 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4453 err = 0;
4454 goto out;
4455 case -NFS4ERR_DELAY:
4456 case -EKEYEXPIRED:
4457 break;
4459 err = nfs4_handle_exception(server, err, &exception);
4460 } while (exception.retry);
4461 out:
4462 return err;
4465 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4467 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4468 size_t buflen, int flags)
4470 struct inode *inode = dentry->d_inode;
4472 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4473 return -EOPNOTSUPP;
4475 return nfs4_proc_set_acl(inode, buf, buflen);
4478 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4479 * and that's what we'll do for e.g. user attributes that haven't been set.
4480 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4481 * attributes in kernel-managed attribute namespaces. */
4482 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4483 size_t buflen)
4485 struct inode *inode = dentry->d_inode;
4487 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4488 return -EOPNOTSUPP;
4490 return nfs4_proc_get_acl(inode, buf, buflen);
4493 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4495 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4497 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4498 return 0;
4499 if (buf && buflen < len)
4500 return -ERANGE;
4501 if (buf)
4502 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4503 return len;
4506 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4508 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4509 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4510 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4511 return;
4513 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4514 NFS_ATTR_FATTR_NLINK;
4515 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4516 fattr->nlink = 2;
4519 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4520 struct nfs4_fs_locations *fs_locations, struct page *page)
4522 struct nfs_server *server = NFS_SERVER(dir);
4523 u32 bitmask[2] = {
4524 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4525 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4527 struct nfs4_fs_locations_arg args = {
4528 .dir_fh = NFS_FH(dir),
4529 .name = name,
4530 .page = page,
4531 .bitmask = bitmask,
4533 struct nfs4_fs_locations_res res = {
4534 .fs_locations = fs_locations,
4536 struct rpc_message msg = {
4537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4538 .rpc_argp = &args,
4539 .rpc_resp = &res,
4541 int status;
4543 dprintk("%s: start\n", __func__);
4544 nfs_fattr_init(&fs_locations->fattr);
4545 fs_locations->server = server;
4546 fs_locations->nlocations = 0;
4547 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4548 nfs_fixup_referral_attributes(&fs_locations->fattr);
4549 dprintk("%s: returned status = %d\n", __func__, status);
4550 return status;
4553 #ifdef CONFIG_NFS_V4_1
4555 * nfs4_proc_exchange_id()
4557 * Since the clientid has expired, all compounds using sessions
4558 * associated with the stale clientid will be returning
4559 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4560 * be in some phase of session reset.
4562 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4564 nfs4_verifier verifier;
4565 struct nfs41_exchange_id_args args = {
4566 .client = clp,
4567 .flags = clp->cl_exchange_flags,
4569 struct nfs41_exchange_id_res res = {
4570 .client = clp,
4572 int status;
4573 struct rpc_message msg = {
4574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4575 .rpc_argp = &args,
4576 .rpc_resp = &res,
4577 .rpc_cred = cred,
4579 __be32 *p;
4581 dprintk("--> %s\n", __func__);
4582 BUG_ON(clp == NULL);
4584 /* Remove server-only flags */
4585 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4587 p = (u32 *)verifier.data;
4588 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4589 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4590 args.verifier = &verifier;
4592 while (1) {
4593 args.id_len = scnprintf(args.id, sizeof(args.id),
4594 "%s/%s %u",
4595 clp->cl_ipaddr,
4596 rpc_peeraddr2str(clp->cl_rpcclient,
4597 RPC_DISPLAY_ADDR),
4598 clp->cl_id_uniquifier);
4600 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4602 if (status != -NFS4ERR_CLID_INUSE)
4603 break;
4605 if (signalled())
4606 break;
4608 if (++clp->cl_id_uniquifier == 0)
4609 break;
4612 dprintk("<-- %s status= %d\n", __func__, status);
4613 return status;
4616 struct nfs4_get_lease_time_data {
4617 struct nfs4_get_lease_time_args *args;
4618 struct nfs4_get_lease_time_res *res;
4619 struct nfs_client *clp;
4622 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4623 void *calldata)
4625 int ret;
4626 struct nfs4_get_lease_time_data *data =
4627 (struct nfs4_get_lease_time_data *)calldata;
4629 dprintk("--> %s\n", __func__);
4630 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4631 /* just setup sequence, do not trigger session recovery
4632 since we're invoked within one */
4633 ret = nfs41_setup_sequence(data->clp->cl_session,
4634 &data->args->la_seq_args,
4635 &data->res->lr_seq_res, 0, task);
4637 BUG_ON(ret == -EAGAIN);
4638 rpc_call_start(task);
4639 dprintk("<-- %s\n", __func__);
4643 * Called from nfs4_state_manager thread for session setup, so don't recover
4644 * from sequence operation or clientid errors.
4646 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4648 struct nfs4_get_lease_time_data *data =
4649 (struct nfs4_get_lease_time_data *)calldata;
4651 dprintk("--> %s\n", __func__);
4652 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4653 switch (task->tk_status) {
4654 case -NFS4ERR_DELAY:
4655 case -NFS4ERR_GRACE:
4656 case -EKEYEXPIRED:
4657 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4658 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4659 task->tk_status = 0;
4660 nfs_restart_rpc(task, data->clp);
4661 return;
4663 dprintk("<-- %s\n", __func__);
4666 struct rpc_call_ops nfs4_get_lease_time_ops = {
4667 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4668 .rpc_call_done = nfs4_get_lease_time_done,
4671 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4673 struct rpc_task *task;
4674 struct nfs4_get_lease_time_args args;
4675 struct nfs4_get_lease_time_res res = {
4676 .lr_fsinfo = fsinfo,
4678 struct nfs4_get_lease_time_data data = {
4679 .args = &args,
4680 .res = &res,
4681 .clp = clp,
4683 struct rpc_message msg = {
4684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4685 .rpc_argp = &args,
4686 .rpc_resp = &res,
4688 struct rpc_task_setup task_setup = {
4689 .rpc_client = clp->cl_rpcclient,
4690 .rpc_message = &msg,
4691 .callback_ops = &nfs4_get_lease_time_ops,
4692 .callback_data = &data
4694 int status;
4696 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4697 dprintk("--> %s\n", __func__);
4698 task = rpc_run_task(&task_setup);
4700 if (IS_ERR(task))
4701 status = PTR_ERR(task);
4702 else {
4703 status = task->tk_status;
4704 rpc_put_task(task);
4706 dprintk("<-- %s return %d\n", __func__, status);
4708 return status;
4712 * Reset a slot table
4714 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4715 int ivalue)
4717 struct nfs4_slot *new = NULL;
4718 int i;
4719 int ret = 0;
4721 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4722 max_reqs, tbl->max_slots);
4724 /* Does the newly negotiated max_reqs match the existing slot table? */
4725 if (max_reqs != tbl->max_slots) {
4726 ret = -ENOMEM;
4727 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4728 GFP_NOFS);
4729 if (!new)
4730 goto out;
4731 ret = 0;
4732 kfree(tbl->slots);
4734 spin_lock(&tbl->slot_tbl_lock);
4735 if (new) {
4736 tbl->slots = new;
4737 tbl->max_slots = max_reqs;
4739 for (i = 0; i < tbl->max_slots; ++i)
4740 tbl->slots[i].seq_nr = ivalue;
4741 spin_unlock(&tbl->slot_tbl_lock);
4742 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4743 tbl, tbl->slots, tbl->max_slots);
4744 out:
4745 dprintk("<-- %s: return %d\n", __func__, ret);
4746 return ret;
4750 * Reset the forechannel and backchannel slot tables
4752 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4754 int status;
4756 status = nfs4_reset_slot_table(&session->fc_slot_table,
4757 session->fc_attrs.max_reqs, 1);
4758 if (status)
4759 return status;
4761 status = nfs4_reset_slot_table(&session->bc_slot_table,
4762 session->bc_attrs.max_reqs, 0);
4763 return status;
4766 /* Destroy the slot table */
4767 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4769 if (session->fc_slot_table.slots != NULL) {
4770 kfree(session->fc_slot_table.slots);
4771 session->fc_slot_table.slots = NULL;
4773 if (session->bc_slot_table.slots != NULL) {
4774 kfree(session->bc_slot_table.slots);
4775 session->bc_slot_table.slots = NULL;
4777 return;
4781 * Initialize slot table
4783 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4784 int max_slots, int ivalue)
4786 struct nfs4_slot *slot;
4787 int ret = -ENOMEM;
4789 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4791 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4793 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4794 if (!slot)
4795 goto out;
4796 ret = 0;
4798 spin_lock(&tbl->slot_tbl_lock);
4799 tbl->max_slots = max_slots;
4800 tbl->slots = slot;
4801 tbl->highest_used_slotid = -1; /* no slot is currently used */
4802 spin_unlock(&tbl->slot_tbl_lock);
4803 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4804 tbl, tbl->slots, tbl->max_slots);
4805 out:
4806 dprintk("<-- %s: return %d\n", __func__, ret);
4807 return ret;
4811 * Initialize the forechannel and backchannel tables
4813 static int nfs4_init_slot_tables(struct nfs4_session *session)
4815 struct nfs4_slot_table *tbl;
4816 int status = 0;
4818 tbl = &session->fc_slot_table;
4819 if (tbl->slots == NULL) {
4820 status = nfs4_init_slot_table(tbl,
4821 session->fc_attrs.max_reqs, 1);
4822 if (status)
4823 return status;
4826 tbl = &session->bc_slot_table;
4827 if (tbl->slots == NULL) {
4828 status = nfs4_init_slot_table(tbl,
4829 session->bc_attrs.max_reqs, 0);
4830 if (status)
4831 nfs4_destroy_slot_tables(session);
4834 return status;
4837 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4839 struct nfs4_session *session;
4840 struct nfs4_slot_table *tbl;
4842 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4843 if (!session)
4844 return NULL;
4847 * The create session reply races with the server back
4848 * channel probe. Mark the client NFS_CS_SESSION_INITING
4849 * so that the client back channel can find the
4850 * nfs_client struct
4852 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4853 init_completion(&session->complete);
4855 tbl = &session->fc_slot_table;
4856 tbl->highest_used_slotid = -1;
4857 spin_lock_init(&tbl->slot_tbl_lock);
4858 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4860 tbl = &session->bc_slot_table;
4861 tbl->highest_used_slotid = -1;
4862 spin_lock_init(&tbl->slot_tbl_lock);
4863 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4865 session->clp = clp;
4866 return session;
4869 void nfs4_destroy_session(struct nfs4_session *session)
4871 nfs4_proc_destroy_session(session);
4872 dprintk("%s Destroy backchannel for xprt %p\n",
4873 __func__, session->clp->cl_rpcclient->cl_xprt);
4874 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4875 NFS41_BC_MIN_CALLBACKS);
4876 nfs4_destroy_slot_tables(session);
4877 kfree(session);
4881 * Initialize the values to be used by the client in CREATE_SESSION
4882 * If nfs4_init_session set the fore channel request and response sizes,
4883 * use them.
4885 * Set the back channel max_resp_sz_cached to zero to force the client to
4886 * always set csa_cachethis to FALSE because the current implementation
4887 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4889 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4891 struct nfs4_session *session = args->client->cl_session;
4892 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4893 mxresp_sz = session->fc_attrs.max_resp_sz;
4895 if (mxrqst_sz == 0)
4896 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4897 if (mxresp_sz == 0)
4898 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4899 /* Fore channel attributes */
4900 args->fc_attrs.headerpadsz = 0;
4901 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4902 args->fc_attrs.max_resp_sz = mxresp_sz;
4903 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4904 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4906 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4907 "max_ops=%u max_reqs=%u\n",
4908 __func__,
4909 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4910 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4912 /* Back channel attributes */
4913 args->bc_attrs.headerpadsz = 0;
4914 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4915 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4916 args->bc_attrs.max_resp_sz_cached = 0;
4917 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4918 args->bc_attrs.max_reqs = 1;
4920 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4921 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4922 __func__,
4923 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4924 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4925 args->bc_attrs.max_reqs);
4928 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4930 if (rcvd <= sent)
4931 return 0;
4932 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4933 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4934 return -EINVAL;
4937 #define _verify_fore_channel_attr(_name_) \
4938 _verify_channel_attr("fore", #_name_, \
4939 args->fc_attrs._name_, \
4940 session->fc_attrs._name_)
4942 #define _verify_back_channel_attr(_name_) \
4943 _verify_channel_attr("back", #_name_, \
4944 args->bc_attrs._name_, \
4945 session->bc_attrs._name_)
4948 * The server is not allowed to increase the fore channel header pad size,
4949 * maximum response size, or maximum number of operations.
4951 * The back channel attributes are only negotiatied down: We send what the
4952 * (back channel) server insists upon.
4954 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4955 struct nfs4_session *session)
4957 int ret = 0;
4959 ret |= _verify_fore_channel_attr(headerpadsz);
4960 ret |= _verify_fore_channel_attr(max_resp_sz);
4961 ret |= _verify_fore_channel_attr(max_ops);
4963 ret |= _verify_back_channel_attr(headerpadsz);
4964 ret |= _verify_back_channel_attr(max_rqst_sz);
4965 ret |= _verify_back_channel_attr(max_resp_sz);
4966 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4967 ret |= _verify_back_channel_attr(max_ops);
4968 ret |= _verify_back_channel_attr(max_reqs);
4970 return ret;
4973 static int _nfs4_proc_create_session(struct nfs_client *clp)
4975 struct nfs4_session *session = clp->cl_session;
4976 struct nfs41_create_session_args args = {
4977 .client = clp,
4978 .cb_program = NFS4_CALLBACK,
4980 struct nfs41_create_session_res res = {
4981 .client = clp,
4983 struct rpc_message msg = {
4984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4985 .rpc_argp = &args,
4986 .rpc_resp = &res,
4988 int status;
4990 nfs4_init_channel_attrs(&args);
4991 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4993 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4995 if (!status)
4996 /* Verify the session's negotiated channel_attrs values */
4997 status = nfs4_verify_channel_attrs(&args, session);
4998 if (!status) {
4999 /* Increment the clientid slot sequence id */
5000 clp->cl_seqid++;
5003 return status;
5007 * Issues a CREATE_SESSION operation to the server.
5008 * It is the responsibility of the caller to verify the session is
5009 * expired before calling this routine.
5011 int nfs4_proc_create_session(struct nfs_client *clp)
5013 int status;
5014 unsigned *ptr;
5015 struct nfs4_session *session = clp->cl_session;
5017 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5019 status = _nfs4_proc_create_session(clp);
5020 if (status)
5021 goto out;
5023 /* Init and reset the fore channel */
5024 status = nfs4_init_slot_tables(session);
5025 dprintk("slot table initialization returned %d\n", status);
5026 if (status)
5027 goto out;
5028 status = nfs4_reset_slot_tables(session);
5029 dprintk("slot table reset returned %d\n", status);
5030 if (status)
5031 goto out;
5033 ptr = (unsigned *)&session->sess_id.data[0];
5034 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5035 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5036 out:
5037 dprintk("<-- %s\n", __func__);
5038 return status;
5042 * Issue the over-the-wire RPC DESTROY_SESSION.
5043 * The caller must serialize access to this routine.
5045 int nfs4_proc_destroy_session(struct nfs4_session *session)
5047 int status = 0;
5048 struct rpc_message msg;
5050 dprintk("--> nfs4_proc_destroy_session\n");
5052 /* session is still being setup */
5053 if (session->clp->cl_cons_state != NFS_CS_READY)
5054 return status;
5056 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5057 msg.rpc_argp = session;
5058 msg.rpc_resp = NULL;
5059 msg.rpc_cred = NULL;
5060 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5062 if (status)
5063 printk(KERN_WARNING
5064 "Got error %d from the server on DESTROY_SESSION. "
5065 "Session has been destroyed regardless...\n", status);
5067 dprintk("<-- nfs4_proc_destroy_session\n");
5068 return status;
5071 int nfs4_init_session(struct nfs_server *server)
5073 struct nfs_client *clp = server->nfs_client;
5074 struct nfs4_session *session;
5075 unsigned int rsize, wsize;
5076 int ret;
5078 if (!nfs4_has_session(clp))
5079 return 0;
5081 rsize = server->rsize;
5082 if (rsize == 0)
5083 rsize = NFS_MAX_FILE_IO_SIZE;
5084 wsize = server->wsize;
5085 if (wsize == 0)
5086 wsize = NFS_MAX_FILE_IO_SIZE;
5088 session = clp->cl_session;
5089 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5090 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5092 ret = nfs4_recover_expired_lease(server);
5093 if (!ret)
5094 ret = nfs4_check_client_ready(clp);
5095 return ret;
5099 * Renew the cl_session lease.
5101 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5103 struct nfs4_sequence_args args;
5104 struct nfs4_sequence_res res;
5106 struct rpc_message msg = {
5107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5108 .rpc_argp = &args,
5109 .rpc_resp = &res,
5110 .rpc_cred = cred,
5113 args.sa_cache_this = 0;
5115 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
5116 &res, args.sa_cache_this, 1);
5119 static void nfs41_sequence_release(void *data)
5121 struct nfs_client *clp = (struct nfs_client *)data;
5123 if (atomic_read(&clp->cl_count) > 1)
5124 nfs4_schedule_state_renewal(clp);
5125 nfs_put_client(clp);
5128 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5130 struct nfs_client *clp = (struct nfs_client *)data;
5132 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
5134 if (task->tk_status < 0) {
5135 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5136 if (atomic_read(&clp->cl_count) == 1)
5137 goto out;
5139 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
5140 == -EAGAIN) {
5141 nfs_restart_rpc(task, clp);
5142 return;
5145 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5146 out:
5147 kfree(task->tk_msg.rpc_argp);
5148 kfree(task->tk_msg.rpc_resp);
5150 dprintk("<-- %s\n", __func__);
5153 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5155 struct nfs_client *clp;
5156 struct nfs4_sequence_args *args;
5157 struct nfs4_sequence_res *res;
5159 clp = (struct nfs_client *)data;
5160 args = task->tk_msg.rpc_argp;
5161 res = task->tk_msg.rpc_resp;
5163 if (nfs4_setup_sequence(clp, args, res, 0, task))
5164 return;
5165 rpc_call_start(task);
5168 static const struct rpc_call_ops nfs41_sequence_ops = {
5169 .rpc_call_done = nfs41_sequence_call_done,
5170 .rpc_call_prepare = nfs41_sequence_prepare,
5171 .rpc_release = nfs41_sequence_release,
5174 static int nfs41_proc_async_sequence(struct nfs_client *clp,
5175 struct rpc_cred *cred)
5177 struct nfs4_sequence_args *args;
5178 struct nfs4_sequence_res *res;
5179 struct rpc_message msg = {
5180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5181 .rpc_cred = cred,
5184 if (!atomic_inc_not_zero(&clp->cl_count))
5185 return -EIO;
5186 args = kzalloc(sizeof(*args), GFP_NOFS);
5187 res = kzalloc(sizeof(*res), GFP_NOFS);
5188 if (!args || !res) {
5189 kfree(args);
5190 kfree(res);
5191 nfs_put_client(clp);
5192 return -ENOMEM;
5194 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5195 msg.rpc_argp = args;
5196 msg.rpc_resp = res;
5198 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5199 &nfs41_sequence_ops, (void *)clp);
5202 struct nfs4_reclaim_complete_data {
5203 struct nfs_client *clp;
5204 struct nfs41_reclaim_complete_args arg;
5205 struct nfs41_reclaim_complete_res res;
5208 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5210 struct nfs4_reclaim_complete_data *calldata = data;
5212 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5213 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
5214 &calldata->res.seq_res, 0, task))
5215 return;
5217 rpc_call_start(task);
5220 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5222 struct nfs4_reclaim_complete_data *calldata = data;
5223 struct nfs_client *clp = calldata->clp;
5224 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5226 dprintk("--> %s\n", __func__);
5227 nfs41_sequence_done(clp, res, task->tk_status);
5228 switch (task->tk_status) {
5229 case 0:
5230 case -NFS4ERR_COMPLETE_ALREADY:
5231 break;
5232 case -NFS4ERR_BADSESSION:
5233 case -NFS4ERR_DEADSESSION:
5235 * Handle the session error, but do not retry the operation, as
5236 * we have no way of telling whether the clientid had to be
5237 * reset before we got our reply. If reset, a new wave of
5238 * reclaim operations will follow, containing their own reclaim
5239 * complete. We don't want our retry to get on the way of
5240 * recovery by incorrectly indicating to the server that we're
5241 * done reclaiming state since the process had to be restarted.
5243 _nfs4_async_handle_error(task, NULL, clp, NULL);
5244 break;
5245 default:
5246 if (_nfs4_async_handle_error(
5247 task, NULL, clp, NULL) == -EAGAIN) {
5248 rpc_restart_call_prepare(task);
5249 return;
5253 dprintk("<-- %s\n", __func__);
5256 static void nfs4_free_reclaim_complete_data(void *data)
5258 struct nfs4_reclaim_complete_data *calldata = data;
5260 kfree(calldata);
5263 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5264 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5265 .rpc_call_done = nfs4_reclaim_complete_done,
5266 .rpc_release = nfs4_free_reclaim_complete_data,
5270 * Issue a global reclaim complete.
5272 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5274 struct nfs4_reclaim_complete_data *calldata;
5275 struct rpc_task *task;
5276 struct rpc_message msg = {
5277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5279 struct rpc_task_setup task_setup_data = {
5280 .rpc_client = clp->cl_rpcclient,
5281 .rpc_message = &msg,
5282 .callback_ops = &nfs4_reclaim_complete_call_ops,
5283 .flags = RPC_TASK_ASYNC,
5285 int status = -ENOMEM;
5287 dprintk("--> %s\n", __func__);
5288 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5289 if (calldata == NULL)
5290 goto out;
5291 calldata->clp = clp;
5292 calldata->arg.one_fs = 0;
5293 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5295 msg.rpc_argp = &calldata->arg;
5296 msg.rpc_resp = &calldata->res;
5297 task_setup_data.callback_data = calldata;
5298 task = rpc_run_task(&task_setup_data);
5299 if (IS_ERR(task)) {
5300 status = PTR_ERR(task);
5301 goto out;
5303 rpc_put_task(task);
5304 return 0;
5305 out:
5306 dprintk("<-- %s status=%d\n", __func__, status);
5307 return status;
5309 #endif /* CONFIG_NFS_V4_1 */
5311 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5312 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5313 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5314 .recover_open = nfs4_open_reclaim,
5315 .recover_lock = nfs4_lock_reclaim,
5316 .establish_clid = nfs4_init_clientid,
5317 .get_clid_cred = nfs4_get_setclientid_cred,
5320 #if defined(CONFIG_NFS_V4_1)
5321 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5322 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5323 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5324 .recover_open = nfs4_open_reclaim,
5325 .recover_lock = nfs4_lock_reclaim,
5326 .establish_clid = nfs41_init_clientid,
5327 .get_clid_cred = nfs4_get_exchange_id_cred,
5328 .reclaim_complete = nfs41_proc_reclaim_complete,
5330 #endif /* CONFIG_NFS_V4_1 */
5332 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5333 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5334 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5335 .recover_open = nfs4_open_expired,
5336 .recover_lock = nfs4_lock_expired,
5337 .establish_clid = nfs4_init_clientid,
5338 .get_clid_cred = nfs4_get_setclientid_cred,
5341 #if defined(CONFIG_NFS_V4_1)
5342 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5343 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5344 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5345 .recover_open = nfs4_open_expired,
5346 .recover_lock = nfs4_lock_expired,
5347 .establish_clid = nfs41_init_clientid,
5348 .get_clid_cred = nfs4_get_exchange_id_cred,
5350 #endif /* CONFIG_NFS_V4_1 */
5352 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5353 .sched_state_renewal = nfs4_proc_async_renew,
5354 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5355 .renew_lease = nfs4_proc_renew,
5358 #if defined(CONFIG_NFS_V4_1)
5359 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5360 .sched_state_renewal = nfs41_proc_async_sequence,
5361 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5362 .renew_lease = nfs4_proc_sequence,
5364 #endif
5367 * Per minor version reboot and network partition recovery ops
5370 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5371 &nfs40_reboot_recovery_ops,
5372 #if defined(CONFIG_NFS_V4_1)
5373 &nfs41_reboot_recovery_ops,
5374 #endif
5377 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5378 &nfs40_nograce_recovery_ops,
5379 #if defined(CONFIG_NFS_V4_1)
5380 &nfs41_nograce_recovery_ops,
5381 #endif
5384 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5385 &nfs40_state_renewal_ops,
5386 #if defined(CONFIG_NFS_V4_1)
5387 &nfs41_state_renewal_ops,
5388 #endif
5391 static const struct inode_operations nfs4_file_inode_operations = {
5392 .permission = nfs_permission,
5393 .getattr = nfs_getattr,
5394 .setattr = nfs_setattr,
5395 .getxattr = nfs4_getxattr,
5396 .setxattr = nfs4_setxattr,
5397 .listxattr = nfs4_listxattr,
5400 const struct nfs_rpc_ops nfs_v4_clientops = {
5401 .version = 4, /* protocol version */
5402 .dentry_ops = &nfs4_dentry_operations,
5403 .dir_inode_ops = &nfs4_dir_inode_operations,
5404 .file_inode_ops = &nfs4_file_inode_operations,
5405 .getroot = nfs4_proc_get_root,
5406 .getattr = nfs4_proc_getattr,
5407 .setattr = nfs4_proc_setattr,
5408 .lookupfh = nfs4_proc_lookupfh,
5409 .lookup = nfs4_proc_lookup,
5410 .access = nfs4_proc_access,
5411 .readlink = nfs4_proc_readlink,
5412 .create = nfs4_proc_create,
5413 .remove = nfs4_proc_remove,
5414 .unlink_setup = nfs4_proc_unlink_setup,
5415 .unlink_done = nfs4_proc_unlink_done,
5416 .rename = nfs4_proc_rename,
5417 .link = nfs4_proc_link,
5418 .symlink = nfs4_proc_symlink,
5419 .mkdir = nfs4_proc_mkdir,
5420 .rmdir = nfs4_proc_remove,
5421 .readdir = nfs4_proc_readdir,
5422 .mknod = nfs4_proc_mknod,
5423 .statfs = nfs4_proc_statfs,
5424 .fsinfo = nfs4_proc_fsinfo,
5425 .pathconf = nfs4_proc_pathconf,
5426 .set_capabilities = nfs4_server_capabilities,
5427 .decode_dirent = nfs4_decode_dirent,
5428 .read_setup = nfs4_proc_read_setup,
5429 .read_done = nfs4_read_done,
5430 .write_setup = nfs4_proc_write_setup,
5431 .write_done = nfs4_write_done,
5432 .commit_setup = nfs4_proc_commit_setup,
5433 .commit_done = nfs4_commit_done,
5434 .lock = nfs4_proc_lock,
5435 .clear_acl_cache = nfs4_zap_acl_attr,
5436 .close_context = nfs4_close_context,
5440 * Local variables:
5441 * c-basic-offset: 8
5442 * End: