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