xen: register runstate on secondary CPUs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfs4proc.c
blobed7c269e25143175a21156a4c5ce784e3efea4c1
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/sunrpc/clnt.h>
43 #include <linux/nfs.h>
44 #include <linux/nfs4.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/nfs_page.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/module.h>
50 #include <linux/sunrpc/bc_xprt.h>
52 #include "nfs4_fs.h"
53 #include "delegation.h"
54 #include "internal.h"
55 #include "iostat.h"
56 #include "callback.h"
58 #define NFSDBG_FACILITY NFSDBG_PROC
60 #define NFS4_POLL_RETRY_MIN (HZ/10)
61 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 #define NFS4_MAX_LOOP_ON_RECOVER (10)
65 struct nfs4_opendata;
66 static int _nfs4_proc_open(struct nfs4_opendata *data);
67 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
68 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
69 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72 /* Prevent leaks of NFSv4 errors into userland */
73 static int nfs4_map_errors(int err)
75 if (err < -1000) {
76 dprintk("%s could not handle NFSv4 error %d\n",
77 __func__, -err);
78 return -EIO;
80 return err;
84 * This is our standard bitmap for GETATTR requests.
86 const u32 nfs4_fattr_bitmap[2] = {
87 FATTR4_WORD0_TYPE
88 | FATTR4_WORD0_CHANGE
89 | FATTR4_WORD0_SIZE
90 | FATTR4_WORD0_FSID
91 | FATTR4_WORD0_FILEID,
92 FATTR4_WORD1_MODE
93 | FATTR4_WORD1_NUMLINKS
94 | FATTR4_WORD1_OWNER
95 | FATTR4_WORD1_OWNER_GROUP
96 | FATTR4_WORD1_RAWDEV
97 | FATTR4_WORD1_SPACE_USED
98 | FATTR4_WORD1_TIME_ACCESS
99 | FATTR4_WORD1_TIME_METADATA
100 | FATTR4_WORD1_TIME_MODIFY
103 const u32 nfs4_statfs_bitmap[2] = {
104 FATTR4_WORD0_FILES_AVAIL
105 | FATTR4_WORD0_FILES_FREE
106 | FATTR4_WORD0_FILES_TOTAL,
107 FATTR4_WORD1_SPACE_AVAIL
108 | FATTR4_WORD1_SPACE_FREE
109 | FATTR4_WORD1_SPACE_TOTAL
112 const u32 nfs4_pathconf_bitmap[2] = {
113 FATTR4_WORD0_MAXLINK
114 | FATTR4_WORD0_MAXNAME,
118 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
119 | FATTR4_WORD0_MAXREAD
120 | FATTR4_WORD0_MAXWRITE
121 | FATTR4_WORD0_LEASE_TIME,
125 const u32 nfs4_fs_locations_bitmap[2] = {
126 FATTR4_WORD0_TYPE
127 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_SIZE
129 | FATTR4_WORD0_FSID
130 | FATTR4_WORD0_FILEID
131 | FATTR4_WORD0_FS_LOCATIONS,
132 FATTR4_WORD1_MODE
133 | FATTR4_WORD1_NUMLINKS
134 | FATTR4_WORD1_OWNER
135 | FATTR4_WORD1_OWNER_GROUP
136 | FATTR4_WORD1_RAWDEV
137 | FATTR4_WORD1_SPACE_USED
138 | FATTR4_WORD1_TIME_ACCESS
139 | FATTR4_WORD1_TIME_METADATA
140 | FATTR4_WORD1_TIME_MODIFY
141 | FATTR4_WORD1_MOUNTED_ON_FILEID
144 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
145 struct nfs4_readdir_arg *readdir)
147 __be32 *start, *p;
149 BUG_ON(readdir->count < 80);
150 if (cookie > 2) {
151 readdir->cookie = cookie;
152 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
153 return;
156 readdir->cookie = 0;
157 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
158 if (cookie == 2)
159 return;
162 * NFSv4 servers do not return entries for '.' and '..'
163 * Therefore, we fake these entries here. We let '.'
164 * have cookie 0 and '..' have cookie 1. Note that
165 * when talking to the server, we always send cookie 0
166 * instead of 1 or 2.
168 start = p = kmap_atomic(*readdir->pages, KM_USER0);
170 if (cookie == 0) {
171 *p++ = xdr_one; /* next */
172 *p++ = xdr_zero; /* cookie, first word */
173 *p++ = xdr_one; /* cookie, second word */
174 *p++ = xdr_one; /* entry len */
175 memcpy(p, ".\0\0\0", 4); /* entry */
176 p++;
177 *p++ = xdr_one; /* bitmap length */
178 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
179 *p++ = htonl(8); /* attribute buffer length */
180 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
183 *p++ = xdr_one; /* next */
184 *p++ = xdr_zero; /* cookie, first word */
185 *p++ = xdr_two; /* cookie, second word */
186 *p++ = xdr_two; /* entry len */
187 memcpy(p, "..\0\0", 4); /* entry */
188 p++;
189 *p++ = xdr_one; /* bitmap length */
190 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
191 *p++ = htonl(8); /* attribute buffer length */
192 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
194 readdir->pgbase = (char *)p - (char *)start;
195 readdir->count -= readdir->pgbase;
196 kunmap_atomic(start, KM_USER0);
199 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
201 int res;
203 might_sleep();
205 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
206 nfs_wait_bit_killable, TASK_KILLABLE);
207 return res;
210 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
212 int res = 0;
214 might_sleep();
216 if (*timeout <= 0)
217 *timeout = NFS4_POLL_RETRY_MIN;
218 if (*timeout > NFS4_POLL_RETRY_MAX)
219 *timeout = NFS4_POLL_RETRY_MAX;
220 schedule_timeout_killable(*timeout);
221 if (fatal_signal_pending(current))
222 res = -ERESTARTSYS;
223 *timeout <<= 1;
224 return res;
227 /* This is the error handling routine for processes that are allowed
228 * to sleep.
230 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
232 struct nfs_client *clp = server->nfs_client;
233 struct nfs4_state *state = exception->state;
234 int ret = errorcode;
236 exception->retry = 0;
237 switch(errorcode) {
238 case 0:
239 return 0;
240 case -NFS4ERR_ADMIN_REVOKED:
241 case -NFS4ERR_BAD_STATEID:
242 case -NFS4ERR_OPENMODE:
243 if (state == NULL)
244 break;
245 nfs4_state_mark_reclaim_nograce(clp, state);
246 case -NFS4ERR_STALE_CLIENTID:
247 case -NFS4ERR_STALE_STATEID:
248 case -NFS4ERR_EXPIRED:
249 nfs4_schedule_state_recovery(clp);
250 ret = nfs4_wait_clnt_recover(clp);
251 if (ret == 0)
252 exception->retry = 1;
253 #if !defined(CONFIG_NFS_V4_1)
254 break;
255 #else /* !defined(CONFIG_NFS_V4_1) */
256 if (!nfs4_has_session(server->nfs_client))
257 break;
258 /* FALLTHROUGH */
259 case -NFS4ERR_BADSESSION:
260 case -NFS4ERR_BADSLOT:
261 case -NFS4ERR_BAD_HIGH_SLOT:
262 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
263 case -NFS4ERR_DEADSESSION:
264 case -NFS4ERR_SEQ_FALSE_RETRY:
265 case -NFS4ERR_SEQ_MISORDERED:
266 dprintk("%s ERROR: %d Reset session\n", __func__,
267 errorcode);
268 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
269 exception->retry = 1;
270 /* FALLTHROUGH */
271 #endif /* !defined(CONFIG_NFS_V4_1) */
272 case -NFS4ERR_FILE_OPEN:
273 case -NFS4ERR_GRACE:
274 case -NFS4ERR_DELAY:
275 ret = nfs4_delay(server->client, &exception->timeout);
276 if (ret != 0)
277 break;
278 case -NFS4ERR_OLD_STATEID:
279 exception->retry = 1;
281 /* We failed to handle the error */
282 return nfs4_map_errors(ret);
286 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
288 struct nfs_client *clp = server->nfs_client;
289 spin_lock(&clp->cl_lock);
290 if (time_before(clp->cl_last_renewal,timestamp))
291 clp->cl_last_renewal = timestamp;
292 spin_unlock(&clp->cl_lock);
295 #if defined(CONFIG_NFS_V4_1)
298 * nfs4_free_slot - free a slot and efficiently update slot table.
300 * freeing a slot is trivially done by clearing its respective bit
301 * in the bitmap.
302 * If the freed slotid equals highest_used_slotid we want to update it
303 * so that the server would be able to size down the slot table if needed,
304 * otherwise we know that the highest_used_slotid is still in use.
305 * When updating highest_used_slotid there may be "holes" in the bitmap
306 * so we need to scan down from highest_used_slotid to 0 looking for the now
307 * highest slotid in use.
308 * If none found, highest_used_slotid is set to -1.
310 static void
311 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
313 int slotid = free_slotid;
315 spin_lock(&tbl->slot_tbl_lock);
316 /* clear used bit in bitmap */
317 __clear_bit(slotid, tbl->used_slots);
319 /* update highest_used_slotid when it is freed */
320 if (slotid == tbl->highest_used_slotid) {
321 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
322 if (slotid >= 0 && slotid < tbl->max_slots)
323 tbl->highest_used_slotid = slotid;
324 else
325 tbl->highest_used_slotid = -1;
327 rpc_wake_up_next(&tbl->slot_tbl_waitq);
328 spin_unlock(&tbl->slot_tbl_lock);
329 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
330 free_slotid, tbl->highest_used_slotid);
333 void nfs41_sequence_free_slot(const struct nfs_client *clp,
334 struct nfs4_sequence_res *res)
336 struct nfs4_slot_table *tbl;
338 if (!nfs4_has_session(clp)) {
339 dprintk("%s: No session\n", __func__);
340 return;
342 tbl = &clp->cl_session->fc_slot_table;
343 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
344 dprintk("%s: No slot\n", __func__);
345 /* just wake up the next guy waiting since
346 * we may have not consumed a slot after all */
347 rpc_wake_up_next(&tbl->slot_tbl_waitq);
348 return;
350 nfs4_free_slot(tbl, res->sr_slotid);
351 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
354 static void nfs41_sequence_done(struct nfs_client *clp,
355 struct nfs4_sequence_res *res,
356 int rpc_status)
358 unsigned long timestamp;
359 struct nfs4_slot_table *tbl;
360 struct nfs4_slot *slot;
363 * sr_status remains 1 if an RPC level error occurred. The server
364 * may or may not have processed the sequence operation..
365 * Proceed as if the server received and processed the sequence
366 * operation.
368 if (res->sr_status == 1)
369 res->sr_status = NFS_OK;
371 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
372 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
373 goto out;
375 tbl = &clp->cl_session->fc_slot_table;
376 slot = tbl->slots + res->sr_slotid;
378 if (res->sr_status == 0) {
379 /* Update the slot's sequence and clientid lease timer */
380 ++slot->seq_nr;
381 timestamp = res->sr_renewal_time;
382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal, timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
386 return;
388 out:
389 /* The session may be reset by one of the error handlers. */
390 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
391 nfs41_sequence_free_slot(clp, res);
395 * nfs4_find_slot - efficiently look for a free slot
397 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
398 * If found, we mark the slot as used, update the highest_used_slotid,
399 * and respectively set up the sequence operation args.
400 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
402 * Note: must be called with under the slot_tbl_lock.
404 static u8
405 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
407 int slotid;
408 u8 ret_id = NFS4_MAX_SLOT_TABLE;
409 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
411 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
412 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
413 tbl->max_slots);
414 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
415 if (slotid >= tbl->max_slots)
416 goto out;
417 __set_bit(slotid, tbl->used_slots);
418 if (slotid > tbl->highest_used_slotid)
419 tbl->highest_used_slotid = slotid;
420 ret_id = slotid;
421 out:
422 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
423 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
424 return ret_id;
427 static int nfs4_recover_session(struct nfs4_session *session)
429 struct nfs_client *clp = session->clp;
430 unsigned int loop;
431 int ret;
433 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
434 ret = nfs4_wait_clnt_recover(clp);
435 if (ret != 0)
436 break;
437 if (!test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
438 break;
439 nfs4_schedule_state_manager(clp);
440 ret = -EIO;
442 return ret;
445 static int nfs41_setup_sequence(struct nfs4_session *session,
446 struct nfs4_sequence_args *args,
447 struct nfs4_sequence_res *res,
448 int cache_reply,
449 struct rpc_task *task)
451 struct nfs4_slot *slot;
452 struct nfs4_slot_table *tbl;
453 int status = 0;
454 u8 slotid;
456 dprintk("--> %s\n", __func__);
457 /* slot already allocated? */
458 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
459 return 0;
461 memset(res, 0, sizeof(*res));
462 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
463 tbl = &session->fc_slot_table;
465 spin_lock(&tbl->slot_tbl_lock);
466 if (test_bit(NFS4CLNT_SESSION_SETUP, &session->clp->cl_state)) {
467 if (tbl->highest_used_slotid != -1) {
468 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
469 spin_unlock(&tbl->slot_tbl_lock);
470 dprintk("<-- %s: Session reset: draining\n", __func__);
471 return -EAGAIN;
474 /* The slot table is empty; start the reset thread */
475 dprintk("%s Session Reset\n", __func__);
476 spin_unlock(&tbl->slot_tbl_lock);
477 status = nfs4_recover_session(session);
478 if (status)
479 return status;
480 spin_lock(&tbl->slot_tbl_lock);
483 slotid = nfs4_find_slot(tbl, task);
484 if (slotid == NFS4_MAX_SLOT_TABLE) {
485 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
486 spin_unlock(&tbl->slot_tbl_lock);
487 dprintk("<-- %s: no free slots\n", __func__);
488 return -EAGAIN;
490 spin_unlock(&tbl->slot_tbl_lock);
492 slot = tbl->slots + slotid;
493 args->sa_session = session;
494 args->sa_slotid = slotid;
495 args->sa_cache_this = cache_reply;
497 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
499 res->sr_session = session;
500 res->sr_slotid = slotid;
501 res->sr_renewal_time = jiffies;
503 * sr_status is only set in decode_sequence, and so will remain
504 * set to 1 if an rpc level failure occurs.
506 res->sr_status = 1;
507 return 0;
510 int nfs4_setup_sequence(struct nfs_client *clp,
511 struct nfs4_sequence_args *args,
512 struct nfs4_sequence_res *res,
513 int cache_reply,
514 struct rpc_task *task)
516 int ret = 0;
518 dprintk("--> %s clp %p session %p sr_slotid %d\n",
519 __func__, clp, clp->cl_session, res->sr_slotid);
521 if (!nfs4_has_session(clp))
522 goto out;
523 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
524 task);
525 if (ret != -EAGAIN) {
526 /* terminate rpc task */
527 task->tk_status = ret;
528 task->tk_action = NULL;
530 out:
531 dprintk("<-- %s status=%d\n", __func__, ret);
532 return ret;
535 struct nfs41_call_sync_data {
536 struct nfs_client *clp;
537 struct nfs4_sequence_args *seq_args;
538 struct nfs4_sequence_res *seq_res;
539 int cache_reply;
542 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
544 struct nfs41_call_sync_data *data = calldata;
546 dprintk("--> %s data->clp->cl_session %p\n", __func__,
547 data->clp->cl_session);
548 if (nfs4_setup_sequence(data->clp, data->seq_args,
549 data->seq_res, data->cache_reply, task))
550 return;
551 rpc_call_start(task);
554 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
556 struct nfs41_call_sync_data *data = calldata;
558 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
559 nfs41_sequence_free_slot(data->clp, data->seq_res);
562 struct rpc_call_ops nfs41_call_sync_ops = {
563 .rpc_call_prepare = nfs41_call_sync_prepare,
564 .rpc_call_done = nfs41_call_sync_done,
567 static int nfs4_call_sync_sequence(struct nfs_client *clp,
568 struct rpc_clnt *clnt,
569 struct rpc_message *msg,
570 struct nfs4_sequence_args *args,
571 struct nfs4_sequence_res *res,
572 int cache_reply)
574 int ret;
575 struct rpc_task *task;
576 struct nfs41_call_sync_data data = {
577 .clp = clp,
578 .seq_args = args,
579 .seq_res = res,
580 .cache_reply = cache_reply,
582 struct rpc_task_setup task_setup = {
583 .rpc_client = clnt,
584 .rpc_message = msg,
585 .callback_ops = &nfs41_call_sync_ops,
586 .callback_data = &data
589 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
590 task = rpc_run_task(&task_setup);
591 if (IS_ERR(task))
592 ret = PTR_ERR(task);
593 else {
594 ret = task->tk_status;
595 rpc_put_task(task);
597 return ret;
600 int _nfs4_call_sync_session(struct nfs_server *server,
601 struct rpc_message *msg,
602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
604 int cache_reply)
606 return nfs4_call_sync_sequence(server->nfs_client, server->client,
607 msg, args, res, cache_reply);
610 #endif /* CONFIG_NFS_V4_1 */
612 int _nfs4_call_sync(struct nfs_server *server,
613 struct rpc_message *msg,
614 struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res,
616 int cache_reply)
618 args->sa_session = res->sr_session = NULL;
619 return rpc_call_sync(server->client, msg, 0);
622 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
623 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
624 &(res)->seq_res, (cache_reply))
626 static void nfs4_sequence_done(const struct nfs_server *server,
627 struct nfs4_sequence_res *res, int rpc_status)
629 #ifdef CONFIG_NFS_V4_1
630 if (nfs4_has_session(server->nfs_client))
631 nfs41_sequence_done(server->nfs_client, res, rpc_status);
632 #endif /* CONFIG_NFS_V4_1 */
635 /* no restart, therefore free slot here */
636 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
637 struct nfs4_sequence_res *res,
638 int rpc_status)
640 nfs4_sequence_done(server, res, rpc_status);
641 nfs4_sequence_free_slot(server->nfs_client, res);
644 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
646 struct nfs_inode *nfsi = NFS_I(dir);
648 spin_lock(&dir->i_lock);
649 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
650 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
651 nfs_force_lookup_revalidate(dir);
652 nfsi->change_attr = cinfo->after;
653 spin_unlock(&dir->i_lock);
656 struct nfs4_opendata {
657 struct kref kref;
658 struct nfs_openargs o_arg;
659 struct nfs_openres o_res;
660 struct nfs_open_confirmargs c_arg;
661 struct nfs_open_confirmres c_res;
662 struct nfs_fattr f_attr;
663 struct nfs_fattr dir_attr;
664 struct path path;
665 struct dentry *dir;
666 struct nfs4_state_owner *owner;
667 struct nfs4_state *state;
668 struct iattr attrs;
669 unsigned long timestamp;
670 unsigned int rpc_done : 1;
671 int rpc_status;
672 int cancelled;
676 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
678 p->o_res.f_attr = &p->f_attr;
679 p->o_res.dir_attr = &p->dir_attr;
680 p->o_res.seqid = p->o_arg.seqid;
681 p->c_res.seqid = p->c_arg.seqid;
682 p->o_res.server = p->o_arg.server;
683 nfs_fattr_init(&p->f_attr);
684 nfs_fattr_init(&p->dir_attr);
685 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
688 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
689 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
690 const struct iattr *attrs)
692 struct dentry *parent = dget_parent(path->dentry);
693 struct inode *dir = parent->d_inode;
694 struct nfs_server *server = NFS_SERVER(dir);
695 struct nfs4_opendata *p;
697 p = kzalloc(sizeof(*p), GFP_KERNEL);
698 if (p == NULL)
699 goto err;
700 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
701 if (p->o_arg.seqid == NULL)
702 goto err_free;
703 p->path.mnt = mntget(path->mnt);
704 p->path.dentry = dget(path->dentry);
705 p->dir = parent;
706 p->owner = sp;
707 atomic_inc(&sp->so_count);
708 p->o_arg.fh = NFS_FH(dir);
709 p->o_arg.open_flags = flags;
710 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
711 p->o_arg.clientid = server->nfs_client->cl_clientid;
712 p->o_arg.id = sp->so_owner_id.id;
713 p->o_arg.name = &p->path.dentry->d_name;
714 p->o_arg.server = server;
715 p->o_arg.bitmask = server->attr_bitmask;
716 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
717 if (flags & O_EXCL) {
718 u32 *s = (u32 *) p->o_arg.u.verifier.data;
719 s[0] = jiffies;
720 s[1] = current->pid;
721 } else if (flags & O_CREAT) {
722 p->o_arg.u.attrs = &p->attrs;
723 memcpy(&p->attrs, attrs, sizeof(p->attrs));
725 p->c_arg.fh = &p->o_res.fh;
726 p->c_arg.stateid = &p->o_res.stateid;
727 p->c_arg.seqid = p->o_arg.seqid;
728 nfs4_init_opendata_res(p);
729 kref_init(&p->kref);
730 return p;
731 err_free:
732 kfree(p);
733 err:
734 dput(parent);
735 return NULL;
738 static void nfs4_opendata_free(struct kref *kref)
740 struct nfs4_opendata *p = container_of(kref,
741 struct nfs4_opendata, kref);
743 nfs_free_seqid(p->o_arg.seqid);
744 if (p->state != NULL)
745 nfs4_put_open_state(p->state);
746 nfs4_put_state_owner(p->owner);
747 dput(p->dir);
748 path_put(&p->path);
749 kfree(p);
752 static void nfs4_opendata_put(struct nfs4_opendata *p)
754 if (p != NULL)
755 kref_put(&p->kref, nfs4_opendata_free);
758 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
760 int ret;
762 ret = rpc_wait_for_completion_task(task);
763 return ret;
766 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
768 int ret = 0;
770 if (open_mode & O_EXCL)
771 goto out;
772 switch (mode & (FMODE_READ|FMODE_WRITE)) {
773 case FMODE_READ:
774 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
775 break;
776 case FMODE_WRITE:
777 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
778 break;
779 case FMODE_READ|FMODE_WRITE:
780 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
782 out:
783 return ret;
786 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
788 if ((delegation->type & fmode) != fmode)
789 return 0;
790 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
791 return 0;
792 nfs_mark_delegation_referenced(delegation);
793 return 1;
796 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
798 switch (fmode) {
799 case FMODE_WRITE:
800 state->n_wronly++;
801 break;
802 case FMODE_READ:
803 state->n_rdonly++;
804 break;
805 case FMODE_READ|FMODE_WRITE:
806 state->n_rdwr++;
808 nfs4_state_set_mode_locked(state, state->state | fmode);
811 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
813 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
814 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
815 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
816 switch (fmode) {
817 case FMODE_READ:
818 set_bit(NFS_O_RDONLY_STATE, &state->flags);
819 break;
820 case FMODE_WRITE:
821 set_bit(NFS_O_WRONLY_STATE, &state->flags);
822 break;
823 case FMODE_READ|FMODE_WRITE:
824 set_bit(NFS_O_RDWR_STATE, &state->flags);
828 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
830 write_seqlock(&state->seqlock);
831 nfs_set_open_stateid_locked(state, stateid, fmode);
832 write_sequnlock(&state->seqlock);
835 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
838 * Protect the call to nfs4_state_set_mode_locked and
839 * serialise the stateid update
841 write_seqlock(&state->seqlock);
842 if (deleg_stateid != NULL) {
843 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
844 set_bit(NFS_DELEGATED_STATE, &state->flags);
846 if (open_stateid != NULL)
847 nfs_set_open_stateid_locked(state, open_stateid, fmode);
848 write_sequnlock(&state->seqlock);
849 spin_lock(&state->owner->so_lock);
850 update_open_stateflags(state, fmode);
851 spin_unlock(&state->owner->so_lock);
854 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
856 struct nfs_inode *nfsi = NFS_I(state->inode);
857 struct nfs_delegation *deleg_cur;
858 int ret = 0;
860 fmode &= (FMODE_READ|FMODE_WRITE);
862 rcu_read_lock();
863 deleg_cur = rcu_dereference(nfsi->delegation);
864 if (deleg_cur == NULL)
865 goto no_delegation;
867 spin_lock(&deleg_cur->lock);
868 if (nfsi->delegation != deleg_cur ||
869 (deleg_cur->type & fmode) != fmode)
870 goto no_delegation_unlock;
872 if (delegation == NULL)
873 delegation = &deleg_cur->stateid;
874 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
875 goto no_delegation_unlock;
877 nfs_mark_delegation_referenced(deleg_cur);
878 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
879 ret = 1;
880 no_delegation_unlock:
881 spin_unlock(&deleg_cur->lock);
882 no_delegation:
883 rcu_read_unlock();
885 if (!ret && open_stateid != NULL) {
886 __update_open_stateid(state, open_stateid, NULL, fmode);
887 ret = 1;
890 return ret;
894 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
896 struct nfs_delegation *delegation;
898 rcu_read_lock();
899 delegation = rcu_dereference(NFS_I(inode)->delegation);
900 if (delegation == NULL || (delegation->type & fmode) == fmode) {
901 rcu_read_unlock();
902 return;
904 rcu_read_unlock();
905 nfs_inode_return_delegation(inode);
908 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
910 struct nfs4_state *state = opendata->state;
911 struct nfs_inode *nfsi = NFS_I(state->inode);
912 struct nfs_delegation *delegation;
913 int open_mode = opendata->o_arg.open_flags & O_EXCL;
914 fmode_t fmode = opendata->o_arg.fmode;
915 nfs4_stateid stateid;
916 int ret = -EAGAIN;
918 for (;;) {
919 if (can_open_cached(state, fmode, open_mode)) {
920 spin_lock(&state->owner->so_lock);
921 if (can_open_cached(state, fmode, open_mode)) {
922 update_open_stateflags(state, fmode);
923 spin_unlock(&state->owner->so_lock);
924 goto out_return_state;
926 spin_unlock(&state->owner->so_lock);
928 rcu_read_lock();
929 delegation = rcu_dereference(nfsi->delegation);
930 if (delegation == NULL ||
931 !can_open_delegated(delegation, fmode)) {
932 rcu_read_unlock();
933 break;
935 /* Save the delegation */
936 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
937 rcu_read_unlock();
938 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
939 if (ret != 0)
940 goto out;
941 ret = -EAGAIN;
943 /* Try to update the stateid using the delegation */
944 if (update_open_stateid(state, NULL, &stateid, fmode))
945 goto out_return_state;
947 out:
948 return ERR_PTR(ret);
949 out_return_state:
950 atomic_inc(&state->count);
951 return state;
954 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
956 struct inode *inode;
957 struct nfs4_state *state = NULL;
958 struct nfs_delegation *delegation;
959 int ret;
961 if (!data->rpc_done) {
962 state = nfs4_try_open_cached(data);
963 goto out;
966 ret = -EAGAIN;
967 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
968 goto err;
969 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
970 ret = PTR_ERR(inode);
971 if (IS_ERR(inode))
972 goto err;
973 ret = -ENOMEM;
974 state = nfs4_get_open_state(inode, data->owner);
975 if (state == NULL)
976 goto err_put_inode;
977 if (data->o_res.delegation_type != 0) {
978 int delegation_flags = 0;
980 rcu_read_lock();
981 delegation = rcu_dereference(NFS_I(inode)->delegation);
982 if (delegation)
983 delegation_flags = delegation->flags;
984 rcu_read_unlock();
985 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
986 nfs_inode_set_delegation(state->inode,
987 data->owner->so_cred,
988 &data->o_res);
989 else
990 nfs_inode_reclaim_delegation(state->inode,
991 data->owner->so_cred,
992 &data->o_res);
995 update_open_stateid(state, &data->o_res.stateid, NULL,
996 data->o_arg.fmode);
997 iput(inode);
998 out:
999 return state;
1000 err_put_inode:
1001 iput(inode);
1002 err:
1003 return ERR_PTR(ret);
1006 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1008 struct nfs_inode *nfsi = NFS_I(state->inode);
1009 struct nfs_open_context *ctx;
1011 spin_lock(&state->inode->i_lock);
1012 list_for_each_entry(ctx, &nfsi->open_files, list) {
1013 if (ctx->state != state)
1014 continue;
1015 get_nfs_open_context(ctx);
1016 spin_unlock(&state->inode->i_lock);
1017 return ctx;
1019 spin_unlock(&state->inode->i_lock);
1020 return ERR_PTR(-ENOENT);
1023 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1025 struct nfs4_opendata *opendata;
1027 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1028 if (opendata == NULL)
1029 return ERR_PTR(-ENOMEM);
1030 opendata->state = state;
1031 atomic_inc(&state->count);
1032 return opendata;
1035 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1037 struct nfs4_state *newstate;
1038 int ret;
1040 opendata->o_arg.open_flags = 0;
1041 opendata->o_arg.fmode = fmode;
1042 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1043 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1044 nfs4_init_opendata_res(opendata);
1045 ret = _nfs4_proc_open(opendata);
1046 if (ret != 0)
1047 return ret;
1048 newstate = nfs4_opendata_to_nfs4_state(opendata);
1049 if (IS_ERR(newstate))
1050 return PTR_ERR(newstate);
1051 nfs4_close_state(&opendata->path, newstate, fmode);
1052 *res = newstate;
1053 return 0;
1056 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1058 struct nfs4_state *newstate;
1059 int ret;
1061 /* memory barrier prior to reading state->n_* */
1062 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1063 smp_rmb();
1064 if (state->n_rdwr != 0) {
1065 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1066 if (ret != 0)
1067 return ret;
1068 if (newstate != state)
1069 return -ESTALE;
1071 if (state->n_wronly != 0) {
1072 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1073 if (ret != 0)
1074 return ret;
1075 if (newstate != state)
1076 return -ESTALE;
1078 if (state->n_rdonly != 0) {
1079 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1080 if (ret != 0)
1081 return ret;
1082 if (newstate != state)
1083 return -ESTALE;
1086 * We may have performed cached opens for all three recoveries.
1087 * Check if we need to update the current stateid.
1089 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1090 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1091 write_seqlock(&state->seqlock);
1092 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1093 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1094 write_sequnlock(&state->seqlock);
1096 return 0;
1100 * OPEN_RECLAIM:
1101 * reclaim state on the server after a reboot.
1103 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1105 struct nfs_delegation *delegation;
1106 struct nfs4_opendata *opendata;
1107 fmode_t delegation_type = 0;
1108 int status;
1110 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1111 if (IS_ERR(opendata))
1112 return PTR_ERR(opendata);
1113 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1114 opendata->o_arg.fh = NFS_FH(state->inode);
1115 rcu_read_lock();
1116 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1117 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1118 delegation_type = delegation->type;
1119 rcu_read_unlock();
1120 opendata->o_arg.u.delegation_type = delegation_type;
1121 status = nfs4_open_recover(opendata, state);
1122 nfs4_opendata_put(opendata);
1123 return status;
1126 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1128 struct nfs_server *server = NFS_SERVER(state->inode);
1129 struct nfs4_exception exception = { };
1130 int err;
1131 do {
1132 err = _nfs4_do_open_reclaim(ctx, state);
1133 if (err != -NFS4ERR_DELAY)
1134 break;
1135 nfs4_handle_exception(server, err, &exception);
1136 } while (exception.retry);
1137 return err;
1140 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1142 struct nfs_open_context *ctx;
1143 int ret;
1145 ctx = nfs4_state_find_open_context(state);
1146 if (IS_ERR(ctx))
1147 return PTR_ERR(ctx);
1148 ret = nfs4_do_open_reclaim(ctx, state);
1149 put_nfs_open_context(ctx);
1150 return ret;
1153 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1155 struct nfs4_opendata *opendata;
1156 int ret;
1158 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1159 if (IS_ERR(opendata))
1160 return PTR_ERR(opendata);
1161 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1162 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1163 sizeof(opendata->o_arg.u.delegation.data));
1164 ret = nfs4_open_recover(opendata, state);
1165 nfs4_opendata_put(opendata);
1166 return ret;
1169 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1171 struct nfs4_exception exception = { };
1172 struct nfs_server *server = NFS_SERVER(state->inode);
1173 int err;
1174 do {
1175 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1176 switch (err) {
1177 case 0:
1178 case -ENOENT:
1179 case -ESTALE:
1180 goto out;
1181 case -NFS4ERR_STALE_CLIENTID:
1182 case -NFS4ERR_STALE_STATEID:
1183 case -NFS4ERR_EXPIRED:
1184 /* Don't recall a delegation if it was lost */
1185 nfs4_schedule_state_recovery(server->nfs_client);
1186 goto out;
1187 case -ERESTARTSYS:
1189 * The show must go on: exit, but mark the
1190 * stateid as needing recovery.
1192 case -NFS4ERR_ADMIN_REVOKED:
1193 case -NFS4ERR_BAD_STATEID:
1194 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1195 case -ENOMEM:
1196 err = 0;
1197 goto out;
1199 err = nfs4_handle_exception(server, err, &exception);
1200 } while (exception.retry);
1201 out:
1202 return err;
1205 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1207 struct nfs4_opendata *data = calldata;
1209 data->rpc_status = task->tk_status;
1210 if (RPC_ASSASSINATED(task))
1211 return;
1212 if (data->rpc_status == 0) {
1213 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1214 sizeof(data->o_res.stateid.data));
1215 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1216 renew_lease(data->o_res.server, data->timestamp);
1217 data->rpc_done = 1;
1221 static void nfs4_open_confirm_release(void *calldata)
1223 struct nfs4_opendata *data = calldata;
1224 struct nfs4_state *state = NULL;
1226 /* If this request hasn't been cancelled, do nothing */
1227 if (data->cancelled == 0)
1228 goto out_free;
1229 /* In case of error, no cleanup! */
1230 if (!data->rpc_done)
1231 goto out_free;
1232 state = nfs4_opendata_to_nfs4_state(data);
1233 if (!IS_ERR(state))
1234 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1235 out_free:
1236 nfs4_opendata_put(data);
1239 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1240 .rpc_call_done = nfs4_open_confirm_done,
1241 .rpc_release = nfs4_open_confirm_release,
1245 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1247 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1249 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1250 struct rpc_task *task;
1251 struct rpc_message msg = {
1252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1253 .rpc_argp = &data->c_arg,
1254 .rpc_resp = &data->c_res,
1255 .rpc_cred = data->owner->so_cred,
1257 struct rpc_task_setup task_setup_data = {
1258 .rpc_client = server->client,
1259 .rpc_message = &msg,
1260 .callback_ops = &nfs4_open_confirm_ops,
1261 .callback_data = data,
1262 .workqueue = nfsiod_workqueue,
1263 .flags = RPC_TASK_ASYNC,
1265 int status;
1267 kref_get(&data->kref);
1268 data->rpc_done = 0;
1269 data->rpc_status = 0;
1270 data->timestamp = jiffies;
1271 task = rpc_run_task(&task_setup_data);
1272 if (IS_ERR(task))
1273 return PTR_ERR(task);
1274 status = nfs4_wait_for_completion_rpc_task(task);
1275 if (status != 0) {
1276 data->cancelled = 1;
1277 smp_wmb();
1278 } else
1279 status = data->rpc_status;
1280 rpc_put_task(task);
1281 return status;
1284 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1286 struct nfs4_opendata *data = calldata;
1287 struct nfs4_state_owner *sp = data->owner;
1289 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1290 return;
1292 * Check if we still need to send an OPEN call, or if we can use
1293 * a delegation instead.
1295 if (data->state != NULL) {
1296 struct nfs_delegation *delegation;
1298 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1299 goto out_no_action;
1300 rcu_read_lock();
1301 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1302 if (delegation != NULL &&
1303 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1304 rcu_read_unlock();
1305 goto out_no_action;
1307 rcu_read_unlock();
1309 /* Update sequence id. */
1310 data->o_arg.id = sp->so_owner_id.id;
1311 data->o_arg.clientid = sp->so_client->cl_clientid;
1312 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1313 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1314 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1316 data->timestamp = jiffies;
1317 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1318 &data->o_arg.seq_args,
1319 &data->o_res.seq_res, 1, task))
1320 return;
1321 rpc_call_start(task);
1322 return;
1323 out_no_action:
1324 task->tk_action = NULL;
1328 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1330 struct nfs4_opendata *data = calldata;
1332 data->rpc_status = task->tk_status;
1334 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1335 task->tk_status);
1337 if (RPC_ASSASSINATED(task))
1338 return;
1339 if (task->tk_status == 0) {
1340 switch (data->o_res.f_attr->mode & S_IFMT) {
1341 case S_IFREG:
1342 break;
1343 case S_IFLNK:
1344 data->rpc_status = -ELOOP;
1345 break;
1346 case S_IFDIR:
1347 data->rpc_status = -EISDIR;
1348 break;
1349 default:
1350 data->rpc_status = -ENOTDIR;
1352 renew_lease(data->o_res.server, data->timestamp);
1353 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1354 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1356 data->rpc_done = 1;
1359 static void nfs4_open_release(void *calldata)
1361 struct nfs4_opendata *data = calldata;
1362 struct nfs4_state *state = NULL;
1364 /* If this request hasn't been cancelled, do nothing */
1365 if (data->cancelled == 0)
1366 goto out_free;
1367 /* In case of error, no cleanup! */
1368 if (data->rpc_status != 0 || !data->rpc_done)
1369 goto out_free;
1370 /* In case we need an open_confirm, no cleanup! */
1371 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1372 goto out_free;
1373 state = nfs4_opendata_to_nfs4_state(data);
1374 if (!IS_ERR(state))
1375 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1376 out_free:
1377 nfs4_opendata_put(data);
1380 static const struct rpc_call_ops nfs4_open_ops = {
1381 .rpc_call_prepare = nfs4_open_prepare,
1382 .rpc_call_done = nfs4_open_done,
1383 .rpc_release = nfs4_open_release,
1387 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1389 static int _nfs4_proc_open(struct nfs4_opendata *data)
1391 struct inode *dir = data->dir->d_inode;
1392 struct nfs_server *server = NFS_SERVER(dir);
1393 struct nfs_openargs *o_arg = &data->o_arg;
1394 struct nfs_openres *o_res = &data->o_res;
1395 struct rpc_task *task;
1396 struct rpc_message msg = {
1397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1398 .rpc_argp = o_arg,
1399 .rpc_resp = o_res,
1400 .rpc_cred = data->owner->so_cred,
1402 struct rpc_task_setup task_setup_data = {
1403 .rpc_client = server->client,
1404 .rpc_message = &msg,
1405 .callback_ops = &nfs4_open_ops,
1406 .callback_data = data,
1407 .workqueue = nfsiod_workqueue,
1408 .flags = RPC_TASK_ASYNC,
1410 int status;
1412 kref_get(&data->kref);
1413 data->rpc_done = 0;
1414 data->rpc_status = 0;
1415 data->cancelled = 0;
1416 task = rpc_run_task(&task_setup_data);
1417 if (IS_ERR(task))
1418 return PTR_ERR(task);
1419 status = nfs4_wait_for_completion_rpc_task(task);
1420 if (status != 0) {
1421 data->cancelled = 1;
1422 smp_wmb();
1423 } else
1424 status = data->rpc_status;
1425 rpc_put_task(task);
1426 if (status != 0 || !data->rpc_done)
1427 return status;
1429 if (o_res->fh.size == 0)
1430 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1432 if (o_arg->open_flags & O_CREAT) {
1433 update_changeattr(dir, &o_res->cinfo);
1434 nfs_post_op_update_inode(dir, o_res->dir_attr);
1435 } else
1436 nfs_refresh_inode(dir, o_res->dir_attr);
1437 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1438 status = _nfs4_proc_open_confirm(data);
1439 if (status != 0)
1440 return status;
1442 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1443 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1444 return 0;
1447 static int nfs4_recover_expired_lease(struct nfs_server *server)
1449 struct nfs_client *clp = server->nfs_client;
1450 unsigned int loop;
1451 int ret;
1453 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1454 ret = nfs4_wait_clnt_recover(clp);
1455 if (ret != 0)
1456 break;
1457 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1458 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1459 break;
1460 nfs4_schedule_state_recovery(clp);
1461 ret = -EIO;
1463 return ret;
1467 * OPEN_EXPIRED:
1468 * reclaim state on the server after a network partition.
1469 * Assumes caller holds the appropriate lock
1471 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1473 struct nfs4_opendata *opendata;
1474 int ret;
1476 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1477 if (IS_ERR(opendata))
1478 return PTR_ERR(opendata);
1479 ret = nfs4_open_recover(opendata, state);
1480 if (ret == -ESTALE)
1481 d_drop(ctx->path.dentry);
1482 nfs4_opendata_put(opendata);
1483 return ret;
1486 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1488 struct nfs_server *server = NFS_SERVER(state->inode);
1489 struct nfs4_exception exception = { };
1490 int err;
1492 do {
1493 err = _nfs4_open_expired(ctx, state);
1494 if (err != -NFS4ERR_DELAY)
1495 break;
1496 nfs4_handle_exception(server, err, &exception);
1497 } while (exception.retry);
1498 return err;
1501 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1503 struct nfs_open_context *ctx;
1504 int ret;
1506 ctx = nfs4_state_find_open_context(state);
1507 if (IS_ERR(ctx))
1508 return PTR_ERR(ctx);
1509 ret = nfs4_do_open_expired(ctx, state);
1510 put_nfs_open_context(ctx);
1511 return ret;
1515 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1516 * fields corresponding to attributes that were used to store the verifier.
1517 * Make sure we clobber those fields in the later setattr call
1519 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1521 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1522 !(sattr->ia_valid & ATTR_ATIME_SET))
1523 sattr->ia_valid |= ATTR_ATIME;
1525 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1526 !(sattr->ia_valid & ATTR_MTIME_SET))
1527 sattr->ia_valid |= ATTR_MTIME;
1531 * Returns a referenced nfs4_state
1533 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)
1535 struct nfs4_state_owner *sp;
1536 struct nfs4_state *state = NULL;
1537 struct nfs_server *server = NFS_SERVER(dir);
1538 struct nfs4_opendata *opendata;
1539 int status;
1541 /* Protect against reboot recovery conflicts */
1542 status = -ENOMEM;
1543 if (!(sp = nfs4_get_state_owner(server, cred))) {
1544 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1545 goto out_err;
1547 status = nfs4_recover_expired_lease(server);
1548 if (status != 0)
1549 goto err_put_state_owner;
1550 if (path->dentry->d_inode != NULL)
1551 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1552 status = -ENOMEM;
1553 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1554 if (opendata == NULL)
1555 goto err_put_state_owner;
1557 if (path->dentry->d_inode != NULL)
1558 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1560 status = _nfs4_proc_open(opendata);
1561 if (status != 0)
1562 goto err_opendata_put;
1564 if (opendata->o_arg.open_flags & O_EXCL)
1565 nfs4_exclusive_attrset(opendata, sattr);
1567 state = nfs4_opendata_to_nfs4_state(opendata);
1568 status = PTR_ERR(state);
1569 if (IS_ERR(state))
1570 goto err_opendata_put;
1571 nfs4_opendata_put(opendata);
1572 nfs4_put_state_owner(sp);
1573 *res = state;
1574 return 0;
1575 err_opendata_put:
1576 nfs4_opendata_put(opendata);
1577 err_put_state_owner:
1578 nfs4_put_state_owner(sp);
1579 out_err:
1580 *res = NULL;
1581 return status;
1585 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)
1587 struct nfs4_exception exception = { };
1588 struct nfs4_state *res;
1589 int status;
1591 do {
1592 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1593 if (status == 0)
1594 break;
1595 /* NOTE: BAD_SEQID means the server and client disagree about the
1596 * book-keeping w.r.t. state-changing operations
1597 * (OPEN/CLOSE/LOCK/LOCKU...)
1598 * It is actually a sign of a bug on the client or on the server.
1600 * If we receive a BAD_SEQID error in the particular case of
1601 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1602 * have unhashed the old state_owner for us, and that we can
1603 * therefore safely retry using a new one. We should still warn
1604 * the user though...
1606 if (status == -NFS4ERR_BAD_SEQID) {
1607 printk(KERN_WARNING "NFS: v4 server %s "
1608 " returned a bad sequence-id error!\n",
1609 NFS_SERVER(dir)->nfs_client->cl_hostname);
1610 exception.retry = 1;
1611 continue;
1614 * BAD_STATEID on OPEN means that the server cancelled our
1615 * state before it received the OPEN_CONFIRM.
1616 * Recover by retrying the request as per the discussion
1617 * on Page 181 of RFC3530.
1619 if (status == -NFS4ERR_BAD_STATEID) {
1620 exception.retry = 1;
1621 continue;
1623 if (status == -EAGAIN) {
1624 /* We must have found a delegation */
1625 exception.retry = 1;
1626 continue;
1628 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1629 status, &exception));
1630 } while (exception.retry);
1631 return res;
1634 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1635 struct nfs_fattr *fattr, struct iattr *sattr,
1636 struct nfs4_state *state)
1638 struct nfs_server *server = NFS_SERVER(inode);
1639 struct nfs_setattrargs arg = {
1640 .fh = NFS_FH(inode),
1641 .iap = sattr,
1642 .server = server,
1643 .bitmask = server->attr_bitmask,
1645 struct nfs_setattrres res = {
1646 .fattr = fattr,
1647 .server = server,
1649 struct rpc_message msg = {
1650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1651 .rpc_argp = &arg,
1652 .rpc_resp = &res,
1653 .rpc_cred = cred,
1655 unsigned long timestamp = jiffies;
1656 int status;
1658 nfs_fattr_init(fattr);
1660 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1661 /* Use that stateid */
1662 } else if (state != NULL) {
1663 nfs4_copy_stateid(&arg.stateid, state, current->files);
1664 } else
1665 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1667 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1668 if (status == 0 && state != NULL)
1669 renew_lease(server, timestamp);
1670 return status;
1673 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1674 struct nfs_fattr *fattr, struct iattr *sattr,
1675 struct nfs4_state *state)
1677 struct nfs_server *server = NFS_SERVER(inode);
1678 struct nfs4_exception exception = { };
1679 int err;
1680 do {
1681 err = nfs4_handle_exception(server,
1682 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1683 &exception);
1684 } while (exception.retry);
1685 return err;
1688 struct nfs4_closedata {
1689 struct path path;
1690 struct inode *inode;
1691 struct nfs4_state *state;
1692 struct nfs_closeargs arg;
1693 struct nfs_closeres res;
1694 struct nfs_fattr fattr;
1695 unsigned long timestamp;
1698 static void nfs4_free_closedata(void *data)
1700 struct nfs4_closedata *calldata = data;
1701 struct nfs4_state_owner *sp = calldata->state->owner;
1703 nfs4_put_open_state(calldata->state);
1704 nfs_free_seqid(calldata->arg.seqid);
1705 nfs4_put_state_owner(sp);
1706 path_put(&calldata->path);
1707 kfree(calldata);
1710 static void nfs4_close_done(struct rpc_task *task, void *data)
1712 struct nfs4_closedata *calldata = data;
1713 struct nfs4_state *state = calldata->state;
1714 struct nfs_server *server = NFS_SERVER(calldata->inode);
1716 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1717 if (RPC_ASSASSINATED(task))
1718 return;
1719 /* hmm. we are done with the inode, and in the process of freeing
1720 * the state_owner. we keep this around to process errors
1722 switch (task->tk_status) {
1723 case 0:
1724 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1725 renew_lease(server, calldata->timestamp);
1726 break;
1727 case -NFS4ERR_STALE_STATEID:
1728 case -NFS4ERR_OLD_STATEID:
1729 case -NFS4ERR_BAD_STATEID:
1730 case -NFS4ERR_EXPIRED:
1731 if (calldata->arg.fmode == 0)
1732 break;
1733 default:
1734 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1735 nfs4_restart_rpc(task, server->nfs_client);
1736 return;
1739 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1740 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1743 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1745 struct nfs4_closedata *calldata = data;
1746 struct nfs4_state *state = calldata->state;
1747 int clear_rd, clear_wr, clear_rdwr;
1749 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1750 return;
1752 clear_rd = clear_wr = clear_rdwr = 0;
1753 spin_lock(&state->owner->so_lock);
1754 /* Calculate the change in open mode */
1755 if (state->n_rdwr == 0) {
1756 if (state->n_rdonly == 0) {
1757 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1758 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1760 if (state->n_wronly == 0) {
1761 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1762 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1765 spin_unlock(&state->owner->so_lock);
1766 if (!clear_rd && !clear_wr && !clear_rdwr) {
1767 /* Note: exit _without_ calling nfs4_close_done */
1768 task->tk_action = NULL;
1769 return;
1771 nfs_fattr_init(calldata->res.fattr);
1772 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1773 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1774 calldata->arg.fmode = FMODE_READ;
1775 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1776 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1777 calldata->arg.fmode = FMODE_WRITE;
1779 calldata->timestamp = jiffies;
1780 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1781 &calldata->arg.seq_args, &calldata->res.seq_res,
1782 1, task))
1783 return;
1784 rpc_call_start(task);
1787 static const struct rpc_call_ops nfs4_close_ops = {
1788 .rpc_call_prepare = nfs4_close_prepare,
1789 .rpc_call_done = nfs4_close_done,
1790 .rpc_release = nfs4_free_closedata,
1794 * It is possible for data to be read/written from a mem-mapped file
1795 * after the sys_close call (which hits the vfs layer as a flush).
1796 * This means that we can't safely call nfsv4 close on a file until
1797 * the inode is cleared. This in turn means that we are not good
1798 * NFSv4 citizens - we do not indicate to the server to update the file's
1799 * share state even when we are done with one of the three share
1800 * stateid's in the inode.
1802 * NOTE: Caller must be holding the sp->so_owner semaphore!
1804 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1806 struct nfs_server *server = NFS_SERVER(state->inode);
1807 struct nfs4_closedata *calldata;
1808 struct nfs4_state_owner *sp = state->owner;
1809 struct rpc_task *task;
1810 struct rpc_message msg = {
1811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1812 .rpc_cred = state->owner->so_cred,
1814 struct rpc_task_setup task_setup_data = {
1815 .rpc_client = server->client,
1816 .rpc_message = &msg,
1817 .callback_ops = &nfs4_close_ops,
1818 .workqueue = nfsiod_workqueue,
1819 .flags = RPC_TASK_ASYNC,
1821 int status = -ENOMEM;
1823 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1824 if (calldata == NULL)
1825 goto out;
1826 calldata->inode = state->inode;
1827 calldata->state = state;
1828 calldata->arg.fh = NFS_FH(state->inode);
1829 calldata->arg.stateid = &state->open_stateid;
1830 if (nfs4_has_session(server->nfs_client))
1831 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
1832 /* Serialization for the sequence id */
1833 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1834 if (calldata->arg.seqid == NULL)
1835 goto out_free_calldata;
1836 calldata->arg.fmode = 0;
1837 calldata->arg.bitmask = server->cache_consistency_bitmask;
1838 calldata->res.fattr = &calldata->fattr;
1839 calldata->res.seqid = calldata->arg.seqid;
1840 calldata->res.server = server;
1841 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1842 calldata->path.mnt = mntget(path->mnt);
1843 calldata->path.dentry = dget(path->dentry);
1845 msg.rpc_argp = &calldata->arg,
1846 msg.rpc_resp = &calldata->res,
1847 task_setup_data.callback_data = calldata;
1848 task = rpc_run_task(&task_setup_data);
1849 if (IS_ERR(task))
1850 return PTR_ERR(task);
1851 status = 0;
1852 if (wait)
1853 status = rpc_wait_for_completion_task(task);
1854 rpc_put_task(task);
1855 return status;
1856 out_free_calldata:
1857 kfree(calldata);
1858 out:
1859 nfs4_put_open_state(state);
1860 nfs4_put_state_owner(sp);
1861 return status;
1864 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1866 struct file *filp;
1867 int ret;
1869 /* If the open_intent is for execute, we have an extra check to make */
1870 if (fmode & FMODE_EXEC) {
1871 ret = nfs_may_open(state->inode,
1872 state->owner->so_cred,
1873 nd->intent.open.flags);
1874 if (ret < 0)
1875 goto out_close;
1877 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1878 if (!IS_ERR(filp)) {
1879 struct nfs_open_context *ctx;
1880 ctx = nfs_file_open_context(filp);
1881 ctx->state = state;
1882 return 0;
1884 ret = PTR_ERR(filp);
1885 out_close:
1886 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1887 return ret;
1890 struct dentry *
1891 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1893 struct path path = {
1894 .mnt = nd->path.mnt,
1895 .dentry = dentry,
1897 struct dentry *parent;
1898 struct iattr attr;
1899 struct rpc_cred *cred;
1900 struct nfs4_state *state;
1901 struct dentry *res;
1902 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1904 if (nd->flags & LOOKUP_CREATE) {
1905 attr.ia_mode = nd->intent.open.create_mode;
1906 attr.ia_valid = ATTR_MODE;
1907 if (!IS_POSIXACL(dir))
1908 attr.ia_mode &= ~current_umask();
1909 } else {
1910 attr.ia_valid = 0;
1911 BUG_ON(nd->intent.open.flags & O_CREAT);
1914 cred = rpc_lookup_cred();
1915 if (IS_ERR(cred))
1916 return (struct dentry *)cred;
1917 parent = dentry->d_parent;
1918 /* Protect against concurrent sillydeletes */
1919 nfs_block_sillyrename(parent);
1920 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1921 put_rpccred(cred);
1922 if (IS_ERR(state)) {
1923 if (PTR_ERR(state) == -ENOENT) {
1924 d_add(dentry, NULL);
1925 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1927 nfs_unblock_sillyrename(parent);
1928 return (struct dentry *)state;
1930 res = d_add_unique(dentry, igrab(state->inode));
1931 if (res != NULL)
1932 path.dentry = res;
1933 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1934 nfs_unblock_sillyrename(parent);
1935 nfs4_intent_set_file(nd, &path, state, fmode);
1936 return res;
1940 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1942 struct path path = {
1943 .mnt = nd->path.mnt,
1944 .dentry = dentry,
1946 struct rpc_cred *cred;
1947 struct nfs4_state *state;
1948 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1950 cred = rpc_lookup_cred();
1951 if (IS_ERR(cred))
1952 return PTR_ERR(cred);
1953 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1954 put_rpccred(cred);
1955 if (IS_ERR(state)) {
1956 switch (PTR_ERR(state)) {
1957 case -EPERM:
1958 case -EACCES:
1959 case -EDQUOT:
1960 case -ENOSPC:
1961 case -EROFS:
1962 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1963 return 1;
1964 default:
1965 goto out_drop;
1968 if (state->inode == dentry->d_inode) {
1969 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1970 nfs4_intent_set_file(nd, &path, state, fmode);
1971 return 1;
1973 nfs4_close_sync(&path, state, fmode);
1974 out_drop:
1975 d_drop(dentry);
1976 return 0;
1979 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1981 if (ctx->state == NULL)
1982 return;
1983 if (is_sync)
1984 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1985 else
1986 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1989 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1991 struct nfs4_server_caps_arg args = {
1992 .fhandle = fhandle,
1994 struct nfs4_server_caps_res res = {};
1995 struct rpc_message msg = {
1996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1997 .rpc_argp = &args,
1998 .rpc_resp = &res,
2000 int status;
2002 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2003 if (status == 0) {
2004 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2005 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2006 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2007 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2008 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2009 NFS_CAP_CTIME|NFS_CAP_MTIME);
2010 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2011 server->caps |= NFS_CAP_ACLS;
2012 if (res.has_links != 0)
2013 server->caps |= NFS_CAP_HARDLINKS;
2014 if (res.has_symlinks != 0)
2015 server->caps |= NFS_CAP_SYMLINKS;
2016 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2017 server->caps |= NFS_CAP_FILEID;
2018 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2019 server->caps |= NFS_CAP_MODE;
2020 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2021 server->caps |= NFS_CAP_NLINK;
2022 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2023 server->caps |= NFS_CAP_OWNER;
2024 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2025 server->caps |= NFS_CAP_OWNER_GROUP;
2026 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2027 server->caps |= NFS_CAP_ATIME;
2028 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2029 server->caps |= NFS_CAP_CTIME;
2030 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2031 server->caps |= NFS_CAP_MTIME;
2033 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2034 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2035 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2036 server->acl_bitmask = res.acl_bitmask;
2039 return status;
2042 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2044 struct nfs4_exception exception = { };
2045 int err;
2046 do {
2047 err = nfs4_handle_exception(server,
2048 _nfs4_server_capabilities(server, fhandle),
2049 &exception);
2050 } while (exception.retry);
2051 return err;
2054 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2055 struct nfs_fsinfo *info)
2057 struct nfs4_lookup_root_arg args = {
2058 .bitmask = nfs4_fattr_bitmap,
2060 struct nfs4_lookup_res res = {
2061 .server = server,
2062 .fattr = info->fattr,
2063 .fh = fhandle,
2065 struct rpc_message msg = {
2066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2067 .rpc_argp = &args,
2068 .rpc_resp = &res,
2071 nfs_fattr_init(info->fattr);
2072 return nfs4_call_sync(server, &msg, &args, &res, 0);
2075 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2076 struct nfs_fsinfo *info)
2078 struct nfs4_exception exception = { };
2079 int err;
2080 do {
2081 err = nfs4_handle_exception(server,
2082 _nfs4_lookup_root(server, fhandle, info),
2083 &exception);
2084 } while (exception.retry);
2085 return err;
2089 * get the file handle for the "/" directory on the server
2091 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2092 struct nfs_fsinfo *info)
2094 int status;
2096 status = nfs4_lookup_root(server, fhandle, info);
2097 if (status == 0)
2098 status = nfs4_server_capabilities(server, fhandle);
2099 if (status == 0)
2100 status = nfs4_do_fsinfo(server, fhandle, info);
2101 return nfs4_map_errors(status);
2105 * Get locations and (maybe) other attributes of a referral.
2106 * Note that we'll actually follow the referral later when
2107 * we detect fsid mismatch in inode revalidation
2109 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2111 int status = -ENOMEM;
2112 struct page *page = NULL;
2113 struct nfs4_fs_locations *locations = NULL;
2115 page = alloc_page(GFP_KERNEL);
2116 if (page == NULL)
2117 goto out;
2118 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2119 if (locations == NULL)
2120 goto out;
2122 status = nfs4_proc_fs_locations(dir, name, locations, page);
2123 if (status != 0)
2124 goto out;
2125 /* Make sure server returned a different fsid for the referral */
2126 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2127 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2128 status = -EIO;
2129 goto out;
2132 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2133 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2134 if (!fattr->mode)
2135 fattr->mode = S_IFDIR;
2136 memset(fhandle, 0, sizeof(struct nfs_fh));
2137 out:
2138 if (page)
2139 __free_page(page);
2140 if (locations)
2141 kfree(locations);
2142 return status;
2145 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2147 struct nfs4_getattr_arg args = {
2148 .fh = fhandle,
2149 .bitmask = server->attr_bitmask,
2151 struct nfs4_getattr_res res = {
2152 .fattr = fattr,
2153 .server = server,
2155 struct rpc_message msg = {
2156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2157 .rpc_argp = &args,
2158 .rpc_resp = &res,
2161 nfs_fattr_init(fattr);
2162 return nfs4_call_sync(server, &msg, &args, &res, 0);
2165 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
2170 err = nfs4_handle_exception(server,
2171 _nfs4_proc_getattr(server, fhandle, fattr),
2172 &exception);
2173 } while (exception.retry);
2174 return err;
2178 * The file is not closed if it is opened due to the a request to change
2179 * the size of the file. The open call will not be needed once the
2180 * VFS layer lookup-intents are implemented.
2182 * Close is called when the inode is destroyed.
2183 * If we haven't opened the file for O_WRONLY, we
2184 * need to in the size_change case to obtain a stateid.
2186 * Got race?
2187 * Because OPEN is always done by name in nfsv4, it is
2188 * possible that we opened a different file by the same
2189 * name. We can recognize this race condition, but we
2190 * can't do anything about it besides returning an error.
2192 * This will be fixed with VFS changes (lookup-intent).
2194 static int
2195 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2196 struct iattr *sattr)
2198 struct inode *inode = dentry->d_inode;
2199 struct rpc_cred *cred = NULL;
2200 struct nfs4_state *state = NULL;
2201 int status;
2203 nfs_fattr_init(fattr);
2205 /* Search for an existing open(O_WRITE) file */
2206 if (sattr->ia_valid & ATTR_FILE) {
2207 struct nfs_open_context *ctx;
2209 ctx = nfs_file_open_context(sattr->ia_file);
2210 if (ctx) {
2211 cred = ctx->cred;
2212 state = ctx->state;
2216 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2217 if (status == 0)
2218 nfs_setattr_update_inode(inode, sattr);
2219 return status;
2222 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2223 const struct qstr *name, struct nfs_fh *fhandle,
2224 struct nfs_fattr *fattr)
2226 int status;
2227 struct nfs4_lookup_arg args = {
2228 .bitmask = server->attr_bitmask,
2229 .dir_fh = dirfh,
2230 .name = name,
2232 struct nfs4_lookup_res res = {
2233 .server = server,
2234 .fattr = fattr,
2235 .fh = fhandle,
2237 struct rpc_message msg = {
2238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2239 .rpc_argp = &args,
2240 .rpc_resp = &res,
2243 nfs_fattr_init(fattr);
2245 dprintk("NFS call lookupfh %s\n", name->name);
2246 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2247 dprintk("NFS reply lookupfh: %d\n", status);
2248 return status;
2251 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2252 struct qstr *name, struct nfs_fh *fhandle,
2253 struct nfs_fattr *fattr)
2255 struct nfs4_exception exception = { };
2256 int err;
2257 do {
2258 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2259 /* FIXME: !!!! */
2260 if (err == -NFS4ERR_MOVED) {
2261 err = -EREMOTE;
2262 break;
2264 err = nfs4_handle_exception(server, err, &exception);
2265 } while (exception.retry);
2266 return err;
2269 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2270 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2272 int status;
2274 dprintk("NFS call lookup %s\n", name->name);
2275 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2276 if (status == -NFS4ERR_MOVED)
2277 status = nfs4_get_referral(dir, name, fattr, fhandle);
2278 dprintk("NFS reply lookup: %d\n", status);
2279 return status;
2282 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2284 struct nfs4_exception exception = { };
2285 int err;
2286 do {
2287 err = nfs4_handle_exception(NFS_SERVER(dir),
2288 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2289 &exception);
2290 } while (exception.retry);
2291 return err;
2294 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2296 struct nfs_server *server = NFS_SERVER(inode);
2297 struct nfs_fattr fattr;
2298 struct nfs4_accessargs args = {
2299 .fh = NFS_FH(inode),
2300 .bitmask = server->attr_bitmask,
2302 struct nfs4_accessres res = {
2303 .server = server,
2304 .fattr = &fattr,
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2308 .rpc_argp = &args,
2309 .rpc_resp = &res,
2310 .rpc_cred = entry->cred,
2312 int mode = entry->mask;
2313 int status;
2316 * Determine which access bits we want to ask for...
2318 if (mode & MAY_READ)
2319 args.access |= NFS4_ACCESS_READ;
2320 if (S_ISDIR(inode->i_mode)) {
2321 if (mode & MAY_WRITE)
2322 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2323 if (mode & MAY_EXEC)
2324 args.access |= NFS4_ACCESS_LOOKUP;
2325 } else {
2326 if (mode & MAY_WRITE)
2327 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2328 if (mode & MAY_EXEC)
2329 args.access |= NFS4_ACCESS_EXECUTE;
2331 nfs_fattr_init(&fattr);
2332 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2333 if (!status) {
2334 entry->mask = 0;
2335 if (res.access & NFS4_ACCESS_READ)
2336 entry->mask |= MAY_READ;
2337 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2338 entry->mask |= MAY_WRITE;
2339 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2340 entry->mask |= MAY_EXEC;
2341 nfs_refresh_inode(inode, &fattr);
2343 return status;
2346 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2348 struct nfs4_exception exception = { };
2349 int err;
2350 do {
2351 err = nfs4_handle_exception(NFS_SERVER(inode),
2352 _nfs4_proc_access(inode, entry),
2353 &exception);
2354 } while (exception.retry);
2355 return err;
2359 * TODO: For the time being, we don't try to get any attributes
2360 * along with any of the zero-copy operations READ, READDIR,
2361 * READLINK, WRITE.
2363 * In the case of the first three, we want to put the GETATTR
2364 * after the read-type operation -- this is because it is hard
2365 * to predict the length of a GETATTR response in v4, and thus
2366 * align the READ data correctly. This means that the GETATTR
2367 * may end up partially falling into the page cache, and we should
2368 * shift it into the 'tail' of the xdr_buf before processing.
2369 * To do this efficiently, we need to know the total length
2370 * of data received, which doesn't seem to be available outside
2371 * of the RPC layer.
2373 * In the case of WRITE, we also want to put the GETATTR after
2374 * the operation -- in this case because we want to make sure
2375 * we get the post-operation mtime and size. This means that
2376 * we can't use xdr_encode_pages() as written: we need a variant
2377 * of it which would leave room in the 'tail' iovec.
2379 * Both of these changes to the XDR layer would in fact be quite
2380 * minor, but I decided to leave them for a subsequent patch.
2382 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2383 unsigned int pgbase, unsigned int pglen)
2385 struct nfs4_readlink args = {
2386 .fh = NFS_FH(inode),
2387 .pgbase = pgbase,
2388 .pglen = pglen,
2389 .pages = &page,
2391 struct nfs4_readlink_res res;
2392 struct rpc_message msg = {
2393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2394 .rpc_argp = &args,
2395 .rpc_resp = &res,
2398 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2401 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2402 unsigned int pgbase, unsigned int pglen)
2404 struct nfs4_exception exception = { };
2405 int err;
2406 do {
2407 err = nfs4_handle_exception(NFS_SERVER(inode),
2408 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2409 &exception);
2410 } while (exception.retry);
2411 return err;
2415 * Got race?
2416 * We will need to arrange for the VFS layer to provide an atomic open.
2417 * Until then, this create/open method is prone to inefficiency and race
2418 * conditions due to the lookup, create, and open VFS calls from sys_open()
2419 * placed on the wire.
2421 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2422 * The file will be opened again in the subsequent VFS open call
2423 * (nfs4_proc_file_open).
2425 * The open for read will just hang around to be used by any process that
2426 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2429 static int
2430 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2431 int flags, struct nameidata *nd)
2433 struct path path = {
2434 .mnt = nd->path.mnt,
2435 .dentry = dentry,
2437 struct nfs4_state *state;
2438 struct rpc_cred *cred;
2439 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2440 int status = 0;
2442 cred = rpc_lookup_cred();
2443 if (IS_ERR(cred)) {
2444 status = PTR_ERR(cred);
2445 goto out;
2447 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2448 d_drop(dentry);
2449 if (IS_ERR(state)) {
2450 status = PTR_ERR(state);
2451 goto out_putcred;
2453 d_add(dentry, igrab(state->inode));
2454 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2455 if (flags & O_EXCL) {
2456 struct nfs_fattr fattr;
2457 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2458 if (status == 0)
2459 nfs_setattr_update_inode(state->inode, sattr);
2460 nfs_post_op_update_inode(state->inode, &fattr);
2462 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2463 status = nfs4_intent_set_file(nd, &path, state, fmode);
2464 else
2465 nfs4_close_sync(&path, state, fmode);
2466 out_putcred:
2467 put_rpccred(cred);
2468 out:
2469 return status;
2472 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2474 struct nfs_server *server = NFS_SERVER(dir);
2475 struct nfs_removeargs args = {
2476 .fh = NFS_FH(dir),
2477 .name.len = name->len,
2478 .name.name = name->name,
2479 .bitmask = server->attr_bitmask,
2481 struct nfs_removeres res = {
2482 .server = server,
2484 struct rpc_message msg = {
2485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2486 .rpc_argp = &args,
2487 .rpc_resp = &res,
2489 int status;
2491 nfs_fattr_init(&res.dir_attr);
2492 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2493 if (status == 0) {
2494 update_changeattr(dir, &res.cinfo);
2495 nfs_post_op_update_inode(dir, &res.dir_attr);
2497 return status;
2500 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2502 struct nfs4_exception exception = { };
2503 int err;
2504 do {
2505 err = nfs4_handle_exception(NFS_SERVER(dir),
2506 _nfs4_proc_remove(dir, name),
2507 &exception);
2508 } while (exception.retry);
2509 return err;
2512 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2514 struct nfs_server *server = NFS_SERVER(dir);
2515 struct nfs_removeargs *args = msg->rpc_argp;
2516 struct nfs_removeres *res = msg->rpc_resp;
2518 args->bitmask = server->cache_consistency_bitmask;
2519 res->server = server;
2520 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2523 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2525 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2527 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2528 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2529 return 0;
2530 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2531 update_changeattr(dir, &res->cinfo);
2532 nfs_post_op_update_inode(dir, &res->dir_attr);
2533 return 1;
2536 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2537 struct inode *new_dir, struct qstr *new_name)
2539 struct nfs_server *server = NFS_SERVER(old_dir);
2540 struct nfs4_rename_arg arg = {
2541 .old_dir = NFS_FH(old_dir),
2542 .new_dir = NFS_FH(new_dir),
2543 .old_name = old_name,
2544 .new_name = new_name,
2545 .bitmask = server->attr_bitmask,
2547 struct nfs_fattr old_fattr, new_fattr;
2548 struct nfs4_rename_res res = {
2549 .server = server,
2550 .old_fattr = &old_fattr,
2551 .new_fattr = &new_fattr,
2553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2555 .rpc_argp = &arg,
2556 .rpc_resp = &res,
2558 int status;
2560 nfs_fattr_init(res.old_fattr);
2561 nfs_fattr_init(res.new_fattr);
2562 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2564 if (!status) {
2565 update_changeattr(old_dir, &res.old_cinfo);
2566 nfs_post_op_update_inode(old_dir, res.old_fattr);
2567 update_changeattr(new_dir, &res.new_cinfo);
2568 nfs_post_op_update_inode(new_dir, res.new_fattr);
2570 return status;
2573 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2574 struct inode *new_dir, struct qstr *new_name)
2576 struct nfs4_exception exception = { };
2577 int err;
2578 do {
2579 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2580 _nfs4_proc_rename(old_dir, old_name,
2581 new_dir, new_name),
2582 &exception);
2583 } while (exception.retry);
2584 return err;
2587 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2589 struct nfs_server *server = NFS_SERVER(inode);
2590 struct nfs4_link_arg arg = {
2591 .fh = NFS_FH(inode),
2592 .dir_fh = NFS_FH(dir),
2593 .name = name,
2594 .bitmask = server->attr_bitmask,
2596 struct nfs_fattr fattr, dir_attr;
2597 struct nfs4_link_res res = {
2598 .server = server,
2599 .fattr = &fattr,
2600 .dir_attr = &dir_attr,
2602 struct rpc_message msg = {
2603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2604 .rpc_argp = &arg,
2605 .rpc_resp = &res,
2607 int status;
2609 nfs_fattr_init(res.fattr);
2610 nfs_fattr_init(res.dir_attr);
2611 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2612 if (!status) {
2613 update_changeattr(dir, &res.cinfo);
2614 nfs_post_op_update_inode(dir, res.dir_attr);
2615 nfs_post_op_update_inode(inode, res.fattr);
2618 return status;
2621 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2623 struct nfs4_exception exception = { };
2624 int err;
2625 do {
2626 err = nfs4_handle_exception(NFS_SERVER(inode),
2627 _nfs4_proc_link(inode, dir, name),
2628 &exception);
2629 } while (exception.retry);
2630 return err;
2633 struct nfs4_createdata {
2634 struct rpc_message msg;
2635 struct nfs4_create_arg arg;
2636 struct nfs4_create_res res;
2637 struct nfs_fh fh;
2638 struct nfs_fattr fattr;
2639 struct nfs_fattr dir_fattr;
2642 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2643 struct qstr *name, struct iattr *sattr, u32 ftype)
2645 struct nfs4_createdata *data;
2647 data = kzalloc(sizeof(*data), GFP_KERNEL);
2648 if (data != NULL) {
2649 struct nfs_server *server = NFS_SERVER(dir);
2651 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2652 data->msg.rpc_argp = &data->arg;
2653 data->msg.rpc_resp = &data->res;
2654 data->arg.dir_fh = NFS_FH(dir);
2655 data->arg.server = server;
2656 data->arg.name = name;
2657 data->arg.attrs = sattr;
2658 data->arg.ftype = ftype;
2659 data->arg.bitmask = server->attr_bitmask;
2660 data->res.server = server;
2661 data->res.fh = &data->fh;
2662 data->res.fattr = &data->fattr;
2663 data->res.dir_fattr = &data->dir_fattr;
2664 nfs_fattr_init(data->res.fattr);
2665 nfs_fattr_init(data->res.dir_fattr);
2667 return data;
2670 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2672 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2673 &data->arg, &data->res, 1);
2674 if (status == 0) {
2675 update_changeattr(dir, &data->res.dir_cinfo);
2676 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2677 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2679 return status;
2682 static void nfs4_free_createdata(struct nfs4_createdata *data)
2684 kfree(data);
2687 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2688 struct page *page, unsigned int len, struct iattr *sattr)
2690 struct nfs4_createdata *data;
2691 int status = -ENAMETOOLONG;
2693 if (len > NFS4_MAXPATHLEN)
2694 goto out;
2696 status = -ENOMEM;
2697 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2698 if (data == NULL)
2699 goto out;
2701 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2702 data->arg.u.symlink.pages = &page;
2703 data->arg.u.symlink.len = len;
2705 status = nfs4_do_create(dir, dentry, data);
2707 nfs4_free_createdata(data);
2708 out:
2709 return status;
2712 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2713 struct page *page, unsigned int len, struct iattr *sattr)
2715 struct nfs4_exception exception = { };
2716 int err;
2717 do {
2718 err = nfs4_handle_exception(NFS_SERVER(dir),
2719 _nfs4_proc_symlink(dir, dentry, page,
2720 len, sattr),
2721 &exception);
2722 } while (exception.retry);
2723 return err;
2726 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2727 struct iattr *sattr)
2729 struct nfs4_createdata *data;
2730 int status = -ENOMEM;
2732 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2733 if (data == NULL)
2734 goto out;
2736 status = nfs4_do_create(dir, dentry, data);
2738 nfs4_free_createdata(data);
2739 out:
2740 return status;
2743 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2744 struct iattr *sattr)
2746 struct nfs4_exception exception = { };
2747 int err;
2748 do {
2749 err = nfs4_handle_exception(NFS_SERVER(dir),
2750 _nfs4_proc_mkdir(dir, dentry, sattr),
2751 &exception);
2752 } while (exception.retry);
2753 return err;
2756 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2757 u64 cookie, struct page *page, unsigned int count, int plus)
2759 struct inode *dir = dentry->d_inode;
2760 struct nfs4_readdir_arg args = {
2761 .fh = NFS_FH(dir),
2762 .pages = &page,
2763 .pgbase = 0,
2764 .count = count,
2765 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2767 struct nfs4_readdir_res res;
2768 struct rpc_message msg = {
2769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2770 .rpc_argp = &args,
2771 .rpc_resp = &res,
2772 .rpc_cred = cred,
2774 int status;
2776 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2777 dentry->d_parent->d_name.name,
2778 dentry->d_name.name,
2779 (unsigned long long)cookie);
2780 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2781 res.pgbase = args.pgbase;
2782 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2783 if (status == 0)
2784 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2786 nfs_invalidate_atime(dir);
2788 dprintk("%s: returns %d\n", __func__, status);
2789 return status;
2792 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2793 u64 cookie, struct page *page, unsigned int count, int plus)
2795 struct nfs4_exception exception = { };
2796 int err;
2797 do {
2798 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2799 _nfs4_proc_readdir(dentry, cred, cookie,
2800 page, count, plus),
2801 &exception);
2802 } while (exception.retry);
2803 return err;
2806 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2807 struct iattr *sattr, dev_t rdev)
2809 struct nfs4_createdata *data;
2810 int mode = sattr->ia_mode;
2811 int status = -ENOMEM;
2813 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2814 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2816 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2817 if (data == NULL)
2818 goto out;
2820 if (S_ISFIFO(mode))
2821 data->arg.ftype = NF4FIFO;
2822 else if (S_ISBLK(mode)) {
2823 data->arg.ftype = NF4BLK;
2824 data->arg.u.device.specdata1 = MAJOR(rdev);
2825 data->arg.u.device.specdata2 = MINOR(rdev);
2827 else if (S_ISCHR(mode)) {
2828 data->arg.ftype = NF4CHR;
2829 data->arg.u.device.specdata1 = MAJOR(rdev);
2830 data->arg.u.device.specdata2 = MINOR(rdev);
2833 status = nfs4_do_create(dir, dentry, data);
2835 nfs4_free_createdata(data);
2836 out:
2837 return status;
2840 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2841 struct iattr *sattr, dev_t rdev)
2843 struct nfs4_exception exception = { };
2844 int err;
2845 do {
2846 err = nfs4_handle_exception(NFS_SERVER(dir),
2847 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2848 &exception);
2849 } while (exception.retry);
2850 return err;
2853 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2854 struct nfs_fsstat *fsstat)
2856 struct nfs4_statfs_arg args = {
2857 .fh = fhandle,
2858 .bitmask = server->attr_bitmask,
2860 struct nfs4_statfs_res res = {
2861 .fsstat = fsstat,
2863 struct rpc_message msg = {
2864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2865 .rpc_argp = &args,
2866 .rpc_resp = &res,
2869 nfs_fattr_init(fsstat->fattr);
2870 return nfs4_call_sync(server, &msg, &args, &res, 0);
2873 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2875 struct nfs4_exception exception = { };
2876 int err;
2877 do {
2878 err = nfs4_handle_exception(server,
2879 _nfs4_proc_statfs(server, fhandle, fsstat),
2880 &exception);
2881 } while (exception.retry);
2882 return err;
2885 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2886 struct nfs_fsinfo *fsinfo)
2888 struct nfs4_fsinfo_arg args = {
2889 .fh = fhandle,
2890 .bitmask = server->attr_bitmask,
2892 struct nfs4_fsinfo_res res = {
2893 .fsinfo = fsinfo,
2895 struct rpc_message msg = {
2896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2897 .rpc_argp = &args,
2898 .rpc_resp = &res,
2901 return nfs4_call_sync(server, &msg, &args, &res, 0);
2904 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2906 struct nfs4_exception exception = { };
2907 int err;
2909 do {
2910 err = nfs4_handle_exception(server,
2911 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2912 &exception);
2913 } while (exception.retry);
2914 return err;
2917 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2919 nfs_fattr_init(fsinfo->fattr);
2920 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2923 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2924 struct nfs_pathconf *pathconf)
2926 struct nfs4_pathconf_arg args = {
2927 .fh = fhandle,
2928 .bitmask = server->attr_bitmask,
2930 struct nfs4_pathconf_res res = {
2931 .pathconf = pathconf,
2933 struct rpc_message msg = {
2934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2935 .rpc_argp = &args,
2936 .rpc_resp = &res,
2939 /* None of the pathconf attributes are mandatory to implement */
2940 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2941 memset(pathconf, 0, sizeof(*pathconf));
2942 return 0;
2945 nfs_fattr_init(pathconf->fattr);
2946 return nfs4_call_sync(server, &msg, &args, &res, 0);
2949 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2950 struct nfs_pathconf *pathconf)
2952 struct nfs4_exception exception = { };
2953 int err;
2955 do {
2956 err = nfs4_handle_exception(server,
2957 _nfs4_proc_pathconf(server, fhandle, pathconf),
2958 &exception);
2959 } while (exception.retry);
2960 return err;
2963 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2965 struct nfs_server *server = NFS_SERVER(data->inode);
2967 dprintk("--> %s\n", __func__);
2969 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2970 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2972 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2973 nfs4_restart_rpc(task, server->nfs_client);
2974 return -EAGAIN;
2977 nfs_invalidate_atime(data->inode);
2978 if (task->tk_status > 0)
2979 renew_lease(server, data->timestamp);
2980 return 0;
2983 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2985 data->timestamp = jiffies;
2986 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2989 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2991 struct inode *inode = data->inode;
2993 /* slot is freed in nfs_writeback_done */
2994 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2995 task->tk_status);
2997 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2998 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
2999 return -EAGAIN;
3001 if (task->tk_status >= 0) {
3002 renew_lease(NFS_SERVER(inode), data->timestamp);
3003 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3005 return 0;
3008 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3010 struct nfs_server *server = NFS_SERVER(data->inode);
3012 data->args.bitmask = server->cache_consistency_bitmask;
3013 data->res.server = server;
3014 data->timestamp = jiffies;
3016 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3019 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3021 struct inode *inode = data->inode;
3023 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3024 task->tk_status);
3025 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3026 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3027 return -EAGAIN;
3029 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3030 &data->res.seq_res);
3031 nfs_refresh_inode(inode, data->res.fattr);
3032 return 0;
3035 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3037 struct nfs_server *server = NFS_SERVER(data->inode);
3039 data->args.bitmask = server->cache_consistency_bitmask;
3040 data->res.server = server;
3041 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3045 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3046 * standalone procedure for queueing an asynchronous RENEW.
3048 static void nfs4_renew_done(struct rpc_task *task, void *data)
3050 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3051 unsigned long timestamp = (unsigned long)data;
3053 if (task->tk_status < 0) {
3054 /* Unless we're shutting down, schedule state recovery! */
3055 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3056 nfs4_schedule_state_recovery(clp);
3057 return;
3059 spin_lock(&clp->cl_lock);
3060 if (time_before(clp->cl_last_renewal,timestamp))
3061 clp->cl_last_renewal = timestamp;
3062 spin_unlock(&clp->cl_lock);
3063 dprintk("%s calling put_rpccred on rpc_cred %p\n", __func__,
3064 task->tk_msg.rpc_cred);
3065 put_rpccred(task->tk_msg.rpc_cred);
3068 static const struct rpc_call_ops nfs4_renew_ops = {
3069 .rpc_call_done = nfs4_renew_done,
3072 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3074 struct rpc_message msg = {
3075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3076 .rpc_argp = clp,
3077 .rpc_cred = cred,
3080 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3081 &nfs4_renew_ops, (void *)jiffies);
3084 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3086 struct rpc_message msg = {
3087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3088 .rpc_argp = clp,
3089 .rpc_cred = cred,
3091 unsigned long now = jiffies;
3092 int status;
3094 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3095 if (status < 0)
3096 return status;
3097 spin_lock(&clp->cl_lock);
3098 if (time_before(clp->cl_last_renewal,now))
3099 clp->cl_last_renewal = now;
3100 spin_unlock(&clp->cl_lock);
3101 return 0;
3104 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3106 return (server->caps & NFS_CAP_ACLS)
3107 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3108 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3111 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3112 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3113 * the stack.
3115 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3117 static void buf_to_pages(const void *buf, size_t buflen,
3118 struct page **pages, unsigned int *pgbase)
3120 const void *p = buf;
3122 *pgbase = offset_in_page(buf);
3123 p -= *pgbase;
3124 while (p < buf + buflen) {
3125 *(pages++) = virt_to_page(p);
3126 p += PAGE_CACHE_SIZE;
3130 struct nfs4_cached_acl {
3131 int cached;
3132 size_t len;
3133 char data[0];
3136 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3138 struct nfs_inode *nfsi = NFS_I(inode);
3140 spin_lock(&inode->i_lock);
3141 kfree(nfsi->nfs4_acl);
3142 nfsi->nfs4_acl = acl;
3143 spin_unlock(&inode->i_lock);
3146 static void nfs4_zap_acl_attr(struct inode *inode)
3148 nfs4_set_cached_acl(inode, NULL);
3151 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3153 struct nfs_inode *nfsi = NFS_I(inode);
3154 struct nfs4_cached_acl *acl;
3155 int ret = -ENOENT;
3157 spin_lock(&inode->i_lock);
3158 acl = nfsi->nfs4_acl;
3159 if (acl == NULL)
3160 goto out;
3161 if (buf == NULL) /* user is just asking for length */
3162 goto out_len;
3163 if (acl->cached == 0)
3164 goto out;
3165 ret = -ERANGE; /* see getxattr(2) man page */
3166 if (acl->len > buflen)
3167 goto out;
3168 memcpy(buf, acl->data, acl->len);
3169 out_len:
3170 ret = acl->len;
3171 out:
3172 spin_unlock(&inode->i_lock);
3173 return ret;
3176 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3178 struct nfs4_cached_acl *acl;
3180 if (buf && acl_len <= PAGE_SIZE) {
3181 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3182 if (acl == NULL)
3183 goto out;
3184 acl->cached = 1;
3185 memcpy(acl->data, buf, acl_len);
3186 } else {
3187 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3188 if (acl == NULL)
3189 goto out;
3190 acl->cached = 0;
3192 acl->len = acl_len;
3193 out:
3194 nfs4_set_cached_acl(inode, acl);
3197 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3199 struct page *pages[NFS4ACL_MAXPAGES];
3200 struct nfs_getaclargs args = {
3201 .fh = NFS_FH(inode),
3202 .acl_pages = pages,
3203 .acl_len = buflen,
3205 struct nfs_getaclres res = {
3206 .acl_len = buflen,
3208 void *resp_buf;
3209 struct rpc_message msg = {
3210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3211 .rpc_argp = &args,
3212 .rpc_resp = &res,
3214 struct page *localpage = NULL;
3215 int ret;
3217 if (buflen < PAGE_SIZE) {
3218 /* As long as we're doing a round trip to the server anyway,
3219 * let's be prepared for a page of acl data. */
3220 localpage = alloc_page(GFP_KERNEL);
3221 resp_buf = page_address(localpage);
3222 if (localpage == NULL)
3223 return -ENOMEM;
3224 args.acl_pages[0] = localpage;
3225 args.acl_pgbase = 0;
3226 args.acl_len = PAGE_SIZE;
3227 } else {
3228 resp_buf = buf;
3229 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3231 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3232 if (ret)
3233 goto out_free;
3234 if (res.acl_len > args.acl_len)
3235 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3236 else
3237 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3238 if (buf) {
3239 ret = -ERANGE;
3240 if (res.acl_len > buflen)
3241 goto out_free;
3242 if (localpage)
3243 memcpy(buf, resp_buf, res.acl_len);
3245 ret = res.acl_len;
3246 out_free:
3247 if (localpage)
3248 __free_page(localpage);
3249 return ret;
3252 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3254 struct nfs4_exception exception = { };
3255 ssize_t ret;
3256 do {
3257 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3258 if (ret >= 0)
3259 break;
3260 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3261 } while (exception.retry);
3262 return ret;
3265 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3267 struct nfs_server *server = NFS_SERVER(inode);
3268 int ret;
3270 if (!nfs4_server_supports_acls(server))
3271 return -EOPNOTSUPP;
3272 ret = nfs_revalidate_inode(server, inode);
3273 if (ret < 0)
3274 return ret;
3275 ret = nfs4_read_cached_acl(inode, buf, buflen);
3276 if (ret != -ENOENT)
3277 return ret;
3278 return nfs4_get_acl_uncached(inode, buf, buflen);
3281 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3283 struct nfs_server *server = NFS_SERVER(inode);
3284 struct page *pages[NFS4ACL_MAXPAGES];
3285 struct nfs_setaclargs arg = {
3286 .fh = NFS_FH(inode),
3287 .acl_pages = pages,
3288 .acl_len = buflen,
3290 struct nfs_setaclres res;
3291 struct rpc_message msg = {
3292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3293 .rpc_argp = &arg,
3294 .rpc_resp = &res,
3296 int ret;
3298 if (!nfs4_server_supports_acls(server))
3299 return -EOPNOTSUPP;
3300 nfs_inode_return_delegation(inode);
3301 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3302 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3303 nfs_access_zap_cache(inode);
3304 nfs_zap_acl_cache(inode);
3305 return ret;
3308 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3310 struct nfs4_exception exception = { };
3311 int err;
3312 do {
3313 err = nfs4_handle_exception(NFS_SERVER(inode),
3314 __nfs4_proc_set_acl(inode, buf, buflen),
3315 &exception);
3316 } while (exception.retry);
3317 return err;
3320 static int
3321 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3323 if (!clp || task->tk_status >= 0)
3324 return 0;
3325 switch(task->tk_status) {
3326 case -NFS4ERR_ADMIN_REVOKED:
3327 case -NFS4ERR_BAD_STATEID:
3328 case -NFS4ERR_OPENMODE:
3329 if (state == NULL)
3330 break;
3331 nfs4_state_mark_reclaim_nograce(clp, state);
3332 case -NFS4ERR_STALE_CLIENTID:
3333 case -NFS4ERR_STALE_STATEID:
3334 case -NFS4ERR_EXPIRED:
3335 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3336 nfs4_schedule_state_recovery(clp);
3337 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3338 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3339 task->tk_status = 0;
3340 return -EAGAIN;
3341 #if defined(CONFIG_NFS_V4_1)
3342 case -NFS4ERR_BADSESSION:
3343 case -NFS4ERR_BADSLOT:
3344 case -NFS4ERR_BAD_HIGH_SLOT:
3345 case -NFS4ERR_DEADSESSION:
3346 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3347 case -NFS4ERR_SEQ_FALSE_RETRY:
3348 case -NFS4ERR_SEQ_MISORDERED:
3349 dprintk("%s ERROR %d, Reset session\n", __func__,
3350 task->tk_status);
3351 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3352 task->tk_status = 0;
3353 return -EAGAIN;
3354 #endif /* CONFIG_NFS_V4_1 */
3355 case -NFS4ERR_DELAY:
3356 if (server)
3357 nfs_inc_server_stats(server, NFSIOS_DELAY);
3358 case -NFS4ERR_GRACE:
3359 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3360 task->tk_status = 0;
3361 return -EAGAIN;
3362 case -NFS4ERR_OLD_STATEID:
3363 task->tk_status = 0;
3364 return -EAGAIN;
3366 task->tk_status = nfs4_map_errors(task->tk_status);
3367 return 0;
3370 static int
3371 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3373 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3376 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3378 nfs4_verifier sc_verifier;
3379 struct nfs4_setclientid setclientid = {
3380 .sc_verifier = &sc_verifier,
3381 .sc_prog = program,
3383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3385 .rpc_argp = &setclientid,
3386 .rpc_resp = clp,
3387 .rpc_cred = cred,
3389 __be32 *p;
3390 int loop = 0;
3391 int status;
3393 p = (__be32*)sc_verifier.data;
3394 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3395 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3397 for(;;) {
3398 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3399 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3400 clp->cl_ipaddr,
3401 rpc_peeraddr2str(clp->cl_rpcclient,
3402 RPC_DISPLAY_ADDR),
3403 rpc_peeraddr2str(clp->cl_rpcclient,
3404 RPC_DISPLAY_PROTO),
3405 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3406 clp->cl_id_uniquifier);
3407 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3408 sizeof(setclientid.sc_netid),
3409 rpc_peeraddr2str(clp->cl_rpcclient,
3410 RPC_DISPLAY_NETID));
3411 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3412 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3413 clp->cl_ipaddr, port >> 8, port & 255);
3415 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3416 if (status != -NFS4ERR_CLID_INUSE)
3417 break;
3418 if (signalled())
3419 break;
3420 if (loop++ & 1)
3421 ssleep(clp->cl_lease_time + 1);
3422 else
3423 if (++clp->cl_id_uniquifier == 0)
3424 break;
3426 return status;
3429 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3431 struct nfs_fsinfo fsinfo;
3432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3434 .rpc_argp = clp,
3435 .rpc_resp = &fsinfo,
3436 .rpc_cred = cred,
3438 unsigned long now;
3439 int status;
3441 now = jiffies;
3442 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3443 if (status == 0) {
3444 spin_lock(&clp->cl_lock);
3445 clp->cl_lease_time = fsinfo.lease_time * HZ;
3446 clp->cl_last_renewal = now;
3447 spin_unlock(&clp->cl_lock);
3449 return status;
3452 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3454 long timeout = 0;
3455 int err;
3456 do {
3457 err = _nfs4_proc_setclientid_confirm(clp, cred);
3458 switch (err) {
3459 case 0:
3460 return err;
3461 case -NFS4ERR_RESOURCE:
3462 /* The IBM lawyers misread another document! */
3463 case -NFS4ERR_DELAY:
3464 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3466 } while (err == 0);
3467 return err;
3470 struct nfs4_delegreturndata {
3471 struct nfs4_delegreturnargs args;
3472 struct nfs4_delegreturnres res;
3473 struct nfs_fh fh;
3474 nfs4_stateid stateid;
3475 unsigned long timestamp;
3476 struct nfs_fattr fattr;
3477 int rpc_status;
3480 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3482 struct nfs4_delegreturndata *data = calldata;
3484 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3485 task->tk_status);
3487 data->rpc_status = task->tk_status;
3488 if (data->rpc_status == 0)
3489 renew_lease(data->res.server, data->timestamp);
3492 static void nfs4_delegreturn_release(void *calldata)
3494 kfree(calldata);
3497 #if defined(CONFIG_NFS_V4_1)
3498 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3500 struct nfs4_delegreturndata *d_data;
3502 d_data = (struct nfs4_delegreturndata *)data;
3504 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3505 &d_data->args.seq_args,
3506 &d_data->res.seq_res, 1, task))
3507 return;
3508 rpc_call_start(task);
3510 #endif /* CONFIG_NFS_V4_1 */
3512 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3513 #if defined(CONFIG_NFS_V4_1)
3514 .rpc_call_prepare = nfs4_delegreturn_prepare,
3515 #endif /* CONFIG_NFS_V4_1 */
3516 .rpc_call_done = nfs4_delegreturn_done,
3517 .rpc_release = nfs4_delegreturn_release,
3520 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3522 struct nfs4_delegreturndata *data;
3523 struct nfs_server *server = NFS_SERVER(inode);
3524 struct rpc_task *task;
3525 struct rpc_message msg = {
3526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3527 .rpc_cred = cred,
3529 struct rpc_task_setup task_setup_data = {
3530 .rpc_client = server->client,
3531 .rpc_message = &msg,
3532 .callback_ops = &nfs4_delegreturn_ops,
3533 .flags = RPC_TASK_ASYNC,
3535 int status = 0;
3537 data = kzalloc(sizeof(*data), GFP_KERNEL);
3538 if (data == NULL)
3539 return -ENOMEM;
3540 data->args.fhandle = &data->fh;
3541 data->args.stateid = &data->stateid;
3542 data->args.bitmask = server->attr_bitmask;
3543 nfs_copy_fh(&data->fh, NFS_FH(inode));
3544 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3545 data->res.fattr = &data->fattr;
3546 data->res.server = server;
3547 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3548 nfs_fattr_init(data->res.fattr);
3549 data->timestamp = jiffies;
3550 data->rpc_status = 0;
3552 task_setup_data.callback_data = data;
3553 msg.rpc_argp = &data->args,
3554 msg.rpc_resp = &data->res,
3555 task = rpc_run_task(&task_setup_data);
3556 if (IS_ERR(task))
3557 return PTR_ERR(task);
3558 if (!issync)
3559 goto out;
3560 status = nfs4_wait_for_completion_rpc_task(task);
3561 if (status != 0)
3562 goto out;
3563 status = data->rpc_status;
3564 if (status != 0)
3565 goto out;
3566 nfs_refresh_inode(inode, &data->fattr);
3567 out:
3568 rpc_put_task(task);
3569 return status;
3572 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3574 struct nfs_server *server = NFS_SERVER(inode);
3575 struct nfs4_exception exception = { };
3576 int err;
3577 do {
3578 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3579 switch (err) {
3580 case -NFS4ERR_STALE_STATEID:
3581 case -NFS4ERR_EXPIRED:
3582 case 0:
3583 return 0;
3585 err = nfs4_handle_exception(server, err, &exception);
3586 } while (exception.retry);
3587 return err;
3590 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3591 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3594 * sleep, with exponential backoff, and retry the LOCK operation.
3596 static unsigned long
3597 nfs4_set_lock_task_retry(unsigned long timeout)
3599 schedule_timeout_killable(timeout);
3600 timeout <<= 1;
3601 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3602 return NFS4_LOCK_MAXTIMEOUT;
3603 return timeout;
3606 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3608 struct inode *inode = state->inode;
3609 struct nfs_server *server = NFS_SERVER(inode);
3610 struct nfs_client *clp = server->nfs_client;
3611 struct nfs_lockt_args arg = {
3612 .fh = NFS_FH(inode),
3613 .fl = request,
3615 struct nfs_lockt_res res = {
3616 .denied = request,
3618 struct rpc_message msg = {
3619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3620 .rpc_argp = &arg,
3621 .rpc_resp = &res,
3622 .rpc_cred = state->owner->so_cred,
3624 struct nfs4_lock_state *lsp;
3625 int status;
3627 arg.lock_owner.clientid = clp->cl_clientid;
3628 status = nfs4_set_lock_state(state, request);
3629 if (status != 0)
3630 goto out;
3631 lsp = request->fl_u.nfs4_fl.owner;
3632 arg.lock_owner.id = lsp->ls_id.id;
3633 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3634 switch (status) {
3635 case 0:
3636 request->fl_type = F_UNLCK;
3637 break;
3638 case -NFS4ERR_DENIED:
3639 status = 0;
3641 request->fl_ops->fl_release_private(request);
3642 out:
3643 return status;
3646 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3648 struct nfs4_exception exception = { };
3649 int err;
3651 do {
3652 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3653 _nfs4_proc_getlk(state, cmd, request),
3654 &exception);
3655 } while (exception.retry);
3656 return err;
3659 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3661 int res = 0;
3662 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3663 case FL_POSIX:
3664 res = posix_lock_file_wait(file, fl);
3665 break;
3666 case FL_FLOCK:
3667 res = flock_lock_file_wait(file, fl);
3668 break;
3669 default:
3670 BUG();
3672 return res;
3675 struct nfs4_unlockdata {
3676 struct nfs_locku_args arg;
3677 struct nfs_locku_res res;
3678 struct nfs4_lock_state *lsp;
3679 struct nfs_open_context *ctx;
3680 struct file_lock fl;
3681 const struct nfs_server *server;
3682 unsigned long timestamp;
3685 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3686 struct nfs_open_context *ctx,
3687 struct nfs4_lock_state *lsp,
3688 struct nfs_seqid *seqid)
3690 struct nfs4_unlockdata *p;
3691 struct inode *inode = lsp->ls_state->inode;
3693 p = kzalloc(sizeof(*p), GFP_KERNEL);
3694 if (p == NULL)
3695 return NULL;
3696 p->arg.fh = NFS_FH(inode);
3697 p->arg.fl = &p->fl;
3698 p->arg.seqid = seqid;
3699 p->res.seqid = seqid;
3700 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3701 p->arg.stateid = &lsp->ls_stateid;
3702 p->lsp = lsp;
3703 atomic_inc(&lsp->ls_count);
3704 /* Ensure we don't close file until we're done freeing locks! */
3705 p->ctx = get_nfs_open_context(ctx);
3706 memcpy(&p->fl, fl, sizeof(p->fl));
3707 p->server = NFS_SERVER(inode);
3708 return p;
3711 static void nfs4_locku_release_calldata(void *data)
3713 struct nfs4_unlockdata *calldata = data;
3714 nfs_free_seqid(calldata->arg.seqid);
3715 nfs4_put_lock_state(calldata->lsp);
3716 put_nfs_open_context(calldata->ctx);
3717 kfree(calldata);
3720 static void nfs4_locku_done(struct rpc_task *task, void *data)
3722 struct nfs4_unlockdata *calldata = data;
3724 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3725 task->tk_status);
3726 if (RPC_ASSASSINATED(task))
3727 return;
3728 switch (task->tk_status) {
3729 case 0:
3730 memcpy(calldata->lsp->ls_stateid.data,
3731 calldata->res.stateid.data,
3732 sizeof(calldata->lsp->ls_stateid.data));
3733 renew_lease(calldata->server, calldata->timestamp);
3734 break;
3735 case -NFS4ERR_BAD_STATEID:
3736 case -NFS4ERR_OLD_STATEID:
3737 case -NFS4ERR_STALE_STATEID:
3738 case -NFS4ERR_EXPIRED:
3739 break;
3740 default:
3741 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3742 nfs4_restart_rpc(task,
3743 calldata->server->nfs_client);
3745 nfs4_sequence_free_slot(calldata->server->nfs_client,
3746 &calldata->res.seq_res);
3749 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3751 struct nfs4_unlockdata *calldata = data;
3753 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3754 return;
3755 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3756 /* Note: exit _without_ running nfs4_locku_done */
3757 task->tk_action = NULL;
3758 return;
3760 calldata->timestamp = jiffies;
3761 if (nfs4_setup_sequence(calldata->server->nfs_client,
3762 &calldata->arg.seq_args,
3763 &calldata->res.seq_res, 1, task))
3764 return;
3765 rpc_call_start(task);
3768 static const struct rpc_call_ops nfs4_locku_ops = {
3769 .rpc_call_prepare = nfs4_locku_prepare,
3770 .rpc_call_done = nfs4_locku_done,
3771 .rpc_release = nfs4_locku_release_calldata,
3774 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3775 struct nfs_open_context *ctx,
3776 struct nfs4_lock_state *lsp,
3777 struct nfs_seqid *seqid)
3779 struct nfs4_unlockdata *data;
3780 struct rpc_message msg = {
3781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3782 .rpc_cred = ctx->cred,
3784 struct rpc_task_setup task_setup_data = {
3785 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3786 .rpc_message = &msg,
3787 .callback_ops = &nfs4_locku_ops,
3788 .workqueue = nfsiod_workqueue,
3789 .flags = RPC_TASK_ASYNC,
3792 /* Ensure this is an unlock - when canceling a lock, the
3793 * canceled lock is passed in, and it won't be an unlock.
3795 fl->fl_type = F_UNLCK;
3797 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3798 if (data == NULL) {
3799 nfs_free_seqid(seqid);
3800 return ERR_PTR(-ENOMEM);
3803 msg.rpc_argp = &data->arg,
3804 msg.rpc_resp = &data->res,
3805 task_setup_data.callback_data = data;
3806 return rpc_run_task(&task_setup_data);
3809 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3811 struct nfs_inode *nfsi = NFS_I(state->inode);
3812 struct nfs_seqid *seqid;
3813 struct nfs4_lock_state *lsp;
3814 struct rpc_task *task;
3815 int status = 0;
3816 unsigned char fl_flags = request->fl_flags;
3818 status = nfs4_set_lock_state(state, request);
3819 /* Unlock _before_ we do the RPC call */
3820 request->fl_flags |= FL_EXISTS;
3821 down_read(&nfsi->rwsem);
3822 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3823 up_read(&nfsi->rwsem);
3824 goto out;
3826 up_read(&nfsi->rwsem);
3827 if (status != 0)
3828 goto out;
3829 /* Is this a delegated lock? */
3830 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3831 goto out;
3832 lsp = request->fl_u.nfs4_fl.owner;
3833 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3834 status = -ENOMEM;
3835 if (seqid == NULL)
3836 goto out;
3837 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3838 status = PTR_ERR(task);
3839 if (IS_ERR(task))
3840 goto out;
3841 status = nfs4_wait_for_completion_rpc_task(task);
3842 rpc_put_task(task);
3843 out:
3844 request->fl_flags = fl_flags;
3845 return status;
3848 struct nfs4_lockdata {
3849 struct nfs_lock_args arg;
3850 struct nfs_lock_res res;
3851 struct nfs4_lock_state *lsp;
3852 struct nfs_open_context *ctx;
3853 struct file_lock fl;
3854 unsigned long timestamp;
3855 int rpc_status;
3856 int cancelled;
3857 struct nfs_server *server;
3860 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3861 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3863 struct nfs4_lockdata *p;
3864 struct inode *inode = lsp->ls_state->inode;
3865 struct nfs_server *server = NFS_SERVER(inode);
3867 p = kzalloc(sizeof(*p), GFP_KERNEL);
3868 if (p == NULL)
3869 return NULL;
3871 p->arg.fh = NFS_FH(inode);
3872 p->arg.fl = &p->fl;
3873 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3874 if (p->arg.open_seqid == NULL)
3875 goto out_free;
3876 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3877 if (p->arg.lock_seqid == NULL)
3878 goto out_free_seqid;
3879 p->arg.lock_stateid = &lsp->ls_stateid;
3880 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3881 p->arg.lock_owner.id = lsp->ls_id.id;
3882 p->res.lock_seqid = p->arg.lock_seqid;
3883 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3884 p->lsp = lsp;
3885 p->server = server;
3886 atomic_inc(&lsp->ls_count);
3887 p->ctx = get_nfs_open_context(ctx);
3888 memcpy(&p->fl, fl, sizeof(p->fl));
3889 return p;
3890 out_free_seqid:
3891 nfs_free_seqid(p->arg.open_seqid);
3892 out_free:
3893 kfree(p);
3894 return NULL;
3897 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3899 struct nfs4_lockdata *data = calldata;
3900 struct nfs4_state *state = data->lsp->ls_state;
3902 dprintk("%s: begin!\n", __func__);
3903 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3904 return;
3905 /* Do we need to do an open_to_lock_owner? */
3906 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3907 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3908 return;
3909 data->arg.open_stateid = &state->stateid;
3910 data->arg.new_lock_owner = 1;
3911 data->res.open_seqid = data->arg.open_seqid;
3912 } else
3913 data->arg.new_lock_owner = 0;
3914 data->timestamp = jiffies;
3915 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3916 &data->res.seq_res, 1, task))
3917 return;
3918 rpc_call_start(task);
3919 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3922 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3924 struct nfs4_lockdata *data = calldata;
3926 dprintk("%s: begin!\n", __func__);
3928 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3929 task->tk_status);
3931 data->rpc_status = task->tk_status;
3932 if (RPC_ASSASSINATED(task))
3933 goto out;
3934 if (data->arg.new_lock_owner != 0) {
3935 if (data->rpc_status == 0)
3936 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3937 else
3938 goto out;
3940 if (data->rpc_status == 0) {
3941 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3942 sizeof(data->lsp->ls_stateid.data));
3943 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3944 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3946 out:
3947 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3950 static void nfs4_lock_release(void *calldata)
3952 struct nfs4_lockdata *data = calldata;
3954 dprintk("%s: begin!\n", __func__);
3955 nfs_free_seqid(data->arg.open_seqid);
3956 if (data->cancelled != 0) {
3957 struct rpc_task *task;
3958 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3959 data->arg.lock_seqid);
3960 if (!IS_ERR(task))
3961 rpc_put_task(task);
3962 dprintk("%s: cancelling lock!\n", __func__);
3963 } else
3964 nfs_free_seqid(data->arg.lock_seqid);
3965 nfs4_put_lock_state(data->lsp);
3966 put_nfs_open_context(data->ctx);
3967 kfree(data);
3968 dprintk("%s: done!\n", __func__);
3971 static const struct rpc_call_ops nfs4_lock_ops = {
3972 .rpc_call_prepare = nfs4_lock_prepare,
3973 .rpc_call_done = nfs4_lock_done,
3974 .rpc_release = nfs4_lock_release,
3977 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3979 struct nfs4_lockdata *data;
3980 struct rpc_task *task;
3981 struct rpc_message msg = {
3982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3983 .rpc_cred = state->owner->so_cred,
3985 struct rpc_task_setup task_setup_data = {
3986 .rpc_client = NFS_CLIENT(state->inode),
3987 .rpc_message = &msg,
3988 .callback_ops = &nfs4_lock_ops,
3989 .workqueue = nfsiod_workqueue,
3990 .flags = RPC_TASK_ASYNC,
3992 int ret;
3994 dprintk("%s: begin!\n", __func__);
3995 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3996 fl->fl_u.nfs4_fl.owner);
3997 if (data == NULL)
3998 return -ENOMEM;
3999 if (IS_SETLKW(cmd))
4000 data->arg.block = 1;
4001 if (reclaim != 0)
4002 data->arg.reclaim = 1;
4003 msg.rpc_argp = &data->arg,
4004 msg.rpc_resp = &data->res,
4005 task_setup_data.callback_data = data;
4006 task = rpc_run_task(&task_setup_data);
4007 if (IS_ERR(task))
4008 return PTR_ERR(task);
4009 ret = nfs4_wait_for_completion_rpc_task(task);
4010 if (ret == 0) {
4011 ret = data->rpc_status;
4012 } else
4013 data->cancelled = 1;
4014 rpc_put_task(task);
4015 dprintk("%s: done, ret = %d!\n", __func__, ret);
4016 return ret;
4019 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4021 struct nfs_server *server = NFS_SERVER(state->inode);
4022 struct nfs4_exception exception = { };
4023 int err;
4025 do {
4026 /* Cache the lock if possible... */
4027 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4028 return 0;
4029 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4030 if (err != -NFS4ERR_DELAY)
4031 break;
4032 nfs4_handle_exception(server, err, &exception);
4033 } while (exception.retry);
4034 return err;
4037 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4039 struct nfs_server *server = NFS_SERVER(state->inode);
4040 struct nfs4_exception exception = { };
4041 int err;
4043 err = nfs4_set_lock_state(state, request);
4044 if (err != 0)
4045 return err;
4046 do {
4047 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4048 return 0;
4049 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4050 if (err != -NFS4ERR_DELAY)
4051 break;
4052 nfs4_handle_exception(server, err, &exception);
4053 } while (exception.retry);
4054 return err;
4057 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4059 struct nfs_inode *nfsi = NFS_I(state->inode);
4060 unsigned char fl_flags = request->fl_flags;
4061 int status;
4063 /* Is this a delegated open? */
4064 status = nfs4_set_lock_state(state, request);
4065 if (status != 0)
4066 goto out;
4067 request->fl_flags |= FL_ACCESS;
4068 status = do_vfs_lock(request->fl_file, request);
4069 if (status < 0)
4070 goto out;
4071 down_read(&nfsi->rwsem);
4072 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4073 /* Yes: cache locks! */
4074 /* ...but avoid races with delegation recall... */
4075 request->fl_flags = fl_flags & ~FL_SLEEP;
4076 status = do_vfs_lock(request->fl_file, request);
4077 goto out_unlock;
4079 status = _nfs4_do_setlk(state, cmd, request, 0);
4080 if (status != 0)
4081 goto out_unlock;
4082 /* Note: we always want to sleep here! */
4083 request->fl_flags = fl_flags | FL_SLEEP;
4084 if (do_vfs_lock(request->fl_file, request) < 0)
4085 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4086 out_unlock:
4087 up_read(&nfsi->rwsem);
4088 out:
4089 request->fl_flags = fl_flags;
4090 return status;
4093 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4095 struct nfs4_exception exception = { };
4096 int err;
4098 do {
4099 err = _nfs4_proc_setlk(state, cmd, request);
4100 if (err == -NFS4ERR_DENIED)
4101 err = -EAGAIN;
4102 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4103 err, &exception);
4104 } while (exception.retry);
4105 return err;
4108 static int
4109 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4111 struct nfs_open_context *ctx;
4112 struct nfs4_state *state;
4113 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4114 int status;
4116 /* verify open state */
4117 ctx = nfs_file_open_context(filp);
4118 state = ctx->state;
4120 if (request->fl_start < 0 || request->fl_end < 0)
4121 return -EINVAL;
4123 if (IS_GETLK(cmd)) {
4124 if (state != NULL)
4125 return nfs4_proc_getlk(state, F_GETLK, request);
4126 return 0;
4129 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4130 return -EINVAL;
4132 if (request->fl_type == F_UNLCK) {
4133 if (state != NULL)
4134 return nfs4_proc_unlck(state, cmd, request);
4135 return 0;
4138 if (state == NULL)
4139 return -ENOLCK;
4140 do {
4141 status = nfs4_proc_setlk(state, cmd, request);
4142 if ((status != -EAGAIN) || IS_SETLK(cmd))
4143 break;
4144 timeout = nfs4_set_lock_task_retry(timeout);
4145 status = -ERESTARTSYS;
4146 if (signalled())
4147 break;
4148 } while(status < 0);
4149 return status;
4152 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4154 struct nfs_server *server = NFS_SERVER(state->inode);
4155 struct nfs4_exception exception = { };
4156 int err;
4158 err = nfs4_set_lock_state(state, fl);
4159 if (err != 0)
4160 goto out;
4161 do {
4162 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4163 switch (err) {
4164 default:
4165 printk(KERN_ERR "%s: unhandled error %d.\n",
4166 __func__, err);
4167 case 0:
4168 case -ESTALE:
4169 goto out;
4170 case -NFS4ERR_EXPIRED:
4171 case -NFS4ERR_STALE_CLIENTID:
4172 case -NFS4ERR_STALE_STATEID:
4173 nfs4_schedule_state_recovery(server->nfs_client);
4174 goto out;
4175 case -ERESTARTSYS:
4177 * The show must go on: exit, but mark the
4178 * stateid as needing recovery.
4180 case -NFS4ERR_ADMIN_REVOKED:
4181 case -NFS4ERR_BAD_STATEID:
4182 case -NFS4ERR_OPENMODE:
4183 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4184 err = 0;
4185 goto out;
4186 case -ENOMEM:
4187 case -NFS4ERR_DENIED:
4188 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4189 err = 0;
4190 goto out;
4191 case -NFS4ERR_DELAY:
4192 break;
4194 err = nfs4_handle_exception(server, err, &exception);
4195 } while (exception.retry);
4196 out:
4197 return err;
4200 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4202 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4203 size_t buflen, int flags)
4205 struct inode *inode = dentry->d_inode;
4207 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4208 return -EOPNOTSUPP;
4210 return nfs4_proc_set_acl(inode, buf, buflen);
4213 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4214 * and that's what we'll do for e.g. user attributes that haven't been set.
4215 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4216 * attributes in kernel-managed attribute namespaces. */
4217 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4218 size_t buflen)
4220 struct inode *inode = dentry->d_inode;
4222 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4223 return -EOPNOTSUPP;
4225 return nfs4_proc_get_acl(inode, buf, buflen);
4228 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4230 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4232 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4233 return 0;
4234 if (buf && buflen < len)
4235 return -ERANGE;
4236 if (buf)
4237 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4238 return len;
4241 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4243 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4244 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4245 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4246 return;
4248 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4249 NFS_ATTR_FATTR_NLINK;
4250 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4251 fattr->nlink = 2;
4254 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4255 struct nfs4_fs_locations *fs_locations, struct page *page)
4257 struct nfs_server *server = NFS_SERVER(dir);
4258 u32 bitmask[2] = {
4259 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4260 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4262 struct nfs4_fs_locations_arg args = {
4263 .dir_fh = NFS_FH(dir),
4264 .name = name,
4265 .page = page,
4266 .bitmask = bitmask,
4268 struct nfs4_fs_locations_res res = {
4269 .fs_locations = fs_locations,
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4273 .rpc_argp = &args,
4274 .rpc_resp = &res,
4276 int status;
4278 dprintk("%s: start\n", __func__);
4279 nfs_fattr_init(&fs_locations->fattr);
4280 fs_locations->server = server;
4281 fs_locations->nlocations = 0;
4282 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4283 nfs_fixup_referral_attributes(&fs_locations->fattr);
4284 dprintk("%s: returned status = %d\n", __func__, status);
4285 return status;
4288 #ifdef CONFIG_NFS_V4_1
4290 * nfs4_proc_exchange_id()
4292 * Since the clientid has expired, all compounds using sessions
4293 * associated with the stale clientid will be returning
4294 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4295 * be in some phase of session reset.
4297 static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4299 nfs4_verifier verifier;
4300 struct nfs41_exchange_id_args args = {
4301 .client = clp,
4302 .flags = clp->cl_exchange_flags,
4304 struct nfs41_exchange_id_res res = {
4305 .client = clp,
4307 int status;
4308 struct rpc_message msg = {
4309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4310 .rpc_argp = &args,
4311 .rpc_resp = &res,
4312 .rpc_cred = cred,
4314 __be32 *p;
4316 dprintk("--> %s\n", __func__);
4317 BUG_ON(clp == NULL);
4319 p = (u32 *)verifier.data;
4320 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4321 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4322 args.verifier = &verifier;
4324 while (1) {
4325 args.id_len = scnprintf(args.id, sizeof(args.id),
4326 "%s/%s %u",
4327 clp->cl_ipaddr,
4328 rpc_peeraddr2str(clp->cl_rpcclient,
4329 RPC_DISPLAY_ADDR),
4330 clp->cl_id_uniquifier);
4332 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4334 if (status != NFS4ERR_CLID_INUSE)
4335 break;
4337 if (signalled())
4338 break;
4340 if (++clp->cl_id_uniquifier == 0)
4341 break;
4344 dprintk("<-- %s status= %d\n", __func__, status);
4345 return status;
4348 struct nfs4_get_lease_time_data {
4349 struct nfs4_get_lease_time_args *args;
4350 struct nfs4_get_lease_time_res *res;
4351 struct nfs_client *clp;
4354 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4355 void *calldata)
4357 int ret;
4358 struct nfs4_get_lease_time_data *data =
4359 (struct nfs4_get_lease_time_data *)calldata;
4361 dprintk("--> %s\n", __func__);
4362 /* just setup sequence, do not trigger session recovery
4363 since we're invoked within one */
4364 ret = nfs41_setup_sequence(data->clp->cl_session,
4365 &data->args->la_seq_args,
4366 &data->res->lr_seq_res, 0, task);
4368 BUG_ON(ret == -EAGAIN);
4369 rpc_call_start(task);
4370 dprintk("<-- %s\n", __func__);
4374 * Called from nfs4_state_manager thread for session setup, so don't recover
4375 * from sequence operation or clientid errors.
4377 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4379 struct nfs4_get_lease_time_data *data =
4380 (struct nfs4_get_lease_time_data *)calldata;
4382 dprintk("--> %s\n", __func__);
4383 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4384 switch (task->tk_status) {
4385 case -NFS4ERR_DELAY:
4386 case -NFS4ERR_GRACE:
4387 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4388 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4389 task->tk_status = 0;
4390 nfs4_restart_rpc(task, data->clp);
4391 return;
4393 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4394 dprintk("<-- %s\n", __func__);
4397 struct rpc_call_ops nfs4_get_lease_time_ops = {
4398 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4399 .rpc_call_done = nfs4_get_lease_time_done,
4402 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4404 struct rpc_task *task;
4405 struct nfs4_get_lease_time_args args;
4406 struct nfs4_get_lease_time_res res = {
4407 .lr_fsinfo = fsinfo,
4409 struct nfs4_get_lease_time_data data = {
4410 .args = &args,
4411 .res = &res,
4412 .clp = clp,
4414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4416 .rpc_argp = &args,
4417 .rpc_resp = &res,
4419 struct rpc_task_setup task_setup = {
4420 .rpc_client = clp->cl_rpcclient,
4421 .rpc_message = &msg,
4422 .callback_ops = &nfs4_get_lease_time_ops,
4423 .callback_data = &data
4425 int status;
4427 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4428 dprintk("--> %s\n", __func__);
4429 task = rpc_run_task(&task_setup);
4431 if (IS_ERR(task))
4432 status = PTR_ERR(task);
4433 else {
4434 status = task->tk_status;
4435 rpc_put_task(task);
4437 dprintk("<-- %s return %d\n", __func__, status);
4439 return status;
4443 * Reset a slot table
4445 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4446 int old_max_slots, int ivalue)
4448 int i;
4449 int ret = 0;
4451 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4454 * Until we have dynamic slot table adjustment, insist
4455 * upon the same slot table size
4457 if (max_slots != old_max_slots) {
4458 dprintk("%s reset slot table does't match old\n",
4459 __func__);
4460 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4461 goto out;
4463 spin_lock(&tbl->slot_tbl_lock);
4464 for (i = 0; i < max_slots; ++i)
4465 tbl->slots[i].seq_nr = ivalue;
4466 tbl->highest_used_slotid = -1;
4467 spin_unlock(&tbl->slot_tbl_lock);
4468 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4469 tbl, tbl->slots, tbl->max_slots);
4470 out:
4471 dprintk("<-- %s: return %d\n", __func__, ret);
4472 return ret;
4476 * Reset the forechannel and backchannel slot tables
4478 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4480 int status;
4482 status = nfs4_reset_slot_table(&session->fc_slot_table,
4483 session->fc_attrs.max_reqs,
4484 session->fc_slot_table.max_slots,
4486 if (status)
4487 return status;
4489 status = nfs4_reset_slot_table(&session->bc_slot_table,
4490 session->bc_attrs.max_reqs,
4491 session->bc_slot_table.max_slots,
4493 return status;
4496 /* Destroy the slot table */
4497 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4499 if (session->fc_slot_table.slots != NULL) {
4500 kfree(session->fc_slot_table.slots);
4501 session->fc_slot_table.slots = NULL;
4503 if (session->bc_slot_table.slots != NULL) {
4504 kfree(session->bc_slot_table.slots);
4505 session->bc_slot_table.slots = NULL;
4507 return;
4511 * Initialize slot table
4513 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4514 int max_slots, int ivalue)
4516 int i;
4517 struct nfs4_slot *slot;
4518 int ret = -ENOMEM;
4520 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4522 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4524 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4525 if (!slot)
4526 goto out;
4527 for (i = 0; i < max_slots; ++i)
4528 slot[i].seq_nr = ivalue;
4529 ret = 0;
4531 spin_lock(&tbl->slot_tbl_lock);
4532 if (tbl->slots != NULL) {
4533 spin_unlock(&tbl->slot_tbl_lock);
4534 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4535 __func__, tbl, tbl->slots);
4536 WARN_ON(1);
4537 goto out_free;
4539 tbl->max_slots = max_slots;
4540 tbl->slots = slot;
4541 tbl->highest_used_slotid = -1; /* no slot is currently used */
4542 spin_unlock(&tbl->slot_tbl_lock);
4543 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4544 tbl, tbl->slots, tbl->max_slots);
4545 out:
4546 dprintk("<-- %s: return %d\n", __func__, ret);
4547 return ret;
4549 out_free:
4550 kfree(slot);
4551 goto out;
4555 * Initialize the forechannel and backchannel tables
4557 static int nfs4_init_slot_tables(struct nfs4_session *session)
4559 int status;
4561 status = nfs4_init_slot_table(&session->fc_slot_table,
4562 session->fc_attrs.max_reqs, 1);
4563 if (status)
4564 return status;
4566 status = nfs4_init_slot_table(&session->bc_slot_table,
4567 session->bc_attrs.max_reqs, 0);
4568 if (status)
4569 nfs4_destroy_slot_tables(session);
4571 return status;
4574 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4576 struct nfs4_session *session;
4577 struct nfs4_slot_table *tbl;
4579 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4580 if (!session)
4581 return NULL;
4583 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4585 * The create session reply races with the server back
4586 * channel probe. Mark the client NFS_CS_SESSION_INITING
4587 * so that the client back channel can find the
4588 * nfs_client struct
4590 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4592 tbl = &session->fc_slot_table;
4593 spin_lock_init(&tbl->slot_tbl_lock);
4594 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4596 tbl = &session->bc_slot_table;
4597 spin_lock_init(&tbl->slot_tbl_lock);
4598 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4600 session->clp = clp;
4601 return session;
4604 void nfs4_destroy_session(struct nfs4_session *session)
4606 nfs4_proc_destroy_session(session);
4607 dprintk("%s Destroy backchannel for xprt %p\n",
4608 __func__, session->clp->cl_rpcclient->cl_xprt);
4609 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4610 NFS41_BC_MIN_CALLBACKS);
4611 nfs4_destroy_slot_tables(session);
4612 kfree(session);
4616 * Initialize the values to be used by the client in CREATE_SESSION
4617 * If nfs4_init_session set the fore channel request and response sizes,
4618 * use them.
4620 * Set the back channel max_resp_sz_cached to zero to force the client to
4621 * always set csa_cachethis to FALSE because the current implementation
4622 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4624 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4626 struct nfs4_session *session = args->client->cl_session;
4627 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4628 mxresp_sz = session->fc_attrs.max_resp_sz;
4630 if (mxrqst_sz == 0)
4631 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4632 if (mxresp_sz == 0)
4633 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4634 /* Fore channel attributes */
4635 args->fc_attrs.headerpadsz = 0;
4636 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4637 args->fc_attrs.max_resp_sz = mxresp_sz;
4638 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4639 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4640 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4642 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4643 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4644 __func__,
4645 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4646 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4647 args->fc_attrs.max_reqs);
4649 /* Back channel attributes */
4650 args->bc_attrs.headerpadsz = 0;
4651 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4652 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4653 args->bc_attrs.max_resp_sz_cached = 0;
4654 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4655 args->bc_attrs.max_reqs = 1;
4657 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4658 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4659 __func__,
4660 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4661 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4662 args->bc_attrs.max_reqs);
4665 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4667 if (rcvd <= sent)
4668 return 0;
4669 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4670 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4671 return -EINVAL;
4674 #define _verify_fore_channel_attr(_name_) \
4675 _verify_channel_attr("fore", #_name_, \
4676 args->fc_attrs._name_, \
4677 session->fc_attrs._name_)
4679 #define _verify_back_channel_attr(_name_) \
4680 _verify_channel_attr("back", #_name_, \
4681 args->bc_attrs._name_, \
4682 session->bc_attrs._name_)
4685 * The server is not allowed to increase the fore channel header pad size,
4686 * maximum response size, or maximum number of operations.
4688 * The back channel attributes are only negotiatied down: We send what the
4689 * (back channel) server insists upon.
4691 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4692 struct nfs4_session *session)
4694 int ret = 0;
4696 ret |= _verify_fore_channel_attr(headerpadsz);
4697 ret |= _verify_fore_channel_attr(max_resp_sz);
4698 ret |= _verify_fore_channel_attr(max_ops);
4700 ret |= _verify_back_channel_attr(headerpadsz);
4701 ret |= _verify_back_channel_attr(max_rqst_sz);
4702 ret |= _verify_back_channel_attr(max_resp_sz);
4703 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4704 ret |= _verify_back_channel_attr(max_ops);
4705 ret |= _verify_back_channel_attr(max_reqs);
4707 return ret;
4710 static int _nfs4_proc_create_session(struct nfs_client *clp)
4712 struct nfs4_session *session = clp->cl_session;
4713 struct nfs41_create_session_args args = {
4714 .client = clp,
4715 .cb_program = NFS4_CALLBACK,
4717 struct nfs41_create_session_res res = {
4718 .client = clp,
4720 struct rpc_message msg = {
4721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4722 .rpc_argp = &args,
4723 .rpc_resp = &res,
4725 int status;
4727 nfs4_init_channel_attrs(&args);
4728 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4730 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4732 if (!status)
4733 /* Verify the session's negotiated channel_attrs values */
4734 status = nfs4_verify_channel_attrs(&args, session);
4735 if (!status) {
4736 /* Increment the clientid slot sequence id */
4737 clp->cl_seqid++;
4740 return status;
4744 * Issues a CREATE_SESSION operation to the server.
4745 * It is the responsibility of the caller to verify the session is
4746 * expired before calling this routine.
4748 int nfs4_proc_create_session(struct nfs_client *clp, int reset)
4750 int status;
4751 unsigned *ptr;
4752 struct nfs_fsinfo fsinfo;
4753 struct nfs4_session *session = clp->cl_session;
4755 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4757 status = _nfs4_proc_create_session(clp);
4758 if (status)
4759 goto out;
4761 /* Init or reset the fore channel */
4762 if (reset)
4763 status = nfs4_reset_slot_tables(session);
4764 else
4765 status = nfs4_init_slot_tables(session);
4766 dprintk("fore channel slot table initialization returned %d\n", status);
4767 if (status)
4768 goto out;
4770 ptr = (unsigned *)&session->sess_id.data[0];
4771 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4772 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4774 if (reset)
4775 /* Lease time is aleady set */
4776 goto out;
4778 /* Get the lease time */
4779 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4780 if (status == 0) {
4781 /* Update lease time and schedule renewal */
4782 spin_lock(&clp->cl_lock);
4783 clp->cl_lease_time = fsinfo.lease_time * HZ;
4784 clp->cl_last_renewal = jiffies;
4785 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4786 spin_unlock(&clp->cl_lock);
4788 nfs4_schedule_state_renewal(clp);
4790 out:
4791 dprintk("<-- %s\n", __func__);
4792 return status;
4796 * Issue the over-the-wire RPC DESTROY_SESSION.
4797 * The caller must serialize access to this routine.
4799 int nfs4_proc_destroy_session(struct nfs4_session *session)
4801 int status = 0;
4802 struct rpc_message msg;
4804 dprintk("--> nfs4_proc_destroy_session\n");
4806 /* session is still being setup */
4807 if (session->clp->cl_cons_state != NFS_CS_READY)
4808 return status;
4810 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4811 msg.rpc_argp = session;
4812 msg.rpc_resp = NULL;
4813 msg.rpc_cred = NULL;
4814 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4816 if (status)
4817 printk(KERN_WARNING
4818 "Got error %d from the server on DESTROY_SESSION. "
4819 "Session has been destroyed regardless...\n", status);
4821 dprintk("<-- nfs4_proc_destroy_session\n");
4822 return status;
4825 int nfs4_init_session(struct nfs_server *server)
4827 struct nfs_client *clp = server->nfs_client;
4828 int ret;
4830 if (!nfs4_has_session(clp))
4831 return 0;
4833 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4834 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4835 ret = nfs4_recover_expired_lease(server);
4836 if (!ret)
4837 ret = nfs4_check_client_ready(clp);
4838 return ret;
4842 * Renew the cl_session lease.
4844 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4846 struct nfs4_sequence_args args;
4847 struct nfs4_sequence_res res;
4849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4851 .rpc_argp = &args,
4852 .rpc_resp = &res,
4853 .rpc_cred = cred,
4856 args.sa_cache_this = 0;
4858 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4859 &res, 0);
4862 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4864 struct nfs_client *clp = (struct nfs_client *)data;
4866 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4868 if (task->tk_status < 0) {
4869 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4871 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4872 == -EAGAIN) {
4873 nfs4_restart_rpc(task, clp);
4874 return;
4877 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4878 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4880 put_rpccred(task->tk_msg.rpc_cred);
4881 kfree(task->tk_msg.rpc_argp);
4882 kfree(task->tk_msg.rpc_resp);
4884 dprintk("<-- %s\n", __func__);
4887 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4889 struct nfs_client *clp;
4890 struct nfs4_sequence_args *args;
4891 struct nfs4_sequence_res *res;
4893 clp = (struct nfs_client *)data;
4894 args = task->tk_msg.rpc_argp;
4895 res = task->tk_msg.rpc_resp;
4897 if (nfs4_setup_sequence(clp, args, res, 0, task))
4898 return;
4899 rpc_call_start(task);
4902 static const struct rpc_call_ops nfs41_sequence_ops = {
4903 .rpc_call_done = nfs41_sequence_call_done,
4904 .rpc_call_prepare = nfs41_sequence_prepare,
4907 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4908 struct rpc_cred *cred)
4910 struct nfs4_sequence_args *args;
4911 struct nfs4_sequence_res *res;
4912 struct rpc_message msg = {
4913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4914 .rpc_cred = cred,
4917 args = kzalloc(sizeof(*args), GFP_KERNEL);
4918 if (!args)
4919 return -ENOMEM;
4920 res = kzalloc(sizeof(*res), GFP_KERNEL);
4921 if (!res) {
4922 kfree(args);
4923 return -ENOMEM;
4925 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4926 msg.rpc_argp = args;
4927 msg.rpc_resp = res;
4929 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4930 &nfs41_sequence_ops, (void *)clp);
4933 #endif /* CONFIG_NFS_V4_1 */
4935 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
4936 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4937 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4938 .recover_open = nfs4_open_reclaim,
4939 .recover_lock = nfs4_lock_reclaim,
4940 .establish_clid = nfs4_init_clientid,
4941 .get_clid_cred = nfs4_get_setclientid_cred,
4944 #if defined(CONFIG_NFS_V4_1)
4945 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4946 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4947 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4948 .recover_open = nfs4_open_reclaim,
4949 .recover_lock = nfs4_lock_reclaim,
4950 .establish_clid = nfs4_proc_exchange_id,
4951 .get_clid_cred = nfs4_get_exchange_id_cred,
4953 #endif /* CONFIG_NFS_V4_1 */
4955 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
4956 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4957 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4958 .recover_open = nfs4_open_expired,
4959 .recover_lock = nfs4_lock_expired,
4960 .establish_clid = nfs4_init_clientid,
4961 .get_clid_cred = nfs4_get_setclientid_cred,
4964 #if defined(CONFIG_NFS_V4_1)
4965 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
4966 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4967 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4968 .recover_open = nfs4_open_expired,
4969 .recover_lock = nfs4_lock_expired,
4970 .establish_clid = nfs4_proc_exchange_id,
4971 .get_clid_cred = nfs4_get_exchange_id_cred,
4973 #endif /* CONFIG_NFS_V4_1 */
4975 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
4976 .sched_state_renewal = nfs4_proc_async_renew,
4977 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
4978 .renew_lease = nfs4_proc_renew,
4981 #if defined(CONFIG_NFS_V4_1)
4982 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
4983 .sched_state_renewal = nfs41_proc_async_sequence,
4984 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
4985 .renew_lease = nfs4_proc_sequence,
4987 #endif
4990 * Per minor version reboot and network partition recovery ops
4993 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
4994 &nfs40_reboot_recovery_ops,
4995 #if defined(CONFIG_NFS_V4_1)
4996 &nfs41_reboot_recovery_ops,
4997 #endif
5000 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5001 &nfs40_nograce_recovery_ops,
5002 #if defined(CONFIG_NFS_V4_1)
5003 &nfs41_nograce_recovery_ops,
5004 #endif
5007 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5008 &nfs40_state_renewal_ops,
5009 #if defined(CONFIG_NFS_V4_1)
5010 &nfs41_state_renewal_ops,
5011 #endif
5014 static const struct inode_operations nfs4_file_inode_operations = {
5015 .permission = nfs_permission,
5016 .getattr = nfs_getattr,
5017 .setattr = nfs_setattr,
5018 .getxattr = nfs4_getxattr,
5019 .setxattr = nfs4_setxattr,
5020 .listxattr = nfs4_listxattr,
5023 const struct nfs_rpc_ops nfs_v4_clientops = {
5024 .version = 4, /* protocol version */
5025 .dentry_ops = &nfs4_dentry_operations,
5026 .dir_inode_ops = &nfs4_dir_inode_operations,
5027 .file_inode_ops = &nfs4_file_inode_operations,
5028 .getroot = nfs4_proc_get_root,
5029 .getattr = nfs4_proc_getattr,
5030 .setattr = nfs4_proc_setattr,
5031 .lookupfh = nfs4_proc_lookupfh,
5032 .lookup = nfs4_proc_lookup,
5033 .access = nfs4_proc_access,
5034 .readlink = nfs4_proc_readlink,
5035 .create = nfs4_proc_create,
5036 .remove = nfs4_proc_remove,
5037 .unlink_setup = nfs4_proc_unlink_setup,
5038 .unlink_done = nfs4_proc_unlink_done,
5039 .rename = nfs4_proc_rename,
5040 .link = nfs4_proc_link,
5041 .symlink = nfs4_proc_symlink,
5042 .mkdir = nfs4_proc_mkdir,
5043 .rmdir = nfs4_proc_remove,
5044 .readdir = nfs4_proc_readdir,
5045 .mknod = nfs4_proc_mknod,
5046 .statfs = nfs4_proc_statfs,
5047 .fsinfo = nfs4_proc_fsinfo,
5048 .pathconf = nfs4_proc_pathconf,
5049 .set_capabilities = nfs4_server_capabilities,
5050 .decode_dirent = nfs4_decode_dirent,
5051 .read_setup = nfs4_proc_read_setup,
5052 .read_done = nfs4_read_done,
5053 .write_setup = nfs4_proc_write_setup,
5054 .write_done = nfs4_write_done,
5055 .commit_setup = nfs4_proc_commit_setup,
5056 .commit_done = nfs4_commit_done,
5057 .lock = nfs4_proc_lock,
5058 .clear_acl_cache = nfs4_zap_acl_attr,
5059 .close_context = nfs4_close_context,
5063 * Local variables:
5064 * c-basic-offset: 8
5065 * End: