init from v2.6.32.60
[mach-moxart.git] / fs / nfs / nfs4proc.c
blob21c7190691d83b97ee2ddbb3e5dee7bad2a4b37b
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 return err;
77 switch (err) {
78 case -NFS4ERR_RESOURCE:
79 return -EREMOTEIO;
80 default:
81 dprintk("%s could not handle NFSv4 error %d\n",
82 __func__, -err);
83 break;
85 return -EIO;
89 * This is our standard bitmap for GETATTR requests.
91 const u32 nfs4_fattr_bitmap[2] = {
92 FATTR4_WORD0_TYPE
93 | FATTR4_WORD0_CHANGE
94 | FATTR4_WORD0_SIZE
95 | FATTR4_WORD0_FSID
96 | FATTR4_WORD0_FILEID,
97 FATTR4_WORD1_MODE
98 | FATTR4_WORD1_NUMLINKS
99 | FATTR4_WORD1_OWNER
100 | FATTR4_WORD1_OWNER_GROUP
101 | FATTR4_WORD1_RAWDEV
102 | FATTR4_WORD1_SPACE_USED
103 | FATTR4_WORD1_TIME_ACCESS
104 | FATTR4_WORD1_TIME_METADATA
105 | FATTR4_WORD1_TIME_MODIFY
108 const u32 nfs4_statfs_bitmap[2] = {
109 FATTR4_WORD0_FILES_AVAIL
110 | FATTR4_WORD0_FILES_FREE
111 | FATTR4_WORD0_FILES_TOTAL,
112 FATTR4_WORD1_SPACE_AVAIL
113 | FATTR4_WORD1_SPACE_FREE
114 | FATTR4_WORD1_SPACE_TOTAL
117 const u32 nfs4_pathconf_bitmap[2] = {
118 FATTR4_WORD0_MAXLINK
119 | FATTR4_WORD0_MAXNAME,
123 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
124 | FATTR4_WORD0_MAXREAD
125 | FATTR4_WORD0_MAXWRITE
126 | FATTR4_WORD0_LEASE_TIME,
130 const u32 nfs4_fs_locations_bitmap[2] = {
131 FATTR4_WORD0_TYPE
132 | FATTR4_WORD0_CHANGE
133 | FATTR4_WORD0_SIZE
134 | FATTR4_WORD0_FSID
135 | FATTR4_WORD0_FILEID
136 | FATTR4_WORD0_FS_LOCATIONS,
137 FATTR4_WORD1_MODE
138 | FATTR4_WORD1_NUMLINKS
139 | FATTR4_WORD1_OWNER
140 | FATTR4_WORD1_OWNER_GROUP
141 | FATTR4_WORD1_RAWDEV
142 | FATTR4_WORD1_SPACE_USED
143 | FATTR4_WORD1_TIME_ACCESS
144 | FATTR4_WORD1_TIME_METADATA
145 | FATTR4_WORD1_TIME_MODIFY
146 | FATTR4_WORD1_MOUNTED_ON_FILEID
149 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
150 struct nfs4_readdir_arg *readdir)
152 __be32 *start, *p;
154 BUG_ON(readdir->count < 80);
155 if (cookie > 2) {
156 readdir->cookie = cookie;
157 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
158 return;
161 readdir->cookie = 0;
162 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
163 if (cookie == 2)
164 return;
167 * NFSv4 servers do not return entries for '.' and '..'
168 * Therefore, we fake these entries here. We let '.'
169 * have cookie 0 and '..' have cookie 1. Note that
170 * when talking to the server, we always send cookie 0
171 * instead of 1 or 2.
173 start = p = kmap_atomic(*readdir->pages, KM_USER0);
175 if (cookie == 0) {
176 *p++ = xdr_one; /* next */
177 *p++ = xdr_zero; /* cookie, first word */
178 *p++ = xdr_one; /* cookie, second word */
179 *p++ = xdr_one; /* entry len */
180 memcpy(p, ".\0\0\0", 4); /* entry */
181 p++;
182 *p++ = xdr_one; /* bitmap length */
183 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
184 *p++ = htonl(8); /* attribute buffer length */
185 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
188 *p++ = xdr_one; /* next */
189 *p++ = xdr_zero; /* cookie, first word */
190 *p++ = xdr_two; /* cookie, second word */
191 *p++ = xdr_two; /* entry len */
192 memcpy(p, "..\0\0", 4); /* entry */
193 p++;
194 *p++ = xdr_one; /* bitmap length */
195 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
196 *p++ = htonl(8); /* attribute buffer length */
197 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
199 readdir->pgbase = (char *)p - (char *)start;
200 readdir->count -= readdir->pgbase;
201 kunmap_atomic(start, KM_USER0);
204 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
206 int res;
208 might_sleep();
210 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
211 nfs_wait_bit_killable, TASK_KILLABLE);
212 return res;
215 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
217 int res = 0;
219 might_sleep();
221 if (*timeout <= 0)
222 *timeout = NFS4_POLL_RETRY_MIN;
223 if (*timeout > NFS4_POLL_RETRY_MAX)
224 *timeout = NFS4_POLL_RETRY_MAX;
225 schedule_timeout_killable(*timeout);
226 if (fatal_signal_pending(current))
227 res = -ERESTARTSYS;
228 *timeout <<= 1;
229 return res;
232 /* This is the error handling routine for processes that are allowed
233 * to sleep.
235 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
237 struct nfs_client *clp = server->nfs_client;
238 struct nfs4_state *state = exception->state;
239 int ret = errorcode;
241 exception->retry = 0;
242 switch(errorcode) {
243 case 0:
244 return 0;
245 case -NFS4ERR_ADMIN_REVOKED:
246 case -NFS4ERR_BAD_STATEID:
247 case -NFS4ERR_OPENMODE:
248 if (state == NULL)
249 break;
250 nfs4_state_mark_reclaim_nograce(clp, state);
251 case -NFS4ERR_STALE_CLIENTID:
252 case -NFS4ERR_STALE_STATEID:
253 case -NFS4ERR_EXPIRED:
254 nfs4_schedule_state_recovery(clp);
255 ret = nfs4_wait_clnt_recover(clp);
256 if (ret == 0)
257 exception->retry = 1;
258 #if !defined(CONFIG_NFS_V4_1)
259 break;
260 #else /* !defined(CONFIG_NFS_V4_1) */
261 if (!nfs4_has_session(server->nfs_client))
262 break;
263 /* FALLTHROUGH */
264 case -NFS4ERR_BADSESSION:
265 case -NFS4ERR_BADSLOT:
266 case -NFS4ERR_BAD_HIGH_SLOT:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268 case -NFS4ERR_DEADSESSION:
269 case -NFS4ERR_SEQ_FALSE_RETRY:
270 case -NFS4ERR_SEQ_MISORDERED:
271 dprintk("%s ERROR: %d Reset session\n", __func__,
272 errorcode);
273 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
274 exception->retry = 1;
275 /* FALLTHROUGH */
276 #endif /* !defined(CONFIG_NFS_V4_1) */
277 case -NFS4ERR_FILE_OPEN:
278 case -NFS4ERR_GRACE:
279 case -NFS4ERR_DELAY:
280 ret = nfs4_delay(server->client, &exception->timeout);
281 if (ret != 0)
282 break;
283 case -NFS4ERR_OLD_STATEID:
284 exception->retry = 1;
286 /* We failed to handle the error */
287 return nfs4_map_errors(ret);
291 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
293 struct nfs_client *clp = server->nfs_client;
294 spin_lock(&clp->cl_lock);
295 if (time_before(clp->cl_last_renewal,timestamp))
296 clp->cl_last_renewal = timestamp;
297 spin_unlock(&clp->cl_lock);
300 #if defined(CONFIG_NFS_V4_1)
303 * nfs4_free_slot - free a slot and efficiently update slot table.
305 * freeing a slot is trivially done by clearing its respective bit
306 * in the bitmap.
307 * If the freed slotid equals highest_used_slotid we want to update it
308 * so that the server would be able to size down the slot table if needed,
309 * otherwise we know that the highest_used_slotid is still in use.
310 * When updating highest_used_slotid there may be "holes" in the bitmap
311 * so we need to scan down from highest_used_slotid to 0 looking for the now
312 * highest slotid in use.
313 * If none found, highest_used_slotid is set to -1.
315 static void
316 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
318 int slotid = free_slotid;
320 spin_lock(&tbl->slot_tbl_lock);
321 /* clear used bit in bitmap */
322 __clear_bit(slotid, tbl->used_slots);
324 /* update highest_used_slotid when it is freed */
325 if (slotid == tbl->highest_used_slotid) {
326 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
327 if (slotid >= 0 && slotid < tbl->max_slots)
328 tbl->highest_used_slotid = slotid;
329 else
330 tbl->highest_used_slotid = -1;
332 rpc_wake_up_next(&tbl->slot_tbl_waitq);
333 spin_unlock(&tbl->slot_tbl_lock);
334 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
335 free_slotid, tbl->highest_used_slotid);
338 void nfs41_sequence_free_slot(const struct nfs_client *clp,
339 struct nfs4_sequence_res *res)
341 struct nfs4_slot_table *tbl;
343 if (!nfs4_has_session(clp)) {
344 dprintk("%s: No session\n", __func__);
345 return;
347 tbl = &clp->cl_session->fc_slot_table;
348 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
349 dprintk("%s: No slot\n", __func__);
350 /* just wake up the next guy waiting since
351 * we may have not consumed a slot after all */
352 rpc_wake_up_next(&tbl->slot_tbl_waitq);
353 return;
355 nfs4_free_slot(tbl, res->sr_slotid);
356 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
359 static void nfs41_sequence_done(struct nfs_client *clp,
360 struct nfs4_sequence_res *res,
361 int rpc_status)
363 unsigned long timestamp;
364 struct nfs4_slot_table *tbl;
365 struct nfs4_slot *slot;
368 * sr_status remains 1 if an RPC level error occurred. The server
369 * may or may not have processed the sequence operation..
370 * Proceed as if the server received and processed the sequence
371 * operation.
373 if (res->sr_status == 1)
374 res->sr_status = NFS_OK;
376 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
377 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
378 goto out;
380 tbl = &clp->cl_session->fc_slot_table;
381 slot = tbl->slots + res->sr_slotid;
383 if (res->sr_status == 0) {
384 /* Update the slot's sequence and clientid lease timer */
385 ++slot->seq_nr;
386 timestamp = res->sr_renewal_time;
387 spin_lock(&clp->cl_lock);
388 if (time_before(clp->cl_last_renewal, timestamp))
389 clp->cl_last_renewal = timestamp;
390 spin_unlock(&clp->cl_lock);
391 return;
393 out:
394 /* The session may be reset by one of the error handlers. */
395 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
396 nfs41_sequence_free_slot(clp, res);
400 * nfs4_find_slot - efficiently look for a free slot
402 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
403 * If found, we mark the slot as used, update the highest_used_slotid,
404 * and respectively set up the sequence operation args.
405 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
407 * Note: must be called with under the slot_tbl_lock.
409 static u8
410 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
412 int slotid;
413 u8 ret_id = NFS4_MAX_SLOT_TABLE;
414 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
416 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
417 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
418 tbl->max_slots);
419 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
420 if (slotid >= tbl->max_slots)
421 goto out;
422 __set_bit(slotid, tbl->used_slots);
423 if (slotid > tbl->highest_used_slotid)
424 tbl->highest_used_slotid = slotid;
425 ret_id = slotid;
426 out:
427 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
428 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
429 return ret_id;
432 static int nfs4_recover_session(struct nfs4_session *session)
434 struct nfs_client *clp = session->clp;
435 unsigned int loop;
436 int ret;
438 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
439 ret = nfs4_wait_clnt_recover(clp);
440 if (ret != 0)
441 break;
442 if (!test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
443 break;
444 nfs4_schedule_state_manager(clp);
445 ret = -EIO;
447 return ret;
450 static int nfs41_setup_sequence(struct nfs4_session *session,
451 struct nfs4_sequence_args *args,
452 struct nfs4_sequence_res *res,
453 int cache_reply,
454 struct rpc_task *task)
456 struct nfs4_slot *slot;
457 struct nfs4_slot_table *tbl;
458 int status = 0;
459 u8 slotid;
461 dprintk("--> %s\n", __func__);
462 /* slot already allocated? */
463 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
464 return 0;
466 memset(res, 0, sizeof(*res));
467 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
468 tbl = &session->fc_slot_table;
470 spin_lock(&tbl->slot_tbl_lock);
471 if (test_bit(NFS4CLNT_SESSION_SETUP, &session->clp->cl_state)) {
472 if (tbl->highest_used_slotid != -1) {
473 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
474 spin_unlock(&tbl->slot_tbl_lock);
475 dprintk("<-- %s: Session reset: draining\n", __func__);
476 return -EAGAIN;
479 /* The slot table is empty; start the reset thread */
480 dprintk("%s Session Reset\n", __func__);
481 spin_unlock(&tbl->slot_tbl_lock);
482 status = nfs4_recover_session(session);
483 if (status)
484 return status;
485 spin_lock(&tbl->slot_tbl_lock);
488 slotid = nfs4_find_slot(tbl, task);
489 if (slotid == NFS4_MAX_SLOT_TABLE) {
490 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
491 spin_unlock(&tbl->slot_tbl_lock);
492 dprintk("<-- %s: no free slots\n", __func__);
493 return -EAGAIN;
495 spin_unlock(&tbl->slot_tbl_lock);
497 slot = tbl->slots + slotid;
498 args->sa_session = session;
499 args->sa_slotid = slotid;
500 args->sa_cache_this = cache_reply;
502 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
504 res->sr_session = session;
505 res->sr_slotid = slotid;
506 res->sr_renewal_time = jiffies;
508 * sr_status is only set in decode_sequence, and so will remain
509 * set to 1 if an rpc level failure occurs.
511 res->sr_status = 1;
512 return 0;
515 int nfs4_setup_sequence(struct nfs_client *clp,
516 struct nfs4_sequence_args *args,
517 struct nfs4_sequence_res *res,
518 int cache_reply,
519 struct rpc_task *task)
521 int ret = 0;
523 dprintk("--> %s clp %p session %p sr_slotid %d\n",
524 __func__, clp, clp->cl_session, res->sr_slotid);
526 if (!nfs4_has_session(clp))
527 goto out;
528 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
529 task);
530 if (ret != -EAGAIN) {
531 /* terminate rpc task */
532 task->tk_status = ret;
533 task->tk_action = NULL;
535 out:
536 dprintk("<-- %s status=%d\n", __func__, ret);
537 return ret;
540 struct nfs41_call_sync_data {
541 struct nfs_client *clp;
542 struct nfs4_sequence_args *seq_args;
543 struct nfs4_sequence_res *seq_res;
544 int cache_reply;
547 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
549 struct nfs41_call_sync_data *data = calldata;
551 dprintk("--> %s data->clp->cl_session %p\n", __func__,
552 data->clp->cl_session);
553 if (nfs4_setup_sequence(data->clp, data->seq_args,
554 data->seq_res, data->cache_reply, task))
555 return;
556 rpc_call_start(task);
559 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
561 struct nfs41_call_sync_data *data = calldata;
563 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
564 nfs41_sequence_free_slot(data->clp, data->seq_res);
567 struct rpc_call_ops nfs41_call_sync_ops = {
568 .rpc_call_prepare = nfs41_call_sync_prepare,
569 .rpc_call_done = nfs41_call_sync_done,
572 static int nfs4_call_sync_sequence(struct nfs_client *clp,
573 struct rpc_clnt *clnt,
574 struct rpc_message *msg,
575 struct nfs4_sequence_args *args,
576 struct nfs4_sequence_res *res,
577 int cache_reply)
579 int ret;
580 struct rpc_task *task;
581 struct nfs41_call_sync_data data = {
582 .clp = clp,
583 .seq_args = args,
584 .seq_res = res,
585 .cache_reply = cache_reply,
587 struct rpc_task_setup task_setup = {
588 .rpc_client = clnt,
589 .rpc_message = msg,
590 .callback_ops = &nfs41_call_sync_ops,
591 .callback_data = &data
594 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
595 task = rpc_run_task(&task_setup);
596 if (IS_ERR(task))
597 ret = PTR_ERR(task);
598 else {
599 ret = task->tk_status;
600 rpc_put_task(task);
602 return ret;
605 int _nfs4_call_sync_session(struct nfs_server *server,
606 struct rpc_message *msg,
607 struct nfs4_sequence_args *args,
608 struct nfs4_sequence_res *res,
609 int cache_reply)
611 return nfs4_call_sync_sequence(server->nfs_client, server->client,
612 msg, args, res, cache_reply);
615 #endif /* CONFIG_NFS_V4_1 */
617 int _nfs4_call_sync(struct nfs_server *server,
618 struct rpc_message *msg,
619 struct nfs4_sequence_args *args,
620 struct nfs4_sequence_res *res,
621 int cache_reply)
623 args->sa_session = res->sr_session = NULL;
624 return rpc_call_sync(server->client, msg, 0);
627 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
628 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
629 &(res)->seq_res, (cache_reply))
631 static void nfs4_sequence_done(const struct nfs_server *server,
632 struct nfs4_sequence_res *res, int rpc_status)
634 #ifdef CONFIG_NFS_V4_1
635 if (nfs4_has_session(server->nfs_client))
636 nfs41_sequence_done(server->nfs_client, res, rpc_status);
637 #endif /* CONFIG_NFS_V4_1 */
640 /* no restart, therefore free slot here */
641 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
642 struct nfs4_sequence_res *res,
643 int rpc_status)
645 nfs4_sequence_done(server, res, rpc_status);
646 nfs4_sequence_free_slot(server->nfs_client, res);
649 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
651 struct nfs_inode *nfsi = NFS_I(dir);
653 spin_lock(&dir->i_lock);
654 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
655 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
656 nfs_force_lookup_revalidate(dir);
657 nfsi->change_attr = cinfo->after;
658 spin_unlock(&dir->i_lock);
661 struct nfs4_opendata {
662 struct kref kref;
663 struct nfs_openargs o_arg;
664 struct nfs_openres o_res;
665 struct nfs_open_confirmargs c_arg;
666 struct nfs_open_confirmres c_res;
667 struct nfs_fattr f_attr;
668 struct nfs_fattr dir_attr;
669 struct path path;
670 struct dentry *dir;
671 struct nfs4_state_owner *owner;
672 struct nfs4_state *state;
673 struct iattr attrs;
674 unsigned long timestamp;
675 unsigned int rpc_done : 1;
676 int rpc_status;
677 int cancelled;
681 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
683 p->o_res.f_attr = &p->f_attr;
684 p->o_res.dir_attr = &p->dir_attr;
685 p->o_res.seqid = p->o_arg.seqid;
686 p->c_res.seqid = p->c_arg.seqid;
687 p->o_res.server = p->o_arg.server;
688 nfs_fattr_init(&p->f_attr);
689 nfs_fattr_init(&p->dir_attr);
690 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
693 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
694 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
695 const struct iattr *attrs)
697 struct dentry *parent = dget_parent(path->dentry);
698 struct inode *dir = parent->d_inode;
699 struct nfs_server *server = NFS_SERVER(dir);
700 struct nfs4_opendata *p;
702 p = kzalloc(sizeof(*p), GFP_KERNEL);
703 if (p == NULL)
704 goto err;
705 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
706 if (p->o_arg.seqid == NULL)
707 goto err_free;
708 p->path.mnt = mntget(path->mnt);
709 p->path.dentry = dget(path->dentry);
710 p->dir = parent;
711 p->owner = sp;
712 atomic_inc(&sp->so_count);
713 p->o_arg.fh = NFS_FH(dir);
714 p->o_arg.open_flags = flags;
715 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
716 p->o_arg.clientid = server->nfs_client->cl_clientid;
717 p->o_arg.id = sp->so_owner_id.id;
718 p->o_arg.name = &p->path.dentry->d_name;
719 p->o_arg.server = server;
720 p->o_arg.bitmask = server->attr_bitmask;
721 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
722 if (flags & O_EXCL) {
723 u32 *s = (u32 *) p->o_arg.u.verifier.data;
724 s[0] = jiffies;
725 s[1] = current->pid;
726 } else if (flags & O_CREAT) {
727 p->o_arg.u.attrs = &p->attrs;
728 memcpy(&p->attrs, attrs, sizeof(p->attrs));
730 p->c_arg.fh = &p->o_res.fh;
731 p->c_arg.stateid = &p->o_res.stateid;
732 p->c_arg.seqid = p->o_arg.seqid;
733 nfs4_init_opendata_res(p);
734 kref_init(&p->kref);
735 return p;
736 err_free:
737 kfree(p);
738 err:
739 dput(parent);
740 return NULL;
743 static void nfs4_opendata_free(struct kref *kref)
745 struct nfs4_opendata *p = container_of(kref,
746 struct nfs4_opendata, kref);
748 nfs_free_seqid(p->o_arg.seqid);
749 if (p->state != NULL)
750 nfs4_put_open_state(p->state);
751 nfs4_put_state_owner(p->owner);
752 dput(p->dir);
753 path_put(&p->path);
754 kfree(p);
757 static void nfs4_opendata_put(struct nfs4_opendata *p)
759 if (p != NULL)
760 kref_put(&p->kref, nfs4_opendata_free);
763 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
765 int ret;
767 ret = rpc_wait_for_completion_task(task);
768 return ret;
771 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
773 int ret = 0;
775 if (open_mode & O_EXCL)
776 goto out;
777 switch (mode & (FMODE_READ|FMODE_WRITE)) {
778 case FMODE_READ:
779 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
780 break;
781 case FMODE_WRITE:
782 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
783 break;
784 case FMODE_READ|FMODE_WRITE:
785 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
787 out:
788 return ret;
791 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
793 if ((delegation->type & fmode) != fmode)
794 return 0;
795 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
796 return 0;
797 nfs_mark_delegation_referenced(delegation);
798 return 1;
801 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
803 switch (fmode) {
804 case FMODE_WRITE:
805 state->n_wronly++;
806 break;
807 case FMODE_READ:
808 state->n_rdonly++;
809 break;
810 case FMODE_READ|FMODE_WRITE:
811 state->n_rdwr++;
813 nfs4_state_set_mode_locked(state, state->state | fmode);
816 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
818 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
819 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
820 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
821 switch (fmode) {
822 case FMODE_READ:
823 set_bit(NFS_O_RDONLY_STATE, &state->flags);
824 break;
825 case FMODE_WRITE:
826 set_bit(NFS_O_WRONLY_STATE, &state->flags);
827 break;
828 case FMODE_READ|FMODE_WRITE:
829 set_bit(NFS_O_RDWR_STATE, &state->flags);
833 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
835 write_seqlock(&state->seqlock);
836 nfs_set_open_stateid_locked(state, stateid, fmode);
837 write_sequnlock(&state->seqlock);
840 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
843 * Protect the call to nfs4_state_set_mode_locked and
844 * serialise the stateid update
846 write_seqlock(&state->seqlock);
847 if (deleg_stateid != NULL) {
848 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
849 set_bit(NFS_DELEGATED_STATE, &state->flags);
851 if (open_stateid != NULL)
852 nfs_set_open_stateid_locked(state, open_stateid, fmode);
853 write_sequnlock(&state->seqlock);
854 spin_lock(&state->owner->so_lock);
855 update_open_stateflags(state, fmode);
856 spin_unlock(&state->owner->so_lock);
859 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
861 struct nfs_inode *nfsi = NFS_I(state->inode);
862 struct nfs_delegation *deleg_cur;
863 int ret = 0;
865 fmode &= (FMODE_READ|FMODE_WRITE);
867 rcu_read_lock();
868 deleg_cur = rcu_dereference(nfsi->delegation);
869 if (deleg_cur == NULL)
870 goto no_delegation;
872 spin_lock(&deleg_cur->lock);
873 if (nfsi->delegation != deleg_cur ||
874 (deleg_cur->type & fmode) != fmode)
875 goto no_delegation_unlock;
877 if (delegation == NULL)
878 delegation = &deleg_cur->stateid;
879 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
880 goto no_delegation_unlock;
882 nfs_mark_delegation_referenced(deleg_cur);
883 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
884 ret = 1;
885 no_delegation_unlock:
886 spin_unlock(&deleg_cur->lock);
887 no_delegation:
888 rcu_read_unlock();
890 if (!ret && open_stateid != NULL) {
891 __update_open_stateid(state, open_stateid, NULL, fmode);
892 ret = 1;
895 return ret;
899 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
901 struct nfs_delegation *delegation;
903 rcu_read_lock();
904 delegation = rcu_dereference(NFS_I(inode)->delegation);
905 if (delegation == NULL || (delegation->type & fmode) == fmode) {
906 rcu_read_unlock();
907 return;
909 rcu_read_unlock();
910 nfs_inode_return_delegation(inode);
913 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
915 struct nfs4_state *state = opendata->state;
916 struct nfs_inode *nfsi = NFS_I(state->inode);
917 struct nfs_delegation *delegation;
918 int open_mode = opendata->o_arg.open_flags & O_EXCL;
919 fmode_t fmode = opendata->o_arg.fmode;
920 nfs4_stateid stateid;
921 int ret = -EAGAIN;
923 for (;;) {
924 if (can_open_cached(state, fmode, open_mode)) {
925 spin_lock(&state->owner->so_lock);
926 if (can_open_cached(state, fmode, open_mode)) {
927 update_open_stateflags(state, fmode);
928 spin_unlock(&state->owner->so_lock);
929 goto out_return_state;
931 spin_unlock(&state->owner->so_lock);
933 rcu_read_lock();
934 delegation = rcu_dereference(nfsi->delegation);
935 if (delegation == NULL ||
936 !can_open_delegated(delegation, fmode)) {
937 rcu_read_unlock();
938 break;
940 /* Save the delegation */
941 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
942 rcu_read_unlock();
943 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
944 if (ret != 0)
945 goto out;
946 ret = -EAGAIN;
948 /* Try to update the stateid using the delegation */
949 if (update_open_stateid(state, NULL, &stateid, fmode))
950 goto out_return_state;
952 out:
953 return ERR_PTR(ret);
954 out_return_state:
955 atomic_inc(&state->count);
956 return state;
959 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
961 struct inode *inode;
962 struct nfs4_state *state = NULL;
963 struct nfs_delegation *delegation;
964 int ret;
966 if (!data->rpc_done) {
967 state = nfs4_try_open_cached(data);
968 goto out;
971 ret = -EAGAIN;
972 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
973 goto err;
974 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
975 ret = PTR_ERR(inode);
976 if (IS_ERR(inode))
977 goto err;
978 ret = -ENOMEM;
979 state = nfs4_get_open_state(inode, data->owner);
980 if (state == NULL)
981 goto err_put_inode;
982 if (data->o_res.delegation_type != 0) {
983 int delegation_flags = 0;
985 rcu_read_lock();
986 delegation = rcu_dereference(NFS_I(inode)->delegation);
987 if (delegation)
988 delegation_flags = delegation->flags;
989 rcu_read_unlock();
990 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
991 nfs_inode_set_delegation(state->inode,
992 data->owner->so_cred,
993 &data->o_res);
994 else
995 nfs_inode_reclaim_delegation(state->inode,
996 data->owner->so_cred,
997 &data->o_res);
1000 update_open_stateid(state, &data->o_res.stateid, NULL,
1001 data->o_arg.fmode);
1002 iput(inode);
1003 out:
1004 return state;
1005 err_put_inode:
1006 iput(inode);
1007 err:
1008 return ERR_PTR(ret);
1011 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1013 struct nfs_inode *nfsi = NFS_I(state->inode);
1014 struct nfs_open_context *ctx;
1016 spin_lock(&state->inode->i_lock);
1017 list_for_each_entry(ctx, &nfsi->open_files, list) {
1018 if (ctx->state != state)
1019 continue;
1020 get_nfs_open_context(ctx);
1021 spin_unlock(&state->inode->i_lock);
1022 return ctx;
1024 spin_unlock(&state->inode->i_lock);
1025 return ERR_PTR(-ENOENT);
1028 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1030 struct nfs4_opendata *opendata;
1032 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1033 if (opendata == NULL)
1034 return ERR_PTR(-ENOMEM);
1035 opendata->state = state;
1036 atomic_inc(&state->count);
1037 return opendata;
1040 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1042 struct nfs4_state *newstate;
1043 int ret;
1045 opendata->o_arg.open_flags = 0;
1046 opendata->o_arg.fmode = fmode;
1047 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1048 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1049 nfs4_init_opendata_res(opendata);
1050 ret = _nfs4_proc_open(opendata);
1051 if (ret != 0)
1052 return ret;
1053 newstate = nfs4_opendata_to_nfs4_state(opendata);
1054 if (IS_ERR(newstate))
1055 return PTR_ERR(newstate);
1056 nfs4_close_state(&opendata->path, newstate, fmode);
1057 *res = newstate;
1058 return 0;
1061 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1063 struct nfs4_state *newstate;
1064 int ret;
1066 /* memory barrier prior to reading state->n_* */
1067 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1068 smp_rmb();
1069 if (state->n_rdwr != 0) {
1070 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1071 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1072 if (ret != 0)
1073 return ret;
1074 if (newstate != state)
1075 return -ESTALE;
1077 if (state->n_wronly != 0) {
1078 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1079 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1080 if (ret != 0)
1081 return ret;
1082 if (newstate != state)
1083 return -ESTALE;
1085 if (state->n_rdonly != 0) {
1086 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1087 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1088 if (ret != 0)
1089 return ret;
1090 if (newstate != state)
1091 return -ESTALE;
1094 * We may have performed cached opens for all three recoveries.
1095 * Check if we need to update the current stateid.
1097 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1098 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1099 write_seqlock(&state->seqlock);
1100 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1101 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1102 write_sequnlock(&state->seqlock);
1104 return 0;
1108 * OPEN_RECLAIM:
1109 * reclaim state on the server after a reboot.
1111 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1113 struct nfs_delegation *delegation;
1114 struct nfs4_opendata *opendata;
1115 fmode_t delegation_type = 0;
1116 int status;
1118 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1119 if (IS_ERR(opendata))
1120 return PTR_ERR(opendata);
1121 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1122 opendata->o_arg.fh = NFS_FH(state->inode);
1123 rcu_read_lock();
1124 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1125 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1126 delegation_type = delegation->type;
1127 rcu_read_unlock();
1128 opendata->o_arg.u.delegation_type = delegation_type;
1129 status = nfs4_open_recover(opendata, state);
1130 nfs4_opendata_put(opendata);
1131 return status;
1134 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1136 struct nfs_server *server = NFS_SERVER(state->inode);
1137 struct nfs4_exception exception = { };
1138 int err;
1139 do {
1140 err = _nfs4_do_open_reclaim(ctx, state);
1141 if (err != -NFS4ERR_DELAY)
1142 break;
1143 nfs4_handle_exception(server, err, &exception);
1144 } while (exception.retry);
1145 return err;
1148 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1150 struct nfs_open_context *ctx;
1151 int ret;
1153 ctx = nfs4_state_find_open_context(state);
1154 if (IS_ERR(ctx))
1155 return PTR_ERR(ctx);
1156 ret = nfs4_do_open_reclaim(ctx, state);
1157 put_nfs_open_context(ctx);
1158 return ret;
1161 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1163 struct nfs4_opendata *opendata;
1164 int ret;
1166 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1167 if (IS_ERR(opendata))
1168 return PTR_ERR(opendata);
1169 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1170 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1171 sizeof(opendata->o_arg.u.delegation.data));
1172 ret = nfs4_open_recover(opendata, state);
1173 nfs4_opendata_put(opendata);
1174 return ret;
1177 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1179 struct nfs4_exception exception = { };
1180 struct nfs_server *server = NFS_SERVER(state->inode);
1181 int err;
1182 do {
1183 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1184 switch (err) {
1185 case 0:
1186 case -ENOENT:
1187 case -ESTALE:
1188 goto out;
1189 case -NFS4ERR_STALE_CLIENTID:
1190 case -NFS4ERR_STALE_STATEID:
1191 case -NFS4ERR_EXPIRED:
1192 /* Don't recall a delegation if it was lost */
1193 nfs4_schedule_state_recovery(server->nfs_client);
1194 goto out;
1195 case -ERESTARTSYS:
1197 * The show must go on: exit, but mark the
1198 * stateid as needing recovery.
1200 case -NFS4ERR_ADMIN_REVOKED:
1201 case -NFS4ERR_BAD_STATEID:
1202 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1203 case -ENOMEM:
1204 err = 0;
1205 goto out;
1207 err = nfs4_handle_exception(server, err, &exception);
1208 } while (exception.retry);
1209 out:
1210 return err;
1213 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1215 struct nfs4_opendata *data = calldata;
1217 data->rpc_status = task->tk_status;
1218 if (RPC_ASSASSINATED(task))
1219 return;
1220 if (data->rpc_status == 0) {
1221 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1222 sizeof(data->o_res.stateid.data));
1223 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1224 renew_lease(data->o_res.server, data->timestamp);
1225 data->rpc_done = 1;
1229 static void nfs4_open_confirm_release(void *calldata)
1231 struct nfs4_opendata *data = calldata;
1232 struct nfs4_state *state = NULL;
1234 /* If this request hasn't been cancelled, do nothing */
1235 if (data->cancelled == 0)
1236 goto out_free;
1237 /* In case of error, no cleanup! */
1238 if (!data->rpc_done)
1239 goto out_free;
1240 state = nfs4_opendata_to_nfs4_state(data);
1241 if (!IS_ERR(state))
1242 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1243 out_free:
1244 nfs4_opendata_put(data);
1247 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1248 .rpc_call_done = nfs4_open_confirm_done,
1249 .rpc_release = nfs4_open_confirm_release,
1253 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1255 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1257 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1258 struct rpc_task *task;
1259 struct rpc_message msg = {
1260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1261 .rpc_argp = &data->c_arg,
1262 .rpc_resp = &data->c_res,
1263 .rpc_cred = data->owner->so_cred,
1265 struct rpc_task_setup task_setup_data = {
1266 .rpc_client = server->client,
1267 .rpc_message = &msg,
1268 .callback_ops = &nfs4_open_confirm_ops,
1269 .callback_data = data,
1270 .workqueue = nfsiod_workqueue,
1271 .flags = RPC_TASK_ASYNC,
1273 int status;
1275 kref_get(&data->kref);
1276 data->rpc_done = 0;
1277 data->rpc_status = 0;
1278 data->timestamp = jiffies;
1279 task = rpc_run_task(&task_setup_data);
1280 if (IS_ERR(task))
1281 return PTR_ERR(task);
1282 status = nfs4_wait_for_completion_rpc_task(task);
1283 if (status != 0) {
1284 data->cancelled = 1;
1285 smp_wmb();
1286 } else
1287 status = data->rpc_status;
1288 rpc_put_task(task);
1289 return status;
1292 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1294 struct nfs4_opendata *data = calldata;
1295 struct nfs4_state_owner *sp = data->owner;
1297 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1298 return;
1300 * Check if we still need to send an OPEN call, or if we can use
1301 * a delegation instead.
1303 if (data->state != NULL) {
1304 struct nfs_delegation *delegation;
1306 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1307 goto out_no_action;
1308 rcu_read_lock();
1309 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1310 if (delegation != NULL &&
1311 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1312 rcu_read_unlock();
1313 goto out_no_action;
1315 rcu_read_unlock();
1317 /* Update sequence id. */
1318 data->o_arg.id = sp->so_owner_id.id;
1319 data->o_arg.clientid = sp->so_client->cl_clientid;
1320 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1321 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1322 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1324 data->timestamp = jiffies;
1325 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1326 &data->o_arg.seq_args,
1327 &data->o_res.seq_res, 1, task))
1328 return;
1329 rpc_call_start(task);
1330 return;
1331 out_no_action:
1332 task->tk_action = NULL;
1336 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1338 struct nfs4_opendata *data = calldata;
1340 data->rpc_status = task->tk_status;
1342 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1343 task->tk_status);
1345 if (RPC_ASSASSINATED(task))
1346 return;
1347 if (task->tk_status == 0) {
1348 switch (data->o_res.f_attr->mode & S_IFMT) {
1349 case S_IFREG:
1350 break;
1351 case S_IFLNK:
1352 data->rpc_status = -ELOOP;
1353 break;
1354 case S_IFDIR:
1355 data->rpc_status = -EISDIR;
1356 break;
1357 default:
1358 data->rpc_status = -ENOTDIR;
1360 renew_lease(data->o_res.server, data->timestamp);
1361 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1362 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1364 data->rpc_done = 1;
1367 static void nfs4_open_release(void *calldata)
1369 struct nfs4_opendata *data = calldata;
1370 struct nfs4_state *state = NULL;
1372 /* If this request hasn't been cancelled, do nothing */
1373 if (data->cancelled == 0)
1374 goto out_free;
1375 /* In case of error, no cleanup! */
1376 if (data->rpc_status != 0 || !data->rpc_done)
1377 goto out_free;
1378 /* In case we need an open_confirm, no cleanup! */
1379 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1380 goto out_free;
1381 state = nfs4_opendata_to_nfs4_state(data);
1382 if (!IS_ERR(state))
1383 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1384 out_free:
1385 nfs4_opendata_put(data);
1388 static const struct rpc_call_ops nfs4_open_ops = {
1389 .rpc_call_prepare = nfs4_open_prepare,
1390 .rpc_call_done = nfs4_open_done,
1391 .rpc_release = nfs4_open_release,
1395 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1397 static int _nfs4_proc_open(struct nfs4_opendata *data)
1399 struct inode *dir = data->dir->d_inode;
1400 struct nfs_server *server = NFS_SERVER(dir);
1401 struct nfs_openargs *o_arg = &data->o_arg;
1402 struct nfs_openres *o_res = &data->o_res;
1403 struct rpc_task *task;
1404 struct rpc_message msg = {
1405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1406 .rpc_argp = o_arg,
1407 .rpc_resp = o_res,
1408 .rpc_cred = data->owner->so_cred,
1410 struct rpc_task_setup task_setup_data = {
1411 .rpc_client = server->client,
1412 .rpc_message = &msg,
1413 .callback_ops = &nfs4_open_ops,
1414 .callback_data = data,
1415 .workqueue = nfsiod_workqueue,
1416 .flags = RPC_TASK_ASYNC,
1418 int status;
1420 kref_get(&data->kref);
1421 data->rpc_done = 0;
1422 data->rpc_status = 0;
1423 data->cancelled = 0;
1424 task = rpc_run_task(&task_setup_data);
1425 if (IS_ERR(task))
1426 return PTR_ERR(task);
1427 status = nfs4_wait_for_completion_rpc_task(task);
1428 if (status != 0) {
1429 data->cancelled = 1;
1430 smp_wmb();
1431 } else
1432 status = data->rpc_status;
1433 rpc_put_task(task);
1434 if (status != 0 || !data->rpc_done)
1435 return status;
1437 if (o_res->fh.size == 0)
1438 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1440 if (o_arg->open_flags & O_CREAT) {
1441 update_changeattr(dir, &o_res->cinfo);
1442 nfs_post_op_update_inode(dir, o_res->dir_attr);
1443 } else
1444 nfs_refresh_inode(dir, o_res->dir_attr);
1445 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1446 server->caps &= ~NFS_CAP_POSIX_LOCK;
1447 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1448 status = _nfs4_proc_open_confirm(data);
1449 if (status != 0)
1450 return status;
1452 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1453 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1454 return 0;
1457 static int nfs4_recover_expired_lease(struct nfs_server *server)
1459 struct nfs_client *clp = server->nfs_client;
1460 unsigned int loop;
1461 int ret;
1463 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1464 ret = nfs4_wait_clnt_recover(clp);
1465 if (ret != 0)
1466 break;
1467 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1468 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1469 break;
1470 nfs4_schedule_state_recovery(clp);
1471 ret = -EIO;
1473 return ret;
1477 * OPEN_EXPIRED:
1478 * reclaim state on the server after a network partition.
1479 * Assumes caller holds the appropriate lock
1481 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1483 struct nfs4_opendata *opendata;
1484 int ret;
1486 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1487 if (IS_ERR(opendata))
1488 return PTR_ERR(opendata);
1489 ret = nfs4_open_recover(opendata, state);
1490 if (ret == -ESTALE)
1491 d_drop(ctx->path.dentry);
1492 nfs4_opendata_put(opendata);
1493 return ret;
1496 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1498 struct nfs_server *server = NFS_SERVER(state->inode);
1499 struct nfs4_exception exception = { };
1500 int err;
1502 do {
1503 err = _nfs4_open_expired(ctx, state);
1504 switch (err) {
1505 default:
1506 goto out;
1507 case -NFS4ERR_GRACE:
1508 case -NFS4ERR_DELAY:
1509 nfs4_handle_exception(server, err, &exception);
1510 err = 0;
1512 } while (exception.retry);
1513 out:
1514 return err;
1517 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1519 struct nfs_open_context *ctx;
1520 int ret;
1522 ctx = nfs4_state_find_open_context(state);
1523 if (IS_ERR(ctx))
1524 return PTR_ERR(ctx);
1525 ret = nfs4_do_open_expired(ctx, state);
1526 put_nfs_open_context(ctx);
1527 return ret;
1531 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1532 * fields corresponding to attributes that were used to store the verifier.
1533 * Make sure we clobber those fields in the later setattr call
1535 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1537 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1538 !(sattr->ia_valid & ATTR_ATIME_SET))
1539 sattr->ia_valid |= ATTR_ATIME;
1541 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1542 !(sattr->ia_valid & ATTR_MTIME_SET))
1543 sattr->ia_valid |= ATTR_MTIME;
1547 * Returns a referenced nfs4_state
1549 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)
1551 struct nfs4_state_owner *sp;
1552 struct nfs4_state *state = NULL;
1553 struct nfs_server *server = NFS_SERVER(dir);
1554 struct nfs4_opendata *opendata;
1555 int status;
1557 /* Protect against reboot recovery conflicts */
1558 status = -ENOMEM;
1559 if (!(sp = nfs4_get_state_owner(server, cred))) {
1560 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1561 goto out_err;
1563 status = nfs4_recover_expired_lease(server);
1564 if (status != 0)
1565 goto err_put_state_owner;
1566 if (path->dentry->d_inode != NULL)
1567 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1568 status = -ENOMEM;
1569 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1570 if (opendata == NULL)
1571 goto err_put_state_owner;
1573 if (path->dentry->d_inode != NULL)
1574 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1576 status = _nfs4_proc_open(opendata);
1577 if (status != 0)
1578 goto err_opendata_put;
1580 if (opendata->o_arg.open_flags & O_EXCL)
1581 nfs4_exclusive_attrset(opendata, sattr);
1583 state = nfs4_opendata_to_nfs4_state(opendata);
1584 status = PTR_ERR(state);
1585 if (IS_ERR(state))
1586 goto err_opendata_put;
1587 if (server->caps & NFS_CAP_POSIX_LOCK)
1588 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1589 nfs_revalidate_inode(server, state->inode);
1590 nfs4_opendata_put(opendata);
1591 nfs4_put_state_owner(sp);
1592 *res = state;
1593 return 0;
1594 err_opendata_put:
1595 nfs4_opendata_put(opendata);
1596 err_put_state_owner:
1597 nfs4_put_state_owner(sp);
1598 out_err:
1599 *res = NULL;
1600 return status;
1604 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)
1606 struct nfs4_exception exception = { };
1607 struct nfs4_state *res;
1608 int status;
1610 do {
1611 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1612 if (status == 0)
1613 break;
1614 /* NOTE: BAD_SEQID means the server and client disagree about the
1615 * book-keeping w.r.t. state-changing operations
1616 * (OPEN/CLOSE/LOCK/LOCKU...)
1617 * It is actually a sign of a bug on the client or on the server.
1619 * If we receive a BAD_SEQID error in the particular case of
1620 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1621 * have unhashed the old state_owner for us, and that we can
1622 * therefore safely retry using a new one. We should still warn
1623 * the user though...
1625 if (status == -NFS4ERR_BAD_SEQID) {
1626 printk(KERN_WARNING "NFS: v4 server %s "
1627 " returned a bad sequence-id error!\n",
1628 NFS_SERVER(dir)->nfs_client->cl_hostname);
1629 exception.retry = 1;
1630 continue;
1633 * BAD_STATEID on OPEN means that the server cancelled our
1634 * state before it received the OPEN_CONFIRM.
1635 * Recover by retrying the request as per the discussion
1636 * on Page 181 of RFC3530.
1638 if (status == -NFS4ERR_BAD_STATEID) {
1639 exception.retry = 1;
1640 continue;
1642 if (status == -EAGAIN) {
1643 /* We must have found a delegation */
1644 exception.retry = 1;
1645 continue;
1647 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1648 status, &exception));
1649 } while (exception.retry);
1650 return res;
1653 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1654 struct nfs_fattr *fattr, struct iattr *sattr,
1655 struct nfs4_state *state)
1657 struct nfs_server *server = NFS_SERVER(inode);
1658 struct nfs_setattrargs arg = {
1659 .fh = NFS_FH(inode),
1660 .iap = sattr,
1661 .server = server,
1662 .bitmask = server->attr_bitmask,
1664 struct nfs_setattrres res = {
1665 .fattr = fattr,
1666 .server = server,
1668 struct rpc_message msg = {
1669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1670 .rpc_argp = &arg,
1671 .rpc_resp = &res,
1672 .rpc_cred = cred,
1674 unsigned long timestamp = jiffies;
1675 int status;
1677 nfs_fattr_init(fattr);
1679 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1680 /* Use that stateid */
1681 } else if (state != NULL) {
1682 nfs4_copy_stateid(&arg.stateid, state, current->files);
1683 } else
1684 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1686 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1687 if (status == 0 && state != NULL)
1688 renew_lease(server, timestamp);
1689 return status;
1692 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1693 struct nfs_fattr *fattr, struct iattr *sattr,
1694 struct nfs4_state *state)
1696 struct nfs_server *server = NFS_SERVER(inode);
1697 struct nfs4_exception exception = { };
1698 int err;
1699 do {
1700 err = nfs4_handle_exception(server,
1701 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1702 &exception);
1703 } while (exception.retry);
1704 return err;
1707 struct nfs4_closedata {
1708 struct path path;
1709 struct inode *inode;
1710 struct nfs4_state *state;
1711 struct nfs_closeargs arg;
1712 struct nfs_closeres res;
1713 struct nfs_fattr fattr;
1714 unsigned long timestamp;
1717 static void nfs4_free_closedata(void *data)
1719 struct nfs4_closedata *calldata = data;
1720 struct nfs4_state_owner *sp = calldata->state->owner;
1722 nfs4_put_open_state(calldata->state);
1723 nfs_free_seqid(calldata->arg.seqid);
1724 nfs4_put_state_owner(sp);
1725 path_put(&calldata->path);
1726 kfree(calldata);
1729 static void nfs4_close_done(struct rpc_task *task, void *data)
1731 struct nfs4_closedata *calldata = data;
1732 struct nfs4_state *state = calldata->state;
1733 struct nfs_server *server = NFS_SERVER(calldata->inode);
1735 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1736 if (RPC_ASSASSINATED(task))
1737 return;
1738 /* hmm. we are done with the inode, and in the process of freeing
1739 * the state_owner. we keep this around to process errors
1741 switch (task->tk_status) {
1742 case 0:
1743 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1744 renew_lease(server, calldata->timestamp);
1745 break;
1746 case -NFS4ERR_STALE_STATEID:
1747 case -NFS4ERR_OLD_STATEID:
1748 case -NFS4ERR_BAD_STATEID:
1749 case -NFS4ERR_EXPIRED:
1750 if (calldata->arg.fmode == 0)
1751 break;
1752 default:
1753 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1754 nfs4_restart_rpc(task, server->nfs_client);
1755 return;
1758 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1759 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1762 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1764 struct nfs4_closedata *calldata = data;
1765 struct nfs4_state *state = calldata->state;
1766 int clear_rd, clear_wr, clear_rdwr;
1768 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1769 return;
1771 clear_rd = clear_wr = clear_rdwr = 0;
1772 spin_lock(&state->owner->so_lock);
1773 /* Calculate the change in open mode */
1774 if (state->n_rdwr == 0) {
1775 if (state->n_rdonly == 0) {
1776 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1777 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1779 if (state->n_wronly == 0) {
1780 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1781 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1784 spin_unlock(&state->owner->so_lock);
1785 if (!clear_rd && !clear_wr && !clear_rdwr) {
1786 /* Note: exit _without_ calling nfs4_close_done */
1787 task->tk_action = NULL;
1788 return;
1790 nfs_fattr_init(calldata->res.fattr);
1791 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1792 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1793 calldata->arg.fmode = FMODE_READ;
1794 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1795 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1796 calldata->arg.fmode = FMODE_WRITE;
1798 calldata->timestamp = jiffies;
1799 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1800 &calldata->arg.seq_args, &calldata->res.seq_res,
1801 1, task))
1802 return;
1803 rpc_call_start(task);
1806 static const struct rpc_call_ops nfs4_close_ops = {
1807 .rpc_call_prepare = nfs4_close_prepare,
1808 .rpc_call_done = nfs4_close_done,
1809 .rpc_release = nfs4_free_closedata,
1813 * It is possible for data to be read/written from a mem-mapped file
1814 * after the sys_close call (which hits the vfs layer as a flush).
1815 * This means that we can't safely call nfsv4 close on a file until
1816 * the inode is cleared. This in turn means that we are not good
1817 * NFSv4 citizens - we do not indicate to the server to update the file's
1818 * share state even when we are done with one of the three share
1819 * stateid's in the inode.
1821 * NOTE: Caller must be holding the sp->so_owner semaphore!
1823 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1825 struct nfs_server *server = NFS_SERVER(state->inode);
1826 struct nfs4_closedata *calldata;
1827 struct nfs4_state_owner *sp = state->owner;
1828 struct rpc_task *task;
1829 struct rpc_message msg = {
1830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1831 .rpc_cred = state->owner->so_cred,
1833 struct rpc_task_setup task_setup_data = {
1834 .rpc_client = server->client,
1835 .rpc_message = &msg,
1836 .callback_ops = &nfs4_close_ops,
1837 .workqueue = nfsiod_workqueue,
1838 .flags = RPC_TASK_ASYNC,
1840 int status = -ENOMEM;
1842 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1843 if (calldata == NULL)
1844 goto out;
1845 calldata->inode = state->inode;
1846 calldata->state = state;
1847 calldata->arg.fh = NFS_FH(state->inode);
1848 calldata->arg.stateid = &state->open_stateid;
1849 if (nfs4_has_session(server->nfs_client))
1850 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
1851 /* Serialization for the sequence id */
1852 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1853 if (calldata->arg.seqid == NULL)
1854 goto out_free_calldata;
1855 calldata->arg.fmode = 0;
1856 calldata->arg.bitmask = server->cache_consistency_bitmask;
1857 calldata->res.fattr = &calldata->fattr;
1858 calldata->res.seqid = calldata->arg.seqid;
1859 calldata->res.server = server;
1860 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1861 calldata->path.mnt = mntget(path->mnt);
1862 calldata->path.dentry = dget(path->dentry);
1864 msg.rpc_argp = &calldata->arg,
1865 msg.rpc_resp = &calldata->res,
1866 task_setup_data.callback_data = calldata;
1867 task = rpc_run_task(&task_setup_data);
1868 if (IS_ERR(task))
1869 return PTR_ERR(task);
1870 status = 0;
1871 if (wait)
1872 status = rpc_wait_for_completion_task(task);
1873 rpc_put_task(task);
1874 return status;
1875 out_free_calldata:
1876 kfree(calldata);
1877 out:
1878 nfs4_put_open_state(state);
1879 nfs4_put_state_owner(sp);
1880 return status;
1883 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1885 struct file *filp;
1886 int ret;
1888 /* If the open_intent is for execute, we have an extra check to make */
1889 if (fmode & FMODE_EXEC) {
1890 ret = nfs_may_open(state->inode,
1891 state->owner->so_cred,
1892 nd->intent.open.flags);
1893 if (ret < 0)
1894 goto out_close;
1896 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1897 if (!IS_ERR(filp)) {
1898 struct nfs_open_context *ctx;
1899 ctx = nfs_file_open_context(filp);
1900 ctx->state = state;
1901 return 0;
1903 ret = PTR_ERR(filp);
1904 out_close:
1905 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1906 return ret;
1909 struct dentry *
1910 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1912 struct path path = {
1913 .mnt = nd->path.mnt,
1914 .dentry = dentry,
1916 struct dentry *parent;
1917 struct iattr attr;
1918 struct rpc_cred *cred;
1919 struct nfs4_state *state;
1920 struct dentry *res;
1921 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1923 if (nd->flags & LOOKUP_CREATE) {
1924 attr.ia_mode = nd->intent.open.create_mode;
1925 attr.ia_valid = ATTR_MODE;
1926 if (!IS_POSIXACL(dir))
1927 attr.ia_mode &= ~current_umask();
1928 } else {
1929 attr.ia_valid = 0;
1930 BUG_ON(nd->intent.open.flags & O_CREAT);
1933 cred = rpc_lookup_cred();
1934 if (IS_ERR(cred))
1935 return (struct dentry *)cred;
1936 parent = dentry->d_parent;
1937 /* Protect against concurrent sillydeletes */
1938 nfs_block_sillyrename(parent);
1939 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1940 put_rpccred(cred);
1941 if (IS_ERR(state)) {
1942 if (PTR_ERR(state) == -ENOENT) {
1943 d_add(dentry, NULL);
1944 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1946 nfs_unblock_sillyrename(parent);
1947 return (struct dentry *)state;
1949 res = d_add_unique(dentry, igrab(state->inode));
1950 if (res != NULL)
1951 path.dentry = res;
1952 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1953 nfs_unblock_sillyrename(parent);
1954 nfs4_intent_set_file(nd, &path, state, fmode);
1955 return res;
1959 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1961 struct path path = {
1962 .mnt = nd->path.mnt,
1963 .dentry = dentry,
1965 struct rpc_cred *cred;
1966 struct nfs4_state *state;
1967 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1969 cred = rpc_lookup_cred();
1970 if (IS_ERR(cred))
1971 return PTR_ERR(cred);
1972 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1973 put_rpccred(cred);
1974 if (IS_ERR(state)) {
1975 switch (PTR_ERR(state)) {
1976 case -EPERM:
1977 case -EACCES:
1978 case -EDQUOT:
1979 case -ENOSPC:
1980 case -EROFS:
1981 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1982 return 1;
1983 default:
1984 goto out_drop;
1987 if (state->inode == dentry->d_inode) {
1988 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1989 nfs4_intent_set_file(nd, &path, state, fmode);
1990 return 1;
1992 nfs4_close_sync(&path, state, fmode);
1993 out_drop:
1994 d_drop(dentry);
1995 return 0;
1998 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2000 if (ctx->state == NULL)
2001 return;
2002 if (is_sync)
2003 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2004 else
2005 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2008 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2010 struct nfs4_server_caps_arg args = {
2011 .fhandle = fhandle,
2013 struct nfs4_server_caps_res res = {};
2014 struct rpc_message msg = {
2015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2016 .rpc_argp = &args,
2017 .rpc_resp = &res,
2019 int status;
2021 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2022 if (status == 0) {
2023 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2024 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2025 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2026 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2027 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2028 NFS_CAP_CTIME|NFS_CAP_MTIME);
2029 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2030 server->caps |= NFS_CAP_ACLS;
2031 if (res.has_links != 0)
2032 server->caps |= NFS_CAP_HARDLINKS;
2033 if (res.has_symlinks != 0)
2034 server->caps |= NFS_CAP_SYMLINKS;
2035 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2036 server->caps |= NFS_CAP_FILEID;
2037 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2038 server->caps |= NFS_CAP_MODE;
2039 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2040 server->caps |= NFS_CAP_NLINK;
2041 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2042 server->caps |= NFS_CAP_OWNER;
2043 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2044 server->caps |= NFS_CAP_OWNER_GROUP;
2045 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2046 server->caps |= NFS_CAP_ATIME;
2047 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2048 server->caps |= NFS_CAP_CTIME;
2049 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2050 server->caps |= NFS_CAP_MTIME;
2052 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2053 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2054 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2055 server->acl_bitmask = res.acl_bitmask;
2058 return status;
2061 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2063 struct nfs4_exception exception = { };
2064 int err;
2065 do {
2066 err = nfs4_handle_exception(server,
2067 _nfs4_server_capabilities(server, fhandle),
2068 &exception);
2069 } while (exception.retry);
2070 return err;
2073 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2074 struct nfs_fsinfo *info)
2076 struct nfs4_lookup_root_arg args = {
2077 .bitmask = nfs4_fattr_bitmap,
2079 struct nfs4_lookup_res res = {
2080 .server = server,
2081 .fattr = info->fattr,
2082 .fh = fhandle,
2084 struct rpc_message msg = {
2085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2086 .rpc_argp = &args,
2087 .rpc_resp = &res,
2090 nfs_fattr_init(info->fattr);
2091 return nfs4_call_sync(server, &msg, &args, &res, 0);
2094 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2095 struct nfs_fsinfo *info)
2097 struct nfs4_exception exception = { };
2098 int err;
2099 do {
2100 err = nfs4_handle_exception(server,
2101 _nfs4_lookup_root(server, fhandle, info),
2102 &exception);
2103 } while (exception.retry);
2104 return err;
2108 * get the file handle for the "/" directory on the server
2110 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2111 struct nfs_fsinfo *info)
2113 int status;
2115 status = nfs4_lookup_root(server, fhandle, info);
2116 if (status == 0)
2117 status = nfs4_server_capabilities(server, fhandle);
2118 if (status == 0)
2119 status = nfs4_do_fsinfo(server, fhandle, info);
2120 return nfs4_map_errors(status);
2124 * Get locations and (maybe) other attributes of a referral.
2125 * Note that we'll actually follow the referral later when
2126 * we detect fsid mismatch in inode revalidation
2128 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2130 int status = -ENOMEM;
2131 struct page *page = NULL;
2132 struct nfs4_fs_locations *locations = NULL;
2134 page = alloc_page(GFP_KERNEL);
2135 if (page == NULL)
2136 goto out;
2137 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2138 if (locations == NULL)
2139 goto out;
2141 status = nfs4_proc_fs_locations(dir, name, locations, page);
2142 if (status != 0)
2143 goto out;
2144 /* Make sure server returned a different fsid for the referral */
2145 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2146 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2147 status = -EIO;
2148 goto out;
2151 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2152 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2153 if (!fattr->mode)
2154 fattr->mode = S_IFDIR;
2155 memset(fhandle, 0, sizeof(struct nfs_fh));
2156 out:
2157 if (page)
2158 __free_page(page);
2159 if (locations)
2160 kfree(locations);
2161 return status;
2164 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2166 struct nfs4_getattr_arg args = {
2167 .fh = fhandle,
2168 .bitmask = server->attr_bitmask,
2170 struct nfs4_getattr_res res = {
2171 .fattr = fattr,
2172 .server = server,
2174 struct rpc_message msg = {
2175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2176 .rpc_argp = &args,
2177 .rpc_resp = &res,
2180 nfs_fattr_init(fattr);
2181 return nfs4_call_sync(server, &msg, &args, &res, 0);
2184 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2186 struct nfs4_exception exception = { };
2187 int err;
2188 do {
2189 err = nfs4_handle_exception(server,
2190 _nfs4_proc_getattr(server, fhandle, fattr),
2191 &exception);
2192 } while (exception.retry);
2193 return err;
2197 * The file is not closed if it is opened due to the a request to change
2198 * the size of the file. The open call will not be needed once the
2199 * VFS layer lookup-intents are implemented.
2201 * Close is called when the inode is destroyed.
2202 * If we haven't opened the file for O_WRONLY, we
2203 * need to in the size_change case to obtain a stateid.
2205 * Got race?
2206 * Because OPEN is always done by name in nfsv4, it is
2207 * possible that we opened a different file by the same
2208 * name. We can recognize this race condition, but we
2209 * can't do anything about it besides returning an error.
2211 * This will be fixed with VFS changes (lookup-intent).
2213 static int
2214 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2215 struct iattr *sattr)
2217 struct inode *inode = dentry->d_inode;
2218 struct rpc_cred *cred = NULL;
2219 struct nfs4_state *state = NULL;
2220 int status;
2222 nfs_fattr_init(fattr);
2224 /* Search for an existing open(O_WRITE) file */
2225 if (sattr->ia_valid & ATTR_FILE) {
2226 struct nfs_open_context *ctx;
2228 ctx = nfs_file_open_context(sattr->ia_file);
2229 if (ctx) {
2230 cred = ctx->cred;
2231 state = ctx->state;
2235 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2236 if (status == 0)
2237 nfs_setattr_update_inode(inode, sattr);
2238 return status;
2241 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2242 const struct qstr *name, struct nfs_fh *fhandle,
2243 struct nfs_fattr *fattr)
2245 int status;
2246 struct nfs4_lookup_arg args = {
2247 .bitmask = server->attr_bitmask,
2248 .dir_fh = dirfh,
2249 .name = name,
2251 struct nfs4_lookup_res res = {
2252 .server = server,
2253 .fattr = fattr,
2254 .fh = fhandle,
2256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2258 .rpc_argp = &args,
2259 .rpc_resp = &res,
2262 nfs_fattr_init(fattr);
2264 dprintk("NFS call lookupfh %s\n", name->name);
2265 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2266 dprintk("NFS reply lookupfh: %d\n", status);
2267 return status;
2270 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2271 struct qstr *name, struct nfs_fh *fhandle,
2272 struct nfs_fattr *fattr)
2274 struct nfs4_exception exception = { };
2275 int err;
2276 do {
2277 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2278 /* FIXME: !!!! */
2279 if (err == -NFS4ERR_MOVED) {
2280 err = -EREMOTE;
2281 break;
2283 err = nfs4_handle_exception(server, err, &exception);
2284 } while (exception.retry);
2285 return err;
2288 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2289 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2291 int status;
2293 dprintk("NFS call lookup %s\n", name->name);
2294 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2295 if (status == -NFS4ERR_MOVED)
2296 status = nfs4_get_referral(dir, name, fattr, fhandle);
2297 dprintk("NFS reply lookup: %d\n", status);
2298 return status;
2301 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2303 struct nfs4_exception exception = { };
2304 int err;
2305 do {
2306 err = nfs4_handle_exception(NFS_SERVER(dir),
2307 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2308 &exception);
2309 } while (exception.retry);
2310 return err;
2313 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2315 struct nfs_server *server = NFS_SERVER(inode);
2316 struct nfs_fattr fattr;
2317 struct nfs4_accessargs args = {
2318 .fh = NFS_FH(inode),
2319 .bitmask = server->attr_bitmask,
2321 struct nfs4_accessres res = {
2322 .server = server,
2323 .fattr = &fattr,
2325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2327 .rpc_argp = &args,
2328 .rpc_resp = &res,
2329 .rpc_cred = entry->cred,
2331 int mode = entry->mask;
2332 int status;
2335 * Determine which access bits we want to ask for...
2337 if (mode & MAY_READ)
2338 args.access |= NFS4_ACCESS_READ;
2339 if (S_ISDIR(inode->i_mode)) {
2340 if (mode & MAY_WRITE)
2341 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2342 if (mode & MAY_EXEC)
2343 args.access |= NFS4_ACCESS_LOOKUP;
2344 } else {
2345 if (mode & MAY_WRITE)
2346 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2347 if (mode & MAY_EXEC)
2348 args.access |= NFS4_ACCESS_EXECUTE;
2350 nfs_fattr_init(&fattr);
2351 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2352 if (!status) {
2353 entry->mask = 0;
2354 if (res.access & NFS4_ACCESS_READ)
2355 entry->mask |= MAY_READ;
2356 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2357 entry->mask |= MAY_WRITE;
2358 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2359 entry->mask |= MAY_EXEC;
2360 nfs_refresh_inode(inode, &fattr);
2362 return status;
2365 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2367 struct nfs4_exception exception = { };
2368 int err;
2369 do {
2370 err = nfs4_handle_exception(NFS_SERVER(inode),
2371 _nfs4_proc_access(inode, entry),
2372 &exception);
2373 } while (exception.retry);
2374 return err;
2378 * TODO: For the time being, we don't try to get any attributes
2379 * along with any of the zero-copy operations READ, READDIR,
2380 * READLINK, WRITE.
2382 * In the case of the first three, we want to put the GETATTR
2383 * after the read-type operation -- this is because it is hard
2384 * to predict the length of a GETATTR response in v4, and thus
2385 * align the READ data correctly. This means that the GETATTR
2386 * may end up partially falling into the page cache, and we should
2387 * shift it into the 'tail' of the xdr_buf before processing.
2388 * To do this efficiently, we need to know the total length
2389 * of data received, which doesn't seem to be available outside
2390 * of the RPC layer.
2392 * In the case of WRITE, we also want to put the GETATTR after
2393 * the operation -- in this case because we want to make sure
2394 * we get the post-operation mtime and size. This means that
2395 * we can't use xdr_encode_pages() as written: we need a variant
2396 * of it which would leave room in the 'tail' iovec.
2398 * Both of these changes to the XDR layer would in fact be quite
2399 * minor, but I decided to leave them for a subsequent patch.
2401 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2402 unsigned int pgbase, unsigned int pglen)
2404 struct nfs4_readlink args = {
2405 .fh = NFS_FH(inode),
2406 .pgbase = pgbase,
2407 .pglen = pglen,
2408 .pages = &page,
2410 struct nfs4_readlink_res res;
2411 struct rpc_message msg = {
2412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2413 .rpc_argp = &args,
2414 .rpc_resp = &res,
2417 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2420 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2421 unsigned int pgbase, unsigned int pglen)
2423 struct nfs4_exception exception = { };
2424 int err;
2425 do {
2426 err = nfs4_handle_exception(NFS_SERVER(inode),
2427 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2428 &exception);
2429 } while (exception.retry);
2430 return err;
2434 * Got race?
2435 * We will need to arrange for the VFS layer to provide an atomic open.
2436 * Until then, this create/open method is prone to inefficiency and race
2437 * conditions due to the lookup, create, and open VFS calls from sys_open()
2438 * placed on the wire.
2440 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2441 * The file will be opened again in the subsequent VFS open call
2442 * (nfs4_proc_file_open).
2444 * The open for read will just hang around to be used by any process that
2445 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2448 static int
2449 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2450 int flags, struct nameidata *nd)
2452 struct path path = {
2453 .mnt = nd->path.mnt,
2454 .dentry = dentry,
2456 struct nfs4_state *state;
2457 struct rpc_cred *cred;
2458 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2459 int status = 0;
2461 cred = rpc_lookup_cred();
2462 if (IS_ERR(cred)) {
2463 status = PTR_ERR(cred);
2464 goto out;
2466 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2467 d_drop(dentry);
2468 if (IS_ERR(state)) {
2469 status = PTR_ERR(state);
2470 goto out_putcred;
2472 d_add(dentry, igrab(state->inode));
2473 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2474 if (flags & O_EXCL) {
2475 struct nfs_fattr fattr;
2476 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2477 if (status == 0)
2478 nfs_setattr_update_inode(state->inode, sattr);
2479 nfs_post_op_update_inode(state->inode, &fattr);
2481 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2482 status = nfs4_intent_set_file(nd, &path, state, fmode);
2483 else
2484 nfs4_close_sync(&path, state, fmode);
2485 out_putcred:
2486 put_rpccred(cred);
2487 out:
2488 return status;
2491 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2493 struct nfs_server *server = NFS_SERVER(dir);
2494 struct nfs_removeargs args = {
2495 .fh = NFS_FH(dir),
2496 .name.len = name->len,
2497 .name.name = name->name,
2498 .bitmask = server->attr_bitmask,
2500 struct nfs_removeres res = {
2501 .server = server,
2503 struct rpc_message msg = {
2504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2505 .rpc_argp = &args,
2506 .rpc_resp = &res,
2508 int status;
2510 nfs_fattr_init(&res.dir_attr);
2511 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2512 if (status == 0) {
2513 update_changeattr(dir, &res.cinfo);
2514 nfs_post_op_update_inode(dir, &res.dir_attr);
2516 return status;
2519 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2521 struct nfs4_exception exception = { };
2522 int err;
2523 do {
2524 err = nfs4_handle_exception(NFS_SERVER(dir),
2525 _nfs4_proc_remove(dir, name),
2526 &exception);
2527 } while (exception.retry);
2528 return err;
2531 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2533 struct nfs_server *server = NFS_SERVER(dir);
2534 struct nfs_removeargs *args = msg->rpc_argp;
2535 struct nfs_removeres *res = msg->rpc_resp;
2537 args->bitmask = server->cache_consistency_bitmask;
2538 res->server = server;
2539 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2542 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2544 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2546 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2547 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2548 return 0;
2549 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2550 update_changeattr(dir, &res->cinfo);
2551 nfs_post_op_update_inode(dir, &res->dir_attr);
2552 return 1;
2555 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2556 struct inode *new_dir, struct qstr *new_name)
2558 struct nfs_server *server = NFS_SERVER(old_dir);
2559 struct nfs4_rename_arg arg = {
2560 .old_dir = NFS_FH(old_dir),
2561 .new_dir = NFS_FH(new_dir),
2562 .old_name = old_name,
2563 .new_name = new_name,
2564 .bitmask = server->attr_bitmask,
2566 struct nfs_fattr old_fattr, new_fattr;
2567 struct nfs4_rename_res res = {
2568 .server = server,
2569 .old_fattr = &old_fattr,
2570 .new_fattr = &new_fattr,
2572 struct rpc_message msg = {
2573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2574 .rpc_argp = &arg,
2575 .rpc_resp = &res,
2577 int status;
2579 nfs_fattr_init(res.old_fattr);
2580 nfs_fattr_init(res.new_fattr);
2581 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2583 if (!status) {
2584 update_changeattr(old_dir, &res.old_cinfo);
2585 nfs_post_op_update_inode(old_dir, res.old_fattr);
2586 update_changeattr(new_dir, &res.new_cinfo);
2587 nfs_post_op_update_inode(new_dir, res.new_fattr);
2589 return status;
2592 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2593 struct inode *new_dir, struct qstr *new_name)
2595 struct nfs4_exception exception = { };
2596 int err;
2597 do {
2598 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2599 _nfs4_proc_rename(old_dir, old_name,
2600 new_dir, new_name),
2601 &exception);
2602 } while (exception.retry);
2603 return err;
2606 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2608 struct nfs_server *server = NFS_SERVER(inode);
2609 struct nfs4_link_arg arg = {
2610 .fh = NFS_FH(inode),
2611 .dir_fh = NFS_FH(dir),
2612 .name = name,
2613 .bitmask = server->attr_bitmask,
2615 struct nfs_fattr fattr, dir_attr;
2616 struct nfs4_link_res res = {
2617 .server = server,
2618 .fattr = &fattr,
2619 .dir_attr = &dir_attr,
2621 struct rpc_message msg = {
2622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2623 .rpc_argp = &arg,
2624 .rpc_resp = &res,
2626 int status;
2628 nfs_fattr_init(res.fattr);
2629 nfs_fattr_init(res.dir_attr);
2630 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2631 if (!status) {
2632 update_changeattr(dir, &res.cinfo);
2633 nfs_post_op_update_inode(dir, res.dir_attr);
2634 nfs_post_op_update_inode(inode, res.fattr);
2637 return status;
2640 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2642 struct nfs4_exception exception = { };
2643 int err;
2644 do {
2645 err = nfs4_handle_exception(NFS_SERVER(inode),
2646 _nfs4_proc_link(inode, dir, name),
2647 &exception);
2648 } while (exception.retry);
2649 return err;
2652 struct nfs4_createdata {
2653 struct rpc_message msg;
2654 struct nfs4_create_arg arg;
2655 struct nfs4_create_res res;
2656 struct nfs_fh fh;
2657 struct nfs_fattr fattr;
2658 struct nfs_fattr dir_fattr;
2661 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2662 struct qstr *name, struct iattr *sattr, u32 ftype)
2664 struct nfs4_createdata *data;
2666 data = kzalloc(sizeof(*data), GFP_KERNEL);
2667 if (data != NULL) {
2668 struct nfs_server *server = NFS_SERVER(dir);
2670 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2671 data->msg.rpc_argp = &data->arg;
2672 data->msg.rpc_resp = &data->res;
2673 data->arg.dir_fh = NFS_FH(dir);
2674 data->arg.server = server;
2675 data->arg.name = name;
2676 data->arg.attrs = sattr;
2677 data->arg.ftype = ftype;
2678 data->arg.bitmask = server->attr_bitmask;
2679 data->res.server = server;
2680 data->res.fh = &data->fh;
2681 data->res.fattr = &data->fattr;
2682 data->res.dir_fattr = &data->dir_fattr;
2683 nfs_fattr_init(data->res.fattr);
2684 nfs_fattr_init(data->res.dir_fattr);
2686 return data;
2689 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2691 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2692 &data->arg, &data->res, 1);
2693 if (status == 0) {
2694 update_changeattr(dir, &data->res.dir_cinfo);
2695 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2696 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2698 return status;
2701 static void nfs4_free_createdata(struct nfs4_createdata *data)
2703 kfree(data);
2706 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2707 struct page *page, unsigned int len, struct iattr *sattr)
2709 struct nfs4_createdata *data;
2710 int status = -ENAMETOOLONG;
2712 if (len > NFS4_MAXPATHLEN)
2713 goto out;
2715 status = -ENOMEM;
2716 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2717 if (data == NULL)
2718 goto out;
2720 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2721 data->arg.u.symlink.pages = &page;
2722 data->arg.u.symlink.len = len;
2724 status = nfs4_do_create(dir, dentry, data);
2726 nfs4_free_createdata(data);
2727 out:
2728 return status;
2731 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2732 struct page *page, unsigned int len, struct iattr *sattr)
2734 struct nfs4_exception exception = { };
2735 int err;
2736 do {
2737 err = nfs4_handle_exception(NFS_SERVER(dir),
2738 _nfs4_proc_symlink(dir, dentry, page,
2739 len, sattr),
2740 &exception);
2741 } while (exception.retry);
2742 return err;
2745 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2746 struct iattr *sattr)
2748 struct nfs4_createdata *data;
2749 int status = -ENOMEM;
2751 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2752 if (data == NULL)
2753 goto out;
2755 status = nfs4_do_create(dir, dentry, data);
2757 nfs4_free_createdata(data);
2758 out:
2759 return status;
2762 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2763 struct iattr *sattr)
2765 struct nfs4_exception exception = { };
2766 int err;
2767 do {
2768 err = nfs4_handle_exception(NFS_SERVER(dir),
2769 _nfs4_proc_mkdir(dir, dentry, sattr),
2770 &exception);
2771 } while (exception.retry);
2772 return err;
2775 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2776 u64 cookie, struct page *page, unsigned int count, int plus)
2778 struct inode *dir = dentry->d_inode;
2779 struct nfs4_readdir_arg args = {
2780 .fh = NFS_FH(dir),
2781 .pages = &page,
2782 .pgbase = 0,
2783 .count = count,
2784 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2786 struct nfs4_readdir_res res;
2787 struct rpc_message msg = {
2788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2789 .rpc_argp = &args,
2790 .rpc_resp = &res,
2791 .rpc_cred = cred,
2793 int status;
2795 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2796 dentry->d_parent->d_name.name,
2797 dentry->d_name.name,
2798 (unsigned long long)cookie);
2799 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2800 res.pgbase = args.pgbase;
2801 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2802 if (status == 0)
2803 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2805 nfs_invalidate_atime(dir);
2807 dprintk("%s: returns %d\n", __func__, status);
2808 return status;
2811 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2812 u64 cookie, struct page *page, unsigned int count, int plus)
2814 struct nfs4_exception exception = { };
2815 int err;
2816 do {
2817 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2818 _nfs4_proc_readdir(dentry, cred, cookie,
2819 page, count, plus),
2820 &exception);
2821 } while (exception.retry);
2822 return err;
2825 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2826 struct iattr *sattr, dev_t rdev)
2828 struct nfs4_createdata *data;
2829 int mode = sattr->ia_mode;
2830 int status = -ENOMEM;
2832 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2833 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2835 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2836 if (data == NULL)
2837 goto out;
2839 if (S_ISFIFO(mode))
2840 data->arg.ftype = NF4FIFO;
2841 else if (S_ISBLK(mode)) {
2842 data->arg.ftype = NF4BLK;
2843 data->arg.u.device.specdata1 = MAJOR(rdev);
2844 data->arg.u.device.specdata2 = MINOR(rdev);
2846 else if (S_ISCHR(mode)) {
2847 data->arg.ftype = NF4CHR;
2848 data->arg.u.device.specdata1 = MAJOR(rdev);
2849 data->arg.u.device.specdata2 = MINOR(rdev);
2852 status = nfs4_do_create(dir, dentry, data);
2854 nfs4_free_createdata(data);
2855 out:
2856 return status;
2859 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2860 struct iattr *sattr, dev_t rdev)
2862 struct nfs4_exception exception = { };
2863 int err;
2864 do {
2865 err = nfs4_handle_exception(NFS_SERVER(dir),
2866 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2867 &exception);
2868 } while (exception.retry);
2869 return err;
2872 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2873 struct nfs_fsstat *fsstat)
2875 struct nfs4_statfs_arg args = {
2876 .fh = fhandle,
2877 .bitmask = server->attr_bitmask,
2879 struct nfs4_statfs_res res = {
2880 .fsstat = fsstat,
2882 struct rpc_message msg = {
2883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2884 .rpc_argp = &args,
2885 .rpc_resp = &res,
2888 nfs_fattr_init(fsstat->fattr);
2889 return nfs4_call_sync(server, &msg, &args, &res, 0);
2892 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2894 struct nfs4_exception exception = { };
2895 int err;
2896 do {
2897 err = nfs4_handle_exception(server,
2898 _nfs4_proc_statfs(server, fhandle, fsstat),
2899 &exception);
2900 } while (exception.retry);
2901 return err;
2904 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2905 struct nfs_fsinfo *fsinfo)
2907 struct nfs4_fsinfo_arg args = {
2908 .fh = fhandle,
2909 .bitmask = server->attr_bitmask,
2911 struct nfs4_fsinfo_res res = {
2912 .fsinfo = fsinfo,
2914 struct rpc_message msg = {
2915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2916 .rpc_argp = &args,
2917 .rpc_resp = &res,
2920 return nfs4_call_sync(server, &msg, &args, &res, 0);
2923 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2925 struct nfs4_exception exception = { };
2926 int err;
2928 do {
2929 err = nfs4_handle_exception(server,
2930 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2931 &exception);
2932 } while (exception.retry);
2933 return err;
2936 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2938 nfs_fattr_init(fsinfo->fattr);
2939 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2942 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2943 struct nfs_pathconf *pathconf)
2945 struct nfs4_pathconf_arg args = {
2946 .fh = fhandle,
2947 .bitmask = server->attr_bitmask,
2949 struct nfs4_pathconf_res res = {
2950 .pathconf = pathconf,
2952 struct rpc_message msg = {
2953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2954 .rpc_argp = &args,
2955 .rpc_resp = &res,
2958 /* None of the pathconf attributes are mandatory to implement */
2959 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2960 memset(pathconf, 0, sizeof(*pathconf));
2961 return 0;
2964 nfs_fattr_init(pathconf->fattr);
2965 return nfs4_call_sync(server, &msg, &args, &res, 0);
2968 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2969 struct nfs_pathconf *pathconf)
2971 struct nfs4_exception exception = { };
2972 int err;
2974 do {
2975 err = nfs4_handle_exception(server,
2976 _nfs4_proc_pathconf(server, fhandle, pathconf),
2977 &exception);
2978 } while (exception.retry);
2979 return err;
2982 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2984 struct nfs_server *server = NFS_SERVER(data->inode);
2986 dprintk("--> %s\n", __func__);
2988 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2989 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2991 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2992 nfs4_restart_rpc(task, server->nfs_client);
2993 return -EAGAIN;
2996 nfs_invalidate_atime(data->inode);
2997 if (task->tk_status > 0)
2998 renew_lease(server, data->timestamp);
2999 return 0;
3002 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3004 data->timestamp = jiffies;
3005 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3008 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3010 struct inode *inode = data->inode;
3012 /* slot is freed in nfs_writeback_done */
3013 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3014 task->tk_status);
3016 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3017 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3018 return -EAGAIN;
3020 if (task->tk_status >= 0) {
3021 renew_lease(NFS_SERVER(inode), data->timestamp);
3022 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3024 return 0;
3027 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3029 struct nfs_server *server = NFS_SERVER(data->inode);
3031 data->args.bitmask = server->cache_consistency_bitmask;
3032 data->res.server = server;
3033 data->timestamp = jiffies;
3035 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3038 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3040 struct inode *inode = data->inode;
3042 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3043 task->tk_status);
3044 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3045 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3046 return -EAGAIN;
3048 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3049 &data->res.seq_res);
3050 nfs_refresh_inode(inode, data->res.fattr);
3051 return 0;
3054 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3056 struct nfs_server *server = NFS_SERVER(data->inode);
3058 data->args.bitmask = server->cache_consistency_bitmask;
3059 data->res.server = server;
3060 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3064 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3065 * standalone procedure for queueing an asynchronous RENEW.
3067 static void nfs4_renew_done(struct rpc_task *task, void *data)
3069 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3070 unsigned long timestamp = (unsigned long)data;
3072 if (task->tk_status < 0) {
3073 /* Unless we're shutting down, schedule state recovery! */
3074 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3075 nfs4_schedule_state_recovery(clp);
3076 return;
3078 spin_lock(&clp->cl_lock);
3079 if (time_before(clp->cl_last_renewal,timestamp))
3080 clp->cl_last_renewal = timestamp;
3081 spin_unlock(&clp->cl_lock);
3084 static const struct rpc_call_ops nfs4_renew_ops = {
3085 .rpc_call_done = nfs4_renew_done,
3088 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3090 struct rpc_message msg = {
3091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3092 .rpc_argp = clp,
3093 .rpc_cred = cred,
3096 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3097 &nfs4_renew_ops, (void *)jiffies);
3100 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3102 struct rpc_message msg = {
3103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3104 .rpc_argp = clp,
3105 .rpc_cred = cred,
3107 unsigned long now = jiffies;
3108 int status;
3110 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3111 if (status < 0)
3112 return status;
3113 spin_lock(&clp->cl_lock);
3114 if (time_before(clp->cl_last_renewal,now))
3115 clp->cl_last_renewal = now;
3116 spin_unlock(&clp->cl_lock);
3117 return 0;
3120 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3122 return (server->caps & NFS_CAP_ACLS)
3123 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3124 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3127 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3128 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3129 * the stack.
3131 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3133 static void buf_to_pages(const void *buf, size_t buflen,
3134 struct page **pages, unsigned int *pgbase)
3136 const void *p = buf;
3138 *pgbase = offset_in_page(buf);
3139 p -= *pgbase;
3140 while (p < buf + buflen) {
3141 *(pages++) = virt_to_page(p);
3142 p += PAGE_CACHE_SIZE;
3146 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3147 struct page **pages, unsigned int *pgbase)
3149 struct page *newpage, **spages;
3150 int rc = 0;
3151 size_t len;
3152 spages = pages;
3154 do {
3155 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3156 newpage = alloc_page(GFP_KERNEL);
3158 if (newpage == NULL)
3159 goto unwind;
3160 memcpy(page_address(newpage), buf, len);
3161 buf += len;
3162 buflen -= len;
3163 *pages++ = newpage;
3164 rc++;
3165 } while (buflen != 0);
3167 return rc;
3169 unwind:
3170 for(; rc > 0; rc--)
3171 __free_page(spages[rc-1]);
3172 return -ENOMEM;
3175 struct nfs4_cached_acl {
3176 int cached;
3177 size_t len;
3178 char data[0];
3181 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3183 struct nfs_inode *nfsi = NFS_I(inode);
3185 spin_lock(&inode->i_lock);
3186 kfree(nfsi->nfs4_acl);
3187 nfsi->nfs4_acl = acl;
3188 spin_unlock(&inode->i_lock);
3191 static void nfs4_zap_acl_attr(struct inode *inode)
3193 nfs4_set_cached_acl(inode, NULL);
3196 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3198 struct nfs_inode *nfsi = NFS_I(inode);
3199 struct nfs4_cached_acl *acl;
3200 int ret = -ENOENT;
3202 spin_lock(&inode->i_lock);
3203 acl = nfsi->nfs4_acl;
3204 if (acl == NULL)
3205 goto out;
3206 if (buf == NULL) /* user is just asking for length */
3207 goto out_len;
3208 if (acl->cached == 0)
3209 goto out;
3210 ret = -ERANGE; /* see getxattr(2) man page */
3211 if (acl->len > buflen)
3212 goto out;
3213 memcpy(buf, acl->data, acl->len);
3214 out_len:
3215 ret = acl->len;
3216 out:
3217 spin_unlock(&inode->i_lock);
3218 return ret;
3221 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3223 struct nfs4_cached_acl *acl;
3225 if (buf && acl_len <= PAGE_SIZE) {
3226 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3227 if (acl == NULL)
3228 goto out;
3229 acl->cached = 1;
3230 memcpy(acl->data, buf, acl_len);
3231 } else {
3232 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3233 if (acl == NULL)
3234 goto out;
3235 acl->cached = 0;
3237 acl->len = acl_len;
3238 out:
3239 nfs4_set_cached_acl(inode, acl);
3242 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3244 struct page *pages[NFS4ACL_MAXPAGES];
3245 struct nfs_getaclargs args = {
3246 .fh = NFS_FH(inode),
3247 .acl_pages = pages,
3248 .acl_len = buflen,
3250 struct nfs_getaclres res = {
3251 .acl_len = buflen,
3253 void *resp_buf;
3254 struct rpc_message msg = {
3255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3256 .rpc_argp = &args,
3257 .rpc_resp = &res,
3259 struct page *localpage = NULL;
3260 int ret;
3262 if (buflen < PAGE_SIZE) {
3263 /* As long as we're doing a round trip to the server anyway,
3264 * let's be prepared for a page of acl data. */
3265 localpage = alloc_page(GFP_KERNEL);
3266 resp_buf = page_address(localpage);
3267 if (localpage == NULL)
3268 return -ENOMEM;
3269 args.acl_pages[0] = localpage;
3270 args.acl_pgbase = 0;
3271 args.acl_len = PAGE_SIZE;
3272 } else {
3273 resp_buf = buf;
3274 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3276 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3277 if (ret)
3278 goto out_free;
3279 if (res.acl_len > args.acl_len)
3280 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3281 else
3282 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3283 if (buf) {
3284 ret = -ERANGE;
3285 if (res.acl_len > buflen)
3286 goto out_free;
3287 if (localpage)
3288 memcpy(buf, resp_buf, res.acl_len);
3290 ret = res.acl_len;
3291 out_free:
3292 if (localpage)
3293 __free_page(localpage);
3294 return ret;
3297 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3299 struct nfs4_exception exception = { };
3300 ssize_t ret;
3301 do {
3302 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3303 if (ret >= 0)
3304 break;
3305 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3306 } while (exception.retry);
3307 return ret;
3310 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3312 struct nfs_server *server = NFS_SERVER(inode);
3313 int ret;
3315 if (!nfs4_server_supports_acls(server))
3316 return -EOPNOTSUPP;
3317 ret = nfs_revalidate_inode(server, inode);
3318 if (ret < 0)
3319 return ret;
3320 ret = nfs4_read_cached_acl(inode, buf, buflen);
3321 if (ret != -ENOENT)
3322 return ret;
3323 return nfs4_get_acl_uncached(inode, buf, buflen);
3326 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3328 struct nfs_server *server = NFS_SERVER(inode);
3329 struct page *pages[NFS4ACL_MAXPAGES];
3330 struct nfs_setaclargs arg = {
3331 .fh = NFS_FH(inode),
3332 .acl_pages = pages,
3333 .acl_len = buflen,
3335 struct nfs_setaclres res;
3336 struct rpc_message msg = {
3337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3338 .rpc_argp = &arg,
3339 .rpc_resp = &res,
3341 int ret, i;
3343 if (!nfs4_server_supports_acls(server))
3344 return -EOPNOTSUPP;
3345 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3346 if (i < 0)
3347 return i;
3348 nfs_inode_return_delegation(inode);
3349 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3352 * Free each page after tx, so the only ref left is
3353 * held by the network stack
3355 for (; i > 0; i--)
3356 put_page(pages[i-1]);
3358 nfs_access_zap_cache(inode);
3359 nfs_zap_acl_cache(inode);
3360 return ret;
3363 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3365 struct nfs4_exception exception = { };
3366 int err;
3367 do {
3368 err = nfs4_handle_exception(NFS_SERVER(inode),
3369 __nfs4_proc_set_acl(inode, buf, buflen),
3370 &exception);
3371 } while (exception.retry);
3372 return err;
3375 static int
3376 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3378 if (!clp || task->tk_status >= 0)
3379 return 0;
3380 switch(task->tk_status) {
3381 case -NFS4ERR_ADMIN_REVOKED:
3382 case -NFS4ERR_BAD_STATEID:
3383 case -NFS4ERR_OPENMODE:
3384 if (state == NULL)
3385 break;
3386 nfs4_state_mark_reclaim_nograce(clp, state);
3387 case -NFS4ERR_STALE_CLIENTID:
3388 case -NFS4ERR_STALE_STATEID:
3389 case -NFS4ERR_EXPIRED:
3390 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3391 nfs4_schedule_state_recovery(clp);
3392 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3393 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3394 task->tk_status = 0;
3395 return -EAGAIN;
3396 #if defined(CONFIG_NFS_V4_1)
3397 case -NFS4ERR_BADSESSION:
3398 case -NFS4ERR_BADSLOT:
3399 case -NFS4ERR_BAD_HIGH_SLOT:
3400 case -NFS4ERR_DEADSESSION:
3401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3402 case -NFS4ERR_SEQ_FALSE_RETRY:
3403 case -NFS4ERR_SEQ_MISORDERED:
3404 dprintk("%s ERROR %d, Reset session\n", __func__,
3405 task->tk_status);
3406 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3407 task->tk_status = 0;
3408 return -EAGAIN;
3409 #endif /* CONFIG_NFS_V4_1 */
3410 case -NFS4ERR_DELAY:
3411 if (server)
3412 nfs_inc_server_stats(server, NFSIOS_DELAY);
3413 case -NFS4ERR_GRACE:
3414 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3415 task->tk_status = 0;
3416 return -EAGAIN;
3417 case -NFS4ERR_OLD_STATEID:
3418 task->tk_status = 0;
3419 return -EAGAIN;
3421 task->tk_status = nfs4_map_errors(task->tk_status);
3422 return 0;
3425 static int
3426 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3428 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3431 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3433 nfs4_verifier sc_verifier;
3434 struct nfs4_setclientid setclientid = {
3435 .sc_verifier = &sc_verifier,
3436 .sc_prog = program,
3438 struct rpc_message msg = {
3439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3440 .rpc_argp = &setclientid,
3441 .rpc_resp = clp,
3442 .rpc_cred = cred,
3444 __be32 *p;
3445 int loop = 0;
3446 int status;
3448 p = (__be32*)sc_verifier.data;
3449 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3450 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3452 for(;;) {
3453 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3454 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3455 clp->cl_ipaddr,
3456 rpc_peeraddr2str(clp->cl_rpcclient,
3457 RPC_DISPLAY_ADDR),
3458 rpc_peeraddr2str(clp->cl_rpcclient,
3459 RPC_DISPLAY_PROTO),
3460 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3461 clp->cl_id_uniquifier);
3462 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3463 sizeof(setclientid.sc_netid),
3464 rpc_peeraddr2str(clp->cl_rpcclient,
3465 RPC_DISPLAY_NETID));
3466 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3467 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3468 clp->cl_ipaddr, port >> 8, port & 255);
3470 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3471 if (status != -NFS4ERR_CLID_INUSE)
3472 break;
3473 if (signalled())
3474 break;
3475 if (loop++ & 1)
3476 ssleep(clp->cl_lease_time + 1);
3477 else
3478 if (++clp->cl_id_uniquifier == 0)
3479 break;
3481 return status;
3484 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3486 struct nfs_fsinfo fsinfo;
3487 struct rpc_message msg = {
3488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3489 .rpc_argp = clp,
3490 .rpc_resp = &fsinfo,
3491 .rpc_cred = cred,
3493 unsigned long now;
3494 int status;
3496 now = jiffies;
3497 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3498 if (status == 0) {
3499 spin_lock(&clp->cl_lock);
3500 clp->cl_lease_time = fsinfo.lease_time * HZ;
3501 clp->cl_last_renewal = now;
3502 spin_unlock(&clp->cl_lock);
3504 return status;
3507 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3509 long timeout = 0;
3510 int err;
3511 do {
3512 err = _nfs4_proc_setclientid_confirm(clp, cred);
3513 switch (err) {
3514 case 0:
3515 return err;
3516 case -NFS4ERR_RESOURCE:
3517 /* The IBM lawyers misread another document! */
3518 case -NFS4ERR_DELAY:
3519 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3521 } while (err == 0);
3522 return err;
3525 struct nfs4_delegreturndata {
3526 struct nfs4_delegreturnargs args;
3527 struct nfs4_delegreturnres res;
3528 struct nfs_fh fh;
3529 nfs4_stateid stateid;
3530 unsigned long timestamp;
3531 struct nfs_fattr fattr;
3532 int rpc_status;
3535 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3537 struct nfs4_delegreturndata *data = calldata;
3539 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3540 task->tk_status);
3542 data->rpc_status = task->tk_status;
3543 if (data->rpc_status == 0)
3544 renew_lease(data->res.server, data->timestamp);
3547 static void nfs4_delegreturn_release(void *calldata)
3549 kfree(calldata);
3552 #if defined(CONFIG_NFS_V4_1)
3553 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3555 struct nfs4_delegreturndata *d_data;
3557 d_data = (struct nfs4_delegreturndata *)data;
3559 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3560 &d_data->args.seq_args,
3561 &d_data->res.seq_res, 1, task))
3562 return;
3563 rpc_call_start(task);
3565 #endif /* CONFIG_NFS_V4_1 */
3567 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3568 #if defined(CONFIG_NFS_V4_1)
3569 .rpc_call_prepare = nfs4_delegreturn_prepare,
3570 #endif /* CONFIG_NFS_V4_1 */
3571 .rpc_call_done = nfs4_delegreturn_done,
3572 .rpc_release = nfs4_delegreturn_release,
3575 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3577 struct nfs4_delegreturndata *data;
3578 struct nfs_server *server = NFS_SERVER(inode);
3579 struct rpc_task *task;
3580 struct rpc_message msg = {
3581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3582 .rpc_cred = cred,
3584 struct rpc_task_setup task_setup_data = {
3585 .rpc_client = server->client,
3586 .rpc_message = &msg,
3587 .callback_ops = &nfs4_delegreturn_ops,
3588 .flags = RPC_TASK_ASYNC,
3590 int status = 0;
3592 data = kzalloc(sizeof(*data), GFP_KERNEL);
3593 if (data == NULL)
3594 return -ENOMEM;
3595 data->args.fhandle = &data->fh;
3596 data->args.stateid = &data->stateid;
3597 data->args.bitmask = server->attr_bitmask;
3598 nfs_copy_fh(&data->fh, NFS_FH(inode));
3599 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3600 data->res.fattr = &data->fattr;
3601 data->res.server = server;
3602 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3603 nfs_fattr_init(data->res.fattr);
3604 data->timestamp = jiffies;
3605 data->rpc_status = 0;
3607 task_setup_data.callback_data = data;
3608 msg.rpc_argp = &data->args,
3609 msg.rpc_resp = &data->res,
3610 task = rpc_run_task(&task_setup_data);
3611 if (IS_ERR(task))
3612 return PTR_ERR(task);
3613 if (!issync)
3614 goto out;
3615 status = nfs4_wait_for_completion_rpc_task(task);
3616 if (status != 0)
3617 goto out;
3618 status = data->rpc_status;
3619 if (status != 0)
3620 goto out;
3621 nfs_refresh_inode(inode, &data->fattr);
3622 out:
3623 rpc_put_task(task);
3624 return status;
3627 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3629 struct nfs_server *server = NFS_SERVER(inode);
3630 struct nfs4_exception exception = { };
3631 int err;
3632 do {
3633 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3634 switch (err) {
3635 case -NFS4ERR_STALE_STATEID:
3636 case -NFS4ERR_EXPIRED:
3637 case 0:
3638 return 0;
3640 err = nfs4_handle_exception(server, err, &exception);
3641 } while (exception.retry);
3642 return err;
3645 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3646 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3649 * sleep, with exponential backoff, and retry the LOCK operation.
3651 static unsigned long
3652 nfs4_set_lock_task_retry(unsigned long timeout)
3654 schedule_timeout_killable(timeout);
3655 timeout <<= 1;
3656 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3657 return NFS4_LOCK_MAXTIMEOUT;
3658 return timeout;
3661 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3663 struct inode *inode = state->inode;
3664 struct nfs_server *server = NFS_SERVER(inode);
3665 struct nfs_client *clp = server->nfs_client;
3666 struct nfs_lockt_args arg = {
3667 .fh = NFS_FH(inode),
3668 .fl = request,
3670 struct nfs_lockt_res res = {
3671 .denied = request,
3673 struct rpc_message msg = {
3674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3675 .rpc_argp = &arg,
3676 .rpc_resp = &res,
3677 .rpc_cred = state->owner->so_cred,
3679 struct nfs4_lock_state *lsp;
3680 int status;
3682 arg.lock_owner.clientid = clp->cl_clientid;
3683 status = nfs4_set_lock_state(state, request);
3684 if (status != 0)
3685 goto out;
3686 lsp = request->fl_u.nfs4_fl.owner;
3687 arg.lock_owner.id = lsp->ls_id.id;
3688 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3689 switch (status) {
3690 case 0:
3691 request->fl_type = F_UNLCK;
3692 break;
3693 case -NFS4ERR_DENIED:
3694 status = 0;
3696 request->fl_ops->fl_release_private(request);
3697 out:
3698 return status;
3701 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3703 struct nfs4_exception exception = { };
3704 int err;
3706 do {
3707 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3708 _nfs4_proc_getlk(state, cmd, request),
3709 &exception);
3710 } while (exception.retry);
3711 return err;
3714 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3716 int res = 0;
3717 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3718 case FL_POSIX:
3719 res = posix_lock_file_wait(file, fl);
3720 break;
3721 case FL_FLOCK:
3722 res = flock_lock_file_wait(file, fl);
3723 break;
3724 default:
3725 BUG();
3727 return res;
3730 struct nfs4_unlockdata {
3731 struct nfs_locku_args arg;
3732 struct nfs_locku_res res;
3733 struct nfs4_lock_state *lsp;
3734 struct nfs_open_context *ctx;
3735 struct file_lock fl;
3736 const struct nfs_server *server;
3737 unsigned long timestamp;
3740 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3741 struct nfs_open_context *ctx,
3742 struct nfs4_lock_state *lsp,
3743 struct nfs_seqid *seqid)
3745 struct nfs4_unlockdata *p;
3746 struct inode *inode = lsp->ls_state->inode;
3748 p = kzalloc(sizeof(*p), GFP_KERNEL);
3749 if (p == NULL)
3750 return NULL;
3751 p->arg.fh = NFS_FH(inode);
3752 p->arg.fl = &p->fl;
3753 p->arg.seqid = seqid;
3754 p->res.seqid = seqid;
3755 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3756 p->arg.stateid = &lsp->ls_stateid;
3757 p->lsp = lsp;
3758 atomic_inc(&lsp->ls_count);
3759 /* Ensure we don't close file until we're done freeing locks! */
3760 p->ctx = get_nfs_open_context(ctx);
3761 memcpy(&p->fl, fl, sizeof(p->fl));
3762 p->server = NFS_SERVER(inode);
3763 return p;
3766 static void nfs4_locku_release_calldata(void *data)
3768 struct nfs4_unlockdata *calldata = data;
3769 nfs_free_seqid(calldata->arg.seqid);
3770 nfs4_put_lock_state(calldata->lsp);
3771 put_nfs_open_context(calldata->ctx);
3772 kfree(calldata);
3775 static void nfs4_locku_done(struct rpc_task *task, void *data)
3777 struct nfs4_unlockdata *calldata = data;
3779 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3780 task->tk_status);
3781 if (RPC_ASSASSINATED(task))
3782 return;
3783 switch (task->tk_status) {
3784 case 0:
3785 memcpy(calldata->lsp->ls_stateid.data,
3786 calldata->res.stateid.data,
3787 sizeof(calldata->lsp->ls_stateid.data));
3788 renew_lease(calldata->server, calldata->timestamp);
3789 break;
3790 case -NFS4ERR_BAD_STATEID:
3791 case -NFS4ERR_OLD_STATEID:
3792 case -NFS4ERR_STALE_STATEID:
3793 case -NFS4ERR_EXPIRED:
3794 break;
3795 default:
3796 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3797 nfs4_restart_rpc(task,
3798 calldata->server->nfs_client);
3800 nfs4_sequence_free_slot(calldata->server->nfs_client,
3801 &calldata->res.seq_res);
3804 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3806 struct nfs4_unlockdata *calldata = data;
3808 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3809 return;
3810 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3811 /* Note: exit _without_ running nfs4_locku_done */
3812 task->tk_action = NULL;
3813 return;
3815 calldata->timestamp = jiffies;
3816 if (nfs4_setup_sequence(calldata->server->nfs_client,
3817 &calldata->arg.seq_args,
3818 &calldata->res.seq_res, 1, task))
3819 return;
3820 rpc_call_start(task);
3823 static const struct rpc_call_ops nfs4_locku_ops = {
3824 .rpc_call_prepare = nfs4_locku_prepare,
3825 .rpc_call_done = nfs4_locku_done,
3826 .rpc_release = nfs4_locku_release_calldata,
3829 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3830 struct nfs_open_context *ctx,
3831 struct nfs4_lock_state *lsp,
3832 struct nfs_seqid *seqid)
3834 struct nfs4_unlockdata *data;
3835 struct rpc_message msg = {
3836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3837 .rpc_cred = ctx->cred,
3839 struct rpc_task_setup task_setup_data = {
3840 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3841 .rpc_message = &msg,
3842 .callback_ops = &nfs4_locku_ops,
3843 .workqueue = nfsiod_workqueue,
3844 .flags = RPC_TASK_ASYNC,
3847 /* Ensure this is an unlock - when canceling a lock, the
3848 * canceled lock is passed in, and it won't be an unlock.
3850 fl->fl_type = F_UNLCK;
3852 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3853 if (data == NULL) {
3854 nfs_free_seqid(seqid);
3855 return ERR_PTR(-ENOMEM);
3858 msg.rpc_argp = &data->arg,
3859 msg.rpc_resp = &data->res,
3860 task_setup_data.callback_data = data;
3861 return rpc_run_task(&task_setup_data);
3864 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3866 struct nfs_inode *nfsi = NFS_I(state->inode);
3867 struct nfs_seqid *seqid;
3868 struct nfs4_lock_state *lsp;
3869 struct rpc_task *task;
3870 int status = 0;
3871 unsigned char fl_flags = request->fl_flags;
3873 status = nfs4_set_lock_state(state, request);
3874 /* Unlock _before_ we do the RPC call */
3875 request->fl_flags |= FL_EXISTS;
3876 down_read(&nfsi->rwsem);
3877 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3878 up_read(&nfsi->rwsem);
3879 goto out;
3881 up_read(&nfsi->rwsem);
3882 if (status != 0)
3883 goto out;
3884 /* Is this a delegated lock? */
3885 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3886 goto out;
3887 lsp = request->fl_u.nfs4_fl.owner;
3888 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3889 status = -ENOMEM;
3890 if (seqid == NULL)
3891 goto out;
3892 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3893 status = PTR_ERR(task);
3894 if (IS_ERR(task))
3895 goto out;
3896 status = nfs4_wait_for_completion_rpc_task(task);
3897 rpc_put_task(task);
3898 out:
3899 request->fl_flags = fl_flags;
3900 return status;
3903 struct nfs4_lockdata {
3904 struct nfs_lock_args arg;
3905 struct nfs_lock_res res;
3906 struct nfs4_lock_state *lsp;
3907 struct nfs_open_context *ctx;
3908 struct file_lock fl;
3909 unsigned long timestamp;
3910 int rpc_status;
3911 int cancelled;
3912 struct nfs_server *server;
3915 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3916 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3918 struct nfs4_lockdata *p;
3919 struct inode *inode = lsp->ls_state->inode;
3920 struct nfs_server *server = NFS_SERVER(inode);
3922 p = kzalloc(sizeof(*p), GFP_KERNEL);
3923 if (p == NULL)
3924 return NULL;
3926 p->arg.fh = NFS_FH(inode);
3927 p->arg.fl = &p->fl;
3928 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3929 if (p->arg.open_seqid == NULL)
3930 goto out_free;
3931 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3932 if (p->arg.lock_seqid == NULL)
3933 goto out_free_seqid;
3934 p->arg.lock_stateid = &lsp->ls_stateid;
3935 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3936 p->arg.lock_owner.id = lsp->ls_id.id;
3937 p->res.lock_seqid = p->arg.lock_seqid;
3938 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3939 p->lsp = lsp;
3940 p->server = server;
3941 atomic_inc(&lsp->ls_count);
3942 p->ctx = get_nfs_open_context(ctx);
3943 memcpy(&p->fl, fl, sizeof(p->fl));
3944 return p;
3945 out_free_seqid:
3946 nfs_free_seqid(p->arg.open_seqid);
3947 out_free:
3948 kfree(p);
3949 return NULL;
3952 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3954 struct nfs4_lockdata *data = calldata;
3955 struct nfs4_state *state = data->lsp->ls_state;
3957 dprintk("%s: begin!\n", __func__);
3958 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3959 return;
3960 /* Do we need to do an open_to_lock_owner? */
3961 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3962 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3963 return;
3964 data->arg.open_stateid = &state->stateid;
3965 data->arg.new_lock_owner = 1;
3966 data->res.open_seqid = data->arg.open_seqid;
3967 } else
3968 data->arg.new_lock_owner = 0;
3969 data->timestamp = jiffies;
3970 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3971 &data->res.seq_res, 1, task))
3972 return;
3973 rpc_call_start(task);
3974 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3977 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3979 struct nfs4_lockdata *data = calldata;
3981 dprintk("%s: begin!\n", __func__);
3983 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3984 task->tk_status);
3986 data->rpc_status = task->tk_status;
3987 if (RPC_ASSASSINATED(task))
3988 goto out;
3989 if (data->arg.new_lock_owner != 0) {
3990 if (data->rpc_status == 0)
3991 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3992 else
3993 goto out;
3995 if (data->rpc_status == 0) {
3996 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3997 sizeof(data->lsp->ls_stateid.data));
3998 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3999 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4001 out:
4002 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4005 static void nfs4_lock_release(void *calldata)
4007 struct nfs4_lockdata *data = calldata;
4009 dprintk("%s: begin!\n", __func__);
4010 nfs_free_seqid(data->arg.open_seqid);
4011 if (data->cancelled != 0) {
4012 struct rpc_task *task;
4013 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4014 data->arg.lock_seqid);
4015 if (!IS_ERR(task))
4016 rpc_put_task(task);
4017 dprintk("%s: cancelling lock!\n", __func__);
4018 } else
4019 nfs_free_seqid(data->arg.lock_seqid);
4020 nfs4_put_lock_state(data->lsp);
4021 put_nfs_open_context(data->ctx);
4022 kfree(data);
4023 dprintk("%s: done!\n", __func__);
4026 static const struct rpc_call_ops nfs4_lock_ops = {
4027 .rpc_call_prepare = nfs4_lock_prepare,
4028 .rpc_call_done = nfs4_lock_done,
4029 .rpc_release = nfs4_lock_release,
4032 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4034 struct nfs_client *clp = server->nfs_client;
4035 struct nfs4_state *state = lsp->ls_state;
4037 switch (error) {
4038 case -NFS4ERR_ADMIN_REVOKED:
4039 case -NFS4ERR_BAD_STATEID:
4040 case -NFS4ERR_EXPIRED:
4041 if (new_lock_owner != 0 ||
4042 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4043 nfs4_state_mark_reclaim_nograce(clp, state);
4044 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4048 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4050 struct nfs4_lockdata *data;
4051 struct rpc_task *task;
4052 struct rpc_message msg = {
4053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4054 .rpc_cred = state->owner->so_cred,
4056 struct rpc_task_setup task_setup_data = {
4057 .rpc_client = NFS_CLIENT(state->inode),
4058 .rpc_message = &msg,
4059 .callback_ops = &nfs4_lock_ops,
4060 .workqueue = nfsiod_workqueue,
4061 .flags = RPC_TASK_ASYNC,
4063 int ret;
4065 dprintk("%s: begin!\n", __func__);
4066 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4067 fl->fl_u.nfs4_fl.owner);
4068 if (data == NULL)
4069 return -ENOMEM;
4070 if (IS_SETLKW(cmd))
4071 data->arg.block = 1;
4072 if (reclaim != 0)
4073 data->arg.reclaim = 1;
4074 msg.rpc_argp = &data->arg,
4075 msg.rpc_resp = &data->res,
4076 task_setup_data.callback_data = data;
4077 task = rpc_run_task(&task_setup_data);
4078 if (IS_ERR(task))
4079 return PTR_ERR(task);
4080 ret = nfs4_wait_for_completion_rpc_task(task);
4081 if (ret == 0) {
4082 ret = data->rpc_status;
4083 if (ret)
4084 nfs4_handle_setlk_error(data->server, data->lsp,
4085 data->arg.new_lock_owner, ret);
4086 } else
4087 data->cancelled = 1;
4088 rpc_put_task(task);
4089 dprintk("%s: done, ret = %d!\n", __func__, ret);
4090 return ret;
4093 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4095 struct nfs_server *server = NFS_SERVER(state->inode);
4096 struct nfs4_exception exception = { };
4097 int err;
4099 do {
4100 /* Cache the lock if possible... */
4101 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4102 return 0;
4103 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4104 if (err != -NFS4ERR_DELAY)
4105 break;
4106 nfs4_handle_exception(server, err, &exception);
4107 } while (exception.retry);
4108 return err;
4111 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4113 struct nfs_server *server = NFS_SERVER(state->inode);
4114 struct nfs4_exception exception = { };
4115 int err;
4117 err = nfs4_set_lock_state(state, request);
4118 if (err != 0)
4119 return err;
4120 do {
4121 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4122 return 0;
4123 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4124 switch (err) {
4125 default:
4126 goto out;
4127 case -NFS4ERR_GRACE:
4128 case -NFS4ERR_DELAY:
4129 nfs4_handle_exception(server, err, &exception);
4130 err = 0;
4132 } while (exception.retry);
4133 out:
4134 return err;
4137 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4139 struct nfs_inode *nfsi = NFS_I(state->inode);
4140 unsigned char fl_flags = request->fl_flags;
4141 int status = -ENOLCK;
4143 if ((fl_flags & FL_POSIX) &&
4144 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4145 goto out;
4146 /* Is this a delegated open? */
4147 status = nfs4_set_lock_state(state, request);
4148 if (status != 0)
4149 goto out;
4150 request->fl_flags |= FL_ACCESS;
4151 status = do_vfs_lock(request->fl_file, request);
4152 if (status < 0)
4153 goto out;
4154 down_read(&nfsi->rwsem);
4155 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4156 /* Yes: cache locks! */
4157 /* ...but avoid races with delegation recall... */
4158 request->fl_flags = fl_flags & ~FL_SLEEP;
4159 status = do_vfs_lock(request->fl_file, request);
4160 goto out_unlock;
4162 status = _nfs4_do_setlk(state, cmd, request, 0);
4163 if (status != 0)
4164 goto out_unlock;
4165 /* Note: we always want to sleep here! */
4166 request->fl_flags = fl_flags | FL_SLEEP;
4167 if (do_vfs_lock(request->fl_file, request) < 0)
4168 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4169 out_unlock:
4170 up_read(&nfsi->rwsem);
4171 out:
4172 request->fl_flags = fl_flags;
4173 return status;
4176 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4178 struct nfs4_exception exception = { };
4179 int err;
4181 do {
4182 err = _nfs4_proc_setlk(state, cmd, request);
4183 if (err == -NFS4ERR_DENIED)
4184 err = -EAGAIN;
4185 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4186 err, &exception);
4187 } while (exception.retry);
4188 return err;
4191 static int
4192 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4194 struct nfs_open_context *ctx;
4195 struct nfs4_state *state;
4196 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4197 int status;
4199 /* verify open state */
4200 ctx = nfs_file_open_context(filp);
4201 state = ctx->state;
4203 if (request->fl_start < 0 || request->fl_end < 0)
4204 return -EINVAL;
4206 if (IS_GETLK(cmd)) {
4207 if (state != NULL)
4208 return nfs4_proc_getlk(state, F_GETLK, request);
4209 return 0;
4212 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4213 return -EINVAL;
4215 if (request->fl_type == F_UNLCK) {
4216 if (state != NULL)
4217 return nfs4_proc_unlck(state, cmd, request);
4218 return 0;
4221 if (state == NULL)
4222 return -ENOLCK;
4223 do {
4224 status = nfs4_proc_setlk(state, cmd, request);
4225 if ((status != -EAGAIN) || IS_SETLK(cmd))
4226 break;
4227 timeout = nfs4_set_lock_task_retry(timeout);
4228 status = -ERESTARTSYS;
4229 if (signalled())
4230 break;
4231 } while(status < 0);
4232 return status;
4235 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4237 struct nfs_server *server = NFS_SERVER(state->inode);
4238 struct nfs4_exception exception = { };
4239 int err;
4241 err = nfs4_set_lock_state(state, fl);
4242 if (err != 0)
4243 goto out;
4244 do {
4245 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4246 switch (err) {
4247 default:
4248 printk(KERN_ERR "%s: unhandled error %d.\n",
4249 __func__, err);
4250 case 0:
4251 case -ESTALE:
4252 goto out;
4253 case -NFS4ERR_EXPIRED:
4254 case -NFS4ERR_STALE_CLIENTID:
4255 case -NFS4ERR_STALE_STATEID:
4256 nfs4_schedule_state_recovery(server->nfs_client);
4257 goto out;
4258 case -ERESTARTSYS:
4260 * The show must go on: exit, but mark the
4261 * stateid as needing recovery.
4263 case -NFS4ERR_ADMIN_REVOKED:
4264 case -NFS4ERR_BAD_STATEID:
4265 case -NFS4ERR_OPENMODE:
4266 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4267 err = 0;
4268 goto out;
4269 case -ENOMEM:
4270 case -NFS4ERR_DENIED:
4271 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4272 err = 0;
4273 goto out;
4274 case -NFS4ERR_DELAY:
4275 break;
4277 err = nfs4_handle_exception(server, err, &exception);
4278 } while (exception.retry);
4279 out:
4280 return err;
4283 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4285 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4286 size_t buflen, int flags)
4288 struct inode *inode = dentry->d_inode;
4290 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4291 return -EOPNOTSUPP;
4293 return nfs4_proc_set_acl(inode, buf, buflen);
4296 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4297 * and that's what we'll do for e.g. user attributes that haven't been set.
4298 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4299 * attributes in kernel-managed attribute namespaces. */
4300 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4301 size_t buflen)
4303 struct inode *inode = dentry->d_inode;
4305 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4306 return -EOPNOTSUPP;
4308 return nfs4_proc_get_acl(inode, buf, buflen);
4311 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4313 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4315 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4316 return 0;
4317 if (buf && buflen < len)
4318 return -ERANGE;
4319 if (buf)
4320 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4321 return len;
4324 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4326 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4327 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4328 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4329 return;
4331 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4332 NFS_ATTR_FATTR_NLINK;
4333 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4334 fattr->nlink = 2;
4337 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4338 struct nfs4_fs_locations *fs_locations, struct page *page)
4340 struct nfs_server *server = NFS_SERVER(dir);
4341 u32 bitmask[2] = {
4342 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4343 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4345 struct nfs4_fs_locations_arg args = {
4346 .dir_fh = NFS_FH(dir),
4347 .name = name,
4348 .page = page,
4349 .bitmask = bitmask,
4351 struct nfs4_fs_locations_res res = {
4352 .fs_locations = fs_locations,
4354 struct rpc_message msg = {
4355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4356 .rpc_argp = &args,
4357 .rpc_resp = &res,
4359 int status;
4361 dprintk("%s: start\n", __func__);
4362 nfs_fattr_init(&fs_locations->fattr);
4363 fs_locations->server = server;
4364 fs_locations->nlocations = 0;
4365 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4366 nfs_fixup_referral_attributes(&fs_locations->fattr);
4367 dprintk("%s: returned status = %d\n", __func__, status);
4368 return status;
4371 #ifdef CONFIG_NFS_V4_1
4373 * nfs4_proc_exchange_id()
4375 * Since the clientid has expired, all compounds using sessions
4376 * associated with the stale clientid will be returning
4377 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4378 * be in some phase of session reset.
4380 static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4382 nfs4_verifier verifier;
4383 struct nfs41_exchange_id_args args = {
4384 .client = clp,
4385 .flags = clp->cl_exchange_flags,
4387 struct nfs41_exchange_id_res res = {
4388 .client = clp,
4390 int status;
4391 struct rpc_message msg = {
4392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4393 .rpc_argp = &args,
4394 .rpc_resp = &res,
4395 .rpc_cred = cred,
4397 __be32 *p;
4399 dprintk("--> %s\n", __func__);
4400 BUG_ON(clp == NULL);
4402 p = (u32 *)verifier.data;
4403 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4404 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4405 args.verifier = &verifier;
4407 while (1) {
4408 args.id_len = scnprintf(args.id, sizeof(args.id),
4409 "%s/%s %u",
4410 clp->cl_ipaddr,
4411 rpc_peeraddr2str(clp->cl_rpcclient,
4412 RPC_DISPLAY_ADDR),
4413 clp->cl_id_uniquifier);
4415 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4417 if (status != NFS4ERR_CLID_INUSE)
4418 break;
4420 if (signalled())
4421 break;
4423 if (++clp->cl_id_uniquifier == 0)
4424 break;
4427 dprintk("<-- %s status= %d\n", __func__, status);
4428 return status;
4431 struct nfs4_get_lease_time_data {
4432 struct nfs4_get_lease_time_args *args;
4433 struct nfs4_get_lease_time_res *res;
4434 struct nfs_client *clp;
4437 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4438 void *calldata)
4440 int ret;
4441 struct nfs4_get_lease_time_data *data =
4442 (struct nfs4_get_lease_time_data *)calldata;
4444 dprintk("--> %s\n", __func__);
4445 /* just setup sequence, do not trigger session recovery
4446 since we're invoked within one */
4447 ret = nfs41_setup_sequence(data->clp->cl_session,
4448 &data->args->la_seq_args,
4449 &data->res->lr_seq_res, 0, task);
4451 BUG_ON(ret == -EAGAIN);
4452 rpc_call_start(task);
4453 dprintk("<-- %s\n", __func__);
4457 * Called from nfs4_state_manager thread for session setup, so don't recover
4458 * from sequence operation or clientid errors.
4460 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4462 struct nfs4_get_lease_time_data *data =
4463 (struct nfs4_get_lease_time_data *)calldata;
4465 dprintk("--> %s\n", __func__);
4466 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4467 switch (task->tk_status) {
4468 case -NFS4ERR_DELAY:
4469 case -NFS4ERR_GRACE:
4470 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4471 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4472 task->tk_status = 0;
4473 nfs4_restart_rpc(task, data->clp);
4474 return;
4476 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4477 dprintk("<-- %s\n", __func__);
4480 struct rpc_call_ops nfs4_get_lease_time_ops = {
4481 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4482 .rpc_call_done = nfs4_get_lease_time_done,
4485 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4487 struct rpc_task *task;
4488 struct nfs4_get_lease_time_args args;
4489 struct nfs4_get_lease_time_res res = {
4490 .lr_fsinfo = fsinfo,
4492 struct nfs4_get_lease_time_data data = {
4493 .args = &args,
4494 .res = &res,
4495 .clp = clp,
4497 struct rpc_message msg = {
4498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4499 .rpc_argp = &args,
4500 .rpc_resp = &res,
4502 struct rpc_task_setup task_setup = {
4503 .rpc_client = clp->cl_rpcclient,
4504 .rpc_message = &msg,
4505 .callback_ops = &nfs4_get_lease_time_ops,
4506 .callback_data = &data
4508 int status;
4510 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4511 dprintk("--> %s\n", __func__);
4512 task = rpc_run_task(&task_setup);
4514 if (IS_ERR(task))
4515 status = PTR_ERR(task);
4516 else {
4517 status = task->tk_status;
4518 rpc_put_task(task);
4520 dprintk("<-- %s return %d\n", __func__, status);
4522 return status;
4526 * Reset a slot table
4528 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4529 int old_max_slots, int ivalue)
4531 int i;
4532 int ret = 0;
4534 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4537 * Until we have dynamic slot table adjustment, insist
4538 * upon the same slot table size
4540 if (max_slots != old_max_slots) {
4541 dprintk("%s reset slot table does't match old\n",
4542 __func__);
4543 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4544 goto out;
4546 spin_lock(&tbl->slot_tbl_lock);
4547 for (i = 0; i < max_slots; ++i)
4548 tbl->slots[i].seq_nr = ivalue;
4549 tbl->highest_used_slotid = -1;
4550 spin_unlock(&tbl->slot_tbl_lock);
4551 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4552 tbl, tbl->slots, tbl->max_slots);
4553 out:
4554 dprintk("<-- %s: return %d\n", __func__, ret);
4555 return ret;
4559 * Reset the forechannel and backchannel slot tables
4561 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4563 int status;
4565 status = nfs4_reset_slot_table(&session->fc_slot_table,
4566 session->fc_attrs.max_reqs,
4567 session->fc_slot_table.max_slots,
4569 if (status)
4570 return status;
4572 status = nfs4_reset_slot_table(&session->bc_slot_table,
4573 session->bc_attrs.max_reqs,
4574 session->bc_slot_table.max_slots,
4576 return status;
4579 /* Destroy the slot table */
4580 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4582 if (session->fc_slot_table.slots != NULL) {
4583 kfree(session->fc_slot_table.slots);
4584 session->fc_slot_table.slots = NULL;
4586 if (session->bc_slot_table.slots != NULL) {
4587 kfree(session->bc_slot_table.slots);
4588 session->bc_slot_table.slots = NULL;
4590 return;
4594 * Initialize slot table
4596 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4597 int max_slots, int ivalue)
4599 int i;
4600 struct nfs4_slot *slot;
4601 int ret = -ENOMEM;
4603 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4605 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4607 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4608 if (!slot)
4609 goto out;
4610 for (i = 0; i < max_slots; ++i)
4611 slot[i].seq_nr = ivalue;
4612 ret = 0;
4614 spin_lock(&tbl->slot_tbl_lock);
4615 if (tbl->slots != NULL) {
4616 spin_unlock(&tbl->slot_tbl_lock);
4617 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4618 __func__, tbl, tbl->slots);
4619 WARN_ON(1);
4620 goto out_free;
4622 tbl->max_slots = max_slots;
4623 tbl->slots = slot;
4624 tbl->highest_used_slotid = -1; /* no slot is currently used */
4625 spin_unlock(&tbl->slot_tbl_lock);
4626 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4627 tbl, tbl->slots, tbl->max_slots);
4628 out:
4629 dprintk("<-- %s: return %d\n", __func__, ret);
4630 return ret;
4632 out_free:
4633 kfree(slot);
4634 goto out;
4638 * Initialize the forechannel and backchannel tables
4640 static int nfs4_init_slot_tables(struct nfs4_session *session)
4642 int status;
4644 status = nfs4_init_slot_table(&session->fc_slot_table,
4645 session->fc_attrs.max_reqs, 1);
4646 if (status)
4647 return status;
4649 status = nfs4_init_slot_table(&session->bc_slot_table,
4650 session->bc_attrs.max_reqs, 0);
4651 if (status)
4652 nfs4_destroy_slot_tables(session);
4654 return status;
4657 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4659 struct nfs4_session *session;
4660 struct nfs4_slot_table *tbl;
4662 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4663 if (!session)
4664 return NULL;
4666 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4668 * The create session reply races with the server back
4669 * channel probe. Mark the client NFS_CS_SESSION_INITING
4670 * so that the client back channel can find the
4671 * nfs_client struct
4673 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4675 tbl = &session->fc_slot_table;
4676 spin_lock_init(&tbl->slot_tbl_lock);
4677 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4679 tbl = &session->bc_slot_table;
4680 spin_lock_init(&tbl->slot_tbl_lock);
4681 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4683 session->clp = clp;
4684 return session;
4687 void nfs4_destroy_session(struct nfs4_session *session)
4689 nfs4_proc_destroy_session(session);
4690 dprintk("%s Destroy backchannel for xprt %p\n",
4691 __func__, session->clp->cl_rpcclient->cl_xprt);
4692 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4693 NFS41_BC_MIN_CALLBACKS);
4694 nfs4_destroy_slot_tables(session);
4695 kfree(session);
4699 * Initialize the values to be used by the client in CREATE_SESSION
4700 * If nfs4_init_session set the fore channel request and response sizes,
4701 * use them.
4703 * Set the back channel max_resp_sz_cached to zero to force the client to
4704 * always set csa_cachethis to FALSE because the current implementation
4705 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4707 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4709 struct nfs4_session *session = args->client->cl_session;
4710 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4711 mxresp_sz = session->fc_attrs.max_resp_sz;
4713 if (mxrqst_sz == 0)
4714 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4715 if (mxresp_sz == 0)
4716 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4717 /* Fore channel attributes */
4718 args->fc_attrs.headerpadsz = 0;
4719 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4720 args->fc_attrs.max_resp_sz = mxresp_sz;
4721 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4722 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4723 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4725 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4726 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4727 __func__,
4728 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4729 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4730 args->fc_attrs.max_reqs);
4732 /* Back channel attributes */
4733 args->bc_attrs.headerpadsz = 0;
4734 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4735 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4736 args->bc_attrs.max_resp_sz_cached = 0;
4737 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4738 args->bc_attrs.max_reqs = 1;
4740 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4741 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4742 __func__,
4743 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4744 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4745 args->bc_attrs.max_reqs);
4748 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4750 if (rcvd <= sent)
4751 return 0;
4752 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4753 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4754 return -EINVAL;
4757 #define _verify_fore_channel_attr(_name_) \
4758 _verify_channel_attr("fore", #_name_, \
4759 args->fc_attrs._name_, \
4760 session->fc_attrs._name_)
4762 #define _verify_back_channel_attr(_name_) \
4763 _verify_channel_attr("back", #_name_, \
4764 args->bc_attrs._name_, \
4765 session->bc_attrs._name_)
4768 * The server is not allowed to increase the fore channel header pad size,
4769 * maximum response size, or maximum number of operations.
4771 * The back channel attributes are only negotiatied down: We send what the
4772 * (back channel) server insists upon.
4774 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4775 struct nfs4_session *session)
4777 int ret = 0;
4779 ret |= _verify_fore_channel_attr(headerpadsz);
4780 ret |= _verify_fore_channel_attr(max_resp_sz);
4781 ret |= _verify_fore_channel_attr(max_ops);
4783 ret |= _verify_back_channel_attr(headerpadsz);
4784 ret |= _verify_back_channel_attr(max_rqst_sz);
4785 ret |= _verify_back_channel_attr(max_resp_sz);
4786 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4787 ret |= _verify_back_channel_attr(max_ops);
4788 ret |= _verify_back_channel_attr(max_reqs);
4790 return ret;
4793 static int _nfs4_proc_create_session(struct nfs_client *clp)
4795 struct nfs4_session *session = clp->cl_session;
4796 struct nfs41_create_session_args args = {
4797 .client = clp,
4798 .cb_program = NFS4_CALLBACK,
4800 struct nfs41_create_session_res res = {
4801 .client = clp,
4803 struct rpc_message msg = {
4804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4805 .rpc_argp = &args,
4806 .rpc_resp = &res,
4808 int status;
4810 nfs4_init_channel_attrs(&args);
4811 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4813 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4815 if (!status)
4816 /* Verify the session's negotiated channel_attrs values */
4817 status = nfs4_verify_channel_attrs(&args, session);
4818 if (!status) {
4819 /* Increment the clientid slot sequence id */
4820 clp->cl_seqid++;
4823 return status;
4827 * Issues a CREATE_SESSION operation to the server.
4828 * It is the responsibility of the caller to verify the session is
4829 * expired before calling this routine.
4831 int nfs4_proc_create_session(struct nfs_client *clp, int reset)
4833 int status;
4834 unsigned *ptr;
4835 struct nfs_fsinfo fsinfo;
4836 struct nfs4_session *session = clp->cl_session;
4838 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4840 status = _nfs4_proc_create_session(clp);
4841 if (status)
4842 goto out;
4844 /* Init or reset the fore channel */
4845 if (reset)
4846 status = nfs4_reset_slot_tables(session);
4847 else
4848 status = nfs4_init_slot_tables(session);
4849 dprintk("fore channel slot table initialization returned %d\n", status);
4850 if (status)
4851 goto out;
4853 ptr = (unsigned *)&session->sess_id.data[0];
4854 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4855 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4857 if (reset)
4858 /* Lease time is aleady set */
4859 goto out;
4861 /* Get the lease time */
4862 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4863 if (status == 0) {
4864 /* Update lease time and schedule renewal */
4865 spin_lock(&clp->cl_lock);
4866 clp->cl_lease_time = fsinfo.lease_time * HZ;
4867 clp->cl_last_renewal = jiffies;
4868 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4869 spin_unlock(&clp->cl_lock);
4871 nfs4_schedule_state_renewal(clp);
4873 out:
4874 dprintk("<-- %s\n", __func__);
4875 return status;
4879 * Issue the over-the-wire RPC DESTROY_SESSION.
4880 * The caller must serialize access to this routine.
4882 int nfs4_proc_destroy_session(struct nfs4_session *session)
4884 int status = 0;
4885 struct rpc_message msg;
4887 dprintk("--> nfs4_proc_destroy_session\n");
4889 /* session is still being setup */
4890 if (session->clp->cl_cons_state != NFS_CS_READY)
4891 return status;
4893 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4894 msg.rpc_argp = session;
4895 msg.rpc_resp = NULL;
4896 msg.rpc_cred = NULL;
4897 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4899 if (status)
4900 printk(KERN_WARNING
4901 "Got error %d from the server on DESTROY_SESSION. "
4902 "Session has been destroyed regardless...\n", status);
4904 dprintk("<-- nfs4_proc_destroy_session\n");
4905 return status;
4908 int nfs4_init_session(struct nfs_server *server)
4910 struct nfs_client *clp = server->nfs_client;
4911 int ret;
4913 if (!nfs4_has_session(clp))
4914 return 0;
4916 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4917 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4918 ret = nfs4_recover_expired_lease(server);
4919 if (!ret)
4920 ret = nfs4_check_client_ready(clp);
4921 return ret;
4925 * Renew the cl_session lease.
4927 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4929 struct nfs4_sequence_args args;
4930 struct nfs4_sequence_res res;
4932 struct rpc_message msg = {
4933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4934 .rpc_argp = &args,
4935 .rpc_resp = &res,
4936 .rpc_cred = cred,
4939 args.sa_cache_this = 0;
4941 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4942 &res, 0);
4945 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4947 struct nfs_client *clp = (struct nfs_client *)data;
4949 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4951 if (task->tk_status < 0) {
4952 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4954 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4955 == -EAGAIN) {
4956 nfs4_restart_rpc(task, clp);
4957 return;
4960 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4961 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4963 kfree(task->tk_msg.rpc_argp);
4964 kfree(task->tk_msg.rpc_resp);
4966 dprintk("<-- %s\n", __func__);
4969 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4971 struct nfs_client *clp;
4972 struct nfs4_sequence_args *args;
4973 struct nfs4_sequence_res *res;
4975 clp = (struct nfs_client *)data;
4976 args = task->tk_msg.rpc_argp;
4977 res = task->tk_msg.rpc_resp;
4979 if (nfs4_setup_sequence(clp, args, res, 0, task))
4980 return;
4981 rpc_call_start(task);
4984 static const struct rpc_call_ops nfs41_sequence_ops = {
4985 .rpc_call_done = nfs41_sequence_call_done,
4986 .rpc_call_prepare = nfs41_sequence_prepare,
4989 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4990 struct rpc_cred *cred)
4992 struct nfs4_sequence_args *args;
4993 struct nfs4_sequence_res *res;
4994 struct rpc_message msg = {
4995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4996 .rpc_cred = cred,
4999 args = kzalloc(sizeof(*args), GFP_KERNEL);
5000 if (!args)
5001 return -ENOMEM;
5002 res = kzalloc(sizeof(*res), GFP_KERNEL);
5003 if (!res) {
5004 kfree(args);
5005 return -ENOMEM;
5007 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
5008 msg.rpc_argp = args;
5009 msg.rpc_resp = res;
5011 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
5012 &nfs41_sequence_ops, (void *)clp);
5015 #endif /* CONFIG_NFS_V4_1 */
5017 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5018 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5019 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5020 .recover_open = nfs4_open_reclaim,
5021 .recover_lock = nfs4_lock_reclaim,
5022 .establish_clid = nfs4_init_clientid,
5023 .get_clid_cred = nfs4_get_setclientid_cred,
5026 #if defined(CONFIG_NFS_V4_1)
5027 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5028 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5029 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5030 .recover_open = nfs4_open_reclaim,
5031 .recover_lock = nfs4_lock_reclaim,
5032 .establish_clid = nfs4_proc_exchange_id,
5033 .get_clid_cred = nfs4_get_exchange_id_cred,
5035 #endif /* CONFIG_NFS_V4_1 */
5037 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5038 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5039 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5040 .recover_open = nfs4_open_expired,
5041 .recover_lock = nfs4_lock_expired,
5042 .establish_clid = nfs4_init_clientid,
5043 .get_clid_cred = nfs4_get_setclientid_cred,
5046 #if defined(CONFIG_NFS_V4_1)
5047 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5048 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5049 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5050 .recover_open = nfs4_open_expired,
5051 .recover_lock = nfs4_lock_expired,
5052 .establish_clid = nfs4_proc_exchange_id,
5053 .get_clid_cred = nfs4_get_exchange_id_cred,
5055 #endif /* CONFIG_NFS_V4_1 */
5057 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5058 .sched_state_renewal = nfs4_proc_async_renew,
5059 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5060 .renew_lease = nfs4_proc_renew,
5063 #if defined(CONFIG_NFS_V4_1)
5064 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5065 .sched_state_renewal = nfs41_proc_async_sequence,
5066 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5067 .renew_lease = nfs4_proc_sequence,
5069 #endif
5072 * Per minor version reboot and network partition recovery ops
5075 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5076 &nfs40_reboot_recovery_ops,
5077 #if defined(CONFIG_NFS_V4_1)
5078 &nfs41_reboot_recovery_ops,
5079 #endif
5082 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5083 &nfs40_nograce_recovery_ops,
5084 #if defined(CONFIG_NFS_V4_1)
5085 &nfs41_nograce_recovery_ops,
5086 #endif
5089 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5090 &nfs40_state_renewal_ops,
5091 #if defined(CONFIG_NFS_V4_1)
5092 &nfs41_state_renewal_ops,
5093 #endif
5096 static const struct inode_operations nfs4_file_inode_operations = {
5097 .permission = nfs_permission,
5098 .getattr = nfs_getattr,
5099 .setattr = nfs_setattr,
5100 .getxattr = nfs4_getxattr,
5101 .setxattr = nfs4_setxattr,
5102 .listxattr = nfs4_listxattr,
5105 const struct nfs_rpc_ops nfs_v4_clientops = {
5106 .version = 4, /* protocol version */
5107 .dentry_ops = &nfs4_dentry_operations,
5108 .dir_inode_ops = &nfs4_dir_inode_operations,
5109 .file_inode_ops = &nfs4_file_inode_operations,
5110 .getroot = nfs4_proc_get_root,
5111 .getattr = nfs4_proc_getattr,
5112 .setattr = nfs4_proc_setattr,
5113 .lookupfh = nfs4_proc_lookupfh,
5114 .lookup = nfs4_proc_lookup,
5115 .access = nfs4_proc_access,
5116 .readlink = nfs4_proc_readlink,
5117 .create = nfs4_proc_create,
5118 .remove = nfs4_proc_remove,
5119 .unlink_setup = nfs4_proc_unlink_setup,
5120 .unlink_done = nfs4_proc_unlink_done,
5121 .rename = nfs4_proc_rename,
5122 .link = nfs4_proc_link,
5123 .symlink = nfs4_proc_symlink,
5124 .mkdir = nfs4_proc_mkdir,
5125 .rmdir = nfs4_proc_remove,
5126 .readdir = nfs4_proc_readdir,
5127 .mknod = nfs4_proc_mknod,
5128 .statfs = nfs4_proc_statfs,
5129 .fsinfo = nfs4_proc_fsinfo,
5130 .pathconf = nfs4_proc_pathconf,
5131 .set_capabilities = nfs4_server_capabilities,
5132 .decode_dirent = nfs4_decode_dirent,
5133 .read_setup = nfs4_proc_read_setup,
5134 .read_done = nfs4_read_done,
5135 .write_setup = nfs4_proc_write_setup,
5136 .write_done = nfs4_write_done,
5137 .commit_setup = nfs4_proc_commit_setup,
5138 .commit_done = nfs4_commit_done,
5139 .lock = nfs4_proc_lock,
5140 .clear_acl_cache = nfs4_zap_acl_attr,
5141 .close_context = nfs4_close_context,
5145 * Local variables:
5146 * c-basic-offset: 8
5147 * End: