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