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