NFSv4: Make nfs4_state track O_RDWR, O_RDONLY and O_WRONLY separately
[linux-2.6-block.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>
1da177e4 51
4ce79717 52#include "nfs4_fs.h"
1da177e4
LT
53#include "delegation.h"
54
55#define NFSDBG_FACILITY NFSDBG_PROC
56
57#define NFS4_POLL_RETRY_MIN (1*HZ)
58#define NFS4_POLL_RETRY_MAX (15*HZ)
59
cdd4e68b
TM
60struct nfs4_opendata;
61static int _nfs4_proc_open_confirm(struct nfs4_opendata *data);
1da177e4 62static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
faf5f49c 63static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
1da177e4 64static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
faf5f49c 65static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
1da177e4
LT
66extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
67extern struct rpc_procinfo nfs4_procedures[];
68
1da177e4
LT
69/* Prevent leaks of NFSv4 errors into userland */
70int nfs4_map_errors(int err)
71{
72 if (err < -1000) {
73 dprintk("%s could not handle NFSv4 error %d\n",
74 __FUNCTION__, -err);
75 return -EIO;
76 }
77 return err;
78}
79
80/*
81 * This is our standard bitmap for GETATTR requests.
82 */
83const u32 nfs4_fattr_bitmap[2] = {
84 FATTR4_WORD0_TYPE
85 | FATTR4_WORD0_CHANGE
86 | FATTR4_WORD0_SIZE
87 | FATTR4_WORD0_FSID
88 | FATTR4_WORD0_FILEID,
89 FATTR4_WORD1_MODE
90 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER
92 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_RAWDEV
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
98};
99
100const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
107};
108
4ce79717 109const u32 nfs4_pathconf_bitmap[2] = {
1da177e4
LT
110 FATTR4_WORD0_MAXLINK
111 | FATTR4_WORD0_MAXNAME,
112 0
113};
114
115const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
119 0
120};
121
122static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
123 struct nfs4_readdir_arg *readdir)
124{
125 u32 *start, *p;
126
127 BUG_ON(readdir->count < 80);
128 if (cookie > 2) {
b7ef1956 129 readdir->cookie = cookie;
1da177e4
LT
130 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
131 return;
132 }
133
134 readdir->cookie = 0;
135 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
136 if (cookie == 2)
137 return;
138
139 /*
140 * NFSv4 servers do not return entries for '.' and '..'
141 * Therefore, we fake these entries here. We let '.'
142 * have cookie 0 and '..' have cookie 1. Note that
143 * when talking to the server, we always send cookie 0
144 * instead of 1 or 2.
145 */
146 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
147
148 if (cookie == 0) {
149 *p++ = xdr_one; /* next */
150 *p++ = xdr_zero; /* cookie, first word */
151 *p++ = xdr_one; /* cookie, second word */
152 *p++ = xdr_one; /* entry len */
153 memcpy(p, ".\0\0\0", 4); /* entry */
154 p++;
155 *p++ = xdr_one; /* bitmap length */
156 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
157 *p++ = htonl(8); /* attribute buffer length */
158 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
159 }
160
161 *p++ = xdr_one; /* next */
162 *p++ = xdr_zero; /* cookie, first word */
163 *p++ = xdr_two; /* cookie, second word */
164 *p++ = xdr_two; /* entry len */
165 memcpy(p, "..\0\0", 4); /* entry */
166 p++;
167 *p++ = xdr_one; /* bitmap length */
168 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
169 *p++ = htonl(8); /* attribute buffer length */
170 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
171
172 readdir->pgbase = (char *)p - (char *)start;
173 readdir->count -= readdir->pgbase;
174 kunmap_atomic(start, KM_USER0);
175}
176
177static void
178renew_lease(struct nfs_server *server, unsigned long timestamp)
179{
180 struct nfs4_client *clp = server->nfs4_state;
181 spin_lock(&clp->cl_lock);
182 if (time_before(clp->cl_last_renewal,timestamp))
183 clp->cl_last_renewal = timestamp;
184 spin_unlock(&clp->cl_lock);
185}
186
187static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
188{
189 struct nfs_inode *nfsi = NFS_I(inode);
190
decf491f
TM
191 spin_lock(&inode->i_lock);
192 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
1da177e4
LT
193 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
194 nfsi->change_attr = cinfo->after;
decf491f 195 spin_unlock(&inode->i_lock);
1da177e4
LT
196}
197
e56e0b78 198struct nfs4_opendata {
24ac23ab 199 atomic_t count;
e56e0b78
TM
200 struct nfs_openargs o_arg;
201 struct nfs_openres o_res;
cdd4e68b
TM
202 struct nfs_open_confirmargs c_arg;
203 struct nfs_open_confirmres c_res;
e56e0b78
TM
204 struct nfs_fattr f_attr;
205 struct nfs_fattr dir_attr;
206 struct dentry *dentry;
207 struct dentry *dir;
208 struct nfs4_state_owner *owner;
209 struct iattr attrs;
24ac23ab
TM
210 int rpc_status;
211 int cancelled;
e56e0b78
TM
212};
213
214static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
215 struct nfs4_state_owner *sp, int flags,
216 const struct iattr *attrs)
217{
218 struct dentry *parent = dget_parent(dentry);
219 struct inode *dir = parent->d_inode;
220 struct nfs_server *server = NFS_SERVER(dir);
221 struct nfs4_opendata *p;
222
223 p = kzalloc(sizeof(*p), GFP_KERNEL);
224 if (p == NULL)
225 goto err;
226 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
227 if (p->o_arg.seqid == NULL)
228 goto err_free;
24ac23ab 229 atomic_set(&p->count, 1);
e56e0b78
TM
230 p->dentry = dget(dentry);
231 p->dir = parent;
232 p->owner = sp;
233 atomic_inc(&sp->so_count);
234 p->o_arg.fh = NFS_FH(dir);
235 p->o_arg.open_flags = flags,
236 p->o_arg.clientid = server->nfs4_state->cl_clientid;
237 p->o_arg.id = sp->so_id;
238 p->o_arg.name = &dentry->d_name;
239 p->o_arg.server = server;
240 p->o_arg.bitmask = server->attr_bitmask;
241 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
242 p->o_res.f_attr = &p->f_attr;
243 p->o_res.dir_attr = &p->dir_attr;
244 p->o_res.server = server;
245 nfs_fattr_init(&p->f_attr);
246 nfs_fattr_init(&p->dir_attr);
247 if (flags & O_EXCL) {
248 u32 *s = (u32 *) p->o_arg.u.verifier.data;
249 s[0] = jiffies;
250 s[1] = current->pid;
251 } else if (flags & O_CREAT) {
252 p->o_arg.u.attrs = &p->attrs;
253 memcpy(&p->attrs, attrs, sizeof(p->attrs));
254 }
cdd4e68b
TM
255 p->c_arg.fh = &p->o_res.fh;
256 p->c_arg.stateid = &p->o_res.stateid;
257 p->c_arg.seqid = p->o_arg.seqid;
e56e0b78
TM
258 return p;
259err_free:
260 kfree(p);
261err:
262 dput(parent);
263 return NULL;
264}
265
266static void nfs4_opendata_free(struct nfs4_opendata *p)
267{
24ac23ab 268 if (p != NULL && atomic_dec_and_test(&p->count)) {
e56e0b78
TM
269 nfs_free_seqid(p->o_arg.seqid);
270 nfs4_put_state_owner(p->owner);
271 dput(p->dir);
272 dput(p->dentry);
273 kfree(p);
274 }
275}
276
9512135d 277/* Helper for asynchronous RPC calls */
4ce70ada 278static int nfs4_call_async(struct rpc_clnt *clnt,
963d8fe5 279 const struct rpc_call_ops *tk_ops, void *calldata)
9512135d
TM
280{
281 struct rpc_task *task;
282
963d8fe5 283 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
9512135d 284 return -ENOMEM;
9512135d
TM
285 rpc_execute(task);
286 return 0;
287}
288
06f814a3
TM
289static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
290{
291 sigset_t oldset;
292 int ret;
293
294 rpc_clnt_sigmask(task->tk_client, &oldset);
295 ret = rpc_wait_for_completion_task(task);
296 rpc_clnt_sigunmask(task->tk_client, &oldset);
297 return ret;
298}
299
e7616923
TM
300static inline void update_open_stateflags(struct nfs4_state *state, mode_t open_flags)
301{
302 switch (open_flags) {
303 case FMODE_WRITE:
304 state->n_wronly++;
305 break;
306 case FMODE_READ:
307 state->n_rdonly++;
308 break;
309 case FMODE_READ|FMODE_WRITE:
310 state->n_rdwr++;
311 }
312}
313
1da177e4
LT
314static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
315{
316 struct inode *inode = state->inode;
317
318 open_flags &= (FMODE_READ|FMODE_WRITE);
d530838b 319 /* Protect against nfs4_find_state_byowner() */
ec073428 320 spin_lock(&state->owner->so_lock);
1da177e4 321 spin_lock(&inode->i_lock);
4cecb76f 322 memcpy(&state->stateid, stateid, sizeof(state->stateid));
e7616923 323 update_open_stateflags(state, open_flags);
4cecb76f 324 nfs4_state_set_mode_locked(state, state->state | open_flags);
1da177e4 325 spin_unlock(&inode->i_lock);
ec073428 326 spin_unlock(&state->owner->so_lock);
1da177e4
LT
327}
328
24ac23ab
TM
329static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
330{
331 struct inode *inode;
332 struct nfs4_state *state = NULL;
333
334 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
335 goto out;
336 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
337 if (inode == NULL)
338 goto out;
339 state = nfs4_get_open_state(inode, data->owner);
340 if (state == NULL)
341 goto put_inode;
342 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
343put_inode:
344 iput(inode);
345out:
346 return state;
347}
348
1da177e4
LT
349/*
350 * OPEN_RECLAIM:
351 * reclaim state on the server after a reboot.
1da177e4
LT
352 */
353static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
354{
355 struct inode *inode = state->inode;
356 struct nfs_server *server = NFS_SERVER(inode);
357 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
358 struct nfs_openargs o_arg = {
359 .fh = NFS_FH(inode),
1da177e4
LT
360 .id = sp->so_id,
361 .open_flags = state->state,
362 .clientid = server->nfs4_state->cl_clientid,
363 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
364 .bitmask = server->attr_bitmask,
365 };
366 struct nfs_openres o_res = {
367 .server = server, /* Grrr */
368 };
369 struct rpc_message msg = {
370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
371 .rpc_argp = &o_arg,
372 .rpc_resp = &o_res,
373 .rpc_cred = sp->so_cred,
374 };
375 int status;
376
377 if (delegation != NULL) {
378 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
379 memcpy(&state->stateid, &delegation->stateid,
380 sizeof(state->stateid));
381 set_bit(NFS_DELEGATED_STATE, &state->flags);
382 return 0;
383 }
384 o_arg.u.delegation_type = delegation->type;
385 }
cee54fc9
TM
386 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
387 if (o_arg.seqid == NULL)
388 return -ENOMEM;
1da177e4 389 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
cee54fc9
TM
390 /* Confirm the sequence as being established */
391 nfs_confirm_seqid(&sp->so_seqid, status);
392 nfs_increment_open_seqid(status, o_arg.seqid);
1da177e4
LT
393 if (status == 0) {
394 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
395 if (o_res.delegation_type != 0) {
396 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
397 /* Did the server issue an immediate delegation recall? */
398 if (o_res.do_recall)
399 nfs_async_inode_return_delegation(inode, &o_res.stateid);
400 }
401 }
cee54fc9 402 nfs_free_seqid(o_arg.seqid);
1da177e4
LT
403 clear_bit(NFS_DELEGATED_STATE, &state->flags);
404 /* Ensure we update the inode attributes */
405 NFS_CACHEINV(inode);
406 return status;
407}
408
409static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
410{
411 struct nfs_server *server = NFS_SERVER(state->inode);
412 struct nfs4_exception exception = { };
413 int err;
414 do {
415 err = _nfs4_open_reclaim(sp, state);
202b50dc
TM
416 if (err != -NFS4ERR_DELAY)
417 break;
418 nfs4_handle_exception(server, err, &exception);
1da177e4
LT
419 } while (exception.retry);
420 return err;
421}
422
423static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
424{
425 struct nfs4_state_owner *sp = state->owner;
426 struct inode *inode = dentry->d_inode;
427 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
428 struct rpc_message msg = {
429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
1da177e4
LT
430 .rpc_cred = sp->so_cred,
431 };
e56e0b78 432 struct nfs4_opendata *opendata;
1da177e4
LT
433 int status = 0;
434
1da177e4
LT
435 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
436 goto out;
437 if (state->state == 0)
438 goto out;
e56e0b78 439 opendata = nfs4_opendata_alloc(dentry, sp, state->state, NULL);
cee54fc9 440 status = -ENOMEM;
e56e0b78 441 if (opendata == NULL)
cee54fc9 442 goto out;
e56e0b78
TM
443 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
444 msg.rpc_argp = &opendata->o_arg;
445 msg.rpc_resp = &opendata->o_res;
446 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
447 sizeof(opendata->o_arg.u.delegation.data));
1da177e4 448 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
e56e0b78 449 nfs_increment_open_seqid(status, opendata->o_arg.seqid);
0a8838f9
TM
450 if (status != 0)
451 goto out_free;
e56e0b78 452 if(opendata->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
cdd4e68b 453 status = _nfs4_proc_open_confirm(opendata);
0a8838f9
TM
454 if (status != 0)
455 goto out_free;
456 }
457 nfs_confirm_seqid(&sp->so_seqid, 0);
1da177e4 458 if (status >= 0) {
e56e0b78 459 memcpy(state->stateid.data, opendata->o_res.stateid.data,
1da177e4
LT
460 sizeof(state->stateid.data));
461 clear_bit(NFS_DELEGATED_STATE, &state->flags);
462 }
0a8838f9 463out_free:
e56e0b78 464 nfs4_opendata_free(opendata);
1da177e4 465out:
1da177e4
LT
466 return status;
467}
468
469int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
470{
471 struct nfs4_exception exception = { };
472 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
473 int err;
474 do {
475 err = _nfs4_open_delegation_recall(dentry, state);
476 switch (err) {
477 case 0:
478 return err;
479 case -NFS4ERR_STALE_CLIENTID:
480 case -NFS4ERR_STALE_STATEID:
481 case -NFS4ERR_EXPIRED:
482 /* Don't recall a delegation if it was lost */
483 nfs4_schedule_state_recovery(server->nfs4_state);
484 return err;
485 }
486 err = nfs4_handle_exception(server, err, &exception);
487 } while (exception.retry);
488 return err;
489}
490
cdd4e68b 491static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1da177e4 492{
cdd4e68b
TM
493 struct nfs4_opendata *data = calldata;
494 struct rpc_message msg = {
495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
496 .rpc_argp = &data->c_arg,
497 .rpc_resp = &data->c_res,
498 .rpc_cred = data->owner->so_cred,
1da177e4 499 };
cdd4e68b
TM
500 rpc_call_setup(task, &msg, 0);
501}
502
503static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
504{
505 struct nfs4_opendata *data = calldata;
506
507 data->rpc_status = task->tk_status;
508 if (RPC_ASSASSINATED(task))
509 return;
510 if (data->rpc_status == 0)
511 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
512 sizeof(data->o_res.stateid.data));
513 nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid);
514 nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status);
515}
516
517static void nfs4_open_confirm_release(void *calldata)
518{
519 struct nfs4_opendata *data = calldata;
520 struct nfs4_state *state = NULL;
521
522 /* If this request hasn't been cancelled, do nothing */
523 if (data->cancelled == 0)
524 goto out_free;
525 /* In case of error, no cleanup! */
526 if (data->rpc_status != 0)
527 goto out_free;
528 nfs_confirm_seqid(&data->owner->so_seqid, 0);
529 state = nfs4_opendata_to_nfs4_state(data);
530 if (state != NULL)
531 nfs4_close_state(state, data->o_arg.open_flags);
532out_free:
533 nfs4_opendata_free(data);
534}
535
536static const struct rpc_call_ops nfs4_open_confirm_ops = {
537 .rpc_call_prepare = nfs4_open_confirm_prepare,
538 .rpc_call_done = nfs4_open_confirm_done,
539 .rpc_release = nfs4_open_confirm_release,
540};
541
542/*
543 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
544 */
545static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
546{
547 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
548 struct rpc_task *task;
1da177e4
LT
549 int status;
550
cdd4e68b
TM
551 atomic_inc(&data->count);
552 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data);
553 if (IS_ERR(task)) {
554 nfs4_opendata_free(data);
555 return PTR_ERR(task);
556 }
557 status = nfs4_wait_for_completion_rpc_task(task);
558 if (status != 0) {
559 data->cancelled = 1;
560 smp_wmb();
561 } else
562 status = data->rpc_status;
563 rpc_release_task(task);
1da177e4
LT
564 return status;
565}
566
24ac23ab 567static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1da177e4 568{
24ac23ab
TM
569 struct nfs4_opendata *data = calldata;
570 struct nfs4_state_owner *sp = data->owner;
1da177e4
LT
571 struct rpc_message msg = {
572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
24ac23ab
TM
573 .rpc_argp = &data->o_arg,
574 .rpc_resp = &data->o_res,
1da177e4
LT
575 .rpc_cred = sp->so_cred,
576 };
24ac23ab
TM
577
578 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
579 return;
580 /* Update sequence id. */
581 data->o_arg.id = sp->so_id;
582 data->o_arg.clientid = sp->so_client->cl_clientid;
583 rpc_call_setup(task, &msg, 0);
584}
1da177e4 585
24ac23ab
TM
586static void nfs4_open_done(struct rpc_task *task, void *calldata)
587{
588 struct nfs4_opendata *data = calldata;
1da177e4 589
24ac23ab
TM
590 data->rpc_status = task->tk_status;
591 if (RPC_ASSASSINATED(task))
592 return;
593 if (task->tk_status == 0) {
594 switch (data->o_res.f_attr->mode & S_IFMT) {
6f926b5b
TM
595 case S_IFREG:
596 break;
597 case S_IFLNK:
24ac23ab 598 data->rpc_status = -ELOOP;
6f926b5b
TM
599 break;
600 case S_IFDIR:
24ac23ab 601 data->rpc_status = -EISDIR;
6f926b5b
TM
602 break;
603 default:
24ac23ab 604 data->rpc_status = -ENOTDIR;
6f926b5b
TM
605 }
606 }
24ac23ab
TM
607 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
608}
6f926b5b 609
24ac23ab
TM
610static void nfs4_open_release(void *calldata)
611{
612 struct nfs4_opendata *data = calldata;
613 struct nfs4_state *state = NULL;
614
615 /* If this request hasn't been cancelled, do nothing */
616 if (data->cancelled == 0)
617 goto out_free;
618 /* In case of error, no cleanup! */
619 if (data->rpc_status != 0)
620 goto out_free;
621 /* In case we need an open_confirm, no cleanup! */
622 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
623 goto out_free;
624 nfs_confirm_seqid(&data->owner->so_seqid, 0);
625 state = nfs4_opendata_to_nfs4_state(data);
626 if (state != NULL)
627 nfs4_close_state(state, data->o_arg.open_flags);
628out_free:
629 nfs4_opendata_free(data);
630}
631
632static const struct rpc_call_ops nfs4_open_ops = {
633 .rpc_call_prepare = nfs4_open_prepare,
634 .rpc_call_done = nfs4_open_done,
635 .rpc_release = nfs4_open_release,
636};
637
638/*
639 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
640 */
641static int _nfs4_proc_open(struct nfs4_opendata *data)
642{
643 struct inode *dir = data->dir->d_inode;
644 struct nfs_server *server = NFS_SERVER(dir);
645 struct nfs_openargs *o_arg = &data->o_arg;
646 struct nfs_openres *o_res = &data->o_res;
647 struct rpc_task *task;
648 int status;
649
650 atomic_inc(&data->count);
651 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
652 if (IS_ERR(task)) {
653 nfs4_opendata_free(data);
654 return PTR_ERR(task);
655 }
656 status = nfs4_wait_for_completion_rpc_task(task);
657 if (status != 0) {
658 data->cancelled = 1;
659 smp_wmb();
660 } else
661 status = data->rpc_status;
662 rpc_release_task(task);
1da177e4 663 if (status != 0)
24ac23ab
TM
664 return status;
665
56ae19f3
TM
666 if (o_arg->open_flags & O_CREAT) {
667 update_changeattr(dir, &o_res->cinfo);
668 nfs_post_op_update_inode(dir, o_res->dir_attr);
669 } else
670 nfs_refresh_inode(dir, o_res->dir_attr);
1da177e4 671 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
cdd4e68b 672 status = _nfs4_proc_open_confirm(data);
1da177e4 673 if (status != 0)
24ac23ab 674 return status;
1da177e4 675 }
24ac23ab 676 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1da177e4 677 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
24ac23ab
TM
678 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
679 return 0;
1da177e4
LT
680}
681
682static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
683{
684 struct nfs_access_entry cache;
685 int mask = 0;
686 int status;
687
688 if (openflags & FMODE_READ)
689 mask |= MAY_READ;
690 if (openflags & FMODE_WRITE)
691 mask |= MAY_WRITE;
692 status = nfs_access_get_cached(inode, cred, &cache);
693 if (status == 0)
694 goto out;
695
696 /* Be clever: ask server to check for all possible rights */
697 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
698 cache.cred = cred;
699 cache.jiffies = jiffies;
700 status = _nfs4_proc_access(inode, &cache);
701 if (status != 0)
702 return status;
703 nfs_access_add_cache(inode, &cache);
704out:
705 if ((cache.mask & mask) == mask)
706 return 0;
707 return -EACCES;
708}
709
710/*
711 * OPEN_EXPIRED:
712 * reclaim state on the server after a network partition.
713 * Assumes caller holds the appropriate lock
714 */
715static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
716{
717 struct dentry *parent = dget_parent(dentry);
1da177e4 718 struct inode *inode = state->inode;
1da177e4 719 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
e56e0b78 720 struct nfs4_opendata *opendata;
24ac23ab
TM
721 struct nfs4_state *newstate;
722 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
1da177e4
LT
723 int status = 0;
724
725 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
24ac23ab 726 status = _nfs4_do_access(inode, sp->so_cred, openflags);
1da177e4
LT
727 if (status < 0)
728 goto out;
729 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
730 set_bit(NFS_DELEGATED_STATE, &state->flags);
731 goto out;
732 }
cee54fc9 733 status = -ENOMEM;
24ac23ab 734 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
e56e0b78 735 if (opendata == NULL)
cee54fc9 736 goto out;
24ac23ab 737 status = _nfs4_proc_open(opendata);
1da177e4
LT
738 if (status != 0)
739 goto out_nodeleg;
24ac23ab
TM
740 newstate = nfs4_opendata_to_nfs4_state(opendata);
741 if (newstate != state)
1da177e4 742 goto out_stale;
e56e0b78 743 if (opendata->o_res.delegation_type != 0) {
1da177e4 744 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
e56e0b78
TM
745 nfs_inode_set_delegation(inode, sp->so_cred,
746 &opendata->o_res);
1da177e4 747 else
e56e0b78
TM
748 nfs_inode_reclaim_delegation(inode, sp->so_cred,
749 &opendata->o_res);
1da177e4 750 }
24ac23ab
TM
751out_close_state:
752 nfs4_close_state(newstate, openflags);
1da177e4 753out_nodeleg:
e56e0b78 754 nfs4_opendata_free(opendata);
1da177e4
LT
755 clear_bit(NFS_DELEGATED_STATE, &state->flags);
756out:
757 dput(parent);
758 return status;
759out_stale:
760 status = -ESTALE;
761 /* Invalidate the state owner so we don't ever use it again */
762 nfs4_drop_state_owner(sp);
763 d_drop(dentry);
764 /* Should we be trying to close that stateid? */
24ac23ab 765 goto out_close_state;
1da177e4
LT
766}
767
202b50dc
TM
768static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
769{
770 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
771 struct nfs4_exception exception = { };
772 int err;
773
774 do {
775 err = _nfs4_open_expired(sp, state, dentry);
776 if (err == -NFS4ERR_DELAY)
777 nfs4_handle_exception(server, err, &exception);
778 } while (exception.retry);
779 return err;
780}
781
1da177e4
LT
782static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
783{
784 struct nfs_inode *nfsi = NFS_I(state->inode);
785 struct nfs_open_context *ctx;
786 int status;
787
788 spin_lock(&state->inode->i_lock);
789 list_for_each_entry(ctx, &nfsi->open_files, list) {
790 if (ctx->state != state)
791 continue;
792 get_nfs_open_context(ctx);
793 spin_unlock(&state->inode->i_lock);
202b50dc 794 status = nfs4_do_open_expired(sp, state, ctx->dentry);
1da177e4
LT
795 put_nfs_open_context(ctx);
796 return status;
797 }
798 spin_unlock(&state->inode->i_lock);
799 return -ENOENT;
800}
801
802/*
24ac23ab 803 * Returns a referenced nfs4_state if there is an open delegation on the file
1da177e4
LT
804 */
805static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
806{
807 struct nfs_delegation *delegation;
808 struct nfs_server *server = NFS_SERVER(inode);
809 struct nfs4_client *clp = server->nfs4_state;
810 struct nfs_inode *nfsi = NFS_I(inode);
811 struct nfs4_state_owner *sp = NULL;
812 struct nfs4_state *state = NULL;
813 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
814 int err;
815
816 /* Protect against reboot recovery - NOTE ORDER! */
817 down_read(&clp->cl_sem);
818 /* Protect against delegation recall */
819 down_read(&nfsi->rwsem);
820 delegation = NFS_I(inode)->delegation;
821 err = -ENOENT;
822 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
823 goto out_err;
824 err = -ENOMEM;
825 if (!(sp = nfs4_get_state_owner(server, cred))) {
826 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
827 goto out_err;
828 }
1da177e4
LT
829 state = nfs4_get_open_state(inode, sp);
830 if (state == NULL)
831 goto out_err;
832
833 err = -ENOENT;
834 if ((state->state & open_flags) == open_flags) {
835 spin_lock(&inode->i_lock);
e7616923 836 update_open_stateflags(state, open_flags);
1da177e4
LT
837 spin_unlock(&inode->i_lock);
838 goto out_ok;
839 } else if (state->state != 0)
840 goto out_err;
841
842 lock_kernel();
843 err = _nfs4_do_access(inode, cred, open_flags);
844 unlock_kernel();
845 if (err != 0)
846 goto out_err;
847 set_bit(NFS_DELEGATED_STATE, &state->flags);
848 update_open_stateid(state, &delegation->stateid, open_flags);
849out_ok:
1da177e4
LT
850 nfs4_put_state_owner(sp);
851 up_read(&nfsi->rwsem);
852 up_read(&clp->cl_sem);
1da177e4
LT
853 *res = state;
854 return 0;
855out_err:
856 if (sp != NULL) {
857 if (state != NULL)
858 nfs4_put_open_state(state);
1da177e4
LT
859 nfs4_put_state_owner(sp);
860 }
861 up_read(&nfsi->rwsem);
862 up_read(&clp->cl_sem);
b8e5c4c2
TM
863 if (err != -EACCES)
864 nfs_inode_return_delegation(inode);
1da177e4
LT
865 return err;
866}
867
868static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
869{
870 struct nfs4_exception exception = { };
871 struct nfs4_state *res;
872 int err;
873
874 do {
875 err = _nfs4_open_delegated(inode, flags, cred, &res);
876 if (err == 0)
877 break;
878 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
879 err, &exception));
880 } while (exception.retry);
881 return res;
882}
883
884/*
24ac23ab 885 * Returns a referenced nfs4_state
1da177e4
LT
886 */
887static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
888{
889 struct nfs4_state_owner *sp;
890 struct nfs4_state *state = NULL;
891 struct nfs_server *server = NFS_SERVER(dir);
892 struct nfs4_client *clp = server->nfs4_state;
e56e0b78 893 struct nfs4_opendata *opendata;
1da177e4 894 int status;
1da177e4
LT
895
896 /* Protect against reboot recovery conflicts */
897 down_read(&clp->cl_sem);
898 status = -ENOMEM;
899 if (!(sp = nfs4_get_state_owner(server, cred))) {
900 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
901 goto out_err;
902 }
e56e0b78
TM
903 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
904 if (opendata == NULL)
905 goto err_put_state_owner;
1da177e4 906
24ac23ab 907 status = _nfs4_proc_open(opendata);
1da177e4 908 if (status != 0)
e56e0b78 909 goto err_opendata_free;
1da177e4
LT
910
911 status = -ENOMEM;
24ac23ab
TM
912 state = nfs4_opendata_to_nfs4_state(opendata);
913 if (state == NULL)
e56e0b78 914 goto err_opendata_free;
e56e0b78 915 if (opendata->o_res.delegation_type != 0)
24ac23ab 916 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
e56e0b78 917 nfs4_opendata_free(opendata);
1da177e4
LT
918 nfs4_put_state_owner(sp);
919 up_read(&clp->cl_sem);
920 *res = state;
921 return 0;
e56e0b78
TM
922err_opendata_free:
923 nfs4_opendata_free(opendata);
924err_put_state_owner:
925 nfs4_put_state_owner(sp);
1da177e4 926out_err:
1da177e4
LT
927 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
928 up_read(&clp->cl_sem);
1da177e4
LT
929 *res = NULL;
930 return status;
931}
932
933
934static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
935{
936 struct nfs4_exception exception = { };
937 struct nfs4_state *res;
938 int status;
939
940 do {
941 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
942 if (status == 0)
943 break;
944 /* NOTE: BAD_SEQID means the server and client disagree about the
945 * book-keeping w.r.t. state-changing operations
946 * (OPEN/CLOSE/LOCK/LOCKU...)
947 * It is actually a sign of a bug on the client or on the server.
948 *
949 * If we receive a BAD_SEQID error in the particular case of
cee54fc9 950 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1da177e4
LT
951 * have unhashed the old state_owner for us, and that we can
952 * therefore safely retry using a new one. We should still warn
953 * the user though...
954 */
955 if (status == -NFS4ERR_BAD_SEQID) {
956 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
957 exception.retry = 1;
958 continue;
959 }
550f5747
TM
960 /*
961 * BAD_STATEID on OPEN means that the server cancelled our
962 * state before it received the OPEN_CONFIRM.
963 * Recover by retrying the request as per the discussion
964 * on Page 181 of RFC3530.
965 */
966 if (status == -NFS4ERR_BAD_STATEID) {
967 exception.retry = 1;
968 continue;
969 }
1da177e4
LT
970 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
971 status, &exception));
972 } while (exception.retry);
973 return res;
974}
975
976static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
977 struct nfs_fh *fhandle, struct iattr *sattr,
978 struct nfs4_state *state)
979{
980 struct nfs_setattrargs arg = {
981 .fh = fhandle,
982 .iap = sattr,
983 .server = server,
984 .bitmask = server->attr_bitmask,
985 };
986 struct nfs_setattrres res = {
987 .fattr = fattr,
988 .server = server,
989 };
990 struct rpc_message msg = {
991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
992 .rpc_argp = &arg,
993 .rpc_resp = &res,
994 };
65e4308d 995 int status;
1da177e4 996
0e574af1 997 nfs_fattr_init(fattr);
1da177e4 998
08e9eac4 999 if (state != NULL) {
1da177e4 1000 msg.rpc_cred = state->owner->so_cred;
08e9eac4
TM
1001 nfs4_copy_stateid(&arg.stateid, state, current->files);
1002 } else
1da177e4
LT
1003 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1004
65e4308d
TM
1005 status = rpc_call_sync(server->client, &msg, 0);
1006 return status;
1da177e4
LT
1007}
1008
1009static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
1010 struct nfs_fh *fhandle, struct iattr *sattr,
1011 struct nfs4_state *state)
1012{
1013 struct nfs4_exception exception = { };
1014 int err;
1015 do {
1016 err = nfs4_handle_exception(server,
1017 _nfs4_do_setattr(server, fattr, fhandle, sattr,
1018 state),
1019 &exception);
1020 } while (exception.retry);
1021 return err;
1022}
1023
1024struct nfs4_closedata {
1025 struct inode *inode;
1026 struct nfs4_state *state;
1027 struct nfs_closeargs arg;
1028 struct nfs_closeres res;
516a6af6 1029 struct nfs_fattr fattr;
1da177e4
LT
1030};
1031
963d8fe5 1032static void nfs4_free_closedata(void *data)
9512135d 1033{
963d8fe5
TM
1034 struct nfs4_closedata *calldata = data;
1035 struct nfs4_state_owner *sp = calldata->state->owner;
9512135d
TM
1036
1037 nfs4_put_open_state(calldata->state);
1038 nfs_free_seqid(calldata->arg.seqid);
9512135d 1039 nfs4_put_state_owner(sp);
9512135d
TM
1040 kfree(calldata);
1041}
1042
963d8fe5 1043static void nfs4_close_done(struct rpc_task *task, void *data)
1da177e4 1044{
963d8fe5 1045 struct nfs4_closedata *calldata = data;
1da177e4 1046 struct nfs4_state *state = calldata->state;
1da177e4
LT
1047 struct nfs_server *server = NFS_SERVER(calldata->inode);
1048
4ce70ada
TM
1049 if (RPC_ASSASSINATED(task))
1050 return;
1da177e4
LT
1051 /* hmm. we are done with the inode, and in the process of freeing
1052 * the state_owner. we keep this around to process errors
1053 */
cee54fc9 1054 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
1da177e4
LT
1055 switch (task->tk_status) {
1056 case 0:
1057 memcpy(&state->stateid, &calldata->res.stateid,
1058 sizeof(state->stateid));
1059 break;
1060 case -NFS4ERR_STALE_STATEID:
1061 case -NFS4ERR_EXPIRED:
1da177e4
LT
1062 nfs4_schedule_state_recovery(server->nfs4_state);
1063 break;
1064 default:
1065 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1066 rpc_restart_call(task);
1067 return;
1068 }
1069 }
516a6af6 1070 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1da177e4
LT
1071}
1072
4ce70ada 1073static void nfs4_close_prepare(struct rpc_task *task, void *data)
1da177e4 1074{
4ce70ada 1075 struct nfs4_closedata *calldata = data;
9512135d 1076 struct nfs4_state *state = calldata->state;
1da177e4
LT
1077 struct rpc_message msg = {
1078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1079 .rpc_argp = &calldata->arg,
1080 .rpc_resp = &calldata->res,
9512135d 1081 .rpc_cred = state->owner->so_cred,
1da177e4 1082 };
4cecb76f 1083 int mode = 0, old_mode;
9512135d 1084
963d8fe5 1085 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
9512135d 1086 return;
9512135d
TM
1087 /* Recalculate the new open mode in case someone reopened the file
1088 * while we were waiting in line to be scheduled.
1089 */
4cecb76f
TM
1090 spin_lock(&state->owner->so_lock);
1091 spin_lock(&calldata->inode->i_lock);
1092 mode = old_mode = state->state;
e7616923
TM
1093 if (state->n_rdwr == 0) {
1094 if (state->n_rdonly == 0)
1095 mode &= ~FMODE_READ;
1096 if (state->n_wronly == 0)
1097 mode &= ~FMODE_WRITE;
1098 }
4cecb76f
TM
1099 nfs4_state_set_mode_locked(state, mode);
1100 spin_unlock(&calldata->inode->i_lock);
1101 spin_unlock(&state->owner->so_lock);
1102 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
963d8fe5
TM
1103 /* Note: exit _without_ calling nfs4_close_done */
1104 task->tk_action = NULL;
9512135d
TM
1105 return;
1106 }
516a6af6 1107 nfs_fattr_init(calldata->res.fattr);
9512135d 1108 if (mode != 0)
1da177e4 1109 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
9512135d
TM
1110 calldata->arg.open_flags = mode;
1111 rpc_call_setup(task, &msg, 0);
1da177e4
LT
1112}
1113
963d8fe5 1114static const struct rpc_call_ops nfs4_close_ops = {
4ce70ada 1115 .rpc_call_prepare = nfs4_close_prepare,
963d8fe5
TM
1116 .rpc_call_done = nfs4_close_done,
1117 .rpc_release = nfs4_free_closedata,
1118};
1119
1da177e4
LT
1120/*
1121 * It is possible for data to be read/written from a mem-mapped file
1122 * after the sys_close call (which hits the vfs layer as a flush).
1123 * This means that we can't safely call nfsv4 close on a file until
1124 * the inode is cleared. This in turn means that we are not good
1125 * NFSv4 citizens - we do not indicate to the server to update the file's
1126 * share state even when we are done with one of the three share
1127 * stateid's in the inode.
1128 *
1129 * NOTE: Caller must be holding the sp->so_owner semaphore!
1130 */
4cecb76f 1131int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
1da177e4 1132{
516a6af6 1133 struct nfs_server *server = NFS_SERVER(inode);
1da177e4 1134 struct nfs4_closedata *calldata;
9512135d 1135 int status = -ENOMEM;
1da177e4 1136
9512135d 1137 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
1da177e4 1138 if (calldata == NULL)
9512135d 1139 goto out;
1da177e4
LT
1140 calldata->inode = inode;
1141 calldata->state = state;
1142 calldata->arg.fh = NFS_FH(inode);
9512135d 1143 calldata->arg.stateid = &state->stateid;
1da177e4 1144 /* Serialization for the sequence id */
cee54fc9 1145 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
9512135d
TM
1146 if (calldata->arg.seqid == NULL)
1147 goto out_free_calldata;
516a6af6
TM
1148 calldata->arg.bitmask = server->attr_bitmask;
1149 calldata->res.fattr = &calldata->fattr;
1150 calldata->res.server = server;
9512135d 1151
4ce70ada 1152 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
9512135d
TM
1153 if (status == 0)
1154 goto out;
1155
1156 nfs_free_seqid(calldata->arg.seqid);
1157out_free_calldata:
1158 kfree(calldata);
1159out:
1160 return status;
1da177e4
LT
1161}
1162
02a913a7
TM
1163static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1164{
1165 struct file *filp;
1166
1167 filp = lookup_instantiate_filp(nd, dentry, NULL);
1168 if (!IS_ERR(filp)) {
1169 struct nfs_open_context *ctx;
1170 ctx = (struct nfs_open_context *)filp->private_data;
1171 ctx->state = state;
1172 } else
1173 nfs4_close_state(state, nd->intent.open.flags);
1174}
1175
1176struct dentry *
1da177e4
LT
1177nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1178{
1179 struct iattr attr;
1180 struct rpc_cred *cred;
1181 struct nfs4_state *state;
02a913a7 1182 struct dentry *res;
1da177e4
LT
1183
1184 if (nd->flags & LOOKUP_CREATE) {
1185 attr.ia_mode = nd->intent.open.create_mode;
1186 attr.ia_valid = ATTR_MODE;
1187 if (!IS_POSIXACL(dir))
1188 attr.ia_mode &= ~current->fs->umask;
1189 } else {
1190 attr.ia_valid = 0;
1191 BUG_ON(nd->intent.open.flags & O_CREAT);
1192 }
1193
1194 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1195 if (IS_ERR(cred))
02a913a7 1196 return (struct dentry *)cred;
1da177e4
LT
1197 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1198 put_rpccred(cred);
02a913a7
TM
1199 if (IS_ERR(state)) {
1200 if (PTR_ERR(state) == -ENOENT)
1201 d_add(dentry, NULL);
1202 return (struct dentry *)state;
1203 }
24ac23ab 1204 res = d_add_unique(dentry, igrab(state->inode));
02a913a7
TM
1205 if (res != NULL)
1206 dentry = res;
1207 nfs4_intent_set_file(nd, dentry, state);
1208 return res;
1da177e4
LT
1209}
1210
1211int
02a913a7 1212nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1da177e4
LT
1213{
1214 struct rpc_cred *cred;
1215 struct nfs4_state *state;
1da177e4
LT
1216
1217 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1218 if (IS_ERR(cred))
1219 return PTR_ERR(cred);
1220 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1221 if (IS_ERR(state))
1222 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1223 put_rpccred(cred);
02a913a7
TM
1224 if (IS_ERR(state)) {
1225 switch (PTR_ERR(state)) {
1226 case -EPERM:
1227 case -EACCES:
1228 case -EDQUOT:
1229 case -ENOSPC:
1230 case -EROFS:
1231 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1232 return 1;
1233 case -ENOENT:
1234 if (dentry->d_inode == NULL)
1235 return 1;
1236 }
1237 goto out_drop;
1238 }
24ac23ab 1239 if (state->inode == dentry->d_inode) {
02a913a7 1240 nfs4_intent_set_file(nd, dentry, state);
1da177e4
LT
1241 return 1;
1242 }
1da177e4 1243 nfs4_close_state(state, openflags);
02a913a7
TM
1244out_drop:
1245 d_drop(dentry);
1da177e4
LT
1246 return 0;
1247}
1248
1249
1250static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1251{
1252 struct nfs4_server_caps_res res = {};
1253 struct rpc_message msg = {
1254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1255 .rpc_argp = fhandle,
1256 .rpc_resp = &res,
1257 };
1258 int status;
1259
1260 status = rpc_call_sync(server->client, &msg, 0);
1261 if (status == 0) {
1262 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1263 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1264 server->caps |= NFS_CAP_ACLS;
1265 if (res.has_links != 0)
1266 server->caps |= NFS_CAP_HARDLINKS;
1267 if (res.has_symlinks != 0)
1268 server->caps |= NFS_CAP_SYMLINKS;
1269 server->acl_bitmask = res.acl_bitmask;
1270 }
1271 return status;
1272}
1273
1274static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1275{
1276 struct nfs4_exception exception = { };
1277 int err;
1278 do {
1279 err = nfs4_handle_exception(server,
1280 _nfs4_server_capabilities(server, fhandle),
1281 &exception);
1282 } while (exception.retry);
1283 return err;
1284}
1285
1286static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1287 struct nfs_fsinfo *info)
1288{
1da177e4
LT
1289 struct nfs4_lookup_root_arg args = {
1290 .bitmask = nfs4_fattr_bitmap,
1291 };
1292 struct nfs4_lookup_res res = {
1293 .server = server,
0e574af1 1294 .fattr = info->fattr,
1da177e4
LT
1295 .fh = fhandle,
1296 };
1297 struct rpc_message msg = {
1298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1299 .rpc_argp = &args,
1300 .rpc_resp = &res,
1301 };
0e574af1 1302 nfs_fattr_init(info->fattr);
1da177e4
LT
1303 return rpc_call_sync(server->client, &msg, 0);
1304}
1305
1306static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1307 struct nfs_fsinfo *info)
1308{
1309 struct nfs4_exception exception = { };
1310 int err;
1311 do {
1312 err = nfs4_handle_exception(server,
1313 _nfs4_lookup_root(server, fhandle, info),
1314 &exception);
1315 } while (exception.retry);
1316 return err;
1317}
1318
1319static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1320 struct nfs_fsinfo *info)
1321{
1322 struct nfs_fattr * fattr = info->fattr;
1323 unsigned char * p;
1324 struct qstr q;
1325 struct nfs4_lookup_arg args = {
1326 .dir_fh = fhandle,
1327 .name = &q,
1328 .bitmask = nfs4_fattr_bitmap,
1329 };
1330 struct nfs4_lookup_res res = {
1331 .server = server,
1332 .fattr = fattr,
1333 .fh = fhandle,
1334 };
1335 struct rpc_message msg = {
1336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1337 .rpc_argp = &args,
1338 .rpc_resp = &res,
1339 };
1340 int status;
1341
1342 /*
1343 * Now we do a separate LOOKUP for each component of the mount path.
1344 * The LOOKUPs are done separately so that we can conveniently
1345 * catch an ERR_WRONGSEC if it occurs along the way...
1346 */
1347 status = nfs4_lookup_root(server, fhandle, info);
1348 if (status)
1349 goto out;
1350
1351 p = server->mnt_path;
1352 for (;;) {
1353 struct nfs4_exception exception = { };
1354
1355 while (*p == '/')
1356 p++;
1357 if (!*p)
1358 break;
1359 q.name = p;
1360 while (*p && (*p != '/'))
1361 p++;
1362 q.len = p - q.name;
1363
1364 do {
0e574af1 1365 nfs_fattr_init(fattr);
1da177e4
LT
1366 status = nfs4_handle_exception(server,
1367 rpc_call_sync(server->client, &msg, 0),
1368 &exception);
1369 } while (exception.retry);
1370 if (status == 0)
1371 continue;
1372 if (status == -ENOENT) {
1373 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1374 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1375 }
1376 break;
1377 }
1378 if (status == 0)
1379 status = nfs4_server_capabilities(server, fhandle);
1380 if (status == 0)
1381 status = nfs4_do_fsinfo(server, fhandle, info);
1382out:
1383 return status;
1384}
1385
1386static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1387{
1388 struct nfs4_getattr_arg args = {
1389 .fh = fhandle,
1390 .bitmask = server->attr_bitmask,
1391 };
1392 struct nfs4_getattr_res res = {
1393 .fattr = fattr,
1394 .server = server,
1395 };
1396 struct rpc_message msg = {
1397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1398 .rpc_argp = &args,
1399 .rpc_resp = &res,
1400 };
1401
0e574af1 1402 nfs_fattr_init(fattr);
1da177e4
LT
1403 return rpc_call_sync(server->client, &msg, 0);
1404}
1405
1406static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1407{
1408 struct nfs4_exception exception = { };
1409 int err;
1410 do {
1411 err = nfs4_handle_exception(server,
1412 _nfs4_proc_getattr(server, fhandle, fattr),
1413 &exception);
1414 } while (exception.retry);
1415 return err;
1416}
1417
1418/*
1419 * The file is not closed if it is opened due to the a request to change
1420 * the size of the file. The open call will not be needed once the
1421 * VFS layer lookup-intents are implemented.
1422 *
1423 * Close is called when the inode is destroyed.
1424 * If we haven't opened the file for O_WRONLY, we
1425 * need to in the size_change case to obtain a stateid.
1426 *
1427 * Got race?
1428 * Because OPEN is always done by name in nfsv4, it is
1429 * possible that we opened a different file by the same
1430 * name. We can recognize this race condition, but we
1431 * can't do anything about it besides returning an error.
1432 *
1433 * This will be fixed with VFS changes (lookup-intent).
1434 */
1435static int
1436nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1437 struct iattr *sattr)
1438{
08e9eac4
TM
1439 struct rpc_cred *cred;
1440 struct inode *inode = dentry->d_inode;
d530838b
TM
1441 struct nfs_open_context *ctx;
1442 struct nfs4_state *state = NULL;
1da177e4
LT
1443 int status;
1444
0e574af1 1445 nfs_fattr_init(fattr);
1da177e4 1446
08e9eac4
TM
1447 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1448 if (IS_ERR(cred))
1449 return PTR_ERR(cred);
d530838b
TM
1450
1451 /* Search for an existing open(O_WRITE) file */
1452 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1453 if (ctx != NULL)
1454 state = ctx->state;
08e9eac4 1455
1da177e4
LT
1456 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1457 NFS_FH(inode), sattr, state);
65e4308d
TM
1458 if (status == 0)
1459 nfs_setattr_update_inode(inode, sattr);
d530838b
TM
1460 if (ctx != NULL)
1461 put_nfs_open_context(ctx);
08e9eac4 1462 put_rpccred(cred);
1da177e4
LT
1463 return status;
1464}
1465
1466static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1467 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1468{
1469 int status;
1470 struct nfs_server *server = NFS_SERVER(dir);
1471 struct nfs4_lookup_arg args = {
1472 .bitmask = server->attr_bitmask,
1473 .dir_fh = NFS_FH(dir),
1474 .name = name,
1475 };
1476 struct nfs4_lookup_res res = {
1477 .server = server,
1478 .fattr = fattr,
1479 .fh = fhandle,
1480 };
1481 struct rpc_message msg = {
1482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1483 .rpc_argp = &args,
1484 .rpc_resp = &res,
1485 };
1486
0e574af1 1487 nfs_fattr_init(fattr);
1da177e4
LT
1488
1489 dprintk("NFS call lookup %s\n", name->name);
1490 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1491 dprintk("NFS reply lookup: %d\n", status);
1492 return status;
1493}
1494
1495static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1496{
1497 struct nfs4_exception exception = { };
1498 int err;
1499 do {
1500 err = nfs4_handle_exception(NFS_SERVER(dir),
1501 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1502 &exception);
1503 } while (exception.retry);
1504 return err;
1505}
1506
1507static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1508{
1509 struct nfs4_accessargs args = {
1510 .fh = NFS_FH(inode),
1511 };
1512 struct nfs4_accessres res = { 0 };
1513 struct rpc_message msg = {
1514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1515 .rpc_argp = &args,
1516 .rpc_resp = &res,
1517 .rpc_cred = entry->cred,
1518 };
1519 int mode = entry->mask;
1520 int status;
1521
1522 /*
1523 * Determine which access bits we want to ask for...
1524 */
1525 if (mode & MAY_READ)
1526 args.access |= NFS4_ACCESS_READ;
1527 if (S_ISDIR(inode->i_mode)) {
1528 if (mode & MAY_WRITE)
1529 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1530 if (mode & MAY_EXEC)
1531 args.access |= NFS4_ACCESS_LOOKUP;
1532 } else {
1533 if (mode & MAY_WRITE)
1534 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1535 if (mode & MAY_EXEC)
1536 args.access |= NFS4_ACCESS_EXECUTE;
1537 }
1538 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1539 if (!status) {
1540 entry->mask = 0;
1541 if (res.access & NFS4_ACCESS_READ)
1542 entry->mask |= MAY_READ;
1543 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1544 entry->mask |= MAY_WRITE;
1545 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1546 entry->mask |= MAY_EXEC;
1547 }
1548 return status;
1549}
1550
1551static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1552{
1553 struct nfs4_exception exception = { };
1554 int err;
1555 do {
1556 err = nfs4_handle_exception(NFS_SERVER(inode),
1557 _nfs4_proc_access(inode, entry),
1558 &exception);
1559 } while (exception.retry);
1560 return err;
1561}
1562
1563/*
1564 * TODO: For the time being, we don't try to get any attributes
1565 * along with any of the zero-copy operations READ, READDIR,
1566 * READLINK, WRITE.
1567 *
1568 * In the case of the first three, we want to put the GETATTR
1569 * after the read-type operation -- this is because it is hard
1570 * to predict the length of a GETATTR response in v4, and thus
1571 * align the READ data correctly. This means that the GETATTR
1572 * may end up partially falling into the page cache, and we should
1573 * shift it into the 'tail' of the xdr_buf before processing.
1574 * To do this efficiently, we need to know the total length
1575 * of data received, which doesn't seem to be available outside
1576 * of the RPC layer.
1577 *
1578 * In the case of WRITE, we also want to put the GETATTR after
1579 * the operation -- in this case because we want to make sure
1580 * we get the post-operation mtime and size. This means that
1581 * we can't use xdr_encode_pages() as written: we need a variant
1582 * of it which would leave room in the 'tail' iovec.
1583 *
1584 * Both of these changes to the XDR layer would in fact be quite
1585 * minor, but I decided to leave them for a subsequent patch.
1586 */
1587static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1588 unsigned int pgbase, unsigned int pglen)
1589{
1590 struct nfs4_readlink args = {
1591 .fh = NFS_FH(inode),
1592 .pgbase = pgbase,
1593 .pglen = pglen,
1594 .pages = &page,
1595 };
1596 struct rpc_message msg = {
1597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1598 .rpc_argp = &args,
1599 .rpc_resp = NULL,
1600 };
1601
1602 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1603}
1604
1605static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1606 unsigned int pgbase, unsigned int pglen)
1607{
1608 struct nfs4_exception exception = { };
1609 int err;
1610 do {
1611 err = nfs4_handle_exception(NFS_SERVER(inode),
1612 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1613 &exception);
1614 } while (exception.retry);
1615 return err;
1616}
1617
1618static int _nfs4_proc_read(struct nfs_read_data *rdata)
1619{
1620 int flags = rdata->flags;
1621 struct inode *inode = rdata->inode;
1622 struct nfs_fattr *fattr = rdata->res.fattr;
1623 struct nfs_server *server = NFS_SERVER(inode);
1624 struct rpc_message msg = {
1625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1626 .rpc_argp = &rdata->args,
1627 .rpc_resp = &rdata->res,
1628 .rpc_cred = rdata->cred,
1629 };
1630 unsigned long timestamp = jiffies;
1631 int status;
1632
1633 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1634 (long long) rdata->args.offset);
1635
0e574af1 1636 nfs_fattr_init(fattr);
1da177e4
LT
1637 status = rpc_call_sync(server->client, &msg, flags);
1638 if (!status)
1639 renew_lease(server, timestamp);
1640 dprintk("NFS reply read: %d\n", status);
1641 return status;
1642}
1643
1644static int nfs4_proc_read(struct nfs_read_data *rdata)
1645{
1646 struct nfs4_exception exception = { };
1647 int err;
1648 do {
1649 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1650 _nfs4_proc_read(rdata),
1651 &exception);
1652 } while (exception.retry);
1653 return err;
1654}
1655
1656static int _nfs4_proc_write(struct nfs_write_data *wdata)
1657{
1658 int rpcflags = wdata->flags;
1659 struct inode *inode = wdata->inode;
1660 struct nfs_fattr *fattr = wdata->res.fattr;
1661 struct nfs_server *server = NFS_SERVER(inode);
1662 struct rpc_message msg = {
1663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1664 .rpc_argp = &wdata->args,
1665 .rpc_resp = &wdata->res,
1666 .rpc_cred = wdata->cred,
1667 };
1668 int status;
1669
1670 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1671 (long long) wdata->args.offset);
1672
3b6efee9
TM
1673 wdata->args.bitmask = server->attr_bitmask;
1674 wdata->res.server = server;
0e574af1 1675 nfs_fattr_init(fattr);
1da177e4
LT
1676 status = rpc_call_sync(server->client, &msg, rpcflags);
1677 dprintk("NFS reply write: %d\n", status);
3b6efee9
TM
1678 if (status < 0)
1679 return status;
1680 nfs_post_op_update_inode(inode, fattr);
1681 return wdata->res.count;
1da177e4
LT
1682}
1683
1684static int nfs4_proc_write(struct nfs_write_data *wdata)
1685{
1686 struct nfs4_exception exception = { };
1687 int err;
1688 do {
1689 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1690 _nfs4_proc_write(wdata),
1691 &exception);
1692 } while (exception.retry);
1693 return err;
1694}
1695
1696static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1697{
1698 struct inode *inode = cdata->inode;
1699 struct nfs_fattr *fattr = cdata->res.fattr;
1700 struct nfs_server *server = NFS_SERVER(inode);
1701 struct rpc_message msg = {
1702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1703 .rpc_argp = &cdata->args,
1704 .rpc_resp = &cdata->res,
1705 .rpc_cred = cdata->cred,
1706 };
1707 int status;
1708
1709 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1710 (long long) cdata->args.offset);
1711
3b6efee9
TM
1712 cdata->args.bitmask = server->attr_bitmask;
1713 cdata->res.server = server;
0e574af1 1714 nfs_fattr_init(fattr);
1da177e4
LT
1715 status = rpc_call_sync(server->client, &msg, 0);
1716 dprintk("NFS reply commit: %d\n", status);
3b6efee9
TM
1717 if (status >= 0)
1718 nfs_post_op_update_inode(inode, fattr);
1da177e4
LT
1719 return status;
1720}
1721
1722static int nfs4_proc_commit(struct nfs_write_data *cdata)
1723{
1724 struct nfs4_exception exception = { };
1725 int err;
1726 do {
1727 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1728 _nfs4_proc_commit(cdata),
1729 &exception);
1730 } while (exception.retry);
1731 return err;
1732}
1733
1734/*
1735 * Got race?
1736 * We will need to arrange for the VFS layer to provide an atomic open.
1737 * Until then, this create/open method is prone to inefficiency and race
1738 * conditions due to the lookup, create, and open VFS calls from sys_open()
1739 * placed on the wire.
1740 *
1741 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1742 * The file will be opened again in the subsequent VFS open call
1743 * (nfs4_proc_file_open).
1744 *
1745 * The open for read will just hang around to be used by any process that
1746 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1747 */
1748
1749static int
1750nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
02a913a7 1751 int flags, struct nameidata *nd)
1da177e4
LT
1752{
1753 struct nfs4_state *state;
1754 struct rpc_cred *cred;
1755 int status = 0;
1756
1757 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1758 if (IS_ERR(cred)) {
1759 status = PTR_ERR(cred);
1760 goto out;
1761 }
1762 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1763 put_rpccred(cred);
1764 if (IS_ERR(state)) {
1765 status = PTR_ERR(state);
1766 goto out;
1767 }
24ac23ab 1768 d_instantiate(dentry, igrab(state->inode));
1da177e4
LT
1769 if (flags & O_EXCL) {
1770 struct nfs_fattr fattr;
1771 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1772 NFS_FH(state->inode), sattr, state);
02a913a7 1773 if (status == 0)
65e4308d 1774 nfs_setattr_update_inode(state->inode, sattr);
02a913a7
TM
1775 }
1776 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1777 nfs4_intent_set_file(nd, dentry, state);
1778 else
1779 nfs4_close_state(state, flags);
1da177e4
LT
1780out:
1781 return status;
1782}
1783
1784static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1785{
16e42959 1786 struct nfs_server *server = NFS_SERVER(dir);
1da177e4
LT
1787 struct nfs4_remove_arg args = {
1788 .fh = NFS_FH(dir),
1789 .name = name,
16e42959
TM
1790 .bitmask = server->attr_bitmask,
1791 };
1792 struct nfs_fattr dir_attr;
1793 struct nfs4_remove_res res = {
1794 .server = server,
1795 .dir_attr = &dir_attr,
1da177e4 1796 };
1da177e4
LT
1797 struct rpc_message msg = {
1798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1799 .rpc_argp = &args,
1800 .rpc_resp = &res,
1801 };
1802 int status;
1803
16e42959
TM
1804 nfs_fattr_init(res.dir_attr);
1805 status = rpc_call_sync(server->client, &msg, 0);
1806 if (status == 0) {
1807 update_changeattr(dir, &res.cinfo);
1808 nfs_post_op_update_inode(dir, res.dir_attr);
1809 }
1da177e4
LT
1810 return status;
1811}
1812
1813static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1814{
1815 struct nfs4_exception exception = { };
1816 int err;
1817 do {
1818 err = nfs4_handle_exception(NFS_SERVER(dir),
1819 _nfs4_proc_remove(dir, name),
1820 &exception);
1821 } while (exception.retry);
1822 return err;
1823}
1824
1825struct unlink_desc {
1826 struct nfs4_remove_arg args;
16e42959
TM
1827 struct nfs4_remove_res res;
1828 struct nfs_fattr dir_attr;
1da177e4
LT
1829};
1830
1831static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1832 struct qstr *name)
1833{
16e42959 1834 struct nfs_server *server = NFS_SERVER(dir->d_inode);
1da177e4
LT
1835 struct unlink_desc *up;
1836
1837 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1838 if (!up)
1839 return -ENOMEM;
1840
1841 up->args.fh = NFS_FH(dir->d_inode);
1842 up->args.name = name;
16e42959
TM
1843 up->args.bitmask = server->attr_bitmask;
1844 up->res.server = server;
1845 up->res.dir_attr = &up->dir_attr;
1da177e4
LT
1846
1847 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1848 msg->rpc_argp = &up->args;
1849 msg->rpc_resp = &up->res;
1850 return 0;
1851}
1852
1853static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1854{
1855 struct rpc_message *msg = &task->tk_msg;
1856 struct unlink_desc *up;
1857
1858 if (msg->rpc_resp != NULL) {
1859 up = container_of(msg->rpc_resp, struct unlink_desc, res);
16e42959
TM
1860 update_changeattr(dir->d_inode, &up->res.cinfo);
1861 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
1da177e4
LT
1862 kfree(up);
1863 msg->rpc_resp = NULL;
1864 msg->rpc_argp = NULL;
1865 }
1866 return 0;
1867}
1868
1869static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1870 struct inode *new_dir, struct qstr *new_name)
1871{
6caf2c82 1872 struct nfs_server *server = NFS_SERVER(old_dir);
1da177e4
LT
1873 struct nfs4_rename_arg arg = {
1874 .old_dir = NFS_FH(old_dir),
1875 .new_dir = NFS_FH(new_dir),
1876 .old_name = old_name,
1877 .new_name = new_name,
6caf2c82
TM
1878 .bitmask = server->attr_bitmask,
1879 };
1880 struct nfs_fattr old_fattr, new_fattr;
1881 struct nfs4_rename_res res = {
1882 .server = server,
1883 .old_fattr = &old_fattr,
1884 .new_fattr = &new_fattr,
1da177e4 1885 };
1da177e4
LT
1886 struct rpc_message msg = {
1887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1888 .rpc_argp = &arg,
1889 .rpc_resp = &res,
1890 };
1891 int status;
1892
6caf2c82
TM
1893 nfs_fattr_init(res.old_fattr);
1894 nfs_fattr_init(res.new_fattr);
1895 status = rpc_call_sync(server->client, &msg, 0);
1da177e4
LT
1896
1897 if (!status) {
1898 update_changeattr(old_dir, &res.old_cinfo);
6caf2c82 1899 nfs_post_op_update_inode(old_dir, res.old_fattr);
1da177e4 1900 update_changeattr(new_dir, &res.new_cinfo);
6caf2c82 1901 nfs_post_op_update_inode(new_dir, res.new_fattr);
1da177e4
LT
1902 }
1903 return status;
1904}
1905
1906static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1907 struct inode *new_dir, struct qstr *new_name)
1908{
1909 struct nfs4_exception exception = { };
1910 int err;
1911 do {
1912 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1913 _nfs4_proc_rename(old_dir, old_name,
1914 new_dir, new_name),
1915 &exception);
1916 } while (exception.retry);
1917 return err;
1918}
1919
1920static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1921{
91ba2eee 1922 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
1923 struct nfs4_link_arg arg = {
1924 .fh = NFS_FH(inode),
1925 .dir_fh = NFS_FH(dir),
1926 .name = name,
91ba2eee
TM
1927 .bitmask = server->attr_bitmask,
1928 };
1929 struct nfs_fattr fattr, dir_attr;
1930 struct nfs4_link_res res = {
1931 .server = server,
1932 .fattr = &fattr,
1933 .dir_attr = &dir_attr,
1da177e4 1934 };
1da177e4
LT
1935 struct rpc_message msg = {
1936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1937 .rpc_argp = &arg,
91ba2eee 1938 .rpc_resp = &res,
1da177e4
LT
1939 };
1940 int status;
1941
91ba2eee
TM
1942 nfs_fattr_init(res.fattr);
1943 nfs_fattr_init(res.dir_attr);
1944 status = rpc_call_sync(server->client, &msg, 0);
1945 if (!status) {
1946 update_changeattr(dir, &res.cinfo);
1947 nfs_post_op_update_inode(dir, res.dir_attr);
1948 nfs_refresh_inode(inode, res.fattr);
1949 }
1da177e4
LT
1950
1951 return status;
1952}
1953
1954static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1955{
1956 struct nfs4_exception exception = { };
1957 int err;
1958 do {
1959 err = nfs4_handle_exception(NFS_SERVER(inode),
1960 _nfs4_proc_link(inode, dir, name),
1961 &exception);
1962 } while (exception.retry);
1963 return err;
1964}
1965
1966static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1967 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1968 struct nfs_fattr *fattr)
1969{
1970 struct nfs_server *server = NFS_SERVER(dir);
56ae19f3 1971 struct nfs_fattr dir_fattr;
1da177e4
LT
1972 struct nfs4_create_arg arg = {
1973 .dir_fh = NFS_FH(dir),
1974 .server = server,
1975 .name = name,
1976 .attrs = sattr,
1977 .ftype = NF4LNK,
1978 .bitmask = server->attr_bitmask,
1979 };
1980 struct nfs4_create_res res = {
1981 .server = server,
1982 .fh = fhandle,
1983 .fattr = fattr,
56ae19f3 1984 .dir_fattr = &dir_fattr,
1da177e4
LT
1985 };
1986 struct rpc_message msg = {
1987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1988 .rpc_argp = &arg,
1989 .rpc_resp = &res,
1990 };
1991 int status;
1992
1993 if (path->len > NFS4_MAXPATHLEN)
1994 return -ENAMETOOLONG;
1995 arg.u.symlink = path;
0e574af1 1996 nfs_fattr_init(fattr);
56ae19f3 1997 nfs_fattr_init(&dir_fattr);
1da177e4
LT
1998
1999 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2000 if (!status)
2001 update_changeattr(dir, &res.dir_cinfo);
56ae19f3 2002 nfs_post_op_update_inode(dir, res.dir_fattr);
1da177e4
LT
2003 return status;
2004}
2005
2006static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
2007 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
2008 struct nfs_fattr *fattr)
2009{
2010 struct nfs4_exception exception = { };
2011 int err;
2012 do {
2013 err = nfs4_handle_exception(NFS_SERVER(dir),
2014 _nfs4_proc_symlink(dir, name, path, sattr,
2015 fhandle, fattr),
2016 &exception);
2017 } while (exception.retry);
2018 return err;
2019}
2020
2021static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2022 struct iattr *sattr)
2023{
2024 struct nfs_server *server = NFS_SERVER(dir);
2025 struct nfs_fh fhandle;
56ae19f3 2026 struct nfs_fattr fattr, dir_fattr;
1da177e4
LT
2027 struct nfs4_create_arg arg = {
2028 .dir_fh = NFS_FH(dir),
2029 .server = server,
2030 .name = &dentry->d_name,
2031 .attrs = sattr,
2032 .ftype = NF4DIR,
2033 .bitmask = server->attr_bitmask,
2034 };
2035 struct nfs4_create_res res = {
2036 .server = server,
2037 .fh = &fhandle,
2038 .fattr = &fattr,
56ae19f3 2039 .dir_fattr = &dir_fattr,
1da177e4
LT
2040 };
2041 struct rpc_message msg = {
2042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2043 .rpc_argp = &arg,
2044 .rpc_resp = &res,
2045 };
2046 int status;
2047
0e574af1 2048 nfs_fattr_init(&fattr);
56ae19f3 2049 nfs_fattr_init(&dir_fattr);
1da177e4
LT
2050
2051 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2052 if (!status) {
2053 update_changeattr(dir, &res.dir_cinfo);
56ae19f3 2054 nfs_post_op_update_inode(dir, res.dir_fattr);
1da177e4
LT
2055 status = nfs_instantiate(dentry, &fhandle, &fattr);
2056 }
2057 return status;
2058}
2059
2060static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2061 struct iattr *sattr)
2062{
2063 struct nfs4_exception exception = { };
2064 int err;
2065 do {
2066 err = nfs4_handle_exception(NFS_SERVER(dir),
2067 _nfs4_proc_mkdir(dir, dentry, sattr),
2068 &exception);
2069 } while (exception.retry);
2070 return err;
2071}
2072
2073static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2074 u64 cookie, struct page *page, unsigned int count, int plus)
2075{
2076 struct inode *dir = dentry->d_inode;
2077 struct nfs4_readdir_arg args = {
2078 .fh = NFS_FH(dir),
2079 .pages = &page,
2080 .pgbase = 0,
2081 .count = count,
2082 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2083 };
2084 struct nfs4_readdir_res res;
2085 struct rpc_message msg = {
2086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2087 .rpc_argp = &args,
2088 .rpc_resp = &res,
2089 .rpc_cred = cred,
2090 };
2091 int status;
2092
eadf4598
TM
2093 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2094 dentry->d_parent->d_name.name,
2095 dentry->d_name.name,
2096 (unsigned long long)cookie);
1da177e4
LT
2097 lock_kernel();
2098 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2099 res.pgbase = args.pgbase;
2100 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2101 if (status == 0)
2102 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2103 unlock_kernel();
eadf4598 2104 dprintk("%s: returns %d\n", __FUNCTION__, status);
1da177e4
LT
2105 return status;
2106}
2107
2108static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2109 u64 cookie, struct page *page, unsigned int count, int plus)
2110{
2111 struct nfs4_exception exception = { };
2112 int err;
2113 do {
2114 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2115 _nfs4_proc_readdir(dentry, cred, cookie,
2116 page, count, plus),
2117 &exception);
2118 } while (exception.retry);
2119 return err;
2120}
2121
2122static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2123 struct iattr *sattr, dev_t rdev)
2124{
2125 struct nfs_server *server = NFS_SERVER(dir);
2126 struct nfs_fh fh;
56ae19f3 2127 struct nfs_fattr fattr, dir_fattr;
1da177e4
LT
2128 struct nfs4_create_arg arg = {
2129 .dir_fh = NFS_FH(dir),
2130 .server = server,
2131 .name = &dentry->d_name,
2132 .attrs = sattr,
2133 .bitmask = server->attr_bitmask,
2134 };
2135 struct nfs4_create_res res = {
2136 .server = server,
2137 .fh = &fh,
2138 .fattr = &fattr,
56ae19f3 2139 .dir_fattr = &dir_fattr,
1da177e4
LT
2140 };
2141 struct rpc_message msg = {
2142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2143 .rpc_argp = &arg,
2144 .rpc_resp = &res,
2145 };
2146 int status;
2147 int mode = sattr->ia_mode;
2148
0e574af1 2149 nfs_fattr_init(&fattr);
56ae19f3 2150 nfs_fattr_init(&dir_fattr);
1da177e4
LT
2151
2152 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2153 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2154 if (S_ISFIFO(mode))
2155 arg.ftype = NF4FIFO;
2156 else if (S_ISBLK(mode)) {
2157 arg.ftype = NF4BLK;
2158 arg.u.device.specdata1 = MAJOR(rdev);
2159 arg.u.device.specdata2 = MINOR(rdev);
2160 }
2161 else if (S_ISCHR(mode)) {
2162 arg.ftype = NF4CHR;
2163 arg.u.device.specdata1 = MAJOR(rdev);
2164 arg.u.device.specdata2 = MINOR(rdev);
2165 }
2166 else
2167 arg.ftype = NF4SOCK;
2168
2169 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2170 if (status == 0) {
2171 update_changeattr(dir, &res.dir_cinfo);
56ae19f3 2172 nfs_post_op_update_inode(dir, res.dir_fattr);
1da177e4
LT
2173 status = nfs_instantiate(dentry, &fh, &fattr);
2174 }
2175 return status;
2176}
2177
2178static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2179 struct iattr *sattr, dev_t rdev)
2180{
2181 struct nfs4_exception exception = { };
2182 int err;
2183 do {
2184 err = nfs4_handle_exception(NFS_SERVER(dir),
2185 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2186 &exception);
2187 } while (exception.retry);
2188 return err;
2189}
2190
2191static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2192 struct nfs_fsstat *fsstat)
2193{
2194 struct nfs4_statfs_arg args = {
2195 .fh = fhandle,
2196 .bitmask = server->attr_bitmask,
2197 };
2198 struct rpc_message msg = {
2199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2200 .rpc_argp = &args,
2201 .rpc_resp = fsstat,
2202 };
2203
0e574af1 2204 nfs_fattr_init(fsstat->fattr);
1da177e4
LT
2205 return rpc_call_sync(server->client, &msg, 0);
2206}
2207
2208static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2209{
2210 struct nfs4_exception exception = { };
2211 int err;
2212 do {
2213 err = nfs4_handle_exception(server,
2214 _nfs4_proc_statfs(server, fhandle, fsstat),
2215 &exception);
2216 } while (exception.retry);
2217 return err;
2218}
2219
2220static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsinfo *fsinfo)
2222{
2223 struct nfs4_fsinfo_arg args = {
2224 .fh = fhandle,
2225 .bitmask = server->attr_bitmask,
2226 };
2227 struct rpc_message msg = {
2228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2229 .rpc_argp = &args,
2230 .rpc_resp = fsinfo,
2231 };
2232
2233 return rpc_call_sync(server->client, &msg, 0);
2234}
2235
2236static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2237{
2238 struct nfs4_exception exception = { };
2239 int err;
2240
2241 do {
2242 err = nfs4_handle_exception(server,
2243 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2244 &exception);
2245 } while (exception.retry);
2246 return err;
2247}
2248
2249static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2250{
0e574af1 2251 nfs_fattr_init(fsinfo->fattr);
1da177e4
LT
2252 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2253}
2254
2255static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2256 struct nfs_pathconf *pathconf)
2257{
2258 struct nfs4_pathconf_arg args = {
2259 .fh = fhandle,
2260 .bitmask = server->attr_bitmask,
2261 };
2262 struct rpc_message msg = {
2263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2264 .rpc_argp = &args,
2265 .rpc_resp = pathconf,
2266 };
2267
2268 /* None of the pathconf attributes are mandatory to implement */
2269 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2270 memset(pathconf, 0, sizeof(*pathconf));
2271 return 0;
2272 }
2273
0e574af1 2274 nfs_fattr_init(pathconf->fattr);
1da177e4
LT
2275 return rpc_call_sync(server->client, &msg, 0);
2276}
2277
2278static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2279 struct nfs_pathconf *pathconf)
2280{
2281 struct nfs4_exception exception = { };
2282 int err;
2283
2284 do {
2285 err = nfs4_handle_exception(server,
2286 _nfs4_proc_pathconf(server, fhandle, pathconf),
2287 &exception);
2288 } while (exception.retry);
2289 return err;
2290}
2291
963d8fe5 2292static void nfs4_read_done(struct rpc_task *task, void *calldata)
1da177e4 2293{
963d8fe5 2294 struct nfs_read_data *data = calldata;
1da177e4
LT
2295 struct inode *inode = data->inode;
2296
2297 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2298 rpc_restart_call(task);
2299 return;
2300 }
2301 if (task->tk_status > 0)
2302 renew_lease(NFS_SERVER(inode), data->timestamp);
2303 /* Call back common NFS readpage processing */
963d8fe5 2304 nfs_readpage_result(task, calldata);
1da177e4
LT
2305}
2306
963d8fe5
TM
2307static const struct rpc_call_ops nfs4_read_ops = {
2308 .rpc_call_done = nfs4_read_done,
2309 .rpc_release = nfs_readdata_release,
2310};
2311
1da177e4
LT
2312static void
2313nfs4_proc_read_setup(struct nfs_read_data *data)
2314{
2315 struct rpc_task *task = &data->task;
2316 struct rpc_message msg = {
2317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2318 .rpc_argp = &data->args,
2319 .rpc_resp = &data->res,
2320 .rpc_cred = data->cred,
2321 };
2322 struct inode *inode = data->inode;
2323 int flags;
2324
2325 data->timestamp = jiffies;
2326
2327 /* N.B. Do we need to test? Never called for swapfile inode */
2328 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2329
2330 /* Finalize the task. */
963d8fe5 2331 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
1da177e4
LT
2332 rpc_call_setup(task, &msg, 0);
2333}
2334
963d8fe5 2335static void nfs4_write_done(struct rpc_task *task, void *calldata)
1da177e4 2336{
963d8fe5 2337 struct nfs_write_data *data = calldata;
1da177e4
LT
2338 struct inode *inode = data->inode;
2339
2340 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2341 rpc_restart_call(task);
2342 return;
2343 }
4f9838c7 2344 if (task->tk_status >= 0) {
1da177e4 2345 renew_lease(NFS_SERVER(inode), data->timestamp);
4f9838c7
TM
2346 nfs_post_op_update_inode(inode, data->res.fattr);
2347 }
1da177e4 2348 /* Call back common NFS writeback processing */
963d8fe5 2349 nfs_writeback_done(task, calldata);
1da177e4
LT
2350}
2351
963d8fe5
TM
2352static const struct rpc_call_ops nfs4_write_ops = {
2353 .rpc_call_done = nfs4_write_done,
2354 .rpc_release = nfs_writedata_release,
2355};
2356
1da177e4
LT
2357static void
2358nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2359{
2360 struct rpc_task *task = &data->task;
2361 struct rpc_message msg = {
2362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2363 .rpc_argp = &data->args,
2364 .rpc_resp = &data->res,
2365 .rpc_cred = data->cred,
2366 };
2367 struct inode *inode = data->inode;
4f9838c7 2368 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
2369 int stable;
2370 int flags;
2371
2372 if (how & FLUSH_STABLE) {
2373 if (!NFS_I(inode)->ncommit)
2374 stable = NFS_FILE_SYNC;
2375 else
2376 stable = NFS_DATA_SYNC;
2377 } else
2378 stable = NFS_UNSTABLE;
2379 data->args.stable = stable;
4f9838c7
TM
2380 data->args.bitmask = server->attr_bitmask;
2381 data->res.server = server;
1da177e4
LT
2382
2383 data->timestamp = jiffies;
2384
2385 /* Set the initial flags for the task. */
2386 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2387
2388 /* Finalize the task. */
963d8fe5 2389 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
1da177e4
LT
2390 rpc_call_setup(task, &msg, 0);
2391}
2392
963d8fe5 2393static void nfs4_commit_done(struct rpc_task *task, void *calldata)
1da177e4 2394{
963d8fe5 2395 struct nfs_write_data *data = calldata;
1da177e4
LT
2396 struct inode *inode = data->inode;
2397
2398 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2399 rpc_restart_call(task);
2400 return;
2401 }
4f9838c7
TM
2402 if (task->tk_status >= 0)
2403 nfs_post_op_update_inode(inode, data->res.fattr);
1da177e4 2404 /* Call back common NFS writeback processing */
963d8fe5 2405 nfs_commit_done(task, calldata);
1da177e4
LT
2406}
2407
963d8fe5
TM
2408static const struct rpc_call_ops nfs4_commit_ops = {
2409 .rpc_call_done = nfs4_commit_done,
2410 .rpc_release = nfs_commit_release,
2411};
2412
1da177e4
LT
2413static void
2414nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2415{
2416 struct rpc_task *task = &data->task;
2417 struct rpc_message msg = {
2418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2419 .rpc_argp = &data->args,
2420 .rpc_resp = &data->res,
2421 .rpc_cred = data->cred,
2422 };
2423 struct inode *inode = data->inode;
4f9838c7 2424 struct nfs_server *server = NFS_SERVER(inode);
1da177e4
LT
2425 int flags;
2426
4f9838c7
TM
2427 data->args.bitmask = server->attr_bitmask;
2428 data->res.server = server;
2429
1da177e4
LT
2430 /* Set the initial flags for the task. */
2431 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2432
2433 /* Finalize the task. */
963d8fe5 2434 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
1da177e4
LT
2435 rpc_call_setup(task, &msg, 0);
2436}
2437
2438/*
2439 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2440 * standalone procedure for queueing an asynchronous RENEW.
2441 */
963d8fe5 2442static void nfs4_renew_done(struct rpc_task *task, void *data)
1da177e4
LT
2443{
2444 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
963d8fe5 2445 unsigned long timestamp = (unsigned long)data;
1da177e4
LT
2446
2447 if (task->tk_status < 0) {
2448 switch (task->tk_status) {
2449 case -NFS4ERR_STALE_CLIENTID:
2450 case -NFS4ERR_EXPIRED:
2451 case -NFS4ERR_CB_PATH_DOWN:
2452 nfs4_schedule_state_recovery(clp);
2453 }
2454 return;
2455 }
2456 spin_lock(&clp->cl_lock);
2457 if (time_before(clp->cl_last_renewal,timestamp))
2458 clp->cl_last_renewal = timestamp;
2459 spin_unlock(&clp->cl_lock);
2460}
2461
963d8fe5
TM
2462static const struct rpc_call_ops nfs4_renew_ops = {
2463 .rpc_call_done = nfs4_renew_done,
2464};
2465
1da177e4
LT
2466int
2467nfs4_proc_async_renew(struct nfs4_client *clp)
2468{
2469 struct rpc_message msg = {
2470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2471 .rpc_argp = clp,
2472 .rpc_cred = clp->cl_cred,
2473 };
2474
2475 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
963d8fe5 2476 &nfs4_renew_ops, (void *)jiffies);
1da177e4
LT
2477}
2478
2479int
2480nfs4_proc_renew(struct nfs4_client *clp)
2481{
2482 struct rpc_message msg = {
2483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2484 .rpc_argp = clp,
2485 .rpc_cred = clp->cl_cred,
2486 };
2487 unsigned long now = jiffies;
2488 int status;
2489
2490 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2491 if (status < 0)
2492 return status;
2493 spin_lock(&clp->cl_lock);
2494 if (time_before(clp->cl_last_renewal,now))
2495 clp->cl_last_renewal = now;
2496 spin_unlock(&clp->cl_lock);
2497 return 0;
2498}
2499
aa1870af
BF
2500static inline int nfs4_server_supports_acls(struct nfs_server *server)
2501{
2502 return (server->caps & NFS_CAP_ACLS)
2503 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2504 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2505}
2506
2507/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2508 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2509 * the stack.
2510 */
2511#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2512
2513static void buf_to_pages(const void *buf, size_t buflen,
2514 struct page **pages, unsigned int *pgbase)
2515{
2516 const void *p = buf;
2517
2518 *pgbase = offset_in_page(buf);
2519 p -= *pgbase;
2520 while (p < buf + buflen) {
2521 *(pages++) = virt_to_page(p);
2522 p += PAGE_CACHE_SIZE;
2523 }
2524}
2525
e50a1c2e
BF
2526struct nfs4_cached_acl {
2527 int cached;
2528 size_t len;
3e9d4154 2529 char data[0];
e50a1c2e
BF
2530};
2531
2532static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2533{
2534 struct nfs_inode *nfsi = NFS_I(inode);
2535
2536 spin_lock(&inode->i_lock);
2537 kfree(nfsi->nfs4_acl);
2538 nfsi->nfs4_acl = acl;
2539 spin_unlock(&inode->i_lock);
2540}
2541
2542static void nfs4_zap_acl_attr(struct inode *inode)
2543{
2544 nfs4_set_cached_acl(inode, NULL);
2545}
2546
2547static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2548{
2549 struct nfs_inode *nfsi = NFS_I(inode);
2550 struct nfs4_cached_acl *acl;
2551 int ret = -ENOENT;
2552
2553 spin_lock(&inode->i_lock);
2554 acl = nfsi->nfs4_acl;
2555 if (acl == NULL)
2556 goto out;
2557 if (buf == NULL) /* user is just asking for length */
2558 goto out_len;
2559 if (acl->cached == 0)
2560 goto out;
2561 ret = -ERANGE; /* see getxattr(2) man page */
2562 if (acl->len > buflen)
2563 goto out;
2564 memcpy(buf, acl->data, acl->len);
2565out_len:
2566 ret = acl->len;
2567out:
2568 spin_unlock(&inode->i_lock);
2569 return ret;
2570}
2571
2572static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2573{
2574 struct nfs4_cached_acl *acl;
2575
2576 if (buf && acl_len <= PAGE_SIZE) {
2577 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2578 if (acl == NULL)
2579 goto out;
2580 acl->cached = 1;
2581 memcpy(acl->data, buf, acl_len);
2582 } else {
2583 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2584 if (acl == NULL)
2585 goto out;
2586 acl->cached = 0;
2587 }
2588 acl->len = acl_len;
2589out:
2590 nfs4_set_cached_acl(inode, acl);
2591}
2592
2593static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
aa1870af 2594{
aa1870af
BF
2595 struct page *pages[NFS4ACL_MAXPAGES];
2596 struct nfs_getaclargs args = {
2597 .fh = NFS_FH(inode),
2598 .acl_pages = pages,
2599 .acl_len = buflen,
2600 };
2601 size_t resp_len = buflen;
e50a1c2e 2602 void *resp_buf;
aa1870af
BF
2603 struct rpc_message msg = {
2604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2605 .rpc_argp = &args,
2606 .rpc_resp = &resp_len,
2607 };
e50a1c2e 2608 struct page *localpage = NULL;
aa1870af
BF
2609 int ret;
2610
e50a1c2e
BF
2611 if (buflen < PAGE_SIZE) {
2612 /* As long as we're doing a round trip to the server anyway,
2613 * let's be prepared for a page of acl data. */
2614 localpage = alloc_page(GFP_KERNEL);
2615 resp_buf = page_address(localpage);
2616 if (localpage == NULL)
2617 return -ENOMEM;
2618 args.acl_pages[0] = localpage;
2619 args.acl_pgbase = 0;
1d95db8e 2620 resp_len = args.acl_len = PAGE_SIZE;
e50a1c2e
BF
2621 } else {
2622 resp_buf = buf;
2623 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2624 }
aa1870af 2625 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
e50a1c2e
BF
2626 if (ret)
2627 goto out_free;
2628 if (resp_len > args.acl_len)
2629 nfs4_write_cached_acl(inode, NULL, resp_len);
2630 else
2631 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2632 if (buf) {
2633 ret = -ERANGE;
2634 if (resp_len > buflen)
2635 goto out_free;
2636 if (localpage)
2637 memcpy(buf, resp_buf, resp_len);
2638 }
2639 ret = resp_len;
2640out_free:
2641 if (localpage)
2642 __free_page(localpage);
aa1870af
BF
2643 return ret;
2644}
2645
e50a1c2e
BF
2646static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2647{
2648 struct nfs_server *server = NFS_SERVER(inode);
2649 int ret;
2650
2651 if (!nfs4_server_supports_acls(server))
2652 return -EOPNOTSUPP;
2653 ret = nfs_revalidate_inode(server, inode);
2654 if (ret < 0)
2655 return ret;
2656 ret = nfs4_read_cached_acl(inode, buf, buflen);
2657 if (ret != -ENOENT)
2658 return ret;
2659 return nfs4_get_acl_uncached(inode, buf, buflen);
2660}
2661
4b580ee3
BF
2662static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2663{
2664 struct nfs_server *server = NFS_SERVER(inode);
2665 struct page *pages[NFS4ACL_MAXPAGES];
2666 struct nfs_setaclargs arg = {
2667 .fh = NFS_FH(inode),
2668 .acl_pages = pages,
2669 .acl_len = buflen,
2670 };
2671 struct rpc_message msg = {
2672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2673 .rpc_argp = &arg,
2674 .rpc_resp = NULL,
2675 };
2676 int ret;
2677
2678 if (!nfs4_server_supports_acls(server))
2679 return -EOPNOTSUPP;
642ac549 2680 nfs_inode_return_delegation(inode);
4b580ee3
BF
2681 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2682 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
e50a1c2e
BF
2683 if (ret == 0)
2684 nfs4_write_cached_acl(inode, buf, buflen);
4b580ee3
BF
2685 return ret;
2686}
2687
1da177e4 2688static int
faf5f49c 2689nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
1da177e4
LT
2690{
2691 struct nfs4_client *clp = server->nfs4_state;
2692
2693 if (!clp || task->tk_status >= 0)
2694 return 0;
2695 switch(task->tk_status) {
2696 case -NFS4ERR_STALE_CLIENTID:
2697 case -NFS4ERR_STALE_STATEID:
2698 case -NFS4ERR_EXPIRED:
2699 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2700 nfs4_schedule_state_recovery(clp);
2701 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2702 rpc_wake_up_task(task);
2703 task->tk_status = 0;
2704 return -EAGAIN;
2705 case -NFS4ERR_GRACE:
2706 case -NFS4ERR_DELAY:
2707 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2708 task->tk_status = 0;
2709 return -EAGAIN;
2710 case -NFS4ERR_OLD_STATEID:
2711 task->tk_status = 0;
2712 return -EAGAIN;
2713 }
2714 task->tk_status = nfs4_map_errors(task->tk_status);
2715 return 0;
2716}
2717
2718static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2719{
2720 DEFINE_WAIT(wait);
2721 sigset_t oldset;
2722 int interruptible, res = 0;
2723
2724 might_sleep();
2725
2726 rpc_clnt_sigmask(clnt, &oldset);
2727 interruptible = TASK_UNINTERRUPTIBLE;
2728 if (clnt->cl_intr)
2729 interruptible = TASK_INTERRUPTIBLE;
2730 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2731 nfs4_schedule_state_recovery(clp);
2732 if (clnt->cl_intr && signalled())
2733 res = -ERESTARTSYS;
2734 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2735 schedule();
2736 finish_wait(&clp->cl_waitq, &wait);
2737 rpc_clnt_sigunmask(clnt, &oldset);
2738 return res;
2739}
2740
2741static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2742{
2743 sigset_t oldset;
2744 int res = 0;
2745
2746 might_sleep();
2747
2748 if (*timeout <= 0)
2749 *timeout = NFS4_POLL_RETRY_MIN;
2750 if (*timeout > NFS4_POLL_RETRY_MAX)
2751 *timeout = NFS4_POLL_RETRY_MAX;
2752 rpc_clnt_sigmask(clnt, &oldset);
2753 if (clnt->cl_intr) {
041e0e3b 2754 schedule_timeout_interruptible(*timeout);
1da177e4
LT
2755 if (signalled())
2756 res = -ERESTARTSYS;
041e0e3b
NA
2757 } else
2758 schedule_timeout_uninterruptible(*timeout);
1da177e4
LT
2759 rpc_clnt_sigunmask(clnt, &oldset);
2760 *timeout <<= 1;
2761 return res;
2762}
2763
2764/* This is the error handling routine for processes that are allowed
2765 * to sleep.
2766 */
faf5f49c 2767int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
1da177e4
LT
2768{
2769 struct nfs4_client *clp = server->nfs4_state;
2770 int ret = errorcode;
2771
2772 exception->retry = 0;
2773 switch(errorcode) {
2774 case 0:
2775 return 0;
2776 case -NFS4ERR_STALE_CLIENTID:
2777 case -NFS4ERR_STALE_STATEID:
2778 case -NFS4ERR_EXPIRED:
2779 ret = nfs4_wait_clnt_recover(server->client, clp);
2780 if (ret == 0)
2781 exception->retry = 1;
2782 break;
2783 case -NFS4ERR_GRACE:
2784 case -NFS4ERR_DELAY:
2785 ret = nfs4_delay(server->client, &exception->timeout);
2c56617d
TM
2786 if (ret != 0)
2787 break;
1da177e4 2788 case -NFS4ERR_OLD_STATEID:
2c56617d 2789 exception->retry = 1;
1da177e4
LT
2790 }
2791 /* We failed to handle the error */
2792 return nfs4_map_errors(ret);
2793}
2794
2795int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2796{
2797 nfs4_verifier sc_verifier;
2798 struct nfs4_setclientid setclientid = {
2799 .sc_verifier = &sc_verifier,
2800 .sc_prog = program,
2801 };
2802 struct rpc_message msg = {
2803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2804 .rpc_argp = &setclientid,
2805 .rpc_resp = clp,
2806 .rpc_cred = clp->cl_cred,
2807 };
2808 u32 *p;
2809 int loop = 0;
2810 int status;
2811
2812 p = (u32*)sc_verifier.data;
2813 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2814 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2815
2816 for(;;) {
2817 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2818 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2819 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2820 clp->cl_cred->cr_ops->cr_name,
2821 clp->cl_id_uniquifier);
2822 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2823 sizeof(setclientid.sc_netid), "tcp");
2824 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2825 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2826 clp->cl_ipaddr, port >> 8, port & 255);
2827
2828 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2829 if (status != -NFS4ERR_CLID_INUSE)
2830 break;
2831 if (signalled())
2832 break;
2833 if (loop++ & 1)
2834 ssleep(clp->cl_lease_time + 1);
2835 else
2836 if (++clp->cl_id_uniquifier == 0)
2837 break;
2838 }
2839 return status;
2840}
2841
2842int
2843nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2844{
2845 struct nfs_fsinfo fsinfo;
2846 struct rpc_message msg = {
2847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2848 .rpc_argp = clp,
2849 .rpc_resp = &fsinfo,
2850 .rpc_cred = clp->cl_cred,
2851 };
2852 unsigned long now;
2853 int status;
2854
2855 now = jiffies;
2856 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2857 if (status == 0) {
2858 spin_lock(&clp->cl_lock);
2859 clp->cl_lease_time = fsinfo.lease_time * HZ;
2860 clp->cl_last_renewal = now;
2861 spin_unlock(&clp->cl_lock);
2862 }
2863 return status;
2864}
2865
2866static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2867{
2868 struct nfs4_delegreturnargs args = {
2869 .fhandle = NFS_FH(inode),
2870 .stateid = stateid,
2871 };
2872 struct rpc_message msg = {
2873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2874 .rpc_argp = &args,
2875 .rpc_cred = cred,
2876 };
2877
2878 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2879}
2880
2881int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2882{
2883 struct nfs_server *server = NFS_SERVER(inode);
2884 struct nfs4_exception exception = { };
2885 int err;
2886 do {
2887 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2888 switch (err) {
2889 case -NFS4ERR_STALE_STATEID:
2890 case -NFS4ERR_EXPIRED:
2891 nfs4_schedule_state_recovery(server->nfs4_state);
2892 case 0:
2893 return 0;
2894 }
2895 err = nfs4_handle_exception(server, err, &exception);
2896 } while (exception.retry);
2897 return err;
2898}
2899
2900#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2901#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2902
2903/*
2904 * sleep, with exponential backoff, and retry the LOCK operation.
2905 */
2906static unsigned long
2907nfs4_set_lock_task_retry(unsigned long timeout)
2908{
041e0e3b 2909 schedule_timeout_interruptible(timeout);
1da177e4
LT
2910 timeout <<= 1;
2911 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2912 return NFS4_LOCK_MAXTIMEOUT;
2913 return timeout;
2914}
2915
2916static inline int
2917nfs4_lck_type(int cmd, struct file_lock *request)
2918{
2919 /* set lock type */
2920 switch (request->fl_type) {
2921 case F_RDLCK:
2922 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2923 case F_WRLCK:
2924 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2925 case F_UNLCK:
2926 return NFS4_WRITE_LT;
2927 }
2928 BUG();
2929 return 0;
2930}
2931
2932static inline uint64_t
2933nfs4_lck_length(struct file_lock *request)
2934{
2935 if (request->fl_end == OFFSET_MAX)
2936 return ~(uint64_t)0;
2937 return request->fl_end - request->fl_start + 1;
2938}
2939
2940static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2941{
2942 struct inode *inode = state->inode;
2943 struct nfs_server *server = NFS_SERVER(inode);
2944 struct nfs4_client *clp = server->nfs4_state;
2945 struct nfs_lockargs arg = {
2946 .fh = NFS_FH(inode),
2947 .type = nfs4_lck_type(cmd, request),
2948 .offset = request->fl_start,
2949 .length = nfs4_lck_length(request),
2950 };
2951 struct nfs_lockres res = {
2952 .server = server,
2953 };
2954 struct rpc_message msg = {
2955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2956 .rpc_argp = &arg,
2957 .rpc_resp = &res,
2958 .rpc_cred = state->owner->so_cred,
2959 };
2960 struct nfs_lowner nlo;
2961 struct nfs4_lock_state *lsp;
2962 int status;
2963
2964 down_read(&clp->cl_sem);
2965 nlo.clientid = clp->cl_clientid;
8d0a8a9d
TM
2966 status = nfs4_set_lock_state(state, request);
2967 if (status != 0)
2968 goto out;
2969 lsp = request->fl_u.nfs4_fl.owner;
2970 nlo.id = lsp->ls_id;
1da177e4
LT
2971 arg.u.lockt = &nlo;
2972 status = rpc_call_sync(server->client, &msg, 0);
2973 if (!status) {
2974 request->fl_type = F_UNLCK;
2975 } else if (status == -NFS4ERR_DENIED) {
2976 int64_t len, start, end;
2977 start = res.u.denied.offset;
2978 len = res.u.denied.length;
2979 end = start + len - 1;
2980 if (end < 0 || len == 0)
2981 request->fl_end = OFFSET_MAX;
2982 else
2983 request->fl_end = (loff_t)end;
2984 request->fl_start = (loff_t)start;
2985 request->fl_type = F_WRLCK;
2986 if (res.u.denied.type & 1)
2987 request->fl_type = F_RDLCK;
2988 request->fl_pid = 0;
2989 status = 0;
2990 }
8d0a8a9d 2991out:
1da177e4
LT
2992 up_read(&clp->cl_sem);
2993 return status;
2994}
2995
2996static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2997{
2998 struct nfs4_exception exception = { };
2999 int err;
3000
3001 do {
3002 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3003 _nfs4_proc_getlk(state, cmd, request),
3004 &exception);
3005 } while (exception.retry);
3006 return err;
3007}
3008
3009static int do_vfs_lock(struct file *file, struct file_lock *fl)
3010{
3011 int res = 0;
3012 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3013 case FL_POSIX:
3014 res = posix_lock_file_wait(file, fl);
3015 break;
3016 case FL_FLOCK:
3017 res = flock_lock_file_wait(file, fl);
3018 break;
3019 default:
3020 BUG();
3021 }
3022 if (res < 0)
3023 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
3024 __FUNCTION__);
3025 return res;
3026}
3027
faf5f49c
TM
3028struct nfs4_unlockdata {
3029 struct nfs_lockargs arg;
3030 struct nfs_locku_opargs luargs;
3031 struct nfs_lockres res;
3032 struct nfs4_lock_state *lsp;
3033 struct nfs_open_context *ctx;
faf5f49c
TM
3034};
3035
06f814a3 3036static void nfs4_locku_release_calldata(void *data)
faf5f49c 3037{
963d8fe5 3038 struct nfs4_unlockdata *calldata = data;
06f814a3
TM
3039 nfs_free_seqid(calldata->luargs.seqid);
3040 nfs4_put_lock_state(calldata->lsp);
3041 put_nfs_open_context(calldata->ctx);
3042 kfree(calldata);
faf5f49c
TM
3043}
3044
963d8fe5 3045static void nfs4_locku_done(struct rpc_task *task, void *data)
faf5f49c 3046{
963d8fe5 3047 struct nfs4_unlockdata *calldata = data;
faf5f49c 3048
06f814a3
TM
3049 if (RPC_ASSASSINATED(task))
3050 return;
faf5f49c
TM
3051 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
3052 switch (task->tk_status) {
3053 case 0:
3054 memcpy(calldata->lsp->ls_stateid.data,
3055 calldata->res.u.stateid.data,
3056 sizeof(calldata->lsp->ls_stateid.data));
3057 break;
3058 case -NFS4ERR_STALE_STATEID:
3059 case -NFS4ERR_EXPIRED:
3060 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
3061 break;
3062 default:
3063 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
3064 rpc_restart_call(task);
faf5f49c
TM
3065 }
3066 }
faf5f49c
TM
3067}
3068
4ce70ada 3069static void nfs4_locku_prepare(struct rpc_task *task, void *data)
faf5f49c 3070{
4ce70ada 3071 struct nfs4_unlockdata *calldata = data;
1da177e4
LT
3072 struct rpc_message msg = {
3073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
faf5f49c
TM
3074 .rpc_argp = &calldata->arg,
3075 .rpc_resp = &calldata->res,
3076 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
1da177e4 3077 };
faf5f49c
TM
3078 int status;
3079
3080 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
3081 if (status != 0)
3082 return;
3083 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
963d8fe5
TM
3084 /* Note: exit _without_ running nfs4_locku_done */
3085 task->tk_action = NULL;
faf5f49c
TM
3086 return;
3087 }
3088 rpc_call_setup(task, &msg, 0);
3089}
3090
963d8fe5 3091static const struct rpc_call_ops nfs4_locku_ops = {
4ce70ada 3092 .rpc_call_prepare = nfs4_locku_prepare,
963d8fe5 3093 .rpc_call_done = nfs4_locku_done,
06f814a3 3094 .rpc_release = nfs4_locku_release_calldata,
963d8fe5
TM
3095};
3096
faf5f49c
TM
3097static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3098{
3099 struct nfs4_unlockdata *calldata;
3100 struct inode *inode = state->inode;
3101 struct nfs_server *server = NFS_SERVER(inode);
1da177e4 3102 struct nfs4_lock_state *lsp;
06f814a3
TM
3103 struct rpc_task *task;
3104 int status = 0;
faf5f49c 3105
6bfc93ef
TM
3106 /* Is this a delegated lock? */
3107 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
06f814a3 3108 goto out;
6bfc93ef 3109
8d0a8a9d
TM
3110 status = nfs4_set_lock_state(state, request);
3111 if (status != 0)
06f814a3 3112 goto out;
8d0a8a9d 3113 lsp = request->fl_u.nfs4_fl.owner;
1da177e4 3114 /* We might have lost the locks! */
8d0a8a9d 3115 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
06f814a3
TM
3116 goto out;
3117 status = -ENOMEM;
faf5f49c
TM
3118 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
3119 if (calldata == NULL)
06f814a3 3120 goto out;
faf5f49c
TM
3121 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3122 if (calldata->luargs.seqid == NULL) {
3123 kfree(calldata);
06f814a3 3124 goto out;
faf5f49c
TM
3125 }
3126 calldata->luargs.stateid = &lsp->ls_stateid;
3127 calldata->arg.fh = NFS_FH(inode);
3128 calldata->arg.type = nfs4_lck_type(cmd, request);
3129 calldata->arg.offset = request->fl_start;
3130 calldata->arg.length = nfs4_lck_length(request);
3131 calldata->arg.u.locku = &calldata->luargs;
3132 calldata->res.server = server;
3133 calldata->lsp = lsp;
3134 atomic_inc(&lsp->ls_count);
3135
3136 /* Ensure we don't close file until we're done freeing locks! */
3137 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
3138
06f814a3
TM
3139 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata);
3140 if (!IS_ERR(task)) {
3141 status = nfs4_wait_for_completion_rpc_task(task);
3142 rpc_release_task(task);
3143 } else {
3144 status = PTR_ERR(task);
3145 nfs4_locku_release_calldata(calldata);
3146 }
3147out:
faf5f49c 3148 do_vfs_lock(request->fl_file, request);
1da177e4
LT
3149 return status;
3150}
3151
1da177e4
LT
3152static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
3153{
3154 struct inode *inode = state->inode;
3155 struct nfs_server *server = NFS_SERVER(inode);
8d0a8a9d 3156 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
06735b34
TM
3157 struct nfs_lock_opargs largs = {
3158 .lock_stateid = &lsp->ls_stateid,
3159 .open_stateid = &state->stateid,
3160 .lock_owner = {
3161 .clientid = server->nfs4_state->cl_clientid,
3162 .id = lsp->ls_id,
3163 },
3164 .reclaim = reclaim,
3165 };
1da177e4
LT
3166 struct nfs_lockargs arg = {
3167 .fh = NFS_FH(inode),
3168 .type = nfs4_lck_type(cmd, request),
3169 .offset = request->fl_start,
3170 .length = nfs4_lck_length(request),
06735b34
TM
3171 .u = {
3172 .lock = &largs,
3173 },
1da177e4
LT
3174 };
3175 struct nfs_lockres res = {
3176 .server = server,
3177 };
3178 struct rpc_message msg = {
3179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3180 .rpc_argp = &arg,
3181 .rpc_resp = &res,
3182 .rpc_cred = state->owner->so_cred,
3183 };
cee54fc9 3184 int status = -ENOMEM;
1da177e4 3185
06735b34
TM
3186 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3187 if (largs.lock_seqid == NULL)
cee54fc9
TM
3188 return -ENOMEM;
3189 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
1da177e4 3190 struct nfs4_state_owner *owner = state->owner;
06735b34
TM
3191
3192 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3193 if (largs.open_seqid == NULL)
3194 goto out;
1da177e4 3195 largs.new_lock_owner = 1;
06735b34
TM
3196 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3197 /* increment open seqid on success, and seqid mutating errors */
3198 if (largs.new_lock_owner != 0) {
3199 nfs_increment_open_seqid(status, largs.open_seqid);
3200 if (status == 0)
3201 nfs_confirm_seqid(&lsp->ls_seqid, 0);
8d0a8a9d 3202 }
06735b34
TM
3203 nfs_free_seqid(largs.open_seqid);
3204 } else
1da177e4 3205 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
06735b34
TM
3206 /* increment lock seqid on success, and seqid mutating errors*/
3207 nfs_increment_lock_seqid(status, largs.lock_seqid);
1da177e4 3208 /* save the returned stateid. */
cee54fc9 3209 if (status == 0) {
06735b34
TM
3210 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3211 sizeof(lsp->ls_stateid.data));
cee54fc9
TM
3212 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3213 } else if (status == -NFS4ERR_DENIED)
1da177e4 3214 status = -EAGAIN;
06735b34
TM
3215out:
3216 nfs_free_seqid(largs.lock_seqid);
1da177e4
LT
3217 return status;
3218}
3219
3220static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3221{
202b50dc
TM
3222 struct nfs_server *server = NFS_SERVER(state->inode);
3223 struct nfs4_exception exception = { };
3224 int err;
3225
6bfc93ef
TM
3226 /* Cache the lock if possible... */
3227 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3228 return 0;
202b50dc
TM
3229 do {
3230 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3231 if (err != -NFS4ERR_DELAY)
3232 break;
3233 nfs4_handle_exception(server, err, &exception);
3234 } while (exception.retry);
3235 return err;
1da177e4
LT
3236}
3237
3238static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3239{
202b50dc
TM
3240 struct nfs_server *server = NFS_SERVER(state->inode);
3241 struct nfs4_exception exception = { };
3242 int err;
3243
6bfc93ef
TM
3244 err = nfs4_set_lock_state(state, request);
3245 if (err != 0)
3246 return err;
202b50dc
TM
3247 do {
3248 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3249 if (err != -NFS4ERR_DELAY)
3250 break;
3251 nfs4_handle_exception(server, err, &exception);
3252 } while (exception.retry);
3253 return err;
1da177e4
LT
3254}
3255
3256static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3257{
3258 struct nfs4_client *clp = state->owner->so_client;
3259 int status;
3260
6bfc93ef 3261 /* Is this a delegated open? */
ff604066 3262 if (NFS_I(state->inode)->delegation_state != 0) {
6bfc93ef
TM
3263 /* Yes: cache locks! */
3264 status = do_vfs_lock(request->fl_file, request);
3265 /* ...but avoid races with delegation recall... */
3266 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
ff604066 3267 return status;
1da177e4 3268 }
ff604066 3269 down_read(&clp->cl_sem);
6bfc93ef
TM
3270 status = nfs4_set_lock_state(state, request);
3271 if (status != 0)
3272 goto out;
3273 status = _nfs4_do_setlk(state, cmd, request, 0);
3274 if (status != 0)
3275 goto out;
3276 /* Note: we always want to sleep here! */
3277 request->fl_flags |= FL_SLEEP;
3278 if (do_vfs_lock(request->fl_file, request) < 0)
3279 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3280out:
1da177e4
LT
3281 up_read(&clp->cl_sem);
3282 return status;
3283}
3284
3285static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3286{
3287 struct nfs4_exception exception = { };
3288 int err;
3289
3290 do {
3291 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3292 _nfs4_proc_setlk(state, cmd, request),
3293 &exception);
3294 } while (exception.retry);
3295 return err;
3296}
3297
3298static int
3299nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3300{
3301 struct nfs_open_context *ctx;
3302 struct nfs4_state *state;
3303 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3304 int status;
3305
3306 /* verify open state */
3307 ctx = (struct nfs_open_context *)filp->private_data;
3308 state = ctx->state;
3309
3310 if (request->fl_start < 0 || request->fl_end < 0)
3311 return -EINVAL;
3312
3313 if (IS_GETLK(cmd))
3314 return nfs4_proc_getlk(state, F_GETLK, request);
3315
3316 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3317 return -EINVAL;
3318
3319 if (request->fl_type == F_UNLCK)
3320 return nfs4_proc_unlck(state, cmd, request);
3321
3322 do {
3323 status = nfs4_proc_setlk(state, cmd, request);
3324 if ((status != -EAGAIN) || IS_SETLK(cmd))
3325 break;
3326 timeout = nfs4_set_lock_task_retry(timeout);
3327 status = -ERESTARTSYS;
3328 if (signalled())
3329 break;
3330 } while(status < 0);
1da177e4
LT
3331 return status;
3332}
3333
888e694c
TM
3334int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3335{
3336 struct nfs_server *server = NFS_SERVER(state->inode);
3337 struct nfs4_exception exception = { };
3338 int err;
3339
3340 err = nfs4_set_lock_state(state, fl);
3341 if (err != 0)
3342 goto out;
3343 do {
3344 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3345 if (err != -NFS4ERR_DELAY)
3346 break;
3347 err = nfs4_handle_exception(server, err, &exception);
3348 } while (exception.retry);
3349out:
3350 return err;
3351}
6b3b5496 3352
aa1870af
BF
3353#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3354
6b3b5496
BF
3355int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3356 size_t buflen, int flags)
3357{
4b580ee3
BF
3358 struct inode *inode = dentry->d_inode;
3359
3360 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3361 return -EOPNOTSUPP;
3362
3363 if (!S_ISREG(inode->i_mode) &&
3364 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3365 return -EPERM;
3366
3367 return nfs4_proc_set_acl(inode, buf, buflen);
6b3b5496
BF
3368}
3369
3370/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3371 * and that's what we'll do for e.g. user attributes that haven't been set.
3372 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3373 * attributes in kernel-managed attribute namespaces. */
3374ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3375 size_t buflen)
3376{
aa1870af
BF
3377 struct inode *inode = dentry->d_inode;
3378
3379 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3380 return -EOPNOTSUPP;
3381
3382 return nfs4_proc_get_acl(inode, buf, buflen);
6b3b5496
BF
3383}
3384
3385ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3386{
aa1870af 3387 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
6b3b5496
BF
3388
3389 if (buf && buflen < len)
3390 return -ERANGE;
3391 if (buf)
aa1870af
BF
3392 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3393 return len;
6b3b5496
BF
3394}
3395
1da177e4
LT
3396struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3397 .recover_open = nfs4_open_reclaim,
3398 .recover_lock = nfs4_lock_reclaim,
3399};
3400
3401struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3402 .recover_open = nfs4_open_expired,
3403 .recover_lock = nfs4_lock_expired,
3404};
3405
6b3b5496
BF
3406static struct inode_operations nfs4_file_inode_operations = {
3407 .permission = nfs_permission,
3408 .getattr = nfs_getattr,
3409 .setattr = nfs_setattr,
3410 .getxattr = nfs4_getxattr,
3411 .setxattr = nfs4_setxattr,
3412 .listxattr = nfs4_listxattr,
3413};
3414
1da177e4
LT
3415struct nfs_rpc_ops nfs_v4_clientops = {
3416 .version = 4, /* protocol version */
3417 .dentry_ops = &nfs4_dentry_operations,
3418 .dir_inode_ops = &nfs4_dir_inode_operations,
6b3b5496 3419 .file_inode_ops = &nfs4_file_inode_operations,
1da177e4
LT
3420 .getroot = nfs4_proc_get_root,
3421 .getattr = nfs4_proc_getattr,
3422 .setattr = nfs4_proc_setattr,
3423 .lookup = nfs4_proc_lookup,
3424 .access = nfs4_proc_access,
3425 .readlink = nfs4_proc_readlink,
3426 .read = nfs4_proc_read,
3427 .write = nfs4_proc_write,
3428 .commit = nfs4_proc_commit,
3429 .create = nfs4_proc_create,
3430 .remove = nfs4_proc_remove,
3431 .unlink_setup = nfs4_proc_unlink_setup,
3432 .unlink_done = nfs4_proc_unlink_done,
3433 .rename = nfs4_proc_rename,
3434 .link = nfs4_proc_link,
3435 .symlink = nfs4_proc_symlink,
3436 .mkdir = nfs4_proc_mkdir,
3437 .rmdir = nfs4_proc_remove,
3438 .readdir = nfs4_proc_readdir,
3439 .mknod = nfs4_proc_mknod,
3440 .statfs = nfs4_proc_statfs,
3441 .fsinfo = nfs4_proc_fsinfo,
3442 .pathconf = nfs4_proc_pathconf,
3443 .decode_dirent = nfs4_decode_dirent,
3444 .read_setup = nfs4_proc_read_setup,
3445 .write_setup = nfs4_proc_write_setup,
3446 .commit_setup = nfs4_proc_commit_setup,
02a913a7
TM
3447 .file_open = nfs_open,
3448 .file_release = nfs_release,
1da177e4 3449 .lock = nfs4_proc_lock,
e50a1c2e 3450 .clear_acl_cache = nfs4_zap_acl_attr,
1da177e4
LT
3451};
3452
3453/*
3454 * Local variables:
3455 * c-basic-offset: 8
3456 * End:
3457 */