blob: 2a759e8e387c85012c0bd137f786601f32d171e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#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
Trond Myklebust0a8838f2005-10-18 14:20:14 -070060static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070062static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
66extern struct rpc_procinfo nfs4_procedures[];
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* 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
Trond Myklebust4ce79712005-06-22 17:16:21 +0000108const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 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) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000128 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 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
Trond Myklebust95121352005-10-18 14:20:12 -0700194/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static 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() */
Trond Myklebustec073422005-10-20 14:22:47 -0700215 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 spin_lock(&inode->i_lock);
217 state->state |= open_flags;
218 /* NB! List reordering - see the reclaim code for why. */
219 if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
220 list_move(&state->open_states, &state->owner->so_states);
221 if (open_flags & FMODE_READ)
222 state->nreaders++;
223 memcpy(&state->stateid, stateid, sizeof(state->stateid));
224 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700225 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/*
229 * OPEN_RECLAIM:
230 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
233{
234 struct inode *inode = state->inode;
235 struct nfs_server *server = NFS_SERVER(inode);
236 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
237 struct nfs_openargs o_arg = {
238 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .id = sp->so_id,
240 .open_flags = state->state,
241 .clientid = server->nfs4_state->cl_clientid,
242 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
243 .bitmask = server->attr_bitmask,
244 };
245 struct nfs_openres o_res = {
246 .server = server, /* Grrr */
247 };
248 struct rpc_message msg = {
249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
250 .rpc_argp = &o_arg,
251 .rpc_resp = &o_res,
252 .rpc_cred = sp->so_cred,
253 };
254 int status;
255
256 if (delegation != NULL) {
257 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
258 memcpy(&state->stateid, &delegation->stateid,
259 sizeof(state->stateid));
260 set_bit(NFS_DELEGATED_STATE, &state->flags);
261 return 0;
262 }
263 o_arg.u.delegation_type = delegation->type;
264 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700265 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
266 if (o_arg.seqid == NULL)
267 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700269 /* Confirm the sequence as being established */
270 nfs_confirm_seqid(&sp->so_seqid, status);
271 nfs_increment_open_seqid(status, o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (status == 0) {
273 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
274 if (o_res.delegation_type != 0) {
275 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
276 /* Did the server issue an immediate delegation recall? */
277 if (o_res.do_recall)
278 nfs_async_inode_return_delegation(inode, &o_res.stateid);
279 }
280 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700281 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 clear_bit(NFS_DELEGATED_STATE, &state->flags);
283 /* Ensure we update the inode attributes */
284 NFS_CACHEINV(inode);
285 return status;
286}
287
288static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
289{
290 struct nfs_server *server = NFS_SERVER(state->inode);
291 struct nfs4_exception exception = { };
292 int err;
293 do {
294 err = _nfs4_open_reclaim(sp, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000295 if (err != -NFS4ERR_DELAY)
296 break;
297 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } while (exception.retry);
299 return err;
300}
301
302static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
303{
304 struct nfs4_state_owner *sp = state->owner;
305 struct inode *inode = dentry->d_inode;
306 struct nfs_server *server = NFS_SERVER(inode);
307 struct dentry *parent = dget_parent(dentry);
308 struct nfs_openargs arg = {
309 .fh = NFS_FH(parent->d_inode),
310 .clientid = server->nfs4_state->cl_clientid,
311 .name = &dentry->d_name,
312 .id = sp->so_id,
313 .server = server,
314 .bitmask = server->attr_bitmask,
315 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
316 };
317 struct nfs_openres res = {
318 .server = server,
319 };
320 struct rpc_message msg = {
321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
322 .rpc_argp = &arg,
323 .rpc_resp = &res,
324 .rpc_cred = sp->so_cred,
325 };
326 int status = 0;
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
329 goto out;
330 if (state->state == 0)
331 goto out;
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700332 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
333 status = -ENOMEM;
334 if (arg.seqid == NULL)
335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 arg.open_flags = state->state;
337 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
338 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700339 nfs_increment_open_seqid(status, arg.seqid);
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700340 if (status != 0)
341 goto out_free;
342 if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
343 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
344 sp, &res.stateid, arg.seqid);
345 if (status != 0)
346 goto out_free;
347 }
348 nfs_confirm_seqid(&sp->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (status >= 0) {
350 memcpy(state->stateid.data, res.stateid.data,
351 sizeof(state->stateid.data));
352 clear_bit(NFS_DELEGATED_STATE, &state->flags);
353 }
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700354out_free:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700355 nfs_free_seqid(arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dput(parent);
358 return status;
359}
360
361int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
362{
363 struct nfs4_exception exception = { };
364 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
365 int err;
366 do {
367 err = _nfs4_open_delegation_recall(dentry, state);
368 switch (err) {
369 case 0:
370 return err;
371 case -NFS4ERR_STALE_CLIENTID:
372 case -NFS4ERR_STALE_STATEID:
373 case -NFS4ERR_EXPIRED:
374 /* Don't recall a delegation if it was lost */
375 nfs4_schedule_state_recovery(server->nfs4_state);
376 return err;
377 }
378 err = nfs4_handle_exception(server, err, &exception);
379 } while (exception.retry);
380 return err;
381}
382
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700383static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct nfs_open_confirmargs arg = {
386 .fh = fh,
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700387 .seqid = seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .stateid = *stateid,
389 };
390 struct nfs_open_confirmres res;
391 struct rpc_message msg = {
392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
393 .rpc_argp = &arg,
394 .rpc_resp = &res,
395 .rpc_cred = sp->so_cred,
396 };
397 int status;
398
399 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700400 /* Confirm the sequence as being established */
401 nfs_confirm_seqid(&sp->so_seqid, status);
402 nfs_increment_open_seqid(status, seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (status >= 0)
404 memcpy(stateid, &res.stateid, sizeof(*stateid));
405 return status;
406}
407
408static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
409{
410 struct nfs_server *server = NFS_SERVER(dir);
411 struct rpc_message msg = {
412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
413 .rpc_argp = o_arg,
414 .rpc_resp = o_res,
415 .rpc_cred = sp->so_cred,
416 };
417 int status;
418
419 /* Update sequence id. The caller must serialize! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 o_arg->id = sp->so_id;
421 o_arg->clientid = sp->so_client->cl_clientid;
422
423 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebust6f926b52005-10-18 14:20:18 -0700424 if (status == 0) {
425 /* OPEN on anything except a regular file is disallowed in NFSv4 */
426 switch (o_res->f_attr->mode & S_IFMT) {
427 case S_IFREG:
428 break;
429 case S_IFLNK:
430 status = -ELOOP;
431 break;
432 case S_IFDIR:
433 status = -EISDIR;
434 break;
435 default:
436 status = -ENOTDIR;
437 }
438 }
439
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700440 nfs_increment_open_seqid(status, o_arg->seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (status != 0)
442 goto out;
443 update_changeattr(dir, &o_res->cinfo);
444 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
445 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700446 sp, &o_res->stateid, o_arg->seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (status != 0)
448 goto out;
449 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700450 nfs_confirm_seqid(&sp->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
452 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
453out:
454 return status;
455}
456
457static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
458{
459 struct nfs_access_entry cache;
460 int mask = 0;
461 int status;
462
463 if (openflags & FMODE_READ)
464 mask |= MAY_READ;
465 if (openflags & FMODE_WRITE)
466 mask |= MAY_WRITE;
467 status = nfs_access_get_cached(inode, cred, &cache);
468 if (status == 0)
469 goto out;
470
471 /* Be clever: ask server to check for all possible rights */
472 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
473 cache.cred = cred;
474 cache.jiffies = jiffies;
475 status = _nfs4_proc_access(inode, &cache);
476 if (status != 0)
477 return status;
478 nfs_access_add_cache(inode, &cache);
479out:
480 if ((cache.mask & mask) == mask)
481 return 0;
482 return -EACCES;
483}
484
485/*
486 * OPEN_EXPIRED:
487 * reclaim state on the server after a network partition.
488 * Assumes caller holds the appropriate lock
489 */
490static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
491{
492 struct dentry *parent = dget_parent(dentry);
493 struct inode *dir = parent->d_inode;
494 struct inode *inode = state->inode;
495 struct nfs_server *server = NFS_SERVER(dir);
496 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400497 struct nfs_fattr f_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 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 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700519 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
520 status = -ENOMEM;
521 if (o_arg.seqid == NULL)
522 goto out;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400523 nfs_fattr_init(&f_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
525 if (status != 0)
526 goto out_nodeleg;
527 /* Check if files differ */
528 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
529 goto out_stale;
530 /* Has the file handle changed? */
531 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
532 /* Verify if the change attributes are the same */
533 if (f_attr.change_attr != NFS_I(inode)->change_attr)
534 goto out_stale;
535 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
536 goto out_stale;
537 /* Lets just pretend that this is the same file */
538 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
539 NFS_I(inode)->fileid = f_attr.fileid;
540 }
541 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
542 if (o_res.delegation_type != 0) {
543 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
544 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
545 else
546 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
547 }
548out_nodeleg:
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700549 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 clear_bit(NFS_DELEGATED_STATE, &state->flags);
551out:
552 dput(parent);
553 return status;
554out_stale:
555 status = -ESTALE;
556 /* Invalidate the state owner so we don't ever use it again */
557 nfs4_drop_state_owner(sp);
558 d_drop(dentry);
559 /* Should we be trying to close that stateid? */
560 goto out_nodeleg;
561}
562
Trond Myklebust202b50d2005-06-22 17:16:29 +0000563static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
564{
565 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
566 struct nfs4_exception exception = { };
567 int err;
568
569 do {
570 err = _nfs4_open_expired(sp, state, dentry);
571 if (err == -NFS4ERR_DELAY)
572 nfs4_handle_exception(server, err, &exception);
573 } while (exception.retry);
574 return err;
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
578{
579 struct nfs_inode *nfsi = NFS_I(state->inode);
580 struct nfs_open_context *ctx;
581 int status;
582
583 spin_lock(&state->inode->i_lock);
584 list_for_each_entry(ctx, &nfsi->open_files, list) {
585 if (ctx->state != state)
586 continue;
587 get_nfs_open_context(ctx);
588 spin_unlock(&state->inode->i_lock);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000589 status = nfs4_do_open_expired(sp, state, ctx->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 put_nfs_open_context(ctx);
591 return status;
592 }
593 spin_unlock(&state->inode->i_lock);
594 return -ENOENT;
595}
596
597/*
598 * Returns an nfs4_state + an extra reference to the inode
599 */
600static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
601{
602 struct nfs_delegation *delegation;
603 struct nfs_server *server = NFS_SERVER(inode);
604 struct nfs4_client *clp = server->nfs4_state;
605 struct nfs_inode *nfsi = NFS_I(inode);
606 struct nfs4_state_owner *sp = NULL;
607 struct nfs4_state *state = NULL;
608 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
609 int err;
610
611 /* Protect against reboot recovery - NOTE ORDER! */
612 down_read(&clp->cl_sem);
613 /* Protect against delegation recall */
614 down_read(&nfsi->rwsem);
615 delegation = NFS_I(inode)->delegation;
616 err = -ENOENT;
617 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
618 goto out_err;
619 err = -ENOMEM;
620 if (!(sp = nfs4_get_state_owner(server, cred))) {
621 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
622 goto out_err;
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 state = nfs4_get_open_state(inode, sp);
625 if (state == NULL)
626 goto out_err;
627
628 err = -ENOENT;
629 if ((state->state & open_flags) == open_flags) {
630 spin_lock(&inode->i_lock);
631 if (open_flags & FMODE_READ)
632 state->nreaders++;
633 if (open_flags & FMODE_WRITE)
634 state->nwriters++;
635 spin_unlock(&inode->i_lock);
636 goto out_ok;
637 } else if (state->state != 0)
638 goto out_err;
639
640 lock_kernel();
641 err = _nfs4_do_access(inode, cred, open_flags);
642 unlock_kernel();
643 if (err != 0)
644 goto out_err;
645 set_bit(NFS_DELEGATED_STATE, &state->flags);
646 update_open_stateid(state, &delegation->stateid, open_flags);
647out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 nfs4_put_state_owner(sp);
649 up_read(&nfsi->rwsem);
650 up_read(&clp->cl_sem);
651 igrab(inode);
652 *res = state;
653 return 0;
654out_err:
655 if (sp != NULL) {
656 if (state != NULL)
657 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 nfs4_put_state_owner(sp);
659 }
660 up_read(&nfsi->rwsem);
661 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700662 if (err != -EACCES)
663 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return err;
665}
666
667static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
668{
669 struct nfs4_exception exception = { };
670 struct nfs4_state *res;
671 int err;
672
673 do {
674 err = _nfs4_open_delegated(inode, flags, cred, &res);
675 if (err == 0)
676 break;
677 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
678 err, &exception));
679 } while (exception.retry);
680 return res;
681}
682
683/*
684 * Returns an nfs4_state + an referenced inode
685 */
686static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
687{
688 struct nfs4_state_owner *sp;
689 struct nfs4_state *state = NULL;
690 struct nfs_server *server = NFS_SERVER(dir);
691 struct nfs4_client *clp = server->nfs4_state;
692 struct inode *inode = NULL;
693 int status;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400694 struct nfs_fattr f_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct nfs_openargs o_arg = {
696 .fh = NFS_FH(dir),
697 .open_flags = flags,
698 .name = &dentry->d_name,
699 .server = server,
700 .bitmask = server->attr_bitmask,
701 .claim = NFS4_OPEN_CLAIM_NULL,
702 };
703 struct nfs_openres o_res = {
704 .f_attr = &f_attr,
705 .server = server,
706 };
707
708 /* Protect against reboot recovery conflicts */
709 down_read(&clp->cl_sem);
710 status = -ENOMEM;
711 if (!(sp = nfs4_get_state_owner(server, cred))) {
712 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
713 goto out_err;
714 }
715 if (flags & O_EXCL) {
716 u32 *p = (u32 *) o_arg.u.verifier.data;
717 p[0] = jiffies;
718 p[1] = current->pid;
719 } else
720 o_arg.u.attrs = sattr;
721 /* Serialization for the sequence id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700723 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
724 if (o_arg.seqid == NULL)
725 return -ENOMEM;
Trond Myklebust0e574af2005-10-27 22:12:38 -0400726 nfs_fattr_init(&f_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 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);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700741 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 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);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700750 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 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
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700778 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * 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 }
Trond Myklebust550f5742005-10-18 14:20:21 -0700788 /*
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 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 };
Trond Myklebust65e43082005-08-16 11:49:44 -0400823 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Trond Myklebust0e574af2005-10-27 22:12:38 -0400825 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000827 if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000829 nfs4_copy_stateid(&arg.stateid, state, current->files);
830 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
832
Trond Myklebust65e43082005-08-16 11:49:44 -0400833 status = rpc_call_sync(server->client, &msg, 0);
834 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
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
Trond Myklebust95121352005-10-18 14:20:12 -0700859static void nfs4_free_closedata(struct nfs4_closedata *calldata)
860{
861 struct nfs4_state *state = calldata->state;
862 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -0700863
864 nfs4_put_open_state(calldata->state);
865 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -0700866 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -0700867 kfree(calldata);
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 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 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700879 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 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;
Trond Myklebust95121352005-10-18 14:20:12 -0700897 nfs4_free_closedata(calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Trond Myklebust95121352005-10-18 14:20:12 -0700900static void nfs4_close_begin(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Trond Myklebust95121352005-10-18 14:20:12 -0700902 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
903 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct rpc_message msg = {
905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
906 .rpc_argp = &calldata->arg,
907 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -0700908 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 };
Trond Myklebust95121352005-10-18 14:20:12 -0700910 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -0700935 calldata->arg.open_flags = mode;
936 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
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;
Trond Myklebust95121352005-10-18 14:20:12 -0700953 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Trond Myklebust95121352005-10-18 14:20:12 -0700955 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -0700957 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 calldata->inode = inode;
959 calldata->state = state;
960 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -0700961 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700963 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -0700964 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Trond Myklebust02a913a2005-10-18 14:20:17 -0700979static 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993nfs4_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;
Trond Myklebust02a913a2005-10-18 14:20:17 -0700998 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
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))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001012 return (struct dentry *)cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1014 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001015 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001028nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
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);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001041 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 inode = state->inode;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001057 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (inode == dentry->d_inode) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001059 nfs4_intent_set_file(nd, dentry, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 1;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 nfs4_close_state(state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001063out_drop:
1064 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct nfs4_lookup_root_arg args = {
1109 .bitmask = nfs4_fattr_bitmap,
1110 };
1111 struct nfs4_lookup_res res = {
1112 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001113 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 .fh = fhandle,
1115 };
1116 struct rpc_message msg = {
1117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1118 .rpc_argp = &args,
1119 .rpc_resp = &res,
1120 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001121 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return rpc_call_sync(server->client, &msg, 0);
1123}
1124
1125static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1126 struct nfs_fsinfo *info)
1127{
1128 struct nfs4_exception exception = { };
1129 int err;
1130 do {
1131 err = nfs4_handle_exception(server,
1132 _nfs4_lookup_root(server, fhandle, info),
1133 &exception);
1134 } while (exception.retry);
1135 return err;
1136}
1137
1138static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1139 struct nfs_fsinfo *info)
1140{
1141 struct nfs_fattr * fattr = info->fattr;
1142 unsigned char * p;
1143 struct qstr q;
1144 struct nfs4_lookup_arg args = {
1145 .dir_fh = fhandle,
1146 .name = &q,
1147 .bitmask = nfs4_fattr_bitmap,
1148 };
1149 struct nfs4_lookup_res res = {
1150 .server = server,
1151 .fattr = fattr,
1152 .fh = fhandle,
1153 };
1154 struct rpc_message msg = {
1155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1156 .rpc_argp = &args,
1157 .rpc_resp = &res,
1158 };
1159 int status;
1160
1161 /*
1162 * Now we do a separate LOOKUP for each component of the mount path.
1163 * The LOOKUPs are done separately so that we can conveniently
1164 * catch an ERR_WRONGSEC if it occurs along the way...
1165 */
1166 status = nfs4_lookup_root(server, fhandle, info);
1167 if (status)
1168 goto out;
1169
1170 p = server->mnt_path;
1171 for (;;) {
1172 struct nfs4_exception exception = { };
1173
1174 while (*p == '/')
1175 p++;
1176 if (!*p)
1177 break;
1178 q.name = p;
1179 while (*p && (*p != '/'))
1180 p++;
1181 q.len = p - q.name;
1182
1183 do {
Trond Myklebust0e574af2005-10-27 22:12:38 -04001184 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 status = nfs4_handle_exception(server,
1186 rpc_call_sync(server->client, &msg, 0),
1187 &exception);
1188 } while (exception.retry);
1189 if (status == 0)
1190 continue;
1191 if (status == -ENOENT) {
1192 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1193 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1194 }
1195 break;
1196 }
1197 if (status == 0)
1198 status = nfs4_server_capabilities(server, fhandle);
1199 if (status == 0)
1200 status = nfs4_do_fsinfo(server, fhandle, info);
1201out:
1202 return status;
1203}
1204
1205static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1206{
1207 struct nfs4_getattr_arg args = {
1208 .fh = fhandle,
1209 .bitmask = server->attr_bitmask,
1210 };
1211 struct nfs4_getattr_res res = {
1212 .fattr = fattr,
1213 .server = server,
1214 };
1215 struct rpc_message msg = {
1216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1217 .rpc_argp = &args,
1218 .rpc_resp = &res,
1219 };
1220
Trond Myklebust0e574af2005-10-27 22:12:38 -04001221 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return rpc_call_sync(server->client, &msg, 0);
1223}
1224
1225static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1226{
1227 struct nfs4_exception exception = { };
1228 int err;
1229 do {
1230 err = nfs4_handle_exception(server,
1231 _nfs4_proc_getattr(server, fhandle, fattr),
1232 &exception);
1233 } while (exception.retry);
1234 return err;
1235}
1236
1237/*
1238 * The file is not closed if it is opened due to the a request to change
1239 * the size of the file. The open call will not be needed once the
1240 * VFS layer lookup-intents are implemented.
1241 *
1242 * Close is called when the inode is destroyed.
1243 * If we haven't opened the file for O_WRONLY, we
1244 * need to in the size_change case to obtain a stateid.
1245 *
1246 * Got race?
1247 * Because OPEN is always done by name in nfsv4, it is
1248 * possible that we opened a different file by the same
1249 * name. We can recognize this race condition, but we
1250 * can't do anything about it besides returning an error.
1251 *
1252 * This will be fixed with VFS changes (lookup-intent).
1253 */
1254static int
1255nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1256 struct iattr *sattr)
1257{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001258 struct rpc_cred *cred;
1259 struct inode *inode = dentry->d_inode;
1260 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int status;
1262
Trond Myklebust0e574af2005-10-27 22:12:38 -04001263 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001265 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1266 if (IS_ERR(cred))
1267 return PTR_ERR(cred);
1268 /* Search for an existing WRITE delegation first */
1269 state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1270 if (!IS_ERR(state)) {
1271 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1272 iput(inode);
1273 } else {
1274 /* Search for an existing open(O_WRITE) stateid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 state = nfs4_find_state(inode, cred, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1279 NFS_FH(inode), sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001280 if (status == 0)
1281 nfs_setattr_update_inode(inode, sattr);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001282 if (state != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 nfs4_close_state(state, FMODE_WRITE);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001284 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return status;
1286}
1287
1288static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1289 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1290{
1291 int status;
1292 struct nfs_server *server = NFS_SERVER(dir);
1293 struct nfs4_lookup_arg args = {
1294 .bitmask = server->attr_bitmask,
1295 .dir_fh = NFS_FH(dir),
1296 .name = name,
1297 };
1298 struct nfs4_lookup_res res = {
1299 .server = server,
1300 .fattr = fattr,
1301 .fh = fhandle,
1302 };
1303 struct rpc_message msg = {
1304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1305 .rpc_argp = &args,
1306 .rpc_resp = &res,
1307 };
1308
Trond Myklebust0e574af2005-10-27 22:12:38 -04001309 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 dprintk("NFS call lookup %s\n", name->name);
1312 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1313 dprintk("NFS reply lookup: %d\n", status);
1314 return status;
1315}
1316
1317static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1318{
1319 struct nfs4_exception exception = { };
1320 int err;
1321 do {
1322 err = nfs4_handle_exception(NFS_SERVER(dir),
1323 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1324 &exception);
1325 } while (exception.retry);
1326 return err;
1327}
1328
1329static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1330{
1331 struct nfs4_accessargs args = {
1332 .fh = NFS_FH(inode),
1333 };
1334 struct nfs4_accessres res = { 0 };
1335 struct rpc_message msg = {
1336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1337 .rpc_argp = &args,
1338 .rpc_resp = &res,
1339 .rpc_cred = entry->cred,
1340 };
1341 int mode = entry->mask;
1342 int status;
1343
1344 /*
1345 * Determine which access bits we want to ask for...
1346 */
1347 if (mode & MAY_READ)
1348 args.access |= NFS4_ACCESS_READ;
1349 if (S_ISDIR(inode->i_mode)) {
1350 if (mode & MAY_WRITE)
1351 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1352 if (mode & MAY_EXEC)
1353 args.access |= NFS4_ACCESS_LOOKUP;
1354 } else {
1355 if (mode & MAY_WRITE)
1356 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1357 if (mode & MAY_EXEC)
1358 args.access |= NFS4_ACCESS_EXECUTE;
1359 }
1360 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1361 if (!status) {
1362 entry->mask = 0;
1363 if (res.access & NFS4_ACCESS_READ)
1364 entry->mask |= MAY_READ;
1365 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1366 entry->mask |= MAY_WRITE;
1367 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1368 entry->mask |= MAY_EXEC;
1369 }
1370 return status;
1371}
1372
1373static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1374{
1375 struct nfs4_exception exception = { };
1376 int err;
1377 do {
1378 err = nfs4_handle_exception(NFS_SERVER(inode),
1379 _nfs4_proc_access(inode, entry),
1380 &exception);
1381 } while (exception.retry);
1382 return err;
1383}
1384
1385/*
1386 * TODO: For the time being, we don't try to get any attributes
1387 * along with any of the zero-copy operations READ, READDIR,
1388 * READLINK, WRITE.
1389 *
1390 * In the case of the first three, we want to put the GETATTR
1391 * after the read-type operation -- this is because it is hard
1392 * to predict the length of a GETATTR response in v4, and thus
1393 * align the READ data correctly. This means that the GETATTR
1394 * may end up partially falling into the page cache, and we should
1395 * shift it into the 'tail' of the xdr_buf before processing.
1396 * To do this efficiently, we need to know the total length
1397 * of data received, which doesn't seem to be available outside
1398 * of the RPC layer.
1399 *
1400 * In the case of WRITE, we also want to put the GETATTR after
1401 * the operation -- in this case because we want to make sure
1402 * we get the post-operation mtime and size. This means that
1403 * we can't use xdr_encode_pages() as written: we need a variant
1404 * of it which would leave room in the 'tail' iovec.
1405 *
1406 * Both of these changes to the XDR layer would in fact be quite
1407 * minor, but I decided to leave them for a subsequent patch.
1408 */
1409static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1410 unsigned int pgbase, unsigned int pglen)
1411{
1412 struct nfs4_readlink args = {
1413 .fh = NFS_FH(inode),
1414 .pgbase = pgbase,
1415 .pglen = pglen,
1416 .pages = &page,
1417 };
1418 struct rpc_message msg = {
1419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1420 .rpc_argp = &args,
1421 .rpc_resp = NULL,
1422 };
1423
1424 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1425}
1426
1427static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1428 unsigned int pgbase, unsigned int pglen)
1429{
1430 struct nfs4_exception exception = { };
1431 int err;
1432 do {
1433 err = nfs4_handle_exception(NFS_SERVER(inode),
1434 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1435 &exception);
1436 } while (exception.retry);
1437 return err;
1438}
1439
1440static int _nfs4_proc_read(struct nfs_read_data *rdata)
1441{
1442 int flags = rdata->flags;
1443 struct inode *inode = rdata->inode;
1444 struct nfs_fattr *fattr = rdata->res.fattr;
1445 struct nfs_server *server = NFS_SERVER(inode);
1446 struct rpc_message msg = {
1447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1448 .rpc_argp = &rdata->args,
1449 .rpc_resp = &rdata->res,
1450 .rpc_cred = rdata->cred,
1451 };
1452 unsigned long timestamp = jiffies;
1453 int status;
1454
1455 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1456 (long long) rdata->args.offset);
1457
Trond Myklebust0e574af2005-10-27 22:12:38 -04001458 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 status = rpc_call_sync(server->client, &msg, flags);
1460 if (!status)
1461 renew_lease(server, timestamp);
1462 dprintk("NFS reply read: %d\n", status);
1463 return status;
1464}
1465
1466static int nfs4_proc_read(struct nfs_read_data *rdata)
1467{
1468 struct nfs4_exception exception = { };
1469 int err;
1470 do {
1471 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1472 _nfs4_proc_read(rdata),
1473 &exception);
1474 } while (exception.retry);
1475 return err;
1476}
1477
1478static int _nfs4_proc_write(struct nfs_write_data *wdata)
1479{
1480 int rpcflags = wdata->flags;
1481 struct inode *inode = wdata->inode;
1482 struct nfs_fattr *fattr = wdata->res.fattr;
1483 struct nfs_server *server = NFS_SERVER(inode);
1484 struct rpc_message msg = {
1485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1486 .rpc_argp = &wdata->args,
1487 .rpc_resp = &wdata->res,
1488 .rpc_cred = wdata->cred,
1489 };
1490 int status;
1491
1492 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1493 (long long) wdata->args.offset);
1494
Trond Myklebust0e574af2005-10-27 22:12:38 -04001495 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 status = rpc_call_sync(server->client, &msg, rpcflags);
1497 dprintk("NFS reply write: %d\n", status);
1498 return status;
1499}
1500
1501static int nfs4_proc_write(struct nfs_write_data *wdata)
1502{
1503 struct nfs4_exception exception = { };
1504 int err;
1505 do {
1506 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1507 _nfs4_proc_write(wdata),
1508 &exception);
1509 } while (exception.retry);
1510 return err;
1511}
1512
1513static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1514{
1515 struct inode *inode = cdata->inode;
1516 struct nfs_fattr *fattr = cdata->res.fattr;
1517 struct nfs_server *server = NFS_SERVER(inode);
1518 struct rpc_message msg = {
1519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1520 .rpc_argp = &cdata->args,
1521 .rpc_resp = &cdata->res,
1522 .rpc_cred = cdata->cred,
1523 };
1524 int status;
1525
1526 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1527 (long long) cdata->args.offset);
1528
Trond Myklebust0e574af2005-10-27 22:12:38 -04001529 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 status = rpc_call_sync(server->client, &msg, 0);
1531 dprintk("NFS reply commit: %d\n", status);
1532 return status;
1533}
1534
1535static int nfs4_proc_commit(struct nfs_write_data *cdata)
1536{
1537 struct nfs4_exception exception = { };
1538 int err;
1539 do {
1540 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1541 _nfs4_proc_commit(cdata),
1542 &exception);
1543 } while (exception.retry);
1544 return err;
1545}
1546
1547/*
1548 * Got race?
1549 * We will need to arrange for the VFS layer to provide an atomic open.
1550 * Until then, this create/open method is prone to inefficiency and race
1551 * conditions due to the lookup, create, and open VFS calls from sys_open()
1552 * placed on the wire.
1553 *
1554 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1555 * The file will be opened again in the subsequent VFS open call
1556 * (nfs4_proc_file_open).
1557 *
1558 * The open for read will just hang around to be used by any process that
1559 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1560 */
1561
1562static int
1563nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001564 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct nfs4_state *state;
1567 struct rpc_cred *cred;
1568 int status = 0;
1569
1570 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1571 if (IS_ERR(cred)) {
1572 status = PTR_ERR(cred);
1573 goto out;
1574 }
1575 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1576 put_rpccred(cred);
1577 if (IS_ERR(state)) {
1578 status = PTR_ERR(state);
1579 goto out;
1580 }
1581 d_instantiate(dentry, state->inode);
1582 if (flags & O_EXCL) {
1583 struct nfs_fattr fattr;
1584 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1585 NFS_FH(state->inode), sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001586 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001587 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001588 }
1589 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1590 nfs4_intent_set_file(nd, dentry, state);
1591 else
1592 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593out:
1594 return status;
1595}
1596
1597static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1598{
1599 struct nfs4_remove_arg args = {
1600 .fh = NFS_FH(dir),
1601 .name = name,
1602 };
1603 struct nfs4_change_info res;
1604 struct rpc_message msg = {
1605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1606 .rpc_argp = &args,
1607 .rpc_resp = &res,
1608 };
1609 int status;
1610
1611 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1612 if (status == 0)
1613 update_changeattr(dir, &res);
1614 return status;
1615}
1616
1617static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1618{
1619 struct nfs4_exception exception = { };
1620 int err;
1621 do {
1622 err = nfs4_handle_exception(NFS_SERVER(dir),
1623 _nfs4_proc_remove(dir, name),
1624 &exception);
1625 } while (exception.retry);
1626 return err;
1627}
1628
1629struct unlink_desc {
1630 struct nfs4_remove_arg args;
1631 struct nfs4_change_info res;
1632};
1633
1634static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1635 struct qstr *name)
1636{
1637 struct unlink_desc *up;
1638
1639 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1640 if (!up)
1641 return -ENOMEM;
1642
1643 up->args.fh = NFS_FH(dir->d_inode);
1644 up->args.name = name;
1645
1646 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1647 msg->rpc_argp = &up->args;
1648 msg->rpc_resp = &up->res;
1649 return 0;
1650}
1651
1652static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1653{
1654 struct rpc_message *msg = &task->tk_msg;
1655 struct unlink_desc *up;
1656
1657 if (msg->rpc_resp != NULL) {
1658 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1659 update_changeattr(dir->d_inode, &up->res);
1660 kfree(up);
1661 msg->rpc_resp = NULL;
1662 msg->rpc_argp = NULL;
1663 }
1664 return 0;
1665}
1666
1667static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1668 struct inode *new_dir, struct qstr *new_name)
1669{
1670 struct nfs4_rename_arg arg = {
1671 .old_dir = NFS_FH(old_dir),
1672 .new_dir = NFS_FH(new_dir),
1673 .old_name = old_name,
1674 .new_name = new_name,
1675 };
1676 struct nfs4_rename_res res = { };
1677 struct rpc_message msg = {
1678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1679 .rpc_argp = &arg,
1680 .rpc_resp = &res,
1681 };
1682 int status;
1683
1684 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1685
1686 if (!status) {
1687 update_changeattr(old_dir, &res.old_cinfo);
1688 update_changeattr(new_dir, &res.new_cinfo);
1689 }
1690 return status;
1691}
1692
1693static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1694 struct inode *new_dir, struct qstr *new_name)
1695{
1696 struct nfs4_exception exception = { };
1697 int err;
1698 do {
1699 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1700 _nfs4_proc_rename(old_dir, old_name,
1701 new_dir, new_name),
1702 &exception);
1703 } while (exception.retry);
1704 return err;
1705}
1706
1707static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1708{
1709 struct nfs4_link_arg arg = {
1710 .fh = NFS_FH(inode),
1711 .dir_fh = NFS_FH(dir),
1712 .name = name,
1713 };
1714 struct nfs4_change_info cinfo = { };
1715 struct rpc_message msg = {
1716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1717 .rpc_argp = &arg,
1718 .rpc_resp = &cinfo,
1719 };
1720 int status;
1721
1722 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1723 if (!status)
1724 update_changeattr(dir, &cinfo);
1725
1726 return status;
1727}
1728
1729static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1730{
1731 struct nfs4_exception exception = { };
1732 int err;
1733 do {
1734 err = nfs4_handle_exception(NFS_SERVER(inode),
1735 _nfs4_proc_link(inode, dir, name),
1736 &exception);
1737 } while (exception.retry);
1738 return err;
1739}
1740
1741static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1742 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1743 struct nfs_fattr *fattr)
1744{
1745 struct nfs_server *server = NFS_SERVER(dir);
1746 struct nfs4_create_arg arg = {
1747 .dir_fh = NFS_FH(dir),
1748 .server = server,
1749 .name = name,
1750 .attrs = sattr,
1751 .ftype = NF4LNK,
1752 .bitmask = server->attr_bitmask,
1753 };
1754 struct nfs4_create_res res = {
1755 .server = server,
1756 .fh = fhandle,
1757 .fattr = fattr,
1758 };
1759 struct rpc_message msg = {
1760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1761 .rpc_argp = &arg,
1762 .rpc_resp = &res,
1763 };
1764 int status;
1765
1766 if (path->len > NFS4_MAXPATHLEN)
1767 return -ENAMETOOLONG;
1768 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001769 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1772 if (!status)
1773 update_changeattr(dir, &res.dir_cinfo);
1774 return status;
1775}
1776
1777static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1778 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1779 struct nfs_fattr *fattr)
1780{
1781 struct nfs4_exception exception = { };
1782 int err;
1783 do {
1784 err = nfs4_handle_exception(NFS_SERVER(dir),
1785 _nfs4_proc_symlink(dir, name, path, sattr,
1786 fhandle, fattr),
1787 &exception);
1788 } while (exception.retry);
1789 return err;
1790}
1791
1792static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1793 struct iattr *sattr)
1794{
1795 struct nfs_server *server = NFS_SERVER(dir);
1796 struct nfs_fh fhandle;
1797 struct nfs_fattr fattr;
1798 struct nfs4_create_arg arg = {
1799 .dir_fh = NFS_FH(dir),
1800 .server = server,
1801 .name = &dentry->d_name,
1802 .attrs = sattr,
1803 .ftype = NF4DIR,
1804 .bitmask = server->attr_bitmask,
1805 };
1806 struct nfs4_create_res res = {
1807 .server = server,
1808 .fh = &fhandle,
1809 .fattr = &fattr,
1810 };
1811 struct rpc_message msg = {
1812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1813 .rpc_argp = &arg,
1814 .rpc_resp = &res,
1815 };
1816 int status;
1817
Trond Myklebust0e574af2005-10-27 22:12:38 -04001818 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1821 if (!status) {
1822 update_changeattr(dir, &res.dir_cinfo);
1823 status = nfs_instantiate(dentry, &fhandle, &fattr);
1824 }
1825 return status;
1826}
1827
1828static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1829 struct iattr *sattr)
1830{
1831 struct nfs4_exception exception = { };
1832 int err;
1833 do {
1834 err = nfs4_handle_exception(NFS_SERVER(dir),
1835 _nfs4_proc_mkdir(dir, dentry, sattr),
1836 &exception);
1837 } while (exception.retry);
1838 return err;
1839}
1840
1841static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1842 u64 cookie, struct page *page, unsigned int count, int plus)
1843{
1844 struct inode *dir = dentry->d_inode;
1845 struct nfs4_readdir_arg args = {
1846 .fh = NFS_FH(dir),
1847 .pages = &page,
1848 .pgbase = 0,
1849 .count = count,
1850 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1851 };
1852 struct nfs4_readdir_res res;
1853 struct rpc_message msg = {
1854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1855 .rpc_argp = &args,
1856 .rpc_resp = &res,
1857 .rpc_cred = cred,
1858 };
1859 int status;
1860
Trond Myklebusteadf4592005-06-22 17:16:39 +00001861 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1862 dentry->d_parent->d_name.name,
1863 dentry->d_name.name,
1864 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 lock_kernel();
1866 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1867 res.pgbase = args.pgbase;
1868 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1869 if (status == 0)
1870 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1871 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00001872 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return status;
1874}
1875
1876static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1877 u64 cookie, struct page *page, unsigned int count, int plus)
1878{
1879 struct nfs4_exception exception = { };
1880 int err;
1881 do {
1882 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1883 _nfs4_proc_readdir(dentry, cred, cookie,
1884 page, count, plus),
1885 &exception);
1886 } while (exception.retry);
1887 return err;
1888}
1889
1890static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1891 struct iattr *sattr, dev_t rdev)
1892{
1893 struct nfs_server *server = NFS_SERVER(dir);
1894 struct nfs_fh fh;
1895 struct nfs_fattr fattr;
1896 struct nfs4_create_arg arg = {
1897 .dir_fh = NFS_FH(dir),
1898 .server = server,
1899 .name = &dentry->d_name,
1900 .attrs = sattr,
1901 .bitmask = server->attr_bitmask,
1902 };
1903 struct nfs4_create_res res = {
1904 .server = server,
1905 .fh = &fh,
1906 .fattr = &fattr,
1907 };
1908 struct rpc_message msg = {
1909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1910 .rpc_argp = &arg,
1911 .rpc_resp = &res,
1912 };
1913 int status;
1914 int mode = sattr->ia_mode;
1915
Trond Myklebust0e574af2005-10-27 22:12:38 -04001916 nfs_fattr_init(&fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1919 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1920 if (S_ISFIFO(mode))
1921 arg.ftype = NF4FIFO;
1922 else if (S_ISBLK(mode)) {
1923 arg.ftype = NF4BLK;
1924 arg.u.device.specdata1 = MAJOR(rdev);
1925 arg.u.device.specdata2 = MINOR(rdev);
1926 }
1927 else if (S_ISCHR(mode)) {
1928 arg.ftype = NF4CHR;
1929 arg.u.device.specdata1 = MAJOR(rdev);
1930 arg.u.device.specdata2 = MINOR(rdev);
1931 }
1932 else
1933 arg.ftype = NF4SOCK;
1934
1935 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1936 if (status == 0) {
1937 update_changeattr(dir, &res.dir_cinfo);
1938 status = nfs_instantiate(dentry, &fh, &fattr);
1939 }
1940 return status;
1941}
1942
1943static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1944 struct iattr *sattr, dev_t rdev)
1945{
1946 struct nfs4_exception exception = { };
1947 int err;
1948 do {
1949 err = nfs4_handle_exception(NFS_SERVER(dir),
1950 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1951 &exception);
1952 } while (exception.retry);
1953 return err;
1954}
1955
1956static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1957 struct nfs_fsstat *fsstat)
1958{
1959 struct nfs4_statfs_arg args = {
1960 .fh = fhandle,
1961 .bitmask = server->attr_bitmask,
1962 };
1963 struct rpc_message msg = {
1964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1965 .rpc_argp = &args,
1966 .rpc_resp = fsstat,
1967 };
1968
Trond Myklebust0e574af2005-10-27 22:12:38 -04001969 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return rpc_call_sync(server->client, &msg, 0);
1971}
1972
1973static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1974{
1975 struct nfs4_exception exception = { };
1976 int err;
1977 do {
1978 err = nfs4_handle_exception(server,
1979 _nfs4_proc_statfs(server, fhandle, fsstat),
1980 &exception);
1981 } while (exception.retry);
1982 return err;
1983}
1984
1985static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1986 struct nfs_fsinfo *fsinfo)
1987{
1988 struct nfs4_fsinfo_arg args = {
1989 .fh = fhandle,
1990 .bitmask = server->attr_bitmask,
1991 };
1992 struct rpc_message msg = {
1993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1994 .rpc_argp = &args,
1995 .rpc_resp = fsinfo,
1996 };
1997
1998 return rpc_call_sync(server->client, &msg, 0);
1999}
2000
2001static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2002{
2003 struct nfs4_exception exception = { };
2004 int err;
2005
2006 do {
2007 err = nfs4_handle_exception(server,
2008 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2009 &exception);
2010 } while (exception.retry);
2011 return err;
2012}
2013
2014static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2015{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002016 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2018}
2019
2020static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2021 struct nfs_pathconf *pathconf)
2022{
2023 struct nfs4_pathconf_arg args = {
2024 .fh = fhandle,
2025 .bitmask = server->attr_bitmask,
2026 };
2027 struct rpc_message msg = {
2028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2029 .rpc_argp = &args,
2030 .rpc_resp = pathconf,
2031 };
2032
2033 /* None of the pathconf attributes are mandatory to implement */
2034 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2035 memset(pathconf, 0, sizeof(*pathconf));
2036 return 0;
2037 }
2038
Trond Myklebust0e574af2005-10-27 22:12:38 -04002039 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return rpc_call_sync(server->client, &msg, 0);
2041}
2042
2043static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2044 struct nfs_pathconf *pathconf)
2045{
2046 struct nfs4_exception exception = { };
2047 int err;
2048
2049 do {
2050 err = nfs4_handle_exception(server,
2051 _nfs4_proc_pathconf(server, fhandle, pathconf),
2052 &exception);
2053 } while (exception.retry);
2054 return err;
2055}
2056
2057static void
2058nfs4_read_done(struct rpc_task *task)
2059{
2060 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
2061 struct inode *inode = data->inode;
2062
2063 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2064 rpc_restart_call(task);
2065 return;
2066 }
2067 if (task->tk_status > 0)
2068 renew_lease(NFS_SERVER(inode), data->timestamp);
2069 /* Call back common NFS readpage processing */
2070 nfs_readpage_result(task);
2071}
2072
2073static void
2074nfs4_proc_read_setup(struct nfs_read_data *data)
2075{
2076 struct rpc_task *task = &data->task;
2077 struct rpc_message msg = {
2078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2079 .rpc_argp = &data->args,
2080 .rpc_resp = &data->res,
2081 .rpc_cred = data->cred,
2082 };
2083 struct inode *inode = data->inode;
2084 int flags;
2085
2086 data->timestamp = jiffies;
2087
2088 /* N.B. Do we need to test? Never called for swapfile inode */
2089 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2090
2091 /* Finalize the task. */
2092 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2093 rpc_call_setup(task, &msg, 0);
2094}
2095
2096static void
2097nfs4_write_done(struct rpc_task *task)
2098{
2099 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2100 struct inode *inode = data->inode;
2101
2102 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2103 rpc_restart_call(task);
2104 return;
2105 }
2106 if (task->tk_status >= 0)
2107 renew_lease(NFS_SERVER(inode), data->timestamp);
2108 /* Call back common NFS writeback processing */
2109 nfs_writeback_done(task);
2110}
2111
2112static void
2113nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2114{
2115 struct rpc_task *task = &data->task;
2116 struct rpc_message msg = {
2117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2118 .rpc_argp = &data->args,
2119 .rpc_resp = &data->res,
2120 .rpc_cred = data->cred,
2121 };
2122 struct inode *inode = data->inode;
2123 int stable;
2124 int flags;
2125
2126 if (how & FLUSH_STABLE) {
2127 if (!NFS_I(inode)->ncommit)
2128 stable = NFS_FILE_SYNC;
2129 else
2130 stable = NFS_DATA_SYNC;
2131 } else
2132 stable = NFS_UNSTABLE;
2133 data->args.stable = stable;
2134
2135 data->timestamp = jiffies;
2136
2137 /* Set the initial flags for the task. */
2138 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2139
2140 /* Finalize the task. */
2141 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2142 rpc_call_setup(task, &msg, 0);
2143}
2144
2145static void
2146nfs4_commit_done(struct rpc_task *task)
2147{
2148 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2149 struct inode *inode = data->inode;
2150
2151 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2152 rpc_restart_call(task);
2153 return;
2154 }
2155 /* Call back common NFS writeback processing */
2156 nfs_commit_done(task);
2157}
2158
2159static void
2160nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2161{
2162 struct rpc_task *task = &data->task;
2163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2165 .rpc_argp = &data->args,
2166 .rpc_resp = &data->res,
2167 .rpc_cred = data->cred,
2168 };
2169 struct inode *inode = data->inode;
2170 int flags;
2171
2172 /* Set the initial flags for the task. */
2173 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2174
2175 /* Finalize the task. */
2176 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2177 rpc_call_setup(task, &msg, 0);
2178}
2179
2180/*
2181 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2182 * standalone procedure for queueing an asynchronous RENEW.
2183 */
2184static void
2185renew_done(struct rpc_task *task)
2186{
2187 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2188 unsigned long timestamp = (unsigned long)task->tk_calldata;
2189
2190 if (task->tk_status < 0) {
2191 switch (task->tk_status) {
2192 case -NFS4ERR_STALE_CLIENTID:
2193 case -NFS4ERR_EXPIRED:
2194 case -NFS4ERR_CB_PATH_DOWN:
2195 nfs4_schedule_state_recovery(clp);
2196 }
2197 return;
2198 }
2199 spin_lock(&clp->cl_lock);
2200 if (time_before(clp->cl_last_renewal,timestamp))
2201 clp->cl_last_renewal = timestamp;
2202 spin_unlock(&clp->cl_lock);
2203}
2204
2205int
2206nfs4_proc_async_renew(struct nfs4_client *clp)
2207{
2208 struct rpc_message msg = {
2209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2210 .rpc_argp = clp,
2211 .rpc_cred = clp->cl_cred,
2212 };
2213
2214 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2215 renew_done, (void *)jiffies);
2216}
2217
2218int
2219nfs4_proc_renew(struct nfs4_client *clp)
2220{
2221 struct rpc_message msg = {
2222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2223 .rpc_argp = clp,
2224 .rpc_cred = clp->cl_cred,
2225 };
2226 unsigned long now = jiffies;
2227 int status;
2228
2229 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2230 if (status < 0)
2231 return status;
2232 spin_lock(&clp->cl_lock);
2233 if (time_before(clp->cl_last_renewal,now))
2234 clp->cl_last_renewal = now;
2235 spin_unlock(&clp->cl_lock);
2236 return 0;
2237}
2238
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002239static inline int nfs4_server_supports_acls(struct nfs_server *server)
2240{
2241 return (server->caps & NFS_CAP_ACLS)
2242 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2243 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2244}
2245
2246/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2247 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2248 * the stack.
2249 */
2250#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2251
2252static void buf_to_pages(const void *buf, size_t buflen,
2253 struct page **pages, unsigned int *pgbase)
2254{
2255 const void *p = buf;
2256
2257 *pgbase = offset_in_page(buf);
2258 p -= *pgbase;
2259 while (p < buf + buflen) {
2260 *(pages++) = virt_to_page(p);
2261 p += PAGE_CACHE_SIZE;
2262 }
2263}
2264
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002265struct nfs4_cached_acl {
2266 int cached;
2267 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002268 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002269};
2270
2271static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002272{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002273 struct nfs_inode *nfsi = NFS_I(inode);
2274
2275 spin_lock(&inode->i_lock);
2276 kfree(nfsi->nfs4_acl);
2277 nfsi->nfs4_acl = acl;
2278 spin_unlock(&inode->i_lock);
2279}
2280
2281static void nfs4_zap_acl_attr(struct inode *inode)
2282{
2283 nfs4_set_cached_acl(inode, NULL);
2284}
2285
2286static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2287{
2288 struct nfs_inode *nfsi = NFS_I(inode);
2289 struct nfs4_cached_acl *acl;
2290 int ret = -ENOENT;
2291
2292 spin_lock(&inode->i_lock);
2293 acl = nfsi->nfs4_acl;
2294 if (acl == NULL)
2295 goto out;
2296 if (buf == NULL) /* user is just asking for length */
2297 goto out_len;
2298 if (acl->cached == 0)
2299 goto out;
2300 ret = -ERANGE; /* see getxattr(2) man page */
2301 if (acl->len > buflen)
2302 goto out;
2303 memcpy(buf, acl->data, acl->len);
2304out_len:
2305 ret = acl->len;
2306out:
2307 spin_unlock(&inode->i_lock);
2308 return ret;
2309}
2310
2311static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2312{
2313 struct nfs4_cached_acl *acl;
2314
2315 if (buf && acl_len <= PAGE_SIZE) {
2316 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2317 if (acl == NULL)
2318 goto out;
2319 acl->cached = 1;
2320 memcpy(acl->data, buf, acl_len);
2321 } else {
2322 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2323 if (acl == NULL)
2324 goto out;
2325 acl->cached = 0;
2326 }
2327 acl->len = acl_len;
2328out:
2329 nfs4_set_cached_acl(inode, acl);
2330}
2331
2332static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2333{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002334 struct page *pages[NFS4ACL_MAXPAGES];
2335 struct nfs_getaclargs args = {
2336 .fh = NFS_FH(inode),
2337 .acl_pages = pages,
2338 .acl_len = buflen,
2339 };
2340 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002341 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002342 struct rpc_message msg = {
2343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2344 .rpc_argp = &args,
2345 .rpc_resp = &resp_len,
2346 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002347 struct page *localpage = NULL;
2348 int ret;
2349
2350 if (buflen < PAGE_SIZE) {
2351 /* As long as we're doing a round trip to the server anyway,
2352 * let's be prepared for a page of acl data. */
2353 localpage = alloc_page(GFP_KERNEL);
2354 resp_buf = page_address(localpage);
2355 if (localpage == NULL)
2356 return -ENOMEM;
2357 args.acl_pages[0] = localpage;
2358 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002359 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002360 } else {
2361 resp_buf = buf;
2362 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2363 }
2364 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2365 if (ret)
2366 goto out_free;
2367 if (resp_len > args.acl_len)
2368 nfs4_write_cached_acl(inode, NULL, resp_len);
2369 else
2370 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2371 if (buf) {
2372 ret = -ERANGE;
2373 if (resp_len > buflen)
2374 goto out_free;
2375 if (localpage)
2376 memcpy(buf, resp_buf, resp_len);
2377 }
2378 ret = resp_len;
2379out_free:
2380 if (localpage)
2381 __free_page(localpage);
2382 return ret;
2383}
2384
2385static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2386{
2387 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002388 int ret;
2389
2390 if (!nfs4_server_supports_acls(server))
2391 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002392 ret = nfs_revalidate_inode(server, inode);
2393 if (ret < 0)
2394 return ret;
2395 ret = nfs4_read_cached_acl(inode, buf, buflen);
2396 if (ret != -ENOENT)
2397 return ret;
2398 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002399}
2400
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002401static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2402{
2403 struct nfs_server *server = NFS_SERVER(inode);
2404 struct page *pages[NFS4ACL_MAXPAGES];
2405 struct nfs_setaclargs arg = {
2406 .fh = NFS_FH(inode),
2407 .acl_pages = pages,
2408 .acl_len = buflen,
2409 };
2410 struct rpc_message msg = {
2411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2412 .rpc_argp = &arg,
2413 .rpc_resp = NULL,
2414 };
2415 int ret;
2416
2417 if (!nfs4_server_supports_acls(server))
2418 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002419 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002420 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2421 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002422 if (ret == 0)
2423 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002424 return ret;
2425}
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002428nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
2430 struct nfs4_client *clp = server->nfs4_state;
2431
2432 if (!clp || task->tk_status >= 0)
2433 return 0;
2434 switch(task->tk_status) {
2435 case -NFS4ERR_STALE_CLIENTID:
2436 case -NFS4ERR_STALE_STATEID:
2437 case -NFS4ERR_EXPIRED:
2438 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2439 nfs4_schedule_state_recovery(clp);
2440 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2441 rpc_wake_up_task(task);
2442 task->tk_status = 0;
2443 return -EAGAIN;
2444 case -NFS4ERR_GRACE:
2445 case -NFS4ERR_DELAY:
2446 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2447 task->tk_status = 0;
2448 return -EAGAIN;
2449 case -NFS4ERR_OLD_STATEID:
2450 task->tk_status = 0;
2451 return -EAGAIN;
2452 }
2453 task->tk_status = nfs4_map_errors(task->tk_status);
2454 return 0;
2455}
2456
2457static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2458{
2459 DEFINE_WAIT(wait);
2460 sigset_t oldset;
2461 int interruptible, res = 0;
2462
2463 might_sleep();
2464
2465 rpc_clnt_sigmask(clnt, &oldset);
2466 interruptible = TASK_UNINTERRUPTIBLE;
2467 if (clnt->cl_intr)
2468 interruptible = TASK_INTERRUPTIBLE;
2469 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2470 nfs4_schedule_state_recovery(clp);
2471 if (clnt->cl_intr && signalled())
2472 res = -ERESTARTSYS;
2473 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2474 schedule();
2475 finish_wait(&clp->cl_waitq, &wait);
2476 rpc_clnt_sigunmask(clnt, &oldset);
2477 return res;
2478}
2479
2480static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2481{
2482 sigset_t oldset;
2483 int res = 0;
2484
2485 might_sleep();
2486
2487 if (*timeout <= 0)
2488 *timeout = NFS4_POLL_RETRY_MIN;
2489 if (*timeout > NFS4_POLL_RETRY_MAX)
2490 *timeout = NFS4_POLL_RETRY_MAX;
2491 rpc_clnt_sigmask(clnt, &oldset);
2492 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002493 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (signalled())
2495 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002496 } else
2497 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 rpc_clnt_sigunmask(clnt, &oldset);
2499 *timeout <<= 1;
2500 return res;
2501}
2502
2503/* This is the error handling routine for processes that are allowed
2504 * to sleep.
2505 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002506int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 struct nfs4_client *clp = server->nfs4_state;
2509 int ret = errorcode;
2510
2511 exception->retry = 0;
2512 switch(errorcode) {
2513 case 0:
2514 return 0;
2515 case -NFS4ERR_STALE_CLIENTID:
2516 case -NFS4ERR_STALE_STATEID:
2517 case -NFS4ERR_EXPIRED:
2518 ret = nfs4_wait_clnt_recover(server->client, clp);
2519 if (ret == 0)
2520 exception->retry = 1;
2521 break;
2522 case -NFS4ERR_GRACE:
2523 case -NFS4ERR_DELAY:
2524 ret = nfs4_delay(server->client, &exception->timeout);
2525 if (ret == 0)
2526 exception->retry = 1;
2527 break;
2528 case -NFS4ERR_OLD_STATEID:
2529 if (ret == 0)
2530 exception->retry = 1;
2531 }
2532 /* We failed to handle the error */
2533 return nfs4_map_errors(ret);
2534}
2535
2536int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2537{
2538 nfs4_verifier sc_verifier;
2539 struct nfs4_setclientid setclientid = {
2540 .sc_verifier = &sc_verifier,
2541 .sc_prog = program,
2542 };
2543 struct rpc_message msg = {
2544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2545 .rpc_argp = &setclientid,
2546 .rpc_resp = clp,
2547 .rpc_cred = clp->cl_cred,
2548 };
2549 u32 *p;
2550 int loop = 0;
2551 int status;
2552
2553 p = (u32*)sc_verifier.data;
2554 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2555 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2556
2557 for(;;) {
2558 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2559 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2560 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2561 clp->cl_cred->cr_ops->cr_name,
2562 clp->cl_id_uniquifier);
2563 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2564 sizeof(setclientid.sc_netid), "tcp");
2565 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2566 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2567 clp->cl_ipaddr, port >> 8, port & 255);
2568
2569 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2570 if (status != -NFS4ERR_CLID_INUSE)
2571 break;
2572 if (signalled())
2573 break;
2574 if (loop++ & 1)
2575 ssleep(clp->cl_lease_time + 1);
2576 else
2577 if (++clp->cl_id_uniquifier == 0)
2578 break;
2579 }
2580 return status;
2581}
2582
2583int
2584nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2585{
2586 struct nfs_fsinfo fsinfo;
2587 struct rpc_message msg = {
2588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2589 .rpc_argp = clp,
2590 .rpc_resp = &fsinfo,
2591 .rpc_cred = clp->cl_cred,
2592 };
2593 unsigned long now;
2594 int status;
2595
2596 now = jiffies;
2597 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2598 if (status == 0) {
2599 spin_lock(&clp->cl_lock);
2600 clp->cl_lease_time = fsinfo.lease_time * HZ;
2601 clp->cl_last_renewal = now;
2602 spin_unlock(&clp->cl_lock);
2603 }
2604 return status;
2605}
2606
2607static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2608{
2609 struct nfs4_delegreturnargs args = {
2610 .fhandle = NFS_FH(inode),
2611 .stateid = stateid,
2612 };
2613 struct rpc_message msg = {
2614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2615 .rpc_argp = &args,
2616 .rpc_cred = cred,
2617 };
2618
2619 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2620}
2621
2622int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2623{
2624 struct nfs_server *server = NFS_SERVER(inode);
2625 struct nfs4_exception exception = { };
2626 int err;
2627 do {
2628 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2629 switch (err) {
2630 case -NFS4ERR_STALE_STATEID:
2631 case -NFS4ERR_EXPIRED:
2632 nfs4_schedule_state_recovery(server->nfs4_state);
2633 case 0:
2634 return 0;
2635 }
2636 err = nfs4_handle_exception(server, err, &exception);
2637 } while (exception.retry);
2638 return err;
2639}
2640
2641#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2642#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2643
2644/*
2645 * sleep, with exponential backoff, and retry the LOCK operation.
2646 */
2647static unsigned long
2648nfs4_set_lock_task_retry(unsigned long timeout)
2649{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002650 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 timeout <<= 1;
2652 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2653 return NFS4_LOCK_MAXTIMEOUT;
2654 return timeout;
2655}
2656
2657static inline int
2658nfs4_lck_type(int cmd, struct file_lock *request)
2659{
2660 /* set lock type */
2661 switch (request->fl_type) {
2662 case F_RDLCK:
2663 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2664 case F_WRLCK:
2665 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2666 case F_UNLCK:
2667 return NFS4_WRITE_LT;
2668 }
2669 BUG();
2670 return 0;
2671}
2672
2673static inline uint64_t
2674nfs4_lck_length(struct file_lock *request)
2675{
2676 if (request->fl_end == OFFSET_MAX)
2677 return ~(uint64_t)0;
2678 return request->fl_end - request->fl_start + 1;
2679}
2680
2681static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2682{
2683 struct inode *inode = state->inode;
2684 struct nfs_server *server = NFS_SERVER(inode);
2685 struct nfs4_client *clp = server->nfs4_state;
2686 struct nfs_lockargs arg = {
2687 .fh = NFS_FH(inode),
2688 .type = nfs4_lck_type(cmd, request),
2689 .offset = request->fl_start,
2690 .length = nfs4_lck_length(request),
2691 };
2692 struct nfs_lockres res = {
2693 .server = server,
2694 };
2695 struct rpc_message msg = {
2696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2697 .rpc_argp = &arg,
2698 .rpc_resp = &res,
2699 .rpc_cred = state->owner->so_cred,
2700 };
2701 struct nfs_lowner nlo;
2702 struct nfs4_lock_state *lsp;
2703 int status;
2704
2705 down_read(&clp->cl_sem);
2706 nlo.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002707 status = nfs4_set_lock_state(state, request);
2708 if (status != 0)
2709 goto out;
2710 lsp = request->fl_u.nfs4_fl.owner;
2711 nlo.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 arg.u.lockt = &nlo;
2713 status = rpc_call_sync(server->client, &msg, 0);
2714 if (!status) {
2715 request->fl_type = F_UNLCK;
2716 } else if (status == -NFS4ERR_DENIED) {
2717 int64_t len, start, end;
2718 start = res.u.denied.offset;
2719 len = res.u.denied.length;
2720 end = start + len - 1;
2721 if (end < 0 || len == 0)
2722 request->fl_end = OFFSET_MAX;
2723 else
2724 request->fl_end = (loff_t)end;
2725 request->fl_start = (loff_t)start;
2726 request->fl_type = F_WRLCK;
2727 if (res.u.denied.type & 1)
2728 request->fl_type = F_RDLCK;
2729 request->fl_pid = 0;
2730 status = 0;
2731 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002732out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 up_read(&clp->cl_sem);
2734 return status;
2735}
2736
2737static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2738{
2739 struct nfs4_exception exception = { };
2740 int err;
2741
2742 do {
2743 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2744 _nfs4_proc_getlk(state, cmd, request),
2745 &exception);
2746 } while (exception.retry);
2747 return err;
2748}
2749
2750static int do_vfs_lock(struct file *file, struct file_lock *fl)
2751{
2752 int res = 0;
2753 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2754 case FL_POSIX:
2755 res = posix_lock_file_wait(file, fl);
2756 break;
2757 case FL_FLOCK:
2758 res = flock_lock_file_wait(file, fl);
2759 break;
2760 default:
2761 BUG();
2762 }
2763 if (res < 0)
2764 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2765 __FUNCTION__);
2766 return res;
2767}
2768
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002769struct nfs4_unlockdata {
2770 struct nfs_lockargs arg;
2771 struct nfs_locku_opargs luargs;
2772 struct nfs_lockres res;
2773 struct nfs4_lock_state *lsp;
2774 struct nfs_open_context *ctx;
2775 atomic_t refcount;
2776 struct completion completion;
2777};
2778
2779static void nfs4_locku_release_calldata(struct nfs4_unlockdata *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002781 if (atomic_dec_and_test(&calldata->refcount)) {
2782 nfs_free_seqid(calldata->luargs.seqid);
2783 nfs4_put_lock_state(calldata->lsp);
2784 put_nfs_open_context(calldata->ctx);
2785 kfree(calldata);
2786 }
2787}
2788
2789static void nfs4_locku_complete(struct nfs4_unlockdata *calldata)
2790{
2791 complete(&calldata->completion);
2792 nfs4_locku_release_calldata(calldata);
2793}
2794
2795static void nfs4_locku_done(struct rpc_task *task)
2796{
2797 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2798
2799 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2800 switch (task->tk_status) {
2801 case 0:
2802 memcpy(calldata->lsp->ls_stateid.data,
2803 calldata->res.u.stateid.data,
2804 sizeof(calldata->lsp->ls_stateid.data));
2805 break;
2806 case -NFS4ERR_STALE_STATEID:
2807 case -NFS4ERR_EXPIRED:
2808 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2809 break;
2810 default:
2811 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2812 rpc_restart_call(task);
2813 return;
2814 }
2815 }
2816 nfs4_locku_complete(calldata);
2817}
2818
2819static void nfs4_locku_begin(struct rpc_task *task)
2820{
2821 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 struct rpc_message msg = {
2823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002824 .rpc_argp = &calldata->arg,
2825 .rpc_resp = &calldata->res,
2826 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 };
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002828 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002830 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2831 if (status != 0)
2832 return;
2833 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
2834 nfs4_locku_complete(calldata);
2835 task->tk_exit = NULL;
2836 rpc_exit(task, 0);
2837 return;
2838 }
2839 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2843{
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002844 struct nfs4_unlockdata *calldata;
2845 struct inode *inode = state->inode;
2846 struct nfs_server *server = NFS_SERVER(inode);
2847 struct nfs4_lock_state *lsp;
2848 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002850 status = nfs4_set_lock_state(state, request);
2851 if (status != 0)
2852 return status;
2853 lsp = request->fl_u.nfs4_fl.owner;
2854 /* We might have lost the locks! */
2855 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2856 return 0;
2857 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2858 if (calldata == NULL)
2859 return -ENOMEM;
2860 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2861 if (calldata->luargs.seqid == NULL) {
2862 kfree(calldata);
2863 return -ENOMEM;
2864 }
2865 calldata->luargs.stateid = &lsp->ls_stateid;
2866 calldata->arg.fh = NFS_FH(inode);
2867 calldata->arg.type = nfs4_lck_type(cmd, request);
2868 calldata->arg.offset = request->fl_start;
2869 calldata->arg.length = nfs4_lck_length(request);
2870 calldata->arg.u.locku = &calldata->luargs;
2871 calldata->res.server = server;
2872 calldata->lsp = lsp;
2873 atomic_inc(&lsp->ls_count);
2874
2875 /* Ensure we don't close file until we're done freeing locks! */
2876 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
2877
2878 atomic_set(&calldata->refcount, 2);
2879 init_completion(&calldata->completion);
2880
2881 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_locku_begin,
2882 nfs4_locku_done, calldata);
2883 if (status == 0)
2884 wait_for_completion_interruptible(&calldata->completion);
2885 do_vfs_lock(request->fl_file, request);
2886 nfs4_locku_release_calldata(calldata);
2887 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
2890static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2891{
2892 struct inode *inode = state->inode;
2893 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002894 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06735b32005-10-18 14:20:15 -07002895 struct nfs_lock_opargs largs = {
2896 .lock_stateid = &lsp->ls_stateid,
2897 .open_stateid = &state->stateid,
2898 .lock_owner = {
2899 .clientid = server->nfs4_state->cl_clientid,
2900 .id = lsp->ls_id,
2901 },
2902 .reclaim = reclaim,
2903 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 struct nfs_lockargs arg = {
2905 .fh = NFS_FH(inode),
2906 .type = nfs4_lck_type(cmd, request),
2907 .offset = request->fl_start,
2908 .length = nfs4_lck_length(request),
Trond Myklebust06735b32005-10-18 14:20:15 -07002909 .u = {
2910 .lock = &largs,
2911 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 };
2913 struct nfs_lockres res = {
2914 .server = server,
2915 };
2916 struct rpc_message msg = {
2917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2918 .rpc_argp = &arg,
2919 .rpc_resp = &res,
2920 .rpc_cred = state->owner->so_cred,
2921 };
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002922 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Trond Myklebust06735b32005-10-18 14:20:15 -07002924 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2925 if (largs.lock_seqid == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002926 return -ENOMEM;
2927 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 struct nfs4_state_owner *owner = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Trond Myklebust06735b32005-10-18 14:20:15 -07002930 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2931 if (largs.open_seqid == NULL)
2932 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 largs.new_lock_owner = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebust06735b32005-10-18 14:20:15 -07002935 /* increment open seqid on success, and seqid mutating errors */
2936 if (largs.new_lock_owner != 0) {
2937 nfs_increment_open_seqid(status, largs.open_seqid);
2938 if (status == 0)
2939 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2940 }
2941 nfs_free_seqid(largs.open_seqid);
2942 } else
2943 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2944 /* increment lock seqid on success, and seqid mutating errors*/
2945 nfs_increment_lock_seqid(status, largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 /* save the returned stateid. */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002947 if (status == 0) {
Trond Myklebust06735b32005-10-18 14:20:15 -07002948 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2949 sizeof(lsp->ls_stateid.data));
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002950 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2951 } else if (status == -NFS4ERR_DENIED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 status = -EAGAIN;
Trond Myklebust06735b32005-10-18 14:20:15 -07002953out:
2954 nfs_free_seqid(largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return status;
2956}
2957
2958static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2959{
Trond Myklebust202b50d2005-06-22 17:16:29 +00002960 struct nfs_server *server = NFS_SERVER(state->inode);
2961 struct nfs4_exception exception = { };
2962 int err;
2963
2964 do {
2965 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2966 if (err != -NFS4ERR_DELAY)
2967 break;
2968 nfs4_handle_exception(server, err, &exception);
2969 } while (exception.retry);
2970 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
2973static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2974{
Trond Myklebust202b50d2005-06-22 17:16:29 +00002975 struct nfs_server *server = NFS_SERVER(state->inode);
2976 struct nfs4_exception exception = { };
2977 int err;
2978
2979 do {
2980 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2981 if (err != -NFS4ERR_DELAY)
2982 break;
2983 nfs4_handle_exception(server, err, &exception);
2984 } while (exception.retry);
2985 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2989{
2990 struct nfs4_client *clp = state->owner->so_client;
2991 int status;
2992
2993 down_read(&clp->cl_sem);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002994 status = nfs4_set_lock_state(state, request);
2995 if (status == 0)
2996 status = _nfs4_do_setlk(state, cmd, request, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 if (status == 0) {
2998 /* Note: we always want to sleep here! */
2999 request->fl_flags |= FL_SLEEP;
3000 if (do_vfs_lock(request->fl_file, request) < 0)
3001 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3002 }
3003 up_read(&clp->cl_sem);
3004 return status;
3005}
3006
3007static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3008{
3009 struct nfs4_exception exception = { };
3010 int err;
3011
3012 do {
3013 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3014 _nfs4_proc_setlk(state, cmd, request),
3015 &exception);
3016 } while (exception.retry);
3017 return err;
3018}
3019
3020static int
3021nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3022{
3023 struct nfs_open_context *ctx;
3024 struct nfs4_state *state;
3025 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3026 int status;
3027
3028 /* verify open state */
3029 ctx = (struct nfs_open_context *)filp->private_data;
3030 state = ctx->state;
3031
3032 if (request->fl_start < 0 || request->fl_end < 0)
3033 return -EINVAL;
3034
3035 if (IS_GETLK(cmd))
3036 return nfs4_proc_getlk(state, F_GETLK, request);
3037
3038 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3039 return -EINVAL;
3040
3041 if (request->fl_type == F_UNLCK)
3042 return nfs4_proc_unlck(state, cmd, request);
3043
3044 do {
3045 status = nfs4_proc_setlk(state, cmd, request);
3046 if ((status != -EAGAIN) || IS_SETLK(cmd))
3047 break;
3048 timeout = nfs4_set_lock_task_retry(timeout);
3049 status = -ERESTARTSYS;
3050 if (signalled())
3051 break;
3052 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return status;
3054}
3055
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003056
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003057#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3058
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003059int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3060 size_t buflen, int flags)
3061{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003062 struct inode *inode = dentry->d_inode;
3063
3064 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3065 return -EOPNOTSUPP;
3066
3067 if (!S_ISREG(inode->i_mode) &&
3068 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3069 return -EPERM;
3070
3071 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003072}
3073
3074/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3075 * and that's what we'll do for e.g. user attributes that haven't been set.
3076 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3077 * attributes in kernel-managed attribute namespaces. */
3078ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3079 size_t buflen)
3080{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003081 struct inode *inode = dentry->d_inode;
3082
3083 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3084 return -EOPNOTSUPP;
3085
3086 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003087}
3088
3089ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3090{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003091 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003092
3093 if (buf && buflen < len)
3094 return -ERANGE;
3095 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003096 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3097 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003098}
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3101 .recover_open = nfs4_open_reclaim,
3102 .recover_lock = nfs4_lock_reclaim,
3103};
3104
3105struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3106 .recover_open = nfs4_open_expired,
3107 .recover_lock = nfs4_lock_expired,
3108};
3109
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003110static struct inode_operations nfs4_file_inode_operations = {
3111 .permission = nfs_permission,
3112 .getattr = nfs_getattr,
3113 .setattr = nfs_setattr,
3114 .getxattr = nfs4_getxattr,
3115 .setxattr = nfs4_setxattr,
3116 .listxattr = nfs4_listxattr,
3117};
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119struct nfs_rpc_ops nfs_v4_clientops = {
3120 .version = 4, /* protocol version */
3121 .dentry_ops = &nfs4_dentry_operations,
3122 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003123 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 .getroot = nfs4_proc_get_root,
3125 .getattr = nfs4_proc_getattr,
3126 .setattr = nfs4_proc_setattr,
3127 .lookup = nfs4_proc_lookup,
3128 .access = nfs4_proc_access,
3129 .readlink = nfs4_proc_readlink,
3130 .read = nfs4_proc_read,
3131 .write = nfs4_proc_write,
3132 .commit = nfs4_proc_commit,
3133 .create = nfs4_proc_create,
3134 .remove = nfs4_proc_remove,
3135 .unlink_setup = nfs4_proc_unlink_setup,
3136 .unlink_done = nfs4_proc_unlink_done,
3137 .rename = nfs4_proc_rename,
3138 .link = nfs4_proc_link,
3139 .symlink = nfs4_proc_symlink,
3140 .mkdir = nfs4_proc_mkdir,
3141 .rmdir = nfs4_proc_remove,
3142 .readdir = nfs4_proc_readdir,
3143 .mknod = nfs4_proc_mknod,
3144 .statfs = nfs4_proc_statfs,
3145 .fsinfo = nfs4_proc_fsinfo,
3146 .pathconf = nfs4_proc_pathconf,
3147 .decode_dirent = nfs4_decode_dirent,
3148 .read_setup = nfs4_proc_read_setup,
3149 .write_setup = nfs4_proc_write_setup,
3150 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003151 .file_open = nfs_open,
3152 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003154 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155};
3156
3157/*
3158 * Local variables:
3159 * c-basic-offset: 8
3160 * End:
3161 */