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