]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nfs/nfs4proc.c
nfs41: Backchannel: Refactor nfs4_reset_slot_table()
[mirror_ubuntu-artful-kernel.git] / fs / nfs / nfs4proc.c
CommitLineData
1da177e4
LT
1/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
02a913a7 50#include <linux/mount.h>
99fe60d0 51#include <linux/module.h>
5a0ffe54 52#include <linux/sunrpc/bc_xprt.h>
1da177e4 53
4ce79717 54#include "nfs4_fs.h"
1da177e4 55#include "delegation.h"
101070ca 56#include "internal.h"
006ea73e 57#include "iostat.h"
fc931582 58#include "callback.h"
1da177e4
LT
59
60#define NFSDBG_FACILITY NFSDBG_PROC
61
2066fe89 62#define NFS4_POLL_RETRY_MIN (HZ/10)
1da177e4
LT
63#define NFS4_POLL_RETRY_MAX (15*HZ)
64
cdd4e68b 65struct nfs4_opendata;
864472e9 66static int _nfs4_proc_open(struct nfs4_opendata *data);
1da177e4 67static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
9e33bed5 68static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
9936781d
TM
69static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
1da177e4 71
1da177e4 72/* Prevent leaks of NFSv4 errors into userland */
46f72f57 73static int nfs4_map_errors(int err)
1da177e4
LT
74{
75 if (err < -1000) {
76 dprintk("%s could not handle NFSv4 error %d\n",
3110ff80 77 __func__, -err);
1da177e4
LT
78 return -EIO;
79 }
80 return err;
81}
82
83/*
84 * This is our standard bitmap for GETATTR requests.
85 */
86const u32 nfs4_fattr_bitmap[2] = {
87 FATTR4_WORD0_TYPE
88 | FATTR4_WORD0_CHANGE
89 | FATTR4_WORD0_SIZE
90 | FATTR4_WORD0_FSID
91 | FATTR4_WORD0_FILEID,
92 FATTR4_WORD1_MODE
93 | FATTR4_WORD1_NUMLINKS
94 | FATTR4_WORD1_OWNER
95 | FATTR4_WORD1_OWNER_GROUP
96 | FATTR4_WORD1_RAWDEV
97 | FATTR4_WORD1_SPACE_USED
98 | FATTR4_WORD1_TIME_ACCESS
99 | FATTR4_WORD1_TIME_METADATA
100 | FATTR4_WORD1_TIME_MODIFY
101};
102
103const u32 nfs4_statfs_bitmap[2] = {
104 FATTR4_WORD0_FILES_AVAIL
105 | FATTR4_WORD0_FILES_FREE
106 | FATTR4_WORD0_FILES_TOTAL,
107 FATTR4_WORD1_SPACE_AVAIL
108 | FATTR4_WORD1_SPACE_FREE
109 | FATTR4_WORD1_SPACE_TOTAL
110};
111
4ce79717 112const u32 nfs4_pathconf_bitmap[2] = {
1da177e4
LT
113 FATTR4_WORD0_MAXLINK
114 | FATTR4_WORD0_MAXNAME,
115 0
116};
117
118const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
119 | FATTR4_WORD0_MAXREAD
120 | FATTR4_WORD0_MAXWRITE
121 | FATTR4_WORD0_LEASE_TIME,
122 0
123};
124
830b8e33
MN
125const u32 nfs4_fs_locations_bitmap[2] = {
126 FATTR4_WORD0_TYPE
127 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_SIZE
129 | FATTR4_WORD0_FSID
130 | FATTR4_WORD0_FILEID
131 | FATTR4_WORD0_FS_LOCATIONS,
132 FATTR4_WORD1_MODE
133 | FATTR4_WORD1_NUMLINKS
134 | FATTR4_WORD1_OWNER
135 | FATTR4_WORD1_OWNER_GROUP
136 | FATTR4_WORD1_RAWDEV
137 | FATTR4_WORD1_SPACE_USED
138 | FATTR4_WORD1_TIME_ACCESS
139 | FATTR4_WORD1_TIME_METADATA
140 | FATTR4_WORD1_TIME_MODIFY
141 | FATTR4_WORD1_MOUNTED_ON_FILEID
142};
143
bc4785cd 144static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
1da177e4
LT
145 struct nfs4_readdir_arg *readdir)
146{
0dbb4c67 147 __be32 *start, *p;
1da177e4
LT
148
149 BUG_ON(readdir->count < 80);
150 if (cookie > 2) {
b7ef1956 151 readdir->cookie = cookie;
1da177e4
LT
152 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
153 return;
154 }
155
156 readdir->cookie = 0;
157 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
158 if (cookie == 2)
159 return;
160
161 /*
162 * NFSv4 servers do not return entries for '.' and '..'
163 * Therefore, we fake these entries here. We let '.'
164 * have cookie 0 and '..' have cookie 1. Note that
165 * when talking to the server, we always send cookie 0
166 * instead of 1 or 2.
167 */
0dbb4c67 168 start = p = kmap_atomic(*readdir->pages, KM_USER0);
1da177e4
LT
169
170 if (cookie == 0) {
171 *p++ = xdr_one; /* next */
172 *p++ = xdr_zero; /* cookie, first word */
173 *p++ = xdr_one; /* cookie, second word */
174 *p++ = xdr_one; /* entry len */
175 memcpy(p, ".\0\0\0", 4); /* entry */
176 p++;
177 *p++ = xdr_one; /* bitmap length */
178 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
179 *p++ = htonl(8); /* attribute buffer length */
4e769b93 180 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
1da177e4
LT
181 }
182
183 *p++ = xdr_one; /* next */
184 *p++ = xdr_zero; /* cookie, first word */
185 *p++ = xdr_two; /* cookie, second word */
186 *p++ = xdr_two; /* entry len */
187 memcpy(p, "..\0\0", 4); /* entry */
188 p++;
189 *p++ = xdr_one; /* bitmap length */
190 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
191 *p++ = htonl(8); /* attribute buffer length */
4e769b93 192 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
1da177e4
LT
193
194 readdir->pgbase = (char *)p - (char *)start;
195 readdir->count -= readdir->pgbase;
196 kunmap_atomic(start, KM_USER0);
197}
198
65de872e
TM
199static int nfs4_wait_clnt_recover(struct nfs_client *clp)
200{
201 int res;
202
203 might_sleep();
204
e005e804 205 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
72cb77f4 206 nfs_wait_bit_killable, TASK_KILLABLE);
65de872e
TM
207 return res;
208}
209
210static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
211{
212 int res = 0;
213
214 might_sleep();
215
216 if (*timeout <= 0)
217 *timeout = NFS4_POLL_RETRY_MIN;
218 if (*timeout > NFS4_POLL_RETRY_MAX)
219 *timeout = NFS4_POLL_RETRY_MAX;
220 schedule_timeout_killable(*timeout);
221 if (fatal_signal_pending(current))
222 res = -ERESTARTSYS;
223 *timeout <<= 1;
224 return res;
225}
226
227/* This is the error handling routine for processes that are allowed
228 * to sleep.
229 */
230static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
231{
232 struct nfs_client *clp = server->nfs_client;
9e33bed5 233 struct nfs4_state *state = exception->state;
65de872e
TM
234 int ret = errorcode;
235
236 exception->retry = 0;
237 switch(errorcode) {
238 case 0:
239 return 0;
9e33bed5
TM
240 case -NFS4ERR_ADMIN_REVOKED:
241 case -NFS4ERR_BAD_STATEID:
242 case -NFS4ERR_OPENMODE:
243 if (state == NULL)
244 break;
245 nfs4_state_mark_reclaim_nograce(clp, state);
65de872e
TM
246 case -NFS4ERR_STALE_CLIENTID:
247 case -NFS4ERR_STALE_STATEID:
248 case -NFS4ERR_EXPIRED:
249 nfs4_schedule_state_recovery(clp);
250 ret = nfs4_wait_clnt_recover(clp);
251 if (ret == 0)
252 exception->retry = 1;
4745e315 253#if !defined(CONFIG_NFS_V4_1)
65de872e 254 break;
4745e315
AA
255#else /* !defined(CONFIG_NFS_V4_1) */
256 if (!nfs4_has_session(server->nfs_client))
257 break;
258 /* FALLTHROUGH */
259 case -NFS4ERR_BADSESSION:
260 case -NFS4ERR_BADSLOT:
261 case -NFS4ERR_BAD_HIGH_SLOT:
262 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
263 case -NFS4ERR_DEADSESSION:
264 case -NFS4ERR_SEQ_FALSE_RETRY:
265 case -NFS4ERR_SEQ_MISORDERED:
266 dprintk("%s ERROR: %d Reset session\n", __func__,
267 errorcode);
268 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
269 exception->retry = 1;
270 /* FALLTHROUGH */
271#endif /* !defined(CONFIG_NFS_V4_1) */
65de872e
TM
272 case -NFS4ERR_FILE_OPEN:
273 case -NFS4ERR_GRACE:
274 case -NFS4ERR_DELAY:
275 ret = nfs4_delay(server->client, &exception->timeout);
276 if (ret != 0)
277 break;
278 case -NFS4ERR_OLD_STATEID:
279 exception->retry = 1;
280 }
281 /* We failed to handle the error */
282 return nfs4_map_errors(ret);
283}
284
285
26e976a8 286static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
1da177e4 287{
7539bbab 288 struct nfs_client *clp = server->nfs_client;
1da177e4
LT
289 spin_lock(&clp->cl_lock);
290 if (time_before(clp->cl_last_renewal,timestamp))
291 clp->cl_last_renewal = timestamp;
292 spin_unlock(&clp->cl_lock);
293}
294
cccef3b9
AA
295#if defined(CONFIG_NFS_V4_1)
296
e2c4ab3c
AA
297/*
298 * nfs4_free_slot - free a slot and efficiently update slot table.
299 *
300 * freeing a slot is trivially done by clearing its respective bit
301 * in the bitmap.
302 * If the freed slotid equals highest_used_slotid we want to update it
303 * so that the server would be able to size down the slot table if needed,
304 * otherwise we know that the highest_used_slotid is still in use.
305 * When updating highest_used_slotid there may be "holes" in the bitmap
306 * so we need to scan down from highest_used_slotid to 0 looking for the now
307 * highest slotid in use.
308 * If none found, highest_used_slotid is set to -1.
309 */
310static void
311nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
312{
313 int slotid = free_slotid;
314
315 spin_lock(&tbl->slot_tbl_lock);
316 /* clear used bit in bitmap */
317 __clear_bit(slotid, tbl->used_slots);
318
319 /* update highest_used_slotid when it is freed */
320 if (slotid == tbl->highest_used_slotid) {
321 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
322 if (slotid >= 0 && slotid < tbl->max_slots)
323 tbl->highest_used_slotid = slotid;
324 else
325 tbl->highest_used_slotid = -1;
326 }
327 rpc_wake_up_next(&tbl->slot_tbl_waitq);
328 spin_unlock(&tbl->slot_tbl_lock);
329 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
330 free_slotid, tbl->highest_used_slotid);
331}
332
13615871
AA
333void nfs41_sequence_free_slot(const struct nfs_client *clp,
334 struct nfs4_sequence_res *res)
335{
336 struct nfs4_slot_table *tbl;
337
338 if (!nfs4_has_session(clp)) {
339 dprintk("%s: No session\n", __func__);
340 return;
341 }
342 tbl = &clp->cl_session->fc_slot_table;
343 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
344 dprintk("%s: No slot\n", __func__);
345 /* just wake up the next guy waiting since
346 * we may have not consumed a slot after all */
347 rpc_wake_up_next(&tbl->slot_tbl_waitq);
348 return;
349 }
350 nfs4_free_slot(tbl, res->sr_slotid);
351 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
352}
353
b0df806c
AA
354static void nfs41_sequence_done(struct nfs_client *clp,
355 struct nfs4_sequence_res *res,
356 int rpc_status)
357{
358 unsigned long timestamp;
359 struct nfs4_slot_table *tbl;
360 struct nfs4_slot *slot;
361
362 /*
363 * sr_status remains 1 if an RPC level error occurred. The server
364 * may or may not have processed the sequence operation..
365 * Proceed as if the server received and processed the sequence
366 * operation.
367 */
368 if (res->sr_status == 1)
369 res->sr_status = NFS_OK;
370
371 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
372 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
373 goto out;
374
375 tbl = &clp->cl_session->fc_slot_table;
376 slot = tbl->slots + res->sr_slotid;
377
378 if (res->sr_status == 0) {
379 /* Update the slot's sequence and clientid lease timer */
380 ++slot->seq_nr;
381 timestamp = res->sr_renewal_time;
382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal, timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
386 return;
387 }
388out:
389 /* The session may be reset by one of the error handlers. */
390 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
391 nfs41_sequence_free_slot(clp, res);
392}
393
510b8175
BH
394/*
395 * nfs4_find_slot - efficiently look for a free slot
396 *
397 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
398 * If found, we mark the slot as used, update the highest_used_slotid,
399 * and respectively set up the sequence operation args.
400 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
fbcd4abc
AA
401 *
402 * Note: must be called with under the slot_tbl_lock.
510b8175
BH
403 */
404static u8
405nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
406{
407 int slotid;
408 u8 ret_id = NFS4_MAX_SLOT_TABLE;
409 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
410
510b8175
BH
411 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
412 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
413 tbl->max_slots);
414 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
415 if (slotid >= tbl->max_slots)
416 goto out;
417 __set_bit(slotid, tbl->used_slots);
418 if (slotid > tbl->highest_used_slotid)
419 tbl->highest_used_slotid = slotid;
420 ret_id = slotid;
421out:
422 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
423 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
510b8175
BH
424 return ret_id;
425}
426
b069d94a
AA
427static int nfs4_recover_session(struct nfs4_session *session)
428{
429 struct nfs_client *clp = session->clp;
430 int ret;
431
432 for (;;) {
433 ret = nfs4_wait_clnt_recover(clp);
434 if (ret != 0)
435 return ret;
436 if (!test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
437 break;
438 nfs4_schedule_state_manager(clp);
439 }
440 return 0;
441}
442
ce5039c1
AA
443static int nfs41_setup_sequence(struct nfs4_session *session,
444 struct nfs4_sequence_args *args,
445 struct nfs4_sequence_res *res,
446 int cache_reply,
447 struct rpc_task *task)
448{
fbcd4abc
AA
449 struct nfs4_slot *slot;
450 struct nfs4_slot_table *tbl;
b069d94a 451 int status = 0;
fbcd4abc
AA
452 u8 slotid;
453
454 dprintk("--> %s\n", __func__);
ce5039c1
AA
455 /* slot already allocated? */
456 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
457 return 0;
458
459 memset(res, 0, sizeof(*res));
460 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
fbcd4abc
AA
461 tbl = &session->fc_slot_table;
462
463 spin_lock(&tbl->slot_tbl_lock);
b069d94a
AA
464 if (test_bit(NFS4CLNT_SESSION_SETUP, &session->clp->cl_state)) {
465 if (tbl->highest_used_slotid != -1) {
466 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
467 spin_unlock(&tbl->slot_tbl_lock);
468 dprintk("<-- %s: Session reset: draining\n", __func__);
469 return -EAGAIN;
470 }
471
472 /* The slot table is empty; start the reset thread */
473 dprintk("%s Session Reset\n", __func__);
474 spin_unlock(&tbl->slot_tbl_lock);
475 status = nfs4_recover_session(session);
476 if (status)
477 return status;
478 spin_lock(&tbl->slot_tbl_lock);
479 }
480
fbcd4abc
AA
481 slotid = nfs4_find_slot(tbl, task);
482 if (slotid == NFS4_MAX_SLOT_TABLE) {
483 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
484 spin_unlock(&tbl->slot_tbl_lock);
485 dprintk("<-- %s: no free slots\n", __func__);
486 return -EAGAIN;
487 }
488 spin_unlock(&tbl->slot_tbl_lock);
489
490 slot = tbl->slots + slotid;
99fe60d0 491 args->sa_session = session;
fbcd4abc
AA
492 args->sa_slotid = slotid;
493 args->sa_cache_this = cache_reply;
494
495 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
496
99fe60d0 497 res->sr_session = session;
fbcd4abc
AA
498 res->sr_slotid = slotid;
499 res->sr_renewal_time = jiffies;
500 /*
501 * sr_status is only set in decode_sequence, and so will remain
502 * set to 1 if an rpc level failure occurs.
503 */
504 res->sr_status = 1;
ce5039c1
AA
505 return 0;
506}
507
508int nfs4_setup_sequence(struct nfs_client *clp,
509 struct nfs4_sequence_args *args,
510 struct nfs4_sequence_res *res,
511 int cache_reply,
512 struct rpc_task *task)
513{
514 int ret = 0;
515
516 dprintk("--> %s clp %p session %p sr_slotid %d\n",
517 __func__, clp, clp->cl_session, res->sr_slotid);
518
519 if (!nfs4_has_session(clp))
520 goto out;
521 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
522 task);
523 if (ret != -EAGAIN) {
524 /* terminate rpc task */
525 task->tk_status = ret;
526 task->tk_action = NULL;
527 }
528out:
529 dprintk("<-- %s status=%d\n", __func__, ret);
530 return ret;
531}
532
533struct nfs41_call_sync_data {
534 struct nfs_client *clp;
535 struct nfs4_sequence_args *seq_args;
536 struct nfs4_sequence_res *seq_res;
537 int cache_reply;
538};
539
540static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
541{
542 struct nfs41_call_sync_data *data = calldata;
543
544 dprintk("--> %s data->clp->cl_session %p\n", __func__,
545 data->clp->cl_session);
546 if (nfs4_setup_sequence(data->clp, data->seq_args,
547 data->seq_res, data->cache_reply, task))
548 return;
549 rpc_call_start(task);
550}
551
69ab40c4
AA
552static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
553{
554 struct nfs41_call_sync_data *data = calldata;
555
556 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
557 nfs41_sequence_free_slot(data->clp, data->seq_res);
558}
559
ce5039c1
AA
560struct rpc_call_ops nfs41_call_sync_ops = {
561 .rpc_call_prepare = nfs41_call_sync_prepare,
69ab40c4 562 .rpc_call_done = nfs41_call_sync_done,
ce5039c1
AA
563};
564
565static int nfs4_call_sync_sequence(struct nfs_client *clp,
566 struct rpc_clnt *clnt,
567 struct rpc_message *msg,
568 struct nfs4_sequence_args *args,
569 struct nfs4_sequence_res *res,
570 int cache_reply)
571{
572 int ret;
573 struct rpc_task *task;
574 struct nfs41_call_sync_data data = {
575 .clp = clp,
576 .seq_args = args,
577 .seq_res = res,
578 .cache_reply = cache_reply,
579 };
580 struct rpc_task_setup task_setup = {
581 .rpc_client = clnt,
582 .rpc_message = msg,
583 .callback_ops = &nfs41_call_sync_ops,
584 .callback_data = &data
585 };
586
587 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
588 task = rpc_run_task(&task_setup);
589 if (IS_ERR(task))
590 ret = PTR_ERR(task);
591 else {
592 ret = task->tk_status;
593 rpc_put_task(task);
594 }
595 return ret;
596}
597
cccef3b9
AA
598int _nfs4_call_sync_session(struct nfs_server *server,
599 struct rpc_message *msg,
600 struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res,
602 int cache_reply)
603{
ce5039c1
AA
604 return nfs4_call_sync_sequence(server->nfs_client, server->client,
605 msg, args, res, cache_reply);
cccef3b9
AA
606}
607
608#endif /* CONFIG_NFS_V4_1 */
609
610int _nfs4_call_sync(struct nfs_server *server,
611 struct rpc_message *msg,
612 struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res,
614 int cache_reply)
615{
f3752975 616 args->sa_session = res->sr_session = NULL;
cccef3b9
AA
617 return rpc_call_sync(server->client, msg, 0);
618}
619
620#define nfs4_call_sync(server, msg, args, res, cache_reply) \
621 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
622 &(res)->seq_res, (cache_reply))
623
b0df806c
AA
624static void nfs4_sequence_done(const struct nfs_server *server,
625 struct nfs4_sequence_res *res, int rpc_status)
626{
627#ifdef CONFIG_NFS_V4_1
628 if (nfs4_has_session(server->nfs_client))
629 nfs41_sequence_done(server->nfs_client, res, rpc_status);
630#endif /* CONFIG_NFS_V4_1 */
631}
632
633/* no restart, therefore free slot here */
634static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
635 struct nfs4_sequence_res *res,
636 int rpc_status)
637{
638 nfs4_sequence_done(server, res, rpc_status);
639 nfs4_sequence_free_slot(server->nfs_client, res);
640}
641
38478b24 642static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1da177e4 643{
38478b24 644 struct nfs_inode *nfsi = NFS_I(dir);
1da177e4 645
38478b24 646 spin_lock(&dir->i_lock);
40d24704
TM
647 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
648 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
bfc69a45 649 nfs_force_lookup_revalidate(dir);
40d24704 650 nfsi->change_attr = cinfo->after;
38478b24 651 spin_unlock(&dir->i_lock);
1da177e4
LT
652}
653
e56e0b78 654struct nfs4_opendata {
c6d00e63 655 struct kref kref;
e56e0b78
TM
656 struct nfs_openargs o_arg;
657 struct nfs_openres o_res;
cdd4e68b
TM
658 struct nfs_open_confirmargs c_arg;
659 struct nfs_open_confirmres c_res;
e56e0b78
TM
660 struct nfs_fattr f_attr;
661 struct nfs_fattr dir_attr;
ad389da7 662 struct path path;
e56e0b78
TM
663 struct dentry *dir;
664 struct nfs4_state_owner *owner;
aac00a8d 665 struct nfs4_state *state;
e56e0b78 666 struct iattr attrs;
26e976a8 667 unsigned long timestamp;
3e309914 668 unsigned int rpc_done : 1;
24ac23ab
TM
669 int rpc_status;
670 int cancelled;
e56e0b78
TM
671};
672
2ced46c2
TM
673
674static void nfs4_init_opendata_res(struct nfs4_opendata *p)
675{
676 p->o_res.f_attr = &p->f_attr;
677 p->o_res.dir_attr = &p->dir_attr;
c1d51931
TM
678 p->o_res.seqid = p->o_arg.seqid;
679 p->c_res.seqid = p->c_arg.seqid;
2ced46c2
TM
680 p->o_res.server = p->o_arg.server;
681 nfs_fattr_init(&p->f_attr);
682 nfs_fattr_init(&p->dir_attr);
683}
684
ad389da7 685static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
dc0b027d 686 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
e56e0b78
TM
687 const struct iattr *attrs)
688{
ad389da7 689 struct dentry *parent = dget_parent(path->dentry);
e56e0b78
TM
690 struct inode *dir = parent->d_inode;
691 struct nfs_server *server = NFS_SERVER(dir);
692 struct nfs4_opendata *p;
693
694 p = kzalloc(sizeof(*p), GFP_KERNEL);
695 if (p == NULL)
696 goto err;
697 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
698 if (p->o_arg.seqid == NULL)
699 goto err_free;
ad389da7
TM
700 p->path.mnt = mntget(path->mnt);
701 p->path.dentry = dget(path->dentry);
e56e0b78
TM
702 p->dir = parent;
703 p->owner = sp;
704 atomic_inc(&sp->so_count);
705 p->o_arg.fh = NFS_FH(dir);
dc0b027d
TM
706 p->o_arg.open_flags = flags;
707 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
7539bbab 708 p->o_arg.clientid = server->nfs_client->cl_clientid;
9f958ab8 709 p->o_arg.id = sp->so_owner_id.id;
ad389da7 710 p->o_arg.name = &p->path.dentry->d_name;
e56e0b78
TM
711 p->o_arg.server = server;
712 p->o_arg.bitmask = server->attr_bitmask;
713 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
5f7dbd5c 714 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
e56e0b78
TM
715 if (flags & O_EXCL) {
716 u32 *s = (u32 *) p->o_arg.u.verifier.data;
717 s[0] = jiffies;
718 s[1] = current->pid;
719 } else if (flags & O_CREAT) {
720 p->o_arg.u.attrs = &p->attrs;
721 memcpy(&p->attrs, attrs, sizeof(p->attrs));
722 }
cdd4e68b
TM
723 p->c_arg.fh = &p->o_res.fh;
724 p->c_arg.stateid = &p->o_res.stateid;
725 p->c_arg.seqid = p->o_arg.seqid;
2ced46c2 726 nfs4_init_opendata_res(p);
c6d00e63 727 kref_init(&p->kref);
e56e0b78
TM
728 return p;
729err_free:
730 kfree(p);
731err:
732 dput(parent);
733 return NULL;
734}
735
c6d00e63 736static void nfs4_opendata_free(struct kref *kref)
e56e0b78 737{
c6d00e63
TM
738 struct nfs4_opendata *p = container_of(kref,
739 struct nfs4_opendata, kref);
740
741 nfs_free_seqid(p->o_arg.seqid);
aac00a8d
TM
742 if (p->state != NULL)
743 nfs4_put_open_state(p->state);
c6d00e63
TM
744 nfs4_put_state_owner(p->owner);
745 dput(p->dir);
31f31db1 746 path_put(&p->path);
c6d00e63
TM
747 kfree(p);
748}
749
750static void nfs4_opendata_put(struct nfs4_opendata *p)
751{
752 if (p != NULL)
753 kref_put(&p->kref, nfs4_opendata_free);
e56e0b78
TM
754}
755
06f814a3
TM
756static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
757{
06f814a3
TM
758 int ret;
759
06f814a3 760 ret = rpc_wait_for_completion_task(task);
06f814a3
TM
761 return ret;
762}
763
dc0b027d 764static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
6ee41268
TM
765{
766 int ret = 0;
dc0b027d
TM
767
768 if (open_mode & O_EXCL)
769 goto out;
770 switch (mode & (FMODE_READ|FMODE_WRITE)) {
6ee41268
TM
771 case FMODE_READ:
772 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
6ee41268
TM
773 break;
774 case FMODE_WRITE:
775 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
6ee41268
TM
776 break;
777 case FMODE_READ|FMODE_WRITE:
778 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
779 }
dc0b027d 780out:
6ee41268
TM
781 return ret;
782}
783
dc0b027d 784static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
aac00a8d 785{
dc0b027d 786 if ((delegation->type & fmode) != fmode)
aac00a8d 787 return 0;
15c831bf 788 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
aac00a8d 789 return 0;
b7391f44 790 nfs_mark_delegation_referenced(delegation);
aac00a8d
TM
791 return 1;
792}
793
dc0b027d 794static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
e7616923 795{
dc0b027d 796 switch (fmode) {
e7616923
TM
797 case FMODE_WRITE:
798 state->n_wronly++;
799 break;
800 case FMODE_READ:
801 state->n_rdonly++;
802 break;
803 case FMODE_READ|FMODE_WRITE:
804 state->n_rdwr++;
805 }
dc0b027d 806 nfs4_state_set_mode_locked(state, state->state | fmode);
003707c7
TM
807}
808
dc0b027d 809static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
003707c7
TM
810{
811 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
812 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
813 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
dc0b027d 814 switch (fmode) {
003707c7
TM
815 case FMODE_READ:
816 set_bit(NFS_O_RDONLY_STATE, &state->flags);
817 break;
818 case FMODE_WRITE:
819 set_bit(NFS_O_WRONLY_STATE, &state->flags);
820 break;
821 case FMODE_READ|FMODE_WRITE:
822 set_bit(NFS_O_RDWR_STATE, &state->flags);
823 }
e7616923
TM
824}
825
dc0b027d 826static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
003707c7 827{
8bda4e4c 828 write_seqlock(&state->seqlock);
dc0b027d 829 nfs_set_open_stateid_locked(state, stateid, fmode);
8bda4e4c 830 write_sequnlock(&state->seqlock);
003707c7
TM
831}
832
dc0b027d 833static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1da177e4 834{
8bda4e4c
TM
835 /*
836 * Protect the call to nfs4_state_set_mode_locked and
837 * serialise the stateid update
838 */
839 write_seqlock(&state->seqlock);
003707c7
TM
840 if (deleg_stateid != NULL) {
841 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
842 set_bit(NFS_DELEGATED_STATE, &state->flags);
843 }
844 if (open_stateid != NULL)
dc0b027d 845 nfs_set_open_stateid_locked(state, open_stateid, fmode);
8bda4e4c
TM
846 write_sequnlock(&state->seqlock);
847 spin_lock(&state->owner->so_lock);
dc0b027d 848 update_open_stateflags(state, fmode);
ec073428 849 spin_unlock(&state->owner->so_lock);
1da177e4
LT
850}
851
dc0b027d 852static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
34310430
TM
853{
854 struct nfs_inode *nfsi = NFS_I(state->inode);
855 struct nfs_delegation *deleg_cur;
856 int ret = 0;
857
dc0b027d 858 fmode &= (FMODE_READ|FMODE_WRITE);
34310430
TM
859
860 rcu_read_lock();
861 deleg_cur = rcu_dereference(nfsi->delegation);
862 if (deleg_cur == NULL)
863 goto no_delegation;
864
865 spin_lock(&deleg_cur->lock);
866 if (nfsi->delegation != deleg_cur ||
dc0b027d 867 (deleg_cur->type & fmode) != fmode)
34310430
TM
868 goto no_delegation_unlock;
869
870 if (delegation == NULL)
871 delegation = &deleg_cur->stateid;
872 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
873 goto no_delegation_unlock;
874
b7391f44 875 nfs_mark_delegation_referenced(deleg_cur);
dc0b027d 876 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
34310430
TM
877 ret = 1;
878no_delegation_unlock:
879 spin_unlock(&deleg_cur->lock);
880no_delegation:
881 rcu_read_unlock();
882
883 if (!ret && open_stateid != NULL) {
dc0b027d 884 __update_open_stateid(state, open_stateid, NULL, fmode);
34310430
TM
885 ret = 1;
886 }
887
888 return ret;
889}
890
891
dc0b027d 892static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
aac00a8d
TM
893{
894 struct nfs_delegation *delegation;
895
896 rcu_read_lock();
897 delegation = rcu_dereference(NFS_I(inode)->delegation);
dc0b027d 898 if (delegation == NULL || (delegation->type & fmode) == fmode) {
aac00a8d
TM
899 rcu_read_unlock();
900 return;
901 }
902 rcu_read_unlock();
903 nfs_inode_return_delegation(inode);
904}
905
6ee41268 906static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
aac00a8d
TM
907{
908 struct nfs4_state *state = opendata->state;
909 struct nfs_inode *nfsi = NFS_I(state->inode);
910 struct nfs_delegation *delegation;
dc0b027d
TM
911 int open_mode = opendata->o_arg.open_flags & O_EXCL;
912 fmode_t fmode = opendata->o_arg.fmode;
aac00a8d
TM
913 nfs4_stateid stateid;
914 int ret = -EAGAIN;
915
aac00a8d 916 for (;;) {
dc0b027d 917 if (can_open_cached(state, fmode, open_mode)) {
6ee41268 918 spin_lock(&state->owner->so_lock);
dc0b027d
TM
919 if (can_open_cached(state, fmode, open_mode)) {
920 update_open_stateflags(state, fmode);
6ee41268 921 spin_unlock(&state->owner->so_lock);
6ee41268
TM
922 goto out_return_state;
923 }
924 spin_unlock(&state->owner->so_lock);
925 }
34310430
TM
926 rcu_read_lock();
927 delegation = rcu_dereference(nfsi->delegation);
928 if (delegation == NULL ||
dc0b027d 929 !can_open_delegated(delegation, fmode)) {
34310430 930 rcu_read_unlock();
aac00a8d 931 break;
34310430 932 }
aac00a8d
TM
933 /* Save the delegation */
934 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
935 rcu_read_unlock();
af22f94a 936 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
aac00a8d
TM
937 if (ret != 0)
938 goto out;
939 ret = -EAGAIN;
34310430
TM
940
941 /* Try to update the stateid using the delegation */
dc0b027d 942 if (update_open_stateid(state, NULL, &stateid, fmode))
34310430 943 goto out_return_state;
aac00a8d 944 }
aac00a8d
TM
945out:
946 return ERR_PTR(ret);
947out_return_state:
948 atomic_inc(&state->count);
949 return state;
950}
951
24ac23ab
TM
952static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
953{
954 struct inode *inode;
955 struct nfs4_state *state = NULL;
003707c7 956 struct nfs_delegation *delegation;
1b370bc2 957 int ret;
24ac23ab 958
aac00a8d 959 if (!data->rpc_done) {
6ee41268 960 state = nfs4_try_open_cached(data);
aac00a8d
TM
961 goto out;
962 }
963
1b370bc2 964 ret = -EAGAIN;
24ac23ab 965 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1b370bc2 966 goto err;
24ac23ab 967 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1b370bc2 968 ret = PTR_ERR(inode);
03f28e3a 969 if (IS_ERR(inode))
1b370bc2
TM
970 goto err;
971 ret = -ENOMEM;
24ac23ab
TM
972 state = nfs4_get_open_state(inode, data->owner);
973 if (state == NULL)
1b370bc2 974 goto err_put_inode;
549d6ed5 975 if (data->o_res.delegation_type != 0) {
549d6ed5
TM
976 int delegation_flags = 0;
977
003707c7
TM
978 rcu_read_lock();
979 delegation = rcu_dereference(NFS_I(inode)->delegation);
980 if (delegation)
981 delegation_flags = delegation->flags;
982 rcu_read_unlock();
15c831bf 983 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
549d6ed5
TM
984 nfs_inode_set_delegation(state->inode,
985 data->owner->so_cred,
986 &data->o_res);
987 else
988 nfs_inode_reclaim_delegation(state->inode,
989 data->owner->so_cred,
990 &data->o_res);
991 }
34310430
TM
992
993 update_open_stateid(state, &data->o_res.stateid, NULL,
dc0b027d 994 data->o_arg.fmode);
24ac23ab 995 iput(inode);
aac00a8d 996out:
24ac23ab 997 return state;
1b370bc2
TM
998err_put_inode:
999 iput(inode);
1000err:
1001 return ERR_PTR(ret);
24ac23ab
TM
1002}
1003
864472e9
TM
1004static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1005{
1006 struct nfs_inode *nfsi = NFS_I(state->inode);
1007 struct nfs_open_context *ctx;
1008
1009 spin_lock(&state->inode->i_lock);
1010 list_for_each_entry(ctx, &nfsi->open_files, list) {
1011 if (ctx->state != state)
1012 continue;
1013 get_nfs_open_context(ctx);
1014 spin_unlock(&state->inode->i_lock);
1015 return ctx;
1016 }
1017 spin_unlock(&state->inode->i_lock);
1018 return ERR_PTR(-ENOENT);
1019}
1020
6f220ed5
TM
1021static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1022{
1023 struct nfs4_opendata *opendata;
1024
dc0b027d 1025 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
6f220ed5
TM
1026 if (opendata == NULL)
1027 return ERR_PTR(-ENOMEM);
1028 opendata->state = state;
1029 atomic_inc(&state->count);
1030 return opendata;
1031}
1032
dc0b027d 1033static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
864472e9 1034{
2ced46c2 1035 struct nfs4_state *newstate;
864472e9
TM
1036 int ret;
1037
dc0b027d
TM
1038 opendata->o_arg.open_flags = 0;
1039 opendata->o_arg.fmode = fmode;
2ced46c2
TM
1040 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1041 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1042 nfs4_init_opendata_res(opendata);
864472e9
TM
1043 ret = _nfs4_proc_open(opendata);
1044 if (ret != 0)
1045 return ret;
2ced46c2 1046 newstate = nfs4_opendata_to_nfs4_state(opendata);
1b370bc2
TM
1047 if (IS_ERR(newstate))
1048 return PTR_ERR(newstate);
dc0b027d 1049 nfs4_close_state(&opendata->path, newstate, fmode);
2ced46c2 1050 *res = newstate;
864472e9
TM
1051 return 0;
1052}
1053
1054static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1055{
864472e9 1056 struct nfs4_state *newstate;
864472e9
TM
1057 int ret;
1058
1059 /* memory barrier prior to reading state->n_* */
2ced46c2 1060 clear_bit(NFS_DELEGATED_STATE, &state->flags);
864472e9
TM
1061 smp_rmb();
1062 if (state->n_rdwr != 0) {
2ced46c2 1063 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
864472e9
TM
1064 if (ret != 0)
1065 return ret;
2ced46c2
TM
1066 if (newstate != state)
1067 return -ESTALE;
864472e9
TM
1068 }
1069 if (state->n_wronly != 0) {
2ced46c2 1070 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
864472e9
TM
1071 if (ret != 0)
1072 return ret;
2ced46c2
TM
1073 if (newstate != state)
1074 return -ESTALE;
864472e9
TM
1075 }
1076 if (state->n_rdonly != 0) {
2ced46c2 1077 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
864472e9
TM
1078 if (ret != 0)
1079 return ret;
2ced46c2
TM
1080 if (newstate != state)
1081 return -ESTALE;
864472e9 1082 }
1ac7e2fd
TM
1083 /*
1084 * We may have performed cached opens for all three recoveries.
1085 * Check if we need to update the current stateid.
1086 */
1087 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1088 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
8bda4e4c 1089 write_seqlock(&state->seqlock);
1ac7e2fd
TM
1090 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1091 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
8bda4e4c 1092 write_sequnlock(&state->seqlock);
1ac7e2fd 1093 }
864472e9
TM
1094 return 0;
1095}
1096
1da177e4
LT
1097/*
1098 * OPEN_RECLAIM:
1099 * reclaim state on the server after a reboot.
1da177e4 1100 */
539cd03a 1101static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4 1102{
1ac7e2fd 1103 struct nfs_delegation *delegation;
864472e9 1104 struct nfs4_opendata *opendata;
dc0b027d 1105 fmode_t delegation_type = 0;
1da177e4
LT
1106 int status;
1107
6f220ed5
TM
1108 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1109 if (IS_ERR(opendata))
1110 return PTR_ERR(opendata);
864472e9
TM
1111 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1112 opendata->o_arg.fh = NFS_FH(state->inode);
1ac7e2fd
TM
1113 rcu_read_lock();
1114 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
15c831bf 1115 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
65bbf6bd 1116 delegation_type = delegation->type;
1ac7e2fd 1117 rcu_read_unlock();
864472e9
TM
1118 opendata->o_arg.u.delegation_type = delegation_type;
1119 status = nfs4_open_recover(opendata, state);
c6d00e63 1120 nfs4_opendata_put(opendata);
1da177e4
LT
1121 return status;
1122}
1123
539cd03a 1124static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4
LT
1125{
1126 struct nfs_server *server = NFS_SERVER(state->inode);
1127 struct nfs4_exception exception = { };
1128 int err;
1129 do {
539cd03a 1130 err = _nfs4_do_open_reclaim(ctx, state);
202b50dc
TM
1131 if (err != -NFS4ERR_DELAY)
1132 break;
1133 nfs4_handle_exception(server, err, &exception);
1da177e4
LT
1134 } while (exception.retry);
1135 return err;
1136}
1137
864472e9
TM
1138static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1139{
1140 struct nfs_open_context *ctx;
1141 int ret;
1142
1143 ctx = nfs4_state_find_open_context(state);
1144 if (IS_ERR(ctx))
1145 return PTR_ERR(ctx);
539cd03a 1146 ret = nfs4_do_open_reclaim(ctx, state);
864472e9
TM
1147 put_nfs_open_context(ctx);
1148 return ret;
1149}
1150
13437e12 1151static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1da177e4 1152{
e56e0b78 1153 struct nfs4_opendata *opendata;
864472e9 1154 int ret;
1da177e4 1155
6f220ed5
TM
1156 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1157 if (IS_ERR(opendata))
1158 return PTR_ERR(opendata);
e56e0b78 1159 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
13437e12 1160 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
e56e0b78 1161 sizeof(opendata->o_arg.u.delegation.data));
864472e9 1162 ret = nfs4_open_recover(opendata, state);
c6d00e63 1163 nfs4_opendata_put(opendata);
864472e9 1164 return ret;
1da177e4
LT
1165}
1166
13437e12 1167int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1da177e4
LT
1168{
1169 struct nfs4_exception exception = { };
539cd03a 1170 struct nfs_server *server = NFS_SERVER(state->inode);
1da177e4
LT
1171 int err;
1172 do {
13437e12 1173 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1da177e4
LT
1174 switch (err) {
1175 case 0:
1176 return err;
1177 case -NFS4ERR_STALE_CLIENTID:
1178 case -NFS4ERR_STALE_STATEID:
1179 case -NFS4ERR_EXPIRED:
1180 /* Don't recall a delegation if it was lost */
7539bbab 1181 nfs4_schedule_state_recovery(server->nfs_client);
1da177e4
LT
1182 return err;
1183 }
1184 err = nfs4_handle_exception(server, err, &exception);
1185 } while (exception.retry);
1186 return err;
1187}
1188
cdd4e68b
TM
1189static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1190{
1191 struct nfs4_opendata *data = calldata;
1192
1193 data->rpc_status = task->tk_status;
1194 if (RPC_ASSASSINATED(task))
1195 return;
26e976a8 1196 if (data->rpc_status == 0) {
cdd4e68b
TM
1197 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1198 sizeof(data->o_res.stateid.data));
bb22629e 1199 nfs_confirm_seqid(&data->owner->so_seqid, 0);
26e976a8 1200 renew_lease(data->o_res.server, data->timestamp);
3e309914 1201 data->rpc_done = 1;
26e976a8 1202 }
cdd4e68b
TM
1203}
1204
1205static void nfs4_open_confirm_release(void *calldata)
1206{
1207 struct nfs4_opendata *data = calldata;
1208 struct nfs4_state *state = NULL;
1209
1210 /* If this request hasn't been cancelled, do nothing */
1211 if (data->cancelled == 0)
1212 goto out_free;
1213 /* In case of error, no cleanup! */
3e309914 1214 if (!data->rpc_done)
cdd4e68b 1215 goto out_free;
cdd4e68b 1216 state = nfs4_opendata_to_nfs4_state(data);
1b370bc2 1217 if (!IS_ERR(state))
dc0b027d 1218 nfs4_close_state(&data->path, state, data->o_arg.fmode);
cdd4e68b 1219out_free:
c6d00e63 1220 nfs4_opendata_put(data);
cdd4e68b
TM
1221}
1222
1223static const struct rpc_call_ops nfs4_open_confirm_ops = {
cdd4e68b
TM
1224 .rpc_call_done = nfs4_open_confirm_done,
1225 .rpc_release = nfs4_open_confirm_release,
1226};
1227
1228/*
1229 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1230 */
1231static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1232{
1233 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1234 struct rpc_task *task;
5138fde0
TM
1235 struct rpc_message msg = {
1236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1237 .rpc_argp = &data->c_arg,
1238 .rpc_resp = &data->c_res,
1239 .rpc_cred = data->owner->so_cred,
1240 };
c970aa85
TM
1241 struct rpc_task_setup task_setup_data = {
1242 .rpc_client = server->client,
5138fde0 1243 .rpc_message = &msg,
c970aa85
TM
1244 .callback_ops = &nfs4_open_confirm_ops,
1245 .callback_data = data,
101070ca 1246 .workqueue = nfsiod_workqueue,
c970aa85
TM
1247 .flags = RPC_TASK_ASYNC,
1248 };
1da177e4
LT
1249 int status;
1250
c6d00e63 1251 kref_get(&data->kref);
3e309914
TM
1252 data->rpc_done = 0;
1253 data->rpc_status = 0;
5138fde0 1254 data->timestamp = jiffies;
c970aa85 1255 task = rpc_run_task(&task_setup_data);
7a1218a2 1256 if (IS_ERR(task))
cdd4e68b 1257 return PTR_ERR(task);
cdd4e68b
TM
1258 status = nfs4_wait_for_completion_rpc_task(task);
1259 if (status != 0) {
1260 data->cancelled = 1;
1261 smp_wmb();
1262 } else
1263 status = data->rpc_status;
e6b3c4db 1264 rpc_put_task(task);
1da177e4
LT
1265 return status;
1266}
1267
24ac23ab 1268static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1da177e4 1269{
24ac23ab
TM
1270 struct nfs4_opendata *data = calldata;
1271 struct nfs4_state_owner *sp = data->owner;
5138fde0 1272
24ac23ab
TM
1273 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1274 return;
aac00a8d
TM
1275 /*
1276 * Check if we still need to send an OPEN call, or if we can use
1277 * a delegation instead.
1278 */
1279 if (data->state != NULL) {
1280 struct nfs_delegation *delegation;
1281
dc0b027d 1282 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
6ee41268 1283 goto out_no_action;
aac00a8d
TM
1284 rcu_read_lock();
1285 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1286 if (delegation != NULL &&
15c831bf 1287 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
aac00a8d 1288 rcu_read_unlock();
6ee41268 1289 goto out_no_action;
aac00a8d
TM
1290 }
1291 rcu_read_unlock();
1292 }
24ac23ab 1293 /* Update sequence id. */
9f958ab8 1294 data->o_arg.id = sp->so_owner_id.id;
24ac23ab 1295 data->o_arg.clientid = sp->so_client->cl_clientid;
6f220ed5 1296 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
5138fde0 1297 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
6f220ed5
TM
1298 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1299 }
26e976a8 1300 data->timestamp = jiffies;
d898528c
AA
1301 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1302 &data->o_arg.seq_args,
1303 &data->o_res.seq_res, 1, task))
1304 return;
5138fde0 1305 rpc_call_start(task);
6ee41268
TM
1306 return;
1307out_no_action:
1308 task->tk_action = NULL;
1309
24ac23ab 1310}
1da177e4 1311
24ac23ab
TM
1312static void nfs4_open_done(struct rpc_task *task, void *calldata)
1313{
1314 struct nfs4_opendata *data = calldata;
1da177e4 1315
24ac23ab 1316 data->rpc_status = task->tk_status;
d898528c
AA
1317
1318 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1319 task->tk_status);
1320
24ac23ab
TM
1321 if (RPC_ASSASSINATED(task))
1322 return;
1323 if (task->tk_status == 0) {
1324 switch (data->o_res.f_attr->mode & S_IFMT) {
6f926b5b
TM
1325 case S_IFREG:
1326 break;
1327 case S_IFLNK:
24ac23ab 1328 data->rpc_status = -ELOOP;
6f926b5b
TM
1329 break;
1330 case S_IFDIR:
24ac23ab 1331 data->rpc_status = -EISDIR;
6f926b5b
TM
1332 break;
1333 default:
24ac23ab 1334 data->rpc_status = -ENOTDIR;
6f926b5b 1335 }
26e976a8 1336 renew_lease(data->o_res.server, data->timestamp);
0f9f95e0
TM
1337 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1338 nfs_confirm_seqid(&data->owner->so_seqid, 0);
6f926b5b 1339 }
3e309914 1340 data->rpc_done = 1;
24ac23ab 1341}
6f926b5b 1342
24ac23ab
TM
1343static void nfs4_open_release(void *calldata)
1344{
1345 struct nfs4_opendata *data = calldata;
1346 struct nfs4_state *state = NULL;
1347
1348 /* If this request hasn't been cancelled, do nothing */
1349 if (data->cancelled == 0)
1350 goto out_free;
1351 /* In case of error, no cleanup! */
3e309914 1352 if (data->rpc_status != 0 || !data->rpc_done)
24ac23ab
TM
1353 goto out_free;
1354 /* In case we need an open_confirm, no cleanup! */
1355 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1356 goto out_free;
24ac23ab 1357 state = nfs4_opendata_to_nfs4_state(data);
1b370bc2 1358 if (!IS_ERR(state))
dc0b027d 1359 nfs4_close_state(&data->path, state, data->o_arg.fmode);
24ac23ab 1360out_free:
c6d00e63 1361 nfs4_opendata_put(data);
24ac23ab
TM
1362}
1363
1364static const struct rpc_call_ops nfs4_open_ops = {
1365 .rpc_call_prepare = nfs4_open_prepare,
1366 .rpc_call_done = nfs4_open_done,
1367 .rpc_release = nfs4_open_release,
1368};
1369
1370/*
1371 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1372 */
1373static int _nfs4_proc_open(struct nfs4_opendata *data)
1374{
1375 struct inode *dir = data->dir->d_inode;
1376 struct nfs_server *server = NFS_SERVER(dir);
1377 struct nfs_openargs *o_arg = &data->o_arg;
1378 struct nfs_openres *o_res = &data->o_res;
1379 struct rpc_task *task;
5138fde0
TM
1380 struct rpc_message msg = {
1381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1382 .rpc_argp = o_arg,
1383 .rpc_resp = o_res,
1384 .rpc_cred = data->owner->so_cred,
1385 };
c970aa85
TM
1386 struct rpc_task_setup task_setup_data = {
1387 .rpc_client = server->client,
5138fde0 1388 .rpc_message = &msg,
c970aa85
TM
1389 .callback_ops = &nfs4_open_ops,
1390 .callback_data = data,
101070ca 1391 .workqueue = nfsiod_workqueue,
c970aa85
TM
1392 .flags = RPC_TASK_ASYNC,
1393 };
24ac23ab
TM
1394 int status;
1395
c6d00e63 1396 kref_get(&data->kref);
3e309914
TM
1397 data->rpc_done = 0;
1398 data->rpc_status = 0;
2ced46c2 1399 data->cancelled = 0;
c970aa85 1400 task = rpc_run_task(&task_setup_data);
7a1218a2 1401 if (IS_ERR(task))
24ac23ab 1402 return PTR_ERR(task);
24ac23ab
TM
1403 status = nfs4_wait_for_completion_rpc_task(task);
1404 if (status != 0) {
1405 data->cancelled = 1;
1406 smp_wmb();
1407 } else
1408 status = data->rpc_status;
e6b3c4db 1409 rpc_put_task(task);
3e309914 1410 if (status != 0 || !data->rpc_done)
24ac23ab
TM
1411 return status;
1412
9936781d
TM
1413 if (o_res->fh.size == 0)
1414 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1415
56ae19f3
TM
1416 if (o_arg->open_flags & O_CREAT) {
1417 update_changeattr(dir, &o_res->cinfo);
1418 nfs_post_op_update_inode(dir, o_res->dir_attr);
1419 } else
1420 nfs_refresh_inode(dir, o_res->dir_attr);
1da177e4 1421 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
cdd4e68b 1422 status = _nfs4_proc_open_confirm(data);
1da177e4 1423 if (status != 0)
24ac23ab 1424 return status;
1da177e4
LT
1425 }
1426 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
9936781d 1427 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
24ac23ab 1428 return 0;
1da177e4
LT
1429}
1430
10afec90 1431static int nfs4_recover_expired_lease(struct nfs_server *server)
58d9714a 1432{
7539bbab 1433 struct nfs_client *clp = server->nfs_client;
6b30954e 1434 int ret;
58d9714a 1435
6b30954e 1436 for (;;) {
65de872e 1437 ret = nfs4_wait_clnt_recover(clp);
6b30954e
TM
1438 if (ret != 0)
1439 return ret;
e598d843
TM
1440 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1441 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
6b30954e 1442 break;
58d9714a 1443 nfs4_schedule_state_recovery(clp);
6b30954e
TM
1444 }
1445 return 0;
58d9714a
TM
1446}
1447
1da177e4
LT
1448/*
1449 * OPEN_EXPIRED:
1450 * reclaim state on the server after a network partition.
1451 * Assumes caller holds the appropriate lock
1452 */
539cd03a 1453static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1da177e4 1454{
e56e0b78 1455 struct nfs4_opendata *opendata;
864472e9 1456 int ret;
1da177e4 1457
6f220ed5
TM
1458 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1459 if (IS_ERR(opendata))
1460 return PTR_ERR(opendata);
864472e9 1461 ret = nfs4_open_recover(opendata, state);
35d05778 1462 if (ret == -ESTALE)
539cd03a 1463 d_drop(ctx->path.dentry);
c6d00e63 1464 nfs4_opendata_put(opendata);
864472e9 1465 return ret;
1da177e4
LT
1466}
1467
539cd03a 1468static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
202b50dc 1469{
539cd03a 1470 struct nfs_server *server = NFS_SERVER(state->inode);
202b50dc
TM
1471 struct nfs4_exception exception = { };
1472 int err;
1473
1474 do {
539cd03a 1475 err = _nfs4_open_expired(ctx, state);
027b6ca0
TM
1476 if (err != -NFS4ERR_DELAY)
1477 break;
1478 nfs4_handle_exception(server, err, &exception);
202b50dc
TM
1479 } while (exception.retry);
1480 return err;
1481}
1482
1da177e4
LT
1483static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1484{
1da177e4 1485 struct nfs_open_context *ctx;
864472e9 1486 int ret;
1da177e4 1487
864472e9
TM
1488 ctx = nfs4_state_find_open_context(state);
1489 if (IS_ERR(ctx))
1490 return PTR_ERR(ctx);
539cd03a 1491 ret = nfs4_do_open_expired(ctx, state);
864472e9
TM
1492 put_nfs_open_context(ctx);
1493 return ret;
1da177e4
LT
1494}
1495
aa53ed54
JL
1496/*
1497 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1498 * fields corresponding to attributes that were used to store the verifier.
1499 * Make sure we clobber those fields in the later setattr call
1500 */
1501static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1502{
1503 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1504 !(sattr->ia_valid & ATTR_ATIME_SET))
1505 sattr->ia_valid |= ATTR_ATIME;
1506
1507 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1508 !(sattr->ia_valid & ATTR_MTIME_SET))
1509 sattr->ia_valid |= ATTR_MTIME;
1510}
1511
1da177e4 1512/*
24ac23ab 1513 * Returns a referenced nfs4_state
1da177e4 1514 */
dc0b027d 1515static 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)
1da177e4
LT
1516{
1517 struct nfs4_state_owner *sp;
1518 struct nfs4_state *state = NULL;
1519 struct nfs_server *server = NFS_SERVER(dir);
e56e0b78 1520 struct nfs4_opendata *opendata;
aac00a8d 1521 int status;
1da177e4
LT
1522
1523 /* Protect against reboot recovery conflicts */
1da177e4
LT
1524 status = -ENOMEM;
1525 if (!(sp = nfs4_get_state_owner(server, cred))) {
1526 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1527 goto out_err;
1528 }
58d9714a
TM
1529 status = nfs4_recover_expired_lease(server);
1530 if (status != 0)
b4454fe1 1531 goto err_put_state_owner;
aac00a8d 1532 if (path->dentry->d_inode != NULL)
dc0b027d 1533 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
58d9714a 1534 status = -ENOMEM;
dc0b027d 1535 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
e56e0b78 1536 if (opendata == NULL)
95d35cb4 1537 goto err_put_state_owner;
1da177e4 1538
aac00a8d
TM
1539 if (path->dentry->d_inode != NULL)
1540 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1541
24ac23ab 1542 status = _nfs4_proc_open(opendata);
1da177e4 1543 if (status != 0)
c6d00e63 1544 goto err_opendata_put;
1da177e4 1545
aa53ed54
JL
1546 if (opendata->o_arg.open_flags & O_EXCL)
1547 nfs4_exclusive_attrset(opendata, sattr);
1548
24ac23ab 1549 state = nfs4_opendata_to_nfs4_state(opendata);
1b370bc2
TM
1550 status = PTR_ERR(state);
1551 if (IS_ERR(state))
c6d00e63 1552 goto err_opendata_put;
c6d00e63 1553 nfs4_opendata_put(opendata);
1da177e4 1554 nfs4_put_state_owner(sp);
1da177e4
LT
1555 *res = state;
1556 return 0;
c6d00e63
TM
1557err_opendata_put:
1558 nfs4_opendata_put(opendata);
e56e0b78
TM
1559err_put_state_owner:
1560 nfs4_put_state_owner(sp);
1da177e4 1561out_err:
1da177e4
LT
1562 *res = NULL;
1563 return status;
1564}
1565
1566
dc0b027d 1567static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1da177e4
LT
1568{
1569 struct nfs4_exception exception = { };
1570 struct nfs4_state *res;
1571 int status;
1572
1573 do {
dc0b027d 1574 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1da177e4
LT
1575 if (status == 0)
1576 break;
1577 /* NOTE: BAD_SEQID means the server and client disagree about the
1578 * book-keeping w.r.t. state-changing operations
1579 * (OPEN/CLOSE/LOCK/LOCKU...)
1580 * It is actually a sign of a bug on the client or on the server.
1581 *
1582 * If we receive a BAD_SEQID error in the particular case of
cee54fc9 1583 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1da177e4
LT
1584 * have unhashed the old state_owner for us, and that we can
1585 * therefore safely retry using a new one. We should still warn
1586 * the user though...
1587 */
1588 if (status == -NFS4ERR_BAD_SEQID) {
6f43ddcc
TM
1589 printk(KERN_WARNING "NFS: v4 server %s "
1590 " returned a bad sequence-id error!\n",
1591 NFS_SERVER(dir)->nfs_client->cl_hostname);
1da177e4
LT
1592 exception.retry = 1;
1593 continue;
1594 }
550f5747
TM
1595 /*
1596 * BAD_STATEID on OPEN means that the server cancelled our
1597 * state before it received the OPEN_CONFIRM.
1598 * Recover by retrying the request as per the discussion
1599 * on Page 181 of RFC3530.
1600 */
1601 if (status == -NFS4ERR_BAD_STATEID) {
1602 exception.retry = 1;
1603 continue;
1604 }
aac00a8d
TM
1605 if (status == -EAGAIN) {
1606 /* We must have found a delegation */
1607 exception.retry = 1;
1608 continue;
1609 }
1da177e4
LT
1610 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1611 status, &exception));
1612 } while (exception.retry);
1613 return res;
1614}
1615
659bfcd6
TM
1616static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1617 struct nfs_fattr *fattr, struct iattr *sattr,
1618 struct nfs4_state *state)
1da177e4 1619{
3e4f6290 1620 struct nfs_server *server = NFS_SERVER(inode);
1da177e4 1621 struct nfs_setattrargs arg = {
3e4f6290 1622 .fh = NFS_FH(inode),
1da177e4
LT
1623 .iap = sattr,
1624 .server = server,
1625 .bitmask = server->attr_bitmask,
1626 };
1627 struct nfs_setattrres res = {
1628 .fattr = fattr,
1629 .server = server,
1630 };
1631 struct rpc_message msg = {
659bfcd6
TM
1632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1633 .rpc_argp = &arg,
1634 .rpc_resp = &res,
1635 .rpc_cred = cred,
1da177e4 1636 };
26e976a8 1637 unsigned long timestamp = jiffies;
65e4308d 1638 int status;
1da177e4 1639
0e574af1 1640 nfs_fattr_init(fattr);
1da177e4 1641
3e4f6290
TM
1642 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1643 /* Use that stateid */
1644 } else if (state != NULL) {
08e9eac4
TM
1645 nfs4_copy_stateid(&arg.stateid, state, current->files);
1646 } else
1da177e4
LT
1647 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1648
cccef3b9 1649 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
26e976a8
TM
1650 if (status == 0 && state != NULL)
1651 renew_lease(server, timestamp);
65e4308d 1652 return status;
1da177e4
LT
1653}
1654
659bfcd6
TM
1655static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1656 struct nfs_fattr *fattr, struct iattr *sattr,
1657 struct nfs4_state *state)
1da177e4 1658{
3e4f6290 1659 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
1660 struct nfs4_exception exception = { };
1661 int err;
1662 do {
1663 err = nfs4_handle_exception(server,
659bfcd6 1664 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1da177e4
LT
1665 &exception);
1666 } while (exception.retry);
1667 return err;
1668}
1669
1670struct nfs4_closedata {
4a35bd41 1671 struct path path;
1da177e4
LT
1672 struct inode *inode;
1673 struct nfs4_state *state;
1674 struct nfs_closeargs arg;
1675 struct nfs_closeres res;
516a6af6 1676 struct nfs_fattr fattr;
26e976a8 1677 unsigned long timestamp;
1da177e4
LT
1678};
1679
963d8fe5 1680static void nfs4_free_closedata(void *data)
9512135d 1681{
963d8fe5
TM
1682 struct nfs4_closedata *calldata = data;
1683 struct nfs4_state_owner *sp = calldata->state->owner;
9512135d
TM
1684
1685 nfs4_put_open_state(calldata->state);
1686 nfs_free_seqid(calldata->arg.seqid);
9512135d 1687 nfs4_put_state_owner(sp);
31f31db1 1688 path_put(&calldata->path);
9512135d
TM
1689 kfree(calldata);
1690}
1691
963d8fe5 1692static void nfs4_close_done(struct rpc_task *task, void *data)
1da177e4 1693{
963d8fe5 1694 struct nfs4_closedata *calldata = data;
1da177e4 1695 struct nfs4_state *state = calldata->state;
1da177e4
LT
1696 struct nfs_server *server = NFS_SERVER(calldata->inode);
1697
19ddab06 1698 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
4ce70ada
TM
1699 if (RPC_ASSASSINATED(task))
1700 return;
1da177e4
LT
1701 /* hmm. we are done with the inode, and in the process of freeing
1702 * the state_owner. we keep this around to process errors
1703 */
1da177e4
LT
1704 switch (task->tk_status) {
1705 case 0:
45328c35 1706 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
26e976a8 1707 renew_lease(server, calldata->timestamp);
1da177e4
LT
1708 break;
1709 case -NFS4ERR_STALE_STATEID:
9e33bed5
TM
1710 case -NFS4ERR_OLD_STATEID:
1711 case -NFS4ERR_BAD_STATEID:
1da177e4 1712 case -NFS4ERR_EXPIRED:
dc0b027d 1713 if (calldata->arg.fmode == 0)
9e33bed5 1714 break;
1da177e4 1715 default:
9e33bed5 1716 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
eedc020e 1717 nfs4_restart_rpc(task, server->nfs_client);
1da177e4
LT
1718 return;
1719 }
1720 }
19ddab06 1721 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
516a6af6 1722 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1da177e4
LT
1723}
1724
4ce70ada 1725static void nfs4_close_prepare(struct rpc_task *task, void *data)
1da177e4 1726{
4ce70ada 1727 struct nfs4_closedata *calldata = data;
9512135d 1728 struct nfs4_state *state = calldata->state;
003707c7 1729 int clear_rd, clear_wr, clear_rdwr;
9512135d 1730
963d8fe5 1731 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
9512135d 1732 return;
003707c7 1733
003707c7 1734 clear_rd = clear_wr = clear_rdwr = 0;
4cecb76f 1735 spin_lock(&state->owner->so_lock);
003707c7 1736 /* Calculate the change in open mode */
e7616923 1737 if (state->n_rdwr == 0) {
003707c7 1738 if (state->n_rdonly == 0) {
003707c7
TM
1739 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1740 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1741 }
1742 if (state->n_wronly == 0) {
003707c7
TM
1743 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1744 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1745 }
e7616923 1746 }
4cecb76f 1747 spin_unlock(&state->owner->so_lock);
003707c7 1748 if (!clear_rd && !clear_wr && !clear_rdwr) {
963d8fe5
TM
1749 /* Note: exit _without_ calling nfs4_close_done */
1750 task->tk_action = NULL;
9512135d
TM
1751 return;
1752 }
516a6af6 1753 nfs_fattr_init(calldata->res.fattr);
45328c35 1754 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
5138fde0 1755 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
dc0b027d 1756 calldata->arg.fmode = FMODE_READ;
45328c35 1757 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
5138fde0 1758 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
dc0b027d 1759 calldata->arg.fmode = FMODE_WRITE;
45328c35 1760 }
26e976a8 1761 calldata->timestamp = jiffies;
19ddab06
AA
1762 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1763 &calldata->arg.seq_args, &calldata->res.seq_res,
1764 1, task))
1765 return;
5138fde0 1766 rpc_call_start(task);
1da177e4
LT
1767}
1768
963d8fe5 1769static const struct rpc_call_ops nfs4_close_ops = {
4ce70ada 1770 .rpc_call_prepare = nfs4_close_prepare,
963d8fe5
TM
1771 .rpc_call_done = nfs4_close_done,
1772 .rpc_release = nfs4_free_closedata,
1773};
1774
1da177e4
LT
1775/*
1776 * It is possible for data to be read/written from a mem-mapped file
1777 * after the sys_close call (which hits the vfs layer as a flush).
1778 * This means that we can't safely call nfsv4 close on a file until
1779 * the inode is cleared. This in turn means that we are not good
1780 * NFSv4 citizens - we do not indicate to the server to update the file's
1781 * share state even when we are done with one of the three share
1782 * stateid's in the inode.
1783 *
1784 * NOTE: Caller must be holding the sp->so_owner semaphore!
1785 */
a49c3c77 1786int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1da177e4 1787{
4a35bd41 1788 struct nfs_server *server = NFS_SERVER(state->inode);
1da177e4 1789 struct nfs4_closedata *calldata;
b39e625b
TM
1790 struct nfs4_state_owner *sp = state->owner;
1791 struct rpc_task *task;
5138fde0
TM
1792 struct rpc_message msg = {
1793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1794 .rpc_cred = state->owner->so_cred,
1795 };
c970aa85
TM
1796 struct rpc_task_setup task_setup_data = {
1797 .rpc_client = server->client,
5138fde0 1798 .rpc_message = &msg,
c970aa85 1799 .callback_ops = &nfs4_close_ops,
101070ca 1800 .workqueue = nfsiod_workqueue,
c970aa85
TM
1801 .flags = RPC_TASK_ASYNC,
1802 };
9512135d 1803 int status = -ENOMEM;
1da177e4 1804
19ddab06 1805 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1da177e4 1806 if (calldata == NULL)
9512135d 1807 goto out;
4a35bd41 1808 calldata->inode = state->inode;
1da177e4 1809 calldata->state = state;
4a35bd41 1810 calldata->arg.fh = NFS_FH(state->inode);
003707c7 1811 calldata->arg.stateid = &state->open_stateid;
34dc1ad7
BH
1812 if (nfs4_has_session(server->nfs_client))
1813 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
1da177e4 1814 /* Serialization for the sequence id */
cee54fc9 1815 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
9512135d
TM
1816 if (calldata->arg.seqid == NULL)
1817 goto out_free_calldata;
dc0b027d 1818 calldata->arg.fmode = 0;
a65318bf 1819 calldata->arg.bitmask = server->cache_consistency_bitmask;
516a6af6 1820 calldata->res.fattr = &calldata->fattr;
c1d51931 1821 calldata->res.seqid = calldata->arg.seqid;
516a6af6 1822 calldata->res.server = server;
5f7dbd5c 1823 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4a35bd41
TM
1824 calldata->path.mnt = mntget(path->mnt);
1825 calldata->path.dentry = dget(path->dentry);
9512135d 1826
5138fde0
TM
1827 msg.rpc_argp = &calldata->arg,
1828 msg.rpc_resp = &calldata->res,
c970aa85
TM
1829 task_setup_data.callback_data = calldata;
1830 task = rpc_run_task(&task_setup_data);
b39e625b
TM
1831 if (IS_ERR(task))
1832 return PTR_ERR(task);
a49c3c77
TM
1833 status = 0;
1834 if (wait)
1835 status = rpc_wait_for_completion_task(task);
b39e625b 1836 rpc_put_task(task);
a49c3c77 1837 return status;
9512135d
TM
1838out_free_calldata:
1839 kfree(calldata);
1840out:
b39e625b
TM
1841 nfs4_put_open_state(state);
1842 nfs4_put_state_owner(sp);
9512135d 1843 return status;
1da177e4
LT
1844}
1845
dc0b027d 1846static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
02a913a7
TM
1847{
1848 struct file *filp;
1b45c46c 1849 int ret;
02a913a7 1850
1b45c46c 1851 /* If the open_intent is for execute, we have an extra check to make */
dc0b027d 1852 if (fmode & FMODE_EXEC) {
af22f94a 1853 ret = nfs_may_open(state->inode,
1b45c46c
TM
1854 state->owner->so_cred,
1855 nd->intent.open.flags);
1856 if (ret < 0)
1857 goto out_close;
1858 }
4a35bd41 1859 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
02a913a7
TM
1860 if (!IS_ERR(filp)) {
1861 struct nfs_open_context *ctx;
cd3758e3 1862 ctx = nfs_file_open_context(filp);
02a913a7 1863 ctx->state = state;
95cf959b
TM
1864 return 0;
1865 }
1b45c46c
TM
1866 ret = PTR_ERR(filp);
1867out_close:
dc0b027d 1868 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1b45c46c 1869 return ret;
02a913a7
TM
1870}
1871
1872struct dentry *
1da177e4
LT
1873nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1874{
ad389da7 1875 struct path path = {
4ac91378 1876 .mnt = nd->path.mnt,
ad389da7
TM
1877 .dentry = dentry,
1878 };
4ac91378 1879 struct dentry *parent;
1da177e4
LT
1880 struct iattr attr;
1881 struct rpc_cred *cred;
1882 struct nfs4_state *state;
02a913a7 1883 struct dentry *res;
dc0b027d 1884 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1da177e4
LT
1885
1886 if (nd->flags & LOOKUP_CREATE) {
1887 attr.ia_mode = nd->intent.open.create_mode;
1888 attr.ia_valid = ATTR_MODE;
1889 if (!IS_POSIXACL(dir))
ce3b0f8d 1890 attr.ia_mode &= ~current_umask();
1da177e4
LT
1891 } else {
1892 attr.ia_valid = 0;
1893 BUG_ON(nd->intent.open.flags & O_CREAT);
1894 }
1895
98a8e323 1896 cred = rpc_lookup_cred();
1da177e4 1897 if (IS_ERR(cred))
02a913a7 1898 return (struct dentry *)cred;
565277f6
TM
1899 parent = dentry->d_parent;
1900 /* Protect against concurrent sillydeletes */
1901 nfs_block_sillyrename(parent);
dc0b027d 1902 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1da177e4 1903 put_rpccred(cred);
02a913a7 1904 if (IS_ERR(state)) {
d75340cc 1905 if (PTR_ERR(state) == -ENOENT) {
02a913a7 1906 d_add(dentry, NULL);
d75340cc
TM
1907 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1908 }
565277f6 1909 nfs_unblock_sillyrename(parent);
02a913a7
TM
1910 return (struct dentry *)state;
1911 }
24ac23ab 1912 res = d_add_unique(dentry, igrab(state->inode));
02a913a7 1913 if (res != NULL)
deee9369 1914 path.dentry = res;
d75340cc 1915 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
565277f6 1916 nfs_unblock_sillyrename(parent);
dc0b027d 1917 nfs4_intent_set_file(nd, &path, state, fmode);
02a913a7 1918 return res;
1da177e4
LT
1919}
1920
1921int
02a913a7 1922nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1da177e4 1923{
ad389da7 1924 struct path path = {
4ac91378 1925 .mnt = nd->path.mnt,
ad389da7
TM
1926 .dentry = dentry,
1927 };
1da177e4
LT
1928 struct rpc_cred *cred;
1929 struct nfs4_state *state;
dc0b027d 1930 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1da177e4 1931
98a8e323 1932 cred = rpc_lookup_cred();
1da177e4
LT
1933 if (IS_ERR(cred))
1934 return PTR_ERR(cred);
dc0b027d 1935 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1da177e4 1936 put_rpccred(cred);
02a913a7
TM
1937 if (IS_ERR(state)) {
1938 switch (PTR_ERR(state)) {
1939 case -EPERM:
1940 case -EACCES:
1941 case -EDQUOT:
1942 case -ENOSPC:
1943 case -EROFS:
1944 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1945 return 1;
b87c0adf
CL
1946 default:
1947 goto out_drop;
02a913a7 1948 }
02a913a7 1949 }
24ac23ab 1950 if (state->inode == dentry->d_inode) {
d75340cc 1951 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
dc0b027d 1952 nfs4_intent_set_file(nd, &path, state, fmode);
1da177e4
LT
1953 return 1;
1954 }
dc0b027d 1955 nfs4_close_sync(&path, state, fmode);
02a913a7
TM
1956out_drop:
1957 d_drop(dentry);
1da177e4
LT
1958 return 0;
1959}
1960
7fe5c398
TM
1961void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1962{
1963 if (ctx->state == NULL)
1964 return;
1965 if (is_sync)
1966 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1967 else
1968 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1969}
1da177e4
LT
1970
1971static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1972{
43652ad5
BH
1973 struct nfs4_server_caps_arg args = {
1974 .fhandle = fhandle,
1975 };
1da177e4
LT
1976 struct nfs4_server_caps_res res = {};
1977 struct rpc_message msg = {
1978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
43652ad5 1979 .rpc_argp = &args,
1da177e4
LT
1980 .rpc_resp = &res,
1981 };
1982 int status;
1983
cccef3b9 1984 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1da177e4
LT
1985 if (status == 0) {
1986 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1987 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1988 server->caps |= NFS_CAP_ACLS;
1989 if (res.has_links != 0)
1990 server->caps |= NFS_CAP_HARDLINKS;
1991 if (res.has_symlinks != 0)
1992 server->caps |= NFS_CAP_SYMLINKS;
a65318bf
TM
1993 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1994 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1995 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1da177e4
LT
1996 server->acl_bitmask = res.acl_bitmask;
1997 }
cccef3b9 1998
1da177e4
LT
1999 return status;
2000}
2001
55a97593 2002int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1da177e4
LT
2003{
2004 struct nfs4_exception exception = { };
2005 int err;
2006 do {
2007 err = nfs4_handle_exception(server,
2008 _nfs4_server_capabilities(server, fhandle),
2009 &exception);
2010 } while (exception.retry);
2011 return err;
2012}
2013
2014static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2015 struct nfs_fsinfo *info)
2016{
1da177e4
LT
2017 struct nfs4_lookup_root_arg args = {
2018 .bitmask = nfs4_fattr_bitmap,
2019 };
2020 struct nfs4_lookup_res res = {
2021 .server = server,
0e574af1 2022 .fattr = info->fattr,
1da177e4
LT
2023 .fh = fhandle,
2024 };
2025 struct rpc_message msg = {
2026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2027 .rpc_argp = &args,
2028 .rpc_resp = &res,
2029 };
008f55d0
BH
2030 int status;
2031
0e574af1 2032 nfs_fattr_init(info->fattr);
008f55d0
BH
2033 status = nfs4_recover_expired_lease(server);
2034 if (!status)
2035 status = nfs4_check_client_ready(server->nfs_client);
2036 if (!status)
2037 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2038 return status;
1da177e4
LT
2039}
2040
2041static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2042 struct nfs_fsinfo *info)
2043{
2044 struct nfs4_exception exception = { };
2045 int err;
2046 do {
2047 err = nfs4_handle_exception(server,
2048 _nfs4_lookup_root(server, fhandle, info),
2049 &exception);
2050 } while (exception.retry);
2051 return err;
2052}
2053
54ceac45
DH
2054/*
2055 * get the file handle for the "/" directory on the server
2056 */
1da177e4 2057static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
54ceac45 2058 struct nfs_fsinfo *info)
1da177e4 2059{
1da177e4
LT
2060 int status;
2061
1da177e4 2062 status = nfs4_lookup_root(server, fhandle, info);
1da177e4
LT
2063 if (status == 0)
2064 status = nfs4_server_capabilities(server, fhandle);
2065 if (status == 0)
2066 status = nfs4_do_fsinfo(server, fhandle, info);
c12e87f4 2067 return nfs4_map_errors(status);
1da177e4
LT
2068}
2069
6b97fd3d
MN
2070/*
2071 * Get locations and (maybe) other attributes of a referral.
2072 * Note that we'll actually follow the referral later when
2073 * we detect fsid mismatch in inode revalidation
2074 */
56659e99 2075static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
6b97fd3d
MN
2076{
2077 int status = -ENOMEM;
2078 struct page *page = NULL;
2079 struct nfs4_fs_locations *locations = NULL;
6b97fd3d
MN
2080
2081 page = alloc_page(GFP_KERNEL);
2082 if (page == NULL)
2083 goto out;
2084 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2085 if (locations == NULL)
2086 goto out;
2087
c228fd3a 2088 status = nfs4_proc_fs_locations(dir, name, locations, page);
6b97fd3d
MN
2089 if (status != 0)
2090 goto out;
2091 /* Make sure server returned a different fsid for the referral */
2092 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3110ff80 2093 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
6b97fd3d
MN
2094 status = -EIO;
2095 goto out;
2096 }
2097
2098 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2099 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2100 if (!fattr->mode)
2101 fattr->mode = S_IFDIR;
2102 memset(fhandle, 0, sizeof(struct nfs_fh));
2103out:
2104 if (page)
2105 __free_page(page);
2106 if (locations)
2107 kfree(locations);
2108 return status;
2109}
2110
1da177e4
LT
2111static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2112{
2113 struct nfs4_getattr_arg args = {
2114 .fh = fhandle,
2115 .bitmask = server->attr_bitmask,
2116 };
2117 struct nfs4_getattr_res res = {
2118 .fattr = fattr,
2119 .server = server,
2120 };
2121 struct rpc_message msg = {
2122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2123 .rpc_argp = &args,
2124 .rpc_resp = &res,
2125 };
2126
0e574af1 2127 nfs_fattr_init(fattr);
cccef3b9 2128 return nfs4_call_sync(server, &msg, &args, &res, 0);
1da177e4
LT
2129}
2130
2131static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2132{
2133 struct nfs4_exception exception = { };
2134 int err;
2135 do {
2136 err = nfs4_handle_exception(server,
2137 _nfs4_proc_getattr(server, fhandle, fattr),
2138 &exception);
2139 } while (exception.retry);
2140 return err;
2141}
2142
2143/*
2144 * The file is not closed if it is opened due to the a request to change
2145 * the size of the file. The open call will not be needed once the
2146 * VFS layer lookup-intents are implemented.
2147 *
2148 * Close is called when the inode is destroyed.
2149 * If we haven't opened the file for O_WRONLY, we
2150 * need to in the size_change case to obtain a stateid.
2151 *
2152 * Got race?
2153 * Because OPEN is always done by name in nfsv4, it is
2154 * possible that we opened a different file by the same
2155 * name. We can recognize this race condition, but we
2156 * can't do anything about it besides returning an error.
2157 *
2158 * This will be fixed with VFS changes (lookup-intent).
2159 */
2160static int
2161nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2162 struct iattr *sattr)
2163{
08e9eac4 2164 struct inode *inode = dentry->d_inode;
659bfcd6 2165 struct rpc_cred *cred = NULL;
d530838b 2166 struct nfs4_state *state = NULL;
1da177e4
LT
2167 int status;
2168
0e574af1 2169 nfs_fattr_init(fattr);
1da177e4 2170
d530838b 2171 /* Search for an existing open(O_WRITE) file */
659bfcd6
TM
2172 if (sattr->ia_valid & ATTR_FILE) {
2173 struct nfs_open_context *ctx;
2174
2175 ctx = nfs_file_open_context(sattr->ia_file);
504e5189
NB
2176 if (ctx) {
2177 cred = ctx->cred;
2178 state = ctx->state;
2179 }
659bfcd6 2180 }
08e9eac4 2181
659bfcd6 2182 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
65e4308d
TM
2183 if (status == 0)
2184 nfs_setattr_update_inode(inode, sattr);
1da177e4
LT
2185 return status;
2186}
2187
56659e99
TM
2188static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2189 const struct qstr *name, struct nfs_fh *fhandle,
2b3de441
DH
2190 struct nfs_fattr *fattr)
2191{
2192 int status;
2193 struct nfs4_lookup_arg args = {
2194 .bitmask = server->attr_bitmask,
2195 .dir_fh = dirfh,
2196 .name = name,
2197 };
2198 struct nfs4_lookup_res res = {
2199 .server = server,
2200 .fattr = fattr,
2201 .fh = fhandle,
2202 };
2203 struct rpc_message msg = {
2204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2205 .rpc_argp = &args,
2206 .rpc_resp = &res,
2207 };
2208
2209 nfs_fattr_init(fattr);
2210
2211 dprintk("NFS call lookupfh %s\n", name->name);
cccef3b9 2212 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2b3de441 2213 dprintk("NFS reply lookupfh: %d\n", status);
2b3de441
DH
2214 return status;
2215}
2216
2217static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2218 struct qstr *name, struct nfs_fh *fhandle,
2219 struct nfs_fattr *fattr)
2220{
2221 struct nfs4_exception exception = { };
2222 int err;
2223 do {
4e56e082
TM
2224 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2225 /* FIXME: !!!! */
2226 if (err == -NFS4ERR_MOVED) {
2227 err = -EREMOTE;
2228 break;
2229 }
2230 err = nfs4_handle_exception(server, err, &exception);
2b3de441
DH
2231 } while (exception.retry);
2232 return err;
2233}
2234
56659e99 2235static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
1da177e4
LT
2236 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2237{
4e56e082 2238 int status;
1da177e4
LT
2239
2240 dprintk("NFS call lookup %s\n", name->name);
4e56e082 2241 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
6b97fd3d
MN
2242 if (status == -NFS4ERR_MOVED)
2243 status = nfs4_get_referral(dir, name, fattr, fhandle);
1da177e4
LT
2244 dprintk("NFS reply lookup: %d\n", status);
2245 return status;
2246}
2247
2248static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2249{
2250 struct nfs4_exception exception = { };
2251 int err;
2252 do {
2253 err = nfs4_handle_exception(NFS_SERVER(dir),
2254 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2255 &exception);
2256 } while (exception.retry);
2257 return err;
2258}
2259
2260static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2261{
76b32999
TM
2262 struct nfs_server *server = NFS_SERVER(inode);
2263 struct nfs_fattr fattr;
1da177e4
LT
2264 struct nfs4_accessargs args = {
2265 .fh = NFS_FH(inode),
76b32999
TM
2266 .bitmask = server->attr_bitmask,
2267 };
2268 struct nfs4_accessres res = {
2269 .server = server,
2270 .fattr = &fattr,
1da177e4 2271 };
1da177e4
LT
2272 struct rpc_message msg = {
2273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2274 .rpc_argp = &args,
2275 .rpc_resp = &res,
2276 .rpc_cred = entry->cred,
2277 };
2278 int mode = entry->mask;
2279 int status;
2280
2281 /*
2282 * Determine which access bits we want to ask for...
2283 */
2284 if (mode & MAY_READ)
2285 args.access |= NFS4_ACCESS_READ;
2286 if (S_ISDIR(inode->i_mode)) {
2287 if (mode & MAY_WRITE)
2288 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2289 if (mode & MAY_EXEC)
2290 args.access |= NFS4_ACCESS_LOOKUP;
2291 } else {
2292 if (mode & MAY_WRITE)
2293 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2294 if (mode & MAY_EXEC)
2295 args.access |= NFS4_ACCESS_EXECUTE;
2296 }
76b32999 2297 nfs_fattr_init(&fattr);
cccef3b9 2298 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1da177e4
LT
2299 if (!status) {
2300 entry->mask = 0;
2301 if (res.access & NFS4_ACCESS_READ)
2302 entry->mask |= MAY_READ;
2303 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2304 entry->mask |= MAY_WRITE;
2305 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2306 entry->mask |= MAY_EXEC;
76b32999 2307 nfs_refresh_inode(inode, &fattr);
1da177e4
LT
2308 }
2309 return status;
2310}
2311
2312static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2313{
2314 struct nfs4_exception exception = { };
2315 int err;
2316 do {
2317 err = nfs4_handle_exception(NFS_SERVER(inode),
2318 _nfs4_proc_access(inode, entry),
2319 &exception);
2320 } while (exception.retry);
2321 return err;
2322}
2323
2324/*
2325 * TODO: For the time being, we don't try to get any attributes
2326 * along with any of the zero-copy operations READ, READDIR,
2327 * READLINK, WRITE.
2328 *
2329 * In the case of the first three, we want to put the GETATTR
2330 * after the read-type operation -- this is because it is hard
2331 * to predict the length of a GETATTR response in v4, and thus
2332 * align the READ data correctly. This means that the GETATTR
2333 * may end up partially falling into the page cache, and we should
2334 * shift it into the 'tail' of the xdr_buf before processing.
2335 * To do this efficiently, we need to know the total length
2336 * of data received, which doesn't seem to be available outside
2337 * of the RPC layer.
2338 *
2339 * In the case of WRITE, we also want to put the GETATTR after
2340 * the operation -- in this case because we want to make sure
2341 * we get the post-operation mtime and size. This means that
2342 * we can't use xdr_encode_pages() as written: we need a variant
2343 * of it which would leave room in the 'tail' iovec.
2344 *
2345 * Both of these changes to the XDR layer would in fact be quite
2346 * minor, but I decided to leave them for a subsequent patch.
2347 */
2348static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2349 unsigned int pgbase, unsigned int pglen)
2350{
2351 struct nfs4_readlink args = {
2352 .fh = NFS_FH(inode),
2353 .pgbase = pgbase,
2354 .pglen = pglen,
2355 .pages = &page,
2356 };
f50c7000 2357 struct nfs4_readlink_res res;
1da177e4
LT
2358 struct rpc_message msg = {
2359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2360 .rpc_argp = &args,
f50c7000 2361 .rpc_resp = &res,
1da177e4
LT
2362 };
2363
cccef3b9 2364 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
1da177e4
LT
2365}
2366
2367static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2368 unsigned int pgbase, unsigned int pglen)
2369{
2370 struct nfs4_exception exception = { };
2371 int err;
2372 do {
2373 err = nfs4_handle_exception(NFS_SERVER(inode),
2374 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2375 &exception);
2376 } while (exception.retry);
2377 return err;
2378}
2379
1da177e4
LT
2380/*
2381 * Got race?
2382 * We will need to arrange for the VFS layer to provide an atomic open.
2383 * Until then, this create/open method is prone to inefficiency and race
2384 * conditions due to the lookup, create, and open VFS calls from sys_open()
2385 * placed on the wire.
2386 *
2387 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2388 * The file will be opened again in the subsequent VFS open call
2389 * (nfs4_proc_file_open).
2390 *
2391 * The open for read will just hang around to be used by any process that
2392 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2393 */
2394
2395static int
2396nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
02a913a7 2397 int flags, struct nameidata *nd)
1da177e4 2398{
ad389da7 2399 struct path path = {
4ac91378 2400 .mnt = nd->path.mnt,
ad389da7
TM
2401 .dentry = dentry,
2402 };
1da177e4
LT
2403 struct nfs4_state *state;
2404 struct rpc_cred *cred;
dc0b027d 2405 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
1da177e4
LT
2406 int status = 0;
2407
98a8e323 2408 cred = rpc_lookup_cred();
1da177e4
LT
2409 if (IS_ERR(cred)) {
2410 status = PTR_ERR(cred);
2411 goto out;
2412 }
dc0b027d 2413 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
d4d9cdcb 2414 d_drop(dentry);
1da177e4
LT
2415 if (IS_ERR(state)) {
2416 status = PTR_ERR(state);
659bfcd6 2417 goto out_putcred;
1da177e4 2418 }
d4d9cdcb 2419 d_add(dentry, igrab(state->inode));
d75340cc 2420 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1da177e4
LT
2421 if (flags & O_EXCL) {
2422 struct nfs_fattr fattr;
659bfcd6 2423 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
02a913a7 2424 if (status == 0)
65e4308d 2425 nfs_setattr_update_inode(state->inode, sattr);
aa53ed54 2426 nfs_post_op_update_inode(state->inode, &fattr);
02a913a7 2427 }
ad389da7 2428 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
dc0b027d 2429 status = nfs4_intent_set_file(nd, &path, state, fmode);
02a913a7 2430 else
dc0b027d 2431 nfs4_close_sync(&path, state, fmode);
659bfcd6
TM
2432out_putcred:
2433 put_rpccred(cred);
1da177e4
LT
2434out:
2435 return status;
2436}
2437
2438static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2439{
16e42959 2440 struct nfs_server *server = NFS_SERVER(dir);
4fdc17b2 2441 struct nfs_removeargs args = {
1da177e4 2442 .fh = NFS_FH(dir),
4fdc17b2
TM
2443 .name.len = name->len,
2444 .name.name = name->name,
16e42959
TM
2445 .bitmask = server->attr_bitmask,
2446 };
4fdc17b2 2447 struct nfs_removeres res = {
16e42959 2448 .server = server,
1da177e4 2449 };
1da177e4 2450 struct rpc_message msg = {
4fdc17b2
TM
2451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2452 .rpc_argp = &args,
2453 .rpc_resp = &res,
1da177e4
LT
2454 };
2455 int status;
2456
4fdc17b2 2457 nfs_fattr_init(&res.dir_attr);
cccef3b9 2458 status = nfs4_call_sync(server, &msg, &args, &res, 1);
16e42959
TM
2459 if (status == 0) {
2460 update_changeattr(dir, &res.cinfo);
4fdc17b2 2461 nfs_post_op_update_inode(dir, &res.dir_attr);
16e42959 2462 }
1da177e4
LT
2463 return status;
2464}
2465
2466static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2467{
2468 struct nfs4_exception exception = { };
2469 int err;
2470 do {
2471 err = nfs4_handle_exception(NFS_SERVER(dir),
2472 _nfs4_proc_remove(dir, name),
2473 &exception);
2474 } while (exception.retry);
2475 return err;
2476}
2477
e4eff1a6 2478static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
1da177e4 2479{
e4eff1a6
TM
2480 struct nfs_server *server = NFS_SERVER(dir);
2481 struct nfs_removeargs *args = msg->rpc_argp;
2482 struct nfs_removeres *res = msg->rpc_resp;
1da177e4 2483
a65318bf 2484 args->bitmask = server->cache_consistency_bitmask;
e4eff1a6 2485 res->server = server;
1da177e4 2486 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1da177e4
LT
2487}
2488
e4eff1a6 2489static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
1da177e4 2490{
e4eff1a6
TM
2491 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2492
472cfbd9 2493 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
9e33bed5 2494 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
e4eff1a6 2495 return 0;
472cfbd9 2496 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
e4eff1a6
TM
2497 update_changeattr(dir, &res->cinfo);
2498 nfs_post_op_update_inode(dir, &res->dir_attr);
2499 return 1;
1da177e4
LT
2500}
2501
2502static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2503 struct inode *new_dir, struct qstr *new_name)
2504{
6caf2c82 2505 struct nfs_server *server = NFS_SERVER(old_dir);
1da177e4
LT
2506 struct nfs4_rename_arg arg = {
2507 .old_dir = NFS_FH(old_dir),
2508 .new_dir = NFS_FH(new_dir),
2509 .old_name = old_name,
2510 .new_name = new_name,
6caf2c82
TM
2511 .bitmask = server->attr_bitmask,
2512 };
2513 struct nfs_fattr old_fattr, new_fattr;
2514 struct nfs4_rename_res res = {
2515 .server = server,
2516 .old_fattr = &old_fattr,
2517 .new_fattr = &new_fattr,
1da177e4 2518 };
1da177e4
LT
2519 struct rpc_message msg = {
2520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2521 .rpc_argp = &arg,
2522 .rpc_resp = &res,
2523 };
2524 int status;
2525
6caf2c82
TM
2526 nfs_fattr_init(res.old_fattr);
2527 nfs_fattr_init(res.new_fattr);
cccef3b9 2528 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1da177e4
LT
2529
2530 if (!status) {
2531 update_changeattr(old_dir, &res.old_cinfo);
6caf2c82 2532 nfs_post_op_update_inode(old_dir, res.old_fattr);
1da177e4 2533 update_changeattr(new_dir, &res.new_cinfo);
6caf2c82 2534 nfs_post_op_update_inode(new_dir, res.new_fattr);
1da177e4
LT
2535 }
2536 return status;
2537}
2538
2539static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2540 struct inode *new_dir, struct qstr *new_name)
2541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2546 _nfs4_proc_rename(old_dir, old_name,
2547 new_dir, new_name),
2548 &exception);
2549 } while (exception.retry);
2550 return err;
2551}
2552
2553static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2554{
91ba2eee 2555 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
2556 struct nfs4_link_arg arg = {
2557 .fh = NFS_FH(inode),
2558 .dir_fh = NFS_FH(dir),
2559 .name = name,
91ba2eee
TM
2560 .bitmask = server->attr_bitmask,
2561 };
2562 struct nfs_fattr fattr, dir_attr;
2563 struct nfs4_link_res res = {
2564 .server = server,
2565 .fattr = &fattr,
2566 .dir_attr = &dir_attr,
1da177e4 2567 };
1da177e4
LT
2568 struct rpc_message msg = {
2569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2570 .rpc_argp = &arg,
91ba2eee 2571 .rpc_resp = &res,
1da177e4
LT
2572 };
2573 int status;
2574
91ba2eee
TM
2575 nfs_fattr_init(res.fattr);
2576 nfs_fattr_init(res.dir_attr);
cccef3b9 2577 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
91ba2eee
TM
2578 if (!status) {
2579 update_changeattr(dir, &res.cinfo);
2580 nfs_post_op_update_inode(dir, res.dir_attr);
73a3d07c 2581 nfs_post_op_update_inode(inode, res.fattr);
91ba2eee 2582 }
1da177e4
LT
2583
2584 return status;
2585}
2586
2587static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2588{
2589 struct nfs4_exception exception = { };
2590 int err;
2591 do {
2592 err = nfs4_handle_exception(NFS_SERVER(inode),
2593 _nfs4_proc_link(inode, dir, name),
2594 &exception);
2595 } while (exception.retry);
2596 return err;
2597}
2598
57dc9a57
TM
2599struct nfs4_createdata {
2600 struct rpc_message msg;
2601 struct nfs4_create_arg arg;
2602 struct nfs4_create_res res;
2603 struct nfs_fh fh;
2604 struct nfs_fattr fattr;
2605 struct nfs_fattr dir_fattr;
2606};
2607
2608static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2609 struct qstr *name, struct iattr *sattr, u32 ftype)
2610{
2611 struct nfs4_createdata *data;
2612
2613 data = kzalloc(sizeof(*data), GFP_KERNEL);
2614 if (data != NULL) {
2615 struct nfs_server *server = NFS_SERVER(dir);
2616
2617 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2618 data->msg.rpc_argp = &data->arg;
2619 data->msg.rpc_resp = &data->res;
2620 data->arg.dir_fh = NFS_FH(dir);
2621 data->arg.server = server;
2622 data->arg.name = name;
2623 data->arg.attrs = sattr;
2624 data->arg.ftype = ftype;
2625 data->arg.bitmask = server->attr_bitmask;
2626 data->res.server = server;
2627 data->res.fh = &data->fh;
2628 data->res.fattr = &data->fattr;
2629 data->res.dir_fattr = &data->dir_fattr;
2630 nfs_fattr_init(data->res.fattr);
2631 nfs_fattr_init(data->res.dir_fattr);
2632 }
2633 return data;
2634}
2635
2636static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2637{
cccef3b9
AA
2638 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2639 &data->arg, &data->res, 1);
57dc9a57
TM
2640 if (status == 0) {
2641 update_changeattr(dir, &data->res.dir_cinfo);
2642 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2643 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2644 }
2645 return status;
2646}
2647
2648static void nfs4_free_createdata(struct nfs4_createdata *data)
2649{
2650 kfree(data);
2651}
2652
4f390c15 2653static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
94a6d753 2654 struct page *page, unsigned int len, struct iattr *sattr)
1da177e4 2655{
57dc9a57
TM
2656 struct nfs4_createdata *data;
2657 int status = -ENAMETOOLONG;
1da177e4 2658
94a6d753 2659 if (len > NFS4_MAXPATHLEN)
57dc9a57 2660 goto out;
4f390c15 2661
57dc9a57
TM
2662 status = -ENOMEM;
2663 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2664 if (data == NULL)
2665 goto out;
2666
2667 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2668 data->arg.u.symlink.pages = &page;
2669 data->arg.u.symlink.len = len;
1da177e4 2670
57dc9a57
TM
2671 status = nfs4_do_create(dir, dentry, data);
2672
2673 nfs4_free_createdata(data);
2674out:
1da177e4
LT
2675 return status;
2676}
2677
4f390c15 2678static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
94a6d753 2679 struct page *page, unsigned int len, struct iattr *sattr)
1da177e4
LT
2680{
2681 struct nfs4_exception exception = { };
2682 int err;
2683 do {
2684 err = nfs4_handle_exception(NFS_SERVER(dir),
94a6d753
CL
2685 _nfs4_proc_symlink(dir, dentry, page,
2686 len, sattr),
1da177e4
LT
2687 &exception);
2688 } while (exception.retry);
2689 return err;
2690}
2691
2692static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2693 struct iattr *sattr)
2694{
57dc9a57
TM
2695 struct nfs4_createdata *data;
2696 int status = -ENOMEM;
1da177e4 2697
57dc9a57
TM
2698 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2699 if (data == NULL)
2700 goto out;
2701
2702 status = nfs4_do_create(dir, dentry, data);
2703
2704 nfs4_free_createdata(data);
2705out:
1da177e4
LT
2706 return status;
2707}
2708
2709static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2710 struct iattr *sattr)
2711{
2712 struct nfs4_exception exception = { };
2713 int err;
2714 do {
2715 err = nfs4_handle_exception(NFS_SERVER(dir),
2716 _nfs4_proc_mkdir(dir, dentry, sattr),
2717 &exception);
2718 } while (exception.retry);
2719 return err;
2720}
2721
2722static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2723 u64 cookie, struct page *page, unsigned int count, int plus)
2724{
2725 struct inode *dir = dentry->d_inode;
2726 struct nfs4_readdir_arg args = {
2727 .fh = NFS_FH(dir),
2728 .pages = &page,
2729 .pgbase = 0,
2730 .count = count,
a65318bf 2731 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
1da177e4
LT
2732 };
2733 struct nfs4_readdir_res res;
2734 struct rpc_message msg = {
2735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2736 .rpc_argp = &args,
2737 .rpc_resp = &res,
2738 .rpc_cred = cred,
2739 };
2740 int status;
2741
3110ff80 2742 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
eadf4598
TM
2743 dentry->d_parent->d_name.name,
2744 dentry->d_name.name,
2745 (unsigned long long)cookie);
1da177e4
LT
2746 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2747 res.pgbase = args.pgbase;
cccef3b9 2748 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
1da177e4
LT
2749 if (status == 0)
2750 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
c4812998
TM
2751
2752 nfs_invalidate_atime(dir);
2753
3110ff80 2754 dprintk("%s: returns %d\n", __func__, status);
1da177e4
LT
2755 return status;
2756}
2757
2758static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2759 u64 cookie, struct page *page, unsigned int count, int plus)
2760{
2761 struct nfs4_exception exception = { };
2762 int err;
2763 do {
2764 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2765 _nfs4_proc_readdir(dentry, cred, cookie,
2766 page, count, plus),
2767 &exception);
2768 } while (exception.retry);
2769 return err;
2770}
2771
2772static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2773 struct iattr *sattr, dev_t rdev)
2774{
57dc9a57
TM
2775 struct nfs4_createdata *data;
2776 int mode = sattr->ia_mode;
2777 int status = -ENOMEM;
1da177e4
LT
2778
2779 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2780 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
57dc9a57
TM
2781
2782 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2783 if (data == NULL)
2784 goto out;
2785
1da177e4 2786 if (S_ISFIFO(mode))
57dc9a57 2787 data->arg.ftype = NF4FIFO;
1da177e4 2788 else if (S_ISBLK(mode)) {
57dc9a57
TM
2789 data->arg.ftype = NF4BLK;
2790 data->arg.u.device.specdata1 = MAJOR(rdev);
2791 data->arg.u.device.specdata2 = MINOR(rdev);
1da177e4
LT
2792 }
2793 else if (S_ISCHR(mode)) {
57dc9a57
TM
2794 data->arg.ftype = NF4CHR;
2795 data->arg.u.device.specdata1 = MAJOR(rdev);
2796 data->arg.u.device.specdata2 = MINOR(rdev);
1da177e4 2797 }
1da177e4 2798
57dc9a57
TM
2799 status = nfs4_do_create(dir, dentry, data);
2800
2801 nfs4_free_createdata(data);
2802out:
1da177e4
LT
2803 return status;
2804}
2805
2806static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2807 struct iattr *sattr, dev_t rdev)
2808{
2809 struct nfs4_exception exception = { };
2810 int err;
2811 do {
2812 err = nfs4_handle_exception(NFS_SERVER(dir),
2813 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2814 &exception);
2815 } while (exception.retry);
2816 return err;
2817}
2818
2819static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2820 struct nfs_fsstat *fsstat)
2821{
2822 struct nfs4_statfs_arg args = {
2823 .fh = fhandle,
2824 .bitmask = server->attr_bitmask,
2825 };
24ad148a
BH
2826 struct nfs4_statfs_res res = {
2827 .fsstat = fsstat,
2828 };
1da177e4
LT
2829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2831 .rpc_argp = &args,
24ad148a 2832 .rpc_resp = &res,
1da177e4
LT
2833 };
2834
0e574af1 2835 nfs_fattr_init(fsstat->fattr);
cccef3b9 2836 return nfs4_call_sync(server, &msg, &args, &res, 0);
1da177e4
LT
2837}
2838
2839static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2840{
2841 struct nfs4_exception exception = { };
2842 int err;
2843 do {
2844 err = nfs4_handle_exception(server,
2845 _nfs4_proc_statfs(server, fhandle, fsstat),
2846 &exception);
2847 } while (exception.retry);
2848 return err;
2849}
2850
2851static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2852 struct nfs_fsinfo *fsinfo)
2853{
2854 struct nfs4_fsinfo_arg args = {
2855 .fh = fhandle,
2856 .bitmask = server->attr_bitmask,
2857 };
3dda5e43
BH
2858 struct nfs4_fsinfo_res res = {
2859 .fsinfo = fsinfo,
2860 };
1da177e4
LT
2861 struct rpc_message msg = {
2862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2863 .rpc_argp = &args,
3dda5e43 2864 .rpc_resp = &res,
1da177e4
LT
2865 };
2866
cccef3b9 2867 return nfs4_call_sync(server, &msg, &args, &res, 0);
1da177e4
LT
2868}
2869
2870static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2871{
2872 struct nfs4_exception exception = { };
2873 int err;
2874
2875 do {
2876 err = nfs4_handle_exception(server,
2877 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2878 &exception);
2879 } while (exception.retry);
2880 return err;
2881}
2882
2883static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2884{
0e574af1 2885 nfs_fattr_init(fsinfo->fattr);
1da177e4
LT
2886 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2887}
2888
2889static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2890 struct nfs_pathconf *pathconf)
2891{
2892 struct nfs4_pathconf_arg args = {
2893 .fh = fhandle,
2894 .bitmask = server->attr_bitmask,
2895 };
d45b2989
BH
2896 struct nfs4_pathconf_res res = {
2897 .pathconf = pathconf,
2898 };
1da177e4
LT
2899 struct rpc_message msg = {
2900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2901 .rpc_argp = &args,
d45b2989 2902 .rpc_resp = &res,
1da177e4
LT
2903 };
2904
2905 /* None of the pathconf attributes are mandatory to implement */
2906 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2907 memset(pathconf, 0, sizeof(*pathconf));
2908 return 0;
2909 }
2910
0e574af1 2911 nfs_fattr_init(pathconf->fattr);
cccef3b9 2912 return nfs4_call_sync(server, &msg, &args, &res, 0);
1da177e4
LT
2913}
2914
2915static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2916 struct nfs_pathconf *pathconf)
2917{
2918 struct nfs4_exception exception = { };
2919 int err;
2920
2921 do {
2922 err = nfs4_handle_exception(server,
2923 _nfs4_proc_pathconf(server, fhandle, pathconf),
2924 &exception);
2925 } while (exception.retry);
2926 return err;
2927}
2928
ec06c096 2929static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
1da177e4 2930{
ec06c096 2931 struct nfs_server *server = NFS_SERVER(data->inode);
1da177e4 2932
f11c88af
AA
2933 dprintk("--> %s\n", __func__);
2934
2935 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2936 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2937
9e33bed5 2938 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
eedc020e 2939 nfs4_restart_rpc(task, server->nfs_client);
ec06c096 2940 return -EAGAIN;
1da177e4 2941 }
8850df99
TM
2942
2943 nfs_invalidate_atime(data->inode);
1da177e4 2944 if (task->tk_status > 0)
ec06c096
TM
2945 renew_lease(server, data->timestamp);
2946 return 0;
1da177e4
LT
2947}
2948
bdc7f021 2949static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
1da177e4 2950{
1da177e4 2951 data->timestamp = jiffies;
bdc7f021 2952 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
1da177e4
LT
2953}
2954
788e7a89 2955static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 2956{
1da177e4
LT
2957 struct inode *inode = data->inode;
2958
def6ed7e
AA
2959 /* slot is freed in nfs_writeback_done */
2960 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2961 task->tk_status);
2962
9e33bed5 2963 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
eedc020e 2964 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
788e7a89 2965 return -EAGAIN;
1da177e4 2966 }
4f9838c7 2967 if (task->tk_status >= 0) {
1da177e4 2968 renew_lease(NFS_SERVER(inode), data->timestamp);
70ca8852 2969 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
4f9838c7 2970 }
788e7a89 2971 return 0;
1da177e4
LT
2972}
2973
bdc7f021 2974static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
1da177e4 2975{
bdc7f021
TM
2976 struct nfs_server *server = NFS_SERVER(data->inode);
2977
a65318bf 2978 data->args.bitmask = server->cache_consistency_bitmask;
4f9838c7 2979 data->res.server = server;
1da177e4
LT
2980 data->timestamp = jiffies;
2981
bdc7f021 2982 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
1da177e4
LT
2983}
2984
788e7a89 2985static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
1da177e4 2986{
1da177e4
LT
2987 struct inode *inode = data->inode;
2988
21d9a851
AA
2989 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2990 task->tk_status);
9e33bed5 2991 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
eedc020e 2992 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
788e7a89 2993 return -EAGAIN;
1da177e4 2994 }
21d9a851
AA
2995 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
2996 &data->res.seq_res);
9e08a3c5 2997 nfs_refresh_inode(inode, data->res.fattr);
788e7a89 2998 return 0;
1da177e4
LT
2999}
3000
bdc7f021 3001static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
1da177e4 3002{
788e7a89 3003 struct nfs_server *server = NFS_SERVER(data->inode);
1da177e4 3004
a65318bf 3005 data->args.bitmask = server->cache_consistency_bitmask;
4f9838c7 3006 data->res.server = server;
bdc7f021 3007 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
1da177e4
LT
3008}
3009
3010/*
3011 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3012 * standalone procedure for queueing an asynchronous RENEW.
3013 */
963d8fe5 3014static void nfs4_renew_done(struct rpc_task *task, void *data)
1da177e4 3015{
adfa6f98 3016 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
963d8fe5 3017 unsigned long timestamp = (unsigned long)data;
1da177e4
LT
3018
3019 if (task->tk_status < 0) {
95baa25c
TM
3020 /* Unless we're shutting down, schedule state recovery! */
3021 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3022 nfs4_schedule_state_recovery(clp);
1da177e4
LT
3023 return;
3024 }
3025 spin_lock(&clp->cl_lock);
3026 if (time_before(clp->cl_last_renewal,timestamp))
3027 clp->cl_last_renewal = timestamp;
3028 spin_unlock(&clp->cl_lock);
29fba38b
BH
3029 dprintk("%s calling put_rpccred on rpc_cred %p\n", __func__,
3030 task->tk_msg.rpc_cred);
3031 put_rpccred(task->tk_msg.rpc_cred);
1da177e4
LT
3032}
3033
963d8fe5
TM
3034static const struct rpc_call_ops nfs4_renew_ops = {
3035 .rpc_call_done = nfs4_renew_done,
3036};
3037
adfa6f98 3038int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
1da177e4
LT
3039{
3040 struct rpc_message msg = {
3041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3042 .rpc_argp = clp,
b4454fe1 3043 .rpc_cred = cred,
1da177e4
LT
3044 };
3045
3046 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
963d8fe5 3047 &nfs4_renew_ops, (void *)jiffies);
1da177e4
LT
3048}
3049
adfa6f98 3050int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
1da177e4
LT
3051{
3052 struct rpc_message msg = {
3053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3054 .rpc_argp = clp,
b4454fe1 3055 .rpc_cred = cred,
1da177e4
LT
3056 };
3057 unsigned long now = jiffies;
3058 int status;
3059
3060 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3061 if (status < 0)
3062 return status;
3063 spin_lock(&clp->cl_lock);
3064 if (time_before(clp->cl_last_renewal,now))
3065 clp->cl_last_renewal = now;
3066 spin_unlock(&clp->cl_lock);
3067 return 0;
3068}
3069
aa1870af
BF
3070static inline int nfs4_server_supports_acls(struct nfs_server *server)
3071{
3072 return (server->caps & NFS_CAP_ACLS)
3073 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3074 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3075}
3076
3077/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3078 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3079 * the stack.
3080 */
3081#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3082
3083static void buf_to_pages(const void *buf, size_t buflen,
3084 struct page **pages, unsigned int *pgbase)
3085{
3086 const void *p = buf;
3087
3088 *pgbase = offset_in_page(buf);
3089 p -= *pgbase;
3090 while (p < buf + buflen) {
3091 *(pages++) = virt_to_page(p);
3092 p += PAGE_CACHE_SIZE;
3093 }
3094}
3095
e50a1c2e
BF
3096struct nfs4_cached_acl {
3097 int cached;
3098 size_t len;
3e9d4154 3099 char data[0];
e50a1c2e
BF
3100};
3101
3102static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3103{
3104 struct nfs_inode *nfsi = NFS_I(inode);
3105
3106 spin_lock(&inode->i_lock);
3107 kfree(nfsi->nfs4_acl);
3108 nfsi->nfs4_acl = acl;
3109 spin_unlock(&inode->i_lock);
3110}
3111
3112static void nfs4_zap_acl_attr(struct inode *inode)
3113{
3114 nfs4_set_cached_acl(inode, NULL);
3115}
3116
3117static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3118{
3119 struct nfs_inode *nfsi = NFS_I(inode);
3120 struct nfs4_cached_acl *acl;
3121 int ret = -ENOENT;
3122
3123 spin_lock(&inode->i_lock);
3124 acl = nfsi->nfs4_acl;
3125 if (acl == NULL)
3126 goto out;
3127 if (buf == NULL) /* user is just asking for length */
3128 goto out_len;
3129 if (acl->cached == 0)
3130 goto out;
3131 ret = -ERANGE; /* see getxattr(2) man page */
3132 if (acl->len > buflen)
3133 goto out;
3134 memcpy(buf, acl->data, acl->len);
3135out_len:
3136 ret = acl->len;
3137out:
3138 spin_unlock(&inode->i_lock);
3139 return ret;
3140}
3141
3142static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3143{
3144 struct nfs4_cached_acl *acl;
3145
3146 if (buf && acl_len <= PAGE_SIZE) {
3147 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3148 if (acl == NULL)
3149 goto out;
3150 acl->cached = 1;
3151 memcpy(acl->data, buf, acl_len);
3152 } else {
3153 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3154 if (acl == NULL)
3155 goto out;
3156 acl->cached = 0;
3157 }
3158 acl->len = acl_len;
3159out:
3160 nfs4_set_cached_acl(inode, acl);
3161}
3162
16b4289c 3163static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
aa1870af 3164{
aa1870af
BF
3165 struct page *pages[NFS4ACL_MAXPAGES];
3166 struct nfs_getaclargs args = {
3167 .fh = NFS_FH(inode),
3168 .acl_pages = pages,
3169 .acl_len = buflen,
3170 };
663c79b3
BH
3171 struct nfs_getaclres res = {
3172 .acl_len = buflen,
3173 };
e50a1c2e 3174 void *resp_buf;
aa1870af
BF
3175 struct rpc_message msg = {
3176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3177 .rpc_argp = &args,
663c79b3 3178 .rpc_resp = &res,
aa1870af 3179 };
e50a1c2e 3180 struct page *localpage = NULL;
aa1870af
BF
3181 int ret;
3182
e50a1c2e
BF
3183 if (buflen < PAGE_SIZE) {
3184 /* As long as we're doing a round trip to the server anyway,
3185 * let's be prepared for a page of acl data. */
3186 localpage = alloc_page(GFP_KERNEL);
3187 resp_buf = page_address(localpage);
3188 if (localpage == NULL)
3189 return -ENOMEM;
3190 args.acl_pages[0] = localpage;
3191 args.acl_pgbase = 0;
663c79b3 3192 args.acl_len = PAGE_SIZE;
e50a1c2e
BF
3193 } else {
3194 resp_buf = buf;
3195 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3196 }
cccef3b9 3197 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
e50a1c2e
BF
3198 if (ret)
3199 goto out_free;
663c79b3
BH
3200 if (res.acl_len > args.acl_len)
3201 nfs4_write_cached_acl(inode, NULL, res.acl_len);
e50a1c2e 3202 else
663c79b3 3203 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
e50a1c2e
BF
3204 if (buf) {
3205 ret = -ERANGE;
663c79b3 3206 if (res.acl_len > buflen)
e50a1c2e
BF
3207 goto out_free;
3208 if (localpage)
663c79b3 3209 memcpy(buf, resp_buf, res.acl_len);
e50a1c2e 3210 }
663c79b3 3211 ret = res.acl_len;
e50a1c2e
BF
3212out_free:
3213 if (localpage)
3214 __free_page(localpage);
aa1870af
BF
3215 return ret;
3216}
3217
16b4289c
TM
3218static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3219{
3220 struct nfs4_exception exception = { };
3221 ssize_t ret;
3222 do {
3223 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3224 if (ret >= 0)
3225 break;
3226 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3227 } while (exception.retry);
3228 return ret;
3229}
3230
e50a1c2e
BF
3231static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3232{
3233 struct nfs_server *server = NFS_SERVER(inode);
3234 int ret;
3235
3236 if (!nfs4_server_supports_acls(server))
3237 return -EOPNOTSUPP;
3238 ret = nfs_revalidate_inode(server, inode);
3239 if (ret < 0)
3240 return ret;
f41f7418
TM
3241 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3242 nfs_zap_acl_cache(inode);
e50a1c2e
BF
3243 ret = nfs4_read_cached_acl(inode, buf, buflen);
3244 if (ret != -ENOENT)
3245 return ret;
3246 return nfs4_get_acl_uncached(inode, buf, buflen);
3247}
3248
16b4289c 3249static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4b580ee3
BF
3250{
3251 struct nfs_server *server = NFS_SERVER(inode);
3252 struct page *pages[NFS4ACL_MAXPAGES];
3253 struct nfs_setaclargs arg = {
3254 .fh = NFS_FH(inode),
3255 .acl_pages = pages,
3256 .acl_len = buflen,
3257 };
73c403a9 3258 struct nfs_setaclres res;
4b580ee3
BF
3259 struct rpc_message msg = {
3260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3261 .rpc_argp = &arg,
73c403a9 3262 .rpc_resp = &res,
4b580ee3
BF
3263 };
3264 int ret;
3265
3266 if (!nfs4_server_supports_acls(server))
3267 return -EOPNOTSUPP;
642ac549 3268 nfs_inode_return_delegation(inode);
4b580ee3 3269 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
cccef3b9 3270 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
f41f7418
TM
3271 nfs_access_zap_cache(inode);
3272 nfs_zap_acl_cache(inode);
4b580ee3
BF
3273 return ret;
3274}
3275
16b4289c
TM
3276static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3277{
3278 struct nfs4_exception exception = { };
3279 int err;
3280 do {
3281 err = nfs4_handle_exception(NFS_SERVER(inode),
3282 __nfs4_proc_set_acl(inode, buf, buflen),
3283 &exception);
3284 } while (exception.retry);
3285 return err;
3286}
3287
1da177e4 3288static int
8328d59f 3289_nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
1da177e4 3290{
1da177e4
LT
3291 if (!clp || task->tk_status >= 0)
3292 return 0;
3293 switch(task->tk_status) {
9e33bed5
TM
3294 case -NFS4ERR_ADMIN_REVOKED:
3295 case -NFS4ERR_BAD_STATEID:
3296 case -NFS4ERR_OPENMODE:
3297 if (state == NULL)
3298 break;
3299 nfs4_state_mark_reclaim_nograce(clp, state);
1da177e4
LT
3300 case -NFS4ERR_STALE_CLIENTID:
3301 case -NFS4ERR_STALE_STATEID:
3302 case -NFS4ERR_EXPIRED:
5d00837b 3303 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
1da177e4 3304 nfs4_schedule_state_recovery(clp);
e005e804 3305 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
fda13939 3306 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
1da177e4
LT
3307 task->tk_status = 0;
3308 return -EAGAIN;
4745e315
AA
3309#if defined(CONFIG_NFS_V4_1)
3310 case -NFS4ERR_BADSESSION:
3311 case -NFS4ERR_BADSLOT:
3312 case -NFS4ERR_BAD_HIGH_SLOT:
3313 case -NFS4ERR_DEADSESSION:
3314 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3315 case -NFS4ERR_SEQ_FALSE_RETRY:
3316 case -NFS4ERR_SEQ_MISORDERED:
3317 dprintk("%s ERROR %d, Reset session\n", __func__,
3318 task->tk_status);
3319 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3320 task->tk_status = 0;
3321 return -EAGAIN;
3322#endif /* CONFIG_NFS_V4_1 */
1da177e4 3323 case -NFS4ERR_DELAY:
8328d59f
AA
3324 if (server)
3325 nfs_inc_server_stats(server, NFSIOS_DELAY);
006ea73e 3326 case -NFS4ERR_GRACE:
1da177e4
LT
3327 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3328 task->tk_status = 0;
3329 return -EAGAIN;
3330 case -NFS4ERR_OLD_STATEID:
3331 task->tk_status = 0;
3332 return -EAGAIN;
3333 }
3334 task->tk_status = nfs4_map_errors(task->tk_status);
3335 return 0;
3336}
3337
8328d59f
AA
3338static int
3339nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3340{
3341 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3342}
3343
adfa6f98 3344int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
1da177e4
LT
3345{
3346 nfs4_verifier sc_verifier;
3347 struct nfs4_setclientid setclientid = {
3348 .sc_verifier = &sc_verifier,
3349 .sc_prog = program,
3350 };
3351 struct rpc_message msg = {
3352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3353 .rpc_argp = &setclientid,
3354 .rpc_resp = clp,
286d7d6a 3355 .rpc_cred = cred,
1da177e4 3356 };
bc4785cd 3357 __be32 *p;
1da177e4
LT
3358 int loop = 0;
3359 int status;
3360
bc4785cd 3361 p = (__be32*)sc_verifier.data;
1da177e4
LT
3362 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3363 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3364
3365 for(;;) {
3366 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
69dd716c 3367 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
d4d3c507
CL
3368 clp->cl_ipaddr,
3369 rpc_peeraddr2str(clp->cl_rpcclient,
3370 RPC_DISPLAY_ADDR),
69dd716c
TM
3371 rpc_peeraddr2str(clp->cl_rpcclient,
3372 RPC_DISPLAY_PROTO),
78ea323b 3373 clp->cl_rpcclient->cl_auth->au_ops->au_name,
1da177e4
LT
3374 clp->cl_id_uniquifier);
3375 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
d4d3c507
CL
3376 sizeof(setclientid.sc_netid),
3377 rpc_peeraddr2str(clp->cl_rpcclient,
3378 RPC_DISPLAY_NETID));
1da177e4 3379 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
d4d3c507 3380 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
1da177e4
LT
3381 clp->cl_ipaddr, port >> 8, port & 255);
3382
3383 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3384 if (status != -NFS4ERR_CLID_INUSE)
3385 break;
3386 if (signalled())
3387 break;
3388 if (loop++ & 1)
3389 ssleep(clp->cl_lease_time + 1);
3390 else
3391 if (++clp->cl_id_uniquifier == 0)
3392 break;
3393 }
3394 return status;
3395}
3396
adfa6f98 3397static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
1da177e4
LT
3398{
3399 struct nfs_fsinfo fsinfo;
3400 struct rpc_message msg = {
3401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3402 .rpc_argp = clp,
3403 .rpc_resp = &fsinfo,
286d7d6a 3404 .rpc_cred = cred,
1da177e4
LT
3405 };
3406 unsigned long now;
3407 int status;
3408
3409 now = jiffies;
3410 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3411 if (status == 0) {
3412 spin_lock(&clp->cl_lock);
3413 clp->cl_lease_time = fsinfo.lease_time * HZ;
3414 clp->cl_last_renewal = now;
3415 spin_unlock(&clp->cl_lock);
3416 }
3417 return status;
3418}
3419
adfa6f98 3420int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
51581f3b 3421{
77e03677 3422 long timeout = 0;
51581f3b
TM
3423 int err;
3424 do {
3425 err = _nfs4_proc_setclientid_confirm(clp, cred);
3426 switch (err) {
3427 case 0:
3428 return err;
3429 case -NFS4ERR_RESOURCE:
3430 /* The IBM lawyers misread another document! */
3431 case -NFS4ERR_DELAY:
3432 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3433 }
3434 } while (err == 0);
3435 return err;
3436}
3437
fe650407
TM
3438struct nfs4_delegreturndata {
3439 struct nfs4_delegreturnargs args;
fa178f29 3440 struct nfs4_delegreturnres res;
fe650407
TM
3441 struct nfs_fh fh;
3442 nfs4_stateid stateid;
26e976a8 3443 unsigned long timestamp;
fa178f29 3444 struct nfs_fattr fattr;
fe650407
TM
3445 int rpc_status;
3446};
3447
fe650407
TM
3448static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3449{
3450 struct nfs4_delegreturndata *data = calldata;
938e1010
AA
3451
3452 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3453 task->tk_status);
3454
fe650407 3455 data->rpc_status = task->tk_status;
26e976a8 3456 if (data->rpc_status == 0)
fa178f29 3457 renew_lease(data->res.server, data->timestamp);
fe650407
TM
3458}
3459
3460static void nfs4_delegreturn_release(void *calldata)
3461{
fe650407
TM
3462 kfree(calldata);
3463}
3464
938e1010
AA
3465#if defined(CONFIG_NFS_V4_1)
3466static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3467{
3468 struct nfs4_delegreturndata *d_data;
3469
3470 d_data = (struct nfs4_delegreturndata *)data;
3471
3472 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3473 &d_data->args.seq_args,
3474 &d_data->res.seq_res, 1, task))
3475 return;
3476 rpc_call_start(task);
3477}
3478#endif /* CONFIG_NFS_V4_1 */
3479
c8d149f3 3480static const struct rpc_call_ops nfs4_delegreturn_ops = {
938e1010
AA
3481#if defined(CONFIG_NFS_V4_1)
3482 .rpc_call_prepare = nfs4_delegreturn_prepare,
3483#endif /* CONFIG_NFS_V4_1 */
fe650407
TM
3484 .rpc_call_done = nfs4_delegreturn_done,
3485 .rpc_release = nfs4_delegreturn_release,
3486};
3487
e6f81075 3488static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
fe650407
TM
3489{
3490 struct nfs4_delegreturndata *data;
fa178f29 3491 struct nfs_server *server = NFS_SERVER(inode);
fe650407 3492 struct rpc_task *task;
5138fde0
TM
3493 struct rpc_message msg = {
3494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3495 .rpc_cred = cred,
3496 };
c970aa85
TM
3497 struct rpc_task_setup task_setup_data = {
3498 .rpc_client = server->client,
5138fde0 3499 .rpc_message = &msg,
c970aa85
TM
3500 .callback_ops = &nfs4_delegreturn_ops,
3501 .flags = RPC_TASK_ASYNC,
3502 };
e6f81075 3503 int status = 0;
fe650407 3504
938e1010 3505 data = kzalloc(sizeof(*data), GFP_KERNEL);
fe650407
TM
3506 if (data == NULL)
3507 return -ENOMEM;
3508 data->args.fhandle = &data->fh;
3509 data->args.stateid = &data->stateid;
fa178f29 3510 data->args.bitmask = server->attr_bitmask;
fe650407
TM
3511 nfs_copy_fh(&data->fh, NFS_FH(inode));
3512 memcpy(&data->stateid, stateid, sizeof(data->stateid));
fa178f29
TM
3513 data->res.fattr = &data->fattr;
3514 data->res.server = server;
5f7dbd5c 3515 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5138fde0 3516 nfs_fattr_init(data->res.fattr);
26e976a8 3517 data->timestamp = jiffies;
fe650407
TM
3518 data->rpc_status = 0;
3519
c970aa85 3520 task_setup_data.callback_data = data;
5138fde0
TM
3521 msg.rpc_argp = &data->args,
3522 msg.rpc_resp = &data->res,
c970aa85 3523 task = rpc_run_task(&task_setup_data);
7a1218a2 3524 if (IS_ERR(task))
fe650407 3525 return PTR_ERR(task);
e6f81075
TM
3526 if (!issync)
3527 goto out;
fe650407 3528 status = nfs4_wait_for_completion_rpc_task(task);
e6f81075
TM
3529 if (status != 0)
3530 goto out;
3531 status = data->rpc_status;
3532 if (status != 0)
3533 goto out;
3534 nfs_refresh_inode(inode, &data->fattr);
3535out:
e6b3c4db 3536 rpc_put_task(task);
fe650407 3537 return status;
1da177e4
LT
3538}
3539
e6f81075 3540int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
1da177e4
LT
3541{
3542 struct nfs_server *server = NFS_SERVER(inode);
3543 struct nfs4_exception exception = { };
3544 int err;
3545 do {
e6f81075 3546 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
1da177e4
LT
3547 switch (err) {
3548 case -NFS4ERR_STALE_STATEID:
3549 case -NFS4ERR_EXPIRED:
1da177e4
LT
3550 case 0:
3551 return 0;
3552 }
3553 err = nfs4_handle_exception(server, err, &exception);
3554 } while (exception.retry);
3555 return err;
3556}
3557
3558#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3559#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3560
3561/*
3562 * sleep, with exponential backoff, and retry the LOCK operation.
3563 */
3564static unsigned long
3565nfs4_set_lock_task_retry(unsigned long timeout)
3566{
150030b7 3567 schedule_timeout_killable(timeout);
1da177e4
LT
3568 timeout <<= 1;
3569 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3570 return NFS4_LOCK_MAXTIMEOUT;
3571 return timeout;
3572}
3573
1da177e4
LT
3574static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3575{
3576 struct inode *inode = state->inode;
3577 struct nfs_server *server = NFS_SERVER(inode);
7539bbab 3578 struct nfs_client *clp = server->nfs_client;
911d1aaf 3579 struct nfs_lockt_args arg = {
1da177e4 3580 .fh = NFS_FH(inode),
911d1aaf 3581 .fl = request,
1da177e4 3582 };
911d1aaf
TM
3583 struct nfs_lockt_res res = {
3584 .denied = request,
1da177e4
LT
3585 };
3586 struct rpc_message msg = {
3587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3588 .rpc_argp = &arg,
3589 .rpc_resp = &res,
3590 .rpc_cred = state->owner->so_cred,
3591 };
1da177e4
LT
3592 struct nfs4_lock_state *lsp;
3593 int status;
3594
911d1aaf 3595 arg.lock_owner.clientid = clp->cl_clientid;
8d0a8a9d
TM
3596 status = nfs4_set_lock_state(state, request);
3597 if (status != 0)
3598 goto out;
3599 lsp = request->fl_u.nfs4_fl.owner;
9f958ab8 3600 arg.lock_owner.id = lsp->ls_id.id;
cccef3b9 3601 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
911d1aaf
TM
3602 switch (status) {
3603 case 0:
3604 request->fl_type = F_UNLCK;
3605 break;
3606 case -NFS4ERR_DENIED:
3607 status = 0;
1da177e4 3608 }
70cc6487 3609 request->fl_ops->fl_release_private(request);
8d0a8a9d 3610out:
1da177e4
LT
3611 return status;
3612}
3613
3614static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3615{
3616 struct nfs4_exception exception = { };
3617 int err;
3618
3619 do {
3620 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3621 _nfs4_proc_getlk(state, cmd, request),
3622 &exception);
3623 } while (exception.retry);
3624 return err;
3625}
3626
3627static int do_vfs_lock(struct file *file, struct file_lock *fl)
3628{
3629 int res = 0;
3630 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3631 case FL_POSIX:
3632 res = posix_lock_file_wait(file, fl);
3633 break;
3634 case FL_FLOCK:
3635 res = flock_lock_file_wait(file, fl);
3636 break;
3637 default:
3638 BUG();
3639 }
1da177e4
LT
3640 return res;
3641}
3642
faf5f49c 3643struct nfs4_unlockdata {
911d1aaf
TM
3644 struct nfs_locku_args arg;
3645 struct nfs_locku_res res;
faf5f49c
TM
3646 struct nfs4_lock_state *lsp;
3647 struct nfs_open_context *ctx;
911d1aaf
TM
3648 struct file_lock fl;
3649 const struct nfs_server *server;
26e976a8 3650 unsigned long timestamp;
faf5f49c
TM
3651};
3652
911d1aaf
TM
3653static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3654 struct nfs_open_context *ctx,
3655 struct nfs4_lock_state *lsp,
3656 struct nfs_seqid *seqid)
3657{
3658 struct nfs4_unlockdata *p;
3659 struct inode *inode = lsp->ls_state->inode;
3660
a893693c 3661 p = kzalloc(sizeof(*p), GFP_KERNEL);
911d1aaf
TM
3662 if (p == NULL)
3663 return NULL;
3664 p->arg.fh = NFS_FH(inode);
3665 p->arg.fl = &p->fl;
3666 p->arg.seqid = seqid;
c1d51931 3667 p->res.seqid = seqid;
5f7dbd5c 3668 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
911d1aaf
TM
3669 p->arg.stateid = &lsp->ls_stateid;
3670 p->lsp = lsp;
3671 atomic_inc(&lsp->ls_count);
3672 /* Ensure we don't close file until we're done freeing locks! */
3673 p->ctx = get_nfs_open_context(ctx);
3674 memcpy(&p->fl, fl, sizeof(p->fl));
3675 p->server = NFS_SERVER(inode);
3676 return p;
3677}
3678
06f814a3 3679static void nfs4_locku_release_calldata(void *data)
faf5f49c 3680{
963d8fe5 3681 struct nfs4_unlockdata *calldata = data;
911d1aaf 3682 nfs_free_seqid(calldata->arg.seqid);
06f814a3
TM
3683 nfs4_put_lock_state(calldata->lsp);
3684 put_nfs_open_context(calldata->ctx);
3685 kfree(calldata);
faf5f49c
TM
3686}
3687
963d8fe5 3688static void nfs4_locku_done(struct rpc_task *task, void *data)
faf5f49c 3689{
963d8fe5 3690 struct nfs4_unlockdata *calldata = data;
faf5f49c 3691
a893693c
AA
3692 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3693 task->tk_status);
06f814a3
TM
3694 if (RPC_ASSASSINATED(task))
3695 return;
faf5f49c
TM
3696 switch (task->tk_status) {
3697 case 0:
3698 memcpy(calldata->lsp->ls_stateid.data,
911d1aaf 3699 calldata->res.stateid.data,
faf5f49c 3700 sizeof(calldata->lsp->ls_stateid.data));
26e976a8 3701 renew_lease(calldata->server, calldata->timestamp);
faf5f49c 3702 break;
9e33bed5
TM
3703 case -NFS4ERR_BAD_STATEID:
3704 case -NFS4ERR_OLD_STATEID:
faf5f49c
TM
3705 case -NFS4ERR_STALE_STATEID:
3706 case -NFS4ERR_EXPIRED:
faf5f49c
TM
3707 break;
3708 default:
9e33bed5 3709 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
eedc020e
AA
3710 nfs4_restart_rpc(task,
3711 calldata->server->nfs_client);
faf5f49c 3712 }
a893693c
AA
3713 nfs4_sequence_free_slot(calldata->server->nfs_client,
3714 &calldata->res.seq_res);
faf5f49c
TM
3715}
3716
4ce70ada 3717static void nfs4_locku_prepare(struct rpc_task *task, void *data)
faf5f49c 3718{
4ce70ada 3719 struct nfs4_unlockdata *calldata = data;
faf5f49c 3720
911d1aaf 3721 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
faf5f49c
TM
3722 return;
3723 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
963d8fe5
TM
3724 /* Note: exit _without_ running nfs4_locku_done */
3725 task->tk_action = NULL;
faf5f49c
TM
3726 return;
3727 }
26e976a8 3728 calldata->timestamp = jiffies;
a893693c
AA
3729 if (nfs4_setup_sequence(calldata->server->nfs_client,
3730 &calldata->arg.seq_args,
3731 &calldata->res.seq_res, 1, task))
3732 return;
5138fde0 3733 rpc_call_start(task);
faf5f49c
TM
3734}
3735
963d8fe5 3736static const struct rpc_call_ops nfs4_locku_ops = {
4ce70ada 3737 .rpc_call_prepare = nfs4_locku_prepare,
963d8fe5 3738 .rpc_call_done = nfs4_locku_done,
06f814a3 3739 .rpc_release = nfs4_locku_release_calldata,
963d8fe5
TM
3740};
3741
a5d16a4d
TM
3742static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3743 struct nfs_open_context *ctx,
3744 struct nfs4_lock_state *lsp,
3745 struct nfs_seqid *seqid)
3746{
3747 struct nfs4_unlockdata *data;
5138fde0
TM
3748 struct rpc_message msg = {
3749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3750 .rpc_cred = ctx->cred,
3751 };
c970aa85
TM
3752 struct rpc_task_setup task_setup_data = {
3753 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5138fde0 3754 .rpc_message = &msg,
c970aa85 3755 .callback_ops = &nfs4_locku_ops,
101070ca 3756 .workqueue = nfsiod_workqueue,
c970aa85
TM
3757 .flags = RPC_TASK_ASYNC,
3758 };
a5d16a4d 3759
137d6aca
FF
3760 /* Ensure this is an unlock - when canceling a lock, the
3761 * canceled lock is passed in, and it won't be an unlock.
3762 */
3763 fl->fl_type = F_UNLCK;
3764
a5d16a4d
TM
3765 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3766 if (data == NULL) {
3767 nfs_free_seqid(seqid);
3768 return ERR_PTR(-ENOMEM);
3769 }
3770
5138fde0
TM
3771 msg.rpc_argp = &data->arg,
3772 msg.rpc_resp = &data->res,
c970aa85
TM
3773 task_setup_data.callback_data = data;
3774 return rpc_run_task(&task_setup_data);
a5d16a4d
TM
3775}
3776
faf5f49c
TM
3777static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3778{
19e03c57 3779 struct nfs_inode *nfsi = NFS_I(state->inode);
911d1aaf 3780 struct nfs_seqid *seqid;
1da177e4 3781 struct nfs4_lock_state *lsp;
06f814a3
TM
3782 struct rpc_task *task;
3783 int status = 0;
536ff0f8 3784 unsigned char fl_flags = request->fl_flags;
faf5f49c 3785
8d0a8a9d 3786 status = nfs4_set_lock_state(state, request);
9b073574
TM
3787 /* Unlock _before_ we do the RPC call */
3788 request->fl_flags |= FL_EXISTS;
19e03c57
TM
3789 down_read(&nfsi->rwsem);
3790 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3791 up_read(&nfsi->rwsem);
9b073574 3792 goto out;
19e03c57
TM
3793 }
3794 up_read(&nfsi->rwsem);
8d0a8a9d 3795 if (status != 0)
9b073574
TM
3796 goto out;
3797 /* Is this a delegated lock? */
3798 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3799 goto out;
8d0a8a9d 3800 lsp = request->fl_u.nfs4_fl.owner;
911d1aaf 3801 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
9b073574 3802 status = -ENOMEM;
911d1aaf 3803 if (seqid == NULL)
9b073574 3804 goto out;
cd3758e3 3805 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
a5d16a4d
TM
3806 status = PTR_ERR(task);
3807 if (IS_ERR(task))
9b073574 3808 goto out;
a5d16a4d 3809 status = nfs4_wait_for_completion_rpc_task(task);
e6b3c4db 3810 rpc_put_task(task);
9b073574 3811out:
536ff0f8 3812 request->fl_flags = fl_flags;
1da177e4
LT
3813 return status;
3814}
3815
a5d16a4d
TM
3816struct nfs4_lockdata {
3817 struct nfs_lock_args arg;
3818 struct nfs_lock_res res;
3819 struct nfs4_lock_state *lsp;
3820 struct nfs_open_context *ctx;
3821 struct file_lock fl;
26e976a8 3822 unsigned long timestamp;
a5d16a4d
TM
3823 int rpc_status;
3824 int cancelled;
66179efe 3825 struct nfs_server *server;
a5d16a4d
TM
3826};
3827
3828static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3829 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
1da177e4 3830{
a5d16a4d
TM
3831 struct nfs4_lockdata *p;
3832 struct inode *inode = lsp->ls_state->inode;
1da177e4 3833 struct nfs_server *server = NFS_SERVER(inode);
a5d16a4d
TM
3834
3835 p = kzalloc(sizeof(*p), GFP_KERNEL);
3836 if (p == NULL)
3837 return NULL;
3838
3839 p->arg.fh = NFS_FH(inode);
3840 p->arg.fl = &p->fl;
2f74c0a0
TM
3841 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3842 if (p->arg.open_seqid == NULL)
3843 goto out_free;
a5d16a4d
TM
3844 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3845 if (p->arg.lock_seqid == NULL)
2f74c0a0 3846 goto out_free_seqid;
a5d16a4d 3847 p->arg.lock_stateid = &lsp->ls_stateid;
7539bbab 3848 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
9f958ab8 3849 p->arg.lock_owner.id = lsp->ls_id.id;
c1d51931 3850 p->res.lock_seqid = p->arg.lock_seqid;
5f7dbd5c 3851 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
a5d16a4d 3852 p->lsp = lsp;
66179efe 3853 p->server = server;
a5d16a4d
TM
3854 atomic_inc(&lsp->ls_count);
3855 p->ctx = get_nfs_open_context(ctx);
3856 memcpy(&p->fl, fl, sizeof(p->fl));
3857 return p;
2f74c0a0
TM
3858out_free_seqid:
3859 nfs_free_seqid(p->arg.open_seqid);
a5d16a4d
TM
3860out_free:
3861 kfree(p);
3862 return NULL;
3863}
3864
3865static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3866{
3867 struct nfs4_lockdata *data = calldata;
3868 struct nfs4_state *state = data->lsp->ls_state;
06735b34 3869
3110ff80 3870 dprintk("%s: begin!\n", __func__);
2f74c0a0
TM
3871 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3872 return;
a5d16a4d
TM
3873 /* Do we need to do an open_to_lock_owner? */
3874 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
e6e21970
TM
3875 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3876 return;
a5d16a4d
TM
3877 data->arg.open_stateid = &state->stateid;
3878 data->arg.new_lock_owner = 1;
c1d51931 3879 data->res.open_seqid = data->arg.open_seqid;
2f74c0a0
TM
3880 } else
3881 data->arg.new_lock_owner = 0;
26e976a8 3882 data->timestamp = jiffies;
66179efe
AA
3883 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3884 &data->res.seq_res, 1, task))
3885 return;
5138fde0 3886 rpc_call_start(task);
3110ff80 3887 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
a5d16a4d
TM
3888}
3889
3890static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3891{
3892 struct nfs4_lockdata *data = calldata;
3893
3110ff80 3894 dprintk("%s: begin!\n", __func__);
a5d16a4d 3895
66179efe
AA
3896 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3897 task->tk_status);
3898
a5d16a4d
TM
3899 data->rpc_status = task->tk_status;
3900 if (RPC_ASSASSINATED(task))
3901 goto out;
3902 if (data->arg.new_lock_owner != 0) {
a5d16a4d
TM
3903 if (data->rpc_status == 0)
3904 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3905 else
3906 goto out;
3907 }
3908 if (data->rpc_status == 0) {
3909 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3910 sizeof(data->lsp->ls_stateid.data));
3911 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
88be9f99 3912 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
a5d16a4d 3913 }
a5d16a4d 3914out:
3110ff80 3915 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
a5d16a4d
TM
3916}
3917
3918static void nfs4_lock_release(void *calldata)
3919{
3920 struct nfs4_lockdata *data = calldata;
3921
3110ff80 3922 dprintk("%s: begin!\n", __func__);
2f74c0a0 3923 nfs_free_seqid(data->arg.open_seqid);
a5d16a4d
TM
3924 if (data->cancelled != 0) {
3925 struct rpc_task *task;
3926 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3927 data->arg.lock_seqid);
3928 if (!IS_ERR(task))
e6b3c4db 3929 rpc_put_task(task);
3110ff80 3930 dprintk("%s: cancelling lock!\n", __func__);
a5d16a4d
TM
3931 } else
3932 nfs_free_seqid(data->arg.lock_seqid);
3933 nfs4_put_lock_state(data->lsp);
3934 put_nfs_open_context(data->ctx);
3935 kfree(data);
3110ff80 3936 dprintk("%s: done!\n", __func__);
a5d16a4d
TM
3937}
3938
3939static const struct rpc_call_ops nfs4_lock_ops = {
3940 .rpc_call_prepare = nfs4_lock_prepare,
3941 .rpc_call_done = nfs4_lock_done,
3942 .rpc_release = nfs4_lock_release,
3943};
3944
3945static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3946{
3947 struct nfs4_lockdata *data;
3948 struct rpc_task *task;
5138fde0
TM
3949 struct rpc_message msg = {
3950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3951 .rpc_cred = state->owner->so_cred,
3952 };
c970aa85
TM
3953 struct rpc_task_setup task_setup_data = {
3954 .rpc_client = NFS_CLIENT(state->inode),
5138fde0 3955 .rpc_message = &msg,
c970aa85 3956 .callback_ops = &nfs4_lock_ops,
101070ca 3957 .workqueue = nfsiod_workqueue,
c970aa85
TM
3958 .flags = RPC_TASK_ASYNC,
3959 };
a5d16a4d
TM
3960 int ret;
3961
3110ff80 3962 dprintk("%s: begin!\n", __func__);
cd3758e3 3963 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
a5d16a4d
TM
3964 fl->fl_u.nfs4_fl.owner);
3965 if (data == NULL)
3966 return -ENOMEM;
3967 if (IS_SETLKW(cmd))
3968 data->arg.block = 1;
3969 if (reclaim != 0)
3970 data->arg.reclaim = 1;
5138fde0
TM
3971 msg.rpc_argp = &data->arg,
3972 msg.rpc_resp = &data->res,
c970aa85
TM
3973 task_setup_data.callback_data = data;
3974 task = rpc_run_task(&task_setup_data);
7a1218a2 3975 if (IS_ERR(task))
a5d16a4d 3976 return PTR_ERR(task);
a5d16a4d
TM
3977 ret = nfs4_wait_for_completion_rpc_task(task);
3978 if (ret == 0) {
3979 ret = data->rpc_status;
3980 if (ret == -NFS4ERR_DENIED)
3981 ret = -EAGAIN;
3982 } else
3983 data->cancelled = 1;
e6b3c4db 3984 rpc_put_task(task);
3110ff80 3985 dprintk("%s: done, ret = %d!\n", __func__, ret);
a5d16a4d 3986 return ret;
1da177e4
LT
3987}
3988
3989static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3990{
202b50dc
TM
3991 struct nfs_server *server = NFS_SERVER(state->inode);
3992 struct nfs4_exception exception = { };
3993 int err;
3994
3995 do {
42a2d13e
TM
3996 /* Cache the lock if possible... */
3997 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3998 return 0;
202b50dc
TM
3999 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4000 if (err != -NFS4ERR_DELAY)
4001 break;
4002 nfs4_handle_exception(server, err, &exception);
4003 } while (exception.retry);
4004 return err;
1da177e4
LT
4005}
4006
4007static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4008{
202b50dc
TM
4009 struct nfs_server *server = NFS_SERVER(state->inode);
4010 struct nfs4_exception exception = { };
4011 int err;
4012
6bfc93ef
TM
4013 err = nfs4_set_lock_state(state, request);
4014 if (err != 0)
4015 return err;
202b50dc 4016 do {
42a2d13e
TM
4017 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4018 return 0;
202b50dc
TM
4019 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4020 if (err != -NFS4ERR_DELAY)
4021 break;
4022 nfs4_handle_exception(server, err, &exception);
4023 } while (exception.retry);
4024 return err;
1da177e4
LT
4025}
4026
4027static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4028{
19e03c57 4029 struct nfs_inode *nfsi = NFS_I(state->inode);
01c3b861 4030 unsigned char fl_flags = request->fl_flags;
1da177e4
LT
4031 int status;
4032
6bfc93ef 4033 /* Is this a delegated open? */
6bfc93ef
TM
4034 status = nfs4_set_lock_state(state, request);
4035 if (status != 0)
4036 goto out;
01c3b861
TM
4037 request->fl_flags |= FL_ACCESS;
4038 status = do_vfs_lock(request->fl_file, request);
4039 if (status < 0)
4040 goto out;
19e03c57 4041 down_read(&nfsi->rwsem);
01c3b861 4042 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
01c3b861 4043 /* Yes: cache locks! */
01c3b861 4044 /* ...but avoid races with delegation recall... */
19e03c57
TM
4045 request->fl_flags = fl_flags & ~FL_SLEEP;
4046 status = do_vfs_lock(request->fl_file, request);
4047 goto out_unlock;
01c3b861 4048 }
6bfc93ef
TM
4049 status = _nfs4_do_setlk(state, cmd, request, 0);
4050 if (status != 0)
01c3b861 4051 goto out_unlock;
6bfc93ef 4052 /* Note: we always want to sleep here! */
01c3b861 4053 request->fl_flags = fl_flags | FL_SLEEP;
6bfc93ef 4054 if (do_vfs_lock(request->fl_file, request) < 0)
3110ff80 4055 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
01c3b861 4056out_unlock:
19e03c57 4057 up_read(&nfsi->rwsem);
01c3b861
TM
4058out:
4059 request->fl_flags = fl_flags;
1da177e4
LT
4060 return status;
4061}
4062
4063static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4064{
4065 struct nfs4_exception exception = { };
4066 int err;
4067
4068 do {
4069 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4070 _nfs4_proc_setlk(state, cmd, request),
4071 &exception);
4072 } while (exception.retry);
4073 return err;
4074}
4075
4076static int
4077nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4078{
4079 struct nfs_open_context *ctx;
4080 struct nfs4_state *state;
4081 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4082 int status;
4083
4084 /* verify open state */
cd3758e3 4085 ctx = nfs_file_open_context(filp);
1da177e4
LT
4086 state = ctx->state;
4087
4088 if (request->fl_start < 0 || request->fl_end < 0)
4089 return -EINVAL;
4090
4091 if (IS_GETLK(cmd))
4092 return nfs4_proc_getlk(state, F_GETLK, request);
4093
4094 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4095 return -EINVAL;
4096
4097 if (request->fl_type == F_UNLCK)
4098 return nfs4_proc_unlck(state, cmd, request);
4099
4100 do {
4101 status = nfs4_proc_setlk(state, cmd, request);
4102 if ((status != -EAGAIN) || IS_SETLK(cmd))
4103 break;
4104 timeout = nfs4_set_lock_task_retry(timeout);
4105 status = -ERESTARTSYS;
4106 if (signalled())
4107 break;
4108 } while(status < 0);
1da177e4
LT
4109 return status;
4110}
4111
888e694c
TM
4112int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4113{
4114 struct nfs_server *server = NFS_SERVER(state->inode);
4115 struct nfs4_exception exception = { };
4116 int err;
4117
4118 err = nfs4_set_lock_state(state, fl);
4119 if (err != 0)
4120 goto out;
4121 do {
4122 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4123 if (err != -NFS4ERR_DELAY)
4124 break;
4125 err = nfs4_handle_exception(server, err, &exception);
4126 } while (exception.retry);
4127out:
4128 return err;
4129}
6b3b5496 4130
aa1870af
BF
4131#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4132
6b3b5496
BF
4133int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4134 size_t buflen, int flags)
4135{
4b580ee3
BF
4136 struct inode *inode = dentry->d_inode;
4137
4138 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4139 return -EOPNOTSUPP;
4140
4b580ee3 4141 return nfs4_proc_set_acl(inode, buf, buflen);
6b3b5496
BF
4142}
4143
4144/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4145 * and that's what we'll do for e.g. user attributes that haven't been set.
4146 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4147 * attributes in kernel-managed attribute namespaces. */
4148ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4149 size_t buflen)
4150{
aa1870af
BF
4151 struct inode *inode = dentry->d_inode;
4152
4153 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4154 return -EOPNOTSUPP;
4155
4156 return nfs4_proc_get_acl(inode, buf, buflen);
6b3b5496
BF
4157}
4158
4159ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4160{
aa1870af 4161 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
6b3b5496 4162
096455a2
BF
4163 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4164 return 0;
6b3b5496
BF
4165 if (buf && buflen < len)
4166 return -ERANGE;
4167 if (buf)
aa1870af
BF
4168 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4169 return len;
6b3b5496
BF
4170}
4171
69aaaae1
TM
4172static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4173{
4174 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4175 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4176 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4177 return;
4178
4179 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4180 NFS_ATTR_FATTR_NLINK;
4181 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4182 fattr->nlink = 2;
4183}
4184
56659e99 4185int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
7aaa0b3b 4186 struct nfs4_fs_locations *fs_locations, struct page *page)
683b57b4
TM
4187{
4188 struct nfs_server *server = NFS_SERVER(dir);
4189 u32 bitmask[2] = {
361e624f
MN
4190 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4191 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
683b57b4
TM
4192 };
4193 struct nfs4_fs_locations_arg args = {
4194 .dir_fh = NFS_FH(dir),
c228fd3a 4195 .name = name,
683b57b4
TM
4196 .page = page,
4197 .bitmask = bitmask,
4198 };
22958463
BH
4199 struct nfs4_fs_locations_res res = {
4200 .fs_locations = fs_locations,
4201 };
683b57b4
TM
4202 struct rpc_message msg = {
4203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4204 .rpc_argp = &args,
22958463 4205 .rpc_resp = &res,
683b57b4
TM
4206 };
4207 int status;
4208
3110ff80 4209 dprintk("%s: start\n", __func__);
c228fd3a 4210 nfs_fattr_init(&fs_locations->fattr);
683b57b4 4211 fs_locations->server = server;
830b8e33 4212 fs_locations->nlocations = 0;
cccef3b9 4213 status = nfs4_call_sync(server, &msg, &args, &res, 0);
69aaaae1 4214 nfs_fixup_referral_attributes(&fs_locations->fattr);
3110ff80 4215 dprintk("%s: returned status = %d\n", __func__, status);
683b57b4
TM
4216 return status;
4217}
4218
557134a3 4219#ifdef CONFIG_NFS_V4_1
99fe60d0
BH
4220/*
4221 * nfs4_proc_exchange_id()
4222 *
4223 * Since the clientid has expired, all compounds using sessions
4224 * associated with the stale clientid will be returning
4225 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4226 * be in some phase of session reset.
4227 */
4228static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4229{
4230 nfs4_verifier verifier;
4231 struct nfs41_exchange_id_args args = {
4232 .client = clp,
4233 .flags = clp->cl_exchange_flags,
4234 };
4235 struct nfs41_exchange_id_res res = {
4236 .client = clp,
4237 };
4238 int status;
4239 struct rpc_message msg = {
4240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4241 .rpc_argp = &args,
4242 .rpc_resp = &res,
4243 .rpc_cred = cred,
4244 };
4245 __be32 *p;
4246
4247 dprintk("--> %s\n", __func__);
4248 BUG_ON(clp == NULL);
a7b72103 4249
99fe60d0
BH
4250 p = (u32 *)verifier.data;
4251 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4252 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4253 args.verifier = &verifier;
4254
4255 while (1) {
4256 args.id_len = scnprintf(args.id, sizeof(args.id),
4257 "%s/%s %u",
4258 clp->cl_ipaddr,
4259 rpc_peeraddr2str(clp->cl_rpcclient,
4260 RPC_DISPLAY_ADDR),
4261 clp->cl_id_uniquifier);
4262
4263 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4264
4265 if (status != NFS4ERR_CLID_INUSE)
4266 break;
4267
4268 if (signalled())
4269 break;
4270
4271 if (++clp->cl_id_uniquifier == 0)
4272 break;
4273 }
4274
4275 dprintk("<-- %s status= %d\n", __func__, status);
4276 return status;
4277}
4278
2050f0cc
AA
4279struct nfs4_get_lease_time_data {
4280 struct nfs4_get_lease_time_args *args;
4281 struct nfs4_get_lease_time_res *res;
4282 struct nfs_client *clp;
4283};
4284
4285static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4286 void *calldata)
4287{
4288 int ret;
4289 struct nfs4_get_lease_time_data *data =
4290 (struct nfs4_get_lease_time_data *)calldata;
4291
4292 dprintk("--> %s\n", __func__);
4293 /* just setup sequence, do not trigger session recovery
4294 since we're invoked within one */
4295 ret = nfs41_setup_sequence(data->clp->cl_session,
4296 &data->args->la_seq_args,
4297 &data->res->lr_seq_res, 0, task);
4298
4299 BUG_ON(ret == -EAGAIN);
4300 rpc_call_start(task);
4301 dprintk("<-- %s\n", __func__);
4302}
4303
4304/*
4305 * Called from nfs4_state_manager thread for session setup, so don't recover
4306 * from sequence operation or clientid errors.
4307 */
4308static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4309{
4310 struct nfs4_get_lease_time_data *data =
4311 (struct nfs4_get_lease_time_data *)calldata;
4312
4313 dprintk("--> %s\n", __func__);
4314 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4315 switch (task->tk_status) {
4316 case -NFS4ERR_DELAY:
4317 case -NFS4ERR_GRACE:
4318 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4319 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4320 task->tk_status = 0;
eedc020e 4321 nfs4_restart_rpc(task, data->clp);
2050f0cc
AA
4322 return;
4323 }
4324 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4325 dprintk("<-- %s\n", __func__);
4326}
4327
4328struct rpc_call_ops nfs4_get_lease_time_ops = {
4329 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4330 .rpc_call_done = nfs4_get_lease_time_done,
4331};
4332
4333int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4334{
4335 struct rpc_task *task;
4336 struct nfs4_get_lease_time_args args;
4337 struct nfs4_get_lease_time_res res = {
4338 .lr_fsinfo = fsinfo,
4339 };
4340 struct nfs4_get_lease_time_data data = {
4341 .args = &args,
4342 .res = &res,
4343 .clp = clp,
4344 };
4345 struct rpc_message msg = {
4346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4347 .rpc_argp = &args,
4348 .rpc_resp = &res,
4349 };
4350 struct rpc_task_setup task_setup = {
4351 .rpc_client = clp->cl_rpcclient,
4352 .rpc_message = &msg,
4353 .callback_ops = &nfs4_get_lease_time_ops,
4354 .callback_data = &data
4355 };
4356 int status;
4357
4358 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4359 dprintk("--> %s\n", __func__);
4360 task = rpc_run_task(&task_setup);
4361
4362 if (IS_ERR(task))
4363 status = PTR_ERR(task);
4364 else {
4365 status = task->tk_status;
4366 rpc_put_task(task);
4367 }
4368 dprintk("<-- %s return %d\n", __func__, status);
4369
4370 return status;
4371}
4372
b73dafa7
RL
4373/*
4374 * Reset a slot table
4375 */
4376static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4377 int old_max_slots, int ivalue)
ac72b7b3 4378{
b73dafa7 4379 int i;
ac72b7b3
AA
4380 int ret = 0;
4381
b73dafa7 4382 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
ac72b7b3 4383
b73dafa7
RL
4384 /*
4385 * Until we have dynamic slot table adjustment, insist
4386 * upon the same slot table size
4387 */
ac72b7b3
AA
4388 if (max_slots != old_max_slots) {
4389 dprintk("%s reset slot table does't match old\n",
4390 __func__);
4391 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4392 goto out;
4393 }
4394 spin_lock(&tbl->slot_tbl_lock);
4395 for (i = 0; i < max_slots; ++i)
b73dafa7 4396 tbl->slots[i].seq_nr = ivalue;
ac72b7b3
AA
4397 tbl->highest_used_slotid = -1;
4398 spin_unlock(&tbl->slot_tbl_lock);
4399 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4400 tbl, tbl->slots, tbl->max_slots);
4401out:
4402 dprintk("<-- %s: return %d\n", __func__, ret);
4403 return ret;
4404}
4405
b73dafa7
RL
4406/*
4407 * Reset the forechannel and backchannel slot tables
4408 */
4409static int nfs4_reset_slot_tables(struct nfs4_session *session)
4410{
4411 int status;
4412
4413 status = nfs4_reset_slot_table(&session->fc_slot_table,
4414 session->fc_attrs.max_reqs,
4415 session->fc_slot_table.max_slots,
4416 1);
4417 return status;
4418}
4419
fc931582
AA
4420/*
4421 * Initialize slot table
4422 */
4423static int nfs4_init_slot_table(struct nfs4_session *session)
4424{
4425 struct nfs4_slot_table *tbl = &session->fc_slot_table;
4426 int i, max_slots = session->fc_attrs.max_reqs;
4427 struct nfs4_slot *slot;
4428 int ret = -ENOMEM;
4429
4430 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4431
4432 dprintk("--> %s: max_reqs=%u\n", __func__,
4433 session->fc_attrs.max_reqs);
4434
4435 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4436 if (!slot)
4437 goto out;
4438 for (i = 0; i < max_slots; ++i)
4439 slot[i].seq_nr = 1;
4440 ret = 0;
4441
4442 spin_lock(&tbl->slot_tbl_lock);
4443 if (tbl->slots != NULL) {
4444 spin_unlock(&tbl->slot_tbl_lock);
4445 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4446 __func__, tbl, tbl->slots);
4447 WARN_ON(1);
4448 goto out_free;
4449 }
4450 tbl->max_slots = max_slots;
4451 tbl->slots = slot;
4452 tbl->highest_used_slotid = -1; /* no slot is currently used */
4453 spin_unlock(&tbl->slot_tbl_lock);
4454 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4455 tbl, tbl->slots, tbl->max_slots);
4456out:
4457 dprintk("<-- %s: return %d\n", __func__, ret);
4458 return ret;
4459out_free:
4460 kfree(slot);
4461 goto out;
4462}
4463
557134a3
AA
4464/* Destroy the slot table */
4465static void nfs4_destroy_slot_table(struct nfs4_session *session)
4466{
4467 if (session->fc_slot_table.slots == NULL)
4468 return;
4469 kfree(session->fc_slot_table.slots);
4470 session->fc_slot_table.slots = NULL;
4471 return;
4472}
4473
4474struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4475{
4476 struct nfs4_session *session;
4477 struct nfs4_slot_table *tbl;
4478
4479 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4480 if (!session)
4481 return NULL;
76db6d95
AA
4482
4483 set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4484 /*
4485 * The create session reply races with the server back
4486 * channel probe. Mark the client NFS_CS_SESSION_INITING
4487 * so that the client back channel can find the
4488 * nfs_client struct
4489 */
4490 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4491
557134a3
AA
4492 tbl = &session->fc_slot_table;
4493 spin_lock_init(&tbl->slot_tbl_lock);
4494 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4495 session->clp = clp;
4496 return session;
4497}
4498
4499void nfs4_destroy_session(struct nfs4_session *session)
4500{
5a0ffe54
AA
4501 nfs4_proc_destroy_session(session);
4502 dprintk("%s Destroy backchannel for xprt %p\n",
4503 __func__, session->clp->cl_rpcclient->cl_xprt);
4504 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4505 NFS41_BC_MIN_CALLBACKS);
557134a3
AA
4506 nfs4_destroy_slot_table(session);
4507 kfree(session);
4508}
4509
fc931582
AA
4510/*
4511 * Initialize the values to be used by the client in CREATE_SESSION
4512 * If nfs4_init_session set the fore channel request and response sizes,
4513 * use them.
4514 *
4515 * Set the back channel max_resp_sz_cached to zero to force the client to
4516 * always set csa_cachethis to FALSE because the current implementation
4517 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4518 */
4519static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4520{
4521 struct nfs4_session *session = args->client->cl_session;
4522 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4523 mxresp_sz = session->fc_attrs.max_resp_sz;
4524
4525 if (mxrqst_sz == 0)
4526 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4527 if (mxresp_sz == 0)
4528 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4529 /* Fore channel attributes */
4530 args->fc_attrs.headerpadsz = 0;
4531 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4532 args->fc_attrs.max_resp_sz = mxresp_sz;
4533 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4534 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4535 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4536
4537 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4538 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4539 __func__,
4540 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4541 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4542 args->fc_attrs.max_reqs);
4543
4544 /* Back channel attributes */
4545 args->bc_attrs.headerpadsz = 0;
4546 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4547 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4548 args->bc_attrs.max_resp_sz_cached = 0;
4549 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4550 args->bc_attrs.max_reqs = 1;
4551
4552 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4553 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4554 __func__,
4555 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4556 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4557 args->bc_attrs.max_reqs);
4558}
4559
8d35301d
AA
4560static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4561{
4562 if (rcvd <= sent)
4563 return 0;
4564 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4565 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4566 return -EINVAL;
4567}
4568
4569#define _verify_fore_channel_attr(_name_) \
4570 _verify_channel_attr("fore", #_name_, \
4571 args->fc_attrs._name_, \
4572 session->fc_attrs._name_)
4573
4574#define _verify_back_channel_attr(_name_) \
4575 _verify_channel_attr("back", #_name_, \
4576 args->bc_attrs._name_, \
4577 session->bc_attrs._name_)
4578
4579/*
4580 * The server is not allowed to increase the fore channel header pad size,
4581 * maximum response size, or maximum number of operations.
4582 *
4583 * The back channel attributes are only negotiatied down: We send what the
4584 * (back channel) server insists upon.
4585 */
4586static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4587 struct nfs4_session *session)
4588{
4589 int ret = 0;
4590
4591 ret |= _verify_fore_channel_attr(headerpadsz);
4592 ret |= _verify_fore_channel_attr(max_resp_sz);
4593 ret |= _verify_fore_channel_attr(max_ops);
4594
4595 ret |= _verify_back_channel_attr(headerpadsz);
4596 ret |= _verify_back_channel_attr(max_rqst_sz);
4597 ret |= _verify_back_channel_attr(max_resp_sz);
4598 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4599 ret |= _verify_back_channel_attr(max_ops);
4600 ret |= _verify_back_channel_attr(max_reqs);
4601
4602 return ret;
4603}
4604
fc931582
AA
4605static int _nfs4_proc_create_session(struct nfs_client *clp)
4606{
4607 struct nfs4_session *session = clp->cl_session;
4608 struct nfs41_create_session_args args = {
4609 .client = clp,
4610 .cb_program = NFS4_CALLBACK,
4611 };
4612 struct nfs41_create_session_res res = {
4613 .client = clp,
4614 };
4615 struct rpc_message msg = {
4616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4617 .rpc_argp = &args,
4618 .rpc_resp = &res,
4619 };
4620 int status;
4621
4622 nfs4_init_channel_attrs(&args);
0f91421e 4623 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
fc931582
AA
4624
4625 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4626
8d35301d
AA
4627 if (!status)
4628 /* Verify the session's negotiated channel_attrs values */
4629 status = nfs4_verify_channel_attrs(&args, session);
fc931582
AA
4630 if (!status) {
4631 /* Increment the clientid slot sequence id */
4632 clp->cl_seqid++;
4633 }
4634
4635 return status;
4636}
4637
4638/*
4639 * Issues a CREATE_SESSION operation to the server.
4640 * It is the responsibility of the caller to verify the session is
4641 * expired before calling this routine.
4642 */
ac72b7b3 4643int nfs4_proc_create_session(struct nfs_client *clp, int reset)
fc931582
AA
4644{
4645 int status;
4646 unsigned *ptr;
4647 struct nfs_fsinfo fsinfo;
4648 struct nfs4_session *session = clp->cl_session;
4649
4650 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4651
4652 status = _nfs4_proc_create_session(clp);
4653 if (status)
4654 goto out;
4655
ac72b7b3
AA
4656 /* Init or reset the fore channel */
4657 if (reset)
b73dafa7 4658 status = nfs4_reset_slot_tables(session);
ac72b7b3
AA
4659 else
4660 status = nfs4_init_slot_table(session);
fc931582
AA
4661 dprintk("fore channel slot table initialization returned %d\n", status);
4662 if (status)
4663 goto out;
4664
4665 ptr = (unsigned *)&session->sess_id.data[0];
4666 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4667 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4668
ac72b7b3
AA
4669 if (reset)
4670 /* Lease time is aleady set */
4671 goto out;
4672
fc931582
AA
4673 /* Get the lease time */
4674 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4675 if (status == 0) {
4676 /* Update lease time and schedule renewal */
4677 spin_lock(&clp->cl_lock);
4678 clp->cl_lease_time = fsinfo.lease_time * HZ;
4679 clp->cl_last_renewal = jiffies;
4680 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4681 spin_unlock(&clp->cl_lock);
4682
4683 nfs4_schedule_state_renewal(clp);
4684 }
4685out:
4686 dprintk("<-- %s\n", __func__);
4687 return status;
4688}
4689
0f3e66c6
AA
4690/*
4691 * Issue the over-the-wire RPC DESTROY_SESSION.
4692 * The caller must serialize access to this routine.
4693 */
4694int nfs4_proc_destroy_session(struct nfs4_session *session)
4695{
4696 int status = 0;
4697 struct rpc_message msg;
4698
4699 dprintk("--> nfs4_proc_destroy_session\n");
4700
4701 /* session is still being setup */
4702 if (session->clp->cl_cons_state != NFS_CS_READY)
4703 return status;
4704
4705 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4706 msg.rpc_argp = session;
4707 msg.rpc_resp = NULL;
4708 msg.rpc_cred = NULL;
4709 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4710
4711 if (status)
4712 printk(KERN_WARNING
4713 "Got error %d from the server on DESTROY_SESSION. "
4714 "Session has been destroyed regardless...\n", status);
4715
4716 dprintk("<-- nfs4_proc_destroy_session\n");
4717 return status;
4718}
4719
fc01cea9
AA
4720/*
4721 * Renew the cl_session lease.
4722 */
4723static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4724{
4725 struct nfs4_sequence_args args;
4726 struct nfs4_sequence_res res;
4727
4728 struct rpc_message msg = {
4729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4730 .rpc_argp = &args,
4731 .rpc_resp = &res,
4732 .rpc_cred = cred,
4733 };
4734
4735 args.sa_cache_this = 0;
4736
4737 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4738 &res, 0);
4739}
4740
4741void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4742{
4743 struct nfs_client *clp = (struct nfs_client *)data;
4744
4745 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4746
4747 if (task->tk_status < 0) {
4748 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4749
4750 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4751 == -EAGAIN) {
eedc020e 4752 nfs4_restart_rpc(task, clp);
fc01cea9
AA
4753 return;
4754 }
4755 }
4756 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4757 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4758
4759 put_rpccred(task->tk_msg.rpc_cred);
4760 kfree(task->tk_msg.rpc_argp);
4761 kfree(task->tk_msg.rpc_resp);
4762
4763 dprintk("<-- %s\n", __func__);
4764}
4765
4766static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4767{
4768 struct nfs_client *clp;
4769 struct nfs4_sequence_args *args;
4770 struct nfs4_sequence_res *res;
4771
4772 clp = (struct nfs_client *)data;
4773 args = task->tk_msg.rpc_argp;
4774 res = task->tk_msg.rpc_resp;
4775
4776 if (nfs4_setup_sequence(clp, args, res, 0, task))
4777 return;
4778 rpc_call_start(task);
4779}
4780
4781static const struct rpc_call_ops nfs41_sequence_ops = {
4782 .rpc_call_done = nfs41_sequence_call_done,
4783 .rpc_call_prepare = nfs41_sequence_prepare,
4784};
4785
4786static int nfs41_proc_async_sequence(struct nfs_client *clp,
4787 struct rpc_cred *cred)
4788{
4789 struct nfs4_sequence_args *args;
4790 struct nfs4_sequence_res *res;
4791 struct rpc_message msg = {
4792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4793 .rpc_cred = cred,
4794 };
4795
4796 args = kzalloc(sizeof(*args), GFP_KERNEL);
4797 if (!args)
4798 return -ENOMEM;
4799 res = kzalloc(sizeof(*res), GFP_KERNEL);
4800 if (!res) {
4801 kfree(args);
4802 return -ENOMEM;
4803 }
4804 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4805 msg.rpc_argp = args;
4806 msg.rpc_resp = res;
4807
4808 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4809 &nfs41_sequence_ops, (void *)clp);
4810}
4811
557134a3
AA
4812#endif /* CONFIG_NFS_V4_1 */
4813
591d71cb 4814struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7eff03ae 4815 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
b79a4a1b 4816 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
1da177e4
LT
4817 .recover_open = nfs4_open_reclaim,
4818 .recover_lock = nfs4_lock_reclaim,
591d71cb 4819 .establish_clid = nfs4_init_clientid,
90a16617 4820 .get_clid_cred = nfs4_get_setclientid_cred,
1da177e4
LT
4821};
4822
591d71cb
AA
4823#if defined(CONFIG_NFS_V4_1)
4824struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4825 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4826 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4827 .recover_open = nfs4_open_reclaim,
4828 .recover_lock = nfs4_lock_reclaim,
4829 .establish_clid = nfs4_proc_exchange_id,
b4b82607 4830 .get_clid_cred = nfs4_get_exchange_id_cred,
591d71cb
AA
4831};
4832#endif /* CONFIG_NFS_V4_1 */
4833
4834struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
4835 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4836 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4837 .recover_open = nfs4_open_expired,
4838 .recover_lock = nfs4_lock_expired,
4839 .establish_clid = nfs4_init_clientid,
90a16617 4840 .get_clid_cred = nfs4_get_setclientid_cred,
591d71cb
AA
4841};
4842
4843#if defined(CONFIG_NFS_V4_1)
4844struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7eff03ae 4845 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
b79a4a1b 4846 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
1da177e4
LT
4847 .recover_open = nfs4_open_expired,
4848 .recover_lock = nfs4_lock_expired,
591d71cb 4849 .establish_clid = nfs4_proc_exchange_id,
b4b82607 4850 .get_clid_cred = nfs4_get_exchange_id_cred,
1da177e4 4851};
591d71cb 4852#endif /* CONFIG_NFS_V4_1 */
1da177e4 4853
29fba38b
BH
4854struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
4855 .sched_state_renewal = nfs4_proc_async_renew,
a7b72103 4856 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8e69514f 4857 .renew_lease = nfs4_proc_renew,
29fba38b
BH
4858};
4859
4860#if defined(CONFIG_NFS_V4_1)
4861struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
4862 .sched_state_renewal = nfs41_proc_async_sequence,
a7b72103 4863 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8e69514f 4864 .renew_lease = nfs4_proc_sequence,
29fba38b
BH
4865};
4866#endif
4867
4868/*
4869 * Per minor version reboot and network partition recovery ops
4870 */
4871
591d71cb
AA
4872struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
4873 &nfs40_reboot_recovery_ops,
4874#if defined(CONFIG_NFS_V4_1)
4875 &nfs41_reboot_recovery_ops,
4876#endif
4877};
4878
4879struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
4880 &nfs40_nograce_recovery_ops,
4881#if defined(CONFIG_NFS_V4_1)
4882 &nfs41_nograce_recovery_ops,
4883#endif
4884};
4885
29fba38b
BH
4886struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
4887 &nfs40_state_renewal_ops,
4888#if defined(CONFIG_NFS_V4_1)
4889 &nfs41_state_renewal_ops,
4890#endif
4891};
4892
92e1d5be 4893static const struct inode_operations nfs4_file_inode_operations = {
6b3b5496
BF
4894 .permission = nfs_permission,
4895 .getattr = nfs_getattr,
4896 .setattr = nfs_setattr,
4897 .getxattr = nfs4_getxattr,
4898 .setxattr = nfs4_setxattr,
4899 .listxattr = nfs4_listxattr,
4900};
4901
509de811 4902const struct nfs_rpc_ops nfs_v4_clientops = {
1da177e4
LT
4903 .version = 4, /* protocol version */
4904 .dentry_ops = &nfs4_dentry_operations,
4905 .dir_inode_ops = &nfs4_dir_inode_operations,
6b3b5496 4906 .file_inode_ops = &nfs4_file_inode_operations,
1da177e4
LT
4907 .getroot = nfs4_proc_get_root,
4908 .getattr = nfs4_proc_getattr,
4909 .setattr = nfs4_proc_setattr,
2b3de441 4910 .lookupfh = nfs4_proc_lookupfh,
1da177e4
LT
4911 .lookup = nfs4_proc_lookup,
4912 .access = nfs4_proc_access,
4913 .readlink = nfs4_proc_readlink,
1da177e4
LT
4914 .create = nfs4_proc_create,
4915 .remove = nfs4_proc_remove,
4916 .unlink_setup = nfs4_proc_unlink_setup,
4917 .unlink_done = nfs4_proc_unlink_done,
4918 .rename = nfs4_proc_rename,
4919 .link = nfs4_proc_link,
4920 .symlink = nfs4_proc_symlink,
4921 .mkdir = nfs4_proc_mkdir,
4922 .rmdir = nfs4_proc_remove,
4923 .readdir = nfs4_proc_readdir,
4924 .mknod = nfs4_proc_mknod,
4925 .statfs = nfs4_proc_statfs,
4926 .fsinfo = nfs4_proc_fsinfo,
4927 .pathconf = nfs4_proc_pathconf,
e9326dca 4928 .set_capabilities = nfs4_server_capabilities,
1da177e4
LT
4929 .decode_dirent = nfs4_decode_dirent,
4930 .read_setup = nfs4_proc_read_setup,
ec06c096 4931 .read_done = nfs4_read_done,
1da177e4 4932 .write_setup = nfs4_proc_write_setup,
788e7a89 4933 .write_done = nfs4_write_done,
1da177e4 4934 .commit_setup = nfs4_proc_commit_setup,
788e7a89 4935 .commit_done = nfs4_commit_done,
1da177e4 4936 .lock = nfs4_proc_lock,
e50a1c2e 4937 .clear_acl_cache = nfs4_zap_acl_attr,
7fe5c398 4938 .close_context = nfs4_close_context,
1da177e4
LT
4939};
4940
4941/*
4942 * Local variables:
4943 * c-basic-offset: 8
4944 * End:
4945 */